Rafael J. Wysocki | 25761b6 | 2005-10-30 14:59:56 -0800 | [diff] [blame] | 1 | /* |
Pavel Machek | 96bc7ae | 2005-10-30 14:59:58 -0800 | [diff] [blame] | 2 | * linux/kernel/power/snapshot.c |
Rafael J. Wysocki | 25761b6 | 2005-10-30 14:59:56 -0800 | [diff] [blame] | 3 | * |
Rafael J. Wysocki | 8357376 | 2006-12-06 20:34:18 -0800 | [diff] [blame] | 4 | * This file provides system snapshot/restore functionality for swsusp. |
Rafael J. Wysocki | 25761b6 | 2005-10-30 14:59:56 -0800 | [diff] [blame] | 5 | * |
Pavel Machek | a253129 | 2010-07-18 14:27:13 +0200 | [diff] [blame] | 6 | * Copyright (C) 1998-2005 Pavel Machek <pavel@ucw.cz> |
Rafael J. Wysocki | 8357376 | 2006-12-06 20:34:18 -0800 | [diff] [blame] | 7 | * Copyright (C) 2006 Rafael J. Wysocki <rjw@sisk.pl> |
Rafael J. Wysocki | 25761b6 | 2005-10-30 14:59:56 -0800 | [diff] [blame] | 8 | * |
Rafael J. Wysocki | 8357376 | 2006-12-06 20:34:18 -0800 | [diff] [blame] | 9 | * This file is released under the GPLv2. |
Rafael J. Wysocki | 25761b6 | 2005-10-30 14:59:56 -0800 | [diff] [blame] | 10 | * |
| 11 | */ |
| 12 | |
Rafael J. Wysocki | f577eb3 | 2006-03-23 02:59:59 -0800 | [diff] [blame] | 13 | #include <linux/version.h> |
Rafael J. Wysocki | 25761b6 | 2005-10-30 14:59:56 -0800 | [diff] [blame] | 14 | #include <linux/module.h> |
| 15 | #include <linux/mm.h> |
| 16 | #include <linux/suspend.h> |
Rafael J. Wysocki | 25761b6 | 2005-10-30 14:59:56 -0800 | [diff] [blame] | 17 | #include <linux/delay.h> |
Rafael J. Wysocki | 25761b6 | 2005-10-30 14:59:56 -0800 | [diff] [blame] | 18 | #include <linux/bitops.h> |
Rafael J. Wysocki | 25761b6 | 2005-10-30 14:59:56 -0800 | [diff] [blame] | 19 | #include <linux/spinlock.h> |
Rafael J. Wysocki | 25761b6 | 2005-10-30 14:59:56 -0800 | [diff] [blame] | 20 | #include <linux/kernel.h> |
Rafael J. Wysocki | 25761b6 | 2005-10-30 14:59:56 -0800 | [diff] [blame] | 21 | #include <linux/pm.h> |
| 22 | #include <linux/device.h> |
Rafael J. Wysocki | 74dfd66 | 2007-05-06 14:50:43 -0700 | [diff] [blame] | 23 | #include <linux/init.h> |
Rafael J. Wysocki | 25761b6 | 2005-10-30 14:59:56 -0800 | [diff] [blame] | 24 | #include <linux/bootmem.h> |
| 25 | #include <linux/syscalls.h> |
| 26 | #include <linux/console.h> |
| 27 | #include <linux/highmem.h> |
Rafael J. Wysocki | 846705d | 2008-11-26 18:00:24 -0500 | [diff] [blame] | 28 | #include <linux/list.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 29 | #include <linux/slab.h> |
Gideon Israel Dsouza | 52f5684c | 2014-04-07 15:39:20 -0700 | [diff] [blame] | 30 | #include <linux/compiler.h> |
Rafael J. Wysocki | 25761b6 | 2005-10-30 14:59:56 -0800 | [diff] [blame] | 31 | |
| 32 | #include <asm/uaccess.h> |
| 33 | #include <asm/mmu_context.h> |
| 34 | #include <asm/pgtable.h> |
| 35 | #include <asm/tlbflush.h> |
| 36 | #include <asm/io.h> |
| 37 | |
Rafael J. Wysocki | 25761b6 | 2005-10-30 14:59:56 -0800 | [diff] [blame] | 38 | #include "power.h" |
| 39 | |
Rafael J. Wysocki | 74dfd66 | 2007-05-06 14:50:43 -0700 | [diff] [blame] | 40 | static int swsusp_page_is_free(struct page *); |
| 41 | static void swsusp_set_page_forbidden(struct page *); |
| 42 | static void swsusp_unset_page_forbidden(struct page *); |
| 43 | |
Rafael J. Wysocki | fe41953 | 2009-06-11 23:11:17 +0200 | [diff] [blame] | 44 | /* |
Rafael J. Wysocki | ddeb648 | 2011-05-15 11:38:48 +0200 | [diff] [blame] | 45 | * Number of bytes to reserve for memory allocations made by device drivers |
| 46 | * from their ->freeze() and ->freeze_noirq() callbacks so that they don't |
| 47 | * cause image creation to fail (tunable via /sys/power/reserved_size). |
| 48 | */ |
| 49 | unsigned long reserved_size; |
| 50 | |
| 51 | void __init hibernate_reserved_size_init(void) |
| 52 | { |
| 53 | reserved_size = SPARE_PAGES * PAGE_SIZE; |
| 54 | } |
| 55 | |
| 56 | /* |
Rafael J. Wysocki | fe41953 | 2009-06-11 23:11:17 +0200 | [diff] [blame] | 57 | * Preferred image size in bytes (tunable via /sys/power/image_size). |
Rafael J. Wysocki | 1c1be3a | 2011-05-15 11:39:48 +0200 | [diff] [blame] | 58 | * When it is set to N, swsusp will do its best to ensure the image |
| 59 | * size will not exceed N bytes, but if that is impossible, it will |
| 60 | * try to create the smallest image possible. |
Rafael J. Wysocki | fe41953 | 2009-06-11 23:11:17 +0200 | [diff] [blame] | 61 | */ |
Rafael J. Wysocki | ac5c24ec | 2010-09-20 19:44:56 +0200 | [diff] [blame] | 62 | unsigned long image_size; |
| 63 | |
| 64 | void __init hibernate_image_size_init(void) |
| 65 | { |
Rafael J. Wysocki | 1c1be3a | 2011-05-15 11:39:48 +0200 | [diff] [blame] | 66 | image_size = ((totalram_pages * 2) / 5) * PAGE_SIZE; |
Rafael J. Wysocki | ac5c24ec | 2010-09-20 19:44:56 +0200 | [diff] [blame] | 67 | } |
Rafael J. Wysocki | fe41953 | 2009-06-11 23:11:17 +0200 | [diff] [blame] | 68 | |
Rafael J. Wysocki | 8357376 | 2006-12-06 20:34:18 -0800 | [diff] [blame] | 69 | /* List of PBEs needed for restoring the pages that were allocated before |
| 70 | * the suspend and included in the suspend image, but have also been |
| 71 | * allocated by the "resume" kernel, so their contents cannot be written |
| 72 | * directly to their "original" page frames. |
| 73 | */ |
Rafael J. Wysocki | 75534b5 | 2006-09-25 23:32:52 -0700 | [diff] [blame] | 74 | struct pbe *restore_pblist; |
| 75 | |
Rafael J. Wysocki | 8357376 | 2006-12-06 20:34:18 -0800 | [diff] [blame] | 76 | /* Pointer to an auxiliary buffer (1 page) */ |
Rafael J. Wysocki | 940864d | 2006-09-25 23:32:55 -0700 | [diff] [blame] | 77 | static void *buffer; |
Rafael J. Wysocki | 7088a5c | 2006-01-06 00:13:05 -0800 | [diff] [blame] | 78 | |
Rafael J. Wysocki | f6143aa | 2006-09-25 23:32:50 -0700 | [diff] [blame] | 79 | /** |
| 80 | * @safe_needed - on resume, for storing the PBE list and the image, |
| 81 | * we can only use memory pages that do not conflict with the pages |
Rafael J. Wysocki | 8357376 | 2006-12-06 20:34:18 -0800 | [diff] [blame] | 82 | * used before suspend. The unsafe pages have PageNosaveFree set |
| 83 | * and we count them using unsafe_pages. |
Rafael J. Wysocki | f6143aa | 2006-09-25 23:32:50 -0700 | [diff] [blame] | 84 | * |
Rafael J. Wysocki | 8357376 | 2006-12-06 20:34:18 -0800 | [diff] [blame] | 85 | * Each allocated image page is marked as PageNosave and PageNosaveFree |
| 86 | * so that swsusp_free() can release it. |
Rafael J. Wysocki | f6143aa | 2006-09-25 23:32:50 -0700 | [diff] [blame] | 87 | */ |
| 88 | |
Rafael J. Wysocki | 0bcd888 | 2006-09-25 23:32:52 -0700 | [diff] [blame] | 89 | #define PG_ANY 0 |
| 90 | #define PG_SAFE 1 |
| 91 | #define PG_UNSAFE_CLEAR 1 |
| 92 | #define PG_UNSAFE_KEEP 0 |
| 93 | |
Rafael J. Wysocki | 940864d | 2006-09-25 23:32:55 -0700 | [diff] [blame] | 94 | static unsigned int allocated_unsafe_pages; |
Rafael J. Wysocki | f6143aa | 2006-09-25 23:32:50 -0700 | [diff] [blame] | 95 | |
Rafael J. Wysocki | 8357376 | 2006-12-06 20:34:18 -0800 | [diff] [blame] | 96 | static void *get_image_page(gfp_t gfp_mask, int safe_needed) |
Rafael J. Wysocki | f6143aa | 2006-09-25 23:32:50 -0700 | [diff] [blame] | 97 | { |
| 98 | void *res; |
| 99 | |
| 100 | res = (void *)get_zeroed_page(gfp_mask); |
| 101 | if (safe_needed) |
Rafael J. Wysocki | 7be9823 | 2007-05-06 14:50:42 -0700 | [diff] [blame] | 102 | while (res && swsusp_page_is_free(virt_to_page(res))) { |
Rafael J. Wysocki | f6143aa | 2006-09-25 23:32:50 -0700 | [diff] [blame] | 103 | /* The page is unsafe, mark it for swsusp_free() */ |
Rafael J. Wysocki | 7be9823 | 2007-05-06 14:50:42 -0700 | [diff] [blame] | 104 | swsusp_set_page_forbidden(virt_to_page(res)); |
Rafael J. Wysocki | 940864d | 2006-09-25 23:32:55 -0700 | [diff] [blame] | 105 | allocated_unsafe_pages++; |
Rafael J. Wysocki | f6143aa | 2006-09-25 23:32:50 -0700 | [diff] [blame] | 106 | res = (void *)get_zeroed_page(gfp_mask); |
| 107 | } |
| 108 | if (res) { |
Rafael J. Wysocki | 7be9823 | 2007-05-06 14:50:42 -0700 | [diff] [blame] | 109 | swsusp_set_page_forbidden(virt_to_page(res)); |
| 110 | swsusp_set_page_free(virt_to_page(res)); |
Rafael J. Wysocki | f6143aa | 2006-09-25 23:32:50 -0700 | [diff] [blame] | 111 | } |
| 112 | return res; |
| 113 | } |
| 114 | |
| 115 | unsigned long get_safe_page(gfp_t gfp_mask) |
| 116 | { |
Rafael J. Wysocki | 8357376 | 2006-12-06 20:34:18 -0800 | [diff] [blame] | 117 | return (unsigned long)get_image_page(gfp_mask, PG_SAFE); |
| 118 | } |
| 119 | |
Rafael J. Wysocki | 5b6d15d | 2006-12-06 20:34:43 -0800 | [diff] [blame] | 120 | static struct page *alloc_image_page(gfp_t gfp_mask) |
| 121 | { |
Rafael J. Wysocki | 8357376 | 2006-12-06 20:34:18 -0800 | [diff] [blame] | 122 | struct page *page; |
| 123 | |
| 124 | page = alloc_page(gfp_mask); |
| 125 | if (page) { |
Rafael J. Wysocki | 7be9823 | 2007-05-06 14:50:42 -0700 | [diff] [blame] | 126 | swsusp_set_page_forbidden(page); |
| 127 | swsusp_set_page_free(page); |
Rafael J. Wysocki | 8357376 | 2006-12-06 20:34:18 -0800 | [diff] [blame] | 128 | } |
| 129 | return page; |
Rafael J. Wysocki | f6143aa | 2006-09-25 23:32:50 -0700 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | /** |
| 133 | * free_image_page - free page represented by @addr, allocated with |
Rafael J. Wysocki | 8357376 | 2006-12-06 20:34:18 -0800 | [diff] [blame] | 134 | * get_image_page (page flags set by it must be cleared) |
Rafael J. Wysocki | f6143aa | 2006-09-25 23:32:50 -0700 | [diff] [blame] | 135 | */ |
| 136 | |
| 137 | static inline void free_image_page(void *addr, int clear_nosave_free) |
| 138 | { |
Rafael J. Wysocki | 8357376 | 2006-12-06 20:34:18 -0800 | [diff] [blame] | 139 | struct page *page; |
| 140 | |
| 141 | BUG_ON(!virt_addr_valid(addr)); |
| 142 | |
| 143 | page = virt_to_page(addr); |
| 144 | |
Rafael J. Wysocki | 7be9823 | 2007-05-06 14:50:42 -0700 | [diff] [blame] | 145 | swsusp_unset_page_forbidden(page); |
Rafael J. Wysocki | f6143aa | 2006-09-25 23:32:50 -0700 | [diff] [blame] | 146 | if (clear_nosave_free) |
Rafael J. Wysocki | 7be9823 | 2007-05-06 14:50:42 -0700 | [diff] [blame] | 147 | swsusp_unset_page_free(page); |
Rafael J. Wysocki | 8357376 | 2006-12-06 20:34:18 -0800 | [diff] [blame] | 148 | |
| 149 | __free_page(page); |
Rafael J. Wysocki | f6143aa | 2006-09-25 23:32:50 -0700 | [diff] [blame] | 150 | } |
| 151 | |
Rafael J. Wysocki | b788db7 | 2006-09-25 23:32:54 -0700 | [diff] [blame] | 152 | /* struct linked_page is used to build chains of pages */ |
| 153 | |
| 154 | #define LINKED_PAGE_DATA_SIZE (PAGE_SIZE - sizeof(void *)) |
| 155 | |
| 156 | struct linked_page { |
| 157 | struct linked_page *next; |
| 158 | char data[LINKED_PAGE_DATA_SIZE]; |
Gideon Israel Dsouza | 52f5684c | 2014-04-07 15:39:20 -0700 | [diff] [blame] | 159 | } __packed; |
Rafael J. Wysocki | b788db7 | 2006-09-25 23:32:54 -0700 | [diff] [blame] | 160 | |
| 161 | static inline void |
| 162 | free_list_of_pages(struct linked_page *list, int clear_page_nosave) |
| 163 | { |
| 164 | while (list) { |
| 165 | struct linked_page *lp = list->next; |
| 166 | |
| 167 | free_image_page(list, clear_page_nosave); |
| 168 | list = lp; |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | /** |
| 173 | * struct chain_allocator is used for allocating small objects out of |
| 174 | * a linked list of pages called 'the chain'. |
| 175 | * |
| 176 | * The chain grows each time when there is no room for a new object in |
| 177 | * the current page. The allocated objects cannot be freed individually. |
| 178 | * It is only possible to free them all at once, by freeing the entire |
| 179 | * chain. |
| 180 | * |
| 181 | * NOTE: The chain allocator may be inefficient if the allocated objects |
| 182 | * are not much smaller than PAGE_SIZE. |
| 183 | */ |
| 184 | |
| 185 | struct chain_allocator { |
| 186 | struct linked_page *chain; /* the chain */ |
| 187 | unsigned int used_space; /* total size of objects allocated out |
| 188 | * of the current page |
| 189 | */ |
| 190 | gfp_t gfp_mask; /* mask for allocating pages */ |
| 191 | int safe_needed; /* if set, only "safe" pages are allocated */ |
| 192 | }; |
| 193 | |
| 194 | static void |
| 195 | chain_init(struct chain_allocator *ca, gfp_t gfp_mask, int safe_needed) |
| 196 | { |
| 197 | ca->chain = NULL; |
| 198 | ca->used_space = LINKED_PAGE_DATA_SIZE; |
| 199 | ca->gfp_mask = gfp_mask; |
| 200 | ca->safe_needed = safe_needed; |
| 201 | } |
| 202 | |
| 203 | static void *chain_alloc(struct chain_allocator *ca, unsigned int size) |
| 204 | { |
| 205 | void *ret; |
| 206 | |
| 207 | if (LINKED_PAGE_DATA_SIZE - ca->used_space < size) { |
| 208 | struct linked_page *lp; |
| 209 | |
Rafael J. Wysocki | 8357376 | 2006-12-06 20:34:18 -0800 | [diff] [blame] | 210 | lp = get_image_page(ca->gfp_mask, ca->safe_needed); |
Rafael J. Wysocki | b788db7 | 2006-09-25 23:32:54 -0700 | [diff] [blame] | 211 | if (!lp) |
| 212 | return NULL; |
| 213 | |
| 214 | lp->next = ca->chain; |
| 215 | ca->chain = lp; |
| 216 | ca->used_space = 0; |
| 217 | } |
| 218 | ret = ca->chain->data + ca->used_space; |
| 219 | ca->used_space += size; |
| 220 | return ret; |
| 221 | } |
| 222 | |
Rafael J. Wysocki | b788db7 | 2006-09-25 23:32:54 -0700 | [diff] [blame] | 223 | /** |
| 224 | * Data types related to memory bitmaps. |
| 225 | * |
| 226 | * Memory bitmap is a structure consiting of many linked lists of |
| 227 | * objects. The main list's elements are of type struct zone_bitmap |
| 228 | * and each of them corresonds to one zone. For each zone bitmap |
| 229 | * object there is a list of objects of type struct bm_block that |
Akinobu Mita | 0d83304 | 2008-07-23 21:28:38 -0700 | [diff] [blame] | 230 | * represent each blocks of bitmap in which information is stored. |
Rafael J. Wysocki | b788db7 | 2006-09-25 23:32:54 -0700 | [diff] [blame] | 231 | * |
| 232 | * struct memory_bitmap contains a pointer to the main list of zone |
| 233 | * bitmap objects, a struct bm_position used for browsing the bitmap, |
| 234 | * and a pointer to the list of pages used for allocating all of the |
| 235 | * zone bitmap objects and bitmap block objects. |
| 236 | * |
| 237 | * NOTE: It has to be possible to lay out the bitmap in memory |
| 238 | * using only allocations of order 0. Additionally, the bitmap is |
| 239 | * designed to work with arbitrary number of zones (this is over the |
| 240 | * top for now, but let's avoid making unnecessary assumptions ;-). |
| 241 | * |
| 242 | * struct zone_bitmap contains a pointer to a list of bitmap block |
| 243 | * objects and a pointer to the bitmap block object that has been |
| 244 | * most recently used for setting bits. Additionally, it contains the |
| 245 | * pfns that correspond to the start and end of the represented zone. |
| 246 | * |
| 247 | * struct bm_block contains a pointer to the memory page in which |
Akinobu Mita | 0d83304 | 2008-07-23 21:28:38 -0700 | [diff] [blame] | 248 | * information is stored (in the form of a block of bitmap) |
| 249 | * It also contains the pfns that correspond to the start and end of |
| 250 | * the represented memory area. |
Joerg Roedel | f469f02 | 2014-07-21 12:26:57 +0200 | [diff] [blame] | 251 | * |
| 252 | * The memory bitmap is organized as a radix tree to guarantee fast random |
| 253 | * access to the bits. There is one radix tree for each zone (as returned |
| 254 | * from create_mem_extents). |
| 255 | * |
| 256 | * One radix tree is represented by one struct mem_zone_bm_rtree. There are |
| 257 | * two linked lists for the nodes of the tree, one for the inner nodes and |
| 258 | * one for the leave nodes. The linked leave nodes are used for fast linear |
| 259 | * access of the memory bitmap. |
| 260 | * |
| 261 | * The struct rtree_node represents one node of the radix tree. |
Rafael J. Wysocki | b788db7 | 2006-09-25 23:32:54 -0700 | [diff] [blame] | 262 | */ |
| 263 | |
| 264 | #define BM_END_OF_MAP (~0UL) |
| 265 | |
Wu Fengguang | 8de0307 | 2009-07-22 19:56:10 +0200 | [diff] [blame] | 266 | #define BM_BITS_PER_BLOCK (PAGE_SIZE * BITS_PER_BYTE) |
Joerg Roedel | f469f02 | 2014-07-21 12:26:57 +0200 | [diff] [blame] | 267 | #define BM_BLOCK_SHIFT (PAGE_SHIFT + 3) |
| 268 | #define BM_BLOCK_MASK ((1UL << BM_BLOCK_SHIFT) - 1) |
Rafael J. Wysocki | b788db7 | 2006-09-25 23:32:54 -0700 | [diff] [blame] | 269 | |
| 270 | struct bm_block { |
Rafael J. Wysocki | 846705d | 2008-11-26 18:00:24 -0500 | [diff] [blame] | 271 | struct list_head hook; /* hook into a list of bitmap blocks */ |
Rafael J. Wysocki | b788db7 | 2006-09-25 23:32:54 -0700 | [diff] [blame] | 272 | unsigned long start_pfn; /* pfn represented by the first bit */ |
| 273 | unsigned long end_pfn; /* pfn represented by the last bit plus 1 */ |
Akinobu Mita | 0d83304 | 2008-07-23 21:28:38 -0700 | [diff] [blame] | 274 | unsigned long *data; /* bitmap representing pages */ |
Rafael J. Wysocki | b788db7 | 2006-09-25 23:32:54 -0700 | [diff] [blame] | 275 | }; |
| 276 | |
Akinobu Mita | 0d83304 | 2008-07-23 21:28:38 -0700 | [diff] [blame] | 277 | static inline unsigned long bm_block_bits(struct bm_block *bb) |
| 278 | { |
| 279 | return bb->end_pfn - bb->start_pfn; |
| 280 | } |
| 281 | |
Joerg Roedel | f469f02 | 2014-07-21 12:26:57 +0200 | [diff] [blame] | 282 | /* |
| 283 | * struct rtree_node is a wrapper struct to link the nodes |
| 284 | * of the rtree together for easy linear iteration over |
| 285 | * bits and easy freeing |
| 286 | */ |
| 287 | struct rtree_node { |
| 288 | struct list_head list; |
| 289 | unsigned long *data; |
| 290 | }; |
| 291 | |
| 292 | /* |
| 293 | * struct mem_zone_bm_rtree represents a bitmap used for one |
| 294 | * populated memory zone. |
| 295 | */ |
| 296 | struct mem_zone_bm_rtree { |
| 297 | struct list_head list; /* Link Zones together */ |
| 298 | struct list_head nodes; /* Radix Tree inner nodes */ |
| 299 | struct list_head leaves; /* Radix Tree leaves */ |
| 300 | unsigned long start_pfn; /* Zone start page frame */ |
| 301 | unsigned long end_pfn; /* Zone end page frame + 1 */ |
| 302 | struct rtree_node *rtree; /* Radix Tree Root */ |
| 303 | int levels; /* Number of Radix Tree Levels */ |
| 304 | unsigned int blocks; /* Number of Bitmap Blocks */ |
| 305 | }; |
| 306 | |
Rafael J. Wysocki | b788db7 | 2006-09-25 23:32:54 -0700 | [diff] [blame] | 307 | /* strcut bm_position is used for browsing memory bitmaps */ |
| 308 | |
| 309 | struct bm_position { |
Rafael J. Wysocki | b788db7 | 2006-09-25 23:32:54 -0700 | [diff] [blame] | 310 | struct bm_block *block; |
Rafael J. Wysocki | b788db7 | 2006-09-25 23:32:54 -0700 | [diff] [blame] | 311 | int bit; |
| 312 | }; |
| 313 | |
| 314 | struct memory_bitmap { |
Joerg Roedel | f469f02 | 2014-07-21 12:26:57 +0200 | [diff] [blame] | 315 | struct list_head zones; |
Rafael J. Wysocki | 846705d | 2008-11-26 18:00:24 -0500 | [diff] [blame] | 316 | struct list_head blocks; /* list of bitmap blocks */ |
Rafael J. Wysocki | b788db7 | 2006-09-25 23:32:54 -0700 | [diff] [blame] | 317 | struct linked_page *p_list; /* list of pages used to store zone |
| 318 | * bitmap objects and bitmap block |
| 319 | * objects |
| 320 | */ |
| 321 | struct bm_position cur; /* most recently used bit position */ |
| 322 | }; |
| 323 | |
| 324 | /* Functions that operate on memory bitmaps */ |
| 325 | |
Joerg Roedel | f469f02 | 2014-07-21 12:26:57 +0200 | [diff] [blame] | 326 | #define BM_ENTRIES_PER_LEVEL (PAGE_SIZE / sizeof(unsigned long)) |
| 327 | #if BITS_PER_LONG == 32 |
| 328 | #define BM_RTREE_LEVEL_SHIFT (PAGE_SHIFT - 2) |
| 329 | #else |
| 330 | #define BM_RTREE_LEVEL_SHIFT (PAGE_SHIFT - 3) |
| 331 | #endif |
| 332 | #define BM_RTREE_LEVEL_MASK ((1UL << BM_RTREE_LEVEL_SHIFT) - 1) |
| 333 | |
| 334 | /* |
| 335 | * alloc_rtree_node - Allocate a new node and add it to the radix tree. |
| 336 | * |
| 337 | * This function is used to allocate inner nodes as well as the |
| 338 | * leave nodes of the radix tree. It also adds the node to the |
| 339 | * corresponding linked list passed in by the *list parameter. |
| 340 | */ |
| 341 | static struct rtree_node *alloc_rtree_node(gfp_t gfp_mask, int safe_needed, |
| 342 | struct chain_allocator *ca, |
| 343 | struct list_head *list) |
| 344 | { |
| 345 | struct rtree_node *node; |
| 346 | |
| 347 | node = chain_alloc(ca, sizeof(struct rtree_node)); |
| 348 | if (!node) |
| 349 | return NULL; |
| 350 | |
| 351 | node->data = get_image_page(gfp_mask, safe_needed); |
| 352 | if (!node->data) |
| 353 | return NULL; |
| 354 | |
| 355 | list_add_tail(&node->list, list); |
| 356 | |
| 357 | return node; |
| 358 | } |
| 359 | |
| 360 | /* |
| 361 | * add_rtree_block - Add a new leave node to the radix tree |
| 362 | * |
| 363 | * The leave nodes need to be allocated in order to keep the leaves |
| 364 | * linked list in order. This is guaranteed by the zone->blocks |
| 365 | * counter. |
| 366 | */ |
| 367 | static int add_rtree_block(struct mem_zone_bm_rtree *zone, gfp_t gfp_mask, |
| 368 | int safe_needed, struct chain_allocator *ca) |
| 369 | { |
| 370 | struct rtree_node *node, *block, **dst; |
| 371 | unsigned int levels_needed, block_nr; |
| 372 | int i; |
| 373 | |
| 374 | block_nr = zone->blocks; |
| 375 | levels_needed = 0; |
| 376 | |
| 377 | /* How many levels do we need for this block nr? */ |
| 378 | while (block_nr) { |
| 379 | levels_needed += 1; |
| 380 | block_nr >>= BM_RTREE_LEVEL_SHIFT; |
| 381 | } |
| 382 | |
| 383 | /* Make sure the rtree has enough levels */ |
| 384 | for (i = zone->levels; i < levels_needed; i++) { |
| 385 | node = alloc_rtree_node(gfp_mask, safe_needed, ca, |
| 386 | &zone->nodes); |
| 387 | if (!node) |
| 388 | return -ENOMEM; |
| 389 | |
| 390 | node->data[0] = (unsigned long)zone->rtree; |
| 391 | zone->rtree = node; |
| 392 | zone->levels += 1; |
| 393 | } |
| 394 | |
| 395 | /* Allocate new block */ |
| 396 | block = alloc_rtree_node(gfp_mask, safe_needed, ca, &zone->leaves); |
| 397 | if (!block) |
| 398 | return -ENOMEM; |
| 399 | |
| 400 | /* Now walk the rtree to insert the block */ |
| 401 | node = zone->rtree; |
| 402 | dst = &zone->rtree; |
| 403 | block_nr = zone->blocks; |
| 404 | for (i = zone->levels; i > 0; i--) { |
| 405 | int index; |
| 406 | |
| 407 | if (!node) { |
| 408 | node = alloc_rtree_node(gfp_mask, safe_needed, ca, |
| 409 | &zone->nodes); |
| 410 | if (!node) |
| 411 | return -ENOMEM; |
| 412 | *dst = node; |
| 413 | } |
| 414 | |
| 415 | index = block_nr >> ((i - 1) * BM_RTREE_LEVEL_SHIFT); |
| 416 | index &= BM_RTREE_LEVEL_MASK; |
| 417 | dst = (struct rtree_node **)&((*dst)->data[index]); |
| 418 | node = *dst; |
| 419 | } |
| 420 | |
| 421 | zone->blocks += 1; |
| 422 | *dst = block; |
| 423 | |
| 424 | return 0; |
| 425 | } |
| 426 | |
| 427 | static void free_zone_bm_rtree(struct mem_zone_bm_rtree *zone, |
| 428 | int clear_nosave_free); |
| 429 | |
| 430 | /* |
| 431 | * create_zone_bm_rtree - create a radix tree for one zone |
| 432 | * |
| 433 | * Allocated the mem_zone_bm_rtree structure and initializes it. |
| 434 | * This function also allocated and builds the radix tree for the |
| 435 | * zone. |
| 436 | */ |
| 437 | static struct mem_zone_bm_rtree * |
| 438 | create_zone_bm_rtree(gfp_t gfp_mask, int safe_needed, |
| 439 | struct chain_allocator *ca, |
| 440 | unsigned long start, unsigned long end) |
| 441 | { |
| 442 | struct mem_zone_bm_rtree *zone; |
| 443 | unsigned int i, nr_blocks; |
| 444 | unsigned long pages; |
| 445 | |
| 446 | pages = end - start; |
| 447 | zone = chain_alloc(ca, sizeof(struct mem_zone_bm_rtree)); |
| 448 | if (!zone) |
| 449 | return NULL; |
| 450 | |
| 451 | INIT_LIST_HEAD(&zone->nodes); |
| 452 | INIT_LIST_HEAD(&zone->leaves); |
| 453 | zone->start_pfn = start; |
| 454 | zone->end_pfn = end; |
| 455 | nr_blocks = DIV_ROUND_UP(pages, BM_BITS_PER_BLOCK); |
| 456 | |
| 457 | for (i = 0; i < nr_blocks; i++) { |
| 458 | if (add_rtree_block(zone, gfp_mask, safe_needed, ca)) { |
| 459 | free_zone_bm_rtree(zone, PG_UNSAFE_CLEAR); |
| 460 | return NULL; |
| 461 | } |
| 462 | } |
| 463 | |
| 464 | return zone; |
| 465 | } |
| 466 | |
| 467 | /* |
| 468 | * free_zone_bm_rtree - Free the memory of the radix tree |
| 469 | * |
| 470 | * Free all node pages of the radix tree. The mem_zone_bm_rtree |
| 471 | * structure itself is not freed here nor are the rtree_node |
| 472 | * structs. |
| 473 | */ |
| 474 | static void free_zone_bm_rtree(struct mem_zone_bm_rtree *zone, |
| 475 | int clear_nosave_free) |
| 476 | { |
| 477 | struct rtree_node *node; |
| 478 | |
| 479 | list_for_each_entry(node, &zone->nodes, list) |
| 480 | free_image_page(node->data, clear_nosave_free); |
| 481 | |
| 482 | list_for_each_entry(node, &zone->leaves, list) |
| 483 | free_image_page(node->data, clear_nosave_free); |
| 484 | } |
| 485 | |
Rafael J. Wysocki | b788db7 | 2006-09-25 23:32:54 -0700 | [diff] [blame] | 486 | static void memory_bm_position_reset(struct memory_bitmap *bm) |
| 487 | { |
Rafael J. Wysocki | 846705d | 2008-11-26 18:00:24 -0500 | [diff] [blame] | 488 | bm->cur.block = list_entry(bm->blocks.next, struct bm_block, hook); |
Akinobu Mita | 0d83304 | 2008-07-23 21:28:38 -0700 | [diff] [blame] | 489 | bm->cur.bit = 0; |
Rafael J. Wysocki | b788db7 | 2006-09-25 23:32:54 -0700 | [diff] [blame] | 490 | } |
| 491 | |
| 492 | static void memory_bm_free(struct memory_bitmap *bm, int clear_nosave_free); |
| 493 | |
| 494 | /** |
| 495 | * create_bm_block_list - create a list of block bitmap objects |
Wu Fengguang | 8de0307 | 2009-07-22 19:56:10 +0200 | [diff] [blame] | 496 | * @pages - number of pages to track |
Rafael J. Wysocki | 846705d | 2008-11-26 18:00:24 -0500 | [diff] [blame] | 497 | * @list - list to put the allocated blocks into |
| 498 | * @ca - chain allocator to be used for allocating memory |
Rafael J. Wysocki | b788db7 | 2006-09-25 23:32:54 -0700 | [diff] [blame] | 499 | */ |
Rafael J. Wysocki | 846705d | 2008-11-26 18:00:24 -0500 | [diff] [blame] | 500 | static int create_bm_block_list(unsigned long pages, |
| 501 | struct list_head *list, |
| 502 | struct chain_allocator *ca) |
Rafael J. Wysocki | b788db7 | 2006-09-25 23:32:54 -0700 | [diff] [blame] | 503 | { |
Rafael J. Wysocki | 846705d | 2008-11-26 18:00:24 -0500 | [diff] [blame] | 504 | unsigned int nr_blocks = DIV_ROUND_UP(pages, BM_BITS_PER_BLOCK); |
Rafael J. Wysocki | b788db7 | 2006-09-25 23:32:54 -0700 | [diff] [blame] | 505 | |
| 506 | while (nr_blocks-- > 0) { |
| 507 | struct bm_block *bb; |
| 508 | |
| 509 | bb = chain_alloc(ca, sizeof(struct bm_block)); |
| 510 | if (!bb) |
Rafael J. Wysocki | 846705d | 2008-11-26 18:00:24 -0500 | [diff] [blame] | 511 | return -ENOMEM; |
| 512 | list_add(&bb->hook, list); |
Rafael J. Wysocki | b788db7 | 2006-09-25 23:32:54 -0700 | [diff] [blame] | 513 | } |
Rafael J. Wysocki | 846705d | 2008-11-26 18:00:24 -0500 | [diff] [blame] | 514 | |
| 515 | return 0; |
| 516 | } |
| 517 | |
| 518 | struct mem_extent { |
| 519 | struct list_head hook; |
| 520 | unsigned long start; |
| 521 | unsigned long end; |
| 522 | }; |
| 523 | |
| 524 | /** |
| 525 | * free_mem_extents - free a list of memory extents |
| 526 | * @list - list of extents to empty |
| 527 | */ |
| 528 | static void free_mem_extents(struct list_head *list) |
| 529 | { |
| 530 | struct mem_extent *ext, *aux; |
| 531 | |
| 532 | list_for_each_entry_safe(ext, aux, list, hook) { |
| 533 | list_del(&ext->hook); |
| 534 | kfree(ext); |
| 535 | } |
Rafael J. Wysocki | b788db7 | 2006-09-25 23:32:54 -0700 | [diff] [blame] | 536 | } |
| 537 | |
| 538 | /** |
Rafael J. Wysocki | 846705d | 2008-11-26 18:00:24 -0500 | [diff] [blame] | 539 | * create_mem_extents - create a list of memory extents representing |
| 540 | * contiguous ranges of PFNs |
| 541 | * @list - list to put the extents into |
| 542 | * @gfp_mask - mask to use for memory allocations |
Rafael J. Wysocki | b788db7 | 2006-09-25 23:32:54 -0700 | [diff] [blame] | 543 | */ |
Rafael J. Wysocki | 846705d | 2008-11-26 18:00:24 -0500 | [diff] [blame] | 544 | static int create_mem_extents(struct list_head *list, gfp_t gfp_mask) |
Rafael J. Wysocki | b788db7 | 2006-09-25 23:32:54 -0700 | [diff] [blame] | 545 | { |
Rafael J. Wysocki | 846705d | 2008-11-26 18:00:24 -0500 | [diff] [blame] | 546 | struct zone *zone; |
Rafael J. Wysocki | b788db7 | 2006-09-25 23:32:54 -0700 | [diff] [blame] | 547 | |
Rafael J. Wysocki | 846705d | 2008-11-26 18:00:24 -0500 | [diff] [blame] | 548 | INIT_LIST_HEAD(list); |
Rafael J. Wysocki | b788db7 | 2006-09-25 23:32:54 -0700 | [diff] [blame] | 549 | |
KOSAKI Motohiro | ee99c71 | 2009-03-31 15:19:31 -0700 | [diff] [blame] | 550 | for_each_populated_zone(zone) { |
Rafael J. Wysocki | 846705d | 2008-11-26 18:00:24 -0500 | [diff] [blame] | 551 | unsigned long zone_start, zone_end; |
| 552 | struct mem_extent *ext, *cur, *aux; |
Rafael J. Wysocki | b788db7 | 2006-09-25 23:32:54 -0700 | [diff] [blame] | 553 | |
Rafael J. Wysocki | 846705d | 2008-11-26 18:00:24 -0500 | [diff] [blame] | 554 | zone_start = zone->zone_start_pfn; |
Xishi Qiu | c33bc31 | 2013-09-11 14:21:44 -0700 | [diff] [blame] | 555 | zone_end = zone_end_pfn(zone); |
Rafael J. Wysocki | 846705d | 2008-11-26 18:00:24 -0500 | [diff] [blame] | 556 | |
| 557 | list_for_each_entry(ext, list, hook) |
| 558 | if (zone_start <= ext->end) |
| 559 | break; |
| 560 | |
| 561 | if (&ext->hook == list || zone_end < ext->start) { |
| 562 | /* New extent is necessary */ |
| 563 | struct mem_extent *new_ext; |
| 564 | |
| 565 | new_ext = kzalloc(sizeof(struct mem_extent), gfp_mask); |
| 566 | if (!new_ext) { |
| 567 | free_mem_extents(list); |
| 568 | return -ENOMEM; |
| 569 | } |
| 570 | new_ext->start = zone_start; |
| 571 | new_ext->end = zone_end; |
| 572 | list_add_tail(&new_ext->hook, &ext->hook); |
| 573 | continue; |
| 574 | } |
| 575 | |
| 576 | /* Merge this zone's range of PFNs with the existing one */ |
| 577 | if (zone_start < ext->start) |
| 578 | ext->start = zone_start; |
| 579 | if (zone_end > ext->end) |
| 580 | ext->end = zone_end; |
| 581 | |
| 582 | /* More merging may be possible */ |
| 583 | cur = ext; |
| 584 | list_for_each_entry_safe_continue(cur, aux, list, hook) { |
| 585 | if (zone_end < cur->start) |
| 586 | break; |
| 587 | if (zone_end < cur->end) |
| 588 | ext->end = cur->end; |
| 589 | list_del(&cur->hook); |
| 590 | kfree(cur); |
| 591 | } |
Rafael J. Wysocki | b788db7 | 2006-09-25 23:32:54 -0700 | [diff] [blame] | 592 | } |
Rafael J. Wysocki | 846705d | 2008-11-26 18:00:24 -0500 | [diff] [blame] | 593 | |
| 594 | return 0; |
Rafael J. Wysocki | b788db7 | 2006-09-25 23:32:54 -0700 | [diff] [blame] | 595 | } |
| 596 | |
| 597 | /** |
| 598 | * memory_bm_create - allocate memory for a memory bitmap |
| 599 | */ |
Rafael J. Wysocki | b788db7 | 2006-09-25 23:32:54 -0700 | [diff] [blame] | 600 | static int |
| 601 | memory_bm_create(struct memory_bitmap *bm, gfp_t gfp_mask, int safe_needed) |
| 602 | { |
| 603 | struct chain_allocator ca; |
Rafael J. Wysocki | 846705d | 2008-11-26 18:00:24 -0500 | [diff] [blame] | 604 | struct list_head mem_extents; |
| 605 | struct mem_extent *ext; |
| 606 | int error; |
Rafael J. Wysocki | b788db7 | 2006-09-25 23:32:54 -0700 | [diff] [blame] | 607 | |
| 608 | chain_init(&ca, gfp_mask, safe_needed); |
Rafael J. Wysocki | 846705d | 2008-11-26 18:00:24 -0500 | [diff] [blame] | 609 | INIT_LIST_HEAD(&bm->blocks); |
Joerg Roedel | f469f02 | 2014-07-21 12:26:57 +0200 | [diff] [blame] | 610 | INIT_LIST_HEAD(&bm->zones); |
Rafael J. Wysocki | b788db7 | 2006-09-25 23:32:54 -0700 | [diff] [blame] | 611 | |
Rafael J. Wysocki | 846705d | 2008-11-26 18:00:24 -0500 | [diff] [blame] | 612 | error = create_mem_extents(&mem_extents, gfp_mask); |
| 613 | if (error) |
| 614 | return error; |
Rafael J. Wysocki | b788db7 | 2006-09-25 23:32:54 -0700 | [diff] [blame] | 615 | |
Rafael J. Wysocki | 846705d | 2008-11-26 18:00:24 -0500 | [diff] [blame] | 616 | list_for_each_entry(ext, &mem_extents, hook) { |
Joerg Roedel | f469f02 | 2014-07-21 12:26:57 +0200 | [diff] [blame] | 617 | struct mem_zone_bm_rtree *zone; |
Rafael J. Wysocki | 846705d | 2008-11-26 18:00:24 -0500 | [diff] [blame] | 618 | struct bm_block *bb; |
| 619 | unsigned long pfn = ext->start; |
| 620 | unsigned long pages = ext->end - ext->start; |
Rafael J. Wysocki | b788db7 | 2006-09-25 23:32:54 -0700 | [diff] [blame] | 621 | |
Rafael J. Wysocki | 846705d | 2008-11-26 18:00:24 -0500 | [diff] [blame] | 622 | bb = list_entry(bm->blocks.prev, struct bm_block, hook); |
Rafael J. Wysocki | b788db7 | 2006-09-25 23:32:54 -0700 | [diff] [blame] | 623 | |
Rafael J. Wysocki | 846705d | 2008-11-26 18:00:24 -0500 | [diff] [blame] | 624 | error = create_bm_block_list(pages, bm->blocks.prev, &ca); |
| 625 | if (error) |
| 626 | goto Error; |
Rafael J. Wysocki | b788db7 | 2006-09-25 23:32:54 -0700 | [diff] [blame] | 627 | |
Rafael J. Wysocki | 846705d | 2008-11-26 18:00:24 -0500 | [diff] [blame] | 628 | list_for_each_entry_continue(bb, &bm->blocks, hook) { |
| 629 | bb->data = get_image_page(gfp_mask, safe_needed); |
| 630 | if (!bb->data) { |
| 631 | error = -ENOMEM; |
| 632 | goto Error; |
| 633 | } |
Rafael J. Wysocki | b788db7 | 2006-09-25 23:32:54 -0700 | [diff] [blame] | 634 | |
| 635 | bb->start_pfn = pfn; |
Rafael J. Wysocki | 846705d | 2008-11-26 18:00:24 -0500 | [diff] [blame] | 636 | if (pages >= BM_BITS_PER_BLOCK) { |
Rafael J. Wysocki | b788db7 | 2006-09-25 23:32:54 -0700 | [diff] [blame] | 637 | pfn += BM_BITS_PER_BLOCK; |
Rafael J. Wysocki | 846705d | 2008-11-26 18:00:24 -0500 | [diff] [blame] | 638 | pages -= BM_BITS_PER_BLOCK; |
Rafael J. Wysocki | b788db7 | 2006-09-25 23:32:54 -0700 | [diff] [blame] | 639 | } else { |
| 640 | /* This is executed only once in the loop */ |
Rafael J. Wysocki | 846705d | 2008-11-26 18:00:24 -0500 | [diff] [blame] | 641 | pfn += pages; |
Rafael J. Wysocki | b788db7 | 2006-09-25 23:32:54 -0700 | [diff] [blame] | 642 | } |
| 643 | bb->end_pfn = pfn; |
Rafael J. Wysocki | b788db7 | 2006-09-25 23:32:54 -0700 | [diff] [blame] | 644 | } |
Joerg Roedel | f469f02 | 2014-07-21 12:26:57 +0200 | [diff] [blame] | 645 | |
| 646 | zone = create_zone_bm_rtree(gfp_mask, safe_needed, &ca, |
| 647 | ext->start, ext->end); |
| 648 | if (!zone) |
| 649 | goto Error; |
| 650 | list_add_tail(&zone->list, &bm->zones); |
Rafael J. Wysocki | b788db7 | 2006-09-25 23:32:54 -0700 | [diff] [blame] | 651 | } |
Rafael J. Wysocki | 846705d | 2008-11-26 18:00:24 -0500 | [diff] [blame] | 652 | |
Rafael J. Wysocki | b788db7 | 2006-09-25 23:32:54 -0700 | [diff] [blame] | 653 | bm->p_list = ca.chain; |
| 654 | memory_bm_position_reset(bm); |
Rafael J. Wysocki | 846705d | 2008-11-26 18:00:24 -0500 | [diff] [blame] | 655 | Exit: |
| 656 | free_mem_extents(&mem_extents); |
| 657 | return error; |
Rafael J. Wysocki | b788db7 | 2006-09-25 23:32:54 -0700 | [diff] [blame] | 658 | |
Rafael J. Wysocki | 846705d | 2008-11-26 18:00:24 -0500 | [diff] [blame] | 659 | Error: |
Rafael J. Wysocki | b788db7 | 2006-09-25 23:32:54 -0700 | [diff] [blame] | 660 | bm->p_list = ca.chain; |
| 661 | memory_bm_free(bm, PG_UNSAFE_CLEAR); |
Rafael J. Wysocki | 846705d | 2008-11-26 18:00:24 -0500 | [diff] [blame] | 662 | goto Exit; |
Rafael J. Wysocki | b788db7 | 2006-09-25 23:32:54 -0700 | [diff] [blame] | 663 | } |
| 664 | |
| 665 | /** |
| 666 | * memory_bm_free - free memory occupied by the memory bitmap @bm |
| 667 | */ |
Rafael J. Wysocki | b788db7 | 2006-09-25 23:32:54 -0700 | [diff] [blame] | 668 | static void memory_bm_free(struct memory_bitmap *bm, int clear_nosave_free) |
| 669 | { |
Joerg Roedel | f469f02 | 2014-07-21 12:26:57 +0200 | [diff] [blame] | 670 | struct mem_zone_bm_rtree *zone; |
Rafael J. Wysocki | 846705d | 2008-11-26 18:00:24 -0500 | [diff] [blame] | 671 | struct bm_block *bb; |
Rafael J. Wysocki | b788db7 | 2006-09-25 23:32:54 -0700 | [diff] [blame] | 672 | |
Rafael J. Wysocki | 846705d | 2008-11-26 18:00:24 -0500 | [diff] [blame] | 673 | list_for_each_entry(bb, &bm->blocks, hook) |
| 674 | if (bb->data) |
| 675 | free_image_page(bb->data, clear_nosave_free); |
Rafael J. Wysocki | b788db7 | 2006-09-25 23:32:54 -0700 | [diff] [blame] | 676 | |
Joerg Roedel | f469f02 | 2014-07-21 12:26:57 +0200 | [diff] [blame] | 677 | list_for_each_entry(zone, &bm->zones, list) |
| 678 | free_zone_bm_rtree(zone, clear_nosave_free); |
| 679 | |
Rafael J. Wysocki | b788db7 | 2006-09-25 23:32:54 -0700 | [diff] [blame] | 680 | free_list_of_pages(bm->p_list, clear_nosave_free); |
Rafael J. Wysocki | 846705d | 2008-11-26 18:00:24 -0500 | [diff] [blame] | 681 | |
Joerg Roedel | f469f02 | 2014-07-21 12:26:57 +0200 | [diff] [blame] | 682 | INIT_LIST_HEAD(&bm->zones); |
Rafael J. Wysocki | 846705d | 2008-11-26 18:00:24 -0500 | [diff] [blame] | 683 | INIT_LIST_HEAD(&bm->blocks); |
Rafael J. Wysocki | b788db7 | 2006-09-25 23:32:54 -0700 | [diff] [blame] | 684 | } |
| 685 | |
| 686 | /** |
Rafael J. Wysocki | 74dfd66 | 2007-05-06 14:50:43 -0700 | [diff] [blame] | 687 | * memory_bm_find_bit - find the bit in the bitmap @bm that corresponds |
Rafael J. Wysocki | b788db7 | 2006-09-25 23:32:54 -0700 | [diff] [blame] | 688 | * to given pfn. The cur_zone_bm member of @bm and the cur_block member |
| 689 | * of @bm->cur_zone_bm are updated. |
Rafael J. Wysocki | b788db7 | 2006-09-25 23:32:54 -0700 | [diff] [blame] | 690 | */ |
Rafael J. Wysocki | a82f711 | 2008-03-12 00:34:57 +0100 | [diff] [blame] | 691 | static int memory_bm_find_bit(struct memory_bitmap *bm, unsigned long pfn, |
Rafael J. Wysocki | 74dfd66 | 2007-05-06 14:50:43 -0700 | [diff] [blame] | 692 | void **addr, unsigned int *bit_nr) |
Rafael J. Wysocki | b788db7 | 2006-09-25 23:32:54 -0700 | [diff] [blame] | 693 | { |
Rafael J. Wysocki | b788db7 | 2006-09-25 23:32:54 -0700 | [diff] [blame] | 694 | struct bm_block *bb; |
| 695 | |
Rafael J. Wysocki | 846705d | 2008-11-26 18:00:24 -0500 | [diff] [blame] | 696 | /* |
| 697 | * Check if the pfn corresponds to the current bitmap block and find |
| 698 | * the block where it fits if this is not the case. |
| 699 | */ |
| 700 | bb = bm->cur.block; |
Rafael J. Wysocki | b788db7 | 2006-09-25 23:32:54 -0700 | [diff] [blame] | 701 | if (pfn < bb->start_pfn) |
Rafael J. Wysocki | 846705d | 2008-11-26 18:00:24 -0500 | [diff] [blame] | 702 | list_for_each_entry_continue_reverse(bb, &bm->blocks, hook) |
| 703 | if (pfn >= bb->start_pfn) |
| 704 | break; |
Rafael J. Wysocki | b788db7 | 2006-09-25 23:32:54 -0700 | [diff] [blame] | 705 | |
Rafael J. Wysocki | 846705d | 2008-11-26 18:00:24 -0500 | [diff] [blame] | 706 | if (pfn >= bb->end_pfn) |
| 707 | list_for_each_entry_continue(bb, &bm->blocks, hook) |
| 708 | if (pfn >= bb->start_pfn && pfn < bb->end_pfn) |
| 709 | break; |
Rafael J. Wysocki | 74dfd66 | 2007-05-06 14:50:43 -0700 | [diff] [blame] | 710 | |
Rafael J. Wysocki | 846705d | 2008-11-26 18:00:24 -0500 | [diff] [blame] | 711 | if (&bb->hook == &bm->blocks) |
| 712 | return -EFAULT; |
| 713 | |
| 714 | /* The block has been found */ |
| 715 | bm->cur.block = bb; |
Rafael J. Wysocki | b788db7 | 2006-09-25 23:32:54 -0700 | [diff] [blame] | 716 | pfn -= bb->start_pfn; |
Rafael J. Wysocki | 846705d | 2008-11-26 18:00:24 -0500 | [diff] [blame] | 717 | bm->cur.bit = pfn + 1; |
Akinobu Mita | 0d83304 | 2008-07-23 21:28:38 -0700 | [diff] [blame] | 718 | *bit_nr = pfn; |
| 719 | *addr = bb->data; |
Rafael J. Wysocki | a82f711 | 2008-03-12 00:34:57 +0100 | [diff] [blame] | 720 | return 0; |
Rafael J. Wysocki | 74dfd66 | 2007-05-06 14:50:43 -0700 | [diff] [blame] | 721 | } |
| 722 | |
Joerg Roedel | 07a3382 | 2014-07-21 12:26:58 +0200 | [diff] [blame^] | 723 | /* |
| 724 | * memory_rtree_find_bit - Find the bit for pfn in the memory |
| 725 | * bitmap |
| 726 | * |
| 727 | * Walks the radix tree to find the page which contains the bit for |
| 728 | * pfn and returns the bit position in **addr and *bit_nr. |
| 729 | */ |
| 730 | static int memory_rtree_find_bit(struct memory_bitmap *bm, unsigned long pfn, |
| 731 | void **addr, unsigned int *bit_nr) |
| 732 | { |
| 733 | struct mem_zone_bm_rtree *curr, *zone; |
| 734 | struct rtree_node *node; |
| 735 | int i, block_nr; |
| 736 | |
| 737 | zone = NULL; |
| 738 | |
| 739 | /* Find the right zone */ |
| 740 | list_for_each_entry(curr, &bm->zones, list) { |
| 741 | if (pfn >= curr->start_pfn && pfn < curr->end_pfn) { |
| 742 | zone = curr; |
| 743 | break; |
| 744 | } |
| 745 | } |
| 746 | |
| 747 | if (!zone) |
| 748 | return -EFAULT; |
| 749 | |
| 750 | /* |
| 751 | * We have a zone. Now walk the radix tree to find the leave |
| 752 | * node for our pfn. |
| 753 | */ |
| 754 | node = zone->rtree; |
| 755 | block_nr = (pfn - zone->start_pfn) >> BM_BLOCK_SHIFT; |
| 756 | |
| 757 | for (i = zone->levels; i > 0; i--) { |
| 758 | int index; |
| 759 | |
| 760 | index = block_nr >> ((i - 1) * BM_RTREE_LEVEL_SHIFT); |
| 761 | index &= BM_RTREE_LEVEL_MASK; |
| 762 | BUG_ON(node->data[index] == 0); |
| 763 | node = (struct rtree_node *)node->data[index]; |
| 764 | } |
| 765 | |
| 766 | /* Set return values */ |
| 767 | *addr = node->data; |
| 768 | *bit_nr = (pfn - zone->start_pfn) & BM_BLOCK_MASK; |
| 769 | |
| 770 | return 0; |
| 771 | } |
| 772 | |
Rafael J. Wysocki | 74dfd66 | 2007-05-06 14:50:43 -0700 | [diff] [blame] | 773 | static void memory_bm_set_bit(struct memory_bitmap *bm, unsigned long pfn) |
| 774 | { |
| 775 | void *addr; |
| 776 | unsigned int bit; |
Rafael J. Wysocki | a82f711 | 2008-03-12 00:34:57 +0100 | [diff] [blame] | 777 | int error; |
Rafael J. Wysocki | 74dfd66 | 2007-05-06 14:50:43 -0700 | [diff] [blame] | 778 | |
Rafael J. Wysocki | a82f711 | 2008-03-12 00:34:57 +0100 | [diff] [blame] | 779 | error = memory_bm_find_bit(bm, pfn, &addr, &bit); |
| 780 | BUG_ON(error); |
Rafael J. Wysocki | 74dfd66 | 2007-05-06 14:50:43 -0700 | [diff] [blame] | 781 | set_bit(bit, addr); |
Joerg Roedel | 07a3382 | 2014-07-21 12:26:58 +0200 | [diff] [blame^] | 782 | |
| 783 | error = memory_rtree_find_bit(bm, pfn, &addr, &bit); |
| 784 | BUG_ON(error); |
| 785 | set_bit(bit, addr); |
Rafael J. Wysocki | 74dfd66 | 2007-05-06 14:50:43 -0700 | [diff] [blame] | 786 | } |
| 787 | |
Rafael J. Wysocki | a82f711 | 2008-03-12 00:34:57 +0100 | [diff] [blame] | 788 | static int mem_bm_set_bit_check(struct memory_bitmap *bm, unsigned long pfn) |
| 789 | { |
| 790 | void *addr; |
| 791 | unsigned int bit; |
| 792 | int error; |
| 793 | |
| 794 | error = memory_bm_find_bit(bm, pfn, &addr, &bit); |
| 795 | if (!error) |
| 796 | set_bit(bit, addr); |
Joerg Roedel | 07a3382 | 2014-07-21 12:26:58 +0200 | [diff] [blame^] | 797 | else |
| 798 | return error; |
| 799 | |
| 800 | error = memory_rtree_find_bit(bm, pfn, &addr, &bit); |
| 801 | if (!error) |
| 802 | set_bit(bit, addr); |
| 803 | |
Rafael J. Wysocki | a82f711 | 2008-03-12 00:34:57 +0100 | [diff] [blame] | 804 | return error; |
| 805 | } |
| 806 | |
Rafael J. Wysocki | 74dfd66 | 2007-05-06 14:50:43 -0700 | [diff] [blame] | 807 | static void memory_bm_clear_bit(struct memory_bitmap *bm, unsigned long pfn) |
| 808 | { |
| 809 | void *addr; |
| 810 | unsigned int bit; |
Rafael J. Wysocki | a82f711 | 2008-03-12 00:34:57 +0100 | [diff] [blame] | 811 | int error; |
Rafael J. Wysocki | 74dfd66 | 2007-05-06 14:50:43 -0700 | [diff] [blame] | 812 | |
Rafael J. Wysocki | a82f711 | 2008-03-12 00:34:57 +0100 | [diff] [blame] | 813 | error = memory_bm_find_bit(bm, pfn, &addr, &bit); |
| 814 | BUG_ON(error); |
Rafael J. Wysocki | 74dfd66 | 2007-05-06 14:50:43 -0700 | [diff] [blame] | 815 | clear_bit(bit, addr); |
Joerg Roedel | 07a3382 | 2014-07-21 12:26:58 +0200 | [diff] [blame^] | 816 | |
| 817 | error = memory_rtree_find_bit(bm, pfn, &addr, &bit); |
| 818 | BUG_ON(error); |
| 819 | clear_bit(bit, addr); |
Rafael J. Wysocki | 74dfd66 | 2007-05-06 14:50:43 -0700 | [diff] [blame] | 820 | } |
| 821 | |
| 822 | static int memory_bm_test_bit(struct memory_bitmap *bm, unsigned long pfn) |
| 823 | { |
| 824 | void *addr; |
| 825 | unsigned int bit; |
Joerg Roedel | 07a3382 | 2014-07-21 12:26:58 +0200 | [diff] [blame^] | 826 | int error, error2; |
| 827 | int v; |
Rafael J. Wysocki | 74dfd66 | 2007-05-06 14:50:43 -0700 | [diff] [blame] | 828 | |
Rafael J. Wysocki | a82f711 | 2008-03-12 00:34:57 +0100 | [diff] [blame] | 829 | error = memory_bm_find_bit(bm, pfn, &addr, &bit); |
| 830 | BUG_ON(error); |
Joerg Roedel | 07a3382 | 2014-07-21 12:26:58 +0200 | [diff] [blame^] | 831 | v = test_bit(bit, addr); |
| 832 | |
| 833 | error2 = memory_rtree_find_bit(bm, pfn, &addr, &bit); |
| 834 | BUG_ON(error2); |
| 835 | |
| 836 | WARN_ON_ONCE(v != test_bit(bit, addr)); |
| 837 | |
| 838 | return v; |
Rafael J. Wysocki | b788db7 | 2006-09-25 23:32:54 -0700 | [diff] [blame] | 839 | } |
| 840 | |
Rafael J. Wysocki | 6964327 | 2008-11-11 21:32:44 +0100 | [diff] [blame] | 841 | static bool memory_bm_pfn_present(struct memory_bitmap *bm, unsigned long pfn) |
| 842 | { |
| 843 | void *addr; |
| 844 | unsigned int bit; |
Joerg Roedel | 07a3382 | 2014-07-21 12:26:58 +0200 | [diff] [blame^] | 845 | int present; |
Rafael J. Wysocki | 6964327 | 2008-11-11 21:32:44 +0100 | [diff] [blame] | 846 | |
Joerg Roedel | 07a3382 | 2014-07-21 12:26:58 +0200 | [diff] [blame^] | 847 | present = !memory_bm_find_bit(bm, pfn, &addr, &bit); |
| 848 | |
| 849 | WARN_ON_ONCE(present != !memory_rtree_find_bit(bm, pfn, &addr, &bit)); |
| 850 | |
| 851 | return present; |
Rafael J. Wysocki | 6964327 | 2008-11-11 21:32:44 +0100 | [diff] [blame] | 852 | } |
| 853 | |
Rafael J. Wysocki | b788db7 | 2006-09-25 23:32:54 -0700 | [diff] [blame] | 854 | /** |
| 855 | * memory_bm_next_pfn - find the pfn that corresponds to the next set bit |
| 856 | * in the bitmap @bm. If the pfn cannot be found, BM_END_OF_MAP is |
| 857 | * returned. |
| 858 | * |
| 859 | * It is required to run memory_bm_position_reset() before the first call to |
| 860 | * this function. |
| 861 | */ |
| 862 | |
| 863 | static unsigned long memory_bm_next_pfn(struct memory_bitmap *bm) |
| 864 | { |
Rafael J. Wysocki | b788db7 | 2006-09-25 23:32:54 -0700 | [diff] [blame] | 865 | struct bm_block *bb; |
Rafael J. Wysocki | b788db7 | 2006-09-25 23:32:54 -0700 | [diff] [blame] | 866 | int bit; |
| 867 | |
Rafael J. Wysocki | 846705d | 2008-11-26 18:00:24 -0500 | [diff] [blame] | 868 | bb = bm->cur.block; |
Rafael J. Wysocki | b788db7 | 2006-09-25 23:32:54 -0700 | [diff] [blame] | 869 | do { |
Rafael J. Wysocki | 846705d | 2008-11-26 18:00:24 -0500 | [diff] [blame] | 870 | bit = bm->cur.bit; |
| 871 | bit = find_next_bit(bb->data, bm_block_bits(bb), bit); |
| 872 | if (bit < bm_block_bits(bb)) |
| 873 | goto Return_pfn; |
Rafael J. Wysocki | b788db7 | 2006-09-25 23:32:54 -0700 | [diff] [blame] | 874 | |
Rafael J. Wysocki | 846705d | 2008-11-26 18:00:24 -0500 | [diff] [blame] | 875 | bb = list_entry(bb->hook.next, struct bm_block, hook); |
| 876 | bm->cur.block = bb; |
| 877 | bm->cur.bit = 0; |
| 878 | } while (&bb->hook != &bm->blocks); |
| 879 | |
Rafael J. Wysocki | b788db7 | 2006-09-25 23:32:54 -0700 | [diff] [blame] | 880 | memory_bm_position_reset(bm); |
| 881 | return BM_END_OF_MAP; |
| 882 | |
Rafael J. Wysocki | 59a49335 | 2006-12-06 20:34:44 -0800 | [diff] [blame] | 883 | Return_pfn: |
Akinobu Mita | 0d83304 | 2008-07-23 21:28:38 -0700 | [diff] [blame] | 884 | bm->cur.bit = bit + 1; |
| 885 | return bb->start_pfn + bit; |
Rafael J. Wysocki | b788db7 | 2006-09-25 23:32:54 -0700 | [diff] [blame] | 886 | } |
| 887 | |
| 888 | /** |
Rafael J. Wysocki | 74dfd66 | 2007-05-06 14:50:43 -0700 | [diff] [blame] | 889 | * This structure represents a range of page frames the contents of which |
| 890 | * should not be saved during the suspend. |
| 891 | */ |
| 892 | |
| 893 | struct nosave_region { |
| 894 | struct list_head list; |
| 895 | unsigned long start_pfn; |
| 896 | unsigned long end_pfn; |
| 897 | }; |
| 898 | |
| 899 | static LIST_HEAD(nosave_regions); |
| 900 | |
| 901 | /** |
| 902 | * register_nosave_region - register a range of page frames the contents |
| 903 | * of which should not be saved during the suspend (to be used in the early |
| 904 | * initialization code) |
| 905 | */ |
| 906 | |
| 907 | void __init |
Johannes Berg | 940d67f | 2007-05-08 19:23:49 +1000 | [diff] [blame] | 908 | __register_nosave_region(unsigned long start_pfn, unsigned long end_pfn, |
| 909 | int use_kmalloc) |
Rafael J. Wysocki | 74dfd66 | 2007-05-06 14:50:43 -0700 | [diff] [blame] | 910 | { |
| 911 | struct nosave_region *region; |
| 912 | |
| 913 | if (start_pfn >= end_pfn) |
| 914 | return; |
| 915 | |
| 916 | if (!list_empty(&nosave_regions)) { |
| 917 | /* Try to extend the previous region (they should be sorted) */ |
| 918 | region = list_entry(nosave_regions.prev, |
| 919 | struct nosave_region, list); |
| 920 | if (region->end_pfn == start_pfn) { |
| 921 | region->end_pfn = end_pfn; |
| 922 | goto Report; |
| 923 | } |
| 924 | } |
Johannes Berg | 940d67f | 2007-05-08 19:23:49 +1000 | [diff] [blame] | 925 | if (use_kmalloc) { |
| 926 | /* during init, this shouldn't fail */ |
| 927 | region = kmalloc(sizeof(struct nosave_region), GFP_KERNEL); |
| 928 | BUG_ON(!region); |
| 929 | } else |
| 930 | /* This allocation cannot fail */ |
Santosh Shilimkar | c2f69cd | 2014-01-21 15:50:27 -0800 | [diff] [blame] | 931 | region = memblock_virt_alloc(sizeof(struct nosave_region), 0); |
Rafael J. Wysocki | 74dfd66 | 2007-05-06 14:50:43 -0700 | [diff] [blame] | 932 | region->start_pfn = start_pfn; |
| 933 | region->end_pfn = end_pfn; |
| 934 | list_add_tail(®ion->list, &nosave_regions); |
| 935 | Report: |
Bjorn Helgaas | cd38ca8 | 2013-06-03 18:20:29 +0000 | [diff] [blame] | 936 | printk(KERN_INFO "PM: Registered nosave memory: [mem %#010llx-%#010llx]\n", |
| 937 | (unsigned long long) start_pfn << PAGE_SHIFT, |
| 938 | ((unsigned long long) end_pfn << PAGE_SHIFT) - 1); |
Rafael J. Wysocki | 74dfd66 | 2007-05-06 14:50:43 -0700 | [diff] [blame] | 939 | } |
| 940 | |
| 941 | /* |
| 942 | * Set bits in this map correspond to the page frames the contents of which |
| 943 | * should not be saved during the suspend. |
| 944 | */ |
| 945 | static struct memory_bitmap *forbidden_pages_map; |
| 946 | |
| 947 | /* Set bits in this map correspond to free page frames. */ |
| 948 | static struct memory_bitmap *free_pages_map; |
| 949 | |
| 950 | /* |
| 951 | * Each page frame allocated for creating the image is marked by setting the |
| 952 | * corresponding bits in forbidden_pages_map and free_pages_map simultaneously |
| 953 | */ |
| 954 | |
| 955 | void swsusp_set_page_free(struct page *page) |
| 956 | { |
| 957 | if (free_pages_map) |
| 958 | memory_bm_set_bit(free_pages_map, page_to_pfn(page)); |
| 959 | } |
| 960 | |
| 961 | static int swsusp_page_is_free(struct page *page) |
| 962 | { |
| 963 | return free_pages_map ? |
| 964 | memory_bm_test_bit(free_pages_map, page_to_pfn(page)) : 0; |
| 965 | } |
| 966 | |
| 967 | void swsusp_unset_page_free(struct page *page) |
| 968 | { |
| 969 | if (free_pages_map) |
| 970 | memory_bm_clear_bit(free_pages_map, page_to_pfn(page)); |
| 971 | } |
| 972 | |
| 973 | static void swsusp_set_page_forbidden(struct page *page) |
| 974 | { |
| 975 | if (forbidden_pages_map) |
| 976 | memory_bm_set_bit(forbidden_pages_map, page_to_pfn(page)); |
| 977 | } |
| 978 | |
| 979 | int swsusp_page_is_forbidden(struct page *page) |
| 980 | { |
| 981 | return forbidden_pages_map ? |
| 982 | memory_bm_test_bit(forbidden_pages_map, page_to_pfn(page)) : 0; |
| 983 | } |
| 984 | |
| 985 | static void swsusp_unset_page_forbidden(struct page *page) |
| 986 | { |
| 987 | if (forbidden_pages_map) |
| 988 | memory_bm_clear_bit(forbidden_pages_map, page_to_pfn(page)); |
| 989 | } |
| 990 | |
| 991 | /** |
| 992 | * mark_nosave_pages - set bits corresponding to the page frames the |
| 993 | * contents of which should not be saved in a given bitmap. |
| 994 | */ |
| 995 | |
| 996 | static void mark_nosave_pages(struct memory_bitmap *bm) |
| 997 | { |
| 998 | struct nosave_region *region; |
| 999 | |
| 1000 | if (list_empty(&nosave_regions)) |
| 1001 | return; |
| 1002 | |
| 1003 | list_for_each_entry(region, &nosave_regions, list) { |
| 1004 | unsigned long pfn; |
| 1005 | |
Bjorn Helgaas | 69f1d47 | 2012-02-14 22:20:52 +0100 | [diff] [blame] | 1006 | pr_debug("PM: Marking nosave pages: [mem %#010llx-%#010llx]\n", |
| 1007 | (unsigned long long) region->start_pfn << PAGE_SHIFT, |
| 1008 | ((unsigned long long) region->end_pfn << PAGE_SHIFT) |
| 1009 | - 1); |
Rafael J. Wysocki | 74dfd66 | 2007-05-06 14:50:43 -0700 | [diff] [blame] | 1010 | |
| 1011 | for (pfn = region->start_pfn; pfn < region->end_pfn; pfn++) |
Rafael J. Wysocki | a82f711 | 2008-03-12 00:34:57 +0100 | [diff] [blame] | 1012 | if (pfn_valid(pfn)) { |
| 1013 | /* |
| 1014 | * It is safe to ignore the result of |
| 1015 | * mem_bm_set_bit_check() here, since we won't |
| 1016 | * touch the PFNs for which the error is |
| 1017 | * returned anyway. |
| 1018 | */ |
| 1019 | mem_bm_set_bit_check(bm, pfn); |
| 1020 | } |
Rafael J. Wysocki | 74dfd66 | 2007-05-06 14:50:43 -0700 | [diff] [blame] | 1021 | } |
| 1022 | } |
| 1023 | |
| 1024 | /** |
| 1025 | * create_basic_memory_bitmaps - create bitmaps needed for marking page |
| 1026 | * frames that should not be saved and free page frames. The pointers |
| 1027 | * forbidden_pages_map and free_pages_map are only modified if everything |
| 1028 | * goes well, because we don't want the bits to be used before both bitmaps |
| 1029 | * are set up. |
| 1030 | */ |
| 1031 | |
| 1032 | int create_basic_memory_bitmaps(void) |
| 1033 | { |
| 1034 | struct memory_bitmap *bm1, *bm2; |
| 1035 | int error = 0; |
| 1036 | |
Rafael J. Wysocki | aab1728 | 2013-09-30 19:40:56 +0200 | [diff] [blame] | 1037 | if (forbidden_pages_map && free_pages_map) |
| 1038 | return 0; |
| 1039 | else |
| 1040 | BUG_ON(forbidden_pages_map || free_pages_map); |
Rafael J. Wysocki | 74dfd66 | 2007-05-06 14:50:43 -0700 | [diff] [blame] | 1041 | |
Rafael J. Wysocki | 0709db6 | 2007-05-06 14:50:45 -0700 | [diff] [blame] | 1042 | bm1 = kzalloc(sizeof(struct memory_bitmap), GFP_KERNEL); |
Rafael J. Wysocki | 74dfd66 | 2007-05-06 14:50:43 -0700 | [diff] [blame] | 1043 | if (!bm1) |
| 1044 | return -ENOMEM; |
| 1045 | |
Rafael J. Wysocki | 0709db6 | 2007-05-06 14:50:45 -0700 | [diff] [blame] | 1046 | error = memory_bm_create(bm1, GFP_KERNEL, PG_ANY); |
Rafael J. Wysocki | 74dfd66 | 2007-05-06 14:50:43 -0700 | [diff] [blame] | 1047 | if (error) |
| 1048 | goto Free_first_object; |
| 1049 | |
Rafael J. Wysocki | 0709db6 | 2007-05-06 14:50:45 -0700 | [diff] [blame] | 1050 | bm2 = kzalloc(sizeof(struct memory_bitmap), GFP_KERNEL); |
Rafael J. Wysocki | 74dfd66 | 2007-05-06 14:50:43 -0700 | [diff] [blame] | 1051 | if (!bm2) |
| 1052 | goto Free_first_bitmap; |
| 1053 | |
Rafael J. Wysocki | 0709db6 | 2007-05-06 14:50:45 -0700 | [diff] [blame] | 1054 | error = memory_bm_create(bm2, GFP_KERNEL, PG_ANY); |
Rafael J. Wysocki | 74dfd66 | 2007-05-06 14:50:43 -0700 | [diff] [blame] | 1055 | if (error) |
| 1056 | goto Free_second_object; |
| 1057 | |
| 1058 | forbidden_pages_map = bm1; |
| 1059 | free_pages_map = bm2; |
| 1060 | mark_nosave_pages(forbidden_pages_map); |
| 1061 | |
Rafael J. Wysocki | 2397672 | 2007-12-08 02:09:43 +0100 | [diff] [blame] | 1062 | pr_debug("PM: Basic memory bitmaps created\n"); |
Rafael J. Wysocki | 74dfd66 | 2007-05-06 14:50:43 -0700 | [diff] [blame] | 1063 | |
| 1064 | return 0; |
| 1065 | |
| 1066 | Free_second_object: |
| 1067 | kfree(bm2); |
| 1068 | Free_first_bitmap: |
| 1069 | memory_bm_free(bm1, PG_UNSAFE_CLEAR); |
| 1070 | Free_first_object: |
| 1071 | kfree(bm1); |
| 1072 | return -ENOMEM; |
| 1073 | } |
| 1074 | |
| 1075 | /** |
| 1076 | * free_basic_memory_bitmaps - free memory bitmaps allocated by |
| 1077 | * create_basic_memory_bitmaps(). The auxiliary pointers are necessary |
| 1078 | * so that the bitmaps themselves are not referred to while they are being |
| 1079 | * freed. |
| 1080 | */ |
| 1081 | |
| 1082 | void free_basic_memory_bitmaps(void) |
| 1083 | { |
| 1084 | struct memory_bitmap *bm1, *bm2; |
| 1085 | |
Rafael J. Wysocki | 6a0c7cd | 2013-11-14 23:26:58 +0100 | [diff] [blame] | 1086 | if (WARN_ON(!(forbidden_pages_map && free_pages_map))) |
| 1087 | return; |
Rafael J. Wysocki | 74dfd66 | 2007-05-06 14:50:43 -0700 | [diff] [blame] | 1088 | |
| 1089 | bm1 = forbidden_pages_map; |
| 1090 | bm2 = free_pages_map; |
| 1091 | forbidden_pages_map = NULL; |
| 1092 | free_pages_map = NULL; |
| 1093 | memory_bm_free(bm1, PG_UNSAFE_CLEAR); |
| 1094 | kfree(bm1); |
| 1095 | memory_bm_free(bm2, PG_UNSAFE_CLEAR); |
| 1096 | kfree(bm2); |
| 1097 | |
Rafael J. Wysocki | 2397672 | 2007-12-08 02:09:43 +0100 | [diff] [blame] | 1098 | pr_debug("PM: Basic memory bitmaps freed\n"); |
Rafael J. Wysocki | 74dfd66 | 2007-05-06 14:50:43 -0700 | [diff] [blame] | 1099 | } |
| 1100 | |
| 1101 | /** |
Rafael J. Wysocki | b788db7 | 2006-09-25 23:32:54 -0700 | [diff] [blame] | 1102 | * snapshot_additional_pages - estimate the number of additional pages |
| 1103 | * be needed for setting up the suspend image data structures for given |
| 1104 | * zone (usually the returned value is greater than the exact number) |
| 1105 | */ |
| 1106 | |
| 1107 | unsigned int snapshot_additional_pages(struct zone *zone) |
| 1108 | { |
Joerg Roedel | f469f02 | 2014-07-21 12:26:57 +0200 | [diff] [blame] | 1109 | unsigned int rtree, nodes; |
Rafael J. Wysocki | b788db7 | 2006-09-25 23:32:54 -0700 | [diff] [blame] | 1110 | unsigned int res; |
| 1111 | |
| 1112 | res = DIV_ROUND_UP(zone->spanned_pages, BM_BITS_PER_BLOCK); |
Namhyung Kim | 160cb5a | 2012-01-19 23:23:10 +0100 | [diff] [blame] | 1113 | res += DIV_ROUND_UP(res * sizeof(struct bm_block), |
| 1114 | LINKED_PAGE_DATA_SIZE); |
Joerg Roedel | f469f02 | 2014-07-21 12:26:57 +0200 | [diff] [blame] | 1115 | rtree = nodes = DIV_ROUND_UP(zone->spanned_pages, BM_BITS_PER_BLOCK); |
| 1116 | rtree += DIV_ROUND_UP(rtree * sizeof(struct rtree_node), |
| 1117 | LINKED_PAGE_DATA_SIZE); |
| 1118 | while (nodes > 1) { |
| 1119 | nodes = DIV_ROUND_UP(nodes, BM_ENTRIES_PER_LEVEL); |
| 1120 | rtree += nodes; |
| 1121 | } |
| 1122 | |
| 1123 | return 2 * (res + rtree); |
Rafael J. Wysocki | b788db7 | 2006-09-25 23:32:54 -0700 | [diff] [blame] | 1124 | } |
| 1125 | |
Rafael J. Wysocki | 8357376 | 2006-12-06 20:34:18 -0800 | [diff] [blame] | 1126 | #ifdef CONFIG_HIGHMEM |
| 1127 | /** |
| 1128 | * count_free_highmem_pages - compute the total number of free highmem |
| 1129 | * pages, system-wide. |
| 1130 | */ |
| 1131 | |
| 1132 | static unsigned int count_free_highmem_pages(void) |
| 1133 | { |
| 1134 | struct zone *zone; |
| 1135 | unsigned int cnt = 0; |
| 1136 | |
KOSAKI Motohiro | ee99c71 | 2009-03-31 15:19:31 -0700 | [diff] [blame] | 1137 | for_each_populated_zone(zone) |
| 1138 | if (is_highmem(zone)) |
Christoph Lameter | d23ad42 | 2007-02-10 01:43:02 -0800 | [diff] [blame] | 1139 | cnt += zone_page_state(zone, NR_FREE_PAGES); |
Rafael J. Wysocki | 8357376 | 2006-12-06 20:34:18 -0800 | [diff] [blame] | 1140 | |
| 1141 | return cnt; |
| 1142 | } |
| 1143 | |
| 1144 | /** |
| 1145 | * saveable_highmem_page - Determine whether a highmem page should be |
| 1146 | * included in the suspend image. |
| 1147 | * |
| 1148 | * We should save the page if it isn't Nosave or NosaveFree, or Reserved, |
| 1149 | * and it isn't a part of a free chunk of pages. |
| 1150 | */ |
Rafael J. Wysocki | 846705d | 2008-11-26 18:00:24 -0500 | [diff] [blame] | 1151 | static struct page *saveable_highmem_page(struct zone *zone, unsigned long pfn) |
Rafael J. Wysocki | 8357376 | 2006-12-06 20:34:18 -0800 | [diff] [blame] | 1152 | { |
| 1153 | struct page *page; |
| 1154 | |
| 1155 | if (!pfn_valid(pfn)) |
| 1156 | return NULL; |
| 1157 | |
| 1158 | page = pfn_to_page(pfn); |
Rafael J. Wysocki | 846705d | 2008-11-26 18:00:24 -0500 | [diff] [blame] | 1159 | if (page_zone(page) != zone) |
| 1160 | return NULL; |
Rafael J. Wysocki | 8357376 | 2006-12-06 20:34:18 -0800 | [diff] [blame] | 1161 | |
| 1162 | BUG_ON(!PageHighMem(page)); |
| 1163 | |
Rafael J. Wysocki | 7be9823 | 2007-05-06 14:50:42 -0700 | [diff] [blame] | 1164 | if (swsusp_page_is_forbidden(page) || swsusp_page_is_free(page) || |
| 1165 | PageReserved(page)) |
Rafael J. Wysocki | 8357376 | 2006-12-06 20:34:18 -0800 | [diff] [blame] | 1166 | return NULL; |
| 1167 | |
Stanislaw Gruszka | c6968e7 | 2012-01-10 15:07:31 -0800 | [diff] [blame] | 1168 | if (page_is_guard(page)) |
| 1169 | return NULL; |
| 1170 | |
Rafael J. Wysocki | 8357376 | 2006-12-06 20:34:18 -0800 | [diff] [blame] | 1171 | return page; |
| 1172 | } |
| 1173 | |
| 1174 | /** |
| 1175 | * count_highmem_pages - compute the total number of saveable highmem |
| 1176 | * pages. |
| 1177 | */ |
| 1178 | |
Rafael J. Wysocki | fe41953 | 2009-06-11 23:11:17 +0200 | [diff] [blame] | 1179 | static unsigned int count_highmem_pages(void) |
Rafael J. Wysocki | 8357376 | 2006-12-06 20:34:18 -0800 | [diff] [blame] | 1180 | { |
| 1181 | struct zone *zone; |
| 1182 | unsigned int n = 0; |
| 1183 | |
Gerald Schaefer | 98e73dc | 2009-07-22 00:36:56 +0200 | [diff] [blame] | 1184 | for_each_populated_zone(zone) { |
Rafael J. Wysocki | 8357376 | 2006-12-06 20:34:18 -0800 | [diff] [blame] | 1185 | unsigned long pfn, max_zone_pfn; |
| 1186 | |
| 1187 | if (!is_highmem(zone)) |
| 1188 | continue; |
| 1189 | |
| 1190 | mark_free_pages(zone); |
Xishi Qiu | c33bc31 | 2013-09-11 14:21:44 -0700 | [diff] [blame] | 1191 | max_zone_pfn = zone_end_pfn(zone); |
Rafael J. Wysocki | 8357376 | 2006-12-06 20:34:18 -0800 | [diff] [blame] | 1192 | for (pfn = zone->zone_start_pfn; pfn < max_zone_pfn; pfn++) |
Rafael J. Wysocki | 846705d | 2008-11-26 18:00:24 -0500 | [diff] [blame] | 1193 | if (saveable_highmem_page(zone, pfn)) |
Rafael J. Wysocki | 8357376 | 2006-12-06 20:34:18 -0800 | [diff] [blame] | 1194 | n++; |
| 1195 | } |
| 1196 | return n; |
| 1197 | } |
| 1198 | #else |
Rafael J. Wysocki | 846705d | 2008-11-26 18:00:24 -0500 | [diff] [blame] | 1199 | static inline void *saveable_highmem_page(struct zone *z, unsigned long p) |
| 1200 | { |
| 1201 | return NULL; |
| 1202 | } |
Rafael J. Wysocki | 8357376 | 2006-12-06 20:34:18 -0800 | [diff] [blame] | 1203 | #endif /* CONFIG_HIGHMEM */ |
| 1204 | |
Rafael J. Wysocki | f6143aa | 2006-09-25 23:32:50 -0700 | [diff] [blame] | 1205 | /** |
Rafael J. Wysocki | 8a235ef | 2008-02-20 01:47:44 +0100 | [diff] [blame] | 1206 | * saveable_page - Determine whether a non-highmem page should be included |
| 1207 | * in the suspend image. |
Rafael J. Wysocki | 25761b6 | 2005-10-30 14:59:56 -0800 | [diff] [blame] | 1208 | * |
Rafael J. Wysocki | 8357376 | 2006-12-06 20:34:18 -0800 | [diff] [blame] | 1209 | * We should save the page if it isn't Nosave, and is not in the range |
| 1210 | * of pages statically defined as 'unsaveable', and it isn't a part of |
| 1211 | * a free chunk of pages. |
Rafael J. Wysocki | 25761b6 | 2005-10-30 14:59:56 -0800 | [diff] [blame] | 1212 | */ |
Rafael J. Wysocki | 846705d | 2008-11-26 18:00:24 -0500 | [diff] [blame] | 1213 | static struct page *saveable_page(struct zone *zone, unsigned long pfn) |
Rafael J. Wysocki | 25761b6 | 2005-10-30 14:59:56 -0800 | [diff] [blame] | 1214 | { |
Pavel Machek | de49186 | 2005-10-30 14:59:59 -0800 | [diff] [blame] | 1215 | struct page *page; |
Rafael J. Wysocki | 25761b6 | 2005-10-30 14:59:56 -0800 | [diff] [blame] | 1216 | |
| 1217 | if (!pfn_valid(pfn)) |
Rafael J. Wysocki | ae83c5ee | 2006-09-25 23:32:45 -0700 | [diff] [blame] | 1218 | return NULL; |
Rafael J. Wysocki | 25761b6 | 2005-10-30 14:59:56 -0800 | [diff] [blame] | 1219 | |
| 1220 | page = pfn_to_page(pfn); |
Rafael J. Wysocki | 846705d | 2008-11-26 18:00:24 -0500 | [diff] [blame] | 1221 | if (page_zone(page) != zone) |
| 1222 | return NULL; |
Rafael J. Wysocki | 25761b6 | 2005-10-30 14:59:56 -0800 | [diff] [blame] | 1223 | |
Rafael J. Wysocki | 8357376 | 2006-12-06 20:34:18 -0800 | [diff] [blame] | 1224 | BUG_ON(PageHighMem(page)); |
| 1225 | |
Rafael J. Wysocki | 7be9823 | 2007-05-06 14:50:42 -0700 | [diff] [blame] | 1226 | if (swsusp_page_is_forbidden(page) || swsusp_page_is_free(page)) |
Rafael J. Wysocki | ae83c5ee | 2006-09-25 23:32:45 -0700 | [diff] [blame] | 1227 | return NULL; |
Rafael J. Wysocki | 8357376 | 2006-12-06 20:34:18 -0800 | [diff] [blame] | 1228 | |
Rafael J. Wysocki | 8a235ef | 2008-02-20 01:47:44 +0100 | [diff] [blame] | 1229 | if (PageReserved(page) |
| 1230 | && (!kernel_page_present(page) || pfn_is_nosave(pfn))) |
Rafael J. Wysocki | ae83c5ee | 2006-09-25 23:32:45 -0700 | [diff] [blame] | 1231 | return NULL; |
Rafael J. Wysocki | ae83c5ee | 2006-09-25 23:32:45 -0700 | [diff] [blame] | 1232 | |
Stanislaw Gruszka | c6968e7 | 2012-01-10 15:07:31 -0800 | [diff] [blame] | 1233 | if (page_is_guard(page)) |
| 1234 | return NULL; |
| 1235 | |
Rafael J. Wysocki | ae83c5ee | 2006-09-25 23:32:45 -0700 | [diff] [blame] | 1236 | return page; |
Rafael J. Wysocki | 25761b6 | 2005-10-30 14:59:56 -0800 | [diff] [blame] | 1237 | } |
| 1238 | |
Rafael J. Wysocki | 8357376 | 2006-12-06 20:34:18 -0800 | [diff] [blame] | 1239 | /** |
| 1240 | * count_data_pages - compute the total number of saveable non-highmem |
| 1241 | * pages. |
| 1242 | */ |
| 1243 | |
Rafael J. Wysocki | fe41953 | 2009-06-11 23:11:17 +0200 | [diff] [blame] | 1244 | static unsigned int count_data_pages(void) |
Rafael J. Wysocki | 25761b6 | 2005-10-30 14:59:56 -0800 | [diff] [blame] | 1245 | { |
| 1246 | struct zone *zone; |
Rafael J. Wysocki | ae83c5ee | 2006-09-25 23:32:45 -0700 | [diff] [blame] | 1247 | unsigned long pfn, max_zone_pfn; |
Pavel Machek | dc19d50 | 2005-11-07 00:58:40 -0800 | [diff] [blame] | 1248 | unsigned int n = 0; |
Rafael J. Wysocki | 25761b6 | 2005-10-30 14:59:56 -0800 | [diff] [blame] | 1249 | |
Gerald Schaefer | 98e73dc | 2009-07-22 00:36:56 +0200 | [diff] [blame] | 1250 | for_each_populated_zone(zone) { |
Rafael J. Wysocki | 25761b6 | 2005-10-30 14:59:56 -0800 | [diff] [blame] | 1251 | if (is_highmem(zone)) |
| 1252 | continue; |
Rafael J. Wysocki | 8357376 | 2006-12-06 20:34:18 -0800 | [diff] [blame] | 1253 | |
Rafael J. Wysocki | 25761b6 | 2005-10-30 14:59:56 -0800 | [diff] [blame] | 1254 | mark_free_pages(zone); |
Xishi Qiu | c33bc31 | 2013-09-11 14:21:44 -0700 | [diff] [blame] | 1255 | max_zone_pfn = zone_end_pfn(zone); |
Rafael J. Wysocki | ae83c5ee | 2006-09-25 23:32:45 -0700 | [diff] [blame] | 1256 | for (pfn = zone->zone_start_pfn; pfn < max_zone_pfn; pfn++) |
Rafael J. Wysocki | 846705d | 2008-11-26 18:00:24 -0500 | [diff] [blame] | 1257 | if (saveable_page(zone, pfn)) |
Rafael J. Wysocki | 8357376 | 2006-12-06 20:34:18 -0800 | [diff] [blame] | 1258 | n++; |
Rafael J. Wysocki | 25761b6 | 2005-10-30 14:59:56 -0800 | [diff] [blame] | 1259 | } |
Rafael J. Wysocki | a0f4965 | 2005-10-30 14:59:57 -0800 | [diff] [blame] | 1260 | return n; |
Rafael J. Wysocki | 25761b6 | 2005-10-30 14:59:56 -0800 | [diff] [blame] | 1261 | } |
| 1262 | |
Rafael J. Wysocki | 8357376 | 2006-12-06 20:34:18 -0800 | [diff] [blame] | 1263 | /* This is needed, because copy_page and memcpy are not usable for copying |
| 1264 | * task structs. |
| 1265 | */ |
| 1266 | static inline void do_copy_page(long *dst, long *src) |
Rafael J. Wysocki | f623f0d | 2006-09-25 23:32:49 -0700 | [diff] [blame] | 1267 | { |
| 1268 | int n; |
| 1269 | |
Rafael J. Wysocki | f623f0d | 2006-09-25 23:32:49 -0700 | [diff] [blame] | 1270 | for (n = PAGE_SIZE / sizeof(long); n; n--) |
| 1271 | *dst++ = *src++; |
| 1272 | } |
| 1273 | |
Rafael J. Wysocki | 8a235ef | 2008-02-20 01:47:44 +0100 | [diff] [blame] | 1274 | |
| 1275 | /** |
| 1276 | * safe_copy_page - check if the page we are going to copy is marked as |
| 1277 | * present in the kernel page tables (this always is the case if |
| 1278 | * CONFIG_DEBUG_PAGEALLOC is not set and in that case |
| 1279 | * kernel_page_present() always returns 'true'). |
| 1280 | */ |
| 1281 | static void safe_copy_page(void *dst, struct page *s_page) |
| 1282 | { |
| 1283 | if (kernel_page_present(s_page)) { |
| 1284 | do_copy_page(dst, page_address(s_page)); |
| 1285 | } else { |
| 1286 | kernel_map_pages(s_page, 1, 1); |
| 1287 | do_copy_page(dst, page_address(s_page)); |
| 1288 | kernel_map_pages(s_page, 1, 0); |
| 1289 | } |
| 1290 | } |
| 1291 | |
| 1292 | |
Rafael J. Wysocki | 8357376 | 2006-12-06 20:34:18 -0800 | [diff] [blame] | 1293 | #ifdef CONFIG_HIGHMEM |
| 1294 | static inline struct page * |
| 1295 | page_is_saveable(struct zone *zone, unsigned long pfn) |
| 1296 | { |
| 1297 | return is_highmem(zone) ? |
Rafael J. Wysocki | 846705d | 2008-11-26 18:00:24 -0500 | [diff] [blame] | 1298 | saveable_highmem_page(zone, pfn) : saveable_page(zone, pfn); |
Rafael J. Wysocki | 8357376 | 2006-12-06 20:34:18 -0800 | [diff] [blame] | 1299 | } |
| 1300 | |
Rafael J. Wysocki | 8a235ef | 2008-02-20 01:47:44 +0100 | [diff] [blame] | 1301 | static void copy_data_page(unsigned long dst_pfn, unsigned long src_pfn) |
Rafael J. Wysocki | 8357376 | 2006-12-06 20:34:18 -0800 | [diff] [blame] | 1302 | { |
| 1303 | struct page *s_page, *d_page; |
| 1304 | void *src, *dst; |
| 1305 | |
| 1306 | s_page = pfn_to_page(src_pfn); |
| 1307 | d_page = pfn_to_page(dst_pfn); |
| 1308 | if (PageHighMem(s_page)) { |
Cong Wang | 0de9a1e | 2011-11-25 23:14:38 +0800 | [diff] [blame] | 1309 | src = kmap_atomic(s_page); |
| 1310 | dst = kmap_atomic(d_page); |
Rafael J. Wysocki | 8357376 | 2006-12-06 20:34:18 -0800 | [diff] [blame] | 1311 | do_copy_page(dst, src); |
Cong Wang | 0de9a1e | 2011-11-25 23:14:38 +0800 | [diff] [blame] | 1312 | kunmap_atomic(dst); |
| 1313 | kunmap_atomic(src); |
Rafael J. Wysocki | 8357376 | 2006-12-06 20:34:18 -0800 | [diff] [blame] | 1314 | } else { |
Rafael J. Wysocki | 8357376 | 2006-12-06 20:34:18 -0800 | [diff] [blame] | 1315 | if (PageHighMem(d_page)) { |
| 1316 | /* Page pointed to by src may contain some kernel |
| 1317 | * data modified by kmap_atomic() |
| 1318 | */ |
Rafael J. Wysocki | 8a235ef | 2008-02-20 01:47:44 +0100 | [diff] [blame] | 1319 | safe_copy_page(buffer, s_page); |
Cong Wang | 0de9a1e | 2011-11-25 23:14:38 +0800 | [diff] [blame] | 1320 | dst = kmap_atomic(d_page); |
Jan Beulich | 3ecb01d | 2010-10-26 14:22:27 -0700 | [diff] [blame] | 1321 | copy_page(dst, buffer); |
Cong Wang | 0de9a1e | 2011-11-25 23:14:38 +0800 | [diff] [blame] | 1322 | kunmap_atomic(dst); |
Rafael J. Wysocki | 8357376 | 2006-12-06 20:34:18 -0800 | [diff] [blame] | 1323 | } else { |
Rafael J. Wysocki | 8a235ef | 2008-02-20 01:47:44 +0100 | [diff] [blame] | 1324 | safe_copy_page(page_address(d_page), s_page); |
Rafael J. Wysocki | 8357376 | 2006-12-06 20:34:18 -0800 | [diff] [blame] | 1325 | } |
| 1326 | } |
| 1327 | } |
| 1328 | #else |
Rafael J. Wysocki | 846705d | 2008-11-26 18:00:24 -0500 | [diff] [blame] | 1329 | #define page_is_saveable(zone, pfn) saveable_page(zone, pfn) |
Rafael J. Wysocki | 8357376 | 2006-12-06 20:34:18 -0800 | [diff] [blame] | 1330 | |
Rafael J. Wysocki | 8a235ef | 2008-02-20 01:47:44 +0100 | [diff] [blame] | 1331 | static inline void copy_data_page(unsigned long dst_pfn, unsigned long src_pfn) |
Rafael J. Wysocki | 8357376 | 2006-12-06 20:34:18 -0800 | [diff] [blame] | 1332 | { |
Rafael J. Wysocki | 8a235ef | 2008-02-20 01:47:44 +0100 | [diff] [blame] | 1333 | safe_copy_page(page_address(pfn_to_page(dst_pfn)), |
| 1334 | pfn_to_page(src_pfn)); |
Rafael J. Wysocki | 8357376 | 2006-12-06 20:34:18 -0800 | [diff] [blame] | 1335 | } |
| 1336 | #endif /* CONFIG_HIGHMEM */ |
| 1337 | |
Rafael J. Wysocki | b788db7 | 2006-09-25 23:32:54 -0700 | [diff] [blame] | 1338 | static void |
| 1339 | copy_data_pages(struct memory_bitmap *copy_bm, struct memory_bitmap *orig_bm) |
Rafael J. Wysocki | 25761b6 | 2005-10-30 14:59:56 -0800 | [diff] [blame] | 1340 | { |
| 1341 | struct zone *zone; |
Rafael J. Wysocki | b788db7 | 2006-09-25 23:32:54 -0700 | [diff] [blame] | 1342 | unsigned long pfn; |
Rafael J. Wysocki | 25761b6 | 2005-10-30 14:59:56 -0800 | [diff] [blame] | 1343 | |
Gerald Schaefer | 98e73dc | 2009-07-22 00:36:56 +0200 | [diff] [blame] | 1344 | for_each_populated_zone(zone) { |
Rafael J. Wysocki | b788db7 | 2006-09-25 23:32:54 -0700 | [diff] [blame] | 1345 | unsigned long max_zone_pfn; |
| 1346 | |
Rafael J. Wysocki | 25761b6 | 2005-10-30 14:59:56 -0800 | [diff] [blame] | 1347 | mark_free_pages(zone); |
Xishi Qiu | c33bc31 | 2013-09-11 14:21:44 -0700 | [diff] [blame] | 1348 | max_zone_pfn = zone_end_pfn(zone); |
Rafael J. Wysocki | b788db7 | 2006-09-25 23:32:54 -0700 | [diff] [blame] | 1349 | for (pfn = zone->zone_start_pfn; pfn < max_zone_pfn; pfn++) |
Rafael J. Wysocki | 8357376 | 2006-12-06 20:34:18 -0800 | [diff] [blame] | 1350 | if (page_is_saveable(zone, pfn)) |
Rafael J. Wysocki | b788db7 | 2006-09-25 23:32:54 -0700 | [diff] [blame] | 1351 | memory_bm_set_bit(orig_bm, pfn); |
Rafael J. Wysocki | 25761b6 | 2005-10-30 14:59:56 -0800 | [diff] [blame] | 1352 | } |
Rafael J. Wysocki | b788db7 | 2006-09-25 23:32:54 -0700 | [diff] [blame] | 1353 | memory_bm_position_reset(orig_bm); |
| 1354 | memory_bm_position_reset(copy_bm); |
Fengguang Wu | df7c487 | 2007-10-20 02:26:04 +0200 | [diff] [blame] | 1355 | for(;;) { |
Rafael J. Wysocki | b788db7 | 2006-09-25 23:32:54 -0700 | [diff] [blame] | 1356 | pfn = memory_bm_next_pfn(orig_bm); |
Fengguang Wu | df7c487 | 2007-10-20 02:26:04 +0200 | [diff] [blame] | 1357 | if (unlikely(pfn == BM_END_OF_MAP)) |
| 1358 | break; |
| 1359 | copy_data_page(memory_bm_next_pfn(copy_bm), pfn); |
| 1360 | } |
Rafael J. Wysocki | 25761b6 | 2005-10-30 14:59:56 -0800 | [diff] [blame] | 1361 | } |
| 1362 | |
Rafael J. Wysocki | 8357376 | 2006-12-06 20:34:18 -0800 | [diff] [blame] | 1363 | /* Total number of image pages */ |
| 1364 | static unsigned int nr_copy_pages; |
| 1365 | /* Number of pages needed for saving the original pfns of the image pages */ |
| 1366 | static unsigned int nr_meta_pages; |
Rafael J. Wysocki | 64a473c | 2009-07-08 13:24:05 +0200 | [diff] [blame] | 1367 | /* |
| 1368 | * Numbers of normal and highmem page frames allocated for hibernation image |
| 1369 | * before suspending devices. |
| 1370 | */ |
| 1371 | unsigned int alloc_normal, alloc_highmem; |
| 1372 | /* |
| 1373 | * Memory bitmap used for marking saveable pages (during hibernation) or |
| 1374 | * hibernation image pages (during restore) |
| 1375 | */ |
| 1376 | static struct memory_bitmap orig_bm; |
| 1377 | /* |
| 1378 | * Memory bitmap used during hibernation for marking allocated page frames that |
| 1379 | * will contain copies of saveable pages. During restore it is initially used |
| 1380 | * for marking hibernation image pages, but then the set bits from it are |
| 1381 | * duplicated in @orig_bm and it is released. On highmem systems it is next |
| 1382 | * used for marking "safe" highmem pages, but it has to be reinitialized for |
| 1383 | * this purpose. |
| 1384 | */ |
| 1385 | static struct memory_bitmap copy_bm; |
Rafael J. Wysocki | 8357376 | 2006-12-06 20:34:18 -0800 | [diff] [blame] | 1386 | |
Rafael J. Wysocki | 25761b6 | 2005-10-30 14:59:56 -0800 | [diff] [blame] | 1387 | /** |
Rafael J. Wysocki | 940864d | 2006-09-25 23:32:55 -0700 | [diff] [blame] | 1388 | * swsusp_free - free pages allocated for the suspend. |
Rafael J. Wysocki | cd560bb | 2006-09-25 23:32:50 -0700 | [diff] [blame] | 1389 | * |
Rafael J. Wysocki | 940864d | 2006-09-25 23:32:55 -0700 | [diff] [blame] | 1390 | * Suspend pages are alocated before the atomic copy is made, so we |
| 1391 | * need to release them after the resume. |
Rafael J. Wysocki | 25761b6 | 2005-10-30 14:59:56 -0800 | [diff] [blame] | 1392 | */ |
| 1393 | |
| 1394 | void swsusp_free(void) |
| 1395 | { |
| 1396 | struct zone *zone; |
Rafael J. Wysocki | ae83c5ee | 2006-09-25 23:32:45 -0700 | [diff] [blame] | 1397 | unsigned long pfn, max_zone_pfn; |
Rafael J. Wysocki | 25761b6 | 2005-10-30 14:59:56 -0800 | [diff] [blame] | 1398 | |
Gerald Schaefer | 98e73dc | 2009-07-22 00:36:56 +0200 | [diff] [blame] | 1399 | for_each_populated_zone(zone) { |
Xishi Qiu | c33bc31 | 2013-09-11 14:21:44 -0700 | [diff] [blame] | 1400 | max_zone_pfn = zone_end_pfn(zone); |
Rafael J. Wysocki | ae83c5ee | 2006-09-25 23:32:45 -0700 | [diff] [blame] | 1401 | for (pfn = zone->zone_start_pfn; pfn < max_zone_pfn; pfn++) |
| 1402 | if (pfn_valid(pfn)) { |
| 1403 | struct page *page = pfn_to_page(pfn); |
| 1404 | |
Rafael J. Wysocki | 7be9823 | 2007-05-06 14:50:42 -0700 | [diff] [blame] | 1405 | if (swsusp_page_is_forbidden(page) && |
| 1406 | swsusp_page_is_free(page)) { |
| 1407 | swsusp_unset_page_forbidden(page); |
| 1408 | swsusp_unset_page_free(page); |
Rafael J. Wysocki | 8357376 | 2006-12-06 20:34:18 -0800 | [diff] [blame] | 1409 | __free_page(page); |
Rafael J. Wysocki | 25761b6 | 2005-10-30 14:59:56 -0800 | [diff] [blame] | 1410 | } |
| 1411 | } |
| 1412 | } |
Rafael J. Wysocki | f577eb3 | 2006-03-23 02:59:59 -0800 | [diff] [blame] | 1413 | nr_copy_pages = 0; |
| 1414 | nr_meta_pages = 0; |
Rafael J. Wysocki | 75534b5 | 2006-09-25 23:32:52 -0700 | [diff] [blame] | 1415 | restore_pblist = NULL; |
Rafael J. Wysocki | 6e1819d | 2006-03-23 03:00:03 -0800 | [diff] [blame] | 1416 | buffer = NULL; |
Rafael J. Wysocki | 64a473c | 2009-07-08 13:24:05 +0200 | [diff] [blame] | 1417 | alloc_normal = 0; |
| 1418 | alloc_highmem = 0; |
Rafael J. Wysocki | 25761b6 | 2005-10-30 14:59:56 -0800 | [diff] [blame] | 1419 | } |
| 1420 | |
Rafael J. Wysocki | 4bb3343 | 2009-07-08 13:23:51 +0200 | [diff] [blame] | 1421 | /* Helper functions used for the shrinking of memory. */ |
Rafael J. Wysocki | fe41953 | 2009-06-11 23:11:17 +0200 | [diff] [blame] | 1422 | |
Rafael J. Wysocki | 4bb3343 | 2009-07-08 13:23:51 +0200 | [diff] [blame] | 1423 | #define GFP_IMAGE (GFP_KERNEL | __GFP_NOWARN) |
| 1424 | |
| 1425 | /** |
| 1426 | * preallocate_image_pages - Allocate a number of pages for hibernation image |
| 1427 | * @nr_pages: Number of page frames to allocate. |
| 1428 | * @mask: GFP flags to use for the allocation. |
| 1429 | * |
| 1430 | * Return value: Number of page frames actually allocated |
| 1431 | */ |
| 1432 | static unsigned long preallocate_image_pages(unsigned long nr_pages, gfp_t mask) |
Rafael J. Wysocki | fe41953 | 2009-06-11 23:11:17 +0200 | [diff] [blame] | 1433 | { |
Rafael J. Wysocki | 4bb3343 | 2009-07-08 13:23:51 +0200 | [diff] [blame] | 1434 | unsigned long nr_alloc = 0; |
| 1435 | |
| 1436 | while (nr_pages > 0) { |
Rafael J. Wysocki | 64a473c | 2009-07-08 13:24:05 +0200 | [diff] [blame] | 1437 | struct page *page; |
| 1438 | |
| 1439 | page = alloc_image_page(mask); |
| 1440 | if (!page) |
Rafael J. Wysocki | 4bb3343 | 2009-07-08 13:23:51 +0200 | [diff] [blame] | 1441 | break; |
Rafael J. Wysocki | 64a473c | 2009-07-08 13:24:05 +0200 | [diff] [blame] | 1442 | memory_bm_set_bit(©_bm, page_to_pfn(page)); |
| 1443 | if (PageHighMem(page)) |
| 1444 | alloc_highmem++; |
| 1445 | else |
| 1446 | alloc_normal++; |
Rafael J. Wysocki | 4bb3343 | 2009-07-08 13:23:51 +0200 | [diff] [blame] | 1447 | nr_pages--; |
| 1448 | nr_alloc++; |
| 1449 | } |
| 1450 | |
| 1451 | return nr_alloc; |
Rafael J. Wysocki | fe41953 | 2009-06-11 23:11:17 +0200 | [diff] [blame] | 1452 | } |
| 1453 | |
Rafael J. Wysocki | 6715045 | 2010-09-11 20:58:27 +0200 | [diff] [blame] | 1454 | static unsigned long preallocate_image_memory(unsigned long nr_pages, |
| 1455 | unsigned long avail_normal) |
Rafael J. Wysocki | 4bb3343 | 2009-07-08 13:23:51 +0200 | [diff] [blame] | 1456 | { |
Rafael J. Wysocki | 6715045 | 2010-09-11 20:58:27 +0200 | [diff] [blame] | 1457 | unsigned long alloc; |
| 1458 | |
| 1459 | if (avail_normal <= alloc_normal) |
| 1460 | return 0; |
| 1461 | |
| 1462 | alloc = avail_normal - alloc_normal; |
| 1463 | if (nr_pages < alloc) |
| 1464 | alloc = nr_pages; |
| 1465 | |
| 1466 | return preallocate_image_pages(alloc, GFP_IMAGE); |
Rafael J. Wysocki | 4bb3343 | 2009-07-08 13:23:51 +0200 | [diff] [blame] | 1467 | } |
| 1468 | |
| 1469 | #ifdef CONFIG_HIGHMEM |
| 1470 | static unsigned long preallocate_image_highmem(unsigned long nr_pages) |
| 1471 | { |
| 1472 | return preallocate_image_pages(nr_pages, GFP_IMAGE | __GFP_HIGHMEM); |
| 1473 | } |
| 1474 | |
| 1475 | /** |
| 1476 | * __fraction - Compute (an approximation of) x * (multiplier / base) |
| 1477 | */ |
| 1478 | static unsigned long __fraction(u64 x, u64 multiplier, u64 base) |
| 1479 | { |
| 1480 | x *= multiplier; |
| 1481 | do_div(x, base); |
| 1482 | return (unsigned long)x; |
| 1483 | } |
| 1484 | |
| 1485 | static unsigned long preallocate_highmem_fraction(unsigned long nr_pages, |
| 1486 | unsigned long highmem, |
| 1487 | unsigned long total) |
| 1488 | { |
| 1489 | unsigned long alloc = __fraction(nr_pages, highmem, total); |
| 1490 | |
| 1491 | return preallocate_image_pages(alloc, GFP_IMAGE | __GFP_HIGHMEM); |
| 1492 | } |
| 1493 | #else /* CONFIG_HIGHMEM */ |
| 1494 | static inline unsigned long preallocate_image_highmem(unsigned long nr_pages) |
| 1495 | { |
| 1496 | return 0; |
| 1497 | } |
| 1498 | |
| 1499 | static inline unsigned long preallocate_highmem_fraction(unsigned long nr_pages, |
| 1500 | unsigned long highmem, |
| 1501 | unsigned long total) |
| 1502 | { |
| 1503 | return 0; |
| 1504 | } |
| 1505 | #endif /* CONFIG_HIGHMEM */ |
| 1506 | |
| 1507 | /** |
Rafael J. Wysocki | 64a473c | 2009-07-08 13:24:05 +0200 | [diff] [blame] | 1508 | * free_unnecessary_pages - Release preallocated pages not needed for the image |
| 1509 | */ |
| 1510 | static void free_unnecessary_pages(void) |
| 1511 | { |
Rafael J. Wysocki | 6715045 | 2010-09-11 20:58:27 +0200 | [diff] [blame] | 1512 | unsigned long save, to_free_normal, to_free_highmem; |
Rafael J. Wysocki | 64a473c | 2009-07-08 13:24:05 +0200 | [diff] [blame] | 1513 | |
Rafael J. Wysocki | 6715045 | 2010-09-11 20:58:27 +0200 | [diff] [blame] | 1514 | save = count_data_pages(); |
| 1515 | if (alloc_normal >= save) { |
| 1516 | to_free_normal = alloc_normal - save; |
| 1517 | save = 0; |
| 1518 | } else { |
| 1519 | to_free_normal = 0; |
| 1520 | save -= alloc_normal; |
| 1521 | } |
| 1522 | save += count_highmem_pages(); |
| 1523 | if (alloc_highmem >= save) { |
| 1524 | to_free_highmem = alloc_highmem - save; |
Rafael J. Wysocki | 64a473c | 2009-07-08 13:24:05 +0200 | [diff] [blame] | 1525 | } else { |
| 1526 | to_free_highmem = 0; |
Rafael J. Wysocki | 4d4cf23 | 2011-07-06 20:15:23 +0200 | [diff] [blame] | 1527 | save -= alloc_highmem; |
| 1528 | if (to_free_normal > save) |
| 1529 | to_free_normal -= save; |
| 1530 | else |
| 1531 | to_free_normal = 0; |
Rafael J. Wysocki | 64a473c | 2009-07-08 13:24:05 +0200 | [diff] [blame] | 1532 | } |
| 1533 | |
| 1534 | memory_bm_position_reset(©_bm); |
| 1535 | |
Rafael J. Wysocki | a9c9b44 | 2010-02-25 22:32:37 +0100 | [diff] [blame] | 1536 | while (to_free_normal > 0 || to_free_highmem > 0) { |
Rafael J. Wysocki | 64a473c | 2009-07-08 13:24:05 +0200 | [diff] [blame] | 1537 | unsigned long pfn = memory_bm_next_pfn(©_bm); |
| 1538 | struct page *page = pfn_to_page(pfn); |
| 1539 | |
| 1540 | if (PageHighMem(page)) { |
| 1541 | if (!to_free_highmem) |
| 1542 | continue; |
| 1543 | to_free_highmem--; |
| 1544 | alloc_highmem--; |
| 1545 | } else { |
| 1546 | if (!to_free_normal) |
| 1547 | continue; |
| 1548 | to_free_normal--; |
| 1549 | alloc_normal--; |
| 1550 | } |
| 1551 | memory_bm_clear_bit(©_bm, pfn); |
| 1552 | swsusp_unset_page_forbidden(page); |
| 1553 | swsusp_unset_page_free(page); |
| 1554 | __free_page(page); |
| 1555 | } |
| 1556 | } |
| 1557 | |
| 1558 | /** |
Rafael J. Wysocki | ef4aede | 2009-07-08 13:24:12 +0200 | [diff] [blame] | 1559 | * minimum_image_size - Estimate the minimum acceptable size of an image |
| 1560 | * @saveable: Number of saveable pages in the system. |
| 1561 | * |
| 1562 | * We want to avoid attempting to free too much memory too hard, so estimate the |
| 1563 | * minimum acceptable size of a hibernation image to use as the lower limit for |
| 1564 | * preallocating memory. |
| 1565 | * |
| 1566 | * We assume that the minimum image size should be proportional to |
| 1567 | * |
| 1568 | * [number of saveable pages] - [number of pages that can be freed in theory] |
| 1569 | * |
| 1570 | * where the second term is the sum of (1) reclaimable slab pages, (2) active |
Geert Uytterhoeven | 4d43482 | 2014-03-11 11:23:41 +0100 | [diff] [blame] | 1571 | * and (3) inactive anonymous pages, (4) active and (5) inactive file pages, |
Rafael J. Wysocki | ef4aede | 2009-07-08 13:24:12 +0200 | [diff] [blame] | 1572 | * minus mapped file pages. |
| 1573 | */ |
| 1574 | static unsigned long minimum_image_size(unsigned long saveable) |
| 1575 | { |
| 1576 | unsigned long size; |
| 1577 | |
| 1578 | size = global_page_state(NR_SLAB_RECLAIMABLE) |
| 1579 | + global_page_state(NR_ACTIVE_ANON) |
| 1580 | + global_page_state(NR_INACTIVE_ANON) |
| 1581 | + global_page_state(NR_ACTIVE_FILE) |
| 1582 | + global_page_state(NR_INACTIVE_FILE) |
| 1583 | - global_page_state(NR_FILE_MAPPED); |
| 1584 | |
| 1585 | return saveable <= size ? 0 : saveable - size; |
| 1586 | } |
| 1587 | |
| 1588 | /** |
Rafael J. Wysocki | 64a473c | 2009-07-08 13:24:05 +0200 | [diff] [blame] | 1589 | * hibernate_preallocate_memory - Preallocate memory for hibernation image |
Rafael J. Wysocki | 4bb3343 | 2009-07-08 13:23:51 +0200 | [diff] [blame] | 1590 | * |
| 1591 | * To create a hibernation image it is necessary to make a copy of every page |
| 1592 | * frame in use. We also need a number of page frames to be free during |
| 1593 | * hibernation for allocations made while saving the image and for device |
| 1594 | * drivers, in case they need to allocate memory from their hibernation |
Rafael J. Wysocki | ddeb648 | 2011-05-15 11:38:48 +0200 | [diff] [blame] | 1595 | * callbacks (these two numbers are given by PAGES_FOR_IO (which is a rough |
| 1596 | * estimate) and reserverd_size divided by PAGE_SIZE (which is tunable through |
| 1597 | * /sys/power/reserved_size, respectively). To make this happen, we compute the |
| 1598 | * total number of available page frames and allocate at least |
Rafael J. Wysocki | 4bb3343 | 2009-07-08 13:23:51 +0200 | [diff] [blame] | 1599 | * |
Rafael J. Wysocki | ddeb648 | 2011-05-15 11:38:48 +0200 | [diff] [blame] | 1600 | * ([page frames total] + PAGES_FOR_IO + [metadata pages]) / 2 |
| 1601 | * + 2 * DIV_ROUND_UP(reserved_size, PAGE_SIZE) |
Rafael J. Wysocki | 4bb3343 | 2009-07-08 13:23:51 +0200 | [diff] [blame] | 1602 | * |
| 1603 | * of them, which corresponds to the maximum size of a hibernation image. |
| 1604 | * |
| 1605 | * If image_size is set below the number following from the above formula, |
| 1606 | * the preallocation of memory is continued until the total number of saveable |
Rafael J. Wysocki | ef4aede | 2009-07-08 13:24:12 +0200 | [diff] [blame] | 1607 | * pages in the system is below the requested image size or the minimum |
| 1608 | * acceptable image size returned by minimum_image_size(), whichever is greater. |
Rafael J. Wysocki | 4bb3343 | 2009-07-08 13:23:51 +0200 | [diff] [blame] | 1609 | */ |
Rafael J. Wysocki | 64a473c | 2009-07-08 13:24:05 +0200 | [diff] [blame] | 1610 | int hibernate_preallocate_memory(void) |
Rafael J. Wysocki | fe41953 | 2009-06-11 23:11:17 +0200 | [diff] [blame] | 1611 | { |
Rafael J. Wysocki | fe41953 | 2009-06-11 23:11:17 +0200 | [diff] [blame] | 1612 | struct zone *zone; |
Rafael J. Wysocki | 4bb3343 | 2009-07-08 13:23:51 +0200 | [diff] [blame] | 1613 | unsigned long saveable, size, max_size, count, highmem, pages = 0; |
Rafael J. Wysocki | 6715045 | 2010-09-11 20:58:27 +0200 | [diff] [blame] | 1614 | unsigned long alloc, save_highmem, pages_highmem, avail_normal; |
Rafael J. Wysocki | fe41953 | 2009-06-11 23:11:17 +0200 | [diff] [blame] | 1615 | struct timeval start, stop; |
Rafael J. Wysocki | 64a473c | 2009-07-08 13:24:05 +0200 | [diff] [blame] | 1616 | int error; |
Rafael J. Wysocki | fe41953 | 2009-06-11 23:11:17 +0200 | [diff] [blame] | 1617 | |
Rafael J. Wysocki | 64a473c | 2009-07-08 13:24:05 +0200 | [diff] [blame] | 1618 | printk(KERN_INFO "PM: Preallocating image memory... "); |
Rafael J. Wysocki | fe41953 | 2009-06-11 23:11:17 +0200 | [diff] [blame] | 1619 | do_gettimeofday(&start); |
Rafael J. Wysocki | fe41953 | 2009-06-11 23:11:17 +0200 | [diff] [blame] | 1620 | |
Rafael J. Wysocki | 64a473c | 2009-07-08 13:24:05 +0200 | [diff] [blame] | 1621 | error = memory_bm_create(&orig_bm, GFP_IMAGE, PG_ANY); |
| 1622 | if (error) |
| 1623 | goto err_out; |
| 1624 | |
| 1625 | error = memory_bm_create(©_bm, GFP_IMAGE, PG_ANY); |
| 1626 | if (error) |
| 1627 | goto err_out; |
| 1628 | |
| 1629 | alloc_normal = 0; |
| 1630 | alloc_highmem = 0; |
| 1631 | |
Rafael J. Wysocki | 4bb3343 | 2009-07-08 13:23:51 +0200 | [diff] [blame] | 1632 | /* Count the number of saveable data pages. */ |
Rafael J. Wysocki | 64a473c | 2009-07-08 13:24:05 +0200 | [diff] [blame] | 1633 | save_highmem = count_highmem_pages(); |
Rafael J. Wysocki | 4bb3343 | 2009-07-08 13:23:51 +0200 | [diff] [blame] | 1634 | saveable = count_data_pages(); |
Rafael J. Wysocki | fe41953 | 2009-06-11 23:11:17 +0200 | [diff] [blame] | 1635 | |
Rafael J. Wysocki | 4bb3343 | 2009-07-08 13:23:51 +0200 | [diff] [blame] | 1636 | /* |
| 1637 | * Compute the total number of page frames we can use (count) and the |
| 1638 | * number of pages needed for image metadata (size). |
| 1639 | */ |
| 1640 | count = saveable; |
Rafael J. Wysocki | 64a473c | 2009-07-08 13:24:05 +0200 | [diff] [blame] | 1641 | saveable += save_highmem; |
| 1642 | highmem = save_highmem; |
Rafael J. Wysocki | 4bb3343 | 2009-07-08 13:23:51 +0200 | [diff] [blame] | 1643 | size = 0; |
| 1644 | for_each_populated_zone(zone) { |
| 1645 | size += snapshot_additional_pages(zone); |
| 1646 | if (is_highmem(zone)) |
| 1647 | highmem += zone_page_state(zone, NR_FREE_PAGES); |
| 1648 | else |
| 1649 | count += zone_page_state(zone, NR_FREE_PAGES); |
| 1650 | } |
Rafael J. Wysocki | 6715045 | 2010-09-11 20:58:27 +0200 | [diff] [blame] | 1651 | avail_normal = count; |
Rafael J. Wysocki | 4bb3343 | 2009-07-08 13:23:51 +0200 | [diff] [blame] | 1652 | count += highmem; |
| 1653 | count -= totalreserve_pages; |
Rafael J. Wysocki | fe41953 | 2009-06-11 23:11:17 +0200 | [diff] [blame] | 1654 | |
Martin Schwidefsky | 85055dd | 2011-08-17 20:42:24 +0200 | [diff] [blame] | 1655 | /* Add number of pages required for page keys (s390 only). */ |
| 1656 | size += page_key_additional_pages(saveable); |
| 1657 | |
Rafael J. Wysocki | 4bb3343 | 2009-07-08 13:23:51 +0200 | [diff] [blame] | 1658 | /* Compute the maximum number of saveable pages to leave in memory. */ |
Rafael J. Wysocki | ddeb648 | 2011-05-15 11:38:48 +0200 | [diff] [blame] | 1659 | max_size = (count - (size + PAGES_FOR_IO)) / 2 |
| 1660 | - 2 * DIV_ROUND_UP(reserved_size, PAGE_SIZE); |
Rafael J. Wysocki | 266f1a2 | 2010-09-20 19:44:38 +0200 | [diff] [blame] | 1661 | /* Compute the desired number of image pages specified by image_size. */ |
Rafael J. Wysocki | 4bb3343 | 2009-07-08 13:23:51 +0200 | [diff] [blame] | 1662 | size = DIV_ROUND_UP(image_size, PAGE_SIZE); |
| 1663 | if (size > max_size) |
| 1664 | size = max_size; |
| 1665 | /* |
Rafael J. Wysocki | 266f1a2 | 2010-09-20 19:44:38 +0200 | [diff] [blame] | 1666 | * If the desired number of image pages is at least as large as the |
| 1667 | * current number of saveable pages in memory, allocate page frames for |
| 1668 | * the image and we're done. |
Rafael J. Wysocki | 4bb3343 | 2009-07-08 13:23:51 +0200 | [diff] [blame] | 1669 | */ |
Rafael J. Wysocki | 64a473c | 2009-07-08 13:24:05 +0200 | [diff] [blame] | 1670 | if (size >= saveable) { |
| 1671 | pages = preallocate_image_highmem(save_highmem); |
Rafael J. Wysocki | 6715045 | 2010-09-11 20:58:27 +0200 | [diff] [blame] | 1672 | pages += preallocate_image_memory(saveable - pages, avail_normal); |
Rafael J. Wysocki | 4bb3343 | 2009-07-08 13:23:51 +0200 | [diff] [blame] | 1673 | goto out; |
Rafael J. Wysocki | 64a473c | 2009-07-08 13:24:05 +0200 | [diff] [blame] | 1674 | } |
Rafael J. Wysocki | 4bb3343 | 2009-07-08 13:23:51 +0200 | [diff] [blame] | 1675 | |
Rafael J. Wysocki | ef4aede | 2009-07-08 13:24:12 +0200 | [diff] [blame] | 1676 | /* Estimate the minimum size of the image. */ |
| 1677 | pages = minimum_image_size(saveable); |
Rafael J. Wysocki | 6715045 | 2010-09-11 20:58:27 +0200 | [diff] [blame] | 1678 | /* |
| 1679 | * To avoid excessive pressure on the normal zone, leave room in it to |
| 1680 | * accommodate an image of the minimum size (unless it's already too |
| 1681 | * small, in which case don't preallocate pages from it at all). |
| 1682 | */ |
| 1683 | if (avail_normal > pages) |
| 1684 | avail_normal -= pages; |
| 1685 | else |
| 1686 | avail_normal = 0; |
Rafael J. Wysocki | ef4aede | 2009-07-08 13:24:12 +0200 | [diff] [blame] | 1687 | if (size < pages) |
| 1688 | size = min_t(unsigned long, pages, max_size); |
| 1689 | |
Rafael J. Wysocki | 4bb3343 | 2009-07-08 13:23:51 +0200 | [diff] [blame] | 1690 | /* |
| 1691 | * Let the memory management subsystem know that we're going to need a |
| 1692 | * large number of page frames to allocate and make it free some memory. |
| 1693 | * NOTE: If this is not done, performance will be hurt badly in some |
| 1694 | * test cases. |
| 1695 | */ |
| 1696 | shrink_all_memory(saveable - size); |
| 1697 | |
| 1698 | /* |
| 1699 | * The number of saveable pages in memory was too high, so apply some |
| 1700 | * pressure to decrease it. First, make room for the largest possible |
| 1701 | * image and fail if that doesn't work. Next, try to decrease the size |
Rafael J. Wysocki | ef4aede | 2009-07-08 13:24:12 +0200 | [diff] [blame] | 1702 | * of the image as much as indicated by 'size' using allocations from |
| 1703 | * highmem and non-highmem zones separately. |
Rafael J. Wysocki | 4bb3343 | 2009-07-08 13:23:51 +0200 | [diff] [blame] | 1704 | */ |
| 1705 | pages_highmem = preallocate_image_highmem(highmem / 2); |
Aaron Lu | fd432b9 | 2013-11-06 08:41:31 +0800 | [diff] [blame] | 1706 | alloc = count - max_size; |
| 1707 | if (alloc > pages_highmem) |
| 1708 | alloc -= pages_highmem; |
| 1709 | else |
| 1710 | alloc = 0; |
Rafael J. Wysocki | 6715045 | 2010-09-11 20:58:27 +0200 | [diff] [blame] | 1711 | pages = preallocate_image_memory(alloc, avail_normal); |
| 1712 | if (pages < alloc) { |
| 1713 | /* We have exhausted non-highmem pages, try highmem. */ |
| 1714 | alloc -= pages; |
| 1715 | pages += pages_highmem; |
| 1716 | pages_highmem = preallocate_image_highmem(alloc); |
| 1717 | if (pages_highmem < alloc) |
| 1718 | goto err_out; |
| 1719 | pages += pages_highmem; |
| 1720 | /* |
| 1721 | * size is the desired number of saveable pages to leave in |
| 1722 | * memory, so try to preallocate (all memory - size) pages. |
| 1723 | */ |
| 1724 | alloc = (count - pages) - size; |
| 1725 | pages += preallocate_image_highmem(alloc); |
| 1726 | } else { |
| 1727 | /* |
| 1728 | * There are approximately max_size saveable pages at this point |
| 1729 | * and we want to reduce this number down to size. |
| 1730 | */ |
| 1731 | alloc = max_size - size; |
| 1732 | size = preallocate_highmem_fraction(alloc, highmem, count); |
| 1733 | pages_highmem += size; |
| 1734 | alloc -= size; |
| 1735 | size = preallocate_image_memory(alloc, avail_normal); |
| 1736 | pages_highmem += preallocate_image_highmem(alloc - size); |
| 1737 | pages += pages_highmem + size; |
| 1738 | } |
Rafael J. Wysocki | 4bb3343 | 2009-07-08 13:23:51 +0200 | [diff] [blame] | 1739 | |
Rafael J. Wysocki | 64a473c | 2009-07-08 13:24:05 +0200 | [diff] [blame] | 1740 | /* |
| 1741 | * We only need as many page frames for the image as there are saveable |
| 1742 | * pages in memory, but we have allocated more. Release the excessive |
| 1743 | * ones now. |
| 1744 | */ |
| 1745 | free_unnecessary_pages(); |
Rafael J. Wysocki | 4bb3343 | 2009-07-08 13:23:51 +0200 | [diff] [blame] | 1746 | |
| 1747 | out: |
Rafael J. Wysocki | fe41953 | 2009-06-11 23:11:17 +0200 | [diff] [blame] | 1748 | do_gettimeofday(&stop); |
Rafael J. Wysocki | 64a473c | 2009-07-08 13:24:05 +0200 | [diff] [blame] | 1749 | printk(KERN_CONT "done (allocated %lu pages)\n", pages); |
| 1750 | swsusp_show_speed(&start, &stop, pages, "Allocated"); |
Rafael J. Wysocki | fe41953 | 2009-06-11 23:11:17 +0200 | [diff] [blame] | 1751 | |
| 1752 | return 0; |
Rafael J. Wysocki | 64a473c | 2009-07-08 13:24:05 +0200 | [diff] [blame] | 1753 | |
| 1754 | err_out: |
| 1755 | printk(KERN_CONT "\n"); |
| 1756 | swsusp_free(); |
| 1757 | return -ENOMEM; |
Rafael J. Wysocki | fe41953 | 2009-06-11 23:11:17 +0200 | [diff] [blame] | 1758 | } |
| 1759 | |
Rafael J. Wysocki | 8357376 | 2006-12-06 20:34:18 -0800 | [diff] [blame] | 1760 | #ifdef CONFIG_HIGHMEM |
| 1761 | /** |
| 1762 | * count_pages_for_highmem - compute the number of non-highmem pages |
| 1763 | * that will be necessary for creating copies of highmem pages. |
| 1764 | */ |
| 1765 | |
| 1766 | static unsigned int count_pages_for_highmem(unsigned int nr_highmem) |
| 1767 | { |
Rafael J. Wysocki | 64a473c | 2009-07-08 13:24:05 +0200 | [diff] [blame] | 1768 | unsigned int free_highmem = count_free_highmem_pages() + alloc_highmem; |
Rafael J. Wysocki | 8357376 | 2006-12-06 20:34:18 -0800 | [diff] [blame] | 1769 | |
| 1770 | if (free_highmem >= nr_highmem) |
| 1771 | nr_highmem = 0; |
| 1772 | else |
| 1773 | nr_highmem -= free_highmem; |
| 1774 | |
| 1775 | return nr_highmem; |
| 1776 | } |
| 1777 | #else |
| 1778 | static unsigned int |
| 1779 | count_pages_for_highmem(unsigned int nr_highmem) { return 0; } |
| 1780 | #endif /* CONFIG_HIGHMEM */ |
Rafael J. Wysocki | 25761b6 | 2005-10-30 14:59:56 -0800 | [diff] [blame] | 1781 | |
| 1782 | /** |
Rafael J. Wysocki | 8357376 | 2006-12-06 20:34:18 -0800 | [diff] [blame] | 1783 | * enough_free_mem - Make sure we have enough free memory for the |
| 1784 | * snapshot image. |
Rafael J. Wysocki | 25761b6 | 2005-10-30 14:59:56 -0800 | [diff] [blame] | 1785 | */ |
| 1786 | |
Rafael J. Wysocki | 8357376 | 2006-12-06 20:34:18 -0800 | [diff] [blame] | 1787 | static int enough_free_mem(unsigned int nr_pages, unsigned int nr_highmem) |
Rafael J. Wysocki | 25761b6 | 2005-10-30 14:59:56 -0800 | [diff] [blame] | 1788 | { |
Rafael J. Wysocki | e5e2fa7 | 2006-01-06 00:14:20 -0800 | [diff] [blame] | 1789 | struct zone *zone; |
Rafael J. Wysocki | 64a473c | 2009-07-08 13:24:05 +0200 | [diff] [blame] | 1790 | unsigned int free = alloc_normal; |
Rafael J. Wysocki | e5e2fa7 | 2006-01-06 00:14:20 -0800 | [diff] [blame] | 1791 | |
Gerald Schaefer | 98e73dc | 2009-07-22 00:36:56 +0200 | [diff] [blame] | 1792 | for_each_populated_zone(zone) |
Rafael J. Wysocki | 8357376 | 2006-12-06 20:34:18 -0800 | [diff] [blame] | 1793 | if (!is_highmem(zone)) |
Christoph Lameter | d23ad42 | 2007-02-10 01:43:02 -0800 | [diff] [blame] | 1794 | free += zone_page_state(zone, NR_FREE_PAGES); |
Rafael J. Wysocki | 940864d | 2006-09-25 23:32:55 -0700 | [diff] [blame] | 1795 | |
Rafael J. Wysocki | 8357376 | 2006-12-06 20:34:18 -0800 | [diff] [blame] | 1796 | nr_pages += count_pages_for_highmem(nr_highmem); |
Rafael J. Wysocki | 64a473c | 2009-07-08 13:24:05 +0200 | [diff] [blame] | 1797 | pr_debug("PM: Normal pages needed: %u + %u, available pages: %u\n", |
| 1798 | nr_pages, PAGES_FOR_IO, free); |
Rafael J. Wysocki | 940864d | 2006-09-25 23:32:55 -0700 | [diff] [blame] | 1799 | |
Rafael J. Wysocki | 64a473c | 2009-07-08 13:24:05 +0200 | [diff] [blame] | 1800 | return free > nr_pages + PAGES_FOR_IO; |
Rafael J. Wysocki | 25761b6 | 2005-10-30 14:59:56 -0800 | [diff] [blame] | 1801 | } |
| 1802 | |
Rafael J. Wysocki | 8357376 | 2006-12-06 20:34:18 -0800 | [diff] [blame] | 1803 | #ifdef CONFIG_HIGHMEM |
| 1804 | /** |
| 1805 | * get_highmem_buffer - if there are some highmem pages in the suspend |
| 1806 | * image, we may need the buffer to copy them and/or load their data. |
| 1807 | */ |
| 1808 | |
| 1809 | static inline int get_highmem_buffer(int safe_needed) |
| 1810 | { |
| 1811 | buffer = get_image_page(GFP_ATOMIC | __GFP_COLD, safe_needed); |
| 1812 | return buffer ? 0 : -ENOMEM; |
| 1813 | } |
| 1814 | |
| 1815 | /** |
| 1816 | * alloc_highmem_image_pages - allocate some highmem pages for the image. |
| 1817 | * Try to allocate as many pages as needed, but if the number of free |
| 1818 | * highmem pages is lesser than that, allocate them all. |
| 1819 | */ |
| 1820 | |
| 1821 | static inline unsigned int |
Rafael J. Wysocki | 64a473c | 2009-07-08 13:24:05 +0200 | [diff] [blame] | 1822 | alloc_highmem_pages(struct memory_bitmap *bm, unsigned int nr_highmem) |
Rafael J. Wysocki | 8357376 | 2006-12-06 20:34:18 -0800 | [diff] [blame] | 1823 | { |
| 1824 | unsigned int to_alloc = count_free_highmem_pages(); |
| 1825 | |
| 1826 | if (to_alloc > nr_highmem) |
| 1827 | to_alloc = nr_highmem; |
| 1828 | |
| 1829 | nr_highmem -= to_alloc; |
| 1830 | while (to_alloc-- > 0) { |
| 1831 | struct page *page; |
| 1832 | |
| 1833 | page = alloc_image_page(__GFP_HIGHMEM); |
| 1834 | memory_bm_set_bit(bm, page_to_pfn(page)); |
| 1835 | } |
| 1836 | return nr_highmem; |
| 1837 | } |
| 1838 | #else |
| 1839 | static inline int get_highmem_buffer(int safe_needed) { return 0; } |
| 1840 | |
| 1841 | static inline unsigned int |
Rafael J. Wysocki | 64a473c | 2009-07-08 13:24:05 +0200 | [diff] [blame] | 1842 | alloc_highmem_pages(struct memory_bitmap *bm, unsigned int n) { return 0; } |
Rafael J. Wysocki | 8357376 | 2006-12-06 20:34:18 -0800 | [diff] [blame] | 1843 | #endif /* CONFIG_HIGHMEM */ |
| 1844 | |
| 1845 | /** |
| 1846 | * swsusp_alloc - allocate memory for the suspend image |
| 1847 | * |
| 1848 | * We first try to allocate as many highmem pages as there are |
| 1849 | * saveable highmem pages in the system. If that fails, we allocate |
| 1850 | * non-highmem pages for the copies of the remaining highmem ones. |
| 1851 | * |
| 1852 | * In this approach it is likely that the copies of highmem pages will |
| 1853 | * also be located in the high memory, because of the way in which |
| 1854 | * copy_data_pages() works. |
| 1855 | */ |
| 1856 | |
Rafael J. Wysocki | b788db7 | 2006-09-25 23:32:54 -0700 | [diff] [blame] | 1857 | static int |
| 1858 | swsusp_alloc(struct memory_bitmap *orig_bm, struct memory_bitmap *copy_bm, |
Rafael J. Wysocki | 8357376 | 2006-12-06 20:34:18 -0800 | [diff] [blame] | 1859 | unsigned int nr_pages, unsigned int nr_highmem) |
Rafael J. Wysocki | 054bd4c | 2005-11-08 21:34:39 -0800 | [diff] [blame] | 1860 | { |
Rafael J. Wysocki | 8357376 | 2006-12-06 20:34:18 -0800 | [diff] [blame] | 1861 | if (nr_highmem > 0) { |
Stanislaw Gruszka | 2e725a0 | 2011-02-12 21:06:51 +0100 | [diff] [blame] | 1862 | if (get_highmem_buffer(PG_ANY)) |
Rafael J. Wysocki | 64a473c | 2009-07-08 13:24:05 +0200 | [diff] [blame] | 1863 | goto err_out; |
| 1864 | if (nr_highmem > alloc_highmem) { |
| 1865 | nr_highmem -= alloc_highmem; |
| 1866 | nr_pages += alloc_highmem_pages(copy_bm, nr_highmem); |
| 1867 | } |
Rafael J. Wysocki | 8357376 | 2006-12-06 20:34:18 -0800 | [diff] [blame] | 1868 | } |
Rafael J. Wysocki | 64a473c | 2009-07-08 13:24:05 +0200 | [diff] [blame] | 1869 | if (nr_pages > alloc_normal) { |
| 1870 | nr_pages -= alloc_normal; |
| 1871 | while (nr_pages-- > 0) { |
| 1872 | struct page *page; |
Rafael J. Wysocki | 8357376 | 2006-12-06 20:34:18 -0800 | [diff] [blame] | 1873 | |
Rafael J. Wysocki | 64a473c | 2009-07-08 13:24:05 +0200 | [diff] [blame] | 1874 | page = alloc_image_page(GFP_ATOMIC | __GFP_COLD); |
| 1875 | if (!page) |
| 1876 | goto err_out; |
| 1877 | memory_bm_set_bit(copy_bm, page_to_pfn(page)); |
| 1878 | } |
Rafael J. Wysocki | 054bd4c | 2005-11-08 21:34:39 -0800 | [diff] [blame] | 1879 | } |
Rafael J. Wysocki | 64a473c | 2009-07-08 13:24:05 +0200 | [diff] [blame] | 1880 | |
Rafael J. Wysocki | 054bd4c | 2005-11-08 21:34:39 -0800 | [diff] [blame] | 1881 | return 0; |
Rafael J. Wysocki | b788db7 | 2006-09-25 23:32:54 -0700 | [diff] [blame] | 1882 | |
Rafael J. Wysocki | 64a473c | 2009-07-08 13:24:05 +0200 | [diff] [blame] | 1883 | err_out: |
Rafael J. Wysocki | b788db7 | 2006-09-25 23:32:54 -0700 | [diff] [blame] | 1884 | swsusp_free(); |
Stanislaw Gruszka | 2e725a0 | 2011-02-12 21:06:51 +0100 | [diff] [blame] | 1885 | return -ENOMEM; |
Rafael J. Wysocki | 054bd4c | 2005-11-08 21:34:39 -0800 | [diff] [blame] | 1886 | } |
Rafael J. Wysocki | 25761b6 | 2005-10-30 14:59:56 -0800 | [diff] [blame] | 1887 | |
Andi Kleen | 722a9f9 | 2014-05-02 00:44:38 +0200 | [diff] [blame] | 1888 | asmlinkage __visible int swsusp_save(void) |
Rafael J. Wysocki | 25761b6 | 2005-10-30 14:59:56 -0800 | [diff] [blame] | 1889 | { |
Rafael J. Wysocki | 8357376 | 2006-12-06 20:34:18 -0800 | [diff] [blame] | 1890 | unsigned int nr_pages, nr_highmem; |
Rafael J. Wysocki | 25761b6 | 2005-10-30 14:59:56 -0800 | [diff] [blame] | 1891 | |
Frans Pop | 07c3bb5 | 2010-02-11 23:09:08 +0100 | [diff] [blame] | 1892 | printk(KERN_INFO "PM: Creating hibernation image:\n"); |
Rafael J. Wysocki | 25761b6 | 2005-10-30 14:59:56 -0800 | [diff] [blame] | 1893 | |
Christoph Lameter | 9f8f217 | 2008-02-04 22:29:11 -0800 | [diff] [blame] | 1894 | drain_local_pages(NULL); |
Rafael J. Wysocki | a0f4965 | 2005-10-30 14:59:57 -0800 | [diff] [blame] | 1895 | nr_pages = count_data_pages(); |
Rafael J. Wysocki | 8357376 | 2006-12-06 20:34:18 -0800 | [diff] [blame] | 1896 | nr_highmem = count_highmem_pages(); |
Rafael J. Wysocki | 2397672 | 2007-12-08 02:09:43 +0100 | [diff] [blame] | 1897 | printk(KERN_INFO "PM: Need to copy %u pages\n", nr_pages + nr_highmem); |
Rafael J. Wysocki | 25761b6 | 2005-10-30 14:59:56 -0800 | [diff] [blame] | 1898 | |
Rafael J. Wysocki | 8357376 | 2006-12-06 20:34:18 -0800 | [diff] [blame] | 1899 | if (!enough_free_mem(nr_pages, nr_highmem)) { |
Rafael J. Wysocki | 2397672 | 2007-12-08 02:09:43 +0100 | [diff] [blame] | 1900 | printk(KERN_ERR "PM: Not enough free memory\n"); |
Rafael J. Wysocki | 25761b6 | 2005-10-30 14:59:56 -0800 | [diff] [blame] | 1901 | return -ENOMEM; |
| 1902 | } |
| 1903 | |
Rafael J. Wysocki | 8357376 | 2006-12-06 20:34:18 -0800 | [diff] [blame] | 1904 | if (swsusp_alloc(&orig_bm, ©_bm, nr_pages, nr_highmem)) { |
Rafael J. Wysocki | 2397672 | 2007-12-08 02:09:43 +0100 | [diff] [blame] | 1905 | printk(KERN_ERR "PM: Memory allocation failed\n"); |
Rafael J. Wysocki | a0f4965 | 2005-10-30 14:59:57 -0800 | [diff] [blame] | 1906 | return -ENOMEM; |
Rafael J. Wysocki | 8357376 | 2006-12-06 20:34:18 -0800 | [diff] [blame] | 1907 | } |
Rafael J. Wysocki | 25761b6 | 2005-10-30 14:59:56 -0800 | [diff] [blame] | 1908 | |
| 1909 | /* During allocating of suspend pagedir, new cold pages may appear. |
| 1910 | * Kill them. |
| 1911 | */ |
Christoph Lameter | 9f8f217 | 2008-02-04 22:29:11 -0800 | [diff] [blame] | 1912 | drain_local_pages(NULL); |
Rafael J. Wysocki | b788db7 | 2006-09-25 23:32:54 -0700 | [diff] [blame] | 1913 | copy_data_pages(©_bm, &orig_bm); |
Rafael J. Wysocki | 25761b6 | 2005-10-30 14:59:56 -0800 | [diff] [blame] | 1914 | |
| 1915 | /* |
| 1916 | * End of critical section. From now on, we can write to memory, |
| 1917 | * but we should not touch disk. This specially means we must _not_ |
| 1918 | * touch swap space! Except we must write out our image of course. |
| 1919 | */ |
| 1920 | |
Rafael J. Wysocki | 8357376 | 2006-12-06 20:34:18 -0800 | [diff] [blame] | 1921 | nr_pages += nr_highmem; |
Rafael J. Wysocki | a0f4965 | 2005-10-30 14:59:57 -0800 | [diff] [blame] | 1922 | nr_copy_pages = nr_pages; |
Rafael J. Wysocki | 8357376 | 2006-12-06 20:34:18 -0800 | [diff] [blame] | 1923 | nr_meta_pages = DIV_ROUND_UP(nr_pages * sizeof(long), PAGE_SIZE); |
Rafael J. Wysocki | a0f4965 | 2005-10-30 14:59:57 -0800 | [diff] [blame] | 1924 | |
Rafael J. Wysocki | 2397672 | 2007-12-08 02:09:43 +0100 | [diff] [blame] | 1925 | printk(KERN_INFO "PM: Hibernation image created (%d pages copied)\n", |
| 1926 | nr_pages); |
Rafael J. Wysocki | 8357376 | 2006-12-06 20:34:18 -0800 | [diff] [blame] | 1927 | |
Rafael J. Wysocki | 25761b6 | 2005-10-30 14:59:56 -0800 | [diff] [blame] | 1928 | return 0; |
| 1929 | } |
Rafael J. Wysocki | f577eb3 | 2006-03-23 02:59:59 -0800 | [diff] [blame] | 1930 | |
Rafael J. Wysocki | d307c4a | 2007-10-18 03:04:52 -0700 | [diff] [blame] | 1931 | #ifndef CONFIG_ARCH_HIBERNATION_HEADER |
| 1932 | static int init_header_complete(struct swsusp_info *info) |
| 1933 | { |
| 1934 | memcpy(&info->uts, init_utsname(), sizeof(struct new_utsname)); |
| 1935 | info->version_code = LINUX_VERSION_CODE; |
| 1936 | return 0; |
| 1937 | } |
| 1938 | |
| 1939 | static char *check_image_kernel(struct swsusp_info *info) |
| 1940 | { |
| 1941 | if (info->version_code != LINUX_VERSION_CODE) |
| 1942 | return "kernel version"; |
| 1943 | if (strcmp(info->uts.sysname,init_utsname()->sysname)) |
| 1944 | return "system type"; |
| 1945 | if (strcmp(info->uts.release,init_utsname()->release)) |
| 1946 | return "kernel release"; |
| 1947 | if (strcmp(info->uts.version,init_utsname()->version)) |
| 1948 | return "version"; |
| 1949 | if (strcmp(info->uts.machine,init_utsname()->machine)) |
| 1950 | return "machine"; |
| 1951 | return NULL; |
| 1952 | } |
| 1953 | #endif /* CONFIG_ARCH_HIBERNATION_HEADER */ |
| 1954 | |
Rafael J. Wysocki | af508b3 | 2007-10-26 00:59:31 +0200 | [diff] [blame] | 1955 | unsigned long snapshot_get_image_size(void) |
| 1956 | { |
| 1957 | return nr_copy_pages + nr_meta_pages + 1; |
| 1958 | } |
| 1959 | |
Rafael J. Wysocki | d307c4a | 2007-10-18 03:04:52 -0700 | [diff] [blame] | 1960 | static int init_header(struct swsusp_info *info) |
Rafael J. Wysocki | f577eb3 | 2006-03-23 02:59:59 -0800 | [diff] [blame] | 1961 | { |
| 1962 | memset(info, 0, sizeof(struct swsusp_info)); |
Jiang Liu | 0ed5fd1 | 2013-07-03 15:03:43 -0700 | [diff] [blame] | 1963 | info->num_physpages = get_num_physpages(); |
Rafael J. Wysocki | f577eb3 | 2006-03-23 02:59:59 -0800 | [diff] [blame] | 1964 | info->image_pages = nr_copy_pages; |
Rafael J. Wysocki | af508b3 | 2007-10-26 00:59:31 +0200 | [diff] [blame] | 1965 | info->pages = snapshot_get_image_size(); |
Rafael J. Wysocki | 6e1819d | 2006-03-23 03:00:03 -0800 | [diff] [blame] | 1966 | info->size = info->pages; |
| 1967 | info->size <<= PAGE_SHIFT; |
Rafael J. Wysocki | d307c4a | 2007-10-18 03:04:52 -0700 | [diff] [blame] | 1968 | return init_header_complete(info); |
Rafael J. Wysocki | f577eb3 | 2006-03-23 02:59:59 -0800 | [diff] [blame] | 1969 | } |
| 1970 | |
| 1971 | /** |
Rafael J. Wysocki | 940864d | 2006-09-25 23:32:55 -0700 | [diff] [blame] | 1972 | * pack_pfns - pfns corresponding to the set bits found in the bitmap @bm |
| 1973 | * are stored in the array @buf[] (1 page at a time) |
Rafael J. Wysocki | f577eb3 | 2006-03-23 02:59:59 -0800 | [diff] [blame] | 1974 | */ |
| 1975 | |
Rafael J. Wysocki | b788db7 | 2006-09-25 23:32:54 -0700 | [diff] [blame] | 1976 | static inline void |
Rafael J. Wysocki | 940864d | 2006-09-25 23:32:55 -0700 | [diff] [blame] | 1977 | pack_pfns(unsigned long *buf, struct memory_bitmap *bm) |
Rafael J. Wysocki | f577eb3 | 2006-03-23 02:59:59 -0800 | [diff] [blame] | 1978 | { |
| 1979 | int j; |
| 1980 | |
Rafael J. Wysocki | b788db7 | 2006-09-25 23:32:54 -0700 | [diff] [blame] | 1981 | for (j = 0; j < PAGE_SIZE / sizeof(long); j++) { |
Rafael J. Wysocki | 940864d | 2006-09-25 23:32:55 -0700 | [diff] [blame] | 1982 | buf[j] = memory_bm_next_pfn(bm); |
| 1983 | if (unlikely(buf[j] == BM_END_OF_MAP)) |
Rafael J. Wysocki | b788db7 | 2006-09-25 23:32:54 -0700 | [diff] [blame] | 1984 | break; |
Martin Schwidefsky | 85055dd | 2011-08-17 20:42:24 +0200 | [diff] [blame] | 1985 | /* Save page key for data page (s390 only). */ |
| 1986 | page_key_read(buf + j); |
Rafael J. Wysocki | f577eb3 | 2006-03-23 02:59:59 -0800 | [diff] [blame] | 1987 | } |
Rafael J. Wysocki | f577eb3 | 2006-03-23 02:59:59 -0800 | [diff] [blame] | 1988 | } |
| 1989 | |
| 1990 | /** |
| 1991 | * snapshot_read_next - used for reading the system memory snapshot. |
| 1992 | * |
| 1993 | * On the first call to it @handle should point to a zeroed |
| 1994 | * snapshot_handle structure. The structure gets updated and a pointer |
| 1995 | * to it should be passed to this function every next time. |
| 1996 | * |
Rafael J. Wysocki | f577eb3 | 2006-03-23 02:59:59 -0800 | [diff] [blame] | 1997 | * On success the function returns a positive number. Then, the caller |
| 1998 | * is allowed to read up to the returned number of bytes from the memory |
Jiri Slaby | d3c1b24 | 2010-05-01 23:52:02 +0200 | [diff] [blame] | 1999 | * location computed by the data_of() macro. |
Rafael J. Wysocki | f577eb3 | 2006-03-23 02:59:59 -0800 | [diff] [blame] | 2000 | * |
| 2001 | * The function returns 0 to indicate the end of data stream condition, |
| 2002 | * and a negative number is returned on error. In such cases the |
| 2003 | * structure pointed to by @handle is not updated and should not be used |
| 2004 | * any more. |
| 2005 | */ |
| 2006 | |
Jiri Slaby | d3c1b24 | 2010-05-01 23:52:02 +0200 | [diff] [blame] | 2007 | int snapshot_read_next(struct snapshot_handle *handle) |
Rafael J. Wysocki | f577eb3 | 2006-03-23 02:59:59 -0800 | [diff] [blame] | 2008 | { |
Rafael J. Wysocki | fb13a28 | 2006-09-25 23:32:46 -0700 | [diff] [blame] | 2009 | if (handle->cur > nr_meta_pages + nr_copy_pages) |
Rafael J. Wysocki | f577eb3 | 2006-03-23 02:59:59 -0800 | [diff] [blame] | 2010 | return 0; |
Rafael J. Wysocki | b788db7 | 2006-09-25 23:32:54 -0700 | [diff] [blame] | 2011 | |
Rafael J. Wysocki | f577eb3 | 2006-03-23 02:59:59 -0800 | [diff] [blame] | 2012 | if (!buffer) { |
| 2013 | /* This makes the buffer be freed by swsusp_free() */ |
Rafael J. Wysocki | 8357376 | 2006-12-06 20:34:18 -0800 | [diff] [blame] | 2014 | buffer = get_image_page(GFP_ATOMIC, PG_ANY); |
Rafael J. Wysocki | f577eb3 | 2006-03-23 02:59:59 -0800 | [diff] [blame] | 2015 | if (!buffer) |
| 2016 | return -ENOMEM; |
| 2017 | } |
Jiri Slaby | d3c1b24 | 2010-05-01 23:52:02 +0200 | [diff] [blame] | 2018 | if (!handle->cur) { |
Rafael J. Wysocki | d307c4a | 2007-10-18 03:04:52 -0700 | [diff] [blame] | 2019 | int error; |
| 2020 | |
| 2021 | error = init_header((struct swsusp_info *)buffer); |
| 2022 | if (error) |
| 2023 | return error; |
Rafael J. Wysocki | f577eb3 | 2006-03-23 02:59:59 -0800 | [diff] [blame] | 2024 | handle->buffer = buffer; |
Rafael J. Wysocki | b788db7 | 2006-09-25 23:32:54 -0700 | [diff] [blame] | 2025 | memory_bm_position_reset(&orig_bm); |
| 2026 | memory_bm_position_reset(©_bm); |
Jiri Slaby | d3c1b24 | 2010-05-01 23:52:02 +0200 | [diff] [blame] | 2027 | } else if (handle->cur <= nr_meta_pages) { |
Jan Beulich | 3ecb01d | 2010-10-26 14:22:27 -0700 | [diff] [blame] | 2028 | clear_page(buffer); |
Jiri Slaby | d3c1b24 | 2010-05-01 23:52:02 +0200 | [diff] [blame] | 2029 | pack_pfns(buffer, &orig_bm); |
Rafael J. Wysocki | f577eb3 | 2006-03-23 02:59:59 -0800 | [diff] [blame] | 2030 | } else { |
Jiri Slaby | d3c1b24 | 2010-05-01 23:52:02 +0200 | [diff] [blame] | 2031 | struct page *page; |
| 2032 | |
| 2033 | page = pfn_to_page(memory_bm_next_pfn(©_bm)); |
| 2034 | if (PageHighMem(page)) { |
| 2035 | /* Highmem pages are copied to the buffer, |
| 2036 | * because we can't return with a kmapped |
| 2037 | * highmem page (we may not be called again). |
| 2038 | */ |
| 2039 | void *kaddr; |
| 2040 | |
Cong Wang | 0de9a1e | 2011-11-25 23:14:38 +0800 | [diff] [blame] | 2041 | kaddr = kmap_atomic(page); |
Jan Beulich | 3ecb01d | 2010-10-26 14:22:27 -0700 | [diff] [blame] | 2042 | copy_page(buffer, kaddr); |
Cong Wang | 0de9a1e | 2011-11-25 23:14:38 +0800 | [diff] [blame] | 2043 | kunmap_atomic(kaddr); |
Jiri Slaby | d3c1b24 | 2010-05-01 23:52:02 +0200 | [diff] [blame] | 2044 | handle->buffer = buffer; |
| 2045 | } else { |
| 2046 | handle->buffer = page_address(page); |
| 2047 | } |
Rafael J. Wysocki | f577eb3 | 2006-03-23 02:59:59 -0800 | [diff] [blame] | 2048 | } |
Jiri Slaby | d3c1b24 | 2010-05-01 23:52:02 +0200 | [diff] [blame] | 2049 | handle->cur++; |
| 2050 | return PAGE_SIZE; |
Rafael J. Wysocki | f577eb3 | 2006-03-23 02:59:59 -0800 | [diff] [blame] | 2051 | } |
| 2052 | |
| 2053 | /** |
| 2054 | * mark_unsafe_pages - mark the pages that cannot be used for storing |
| 2055 | * the image during resume, because they conflict with the pages that |
| 2056 | * had been used before suspend |
| 2057 | */ |
| 2058 | |
Rafael J. Wysocki | 940864d | 2006-09-25 23:32:55 -0700 | [diff] [blame] | 2059 | static int mark_unsafe_pages(struct memory_bitmap *bm) |
Rafael J. Wysocki | f577eb3 | 2006-03-23 02:59:59 -0800 | [diff] [blame] | 2060 | { |
| 2061 | struct zone *zone; |
Rafael J. Wysocki | ae83c5ee | 2006-09-25 23:32:45 -0700 | [diff] [blame] | 2062 | unsigned long pfn, max_zone_pfn; |
Rafael J. Wysocki | f577eb3 | 2006-03-23 02:59:59 -0800 | [diff] [blame] | 2063 | |
| 2064 | /* Clear page flags */ |
Gerald Schaefer | 98e73dc | 2009-07-22 00:36:56 +0200 | [diff] [blame] | 2065 | for_each_populated_zone(zone) { |
Xishi Qiu | c33bc31 | 2013-09-11 14:21:44 -0700 | [diff] [blame] | 2066 | max_zone_pfn = zone_end_pfn(zone); |
Rafael J. Wysocki | ae83c5ee | 2006-09-25 23:32:45 -0700 | [diff] [blame] | 2067 | for (pfn = zone->zone_start_pfn; pfn < max_zone_pfn; pfn++) |
| 2068 | if (pfn_valid(pfn)) |
Rafael J. Wysocki | 7be9823 | 2007-05-06 14:50:42 -0700 | [diff] [blame] | 2069 | swsusp_unset_page_free(pfn_to_page(pfn)); |
Rafael J. Wysocki | f577eb3 | 2006-03-23 02:59:59 -0800 | [diff] [blame] | 2070 | } |
| 2071 | |
Rafael J. Wysocki | 940864d | 2006-09-25 23:32:55 -0700 | [diff] [blame] | 2072 | /* Mark pages that correspond to the "original" pfns as "unsafe" */ |
| 2073 | memory_bm_position_reset(bm); |
| 2074 | do { |
| 2075 | pfn = memory_bm_next_pfn(bm); |
| 2076 | if (likely(pfn != BM_END_OF_MAP)) { |
| 2077 | if (likely(pfn_valid(pfn))) |
Rafael J. Wysocki | 7be9823 | 2007-05-06 14:50:42 -0700 | [diff] [blame] | 2078 | swsusp_set_page_free(pfn_to_page(pfn)); |
Rafael J. Wysocki | 940864d | 2006-09-25 23:32:55 -0700 | [diff] [blame] | 2079 | else |
| 2080 | return -EFAULT; |
| 2081 | } |
| 2082 | } while (pfn != BM_END_OF_MAP); |
Rafael J. Wysocki | f577eb3 | 2006-03-23 02:59:59 -0800 | [diff] [blame] | 2083 | |
Rafael J. Wysocki | 940864d | 2006-09-25 23:32:55 -0700 | [diff] [blame] | 2084 | allocated_unsafe_pages = 0; |
Rafael J. Wysocki | 968808b8 | 2006-06-23 02:04:48 -0700 | [diff] [blame] | 2085 | |
Rafael J. Wysocki | f577eb3 | 2006-03-23 02:59:59 -0800 | [diff] [blame] | 2086 | return 0; |
| 2087 | } |
| 2088 | |
Rafael J. Wysocki | 940864d | 2006-09-25 23:32:55 -0700 | [diff] [blame] | 2089 | static void |
| 2090 | duplicate_memory_bitmap(struct memory_bitmap *dst, struct memory_bitmap *src) |
Rafael J. Wysocki | f577eb3 | 2006-03-23 02:59:59 -0800 | [diff] [blame] | 2091 | { |
Rafael J. Wysocki | 940864d | 2006-09-25 23:32:55 -0700 | [diff] [blame] | 2092 | unsigned long pfn; |
| 2093 | |
| 2094 | memory_bm_position_reset(src); |
| 2095 | pfn = memory_bm_next_pfn(src); |
| 2096 | while (pfn != BM_END_OF_MAP) { |
| 2097 | memory_bm_set_bit(dst, pfn); |
| 2098 | pfn = memory_bm_next_pfn(src); |
Rafael J. Wysocki | f577eb3 | 2006-03-23 02:59:59 -0800 | [diff] [blame] | 2099 | } |
| 2100 | } |
| 2101 | |
Rafael J. Wysocki | d307c4a | 2007-10-18 03:04:52 -0700 | [diff] [blame] | 2102 | static int check_header(struct swsusp_info *info) |
Rafael J. Wysocki | f577eb3 | 2006-03-23 02:59:59 -0800 | [diff] [blame] | 2103 | { |
Rafael J. Wysocki | d307c4a | 2007-10-18 03:04:52 -0700 | [diff] [blame] | 2104 | char *reason; |
Rafael J. Wysocki | f577eb3 | 2006-03-23 02:59:59 -0800 | [diff] [blame] | 2105 | |
Rafael J. Wysocki | d307c4a | 2007-10-18 03:04:52 -0700 | [diff] [blame] | 2106 | reason = check_image_kernel(info); |
Jiang Liu | 0ed5fd1 | 2013-07-03 15:03:43 -0700 | [diff] [blame] | 2107 | if (!reason && info->num_physpages != get_num_physpages()) |
Rafael J. Wysocki | f577eb3 | 2006-03-23 02:59:59 -0800 | [diff] [blame] | 2108 | reason = "memory size"; |
Rafael J. Wysocki | f577eb3 | 2006-03-23 02:59:59 -0800 | [diff] [blame] | 2109 | if (reason) { |
Rafael J. Wysocki | 2397672 | 2007-12-08 02:09:43 +0100 | [diff] [blame] | 2110 | printk(KERN_ERR "PM: Image mismatch: %s\n", reason); |
Rafael J. Wysocki | f577eb3 | 2006-03-23 02:59:59 -0800 | [diff] [blame] | 2111 | return -EPERM; |
| 2112 | } |
| 2113 | return 0; |
| 2114 | } |
| 2115 | |
| 2116 | /** |
| 2117 | * load header - check the image header and copy data from it |
| 2118 | */ |
| 2119 | |
Rafael J. Wysocki | 940864d | 2006-09-25 23:32:55 -0700 | [diff] [blame] | 2120 | static int |
| 2121 | load_header(struct swsusp_info *info) |
Rafael J. Wysocki | f577eb3 | 2006-03-23 02:59:59 -0800 | [diff] [blame] | 2122 | { |
| 2123 | int error; |
Rafael J. Wysocki | f577eb3 | 2006-03-23 02:59:59 -0800 | [diff] [blame] | 2124 | |
Rafael J. Wysocki | 940864d | 2006-09-25 23:32:55 -0700 | [diff] [blame] | 2125 | restore_pblist = NULL; |
Rafael J. Wysocki | f577eb3 | 2006-03-23 02:59:59 -0800 | [diff] [blame] | 2126 | error = check_header(info); |
| 2127 | if (!error) { |
Rafael J. Wysocki | f577eb3 | 2006-03-23 02:59:59 -0800 | [diff] [blame] | 2128 | nr_copy_pages = info->image_pages; |
| 2129 | nr_meta_pages = info->pages - info->image_pages - 1; |
| 2130 | } |
| 2131 | return error; |
| 2132 | } |
| 2133 | |
| 2134 | /** |
Rafael J. Wysocki | 940864d | 2006-09-25 23:32:55 -0700 | [diff] [blame] | 2135 | * unpack_orig_pfns - for each element of @buf[] (1 page at a time) set |
| 2136 | * the corresponding bit in the memory bitmap @bm |
Rafael J. Wysocki | f577eb3 | 2006-03-23 02:59:59 -0800 | [diff] [blame] | 2137 | */ |
Rafael J. Wysocki | 6964327 | 2008-11-11 21:32:44 +0100 | [diff] [blame] | 2138 | static int unpack_orig_pfns(unsigned long *buf, struct memory_bitmap *bm) |
Rafael J. Wysocki | f577eb3 | 2006-03-23 02:59:59 -0800 | [diff] [blame] | 2139 | { |
| 2140 | int j; |
| 2141 | |
Rafael J. Wysocki | 940864d | 2006-09-25 23:32:55 -0700 | [diff] [blame] | 2142 | for (j = 0; j < PAGE_SIZE / sizeof(long); j++) { |
| 2143 | if (unlikely(buf[j] == BM_END_OF_MAP)) |
| 2144 | break; |
| 2145 | |
Martin Schwidefsky | 85055dd | 2011-08-17 20:42:24 +0200 | [diff] [blame] | 2146 | /* Extract and buffer page key for data page (s390 only). */ |
| 2147 | page_key_memorize(buf + j); |
| 2148 | |
Rafael J. Wysocki | 6964327 | 2008-11-11 21:32:44 +0100 | [diff] [blame] | 2149 | if (memory_bm_pfn_present(bm, buf[j])) |
| 2150 | memory_bm_set_bit(bm, buf[j]); |
| 2151 | else |
| 2152 | return -EFAULT; |
Rafael J. Wysocki | f577eb3 | 2006-03-23 02:59:59 -0800 | [diff] [blame] | 2153 | } |
Rafael J. Wysocki | 6964327 | 2008-11-11 21:32:44 +0100 | [diff] [blame] | 2154 | |
| 2155 | return 0; |
Rafael J. Wysocki | f577eb3 | 2006-03-23 02:59:59 -0800 | [diff] [blame] | 2156 | } |
| 2157 | |
Rafael J. Wysocki | 8357376 | 2006-12-06 20:34:18 -0800 | [diff] [blame] | 2158 | /* List of "safe" pages that may be used to store data loaded from the suspend |
| 2159 | * image |
| 2160 | */ |
| 2161 | static struct linked_page *safe_pages_list; |
| 2162 | |
| 2163 | #ifdef CONFIG_HIGHMEM |
| 2164 | /* struct highmem_pbe is used for creating the list of highmem pages that |
| 2165 | * should be restored atomically during the resume from disk, because the page |
| 2166 | * frames they have occupied before the suspend are in use. |
| 2167 | */ |
| 2168 | struct highmem_pbe { |
| 2169 | struct page *copy_page; /* data is here now */ |
| 2170 | struct page *orig_page; /* data was here before the suspend */ |
| 2171 | struct highmem_pbe *next; |
| 2172 | }; |
| 2173 | |
| 2174 | /* List of highmem PBEs needed for restoring the highmem pages that were |
| 2175 | * allocated before the suspend and included in the suspend image, but have |
| 2176 | * also been allocated by the "resume" kernel, so their contents cannot be |
| 2177 | * written directly to their "original" page frames. |
| 2178 | */ |
| 2179 | static struct highmem_pbe *highmem_pblist; |
| 2180 | |
| 2181 | /** |
| 2182 | * count_highmem_image_pages - compute the number of highmem pages in the |
| 2183 | * suspend image. The bits in the memory bitmap @bm that correspond to the |
| 2184 | * image pages are assumed to be set. |
| 2185 | */ |
| 2186 | |
| 2187 | static unsigned int count_highmem_image_pages(struct memory_bitmap *bm) |
| 2188 | { |
| 2189 | unsigned long pfn; |
| 2190 | unsigned int cnt = 0; |
| 2191 | |
| 2192 | memory_bm_position_reset(bm); |
| 2193 | pfn = memory_bm_next_pfn(bm); |
| 2194 | while (pfn != BM_END_OF_MAP) { |
| 2195 | if (PageHighMem(pfn_to_page(pfn))) |
| 2196 | cnt++; |
| 2197 | |
| 2198 | pfn = memory_bm_next_pfn(bm); |
| 2199 | } |
| 2200 | return cnt; |
| 2201 | } |
| 2202 | |
| 2203 | /** |
| 2204 | * prepare_highmem_image - try to allocate as many highmem pages as |
| 2205 | * there are highmem image pages (@nr_highmem_p points to the variable |
| 2206 | * containing the number of highmem image pages). The pages that are |
| 2207 | * "safe" (ie. will not be overwritten when the suspend image is |
| 2208 | * restored) have the corresponding bits set in @bm (it must be |
| 2209 | * unitialized). |
| 2210 | * |
| 2211 | * NOTE: This function should not be called if there are no highmem |
| 2212 | * image pages. |
| 2213 | */ |
| 2214 | |
| 2215 | static unsigned int safe_highmem_pages; |
| 2216 | |
| 2217 | static struct memory_bitmap *safe_highmem_bm; |
| 2218 | |
| 2219 | static int |
| 2220 | prepare_highmem_image(struct memory_bitmap *bm, unsigned int *nr_highmem_p) |
| 2221 | { |
| 2222 | unsigned int to_alloc; |
| 2223 | |
| 2224 | if (memory_bm_create(bm, GFP_ATOMIC, PG_SAFE)) |
| 2225 | return -ENOMEM; |
| 2226 | |
| 2227 | if (get_highmem_buffer(PG_SAFE)) |
| 2228 | return -ENOMEM; |
| 2229 | |
| 2230 | to_alloc = count_free_highmem_pages(); |
| 2231 | if (to_alloc > *nr_highmem_p) |
| 2232 | to_alloc = *nr_highmem_p; |
| 2233 | else |
| 2234 | *nr_highmem_p = to_alloc; |
| 2235 | |
| 2236 | safe_highmem_pages = 0; |
| 2237 | while (to_alloc-- > 0) { |
| 2238 | struct page *page; |
| 2239 | |
| 2240 | page = alloc_page(__GFP_HIGHMEM); |
Rafael J. Wysocki | 7be9823 | 2007-05-06 14:50:42 -0700 | [diff] [blame] | 2241 | if (!swsusp_page_is_free(page)) { |
Rafael J. Wysocki | 8357376 | 2006-12-06 20:34:18 -0800 | [diff] [blame] | 2242 | /* The page is "safe", set its bit the bitmap */ |
| 2243 | memory_bm_set_bit(bm, page_to_pfn(page)); |
| 2244 | safe_highmem_pages++; |
| 2245 | } |
| 2246 | /* Mark the page as allocated */ |
Rafael J. Wysocki | 7be9823 | 2007-05-06 14:50:42 -0700 | [diff] [blame] | 2247 | swsusp_set_page_forbidden(page); |
| 2248 | swsusp_set_page_free(page); |
Rafael J. Wysocki | 8357376 | 2006-12-06 20:34:18 -0800 | [diff] [blame] | 2249 | } |
| 2250 | memory_bm_position_reset(bm); |
| 2251 | safe_highmem_bm = bm; |
| 2252 | return 0; |
| 2253 | } |
| 2254 | |
| 2255 | /** |
| 2256 | * get_highmem_page_buffer - for given highmem image page find the buffer |
| 2257 | * that suspend_write_next() should set for its caller to write to. |
| 2258 | * |
| 2259 | * If the page is to be saved to its "original" page frame or a copy of |
| 2260 | * the page is to be made in the highmem, @buffer is returned. Otherwise, |
| 2261 | * the copy of the page is to be made in normal memory, so the address of |
| 2262 | * the copy is returned. |
| 2263 | * |
| 2264 | * If @buffer is returned, the caller of suspend_write_next() will write |
| 2265 | * the page's contents to @buffer, so they will have to be copied to the |
| 2266 | * right location on the next call to suspend_write_next() and it is done |
| 2267 | * with the help of copy_last_highmem_page(). For this purpose, if |
| 2268 | * @buffer is returned, @last_highmem page is set to the page to which |
| 2269 | * the data will have to be copied from @buffer. |
| 2270 | */ |
| 2271 | |
| 2272 | static struct page *last_highmem_page; |
| 2273 | |
| 2274 | static void * |
| 2275 | get_highmem_page_buffer(struct page *page, struct chain_allocator *ca) |
| 2276 | { |
| 2277 | struct highmem_pbe *pbe; |
| 2278 | void *kaddr; |
| 2279 | |
Rafael J. Wysocki | 7be9823 | 2007-05-06 14:50:42 -0700 | [diff] [blame] | 2280 | if (swsusp_page_is_forbidden(page) && swsusp_page_is_free(page)) { |
Rafael J. Wysocki | 8357376 | 2006-12-06 20:34:18 -0800 | [diff] [blame] | 2281 | /* We have allocated the "original" page frame and we can |
| 2282 | * use it directly to store the loaded page. |
| 2283 | */ |
| 2284 | last_highmem_page = page; |
| 2285 | return buffer; |
| 2286 | } |
| 2287 | /* The "original" page frame has not been allocated and we have to |
| 2288 | * use a "safe" page frame to store the loaded page. |
| 2289 | */ |
| 2290 | pbe = chain_alloc(ca, sizeof(struct highmem_pbe)); |
| 2291 | if (!pbe) { |
| 2292 | swsusp_free(); |
Rafael J. Wysocki | 6964327 | 2008-11-11 21:32:44 +0100 | [diff] [blame] | 2293 | return ERR_PTR(-ENOMEM); |
Rafael J. Wysocki | 8357376 | 2006-12-06 20:34:18 -0800 | [diff] [blame] | 2294 | } |
| 2295 | pbe->orig_page = page; |
| 2296 | if (safe_highmem_pages > 0) { |
| 2297 | struct page *tmp; |
| 2298 | |
| 2299 | /* Copy of the page will be stored in high memory */ |
| 2300 | kaddr = buffer; |
| 2301 | tmp = pfn_to_page(memory_bm_next_pfn(safe_highmem_bm)); |
| 2302 | safe_highmem_pages--; |
| 2303 | last_highmem_page = tmp; |
| 2304 | pbe->copy_page = tmp; |
| 2305 | } else { |
| 2306 | /* Copy of the page will be stored in normal memory */ |
| 2307 | kaddr = safe_pages_list; |
| 2308 | safe_pages_list = safe_pages_list->next; |
| 2309 | pbe->copy_page = virt_to_page(kaddr); |
| 2310 | } |
| 2311 | pbe->next = highmem_pblist; |
| 2312 | highmem_pblist = pbe; |
| 2313 | return kaddr; |
| 2314 | } |
| 2315 | |
| 2316 | /** |
| 2317 | * copy_last_highmem_page - copy the contents of a highmem image from |
| 2318 | * @buffer, where the caller of snapshot_write_next() has place them, |
| 2319 | * to the right location represented by @last_highmem_page . |
| 2320 | */ |
| 2321 | |
| 2322 | static void copy_last_highmem_page(void) |
| 2323 | { |
| 2324 | if (last_highmem_page) { |
| 2325 | void *dst; |
| 2326 | |
Cong Wang | 0de9a1e | 2011-11-25 23:14:38 +0800 | [diff] [blame] | 2327 | dst = kmap_atomic(last_highmem_page); |
Jan Beulich | 3ecb01d | 2010-10-26 14:22:27 -0700 | [diff] [blame] | 2328 | copy_page(dst, buffer); |
Cong Wang | 0de9a1e | 2011-11-25 23:14:38 +0800 | [diff] [blame] | 2329 | kunmap_atomic(dst); |
Rafael J. Wysocki | 8357376 | 2006-12-06 20:34:18 -0800 | [diff] [blame] | 2330 | last_highmem_page = NULL; |
| 2331 | } |
| 2332 | } |
| 2333 | |
| 2334 | static inline int last_highmem_page_copied(void) |
| 2335 | { |
| 2336 | return !last_highmem_page; |
| 2337 | } |
| 2338 | |
| 2339 | static inline void free_highmem_data(void) |
| 2340 | { |
| 2341 | if (safe_highmem_bm) |
| 2342 | memory_bm_free(safe_highmem_bm, PG_UNSAFE_CLEAR); |
| 2343 | |
| 2344 | if (buffer) |
| 2345 | free_image_page(buffer, PG_UNSAFE_CLEAR); |
| 2346 | } |
| 2347 | #else |
| 2348 | static inline int get_safe_write_buffer(void) { return 0; } |
| 2349 | |
| 2350 | static unsigned int |
| 2351 | count_highmem_image_pages(struct memory_bitmap *bm) { return 0; } |
| 2352 | |
| 2353 | static inline int |
| 2354 | prepare_highmem_image(struct memory_bitmap *bm, unsigned int *nr_highmem_p) |
| 2355 | { |
| 2356 | return 0; |
| 2357 | } |
| 2358 | |
| 2359 | static inline void * |
| 2360 | get_highmem_page_buffer(struct page *page, struct chain_allocator *ca) |
| 2361 | { |
Rafael J. Wysocki | 6964327 | 2008-11-11 21:32:44 +0100 | [diff] [blame] | 2362 | return ERR_PTR(-EINVAL); |
Rafael J. Wysocki | 8357376 | 2006-12-06 20:34:18 -0800 | [diff] [blame] | 2363 | } |
| 2364 | |
| 2365 | static inline void copy_last_highmem_page(void) {} |
| 2366 | static inline int last_highmem_page_copied(void) { return 1; } |
| 2367 | static inline void free_highmem_data(void) {} |
| 2368 | #endif /* CONFIG_HIGHMEM */ |
| 2369 | |
Rafael J. Wysocki | f577eb3 | 2006-03-23 02:59:59 -0800 | [diff] [blame] | 2370 | /** |
Rafael J. Wysocki | 940864d | 2006-09-25 23:32:55 -0700 | [diff] [blame] | 2371 | * prepare_image - use the memory bitmap @bm to mark the pages that will |
| 2372 | * be overwritten in the process of restoring the system memory state |
| 2373 | * from the suspend image ("unsafe" pages) and allocate memory for the |
| 2374 | * image. |
Rafael J. Wysocki | 968808b8 | 2006-06-23 02:04:48 -0700 | [diff] [blame] | 2375 | * |
Rafael J. Wysocki | 940864d | 2006-09-25 23:32:55 -0700 | [diff] [blame] | 2376 | * The idea is to allocate a new memory bitmap first and then allocate |
| 2377 | * as many pages as needed for the image data, but not to assign these |
| 2378 | * pages to specific tasks initially. Instead, we just mark them as |
Rafael J. Wysocki | 8357376 | 2006-12-06 20:34:18 -0800 | [diff] [blame] | 2379 | * allocated and create a lists of "safe" pages that will be used |
| 2380 | * later. On systems with high memory a list of "safe" highmem pages is |
| 2381 | * also created. |
Rafael J. Wysocki | f577eb3 | 2006-03-23 02:59:59 -0800 | [diff] [blame] | 2382 | */ |
| 2383 | |
Rafael J. Wysocki | 940864d | 2006-09-25 23:32:55 -0700 | [diff] [blame] | 2384 | #define PBES_PER_LINKED_PAGE (LINKED_PAGE_DATA_SIZE / sizeof(struct pbe)) |
Rafael J. Wysocki | 968808b8 | 2006-06-23 02:04:48 -0700 | [diff] [blame] | 2385 | |
Rafael J. Wysocki | 940864d | 2006-09-25 23:32:55 -0700 | [diff] [blame] | 2386 | static int |
| 2387 | prepare_image(struct memory_bitmap *new_bm, struct memory_bitmap *bm) |
Rafael J. Wysocki | f577eb3 | 2006-03-23 02:59:59 -0800 | [diff] [blame] | 2388 | { |
Rafael J. Wysocki | 8357376 | 2006-12-06 20:34:18 -0800 | [diff] [blame] | 2389 | unsigned int nr_pages, nr_highmem; |
Rafael J. Wysocki | 940864d | 2006-09-25 23:32:55 -0700 | [diff] [blame] | 2390 | struct linked_page *sp_list, *lp; |
| 2391 | int error; |
Rafael J. Wysocki | f577eb3 | 2006-03-23 02:59:59 -0800 | [diff] [blame] | 2392 | |
Rafael J. Wysocki | 8357376 | 2006-12-06 20:34:18 -0800 | [diff] [blame] | 2393 | /* If there is no highmem, the buffer will not be necessary */ |
| 2394 | free_image_page(buffer, PG_UNSAFE_CLEAR); |
| 2395 | buffer = NULL; |
| 2396 | |
| 2397 | nr_highmem = count_highmem_image_pages(bm); |
Rafael J. Wysocki | 940864d | 2006-09-25 23:32:55 -0700 | [diff] [blame] | 2398 | error = mark_unsafe_pages(bm); |
| 2399 | if (error) |
| 2400 | goto Free; |
| 2401 | |
| 2402 | error = memory_bm_create(new_bm, GFP_ATOMIC, PG_SAFE); |
| 2403 | if (error) |
| 2404 | goto Free; |
| 2405 | |
| 2406 | duplicate_memory_bitmap(new_bm, bm); |
| 2407 | memory_bm_free(bm, PG_UNSAFE_KEEP); |
Rafael J. Wysocki | 8357376 | 2006-12-06 20:34:18 -0800 | [diff] [blame] | 2408 | if (nr_highmem > 0) { |
| 2409 | error = prepare_highmem_image(bm, &nr_highmem); |
| 2410 | if (error) |
| 2411 | goto Free; |
| 2412 | } |
Rafael J. Wysocki | 940864d | 2006-09-25 23:32:55 -0700 | [diff] [blame] | 2413 | /* Reserve some safe pages for potential later use. |
| 2414 | * |
| 2415 | * NOTE: This way we make sure there will be enough safe pages for the |
| 2416 | * chain_alloc() in get_buffer(). It is a bit wasteful, but |
| 2417 | * nr_copy_pages cannot be greater than 50% of the memory anyway. |
| 2418 | */ |
| 2419 | sp_list = NULL; |
| 2420 | /* nr_copy_pages cannot be lesser than allocated_unsafe_pages */ |
Rafael J. Wysocki | 8357376 | 2006-12-06 20:34:18 -0800 | [diff] [blame] | 2421 | nr_pages = nr_copy_pages - nr_highmem - allocated_unsafe_pages; |
Rafael J. Wysocki | 940864d | 2006-09-25 23:32:55 -0700 | [diff] [blame] | 2422 | nr_pages = DIV_ROUND_UP(nr_pages, PBES_PER_LINKED_PAGE); |
| 2423 | while (nr_pages > 0) { |
Rafael J. Wysocki | 8357376 | 2006-12-06 20:34:18 -0800 | [diff] [blame] | 2424 | lp = get_image_page(GFP_ATOMIC, PG_SAFE); |
Rafael J. Wysocki | 940864d | 2006-09-25 23:32:55 -0700 | [diff] [blame] | 2425 | if (!lp) { |
Rafael J. Wysocki | f577eb3 | 2006-03-23 02:59:59 -0800 | [diff] [blame] | 2426 | error = -ENOMEM; |
Rafael J. Wysocki | 940864d | 2006-09-25 23:32:55 -0700 | [diff] [blame] | 2427 | goto Free; |
Rafael J. Wysocki | 968808b8 | 2006-06-23 02:04:48 -0700 | [diff] [blame] | 2428 | } |
Rafael J. Wysocki | 940864d | 2006-09-25 23:32:55 -0700 | [diff] [blame] | 2429 | lp->next = sp_list; |
| 2430 | sp_list = lp; |
| 2431 | nr_pages--; |
Rafael J. Wysocki | 968808b8 | 2006-06-23 02:04:48 -0700 | [diff] [blame] | 2432 | } |
Rafael J. Wysocki | 940864d | 2006-09-25 23:32:55 -0700 | [diff] [blame] | 2433 | /* Preallocate memory for the image */ |
| 2434 | safe_pages_list = NULL; |
Rafael J. Wysocki | 8357376 | 2006-12-06 20:34:18 -0800 | [diff] [blame] | 2435 | nr_pages = nr_copy_pages - nr_highmem - allocated_unsafe_pages; |
Rafael J. Wysocki | 940864d | 2006-09-25 23:32:55 -0700 | [diff] [blame] | 2436 | while (nr_pages > 0) { |
| 2437 | lp = (struct linked_page *)get_zeroed_page(GFP_ATOMIC); |
| 2438 | if (!lp) { |
| 2439 | error = -ENOMEM; |
| 2440 | goto Free; |
| 2441 | } |
Rafael J. Wysocki | 7be9823 | 2007-05-06 14:50:42 -0700 | [diff] [blame] | 2442 | if (!swsusp_page_is_free(virt_to_page(lp))) { |
Rafael J. Wysocki | 940864d | 2006-09-25 23:32:55 -0700 | [diff] [blame] | 2443 | /* The page is "safe", add it to the list */ |
| 2444 | lp->next = safe_pages_list; |
| 2445 | safe_pages_list = lp; |
| 2446 | } |
| 2447 | /* Mark the page as allocated */ |
Rafael J. Wysocki | 7be9823 | 2007-05-06 14:50:42 -0700 | [diff] [blame] | 2448 | swsusp_set_page_forbidden(virt_to_page(lp)); |
| 2449 | swsusp_set_page_free(virt_to_page(lp)); |
Rafael J. Wysocki | 940864d | 2006-09-25 23:32:55 -0700 | [diff] [blame] | 2450 | nr_pages--; |
Rafael J. Wysocki | f577eb3 | 2006-03-23 02:59:59 -0800 | [diff] [blame] | 2451 | } |
Rafael J. Wysocki | 940864d | 2006-09-25 23:32:55 -0700 | [diff] [blame] | 2452 | /* Free the reserved safe pages so that chain_alloc() can use them */ |
| 2453 | while (sp_list) { |
| 2454 | lp = sp_list->next; |
| 2455 | free_image_page(sp_list, PG_UNSAFE_CLEAR); |
| 2456 | sp_list = lp; |
| 2457 | } |
| 2458 | return 0; |
| 2459 | |
Rafael J. Wysocki | 59a49335 | 2006-12-06 20:34:44 -0800 | [diff] [blame] | 2460 | Free: |
Rafael J. Wysocki | 940864d | 2006-09-25 23:32:55 -0700 | [diff] [blame] | 2461 | swsusp_free(); |
Rafael J. Wysocki | f577eb3 | 2006-03-23 02:59:59 -0800 | [diff] [blame] | 2462 | return error; |
| 2463 | } |
| 2464 | |
Rafael J. Wysocki | 940864d | 2006-09-25 23:32:55 -0700 | [diff] [blame] | 2465 | /** |
| 2466 | * get_buffer - compute the address that snapshot_write_next() should |
| 2467 | * set for its caller to write to. |
| 2468 | */ |
Rafael J. Wysocki | 968808b8 | 2006-06-23 02:04:48 -0700 | [diff] [blame] | 2469 | |
Rafael J. Wysocki | 940864d | 2006-09-25 23:32:55 -0700 | [diff] [blame] | 2470 | static void *get_buffer(struct memory_bitmap *bm, struct chain_allocator *ca) |
| 2471 | { |
| 2472 | struct pbe *pbe; |
Rafael J. Wysocki | 6964327 | 2008-11-11 21:32:44 +0100 | [diff] [blame] | 2473 | struct page *page; |
| 2474 | unsigned long pfn = memory_bm_next_pfn(bm); |
Rafael J. Wysocki | 940864d | 2006-09-25 23:32:55 -0700 | [diff] [blame] | 2475 | |
Rafael J. Wysocki | 6964327 | 2008-11-11 21:32:44 +0100 | [diff] [blame] | 2476 | if (pfn == BM_END_OF_MAP) |
| 2477 | return ERR_PTR(-EFAULT); |
| 2478 | |
| 2479 | page = pfn_to_page(pfn); |
Rafael J. Wysocki | 8357376 | 2006-12-06 20:34:18 -0800 | [diff] [blame] | 2480 | if (PageHighMem(page)) |
| 2481 | return get_highmem_page_buffer(page, ca); |
| 2482 | |
Rafael J. Wysocki | 7be9823 | 2007-05-06 14:50:42 -0700 | [diff] [blame] | 2483 | if (swsusp_page_is_forbidden(page) && swsusp_page_is_free(page)) |
Rafael J. Wysocki | 940864d | 2006-09-25 23:32:55 -0700 | [diff] [blame] | 2484 | /* We have allocated the "original" page frame and we can |
| 2485 | * use it directly to store the loaded page. |
Rafael J. Wysocki | 968808b8 | 2006-06-23 02:04:48 -0700 | [diff] [blame] | 2486 | */ |
Rafael J. Wysocki | 940864d | 2006-09-25 23:32:55 -0700 | [diff] [blame] | 2487 | return page_address(page); |
| 2488 | |
| 2489 | /* The "original" page frame has not been allocated and we have to |
| 2490 | * use a "safe" page frame to store the loaded page. |
Rafael J. Wysocki | 968808b8 | 2006-06-23 02:04:48 -0700 | [diff] [blame] | 2491 | */ |
Rafael J. Wysocki | 940864d | 2006-09-25 23:32:55 -0700 | [diff] [blame] | 2492 | pbe = chain_alloc(ca, sizeof(struct pbe)); |
| 2493 | if (!pbe) { |
| 2494 | swsusp_free(); |
Rafael J. Wysocki | 6964327 | 2008-11-11 21:32:44 +0100 | [diff] [blame] | 2495 | return ERR_PTR(-ENOMEM); |
Rafael J. Wysocki | 940864d | 2006-09-25 23:32:55 -0700 | [diff] [blame] | 2496 | } |
Rafael J. Wysocki | 8357376 | 2006-12-06 20:34:18 -0800 | [diff] [blame] | 2497 | pbe->orig_address = page_address(page); |
| 2498 | pbe->address = safe_pages_list; |
Rafael J. Wysocki | 940864d | 2006-09-25 23:32:55 -0700 | [diff] [blame] | 2499 | safe_pages_list = safe_pages_list->next; |
| 2500 | pbe->next = restore_pblist; |
| 2501 | restore_pblist = pbe; |
Rafael J. Wysocki | 8357376 | 2006-12-06 20:34:18 -0800 | [diff] [blame] | 2502 | return pbe->address; |
Rafael J. Wysocki | 968808b8 | 2006-06-23 02:04:48 -0700 | [diff] [blame] | 2503 | } |
| 2504 | |
Rafael J. Wysocki | f577eb3 | 2006-03-23 02:59:59 -0800 | [diff] [blame] | 2505 | /** |
| 2506 | * snapshot_write_next - used for writing the system memory snapshot. |
| 2507 | * |
| 2508 | * On the first call to it @handle should point to a zeroed |
| 2509 | * snapshot_handle structure. The structure gets updated and a pointer |
| 2510 | * to it should be passed to this function every next time. |
| 2511 | * |
Rafael J. Wysocki | f577eb3 | 2006-03-23 02:59:59 -0800 | [diff] [blame] | 2512 | * On success the function returns a positive number. Then, the caller |
| 2513 | * is allowed to write up to the returned number of bytes to the memory |
Jiri Slaby | d3c1b24 | 2010-05-01 23:52:02 +0200 | [diff] [blame] | 2514 | * location computed by the data_of() macro. |
Rafael J. Wysocki | f577eb3 | 2006-03-23 02:59:59 -0800 | [diff] [blame] | 2515 | * |
| 2516 | * The function returns 0 to indicate the "end of file" condition, |
| 2517 | * and a negative number is returned on error. In such cases the |
| 2518 | * structure pointed to by @handle is not updated and should not be used |
| 2519 | * any more. |
| 2520 | */ |
| 2521 | |
Jiri Slaby | d3c1b24 | 2010-05-01 23:52:02 +0200 | [diff] [blame] | 2522 | int snapshot_write_next(struct snapshot_handle *handle) |
Rafael J. Wysocki | f577eb3 | 2006-03-23 02:59:59 -0800 | [diff] [blame] | 2523 | { |
Rafael J. Wysocki | 940864d | 2006-09-25 23:32:55 -0700 | [diff] [blame] | 2524 | static struct chain_allocator ca; |
Rafael J. Wysocki | f577eb3 | 2006-03-23 02:59:59 -0800 | [diff] [blame] | 2525 | int error = 0; |
| 2526 | |
Rafael J. Wysocki | 940864d | 2006-09-25 23:32:55 -0700 | [diff] [blame] | 2527 | /* Check if we have already loaded the entire image */ |
Jiri Slaby | d3c1b24 | 2010-05-01 23:52:02 +0200 | [diff] [blame] | 2528 | if (handle->cur > 1 && handle->cur > nr_meta_pages + nr_copy_pages) |
Rafael J. Wysocki | f577eb3 | 2006-03-23 02:59:59 -0800 | [diff] [blame] | 2529 | return 0; |
Rafael J. Wysocki | 940864d | 2006-09-25 23:32:55 -0700 | [diff] [blame] | 2530 | |
Jiri Slaby | d3c1b24 | 2010-05-01 23:52:02 +0200 | [diff] [blame] | 2531 | handle->sync_read = 1; |
| 2532 | |
| 2533 | if (!handle->cur) { |
Rafael J. Wysocki | 8357376 | 2006-12-06 20:34:18 -0800 | [diff] [blame] | 2534 | if (!buffer) |
| 2535 | /* This makes the buffer be freed by swsusp_free() */ |
| 2536 | buffer = get_image_page(GFP_ATOMIC, PG_ANY); |
| 2537 | |
Rafael J. Wysocki | f577eb3 | 2006-03-23 02:59:59 -0800 | [diff] [blame] | 2538 | if (!buffer) |
| 2539 | return -ENOMEM; |
Rafael J. Wysocki | 8357376 | 2006-12-06 20:34:18 -0800 | [diff] [blame] | 2540 | |
Rafael J. Wysocki | f577eb3 | 2006-03-23 02:59:59 -0800 | [diff] [blame] | 2541 | handle->buffer = buffer; |
Jiri Slaby | d3c1b24 | 2010-05-01 23:52:02 +0200 | [diff] [blame] | 2542 | } else if (handle->cur == 1) { |
| 2543 | error = load_header(buffer); |
| 2544 | if (error) |
| 2545 | return error; |
| 2546 | |
| 2547 | error = memory_bm_create(©_bm, GFP_ATOMIC, PG_ANY); |
| 2548 | if (error) |
| 2549 | return error; |
| 2550 | |
Martin Schwidefsky | 85055dd | 2011-08-17 20:42:24 +0200 | [diff] [blame] | 2551 | /* Allocate buffer for page keys. */ |
| 2552 | error = page_key_alloc(nr_copy_pages); |
| 2553 | if (error) |
| 2554 | return error; |
| 2555 | |
Jiri Slaby | d3c1b24 | 2010-05-01 23:52:02 +0200 | [diff] [blame] | 2556 | } else if (handle->cur <= nr_meta_pages + 1) { |
| 2557 | error = unpack_orig_pfns(buffer, ©_bm); |
| 2558 | if (error) |
| 2559 | return error; |
| 2560 | |
| 2561 | if (handle->cur == nr_meta_pages + 1) { |
| 2562 | error = prepare_image(&orig_bm, ©_bm); |
Rafael J. Wysocki | f577eb3 | 2006-03-23 02:59:59 -0800 | [diff] [blame] | 2563 | if (error) |
| 2564 | return error; |
Rafael J. Wysocki | 940864d | 2006-09-25 23:32:55 -0700 | [diff] [blame] | 2565 | |
Jiri Slaby | d3c1b24 | 2010-05-01 23:52:02 +0200 | [diff] [blame] | 2566 | chain_init(&ca, GFP_ATOMIC, PG_SAFE); |
| 2567 | memory_bm_position_reset(&orig_bm); |
| 2568 | restore_pblist = NULL; |
Rafael J. Wysocki | 940864d | 2006-09-25 23:32:55 -0700 | [diff] [blame] | 2569 | handle->buffer = get_buffer(&orig_bm, &ca); |
Jiri Slaby | d3c1b24 | 2010-05-01 23:52:02 +0200 | [diff] [blame] | 2570 | handle->sync_read = 0; |
Rafael J. Wysocki | 6964327 | 2008-11-11 21:32:44 +0100 | [diff] [blame] | 2571 | if (IS_ERR(handle->buffer)) |
| 2572 | return PTR_ERR(handle->buffer); |
Rafael J. Wysocki | f577eb3 | 2006-03-23 02:59:59 -0800 | [diff] [blame] | 2573 | } |
Rafael J. Wysocki | f577eb3 | 2006-03-23 02:59:59 -0800 | [diff] [blame] | 2574 | } else { |
Jiri Slaby | d3c1b24 | 2010-05-01 23:52:02 +0200 | [diff] [blame] | 2575 | copy_last_highmem_page(); |
Martin Schwidefsky | 85055dd | 2011-08-17 20:42:24 +0200 | [diff] [blame] | 2576 | /* Restore page key for data page (s390 only). */ |
| 2577 | page_key_write(handle->buffer); |
Jiri Slaby | d3c1b24 | 2010-05-01 23:52:02 +0200 | [diff] [blame] | 2578 | handle->buffer = get_buffer(&orig_bm, &ca); |
| 2579 | if (IS_ERR(handle->buffer)) |
| 2580 | return PTR_ERR(handle->buffer); |
| 2581 | if (handle->buffer != buffer) |
| 2582 | handle->sync_read = 0; |
Rafael J. Wysocki | f577eb3 | 2006-03-23 02:59:59 -0800 | [diff] [blame] | 2583 | } |
Jiri Slaby | d3c1b24 | 2010-05-01 23:52:02 +0200 | [diff] [blame] | 2584 | handle->cur++; |
| 2585 | return PAGE_SIZE; |
Rafael J. Wysocki | f577eb3 | 2006-03-23 02:59:59 -0800 | [diff] [blame] | 2586 | } |
| 2587 | |
Rafael J. Wysocki | 8357376 | 2006-12-06 20:34:18 -0800 | [diff] [blame] | 2588 | /** |
| 2589 | * snapshot_write_finalize - must be called after the last call to |
| 2590 | * snapshot_write_next() in case the last page in the image happens |
| 2591 | * to be a highmem page and its contents should be stored in the |
| 2592 | * highmem. Additionally, it releases the memory that will not be |
| 2593 | * used any more. |
| 2594 | */ |
| 2595 | |
| 2596 | void snapshot_write_finalize(struct snapshot_handle *handle) |
| 2597 | { |
| 2598 | copy_last_highmem_page(); |
Martin Schwidefsky | 85055dd | 2011-08-17 20:42:24 +0200 | [diff] [blame] | 2599 | /* Restore page key for data page (s390 only). */ |
| 2600 | page_key_write(handle->buffer); |
| 2601 | page_key_free(); |
Rafael J. Wysocki | 8357376 | 2006-12-06 20:34:18 -0800 | [diff] [blame] | 2602 | /* Free only if we have loaded the image entirely */ |
Jiri Slaby | d3c1b24 | 2010-05-01 23:52:02 +0200 | [diff] [blame] | 2603 | if (handle->cur > 1 && handle->cur > nr_meta_pages + nr_copy_pages) { |
Rafael J. Wysocki | 8357376 | 2006-12-06 20:34:18 -0800 | [diff] [blame] | 2604 | memory_bm_free(&orig_bm, PG_UNSAFE_CLEAR); |
| 2605 | free_highmem_data(); |
| 2606 | } |
| 2607 | } |
| 2608 | |
Rafael J. Wysocki | f577eb3 | 2006-03-23 02:59:59 -0800 | [diff] [blame] | 2609 | int snapshot_image_loaded(struct snapshot_handle *handle) |
| 2610 | { |
Rafael J. Wysocki | 8357376 | 2006-12-06 20:34:18 -0800 | [diff] [blame] | 2611 | return !(!nr_copy_pages || !last_highmem_page_copied() || |
Rafael J. Wysocki | 940864d | 2006-09-25 23:32:55 -0700 | [diff] [blame] | 2612 | handle->cur <= nr_meta_pages + nr_copy_pages); |
| 2613 | } |
| 2614 | |
Rafael J. Wysocki | 8357376 | 2006-12-06 20:34:18 -0800 | [diff] [blame] | 2615 | #ifdef CONFIG_HIGHMEM |
| 2616 | /* Assumes that @buf is ready and points to a "safe" page */ |
| 2617 | static inline void |
| 2618 | swap_two_pages_data(struct page *p1, struct page *p2, void *buf) |
Rafael J. Wysocki | 940864d | 2006-09-25 23:32:55 -0700 | [diff] [blame] | 2619 | { |
Rafael J. Wysocki | 8357376 | 2006-12-06 20:34:18 -0800 | [diff] [blame] | 2620 | void *kaddr1, *kaddr2; |
| 2621 | |
Cong Wang | 0de9a1e | 2011-11-25 23:14:38 +0800 | [diff] [blame] | 2622 | kaddr1 = kmap_atomic(p1); |
| 2623 | kaddr2 = kmap_atomic(p2); |
Jan Beulich | 3ecb01d | 2010-10-26 14:22:27 -0700 | [diff] [blame] | 2624 | copy_page(buf, kaddr1); |
| 2625 | copy_page(kaddr1, kaddr2); |
| 2626 | copy_page(kaddr2, buf); |
Cong Wang | 0de9a1e | 2011-11-25 23:14:38 +0800 | [diff] [blame] | 2627 | kunmap_atomic(kaddr2); |
| 2628 | kunmap_atomic(kaddr1); |
Rafael J. Wysocki | f577eb3 | 2006-03-23 02:59:59 -0800 | [diff] [blame] | 2629 | } |
Rafael J. Wysocki | 8357376 | 2006-12-06 20:34:18 -0800 | [diff] [blame] | 2630 | |
| 2631 | /** |
| 2632 | * restore_highmem - for each highmem page that was allocated before |
| 2633 | * the suspend and included in the suspend image, and also has been |
| 2634 | * allocated by the "resume" kernel swap its current (ie. "before |
| 2635 | * resume") contents with the previous (ie. "before suspend") one. |
| 2636 | * |
| 2637 | * If the resume eventually fails, we can call this function once |
| 2638 | * again and restore the "before resume" highmem state. |
| 2639 | */ |
| 2640 | |
| 2641 | int restore_highmem(void) |
| 2642 | { |
| 2643 | struct highmem_pbe *pbe = highmem_pblist; |
| 2644 | void *buf; |
| 2645 | |
| 2646 | if (!pbe) |
| 2647 | return 0; |
| 2648 | |
| 2649 | buf = get_image_page(GFP_ATOMIC, PG_SAFE); |
| 2650 | if (!buf) |
| 2651 | return -ENOMEM; |
| 2652 | |
| 2653 | while (pbe) { |
| 2654 | swap_two_pages_data(pbe->copy_page, pbe->orig_page, buf); |
| 2655 | pbe = pbe->next; |
| 2656 | } |
| 2657 | free_image_page(buf, PG_UNSAFE_CLEAR); |
| 2658 | return 0; |
| 2659 | } |
| 2660 | #endif /* CONFIG_HIGHMEM */ |