blob: 48fc7a35571bf2039664a59c1ede0bfecc04569c [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>
17#include <linux/smp_lock.h>
Rafael J. Wysocki25761b62005-10-30 14:59:56 -080018#include <linux/delay.h>
Rafael J. Wysocki25761b62005-10-30 14:59:56 -080019#include <linux/bitops.h>
Rafael J. Wysocki25761b62005-10-30 14:59:56 -080020#include <linux/spinlock.h>
Rafael J. Wysocki25761b62005-10-30 14:59:56 -080021#include <linux/kernel.h>
Rafael J. Wysocki25761b62005-10-30 14:59:56 -080022#include <linux/pm.h>
23#include <linux/device.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. Wysocki83573762006-12-06 20:34:18 -080037/* List of PBEs needed for restoring the pages that were allocated before
38 * the suspend and included in the suspend image, but have also been
39 * allocated by the "resume" kernel, so their contents cannot be written
40 * directly to their "original" page frames.
41 */
Rafael J. Wysocki75534b52006-09-25 23:32:52 -070042struct pbe *restore_pblist;
43
Rafael J. Wysocki83573762006-12-06 20:34:18 -080044/* Pointer to an auxiliary buffer (1 page) */
Rafael J. Wysocki940864d2006-09-25 23:32:55 -070045static void *buffer;
Rafael J. Wysocki7088a5c2006-01-06 00:13:05 -080046
Rafael J. Wysockif6143aa2006-09-25 23:32:50 -070047/**
48 * @safe_needed - on resume, for storing the PBE list and the image,
49 * we can only use memory pages that do not conflict with the pages
Rafael J. Wysocki83573762006-12-06 20:34:18 -080050 * used before suspend. The unsafe pages have PageNosaveFree set
51 * and we count them using unsafe_pages.
Rafael J. Wysockif6143aa2006-09-25 23:32:50 -070052 *
Rafael J. Wysocki83573762006-12-06 20:34:18 -080053 * Each allocated image page is marked as PageNosave and PageNosaveFree
54 * so that swsusp_free() can release it.
Rafael J. Wysockif6143aa2006-09-25 23:32:50 -070055 */
56
Rafael J. Wysocki0bcd8882006-09-25 23:32:52 -070057#define PG_ANY 0
58#define PG_SAFE 1
59#define PG_UNSAFE_CLEAR 1
60#define PG_UNSAFE_KEEP 0
61
Rafael J. Wysocki940864d2006-09-25 23:32:55 -070062static unsigned int allocated_unsafe_pages;
Rafael J. Wysockif6143aa2006-09-25 23:32:50 -070063
Rafael J. Wysocki83573762006-12-06 20:34:18 -080064static void *get_image_page(gfp_t gfp_mask, int safe_needed)
Rafael J. Wysockif6143aa2006-09-25 23:32:50 -070065{
66 void *res;
67
68 res = (void *)get_zeroed_page(gfp_mask);
69 if (safe_needed)
Rafael J. Wysocki7be98232007-05-06 14:50:42 -070070 while (res && swsusp_page_is_free(virt_to_page(res))) {
Rafael J. Wysockif6143aa2006-09-25 23:32:50 -070071 /* The page is unsafe, mark it for swsusp_free() */
Rafael J. Wysocki7be98232007-05-06 14:50:42 -070072 swsusp_set_page_forbidden(virt_to_page(res));
Rafael J. Wysocki940864d2006-09-25 23:32:55 -070073 allocated_unsafe_pages++;
Rafael J. Wysockif6143aa2006-09-25 23:32:50 -070074 res = (void *)get_zeroed_page(gfp_mask);
75 }
76 if (res) {
Rafael J. Wysocki7be98232007-05-06 14:50:42 -070077 swsusp_set_page_forbidden(virt_to_page(res));
78 swsusp_set_page_free(virt_to_page(res));
Rafael J. Wysockif6143aa2006-09-25 23:32:50 -070079 }
80 return res;
81}
82
83unsigned long get_safe_page(gfp_t gfp_mask)
84{
Rafael J. Wysocki83573762006-12-06 20:34:18 -080085 return (unsigned long)get_image_page(gfp_mask, PG_SAFE);
86}
87
Rafael J. Wysocki5b6d15d2006-12-06 20:34:43 -080088static struct page *alloc_image_page(gfp_t gfp_mask)
89{
Rafael J. Wysocki83573762006-12-06 20:34:18 -080090 struct page *page;
91
92 page = alloc_page(gfp_mask);
93 if (page) {
Rafael J. Wysocki7be98232007-05-06 14:50:42 -070094 swsusp_set_page_forbidden(page);
95 swsusp_set_page_free(page);
Rafael J. Wysocki83573762006-12-06 20:34:18 -080096 }
97 return page;
Rafael J. Wysockif6143aa2006-09-25 23:32:50 -070098}
99
100/**
101 * free_image_page - free page represented by @addr, allocated with
Rafael J. Wysocki83573762006-12-06 20:34:18 -0800102 * get_image_page (page flags set by it must be cleared)
Rafael J. Wysockif6143aa2006-09-25 23:32:50 -0700103 */
104
105static inline void free_image_page(void *addr, int clear_nosave_free)
106{
Rafael J. Wysocki83573762006-12-06 20:34:18 -0800107 struct page *page;
108
109 BUG_ON(!virt_addr_valid(addr));
110
111 page = virt_to_page(addr);
112
Rafael J. Wysocki7be98232007-05-06 14:50:42 -0700113 swsusp_unset_page_forbidden(page);
Rafael J. Wysockif6143aa2006-09-25 23:32:50 -0700114 if (clear_nosave_free)
Rafael J. Wysocki7be98232007-05-06 14:50:42 -0700115 swsusp_unset_page_free(page);
Rafael J. Wysocki83573762006-12-06 20:34:18 -0800116
117 __free_page(page);
Rafael J. Wysockif6143aa2006-09-25 23:32:50 -0700118}
119
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700120/* struct linked_page is used to build chains of pages */
121
122#define LINKED_PAGE_DATA_SIZE (PAGE_SIZE - sizeof(void *))
123
124struct linked_page {
125 struct linked_page *next;
126 char data[LINKED_PAGE_DATA_SIZE];
127} __attribute__((packed));
128
129static inline void
130free_list_of_pages(struct linked_page *list, int clear_page_nosave)
131{
132 while (list) {
133 struct linked_page *lp = list->next;
134
135 free_image_page(list, clear_page_nosave);
136 list = lp;
137 }
138}
139
140/**
141 * struct chain_allocator is used for allocating small objects out of
142 * a linked list of pages called 'the chain'.
143 *
144 * The chain grows each time when there is no room for a new object in
145 * the current page. The allocated objects cannot be freed individually.
146 * It is only possible to free them all at once, by freeing the entire
147 * chain.
148 *
149 * NOTE: The chain allocator may be inefficient if the allocated objects
150 * are not much smaller than PAGE_SIZE.
151 */
152
153struct chain_allocator {
154 struct linked_page *chain; /* the chain */
155 unsigned int used_space; /* total size of objects allocated out
156 * of the current page
157 */
158 gfp_t gfp_mask; /* mask for allocating pages */
159 int safe_needed; /* if set, only "safe" pages are allocated */
160};
161
162static void
163chain_init(struct chain_allocator *ca, gfp_t gfp_mask, int safe_needed)
164{
165 ca->chain = NULL;
166 ca->used_space = LINKED_PAGE_DATA_SIZE;
167 ca->gfp_mask = gfp_mask;
168 ca->safe_needed = safe_needed;
169}
170
171static void *chain_alloc(struct chain_allocator *ca, unsigned int size)
172{
173 void *ret;
174
175 if (LINKED_PAGE_DATA_SIZE - ca->used_space < size) {
176 struct linked_page *lp;
177
Rafael J. Wysocki83573762006-12-06 20:34:18 -0800178 lp = get_image_page(ca->gfp_mask, ca->safe_needed);
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700179 if (!lp)
180 return NULL;
181
182 lp->next = ca->chain;
183 ca->chain = lp;
184 ca->used_space = 0;
185 }
186 ret = ca->chain->data + ca->used_space;
187 ca->used_space += size;
188 return ret;
189}
190
191static void chain_free(struct chain_allocator *ca, int clear_page_nosave)
192{
193 free_list_of_pages(ca->chain, clear_page_nosave);
194 memset(ca, 0, sizeof(struct chain_allocator));
195}
196
197/**
198 * Data types related to memory bitmaps.
199 *
200 * Memory bitmap is a structure consiting of many linked lists of
201 * objects. The main list's elements are of type struct zone_bitmap
202 * and each of them corresonds to one zone. For each zone bitmap
203 * object there is a list of objects of type struct bm_block that
204 * represent each blocks of bit chunks in which information is
205 * stored.
206 *
207 * struct memory_bitmap contains a pointer to the main list of zone
208 * bitmap objects, a struct bm_position used for browsing the bitmap,
209 * and a pointer to the list of pages used for allocating all of the
210 * zone bitmap objects and bitmap block objects.
211 *
212 * NOTE: It has to be possible to lay out the bitmap in memory
213 * using only allocations of order 0. Additionally, the bitmap is
214 * designed to work with arbitrary number of zones (this is over the
215 * top for now, but let's avoid making unnecessary assumptions ;-).
216 *
217 * struct zone_bitmap contains a pointer to a list of bitmap block
218 * objects and a pointer to the bitmap block object that has been
219 * most recently used for setting bits. Additionally, it contains the
220 * pfns that correspond to the start and end of the represented zone.
221 *
222 * struct bm_block contains a pointer to the memory page in which
223 * information is stored (in the form of a block of bit chunks
224 * of type unsigned long each). It also contains the pfns that
225 * correspond to the start and end of the represented memory area and
226 * the number of bit chunks in the block.
227 *
228 * NOTE: Memory bitmaps are used for two types of operations only:
229 * "set a bit" and "find the next bit set". Moreover, the searching
230 * is always carried out after all of the "set a bit" operations
231 * on given bitmap.
232 */
233
234#define BM_END_OF_MAP (~0UL)
235
236#define BM_CHUNKS_PER_BLOCK (PAGE_SIZE / sizeof(long))
237#define BM_BITS_PER_CHUNK (sizeof(long) << 3)
238#define BM_BITS_PER_BLOCK (PAGE_SIZE << 3)
239
240struct bm_block {
241 struct bm_block *next; /* next element of the list */
242 unsigned long start_pfn; /* pfn represented by the first bit */
243 unsigned long end_pfn; /* pfn represented by the last bit plus 1 */
244 unsigned int size; /* number of bit chunks */
245 unsigned long *data; /* chunks of bits representing pages */
246};
247
248struct zone_bitmap {
249 struct zone_bitmap *next; /* next element of the list */
250 unsigned long start_pfn; /* minimal pfn in this zone */
251 unsigned long end_pfn; /* maximal pfn in this zone plus 1 */
252 struct bm_block *bm_blocks; /* list of bitmap blocks */
253 struct bm_block *cur_block; /* recently used bitmap block */
254};
255
256/* strcut bm_position is used for browsing memory bitmaps */
257
258struct bm_position {
259 struct zone_bitmap *zone_bm;
260 struct bm_block *block;
261 int chunk;
262 int bit;
263};
264
265struct memory_bitmap {
266 struct zone_bitmap *zone_bm_list; /* list of zone bitmaps */
267 struct linked_page *p_list; /* list of pages used to store zone
268 * bitmap objects and bitmap block
269 * objects
270 */
271 struct bm_position cur; /* most recently used bit position */
272};
273
274/* Functions that operate on memory bitmaps */
275
276static inline void memory_bm_reset_chunk(struct memory_bitmap *bm)
277{
278 bm->cur.chunk = 0;
279 bm->cur.bit = -1;
280}
281
282static void memory_bm_position_reset(struct memory_bitmap *bm)
283{
284 struct zone_bitmap *zone_bm;
285
286 zone_bm = bm->zone_bm_list;
287 bm->cur.zone_bm = zone_bm;
288 bm->cur.block = zone_bm->bm_blocks;
289 memory_bm_reset_chunk(bm);
290}
291
292static void memory_bm_free(struct memory_bitmap *bm, int clear_nosave_free);
293
294/**
295 * create_bm_block_list - create a list of block bitmap objects
296 */
297
298static inline struct bm_block *
299create_bm_block_list(unsigned int nr_blocks, struct chain_allocator *ca)
300{
301 struct bm_block *bblist = NULL;
302
303 while (nr_blocks-- > 0) {
304 struct bm_block *bb;
305
306 bb = chain_alloc(ca, sizeof(struct bm_block));
307 if (!bb)
308 return NULL;
309
310 bb->next = bblist;
311 bblist = bb;
312 }
313 return bblist;
314}
315
316/**
317 * create_zone_bm_list - create a list of zone bitmap objects
318 */
319
320static inline struct zone_bitmap *
321create_zone_bm_list(unsigned int nr_zones, struct chain_allocator *ca)
322{
323 struct zone_bitmap *zbmlist = NULL;
324
325 while (nr_zones-- > 0) {
326 struct zone_bitmap *zbm;
327
328 zbm = chain_alloc(ca, sizeof(struct zone_bitmap));
329 if (!zbm)
330 return NULL;
331
332 zbm->next = zbmlist;
333 zbmlist = zbm;
334 }
335 return zbmlist;
336}
337
338/**
339 * memory_bm_create - allocate memory for a memory bitmap
340 */
341
342static int
343memory_bm_create(struct memory_bitmap *bm, gfp_t gfp_mask, int safe_needed)
344{
345 struct chain_allocator ca;
346 struct zone *zone;
347 struct zone_bitmap *zone_bm;
348 struct bm_block *bb;
349 unsigned int nr;
350
351 chain_init(&ca, gfp_mask, safe_needed);
352
353 /* Compute the number of zones */
354 nr = 0;
Rafael J. Wysocki83573762006-12-06 20:34:18 -0800355 for_each_zone(zone)
356 if (populated_zone(zone))
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700357 nr++;
358
359 /* Allocate the list of zones bitmap objects */
360 zone_bm = create_zone_bm_list(nr, &ca);
361 bm->zone_bm_list = zone_bm;
362 if (!zone_bm) {
363 chain_free(&ca, PG_UNSAFE_CLEAR);
364 return -ENOMEM;
365 }
366
367 /* Initialize the zone bitmap objects */
Rafael J. Wysocki83573762006-12-06 20:34:18 -0800368 for_each_zone(zone) {
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700369 unsigned long pfn;
370
Rafael J. Wysocki83573762006-12-06 20:34:18 -0800371 if (!populated_zone(zone))
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700372 continue;
373
374 zone_bm->start_pfn = zone->zone_start_pfn;
375 zone_bm->end_pfn = zone->zone_start_pfn + zone->spanned_pages;
376 /* Allocate the list of bitmap block objects */
377 nr = DIV_ROUND_UP(zone->spanned_pages, BM_BITS_PER_BLOCK);
378 bb = create_bm_block_list(nr, &ca);
379 zone_bm->bm_blocks = bb;
380 zone_bm->cur_block = bb;
381 if (!bb)
382 goto Free;
383
384 nr = zone->spanned_pages;
385 pfn = zone->zone_start_pfn;
386 /* Initialize the bitmap block objects */
387 while (bb) {
388 unsigned long *ptr;
389
Rafael J. Wysocki83573762006-12-06 20:34:18 -0800390 ptr = get_image_page(gfp_mask, safe_needed);
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700391 bb->data = ptr;
392 if (!ptr)
393 goto Free;
394
395 bb->start_pfn = pfn;
396 if (nr >= BM_BITS_PER_BLOCK) {
397 pfn += BM_BITS_PER_BLOCK;
398 bb->size = BM_CHUNKS_PER_BLOCK;
399 nr -= BM_BITS_PER_BLOCK;
400 } else {
401 /* This is executed only once in the loop */
402 pfn += nr;
403 bb->size = DIV_ROUND_UP(nr, BM_BITS_PER_CHUNK);
404 }
405 bb->end_pfn = pfn;
406 bb = bb->next;
407 }
408 zone_bm = zone_bm->next;
409 }
410 bm->p_list = ca.chain;
411 memory_bm_position_reset(bm);
412 return 0;
413
Rafael J. Wysocki59a49332006-12-06 20:34:44 -0800414 Free:
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700415 bm->p_list = ca.chain;
416 memory_bm_free(bm, PG_UNSAFE_CLEAR);
417 return -ENOMEM;
418}
419
420/**
421 * memory_bm_free - free memory occupied by the memory bitmap @bm
422 */
423
424static void memory_bm_free(struct memory_bitmap *bm, int clear_nosave_free)
425{
426 struct zone_bitmap *zone_bm;
427
428 /* Free the list of bit blocks for each zone_bitmap object */
429 zone_bm = bm->zone_bm_list;
430 while (zone_bm) {
431 struct bm_block *bb;
432
433 bb = zone_bm->bm_blocks;
434 while (bb) {
435 if (bb->data)
436 free_image_page(bb->data, clear_nosave_free);
437 bb = bb->next;
438 }
439 zone_bm = zone_bm->next;
440 }
441 free_list_of_pages(bm->p_list, clear_nosave_free);
442 bm->zone_bm_list = NULL;
443}
444
445/**
446 * memory_bm_set_bit - set the bit in the bitmap @bm that corresponds
447 * to given pfn. The cur_zone_bm member of @bm and the cur_block member
448 * of @bm->cur_zone_bm are updated.
449 *
450 * If the bit cannot be set, the function returns -EINVAL .
451 */
452
453static int
454memory_bm_set_bit(struct memory_bitmap *bm, unsigned long pfn)
455{
456 struct zone_bitmap *zone_bm;
457 struct bm_block *bb;
458
459 /* Check if the pfn is from the current zone */
460 zone_bm = bm->cur.zone_bm;
461 if (pfn < zone_bm->start_pfn || pfn >= zone_bm->end_pfn) {
462 zone_bm = bm->zone_bm_list;
463 /* We don't assume that the zones are sorted by pfns */
464 while (pfn < zone_bm->start_pfn || pfn >= zone_bm->end_pfn) {
465 zone_bm = zone_bm->next;
466 if (unlikely(!zone_bm))
467 return -EINVAL;
468 }
469 bm->cur.zone_bm = zone_bm;
470 }
471 /* Check if the pfn corresponds to the current bitmap block */
472 bb = zone_bm->cur_block;
473 if (pfn < bb->start_pfn)
474 bb = zone_bm->bm_blocks;
475
476 while (pfn >= bb->end_pfn) {
477 bb = bb->next;
478 if (unlikely(!bb))
479 return -EINVAL;
480 }
481 zone_bm->cur_block = bb;
482 pfn -= bb->start_pfn;
483 set_bit(pfn % BM_BITS_PER_CHUNK, bb->data + pfn / BM_BITS_PER_CHUNK);
484 return 0;
485}
486
487/* Two auxiliary functions for memory_bm_next_pfn */
488
489/* Find the first set bit in the given chunk, if there is one */
490
491static inline int next_bit_in_chunk(int bit, unsigned long *chunk_p)
492{
493 bit++;
494 while (bit < BM_BITS_PER_CHUNK) {
495 if (test_bit(bit, chunk_p))
496 return bit;
497
498 bit++;
499 }
500 return -1;
501}
502
503/* Find a chunk containing some bits set in given block of bits */
504
505static inline int next_chunk_in_block(int n, struct bm_block *bb)
506{
507 n++;
508 while (n < bb->size) {
509 if (bb->data[n])
510 return n;
511
512 n++;
513 }
514 return -1;
515}
516
517/**
518 * memory_bm_next_pfn - find the pfn that corresponds to the next set bit
519 * in the bitmap @bm. If the pfn cannot be found, BM_END_OF_MAP is
520 * returned.
521 *
522 * It is required to run memory_bm_position_reset() before the first call to
523 * this function.
524 */
525
526static unsigned long memory_bm_next_pfn(struct memory_bitmap *bm)
527{
528 struct zone_bitmap *zone_bm;
529 struct bm_block *bb;
530 int chunk;
531 int bit;
532
533 do {
534 bb = bm->cur.block;
535 do {
536 chunk = bm->cur.chunk;
537 bit = bm->cur.bit;
538 do {
539 bit = next_bit_in_chunk(bit, bb->data + chunk);
540 if (bit >= 0)
541 goto Return_pfn;
542
543 chunk = next_chunk_in_block(chunk, bb);
544 bit = -1;
545 } while (chunk >= 0);
546 bb = bb->next;
547 bm->cur.block = bb;
548 memory_bm_reset_chunk(bm);
549 } while (bb);
550 zone_bm = bm->cur.zone_bm->next;
551 if (zone_bm) {
552 bm->cur.zone_bm = zone_bm;
553 bm->cur.block = zone_bm->bm_blocks;
554 memory_bm_reset_chunk(bm);
555 }
556 } while (zone_bm);
557 memory_bm_position_reset(bm);
558 return BM_END_OF_MAP;
559
Rafael J. Wysocki59a49332006-12-06 20:34:44 -0800560 Return_pfn:
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700561 bm->cur.chunk = chunk;
562 bm->cur.bit = bit;
563 return bb->start_pfn + chunk * BM_BITS_PER_CHUNK + bit;
564}
565
566/**
567 * snapshot_additional_pages - estimate the number of additional pages
568 * be needed for setting up the suspend image data structures for given
569 * zone (usually the returned value is greater than the exact number)
570 */
571
572unsigned int snapshot_additional_pages(struct zone *zone)
573{
574 unsigned int res;
575
576 res = DIV_ROUND_UP(zone->spanned_pages, BM_BITS_PER_BLOCK);
577 res += DIV_ROUND_UP(res * sizeof(struct bm_block), PAGE_SIZE);
Rafael J. Wysocki83573762006-12-06 20:34:18 -0800578 return 2 * res;
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700579}
580
Rafael J. Wysocki83573762006-12-06 20:34:18 -0800581#ifdef CONFIG_HIGHMEM
582/**
583 * count_free_highmem_pages - compute the total number of free highmem
584 * pages, system-wide.
585 */
586
587static unsigned int count_free_highmem_pages(void)
588{
589 struct zone *zone;
590 unsigned int cnt = 0;
591
592 for_each_zone(zone)
593 if (populated_zone(zone) && is_highmem(zone))
Christoph Lameterd23ad422007-02-10 01:43:02 -0800594 cnt += zone_page_state(zone, NR_FREE_PAGES);
Rafael J. Wysocki83573762006-12-06 20:34:18 -0800595
596 return cnt;
597}
598
599/**
600 * saveable_highmem_page - Determine whether a highmem page should be
601 * included in the suspend image.
602 *
603 * We should save the page if it isn't Nosave or NosaveFree, or Reserved,
604 * and it isn't a part of a free chunk of pages.
605 */
606
607static struct page *saveable_highmem_page(unsigned long pfn)
608{
609 struct page *page;
610
611 if (!pfn_valid(pfn))
612 return NULL;
613
614 page = pfn_to_page(pfn);
615
616 BUG_ON(!PageHighMem(page));
617
Rafael J. Wysocki7be98232007-05-06 14:50:42 -0700618 if (swsusp_page_is_forbidden(page) || swsusp_page_is_free(page) ||
619 PageReserved(page))
Rafael J. Wysocki83573762006-12-06 20:34:18 -0800620 return NULL;
621
622 return page;
623}
624
625/**
626 * count_highmem_pages - compute the total number of saveable highmem
627 * pages.
628 */
629
630unsigned int count_highmem_pages(void)
631{
632 struct zone *zone;
633 unsigned int n = 0;
634
635 for_each_zone(zone) {
636 unsigned long pfn, max_zone_pfn;
637
638 if (!is_highmem(zone))
639 continue;
640
641 mark_free_pages(zone);
642 max_zone_pfn = zone->zone_start_pfn + zone->spanned_pages;
643 for (pfn = zone->zone_start_pfn; pfn < max_zone_pfn; pfn++)
644 if (saveable_highmem_page(pfn))
645 n++;
646 }
647 return n;
648}
649#else
650static inline void *saveable_highmem_page(unsigned long pfn) { return NULL; }
651static inline unsigned int count_highmem_pages(void) { return 0; }
652#endif /* CONFIG_HIGHMEM */
653
Rafael J. Wysockif6143aa2006-09-25 23:32:50 -0700654/**
Rafael J. Wysocki83573762006-12-06 20:34:18 -0800655 * saveable - Determine whether a non-highmem page should be included in
656 * the suspend image.
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800657 *
Rafael J. Wysocki83573762006-12-06 20:34:18 -0800658 * We should save the page if it isn't Nosave, and is not in the range
659 * of pages statically defined as 'unsaveable', and it isn't a part of
660 * a free chunk of pages.
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800661 */
662
Rafael J. Wysockiae83c5e2006-09-25 23:32:45 -0700663static struct page *saveable_page(unsigned long pfn)
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800664{
Pavel Machekde491862005-10-30 14:59:59 -0800665 struct page *page;
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800666
667 if (!pfn_valid(pfn))
Rafael J. Wysockiae83c5e2006-09-25 23:32:45 -0700668 return NULL;
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800669
670 page = pfn_to_page(pfn);
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800671
Rafael J. Wysocki83573762006-12-06 20:34:18 -0800672 BUG_ON(PageHighMem(page));
673
Rafael J. Wysocki7be98232007-05-06 14:50:42 -0700674 if (swsusp_page_is_forbidden(page) || swsusp_page_is_free(page))
Rafael J. Wysockiae83c5e2006-09-25 23:32:45 -0700675 return NULL;
Rafael J. Wysocki83573762006-12-06 20:34:18 -0800676
Rafael J. Wysockiae83c5e2006-09-25 23:32:45 -0700677 if (PageReserved(page) && pfn_is_nosave(pfn))
678 return NULL;
Rafael J. Wysockiae83c5e2006-09-25 23:32:45 -0700679
680 return page;
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800681}
682
Rafael J. Wysocki83573762006-12-06 20:34:18 -0800683/**
684 * count_data_pages - compute the total number of saveable non-highmem
685 * pages.
686 */
687
Rafael J. Wysocki72a97e02006-01-06 00:13:46 -0800688unsigned int count_data_pages(void)
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800689{
690 struct zone *zone;
Rafael J. Wysockiae83c5e2006-09-25 23:32:45 -0700691 unsigned long pfn, max_zone_pfn;
Pavel Machekdc19d502005-11-07 00:58:40 -0800692 unsigned int n = 0;
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800693
Rafael J. Wysocki83573762006-12-06 20:34:18 -0800694 for_each_zone(zone) {
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800695 if (is_highmem(zone))
696 continue;
Rafael J. Wysocki83573762006-12-06 20:34:18 -0800697
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800698 mark_free_pages(zone);
Rafael J. Wysockiae83c5e2006-09-25 23:32:45 -0700699 max_zone_pfn = zone->zone_start_pfn + zone->spanned_pages;
700 for (pfn = zone->zone_start_pfn; pfn < max_zone_pfn; pfn++)
Rafael J. Wysocki83573762006-12-06 20:34:18 -0800701 if(saveable_page(pfn))
702 n++;
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800703 }
Rafael J. Wysockia0f49652005-10-30 14:59:57 -0800704 return n;
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800705}
706
Rafael J. Wysocki83573762006-12-06 20:34:18 -0800707/* This is needed, because copy_page and memcpy are not usable for copying
708 * task structs.
709 */
710static inline void do_copy_page(long *dst, long *src)
Rafael J. Wysockif623f0d2006-09-25 23:32:49 -0700711{
712 int n;
713
Rafael J. Wysockif623f0d2006-09-25 23:32:49 -0700714 for (n = PAGE_SIZE / sizeof(long); n; n--)
715 *dst++ = *src++;
716}
717
Rafael J. Wysocki83573762006-12-06 20:34:18 -0800718#ifdef CONFIG_HIGHMEM
719static inline struct page *
720page_is_saveable(struct zone *zone, unsigned long pfn)
721{
722 return is_highmem(zone) ?
723 saveable_highmem_page(pfn) : saveable_page(pfn);
724}
725
726static inline void
727copy_data_page(unsigned long dst_pfn, unsigned long src_pfn)
728{
729 struct page *s_page, *d_page;
730 void *src, *dst;
731
732 s_page = pfn_to_page(src_pfn);
733 d_page = pfn_to_page(dst_pfn);
734 if (PageHighMem(s_page)) {
735 src = kmap_atomic(s_page, KM_USER0);
736 dst = kmap_atomic(d_page, KM_USER1);
737 do_copy_page(dst, src);
738 kunmap_atomic(src, KM_USER0);
739 kunmap_atomic(dst, KM_USER1);
740 } else {
741 src = page_address(s_page);
742 if (PageHighMem(d_page)) {
743 /* Page pointed to by src may contain some kernel
744 * data modified by kmap_atomic()
745 */
746 do_copy_page(buffer, src);
747 dst = kmap_atomic(pfn_to_page(dst_pfn), KM_USER0);
748 memcpy(dst, buffer, PAGE_SIZE);
749 kunmap_atomic(dst, KM_USER0);
750 } else {
751 dst = page_address(d_page);
752 do_copy_page(dst, src);
753 }
754 }
755}
756#else
757#define page_is_saveable(zone, pfn) saveable_page(pfn)
758
759static inline void
760copy_data_page(unsigned long dst_pfn, unsigned long src_pfn)
761{
762 do_copy_page(page_address(pfn_to_page(dst_pfn)),
763 page_address(pfn_to_page(src_pfn)));
764}
765#endif /* CONFIG_HIGHMEM */
766
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700767static void
768copy_data_pages(struct memory_bitmap *copy_bm, struct memory_bitmap *orig_bm)
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800769{
770 struct zone *zone;
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700771 unsigned long pfn;
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800772
Rafael J. Wysocki83573762006-12-06 20:34:18 -0800773 for_each_zone(zone) {
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700774 unsigned long max_zone_pfn;
775
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800776 mark_free_pages(zone);
Rafael J. Wysockiae83c5e2006-09-25 23:32:45 -0700777 max_zone_pfn = zone->zone_start_pfn + zone->spanned_pages;
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700778 for (pfn = zone->zone_start_pfn; pfn < max_zone_pfn; pfn++)
Rafael J. Wysocki83573762006-12-06 20:34:18 -0800779 if (page_is_saveable(zone, pfn))
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700780 memory_bm_set_bit(orig_bm, pfn);
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800781 }
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700782 memory_bm_position_reset(orig_bm);
783 memory_bm_position_reset(copy_bm);
784 do {
785 pfn = memory_bm_next_pfn(orig_bm);
Rafael J. Wysocki83573762006-12-06 20:34:18 -0800786 if (likely(pfn != BM_END_OF_MAP))
787 copy_data_page(memory_bm_next_pfn(copy_bm), pfn);
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700788 } while (pfn != BM_END_OF_MAP);
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800789}
790
Rafael J. Wysocki83573762006-12-06 20:34:18 -0800791/* Total number of image pages */
792static unsigned int nr_copy_pages;
793/* Number of pages needed for saving the original pfns of the image pages */
794static unsigned int nr_meta_pages;
795
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800796/**
Rafael J. Wysocki940864d2006-09-25 23:32:55 -0700797 * swsusp_free - free pages allocated for the suspend.
Rafael J. Wysockicd560bb2006-09-25 23:32:50 -0700798 *
Rafael J. Wysocki940864d2006-09-25 23:32:55 -0700799 * Suspend pages are alocated before the atomic copy is made, so we
800 * need to release them after the resume.
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800801 */
802
803void swsusp_free(void)
804{
805 struct zone *zone;
Rafael J. Wysockiae83c5e2006-09-25 23:32:45 -0700806 unsigned long pfn, max_zone_pfn;
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800807
808 for_each_zone(zone) {
Rafael J. Wysockiae83c5e2006-09-25 23:32:45 -0700809 max_zone_pfn = zone->zone_start_pfn + zone->spanned_pages;
810 for (pfn = zone->zone_start_pfn; pfn < max_zone_pfn; pfn++)
811 if (pfn_valid(pfn)) {
812 struct page *page = pfn_to_page(pfn);
813
Rafael J. Wysocki7be98232007-05-06 14:50:42 -0700814 if (swsusp_page_is_forbidden(page) &&
815 swsusp_page_is_free(page)) {
816 swsusp_unset_page_forbidden(page);
817 swsusp_unset_page_free(page);
Rafael J. Wysocki83573762006-12-06 20:34:18 -0800818 __free_page(page);
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800819 }
820 }
821 }
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800822 nr_copy_pages = 0;
823 nr_meta_pages = 0;
Rafael J. Wysocki75534b52006-09-25 23:32:52 -0700824 restore_pblist = NULL;
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -0800825 buffer = NULL;
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800826}
827
Rafael J. Wysocki83573762006-12-06 20:34:18 -0800828#ifdef CONFIG_HIGHMEM
829/**
830 * count_pages_for_highmem - compute the number of non-highmem pages
831 * that will be necessary for creating copies of highmem pages.
832 */
833
834static unsigned int count_pages_for_highmem(unsigned int nr_highmem)
835{
836 unsigned int free_highmem = count_free_highmem_pages();
837
838 if (free_highmem >= nr_highmem)
839 nr_highmem = 0;
840 else
841 nr_highmem -= free_highmem;
842
843 return nr_highmem;
844}
845#else
846static unsigned int
847count_pages_for_highmem(unsigned int nr_highmem) { return 0; }
848#endif /* CONFIG_HIGHMEM */
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800849
850/**
Rafael J. Wysocki83573762006-12-06 20:34:18 -0800851 * enough_free_mem - Make sure we have enough free memory for the
852 * snapshot image.
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800853 */
854
Rafael J. Wysocki83573762006-12-06 20:34:18 -0800855static int enough_free_mem(unsigned int nr_pages, unsigned int nr_highmem)
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800856{
Rafael J. Wysockie5e2fa72006-01-06 00:14:20 -0800857 struct zone *zone;
Rafael J. Wysocki940864d2006-09-25 23:32:55 -0700858 unsigned int free = 0, meta = 0;
Rafael J. Wysockie5e2fa72006-01-06 00:14:20 -0800859
Rafael J. Wysocki83573762006-12-06 20:34:18 -0800860 for_each_zone(zone) {
861 meta += snapshot_additional_pages(zone);
862 if (!is_highmem(zone))
Christoph Lameterd23ad422007-02-10 01:43:02 -0800863 free += zone_page_state(zone, NR_FREE_PAGES);
Rafael J. Wysocki83573762006-12-06 20:34:18 -0800864 }
Rafael J. Wysocki940864d2006-09-25 23:32:55 -0700865
Rafael J. Wysocki83573762006-12-06 20:34:18 -0800866 nr_pages += count_pages_for_highmem(nr_highmem);
867 pr_debug("swsusp: Normal pages needed: %u + %u + %u, available pages: %u\n",
Rafael J. Wysocki940864d2006-09-25 23:32:55 -0700868 nr_pages, PAGES_FOR_IO, meta, free);
869
870 return free > nr_pages + PAGES_FOR_IO + meta;
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800871}
872
Rafael J. Wysocki83573762006-12-06 20:34:18 -0800873#ifdef CONFIG_HIGHMEM
874/**
875 * get_highmem_buffer - if there are some highmem pages in the suspend
876 * image, we may need the buffer to copy them and/or load their data.
877 */
878
879static inline int get_highmem_buffer(int safe_needed)
880{
881 buffer = get_image_page(GFP_ATOMIC | __GFP_COLD, safe_needed);
882 return buffer ? 0 : -ENOMEM;
883}
884
885/**
886 * alloc_highmem_image_pages - allocate some highmem pages for the image.
887 * Try to allocate as many pages as needed, but if the number of free
888 * highmem pages is lesser than that, allocate them all.
889 */
890
891static inline unsigned int
892alloc_highmem_image_pages(struct memory_bitmap *bm, unsigned int nr_highmem)
893{
894 unsigned int to_alloc = count_free_highmem_pages();
895
896 if (to_alloc > nr_highmem)
897 to_alloc = nr_highmem;
898
899 nr_highmem -= to_alloc;
900 while (to_alloc-- > 0) {
901 struct page *page;
902
903 page = alloc_image_page(__GFP_HIGHMEM);
904 memory_bm_set_bit(bm, page_to_pfn(page));
905 }
906 return nr_highmem;
907}
908#else
909static inline int get_highmem_buffer(int safe_needed) { return 0; }
910
911static inline unsigned int
912alloc_highmem_image_pages(struct memory_bitmap *bm, unsigned int n) { return 0; }
913#endif /* CONFIG_HIGHMEM */
914
915/**
916 * swsusp_alloc - allocate memory for the suspend image
917 *
918 * We first try to allocate as many highmem pages as there are
919 * saveable highmem pages in the system. If that fails, we allocate
920 * non-highmem pages for the copies of the remaining highmem ones.
921 *
922 * In this approach it is likely that the copies of highmem pages will
923 * also be located in the high memory, because of the way in which
924 * copy_data_pages() works.
925 */
926
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700927static int
928swsusp_alloc(struct memory_bitmap *orig_bm, struct memory_bitmap *copy_bm,
Rafael J. Wysocki83573762006-12-06 20:34:18 -0800929 unsigned int nr_pages, unsigned int nr_highmem)
Rafael J. Wysocki054bd4c2005-11-08 21:34:39 -0800930{
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700931 int error;
Rafael J. Wysocki054bd4c2005-11-08 21:34:39 -0800932
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700933 error = memory_bm_create(orig_bm, GFP_ATOMIC | __GFP_COLD, PG_ANY);
934 if (error)
935 goto Free;
936
937 error = memory_bm_create(copy_bm, GFP_ATOMIC | __GFP_COLD, PG_ANY);
938 if (error)
939 goto Free;
940
Rafael J. Wysocki83573762006-12-06 20:34:18 -0800941 if (nr_highmem > 0) {
942 error = get_highmem_buffer(PG_ANY);
943 if (error)
944 goto Free;
945
946 nr_pages += alloc_highmem_image_pages(copy_bm, nr_highmem);
947 }
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700948 while (nr_pages-- > 0) {
Rafael J. Wysocki83573762006-12-06 20:34:18 -0800949 struct page *page = alloc_image_page(GFP_ATOMIC | __GFP_COLD);
950
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700951 if (!page)
952 goto Free;
953
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700954 memory_bm_set_bit(copy_bm, page_to_pfn(page));
Rafael J. Wysocki054bd4c2005-11-08 21:34:39 -0800955 }
956 return 0;
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700957
Rafael J. Wysocki59a49332006-12-06 20:34:44 -0800958 Free:
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700959 swsusp_free();
960 return -ENOMEM;
Rafael J. Wysocki054bd4c2005-11-08 21:34:39 -0800961}
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800962
Rafael J. Wysocki83573762006-12-06 20:34:18 -0800963/* Memory bitmap used for marking saveable pages (during suspend) or the
964 * suspend image pages (during resume)
965 */
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700966static struct memory_bitmap orig_bm;
Rafael J. Wysocki83573762006-12-06 20:34:18 -0800967/* Memory bitmap used on suspend for marking allocated pages that will contain
968 * the copies of saveable pages. During resume it is initially used for
969 * marking the suspend image pages, but then its set bits are duplicated in
970 * @orig_bm and it is released. Next, on systems with high memory, it may be
971 * used for marking "safe" highmem pages, but it has to be reinitialized for
972 * this purpose.
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700973 */
974static struct memory_bitmap copy_bm;
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800975
Rafael J. Wysocki2e32a432005-10-30 15:00:00 -0800976asmlinkage int swsusp_save(void)
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800977{
Rafael J. Wysocki83573762006-12-06 20:34:18 -0800978 unsigned int nr_pages, nr_highmem;
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800979
Rafael J. Wysocki83573762006-12-06 20:34:18 -0800980 printk("swsusp: critical section: \n");
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800981
982 drain_local_pages();
Rafael J. Wysockia0f49652005-10-30 14:59:57 -0800983 nr_pages = count_data_pages();
Rafael J. Wysocki83573762006-12-06 20:34:18 -0800984 nr_highmem = count_highmem_pages();
985 printk("swsusp: Need to copy %u pages\n", nr_pages + nr_highmem);
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800986
Rafael J. Wysocki83573762006-12-06 20:34:18 -0800987 if (!enough_free_mem(nr_pages, nr_highmem)) {
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800988 printk(KERN_ERR "swsusp: Not enough free memory\n");
989 return -ENOMEM;
990 }
991
Rafael J. Wysocki83573762006-12-06 20:34:18 -0800992 if (swsusp_alloc(&orig_bm, &copy_bm, nr_pages, nr_highmem)) {
993 printk(KERN_ERR "swsusp: Memory allocation failed\n");
Rafael J. Wysockia0f49652005-10-30 14:59:57 -0800994 return -ENOMEM;
Rafael J. Wysocki83573762006-12-06 20:34:18 -0800995 }
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800996
997 /* During allocating of suspend pagedir, new cold pages may appear.
998 * Kill them.
999 */
1000 drain_local_pages();
Rafael J. Wysockib788db72006-09-25 23:32:54 -07001001 copy_data_pages(&copy_bm, &orig_bm);
Rafael J. Wysocki25761b62005-10-30 14:59:56 -08001002
1003 /*
1004 * End of critical section. From now on, we can write to memory,
1005 * but we should not touch disk. This specially means we must _not_
1006 * touch swap space! Except we must write out our image of course.
1007 */
1008
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001009 nr_pages += nr_highmem;
Rafael J. Wysockia0f49652005-10-30 14:59:57 -08001010 nr_copy_pages = nr_pages;
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001011 nr_meta_pages = DIV_ROUND_UP(nr_pages * sizeof(long), PAGE_SIZE);
Rafael J. Wysockia0f49652005-10-30 14:59:57 -08001012
1013 printk("swsusp: critical section/: done (%d pages copied)\n", nr_pages);
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001014
Rafael J. Wysocki25761b62005-10-30 14:59:56 -08001015 return 0;
1016}
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001017
1018static void init_header(struct swsusp_info *info)
1019{
1020 memset(info, 0, sizeof(struct swsusp_info));
1021 info->version_code = LINUX_VERSION_CODE;
1022 info->num_physpages = num_physpages;
Serge E. Hallyn96b644b2006-10-02 02:18:13 -07001023 memcpy(&info->uts, init_utsname(), sizeof(struct new_utsname));
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001024 info->cpus = num_online_cpus();
1025 info->image_pages = nr_copy_pages;
1026 info->pages = nr_copy_pages + nr_meta_pages + 1;
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -08001027 info->size = info->pages;
1028 info->size <<= PAGE_SHIFT;
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001029}
1030
1031/**
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001032 * pack_pfns - pfns corresponding to the set bits found in the bitmap @bm
1033 * are stored in the array @buf[] (1 page at a time)
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001034 */
1035
Rafael J. Wysockib788db72006-09-25 23:32:54 -07001036static inline void
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001037pack_pfns(unsigned long *buf, struct memory_bitmap *bm)
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001038{
1039 int j;
1040
Rafael J. Wysockib788db72006-09-25 23:32:54 -07001041 for (j = 0; j < PAGE_SIZE / sizeof(long); j++) {
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001042 buf[j] = memory_bm_next_pfn(bm);
1043 if (unlikely(buf[j] == BM_END_OF_MAP))
Rafael J. Wysockib788db72006-09-25 23:32:54 -07001044 break;
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001045 }
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001046}
1047
1048/**
1049 * snapshot_read_next - used for reading the system memory snapshot.
1050 *
1051 * On the first call to it @handle should point to a zeroed
1052 * snapshot_handle structure. The structure gets updated and a pointer
1053 * to it should be passed to this function every next time.
1054 *
1055 * The @count parameter should contain the number of bytes the caller
1056 * wants to read from the snapshot. It must not be zero.
1057 *
1058 * On success the function returns a positive number. Then, the caller
1059 * is allowed to read up to the returned number of bytes from the memory
1060 * location computed by the data_of() macro. The number returned
1061 * may be smaller than @count, but this only happens if the read would
1062 * cross a page boundary otherwise.
1063 *
1064 * The function returns 0 to indicate the end of data stream condition,
1065 * and a negative number is returned on error. In such cases the
1066 * structure pointed to by @handle is not updated and should not be used
1067 * any more.
1068 */
1069
1070int snapshot_read_next(struct snapshot_handle *handle, size_t count)
1071{
Rafael J. Wysockifb13a282006-09-25 23:32:46 -07001072 if (handle->cur > nr_meta_pages + nr_copy_pages)
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001073 return 0;
Rafael J. Wysockib788db72006-09-25 23:32:54 -07001074
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001075 if (!buffer) {
1076 /* This makes the buffer be freed by swsusp_free() */
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001077 buffer = get_image_page(GFP_ATOMIC, PG_ANY);
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001078 if (!buffer)
1079 return -ENOMEM;
1080 }
1081 if (!handle->offset) {
1082 init_header((struct swsusp_info *)buffer);
1083 handle->buffer = buffer;
Rafael J. Wysockib788db72006-09-25 23:32:54 -07001084 memory_bm_position_reset(&orig_bm);
1085 memory_bm_position_reset(&copy_bm);
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001086 }
Rafael J. Wysockifb13a282006-09-25 23:32:46 -07001087 if (handle->prev < handle->cur) {
1088 if (handle->cur <= nr_meta_pages) {
Rafael J. Wysockib788db72006-09-25 23:32:54 -07001089 memset(buffer, 0, PAGE_SIZE);
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001090 pack_pfns(buffer, &orig_bm);
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001091 } else {
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001092 struct page *page;
Rafael J. Wysockib788db72006-09-25 23:32:54 -07001093
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001094 page = pfn_to_page(memory_bm_next_pfn(&copy_bm));
1095 if (PageHighMem(page)) {
1096 /* Highmem pages are copied to the buffer,
1097 * because we can't return with a kmapped
1098 * highmem page (we may not be called again).
1099 */
1100 void *kaddr;
1101
1102 kaddr = kmap_atomic(page, KM_USER0);
1103 memcpy(buffer, kaddr, PAGE_SIZE);
1104 kunmap_atomic(kaddr, KM_USER0);
1105 handle->buffer = buffer;
1106 } else {
1107 handle->buffer = page_address(page);
1108 }
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001109 }
Rafael J. Wysockifb13a282006-09-25 23:32:46 -07001110 handle->prev = handle->cur;
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001111 }
Rafael J. Wysockifb13a282006-09-25 23:32:46 -07001112 handle->buf_offset = handle->cur_offset;
1113 if (handle->cur_offset + count >= PAGE_SIZE) {
1114 count = PAGE_SIZE - handle->cur_offset;
1115 handle->cur_offset = 0;
1116 handle->cur++;
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001117 } else {
Rafael J. Wysockifb13a282006-09-25 23:32:46 -07001118 handle->cur_offset += count;
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001119 }
1120 handle->offset += count;
1121 return count;
1122}
1123
1124/**
1125 * mark_unsafe_pages - mark the pages that cannot be used for storing
1126 * the image during resume, because they conflict with the pages that
1127 * had been used before suspend
1128 */
1129
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001130static int mark_unsafe_pages(struct memory_bitmap *bm)
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001131{
1132 struct zone *zone;
Rafael J. Wysockiae83c5e2006-09-25 23:32:45 -07001133 unsigned long pfn, max_zone_pfn;
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001134
1135 /* Clear page flags */
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001136 for_each_zone(zone) {
Rafael J. Wysockiae83c5e2006-09-25 23:32:45 -07001137 max_zone_pfn = zone->zone_start_pfn + zone->spanned_pages;
1138 for (pfn = zone->zone_start_pfn; pfn < max_zone_pfn; pfn++)
1139 if (pfn_valid(pfn))
Rafael J. Wysocki7be98232007-05-06 14:50:42 -07001140 swsusp_unset_page_free(pfn_to_page(pfn));
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001141 }
1142
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001143 /* Mark pages that correspond to the "original" pfns as "unsafe" */
1144 memory_bm_position_reset(bm);
1145 do {
1146 pfn = memory_bm_next_pfn(bm);
1147 if (likely(pfn != BM_END_OF_MAP)) {
1148 if (likely(pfn_valid(pfn)))
Rafael J. Wysocki7be98232007-05-06 14:50:42 -07001149 swsusp_set_page_free(pfn_to_page(pfn));
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001150 else
1151 return -EFAULT;
1152 }
1153 } while (pfn != BM_END_OF_MAP);
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001154
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001155 allocated_unsafe_pages = 0;
Rafael J. Wysocki968808b2006-06-23 02:04:48 -07001156
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001157 return 0;
1158}
1159
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001160static void
1161duplicate_memory_bitmap(struct memory_bitmap *dst, struct memory_bitmap *src)
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001162{
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001163 unsigned long pfn;
1164
1165 memory_bm_position_reset(src);
1166 pfn = memory_bm_next_pfn(src);
1167 while (pfn != BM_END_OF_MAP) {
1168 memory_bm_set_bit(dst, pfn);
1169 pfn = memory_bm_next_pfn(src);
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001170 }
1171}
1172
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001173static inline int check_header(struct swsusp_info *info)
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001174{
1175 char *reason = NULL;
1176
1177 if (info->version_code != LINUX_VERSION_CODE)
1178 reason = "kernel version";
1179 if (info->num_physpages != num_physpages)
1180 reason = "memory size";
Serge E. Hallyn96b644b2006-10-02 02:18:13 -07001181 if (strcmp(info->uts.sysname,init_utsname()->sysname))
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001182 reason = "system type";
Serge E. Hallyn96b644b2006-10-02 02:18:13 -07001183 if (strcmp(info->uts.release,init_utsname()->release))
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001184 reason = "kernel release";
Serge E. Hallyn96b644b2006-10-02 02:18:13 -07001185 if (strcmp(info->uts.version,init_utsname()->version))
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001186 reason = "version";
Serge E. Hallyn96b644b2006-10-02 02:18:13 -07001187 if (strcmp(info->uts.machine,init_utsname()->machine))
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001188 reason = "machine";
1189 if (reason) {
1190 printk(KERN_ERR "swsusp: Resume mismatch: %s\n", reason);
1191 return -EPERM;
1192 }
1193 return 0;
1194}
1195
1196/**
1197 * load header - check the image header and copy data from it
1198 */
1199
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001200static int
1201load_header(struct swsusp_info *info)
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001202{
1203 int error;
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001204
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001205 restore_pblist = NULL;
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001206 error = check_header(info);
1207 if (!error) {
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001208 nr_copy_pages = info->image_pages;
1209 nr_meta_pages = info->pages - info->image_pages - 1;
1210 }
1211 return error;
1212}
1213
1214/**
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001215 * unpack_orig_pfns - for each element of @buf[] (1 page at a time) set
1216 * the corresponding bit in the memory bitmap @bm
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001217 */
1218
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001219static inline void
1220unpack_orig_pfns(unsigned long *buf, struct memory_bitmap *bm)
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001221{
1222 int j;
1223
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001224 for (j = 0; j < PAGE_SIZE / sizeof(long); j++) {
1225 if (unlikely(buf[j] == BM_END_OF_MAP))
1226 break;
1227
1228 memory_bm_set_bit(bm, buf[j]);
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001229 }
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001230}
1231
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001232/* List of "safe" pages that may be used to store data loaded from the suspend
1233 * image
1234 */
1235static struct linked_page *safe_pages_list;
1236
1237#ifdef CONFIG_HIGHMEM
1238/* struct highmem_pbe is used for creating the list of highmem pages that
1239 * should be restored atomically during the resume from disk, because the page
1240 * frames they have occupied before the suspend are in use.
1241 */
1242struct highmem_pbe {
1243 struct page *copy_page; /* data is here now */
1244 struct page *orig_page; /* data was here before the suspend */
1245 struct highmem_pbe *next;
1246};
1247
1248/* List of highmem PBEs needed for restoring the highmem pages that were
1249 * allocated before the suspend and included in the suspend image, but have
1250 * also been allocated by the "resume" kernel, so their contents cannot be
1251 * written directly to their "original" page frames.
1252 */
1253static struct highmem_pbe *highmem_pblist;
1254
1255/**
1256 * count_highmem_image_pages - compute the number of highmem pages in the
1257 * suspend image. The bits in the memory bitmap @bm that correspond to the
1258 * image pages are assumed to be set.
1259 */
1260
1261static unsigned int count_highmem_image_pages(struct memory_bitmap *bm)
1262{
1263 unsigned long pfn;
1264 unsigned int cnt = 0;
1265
1266 memory_bm_position_reset(bm);
1267 pfn = memory_bm_next_pfn(bm);
1268 while (pfn != BM_END_OF_MAP) {
1269 if (PageHighMem(pfn_to_page(pfn)))
1270 cnt++;
1271
1272 pfn = memory_bm_next_pfn(bm);
1273 }
1274 return cnt;
1275}
1276
1277/**
1278 * prepare_highmem_image - try to allocate as many highmem pages as
1279 * there are highmem image pages (@nr_highmem_p points to the variable
1280 * containing the number of highmem image pages). The pages that are
1281 * "safe" (ie. will not be overwritten when the suspend image is
1282 * restored) have the corresponding bits set in @bm (it must be
1283 * unitialized).
1284 *
1285 * NOTE: This function should not be called if there are no highmem
1286 * image pages.
1287 */
1288
1289static unsigned int safe_highmem_pages;
1290
1291static struct memory_bitmap *safe_highmem_bm;
1292
1293static int
1294prepare_highmem_image(struct memory_bitmap *bm, unsigned int *nr_highmem_p)
1295{
1296 unsigned int to_alloc;
1297
1298 if (memory_bm_create(bm, GFP_ATOMIC, PG_SAFE))
1299 return -ENOMEM;
1300
1301 if (get_highmem_buffer(PG_SAFE))
1302 return -ENOMEM;
1303
1304 to_alloc = count_free_highmem_pages();
1305 if (to_alloc > *nr_highmem_p)
1306 to_alloc = *nr_highmem_p;
1307 else
1308 *nr_highmem_p = to_alloc;
1309
1310 safe_highmem_pages = 0;
1311 while (to_alloc-- > 0) {
1312 struct page *page;
1313
1314 page = alloc_page(__GFP_HIGHMEM);
Rafael J. Wysocki7be98232007-05-06 14:50:42 -07001315 if (!swsusp_page_is_free(page)) {
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001316 /* The page is "safe", set its bit the bitmap */
1317 memory_bm_set_bit(bm, page_to_pfn(page));
1318 safe_highmem_pages++;
1319 }
1320 /* Mark the page as allocated */
Rafael J. Wysocki7be98232007-05-06 14:50:42 -07001321 swsusp_set_page_forbidden(page);
1322 swsusp_set_page_free(page);
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001323 }
1324 memory_bm_position_reset(bm);
1325 safe_highmem_bm = bm;
1326 return 0;
1327}
1328
1329/**
1330 * get_highmem_page_buffer - for given highmem image page find the buffer
1331 * that suspend_write_next() should set for its caller to write to.
1332 *
1333 * If the page is to be saved to its "original" page frame or a copy of
1334 * the page is to be made in the highmem, @buffer is returned. Otherwise,
1335 * the copy of the page is to be made in normal memory, so the address of
1336 * the copy is returned.
1337 *
1338 * If @buffer is returned, the caller of suspend_write_next() will write
1339 * the page's contents to @buffer, so they will have to be copied to the
1340 * right location on the next call to suspend_write_next() and it is done
1341 * with the help of copy_last_highmem_page(). For this purpose, if
1342 * @buffer is returned, @last_highmem page is set to the page to which
1343 * the data will have to be copied from @buffer.
1344 */
1345
1346static struct page *last_highmem_page;
1347
1348static void *
1349get_highmem_page_buffer(struct page *page, struct chain_allocator *ca)
1350{
1351 struct highmem_pbe *pbe;
1352 void *kaddr;
1353
Rafael J. Wysocki7be98232007-05-06 14:50:42 -07001354 if (swsusp_page_is_forbidden(page) && swsusp_page_is_free(page)) {
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001355 /* We have allocated the "original" page frame and we can
1356 * use it directly to store the loaded page.
1357 */
1358 last_highmem_page = page;
1359 return buffer;
1360 }
1361 /* The "original" page frame has not been allocated and we have to
1362 * use a "safe" page frame to store the loaded page.
1363 */
1364 pbe = chain_alloc(ca, sizeof(struct highmem_pbe));
1365 if (!pbe) {
1366 swsusp_free();
1367 return NULL;
1368 }
1369 pbe->orig_page = page;
1370 if (safe_highmem_pages > 0) {
1371 struct page *tmp;
1372
1373 /* Copy of the page will be stored in high memory */
1374 kaddr = buffer;
1375 tmp = pfn_to_page(memory_bm_next_pfn(safe_highmem_bm));
1376 safe_highmem_pages--;
1377 last_highmem_page = tmp;
1378 pbe->copy_page = tmp;
1379 } else {
1380 /* Copy of the page will be stored in normal memory */
1381 kaddr = safe_pages_list;
1382 safe_pages_list = safe_pages_list->next;
1383 pbe->copy_page = virt_to_page(kaddr);
1384 }
1385 pbe->next = highmem_pblist;
1386 highmem_pblist = pbe;
1387 return kaddr;
1388}
1389
1390/**
1391 * copy_last_highmem_page - copy the contents of a highmem image from
1392 * @buffer, where the caller of snapshot_write_next() has place them,
1393 * to the right location represented by @last_highmem_page .
1394 */
1395
1396static void copy_last_highmem_page(void)
1397{
1398 if (last_highmem_page) {
1399 void *dst;
1400
1401 dst = kmap_atomic(last_highmem_page, KM_USER0);
1402 memcpy(dst, buffer, PAGE_SIZE);
1403 kunmap_atomic(dst, KM_USER0);
1404 last_highmem_page = NULL;
1405 }
1406}
1407
1408static inline int last_highmem_page_copied(void)
1409{
1410 return !last_highmem_page;
1411}
1412
1413static inline void free_highmem_data(void)
1414{
1415 if (safe_highmem_bm)
1416 memory_bm_free(safe_highmem_bm, PG_UNSAFE_CLEAR);
1417
1418 if (buffer)
1419 free_image_page(buffer, PG_UNSAFE_CLEAR);
1420}
1421#else
1422static inline int get_safe_write_buffer(void) { return 0; }
1423
1424static unsigned int
1425count_highmem_image_pages(struct memory_bitmap *bm) { return 0; }
1426
1427static inline int
1428prepare_highmem_image(struct memory_bitmap *bm, unsigned int *nr_highmem_p)
1429{
1430 return 0;
1431}
1432
1433static inline void *
1434get_highmem_page_buffer(struct page *page, struct chain_allocator *ca)
1435{
1436 return NULL;
1437}
1438
1439static inline void copy_last_highmem_page(void) {}
1440static inline int last_highmem_page_copied(void) { return 1; }
1441static inline void free_highmem_data(void) {}
1442#endif /* CONFIG_HIGHMEM */
1443
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001444/**
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001445 * prepare_image - use the memory bitmap @bm to mark the pages that will
1446 * be overwritten in the process of restoring the system memory state
1447 * from the suspend image ("unsafe" pages) and allocate memory for the
1448 * image.
Rafael J. Wysocki968808b2006-06-23 02:04:48 -07001449 *
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001450 * The idea is to allocate a new memory bitmap first and then allocate
1451 * as many pages as needed for the image data, but not to assign these
1452 * pages to specific tasks initially. Instead, we just mark them as
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001453 * allocated and create a lists of "safe" pages that will be used
1454 * later. On systems with high memory a list of "safe" highmem pages is
1455 * also created.
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001456 */
1457
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001458#define PBES_PER_LINKED_PAGE (LINKED_PAGE_DATA_SIZE / sizeof(struct pbe))
Rafael J. Wysocki968808b2006-06-23 02:04:48 -07001459
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001460static int
1461prepare_image(struct memory_bitmap *new_bm, struct memory_bitmap *bm)
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001462{
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001463 unsigned int nr_pages, nr_highmem;
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001464 struct linked_page *sp_list, *lp;
1465 int error;
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001466
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001467 /* If there is no highmem, the buffer will not be necessary */
1468 free_image_page(buffer, PG_UNSAFE_CLEAR);
1469 buffer = NULL;
1470
1471 nr_highmem = count_highmem_image_pages(bm);
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001472 error = mark_unsafe_pages(bm);
1473 if (error)
1474 goto Free;
1475
1476 error = memory_bm_create(new_bm, GFP_ATOMIC, PG_SAFE);
1477 if (error)
1478 goto Free;
1479
1480 duplicate_memory_bitmap(new_bm, bm);
1481 memory_bm_free(bm, PG_UNSAFE_KEEP);
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001482 if (nr_highmem > 0) {
1483 error = prepare_highmem_image(bm, &nr_highmem);
1484 if (error)
1485 goto Free;
1486 }
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001487 /* Reserve some safe pages for potential later use.
1488 *
1489 * NOTE: This way we make sure there will be enough safe pages for the
1490 * chain_alloc() in get_buffer(). It is a bit wasteful, but
1491 * nr_copy_pages cannot be greater than 50% of the memory anyway.
1492 */
1493 sp_list = NULL;
1494 /* nr_copy_pages cannot be lesser than allocated_unsafe_pages */
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001495 nr_pages = nr_copy_pages - nr_highmem - allocated_unsafe_pages;
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001496 nr_pages = DIV_ROUND_UP(nr_pages, PBES_PER_LINKED_PAGE);
1497 while (nr_pages > 0) {
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001498 lp = get_image_page(GFP_ATOMIC, PG_SAFE);
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001499 if (!lp) {
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001500 error = -ENOMEM;
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001501 goto Free;
Rafael J. Wysocki968808b2006-06-23 02:04:48 -07001502 }
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001503 lp->next = sp_list;
1504 sp_list = lp;
1505 nr_pages--;
Rafael J. Wysocki968808b2006-06-23 02:04:48 -07001506 }
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001507 /* Preallocate memory for the image */
1508 safe_pages_list = NULL;
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001509 nr_pages = nr_copy_pages - nr_highmem - allocated_unsafe_pages;
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001510 while (nr_pages > 0) {
1511 lp = (struct linked_page *)get_zeroed_page(GFP_ATOMIC);
1512 if (!lp) {
1513 error = -ENOMEM;
1514 goto Free;
1515 }
Rafael J. Wysocki7be98232007-05-06 14:50:42 -07001516 if (!swsusp_page_is_free(virt_to_page(lp))) {
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001517 /* The page is "safe", add it to the list */
1518 lp->next = safe_pages_list;
1519 safe_pages_list = lp;
1520 }
1521 /* Mark the page as allocated */
Rafael J. Wysocki7be98232007-05-06 14:50:42 -07001522 swsusp_set_page_forbidden(virt_to_page(lp));
1523 swsusp_set_page_free(virt_to_page(lp));
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001524 nr_pages--;
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001525 }
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001526 /* Free the reserved safe pages so that chain_alloc() can use them */
1527 while (sp_list) {
1528 lp = sp_list->next;
1529 free_image_page(sp_list, PG_UNSAFE_CLEAR);
1530 sp_list = lp;
1531 }
1532 return 0;
1533
Rafael J. Wysocki59a49332006-12-06 20:34:44 -08001534 Free:
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001535 swsusp_free();
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001536 return error;
1537}
1538
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001539/**
1540 * get_buffer - compute the address that snapshot_write_next() should
1541 * set for its caller to write to.
1542 */
Rafael J. Wysocki968808b2006-06-23 02:04:48 -07001543
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001544static void *get_buffer(struct memory_bitmap *bm, struct chain_allocator *ca)
1545{
1546 struct pbe *pbe;
1547 struct page *page = pfn_to_page(memory_bm_next_pfn(bm));
1548
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001549 if (PageHighMem(page))
1550 return get_highmem_page_buffer(page, ca);
1551
Rafael J. Wysocki7be98232007-05-06 14:50:42 -07001552 if (swsusp_page_is_forbidden(page) && swsusp_page_is_free(page))
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001553 /* We have allocated the "original" page frame and we can
1554 * use it directly to store the loaded page.
Rafael J. Wysocki968808b2006-06-23 02:04:48 -07001555 */
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001556 return page_address(page);
1557
1558 /* The "original" page frame has not been allocated and we have to
1559 * use a "safe" page frame to store the loaded page.
Rafael J. Wysocki968808b2006-06-23 02:04:48 -07001560 */
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001561 pbe = chain_alloc(ca, sizeof(struct pbe));
1562 if (!pbe) {
1563 swsusp_free();
1564 return NULL;
1565 }
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001566 pbe->orig_address = page_address(page);
1567 pbe->address = safe_pages_list;
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001568 safe_pages_list = safe_pages_list->next;
1569 pbe->next = restore_pblist;
1570 restore_pblist = pbe;
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001571 return pbe->address;
Rafael J. Wysocki968808b2006-06-23 02:04:48 -07001572}
1573
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001574/**
1575 * snapshot_write_next - used for writing the system memory snapshot.
1576 *
1577 * On the first call to it @handle should point to a zeroed
1578 * snapshot_handle structure. The structure gets updated and a pointer
1579 * to it should be passed to this function every next time.
1580 *
1581 * The @count parameter should contain the number of bytes the caller
1582 * wants to write to the image. It must not be zero.
1583 *
1584 * On success the function returns a positive number. Then, the caller
1585 * is allowed to write up to the returned number of bytes to the memory
1586 * location computed by the data_of() macro. The number returned
1587 * may be smaller than @count, but this only happens if the write would
1588 * cross a page boundary otherwise.
1589 *
1590 * The function returns 0 to indicate the "end of file" condition,
1591 * and a negative number is returned on error. In such cases the
1592 * structure pointed to by @handle is not updated and should not be used
1593 * any more.
1594 */
1595
1596int snapshot_write_next(struct snapshot_handle *handle, size_t count)
1597{
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001598 static struct chain_allocator ca;
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001599 int error = 0;
1600
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001601 /* Check if we have already loaded the entire image */
Rafael J. Wysockifb13a282006-09-25 23:32:46 -07001602 if (handle->prev && handle->cur > nr_meta_pages + nr_copy_pages)
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001603 return 0;
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001604
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001605 if (handle->offset == 0) {
1606 if (!buffer)
1607 /* This makes the buffer be freed by swsusp_free() */
1608 buffer = get_image_page(GFP_ATOMIC, PG_ANY);
1609
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001610 if (!buffer)
1611 return -ENOMEM;
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001612
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001613 handle->buffer = buffer;
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001614 }
Andrew Morton546e0d22006-09-25 23:32:44 -07001615 handle->sync_read = 1;
Rafael J. Wysockifb13a282006-09-25 23:32:46 -07001616 if (handle->prev < handle->cur) {
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001617 if (handle->prev == 0) {
1618 error = load_header(buffer);
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001619 if (error)
1620 return error;
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001621
1622 error = memory_bm_create(&copy_bm, GFP_ATOMIC, PG_ANY);
1623 if (error)
1624 return error;
1625
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001626 } else if (handle->prev <= nr_meta_pages) {
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001627 unpack_orig_pfns(buffer, &copy_bm);
1628 if (handle->prev == nr_meta_pages) {
1629 error = prepare_image(&orig_bm, &copy_bm);
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001630 if (error)
1631 return error;
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001632
1633 chain_init(&ca, GFP_ATOMIC, PG_SAFE);
1634 memory_bm_position_reset(&orig_bm);
1635 restore_pblist = NULL;
1636 handle->buffer = get_buffer(&orig_bm, &ca);
Andrew Morton546e0d22006-09-25 23:32:44 -07001637 handle->sync_read = 0;
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001638 if (!handle->buffer)
1639 return -ENOMEM;
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001640 }
1641 } else {
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001642 copy_last_highmem_page();
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001643 handle->buffer = get_buffer(&orig_bm, &ca);
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001644 if (handle->buffer != buffer)
1645 handle->sync_read = 0;
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001646 }
Rafael J. Wysockifb13a282006-09-25 23:32:46 -07001647 handle->prev = handle->cur;
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001648 }
Rafael J. Wysockifb13a282006-09-25 23:32:46 -07001649 handle->buf_offset = handle->cur_offset;
1650 if (handle->cur_offset + count >= PAGE_SIZE) {
1651 count = PAGE_SIZE - handle->cur_offset;
1652 handle->cur_offset = 0;
1653 handle->cur++;
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001654 } else {
Rafael J. Wysockifb13a282006-09-25 23:32:46 -07001655 handle->cur_offset += count;
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001656 }
1657 handle->offset += count;
1658 return count;
1659}
1660
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001661/**
1662 * snapshot_write_finalize - must be called after the last call to
1663 * snapshot_write_next() in case the last page in the image happens
1664 * to be a highmem page and its contents should be stored in the
1665 * highmem. Additionally, it releases the memory that will not be
1666 * used any more.
1667 */
1668
1669void snapshot_write_finalize(struct snapshot_handle *handle)
1670{
1671 copy_last_highmem_page();
1672 /* Free only if we have loaded the image entirely */
1673 if (handle->prev && handle->cur > nr_meta_pages + nr_copy_pages) {
1674 memory_bm_free(&orig_bm, PG_UNSAFE_CLEAR);
1675 free_highmem_data();
1676 }
1677}
1678
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001679int snapshot_image_loaded(struct snapshot_handle *handle)
1680{
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001681 return !(!nr_copy_pages || !last_highmem_page_copied() ||
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001682 handle->cur <= nr_meta_pages + nr_copy_pages);
1683}
1684
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001685#ifdef CONFIG_HIGHMEM
1686/* Assumes that @buf is ready and points to a "safe" page */
1687static inline void
1688swap_two_pages_data(struct page *p1, struct page *p2, void *buf)
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001689{
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001690 void *kaddr1, *kaddr2;
1691
1692 kaddr1 = kmap_atomic(p1, KM_USER0);
1693 kaddr2 = kmap_atomic(p2, KM_USER1);
1694 memcpy(buf, kaddr1, PAGE_SIZE);
1695 memcpy(kaddr1, kaddr2, PAGE_SIZE);
1696 memcpy(kaddr2, buf, PAGE_SIZE);
1697 kunmap_atomic(kaddr1, KM_USER0);
1698 kunmap_atomic(kaddr2, KM_USER1);
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001699}
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001700
1701/**
1702 * restore_highmem - for each highmem page that was allocated before
1703 * the suspend and included in the suspend image, and also has been
1704 * allocated by the "resume" kernel swap its current (ie. "before
1705 * resume") contents with the previous (ie. "before suspend") one.
1706 *
1707 * If the resume eventually fails, we can call this function once
1708 * again and restore the "before resume" highmem state.
1709 */
1710
1711int restore_highmem(void)
1712{
1713 struct highmem_pbe *pbe = highmem_pblist;
1714 void *buf;
1715
1716 if (!pbe)
1717 return 0;
1718
1719 buf = get_image_page(GFP_ATOMIC, PG_SAFE);
1720 if (!buf)
1721 return -ENOMEM;
1722
1723 while (pbe) {
1724 swap_two_pages_data(pbe->copy_page, pbe->orig_page, buf);
1725 pbe = pbe->next;
1726 }
1727 free_image_page(buf, PG_UNSAFE_CLEAR);
1728 return 0;
1729}
1730#endif /* CONFIG_HIGHMEM */