2 * linux/kernel/power/snapshot.c
4 * This file provides system snapshot/restore functionality for swsusp.
6 * Copyright (C) 1998-2005 Pavel Machek <pavel@suse.cz>
7 * Copyright (C) 2006 Rafael J. Wysocki <rjw@sisk.pl>
9 * This file is released under the GPLv2.
13 #include <linux/version.h>
14 #include <linux/module.h>
16 #include <linux/suspend.h>
17 #include <linux/delay.h>
18 #include <linux/bitops.h>
19 #include <linux/spinlock.h>
20 #include <linux/kernel.h>
22 #include <linux/device.h>
23 #include <linux/init.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 static int swsusp_page_is_free(struct page
*);
38 static void swsusp_set_page_forbidden(struct page
*);
39 static void swsusp_unset_page_forbidden(struct page
*);
41 /* List of PBEs needed for restoring the pages that were allocated before
42 * the suspend and included in the suspend image, but have also been
43 * allocated by the "resume" kernel, so their contents cannot be written
44 * directly to their "original" page frames.
46 struct pbe
*restore_pblist
;
48 /* Pointer to an auxiliary buffer (1 page) */
52 * @safe_needed - on resume, for storing the PBE list and the image,
53 * we can only use memory pages that do not conflict with the pages
54 * used before suspend. The unsafe pages have PageNosaveFree set
55 * and we count them using unsafe_pages.
57 * Each allocated image page is marked as PageNosave and PageNosaveFree
58 * so that swsusp_free() can release it.
63 #define PG_UNSAFE_CLEAR 1
64 #define PG_UNSAFE_KEEP 0
66 static unsigned int allocated_unsafe_pages
;
68 static void *get_image_page(gfp_t gfp_mask
, int safe_needed
)
72 res
= (void *)get_zeroed_page(gfp_mask
);
74 while (res
&& swsusp_page_is_free(virt_to_page(res
))) {
75 /* The page is unsafe, mark it for swsusp_free() */
76 swsusp_set_page_forbidden(virt_to_page(res
));
77 allocated_unsafe_pages
++;
78 res
= (void *)get_zeroed_page(gfp_mask
);
81 swsusp_set_page_forbidden(virt_to_page(res
));
82 swsusp_set_page_free(virt_to_page(res
));
87 unsigned long get_safe_page(gfp_t gfp_mask
)
89 return (unsigned long)get_image_page(gfp_mask
, PG_SAFE
);
92 static struct page
*alloc_image_page(gfp_t gfp_mask
)
96 page
= alloc_page(gfp_mask
);
98 swsusp_set_page_forbidden(page
);
99 swsusp_set_page_free(page
);
105 * free_image_page - free page represented by @addr, allocated with
106 * get_image_page (page flags set by it must be cleared)
109 static inline void free_image_page(void *addr
, int clear_nosave_free
)
113 BUG_ON(!virt_addr_valid(addr
));
115 page
= virt_to_page(addr
);
117 swsusp_unset_page_forbidden(page
);
118 if (clear_nosave_free
)
119 swsusp_unset_page_free(page
);
124 /* struct linked_page is used to build chains of pages */
126 #define LINKED_PAGE_DATA_SIZE (PAGE_SIZE - sizeof(void *))
129 struct linked_page
*next
;
130 char data
[LINKED_PAGE_DATA_SIZE
];
131 } __attribute__((packed
));
134 free_list_of_pages(struct linked_page
*list
, int clear_page_nosave
)
137 struct linked_page
*lp
= list
->next
;
139 free_image_page(list
, clear_page_nosave
);
145 * struct chain_allocator is used for allocating small objects out of
146 * a linked list of pages called 'the chain'.
148 * The chain grows each time when there is no room for a new object in
149 * the current page. The allocated objects cannot be freed individually.
150 * It is only possible to free them all at once, by freeing the entire
153 * NOTE: The chain allocator may be inefficient if the allocated objects
154 * are not much smaller than PAGE_SIZE.
157 struct chain_allocator
{
158 struct linked_page
*chain
; /* the chain */
159 unsigned int used_space
; /* total size of objects allocated out
160 * of the current page
162 gfp_t gfp_mask
; /* mask for allocating pages */
163 int safe_needed
; /* if set, only "safe" pages are allocated */
167 chain_init(struct chain_allocator
*ca
, gfp_t gfp_mask
, int safe_needed
)
170 ca
->used_space
= LINKED_PAGE_DATA_SIZE
;
171 ca
->gfp_mask
= gfp_mask
;
172 ca
->safe_needed
= safe_needed
;
175 static void *chain_alloc(struct chain_allocator
*ca
, unsigned int size
)
179 if (LINKED_PAGE_DATA_SIZE
- ca
->used_space
< size
) {
180 struct linked_page
*lp
;
182 lp
= get_image_page(ca
->gfp_mask
, ca
->safe_needed
);
186 lp
->next
= ca
->chain
;
190 ret
= ca
->chain
->data
+ ca
->used_space
;
191 ca
->used_space
+= size
;
195 static void chain_free(struct chain_allocator
*ca
, int clear_page_nosave
)
197 free_list_of_pages(ca
->chain
, clear_page_nosave
);
198 memset(ca
, 0, sizeof(struct chain_allocator
));
202 * Data types related to memory bitmaps.
204 * Memory bitmap is a structure consiting of many linked lists of
205 * objects. The main list's elements are of type struct zone_bitmap
206 * and each of them corresonds to one zone. For each zone bitmap
207 * object there is a list of objects of type struct bm_block that
208 * represent each blocks of bitmap in which information is stored.
210 * struct memory_bitmap contains a pointer to the main list of zone
211 * bitmap objects, a struct bm_position used for browsing the bitmap,
212 * and a pointer to the list of pages used for allocating all of the
213 * zone bitmap objects and bitmap block objects.
215 * NOTE: It has to be possible to lay out the bitmap in memory
216 * using only allocations of order 0. Additionally, the bitmap is
217 * designed to work with arbitrary number of zones (this is over the
218 * top for now, but let's avoid making unnecessary assumptions ;-).
220 * struct zone_bitmap contains a pointer to a list of bitmap block
221 * objects and a pointer to the bitmap block object that has been
222 * most recently used for setting bits. Additionally, it contains the
223 * pfns that correspond to the start and end of the represented zone.
225 * struct bm_block contains a pointer to the memory page in which
226 * information is stored (in the form of a block of bitmap)
227 * It also contains the pfns that correspond to the start and end of
228 * the represented memory area.
231 #define BM_END_OF_MAP (~0UL)
233 #define BM_BITS_PER_BLOCK (PAGE_SIZE << 3)
236 struct bm_block
*next
; /* next element of the list */
237 unsigned long start_pfn
; /* pfn represented by the first bit */
238 unsigned long end_pfn
; /* pfn represented by the last bit plus 1 */
239 unsigned long *data
; /* bitmap representing pages */
242 static inline unsigned long bm_block_bits(struct bm_block
*bb
)
244 return bb
->end_pfn
- bb
->start_pfn
;
248 struct zone_bitmap
*next
; /* next element of the list */
249 unsigned long start_pfn
; /* minimal pfn in this zone */
250 unsigned long end_pfn
; /* maximal pfn in this zone plus 1 */
251 struct bm_block
*bm_blocks
; /* list of bitmap blocks */
252 struct bm_block
*cur_block
; /* recently used bitmap block */
255 /* strcut bm_position is used for browsing memory bitmaps */
258 struct zone_bitmap
*zone_bm
;
259 struct bm_block
*block
;
263 struct memory_bitmap
{
264 struct zone_bitmap
*zone_bm_list
; /* list of zone bitmaps */
265 struct linked_page
*p_list
; /* list of pages used to store zone
266 * bitmap objects and bitmap block
269 struct bm_position cur
; /* most recently used bit position */
272 /* Functions that operate on memory bitmaps */
274 static void memory_bm_position_reset(struct memory_bitmap
*bm
)
276 struct zone_bitmap
*zone_bm
;
278 zone_bm
= bm
->zone_bm_list
;
279 bm
->cur
.zone_bm
= zone_bm
;
280 bm
->cur
.block
= zone_bm
->bm_blocks
;
284 static void memory_bm_free(struct memory_bitmap
*bm
, int clear_nosave_free
);
287 * create_bm_block_list - create a list of block bitmap objects
290 static inline struct bm_block
*
291 create_bm_block_list(unsigned int nr_blocks
, struct chain_allocator
*ca
)
293 struct bm_block
*bblist
= NULL
;
295 while (nr_blocks
-- > 0) {
298 bb
= chain_alloc(ca
, sizeof(struct bm_block
));
309 * create_zone_bm_list - create a list of zone bitmap objects
312 static inline struct zone_bitmap
*
313 create_zone_bm_list(unsigned int nr_zones
, struct chain_allocator
*ca
)
315 struct zone_bitmap
*zbmlist
= NULL
;
317 while (nr_zones
-- > 0) {
318 struct zone_bitmap
*zbm
;
320 zbm
= chain_alloc(ca
, sizeof(struct zone_bitmap
));
331 * memory_bm_create - allocate memory for a memory bitmap
335 memory_bm_create(struct memory_bitmap
*bm
, gfp_t gfp_mask
, int safe_needed
)
337 struct chain_allocator ca
;
339 struct zone_bitmap
*zone_bm
;
343 chain_init(&ca
, gfp_mask
, safe_needed
);
345 /* Compute the number of zones */
348 if (populated_zone(zone
))
351 /* Allocate the list of zones bitmap objects */
352 zone_bm
= create_zone_bm_list(nr
, &ca
);
353 bm
->zone_bm_list
= zone_bm
;
355 chain_free(&ca
, PG_UNSAFE_CLEAR
);
359 /* Initialize the zone bitmap objects */
360 for_each_zone(zone
) {
363 if (!populated_zone(zone
))
366 zone_bm
->start_pfn
= zone
->zone_start_pfn
;
367 zone_bm
->end_pfn
= zone
->zone_start_pfn
+ zone
->spanned_pages
;
368 /* Allocate the list of bitmap block objects */
369 nr
= DIV_ROUND_UP(zone
->spanned_pages
, BM_BITS_PER_BLOCK
);
370 bb
= create_bm_block_list(nr
, &ca
);
371 zone_bm
->bm_blocks
= bb
;
372 zone_bm
->cur_block
= bb
;
376 nr
= zone
->spanned_pages
;
377 pfn
= zone
->zone_start_pfn
;
378 /* Initialize the bitmap block objects */
382 ptr
= get_image_page(gfp_mask
, safe_needed
);
388 if (nr
>= BM_BITS_PER_BLOCK
) {
389 pfn
+= BM_BITS_PER_BLOCK
;
390 nr
-= BM_BITS_PER_BLOCK
;
392 /* This is executed only once in the loop */
398 zone_bm
= zone_bm
->next
;
400 bm
->p_list
= ca
.chain
;
401 memory_bm_position_reset(bm
);
405 bm
->p_list
= ca
.chain
;
406 memory_bm_free(bm
, PG_UNSAFE_CLEAR
);
411 * memory_bm_free - free memory occupied by the memory bitmap @bm
414 static void memory_bm_free(struct memory_bitmap
*bm
, int clear_nosave_free
)
416 struct zone_bitmap
*zone_bm
;
418 /* Free the list of bit blocks for each zone_bitmap object */
419 zone_bm
= bm
->zone_bm_list
;
423 bb
= zone_bm
->bm_blocks
;
426 free_image_page(bb
->data
, clear_nosave_free
);
429 zone_bm
= zone_bm
->next
;
431 free_list_of_pages(bm
->p_list
, clear_nosave_free
);
432 bm
->zone_bm_list
= NULL
;
436 * memory_bm_find_bit - find the bit in the bitmap @bm that corresponds
437 * to given pfn. The cur_zone_bm member of @bm and the cur_block member
438 * of @bm->cur_zone_bm are updated.
441 static int memory_bm_find_bit(struct memory_bitmap
*bm
, unsigned long pfn
,
442 void **addr
, unsigned int *bit_nr
)
444 struct zone_bitmap
*zone_bm
;
447 /* Check if the pfn is from the current zone */
448 zone_bm
= bm
->cur
.zone_bm
;
449 if (pfn
< zone_bm
->start_pfn
|| pfn
>= zone_bm
->end_pfn
) {
450 zone_bm
= bm
->zone_bm_list
;
451 /* We don't assume that the zones are sorted by pfns */
452 while (pfn
< zone_bm
->start_pfn
|| pfn
>= zone_bm
->end_pfn
) {
453 zone_bm
= zone_bm
->next
;
458 bm
->cur
.zone_bm
= zone_bm
;
460 /* Check if the pfn corresponds to the current bitmap block */
461 bb
= zone_bm
->cur_block
;
462 if (pfn
< bb
->start_pfn
)
463 bb
= zone_bm
->bm_blocks
;
465 while (pfn
>= bb
->end_pfn
) {
470 zone_bm
->cur_block
= bb
;
471 pfn
-= bb
->start_pfn
;
477 static void memory_bm_set_bit(struct memory_bitmap
*bm
, unsigned long pfn
)
483 error
= memory_bm_find_bit(bm
, pfn
, &addr
, &bit
);
488 static int mem_bm_set_bit_check(struct memory_bitmap
*bm
, unsigned long pfn
)
494 error
= memory_bm_find_bit(bm
, pfn
, &addr
, &bit
);
500 static void memory_bm_clear_bit(struct memory_bitmap
*bm
, unsigned long pfn
)
506 error
= memory_bm_find_bit(bm
, pfn
, &addr
, &bit
);
508 clear_bit(bit
, addr
);
511 static int memory_bm_test_bit(struct memory_bitmap
*bm
, unsigned long pfn
)
517 error
= memory_bm_find_bit(bm
, pfn
, &addr
, &bit
);
519 return test_bit(bit
, addr
);
523 * memory_bm_next_pfn - find the pfn that corresponds to the next set bit
524 * in the bitmap @bm. If the pfn cannot be found, BM_END_OF_MAP is
527 * It is required to run memory_bm_position_reset() before the first call to
531 static unsigned long memory_bm_next_pfn(struct memory_bitmap
*bm
)
533 struct zone_bitmap
*zone_bm
;
541 bit
= find_next_bit(bb
->data
, bm_block_bits(bb
), bit
);
542 if (bit
< bm_block_bits(bb
))
549 zone_bm
= bm
->cur
.zone_bm
->next
;
551 bm
->cur
.zone_bm
= zone_bm
;
552 bm
->cur
.block
= zone_bm
->bm_blocks
;
556 memory_bm_position_reset(bm
);
557 return BM_END_OF_MAP
;
560 bm
->cur
.bit
= bit
+ 1;
561 return bb
->start_pfn
+ bit
;
565 * This structure represents a range of page frames the contents of which
566 * should not be saved during the suspend.
569 struct nosave_region
{
570 struct list_head list
;
571 unsigned long start_pfn
;
572 unsigned long end_pfn
;
575 static LIST_HEAD(nosave_regions
);
578 * register_nosave_region - register a range of page frames the contents
579 * of which should not be saved during the suspend (to be used in the early
580 * initialization code)
584 __register_nosave_region(unsigned long start_pfn
, unsigned long end_pfn
,
587 struct nosave_region
*region
;
589 if (start_pfn
>= end_pfn
)
592 if (!list_empty(&nosave_regions
)) {
593 /* Try to extend the previous region (they should be sorted) */
594 region
= list_entry(nosave_regions
.prev
,
595 struct nosave_region
, list
);
596 if (region
->end_pfn
== start_pfn
) {
597 region
->end_pfn
= end_pfn
;
602 /* during init, this shouldn't fail */
603 region
= kmalloc(sizeof(struct nosave_region
), GFP_KERNEL
);
606 /* This allocation cannot fail */
607 region
= alloc_bootmem_low(sizeof(struct nosave_region
));
608 region
->start_pfn
= start_pfn
;
609 region
->end_pfn
= end_pfn
;
610 list_add_tail(®ion
->list
, &nosave_regions
);
612 printk(KERN_INFO
"PM: Registered nosave memory: %016lx - %016lx\n",
613 start_pfn
<< PAGE_SHIFT
, end_pfn
<< PAGE_SHIFT
);
617 * Set bits in this map correspond to the page frames the contents of which
618 * should not be saved during the suspend.
620 static struct memory_bitmap
*forbidden_pages_map
;
622 /* Set bits in this map correspond to free page frames. */
623 static struct memory_bitmap
*free_pages_map
;
626 * Each page frame allocated for creating the image is marked by setting the
627 * corresponding bits in forbidden_pages_map and free_pages_map simultaneously
630 void swsusp_set_page_free(struct page
*page
)
633 memory_bm_set_bit(free_pages_map
, page_to_pfn(page
));
636 static int swsusp_page_is_free(struct page
*page
)
638 return free_pages_map
?
639 memory_bm_test_bit(free_pages_map
, page_to_pfn(page
)) : 0;
642 void swsusp_unset_page_free(struct page
*page
)
645 memory_bm_clear_bit(free_pages_map
, page_to_pfn(page
));
648 static void swsusp_set_page_forbidden(struct page
*page
)
650 if (forbidden_pages_map
)
651 memory_bm_set_bit(forbidden_pages_map
, page_to_pfn(page
));
654 int swsusp_page_is_forbidden(struct page
*page
)
656 return forbidden_pages_map
?
657 memory_bm_test_bit(forbidden_pages_map
, page_to_pfn(page
)) : 0;
660 static void swsusp_unset_page_forbidden(struct page
*page
)
662 if (forbidden_pages_map
)
663 memory_bm_clear_bit(forbidden_pages_map
, page_to_pfn(page
));
667 * mark_nosave_pages - set bits corresponding to the page frames the
668 * contents of which should not be saved in a given bitmap.
671 static void mark_nosave_pages(struct memory_bitmap
*bm
)
673 struct nosave_region
*region
;
675 if (list_empty(&nosave_regions
))
678 list_for_each_entry(region
, &nosave_regions
, list
) {
681 pr_debug("PM: Marking nosave pages: %016lx - %016lx\n",
682 region
->start_pfn
<< PAGE_SHIFT
,
683 region
->end_pfn
<< PAGE_SHIFT
);
685 for (pfn
= region
->start_pfn
; pfn
< region
->end_pfn
; pfn
++)
686 if (pfn_valid(pfn
)) {
688 * It is safe to ignore the result of
689 * mem_bm_set_bit_check() here, since we won't
690 * touch the PFNs for which the error is
693 mem_bm_set_bit_check(bm
, pfn
);
699 * create_basic_memory_bitmaps - create bitmaps needed for marking page
700 * frames that should not be saved and free page frames. The pointers
701 * forbidden_pages_map and free_pages_map are only modified if everything
702 * goes well, because we don't want the bits to be used before both bitmaps
706 int create_basic_memory_bitmaps(void)
708 struct memory_bitmap
*bm1
, *bm2
;
711 BUG_ON(forbidden_pages_map
|| free_pages_map
);
713 bm1
= kzalloc(sizeof(struct memory_bitmap
), GFP_KERNEL
);
717 error
= memory_bm_create(bm1
, GFP_KERNEL
, PG_ANY
);
719 goto Free_first_object
;
721 bm2
= kzalloc(sizeof(struct memory_bitmap
), GFP_KERNEL
);
723 goto Free_first_bitmap
;
725 error
= memory_bm_create(bm2
, GFP_KERNEL
, PG_ANY
);
727 goto Free_second_object
;
729 forbidden_pages_map
= bm1
;
730 free_pages_map
= bm2
;
731 mark_nosave_pages(forbidden_pages_map
);
733 pr_debug("PM: Basic memory bitmaps created\n");
740 memory_bm_free(bm1
, PG_UNSAFE_CLEAR
);
747 * free_basic_memory_bitmaps - free memory bitmaps allocated by
748 * create_basic_memory_bitmaps(). The auxiliary pointers are necessary
749 * so that the bitmaps themselves are not referred to while they are being
753 void free_basic_memory_bitmaps(void)
755 struct memory_bitmap
*bm1
, *bm2
;
757 BUG_ON(!(forbidden_pages_map
&& free_pages_map
));
759 bm1
= forbidden_pages_map
;
760 bm2
= free_pages_map
;
761 forbidden_pages_map
= NULL
;
762 free_pages_map
= NULL
;
763 memory_bm_free(bm1
, PG_UNSAFE_CLEAR
);
765 memory_bm_free(bm2
, PG_UNSAFE_CLEAR
);
768 pr_debug("PM: Basic memory bitmaps freed\n");
772 * snapshot_additional_pages - estimate the number of additional pages
773 * be needed for setting up the suspend image data structures for given
774 * zone (usually the returned value is greater than the exact number)
777 unsigned int snapshot_additional_pages(struct zone
*zone
)
781 res
= DIV_ROUND_UP(zone
->spanned_pages
, BM_BITS_PER_BLOCK
);
782 res
+= DIV_ROUND_UP(res
* sizeof(struct bm_block
), PAGE_SIZE
);
786 #ifdef CONFIG_HIGHMEM
788 * count_free_highmem_pages - compute the total number of free highmem
789 * pages, system-wide.
792 static unsigned int count_free_highmem_pages(void)
795 unsigned int cnt
= 0;
798 if (populated_zone(zone
) && is_highmem(zone
))
799 cnt
+= zone_page_state(zone
, NR_FREE_PAGES
);
805 * saveable_highmem_page - Determine whether a highmem page should be
806 * included in the suspend image.
808 * We should save the page if it isn't Nosave or NosaveFree, or Reserved,
809 * and it isn't a part of a free chunk of pages.
812 static struct page
*saveable_highmem_page(unsigned long pfn
)
819 page
= pfn_to_page(pfn
);
821 BUG_ON(!PageHighMem(page
));
823 if (swsusp_page_is_forbidden(page
) || swsusp_page_is_free(page
) ||
831 * count_highmem_pages - compute the total number of saveable highmem
835 unsigned int count_highmem_pages(void)
840 for_each_zone(zone
) {
841 unsigned long pfn
, max_zone_pfn
;
843 if (!is_highmem(zone
))
846 mark_free_pages(zone
);
847 max_zone_pfn
= zone
->zone_start_pfn
+ zone
->spanned_pages
;
848 for (pfn
= zone
->zone_start_pfn
; pfn
< max_zone_pfn
; pfn
++)
849 if (saveable_highmem_page(pfn
))
855 static inline void *saveable_highmem_page(unsigned long pfn
) { return NULL
; }
856 #endif /* CONFIG_HIGHMEM */
859 * saveable_page - Determine whether a non-highmem page should be included
860 * in the suspend image.
862 * We should save the page if it isn't Nosave, and is not in the range
863 * of pages statically defined as 'unsaveable', and it isn't a part of
864 * a free chunk of pages.
867 static struct page
*saveable_page(unsigned long pfn
)
874 page
= pfn_to_page(pfn
);
876 BUG_ON(PageHighMem(page
));
878 if (swsusp_page_is_forbidden(page
) || swsusp_page_is_free(page
))
881 if (PageReserved(page
)
882 && (!kernel_page_present(page
) || pfn_is_nosave(pfn
)))
889 * count_data_pages - compute the total number of saveable non-highmem
893 unsigned int count_data_pages(void)
896 unsigned long pfn
, max_zone_pfn
;
899 for_each_zone(zone
) {
900 if (is_highmem(zone
))
903 mark_free_pages(zone
);
904 max_zone_pfn
= zone
->zone_start_pfn
+ zone
->spanned_pages
;
905 for (pfn
= zone
->zone_start_pfn
; pfn
< max_zone_pfn
; pfn
++)
906 if(saveable_page(pfn
))
912 /* This is needed, because copy_page and memcpy are not usable for copying
915 static inline void do_copy_page(long *dst
, long *src
)
919 for (n
= PAGE_SIZE
/ sizeof(long); n
; n
--)
925 * safe_copy_page - check if the page we are going to copy is marked as
926 * present in the kernel page tables (this always is the case if
927 * CONFIG_DEBUG_PAGEALLOC is not set and in that case
928 * kernel_page_present() always returns 'true').
930 static void safe_copy_page(void *dst
, struct page
*s_page
)
932 if (kernel_page_present(s_page
)) {
933 do_copy_page(dst
, page_address(s_page
));
935 kernel_map_pages(s_page
, 1, 1);
936 do_copy_page(dst
, page_address(s_page
));
937 kernel_map_pages(s_page
, 1, 0);
942 #ifdef CONFIG_HIGHMEM
943 static inline struct page
*
944 page_is_saveable(struct zone
*zone
, unsigned long pfn
)
946 return is_highmem(zone
) ?
947 saveable_highmem_page(pfn
) : saveable_page(pfn
);
950 static void copy_data_page(unsigned long dst_pfn
, unsigned long src_pfn
)
952 struct page
*s_page
, *d_page
;
955 s_page
= pfn_to_page(src_pfn
);
956 d_page
= pfn_to_page(dst_pfn
);
957 if (PageHighMem(s_page
)) {
958 src
= kmap_atomic(s_page
, KM_USER0
);
959 dst
= kmap_atomic(d_page
, KM_USER1
);
960 do_copy_page(dst
, src
);
961 kunmap_atomic(src
, KM_USER0
);
962 kunmap_atomic(dst
, KM_USER1
);
964 if (PageHighMem(d_page
)) {
965 /* Page pointed to by src may contain some kernel
966 * data modified by kmap_atomic()
968 safe_copy_page(buffer
, s_page
);
969 dst
= kmap_atomic(pfn_to_page(dst_pfn
), KM_USER0
);
970 memcpy(dst
, buffer
, PAGE_SIZE
);
971 kunmap_atomic(dst
, KM_USER0
);
973 safe_copy_page(page_address(d_page
), s_page
);
978 #define page_is_saveable(zone, pfn) saveable_page(pfn)
980 static inline void copy_data_page(unsigned long dst_pfn
, unsigned long src_pfn
)
982 safe_copy_page(page_address(pfn_to_page(dst_pfn
)),
983 pfn_to_page(src_pfn
));
985 #endif /* CONFIG_HIGHMEM */
988 copy_data_pages(struct memory_bitmap
*copy_bm
, struct memory_bitmap
*orig_bm
)
993 for_each_zone(zone
) {
994 unsigned long max_zone_pfn
;
996 mark_free_pages(zone
);
997 max_zone_pfn
= zone
->zone_start_pfn
+ zone
->spanned_pages
;
998 for (pfn
= zone
->zone_start_pfn
; pfn
< max_zone_pfn
; pfn
++)
999 if (page_is_saveable(zone
, pfn
))
1000 memory_bm_set_bit(orig_bm
, pfn
);
1002 memory_bm_position_reset(orig_bm
);
1003 memory_bm_position_reset(copy_bm
);
1005 pfn
= memory_bm_next_pfn(orig_bm
);
1006 if (unlikely(pfn
== BM_END_OF_MAP
))
1008 copy_data_page(memory_bm_next_pfn(copy_bm
), pfn
);
1012 /* Total number of image pages */
1013 static unsigned int nr_copy_pages
;
1014 /* Number of pages needed for saving the original pfns of the image pages */
1015 static unsigned int nr_meta_pages
;
1018 * swsusp_free - free pages allocated for the suspend.
1020 * Suspend pages are alocated before the atomic copy is made, so we
1021 * need to release them after the resume.
1024 void swsusp_free(void)
1027 unsigned long pfn
, max_zone_pfn
;
1029 for_each_zone(zone
) {
1030 max_zone_pfn
= zone
->zone_start_pfn
+ zone
->spanned_pages
;
1031 for (pfn
= zone
->zone_start_pfn
; pfn
< max_zone_pfn
; pfn
++)
1032 if (pfn_valid(pfn
)) {
1033 struct page
*page
= pfn_to_page(pfn
);
1035 if (swsusp_page_is_forbidden(page
) &&
1036 swsusp_page_is_free(page
)) {
1037 swsusp_unset_page_forbidden(page
);
1038 swsusp_unset_page_free(page
);
1045 restore_pblist
= NULL
;
1049 #ifdef CONFIG_HIGHMEM
1051 * count_pages_for_highmem - compute the number of non-highmem pages
1052 * that will be necessary for creating copies of highmem pages.
1055 static unsigned int count_pages_for_highmem(unsigned int nr_highmem
)
1057 unsigned int free_highmem
= count_free_highmem_pages();
1059 if (free_highmem
>= nr_highmem
)
1062 nr_highmem
-= free_highmem
;
1068 count_pages_for_highmem(unsigned int nr_highmem
) { return 0; }
1069 #endif /* CONFIG_HIGHMEM */
1072 * enough_free_mem - Make sure we have enough free memory for the
1076 static int enough_free_mem(unsigned int nr_pages
, unsigned int nr_highmem
)
1079 unsigned int free
= 0, meta
= 0;
1081 for_each_zone(zone
) {
1082 meta
+= snapshot_additional_pages(zone
);
1083 if (!is_highmem(zone
))
1084 free
+= zone_page_state(zone
, NR_FREE_PAGES
);
1087 nr_pages
+= count_pages_for_highmem(nr_highmem
);
1088 pr_debug("PM: Normal pages needed: %u + %u + %u, available pages: %u\n",
1089 nr_pages
, PAGES_FOR_IO
, meta
, free
);
1091 return free
> nr_pages
+ PAGES_FOR_IO
+ meta
;
1094 #ifdef CONFIG_HIGHMEM
1096 * get_highmem_buffer - if there are some highmem pages in the suspend
1097 * image, we may need the buffer to copy them and/or load their data.
1100 static inline int get_highmem_buffer(int safe_needed
)
1102 buffer
= get_image_page(GFP_ATOMIC
| __GFP_COLD
, safe_needed
);
1103 return buffer
? 0 : -ENOMEM
;
1107 * alloc_highmem_image_pages - allocate some highmem pages for the image.
1108 * Try to allocate as many pages as needed, but if the number of free
1109 * highmem pages is lesser than that, allocate them all.
1112 static inline unsigned int
1113 alloc_highmem_image_pages(struct memory_bitmap
*bm
, unsigned int nr_highmem
)
1115 unsigned int to_alloc
= count_free_highmem_pages();
1117 if (to_alloc
> nr_highmem
)
1118 to_alloc
= nr_highmem
;
1120 nr_highmem
-= to_alloc
;
1121 while (to_alloc
-- > 0) {
1124 page
= alloc_image_page(__GFP_HIGHMEM
);
1125 memory_bm_set_bit(bm
, page_to_pfn(page
));
1130 static inline int get_highmem_buffer(int safe_needed
) { return 0; }
1132 static inline unsigned int
1133 alloc_highmem_image_pages(struct memory_bitmap
*bm
, unsigned int n
) { return 0; }
1134 #endif /* CONFIG_HIGHMEM */
1137 * swsusp_alloc - allocate memory for the suspend image
1139 * We first try to allocate as many highmem pages as there are
1140 * saveable highmem pages in the system. If that fails, we allocate
1141 * non-highmem pages for the copies of the remaining highmem ones.
1143 * In this approach it is likely that the copies of highmem pages will
1144 * also be located in the high memory, because of the way in which
1145 * copy_data_pages() works.
1149 swsusp_alloc(struct memory_bitmap
*orig_bm
, struct memory_bitmap
*copy_bm
,
1150 unsigned int nr_pages
, unsigned int nr_highmem
)
1154 error
= memory_bm_create(orig_bm
, GFP_ATOMIC
| __GFP_COLD
, PG_ANY
);
1158 error
= memory_bm_create(copy_bm
, GFP_ATOMIC
| __GFP_COLD
, PG_ANY
);
1162 if (nr_highmem
> 0) {
1163 error
= get_highmem_buffer(PG_ANY
);
1167 nr_pages
+= alloc_highmem_image_pages(copy_bm
, nr_highmem
);
1169 while (nr_pages
-- > 0) {
1170 struct page
*page
= alloc_image_page(GFP_ATOMIC
| __GFP_COLD
);
1175 memory_bm_set_bit(copy_bm
, page_to_pfn(page
));
1184 /* Memory bitmap used for marking saveable pages (during suspend) or the
1185 * suspend image pages (during resume)
1187 static struct memory_bitmap orig_bm
;
1188 /* Memory bitmap used on suspend for marking allocated pages that will contain
1189 * the copies of saveable pages. During resume it is initially used for
1190 * marking the suspend image pages, but then its set bits are duplicated in
1191 * @orig_bm and it is released. Next, on systems with high memory, it may be
1192 * used for marking "safe" highmem pages, but it has to be reinitialized for
1195 static struct memory_bitmap copy_bm
;
1197 asmlinkage
int swsusp_save(void)
1199 unsigned int nr_pages
, nr_highmem
;
1201 printk(KERN_INFO
"PM: Creating hibernation image: \n");
1203 drain_local_pages(NULL
);
1204 nr_pages
= count_data_pages();
1205 nr_highmem
= count_highmem_pages();
1206 printk(KERN_INFO
"PM: Need to copy %u pages\n", nr_pages
+ nr_highmem
);
1208 if (!enough_free_mem(nr_pages
, nr_highmem
)) {
1209 printk(KERN_ERR
"PM: Not enough free memory\n");
1213 if (swsusp_alloc(&orig_bm
, ©_bm
, nr_pages
, nr_highmem
)) {
1214 printk(KERN_ERR
"PM: Memory allocation failed\n");
1218 /* During allocating of suspend pagedir, new cold pages may appear.
1221 drain_local_pages(NULL
);
1222 copy_data_pages(©_bm
, &orig_bm
);
1225 * End of critical section. From now on, we can write to memory,
1226 * but we should not touch disk. This specially means we must _not_
1227 * touch swap space! Except we must write out our image of course.
1230 nr_pages
+= nr_highmem
;
1231 nr_copy_pages
= nr_pages
;
1232 nr_meta_pages
= DIV_ROUND_UP(nr_pages
* sizeof(long), PAGE_SIZE
);
1234 printk(KERN_INFO
"PM: Hibernation image created (%d pages copied)\n",
1240 #ifndef CONFIG_ARCH_HIBERNATION_HEADER
1241 static int init_header_complete(struct swsusp_info
*info
)
1243 memcpy(&info
->uts
, init_utsname(), sizeof(struct new_utsname
));
1244 info
->version_code
= LINUX_VERSION_CODE
;
1248 static char *check_image_kernel(struct swsusp_info
*info
)
1250 if (info
->version_code
!= LINUX_VERSION_CODE
)
1251 return "kernel version";
1252 if (strcmp(info
->uts
.sysname
,init_utsname()->sysname
))
1253 return "system type";
1254 if (strcmp(info
->uts
.release
,init_utsname()->release
))
1255 return "kernel release";
1256 if (strcmp(info
->uts
.version
,init_utsname()->version
))
1258 if (strcmp(info
->uts
.machine
,init_utsname()->machine
))
1262 #endif /* CONFIG_ARCH_HIBERNATION_HEADER */
1264 unsigned long snapshot_get_image_size(void)
1266 return nr_copy_pages
+ nr_meta_pages
+ 1;
1269 static int init_header(struct swsusp_info
*info
)
1271 memset(info
, 0, sizeof(struct swsusp_info
));
1272 info
->num_physpages
= num_physpages
;
1273 info
->image_pages
= nr_copy_pages
;
1274 info
->pages
= snapshot_get_image_size();
1275 info
->size
= info
->pages
;
1276 info
->size
<<= PAGE_SHIFT
;
1277 return init_header_complete(info
);
1281 * pack_pfns - pfns corresponding to the set bits found in the bitmap @bm
1282 * are stored in the array @buf[] (1 page at a time)
1286 pack_pfns(unsigned long *buf
, struct memory_bitmap
*bm
)
1290 for (j
= 0; j
< PAGE_SIZE
/ sizeof(long); j
++) {
1291 buf
[j
] = memory_bm_next_pfn(bm
);
1292 if (unlikely(buf
[j
] == BM_END_OF_MAP
))
1298 * snapshot_read_next - used for reading the system memory snapshot.
1300 * On the first call to it @handle should point to a zeroed
1301 * snapshot_handle structure. The structure gets updated and a pointer
1302 * to it should be passed to this function every next time.
1304 * The @count parameter should contain the number of bytes the caller
1305 * wants to read from the snapshot. It must not be zero.
1307 * On success the function returns a positive number. Then, the caller
1308 * is allowed to read up to the returned number of bytes from the memory
1309 * location computed by the data_of() macro. The number returned
1310 * may be smaller than @count, but this only happens if the read would
1311 * cross a page boundary otherwise.
1313 * The function returns 0 to indicate the end of data stream condition,
1314 * and a negative number is returned on error. In such cases the
1315 * structure pointed to by @handle is not updated and should not be used
1319 int snapshot_read_next(struct snapshot_handle
*handle
, size_t count
)
1321 if (handle
->cur
> nr_meta_pages
+ nr_copy_pages
)
1325 /* This makes the buffer be freed by swsusp_free() */
1326 buffer
= get_image_page(GFP_ATOMIC
, PG_ANY
);
1330 if (!handle
->offset
) {
1333 error
= init_header((struct swsusp_info
*)buffer
);
1336 handle
->buffer
= buffer
;
1337 memory_bm_position_reset(&orig_bm
);
1338 memory_bm_position_reset(©_bm
);
1340 if (handle
->prev
< handle
->cur
) {
1341 if (handle
->cur
<= nr_meta_pages
) {
1342 memset(buffer
, 0, PAGE_SIZE
);
1343 pack_pfns(buffer
, &orig_bm
);
1347 page
= pfn_to_page(memory_bm_next_pfn(©_bm
));
1348 if (PageHighMem(page
)) {
1349 /* Highmem pages are copied to the buffer,
1350 * because we can't return with a kmapped
1351 * highmem page (we may not be called again).
1355 kaddr
= kmap_atomic(page
, KM_USER0
);
1356 memcpy(buffer
, kaddr
, PAGE_SIZE
);
1357 kunmap_atomic(kaddr
, KM_USER0
);
1358 handle
->buffer
= buffer
;
1360 handle
->buffer
= page_address(page
);
1363 handle
->prev
= handle
->cur
;
1365 handle
->buf_offset
= handle
->cur_offset
;
1366 if (handle
->cur_offset
+ count
>= PAGE_SIZE
) {
1367 count
= PAGE_SIZE
- handle
->cur_offset
;
1368 handle
->cur_offset
= 0;
1371 handle
->cur_offset
+= count
;
1373 handle
->offset
+= count
;
1378 * mark_unsafe_pages - mark the pages that cannot be used for storing
1379 * the image during resume, because they conflict with the pages that
1380 * had been used before suspend
1383 static int mark_unsafe_pages(struct memory_bitmap
*bm
)
1386 unsigned long pfn
, max_zone_pfn
;
1388 /* Clear page flags */
1389 for_each_zone(zone
) {
1390 max_zone_pfn
= zone
->zone_start_pfn
+ zone
->spanned_pages
;
1391 for (pfn
= zone
->zone_start_pfn
; pfn
< max_zone_pfn
; pfn
++)
1393 swsusp_unset_page_free(pfn_to_page(pfn
));
1396 /* Mark pages that correspond to the "original" pfns as "unsafe" */
1397 memory_bm_position_reset(bm
);
1399 pfn
= memory_bm_next_pfn(bm
);
1400 if (likely(pfn
!= BM_END_OF_MAP
)) {
1401 if (likely(pfn_valid(pfn
)))
1402 swsusp_set_page_free(pfn_to_page(pfn
));
1406 } while (pfn
!= BM_END_OF_MAP
);
1408 allocated_unsafe_pages
= 0;
1414 duplicate_memory_bitmap(struct memory_bitmap
*dst
, struct memory_bitmap
*src
)
1418 memory_bm_position_reset(src
);
1419 pfn
= memory_bm_next_pfn(src
);
1420 while (pfn
!= BM_END_OF_MAP
) {
1421 memory_bm_set_bit(dst
, pfn
);
1422 pfn
= memory_bm_next_pfn(src
);
1426 static int check_header(struct swsusp_info
*info
)
1430 reason
= check_image_kernel(info
);
1431 if (!reason
&& info
->num_physpages
!= num_physpages
)
1432 reason
= "memory size";
1434 printk(KERN_ERR
"PM: Image mismatch: %s\n", reason
);
1441 * load header - check the image header and copy data from it
1445 load_header(struct swsusp_info
*info
)
1449 restore_pblist
= NULL
;
1450 error
= check_header(info
);
1452 nr_copy_pages
= info
->image_pages
;
1453 nr_meta_pages
= info
->pages
- info
->image_pages
- 1;
1459 * unpack_orig_pfns - for each element of @buf[] (1 page at a time) set
1460 * the corresponding bit in the memory bitmap @bm
1464 unpack_orig_pfns(unsigned long *buf
, struct memory_bitmap
*bm
)
1468 for (j
= 0; j
< PAGE_SIZE
/ sizeof(long); j
++) {
1469 if (unlikely(buf
[j
] == BM_END_OF_MAP
))
1472 memory_bm_set_bit(bm
, buf
[j
]);
1476 /* List of "safe" pages that may be used to store data loaded from the suspend
1479 static struct linked_page
*safe_pages_list
;
1481 #ifdef CONFIG_HIGHMEM
1482 /* struct highmem_pbe is used for creating the list of highmem pages that
1483 * should be restored atomically during the resume from disk, because the page
1484 * frames they have occupied before the suspend are in use.
1486 struct highmem_pbe
{
1487 struct page
*copy_page
; /* data is here now */
1488 struct page
*orig_page
; /* data was here before the suspend */
1489 struct highmem_pbe
*next
;
1492 /* List of highmem PBEs needed for restoring the highmem pages that were
1493 * allocated before the suspend and included in the suspend image, but have
1494 * also been allocated by the "resume" kernel, so their contents cannot be
1495 * written directly to their "original" page frames.
1497 static struct highmem_pbe
*highmem_pblist
;
1500 * count_highmem_image_pages - compute the number of highmem pages in the
1501 * suspend image. The bits in the memory bitmap @bm that correspond to the
1502 * image pages are assumed to be set.
1505 static unsigned int count_highmem_image_pages(struct memory_bitmap
*bm
)
1508 unsigned int cnt
= 0;
1510 memory_bm_position_reset(bm
);
1511 pfn
= memory_bm_next_pfn(bm
);
1512 while (pfn
!= BM_END_OF_MAP
) {
1513 if (PageHighMem(pfn_to_page(pfn
)))
1516 pfn
= memory_bm_next_pfn(bm
);
1522 * prepare_highmem_image - try to allocate as many highmem pages as
1523 * there are highmem image pages (@nr_highmem_p points to the variable
1524 * containing the number of highmem image pages). The pages that are
1525 * "safe" (ie. will not be overwritten when the suspend image is
1526 * restored) have the corresponding bits set in @bm (it must be
1529 * NOTE: This function should not be called if there are no highmem
1533 static unsigned int safe_highmem_pages
;
1535 static struct memory_bitmap
*safe_highmem_bm
;
1538 prepare_highmem_image(struct memory_bitmap
*bm
, unsigned int *nr_highmem_p
)
1540 unsigned int to_alloc
;
1542 if (memory_bm_create(bm
, GFP_ATOMIC
, PG_SAFE
))
1545 if (get_highmem_buffer(PG_SAFE
))
1548 to_alloc
= count_free_highmem_pages();
1549 if (to_alloc
> *nr_highmem_p
)
1550 to_alloc
= *nr_highmem_p
;
1552 *nr_highmem_p
= to_alloc
;
1554 safe_highmem_pages
= 0;
1555 while (to_alloc
-- > 0) {
1558 page
= alloc_page(__GFP_HIGHMEM
);
1559 if (!swsusp_page_is_free(page
)) {
1560 /* The page is "safe", set its bit the bitmap */
1561 memory_bm_set_bit(bm
, page_to_pfn(page
));
1562 safe_highmem_pages
++;
1564 /* Mark the page as allocated */
1565 swsusp_set_page_forbidden(page
);
1566 swsusp_set_page_free(page
);
1568 memory_bm_position_reset(bm
);
1569 safe_highmem_bm
= bm
;
1574 * get_highmem_page_buffer - for given highmem image page find the buffer
1575 * that suspend_write_next() should set for its caller to write to.
1577 * If the page is to be saved to its "original" page frame or a copy of
1578 * the page is to be made in the highmem, @buffer is returned. Otherwise,
1579 * the copy of the page is to be made in normal memory, so the address of
1580 * the copy is returned.
1582 * If @buffer is returned, the caller of suspend_write_next() will write
1583 * the page's contents to @buffer, so they will have to be copied to the
1584 * right location on the next call to suspend_write_next() and it is done
1585 * with the help of copy_last_highmem_page(). For this purpose, if
1586 * @buffer is returned, @last_highmem page is set to the page to which
1587 * the data will have to be copied from @buffer.
1590 static struct page
*last_highmem_page
;
1593 get_highmem_page_buffer(struct page
*page
, struct chain_allocator
*ca
)
1595 struct highmem_pbe
*pbe
;
1598 if (swsusp_page_is_forbidden(page
) && swsusp_page_is_free(page
)) {
1599 /* We have allocated the "original" page frame and we can
1600 * use it directly to store the loaded page.
1602 last_highmem_page
= page
;
1605 /* The "original" page frame has not been allocated and we have to
1606 * use a "safe" page frame to store the loaded page.
1608 pbe
= chain_alloc(ca
, sizeof(struct highmem_pbe
));
1613 pbe
->orig_page
= page
;
1614 if (safe_highmem_pages
> 0) {
1617 /* Copy of the page will be stored in high memory */
1619 tmp
= pfn_to_page(memory_bm_next_pfn(safe_highmem_bm
));
1620 safe_highmem_pages
--;
1621 last_highmem_page
= tmp
;
1622 pbe
->copy_page
= tmp
;
1624 /* Copy of the page will be stored in normal memory */
1625 kaddr
= safe_pages_list
;
1626 safe_pages_list
= safe_pages_list
->next
;
1627 pbe
->copy_page
= virt_to_page(kaddr
);
1629 pbe
->next
= highmem_pblist
;
1630 highmem_pblist
= pbe
;
1635 * copy_last_highmem_page - copy the contents of a highmem image from
1636 * @buffer, where the caller of snapshot_write_next() has place them,
1637 * to the right location represented by @last_highmem_page .
1640 static void copy_last_highmem_page(void)
1642 if (last_highmem_page
) {
1645 dst
= kmap_atomic(last_highmem_page
, KM_USER0
);
1646 memcpy(dst
, buffer
, PAGE_SIZE
);
1647 kunmap_atomic(dst
, KM_USER0
);
1648 last_highmem_page
= NULL
;
1652 static inline int last_highmem_page_copied(void)
1654 return !last_highmem_page
;
1657 static inline void free_highmem_data(void)
1659 if (safe_highmem_bm
)
1660 memory_bm_free(safe_highmem_bm
, PG_UNSAFE_CLEAR
);
1663 free_image_page(buffer
, PG_UNSAFE_CLEAR
);
1666 static inline int get_safe_write_buffer(void) { return 0; }
1669 count_highmem_image_pages(struct memory_bitmap
*bm
) { return 0; }
1672 prepare_highmem_image(struct memory_bitmap
*bm
, unsigned int *nr_highmem_p
)
1677 static inline void *
1678 get_highmem_page_buffer(struct page
*page
, struct chain_allocator
*ca
)
1683 static inline void copy_last_highmem_page(void) {}
1684 static inline int last_highmem_page_copied(void) { return 1; }
1685 static inline void free_highmem_data(void) {}
1686 #endif /* CONFIG_HIGHMEM */
1689 * prepare_image - use the memory bitmap @bm to mark the pages that will
1690 * be overwritten in the process of restoring the system memory state
1691 * from the suspend image ("unsafe" pages) and allocate memory for the
1694 * The idea is to allocate a new memory bitmap first and then allocate
1695 * as many pages as needed for the image data, but not to assign these
1696 * pages to specific tasks initially. Instead, we just mark them as
1697 * allocated and create a lists of "safe" pages that will be used
1698 * later. On systems with high memory a list of "safe" highmem pages is
1702 #define PBES_PER_LINKED_PAGE (LINKED_PAGE_DATA_SIZE / sizeof(struct pbe))
1705 prepare_image(struct memory_bitmap
*new_bm
, struct memory_bitmap
*bm
)
1707 unsigned int nr_pages
, nr_highmem
;
1708 struct linked_page
*sp_list
, *lp
;
1711 /* If there is no highmem, the buffer will not be necessary */
1712 free_image_page(buffer
, PG_UNSAFE_CLEAR
);
1715 nr_highmem
= count_highmem_image_pages(bm
);
1716 error
= mark_unsafe_pages(bm
);
1720 error
= memory_bm_create(new_bm
, GFP_ATOMIC
, PG_SAFE
);
1724 duplicate_memory_bitmap(new_bm
, bm
);
1725 memory_bm_free(bm
, PG_UNSAFE_KEEP
);
1726 if (nr_highmem
> 0) {
1727 error
= prepare_highmem_image(bm
, &nr_highmem
);
1731 /* Reserve some safe pages for potential later use.
1733 * NOTE: This way we make sure there will be enough safe pages for the
1734 * chain_alloc() in get_buffer(). It is a bit wasteful, but
1735 * nr_copy_pages cannot be greater than 50% of the memory anyway.
1738 /* nr_copy_pages cannot be lesser than allocated_unsafe_pages */
1739 nr_pages
= nr_copy_pages
- nr_highmem
- allocated_unsafe_pages
;
1740 nr_pages
= DIV_ROUND_UP(nr_pages
, PBES_PER_LINKED_PAGE
);
1741 while (nr_pages
> 0) {
1742 lp
= get_image_page(GFP_ATOMIC
, PG_SAFE
);
1751 /* Preallocate memory for the image */
1752 safe_pages_list
= NULL
;
1753 nr_pages
= nr_copy_pages
- nr_highmem
- allocated_unsafe_pages
;
1754 while (nr_pages
> 0) {
1755 lp
= (struct linked_page
*)get_zeroed_page(GFP_ATOMIC
);
1760 if (!swsusp_page_is_free(virt_to_page(lp
))) {
1761 /* The page is "safe", add it to the list */
1762 lp
->next
= safe_pages_list
;
1763 safe_pages_list
= lp
;
1765 /* Mark the page as allocated */
1766 swsusp_set_page_forbidden(virt_to_page(lp
));
1767 swsusp_set_page_free(virt_to_page(lp
));
1770 /* Free the reserved safe pages so that chain_alloc() can use them */
1773 free_image_page(sp_list
, PG_UNSAFE_CLEAR
);
1784 * get_buffer - compute the address that snapshot_write_next() should
1785 * set for its caller to write to.
1788 static void *get_buffer(struct memory_bitmap
*bm
, struct chain_allocator
*ca
)
1791 struct page
*page
= pfn_to_page(memory_bm_next_pfn(bm
));
1793 if (PageHighMem(page
))
1794 return get_highmem_page_buffer(page
, ca
);
1796 if (swsusp_page_is_forbidden(page
) && swsusp_page_is_free(page
))
1797 /* We have allocated the "original" page frame and we can
1798 * use it directly to store the loaded page.
1800 return page_address(page
);
1802 /* The "original" page frame has not been allocated and we have to
1803 * use a "safe" page frame to store the loaded page.
1805 pbe
= chain_alloc(ca
, sizeof(struct pbe
));
1810 pbe
->orig_address
= page_address(page
);
1811 pbe
->address
= safe_pages_list
;
1812 safe_pages_list
= safe_pages_list
->next
;
1813 pbe
->next
= restore_pblist
;
1814 restore_pblist
= pbe
;
1815 return pbe
->address
;
1819 * snapshot_write_next - used for writing the system memory snapshot.
1821 * On the first call to it @handle should point to a zeroed
1822 * snapshot_handle structure. The structure gets updated and a pointer
1823 * to it should be passed to this function every next time.
1825 * The @count parameter should contain the number of bytes the caller
1826 * wants to write to the image. It must not be zero.
1828 * On success the function returns a positive number. Then, the caller
1829 * is allowed to write up to the returned number of bytes to the memory
1830 * location computed by the data_of() macro. The number returned
1831 * may be smaller than @count, but this only happens if the write would
1832 * cross a page boundary otherwise.
1834 * The function returns 0 to indicate the "end of file" condition,
1835 * and a negative number is returned on error. In such cases the
1836 * structure pointed to by @handle is not updated and should not be used
1840 int snapshot_write_next(struct snapshot_handle
*handle
, size_t count
)
1842 static struct chain_allocator ca
;
1845 /* Check if we have already loaded the entire image */
1846 if (handle
->prev
&& handle
->cur
> nr_meta_pages
+ nr_copy_pages
)
1849 if (handle
->offset
== 0) {
1851 /* This makes the buffer be freed by swsusp_free() */
1852 buffer
= get_image_page(GFP_ATOMIC
, PG_ANY
);
1857 handle
->buffer
= buffer
;
1859 handle
->sync_read
= 1;
1860 if (handle
->prev
< handle
->cur
) {
1861 if (handle
->prev
== 0) {
1862 error
= load_header(buffer
);
1866 error
= memory_bm_create(©_bm
, GFP_ATOMIC
, PG_ANY
);
1870 } else if (handle
->prev
<= nr_meta_pages
) {
1871 unpack_orig_pfns(buffer
, ©_bm
);
1872 if (handle
->prev
== nr_meta_pages
) {
1873 error
= prepare_image(&orig_bm
, ©_bm
);
1877 chain_init(&ca
, GFP_ATOMIC
, PG_SAFE
);
1878 memory_bm_position_reset(&orig_bm
);
1879 restore_pblist
= NULL
;
1880 handle
->buffer
= get_buffer(&orig_bm
, &ca
);
1881 handle
->sync_read
= 0;
1882 if (!handle
->buffer
)
1886 copy_last_highmem_page();
1887 handle
->buffer
= get_buffer(&orig_bm
, &ca
);
1888 if (handle
->buffer
!= buffer
)
1889 handle
->sync_read
= 0;
1891 handle
->prev
= handle
->cur
;
1893 handle
->buf_offset
= handle
->cur_offset
;
1894 if (handle
->cur_offset
+ count
>= PAGE_SIZE
) {
1895 count
= PAGE_SIZE
- handle
->cur_offset
;
1896 handle
->cur_offset
= 0;
1899 handle
->cur_offset
+= count
;
1901 handle
->offset
+= count
;
1906 * snapshot_write_finalize - must be called after the last call to
1907 * snapshot_write_next() in case the last page in the image happens
1908 * to be a highmem page and its contents should be stored in the
1909 * highmem. Additionally, it releases the memory that will not be
1913 void snapshot_write_finalize(struct snapshot_handle
*handle
)
1915 copy_last_highmem_page();
1916 /* Free only if we have loaded the image entirely */
1917 if (handle
->prev
&& handle
->cur
> nr_meta_pages
+ nr_copy_pages
) {
1918 memory_bm_free(&orig_bm
, PG_UNSAFE_CLEAR
);
1919 free_highmem_data();
1923 int snapshot_image_loaded(struct snapshot_handle
*handle
)
1925 return !(!nr_copy_pages
|| !last_highmem_page_copied() ||
1926 handle
->cur
<= nr_meta_pages
+ nr_copy_pages
);
1929 #ifdef CONFIG_HIGHMEM
1930 /* Assumes that @buf is ready and points to a "safe" page */
1932 swap_two_pages_data(struct page
*p1
, struct page
*p2
, void *buf
)
1934 void *kaddr1
, *kaddr2
;
1936 kaddr1
= kmap_atomic(p1
, KM_USER0
);
1937 kaddr2
= kmap_atomic(p2
, KM_USER1
);
1938 memcpy(buf
, kaddr1
, PAGE_SIZE
);
1939 memcpy(kaddr1
, kaddr2
, PAGE_SIZE
);
1940 memcpy(kaddr2
, buf
, PAGE_SIZE
);
1941 kunmap_atomic(kaddr1
, KM_USER0
);
1942 kunmap_atomic(kaddr2
, KM_USER1
);
1946 * restore_highmem - for each highmem page that was allocated before
1947 * the suspend and included in the suspend image, and also has been
1948 * allocated by the "resume" kernel swap its current (ie. "before
1949 * resume") contents with the previous (ie. "before suspend") one.
1951 * If the resume eventually fails, we can call this function once
1952 * again and restore the "before resume" highmem state.
1955 int restore_highmem(void)
1957 struct highmem_pbe
*pbe
= highmem_pblist
;
1963 buf
= get_image_page(GFP_ATOMIC
, PG_SAFE
);
1968 swap_two_pages_data(pbe
->copy_page
, pbe
->orig_page
, buf
);
1971 free_image_page(buf
, PG_UNSAFE_CLEAR
);
1974 #endif /* CONFIG_HIGHMEM */