Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * linux/kernel/power/swsusp.c | |
3 | * | |
4 | * This file is to realize architecture-independent | |
5 | * machine suspend feature using pretty near only high-level routines | |
6 | * | |
7 | * Copyright (C) 1998-2001 Gabor Kuti <seasons@fornax.hu> | |
25761b6e | 8 | * Copyright (C) 1998,2001-2005 Pavel Machek <pavel@suse.cz> |
1da177e4 LT |
9 | * |
10 | * This file is released under the GPLv2. | |
11 | * | |
12 | * I'd like to thank the following people for their work: | |
2e4d5822 | 13 | * |
1da177e4 LT |
14 | * Pavel Machek <pavel@ucw.cz>: |
15 | * Modifications, defectiveness pointing, being with me at the very beginning, | |
16 | * suspend to swap space, stop all tasks. Port to 2.4.18-ac and 2.5.17. | |
17 | * | |
2e4d5822 | 18 | * Steve Doddi <dirk@loth.demon.co.uk>: |
1da177e4 LT |
19 | * Support the possibility of hardware state restoring. |
20 | * | |
21 | * Raph <grey.havens@earthling.net>: | |
22 | * Support for preserving states of network devices and virtual console | |
23 | * (including X and svgatextmode) | |
24 | * | |
25 | * Kurt Garloff <garloff@suse.de>: | |
26 | * Straightened the critical function in order to prevent compilers from | |
27 | * playing tricks with local variables. | |
28 | * | |
29 | * Andreas Mohr <a.mohr@mailto.de> | |
30 | * | |
31 | * Alex Badea <vampire@go.ro>: | |
32 | * Fixed runaway init | |
33 | * | |
c2ff18f4 AS |
34 | * Andreas Steinmetz <ast@domdv.de>: |
35 | * Added encrypted suspend option | |
36 | * | |
1da177e4 LT |
37 | * More state savers are welcome. Especially for the scsi layer... |
38 | * | |
39 | * For TODOs,FIXMEs also look in Documentation/power/swsusp.txt | |
40 | */ | |
41 | ||
42 | #include <linux/module.h> | |
43 | #include <linux/mm.h> | |
44 | #include <linux/suspend.h> | |
45 | #include <linux/smp_lock.h> | |
46 | #include <linux/file.h> | |
47 | #include <linux/utsname.h> | |
48 | #include <linux/version.h> | |
49 | #include <linux/delay.h> | |
50 | #include <linux/reboot.h> | |
51 | #include <linux/bitops.h> | |
52 | #include <linux/vt_kern.h> | |
53 | #include <linux/kbd_kern.h> | |
54 | #include <linux/keyboard.h> | |
55 | #include <linux/spinlock.h> | |
56 | #include <linux/genhd.h> | |
57 | #include <linux/kernel.h> | |
58 | #include <linux/major.h> | |
59 | #include <linux/swap.h> | |
60 | #include <linux/pm.h> | |
61 | #include <linux/device.h> | |
62 | #include <linux/buffer_head.h> | |
63 | #include <linux/swapops.h> | |
64 | #include <linux/bootmem.h> | |
65 | #include <linux/syscalls.h> | |
66 | #include <linux/console.h> | |
67 | #include <linux/highmem.h> | |
68 | #include <linux/bio.h> | |
d53d9f16 | 69 | #include <linux/mount.h> |
1da177e4 LT |
70 | |
71 | #include <asm/uaccess.h> | |
72 | #include <asm/mmu_context.h> | |
73 | #include <asm/pgtable.h> | |
74 | #include <asm/tlbflush.h> | |
75 | #include <asm/io.h> | |
76 | ||
c2ff18f4 AS |
77 | #include <linux/random.h> |
78 | #include <linux/crypto.h> | |
79 | #include <asm/scatterlist.h> | |
80 | ||
1da177e4 LT |
81 | #include "power.h" |
82 | ||
c2ff18f4 AS |
83 | #define CIPHER "aes" |
84 | #define MAXKEY 32 | |
85 | #define MAXIV 32 | |
86 | ||
1da177e4 LT |
87 | extern char resume_file[]; |
88 | ||
89 | /* Local variables that should not be affected by save */ | |
25761b6e | 90 | unsigned int nr_copy_pages __nosavedata = 0; |
1da177e4 LT |
91 | |
92 | /* Suspend pagedir is allocated before final copy, therefore it | |
2e4d5822 | 93 | must be freed after resume |
1da177e4 LT |
94 | |
95 | Warning: this is evil. There are actually two pagedirs at time of | |
96 | resume. One is "pagedir_save", which is empty frame allocated at | |
2e4d5822 | 97 | time of suspend, that must be freed. Second is "pagedir_nosave", |
1da177e4 LT |
98 | allocated at time of resume, that travels through memory not to |
99 | collide with anything. | |
100 | ||
101 | Warning: this is even more evil than it seems. Pagedirs this file | |
102 | talks about are completely different from page directories used by | |
103 | MMU hardware. | |
104 | */ | |
105 | suspend_pagedir_t *pagedir_nosave __nosavedata = NULL; | |
25761b6e | 106 | suspend_pagedir_t *pagedir_save; |
1da177e4 LT |
107 | |
108 | #define SWSUSP_SIG "S1SUSPEND" | |
109 | ||
110 | static struct swsusp_header { | |
c2ff18f4 AS |
111 | char reserved[PAGE_SIZE - 20 - MAXKEY - MAXIV - sizeof(swp_entry_t)]; |
112 | u8 key_iv[MAXKEY+MAXIV]; | |
1da177e4 LT |
113 | swp_entry_t swsusp_info; |
114 | char orig_sig[10]; | |
115 | char sig[10]; | |
116 | } __attribute__((packed, aligned(PAGE_SIZE))) swsusp_header; | |
117 | ||
118 | static struct swsusp_info swsusp_info; | |
119 | ||
1da177e4 LT |
120 | /* |
121 | * Saving part... | |
122 | */ | |
123 | ||
124 | /* We memorize in swapfile_used what swap devices are used for suspension */ | |
125 | #define SWAPFILE_UNUSED 0 | |
126 | #define SWAPFILE_SUSPEND 1 /* This is the suspending device */ | |
127 | #define SWAPFILE_IGNORED 2 /* Those are other swap devices ignored for suspension */ | |
128 | ||
129 | static unsigned short swapfile_used[MAX_SWAPFILES]; | |
130 | static unsigned short root_swap; | |
131 | ||
c2ff18f4 AS |
132 | static int write_page(unsigned long addr, swp_entry_t * loc); |
133 | static int bio_read_page(pgoff_t page_off, void * page); | |
134 | ||
135 | static u8 key_iv[MAXKEY+MAXIV]; | |
136 | ||
137 | #ifdef CONFIG_SWSUSP_ENCRYPT | |
138 | ||
139 | static int crypto_init(int mode, void **mem) | |
140 | { | |
141 | int error = 0; | |
142 | int len; | |
143 | char *modemsg; | |
144 | struct crypto_tfm *tfm; | |
145 | ||
146 | modemsg = mode ? "suspend not possible" : "resume not possible"; | |
147 | ||
148 | tfm = crypto_alloc_tfm(CIPHER, CRYPTO_TFM_MODE_CBC); | |
149 | if(!tfm) { | |
150 | printk(KERN_ERR "swsusp: no tfm, %s\n", modemsg); | |
151 | error = -EINVAL; | |
152 | goto out; | |
153 | } | |
154 | ||
155 | if(MAXKEY < crypto_tfm_alg_min_keysize(tfm)) { | |
156 | printk(KERN_ERR "swsusp: key buffer too small, %s\n", modemsg); | |
157 | error = -ENOKEY; | |
158 | goto fail; | |
159 | } | |
160 | ||
161 | if (mode) | |
162 | get_random_bytes(key_iv, MAXKEY+MAXIV); | |
163 | ||
164 | len = crypto_tfm_alg_max_keysize(tfm); | |
165 | if (len > MAXKEY) | |
166 | len = MAXKEY; | |
167 | ||
168 | if (crypto_cipher_setkey(tfm, key_iv, len)) { | |
169 | printk(KERN_ERR "swsusp: key setup failure, %s\n", modemsg); | |
170 | error = -EKEYREJECTED; | |
171 | goto fail; | |
172 | } | |
173 | ||
174 | len = crypto_tfm_alg_ivsize(tfm); | |
175 | ||
176 | if (MAXIV < len) { | |
177 | printk(KERN_ERR "swsusp: iv buffer too small, %s\n", modemsg); | |
178 | error = -EOVERFLOW; | |
179 | goto fail; | |
180 | } | |
181 | ||
182 | crypto_cipher_set_iv(tfm, key_iv+MAXKEY, len); | |
183 | ||
184 | *mem=(void *)tfm; | |
185 | ||
186 | goto out; | |
187 | ||
188 | fail: crypto_free_tfm(tfm); | |
189 | out: return error; | |
190 | } | |
191 | ||
192 | static __inline__ void crypto_exit(void *mem) | |
193 | { | |
194 | crypto_free_tfm((struct crypto_tfm *)mem); | |
195 | } | |
196 | ||
197 | static __inline__ int crypto_write(struct pbe *p, void *mem) | |
198 | { | |
199 | int error = 0; | |
200 | struct scatterlist src, dst; | |
201 | ||
202 | src.page = virt_to_page(p->address); | |
203 | src.offset = 0; | |
204 | src.length = PAGE_SIZE; | |
205 | dst.page = virt_to_page((void *)&swsusp_header); | |
206 | dst.offset = 0; | |
207 | dst.length = PAGE_SIZE; | |
208 | ||
209 | error = crypto_cipher_encrypt((struct crypto_tfm *)mem, &dst, &src, | |
210 | PAGE_SIZE); | |
211 | ||
212 | if (!error) | |
213 | error = write_page((unsigned long)&swsusp_header, | |
214 | &(p->swap_address)); | |
215 | return error; | |
216 | } | |
217 | ||
218 | static __inline__ int crypto_read(struct pbe *p, void *mem) | |
219 | { | |
220 | int error = 0; | |
221 | struct scatterlist src, dst; | |
222 | ||
223 | error = bio_read_page(swp_offset(p->swap_address), (void *)p->address); | |
224 | if (!error) { | |
225 | src.offset = 0; | |
226 | src.length = PAGE_SIZE; | |
227 | dst.offset = 0; | |
228 | dst.length = PAGE_SIZE; | |
229 | src.page = dst.page = virt_to_page((void *)p->address); | |
230 | ||
231 | error = crypto_cipher_decrypt((struct crypto_tfm *)mem, &dst, | |
232 | &src, PAGE_SIZE); | |
233 | } | |
234 | return error; | |
235 | } | |
236 | #else | |
237 | static __inline__ int crypto_init(int mode, void *mem) | |
238 | { | |
239 | return 0; | |
240 | } | |
241 | ||
242 | static __inline__ void crypto_exit(void *mem) | |
243 | { | |
244 | } | |
245 | ||
246 | static __inline__ int crypto_write(struct pbe *p, void *mem) | |
247 | { | |
248 | return write_page(p->address, &(p->swap_address)); | |
249 | } | |
250 | ||
251 | static __inline__ int crypto_read(struct pbe *p, void *mem) | |
252 | { | |
253 | return bio_read_page(swp_offset(p->swap_address), (void *)p->address); | |
254 | } | |
255 | #endif | |
256 | ||
1da177e4 LT |
257 | static int mark_swapfiles(swp_entry_t prev) |
258 | { | |
259 | int error; | |
260 | ||
2e4d5822 | 261 | rw_swap_page_sync(READ, |
1da177e4 LT |
262 | swp_entry(root_swap, 0), |
263 | virt_to_page((unsigned long)&swsusp_header)); | |
264 | if (!memcmp("SWAP-SPACE",swsusp_header.sig, 10) || | |
265 | !memcmp("SWAPSPACE2",swsusp_header.sig, 10)) { | |
266 | memcpy(swsusp_header.orig_sig,swsusp_header.sig, 10); | |
267 | memcpy(swsusp_header.sig,SWSUSP_SIG, 10); | |
c2ff18f4 | 268 | memcpy(swsusp_header.key_iv, key_iv, MAXKEY+MAXIV); |
1da177e4 | 269 | swsusp_header.swsusp_info = prev; |
2e4d5822 | 270 | error = rw_swap_page_sync(WRITE, |
1da177e4 LT |
271 | swp_entry(root_swap, 0), |
272 | virt_to_page((unsigned long) | |
273 | &swsusp_header)); | |
274 | } else { | |
275 | pr_debug("swsusp: Partition is not swap space.\n"); | |
276 | error = -ENODEV; | |
277 | } | |
278 | return error; | |
279 | } | |
280 | ||
281 | /* | |
282 | * Check whether the swap device is the specified resume | |
283 | * device, irrespective of whether they are specified by | |
284 | * identical names. | |
285 | * | |
286 | * (Thus, device inode aliasing is allowed. You can say /dev/hda4 | |
287 | * instead of /dev/ide/host0/bus0/target0/lun0/part4 [if using devfs] | |
288 | * and they'll be considered the same device. This is *necessary* for | |
289 | * devfs, since the resume code can only recognize the form /dev/hda4, | |
290 | * but the suspend code would see the long name.) | |
291 | */ | |
292 | static int is_resume_device(const struct swap_info_struct *swap_info) | |
293 | { | |
294 | struct file *file = swap_info->swap_file; | |
295 | struct inode *inode = file->f_dentry->d_inode; | |
296 | ||
297 | return S_ISBLK(inode->i_mode) && | |
298 | swsusp_resume_device == MKDEV(imajor(inode), iminor(inode)); | |
299 | } | |
300 | ||
301 | static int swsusp_swap_check(void) /* This is called before saving image */ | |
302 | { | |
303 | int i, len; | |
2e4d5822 | 304 | |
1da177e4 LT |
305 | len=strlen(resume_file); |
306 | root_swap = 0xFFFF; | |
2e4d5822 | 307 | |
dae06ac4 | 308 | spin_lock(&swap_lock); |
2e4d5822 | 309 | for (i=0; i<MAX_SWAPFILES; i++) { |
dae06ac4 | 310 | if (!(swap_info[i].flags & SWP_WRITEOK)) { |
1da177e4 LT |
311 | swapfile_used[i]=SWAPFILE_UNUSED; |
312 | } else { | |
2e4d5822 | 313 | if (!len) { |
1da177e4 | 314 | printk(KERN_WARNING "resume= option should be used to set suspend device" ); |
2e4d5822 | 315 | if (root_swap == 0xFFFF) { |
1da177e4 LT |
316 | swapfile_used[i] = SWAPFILE_SUSPEND; |
317 | root_swap = i; | |
318 | } else | |
2e4d5822 | 319 | swapfile_used[i] = SWAPFILE_IGNORED; |
1da177e4 LT |
320 | } else { |
321 | /* we ignore all swap devices that are not the resume_file */ | |
322 | if (is_resume_device(&swap_info[i])) { | |
323 | swapfile_used[i] = SWAPFILE_SUSPEND; | |
324 | root_swap = i; | |
325 | } else { | |
326 | swapfile_used[i] = SWAPFILE_IGNORED; | |
327 | } | |
328 | } | |
329 | } | |
330 | } | |
dae06ac4 | 331 | spin_unlock(&swap_lock); |
1da177e4 LT |
332 | return (root_swap != 0xffff) ? 0 : -ENODEV; |
333 | } | |
334 | ||
335 | /** | |
336 | * This is called after saving image so modification | |
337 | * will be lost after resume... and that's what we want. | |
338 | * we make the device unusable. A new call to | |
2e4d5822 | 339 | * lock_swapdevices can unlock the devices. |
1da177e4 LT |
340 | */ |
341 | static void lock_swapdevices(void) | |
342 | { | |
343 | int i; | |
344 | ||
dae06ac4 | 345 | spin_lock(&swap_lock); |
2e4d5822 PM |
346 | for (i = 0; i< MAX_SWAPFILES; i++) |
347 | if (swapfile_used[i] == SWAPFILE_IGNORED) { | |
dae06ac4 | 348 | swap_info[i].flags ^= SWP_WRITEOK; |
1da177e4 | 349 | } |
dae06ac4 | 350 | spin_unlock(&swap_lock); |
1da177e4 LT |
351 | } |
352 | ||
353 | /** | |
8686bcd0 | 354 | * write_page - Write one page to a fresh swap location. |
1da177e4 LT |
355 | * @addr: Address we're writing. |
356 | * @loc: Place to store the entry we used. | |
357 | * | |
358 | * Allocate a new swap entry and 'sync' it. Note we discard -EIO | |
2e4d5822 | 359 | * errors. That is an artifact left over from swsusp. It did not |
1da177e4 LT |
360 | * check the return of rw_swap_page_sync() at all, since most pages |
361 | * written back to swap would return -EIO. | |
362 | * This is a partial improvement, since we will at least return other | |
363 | * errors, though we need to eventually fix the damn code. | |
364 | */ | |
365 | static int write_page(unsigned long addr, swp_entry_t * loc) | |
366 | { | |
367 | swp_entry_t entry; | |
368 | int error = 0; | |
369 | ||
370 | entry = get_swap_page(); | |
2e4d5822 | 371 | if (swp_offset(entry) && |
1da177e4 LT |
372 | swapfile_used[swp_type(entry)] == SWAPFILE_SUSPEND) { |
373 | error = rw_swap_page_sync(WRITE, entry, | |
374 | virt_to_page(addr)); | |
375 | if (error == -EIO) | |
376 | error = 0; | |
377 | if (!error) | |
378 | *loc = entry; | |
379 | } else | |
380 | error = -ENOSPC; | |
381 | return error; | |
382 | } | |
383 | ||
384 | /** | |
385 | * data_free - Free the swap entries used by the saved image. | |
386 | * | |
2e4d5822 | 387 | * Walk the list of used swap entries and free each one. |
1da177e4 LT |
388 | * This is only used for cleanup when suspend fails. |
389 | */ | |
390 | static void data_free(void) | |
391 | { | |
392 | swp_entry_t entry; | |
254b5477 | 393 | struct pbe * p; |
1da177e4 | 394 | |
254b5477 RW |
395 | for_each_pbe(p, pagedir_nosave) { |
396 | entry = p->swap_address; | |
1da177e4 LT |
397 | if (entry.val) |
398 | swap_free(entry); | |
399 | else | |
400 | break; | |
1da177e4 LT |
401 | } |
402 | } | |
403 | ||
404 | /** | |
405 | * data_write - Write saved image to swap. | |
406 | * | |
407 | * Walk the list of pages in the image and sync each one to swap. | |
408 | */ | |
409 | static int data_write(void) | |
410 | { | |
411 | int error = 0, i = 0; | |
412 | unsigned int mod = nr_copy_pages / 100; | |
413 | struct pbe *p; | |
c2ff18f4 AS |
414 | void *tfm; |
415 | ||
416 | if ((error = crypto_init(1, &tfm))) | |
417 | return error; | |
1da177e4 LT |
418 | |
419 | if (!mod) | |
420 | mod = 1; | |
421 | ||
422 | printk( "Writing data to swap (%d pages)... ", nr_copy_pages ); | |
2e4d5822 | 423 | for_each_pbe (p, pagedir_nosave) { |
1da177e4 LT |
424 | if (!(i%mod)) |
425 | printk( "\b\b\b\b%3d%%", i / mod ); | |
c2ff18f4 AS |
426 | if ((error = crypto_write(p, tfm))) { |
427 | crypto_exit(tfm); | |
1da177e4 | 428 | return error; |
c2ff18f4 | 429 | } |
1da177e4 LT |
430 | i++; |
431 | } | |
432 | printk("\b\b\b\bdone\n"); | |
c2ff18f4 | 433 | crypto_exit(tfm); |
1da177e4 LT |
434 | return error; |
435 | } | |
436 | ||
437 | static void dump_info(void) | |
438 | { | |
439 | pr_debug(" swsusp: Version: %u\n",swsusp_info.version_code); | |
440 | pr_debug(" swsusp: Num Pages: %ld\n",swsusp_info.num_physpages); | |
441 | pr_debug(" swsusp: UTS Sys: %s\n",swsusp_info.uts.sysname); | |
442 | pr_debug(" swsusp: UTS Node: %s\n",swsusp_info.uts.nodename); | |
443 | pr_debug(" swsusp: UTS Release: %s\n",swsusp_info.uts.release); | |
444 | pr_debug(" swsusp: UTS Version: %s\n",swsusp_info.uts.version); | |
445 | pr_debug(" swsusp: UTS Machine: %s\n",swsusp_info.uts.machine); | |
446 | pr_debug(" swsusp: UTS Domain: %s\n",swsusp_info.uts.domainname); | |
447 | pr_debug(" swsusp: CPUs: %d\n",swsusp_info.cpus); | |
448 | pr_debug(" swsusp: Image: %ld Pages\n",swsusp_info.image_pages); | |
449 | pr_debug(" swsusp: Pagedir: %ld Pages\n",swsusp_info.pagedir_pages); | |
450 | } | |
451 | ||
452 | static void init_header(void) | |
453 | { | |
454 | memset(&swsusp_info, 0, sizeof(swsusp_info)); | |
455 | swsusp_info.version_code = LINUX_VERSION_CODE; | |
456 | swsusp_info.num_physpages = num_physpages; | |
457 | memcpy(&swsusp_info.uts, &system_utsname, sizeof(system_utsname)); | |
458 | ||
459 | swsusp_info.suspend_pagedir = pagedir_nosave; | |
460 | swsusp_info.cpus = num_online_cpus(); | |
461 | swsusp_info.image_pages = nr_copy_pages; | |
462 | } | |
463 | ||
464 | static int close_swap(void) | |
465 | { | |
466 | swp_entry_t entry; | |
467 | int error; | |
468 | ||
469 | dump_info(); | |
470 | error = write_page((unsigned long)&swsusp_info, &entry); | |
2e4d5822 | 471 | if (!error) { |
1da177e4 LT |
472 | printk( "S" ); |
473 | error = mark_swapfiles(entry); | |
474 | printk( "|\n" ); | |
475 | } | |
476 | return error; | |
477 | } | |
478 | ||
479 | /** | |
480 | * free_pagedir_entries - Free pages used by the page directory. | |
481 | * | |
482 | * This is used during suspend for error recovery. | |
483 | */ | |
484 | ||
485 | static void free_pagedir_entries(void) | |
486 | { | |
487 | int i; | |
488 | ||
489 | for (i = 0; i < swsusp_info.pagedir_pages; i++) | |
490 | swap_free(swsusp_info.pagedir[i]); | |
491 | } | |
492 | ||
493 | ||
494 | /** | |
495 | * write_pagedir - Write the array of pages holding the page directory. | |
496 | * @last: Last swap entry we write (needed for header). | |
497 | */ | |
498 | ||
499 | static int write_pagedir(void) | |
500 | { | |
501 | int error = 0; | |
502 | unsigned n = 0; | |
503 | struct pbe * pbe; | |
504 | ||
505 | printk( "Writing pagedir..."); | |
2e4d5822 | 506 | for_each_pb_page (pbe, pagedir_nosave) { |
1da177e4 LT |
507 | if ((error = write_page((unsigned long)pbe, &swsusp_info.pagedir[n++]))) |
508 | return error; | |
509 | } | |
510 | ||
511 | swsusp_info.pagedir_pages = n; | |
512 | printk("done (%u pages)\n", n); | |
513 | return error; | |
514 | } | |
515 | ||
516 | /** | |
517 | * write_suspend_image - Write entire image and metadata. | |
518 | * | |
519 | */ | |
1da177e4 LT |
520 | static int write_suspend_image(void) |
521 | { | |
522 | int error; | |
523 | ||
524 | init_header(); | |
525 | if ((error = data_write())) | |
526 | goto FreeData; | |
527 | ||
528 | if ((error = write_pagedir())) | |
529 | goto FreePagedir; | |
530 | ||
531 | if ((error = close_swap())) | |
532 | goto FreePagedir; | |
533 | Done: | |
c2ff18f4 | 534 | memset(key_iv, 0, MAXKEY+MAXIV); |
1da177e4 LT |
535 | return error; |
536 | FreePagedir: | |
537 | free_pagedir_entries(); | |
538 | FreeData: | |
539 | data_free(); | |
540 | goto Done; | |
541 | } | |
542 | ||
1da177e4 LT |
543 | /** |
544 | * enough_swap - Make sure we have enough swap to save the image. | |
545 | * | |
2e4d5822 | 546 | * Returns TRUE or FALSE after checking the total amount of swap |
1da177e4 LT |
547 | * space avaiable. |
548 | * | |
549 | * FIXME: si_swapinfo(&i) returns all swap devices information. | |
2e4d5822 | 550 | * We should only consider resume_device. |
1da177e4 LT |
551 | */ |
552 | ||
a0f49651 | 553 | int enough_swap(unsigned nr_pages) |
1da177e4 LT |
554 | { |
555 | struct sysinfo i; | |
556 | ||
557 | si_swapinfo(&i); | |
351619ba | 558 | pr_debug("swsusp: available swap: %lu pages\n", i.freeswap); |
a0f49651 RW |
559 | return i.freeswap > (nr_pages + PAGES_FOR_IO + |
560 | (nr_pages + PBES_PER_PAGE - 1) / PBES_PER_PAGE); | |
1da177e4 LT |
561 | } |
562 | ||
1da177e4 LT |
563 | |
564 | /* It is important _NOT_ to umount filesystems at this point. We want | |
565 | * them synced (in case something goes wrong) but we DO not want to mark | |
566 | * filesystem clean: it is not. (And it does not matter, if we resume | |
567 | * correctly, we'll mark system clean, anyway.) | |
568 | */ | |
569 | int swsusp_write(void) | |
570 | { | |
571 | int error; | |
572 | device_resume(); | |
573 | lock_swapdevices(); | |
574 | error = write_suspend_image(); | |
575 | /* This will unlock ignored swap devices since writing is finished */ | |
576 | lock_swapdevices(); | |
577 | return error; | |
578 | ||
579 | } | |
580 | ||
581 | ||
1da177e4 LT |
582 | |
583 | int swsusp_suspend(void) | |
584 | { | |
585 | int error; | |
586 | if ((error = arch_prepare_suspend())) | |
587 | return error; | |
588 | local_irq_disable(); | |
589 | /* At this point, device_suspend() has been called, but *not* | |
590 | * device_power_down(). We *must* device_power_down() now. | |
591 | * Otherwise, drivers for some devices (e.g. interrupt controllers) | |
592 | * become desynchronized with the actual state of the hardware | |
593 | * at resume time, and evil weirdness ensues. | |
594 | */ | |
595 | if ((error = device_power_down(PMSG_FREEZE))) { | |
99dc7d63 | 596 | printk(KERN_ERR "Some devices failed to power down, aborting suspend\n"); |
1da177e4 | 597 | local_irq_enable(); |
1da177e4 LT |
598 | return error; |
599 | } | |
47b724f3 PM |
600 | |
601 | if ((error = swsusp_swap_check())) { | |
99dc7d63 PM |
602 | printk(KERN_ERR "swsusp: cannot find swap device, try swapon -a.\n"); |
603 | device_power_up(); | |
47b724f3 PM |
604 | local_irq_enable(); |
605 | return error; | |
606 | } | |
607 | ||
1da177e4 LT |
608 | save_processor_state(); |
609 | if ((error = swsusp_arch_suspend())) | |
99dc7d63 | 610 | printk(KERN_ERR "Error %d suspending\n", error); |
1da177e4 LT |
611 | /* Restore control flow magically appears here */ |
612 | restore_processor_state(); | |
1da177e4 LT |
613 | restore_highmem(); |
614 | device_power_up(); | |
615 | local_irq_enable(); | |
616 | return error; | |
617 | } | |
618 | ||
619 | int swsusp_resume(void) | |
620 | { | |
621 | int error; | |
622 | local_irq_disable(); | |
623 | if (device_power_down(PMSG_FREEZE)) | |
624 | printk(KERN_ERR "Some devices failed to power down, very bad\n"); | |
625 | /* We'll ignore saved state, but this gets preempt count (etc) right */ | |
626 | save_processor_state(); | |
627 | error = swsusp_arch_resume(); | |
628 | /* Code below is only ever reached in case of failure. Otherwise | |
629 | * execution continues at place where swsusp_arch_suspend was called | |
630 | */ | |
631 | BUG_ON(!error); | |
632 | restore_processor_state(); | |
633 | restore_highmem(); | |
8446f1d3 | 634 | touch_softlockup_watchdog(); |
1da177e4 LT |
635 | device_power_up(); |
636 | local_irq_enable(); | |
637 | return error; | |
638 | } | |
639 | ||
1da177e4 LT |
640 | /** |
641 | * On resume, for storing the PBE list and the image, | |
642 | * we can only use memory pages that do not conflict with the pages | |
643 | * which had been used before suspend. | |
644 | * | |
645 | * We don't know which pages are usable until we allocate them. | |
646 | * | |
647 | * Allocated but unusable (ie eaten) memory pages are linked together | |
648 | * to create a list, so that we can free them easily | |
649 | * | |
650 | * We could have used a type other than (void *) | |
651 | * for this purpose, but ... | |
652 | */ | |
653 | static void **eaten_memory = NULL; | |
654 | ||
655 | static inline void eat_page(void *page) | |
656 | { | |
657 | void **c; | |
658 | ||
659 | c = eaten_memory; | |
660 | eaten_memory = page; | |
661 | *eaten_memory = c; | |
662 | } | |
663 | ||
9796fdd8 | 664 | unsigned long get_usable_page(gfp_t gfp_mask) |
1da177e4 LT |
665 | { |
666 | unsigned long m; | |
667 | ||
668 | m = get_zeroed_page(gfp_mask); | |
8f9bdf15 | 669 | while (!PageNosaveFree(virt_to_page(m))) { |
1da177e4 LT |
670 | eat_page((void *)m); |
671 | m = get_zeroed_page(gfp_mask); | |
672 | if (!m) | |
673 | break; | |
674 | } | |
675 | return m; | |
676 | } | |
677 | ||
3dd08325 | 678 | void free_eaten_memory(void) |
1da177e4 LT |
679 | { |
680 | unsigned long m; | |
681 | void **c; | |
682 | int i = 0; | |
683 | ||
684 | c = eaten_memory; | |
685 | while (c) { | |
686 | m = (unsigned long)c; | |
687 | c = *c; | |
688 | free_page(m); | |
689 | i++; | |
690 | } | |
691 | eaten_memory = NULL; | |
692 | pr_debug("swsusp: %d unused pages freed\n", i); | |
693 | } | |
694 | ||
695 | /** | |
696 | * check_pagedir - We ensure here that pages that the PBEs point to | |
697 | * won't collide with pages where we're going to restore from the loaded | |
698 | * pages later | |
699 | */ | |
700 | ||
701 | static int check_pagedir(struct pbe *pblist) | |
702 | { | |
703 | struct pbe *p; | |
704 | ||
705 | /* This is necessary, so that we can free allocated pages | |
706 | * in case of failure | |
707 | */ | |
708 | for_each_pbe (p, pblist) | |
709 | p->address = 0UL; | |
710 | ||
711 | for_each_pbe (p, pblist) { | |
712 | p->address = get_usable_page(GFP_ATOMIC); | |
713 | if (!p->address) | |
714 | return -ENOMEM; | |
715 | } | |
716 | return 0; | |
717 | } | |
718 | ||
719 | /** | |
720 | * swsusp_pagedir_relocate - It is possible, that some memory pages | |
721 | * occupied by the list of PBEs collide with pages where we're going to | |
722 | * restore from the loaded pages later. We relocate them here. | |
723 | */ | |
724 | ||
725 | static struct pbe * swsusp_pagedir_relocate(struct pbe *pblist) | |
726 | { | |
727 | struct zone *zone; | |
728 | unsigned long zone_pfn; | |
729 | struct pbe *pbpage, *tail, *p; | |
730 | void *m; | |
731 | int rel = 0, error = 0; | |
732 | ||
733 | if (!pblist) /* a sanity check */ | |
734 | return NULL; | |
735 | ||
736 | pr_debug("swsusp: Relocating pagedir (%lu pages to check)\n", | |
737 | swsusp_info.pagedir_pages); | |
738 | ||
739 | /* Set page flags */ | |
740 | ||
2e4d5822 | 741 | for_each_zone (zone) { |
1da177e4 LT |
742 | for (zone_pfn = 0; zone_pfn < zone->spanned_pages; ++zone_pfn) |
743 | SetPageNosaveFree(pfn_to_page(zone_pfn + | |
744 | zone->zone_start_pfn)); | |
745 | } | |
746 | ||
747 | /* Clear orig addresses */ | |
748 | ||
749 | for_each_pbe (p, pblist) | |
750 | ClearPageNosaveFree(virt_to_page(p->orig_address)); | |
751 | ||
752 | tail = pblist + PB_PAGE_SKIP; | |
753 | ||
754 | /* Relocate colliding pages */ | |
755 | ||
756 | for_each_pb_page (pbpage, pblist) { | |
8f9bdf15 | 757 | if (!PageNosaveFree(virt_to_page((unsigned long)pbpage))) { |
1da177e4 LT |
758 | m = (void *)get_usable_page(GFP_ATOMIC | __GFP_COLD); |
759 | if (!m) { | |
760 | error = -ENOMEM; | |
761 | break; | |
762 | } | |
763 | memcpy(m, (void *)pbpage, PAGE_SIZE); | |
764 | if (pbpage == pblist) | |
765 | pblist = (struct pbe *)m; | |
766 | else | |
767 | tail->next = (struct pbe *)m; | |
768 | ||
769 | eat_page((void *)pbpage); | |
770 | pbpage = (struct pbe *)m; | |
771 | ||
772 | /* We have to link the PBEs again */ | |
773 | ||
774 | for (p = pbpage; p < pbpage + PB_PAGE_SKIP; p++) | |
775 | if (p->next) /* needed to save the end */ | |
776 | p->next = p + 1; | |
777 | ||
778 | rel++; | |
779 | } | |
780 | tail = pbpage + PB_PAGE_SKIP; | |
781 | } | |
782 | ||
783 | if (error) { | |
784 | printk("\nswsusp: Out of memory\n\n"); | |
785 | free_pagedir(pblist); | |
786 | free_eaten_memory(); | |
787 | pblist = NULL; | |
8686bcd0 PM |
788 | /* Is this even worth handling? It should never ever happen, and we |
789 | have just lost user's state, anyway... */ | |
790 | } else | |
1da177e4 LT |
791 | printk("swsusp: Relocated %d pages\n", rel); |
792 | ||
793 | return pblist; | |
794 | } | |
795 | ||
4dc3b16b | 796 | /* |
1da177e4 LT |
797 | * Using bio to read from swap. |
798 | * This code requires a bit more work than just using buffer heads | |
799 | * but, it is the recommended way for 2.5/2.6. | |
800 | * The following are to signal the beginning and end of I/O. Bios | |
801 | * finish asynchronously, while we want them to happen synchronously. | |
802 | * A simple atomic_t, and a wait loop take care of this problem. | |
803 | */ | |
804 | ||
805 | static atomic_t io_done = ATOMIC_INIT(0); | |
806 | ||
807 | static int end_io(struct bio * bio, unsigned int num, int err) | |
808 | { | |
809 | if (!test_bit(BIO_UPTODATE, &bio->bi_flags)) | |
810 | panic("I/O error reading memory image"); | |
811 | atomic_set(&io_done, 0); | |
812 | return 0; | |
813 | } | |
814 | ||
815 | static struct block_device * resume_bdev; | |
816 | ||
817 | /** | |
818 | * submit - submit BIO request. | |
819 | * @rw: READ or WRITE. | |
820 | * @off physical offset of page. | |
821 | * @page: page we're reading or writing. | |
822 | * | |
823 | * Straight from the textbook - allocate and initialize the bio. | |
824 | * If we're writing, make sure the page is marked as dirty. | |
825 | * Then submit it and wait. | |
826 | */ | |
827 | ||
828 | static int submit(int rw, pgoff_t page_off, void * page) | |
829 | { | |
830 | int error = 0; | |
831 | struct bio * bio; | |
832 | ||
833 | bio = bio_alloc(GFP_ATOMIC, 1); | |
834 | if (!bio) | |
835 | return -ENOMEM; | |
836 | bio->bi_sector = page_off * (PAGE_SIZE >> 9); | |
837 | bio_get(bio); | |
838 | bio->bi_bdev = resume_bdev; | |
839 | bio->bi_end_io = end_io; | |
840 | ||
841 | if (bio_add_page(bio, virt_to_page(page), PAGE_SIZE, 0) < PAGE_SIZE) { | |
842 | printk("swsusp: ERROR: adding page to bio at %ld\n",page_off); | |
843 | error = -EFAULT; | |
844 | goto Done; | |
845 | } | |
846 | ||
847 | if (rw == WRITE) | |
848 | bio_set_pages_dirty(bio); | |
849 | ||
850 | atomic_set(&io_done, 1); | |
851 | submit_bio(rw | (1 << BIO_RW_SYNC), bio); | |
852 | while (atomic_read(&io_done)) | |
853 | yield(); | |
854 | ||
855 | Done: | |
856 | bio_put(bio); | |
857 | return error; | |
858 | } | |
859 | ||
860 | static int bio_read_page(pgoff_t page_off, void * page) | |
861 | { | |
862 | return submit(READ, page_off, page); | |
863 | } | |
864 | ||
865 | static int bio_write_page(pgoff_t page_off, void * page) | |
866 | { | |
867 | return submit(WRITE, page_off, page); | |
868 | } | |
869 | ||
870 | /* | |
871 | * Sanity check if this image makes sense with this kernel/swap context | |
872 | * I really don't think that it's foolproof but more than nothing.. | |
873 | */ | |
874 | ||
875 | static const char * sanity_check(void) | |
876 | { | |
877 | dump_info(); | |
47b724f3 | 878 | if (swsusp_info.version_code != LINUX_VERSION_CODE) |
1da177e4 | 879 | return "kernel version"; |
47b724f3 | 880 | if (swsusp_info.num_physpages != num_physpages) |
1da177e4 LT |
881 | return "memory size"; |
882 | if (strcmp(swsusp_info.uts.sysname,system_utsname.sysname)) | |
883 | return "system type"; | |
884 | if (strcmp(swsusp_info.uts.release,system_utsname.release)) | |
885 | return "kernel release"; | |
886 | if (strcmp(swsusp_info.uts.version,system_utsname.version)) | |
887 | return "version"; | |
888 | if (strcmp(swsusp_info.uts.machine,system_utsname.machine)) | |
889 | return "machine"; | |
5a72e04d | 890 | #if 0 |
99dc7d63 PM |
891 | /* We can't use number of online CPUs when we use hotplug to remove them ;-))) */ |
892 | if (swsusp_info.cpus != num_possible_cpus()) | |
1da177e4 | 893 | return "number of cpus"; |
5a72e04d | 894 | #endif |
1da177e4 LT |
895 | return NULL; |
896 | } | |
897 | ||
898 | ||
899 | static int check_header(void) | |
900 | { | |
901 | const char * reason = NULL; | |
902 | int error; | |
903 | ||
904 | if ((error = bio_read_page(swp_offset(swsusp_header.swsusp_info), &swsusp_info))) | |
905 | return error; | |
906 | ||
907 | /* Is this same machine? */ | |
908 | if ((reason = sanity_check())) { | |
909 | printk(KERN_ERR "swsusp: Resume mismatch: %s\n",reason); | |
910 | return -EPERM; | |
911 | } | |
912 | nr_copy_pages = swsusp_info.image_pages; | |
913 | return error; | |
914 | } | |
915 | ||
916 | static int check_sig(void) | |
917 | { | |
918 | int error; | |
919 | ||
920 | memset(&swsusp_header, 0, sizeof(swsusp_header)); | |
921 | if ((error = bio_read_page(0, &swsusp_header))) | |
922 | return error; | |
923 | if (!memcmp(SWSUSP_SIG, swsusp_header.sig, 10)) { | |
924 | memcpy(swsusp_header.sig, swsusp_header.orig_sig, 10); | |
c2ff18f4 AS |
925 | memcpy(key_iv, swsusp_header.key_iv, MAXKEY+MAXIV); |
926 | memset(swsusp_header.key_iv, 0, MAXKEY+MAXIV); | |
1da177e4 LT |
927 | |
928 | /* | |
929 | * Reset swap signature now. | |
930 | */ | |
931 | error = bio_write_page(0, &swsusp_header); | |
932 | } else { | |
1da177e4 LT |
933 | return -EINVAL; |
934 | } | |
935 | if (!error) | |
936 | pr_debug("swsusp: Signature found, resuming\n"); | |
937 | return error; | |
938 | } | |
939 | ||
940 | /** | |
941 | * data_read - Read image pages from swap. | |
942 | * | |
943 | * You do not need to check for overlaps, check_pagedir() | |
944 | * already did that. | |
945 | */ | |
946 | ||
947 | static int data_read(struct pbe *pblist) | |
948 | { | |
949 | struct pbe * p; | |
950 | int error = 0; | |
951 | int i = 0; | |
952 | int mod = swsusp_info.image_pages / 100; | |
c2ff18f4 AS |
953 | void *tfm; |
954 | ||
955 | if ((error = crypto_init(0, &tfm))) | |
956 | return error; | |
1da177e4 LT |
957 | |
958 | if (!mod) | |
959 | mod = 1; | |
960 | ||
961 | printk("swsusp: Reading image data (%lu pages): ", | |
962 | swsusp_info.image_pages); | |
963 | ||
964 | for_each_pbe (p, pblist) { | |
965 | if (!(i % mod)) | |
966 | printk("\b\b\b\b%3d%%", i / mod); | |
967 | ||
c2ff18f4 AS |
968 | if ((error = crypto_read(p, tfm))) { |
969 | crypto_exit(tfm); | |
1da177e4 | 970 | return error; |
c2ff18f4 | 971 | } |
1da177e4 LT |
972 | |
973 | i++; | |
974 | } | |
975 | printk("\b\b\b\bdone\n"); | |
c2ff18f4 | 976 | crypto_exit(tfm); |
1da177e4 LT |
977 | return error; |
978 | } | |
979 | ||
1da177e4 LT |
980 | /** |
981 | * read_pagedir - Read page backup list pages from swap | |
982 | */ | |
983 | ||
984 | static int read_pagedir(struct pbe *pblist) | |
985 | { | |
986 | struct pbe *pbpage, *p; | |
987 | unsigned i = 0; | |
988 | int error; | |
989 | ||
990 | if (!pblist) | |
991 | return -EFAULT; | |
992 | ||
993 | printk("swsusp: Reading pagedir (%lu pages)\n", | |
994 | swsusp_info.pagedir_pages); | |
995 | ||
996 | for_each_pb_page (pbpage, pblist) { | |
997 | unsigned long offset = swp_offset(swsusp_info.pagedir[i++]); | |
998 | ||
999 | error = -EFAULT; | |
1000 | if (offset) { | |
1001 | p = (pbpage + PB_PAGE_SKIP)->next; | |
1002 | error = bio_read_page(offset, (void *)pbpage); | |
1003 | (pbpage + PB_PAGE_SKIP)->next = p; | |
1004 | } | |
1005 | if (error) | |
1006 | break; | |
1007 | } | |
1008 | ||
1009 | if (error) | |
f2d61379 RW |
1010 | free_pagedir(pblist); |
1011 | else | |
1012 | BUG_ON(i != swsusp_info.pagedir_pages); | |
1da177e4 LT |
1013 | |
1014 | return error; | |
1015 | } | |
1016 | ||
1017 | ||
1018 | static int check_suspend_image(void) | |
1019 | { | |
1020 | int error = 0; | |
1021 | ||
1022 | if ((error = check_sig())) | |
1023 | return error; | |
1024 | ||
1025 | if ((error = check_header())) | |
1026 | return error; | |
1027 | ||
1028 | return 0; | |
1029 | } | |
1030 | ||
1031 | static int read_suspend_image(void) | |
1032 | { | |
1033 | int error = 0; | |
1034 | struct pbe *p; | |
1035 | ||
1036 | if (!(p = alloc_pagedir(nr_copy_pages))) | |
1037 | return -ENOMEM; | |
1038 | ||
1039 | if ((error = read_pagedir(p))) | |
1040 | return error; | |
1041 | ||
1042 | create_pbe_list(p, nr_copy_pages); | |
1043 | ||
1044 | if (!(pagedir_nosave = swsusp_pagedir_relocate(p))) | |
1045 | return -ENOMEM; | |
1046 | ||
1047 | /* Allocate memory for the image and read the data from swap */ | |
1048 | ||
1049 | error = check_pagedir(pagedir_nosave); | |
3dd08325 | 1050 | |
1da177e4 LT |
1051 | if (!error) |
1052 | error = data_read(pagedir_nosave); | |
1053 | ||
1054 | if (error) { /* We fail cleanly */ | |
3dd08325 | 1055 | free_eaten_memory(); |
1da177e4 LT |
1056 | for_each_pbe (p, pagedir_nosave) |
1057 | if (p->address) { | |
1058 | free_page(p->address); | |
1059 | p->address = 0UL; | |
1060 | } | |
1061 | free_pagedir(pagedir_nosave); | |
1062 | } | |
1063 | return error; | |
1064 | } | |
1065 | ||
1066 | /** | |
1067 | * swsusp_check - Check for saved image in swap | |
1068 | */ | |
1069 | ||
1070 | int swsusp_check(void) | |
1071 | { | |
1072 | int error; | |
1073 | ||
1da177e4 LT |
1074 | resume_bdev = open_by_devnum(swsusp_resume_device, FMODE_READ); |
1075 | if (!IS_ERR(resume_bdev)) { | |
1076 | set_blocksize(resume_bdev, PAGE_SIZE); | |
1077 | error = check_suspend_image(); | |
1078 | if (error) | |
1079 | blkdev_put(resume_bdev); | |
1080 | } else | |
1081 | error = PTR_ERR(resume_bdev); | |
1082 | ||
1083 | if (!error) | |
1084 | pr_debug("swsusp: resume file found\n"); | |
1085 | else | |
1086 | pr_debug("swsusp: Error %d check for resume file\n", error); | |
1087 | return error; | |
1088 | } | |
1089 | ||
1090 | /** | |
1091 | * swsusp_read - Read saved image from swap. | |
1092 | */ | |
1093 | ||
1094 | int swsusp_read(void) | |
1095 | { | |
1096 | int error; | |
1097 | ||
1098 | if (IS_ERR(resume_bdev)) { | |
1099 | pr_debug("swsusp: block device not initialised\n"); | |
1100 | return PTR_ERR(resume_bdev); | |
1101 | } | |
1102 | ||
1103 | error = read_suspend_image(); | |
1104 | blkdev_put(resume_bdev); | |
c2ff18f4 | 1105 | memset(key_iv, 0, MAXKEY+MAXIV); |
1da177e4 LT |
1106 | |
1107 | if (!error) | |
1108 | pr_debug("swsusp: Reading resume file was successful\n"); | |
1109 | else | |
1110 | pr_debug("swsusp: Error %d resuming\n", error); | |
1111 | return error; | |
1112 | } | |
1113 | ||
1114 | /** | |
1115 | * swsusp_close - close swap device. | |
1116 | */ | |
1117 | ||
1118 | void swsusp_close(void) | |
1119 | { | |
1120 | if (IS_ERR(resume_bdev)) { | |
1121 | pr_debug("swsusp: block device not initialised\n"); | |
1122 | return; | |
1123 | } | |
1124 | ||
1125 | blkdev_put(resume_bdev); | |
1126 | } |