2 * linux/kernel/power/snapshot.c
4 * This file provide system snapshot/restore functionality.
6 * Copyright (C) 1998-2005 Pavel Machek <pavel@suse.cz>
8 * This file is released under the GPLv2, and is based on swsusp.c.
13 #include <linux/version.h>
14 #include <linux/module.h>
16 #include <linux/suspend.h>
17 #include <linux/smp_lock.h>
18 #include <linux/delay.h>
19 #include <linux/bitops.h>
20 #include <linux/spinlock.h>
21 #include <linux/kernel.h>
23 #include <linux/device.h>
24 #include <linux/bootmem.h>
25 #include <linux/syscalls.h>
26 #include <linux/console.h>
27 #include <linux/highmem.h>
29 #include <asm/uaccess.h>
30 #include <asm/mmu_context.h>
31 #include <asm/pgtable.h>
32 #include <asm/tlbflush.h>
37 struct pbe
*pagedir_nosave
;
38 static unsigned int nr_copy_pages
;
39 static unsigned int nr_meta_pages
;
40 static unsigned long *buffer
;
43 unsigned int count_highmem_pages(void)
46 unsigned long zone_pfn
;
50 if (is_highmem(zone
)) {
51 mark_free_pages(zone
);
52 for (zone_pfn
= 0; zone_pfn
< zone
->spanned_pages
; zone_pfn
++) {
54 unsigned long pfn
= zone_pfn
+ zone
->zone_start_pfn
;
57 page
= pfn_to_page(pfn
);
58 if (PageReserved(page
))
60 if (PageNosaveFree(page
))
71 struct highmem_page
*next
;
74 static struct highmem_page
*highmem_copy
;
76 static int save_highmem_zone(struct zone
*zone
)
78 unsigned long zone_pfn
;
79 mark_free_pages(zone
);
80 for (zone_pfn
= 0; zone_pfn
< zone
->spanned_pages
; ++zone_pfn
) {
82 struct highmem_page
*save
;
84 unsigned long pfn
= zone_pfn
+ zone
->zone_start_pfn
;
90 page
= pfn_to_page(pfn
);
92 * This condition results from rvmalloc() sans vmalloc_32()
93 * and architectural memory reservations. This should be
94 * corrected eventually when the cases giving rise to this
95 * are better understood.
97 if (PageReserved(page
))
99 BUG_ON(PageNosave(page
));
100 if (PageNosaveFree(page
))
102 save
= kmalloc(sizeof(struct highmem_page
), GFP_ATOMIC
);
105 save
->next
= highmem_copy
;
107 save
->data
= (void *) get_zeroed_page(GFP_ATOMIC
);
112 kaddr
= kmap_atomic(page
, KM_USER0
);
113 memcpy(save
->data
, kaddr
, PAGE_SIZE
);
114 kunmap_atomic(kaddr
, KM_USER0
);
120 int save_highmem(void)
125 pr_debug("swsusp: Saving Highmem");
126 for_each_zone (zone
) {
127 if (is_highmem(zone
))
128 res
= save_highmem_zone(zone
);
136 int restore_highmem(void)
138 printk("swsusp: Restoring Highmem\n");
139 while (highmem_copy
) {
140 struct highmem_page
*save
= highmem_copy
;
142 highmem_copy
= save
->next
;
144 kaddr
= kmap_atomic(save
->page
, KM_USER0
);
145 memcpy(kaddr
, save
->data
, PAGE_SIZE
);
146 kunmap_atomic(kaddr
, KM_USER0
);
147 free_page((long) save
->data
);
154 static int pfn_is_nosave(unsigned long pfn
)
156 unsigned long nosave_begin_pfn
= __pa(&__nosave_begin
) >> PAGE_SHIFT
;
157 unsigned long nosave_end_pfn
= PAGE_ALIGN(__pa(&__nosave_end
)) >> PAGE_SHIFT
;
158 return (pfn
>= nosave_begin_pfn
) && (pfn
< nosave_end_pfn
);
162 * saveable - Determine whether a page should be cloned or not.
165 * We save a page if it's Reserved, and not in the range of pages
166 * statically defined as 'unsaveable', or if it isn't reserved, and
167 * isn't part of a free chunk of pages.
170 static int saveable(struct zone
*zone
, unsigned long *zone_pfn
)
172 unsigned long pfn
= *zone_pfn
+ zone
->zone_start_pfn
;
178 page
= pfn_to_page(pfn
);
179 BUG_ON(PageReserved(page
) && PageNosave(page
));
180 if (PageNosave(page
))
182 if (PageReserved(page
) && pfn_is_nosave(pfn
))
184 if (PageNosaveFree(page
))
190 unsigned int count_data_pages(void)
193 unsigned long zone_pfn
;
196 for_each_zone (zone
) {
197 if (is_highmem(zone
))
199 mark_free_pages(zone
);
200 for (zone_pfn
= 0; zone_pfn
< zone
->spanned_pages
; ++zone_pfn
)
201 n
+= saveable(zone
, &zone_pfn
);
206 static void copy_data_pages(struct pbe
*pblist
)
209 unsigned long zone_pfn
;
213 for_each_zone (zone
) {
214 if (is_highmem(zone
))
216 mark_free_pages(zone
);
217 /* This is necessary for swsusp_free() */
218 for_each_pb_page (p
, pblist
)
219 SetPageNosaveFree(virt_to_page(p
));
220 for_each_pbe (p
, pblist
)
221 SetPageNosaveFree(virt_to_page(p
->address
));
222 for (zone_pfn
= 0; zone_pfn
< zone
->spanned_pages
; ++zone_pfn
) {
223 if (saveable(zone
, &zone_pfn
)) {
225 page
= pfn_to_page(zone_pfn
+ zone
->zone_start_pfn
);
227 pbe
->orig_address
= (unsigned long)page_address(page
);
228 /* copy_page is not usable for copying task structs. */
229 memcpy((void *)pbe
->address
, (void *)pbe
->orig_address
, PAGE_SIZE
);
239 * free_pagedir - free pages allocated with alloc_pagedir()
242 static void free_pagedir(struct pbe
*pblist
)
247 pbe
= (pblist
+ PB_PAGE_SKIP
)->next
;
248 ClearPageNosave(virt_to_page(pblist
));
249 ClearPageNosaveFree(virt_to_page(pblist
));
250 free_page((unsigned long)pblist
);
256 * fill_pb_page - Create a list of PBEs on a given memory page
259 static inline void fill_pb_page(struct pbe
*pbpage
)
264 pbpage
+= PB_PAGE_SKIP
;
267 while (++p
< pbpage
);
271 * create_pbe_list - Create a list of PBEs on top of a given chain
272 * of memory pages allocated with alloc_pagedir()
275 static inline void create_pbe_list(struct pbe
*pblist
, unsigned int nr_pages
)
277 struct pbe
*pbpage
, *p
;
278 unsigned int num
= PBES_PER_PAGE
;
280 for_each_pb_page (pbpage
, pblist
) {
284 fill_pb_page(pbpage
);
285 num
+= PBES_PER_PAGE
;
288 for (num
-= PBES_PER_PAGE
- 1, p
= pbpage
; num
< nr_pages
; p
++, num
++)
295 * On resume it is necessary to trace and eventually free the unsafe
296 * pages that have been allocated, because they are needed for I/O
297 * (on x86-64 we likely will "eat" these pages once again while
298 * creating the temporary page translation tables)
302 struct eaten_page
*next
;
303 char padding
[PAGE_SIZE
- sizeof(void *)];
306 static struct eaten_page
*eaten_pages
= NULL
;
308 static void release_eaten_pages(void)
310 struct eaten_page
*p
, *q
;
315 /* We don't want swsusp_free() to free this page again */
316 ClearPageNosave(virt_to_page(p
));
317 free_page((unsigned long)p
);
324 * @safe_needed - on resume, for storing the PBE list and the image,
325 * we can only use memory pages that do not conflict with the pages
326 * which had been used before suspend.
328 * The unsafe pages are marked with the PG_nosave_free flag
330 * Allocated but unusable (ie eaten) memory pages should be marked
331 * so that swsusp_free() can release them
334 static inline void *alloc_image_page(gfp_t gfp_mask
, int safe_needed
)
340 res
= (void *)get_zeroed_page(gfp_mask
);
341 if (res
&& PageNosaveFree(virt_to_page(res
))) {
342 /* This is for swsusp_free() */
343 SetPageNosave(virt_to_page(res
));
344 ((struct eaten_page
*)res
)->next
= eaten_pages
;
347 } while (res
&& PageNosaveFree(virt_to_page(res
)));
349 res
= (void *)get_zeroed_page(gfp_mask
);
351 SetPageNosave(virt_to_page(res
));
352 SetPageNosaveFree(virt_to_page(res
));
357 unsigned long get_safe_page(gfp_t gfp_mask
)
359 return (unsigned long)alloc_image_page(gfp_mask
, 1);
363 * alloc_pagedir - Allocate the page directory.
365 * First, determine exactly how many pages we need and
368 * We arrange the pages in a chain: each page is an array of PBES_PER_PAGE
369 * struct pbe elements (pbes) and the last element in the page points
372 * On each page we set up a list of struct_pbe elements.
375 struct pbe
*alloc_pagedir(unsigned int nr_pages
, gfp_t gfp_mask
, int safe_needed
)
378 struct pbe
*pblist
, *pbe
;
383 pblist
= alloc_image_page(gfp_mask
, safe_needed
);
384 /* FIXME: rewrite this ugly loop */
385 for (pbe
= pblist
, num
= PBES_PER_PAGE
; pbe
&& num
< nr_pages
;
386 pbe
= pbe
->next
, num
+= PBES_PER_PAGE
) {
388 pbe
->next
= alloc_image_page(gfp_mask
, safe_needed
);
390 if (!pbe
) { /* get_zeroed_page() failed */
391 free_pagedir(pblist
);
394 create_pbe_list(pblist
, nr_pages
);
399 * Free pages we allocated for suspend. Suspend pages are alocated
400 * before atomic copy, so we need to free them after resume.
403 void swsusp_free(void)
406 unsigned long zone_pfn
;
408 for_each_zone(zone
) {
409 for (zone_pfn
= 0; zone_pfn
< zone
->spanned_pages
; ++zone_pfn
)
410 if (pfn_valid(zone_pfn
+ zone
->zone_start_pfn
)) {
412 page
= pfn_to_page(zone_pfn
+ zone
->zone_start_pfn
);
413 if (PageNosave(page
) && PageNosaveFree(page
)) {
414 ClearPageNosave(page
);
415 ClearPageNosaveFree(page
);
416 free_page((long) page_address(page
));
422 pagedir_nosave
= NULL
;
428 * enough_free_mem - Make sure we enough free memory to snapshot.
430 * Returns TRUE or FALSE after checking the number of available
434 static int enough_free_mem(unsigned int nr_pages
)
440 if (!is_highmem(zone
))
441 n
+= zone
->free_pages
;
442 pr_debug("swsusp: available memory: %u pages\n", n
);
443 return n
> (nr_pages
+ PAGES_FOR_IO
+
444 (nr_pages
+ PBES_PER_PAGE
- 1) / PBES_PER_PAGE
);
447 static int alloc_data_pages(struct pbe
*pblist
, gfp_t gfp_mask
, int safe_needed
)
451 for_each_pbe (p
, pblist
) {
452 p
->address
= (unsigned long)alloc_image_page(gfp_mask
, safe_needed
);
459 static struct pbe
*swsusp_alloc(unsigned int nr_pages
)
463 if (!(pblist
= alloc_pagedir(nr_pages
, GFP_ATOMIC
| __GFP_COLD
, 0))) {
464 printk(KERN_ERR
"suspend: Allocating pagedir failed.\n");
468 if (alloc_data_pages(pblist
, GFP_ATOMIC
| __GFP_COLD
, 0)) {
469 printk(KERN_ERR
"suspend: Allocating image pages failed.\n");
477 asmlinkage
int swsusp_save(void)
479 unsigned int nr_pages
;
481 pr_debug("swsusp: critical section: \n");
484 nr_pages
= count_data_pages();
485 printk("swsusp: Need to copy %u pages\n", nr_pages
);
487 pr_debug("swsusp: pages needed: %u + %lu + %u, free: %u\n",
489 (nr_pages
+ PBES_PER_PAGE
- 1) / PBES_PER_PAGE
,
490 PAGES_FOR_IO
, nr_free_pages());
492 if (!enough_free_mem(nr_pages
)) {
493 printk(KERN_ERR
"swsusp: Not enough free memory\n");
497 pagedir_nosave
= swsusp_alloc(nr_pages
);
501 /* During allocating of suspend pagedir, new cold pages may appear.
505 copy_data_pages(pagedir_nosave
);
508 * End of critical section. From now on, we can write to memory,
509 * but we should not touch disk. This specially means we must _not_
510 * touch swap space! Except we must write out our image of course.
513 nr_copy_pages
= nr_pages
;
514 nr_meta_pages
= (nr_pages
* sizeof(long) + PAGE_SIZE
- 1) >> PAGE_SHIFT
;
516 printk("swsusp: critical section/: done (%d pages copied)\n", nr_pages
);
520 static void init_header(struct swsusp_info
*info
)
522 memset(info
, 0, sizeof(struct swsusp_info
));
523 info
->version_code
= LINUX_VERSION_CODE
;
524 info
->num_physpages
= num_physpages
;
525 memcpy(&info
->uts
, &system_utsname
, sizeof(system_utsname
));
526 info
->cpus
= num_online_cpus();
527 info
->image_pages
= nr_copy_pages
;
528 info
->pages
= nr_copy_pages
+ nr_meta_pages
+ 1;
529 info
->size
= info
->pages
;
530 info
->size
<<= PAGE_SHIFT
;
534 * pack_orig_addresses - the .orig_address fields of the PBEs from the
535 * list starting at @pbe are stored in the array @buf[] (1 page)
538 static inline struct pbe
*pack_orig_addresses(unsigned long *buf
, struct pbe
*pbe
)
542 for (j
= 0; j
< PAGE_SIZE
/ sizeof(long) && pbe
; j
++) {
543 buf
[j
] = pbe
->orig_address
;
547 for (; j
< PAGE_SIZE
/ sizeof(long); j
++)
553 * snapshot_read_next - used for reading the system memory snapshot.
555 * On the first call to it @handle should point to a zeroed
556 * snapshot_handle structure. The structure gets updated and a pointer
557 * to it should be passed to this function every next time.
559 * The @count parameter should contain the number of bytes the caller
560 * wants to read from the snapshot. It must not be zero.
562 * On success the function returns a positive number. Then, the caller
563 * is allowed to read up to the returned number of bytes from the memory
564 * location computed by the data_of() macro. The number returned
565 * may be smaller than @count, but this only happens if the read would
566 * cross a page boundary otherwise.
568 * The function returns 0 to indicate the end of data stream condition,
569 * and a negative number is returned on error. In such cases the
570 * structure pointed to by @handle is not updated and should not be used
574 int snapshot_read_next(struct snapshot_handle
*handle
, size_t count
)
576 if (handle
->page
> nr_meta_pages
+ nr_copy_pages
)
579 /* This makes the buffer be freed by swsusp_free() */
580 buffer
= alloc_image_page(GFP_ATOMIC
, 0);
584 if (!handle
->offset
) {
585 init_header((struct swsusp_info
*)buffer
);
586 handle
->buffer
= buffer
;
587 handle
->pbe
= pagedir_nosave
;
589 if (handle
->prev
< handle
->page
) {
590 if (handle
->page
<= nr_meta_pages
) {
591 handle
->pbe
= pack_orig_addresses(buffer
, handle
->pbe
);
593 handle
->pbe
= pagedir_nosave
;
595 handle
->buffer
= (void *)handle
->pbe
->address
;
596 handle
->pbe
= handle
->pbe
->next
;
598 handle
->prev
= handle
->page
;
600 handle
->buf_offset
= handle
->page_offset
;
601 if (handle
->page_offset
+ count
>= PAGE_SIZE
) {
602 count
= PAGE_SIZE
- handle
->page_offset
;
603 handle
->page_offset
= 0;
606 handle
->page_offset
+= count
;
608 handle
->offset
+= count
;
613 * mark_unsafe_pages - mark the pages that cannot be used for storing
614 * the image during resume, because they conflict with the pages that
615 * had been used before suspend
618 static int mark_unsafe_pages(struct pbe
*pblist
)
621 unsigned long zone_pfn
;
624 if (!pblist
) /* a sanity check */
627 /* Clear page flags */
628 for_each_zone (zone
) {
629 for (zone_pfn
= 0; zone_pfn
< zone
->spanned_pages
; ++zone_pfn
)
630 if (pfn_valid(zone_pfn
+ zone
->zone_start_pfn
))
631 ClearPageNosaveFree(pfn_to_page(zone_pfn
+
632 zone
->zone_start_pfn
));
635 /* Mark orig addresses */
636 for_each_pbe (p
, pblist
) {
637 if (virt_addr_valid(p
->orig_address
))
638 SetPageNosaveFree(virt_to_page(p
->orig_address
));
646 static void copy_page_backup_list(struct pbe
*dst
, struct pbe
*src
)
648 /* We assume both lists contain the same number of elements */
650 dst
->orig_address
= src
->orig_address
;
656 static int check_header(struct swsusp_info
*info
)
660 if (info
->version_code
!= LINUX_VERSION_CODE
)
661 reason
= "kernel version";
662 if (info
->num_physpages
!= num_physpages
)
663 reason
= "memory size";
664 if (strcmp(info
->uts
.sysname
,system_utsname
.sysname
))
665 reason
= "system type";
666 if (strcmp(info
->uts
.release
,system_utsname
.release
))
667 reason
= "kernel release";
668 if (strcmp(info
->uts
.version
,system_utsname
.version
))
670 if (strcmp(info
->uts
.machine
,system_utsname
.machine
))
673 printk(KERN_ERR
"swsusp: Resume mismatch: %s\n", reason
);
680 * load header - check the image header and copy data from it
683 static int load_header(struct snapshot_handle
*handle
,
684 struct swsusp_info
*info
)
689 error
= check_header(info
);
691 pblist
= alloc_pagedir(info
->image_pages
, GFP_ATOMIC
, 0);
694 pagedir_nosave
= pblist
;
695 handle
->pbe
= pblist
;
696 nr_copy_pages
= info
->image_pages
;
697 nr_meta_pages
= info
->pages
- info
->image_pages
- 1;
703 * unpack_orig_addresses - copy the elements of @buf[] (1 page) to
704 * the PBEs in the list starting at @pbe
707 static inline struct pbe
*unpack_orig_addresses(unsigned long *buf
,
712 for (j
= 0; j
< PAGE_SIZE
/ sizeof(long) && pbe
; j
++) {
713 pbe
->orig_address
= buf
[j
];
720 * create_image - use metadata contained in the PBE list
721 * pointed to by pagedir_nosave to mark the pages that will
722 * be overwritten in the process of restoring the system
723 * memory state from the image and allocate memory for
724 * the image avoiding these pages
727 static int create_image(struct snapshot_handle
*handle
)
730 struct pbe
*p
, *pblist
;
733 error
= mark_unsafe_pages(p
);
735 pblist
= alloc_pagedir(nr_copy_pages
, GFP_ATOMIC
, 1);
737 copy_page_backup_list(pblist
, p
);
743 error
= alloc_data_pages(pblist
, GFP_ATOMIC
, 1);
745 release_eaten_pages();
746 pagedir_nosave
= pblist
;
748 pagedir_nosave
= NULL
;
757 * snapshot_write_next - used for writing the system memory snapshot.
759 * On the first call to it @handle should point to a zeroed
760 * snapshot_handle structure. The structure gets updated and a pointer
761 * to it should be passed to this function every next time.
763 * The @count parameter should contain the number of bytes the caller
764 * wants to write to the image. It must not be zero.
766 * On success the function returns a positive number. Then, the caller
767 * is allowed to write up to the returned number of bytes to the memory
768 * location computed by the data_of() macro. The number returned
769 * may be smaller than @count, but this only happens if the write would
770 * cross a page boundary otherwise.
772 * The function returns 0 to indicate the "end of file" condition,
773 * and a negative number is returned on error. In such cases the
774 * structure pointed to by @handle is not updated and should not be used
778 int snapshot_write_next(struct snapshot_handle
*handle
, size_t count
)
782 if (handle
->prev
&& handle
->page
> nr_meta_pages
+ nr_copy_pages
)
785 /* This makes the buffer be freed by swsusp_free() */
786 buffer
= alloc_image_page(GFP_ATOMIC
, 0);
791 handle
->buffer
= buffer
;
792 if (handle
->prev
< handle
->page
) {
794 error
= load_header(handle
, (struct swsusp_info
*)buffer
);
797 } else if (handle
->prev
<= nr_meta_pages
) {
798 handle
->pbe
= unpack_orig_addresses(buffer
, handle
->pbe
);
800 error
= create_image(handle
);
803 handle
->pbe
= pagedir_nosave
;
804 handle
->buffer
= (void *)handle
->pbe
->address
;
807 handle
->pbe
= handle
->pbe
->next
;
808 handle
->buffer
= (void *)handle
->pbe
->address
;
810 handle
->prev
= handle
->page
;
812 handle
->buf_offset
= handle
->page_offset
;
813 if (handle
->page_offset
+ count
>= PAGE_SIZE
) {
814 count
= PAGE_SIZE
- handle
->page_offset
;
815 handle
->page_offset
= 0;
818 handle
->page_offset
+= count
;
820 handle
->offset
+= count
;
824 int snapshot_image_loaded(struct snapshot_handle
*handle
)
826 return !(!handle
->pbe
|| handle
->pbe
->next
|| !nr_copy_pages
||
827 handle
->page
<= nr_meta_pages
+ nr_copy_pages
);