diff options
Diffstat (limited to 'kernel/power/snapshot.c')
-rw-r--r-- | kernel/power/snapshot.c | 860 |
1 files changed, 642 insertions, 218 deletions
diff --git a/kernel/power/snapshot.c b/kernel/power/snapshot.c index 99f9b7d177d6..c024606221c4 100644 --- a/kernel/power/snapshot.c +++ b/kernel/power/snapshot.c | |||
@@ -1,15 +1,15 @@ | |||
1 | /* | 1 | /* |
2 | * linux/kernel/power/snapshot.c | 2 | * linux/kernel/power/snapshot.c |
3 | * | 3 | * |
4 | * This file provide system snapshot/restore functionality. | 4 | * This file provides system snapshot/restore functionality for swsusp. |
5 | * | 5 | * |
6 | * Copyright (C) 1998-2005 Pavel Machek <pavel@suse.cz> | 6 | * Copyright (C) 1998-2005 Pavel Machek <pavel@suse.cz> |
7 | * Copyright (C) 2006 Rafael J. Wysocki <rjw@sisk.pl> | ||
7 | * | 8 | * |
8 | * This file is released under the GPLv2, and is based on swsusp.c. | 9 | * This file is released under the GPLv2. |
9 | * | 10 | * |
10 | */ | 11 | */ |
11 | 12 | ||
12 | |||
13 | #include <linux/version.h> | 13 | #include <linux/version.h> |
14 | #include <linux/module.h> | 14 | #include <linux/module.h> |
15 | #include <linux/mm.h> | 15 | #include <linux/mm.h> |
@@ -34,137 +34,24 @@ | |||
34 | 34 | ||
35 | #include "power.h" | 35 | #include "power.h" |
36 | 36 | ||
37 | /* List of PBEs used for creating and restoring the suspend image */ | 37 | /* List of PBEs needed for restoring the pages that were allocated before |
38 | * the suspend and included in the suspend image, but have also been | ||
39 | * allocated by the "resume" kernel, so their contents cannot be written | ||
40 | * directly to their "original" page frames. | ||
41 | */ | ||
38 | struct pbe *restore_pblist; | 42 | struct pbe *restore_pblist; |
39 | 43 | ||
40 | static unsigned int nr_copy_pages; | 44 | /* Pointer to an auxiliary buffer (1 page) */ |
41 | static unsigned int nr_meta_pages; | ||
42 | static void *buffer; | 45 | static void *buffer; |
43 | 46 | ||
44 | #ifdef CONFIG_HIGHMEM | ||
45 | unsigned int count_highmem_pages(void) | ||
46 | { | ||
47 | struct zone *zone; | ||
48 | unsigned long zone_pfn; | ||
49 | unsigned int n = 0; | ||
50 | |||
51 | for_each_zone (zone) | ||
52 | if (is_highmem(zone)) { | ||
53 | mark_free_pages(zone); | ||
54 | for (zone_pfn = 0; zone_pfn < zone->spanned_pages; zone_pfn++) { | ||
55 | struct page *page; | ||
56 | unsigned long pfn = zone_pfn + zone->zone_start_pfn; | ||
57 | if (!pfn_valid(pfn)) | ||
58 | continue; | ||
59 | page = pfn_to_page(pfn); | ||
60 | if (PageReserved(page)) | ||
61 | continue; | ||
62 | if (PageNosaveFree(page)) | ||
63 | continue; | ||
64 | n++; | ||
65 | } | ||
66 | } | ||
67 | return n; | ||
68 | } | ||
69 | |||
70 | struct highmem_page { | ||
71 | char *data; | ||
72 | struct page *page; | ||
73 | struct highmem_page *next; | ||
74 | }; | ||
75 | |||
76 | static struct highmem_page *highmem_copy; | ||
77 | |||
78 | static int save_highmem_zone(struct zone *zone) | ||
79 | { | ||
80 | unsigned long zone_pfn; | ||
81 | mark_free_pages(zone); | ||
82 | for (zone_pfn = 0; zone_pfn < zone->spanned_pages; ++zone_pfn) { | ||
83 | struct page *page; | ||
84 | struct highmem_page *save; | ||
85 | void *kaddr; | ||
86 | unsigned long pfn = zone_pfn + zone->zone_start_pfn; | ||
87 | |||
88 | if (!(pfn%10000)) | ||
89 | printk("."); | ||
90 | if (!pfn_valid(pfn)) | ||
91 | continue; | ||
92 | page = pfn_to_page(pfn); | ||
93 | /* | ||
94 | * This condition results from rvmalloc() sans vmalloc_32() | ||
95 | * and architectural memory reservations. This should be | ||
96 | * corrected eventually when the cases giving rise to this | ||
97 | * are better understood. | ||
98 | */ | ||
99 | if (PageReserved(page)) | ||
100 | continue; | ||
101 | BUG_ON(PageNosave(page)); | ||
102 | if (PageNosaveFree(page)) | ||
103 | continue; | ||
104 | save = kmalloc(sizeof(struct highmem_page), GFP_ATOMIC); | ||
105 | if (!save) | ||
106 | return -ENOMEM; | ||
107 | save->next = highmem_copy; | ||
108 | save->page = page; | ||
109 | save->data = (void *) get_zeroed_page(GFP_ATOMIC); | ||
110 | if (!save->data) { | ||
111 | kfree(save); | ||
112 | return -ENOMEM; | ||
113 | } | ||
114 | kaddr = kmap_atomic(page, KM_USER0); | ||
115 | memcpy(save->data, kaddr, PAGE_SIZE); | ||
116 | kunmap_atomic(kaddr, KM_USER0); | ||
117 | highmem_copy = save; | ||
118 | } | ||
119 | return 0; | ||
120 | } | ||
121 | |||
122 | int save_highmem(void) | ||
123 | { | ||
124 | struct zone *zone; | ||
125 | int res = 0; | ||
126 | |||
127 | pr_debug("swsusp: Saving Highmem"); | ||
128 | drain_local_pages(); | ||
129 | for_each_zone (zone) { | ||
130 | if (is_highmem(zone)) | ||
131 | res = save_highmem_zone(zone); | ||
132 | if (res) | ||
133 | return res; | ||
134 | } | ||
135 | printk("\n"); | ||
136 | return 0; | ||
137 | } | ||
138 | |||
139 | int restore_highmem(void) | ||
140 | { | ||
141 | printk("swsusp: Restoring Highmem\n"); | ||
142 | while (highmem_copy) { | ||
143 | struct highmem_page *save = highmem_copy; | ||
144 | void *kaddr; | ||
145 | highmem_copy = save->next; | ||
146 | |||
147 | kaddr = kmap_atomic(save->page, KM_USER0); | ||
148 | memcpy(kaddr, save->data, PAGE_SIZE); | ||
149 | kunmap_atomic(kaddr, KM_USER0); | ||
150 | free_page((long) save->data); | ||
151 | kfree(save); | ||
152 | } | ||
153 | return 0; | ||
154 | } | ||
155 | #else | ||
156 | static inline unsigned int count_highmem_pages(void) {return 0;} | ||
157 | static inline int save_highmem(void) {return 0;} | ||
158 | static inline int restore_highmem(void) {return 0;} | ||
159 | #endif | ||
160 | |||
161 | /** | 47 | /** |
162 | * @safe_needed - on resume, for storing the PBE list and the image, | 48 | * @safe_needed - on resume, for storing the PBE list and the image, |
163 | * we can only use memory pages that do not conflict with the pages | 49 | * we can only use memory pages that do not conflict with the pages |
164 | * used before suspend. | 50 | * used before suspend. The unsafe pages have PageNosaveFree set |
51 | * and we count them using unsafe_pages. | ||
165 | * | 52 | * |
166 | * The unsafe pages are marked with the PG_nosave_free flag | 53 | * Each allocated image page is marked as PageNosave and PageNosaveFree |
167 | * and we count them using unsafe_pages | 54 | * so that swsusp_free() can release it. |
168 | */ | 55 | */ |
169 | 56 | ||
170 | #define PG_ANY 0 | 57 | #define PG_ANY 0 |
@@ -174,7 +61,7 @@ static inline int restore_highmem(void) {return 0;} | |||
174 | 61 | ||
175 | static unsigned int allocated_unsafe_pages; | 62 | static unsigned int allocated_unsafe_pages; |
176 | 63 | ||
177 | static void *alloc_image_page(gfp_t gfp_mask, int safe_needed) | 64 | static void *get_image_page(gfp_t gfp_mask, int safe_needed) |
178 | { | 65 | { |
179 | void *res; | 66 | void *res; |
180 | 67 | ||
@@ -195,20 +82,39 @@ static void *alloc_image_page(gfp_t gfp_mask, int safe_needed) | |||
195 | 82 | ||
196 | unsigned long get_safe_page(gfp_t gfp_mask) | 83 | unsigned long get_safe_page(gfp_t gfp_mask) |
197 | { | 84 | { |
198 | return (unsigned long)alloc_image_page(gfp_mask, PG_SAFE); | 85 | return (unsigned long)get_image_page(gfp_mask, PG_SAFE); |
86 | } | ||
87 | |||
88 | static struct page *alloc_image_page(gfp_t gfp_mask) | ||
89 | { | ||
90 | struct page *page; | ||
91 | |||
92 | page = alloc_page(gfp_mask); | ||
93 | if (page) { | ||
94 | SetPageNosave(page); | ||
95 | SetPageNosaveFree(page); | ||
96 | } | ||
97 | return page; | ||
199 | } | 98 | } |
200 | 99 | ||
201 | /** | 100 | /** |
202 | * free_image_page - free page represented by @addr, allocated with | 101 | * free_image_page - free page represented by @addr, allocated with |
203 | * alloc_image_page (page flags set by it must be cleared) | 102 | * get_image_page (page flags set by it must be cleared) |
204 | */ | 103 | */ |
205 | 104 | ||
206 | static inline void free_image_page(void *addr, int clear_nosave_free) | 105 | static inline void free_image_page(void *addr, int clear_nosave_free) |
207 | { | 106 | { |
208 | ClearPageNosave(virt_to_page(addr)); | 107 | struct page *page; |
108 | |||
109 | BUG_ON(!virt_addr_valid(addr)); | ||
110 | |||
111 | page = virt_to_page(addr); | ||
112 | |||
113 | ClearPageNosave(page); | ||
209 | if (clear_nosave_free) | 114 | if (clear_nosave_free) |
210 | ClearPageNosaveFree(virt_to_page(addr)); | 115 | ClearPageNosaveFree(page); |
211 | free_page((unsigned long)addr); | 116 | |
117 | __free_page(page); | ||
212 | } | 118 | } |
213 | 119 | ||
214 | /* struct linked_page is used to build chains of pages */ | 120 | /* struct linked_page is used to build chains of pages */ |
@@ -269,7 +175,7 @@ static void *chain_alloc(struct chain_allocator *ca, unsigned int size) | |||
269 | if (LINKED_PAGE_DATA_SIZE - ca->used_space < size) { | 175 | if (LINKED_PAGE_DATA_SIZE - ca->used_space < size) { |
270 | struct linked_page *lp; | 176 | struct linked_page *lp; |
271 | 177 | ||
272 | lp = alloc_image_page(ca->gfp_mask, ca->safe_needed); | 178 | lp = get_image_page(ca->gfp_mask, ca->safe_needed); |
273 | if (!lp) | 179 | if (!lp) |
274 | return NULL; | 180 | return NULL; |
275 | 181 | ||
@@ -446,8 +352,8 @@ memory_bm_create(struct memory_bitmap *bm, gfp_t gfp_mask, int safe_needed) | |||
446 | 352 | ||
447 | /* Compute the number of zones */ | 353 | /* Compute the number of zones */ |
448 | nr = 0; | 354 | nr = 0; |
449 | for_each_zone (zone) | 355 | for_each_zone(zone) |
450 | if (populated_zone(zone) && !is_highmem(zone)) | 356 | if (populated_zone(zone)) |
451 | nr++; | 357 | nr++; |
452 | 358 | ||
453 | /* Allocate the list of zones bitmap objects */ | 359 | /* Allocate the list of zones bitmap objects */ |
@@ -459,10 +365,10 @@ memory_bm_create(struct memory_bitmap *bm, gfp_t gfp_mask, int safe_needed) | |||
459 | } | 365 | } |
460 | 366 | ||
461 | /* Initialize the zone bitmap objects */ | 367 | /* Initialize the zone bitmap objects */ |
462 | for_each_zone (zone) { | 368 | for_each_zone(zone) { |
463 | unsigned long pfn; | 369 | unsigned long pfn; |
464 | 370 | ||
465 | if (!populated_zone(zone) || is_highmem(zone)) | 371 | if (!populated_zone(zone)) |
466 | continue; | 372 | continue; |
467 | 373 | ||
468 | zone_bm->start_pfn = zone->zone_start_pfn; | 374 | zone_bm->start_pfn = zone->zone_start_pfn; |
@@ -481,7 +387,7 @@ memory_bm_create(struct memory_bitmap *bm, gfp_t gfp_mask, int safe_needed) | |||
481 | while (bb) { | 387 | while (bb) { |
482 | unsigned long *ptr; | 388 | unsigned long *ptr; |
483 | 389 | ||
484 | ptr = alloc_image_page(gfp_mask, safe_needed); | 390 | ptr = get_image_page(gfp_mask, safe_needed); |
485 | bb->data = ptr; | 391 | bb->data = ptr; |
486 | if (!ptr) | 392 | if (!ptr) |
487 | goto Free; | 393 | goto Free; |
@@ -505,7 +411,7 @@ memory_bm_create(struct memory_bitmap *bm, gfp_t gfp_mask, int safe_needed) | |||
505 | memory_bm_position_reset(bm); | 411 | memory_bm_position_reset(bm); |
506 | return 0; | 412 | return 0; |
507 | 413 | ||
508 | Free: | 414 | Free: |
509 | bm->p_list = ca.chain; | 415 | bm->p_list = ca.chain; |
510 | memory_bm_free(bm, PG_UNSAFE_CLEAR); | 416 | memory_bm_free(bm, PG_UNSAFE_CLEAR); |
511 | return -ENOMEM; | 417 | return -ENOMEM; |
@@ -651,7 +557,7 @@ static unsigned long memory_bm_next_pfn(struct memory_bitmap *bm) | |||
651 | memory_bm_position_reset(bm); | 557 | memory_bm_position_reset(bm); |
652 | return BM_END_OF_MAP; | 558 | return BM_END_OF_MAP; |
653 | 559 | ||
654 | Return_pfn: | 560 | Return_pfn: |
655 | bm->cur.chunk = chunk; | 561 | bm->cur.chunk = chunk; |
656 | bm->cur.bit = bit; | 562 | bm->cur.bit = bit; |
657 | return bb->start_pfn + chunk * BM_BITS_PER_CHUNK + bit; | 563 | return bb->start_pfn + chunk * BM_BITS_PER_CHUNK + bit; |
@@ -669,10 +575,82 @@ unsigned int snapshot_additional_pages(struct zone *zone) | |||
669 | 575 | ||
670 | res = DIV_ROUND_UP(zone->spanned_pages, BM_BITS_PER_BLOCK); | 576 | res = DIV_ROUND_UP(zone->spanned_pages, BM_BITS_PER_BLOCK); |
671 | res += DIV_ROUND_UP(res * sizeof(struct bm_block), PAGE_SIZE); | 577 | res += DIV_ROUND_UP(res * sizeof(struct bm_block), PAGE_SIZE); |
672 | return res; | 578 | return 2 * res; |
579 | } | ||
580 | |||
581 | #ifdef CONFIG_HIGHMEM | ||
582 | /** | ||
583 | * count_free_highmem_pages - compute the total number of free highmem | ||
584 | * pages, system-wide. | ||
585 | */ | ||
586 | |||
587 | static unsigned int count_free_highmem_pages(void) | ||
588 | { | ||
589 | struct zone *zone; | ||
590 | unsigned int cnt = 0; | ||
591 | |||
592 | for_each_zone(zone) | ||
593 | if (populated_zone(zone) && is_highmem(zone)) | ||
594 | cnt += zone->free_pages; | ||
595 | |||
596 | return cnt; | ||
597 | } | ||
598 | |||
599 | /** | ||
600 | * saveable_highmem_page - Determine whether a highmem page should be | ||
601 | * included in the suspend image. | ||
602 | * | ||
603 | * We should save the page if it isn't Nosave or NosaveFree, or Reserved, | ||
604 | * and it isn't a part of a free chunk of pages. | ||
605 | */ | ||
606 | |||
607 | static struct page *saveable_highmem_page(unsigned long pfn) | ||
608 | { | ||
609 | struct page *page; | ||
610 | |||
611 | if (!pfn_valid(pfn)) | ||
612 | return NULL; | ||
613 | |||
614 | page = pfn_to_page(pfn); | ||
615 | |||
616 | BUG_ON(!PageHighMem(page)); | ||
617 | |||
618 | if (PageNosave(page) || PageReserved(page) || PageNosaveFree(page)) | ||
619 | return NULL; | ||
620 | |||
621 | return page; | ||
673 | } | 622 | } |
674 | 623 | ||
675 | /** | 624 | /** |
625 | * count_highmem_pages - compute the total number of saveable highmem | ||
626 | * pages. | ||
627 | */ | ||
628 | |||
629 | unsigned int count_highmem_pages(void) | ||
630 | { | ||
631 | struct zone *zone; | ||
632 | unsigned int n = 0; | ||
633 | |||
634 | for_each_zone(zone) { | ||
635 | unsigned long pfn, max_zone_pfn; | ||
636 | |||
637 | if (!is_highmem(zone)) | ||
638 | continue; | ||
639 | |||
640 | mark_free_pages(zone); | ||
641 | max_zone_pfn = zone->zone_start_pfn + zone->spanned_pages; | ||
642 | for (pfn = zone->zone_start_pfn; pfn < max_zone_pfn; pfn++) | ||
643 | if (saveable_highmem_page(pfn)) | ||
644 | n++; | ||
645 | } | ||
646 | return n; | ||
647 | } | ||
648 | #else | ||
649 | static inline void *saveable_highmem_page(unsigned long pfn) { return NULL; } | ||
650 | static inline unsigned int count_highmem_pages(void) { return 0; } | ||
651 | #endif /* CONFIG_HIGHMEM */ | ||
652 | |||
653 | /** | ||
676 | * pfn_is_nosave - check if given pfn is in the 'nosave' section | 654 | * pfn_is_nosave - check if given pfn is in the 'nosave' section |
677 | */ | 655 | */ |
678 | 656 | ||
@@ -684,12 +662,12 @@ static inline int pfn_is_nosave(unsigned long pfn) | |||
684 | } | 662 | } |
685 | 663 | ||
686 | /** | 664 | /** |
687 | * saveable - Determine whether a page should be cloned or not. | 665 | * saveable - Determine whether a non-highmem page should be included in |
688 | * @pfn: The page | 666 | * the suspend image. |
689 | * | 667 | * |
690 | * We save a page if it isn't Nosave, and is not in the range of pages | 668 | * We should save the page if it isn't Nosave, and is not in the range |
691 | * statically defined as 'unsaveable', and it | 669 | * of pages statically defined as 'unsaveable', and it isn't a part of |
692 | * isn't a part of a free chunk of pages. | 670 | * a free chunk of pages. |
693 | */ | 671 | */ |
694 | 672 | ||
695 | static struct page *saveable_page(unsigned long pfn) | 673 | static struct page *saveable_page(unsigned long pfn) |
@@ -701,76 +679,130 @@ static struct page *saveable_page(unsigned long pfn) | |||
701 | 679 | ||
702 | page = pfn_to_page(pfn); | 680 | page = pfn_to_page(pfn); |
703 | 681 | ||
704 | if (PageNosave(page)) | 682 | BUG_ON(PageHighMem(page)); |
683 | |||
684 | if (PageNosave(page) || PageNosaveFree(page)) | ||
705 | return NULL; | 685 | return NULL; |
686 | |||
706 | if (PageReserved(page) && pfn_is_nosave(pfn)) | 687 | if (PageReserved(page) && pfn_is_nosave(pfn)) |
707 | return NULL; | 688 | return NULL; |
708 | if (PageNosaveFree(page)) | ||
709 | return NULL; | ||
710 | 689 | ||
711 | return page; | 690 | return page; |
712 | } | 691 | } |
713 | 692 | ||
693 | /** | ||
694 | * count_data_pages - compute the total number of saveable non-highmem | ||
695 | * pages. | ||
696 | */ | ||
697 | |||
714 | unsigned int count_data_pages(void) | 698 | unsigned int count_data_pages(void) |
715 | { | 699 | { |
716 | struct zone *zone; | 700 | struct zone *zone; |
717 | unsigned long pfn, max_zone_pfn; | 701 | unsigned long pfn, max_zone_pfn; |
718 | unsigned int n = 0; | 702 | unsigned int n = 0; |
719 | 703 | ||
720 | for_each_zone (zone) { | 704 | for_each_zone(zone) { |
721 | if (is_highmem(zone)) | 705 | if (is_highmem(zone)) |
722 | continue; | 706 | continue; |
707 | |||
723 | mark_free_pages(zone); | 708 | mark_free_pages(zone); |
724 | max_zone_pfn = zone->zone_start_pfn + zone->spanned_pages; | 709 | max_zone_pfn = zone->zone_start_pfn + zone->spanned_pages; |
725 | for (pfn = zone->zone_start_pfn; pfn < max_zone_pfn; pfn++) | 710 | for (pfn = zone->zone_start_pfn; pfn < max_zone_pfn; pfn++) |
726 | n += !!saveable_page(pfn); | 711 | if(saveable_page(pfn)) |
712 | n++; | ||
727 | } | 713 | } |
728 | return n; | 714 | return n; |
729 | } | 715 | } |
730 | 716 | ||
731 | static inline void copy_data_page(long *dst, long *src) | 717 | /* This is needed, because copy_page and memcpy are not usable for copying |
718 | * task structs. | ||
719 | */ | ||
720 | static inline void do_copy_page(long *dst, long *src) | ||
732 | { | 721 | { |
733 | int n; | 722 | int n; |
734 | 723 | ||
735 | /* copy_page and memcpy are not usable for copying task structs. */ | ||
736 | for (n = PAGE_SIZE / sizeof(long); n; n--) | 724 | for (n = PAGE_SIZE / sizeof(long); n; n--) |
737 | *dst++ = *src++; | 725 | *dst++ = *src++; |
738 | } | 726 | } |
739 | 727 | ||
728 | #ifdef CONFIG_HIGHMEM | ||
729 | static inline struct page * | ||
730 | page_is_saveable(struct zone *zone, unsigned long pfn) | ||
731 | { | ||
732 | return is_highmem(zone) ? | ||
733 | saveable_highmem_page(pfn) : saveable_page(pfn); | ||
734 | } | ||
735 | |||
736 | static inline void | ||
737 | copy_data_page(unsigned long dst_pfn, unsigned long src_pfn) | ||
738 | { | ||
739 | struct page *s_page, *d_page; | ||
740 | void *src, *dst; | ||
741 | |||
742 | s_page = pfn_to_page(src_pfn); | ||
743 | d_page = pfn_to_page(dst_pfn); | ||
744 | if (PageHighMem(s_page)) { | ||
745 | src = kmap_atomic(s_page, KM_USER0); | ||
746 | dst = kmap_atomic(d_page, KM_USER1); | ||
747 | do_copy_page(dst, src); | ||
748 | kunmap_atomic(src, KM_USER0); | ||
749 | kunmap_atomic(dst, KM_USER1); | ||
750 | } else { | ||
751 | src = page_address(s_page); | ||
752 | if (PageHighMem(d_page)) { | ||
753 | /* Page pointed to by src may contain some kernel | ||
754 | * data modified by kmap_atomic() | ||
755 | */ | ||
756 | do_copy_page(buffer, src); | ||
757 | dst = kmap_atomic(pfn_to_page(dst_pfn), KM_USER0); | ||
758 | memcpy(dst, buffer, PAGE_SIZE); | ||
759 | kunmap_atomic(dst, KM_USER0); | ||
760 | } else { | ||
761 | dst = page_address(d_page); | ||
762 | do_copy_page(dst, src); | ||
763 | } | ||
764 | } | ||
765 | } | ||
766 | #else | ||
767 | #define page_is_saveable(zone, pfn) saveable_page(pfn) | ||
768 | |||
769 | static inline void | ||
770 | copy_data_page(unsigned long dst_pfn, unsigned long src_pfn) | ||
771 | { | ||
772 | do_copy_page(page_address(pfn_to_page(dst_pfn)), | ||
773 | page_address(pfn_to_page(src_pfn))); | ||
774 | } | ||
775 | #endif /* CONFIG_HIGHMEM */ | ||
776 | |||
740 | static void | 777 | static void |
741 | copy_data_pages(struct memory_bitmap *copy_bm, struct memory_bitmap *orig_bm) | 778 | copy_data_pages(struct memory_bitmap *copy_bm, struct memory_bitmap *orig_bm) |
742 | { | 779 | { |
743 | struct zone *zone; | 780 | struct zone *zone; |
744 | unsigned long pfn; | 781 | unsigned long pfn; |
745 | 782 | ||
746 | for_each_zone (zone) { | 783 | for_each_zone(zone) { |
747 | unsigned long max_zone_pfn; | 784 | unsigned long max_zone_pfn; |
748 | 785 | ||
749 | if (is_highmem(zone)) | ||
750 | continue; | ||
751 | |||
752 | mark_free_pages(zone); | 786 | mark_free_pages(zone); |
753 | max_zone_pfn = zone->zone_start_pfn + zone->spanned_pages; | 787 | max_zone_pfn = zone->zone_start_pfn + zone->spanned_pages; |
754 | for (pfn = zone->zone_start_pfn; pfn < max_zone_pfn; pfn++) | 788 | for (pfn = zone->zone_start_pfn; pfn < max_zone_pfn; pfn++) |
755 | if (saveable_page(pfn)) | 789 | if (page_is_saveable(zone, pfn)) |
756 | memory_bm_set_bit(orig_bm, pfn); | 790 | memory_bm_set_bit(orig_bm, pfn); |
757 | } | 791 | } |
758 | memory_bm_position_reset(orig_bm); | 792 | memory_bm_position_reset(orig_bm); |
759 | memory_bm_position_reset(copy_bm); | 793 | memory_bm_position_reset(copy_bm); |
760 | do { | 794 | do { |
761 | pfn = memory_bm_next_pfn(orig_bm); | 795 | pfn = memory_bm_next_pfn(orig_bm); |
762 | if (likely(pfn != BM_END_OF_MAP)) { | 796 | if (likely(pfn != BM_END_OF_MAP)) |
763 | struct page *page; | 797 | copy_data_page(memory_bm_next_pfn(copy_bm), pfn); |
764 | void *src; | ||
765 | |||
766 | page = pfn_to_page(pfn); | ||
767 | src = page_address(page); | ||
768 | page = pfn_to_page(memory_bm_next_pfn(copy_bm)); | ||
769 | copy_data_page(page_address(page), src); | ||
770 | } | ||
771 | } while (pfn != BM_END_OF_MAP); | 798 | } while (pfn != BM_END_OF_MAP); |
772 | } | 799 | } |
773 | 800 | ||
801 | /* Total number of image pages */ | ||
802 | static unsigned int nr_copy_pages; | ||
803 | /* Number of pages needed for saving the original pfns of the image pages */ | ||
804 | static unsigned int nr_meta_pages; | ||
805 | |||
774 | /** | 806 | /** |
775 | * swsusp_free - free pages allocated for the suspend. | 807 | * swsusp_free - free pages allocated for the suspend. |
776 | * | 808 | * |
@@ -792,7 +824,7 @@ void swsusp_free(void) | |||
792 | if (PageNosave(page) && PageNosaveFree(page)) { | 824 | if (PageNosave(page) && PageNosaveFree(page)) { |
793 | ClearPageNosave(page); | 825 | ClearPageNosave(page); |
794 | ClearPageNosaveFree(page); | 826 | ClearPageNosaveFree(page); |
795 | free_page((long) page_address(page)); | 827 | __free_page(page); |
796 | } | 828 | } |
797 | } | 829 | } |
798 | } | 830 | } |
@@ -802,34 +834,108 @@ void swsusp_free(void) | |||
802 | buffer = NULL; | 834 | buffer = NULL; |
803 | } | 835 | } |
804 | 836 | ||
837 | #ifdef CONFIG_HIGHMEM | ||
838 | /** | ||
839 | * count_pages_for_highmem - compute the number of non-highmem pages | ||
840 | * that will be necessary for creating copies of highmem pages. | ||
841 | */ | ||
842 | |||
843 | static unsigned int count_pages_for_highmem(unsigned int nr_highmem) | ||
844 | { | ||
845 | unsigned int free_highmem = count_free_highmem_pages(); | ||
846 | |||
847 | if (free_highmem >= nr_highmem) | ||
848 | nr_highmem = 0; | ||
849 | else | ||
850 | nr_highmem -= free_highmem; | ||
851 | |||
852 | return nr_highmem; | ||
853 | } | ||
854 | #else | ||
855 | static unsigned int | ||
856 | count_pages_for_highmem(unsigned int nr_highmem) { return 0; } | ||
857 | #endif /* CONFIG_HIGHMEM */ | ||
805 | 858 | ||
806 | /** | 859 | /** |
807 | * enough_free_mem - Make sure we enough free memory to snapshot. | 860 | * enough_free_mem - Make sure we have enough free memory for the |
808 | * | 861 | * snapshot image. |
809 | * Returns TRUE or FALSE after checking the number of available | ||
810 | * free pages. | ||
811 | */ | 862 | */ |
812 | 863 | ||
813 | static int enough_free_mem(unsigned int nr_pages) | 864 | static int enough_free_mem(unsigned int nr_pages, unsigned int nr_highmem) |
814 | { | 865 | { |
815 | struct zone *zone; | 866 | struct zone *zone; |
816 | unsigned int free = 0, meta = 0; | 867 | unsigned int free = 0, meta = 0; |
817 | 868 | ||
818 | for_each_zone (zone) | 869 | for_each_zone(zone) { |
819 | if (!is_highmem(zone)) { | 870 | meta += snapshot_additional_pages(zone); |
871 | if (!is_highmem(zone)) | ||
820 | free += zone->free_pages; | 872 | free += zone->free_pages; |
821 | meta += snapshot_additional_pages(zone); | 873 | } |
822 | } | ||
823 | 874 | ||
824 | pr_debug("swsusp: pages needed: %u + %u + %u, available pages: %u\n", | 875 | nr_pages += count_pages_for_highmem(nr_highmem); |
876 | pr_debug("swsusp: Normal pages needed: %u + %u + %u, available pages: %u\n", | ||
825 | nr_pages, PAGES_FOR_IO, meta, free); | 877 | nr_pages, PAGES_FOR_IO, meta, free); |
826 | 878 | ||
827 | return free > nr_pages + PAGES_FOR_IO + meta; | 879 | return free > nr_pages + PAGES_FOR_IO + meta; |
828 | } | 880 | } |
829 | 881 | ||
882 | #ifdef CONFIG_HIGHMEM | ||
883 | /** | ||
884 | * get_highmem_buffer - if there are some highmem pages in the suspend | ||
885 | * image, we may need the buffer to copy them and/or load their data. | ||
886 | */ | ||
887 | |||
888 | static inline int get_highmem_buffer(int safe_needed) | ||
889 | { | ||
890 | buffer = get_image_page(GFP_ATOMIC | __GFP_COLD, safe_needed); | ||
891 | return buffer ? 0 : -ENOMEM; | ||
892 | } | ||
893 | |||
894 | /** | ||
895 | * alloc_highmem_image_pages - allocate some highmem pages for the image. | ||
896 | * Try to allocate as many pages as needed, but if the number of free | ||
897 | * highmem pages is lesser than that, allocate them all. | ||
898 | */ | ||
899 | |||
900 | static inline unsigned int | ||
901 | alloc_highmem_image_pages(struct memory_bitmap *bm, unsigned int nr_highmem) | ||
902 | { | ||
903 | unsigned int to_alloc = count_free_highmem_pages(); | ||
904 | |||
905 | if (to_alloc > nr_highmem) | ||
906 | to_alloc = nr_highmem; | ||
907 | |||
908 | nr_highmem -= to_alloc; | ||
909 | while (to_alloc-- > 0) { | ||
910 | struct page *page; | ||
911 | |||
912 | page = alloc_image_page(__GFP_HIGHMEM); | ||
913 | memory_bm_set_bit(bm, page_to_pfn(page)); | ||
914 | } | ||
915 | return nr_highmem; | ||
916 | } | ||
917 | #else | ||
918 | static inline int get_highmem_buffer(int safe_needed) { return 0; } | ||
919 | |||
920 | static inline unsigned int | ||
921 | alloc_highmem_image_pages(struct memory_bitmap *bm, unsigned int n) { return 0; } | ||
922 | #endif /* CONFIG_HIGHMEM */ | ||
923 | |||
924 | /** | ||
925 | * swsusp_alloc - allocate memory for the suspend image | ||
926 | * | ||
927 | * We first try to allocate as many highmem pages as there are | ||
928 | * saveable highmem pages in the system. If that fails, we allocate | ||
929 | * non-highmem pages for the copies of the remaining highmem ones. | ||
930 | * | ||
931 | * In this approach it is likely that the copies of highmem pages will | ||
932 | * also be located in the high memory, because of the way in which | ||
933 | * copy_data_pages() works. | ||
934 | */ | ||
935 | |||
830 | static int | 936 | static int |
831 | swsusp_alloc(struct memory_bitmap *orig_bm, struct memory_bitmap *copy_bm, | 937 | swsusp_alloc(struct memory_bitmap *orig_bm, struct memory_bitmap *copy_bm, |
832 | unsigned int nr_pages) | 938 | unsigned int nr_pages, unsigned int nr_highmem) |
833 | { | 939 | { |
834 | int error; | 940 | int error; |
835 | 941 | ||
@@ -841,46 +947,61 @@ swsusp_alloc(struct memory_bitmap *orig_bm, struct memory_bitmap *copy_bm, | |||
841 | if (error) | 947 | if (error) |
842 | goto Free; | 948 | goto Free; |
843 | 949 | ||
950 | if (nr_highmem > 0) { | ||
951 | error = get_highmem_buffer(PG_ANY); | ||
952 | if (error) | ||
953 | goto Free; | ||
954 | |||
955 | nr_pages += alloc_highmem_image_pages(copy_bm, nr_highmem); | ||
956 | } | ||
844 | while (nr_pages-- > 0) { | 957 | while (nr_pages-- > 0) { |
845 | struct page *page = alloc_page(GFP_ATOMIC | __GFP_COLD); | 958 | struct page *page = alloc_image_page(GFP_ATOMIC | __GFP_COLD); |
959 | |||
846 | if (!page) | 960 | if (!page) |
847 | goto Free; | 961 | goto Free; |
848 | 962 | ||
849 | SetPageNosave(page); | ||
850 | SetPageNosaveFree(page); | ||
851 | memory_bm_set_bit(copy_bm, page_to_pfn(page)); | 963 | memory_bm_set_bit(copy_bm, page_to_pfn(page)); |
852 | } | 964 | } |
853 | return 0; | 965 | return 0; |
854 | 966 | ||
855 | Free: | 967 | Free: |
856 | swsusp_free(); | 968 | swsusp_free(); |
857 | return -ENOMEM; | 969 | return -ENOMEM; |
858 | } | 970 | } |
859 | 971 | ||
860 | /* Memory bitmap used for marking saveable pages */ | 972 | /* Memory bitmap used for marking saveable pages (during suspend) or the |
973 | * suspend image pages (during resume) | ||
974 | */ | ||
861 | static struct memory_bitmap orig_bm; | 975 | static struct memory_bitmap orig_bm; |
862 | /* Memory bitmap used for marking allocated pages that will contain the copies | 976 | /* Memory bitmap used on suspend for marking allocated pages that will contain |
863 | * of saveable pages | 977 | * the copies of saveable pages. During resume it is initially used for |
978 | * marking the suspend image pages, but then its set bits are duplicated in | ||
979 | * @orig_bm and it is released. Next, on systems with high memory, it may be | ||
980 | * used for marking "safe" highmem pages, but it has to be reinitialized for | ||
981 | * this purpose. | ||
864 | */ | 982 | */ |
865 | static struct memory_bitmap copy_bm; | 983 | static struct memory_bitmap copy_bm; |
866 | 984 | ||
867 | asmlinkage int swsusp_save(void) | 985 | asmlinkage int swsusp_save(void) |
868 | { | 986 | { |
869 | unsigned int nr_pages; | 987 | unsigned int nr_pages, nr_highmem; |
870 | 988 | ||
871 | pr_debug("swsusp: critical section: \n"); | 989 | printk("swsusp: critical section: \n"); |
872 | 990 | ||
873 | drain_local_pages(); | 991 | drain_local_pages(); |
874 | nr_pages = count_data_pages(); | 992 | nr_pages = count_data_pages(); |
875 | printk("swsusp: Need to copy %u pages\n", nr_pages); | 993 | nr_highmem = count_highmem_pages(); |
994 | printk("swsusp: Need to copy %u pages\n", nr_pages + nr_highmem); | ||
876 | 995 | ||
877 | if (!enough_free_mem(nr_pages)) { | 996 | if (!enough_free_mem(nr_pages, nr_highmem)) { |
878 | printk(KERN_ERR "swsusp: Not enough free memory\n"); | 997 | printk(KERN_ERR "swsusp: Not enough free memory\n"); |
879 | return -ENOMEM; | 998 | return -ENOMEM; |
880 | } | 999 | } |
881 | 1000 | ||
882 | if (swsusp_alloc(&orig_bm, ©_bm, nr_pages)) | 1001 | if (swsusp_alloc(&orig_bm, ©_bm, nr_pages, nr_highmem)) { |
1002 | printk(KERN_ERR "swsusp: Memory allocation failed\n"); | ||
883 | return -ENOMEM; | 1003 | return -ENOMEM; |
1004 | } | ||
884 | 1005 | ||
885 | /* During allocating of suspend pagedir, new cold pages may appear. | 1006 | /* During allocating of suspend pagedir, new cold pages may appear. |
886 | * Kill them. | 1007 | * Kill them. |
@@ -894,10 +1015,12 @@ asmlinkage int swsusp_save(void) | |||
894 | * touch swap space! Except we must write out our image of course. | 1015 | * touch swap space! Except we must write out our image of course. |
895 | */ | 1016 | */ |
896 | 1017 | ||
1018 | nr_pages += nr_highmem; | ||
897 | nr_copy_pages = nr_pages; | 1019 | nr_copy_pages = nr_pages; |
898 | nr_meta_pages = (nr_pages * sizeof(long) + PAGE_SIZE - 1) >> PAGE_SHIFT; | 1020 | nr_meta_pages = DIV_ROUND_UP(nr_pages * sizeof(long), PAGE_SIZE); |
899 | 1021 | ||
900 | printk("swsusp: critical section/: done (%d pages copied)\n", nr_pages); | 1022 | printk("swsusp: critical section/: done (%d pages copied)\n", nr_pages); |
1023 | |||
901 | return 0; | 1024 | return 0; |
902 | } | 1025 | } |
903 | 1026 | ||
@@ -960,7 +1083,7 @@ int snapshot_read_next(struct snapshot_handle *handle, size_t count) | |||
960 | 1083 | ||
961 | if (!buffer) { | 1084 | if (!buffer) { |
962 | /* This makes the buffer be freed by swsusp_free() */ | 1085 | /* This makes the buffer be freed by swsusp_free() */ |
963 | buffer = alloc_image_page(GFP_ATOMIC, PG_ANY); | 1086 | buffer = get_image_page(GFP_ATOMIC, PG_ANY); |
964 | if (!buffer) | 1087 | if (!buffer) |
965 | return -ENOMEM; | 1088 | return -ENOMEM; |
966 | } | 1089 | } |
@@ -975,9 +1098,23 @@ int snapshot_read_next(struct snapshot_handle *handle, size_t count) | |||
975 | memset(buffer, 0, PAGE_SIZE); | 1098 | memset(buffer, 0, PAGE_SIZE); |
976 | pack_pfns(buffer, &orig_bm); | 1099 | pack_pfns(buffer, &orig_bm); |
977 | } else { | 1100 | } else { |
978 | unsigned long pfn = memory_bm_next_pfn(©_bm); | 1101 | struct page *page; |
979 | 1102 | ||
980 | handle->buffer = page_address(pfn_to_page(pfn)); | 1103 | page = pfn_to_page(memory_bm_next_pfn(©_bm)); |
1104 | if (PageHighMem(page)) { | ||
1105 | /* Highmem pages are copied to the buffer, | ||
1106 | * because we can't return with a kmapped | ||
1107 | * highmem page (we may not be called again). | ||
1108 | */ | ||
1109 | void *kaddr; | ||
1110 | |||
1111 | kaddr = kmap_atomic(page, KM_USER0); | ||
1112 | memcpy(buffer, kaddr, PAGE_SIZE); | ||
1113 | kunmap_atomic(kaddr, KM_USER0); | ||
1114 | handle->buffer = buffer; | ||
1115 | } else { | ||
1116 | handle->buffer = page_address(page); | ||
1117 | } | ||
981 | } | 1118 | } |
982 | handle->prev = handle->cur; | 1119 | handle->prev = handle->cur; |
983 | } | 1120 | } |
@@ -1005,7 +1142,7 @@ static int mark_unsafe_pages(struct memory_bitmap *bm) | |||
1005 | unsigned long pfn, max_zone_pfn; | 1142 | unsigned long pfn, max_zone_pfn; |
1006 | 1143 | ||
1007 | /* Clear page flags */ | 1144 | /* Clear page flags */ |
1008 | for_each_zone (zone) { | 1145 | for_each_zone(zone) { |
1009 | max_zone_pfn = zone->zone_start_pfn + zone->spanned_pages; | 1146 | max_zone_pfn = zone->zone_start_pfn + zone->spanned_pages; |
1010 | for (pfn = zone->zone_start_pfn; pfn < max_zone_pfn; pfn++) | 1147 | for (pfn = zone->zone_start_pfn; pfn < max_zone_pfn; pfn++) |
1011 | if (pfn_valid(pfn)) | 1148 | if (pfn_valid(pfn)) |
@@ -1101,6 +1238,218 @@ unpack_orig_pfns(unsigned long *buf, struct memory_bitmap *bm) | |||
1101 | } | 1238 | } |
1102 | } | 1239 | } |
1103 | 1240 | ||
1241 | /* List of "safe" pages that may be used to store data loaded from the suspend | ||
1242 | * image | ||
1243 | */ | ||
1244 | static struct linked_page *safe_pages_list; | ||
1245 | |||
1246 | #ifdef CONFIG_HIGHMEM | ||
1247 | /* struct highmem_pbe is used for creating the list of highmem pages that | ||
1248 | * should be restored atomically during the resume from disk, because the page | ||
1249 | * frames they have occupied before the suspend are in use. | ||
1250 | */ | ||
1251 | struct highmem_pbe { | ||
1252 | struct page *copy_page; /* data is here now */ | ||
1253 | struct page *orig_page; /* data was here before the suspend */ | ||
1254 | struct highmem_pbe *next; | ||
1255 | }; | ||
1256 | |||
1257 | /* List of highmem PBEs needed for restoring the highmem pages that were | ||
1258 | * allocated before the suspend and included in the suspend image, but have | ||
1259 | * also been allocated by the "resume" kernel, so their contents cannot be | ||
1260 | * written directly to their "original" page frames. | ||
1261 | */ | ||
1262 | static struct highmem_pbe *highmem_pblist; | ||
1263 | |||
1264 | /** | ||
1265 | * count_highmem_image_pages - compute the number of highmem pages in the | ||
1266 | * suspend image. The bits in the memory bitmap @bm that correspond to the | ||
1267 | * image pages are assumed to be set. | ||
1268 | */ | ||
1269 | |||
1270 | static unsigned int count_highmem_image_pages(struct memory_bitmap *bm) | ||
1271 | { | ||
1272 | unsigned long pfn; | ||
1273 | unsigned int cnt = 0; | ||
1274 | |||
1275 | memory_bm_position_reset(bm); | ||
1276 | pfn = memory_bm_next_pfn(bm); | ||
1277 | while (pfn != BM_END_OF_MAP) { | ||
1278 | if (PageHighMem(pfn_to_page(pfn))) | ||
1279 | cnt++; | ||
1280 | |||
1281 | pfn = memory_bm_next_pfn(bm); | ||
1282 | } | ||
1283 | return cnt; | ||
1284 | } | ||
1285 | |||
1286 | /** | ||
1287 | * prepare_highmem_image - try to allocate as many highmem pages as | ||
1288 | * there are highmem image pages (@nr_highmem_p points to the variable | ||
1289 | * containing the number of highmem image pages). The pages that are | ||
1290 | * "safe" (ie. will not be overwritten when the suspend image is | ||
1291 | * restored) have the corresponding bits set in @bm (it must be | ||
1292 | * unitialized). | ||
1293 | * | ||
1294 | * NOTE: This function should not be called if there are no highmem | ||
1295 | * image pages. | ||
1296 | */ | ||
1297 | |||
1298 | static unsigned int safe_highmem_pages; | ||
1299 | |||
1300 | static struct memory_bitmap *safe_highmem_bm; | ||
1301 | |||
1302 | static int | ||
1303 | prepare_highmem_image(struct memory_bitmap *bm, unsigned int *nr_highmem_p) | ||
1304 | { | ||
1305 | unsigned int to_alloc; | ||
1306 | |||
1307 | if (memory_bm_create(bm, GFP_ATOMIC, PG_SAFE)) | ||
1308 | return -ENOMEM; | ||
1309 | |||
1310 | if (get_highmem_buffer(PG_SAFE)) | ||
1311 | return -ENOMEM; | ||
1312 | |||
1313 | to_alloc = count_free_highmem_pages(); | ||
1314 | if (to_alloc > *nr_highmem_p) | ||
1315 | to_alloc = *nr_highmem_p; | ||
1316 | else | ||
1317 | *nr_highmem_p = to_alloc; | ||
1318 | |||
1319 | safe_highmem_pages = 0; | ||
1320 | while (to_alloc-- > 0) { | ||
1321 | struct page *page; | ||
1322 | |||
1323 | page = alloc_page(__GFP_HIGHMEM); | ||
1324 | if (!PageNosaveFree(page)) { | ||
1325 | /* The page is "safe", set its bit the bitmap */ | ||
1326 | memory_bm_set_bit(bm, page_to_pfn(page)); | ||
1327 | safe_highmem_pages++; | ||
1328 | } | ||
1329 | /* Mark the page as allocated */ | ||
1330 | SetPageNosave(page); | ||
1331 | SetPageNosaveFree(page); | ||
1332 | } | ||
1333 | memory_bm_position_reset(bm); | ||
1334 | safe_highmem_bm = bm; | ||
1335 | return 0; | ||
1336 | } | ||
1337 | |||
1338 | /** | ||
1339 | * get_highmem_page_buffer - for given highmem image page find the buffer | ||
1340 | * that suspend_write_next() should set for its caller to write to. | ||
1341 | * | ||
1342 | * If the page is to be saved to its "original" page frame or a copy of | ||
1343 | * the page is to be made in the highmem, @buffer is returned. Otherwise, | ||
1344 | * the copy of the page is to be made in normal memory, so the address of | ||
1345 | * the copy is returned. | ||
1346 | * | ||
1347 | * If @buffer is returned, the caller of suspend_write_next() will write | ||
1348 | * the page's contents to @buffer, so they will have to be copied to the | ||
1349 | * right location on the next call to suspend_write_next() and it is done | ||
1350 | * with the help of copy_last_highmem_page(). For this purpose, if | ||
1351 | * @buffer is returned, @last_highmem page is set to the page to which | ||
1352 | * the data will have to be copied from @buffer. | ||
1353 | */ | ||
1354 | |||
1355 | static struct page *last_highmem_page; | ||
1356 | |||
1357 | static void * | ||
1358 | get_highmem_page_buffer(struct page *page, struct chain_allocator *ca) | ||
1359 | { | ||
1360 | struct highmem_pbe *pbe; | ||
1361 | void *kaddr; | ||
1362 | |||
1363 | if (PageNosave(page) && PageNosaveFree(page)) { | ||
1364 | /* We have allocated the "original" page frame and we can | ||
1365 | * use it directly to store the loaded page. | ||
1366 | */ | ||
1367 | last_highmem_page = page; | ||
1368 | return buffer; | ||
1369 | } | ||
1370 | /* The "original" page frame has not been allocated and we have to | ||
1371 | * use a "safe" page frame to store the loaded page. | ||
1372 | */ | ||
1373 | pbe = chain_alloc(ca, sizeof(struct highmem_pbe)); | ||
1374 | if (!pbe) { | ||
1375 | swsusp_free(); | ||
1376 | return NULL; | ||
1377 | } | ||
1378 | pbe->orig_page = page; | ||
1379 | if (safe_highmem_pages > 0) { | ||
1380 | struct page *tmp; | ||
1381 | |||
1382 | /* Copy of the page will be stored in high memory */ | ||
1383 | kaddr = buffer; | ||
1384 | tmp = pfn_to_page(memory_bm_next_pfn(safe_highmem_bm)); | ||
1385 | safe_highmem_pages--; | ||
1386 | last_highmem_page = tmp; | ||
1387 | pbe->copy_page = tmp; | ||
1388 | } else { | ||
1389 | /* Copy of the page will be stored in normal memory */ | ||
1390 | kaddr = safe_pages_list; | ||
1391 | safe_pages_list = safe_pages_list->next; | ||
1392 | pbe->copy_page = virt_to_page(kaddr); | ||
1393 | } | ||
1394 | pbe->next = highmem_pblist; | ||
1395 | highmem_pblist = pbe; | ||
1396 | return kaddr; | ||
1397 | } | ||
1398 | |||
1399 | /** | ||
1400 | * copy_last_highmem_page - copy the contents of a highmem image from | ||
1401 | * @buffer, where the caller of snapshot_write_next() has place them, | ||
1402 | * to the right location represented by @last_highmem_page . | ||
1403 | */ | ||
1404 | |||
1405 | static void copy_last_highmem_page(void) | ||
1406 | { | ||
1407 | if (last_highmem_page) { | ||
1408 | void *dst; | ||
1409 | |||
1410 | dst = kmap_atomic(last_highmem_page, KM_USER0); | ||
1411 | memcpy(dst, buffer, PAGE_SIZE); | ||
1412 | kunmap_atomic(dst, KM_USER0); | ||
1413 | last_highmem_page = NULL; | ||
1414 | } | ||
1415 | } | ||
1416 | |||
1417 | static inline int last_highmem_page_copied(void) | ||
1418 | { | ||
1419 | return !last_highmem_page; | ||
1420 | } | ||
1421 | |||
1422 | static inline void free_highmem_data(void) | ||
1423 | { | ||
1424 | if (safe_highmem_bm) | ||
1425 | memory_bm_free(safe_highmem_bm, PG_UNSAFE_CLEAR); | ||
1426 | |||
1427 | if (buffer) | ||
1428 | free_image_page(buffer, PG_UNSAFE_CLEAR); | ||
1429 | } | ||
1430 | #else | ||
1431 | static inline int get_safe_write_buffer(void) { return 0; } | ||
1432 | |||
1433 | static unsigned int | ||
1434 | count_highmem_image_pages(struct memory_bitmap *bm) { return 0; } | ||
1435 | |||
1436 | static inline int | ||
1437 | prepare_highmem_image(struct memory_bitmap *bm, unsigned int *nr_highmem_p) | ||
1438 | { | ||
1439 | return 0; | ||
1440 | } | ||
1441 | |||
1442 | static inline void * | ||
1443 | get_highmem_page_buffer(struct page *page, struct chain_allocator *ca) | ||
1444 | { | ||
1445 | return NULL; | ||
1446 | } | ||
1447 | |||
1448 | static inline void copy_last_highmem_page(void) {} | ||
1449 | static inline int last_highmem_page_copied(void) { return 1; } | ||
1450 | static inline void free_highmem_data(void) {} | ||
1451 | #endif /* CONFIG_HIGHMEM */ | ||
1452 | |||
1104 | /** | 1453 | /** |
1105 | * prepare_image - use the memory bitmap @bm to mark the pages that will | 1454 | * prepare_image - use the memory bitmap @bm to mark the pages that will |
1106 | * be overwritten in the process of restoring the system memory state | 1455 | * be overwritten in the process of restoring the system memory state |
@@ -1110,20 +1459,25 @@ unpack_orig_pfns(unsigned long *buf, struct memory_bitmap *bm) | |||
1110 | * The idea is to allocate a new memory bitmap first and then allocate | 1459 | * The idea is to allocate a new memory bitmap first and then allocate |
1111 | * as many pages as needed for the image data, but not to assign these | 1460 | * as many pages as needed for the image data, but not to assign these |
1112 | * pages to specific tasks initially. Instead, we just mark them as | 1461 | * pages to specific tasks initially. Instead, we just mark them as |
1113 | * allocated and create a list of "safe" pages that will be used later. | 1462 | * allocated and create a lists of "safe" pages that will be used |
1463 | * later. On systems with high memory a list of "safe" highmem pages is | ||
1464 | * also created. | ||
1114 | */ | 1465 | */ |
1115 | 1466 | ||
1116 | #define PBES_PER_LINKED_PAGE (LINKED_PAGE_DATA_SIZE / sizeof(struct pbe)) | 1467 | #define PBES_PER_LINKED_PAGE (LINKED_PAGE_DATA_SIZE / sizeof(struct pbe)) |
1117 | 1468 | ||
1118 | static struct linked_page *safe_pages_list; | ||
1119 | |||
1120 | static int | 1469 | static int |
1121 | prepare_image(struct memory_bitmap *new_bm, struct memory_bitmap *bm) | 1470 | prepare_image(struct memory_bitmap *new_bm, struct memory_bitmap *bm) |
1122 | { | 1471 | { |
1123 | unsigned int nr_pages; | 1472 | unsigned int nr_pages, nr_highmem; |
1124 | struct linked_page *sp_list, *lp; | 1473 | struct linked_page *sp_list, *lp; |
1125 | int error; | 1474 | int error; |
1126 | 1475 | ||
1476 | /* If there is no highmem, the buffer will not be necessary */ | ||
1477 | free_image_page(buffer, PG_UNSAFE_CLEAR); | ||
1478 | buffer = NULL; | ||
1479 | |||
1480 | nr_highmem = count_highmem_image_pages(bm); | ||
1127 | error = mark_unsafe_pages(bm); | 1481 | error = mark_unsafe_pages(bm); |
1128 | if (error) | 1482 | if (error) |
1129 | goto Free; | 1483 | goto Free; |
@@ -1134,6 +1488,11 @@ prepare_image(struct memory_bitmap *new_bm, struct memory_bitmap *bm) | |||
1134 | 1488 | ||
1135 | duplicate_memory_bitmap(new_bm, bm); | 1489 | duplicate_memory_bitmap(new_bm, bm); |
1136 | memory_bm_free(bm, PG_UNSAFE_KEEP); | 1490 | memory_bm_free(bm, PG_UNSAFE_KEEP); |
1491 | if (nr_highmem > 0) { | ||
1492 | error = prepare_highmem_image(bm, &nr_highmem); | ||
1493 | if (error) | ||
1494 | goto Free; | ||
1495 | } | ||
1137 | /* Reserve some safe pages for potential later use. | 1496 | /* Reserve some safe pages for potential later use. |
1138 | * | 1497 | * |
1139 | * NOTE: This way we make sure there will be enough safe pages for the | 1498 | * NOTE: This way we make sure there will be enough safe pages for the |
@@ -1142,10 +1501,10 @@ prepare_image(struct memory_bitmap *new_bm, struct memory_bitmap *bm) | |||
1142 | */ | 1501 | */ |
1143 | sp_list = NULL; | 1502 | sp_list = NULL; |
1144 | /* nr_copy_pages cannot be lesser than allocated_unsafe_pages */ | 1503 | /* nr_copy_pages cannot be lesser than allocated_unsafe_pages */ |
1145 | nr_pages = nr_copy_pages - allocated_unsafe_pages; | 1504 | nr_pages = nr_copy_pages - nr_highmem - allocated_unsafe_pages; |
1146 | nr_pages = DIV_ROUND_UP(nr_pages, PBES_PER_LINKED_PAGE); | 1505 | nr_pages = DIV_ROUND_UP(nr_pages, PBES_PER_LINKED_PAGE); |
1147 | while (nr_pages > 0) { | 1506 | while (nr_pages > 0) { |
1148 | lp = alloc_image_page(GFP_ATOMIC, PG_SAFE); | 1507 | lp = get_image_page(GFP_ATOMIC, PG_SAFE); |
1149 | if (!lp) { | 1508 | if (!lp) { |
1150 | error = -ENOMEM; | 1509 | error = -ENOMEM; |
1151 | goto Free; | 1510 | goto Free; |
@@ -1156,7 +1515,7 @@ prepare_image(struct memory_bitmap *new_bm, struct memory_bitmap *bm) | |||
1156 | } | 1515 | } |
1157 | /* Preallocate memory for the image */ | 1516 | /* Preallocate memory for the image */ |
1158 | safe_pages_list = NULL; | 1517 | safe_pages_list = NULL; |
1159 | nr_pages = nr_copy_pages - allocated_unsafe_pages; | 1518 | nr_pages = nr_copy_pages - nr_highmem - allocated_unsafe_pages; |
1160 | while (nr_pages > 0) { | 1519 | while (nr_pages > 0) { |
1161 | lp = (struct linked_page *)get_zeroed_page(GFP_ATOMIC); | 1520 | lp = (struct linked_page *)get_zeroed_page(GFP_ATOMIC); |
1162 | if (!lp) { | 1521 | if (!lp) { |
@@ -1181,7 +1540,7 @@ prepare_image(struct memory_bitmap *new_bm, struct memory_bitmap *bm) | |||
1181 | } | 1540 | } |
1182 | return 0; | 1541 | return 0; |
1183 | 1542 | ||
1184 | Free: | 1543 | Free: |
1185 | swsusp_free(); | 1544 | swsusp_free(); |
1186 | return error; | 1545 | return error; |
1187 | } | 1546 | } |
@@ -1196,6 +1555,9 @@ static void *get_buffer(struct memory_bitmap *bm, struct chain_allocator *ca) | |||
1196 | struct pbe *pbe; | 1555 | struct pbe *pbe; |
1197 | struct page *page = pfn_to_page(memory_bm_next_pfn(bm)); | 1556 | struct page *page = pfn_to_page(memory_bm_next_pfn(bm)); |
1198 | 1557 | ||
1558 | if (PageHighMem(page)) | ||
1559 | return get_highmem_page_buffer(page, ca); | ||
1560 | |||
1199 | if (PageNosave(page) && PageNosaveFree(page)) | 1561 | if (PageNosave(page) && PageNosaveFree(page)) |
1200 | /* We have allocated the "original" page frame and we can | 1562 | /* We have allocated the "original" page frame and we can |
1201 | * use it directly to store the loaded page. | 1563 | * use it directly to store the loaded page. |
@@ -1210,12 +1572,12 @@ static void *get_buffer(struct memory_bitmap *bm, struct chain_allocator *ca) | |||
1210 | swsusp_free(); | 1572 | swsusp_free(); |
1211 | return NULL; | 1573 | return NULL; |
1212 | } | 1574 | } |
1213 | pbe->orig_address = (unsigned long)page_address(page); | 1575 | pbe->orig_address = page_address(page); |
1214 | pbe->address = (unsigned long)safe_pages_list; | 1576 | pbe->address = safe_pages_list; |
1215 | safe_pages_list = safe_pages_list->next; | 1577 | safe_pages_list = safe_pages_list->next; |
1216 | pbe->next = restore_pblist; | 1578 | pbe->next = restore_pblist; |
1217 | restore_pblist = pbe; | 1579 | restore_pblist = pbe; |
1218 | return (void *)pbe->address; | 1580 | return pbe->address; |
1219 | } | 1581 | } |
1220 | 1582 | ||
1221 | /** | 1583 | /** |
@@ -1249,14 +1611,16 @@ int snapshot_write_next(struct snapshot_handle *handle, size_t count) | |||
1249 | if (handle->prev && handle->cur > nr_meta_pages + nr_copy_pages) | 1611 | if (handle->prev && handle->cur > nr_meta_pages + nr_copy_pages) |
1250 | return 0; | 1612 | return 0; |
1251 | 1613 | ||
1252 | if (!buffer) { | 1614 | if (handle->offset == 0) { |
1253 | /* This makes the buffer be freed by swsusp_free() */ | 1615 | if (!buffer) |
1254 | buffer = alloc_image_page(GFP_ATOMIC, PG_ANY); | 1616 | /* This makes the buffer be freed by swsusp_free() */ |
1617 | buffer = get_image_page(GFP_ATOMIC, PG_ANY); | ||
1618 | |||
1255 | if (!buffer) | 1619 | if (!buffer) |
1256 | return -ENOMEM; | 1620 | return -ENOMEM; |
1257 | } | 1621 | |
1258 | if (!handle->offset) | ||
1259 | handle->buffer = buffer; | 1622 | handle->buffer = buffer; |
1623 | } | ||
1260 | handle->sync_read = 1; | 1624 | handle->sync_read = 1; |
1261 | if (handle->prev < handle->cur) { | 1625 | if (handle->prev < handle->cur) { |
1262 | if (handle->prev == 0) { | 1626 | if (handle->prev == 0) { |
@@ -1284,8 +1648,10 @@ int snapshot_write_next(struct snapshot_handle *handle, size_t count) | |||
1284 | return -ENOMEM; | 1648 | return -ENOMEM; |
1285 | } | 1649 | } |
1286 | } else { | 1650 | } else { |
1651 | copy_last_highmem_page(); | ||
1287 | handle->buffer = get_buffer(&orig_bm, &ca); | 1652 | handle->buffer = get_buffer(&orig_bm, &ca); |
1288 | handle->sync_read = 0; | 1653 | if (handle->buffer != buffer) |
1654 | handle->sync_read = 0; | ||
1289 | } | 1655 | } |
1290 | handle->prev = handle->cur; | 1656 | handle->prev = handle->cur; |
1291 | } | 1657 | } |
@@ -1301,15 +1667,73 @@ int snapshot_write_next(struct snapshot_handle *handle, size_t count) | |||
1301 | return count; | 1667 | return count; |
1302 | } | 1668 | } |
1303 | 1669 | ||
1670 | /** | ||
1671 | * snapshot_write_finalize - must be called after the last call to | ||
1672 | * snapshot_write_next() in case the last page in the image happens | ||
1673 | * to be a highmem page and its contents should be stored in the | ||
1674 | * highmem. Additionally, it releases the memory that will not be | ||
1675 | * used any more. | ||
1676 | */ | ||
1677 | |||
1678 | void snapshot_write_finalize(struct snapshot_handle *handle) | ||
1679 | { | ||
1680 | copy_last_highmem_page(); | ||
1681 | /* Free only if we have loaded the image entirely */ | ||
1682 | if (handle->prev && handle->cur > nr_meta_pages + nr_copy_pages) { | ||
1683 | memory_bm_free(&orig_bm, PG_UNSAFE_CLEAR); | ||
1684 | free_highmem_data(); | ||
1685 | } | ||
1686 | } | ||
1687 | |||
1304 | int snapshot_image_loaded(struct snapshot_handle *handle) | 1688 | int snapshot_image_loaded(struct snapshot_handle *handle) |
1305 | { | 1689 | { |
1306 | return !(!nr_copy_pages || | 1690 | return !(!nr_copy_pages || !last_highmem_page_copied() || |
1307 | handle->cur <= nr_meta_pages + nr_copy_pages); | 1691 | handle->cur <= nr_meta_pages + nr_copy_pages); |
1308 | } | 1692 | } |
1309 | 1693 | ||
1310 | void snapshot_free_unused_memory(struct snapshot_handle *handle) | 1694 | #ifdef CONFIG_HIGHMEM |
1695 | /* Assumes that @buf is ready and points to a "safe" page */ | ||
1696 | static inline void | ||
1697 | swap_two_pages_data(struct page *p1, struct page *p2, void *buf) | ||
1311 | { | 1698 | { |
1312 | /* Free only if we have loaded the image entirely */ | 1699 | void *kaddr1, *kaddr2; |
1313 | if (handle->prev && handle->cur > nr_meta_pages + nr_copy_pages) | 1700 | |
1314 | memory_bm_free(&orig_bm, PG_UNSAFE_CLEAR); | 1701 | kaddr1 = kmap_atomic(p1, KM_USER0); |
1702 | kaddr2 = kmap_atomic(p2, KM_USER1); | ||
1703 | memcpy(buf, kaddr1, PAGE_SIZE); | ||
1704 | memcpy(kaddr1, kaddr2, PAGE_SIZE); | ||
1705 | memcpy(kaddr2, buf, PAGE_SIZE); | ||
1706 | kunmap_atomic(kaddr1, KM_USER0); | ||
1707 | kunmap_atomic(kaddr2, KM_USER1); | ||
1708 | } | ||
1709 | |||
1710 | /** | ||
1711 | * restore_highmem - for each highmem page that was allocated before | ||
1712 | * the suspend and included in the suspend image, and also has been | ||
1713 | * allocated by the "resume" kernel swap its current (ie. "before | ||
1714 | * resume") contents with the previous (ie. "before suspend") one. | ||
1715 | * | ||
1716 | * If the resume eventually fails, we can call this function once | ||
1717 | * again and restore the "before resume" highmem state. | ||
1718 | */ | ||
1719 | |||
1720 | int restore_highmem(void) | ||
1721 | { | ||
1722 | struct highmem_pbe *pbe = highmem_pblist; | ||
1723 | void *buf; | ||
1724 | |||
1725 | if (!pbe) | ||
1726 | return 0; | ||
1727 | |||
1728 | buf = get_image_page(GFP_ATOMIC, PG_SAFE); | ||
1729 | if (!buf) | ||
1730 | return -ENOMEM; | ||
1731 | |||
1732 | while (pbe) { | ||
1733 | swap_two_pages_data(pbe->copy_page, pbe->orig_page, buf); | ||
1734 | pbe = pbe->next; | ||
1735 | } | ||
1736 | free_image_page(buf, PG_UNSAFE_CLEAR); | ||
1737 | return 0; | ||
1315 | } | 1738 | } |
1739 | #endif /* CONFIG_HIGHMEM */ | ||