2 * linux/kernel/power/snapshot.c
4 * This file provides system snapshot/restore functionality for swsusp.
6 * Copyright (C) 1998-2005 Pavel Machek <pavel@ucw.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>
28 #include <linux/list.h>
29 #include <linux/slab.h>
30 #include <linux/compiler.h>
31 #include <linux/ktime.h>
33 #include <asm/uaccess.h>
34 #include <asm/mmu_context.h>
35 #include <asm/pgtable.h>
36 #include <asm/tlbflush.h>
41 static int swsusp_page_is_free(struct page *);
42 static void swsusp_set_page_forbidden(struct page *);
43 static void swsusp_unset_page_forbidden(struct page *);
46 * Number of bytes to reserve for memory allocations made by device drivers
47 * from their ->freeze() and ->freeze_noirq() callbacks so that they don't
48 * cause image creation to fail (tunable via /sys/power/reserved_size).
50 unsigned long reserved_size;
52 void __init hibernate_reserved_size_init(void)
54 reserved_size = SPARE_PAGES * PAGE_SIZE;
58 * Preferred image size in bytes (tunable via /sys/power/image_size).
59 * When it is set to N, swsusp will do its best to ensure the image
60 * size will not exceed N bytes, but if that is impossible, it will
61 * try to create the smallest image possible.
63 unsigned long image_size;
65 void __init hibernate_image_size_init(void)
67 image_size = ((totalram_pages * 2) / 5) * PAGE_SIZE;
70 /* List of PBEs needed for restoring the pages that were allocated before
71 * the suspend and included in the suspend image, but have also been
72 * allocated by the "resume" kernel, so their contents cannot be written
73 * directly to their "original" page frames.
75 struct pbe *restore_pblist;
77 /* struct linked_page is used to build chains of pages */
79 #define LINKED_PAGE_DATA_SIZE (PAGE_SIZE - sizeof(void *))
82 struct linked_page *next;
83 char data[LINKED_PAGE_DATA_SIZE];
87 * List of "safe" pages (ie. pages that were not used by the image kernel
88 * before hibernation) that may be used as temporary storage for image kernel
91 static struct linked_page *safe_pages_list;
93 /* Pointer to an auxiliary buffer (1 page) */
97 * @safe_needed - on resume, for storing the PBE list and the image,
98 * we can only use memory pages that do not conflict with the pages
99 * used before suspend. The unsafe pages have PageNosaveFree set
100 * and we count them using unsafe_pages.
102 * Each allocated image page is marked as PageNosave and PageNosaveFree
103 * so that swsusp_free() can release it.
108 #define PG_UNSAFE_CLEAR 1
109 #define PG_UNSAFE_KEEP 0
111 static unsigned int allocated_unsafe_pages;
113 static void *get_image_page(gfp_t gfp_mask, int safe_needed)
117 res = (void *)get_zeroed_page(gfp_mask);
119 while (res && swsusp_page_is_free(virt_to_page(res))) {
120 /* The page is unsafe, mark it for swsusp_free() */
121 swsusp_set_page_forbidden(virt_to_page(res));
122 allocated_unsafe_pages++;
123 res = (void *)get_zeroed_page(gfp_mask);
126 swsusp_set_page_forbidden(virt_to_page(res));
127 swsusp_set_page_free(virt_to_page(res));
132 static void *__get_safe_page(gfp_t gfp_mask)
134 if (safe_pages_list) {
135 void *ret = safe_pages_list;
137 safe_pages_list = safe_pages_list->next;
138 memset(ret, 0, PAGE_SIZE);
141 return get_image_page(gfp_mask, PG_SAFE);
144 unsigned long get_safe_page(gfp_t gfp_mask)
146 return (unsigned long)__get_safe_page(gfp_mask);
149 static struct page *alloc_image_page(gfp_t gfp_mask)
153 page = alloc_page(gfp_mask);
155 swsusp_set_page_forbidden(page);
156 swsusp_set_page_free(page);
162 * free_image_page - free page represented by @addr, allocated with
163 * get_image_page (page flags set by it must be cleared)
166 static inline void free_image_page(void *addr, int clear_nosave_free)
170 BUG_ON(!virt_addr_valid(addr));
172 page = virt_to_page(addr);
174 swsusp_unset_page_forbidden(page);
175 if (clear_nosave_free)
176 swsusp_unset_page_free(page);
182 free_list_of_pages(struct linked_page *list, int clear_page_nosave)
185 struct linked_page *lp = list->next;
187 free_image_page(list, clear_page_nosave);
193 * struct chain_allocator is used for allocating small objects out of
194 * a linked list of pages called 'the chain'.
196 * The chain grows each time when there is no room for a new object in
197 * the current page. The allocated objects cannot be freed individually.
198 * It is only possible to free them all at once, by freeing the entire
201 * NOTE: The chain allocator may be inefficient if the allocated objects
202 * are not much smaller than PAGE_SIZE.
205 struct chain_allocator {
206 struct linked_page *chain; /* the chain */
207 unsigned int used_space; /* total size of objects allocated out
208 * of the current page
210 gfp_t gfp_mask; /* mask for allocating pages */
211 int safe_needed; /* if set, only "safe" pages are allocated */
215 chain_init(struct chain_allocator *ca, gfp_t gfp_mask, int safe_needed)
218 ca->used_space = LINKED_PAGE_DATA_SIZE;
219 ca->gfp_mask = gfp_mask;
220 ca->safe_needed = safe_needed;
223 static void *chain_alloc(struct chain_allocator *ca, unsigned int size)
227 if (LINKED_PAGE_DATA_SIZE - ca->used_space < size) {
228 struct linked_page *lp;
230 lp = ca->safe_needed ? __get_safe_page(ca->gfp_mask) :
231 get_image_page(ca->gfp_mask, PG_ANY);
235 lp->next = ca->chain;
239 ret = ca->chain->data + ca->used_space;
240 ca->used_space += size;
245 * Data types related to memory bitmaps.
247 * Memory bitmap is a structure consiting of many linked lists of
248 * objects. The main list's elements are of type struct zone_bitmap
249 * and each of them corresonds to one zone. For each zone bitmap
250 * object there is a list of objects of type struct bm_block that
251 * represent each blocks of bitmap in which information is stored.
253 * struct memory_bitmap contains a pointer to the main list of zone
254 * bitmap objects, a struct bm_position used for browsing the bitmap,
255 * and a pointer to the list of pages used for allocating all of the
256 * zone bitmap objects and bitmap block objects.
258 * NOTE: It has to be possible to lay out the bitmap in memory
259 * using only allocations of order 0. Additionally, the bitmap is
260 * designed to work with arbitrary number of zones (this is over the
261 * top for now, but let's avoid making unnecessary assumptions ;-).
263 * struct zone_bitmap contains a pointer to a list of bitmap block
264 * objects and a pointer to the bitmap block object that has been
265 * most recently used for setting bits. Additionally, it contains the
266 * pfns that correspond to the start and end of the represented zone.
268 * struct bm_block contains a pointer to the memory page in which
269 * information is stored (in the form of a block of bitmap)
270 * It also contains the pfns that correspond to the start and end of
271 * the represented memory area.
273 * The memory bitmap is organized as a radix tree to guarantee fast random
274 * access to the bits. There is one radix tree for each zone (as returned
275 * from create_mem_extents).
277 * One radix tree is represented by one struct mem_zone_bm_rtree. There are
278 * two linked lists for the nodes of the tree, one for the inner nodes and
279 * one for the leave nodes. The linked leave nodes are used for fast linear
280 * access of the memory bitmap.
282 * The struct rtree_node represents one node of the radix tree.
285 #define BM_END_OF_MAP (~0UL)
287 #define BM_BITS_PER_BLOCK (PAGE_SIZE * BITS_PER_BYTE)
288 #define BM_BLOCK_SHIFT (PAGE_SHIFT + 3)
289 #define BM_BLOCK_MASK ((1UL << BM_BLOCK_SHIFT) - 1)
292 * struct rtree_node is a wrapper struct to link the nodes
293 * of the rtree together for easy linear iteration over
294 * bits and easy freeing
297 struct list_head list;
302 * struct mem_zone_bm_rtree represents a bitmap used for one
303 * populated memory zone.
305 struct mem_zone_bm_rtree {
306 struct list_head list; /* Link Zones together */
307 struct list_head nodes; /* Radix Tree inner nodes */
308 struct list_head leaves; /* Radix Tree leaves */
309 unsigned long start_pfn; /* Zone start page frame */
310 unsigned long end_pfn; /* Zone end page frame + 1 */
311 struct rtree_node *rtree; /* Radix Tree Root */
312 int levels; /* Number of Radix Tree Levels */
313 unsigned int blocks; /* Number of Bitmap Blocks */
316 /* strcut bm_position is used for browsing memory bitmaps */
319 struct mem_zone_bm_rtree *zone;
320 struct rtree_node *node;
321 unsigned long node_pfn;
325 struct memory_bitmap {
326 struct list_head zones;
327 struct linked_page *p_list; /* list of pages used to store zone
328 * bitmap objects and bitmap block
331 struct bm_position cur; /* most recently used bit position */
334 /* Functions that operate on memory bitmaps */
336 #define BM_ENTRIES_PER_LEVEL (PAGE_SIZE / sizeof(unsigned long))
337 #if BITS_PER_LONG == 32
338 #define BM_RTREE_LEVEL_SHIFT (PAGE_SHIFT - 2)
340 #define BM_RTREE_LEVEL_SHIFT (PAGE_SHIFT - 3)
342 #define BM_RTREE_LEVEL_MASK ((1UL << BM_RTREE_LEVEL_SHIFT) - 1)
345 * alloc_rtree_node - Allocate a new node and add it to the radix tree.
347 * This function is used to allocate inner nodes as well as the
348 * leave nodes of the radix tree. It also adds the node to the
349 * corresponding linked list passed in by the *list parameter.
351 static struct rtree_node *alloc_rtree_node(gfp_t gfp_mask, int safe_needed,
352 struct chain_allocator *ca,
353 struct list_head *list)
355 struct rtree_node *node;
357 node = chain_alloc(ca, sizeof(struct rtree_node));
361 node->data = get_image_page(gfp_mask, safe_needed);
365 list_add_tail(&node->list, list);
371 * add_rtree_block - Add a new leave node to the radix tree
373 * The leave nodes need to be allocated in order to keep the leaves
374 * linked list in order. This is guaranteed by the zone->blocks
377 static int add_rtree_block(struct mem_zone_bm_rtree *zone, gfp_t gfp_mask,
378 int safe_needed, struct chain_allocator *ca)
380 struct rtree_node *node, *block, **dst;
381 unsigned int levels_needed, block_nr;
384 block_nr = zone->blocks;
387 /* How many levels do we need for this block nr? */
390 block_nr >>= BM_RTREE_LEVEL_SHIFT;
393 /* Make sure the rtree has enough levels */
394 for (i = zone->levels; i < levels_needed; i++) {
395 node = alloc_rtree_node(gfp_mask, safe_needed, ca,
400 node->data[0] = (unsigned long)zone->rtree;
405 /* Allocate new block */
406 block = alloc_rtree_node(gfp_mask, safe_needed, ca, &zone->leaves);
410 /* Now walk the rtree to insert the block */
413 block_nr = zone->blocks;
414 for (i = zone->levels; i > 0; i--) {
418 node = alloc_rtree_node(gfp_mask, safe_needed, ca,
425 index = block_nr >> ((i - 1) * BM_RTREE_LEVEL_SHIFT);
426 index &= BM_RTREE_LEVEL_MASK;
427 dst = (struct rtree_node **)&((*dst)->data[index]);
437 static void free_zone_bm_rtree(struct mem_zone_bm_rtree *zone,
438 int clear_nosave_free);
441 * create_zone_bm_rtree - create a radix tree for one zone
443 * Allocated the mem_zone_bm_rtree structure and initializes it.
444 * This function also allocated and builds the radix tree for the
447 static struct mem_zone_bm_rtree *
448 create_zone_bm_rtree(gfp_t gfp_mask, int safe_needed,
449 struct chain_allocator *ca,
450 unsigned long start, unsigned long end)
452 struct mem_zone_bm_rtree *zone;
453 unsigned int i, nr_blocks;
457 zone = chain_alloc(ca, sizeof(struct mem_zone_bm_rtree));
461 INIT_LIST_HEAD(&zone->nodes);
462 INIT_LIST_HEAD(&zone->leaves);
463 zone->start_pfn = start;
465 nr_blocks = DIV_ROUND_UP(pages, BM_BITS_PER_BLOCK);
467 for (i = 0; i < nr_blocks; i++) {
468 if (add_rtree_block(zone, gfp_mask, safe_needed, ca)) {
469 free_zone_bm_rtree(zone, PG_UNSAFE_CLEAR);
478 * free_zone_bm_rtree - Free the memory of the radix tree
480 * Free all node pages of the radix tree. The mem_zone_bm_rtree
481 * structure itself is not freed here nor are the rtree_node
484 static void free_zone_bm_rtree(struct mem_zone_bm_rtree *zone,
485 int clear_nosave_free)
487 struct rtree_node *node;
489 list_for_each_entry(node, &zone->nodes, list)
490 free_image_page(node->data, clear_nosave_free);
492 list_for_each_entry(node, &zone->leaves, list)
493 free_image_page(node->data, clear_nosave_free);
496 static void memory_bm_position_reset(struct memory_bitmap *bm)
498 bm->cur.zone = list_entry(bm->zones.next, struct mem_zone_bm_rtree,
500 bm->cur.node = list_entry(bm->cur.zone->leaves.next,
501 struct rtree_node, list);
502 bm->cur.node_pfn = 0;
503 bm->cur.node_bit = 0;
506 static void memory_bm_free(struct memory_bitmap *bm, int clear_nosave_free);
509 struct list_head hook;
515 * free_mem_extents - free a list of memory extents
516 * @list - list of extents to empty
518 static void free_mem_extents(struct list_head *list)
520 struct mem_extent *ext, *aux;
522 list_for_each_entry_safe(ext, aux, list, hook) {
523 list_del(&ext->hook);
529 * create_mem_extents - create a list of memory extents representing
530 * contiguous ranges of PFNs
531 * @list - list to put the extents into
532 * @gfp_mask - mask to use for memory allocations
534 static int create_mem_extents(struct list_head *list, gfp_t gfp_mask)
538 INIT_LIST_HEAD(list);
540 for_each_populated_zone(zone) {
541 unsigned long zone_start, zone_end;
542 struct mem_extent *ext, *cur, *aux;
544 zone_start = zone->zone_start_pfn;
545 zone_end = zone_end_pfn(zone);
547 list_for_each_entry(ext, list, hook)
548 if (zone_start <= ext->end)
551 if (&ext->hook == list || zone_end < ext->start) {
552 /* New extent is necessary */
553 struct mem_extent *new_ext;
555 new_ext = kzalloc(sizeof(struct mem_extent), gfp_mask);
557 free_mem_extents(list);
560 new_ext->start = zone_start;
561 new_ext->end = zone_end;
562 list_add_tail(&new_ext->hook, &ext->hook);
566 /* Merge this zone's range of PFNs with the existing one */
567 if (zone_start < ext->start)
568 ext->start = zone_start;
569 if (zone_end > ext->end)
572 /* More merging may be possible */
574 list_for_each_entry_safe_continue(cur, aux, list, hook) {
575 if (zone_end < cur->start)
577 if (zone_end < cur->end)
579 list_del(&cur->hook);
588 * memory_bm_create - allocate memory for a memory bitmap
591 memory_bm_create(struct memory_bitmap *bm, gfp_t gfp_mask, int safe_needed)
593 struct chain_allocator ca;
594 struct list_head mem_extents;
595 struct mem_extent *ext;
598 chain_init(&ca, gfp_mask, safe_needed);
599 INIT_LIST_HEAD(&bm->zones);
601 error = create_mem_extents(&mem_extents, gfp_mask);
605 list_for_each_entry(ext, &mem_extents, hook) {
606 struct mem_zone_bm_rtree *zone;
608 zone = create_zone_bm_rtree(gfp_mask, safe_needed, &ca,
609 ext->start, ext->end);
614 list_add_tail(&zone->list, &bm->zones);
617 bm->p_list = ca.chain;
618 memory_bm_position_reset(bm);
620 free_mem_extents(&mem_extents);
624 bm->p_list = ca.chain;
625 memory_bm_free(bm, PG_UNSAFE_CLEAR);
630 * memory_bm_free - free memory occupied by the memory bitmap @bm
632 static void memory_bm_free(struct memory_bitmap *bm, int clear_nosave_free)
634 struct mem_zone_bm_rtree *zone;
636 list_for_each_entry(zone, &bm->zones, list)
637 free_zone_bm_rtree(zone, clear_nosave_free);
639 free_list_of_pages(bm->p_list, clear_nosave_free);
641 INIT_LIST_HEAD(&bm->zones);
645 * memory_bm_find_bit - Find the bit for pfn in the memory
648 * Find the bit in the bitmap @bm that corresponds to given pfn.
649 * The cur.zone, cur.block and cur.node_pfn member of @bm are
651 * It walks the radix tree to find the page which contains the bit for
652 * pfn and returns the bit position in **addr and *bit_nr.
654 static int memory_bm_find_bit(struct memory_bitmap *bm, unsigned long pfn,
655 void **addr, unsigned int *bit_nr)
657 struct mem_zone_bm_rtree *curr, *zone;
658 struct rtree_node *node;
663 if (pfn >= zone->start_pfn && pfn < zone->end_pfn)
668 /* Find the right zone */
669 list_for_each_entry(curr, &bm->zones, list) {
670 if (pfn >= curr->start_pfn && pfn < curr->end_pfn) {
681 * We have a zone. Now walk the radix tree to find the leave
686 if (((pfn - zone->start_pfn) & ~BM_BLOCK_MASK) == bm->cur.node_pfn)
690 block_nr = (pfn - zone->start_pfn) >> BM_BLOCK_SHIFT;
692 for (i = zone->levels; i > 0; i--) {
695 index = block_nr >> ((i - 1) * BM_RTREE_LEVEL_SHIFT);
696 index &= BM_RTREE_LEVEL_MASK;
697 BUG_ON(node->data[index] == 0);
698 node = (struct rtree_node *)node->data[index];
702 /* Update last position */
705 bm->cur.node_pfn = (pfn - zone->start_pfn) & ~BM_BLOCK_MASK;
707 /* Set return values */
709 *bit_nr = (pfn - zone->start_pfn) & BM_BLOCK_MASK;
714 static void memory_bm_set_bit(struct memory_bitmap *bm, unsigned long pfn)
720 error = memory_bm_find_bit(bm, pfn, &addr, &bit);
725 static int mem_bm_set_bit_check(struct memory_bitmap *bm, unsigned long pfn)
731 error = memory_bm_find_bit(bm, pfn, &addr, &bit);
738 static void memory_bm_clear_bit(struct memory_bitmap *bm, unsigned long pfn)
744 error = memory_bm_find_bit(bm, pfn, &addr, &bit);
746 clear_bit(bit, addr);
749 static void memory_bm_clear_current(struct memory_bitmap *bm)
753 bit = max(bm->cur.node_bit - 1, 0);
754 clear_bit(bit, bm->cur.node->data);
757 static int memory_bm_test_bit(struct memory_bitmap *bm, unsigned long pfn)
763 error = memory_bm_find_bit(bm, pfn, &addr, &bit);
765 return test_bit(bit, addr);
768 static bool memory_bm_pfn_present(struct memory_bitmap *bm, unsigned long pfn)
773 return !memory_bm_find_bit(bm, pfn, &addr, &bit);
777 * rtree_next_node - Jumps to the next leave node
779 * Sets the position to the beginning of the next node in the
780 * memory bitmap. This is either the next node in the current
781 * zone's radix tree or the first node in the radix tree of the
784 * Returns true if there is a next node, false otherwise.
786 static bool rtree_next_node(struct memory_bitmap *bm)
788 bm->cur.node = list_entry(bm->cur.node->list.next,
789 struct rtree_node, list);
790 if (&bm->cur.node->list != &bm->cur.zone->leaves) {
791 bm->cur.node_pfn += BM_BITS_PER_BLOCK;
792 bm->cur.node_bit = 0;
793 touch_softlockup_watchdog();
797 /* No more nodes, goto next zone */
798 bm->cur.zone = list_entry(bm->cur.zone->list.next,
799 struct mem_zone_bm_rtree, list);
800 if (&bm->cur.zone->list != &bm->zones) {
801 bm->cur.node = list_entry(bm->cur.zone->leaves.next,
802 struct rtree_node, list);
803 bm->cur.node_pfn = 0;
804 bm->cur.node_bit = 0;
813 * memory_bm_rtree_next_pfn - Find the next set bit in the bitmap @bm
815 * Starting from the last returned position this function searches
816 * for the next set bit in the memory bitmap and returns its
817 * number. If no more bit is set BM_END_OF_MAP is returned.
819 * It is required to run memory_bm_position_reset() before the
820 * first call to this function.
822 static unsigned long memory_bm_next_pfn(struct memory_bitmap *bm)
824 unsigned long bits, pfn, pages;
828 pages = bm->cur.zone->end_pfn - bm->cur.zone->start_pfn;
829 bits = min(pages - bm->cur.node_pfn, BM_BITS_PER_BLOCK);
830 bit = find_next_bit(bm->cur.node->data, bits,
833 pfn = bm->cur.zone->start_pfn + bm->cur.node_pfn + bit;
834 bm->cur.node_bit = bit + 1;
837 } while (rtree_next_node(bm));
839 return BM_END_OF_MAP;
843 * This structure represents a range of page frames the contents of which
844 * should not be saved during the suspend.
847 struct nosave_region {
848 struct list_head list;
849 unsigned long start_pfn;
850 unsigned long end_pfn;
853 static LIST_HEAD(nosave_regions);
856 * register_nosave_region - register a range of page frames the contents
857 * of which should not be saved during the suspend (to be used in the early
858 * initialization code)
862 __register_nosave_region(unsigned long start_pfn, unsigned long end_pfn,
865 struct nosave_region *region;
867 if (start_pfn >= end_pfn)
870 if (!list_empty(&nosave_regions)) {
871 /* Try to extend the previous region (they should be sorted) */
872 region = list_entry(nosave_regions.prev,
873 struct nosave_region, list);
874 if (region->end_pfn == start_pfn) {
875 region->end_pfn = end_pfn;
880 /* during init, this shouldn't fail */
881 region = kmalloc(sizeof(struct nosave_region), GFP_KERNEL);
884 /* This allocation cannot fail */
885 region = memblock_virt_alloc(sizeof(struct nosave_region), 0);
886 region->start_pfn = start_pfn;
887 region->end_pfn = end_pfn;
888 list_add_tail(®ion->list, &nosave_regions);
890 printk(KERN_INFO "PM: Registered nosave memory: [mem %#010llx-%#010llx]\n",
891 (unsigned long long) start_pfn << PAGE_SHIFT,
892 ((unsigned long long) end_pfn << PAGE_SHIFT) - 1);
896 * Set bits in this map correspond to the page frames the contents of which
897 * should not be saved during the suspend.
899 static struct memory_bitmap *forbidden_pages_map;
901 /* Set bits in this map correspond to free page frames. */
902 static struct memory_bitmap *free_pages_map;
905 * Each page frame allocated for creating the image is marked by setting the
906 * corresponding bits in forbidden_pages_map and free_pages_map simultaneously
909 void swsusp_set_page_free(struct page *page)
912 memory_bm_set_bit(free_pages_map, page_to_pfn(page));
915 static int swsusp_page_is_free(struct page *page)
917 return free_pages_map ?
918 memory_bm_test_bit(free_pages_map, page_to_pfn(page)) : 0;
921 void swsusp_unset_page_free(struct page *page)
924 memory_bm_clear_bit(free_pages_map, page_to_pfn(page));
927 static void swsusp_set_page_forbidden(struct page *page)
929 if (forbidden_pages_map)
930 memory_bm_set_bit(forbidden_pages_map, page_to_pfn(page));
933 int swsusp_page_is_forbidden(struct page *page)
935 return forbidden_pages_map ?
936 memory_bm_test_bit(forbidden_pages_map, page_to_pfn(page)) : 0;
939 static void swsusp_unset_page_forbidden(struct page *page)
941 if (forbidden_pages_map)
942 memory_bm_clear_bit(forbidden_pages_map, page_to_pfn(page));
946 * mark_nosave_pages - set bits corresponding to the page frames the
947 * contents of which should not be saved in a given bitmap.
950 static void mark_nosave_pages(struct memory_bitmap *bm)
952 struct nosave_region *region;
954 if (list_empty(&nosave_regions))
957 list_for_each_entry(region, &nosave_regions, list) {
960 pr_debug("PM: Marking nosave pages: [mem %#010llx-%#010llx]\n",
961 (unsigned long long) region->start_pfn << PAGE_SHIFT,
962 ((unsigned long long) region->end_pfn << PAGE_SHIFT)
965 for (pfn = region->start_pfn; pfn < region->end_pfn; pfn++)
966 if (pfn_valid(pfn)) {
968 * It is safe to ignore the result of
969 * mem_bm_set_bit_check() here, since we won't
970 * touch the PFNs for which the error is
973 mem_bm_set_bit_check(bm, pfn);
979 * create_basic_memory_bitmaps - create bitmaps needed for marking page
980 * frames that should not be saved and free page frames. The pointers
981 * forbidden_pages_map and free_pages_map are only modified if everything
982 * goes well, because we don't want the bits to be used before both bitmaps
986 int create_basic_memory_bitmaps(void)
988 struct memory_bitmap *bm1, *bm2;
991 if (forbidden_pages_map && free_pages_map)
994 BUG_ON(forbidden_pages_map || free_pages_map);
996 bm1 = kzalloc(sizeof(struct memory_bitmap), GFP_KERNEL);
1000 error = memory_bm_create(bm1, GFP_KERNEL, PG_ANY);
1002 goto Free_first_object;
1004 bm2 = kzalloc(sizeof(struct memory_bitmap), GFP_KERNEL);
1006 goto Free_first_bitmap;
1008 error = memory_bm_create(bm2, GFP_KERNEL, PG_ANY);
1010 goto Free_second_object;
1012 forbidden_pages_map = bm1;
1013 free_pages_map = bm2;
1014 mark_nosave_pages(forbidden_pages_map);
1016 pr_debug("PM: Basic memory bitmaps created\n");
1023 memory_bm_free(bm1, PG_UNSAFE_CLEAR);
1030 * free_basic_memory_bitmaps - free memory bitmaps allocated by
1031 * create_basic_memory_bitmaps(). The auxiliary pointers are necessary
1032 * so that the bitmaps themselves are not referred to while they are being
1036 void free_basic_memory_bitmaps(void)
1038 struct memory_bitmap *bm1, *bm2;
1040 if (WARN_ON(!(forbidden_pages_map && free_pages_map)))
1043 bm1 = forbidden_pages_map;
1044 bm2 = free_pages_map;
1045 forbidden_pages_map = NULL;
1046 free_pages_map = NULL;
1047 memory_bm_free(bm1, PG_UNSAFE_CLEAR);
1049 memory_bm_free(bm2, PG_UNSAFE_CLEAR);
1052 pr_debug("PM: Basic memory bitmaps freed\n");
1056 * snapshot_additional_pages - estimate the number of additional pages
1057 * be needed for setting up the suspend image data structures for given
1058 * zone (usually the returned value is greater than the exact number)
1061 unsigned int snapshot_additional_pages(struct zone *zone)
1063 unsigned int rtree, nodes;
1065 rtree = nodes = DIV_ROUND_UP(zone->spanned_pages, BM_BITS_PER_BLOCK);
1066 rtree += DIV_ROUND_UP(rtree * sizeof(struct rtree_node),
1067 LINKED_PAGE_DATA_SIZE);
1069 nodes = DIV_ROUND_UP(nodes, BM_ENTRIES_PER_LEVEL);
1076 #ifdef CONFIG_HIGHMEM
1078 * count_free_highmem_pages - compute the total number of free highmem
1079 * pages, system-wide.
1082 static unsigned int count_free_highmem_pages(void)
1085 unsigned int cnt = 0;
1087 for_each_populated_zone(zone)
1088 if (is_highmem(zone))
1089 cnt += zone_page_state(zone, NR_FREE_PAGES);
1095 * saveable_highmem_page - Determine whether a highmem page should be
1096 * included in the suspend image.
1098 * We should save the page if it isn't Nosave or NosaveFree, or Reserved,
1099 * and it isn't a part of a free chunk of pages.
1101 static struct page *saveable_highmem_page(struct zone *zone, unsigned long pfn)
1105 if (!pfn_valid(pfn))
1108 page = pfn_to_page(pfn);
1109 if (page_zone(page) != zone)
1112 BUG_ON(!PageHighMem(page));
1114 if (swsusp_page_is_forbidden(page) || swsusp_page_is_free(page) ||
1118 if (page_is_guard(page))
1125 * count_highmem_pages - compute the total number of saveable highmem
1129 static unsigned int count_highmem_pages(void)
1134 for_each_populated_zone(zone) {
1135 unsigned long pfn, max_zone_pfn;
1137 if (!is_highmem(zone))
1140 mark_free_pages(zone);
1141 max_zone_pfn = zone_end_pfn(zone);
1142 for (pfn = zone->zone_start_pfn; pfn < max_zone_pfn; pfn++)
1143 if (saveable_highmem_page(zone, pfn))
1149 static inline void *saveable_highmem_page(struct zone *z, unsigned long p)
1153 #endif /* CONFIG_HIGHMEM */
1156 * saveable_page - Determine whether a non-highmem page should be included
1157 * in the suspend image.
1159 * We should save the page if it isn't Nosave, and is not in the range
1160 * of pages statically defined as 'unsaveable', and it isn't a part of
1161 * a free chunk of pages.
1163 static struct page *saveable_page(struct zone *zone, unsigned long pfn)
1167 if (!pfn_valid(pfn))
1170 page = pfn_to_page(pfn);
1171 if (page_zone(page) != zone)
1174 BUG_ON(PageHighMem(page));
1176 if (swsusp_page_is_forbidden(page) || swsusp_page_is_free(page))
1179 if (PageReserved(page)
1180 && (!kernel_page_present(page) || pfn_is_nosave(pfn)))
1183 if (page_is_guard(page))
1190 * count_data_pages - compute the total number of saveable non-highmem
1194 static unsigned int count_data_pages(void)
1197 unsigned long pfn, max_zone_pfn;
1200 for_each_populated_zone(zone) {
1201 if (is_highmem(zone))
1204 mark_free_pages(zone);
1205 max_zone_pfn = zone_end_pfn(zone);
1206 for (pfn = zone->zone_start_pfn; pfn < max_zone_pfn; pfn++)
1207 if (saveable_page(zone, pfn))
1213 /* This is needed, because copy_page and memcpy are not usable for copying
1216 static inline void do_copy_page(long *dst, long *src)
1220 for (n = PAGE_SIZE / sizeof(long); n; n--)
1226 * safe_copy_page - check if the page we are going to copy is marked as
1227 * present in the kernel page tables (this always is the case if
1228 * CONFIG_DEBUG_PAGEALLOC is not set and in that case
1229 * kernel_page_present() always returns 'true').
1231 static void safe_copy_page(void *dst, struct page *s_page)
1233 if (kernel_page_present(s_page)) {
1234 do_copy_page(dst, page_address(s_page));
1236 kernel_map_pages(s_page, 1, 1);
1237 do_copy_page(dst, page_address(s_page));
1238 kernel_map_pages(s_page, 1, 0);
1243 #ifdef CONFIG_HIGHMEM
1244 static inline struct page *
1245 page_is_saveable(struct zone *zone, unsigned long pfn)
1247 return is_highmem(zone) ?
1248 saveable_highmem_page(zone, pfn) : saveable_page(zone, pfn);
1251 static void copy_data_page(unsigned long dst_pfn, unsigned long src_pfn)
1253 struct page *s_page, *d_page;
1256 s_page = pfn_to_page(src_pfn);
1257 d_page = pfn_to_page(dst_pfn);
1258 if (PageHighMem(s_page)) {
1259 src = kmap_atomic(s_page);
1260 dst = kmap_atomic(d_page);
1261 do_copy_page(dst, src);
1265 if (PageHighMem(d_page)) {
1266 /* Page pointed to by src may contain some kernel
1267 * data modified by kmap_atomic()
1269 safe_copy_page(buffer, s_page);
1270 dst = kmap_atomic(d_page);
1271 copy_page(dst, buffer);
1274 safe_copy_page(page_address(d_page), s_page);
1279 #define page_is_saveable(zone, pfn) saveable_page(zone, pfn)
1281 static inline void copy_data_page(unsigned long dst_pfn, unsigned long src_pfn)
1283 safe_copy_page(page_address(pfn_to_page(dst_pfn)),
1284 pfn_to_page(src_pfn));
1286 #endif /* CONFIG_HIGHMEM */
1289 copy_data_pages(struct memory_bitmap *copy_bm, struct memory_bitmap *orig_bm)
1294 for_each_populated_zone(zone) {
1295 unsigned long max_zone_pfn;
1297 mark_free_pages(zone);
1298 max_zone_pfn = zone_end_pfn(zone);
1299 for (pfn = zone->zone_start_pfn; pfn < max_zone_pfn; pfn++)
1300 if (page_is_saveable(zone, pfn))
1301 memory_bm_set_bit(orig_bm, pfn);
1303 memory_bm_position_reset(orig_bm);
1304 memory_bm_position_reset(copy_bm);
1306 pfn = memory_bm_next_pfn(orig_bm);
1307 if (unlikely(pfn == BM_END_OF_MAP))
1309 copy_data_page(memory_bm_next_pfn(copy_bm), pfn);
1313 /* Total number of image pages */
1314 static unsigned int nr_copy_pages;
1315 /* Number of pages needed for saving the original pfns of the image pages */
1316 static unsigned int nr_meta_pages;
1318 * Numbers of normal and highmem page frames allocated for hibernation image
1319 * before suspending devices.
1321 unsigned int alloc_normal, alloc_highmem;
1323 * Memory bitmap used for marking saveable pages (during hibernation) or
1324 * hibernation image pages (during restore)
1326 static struct memory_bitmap orig_bm;
1328 * Memory bitmap used during hibernation for marking allocated page frames that
1329 * will contain copies of saveable pages. During restore it is initially used
1330 * for marking hibernation image pages, but then the set bits from it are
1331 * duplicated in @orig_bm and it is released. On highmem systems it is next
1332 * used for marking "safe" highmem pages, but it has to be reinitialized for
1335 static struct memory_bitmap copy_bm;
1338 * swsusp_free - free pages allocated for the suspend.
1340 * Suspend pages are alocated before the atomic copy is made, so we
1341 * need to release them after the resume.
1344 void swsusp_free(void)
1346 unsigned long fb_pfn, fr_pfn;
1348 if (!forbidden_pages_map || !free_pages_map)
1351 memory_bm_position_reset(forbidden_pages_map);
1352 memory_bm_position_reset(free_pages_map);
1355 fr_pfn = memory_bm_next_pfn(free_pages_map);
1356 fb_pfn = memory_bm_next_pfn(forbidden_pages_map);
1359 * Find the next bit set in both bitmaps. This is guaranteed to
1360 * terminate when fb_pfn == fr_pfn == BM_END_OF_MAP.
1363 if (fb_pfn < fr_pfn)
1364 fb_pfn = memory_bm_next_pfn(forbidden_pages_map);
1365 if (fr_pfn < fb_pfn)
1366 fr_pfn = memory_bm_next_pfn(free_pages_map);
1367 } while (fb_pfn != fr_pfn);
1369 if (fr_pfn != BM_END_OF_MAP && pfn_valid(fr_pfn)) {
1370 struct page *page = pfn_to_page(fr_pfn);
1372 memory_bm_clear_current(forbidden_pages_map);
1373 memory_bm_clear_current(free_pages_map);
1381 restore_pblist = NULL;
1387 /* Helper functions used for the shrinking of memory. */
1389 #define GFP_IMAGE (GFP_KERNEL | __GFP_NOWARN)
1392 * preallocate_image_pages - Allocate a number of pages for hibernation image
1393 * @nr_pages: Number of page frames to allocate.
1394 * @mask: GFP flags to use for the allocation.
1396 * Return value: Number of page frames actually allocated
1398 static unsigned long preallocate_image_pages(unsigned long nr_pages, gfp_t mask)
1400 unsigned long nr_alloc = 0;
1402 while (nr_pages > 0) {
1405 page = alloc_image_page(mask);
1408 memory_bm_set_bit(©_bm, page_to_pfn(page));
1409 if (PageHighMem(page))
1420 static unsigned long preallocate_image_memory(unsigned long nr_pages,
1421 unsigned long avail_normal)
1423 unsigned long alloc;
1425 if (avail_normal <= alloc_normal)
1428 alloc = avail_normal - alloc_normal;
1429 if (nr_pages < alloc)
1432 return preallocate_image_pages(alloc, GFP_IMAGE);
1435 #ifdef CONFIG_HIGHMEM
1436 static unsigned long preallocate_image_highmem(unsigned long nr_pages)
1438 return preallocate_image_pages(nr_pages, GFP_IMAGE | __GFP_HIGHMEM);
1442 * __fraction - Compute (an approximation of) x * (multiplier / base)
1444 static unsigned long __fraction(u64 x, u64 multiplier, u64 base)
1448 return (unsigned long)x;
1451 static unsigned long preallocate_highmem_fraction(unsigned long nr_pages,
1452 unsigned long highmem,
1453 unsigned long total)
1455 unsigned long alloc = __fraction(nr_pages, highmem, total);
1457 return preallocate_image_pages(alloc, GFP_IMAGE | __GFP_HIGHMEM);
1459 #else /* CONFIG_HIGHMEM */
1460 static inline unsigned long preallocate_image_highmem(unsigned long nr_pages)
1465 static inline unsigned long preallocate_highmem_fraction(unsigned long nr_pages,
1466 unsigned long highmem,
1467 unsigned long total)
1471 #endif /* CONFIG_HIGHMEM */
1474 * free_unnecessary_pages - Release preallocated pages not needed for the image
1476 static unsigned long free_unnecessary_pages(void)
1478 unsigned long save, to_free_normal, to_free_highmem, free;
1480 save = count_data_pages();
1481 if (alloc_normal >= save) {
1482 to_free_normal = alloc_normal - save;
1486 save -= alloc_normal;
1488 save += count_highmem_pages();
1489 if (alloc_highmem >= save) {
1490 to_free_highmem = alloc_highmem - save;
1492 to_free_highmem = 0;
1493 save -= alloc_highmem;
1494 if (to_free_normal > save)
1495 to_free_normal -= save;
1499 free = to_free_normal + to_free_highmem;
1501 memory_bm_position_reset(©_bm);
1503 while (to_free_normal > 0 || to_free_highmem > 0) {
1504 unsigned long pfn = memory_bm_next_pfn(©_bm);
1505 struct page *page = pfn_to_page(pfn);
1507 if (PageHighMem(page)) {
1508 if (!to_free_highmem)
1513 if (!to_free_normal)
1518 memory_bm_clear_bit(©_bm, pfn);
1519 swsusp_unset_page_forbidden(page);
1520 swsusp_unset_page_free(page);
1528 * minimum_image_size - Estimate the minimum acceptable size of an image
1529 * @saveable: Number of saveable pages in the system.
1531 * We want to avoid attempting to free too much memory too hard, so estimate the
1532 * minimum acceptable size of a hibernation image to use as the lower limit for
1533 * preallocating memory.
1535 * We assume that the minimum image size should be proportional to
1537 * [number of saveable pages] - [number of pages that can be freed in theory]
1539 * where the second term is the sum of (1) reclaimable slab pages, (2) active
1540 * and (3) inactive anonymous pages, (4) active and (5) inactive file pages,
1541 * minus mapped file pages.
1543 static unsigned long minimum_image_size(unsigned long saveable)
1547 size = global_page_state(NR_SLAB_RECLAIMABLE)
1548 + global_page_state(NR_ACTIVE_ANON)
1549 + global_page_state(NR_INACTIVE_ANON)
1550 + global_page_state(NR_ACTIVE_FILE)
1551 + global_page_state(NR_INACTIVE_FILE)
1552 - global_page_state(NR_FILE_MAPPED);
1554 return saveable <= size ? 0 : saveable - size;
1558 * hibernate_preallocate_memory - Preallocate memory for hibernation image
1560 * To create a hibernation image it is necessary to make a copy of every page
1561 * frame in use. We also need a number of page frames to be free during
1562 * hibernation for allocations made while saving the image and for device
1563 * drivers, in case they need to allocate memory from their hibernation
1564 * callbacks (these two numbers are given by PAGES_FOR_IO (which is a rough
1565 * estimate) and reserverd_size divided by PAGE_SIZE (which is tunable through
1566 * /sys/power/reserved_size, respectively). To make this happen, we compute the
1567 * total number of available page frames and allocate at least
1569 * ([page frames total] + PAGES_FOR_IO + [metadata pages]) / 2
1570 * + 2 * DIV_ROUND_UP(reserved_size, PAGE_SIZE)
1572 * of them, which corresponds to the maximum size of a hibernation image.
1574 * If image_size is set below the number following from the above formula,
1575 * the preallocation of memory is continued until the total number of saveable
1576 * pages in the system is below the requested image size or the minimum
1577 * acceptable image size returned by minimum_image_size(), whichever is greater.
1579 int hibernate_preallocate_memory(void)
1582 unsigned long saveable, size, max_size, count, highmem, pages = 0;
1583 unsigned long alloc, save_highmem, pages_highmem, avail_normal;
1584 ktime_t start, stop;
1587 printk(KERN_INFO "PM: Preallocating image memory... ");
1588 start = ktime_get();
1590 error = memory_bm_create(&orig_bm, GFP_IMAGE, PG_ANY);
1594 error = memory_bm_create(©_bm, GFP_IMAGE, PG_ANY);
1601 /* Count the number of saveable data pages. */
1602 save_highmem = count_highmem_pages();
1603 saveable = count_data_pages();
1606 * Compute the total number of page frames we can use (count) and the
1607 * number of pages needed for image metadata (size).
1610 saveable += save_highmem;
1611 highmem = save_highmem;
1613 for_each_populated_zone(zone) {
1614 size += snapshot_additional_pages(zone);
1615 if (is_highmem(zone))
1616 highmem += zone_page_state(zone, NR_FREE_PAGES);
1618 count += zone_page_state(zone, NR_FREE_PAGES);
1620 avail_normal = count;
1622 count -= totalreserve_pages;
1624 /* Add number of pages required for page keys (s390 only). */
1625 size += page_key_additional_pages(saveable);
1627 /* Compute the maximum number of saveable pages to leave in memory. */
1628 max_size = (count - (size + PAGES_FOR_IO)) / 2
1629 - 2 * DIV_ROUND_UP(reserved_size, PAGE_SIZE);
1630 /* Compute the desired number of image pages specified by image_size. */
1631 size = DIV_ROUND_UP(image_size, PAGE_SIZE);
1632 if (size > max_size)
1635 * If the desired number of image pages is at least as large as the
1636 * current number of saveable pages in memory, allocate page frames for
1637 * the image and we're done.
1639 if (size >= saveable) {
1640 pages = preallocate_image_highmem(save_highmem);
1641 pages += preallocate_image_memory(saveable - pages, avail_normal);
1645 /* Estimate the minimum size of the image. */
1646 pages = minimum_image_size(saveable);
1648 * To avoid excessive pressure on the normal zone, leave room in it to
1649 * accommodate an image of the minimum size (unless it's already too
1650 * small, in which case don't preallocate pages from it at all).
1652 if (avail_normal > pages)
1653 avail_normal -= pages;
1657 size = min_t(unsigned long, pages, max_size);
1660 * Let the memory management subsystem know that we're going to need a
1661 * large number of page frames to allocate and make it free some memory.
1662 * NOTE: If this is not done, performance will be hurt badly in some
1665 shrink_all_memory(saveable - size);
1668 * The number of saveable pages in memory was too high, so apply some
1669 * pressure to decrease it. First, make room for the largest possible
1670 * image and fail if that doesn't work. Next, try to decrease the size
1671 * of the image as much as indicated by 'size' using allocations from
1672 * highmem and non-highmem zones separately.
1674 pages_highmem = preallocate_image_highmem(highmem / 2);
1675 alloc = count - max_size;
1676 if (alloc > pages_highmem)
1677 alloc -= pages_highmem;
1680 pages = preallocate_image_memory(alloc, avail_normal);
1681 if (pages < alloc) {
1682 /* We have exhausted non-highmem pages, try highmem. */
1684 pages += pages_highmem;
1685 pages_highmem = preallocate_image_highmem(alloc);
1686 if (pages_highmem < alloc)
1688 pages += pages_highmem;
1690 * size is the desired number of saveable pages to leave in
1691 * memory, so try to preallocate (all memory - size) pages.
1693 alloc = (count - pages) - size;
1694 pages += preallocate_image_highmem(alloc);
1697 * There are approximately max_size saveable pages at this point
1698 * and we want to reduce this number down to size.
1700 alloc = max_size - size;
1701 size = preallocate_highmem_fraction(alloc, highmem, count);
1702 pages_highmem += size;
1704 size = preallocate_image_memory(alloc, avail_normal);
1705 pages_highmem += preallocate_image_highmem(alloc - size);
1706 pages += pages_highmem + size;
1710 * We only need as many page frames for the image as there are saveable
1711 * pages in memory, but we have allocated more. Release the excessive
1714 pages -= free_unnecessary_pages();
1718 printk(KERN_CONT "done (allocated %lu pages)\n", pages);
1719 swsusp_show_speed(start, stop, pages, "Allocated");
1724 printk(KERN_CONT "\n");
1729 #ifdef CONFIG_HIGHMEM
1731 * count_pages_for_highmem - compute the number of non-highmem pages
1732 * that will be necessary for creating copies of highmem pages.
1735 static unsigned int count_pages_for_highmem(unsigned int nr_highmem)
1737 unsigned int free_highmem = count_free_highmem_pages() + alloc_highmem;
1739 if (free_highmem >= nr_highmem)
1742 nr_highmem -= free_highmem;
1748 count_pages_for_highmem(unsigned int nr_highmem) { return 0; }
1749 #endif /* CONFIG_HIGHMEM */
1752 * enough_free_mem - Make sure we have enough free memory for the
1756 static int enough_free_mem(unsigned int nr_pages, unsigned int nr_highmem)
1759 unsigned int free = alloc_normal;
1761 for_each_populated_zone(zone)
1762 if (!is_highmem(zone))
1763 free += zone_page_state(zone, NR_FREE_PAGES);
1765 nr_pages += count_pages_for_highmem(nr_highmem);
1766 pr_debug("PM: Normal pages needed: %u + %u, available pages: %u\n",
1767 nr_pages, PAGES_FOR_IO, free);
1769 return free > nr_pages + PAGES_FOR_IO;
1772 #ifdef CONFIG_HIGHMEM
1774 * get_highmem_buffer - if there are some highmem pages in the suspend
1775 * image, we may need the buffer to copy them and/or load their data.
1778 static inline int get_highmem_buffer(int safe_needed)
1780 buffer = get_image_page(GFP_ATOMIC | __GFP_COLD, safe_needed);
1781 return buffer ? 0 : -ENOMEM;
1785 * alloc_highmem_image_pages - allocate some highmem pages for the image.
1786 * Try to allocate as many pages as needed, but if the number of free
1787 * highmem pages is lesser than that, allocate them all.
1790 static inline unsigned int
1791 alloc_highmem_pages(struct memory_bitmap *bm, unsigned int nr_highmem)
1793 unsigned int to_alloc = count_free_highmem_pages();
1795 if (to_alloc > nr_highmem)
1796 to_alloc = nr_highmem;
1798 nr_highmem -= to_alloc;
1799 while (to_alloc-- > 0) {
1802 page = alloc_image_page(__GFP_HIGHMEM|__GFP_KSWAPD_RECLAIM);
1803 memory_bm_set_bit(bm, page_to_pfn(page));
1808 static inline int get_highmem_buffer(int safe_needed) { return 0; }
1810 static inline unsigned int
1811 alloc_highmem_pages(struct memory_bitmap *bm, unsigned int n) { return 0; }
1812 #endif /* CONFIG_HIGHMEM */
1815 * swsusp_alloc - allocate memory for the suspend image
1817 * We first try to allocate as many highmem pages as there are
1818 * saveable highmem pages in the system. If that fails, we allocate
1819 * non-highmem pages for the copies of the remaining highmem ones.
1821 * In this approach it is likely that the copies of highmem pages will
1822 * also be located in the high memory, because of the way in which
1823 * copy_data_pages() works.
1827 swsusp_alloc(struct memory_bitmap *orig_bm, struct memory_bitmap *copy_bm,
1828 unsigned int nr_pages, unsigned int nr_highmem)
1830 if (nr_highmem > 0) {
1831 if (get_highmem_buffer(PG_ANY))
1833 if (nr_highmem > alloc_highmem) {
1834 nr_highmem -= alloc_highmem;
1835 nr_pages += alloc_highmem_pages(copy_bm, nr_highmem);
1838 if (nr_pages > alloc_normal) {
1839 nr_pages -= alloc_normal;
1840 while (nr_pages-- > 0) {
1843 page = alloc_image_page(GFP_ATOMIC | __GFP_COLD);
1846 memory_bm_set_bit(copy_bm, page_to_pfn(page));
1857 asmlinkage __visible int swsusp_save(void)
1859 unsigned int nr_pages, nr_highmem;
1861 printk(KERN_INFO "PM: Creating hibernation image:\n");
1863 drain_local_pages(NULL);
1864 nr_pages = count_data_pages();
1865 nr_highmem = count_highmem_pages();
1866 printk(KERN_INFO "PM: Need to copy %u pages\n", nr_pages + nr_highmem);
1868 if (!enough_free_mem(nr_pages, nr_highmem)) {
1869 printk(KERN_ERR "PM: Not enough free memory\n");
1873 if (swsusp_alloc(&orig_bm, ©_bm, nr_pages, nr_highmem)) {
1874 printk(KERN_ERR "PM: Memory allocation failed\n");
1878 /* During allocating of suspend pagedir, new cold pages may appear.
1881 drain_local_pages(NULL);
1882 copy_data_pages(©_bm, &orig_bm);
1885 * End of critical section. From now on, we can write to memory,
1886 * but we should not touch disk. This specially means we must _not_
1887 * touch swap space! Except we must write out our image of course.
1890 nr_pages += nr_highmem;
1891 nr_copy_pages = nr_pages;
1892 nr_meta_pages = DIV_ROUND_UP(nr_pages * sizeof(long), PAGE_SIZE);
1894 printk(KERN_INFO "PM: Hibernation image created (%d pages copied)\n",
1900 #ifndef CONFIG_ARCH_HIBERNATION_HEADER
1901 static int init_header_complete(struct swsusp_info *info)
1903 memcpy(&info->uts, init_utsname(), sizeof(struct new_utsname));
1904 info->version_code = LINUX_VERSION_CODE;
1908 static char *check_image_kernel(struct swsusp_info *info)
1910 if (info->version_code != LINUX_VERSION_CODE)
1911 return "kernel version";
1912 if (strcmp(info->uts.sysname,init_utsname()->sysname))
1913 return "system type";
1914 if (strcmp(info->uts.release,init_utsname()->release))
1915 return "kernel release";
1916 if (strcmp(info->uts.version,init_utsname()->version))
1918 if (strcmp(info->uts.machine,init_utsname()->machine))
1922 #endif /* CONFIG_ARCH_HIBERNATION_HEADER */
1924 unsigned long snapshot_get_image_size(void)
1926 return nr_copy_pages + nr_meta_pages + 1;
1929 static int init_header(struct swsusp_info *info)
1931 memset(info, 0, sizeof(struct swsusp_info));
1932 info->num_physpages = get_num_physpages();
1933 info->image_pages = nr_copy_pages;
1934 info->pages = snapshot_get_image_size();
1935 info->size = info->pages;
1936 info->size <<= PAGE_SHIFT;
1937 return init_header_complete(info);
1941 * pack_pfns - pfns corresponding to the set bits found in the bitmap @bm
1942 * are stored in the array @buf[] (1 page at a time)
1946 pack_pfns(unsigned long *buf, struct memory_bitmap *bm)
1950 for (j = 0; j < PAGE_SIZE / sizeof(long); j++) {
1951 buf[j] = memory_bm_next_pfn(bm);
1952 if (unlikely(buf[j] == BM_END_OF_MAP))
1954 /* Save page key for data page (s390 only). */
1955 page_key_read(buf + j);
1960 * snapshot_read_next - used for reading the system memory snapshot.
1962 * On the first call to it @handle should point to a zeroed
1963 * snapshot_handle structure. The structure gets updated and a pointer
1964 * to it should be passed to this function every next time.
1966 * On success the function returns a positive number. Then, the caller
1967 * is allowed to read up to the returned number of bytes from the memory
1968 * location computed by the data_of() macro.
1970 * The function returns 0 to indicate the end of data stream condition,
1971 * and a negative number is returned on error. In such cases the
1972 * structure pointed to by @handle is not updated and should not be used
1976 int snapshot_read_next(struct snapshot_handle *handle)
1978 if (handle->cur > nr_meta_pages + nr_copy_pages)
1982 /* This makes the buffer be freed by swsusp_free() */
1983 buffer = get_image_page(GFP_ATOMIC, PG_ANY);
1990 error = init_header((struct swsusp_info *)buffer);
1993 handle->buffer = buffer;
1994 memory_bm_position_reset(&orig_bm);
1995 memory_bm_position_reset(©_bm);
1996 } else if (handle->cur <= nr_meta_pages) {
1998 pack_pfns(buffer, &orig_bm);
2002 page = pfn_to_page(memory_bm_next_pfn(©_bm));
2003 if (PageHighMem(page)) {
2004 /* Highmem pages are copied to the buffer,
2005 * because we can't return with a kmapped
2006 * highmem page (we may not be called again).
2010 kaddr = kmap_atomic(page);
2011 copy_page(buffer, kaddr);
2012 kunmap_atomic(kaddr);
2013 handle->buffer = buffer;
2015 handle->buffer = page_address(page);
2023 * mark_unsafe_pages - mark the pages that cannot be used for storing
2024 * the image during resume, because they conflict with the pages that
2025 * had been used before suspend
2028 static int mark_unsafe_pages(struct memory_bitmap *bm)
2031 unsigned long pfn, max_zone_pfn;
2033 /* Clear page flags */
2034 for_each_populated_zone(zone) {
2035 max_zone_pfn = zone_end_pfn(zone);
2036 for (pfn = zone->zone_start_pfn; pfn < max_zone_pfn; pfn++)
2038 swsusp_unset_page_free(pfn_to_page(pfn));
2041 /* Mark pages that correspond to the "original" pfns as "unsafe" */
2042 memory_bm_position_reset(bm);
2044 pfn = memory_bm_next_pfn(bm);
2045 if (likely(pfn != BM_END_OF_MAP)) {
2046 if (likely(pfn_valid(pfn)))
2047 swsusp_set_page_free(pfn_to_page(pfn));
2051 } while (pfn != BM_END_OF_MAP);
2053 allocated_unsafe_pages = 0;
2059 duplicate_memory_bitmap(struct memory_bitmap *dst, struct memory_bitmap *src)
2063 memory_bm_position_reset(src);
2064 pfn = memory_bm_next_pfn(src);
2065 while (pfn != BM_END_OF_MAP) {
2066 memory_bm_set_bit(dst, pfn);
2067 pfn = memory_bm_next_pfn(src);
2071 static int check_header(struct swsusp_info *info)
2075 reason = check_image_kernel(info);
2076 if (!reason && info->num_physpages != get_num_physpages())
2077 reason = "memory size";
2079 printk(KERN_ERR "PM: Image mismatch: %s\n", reason);
2086 * load header - check the image header and copy data from it
2090 load_header(struct swsusp_info *info)
2094 restore_pblist = NULL;
2095 error = check_header(info);
2097 nr_copy_pages = info->image_pages;
2098 nr_meta_pages = info->pages - info->image_pages - 1;
2104 * unpack_orig_pfns - for each element of @buf[] (1 page at a time) set
2105 * the corresponding bit in the memory bitmap @bm
2107 static int unpack_orig_pfns(unsigned long *buf, struct memory_bitmap *bm)
2111 for (j = 0; j < PAGE_SIZE / sizeof(long); j++) {
2112 if (unlikely(buf[j] == BM_END_OF_MAP))
2115 /* Extract and buffer page key for data page (s390 only). */
2116 page_key_memorize(buf + j);
2118 if (memory_bm_pfn_present(bm, buf[j]))
2119 memory_bm_set_bit(bm, buf[j]);
2127 #ifdef CONFIG_HIGHMEM
2128 /* struct highmem_pbe is used for creating the list of highmem pages that
2129 * should be restored atomically during the resume from disk, because the page
2130 * frames they have occupied before the suspend are in use.
2132 struct highmem_pbe {
2133 struct page *copy_page; /* data is here now */
2134 struct page *orig_page; /* data was here before the suspend */
2135 struct highmem_pbe *next;
2138 /* List of highmem PBEs needed for restoring the highmem pages that were
2139 * allocated before the suspend and included in the suspend image, but have
2140 * also been allocated by the "resume" kernel, so their contents cannot be
2141 * written directly to their "original" page frames.
2143 static struct highmem_pbe *highmem_pblist;
2146 * count_highmem_image_pages - compute the number of highmem pages in the
2147 * suspend image. The bits in the memory bitmap @bm that correspond to the
2148 * image pages are assumed to be set.
2151 static unsigned int count_highmem_image_pages(struct memory_bitmap *bm)
2154 unsigned int cnt = 0;
2156 memory_bm_position_reset(bm);
2157 pfn = memory_bm_next_pfn(bm);
2158 while (pfn != BM_END_OF_MAP) {
2159 if (PageHighMem(pfn_to_page(pfn)))
2162 pfn = memory_bm_next_pfn(bm);
2168 * prepare_highmem_image - try to allocate as many highmem pages as
2169 * there are highmem image pages (@nr_highmem_p points to the variable
2170 * containing the number of highmem image pages). The pages that are
2171 * "safe" (ie. will not be overwritten when the suspend image is
2172 * restored) have the corresponding bits set in @bm (it must be
2175 * NOTE: This function should not be called if there are no highmem
2179 static unsigned int safe_highmem_pages;
2181 static struct memory_bitmap *safe_highmem_bm;
2184 prepare_highmem_image(struct memory_bitmap *bm, unsigned int *nr_highmem_p)
2186 unsigned int to_alloc;
2188 if (memory_bm_create(bm, GFP_ATOMIC, PG_SAFE))
2191 if (get_highmem_buffer(PG_SAFE))
2194 to_alloc = count_free_highmem_pages();
2195 if (to_alloc > *nr_highmem_p)
2196 to_alloc = *nr_highmem_p;
2198 *nr_highmem_p = to_alloc;
2200 safe_highmem_pages = 0;
2201 while (to_alloc-- > 0) {
2204 page = alloc_page(__GFP_HIGHMEM);
2205 if (!swsusp_page_is_free(page)) {
2206 /* The page is "safe", set its bit the bitmap */
2207 memory_bm_set_bit(bm, page_to_pfn(page));
2208 safe_highmem_pages++;
2210 /* Mark the page as allocated */
2211 swsusp_set_page_forbidden(page);
2212 swsusp_set_page_free(page);
2214 memory_bm_position_reset(bm);
2215 safe_highmem_bm = bm;
2220 * get_highmem_page_buffer - for given highmem image page find the buffer
2221 * that suspend_write_next() should set for its caller to write to.
2223 * If the page is to be saved to its "original" page frame or a copy of
2224 * the page is to be made in the highmem, @buffer is returned. Otherwise,
2225 * the copy of the page is to be made in normal memory, so the address of
2226 * the copy is returned.
2228 * If @buffer is returned, the caller of suspend_write_next() will write
2229 * the page's contents to @buffer, so they will have to be copied to the
2230 * right location on the next call to suspend_write_next() and it is done
2231 * with the help of copy_last_highmem_page(). For this purpose, if
2232 * @buffer is returned, @last_highmem page is set to the page to which
2233 * the data will have to be copied from @buffer.
2236 static struct page *last_highmem_page;
2239 get_highmem_page_buffer(struct page *page, struct chain_allocator *ca)
2241 struct highmem_pbe *pbe;
2244 if (swsusp_page_is_forbidden(page) && swsusp_page_is_free(page)) {
2245 /* We have allocated the "original" page frame and we can
2246 * use it directly to store the loaded page.
2248 last_highmem_page = page;
2251 /* The "original" page frame has not been allocated and we have to
2252 * use a "safe" page frame to store the loaded page.
2254 pbe = chain_alloc(ca, sizeof(struct highmem_pbe));
2257 return ERR_PTR(-ENOMEM);
2259 pbe->orig_page = page;
2260 if (safe_highmem_pages > 0) {
2263 /* Copy of the page will be stored in high memory */
2265 tmp = pfn_to_page(memory_bm_next_pfn(safe_highmem_bm));
2266 safe_highmem_pages--;
2267 last_highmem_page = tmp;
2268 pbe->copy_page = tmp;
2270 /* Copy of the page will be stored in normal memory */
2271 kaddr = safe_pages_list;
2272 safe_pages_list = safe_pages_list->next;
2273 pbe->copy_page = virt_to_page(kaddr);
2275 pbe->next = highmem_pblist;
2276 highmem_pblist = pbe;
2281 * copy_last_highmem_page - copy the contents of a highmem image from
2282 * @buffer, where the caller of snapshot_write_next() has place them,
2283 * to the right location represented by @last_highmem_page .
2286 static void copy_last_highmem_page(void)
2288 if (last_highmem_page) {
2291 dst = kmap_atomic(last_highmem_page);
2292 copy_page(dst, buffer);
2294 last_highmem_page = NULL;
2298 static inline int last_highmem_page_copied(void)
2300 return !last_highmem_page;
2303 static inline void free_highmem_data(void)
2305 if (safe_highmem_bm)
2306 memory_bm_free(safe_highmem_bm, PG_UNSAFE_CLEAR);
2309 free_image_page(buffer, PG_UNSAFE_CLEAR);
2313 count_highmem_image_pages(struct memory_bitmap *bm) { return 0; }
2316 prepare_highmem_image(struct memory_bitmap *bm, unsigned int *nr_highmem_p)
2321 static inline void *
2322 get_highmem_page_buffer(struct page *page, struct chain_allocator *ca)
2324 return ERR_PTR(-EINVAL);
2327 static inline void copy_last_highmem_page(void) {}
2328 static inline int last_highmem_page_copied(void) { return 1; }
2329 static inline void free_highmem_data(void) {}
2330 #endif /* CONFIG_HIGHMEM */
2333 * prepare_image - use the memory bitmap @bm to mark the pages that will
2334 * be overwritten in the process of restoring the system memory state
2335 * from the suspend image ("unsafe" pages) and allocate memory for the
2338 * The idea is to allocate a new memory bitmap first and then allocate
2339 * as many pages as needed for the image data, but not to assign these
2340 * pages to specific tasks initially. Instead, we just mark them as
2341 * allocated and create a lists of "safe" pages that will be used
2342 * later. On systems with high memory a list of "safe" highmem pages is
2346 #define PBES_PER_LINKED_PAGE (LINKED_PAGE_DATA_SIZE / sizeof(struct pbe))
2349 prepare_image(struct memory_bitmap *new_bm, struct memory_bitmap *bm)
2351 unsigned int nr_pages, nr_highmem;
2352 struct linked_page *lp;
2355 /* If there is no highmem, the buffer will not be necessary */
2356 free_image_page(buffer, PG_UNSAFE_CLEAR);
2359 nr_highmem = count_highmem_image_pages(bm);
2360 error = mark_unsafe_pages(bm);
2364 error = memory_bm_create(new_bm, GFP_ATOMIC, PG_SAFE);
2368 duplicate_memory_bitmap(new_bm, bm);
2369 memory_bm_free(bm, PG_UNSAFE_KEEP);
2370 if (nr_highmem > 0) {
2371 error = prepare_highmem_image(bm, &nr_highmem);
2375 /* Reserve some safe pages for potential later use.
2377 * NOTE: This way we make sure there will be enough safe pages for the
2378 * chain_alloc() in get_buffer(). It is a bit wasteful, but
2379 * nr_copy_pages cannot be greater than 50% of the memory anyway.
2381 * nr_copy_pages cannot be less than allocated_unsafe_pages too.
2383 nr_pages = nr_copy_pages - nr_highmem - allocated_unsafe_pages;
2384 nr_pages = DIV_ROUND_UP(nr_pages, PBES_PER_LINKED_PAGE);
2385 while (nr_pages > 0) {
2386 lp = get_image_page(GFP_ATOMIC, PG_SAFE);
2391 lp->next = safe_pages_list;
2392 safe_pages_list = lp;
2395 /* Preallocate memory for the image */
2396 nr_pages = nr_copy_pages - nr_highmem - allocated_unsafe_pages;
2397 while (nr_pages > 0) {
2398 lp = (struct linked_page *)get_zeroed_page(GFP_ATOMIC);
2403 if (!swsusp_page_is_free(virt_to_page(lp))) {
2404 /* The page is "safe", add it to the list */
2405 lp->next = safe_pages_list;
2406 safe_pages_list = lp;
2408 /* Mark the page as allocated */
2409 swsusp_set_page_forbidden(virt_to_page(lp));
2410 swsusp_set_page_free(virt_to_page(lp));
2421 * get_buffer - compute the address that snapshot_write_next() should
2422 * set for its caller to write to.
2425 static void *get_buffer(struct memory_bitmap *bm, struct chain_allocator *ca)
2429 unsigned long pfn = memory_bm_next_pfn(bm);
2431 if (pfn == BM_END_OF_MAP)
2432 return ERR_PTR(-EFAULT);
2434 page = pfn_to_page(pfn);
2435 if (PageHighMem(page))
2436 return get_highmem_page_buffer(page, ca);
2438 if (swsusp_page_is_forbidden(page) && swsusp_page_is_free(page))
2439 /* We have allocated the "original" page frame and we can
2440 * use it directly to store the loaded page.
2442 return page_address(page);
2444 /* The "original" page frame has not been allocated and we have to
2445 * use a "safe" page frame to store the loaded page.
2447 pbe = chain_alloc(ca, sizeof(struct pbe));
2450 return ERR_PTR(-ENOMEM);
2452 pbe->orig_address = page_address(page);
2453 pbe->address = safe_pages_list;
2454 safe_pages_list = safe_pages_list->next;
2455 pbe->next = restore_pblist;
2456 restore_pblist = pbe;
2457 return pbe->address;
2461 * snapshot_write_next - used for writing the system memory snapshot.
2463 * On the first call to it @handle should point to a zeroed
2464 * snapshot_handle structure. The structure gets updated and a pointer
2465 * to it should be passed to this function every next time.
2467 * On success the function returns a positive number. Then, the caller
2468 * is allowed to write up to the returned number of bytes to the memory
2469 * location computed by the data_of() macro.
2471 * The function returns 0 to indicate the "end of file" condition,
2472 * and a negative number is returned on error. In such cases the
2473 * structure pointed to by @handle is not updated and should not be used
2477 int snapshot_write_next(struct snapshot_handle *handle)
2479 static struct chain_allocator ca;
2482 /* Check if we have already loaded the entire image */
2483 if (handle->cur > 1 && handle->cur > nr_meta_pages + nr_copy_pages)
2486 handle->sync_read = 1;
2490 /* This makes the buffer be freed by swsusp_free() */
2491 buffer = get_image_page(GFP_ATOMIC, PG_ANY);
2496 handle->buffer = buffer;
2497 } else if (handle->cur == 1) {
2498 error = load_header(buffer);
2502 safe_pages_list = NULL;
2504 error = memory_bm_create(©_bm, GFP_ATOMIC, PG_ANY);
2508 /* Allocate buffer for page keys. */
2509 error = page_key_alloc(nr_copy_pages);
2513 } else if (handle->cur <= nr_meta_pages + 1) {
2514 error = unpack_orig_pfns(buffer, ©_bm);
2518 if (handle->cur == nr_meta_pages + 1) {
2519 error = prepare_image(&orig_bm, ©_bm);
2523 chain_init(&ca, GFP_ATOMIC, PG_SAFE);
2524 memory_bm_position_reset(&orig_bm);
2525 restore_pblist = NULL;
2526 handle->buffer = get_buffer(&orig_bm, &ca);
2527 handle->sync_read = 0;
2528 if (IS_ERR(handle->buffer))
2529 return PTR_ERR(handle->buffer);
2532 copy_last_highmem_page();
2533 /* Restore page key for data page (s390 only). */
2534 page_key_write(handle->buffer);
2535 handle->buffer = get_buffer(&orig_bm, &ca);
2536 if (IS_ERR(handle->buffer))
2537 return PTR_ERR(handle->buffer);
2538 if (handle->buffer != buffer)
2539 handle->sync_read = 0;
2546 * snapshot_write_finalize - must be called after the last call to
2547 * snapshot_write_next() in case the last page in the image happens
2548 * to be a highmem page and its contents should be stored in the
2549 * highmem. Additionally, it releases the memory that will not be
2553 void snapshot_write_finalize(struct snapshot_handle *handle)
2555 copy_last_highmem_page();
2556 /* Restore page key for data page (s390 only). */
2557 page_key_write(handle->buffer);
2559 /* Free only if we have loaded the image entirely */
2560 if (handle->cur > 1 && handle->cur > nr_meta_pages + nr_copy_pages) {
2561 memory_bm_free(&orig_bm, PG_UNSAFE_CLEAR);
2562 free_highmem_data();
2566 int snapshot_image_loaded(struct snapshot_handle *handle)
2568 return !(!nr_copy_pages || !last_highmem_page_copied() ||
2569 handle->cur <= nr_meta_pages + nr_copy_pages);
2572 #ifdef CONFIG_HIGHMEM
2573 /* Assumes that @buf is ready and points to a "safe" page */
2575 swap_two_pages_data(struct page *p1, struct page *p2, void *buf)
2577 void *kaddr1, *kaddr2;
2579 kaddr1 = kmap_atomic(p1);
2580 kaddr2 = kmap_atomic(p2);
2581 copy_page(buf, kaddr1);
2582 copy_page(kaddr1, kaddr2);
2583 copy_page(kaddr2, buf);
2584 kunmap_atomic(kaddr2);
2585 kunmap_atomic(kaddr1);
2589 * restore_highmem - for each highmem page that was allocated before
2590 * the suspend and included in the suspend image, and also has been
2591 * allocated by the "resume" kernel swap its current (ie. "before
2592 * resume") contents with the previous (ie. "before suspend") one.
2594 * If the resume eventually fails, we can call this function once
2595 * again and restore the "before resume" highmem state.
2598 int restore_highmem(void)
2600 struct highmem_pbe *pbe = highmem_pblist;
2606 buf = get_image_page(GFP_ATOMIC, PG_SAFE);
2611 swap_two_pages_data(pbe->copy_page, pbe->orig_page, buf);
2614 free_image_page(buf, PG_UNSAFE_CLEAR);
2617 #endif /* CONFIG_HIGHMEM */