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