2 * linux/kernel/power/swsusp.c
4 * This file is to realize architecture-independent
5 * machine suspend feature using pretty near only high-level routines
7 * Copyright (C) 1998-2001 Gabor Kuti <seasons@fornax.hu>
8 * Copyright (C) 1998,2001-2004 Pavel Machek <pavel@suse.cz>
10 * This file is released under the GPLv2.
12 * I'd like to thank the following people for their work:
14 * Pavel Machek <pavel@ucw.cz>:
15 * Modifications, defectiveness pointing, being with me at the very beginning,
16 * suspend to swap space, stop all tasks. Port to 2.4.18-ac and 2.5.17.
18 * Steve Doddi <dirk@loth.demon.co.uk>:
19 * Support the possibility of hardware state restoring.
21 * Raph <grey.havens@earthling.net>:
22 * Support for preserving states of network devices and virtual console
23 * (including X and svgatextmode)
25 * Kurt Garloff <garloff@suse.de>:
26 * Straightened the critical function in order to prevent compilers from
27 * playing tricks with local variables.
29 * Andreas Mohr <a.mohr@mailto.de>
31 * Alex Badea <vampire@go.ro>:
34 * More state savers are welcome. Especially for the scsi layer...
36 * For TODOs,FIXMEs also look in Documentation/power/swsusp.txt
39 #include <linux/module.h>
41 #include <linux/suspend.h>
42 #include <linux/smp_lock.h>
43 #include <linux/file.h>
44 #include <linux/utsname.h>
45 #include <linux/version.h>
46 #include <linux/delay.h>
47 #include <linux/reboot.h>
48 #include <linux/bitops.h>
49 #include <linux/vt_kern.h>
50 #include <linux/kbd_kern.h>
51 #include <linux/keyboard.h>
52 #include <linux/spinlock.h>
53 #include <linux/genhd.h>
54 #include <linux/kernel.h>
55 #include <linux/major.h>
56 #include <linux/swap.h>
58 #include <linux/device.h>
59 #include <linux/buffer_head.h>
60 #include <linux/swapops.h>
61 #include <linux/bootmem.h>
62 #include <linux/syscalls.h>
63 #include <linux/console.h>
64 #include <linux/highmem.h>
65 #include <linux/bio.h>
67 #include <asm/uaccess.h>
68 #include <asm/mmu_context.h>
69 #include <asm/pgtable.h>
70 #include <asm/tlbflush.h>
75 /* References to section boundaries */
76 extern const void __nosave_begin
, __nosave_end
;
78 /* Variables to be preserved over suspend */
79 static int nr_copy_pages_check
;
81 extern char resume_file
[];
83 /* Local variables that should not be affected by save */
84 unsigned int nr_copy_pages __nosavedata
= 0;
86 /* Suspend pagedir is allocated before final copy, therefore it
87 must be freed after resume
89 Warning: this is evil. There are actually two pagedirs at time of
90 resume. One is "pagedir_save", which is empty frame allocated at
91 time of suspend, that must be freed. Second is "pagedir_nosave",
92 allocated at time of resume, that travels through memory not to
93 collide with anything.
95 Warning: this is even more evil than it seems. Pagedirs this file
96 talks about are completely different from page directories used by
99 suspend_pagedir_t
*pagedir_nosave __nosavedata
= NULL
;
100 static suspend_pagedir_t
*pagedir_save
;
102 #define SWSUSP_SIG "S1SUSPEND"
104 static struct swsusp_header
{
105 char reserved
[PAGE_SIZE
- 20 - sizeof(swp_entry_t
)];
106 swp_entry_t swsusp_info
;
109 } __attribute__((packed
, aligned(PAGE_SIZE
))) swsusp_header
;
111 static struct swsusp_info swsusp_info
;
114 * XXX: We try to keep some more pages free so that I/O operations succeed
115 * without paging. Might this be more?
117 #define PAGES_FOR_IO 512
123 /* We memorize in swapfile_used what swap devices are used for suspension */
124 #define SWAPFILE_UNUSED 0
125 #define SWAPFILE_SUSPEND 1 /* This is the suspending device */
126 #define SWAPFILE_IGNORED 2 /* Those are other swap devices ignored for suspension */
128 static unsigned short swapfile_used
[MAX_SWAPFILES
];
129 static unsigned short root_swap
;
131 static int mark_swapfiles(swp_entry_t prev
)
135 rw_swap_page_sync(READ
,
136 swp_entry(root_swap
, 0),
137 virt_to_page((unsigned long)&swsusp_header
));
138 if (!memcmp("SWAP-SPACE",swsusp_header
.sig
, 10) ||
139 !memcmp("SWAPSPACE2",swsusp_header
.sig
, 10)) {
140 memcpy(swsusp_header
.orig_sig
,swsusp_header
.sig
, 10);
141 memcpy(swsusp_header
.sig
,SWSUSP_SIG
, 10);
142 swsusp_header
.swsusp_info
= prev
;
143 error
= rw_swap_page_sync(WRITE
,
144 swp_entry(root_swap
, 0),
145 virt_to_page((unsigned long)
148 pr_debug("swsusp: Partition is not swap space.\n");
155 * Check whether the swap device is the specified resume
156 * device, irrespective of whether they are specified by
159 * (Thus, device inode aliasing is allowed. You can say /dev/hda4
160 * instead of /dev/ide/host0/bus0/target0/lun0/part4 [if using devfs]
161 * and they'll be considered the same device. This is *necessary* for
162 * devfs, since the resume code can only recognize the form /dev/hda4,
163 * but the suspend code would see the long name.)
165 static int is_resume_device(const struct swap_info_struct
*swap_info
)
167 struct file
*file
= swap_info
->swap_file
;
168 struct inode
*inode
= file
->f_dentry
->d_inode
;
170 return S_ISBLK(inode
->i_mode
) &&
171 swsusp_resume_device
== MKDEV(imajor(inode
), iminor(inode
));
174 static int swsusp_swap_check(void) /* This is called before saving image */
178 len
=strlen(resume_file
);
182 for(i
=0; i
<MAX_SWAPFILES
; i
++) {
183 if (swap_info
[i
].flags
== 0) {
184 swapfile_used
[i
]=SWAPFILE_UNUSED
;
187 printk(KERN_WARNING
"resume= option should be used to set suspend device" );
188 if(root_swap
== 0xFFFF) {
189 swapfile_used
[i
] = SWAPFILE_SUSPEND
;
192 swapfile_used
[i
] = SWAPFILE_IGNORED
;
194 /* we ignore all swap devices that are not the resume_file */
195 if (is_resume_device(&swap_info
[i
])) {
196 swapfile_used
[i
] = SWAPFILE_SUSPEND
;
199 swapfile_used
[i
] = SWAPFILE_IGNORED
;
205 return (root_swap
!= 0xffff) ? 0 : -ENODEV
;
209 * This is called after saving image so modification
210 * will be lost after resume... and that's what we want.
211 * we make the device unusable. A new call to
212 * lock_swapdevices can unlock the devices.
214 static void lock_swapdevices(void)
219 for(i
= 0; i
< MAX_SWAPFILES
; i
++)
220 if(swapfile_used
[i
] == SWAPFILE_IGNORED
) {
221 swap_info
[i
].flags
^= 0xFF;
227 * write_swap_page - Write one page to a fresh swap location.
228 * @addr: Address we're writing.
229 * @loc: Place to store the entry we used.
231 * Allocate a new swap entry and 'sync' it. Note we discard -EIO
232 * errors. That is an artifact left over from swsusp. It did not
233 * check the return of rw_swap_page_sync() at all, since most pages
234 * written back to swap would return -EIO.
235 * This is a partial improvement, since we will at least return other
236 * errors, though we need to eventually fix the damn code.
238 static int write_page(unsigned long addr
, swp_entry_t
* loc
)
243 entry
= get_swap_page();
244 if (swp_offset(entry
) &&
245 swapfile_used
[swp_type(entry
)] == SWAPFILE_SUSPEND
) {
246 error
= rw_swap_page_sync(WRITE
, entry
,
258 * data_free - Free the swap entries used by the saved image.
260 * Walk the list of used swap entries and free each one.
261 * This is only used for cleanup when suspend fails.
263 static void data_free(void)
268 for (i
= 0; i
< nr_copy_pages
; i
++) {
269 entry
= (pagedir_nosave
+ i
)->swap_address
;
274 (pagedir_nosave
+ i
)->swap_address
= (swp_entry_t
){0};
279 * data_write - Write saved image to swap.
281 * Walk the list of pages in the image and sync each one to swap.
283 static int data_write(void)
285 int error
= 0, i
= 0;
286 unsigned int mod
= nr_copy_pages
/ 100;
292 printk( "Writing data to swap (%d pages)... ", nr_copy_pages
);
293 for_each_pbe(p
, pagedir_nosave
) {
295 printk( "\b\b\b\b%3d%%", i
/ mod
);
296 if ((error
= write_page(p
->address
, &(p
->swap_address
))))
300 printk("\b\b\b\bdone\n");
304 static void dump_info(void)
306 pr_debug(" swsusp: Version: %u\n",swsusp_info
.version_code
);
307 pr_debug(" swsusp: Num Pages: %ld\n",swsusp_info
.num_physpages
);
308 pr_debug(" swsusp: UTS Sys: %s\n",swsusp_info
.uts
.sysname
);
309 pr_debug(" swsusp: UTS Node: %s\n",swsusp_info
.uts
.nodename
);
310 pr_debug(" swsusp: UTS Release: %s\n",swsusp_info
.uts
.release
);
311 pr_debug(" swsusp: UTS Version: %s\n",swsusp_info
.uts
.version
);
312 pr_debug(" swsusp: UTS Machine: %s\n",swsusp_info
.uts
.machine
);
313 pr_debug(" swsusp: UTS Domain: %s\n",swsusp_info
.uts
.domainname
);
314 pr_debug(" swsusp: CPUs: %d\n",swsusp_info
.cpus
);
315 pr_debug(" swsusp: Image: %ld Pages\n",swsusp_info
.image_pages
);
316 pr_debug(" swsusp: Pagedir: %ld Pages\n",swsusp_info
.pagedir_pages
);
319 static void init_header(void)
321 memset(&swsusp_info
, 0, sizeof(swsusp_info
));
322 swsusp_info
.version_code
= LINUX_VERSION_CODE
;
323 swsusp_info
.num_physpages
= num_physpages
;
324 memcpy(&swsusp_info
.uts
, &system_utsname
, sizeof(system_utsname
));
326 swsusp_info
.suspend_pagedir
= pagedir_nosave
;
327 swsusp_info
.cpus
= num_online_cpus();
328 swsusp_info
.image_pages
= nr_copy_pages
;
331 static int close_swap(void)
337 error
= write_page((unsigned long)&swsusp_info
, &entry
);
340 error
= mark_swapfiles(entry
);
347 * free_pagedir_entries - Free pages used by the page directory.
349 * This is used during suspend for error recovery.
352 static void free_pagedir_entries(void)
356 for (i
= 0; i
< swsusp_info
.pagedir_pages
; i
++)
357 swap_free(swsusp_info
.pagedir
[i
]);
362 * write_pagedir - Write the array of pages holding the page directory.
363 * @last: Last swap entry we write (needed for header).
366 static int write_pagedir(void)
372 printk( "Writing pagedir...");
373 for_each_pb_page(pbe
, pagedir_nosave
) {
374 if ((error
= write_page((unsigned long)pbe
, &swsusp_info
.pagedir
[n
++])))
378 swsusp_info
.pagedir_pages
= n
;
379 printk("done (%u pages)\n", n
);
384 * write_suspend_image - Write entire image and metadata.
388 static int write_suspend_image(void)
393 if ((error
= data_write()))
396 if ((error
= write_pagedir()))
399 if ((error
= close_swap()))
404 free_pagedir_entries();
411 #ifdef CONFIG_HIGHMEM
412 struct highmem_page
{
415 struct highmem_page
*next
;
418 static struct highmem_page
*highmem_copy
;
420 static int save_highmem_zone(struct zone
*zone
)
422 unsigned long zone_pfn
;
423 mark_free_pages(zone
);
424 for (zone_pfn
= 0; zone_pfn
< zone
->spanned_pages
; ++zone_pfn
) {
426 struct highmem_page
*save
;
428 unsigned long pfn
= zone_pfn
+ zone
->zone_start_pfn
;
434 page
= pfn_to_page(pfn
);
436 * This condition results from rvmalloc() sans vmalloc_32()
437 * and architectural memory reservations. This should be
438 * corrected eventually when the cases giving rise to this
439 * are better understood.
441 if (PageReserved(page
)) {
442 printk("highmem reserved page?!\n");
445 BUG_ON(PageNosave(page
));
446 if (PageNosaveFree(page
))
448 save
= kmalloc(sizeof(struct highmem_page
), GFP_ATOMIC
);
451 save
->next
= highmem_copy
;
453 save
->data
= (void *) get_zeroed_page(GFP_ATOMIC
);
458 kaddr
= kmap_atomic(page
, KM_USER0
);
459 memcpy(save
->data
, kaddr
, PAGE_SIZE
);
460 kunmap_atomic(kaddr
, KM_USER0
);
465 #endif /* CONFIG_HIGHMEM */
468 static int save_highmem(void)
470 #ifdef CONFIG_HIGHMEM
474 pr_debug("swsusp: Saving Highmem\n");
475 for_each_zone(zone
) {
476 if (is_highmem(zone
))
477 res
= save_highmem_zone(zone
);
485 static int restore_highmem(void)
487 #ifdef CONFIG_HIGHMEM
488 printk("swsusp: Restoring Highmem\n");
489 while (highmem_copy
) {
490 struct highmem_page
*save
= highmem_copy
;
492 highmem_copy
= save
->next
;
494 kaddr
= kmap_atomic(save
->page
, KM_USER0
);
495 memcpy(kaddr
, save
->data
, PAGE_SIZE
);
496 kunmap_atomic(kaddr
, KM_USER0
);
497 free_page((long) save
->data
);
505 static int pfn_is_nosave(unsigned long pfn
)
507 unsigned long nosave_begin_pfn
= __pa(&__nosave_begin
) >> PAGE_SHIFT
;
508 unsigned long nosave_end_pfn
= PAGE_ALIGN(__pa(&__nosave_end
)) >> PAGE_SHIFT
;
509 return (pfn
>= nosave_begin_pfn
) && (pfn
< nosave_end_pfn
);
513 * saveable - Determine whether a page should be cloned or not.
516 * We save a page if it's Reserved, and not in the range of pages
517 * statically defined as 'unsaveable', or if it isn't reserved, and
518 * isn't part of a free chunk of pages.
521 static int saveable(struct zone
* zone
, unsigned long * zone_pfn
)
523 unsigned long pfn
= *zone_pfn
+ zone
->zone_start_pfn
;
529 page
= pfn_to_page(pfn
);
530 BUG_ON(PageReserved(page
) && PageNosave(page
));
531 if (PageNosave(page
))
533 if (PageReserved(page
) && pfn_is_nosave(pfn
)) {
534 pr_debug("[nosave pfn 0x%lx]", pfn
);
537 if (PageNosaveFree(page
))
543 static void count_data_pages(void)
546 unsigned long zone_pfn
;
550 for_each_zone(zone
) {
551 if (is_highmem(zone
))
553 mark_free_pages(zone
);
554 for (zone_pfn
= 0; zone_pfn
< zone
->spanned_pages
; ++zone_pfn
)
555 nr_copy_pages
+= saveable(zone
, &zone_pfn
);
560 static void copy_data_pages(void)
563 unsigned long zone_pfn
;
564 struct pbe
* pbe
= pagedir_nosave
;
566 pr_debug("copy_data_pages(): pages to copy: %d\n", nr_copy_pages
);
567 for_each_zone(zone
) {
568 if (is_highmem(zone
))
570 mark_free_pages(zone
);
571 for (zone_pfn
= 0; zone_pfn
< zone
->spanned_pages
; ++zone_pfn
) {
572 if (saveable(zone
, &zone_pfn
)) {
574 page
= pfn_to_page(zone_pfn
+ zone
->zone_start_pfn
);
576 pbe
->orig_address
= (long) page_address(page
);
577 /* copy_page is not usable for copying task structs. */
578 memcpy((void *)pbe
->address
, (void *)pbe
->orig_address
, PAGE_SIZE
);
588 * calc_nr - Determine the number of pages needed for a pbe list.
591 static int calc_nr(int nr_copy
)
594 int mod
= !!(nr_copy
% PBES_PER_PAGE
);
595 int diff
= (nr_copy
/ PBES_PER_PAGE
) + mod
;
600 mod
= !!(nr_copy
% PBES_PER_PAGE
);
601 diff
= (nr_copy
/ PBES_PER_PAGE
) + mod
- extra
;
608 * free_pagedir - free pages allocated with alloc_pagedir()
611 static inline void free_pagedir(struct pbe
*pblist
)
616 pbe
= (pblist
+ PB_PAGE_SKIP
)->next
;
617 free_page((unsigned long)pblist
);
623 * fill_pb_page - Create a list of PBEs on a given memory page
626 static inline void fill_pb_page(struct pbe
*pbpage
)
631 pbpage
+= PB_PAGE_SKIP
;
634 while (++p
< pbpage
);
638 * create_pbe_list - Create a list of PBEs on top of a given chain
639 * of memory pages allocated with alloc_pagedir()
642 static void create_pbe_list(struct pbe
*pblist
, unsigned nr_pages
)
644 struct pbe
*pbpage
, *p
;
645 unsigned num
= PBES_PER_PAGE
;
647 for_each_pb_page (pbpage
, pblist
) {
651 fill_pb_page(pbpage
);
652 num
+= PBES_PER_PAGE
;
655 for (num
-= PBES_PER_PAGE
- 1, p
= pbpage
; num
< nr_pages
; p
++, num
++)
659 pr_debug("create_pbe_list(): initialized %d PBEs\n", num
);
663 * alloc_pagedir - Allocate the page directory.
665 * First, determine exactly how many pages we need and
668 * We arrange the pages in a chain: each page is an array of PBES_PER_PAGE
669 * struct pbe elements (pbes) and the last element in the page points
672 * On each page we set up a list of struct_pbe elements.
675 static struct pbe
* alloc_pagedir(unsigned nr_pages
)
678 struct pbe
*pblist
, *pbe
;
683 pr_debug("alloc_pagedir(): nr_pages = %d\n", nr_pages
);
684 pblist
= (struct pbe
*)get_zeroed_page(GFP_ATOMIC
| __GFP_COLD
);
685 for (pbe
= pblist
, num
= PBES_PER_PAGE
; pbe
&& num
< nr_pages
;
686 pbe
= pbe
->next
, num
+= PBES_PER_PAGE
) {
688 pbe
->next
= (struct pbe
*)get_zeroed_page(GFP_ATOMIC
| __GFP_COLD
);
690 if (!pbe
) { /* get_zeroed_page() failed */
691 free_pagedir(pblist
);
698 * free_image_pages - Free pages allocated for snapshot
701 static void free_image_pages(void)
705 for_each_pbe(p
, pagedir_save
) {
707 ClearPageNosave(virt_to_page(p
->address
));
708 free_page(p
->address
);
715 * alloc_image_pages - Allocate pages for the snapshot.
718 static int alloc_image_pages(void)
722 for_each_pbe(p
, pagedir_save
) {
723 p
->address
= get_zeroed_page(GFP_ATOMIC
| __GFP_COLD
);
726 SetPageNosave(virt_to_page(p
->address
));
731 void swsusp_free(void)
733 BUG_ON(PageNosave(virt_to_page(pagedir_save
)));
734 BUG_ON(PageNosaveFree(virt_to_page(pagedir_save
)));
736 free_pagedir(pagedir_save
);
741 * enough_free_mem - Make sure we enough free memory to snapshot.
743 * Returns TRUE or FALSE after checking the number of available
747 static int enough_free_mem(void)
749 if (nr_free_pages() < (nr_copy_pages
+ PAGES_FOR_IO
)) {
750 pr_debug("swsusp: Not enough free pages: Have %d\n",
759 * enough_swap - Make sure we have enough swap to save the image.
761 * Returns TRUE or FALSE after checking the total amount of swap
764 * FIXME: si_swapinfo(&i) returns all swap devices information.
765 * We should only consider resume_device.
768 static int enough_swap(void)
773 if (i
.freeswap
< (nr_copy_pages
+ PAGES_FOR_IO
)) {
774 pr_debug("swsusp: Not enough swap. Need %ld\n",i
.freeswap
);
780 static int swsusp_alloc(void)
784 pr_debug("suspend: (pages needed: %d + %d free: %d)\n",
785 nr_copy_pages
, PAGES_FOR_IO
, nr_free_pages());
787 pagedir_nosave
= NULL
;
788 if (!enough_free_mem())
794 nr_copy_pages
= calc_nr(nr_copy_pages
);
796 if (!(pagedir_save
= alloc_pagedir(nr_copy_pages
))) {
797 printk(KERN_ERR
"suspend: Allocating pagedir failed.\n");
800 create_pbe_list(pagedir_save
, nr_copy_pages
);
801 pagedir_nosave
= pagedir_save
;
802 if ((error
= alloc_image_pages())) {
803 printk(KERN_ERR
"suspend: Allocating image pages failed.\n");
808 nr_copy_pages_check
= nr_copy_pages
;
812 static int suspend_prepare_image(void)
816 pr_debug("swsusp: critical section: \n");
817 if (save_highmem()) {
818 printk(KERN_CRIT
"Suspend machine: Not enough free pages for highmem\n");
825 printk("swsusp: Need to copy %u pages\n", nr_copy_pages
);
827 error
= swsusp_alloc();
831 /* During allocating of suspend pagedir, new cold pages may appear.
838 * End of critical section. From now on, we can write to memory,
839 * but we should not touch disk. This specially means we must _not_
840 * touch swap space! Except we must write out our image of course.
843 printk("swsusp: critical section/: done (%d pages copied)\n", nr_copy_pages
);
848 /* It is important _NOT_ to umount filesystems at this point. We want
849 * them synced (in case something goes wrong) but we DO not want to mark
850 * filesystem clean: it is not. (And it does not matter, if we resume
851 * correctly, we'll mark system clean, anyway.)
853 int swsusp_write(void)
858 error
= write_suspend_image();
859 /* This will unlock ignored swap devices since writing is finished */
866 extern asmlinkage
int swsusp_arch_suspend(void);
867 extern asmlinkage
int swsusp_arch_resume(void);
870 asmlinkage
int swsusp_save(void)
874 if ((error
= swsusp_swap_check())) {
875 printk(KERN_ERR
"swsusp: FATAL: cannot find swap device, try "
879 return suspend_prepare_image();
882 int swsusp_suspend(void)
885 if ((error
= arch_prepare_suspend()))
888 /* At this point, device_suspend() has been called, but *not*
889 * device_power_down(). We *must* device_power_down() now.
890 * Otherwise, drivers for some devices (e.g. interrupt controllers)
891 * become desynchronized with the actual state of the hardware
892 * at resume time, and evil weirdness ensues.
894 if ((error
= device_power_down(PMSG_FREEZE
))) {
895 printk(KERN_ERR
"Some devices failed to power down, aborting suspend\n");
900 save_processor_state();
901 if ((error
= swsusp_arch_suspend()))
903 /* Restore control flow magically appears here */
904 restore_processor_state();
905 BUG_ON (nr_copy_pages_check
!= nr_copy_pages
);
912 int swsusp_resume(void)
916 if (device_power_down(PMSG_FREEZE
))
917 printk(KERN_ERR
"Some devices failed to power down, very bad\n");
918 /* We'll ignore saved state, but this gets preempt count (etc) right */
919 save_processor_state();
920 error
= swsusp_arch_resume();
921 /* Code below is only ever reached in case of failure. Otherwise
922 * execution continues at place where swsusp_arch_suspend was called
925 restore_processor_state();
932 /* More restore stuff */
935 * Returns true if given address/order collides with any orig_address
937 static int does_collide_order(unsigned long addr
, int order
)
941 for (i
=0; i
< (1<<order
); i
++)
942 if (!PageNosaveFree(virt_to_page(addr
+ i
* PAGE_SIZE
)))
948 * On resume, for storing the PBE list and the image,
949 * we can only use memory pages that do not conflict with the pages
950 * which had been used before suspend.
952 * We don't know which pages are usable until we allocate them.
954 * Allocated but unusable (ie eaten) memory pages are linked together
955 * to create a list, so that we can free them easily
957 * We could have used a type other than (void *)
958 * for this purpose, but ...
960 static void **eaten_memory
= NULL
;
962 static inline void eat_page(void *page
)
971 static unsigned long get_usable_page(unsigned gfp_mask
)
975 m
= get_zeroed_page(gfp_mask
);
976 while (does_collide_order(m
, 0)) {
978 m
= get_zeroed_page(gfp_mask
);
985 static void free_eaten_memory(void)
993 m
= (unsigned long)c
;
999 pr_debug("swsusp: %d unused pages freed\n", i
);
1003 * check_pagedir - We ensure here that pages that the PBEs point to
1004 * won't collide with pages where we're going to restore from the loaded
1008 static int check_pagedir(struct pbe
*pblist
)
1012 /* This is necessary, so that we can free allocated pages
1013 * in case of failure
1015 for_each_pbe (p
, pblist
)
1018 for_each_pbe (p
, pblist
) {
1019 p
->address
= get_usable_page(GFP_ATOMIC
);
1027 * swsusp_pagedir_relocate - It is possible, that some memory pages
1028 * occupied by the list of PBEs collide with pages where we're going to
1029 * restore from the loaded pages later. We relocate them here.
1032 static struct pbe
* swsusp_pagedir_relocate(struct pbe
*pblist
)
1035 unsigned long zone_pfn
;
1036 struct pbe
*pbpage
, *tail
, *p
;
1038 int rel
= 0, error
= 0;
1040 if (!pblist
) /* a sanity check */
1043 pr_debug("swsusp: Relocating pagedir (%lu pages to check)\n",
1044 swsusp_info
.pagedir_pages
);
1046 /* Set page flags */
1048 for_each_zone(zone
) {
1049 for (zone_pfn
= 0; zone_pfn
< zone
->spanned_pages
; ++zone_pfn
)
1050 SetPageNosaveFree(pfn_to_page(zone_pfn
+
1051 zone
->zone_start_pfn
));
1054 /* Clear orig addresses */
1056 for_each_pbe (p
, pblist
)
1057 ClearPageNosaveFree(virt_to_page(p
->orig_address
));
1059 tail
= pblist
+ PB_PAGE_SKIP
;
1061 /* Relocate colliding pages */
1063 for_each_pb_page (pbpage
, pblist
) {
1064 if (does_collide_order((unsigned long)pbpage
, 0)) {
1065 m
= (void *)get_usable_page(GFP_ATOMIC
| __GFP_COLD
);
1070 memcpy(m
, (void *)pbpage
, PAGE_SIZE
);
1071 if (pbpage
== pblist
)
1072 pblist
= (struct pbe
*)m
;
1074 tail
->next
= (struct pbe
*)m
;
1076 eat_page((void *)pbpage
);
1077 pbpage
= (struct pbe
*)m
;
1079 /* We have to link the PBEs again */
1081 for (p
= pbpage
; p
< pbpage
+ PB_PAGE_SKIP
; p
++)
1082 if (p
->next
) /* needed to save the end */
1087 tail
= pbpage
+ PB_PAGE_SKIP
;
1091 printk("\nswsusp: Out of memory\n\n");
1092 free_pagedir(pblist
);
1093 free_eaten_memory();
1097 printk("swsusp: Relocated %d pages\n", rel
);
1103 * Using bio to read from swap.
1104 * This code requires a bit more work than just using buffer heads
1105 * but, it is the recommended way for 2.5/2.6.
1106 * The following are to signal the beginning and end of I/O. Bios
1107 * finish asynchronously, while we want them to happen synchronously.
1108 * A simple atomic_t, and a wait loop take care of this problem.
1111 static atomic_t io_done
= ATOMIC_INIT(0);
1113 static int end_io(struct bio
* bio
, unsigned int num
, int err
)
1115 if (!test_bit(BIO_UPTODATE
, &bio
->bi_flags
))
1116 panic("I/O error reading memory image");
1117 atomic_set(&io_done
, 0);
1121 static struct block_device
* resume_bdev
;
1124 * submit - submit BIO request.
1125 * @rw: READ or WRITE.
1126 * @off physical offset of page.
1127 * @page: page we're reading or writing.
1129 * Straight from the textbook - allocate and initialize the bio.
1130 * If we're writing, make sure the page is marked as dirty.
1131 * Then submit it and wait.
1134 static int submit(int rw
, pgoff_t page_off
, void * page
)
1139 bio
= bio_alloc(GFP_ATOMIC
, 1);
1142 bio
->bi_sector
= page_off
* (PAGE_SIZE
>> 9);
1144 bio
->bi_bdev
= resume_bdev
;
1145 bio
->bi_end_io
= end_io
;
1147 if (bio_add_page(bio
, virt_to_page(page
), PAGE_SIZE
, 0) < PAGE_SIZE
) {
1148 printk("swsusp: ERROR: adding page to bio at %ld\n",page_off
);
1154 bio_set_pages_dirty(bio
);
1156 atomic_set(&io_done
, 1);
1157 submit_bio(rw
| (1 << BIO_RW_SYNC
), bio
);
1158 while (atomic_read(&io_done
))
1166 static int bio_read_page(pgoff_t page_off
, void * page
)
1168 return submit(READ
, page_off
, page
);
1171 static int bio_write_page(pgoff_t page_off
, void * page
)
1173 return submit(WRITE
, page_off
, page
);
1177 * Sanity check if this image makes sense with this kernel/swap context
1178 * I really don't think that it's foolproof but more than nothing..
1181 static const char * sanity_check(void)
1184 if(swsusp_info
.version_code
!= LINUX_VERSION_CODE
)
1185 return "kernel version";
1186 if(swsusp_info
.num_physpages
!= num_physpages
)
1187 return "memory size";
1188 if (strcmp(swsusp_info
.uts
.sysname
,system_utsname
.sysname
))
1189 return "system type";
1190 if (strcmp(swsusp_info
.uts
.release
,system_utsname
.release
))
1191 return "kernel release";
1192 if (strcmp(swsusp_info
.uts
.version
,system_utsname
.version
))
1194 if (strcmp(swsusp_info
.uts
.machine
,system_utsname
.machine
))
1196 if(swsusp_info
.cpus
!= num_online_cpus())
1197 return "number of cpus";
1202 static int check_header(void)
1204 const char * reason
= NULL
;
1207 if ((error
= bio_read_page(swp_offset(swsusp_header
.swsusp_info
), &swsusp_info
)))
1210 /* Is this same machine? */
1211 if ((reason
= sanity_check())) {
1212 printk(KERN_ERR
"swsusp: Resume mismatch: %s\n",reason
);
1215 nr_copy_pages
= swsusp_info
.image_pages
;
1219 static int check_sig(void)
1223 memset(&swsusp_header
, 0, sizeof(swsusp_header
));
1224 if ((error
= bio_read_page(0, &swsusp_header
)))
1226 if (!memcmp(SWSUSP_SIG
, swsusp_header
.sig
, 10)) {
1227 memcpy(swsusp_header
.sig
, swsusp_header
.orig_sig
, 10);
1230 * Reset swap signature now.
1232 error
= bio_write_page(0, &swsusp_header
);
1234 printk(KERN_ERR
"swsusp: Suspend partition has wrong signature?\n");
1238 pr_debug("swsusp: Signature found, resuming\n");
1243 * data_read - Read image pages from swap.
1245 * You do not need to check for overlaps, check_pagedir()
1249 static int data_read(struct pbe
*pblist
)
1254 int mod
= swsusp_info
.image_pages
/ 100;
1259 printk("swsusp: Reading image data (%lu pages): ",
1260 swsusp_info
.image_pages
);
1262 for_each_pbe (p
, pblist
) {
1264 printk("\b\b\b\b%3d%%", i
/ mod
);
1266 error
= bio_read_page(swp_offset(p
->swap_address
),
1267 (void *)p
->address
);
1273 printk("\b\b\b\bdone\n");
1277 extern dev_t
name_to_dev_t(const char *line
);
1280 * read_pagedir - Read page backup list pages from swap
1283 static int read_pagedir(struct pbe
*pblist
)
1285 struct pbe
*pbpage
, *p
;
1292 printk("swsusp: Reading pagedir (%lu pages)\n",
1293 swsusp_info
.pagedir_pages
);
1295 for_each_pb_page (pbpage
, pblist
) {
1296 unsigned long offset
= swp_offset(swsusp_info
.pagedir
[i
++]);
1300 p
= (pbpage
+ PB_PAGE_SKIP
)->next
;
1301 error
= bio_read_page(offset
, (void *)pbpage
);
1302 (pbpage
+ PB_PAGE_SKIP
)->next
= p
;
1309 free_page((unsigned long)pblist
);
1311 BUG_ON(i
!= swsusp_info
.pagedir_pages
);
1317 static int check_suspend_image(void)
1321 if ((error
= check_sig()))
1324 if ((error
= check_header()))
1330 static int read_suspend_image(void)
1335 if (!(p
= alloc_pagedir(nr_copy_pages
)))
1338 if ((error
= read_pagedir(p
)))
1341 create_pbe_list(p
, nr_copy_pages
);
1343 if (!(pagedir_nosave
= swsusp_pagedir_relocate(p
)))
1346 /* Allocate memory for the image and read the data from swap */
1348 error
= check_pagedir(pagedir_nosave
);
1349 free_eaten_memory();
1351 error
= data_read(pagedir_nosave
);
1353 if (error
) { /* We fail cleanly */
1354 for_each_pbe (p
, pagedir_nosave
)
1356 free_page(p
->address
);
1359 free_pagedir(pagedir_nosave
);
1365 * swsusp_check - Check for saved image in swap
1368 int swsusp_check(void)
1372 if (!swsusp_resume_device
) {
1373 if (!strlen(resume_file
))
1375 swsusp_resume_device
= name_to_dev_t(resume_file
);
1376 pr_debug("swsusp: Resume From Partition %s\n", resume_file
);
1378 pr_debug("swsusp: Resume From Partition %d:%d\n",
1379 MAJOR(swsusp_resume_device
), MINOR(swsusp_resume_device
));
1382 resume_bdev
= open_by_devnum(swsusp_resume_device
, FMODE_READ
);
1383 if (!IS_ERR(resume_bdev
)) {
1384 set_blocksize(resume_bdev
, PAGE_SIZE
);
1385 error
= check_suspend_image();
1387 blkdev_put(resume_bdev
);
1389 error
= PTR_ERR(resume_bdev
);
1392 pr_debug("swsusp: resume file found\n");
1394 pr_debug("swsusp: Error %d check for resume file\n", error
);
1399 * swsusp_read - Read saved image from swap.
1402 int swsusp_read(void)
1406 if (IS_ERR(resume_bdev
)) {
1407 pr_debug("swsusp: block device not initialised\n");
1408 return PTR_ERR(resume_bdev
);
1411 error
= read_suspend_image();
1412 blkdev_put(resume_bdev
);
1415 pr_debug("swsusp: Reading resume file was successful\n");
1417 pr_debug("swsusp: Error %d resuming\n", error
);
1422 * swsusp_close - close swap device.
1425 void swsusp_close(void)
1427 if (IS_ERR(resume_bdev
)) {
1428 pr_debug("swsusp: block device not initialised\n");
1432 blkdev_put(resume_bdev
);