init from v2.6.32.60
[mach-moxart.git] / kernel / power / snapshot.c
blob356f48722ef1517a26cdec411b439df053be2a74
1 /*
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>
15 #include <linux/mm.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>
21 #include <linux/pm.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>
30 #include <asm/uaccess.h>
31 #include <asm/mmu_context.h>
32 #include <asm/pgtable.h>
33 #include <asm/tlbflush.h>
34 #include <asm/io.h>
36 #include "power.h"
38 static int swsusp_page_is_free(struct page *);
39 static void swsusp_set_page_forbidden(struct page *);
40 static void swsusp_unset_page_forbidden(struct page *);
43 * Preferred image size in bytes (tunable via /sys/power/image_size).
44 * When it is set to N, swsusp will do its best to ensure the image
45 * size will not exceed N bytes, but if that is impossible, it will
46 * try to create the smallest image possible.
48 unsigned long image_size = 500 * 1024 * 1024;
50 /* List of PBEs needed for restoring the pages that were allocated before
51 * the suspend and included in the suspend image, but have also been
52 * allocated by the "resume" kernel, so their contents cannot be written
53 * directly to their "original" page frames.
55 struct pbe *restore_pblist;
57 /* Pointer to an auxiliary buffer (1 page) */
58 static void *buffer;
60 /**
61 * @safe_needed - on resume, for storing the PBE list and the image,
62 * we can only use memory pages that do not conflict with the pages
63 * used before suspend. The unsafe pages have PageNosaveFree set
64 * and we count them using unsafe_pages.
66 * Each allocated image page is marked as PageNosave and PageNosaveFree
67 * so that swsusp_free() can release it.
70 #define PG_ANY 0
71 #define PG_SAFE 1
72 #define PG_UNSAFE_CLEAR 1
73 #define PG_UNSAFE_KEEP 0
75 static unsigned int allocated_unsafe_pages;
77 static void *get_image_page(gfp_t gfp_mask, int safe_needed)
79 void *res;
81 res = (void *)get_zeroed_page(gfp_mask);
82 if (safe_needed)
83 while (res && swsusp_page_is_free(virt_to_page(res))) {
84 /* The page is unsafe, mark it for swsusp_free() */
85 swsusp_set_page_forbidden(virt_to_page(res));
86 allocated_unsafe_pages++;
87 res = (void *)get_zeroed_page(gfp_mask);
89 if (res) {
90 swsusp_set_page_forbidden(virt_to_page(res));
91 swsusp_set_page_free(virt_to_page(res));
93 return res;
96 unsigned long get_safe_page(gfp_t gfp_mask)
98 return (unsigned long)get_image_page(gfp_mask, PG_SAFE);
101 static struct page *alloc_image_page(gfp_t gfp_mask)
103 struct page *page;
105 page = alloc_page(gfp_mask);
106 if (page) {
107 swsusp_set_page_forbidden(page);
108 swsusp_set_page_free(page);
110 return page;
114 * free_image_page - free page represented by @addr, allocated with
115 * get_image_page (page flags set by it must be cleared)
118 static inline void free_image_page(void *addr, int clear_nosave_free)
120 struct page *page;
122 BUG_ON(!virt_addr_valid(addr));
124 page = virt_to_page(addr);
126 swsusp_unset_page_forbidden(page);
127 if (clear_nosave_free)
128 swsusp_unset_page_free(page);
130 __free_page(page);
133 /* struct linked_page is used to build chains of pages */
135 #define LINKED_PAGE_DATA_SIZE (PAGE_SIZE - sizeof(void *))
137 struct linked_page {
138 struct linked_page *next;
139 char data[LINKED_PAGE_DATA_SIZE];
140 } __attribute__((packed));
142 static inline void
143 free_list_of_pages(struct linked_page *list, int clear_page_nosave)
145 while (list) {
146 struct linked_page *lp = list->next;
148 free_image_page(list, clear_page_nosave);
149 list = lp;
154 * struct chain_allocator is used for allocating small objects out of
155 * a linked list of pages called 'the chain'.
157 * The chain grows each time when there is no room for a new object in
158 * the current page. The allocated objects cannot be freed individually.
159 * It is only possible to free them all at once, by freeing the entire
160 * chain.
162 * NOTE: The chain allocator may be inefficient if the allocated objects
163 * are not much smaller than PAGE_SIZE.
166 struct chain_allocator {
167 struct linked_page *chain; /* the chain */
168 unsigned int used_space; /* total size of objects allocated out
169 * of the current page
171 gfp_t gfp_mask; /* mask for allocating pages */
172 int safe_needed; /* if set, only "safe" pages are allocated */
175 static void
176 chain_init(struct chain_allocator *ca, gfp_t gfp_mask, int safe_needed)
178 ca->chain = NULL;
179 ca->used_space = LINKED_PAGE_DATA_SIZE;
180 ca->gfp_mask = gfp_mask;
181 ca->safe_needed = safe_needed;
184 static void *chain_alloc(struct chain_allocator *ca, unsigned int size)
186 void *ret;
188 if (LINKED_PAGE_DATA_SIZE - ca->used_space < size) {
189 struct linked_page *lp;
191 lp = get_image_page(ca->gfp_mask, ca->safe_needed);
192 if (!lp)
193 return NULL;
195 lp->next = ca->chain;
196 ca->chain = lp;
197 ca->used_space = 0;
199 ret = ca->chain->data + ca->used_space;
200 ca->used_space += size;
201 return ret;
205 * Data types related to memory bitmaps.
207 * Memory bitmap is a structure consiting of many linked lists of
208 * objects. The main list's elements are of type struct zone_bitmap
209 * and each of them corresonds to one zone. For each zone bitmap
210 * object there is a list of objects of type struct bm_block that
211 * represent each blocks of bitmap in which information is stored.
213 * struct memory_bitmap contains a pointer to the main list of zone
214 * bitmap objects, a struct bm_position used for browsing the bitmap,
215 * and a pointer to the list of pages used for allocating all of the
216 * zone bitmap objects and bitmap block objects.
218 * NOTE: It has to be possible to lay out the bitmap in memory
219 * using only allocations of order 0. Additionally, the bitmap is
220 * designed to work with arbitrary number of zones (this is over the
221 * top for now, but let's avoid making unnecessary assumptions ;-).
223 * struct zone_bitmap contains a pointer to a list of bitmap block
224 * objects and a pointer to the bitmap block object that has been
225 * most recently used for setting bits. Additionally, it contains the
226 * pfns that correspond to the start and end of the represented zone.
228 * struct bm_block contains a pointer to the memory page in which
229 * information is stored (in the form of a block of bitmap)
230 * It also contains the pfns that correspond to the start and end of
231 * the represented memory area.
234 #define BM_END_OF_MAP (~0UL)
236 #define BM_BITS_PER_BLOCK (PAGE_SIZE * BITS_PER_BYTE)
238 struct bm_block {
239 struct list_head hook; /* hook into a list of bitmap blocks */
240 unsigned long start_pfn; /* pfn represented by the first bit */
241 unsigned long end_pfn; /* pfn represented by the last bit plus 1 */
242 unsigned long *data; /* bitmap representing pages */
245 static inline unsigned long bm_block_bits(struct bm_block *bb)
247 return bb->end_pfn - bb->start_pfn;
250 /* strcut bm_position is used for browsing memory bitmaps */
252 struct bm_position {
253 struct bm_block *block;
254 int bit;
257 struct memory_bitmap {
258 struct list_head blocks; /* list of bitmap blocks */
259 struct linked_page *p_list; /* list of pages used to store zone
260 * bitmap objects and bitmap block
261 * objects
263 struct bm_position cur; /* most recently used bit position */
266 /* Functions that operate on memory bitmaps */
268 static void memory_bm_position_reset(struct memory_bitmap *bm)
270 bm->cur.block = list_entry(bm->blocks.next, struct bm_block, hook);
271 bm->cur.bit = 0;
274 static void memory_bm_free(struct memory_bitmap *bm, int clear_nosave_free);
277 * create_bm_block_list - create a list of block bitmap objects
278 * @pages - number of pages to track
279 * @list - list to put the allocated blocks into
280 * @ca - chain allocator to be used for allocating memory
282 static int create_bm_block_list(unsigned long pages,
283 struct list_head *list,
284 struct chain_allocator *ca)
286 unsigned int nr_blocks = DIV_ROUND_UP(pages, BM_BITS_PER_BLOCK);
288 while (nr_blocks-- > 0) {
289 struct bm_block *bb;
291 bb = chain_alloc(ca, sizeof(struct bm_block));
292 if (!bb)
293 return -ENOMEM;
294 list_add(&bb->hook, list);
297 return 0;
300 struct mem_extent {
301 struct list_head hook;
302 unsigned long start;
303 unsigned long end;
307 * free_mem_extents - free a list of memory extents
308 * @list - list of extents to empty
310 static void free_mem_extents(struct list_head *list)
312 struct mem_extent *ext, *aux;
314 list_for_each_entry_safe(ext, aux, list, hook) {
315 list_del(&ext->hook);
316 kfree(ext);
321 * create_mem_extents - create a list of memory extents representing
322 * contiguous ranges of PFNs
323 * @list - list to put the extents into
324 * @gfp_mask - mask to use for memory allocations
326 static int create_mem_extents(struct list_head *list, gfp_t gfp_mask)
328 struct zone *zone;
330 INIT_LIST_HEAD(list);
332 for_each_populated_zone(zone) {
333 unsigned long zone_start, zone_end;
334 struct mem_extent *ext, *cur, *aux;
336 zone_start = zone->zone_start_pfn;
337 zone_end = zone->zone_start_pfn + zone->spanned_pages;
339 list_for_each_entry(ext, list, hook)
340 if (zone_start <= ext->end)
341 break;
343 if (&ext->hook == list || zone_end < ext->start) {
344 /* New extent is necessary */
345 struct mem_extent *new_ext;
347 new_ext = kzalloc(sizeof(struct mem_extent), gfp_mask);
348 if (!new_ext) {
349 free_mem_extents(list);
350 return -ENOMEM;
352 new_ext->start = zone_start;
353 new_ext->end = zone_end;
354 list_add_tail(&new_ext->hook, &ext->hook);
355 continue;
358 /* Merge this zone's range of PFNs with the existing one */
359 if (zone_start < ext->start)
360 ext->start = zone_start;
361 if (zone_end > ext->end)
362 ext->end = zone_end;
364 /* More merging may be possible */
365 cur = ext;
366 list_for_each_entry_safe_continue(cur, aux, list, hook) {
367 if (zone_end < cur->start)
368 break;
369 if (zone_end < cur->end)
370 ext->end = cur->end;
371 list_del(&cur->hook);
372 kfree(cur);
376 return 0;
380 * memory_bm_create - allocate memory for a memory bitmap
382 static int
383 memory_bm_create(struct memory_bitmap *bm, gfp_t gfp_mask, int safe_needed)
385 struct chain_allocator ca;
386 struct list_head mem_extents;
387 struct mem_extent *ext;
388 int error;
390 chain_init(&ca, gfp_mask, safe_needed);
391 INIT_LIST_HEAD(&bm->blocks);
393 error = create_mem_extents(&mem_extents, gfp_mask);
394 if (error)
395 return error;
397 list_for_each_entry(ext, &mem_extents, hook) {
398 struct bm_block *bb;
399 unsigned long pfn = ext->start;
400 unsigned long pages = ext->end - ext->start;
402 bb = list_entry(bm->blocks.prev, struct bm_block, hook);
404 error = create_bm_block_list(pages, bm->blocks.prev, &ca);
405 if (error)
406 goto Error;
408 list_for_each_entry_continue(bb, &bm->blocks, hook) {
409 bb->data = get_image_page(gfp_mask, safe_needed);
410 if (!bb->data) {
411 error = -ENOMEM;
412 goto Error;
415 bb->start_pfn = pfn;
416 if (pages >= BM_BITS_PER_BLOCK) {
417 pfn += BM_BITS_PER_BLOCK;
418 pages -= BM_BITS_PER_BLOCK;
419 } else {
420 /* This is executed only once in the loop */
421 pfn += pages;
423 bb->end_pfn = pfn;
427 bm->p_list = ca.chain;
428 memory_bm_position_reset(bm);
429 Exit:
430 free_mem_extents(&mem_extents);
431 return error;
433 Error:
434 bm->p_list = ca.chain;
435 memory_bm_free(bm, PG_UNSAFE_CLEAR);
436 goto Exit;
440 * memory_bm_free - free memory occupied by the memory bitmap @bm
442 static void memory_bm_free(struct memory_bitmap *bm, int clear_nosave_free)
444 struct bm_block *bb;
446 list_for_each_entry(bb, &bm->blocks, hook)
447 if (bb->data)
448 free_image_page(bb->data, clear_nosave_free);
450 free_list_of_pages(bm->p_list, clear_nosave_free);
452 INIT_LIST_HEAD(&bm->blocks);
456 * memory_bm_find_bit - find the bit in the bitmap @bm that corresponds
457 * to given pfn. The cur_zone_bm member of @bm and the cur_block member
458 * of @bm->cur_zone_bm are updated.
460 static int memory_bm_find_bit(struct memory_bitmap *bm, unsigned long pfn,
461 void **addr, unsigned int *bit_nr)
463 struct bm_block *bb;
466 * Check if the pfn corresponds to the current bitmap block and find
467 * the block where it fits if this is not the case.
469 bb = bm->cur.block;
470 if (pfn < bb->start_pfn)
471 list_for_each_entry_continue_reverse(bb, &bm->blocks, hook)
472 if (pfn >= bb->start_pfn)
473 break;
475 if (pfn >= bb->end_pfn)
476 list_for_each_entry_continue(bb, &bm->blocks, hook)
477 if (pfn >= bb->start_pfn && pfn < bb->end_pfn)
478 break;
480 if (&bb->hook == &bm->blocks)
481 return -EFAULT;
483 /* The block has been found */
484 bm->cur.block = bb;
485 pfn -= bb->start_pfn;
486 bm->cur.bit = pfn + 1;
487 *bit_nr = pfn;
488 *addr = bb->data;
489 return 0;
492 static void memory_bm_set_bit(struct memory_bitmap *bm, unsigned long pfn)
494 void *addr;
495 unsigned int bit;
496 int error;
498 error = memory_bm_find_bit(bm, pfn, &addr, &bit);
499 BUG_ON(error);
500 set_bit(bit, addr);
503 static int mem_bm_set_bit_check(struct memory_bitmap *bm, unsigned long pfn)
505 void *addr;
506 unsigned int bit;
507 int error;
509 error = memory_bm_find_bit(bm, pfn, &addr, &bit);
510 if (!error)
511 set_bit(bit, addr);
512 return error;
515 static void memory_bm_clear_bit(struct memory_bitmap *bm, unsigned long pfn)
517 void *addr;
518 unsigned int bit;
519 int error;
521 error = memory_bm_find_bit(bm, pfn, &addr, &bit);
522 BUG_ON(error);
523 clear_bit(bit, addr);
526 static int memory_bm_test_bit(struct memory_bitmap *bm, unsigned long pfn)
528 void *addr;
529 unsigned int bit;
530 int error;
532 error = memory_bm_find_bit(bm, pfn, &addr, &bit);
533 BUG_ON(error);
534 return test_bit(bit, addr);
537 static bool memory_bm_pfn_present(struct memory_bitmap *bm, unsigned long pfn)
539 void *addr;
540 unsigned int bit;
542 return !memory_bm_find_bit(bm, pfn, &addr, &bit);
546 * memory_bm_next_pfn - find the pfn that corresponds to the next set bit
547 * in the bitmap @bm. If the pfn cannot be found, BM_END_OF_MAP is
548 * returned.
550 * It is required to run memory_bm_position_reset() before the first call to
551 * this function.
554 static unsigned long memory_bm_next_pfn(struct memory_bitmap *bm)
556 struct bm_block *bb;
557 int bit;
559 bb = bm->cur.block;
560 do {
561 bit = bm->cur.bit;
562 bit = find_next_bit(bb->data, bm_block_bits(bb), bit);
563 if (bit < bm_block_bits(bb))
564 goto Return_pfn;
566 bb = list_entry(bb->hook.next, struct bm_block, hook);
567 bm->cur.block = bb;
568 bm->cur.bit = 0;
569 } while (&bb->hook != &bm->blocks);
571 memory_bm_position_reset(bm);
572 return BM_END_OF_MAP;
574 Return_pfn:
575 bm->cur.bit = bit + 1;
576 return bb->start_pfn + bit;
580 * This structure represents a range of page frames the contents of which
581 * should not be saved during the suspend.
584 struct nosave_region {
585 struct list_head list;
586 unsigned long start_pfn;
587 unsigned long end_pfn;
590 static LIST_HEAD(nosave_regions);
593 * register_nosave_region - register a range of page frames the contents
594 * of which should not be saved during the suspend (to be used in the early
595 * initialization code)
598 void __init
599 __register_nosave_region(unsigned long start_pfn, unsigned long end_pfn,
600 int use_kmalloc)
602 struct nosave_region *region;
604 if (start_pfn >= end_pfn)
605 return;
607 if (!list_empty(&nosave_regions)) {
608 /* Try to extend the previous region (they should be sorted) */
609 region = list_entry(nosave_regions.prev,
610 struct nosave_region, list);
611 if (region->end_pfn == start_pfn) {
612 region->end_pfn = end_pfn;
613 goto Report;
616 if (use_kmalloc) {
617 /* during init, this shouldn't fail */
618 region = kmalloc(sizeof(struct nosave_region), GFP_KERNEL);
619 BUG_ON(!region);
620 } else
621 /* This allocation cannot fail */
622 region = alloc_bootmem(sizeof(struct nosave_region));
623 region->start_pfn = start_pfn;
624 region->end_pfn = end_pfn;
625 list_add_tail(&region->list, &nosave_regions);
626 Report:
627 printk(KERN_INFO "PM: Registered nosave memory: %016lx - %016lx\n",
628 start_pfn << PAGE_SHIFT, end_pfn << PAGE_SHIFT);
632 * Set bits in this map correspond to the page frames the contents of which
633 * should not be saved during the suspend.
635 static struct memory_bitmap *forbidden_pages_map;
637 /* Set bits in this map correspond to free page frames. */
638 static struct memory_bitmap *free_pages_map;
641 * Each page frame allocated for creating the image is marked by setting the
642 * corresponding bits in forbidden_pages_map and free_pages_map simultaneously
645 void swsusp_set_page_free(struct page *page)
647 if (free_pages_map)
648 memory_bm_set_bit(free_pages_map, page_to_pfn(page));
651 static int swsusp_page_is_free(struct page *page)
653 return free_pages_map ?
654 memory_bm_test_bit(free_pages_map, page_to_pfn(page)) : 0;
657 void swsusp_unset_page_free(struct page *page)
659 if (free_pages_map)
660 memory_bm_clear_bit(free_pages_map, page_to_pfn(page));
663 static void swsusp_set_page_forbidden(struct page *page)
665 if (forbidden_pages_map)
666 memory_bm_set_bit(forbidden_pages_map, page_to_pfn(page));
669 int swsusp_page_is_forbidden(struct page *page)
671 return forbidden_pages_map ?
672 memory_bm_test_bit(forbidden_pages_map, page_to_pfn(page)) : 0;
675 static void swsusp_unset_page_forbidden(struct page *page)
677 if (forbidden_pages_map)
678 memory_bm_clear_bit(forbidden_pages_map, page_to_pfn(page));
682 * mark_nosave_pages - set bits corresponding to the page frames the
683 * contents of which should not be saved in a given bitmap.
686 static void mark_nosave_pages(struct memory_bitmap *bm)
688 struct nosave_region *region;
690 if (list_empty(&nosave_regions))
691 return;
693 list_for_each_entry(region, &nosave_regions, list) {
694 unsigned long pfn;
696 pr_debug("PM: Marking nosave pages: %016lx - %016lx\n",
697 region->start_pfn << PAGE_SHIFT,
698 region->end_pfn << PAGE_SHIFT);
700 for (pfn = region->start_pfn; pfn < region->end_pfn; pfn++)
701 if (pfn_valid(pfn)) {
703 * It is safe to ignore the result of
704 * mem_bm_set_bit_check() here, since we won't
705 * touch the PFNs for which the error is
706 * returned anyway.
708 mem_bm_set_bit_check(bm, pfn);
714 * create_basic_memory_bitmaps - create bitmaps needed for marking page
715 * frames that should not be saved and free page frames. The pointers
716 * forbidden_pages_map and free_pages_map are only modified if everything
717 * goes well, because we don't want the bits to be used before both bitmaps
718 * are set up.
721 int create_basic_memory_bitmaps(void)
723 struct memory_bitmap *bm1, *bm2;
724 int error = 0;
726 BUG_ON(forbidden_pages_map || free_pages_map);
728 bm1 = kzalloc(sizeof(struct memory_bitmap), GFP_KERNEL);
729 if (!bm1)
730 return -ENOMEM;
732 error = memory_bm_create(bm1, GFP_KERNEL, PG_ANY);
733 if (error)
734 goto Free_first_object;
736 bm2 = kzalloc(sizeof(struct memory_bitmap), GFP_KERNEL);
737 if (!bm2)
738 goto Free_first_bitmap;
740 error = memory_bm_create(bm2, GFP_KERNEL, PG_ANY);
741 if (error)
742 goto Free_second_object;
744 forbidden_pages_map = bm1;
745 free_pages_map = bm2;
746 mark_nosave_pages(forbidden_pages_map);
748 pr_debug("PM: Basic memory bitmaps created\n");
750 return 0;
752 Free_second_object:
753 kfree(bm2);
754 Free_first_bitmap:
755 memory_bm_free(bm1, PG_UNSAFE_CLEAR);
756 Free_first_object:
757 kfree(bm1);
758 return -ENOMEM;
762 * free_basic_memory_bitmaps - free memory bitmaps allocated by
763 * create_basic_memory_bitmaps(). The auxiliary pointers are necessary
764 * so that the bitmaps themselves are not referred to while they are being
765 * freed.
768 void free_basic_memory_bitmaps(void)
770 struct memory_bitmap *bm1, *bm2;
772 BUG_ON(!(forbidden_pages_map && free_pages_map));
774 bm1 = forbidden_pages_map;
775 bm2 = free_pages_map;
776 forbidden_pages_map = NULL;
777 free_pages_map = NULL;
778 memory_bm_free(bm1, PG_UNSAFE_CLEAR);
779 kfree(bm1);
780 memory_bm_free(bm2, PG_UNSAFE_CLEAR);
781 kfree(bm2);
783 pr_debug("PM: Basic memory bitmaps freed\n");
787 * snapshot_additional_pages - estimate the number of additional pages
788 * be needed for setting up the suspend image data structures for given
789 * zone (usually the returned value is greater than the exact number)
792 unsigned int snapshot_additional_pages(struct zone *zone)
794 unsigned int res;
796 res = DIV_ROUND_UP(zone->spanned_pages, BM_BITS_PER_BLOCK);
797 res += DIV_ROUND_UP(res * sizeof(struct bm_block), PAGE_SIZE);
798 return 2 * res;
801 #ifdef CONFIG_HIGHMEM
803 * count_free_highmem_pages - compute the total number of free highmem
804 * pages, system-wide.
807 static unsigned int count_free_highmem_pages(void)
809 struct zone *zone;
810 unsigned int cnt = 0;
812 for_each_populated_zone(zone)
813 if (is_highmem(zone))
814 cnt += zone_page_state(zone, NR_FREE_PAGES);
816 return cnt;
820 * saveable_highmem_page - Determine whether a highmem page should be
821 * included in the suspend image.
823 * We should save the page if it isn't Nosave or NosaveFree, or Reserved,
824 * and it isn't a part of a free chunk of pages.
826 static struct page *saveable_highmem_page(struct zone *zone, unsigned long pfn)
828 struct page *page;
830 if (!pfn_valid(pfn))
831 return NULL;
833 page = pfn_to_page(pfn);
834 if (page_zone(page) != zone)
835 return NULL;
837 BUG_ON(!PageHighMem(page));
839 if (swsusp_page_is_forbidden(page) || swsusp_page_is_free(page) ||
840 PageReserved(page))
841 return NULL;
843 return page;
847 * count_highmem_pages - compute the total number of saveable highmem
848 * pages.
851 static unsigned int count_highmem_pages(void)
853 struct zone *zone;
854 unsigned int n = 0;
856 for_each_populated_zone(zone) {
857 unsigned long pfn, max_zone_pfn;
859 if (!is_highmem(zone))
860 continue;
862 mark_free_pages(zone);
863 max_zone_pfn = zone->zone_start_pfn + zone->spanned_pages;
864 for (pfn = zone->zone_start_pfn; pfn < max_zone_pfn; pfn++)
865 if (saveable_highmem_page(zone, pfn))
866 n++;
868 return n;
870 #else
871 static inline void *saveable_highmem_page(struct zone *z, unsigned long p)
873 return NULL;
875 #endif /* CONFIG_HIGHMEM */
878 * saveable_page - Determine whether a non-highmem page should be included
879 * in the suspend image.
881 * We should save the page if it isn't Nosave, and is not in the range
882 * of pages statically defined as 'unsaveable', and it isn't a part of
883 * a free chunk of pages.
885 static struct page *saveable_page(struct zone *zone, unsigned long pfn)
887 struct page *page;
889 if (!pfn_valid(pfn))
890 return NULL;
892 page = pfn_to_page(pfn);
893 if (page_zone(page) != zone)
894 return NULL;
896 BUG_ON(PageHighMem(page));
898 if (swsusp_page_is_forbidden(page) || swsusp_page_is_free(page))
899 return NULL;
901 if (PageReserved(page)
902 && (!kernel_page_present(page) || pfn_is_nosave(pfn)))
903 return NULL;
905 return page;
909 * count_data_pages - compute the total number of saveable non-highmem
910 * pages.
913 static unsigned int count_data_pages(void)
915 struct zone *zone;
916 unsigned long pfn, max_zone_pfn;
917 unsigned int n = 0;
919 for_each_populated_zone(zone) {
920 if (is_highmem(zone))
921 continue;
923 mark_free_pages(zone);
924 max_zone_pfn = zone->zone_start_pfn + zone->spanned_pages;
925 for (pfn = zone->zone_start_pfn; pfn < max_zone_pfn; pfn++)
926 if (saveable_page(zone, pfn))
927 n++;
929 return n;
932 /* This is needed, because copy_page and memcpy are not usable for copying
933 * task structs.
935 static inline void do_copy_page(long *dst, long *src)
937 int n;
939 for (n = PAGE_SIZE / sizeof(long); n; n--)
940 *dst++ = *src++;
945 * safe_copy_page - check if the page we are going to copy is marked as
946 * present in the kernel page tables (this always is the case if
947 * CONFIG_DEBUG_PAGEALLOC is not set and in that case
948 * kernel_page_present() always returns 'true').
950 static void safe_copy_page(void *dst, struct page *s_page)
952 if (kernel_page_present(s_page)) {
953 do_copy_page(dst, page_address(s_page));
954 } else {
955 kernel_map_pages(s_page, 1, 1);
956 do_copy_page(dst, page_address(s_page));
957 kernel_map_pages(s_page, 1, 0);
962 #ifdef CONFIG_HIGHMEM
963 static inline struct page *
964 page_is_saveable(struct zone *zone, unsigned long pfn)
966 return is_highmem(zone) ?
967 saveable_highmem_page(zone, pfn) : saveable_page(zone, pfn);
970 static void copy_data_page(unsigned long dst_pfn, unsigned long src_pfn)
972 struct page *s_page, *d_page;
973 void *src, *dst;
975 s_page = pfn_to_page(src_pfn);
976 d_page = pfn_to_page(dst_pfn);
977 if (PageHighMem(s_page)) {
978 src = kmap_atomic(s_page, KM_USER0);
979 dst = kmap_atomic(d_page, KM_USER1);
980 do_copy_page(dst, src);
981 kunmap_atomic(src, KM_USER0);
982 kunmap_atomic(dst, KM_USER1);
983 } else {
984 if (PageHighMem(d_page)) {
985 /* Page pointed to by src may contain some kernel
986 * data modified by kmap_atomic()
988 safe_copy_page(buffer, s_page);
989 dst = kmap_atomic(d_page, KM_USER0);
990 memcpy(dst, buffer, PAGE_SIZE);
991 kunmap_atomic(dst, KM_USER0);
992 } else {
993 safe_copy_page(page_address(d_page), s_page);
997 #else
998 #define page_is_saveable(zone, pfn) saveable_page(zone, pfn)
1000 static inline void copy_data_page(unsigned long dst_pfn, unsigned long src_pfn)
1002 safe_copy_page(page_address(pfn_to_page(dst_pfn)),
1003 pfn_to_page(src_pfn));
1005 #endif /* CONFIG_HIGHMEM */
1007 static void
1008 copy_data_pages(struct memory_bitmap *copy_bm, struct memory_bitmap *orig_bm)
1010 struct zone *zone;
1011 unsigned long pfn;
1013 for_each_populated_zone(zone) {
1014 unsigned long max_zone_pfn;
1016 mark_free_pages(zone);
1017 max_zone_pfn = zone->zone_start_pfn + zone->spanned_pages;
1018 for (pfn = zone->zone_start_pfn; pfn < max_zone_pfn; pfn++)
1019 if (page_is_saveable(zone, pfn))
1020 memory_bm_set_bit(orig_bm, pfn);
1022 memory_bm_position_reset(orig_bm);
1023 memory_bm_position_reset(copy_bm);
1024 for(;;) {
1025 pfn = memory_bm_next_pfn(orig_bm);
1026 if (unlikely(pfn == BM_END_OF_MAP))
1027 break;
1028 copy_data_page(memory_bm_next_pfn(copy_bm), pfn);
1032 /* Total number of image pages */
1033 static unsigned int nr_copy_pages;
1034 /* Number of pages needed for saving the original pfns of the image pages */
1035 static unsigned int nr_meta_pages;
1037 * Numbers of normal and highmem page frames allocated for hibernation image
1038 * before suspending devices.
1040 unsigned int alloc_normal, alloc_highmem;
1042 * Memory bitmap used for marking saveable pages (during hibernation) or
1043 * hibernation image pages (during restore)
1045 static struct memory_bitmap orig_bm;
1047 * Memory bitmap used during hibernation for marking allocated page frames that
1048 * will contain copies of saveable pages. During restore it is initially used
1049 * for marking hibernation image pages, but then the set bits from it are
1050 * duplicated in @orig_bm and it is released. On highmem systems it is next
1051 * used for marking "safe" highmem pages, but it has to be reinitialized for
1052 * this purpose.
1054 static struct memory_bitmap copy_bm;
1057 * swsusp_free - free pages allocated for the suspend.
1059 * Suspend pages are alocated before the atomic copy is made, so we
1060 * need to release them after the resume.
1063 void swsusp_free(void)
1065 struct zone *zone;
1066 unsigned long pfn, max_zone_pfn;
1068 for_each_populated_zone(zone) {
1069 max_zone_pfn = zone->zone_start_pfn + zone->spanned_pages;
1070 for (pfn = zone->zone_start_pfn; pfn < max_zone_pfn; pfn++)
1071 if (pfn_valid(pfn)) {
1072 struct page *page = pfn_to_page(pfn);
1074 if (swsusp_page_is_forbidden(page) &&
1075 swsusp_page_is_free(page)) {
1076 swsusp_unset_page_forbidden(page);
1077 swsusp_unset_page_free(page);
1078 __free_page(page);
1082 nr_copy_pages = 0;
1083 nr_meta_pages = 0;
1084 restore_pblist = NULL;
1085 buffer = NULL;
1086 alloc_normal = 0;
1087 alloc_highmem = 0;
1090 /* Helper functions used for the shrinking of memory. */
1092 #define GFP_IMAGE (GFP_KERNEL | __GFP_NOWARN)
1095 * preallocate_image_pages - Allocate a number of pages for hibernation image
1096 * @nr_pages: Number of page frames to allocate.
1097 * @mask: GFP flags to use for the allocation.
1099 * Return value: Number of page frames actually allocated
1101 static unsigned long preallocate_image_pages(unsigned long nr_pages, gfp_t mask)
1103 unsigned long nr_alloc = 0;
1105 while (nr_pages > 0) {
1106 struct page *page;
1108 page = alloc_image_page(mask);
1109 if (!page)
1110 break;
1111 memory_bm_set_bit(&copy_bm, page_to_pfn(page));
1112 if (PageHighMem(page))
1113 alloc_highmem++;
1114 else
1115 alloc_normal++;
1116 nr_pages--;
1117 nr_alloc++;
1120 return nr_alloc;
1123 static unsigned long preallocate_image_memory(unsigned long nr_pages,
1124 unsigned long avail_normal)
1126 unsigned long alloc;
1128 if (avail_normal <= alloc_normal)
1129 return 0;
1131 alloc = avail_normal - alloc_normal;
1132 if (nr_pages < alloc)
1133 alloc = nr_pages;
1135 return preallocate_image_pages(alloc, GFP_IMAGE);
1138 #ifdef CONFIG_HIGHMEM
1139 static unsigned long preallocate_image_highmem(unsigned long nr_pages)
1141 return preallocate_image_pages(nr_pages, GFP_IMAGE | __GFP_HIGHMEM);
1145 * __fraction - Compute (an approximation of) x * (multiplier / base)
1147 static unsigned long __fraction(u64 x, u64 multiplier, u64 base)
1149 x *= multiplier;
1150 do_div(x, base);
1151 return (unsigned long)x;
1154 static unsigned long preallocate_highmem_fraction(unsigned long nr_pages,
1155 unsigned long highmem,
1156 unsigned long total)
1158 unsigned long alloc = __fraction(nr_pages, highmem, total);
1160 return preallocate_image_pages(alloc, GFP_IMAGE | __GFP_HIGHMEM);
1162 #else /* CONFIG_HIGHMEM */
1163 static inline unsigned long preallocate_image_highmem(unsigned long nr_pages)
1165 return 0;
1168 static inline unsigned long preallocate_highmem_fraction(unsigned long nr_pages,
1169 unsigned long highmem,
1170 unsigned long total)
1172 return 0;
1174 #endif /* CONFIG_HIGHMEM */
1177 * free_unnecessary_pages - Release preallocated pages not needed for the image
1179 static void free_unnecessary_pages(void)
1181 unsigned long save, to_free_normal, to_free_highmem;
1183 save = count_data_pages();
1184 if (alloc_normal >= save) {
1185 to_free_normal = alloc_normal - save;
1186 save = 0;
1187 } else {
1188 to_free_normal = 0;
1189 save -= alloc_normal;
1191 save += count_highmem_pages();
1192 if (alloc_highmem >= save) {
1193 to_free_highmem = alloc_highmem - save;
1194 } else {
1195 to_free_highmem = 0;
1196 save -= alloc_highmem;
1197 if (to_free_normal > save)
1198 to_free_normal -= save;
1199 else
1200 to_free_normal = 0;
1203 memory_bm_position_reset(&copy_bm);
1205 while (to_free_normal > 0 || to_free_highmem > 0) {
1206 unsigned long pfn = memory_bm_next_pfn(&copy_bm);
1207 struct page *page = pfn_to_page(pfn);
1209 if (PageHighMem(page)) {
1210 if (!to_free_highmem)
1211 continue;
1212 to_free_highmem--;
1213 alloc_highmem--;
1214 } else {
1215 if (!to_free_normal)
1216 continue;
1217 to_free_normal--;
1218 alloc_normal--;
1220 memory_bm_clear_bit(&copy_bm, pfn);
1221 swsusp_unset_page_forbidden(page);
1222 swsusp_unset_page_free(page);
1223 __free_page(page);
1228 * minimum_image_size - Estimate the minimum acceptable size of an image
1229 * @saveable: Number of saveable pages in the system.
1231 * We want to avoid attempting to free too much memory too hard, so estimate the
1232 * minimum acceptable size of a hibernation image to use as the lower limit for
1233 * preallocating memory.
1235 * We assume that the minimum image size should be proportional to
1237 * [number of saveable pages] - [number of pages that can be freed in theory]
1239 * where the second term is the sum of (1) reclaimable slab pages, (2) active
1240 * and (3) inactive anonymouns pages, (4) active and (5) inactive file pages,
1241 * minus mapped file pages.
1243 static unsigned long minimum_image_size(unsigned long saveable)
1245 unsigned long size;
1247 size = global_page_state(NR_SLAB_RECLAIMABLE)
1248 + global_page_state(NR_ACTIVE_ANON)
1249 + global_page_state(NR_INACTIVE_ANON)
1250 + global_page_state(NR_ACTIVE_FILE)
1251 + global_page_state(NR_INACTIVE_FILE)
1252 - global_page_state(NR_FILE_MAPPED);
1254 return saveable <= size ? 0 : saveable - size;
1258 * hibernate_preallocate_memory - Preallocate memory for hibernation image
1260 * To create a hibernation image it is necessary to make a copy of every page
1261 * frame in use. We also need a number of page frames to be free during
1262 * hibernation for allocations made while saving the image and for device
1263 * drivers, in case they need to allocate memory from their hibernation
1264 * callbacks (these two numbers are given by PAGES_FOR_IO and SPARE_PAGES,
1265 * respectively, both of which are rough estimates). To make this happen, we
1266 * compute the total number of available page frames and allocate at least
1268 * ([page frames total] + PAGES_FOR_IO + [metadata pages]) / 2 + 2 * SPARE_PAGES
1270 * of them, which corresponds to the maximum size of a hibernation image.
1272 * If image_size is set below the number following from the above formula,
1273 * the preallocation of memory is continued until the total number of saveable
1274 * pages in the system is below the requested image size or the minimum
1275 * acceptable image size returned by minimum_image_size(), whichever is greater.
1277 int hibernate_preallocate_memory(void)
1279 struct zone *zone;
1280 unsigned long saveable, size, max_size, count, highmem, pages = 0;
1281 unsigned long alloc, save_highmem, pages_highmem, avail_normal;
1282 struct timeval start, stop;
1283 int error;
1285 printk(KERN_INFO "PM: Preallocating image memory... ");
1286 do_gettimeofday(&start);
1288 error = memory_bm_create(&orig_bm, GFP_IMAGE, PG_ANY);
1289 if (error)
1290 goto err_out;
1292 error = memory_bm_create(&copy_bm, GFP_IMAGE, PG_ANY);
1293 if (error)
1294 goto err_out;
1296 alloc_normal = 0;
1297 alloc_highmem = 0;
1299 /* Count the number of saveable data pages. */
1300 save_highmem = count_highmem_pages();
1301 saveable = count_data_pages();
1304 * Compute the total number of page frames we can use (count) and the
1305 * number of pages needed for image metadata (size).
1307 count = saveable;
1308 saveable += save_highmem;
1309 highmem = save_highmem;
1310 size = 0;
1311 for_each_populated_zone(zone) {
1312 size += snapshot_additional_pages(zone);
1313 if (is_highmem(zone))
1314 highmem += zone_page_state(zone, NR_FREE_PAGES);
1315 else
1316 count += zone_page_state(zone, NR_FREE_PAGES);
1318 avail_normal = count;
1319 count += highmem;
1320 count -= totalreserve_pages;
1322 /* Compute the maximum number of saveable pages to leave in memory. */
1323 max_size = (count - (size + PAGES_FOR_IO)) / 2 - 2 * SPARE_PAGES;
1324 size = DIV_ROUND_UP(image_size, PAGE_SIZE);
1325 if (size > max_size)
1326 size = max_size;
1328 * If the maximum is not less than the current number of saveable pages
1329 * in memory, allocate page frames for the image and we're done.
1331 if (size >= saveable) {
1332 pages = preallocate_image_highmem(save_highmem);
1333 pages += preallocate_image_memory(saveable - pages, avail_normal);
1334 goto out;
1337 /* Estimate the minimum size of the image. */
1338 pages = minimum_image_size(saveable);
1340 * To avoid excessive pressure on the normal zone, leave room in it to
1341 * accommodate an image of the minimum size (unless it's already too
1342 * small, in which case don't preallocate pages from it at all).
1344 if (avail_normal > pages)
1345 avail_normal -= pages;
1346 else
1347 avail_normal = 0;
1348 if (size < pages)
1349 size = min_t(unsigned long, pages, max_size);
1352 * Let the memory management subsystem know that we're going to need a
1353 * large number of page frames to allocate and make it free some memory.
1354 * NOTE: If this is not done, performance will be hurt badly in some
1355 * test cases.
1357 shrink_all_memory(saveable - size);
1360 * The number of saveable pages in memory was too high, so apply some
1361 * pressure to decrease it. First, make room for the largest possible
1362 * image and fail if that doesn't work. Next, try to decrease the size
1363 * of the image as much as indicated by 'size' using allocations from
1364 * highmem and non-highmem zones separately.
1366 pages_highmem = preallocate_image_highmem(highmem / 2);
1367 alloc = (count - max_size) - pages_highmem;
1368 pages = preallocate_image_memory(alloc, avail_normal);
1369 if (pages < alloc) {
1370 /* We have exhausted non-highmem pages, try highmem. */
1371 alloc -= pages;
1372 pages += pages_highmem;
1373 pages_highmem = preallocate_image_highmem(alloc);
1374 if (pages_highmem < alloc)
1375 goto err_out;
1376 pages += pages_highmem;
1378 * size is the desired number of saveable pages to leave in
1379 * memory, so try to preallocate (all memory - size) pages.
1381 alloc = (count - pages) - size;
1382 pages += preallocate_image_highmem(alloc);
1383 } else {
1385 * There are approximately max_size saveable pages at this point
1386 * and we want to reduce this number down to size.
1388 alloc = max_size - size;
1389 size = preallocate_highmem_fraction(alloc, highmem, count);
1390 pages_highmem += size;
1391 alloc -= size;
1392 size = preallocate_image_memory(alloc, avail_normal);
1393 pages_highmem += preallocate_image_highmem(alloc - size);
1394 pages += pages_highmem + size;
1398 * We only need as many page frames for the image as there are saveable
1399 * pages in memory, but we have allocated more. Release the excessive
1400 * ones now.
1402 free_unnecessary_pages();
1404 out:
1405 do_gettimeofday(&stop);
1406 printk(KERN_CONT "done (allocated %lu pages)\n", pages);
1407 swsusp_show_speed(&start, &stop, pages, "Allocated");
1409 return 0;
1411 err_out:
1412 printk(KERN_CONT "\n");
1413 swsusp_free();
1414 return -ENOMEM;
1417 #ifdef CONFIG_HIGHMEM
1419 * count_pages_for_highmem - compute the number of non-highmem pages
1420 * that will be necessary for creating copies of highmem pages.
1423 static unsigned int count_pages_for_highmem(unsigned int nr_highmem)
1425 unsigned int free_highmem = count_free_highmem_pages() + alloc_highmem;
1427 if (free_highmem >= nr_highmem)
1428 nr_highmem = 0;
1429 else
1430 nr_highmem -= free_highmem;
1432 return nr_highmem;
1434 #else
1435 static unsigned int
1436 count_pages_for_highmem(unsigned int nr_highmem) { return 0; }
1437 #endif /* CONFIG_HIGHMEM */
1440 * enough_free_mem - Make sure we have enough free memory for the
1441 * snapshot image.
1444 static int enough_free_mem(unsigned int nr_pages, unsigned int nr_highmem)
1446 struct zone *zone;
1447 unsigned int free = alloc_normal;
1449 for_each_populated_zone(zone)
1450 if (!is_highmem(zone))
1451 free += zone_page_state(zone, NR_FREE_PAGES);
1453 nr_pages += count_pages_for_highmem(nr_highmem);
1454 pr_debug("PM: Normal pages needed: %u + %u, available pages: %u\n",
1455 nr_pages, PAGES_FOR_IO, free);
1457 return free > nr_pages + PAGES_FOR_IO;
1460 #ifdef CONFIG_HIGHMEM
1462 * get_highmem_buffer - if there are some highmem pages in the suspend
1463 * image, we may need the buffer to copy them and/or load their data.
1466 static inline int get_highmem_buffer(int safe_needed)
1468 buffer = get_image_page(GFP_ATOMIC | __GFP_COLD, safe_needed);
1469 return buffer ? 0 : -ENOMEM;
1473 * alloc_highmem_image_pages - allocate some highmem pages for the image.
1474 * Try to allocate as many pages as needed, but if the number of free
1475 * highmem pages is lesser than that, allocate them all.
1478 static inline unsigned int
1479 alloc_highmem_pages(struct memory_bitmap *bm, unsigned int nr_highmem)
1481 unsigned int to_alloc = count_free_highmem_pages();
1483 if (to_alloc > nr_highmem)
1484 to_alloc = nr_highmem;
1486 nr_highmem -= to_alloc;
1487 while (to_alloc-- > 0) {
1488 struct page *page;
1490 page = alloc_image_page(__GFP_HIGHMEM);
1491 memory_bm_set_bit(bm, page_to_pfn(page));
1493 return nr_highmem;
1495 #else
1496 static inline int get_highmem_buffer(int safe_needed) { return 0; }
1498 static inline unsigned int
1499 alloc_highmem_pages(struct memory_bitmap *bm, unsigned int n) { return 0; }
1500 #endif /* CONFIG_HIGHMEM */
1503 * swsusp_alloc - allocate memory for the suspend image
1505 * We first try to allocate as many highmem pages as there are
1506 * saveable highmem pages in the system. If that fails, we allocate
1507 * non-highmem pages for the copies of the remaining highmem ones.
1509 * In this approach it is likely that the copies of highmem pages will
1510 * also be located in the high memory, because of the way in which
1511 * copy_data_pages() works.
1514 static int
1515 swsusp_alloc(struct memory_bitmap *orig_bm, struct memory_bitmap *copy_bm,
1516 unsigned int nr_pages, unsigned int nr_highmem)
1518 if (nr_highmem > 0) {
1519 if (get_highmem_buffer(PG_ANY))
1520 goto err_out;
1521 if (nr_highmem > alloc_highmem) {
1522 nr_highmem -= alloc_highmem;
1523 nr_pages += alloc_highmem_pages(copy_bm, nr_highmem);
1526 if (nr_pages > alloc_normal) {
1527 nr_pages -= alloc_normal;
1528 while (nr_pages-- > 0) {
1529 struct page *page;
1531 page = alloc_image_page(GFP_ATOMIC | __GFP_COLD);
1532 if (!page)
1533 goto err_out;
1534 memory_bm_set_bit(copy_bm, page_to_pfn(page));
1538 return 0;
1540 err_out:
1541 swsusp_free();
1542 return -ENOMEM;
1545 asmlinkage int swsusp_save(void)
1547 unsigned int nr_pages, nr_highmem;
1549 printk(KERN_INFO "PM: Creating hibernation image: \n");
1551 drain_local_pages(NULL);
1552 nr_pages = count_data_pages();
1553 nr_highmem = count_highmem_pages();
1554 printk(KERN_INFO "PM: Need to copy %u pages\n", nr_pages + nr_highmem);
1556 if (!enough_free_mem(nr_pages, nr_highmem)) {
1557 printk(KERN_ERR "PM: Not enough free memory\n");
1558 return -ENOMEM;
1561 if (swsusp_alloc(&orig_bm, &copy_bm, nr_pages, nr_highmem)) {
1562 printk(KERN_ERR "PM: Memory allocation failed\n");
1563 return -ENOMEM;
1566 /* During allocating of suspend pagedir, new cold pages may appear.
1567 * Kill them.
1569 drain_local_pages(NULL);
1570 copy_data_pages(&copy_bm, &orig_bm);
1573 * End of critical section. From now on, we can write to memory,
1574 * but we should not touch disk. This specially means we must _not_
1575 * touch swap space! Except we must write out our image of course.
1578 nr_pages += nr_highmem;
1579 nr_copy_pages = nr_pages;
1580 nr_meta_pages = DIV_ROUND_UP(nr_pages * sizeof(long), PAGE_SIZE);
1582 printk(KERN_INFO "PM: Hibernation image created (%d pages copied)\n",
1583 nr_pages);
1585 return 0;
1588 #ifndef CONFIG_ARCH_HIBERNATION_HEADER
1589 static int init_header_complete(struct swsusp_info *info)
1591 memcpy(&info->uts, init_utsname(), sizeof(struct new_utsname));
1592 info->version_code = LINUX_VERSION_CODE;
1593 return 0;
1596 static char *check_image_kernel(struct swsusp_info *info)
1598 if (info->version_code != LINUX_VERSION_CODE)
1599 return "kernel version";
1600 if (strcmp(info->uts.sysname,init_utsname()->sysname))
1601 return "system type";
1602 if (strcmp(info->uts.release,init_utsname()->release))
1603 return "kernel release";
1604 if (strcmp(info->uts.version,init_utsname()->version))
1605 return "version";
1606 if (strcmp(info->uts.machine,init_utsname()->machine))
1607 return "machine";
1608 return NULL;
1610 #endif /* CONFIG_ARCH_HIBERNATION_HEADER */
1612 unsigned long snapshot_get_image_size(void)
1614 return nr_copy_pages + nr_meta_pages + 1;
1617 static int init_header(struct swsusp_info *info)
1619 memset(info, 0, sizeof(struct swsusp_info));
1620 info->num_physpages = num_physpages;
1621 info->image_pages = nr_copy_pages;
1622 info->pages = snapshot_get_image_size();
1623 info->size = info->pages;
1624 info->size <<= PAGE_SHIFT;
1625 return init_header_complete(info);
1629 * pack_pfns - pfns corresponding to the set bits found in the bitmap @bm
1630 * are stored in the array @buf[] (1 page at a time)
1633 static inline void
1634 pack_pfns(unsigned long *buf, struct memory_bitmap *bm)
1636 int j;
1638 for (j = 0; j < PAGE_SIZE / sizeof(long); j++) {
1639 buf[j] = memory_bm_next_pfn(bm);
1640 if (unlikely(buf[j] == BM_END_OF_MAP))
1641 break;
1646 * snapshot_read_next - used for reading the system memory snapshot.
1648 * On the first call to it @handle should point to a zeroed
1649 * snapshot_handle structure. The structure gets updated and a pointer
1650 * to it should be passed to this function every next time.
1652 * The @count parameter should contain the number of bytes the caller
1653 * wants to read from the snapshot. It must not be zero.
1655 * On success the function returns a positive number. Then, the caller
1656 * is allowed to read up to the returned number of bytes from the memory
1657 * location computed by the data_of() macro. The number returned
1658 * may be smaller than @count, but this only happens if the read would
1659 * cross a page boundary otherwise.
1661 * The function returns 0 to indicate the end of data stream condition,
1662 * and a negative number is returned on error. In such cases the
1663 * structure pointed to by @handle is not updated and should not be used
1664 * any more.
1667 int snapshot_read_next(struct snapshot_handle *handle, size_t count)
1669 if (handle->cur > nr_meta_pages + nr_copy_pages)
1670 return 0;
1672 if (!buffer) {
1673 /* This makes the buffer be freed by swsusp_free() */
1674 buffer = get_image_page(GFP_ATOMIC, PG_ANY);
1675 if (!buffer)
1676 return -ENOMEM;
1678 if (!handle->offset) {
1679 int error;
1681 error = init_header((struct swsusp_info *)buffer);
1682 if (error)
1683 return error;
1684 handle->buffer = buffer;
1685 memory_bm_position_reset(&orig_bm);
1686 memory_bm_position_reset(&copy_bm);
1688 if (handle->prev < handle->cur) {
1689 if (handle->cur <= nr_meta_pages) {
1690 memset(buffer, 0, PAGE_SIZE);
1691 pack_pfns(buffer, &orig_bm);
1692 } else {
1693 struct page *page;
1695 page = pfn_to_page(memory_bm_next_pfn(&copy_bm));
1696 if (PageHighMem(page)) {
1697 /* Highmem pages are copied to the buffer,
1698 * because we can't return with a kmapped
1699 * highmem page (we may not be called again).
1701 void *kaddr;
1703 kaddr = kmap_atomic(page, KM_USER0);
1704 memcpy(buffer, kaddr, PAGE_SIZE);
1705 kunmap_atomic(kaddr, KM_USER0);
1706 handle->buffer = buffer;
1707 } else {
1708 handle->buffer = page_address(page);
1711 handle->prev = handle->cur;
1713 handle->buf_offset = handle->cur_offset;
1714 if (handle->cur_offset + count >= PAGE_SIZE) {
1715 count = PAGE_SIZE - handle->cur_offset;
1716 handle->cur_offset = 0;
1717 handle->cur++;
1718 } else {
1719 handle->cur_offset += count;
1721 handle->offset += count;
1722 return count;
1726 * mark_unsafe_pages - mark the pages that cannot be used for storing
1727 * the image during resume, because they conflict with the pages that
1728 * had been used before suspend
1731 static int mark_unsafe_pages(struct memory_bitmap *bm)
1733 struct zone *zone;
1734 unsigned long pfn, max_zone_pfn;
1736 /* Clear page flags */
1737 for_each_populated_zone(zone) {
1738 max_zone_pfn = zone->zone_start_pfn + zone->spanned_pages;
1739 for (pfn = zone->zone_start_pfn; pfn < max_zone_pfn; pfn++)
1740 if (pfn_valid(pfn))
1741 swsusp_unset_page_free(pfn_to_page(pfn));
1744 /* Mark pages that correspond to the "original" pfns as "unsafe" */
1745 memory_bm_position_reset(bm);
1746 do {
1747 pfn = memory_bm_next_pfn(bm);
1748 if (likely(pfn != BM_END_OF_MAP)) {
1749 if (likely(pfn_valid(pfn)))
1750 swsusp_set_page_free(pfn_to_page(pfn));
1751 else
1752 return -EFAULT;
1754 } while (pfn != BM_END_OF_MAP);
1756 allocated_unsafe_pages = 0;
1758 return 0;
1761 static void
1762 duplicate_memory_bitmap(struct memory_bitmap *dst, struct memory_bitmap *src)
1764 unsigned long pfn;
1766 memory_bm_position_reset(src);
1767 pfn = memory_bm_next_pfn(src);
1768 while (pfn != BM_END_OF_MAP) {
1769 memory_bm_set_bit(dst, pfn);
1770 pfn = memory_bm_next_pfn(src);
1774 static int check_header(struct swsusp_info *info)
1776 char *reason;
1778 reason = check_image_kernel(info);
1779 if (!reason && info->num_physpages != num_physpages)
1780 reason = "memory size";
1781 if (reason) {
1782 printk(KERN_ERR "PM: Image mismatch: %s\n", reason);
1783 return -EPERM;
1785 return 0;
1789 * load header - check the image header and copy data from it
1792 static int
1793 load_header(struct swsusp_info *info)
1795 int error;
1797 restore_pblist = NULL;
1798 error = check_header(info);
1799 if (!error) {
1800 nr_copy_pages = info->image_pages;
1801 nr_meta_pages = info->pages - info->image_pages - 1;
1803 return error;
1807 * unpack_orig_pfns - for each element of @buf[] (1 page at a time) set
1808 * the corresponding bit in the memory bitmap @bm
1810 static int unpack_orig_pfns(unsigned long *buf, struct memory_bitmap *bm)
1812 int j;
1814 for (j = 0; j < PAGE_SIZE / sizeof(long); j++) {
1815 if (unlikely(buf[j] == BM_END_OF_MAP))
1816 break;
1818 if (memory_bm_pfn_present(bm, buf[j]))
1819 memory_bm_set_bit(bm, buf[j]);
1820 else
1821 return -EFAULT;
1824 return 0;
1827 /* List of "safe" pages that may be used to store data loaded from the suspend
1828 * image
1830 static struct linked_page *safe_pages_list;
1832 #ifdef CONFIG_HIGHMEM
1833 /* struct highmem_pbe is used for creating the list of highmem pages that
1834 * should be restored atomically during the resume from disk, because the page
1835 * frames they have occupied before the suspend are in use.
1837 struct highmem_pbe {
1838 struct page *copy_page; /* data is here now */
1839 struct page *orig_page; /* data was here before the suspend */
1840 struct highmem_pbe *next;
1843 /* List of highmem PBEs needed for restoring the highmem pages that were
1844 * allocated before the suspend and included in the suspend image, but have
1845 * also been allocated by the "resume" kernel, so their contents cannot be
1846 * written directly to their "original" page frames.
1848 static struct highmem_pbe *highmem_pblist;
1851 * count_highmem_image_pages - compute the number of highmem pages in the
1852 * suspend image. The bits in the memory bitmap @bm that correspond to the
1853 * image pages are assumed to be set.
1856 static unsigned int count_highmem_image_pages(struct memory_bitmap *bm)
1858 unsigned long pfn;
1859 unsigned int cnt = 0;
1861 memory_bm_position_reset(bm);
1862 pfn = memory_bm_next_pfn(bm);
1863 while (pfn != BM_END_OF_MAP) {
1864 if (PageHighMem(pfn_to_page(pfn)))
1865 cnt++;
1867 pfn = memory_bm_next_pfn(bm);
1869 return cnt;
1873 * prepare_highmem_image - try to allocate as many highmem pages as
1874 * there are highmem image pages (@nr_highmem_p points to the variable
1875 * containing the number of highmem image pages). The pages that are
1876 * "safe" (ie. will not be overwritten when the suspend image is
1877 * restored) have the corresponding bits set in @bm (it must be
1878 * unitialized).
1880 * NOTE: This function should not be called if there are no highmem
1881 * image pages.
1884 static unsigned int safe_highmem_pages;
1886 static struct memory_bitmap *safe_highmem_bm;
1888 static int
1889 prepare_highmem_image(struct memory_bitmap *bm, unsigned int *nr_highmem_p)
1891 unsigned int to_alloc;
1893 if (memory_bm_create(bm, GFP_ATOMIC, PG_SAFE))
1894 return -ENOMEM;
1896 if (get_highmem_buffer(PG_SAFE))
1897 return -ENOMEM;
1899 to_alloc = count_free_highmem_pages();
1900 if (to_alloc > *nr_highmem_p)
1901 to_alloc = *nr_highmem_p;
1902 else
1903 *nr_highmem_p = to_alloc;
1905 safe_highmem_pages = 0;
1906 while (to_alloc-- > 0) {
1907 struct page *page;
1909 page = alloc_page(__GFP_HIGHMEM);
1910 if (!swsusp_page_is_free(page)) {
1911 /* The page is "safe", set its bit the bitmap */
1912 memory_bm_set_bit(bm, page_to_pfn(page));
1913 safe_highmem_pages++;
1915 /* Mark the page as allocated */
1916 swsusp_set_page_forbidden(page);
1917 swsusp_set_page_free(page);
1919 memory_bm_position_reset(bm);
1920 safe_highmem_bm = bm;
1921 return 0;
1925 * get_highmem_page_buffer - for given highmem image page find the buffer
1926 * that suspend_write_next() should set for its caller to write to.
1928 * If the page is to be saved to its "original" page frame or a copy of
1929 * the page is to be made in the highmem, @buffer is returned. Otherwise,
1930 * the copy of the page is to be made in normal memory, so the address of
1931 * the copy is returned.
1933 * If @buffer is returned, the caller of suspend_write_next() will write
1934 * the page's contents to @buffer, so they will have to be copied to the
1935 * right location on the next call to suspend_write_next() and it is done
1936 * with the help of copy_last_highmem_page(). For this purpose, if
1937 * @buffer is returned, @last_highmem page is set to the page to which
1938 * the data will have to be copied from @buffer.
1941 static struct page *last_highmem_page;
1943 static void *
1944 get_highmem_page_buffer(struct page *page, struct chain_allocator *ca)
1946 struct highmem_pbe *pbe;
1947 void *kaddr;
1949 if (swsusp_page_is_forbidden(page) && swsusp_page_is_free(page)) {
1950 /* We have allocated the "original" page frame and we can
1951 * use it directly to store the loaded page.
1953 last_highmem_page = page;
1954 return buffer;
1956 /* The "original" page frame has not been allocated and we have to
1957 * use a "safe" page frame to store the loaded page.
1959 pbe = chain_alloc(ca, sizeof(struct highmem_pbe));
1960 if (!pbe) {
1961 swsusp_free();
1962 return ERR_PTR(-ENOMEM);
1964 pbe->orig_page = page;
1965 if (safe_highmem_pages > 0) {
1966 struct page *tmp;
1968 /* Copy of the page will be stored in high memory */
1969 kaddr = buffer;
1970 tmp = pfn_to_page(memory_bm_next_pfn(safe_highmem_bm));
1971 safe_highmem_pages--;
1972 last_highmem_page = tmp;
1973 pbe->copy_page = tmp;
1974 } else {
1975 /* Copy of the page will be stored in normal memory */
1976 kaddr = safe_pages_list;
1977 safe_pages_list = safe_pages_list->next;
1978 pbe->copy_page = virt_to_page(kaddr);
1980 pbe->next = highmem_pblist;
1981 highmem_pblist = pbe;
1982 return kaddr;
1986 * copy_last_highmem_page - copy the contents of a highmem image from
1987 * @buffer, where the caller of snapshot_write_next() has place them,
1988 * to the right location represented by @last_highmem_page .
1991 static void copy_last_highmem_page(void)
1993 if (last_highmem_page) {
1994 void *dst;
1996 dst = kmap_atomic(last_highmem_page, KM_USER0);
1997 memcpy(dst, buffer, PAGE_SIZE);
1998 kunmap_atomic(dst, KM_USER0);
1999 last_highmem_page = NULL;
2003 static inline int last_highmem_page_copied(void)
2005 return !last_highmem_page;
2008 static inline void free_highmem_data(void)
2010 if (safe_highmem_bm)
2011 memory_bm_free(safe_highmem_bm, PG_UNSAFE_CLEAR);
2013 if (buffer)
2014 free_image_page(buffer, PG_UNSAFE_CLEAR);
2016 #else
2017 static inline int get_safe_write_buffer(void) { return 0; }
2019 static unsigned int
2020 count_highmem_image_pages(struct memory_bitmap *bm) { return 0; }
2022 static inline int
2023 prepare_highmem_image(struct memory_bitmap *bm, unsigned int *nr_highmem_p)
2025 return 0;
2028 static inline void *
2029 get_highmem_page_buffer(struct page *page, struct chain_allocator *ca)
2031 return ERR_PTR(-EINVAL);
2034 static inline void copy_last_highmem_page(void) {}
2035 static inline int last_highmem_page_copied(void) { return 1; }
2036 static inline void free_highmem_data(void) {}
2037 #endif /* CONFIG_HIGHMEM */
2040 * prepare_image - use the memory bitmap @bm to mark the pages that will
2041 * be overwritten in the process of restoring the system memory state
2042 * from the suspend image ("unsafe" pages) and allocate memory for the
2043 * image.
2045 * The idea is to allocate a new memory bitmap first and then allocate
2046 * as many pages as needed for the image data, but not to assign these
2047 * pages to specific tasks initially. Instead, we just mark them as
2048 * allocated and create a lists of "safe" pages that will be used
2049 * later. On systems with high memory a list of "safe" highmem pages is
2050 * also created.
2053 #define PBES_PER_LINKED_PAGE (LINKED_PAGE_DATA_SIZE / sizeof(struct pbe))
2055 static int
2056 prepare_image(struct memory_bitmap *new_bm, struct memory_bitmap *bm)
2058 unsigned int nr_pages, nr_highmem;
2059 struct linked_page *sp_list, *lp;
2060 int error;
2062 /* If there is no highmem, the buffer will not be necessary */
2063 free_image_page(buffer, PG_UNSAFE_CLEAR);
2064 buffer = NULL;
2066 nr_highmem = count_highmem_image_pages(bm);
2067 error = mark_unsafe_pages(bm);
2068 if (error)
2069 goto Free;
2071 error = memory_bm_create(new_bm, GFP_ATOMIC, PG_SAFE);
2072 if (error)
2073 goto Free;
2075 duplicate_memory_bitmap(new_bm, bm);
2076 memory_bm_free(bm, PG_UNSAFE_KEEP);
2077 if (nr_highmem > 0) {
2078 error = prepare_highmem_image(bm, &nr_highmem);
2079 if (error)
2080 goto Free;
2082 /* Reserve some safe pages for potential later use.
2084 * NOTE: This way we make sure there will be enough safe pages for the
2085 * chain_alloc() in get_buffer(). It is a bit wasteful, but
2086 * nr_copy_pages cannot be greater than 50% of the memory anyway.
2088 sp_list = NULL;
2089 /* nr_copy_pages cannot be lesser than allocated_unsafe_pages */
2090 nr_pages = nr_copy_pages - nr_highmem - allocated_unsafe_pages;
2091 nr_pages = DIV_ROUND_UP(nr_pages, PBES_PER_LINKED_PAGE);
2092 while (nr_pages > 0) {
2093 lp = get_image_page(GFP_ATOMIC, PG_SAFE);
2094 if (!lp) {
2095 error = -ENOMEM;
2096 goto Free;
2098 lp->next = sp_list;
2099 sp_list = lp;
2100 nr_pages--;
2102 /* Preallocate memory for the image */
2103 safe_pages_list = NULL;
2104 nr_pages = nr_copy_pages - nr_highmem - allocated_unsafe_pages;
2105 while (nr_pages > 0) {
2106 lp = (struct linked_page *)get_zeroed_page(GFP_ATOMIC);
2107 if (!lp) {
2108 error = -ENOMEM;
2109 goto Free;
2111 if (!swsusp_page_is_free(virt_to_page(lp))) {
2112 /* The page is "safe", add it to the list */
2113 lp->next = safe_pages_list;
2114 safe_pages_list = lp;
2116 /* Mark the page as allocated */
2117 swsusp_set_page_forbidden(virt_to_page(lp));
2118 swsusp_set_page_free(virt_to_page(lp));
2119 nr_pages--;
2121 /* Free the reserved safe pages so that chain_alloc() can use them */
2122 while (sp_list) {
2123 lp = sp_list->next;
2124 free_image_page(sp_list, PG_UNSAFE_CLEAR);
2125 sp_list = lp;
2127 return 0;
2129 Free:
2130 swsusp_free();
2131 return error;
2135 * get_buffer - compute the address that snapshot_write_next() should
2136 * set for its caller to write to.
2139 static void *get_buffer(struct memory_bitmap *bm, struct chain_allocator *ca)
2141 struct pbe *pbe;
2142 struct page *page;
2143 unsigned long pfn = memory_bm_next_pfn(bm);
2145 if (pfn == BM_END_OF_MAP)
2146 return ERR_PTR(-EFAULT);
2148 page = pfn_to_page(pfn);
2149 if (PageHighMem(page))
2150 return get_highmem_page_buffer(page, ca);
2152 if (swsusp_page_is_forbidden(page) && swsusp_page_is_free(page))
2153 /* We have allocated the "original" page frame and we can
2154 * use it directly to store the loaded page.
2156 return page_address(page);
2158 /* The "original" page frame has not been allocated and we have to
2159 * use a "safe" page frame to store the loaded page.
2161 pbe = chain_alloc(ca, sizeof(struct pbe));
2162 if (!pbe) {
2163 swsusp_free();
2164 return ERR_PTR(-ENOMEM);
2166 pbe->orig_address = page_address(page);
2167 pbe->address = safe_pages_list;
2168 safe_pages_list = safe_pages_list->next;
2169 pbe->next = restore_pblist;
2170 restore_pblist = pbe;
2171 return pbe->address;
2175 * snapshot_write_next - used for writing the system memory snapshot.
2177 * On the first call to it @handle should point to a zeroed
2178 * snapshot_handle structure. The structure gets updated and a pointer
2179 * to it should be passed to this function every next time.
2181 * The @count parameter should contain the number of bytes the caller
2182 * wants to write to the image. It must not be zero.
2184 * On success the function returns a positive number. Then, the caller
2185 * is allowed to write up to the returned number of bytes to the memory
2186 * location computed by the data_of() macro. The number returned
2187 * may be smaller than @count, but this only happens if the write would
2188 * cross a page boundary otherwise.
2190 * The function returns 0 to indicate the "end of file" condition,
2191 * and a negative number is returned on error. In such cases the
2192 * structure pointed to by @handle is not updated and should not be used
2193 * any more.
2196 int snapshot_write_next(struct snapshot_handle *handle, size_t count)
2198 static struct chain_allocator ca;
2199 int error = 0;
2201 /* Check if we have already loaded the entire image */
2202 if (handle->prev && handle->cur > nr_meta_pages + nr_copy_pages)
2203 return 0;
2205 if (handle->offset == 0) {
2206 if (!buffer)
2207 /* This makes the buffer be freed by swsusp_free() */
2208 buffer = get_image_page(GFP_ATOMIC, PG_ANY);
2210 if (!buffer)
2211 return -ENOMEM;
2213 handle->buffer = buffer;
2215 handle->sync_read = 1;
2216 if (handle->prev < handle->cur) {
2217 if (handle->prev == 0) {
2218 error = load_header(buffer);
2219 if (error)
2220 return error;
2222 error = memory_bm_create(&copy_bm, GFP_ATOMIC, PG_ANY);
2223 if (error)
2224 return error;
2226 } else if (handle->prev <= nr_meta_pages) {
2227 error = unpack_orig_pfns(buffer, &copy_bm);
2228 if (error)
2229 return error;
2231 if (handle->prev == nr_meta_pages) {
2232 error = prepare_image(&orig_bm, &copy_bm);
2233 if (error)
2234 return error;
2236 chain_init(&ca, GFP_ATOMIC, PG_SAFE);
2237 memory_bm_position_reset(&orig_bm);
2238 restore_pblist = NULL;
2239 handle->buffer = get_buffer(&orig_bm, &ca);
2240 handle->sync_read = 0;
2241 if (IS_ERR(handle->buffer))
2242 return PTR_ERR(handle->buffer);
2244 } else {
2245 copy_last_highmem_page();
2246 handle->buffer = get_buffer(&orig_bm, &ca);
2247 if (IS_ERR(handle->buffer))
2248 return PTR_ERR(handle->buffer);
2249 if (handle->buffer != buffer)
2250 handle->sync_read = 0;
2252 handle->prev = handle->cur;
2254 handle->buf_offset = handle->cur_offset;
2255 if (handle->cur_offset + count >= PAGE_SIZE) {
2256 count = PAGE_SIZE - handle->cur_offset;
2257 handle->cur_offset = 0;
2258 handle->cur++;
2259 } else {
2260 handle->cur_offset += count;
2262 handle->offset += count;
2263 return count;
2267 * snapshot_write_finalize - must be called after the last call to
2268 * snapshot_write_next() in case the last page in the image happens
2269 * to be a highmem page and its contents should be stored in the
2270 * highmem. Additionally, it releases the memory that will not be
2271 * used any more.
2274 void snapshot_write_finalize(struct snapshot_handle *handle)
2276 copy_last_highmem_page();
2277 /* Free only if we have loaded the image entirely */
2278 if (handle->prev && handle->cur > nr_meta_pages + nr_copy_pages) {
2279 memory_bm_free(&orig_bm, PG_UNSAFE_CLEAR);
2280 free_highmem_data();
2284 int snapshot_image_loaded(struct snapshot_handle *handle)
2286 return !(!nr_copy_pages || !last_highmem_page_copied() ||
2287 handle->cur <= nr_meta_pages + nr_copy_pages);
2290 #ifdef CONFIG_HIGHMEM
2291 /* Assumes that @buf is ready and points to a "safe" page */
2292 static inline void
2293 swap_two_pages_data(struct page *p1, struct page *p2, void *buf)
2295 void *kaddr1, *kaddr2;
2297 kaddr1 = kmap_atomic(p1, KM_USER0);
2298 kaddr2 = kmap_atomic(p2, KM_USER1);
2299 memcpy(buf, kaddr1, PAGE_SIZE);
2300 memcpy(kaddr1, kaddr2, PAGE_SIZE);
2301 memcpy(kaddr2, buf, PAGE_SIZE);
2302 kunmap_atomic(kaddr1, KM_USER0);
2303 kunmap_atomic(kaddr2, KM_USER1);
2307 * restore_highmem - for each highmem page that was allocated before
2308 * the suspend and included in the suspend image, and also has been
2309 * allocated by the "resume" kernel swap its current (ie. "before
2310 * resume") contents with the previous (ie. "before suspend") one.
2312 * If the resume eventually fails, we can call this function once
2313 * again and restore the "before resume" highmem state.
2316 int restore_highmem(void)
2318 struct highmem_pbe *pbe = highmem_pblist;
2319 void *buf;
2321 if (!pbe)
2322 return 0;
2324 buf = get_image_page(GFP_ATOMIC, PG_SAFE);
2325 if (!buf)
2326 return -ENOMEM;
2328 while (pbe) {
2329 swap_two_pages_data(pbe->copy_page, pbe->orig_page, buf);
2330 pbe = pbe->next;
2332 free_image_page(buf, PG_UNSAFE_CLEAR);
2333 return 0;
2335 #endif /* CONFIG_HIGHMEM */