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 <[email protected]> 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 30 static struct snapshot_data { 31 struct snapshot_handle handle; 32 int swap; 33 int mode; 34 bool frozen; 35 bool ready; 36 bool platform_support; 37 bool free_bitmaps; 38 struct inode *bd_inode; 39 } snapshot_state; 40 41 int is_hibernate_resume_dev(const struct inode *bd_inode) 42 { 43 return hibernation_available() && snapshot_state.bd_inode == bd_inode; 44 } 45 46 static int snapshot_open(struct inode *inode, struct file *filp) 47 { 48 struct snapshot_data *data; 49 int error; 50 51 if (!hibernation_available()) 52 return -EPERM; 53 54 lock_system_sleep(); 55 56 if (!hibernate_acquire()) { 57 error = -EBUSY; 58 goto Unlock; 59 } 60 61 if ((filp->f_flags & O_ACCMODE) == O_RDWR) { 62 hibernate_release(); 63 error = -ENOSYS; 64 goto Unlock; 65 } 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_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 wait_for_device_probe(); 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->bd_inode = NULL; 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->bd_inode = NULL; 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 lock_system_sleep(); 173 174 data = filp->private_data; 175 176 if (!pg_offp) { 177 res = snapshot_write_next(&data->handle); 178 if (res <= 0) 179 goto unlock; 180 } else { 181 res = PAGE_SIZE - pg_offp; 182 } 183 184 if (!data_of(data->handle)) { 185 res = -EINVAL; 186 goto unlock; 187 } 188 189 res = simple_write_to_buffer(data_of(data->handle), res, &pg_offp, 190 buf, count); 191 if (res > 0) 192 *offp += res; 193 unlock: 194 unlock_system_sleep(); 195 196 return res; 197 } 198 199 struct compat_resume_swap_area { 200 compat_loff_t offset; 201 u32 dev; 202 } __packed; 203 204 static int snapshot_set_swap_area(struct snapshot_data *data, 205 void __user *argp) 206 { 207 struct block_device *bdev; 208 sector_t offset; 209 dev_t swdev; 210 211 if (swsusp_swap_in_use()) 212 return -EPERM; 213 214 if (in_compat_syscall()) { 215 struct compat_resume_swap_area swap_area; 216 217 if (copy_from_user(&swap_area, argp, sizeof(swap_area))) 218 return -EFAULT; 219 swdev = new_decode_dev(swap_area.dev); 220 offset = swap_area.offset; 221 } else { 222 struct resume_swap_area swap_area; 223 224 if (copy_from_user(&swap_area, argp, sizeof(swap_area))) 225 return -EFAULT; 226 swdev = new_decode_dev(swap_area.dev); 227 offset = swap_area.offset; 228 } 229 230 /* 231 * User space encodes device types as two-byte values, 232 * so we need to recode them 233 */ 234 if (!swdev) { 235 data->swap = -1; 236 return -EINVAL; 237 } 238 data->swap = swap_type_of(swdev, offset, &bdev); 239 if (data->swap < 0) 240 return -ENODEV; 241 242 data->bd_inode = bdev->bd_inode; 243 bdput(bdev); 244 return 0; 245 } 246 247 static long snapshot_ioctl(struct file *filp, unsigned int cmd, 248 unsigned long arg) 249 { 250 int error = 0; 251 struct snapshot_data *data; 252 loff_t size; 253 sector_t offset; 254 255 if (_IOC_TYPE(cmd) != SNAPSHOT_IOC_MAGIC) 256 return -ENOTTY; 257 if (_IOC_NR(cmd) > SNAPSHOT_IOC_MAXNR) 258 return -ENOTTY; 259 if (!capable(CAP_SYS_ADMIN)) 260 return -EPERM; 261 262 if (!mutex_trylock(&system_transition_mutex)) 263 return -EBUSY; 264 265 lock_device_hotplug(); 266 data = filp->private_data; 267 268 switch (cmd) { 269 270 case SNAPSHOT_FREEZE: 271 if (data->frozen) 272 break; 273 274 ksys_sync_helper(); 275 276 error = freeze_processes(); 277 if (error) 278 break; 279 280 error = create_basic_memory_bitmaps(); 281 if (error) 282 thaw_processes(); 283 else 284 data->frozen = true; 285 286 break; 287 288 case SNAPSHOT_UNFREEZE: 289 if (!data->frozen || data->ready) 290 break; 291 pm_restore_gfp_mask(); 292 free_basic_memory_bitmaps(); 293 data->free_bitmaps = false; 294 thaw_processes(); 295 data->frozen = false; 296 break; 297 298 case SNAPSHOT_CREATE_IMAGE: 299 if (data->mode != O_RDONLY || !data->frozen || data->ready) { 300 error = -EPERM; 301 break; 302 } 303 pm_restore_gfp_mask(); 304 error = hibernation_snapshot(data->platform_support); 305 if (!error) { 306 error = put_user(in_suspend, (int __user *)arg); 307 data->ready = !freezer_test_done && !error; 308 freezer_test_done = false; 309 } 310 break; 311 312 case SNAPSHOT_ATOMIC_RESTORE: 313 snapshot_write_finalize(&data->handle); 314 if (data->mode != O_WRONLY || !data->frozen || 315 !snapshot_image_loaded(&data->handle)) { 316 error = -EPERM; 317 break; 318 } 319 error = hibernation_restore(data->platform_support); 320 break; 321 322 case SNAPSHOT_FREE: 323 swsusp_free(); 324 memset(&data->handle, 0, sizeof(struct snapshot_handle)); 325 data->ready = false; 326 /* 327 * It is necessary to thaw kernel threads here, because 328 * SNAPSHOT_CREATE_IMAGE may be invoked directly after 329 * SNAPSHOT_FREE. In that case, if kernel threads were not 330 * thawed, the preallocation of memory carried out by 331 * hibernation_snapshot() might run into problems (i.e. it 332 * might fail or even deadlock). 333 */ 334 thaw_kernel_threads(); 335 break; 336 337 case SNAPSHOT_PREF_IMAGE_SIZE: 338 image_size = arg; 339 break; 340 341 case SNAPSHOT_GET_IMAGE_SIZE: 342 if (!data->ready) { 343 error = -ENODATA; 344 break; 345 } 346 size = snapshot_get_image_size(); 347 size <<= PAGE_SHIFT; 348 error = put_user(size, (loff_t __user *)arg); 349 break; 350 351 case SNAPSHOT_AVAIL_SWAP_SIZE: 352 size = count_swap_pages(data->swap, 1); 353 size <<= PAGE_SHIFT; 354 error = put_user(size, (loff_t __user *)arg); 355 break; 356 357 case SNAPSHOT_ALLOC_SWAP_PAGE: 358 if (data->swap < 0 || data->swap >= MAX_SWAPFILES) { 359 error = -ENODEV; 360 break; 361 } 362 offset = alloc_swapdev_block(data->swap); 363 if (offset) { 364 offset <<= PAGE_SHIFT; 365 error = put_user(offset, (loff_t __user *)arg); 366 } else { 367 error = -ENOSPC; 368 } 369 break; 370 371 case SNAPSHOT_FREE_SWAP_PAGES: 372 if (data->swap < 0 || data->swap >= MAX_SWAPFILES) { 373 error = -ENODEV; 374 break; 375 } 376 free_all_swap_pages(data->swap); 377 break; 378 379 case SNAPSHOT_S2RAM: 380 if (!data->frozen) { 381 error = -EPERM; 382 break; 383 } 384 /* 385 * Tasks are frozen and the notifiers have been called with 386 * PM_HIBERNATION_PREPARE 387 */ 388 error = suspend_devices_and_enter(PM_SUSPEND_MEM); 389 data->ready = false; 390 break; 391 392 case SNAPSHOT_PLATFORM_SUPPORT: 393 data->platform_support = !!arg; 394 break; 395 396 case SNAPSHOT_POWER_OFF: 397 if (data->platform_support) 398 error = hibernation_platform_enter(); 399 break; 400 401 case SNAPSHOT_SET_SWAP_AREA: 402 error = snapshot_set_swap_area(data, (void __user *)arg); 403 break; 404 405 default: 406 error = -ENOTTY; 407 408 } 409 410 unlock_device_hotplug(); 411 mutex_unlock(&system_transition_mutex); 412 413 return error; 414 } 415 416 #ifdef CONFIG_COMPAT 417 static long 418 snapshot_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg) 419 { 420 BUILD_BUG_ON(sizeof(loff_t) != sizeof(compat_loff_t)); 421 422 switch (cmd) { 423 case SNAPSHOT_GET_IMAGE_SIZE: 424 case SNAPSHOT_AVAIL_SWAP_SIZE: 425 case SNAPSHOT_ALLOC_SWAP_PAGE: 426 case SNAPSHOT_CREATE_IMAGE: 427 case SNAPSHOT_SET_SWAP_AREA: 428 return snapshot_ioctl(file, cmd, 429 (unsigned long) compat_ptr(arg)); 430 default: 431 return snapshot_ioctl(file, cmd, arg); 432 } 433 } 434 #endif /* CONFIG_COMPAT */ 435 436 static const struct file_operations snapshot_fops = { 437 .open = snapshot_open, 438 .release = snapshot_release, 439 .read = snapshot_read, 440 .write = snapshot_write, 441 .llseek = no_llseek, 442 .unlocked_ioctl = snapshot_ioctl, 443 #ifdef CONFIG_COMPAT 444 .compat_ioctl = snapshot_compat_ioctl, 445 #endif 446 }; 447 448 static struct miscdevice snapshot_device = { 449 .minor = SNAPSHOT_MINOR, 450 .name = "snapshot", 451 .fops = &snapshot_fops, 452 }; 453 454 static int __init snapshot_device_init(void) 455 { 456 return misc_register(&snapshot_device); 457 }; 458 459 device_initcall(snapshot_device_init); 460