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