2 * linux/kernel/power/swsusp.c
4 * This file provides code to write suspend image to swap and read it back.
6 * Copyright (C) 1998-2001 Gabor Kuti <seasons@fornax.hu>
7 * Copyright (C) 1998,2001-2005 Pavel Machek <pavel@suse.cz>
9 * This file is released under the GPLv2.
11 * I'd like to thank the following people for their work:
13 * 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.
17 * Steve Doddi <dirk@loth.demon.co.uk>:
18 * Support the possibility of hardware state restoring.
20 * Raph <grey.havens@earthling.net>:
21 * Support for preserving states of network devices and virtual console
22 * (including X and svgatextmode)
24 * Kurt Garloff <garloff@suse.de>:
25 * Straightened the critical function in order to prevent compilers from
26 * playing tricks with local variables.
28 * Andreas Mohr <a.mohr@mailto.de>
30 * Alex Badea <vampire@go.ro>:
33 * Andreas Steinmetz <ast@domdv.de>:
34 * Added encrypted suspend option
36 * More state savers are welcome. Especially for the scsi layer...
38 * For TODOs,FIXMEs also look in Documentation/power/swsusp.txt
41 #include <linux/module.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>
49 #include <linux/bitops.h>
50 #include <linux/spinlock.h>
51 #include <linux/genhd.h>
52 #include <linux/kernel.h>
53 #include <linux/major.h>
54 #include <linux/swap.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>
61 #include <linux/highmem.h>
62 #include <linux/bio.h>
64 #include <asm/uaccess.h>
65 #include <asm/mmu_context.h>
66 #include <asm/pgtable.h>
67 #include <asm/tlbflush.h>
70 #include <linux/random.h>
71 #include <linux/crypto.h>
72 #include <asm/scatterlist.h>
77 int save_highmem(void);
78 int restore_highmem(void);
80 static int save_highmem(void) { return 0; }
81 static int restore_highmem(void) { return 0; }
88 extern char resume_file[];
90 /* Local variables that should not be affected by save */
91 unsigned int nr_copy_pages __nosavedata = 0;
93 /* Suspend pagedir is allocated before final copy, therefore it
94 must be freed after resume
96 Warning: this is even more evil than it seems. Pagedirs this file
97 talks about are completely different from page directories used by
100 suspend_pagedir_t *pagedir_nosave __nosavedata = NULL;
102 #define SWSUSP_SIG "S1SUSPEND"
104 static struct swsusp_header {
105 char reserved[PAGE_SIZE - 20 - MAXKEY - MAXIV - sizeof(swp_entry_t)];
106 u8 key_iv[MAXKEY+MAXIV];
107 swp_entry_t swsusp_info;
110 } __attribute__((packed, aligned(PAGE_SIZE))) swsusp_header;
112 static struct swsusp_info swsusp_info;
118 /* We memorize in swapfile_used what swap devices are used for suspension */
119 #define SWAPFILE_UNUSED 0
120 #define SWAPFILE_SUSPEND 1 /* This is the suspending device */
121 #define SWAPFILE_IGNORED 2 /* Those are other swap devices ignored for suspension */
123 static unsigned short swapfile_used[MAX_SWAPFILES];
124 static unsigned short root_swap;
126 static int write_page(unsigned long addr, swp_entry_t *loc);
127 static int bio_read_page(pgoff_t page_off, void *page);
129 static u8 key_iv[MAXKEY+MAXIV];
131 #ifdef CONFIG_SWSUSP_ENCRYPT
133 static int crypto_init(int mode, void **mem)
138 struct crypto_tfm *tfm;
140 modemsg = mode ? "suspend not possible" : "resume not possible";
142 tfm = crypto_alloc_tfm(CIPHER, CRYPTO_TFM_MODE_CBC);
144 printk(KERN_ERR "swsusp: no tfm, %s\n", modemsg);
149 if(MAXKEY < crypto_tfm_alg_min_keysize(tfm)) {
150 printk(KERN_ERR "swsusp: key buffer too small, %s\n", modemsg);
156 get_random_bytes(key_iv, MAXKEY+MAXIV);
158 len = crypto_tfm_alg_max_keysize(tfm);
162 if (crypto_cipher_setkey(tfm, key_iv, len)) {
163 printk(KERN_ERR "swsusp: key setup failure, %s\n", modemsg);
164 error = -EKEYREJECTED;
168 len = crypto_tfm_alg_ivsize(tfm);
171 printk(KERN_ERR "swsusp: iv buffer too small, %s\n", modemsg);
176 crypto_cipher_set_iv(tfm, key_iv+MAXKEY, len);
182 fail: crypto_free_tfm(tfm);
186 static __inline__ void crypto_exit(void *mem)
188 crypto_free_tfm((struct crypto_tfm *)mem);
191 static __inline__ int crypto_write(struct pbe *p, void *mem)
194 struct scatterlist src, dst;
196 src.page = virt_to_page(p->address);
198 src.length = PAGE_SIZE;
199 dst.page = virt_to_page((void *)&swsusp_header);
201 dst.length = PAGE_SIZE;
203 error = crypto_cipher_encrypt((struct crypto_tfm *)mem, &dst, &src,
207 error = write_page((unsigned long)&swsusp_header,
212 static __inline__ int crypto_read(struct pbe *p, void *mem)
215 struct scatterlist src, dst;
217 error = bio_read_page(swp_offset(p->swap_address), (void *)p->address);
220 src.length = PAGE_SIZE;
222 dst.length = PAGE_SIZE;
223 src.page = dst.page = virt_to_page((void *)p->address);
225 error = crypto_cipher_decrypt((struct crypto_tfm *)mem, &dst,
231 static __inline__ int crypto_init(int mode, void *mem)
236 static __inline__ void crypto_exit(void *mem)
240 static __inline__ int crypto_write(struct pbe *p, void *mem)
242 return write_page(p->address, &(p->swap_address));
245 static __inline__ int crypto_read(struct pbe *p, void *mem)
247 return bio_read_page(swp_offset(p->swap_address), (void *)p->address);
251 static int mark_swapfiles(swp_entry_t prev)
255 rw_swap_page_sync(READ,
256 swp_entry(root_swap, 0),
257 virt_to_page((unsigned long)&swsusp_header));
258 if (!memcmp("SWAP-SPACE",swsusp_header.sig, 10) ||
259 !memcmp("SWAPSPACE2",swsusp_header.sig, 10)) {
260 memcpy(swsusp_header.orig_sig,swsusp_header.sig, 10);
261 memcpy(swsusp_header.sig,SWSUSP_SIG, 10);
262 memcpy(swsusp_header.key_iv, key_iv, MAXKEY+MAXIV);
263 swsusp_header.swsusp_info = prev;
264 error = rw_swap_page_sync(WRITE,
265 swp_entry(root_swap, 0),
266 virt_to_page((unsigned long)
269 pr_debug("swsusp: Partition is not swap space.\n");
276 * Check whether the swap device is the specified resume
277 * device, irrespective of whether they are specified by
280 * (Thus, device inode aliasing is allowed. You can say /dev/hda4
281 * instead of /dev/ide/host0/bus0/target0/lun0/part4 [if using devfs]
282 * and they'll be considered the same device. This is *necessary* for
283 * devfs, since the resume code can only recognize the form /dev/hda4,
284 * but the suspend code would see the long name.)
286 static int is_resume_device(const struct swap_info_struct *swap_info)
288 struct file *file = swap_info->swap_file;
289 struct inode *inode = file->f_dentry->d_inode;
291 return S_ISBLK(inode->i_mode) &&
292 swsusp_resume_device == MKDEV(imajor(inode), iminor(inode));
295 static int swsusp_swap_check(void) /* This is called before saving image */
299 len=strlen(resume_file);
302 spin_lock(&swap_lock);
303 for (i=0; i<MAX_SWAPFILES; i++) {
304 if (!(swap_info[i].flags & SWP_WRITEOK)) {
305 swapfile_used[i]=SWAPFILE_UNUSED;
308 printk(KERN_WARNING "resume= option should be used to set suspend device" );
309 if (root_swap == 0xFFFF) {
310 swapfile_used[i] = SWAPFILE_SUSPEND;
313 swapfile_used[i] = SWAPFILE_IGNORED;
315 /* we ignore all swap devices that are not the resume_file */
316 if (is_resume_device(&swap_info[i])) {
317 swapfile_used[i] = SWAPFILE_SUSPEND;
320 swapfile_used[i] = SWAPFILE_IGNORED;
325 spin_unlock(&swap_lock);
326 return (root_swap != 0xffff) ? 0 : -ENODEV;
330 * This is called after saving image so modification
331 * will be lost after resume... and that's what we want.
332 * we make the device unusable. A new call to
333 * lock_swapdevices can unlock the devices.
335 static void lock_swapdevices(void)
339 spin_lock(&swap_lock);
340 for (i = 0; i< MAX_SWAPFILES; i++)
341 if (swapfile_used[i] == SWAPFILE_IGNORED) {
342 swap_info[i].flags ^= SWP_WRITEOK;
344 spin_unlock(&swap_lock);
348 * write_page - Write one page to a fresh swap location.
349 * @addr: Address we're writing.
350 * @loc: Place to store the entry we used.
352 * Allocate a new swap entry and 'sync' it. Note we discard -EIO
353 * errors. That is an artifact left over from swsusp. It did not
354 * check the return of rw_swap_page_sync() at all, since most pages
355 * written back to swap would return -EIO.
356 * This is a partial improvement, since we will at least return other
357 * errors, though we need to eventually fix the damn code.
359 static int write_page(unsigned long addr, swp_entry_t *loc)
364 entry = get_swap_page();
365 if (swp_offset(entry) &&
366 swapfile_used[swp_type(entry)] == SWAPFILE_SUSPEND) {
367 error = rw_swap_page_sync(WRITE, entry,
379 * data_free - Free the swap entries used by the saved image.
381 * Walk the list of used swap entries and free each one.
382 * This is only used for cleanup when suspend fails.
384 static void data_free(void)
389 for_each_pbe (p, pagedir_nosave) {
390 entry = p->swap_address;
399 * data_write - Write saved image to swap.
401 * Walk the list of pages in the image and sync each one to swap.
403 static int data_write(void)
405 int error = 0, i = 0;
406 unsigned int mod = nr_copy_pages / 100;
410 if ((error = crypto_init(1, &tfm)))
416 printk( "Writing data to swap (%d pages)... ", nr_copy_pages );
417 for_each_pbe (p, pagedir_nosave) {
419 printk( "\b\b\b\b%3d%%", i / mod );
420 if ((error = crypto_write(p, tfm))) {
426 printk("\b\b\b\bdone\n");
431 static void dump_info(void)
433 pr_debug(" swsusp: Version: %u\n",swsusp_info.version_code);
434 pr_debug(" swsusp: Num Pages: %ld\n",swsusp_info.num_physpages);
435 pr_debug(" swsusp: UTS Sys: %s\n",swsusp_info.uts.sysname);
436 pr_debug(" swsusp: UTS Node: %s\n",swsusp_info.uts.nodename);
437 pr_debug(" swsusp: UTS Release: %s\n",swsusp_info.uts.release);
438 pr_debug(" swsusp: UTS Version: %s\n",swsusp_info.uts.version);
439 pr_debug(" swsusp: UTS Machine: %s\n",swsusp_info.uts.machine);
440 pr_debug(" swsusp: UTS Domain: %s\n",swsusp_info.uts.domainname);
441 pr_debug(" swsusp: CPUs: %d\n",swsusp_info.cpus);
442 pr_debug(" swsusp: Image: %ld Pages\n",swsusp_info.image_pages);
443 pr_debug(" swsusp: Pagedir: %ld Pages\n",swsusp_info.pagedir_pages);
446 static void init_header(void)
448 memset(&swsusp_info, 0, sizeof(swsusp_info));
449 swsusp_info.version_code = LINUX_VERSION_CODE;
450 swsusp_info.num_physpages = num_physpages;
451 memcpy(&swsusp_info.uts, &system_utsname, sizeof(system_utsname));
453 swsusp_info.suspend_pagedir = pagedir_nosave;
454 swsusp_info.cpus = num_online_cpus();
455 swsusp_info.image_pages = nr_copy_pages;
458 static int close_swap(void)
464 error = write_page((unsigned long)&swsusp_info, &entry);
467 error = mark_swapfiles(entry);
474 * free_pagedir_entries - Free pages used by the page directory.
476 * This is used during suspend for error recovery.
479 static void free_pagedir_entries(void)
483 for (i = 0; i < swsusp_info.pagedir_pages; i++)
484 swap_free(swsusp_info.pagedir[i]);
489 * write_pagedir - Write the array of pages holding the page directory.
490 * @last: Last swap entry we write (needed for header).
493 static int write_pagedir(void)
499 printk( "Writing pagedir...");
500 for_each_pb_page (pbe, pagedir_nosave) {
501 if ((error = write_page((unsigned long)pbe, &swsusp_info.pagedir[n++])))
505 swsusp_info.pagedir_pages = n;
506 printk("done (%u pages)\n", n);
511 * enough_swap - Make sure we have enough swap to save the image.
513 * Returns TRUE or FALSE after checking the total amount of swap
516 * FIXME: si_swapinfo(&i) returns all swap devices information.
517 * We should only consider resume_device.
520 static int enough_swap(unsigned int nr_pages)
525 pr_debug("swsusp: available swap: %lu pages\n", i.freeswap);
526 return i.freeswap > (nr_pages + PAGES_FOR_IO +
527 (nr_pages + PBES_PER_PAGE - 1) / PBES_PER_PAGE);
531 * write_suspend_image - Write entire image and metadata.
534 static int write_suspend_image(void)
538 if (!enough_swap(nr_copy_pages)) {
539 printk(KERN_ERR "swsusp: Not enough free swap\n");
544 if ((error = data_write()))
547 if ((error = write_pagedir()))
550 if ((error = close_swap()))
553 memset(key_iv, 0, MAXKEY+MAXIV);
556 free_pagedir_entries();
562 /* It is important _NOT_ to umount filesystems at this point. We want
563 * them synced (in case something goes wrong) but we DO not want to mark
564 * filesystem clean: it is not. (And it does not matter, if we resume
565 * correctly, we'll mark system clean, anyway.)
567 int swsusp_write(void)
571 if ((error = swsusp_swap_check())) {
572 printk(KERN_ERR "swsusp: cannot find swap device, try swapon -a.\n");
576 error = write_suspend_image();
577 /* This will unlock ignored swap devices since writing is finished */
584 int swsusp_suspend(void)
588 if ((error = arch_prepare_suspend()))
591 /* At this point, device_suspend() has been called, but *not*
592 * device_power_down(). We *must* device_power_down() now.
593 * Otherwise, drivers for some devices (e.g. interrupt controllers)
594 * become desynchronized with the actual state of the hardware
595 * at resume time, and evil weirdness ensues.
597 if ((error = device_power_down(PMSG_FREEZE))) {
598 printk(KERN_ERR "Some devices failed to power down, aborting suspend\n");
602 if ((error = save_highmem())) {
603 printk(KERN_ERR "swsusp: Not enough free pages for highmem\n");
604 goto Restore_highmem;
607 save_processor_state();
608 if ((error = swsusp_arch_suspend()))
609 printk(KERN_ERR "Error %d suspending\n", error);
610 /* Restore control flow magically appears here */
611 restore_processor_state();
620 int swsusp_resume(void)
624 if (device_power_down(PMSG_FREEZE))
625 printk(KERN_ERR "Some devices failed to power down, very bad\n");
626 /* We'll ignore saved state, but this gets preempt count (etc) right */
627 save_processor_state();
628 error = swsusp_arch_resume();
629 /* Code below is only ever reached in case of failure. Otherwise
630 * execution continues at place where swsusp_arch_suspend was called
633 /* The only reason why swsusp_arch_resume() can fail is memory being
634 * very tight, so we have to free it as soon as we can to avoid
635 * subsequent failures
638 restore_processor_state();
640 touch_softlockup_watchdog();
647 * mark_unsafe_pages - mark the pages that cannot be used for storing
648 * the image during resume, because they conflict with the pages that
649 * had been used before suspend
652 static void mark_unsafe_pages(struct pbe *pblist)
655 unsigned long zone_pfn;
658 if (!pblist) /* a sanity check */
661 /* Clear page flags */
662 for_each_zone (zone) {
663 for (zone_pfn = 0; zone_pfn < zone->spanned_pages; ++zone_pfn)
664 if (pfn_valid(zone_pfn + zone->zone_start_pfn))
665 ClearPageNosaveFree(pfn_to_page(zone_pfn +
666 zone->zone_start_pfn));
669 /* Mark orig addresses */
670 for_each_pbe (p, pblist)
671 SetPageNosaveFree(virt_to_page(p->orig_address));
675 static void copy_page_backup_list(struct pbe *dst, struct pbe *src)
677 /* We assume both lists contain the same number of elements */
679 dst->orig_address = src->orig_address;
680 dst->swap_address = src->swap_address;
687 * Using bio to read from swap.
688 * This code requires a bit more work than just using buffer heads
689 * but, it is the recommended way for 2.5/2.6.
690 * The following are to signal the beginning and end of I/O. Bios
691 * finish asynchronously, while we want them to happen synchronously.
692 * A simple atomic_t, and a wait loop take care of this problem.
695 static atomic_t io_done = ATOMIC_INIT(0);
697 static int end_io(struct bio *bio, unsigned int num, int err)
699 if (!test_bit(BIO_UPTODATE, &bio->bi_flags))
700 panic("I/O error reading memory image");
701 atomic_set(&io_done, 0);
705 static struct block_device *resume_bdev;
708 * submit - submit BIO request.
709 * @rw: READ or WRITE.
710 * @off physical offset of page.
711 * @page: page we're reading or writing.
713 * Straight from the textbook - allocate and initialize the bio.
714 * If we're writing, make sure the page is marked as dirty.
715 * Then submit it and wait.
718 static int submit(int rw, pgoff_t page_off, void *page)
723 bio = bio_alloc(GFP_ATOMIC, 1);
726 bio->bi_sector = page_off * (PAGE_SIZE >> 9);
728 bio->bi_bdev = resume_bdev;
729 bio->bi_end_io = end_io;
731 if (bio_add_page(bio, virt_to_page(page), PAGE_SIZE, 0) < PAGE_SIZE) {
732 printk("swsusp: ERROR: adding page to bio at %ld\n",page_off);
738 bio_set_pages_dirty(bio);
740 atomic_set(&io_done, 1);
741 submit_bio(rw | (1 << BIO_RW_SYNC), bio);
742 while (atomic_read(&io_done))
750 static int bio_read_page(pgoff_t page_off, void *page)
752 return submit(READ, page_off, page);
755 static int bio_write_page(pgoff_t page_off, void *page)
757 return submit(WRITE, page_off, page);
761 * Sanity check if this image makes sense with this kernel/swap context
762 * I really don't think that it's foolproof but more than nothing..
765 static const char *sanity_check(void)
768 if (swsusp_info.version_code != LINUX_VERSION_CODE)
769 return "kernel version";
770 if (swsusp_info.num_physpages != num_physpages)
771 return "memory size";
772 if (strcmp(swsusp_info.uts.sysname,system_utsname.sysname))
773 return "system type";
774 if (strcmp(swsusp_info.uts.release,system_utsname.release))
775 return "kernel release";
776 if (strcmp(swsusp_info.uts.version,system_utsname.version))
778 if (strcmp(swsusp_info.uts.machine,system_utsname.machine))
781 /* We can't use number of online CPUs when we use hotplug to remove them ;-))) */
782 if (swsusp_info.cpus != num_possible_cpus())
783 return "number of cpus";
789 static int check_header(void)
791 const char *reason = NULL;
794 if ((error = bio_read_page(swp_offset(swsusp_header.swsusp_info), &swsusp_info)))
797 /* Is this same machine? */
798 if ((reason = sanity_check())) {
799 printk(KERN_ERR "swsusp: Resume mismatch: %s\n",reason);
802 nr_copy_pages = swsusp_info.image_pages;
806 static int check_sig(void)
810 memset(&swsusp_header, 0, sizeof(swsusp_header));
811 if ((error = bio_read_page(0, &swsusp_header)))
813 if (!memcmp(SWSUSP_SIG, swsusp_header.sig, 10)) {
814 memcpy(swsusp_header.sig, swsusp_header.orig_sig, 10);
815 memcpy(key_iv, swsusp_header.key_iv, MAXKEY+MAXIV);
816 memset(swsusp_header.key_iv, 0, MAXKEY+MAXIV);
819 * Reset swap signature now.
821 error = bio_write_page(0, &swsusp_header);
826 pr_debug("swsusp: Signature found, resuming\n");
831 * data_read - Read image pages from swap.
833 * You do not need to check for overlaps, check_pagedir()
837 static int data_read(struct pbe *pblist)
842 int mod = swsusp_info.image_pages / 100;
845 if ((error = crypto_init(0, &tfm)))
851 printk("swsusp: Reading image data (%lu pages): ",
852 swsusp_info.image_pages);
854 for_each_pbe (p, pblist) {
856 printk("\b\b\b\b%3d%%", i / mod);
858 if ((error = crypto_read(p, tfm))) {
865 printk("\b\b\b\bdone\n");
871 * read_pagedir - Read page backup list pages from swap
874 static int read_pagedir(struct pbe *pblist)
876 struct pbe *pbpage, *p;
883 printk("swsusp: Reading pagedir (%lu pages)\n",
884 swsusp_info.pagedir_pages);
886 for_each_pb_page (pbpage, pblist) {
887 unsigned long offset = swp_offset(swsusp_info.pagedir[i++]);
891 p = (pbpage + PB_PAGE_SKIP)->next;
892 error = bio_read_page(offset, (void *)pbpage);
893 (pbpage + PB_PAGE_SKIP)->next = p;
900 BUG_ON(i != swsusp_info.pagedir_pages);
906 static int check_suspend_image(void)
910 if ((error = check_sig()))
913 if ((error = check_header()))
919 static int read_suspend_image(void)
924 if (!(p = alloc_pagedir(nr_copy_pages, GFP_ATOMIC, 0)))
927 if ((error = read_pagedir(p)))
929 create_pbe_list(p, nr_copy_pages);
930 mark_unsafe_pages(p);
931 pagedir_nosave = alloc_pagedir(nr_copy_pages, GFP_ATOMIC, 1);
932 if (pagedir_nosave) {
933 create_pbe_list(pagedir_nosave, nr_copy_pages);
934 copy_page_backup_list(pagedir_nosave, p);
940 /* Allocate memory for the image and read the data from swap */
942 error = alloc_data_pages(pagedir_nosave, GFP_ATOMIC, 1);
945 error = data_read(pagedir_nosave);
951 * swsusp_check - Check for saved image in swap
954 int swsusp_check(void)
958 resume_bdev = open_by_devnum(swsusp_resume_device, FMODE_READ);
959 if (!IS_ERR(resume_bdev)) {
960 set_blocksize(resume_bdev, PAGE_SIZE);
961 error = check_suspend_image();
963 blkdev_put(resume_bdev);
965 error = PTR_ERR(resume_bdev);
968 pr_debug("swsusp: resume file found\n");
970 pr_debug("swsusp: Error %d check for resume file\n", error);
975 * swsusp_read - Read saved image from swap.
978 int swsusp_read(void)
982 if (IS_ERR(resume_bdev)) {
983 pr_debug("swsusp: block device not initialised\n");
984 return PTR_ERR(resume_bdev);
987 error = read_suspend_image();
988 blkdev_put(resume_bdev);
989 memset(key_iv, 0, MAXKEY+MAXIV);
992 pr_debug("swsusp: Reading resume file was successful\n");
994 pr_debug("swsusp: Error %d resuming\n", error);
999 * swsusp_close - close swap device.
1002 void swsusp_close(void)
1004 if (IS_ERR(resume_bdev)) {
1005 pr_debug("swsusp: block device not initialised\n");
1009 blkdev_put(resume_bdev);