user.c 9.7 KB

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