user.c 9.4 KB

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