diff options
Diffstat (limited to 'kernel/power/snapshot.c')
| -rw-r--r-- | kernel/power/snapshot.c | 1241 |
1 files changed, 795 insertions, 446 deletions
diff --git a/kernel/power/snapshot.c b/kernel/power/snapshot.c index 3d9284100b22..1b84313cbab5 100644 --- a/kernel/power/snapshot.c +++ b/kernel/power/snapshot.c | |||
| @@ -34,95 +34,15 @@ | |||
| 34 | 34 | ||
| 35 | #include "power.h" | 35 | #include "power.h" |
| 36 | 36 | ||
| 37 | struct pbe *pagedir_nosave; | 37 | /* List of PBEs used for creating and restoring the suspend image */ |
| 38 | struct pbe *restore_pblist; | ||
| 39 | |||
| 38 | static unsigned int nr_copy_pages; | 40 | static unsigned int nr_copy_pages; |
| 39 | static unsigned int nr_meta_pages; | 41 | static unsigned int nr_meta_pages; |
| 40 | static unsigned long *buffer; | 42 | static void *buffer; |
| 41 | |||
| 42 | struct arch_saveable_page { | ||
| 43 | unsigned long start; | ||
| 44 | unsigned long end; | ||
| 45 | char *data; | ||
| 46 | struct arch_saveable_page *next; | ||
| 47 | }; | ||
| 48 | static struct arch_saveable_page *arch_pages; | ||
| 49 | |||
| 50 | int swsusp_add_arch_pages(unsigned long start, unsigned long end) | ||
| 51 | { | ||
| 52 | struct arch_saveable_page *tmp; | ||
| 53 | |||
| 54 | while (start < end) { | ||
| 55 | tmp = kzalloc(sizeof(struct arch_saveable_page), GFP_KERNEL); | ||
| 56 | if (!tmp) | ||
| 57 | return -ENOMEM; | ||
| 58 | tmp->start = start; | ||
| 59 | tmp->end = ((start >> PAGE_SHIFT) + 1) << PAGE_SHIFT; | ||
| 60 | if (tmp->end > end) | ||
| 61 | tmp->end = end; | ||
| 62 | tmp->next = arch_pages; | ||
| 63 | start = tmp->end; | ||
| 64 | arch_pages = tmp; | ||
| 65 | } | ||
| 66 | return 0; | ||
| 67 | } | ||
| 68 | |||
| 69 | static unsigned int count_arch_pages(void) | ||
| 70 | { | ||
| 71 | unsigned int count = 0; | ||
| 72 | struct arch_saveable_page *tmp = arch_pages; | ||
| 73 | while (tmp) { | ||
| 74 | count++; | ||
| 75 | tmp = tmp->next; | ||
| 76 | } | ||
| 77 | return count; | ||
| 78 | } | ||
| 79 | |||
| 80 | static int save_arch_mem(void) | ||
| 81 | { | ||
| 82 | char *kaddr; | ||
| 83 | struct arch_saveable_page *tmp = arch_pages; | ||
| 84 | int offset; | ||
| 85 | |||
| 86 | pr_debug("swsusp: Saving arch specific memory"); | ||
| 87 | while (tmp) { | ||
| 88 | tmp->data = (char *)__get_free_page(GFP_ATOMIC); | ||
| 89 | if (!tmp->data) | ||
| 90 | return -ENOMEM; | ||
| 91 | offset = tmp->start - (tmp->start & PAGE_MASK); | ||
| 92 | /* arch pages might haven't a 'struct page' */ | ||
| 93 | kaddr = kmap_atomic_pfn(tmp->start >> PAGE_SHIFT, KM_USER0); | ||
| 94 | memcpy(tmp->data + offset, kaddr + offset, | ||
| 95 | tmp->end - tmp->start); | ||
| 96 | kunmap_atomic(kaddr, KM_USER0); | ||
| 97 | |||
| 98 | tmp = tmp->next; | ||
| 99 | } | ||
| 100 | return 0; | ||
| 101 | } | ||
| 102 | |||
| 103 | static int restore_arch_mem(void) | ||
| 104 | { | ||
| 105 | char *kaddr; | ||
| 106 | struct arch_saveable_page *tmp = arch_pages; | ||
| 107 | int offset; | ||
| 108 | |||
| 109 | while (tmp) { | ||
| 110 | if (!tmp->data) | ||
| 111 | continue; | ||
| 112 | offset = tmp->start - (tmp->start & PAGE_MASK); | ||
| 113 | kaddr = kmap_atomic_pfn(tmp->start >> PAGE_SHIFT, KM_USER0); | ||
| 114 | memcpy(kaddr + offset, tmp->data + offset, | ||
| 115 | tmp->end - tmp->start); | ||
| 116 | kunmap_atomic(kaddr, KM_USER0); | ||
| 117 | free_page((long)tmp->data); | ||
| 118 | tmp->data = NULL; | ||
| 119 | tmp = tmp->next; | ||
| 120 | } | ||
| 121 | return 0; | ||
| 122 | } | ||
| 123 | 43 | ||
| 124 | #ifdef CONFIG_HIGHMEM | 44 | #ifdef CONFIG_HIGHMEM |
| 125 | static unsigned int count_highmem_pages(void) | 45 | unsigned int count_highmem_pages(void) |
| 126 | { | 46 | { |
| 127 | struct zone *zone; | 47 | struct zone *zone; |
| 128 | unsigned long zone_pfn; | 48 | unsigned long zone_pfn; |
| @@ -199,7 +119,7 @@ static int save_highmem_zone(struct zone *zone) | |||
| 199 | return 0; | 119 | return 0; |
| 200 | } | 120 | } |
| 201 | 121 | ||
| 202 | static int save_highmem(void) | 122 | int save_highmem(void) |
| 203 | { | 123 | { |
| 204 | struct zone *zone; | 124 | struct zone *zone; |
| 205 | int res = 0; | 125 | int res = 0; |
| @@ -216,7 +136,7 @@ static int save_highmem(void) | |||
| 216 | return 0; | 136 | return 0; |
| 217 | } | 137 | } |
| 218 | 138 | ||
| 219 | static int restore_highmem(void) | 139 | int restore_highmem(void) |
| 220 | { | 140 | { |
| 221 | printk("swsusp: Restoring Highmem\n"); | 141 | printk("swsusp: Restoring Highmem\n"); |
| 222 | while (highmem_copy) { | 142 | while (highmem_copy) { |
| @@ -238,256 +158,637 @@ static inline int save_highmem(void) {return 0;} | |||
| 238 | static inline int restore_highmem(void) {return 0;} | 158 | static inline int restore_highmem(void) {return 0;} |
| 239 | #endif | 159 | #endif |
| 240 | 160 | ||
| 241 | unsigned int count_special_pages(void) | 161 | /** |
| 162 | * @safe_needed - on resume, for storing the PBE list and the image, | ||
| 163 | * we can only use memory pages that do not conflict with the pages | ||
| 164 | * used before suspend. | ||
| 165 | * | ||
| 166 | * The unsafe pages are marked with the PG_nosave_free flag | ||
| 167 | * and we count them using unsafe_pages | ||
| 168 | */ | ||
| 169 | |||
| 170 | #define PG_ANY 0 | ||
| 171 | #define PG_SAFE 1 | ||
| 172 | #define PG_UNSAFE_CLEAR 1 | ||
| 173 | #define PG_UNSAFE_KEEP 0 | ||
| 174 | |||
| 175 | static unsigned int allocated_unsafe_pages; | ||
| 176 | |||
| 177 | static void *alloc_image_page(gfp_t gfp_mask, int safe_needed) | ||
| 242 | { | 178 | { |
| 243 | return count_arch_pages() + count_highmem_pages(); | 179 | void *res; |
| 180 | |||
| 181 | res = (void *)get_zeroed_page(gfp_mask); | ||
| 182 | if (safe_needed) | ||
| 183 | while (res && PageNosaveFree(virt_to_page(res))) { | ||
| 184 | /* The page is unsafe, mark it for swsusp_free() */ | ||
| 185 | SetPageNosave(virt_to_page(res)); | ||
| 186 | allocated_unsafe_pages++; | ||
| 187 | res = (void *)get_zeroed_page(gfp_mask); | ||
| 188 | } | ||
| 189 | if (res) { | ||
| 190 | SetPageNosave(virt_to_page(res)); | ||
| 191 | SetPageNosaveFree(virt_to_page(res)); | ||
| 192 | } | ||
| 193 | return res; | ||
| 244 | } | 194 | } |
| 245 | 195 | ||
| 246 | int save_special_mem(void) | 196 | unsigned long get_safe_page(gfp_t gfp_mask) |
| 247 | { | 197 | { |
| 248 | int ret; | 198 | return (unsigned long)alloc_image_page(gfp_mask, PG_SAFE); |
| 249 | ret = save_arch_mem(); | 199 | } |
| 250 | if (!ret) | 200 | |
| 251 | ret = save_highmem(); | 201 | /** |
| 252 | return ret; | 202 | * free_image_page - free page represented by @addr, allocated with |
| 203 | * alloc_image_page (page flags set by it must be cleared) | ||
| 204 | */ | ||
| 205 | |||
| 206 | static inline void free_image_page(void *addr, int clear_nosave_free) | ||
| 207 | { | ||
| 208 | ClearPageNosave(virt_to_page(addr)); | ||
| 209 | if (clear_nosave_free) | ||
| 210 | ClearPageNosaveFree(virt_to_page(addr)); | ||
| 211 | free_page((unsigned long)addr); | ||
| 212 | } | ||
| 213 | |||
| 214 | /* struct linked_page is used to build chains of pages */ | ||
| 215 | |||
| 216 | #define LINKED_PAGE_DATA_SIZE (PAGE_SIZE - sizeof(void *)) | ||
| 217 | |||
| 218 | struct linked_page { | ||
| 219 | struct linked_page *next; | ||
| 220 | char data[LINKED_PAGE_DATA_SIZE]; | ||
| 221 | } __attribute__((packed)); | ||
| 222 | |||
| 223 | static inline void | ||
| 224 | free_list_of_pages(struct linked_page *list, int clear_page_nosave) | ||
| 225 | { | ||
| 226 | while (list) { | ||
| 227 | struct linked_page *lp = list->next; | ||
| 228 | |||
| 229 | free_image_page(list, clear_page_nosave); | ||
| 230 | list = lp; | ||
| 231 | } | ||
| 232 | } | ||
| 233 | |||
| 234 | /** | ||
| 235 | * struct chain_allocator is used for allocating small objects out of | ||
| 236 | * a linked list of pages called 'the chain'. | ||
| 237 | * | ||
| 238 | * The chain grows each time when there is no room for a new object in | ||
| 239 | * the current page. The allocated objects cannot be freed individually. | ||
| 240 | * It is only possible to free them all at once, by freeing the entire | ||
| 241 | * chain. | ||
| 242 | * | ||
| 243 | * NOTE: The chain allocator may be inefficient if the allocated objects | ||
| 244 | * are not much smaller than PAGE_SIZE. | ||
| 245 | */ | ||
| 246 | |||
| 247 | struct chain_allocator { | ||
| 248 | struct linked_page *chain; /* the chain */ | ||
| 249 | unsigned int used_space; /* total size of objects allocated out | ||
| 250 | * of the current page | ||
| 251 | */ | ||
| 252 | gfp_t gfp_mask; /* mask for allocating pages */ | ||
| 253 | int safe_needed; /* if set, only "safe" pages are allocated */ | ||
| 254 | }; | ||
| 255 | |||
| 256 | static void | ||
| 257 | chain_init(struct chain_allocator *ca, gfp_t gfp_mask, int safe_needed) | ||
| 258 | { | ||
| 259 | ca->chain = NULL; | ||
| 260 | ca->used_space = LINKED_PAGE_DATA_SIZE; | ||
| 261 | ca->gfp_mask = gfp_mask; | ||
| 262 | ca->safe_needed = safe_needed; | ||
| 253 | } | 263 | } |
| 254 | 264 | ||
| 255 | int restore_special_mem(void) | 265 | static void *chain_alloc(struct chain_allocator *ca, unsigned int size) |
| 256 | { | 266 | { |
| 257 | int ret; | 267 | void *ret; |
| 258 | ret = restore_arch_mem(); | 268 | |
| 259 | if (!ret) | 269 | if (LINKED_PAGE_DATA_SIZE - ca->used_space < size) { |
| 260 | ret = restore_highmem(); | 270 | struct linked_page *lp; |
| 271 | |||
| 272 | lp = alloc_image_page(ca->gfp_mask, ca->safe_needed); | ||
| 273 | if (!lp) | ||
| 274 | return NULL; | ||
| 275 | |||
| 276 | lp->next = ca->chain; | ||
| 277 | ca->chain = lp; | ||
| 278 | ca->used_space = 0; | ||
| 279 | } | ||
| 280 | ret = ca->chain->data + ca->used_space; | ||
| 281 | ca->used_space += size; | ||
| 261 | return ret; | 282 | return ret; |
| 262 | } | 283 | } |
| 263 | 284 | ||
| 264 | static int pfn_is_nosave(unsigned long pfn) | 285 | static void chain_free(struct chain_allocator *ca, int clear_page_nosave) |
| 265 | { | 286 | { |
| 266 | unsigned long nosave_begin_pfn = __pa(&__nosave_begin) >> PAGE_SHIFT; | 287 | free_list_of_pages(ca->chain, clear_page_nosave); |
| 267 | unsigned long nosave_end_pfn = PAGE_ALIGN(__pa(&__nosave_end)) >> PAGE_SHIFT; | 288 | memset(ca, 0, sizeof(struct chain_allocator)); |
| 268 | return (pfn >= nosave_begin_pfn) && (pfn < nosave_end_pfn); | ||
| 269 | } | 289 | } |
| 270 | 290 | ||
| 271 | /** | 291 | /** |
| 272 | * saveable - Determine whether a page should be cloned or not. | 292 | * Data types related to memory bitmaps. |
| 273 | * @pfn: The page | 293 | * |
| 294 | * Memory bitmap is a structure consiting of many linked lists of | ||
| 295 | * objects. The main list's elements are of type struct zone_bitmap | ||
| 296 | * and each of them corresonds to one zone. For each zone bitmap | ||
| 297 | * object there is a list of objects of type struct bm_block that | ||
| 298 | * represent each blocks of bit chunks in which information is | ||
| 299 | * stored. | ||
| 300 | * | ||
| 301 | * struct memory_bitmap contains a pointer to the main list of zone | ||
| 302 | * bitmap objects, a struct bm_position used for browsing the bitmap, | ||
| 303 | * and a pointer to the list of pages used for allocating all of the | ||
| 304 | * zone bitmap objects and bitmap block objects. | ||
| 305 | * | ||
| 306 | * NOTE: It has to be possible to lay out the bitmap in memory | ||
| 307 | * using only allocations of order 0. Additionally, the bitmap is | ||
| 308 | * designed to work with arbitrary number of zones (this is over the | ||
| 309 | * top for now, but let's avoid making unnecessary assumptions ;-). | ||
| 274 | * | 310 | * |
| 275 | * We save a page if it's Reserved, and not in the range of pages | 311 | * struct zone_bitmap contains a pointer to a list of bitmap block |
| 276 | * statically defined as 'unsaveable', or if it isn't reserved, and | 312 | * objects and a pointer to the bitmap block object that has been |
| 277 | * isn't part of a free chunk of pages. | 313 | * most recently used for setting bits. Additionally, it contains the |
| 314 | * pfns that correspond to the start and end of the represented zone. | ||
| 315 | * | ||
| 316 | * struct bm_block contains a pointer to the memory page in which | ||
| 317 | * information is stored (in the form of a block of bit chunks | ||
| 318 | * of type unsigned long each). It also contains the pfns that | ||
| 319 | * correspond to the start and end of the represented memory area and | ||
| 320 | * the number of bit chunks in the block. | ||
| 321 | * | ||
| 322 | * NOTE: Memory bitmaps are used for two types of operations only: | ||
| 323 | * "set a bit" and "find the next bit set". Moreover, the searching | ||
| 324 | * is always carried out after all of the "set a bit" operations | ||
| 325 | * on given bitmap. | ||
| 278 | */ | 326 | */ |
| 279 | 327 | ||
| 280 | static int saveable(struct zone *zone, unsigned long *zone_pfn) | 328 | #define BM_END_OF_MAP (~0UL) |
| 329 | |||
| 330 | #define BM_CHUNKS_PER_BLOCK (PAGE_SIZE / sizeof(long)) | ||
| 331 | #define BM_BITS_PER_CHUNK (sizeof(long) << 3) | ||
| 332 | #define BM_BITS_PER_BLOCK (PAGE_SIZE << 3) | ||
| 333 | |||
| 334 | struct bm_block { | ||
| 335 | struct bm_block *next; /* next element of the list */ | ||
| 336 | unsigned long start_pfn; /* pfn represented by the first bit */ | ||
| 337 | unsigned long end_pfn; /* pfn represented by the last bit plus 1 */ | ||
| 338 | unsigned int size; /* number of bit chunks */ | ||
| 339 | unsigned long *data; /* chunks of bits representing pages */ | ||
| 340 | }; | ||
| 341 | |||
| 342 | struct zone_bitmap { | ||
| 343 | struct zone_bitmap *next; /* next element of the list */ | ||
| 344 | unsigned long start_pfn; /* minimal pfn in this zone */ | ||
| 345 | unsigned long end_pfn; /* maximal pfn in this zone plus 1 */ | ||
| 346 | struct bm_block *bm_blocks; /* list of bitmap blocks */ | ||
| 347 | struct bm_block *cur_block; /* recently used bitmap block */ | ||
| 348 | }; | ||
| 349 | |||
| 350 | /* strcut bm_position is used for browsing memory bitmaps */ | ||
| 351 | |||
| 352 | struct bm_position { | ||
| 353 | struct zone_bitmap *zone_bm; | ||
| 354 | struct bm_block *block; | ||
| 355 | int chunk; | ||
| 356 | int bit; | ||
| 357 | }; | ||
| 358 | |||
| 359 | struct memory_bitmap { | ||
| 360 | struct zone_bitmap *zone_bm_list; /* list of zone bitmaps */ | ||
| 361 | struct linked_page *p_list; /* list of pages used to store zone | ||
| 362 | * bitmap objects and bitmap block | ||
| 363 | * objects | ||
| 364 | */ | ||
| 365 | struct bm_position cur; /* most recently used bit position */ | ||
| 366 | }; | ||
| 367 | |||
| 368 | /* Functions that operate on memory bitmaps */ | ||
| 369 | |||
| 370 | static inline void memory_bm_reset_chunk(struct memory_bitmap *bm) | ||
| 281 | { | 371 | { |
| 282 | unsigned long pfn = *zone_pfn + zone->zone_start_pfn; | 372 | bm->cur.chunk = 0; |
| 283 | struct page *page; | 373 | bm->cur.bit = -1; |
| 374 | } | ||
| 284 | 375 | ||
| 285 | if (!pfn_valid(pfn)) | 376 | static void memory_bm_position_reset(struct memory_bitmap *bm) |
| 286 | return 0; | 377 | { |
| 378 | struct zone_bitmap *zone_bm; | ||
| 287 | 379 | ||
| 288 | page = pfn_to_page(pfn); | 380 | zone_bm = bm->zone_bm_list; |
| 289 | if (PageNosave(page)) | 381 | bm->cur.zone_bm = zone_bm; |
| 290 | return 0; | 382 | bm->cur.block = zone_bm->bm_blocks; |
| 291 | if (PageReserved(page) && pfn_is_nosave(pfn)) | 383 | memory_bm_reset_chunk(bm); |
| 292 | return 0; | 384 | } |
| 293 | if (PageNosaveFree(page)) | 385 | |
| 294 | return 0; | 386 | static void memory_bm_free(struct memory_bitmap *bm, int clear_nosave_free); |
| 295 | 387 | ||
| 296 | return 1; | 388 | /** |
| 389 | * create_bm_block_list - create a list of block bitmap objects | ||
| 390 | */ | ||
| 391 | |||
| 392 | static inline struct bm_block * | ||
| 393 | create_bm_block_list(unsigned int nr_blocks, struct chain_allocator *ca) | ||
| 394 | { | ||
| 395 | struct bm_block *bblist = NULL; | ||
| 396 | |||
| 397 | while (nr_blocks-- > 0) { | ||
| 398 | struct bm_block *bb; | ||
| 399 | |||
| 400 | bb = chain_alloc(ca, sizeof(struct bm_block)); | ||
| 401 | if (!bb) | ||
| 402 | return NULL; | ||
| 403 | |||
| 404 | bb->next = bblist; | ||
| 405 | bblist = bb; | ||
| 406 | } | ||
| 407 | return bblist; | ||
| 297 | } | 408 | } |
| 298 | 409 | ||
| 299 | unsigned int count_data_pages(void) | 410 | /** |
| 411 | * create_zone_bm_list - create a list of zone bitmap objects | ||
| 412 | */ | ||
| 413 | |||
| 414 | static inline struct zone_bitmap * | ||
| 415 | create_zone_bm_list(unsigned int nr_zones, struct chain_allocator *ca) | ||
| 300 | { | 416 | { |
| 301 | struct zone *zone; | 417 | struct zone_bitmap *zbmlist = NULL; |
| 302 | unsigned long zone_pfn; | ||
| 303 | unsigned int n = 0; | ||
| 304 | 418 | ||
| 305 | for_each_zone (zone) { | 419 | while (nr_zones-- > 0) { |
| 306 | if (is_highmem(zone)) | 420 | struct zone_bitmap *zbm; |
| 307 | continue; | 421 | |
| 308 | mark_free_pages(zone); | 422 | zbm = chain_alloc(ca, sizeof(struct zone_bitmap)); |
| 309 | for (zone_pfn = 0; zone_pfn < zone->spanned_pages; ++zone_pfn) | 423 | if (!zbm) |
| 310 | n += saveable(zone, &zone_pfn); | 424 | return NULL; |
| 425 | |||
| 426 | zbm->next = zbmlist; | ||
| 427 | zbmlist = zbm; | ||
| 311 | } | 428 | } |
| 312 | return n; | 429 | return zbmlist; |
| 313 | } | 430 | } |
| 314 | 431 | ||
| 315 | static void copy_data_pages(struct pbe *pblist) | 432 | /** |
| 433 | * memory_bm_create - allocate memory for a memory bitmap | ||
| 434 | */ | ||
| 435 | |||
| 436 | static int | ||
| 437 | memory_bm_create(struct memory_bitmap *bm, gfp_t gfp_mask, int safe_needed) | ||
| 316 | { | 438 | { |
| 439 | struct chain_allocator ca; | ||
| 317 | struct zone *zone; | 440 | struct zone *zone; |
| 318 | unsigned long zone_pfn; | 441 | struct zone_bitmap *zone_bm; |
| 319 | struct pbe *pbe, *p; | 442 | struct bm_block *bb; |
| 443 | unsigned int nr; | ||
| 444 | |||
| 445 | chain_init(&ca, gfp_mask, safe_needed); | ||
| 446 | |||
| 447 | /* Compute the number of zones */ | ||
| 448 | nr = 0; | ||
| 449 | for_each_zone (zone) | ||
| 450 | if (populated_zone(zone) && !is_highmem(zone)) | ||
| 451 | nr++; | ||
| 452 | |||
| 453 | /* Allocate the list of zones bitmap objects */ | ||
| 454 | zone_bm = create_zone_bm_list(nr, &ca); | ||
| 455 | bm->zone_bm_list = zone_bm; | ||
| 456 | if (!zone_bm) { | ||
| 457 | chain_free(&ca, PG_UNSAFE_CLEAR); | ||
| 458 | return -ENOMEM; | ||
| 459 | } | ||
| 320 | 460 | ||
| 321 | pbe = pblist; | 461 | /* Initialize the zone bitmap objects */ |
| 322 | for_each_zone (zone) { | 462 | for_each_zone (zone) { |
| 323 | if (is_highmem(zone)) | 463 | unsigned long pfn; |
| 464 | |||
| 465 | if (!populated_zone(zone) || is_highmem(zone)) | ||
| 324 | continue; | 466 | continue; |
| 325 | mark_free_pages(zone); | 467 | |
| 326 | /* This is necessary for swsusp_free() */ | 468 | zone_bm->start_pfn = zone->zone_start_pfn; |
| 327 | for_each_pb_page (p, pblist) | 469 | zone_bm->end_pfn = zone->zone_start_pfn + zone->spanned_pages; |
| 328 | SetPageNosaveFree(virt_to_page(p)); | 470 | /* Allocate the list of bitmap block objects */ |
| 329 | for_each_pbe (p, pblist) | 471 | nr = DIV_ROUND_UP(zone->spanned_pages, BM_BITS_PER_BLOCK); |
| 330 | SetPageNosaveFree(virt_to_page(p->address)); | 472 | bb = create_bm_block_list(nr, &ca); |
| 331 | for (zone_pfn = 0; zone_pfn < zone->spanned_pages; ++zone_pfn) { | 473 | zone_bm->bm_blocks = bb; |
| 332 | if (saveable(zone, &zone_pfn)) { | 474 | zone_bm->cur_block = bb; |
| 333 | struct page *page; | 475 | if (!bb) |
| 334 | page = pfn_to_page(zone_pfn + zone->zone_start_pfn); | 476 | goto Free; |
| 335 | BUG_ON(!pbe); | 477 | |
| 336 | pbe->orig_address = (unsigned long)page_address(page); | 478 | nr = zone->spanned_pages; |
| 337 | /* copy_page is not usable for copying task structs. */ | 479 | pfn = zone->zone_start_pfn; |
| 338 | memcpy((void *)pbe->address, (void *)pbe->orig_address, PAGE_SIZE); | 480 | /* Initialize the bitmap block objects */ |
| 339 | pbe = pbe->next; | 481 | while (bb) { |
| 482 | unsigned long *ptr; | ||
| 483 | |||
| 484 | ptr = alloc_image_page(gfp_mask, safe_needed); | ||
| 485 | bb->data = ptr; | ||
| 486 | if (!ptr) | ||
| 487 | goto Free; | ||
| 488 | |||
| 489 | bb->start_pfn = pfn; | ||
| 490 | if (nr >= BM_BITS_PER_BLOCK) { | ||
| 491 | pfn += BM_BITS_PER_BLOCK; | ||
| 492 | bb->size = BM_CHUNKS_PER_BLOCK; | ||
| 493 | nr -= BM_BITS_PER_BLOCK; | ||
| 494 | } else { | ||
| 495 | /* This is executed only once in the loop */ | ||
| 496 | pfn += nr; | ||
| 497 | bb->size = DIV_ROUND_UP(nr, BM_BITS_PER_CHUNK); | ||
| 340 | } | 498 | } |
| 499 | bb->end_pfn = pfn; | ||
| 500 | bb = bb->next; | ||
| 341 | } | 501 | } |
| 502 | zone_bm = zone_bm->next; | ||
| 342 | } | 503 | } |
| 343 | BUG_ON(pbe); | 504 | bm->p_list = ca.chain; |
| 344 | } | 505 | memory_bm_position_reset(bm); |
| 506 | return 0; | ||
| 345 | 507 | ||
| 508 | Free: | ||
| 509 | bm->p_list = ca.chain; | ||
| 510 | memory_bm_free(bm, PG_UNSAFE_CLEAR); | ||
| 511 | return -ENOMEM; | ||
| 512 | } | ||
| 346 | 513 | ||
| 347 | /** | 514 | /** |
| 348 | * free_pagedir - free pages allocated with alloc_pagedir() | 515 | * memory_bm_free - free memory occupied by the memory bitmap @bm |
| 349 | */ | 516 | */ |
| 350 | 517 | ||
| 351 | static void free_pagedir(struct pbe *pblist, int clear_nosave_free) | 518 | static void memory_bm_free(struct memory_bitmap *bm, int clear_nosave_free) |
| 352 | { | 519 | { |
| 353 | struct pbe *pbe; | 520 | struct zone_bitmap *zone_bm; |
| 354 | 521 | ||
| 355 | while (pblist) { | 522 | /* Free the list of bit blocks for each zone_bitmap object */ |
| 356 | pbe = (pblist + PB_PAGE_SKIP)->next; | 523 | zone_bm = bm->zone_bm_list; |
| 357 | ClearPageNosave(virt_to_page(pblist)); | 524 | while (zone_bm) { |
| 358 | if (clear_nosave_free) | 525 | struct bm_block *bb; |
| 359 | ClearPageNosaveFree(virt_to_page(pblist)); | 526 | |
| 360 | free_page((unsigned long)pblist); | 527 | bb = zone_bm->bm_blocks; |
| 361 | pblist = pbe; | 528 | while (bb) { |
| 529 | if (bb->data) | ||
| 530 | free_image_page(bb->data, clear_nosave_free); | ||
| 531 | bb = bb->next; | ||
| 532 | } | ||
| 533 | zone_bm = zone_bm->next; | ||
| 362 | } | 534 | } |
| 535 | free_list_of_pages(bm->p_list, clear_nosave_free); | ||
| 536 | bm->zone_bm_list = NULL; | ||
| 363 | } | 537 | } |
| 364 | 538 | ||
| 365 | /** | 539 | /** |
| 366 | * fill_pb_page - Create a list of PBEs on a given memory page | 540 | * memory_bm_set_bit - set the bit in the bitmap @bm that corresponds |
| 541 | * to given pfn. The cur_zone_bm member of @bm and the cur_block member | ||
| 542 | * of @bm->cur_zone_bm are updated. | ||
| 543 | * | ||
| 544 | * If the bit cannot be set, the function returns -EINVAL . | ||
| 367 | */ | 545 | */ |
| 368 | 546 | ||
| 369 | static inline void fill_pb_page(struct pbe *pbpage) | 547 | static int |
| 548 | memory_bm_set_bit(struct memory_bitmap *bm, unsigned long pfn) | ||
| 370 | { | 549 | { |
| 371 | struct pbe *p; | 550 | struct zone_bitmap *zone_bm; |
| 372 | 551 | struct bm_block *bb; | |
| 373 | p = pbpage; | 552 | |
| 374 | pbpage += PB_PAGE_SKIP; | 553 | /* Check if the pfn is from the current zone */ |
| 375 | do | 554 | zone_bm = bm->cur.zone_bm; |
| 376 | p->next = p + 1; | 555 | if (pfn < zone_bm->start_pfn || pfn >= zone_bm->end_pfn) { |
| 377 | while (++p < pbpage); | 556 | zone_bm = bm->zone_bm_list; |
| 557 | /* We don't assume that the zones are sorted by pfns */ | ||
| 558 | while (pfn < zone_bm->start_pfn || pfn >= zone_bm->end_pfn) { | ||
| 559 | zone_bm = zone_bm->next; | ||
| 560 | if (unlikely(!zone_bm)) | ||
| 561 | return -EINVAL; | ||
| 562 | } | ||
| 563 | bm->cur.zone_bm = zone_bm; | ||
| 564 | } | ||
| 565 | /* Check if the pfn corresponds to the current bitmap block */ | ||
| 566 | bb = zone_bm->cur_block; | ||
| 567 | if (pfn < bb->start_pfn) | ||
| 568 | bb = zone_bm->bm_blocks; | ||
| 569 | |||
| 570 | while (pfn >= bb->end_pfn) { | ||
| 571 | bb = bb->next; | ||
| 572 | if (unlikely(!bb)) | ||
| 573 | return -EINVAL; | ||
| 574 | } | ||
| 575 | zone_bm->cur_block = bb; | ||
| 576 | pfn -= bb->start_pfn; | ||
| 577 | set_bit(pfn % BM_BITS_PER_CHUNK, bb->data + pfn / BM_BITS_PER_CHUNK); | ||
| 578 | return 0; | ||
| 378 | } | 579 | } |
| 379 | 580 | ||
| 380 | /** | 581 | /* Two auxiliary functions for memory_bm_next_pfn */ |
| 381 | * create_pbe_list - Create a list of PBEs on top of a given chain | ||
| 382 | * of memory pages allocated with alloc_pagedir() | ||
| 383 | */ | ||
| 384 | 582 | ||
| 385 | static inline void create_pbe_list(struct pbe *pblist, unsigned int nr_pages) | 583 | /* Find the first set bit in the given chunk, if there is one */ |
| 386 | { | ||
| 387 | struct pbe *pbpage, *p; | ||
| 388 | unsigned int num = PBES_PER_PAGE; | ||
| 389 | 584 | ||
| 390 | for_each_pb_page (pbpage, pblist) { | 585 | static inline int next_bit_in_chunk(int bit, unsigned long *chunk_p) |
| 391 | if (num >= nr_pages) | 586 | { |
| 392 | break; | 587 | bit++; |
| 588 | while (bit < BM_BITS_PER_CHUNK) { | ||
| 589 | if (test_bit(bit, chunk_p)) | ||
| 590 | return bit; | ||
| 393 | 591 | ||
| 394 | fill_pb_page(pbpage); | 592 | bit++; |
| 395 | num += PBES_PER_PAGE; | ||
| 396 | } | ||
| 397 | if (pbpage) { | ||
| 398 | for (num -= PBES_PER_PAGE - 1, p = pbpage; num < nr_pages; p++, num++) | ||
| 399 | p->next = p + 1; | ||
| 400 | p->next = NULL; | ||
| 401 | } | 593 | } |
| 594 | return -1; | ||
| 402 | } | 595 | } |
| 403 | 596 | ||
| 404 | static unsigned int unsafe_pages; | 597 | /* Find a chunk containing some bits set in given block of bits */ |
| 598 | |||
| 599 | static inline int next_chunk_in_block(int n, struct bm_block *bb) | ||
| 600 | { | ||
| 601 | n++; | ||
| 602 | while (n < bb->size) { | ||
| 603 | if (bb->data[n]) | ||
| 604 | return n; | ||
| 605 | |||
| 606 | n++; | ||
| 607 | } | ||
| 608 | return -1; | ||
| 609 | } | ||
| 405 | 610 | ||
| 406 | /** | 611 | /** |
| 407 | * @safe_needed - on resume, for storing the PBE list and the image, | 612 | * memory_bm_next_pfn - find the pfn that corresponds to the next set bit |
| 408 | * we can only use memory pages that do not conflict with the pages | 613 | * in the bitmap @bm. If the pfn cannot be found, BM_END_OF_MAP is |
| 409 | * used before suspend. | 614 | * returned. |
| 410 | * | 615 | * |
| 411 | * The unsafe pages are marked with the PG_nosave_free flag | 616 | * It is required to run memory_bm_position_reset() before the first call to |
| 412 | * and we count them using unsafe_pages | 617 | * this function. |
| 413 | */ | 618 | */ |
| 414 | 619 | ||
| 415 | static inline void *alloc_image_page(gfp_t gfp_mask, int safe_needed) | 620 | static unsigned long memory_bm_next_pfn(struct memory_bitmap *bm) |
| 416 | { | 621 | { |
| 417 | void *res; | 622 | struct zone_bitmap *zone_bm; |
| 418 | 623 | struct bm_block *bb; | |
| 419 | res = (void *)get_zeroed_page(gfp_mask); | 624 | int chunk; |
| 420 | if (safe_needed) | 625 | int bit; |
| 421 | while (res && PageNosaveFree(virt_to_page(res))) { | 626 | |
| 422 | /* The page is unsafe, mark it for swsusp_free() */ | 627 | do { |
| 423 | SetPageNosave(virt_to_page(res)); | 628 | bb = bm->cur.block; |
| 424 | unsafe_pages++; | 629 | do { |
| 425 | res = (void *)get_zeroed_page(gfp_mask); | 630 | chunk = bm->cur.chunk; |
| 631 | bit = bm->cur.bit; | ||
| 632 | do { | ||
| 633 | bit = next_bit_in_chunk(bit, bb->data + chunk); | ||
| 634 | if (bit >= 0) | ||
| 635 | goto Return_pfn; | ||
| 636 | |||
| 637 | chunk = next_chunk_in_block(chunk, bb); | ||
| 638 | bit = -1; | ||
| 639 | } while (chunk >= 0); | ||
| 640 | bb = bb->next; | ||
| 641 | bm->cur.block = bb; | ||
| 642 | memory_bm_reset_chunk(bm); | ||
| 643 | } while (bb); | ||
| 644 | zone_bm = bm->cur.zone_bm->next; | ||
| 645 | if (zone_bm) { | ||
| 646 | bm->cur.zone_bm = zone_bm; | ||
| 647 | bm->cur.block = zone_bm->bm_blocks; | ||
| 648 | memory_bm_reset_chunk(bm); | ||
| 426 | } | 649 | } |
| 427 | if (res) { | 650 | } while (zone_bm); |
| 428 | SetPageNosave(virt_to_page(res)); | 651 | memory_bm_position_reset(bm); |
| 429 | SetPageNosaveFree(virt_to_page(res)); | 652 | return BM_END_OF_MAP; |
| 430 | } | 653 | |
| 654 | Return_pfn: | ||
| 655 | bm->cur.chunk = chunk; | ||
| 656 | bm->cur.bit = bit; | ||
| 657 | return bb->start_pfn + chunk * BM_BITS_PER_CHUNK + bit; | ||
| 658 | } | ||
| 659 | |||
| 660 | /** | ||
| 661 | * snapshot_additional_pages - estimate the number of additional pages | ||
| 662 | * be needed for setting up the suspend image data structures for given | ||
| 663 | * zone (usually the returned value is greater than the exact number) | ||
| 664 | */ | ||
| 665 | |||
| 666 | unsigned int snapshot_additional_pages(struct zone *zone) | ||
| 667 | { | ||
| 668 | unsigned int res; | ||
| 669 | |||
| 670 | res = DIV_ROUND_UP(zone->spanned_pages, BM_BITS_PER_BLOCK); | ||
| 671 | res += DIV_ROUND_UP(res * sizeof(struct bm_block), PAGE_SIZE); | ||
| 431 | return res; | 672 | return res; |
| 432 | } | 673 | } |
| 433 | 674 | ||
| 434 | unsigned long get_safe_page(gfp_t gfp_mask) | 675 | /** |
| 676 | * pfn_is_nosave - check if given pfn is in the 'nosave' section | ||
| 677 | */ | ||
| 678 | |||
| 679 | static inline int pfn_is_nosave(unsigned long pfn) | ||
| 435 | { | 680 | { |
| 436 | return (unsigned long)alloc_image_page(gfp_mask, 1); | 681 | unsigned long nosave_begin_pfn = __pa(&__nosave_begin) >> PAGE_SHIFT; |
| 682 | unsigned long nosave_end_pfn = PAGE_ALIGN(__pa(&__nosave_end)) >> PAGE_SHIFT; | ||
| 683 | return (pfn >= nosave_begin_pfn) && (pfn < nosave_end_pfn); | ||
| 437 | } | 684 | } |
| 438 | 685 | ||
| 439 | /** | 686 | /** |
| 440 | * alloc_pagedir - Allocate the page directory. | 687 | * saveable - Determine whether a page should be cloned or not. |
| 441 | * | 688 | * @pfn: The page |
| 442 | * First, determine exactly how many pages we need and | ||
| 443 | * allocate them. | ||
| 444 | * | ||
| 445 | * We arrange the pages in a chain: each page is an array of PBES_PER_PAGE | ||
| 446 | * struct pbe elements (pbes) and the last element in the page points | ||
| 447 | * to the next page. | ||
| 448 | * | 689 | * |
| 449 | * On each page we set up a list of struct_pbe elements. | 690 | * We save a page if it isn't Nosave, and is not in the range of pages |
| 691 | * statically defined as 'unsaveable', and it | ||
| 692 | * isn't a part of a free chunk of pages. | ||
| 450 | */ | 693 | */ |
| 451 | 694 | ||
| 452 | static struct pbe *alloc_pagedir(unsigned int nr_pages, gfp_t gfp_mask, | 695 | static struct page *saveable_page(unsigned long pfn) |
| 453 | int safe_needed) | ||
| 454 | { | 696 | { |
| 455 | unsigned int num; | 697 | struct page *page; |
| 456 | struct pbe *pblist, *pbe; | 698 | |
| 699 | if (!pfn_valid(pfn)) | ||
| 700 | return NULL; | ||
| 457 | 701 | ||
| 458 | if (!nr_pages) | 702 | page = pfn_to_page(pfn); |
| 703 | |||
| 704 | if (PageNosave(page)) | ||
| 705 | return NULL; | ||
| 706 | if (PageReserved(page) && pfn_is_nosave(pfn)) | ||
| 459 | return NULL; | 707 | return NULL; |
| 708 | if (PageNosaveFree(page)) | ||
| 709 | return NULL; | ||
| 710 | |||
| 711 | return page; | ||
| 712 | } | ||
| 713 | |||
| 714 | unsigned int count_data_pages(void) | ||
| 715 | { | ||
| 716 | struct zone *zone; | ||
| 717 | unsigned long pfn, max_zone_pfn; | ||
| 718 | unsigned int n = 0; | ||
| 719 | |||
| 720 | for_each_zone (zone) { | ||
| 721 | if (is_highmem(zone)) | ||
| 722 | continue; | ||
| 723 | mark_free_pages(zone); | ||
| 724 | max_zone_pfn = zone->zone_start_pfn + zone->spanned_pages; | ||
| 725 | for (pfn = zone->zone_start_pfn; pfn < max_zone_pfn; pfn++) | ||
| 726 | n += !!saveable_page(pfn); | ||
| 727 | } | ||
| 728 | return n; | ||
| 729 | } | ||
| 730 | |||
| 731 | static inline void copy_data_page(long *dst, long *src) | ||
| 732 | { | ||
| 733 | int n; | ||
| 734 | |||
| 735 | /* copy_page and memcpy are not usable for copying task structs. */ | ||
| 736 | for (n = PAGE_SIZE / sizeof(long); n; n--) | ||
| 737 | *dst++ = *src++; | ||
| 738 | } | ||
| 739 | |||
| 740 | static void | ||
| 741 | copy_data_pages(struct memory_bitmap *copy_bm, struct memory_bitmap *orig_bm) | ||
| 742 | { | ||
| 743 | struct zone *zone; | ||
| 744 | unsigned long pfn; | ||
| 745 | |||
| 746 | for_each_zone (zone) { | ||
| 747 | unsigned long max_zone_pfn; | ||
| 460 | 748 | ||
| 461 | pblist = alloc_image_page(gfp_mask, safe_needed); | 749 | if (is_highmem(zone)) |
| 462 | /* FIXME: rewrite this ugly loop */ | 750 | continue; |
| 463 | for (pbe = pblist, num = PBES_PER_PAGE; pbe && num < nr_pages; | 751 | |
| 464 | pbe = pbe->next, num += PBES_PER_PAGE) { | 752 | mark_free_pages(zone); |
| 465 | pbe += PB_PAGE_SKIP; | 753 | max_zone_pfn = zone->zone_start_pfn + zone->spanned_pages; |
| 466 | pbe->next = alloc_image_page(gfp_mask, safe_needed); | 754 | for (pfn = zone->zone_start_pfn; pfn < max_zone_pfn; pfn++) |
| 755 | if (saveable_page(pfn)) | ||
| 756 | memory_bm_set_bit(orig_bm, pfn); | ||
| 467 | } | 757 | } |
| 468 | if (!pbe) { /* get_zeroed_page() failed */ | 758 | memory_bm_position_reset(orig_bm); |
| 469 | free_pagedir(pblist, 1); | 759 | memory_bm_position_reset(copy_bm); |
| 470 | pblist = NULL; | 760 | do { |
| 471 | } else | 761 | pfn = memory_bm_next_pfn(orig_bm); |
| 472 | create_pbe_list(pblist, nr_pages); | 762 | if (likely(pfn != BM_END_OF_MAP)) { |
| 473 | return pblist; | 763 | struct page *page; |
| 764 | void *src; | ||
| 765 | |||
| 766 | page = pfn_to_page(pfn); | ||
| 767 | src = page_address(page); | ||
| 768 | page = pfn_to_page(memory_bm_next_pfn(copy_bm)); | ||
| 769 | copy_data_page(page_address(page), src); | ||
| 770 | } | ||
| 771 | } while (pfn != BM_END_OF_MAP); | ||
| 474 | } | 772 | } |
| 475 | 773 | ||
| 476 | /** | 774 | /** |
| 477 | * Free pages we allocated for suspend. Suspend pages are alocated | 775 | * swsusp_free - free pages allocated for the suspend. |
| 478 | * before atomic copy, so we need to free them after resume. | 776 | * |
| 777 | * Suspend pages are alocated before the atomic copy is made, so we | ||
| 778 | * need to release them after the resume. | ||
| 479 | */ | 779 | */ |
| 480 | 780 | ||
| 481 | void swsusp_free(void) | 781 | void swsusp_free(void) |
| 482 | { | 782 | { |
| 483 | struct zone *zone; | 783 | struct zone *zone; |
| 484 | unsigned long zone_pfn; | 784 | unsigned long pfn, max_zone_pfn; |
| 485 | 785 | ||
| 486 | for_each_zone(zone) { | 786 | for_each_zone(zone) { |
| 487 | for (zone_pfn = 0; zone_pfn < zone->spanned_pages; ++zone_pfn) | 787 | max_zone_pfn = zone->zone_start_pfn + zone->spanned_pages; |
| 488 | if (pfn_valid(zone_pfn + zone->zone_start_pfn)) { | 788 | for (pfn = zone->zone_start_pfn; pfn < max_zone_pfn; pfn++) |
| 489 | struct page *page; | 789 | if (pfn_valid(pfn)) { |
| 490 | page = pfn_to_page(zone_pfn + zone->zone_start_pfn); | 790 | struct page *page = pfn_to_page(pfn); |
| 791 | |||
| 491 | if (PageNosave(page) && PageNosaveFree(page)) { | 792 | if (PageNosave(page) && PageNosaveFree(page)) { |
| 492 | ClearPageNosave(page); | 793 | ClearPageNosave(page); |
| 493 | ClearPageNosaveFree(page); | 794 | ClearPageNosaveFree(page); |
| @@ -497,7 +798,7 @@ void swsusp_free(void) | |||
| 497 | } | 798 | } |
| 498 | nr_copy_pages = 0; | 799 | nr_copy_pages = 0; |
| 499 | nr_meta_pages = 0; | 800 | nr_meta_pages = 0; |
| 500 | pagedir_nosave = NULL; | 801 | restore_pblist = NULL; |
| 501 | buffer = NULL; | 802 | buffer = NULL; |
| 502 | } | 803 | } |
| 503 | 804 | ||
| @@ -512,46 +813,57 @@ void swsusp_free(void) | |||
| 512 | static int enough_free_mem(unsigned int nr_pages) | 813 | static int enough_free_mem(unsigned int nr_pages) |
| 513 | { | 814 | { |
| 514 | struct zone *zone; | 815 | struct zone *zone; |
| 515 | unsigned int n = 0; | 816 | unsigned int free = 0, meta = 0; |
| 516 | 817 | ||
| 517 | for_each_zone (zone) | 818 | for_each_zone (zone) |
| 518 | if (!is_highmem(zone)) | 819 | if (!is_highmem(zone)) { |
| 519 | n += zone->free_pages; | 820 | free += zone->free_pages; |
| 520 | pr_debug("swsusp: available memory: %u pages\n", n); | 821 | meta += snapshot_additional_pages(zone); |
| 521 | return n > (nr_pages + PAGES_FOR_IO + | 822 | } |
| 522 | (nr_pages + PBES_PER_PAGE - 1) / PBES_PER_PAGE); | ||
| 523 | } | ||
| 524 | 823 | ||
| 525 | static int alloc_data_pages(struct pbe *pblist, gfp_t gfp_mask, int safe_needed) | 824 | pr_debug("swsusp: pages needed: %u + %u + %u, available pages: %u\n", |
| 526 | { | 825 | nr_pages, PAGES_FOR_IO, meta, free); |
| 527 | struct pbe *p; | ||
| 528 | 826 | ||
| 529 | for_each_pbe (p, pblist) { | 827 | return free > nr_pages + PAGES_FOR_IO + meta; |
| 530 | p->address = (unsigned long)alloc_image_page(gfp_mask, safe_needed); | ||
| 531 | if (!p->address) | ||
| 532 | return -ENOMEM; | ||
| 533 | } | ||
| 534 | return 0; | ||
| 535 | } | 828 | } |
| 536 | 829 | ||
| 537 | static struct pbe *swsusp_alloc(unsigned int nr_pages) | 830 | static int |
| 831 | swsusp_alloc(struct memory_bitmap *orig_bm, struct memory_bitmap *copy_bm, | ||
| 832 | unsigned int nr_pages) | ||
| 538 | { | 833 | { |
| 539 | struct pbe *pblist; | 834 | int error; |
| 540 | 835 | ||
| 541 | if (!(pblist = alloc_pagedir(nr_pages, GFP_ATOMIC | __GFP_COLD, 0))) { | 836 | error = memory_bm_create(orig_bm, GFP_ATOMIC | __GFP_COLD, PG_ANY); |
| 542 | printk(KERN_ERR "suspend: Allocating pagedir failed.\n"); | 837 | if (error) |
| 543 | return NULL; | 838 | goto Free; |
| 544 | } | ||
| 545 | 839 | ||
| 546 | if (alloc_data_pages(pblist, GFP_ATOMIC | __GFP_COLD, 0)) { | 840 | error = memory_bm_create(copy_bm, GFP_ATOMIC | __GFP_COLD, PG_ANY); |
| 547 | printk(KERN_ERR "suspend: Allocating image pages failed.\n"); | 841 | if (error) |
| 548 | swsusp_free(); | 842 | goto Free; |
| 549 | return NULL; | 843 | |
| 844 | while (nr_pages-- > 0) { | ||
| 845 | struct page *page = alloc_page(GFP_ATOMIC | __GFP_COLD); | ||
| 846 | if (!page) | ||
| 847 | goto Free; | ||
| 848 | |||
| 849 | SetPageNosave(page); | ||
| 850 | SetPageNosaveFree(page); | ||
| 851 | memory_bm_set_bit(copy_bm, page_to_pfn(page)); | ||
| 550 | } | 852 | } |
| 853 | return 0; | ||
| 551 | 854 | ||
| 552 | return pblist; | 855 | Free: |
| 856 | swsusp_free(); | ||
| 857 | return -ENOMEM; | ||
| 553 | } | 858 | } |
| 554 | 859 | ||
| 860 | /* Memory bitmap used for marking saveable pages */ | ||
| 861 | static struct memory_bitmap orig_bm; | ||
| 862 | /* Memory bitmap used for marking allocated pages that will contain the copies | ||
| 863 | * of saveable pages | ||
| 864 | */ | ||
| 865 | static struct memory_bitmap copy_bm; | ||
| 866 | |||
| 555 | asmlinkage int swsusp_save(void) | 867 | asmlinkage int swsusp_save(void) |
| 556 | { | 868 | { |
| 557 | unsigned int nr_pages; | 869 | unsigned int nr_pages; |
| @@ -562,25 +874,19 @@ asmlinkage int swsusp_save(void) | |||
| 562 | nr_pages = count_data_pages(); | 874 | nr_pages = count_data_pages(); |
| 563 | printk("swsusp: Need to copy %u pages\n", nr_pages); | 875 | printk("swsusp: Need to copy %u pages\n", nr_pages); |
| 564 | 876 | ||
| 565 | pr_debug("swsusp: pages needed: %u + %lu + %u, free: %u\n", | ||
| 566 | nr_pages, | ||
| 567 | (nr_pages + PBES_PER_PAGE - 1) / PBES_PER_PAGE, | ||
| 568 | PAGES_FOR_IO, nr_free_pages()); | ||
| 569 | |||
| 570 | if (!enough_free_mem(nr_pages)) { | 877 | if (!enough_free_mem(nr_pages)) { |
| 571 | printk(KERN_ERR "swsusp: Not enough free memory\n"); | 878 | printk(KERN_ERR "swsusp: Not enough free memory\n"); |
| 572 | return -ENOMEM; | 879 | return -ENOMEM; |
| 573 | } | 880 | } |
| 574 | 881 | ||
| 575 | pagedir_nosave = swsusp_alloc(nr_pages); | 882 | if (swsusp_alloc(&orig_bm, ©_bm, nr_pages)) |
| 576 | if (!pagedir_nosave) | ||
| 577 | return -ENOMEM; | 883 | return -ENOMEM; |
| 578 | 884 | ||
| 579 | /* During allocating of suspend pagedir, new cold pages may appear. | 885 | /* During allocating of suspend pagedir, new cold pages may appear. |
| 580 | * Kill them. | 886 | * Kill them. |
| 581 | */ | 887 | */ |
| 582 | drain_local_pages(); | 888 | drain_local_pages(); |
| 583 | copy_data_pages(pagedir_nosave); | 889 | copy_data_pages(©_bm, &orig_bm); |
| 584 | 890 | ||
| 585 | /* | 891 | /* |
| 586 | * End of critical section. From now on, we can write to memory, | 892 | * End of critical section. From now on, we can write to memory, |
| @@ -609,22 +915,20 @@ static void init_header(struct swsusp_info *info) | |||
| 609 | } | 915 | } |
| 610 | 916 | ||
| 611 | /** | 917 | /** |
| 612 | * pack_orig_addresses - the .orig_address fields of the PBEs from the | 918 | * pack_pfns - pfns corresponding to the set bits found in the bitmap @bm |
| 613 | * list starting at @pbe are stored in the array @buf[] (1 page) | 919 | * are stored in the array @buf[] (1 page at a time) |
| 614 | */ | 920 | */ |
| 615 | 921 | ||
| 616 | static inline struct pbe *pack_orig_addresses(unsigned long *buf, struct pbe *pbe) | 922 | static inline void |
| 923 | pack_pfns(unsigned long *buf, struct memory_bitmap *bm) | ||
| 617 | { | 924 | { |
| 618 | int j; | 925 | int j; |
| 619 | 926 | ||
| 620 | for (j = 0; j < PAGE_SIZE / sizeof(long) && pbe; j++) { | 927 | for (j = 0; j < PAGE_SIZE / sizeof(long); j++) { |
| 621 | buf[j] = pbe->orig_address; | 928 | buf[j] = memory_bm_next_pfn(bm); |
| 622 | pbe = pbe->next; | 929 | if (unlikely(buf[j] == BM_END_OF_MAP)) |
| 930 | break; | ||
| 623 | } | 931 | } |
| 624 | if (!pbe) | ||
| 625 | for (; j < PAGE_SIZE / sizeof(long); j++) | ||
| 626 | buf[j] = 0; | ||
| 627 | return pbe; | ||
| 628 | } | 932 | } |
| 629 | 933 | ||
| 630 | /** | 934 | /** |
| @@ -651,37 +955,39 @@ static inline struct pbe *pack_orig_addresses(unsigned long *buf, struct pbe *pb | |||
| 651 | 955 | ||
| 652 | int snapshot_read_next(struct snapshot_handle *handle, size_t count) | 956 | int snapshot_read_next(struct snapshot_handle *handle, size_t count) |
| 653 | { | 957 | { |
| 654 | if (handle->page > nr_meta_pages + nr_copy_pages) | 958 | if (handle->cur > nr_meta_pages + nr_copy_pages) |
| 655 | return 0; | 959 | return 0; |
| 960 | |||
| 656 | if (!buffer) { | 961 | if (!buffer) { |
| 657 | /* This makes the buffer be freed by swsusp_free() */ | 962 | /* This makes the buffer be freed by swsusp_free() */ |
| 658 | buffer = alloc_image_page(GFP_ATOMIC, 0); | 963 | buffer = alloc_image_page(GFP_ATOMIC, PG_ANY); |
| 659 | if (!buffer) | 964 | if (!buffer) |
| 660 | return -ENOMEM; | 965 | return -ENOMEM; |
| 661 | } | 966 | } |
| 662 | if (!handle->offset) { | 967 | if (!handle->offset) { |
| 663 | init_header((struct swsusp_info *)buffer); | 968 | init_header((struct swsusp_info *)buffer); |
| 664 | handle->buffer = buffer; | 969 | handle->buffer = buffer; |
| 665 | handle->pbe = pagedir_nosave; | 970 | memory_bm_position_reset(&orig_bm); |
| 971 | memory_bm_position_reset(©_bm); | ||
| 666 | } | 972 | } |
| 667 | if (handle->prev < handle->page) { | 973 | if (handle->prev < handle->cur) { |
| 668 | if (handle->page <= nr_meta_pages) { | 974 | if (handle->cur <= nr_meta_pages) { |
| 669 | handle->pbe = pack_orig_addresses(buffer, handle->pbe); | 975 | memset(buffer, 0, PAGE_SIZE); |
| 670 | if (!handle->pbe) | 976 | pack_pfns(buffer, &orig_bm); |
| 671 | handle->pbe = pagedir_nosave; | ||
| 672 | } else { | 977 | } else { |
| 673 | handle->buffer = (void *)handle->pbe->address; | 978 | unsigned long pfn = memory_bm_next_pfn(©_bm); |
| 674 | handle->pbe = handle->pbe->next; | 979 | |
| 980 | handle->buffer = page_address(pfn_to_page(pfn)); | ||
| 675 | } | 981 | } |
| 676 | handle->prev = handle->page; | 982 | handle->prev = handle->cur; |
| 677 | } | 983 | } |
| 678 | handle->buf_offset = handle->page_offset; | 984 | handle->buf_offset = handle->cur_offset; |
| 679 | if (handle->page_offset + count >= PAGE_SIZE) { | 985 | if (handle->cur_offset + count >= PAGE_SIZE) { |
| 680 | count = PAGE_SIZE - handle->page_offset; | 986 | count = PAGE_SIZE - handle->cur_offset; |
| 681 | handle->page_offset = 0; | 987 | handle->cur_offset = 0; |
| 682 | handle->page++; | 988 | handle->cur++; |
| 683 | } else { | 989 | } else { |
| 684 | handle->page_offset += count; | 990 | handle->cur_offset += count; |
| 685 | } | 991 | } |
| 686 | handle->offset += count; | 992 | handle->offset += count; |
| 687 | return count; | 993 | return count; |
| @@ -693,47 +999,50 @@ int snapshot_read_next(struct snapshot_handle *handle, size_t count) | |||
| 693 | * had been used before suspend | 999 | * had been used before suspend |
| 694 | */ | 1000 | */ |
| 695 | 1001 | ||
| 696 | static int mark_unsafe_pages(struct pbe *pblist) | 1002 | static int mark_unsafe_pages(struct memory_bitmap *bm) |
| 697 | { | 1003 | { |
| 698 | struct zone *zone; | 1004 | struct zone *zone; |
| 699 | unsigned long zone_pfn; | 1005 | unsigned long pfn, max_zone_pfn; |
| 700 | struct pbe *p; | ||
| 701 | |||
| 702 | if (!pblist) /* a sanity check */ | ||
| 703 | return -EINVAL; | ||
| 704 | 1006 | ||
| 705 | /* Clear page flags */ | 1007 | /* Clear page flags */ |
| 706 | for_each_zone (zone) { | 1008 | for_each_zone (zone) { |
| 707 | for (zone_pfn = 0; zone_pfn < zone->spanned_pages; ++zone_pfn) | 1009 | max_zone_pfn = zone->zone_start_pfn + zone->spanned_pages; |
| 708 | if (pfn_valid(zone_pfn + zone->zone_start_pfn)) | 1010 | for (pfn = zone->zone_start_pfn; pfn < max_zone_pfn; pfn++) |
| 709 | ClearPageNosaveFree(pfn_to_page(zone_pfn + | 1011 | if (pfn_valid(pfn)) |
| 710 | zone->zone_start_pfn)); | 1012 | ClearPageNosaveFree(pfn_to_page(pfn)); |
| 711 | } | 1013 | } |
| 712 | 1014 | ||
| 713 | /* Mark orig addresses */ | 1015 | /* Mark pages that correspond to the "original" pfns as "unsafe" */ |
| 714 | for_each_pbe (p, pblist) { | 1016 | memory_bm_position_reset(bm); |
| 715 | if (virt_addr_valid(p->orig_address)) | 1017 | do { |
| 716 | SetPageNosaveFree(virt_to_page(p->orig_address)); | 1018 | pfn = memory_bm_next_pfn(bm); |
| 717 | else | 1019 | if (likely(pfn != BM_END_OF_MAP)) { |
| 718 | return -EFAULT; | 1020 | if (likely(pfn_valid(pfn))) |
| 719 | } | 1021 | SetPageNosaveFree(pfn_to_page(pfn)); |
| 1022 | else | ||
| 1023 | return -EFAULT; | ||
| 1024 | } | ||
| 1025 | } while (pfn != BM_END_OF_MAP); | ||
| 720 | 1026 | ||
| 721 | unsafe_pages = 0; | 1027 | allocated_unsafe_pages = 0; |
| 722 | 1028 | ||
| 723 | return 0; | 1029 | return 0; |
| 724 | } | 1030 | } |
| 725 | 1031 | ||
| 726 | static void copy_page_backup_list(struct pbe *dst, struct pbe *src) | 1032 | static void |
| 1033 | duplicate_memory_bitmap(struct memory_bitmap *dst, struct memory_bitmap *src) | ||
| 727 | { | 1034 | { |
| 728 | /* We assume both lists contain the same number of elements */ | 1035 | unsigned long pfn; |
| 729 | while (src) { | 1036 | |
| 730 | dst->orig_address = src->orig_address; | 1037 | memory_bm_position_reset(src); |
| 731 | dst = dst->next; | 1038 | pfn = memory_bm_next_pfn(src); |
| 732 | src = src->next; | 1039 | while (pfn != BM_END_OF_MAP) { |
| 1040 | memory_bm_set_bit(dst, pfn); | ||
| 1041 | pfn = memory_bm_next_pfn(src); | ||
| 733 | } | 1042 | } |
| 734 | } | 1043 | } |
| 735 | 1044 | ||
| 736 | static int check_header(struct swsusp_info *info) | 1045 | static inline int check_header(struct swsusp_info *info) |
| 737 | { | 1046 | { |
| 738 | char *reason = NULL; | 1047 | char *reason = NULL; |
| 739 | 1048 | ||
| @@ -760,19 +1069,14 @@ static int check_header(struct swsusp_info *info) | |||
| 760 | * load header - check the image header and copy data from it | 1069 | * load header - check the image header and copy data from it |
| 761 | */ | 1070 | */ |
| 762 | 1071 | ||
| 763 | static int load_header(struct snapshot_handle *handle, | 1072 | static int |
| 764 | struct swsusp_info *info) | 1073 | load_header(struct swsusp_info *info) |
| 765 | { | 1074 | { |
| 766 | int error; | 1075 | int error; |
| 767 | struct pbe *pblist; | ||
| 768 | 1076 | ||
| 1077 | restore_pblist = NULL; | ||
| 769 | error = check_header(info); | 1078 | error = check_header(info); |
| 770 | if (!error) { | 1079 | if (!error) { |
| 771 | pblist = alloc_pagedir(info->image_pages, GFP_ATOMIC, 0); | ||
| 772 | if (!pblist) | ||
| 773 | return -ENOMEM; | ||
| 774 | pagedir_nosave = pblist; | ||
| 775 | handle->pbe = pblist; | ||
| 776 | nr_copy_pages = info->image_pages; | 1080 | nr_copy_pages = info->image_pages; |
| 777 | nr_meta_pages = info->pages - info->image_pages - 1; | 1081 | nr_meta_pages = info->pages - info->image_pages - 1; |
| 778 | } | 1082 | } |
| @@ -780,113 +1084,137 @@ static int load_header(struct snapshot_handle *handle, | |||
| 780 | } | 1084 | } |
| 781 | 1085 | ||
| 782 | /** | 1086 | /** |
| 783 | * unpack_orig_addresses - copy the elements of @buf[] (1 page) to | 1087 | * unpack_orig_pfns - for each element of @buf[] (1 page at a time) set |
| 784 | * the PBEs in the list starting at @pbe | 1088 | * the corresponding bit in the memory bitmap @bm |
| 785 | */ | 1089 | */ |
| 786 | 1090 | ||
| 787 | static inline struct pbe *unpack_orig_addresses(unsigned long *buf, | 1091 | static inline void |
| 788 | struct pbe *pbe) | 1092 | unpack_orig_pfns(unsigned long *buf, struct memory_bitmap *bm) |
| 789 | { | 1093 | { |
| 790 | int j; | 1094 | int j; |
| 791 | 1095 | ||
| 792 | for (j = 0; j < PAGE_SIZE / sizeof(long) && pbe; j++) { | 1096 | for (j = 0; j < PAGE_SIZE / sizeof(long); j++) { |
| 793 | pbe->orig_address = buf[j]; | 1097 | if (unlikely(buf[j] == BM_END_OF_MAP)) |
| 794 | pbe = pbe->next; | 1098 | break; |
| 1099 | |||
| 1100 | memory_bm_set_bit(bm, buf[j]); | ||
| 795 | } | 1101 | } |
| 796 | return pbe; | ||
| 797 | } | 1102 | } |
| 798 | 1103 | ||
| 799 | /** | 1104 | /** |
| 800 | * prepare_image - use metadata contained in the PBE list | 1105 | * prepare_image - use the memory bitmap @bm to mark the pages that will |
| 801 | * pointed to by pagedir_nosave to mark the pages that will | 1106 | * be overwritten in the process of restoring the system memory state |
| 802 | * be overwritten in the process of restoring the system | 1107 | * from the suspend image ("unsafe" pages) and allocate memory for the |
| 803 | * memory state from the image ("unsafe" pages) and allocate | 1108 | * image. |
| 804 | * memory for the image | ||
| 805 | * | 1109 | * |
| 806 | * The idea is to allocate the PBE list first and then | 1110 | * The idea is to allocate a new memory bitmap first and then allocate |
| 807 | * allocate as many pages as it's needed for the image data, | 1111 | * as many pages as needed for the image data, but not to assign these |
| 808 | * but not to assign these pages to the PBEs initially. | 1112 | * pages to specific tasks initially. Instead, we just mark them as |
| 809 | * Instead, we just mark them as allocated and create a list | 1113 | * allocated and create a list of "safe" pages that will be used later. |
| 810 | * of "safe" which will be used later | ||
| 811 | */ | 1114 | */ |
| 812 | 1115 | ||
| 813 | struct safe_page { | 1116 | #define PBES_PER_LINKED_PAGE (LINKED_PAGE_DATA_SIZE / sizeof(struct pbe)) |
| 814 | struct safe_page *next; | ||
| 815 | char padding[PAGE_SIZE - sizeof(void *)]; | ||
| 816 | }; | ||
| 817 | 1117 | ||
| 818 | static struct safe_page *safe_pages; | 1118 | static struct linked_page *safe_pages_list; |
| 819 | 1119 | ||
| 820 | static int prepare_image(struct snapshot_handle *handle) | 1120 | static int |
| 1121 | prepare_image(struct memory_bitmap *new_bm, struct memory_bitmap *bm) | ||
| 821 | { | 1122 | { |
| 822 | int error = 0; | 1123 | unsigned int nr_pages; |
| 823 | unsigned int nr_pages = nr_copy_pages; | 1124 | struct linked_page *sp_list, *lp; |
| 824 | struct pbe *p, *pblist = NULL; | 1125 | int error; |
| 825 | 1126 | ||
| 826 | p = pagedir_nosave; | 1127 | error = mark_unsafe_pages(bm); |
| 827 | error = mark_unsafe_pages(p); | 1128 | if (error) |
| 828 | if (!error) { | 1129 | goto Free; |
| 829 | pblist = alloc_pagedir(nr_pages, GFP_ATOMIC, 1); | 1130 | |
| 830 | if (pblist) | 1131 | error = memory_bm_create(new_bm, GFP_ATOMIC, PG_SAFE); |
| 831 | copy_page_backup_list(pblist, p); | 1132 | if (error) |
| 832 | free_pagedir(p, 0); | 1133 | goto Free; |
| 833 | if (!pblist) | 1134 | |
| 1135 | duplicate_memory_bitmap(new_bm, bm); | ||
| 1136 | memory_bm_free(bm, PG_UNSAFE_KEEP); | ||
| 1137 | /* Reserve some safe pages for potential later use. | ||
| 1138 | * | ||
| 1139 | * NOTE: This way we make sure there will be enough safe pages for the | ||
| 1140 | * chain_alloc() in get_buffer(). It is a bit wasteful, but | ||
| 1141 | * nr_copy_pages cannot be greater than 50% of the memory anyway. | ||
| 1142 | */ | ||
| 1143 | sp_list = NULL; | ||
| 1144 | /* nr_copy_pages cannot be lesser than allocated_unsafe_pages */ | ||
| 1145 | nr_pages = nr_copy_pages - allocated_unsafe_pages; | ||
| 1146 | nr_pages = DIV_ROUND_UP(nr_pages, PBES_PER_LINKED_PAGE); | ||
| 1147 | while (nr_pages > 0) { | ||
| 1148 | lp = alloc_image_page(GFP_ATOMIC, PG_SAFE); | ||
| 1149 | if (!lp) { | ||
| 834 | error = -ENOMEM; | 1150 | error = -ENOMEM; |
| 1151 | goto Free; | ||
| 1152 | } | ||
| 1153 | lp->next = sp_list; | ||
| 1154 | sp_list = lp; | ||
| 1155 | nr_pages--; | ||
| 835 | } | 1156 | } |
| 836 | safe_pages = NULL; | 1157 | /* Preallocate memory for the image */ |
| 837 | if (!error && nr_pages > unsafe_pages) { | 1158 | safe_pages_list = NULL; |
| 838 | nr_pages -= unsafe_pages; | 1159 | nr_pages = nr_copy_pages - allocated_unsafe_pages; |
| 839 | while (nr_pages--) { | 1160 | while (nr_pages > 0) { |
| 840 | struct safe_page *ptr; | 1161 | lp = (struct linked_page *)get_zeroed_page(GFP_ATOMIC); |
| 841 | 1162 | if (!lp) { | |
| 842 | ptr = (struct safe_page *)get_zeroed_page(GFP_ATOMIC); | 1163 | error = -ENOMEM; |
| 843 | if (!ptr) { | 1164 | goto Free; |
| 844 | error = -ENOMEM; | 1165 | } |
| 845 | break; | 1166 | if (!PageNosaveFree(virt_to_page(lp))) { |
| 846 | } | 1167 | /* The page is "safe", add it to the list */ |
| 847 | if (!PageNosaveFree(virt_to_page(ptr))) { | 1168 | lp->next = safe_pages_list; |
| 848 | /* The page is "safe", add it to the list */ | 1169 | safe_pages_list = lp; |
| 849 | ptr->next = safe_pages; | ||
| 850 | safe_pages = ptr; | ||
| 851 | } | ||
| 852 | /* Mark the page as allocated */ | ||
| 853 | SetPageNosave(virt_to_page(ptr)); | ||
| 854 | SetPageNosaveFree(virt_to_page(ptr)); | ||
| 855 | } | 1170 | } |
| 1171 | /* Mark the page as allocated */ | ||
| 1172 | SetPageNosave(virt_to_page(lp)); | ||
| 1173 | SetPageNosaveFree(virt_to_page(lp)); | ||
| 1174 | nr_pages--; | ||
| 856 | } | 1175 | } |
| 857 | if (!error) { | 1176 | /* Free the reserved safe pages so that chain_alloc() can use them */ |
| 858 | pagedir_nosave = pblist; | 1177 | while (sp_list) { |
| 859 | } else { | 1178 | lp = sp_list->next; |
| 860 | handle->pbe = NULL; | 1179 | free_image_page(sp_list, PG_UNSAFE_CLEAR); |
| 861 | swsusp_free(); | 1180 | sp_list = lp; |
| 862 | } | 1181 | } |
| 1182 | return 0; | ||
| 1183 | |||
| 1184 | Free: | ||
| 1185 | swsusp_free(); | ||
| 863 | return error; | 1186 | return error; |
| 864 | } | 1187 | } |
| 865 | 1188 | ||
| 866 | static void *get_buffer(struct snapshot_handle *handle) | 1189 | /** |
| 1190 | * get_buffer - compute the address that snapshot_write_next() should | ||
| 1191 | * set for its caller to write to. | ||
| 1192 | */ | ||
| 1193 | |||
| 1194 | static void *get_buffer(struct memory_bitmap *bm, struct chain_allocator *ca) | ||
| 867 | { | 1195 | { |
| 868 | struct pbe *pbe = handle->pbe, *last = handle->last_pbe; | 1196 | struct pbe *pbe; |
| 869 | struct page *page = virt_to_page(pbe->orig_address); | 1197 | struct page *page = pfn_to_page(memory_bm_next_pfn(bm)); |
| 870 | 1198 | ||
| 871 | if (PageNosave(page) && PageNosaveFree(page)) { | 1199 | if (PageNosave(page) && PageNosaveFree(page)) |
| 872 | /* | 1200 | /* We have allocated the "original" page frame and we can |
| 873 | * We have allocated the "original" page frame and we can | 1201 | * use it directly to store the loaded page. |
| 874 | * use it directly to store the read page | ||
| 875 | */ | 1202 | */ |
| 876 | pbe->address = 0; | 1203 | return page_address(page); |
| 877 | if (last && last->next) | 1204 | |
| 878 | last->next = NULL; | 1205 | /* The "original" page frame has not been allocated and we have to |
| 879 | return (void *)pbe->orig_address; | 1206 | * use a "safe" page frame to store the loaded page. |
| 880 | } | ||
| 881 | /* | ||
| 882 | * The "original" page frame has not been allocated and we have to | ||
| 883 | * use a "safe" page frame to store the read page | ||
| 884 | */ | 1207 | */ |
| 885 | pbe->address = (unsigned long)safe_pages; | 1208 | pbe = chain_alloc(ca, sizeof(struct pbe)); |
| 886 | safe_pages = safe_pages->next; | 1209 | if (!pbe) { |
| 887 | if (last) | 1210 | swsusp_free(); |
| 888 | last->next = pbe; | 1211 | return NULL; |
| 889 | handle->last_pbe = pbe; | 1212 | } |
| 1213 | pbe->orig_address = (unsigned long)page_address(page); | ||
| 1214 | pbe->address = (unsigned long)safe_pages_list; | ||
| 1215 | safe_pages_list = safe_pages_list->next; | ||
| 1216 | pbe->next = restore_pblist; | ||
| 1217 | restore_pblist = pbe; | ||
| 890 | return (void *)pbe->address; | 1218 | return (void *)pbe->address; |
| 891 | } | 1219 | } |
| 892 | 1220 | ||
| @@ -914,46 +1242,60 @@ static void *get_buffer(struct snapshot_handle *handle) | |||
| 914 | 1242 | ||
| 915 | int snapshot_write_next(struct snapshot_handle *handle, size_t count) | 1243 | int snapshot_write_next(struct snapshot_handle *handle, size_t count) |
| 916 | { | 1244 | { |
| 1245 | static struct chain_allocator ca; | ||
| 917 | int error = 0; | 1246 | int error = 0; |
| 918 | 1247 | ||
| 919 | if (handle->prev && handle->page > nr_meta_pages + nr_copy_pages) | 1248 | /* Check if we have already loaded the entire image */ |
| 1249 | if (handle->prev && handle->cur > nr_meta_pages + nr_copy_pages) | ||
| 920 | return 0; | 1250 | return 0; |
| 1251 | |||
| 921 | if (!buffer) { | 1252 | if (!buffer) { |
| 922 | /* This makes the buffer be freed by swsusp_free() */ | 1253 | /* This makes the buffer be freed by swsusp_free() */ |
| 923 | buffer = alloc_image_page(GFP_ATOMIC, 0); | 1254 | buffer = alloc_image_page(GFP_ATOMIC, PG_ANY); |
| 924 | if (!buffer) | 1255 | if (!buffer) |
| 925 | return -ENOMEM; | 1256 | return -ENOMEM; |
| 926 | } | 1257 | } |
| 927 | if (!handle->offset) | 1258 | if (!handle->offset) |
| 928 | handle->buffer = buffer; | 1259 | handle->buffer = buffer; |
| 929 | if (handle->prev < handle->page) { | 1260 | handle->sync_read = 1; |
| 930 | if (!handle->prev) { | 1261 | if (handle->prev < handle->cur) { |
| 931 | error = load_header(handle, (struct swsusp_info *)buffer); | 1262 | if (handle->prev == 0) { |
| 1263 | error = load_header(buffer); | ||
| 1264 | if (error) | ||
| 1265 | return error; | ||
| 1266 | |||
| 1267 | error = memory_bm_create(©_bm, GFP_ATOMIC, PG_ANY); | ||
| 932 | if (error) | 1268 | if (error) |
| 933 | return error; | 1269 | return error; |
| 1270 | |||
| 934 | } else if (handle->prev <= nr_meta_pages) { | 1271 | } else if (handle->prev <= nr_meta_pages) { |
| 935 | handle->pbe = unpack_orig_addresses(buffer, handle->pbe); | 1272 | unpack_orig_pfns(buffer, ©_bm); |
| 936 | if (!handle->pbe) { | 1273 | if (handle->prev == nr_meta_pages) { |
| 937 | error = prepare_image(handle); | 1274 | error = prepare_image(&orig_bm, ©_bm); |
| 938 | if (error) | 1275 | if (error) |
| 939 | return error; | 1276 | return error; |
| 940 | handle->pbe = pagedir_nosave; | 1277 | |
| 941 | handle->last_pbe = NULL; | 1278 | chain_init(&ca, GFP_ATOMIC, PG_SAFE); |
| 942 | handle->buffer = get_buffer(handle); | 1279 | memory_bm_position_reset(&orig_bm); |
| 1280 | restore_pblist = NULL; | ||
| 1281 | handle->buffer = get_buffer(&orig_bm, &ca); | ||
| 1282 | handle->sync_read = 0; | ||
| 1283 | if (!handle->buffer) | ||
| 1284 | return -ENOMEM; | ||
| 943 | } | 1285 | } |
| 944 | } else { | 1286 | } else { |
| 945 | handle->pbe = handle->pbe->next; | 1287 | handle->buffer = get_buffer(&orig_bm, &ca); |
| 946 | handle->buffer = get_buffer(handle); | 1288 | handle->sync_read = 0; |
| 947 | } | 1289 | } |
| 948 | handle->prev = handle->page; | 1290 | handle->prev = handle->cur; |
| 949 | } | 1291 | } |
| 950 | handle->buf_offset = handle->page_offset; | 1292 | handle->buf_offset = handle->cur_offset; |
| 951 | if (handle->page_offset + count >= PAGE_SIZE) { | 1293 | if (handle->cur_offset + count >= PAGE_SIZE) { |
| 952 | count = PAGE_SIZE - handle->page_offset; | 1294 | count = PAGE_SIZE - handle->cur_offset; |
| 953 | handle->page_offset = 0; | 1295 | handle->cur_offset = 0; |
| 954 | handle->page++; | 1296 | handle->cur++; |
| 955 | } else { | 1297 | } else { |
| 956 | handle->page_offset += count; | 1298 | handle->cur_offset += count; |
| 957 | } | 1299 | } |
| 958 | handle->offset += count; | 1300 | handle->offset += count; |
| 959 | return count; | 1301 | return count; |
| @@ -961,6 +1303,13 @@ int snapshot_write_next(struct snapshot_handle *handle, size_t count) | |||
| 961 | 1303 | ||
| 962 | int snapshot_image_loaded(struct snapshot_handle *handle) | 1304 | int snapshot_image_loaded(struct snapshot_handle *handle) |
| 963 | { | 1305 | { |
| 964 | return !(!handle->pbe || handle->pbe->next || !nr_copy_pages || | 1306 | return !(!nr_copy_pages || |
| 965 | handle->page <= nr_meta_pages + nr_copy_pages); | 1307 | handle->cur <= nr_meta_pages + nr_copy_pages); |
| 1308 | } | ||
| 1309 | |||
| 1310 | void snapshot_free_unused_memory(struct snapshot_handle *handle) | ||
| 1311 | { | ||
| 1312 | /* Free only if we have loaded the image entirely */ | ||
| 1313 | if (handle->prev && handle->cur > nr_meta_pages + nr_copy_pages) | ||
| 1314 | memory_bm_free(&orig_bm, PG_UNSAFE_CLEAR); | ||
| 966 | } | 1315 | } |
