blob: fc50b5d2dd260d850e1f2993dc6c1ab9945f0f01 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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>
Rafael J. Wysocki25761b62005-10-30 14:59:56 -08008 * Copyright (C) 1998,2001-2005 Pavel Machek <pavel@suse.cz>
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 *
10 * This file is released under the GPLv2.
11 *
12 * I'd like to thank the following people for their work:
Pavel Machek2e4d5822005-06-25 14:55:12 -070013 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070014 * 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 *
Pavel Machek2e4d5822005-06-25 14:55:12 -070018 * Steve Doddi <dirk@loth.demon.co.uk>:
Linus Torvalds1da177e2005-04-16 15:20:36 -070019 * 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 *
Andreas Steinmetzc2ff18f2005-09-03 15:56:59 -070034 * Andreas Steinmetz <ast@domdv.de>:
35 * Added encrypted suspend option
36 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070037 * 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>
Andrew Mortond53d9f12005-07-12 13:58:07 -070069#include <linux/mount.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
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
Andreas Steinmetzc2ff18f2005-09-03 15:56:59 -070077#include <linux/random.h>
78#include <linux/crypto.h>
79#include <asm/scatterlist.h>
80
Linus Torvalds1da177e2005-04-16 15:20:36 -070081#include "power.h"
82
Andreas Steinmetzc2ff18f2005-09-03 15:56:59 -070083#define CIPHER "aes"
84#define MAXKEY 32
85#define MAXIV 32
86
Linus Torvalds1da177e2005-04-16 15:20:36 -070087extern char resume_file[];
88
89/* Local variables that should not be affected by save */
Rafael J. Wysocki25761b62005-10-30 14:59:56 -080090unsigned int nr_copy_pages __nosavedata = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
92/* Suspend pagedir is allocated before final copy, therefore it
Pavel Machek2e4d5822005-06-25 14:55:12 -070093 must be freed after resume
Linus Torvalds1da177e2005-04-16 15:20:36 -070094
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
Pavel Machek2e4d5822005-06-25 14:55:12 -070097 time of suspend, that must be freed. Second is "pagedir_nosave",
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 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 */
105suspend_pagedir_t *pagedir_nosave __nosavedata = NULL;
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800106suspend_pagedir_t *pagedir_save;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
108#define SWSUSP_SIG "S1SUSPEND"
109
110static struct swsusp_header {
Andreas Steinmetzc2ff18f2005-09-03 15:56:59 -0700111 char reserved[PAGE_SIZE - 20 - MAXKEY - MAXIV - sizeof(swp_entry_t)];
112 u8 key_iv[MAXKEY+MAXIV];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 swp_entry_t swsusp_info;
114 char orig_sig[10];
115 char sig[10];
116} __attribute__((packed, aligned(PAGE_SIZE))) swsusp_header;
117
118static struct swsusp_info swsusp_info;
119
120/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 * 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
129static unsigned short swapfile_used[MAX_SWAPFILES];
130static unsigned short root_swap;
131
Andreas Steinmetzc2ff18f2005-09-03 15:56:59 -0700132static int write_page(unsigned long addr, swp_entry_t * loc);
133static int bio_read_page(pgoff_t page_off, void * page);
134
135static u8 key_iv[MAXKEY+MAXIV];
136
137#ifdef CONFIG_SWSUSP_ENCRYPT
138
139static 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
188fail: crypto_free_tfm(tfm);
189out: return error;
190}
191
192static __inline__ void crypto_exit(void *mem)
193{
194 crypto_free_tfm((struct crypto_tfm *)mem);
195}
196
197static __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
218static __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
237static __inline__ int crypto_init(int mode, void *mem)
238{
239 return 0;
240}
241
242static __inline__ void crypto_exit(void *mem)
243{
244}
245
246static __inline__ int crypto_write(struct pbe *p, void *mem)
247{
248 return write_page(p->address, &(p->swap_address));
249}
250
251static __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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257static int mark_swapfiles(swp_entry_t prev)
258{
259 int error;
260
Pavel Machek2e4d5822005-06-25 14:55:12 -0700261 rw_swap_page_sync(READ,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 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);
Andreas Steinmetzc2ff18f2005-09-03 15:56:59 -0700268 memcpy(swsusp_header.key_iv, key_iv, MAXKEY+MAXIV);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 swsusp_header.swsusp_info = prev;
Pavel Machek2e4d5822005-06-25 14:55:12 -0700270 error = rw_swap_page_sync(WRITE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 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 */
292static 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
301static int swsusp_swap_check(void) /* This is called before saving image */
302{
303 int i, len;
Pavel Machek2e4d5822005-06-25 14:55:12 -0700304
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 len=strlen(resume_file);
306 root_swap = 0xFFFF;
Pavel Machek2e4d5822005-06-25 14:55:12 -0700307
Hugh Dickinsdae06ac2005-09-03 15:54:42 -0700308 spin_lock(&swap_lock);
Pavel Machek2e4d5822005-06-25 14:55:12 -0700309 for (i=0; i<MAX_SWAPFILES; i++) {
Hugh Dickinsdae06ac2005-09-03 15:54:42 -0700310 if (!(swap_info[i].flags & SWP_WRITEOK)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 swapfile_used[i]=SWAPFILE_UNUSED;
312 } else {
Pavel Machek2e4d5822005-06-25 14:55:12 -0700313 if (!len) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 printk(KERN_WARNING "resume= option should be used to set suspend device" );
Pavel Machek2e4d5822005-06-25 14:55:12 -0700315 if (root_swap == 0xFFFF) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 swapfile_used[i] = SWAPFILE_SUSPEND;
317 root_swap = i;
318 } else
Pavel Machek2e4d5822005-06-25 14:55:12 -0700319 swapfile_used[i] = SWAPFILE_IGNORED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 } 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 }
Hugh Dickinsdae06ac2005-09-03 15:54:42 -0700331 spin_unlock(&swap_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 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
Pavel Machek2e4d5822005-06-25 14:55:12 -0700339 * lock_swapdevices can unlock the devices.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 */
341static void lock_swapdevices(void)
342{
343 int i;
344
Hugh Dickinsdae06ac2005-09-03 15:54:42 -0700345 spin_lock(&swap_lock);
Pavel Machek2e4d5822005-06-25 14:55:12 -0700346 for (i = 0; i< MAX_SWAPFILES; i++)
347 if (swapfile_used[i] == SWAPFILE_IGNORED) {
Hugh Dickinsdae06ac2005-09-03 15:54:42 -0700348 swap_info[i].flags ^= SWP_WRITEOK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 }
Hugh Dickinsdae06ac2005-09-03 15:54:42 -0700350 spin_unlock(&swap_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351}
352
353/**
Pavel Machek8686bcd2005-09-22 21:44:11 -0700354 * write_page - Write one page to a fresh swap location.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 * @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
Pavel Machek2e4d5822005-06-25 14:55:12 -0700359 * errors. That is an artifact left over from swsusp. It did not
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 * 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 */
365static 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();
Pavel Machek2e4d5822005-06-25 14:55:12 -0700371 if (swp_offset(entry) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 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 *
Pavel Machek2e4d5822005-06-25 14:55:12 -0700387 * Walk the list of used swap entries and free each one.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 * This is only used for cleanup when suspend fails.
389 */
390static void data_free(void)
391{
392 swp_entry_t entry;
Rafael J. Wysocki254b5472005-09-27 21:45:31 -0700393 struct pbe * p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394
Rafael J. Wysocki254b5472005-09-27 21:45:31 -0700395 for_each_pbe(p, pagedir_nosave) {
396 entry = p->swap_address;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 if (entry.val)
398 swap_free(entry);
399 else
400 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 }
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 */
409static int data_write(void)
410{
411 int error = 0, i = 0;
412 unsigned int mod = nr_copy_pages / 100;
413 struct pbe *p;
Andreas Steinmetzc2ff18f2005-09-03 15:56:59 -0700414 void *tfm;
415
416 if ((error = crypto_init(1, &tfm)))
417 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418
419 if (!mod)
420 mod = 1;
421
422 printk( "Writing data to swap (%d pages)... ", nr_copy_pages );
Pavel Machek2e4d5822005-06-25 14:55:12 -0700423 for_each_pbe (p, pagedir_nosave) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 if (!(i%mod))
425 printk( "\b\b\b\b%3d%%", i / mod );
Andreas Steinmetzc2ff18f2005-09-03 15:56:59 -0700426 if ((error = crypto_write(p, tfm))) {
427 crypto_exit(tfm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 return error;
Andreas Steinmetzc2ff18f2005-09-03 15:56:59 -0700429 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 i++;
431 }
432 printk("\b\b\b\bdone\n");
Andreas Steinmetzc2ff18f2005-09-03 15:56:59 -0700433 crypto_exit(tfm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 return error;
435}
436
437static 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
452static 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
464static 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);
Pavel Machek2e4d5822005-06-25 14:55:12 -0700471 if (!error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 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
485static 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
499static int write_pagedir(void)
500{
501 int error = 0;
502 unsigned n = 0;
503 struct pbe * pbe;
504
505 printk( "Writing pagedir...");
Pavel Machek2e4d5822005-06-25 14:55:12 -0700506 for_each_pb_page (pbe, pagedir_nosave) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 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 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520static 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:
Andreas Steinmetzc2ff18f2005-09-03 15:56:59 -0700534 memset(key_iv, 0, MAXKEY+MAXIV);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 return error;
536 FreePagedir:
537 free_pagedir_entries();
538 FreeData:
539 data_free();
540 goto Done;
541}
542
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543/**
544 * enough_swap - Make sure we have enough swap to save the image.
545 *
Pavel Machek2e4d5822005-06-25 14:55:12 -0700546 * Returns TRUE or FALSE after checking the total amount of swap
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 * space avaiable.
548 *
549 * FIXME: si_swapinfo(&i) returns all swap devices information.
Pavel Machek2e4d5822005-06-25 14:55:12 -0700550 * We should only consider resume_device.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 */
552
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800553int enough_swap(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554{
555 struct sysinfo i;
556
557 si_swapinfo(&i);
Rafael J. Wysocki351619b2005-10-30 14:59:55 -0800558 pr_debug("swsusp: available swap: %lu pages\n", i.freeswap);
559 return i.freeswap > (nr_copy_pages + PAGES_FOR_IO +
560 nr_copy_pages/PBES_PER_PAGE + !!(nr_copy_pages%PBES_PER_PAGE));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561}
562
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563
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 */
569int 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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582
583int 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))) {
Pavel Machek99dc7d62005-09-03 15:57:05 -0700596 printk(KERN_ERR "Some devices failed to power down, aborting suspend\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 local_irq_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 return error;
599 }
Pavel Machek47b724f2005-07-07 17:56:44 -0700600
601 if ((error = swsusp_swap_check())) {
Pavel Machek99dc7d62005-09-03 15:57:05 -0700602 printk(KERN_ERR "swsusp: cannot find swap device, try swapon -a.\n");
603 device_power_up();
Pavel Machek47b724f2005-07-07 17:56:44 -0700604 local_irq_enable();
605 return error;
606 }
607
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 save_processor_state();
609 if ((error = swsusp_arch_suspend()))
Pavel Machek99dc7d62005-09-03 15:57:05 -0700610 printk(KERN_ERR "Error %d suspending\n", error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 /* Restore control flow magically appears here */
612 restore_processor_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 restore_highmem();
614 device_power_up();
615 local_irq_enable();
616 return error;
617}
618
619int 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();
Ingo Molnar8446f1d2005-09-06 15:16:27 -0700634 touch_softlockup_watchdog();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 device_power_up();
636 local_irq_enable();
637 return error;
638}
639
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640/**
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 */
653static void **eaten_memory = NULL;
654
655static 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
Al Viro9796fdd2005-10-21 03:22:03 -0400664unsigned long get_usable_page(gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665{
666 unsigned long m;
667
668 m = get_zeroed_page(gfp_mask);
Pavel Machek8f9bdf12005-06-25 14:55:12 -0700669 while (!PageNosaveFree(virt_to_page(m))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 eat_page((void *)m);
671 m = get_zeroed_page(gfp_mask);
672 if (!m)
673 break;
674 }
675 return m;
676}
677
Rafael J. Wysocki3dd08322005-10-09 21:19:40 +0200678void free_eaten_memory(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679{
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
701static 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
725static 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
Pavel Machek2e4d5822005-06-25 14:55:12 -0700741 for_each_zone (zone) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 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) {
Pavel Machek8f9bdf12005-06-25 14:55:12 -0700757 if (!PageNosaveFree(virt_to_page((unsigned long)pbpage))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 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;
Pavel Machek8686bcd2005-09-22 21:44:11 -0700788 /* Is this even worth handling? It should never ever happen, and we
789 have just lost user's state, anyway... */
790 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 printk("swsusp: Relocated %d pages\n", rel);
792
793 return pblist;
794}
795
Pavel Pisa4dc3b162005-05-01 08:59:25 -0700796/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 * 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
805static atomic_t io_done = ATOMIC_INIT(0);
806
807static 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
815static 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
828static 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
860static int bio_read_page(pgoff_t page_off, void * page)
861{
862 return submit(READ, page_off, page);
863}
864
865static 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
875static const char * sanity_check(void)
876{
877 dump_info();
Pavel Machek47b724f2005-07-07 17:56:44 -0700878 if (swsusp_info.version_code != LINUX_VERSION_CODE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 return "kernel version";
Pavel Machek47b724f2005-07-07 17:56:44 -0700880 if (swsusp_info.num_physpages != num_physpages)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 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";
Li Shaohua5a72e042005-06-25 14:55:06 -0700890#if 0
Pavel Machek99dc7d62005-09-03 15:57:05 -0700891 /* We can't use number of online CPUs when we use hotplug to remove them ;-))) */
892 if (swsusp_info.cpus != num_possible_cpus())
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 return "number of cpus";
Li Shaohua5a72e042005-06-25 14:55:06 -0700894#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 return NULL;
896}
897
898
899static 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
916static 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);
Andreas Steinmetzc2ff18f2005-09-03 15:56:59 -0700925 memcpy(key_iv, swsusp_header.key_iv, MAXKEY+MAXIV);
926 memset(swsusp_header.key_iv, 0, MAXKEY+MAXIV);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927
928 /*
929 * Reset swap signature now.
930 */
931 error = bio_write_page(0, &swsusp_header);
932 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 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
947static 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;
Andreas Steinmetzc2ff18f2005-09-03 15:56:59 -0700953 void *tfm;
954
955 if ((error = crypto_init(0, &tfm)))
956 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957
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
Andreas Steinmetzc2ff18f2005-09-03 15:56:59 -0700968 if ((error = crypto_read(p, tfm))) {
969 crypto_exit(tfm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 return error;
Andreas Steinmetzc2ff18f2005-09-03 15:56:59 -0700971 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972
973 i++;
974 }
975 printk("\b\b\b\bdone\n");
Andreas Steinmetzc2ff18f2005-09-03 15:56:59 -0700976 crypto_exit(tfm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 return error;
978}
979
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980/**
981 * read_pagedir - Read page backup list pages from swap
982 */
983
984static 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)
Rafael J. Wysockif2d61372005-09-27 21:45:32 -07001010 free_pagedir(pblist);
1011 else
1012 BUG_ON(i != swsusp_info.pagedir_pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013
1014 return error;
1015}
1016
1017
1018static 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
1031static 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);
Rafael J. Wysocki3dd08322005-10-09 21:19:40 +02001050
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 if (!error)
1052 error = data_read(pagedir_nosave);
1053
1054 if (error) { /* We fail cleanly */
Rafael J. Wysocki3dd08322005-10-09 21:19:40 +02001055 free_eaten_memory();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 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
1070int swsusp_check(void)
1071{
1072 int error;
1073
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 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
1094int 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);
Andreas Steinmetzc2ff18f2005-09-03 15:56:59 -07001105 memset(key_iv, 0, MAXKEY+MAXIV);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106
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
1118void 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}