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