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