blob: b7039772b05ca04a3d0fd67dfde40c31e9539f20 [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. Wysocki59a49332006-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. Wysocki74dfd662007-05-06 14:50:43 -0700450static void memory_bm_find_bit(struct memory_bitmap *bm, unsigned long pfn,
451 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
464 BUG_ON(!zone_bm);
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700465 }
466 bm->cur.zone_bm = zone_bm;
467 }
468 /* Check if the pfn corresponds to the current bitmap block */
469 bb = zone_bm->cur_block;
470 if (pfn < bb->start_pfn)
471 bb = zone_bm->bm_blocks;
472
473 while (pfn >= bb->end_pfn) {
474 bb = bb->next;
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -0700475
476 BUG_ON(!bb);
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700477 }
478 zone_bm->cur_block = bb;
479 pfn -= bb->start_pfn;
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -0700480 *bit_nr = pfn % BM_BITS_PER_CHUNK;
481 *addr = bb->data + pfn / BM_BITS_PER_CHUNK;
482}
483
484static void memory_bm_set_bit(struct memory_bitmap *bm, unsigned long pfn)
485{
486 void *addr;
487 unsigned int bit;
488
489 memory_bm_find_bit(bm, pfn, &addr, &bit);
490 set_bit(bit, addr);
491}
492
493static void memory_bm_clear_bit(struct memory_bitmap *bm, unsigned long pfn)
494{
495 void *addr;
496 unsigned int bit;
497
498 memory_bm_find_bit(bm, pfn, &addr, &bit);
499 clear_bit(bit, addr);
500}
501
502static int memory_bm_test_bit(struct memory_bitmap *bm, unsigned long pfn)
503{
504 void *addr;
505 unsigned int bit;
506
507 memory_bm_find_bit(bm, pfn, &addr, &bit);
508 return test_bit(bit, addr);
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700509}
510
511/* Two auxiliary functions for memory_bm_next_pfn */
512
513/* Find the first set bit in the given chunk, if there is one */
514
515static inline int next_bit_in_chunk(int bit, unsigned long *chunk_p)
516{
517 bit++;
518 while (bit < BM_BITS_PER_CHUNK) {
519 if (test_bit(bit, chunk_p))
520 return bit;
521
522 bit++;
523 }
524 return -1;
525}
526
527/* Find a chunk containing some bits set in given block of bits */
528
529static inline int next_chunk_in_block(int n, struct bm_block *bb)
530{
531 n++;
532 while (n < bb->size) {
533 if (bb->data[n])
534 return n;
535
536 n++;
537 }
538 return -1;
539}
540
541/**
542 * memory_bm_next_pfn - find the pfn that corresponds to the next set bit
543 * in the bitmap @bm. If the pfn cannot be found, BM_END_OF_MAP is
544 * returned.
545 *
546 * It is required to run memory_bm_position_reset() before the first call to
547 * this function.
548 */
549
550static unsigned long memory_bm_next_pfn(struct memory_bitmap *bm)
551{
552 struct zone_bitmap *zone_bm;
553 struct bm_block *bb;
554 int chunk;
555 int bit;
556
557 do {
558 bb = bm->cur.block;
559 do {
560 chunk = bm->cur.chunk;
561 bit = bm->cur.bit;
562 do {
563 bit = next_bit_in_chunk(bit, bb->data + chunk);
564 if (bit >= 0)
565 goto Return_pfn;
566
567 chunk = next_chunk_in_block(chunk, bb);
568 bit = -1;
569 } while (chunk >= 0);
570 bb = bb->next;
571 bm->cur.block = bb;
572 memory_bm_reset_chunk(bm);
573 } while (bb);
574 zone_bm = bm->cur.zone_bm->next;
575 if (zone_bm) {
576 bm->cur.zone_bm = zone_bm;
577 bm->cur.block = zone_bm->bm_blocks;
578 memory_bm_reset_chunk(bm);
579 }
580 } while (zone_bm);
581 memory_bm_position_reset(bm);
582 return BM_END_OF_MAP;
583
Rafael J. Wysocki59a49332006-12-06 20:34:44 -0800584 Return_pfn:
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700585 bm->cur.chunk = chunk;
586 bm->cur.bit = bit;
587 return bb->start_pfn + chunk * BM_BITS_PER_CHUNK + bit;
588}
589
590/**
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -0700591 * This structure represents a range of page frames the contents of which
592 * should not be saved during the suspend.
593 */
594
595struct nosave_region {
596 struct list_head list;
597 unsigned long start_pfn;
598 unsigned long end_pfn;
599};
600
601static LIST_HEAD(nosave_regions);
602
603/**
604 * register_nosave_region - register a range of page frames the contents
605 * of which should not be saved during the suspend (to be used in the early
606 * initialization code)
607 */
608
609void __init
610register_nosave_region(unsigned long start_pfn, unsigned long end_pfn)
611{
612 struct nosave_region *region;
613
614 if (start_pfn >= end_pfn)
615 return;
616
617 if (!list_empty(&nosave_regions)) {
618 /* Try to extend the previous region (they should be sorted) */
619 region = list_entry(nosave_regions.prev,
620 struct nosave_region, list);
621 if (region->end_pfn == start_pfn) {
622 region->end_pfn = end_pfn;
623 goto Report;
624 }
625 }
626 /* This allocation cannot fail */
627 region = alloc_bootmem_low(sizeof(struct nosave_region));
628 region->start_pfn = start_pfn;
629 region->end_pfn = end_pfn;
630 list_add_tail(&region->list, &nosave_regions);
631 Report:
632 printk("swsusp: Registered nosave memory region: %016lx - %016lx\n",
633 start_pfn << PAGE_SHIFT, end_pfn << PAGE_SHIFT);
634}
635
636/*
637 * Set bits in this map correspond to the page frames the contents of which
638 * should not be saved during the suspend.
639 */
640static struct memory_bitmap *forbidden_pages_map;
641
642/* Set bits in this map correspond to free page frames. */
643static struct memory_bitmap *free_pages_map;
644
645/*
646 * Each page frame allocated for creating the image is marked by setting the
647 * corresponding bits in forbidden_pages_map and free_pages_map simultaneously
648 */
649
650void swsusp_set_page_free(struct page *page)
651{
652 if (free_pages_map)
653 memory_bm_set_bit(free_pages_map, page_to_pfn(page));
654}
655
656static int swsusp_page_is_free(struct page *page)
657{
658 return free_pages_map ?
659 memory_bm_test_bit(free_pages_map, page_to_pfn(page)) : 0;
660}
661
662void swsusp_unset_page_free(struct page *page)
663{
664 if (free_pages_map)
665 memory_bm_clear_bit(free_pages_map, page_to_pfn(page));
666}
667
668static void swsusp_set_page_forbidden(struct page *page)
669{
670 if (forbidden_pages_map)
671 memory_bm_set_bit(forbidden_pages_map, page_to_pfn(page));
672}
673
674int swsusp_page_is_forbidden(struct page *page)
675{
676 return forbidden_pages_map ?
677 memory_bm_test_bit(forbidden_pages_map, page_to_pfn(page)) : 0;
678}
679
680static void swsusp_unset_page_forbidden(struct page *page)
681{
682 if (forbidden_pages_map)
683 memory_bm_clear_bit(forbidden_pages_map, page_to_pfn(page));
684}
685
686/**
687 * mark_nosave_pages - set bits corresponding to the page frames the
688 * contents of which should not be saved in a given bitmap.
689 */
690
691static void mark_nosave_pages(struct memory_bitmap *bm)
692{
693 struct nosave_region *region;
694
695 if (list_empty(&nosave_regions))
696 return;
697
698 list_for_each_entry(region, &nosave_regions, list) {
699 unsigned long pfn;
700
701 printk("swsusp: Marking nosave pages: %016lx - %016lx\n",
702 region->start_pfn << PAGE_SHIFT,
703 region->end_pfn << PAGE_SHIFT);
704
705 for (pfn = region->start_pfn; pfn < region->end_pfn; pfn++)
706 memory_bm_set_bit(bm, pfn);
707 }
708}
709
710/**
711 * create_basic_memory_bitmaps - create bitmaps needed for marking page
712 * frames that should not be saved and free page frames. The pointers
713 * forbidden_pages_map and free_pages_map are only modified if everything
714 * goes well, because we don't want the bits to be used before both bitmaps
715 * are set up.
716 */
717
718int create_basic_memory_bitmaps(void)
719{
720 struct memory_bitmap *bm1, *bm2;
721 int error = 0;
722
723 BUG_ON(forbidden_pages_map || free_pages_map);
724
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700725 bm1 = kzalloc(sizeof(struct memory_bitmap), GFP_KERNEL);
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -0700726 if (!bm1)
727 return -ENOMEM;
728
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700729 error = memory_bm_create(bm1, GFP_KERNEL, PG_ANY);
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -0700730 if (error)
731 goto Free_first_object;
732
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700733 bm2 = kzalloc(sizeof(struct memory_bitmap), GFP_KERNEL);
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -0700734 if (!bm2)
735 goto Free_first_bitmap;
736
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700737 error = memory_bm_create(bm2, GFP_KERNEL, PG_ANY);
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -0700738 if (error)
739 goto Free_second_object;
740
741 forbidden_pages_map = bm1;
742 free_pages_map = bm2;
743 mark_nosave_pages(forbidden_pages_map);
744
745 printk("swsusp: Basic memory bitmaps created\n");
746
747 return 0;
748
749 Free_second_object:
750 kfree(bm2);
751 Free_first_bitmap:
752 memory_bm_free(bm1, PG_UNSAFE_CLEAR);
753 Free_first_object:
754 kfree(bm1);
755 return -ENOMEM;
756}
757
758/**
759 * free_basic_memory_bitmaps - free memory bitmaps allocated by
760 * create_basic_memory_bitmaps(). The auxiliary pointers are necessary
761 * so that the bitmaps themselves are not referred to while they are being
762 * freed.
763 */
764
765void free_basic_memory_bitmaps(void)
766{
767 struct memory_bitmap *bm1, *bm2;
768
769 BUG_ON(!(forbidden_pages_map && free_pages_map));
770
771 bm1 = forbidden_pages_map;
772 bm2 = free_pages_map;
773 forbidden_pages_map = NULL;
774 free_pages_map = NULL;
775 memory_bm_free(bm1, PG_UNSAFE_CLEAR);
776 kfree(bm1);
777 memory_bm_free(bm2, PG_UNSAFE_CLEAR);
778 kfree(bm2);
779
780 printk("swsusp: Basic memory bitmaps freed\n");
781}
782
783/**
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700784 * snapshot_additional_pages - estimate the number of additional pages
785 * be needed for setting up the suspend image data structures for given
786 * zone (usually the returned value is greater than the exact number)
787 */
788
789unsigned int snapshot_additional_pages(struct zone *zone)
790{
791 unsigned int res;
792
793 res = DIV_ROUND_UP(zone->spanned_pages, BM_BITS_PER_BLOCK);
794 res += DIV_ROUND_UP(res * sizeof(struct bm_block), PAGE_SIZE);
Rafael J. Wysocki83573762006-12-06 20:34:18 -0800795 return 2 * res;
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700796}
797
Rafael J. Wysocki83573762006-12-06 20:34:18 -0800798#ifdef CONFIG_HIGHMEM
799/**
800 * count_free_highmem_pages - compute the total number of free highmem
801 * pages, system-wide.
802 */
803
804static unsigned int count_free_highmem_pages(void)
805{
806 struct zone *zone;
807 unsigned int cnt = 0;
808
809 for_each_zone(zone)
810 if (populated_zone(zone) && is_highmem(zone))
Christoph Lameterd23ad422007-02-10 01:43:02 -0800811 cnt += zone_page_state(zone, NR_FREE_PAGES);
Rafael J. Wysocki83573762006-12-06 20:34:18 -0800812
813 return cnt;
814}
815
816/**
817 * saveable_highmem_page - Determine whether a highmem page should be
818 * included in the suspend image.
819 *
820 * We should save the page if it isn't Nosave or NosaveFree, or Reserved,
821 * and it isn't a part of a free chunk of pages.
822 */
823
824static struct page *saveable_highmem_page(unsigned long pfn)
825{
826 struct page *page;
827
828 if (!pfn_valid(pfn))
829 return NULL;
830
831 page = pfn_to_page(pfn);
832
833 BUG_ON(!PageHighMem(page));
834
Rafael J. Wysocki7be98232007-05-06 14:50:42 -0700835 if (swsusp_page_is_forbidden(page) || swsusp_page_is_free(page) ||
836 PageReserved(page))
Rafael J. Wysocki83573762006-12-06 20:34:18 -0800837 return NULL;
838
839 return page;
840}
841
842/**
843 * count_highmem_pages - compute the total number of saveable highmem
844 * pages.
845 */
846
847unsigned int count_highmem_pages(void)
848{
849 struct zone *zone;
850 unsigned int n = 0;
851
852 for_each_zone(zone) {
853 unsigned long pfn, max_zone_pfn;
854
855 if (!is_highmem(zone))
856 continue;
857
858 mark_free_pages(zone);
859 max_zone_pfn = zone->zone_start_pfn + zone->spanned_pages;
860 for (pfn = zone->zone_start_pfn; pfn < max_zone_pfn; pfn++)
861 if (saveable_highmem_page(pfn))
862 n++;
863 }
864 return n;
865}
866#else
867static inline void *saveable_highmem_page(unsigned long pfn) { return NULL; }
868static inline unsigned int count_highmem_pages(void) { return 0; }
869#endif /* CONFIG_HIGHMEM */
870
Rafael J. Wysockif6143aa2006-09-25 23:32:50 -0700871/**
Rafael J. Wysocki83573762006-12-06 20:34:18 -0800872 * saveable - Determine whether a non-highmem page should be included in
873 * the suspend image.
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800874 *
Rafael J. Wysocki83573762006-12-06 20:34:18 -0800875 * We should save the page if it isn't Nosave, and is not in the range
876 * of pages statically defined as 'unsaveable', and it isn't a part of
877 * a free chunk of pages.
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800878 */
879
Rafael J. Wysockiae83c5e2006-09-25 23:32:45 -0700880static struct page *saveable_page(unsigned long pfn)
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800881{
Pavel Machekde491862005-10-30 14:59:59 -0800882 struct page *page;
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800883
884 if (!pfn_valid(pfn))
Rafael J. Wysockiae83c5e2006-09-25 23:32:45 -0700885 return NULL;
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800886
887 page = pfn_to_page(pfn);
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800888
Rafael J. Wysocki83573762006-12-06 20:34:18 -0800889 BUG_ON(PageHighMem(page));
890
Rafael J. Wysocki7be98232007-05-06 14:50:42 -0700891 if (swsusp_page_is_forbidden(page) || swsusp_page_is_free(page))
Rafael J. Wysockiae83c5e2006-09-25 23:32:45 -0700892 return NULL;
Rafael J. Wysocki83573762006-12-06 20:34:18 -0800893
Rafael J. Wysockiae83c5e2006-09-25 23:32:45 -0700894 if (PageReserved(page) && pfn_is_nosave(pfn))
895 return NULL;
Rafael J. Wysockiae83c5e2006-09-25 23:32:45 -0700896
897 return page;
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800898}
899
Rafael J. Wysocki83573762006-12-06 20:34:18 -0800900/**
901 * count_data_pages - compute the total number of saveable non-highmem
902 * pages.
903 */
904
Rafael J. Wysocki72a97e02006-01-06 00:13:46 -0800905unsigned int count_data_pages(void)
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800906{
907 struct zone *zone;
Rafael J. Wysockiae83c5e2006-09-25 23:32:45 -0700908 unsigned long pfn, max_zone_pfn;
Pavel Machekdc19d502005-11-07 00:58:40 -0800909 unsigned int n = 0;
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800910
Rafael J. Wysocki83573762006-12-06 20:34:18 -0800911 for_each_zone(zone) {
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800912 if (is_highmem(zone))
913 continue;
Rafael J. Wysocki83573762006-12-06 20:34:18 -0800914
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800915 mark_free_pages(zone);
Rafael J. Wysockiae83c5e2006-09-25 23:32:45 -0700916 max_zone_pfn = zone->zone_start_pfn + zone->spanned_pages;
917 for (pfn = zone->zone_start_pfn; pfn < max_zone_pfn; pfn++)
Rafael J. Wysocki83573762006-12-06 20:34:18 -0800918 if(saveable_page(pfn))
919 n++;
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800920 }
Rafael J. Wysockia0f49652005-10-30 14:59:57 -0800921 return n;
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800922}
923
Rafael J. Wysocki83573762006-12-06 20:34:18 -0800924/* This is needed, because copy_page and memcpy are not usable for copying
925 * task structs.
926 */
927static inline void do_copy_page(long *dst, long *src)
Rafael J. Wysockif623f0d2006-09-25 23:32:49 -0700928{
929 int n;
930
Rafael J. Wysockif623f0d2006-09-25 23:32:49 -0700931 for (n = PAGE_SIZE / sizeof(long); n; n--)
932 *dst++ = *src++;
933}
934
Rafael J. Wysocki83573762006-12-06 20:34:18 -0800935#ifdef CONFIG_HIGHMEM
936static inline struct page *
937page_is_saveable(struct zone *zone, unsigned long pfn)
938{
939 return is_highmem(zone) ?
940 saveable_highmem_page(pfn) : saveable_page(pfn);
941}
942
943static inline void
944copy_data_page(unsigned long dst_pfn, unsigned long src_pfn)
945{
946 struct page *s_page, *d_page;
947 void *src, *dst;
948
949 s_page = pfn_to_page(src_pfn);
950 d_page = pfn_to_page(dst_pfn);
951 if (PageHighMem(s_page)) {
952 src = kmap_atomic(s_page, KM_USER0);
953 dst = kmap_atomic(d_page, KM_USER1);
954 do_copy_page(dst, src);
955 kunmap_atomic(src, KM_USER0);
956 kunmap_atomic(dst, KM_USER1);
957 } else {
958 src = page_address(s_page);
959 if (PageHighMem(d_page)) {
960 /* Page pointed to by src may contain some kernel
961 * data modified by kmap_atomic()
962 */
963 do_copy_page(buffer, src);
964 dst = kmap_atomic(pfn_to_page(dst_pfn), KM_USER0);
965 memcpy(dst, buffer, PAGE_SIZE);
966 kunmap_atomic(dst, KM_USER0);
967 } else {
968 dst = page_address(d_page);
969 do_copy_page(dst, src);
970 }
971 }
972}
973#else
974#define page_is_saveable(zone, pfn) saveable_page(pfn)
975
976static inline void
977copy_data_page(unsigned long dst_pfn, unsigned long src_pfn)
978{
979 do_copy_page(page_address(pfn_to_page(dst_pfn)),
980 page_address(pfn_to_page(src_pfn)));
981}
982#endif /* CONFIG_HIGHMEM */
983
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700984static void
985copy_data_pages(struct memory_bitmap *copy_bm, struct memory_bitmap *orig_bm)
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800986{
987 struct zone *zone;
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700988 unsigned long pfn;
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800989
Rafael J. Wysocki83573762006-12-06 20:34:18 -0800990 for_each_zone(zone) {
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700991 unsigned long max_zone_pfn;
992
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800993 mark_free_pages(zone);
Rafael J. Wysockiae83c5e2006-09-25 23:32:45 -0700994 max_zone_pfn = zone->zone_start_pfn + zone->spanned_pages;
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700995 for (pfn = zone->zone_start_pfn; pfn < max_zone_pfn; pfn++)
Rafael J. Wysocki83573762006-12-06 20:34:18 -0800996 if (page_is_saveable(zone, pfn))
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700997 memory_bm_set_bit(orig_bm, pfn);
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800998 }
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700999 memory_bm_position_reset(orig_bm);
1000 memory_bm_position_reset(copy_bm);
1001 do {
1002 pfn = memory_bm_next_pfn(orig_bm);
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001003 if (likely(pfn != BM_END_OF_MAP))
1004 copy_data_page(memory_bm_next_pfn(copy_bm), pfn);
Rafael J. Wysockib788db72006-09-25 23:32:54 -07001005 } while (pfn != BM_END_OF_MAP);
Rafael J. Wysocki25761b62005-10-30 14:59:56 -08001006}
1007
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001008/* Total number of image pages */
1009static unsigned int nr_copy_pages;
1010/* Number of pages needed for saving the original pfns of the image pages */
1011static unsigned int nr_meta_pages;
1012
Rafael J. Wysocki25761b62005-10-30 14:59:56 -08001013/**
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001014 * swsusp_free - free pages allocated for the suspend.
Rafael J. Wysockicd560bb2006-09-25 23:32:50 -07001015 *
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001016 * Suspend pages are alocated before the atomic copy is made, so we
1017 * need to release them after the resume.
Rafael J. Wysocki25761b62005-10-30 14:59:56 -08001018 */
1019
1020void swsusp_free(void)
1021{
1022 struct zone *zone;
Rafael J. Wysockiae83c5e2006-09-25 23:32:45 -07001023 unsigned long pfn, max_zone_pfn;
Rafael J. Wysocki25761b62005-10-30 14:59:56 -08001024
1025 for_each_zone(zone) {
Rafael J. Wysockiae83c5e2006-09-25 23:32:45 -07001026 max_zone_pfn = zone->zone_start_pfn + zone->spanned_pages;
1027 for (pfn = zone->zone_start_pfn; pfn < max_zone_pfn; pfn++)
1028 if (pfn_valid(pfn)) {
1029 struct page *page = pfn_to_page(pfn);
1030
Rafael J. Wysocki7be98232007-05-06 14:50:42 -07001031 if (swsusp_page_is_forbidden(page) &&
1032 swsusp_page_is_free(page)) {
1033 swsusp_unset_page_forbidden(page);
1034 swsusp_unset_page_free(page);
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001035 __free_page(page);
Rafael J. Wysocki25761b62005-10-30 14:59:56 -08001036 }
1037 }
1038 }
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001039 nr_copy_pages = 0;
1040 nr_meta_pages = 0;
Rafael J. Wysocki75534b52006-09-25 23:32:52 -07001041 restore_pblist = NULL;
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -08001042 buffer = NULL;
Rafael J. Wysocki25761b62005-10-30 14:59:56 -08001043}
1044
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001045#ifdef CONFIG_HIGHMEM
1046/**
1047 * count_pages_for_highmem - compute the number of non-highmem pages
1048 * that will be necessary for creating copies of highmem pages.
1049 */
1050
1051static unsigned int count_pages_for_highmem(unsigned int nr_highmem)
1052{
1053 unsigned int free_highmem = count_free_highmem_pages();
1054
1055 if (free_highmem >= nr_highmem)
1056 nr_highmem = 0;
1057 else
1058 nr_highmem -= free_highmem;
1059
1060 return nr_highmem;
1061}
1062#else
1063static unsigned int
1064count_pages_for_highmem(unsigned int nr_highmem) { return 0; }
1065#endif /* CONFIG_HIGHMEM */
Rafael J. Wysocki25761b62005-10-30 14:59:56 -08001066
1067/**
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001068 * enough_free_mem - Make sure we have enough free memory for the
1069 * snapshot image.
Rafael J. Wysocki25761b62005-10-30 14:59:56 -08001070 */
1071
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001072static int enough_free_mem(unsigned int nr_pages, unsigned int nr_highmem)
Rafael J. Wysocki25761b62005-10-30 14:59:56 -08001073{
Rafael J. Wysockie5e2fa72006-01-06 00:14:20 -08001074 struct zone *zone;
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001075 unsigned int free = 0, meta = 0;
Rafael J. Wysockie5e2fa72006-01-06 00:14:20 -08001076
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001077 for_each_zone(zone) {
1078 meta += snapshot_additional_pages(zone);
1079 if (!is_highmem(zone))
Christoph Lameterd23ad422007-02-10 01:43:02 -08001080 free += zone_page_state(zone, NR_FREE_PAGES);
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001081 }
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001082
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001083 nr_pages += count_pages_for_highmem(nr_highmem);
1084 pr_debug("swsusp: Normal pages needed: %u + %u + %u, available pages: %u\n",
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001085 nr_pages, PAGES_FOR_IO, meta, free);
1086
1087 return free > nr_pages + PAGES_FOR_IO + meta;
Rafael J. Wysocki25761b62005-10-30 14:59:56 -08001088}
1089
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001090#ifdef CONFIG_HIGHMEM
1091/**
1092 * get_highmem_buffer - if there are some highmem pages in the suspend
1093 * image, we may need the buffer to copy them and/or load their data.
1094 */
1095
1096static inline int get_highmem_buffer(int safe_needed)
1097{
1098 buffer = get_image_page(GFP_ATOMIC | __GFP_COLD, safe_needed);
1099 return buffer ? 0 : -ENOMEM;
1100}
1101
1102/**
1103 * alloc_highmem_image_pages - allocate some highmem pages for the image.
1104 * Try to allocate as many pages as needed, but if the number of free
1105 * highmem pages is lesser than that, allocate them all.
1106 */
1107
1108static inline unsigned int
1109alloc_highmem_image_pages(struct memory_bitmap *bm, unsigned int nr_highmem)
1110{
1111 unsigned int to_alloc = count_free_highmem_pages();
1112
1113 if (to_alloc > nr_highmem)
1114 to_alloc = nr_highmem;
1115
1116 nr_highmem -= to_alloc;
1117 while (to_alloc-- > 0) {
1118 struct page *page;
1119
1120 page = alloc_image_page(__GFP_HIGHMEM);
1121 memory_bm_set_bit(bm, page_to_pfn(page));
1122 }
1123 return nr_highmem;
1124}
1125#else
1126static inline int get_highmem_buffer(int safe_needed) { return 0; }
1127
1128static inline unsigned int
1129alloc_highmem_image_pages(struct memory_bitmap *bm, unsigned int n) { return 0; }
1130#endif /* CONFIG_HIGHMEM */
1131
1132/**
1133 * swsusp_alloc - allocate memory for the suspend image
1134 *
1135 * We first try to allocate as many highmem pages as there are
1136 * saveable highmem pages in the system. If that fails, we allocate
1137 * non-highmem pages for the copies of the remaining highmem ones.
1138 *
1139 * In this approach it is likely that the copies of highmem pages will
1140 * also be located in the high memory, because of the way in which
1141 * copy_data_pages() works.
1142 */
1143
Rafael J. Wysockib788db72006-09-25 23:32:54 -07001144static int
1145swsusp_alloc(struct memory_bitmap *orig_bm, struct memory_bitmap *copy_bm,
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001146 unsigned int nr_pages, unsigned int nr_highmem)
Rafael J. Wysocki054bd4c2005-11-08 21:34:39 -08001147{
Rafael J. Wysockib788db72006-09-25 23:32:54 -07001148 int error;
Rafael J. Wysocki054bd4c2005-11-08 21:34:39 -08001149
Rafael J. Wysockib788db72006-09-25 23:32:54 -07001150 error = memory_bm_create(orig_bm, GFP_ATOMIC | __GFP_COLD, PG_ANY);
1151 if (error)
1152 goto Free;
1153
1154 error = memory_bm_create(copy_bm, GFP_ATOMIC | __GFP_COLD, PG_ANY);
1155 if (error)
1156 goto Free;
1157
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001158 if (nr_highmem > 0) {
1159 error = get_highmem_buffer(PG_ANY);
1160 if (error)
1161 goto Free;
1162
1163 nr_pages += alloc_highmem_image_pages(copy_bm, nr_highmem);
1164 }
Rafael J. Wysockib788db72006-09-25 23:32:54 -07001165 while (nr_pages-- > 0) {
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001166 struct page *page = alloc_image_page(GFP_ATOMIC | __GFP_COLD);
1167
Rafael J. Wysockib788db72006-09-25 23:32:54 -07001168 if (!page)
1169 goto Free;
1170
Rafael J. Wysockib788db72006-09-25 23:32:54 -07001171 memory_bm_set_bit(copy_bm, page_to_pfn(page));
Rafael J. Wysocki054bd4c2005-11-08 21:34:39 -08001172 }
1173 return 0;
Rafael J. Wysockib788db72006-09-25 23:32:54 -07001174
Rafael J. Wysocki59a49332006-12-06 20:34:44 -08001175 Free:
Rafael J. Wysockib788db72006-09-25 23:32:54 -07001176 swsusp_free();
1177 return -ENOMEM;
Rafael J. Wysocki054bd4c2005-11-08 21:34:39 -08001178}
Rafael J. Wysocki25761b62005-10-30 14:59:56 -08001179
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001180/* Memory bitmap used for marking saveable pages (during suspend) or the
1181 * suspend image pages (during resume)
1182 */
Rafael J. Wysockib788db72006-09-25 23:32:54 -07001183static struct memory_bitmap orig_bm;
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001184/* Memory bitmap used on suspend for marking allocated pages that will contain
1185 * the copies of saveable pages. During resume it is initially used for
1186 * marking the suspend image pages, but then its set bits are duplicated in
1187 * @orig_bm and it is released. Next, on systems with high memory, it may be
1188 * used for marking "safe" highmem pages, but it has to be reinitialized for
1189 * this purpose.
Rafael J. Wysockib788db72006-09-25 23:32:54 -07001190 */
1191static struct memory_bitmap copy_bm;
Rafael J. Wysocki25761b62005-10-30 14:59:56 -08001192
Rafael J. Wysocki2e32a432005-10-30 15:00:00 -08001193asmlinkage int swsusp_save(void)
Rafael J. Wysocki25761b62005-10-30 14:59:56 -08001194{
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001195 unsigned int nr_pages, nr_highmem;
Rafael J. Wysocki25761b62005-10-30 14:59:56 -08001196
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001197 printk("swsusp: critical section: \n");
Rafael J. Wysocki25761b62005-10-30 14:59:56 -08001198
1199 drain_local_pages();
Rafael J. Wysockia0f49652005-10-30 14:59:57 -08001200 nr_pages = count_data_pages();
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001201 nr_highmem = count_highmem_pages();
1202 printk("swsusp: Need to copy %u pages\n", nr_pages + nr_highmem);
Rafael J. Wysocki25761b62005-10-30 14:59:56 -08001203
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001204 if (!enough_free_mem(nr_pages, nr_highmem)) {
Rafael J. Wysocki25761b62005-10-30 14:59:56 -08001205 printk(KERN_ERR "swsusp: Not enough free memory\n");
1206 return -ENOMEM;
1207 }
1208
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001209 if (swsusp_alloc(&orig_bm, &copy_bm, nr_pages, nr_highmem)) {
1210 printk(KERN_ERR "swsusp: Memory allocation failed\n");
Rafael J. Wysockia0f49652005-10-30 14:59:57 -08001211 return -ENOMEM;
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001212 }
Rafael J. Wysocki25761b62005-10-30 14:59:56 -08001213
1214 /* During allocating of suspend pagedir, new cold pages may appear.
1215 * Kill them.
1216 */
1217 drain_local_pages();
Rafael J. Wysockib788db72006-09-25 23:32:54 -07001218 copy_data_pages(&copy_bm, &orig_bm);
Rafael J. Wysocki25761b62005-10-30 14:59:56 -08001219
1220 /*
1221 * End of critical section. From now on, we can write to memory,
1222 * but we should not touch disk. This specially means we must _not_
1223 * touch swap space! Except we must write out our image of course.
1224 */
1225
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001226 nr_pages += nr_highmem;
Rafael J. Wysockia0f49652005-10-30 14:59:57 -08001227 nr_copy_pages = nr_pages;
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001228 nr_meta_pages = DIV_ROUND_UP(nr_pages * sizeof(long), PAGE_SIZE);
Rafael J. Wysockia0f49652005-10-30 14:59:57 -08001229
1230 printk("swsusp: critical section/: done (%d pages copied)\n", nr_pages);
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001231
Rafael J. Wysocki25761b62005-10-30 14:59:56 -08001232 return 0;
1233}
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001234
1235static void init_header(struct swsusp_info *info)
1236{
1237 memset(info, 0, sizeof(struct swsusp_info));
1238 info->version_code = LINUX_VERSION_CODE;
1239 info->num_physpages = num_physpages;
Serge E. Hallyn96b644b2006-10-02 02:18:13 -07001240 memcpy(&info->uts, init_utsname(), sizeof(struct new_utsname));
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001241 info->cpus = num_online_cpus();
1242 info->image_pages = nr_copy_pages;
1243 info->pages = nr_copy_pages + nr_meta_pages + 1;
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -08001244 info->size = info->pages;
1245 info->size <<= PAGE_SHIFT;
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001246}
1247
1248/**
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001249 * pack_pfns - pfns corresponding to the set bits found in the bitmap @bm
1250 * are stored in the array @buf[] (1 page at a time)
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001251 */
1252
Rafael J. Wysockib788db72006-09-25 23:32:54 -07001253static inline void
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001254pack_pfns(unsigned long *buf, struct memory_bitmap *bm)
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001255{
1256 int j;
1257
Rafael J. Wysockib788db72006-09-25 23:32:54 -07001258 for (j = 0; j < PAGE_SIZE / sizeof(long); j++) {
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001259 buf[j] = memory_bm_next_pfn(bm);
1260 if (unlikely(buf[j] == BM_END_OF_MAP))
Rafael J. Wysockib788db72006-09-25 23:32:54 -07001261 break;
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001262 }
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001263}
1264
1265/**
1266 * snapshot_read_next - used for reading the system memory snapshot.
1267 *
1268 * On the first call to it @handle should point to a zeroed
1269 * snapshot_handle structure. The structure gets updated and a pointer
1270 * to it should be passed to this function every next time.
1271 *
1272 * The @count parameter should contain the number of bytes the caller
1273 * wants to read from the snapshot. It must not be zero.
1274 *
1275 * On success the function returns a positive number. Then, the caller
1276 * is allowed to read up to the returned number of bytes from the memory
1277 * location computed by the data_of() macro. The number returned
1278 * may be smaller than @count, but this only happens if the read would
1279 * cross a page boundary otherwise.
1280 *
1281 * The function returns 0 to indicate the end of data stream condition,
1282 * and a negative number is returned on error. In such cases the
1283 * structure pointed to by @handle is not updated and should not be used
1284 * any more.
1285 */
1286
1287int snapshot_read_next(struct snapshot_handle *handle, size_t count)
1288{
Rafael J. Wysockifb13a282006-09-25 23:32:46 -07001289 if (handle->cur > nr_meta_pages + nr_copy_pages)
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001290 return 0;
Rafael J. Wysockib788db72006-09-25 23:32:54 -07001291
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001292 if (!buffer) {
1293 /* This makes the buffer be freed by swsusp_free() */
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001294 buffer = get_image_page(GFP_ATOMIC, PG_ANY);
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001295 if (!buffer)
1296 return -ENOMEM;
1297 }
1298 if (!handle->offset) {
1299 init_header((struct swsusp_info *)buffer);
1300 handle->buffer = buffer;
Rafael J. Wysockib788db72006-09-25 23:32:54 -07001301 memory_bm_position_reset(&orig_bm);
1302 memory_bm_position_reset(&copy_bm);
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001303 }
Rafael J. Wysockifb13a282006-09-25 23:32:46 -07001304 if (handle->prev < handle->cur) {
1305 if (handle->cur <= nr_meta_pages) {
Rafael J. Wysockib788db72006-09-25 23:32:54 -07001306 memset(buffer, 0, PAGE_SIZE);
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001307 pack_pfns(buffer, &orig_bm);
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001308 } else {
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001309 struct page *page;
Rafael J. Wysockib788db72006-09-25 23:32:54 -07001310
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001311 page = pfn_to_page(memory_bm_next_pfn(&copy_bm));
1312 if (PageHighMem(page)) {
1313 /* Highmem pages are copied to the buffer,
1314 * because we can't return with a kmapped
1315 * highmem page (we may not be called again).
1316 */
1317 void *kaddr;
1318
1319 kaddr = kmap_atomic(page, KM_USER0);
1320 memcpy(buffer, kaddr, PAGE_SIZE);
1321 kunmap_atomic(kaddr, KM_USER0);
1322 handle->buffer = buffer;
1323 } else {
1324 handle->buffer = page_address(page);
1325 }
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001326 }
Rafael J. Wysockifb13a282006-09-25 23:32:46 -07001327 handle->prev = handle->cur;
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001328 }
Rafael J. Wysockifb13a282006-09-25 23:32:46 -07001329 handle->buf_offset = handle->cur_offset;
1330 if (handle->cur_offset + count >= PAGE_SIZE) {
1331 count = PAGE_SIZE - handle->cur_offset;
1332 handle->cur_offset = 0;
1333 handle->cur++;
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001334 } else {
Rafael J. Wysockifb13a282006-09-25 23:32:46 -07001335 handle->cur_offset += count;
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001336 }
1337 handle->offset += count;
1338 return count;
1339}
1340
1341/**
1342 * mark_unsafe_pages - mark the pages that cannot be used for storing
1343 * the image during resume, because they conflict with the pages that
1344 * had been used before suspend
1345 */
1346
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001347static int mark_unsafe_pages(struct memory_bitmap *bm)
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001348{
1349 struct zone *zone;
Rafael J. Wysockiae83c5e2006-09-25 23:32:45 -07001350 unsigned long pfn, max_zone_pfn;
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001351
1352 /* Clear page flags */
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001353 for_each_zone(zone) {
Rafael J. Wysockiae83c5e2006-09-25 23:32:45 -07001354 max_zone_pfn = zone->zone_start_pfn + zone->spanned_pages;
1355 for (pfn = zone->zone_start_pfn; pfn < max_zone_pfn; pfn++)
1356 if (pfn_valid(pfn))
Rafael J. Wysocki7be98232007-05-06 14:50:42 -07001357 swsusp_unset_page_free(pfn_to_page(pfn));
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001358 }
1359
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001360 /* Mark pages that correspond to the "original" pfns as "unsafe" */
1361 memory_bm_position_reset(bm);
1362 do {
1363 pfn = memory_bm_next_pfn(bm);
1364 if (likely(pfn != BM_END_OF_MAP)) {
1365 if (likely(pfn_valid(pfn)))
Rafael J. Wysocki7be98232007-05-06 14:50:42 -07001366 swsusp_set_page_free(pfn_to_page(pfn));
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001367 else
1368 return -EFAULT;
1369 }
1370 } while (pfn != BM_END_OF_MAP);
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001371
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001372 allocated_unsafe_pages = 0;
Rafael J. Wysocki968808b2006-06-23 02:04:48 -07001373
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001374 return 0;
1375}
1376
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001377static void
1378duplicate_memory_bitmap(struct memory_bitmap *dst, struct memory_bitmap *src)
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001379{
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001380 unsigned long pfn;
1381
1382 memory_bm_position_reset(src);
1383 pfn = memory_bm_next_pfn(src);
1384 while (pfn != BM_END_OF_MAP) {
1385 memory_bm_set_bit(dst, pfn);
1386 pfn = memory_bm_next_pfn(src);
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001387 }
1388}
1389
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001390static inline int check_header(struct swsusp_info *info)
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001391{
1392 char *reason = NULL;
1393
1394 if (info->version_code != LINUX_VERSION_CODE)
1395 reason = "kernel version";
1396 if (info->num_physpages != num_physpages)
1397 reason = "memory size";
Serge E. Hallyn96b644b2006-10-02 02:18:13 -07001398 if (strcmp(info->uts.sysname,init_utsname()->sysname))
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001399 reason = "system type";
Serge E. Hallyn96b644b2006-10-02 02:18:13 -07001400 if (strcmp(info->uts.release,init_utsname()->release))
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001401 reason = "kernel release";
Serge E. Hallyn96b644b2006-10-02 02:18:13 -07001402 if (strcmp(info->uts.version,init_utsname()->version))
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001403 reason = "version";
Serge E. Hallyn96b644b2006-10-02 02:18:13 -07001404 if (strcmp(info->uts.machine,init_utsname()->machine))
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001405 reason = "machine";
1406 if (reason) {
1407 printk(KERN_ERR "swsusp: Resume mismatch: %s\n", reason);
1408 return -EPERM;
1409 }
1410 return 0;
1411}
1412
1413/**
1414 * load header - check the image header and copy data from it
1415 */
1416
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001417static int
1418load_header(struct swsusp_info *info)
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001419{
1420 int error;
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001421
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001422 restore_pblist = NULL;
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001423 error = check_header(info);
1424 if (!error) {
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001425 nr_copy_pages = info->image_pages;
1426 nr_meta_pages = info->pages - info->image_pages - 1;
1427 }
1428 return error;
1429}
1430
1431/**
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001432 * unpack_orig_pfns - for each element of @buf[] (1 page at a time) set
1433 * the corresponding bit in the memory bitmap @bm
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001434 */
1435
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001436static inline void
1437unpack_orig_pfns(unsigned long *buf, struct memory_bitmap *bm)
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001438{
1439 int j;
1440
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001441 for (j = 0; j < PAGE_SIZE / sizeof(long); j++) {
1442 if (unlikely(buf[j] == BM_END_OF_MAP))
1443 break;
1444
1445 memory_bm_set_bit(bm, buf[j]);
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001446 }
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001447}
1448
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001449/* List of "safe" pages that may be used to store data loaded from the suspend
1450 * image
1451 */
1452static struct linked_page *safe_pages_list;
1453
1454#ifdef CONFIG_HIGHMEM
1455/* struct highmem_pbe is used for creating the list of highmem pages that
1456 * should be restored atomically during the resume from disk, because the page
1457 * frames they have occupied before the suspend are in use.
1458 */
1459struct highmem_pbe {
1460 struct page *copy_page; /* data is here now */
1461 struct page *orig_page; /* data was here before the suspend */
1462 struct highmem_pbe *next;
1463};
1464
1465/* List of highmem PBEs needed for restoring the highmem pages that were
1466 * allocated before the suspend and included in the suspend image, but have
1467 * also been allocated by the "resume" kernel, so their contents cannot be
1468 * written directly to their "original" page frames.
1469 */
1470static struct highmem_pbe *highmem_pblist;
1471
1472/**
1473 * count_highmem_image_pages - compute the number of highmem pages in the
1474 * suspend image. The bits in the memory bitmap @bm that correspond to the
1475 * image pages are assumed to be set.
1476 */
1477
1478static unsigned int count_highmem_image_pages(struct memory_bitmap *bm)
1479{
1480 unsigned long pfn;
1481 unsigned int cnt = 0;
1482
1483 memory_bm_position_reset(bm);
1484 pfn = memory_bm_next_pfn(bm);
1485 while (pfn != BM_END_OF_MAP) {
1486 if (PageHighMem(pfn_to_page(pfn)))
1487 cnt++;
1488
1489 pfn = memory_bm_next_pfn(bm);
1490 }
1491 return cnt;
1492}
1493
1494/**
1495 * prepare_highmem_image - try to allocate as many highmem pages as
1496 * there are highmem image pages (@nr_highmem_p points to the variable
1497 * containing the number of highmem image pages). The pages that are
1498 * "safe" (ie. will not be overwritten when the suspend image is
1499 * restored) have the corresponding bits set in @bm (it must be
1500 * unitialized).
1501 *
1502 * NOTE: This function should not be called if there are no highmem
1503 * image pages.
1504 */
1505
1506static unsigned int safe_highmem_pages;
1507
1508static struct memory_bitmap *safe_highmem_bm;
1509
1510static int
1511prepare_highmem_image(struct memory_bitmap *bm, unsigned int *nr_highmem_p)
1512{
1513 unsigned int to_alloc;
1514
1515 if (memory_bm_create(bm, GFP_ATOMIC, PG_SAFE))
1516 return -ENOMEM;
1517
1518 if (get_highmem_buffer(PG_SAFE))
1519 return -ENOMEM;
1520
1521 to_alloc = count_free_highmem_pages();
1522 if (to_alloc > *nr_highmem_p)
1523 to_alloc = *nr_highmem_p;
1524 else
1525 *nr_highmem_p = to_alloc;
1526
1527 safe_highmem_pages = 0;
1528 while (to_alloc-- > 0) {
1529 struct page *page;
1530
1531 page = alloc_page(__GFP_HIGHMEM);
Rafael J. Wysocki7be98232007-05-06 14:50:42 -07001532 if (!swsusp_page_is_free(page)) {
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001533 /* The page is "safe", set its bit the bitmap */
1534 memory_bm_set_bit(bm, page_to_pfn(page));
1535 safe_highmem_pages++;
1536 }
1537 /* Mark the page as allocated */
Rafael J. Wysocki7be98232007-05-06 14:50:42 -07001538 swsusp_set_page_forbidden(page);
1539 swsusp_set_page_free(page);
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001540 }
1541 memory_bm_position_reset(bm);
1542 safe_highmem_bm = bm;
1543 return 0;
1544}
1545
1546/**
1547 * get_highmem_page_buffer - for given highmem image page find the buffer
1548 * that suspend_write_next() should set for its caller to write to.
1549 *
1550 * If the page is to be saved to its "original" page frame or a copy of
1551 * the page is to be made in the highmem, @buffer is returned. Otherwise,
1552 * the copy of the page is to be made in normal memory, so the address of
1553 * the copy is returned.
1554 *
1555 * If @buffer is returned, the caller of suspend_write_next() will write
1556 * the page's contents to @buffer, so they will have to be copied to the
1557 * right location on the next call to suspend_write_next() and it is done
1558 * with the help of copy_last_highmem_page(). For this purpose, if
1559 * @buffer is returned, @last_highmem page is set to the page to which
1560 * the data will have to be copied from @buffer.
1561 */
1562
1563static struct page *last_highmem_page;
1564
1565static void *
1566get_highmem_page_buffer(struct page *page, struct chain_allocator *ca)
1567{
1568 struct highmem_pbe *pbe;
1569 void *kaddr;
1570
Rafael J. Wysocki7be98232007-05-06 14:50:42 -07001571 if (swsusp_page_is_forbidden(page) && swsusp_page_is_free(page)) {
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001572 /* We have allocated the "original" page frame and we can
1573 * use it directly to store the loaded page.
1574 */
1575 last_highmem_page = page;
1576 return buffer;
1577 }
1578 /* The "original" page frame has not been allocated and we have to
1579 * use a "safe" page frame to store the loaded page.
1580 */
1581 pbe = chain_alloc(ca, sizeof(struct highmem_pbe));
1582 if (!pbe) {
1583 swsusp_free();
1584 return NULL;
1585 }
1586 pbe->orig_page = page;
1587 if (safe_highmem_pages > 0) {
1588 struct page *tmp;
1589
1590 /* Copy of the page will be stored in high memory */
1591 kaddr = buffer;
1592 tmp = pfn_to_page(memory_bm_next_pfn(safe_highmem_bm));
1593 safe_highmem_pages--;
1594 last_highmem_page = tmp;
1595 pbe->copy_page = tmp;
1596 } else {
1597 /* Copy of the page will be stored in normal memory */
1598 kaddr = safe_pages_list;
1599 safe_pages_list = safe_pages_list->next;
1600 pbe->copy_page = virt_to_page(kaddr);
1601 }
1602 pbe->next = highmem_pblist;
1603 highmem_pblist = pbe;
1604 return kaddr;
1605}
1606
1607/**
1608 * copy_last_highmem_page - copy the contents of a highmem image from
1609 * @buffer, where the caller of snapshot_write_next() has place them,
1610 * to the right location represented by @last_highmem_page .
1611 */
1612
1613static void copy_last_highmem_page(void)
1614{
1615 if (last_highmem_page) {
1616 void *dst;
1617
1618 dst = kmap_atomic(last_highmem_page, KM_USER0);
1619 memcpy(dst, buffer, PAGE_SIZE);
1620 kunmap_atomic(dst, KM_USER0);
1621 last_highmem_page = NULL;
1622 }
1623}
1624
1625static inline int last_highmem_page_copied(void)
1626{
1627 return !last_highmem_page;
1628}
1629
1630static inline void free_highmem_data(void)
1631{
1632 if (safe_highmem_bm)
1633 memory_bm_free(safe_highmem_bm, PG_UNSAFE_CLEAR);
1634
1635 if (buffer)
1636 free_image_page(buffer, PG_UNSAFE_CLEAR);
1637}
1638#else
1639static inline int get_safe_write_buffer(void) { return 0; }
1640
1641static unsigned int
1642count_highmem_image_pages(struct memory_bitmap *bm) { return 0; }
1643
1644static inline int
1645prepare_highmem_image(struct memory_bitmap *bm, unsigned int *nr_highmem_p)
1646{
1647 return 0;
1648}
1649
1650static inline void *
1651get_highmem_page_buffer(struct page *page, struct chain_allocator *ca)
1652{
1653 return NULL;
1654}
1655
1656static inline void copy_last_highmem_page(void) {}
1657static inline int last_highmem_page_copied(void) { return 1; }
1658static inline void free_highmem_data(void) {}
1659#endif /* CONFIG_HIGHMEM */
1660
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001661/**
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001662 * prepare_image - use the memory bitmap @bm to mark the pages that will
1663 * be overwritten in the process of restoring the system memory state
1664 * from the suspend image ("unsafe" pages) and allocate memory for the
1665 * image.
Rafael J. Wysocki968808b2006-06-23 02:04:48 -07001666 *
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001667 * The idea is to allocate a new memory bitmap first and then allocate
1668 * as many pages as needed for the image data, but not to assign these
1669 * pages to specific tasks initially. Instead, we just mark them as
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001670 * allocated and create a lists of "safe" pages that will be used
1671 * later. On systems with high memory a list of "safe" highmem pages is
1672 * also created.
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001673 */
1674
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001675#define PBES_PER_LINKED_PAGE (LINKED_PAGE_DATA_SIZE / sizeof(struct pbe))
Rafael J. Wysocki968808b2006-06-23 02:04:48 -07001676
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001677static int
1678prepare_image(struct memory_bitmap *new_bm, struct memory_bitmap *bm)
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001679{
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001680 unsigned int nr_pages, nr_highmem;
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001681 struct linked_page *sp_list, *lp;
1682 int error;
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001683
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001684 /* If there is no highmem, the buffer will not be necessary */
1685 free_image_page(buffer, PG_UNSAFE_CLEAR);
1686 buffer = NULL;
1687
1688 nr_highmem = count_highmem_image_pages(bm);
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001689 error = mark_unsafe_pages(bm);
1690 if (error)
1691 goto Free;
1692
1693 error = memory_bm_create(new_bm, GFP_ATOMIC, PG_SAFE);
1694 if (error)
1695 goto Free;
1696
1697 duplicate_memory_bitmap(new_bm, bm);
1698 memory_bm_free(bm, PG_UNSAFE_KEEP);
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001699 if (nr_highmem > 0) {
1700 error = prepare_highmem_image(bm, &nr_highmem);
1701 if (error)
1702 goto Free;
1703 }
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001704 /* Reserve some safe pages for potential later use.
1705 *
1706 * NOTE: This way we make sure there will be enough safe pages for the
1707 * chain_alloc() in get_buffer(). It is a bit wasteful, but
1708 * nr_copy_pages cannot be greater than 50% of the memory anyway.
1709 */
1710 sp_list = NULL;
1711 /* nr_copy_pages cannot be lesser than allocated_unsafe_pages */
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001712 nr_pages = nr_copy_pages - nr_highmem - allocated_unsafe_pages;
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001713 nr_pages = DIV_ROUND_UP(nr_pages, PBES_PER_LINKED_PAGE);
1714 while (nr_pages > 0) {
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001715 lp = get_image_page(GFP_ATOMIC, PG_SAFE);
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001716 if (!lp) {
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001717 error = -ENOMEM;
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001718 goto Free;
Rafael J. Wysocki968808b2006-06-23 02:04:48 -07001719 }
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001720 lp->next = sp_list;
1721 sp_list = lp;
1722 nr_pages--;
Rafael J. Wysocki968808b2006-06-23 02:04:48 -07001723 }
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001724 /* Preallocate memory for the image */
1725 safe_pages_list = NULL;
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001726 nr_pages = nr_copy_pages - nr_highmem - allocated_unsafe_pages;
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001727 while (nr_pages > 0) {
1728 lp = (struct linked_page *)get_zeroed_page(GFP_ATOMIC);
1729 if (!lp) {
1730 error = -ENOMEM;
1731 goto Free;
1732 }
Rafael J. Wysocki7be98232007-05-06 14:50:42 -07001733 if (!swsusp_page_is_free(virt_to_page(lp))) {
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001734 /* The page is "safe", add it to the list */
1735 lp->next = safe_pages_list;
1736 safe_pages_list = lp;
1737 }
1738 /* Mark the page as allocated */
Rafael J. Wysocki7be98232007-05-06 14:50:42 -07001739 swsusp_set_page_forbidden(virt_to_page(lp));
1740 swsusp_set_page_free(virt_to_page(lp));
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001741 nr_pages--;
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001742 }
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001743 /* Free the reserved safe pages so that chain_alloc() can use them */
1744 while (sp_list) {
1745 lp = sp_list->next;
1746 free_image_page(sp_list, PG_UNSAFE_CLEAR);
1747 sp_list = lp;
1748 }
1749 return 0;
1750
Rafael J. Wysocki59a49332006-12-06 20:34:44 -08001751 Free:
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001752 swsusp_free();
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001753 return error;
1754}
1755
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001756/**
1757 * get_buffer - compute the address that snapshot_write_next() should
1758 * set for its caller to write to.
1759 */
Rafael J. Wysocki968808b2006-06-23 02:04:48 -07001760
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001761static void *get_buffer(struct memory_bitmap *bm, struct chain_allocator *ca)
1762{
1763 struct pbe *pbe;
1764 struct page *page = pfn_to_page(memory_bm_next_pfn(bm));
1765
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001766 if (PageHighMem(page))
1767 return get_highmem_page_buffer(page, ca);
1768
Rafael J. Wysocki7be98232007-05-06 14:50:42 -07001769 if (swsusp_page_is_forbidden(page) && swsusp_page_is_free(page))
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001770 /* We have allocated the "original" page frame and we can
1771 * use it directly to store the loaded page.
Rafael J. Wysocki968808b2006-06-23 02:04:48 -07001772 */
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001773 return page_address(page);
1774
1775 /* The "original" page frame has not been allocated and we have to
1776 * use a "safe" page frame to store the loaded page.
Rafael J. Wysocki968808b2006-06-23 02:04:48 -07001777 */
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001778 pbe = chain_alloc(ca, sizeof(struct pbe));
1779 if (!pbe) {
1780 swsusp_free();
1781 return NULL;
1782 }
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001783 pbe->orig_address = page_address(page);
1784 pbe->address = safe_pages_list;
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001785 safe_pages_list = safe_pages_list->next;
1786 pbe->next = restore_pblist;
1787 restore_pblist = pbe;
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001788 return pbe->address;
Rafael J. Wysocki968808b2006-06-23 02:04:48 -07001789}
1790
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001791/**
1792 * snapshot_write_next - used for writing the system memory snapshot.
1793 *
1794 * On the first call to it @handle should point to a zeroed
1795 * snapshot_handle structure. The structure gets updated and a pointer
1796 * to it should be passed to this function every next time.
1797 *
1798 * The @count parameter should contain the number of bytes the caller
1799 * wants to write to the image. It must not be zero.
1800 *
1801 * On success the function returns a positive number. Then, the caller
1802 * is allowed to write up to the returned number of bytes to the memory
1803 * location computed by the data_of() macro. The number returned
1804 * may be smaller than @count, but this only happens if the write would
1805 * cross a page boundary otherwise.
1806 *
1807 * The function returns 0 to indicate the "end of file" condition,
1808 * and a negative number is returned on error. In such cases the
1809 * structure pointed to by @handle is not updated and should not be used
1810 * any more.
1811 */
1812
1813int snapshot_write_next(struct snapshot_handle *handle, size_t count)
1814{
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001815 static struct chain_allocator ca;
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001816 int error = 0;
1817
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001818 /* Check if we have already loaded the entire image */
Rafael J. Wysockifb13a282006-09-25 23:32:46 -07001819 if (handle->prev && handle->cur > nr_meta_pages + nr_copy_pages)
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001820 return 0;
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001821
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001822 if (handle->offset == 0) {
1823 if (!buffer)
1824 /* This makes the buffer be freed by swsusp_free() */
1825 buffer = get_image_page(GFP_ATOMIC, PG_ANY);
1826
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001827 if (!buffer)
1828 return -ENOMEM;
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001829
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001830 handle->buffer = buffer;
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001831 }
Andrew Morton546e0d22006-09-25 23:32:44 -07001832 handle->sync_read = 1;
Rafael J. Wysockifb13a282006-09-25 23:32:46 -07001833 if (handle->prev < handle->cur) {
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001834 if (handle->prev == 0) {
1835 error = load_header(buffer);
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001836 if (error)
1837 return error;
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001838
1839 error = memory_bm_create(&copy_bm, GFP_ATOMIC, PG_ANY);
1840 if (error)
1841 return error;
1842
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001843 } else if (handle->prev <= nr_meta_pages) {
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001844 unpack_orig_pfns(buffer, &copy_bm);
1845 if (handle->prev == nr_meta_pages) {
1846 error = prepare_image(&orig_bm, &copy_bm);
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001847 if (error)
1848 return error;
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001849
1850 chain_init(&ca, GFP_ATOMIC, PG_SAFE);
1851 memory_bm_position_reset(&orig_bm);
1852 restore_pblist = NULL;
1853 handle->buffer = get_buffer(&orig_bm, &ca);
Andrew Morton546e0d22006-09-25 23:32:44 -07001854 handle->sync_read = 0;
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001855 if (!handle->buffer)
1856 return -ENOMEM;
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001857 }
1858 } else {
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001859 copy_last_highmem_page();
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001860 handle->buffer = get_buffer(&orig_bm, &ca);
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001861 if (handle->buffer != buffer)
1862 handle->sync_read = 0;
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001863 }
Rafael J. Wysockifb13a282006-09-25 23:32:46 -07001864 handle->prev = handle->cur;
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001865 }
Rafael J. Wysockifb13a282006-09-25 23:32:46 -07001866 handle->buf_offset = handle->cur_offset;
1867 if (handle->cur_offset + count >= PAGE_SIZE) {
1868 count = PAGE_SIZE - handle->cur_offset;
1869 handle->cur_offset = 0;
1870 handle->cur++;
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001871 } else {
Rafael J. Wysockifb13a282006-09-25 23:32:46 -07001872 handle->cur_offset += count;
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001873 }
1874 handle->offset += count;
1875 return count;
1876}
1877
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001878/**
1879 * snapshot_write_finalize - must be called after the last call to
1880 * snapshot_write_next() in case the last page in the image happens
1881 * to be a highmem page and its contents should be stored in the
1882 * highmem. Additionally, it releases the memory that will not be
1883 * used any more.
1884 */
1885
1886void snapshot_write_finalize(struct snapshot_handle *handle)
1887{
1888 copy_last_highmem_page();
1889 /* Free only if we have loaded the image entirely */
1890 if (handle->prev && handle->cur > nr_meta_pages + nr_copy_pages) {
1891 memory_bm_free(&orig_bm, PG_UNSAFE_CLEAR);
1892 free_highmem_data();
1893 }
1894}
1895
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001896int snapshot_image_loaded(struct snapshot_handle *handle)
1897{
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001898 return !(!nr_copy_pages || !last_highmem_page_copied() ||
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001899 handle->cur <= nr_meta_pages + nr_copy_pages);
1900}
1901
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001902#ifdef CONFIG_HIGHMEM
1903/* Assumes that @buf is ready and points to a "safe" page */
1904static inline void
1905swap_two_pages_data(struct page *p1, struct page *p2, void *buf)
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001906{
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001907 void *kaddr1, *kaddr2;
1908
1909 kaddr1 = kmap_atomic(p1, KM_USER0);
1910 kaddr2 = kmap_atomic(p2, KM_USER1);
1911 memcpy(buf, kaddr1, PAGE_SIZE);
1912 memcpy(kaddr1, kaddr2, PAGE_SIZE);
1913 memcpy(kaddr2, buf, PAGE_SIZE);
1914 kunmap_atomic(kaddr1, KM_USER0);
1915 kunmap_atomic(kaddr2, KM_USER1);
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001916}
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001917
1918/**
1919 * restore_highmem - for each highmem page that was allocated before
1920 * the suspend and included in the suspend image, and also has been
1921 * allocated by the "resume" kernel swap its current (ie. "before
1922 * resume") contents with the previous (ie. "before suspend") one.
1923 *
1924 * If the resume eventually fails, we can call this function once
1925 * again and restore the "before resume" highmem state.
1926 */
1927
1928int restore_highmem(void)
1929{
1930 struct highmem_pbe *pbe = highmem_pblist;
1931 void *buf;
1932
1933 if (!pbe)
1934 return 0;
1935
1936 buf = get_image_page(GFP_ATOMIC, PG_SAFE);
1937 if (!buf)
1938 return -ENOMEM;
1939
1940 while (pbe) {
1941 swap_two_pages_data(pbe->copy_page, pbe->orig_page, buf);
1942 pbe = pbe->next;
1943 }
1944 free_image_page(buf, PG_UNSAFE_CLEAR);
1945 return 0;
1946}
1947#endif /* CONFIG_HIGHMEM */