blob: a1300717dc3f6547033d9740742291df661839cb [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/kernel/power/swsusp.c
3 *
Pavel Machek96bc7ae2005-10-30 14:59:58 -08004 * This file provides code to write suspend image to swap and read it back.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 *
6 * Copyright (C) 1998-2001 Gabor Kuti <seasons@fornax.hu>
Rafael J. Wysocki25761b62005-10-30 14:59:56 -08007 * Copyright (C) 1998,2001-2005 Pavel Machek <pavel@suse.cz>
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 *
9 * This file is released under the GPLv2.
10 *
11 * I'd like to thank the following people for their work:
Pavel Machek2e4d5822005-06-25 14:55:12 -070012 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 * Pavel Machek <pavel@ucw.cz>:
14 * Modifications, defectiveness pointing, being with me at the very beginning,
15 * suspend to swap space, stop all tasks. Port to 2.4.18-ac and 2.5.17.
16 *
Pavel Machek2e4d5822005-06-25 14:55:12 -070017 * Steve Doddi <dirk@loth.demon.co.uk>:
Linus Torvalds1da177e2005-04-16 15:20:36 -070018 * Support the possibility of hardware state restoring.
19 *
20 * Raph <grey.havens@earthling.net>:
21 * Support for preserving states of network devices and virtual console
22 * (including X and svgatextmode)
23 *
24 * Kurt Garloff <garloff@suse.de>:
25 * Straightened the critical function in order to prevent compilers from
26 * playing tricks with local variables.
27 *
28 * Andreas Mohr <a.mohr@mailto.de>
29 *
30 * Alex Badea <vampire@go.ro>:
31 * Fixed runaway init
32 *
Andreas Steinmetzc2ff18f2005-09-03 15:56:59 -070033 * Andreas Steinmetz <ast@domdv.de>:
34 * Added encrypted suspend option
35 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070036 * More state savers are welcome. Especially for the scsi layer...
37 *
38 * For TODOs,FIXMEs also look in Documentation/power/swsusp.txt
39 */
40
41#include <linux/module.h>
42#include <linux/mm.h>
43#include <linux/suspend.h>
44#include <linux/smp_lock.h>
45#include <linux/file.h>
46#include <linux/utsname.h>
47#include <linux/version.h>
48#include <linux/delay.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070049#include <linux/bitops.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070050#include <linux/spinlock.h>
51#include <linux/genhd.h>
52#include <linux/kernel.h>
53#include <linux/major.h>
54#include <linux/swap.h>
55#include <linux/pm.h>
56#include <linux/device.h>
57#include <linux/buffer_head.h>
58#include <linux/swapops.h>
59#include <linux/bootmem.h>
60#include <linux/syscalls.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070061#include <linux/highmem.h>
62#include <linux/bio.h>
63
64#include <asm/uaccess.h>
65#include <asm/mmu_context.h>
66#include <asm/pgtable.h>
67#include <asm/tlbflush.h>
68#include <asm/io.h>
69
Andreas Steinmetzc2ff18f2005-09-03 15:56:59 -070070#include <linux/random.h>
71#include <linux/crypto.h>
72#include <asm/scatterlist.h>
73
Linus Torvalds1da177e2005-04-16 15:20:36 -070074#include "power.h"
75
Andreas Steinmetzc2ff18f2005-09-03 15:56:59 -070076#define CIPHER "aes"
77#define MAXKEY 32
78#define MAXIV 32
79
Linus Torvalds1da177e2005-04-16 15:20:36 -070080extern char resume_file[];
81
82/* Local variables that should not be affected by save */
Rafael J. Wysocki25761b62005-10-30 14:59:56 -080083unsigned int nr_copy_pages __nosavedata = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070084
85/* Suspend pagedir is allocated before final copy, therefore it
Pavel Machek2e4d5822005-06-25 14:55:12 -070086 must be freed after resume
Linus Torvalds1da177e2005-04-16 15:20:36 -070087
88 Warning: this is evil. There are actually two pagedirs at time of
89 resume. One is "pagedir_save", which is empty frame allocated at
Pavel Machek2e4d5822005-06-25 14:55:12 -070090 time of suspend, that must be freed. Second is "pagedir_nosave",
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 allocated at time of resume, that travels through memory not to
92 collide with anything.
93
94 Warning: this is even more evil than it seems. Pagedirs this file
95 talks about are completely different from page directories used by
96 MMU hardware.
97 */
98suspend_pagedir_t *pagedir_nosave __nosavedata = NULL;
Rafael J. Wysocki25761b62005-10-30 14:59:56 -080099suspend_pagedir_t *pagedir_save;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
101#define SWSUSP_SIG "S1SUSPEND"
102
103static struct swsusp_header {
Andreas Steinmetzc2ff18f2005-09-03 15:56:59 -0700104 char reserved[PAGE_SIZE - 20 - MAXKEY - MAXIV - sizeof(swp_entry_t)];
105 u8 key_iv[MAXKEY+MAXIV];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 swp_entry_t swsusp_info;
107 char orig_sig[10];
108 char sig[10];
109} __attribute__((packed, aligned(PAGE_SIZE))) swsusp_header;
110
111static struct swsusp_info swsusp_info;
112
113/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 * Saving part...
115 */
116
117/* We memorize in swapfile_used what swap devices are used for suspension */
118#define SWAPFILE_UNUSED 0
119#define SWAPFILE_SUSPEND 1 /* This is the suspending device */
120#define SWAPFILE_IGNORED 2 /* Those are other swap devices ignored for suspension */
121
122static unsigned short swapfile_used[MAX_SWAPFILES];
123static unsigned short root_swap;
124
Pavel Machekdc19d502005-11-07 00:58:40 -0800125static int write_page(unsigned long addr, swp_entry_t *loc);
126static int bio_read_page(pgoff_t page_off, void *page);
Andreas Steinmetzc2ff18f2005-09-03 15:56:59 -0700127
128static u8 key_iv[MAXKEY+MAXIV];
129
130#ifdef CONFIG_SWSUSP_ENCRYPT
131
132static int crypto_init(int mode, void **mem)
133{
134 int error = 0;
135 int len;
136 char *modemsg;
137 struct crypto_tfm *tfm;
138
139 modemsg = mode ? "suspend not possible" : "resume not possible";
140
141 tfm = crypto_alloc_tfm(CIPHER, CRYPTO_TFM_MODE_CBC);
142 if(!tfm) {
143 printk(KERN_ERR "swsusp: no tfm, %s\n", modemsg);
144 error = -EINVAL;
145 goto out;
146 }
147
148 if(MAXKEY < crypto_tfm_alg_min_keysize(tfm)) {
149 printk(KERN_ERR "swsusp: key buffer too small, %s\n", modemsg);
150 error = -ENOKEY;
151 goto fail;
152 }
153
154 if (mode)
155 get_random_bytes(key_iv, MAXKEY+MAXIV);
156
157 len = crypto_tfm_alg_max_keysize(tfm);
158 if (len > MAXKEY)
159 len = MAXKEY;
160
161 if (crypto_cipher_setkey(tfm, key_iv, len)) {
162 printk(KERN_ERR "swsusp: key setup failure, %s\n", modemsg);
163 error = -EKEYREJECTED;
164 goto fail;
165 }
166
167 len = crypto_tfm_alg_ivsize(tfm);
168
169 if (MAXIV < len) {
170 printk(KERN_ERR "swsusp: iv buffer too small, %s\n", modemsg);
171 error = -EOVERFLOW;
172 goto fail;
173 }
174
175 crypto_cipher_set_iv(tfm, key_iv+MAXKEY, len);
176
177 *mem=(void *)tfm;
178
179 goto out;
180
181fail: crypto_free_tfm(tfm);
182out: return error;
183}
184
185static __inline__ void crypto_exit(void *mem)
186{
187 crypto_free_tfm((struct crypto_tfm *)mem);
188}
189
190static __inline__ int crypto_write(struct pbe *p, void *mem)
191{
192 int error = 0;
193 struct scatterlist src, dst;
194
195 src.page = virt_to_page(p->address);
196 src.offset = 0;
197 src.length = PAGE_SIZE;
198 dst.page = virt_to_page((void *)&swsusp_header);
199 dst.offset = 0;
200 dst.length = PAGE_SIZE;
201
202 error = crypto_cipher_encrypt((struct crypto_tfm *)mem, &dst, &src,
203 PAGE_SIZE);
204
205 if (!error)
206 error = write_page((unsigned long)&swsusp_header,
207 &(p->swap_address));
208 return error;
209}
210
211static __inline__ int crypto_read(struct pbe *p, void *mem)
212{
213 int error = 0;
214 struct scatterlist src, dst;
215
216 error = bio_read_page(swp_offset(p->swap_address), (void *)p->address);
217 if (!error) {
218 src.offset = 0;
219 src.length = PAGE_SIZE;
220 dst.offset = 0;
221 dst.length = PAGE_SIZE;
222 src.page = dst.page = virt_to_page((void *)p->address);
223
224 error = crypto_cipher_decrypt((struct crypto_tfm *)mem, &dst,
225 &src, PAGE_SIZE);
226 }
227 return error;
228}
229#else
230static __inline__ int crypto_init(int mode, void *mem)
231{
232 return 0;
233}
234
235static __inline__ void crypto_exit(void *mem)
236{
237}
238
239static __inline__ int crypto_write(struct pbe *p, void *mem)
240{
241 return write_page(p->address, &(p->swap_address));
242}
243
244static __inline__ int crypto_read(struct pbe *p, void *mem)
245{
246 return bio_read_page(swp_offset(p->swap_address), (void *)p->address);
247}
248#endif
249
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250static int mark_swapfiles(swp_entry_t prev)
251{
252 int error;
253
Pavel Machek2e4d5822005-06-25 14:55:12 -0700254 rw_swap_page_sync(READ,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 swp_entry(root_swap, 0),
256 virt_to_page((unsigned long)&swsusp_header));
257 if (!memcmp("SWAP-SPACE",swsusp_header.sig, 10) ||
258 !memcmp("SWAPSPACE2",swsusp_header.sig, 10)) {
259 memcpy(swsusp_header.orig_sig,swsusp_header.sig, 10);
260 memcpy(swsusp_header.sig,SWSUSP_SIG, 10);
Andreas Steinmetzc2ff18f2005-09-03 15:56:59 -0700261 memcpy(swsusp_header.key_iv, key_iv, MAXKEY+MAXIV);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 swsusp_header.swsusp_info = prev;
Pavel Machek2e4d5822005-06-25 14:55:12 -0700263 error = rw_swap_page_sync(WRITE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 swp_entry(root_swap, 0),
265 virt_to_page((unsigned long)
266 &swsusp_header));
267 } else {
268 pr_debug("swsusp: Partition is not swap space.\n");
269 error = -ENODEV;
270 }
271 return error;
272}
273
274/*
275 * Check whether the swap device is the specified resume
276 * device, irrespective of whether they are specified by
277 * identical names.
278 *
279 * (Thus, device inode aliasing is allowed. You can say /dev/hda4
280 * instead of /dev/ide/host0/bus0/target0/lun0/part4 [if using devfs]
281 * and they'll be considered the same device. This is *necessary* for
282 * devfs, since the resume code can only recognize the form /dev/hda4,
283 * but the suspend code would see the long name.)
284 */
285static int is_resume_device(const struct swap_info_struct *swap_info)
286{
287 struct file *file = swap_info->swap_file;
288 struct inode *inode = file->f_dentry->d_inode;
289
290 return S_ISBLK(inode->i_mode) &&
291 swsusp_resume_device == MKDEV(imajor(inode), iminor(inode));
292}
293
294static int swsusp_swap_check(void) /* This is called before saving image */
295{
296 int i, len;
Pavel Machek2e4d5822005-06-25 14:55:12 -0700297
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 len=strlen(resume_file);
299 root_swap = 0xFFFF;
Pavel Machek2e4d5822005-06-25 14:55:12 -0700300
Hugh Dickinsdae06ac2005-09-03 15:54:42 -0700301 spin_lock(&swap_lock);
Pavel Machek2e4d5822005-06-25 14:55:12 -0700302 for (i=0; i<MAX_SWAPFILES; i++) {
Hugh Dickinsdae06ac2005-09-03 15:54:42 -0700303 if (!(swap_info[i].flags & SWP_WRITEOK)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 swapfile_used[i]=SWAPFILE_UNUSED;
305 } else {
Pavel Machek2e4d5822005-06-25 14:55:12 -0700306 if (!len) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 printk(KERN_WARNING "resume= option should be used to set suspend device" );
Pavel Machek2e4d5822005-06-25 14:55:12 -0700308 if (root_swap == 0xFFFF) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 swapfile_used[i] = SWAPFILE_SUSPEND;
310 root_swap = i;
311 } else
Pavel Machek2e4d5822005-06-25 14:55:12 -0700312 swapfile_used[i] = SWAPFILE_IGNORED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 } else {
314 /* we ignore all swap devices that are not the resume_file */
315 if (is_resume_device(&swap_info[i])) {
316 swapfile_used[i] = SWAPFILE_SUSPEND;
317 root_swap = i;
318 } else {
319 swapfile_used[i] = SWAPFILE_IGNORED;
320 }
321 }
322 }
323 }
Hugh Dickinsdae06ac2005-09-03 15:54:42 -0700324 spin_unlock(&swap_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 return (root_swap != 0xffff) ? 0 : -ENODEV;
326}
327
328/**
329 * This is called after saving image so modification
330 * will be lost after resume... and that's what we want.
331 * we make the device unusable. A new call to
Pavel Machek2e4d5822005-06-25 14:55:12 -0700332 * lock_swapdevices can unlock the devices.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 */
334static void lock_swapdevices(void)
335{
336 int i;
337
Hugh Dickinsdae06ac2005-09-03 15:54:42 -0700338 spin_lock(&swap_lock);
Pavel Machek2e4d5822005-06-25 14:55:12 -0700339 for (i = 0; i< MAX_SWAPFILES; i++)
340 if (swapfile_used[i] == SWAPFILE_IGNORED) {
Hugh Dickinsdae06ac2005-09-03 15:54:42 -0700341 swap_info[i].flags ^= SWP_WRITEOK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 }
Hugh Dickinsdae06ac2005-09-03 15:54:42 -0700343 spin_unlock(&swap_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344}
345
346/**
Pavel Machek8686bcd2005-09-22 21:44:11 -0700347 * write_page - Write one page to a fresh swap location.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 * @addr: Address we're writing.
349 * @loc: Place to store the entry we used.
350 *
351 * Allocate a new swap entry and 'sync' it. Note we discard -EIO
Pavel Machek2e4d5822005-06-25 14:55:12 -0700352 * errors. That is an artifact left over from swsusp. It did not
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 * check the return of rw_swap_page_sync() at all, since most pages
354 * written back to swap would return -EIO.
355 * This is a partial improvement, since we will at least return other
356 * errors, though we need to eventually fix the damn code.
357 */
Pavel Machekdc19d502005-11-07 00:58:40 -0800358static int write_page(unsigned long addr, swp_entry_t *loc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359{
360 swp_entry_t entry;
361 int error = 0;
362
363 entry = get_swap_page();
Pavel Machek2e4d5822005-06-25 14:55:12 -0700364 if (swp_offset(entry) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 swapfile_used[swp_type(entry)] == SWAPFILE_SUSPEND) {
366 error = rw_swap_page_sync(WRITE, entry,
367 virt_to_page(addr));
368 if (error == -EIO)
369 error = 0;
370 if (!error)
371 *loc = entry;
372 } else
373 error = -ENOSPC;
374 return error;
375}
376
377/**
378 * data_free - Free the swap entries used by the saved image.
379 *
Pavel Machek2e4d5822005-06-25 14:55:12 -0700380 * Walk the list of used swap entries and free each one.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 * This is only used for cleanup when suspend fails.
382 */
383static void data_free(void)
384{
385 swp_entry_t entry;
Pavel Machekdc19d502005-11-07 00:58:40 -0800386 struct pbe *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387
Rafael J. Wysocki254b5472005-09-27 21:45:31 -0700388 for_each_pbe(p, pagedir_nosave) {
389 entry = p->swap_address;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 if (entry.val)
391 swap_free(entry);
392 else
393 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 }
395}
396
397/**
398 * data_write - Write saved image to swap.
399 *
400 * Walk the list of pages in the image and sync each one to swap.
401 */
402static int data_write(void)
403{
404 int error = 0, i = 0;
405 unsigned int mod = nr_copy_pages / 100;
406 struct pbe *p;
Andreas Steinmetzc2ff18f2005-09-03 15:56:59 -0700407 void *tfm;
408
409 if ((error = crypto_init(1, &tfm)))
410 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411
412 if (!mod)
413 mod = 1;
414
415 printk( "Writing data to swap (%d pages)... ", nr_copy_pages );
Pavel Machek2e4d5822005-06-25 14:55:12 -0700416 for_each_pbe (p, pagedir_nosave) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 if (!(i%mod))
418 printk( "\b\b\b\b%3d%%", i / mod );
Andreas Steinmetzc2ff18f2005-09-03 15:56:59 -0700419 if ((error = crypto_write(p, tfm))) {
420 crypto_exit(tfm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 return error;
Andreas Steinmetzc2ff18f2005-09-03 15:56:59 -0700422 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 i++;
424 }
425 printk("\b\b\b\bdone\n");
Andreas Steinmetzc2ff18f2005-09-03 15:56:59 -0700426 crypto_exit(tfm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 return error;
428}
429
430static void dump_info(void)
431{
432 pr_debug(" swsusp: Version: %u\n",swsusp_info.version_code);
433 pr_debug(" swsusp: Num Pages: %ld\n",swsusp_info.num_physpages);
434 pr_debug(" swsusp: UTS Sys: %s\n",swsusp_info.uts.sysname);
435 pr_debug(" swsusp: UTS Node: %s\n",swsusp_info.uts.nodename);
436 pr_debug(" swsusp: UTS Release: %s\n",swsusp_info.uts.release);
437 pr_debug(" swsusp: UTS Version: %s\n",swsusp_info.uts.version);
438 pr_debug(" swsusp: UTS Machine: %s\n",swsusp_info.uts.machine);
439 pr_debug(" swsusp: UTS Domain: %s\n",swsusp_info.uts.domainname);
440 pr_debug(" swsusp: CPUs: %d\n",swsusp_info.cpus);
441 pr_debug(" swsusp: Image: %ld Pages\n",swsusp_info.image_pages);
442 pr_debug(" swsusp: Pagedir: %ld Pages\n",swsusp_info.pagedir_pages);
443}
444
445static void init_header(void)
446{
447 memset(&swsusp_info, 0, sizeof(swsusp_info));
448 swsusp_info.version_code = LINUX_VERSION_CODE;
449 swsusp_info.num_physpages = num_physpages;
450 memcpy(&swsusp_info.uts, &system_utsname, sizeof(system_utsname));
451
452 swsusp_info.suspend_pagedir = pagedir_nosave;
453 swsusp_info.cpus = num_online_cpus();
454 swsusp_info.image_pages = nr_copy_pages;
455}
456
457static int close_swap(void)
458{
459 swp_entry_t entry;
460 int error;
461
462 dump_info();
463 error = write_page((unsigned long)&swsusp_info, &entry);
Pavel Machek2e4d5822005-06-25 14:55:12 -0700464 if (!error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 printk( "S" );
466 error = mark_swapfiles(entry);
467 printk( "|\n" );
468 }
469 return error;
470}
471
472/**
473 * free_pagedir_entries - Free pages used by the page directory.
474 *
475 * This is used during suspend for error recovery.
476 */
477
478static void free_pagedir_entries(void)
479{
480 int i;
481
482 for (i = 0; i < swsusp_info.pagedir_pages; i++)
483 swap_free(swsusp_info.pagedir[i]);
484}
485
486
487/**
488 * write_pagedir - Write the array of pages holding the page directory.
489 * @last: Last swap entry we write (needed for header).
490 */
491
492static int write_pagedir(void)
493{
494 int error = 0;
Pavel Machekdc19d502005-11-07 00:58:40 -0800495 unsigned int n = 0;
496 struct pbe *pbe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497
498 printk( "Writing pagedir...");
Pavel Machek2e4d5822005-06-25 14:55:12 -0700499 for_each_pb_page (pbe, pagedir_nosave) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 if ((error = write_page((unsigned long)pbe, &swsusp_info.pagedir[n++])))
501 return error;
502 }
503
504 swsusp_info.pagedir_pages = n;
505 printk("done (%u pages)\n", n);
506 return error;
507}
508
509/**
510 * write_suspend_image - Write entire image and metadata.
511 *
512 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513static int write_suspend_image(void)
514{
515 int error;
516
517 init_header();
518 if ((error = data_write()))
519 goto FreeData;
520
521 if ((error = write_pagedir()))
522 goto FreePagedir;
523
524 if ((error = close_swap()))
525 goto FreePagedir;
526 Done:
Andreas Steinmetzc2ff18f2005-09-03 15:56:59 -0700527 memset(key_iv, 0, MAXKEY+MAXIV);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 return error;
529 FreePagedir:
530 free_pagedir_entries();
531 FreeData:
532 data_free();
533 goto Done;
534}
535
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536/**
537 * enough_swap - Make sure we have enough swap to save the image.
538 *
Pavel Machek2e4d5822005-06-25 14:55:12 -0700539 * Returns TRUE or FALSE after checking the total amount of swap
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 * space avaiable.
541 *
542 * FIXME: si_swapinfo(&i) returns all swap devices information.
Pavel Machek2e4d5822005-06-25 14:55:12 -0700543 * We should only consider resume_device.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 */
545
Pavel Machekdc19d502005-11-07 00:58:40 -0800546int enough_swap(unsigned int nr_pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547{
548 struct sysinfo i;
549
550 si_swapinfo(&i);
Rafael J. Wysocki351619b2005-10-30 14:59:55 -0800551 pr_debug("swsusp: available swap: %lu pages\n", i.freeswap);
Rafael J. Wysockia0f49652005-10-30 14:59:57 -0800552 return i.freeswap > (nr_pages + PAGES_FOR_IO +
553 (nr_pages + PBES_PER_PAGE - 1) / PBES_PER_PAGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554}
555
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556
557/* It is important _NOT_ to umount filesystems at this point. We want
558 * them synced (in case something goes wrong) but we DO not want to mark
559 * filesystem clean: it is not. (And it does not matter, if we resume
560 * correctly, we'll mark system clean, anyway.)
561 */
562int swsusp_write(void)
563{
564 int error;
Rafael J. Wysocki0245b3e2005-10-30 15:00:01 -0800565
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 lock_swapdevices();
567 error = write_suspend_image();
568 /* This will unlock ignored swap devices since writing is finished */
569 lock_swapdevices();
570 return error;
571
572}
573
574
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575
576int swsusp_suspend(void)
577{
578 int error;
579 if ((error = arch_prepare_suspend()))
580 return error;
581 local_irq_disable();
582 /* At this point, device_suspend() has been called, but *not*
583 * device_power_down(). We *must* device_power_down() now.
584 * Otherwise, drivers for some devices (e.g. interrupt controllers)
585 * become desynchronized with the actual state of the hardware
586 * at resume time, and evil weirdness ensues.
587 */
588 if ((error = device_power_down(PMSG_FREEZE))) {
Pavel Machek99dc7d62005-09-03 15:57:05 -0700589 printk(KERN_ERR "Some devices failed to power down, aborting suspend\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 local_irq_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 return error;
592 }
Pavel Machek47b724f2005-07-07 17:56:44 -0700593
594 if ((error = swsusp_swap_check())) {
Pavel Machek99dc7d62005-09-03 15:57:05 -0700595 printk(KERN_ERR "swsusp: cannot find swap device, try swapon -a.\n");
596 device_power_up();
Pavel Machek47b724f2005-07-07 17:56:44 -0700597 local_irq_enable();
598 return error;
599 }
600
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 save_processor_state();
602 if ((error = swsusp_arch_suspend()))
Pavel Machek99dc7d62005-09-03 15:57:05 -0700603 printk(KERN_ERR "Error %d suspending\n", error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 /* Restore control flow magically appears here */
605 restore_processor_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 restore_highmem();
607 device_power_up();
608 local_irq_enable();
609 return error;
610}
611
612int swsusp_resume(void)
613{
614 int error;
615 local_irq_disable();
616 if (device_power_down(PMSG_FREEZE))
617 printk(KERN_ERR "Some devices failed to power down, very bad\n");
618 /* We'll ignore saved state, but this gets preempt count (etc) right */
619 save_processor_state();
620 error = swsusp_arch_resume();
621 /* Code below is only ever reached in case of failure. Otherwise
622 * execution continues at place where swsusp_arch_suspend was called
623 */
624 BUG_ON(!error);
Rafael J. Wysocki2c1b4a52005-10-30 14:59:58 -0800625 /* The only reason why swsusp_arch_resume() can fail is memory being
626 * very tight, so we have to free it as soon as we can to avoid
627 * subsequent failures
628 */
629 swsusp_free();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 restore_processor_state();
631 restore_highmem();
Ingo Molnar8446f1d2005-09-06 15:16:27 -0700632 touch_softlockup_watchdog();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 device_power_up();
634 local_irq_enable();
635 return error;
636}
637
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638/**
639 * On resume, for storing the PBE list and the image,
640 * we can only use memory pages that do not conflict with the pages
641 * which had been used before suspend.
642 *
643 * We don't know which pages are usable until we allocate them.
644 *
Rafael J. Wysocki2c1b4a52005-10-30 14:59:58 -0800645 * Allocated but unusable (ie eaten) memory pages are marked so that
646 * swsusp_free() can release them
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648
Rafael J. Wysocki2c1b4a52005-10-30 14:59:58 -0800649unsigned long get_safe_page(gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650{
651 unsigned long m;
652
Rafael J. Wysocki2c1b4a52005-10-30 14:59:58 -0800653 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 m = get_zeroed_page(gfp_mask);
Rafael J. Wysocki2c1b4a52005-10-30 14:59:58 -0800655 if (m && PageNosaveFree(virt_to_page(m)))
656 /* This is for swsusp_free() */
657 SetPageNosave(virt_to_page(m));
658 } while (m && PageNosaveFree(virt_to_page(m)));
659 if (m) {
660 /* This is for swsusp_free() */
661 SetPageNosave(virt_to_page(m));
662 SetPageNosaveFree(virt_to_page(m));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 }
664 return m;
665}
666
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667/**
668 * check_pagedir - We ensure here that pages that the PBEs point to
669 * won't collide with pages where we're going to restore from the loaded
670 * pages later
671 */
672
673static int check_pagedir(struct pbe *pblist)
674{
675 struct pbe *p;
676
677 /* This is necessary, so that we can free allocated pages
678 * in case of failure
679 */
680 for_each_pbe (p, pblist)
681 p->address = 0UL;
682
683 for_each_pbe (p, pblist) {
Rafael J. Wysocki2c1b4a52005-10-30 14:59:58 -0800684 p->address = get_safe_page(GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 if (!p->address)
686 return -ENOMEM;
687 }
688 return 0;
689}
690
691/**
692 * swsusp_pagedir_relocate - It is possible, that some memory pages
693 * occupied by the list of PBEs collide with pages where we're going to
694 * restore from the loaded pages later. We relocate them here.
695 */
696
Pavel Machekdc19d502005-11-07 00:58:40 -0800697static struct pbe *swsusp_pagedir_relocate(struct pbe *pblist)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698{
699 struct zone *zone;
700 unsigned long zone_pfn;
701 struct pbe *pbpage, *tail, *p;
702 void *m;
Rafael J. Wysocki2c1b4a52005-10-30 14:59:58 -0800703 int rel = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704
705 if (!pblist) /* a sanity check */
706 return NULL;
707
708 pr_debug("swsusp: Relocating pagedir (%lu pages to check)\n",
709 swsusp_info.pagedir_pages);
710
Rafael J. Wysocki2c1b4a52005-10-30 14:59:58 -0800711 /* Clear page flags */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712
Pavel Machek2e4d5822005-06-25 14:55:12 -0700713 for_each_zone (zone) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 for (zone_pfn = 0; zone_pfn < zone->spanned_pages; ++zone_pfn)
Rafael J. Wysocki2c1b4a52005-10-30 14:59:58 -0800715 if (pfn_valid(zone_pfn + zone->zone_start_pfn))
716 ClearPageNosaveFree(pfn_to_page(zone_pfn +
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 zone->zone_start_pfn));
718 }
719
Rafael J. Wysocki2c1b4a52005-10-30 14:59:58 -0800720 /* Mark orig addresses */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721
722 for_each_pbe (p, pblist)
Rafael J. Wysocki2c1b4a52005-10-30 14:59:58 -0800723 SetPageNosaveFree(virt_to_page(p->orig_address));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724
725 tail = pblist + PB_PAGE_SKIP;
726
727 /* Relocate colliding pages */
728
729 for_each_pb_page (pbpage, pblist) {
Rafael J. Wysocki2c1b4a52005-10-30 14:59:58 -0800730 if (PageNosaveFree(virt_to_page((unsigned long)pbpage))) {
731 m = (void *)get_safe_page(GFP_ATOMIC | __GFP_COLD);
732 if (!m)
733 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 memcpy(m, (void *)pbpage, PAGE_SIZE);
735 if (pbpage == pblist)
736 pblist = (struct pbe *)m;
737 else
738 tail->next = (struct pbe *)m;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 pbpage = (struct pbe *)m;
740
741 /* We have to link the PBEs again */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 for (p = pbpage; p < pbpage + PB_PAGE_SKIP; p++)
743 if (p->next) /* needed to save the end */
744 p->next = p + 1;
745
746 rel++;
747 }
748 tail = pbpage + PB_PAGE_SKIP;
749 }
750
Rafael J. Wysocki2c1b4a52005-10-30 14:59:58 -0800751 /* This is for swsusp_free() */
752 for_each_pb_page (pbpage, pblist) {
753 SetPageNosave(virt_to_page(pbpage));
754 SetPageNosaveFree(virt_to_page(pbpage));
755 }
756
757 printk("swsusp: Relocated %d pages\n", rel);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758
759 return pblist;
760}
761
Pavel Pisa4dc3b162005-05-01 08:59:25 -0700762/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 * Using bio to read from swap.
764 * This code requires a bit more work than just using buffer heads
765 * but, it is the recommended way for 2.5/2.6.
766 * The following are to signal the beginning and end of I/O. Bios
767 * finish asynchronously, while we want them to happen synchronously.
768 * A simple atomic_t, and a wait loop take care of this problem.
769 */
770
771static atomic_t io_done = ATOMIC_INIT(0);
772
Pavel Machekdc19d502005-11-07 00:58:40 -0800773static int end_io(struct bio *bio, unsigned int num, int err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774{
775 if (!test_bit(BIO_UPTODATE, &bio->bi_flags))
776 panic("I/O error reading memory image");
777 atomic_set(&io_done, 0);
778 return 0;
779}
780
Pavel Machekdc19d502005-11-07 00:58:40 -0800781static struct block_device *resume_bdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782
783/**
784 * submit - submit BIO request.
785 * @rw: READ or WRITE.
786 * @off physical offset of page.
787 * @page: page we're reading or writing.
788 *
789 * Straight from the textbook - allocate and initialize the bio.
790 * If we're writing, make sure the page is marked as dirty.
791 * Then submit it and wait.
792 */
793
Pavel Machekdc19d502005-11-07 00:58:40 -0800794static int submit(int rw, pgoff_t page_off, void *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795{
796 int error = 0;
Pavel Machekdc19d502005-11-07 00:58:40 -0800797 struct bio *bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798
799 bio = bio_alloc(GFP_ATOMIC, 1);
800 if (!bio)
801 return -ENOMEM;
802 bio->bi_sector = page_off * (PAGE_SIZE >> 9);
803 bio_get(bio);
804 bio->bi_bdev = resume_bdev;
805 bio->bi_end_io = end_io;
806
807 if (bio_add_page(bio, virt_to_page(page), PAGE_SIZE, 0) < PAGE_SIZE) {
808 printk("swsusp: ERROR: adding page to bio at %ld\n",page_off);
809 error = -EFAULT;
810 goto Done;
811 }
812
813 if (rw == WRITE)
814 bio_set_pages_dirty(bio);
815
816 atomic_set(&io_done, 1);
817 submit_bio(rw | (1 << BIO_RW_SYNC), bio);
818 while (atomic_read(&io_done))
819 yield();
820
821 Done:
822 bio_put(bio);
823 return error;
824}
825
Pavel Machekdc19d502005-11-07 00:58:40 -0800826static int bio_read_page(pgoff_t page_off, void *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827{
828 return submit(READ, page_off, page);
829}
830
Pavel Machekdc19d502005-11-07 00:58:40 -0800831static int bio_write_page(pgoff_t page_off, void *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832{
833 return submit(WRITE, page_off, page);
834}
835
836/*
837 * Sanity check if this image makes sense with this kernel/swap context
838 * I really don't think that it's foolproof but more than nothing..
839 */
840
Pavel Machekdc19d502005-11-07 00:58:40 -0800841static const char *sanity_check(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842{
843 dump_info();
Pavel Machek47b724f2005-07-07 17:56:44 -0700844 if (swsusp_info.version_code != LINUX_VERSION_CODE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 return "kernel version";
Pavel Machek47b724f2005-07-07 17:56:44 -0700846 if (swsusp_info.num_physpages != num_physpages)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 return "memory size";
848 if (strcmp(swsusp_info.uts.sysname,system_utsname.sysname))
849 return "system type";
850 if (strcmp(swsusp_info.uts.release,system_utsname.release))
851 return "kernel release";
852 if (strcmp(swsusp_info.uts.version,system_utsname.version))
853 return "version";
854 if (strcmp(swsusp_info.uts.machine,system_utsname.machine))
855 return "machine";
Li Shaohua5a72e042005-06-25 14:55:06 -0700856#if 0
Pavel Machek99dc7d62005-09-03 15:57:05 -0700857 /* We can't use number of online CPUs when we use hotplug to remove them ;-))) */
858 if (swsusp_info.cpus != num_possible_cpus())
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 return "number of cpus";
Li Shaohua5a72e042005-06-25 14:55:06 -0700860#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 return NULL;
862}
863
864
865static int check_header(void)
866{
Pavel Machekdc19d502005-11-07 00:58:40 -0800867 const char *reason = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 int error;
869
870 if ((error = bio_read_page(swp_offset(swsusp_header.swsusp_info), &swsusp_info)))
871 return error;
872
873 /* Is this same machine? */
874 if ((reason = sanity_check())) {
875 printk(KERN_ERR "swsusp: Resume mismatch: %s\n",reason);
876 return -EPERM;
877 }
878 nr_copy_pages = swsusp_info.image_pages;
879 return error;
880}
881
882static int check_sig(void)
883{
884 int error;
885
886 memset(&swsusp_header, 0, sizeof(swsusp_header));
887 if ((error = bio_read_page(0, &swsusp_header)))
888 return error;
889 if (!memcmp(SWSUSP_SIG, swsusp_header.sig, 10)) {
890 memcpy(swsusp_header.sig, swsusp_header.orig_sig, 10);
Andreas Steinmetzc2ff18f2005-09-03 15:56:59 -0700891 memcpy(key_iv, swsusp_header.key_iv, MAXKEY+MAXIV);
892 memset(swsusp_header.key_iv, 0, MAXKEY+MAXIV);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893
894 /*
895 * Reset swap signature now.
896 */
897 error = bio_write_page(0, &swsusp_header);
898 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 return -EINVAL;
900 }
901 if (!error)
902 pr_debug("swsusp: Signature found, resuming\n");
903 return error;
904}
905
906/**
907 * data_read - Read image pages from swap.
908 *
909 * You do not need to check for overlaps, check_pagedir()
910 * already did that.
911 */
912
913static int data_read(struct pbe *pblist)
914{
Pavel Machekdc19d502005-11-07 00:58:40 -0800915 struct pbe *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 int error = 0;
917 int i = 0;
918 int mod = swsusp_info.image_pages / 100;
Andreas Steinmetzc2ff18f2005-09-03 15:56:59 -0700919 void *tfm;
920
921 if ((error = crypto_init(0, &tfm)))
922 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923
924 if (!mod)
925 mod = 1;
926
927 printk("swsusp: Reading image data (%lu pages): ",
928 swsusp_info.image_pages);
929
930 for_each_pbe (p, pblist) {
931 if (!(i % mod))
932 printk("\b\b\b\b%3d%%", i / mod);
933
Andreas Steinmetzc2ff18f2005-09-03 15:56:59 -0700934 if ((error = crypto_read(p, tfm))) {
935 crypto_exit(tfm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 return error;
Andreas Steinmetzc2ff18f2005-09-03 15:56:59 -0700937 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938
939 i++;
940 }
941 printk("\b\b\b\bdone\n");
Andreas Steinmetzc2ff18f2005-09-03 15:56:59 -0700942 crypto_exit(tfm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 return error;
944}
945
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946/**
947 * read_pagedir - Read page backup list pages from swap
948 */
949
950static int read_pagedir(struct pbe *pblist)
951{
952 struct pbe *pbpage, *p;
Pavel Machekdc19d502005-11-07 00:58:40 -0800953 unsigned int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 int error;
955
956 if (!pblist)
957 return -EFAULT;
958
959 printk("swsusp: Reading pagedir (%lu pages)\n",
960 swsusp_info.pagedir_pages);
961
962 for_each_pb_page (pbpage, pblist) {
963 unsigned long offset = swp_offset(swsusp_info.pagedir[i++]);
964
965 error = -EFAULT;
966 if (offset) {
967 p = (pbpage + PB_PAGE_SKIP)->next;
968 error = bio_read_page(offset, (void *)pbpage);
969 (pbpage + PB_PAGE_SKIP)->next = p;
970 }
971 if (error)
972 break;
973 }
974
Rafael J. Wysocki2c1b4a52005-10-30 14:59:58 -0800975 if (!error)
Rafael J. Wysockif2d61372005-09-27 21:45:32 -0700976 BUG_ON(i != swsusp_info.pagedir_pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977
978 return error;
979}
980
981
982static int check_suspend_image(void)
983{
984 int error = 0;
985
986 if ((error = check_sig()))
987 return error;
988
989 if ((error = check_header()))
990 return error;
991
992 return 0;
993}
994
995static int read_suspend_image(void)
996{
997 int error = 0;
998 struct pbe *p;
999
1000 if (!(p = alloc_pagedir(nr_copy_pages)))
1001 return -ENOMEM;
1002
1003 if ((error = read_pagedir(p)))
1004 return error;
1005
1006 create_pbe_list(p, nr_copy_pages);
1007
1008 if (!(pagedir_nosave = swsusp_pagedir_relocate(p)))
1009 return -ENOMEM;
1010
1011 /* Allocate memory for the image and read the data from swap */
1012
1013 error = check_pagedir(pagedir_nosave);
Rafael J. Wysocki3dd08322005-10-09 21:19:40 +02001014
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 if (!error)
1016 error = data_read(pagedir_nosave);
1017
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 return error;
1019}
1020
1021/**
1022 * swsusp_check - Check for saved image in swap
1023 */
1024
1025int swsusp_check(void)
1026{
1027 int error;
1028
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 resume_bdev = open_by_devnum(swsusp_resume_device, FMODE_READ);
1030 if (!IS_ERR(resume_bdev)) {
1031 set_blocksize(resume_bdev, PAGE_SIZE);
1032 error = check_suspend_image();
1033 if (error)
1034 blkdev_put(resume_bdev);
1035 } else
1036 error = PTR_ERR(resume_bdev);
1037
1038 if (!error)
1039 pr_debug("swsusp: resume file found\n");
1040 else
1041 pr_debug("swsusp: Error %d check for resume file\n", error);
1042 return error;
1043}
1044
1045/**
1046 * swsusp_read - Read saved image from swap.
1047 */
1048
1049int swsusp_read(void)
1050{
1051 int error;
1052
1053 if (IS_ERR(resume_bdev)) {
1054 pr_debug("swsusp: block device not initialised\n");
1055 return PTR_ERR(resume_bdev);
1056 }
1057
1058 error = read_suspend_image();
1059 blkdev_put(resume_bdev);
Andreas Steinmetzc2ff18f2005-09-03 15:56:59 -07001060 memset(key_iv, 0, MAXKEY+MAXIV);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061
1062 if (!error)
1063 pr_debug("swsusp: Reading resume file was successful\n");
1064 else
1065 pr_debug("swsusp: Error %d resuming\n", error);
1066 return error;
1067}
1068
1069/**
1070 * swsusp_close - close swap device.
1071 */
1072
1073void swsusp_close(void)
1074{
1075 if (IS_ERR(resume_bdev)) {
1076 pr_debug("swsusp: block device not initialised\n");
1077 return;
1078 }
1079
1080 blkdev_put(resume_bdev);
1081}