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;
38 struct inode *bd_inode;
41 int is_hibernate_resume_dev(const struct inode *bd_inode)
43 return hibernation_available() && snapshot_state.bd_inode == bd_inode;
46 static int snapshot_open(struct inode *inode, struct file *filp)
48 struct snapshot_data *data;
49 int error, nr_calls = 0;
51 if (!hibernation_available())
56 if (!hibernate_acquire()) {
61 if ((filp->f_flags & O_ACCMODE) == O_RDWR) {
66 nonseekable_open(inode, filp);
67 data = &snapshot_state;
68 filp->private_data = data;
69 memset(&data->handle, 0, sizeof(struct snapshot_handle));
70 if ((filp->f_flags & O_ACCMODE) == O_RDONLY) {
71 /* Hibernating. The image device should be accessible. */
72 data->swap = swsusp_resume_device ?
73 swap_type_of(swsusp_resume_device, 0, NULL) : -1;
74 data->mode = O_RDONLY;
75 data->free_bitmaps = false;
76 error = __pm_notifier_call_chain(PM_HIBERNATION_PREPARE, -1, &nr_calls);
78 __pm_notifier_call_chain(PM_POST_HIBERNATION, --nr_calls, NULL);
81 * Resuming. We may need to wait for the image device to
84 wait_for_device_probe();
87 data->mode = O_WRONLY;
88 error = __pm_notifier_call_chain(PM_RESTORE_PREPARE, -1, &nr_calls);
90 error = create_basic_memory_bitmaps();
91 data->free_bitmaps = !error;
96 __pm_notifier_call_chain(PM_POST_RESTORE, nr_calls, NULL);
101 data->frozen = false;
103 data->platform_support = false;
104 data->bd_inode = NULL;
107 unlock_system_sleep();
112 static int snapshot_release(struct inode *inode, struct file *filp)
114 struct snapshot_data *data;
119 data = filp->private_data;
120 data->bd_inode = NULL;
121 free_all_swap_pages(data->swap);
123 pm_restore_gfp_mask();
124 free_basic_memory_bitmaps();
126 } else if (data->free_bitmaps) {
127 free_basic_memory_bitmaps();
129 pm_notifier_call_chain(data->mode == O_RDONLY ?
130 PM_POST_HIBERNATION : PM_POST_RESTORE);
133 unlock_system_sleep();
138 static ssize_t snapshot_read(struct file *filp, char __user *buf,
139 size_t count, loff_t *offp)
141 struct snapshot_data *data;
143 loff_t pg_offp = *offp & ~PAGE_MASK;
147 data = filp->private_data;
152 if (!pg_offp) { /* on page boundary? */
153 res = snapshot_read_next(&data->handle);
157 res = PAGE_SIZE - pg_offp;
160 res = simple_read_from_buffer(buf, count, &pg_offp,
161 data_of(data->handle), res);
166 unlock_system_sleep();
171 static ssize_t snapshot_write(struct file *filp, const char __user *buf,
172 size_t count, loff_t *offp)
174 struct snapshot_data *data;
176 loff_t pg_offp = *offp & ~PAGE_MASK;
180 data = filp->private_data;
183 res = snapshot_write_next(&data->handle);
187 res = PAGE_SIZE - pg_offp;
190 if (!data_of(data->handle)) {
195 res = simple_write_to_buffer(data_of(data->handle), res, &pg_offp,
200 unlock_system_sleep();
205 struct compat_resume_swap_area {
206 compat_loff_t offset;
210 static int snapshot_set_swap_area(struct snapshot_data *data,
213 struct block_device *bdev;
217 if (swsusp_swap_in_use())
220 if (in_compat_syscall()) {
221 struct compat_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;
228 struct resume_swap_area swap_area;
230 if (copy_from_user(&swap_area, argp, sizeof(swap_area)))
232 swdev = new_decode_dev(swap_area.dev);
233 offset = swap_area.offset;
237 * User space encodes device types as two-byte values,
238 * so we need to recode them
244 data->swap = swap_type_of(swdev, offset, &bdev);
248 data->bd_inode = bdev->bd_inode;
253 static long snapshot_ioctl(struct file *filp, unsigned int cmd,
257 struct snapshot_data *data;
261 if (_IOC_TYPE(cmd) != SNAPSHOT_IOC_MAGIC)
263 if (_IOC_NR(cmd) > SNAPSHOT_IOC_MAXNR)
265 if (!capable(CAP_SYS_ADMIN))
268 if (!mutex_trylock(&system_transition_mutex))
271 lock_device_hotplug();
272 data = filp->private_data;
276 case SNAPSHOT_FREEZE:
282 error = freeze_processes();
286 error = create_basic_memory_bitmaps();
294 case SNAPSHOT_UNFREEZE:
295 if (!data->frozen || data->ready)
297 pm_restore_gfp_mask();
298 free_basic_memory_bitmaps();
299 data->free_bitmaps = false;
301 data->frozen = false;
304 case SNAPSHOT_CREATE_IMAGE:
305 if (data->mode != O_RDONLY || !data->frozen || data->ready) {
309 pm_restore_gfp_mask();
310 error = hibernation_snapshot(data->platform_support);
312 error = put_user(in_suspend, (int __user *)arg);
313 data->ready = !freezer_test_done && !error;
314 freezer_test_done = false;
318 case SNAPSHOT_ATOMIC_RESTORE:
319 snapshot_write_finalize(&data->handle);
320 if (data->mode != O_WRONLY || !data->frozen ||
321 !snapshot_image_loaded(&data->handle)) {
325 error = hibernation_restore(data->platform_support);
330 memset(&data->handle, 0, sizeof(struct snapshot_handle));
333 * It is necessary to thaw kernel threads here, because
334 * SNAPSHOT_CREATE_IMAGE may be invoked directly after
335 * SNAPSHOT_FREE. In that case, if kernel threads were not
336 * thawed, the preallocation of memory carried out by
337 * hibernation_snapshot() might run into problems (i.e. it
338 * might fail or even deadlock).
340 thaw_kernel_threads();
343 case SNAPSHOT_PREF_IMAGE_SIZE:
347 case SNAPSHOT_GET_IMAGE_SIZE:
352 size = snapshot_get_image_size();
354 error = put_user(size, (loff_t __user *)arg);
357 case SNAPSHOT_AVAIL_SWAP_SIZE:
358 size = count_swap_pages(data->swap, 1);
360 error = put_user(size, (loff_t __user *)arg);
363 case SNAPSHOT_ALLOC_SWAP_PAGE:
364 if (data->swap < 0 || data->swap >= MAX_SWAPFILES) {
368 offset = alloc_swapdev_block(data->swap);
370 offset <<= PAGE_SHIFT;
371 error = put_user(offset, (loff_t __user *)arg);
377 case SNAPSHOT_FREE_SWAP_PAGES:
378 if (data->swap < 0 || data->swap >= MAX_SWAPFILES) {
382 free_all_swap_pages(data->swap);
391 * Tasks are frozen and the notifiers have been called with
392 * PM_HIBERNATION_PREPARE
394 error = suspend_devices_and_enter(PM_SUSPEND_MEM);
398 case SNAPSHOT_PLATFORM_SUPPORT:
399 data->platform_support = !!arg;
402 case SNAPSHOT_POWER_OFF:
403 if (data->platform_support)
404 error = hibernation_platform_enter();
407 case SNAPSHOT_SET_SWAP_AREA:
408 error = snapshot_set_swap_area(data, (void __user *)arg);
416 unlock_device_hotplug();
417 mutex_unlock(&system_transition_mutex);
424 snapshot_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
426 BUILD_BUG_ON(sizeof(loff_t) != sizeof(compat_loff_t));
429 case SNAPSHOT_GET_IMAGE_SIZE:
430 case SNAPSHOT_AVAIL_SWAP_SIZE:
431 case SNAPSHOT_ALLOC_SWAP_PAGE:
432 case SNAPSHOT_CREATE_IMAGE:
433 case SNAPSHOT_SET_SWAP_AREA:
434 return snapshot_ioctl(file, cmd,
435 (unsigned long) compat_ptr(arg));
437 return snapshot_ioctl(file, cmd, arg);
440 #endif /* CONFIG_COMPAT */
442 static const struct file_operations snapshot_fops = {
443 .open = snapshot_open,
444 .release = snapshot_release,
445 .read = snapshot_read,
446 .write = snapshot_write,
448 .unlocked_ioctl = snapshot_ioctl,
450 .compat_ioctl = snapshot_compat_ioctl,
454 static struct miscdevice snapshot_device = {
455 .minor = SNAPSHOT_MINOR,
457 .fops = &snapshot_fops,
460 static int __init snapshot_device_init(void)
462 return misc_register(&snapshot_device);
465 device_initcall(snapshot_device_init);