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