diff options
Diffstat (limited to 'kernel/power')
-rw-r--r-- | kernel/power/Makefile | 2 | ||||
-rw-r--r-- | kernel/power/power.h | 14 | ||||
-rw-r--r-- | kernel/power/snapshot.c | 9 | ||||
-rw-r--r-- | kernel/power/user.c | 301 |
4 files changed, 321 insertions, 5 deletions
diff --git a/kernel/power/Makefile b/kernel/power/Makefile index bb91a0615303..8d0af3d37a4b 100644 --- a/kernel/power/Makefile +++ b/kernel/power/Makefile | |||
@@ -5,7 +5,7 @@ endif | |||
5 | 5 | ||
6 | obj-y := main.o process.o console.o | 6 | obj-y := main.o process.o console.o |
7 | obj-$(CONFIG_PM_LEGACY) += pm.o | 7 | obj-$(CONFIG_PM_LEGACY) += pm.o |
8 | obj-$(CONFIG_SOFTWARE_SUSPEND) += swsusp.o disk.o snapshot.o swap.o | 8 | obj-$(CONFIG_SOFTWARE_SUSPEND) += swsusp.o disk.o snapshot.o swap.o user.o |
9 | 9 | ||
10 | obj-$(CONFIG_SUSPEND_SMP) += smp.o | 10 | obj-$(CONFIG_SUSPEND_SMP) += smp.o |
11 | 11 | ||
diff --git a/kernel/power/power.h b/kernel/power/power.h index 5d1abffbb9ce..42c431c8bdde 100644 --- a/kernel/power/power.h +++ b/kernel/power/power.h | |||
@@ -8,6 +8,7 @@ struct swsusp_info { | |||
8 | int cpus; | 8 | int cpus; |
9 | unsigned long image_pages; | 9 | unsigned long image_pages; |
10 | unsigned long pages; | 10 | unsigned long pages; |
11 | unsigned long size; | ||
11 | } __attribute__((aligned(PAGE_SIZE))); | 12 | } __attribute__((aligned(PAGE_SIZE))); |
12 | 13 | ||
13 | 14 | ||
@@ -65,6 +66,19 @@ extern int snapshot_read_next(struct snapshot_handle *handle, size_t count); | |||
65 | extern int snapshot_write_next(struct snapshot_handle *handle, size_t count); | 66 | extern int snapshot_write_next(struct snapshot_handle *handle, size_t count); |
66 | int snapshot_image_loaded(struct snapshot_handle *handle); | 67 | int snapshot_image_loaded(struct snapshot_handle *handle); |
67 | 68 | ||
69 | #define SNAPSHOT_IOC_MAGIC '3' | ||
70 | #define SNAPSHOT_FREEZE _IO(SNAPSHOT_IOC_MAGIC, 1) | ||
71 | #define SNAPSHOT_UNFREEZE _IO(SNAPSHOT_IOC_MAGIC, 2) | ||
72 | #define SNAPSHOT_ATOMIC_SNAPSHOT _IOW(SNAPSHOT_IOC_MAGIC, 3, void *) | ||
73 | #define SNAPSHOT_ATOMIC_RESTORE _IO(SNAPSHOT_IOC_MAGIC, 4) | ||
74 | #define SNAPSHOT_FREE _IO(SNAPSHOT_IOC_MAGIC, 5) | ||
75 | #define SNAPSHOT_SET_IMAGE_SIZE _IOW(SNAPSHOT_IOC_MAGIC, 6, unsigned long) | ||
76 | #define SNAPSHOT_AVAIL_SWAP _IOR(SNAPSHOT_IOC_MAGIC, 7, void *) | ||
77 | #define SNAPSHOT_GET_SWAP_PAGE _IOR(SNAPSHOT_IOC_MAGIC, 8, void *) | ||
78 | #define SNAPSHOT_FREE_SWAP_PAGES _IO(SNAPSHOT_IOC_MAGIC, 9) | ||
79 | #define SNAPSHOT_SET_SWAP_FILE _IOW(SNAPSHOT_IOC_MAGIC, 10, unsigned int) | ||
80 | #define SNAPSHOT_IOC_MAXNR 10 | ||
81 | |||
68 | /** | 82 | /** |
69 | * The bitmap is used for tracing allocated swap pages | 83 | * The bitmap is used for tracing allocated swap pages |
70 | * | 84 | * |
diff --git a/kernel/power/snapshot.c b/kernel/power/snapshot.c index cc349437fb72..0036955357e0 100644 --- a/kernel/power/snapshot.c +++ b/kernel/power/snapshot.c | |||
@@ -37,6 +37,7 @@ | |||
37 | struct pbe *pagedir_nosave; | 37 | struct pbe *pagedir_nosave; |
38 | static unsigned int nr_copy_pages; | 38 | static unsigned int nr_copy_pages; |
39 | static unsigned int nr_meta_pages; | 39 | static unsigned int nr_meta_pages; |
40 | static unsigned long *buffer; | ||
40 | 41 | ||
41 | #ifdef CONFIG_HIGHMEM | 42 | #ifdef CONFIG_HIGHMEM |
42 | unsigned int count_highmem_pages(void) | 43 | unsigned int count_highmem_pages(void) |
@@ -389,7 +390,7 @@ struct pbe *alloc_pagedir(unsigned int nr_pages, gfp_t gfp_mask, int safe_needed | |||
389 | free_pagedir(pblist); | 390 | free_pagedir(pblist); |
390 | pblist = NULL; | 391 | pblist = NULL; |
391 | } else | 392 | } else |
392 | create_pbe_list(pblist, nr_pages); | 393 | create_pbe_list(pblist, nr_pages); |
393 | return pblist; | 394 | return pblist; |
394 | } | 395 | } |
395 | 396 | ||
@@ -418,6 +419,7 @@ void swsusp_free(void) | |||
418 | nr_copy_pages = 0; | 419 | nr_copy_pages = 0; |
419 | nr_meta_pages = 0; | 420 | nr_meta_pages = 0; |
420 | pagedir_nosave = NULL; | 421 | pagedir_nosave = NULL; |
422 | buffer = NULL; | ||
421 | } | 423 | } |
422 | 424 | ||
423 | 425 | ||
@@ -523,6 +525,8 @@ static void init_header(struct swsusp_info *info) | |||
523 | info->cpus = num_online_cpus(); | 525 | info->cpus = num_online_cpus(); |
524 | info->image_pages = nr_copy_pages; | 526 | info->image_pages = nr_copy_pages; |
525 | info->pages = nr_copy_pages + nr_meta_pages + 1; | 527 | info->pages = nr_copy_pages + nr_meta_pages + 1; |
528 | info->size = info->pages; | ||
529 | info->size <<= PAGE_SHIFT; | ||
526 | } | 530 | } |
527 | 531 | ||
528 | /** | 532 | /** |
@@ -568,8 +572,6 @@ static inline struct pbe *pack_orig_addresses(unsigned long *buf, struct pbe *pb | |||
568 | 572 | ||
569 | int snapshot_read_next(struct snapshot_handle *handle, size_t count) | 573 | int snapshot_read_next(struct snapshot_handle *handle, size_t count) |
570 | { | 574 | { |
571 | static unsigned long *buffer; | ||
572 | |||
573 | if (handle->page > nr_meta_pages + nr_copy_pages) | 575 | if (handle->page > nr_meta_pages + nr_copy_pages) |
574 | return 0; | 576 | return 0; |
575 | if (!buffer) { | 577 | if (!buffer) { |
@@ -774,7 +776,6 @@ static int create_image(struct snapshot_handle *handle) | |||
774 | 776 | ||
775 | int snapshot_write_next(struct snapshot_handle *handle, size_t count) | 777 | int snapshot_write_next(struct snapshot_handle *handle, size_t count) |
776 | { | 778 | { |
777 | static unsigned long *buffer; | ||
778 | int error = 0; | 779 | int error = 0; |
779 | 780 | ||
780 | if (handle->prev && handle->page > nr_meta_pages + nr_copy_pages) | 781 | if (handle->prev && handle->page > nr_meta_pages + nr_copy_pages) |
diff --git a/kernel/power/user.c b/kernel/power/user.c new file mode 100644 index 000000000000..8cabc405ca10 --- /dev/null +++ b/kernel/power/user.c | |||
@@ -0,0 +1,301 @@ | |||
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/string.h> | ||
15 | #include <linux/device.h> | ||
16 | #include <linux/miscdevice.h> | ||
17 | #include <linux/mm.h> | ||
18 | #include <linux/swap.h> | ||
19 | #include <linux/swapops.h> | ||
20 | #include <linux/pm.h> | ||
21 | #include <linux/fs.h> | ||
22 | |||
23 | #include <asm/uaccess.h> | ||
24 | |||
25 | #include "power.h" | ||
26 | |||
27 | #define SNAPSHOT_MINOR 231 | ||
28 | |||
29 | static struct snapshot_data { | ||
30 | struct snapshot_handle handle; | ||
31 | int swap; | ||
32 | struct bitmap_page *bitmap; | ||
33 | int mode; | ||
34 | char frozen; | ||
35 | char ready; | ||
36 | } snapshot_state; | ||
37 | |||
38 | static atomic_t device_available = ATOMIC_INIT(1); | ||
39 | |||
40 | static int snapshot_open(struct inode *inode, struct file *filp) | ||
41 | { | ||
42 | struct snapshot_data *data; | ||
43 | |||
44 | if (!atomic_add_unless(&device_available, -1, 0)) | ||
45 | return -EBUSY; | ||
46 | |||
47 | if ((filp->f_flags & O_ACCMODE) == O_RDWR) | ||
48 | return -ENOSYS; | ||
49 | |||
50 | nonseekable_open(inode, filp); | ||
51 | data = &snapshot_state; | ||
52 | filp->private_data = data; | ||
53 | memset(&data->handle, 0, sizeof(struct snapshot_handle)); | ||
54 | if ((filp->f_flags & O_ACCMODE) == O_RDONLY) { | ||
55 | data->swap = swsusp_resume_device ? swap_type_of(swsusp_resume_device) : -1; | ||
56 | data->mode = O_RDONLY; | ||
57 | } else { | ||
58 | data->swap = -1; | ||
59 | data->mode = O_WRONLY; | ||
60 | } | ||
61 | data->bitmap = NULL; | ||
62 | data->frozen = 0; | ||
63 | data->ready = 0; | ||
64 | |||
65 | return 0; | ||
66 | } | ||
67 | |||
68 | static int snapshot_release(struct inode *inode, struct file *filp) | ||
69 | { | ||
70 | struct snapshot_data *data; | ||
71 | |||
72 | swsusp_free(); | ||
73 | data = filp->private_data; | ||
74 | free_all_swap_pages(data->swap, data->bitmap); | ||
75 | free_bitmap(data->bitmap); | ||
76 | if (data->frozen) { | ||
77 | down(&pm_sem); | ||
78 | thaw_processes(); | ||
79 | enable_nonboot_cpus(); | ||
80 | up(&pm_sem); | ||
81 | } | ||
82 | atomic_inc(&device_available); | ||
83 | return 0; | ||
84 | } | ||
85 | |||
86 | static ssize_t snapshot_read(struct file *filp, char __user *buf, | ||
87 | size_t count, loff_t *offp) | ||
88 | { | ||
89 | struct snapshot_data *data; | ||
90 | ssize_t res; | ||
91 | |||
92 | data = filp->private_data; | ||
93 | res = snapshot_read_next(&data->handle, count); | ||
94 | if (res > 0) { | ||
95 | if (copy_to_user(buf, data_of(data->handle), res)) | ||
96 | res = -EFAULT; | ||
97 | else | ||
98 | *offp = data->handle.offset; | ||
99 | } | ||
100 | return res; | ||
101 | } | ||
102 | |||
103 | static ssize_t snapshot_write(struct file *filp, const char __user *buf, | ||
104 | size_t count, loff_t *offp) | ||
105 | { | ||
106 | struct snapshot_data *data; | ||
107 | ssize_t res; | ||
108 | |||
109 | data = filp->private_data; | ||
110 | res = snapshot_write_next(&data->handle, count); | ||
111 | if (res > 0) { | ||
112 | if (copy_from_user(data_of(data->handle), buf, res)) | ||
113 | res = -EFAULT; | ||
114 | else | ||
115 | *offp = data->handle.offset; | ||
116 | } | ||
117 | return res; | ||
118 | } | ||
119 | |||
120 | static int snapshot_ioctl(struct inode *inode, struct file *filp, | ||
121 | unsigned int cmd, unsigned long arg) | ||
122 | { | ||
123 | int error = 0; | ||
124 | struct snapshot_data *data; | ||
125 | loff_t offset, avail; | ||
126 | |||
127 | if (_IOC_TYPE(cmd) != SNAPSHOT_IOC_MAGIC) | ||
128 | return -ENOTTY; | ||
129 | if (_IOC_NR(cmd) > SNAPSHOT_IOC_MAXNR) | ||
130 | return -ENOTTY; | ||
131 | if (!capable(CAP_SYS_ADMIN)) | ||
132 | return -EPERM; | ||
133 | |||
134 | data = filp->private_data; | ||
135 | |||
136 | switch (cmd) { | ||
137 | |||
138 | case SNAPSHOT_FREEZE: | ||
139 | if (data->frozen) | ||
140 | break; | ||
141 | sys_sync(); | ||
142 | down(&pm_sem); | ||
143 | pm_prepare_console(); | ||
144 | disable_nonboot_cpus(); | ||
145 | if (freeze_processes()) { | ||
146 | thaw_processes(); | ||
147 | enable_nonboot_cpus(); | ||
148 | pm_restore_console(); | ||
149 | error = -EBUSY; | ||
150 | } | ||
151 | up(&pm_sem); | ||
152 | if (!error) | ||
153 | data->frozen = 1; | ||
154 | break; | ||
155 | |||
156 | case SNAPSHOT_UNFREEZE: | ||
157 | if (!data->frozen) | ||
158 | break; | ||
159 | down(&pm_sem); | ||
160 | thaw_processes(); | ||
161 | enable_nonboot_cpus(); | ||
162 | pm_restore_console(); | ||
163 | up(&pm_sem); | ||
164 | data->frozen = 0; | ||
165 | break; | ||
166 | |||
167 | case SNAPSHOT_ATOMIC_SNAPSHOT: | ||
168 | if (data->mode != O_RDONLY || !data->frozen || data->ready) { | ||
169 | error = -EPERM; | ||
170 | break; | ||
171 | } | ||
172 | down(&pm_sem); | ||
173 | /* Free memory before shutting down devices. */ | ||
174 | error = swsusp_shrink_memory(); | ||
175 | if (!error) { | ||
176 | error = device_suspend(PMSG_FREEZE); | ||
177 | if (!error) { | ||
178 | in_suspend = 1; | ||
179 | error = swsusp_suspend(); | ||
180 | device_resume(); | ||
181 | } | ||
182 | } | ||
183 | up(&pm_sem); | ||
184 | if (!error) | ||
185 | error = put_user(in_suspend, (unsigned int __user *)arg); | ||
186 | if (!error) | ||
187 | data->ready = 1; | ||
188 | break; | ||
189 | |||
190 | case SNAPSHOT_ATOMIC_RESTORE: | ||
191 | if (data->mode != O_WRONLY || !data->frozen || | ||
192 | !snapshot_image_loaded(&data->handle)) { | ||
193 | error = -EPERM; | ||
194 | break; | ||
195 | } | ||
196 | down(&pm_sem); | ||
197 | pm_prepare_console(); | ||
198 | error = device_suspend(PMSG_FREEZE); | ||
199 | if (!error) { | ||
200 | error = swsusp_resume(); | ||
201 | device_resume(); | ||
202 | } | ||
203 | pm_restore_console(); | ||
204 | up(&pm_sem); | ||
205 | break; | ||
206 | |||
207 | case SNAPSHOT_FREE: | ||
208 | swsusp_free(); | ||
209 | memset(&data->handle, 0, sizeof(struct snapshot_handle)); | ||
210 | data->ready = 0; | ||
211 | break; | ||
212 | |||
213 | case SNAPSHOT_SET_IMAGE_SIZE: | ||
214 | image_size = arg; | ||
215 | break; | ||
216 | |||
217 | case SNAPSHOT_AVAIL_SWAP: | ||
218 | avail = count_swap_pages(data->swap, 1); | ||
219 | avail <<= PAGE_SHIFT; | ||
220 | error = put_user(avail, (loff_t __user *)arg); | ||
221 | break; | ||
222 | |||
223 | case SNAPSHOT_GET_SWAP_PAGE: | ||
224 | if (data->swap < 0 || data->swap >= MAX_SWAPFILES) { | ||
225 | error = -ENODEV; | ||
226 | break; | ||
227 | } | ||
228 | if (!data->bitmap) { | ||
229 | data->bitmap = alloc_bitmap(count_swap_pages(data->swap, 0)); | ||
230 | if (!data->bitmap) { | ||
231 | error = -ENOMEM; | ||
232 | break; | ||
233 | } | ||
234 | } | ||
235 | offset = alloc_swap_page(data->swap, data->bitmap); | ||
236 | if (offset) { | ||
237 | offset <<= PAGE_SHIFT; | ||
238 | error = put_user(offset, (loff_t __user *)arg); | ||
239 | } else { | ||
240 | error = -ENOSPC; | ||
241 | } | ||
242 | break; | ||
243 | |||
244 | case SNAPSHOT_FREE_SWAP_PAGES: | ||
245 | if (data->swap < 0 || data->swap >= MAX_SWAPFILES) { | ||
246 | error = -ENODEV; | ||
247 | break; | ||
248 | } | ||
249 | free_all_swap_pages(data->swap, data->bitmap); | ||
250 | free_bitmap(data->bitmap); | ||
251 | data->bitmap = NULL; | ||
252 | break; | ||
253 | |||
254 | case SNAPSHOT_SET_SWAP_FILE: | ||
255 | if (!data->bitmap) { | ||
256 | /* | ||
257 | * User space encodes device types as two-byte values, | ||
258 | * so we need to recode them | ||
259 | */ | ||
260 | if (old_decode_dev(arg)) { | ||
261 | data->swap = swap_type_of(old_decode_dev(arg)); | ||
262 | if (data->swap < 0) | ||
263 | error = -ENODEV; | ||
264 | } else { | ||
265 | data->swap = -1; | ||
266 | error = -EINVAL; | ||
267 | } | ||
268 | } else { | ||
269 | error = -EPERM; | ||
270 | } | ||
271 | break; | ||
272 | |||
273 | default: | ||
274 | error = -ENOTTY; | ||
275 | |||
276 | } | ||
277 | |||
278 | return error; | ||
279 | } | ||
280 | |||
281 | static struct file_operations snapshot_fops = { | ||
282 | .open = snapshot_open, | ||
283 | .release = snapshot_release, | ||
284 | .read = snapshot_read, | ||
285 | .write = snapshot_write, | ||
286 | .llseek = no_llseek, | ||
287 | .ioctl = snapshot_ioctl, | ||
288 | }; | ||
289 | |||
290 | static struct miscdevice snapshot_device = { | ||
291 | .minor = SNAPSHOT_MINOR, | ||
292 | .name = "snapshot", | ||
293 | .fops = &snapshot_fops, | ||
294 | }; | ||
295 | |||
296 | static int __init snapshot_device_init(void) | ||
297 | { | ||
298 | return misc_register(&snapshot_device); | ||
299 | }; | ||
300 | |||
301 | device_initcall(snapshot_device_init); | ||