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");
127 for_each_zone (zone
) {
128 if (is_highmem(zone
))
129 res
= save_highmem_zone(zone
);
137 int restore_highmem(void)
139 printk("swsusp: Restoring Highmem\n");
140 while (highmem_copy
) {
141 struct highmem_page
*save
= highmem_copy
;
143 highmem_copy
= save
->next
;
145 kaddr
= kmap_atomic(save
->page
, KM_USER0
);
146 memcpy(kaddr
, save
->data
, PAGE_SIZE
);
147 kunmap_atomic(kaddr
, KM_USER0
);
148 free_page((long) save
->data
);
154 static inline unsigned int count_highmem_pages(void) {return 0;}
155 static inline int save_highmem(void) {return 0;}
156 static inline int restore_highmem(void) {return 0;}
159 static inline int pfn_is_nosave(unsigned long pfn
)
161 unsigned long nosave_begin_pfn
= __pa(&__nosave_begin
) >> PAGE_SHIFT
;
162 unsigned long nosave_end_pfn
= PAGE_ALIGN(__pa(&__nosave_end
)) >> PAGE_SHIFT
;
163 return (pfn
>= nosave_begin_pfn
) && (pfn
< nosave_end_pfn
);
167 * saveable - Determine whether a page should be cloned or not.
170 * We save a page if it isn't Nosave, and is not in the range of pages
171 * statically defined as 'unsaveable', and it
172 * isn't a part of a free chunk of pages.
175 static struct page
*saveable_page(unsigned long pfn
)
182 page
= pfn_to_page(pfn
);
184 if (PageNosave(page
))
186 if (PageReserved(page
) && pfn_is_nosave(pfn
))
188 if (PageNosaveFree(page
))
194 unsigned int count_data_pages(void)
197 unsigned long pfn
, max_zone_pfn
;
200 for_each_zone (zone
) {
201 if (is_highmem(zone
))
203 mark_free_pages(zone
);
204 max_zone_pfn
= zone
->zone_start_pfn
+ zone
->spanned_pages
;
205 for (pfn
= zone
->zone_start_pfn
; pfn
< max_zone_pfn
; pfn
++)
206 n
+= !!saveable_page(pfn
);
211 static void copy_data_pages(struct pbe
*pblist
)
214 unsigned long pfn
, max_zone_pfn
;
218 for_each_zone (zone
) {
219 if (is_highmem(zone
))
221 mark_free_pages(zone
);
222 /* This is necessary for swsusp_free() */
223 for_each_pb_page (p
, pblist
)
224 SetPageNosaveFree(virt_to_page(p
));
225 for_each_pbe (p
, pblist
)
226 SetPageNosaveFree(virt_to_page(p
->address
));
227 max_zone_pfn
= zone
->zone_start_pfn
+ zone
->spanned_pages
;
228 for (pfn
= zone
->zone_start_pfn
; pfn
< max_zone_pfn
; pfn
++) {
229 struct page
*page
= saveable_page(pfn
);
236 pbe
->orig_address
= (unsigned long)page_address(page
);
237 /* copy_page and memcpy are not usable for copying task structs. */
238 dst
= (long *)pbe
->address
;
239 src
= (long *)pbe
->orig_address
;
240 for (n
= PAGE_SIZE
/ sizeof(long); n
; n
--)
251 * free_pagedir - free pages allocated with alloc_pagedir()
254 static void free_pagedir(struct pbe
*pblist
, int clear_nosave_free
)
259 pbe
= (pblist
+ PB_PAGE_SKIP
)->next
;
260 ClearPageNosave(virt_to_page(pblist
));
261 if (clear_nosave_free
)
262 ClearPageNosaveFree(virt_to_page(pblist
));
263 free_page((unsigned long)pblist
);
269 * fill_pb_page - Create a list of PBEs on a given memory page
272 static inline void fill_pb_page(struct pbe
*pbpage
)
277 pbpage
+= PB_PAGE_SKIP
;
280 while (++p
< pbpage
);
284 * create_pbe_list - Create a list of PBEs on top of a given chain
285 * of memory pages allocated with alloc_pagedir()
288 static inline void create_pbe_list(struct pbe
*pblist
, unsigned int nr_pages
)
290 struct pbe
*pbpage
, *p
;
291 unsigned int num
= PBES_PER_PAGE
;
293 for_each_pb_page (pbpage
, pblist
) {
297 fill_pb_page(pbpage
);
298 num
+= PBES_PER_PAGE
;
301 for (num
-= PBES_PER_PAGE
- 1, p
= pbpage
; num
< nr_pages
; p
++, num
++)
307 static unsigned int unsafe_pages
;
310 * @safe_needed - on resume, for storing the PBE list and the image,
311 * we can only use memory pages that do not conflict with the pages
312 * used before suspend.
314 * The unsafe pages are marked with the PG_nosave_free flag
315 * and we count them using unsafe_pages
318 static void *alloc_image_page(gfp_t gfp_mask
, int safe_needed
)
322 res
= (void *)get_zeroed_page(gfp_mask
);
324 while (res
&& PageNosaveFree(virt_to_page(res
))) {
325 /* The page is unsafe, mark it for swsusp_free() */
326 SetPageNosave(virt_to_page(res
));
328 res
= (void *)get_zeroed_page(gfp_mask
);
331 SetPageNosave(virt_to_page(res
));
332 SetPageNosaveFree(virt_to_page(res
));
337 unsigned long get_safe_page(gfp_t gfp_mask
)
339 return (unsigned long)alloc_image_page(gfp_mask
, 1);
343 * alloc_pagedir - Allocate the page directory.
345 * First, determine exactly how many pages we need and
348 * We arrange the pages in a chain: each page is an array of PBES_PER_PAGE
349 * struct pbe elements (pbes) and the last element in the page points
352 * On each page we set up a list of struct_pbe elements.
355 static struct pbe
*alloc_pagedir(unsigned int nr_pages
, gfp_t gfp_mask
,
359 struct pbe
*pblist
, *pbe
;
364 pblist
= alloc_image_page(gfp_mask
, safe_needed
);
365 /* FIXME: rewrite this ugly loop */
366 for (pbe
= pblist
, num
= PBES_PER_PAGE
; pbe
&& num
< nr_pages
;
367 pbe
= pbe
->next
, num
+= PBES_PER_PAGE
) {
369 pbe
->next
= alloc_image_page(gfp_mask
, safe_needed
);
371 if (!pbe
) { /* get_zeroed_page() failed */
372 free_pagedir(pblist
, 1);
375 create_pbe_list(pblist
, nr_pages
);
380 * Free pages we allocated for suspend. Suspend pages are alocated
381 * before atomic copy, so we need to free them after resume.
384 void swsusp_free(void)
387 unsigned long pfn
, max_zone_pfn
;
389 for_each_zone(zone
) {
390 max_zone_pfn
= zone
->zone_start_pfn
+ zone
->spanned_pages
;
391 for (pfn
= zone
->zone_start_pfn
; pfn
< max_zone_pfn
; pfn
++)
392 if (pfn_valid(pfn
)) {
393 struct page
*page
= pfn_to_page(pfn
);
395 if (PageNosave(page
) && PageNosaveFree(page
)) {
396 ClearPageNosave(page
);
397 ClearPageNosaveFree(page
);
398 free_page((long) page_address(page
));
404 pagedir_nosave
= NULL
;
410 * enough_free_mem - Make sure we enough free memory to snapshot.
412 * Returns TRUE or FALSE after checking the number of available
416 static int enough_free_mem(unsigned int nr_pages
)
422 if (!is_highmem(zone
))
423 n
+= zone
->free_pages
;
424 pr_debug("swsusp: available memory: %u pages\n", n
);
425 return n
> (nr_pages
+ PAGES_FOR_IO
+
426 (nr_pages
+ PBES_PER_PAGE
- 1) / PBES_PER_PAGE
);
429 static int alloc_data_pages(struct pbe
*pblist
, gfp_t gfp_mask
, int safe_needed
)
433 for_each_pbe (p
, pblist
) {
434 p
->address
= (unsigned long)alloc_image_page(gfp_mask
, safe_needed
);
441 static struct pbe
*swsusp_alloc(unsigned int nr_pages
)
445 if (!(pblist
= alloc_pagedir(nr_pages
, GFP_ATOMIC
| __GFP_COLD
, 0))) {
446 printk(KERN_ERR
"suspend: Allocating pagedir failed.\n");
450 if (alloc_data_pages(pblist
, GFP_ATOMIC
| __GFP_COLD
, 0)) {
451 printk(KERN_ERR
"suspend: Allocating image pages failed.\n");
459 asmlinkage
int swsusp_save(void)
461 unsigned int nr_pages
;
463 pr_debug("swsusp: critical section: \n");
466 nr_pages
= count_data_pages();
467 printk("swsusp: Need to copy %u pages\n", nr_pages
);
469 pr_debug("swsusp: pages needed: %u + %lu + %u, free: %u\n",
471 (nr_pages
+ PBES_PER_PAGE
- 1) / PBES_PER_PAGE
,
472 PAGES_FOR_IO
, nr_free_pages());
474 if (!enough_free_mem(nr_pages
)) {
475 printk(KERN_ERR
"swsusp: Not enough free memory\n");
479 pagedir_nosave
= swsusp_alloc(nr_pages
);
483 /* During allocating of suspend pagedir, new cold pages may appear.
487 copy_data_pages(pagedir_nosave
);
490 * End of critical section. From now on, we can write to memory,
491 * but we should not touch disk. This specially means we must _not_
492 * touch swap space! Except we must write out our image of course.
495 nr_copy_pages
= nr_pages
;
496 nr_meta_pages
= (nr_pages
* sizeof(long) + PAGE_SIZE
- 1) >> PAGE_SHIFT
;
498 printk("swsusp: critical section/: done (%d pages copied)\n", nr_pages
);
502 static void init_header(struct swsusp_info
*info
)
504 memset(info
, 0, sizeof(struct swsusp_info
));
505 info
->version_code
= LINUX_VERSION_CODE
;
506 info
->num_physpages
= num_physpages
;
507 memcpy(&info
->uts
, &system_utsname
, sizeof(system_utsname
));
508 info
->cpus
= num_online_cpus();
509 info
->image_pages
= nr_copy_pages
;
510 info
->pages
= nr_copy_pages
+ nr_meta_pages
+ 1;
511 info
->size
= info
->pages
;
512 info
->size
<<= PAGE_SHIFT
;
516 * pack_orig_addresses - the .orig_address fields of the PBEs from the
517 * list starting at @pbe are stored in the array @buf[] (1 page)
520 static inline struct pbe
*pack_orig_addresses(unsigned long *buf
, struct pbe
*pbe
)
524 for (j
= 0; j
< PAGE_SIZE
/ sizeof(long) && pbe
; j
++) {
525 buf
[j
] = pbe
->orig_address
;
529 for (; j
< PAGE_SIZE
/ sizeof(long); j
++)
535 * snapshot_read_next - used for reading the system memory snapshot.
537 * On the first call to it @handle should point to a zeroed
538 * snapshot_handle structure. The structure gets updated and a pointer
539 * to it should be passed to this function every next time.
541 * The @count parameter should contain the number of bytes the caller
542 * wants to read from the snapshot. It must not be zero.
544 * On success the function returns a positive number. Then, the caller
545 * is allowed to read up to the returned number of bytes from the memory
546 * location computed by the data_of() macro. The number returned
547 * may be smaller than @count, but this only happens if the read would
548 * cross a page boundary otherwise.
550 * The function returns 0 to indicate the end of data stream condition,
551 * and a negative number is returned on error. In such cases the
552 * structure pointed to by @handle is not updated and should not be used
556 int snapshot_read_next(struct snapshot_handle
*handle
, size_t count
)
558 if (handle
->page
> nr_meta_pages
+ nr_copy_pages
)
561 /* This makes the buffer be freed by swsusp_free() */
562 buffer
= alloc_image_page(GFP_ATOMIC
, 0);
566 if (!handle
->offset
) {
567 init_header((struct swsusp_info
*)buffer
);
568 handle
->buffer
= buffer
;
569 handle
->pbe
= pagedir_nosave
;
571 if (handle
->prev
< handle
->page
) {
572 if (handle
->page
<= nr_meta_pages
) {
573 handle
->pbe
= pack_orig_addresses(buffer
, handle
->pbe
);
575 handle
->pbe
= pagedir_nosave
;
577 handle
->buffer
= (void *)handle
->pbe
->address
;
578 handle
->pbe
= handle
->pbe
->next
;
580 handle
->prev
= handle
->page
;
582 handle
->buf_offset
= handle
->page_offset
;
583 if (handle
->page_offset
+ count
>= PAGE_SIZE
) {
584 count
= PAGE_SIZE
- handle
->page_offset
;
585 handle
->page_offset
= 0;
588 handle
->page_offset
+= count
;
590 handle
->offset
+= count
;
595 * mark_unsafe_pages - mark the pages that cannot be used for storing
596 * the image during resume, because they conflict with the pages that
597 * had been used before suspend
600 static int mark_unsafe_pages(struct pbe
*pblist
)
603 unsigned long pfn
, max_zone_pfn
;
606 if (!pblist
) /* a sanity check */
609 /* Clear page flags */
610 for_each_zone (zone
) {
611 max_zone_pfn
= zone
->zone_start_pfn
+ zone
->spanned_pages
;
612 for (pfn
= zone
->zone_start_pfn
; pfn
< max_zone_pfn
; pfn
++)
614 ClearPageNosaveFree(pfn_to_page(pfn
));
617 /* Mark orig addresses */
618 for_each_pbe (p
, pblist
) {
619 if (virt_addr_valid(p
->orig_address
))
620 SetPageNosaveFree(virt_to_page(p
->orig_address
));
630 static void copy_page_backup_list(struct pbe
*dst
, struct pbe
*src
)
632 /* We assume both lists contain the same number of elements */
634 dst
->orig_address
= src
->orig_address
;
640 static int check_header(struct swsusp_info
*info
)
644 if (info
->version_code
!= LINUX_VERSION_CODE
)
645 reason
= "kernel version";
646 if (info
->num_physpages
!= num_physpages
)
647 reason
= "memory size";
648 if (strcmp(info
->uts
.sysname
,system_utsname
.sysname
))
649 reason
= "system type";
650 if (strcmp(info
->uts
.release
,system_utsname
.release
))
651 reason
= "kernel release";
652 if (strcmp(info
->uts
.version
,system_utsname
.version
))
654 if (strcmp(info
->uts
.machine
,system_utsname
.machine
))
657 printk(KERN_ERR
"swsusp: Resume mismatch: %s\n", reason
);
664 * load header - check the image header and copy data from it
667 static int load_header(struct snapshot_handle
*handle
,
668 struct swsusp_info
*info
)
673 error
= check_header(info
);
675 pblist
= alloc_pagedir(info
->image_pages
, GFP_ATOMIC
, 0);
678 pagedir_nosave
= pblist
;
679 handle
->pbe
= pblist
;
680 nr_copy_pages
= info
->image_pages
;
681 nr_meta_pages
= info
->pages
- info
->image_pages
- 1;
687 * unpack_orig_addresses - copy the elements of @buf[] (1 page) to
688 * the PBEs in the list starting at @pbe
691 static inline struct pbe
*unpack_orig_addresses(unsigned long *buf
,
696 for (j
= 0; j
< PAGE_SIZE
/ sizeof(long) && pbe
; j
++) {
697 pbe
->orig_address
= buf
[j
];
704 * prepare_image - use metadata contained in the PBE list
705 * pointed to by pagedir_nosave to mark the pages that will
706 * be overwritten in the process of restoring the system
707 * memory state from the image ("unsafe" pages) and allocate
708 * memory for the image
710 * The idea is to allocate the PBE list first and then
711 * allocate as many pages as it's needed for the image data,
712 * but not to assign these pages to the PBEs initially.
713 * Instead, we just mark them as allocated and create a list
714 * of "safe" which will be used later
718 struct safe_page
*next
;
719 char padding
[PAGE_SIZE
- sizeof(void *)];
722 static struct safe_page
*safe_pages
;
724 static int prepare_image(struct snapshot_handle
*handle
)
727 unsigned int nr_pages
= nr_copy_pages
;
728 struct pbe
*p
, *pblist
= NULL
;
731 error
= mark_unsafe_pages(p
);
733 pblist
= alloc_pagedir(nr_pages
, GFP_ATOMIC
, 1);
735 copy_page_backup_list(pblist
, p
);
741 if (!error
&& nr_pages
> unsafe_pages
) {
742 nr_pages
-= unsafe_pages
;
744 struct safe_page
*ptr
;
746 ptr
= (struct safe_page
*)get_zeroed_page(GFP_ATOMIC
);
751 if (!PageNosaveFree(virt_to_page(ptr
))) {
752 /* The page is "safe", add it to the list */
753 ptr
->next
= safe_pages
;
756 /* Mark the page as allocated */
757 SetPageNosave(virt_to_page(ptr
));
758 SetPageNosaveFree(virt_to_page(ptr
));
762 pagedir_nosave
= pblist
;
770 static void *get_buffer(struct snapshot_handle
*handle
)
772 struct pbe
*pbe
= handle
->pbe
, *last
= handle
->last_pbe
;
773 struct page
*page
= virt_to_page(pbe
->orig_address
);
775 if (PageNosave(page
) && PageNosaveFree(page
)) {
777 * We have allocated the "original" page frame and we can
778 * use it directly to store the read page
781 if (last
&& last
->next
)
783 return (void *)pbe
->orig_address
;
786 * The "original" page frame has not been allocated and we have to
787 * use a "safe" page frame to store the read page
789 pbe
->address
= (unsigned long)safe_pages
;
790 safe_pages
= safe_pages
->next
;
793 handle
->last_pbe
= pbe
;
794 return (void *)pbe
->address
;
798 * snapshot_write_next - used for writing the system memory snapshot.
800 * On the first call to it @handle should point to a zeroed
801 * snapshot_handle structure. The structure gets updated and a pointer
802 * to it should be passed to this function every next time.
804 * The @count parameter should contain the number of bytes the caller
805 * wants to write to the image. It must not be zero.
807 * On success the function returns a positive number. Then, the caller
808 * is allowed to write up to the returned number of bytes to the memory
809 * location computed by the data_of() macro. The number returned
810 * may be smaller than @count, but this only happens if the write would
811 * cross a page boundary otherwise.
813 * The function returns 0 to indicate the "end of file" condition,
814 * and a negative number is returned on error. In such cases the
815 * structure pointed to by @handle is not updated and should not be used
819 int snapshot_write_next(struct snapshot_handle
*handle
, size_t count
)
823 if (handle
->prev
&& handle
->page
> nr_meta_pages
+ nr_copy_pages
)
826 /* This makes the buffer be freed by swsusp_free() */
827 buffer
= alloc_image_page(GFP_ATOMIC
, 0);
832 handle
->buffer
= buffer
;
833 handle
->sync_read
= 1;
834 if (handle
->prev
< handle
->page
) {
836 error
= load_header(handle
,
837 (struct swsusp_info
*)buffer
);
840 } else if (handle
->prev
<= nr_meta_pages
) {
841 handle
->pbe
= unpack_orig_addresses(buffer
,
844 error
= prepare_image(handle
);
847 handle
->pbe
= pagedir_nosave
;
848 handle
->last_pbe
= NULL
;
849 handle
->buffer
= get_buffer(handle
);
850 handle
->sync_read
= 0;
853 handle
->pbe
= handle
->pbe
->next
;
854 handle
->buffer
= get_buffer(handle
);
855 handle
->sync_read
= 0;
857 handle
->prev
= handle
->page
;
859 handle
->buf_offset
= handle
->page_offset
;
860 if (handle
->page_offset
+ count
>= PAGE_SIZE
) {
861 count
= PAGE_SIZE
- handle
->page_offset
;
862 handle
->page_offset
= 0;
865 handle
->page_offset
+= count
;
867 handle
->offset
+= count
;
871 int snapshot_image_loaded(struct snapshot_handle
*handle
)
873 return !(!handle
->pbe
|| handle
->pbe
->next
|| !nr_copy_pages
||
874 handle
->page
<= nr_meta_pages
+ nr_copy_pages
);
This page took 0.047856 seconds and 5 git commands to generate.