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 RW |
5 | * |
6 | * Copyright (C) 1998-2005 Pavel Machek <pavel@suse.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> | |
17 | #include <linux/smp_lock.h> | |
25761b6e | 18 | #include <linux/delay.h> |
25761b6e | 19 | #include <linux/bitops.h> |
25761b6e | 20 | #include <linux/spinlock.h> |
25761b6e | 21 | #include <linux/kernel.h> |
25761b6e RW |
22 | #include <linux/pm.h> |
23 | #include <linux/device.h> | |
25761b6e RW |
24 | #include <linux/bootmem.h> |
25 | #include <linux/syscalls.h> | |
26 | #include <linux/console.h> | |
27 | #include <linux/highmem.h> | |
25761b6e RW |
28 | |
29 | #include <asm/uaccess.h> | |
30 | #include <asm/mmu_context.h> | |
31 | #include <asm/pgtable.h> | |
32 | #include <asm/tlbflush.h> | |
33 | #include <asm/io.h> | |
34 | ||
25761b6e RW |
35 | #include "power.h" |
36 | ||
8357376d RW |
37 | /* List of PBEs needed for restoring the pages that were allocated before |
38 | * the suspend and included in the suspend image, but have also been | |
39 | * allocated by the "resume" kernel, so their contents cannot be written | |
40 | * directly to their "original" page frames. | |
41 | */ | |
75534b50 RW |
42 | struct pbe *restore_pblist; |
43 | ||
8357376d | 44 | /* Pointer to an auxiliary buffer (1 page) */ |
940864dd | 45 | static void *buffer; |
7088a5c0 | 46 | |
f6143aa6 RW |
47 | /** |
48 | * @safe_needed - on resume, for storing the PBE list and the image, | |
49 | * we can only use memory pages that do not conflict with the pages | |
8357376d RW |
50 | * used before suspend. The unsafe pages have PageNosaveFree set |
51 | * and we count them using unsafe_pages. | |
f6143aa6 | 52 | * |
8357376d RW |
53 | * Each allocated image page is marked as PageNosave and PageNosaveFree |
54 | * so that swsusp_free() can release it. | |
f6143aa6 RW |
55 | */ |
56 | ||
0bcd888d RW |
57 | #define PG_ANY 0 |
58 | #define PG_SAFE 1 | |
59 | #define PG_UNSAFE_CLEAR 1 | |
60 | #define PG_UNSAFE_KEEP 0 | |
61 | ||
940864dd | 62 | static unsigned int allocated_unsafe_pages; |
f6143aa6 | 63 | |
8357376d | 64 | static void *get_image_page(gfp_t gfp_mask, int safe_needed) |
f6143aa6 RW |
65 | { |
66 | void *res; | |
67 | ||
68 | res = (void *)get_zeroed_page(gfp_mask); | |
69 | if (safe_needed) | |
70 | while (res && PageNosaveFree(virt_to_page(res))) { | |
71 | /* The page is unsafe, mark it for swsusp_free() */ | |
72 | SetPageNosave(virt_to_page(res)); | |
940864dd | 73 | allocated_unsafe_pages++; |
f6143aa6 RW |
74 | res = (void *)get_zeroed_page(gfp_mask); |
75 | } | |
76 | if (res) { | |
77 | SetPageNosave(virt_to_page(res)); | |
78 | SetPageNosaveFree(virt_to_page(res)); | |
79 | } | |
80 | return res; | |
81 | } | |
82 | ||
83 | unsigned long get_safe_page(gfp_t gfp_mask) | |
84 | { | |
8357376d RW |
85 | return (unsigned long)get_image_page(gfp_mask, PG_SAFE); |
86 | } | |
87 | ||
88 | static struct page *alloc_image_page(gfp_t gfp_mask) { | |
89 | struct page *page; | |
90 | ||
91 | page = alloc_page(gfp_mask); | |
92 | if (page) { | |
93 | SetPageNosave(page); | |
94 | SetPageNosaveFree(page); | |
95 | } | |
96 | return page; | |
f6143aa6 RW |
97 | } |
98 | ||
99 | /** | |
100 | * free_image_page - free page represented by @addr, allocated with | |
8357376d | 101 | * get_image_page (page flags set by it must be cleared) |
f6143aa6 RW |
102 | */ |
103 | ||
104 | static inline void free_image_page(void *addr, int clear_nosave_free) | |
105 | { | |
8357376d RW |
106 | struct page *page; |
107 | ||
108 | BUG_ON(!virt_addr_valid(addr)); | |
109 | ||
110 | page = virt_to_page(addr); | |
111 | ||
112 | ClearPageNosave(page); | |
f6143aa6 | 113 | if (clear_nosave_free) |
8357376d RW |
114 | ClearPageNosaveFree(page); |
115 | ||
116 | __free_page(page); | |
f6143aa6 RW |
117 | } |
118 | ||
b788db79 RW |
119 | /* struct linked_page is used to build chains of pages */ |
120 | ||
121 | #define LINKED_PAGE_DATA_SIZE (PAGE_SIZE - sizeof(void *)) | |
122 | ||
123 | struct linked_page { | |
124 | struct linked_page *next; | |
125 | char data[LINKED_PAGE_DATA_SIZE]; | |
126 | } __attribute__((packed)); | |
127 | ||
128 | static inline void | |
129 | free_list_of_pages(struct linked_page *list, int clear_page_nosave) | |
130 | { | |
131 | while (list) { | |
132 | struct linked_page *lp = list->next; | |
133 | ||
134 | free_image_page(list, clear_page_nosave); | |
135 | list = lp; | |
136 | } | |
137 | } | |
138 | ||
139 | /** | |
140 | * struct chain_allocator is used for allocating small objects out of | |
141 | * a linked list of pages called 'the chain'. | |
142 | * | |
143 | * The chain grows each time when there is no room for a new object in | |
144 | * the current page. The allocated objects cannot be freed individually. | |
145 | * It is only possible to free them all at once, by freeing the entire | |
146 | * chain. | |
147 | * | |
148 | * NOTE: The chain allocator may be inefficient if the allocated objects | |
149 | * are not much smaller than PAGE_SIZE. | |
150 | */ | |
151 | ||
152 | struct chain_allocator { | |
153 | struct linked_page *chain; /* the chain */ | |
154 | unsigned int used_space; /* total size of objects allocated out | |
155 | * of the current page | |
156 | */ | |
157 | gfp_t gfp_mask; /* mask for allocating pages */ | |
158 | int safe_needed; /* if set, only "safe" pages are allocated */ | |
159 | }; | |
160 | ||
161 | static void | |
162 | chain_init(struct chain_allocator *ca, gfp_t gfp_mask, int safe_needed) | |
163 | { | |
164 | ca->chain = NULL; | |
165 | ca->used_space = LINKED_PAGE_DATA_SIZE; | |
166 | ca->gfp_mask = gfp_mask; | |
167 | ca->safe_needed = safe_needed; | |
168 | } | |
169 | ||
170 | static void *chain_alloc(struct chain_allocator *ca, unsigned int size) | |
171 | { | |
172 | void *ret; | |
173 | ||
174 | if (LINKED_PAGE_DATA_SIZE - ca->used_space < size) { | |
175 | struct linked_page *lp; | |
176 | ||
8357376d | 177 | lp = get_image_page(ca->gfp_mask, ca->safe_needed); |
b788db79 RW |
178 | if (!lp) |
179 | return NULL; | |
180 | ||
181 | lp->next = ca->chain; | |
182 | ca->chain = lp; | |
183 | ca->used_space = 0; | |
184 | } | |
185 | ret = ca->chain->data + ca->used_space; | |
186 | ca->used_space += size; | |
187 | return ret; | |
188 | } | |
189 | ||
190 | static void chain_free(struct chain_allocator *ca, int clear_page_nosave) | |
191 | { | |
192 | free_list_of_pages(ca->chain, clear_page_nosave); | |
193 | memset(ca, 0, sizeof(struct chain_allocator)); | |
194 | } | |
195 | ||
196 | /** | |
197 | * Data types related to memory bitmaps. | |
198 | * | |
199 | * Memory bitmap is a structure consiting of many linked lists of | |
200 | * objects. The main list's elements are of type struct zone_bitmap | |
201 | * and each of them corresonds to one zone. For each zone bitmap | |
202 | * object there is a list of objects of type struct bm_block that | |
203 | * represent each blocks of bit chunks in which information is | |
204 | * stored. | |
205 | * | |
206 | * struct memory_bitmap contains a pointer to the main list of zone | |
207 | * bitmap objects, a struct bm_position used for browsing the bitmap, | |
208 | * and a pointer to the list of pages used for allocating all of the | |
209 | * zone bitmap objects and bitmap block objects. | |
210 | * | |
211 | * NOTE: It has to be possible to lay out the bitmap in memory | |
212 | * using only allocations of order 0. Additionally, the bitmap is | |
213 | * designed to work with arbitrary number of zones (this is over the | |
214 | * top for now, but let's avoid making unnecessary assumptions ;-). | |
215 | * | |
216 | * struct zone_bitmap contains a pointer to a list of bitmap block | |
217 | * objects and a pointer to the bitmap block object that has been | |
218 | * most recently used for setting bits. Additionally, it contains the | |
219 | * pfns that correspond to the start and end of the represented zone. | |
220 | * | |
221 | * struct bm_block contains a pointer to the memory page in which | |
222 | * information is stored (in the form of a block of bit chunks | |
223 | * of type unsigned long each). It also contains the pfns that | |
224 | * correspond to the start and end of the represented memory area and | |
225 | * the number of bit chunks in the block. | |
226 | * | |
227 | * NOTE: Memory bitmaps are used for two types of operations only: | |
228 | * "set a bit" and "find the next bit set". Moreover, the searching | |
229 | * is always carried out after all of the "set a bit" operations | |
230 | * on given bitmap. | |
231 | */ | |
232 | ||
233 | #define BM_END_OF_MAP (~0UL) | |
234 | ||
235 | #define BM_CHUNKS_PER_BLOCK (PAGE_SIZE / sizeof(long)) | |
236 | #define BM_BITS_PER_CHUNK (sizeof(long) << 3) | |
237 | #define BM_BITS_PER_BLOCK (PAGE_SIZE << 3) | |
238 | ||
239 | struct bm_block { | |
240 | struct bm_block *next; /* next element of the list */ | |
241 | unsigned long start_pfn; /* pfn represented by the first bit */ | |
242 | unsigned long end_pfn; /* pfn represented by the last bit plus 1 */ | |
243 | unsigned int size; /* number of bit chunks */ | |
244 | unsigned long *data; /* chunks of bits representing pages */ | |
245 | }; | |
246 | ||
247 | struct zone_bitmap { | |
248 | struct zone_bitmap *next; /* next element of the list */ | |
249 | unsigned long start_pfn; /* minimal pfn in this zone */ | |
250 | unsigned long end_pfn; /* maximal pfn in this zone plus 1 */ | |
251 | struct bm_block *bm_blocks; /* list of bitmap blocks */ | |
252 | struct bm_block *cur_block; /* recently used bitmap block */ | |
253 | }; | |
254 | ||
255 | /* strcut bm_position is used for browsing memory bitmaps */ | |
256 | ||
257 | struct bm_position { | |
258 | struct zone_bitmap *zone_bm; | |
259 | struct bm_block *block; | |
260 | int chunk; | |
261 | int bit; | |
262 | }; | |
263 | ||
264 | struct memory_bitmap { | |
265 | struct zone_bitmap *zone_bm_list; /* list of zone bitmaps */ | |
266 | struct linked_page *p_list; /* list of pages used to store zone | |
267 | * bitmap objects and bitmap block | |
268 | * objects | |
269 | */ | |
270 | struct bm_position cur; /* most recently used bit position */ | |
271 | }; | |
272 | ||
273 | /* Functions that operate on memory bitmaps */ | |
274 | ||
275 | static inline void memory_bm_reset_chunk(struct memory_bitmap *bm) | |
276 | { | |
277 | bm->cur.chunk = 0; | |
278 | bm->cur.bit = -1; | |
279 | } | |
280 | ||
281 | static void memory_bm_position_reset(struct memory_bitmap *bm) | |
282 | { | |
283 | struct zone_bitmap *zone_bm; | |
284 | ||
285 | zone_bm = bm->zone_bm_list; | |
286 | bm->cur.zone_bm = zone_bm; | |
287 | bm->cur.block = zone_bm->bm_blocks; | |
288 | memory_bm_reset_chunk(bm); | |
289 | } | |
290 | ||
291 | static void memory_bm_free(struct memory_bitmap *bm, int clear_nosave_free); | |
292 | ||
293 | /** | |
294 | * create_bm_block_list - create a list of block bitmap objects | |
295 | */ | |
296 | ||
297 | static inline struct bm_block * | |
298 | create_bm_block_list(unsigned int nr_blocks, struct chain_allocator *ca) | |
299 | { | |
300 | struct bm_block *bblist = NULL; | |
301 | ||
302 | while (nr_blocks-- > 0) { | |
303 | struct bm_block *bb; | |
304 | ||
305 | bb = chain_alloc(ca, sizeof(struct bm_block)); | |
306 | if (!bb) | |
307 | return NULL; | |
308 | ||
309 | bb->next = bblist; | |
310 | bblist = bb; | |
311 | } | |
312 | return bblist; | |
313 | } | |
314 | ||
315 | /** | |
316 | * create_zone_bm_list - create a list of zone bitmap objects | |
317 | */ | |
318 | ||
319 | static inline struct zone_bitmap * | |
320 | create_zone_bm_list(unsigned int nr_zones, struct chain_allocator *ca) | |
321 | { | |
322 | struct zone_bitmap *zbmlist = NULL; | |
323 | ||
324 | while (nr_zones-- > 0) { | |
325 | struct zone_bitmap *zbm; | |
326 | ||
327 | zbm = chain_alloc(ca, sizeof(struct zone_bitmap)); | |
328 | if (!zbm) | |
329 | return NULL; | |
330 | ||
331 | zbm->next = zbmlist; | |
332 | zbmlist = zbm; | |
333 | } | |
334 | return zbmlist; | |
335 | } | |
336 | ||
337 | /** | |
338 | * memory_bm_create - allocate memory for a memory bitmap | |
339 | */ | |
340 | ||
341 | static int | |
342 | memory_bm_create(struct memory_bitmap *bm, gfp_t gfp_mask, int safe_needed) | |
343 | { | |
344 | struct chain_allocator ca; | |
345 | struct zone *zone; | |
346 | struct zone_bitmap *zone_bm; | |
347 | struct bm_block *bb; | |
348 | unsigned int nr; | |
349 | ||
350 | chain_init(&ca, gfp_mask, safe_needed); | |
351 | ||
352 | /* Compute the number of zones */ | |
353 | nr = 0; | |
8357376d RW |
354 | for_each_zone(zone) |
355 | if (populated_zone(zone)) | |
b788db79 RW |
356 | nr++; |
357 | ||
358 | /* Allocate the list of zones bitmap objects */ | |
359 | zone_bm = create_zone_bm_list(nr, &ca); | |
360 | bm->zone_bm_list = zone_bm; | |
361 | if (!zone_bm) { | |
362 | chain_free(&ca, PG_UNSAFE_CLEAR); | |
363 | return -ENOMEM; | |
364 | } | |
365 | ||
366 | /* Initialize the zone bitmap objects */ | |
8357376d | 367 | for_each_zone(zone) { |
b788db79 RW |
368 | unsigned long pfn; |
369 | ||
8357376d | 370 | if (!populated_zone(zone)) |
b788db79 RW |
371 | continue; |
372 | ||
373 | zone_bm->start_pfn = zone->zone_start_pfn; | |
374 | zone_bm->end_pfn = zone->zone_start_pfn + zone->spanned_pages; | |
375 | /* Allocate the list of bitmap block objects */ | |
376 | nr = DIV_ROUND_UP(zone->spanned_pages, BM_BITS_PER_BLOCK); | |
377 | bb = create_bm_block_list(nr, &ca); | |
378 | zone_bm->bm_blocks = bb; | |
379 | zone_bm->cur_block = bb; | |
380 | if (!bb) | |
381 | goto Free; | |
382 | ||
383 | nr = zone->spanned_pages; | |
384 | pfn = zone->zone_start_pfn; | |
385 | /* Initialize the bitmap block objects */ | |
386 | while (bb) { | |
387 | unsigned long *ptr; | |
388 | ||
8357376d | 389 | ptr = get_image_page(gfp_mask, safe_needed); |
b788db79 RW |
390 | bb->data = ptr; |
391 | if (!ptr) | |
392 | goto Free; | |
393 | ||
394 | bb->start_pfn = pfn; | |
395 | if (nr >= BM_BITS_PER_BLOCK) { | |
396 | pfn += BM_BITS_PER_BLOCK; | |
397 | bb->size = BM_CHUNKS_PER_BLOCK; | |
398 | nr -= BM_BITS_PER_BLOCK; | |
399 | } else { | |
400 | /* This is executed only once in the loop */ | |
401 | pfn += nr; | |
402 | bb->size = DIV_ROUND_UP(nr, BM_BITS_PER_CHUNK); | |
403 | } | |
404 | bb->end_pfn = pfn; | |
405 | bb = bb->next; | |
406 | } | |
407 | zone_bm = zone_bm->next; | |
408 | } | |
409 | bm->p_list = ca.chain; | |
410 | memory_bm_position_reset(bm); | |
411 | return 0; | |
412 | ||
413 | Free: | |
414 | bm->p_list = ca.chain; | |
415 | memory_bm_free(bm, PG_UNSAFE_CLEAR); | |
416 | return -ENOMEM; | |
417 | } | |
418 | ||
419 | /** | |
420 | * memory_bm_free - free memory occupied by the memory bitmap @bm | |
421 | */ | |
422 | ||
423 | static void memory_bm_free(struct memory_bitmap *bm, int clear_nosave_free) | |
424 | { | |
425 | struct zone_bitmap *zone_bm; | |
426 | ||
427 | /* Free the list of bit blocks for each zone_bitmap object */ | |
428 | zone_bm = bm->zone_bm_list; | |
429 | while (zone_bm) { | |
430 | struct bm_block *bb; | |
431 | ||
432 | bb = zone_bm->bm_blocks; | |
433 | while (bb) { | |
434 | if (bb->data) | |
435 | free_image_page(bb->data, clear_nosave_free); | |
436 | bb = bb->next; | |
437 | } | |
438 | zone_bm = zone_bm->next; | |
439 | } | |
440 | free_list_of_pages(bm->p_list, clear_nosave_free); | |
441 | bm->zone_bm_list = NULL; | |
442 | } | |
443 | ||
444 | /** | |
445 | * memory_bm_set_bit - set the bit in the bitmap @bm that corresponds | |
446 | * to given pfn. The cur_zone_bm member of @bm and the cur_block member | |
447 | * of @bm->cur_zone_bm are updated. | |
448 | * | |
449 | * If the bit cannot be set, the function returns -EINVAL . | |
450 | */ | |
451 | ||
452 | static int | |
453 | memory_bm_set_bit(struct memory_bitmap *bm, unsigned long pfn) | |
454 | { | |
455 | struct zone_bitmap *zone_bm; | |
456 | struct bm_block *bb; | |
457 | ||
458 | /* Check if the pfn is from the current zone */ | |
459 | zone_bm = bm->cur.zone_bm; | |
460 | if (pfn < zone_bm->start_pfn || pfn >= zone_bm->end_pfn) { | |
461 | zone_bm = bm->zone_bm_list; | |
462 | /* We don't assume that the zones are sorted by pfns */ | |
463 | while (pfn < zone_bm->start_pfn || pfn >= zone_bm->end_pfn) { | |
464 | zone_bm = zone_bm->next; | |
465 | if (unlikely(!zone_bm)) | |
466 | return -EINVAL; | |
467 | } | |
468 | bm->cur.zone_bm = zone_bm; | |
469 | } | |
470 | /* Check if the pfn corresponds to the current bitmap block */ | |
471 | bb = zone_bm->cur_block; | |
472 | if (pfn < bb->start_pfn) | |
473 | bb = zone_bm->bm_blocks; | |
474 | ||
475 | while (pfn >= bb->end_pfn) { | |
476 | bb = bb->next; | |
477 | if (unlikely(!bb)) | |
478 | return -EINVAL; | |
479 | } | |
480 | zone_bm->cur_block = bb; | |
481 | pfn -= bb->start_pfn; | |
482 | set_bit(pfn % BM_BITS_PER_CHUNK, bb->data + pfn / BM_BITS_PER_CHUNK); | |
483 | return 0; | |
484 | } | |
485 | ||
486 | /* Two auxiliary functions for memory_bm_next_pfn */ | |
487 | ||
488 | /* Find the first set bit in the given chunk, if there is one */ | |
489 | ||
490 | static inline int next_bit_in_chunk(int bit, unsigned long *chunk_p) | |
491 | { | |
492 | bit++; | |
493 | while (bit < BM_BITS_PER_CHUNK) { | |
494 | if (test_bit(bit, chunk_p)) | |
495 | return bit; | |
496 | ||
497 | bit++; | |
498 | } | |
499 | return -1; | |
500 | } | |
501 | ||
502 | /* Find a chunk containing some bits set in given block of bits */ | |
503 | ||
504 | static inline int next_chunk_in_block(int n, struct bm_block *bb) | |
505 | { | |
506 | n++; | |
507 | while (n < bb->size) { | |
508 | if (bb->data[n]) | |
509 | return n; | |
510 | ||
511 | n++; | |
512 | } | |
513 | return -1; | |
514 | } | |
515 | ||
516 | /** | |
517 | * memory_bm_next_pfn - find the pfn that corresponds to the next set bit | |
518 | * in the bitmap @bm. If the pfn cannot be found, BM_END_OF_MAP is | |
519 | * returned. | |
520 | * | |
521 | * It is required to run memory_bm_position_reset() before the first call to | |
522 | * this function. | |
523 | */ | |
524 | ||
525 | static unsigned long memory_bm_next_pfn(struct memory_bitmap *bm) | |
526 | { | |
527 | struct zone_bitmap *zone_bm; | |
528 | struct bm_block *bb; | |
529 | int chunk; | |
530 | int bit; | |
531 | ||
532 | do { | |
533 | bb = bm->cur.block; | |
534 | do { | |
535 | chunk = bm->cur.chunk; | |
536 | bit = bm->cur.bit; | |
537 | do { | |
538 | bit = next_bit_in_chunk(bit, bb->data + chunk); | |
539 | if (bit >= 0) | |
540 | goto Return_pfn; | |
541 | ||
542 | chunk = next_chunk_in_block(chunk, bb); | |
543 | bit = -1; | |
544 | } while (chunk >= 0); | |
545 | bb = bb->next; | |
546 | bm->cur.block = bb; | |
547 | memory_bm_reset_chunk(bm); | |
548 | } while (bb); | |
549 | zone_bm = bm->cur.zone_bm->next; | |
550 | if (zone_bm) { | |
551 | bm->cur.zone_bm = zone_bm; | |
552 | bm->cur.block = zone_bm->bm_blocks; | |
553 | memory_bm_reset_chunk(bm); | |
554 | } | |
555 | } while (zone_bm); | |
556 | memory_bm_position_reset(bm); | |
557 | return BM_END_OF_MAP; | |
558 | ||
559 | Return_pfn: | |
560 | bm->cur.chunk = chunk; | |
561 | bm->cur.bit = bit; | |
562 | return bb->start_pfn + chunk * BM_BITS_PER_CHUNK + bit; | |
563 | } | |
564 | ||
565 | /** | |
566 | * snapshot_additional_pages - estimate the number of additional pages | |
567 | * be needed for setting up the suspend image data structures for given | |
568 | * zone (usually the returned value is greater than the exact number) | |
569 | */ | |
570 | ||
571 | unsigned int snapshot_additional_pages(struct zone *zone) | |
572 | { | |
573 | unsigned int res; | |
574 | ||
575 | res = DIV_ROUND_UP(zone->spanned_pages, BM_BITS_PER_BLOCK); | |
576 | res += DIV_ROUND_UP(res * sizeof(struct bm_block), PAGE_SIZE); | |
8357376d | 577 | return 2 * res; |
b788db79 RW |
578 | } |
579 | ||
8357376d RW |
580 | #ifdef CONFIG_HIGHMEM |
581 | /** | |
582 | * count_free_highmem_pages - compute the total number of free highmem | |
583 | * pages, system-wide. | |
584 | */ | |
585 | ||
586 | static unsigned int count_free_highmem_pages(void) | |
587 | { | |
588 | struct zone *zone; | |
589 | unsigned int cnt = 0; | |
590 | ||
591 | for_each_zone(zone) | |
592 | if (populated_zone(zone) && is_highmem(zone)) | |
593 | cnt += zone->free_pages; | |
594 | ||
595 | return cnt; | |
596 | } | |
597 | ||
598 | /** | |
599 | * saveable_highmem_page - Determine whether a highmem page should be | |
600 | * included in the suspend image. | |
601 | * | |
602 | * We should save the page if it isn't Nosave or NosaveFree, or Reserved, | |
603 | * and it isn't a part of a free chunk of pages. | |
604 | */ | |
605 | ||
606 | static struct page *saveable_highmem_page(unsigned long pfn) | |
607 | { | |
608 | struct page *page; | |
609 | ||
610 | if (!pfn_valid(pfn)) | |
611 | return NULL; | |
612 | ||
613 | page = pfn_to_page(pfn); | |
614 | ||
615 | BUG_ON(!PageHighMem(page)); | |
616 | ||
617 | if (PageNosave(page) || PageReserved(page) || PageNosaveFree(page)) | |
618 | return NULL; | |
619 | ||
620 | return page; | |
621 | } | |
622 | ||
623 | /** | |
624 | * count_highmem_pages - compute the total number of saveable highmem | |
625 | * pages. | |
626 | */ | |
627 | ||
628 | unsigned int count_highmem_pages(void) | |
629 | { | |
630 | struct zone *zone; | |
631 | unsigned int n = 0; | |
632 | ||
633 | for_each_zone(zone) { | |
634 | unsigned long pfn, max_zone_pfn; | |
635 | ||
636 | if (!is_highmem(zone)) | |
637 | continue; | |
638 | ||
639 | mark_free_pages(zone); | |
640 | max_zone_pfn = zone->zone_start_pfn + zone->spanned_pages; | |
641 | for (pfn = zone->zone_start_pfn; pfn < max_zone_pfn; pfn++) | |
642 | if (saveable_highmem_page(pfn)) | |
643 | n++; | |
644 | } | |
645 | return n; | |
646 | } | |
647 | #else | |
648 | static inline void *saveable_highmem_page(unsigned long pfn) { return NULL; } | |
649 | static inline unsigned int count_highmem_pages(void) { return 0; } | |
650 | #endif /* CONFIG_HIGHMEM */ | |
651 | ||
f6143aa6 RW |
652 | /** |
653 | * pfn_is_nosave - check if given pfn is in the 'nosave' section | |
654 | */ | |
655 | ||
ae83c5ee | 656 | static inline int pfn_is_nosave(unsigned long pfn) |
25761b6e RW |
657 | { |
658 | unsigned long nosave_begin_pfn = __pa(&__nosave_begin) >> PAGE_SHIFT; | |
659 | unsigned long nosave_end_pfn = PAGE_ALIGN(__pa(&__nosave_end)) >> PAGE_SHIFT; | |
660 | return (pfn >= nosave_begin_pfn) && (pfn < nosave_end_pfn); | |
661 | } | |
662 | ||
663 | /** | |
8357376d RW |
664 | * saveable - Determine whether a non-highmem page should be included in |
665 | * the suspend image. | |
25761b6e | 666 | * |
8357376d RW |
667 | * We should save the page if it isn't Nosave, and is not in the range |
668 | * of pages statically defined as 'unsaveable', and it isn't a part of | |
669 | * a free chunk of pages. | |
25761b6e RW |
670 | */ |
671 | ||
ae83c5ee | 672 | static struct page *saveable_page(unsigned long pfn) |
25761b6e | 673 | { |
de491861 | 674 | struct page *page; |
25761b6e RW |
675 | |
676 | if (!pfn_valid(pfn)) | |
ae83c5ee | 677 | return NULL; |
25761b6e RW |
678 | |
679 | page = pfn_to_page(pfn); | |
ae83c5ee | 680 | |
8357376d RW |
681 | BUG_ON(PageHighMem(page)); |
682 | ||
683 | if (PageNosave(page) || PageNosaveFree(page)) | |
ae83c5ee | 684 | return NULL; |
8357376d | 685 | |
72a97e08 | 686 | if (PageReserved(page) && pfn_is_nosave(pfn)) |
ae83c5ee | 687 | return NULL; |
25761b6e | 688 | |
ae83c5ee | 689 | return page; |
25761b6e RW |
690 | } |
691 | ||
8357376d RW |
692 | /** |
693 | * count_data_pages - compute the total number of saveable non-highmem | |
694 | * pages. | |
695 | */ | |
696 | ||
72a97e08 | 697 | unsigned int count_data_pages(void) |
25761b6e RW |
698 | { |
699 | struct zone *zone; | |
ae83c5ee | 700 | unsigned long pfn, max_zone_pfn; |
dc19d507 | 701 | unsigned int n = 0; |
25761b6e | 702 | |
8357376d | 703 | for_each_zone(zone) { |
25761b6e RW |
704 | if (is_highmem(zone)) |
705 | continue; | |
8357376d | 706 | |
25761b6e | 707 | mark_free_pages(zone); |
ae83c5ee RW |
708 | max_zone_pfn = zone->zone_start_pfn + zone->spanned_pages; |
709 | for (pfn = zone->zone_start_pfn; pfn < max_zone_pfn; pfn++) | |
8357376d RW |
710 | if(saveable_page(pfn)) |
711 | n++; | |
25761b6e | 712 | } |
a0f49651 | 713 | return n; |
25761b6e RW |
714 | } |
715 | ||
8357376d RW |
716 | /* This is needed, because copy_page and memcpy are not usable for copying |
717 | * task structs. | |
718 | */ | |
719 | static inline void do_copy_page(long *dst, long *src) | |
f623f0db RW |
720 | { |
721 | int n; | |
722 | ||
f623f0db RW |
723 | for (n = PAGE_SIZE / sizeof(long); n; n--) |
724 | *dst++ = *src++; | |
725 | } | |
726 | ||
8357376d RW |
727 | #ifdef CONFIG_HIGHMEM |
728 | static inline struct page * | |
729 | page_is_saveable(struct zone *zone, unsigned long pfn) | |
730 | { | |
731 | return is_highmem(zone) ? | |
732 | saveable_highmem_page(pfn) : saveable_page(pfn); | |
733 | } | |
734 | ||
735 | static inline void | |
736 | copy_data_page(unsigned long dst_pfn, unsigned long src_pfn) | |
737 | { | |
738 | struct page *s_page, *d_page; | |
739 | void *src, *dst; | |
740 | ||
741 | s_page = pfn_to_page(src_pfn); | |
742 | d_page = pfn_to_page(dst_pfn); | |
743 | if (PageHighMem(s_page)) { | |
744 | src = kmap_atomic(s_page, KM_USER0); | |
745 | dst = kmap_atomic(d_page, KM_USER1); | |
746 | do_copy_page(dst, src); | |
747 | kunmap_atomic(src, KM_USER0); | |
748 | kunmap_atomic(dst, KM_USER1); | |
749 | } else { | |
750 | src = page_address(s_page); | |
751 | if (PageHighMem(d_page)) { | |
752 | /* Page pointed to by src may contain some kernel | |
753 | * data modified by kmap_atomic() | |
754 | */ | |
755 | do_copy_page(buffer, src); | |
756 | dst = kmap_atomic(pfn_to_page(dst_pfn), KM_USER0); | |
757 | memcpy(dst, buffer, PAGE_SIZE); | |
758 | kunmap_atomic(dst, KM_USER0); | |
759 | } else { | |
760 | dst = page_address(d_page); | |
761 | do_copy_page(dst, src); | |
762 | } | |
763 | } | |
764 | } | |
765 | #else | |
766 | #define page_is_saveable(zone, pfn) saveable_page(pfn) | |
767 | ||
768 | static inline void | |
769 | copy_data_page(unsigned long dst_pfn, unsigned long src_pfn) | |
770 | { | |
771 | do_copy_page(page_address(pfn_to_page(dst_pfn)), | |
772 | page_address(pfn_to_page(src_pfn))); | |
773 | } | |
774 | #endif /* CONFIG_HIGHMEM */ | |
775 | ||
b788db79 RW |
776 | static void |
777 | copy_data_pages(struct memory_bitmap *copy_bm, struct memory_bitmap *orig_bm) | |
25761b6e RW |
778 | { |
779 | struct zone *zone; | |
b788db79 | 780 | unsigned long pfn; |
25761b6e | 781 | |
8357376d | 782 | for_each_zone(zone) { |
b788db79 RW |
783 | unsigned long max_zone_pfn; |
784 | ||
25761b6e | 785 | mark_free_pages(zone); |
ae83c5ee | 786 | max_zone_pfn = zone->zone_start_pfn + zone->spanned_pages; |
b788db79 | 787 | for (pfn = zone->zone_start_pfn; pfn < max_zone_pfn; pfn++) |
8357376d | 788 | if (page_is_saveable(zone, pfn)) |
b788db79 | 789 | memory_bm_set_bit(orig_bm, pfn); |
25761b6e | 790 | } |
b788db79 RW |
791 | memory_bm_position_reset(orig_bm); |
792 | memory_bm_position_reset(copy_bm); | |
793 | do { | |
794 | pfn = memory_bm_next_pfn(orig_bm); | |
8357376d RW |
795 | if (likely(pfn != BM_END_OF_MAP)) |
796 | copy_data_page(memory_bm_next_pfn(copy_bm), pfn); | |
b788db79 | 797 | } while (pfn != BM_END_OF_MAP); |
25761b6e RW |
798 | } |
799 | ||
8357376d RW |
800 | /* Total number of image pages */ |
801 | static unsigned int nr_copy_pages; | |
802 | /* Number of pages needed for saving the original pfns of the image pages */ | |
803 | static unsigned int nr_meta_pages; | |
804 | ||
25761b6e | 805 | /** |
940864dd | 806 | * swsusp_free - free pages allocated for the suspend. |
cd560bb2 | 807 | * |
940864dd RW |
808 | * Suspend pages are alocated before the atomic copy is made, so we |
809 | * need to release them after the resume. | |
25761b6e RW |
810 | */ |
811 | ||
812 | void swsusp_free(void) | |
813 | { | |
814 | struct zone *zone; | |
ae83c5ee | 815 | unsigned long pfn, max_zone_pfn; |
25761b6e RW |
816 | |
817 | for_each_zone(zone) { | |
ae83c5ee RW |
818 | max_zone_pfn = zone->zone_start_pfn + zone->spanned_pages; |
819 | for (pfn = zone->zone_start_pfn; pfn < max_zone_pfn; pfn++) | |
820 | if (pfn_valid(pfn)) { | |
821 | struct page *page = pfn_to_page(pfn); | |
822 | ||
25761b6e RW |
823 | if (PageNosave(page) && PageNosaveFree(page)) { |
824 | ClearPageNosave(page); | |
825 | ClearPageNosaveFree(page); | |
8357376d | 826 | __free_page(page); |
25761b6e RW |
827 | } |
828 | } | |
829 | } | |
f577eb30 RW |
830 | nr_copy_pages = 0; |
831 | nr_meta_pages = 0; | |
75534b50 | 832 | restore_pblist = NULL; |
6e1819d6 | 833 | buffer = NULL; |
25761b6e RW |
834 | } |
835 | ||
8357376d RW |
836 | #ifdef CONFIG_HIGHMEM |
837 | /** | |
838 | * count_pages_for_highmem - compute the number of non-highmem pages | |
839 | * that will be necessary for creating copies of highmem pages. | |
840 | */ | |
841 | ||
842 | static unsigned int count_pages_for_highmem(unsigned int nr_highmem) | |
843 | { | |
844 | unsigned int free_highmem = count_free_highmem_pages(); | |
845 | ||
846 | if (free_highmem >= nr_highmem) | |
847 | nr_highmem = 0; | |
848 | else | |
849 | nr_highmem -= free_highmem; | |
850 | ||
851 | return nr_highmem; | |
852 | } | |
853 | #else | |
854 | static unsigned int | |
855 | count_pages_for_highmem(unsigned int nr_highmem) { return 0; } | |
856 | #endif /* CONFIG_HIGHMEM */ | |
25761b6e RW |
857 | |
858 | /** | |
8357376d RW |
859 | * enough_free_mem - Make sure we have enough free memory for the |
860 | * snapshot image. | |
25761b6e RW |
861 | */ |
862 | ||
8357376d | 863 | static int enough_free_mem(unsigned int nr_pages, unsigned int nr_highmem) |
25761b6e | 864 | { |
e5e2fa78 | 865 | struct zone *zone; |
940864dd | 866 | unsigned int free = 0, meta = 0; |
e5e2fa78 | 867 | |
8357376d RW |
868 | for_each_zone(zone) { |
869 | meta += snapshot_additional_pages(zone); | |
870 | if (!is_highmem(zone)) | |
940864dd | 871 | free += zone->free_pages; |
8357376d | 872 | } |
940864dd | 873 | |
8357376d RW |
874 | nr_pages += count_pages_for_highmem(nr_highmem); |
875 | pr_debug("swsusp: Normal pages needed: %u + %u + %u, available pages: %u\n", | |
940864dd RW |
876 | nr_pages, PAGES_FOR_IO, meta, free); |
877 | ||
878 | return free > nr_pages + PAGES_FOR_IO + meta; | |
25761b6e RW |
879 | } |
880 | ||
8357376d RW |
881 | #ifdef CONFIG_HIGHMEM |
882 | /** | |
883 | * get_highmem_buffer - if there are some highmem pages in the suspend | |
884 | * image, we may need the buffer to copy them and/or load their data. | |
885 | */ | |
886 | ||
887 | static inline int get_highmem_buffer(int safe_needed) | |
888 | { | |
889 | buffer = get_image_page(GFP_ATOMIC | __GFP_COLD, safe_needed); | |
890 | return buffer ? 0 : -ENOMEM; | |
891 | } | |
892 | ||
893 | /** | |
894 | * alloc_highmem_image_pages - allocate some highmem pages for the image. | |
895 | * Try to allocate as many pages as needed, but if the number of free | |
896 | * highmem pages is lesser than that, allocate them all. | |
897 | */ | |
898 | ||
899 | static inline unsigned int | |
900 | alloc_highmem_image_pages(struct memory_bitmap *bm, unsigned int nr_highmem) | |
901 | { | |
902 | unsigned int to_alloc = count_free_highmem_pages(); | |
903 | ||
904 | if (to_alloc > nr_highmem) | |
905 | to_alloc = nr_highmem; | |
906 | ||
907 | nr_highmem -= to_alloc; | |
908 | while (to_alloc-- > 0) { | |
909 | struct page *page; | |
910 | ||
911 | page = alloc_image_page(__GFP_HIGHMEM); | |
912 | memory_bm_set_bit(bm, page_to_pfn(page)); | |
913 | } | |
914 | return nr_highmem; | |
915 | } | |
916 | #else | |
917 | static inline int get_highmem_buffer(int safe_needed) { return 0; } | |
918 | ||
919 | static inline unsigned int | |
920 | alloc_highmem_image_pages(struct memory_bitmap *bm, unsigned int n) { return 0; } | |
921 | #endif /* CONFIG_HIGHMEM */ | |
922 | ||
923 | /** | |
924 | * swsusp_alloc - allocate memory for the suspend image | |
925 | * | |
926 | * We first try to allocate as many highmem pages as there are | |
927 | * saveable highmem pages in the system. If that fails, we allocate | |
928 | * non-highmem pages for the copies of the remaining highmem ones. | |
929 | * | |
930 | * In this approach it is likely that the copies of highmem pages will | |
931 | * also be located in the high memory, because of the way in which | |
932 | * copy_data_pages() works. | |
933 | */ | |
934 | ||
b788db79 RW |
935 | static int |
936 | swsusp_alloc(struct memory_bitmap *orig_bm, struct memory_bitmap *copy_bm, | |
8357376d | 937 | unsigned int nr_pages, unsigned int nr_highmem) |
054bd4c1 | 938 | { |
b788db79 | 939 | int error; |
054bd4c1 | 940 | |
b788db79 RW |
941 | error = memory_bm_create(orig_bm, GFP_ATOMIC | __GFP_COLD, PG_ANY); |
942 | if (error) | |
943 | goto Free; | |
25761b6e | 944 | |
b788db79 RW |
945 | error = memory_bm_create(copy_bm, GFP_ATOMIC | __GFP_COLD, PG_ANY); |
946 | if (error) | |
947 | goto Free; | |
25761b6e | 948 | |
8357376d RW |
949 | if (nr_highmem > 0) { |
950 | error = get_highmem_buffer(PG_ANY); | |
951 | if (error) | |
952 | goto Free; | |
953 | ||
954 | nr_pages += alloc_highmem_image_pages(copy_bm, nr_highmem); | |
955 | } | |
b788db79 | 956 | while (nr_pages-- > 0) { |
8357376d RW |
957 | struct page *page = alloc_image_page(GFP_ATOMIC | __GFP_COLD); |
958 | ||
b788db79 RW |
959 | if (!page) |
960 | goto Free; | |
25761b6e | 961 | |
b788db79 | 962 | memory_bm_set_bit(copy_bm, page_to_pfn(page)); |
25761b6e | 963 | } |
b788db79 | 964 | return 0; |
25761b6e | 965 | |
b788db79 RW |
966 | Free: |
967 | swsusp_free(); | |
968 | return -ENOMEM; | |
25761b6e RW |
969 | } |
970 | ||
8357376d RW |
971 | /* Memory bitmap used for marking saveable pages (during suspend) or the |
972 | * suspend image pages (during resume) | |
973 | */ | |
b788db79 | 974 | static struct memory_bitmap orig_bm; |
8357376d RW |
975 | /* Memory bitmap used on suspend for marking allocated pages that will contain |
976 | * the copies of saveable pages. During resume it is initially used for | |
977 | * marking the suspend image pages, but then its set bits are duplicated in | |
978 | * @orig_bm and it is released. Next, on systems with high memory, it may be | |
979 | * used for marking "safe" highmem pages, but it has to be reinitialized for | |
980 | * this purpose. | |
b788db79 RW |
981 | */ |
982 | static struct memory_bitmap copy_bm; | |
983 | ||
2e32a43e | 984 | asmlinkage int swsusp_save(void) |
25761b6e | 985 | { |
8357376d | 986 | unsigned int nr_pages, nr_highmem; |
25761b6e | 987 | |
8357376d | 988 | printk("swsusp: critical section: \n"); |
25761b6e RW |
989 | |
990 | drain_local_pages(); | |
a0f49651 | 991 | nr_pages = count_data_pages(); |
8357376d RW |
992 | nr_highmem = count_highmem_pages(); |
993 | printk("swsusp: Need to copy %u pages\n", nr_pages + nr_highmem); | |
25761b6e | 994 | |
8357376d | 995 | if (!enough_free_mem(nr_pages, nr_highmem)) { |
25761b6e RW |
996 | printk(KERN_ERR "swsusp: Not enough free memory\n"); |
997 | return -ENOMEM; | |
998 | } | |
999 | ||
8357376d RW |
1000 | if (swsusp_alloc(&orig_bm, ©_bm, nr_pages, nr_highmem)) { |
1001 | printk(KERN_ERR "swsusp: Memory allocation failed\n"); | |
a0f49651 | 1002 | return -ENOMEM; |
8357376d | 1003 | } |
25761b6e RW |
1004 | |
1005 | /* During allocating of suspend pagedir, new cold pages may appear. | |
1006 | * Kill them. | |
1007 | */ | |
1008 | drain_local_pages(); | |
b788db79 | 1009 | copy_data_pages(©_bm, &orig_bm); |
25761b6e RW |
1010 | |
1011 | /* | |
1012 | * End of critical section. From now on, we can write to memory, | |
1013 | * but we should not touch disk. This specially means we must _not_ | |
1014 | * touch swap space! Except we must write out our image of course. | |
1015 | */ | |
1016 | ||
8357376d | 1017 | nr_pages += nr_highmem; |
a0f49651 | 1018 | nr_copy_pages = nr_pages; |
8357376d | 1019 | nr_meta_pages = DIV_ROUND_UP(nr_pages * sizeof(long), PAGE_SIZE); |
a0f49651 RW |
1020 | |
1021 | printk("swsusp: critical section/: done (%d pages copied)\n", nr_pages); | |
8357376d | 1022 | |
25761b6e RW |
1023 | return 0; |
1024 | } | |
f577eb30 RW |
1025 | |
1026 | static void init_header(struct swsusp_info *info) | |
1027 | { | |
1028 | memset(info, 0, sizeof(struct swsusp_info)); | |
1029 | info->version_code = LINUX_VERSION_CODE; | |
1030 | info->num_physpages = num_physpages; | |
96b644bd | 1031 | memcpy(&info->uts, init_utsname(), sizeof(struct new_utsname)); |
f577eb30 RW |
1032 | info->cpus = num_online_cpus(); |
1033 | info->image_pages = nr_copy_pages; | |
1034 | info->pages = nr_copy_pages + nr_meta_pages + 1; | |
6e1819d6 RW |
1035 | info->size = info->pages; |
1036 | info->size <<= PAGE_SHIFT; | |
f577eb30 RW |
1037 | } |
1038 | ||
1039 | /** | |
940864dd RW |
1040 | * pack_pfns - pfns corresponding to the set bits found in the bitmap @bm |
1041 | * are stored in the array @buf[] (1 page at a time) | |
f577eb30 RW |
1042 | */ |
1043 | ||
b788db79 | 1044 | static inline void |
940864dd | 1045 | pack_pfns(unsigned long *buf, struct memory_bitmap *bm) |
f577eb30 RW |
1046 | { |
1047 | int j; | |
1048 | ||
b788db79 | 1049 | for (j = 0; j < PAGE_SIZE / sizeof(long); j++) { |
940864dd RW |
1050 | buf[j] = memory_bm_next_pfn(bm); |
1051 | if (unlikely(buf[j] == BM_END_OF_MAP)) | |
b788db79 | 1052 | break; |
f577eb30 | 1053 | } |
f577eb30 RW |
1054 | } |
1055 | ||
1056 | /** | |
1057 | * snapshot_read_next - used for reading the system memory snapshot. | |
1058 | * | |
1059 | * On the first call to it @handle should point to a zeroed | |
1060 | * snapshot_handle structure. The structure gets updated and a pointer | |
1061 | * to it should be passed to this function every next time. | |
1062 | * | |
1063 | * The @count parameter should contain the number of bytes the caller | |
1064 | * wants to read from the snapshot. It must not be zero. | |
1065 | * | |
1066 | * On success the function returns a positive number. Then, the caller | |
1067 | * is allowed to read up to the returned number of bytes from the memory | |
1068 | * location computed by the data_of() macro. The number returned | |
1069 | * may be smaller than @count, but this only happens if the read would | |
1070 | * cross a page boundary otherwise. | |
1071 | * | |
1072 | * The function returns 0 to indicate the end of data stream condition, | |
1073 | * and a negative number is returned on error. In such cases the | |
1074 | * structure pointed to by @handle is not updated and should not be used | |
1075 | * any more. | |
1076 | */ | |
1077 | ||
1078 | int snapshot_read_next(struct snapshot_handle *handle, size_t count) | |
1079 | { | |
fb13a28b | 1080 | if (handle->cur > nr_meta_pages + nr_copy_pages) |
f577eb30 | 1081 | return 0; |
b788db79 | 1082 | |
f577eb30 RW |
1083 | if (!buffer) { |
1084 | /* This makes the buffer be freed by swsusp_free() */ | |
8357376d | 1085 | buffer = get_image_page(GFP_ATOMIC, PG_ANY); |
f577eb30 RW |
1086 | if (!buffer) |
1087 | return -ENOMEM; | |
1088 | } | |
1089 | if (!handle->offset) { | |
1090 | init_header((struct swsusp_info *)buffer); | |
1091 | handle->buffer = buffer; | |
b788db79 RW |
1092 | memory_bm_position_reset(&orig_bm); |
1093 | memory_bm_position_reset(©_bm); | |
f577eb30 | 1094 | } |
fb13a28b RW |
1095 | if (handle->prev < handle->cur) { |
1096 | if (handle->cur <= nr_meta_pages) { | |
b788db79 | 1097 | memset(buffer, 0, PAGE_SIZE); |
940864dd | 1098 | pack_pfns(buffer, &orig_bm); |
f577eb30 | 1099 | } else { |
8357376d | 1100 | struct page *page; |
b788db79 | 1101 | |
8357376d RW |
1102 | page = pfn_to_page(memory_bm_next_pfn(©_bm)); |
1103 | if (PageHighMem(page)) { | |
1104 | /* Highmem pages are copied to the buffer, | |
1105 | * because we can't return with a kmapped | |
1106 | * highmem page (we may not be called again). | |
1107 | */ | |
1108 | void *kaddr; | |
1109 | ||
1110 | kaddr = kmap_atomic(page, KM_USER0); | |
1111 | memcpy(buffer, kaddr, PAGE_SIZE); | |
1112 | kunmap_atomic(kaddr, KM_USER0); | |
1113 | handle->buffer = buffer; | |
1114 | } else { | |
1115 | handle->buffer = page_address(page); | |
1116 | } | |
f577eb30 | 1117 | } |
fb13a28b | 1118 | handle->prev = handle->cur; |
f577eb30 | 1119 | } |
fb13a28b RW |
1120 | handle->buf_offset = handle->cur_offset; |
1121 | if (handle->cur_offset + count >= PAGE_SIZE) { | |
1122 | count = PAGE_SIZE - handle->cur_offset; | |
1123 | handle->cur_offset = 0; | |
1124 | handle->cur++; | |
f577eb30 | 1125 | } else { |
fb13a28b | 1126 | handle->cur_offset += count; |
f577eb30 RW |
1127 | } |
1128 | handle->offset += count; | |
1129 | return count; | |
1130 | } | |
1131 | ||
1132 | /** | |
1133 | * mark_unsafe_pages - mark the pages that cannot be used for storing | |
1134 | * the image during resume, because they conflict with the pages that | |
1135 | * had been used before suspend | |
1136 | */ | |
1137 | ||
940864dd | 1138 | static int mark_unsafe_pages(struct memory_bitmap *bm) |
f577eb30 RW |
1139 | { |
1140 | struct zone *zone; | |
ae83c5ee | 1141 | unsigned long pfn, max_zone_pfn; |
f577eb30 RW |
1142 | |
1143 | /* Clear page flags */ | |
8357376d | 1144 | for_each_zone(zone) { |
ae83c5ee RW |
1145 | max_zone_pfn = zone->zone_start_pfn + zone->spanned_pages; |
1146 | for (pfn = zone->zone_start_pfn; pfn < max_zone_pfn; pfn++) | |
1147 | if (pfn_valid(pfn)) | |
1148 | ClearPageNosaveFree(pfn_to_page(pfn)); | |
f577eb30 RW |
1149 | } |
1150 | ||
940864dd RW |
1151 | /* Mark pages that correspond to the "original" pfns as "unsafe" */ |
1152 | memory_bm_position_reset(bm); | |
1153 | do { | |
1154 | pfn = memory_bm_next_pfn(bm); | |
1155 | if (likely(pfn != BM_END_OF_MAP)) { | |
1156 | if (likely(pfn_valid(pfn))) | |
1157 | SetPageNosaveFree(pfn_to_page(pfn)); | |
1158 | else | |
1159 | return -EFAULT; | |
1160 | } | |
1161 | } while (pfn != BM_END_OF_MAP); | |
f577eb30 | 1162 | |
940864dd | 1163 | allocated_unsafe_pages = 0; |
968808b8 | 1164 | |
f577eb30 RW |
1165 | return 0; |
1166 | } | |
1167 | ||
940864dd RW |
1168 | static void |
1169 | duplicate_memory_bitmap(struct memory_bitmap *dst, struct memory_bitmap *src) | |
f577eb30 | 1170 | { |
940864dd RW |
1171 | unsigned long pfn; |
1172 | ||
1173 | memory_bm_position_reset(src); | |
1174 | pfn = memory_bm_next_pfn(src); | |
1175 | while (pfn != BM_END_OF_MAP) { | |
1176 | memory_bm_set_bit(dst, pfn); | |
1177 | pfn = memory_bm_next_pfn(src); | |
f577eb30 RW |
1178 | } |
1179 | } | |
1180 | ||
940864dd | 1181 | static inline int check_header(struct swsusp_info *info) |
f577eb30 RW |
1182 | { |
1183 | char *reason = NULL; | |
1184 | ||
1185 | if (info->version_code != LINUX_VERSION_CODE) | |
1186 | reason = "kernel version"; | |
1187 | if (info->num_physpages != num_physpages) | |
1188 | reason = "memory size"; | |
96b644bd | 1189 | if (strcmp(info->uts.sysname,init_utsname()->sysname)) |
f577eb30 | 1190 | reason = "system type"; |
96b644bd | 1191 | if (strcmp(info->uts.release,init_utsname()->release)) |
f577eb30 | 1192 | reason = "kernel release"; |
96b644bd | 1193 | if (strcmp(info->uts.version,init_utsname()->version)) |
f577eb30 | 1194 | reason = "version"; |
96b644bd | 1195 | if (strcmp(info->uts.machine,init_utsname()->machine)) |
f577eb30 RW |
1196 | reason = "machine"; |
1197 | if (reason) { | |
1198 | printk(KERN_ERR "swsusp: Resume mismatch: %s\n", reason); | |
1199 | return -EPERM; | |
1200 | } | |
1201 | return 0; | |
1202 | } | |
1203 | ||
1204 | /** | |
1205 | * load header - check the image header and copy data from it | |
1206 | */ | |
1207 | ||
940864dd RW |
1208 | static int |
1209 | load_header(struct swsusp_info *info) | |
f577eb30 RW |
1210 | { |
1211 | int error; | |
f577eb30 | 1212 | |
940864dd | 1213 | restore_pblist = NULL; |
f577eb30 RW |
1214 | error = check_header(info); |
1215 | if (!error) { | |
f577eb30 RW |
1216 | nr_copy_pages = info->image_pages; |
1217 | nr_meta_pages = info->pages - info->image_pages - 1; | |
1218 | } | |
1219 | return error; | |
1220 | } | |
1221 | ||
1222 | /** | |
940864dd RW |
1223 | * unpack_orig_pfns - for each element of @buf[] (1 page at a time) set |
1224 | * the corresponding bit in the memory bitmap @bm | |
f577eb30 RW |
1225 | */ |
1226 | ||
940864dd RW |
1227 | static inline void |
1228 | unpack_orig_pfns(unsigned long *buf, struct memory_bitmap *bm) | |
f577eb30 RW |
1229 | { |
1230 | int j; | |
1231 | ||
940864dd RW |
1232 | for (j = 0; j < PAGE_SIZE / sizeof(long); j++) { |
1233 | if (unlikely(buf[j] == BM_END_OF_MAP)) | |
1234 | break; | |
1235 | ||
1236 | memory_bm_set_bit(bm, buf[j]); | |
f577eb30 | 1237 | } |
f577eb30 RW |
1238 | } |
1239 | ||
8357376d RW |
1240 | /* List of "safe" pages that may be used to store data loaded from the suspend |
1241 | * image | |
1242 | */ | |
1243 | static struct linked_page *safe_pages_list; | |
1244 | ||
1245 | #ifdef CONFIG_HIGHMEM | |
1246 | /* struct highmem_pbe is used for creating the list of highmem pages that | |
1247 | * should be restored atomically during the resume from disk, because the page | |
1248 | * frames they have occupied before the suspend are in use. | |
1249 | */ | |
1250 | struct highmem_pbe { | |
1251 | struct page *copy_page; /* data is here now */ | |
1252 | struct page *orig_page; /* data was here before the suspend */ | |
1253 | struct highmem_pbe *next; | |
1254 | }; | |
1255 | ||
1256 | /* List of highmem PBEs needed for restoring the highmem pages that were | |
1257 | * allocated before the suspend and included in the suspend image, but have | |
1258 | * also been allocated by the "resume" kernel, so their contents cannot be | |
1259 | * written directly to their "original" page frames. | |
1260 | */ | |
1261 | static struct highmem_pbe *highmem_pblist; | |
1262 | ||
1263 | /** | |
1264 | * count_highmem_image_pages - compute the number of highmem pages in the | |
1265 | * suspend image. The bits in the memory bitmap @bm that correspond to the | |
1266 | * image pages are assumed to be set. | |
1267 | */ | |
1268 | ||
1269 | static unsigned int count_highmem_image_pages(struct memory_bitmap *bm) | |
1270 | { | |
1271 | unsigned long pfn; | |
1272 | unsigned int cnt = 0; | |
1273 | ||
1274 | memory_bm_position_reset(bm); | |
1275 | pfn = memory_bm_next_pfn(bm); | |
1276 | while (pfn != BM_END_OF_MAP) { | |
1277 | if (PageHighMem(pfn_to_page(pfn))) | |
1278 | cnt++; | |
1279 | ||
1280 | pfn = memory_bm_next_pfn(bm); | |
1281 | } | |
1282 | return cnt; | |
1283 | } | |
1284 | ||
1285 | /** | |
1286 | * prepare_highmem_image - try to allocate as many highmem pages as | |
1287 | * there are highmem image pages (@nr_highmem_p points to the variable | |
1288 | * containing the number of highmem image pages). The pages that are | |
1289 | * "safe" (ie. will not be overwritten when the suspend image is | |
1290 | * restored) have the corresponding bits set in @bm (it must be | |
1291 | * unitialized). | |
1292 | * | |
1293 | * NOTE: This function should not be called if there are no highmem | |
1294 | * image pages. | |
1295 | */ | |
1296 | ||
1297 | static unsigned int safe_highmem_pages; | |
1298 | ||
1299 | static struct memory_bitmap *safe_highmem_bm; | |
1300 | ||
1301 | static int | |
1302 | prepare_highmem_image(struct memory_bitmap *bm, unsigned int *nr_highmem_p) | |
1303 | { | |
1304 | unsigned int to_alloc; | |
1305 | ||
1306 | if (memory_bm_create(bm, GFP_ATOMIC, PG_SAFE)) | |
1307 | return -ENOMEM; | |
1308 | ||
1309 | if (get_highmem_buffer(PG_SAFE)) | |
1310 | return -ENOMEM; | |
1311 | ||
1312 | to_alloc = count_free_highmem_pages(); | |
1313 | if (to_alloc > *nr_highmem_p) | |
1314 | to_alloc = *nr_highmem_p; | |
1315 | else | |
1316 | *nr_highmem_p = to_alloc; | |
1317 | ||
1318 | safe_highmem_pages = 0; | |
1319 | while (to_alloc-- > 0) { | |
1320 | struct page *page; | |
1321 | ||
1322 | page = alloc_page(__GFP_HIGHMEM); | |
1323 | if (!PageNosaveFree(page)) { | |
1324 | /* The page is "safe", set its bit the bitmap */ | |
1325 | memory_bm_set_bit(bm, page_to_pfn(page)); | |
1326 | safe_highmem_pages++; | |
1327 | } | |
1328 | /* Mark the page as allocated */ | |
1329 | SetPageNosave(page); | |
1330 | SetPageNosaveFree(page); | |
1331 | } | |
1332 | memory_bm_position_reset(bm); | |
1333 | safe_highmem_bm = bm; | |
1334 | return 0; | |
1335 | } | |
1336 | ||
1337 | /** | |
1338 | * get_highmem_page_buffer - for given highmem image page find the buffer | |
1339 | * that suspend_write_next() should set for its caller to write to. | |
1340 | * | |
1341 | * If the page is to be saved to its "original" page frame or a copy of | |
1342 | * the page is to be made in the highmem, @buffer is returned. Otherwise, | |
1343 | * the copy of the page is to be made in normal memory, so the address of | |
1344 | * the copy is returned. | |
1345 | * | |
1346 | * If @buffer is returned, the caller of suspend_write_next() will write | |
1347 | * the page's contents to @buffer, so they will have to be copied to the | |
1348 | * right location on the next call to suspend_write_next() and it is done | |
1349 | * with the help of copy_last_highmem_page(). For this purpose, if | |
1350 | * @buffer is returned, @last_highmem page is set to the page to which | |
1351 | * the data will have to be copied from @buffer. | |
1352 | */ | |
1353 | ||
1354 | static struct page *last_highmem_page; | |
1355 | ||
1356 | static void * | |
1357 | get_highmem_page_buffer(struct page *page, struct chain_allocator *ca) | |
1358 | { | |
1359 | struct highmem_pbe *pbe; | |
1360 | void *kaddr; | |
1361 | ||
1362 | if (PageNosave(page) && PageNosaveFree(page)) { | |
1363 | /* We have allocated the "original" page frame and we can | |
1364 | * use it directly to store the loaded page. | |
1365 | */ | |
1366 | last_highmem_page = page; | |
1367 | return buffer; | |
1368 | } | |
1369 | /* The "original" page frame has not been allocated and we have to | |
1370 | * use a "safe" page frame to store the loaded page. | |
1371 | */ | |
1372 | pbe = chain_alloc(ca, sizeof(struct highmem_pbe)); | |
1373 | if (!pbe) { | |
1374 | swsusp_free(); | |
1375 | return NULL; | |
1376 | } | |
1377 | pbe->orig_page = page; | |
1378 | if (safe_highmem_pages > 0) { | |
1379 | struct page *tmp; | |
1380 | ||
1381 | /* Copy of the page will be stored in high memory */ | |
1382 | kaddr = buffer; | |
1383 | tmp = pfn_to_page(memory_bm_next_pfn(safe_highmem_bm)); | |
1384 | safe_highmem_pages--; | |
1385 | last_highmem_page = tmp; | |
1386 | pbe->copy_page = tmp; | |
1387 | } else { | |
1388 | /* Copy of the page will be stored in normal memory */ | |
1389 | kaddr = safe_pages_list; | |
1390 | safe_pages_list = safe_pages_list->next; | |
1391 | pbe->copy_page = virt_to_page(kaddr); | |
1392 | } | |
1393 | pbe->next = highmem_pblist; | |
1394 | highmem_pblist = pbe; | |
1395 | return kaddr; | |
1396 | } | |
1397 | ||
1398 | /** | |
1399 | * copy_last_highmem_page - copy the contents of a highmem image from | |
1400 | * @buffer, where the caller of snapshot_write_next() has place them, | |
1401 | * to the right location represented by @last_highmem_page . | |
1402 | */ | |
1403 | ||
1404 | static void copy_last_highmem_page(void) | |
1405 | { | |
1406 | if (last_highmem_page) { | |
1407 | void *dst; | |
1408 | ||
1409 | dst = kmap_atomic(last_highmem_page, KM_USER0); | |
1410 | memcpy(dst, buffer, PAGE_SIZE); | |
1411 | kunmap_atomic(dst, KM_USER0); | |
1412 | last_highmem_page = NULL; | |
1413 | } | |
1414 | } | |
1415 | ||
1416 | static inline int last_highmem_page_copied(void) | |
1417 | { | |
1418 | return !last_highmem_page; | |
1419 | } | |
1420 | ||
1421 | static inline void free_highmem_data(void) | |
1422 | { | |
1423 | if (safe_highmem_bm) | |
1424 | memory_bm_free(safe_highmem_bm, PG_UNSAFE_CLEAR); | |
1425 | ||
1426 | if (buffer) | |
1427 | free_image_page(buffer, PG_UNSAFE_CLEAR); | |
1428 | } | |
1429 | #else | |
1430 | static inline int get_safe_write_buffer(void) { return 0; } | |
1431 | ||
1432 | static unsigned int | |
1433 | count_highmem_image_pages(struct memory_bitmap *bm) { return 0; } | |
1434 | ||
1435 | static inline int | |
1436 | prepare_highmem_image(struct memory_bitmap *bm, unsigned int *nr_highmem_p) | |
1437 | { | |
1438 | return 0; | |
1439 | } | |
1440 | ||
1441 | static inline void * | |
1442 | get_highmem_page_buffer(struct page *page, struct chain_allocator *ca) | |
1443 | { | |
1444 | return NULL; | |
1445 | } | |
1446 | ||
1447 | static inline void copy_last_highmem_page(void) {} | |
1448 | static inline int last_highmem_page_copied(void) { return 1; } | |
1449 | static inline void free_highmem_data(void) {} | |
1450 | #endif /* CONFIG_HIGHMEM */ | |
1451 | ||
f577eb30 | 1452 | /** |
940864dd RW |
1453 | * prepare_image - use the memory bitmap @bm to mark the pages that will |
1454 | * be overwritten in the process of restoring the system memory state | |
1455 | * from the suspend image ("unsafe" pages) and allocate memory for the | |
1456 | * image. | |
968808b8 | 1457 | * |
940864dd RW |
1458 | * The idea is to allocate a new memory bitmap first and then allocate |
1459 | * as many pages as needed for the image data, but not to assign these | |
1460 | * pages to specific tasks initially. Instead, we just mark them as | |
8357376d RW |
1461 | * allocated and create a lists of "safe" pages that will be used |
1462 | * later. On systems with high memory a list of "safe" highmem pages is | |
1463 | * also created. | |
f577eb30 RW |
1464 | */ |
1465 | ||
940864dd RW |
1466 | #define PBES_PER_LINKED_PAGE (LINKED_PAGE_DATA_SIZE / sizeof(struct pbe)) |
1467 | ||
940864dd RW |
1468 | static int |
1469 | prepare_image(struct memory_bitmap *new_bm, struct memory_bitmap *bm) | |
f577eb30 | 1470 | { |
8357376d | 1471 | unsigned int nr_pages, nr_highmem; |
940864dd RW |
1472 | struct linked_page *sp_list, *lp; |
1473 | int error; | |
f577eb30 | 1474 | |
8357376d RW |
1475 | /* If there is no highmem, the buffer will not be necessary */ |
1476 | free_image_page(buffer, PG_UNSAFE_CLEAR); | |
1477 | buffer = NULL; | |
1478 | ||
1479 | nr_highmem = count_highmem_image_pages(bm); | |
940864dd RW |
1480 | error = mark_unsafe_pages(bm); |
1481 | if (error) | |
1482 | goto Free; | |
1483 | ||
1484 | error = memory_bm_create(new_bm, GFP_ATOMIC, PG_SAFE); | |
1485 | if (error) | |
1486 | goto Free; | |
1487 | ||
1488 | duplicate_memory_bitmap(new_bm, bm); | |
1489 | memory_bm_free(bm, PG_UNSAFE_KEEP); | |
8357376d RW |
1490 | if (nr_highmem > 0) { |
1491 | error = prepare_highmem_image(bm, &nr_highmem); | |
1492 | if (error) | |
1493 | goto Free; | |
1494 | } | |
940864dd RW |
1495 | /* Reserve some safe pages for potential later use. |
1496 | * | |
1497 | * NOTE: This way we make sure there will be enough safe pages for the | |
1498 | * chain_alloc() in get_buffer(). It is a bit wasteful, but | |
1499 | * nr_copy_pages cannot be greater than 50% of the memory anyway. | |
1500 | */ | |
1501 | sp_list = NULL; | |
1502 | /* nr_copy_pages cannot be lesser than allocated_unsafe_pages */ | |
8357376d | 1503 | nr_pages = nr_copy_pages - nr_highmem - allocated_unsafe_pages; |
940864dd RW |
1504 | nr_pages = DIV_ROUND_UP(nr_pages, PBES_PER_LINKED_PAGE); |
1505 | while (nr_pages > 0) { | |
8357376d | 1506 | lp = get_image_page(GFP_ATOMIC, PG_SAFE); |
940864dd | 1507 | if (!lp) { |
f577eb30 | 1508 | error = -ENOMEM; |
940864dd RW |
1509 | goto Free; |
1510 | } | |
1511 | lp->next = sp_list; | |
1512 | sp_list = lp; | |
1513 | nr_pages--; | |
f577eb30 | 1514 | } |
940864dd RW |
1515 | /* Preallocate memory for the image */ |
1516 | safe_pages_list = NULL; | |
8357376d | 1517 | nr_pages = nr_copy_pages - nr_highmem - allocated_unsafe_pages; |
940864dd RW |
1518 | while (nr_pages > 0) { |
1519 | lp = (struct linked_page *)get_zeroed_page(GFP_ATOMIC); | |
1520 | if (!lp) { | |
1521 | error = -ENOMEM; | |
1522 | goto Free; | |
1523 | } | |
1524 | if (!PageNosaveFree(virt_to_page(lp))) { | |
1525 | /* The page is "safe", add it to the list */ | |
1526 | lp->next = safe_pages_list; | |
1527 | safe_pages_list = lp; | |
968808b8 | 1528 | } |
940864dd RW |
1529 | /* Mark the page as allocated */ |
1530 | SetPageNosave(virt_to_page(lp)); | |
1531 | SetPageNosaveFree(virt_to_page(lp)); | |
1532 | nr_pages--; | |
968808b8 | 1533 | } |
940864dd RW |
1534 | /* Free the reserved safe pages so that chain_alloc() can use them */ |
1535 | while (sp_list) { | |
1536 | lp = sp_list->next; | |
1537 | free_image_page(sp_list, PG_UNSAFE_CLEAR); | |
1538 | sp_list = lp; | |
f577eb30 | 1539 | } |
940864dd RW |
1540 | return 0; |
1541 | ||
1542 | Free: | |
1543 | swsusp_free(); | |
f577eb30 RW |
1544 | return error; |
1545 | } | |
1546 | ||
940864dd RW |
1547 | /** |
1548 | * get_buffer - compute the address that snapshot_write_next() should | |
1549 | * set for its caller to write to. | |
1550 | */ | |
1551 | ||
1552 | static void *get_buffer(struct memory_bitmap *bm, struct chain_allocator *ca) | |
968808b8 | 1553 | { |
940864dd RW |
1554 | struct pbe *pbe; |
1555 | struct page *page = pfn_to_page(memory_bm_next_pfn(bm)); | |
968808b8 | 1556 | |
8357376d RW |
1557 | if (PageHighMem(page)) |
1558 | return get_highmem_page_buffer(page, ca); | |
1559 | ||
940864dd RW |
1560 | if (PageNosave(page) && PageNosaveFree(page)) |
1561 | /* We have allocated the "original" page frame and we can | |
1562 | * use it directly to store the loaded page. | |
968808b8 | 1563 | */ |
940864dd RW |
1564 | return page_address(page); |
1565 | ||
1566 | /* The "original" page frame has not been allocated and we have to | |
1567 | * use a "safe" page frame to store the loaded page. | |
968808b8 | 1568 | */ |
940864dd RW |
1569 | pbe = chain_alloc(ca, sizeof(struct pbe)); |
1570 | if (!pbe) { | |
1571 | swsusp_free(); | |
1572 | return NULL; | |
1573 | } | |
8357376d RW |
1574 | pbe->orig_address = page_address(page); |
1575 | pbe->address = safe_pages_list; | |
940864dd RW |
1576 | safe_pages_list = safe_pages_list->next; |
1577 | pbe->next = restore_pblist; | |
1578 | restore_pblist = pbe; | |
8357376d | 1579 | return pbe->address; |
968808b8 RW |
1580 | } |
1581 | ||
f577eb30 RW |
1582 | /** |
1583 | * snapshot_write_next - used for writing the system memory snapshot. | |
1584 | * | |
1585 | * On the first call to it @handle should point to a zeroed | |
1586 | * snapshot_handle structure. The structure gets updated and a pointer | |
1587 | * to it should be passed to this function every next time. | |
1588 | * | |
1589 | * The @count parameter should contain the number of bytes the caller | |
1590 | * wants to write to the image. It must not be zero. | |
1591 | * | |
1592 | * On success the function returns a positive number. Then, the caller | |
1593 | * is allowed to write up to the returned number of bytes to the memory | |
1594 | * location computed by the data_of() macro. The number returned | |
1595 | * may be smaller than @count, but this only happens if the write would | |
1596 | * cross a page boundary otherwise. | |
1597 | * | |
1598 | * The function returns 0 to indicate the "end of file" condition, | |
1599 | * and a negative number is returned on error. In such cases the | |
1600 | * structure pointed to by @handle is not updated and should not be used | |
1601 | * any more. | |
1602 | */ | |
1603 | ||
1604 | int snapshot_write_next(struct snapshot_handle *handle, size_t count) | |
1605 | { | |
940864dd | 1606 | static struct chain_allocator ca; |
f577eb30 RW |
1607 | int error = 0; |
1608 | ||
940864dd | 1609 | /* Check if we have already loaded the entire image */ |
fb13a28b | 1610 | if (handle->prev && handle->cur > nr_meta_pages + nr_copy_pages) |
f577eb30 | 1611 | return 0; |
940864dd | 1612 | |
8357376d RW |
1613 | if (handle->offset == 0) { |
1614 | if (!buffer) | |
1615 | /* This makes the buffer be freed by swsusp_free() */ | |
1616 | buffer = get_image_page(GFP_ATOMIC, PG_ANY); | |
1617 | ||
f577eb30 RW |
1618 | if (!buffer) |
1619 | return -ENOMEM; | |
8357376d | 1620 | |
f577eb30 | 1621 | handle->buffer = buffer; |
8357376d | 1622 | } |
546e0d27 | 1623 | handle->sync_read = 1; |
fb13a28b | 1624 | if (handle->prev < handle->cur) { |
940864dd RW |
1625 | if (handle->prev == 0) { |
1626 | error = load_header(buffer); | |
1627 | if (error) | |
1628 | return error; | |
1629 | ||
1630 | error = memory_bm_create(©_bm, GFP_ATOMIC, PG_ANY); | |
f577eb30 RW |
1631 | if (error) |
1632 | return error; | |
940864dd | 1633 | |
f577eb30 | 1634 | } else if (handle->prev <= nr_meta_pages) { |
940864dd RW |
1635 | unpack_orig_pfns(buffer, ©_bm); |
1636 | if (handle->prev == nr_meta_pages) { | |
1637 | error = prepare_image(&orig_bm, ©_bm); | |
f577eb30 RW |
1638 | if (error) |
1639 | return error; | |
940864dd RW |
1640 | |
1641 | chain_init(&ca, GFP_ATOMIC, PG_SAFE); | |
1642 | memory_bm_position_reset(&orig_bm); | |
1643 | restore_pblist = NULL; | |
1644 | handle->buffer = get_buffer(&orig_bm, &ca); | |
546e0d27 | 1645 | handle->sync_read = 0; |
940864dd RW |
1646 | if (!handle->buffer) |
1647 | return -ENOMEM; | |
f577eb30 RW |
1648 | } |
1649 | } else { | |
8357376d | 1650 | copy_last_highmem_page(); |
940864dd | 1651 | handle->buffer = get_buffer(&orig_bm, &ca); |
8357376d RW |
1652 | if (handle->buffer != buffer) |
1653 | handle->sync_read = 0; | |
f577eb30 | 1654 | } |
fb13a28b | 1655 | handle->prev = handle->cur; |
f577eb30 | 1656 | } |
fb13a28b RW |
1657 | handle->buf_offset = handle->cur_offset; |
1658 | if (handle->cur_offset + count >= PAGE_SIZE) { | |
1659 | count = PAGE_SIZE - handle->cur_offset; | |
1660 | handle->cur_offset = 0; | |
1661 | handle->cur++; | |
f577eb30 | 1662 | } else { |
fb13a28b | 1663 | handle->cur_offset += count; |
f577eb30 RW |
1664 | } |
1665 | handle->offset += count; | |
1666 | return count; | |
1667 | } | |
1668 | ||
8357376d RW |
1669 | /** |
1670 | * snapshot_write_finalize - must be called after the last call to | |
1671 | * snapshot_write_next() in case the last page in the image happens | |
1672 | * to be a highmem page and its contents should be stored in the | |
1673 | * highmem. Additionally, it releases the memory that will not be | |
1674 | * used any more. | |
1675 | */ | |
1676 | ||
1677 | void snapshot_write_finalize(struct snapshot_handle *handle) | |
1678 | { | |
1679 | copy_last_highmem_page(); | |
1680 | /* Free only if we have loaded the image entirely */ | |
1681 | if (handle->prev && handle->cur > nr_meta_pages + nr_copy_pages) { | |
1682 | memory_bm_free(&orig_bm, PG_UNSAFE_CLEAR); | |
1683 | free_highmem_data(); | |
1684 | } | |
1685 | } | |
1686 | ||
f577eb30 RW |
1687 | int snapshot_image_loaded(struct snapshot_handle *handle) |
1688 | { | |
8357376d | 1689 | return !(!nr_copy_pages || !last_highmem_page_copied() || |
940864dd RW |
1690 | handle->cur <= nr_meta_pages + nr_copy_pages); |
1691 | } | |
1692 | ||
8357376d RW |
1693 | #ifdef CONFIG_HIGHMEM |
1694 | /* Assumes that @buf is ready and points to a "safe" page */ | |
1695 | static inline void | |
1696 | swap_two_pages_data(struct page *p1, struct page *p2, void *buf) | |
940864dd | 1697 | { |
8357376d RW |
1698 | void *kaddr1, *kaddr2; |
1699 | ||
1700 | kaddr1 = kmap_atomic(p1, KM_USER0); | |
1701 | kaddr2 = kmap_atomic(p2, KM_USER1); | |
1702 | memcpy(buf, kaddr1, PAGE_SIZE); | |
1703 | memcpy(kaddr1, kaddr2, PAGE_SIZE); | |
1704 | memcpy(kaddr2, buf, PAGE_SIZE); | |
1705 | kunmap_atomic(kaddr1, KM_USER0); | |
1706 | kunmap_atomic(kaddr2, KM_USER1); | |
1707 | } | |
1708 | ||
1709 | /** | |
1710 | * restore_highmem - for each highmem page that was allocated before | |
1711 | * the suspend and included in the suspend image, and also has been | |
1712 | * allocated by the "resume" kernel swap its current (ie. "before | |
1713 | * resume") contents with the previous (ie. "before suspend") one. | |
1714 | * | |
1715 | * If the resume eventually fails, we can call this function once | |
1716 | * again and restore the "before resume" highmem state. | |
1717 | */ | |
1718 | ||
1719 | int restore_highmem(void) | |
1720 | { | |
1721 | struct highmem_pbe *pbe = highmem_pblist; | |
1722 | void *buf; | |
1723 | ||
1724 | if (!pbe) | |
1725 | return 0; | |
1726 | ||
1727 | buf = get_image_page(GFP_ATOMIC, PG_SAFE); | |
1728 | if (!buf) | |
1729 | return -ENOMEM; | |
1730 | ||
1731 | while (pbe) { | |
1732 | swap_two_pages_data(pbe->copy_page, pbe->orig_page, buf); | |
1733 | pbe = pbe->next; | |
1734 | } | |
1735 | free_image_page(buf, PG_UNSAFE_CLEAR); | |
1736 | return 0; | |
f577eb30 | 1737 | } |
8357376d | 1738 | #endif /* CONFIG_HIGHMEM */ |