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