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/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 /* List of PBEs needed for restoring the pages that were allocated before
38 * the suspend and included in the suspend image, but have also been
39 * allocated by the "resume" kernel, so their contents cannot be written
40 * directly to their "original" page frames.
42 struct pbe *restore_pblist;
44 /* Pointer to an auxiliary buffer (1 page) */
48 * @safe_needed - on resume, for storing the PBE list and the image,
49 * we can only use memory pages that do not conflict with the pages
50 * used before suspend. The unsafe pages have PageNosaveFree set
51 * and we count them using unsafe_pages.
53 * Each allocated image page is marked as PageNosave and PageNosaveFree
54 * so that swsusp_free() can release it.
59 #define PG_UNSAFE_CLEAR 1
60 #define PG_UNSAFE_KEEP 0
62 static unsigned int allocated_unsafe_pages;
64 static void *get_image_page(gfp_t gfp_mask, int safe_needed)
68 res = (void *)get_zeroed_page(gfp_mask);
70 while (res && PageNosaveFree(virt_to_page(res))) {
71 /* The page is unsafe, mark it for swsusp_free() */
72 SetPageNosave(virt_to_page(res));
73 allocated_unsafe_pages++;
74 res = (void *)get_zeroed_page(gfp_mask);
77 SetPageNosave(virt_to_page(res));
78 SetPageNosaveFree(virt_to_page(res));
83 unsigned long get_safe_page(gfp_t gfp_mask)
85 return (unsigned long)get_image_page(gfp_mask, PG_SAFE);
88 static struct page *alloc_image_page(gfp_t gfp_mask)
92 page = alloc_page(gfp_mask);
95 SetPageNosaveFree(page);
101 * free_image_page - free page represented by @addr, allocated with
102 * get_image_page (page flags set by it must be cleared)
105 static inline void free_image_page(void *addr, int clear_nosave_free)
109 BUG_ON(!virt_addr_valid(addr));
111 page = virt_to_page(addr);
113 ClearPageNosave(page);
114 if (clear_nosave_free)
115 ClearPageNosaveFree(page);
120 /* struct linked_page is used to build chains of pages */
122 #define LINKED_PAGE_DATA_SIZE (PAGE_SIZE - sizeof(void *))
125 struct linked_page *next;
126 char data[LINKED_PAGE_DATA_SIZE];
127 } __attribute__((packed));
130 free_list_of_pages(struct linked_page *list, int clear_page_nosave)
133 struct linked_page *lp = list->next;
135 free_image_page(list, clear_page_nosave);
141 * struct chain_allocator is used for allocating small objects out of
142 * a linked list of pages called 'the chain'.
144 * The chain grows each time when there is no room for a new object in
145 * the current page. The allocated objects cannot be freed individually.
146 * It is only possible to free them all at once, by freeing the entire
149 * NOTE: The chain allocator may be inefficient if the allocated objects
150 * are not much smaller than PAGE_SIZE.
153 struct chain_allocator {
154 struct linked_page *chain; /* the chain */
155 unsigned int used_space; /* total size of objects allocated out
156 * of the current page
158 gfp_t gfp_mask; /* mask for allocating pages */
159 int safe_needed; /* if set, only "safe" pages are allocated */
163 chain_init(struct chain_allocator *ca, gfp_t gfp_mask, int safe_needed)
166 ca->used_space = LINKED_PAGE_DATA_SIZE;
167 ca->gfp_mask = gfp_mask;
168 ca->safe_needed = safe_needed;
171 static void *chain_alloc(struct chain_allocator *ca, unsigned int size)
175 if (LINKED_PAGE_DATA_SIZE - ca->used_space < size) {
176 struct linked_page *lp;
178 lp = get_image_page(ca->gfp_mask, ca->safe_needed);
182 lp->next = ca->chain;
186 ret = ca->chain->data + ca->used_space;
187 ca->used_space += size;
191 static void chain_free(struct chain_allocator *ca, int clear_page_nosave)
193 free_list_of_pages(ca->chain, clear_page_nosave);
194 memset(ca, 0, sizeof(struct chain_allocator));
198 * Data types related to memory bitmaps.
200 * Memory bitmap is a structure consiting of many linked lists of
201 * objects. The main list's elements are of type struct zone_bitmap
202 * and each of them corresonds to one zone. For each zone bitmap
203 * object there is a list of objects of type struct bm_block that
204 * represent each blocks of bit chunks in which information is
207 * struct memory_bitmap contains a pointer to the main list of zone
208 * bitmap objects, a struct bm_position used for browsing the bitmap,
209 * and a pointer to the list of pages used for allocating all of the
210 * zone bitmap objects and bitmap block objects.
212 * NOTE: It has to be possible to lay out the bitmap in memory
213 * using only allocations of order 0. Additionally, the bitmap is
214 * designed to work with arbitrary number of zones (this is over the
215 * top for now, but let's avoid making unnecessary assumptions ;-).
217 * struct zone_bitmap contains a pointer to a list of bitmap block
218 * objects and a pointer to the bitmap block object that has been
219 * most recently used for setting bits. Additionally, it contains the
220 * pfns that correspond to the start and end of the represented zone.
222 * struct bm_block contains a pointer to the memory page in which
223 * information is stored (in the form of a block of bit chunks
224 * of type unsigned long each). It also contains the pfns that
225 * correspond to the start and end of the represented memory area and
226 * the number of bit chunks in the block.
228 * NOTE: Memory bitmaps are used for two types of operations only:
229 * "set a bit" and "find the next bit set". Moreover, the searching
230 * is always carried out after all of the "set a bit" operations
234 #define BM_END_OF_MAP (~0UL)
236 #define BM_CHUNKS_PER_BLOCK (PAGE_SIZE / sizeof(long))
237 #define BM_BITS_PER_CHUNK (sizeof(long) << 3)
238 #define BM_BITS_PER_BLOCK (PAGE_SIZE << 3)
241 struct bm_block *next; /* next element of the list */
242 unsigned long start_pfn; /* pfn represented by the first bit */
243 unsigned long end_pfn; /* pfn represented by the last bit plus 1 */
244 unsigned int size; /* number of bit chunks */
245 unsigned long *data; /* chunks of bits representing pages */
249 struct zone_bitmap *next; /* next element of the list */
250 unsigned long start_pfn; /* minimal pfn in this zone */
251 unsigned long end_pfn; /* maximal pfn in this zone plus 1 */
252 struct bm_block *bm_blocks; /* list of bitmap blocks */
253 struct bm_block *cur_block; /* recently used bitmap block */
256 /* strcut bm_position is used for browsing memory bitmaps */
259 struct zone_bitmap *zone_bm;
260 struct bm_block *block;
265 struct memory_bitmap {
266 struct zone_bitmap *zone_bm_list; /* list of zone bitmaps */
267 struct linked_page *p_list; /* list of pages used to store zone
268 * bitmap objects and bitmap block
271 struct bm_position cur; /* most recently used bit position */
274 /* Functions that operate on memory bitmaps */
276 static inline void memory_bm_reset_chunk(struct memory_bitmap *bm)
282 static void memory_bm_position_reset(struct memory_bitmap *bm)
284 struct zone_bitmap *zone_bm;
286 zone_bm = bm->zone_bm_list;
287 bm->cur.zone_bm = zone_bm;
288 bm->cur.block = zone_bm->bm_blocks;
289 memory_bm_reset_chunk(bm);
292 static void memory_bm_free(struct memory_bitmap *bm, int clear_nosave_free);
295 * create_bm_block_list - create a list of block bitmap objects
298 static inline struct bm_block *
299 create_bm_block_list(unsigned int nr_blocks, struct chain_allocator *ca)
301 struct bm_block *bblist = NULL;
303 while (nr_blocks-- > 0) {
306 bb = chain_alloc(ca, sizeof(struct bm_block));
317 * create_zone_bm_list - create a list of zone bitmap objects
320 static inline struct zone_bitmap *
321 create_zone_bm_list(unsigned int nr_zones, struct chain_allocator *ca)
323 struct zone_bitmap *zbmlist = NULL;
325 while (nr_zones-- > 0) {
326 struct zone_bitmap *zbm;
328 zbm = chain_alloc(ca, sizeof(struct zone_bitmap));
339 * memory_bm_create - allocate memory for a memory bitmap
343 memory_bm_create(struct memory_bitmap *bm, gfp_t gfp_mask, int safe_needed)
345 struct chain_allocator ca;
347 struct zone_bitmap *zone_bm;
351 chain_init(&ca, gfp_mask, safe_needed);
353 /* Compute the number of zones */
356 if (populated_zone(zone))
359 /* Allocate the list of zones bitmap objects */
360 zone_bm = create_zone_bm_list(nr, &ca);
361 bm->zone_bm_list = zone_bm;
363 chain_free(&ca, PG_UNSAFE_CLEAR);
367 /* Initialize the zone bitmap objects */
368 for_each_zone(zone) {
371 if (!populated_zone(zone))
374 zone_bm->start_pfn = zone->zone_start_pfn;
375 zone_bm->end_pfn = zone->zone_start_pfn + zone->spanned_pages;
376 /* Allocate the list of bitmap block objects */
377 nr = DIV_ROUND_UP(zone->spanned_pages, BM_BITS_PER_BLOCK);
378 bb = create_bm_block_list(nr, &ca);
379 zone_bm->bm_blocks = bb;
380 zone_bm->cur_block = bb;
384 nr = zone->spanned_pages;
385 pfn = zone->zone_start_pfn;
386 /* Initialize the bitmap block objects */
390 ptr = get_image_page(gfp_mask, safe_needed);
396 if (nr >= BM_BITS_PER_BLOCK) {
397 pfn += BM_BITS_PER_BLOCK;
398 bb->size = BM_CHUNKS_PER_BLOCK;
399 nr -= BM_BITS_PER_BLOCK;
401 /* This is executed only once in the loop */
403 bb->size = DIV_ROUND_UP(nr, BM_BITS_PER_CHUNK);
408 zone_bm = zone_bm->next;
410 bm->p_list = ca.chain;
411 memory_bm_position_reset(bm);
415 bm->p_list = ca.chain;
416 memory_bm_free(bm, PG_UNSAFE_CLEAR);
421 * memory_bm_free - free memory occupied by the memory bitmap @bm
424 static void memory_bm_free(struct memory_bitmap *bm, int clear_nosave_free)
426 struct zone_bitmap *zone_bm;
428 /* Free the list of bit blocks for each zone_bitmap object */
429 zone_bm = bm->zone_bm_list;
433 bb = zone_bm->bm_blocks;
436 free_image_page(bb->data, clear_nosave_free);
439 zone_bm = zone_bm->next;
441 free_list_of_pages(bm->p_list, clear_nosave_free);
442 bm->zone_bm_list = NULL;
446 * memory_bm_set_bit - set the bit in the bitmap @bm that corresponds
447 * to given pfn. The cur_zone_bm member of @bm and the cur_block member
448 * of @bm->cur_zone_bm are updated.
450 * If the bit cannot be set, the function returns -EINVAL .
454 memory_bm_set_bit(struct memory_bitmap *bm, unsigned long pfn)
456 struct zone_bitmap *zone_bm;
459 /* Check if the pfn is from the current zone */
460 zone_bm = bm->cur.zone_bm;
461 if (pfn < zone_bm->start_pfn || pfn >= zone_bm->end_pfn) {
462 zone_bm = bm->zone_bm_list;
463 /* We don't assume that the zones are sorted by pfns */
464 while (pfn < zone_bm->start_pfn || pfn >= zone_bm->end_pfn) {
465 zone_bm = zone_bm->next;
466 if (unlikely(!zone_bm))
469 bm->cur.zone_bm = zone_bm;
471 /* Check if the pfn corresponds to the current bitmap block */
472 bb = zone_bm->cur_block;
473 if (pfn < bb->start_pfn)
474 bb = zone_bm->bm_blocks;
476 while (pfn >= bb->end_pfn) {
481 zone_bm->cur_block = bb;
482 pfn -= bb->start_pfn;
483 set_bit(pfn % BM_BITS_PER_CHUNK, bb->data + pfn / BM_BITS_PER_CHUNK);
487 /* Two auxiliary functions for memory_bm_next_pfn */
489 /* Find the first set bit in the given chunk, if there is one */
491 static inline int next_bit_in_chunk(int bit, unsigned long *chunk_p)
494 while (bit < BM_BITS_PER_CHUNK) {
495 if (test_bit(bit, chunk_p))
503 /* Find a chunk containing some bits set in given block of bits */
505 static inline int next_chunk_in_block(int n, struct bm_block *bb)
508 while (n < bb->size) {
518 * memory_bm_next_pfn - find the pfn that corresponds to the next set bit
519 * in the bitmap @bm. If the pfn cannot be found, BM_END_OF_MAP is
522 * It is required to run memory_bm_position_reset() before the first call to
526 static unsigned long memory_bm_next_pfn(struct memory_bitmap *bm)
528 struct zone_bitmap *zone_bm;
536 chunk = bm->cur.chunk;
539 bit = next_bit_in_chunk(bit, bb->data + chunk);
543 chunk = next_chunk_in_block(chunk, bb);
545 } while (chunk >= 0);
548 memory_bm_reset_chunk(bm);
550 zone_bm = bm->cur.zone_bm->next;
552 bm->cur.zone_bm = zone_bm;
553 bm->cur.block = zone_bm->bm_blocks;
554 memory_bm_reset_chunk(bm);
557 memory_bm_position_reset(bm);
558 return BM_END_OF_MAP;
561 bm->cur.chunk = chunk;
563 return bb->start_pfn + chunk * BM_BITS_PER_CHUNK + bit;
567 * snapshot_additional_pages - estimate the number of additional pages
568 * be needed for setting up the suspend image data structures for given
569 * zone (usually the returned value is greater than the exact number)
572 unsigned int snapshot_additional_pages(struct zone *zone)
576 res = DIV_ROUND_UP(zone->spanned_pages, BM_BITS_PER_BLOCK);
577 res += DIV_ROUND_UP(res * sizeof(struct bm_block), PAGE_SIZE);
581 #ifdef CONFIG_HIGHMEM
583 * count_free_highmem_pages - compute the total number of free highmem
584 * pages, system-wide.
587 static unsigned int count_free_highmem_pages(void)
590 unsigned int cnt = 0;
593 if (populated_zone(zone) && is_highmem(zone))
594 cnt += zone_page_state(zone, NR_FREE_PAGES);
600 * saveable_highmem_page - Determine whether a highmem page should be
601 * included in the suspend image.
603 * We should save the page if it isn't Nosave or NosaveFree, or Reserved,
604 * and it isn't a part of a free chunk of pages.
607 static struct page *saveable_highmem_page(unsigned long pfn)
614 page = pfn_to_page(pfn);
616 BUG_ON(!PageHighMem(page));
618 if (PageNosave(page) || PageReserved(page) || PageNosaveFree(page))
625 * count_highmem_pages - compute the total number of saveable highmem
629 unsigned int count_highmem_pages(void)
634 for_each_zone(zone) {
635 unsigned long pfn, max_zone_pfn;
637 if (!is_highmem(zone))
640 mark_free_pages(zone);
641 max_zone_pfn = zone->zone_start_pfn + zone->spanned_pages;
642 for (pfn = zone->zone_start_pfn; pfn < max_zone_pfn; pfn++)
643 if (saveable_highmem_page(pfn))
649 static inline void *saveable_highmem_page(unsigned long pfn) { return NULL; }
650 static inline unsigned int count_highmem_pages(void) { return 0; }
651 #endif /* CONFIG_HIGHMEM */
654 * saveable - Determine whether a non-highmem page should be included in
657 * We should save the page if it isn't Nosave, and is not in the range
658 * of pages statically defined as 'unsaveable', and it isn't a part of
659 * a free chunk of pages.
662 static struct page *saveable_page(unsigned long pfn)
669 page = pfn_to_page(pfn);
671 BUG_ON(PageHighMem(page));
673 if (PageNosave(page) || PageNosaveFree(page))
676 if (PageReserved(page) && pfn_is_nosave(pfn))
683 * count_data_pages - compute the total number of saveable non-highmem
687 unsigned int count_data_pages(void)
690 unsigned long pfn, max_zone_pfn;
693 for_each_zone(zone) {
694 if (is_highmem(zone))
697 mark_free_pages(zone);
698 max_zone_pfn = zone->zone_start_pfn + zone->spanned_pages;
699 for (pfn = zone->zone_start_pfn; pfn < max_zone_pfn; pfn++)
700 if(saveable_page(pfn))
706 /* This is needed, because copy_page and memcpy are not usable for copying
709 static inline void do_copy_page(long *dst, long *src)
713 for (n = PAGE_SIZE / sizeof(long); n; n--)
717 #ifdef CONFIG_HIGHMEM
718 static inline struct page *
719 page_is_saveable(struct zone *zone, unsigned long pfn)
721 return is_highmem(zone) ?
722 saveable_highmem_page(pfn) : saveable_page(pfn);
726 copy_data_page(unsigned long dst_pfn, unsigned long src_pfn)
728 struct page *s_page, *d_page;
731 s_page = pfn_to_page(src_pfn);
732 d_page = pfn_to_page(dst_pfn);
733 if (PageHighMem(s_page)) {
734 src = kmap_atomic(s_page, KM_USER0);
735 dst = kmap_atomic(d_page, KM_USER1);
736 do_copy_page(dst, src);
737 kunmap_atomic(src, KM_USER0);
738 kunmap_atomic(dst, KM_USER1);
740 src = page_address(s_page);
741 if (PageHighMem(d_page)) {
742 /* Page pointed to by src may contain some kernel
743 * data modified by kmap_atomic()
745 do_copy_page(buffer, src);
746 dst = kmap_atomic(pfn_to_page(dst_pfn), KM_USER0);
747 memcpy(dst, buffer, PAGE_SIZE);
748 kunmap_atomic(dst, KM_USER0);
750 dst = page_address(d_page);
751 do_copy_page(dst, src);
756 #define page_is_saveable(zone, pfn) saveable_page(pfn)
759 copy_data_page(unsigned long dst_pfn, unsigned long src_pfn)
761 do_copy_page(page_address(pfn_to_page(dst_pfn)),
762 page_address(pfn_to_page(src_pfn)));
764 #endif /* CONFIG_HIGHMEM */
767 copy_data_pages(struct memory_bitmap *copy_bm, struct memory_bitmap *orig_bm)
772 for_each_zone(zone) {
773 unsigned long max_zone_pfn;
775 mark_free_pages(zone);
776 max_zone_pfn = zone->zone_start_pfn + zone->spanned_pages;
777 for (pfn = zone->zone_start_pfn; pfn < max_zone_pfn; pfn++)
778 if (page_is_saveable(zone, pfn))
779 memory_bm_set_bit(orig_bm, pfn);
781 memory_bm_position_reset(orig_bm);
782 memory_bm_position_reset(copy_bm);
784 pfn = memory_bm_next_pfn(orig_bm);
785 if (likely(pfn != BM_END_OF_MAP))
786 copy_data_page(memory_bm_next_pfn(copy_bm), pfn);
787 } while (pfn != BM_END_OF_MAP);
790 /* Total number of image pages */
791 static unsigned int nr_copy_pages;
792 /* Number of pages needed for saving the original pfns of the image pages */
793 static unsigned int nr_meta_pages;
796 * swsusp_free - free pages allocated for the suspend.
798 * Suspend pages are alocated before the atomic copy is made, so we
799 * need to release them after the resume.
802 void swsusp_free(void)
805 unsigned long pfn, max_zone_pfn;
807 for_each_zone(zone) {
808 max_zone_pfn = zone->zone_start_pfn + zone->spanned_pages;
809 for (pfn = zone->zone_start_pfn; pfn < max_zone_pfn; pfn++)
810 if (pfn_valid(pfn)) {
811 struct page *page = pfn_to_page(pfn);
813 if (PageNosave(page) && PageNosaveFree(page)) {
814 ClearPageNosave(page);
815 ClearPageNosaveFree(page);
822 restore_pblist = NULL;
826 #ifdef CONFIG_HIGHMEM
828 * count_pages_for_highmem - compute the number of non-highmem pages
829 * that will be necessary for creating copies of highmem pages.
832 static unsigned int count_pages_for_highmem(unsigned int nr_highmem)
834 unsigned int free_highmem = count_free_highmem_pages();
836 if (free_highmem >= nr_highmem)
839 nr_highmem -= free_highmem;
845 count_pages_for_highmem(unsigned int nr_highmem) { return 0; }
846 #endif /* CONFIG_HIGHMEM */
849 * enough_free_mem - Make sure we have enough free memory for the
853 static int enough_free_mem(unsigned int nr_pages, unsigned int nr_highmem)
856 unsigned int free = 0, meta = 0;
858 for_each_zone(zone) {
859 meta += snapshot_additional_pages(zone);
860 if (!is_highmem(zone))
861 free += zone_page_state(zone, NR_FREE_PAGES);
864 nr_pages += count_pages_for_highmem(nr_highmem);
865 pr_debug("swsusp: Normal pages needed: %u + %u + %u, available pages: %u\n",
866 nr_pages, PAGES_FOR_IO, meta, free);
868 return free > nr_pages + PAGES_FOR_IO + meta;
871 #ifdef CONFIG_HIGHMEM
873 * get_highmem_buffer - if there are some highmem pages in the suspend
874 * image, we may need the buffer to copy them and/or load their data.
877 static inline int get_highmem_buffer(int safe_needed)
879 buffer = get_image_page(GFP_ATOMIC | __GFP_COLD, safe_needed);
880 return buffer ? 0 : -ENOMEM;
884 * alloc_highmem_image_pages - allocate some highmem pages for the image.
885 * Try to allocate as many pages as needed, but if the number of free
886 * highmem pages is lesser than that, allocate them all.
889 static inline unsigned int
890 alloc_highmem_image_pages(struct memory_bitmap *bm, unsigned int nr_highmem)
892 unsigned int to_alloc = count_free_highmem_pages();
894 if (to_alloc > nr_highmem)
895 to_alloc = nr_highmem;
897 nr_highmem -= to_alloc;
898 while (to_alloc-- > 0) {
901 page = alloc_image_page(__GFP_HIGHMEM);
902 memory_bm_set_bit(bm, page_to_pfn(page));
907 static inline int get_highmem_buffer(int safe_needed) { return 0; }
909 static inline unsigned int
910 alloc_highmem_image_pages(struct memory_bitmap *bm, unsigned int n) { return 0; }
911 #endif /* CONFIG_HIGHMEM */
914 * swsusp_alloc - allocate memory for the suspend image
916 * We first try to allocate as many highmem pages as there are
917 * saveable highmem pages in the system. If that fails, we allocate
918 * non-highmem pages for the copies of the remaining highmem ones.
920 * In this approach it is likely that the copies of highmem pages will
921 * also be located in the high memory, because of the way in which
922 * copy_data_pages() works.
926 swsusp_alloc(struct memory_bitmap *orig_bm, struct memory_bitmap *copy_bm,
927 unsigned int nr_pages, unsigned int nr_highmem)
931 error = memory_bm_create(orig_bm, GFP_ATOMIC | __GFP_COLD, PG_ANY);
935 error = memory_bm_create(copy_bm, GFP_ATOMIC | __GFP_COLD, PG_ANY);
939 if (nr_highmem > 0) {
940 error = get_highmem_buffer(PG_ANY);
944 nr_pages += alloc_highmem_image_pages(copy_bm, nr_highmem);
946 while (nr_pages-- > 0) {
947 struct page *page = alloc_image_page(GFP_ATOMIC | __GFP_COLD);
952 memory_bm_set_bit(copy_bm, page_to_pfn(page));
961 /* Memory bitmap used for marking saveable pages (during suspend) or the
962 * suspend image pages (during resume)
964 static struct memory_bitmap orig_bm;
965 /* Memory bitmap used on suspend for marking allocated pages that will contain
966 * the copies of saveable pages. During resume it is initially used for
967 * marking the suspend image pages, but then its set bits are duplicated in
968 * @orig_bm and it is released. Next, on systems with high memory, it may be
969 * used for marking "safe" highmem pages, but it has to be reinitialized for
972 static struct memory_bitmap copy_bm;
974 asmlinkage int swsusp_save(void)
976 unsigned int nr_pages, nr_highmem;
978 printk("swsusp: critical section: \n");
981 nr_pages = count_data_pages();
982 nr_highmem = count_highmem_pages();
983 printk("swsusp: Need to copy %u pages\n", nr_pages + nr_highmem);
985 if (!enough_free_mem(nr_pages, nr_highmem)) {
986 printk(KERN_ERR "swsusp: Not enough free memory\n");
990 if (swsusp_alloc(&orig_bm, ©_bm, nr_pages, nr_highmem)) {
991 printk(KERN_ERR "swsusp: Memory allocation failed\n");
995 /* During allocating of suspend pagedir, new cold pages may appear.
999 copy_data_pages(©_bm, &orig_bm);
1002 * End of critical section. From now on, we can write to memory,
1003 * but we should not touch disk. This specially means we must _not_
1004 * touch swap space! Except we must write out our image of course.
1007 nr_pages += nr_highmem;
1008 nr_copy_pages = nr_pages;
1009 nr_meta_pages = DIV_ROUND_UP(nr_pages * sizeof(long), PAGE_SIZE);
1011 printk("swsusp: critical section/: done (%d pages copied)\n", nr_pages);
1016 static void init_header(struct swsusp_info *info)
1018 memset(info, 0, sizeof(struct swsusp_info));
1019 info->version_code = LINUX_VERSION_CODE;
1020 info->num_physpages = num_physpages;
1021 memcpy(&info->uts, init_utsname(), sizeof(struct new_utsname));
1022 info->cpus = num_online_cpus();
1023 info->image_pages = nr_copy_pages;
1024 info->pages = nr_copy_pages + nr_meta_pages + 1;
1025 info->size = info->pages;
1026 info->size <<= PAGE_SHIFT;
1030 * pack_pfns - pfns corresponding to the set bits found in the bitmap @bm
1031 * are stored in the array @buf[] (1 page at a time)
1035 pack_pfns(unsigned long *buf, struct memory_bitmap *bm)
1039 for (j = 0; j < PAGE_SIZE / sizeof(long); j++) {
1040 buf[j] = memory_bm_next_pfn(bm);
1041 if (unlikely(buf[j] == BM_END_OF_MAP))
1047 * snapshot_read_next - used for reading the system memory snapshot.
1049 * On the first call to it @handle should point to a zeroed
1050 * snapshot_handle structure. The structure gets updated and a pointer
1051 * to it should be passed to this function every next time.
1053 * The @count parameter should contain the number of bytes the caller
1054 * wants to read from the snapshot. It must not be zero.
1056 * On success the function returns a positive number. Then, the caller
1057 * is allowed to read up to the returned number of bytes from the memory
1058 * location computed by the data_of() macro. The number returned
1059 * may be smaller than @count, but this only happens if the read would
1060 * cross a page boundary otherwise.
1062 * The function returns 0 to indicate the end of data stream condition,
1063 * and a negative number is returned on error. In such cases the
1064 * structure pointed to by @handle is not updated and should not be used
1068 int snapshot_read_next(struct snapshot_handle *handle, size_t count)
1070 if (handle->cur > nr_meta_pages + nr_copy_pages)
1074 /* This makes the buffer be freed by swsusp_free() */
1075 buffer = get_image_page(GFP_ATOMIC, PG_ANY);
1079 if (!handle->offset) {
1080 init_header((struct swsusp_info *)buffer);
1081 handle->buffer = buffer;
1082 memory_bm_position_reset(&orig_bm);
1083 memory_bm_position_reset(©_bm);
1085 if (handle->prev < handle->cur) {
1086 if (handle->cur <= nr_meta_pages) {
1087 memset(buffer, 0, PAGE_SIZE);
1088 pack_pfns(buffer, &orig_bm);
1092 page = pfn_to_page(memory_bm_next_pfn(©_bm));
1093 if (PageHighMem(page)) {
1094 /* Highmem pages are copied to the buffer,
1095 * because we can't return with a kmapped
1096 * highmem page (we may not be called again).
1100 kaddr = kmap_atomic(page, KM_USER0);
1101 memcpy(buffer, kaddr, PAGE_SIZE);
1102 kunmap_atomic(kaddr, KM_USER0);
1103 handle->buffer = buffer;
1105 handle->buffer = page_address(page);
1108 handle->prev = handle->cur;
1110 handle->buf_offset = handle->cur_offset;
1111 if (handle->cur_offset + count >= PAGE_SIZE) {
1112 count = PAGE_SIZE - handle->cur_offset;
1113 handle->cur_offset = 0;
1116 handle->cur_offset += count;
1118 handle->offset += count;
1123 * mark_unsafe_pages - mark the pages that cannot be used for storing
1124 * the image during resume, because they conflict with the pages that
1125 * had been used before suspend
1128 static int mark_unsafe_pages(struct memory_bitmap *bm)
1131 unsigned long pfn, max_zone_pfn;
1133 /* Clear page flags */
1134 for_each_zone(zone) {
1135 max_zone_pfn = zone->zone_start_pfn + zone->spanned_pages;
1136 for (pfn = zone->zone_start_pfn; pfn < max_zone_pfn; pfn++)
1138 ClearPageNosaveFree(pfn_to_page(pfn));
1141 /* Mark pages that correspond to the "original" pfns as "unsafe" */
1142 memory_bm_position_reset(bm);
1144 pfn = memory_bm_next_pfn(bm);
1145 if (likely(pfn != BM_END_OF_MAP)) {
1146 if (likely(pfn_valid(pfn)))
1147 SetPageNosaveFree(pfn_to_page(pfn));
1151 } while (pfn != BM_END_OF_MAP);
1153 allocated_unsafe_pages = 0;
1159 duplicate_memory_bitmap(struct memory_bitmap *dst, struct memory_bitmap *src)
1163 memory_bm_position_reset(src);
1164 pfn = memory_bm_next_pfn(src);
1165 while (pfn != BM_END_OF_MAP) {
1166 memory_bm_set_bit(dst, pfn);
1167 pfn = memory_bm_next_pfn(src);
1171 static inline int check_header(struct swsusp_info *info)
1173 char *reason = NULL;
1175 if (info->version_code != LINUX_VERSION_CODE)
1176 reason = "kernel version";
1177 if (info->num_physpages != num_physpages)
1178 reason = "memory size";
1179 if (strcmp(info->uts.sysname,init_utsname()->sysname))
1180 reason = "system type";
1181 if (strcmp(info->uts.release,init_utsname()->release))
1182 reason = "kernel release";
1183 if (strcmp(info->uts.version,init_utsname()->version))
1185 if (strcmp(info->uts.machine,init_utsname()->machine))
1188 printk(KERN_ERR "swsusp: Resume mismatch: %s\n", reason);
1195 * load header - check the image header and copy data from it
1199 load_header(struct swsusp_info *info)
1203 restore_pblist = NULL;
1204 error = check_header(info);
1206 nr_copy_pages = info->image_pages;
1207 nr_meta_pages = info->pages - info->image_pages - 1;
1213 * unpack_orig_pfns - for each element of @buf[] (1 page at a time) set
1214 * the corresponding bit in the memory bitmap @bm
1218 unpack_orig_pfns(unsigned long *buf, struct memory_bitmap *bm)
1222 for (j = 0; j < PAGE_SIZE / sizeof(long); j++) {
1223 if (unlikely(buf[j] == BM_END_OF_MAP))
1226 memory_bm_set_bit(bm, buf[j]);
1230 /* List of "safe" pages that may be used to store data loaded from the suspend
1233 static struct linked_page *safe_pages_list;
1235 #ifdef CONFIG_HIGHMEM
1236 /* struct highmem_pbe is used for creating the list of highmem pages that
1237 * should be restored atomically during the resume from disk, because the page
1238 * frames they have occupied before the suspend are in use.
1240 struct highmem_pbe {
1241 struct page *copy_page; /* data is here now */
1242 struct page *orig_page; /* data was here before the suspend */
1243 struct highmem_pbe *next;
1246 /* List of highmem PBEs needed for restoring the highmem pages that were
1247 * allocated before the suspend and included in the suspend image, but have
1248 * also been allocated by the "resume" kernel, so their contents cannot be
1249 * written directly to their "original" page frames.
1251 static struct highmem_pbe *highmem_pblist;
1254 * count_highmem_image_pages - compute the number of highmem pages in the
1255 * suspend image. The bits in the memory bitmap @bm that correspond to the
1256 * image pages are assumed to be set.
1259 static unsigned int count_highmem_image_pages(struct memory_bitmap *bm)
1262 unsigned int cnt = 0;
1264 memory_bm_position_reset(bm);
1265 pfn = memory_bm_next_pfn(bm);
1266 while (pfn != BM_END_OF_MAP) {
1267 if (PageHighMem(pfn_to_page(pfn)))
1270 pfn = memory_bm_next_pfn(bm);
1276 * prepare_highmem_image - try to allocate as many highmem pages as
1277 * there are highmem image pages (@nr_highmem_p points to the variable
1278 * containing the number of highmem image pages). The pages that are
1279 * "safe" (ie. will not be overwritten when the suspend image is
1280 * restored) have the corresponding bits set in @bm (it must be
1283 * NOTE: This function should not be called if there are no highmem
1287 static unsigned int safe_highmem_pages;
1289 static struct memory_bitmap *safe_highmem_bm;
1292 prepare_highmem_image(struct memory_bitmap *bm, unsigned int *nr_highmem_p)
1294 unsigned int to_alloc;
1296 if (memory_bm_create(bm, GFP_ATOMIC, PG_SAFE))
1299 if (get_highmem_buffer(PG_SAFE))
1302 to_alloc = count_free_highmem_pages();
1303 if (to_alloc > *nr_highmem_p)
1304 to_alloc = *nr_highmem_p;
1306 *nr_highmem_p = to_alloc;
1308 safe_highmem_pages = 0;
1309 while (to_alloc-- > 0) {
1312 page = alloc_page(__GFP_HIGHMEM);
1313 if (!PageNosaveFree(page)) {
1314 /* The page is "safe", set its bit the bitmap */
1315 memory_bm_set_bit(bm, page_to_pfn(page));
1316 safe_highmem_pages++;
1318 /* Mark the page as allocated */
1319 SetPageNosave(page);
1320 SetPageNosaveFree(page);
1322 memory_bm_position_reset(bm);
1323 safe_highmem_bm = bm;
1328 * get_highmem_page_buffer - for given highmem image page find the buffer
1329 * that suspend_write_next() should set for its caller to write to.
1331 * If the page is to be saved to its "original" page frame or a copy of
1332 * the page is to be made in the highmem, @buffer is returned. Otherwise,
1333 * the copy of the page is to be made in normal memory, so the address of
1334 * the copy is returned.
1336 * If @buffer is returned, the caller of suspend_write_next() will write
1337 * the page's contents to @buffer, so they will have to be copied to the
1338 * right location on the next call to suspend_write_next() and it is done
1339 * with the help of copy_last_highmem_page(). For this purpose, if
1340 * @buffer is returned, @last_highmem page is set to the page to which
1341 * the data will have to be copied from @buffer.
1344 static struct page *last_highmem_page;
1347 get_highmem_page_buffer(struct page *page, struct chain_allocator *ca)
1349 struct highmem_pbe *pbe;
1352 if (PageNosave(page) && PageNosaveFree(page)) {
1353 /* We have allocated the "original" page frame and we can
1354 * use it directly to store the loaded page.
1356 last_highmem_page = page;
1359 /* The "original" page frame has not been allocated and we have to
1360 * use a "safe" page frame to store the loaded page.
1362 pbe = chain_alloc(ca, sizeof(struct highmem_pbe));
1367 pbe->orig_page = page;
1368 if (safe_highmem_pages > 0) {
1371 /* Copy of the page will be stored in high memory */
1373 tmp = pfn_to_page(memory_bm_next_pfn(safe_highmem_bm));
1374 safe_highmem_pages--;
1375 last_highmem_page = tmp;
1376 pbe->copy_page = tmp;
1378 /* Copy of the page will be stored in normal memory */
1379 kaddr = safe_pages_list;
1380 safe_pages_list = safe_pages_list->next;
1381 pbe->copy_page = virt_to_page(kaddr);
1383 pbe->next = highmem_pblist;
1384 highmem_pblist = pbe;
1389 * copy_last_highmem_page - copy the contents of a highmem image from
1390 * @buffer, where the caller of snapshot_write_next() has place them,
1391 * to the right location represented by @last_highmem_page .
1394 static void copy_last_highmem_page(void)
1396 if (last_highmem_page) {
1399 dst = kmap_atomic(last_highmem_page, KM_USER0);
1400 memcpy(dst, buffer, PAGE_SIZE);
1401 kunmap_atomic(dst, KM_USER0);
1402 last_highmem_page = NULL;
1406 static inline int last_highmem_page_copied(void)
1408 return !last_highmem_page;
1411 static inline void free_highmem_data(void)
1413 if (safe_highmem_bm)
1414 memory_bm_free(safe_highmem_bm, PG_UNSAFE_CLEAR);
1417 free_image_page(buffer, PG_UNSAFE_CLEAR);
1420 static inline int get_safe_write_buffer(void) { return 0; }
1423 count_highmem_image_pages(struct memory_bitmap *bm) { return 0; }
1426 prepare_highmem_image(struct memory_bitmap *bm, unsigned int *nr_highmem_p)
1431 static inline void *
1432 get_highmem_page_buffer(struct page *page, struct chain_allocator *ca)
1437 static inline void copy_last_highmem_page(void) {}
1438 static inline int last_highmem_page_copied(void) { return 1; }
1439 static inline void free_highmem_data(void) {}
1440 #endif /* CONFIG_HIGHMEM */
1443 * prepare_image - use the memory bitmap @bm to mark the pages that will
1444 * be overwritten in the process of restoring the system memory state
1445 * from the suspend image ("unsafe" pages) and allocate memory for the
1448 * The idea is to allocate a new memory bitmap first and then allocate
1449 * as many pages as needed for the image data, but not to assign these
1450 * pages to specific tasks initially. Instead, we just mark them as
1451 * allocated and create a lists of "safe" pages that will be used
1452 * later. On systems with high memory a list of "safe" highmem pages is
1456 #define PBES_PER_LINKED_PAGE (LINKED_PAGE_DATA_SIZE / sizeof(struct pbe))
1459 prepare_image(struct memory_bitmap *new_bm, struct memory_bitmap *bm)
1461 unsigned int nr_pages, nr_highmem;
1462 struct linked_page *sp_list, *lp;
1465 /* If there is no highmem, the buffer will not be necessary */
1466 free_image_page(buffer, PG_UNSAFE_CLEAR);
1469 nr_highmem = count_highmem_image_pages(bm);
1470 error = mark_unsafe_pages(bm);
1474 error = memory_bm_create(new_bm, GFP_ATOMIC, PG_SAFE);
1478 duplicate_memory_bitmap(new_bm, bm);
1479 memory_bm_free(bm, PG_UNSAFE_KEEP);
1480 if (nr_highmem > 0) {
1481 error = prepare_highmem_image(bm, &nr_highmem);
1485 /* Reserve some safe pages for potential later use.
1487 * NOTE: This way we make sure there will be enough safe pages for the
1488 * chain_alloc() in get_buffer(). It is a bit wasteful, but
1489 * nr_copy_pages cannot be greater than 50% of the memory anyway.
1492 /* nr_copy_pages cannot be lesser than allocated_unsafe_pages */
1493 nr_pages = nr_copy_pages - nr_highmem - allocated_unsafe_pages;
1494 nr_pages = DIV_ROUND_UP(nr_pages, PBES_PER_LINKED_PAGE);
1495 while (nr_pages > 0) {
1496 lp = get_image_page(GFP_ATOMIC, PG_SAFE);
1505 /* Preallocate memory for the image */
1506 safe_pages_list = NULL;
1507 nr_pages = nr_copy_pages - nr_highmem - allocated_unsafe_pages;
1508 while (nr_pages > 0) {
1509 lp = (struct linked_page *)get_zeroed_page(GFP_ATOMIC);
1514 if (!PageNosaveFree(virt_to_page(lp))) {
1515 /* The page is "safe", add it to the list */
1516 lp->next = safe_pages_list;
1517 safe_pages_list = lp;
1519 /* Mark the page as allocated */
1520 SetPageNosave(virt_to_page(lp));
1521 SetPageNosaveFree(virt_to_page(lp));
1524 /* Free the reserved safe pages so that chain_alloc() can use them */
1527 free_image_page(sp_list, PG_UNSAFE_CLEAR);
1538 * get_buffer - compute the address that snapshot_write_next() should
1539 * set for its caller to write to.
1542 static void *get_buffer(struct memory_bitmap *bm, struct chain_allocator *ca)
1545 struct page *page = pfn_to_page(memory_bm_next_pfn(bm));
1547 if (PageHighMem(page))
1548 return get_highmem_page_buffer(page, ca);
1550 if (PageNosave(page) && PageNosaveFree(page))
1551 /* We have allocated the "original" page frame and we can
1552 * use it directly to store the loaded page.
1554 return page_address(page);
1556 /* The "original" page frame has not been allocated and we have to
1557 * use a "safe" page frame to store the loaded page.
1559 pbe = chain_alloc(ca, sizeof(struct pbe));
1564 pbe->orig_address = page_address(page);
1565 pbe->address = safe_pages_list;
1566 safe_pages_list = safe_pages_list->next;
1567 pbe->next = restore_pblist;
1568 restore_pblist = pbe;
1569 return pbe->address;
1573 * snapshot_write_next - used for writing the system memory snapshot.
1575 * On the first call to it @handle should point to a zeroed
1576 * snapshot_handle structure. The structure gets updated and a pointer
1577 * to it should be passed to this function every next time.
1579 * The @count parameter should contain the number of bytes the caller
1580 * wants to write to the image. It must not be zero.
1582 * On success the function returns a positive number. Then, the caller
1583 * is allowed to write up to the returned number of bytes to the memory
1584 * location computed by the data_of() macro. The number returned
1585 * may be smaller than @count, but this only happens if the write would
1586 * cross a page boundary otherwise.
1588 * The function returns 0 to indicate the "end of file" condition,
1589 * and a negative number is returned on error. In such cases the
1590 * structure pointed to by @handle is not updated and should not be used
1594 int snapshot_write_next(struct snapshot_handle *handle, size_t count)
1596 static struct chain_allocator ca;
1599 /* Check if we have already loaded the entire image */
1600 if (handle->prev && handle->cur > nr_meta_pages + nr_copy_pages)
1603 if (handle->offset == 0) {
1605 /* This makes the buffer be freed by swsusp_free() */
1606 buffer = get_image_page(GFP_ATOMIC, PG_ANY);
1611 handle->buffer = buffer;
1613 handle->sync_read = 1;
1614 if (handle->prev < handle->cur) {
1615 if (handle->prev == 0) {
1616 error = load_header(buffer);
1620 error = memory_bm_create(©_bm, GFP_ATOMIC, PG_ANY);
1624 } else if (handle->prev <= nr_meta_pages) {
1625 unpack_orig_pfns(buffer, ©_bm);
1626 if (handle->prev == nr_meta_pages) {
1627 error = prepare_image(&orig_bm, ©_bm);
1631 chain_init(&ca, GFP_ATOMIC, PG_SAFE);
1632 memory_bm_position_reset(&orig_bm);
1633 restore_pblist = NULL;
1634 handle->buffer = get_buffer(&orig_bm, &ca);
1635 handle->sync_read = 0;
1636 if (!handle->buffer)
1640 copy_last_highmem_page();
1641 handle->buffer = get_buffer(&orig_bm, &ca);
1642 if (handle->buffer != buffer)
1643 handle->sync_read = 0;
1645 handle->prev = handle->cur;
1647 handle->buf_offset = handle->cur_offset;
1648 if (handle->cur_offset + count >= PAGE_SIZE) {
1649 count = PAGE_SIZE - handle->cur_offset;
1650 handle->cur_offset = 0;
1653 handle->cur_offset += count;
1655 handle->offset += count;
1660 * snapshot_write_finalize - must be called after the last call to
1661 * snapshot_write_next() in case the last page in the image happens
1662 * to be a highmem page and its contents should be stored in the
1663 * highmem. Additionally, it releases the memory that will not be
1667 void snapshot_write_finalize(struct snapshot_handle *handle)
1669 copy_last_highmem_page();
1670 /* Free only if we have loaded the image entirely */
1671 if (handle->prev && handle->cur > nr_meta_pages + nr_copy_pages) {
1672 memory_bm_free(&orig_bm, PG_UNSAFE_CLEAR);
1673 free_highmem_data();
1677 int snapshot_image_loaded(struct snapshot_handle *handle)
1679 return !(!nr_copy_pages || !last_highmem_page_copied() ||
1680 handle->cur <= nr_meta_pages + nr_copy_pages);
1683 #ifdef CONFIG_HIGHMEM
1684 /* Assumes that @buf is ready and points to a "safe" page */
1686 swap_two_pages_data(struct page *p1, struct page *p2, void *buf)
1688 void *kaddr1, *kaddr2;
1690 kaddr1 = kmap_atomic(p1, KM_USER0);
1691 kaddr2 = kmap_atomic(p2, KM_USER1);
1692 memcpy(buf, kaddr1, PAGE_SIZE);
1693 memcpy(kaddr1, kaddr2, PAGE_SIZE);
1694 memcpy(kaddr2, buf, PAGE_SIZE);
1695 kunmap_atomic(kaddr1, KM_USER0);
1696 kunmap_atomic(kaddr2, KM_USER1);
1700 * restore_highmem - for each highmem page that was allocated before
1701 * the suspend and included in the suspend image, and also has been
1702 * allocated by the "resume" kernel swap its current (ie. "before
1703 * resume") contents with the previous (ie. "before suspend") one.
1705 * If the resume eventually fails, we can call this function once
1706 * again and restore the "before resume" highmem state.
1709 int restore_highmem(void)
1711 struct highmem_pbe *pbe = highmem_pblist;
1717 buf = get_image_page(GFP_ATOMIC, PG_SAFE);
1722 swap_two_pages_data(pbe->copy_page, pbe->orig_page, buf);
1725 free_image_page(buf, PG_UNSAFE_CLEAR);
1728 #endif /* CONFIG_HIGHMEM */