1 // SPDX-License-Identifier: GPL-2.0-only
3 * linux/kernel/power/user.c
5 * This file provides the user space interface for software suspend/resume.
7 * Copyright (C) 2006 Rafael J. Wysocki <rjw@sisk.pl>
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>
16 #include <linux/swap.h>
17 #include <linux/swapops.h>
20 #include <linux/compat.h>
21 #include <linux/console.h>
22 #include <linux/cpu.h>
23 #include <linux/freezer.h>
25 #include <linux/uaccess.h>
30 static struct snapshot_data {
31 struct snapshot_handle handle;
36 bool platform_support;
40 atomic_t snapshot_device_available = ATOMIC_INIT(1);
42 static int snapshot_open(struct inode *inode, struct file *filp)
44 struct snapshot_data *data;
45 int error, nr_calls = 0;
47 if (!hibernation_available())
52 if (!atomic_add_unless(&snapshot_device_available, -1, 0)) {
57 if ((filp->f_flags & O_ACCMODE) == O_RDWR) {
58 atomic_inc(&snapshot_device_available);
62 nonseekable_open(inode, filp);
63 data = &snapshot_state;
64 filp->private_data = data;
65 memset(&data->handle, 0, sizeof(struct snapshot_handle));
66 if ((filp->f_flags & O_ACCMODE) == O_RDONLY) {
67 /* Hibernating. The image device should be accessible. */
68 data->swap = swsusp_resume_device ?
69 swap_type_of(swsusp_resume_device, 0, NULL) : -1;
70 data->mode = O_RDONLY;
71 data->free_bitmaps = false;
72 error = __pm_notifier_call_chain(PM_HIBERNATION_PREPARE, -1, &nr_calls);
74 __pm_notifier_call_chain(PM_POST_HIBERNATION, --nr_calls, NULL);
77 * Resuming. We may need to wait for the image device to
80 wait_for_device_probe();
83 data->mode = O_WRONLY;
84 error = __pm_notifier_call_chain(PM_RESTORE_PREPARE, -1, &nr_calls);
86 error = create_basic_memory_bitmaps();
87 data->free_bitmaps = !error;
92 __pm_notifier_call_chain(PM_POST_RESTORE, nr_calls, NULL);
95 atomic_inc(&snapshot_device_available);
99 data->platform_support = false;
102 unlock_system_sleep();
107 static int snapshot_release(struct inode *inode, struct file *filp)
109 struct snapshot_data *data;
114 data = filp->private_data;
115 free_all_swap_pages(data->swap);
117 pm_restore_gfp_mask();
118 free_basic_memory_bitmaps();
120 } else if (data->free_bitmaps) {
121 free_basic_memory_bitmaps();
123 pm_notifier_call_chain(data->mode == O_RDONLY ?
124 PM_POST_HIBERNATION : PM_POST_RESTORE);
125 atomic_inc(&snapshot_device_available);
127 unlock_system_sleep();
132 static ssize_t snapshot_read(struct file *filp, char __user *buf,
133 size_t count, loff_t *offp)
135 struct snapshot_data *data;
137 loff_t pg_offp = *offp & ~PAGE_MASK;
141 data = filp->private_data;
146 if (!pg_offp) { /* on page boundary? */
147 res = snapshot_read_next(&data->handle);
151 res = PAGE_SIZE - pg_offp;
154 res = simple_read_from_buffer(buf, count, &pg_offp,
155 data_of(data->handle), res);
160 unlock_system_sleep();
165 static ssize_t snapshot_write(struct file *filp, const char __user *buf,
166 size_t count, loff_t *offp)
168 struct snapshot_data *data;
170 loff_t pg_offp = *offp & ~PAGE_MASK;
174 data = filp->private_data;
177 res = snapshot_write_next(&data->handle);
181 res = PAGE_SIZE - pg_offp;
184 if (!data_of(data->handle)) {
189 res = simple_write_to_buffer(data_of(data->handle), res, &pg_offp,
194 unlock_system_sleep();
199 struct compat_resume_swap_area {
200 compat_loff_t offset;
204 static int snapshot_set_swap_area(struct snapshot_data *data,
210 if (swsusp_swap_in_use())
213 if (in_compat_syscall()) {
214 struct compat_resume_swap_area swap_area;
216 if (copy_from_user(&swap_area, argp, sizeof(swap_area)))
218 swdev = new_decode_dev(swap_area.dev);
219 offset = swap_area.offset;
221 struct resume_swap_area swap_area;
223 if (copy_from_user(&swap_area, argp, sizeof(swap_area)))
225 swdev = new_decode_dev(swap_area.dev);
226 offset = swap_area.offset;
230 * User space encodes device types as two-byte values,
231 * so we need to recode them
237 data->swap = swap_type_of(swdev, offset, NULL);
243 static long snapshot_ioctl(struct file *filp, unsigned int cmd,
247 struct snapshot_data *data;
251 if (_IOC_TYPE(cmd) != SNAPSHOT_IOC_MAGIC)
253 if (_IOC_NR(cmd) > SNAPSHOT_IOC_MAXNR)
255 if (!capable(CAP_SYS_ADMIN))
258 if (!mutex_trylock(&system_transition_mutex))
261 lock_device_hotplug();
262 data = filp->private_data;
266 case SNAPSHOT_FREEZE:
272 error = freeze_processes();
276 error = create_basic_memory_bitmaps();
284 case SNAPSHOT_UNFREEZE:
285 if (!data->frozen || data->ready)
287 pm_restore_gfp_mask();
288 free_basic_memory_bitmaps();
289 data->free_bitmaps = false;
291 data->frozen = false;
294 case SNAPSHOT_CREATE_IMAGE:
295 if (data->mode != O_RDONLY || !data->frozen || data->ready) {
299 pm_restore_gfp_mask();
300 error = hibernation_snapshot(data->platform_support);
302 error = put_user(in_suspend, (int __user *)arg);
303 data->ready = !freezer_test_done && !error;
304 freezer_test_done = false;
308 case SNAPSHOT_ATOMIC_RESTORE:
309 snapshot_write_finalize(&data->handle);
310 if (data->mode != O_WRONLY || !data->frozen ||
311 !snapshot_image_loaded(&data->handle)) {
315 error = hibernation_restore(data->platform_support);
320 memset(&data->handle, 0, sizeof(struct snapshot_handle));
323 * It is necessary to thaw kernel threads here, because
324 * SNAPSHOT_CREATE_IMAGE may be invoked directly after
325 * SNAPSHOT_FREE. In that case, if kernel threads were not
326 * thawed, the preallocation of memory carried out by
327 * hibernation_snapshot() might run into problems (i.e. it
328 * might fail or even deadlock).
330 thaw_kernel_threads();
333 case SNAPSHOT_PREF_IMAGE_SIZE:
337 case SNAPSHOT_GET_IMAGE_SIZE:
342 size = snapshot_get_image_size();
344 error = put_user(size, (loff_t __user *)arg);
347 case SNAPSHOT_AVAIL_SWAP_SIZE:
348 size = count_swap_pages(data->swap, 1);
350 error = put_user(size, (loff_t __user *)arg);
353 case SNAPSHOT_ALLOC_SWAP_PAGE:
354 if (data->swap < 0 || data->swap >= MAX_SWAPFILES) {
358 offset = alloc_swapdev_block(data->swap);
360 offset <<= PAGE_SHIFT;
361 error = put_user(offset, (loff_t __user *)arg);
367 case SNAPSHOT_FREE_SWAP_PAGES:
368 if (data->swap < 0 || data->swap >= MAX_SWAPFILES) {
372 free_all_swap_pages(data->swap);
381 * Tasks are frozen and the notifiers have been called with
382 * PM_HIBERNATION_PREPARE
384 error = suspend_devices_and_enter(PM_SUSPEND_MEM);
388 case SNAPSHOT_PLATFORM_SUPPORT:
389 data->platform_support = !!arg;
392 case SNAPSHOT_POWER_OFF:
393 if (data->platform_support)
394 error = hibernation_platform_enter();
397 case SNAPSHOT_SET_SWAP_AREA:
398 error = snapshot_set_swap_area(data, (void __user *)arg);
406 unlock_device_hotplug();
407 mutex_unlock(&system_transition_mutex);
414 snapshot_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
416 BUILD_BUG_ON(sizeof(loff_t) != sizeof(compat_loff_t));
419 case SNAPSHOT_GET_IMAGE_SIZE:
420 case SNAPSHOT_AVAIL_SWAP_SIZE:
421 case SNAPSHOT_ALLOC_SWAP_PAGE:
422 case SNAPSHOT_CREATE_IMAGE:
423 case SNAPSHOT_SET_SWAP_AREA:
424 return snapshot_ioctl(file, cmd,
425 (unsigned long) compat_ptr(arg));
427 return snapshot_ioctl(file, cmd, arg);
430 #endif /* CONFIG_COMPAT */
432 static const struct file_operations snapshot_fops = {
433 .open = snapshot_open,
434 .release = snapshot_release,
435 .read = snapshot_read,
436 .write = snapshot_write,
438 .unlocked_ioctl = snapshot_ioctl,
440 .compat_ioctl = snapshot_compat_ioctl,
444 static struct miscdevice snapshot_device = {
445 .minor = SNAPSHOT_MINOR,
447 .fops = &snapshot_fops,
450 static int __init snapshot_device_init(void)
452 return misc_register(&snapshot_device);
455 device_initcall(snapshot_device_init);