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