blob: 5f91a07c4eac7e104f3fc9c333a481be11ead921 [file] [log] [blame]
Rafael J. Wysocki25761b62005-10-30 14:59:56 -08001/*
Pavel Machek96bc7ae2005-10-30 14:59:58 -08002 * linux/kernel/power/snapshot.c
Rafael J. Wysocki25761b62005-10-30 14:59:56 -08003 *
Rafael J. Wysocki83573762006-12-06 20:34:18 -08004 * This file provides system snapshot/restore functionality for swsusp.
Rafael J. Wysocki25761b62005-10-30 14:59:56 -08005 *
6 * Copyright (C) 1998-2005 Pavel Machek <pavel@suse.cz>
Rafael J. Wysocki83573762006-12-06 20:34:18 -08007 * Copyright (C) 2006 Rafael J. Wysocki <rjw@sisk.pl>
Rafael J. Wysocki25761b62005-10-30 14:59:56 -08008 *
Rafael J. Wysocki83573762006-12-06 20:34:18 -08009 * This file is released under the GPLv2.
Rafael J. Wysocki25761b62005-10-30 14:59:56 -080010 *
11 */
12
Rafael J. Wysockif577eb32006-03-23 02:59:59 -080013#include <linux/version.h>
Rafael J. Wysocki25761b62005-10-30 14:59:56 -080014#include <linux/module.h>
15#include <linux/mm.h>
16#include <linux/suspend.h>
Rafael J. Wysocki25761b62005-10-30 14:59:56 -080017#include <linux/delay.h>
Rafael J. Wysocki25761b62005-10-30 14:59:56 -080018#include <linux/bitops.h>
Rafael J. Wysocki25761b62005-10-30 14:59:56 -080019#include <linux/spinlock.h>
Rafael J. Wysocki25761b62005-10-30 14:59:56 -080020#include <linux/kernel.h>
Rafael J. Wysocki25761b62005-10-30 14:59:56 -080021#include <linux/pm.h>
22#include <linux/device.h>
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -070023#include <linux/init.h>
Rafael J. Wysocki25761b62005-10-30 14:59:56 -080024#include <linux/bootmem.h>
25#include <linux/syscalls.h>
26#include <linux/console.h>
27#include <linux/highmem.h>
Rafael J. Wysocki25761b62005-10-30 14:59:56 -080028
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
Rafael J. Wysocki25761b62005-10-30 14:59:56 -080035#include "power.h"
36
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -070037static int swsusp_page_is_free(struct page *);
38static void swsusp_set_page_forbidden(struct page *);
39static void swsusp_unset_page_forbidden(struct page *);
40
Rafael J. Wysocki83573762006-12-06 20:34:18 -080041/* List of PBEs needed for restoring the pages that were allocated before
42 * the suspend and included in the suspend image, but have also been
43 * allocated by the "resume" kernel, so their contents cannot be written
44 * directly to their "original" page frames.
45 */
Rafael J. Wysocki75534b52006-09-25 23:32:52 -070046struct pbe *restore_pblist;
47
Rafael J. Wysocki83573762006-12-06 20:34:18 -080048/* Pointer to an auxiliary buffer (1 page) */
Rafael J. Wysocki940864d2006-09-25 23:32:55 -070049static void *buffer;
Rafael J. Wysocki7088a5c2006-01-06 00:13:05 -080050
Rafael J. Wysockif6143aa2006-09-25 23:32:50 -070051/**
52 * @safe_needed - on resume, for storing the PBE list and the image,
53 * we can only use memory pages that do not conflict with the pages
Rafael J. Wysocki83573762006-12-06 20:34:18 -080054 * used before suspend. The unsafe pages have PageNosaveFree set
55 * and we count them using unsafe_pages.
Rafael J. Wysockif6143aa2006-09-25 23:32:50 -070056 *
Rafael J. Wysocki83573762006-12-06 20:34:18 -080057 * Each allocated image page is marked as PageNosave and PageNosaveFree
58 * so that swsusp_free() can release it.
Rafael J. Wysockif6143aa2006-09-25 23:32:50 -070059 */
60
Rafael J. Wysocki0bcd8882006-09-25 23:32:52 -070061#define PG_ANY 0
62#define PG_SAFE 1
63#define PG_UNSAFE_CLEAR 1
64#define PG_UNSAFE_KEEP 0
65
Rafael J. Wysocki940864d2006-09-25 23:32:55 -070066static unsigned int allocated_unsafe_pages;
Rafael J. Wysockif6143aa2006-09-25 23:32:50 -070067
Rafael J. Wysocki83573762006-12-06 20:34:18 -080068static void *get_image_page(gfp_t gfp_mask, int safe_needed)
Rafael J. Wysockif6143aa2006-09-25 23:32:50 -070069{
70 void *res;
71
72 res = (void *)get_zeroed_page(gfp_mask);
73 if (safe_needed)
Rafael J. Wysocki7be98232007-05-06 14:50:42 -070074 while (res && swsusp_page_is_free(virt_to_page(res))) {
Rafael J. Wysockif6143aa2006-09-25 23:32:50 -070075 /* The page is unsafe, mark it for swsusp_free() */
Rafael J. Wysocki7be98232007-05-06 14:50:42 -070076 swsusp_set_page_forbidden(virt_to_page(res));
Rafael J. Wysocki940864d2006-09-25 23:32:55 -070077 allocated_unsafe_pages++;
Rafael J. Wysockif6143aa2006-09-25 23:32:50 -070078 res = (void *)get_zeroed_page(gfp_mask);
79 }
80 if (res) {
Rafael J. Wysocki7be98232007-05-06 14:50:42 -070081 swsusp_set_page_forbidden(virt_to_page(res));
82 swsusp_set_page_free(virt_to_page(res));
Rafael J. Wysockif6143aa2006-09-25 23:32:50 -070083 }
84 return res;
85}
86
87unsigned long get_safe_page(gfp_t gfp_mask)
88{
Rafael J. Wysocki83573762006-12-06 20:34:18 -080089 return (unsigned long)get_image_page(gfp_mask, PG_SAFE);
90}
91
Rafael J. Wysocki5b6d15d2006-12-06 20:34:43 -080092static struct page *alloc_image_page(gfp_t gfp_mask)
93{
Rafael J. Wysocki83573762006-12-06 20:34:18 -080094 struct page *page;
95
96 page = alloc_page(gfp_mask);
97 if (page) {
Rafael J. Wysocki7be98232007-05-06 14:50:42 -070098 swsusp_set_page_forbidden(page);
99 swsusp_set_page_free(page);
Rafael J. Wysocki83573762006-12-06 20:34:18 -0800100 }
101 return page;
Rafael J. Wysockif6143aa2006-09-25 23:32:50 -0700102}
103
104/**
105 * free_image_page - free page represented by @addr, allocated with
Rafael J. Wysocki83573762006-12-06 20:34:18 -0800106 * get_image_page (page flags set by it must be cleared)
Rafael J. Wysockif6143aa2006-09-25 23:32:50 -0700107 */
108
109static inline void free_image_page(void *addr, int clear_nosave_free)
110{
Rafael J. Wysocki83573762006-12-06 20:34:18 -0800111 struct page *page;
112
113 BUG_ON(!virt_addr_valid(addr));
114
115 page = virt_to_page(addr);
116
Rafael J. Wysocki7be98232007-05-06 14:50:42 -0700117 swsusp_unset_page_forbidden(page);
Rafael J. Wysockif6143aa2006-09-25 23:32:50 -0700118 if (clear_nosave_free)
Rafael J. Wysocki7be98232007-05-06 14:50:42 -0700119 swsusp_unset_page_free(page);
Rafael J. Wysocki83573762006-12-06 20:34:18 -0800120
121 __free_page(page);
Rafael J. Wysockif6143aa2006-09-25 23:32:50 -0700122}
123
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700124/* struct linked_page is used to build chains of pages */
125
126#define LINKED_PAGE_DATA_SIZE (PAGE_SIZE - sizeof(void *))
127
128struct linked_page {
129 struct linked_page *next;
130 char data[LINKED_PAGE_DATA_SIZE];
131} __attribute__((packed));
132
133static inline void
134free_list_of_pages(struct linked_page *list, int clear_page_nosave)
135{
136 while (list) {
137 struct linked_page *lp = list->next;
138
139 free_image_page(list, clear_page_nosave);
140 list = lp;
141 }
142}
143
144/**
145 * struct chain_allocator is used for allocating small objects out of
146 * a linked list of pages called 'the chain'.
147 *
148 * The chain grows each time when there is no room for a new object in
149 * the current page. The allocated objects cannot be freed individually.
150 * It is only possible to free them all at once, by freeing the entire
151 * chain.
152 *
153 * NOTE: The chain allocator may be inefficient if the allocated objects
154 * are not much smaller than PAGE_SIZE.
155 */
156
157struct chain_allocator {
158 struct linked_page *chain; /* the chain */
159 unsigned int used_space; /* total size of objects allocated out
160 * of the current page
161 */
162 gfp_t gfp_mask; /* mask for allocating pages */
163 int safe_needed; /* if set, only "safe" pages are allocated */
164};
165
166static void
167chain_init(struct chain_allocator *ca, gfp_t gfp_mask, int safe_needed)
168{
169 ca->chain = NULL;
170 ca->used_space = LINKED_PAGE_DATA_SIZE;
171 ca->gfp_mask = gfp_mask;
172 ca->safe_needed = safe_needed;
173}
174
175static void *chain_alloc(struct chain_allocator *ca, unsigned int size)
176{
177 void *ret;
178
179 if (LINKED_PAGE_DATA_SIZE - ca->used_space < size) {
180 struct linked_page *lp;
181
Rafael J. Wysocki83573762006-12-06 20:34:18 -0800182 lp = get_image_page(ca->gfp_mask, ca->safe_needed);
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700183 if (!lp)
184 return NULL;
185
186 lp->next = ca->chain;
187 ca->chain = lp;
188 ca->used_space = 0;
189 }
190 ret = ca->chain->data + ca->used_space;
191 ca->used_space += size;
192 return ret;
193}
194
195static void chain_free(struct chain_allocator *ca, int clear_page_nosave)
196{
197 free_list_of_pages(ca->chain, clear_page_nosave);
198 memset(ca, 0, sizeof(struct chain_allocator));
199}
200
201/**
202 * Data types related to memory bitmaps.
203 *
204 * Memory bitmap is a structure consiting of many linked lists of
205 * objects. The main list's elements are of type struct zone_bitmap
206 * and each of them corresonds to one zone. For each zone bitmap
207 * object there is a list of objects of type struct bm_block that
208 * represent each blocks of bit chunks in which information is
209 * stored.
210 *
211 * struct memory_bitmap contains a pointer to the main list of zone
212 * bitmap objects, a struct bm_position used for browsing the bitmap,
213 * and a pointer to the list of pages used for allocating all of the
214 * zone bitmap objects and bitmap block objects.
215 *
216 * NOTE: It has to be possible to lay out the bitmap in memory
217 * using only allocations of order 0. Additionally, the bitmap is
218 * designed to work with arbitrary number of zones (this is over the
219 * top for now, but let's avoid making unnecessary assumptions ;-).
220 *
221 * struct zone_bitmap contains a pointer to a list of bitmap block
222 * objects and a pointer to the bitmap block object that has been
223 * most recently used for setting bits. Additionally, it contains the
224 * pfns that correspond to the start and end of the represented zone.
225 *
226 * struct bm_block contains a pointer to the memory page in which
227 * information is stored (in the form of a block of bit chunks
228 * of type unsigned long each). It also contains the pfns that
229 * correspond to the start and end of the represented memory area and
230 * the number of bit chunks in the block.
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700231 */
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
239struct 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
247struct 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
257struct bm_position {
258 struct zone_bitmap *zone_bm;
259 struct bm_block *block;
260 int chunk;
261 int bit;
262};
263
264struct 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
275static inline void memory_bm_reset_chunk(struct memory_bitmap *bm)
276{
277 bm->cur.chunk = 0;
278 bm->cur.bit = -1;
279}
280
281static 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
291static 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
297static inline struct bm_block *
298create_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
319static inline struct zone_bitmap *
320create_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
341static int
342memory_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;
Rafael J. Wysocki83573762006-12-06 20:34:18 -0800354 for_each_zone(zone)
355 if (populated_zone(zone))
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700356 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 */
Rafael J. Wysocki83573762006-12-06 20:34:18 -0800367 for_each_zone(zone) {
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700368 unsigned long pfn;
369
Rafael J. Wysocki83573762006-12-06 20:34:18 -0800370 if (!populated_zone(zone))
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700371 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
Rafael J. Wysocki83573762006-12-06 20:34:18 -0800389 ptr = get_image_page(gfp_mask, safe_needed);
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700390 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
Rafael J. Wysocki59a493352006-12-06 20:34:44 -0800413 Free:
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700414 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
423static 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/**
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -0700445 * memory_bm_find_bit - find the bit in the bitmap @bm that corresponds
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700446 * to given pfn. The cur_zone_bm member of @bm and the cur_block member
447 * of @bm->cur_zone_bm are updated.
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700448 */
449
Rafael J. Wysockia82f7112008-03-12 00:34:57 +0100450static int memory_bm_find_bit(struct memory_bitmap *bm, unsigned long pfn,
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -0700451 void **addr, unsigned int *bit_nr)
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700452{
453 struct zone_bitmap *zone_bm;
454 struct bm_block *bb;
455
456 /* Check if the pfn is from the current zone */
457 zone_bm = bm->cur.zone_bm;
458 if (pfn < zone_bm->start_pfn || pfn >= zone_bm->end_pfn) {
459 zone_bm = bm->zone_bm_list;
460 /* We don't assume that the zones are sorted by pfns */
461 while (pfn < zone_bm->start_pfn || pfn >= zone_bm->end_pfn) {
462 zone_bm = zone_bm->next;
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -0700463
Rafael J. Wysockia82f7112008-03-12 00:34:57 +0100464 if (!zone_bm)
465 return -EFAULT;
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700466 }
467 bm->cur.zone_bm = zone_bm;
468 }
469 /* Check if the pfn corresponds to the current bitmap block */
470 bb = zone_bm->cur_block;
471 if (pfn < bb->start_pfn)
472 bb = zone_bm->bm_blocks;
473
474 while (pfn >= bb->end_pfn) {
475 bb = bb->next;
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -0700476
477 BUG_ON(!bb);
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700478 }
479 zone_bm->cur_block = bb;
480 pfn -= bb->start_pfn;
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -0700481 *bit_nr = pfn % BM_BITS_PER_CHUNK;
482 *addr = bb->data + pfn / BM_BITS_PER_CHUNK;
Rafael J. Wysockia82f7112008-03-12 00:34:57 +0100483 return 0;
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -0700484}
485
486static void memory_bm_set_bit(struct memory_bitmap *bm, unsigned long pfn)
487{
488 void *addr;
489 unsigned int bit;
Rafael J. Wysockia82f7112008-03-12 00:34:57 +0100490 int error;
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -0700491
Rafael J. Wysockia82f7112008-03-12 00:34:57 +0100492 error = memory_bm_find_bit(bm, pfn, &addr, &bit);
493 BUG_ON(error);
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -0700494 set_bit(bit, addr);
495}
496
Rafael J. Wysockia82f7112008-03-12 00:34:57 +0100497static int mem_bm_set_bit_check(struct memory_bitmap *bm, unsigned long pfn)
498{
499 void *addr;
500 unsigned int bit;
501 int error;
502
503 error = memory_bm_find_bit(bm, pfn, &addr, &bit);
504 if (!error)
505 set_bit(bit, addr);
506 return error;
507}
508
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -0700509static void memory_bm_clear_bit(struct memory_bitmap *bm, unsigned long pfn)
510{
511 void *addr;
512 unsigned int bit;
Rafael J. Wysockia82f7112008-03-12 00:34:57 +0100513 int error;
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -0700514
Rafael J. Wysockia82f7112008-03-12 00:34:57 +0100515 error = memory_bm_find_bit(bm, pfn, &addr, &bit);
516 BUG_ON(error);
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -0700517 clear_bit(bit, addr);
518}
519
520static int memory_bm_test_bit(struct memory_bitmap *bm, unsigned long pfn)
521{
522 void *addr;
523 unsigned int bit;
Rafael J. Wysockia82f7112008-03-12 00:34:57 +0100524 int error;
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -0700525
Rafael J. Wysockia82f7112008-03-12 00:34:57 +0100526 error = memory_bm_find_bit(bm, pfn, &addr, &bit);
527 BUG_ON(error);
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -0700528 return test_bit(bit, addr);
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700529}
530
531/* Two auxiliary functions for memory_bm_next_pfn */
532
533/* Find the first set bit in the given chunk, if there is one */
534
535static inline int next_bit_in_chunk(int bit, unsigned long *chunk_p)
536{
537 bit++;
538 while (bit < BM_BITS_PER_CHUNK) {
539 if (test_bit(bit, chunk_p))
540 return bit;
541
542 bit++;
543 }
544 return -1;
545}
546
547/* Find a chunk containing some bits set in given block of bits */
548
549static inline int next_chunk_in_block(int n, struct bm_block *bb)
550{
551 n++;
552 while (n < bb->size) {
553 if (bb->data[n])
554 return n;
555
556 n++;
557 }
558 return -1;
559}
560
561/**
562 * memory_bm_next_pfn - find the pfn that corresponds to the next set bit
563 * in the bitmap @bm. If the pfn cannot be found, BM_END_OF_MAP is
564 * returned.
565 *
566 * It is required to run memory_bm_position_reset() before the first call to
567 * this function.
568 */
569
570static unsigned long memory_bm_next_pfn(struct memory_bitmap *bm)
571{
572 struct zone_bitmap *zone_bm;
573 struct bm_block *bb;
574 int chunk;
575 int bit;
576
577 do {
578 bb = bm->cur.block;
579 do {
580 chunk = bm->cur.chunk;
581 bit = bm->cur.bit;
582 do {
583 bit = next_bit_in_chunk(bit, bb->data + chunk);
584 if (bit >= 0)
585 goto Return_pfn;
586
587 chunk = next_chunk_in_block(chunk, bb);
588 bit = -1;
589 } while (chunk >= 0);
590 bb = bb->next;
591 bm->cur.block = bb;
592 memory_bm_reset_chunk(bm);
593 } while (bb);
594 zone_bm = bm->cur.zone_bm->next;
595 if (zone_bm) {
596 bm->cur.zone_bm = zone_bm;
597 bm->cur.block = zone_bm->bm_blocks;
598 memory_bm_reset_chunk(bm);
599 }
600 } while (zone_bm);
601 memory_bm_position_reset(bm);
602 return BM_END_OF_MAP;
603
Rafael J. Wysocki59a493352006-12-06 20:34:44 -0800604 Return_pfn:
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700605 bm->cur.chunk = chunk;
606 bm->cur.bit = bit;
607 return bb->start_pfn + chunk * BM_BITS_PER_CHUNK + bit;
608}
609
610/**
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -0700611 * This structure represents a range of page frames the contents of which
612 * should not be saved during the suspend.
613 */
614
615struct nosave_region {
616 struct list_head list;
617 unsigned long start_pfn;
618 unsigned long end_pfn;
619};
620
621static LIST_HEAD(nosave_regions);
622
623/**
624 * register_nosave_region - register a range of page frames the contents
625 * of which should not be saved during the suspend (to be used in the early
626 * initialization code)
627 */
628
629void __init
Johannes Berg940d67f2007-05-08 19:23:49 +1000630__register_nosave_region(unsigned long start_pfn, unsigned long end_pfn,
631 int use_kmalloc)
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -0700632{
633 struct nosave_region *region;
634
635 if (start_pfn >= end_pfn)
636 return;
637
638 if (!list_empty(&nosave_regions)) {
639 /* Try to extend the previous region (they should be sorted) */
640 region = list_entry(nosave_regions.prev,
641 struct nosave_region, list);
642 if (region->end_pfn == start_pfn) {
643 region->end_pfn = end_pfn;
644 goto Report;
645 }
646 }
Johannes Berg940d67f2007-05-08 19:23:49 +1000647 if (use_kmalloc) {
648 /* during init, this shouldn't fail */
649 region = kmalloc(sizeof(struct nosave_region), GFP_KERNEL);
650 BUG_ON(!region);
651 } else
652 /* This allocation cannot fail */
653 region = alloc_bootmem_low(sizeof(struct nosave_region));
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -0700654 region->start_pfn = start_pfn;
655 region->end_pfn = end_pfn;
656 list_add_tail(&region->list, &nosave_regions);
657 Report:
Rafael J. Wysocki23976722007-12-08 02:09:43 +0100658 printk(KERN_INFO "PM: Registered nosave memory: %016lx - %016lx\n",
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -0700659 start_pfn << PAGE_SHIFT, end_pfn << PAGE_SHIFT);
660}
661
662/*
663 * Set bits in this map correspond to the page frames the contents of which
664 * should not be saved during the suspend.
665 */
666static struct memory_bitmap *forbidden_pages_map;
667
668/* Set bits in this map correspond to free page frames. */
669static struct memory_bitmap *free_pages_map;
670
671/*
672 * Each page frame allocated for creating the image is marked by setting the
673 * corresponding bits in forbidden_pages_map and free_pages_map simultaneously
674 */
675
676void swsusp_set_page_free(struct page *page)
677{
678 if (free_pages_map)
679 memory_bm_set_bit(free_pages_map, page_to_pfn(page));
680}
681
682static int swsusp_page_is_free(struct page *page)
683{
684 return free_pages_map ?
685 memory_bm_test_bit(free_pages_map, page_to_pfn(page)) : 0;
686}
687
688void swsusp_unset_page_free(struct page *page)
689{
690 if (free_pages_map)
691 memory_bm_clear_bit(free_pages_map, page_to_pfn(page));
692}
693
694static void swsusp_set_page_forbidden(struct page *page)
695{
696 if (forbidden_pages_map)
697 memory_bm_set_bit(forbidden_pages_map, page_to_pfn(page));
698}
699
700int swsusp_page_is_forbidden(struct page *page)
701{
702 return forbidden_pages_map ?
703 memory_bm_test_bit(forbidden_pages_map, page_to_pfn(page)) : 0;
704}
705
706static void swsusp_unset_page_forbidden(struct page *page)
707{
708 if (forbidden_pages_map)
709 memory_bm_clear_bit(forbidden_pages_map, page_to_pfn(page));
710}
711
712/**
713 * mark_nosave_pages - set bits corresponding to the page frames the
714 * contents of which should not be saved in a given bitmap.
715 */
716
717static void mark_nosave_pages(struct memory_bitmap *bm)
718{
719 struct nosave_region *region;
720
721 if (list_empty(&nosave_regions))
722 return;
723
724 list_for_each_entry(region, &nosave_regions, list) {
725 unsigned long pfn;
726
Rafael J. Wysocki23976722007-12-08 02:09:43 +0100727 pr_debug("PM: Marking nosave pages: %016lx - %016lx\n",
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -0700728 region->start_pfn << PAGE_SHIFT,
729 region->end_pfn << PAGE_SHIFT);
730
731 for (pfn = region->start_pfn; pfn < region->end_pfn; pfn++)
Rafael J. Wysockia82f7112008-03-12 00:34:57 +0100732 if (pfn_valid(pfn)) {
733 /*
734 * It is safe to ignore the result of
735 * mem_bm_set_bit_check() here, since we won't
736 * touch the PFNs for which the error is
737 * returned anyway.
738 */
739 mem_bm_set_bit_check(bm, pfn);
740 }
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -0700741 }
742}
743
744/**
745 * create_basic_memory_bitmaps - create bitmaps needed for marking page
746 * frames that should not be saved and free page frames. The pointers
747 * forbidden_pages_map and free_pages_map are only modified if everything
748 * goes well, because we don't want the bits to be used before both bitmaps
749 * are set up.
750 */
751
752int create_basic_memory_bitmaps(void)
753{
754 struct memory_bitmap *bm1, *bm2;
755 int error = 0;
756
757 BUG_ON(forbidden_pages_map || free_pages_map);
758
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700759 bm1 = kzalloc(sizeof(struct memory_bitmap), GFP_KERNEL);
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -0700760 if (!bm1)
761 return -ENOMEM;
762
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700763 error = memory_bm_create(bm1, GFP_KERNEL, PG_ANY);
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -0700764 if (error)
765 goto Free_first_object;
766
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700767 bm2 = kzalloc(sizeof(struct memory_bitmap), GFP_KERNEL);
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -0700768 if (!bm2)
769 goto Free_first_bitmap;
770
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700771 error = memory_bm_create(bm2, GFP_KERNEL, PG_ANY);
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -0700772 if (error)
773 goto Free_second_object;
774
775 forbidden_pages_map = bm1;
776 free_pages_map = bm2;
777 mark_nosave_pages(forbidden_pages_map);
778
Rafael J. Wysocki23976722007-12-08 02:09:43 +0100779 pr_debug("PM: Basic memory bitmaps created\n");
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -0700780
781 return 0;
782
783 Free_second_object:
784 kfree(bm2);
785 Free_first_bitmap:
786 memory_bm_free(bm1, PG_UNSAFE_CLEAR);
787 Free_first_object:
788 kfree(bm1);
789 return -ENOMEM;
790}
791
792/**
793 * free_basic_memory_bitmaps - free memory bitmaps allocated by
794 * create_basic_memory_bitmaps(). The auxiliary pointers are necessary
795 * so that the bitmaps themselves are not referred to while they are being
796 * freed.
797 */
798
799void free_basic_memory_bitmaps(void)
800{
801 struct memory_bitmap *bm1, *bm2;
802
803 BUG_ON(!(forbidden_pages_map && free_pages_map));
804
805 bm1 = forbidden_pages_map;
806 bm2 = free_pages_map;
807 forbidden_pages_map = NULL;
808 free_pages_map = NULL;
809 memory_bm_free(bm1, PG_UNSAFE_CLEAR);
810 kfree(bm1);
811 memory_bm_free(bm2, PG_UNSAFE_CLEAR);
812 kfree(bm2);
813
Rafael J. Wysocki23976722007-12-08 02:09:43 +0100814 pr_debug("PM: Basic memory bitmaps freed\n");
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -0700815}
816
817/**
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700818 * snapshot_additional_pages - estimate the number of additional pages
819 * be needed for setting up the suspend image data structures for given
820 * zone (usually the returned value is greater than the exact number)
821 */
822
823unsigned int snapshot_additional_pages(struct zone *zone)
824{
825 unsigned int res;
826
827 res = DIV_ROUND_UP(zone->spanned_pages, BM_BITS_PER_BLOCK);
828 res += DIV_ROUND_UP(res * sizeof(struct bm_block), PAGE_SIZE);
Rafael J. Wysocki83573762006-12-06 20:34:18 -0800829 return 2 * res;
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700830}
831
Rafael J. Wysocki83573762006-12-06 20:34:18 -0800832#ifdef CONFIG_HIGHMEM
833/**
834 * count_free_highmem_pages - compute the total number of free highmem
835 * pages, system-wide.
836 */
837
838static unsigned int count_free_highmem_pages(void)
839{
840 struct zone *zone;
841 unsigned int cnt = 0;
842
843 for_each_zone(zone)
844 if (populated_zone(zone) && is_highmem(zone))
Christoph Lameterd23ad422007-02-10 01:43:02 -0800845 cnt += zone_page_state(zone, NR_FREE_PAGES);
Rafael J. Wysocki83573762006-12-06 20:34:18 -0800846
847 return cnt;
848}
849
850/**
851 * saveable_highmem_page - Determine whether a highmem page should be
852 * included in the suspend image.
853 *
854 * We should save the page if it isn't Nosave or NosaveFree, or Reserved,
855 * and it isn't a part of a free chunk of pages.
856 */
857
858static struct page *saveable_highmem_page(unsigned long pfn)
859{
860 struct page *page;
861
862 if (!pfn_valid(pfn))
863 return NULL;
864
865 page = pfn_to_page(pfn);
866
867 BUG_ON(!PageHighMem(page));
868
Rafael J. Wysocki7be98232007-05-06 14:50:42 -0700869 if (swsusp_page_is_forbidden(page) || swsusp_page_is_free(page) ||
870 PageReserved(page))
Rafael J. Wysocki83573762006-12-06 20:34:18 -0800871 return NULL;
872
873 return page;
874}
875
876/**
877 * count_highmem_pages - compute the total number of saveable highmem
878 * pages.
879 */
880
881unsigned int count_highmem_pages(void)
882{
883 struct zone *zone;
884 unsigned int n = 0;
885
886 for_each_zone(zone) {
887 unsigned long pfn, max_zone_pfn;
888
889 if (!is_highmem(zone))
890 continue;
891
892 mark_free_pages(zone);
893 max_zone_pfn = zone->zone_start_pfn + zone->spanned_pages;
894 for (pfn = zone->zone_start_pfn; pfn < max_zone_pfn; pfn++)
895 if (saveable_highmem_page(pfn))
896 n++;
897 }
898 return n;
899}
900#else
901static inline void *saveable_highmem_page(unsigned long pfn) { return NULL; }
Rafael J. Wysocki83573762006-12-06 20:34:18 -0800902#endif /* CONFIG_HIGHMEM */
903
Rafael J. Wysockif6143aa2006-09-25 23:32:50 -0700904/**
Rafael J. Wysocki8a235ef2008-02-20 01:47:44 +0100905 * saveable_page - Determine whether a non-highmem page should be included
906 * in the suspend image.
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800907 *
Rafael J. Wysocki83573762006-12-06 20:34:18 -0800908 * We should save the page if it isn't Nosave, and is not in the range
909 * of pages statically defined as 'unsaveable', and it isn't a part of
910 * a free chunk of pages.
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800911 */
912
Rafael J. Wysockiae83c5ee2006-09-25 23:32:45 -0700913static struct page *saveable_page(unsigned long pfn)
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800914{
Pavel Machekde491862005-10-30 14:59:59 -0800915 struct page *page;
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800916
917 if (!pfn_valid(pfn))
Rafael J. Wysockiae83c5ee2006-09-25 23:32:45 -0700918 return NULL;
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800919
920 page = pfn_to_page(pfn);
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800921
Rafael J. Wysocki83573762006-12-06 20:34:18 -0800922 BUG_ON(PageHighMem(page));
923
Rafael J. Wysocki7be98232007-05-06 14:50:42 -0700924 if (swsusp_page_is_forbidden(page) || swsusp_page_is_free(page))
Rafael J. Wysockiae83c5ee2006-09-25 23:32:45 -0700925 return NULL;
Rafael J. Wysocki83573762006-12-06 20:34:18 -0800926
Rafael J. Wysocki8a235ef2008-02-20 01:47:44 +0100927 if (PageReserved(page)
928 && (!kernel_page_present(page) || pfn_is_nosave(pfn)))
Rafael J. Wysockiae83c5ee2006-09-25 23:32:45 -0700929 return NULL;
Rafael J. Wysockiae83c5ee2006-09-25 23:32:45 -0700930
931 return page;
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800932}
933
Rafael J. Wysocki83573762006-12-06 20:34:18 -0800934/**
935 * count_data_pages - compute the total number of saveable non-highmem
936 * pages.
937 */
938
Rafael J. Wysocki72a97e02006-01-06 00:13:46 -0800939unsigned int count_data_pages(void)
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800940{
941 struct zone *zone;
Rafael J. Wysockiae83c5ee2006-09-25 23:32:45 -0700942 unsigned long pfn, max_zone_pfn;
Pavel Machekdc19d502005-11-07 00:58:40 -0800943 unsigned int n = 0;
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800944
Rafael J. Wysocki83573762006-12-06 20:34:18 -0800945 for_each_zone(zone) {
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800946 if (is_highmem(zone))
947 continue;
Rafael J. Wysocki83573762006-12-06 20:34:18 -0800948
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800949 mark_free_pages(zone);
Rafael J. Wysockiae83c5ee2006-09-25 23:32:45 -0700950 max_zone_pfn = zone->zone_start_pfn + zone->spanned_pages;
951 for (pfn = zone->zone_start_pfn; pfn < max_zone_pfn; pfn++)
Rafael J. Wysocki83573762006-12-06 20:34:18 -0800952 if(saveable_page(pfn))
953 n++;
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800954 }
Rafael J. Wysockia0f49652005-10-30 14:59:57 -0800955 return n;
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800956}
957
Rafael J. Wysocki83573762006-12-06 20:34:18 -0800958/* This is needed, because copy_page and memcpy are not usable for copying
959 * task structs.
960 */
961static inline void do_copy_page(long *dst, long *src)
Rafael J. Wysockif623f0d2006-09-25 23:32:49 -0700962{
963 int n;
964
Rafael J. Wysockif623f0d2006-09-25 23:32:49 -0700965 for (n = PAGE_SIZE / sizeof(long); n; n--)
966 *dst++ = *src++;
967}
968
Rafael J. Wysocki8a235ef2008-02-20 01:47:44 +0100969
970/**
971 * safe_copy_page - check if the page we are going to copy is marked as
972 * present in the kernel page tables (this always is the case if
973 * CONFIG_DEBUG_PAGEALLOC is not set and in that case
974 * kernel_page_present() always returns 'true').
975 */
976static void safe_copy_page(void *dst, struct page *s_page)
977{
978 if (kernel_page_present(s_page)) {
979 do_copy_page(dst, page_address(s_page));
980 } else {
981 kernel_map_pages(s_page, 1, 1);
982 do_copy_page(dst, page_address(s_page));
983 kernel_map_pages(s_page, 1, 0);
984 }
985}
986
987
Rafael J. Wysocki83573762006-12-06 20:34:18 -0800988#ifdef CONFIG_HIGHMEM
989static inline struct page *
990page_is_saveable(struct zone *zone, unsigned long pfn)
991{
992 return is_highmem(zone) ?
993 saveable_highmem_page(pfn) : saveable_page(pfn);
994}
995
Rafael J. Wysocki8a235ef2008-02-20 01:47:44 +0100996static void copy_data_page(unsigned long dst_pfn, unsigned long src_pfn)
Rafael J. Wysocki83573762006-12-06 20:34:18 -0800997{
998 struct page *s_page, *d_page;
999 void *src, *dst;
1000
1001 s_page = pfn_to_page(src_pfn);
1002 d_page = pfn_to_page(dst_pfn);
1003 if (PageHighMem(s_page)) {
1004 src = kmap_atomic(s_page, KM_USER0);
1005 dst = kmap_atomic(d_page, KM_USER1);
1006 do_copy_page(dst, src);
1007 kunmap_atomic(src, KM_USER0);
1008 kunmap_atomic(dst, KM_USER1);
1009 } else {
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001010 if (PageHighMem(d_page)) {
1011 /* Page pointed to by src may contain some kernel
1012 * data modified by kmap_atomic()
1013 */
Rafael J. Wysocki8a235ef2008-02-20 01:47:44 +01001014 safe_copy_page(buffer, s_page);
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001015 dst = kmap_atomic(pfn_to_page(dst_pfn), KM_USER0);
1016 memcpy(dst, buffer, PAGE_SIZE);
1017 kunmap_atomic(dst, KM_USER0);
1018 } else {
Rafael J. Wysocki8a235ef2008-02-20 01:47:44 +01001019 safe_copy_page(page_address(d_page), s_page);
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001020 }
1021 }
1022}
1023#else
1024#define page_is_saveable(zone, pfn) saveable_page(pfn)
1025
Rafael J. Wysocki8a235ef2008-02-20 01:47:44 +01001026static inline void copy_data_page(unsigned long dst_pfn, unsigned long src_pfn)
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001027{
Rafael J. Wysocki8a235ef2008-02-20 01:47:44 +01001028 safe_copy_page(page_address(pfn_to_page(dst_pfn)),
1029 pfn_to_page(src_pfn));
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001030}
1031#endif /* CONFIG_HIGHMEM */
1032
Rafael J. Wysockib788db72006-09-25 23:32:54 -07001033static void
1034copy_data_pages(struct memory_bitmap *copy_bm, struct memory_bitmap *orig_bm)
Rafael J. Wysocki25761b62005-10-30 14:59:56 -08001035{
1036 struct zone *zone;
Rafael J. Wysockib788db72006-09-25 23:32:54 -07001037 unsigned long pfn;
Rafael J. Wysocki25761b62005-10-30 14:59:56 -08001038
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001039 for_each_zone(zone) {
Rafael J. Wysockib788db72006-09-25 23:32:54 -07001040 unsigned long max_zone_pfn;
1041
Rafael J. Wysocki25761b62005-10-30 14:59:56 -08001042 mark_free_pages(zone);
Rafael J. Wysockiae83c5ee2006-09-25 23:32:45 -07001043 max_zone_pfn = zone->zone_start_pfn + zone->spanned_pages;
Rafael J. Wysockib788db72006-09-25 23:32:54 -07001044 for (pfn = zone->zone_start_pfn; pfn < max_zone_pfn; pfn++)
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001045 if (page_is_saveable(zone, pfn))
Rafael J. Wysockib788db72006-09-25 23:32:54 -07001046 memory_bm_set_bit(orig_bm, pfn);
Rafael J. Wysocki25761b62005-10-30 14:59:56 -08001047 }
Rafael J. Wysockib788db72006-09-25 23:32:54 -07001048 memory_bm_position_reset(orig_bm);
1049 memory_bm_position_reset(copy_bm);
Fengguang Wudf7c4872007-10-20 02:26:04 +02001050 for(;;) {
Rafael J. Wysockib788db72006-09-25 23:32:54 -07001051 pfn = memory_bm_next_pfn(orig_bm);
Fengguang Wudf7c4872007-10-20 02:26:04 +02001052 if (unlikely(pfn == BM_END_OF_MAP))
1053 break;
1054 copy_data_page(memory_bm_next_pfn(copy_bm), pfn);
1055 }
Rafael J. Wysocki25761b62005-10-30 14:59:56 -08001056}
1057
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001058/* Total number of image pages */
1059static unsigned int nr_copy_pages;
1060/* Number of pages needed for saving the original pfns of the image pages */
1061static unsigned int nr_meta_pages;
1062
Rafael J. Wysocki25761b62005-10-30 14:59:56 -08001063/**
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001064 * swsusp_free - free pages allocated for the suspend.
Rafael J. Wysockicd560bb2006-09-25 23:32:50 -07001065 *
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001066 * Suspend pages are alocated before the atomic copy is made, so we
1067 * need to release them after the resume.
Rafael J. Wysocki25761b62005-10-30 14:59:56 -08001068 */
1069
1070void swsusp_free(void)
1071{
1072 struct zone *zone;
Rafael J. Wysockiae83c5ee2006-09-25 23:32:45 -07001073 unsigned long pfn, max_zone_pfn;
Rafael J. Wysocki25761b62005-10-30 14:59:56 -08001074
1075 for_each_zone(zone) {
Rafael J. Wysockiae83c5ee2006-09-25 23:32:45 -07001076 max_zone_pfn = zone->zone_start_pfn + zone->spanned_pages;
1077 for (pfn = zone->zone_start_pfn; pfn < max_zone_pfn; pfn++)
1078 if (pfn_valid(pfn)) {
1079 struct page *page = pfn_to_page(pfn);
1080
Rafael J. Wysocki7be98232007-05-06 14:50:42 -07001081 if (swsusp_page_is_forbidden(page) &&
1082 swsusp_page_is_free(page)) {
1083 swsusp_unset_page_forbidden(page);
1084 swsusp_unset_page_free(page);
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001085 __free_page(page);
Rafael J. Wysocki25761b62005-10-30 14:59:56 -08001086 }
1087 }
1088 }
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001089 nr_copy_pages = 0;
1090 nr_meta_pages = 0;
Rafael J. Wysocki75534b52006-09-25 23:32:52 -07001091 restore_pblist = NULL;
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -08001092 buffer = NULL;
Rafael J. Wysocki25761b62005-10-30 14:59:56 -08001093}
1094
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001095#ifdef CONFIG_HIGHMEM
1096/**
1097 * count_pages_for_highmem - compute the number of non-highmem pages
1098 * that will be necessary for creating copies of highmem pages.
1099 */
1100
1101static unsigned int count_pages_for_highmem(unsigned int nr_highmem)
1102{
1103 unsigned int free_highmem = count_free_highmem_pages();
1104
1105 if (free_highmem >= nr_highmem)
1106 nr_highmem = 0;
1107 else
1108 nr_highmem -= free_highmem;
1109
1110 return nr_highmem;
1111}
1112#else
1113static unsigned int
1114count_pages_for_highmem(unsigned int nr_highmem) { return 0; }
1115#endif /* CONFIG_HIGHMEM */
Rafael J. Wysocki25761b62005-10-30 14:59:56 -08001116
1117/**
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001118 * enough_free_mem - Make sure we have enough free memory for the
1119 * snapshot image.
Rafael J. Wysocki25761b62005-10-30 14:59:56 -08001120 */
1121
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001122static int enough_free_mem(unsigned int nr_pages, unsigned int nr_highmem)
Rafael J. Wysocki25761b62005-10-30 14:59:56 -08001123{
Rafael J. Wysockie5e2fa72006-01-06 00:14:20 -08001124 struct zone *zone;
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001125 unsigned int free = 0, meta = 0;
Rafael J. Wysockie5e2fa72006-01-06 00:14:20 -08001126
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001127 for_each_zone(zone) {
1128 meta += snapshot_additional_pages(zone);
1129 if (!is_highmem(zone))
Christoph Lameterd23ad422007-02-10 01:43:02 -08001130 free += zone_page_state(zone, NR_FREE_PAGES);
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001131 }
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001132
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001133 nr_pages += count_pages_for_highmem(nr_highmem);
Rafael J. Wysocki23976722007-12-08 02:09:43 +01001134 pr_debug("PM: Normal pages needed: %u + %u + %u, available pages: %u\n",
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001135 nr_pages, PAGES_FOR_IO, meta, free);
1136
1137 return free > nr_pages + PAGES_FOR_IO + meta;
Rafael J. Wysocki25761b62005-10-30 14:59:56 -08001138}
1139
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001140#ifdef CONFIG_HIGHMEM
1141/**
1142 * get_highmem_buffer - if there are some highmem pages in the suspend
1143 * image, we may need the buffer to copy them and/or load their data.
1144 */
1145
1146static inline int get_highmem_buffer(int safe_needed)
1147{
1148 buffer = get_image_page(GFP_ATOMIC | __GFP_COLD, safe_needed);
1149 return buffer ? 0 : -ENOMEM;
1150}
1151
1152/**
1153 * alloc_highmem_image_pages - allocate some highmem pages for the image.
1154 * Try to allocate as many pages as needed, but if the number of free
1155 * highmem pages is lesser than that, allocate them all.
1156 */
1157
1158static inline unsigned int
1159alloc_highmem_image_pages(struct memory_bitmap *bm, unsigned int nr_highmem)
1160{
1161 unsigned int to_alloc = count_free_highmem_pages();
1162
1163 if (to_alloc > nr_highmem)
1164 to_alloc = nr_highmem;
1165
1166 nr_highmem -= to_alloc;
1167 while (to_alloc-- > 0) {
1168 struct page *page;
1169
1170 page = alloc_image_page(__GFP_HIGHMEM);
1171 memory_bm_set_bit(bm, page_to_pfn(page));
1172 }
1173 return nr_highmem;
1174}
1175#else
1176static inline int get_highmem_buffer(int safe_needed) { return 0; }
1177
1178static inline unsigned int
1179alloc_highmem_image_pages(struct memory_bitmap *bm, unsigned int n) { return 0; }
1180#endif /* CONFIG_HIGHMEM */
1181
1182/**
1183 * swsusp_alloc - allocate memory for the suspend image
1184 *
1185 * We first try to allocate as many highmem pages as there are
1186 * saveable highmem pages in the system. If that fails, we allocate
1187 * non-highmem pages for the copies of the remaining highmem ones.
1188 *
1189 * In this approach it is likely that the copies of highmem pages will
1190 * also be located in the high memory, because of the way in which
1191 * copy_data_pages() works.
1192 */
1193
Rafael J. Wysockib788db72006-09-25 23:32:54 -07001194static int
1195swsusp_alloc(struct memory_bitmap *orig_bm, struct memory_bitmap *copy_bm,
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001196 unsigned int nr_pages, unsigned int nr_highmem)
Rafael J. Wysocki054bd4c2005-11-08 21:34:39 -08001197{
Rafael J. Wysockib788db72006-09-25 23:32:54 -07001198 int error;
Rafael J. Wysocki054bd4c2005-11-08 21:34:39 -08001199
Rafael J. Wysockib788db72006-09-25 23:32:54 -07001200 error = memory_bm_create(orig_bm, GFP_ATOMIC | __GFP_COLD, PG_ANY);
1201 if (error)
1202 goto Free;
1203
1204 error = memory_bm_create(copy_bm, GFP_ATOMIC | __GFP_COLD, PG_ANY);
1205 if (error)
1206 goto Free;
1207
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001208 if (nr_highmem > 0) {
1209 error = get_highmem_buffer(PG_ANY);
1210 if (error)
1211 goto Free;
1212
1213 nr_pages += alloc_highmem_image_pages(copy_bm, nr_highmem);
1214 }
Rafael J. Wysockib788db72006-09-25 23:32:54 -07001215 while (nr_pages-- > 0) {
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001216 struct page *page = alloc_image_page(GFP_ATOMIC | __GFP_COLD);
1217
Rafael J. Wysockib788db72006-09-25 23:32:54 -07001218 if (!page)
1219 goto Free;
1220
Rafael J. Wysockib788db72006-09-25 23:32:54 -07001221 memory_bm_set_bit(copy_bm, page_to_pfn(page));
Rafael J. Wysocki054bd4c2005-11-08 21:34:39 -08001222 }
1223 return 0;
Rafael J. Wysockib788db72006-09-25 23:32:54 -07001224
Rafael J. Wysocki59a493352006-12-06 20:34:44 -08001225 Free:
Rafael J. Wysockib788db72006-09-25 23:32:54 -07001226 swsusp_free();
1227 return -ENOMEM;
Rafael J. Wysocki054bd4c2005-11-08 21:34:39 -08001228}
Rafael J. Wysocki25761b62005-10-30 14:59:56 -08001229
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001230/* Memory bitmap used for marking saveable pages (during suspend) or the
1231 * suspend image pages (during resume)
1232 */
Rafael J. Wysockib788db72006-09-25 23:32:54 -07001233static struct memory_bitmap orig_bm;
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001234/* Memory bitmap used on suspend for marking allocated pages that will contain
1235 * the copies of saveable pages. During resume it is initially used for
1236 * marking the suspend image pages, but then its set bits are duplicated in
1237 * @orig_bm and it is released. Next, on systems with high memory, it may be
1238 * used for marking "safe" highmem pages, but it has to be reinitialized for
1239 * this purpose.
Rafael J. Wysockib788db72006-09-25 23:32:54 -07001240 */
1241static struct memory_bitmap copy_bm;
Rafael J. Wysocki25761b62005-10-30 14:59:56 -08001242
Rafael J. Wysocki2e32a432005-10-30 15:00:00 -08001243asmlinkage int swsusp_save(void)
Rafael J. Wysocki25761b62005-10-30 14:59:56 -08001244{
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001245 unsigned int nr_pages, nr_highmem;
Rafael J. Wysocki25761b62005-10-30 14:59:56 -08001246
Rafael J. Wysocki23976722007-12-08 02:09:43 +01001247 printk(KERN_INFO "PM: Creating hibernation image: \n");
Rafael J. Wysocki25761b62005-10-30 14:59:56 -08001248
Christoph Lameter9f8f2172008-02-04 22:29:11 -08001249 drain_local_pages(NULL);
Rafael J. Wysockia0f49652005-10-30 14:59:57 -08001250 nr_pages = count_data_pages();
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001251 nr_highmem = count_highmem_pages();
Rafael J. Wysocki23976722007-12-08 02:09:43 +01001252 printk(KERN_INFO "PM: Need to copy %u pages\n", nr_pages + nr_highmem);
Rafael J. Wysocki25761b62005-10-30 14:59:56 -08001253
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001254 if (!enough_free_mem(nr_pages, nr_highmem)) {
Rafael J. Wysocki23976722007-12-08 02:09:43 +01001255 printk(KERN_ERR "PM: Not enough free memory\n");
Rafael J. Wysocki25761b62005-10-30 14:59:56 -08001256 return -ENOMEM;
1257 }
1258
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001259 if (swsusp_alloc(&orig_bm, &copy_bm, nr_pages, nr_highmem)) {
Rafael J. Wysocki23976722007-12-08 02:09:43 +01001260 printk(KERN_ERR "PM: Memory allocation failed\n");
Rafael J. Wysockia0f49652005-10-30 14:59:57 -08001261 return -ENOMEM;
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001262 }
Rafael J. Wysocki25761b62005-10-30 14:59:56 -08001263
1264 /* During allocating of suspend pagedir, new cold pages may appear.
1265 * Kill them.
1266 */
Christoph Lameter9f8f2172008-02-04 22:29:11 -08001267 drain_local_pages(NULL);
Rafael J. Wysockib788db72006-09-25 23:32:54 -07001268 copy_data_pages(&copy_bm, &orig_bm);
Rafael J. Wysocki25761b62005-10-30 14:59:56 -08001269
1270 /*
1271 * End of critical section. From now on, we can write to memory,
1272 * but we should not touch disk. This specially means we must _not_
1273 * touch swap space! Except we must write out our image of course.
1274 */
1275
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001276 nr_pages += nr_highmem;
Rafael J. Wysockia0f49652005-10-30 14:59:57 -08001277 nr_copy_pages = nr_pages;
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001278 nr_meta_pages = DIV_ROUND_UP(nr_pages * sizeof(long), PAGE_SIZE);
Rafael J. Wysockia0f49652005-10-30 14:59:57 -08001279
Rafael J. Wysocki23976722007-12-08 02:09:43 +01001280 printk(KERN_INFO "PM: Hibernation image created (%d pages copied)\n",
1281 nr_pages);
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001282
Rafael J. Wysocki25761b62005-10-30 14:59:56 -08001283 return 0;
1284}
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001285
Rafael J. Wysockid307c4a2007-10-18 03:04:52 -07001286#ifndef CONFIG_ARCH_HIBERNATION_HEADER
1287static int init_header_complete(struct swsusp_info *info)
1288{
1289 memcpy(&info->uts, init_utsname(), sizeof(struct new_utsname));
1290 info->version_code = LINUX_VERSION_CODE;
1291 return 0;
1292}
1293
1294static char *check_image_kernel(struct swsusp_info *info)
1295{
1296 if (info->version_code != LINUX_VERSION_CODE)
1297 return "kernel version";
1298 if (strcmp(info->uts.sysname,init_utsname()->sysname))
1299 return "system type";
1300 if (strcmp(info->uts.release,init_utsname()->release))
1301 return "kernel release";
1302 if (strcmp(info->uts.version,init_utsname()->version))
1303 return "version";
1304 if (strcmp(info->uts.machine,init_utsname()->machine))
1305 return "machine";
1306 return NULL;
1307}
1308#endif /* CONFIG_ARCH_HIBERNATION_HEADER */
1309
Rafael J. Wysockiaf508b32007-10-26 00:59:31 +02001310unsigned long snapshot_get_image_size(void)
1311{
1312 return nr_copy_pages + nr_meta_pages + 1;
1313}
1314
Rafael J. Wysockid307c4a2007-10-18 03:04:52 -07001315static int init_header(struct swsusp_info *info)
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001316{
1317 memset(info, 0, sizeof(struct swsusp_info));
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001318 info->num_physpages = num_physpages;
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001319 info->image_pages = nr_copy_pages;
Rafael J. Wysockiaf508b32007-10-26 00:59:31 +02001320 info->pages = snapshot_get_image_size();
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -08001321 info->size = info->pages;
1322 info->size <<= PAGE_SHIFT;
Rafael J. Wysockid307c4a2007-10-18 03:04:52 -07001323 return init_header_complete(info);
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001324}
1325
1326/**
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001327 * pack_pfns - pfns corresponding to the set bits found in the bitmap @bm
1328 * are stored in the array @buf[] (1 page at a time)
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001329 */
1330
Rafael J. Wysockib788db72006-09-25 23:32:54 -07001331static inline void
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001332pack_pfns(unsigned long *buf, struct memory_bitmap *bm)
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001333{
1334 int j;
1335
Rafael J. Wysockib788db72006-09-25 23:32:54 -07001336 for (j = 0; j < PAGE_SIZE / sizeof(long); j++) {
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001337 buf[j] = memory_bm_next_pfn(bm);
1338 if (unlikely(buf[j] == BM_END_OF_MAP))
Rafael J. Wysockib788db72006-09-25 23:32:54 -07001339 break;
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001340 }
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001341}
1342
1343/**
1344 * snapshot_read_next - used for reading the system memory snapshot.
1345 *
1346 * On the first call to it @handle should point to a zeroed
1347 * snapshot_handle structure. The structure gets updated and a pointer
1348 * to it should be passed to this function every next time.
1349 *
1350 * The @count parameter should contain the number of bytes the caller
1351 * wants to read from the snapshot. It must not be zero.
1352 *
1353 * On success the function returns a positive number. Then, the caller
1354 * is allowed to read up to the returned number of bytes from the memory
1355 * location computed by the data_of() macro. The number returned
1356 * may be smaller than @count, but this only happens if the read would
1357 * cross a page boundary otherwise.
1358 *
1359 * The function returns 0 to indicate the end of data stream condition,
1360 * and a negative number is returned on error. In such cases the
1361 * structure pointed to by @handle is not updated and should not be used
1362 * any more.
1363 */
1364
1365int snapshot_read_next(struct snapshot_handle *handle, size_t count)
1366{
Rafael J. Wysockifb13a282006-09-25 23:32:46 -07001367 if (handle->cur > nr_meta_pages + nr_copy_pages)
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001368 return 0;
Rafael J. Wysockib788db72006-09-25 23:32:54 -07001369
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001370 if (!buffer) {
1371 /* This makes the buffer be freed by swsusp_free() */
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001372 buffer = get_image_page(GFP_ATOMIC, PG_ANY);
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001373 if (!buffer)
1374 return -ENOMEM;
1375 }
1376 if (!handle->offset) {
Rafael J. Wysockid307c4a2007-10-18 03:04:52 -07001377 int error;
1378
1379 error = init_header((struct swsusp_info *)buffer);
1380 if (error)
1381 return error;
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001382 handle->buffer = buffer;
Rafael J. Wysockib788db72006-09-25 23:32:54 -07001383 memory_bm_position_reset(&orig_bm);
1384 memory_bm_position_reset(&copy_bm);
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001385 }
Rafael J. Wysockifb13a282006-09-25 23:32:46 -07001386 if (handle->prev < handle->cur) {
1387 if (handle->cur <= nr_meta_pages) {
Rafael J. Wysockib788db72006-09-25 23:32:54 -07001388 memset(buffer, 0, PAGE_SIZE);
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001389 pack_pfns(buffer, &orig_bm);
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001390 } else {
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001391 struct page *page;
Rafael J. Wysockib788db72006-09-25 23:32:54 -07001392
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001393 page = pfn_to_page(memory_bm_next_pfn(&copy_bm));
1394 if (PageHighMem(page)) {
1395 /* Highmem pages are copied to the buffer,
1396 * because we can't return with a kmapped
1397 * highmem page (we may not be called again).
1398 */
1399 void *kaddr;
1400
1401 kaddr = kmap_atomic(page, KM_USER0);
1402 memcpy(buffer, kaddr, PAGE_SIZE);
1403 kunmap_atomic(kaddr, KM_USER0);
1404 handle->buffer = buffer;
1405 } else {
1406 handle->buffer = page_address(page);
1407 }
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001408 }
Rafael J. Wysockifb13a282006-09-25 23:32:46 -07001409 handle->prev = handle->cur;
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001410 }
Rafael J. Wysockifb13a282006-09-25 23:32:46 -07001411 handle->buf_offset = handle->cur_offset;
1412 if (handle->cur_offset + count >= PAGE_SIZE) {
1413 count = PAGE_SIZE - handle->cur_offset;
1414 handle->cur_offset = 0;
1415 handle->cur++;
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001416 } else {
Rafael J. Wysockifb13a282006-09-25 23:32:46 -07001417 handle->cur_offset += count;
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001418 }
1419 handle->offset += count;
1420 return count;
1421}
1422
1423/**
1424 * mark_unsafe_pages - mark the pages that cannot be used for storing
1425 * the image during resume, because they conflict with the pages that
1426 * had been used before suspend
1427 */
1428
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001429static int mark_unsafe_pages(struct memory_bitmap *bm)
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001430{
1431 struct zone *zone;
Rafael J. Wysockiae83c5ee2006-09-25 23:32:45 -07001432 unsigned long pfn, max_zone_pfn;
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001433
1434 /* Clear page flags */
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001435 for_each_zone(zone) {
Rafael J. Wysockiae83c5ee2006-09-25 23:32:45 -07001436 max_zone_pfn = zone->zone_start_pfn + zone->spanned_pages;
1437 for (pfn = zone->zone_start_pfn; pfn < max_zone_pfn; pfn++)
1438 if (pfn_valid(pfn))
Rafael J. Wysocki7be98232007-05-06 14:50:42 -07001439 swsusp_unset_page_free(pfn_to_page(pfn));
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001440 }
1441
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001442 /* Mark pages that correspond to the "original" pfns as "unsafe" */
1443 memory_bm_position_reset(bm);
1444 do {
1445 pfn = memory_bm_next_pfn(bm);
1446 if (likely(pfn != BM_END_OF_MAP)) {
1447 if (likely(pfn_valid(pfn)))
Rafael J. Wysocki7be98232007-05-06 14:50:42 -07001448 swsusp_set_page_free(pfn_to_page(pfn));
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001449 else
1450 return -EFAULT;
1451 }
1452 } while (pfn != BM_END_OF_MAP);
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001453
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001454 allocated_unsafe_pages = 0;
Rafael J. Wysocki968808b82006-06-23 02:04:48 -07001455
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001456 return 0;
1457}
1458
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001459static void
1460duplicate_memory_bitmap(struct memory_bitmap *dst, struct memory_bitmap *src)
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001461{
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001462 unsigned long pfn;
1463
1464 memory_bm_position_reset(src);
1465 pfn = memory_bm_next_pfn(src);
1466 while (pfn != BM_END_OF_MAP) {
1467 memory_bm_set_bit(dst, pfn);
1468 pfn = memory_bm_next_pfn(src);
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001469 }
1470}
1471
Rafael J. Wysockid307c4a2007-10-18 03:04:52 -07001472static int check_header(struct swsusp_info *info)
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001473{
Rafael J. Wysockid307c4a2007-10-18 03:04:52 -07001474 char *reason;
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001475
Rafael J. Wysockid307c4a2007-10-18 03:04:52 -07001476 reason = check_image_kernel(info);
1477 if (!reason && info->num_physpages != num_physpages)
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001478 reason = "memory size";
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001479 if (reason) {
Rafael J. Wysocki23976722007-12-08 02:09:43 +01001480 printk(KERN_ERR "PM: Image mismatch: %s\n", reason);
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001481 return -EPERM;
1482 }
1483 return 0;
1484}
1485
1486/**
1487 * load header - check the image header and copy data from it
1488 */
1489
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001490static int
1491load_header(struct swsusp_info *info)
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001492{
1493 int error;
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001494
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001495 restore_pblist = NULL;
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001496 error = check_header(info);
1497 if (!error) {
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001498 nr_copy_pages = info->image_pages;
1499 nr_meta_pages = info->pages - info->image_pages - 1;
1500 }
1501 return error;
1502}
1503
1504/**
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001505 * unpack_orig_pfns - for each element of @buf[] (1 page at a time) set
1506 * the corresponding bit in the memory bitmap @bm
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001507 */
1508
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001509static inline void
1510unpack_orig_pfns(unsigned long *buf, struct memory_bitmap *bm)
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001511{
1512 int j;
1513
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001514 for (j = 0; j < PAGE_SIZE / sizeof(long); j++) {
1515 if (unlikely(buf[j] == BM_END_OF_MAP))
1516 break;
1517
1518 memory_bm_set_bit(bm, buf[j]);
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001519 }
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001520}
1521
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001522/* List of "safe" pages that may be used to store data loaded from the suspend
1523 * image
1524 */
1525static struct linked_page *safe_pages_list;
1526
1527#ifdef CONFIG_HIGHMEM
1528/* struct highmem_pbe is used for creating the list of highmem pages that
1529 * should be restored atomically during the resume from disk, because the page
1530 * frames they have occupied before the suspend are in use.
1531 */
1532struct highmem_pbe {
1533 struct page *copy_page; /* data is here now */
1534 struct page *orig_page; /* data was here before the suspend */
1535 struct highmem_pbe *next;
1536};
1537
1538/* List of highmem PBEs needed for restoring the highmem pages that were
1539 * allocated before the suspend and included in the suspend image, but have
1540 * also been allocated by the "resume" kernel, so their contents cannot be
1541 * written directly to their "original" page frames.
1542 */
1543static struct highmem_pbe *highmem_pblist;
1544
1545/**
1546 * count_highmem_image_pages - compute the number of highmem pages in the
1547 * suspend image. The bits in the memory bitmap @bm that correspond to the
1548 * image pages are assumed to be set.
1549 */
1550
1551static unsigned int count_highmem_image_pages(struct memory_bitmap *bm)
1552{
1553 unsigned long pfn;
1554 unsigned int cnt = 0;
1555
1556 memory_bm_position_reset(bm);
1557 pfn = memory_bm_next_pfn(bm);
1558 while (pfn != BM_END_OF_MAP) {
1559 if (PageHighMem(pfn_to_page(pfn)))
1560 cnt++;
1561
1562 pfn = memory_bm_next_pfn(bm);
1563 }
1564 return cnt;
1565}
1566
1567/**
1568 * prepare_highmem_image - try to allocate as many highmem pages as
1569 * there are highmem image pages (@nr_highmem_p points to the variable
1570 * containing the number of highmem image pages). The pages that are
1571 * "safe" (ie. will not be overwritten when the suspend image is
1572 * restored) have the corresponding bits set in @bm (it must be
1573 * unitialized).
1574 *
1575 * NOTE: This function should not be called if there are no highmem
1576 * image pages.
1577 */
1578
1579static unsigned int safe_highmem_pages;
1580
1581static struct memory_bitmap *safe_highmem_bm;
1582
1583static int
1584prepare_highmem_image(struct memory_bitmap *bm, unsigned int *nr_highmem_p)
1585{
1586 unsigned int to_alloc;
1587
1588 if (memory_bm_create(bm, GFP_ATOMIC, PG_SAFE))
1589 return -ENOMEM;
1590
1591 if (get_highmem_buffer(PG_SAFE))
1592 return -ENOMEM;
1593
1594 to_alloc = count_free_highmem_pages();
1595 if (to_alloc > *nr_highmem_p)
1596 to_alloc = *nr_highmem_p;
1597 else
1598 *nr_highmem_p = to_alloc;
1599
1600 safe_highmem_pages = 0;
1601 while (to_alloc-- > 0) {
1602 struct page *page;
1603
1604 page = alloc_page(__GFP_HIGHMEM);
Rafael J. Wysocki7be98232007-05-06 14:50:42 -07001605 if (!swsusp_page_is_free(page)) {
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001606 /* The page is "safe", set its bit the bitmap */
1607 memory_bm_set_bit(bm, page_to_pfn(page));
1608 safe_highmem_pages++;
1609 }
1610 /* Mark the page as allocated */
Rafael J. Wysocki7be98232007-05-06 14:50:42 -07001611 swsusp_set_page_forbidden(page);
1612 swsusp_set_page_free(page);
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001613 }
1614 memory_bm_position_reset(bm);
1615 safe_highmem_bm = bm;
1616 return 0;
1617}
1618
1619/**
1620 * get_highmem_page_buffer - for given highmem image page find the buffer
1621 * that suspend_write_next() should set for its caller to write to.
1622 *
1623 * If the page is to be saved to its "original" page frame or a copy of
1624 * the page is to be made in the highmem, @buffer is returned. Otherwise,
1625 * the copy of the page is to be made in normal memory, so the address of
1626 * the copy is returned.
1627 *
1628 * If @buffer is returned, the caller of suspend_write_next() will write
1629 * the page's contents to @buffer, so they will have to be copied to the
1630 * right location on the next call to suspend_write_next() and it is done
1631 * with the help of copy_last_highmem_page(). For this purpose, if
1632 * @buffer is returned, @last_highmem page is set to the page to which
1633 * the data will have to be copied from @buffer.
1634 */
1635
1636static struct page *last_highmem_page;
1637
1638static void *
1639get_highmem_page_buffer(struct page *page, struct chain_allocator *ca)
1640{
1641 struct highmem_pbe *pbe;
1642 void *kaddr;
1643
Rafael J. Wysocki7be98232007-05-06 14:50:42 -07001644 if (swsusp_page_is_forbidden(page) && swsusp_page_is_free(page)) {
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001645 /* We have allocated the "original" page frame and we can
1646 * use it directly to store the loaded page.
1647 */
1648 last_highmem_page = page;
1649 return buffer;
1650 }
1651 /* The "original" page frame has not been allocated and we have to
1652 * use a "safe" page frame to store the loaded page.
1653 */
1654 pbe = chain_alloc(ca, sizeof(struct highmem_pbe));
1655 if (!pbe) {
1656 swsusp_free();
1657 return NULL;
1658 }
1659 pbe->orig_page = page;
1660 if (safe_highmem_pages > 0) {
1661 struct page *tmp;
1662
1663 /* Copy of the page will be stored in high memory */
1664 kaddr = buffer;
1665 tmp = pfn_to_page(memory_bm_next_pfn(safe_highmem_bm));
1666 safe_highmem_pages--;
1667 last_highmem_page = tmp;
1668 pbe->copy_page = tmp;
1669 } else {
1670 /* Copy of the page will be stored in normal memory */
1671 kaddr = safe_pages_list;
1672 safe_pages_list = safe_pages_list->next;
1673 pbe->copy_page = virt_to_page(kaddr);
1674 }
1675 pbe->next = highmem_pblist;
1676 highmem_pblist = pbe;
1677 return kaddr;
1678}
1679
1680/**
1681 * copy_last_highmem_page - copy the contents of a highmem image from
1682 * @buffer, where the caller of snapshot_write_next() has place them,
1683 * to the right location represented by @last_highmem_page .
1684 */
1685
1686static void copy_last_highmem_page(void)
1687{
1688 if (last_highmem_page) {
1689 void *dst;
1690
1691 dst = kmap_atomic(last_highmem_page, KM_USER0);
1692 memcpy(dst, buffer, PAGE_SIZE);
1693 kunmap_atomic(dst, KM_USER0);
1694 last_highmem_page = NULL;
1695 }
1696}
1697
1698static inline int last_highmem_page_copied(void)
1699{
1700 return !last_highmem_page;
1701}
1702
1703static inline void free_highmem_data(void)
1704{
1705 if (safe_highmem_bm)
1706 memory_bm_free(safe_highmem_bm, PG_UNSAFE_CLEAR);
1707
1708 if (buffer)
1709 free_image_page(buffer, PG_UNSAFE_CLEAR);
1710}
1711#else
1712static inline int get_safe_write_buffer(void) { return 0; }
1713
1714static unsigned int
1715count_highmem_image_pages(struct memory_bitmap *bm) { return 0; }
1716
1717static inline int
1718prepare_highmem_image(struct memory_bitmap *bm, unsigned int *nr_highmem_p)
1719{
1720 return 0;
1721}
1722
1723static inline void *
1724get_highmem_page_buffer(struct page *page, struct chain_allocator *ca)
1725{
1726 return NULL;
1727}
1728
1729static inline void copy_last_highmem_page(void) {}
1730static inline int last_highmem_page_copied(void) { return 1; }
1731static inline void free_highmem_data(void) {}
1732#endif /* CONFIG_HIGHMEM */
1733
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001734/**
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001735 * prepare_image - use the memory bitmap @bm to mark the pages that will
1736 * be overwritten in the process of restoring the system memory state
1737 * from the suspend image ("unsafe" pages) and allocate memory for the
1738 * image.
Rafael J. Wysocki968808b82006-06-23 02:04:48 -07001739 *
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001740 * The idea is to allocate a new memory bitmap first and then allocate
1741 * as many pages as needed for the image data, but not to assign these
1742 * pages to specific tasks initially. Instead, we just mark them as
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001743 * allocated and create a lists of "safe" pages that will be used
1744 * later. On systems with high memory a list of "safe" highmem pages is
1745 * also created.
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001746 */
1747
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001748#define PBES_PER_LINKED_PAGE (LINKED_PAGE_DATA_SIZE / sizeof(struct pbe))
Rafael J. Wysocki968808b82006-06-23 02:04:48 -07001749
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001750static int
1751prepare_image(struct memory_bitmap *new_bm, struct memory_bitmap *bm)
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001752{
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001753 unsigned int nr_pages, nr_highmem;
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001754 struct linked_page *sp_list, *lp;
1755 int error;
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001756
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001757 /* If there is no highmem, the buffer will not be necessary */
1758 free_image_page(buffer, PG_UNSAFE_CLEAR);
1759 buffer = NULL;
1760
1761 nr_highmem = count_highmem_image_pages(bm);
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001762 error = mark_unsafe_pages(bm);
1763 if (error)
1764 goto Free;
1765
1766 error = memory_bm_create(new_bm, GFP_ATOMIC, PG_SAFE);
1767 if (error)
1768 goto Free;
1769
1770 duplicate_memory_bitmap(new_bm, bm);
1771 memory_bm_free(bm, PG_UNSAFE_KEEP);
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001772 if (nr_highmem > 0) {
1773 error = prepare_highmem_image(bm, &nr_highmem);
1774 if (error)
1775 goto Free;
1776 }
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001777 /* Reserve some safe pages for potential later use.
1778 *
1779 * NOTE: This way we make sure there will be enough safe pages for the
1780 * chain_alloc() in get_buffer(). It is a bit wasteful, but
1781 * nr_copy_pages cannot be greater than 50% of the memory anyway.
1782 */
1783 sp_list = NULL;
1784 /* nr_copy_pages cannot be lesser than allocated_unsafe_pages */
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001785 nr_pages = nr_copy_pages - nr_highmem - allocated_unsafe_pages;
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001786 nr_pages = DIV_ROUND_UP(nr_pages, PBES_PER_LINKED_PAGE);
1787 while (nr_pages > 0) {
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001788 lp = get_image_page(GFP_ATOMIC, PG_SAFE);
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001789 if (!lp) {
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001790 error = -ENOMEM;
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001791 goto Free;
Rafael J. Wysocki968808b82006-06-23 02:04:48 -07001792 }
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001793 lp->next = sp_list;
1794 sp_list = lp;
1795 nr_pages--;
Rafael J. Wysocki968808b82006-06-23 02:04:48 -07001796 }
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001797 /* Preallocate memory for the image */
1798 safe_pages_list = NULL;
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001799 nr_pages = nr_copy_pages - nr_highmem - allocated_unsafe_pages;
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001800 while (nr_pages > 0) {
1801 lp = (struct linked_page *)get_zeroed_page(GFP_ATOMIC);
1802 if (!lp) {
1803 error = -ENOMEM;
1804 goto Free;
1805 }
Rafael J. Wysocki7be98232007-05-06 14:50:42 -07001806 if (!swsusp_page_is_free(virt_to_page(lp))) {
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001807 /* The page is "safe", add it to the list */
1808 lp->next = safe_pages_list;
1809 safe_pages_list = lp;
1810 }
1811 /* Mark the page as allocated */
Rafael J. Wysocki7be98232007-05-06 14:50:42 -07001812 swsusp_set_page_forbidden(virt_to_page(lp));
1813 swsusp_set_page_free(virt_to_page(lp));
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001814 nr_pages--;
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001815 }
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001816 /* Free the reserved safe pages so that chain_alloc() can use them */
1817 while (sp_list) {
1818 lp = sp_list->next;
1819 free_image_page(sp_list, PG_UNSAFE_CLEAR);
1820 sp_list = lp;
1821 }
1822 return 0;
1823
Rafael J. Wysocki59a493352006-12-06 20:34:44 -08001824 Free:
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001825 swsusp_free();
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001826 return error;
1827}
1828
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001829/**
1830 * get_buffer - compute the address that snapshot_write_next() should
1831 * set for its caller to write to.
1832 */
Rafael J. Wysocki968808b82006-06-23 02:04:48 -07001833
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001834static void *get_buffer(struct memory_bitmap *bm, struct chain_allocator *ca)
1835{
1836 struct pbe *pbe;
1837 struct page *page = pfn_to_page(memory_bm_next_pfn(bm));
1838
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001839 if (PageHighMem(page))
1840 return get_highmem_page_buffer(page, ca);
1841
Rafael J. Wysocki7be98232007-05-06 14:50:42 -07001842 if (swsusp_page_is_forbidden(page) && swsusp_page_is_free(page))
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001843 /* We have allocated the "original" page frame and we can
1844 * use it directly to store the loaded page.
Rafael J. Wysocki968808b82006-06-23 02:04:48 -07001845 */
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001846 return page_address(page);
1847
1848 /* The "original" page frame has not been allocated and we have to
1849 * use a "safe" page frame to store the loaded page.
Rafael J. Wysocki968808b82006-06-23 02:04:48 -07001850 */
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001851 pbe = chain_alloc(ca, sizeof(struct pbe));
1852 if (!pbe) {
1853 swsusp_free();
1854 return NULL;
1855 }
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001856 pbe->orig_address = page_address(page);
1857 pbe->address = safe_pages_list;
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001858 safe_pages_list = safe_pages_list->next;
1859 pbe->next = restore_pblist;
1860 restore_pblist = pbe;
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001861 return pbe->address;
Rafael J. Wysocki968808b82006-06-23 02:04:48 -07001862}
1863
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001864/**
1865 * snapshot_write_next - used for writing the system memory snapshot.
1866 *
1867 * On the first call to it @handle should point to a zeroed
1868 * snapshot_handle structure. The structure gets updated and a pointer
1869 * to it should be passed to this function every next time.
1870 *
1871 * The @count parameter should contain the number of bytes the caller
1872 * wants to write to the image. It must not be zero.
1873 *
1874 * On success the function returns a positive number. Then, the caller
1875 * is allowed to write up to the returned number of bytes to the memory
1876 * location computed by the data_of() macro. The number returned
1877 * may be smaller than @count, but this only happens if the write would
1878 * cross a page boundary otherwise.
1879 *
1880 * The function returns 0 to indicate the "end of file" condition,
1881 * and a negative number is returned on error. In such cases the
1882 * structure pointed to by @handle is not updated and should not be used
1883 * any more.
1884 */
1885
1886int snapshot_write_next(struct snapshot_handle *handle, size_t count)
1887{
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001888 static struct chain_allocator ca;
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001889 int error = 0;
1890
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001891 /* Check if we have already loaded the entire image */
Rafael J. Wysockifb13a282006-09-25 23:32:46 -07001892 if (handle->prev && handle->cur > nr_meta_pages + nr_copy_pages)
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001893 return 0;
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001894
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001895 if (handle->offset == 0) {
1896 if (!buffer)
1897 /* This makes the buffer be freed by swsusp_free() */
1898 buffer = get_image_page(GFP_ATOMIC, PG_ANY);
1899
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001900 if (!buffer)
1901 return -ENOMEM;
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001902
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001903 handle->buffer = buffer;
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001904 }
Andrew Morton546e0d22006-09-25 23:32:44 -07001905 handle->sync_read = 1;
Rafael J. Wysockifb13a282006-09-25 23:32:46 -07001906 if (handle->prev < handle->cur) {
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001907 if (handle->prev == 0) {
1908 error = load_header(buffer);
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001909 if (error)
1910 return error;
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001911
1912 error = memory_bm_create(&copy_bm, GFP_ATOMIC, PG_ANY);
1913 if (error)
1914 return error;
1915
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001916 } else if (handle->prev <= nr_meta_pages) {
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001917 unpack_orig_pfns(buffer, &copy_bm);
1918 if (handle->prev == nr_meta_pages) {
1919 error = prepare_image(&orig_bm, &copy_bm);
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001920 if (error)
1921 return error;
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001922
1923 chain_init(&ca, GFP_ATOMIC, PG_SAFE);
1924 memory_bm_position_reset(&orig_bm);
1925 restore_pblist = NULL;
1926 handle->buffer = get_buffer(&orig_bm, &ca);
Andrew Morton546e0d22006-09-25 23:32:44 -07001927 handle->sync_read = 0;
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001928 if (!handle->buffer)
1929 return -ENOMEM;
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001930 }
1931 } else {
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001932 copy_last_highmem_page();
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001933 handle->buffer = get_buffer(&orig_bm, &ca);
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001934 if (handle->buffer != buffer)
1935 handle->sync_read = 0;
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001936 }
Rafael J. Wysockifb13a282006-09-25 23:32:46 -07001937 handle->prev = handle->cur;
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001938 }
Rafael J. Wysockifb13a282006-09-25 23:32:46 -07001939 handle->buf_offset = handle->cur_offset;
1940 if (handle->cur_offset + count >= PAGE_SIZE) {
1941 count = PAGE_SIZE - handle->cur_offset;
1942 handle->cur_offset = 0;
1943 handle->cur++;
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001944 } else {
Rafael J. Wysockifb13a282006-09-25 23:32:46 -07001945 handle->cur_offset += count;
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001946 }
1947 handle->offset += count;
1948 return count;
1949}
1950
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001951/**
1952 * snapshot_write_finalize - must be called after the last call to
1953 * snapshot_write_next() in case the last page in the image happens
1954 * to be a highmem page and its contents should be stored in the
1955 * highmem. Additionally, it releases the memory that will not be
1956 * used any more.
1957 */
1958
1959void snapshot_write_finalize(struct snapshot_handle *handle)
1960{
1961 copy_last_highmem_page();
1962 /* Free only if we have loaded the image entirely */
1963 if (handle->prev && handle->cur > nr_meta_pages + nr_copy_pages) {
1964 memory_bm_free(&orig_bm, PG_UNSAFE_CLEAR);
1965 free_highmem_data();
1966 }
1967}
1968
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001969int snapshot_image_loaded(struct snapshot_handle *handle)
1970{
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001971 return !(!nr_copy_pages || !last_highmem_page_copied() ||
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001972 handle->cur <= nr_meta_pages + nr_copy_pages);
1973}
1974
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001975#ifdef CONFIG_HIGHMEM
1976/* Assumes that @buf is ready and points to a "safe" page */
1977static inline void
1978swap_two_pages_data(struct page *p1, struct page *p2, void *buf)
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001979{
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001980 void *kaddr1, *kaddr2;
1981
1982 kaddr1 = kmap_atomic(p1, KM_USER0);
1983 kaddr2 = kmap_atomic(p2, KM_USER1);
1984 memcpy(buf, kaddr1, PAGE_SIZE);
1985 memcpy(kaddr1, kaddr2, PAGE_SIZE);
1986 memcpy(kaddr2, buf, PAGE_SIZE);
1987 kunmap_atomic(kaddr1, KM_USER0);
1988 kunmap_atomic(kaddr2, KM_USER1);
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001989}
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001990
1991/**
1992 * restore_highmem - for each highmem page that was allocated before
1993 * the suspend and included in the suspend image, and also has been
1994 * allocated by the "resume" kernel swap its current (ie. "before
1995 * resume") contents with the previous (ie. "before suspend") one.
1996 *
1997 * If the resume eventually fails, we can call this function once
1998 * again and restore the "before resume" highmem state.
1999 */
2000
2001int restore_highmem(void)
2002{
2003 struct highmem_pbe *pbe = highmem_pblist;
2004 void *buf;
2005
2006 if (!pbe)
2007 return 0;
2008
2009 buf = get_image_page(GFP_ATOMIC, PG_SAFE);
2010 if (!buf)
2011 return -ENOMEM;
2012
2013 while (pbe) {
2014 swap_two_pages_data(pbe->copy_page, pbe->orig_page, buf);
2015 pbe = pbe->next;
2016 }
2017 free_image_page(buf, PG_UNSAFE_CLEAR);
2018 return 0;
2019}
2020#endif /* CONFIG_HIGHMEM */