]> git.proxmox.com Git - mirror_ubuntu-hirsute-kernel.git/blob - kernel/power/snapshot.c
PM / Hibernate: Add memory_rtree_find_bit function
[mirror_ubuntu-hirsute-kernel.git] / kernel / power / snapshot.c
1 /*
2 * linux/kernel/power/snapshot.c
3 *
4 * This file provides system snapshot/restore functionality for swsusp.
5 *
6 * Copyright (C) 1998-2005 Pavel Machek <pavel@ucw.cz>
7 * Copyright (C) 2006 Rafael J. Wysocki <rjw@sisk.pl>
8 *
9 * This file is released under the GPLv2.
10 *
11 */
12
13 #include <linux/version.h>
14 #include <linux/module.h>
15 #include <linux/mm.h>
16 #include <linux/suspend.h>
17 #include <linux/delay.h>
18 #include <linux/bitops.h>
19 #include <linux/spinlock.h>
20 #include <linux/kernel.h>
21 #include <linux/pm.h>
22 #include <linux/device.h>
23 #include <linux/init.h>
24 #include <linux/bootmem.h>
25 #include <linux/syscalls.h>
26 #include <linux/console.h>
27 #include <linux/highmem.h>
28 #include <linux/list.h>
29 #include <linux/slab.h>
30 #include <linux/compiler.h>
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
38 #include "power.h"
39
40 static int swsusp_page_is_free(struct page *);
41 static void swsusp_set_page_forbidden(struct page *);
42 static void swsusp_unset_page_forbidden(struct page *);
43
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 */
49 unsigned long reserved_size;
50
51 void __init hibernate_reserved_size_init(void)
52 {
53 reserved_size = SPARE_PAGES * PAGE_SIZE;
54 }
55
56 /*
57 * Preferred image size in bytes (tunable via /sys/power/image_size).
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.
61 */
62 unsigned long image_size;
63
64 void __init hibernate_image_size_init(void)
65 {
66 image_size = ((totalram_pages * 2) / 5) * PAGE_SIZE;
67 }
68
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 */
74 struct pbe *restore_pblist;
75
76 /* Pointer to an auxiliary buffer (1 page) */
77 static void *buffer;
78
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
82 * used before suspend. The unsafe pages have PageNosaveFree set
83 * and we count them using unsafe_pages.
84 *
85 * Each allocated image page is marked as PageNosave and PageNosaveFree
86 * so that swsusp_free() can release it.
87 */
88
89 #define PG_ANY 0
90 #define PG_SAFE 1
91 #define PG_UNSAFE_CLEAR 1
92 #define PG_UNSAFE_KEEP 0
93
94 static unsigned int allocated_unsafe_pages;
95
96 static void *get_image_page(gfp_t gfp_mask, int safe_needed)
97 {
98 void *res;
99
100 res = (void *)get_zeroed_page(gfp_mask);
101 if (safe_needed)
102 while (res && swsusp_page_is_free(virt_to_page(res))) {
103 /* The page is unsafe, mark it for swsusp_free() */
104 swsusp_set_page_forbidden(virt_to_page(res));
105 allocated_unsafe_pages++;
106 res = (void *)get_zeroed_page(gfp_mask);
107 }
108 if (res) {
109 swsusp_set_page_forbidden(virt_to_page(res));
110 swsusp_set_page_free(virt_to_page(res));
111 }
112 return res;
113 }
114
115 unsigned long get_safe_page(gfp_t gfp_mask)
116 {
117 return (unsigned long)get_image_page(gfp_mask, PG_SAFE);
118 }
119
120 static struct page *alloc_image_page(gfp_t gfp_mask)
121 {
122 struct page *page;
123
124 page = alloc_page(gfp_mask);
125 if (page) {
126 swsusp_set_page_forbidden(page);
127 swsusp_set_page_free(page);
128 }
129 return page;
130 }
131
132 /**
133 * free_image_page - free page represented by @addr, allocated with
134 * get_image_page (page flags set by it must be cleared)
135 */
136
137 static inline void free_image_page(void *addr, int clear_nosave_free)
138 {
139 struct page *page;
140
141 BUG_ON(!virt_addr_valid(addr));
142
143 page = virt_to_page(addr);
144
145 swsusp_unset_page_forbidden(page);
146 if (clear_nosave_free)
147 swsusp_unset_page_free(page);
148
149 __free_page(page);
150 }
151
152 /* struct linked_page is used to build chains of pages */
153
154 #define LINKED_PAGE_DATA_SIZE (PAGE_SIZE - sizeof(void *))
155
156 struct linked_page {
157 struct linked_page *next;
158 char data[LINKED_PAGE_DATA_SIZE];
159 } __packed;
160
161 static inline void
162 free_list_of_pages(struct linked_page *list, int clear_page_nosave)
163 {
164 while (list) {
165 struct linked_page *lp = list->next;
166
167 free_image_page(list, clear_page_nosave);
168 list = lp;
169 }
170 }
171
172 /**
173 * struct chain_allocator is used for allocating small objects out of
174 * a linked list of pages called 'the chain'.
175 *
176 * The chain grows each time when there is no room for a new object in
177 * the current page. The allocated objects cannot be freed individually.
178 * It is only possible to free them all at once, by freeing the entire
179 * chain.
180 *
181 * NOTE: The chain allocator may be inefficient if the allocated objects
182 * are not much smaller than PAGE_SIZE.
183 */
184
185 struct chain_allocator {
186 struct linked_page *chain; /* the chain */
187 unsigned int used_space; /* total size of objects allocated out
188 * of the current page
189 */
190 gfp_t gfp_mask; /* mask for allocating pages */
191 int safe_needed; /* if set, only "safe" pages are allocated */
192 };
193
194 static void
195 chain_init(struct chain_allocator *ca, gfp_t gfp_mask, int safe_needed)
196 {
197 ca->chain = NULL;
198 ca->used_space = LINKED_PAGE_DATA_SIZE;
199 ca->gfp_mask = gfp_mask;
200 ca->safe_needed = safe_needed;
201 }
202
203 static void *chain_alloc(struct chain_allocator *ca, unsigned int size)
204 {
205 void *ret;
206
207 if (LINKED_PAGE_DATA_SIZE - ca->used_space < size) {
208 struct linked_page *lp;
209
210 lp = get_image_page(ca->gfp_mask, ca->safe_needed);
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
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
230 * represent each blocks of bitmap in which information is stored.
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
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.
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.
262 */
263
264 #define BM_END_OF_MAP (~0UL)
265
266 #define BM_BITS_PER_BLOCK (PAGE_SIZE * BITS_PER_BYTE)
267 #define BM_BLOCK_SHIFT (PAGE_SHIFT + 3)
268 #define BM_BLOCK_MASK ((1UL << BM_BLOCK_SHIFT) - 1)
269
270 struct bm_block {
271 struct list_head hook; /* hook into a list of bitmap blocks */
272 unsigned long start_pfn; /* pfn represented by the first bit */
273 unsigned long end_pfn; /* pfn represented by the last bit plus 1 */
274 unsigned long *data; /* bitmap representing pages */
275 };
276
277 static inline unsigned long bm_block_bits(struct bm_block *bb)
278 {
279 return bb->end_pfn - bb->start_pfn;
280 }
281
282 /*
283 * struct rtree_node is a wrapper struct to link the nodes
284 * of the rtree together for easy linear iteration over
285 * bits and easy freeing
286 */
287 struct rtree_node {
288 struct list_head list;
289 unsigned long *data;
290 };
291
292 /*
293 * struct mem_zone_bm_rtree represents a bitmap used for one
294 * populated memory zone.
295 */
296 struct mem_zone_bm_rtree {
297 struct list_head list; /* Link Zones together */
298 struct list_head nodes; /* Radix Tree inner nodes */
299 struct list_head leaves; /* Radix Tree leaves */
300 unsigned long start_pfn; /* Zone start page frame */
301 unsigned long end_pfn; /* Zone end page frame + 1 */
302 struct rtree_node *rtree; /* Radix Tree Root */
303 int levels; /* Number of Radix Tree Levels */
304 unsigned int blocks; /* Number of Bitmap Blocks */
305 };
306
307 /* strcut bm_position is used for browsing memory bitmaps */
308
309 struct bm_position {
310 struct bm_block *block;
311 int bit;
312 };
313
314 struct memory_bitmap {
315 struct list_head zones;
316 struct list_head blocks; /* list of bitmap blocks */
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
326 #define BM_ENTRIES_PER_LEVEL (PAGE_SIZE / sizeof(unsigned long))
327 #if BITS_PER_LONG == 32
328 #define BM_RTREE_LEVEL_SHIFT (PAGE_SHIFT - 2)
329 #else
330 #define BM_RTREE_LEVEL_SHIFT (PAGE_SHIFT - 3)
331 #endif
332 #define BM_RTREE_LEVEL_MASK ((1UL << BM_RTREE_LEVEL_SHIFT) - 1)
333
334 /*
335 * alloc_rtree_node - Allocate a new node and add it to the radix tree.
336 *
337 * This function is used to allocate inner nodes as well as the
338 * leave nodes of the radix tree. It also adds the node to the
339 * corresponding linked list passed in by the *list parameter.
340 */
341 static struct rtree_node *alloc_rtree_node(gfp_t gfp_mask, int safe_needed,
342 struct chain_allocator *ca,
343 struct list_head *list)
344 {
345 struct rtree_node *node;
346
347 node = chain_alloc(ca, sizeof(struct rtree_node));
348 if (!node)
349 return NULL;
350
351 node->data = get_image_page(gfp_mask, safe_needed);
352 if (!node->data)
353 return NULL;
354
355 list_add_tail(&node->list, list);
356
357 return node;
358 }
359
360 /*
361 * add_rtree_block - Add a new leave node to the radix tree
362 *
363 * The leave nodes need to be allocated in order to keep the leaves
364 * linked list in order. This is guaranteed by the zone->blocks
365 * counter.
366 */
367 static int add_rtree_block(struct mem_zone_bm_rtree *zone, gfp_t gfp_mask,
368 int safe_needed, struct chain_allocator *ca)
369 {
370 struct rtree_node *node, *block, **dst;
371 unsigned int levels_needed, block_nr;
372 int i;
373
374 block_nr = zone->blocks;
375 levels_needed = 0;
376
377 /* How many levels do we need for this block nr? */
378 while (block_nr) {
379 levels_needed += 1;
380 block_nr >>= BM_RTREE_LEVEL_SHIFT;
381 }
382
383 /* Make sure the rtree has enough levels */
384 for (i = zone->levels; i < levels_needed; i++) {
385 node = alloc_rtree_node(gfp_mask, safe_needed, ca,
386 &zone->nodes);
387 if (!node)
388 return -ENOMEM;
389
390 node->data[0] = (unsigned long)zone->rtree;
391 zone->rtree = node;
392 zone->levels += 1;
393 }
394
395 /* Allocate new block */
396 block = alloc_rtree_node(gfp_mask, safe_needed, ca, &zone->leaves);
397 if (!block)
398 return -ENOMEM;
399
400 /* Now walk the rtree to insert the block */
401 node = zone->rtree;
402 dst = &zone->rtree;
403 block_nr = zone->blocks;
404 for (i = zone->levels; i > 0; i--) {
405 int index;
406
407 if (!node) {
408 node = alloc_rtree_node(gfp_mask, safe_needed, ca,
409 &zone->nodes);
410 if (!node)
411 return -ENOMEM;
412 *dst = node;
413 }
414
415 index = block_nr >> ((i - 1) * BM_RTREE_LEVEL_SHIFT);
416 index &= BM_RTREE_LEVEL_MASK;
417 dst = (struct rtree_node **)&((*dst)->data[index]);
418 node = *dst;
419 }
420
421 zone->blocks += 1;
422 *dst = block;
423
424 return 0;
425 }
426
427 static void free_zone_bm_rtree(struct mem_zone_bm_rtree *zone,
428 int clear_nosave_free);
429
430 /*
431 * create_zone_bm_rtree - create a radix tree for one zone
432 *
433 * Allocated the mem_zone_bm_rtree structure and initializes it.
434 * This function also allocated and builds the radix tree for the
435 * zone.
436 */
437 static struct mem_zone_bm_rtree *
438 create_zone_bm_rtree(gfp_t gfp_mask, int safe_needed,
439 struct chain_allocator *ca,
440 unsigned long start, unsigned long end)
441 {
442 struct mem_zone_bm_rtree *zone;
443 unsigned int i, nr_blocks;
444 unsigned long pages;
445
446 pages = end - start;
447 zone = chain_alloc(ca, sizeof(struct mem_zone_bm_rtree));
448 if (!zone)
449 return NULL;
450
451 INIT_LIST_HEAD(&zone->nodes);
452 INIT_LIST_HEAD(&zone->leaves);
453 zone->start_pfn = start;
454 zone->end_pfn = end;
455 nr_blocks = DIV_ROUND_UP(pages, BM_BITS_PER_BLOCK);
456
457 for (i = 0; i < nr_blocks; i++) {
458 if (add_rtree_block(zone, gfp_mask, safe_needed, ca)) {
459 free_zone_bm_rtree(zone, PG_UNSAFE_CLEAR);
460 return NULL;
461 }
462 }
463
464 return zone;
465 }
466
467 /*
468 * free_zone_bm_rtree - Free the memory of the radix tree
469 *
470 * Free all node pages of the radix tree. The mem_zone_bm_rtree
471 * structure itself is not freed here nor are the rtree_node
472 * structs.
473 */
474 static void free_zone_bm_rtree(struct mem_zone_bm_rtree *zone,
475 int clear_nosave_free)
476 {
477 struct rtree_node *node;
478
479 list_for_each_entry(node, &zone->nodes, list)
480 free_image_page(node->data, clear_nosave_free);
481
482 list_for_each_entry(node, &zone->leaves, list)
483 free_image_page(node->data, clear_nosave_free);
484 }
485
486 static void memory_bm_position_reset(struct memory_bitmap *bm)
487 {
488 bm->cur.block = list_entry(bm->blocks.next, struct bm_block, hook);
489 bm->cur.bit = 0;
490 }
491
492 static void memory_bm_free(struct memory_bitmap *bm, int clear_nosave_free);
493
494 /**
495 * create_bm_block_list - create a list of block bitmap objects
496 * @pages - number of pages to track
497 * @list - list to put the allocated blocks into
498 * @ca - chain allocator to be used for allocating memory
499 */
500 static int create_bm_block_list(unsigned long pages,
501 struct list_head *list,
502 struct chain_allocator *ca)
503 {
504 unsigned int nr_blocks = DIV_ROUND_UP(pages, BM_BITS_PER_BLOCK);
505
506 while (nr_blocks-- > 0) {
507 struct bm_block *bb;
508
509 bb = chain_alloc(ca, sizeof(struct bm_block));
510 if (!bb)
511 return -ENOMEM;
512 list_add(&bb->hook, list);
513 }
514
515 return 0;
516 }
517
518 struct mem_extent {
519 struct list_head hook;
520 unsigned long start;
521 unsigned long end;
522 };
523
524 /**
525 * free_mem_extents - free a list of memory extents
526 * @list - list of extents to empty
527 */
528 static void free_mem_extents(struct list_head *list)
529 {
530 struct mem_extent *ext, *aux;
531
532 list_for_each_entry_safe(ext, aux, list, hook) {
533 list_del(&ext->hook);
534 kfree(ext);
535 }
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 */
544 static int create_mem_extents(struct list_head *list, gfp_t gfp_mask)
545 {
546 struct zone *zone;
547
548 INIT_LIST_HEAD(list);
549
550 for_each_populated_zone(zone) {
551 unsigned long zone_start, zone_end;
552 struct mem_extent *ext, *cur, *aux;
553
554 zone_start = zone->zone_start_pfn;
555 zone_end = zone_end_pfn(zone);
556
557 list_for_each_entry(ext, list, hook)
558 if (zone_start <= ext->end)
559 break;
560
561 if (&ext->hook == list || zone_end < ext->start) {
562 /* New extent is necessary */
563 struct mem_extent *new_ext;
564
565 new_ext = kzalloc(sizeof(struct mem_extent), gfp_mask);
566 if (!new_ext) {
567 free_mem_extents(list);
568 return -ENOMEM;
569 }
570 new_ext->start = zone_start;
571 new_ext->end = zone_end;
572 list_add_tail(&new_ext->hook, &ext->hook);
573 continue;
574 }
575
576 /* Merge this zone's range of PFNs with the existing one */
577 if (zone_start < ext->start)
578 ext->start = zone_start;
579 if (zone_end > ext->end)
580 ext->end = zone_end;
581
582 /* More merging may be possible */
583 cur = ext;
584 list_for_each_entry_safe_continue(cur, aux, list, hook) {
585 if (zone_end < cur->start)
586 break;
587 if (zone_end < cur->end)
588 ext->end = cur->end;
589 list_del(&cur->hook);
590 kfree(cur);
591 }
592 }
593
594 return 0;
595 }
596
597 /**
598 * memory_bm_create - allocate memory for a memory bitmap
599 */
600 static int
601 memory_bm_create(struct memory_bitmap *bm, gfp_t gfp_mask, int safe_needed)
602 {
603 struct chain_allocator ca;
604 struct list_head mem_extents;
605 struct mem_extent *ext;
606 int error;
607
608 chain_init(&ca, gfp_mask, safe_needed);
609 INIT_LIST_HEAD(&bm->blocks);
610 INIT_LIST_HEAD(&bm->zones);
611
612 error = create_mem_extents(&mem_extents, gfp_mask);
613 if (error)
614 return error;
615
616 list_for_each_entry(ext, &mem_extents, hook) {
617 struct mem_zone_bm_rtree *zone;
618 struct bm_block *bb;
619 unsigned long pfn = ext->start;
620 unsigned long pages = ext->end - ext->start;
621
622 bb = list_entry(bm->blocks.prev, struct bm_block, hook);
623
624 error = create_bm_block_list(pages, bm->blocks.prev, &ca);
625 if (error)
626 goto Error;
627
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 }
634
635 bb->start_pfn = pfn;
636 if (pages >= BM_BITS_PER_BLOCK) {
637 pfn += BM_BITS_PER_BLOCK;
638 pages -= BM_BITS_PER_BLOCK;
639 } else {
640 /* This is executed only once in the loop */
641 pfn += pages;
642 }
643 bb->end_pfn = pfn;
644 }
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);
651 }
652
653 bm->p_list = ca.chain;
654 memory_bm_position_reset(bm);
655 Exit:
656 free_mem_extents(&mem_extents);
657 return error;
658
659 Error:
660 bm->p_list = ca.chain;
661 memory_bm_free(bm, PG_UNSAFE_CLEAR);
662 goto Exit;
663 }
664
665 /**
666 * memory_bm_free - free memory occupied by the memory bitmap @bm
667 */
668 static void memory_bm_free(struct memory_bitmap *bm, int clear_nosave_free)
669 {
670 struct mem_zone_bm_rtree *zone;
671 struct bm_block *bb;
672
673 list_for_each_entry(bb, &bm->blocks, hook)
674 if (bb->data)
675 free_image_page(bb->data, clear_nosave_free);
676
677 list_for_each_entry(zone, &bm->zones, list)
678 free_zone_bm_rtree(zone, clear_nosave_free);
679
680 free_list_of_pages(bm->p_list, clear_nosave_free);
681
682 INIT_LIST_HEAD(&bm->zones);
683 INIT_LIST_HEAD(&bm->blocks);
684 }
685
686 /**
687 * memory_bm_find_bit - find the bit in the bitmap @bm that corresponds
688 * to given pfn. The cur_zone_bm member of @bm and the cur_block member
689 * of @bm->cur_zone_bm are updated.
690 */
691 static int memory_bm_find_bit(struct memory_bitmap *bm, unsigned long pfn,
692 void **addr, unsigned int *bit_nr)
693 {
694 struct bm_block *bb;
695
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;
701 if (pfn < bb->start_pfn)
702 list_for_each_entry_continue_reverse(bb, &bm->blocks, hook)
703 if (pfn >= bb->start_pfn)
704 break;
705
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;
710
711 if (&bb->hook == &bm->blocks)
712 return -EFAULT;
713
714 /* The block has been found */
715 bm->cur.block = bb;
716 pfn -= bb->start_pfn;
717 bm->cur.bit = pfn + 1;
718 *bit_nr = pfn;
719 *addr = bb->data;
720 return 0;
721 }
722
723 /*
724 * memory_rtree_find_bit - Find the bit for pfn in the memory
725 * bitmap
726 *
727 * Walks the radix tree to find the page which contains the bit for
728 * pfn and returns the bit position in **addr and *bit_nr.
729 */
730 static int memory_rtree_find_bit(struct memory_bitmap *bm, unsigned long pfn,
731 void **addr, unsigned int *bit_nr)
732 {
733 struct mem_zone_bm_rtree *curr, *zone;
734 struct rtree_node *node;
735 int i, block_nr;
736
737 zone = NULL;
738
739 /* Find the right zone */
740 list_for_each_entry(curr, &bm->zones, list) {
741 if (pfn >= curr->start_pfn && pfn < curr->end_pfn) {
742 zone = curr;
743 break;
744 }
745 }
746
747 if (!zone)
748 return -EFAULT;
749
750 /*
751 * We have a zone. Now walk the radix tree to find the leave
752 * node for our pfn.
753 */
754 node = zone->rtree;
755 block_nr = (pfn - zone->start_pfn) >> BM_BLOCK_SHIFT;
756
757 for (i = zone->levels; i > 0; i--) {
758 int index;
759
760 index = block_nr >> ((i - 1) * BM_RTREE_LEVEL_SHIFT);
761 index &= BM_RTREE_LEVEL_MASK;
762 BUG_ON(node->data[index] == 0);
763 node = (struct rtree_node *)node->data[index];
764 }
765
766 /* Set return values */
767 *addr = node->data;
768 *bit_nr = (pfn - zone->start_pfn) & BM_BLOCK_MASK;
769
770 return 0;
771 }
772
773 static void memory_bm_set_bit(struct memory_bitmap *bm, unsigned long pfn)
774 {
775 void *addr;
776 unsigned int bit;
777 int error;
778
779 error = memory_bm_find_bit(bm, pfn, &addr, &bit);
780 BUG_ON(error);
781 set_bit(bit, addr);
782
783 error = memory_rtree_find_bit(bm, pfn, &addr, &bit);
784 BUG_ON(error);
785 set_bit(bit, addr);
786 }
787
788 static int mem_bm_set_bit_check(struct memory_bitmap *bm, unsigned long pfn)
789 {
790 void *addr;
791 unsigned int bit;
792 int error;
793
794 error = memory_bm_find_bit(bm, pfn, &addr, &bit);
795 if (!error)
796 set_bit(bit, addr);
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
804 return error;
805 }
806
807 static void memory_bm_clear_bit(struct memory_bitmap *bm, unsigned long pfn)
808 {
809 void *addr;
810 unsigned int bit;
811 int error;
812
813 error = memory_bm_find_bit(bm, pfn, &addr, &bit);
814 BUG_ON(error);
815 clear_bit(bit, addr);
816
817 error = memory_rtree_find_bit(bm, pfn, &addr, &bit);
818 BUG_ON(error);
819 clear_bit(bit, addr);
820 }
821
822 static int memory_bm_test_bit(struct memory_bitmap *bm, unsigned long pfn)
823 {
824 void *addr;
825 unsigned int bit;
826 int error, error2;
827 int v;
828
829 error = memory_bm_find_bit(bm, pfn, &addr, &bit);
830 BUG_ON(error);
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;
839 }
840
841 static bool memory_bm_pfn_present(struct memory_bitmap *bm, unsigned long pfn)
842 {
843 void *addr;
844 unsigned int bit;
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));
850
851 return present;
852 }
853
854 /**
855 * memory_bm_next_pfn - find the pfn that corresponds to the next set bit
856 * in the bitmap @bm. If the pfn cannot be found, BM_END_OF_MAP is
857 * returned.
858 *
859 * It is required to run memory_bm_position_reset() before the first call to
860 * this function.
861 */
862
863 static unsigned long memory_bm_next_pfn(struct memory_bitmap *bm)
864 {
865 struct bm_block *bb;
866 int bit;
867
868 bb = bm->cur.block;
869 do {
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
880 memory_bm_position_reset(bm);
881 return BM_END_OF_MAP;
882
883 Return_pfn:
884 bm->cur.bit = bit + 1;
885 return bb->start_pfn + bit;
886 }
887
888 /**
889 * This structure represents a range of page frames the contents of which
890 * should not be saved during the suspend.
891 */
892
893 struct nosave_region {
894 struct list_head list;
895 unsigned long start_pfn;
896 unsigned long end_pfn;
897 };
898
899 static LIST_HEAD(nosave_regions);
900
901 /**
902 * register_nosave_region - register a range of page frames the contents
903 * of which should not be saved during the suspend (to be used in the early
904 * initialization code)
905 */
906
907 void __init
908 __register_nosave_region(unsigned long start_pfn, unsigned long end_pfn,
909 int use_kmalloc)
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 }
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 */
931 region = memblock_virt_alloc(sizeof(struct nosave_region), 0);
932 region->start_pfn = start_pfn;
933 region->end_pfn = end_pfn;
934 list_add_tail(&region->list, &nosave_regions);
935 Report:
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);
939 }
940
941 /*
942 * Set bits in this map correspond to the page frames the contents of which
943 * should not be saved during the suspend.
944 */
945 static struct memory_bitmap *forbidden_pages_map;
946
947 /* Set bits in this map correspond to free page frames. */
948 static struct memory_bitmap *free_pages_map;
949
950 /*
951 * Each page frame allocated for creating the image is marked by setting the
952 * corresponding bits in forbidden_pages_map and free_pages_map simultaneously
953 */
954
955 void swsusp_set_page_free(struct page *page)
956 {
957 if (free_pages_map)
958 memory_bm_set_bit(free_pages_map, page_to_pfn(page));
959 }
960
961 static int swsusp_page_is_free(struct page *page)
962 {
963 return free_pages_map ?
964 memory_bm_test_bit(free_pages_map, page_to_pfn(page)) : 0;
965 }
966
967 void swsusp_unset_page_free(struct page *page)
968 {
969 if (free_pages_map)
970 memory_bm_clear_bit(free_pages_map, page_to_pfn(page));
971 }
972
973 static void swsusp_set_page_forbidden(struct page *page)
974 {
975 if (forbidden_pages_map)
976 memory_bm_set_bit(forbidden_pages_map, page_to_pfn(page));
977 }
978
979 int swsusp_page_is_forbidden(struct page *page)
980 {
981 return forbidden_pages_map ?
982 memory_bm_test_bit(forbidden_pages_map, page_to_pfn(page)) : 0;
983 }
984
985 static void swsusp_unset_page_forbidden(struct page *page)
986 {
987 if (forbidden_pages_map)
988 memory_bm_clear_bit(forbidden_pages_map, page_to_pfn(page));
989 }
990
991 /**
992 * mark_nosave_pages - set bits corresponding to the page frames the
993 * contents of which should not be saved in a given bitmap.
994 */
995
996 static void mark_nosave_pages(struct memory_bitmap *bm)
997 {
998 struct nosave_region *region;
999
1000 if (list_empty(&nosave_regions))
1001 return;
1002
1003 list_for_each_entry(region, &nosave_regions, list) {
1004 unsigned long pfn;
1005
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);
1010
1011 for (pfn = region->start_pfn; pfn < region->end_pfn; pfn++)
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 }
1021 }
1022 }
1023
1024 /**
1025 * create_basic_memory_bitmaps - create bitmaps needed for marking page
1026 * frames that should not be saved and free page frames. The pointers
1027 * forbidden_pages_map and free_pages_map are only modified if everything
1028 * goes well, because we don't want the bits to be used before both bitmaps
1029 * are set up.
1030 */
1031
1032 int create_basic_memory_bitmaps(void)
1033 {
1034 struct memory_bitmap *bm1, *bm2;
1035 int error = 0;
1036
1037 if (forbidden_pages_map && free_pages_map)
1038 return 0;
1039 else
1040 BUG_ON(forbidden_pages_map || free_pages_map);
1041
1042 bm1 = kzalloc(sizeof(struct memory_bitmap), GFP_KERNEL);
1043 if (!bm1)
1044 return -ENOMEM;
1045
1046 error = memory_bm_create(bm1, GFP_KERNEL, PG_ANY);
1047 if (error)
1048 goto Free_first_object;
1049
1050 bm2 = kzalloc(sizeof(struct memory_bitmap), GFP_KERNEL);
1051 if (!bm2)
1052 goto Free_first_bitmap;
1053
1054 error = memory_bm_create(bm2, GFP_KERNEL, PG_ANY);
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
1062 pr_debug("PM: Basic memory bitmaps created\n");
1063
1064 return 0;
1065
1066 Free_second_object:
1067 kfree(bm2);
1068 Free_first_bitmap:
1069 memory_bm_free(bm1, PG_UNSAFE_CLEAR);
1070 Free_first_object:
1071 kfree(bm1);
1072 return -ENOMEM;
1073 }
1074
1075 /**
1076 * free_basic_memory_bitmaps - free memory bitmaps allocated by
1077 * create_basic_memory_bitmaps(). The auxiliary pointers are necessary
1078 * so that the bitmaps themselves are not referred to while they are being
1079 * freed.
1080 */
1081
1082 void free_basic_memory_bitmaps(void)
1083 {
1084 struct memory_bitmap *bm1, *bm2;
1085
1086 if (WARN_ON(!(forbidden_pages_map && free_pages_map)))
1087 return;
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
1098 pr_debug("PM: Basic memory bitmaps freed\n");
1099 }
1100
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
1107 unsigned int snapshot_additional_pages(struct zone *zone)
1108 {
1109 unsigned int rtree, nodes;
1110 unsigned int res;
1111
1112 res = DIV_ROUND_UP(zone->spanned_pages, BM_BITS_PER_BLOCK);
1113 res += DIV_ROUND_UP(res * sizeof(struct bm_block),
1114 LINKED_PAGE_DATA_SIZE);
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);
1124 }
1125
1126 #ifdef CONFIG_HIGHMEM
1127 /**
1128 * count_free_highmem_pages - compute the total number of free highmem
1129 * pages, system-wide.
1130 */
1131
1132 static unsigned int count_free_highmem_pages(void)
1133 {
1134 struct zone *zone;
1135 unsigned int cnt = 0;
1136
1137 for_each_populated_zone(zone)
1138 if (is_highmem(zone))
1139 cnt += zone_page_state(zone, NR_FREE_PAGES);
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 */
1151 static struct page *saveable_highmem_page(struct zone *zone, unsigned long pfn)
1152 {
1153 struct page *page;
1154
1155 if (!pfn_valid(pfn))
1156 return NULL;
1157
1158 page = pfn_to_page(pfn);
1159 if (page_zone(page) != zone)
1160 return NULL;
1161
1162 BUG_ON(!PageHighMem(page));
1163
1164 if (swsusp_page_is_forbidden(page) || swsusp_page_is_free(page) ||
1165 PageReserved(page))
1166 return NULL;
1167
1168 if (page_is_guard(page))
1169 return NULL;
1170
1171 return page;
1172 }
1173
1174 /**
1175 * count_highmem_pages - compute the total number of saveable highmem
1176 * pages.
1177 */
1178
1179 static unsigned int count_highmem_pages(void)
1180 {
1181 struct zone *zone;
1182 unsigned int n = 0;
1183
1184 for_each_populated_zone(zone) {
1185 unsigned long pfn, max_zone_pfn;
1186
1187 if (!is_highmem(zone))
1188 continue;
1189
1190 mark_free_pages(zone);
1191 max_zone_pfn = zone_end_pfn(zone);
1192 for (pfn = zone->zone_start_pfn; pfn < max_zone_pfn; pfn++)
1193 if (saveable_highmem_page(zone, pfn))
1194 n++;
1195 }
1196 return n;
1197 }
1198 #else
1199 static inline void *saveable_highmem_page(struct zone *z, unsigned long p)
1200 {
1201 return NULL;
1202 }
1203 #endif /* CONFIG_HIGHMEM */
1204
1205 /**
1206 * saveable_page - Determine whether a non-highmem page should be included
1207 * in the suspend image.
1208 *
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.
1212 */
1213 static struct page *saveable_page(struct zone *zone, unsigned long pfn)
1214 {
1215 struct page *page;
1216
1217 if (!pfn_valid(pfn))
1218 return NULL;
1219
1220 page = pfn_to_page(pfn);
1221 if (page_zone(page) != zone)
1222 return NULL;
1223
1224 BUG_ON(PageHighMem(page));
1225
1226 if (swsusp_page_is_forbidden(page) || swsusp_page_is_free(page))
1227 return NULL;
1228
1229 if (PageReserved(page)
1230 && (!kernel_page_present(page) || pfn_is_nosave(pfn)))
1231 return NULL;
1232
1233 if (page_is_guard(page))
1234 return NULL;
1235
1236 return page;
1237 }
1238
1239 /**
1240 * count_data_pages - compute the total number of saveable non-highmem
1241 * pages.
1242 */
1243
1244 static unsigned int count_data_pages(void)
1245 {
1246 struct zone *zone;
1247 unsigned long pfn, max_zone_pfn;
1248 unsigned int n = 0;
1249
1250 for_each_populated_zone(zone) {
1251 if (is_highmem(zone))
1252 continue;
1253
1254 mark_free_pages(zone);
1255 max_zone_pfn = zone_end_pfn(zone);
1256 for (pfn = zone->zone_start_pfn; pfn < max_zone_pfn; pfn++)
1257 if (saveable_page(zone, pfn))
1258 n++;
1259 }
1260 return n;
1261 }
1262
1263 /* This is needed, because copy_page and memcpy are not usable for copying
1264 * task structs.
1265 */
1266 static inline void do_copy_page(long *dst, long *src)
1267 {
1268 int n;
1269
1270 for (n = PAGE_SIZE / sizeof(long); n; n--)
1271 *dst++ = *src++;
1272 }
1273
1274
1275 /**
1276 * safe_copy_page - check if the page we are going to copy is marked as
1277 * present in the kernel page tables (this always is the case if
1278 * CONFIG_DEBUG_PAGEALLOC is not set and in that case
1279 * kernel_page_present() always returns 'true').
1280 */
1281 static void safe_copy_page(void *dst, struct page *s_page)
1282 {
1283 if (kernel_page_present(s_page)) {
1284 do_copy_page(dst, page_address(s_page));
1285 } else {
1286 kernel_map_pages(s_page, 1, 1);
1287 do_copy_page(dst, page_address(s_page));
1288 kernel_map_pages(s_page, 1, 0);
1289 }
1290 }
1291
1292
1293 #ifdef CONFIG_HIGHMEM
1294 static inline struct page *
1295 page_is_saveable(struct zone *zone, unsigned long pfn)
1296 {
1297 return is_highmem(zone) ?
1298 saveable_highmem_page(zone, pfn) : saveable_page(zone, pfn);
1299 }
1300
1301 static void copy_data_page(unsigned long dst_pfn, unsigned long src_pfn)
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)) {
1309 src = kmap_atomic(s_page);
1310 dst = kmap_atomic(d_page);
1311 do_copy_page(dst, src);
1312 kunmap_atomic(dst);
1313 kunmap_atomic(src);
1314 } else {
1315 if (PageHighMem(d_page)) {
1316 /* Page pointed to by src may contain some kernel
1317 * data modified by kmap_atomic()
1318 */
1319 safe_copy_page(buffer, s_page);
1320 dst = kmap_atomic(d_page);
1321 copy_page(dst, buffer);
1322 kunmap_atomic(dst);
1323 } else {
1324 safe_copy_page(page_address(d_page), s_page);
1325 }
1326 }
1327 }
1328 #else
1329 #define page_is_saveable(zone, pfn) saveable_page(zone, pfn)
1330
1331 static inline void copy_data_page(unsigned long dst_pfn, unsigned long src_pfn)
1332 {
1333 safe_copy_page(page_address(pfn_to_page(dst_pfn)),
1334 pfn_to_page(src_pfn));
1335 }
1336 #endif /* CONFIG_HIGHMEM */
1337
1338 static void
1339 copy_data_pages(struct memory_bitmap *copy_bm, struct memory_bitmap *orig_bm)
1340 {
1341 struct zone *zone;
1342 unsigned long pfn;
1343
1344 for_each_populated_zone(zone) {
1345 unsigned long max_zone_pfn;
1346
1347 mark_free_pages(zone);
1348 max_zone_pfn = zone_end_pfn(zone);
1349 for (pfn = zone->zone_start_pfn; pfn < max_zone_pfn; pfn++)
1350 if (page_is_saveable(zone, pfn))
1351 memory_bm_set_bit(orig_bm, pfn);
1352 }
1353 memory_bm_position_reset(orig_bm);
1354 memory_bm_position_reset(copy_bm);
1355 for(;;) {
1356 pfn = memory_bm_next_pfn(orig_bm);
1357 if (unlikely(pfn == BM_END_OF_MAP))
1358 break;
1359 copy_data_page(memory_bm_next_pfn(copy_bm), pfn);
1360 }
1361 }
1362
1363 /* Total number of image pages */
1364 static unsigned int nr_copy_pages;
1365 /* Number of pages needed for saving the original pfns of the image pages */
1366 static unsigned int nr_meta_pages;
1367 /*
1368 * Numbers of normal and highmem page frames allocated for hibernation image
1369 * before suspending devices.
1370 */
1371 unsigned int alloc_normal, alloc_highmem;
1372 /*
1373 * Memory bitmap used for marking saveable pages (during hibernation) or
1374 * hibernation image pages (during restore)
1375 */
1376 static struct memory_bitmap orig_bm;
1377 /*
1378 * Memory bitmap used during hibernation for marking allocated page frames that
1379 * will contain copies of saveable pages. During restore it is initially used
1380 * for marking hibernation image pages, but then the set bits from it are
1381 * duplicated in @orig_bm and it is released. On highmem systems it is next
1382 * used for marking "safe" highmem pages, but it has to be reinitialized for
1383 * this purpose.
1384 */
1385 static struct memory_bitmap copy_bm;
1386
1387 /**
1388 * swsusp_free - free pages allocated for the suspend.
1389 *
1390 * Suspend pages are alocated before the atomic copy is made, so we
1391 * need to release them after the resume.
1392 */
1393
1394 void swsusp_free(void)
1395 {
1396 struct zone *zone;
1397 unsigned long pfn, max_zone_pfn;
1398
1399 for_each_populated_zone(zone) {
1400 max_zone_pfn = zone_end_pfn(zone);
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
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);
1409 __free_page(page);
1410 }
1411 }
1412 }
1413 nr_copy_pages = 0;
1414 nr_meta_pages = 0;
1415 restore_pblist = NULL;
1416 buffer = NULL;
1417 alloc_normal = 0;
1418 alloc_highmem = 0;
1419 }
1420
1421 /* Helper functions used for the shrinking of memory. */
1422
1423 #define GFP_IMAGE (GFP_KERNEL | __GFP_NOWARN)
1424
1425 /**
1426 * preallocate_image_pages - Allocate a number of pages for hibernation image
1427 * @nr_pages: Number of page frames to allocate.
1428 * @mask: GFP flags to use for the allocation.
1429 *
1430 * Return value: Number of page frames actually allocated
1431 */
1432 static unsigned long preallocate_image_pages(unsigned long nr_pages, gfp_t mask)
1433 {
1434 unsigned long nr_alloc = 0;
1435
1436 while (nr_pages > 0) {
1437 struct page *page;
1438
1439 page = alloc_image_page(mask);
1440 if (!page)
1441 break;
1442 memory_bm_set_bit(&copy_bm, page_to_pfn(page));
1443 if (PageHighMem(page))
1444 alloc_highmem++;
1445 else
1446 alloc_normal++;
1447 nr_pages--;
1448 nr_alloc++;
1449 }
1450
1451 return nr_alloc;
1452 }
1453
1454 static unsigned long preallocate_image_memory(unsigned long nr_pages,
1455 unsigned long avail_normal)
1456 {
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);
1467 }
1468
1469 #ifdef CONFIG_HIGHMEM
1470 static unsigned long preallocate_image_highmem(unsigned long nr_pages)
1471 {
1472 return preallocate_image_pages(nr_pages, GFP_IMAGE | __GFP_HIGHMEM);
1473 }
1474
1475 /**
1476 * __fraction - Compute (an approximation of) x * (multiplier / base)
1477 */
1478 static unsigned long __fraction(u64 x, u64 multiplier, u64 base)
1479 {
1480 x *= multiplier;
1481 do_div(x, base);
1482 return (unsigned long)x;
1483 }
1484
1485 static unsigned long preallocate_highmem_fraction(unsigned long nr_pages,
1486 unsigned long highmem,
1487 unsigned long total)
1488 {
1489 unsigned long alloc = __fraction(nr_pages, highmem, total);
1490
1491 return preallocate_image_pages(alloc, GFP_IMAGE | __GFP_HIGHMEM);
1492 }
1493 #else /* CONFIG_HIGHMEM */
1494 static inline unsigned long preallocate_image_highmem(unsigned long nr_pages)
1495 {
1496 return 0;
1497 }
1498
1499 static inline unsigned long preallocate_highmem_fraction(unsigned long nr_pages,
1500 unsigned long highmem,
1501 unsigned long total)
1502 {
1503 return 0;
1504 }
1505 #endif /* CONFIG_HIGHMEM */
1506
1507 /**
1508 * free_unnecessary_pages - Release preallocated pages not needed for the image
1509 */
1510 static void free_unnecessary_pages(void)
1511 {
1512 unsigned long save, to_free_normal, to_free_highmem;
1513
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;
1525 } else {
1526 to_free_highmem = 0;
1527 save -= alloc_highmem;
1528 if (to_free_normal > save)
1529 to_free_normal -= save;
1530 else
1531 to_free_normal = 0;
1532 }
1533
1534 memory_bm_position_reset(&copy_bm);
1535
1536 while (to_free_normal > 0 || to_free_highmem > 0) {
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
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
1571 * and (3) inactive anonymous pages, (4) active and (5) inactive file pages,
1572 * minus mapped file pages.
1573 */
1574 static unsigned long minimum_image_size(unsigned long saveable)
1575 {
1576 unsigned long size;
1577
1578 size = global_page_state(NR_SLAB_RECLAIMABLE)
1579 + global_page_state(NR_ACTIVE_ANON)
1580 + global_page_state(NR_INACTIVE_ANON)
1581 + global_page_state(NR_ACTIVE_FILE)
1582 + global_page_state(NR_INACTIVE_FILE)
1583 - global_page_state(NR_FILE_MAPPED);
1584
1585 return saveable <= size ? 0 : saveable - size;
1586 }
1587
1588 /**
1589 * hibernate_preallocate_memory - Preallocate memory for hibernation image
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
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
1599 *
1600 * ([page frames total] + PAGES_FOR_IO + [metadata pages]) / 2
1601 * + 2 * DIV_ROUND_UP(reserved_size, PAGE_SIZE)
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
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.
1609 */
1610 int hibernate_preallocate_memory(void)
1611 {
1612 struct zone *zone;
1613 unsigned long saveable, size, max_size, count, highmem, pages = 0;
1614 unsigned long alloc, save_highmem, pages_highmem, avail_normal;
1615 struct timeval start, stop;
1616 int error;
1617
1618 printk(KERN_INFO "PM: Preallocating image memory... ");
1619 do_gettimeofday(&start);
1620
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
1632 /* Count the number of saveable data pages. */
1633 save_highmem = count_highmem_pages();
1634 saveable = count_data_pages();
1635
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;
1641 saveable += save_highmem;
1642 highmem = save_highmem;
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 }
1651 avail_normal = count;
1652 count += highmem;
1653 count -= totalreserve_pages;
1654
1655 /* Add number of pages required for page keys (s390 only). */
1656 size += page_key_additional_pages(saveable);
1657
1658 /* Compute the maximum number of saveable pages to leave in memory. */
1659 max_size = (count - (size + PAGES_FOR_IO)) / 2
1660 - 2 * DIV_ROUND_UP(reserved_size, PAGE_SIZE);
1661 /* Compute the desired number of image pages specified by image_size. */
1662 size = DIV_ROUND_UP(image_size, PAGE_SIZE);
1663 if (size > max_size)
1664 size = max_size;
1665 /*
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.
1669 */
1670 if (size >= saveable) {
1671 pages = preallocate_image_highmem(save_highmem);
1672 pages += preallocate_image_memory(saveable - pages, avail_normal);
1673 goto out;
1674 }
1675
1676 /* Estimate the minimum size of the image. */
1677 pages = minimum_image_size(saveable);
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;
1687 if (size < pages)
1688 size = min_t(unsigned long, pages, max_size);
1689
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
1702 * of the image as much as indicated by 'size' using allocations from
1703 * highmem and non-highmem zones separately.
1704 */
1705 pages_highmem = preallocate_image_highmem(highmem / 2);
1706 alloc = count - max_size;
1707 if (alloc > pages_highmem)
1708 alloc -= pages_highmem;
1709 else
1710 alloc = 0;
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 }
1739
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();
1746
1747 out:
1748 do_gettimeofday(&stop);
1749 printk(KERN_CONT "done (allocated %lu pages)\n", pages);
1750 swsusp_show_speed(&start, &stop, pages, "Allocated");
1751
1752 return 0;
1753
1754 err_out:
1755 printk(KERN_CONT "\n");
1756 swsusp_free();
1757 return -ENOMEM;
1758 }
1759
1760 #ifdef CONFIG_HIGHMEM
1761 /**
1762 * count_pages_for_highmem - compute the number of non-highmem pages
1763 * that will be necessary for creating copies of highmem pages.
1764 */
1765
1766 static unsigned int count_pages_for_highmem(unsigned int nr_highmem)
1767 {
1768 unsigned int free_highmem = count_free_highmem_pages() + alloc_highmem;
1769
1770 if (free_highmem >= nr_highmem)
1771 nr_highmem = 0;
1772 else
1773 nr_highmem -= free_highmem;
1774
1775 return nr_highmem;
1776 }
1777 #else
1778 static unsigned int
1779 count_pages_for_highmem(unsigned int nr_highmem) { return 0; }
1780 #endif /* CONFIG_HIGHMEM */
1781
1782 /**
1783 * enough_free_mem - Make sure we have enough free memory for the
1784 * snapshot image.
1785 */
1786
1787 static int enough_free_mem(unsigned int nr_pages, unsigned int nr_highmem)
1788 {
1789 struct zone *zone;
1790 unsigned int free = alloc_normal;
1791
1792 for_each_populated_zone(zone)
1793 if (!is_highmem(zone))
1794 free += zone_page_state(zone, NR_FREE_PAGES);
1795
1796 nr_pages += count_pages_for_highmem(nr_highmem);
1797 pr_debug("PM: Normal pages needed: %u + %u, available pages: %u\n",
1798 nr_pages, PAGES_FOR_IO, free);
1799
1800 return free > nr_pages + PAGES_FOR_IO;
1801 }
1802
1803 #ifdef CONFIG_HIGHMEM
1804 /**
1805 * get_highmem_buffer - if there are some highmem pages in the suspend
1806 * image, we may need the buffer to copy them and/or load their data.
1807 */
1808
1809 static inline int get_highmem_buffer(int safe_needed)
1810 {
1811 buffer = get_image_page(GFP_ATOMIC | __GFP_COLD, safe_needed);
1812 return buffer ? 0 : -ENOMEM;
1813 }
1814
1815 /**
1816 * alloc_highmem_image_pages - allocate some highmem pages for the image.
1817 * Try to allocate as many pages as needed, but if the number of free
1818 * highmem pages is lesser than that, allocate them all.
1819 */
1820
1821 static inline unsigned int
1822 alloc_highmem_pages(struct memory_bitmap *bm, unsigned int nr_highmem)
1823 {
1824 unsigned int to_alloc = count_free_highmem_pages();
1825
1826 if (to_alloc > nr_highmem)
1827 to_alloc = nr_highmem;
1828
1829 nr_highmem -= to_alloc;
1830 while (to_alloc-- > 0) {
1831 struct page *page;
1832
1833 page = alloc_image_page(__GFP_HIGHMEM);
1834 memory_bm_set_bit(bm, page_to_pfn(page));
1835 }
1836 return nr_highmem;
1837 }
1838 #else
1839 static inline int get_highmem_buffer(int safe_needed) { return 0; }
1840
1841 static inline unsigned int
1842 alloc_highmem_pages(struct memory_bitmap *bm, unsigned int n) { return 0; }
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
1857 static int
1858 swsusp_alloc(struct memory_bitmap *orig_bm, struct memory_bitmap *copy_bm,
1859 unsigned int nr_pages, unsigned int nr_highmem)
1860 {
1861 if (nr_highmem > 0) {
1862 if (get_highmem_buffer(PG_ANY))
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 }
1868 }
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 }
1879 }
1880
1881 return 0;
1882
1883 err_out:
1884 swsusp_free();
1885 return -ENOMEM;
1886 }
1887
1888 asmlinkage __visible int swsusp_save(void)
1889 {
1890 unsigned int nr_pages, nr_highmem;
1891
1892 printk(KERN_INFO "PM: Creating hibernation image:\n");
1893
1894 drain_local_pages(NULL);
1895 nr_pages = count_data_pages();
1896 nr_highmem = count_highmem_pages();
1897 printk(KERN_INFO "PM: Need to copy %u pages\n", nr_pages + nr_highmem);
1898
1899 if (!enough_free_mem(nr_pages, nr_highmem)) {
1900 printk(KERN_ERR "PM: Not enough free memory\n");
1901 return -ENOMEM;
1902 }
1903
1904 if (swsusp_alloc(&orig_bm, &copy_bm, nr_pages, nr_highmem)) {
1905 printk(KERN_ERR "PM: Memory allocation failed\n");
1906 return -ENOMEM;
1907 }
1908
1909 /* During allocating of suspend pagedir, new cold pages may appear.
1910 * Kill them.
1911 */
1912 drain_local_pages(NULL);
1913 copy_data_pages(&copy_bm, &orig_bm);
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
1921 nr_pages += nr_highmem;
1922 nr_copy_pages = nr_pages;
1923 nr_meta_pages = DIV_ROUND_UP(nr_pages * sizeof(long), PAGE_SIZE);
1924
1925 printk(KERN_INFO "PM: Hibernation image created (%d pages copied)\n",
1926 nr_pages);
1927
1928 return 0;
1929 }
1930
1931 #ifndef CONFIG_ARCH_HIBERNATION_HEADER
1932 static int init_header_complete(struct swsusp_info *info)
1933 {
1934 memcpy(&info->uts, init_utsname(), sizeof(struct new_utsname));
1935 info->version_code = LINUX_VERSION_CODE;
1936 return 0;
1937 }
1938
1939 static char *check_image_kernel(struct swsusp_info *info)
1940 {
1941 if (info->version_code != LINUX_VERSION_CODE)
1942 return "kernel version";
1943 if (strcmp(info->uts.sysname,init_utsname()->sysname))
1944 return "system type";
1945 if (strcmp(info->uts.release,init_utsname()->release))
1946 return "kernel release";
1947 if (strcmp(info->uts.version,init_utsname()->version))
1948 return "version";
1949 if (strcmp(info->uts.machine,init_utsname()->machine))
1950 return "machine";
1951 return NULL;
1952 }
1953 #endif /* CONFIG_ARCH_HIBERNATION_HEADER */
1954
1955 unsigned long snapshot_get_image_size(void)
1956 {
1957 return nr_copy_pages + nr_meta_pages + 1;
1958 }
1959
1960 static int init_header(struct swsusp_info *info)
1961 {
1962 memset(info, 0, sizeof(struct swsusp_info));
1963 info->num_physpages = get_num_physpages();
1964 info->image_pages = nr_copy_pages;
1965 info->pages = snapshot_get_image_size();
1966 info->size = info->pages;
1967 info->size <<= PAGE_SHIFT;
1968 return init_header_complete(info);
1969 }
1970
1971 /**
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)
1974 */
1975
1976 static inline void
1977 pack_pfns(unsigned long *buf, struct memory_bitmap *bm)
1978 {
1979 int j;
1980
1981 for (j = 0; j < PAGE_SIZE / sizeof(long); j++) {
1982 buf[j] = memory_bm_next_pfn(bm);
1983 if (unlikely(buf[j] == BM_END_OF_MAP))
1984 break;
1985 /* Save page key for data page (s390 only). */
1986 page_key_read(buf + j);
1987 }
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 *
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
1999 * location computed by the data_of() macro.
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
2007 int snapshot_read_next(struct snapshot_handle *handle)
2008 {
2009 if (handle->cur > nr_meta_pages + nr_copy_pages)
2010 return 0;
2011
2012 if (!buffer) {
2013 /* This makes the buffer be freed by swsusp_free() */
2014 buffer = get_image_page(GFP_ATOMIC, PG_ANY);
2015 if (!buffer)
2016 return -ENOMEM;
2017 }
2018 if (!handle->cur) {
2019 int error;
2020
2021 error = init_header((struct swsusp_info *)buffer);
2022 if (error)
2023 return error;
2024 handle->buffer = buffer;
2025 memory_bm_position_reset(&orig_bm);
2026 memory_bm_position_reset(&copy_bm);
2027 } else if (handle->cur <= nr_meta_pages) {
2028 clear_page(buffer);
2029 pack_pfns(buffer, &orig_bm);
2030 } else {
2031 struct page *page;
2032
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;
2040
2041 kaddr = kmap_atomic(page);
2042 copy_page(buffer, kaddr);
2043 kunmap_atomic(kaddr);
2044 handle->buffer = buffer;
2045 } else {
2046 handle->buffer = page_address(page);
2047 }
2048 }
2049 handle->cur++;
2050 return PAGE_SIZE;
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
2059 static int mark_unsafe_pages(struct memory_bitmap *bm)
2060 {
2061 struct zone *zone;
2062 unsigned long pfn, max_zone_pfn;
2063
2064 /* Clear page flags */
2065 for_each_populated_zone(zone) {
2066 max_zone_pfn = zone_end_pfn(zone);
2067 for (pfn = zone->zone_start_pfn; pfn < max_zone_pfn; pfn++)
2068 if (pfn_valid(pfn))
2069 swsusp_unset_page_free(pfn_to_page(pfn));
2070 }
2071
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)))
2078 swsusp_set_page_free(pfn_to_page(pfn));
2079 else
2080 return -EFAULT;
2081 }
2082 } while (pfn != BM_END_OF_MAP);
2083
2084 allocated_unsafe_pages = 0;
2085
2086 return 0;
2087 }
2088
2089 static void
2090 duplicate_memory_bitmap(struct memory_bitmap *dst, struct memory_bitmap *src)
2091 {
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);
2099 }
2100 }
2101
2102 static int check_header(struct swsusp_info *info)
2103 {
2104 char *reason;
2105
2106 reason = check_image_kernel(info);
2107 if (!reason && info->num_physpages != get_num_physpages())
2108 reason = "memory size";
2109 if (reason) {
2110 printk(KERN_ERR "PM: Image mismatch: %s\n", reason);
2111 return -EPERM;
2112 }
2113 return 0;
2114 }
2115
2116 /**
2117 * load header - check the image header and copy data from it
2118 */
2119
2120 static int
2121 load_header(struct swsusp_info *info)
2122 {
2123 int error;
2124
2125 restore_pblist = NULL;
2126 error = check_header(info);
2127 if (!error) {
2128 nr_copy_pages = info->image_pages;
2129 nr_meta_pages = info->pages - info->image_pages - 1;
2130 }
2131 return error;
2132 }
2133
2134 /**
2135 * unpack_orig_pfns - for each element of @buf[] (1 page at a time) set
2136 * the corresponding bit in the memory bitmap @bm
2137 */
2138 static int unpack_orig_pfns(unsigned long *buf, struct memory_bitmap *bm)
2139 {
2140 int j;
2141
2142 for (j = 0; j < PAGE_SIZE / sizeof(long); j++) {
2143 if (unlikely(buf[j] == BM_END_OF_MAP))
2144 break;
2145
2146 /* Extract and buffer page key for data page (s390 only). */
2147 page_key_memorize(buf + j);
2148
2149 if (memory_bm_pfn_present(bm, buf[j]))
2150 memory_bm_set_bit(bm, buf[j]);
2151 else
2152 return -EFAULT;
2153 }
2154
2155 return 0;
2156 }
2157
2158 /* List of "safe" pages that may be used to store data loaded from the suspend
2159 * image
2160 */
2161 static struct linked_page *safe_pages_list;
2162
2163 #ifdef CONFIG_HIGHMEM
2164 /* struct highmem_pbe is used for creating the list of highmem pages that
2165 * should be restored atomically during the resume from disk, because the page
2166 * frames they have occupied before the suspend are in use.
2167 */
2168 struct highmem_pbe {
2169 struct page *copy_page; /* data is here now */
2170 struct page *orig_page; /* data was here before the suspend */
2171 struct highmem_pbe *next;
2172 };
2173
2174 /* List of highmem PBEs needed for restoring the highmem pages that were
2175 * allocated before the suspend and included in the suspend image, but have
2176 * also been allocated by the "resume" kernel, so their contents cannot be
2177 * written directly to their "original" page frames.
2178 */
2179 static struct highmem_pbe *highmem_pblist;
2180
2181 /**
2182 * count_highmem_image_pages - compute the number of highmem pages in the
2183 * suspend image. The bits in the memory bitmap @bm that correspond to the
2184 * image pages are assumed to be set.
2185 */
2186
2187 static unsigned int count_highmem_image_pages(struct memory_bitmap *bm)
2188 {
2189 unsigned long pfn;
2190 unsigned int cnt = 0;
2191
2192 memory_bm_position_reset(bm);
2193 pfn = memory_bm_next_pfn(bm);
2194 while (pfn != BM_END_OF_MAP) {
2195 if (PageHighMem(pfn_to_page(pfn)))
2196 cnt++;
2197
2198 pfn = memory_bm_next_pfn(bm);
2199 }
2200 return cnt;
2201 }
2202
2203 /**
2204 * prepare_highmem_image - try to allocate as many highmem pages as
2205 * there are highmem image pages (@nr_highmem_p points to the variable
2206 * containing the number of highmem image pages). The pages that are
2207 * "safe" (ie. will not be overwritten when the suspend image is
2208 * restored) have the corresponding bits set in @bm (it must be
2209 * unitialized).
2210 *
2211 * NOTE: This function should not be called if there are no highmem
2212 * image pages.
2213 */
2214
2215 static unsigned int safe_highmem_pages;
2216
2217 static struct memory_bitmap *safe_highmem_bm;
2218
2219 static int
2220 prepare_highmem_image(struct memory_bitmap *bm, unsigned int *nr_highmem_p)
2221 {
2222 unsigned int to_alloc;
2223
2224 if (memory_bm_create(bm, GFP_ATOMIC, PG_SAFE))
2225 return -ENOMEM;
2226
2227 if (get_highmem_buffer(PG_SAFE))
2228 return -ENOMEM;
2229
2230 to_alloc = count_free_highmem_pages();
2231 if (to_alloc > *nr_highmem_p)
2232 to_alloc = *nr_highmem_p;
2233 else
2234 *nr_highmem_p = to_alloc;
2235
2236 safe_highmem_pages = 0;
2237 while (to_alloc-- > 0) {
2238 struct page *page;
2239
2240 page = alloc_page(__GFP_HIGHMEM);
2241 if (!swsusp_page_is_free(page)) {
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 */
2247 swsusp_set_page_forbidden(page);
2248 swsusp_set_page_free(page);
2249 }
2250 memory_bm_position_reset(bm);
2251 safe_highmem_bm = bm;
2252 return 0;
2253 }
2254
2255 /**
2256 * get_highmem_page_buffer - for given highmem image page find the buffer
2257 * that suspend_write_next() should set for its caller to write to.
2258 *
2259 * If the page is to be saved to its "original" page frame or a copy of
2260 * the page is to be made in the highmem, @buffer is returned. Otherwise,
2261 * the copy of the page is to be made in normal memory, so the address of
2262 * the copy is returned.
2263 *
2264 * If @buffer is returned, the caller of suspend_write_next() will write
2265 * the page's contents to @buffer, so they will have to be copied to the
2266 * right location on the next call to suspend_write_next() and it is done
2267 * with the help of copy_last_highmem_page(). For this purpose, if
2268 * @buffer is returned, @last_highmem page is set to the page to which
2269 * the data will have to be copied from @buffer.
2270 */
2271
2272 static struct page *last_highmem_page;
2273
2274 static void *
2275 get_highmem_page_buffer(struct page *page, struct chain_allocator *ca)
2276 {
2277 struct highmem_pbe *pbe;
2278 void *kaddr;
2279
2280 if (swsusp_page_is_forbidden(page) && swsusp_page_is_free(page)) {
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();
2293 return ERR_PTR(-ENOMEM);
2294 }
2295 pbe->orig_page = page;
2296 if (safe_highmem_pages > 0) {
2297 struct page *tmp;
2298
2299 /* Copy of the page will be stored in high memory */
2300 kaddr = buffer;
2301 tmp = pfn_to_page(memory_bm_next_pfn(safe_highmem_bm));
2302 safe_highmem_pages--;
2303 last_highmem_page = tmp;
2304 pbe->copy_page = tmp;
2305 } else {
2306 /* Copy of the page will be stored in normal memory */
2307 kaddr = safe_pages_list;
2308 safe_pages_list = safe_pages_list->next;
2309 pbe->copy_page = virt_to_page(kaddr);
2310 }
2311 pbe->next = highmem_pblist;
2312 highmem_pblist = pbe;
2313 return kaddr;
2314 }
2315
2316 /**
2317 * copy_last_highmem_page - copy the contents of a highmem image from
2318 * @buffer, where the caller of snapshot_write_next() has place them,
2319 * to the right location represented by @last_highmem_page .
2320 */
2321
2322 static void copy_last_highmem_page(void)
2323 {
2324 if (last_highmem_page) {
2325 void *dst;
2326
2327 dst = kmap_atomic(last_highmem_page);
2328 copy_page(dst, buffer);
2329 kunmap_atomic(dst);
2330 last_highmem_page = NULL;
2331 }
2332 }
2333
2334 static inline int last_highmem_page_copied(void)
2335 {
2336 return !last_highmem_page;
2337 }
2338
2339 static inline void free_highmem_data(void)
2340 {
2341 if (safe_highmem_bm)
2342 memory_bm_free(safe_highmem_bm, PG_UNSAFE_CLEAR);
2343
2344 if (buffer)
2345 free_image_page(buffer, PG_UNSAFE_CLEAR);
2346 }
2347 #else
2348 static inline int get_safe_write_buffer(void) { return 0; }
2349
2350 static unsigned int
2351 count_highmem_image_pages(struct memory_bitmap *bm) { return 0; }
2352
2353 static inline int
2354 prepare_highmem_image(struct memory_bitmap *bm, unsigned int *nr_highmem_p)
2355 {
2356 return 0;
2357 }
2358
2359 static inline void *
2360 get_highmem_page_buffer(struct page *page, struct chain_allocator *ca)
2361 {
2362 return ERR_PTR(-EINVAL);
2363 }
2364
2365 static inline void copy_last_highmem_page(void) {}
2366 static inline int last_highmem_page_copied(void) { return 1; }
2367 static inline void free_highmem_data(void) {}
2368 #endif /* CONFIG_HIGHMEM */
2369
2370 /**
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.
2375 *
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
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.
2382 */
2383
2384 #define PBES_PER_LINKED_PAGE (LINKED_PAGE_DATA_SIZE / sizeof(struct pbe))
2385
2386 static int
2387 prepare_image(struct memory_bitmap *new_bm, struct memory_bitmap *bm)
2388 {
2389 unsigned int nr_pages, nr_highmem;
2390 struct linked_page *sp_list, *lp;
2391 int error;
2392
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);
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);
2408 if (nr_highmem > 0) {
2409 error = prepare_highmem_image(bm, &nr_highmem);
2410 if (error)
2411 goto Free;
2412 }
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 */
2421 nr_pages = nr_copy_pages - nr_highmem - allocated_unsafe_pages;
2422 nr_pages = DIV_ROUND_UP(nr_pages, PBES_PER_LINKED_PAGE);
2423 while (nr_pages > 0) {
2424 lp = get_image_page(GFP_ATOMIC, PG_SAFE);
2425 if (!lp) {
2426 error = -ENOMEM;
2427 goto Free;
2428 }
2429 lp->next = sp_list;
2430 sp_list = lp;
2431 nr_pages--;
2432 }
2433 /* Preallocate memory for the image */
2434 safe_pages_list = NULL;
2435 nr_pages = nr_copy_pages - nr_highmem - allocated_unsafe_pages;
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 }
2442 if (!swsusp_page_is_free(virt_to_page(lp))) {
2443 /* The page is "safe", add it to the list */
2444 lp->next = safe_pages_list;
2445 safe_pages_list = lp;
2446 }
2447 /* Mark the page as allocated */
2448 swsusp_set_page_forbidden(virt_to_page(lp));
2449 swsusp_set_page_free(virt_to_page(lp));
2450 nr_pages--;
2451 }
2452 /* Free the reserved safe pages so that chain_alloc() can use them */
2453 while (sp_list) {
2454 lp = sp_list->next;
2455 free_image_page(sp_list, PG_UNSAFE_CLEAR);
2456 sp_list = lp;
2457 }
2458 return 0;
2459
2460 Free:
2461 swsusp_free();
2462 return error;
2463 }
2464
2465 /**
2466 * get_buffer - compute the address that snapshot_write_next() should
2467 * set for its caller to write to.
2468 */
2469
2470 static void *get_buffer(struct memory_bitmap *bm, struct chain_allocator *ca)
2471 {
2472 struct pbe *pbe;
2473 struct page *page;
2474 unsigned long pfn = memory_bm_next_pfn(bm);
2475
2476 if (pfn == BM_END_OF_MAP)
2477 return ERR_PTR(-EFAULT);
2478
2479 page = pfn_to_page(pfn);
2480 if (PageHighMem(page))
2481 return get_highmem_page_buffer(page, ca);
2482
2483 if (swsusp_page_is_forbidden(page) && swsusp_page_is_free(page))
2484 /* We have allocated the "original" page frame and we can
2485 * use it directly to store the loaded page.
2486 */
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.
2491 */
2492 pbe = chain_alloc(ca, sizeof(struct pbe));
2493 if (!pbe) {
2494 swsusp_free();
2495 return ERR_PTR(-ENOMEM);
2496 }
2497 pbe->orig_address = page_address(page);
2498 pbe->address = safe_pages_list;
2499 safe_pages_list = safe_pages_list->next;
2500 pbe->next = restore_pblist;
2501 restore_pblist = pbe;
2502 return pbe->address;
2503 }
2504
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 *
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
2514 * location computed by the data_of() macro.
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
2522 int snapshot_write_next(struct snapshot_handle *handle)
2523 {
2524 static struct chain_allocator ca;
2525 int error = 0;
2526
2527 /* Check if we have already loaded the entire image */
2528 if (handle->cur > 1 && handle->cur > nr_meta_pages + nr_copy_pages)
2529 return 0;
2530
2531 handle->sync_read = 1;
2532
2533 if (!handle->cur) {
2534 if (!buffer)
2535 /* This makes the buffer be freed by swsusp_free() */
2536 buffer = get_image_page(GFP_ATOMIC, PG_ANY);
2537
2538 if (!buffer)
2539 return -ENOMEM;
2540
2541 handle->buffer = buffer;
2542 } else if (handle->cur == 1) {
2543 error = load_header(buffer);
2544 if (error)
2545 return error;
2546
2547 error = memory_bm_create(&copy_bm, GFP_ATOMIC, PG_ANY);
2548 if (error)
2549 return error;
2550
2551 /* Allocate buffer for page keys. */
2552 error = page_key_alloc(nr_copy_pages);
2553 if (error)
2554 return error;
2555
2556 } else if (handle->cur <= nr_meta_pages + 1) {
2557 error = unpack_orig_pfns(buffer, &copy_bm);
2558 if (error)
2559 return error;
2560
2561 if (handle->cur == nr_meta_pages + 1) {
2562 error = prepare_image(&orig_bm, &copy_bm);
2563 if (error)
2564 return error;
2565
2566 chain_init(&ca, GFP_ATOMIC, PG_SAFE);
2567 memory_bm_position_reset(&orig_bm);
2568 restore_pblist = NULL;
2569 handle->buffer = get_buffer(&orig_bm, &ca);
2570 handle->sync_read = 0;
2571 if (IS_ERR(handle->buffer))
2572 return PTR_ERR(handle->buffer);
2573 }
2574 } else {
2575 copy_last_highmem_page();
2576 /* Restore page key for data page (s390 only). */
2577 page_key_write(handle->buffer);
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;
2583 }
2584 handle->cur++;
2585 return PAGE_SIZE;
2586 }
2587
2588 /**
2589 * snapshot_write_finalize - must be called after the last call to
2590 * snapshot_write_next() in case the last page in the image happens
2591 * to be a highmem page and its contents should be stored in the
2592 * highmem. Additionally, it releases the memory that will not be
2593 * used any more.
2594 */
2595
2596 void snapshot_write_finalize(struct snapshot_handle *handle)
2597 {
2598 copy_last_highmem_page();
2599 /* Restore page key for data page (s390 only). */
2600 page_key_write(handle->buffer);
2601 page_key_free();
2602 /* Free only if we have loaded the image entirely */
2603 if (handle->cur > 1 && handle->cur > nr_meta_pages + nr_copy_pages) {
2604 memory_bm_free(&orig_bm, PG_UNSAFE_CLEAR);
2605 free_highmem_data();
2606 }
2607 }
2608
2609 int snapshot_image_loaded(struct snapshot_handle *handle)
2610 {
2611 return !(!nr_copy_pages || !last_highmem_page_copied() ||
2612 handle->cur <= nr_meta_pages + nr_copy_pages);
2613 }
2614
2615 #ifdef CONFIG_HIGHMEM
2616 /* Assumes that @buf is ready and points to a "safe" page */
2617 static inline void
2618 swap_two_pages_data(struct page *p1, struct page *p2, void *buf)
2619 {
2620 void *kaddr1, *kaddr2;
2621
2622 kaddr1 = kmap_atomic(p1);
2623 kaddr2 = kmap_atomic(p2);
2624 copy_page(buf, kaddr1);
2625 copy_page(kaddr1, kaddr2);
2626 copy_page(kaddr2, buf);
2627 kunmap_atomic(kaddr2);
2628 kunmap_atomic(kaddr1);
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
2641 int restore_highmem(void)
2642 {
2643 struct highmem_pbe *pbe = highmem_pblist;
2644 void *buf;
2645
2646 if (!pbe)
2647 return 0;
2648
2649 buf = get_image_page(GFP_ATOMIC, PG_SAFE);
2650 if (!buf)
2651 return -ENOMEM;
2652
2653 while (pbe) {
2654 swap_two_pages_data(pbe->copy_page, pbe->orig_page, buf);
2655 pbe = pbe->next;
2656 }
2657 free_image_page(buf, PG_UNSAFE_CLEAR);
2658 return 0;
2659 }
2660 #endif /* CONFIG_HIGHMEM */