xref: /openbmc/linux/kernel/power/user.c (revision e23feb16)
1 /*
2  * linux/kernel/power/user.c
3  *
4  * This file provides the user space interface for software suspend/resume.
5  *
6  * Copyright (C) 2006 Rafael J. Wysocki <rjw@sisk.pl>
7  *
8  * This file is released under the GPLv2.
9  *
10  */
11 
12 #include <linux/suspend.h>
13 #include <linux/syscalls.h>
14 #include <linux/reboot.h>
15 #include <linux/string.h>
16 #include <linux/device.h>
17 #include <linux/miscdevice.h>
18 #include <linux/mm.h>
19 #include <linux/swap.h>
20 #include <linux/swapops.h>
21 #include <linux/pm.h>
22 #include <linux/fs.h>
23 #include <linux/compat.h>
24 #include <linux/console.h>
25 #include <linux/cpu.h>
26 #include <linux/freezer.h>
27 
28 #include <asm/uaccess.h>
29 
30 #include "power.h"
31 
32 
33 #define SNAPSHOT_MINOR	231
34 
35 static struct snapshot_data {
36 	struct snapshot_handle handle;
37 	int swap;
38 	int mode;
39 	char frozen;
40 	char ready;
41 	char platform_support;
42 	bool free_bitmaps;
43 } snapshot_state;
44 
45 atomic_t snapshot_device_available = ATOMIC_INIT(1);
46 
47 static int snapshot_open(struct inode *inode, struct file *filp)
48 {
49 	struct snapshot_data *data;
50 	int error;
51 
52 	lock_system_sleep();
53 
54 	if (!atomic_add_unless(&snapshot_device_available, -1, 0)) {
55 		error = -EBUSY;
56 		goto Unlock;
57 	}
58 
59 	if ((filp->f_flags & O_ACCMODE) == O_RDWR) {
60 		atomic_inc(&snapshot_device_available);
61 		error = -ENOSYS;
62 		goto Unlock;
63 	}
64 	nonseekable_open(inode, filp);
65 	data = &snapshot_state;
66 	filp->private_data = data;
67 	memset(&data->handle, 0, sizeof(struct snapshot_handle));
68 	if ((filp->f_flags & O_ACCMODE) == O_RDONLY) {
69 		/* Hibernating.  The image device should be accessible. */
70 		data->swap = swsusp_resume_device ?
71 			swap_type_of(swsusp_resume_device, 0, NULL) : -1;
72 		data->mode = O_RDONLY;
73 		error = pm_notifier_call_chain(PM_HIBERNATION_PREPARE);
74 		if (error)
75 			pm_notifier_call_chain(PM_POST_HIBERNATION);
76 	} else {
77 		/*
78 		 * Resuming.  We may need to wait for the image device to
79 		 * appear.
80 		 */
81 		wait_for_device_probe();
82 
83 		data->swap = -1;
84 		data->mode = O_WRONLY;
85 		error = pm_notifier_call_chain(PM_RESTORE_PREPARE);
86 		if (!error) {
87 			error = create_basic_memory_bitmaps();
88 			data->free_bitmaps = !error;
89 		}
90 		if (error)
91 			pm_notifier_call_chain(PM_POST_RESTORE);
92 	}
93 	if (error)
94 		atomic_inc(&snapshot_device_available);
95 
96 	data->frozen = 0;
97 	data->ready = 0;
98 	data->platform_support = 0;
99 
100  Unlock:
101 	unlock_system_sleep();
102 
103 	return error;
104 }
105 
106 static int snapshot_release(struct inode *inode, struct file *filp)
107 {
108 	struct snapshot_data *data;
109 
110 	lock_system_sleep();
111 
112 	swsusp_free();
113 	data = filp->private_data;
114 	free_all_swap_pages(data->swap);
115 	if (data->frozen) {
116 		pm_restore_gfp_mask();
117 		free_basic_memory_bitmaps();
118 		thaw_processes();
119 	} else if (data->free_bitmaps) {
120 		free_basic_memory_bitmaps();
121 	}
122 	pm_notifier_call_chain(data->mode == O_RDONLY ?
123 			PM_POST_HIBERNATION : PM_POST_RESTORE);
124 	atomic_inc(&snapshot_device_available);
125 
126 	unlock_system_sleep();
127 
128 	return 0;
129 }
130 
131 static ssize_t snapshot_read(struct file *filp, char __user *buf,
132                              size_t count, loff_t *offp)
133 {
134 	struct snapshot_data *data;
135 	ssize_t res;
136 	loff_t pg_offp = *offp & ~PAGE_MASK;
137 
138 	lock_system_sleep();
139 
140 	data = filp->private_data;
141 	if (!data->ready) {
142 		res = -ENODATA;
143 		goto Unlock;
144 	}
145 	if (!pg_offp) { /* on page boundary? */
146 		res = snapshot_read_next(&data->handle);
147 		if (res <= 0)
148 			goto Unlock;
149 	} else {
150 		res = PAGE_SIZE - pg_offp;
151 	}
152 
153 	res = simple_read_from_buffer(buf, count, &pg_offp,
154 			data_of(data->handle), res);
155 	if (res > 0)
156 		*offp += res;
157 
158  Unlock:
159 	unlock_system_sleep();
160 
161 	return res;
162 }
163 
164 static ssize_t snapshot_write(struct file *filp, const char __user *buf,
165                               size_t count, loff_t *offp)
166 {
167 	struct snapshot_data *data;
168 	ssize_t res;
169 	loff_t pg_offp = *offp & ~PAGE_MASK;
170 
171 	lock_system_sleep();
172 
173 	data = filp->private_data;
174 
175 	if (!pg_offp) {
176 		res = snapshot_write_next(&data->handle);
177 		if (res <= 0)
178 			goto unlock;
179 	} else {
180 		res = PAGE_SIZE - pg_offp;
181 	}
182 
183 	res = simple_write_to_buffer(data_of(data->handle), res, &pg_offp,
184 			buf, count);
185 	if (res > 0)
186 		*offp += res;
187 unlock:
188 	unlock_system_sleep();
189 
190 	return res;
191 }
192 
193 static long snapshot_ioctl(struct file *filp, unsigned int cmd,
194 							unsigned long arg)
195 {
196 	int error = 0;
197 	struct snapshot_data *data;
198 	loff_t size;
199 	sector_t offset;
200 
201 	if (_IOC_TYPE(cmd) != SNAPSHOT_IOC_MAGIC)
202 		return -ENOTTY;
203 	if (_IOC_NR(cmd) > SNAPSHOT_IOC_MAXNR)
204 		return -ENOTTY;
205 	if (!capable(CAP_SYS_ADMIN))
206 		return -EPERM;
207 
208 	if (!mutex_trylock(&pm_mutex))
209 		return -EBUSY;
210 
211 	lock_device_hotplug();
212 	data = filp->private_data;
213 
214 	switch (cmd) {
215 
216 	case SNAPSHOT_FREEZE:
217 		if (data->frozen)
218 			break;
219 
220 		printk("Syncing filesystems ... ");
221 		sys_sync();
222 		printk("done.\n");
223 
224 		error = freeze_processes();
225 		if (error)
226 			break;
227 
228 		error = create_basic_memory_bitmaps();
229 		if (error)
230 			thaw_processes();
231 		else
232 			data->frozen = 1;
233 
234 		break;
235 
236 	case SNAPSHOT_UNFREEZE:
237 		if (!data->frozen || data->ready)
238 			break;
239 		pm_restore_gfp_mask();
240 		free_basic_memory_bitmaps();
241 		data->free_bitmaps = false;
242 		thaw_processes();
243 		data->frozen = 0;
244 		break;
245 
246 	case SNAPSHOT_CREATE_IMAGE:
247 		if (data->mode != O_RDONLY || !data->frozen  || data->ready) {
248 			error = -EPERM;
249 			break;
250 		}
251 		pm_restore_gfp_mask();
252 		error = hibernation_snapshot(data->platform_support);
253 		if (!error) {
254 			error = put_user(in_suspend, (int __user *)arg);
255 			data->ready = !freezer_test_done && !error;
256 			freezer_test_done = false;
257 		}
258 		break;
259 
260 	case SNAPSHOT_ATOMIC_RESTORE:
261 		snapshot_write_finalize(&data->handle);
262 		if (data->mode != O_WRONLY || !data->frozen ||
263 		    !snapshot_image_loaded(&data->handle)) {
264 			error = -EPERM;
265 			break;
266 		}
267 		error = hibernation_restore(data->platform_support);
268 		break;
269 
270 	case SNAPSHOT_FREE:
271 		swsusp_free();
272 		memset(&data->handle, 0, sizeof(struct snapshot_handle));
273 		data->ready = 0;
274 		/*
275 		 * It is necessary to thaw kernel threads here, because
276 		 * SNAPSHOT_CREATE_IMAGE may be invoked directly after
277 		 * SNAPSHOT_FREE.  In that case, if kernel threads were not
278 		 * thawed, the preallocation of memory carried out by
279 		 * hibernation_snapshot() might run into problems (i.e. it
280 		 * might fail or even deadlock).
281 		 */
282 		thaw_kernel_threads();
283 		break;
284 
285 	case SNAPSHOT_PREF_IMAGE_SIZE:
286 		image_size = arg;
287 		break;
288 
289 	case SNAPSHOT_GET_IMAGE_SIZE:
290 		if (!data->ready) {
291 			error = -ENODATA;
292 			break;
293 		}
294 		size = snapshot_get_image_size();
295 		size <<= PAGE_SHIFT;
296 		error = put_user(size, (loff_t __user *)arg);
297 		break;
298 
299 	case SNAPSHOT_AVAIL_SWAP_SIZE:
300 		size = count_swap_pages(data->swap, 1);
301 		size <<= PAGE_SHIFT;
302 		error = put_user(size, (loff_t __user *)arg);
303 		break;
304 
305 	case SNAPSHOT_ALLOC_SWAP_PAGE:
306 		if (data->swap < 0 || data->swap >= MAX_SWAPFILES) {
307 			error = -ENODEV;
308 			break;
309 		}
310 		offset = alloc_swapdev_block(data->swap);
311 		if (offset) {
312 			offset <<= PAGE_SHIFT;
313 			error = put_user(offset, (loff_t __user *)arg);
314 		} else {
315 			error = -ENOSPC;
316 		}
317 		break;
318 
319 	case SNAPSHOT_FREE_SWAP_PAGES:
320 		if (data->swap < 0 || data->swap >= MAX_SWAPFILES) {
321 			error = -ENODEV;
322 			break;
323 		}
324 		free_all_swap_pages(data->swap);
325 		break;
326 
327 	case SNAPSHOT_S2RAM:
328 		if (!data->frozen) {
329 			error = -EPERM;
330 			break;
331 		}
332 		/*
333 		 * Tasks are frozen and the notifiers have been called with
334 		 * PM_HIBERNATION_PREPARE
335 		 */
336 		error = suspend_devices_and_enter(PM_SUSPEND_MEM);
337 		data->ready = 0;
338 		break;
339 
340 	case SNAPSHOT_PLATFORM_SUPPORT:
341 		data->platform_support = !!arg;
342 		break;
343 
344 	case SNAPSHOT_POWER_OFF:
345 		if (data->platform_support)
346 			error = hibernation_platform_enter();
347 		break;
348 
349 	case SNAPSHOT_SET_SWAP_AREA:
350 		if (swsusp_swap_in_use()) {
351 			error = -EPERM;
352 		} else {
353 			struct resume_swap_area swap_area;
354 			dev_t swdev;
355 
356 			error = copy_from_user(&swap_area, (void __user *)arg,
357 					sizeof(struct resume_swap_area));
358 			if (error) {
359 				error = -EFAULT;
360 				break;
361 			}
362 
363 			/*
364 			 * User space encodes device types as two-byte values,
365 			 * so we need to recode them
366 			 */
367 			swdev = new_decode_dev(swap_area.dev);
368 			if (swdev) {
369 				offset = swap_area.offset;
370 				data->swap = swap_type_of(swdev, offset, NULL);
371 				if (data->swap < 0)
372 					error = -ENODEV;
373 			} else {
374 				data->swap = -1;
375 				error = -EINVAL;
376 			}
377 		}
378 		break;
379 
380 	default:
381 		error = -ENOTTY;
382 
383 	}
384 
385 	unlock_device_hotplug();
386 	mutex_unlock(&pm_mutex);
387 
388 	return error;
389 }
390 
391 #ifdef CONFIG_COMPAT
392 
393 struct compat_resume_swap_area {
394 	compat_loff_t offset;
395 	u32 dev;
396 } __packed;
397 
398 static long
399 snapshot_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
400 {
401 	BUILD_BUG_ON(sizeof(loff_t) != sizeof(compat_loff_t));
402 
403 	switch (cmd) {
404 	case SNAPSHOT_GET_IMAGE_SIZE:
405 	case SNAPSHOT_AVAIL_SWAP_SIZE:
406 	case SNAPSHOT_ALLOC_SWAP_PAGE: {
407 		compat_loff_t __user *uoffset = compat_ptr(arg);
408 		loff_t offset;
409 		mm_segment_t old_fs;
410 		int err;
411 
412 		old_fs = get_fs();
413 		set_fs(KERNEL_DS);
414 		err = snapshot_ioctl(file, cmd, (unsigned long) &offset);
415 		set_fs(old_fs);
416 		if (!err && put_user(offset, uoffset))
417 			err = -EFAULT;
418 		return err;
419 	}
420 
421 	case SNAPSHOT_CREATE_IMAGE:
422 		return snapshot_ioctl(file, cmd,
423 				      (unsigned long) compat_ptr(arg));
424 
425 	case SNAPSHOT_SET_SWAP_AREA: {
426 		struct compat_resume_swap_area __user *u_swap_area =
427 			compat_ptr(arg);
428 		struct resume_swap_area swap_area;
429 		mm_segment_t old_fs;
430 		int err;
431 
432 		err = get_user(swap_area.offset, &u_swap_area->offset);
433 		err |= get_user(swap_area.dev, &u_swap_area->dev);
434 		if (err)
435 			return -EFAULT;
436 		old_fs = get_fs();
437 		set_fs(KERNEL_DS);
438 		err = snapshot_ioctl(file, SNAPSHOT_SET_SWAP_AREA,
439 				     (unsigned long) &swap_area);
440 		set_fs(old_fs);
441 		return err;
442 	}
443 
444 	default:
445 		return snapshot_ioctl(file, cmd, arg);
446 	}
447 }
448 
449 #endif /* CONFIG_COMPAT */
450 
451 static const struct file_operations snapshot_fops = {
452 	.open = snapshot_open,
453 	.release = snapshot_release,
454 	.read = snapshot_read,
455 	.write = snapshot_write,
456 	.llseek = no_llseek,
457 	.unlocked_ioctl = snapshot_ioctl,
458 #ifdef CONFIG_COMPAT
459 	.compat_ioctl = snapshot_compat_ioctl,
460 #endif
461 };
462 
463 static struct miscdevice snapshot_device = {
464 	.minor = SNAPSHOT_MINOR,
465 	.name = "snapshot",
466 	.fops = &snapshot_fops,
467 };
468 
469 static int __init snapshot_device_init(void)
470 {
471 	return misc_register(&snapshot_device);
472 };
473 
474 device_initcall(snapshot_device_init);
475