13 #include <linux/version.h>
14 #include <linux/module.h>
18 #include <linux/bitops.h>
20 #include <linux/kernel.h>
22 #include <linux/device.h>
28 #include <linux/list.h>
29 #include <linux/slab.h>
31 #include <asm/uaccess.h>
32 #include <asm/mmu_context.h>
33 #include <asm/pgtable.h>
34 #include <asm/tlbflush.h>
39 static int swsusp_page_is_free(
struct page *);
40 static void swsusp_set_page_forbidden(
struct page *);
41 static void swsusp_unset_page_forbidden(
struct page *);
90 #define PG_UNSAFE_CLEAR 1
91 #define PG_UNSAFE_KEEP 0
93 static unsigned int allocated_unsafe_pages;
104 allocated_unsafe_pages++;
116 return (
unsigned long)get_image_page(gfp_mask,
PG_SAFE);
125 swsusp_set_page_forbidden(page);
136 static inline void free_image_page(
void *
addr,
int clear_nosave_free)
144 swsusp_unset_page_forbidden(page);
145 if (clear_nosave_free)
153 #define LINKED_PAGE_DATA_SIZE (PAGE_SIZE - sizeof(void *))
166 free_image_page(list, clear_page_nosave);
252 #define BM_END_OF_MAP (~0UL)
254 #define BM_BITS_PER_BLOCK (PAGE_SIZE * BITS_PER_BYTE)
263 static inline unsigned long bm_block_bits(
struct bm_block *
bb)
286 static void memory_bm_position_reset(
struct memory_bitmap *bm)
292 static void memory_bm_free(
struct memory_bitmap *bm,
int clear_nosave_free);
300 static int create_bm_block_list(
unsigned long pages,
306 while (nr_blocks-- > 0) {
309 bb = chain_alloc(ca,
sizeof(
struct bm_block));
312 list_add(&bb->
hook, list);
328 static void free_mem_extents(
struct list_head *list)
344 static int create_mem_extents(
struct list_head *list,
gfp_t gfp_mask)
348 INIT_LIST_HEAD(list);
351 unsigned long zone_start, zone_end;
358 if (zone_start <= ext->
end)
365 new_ext = kzalloc(
sizeof(
struct mem_extent), gfp_mask);
367 free_mem_extents(list);
370 new_ext->
start = zone_start;
371 new_ext->
end = zone_end;
377 if (zone_start < ext->start)
378 ext->
start = zone_start;
379 if (zone_end > ext->
end)
385 if (zone_end < cur->start)
387 if (zone_end < cur->
end)
408 chain_init(&ca, gfp_mask, safe_needed);
409 INIT_LIST_HEAD(&bm->
blocks);
411 error = create_mem_extents(&mem_extents, gfp_mask);
417 unsigned long pfn = ext->
start;
418 unsigned long pages = ext->
end - ext->
start;
422 error = create_bm_block_list(pages, bm->
blocks.prev, &ca);
427 bb->
data = get_image_page(gfp_mask, safe_needed);
446 memory_bm_position_reset(bm);
448 free_mem_extents(&mem_extents);
460 static void memory_bm_free(
struct memory_bitmap *bm,
int clear_nosave_free)
466 free_image_page(bb->
data, clear_nosave_free);
468 free_list_of_pages(bm->p_list, clear_nosave_free);
470 INIT_LIST_HEAD(&bm->blocks);
479 void **addr,
unsigned int *bit_nr)
490 if (pfn >= bb->start_pfn)
495 if (pfn >= bb->start_pfn && pfn < bb->
end_pfn)
498 if (&bb->
hook == &bm->blocks)
503 pfn -= bb->start_pfn;
504 bm->cur.
bit = pfn + 1;
516 error = memory_bm_find_bit(bm, pfn, &addr, &bit);
521 static int mem_bm_set_bit_check(
struct memory_bitmap *bm,
unsigned long pfn)
527 error = memory_bm_find_bit(bm, pfn, &addr, &bit);
533 static void memory_bm_clear_bit(
struct memory_bitmap *bm,
unsigned long pfn)
539 error = memory_bm_find_bit(bm, pfn, &addr, &bit);
544 static int memory_bm_test_bit(
struct memory_bitmap *bm,
unsigned long pfn)
550 error = memory_bm_find_bit(bm, pfn, &addr, &bit);
555 static bool memory_bm_pfn_present(
struct memory_bitmap *bm,
unsigned long pfn)
560 return !memory_bm_find_bit(bm, pfn, &addr, &bit);
572 static unsigned long memory_bm_next_pfn(
struct memory_bitmap *bm)
581 if (bit < bm_block_bits(bb))
589 memory_bm_position_reset(bm);
593 bm->
cur.bit = bit + 1;
622 if (start_pfn >= end_pfn)
625 if (!list_empty(&nosave_regions)) {
629 if (region->
end_pfn == start_pfn) {
666 memory_bm_set_bit(free_pages_map,
page_to_pfn(page));
669 static int swsusp_page_is_free(
struct page *page)
671 return free_pages_map ?
672 memory_bm_test_bit(free_pages_map,
page_to_pfn(page)) : 0;
678 memory_bm_clear_bit(free_pages_map,
page_to_pfn(page));
681 static void swsusp_set_page_forbidden(
struct page *page)
683 if (forbidden_pages_map)
684 memory_bm_set_bit(forbidden_pages_map,
page_to_pfn(page));
689 return forbidden_pages_map ?
690 memory_bm_test_bit(forbidden_pages_map,
page_to_pfn(page)) : 0;
693 static void swsusp_unset_page_forbidden(
struct page *page)
695 if (forbidden_pages_map)
696 memory_bm_clear_bit(forbidden_pages_map,
page_to_pfn(page));
708 if (list_empty(&nosave_regions))
714 pr_debug(
"PM: Marking nosave pages: [mem %#010llx-%#010llx]\n",
727 mem_bm_set_bit_check(bm, pfn);
745 BUG_ON(forbidden_pages_map || free_pages_map);
753 goto Free_first_object;
757 goto Free_first_bitmap;
761 goto Free_second_object;
763 forbidden_pages_map = bm1;
764 free_pages_map = bm2;
765 mark_nosave_pages(forbidden_pages_map);
767 pr_debug(
"PM: Basic memory bitmaps created\n");
791 BUG_ON(!(forbidden_pages_map && free_pages_map));
793 bm1 = forbidden_pages_map;
794 bm2 = free_pages_map;
795 forbidden_pages_map =
NULL;
796 free_pages_map =
NULL;
802 pr_debug(
"PM: Basic memory bitmaps freed\n");
821 #ifdef CONFIG_HIGHMEM
827 static unsigned int count_free_highmem_pages(
void)
830 unsigned int cnt = 0;
833 if (is_highmem(zone))
846 static
struct page *saveable_highmem_page(
struct zone *zone,
unsigned long pfn)
854 if (page_zone(page) != zone)
857 BUG_ON(!PageHighMem(page));
863 if (page_is_guard(page))
874 static unsigned int count_highmem_pages(
void)
880 unsigned long pfn, max_zone_pfn;
882 if (!is_highmem(zone))
888 if (saveable_highmem_page(zone, pfn))
894 static inline void *saveable_highmem_page(
struct zone *z,
unsigned long p)
908 static struct page *saveable_page(
struct zone *zone,
unsigned long pfn)
916 if (page_zone(page) != zone)
919 BUG_ON(PageHighMem(page));
924 if (PageReserved(page)
928 if (page_is_guard(page))
939 static unsigned int count_data_pages(
void)
942 unsigned long pfn, max_zone_pfn;
946 if (is_highmem(zone))
952 if (saveable_page(zone, pfn))
961 static inline void do_copy_page(
long *
dst,
long *
src)
976 static void safe_copy_page(
void *dst,
struct page *s_page)
978 if (kernel_page_present(s_page)) {
988 #ifdef CONFIG_HIGHMEM
989 static inline struct page *
992 return is_highmem(zone) ?
993 saveable_highmem_page(zone, pfn) : saveable_page(zone, pfn);
996 static void copy_data_page(
unsigned long dst_pfn,
unsigned long src_pfn)
998 struct page *s_page, *d_page;
1003 if (PageHighMem(s_page)) {
1006 do_copy_page(dst, src);
1010 if (PageHighMem(d_page)) {
1014 safe_copy_page(
buffer, s_page);
1024 #define page_is_saveable(zone, pfn) saveable_page(zone, pfn)
1026 static inline void copy_data_page(
unsigned long dst_pfn,
unsigned long src_pfn)
1040 unsigned long max_zone_pfn;
1046 memory_bm_set_bit(orig_bm, pfn);
1048 memory_bm_position_reset(orig_bm);
1049 memory_bm_position_reset(copy_bm);
1051 pfn = memory_bm_next_pfn(orig_bm);
1054 copy_data_page(memory_bm_next_pfn(copy_bm), pfn);
1059 static unsigned int nr_copy_pages;
1061 static unsigned int nr_meta_pages;
1092 unsigned long pfn, max_zone_pfn;
1101 swsusp_page_is_free(page)) {
1102 swsusp_unset_page_forbidden(page);
1110 restore_pblist =
NULL;
1118 #define GFP_IMAGE (GFP_KERNEL | __GFP_NOWARN)
1127 static unsigned long preallocate_image_pages(
unsigned long nr_pages,
gfp_t mask)
1129 unsigned long nr_alloc = 0;
1131 while (nr_pages > 0) {
1134 page = alloc_image_page(mask);
1138 if (PageHighMem(page))
1149 static unsigned long preallocate_image_memory(
unsigned long nr_pages,
1150 unsigned long avail_normal)
1152 unsigned long alloc;
1154 if (avail_normal <= alloc_normal)
1158 if (nr_pages < alloc)
1161 return preallocate_image_pages(alloc,
GFP_IMAGE);
1164 #ifdef CONFIG_HIGHMEM
1165 static unsigned long preallocate_image_highmem(
unsigned long nr_pages)
1173 static unsigned long __fraction(
u64 x,
u64 multiplier,
u64 base)
1177 return (
unsigned long)
x;
1180 static unsigned long preallocate_highmem_fraction(
unsigned long nr_pages,
1182 unsigned long total)
1184 unsigned long alloc = __fraction(nr_pages, highmem, total);
1189 static inline unsigned long preallocate_image_highmem(
unsigned long nr_pages)
1194 static inline unsigned long preallocate_highmem_fraction(
unsigned long nr_pages,
1196 unsigned long total)
1205 static void free_unnecessary_pages(
void)
1207 unsigned long save, to_free_normal, to_free_highmem;
1209 save = count_data_pages();
1210 if (alloc_normal >= save) {
1211 to_free_normal = alloc_normal - save;
1217 save += count_highmem_pages();
1218 if (alloc_highmem >= save) {
1219 to_free_highmem = alloc_highmem - save;
1221 to_free_highmem = 0;
1223 if (to_free_normal > save)
1224 to_free_normal -= save;
1229 memory_bm_position_reset(©_bm);
1231 while (to_free_normal > 0 || to_free_highmem > 0) {
1232 unsigned long pfn = memory_bm_next_pfn(©_bm);
1235 if (PageHighMem(page)) {
1236 if (!to_free_highmem)
1241 if (!to_free_normal)
1246 memory_bm_clear_bit(©_bm, pfn);
1247 swsusp_unset_page_forbidden(page);
1269 static unsigned long minimum_image_size(
unsigned long saveable)
1280 return saveable <= size ? 0 : saveable -
size;
1309 unsigned long alloc, save_highmem, pages_highmem, avail_normal;
1328 save_highmem = count_highmem_pages();
1329 saveable = count_data_pages();
1336 saveable += save_highmem;
1337 highmem = save_highmem;
1341 if (is_highmem(zone))
1342 highmem += zone_page_state(zone, NR_FREE_PAGES);
1344 count += zone_page_state(zone, NR_FREE_PAGES);
1346 avail_normal =
count;
1348 count -= totalreserve_pages;
1354 max_size = (count - (size + PAGES_FOR_IO)) / 2
1358 if (size > max_size)
1365 if (size >= saveable) {
1366 pages = preallocate_image_highmem(save_highmem);
1367 pages += preallocate_image_memory(saveable - pages, avail_normal);
1372 pages = minimum_image_size(saveable);
1378 if (avail_normal > pages)
1379 avail_normal -=
pages;
1383 size =
min_t(
unsigned long, pages, max_size);
1391 shrink_all_memory(saveable - size);
1400 pages_highmem = preallocate_image_highmem(highmem / 2);
1401 alloc = (count -
max_size) - pages_highmem;
1402 pages = preallocate_image_memory(alloc, avail_normal);
1403 if (pages < alloc) {
1406 pages += pages_highmem;
1407 pages_highmem = preallocate_image_highmem(alloc);
1408 if (pages_highmem < alloc)
1410 pages += pages_highmem;
1415 alloc = (count -
pages) - size;
1416 pages += preallocate_image_highmem(alloc);
1422 alloc = max_size -
size;
1423 size = preallocate_highmem_fraction(alloc, highmem, count);
1424 pages_highmem +=
size;
1426 size = preallocate_image_memory(alloc, avail_normal);
1427 pages_highmem += preallocate_image_highmem(alloc - size);
1428 pages += pages_highmem +
size;
1436 free_unnecessary_pages();
1451 #ifdef CONFIG_HIGHMEM
1457 static unsigned int count_pages_for_highmem(
unsigned int nr_highmem)
1459 unsigned int free_highmem = count_free_highmem_pages() +
alloc_highmem;
1461 if (free_highmem >= nr_highmem)
1464 nr_highmem -= free_highmem;
1470 count_pages_for_highmem(
unsigned int nr_highmem) {
return 0; }
1478 static int enough_free_mem(
unsigned int nr_pages,
unsigned int nr_highmem)
1484 if (!is_highmem(zone))
1485 free += zone_page_state(zone, NR_FREE_PAGES);
1487 nr_pages += count_pages_for_highmem(nr_highmem);
1489 nr_pages, PAGES_FOR_IO, free);
1491 return free > nr_pages + PAGES_FOR_IO;
1494 #ifdef CONFIG_HIGHMEM
1500 static inline int get_highmem_buffer(
int safe_needed)
1512 static inline unsigned int
1513 alloc_highmem_pages(
struct memory_bitmap *bm,
unsigned int nr_highmem)
1515 unsigned int to_alloc = count_free_highmem_pages();
1517 if (to_alloc > nr_highmem)
1518 to_alloc = nr_highmem;
1520 nr_highmem -= to_alloc;
1521 while (to_alloc-- > 0) {
1530 static inline int get_highmem_buffer(
int safe_needed) {
return 0; }
1532 static inline unsigned int
1533 alloc_highmem_pages(
struct memory_bitmap *bm,
unsigned int n) {
return 0; }
1550 unsigned int nr_pages,
unsigned int nr_highmem)
1552 if (nr_highmem > 0) {
1553 if (get_highmem_buffer(
PG_ANY))
1555 if (nr_highmem > alloc_highmem) {
1557 nr_pages += alloc_highmem_pages(copy_bm, nr_highmem);
1560 if (nr_pages > alloc_normal) {
1562 while (nr_pages-- > 0) {
1581 unsigned int nr_pages, nr_highmem;
1586 nr_pages = count_data_pages();
1587 nr_highmem = count_highmem_pages();
1588 printk(
KERN_INFO "PM: Need to copy %u pages\n", nr_pages + nr_highmem);
1590 if (!enough_free_mem(nr_pages, nr_highmem)) {
1595 if (swsusp_alloc(&orig_bm, ©_bm, nr_pages, nr_highmem)) {
1604 copy_data_pages(©_bm, &orig_bm);
1612 nr_pages += nr_highmem;
1613 nr_copy_pages = nr_pages;
1622 #ifndef CONFIG_ARCH_HIBERNATION_HEADER
1630 static char *check_image_kernel(
struct swsusp_info *info)
1633 return "kernel version";
1634 if (
strcmp(info->
uts.sysname,init_utsname()->sysname))
1635 return "system type";
1637 return "kernel release";
1648 return nr_copy_pages + nr_meta_pages + 1;
1659 return init_header_complete(info);
1673 buf[
j] = memory_bm_next_pfn(bm);
1700 if (handle->
cur > nr_meta_pages + nr_copy_pages)
1716 memory_bm_position_reset(&orig_bm);
1717 memory_bm_position_reset(©_bm);
1718 }
else if (handle->
cur <= nr_meta_pages) {
1720 pack_pfns(
buffer, &orig_bm);
1725 if (PageHighMem(page)) {
1753 unsigned long pfn, max_zone_pfn;
1764 memory_bm_position_reset(bm);
1766 pfn = memory_bm_next_pfn(bm);
1775 allocated_unsafe_pages = 0;
1785 memory_bm_position_reset(src);
1786 pfn = memory_bm_next_pfn(src);
1788 memory_bm_set_bit(dst, pfn);
1789 pfn = memory_bm_next_pfn(src);
1797 reason = check_image_kernel(info);
1799 reason =
"memory size";
1816 restore_pblist =
NULL;
1817 error = check_header(info);
1829 static int unpack_orig_pfns(
unsigned long *buf,
struct memory_bitmap *bm)
1840 if (memory_bm_pfn_present(bm, buf[j]))
1841 memory_bm_set_bit(bm, buf[j]);
1854 #ifdef CONFIG_HIGHMEM
1859 struct highmem_pbe {
1861 struct page *orig_page;
1862 struct highmem_pbe *
next;
1870 static struct highmem_pbe *highmem_pblist;
1878 static unsigned int count_highmem_image_pages(
struct memory_bitmap *bm)
1881 unsigned int cnt = 0;
1883 memory_bm_position_reset(bm);
1884 pfn = memory_bm_next_pfn(bm);
1889 pfn = memory_bm_next_pfn(bm);
1906 static unsigned int safe_highmem_pages;
1911 prepare_highmem_image(
struct memory_bitmap *bm,
unsigned int *nr_highmem_p)
1913 unsigned int to_alloc;
1918 if (get_highmem_buffer(
PG_SAFE))
1921 to_alloc = count_free_highmem_pages();
1922 if (to_alloc > *nr_highmem_p)
1923 to_alloc = *nr_highmem_p;
1925 *nr_highmem_p = to_alloc;
1927 safe_highmem_pages = 0;
1928 while (to_alloc-- > 0) {
1932 if (!swsusp_page_is_free(page)) {
1935 safe_highmem_pages++;
1938 swsusp_set_page_forbidden(page);
1941 memory_bm_position_reset(bm);
1942 safe_highmem_bm = bm;
1963 static struct page *last_highmem_page;
1966 get_highmem_page_buffer(
struct page *page,
struct chain_allocator *ca)
1968 struct highmem_pbe *
pbe;
1975 last_highmem_page =
page;
1981 pbe = chain_alloc(ca,
sizeof(
struct highmem_pbe));
1986 pbe->orig_page =
page;
1987 if (safe_highmem_pages > 0) {
1992 tmp =
pfn_to_page(memory_bm_next_pfn(safe_highmem_bm));
1993 safe_highmem_pages--;
1994 last_highmem_page =
tmp;
1995 pbe->copy_page =
tmp;
1998 kaddr = safe_pages_list;
1999 safe_pages_list = safe_pages_list->
next;
2002 pbe->next = highmem_pblist;
2003 highmem_pblist = pbe;
2013 static void copy_last_highmem_page(
void)
2015 if (last_highmem_page) {
2021 last_highmem_page =
NULL;
2025 static inline int last_highmem_page_copied(
void)
2027 return !last_highmem_page;
2030 static inline void free_highmem_data(
void)
2032 if (safe_highmem_bm)
2039 static inline int get_safe_write_buffer(
void) {
return 0; }
2042 count_highmem_image_pages(
struct memory_bitmap *bm) {
return 0; }
2045 prepare_highmem_image(
struct memory_bitmap *bm,
unsigned int *nr_highmem_p)
2050 static inline void *
2051 get_highmem_page_buffer(
struct page *page,
struct chain_allocator *ca)
2056 static inline void copy_last_highmem_page(
void) {}
2057 static inline int last_highmem_page_copied(
void) {
return 1; }
2058 static inline void free_highmem_data(
void) {}
2075 #define PBES_PER_LINKED_PAGE (LINKED_PAGE_DATA_SIZE / sizeof(struct pbe))
2080 unsigned int nr_pages, nr_highmem;
2088 nr_highmem = count_highmem_image_pages(bm);
2089 error = mark_unsafe_pages(bm);
2097 duplicate_memory_bitmap(new_bm, bm);
2099 if (nr_highmem > 0) {
2100 error = prepare_highmem_image(bm, &nr_highmem);
2112 nr_pages = nr_copy_pages - nr_highmem - allocated_unsafe_pages;
2114 while (nr_pages > 0) {
2125 safe_pages_list =
NULL;
2126 nr_pages = nr_copy_pages - nr_highmem - allocated_unsafe_pages;
2127 while (nr_pages > 0) {
2135 lp->
next = safe_pages_list;
2136 safe_pages_list = lp;
2165 unsigned long pfn = memory_bm_next_pfn(bm);
2171 if (PageHighMem(page))
2172 return get_highmem_page_buffer(page, ca);
2183 pbe = chain_alloc(ca,
sizeof(
struct pbe));
2189 pbe->
address = safe_pages_list;
2190 safe_pages_list = safe_pages_list->
next;
2192 restore_pblist = pbe;
2219 if (handle->
cur > 1 && handle->
cur > nr_meta_pages + nr_copy_pages)
2233 }
else if (handle->
cur == 1) {
2234 error = load_header(
buffer);
2247 }
else if (handle->
cur <= nr_meta_pages + 1) {
2248 error = unpack_orig_pfns(
buffer, ©_bm);
2252 if (handle->
cur == nr_meta_pages + 1) {
2253 error = prepare_image(&orig_bm, ©_bm);
2258 memory_bm_position_reset(&orig_bm);
2259 restore_pblist =
NULL;
2260 handle->
buffer = get_buffer(&orig_bm, &ca);
2262 if (IS_ERR(handle->
buffer))
2263 return PTR_ERR(handle->
buffer);
2266 copy_last_highmem_page();
2269 handle->
buffer = get_buffer(&orig_bm, &ca);
2270 if (IS_ERR(handle->
buffer))
2271 return PTR_ERR(handle->
buffer);
2289 copy_last_highmem_page();
2294 if (handle->
cur > 1 && handle->
cur > nr_meta_pages + nr_copy_pages) {
2296 free_highmem_data();
2302 return !(!nr_copy_pages || !last_highmem_page_copied() ||
2303 handle->
cur <= nr_meta_pages + nr_copy_pages);
2306 #ifdef CONFIG_HIGHMEM
2309 swap_two_pages_data(
struct page *
p1,
struct page *p2,
void *buf)
2311 void *kaddr1, *kaddr2;
2332 int restore_highmem(
void)
2334 struct highmem_pbe *pbe = highmem_pblist;
2345 swap_two_pages_data(pbe->copy_page, pbe->orig_page, buf);