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