blob: 2b1a7bc24c913e29dc772009e339b5682589fdcd [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. Wysocki846705d2008-11-26 18:00:24 -050028#include <linux/list.h>
Rafael J. Wysocki25761b62005-10-30 14:59:56 -080029
30#include <asm/uaccess.h>
31#include <asm/mmu_context.h>
32#include <asm/pgtable.h>
33#include <asm/tlbflush.h>
34#include <asm/io.h>
35
Rafael J. Wysocki25761b62005-10-30 14:59:56 -080036#include "power.h"
37
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -070038static int swsusp_page_is_free(struct page *);
39static void swsusp_set_page_forbidden(struct page *);
40static void swsusp_unset_page_forbidden(struct page *);
41
Rafael J. Wysockife419532009-06-11 23:11:17 +020042/*
43 * Preferred image size in bytes (tunable via /sys/power/image_size).
44 * When it is set to N, swsusp will do its best to ensure the image
45 * size will not exceed N bytes, but if that is impossible, it will
46 * try to create the smallest image possible.
47 */
48unsigned long image_size = 500 * 1024 * 1024;
49
Rafael J. Wysocki83573762006-12-06 20:34:18 -080050/* List of PBEs needed for restoring the pages that were allocated before
51 * the suspend and included in the suspend image, but have also been
52 * allocated by the "resume" kernel, so their contents cannot be written
53 * directly to their "original" page frames.
54 */
Rafael J. Wysocki75534b52006-09-25 23:32:52 -070055struct pbe *restore_pblist;
56
Rafael J. Wysocki83573762006-12-06 20:34:18 -080057/* Pointer to an auxiliary buffer (1 page) */
Rafael J. Wysocki940864d2006-09-25 23:32:55 -070058static void *buffer;
Rafael J. Wysocki7088a5c2006-01-06 00:13:05 -080059
Rafael J. Wysockif6143aa2006-09-25 23:32:50 -070060/**
61 * @safe_needed - on resume, for storing the PBE list and the image,
62 * we can only use memory pages that do not conflict with the pages
Rafael J. Wysocki83573762006-12-06 20:34:18 -080063 * used before suspend. The unsafe pages have PageNosaveFree set
64 * and we count them using unsafe_pages.
Rafael J. Wysockif6143aa2006-09-25 23:32:50 -070065 *
Rafael J. Wysocki83573762006-12-06 20:34:18 -080066 * Each allocated image page is marked as PageNosave and PageNosaveFree
67 * so that swsusp_free() can release it.
Rafael J. Wysockif6143aa2006-09-25 23:32:50 -070068 */
69
Rafael J. Wysocki0bcd8882006-09-25 23:32:52 -070070#define PG_ANY 0
71#define PG_SAFE 1
72#define PG_UNSAFE_CLEAR 1
73#define PG_UNSAFE_KEEP 0
74
Rafael J. Wysocki940864d2006-09-25 23:32:55 -070075static unsigned int allocated_unsafe_pages;
Rafael J. Wysockif6143aa2006-09-25 23:32:50 -070076
Rafael J. Wysocki83573762006-12-06 20:34:18 -080077static void *get_image_page(gfp_t gfp_mask, int safe_needed)
Rafael J. Wysockif6143aa2006-09-25 23:32:50 -070078{
79 void *res;
80
81 res = (void *)get_zeroed_page(gfp_mask);
82 if (safe_needed)
Rafael J. Wysocki7be98232007-05-06 14:50:42 -070083 while (res && swsusp_page_is_free(virt_to_page(res))) {
Rafael J. Wysockif6143aa2006-09-25 23:32:50 -070084 /* The page is unsafe, mark it for swsusp_free() */
Rafael J. Wysocki7be98232007-05-06 14:50:42 -070085 swsusp_set_page_forbidden(virt_to_page(res));
Rafael J. Wysocki940864d2006-09-25 23:32:55 -070086 allocated_unsafe_pages++;
Rafael J. Wysockif6143aa2006-09-25 23:32:50 -070087 res = (void *)get_zeroed_page(gfp_mask);
88 }
89 if (res) {
Rafael J. Wysocki7be98232007-05-06 14:50:42 -070090 swsusp_set_page_forbidden(virt_to_page(res));
91 swsusp_set_page_free(virt_to_page(res));
Rafael J. Wysockif6143aa2006-09-25 23:32:50 -070092 }
93 return res;
94}
95
96unsigned long get_safe_page(gfp_t gfp_mask)
97{
Rafael J. Wysocki83573762006-12-06 20:34:18 -080098 return (unsigned long)get_image_page(gfp_mask, PG_SAFE);
99}
100
Rafael J. Wysocki5b6d15d2006-12-06 20:34:43 -0800101static struct page *alloc_image_page(gfp_t gfp_mask)
102{
Rafael J. Wysocki83573762006-12-06 20:34:18 -0800103 struct page *page;
104
105 page = alloc_page(gfp_mask);
106 if (page) {
Rafael J. Wysocki7be98232007-05-06 14:50:42 -0700107 swsusp_set_page_forbidden(page);
108 swsusp_set_page_free(page);
Rafael J. Wysocki83573762006-12-06 20:34:18 -0800109 }
110 return page;
Rafael J. Wysockif6143aa2006-09-25 23:32:50 -0700111}
112
113/**
114 * free_image_page - free page represented by @addr, allocated with
Rafael J. Wysocki83573762006-12-06 20:34:18 -0800115 * get_image_page (page flags set by it must be cleared)
Rafael J. Wysockif6143aa2006-09-25 23:32:50 -0700116 */
117
118static inline void free_image_page(void *addr, int clear_nosave_free)
119{
Rafael J. Wysocki83573762006-12-06 20:34:18 -0800120 struct page *page;
121
122 BUG_ON(!virt_addr_valid(addr));
123
124 page = virt_to_page(addr);
125
Rafael J. Wysocki7be98232007-05-06 14:50:42 -0700126 swsusp_unset_page_forbidden(page);
Rafael J. Wysockif6143aa2006-09-25 23:32:50 -0700127 if (clear_nosave_free)
Rafael J. Wysocki7be98232007-05-06 14:50:42 -0700128 swsusp_unset_page_free(page);
Rafael J. Wysocki83573762006-12-06 20:34:18 -0800129
130 __free_page(page);
Rafael J. Wysockif6143aa2006-09-25 23:32:50 -0700131}
132
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700133/* struct linked_page is used to build chains of pages */
134
135#define LINKED_PAGE_DATA_SIZE (PAGE_SIZE - sizeof(void *))
136
137struct linked_page {
138 struct linked_page *next;
139 char data[LINKED_PAGE_DATA_SIZE];
140} __attribute__((packed));
141
142static inline void
143free_list_of_pages(struct linked_page *list, int clear_page_nosave)
144{
145 while (list) {
146 struct linked_page *lp = list->next;
147
148 free_image_page(list, clear_page_nosave);
149 list = lp;
150 }
151}
152
153/**
154 * struct chain_allocator is used for allocating small objects out of
155 * a linked list of pages called 'the chain'.
156 *
157 * The chain grows each time when there is no room for a new object in
158 * the current page. The allocated objects cannot be freed individually.
159 * It is only possible to free them all at once, by freeing the entire
160 * chain.
161 *
162 * NOTE: The chain allocator may be inefficient if the allocated objects
163 * are not much smaller than PAGE_SIZE.
164 */
165
166struct chain_allocator {
167 struct linked_page *chain; /* the chain */
168 unsigned int used_space; /* total size of objects allocated out
169 * of the current page
170 */
171 gfp_t gfp_mask; /* mask for allocating pages */
172 int safe_needed; /* if set, only "safe" pages are allocated */
173};
174
175static void
176chain_init(struct chain_allocator *ca, gfp_t gfp_mask, int safe_needed)
177{
178 ca->chain = NULL;
179 ca->used_space = LINKED_PAGE_DATA_SIZE;
180 ca->gfp_mask = gfp_mask;
181 ca->safe_needed = safe_needed;
182}
183
184static void *chain_alloc(struct chain_allocator *ca, unsigned int size)
185{
186 void *ret;
187
188 if (LINKED_PAGE_DATA_SIZE - ca->used_space < size) {
189 struct linked_page *lp;
190
Rafael J. Wysocki83573762006-12-06 20:34:18 -0800191 lp = get_image_page(ca->gfp_mask, ca->safe_needed);
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700192 if (!lp)
193 return NULL;
194
195 lp->next = ca->chain;
196 ca->chain = lp;
197 ca->used_space = 0;
198 }
199 ret = ca->chain->data + ca->used_space;
200 ca->used_space += size;
201 return ret;
202}
203
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700204/**
205 * Data types related to memory bitmaps.
206 *
207 * Memory bitmap is a structure consiting of many linked lists of
208 * objects. The main list's elements are of type struct zone_bitmap
209 * and each of them corresonds to one zone. For each zone bitmap
210 * object there is a list of objects of type struct bm_block that
Akinobu Mita0d833042008-07-23 21:28:38 -0700211 * represent each blocks of bitmap in which information is stored.
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700212 *
213 * struct memory_bitmap contains a pointer to the main list of zone
214 * bitmap objects, a struct bm_position used for browsing the bitmap,
215 * and a pointer to the list of pages used for allocating all of the
216 * zone bitmap objects and bitmap block objects.
217 *
218 * NOTE: It has to be possible to lay out the bitmap in memory
219 * using only allocations of order 0. Additionally, the bitmap is
220 * designed to work with arbitrary number of zones (this is over the
221 * top for now, but let's avoid making unnecessary assumptions ;-).
222 *
223 * struct zone_bitmap contains a pointer to a list of bitmap block
224 * objects and a pointer to the bitmap block object that has been
225 * most recently used for setting bits. Additionally, it contains the
226 * pfns that correspond to the start and end of the represented zone.
227 *
228 * struct bm_block contains a pointer to the memory page in which
Akinobu Mita0d833042008-07-23 21:28:38 -0700229 * information is stored (in the form of a block of bitmap)
230 * It also contains the pfns that correspond to the start and end of
231 * the represented memory area.
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700232 */
233
234#define BM_END_OF_MAP (~0UL)
235
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700236#define BM_BITS_PER_BLOCK (PAGE_SIZE << 3)
237
238struct bm_block {
Rafael J. Wysocki846705d2008-11-26 18:00:24 -0500239 struct list_head hook; /* hook into a list of bitmap blocks */
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700240 unsigned long start_pfn; /* pfn represented by the first bit */
241 unsigned long end_pfn; /* pfn represented by the last bit plus 1 */
Akinobu Mita0d833042008-07-23 21:28:38 -0700242 unsigned long *data; /* bitmap representing pages */
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700243};
244
Akinobu Mita0d833042008-07-23 21:28:38 -0700245static inline unsigned long bm_block_bits(struct bm_block *bb)
246{
247 return bb->end_pfn - bb->start_pfn;
248}
249
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700250/* strcut bm_position is used for browsing memory bitmaps */
251
252struct bm_position {
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700253 struct bm_block *block;
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700254 int bit;
255};
256
257struct memory_bitmap {
Rafael J. Wysocki846705d2008-11-26 18:00:24 -0500258 struct list_head blocks; /* list of bitmap blocks */
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700259 struct linked_page *p_list; /* list of pages used to store zone
260 * bitmap objects and bitmap block
261 * objects
262 */
263 struct bm_position cur; /* most recently used bit position */
264};
265
266/* Functions that operate on memory bitmaps */
267
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700268static void memory_bm_position_reset(struct memory_bitmap *bm)
269{
Rafael J. Wysocki846705d2008-11-26 18:00:24 -0500270 bm->cur.block = list_entry(bm->blocks.next, struct bm_block, hook);
Akinobu Mita0d833042008-07-23 21:28:38 -0700271 bm->cur.bit = 0;
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700272}
273
274static void memory_bm_free(struct memory_bitmap *bm, int clear_nosave_free);
275
276/**
277 * create_bm_block_list - create a list of block bitmap objects
Rafael J. Wysocki846705d2008-11-26 18:00:24 -0500278 * @nr_blocks - number of blocks to allocate
279 * @list - list to put the allocated blocks into
280 * @ca - chain allocator to be used for allocating memory
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700281 */
Rafael J. Wysocki846705d2008-11-26 18:00:24 -0500282static int create_bm_block_list(unsigned long pages,
283 struct list_head *list,
284 struct chain_allocator *ca)
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700285{
Rafael J. Wysocki846705d2008-11-26 18:00:24 -0500286 unsigned int nr_blocks = DIV_ROUND_UP(pages, BM_BITS_PER_BLOCK);
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700287
288 while (nr_blocks-- > 0) {
289 struct bm_block *bb;
290
291 bb = chain_alloc(ca, sizeof(struct bm_block));
292 if (!bb)
Rafael J. Wysocki846705d2008-11-26 18:00:24 -0500293 return -ENOMEM;
294 list_add(&bb->hook, list);
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700295 }
Rafael J. Wysocki846705d2008-11-26 18:00:24 -0500296
297 return 0;
298}
299
300struct mem_extent {
301 struct list_head hook;
302 unsigned long start;
303 unsigned long end;
304};
305
306/**
307 * free_mem_extents - free a list of memory extents
308 * @list - list of extents to empty
309 */
310static void free_mem_extents(struct list_head *list)
311{
312 struct mem_extent *ext, *aux;
313
314 list_for_each_entry_safe(ext, aux, list, hook) {
315 list_del(&ext->hook);
316 kfree(ext);
317 }
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700318}
319
320/**
Rafael J. Wysocki846705d2008-11-26 18:00:24 -0500321 * create_mem_extents - create a list of memory extents representing
322 * contiguous ranges of PFNs
323 * @list - list to put the extents into
324 * @gfp_mask - mask to use for memory allocations
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700325 */
Rafael J. Wysocki846705d2008-11-26 18:00:24 -0500326static int create_mem_extents(struct list_head *list, gfp_t gfp_mask)
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700327{
Rafael J. Wysocki846705d2008-11-26 18:00:24 -0500328 struct zone *zone;
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700329
Rafael J. Wysocki846705d2008-11-26 18:00:24 -0500330 INIT_LIST_HEAD(list);
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700331
KOSAKI Motohiroee99c712009-03-31 15:19:31 -0700332 for_each_populated_zone(zone) {
Rafael J. Wysocki846705d2008-11-26 18:00:24 -0500333 unsigned long zone_start, zone_end;
334 struct mem_extent *ext, *cur, *aux;
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700335
Rafael J. Wysocki846705d2008-11-26 18:00:24 -0500336 zone_start = zone->zone_start_pfn;
337 zone_end = zone->zone_start_pfn + zone->spanned_pages;
338
339 list_for_each_entry(ext, list, hook)
340 if (zone_start <= ext->end)
341 break;
342
343 if (&ext->hook == list || zone_end < ext->start) {
344 /* New extent is necessary */
345 struct mem_extent *new_ext;
346
347 new_ext = kzalloc(sizeof(struct mem_extent), gfp_mask);
348 if (!new_ext) {
349 free_mem_extents(list);
350 return -ENOMEM;
351 }
352 new_ext->start = zone_start;
353 new_ext->end = zone_end;
354 list_add_tail(&new_ext->hook, &ext->hook);
355 continue;
356 }
357
358 /* Merge this zone's range of PFNs with the existing one */
359 if (zone_start < ext->start)
360 ext->start = zone_start;
361 if (zone_end > ext->end)
362 ext->end = zone_end;
363
364 /* More merging may be possible */
365 cur = ext;
366 list_for_each_entry_safe_continue(cur, aux, list, hook) {
367 if (zone_end < cur->start)
368 break;
369 if (zone_end < cur->end)
370 ext->end = cur->end;
371 list_del(&cur->hook);
372 kfree(cur);
373 }
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700374 }
Rafael J. Wysocki846705d2008-11-26 18:00:24 -0500375
376 return 0;
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700377}
378
379/**
380 * memory_bm_create - allocate memory for a memory bitmap
381 */
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700382static int
383memory_bm_create(struct memory_bitmap *bm, gfp_t gfp_mask, int safe_needed)
384{
385 struct chain_allocator ca;
Rafael J. Wysocki846705d2008-11-26 18:00:24 -0500386 struct list_head mem_extents;
387 struct mem_extent *ext;
388 int error;
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700389
390 chain_init(&ca, gfp_mask, safe_needed);
Rafael J. Wysocki846705d2008-11-26 18:00:24 -0500391 INIT_LIST_HEAD(&bm->blocks);
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700392
Rafael J. Wysocki846705d2008-11-26 18:00:24 -0500393 error = create_mem_extents(&mem_extents, gfp_mask);
394 if (error)
395 return error;
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700396
Rafael J. Wysocki846705d2008-11-26 18:00:24 -0500397 list_for_each_entry(ext, &mem_extents, hook) {
398 struct bm_block *bb;
399 unsigned long pfn = ext->start;
400 unsigned long pages = ext->end - ext->start;
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700401
Rafael J. Wysocki846705d2008-11-26 18:00:24 -0500402 bb = list_entry(bm->blocks.prev, struct bm_block, hook);
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700403
Rafael J. Wysocki846705d2008-11-26 18:00:24 -0500404 error = create_bm_block_list(pages, bm->blocks.prev, &ca);
405 if (error)
406 goto Error;
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700407
Rafael J. Wysocki846705d2008-11-26 18:00:24 -0500408 list_for_each_entry_continue(bb, &bm->blocks, hook) {
409 bb->data = get_image_page(gfp_mask, safe_needed);
410 if (!bb->data) {
411 error = -ENOMEM;
412 goto Error;
413 }
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700414
415 bb->start_pfn = pfn;
Rafael J. Wysocki846705d2008-11-26 18:00:24 -0500416 if (pages >= BM_BITS_PER_BLOCK) {
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700417 pfn += BM_BITS_PER_BLOCK;
Rafael J. Wysocki846705d2008-11-26 18:00:24 -0500418 pages -= BM_BITS_PER_BLOCK;
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700419 } else {
420 /* This is executed only once in the loop */
Rafael J. Wysocki846705d2008-11-26 18:00:24 -0500421 pfn += pages;
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700422 }
423 bb->end_pfn = pfn;
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700424 }
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700425 }
Rafael J. Wysocki846705d2008-11-26 18:00:24 -0500426
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700427 bm->p_list = ca.chain;
428 memory_bm_position_reset(bm);
Rafael J. Wysocki846705d2008-11-26 18:00:24 -0500429 Exit:
430 free_mem_extents(&mem_extents);
431 return error;
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700432
Rafael J. Wysocki846705d2008-11-26 18:00:24 -0500433 Error:
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700434 bm->p_list = ca.chain;
435 memory_bm_free(bm, PG_UNSAFE_CLEAR);
Rafael J. Wysocki846705d2008-11-26 18:00:24 -0500436 goto Exit;
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700437}
438
439/**
440 * memory_bm_free - free memory occupied by the memory bitmap @bm
441 */
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700442static void memory_bm_free(struct memory_bitmap *bm, int clear_nosave_free)
443{
Rafael J. Wysocki846705d2008-11-26 18:00:24 -0500444 struct bm_block *bb;
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700445
Rafael J. Wysocki846705d2008-11-26 18:00:24 -0500446 list_for_each_entry(bb, &bm->blocks, hook)
447 if (bb->data)
448 free_image_page(bb->data, clear_nosave_free);
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700449
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700450 free_list_of_pages(bm->p_list, clear_nosave_free);
Rafael J. Wysocki846705d2008-11-26 18:00:24 -0500451
452 INIT_LIST_HEAD(&bm->blocks);
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700453}
454
455/**
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -0700456 * memory_bm_find_bit - find the bit in the bitmap @bm that corresponds
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700457 * to given pfn. The cur_zone_bm member of @bm and the cur_block member
458 * of @bm->cur_zone_bm are updated.
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700459 */
Rafael J. Wysockia82f7112008-03-12 00:34:57 +0100460static int memory_bm_find_bit(struct memory_bitmap *bm, unsigned long pfn,
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -0700461 void **addr, unsigned int *bit_nr)
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700462{
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700463 struct bm_block *bb;
464
Rafael J. Wysocki846705d2008-11-26 18:00:24 -0500465 /*
466 * Check if the pfn corresponds to the current bitmap block and find
467 * the block where it fits if this is not the case.
468 */
469 bb = bm->cur.block;
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700470 if (pfn < bb->start_pfn)
Rafael J. Wysocki846705d2008-11-26 18:00:24 -0500471 list_for_each_entry_continue_reverse(bb, &bm->blocks, hook)
472 if (pfn >= bb->start_pfn)
473 break;
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700474
Rafael J. Wysocki846705d2008-11-26 18:00:24 -0500475 if (pfn >= bb->end_pfn)
476 list_for_each_entry_continue(bb, &bm->blocks, hook)
477 if (pfn >= bb->start_pfn && pfn < bb->end_pfn)
478 break;
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -0700479
Rafael J. Wysocki846705d2008-11-26 18:00:24 -0500480 if (&bb->hook == &bm->blocks)
481 return -EFAULT;
482
483 /* The block has been found */
484 bm->cur.block = bb;
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700485 pfn -= bb->start_pfn;
Rafael J. Wysocki846705d2008-11-26 18:00:24 -0500486 bm->cur.bit = pfn + 1;
Akinobu Mita0d833042008-07-23 21:28:38 -0700487 *bit_nr = pfn;
488 *addr = bb->data;
Rafael J. Wysockia82f7112008-03-12 00:34:57 +0100489 return 0;
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -0700490}
491
492static void memory_bm_set_bit(struct memory_bitmap *bm, unsigned long pfn)
493{
494 void *addr;
495 unsigned int bit;
Rafael J. Wysockia82f7112008-03-12 00:34:57 +0100496 int error;
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -0700497
Rafael J. Wysockia82f7112008-03-12 00:34:57 +0100498 error = memory_bm_find_bit(bm, pfn, &addr, &bit);
499 BUG_ON(error);
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -0700500 set_bit(bit, addr);
501}
502
Rafael J. Wysockia82f7112008-03-12 00:34:57 +0100503static int mem_bm_set_bit_check(struct memory_bitmap *bm, unsigned long pfn)
504{
505 void *addr;
506 unsigned int bit;
507 int error;
508
509 error = memory_bm_find_bit(bm, pfn, &addr, &bit);
510 if (!error)
511 set_bit(bit, addr);
512 return error;
513}
514
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -0700515static void memory_bm_clear_bit(struct memory_bitmap *bm, unsigned long pfn)
516{
517 void *addr;
518 unsigned int bit;
Rafael J. Wysockia82f7112008-03-12 00:34:57 +0100519 int error;
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -0700520
Rafael J. Wysockia82f7112008-03-12 00:34:57 +0100521 error = memory_bm_find_bit(bm, pfn, &addr, &bit);
522 BUG_ON(error);
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -0700523 clear_bit(bit, addr);
524}
525
526static int memory_bm_test_bit(struct memory_bitmap *bm, unsigned long pfn)
527{
528 void *addr;
529 unsigned int bit;
Rafael J. Wysockia82f7112008-03-12 00:34:57 +0100530 int error;
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -0700531
Rafael J. Wysockia82f7112008-03-12 00:34:57 +0100532 error = memory_bm_find_bit(bm, pfn, &addr, &bit);
533 BUG_ON(error);
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -0700534 return test_bit(bit, addr);
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700535}
536
Rafael J. Wysocki69643272008-11-11 21:32:44 +0100537static bool memory_bm_pfn_present(struct memory_bitmap *bm, unsigned long pfn)
538{
539 void *addr;
540 unsigned int bit;
541
542 return !memory_bm_find_bit(bm, pfn, &addr, &bit);
543}
544
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700545/**
546 * memory_bm_next_pfn - find the pfn that corresponds to the next set bit
547 * in the bitmap @bm. If the pfn cannot be found, BM_END_OF_MAP is
548 * returned.
549 *
550 * It is required to run memory_bm_position_reset() before the first call to
551 * this function.
552 */
553
554static unsigned long memory_bm_next_pfn(struct memory_bitmap *bm)
555{
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700556 struct bm_block *bb;
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700557 int bit;
558
Rafael J. Wysocki846705d2008-11-26 18:00:24 -0500559 bb = bm->cur.block;
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700560 do {
Rafael J. Wysocki846705d2008-11-26 18:00:24 -0500561 bit = bm->cur.bit;
562 bit = find_next_bit(bb->data, bm_block_bits(bb), bit);
563 if (bit < bm_block_bits(bb))
564 goto Return_pfn;
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700565
Rafael J. Wysocki846705d2008-11-26 18:00:24 -0500566 bb = list_entry(bb->hook.next, struct bm_block, hook);
567 bm->cur.block = bb;
568 bm->cur.bit = 0;
569 } while (&bb->hook != &bm->blocks);
570
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700571 memory_bm_position_reset(bm);
572 return BM_END_OF_MAP;
573
Rafael J. Wysocki59a493352006-12-06 20:34:44 -0800574 Return_pfn:
Akinobu Mita0d833042008-07-23 21:28:38 -0700575 bm->cur.bit = bit + 1;
576 return bb->start_pfn + bit;
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700577}
578
579/**
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -0700580 * This structure represents a range of page frames the contents of which
581 * should not be saved during the suspend.
582 */
583
584struct nosave_region {
585 struct list_head list;
586 unsigned long start_pfn;
587 unsigned long end_pfn;
588};
589
590static LIST_HEAD(nosave_regions);
591
592/**
593 * register_nosave_region - register a range of page frames the contents
594 * of which should not be saved during the suspend (to be used in the early
595 * initialization code)
596 */
597
598void __init
Johannes Berg940d67f2007-05-08 19:23:49 +1000599__register_nosave_region(unsigned long start_pfn, unsigned long end_pfn,
600 int use_kmalloc)
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -0700601{
602 struct nosave_region *region;
603
604 if (start_pfn >= end_pfn)
605 return;
606
607 if (!list_empty(&nosave_regions)) {
608 /* Try to extend the previous region (they should be sorted) */
609 region = list_entry(nosave_regions.prev,
610 struct nosave_region, list);
611 if (region->end_pfn == start_pfn) {
612 region->end_pfn = end_pfn;
613 goto Report;
614 }
615 }
Johannes Berg940d67f2007-05-08 19:23:49 +1000616 if (use_kmalloc) {
617 /* during init, this shouldn't fail */
618 region = kmalloc(sizeof(struct nosave_region), GFP_KERNEL);
619 BUG_ON(!region);
620 } else
621 /* This allocation cannot fail */
622 region = alloc_bootmem_low(sizeof(struct nosave_region));
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -0700623 region->start_pfn = start_pfn;
624 region->end_pfn = end_pfn;
625 list_add_tail(&region->list, &nosave_regions);
626 Report:
Rafael J. Wysocki23976722007-12-08 02:09:43 +0100627 printk(KERN_INFO "PM: Registered nosave memory: %016lx - %016lx\n",
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -0700628 start_pfn << PAGE_SHIFT, end_pfn << PAGE_SHIFT);
629}
630
631/*
632 * Set bits in this map correspond to the page frames the contents of which
633 * should not be saved during the suspend.
634 */
635static struct memory_bitmap *forbidden_pages_map;
636
637/* Set bits in this map correspond to free page frames. */
638static struct memory_bitmap *free_pages_map;
639
640/*
641 * Each page frame allocated for creating the image is marked by setting the
642 * corresponding bits in forbidden_pages_map and free_pages_map simultaneously
643 */
644
645void swsusp_set_page_free(struct page *page)
646{
647 if (free_pages_map)
648 memory_bm_set_bit(free_pages_map, page_to_pfn(page));
649}
650
651static int swsusp_page_is_free(struct page *page)
652{
653 return free_pages_map ?
654 memory_bm_test_bit(free_pages_map, page_to_pfn(page)) : 0;
655}
656
657void swsusp_unset_page_free(struct page *page)
658{
659 if (free_pages_map)
660 memory_bm_clear_bit(free_pages_map, page_to_pfn(page));
661}
662
663static void swsusp_set_page_forbidden(struct page *page)
664{
665 if (forbidden_pages_map)
666 memory_bm_set_bit(forbidden_pages_map, page_to_pfn(page));
667}
668
669int swsusp_page_is_forbidden(struct page *page)
670{
671 return forbidden_pages_map ?
672 memory_bm_test_bit(forbidden_pages_map, page_to_pfn(page)) : 0;
673}
674
675static void swsusp_unset_page_forbidden(struct page *page)
676{
677 if (forbidden_pages_map)
678 memory_bm_clear_bit(forbidden_pages_map, page_to_pfn(page));
679}
680
681/**
682 * mark_nosave_pages - set bits corresponding to the page frames the
683 * contents of which should not be saved in a given bitmap.
684 */
685
686static void mark_nosave_pages(struct memory_bitmap *bm)
687{
688 struct nosave_region *region;
689
690 if (list_empty(&nosave_regions))
691 return;
692
693 list_for_each_entry(region, &nosave_regions, list) {
694 unsigned long pfn;
695
Rafael J. Wysocki23976722007-12-08 02:09:43 +0100696 pr_debug("PM: Marking nosave pages: %016lx - %016lx\n",
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -0700697 region->start_pfn << PAGE_SHIFT,
698 region->end_pfn << PAGE_SHIFT);
699
700 for (pfn = region->start_pfn; pfn < region->end_pfn; pfn++)
Rafael J. Wysockia82f7112008-03-12 00:34:57 +0100701 if (pfn_valid(pfn)) {
702 /*
703 * It is safe to ignore the result of
704 * mem_bm_set_bit_check() here, since we won't
705 * touch the PFNs for which the error is
706 * returned anyway.
707 */
708 mem_bm_set_bit_check(bm, pfn);
709 }
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -0700710 }
711}
712
713/**
714 * create_basic_memory_bitmaps - create bitmaps needed for marking page
715 * frames that should not be saved and free page frames. The pointers
716 * forbidden_pages_map and free_pages_map are only modified if everything
717 * goes well, because we don't want the bits to be used before both bitmaps
718 * are set up.
719 */
720
721int create_basic_memory_bitmaps(void)
722{
723 struct memory_bitmap *bm1, *bm2;
724 int error = 0;
725
726 BUG_ON(forbidden_pages_map || free_pages_map);
727
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700728 bm1 = kzalloc(sizeof(struct memory_bitmap), GFP_KERNEL);
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -0700729 if (!bm1)
730 return -ENOMEM;
731
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700732 error = memory_bm_create(bm1, GFP_KERNEL, PG_ANY);
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -0700733 if (error)
734 goto Free_first_object;
735
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700736 bm2 = kzalloc(sizeof(struct memory_bitmap), GFP_KERNEL);
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -0700737 if (!bm2)
738 goto Free_first_bitmap;
739
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700740 error = memory_bm_create(bm2, GFP_KERNEL, PG_ANY);
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -0700741 if (error)
742 goto Free_second_object;
743
744 forbidden_pages_map = bm1;
745 free_pages_map = bm2;
746 mark_nosave_pages(forbidden_pages_map);
747
Rafael J. Wysocki23976722007-12-08 02:09:43 +0100748 pr_debug("PM: Basic memory bitmaps created\n");
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -0700749
750 return 0;
751
752 Free_second_object:
753 kfree(bm2);
754 Free_first_bitmap:
755 memory_bm_free(bm1, PG_UNSAFE_CLEAR);
756 Free_first_object:
757 kfree(bm1);
758 return -ENOMEM;
759}
760
761/**
762 * free_basic_memory_bitmaps - free memory bitmaps allocated by
763 * create_basic_memory_bitmaps(). The auxiliary pointers are necessary
764 * so that the bitmaps themselves are not referred to while they are being
765 * freed.
766 */
767
768void free_basic_memory_bitmaps(void)
769{
770 struct memory_bitmap *bm1, *bm2;
771
772 BUG_ON(!(forbidden_pages_map && free_pages_map));
773
774 bm1 = forbidden_pages_map;
775 bm2 = free_pages_map;
776 forbidden_pages_map = NULL;
777 free_pages_map = NULL;
778 memory_bm_free(bm1, PG_UNSAFE_CLEAR);
779 kfree(bm1);
780 memory_bm_free(bm2, PG_UNSAFE_CLEAR);
781 kfree(bm2);
782
Rafael J. Wysocki23976722007-12-08 02:09:43 +0100783 pr_debug("PM: Basic memory bitmaps freed\n");
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -0700784}
785
786/**
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700787 * snapshot_additional_pages - estimate the number of additional pages
788 * be needed for setting up the suspend image data structures for given
789 * zone (usually the returned value is greater than the exact number)
790 */
791
792unsigned int snapshot_additional_pages(struct zone *zone)
793{
794 unsigned int res;
795
796 res = DIV_ROUND_UP(zone->spanned_pages, BM_BITS_PER_BLOCK);
797 res += DIV_ROUND_UP(res * sizeof(struct bm_block), PAGE_SIZE);
Rafael J. Wysocki83573762006-12-06 20:34:18 -0800798 return 2 * res;
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700799}
800
Rafael J. Wysocki83573762006-12-06 20:34:18 -0800801#ifdef CONFIG_HIGHMEM
802/**
803 * count_free_highmem_pages - compute the total number of free highmem
804 * pages, system-wide.
805 */
806
807static unsigned int count_free_highmem_pages(void)
808{
809 struct zone *zone;
810 unsigned int cnt = 0;
811
KOSAKI Motohiroee99c712009-03-31 15:19:31 -0700812 for_each_populated_zone(zone)
813 if (is_highmem(zone))
Christoph Lameterd23ad422007-02-10 01:43:02 -0800814 cnt += zone_page_state(zone, NR_FREE_PAGES);
Rafael J. Wysocki83573762006-12-06 20:34:18 -0800815
816 return cnt;
817}
818
819/**
820 * saveable_highmem_page - Determine whether a highmem page should be
821 * included in the suspend image.
822 *
823 * We should save the page if it isn't Nosave or NosaveFree, or Reserved,
824 * and it isn't a part of a free chunk of pages.
825 */
Rafael J. Wysocki846705d2008-11-26 18:00:24 -0500826static struct page *saveable_highmem_page(struct zone *zone, unsigned long pfn)
Rafael J. Wysocki83573762006-12-06 20:34:18 -0800827{
828 struct page *page;
829
830 if (!pfn_valid(pfn))
831 return NULL;
832
833 page = pfn_to_page(pfn);
Rafael J. Wysocki846705d2008-11-26 18:00:24 -0500834 if (page_zone(page) != zone)
835 return NULL;
Rafael J. Wysocki83573762006-12-06 20:34:18 -0800836
837 BUG_ON(!PageHighMem(page));
838
Rafael J. Wysocki7be98232007-05-06 14:50:42 -0700839 if (swsusp_page_is_forbidden(page) || swsusp_page_is_free(page) ||
840 PageReserved(page))
Rafael J. Wysocki83573762006-12-06 20:34:18 -0800841 return NULL;
842
843 return page;
844}
845
846/**
847 * count_highmem_pages - compute the total number of saveable highmem
848 * pages.
849 */
850
Rafael J. Wysockife419532009-06-11 23:11:17 +0200851static unsigned int count_highmem_pages(void)
Rafael J. Wysocki83573762006-12-06 20:34:18 -0800852{
853 struct zone *zone;
854 unsigned int n = 0;
855
856 for_each_zone(zone) {
857 unsigned long pfn, max_zone_pfn;
858
859 if (!is_highmem(zone))
860 continue;
861
862 mark_free_pages(zone);
863 max_zone_pfn = zone->zone_start_pfn + zone->spanned_pages;
864 for (pfn = zone->zone_start_pfn; pfn < max_zone_pfn; pfn++)
Rafael J. Wysocki846705d2008-11-26 18:00:24 -0500865 if (saveable_highmem_page(zone, pfn))
Rafael J. Wysocki83573762006-12-06 20:34:18 -0800866 n++;
867 }
868 return n;
869}
870#else
Rafael J. Wysocki846705d2008-11-26 18:00:24 -0500871static inline void *saveable_highmem_page(struct zone *z, unsigned long p)
872{
873 return NULL;
874}
Rafael J. Wysocki83573762006-12-06 20:34:18 -0800875#endif /* CONFIG_HIGHMEM */
876
Rafael J. Wysockif6143aa2006-09-25 23:32:50 -0700877/**
Rafael J. Wysocki8a235ef2008-02-20 01:47:44 +0100878 * saveable_page - Determine whether a non-highmem page should be included
879 * in the suspend image.
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800880 *
Rafael J. Wysocki83573762006-12-06 20:34:18 -0800881 * We should save the page if it isn't Nosave, and is not in the range
882 * of pages statically defined as 'unsaveable', and it isn't a part of
883 * a free chunk of pages.
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800884 */
Rafael J. Wysocki846705d2008-11-26 18:00:24 -0500885static struct page *saveable_page(struct zone *zone, unsigned long pfn)
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800886{
Pavel Machekde491862005-10-30 14:59:59 -0800887 struct page *page;
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800888
889 if (!pfn_valid(pfn))
Rafael J. Wysockiae83c5ee2006-09-25 23:32:45 -0700890 return NULL;
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800891
892 page = pfn_to_page(pfn);
Rafael J. Wysocki846705d2008-11-26 18:00:24 -0500893 if (page_zone(page) != zone)
894 return NULL;
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800895
Rafael J. Wysocki83573762006-12-06 20:34:18 -0800896 BUG_ON(PageHighMem(page));
897
Rafael J. Wysocki7be98232007-05-06 14:50:42 -0700898 if (swsusp_page_is_forbidden(page) || swsusp_page_is_free(page))
Rafael J. Wysockiae83c5ee2006-09-25 23:32:45 -0700899 return NULL;
Rafael J. Wysocki83573762006-12-06 20:34:18 -0800900
Rafael J. Wysocki8a235ef2008-02-20 01:47:44 +0100901 if (PageReserved(page)
902 && (!kernel_page_present(page) || pfn_is_nosave(pfn)))
Rafael J. Wysockiae83c5ee2006-09-25 23:32:45 -0700903 return NULL;
Rafael J. Wysockiae83c5ee2006-09-25 23:32:45 -0700904
905 return page;
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800906}
907
Rafael J. Wysocki83573762006-12-06 20:34:18 -0800908/**
909 * count_data_pages - compute the total number of saveable non-highmem
910 * pages.
911 */
912
Rafael J. Wysockife419532009-06-11 23:11:17 +0200913static unsigned int count_data_pages(void)
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800914{
915 struct zone *zone;
Rafael J. Wysockiae83c5ee2006-09-25 23:32:45 -0700916 unsigned long pfn, max_zone_pfn;
Pavel Machekdc19d502005-11-07 00:58:40 -0800917 unsigned int n = 0;
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800918
Rafael J. Wysocki83573762006-12-06 20:34:18 -0800919 for_each_zone(zone) {
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800920 if (is_highmem(zone))
921 continue;
Rafael J. Wysocki83573762006-12-06 20:34:18 -0800922
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800923 mark_free_pages(zone);
Rafael J. Wysockiae83c5ee2006-09-25 23:32:45 -0700924 max_zone_pfn = zone->zone_start_pfn + zone->spanned_pages;
925 for (pfn = zone->zone_start_pfn; pfn < max_zone_pfn; pfn++)
Rafael J. Wysocki846705d2008-11-26 18:00:24 -0500926 if (saveable_page(zone, pfn))
Rafael J. Wysocki83573762006-12-06 20:34:18 -0800927 n++;
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800928 }
Rafael J. Wysockia0f49652005-10-30 14:59:57 -0800929 return n;
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800930}
931
Rafael J. Wysocki83573762006-12-06 20:34:18 -0800932/* This is needed, because copy_page and memcpy are not usable for copying
933 * task structs.
934 */
935static inline void do_copy_page(long *dst, long *src)
Rafael J. Wysockif623f0d2006-09-25 23:32:49 -0700936{
937 int n;
938
Rafael J. Wysockif623f0d2006-09-25 23:32:49 -0700939 for (n = PAGE_SIZE / sizeof(long); n; n--)
940 *dst++ = *src++;
941}
942
Rafael J. Wysocki8a235ef2008-02-20 01:47:44 +0100943
944/**
945 * safe_copy_page - check if the page we are going to copy is marked as
946 * present in the kernel page tables (this always is the case if
947 * CONFIG_DEBUG_PAGEALLOC is not set and in that case
948 * kernel_page_present() always returns 'true').
949 */
950static void safe_copy_page(void *dst, struct page *s_page)
951{
952 if (kernel_page_present(s_page)) {
953 do_copy_page(dst, page_address(s_page));
954 } else {
955 kernel_map_pages(s_page, 1, 1);
956 do_copy_page(dst, page_address(s_page));
957 kernel_map_pages(s_page, 1, 0);
958 }
959}
960
961
Rafael J. Wysocki83573762006-12-06 20:34:18 -0800962#ifdef CONFIG_HIGHMEM
963static inline struct page *
964page_is_saveable(struct zone *zone, unsigned long pfn)
965{
966 return is_highmem(zone) ?
Rafael J. Wysocki846705d2008-11-26 18:00:24 -0500967 saveable_highmem_page(zone, pfn) : saveable_page(zone, pfn);
Rafael J. Wysocki83573762006-12-06 20:34:18 -0800968}
969
Rafael J. Wysocki8a235ef2008-02-20 01:47:44 +0100970static void copy_data_page(unsigned long dst_pfn, unsigned long src_pfn)
Rafael J. Wysocki83573762006-12-06 20:34:18 -0800971{
972 struct page *s_page, *d_page;
973 void *src, *dst;
974
975 s_page = pfn_to_page(src_pfn);
976 d_page = pfn_to_page(dst_pfn);
977 if (PageHighMem(s_page)) {
978 src = kmap_atomic(s_page, KM_USER0);
979 dst = kmap_atomic(d_page, KM_USER1);
980 do_copy_page(dst, src);
981 kunmap_atomic(src, KM_USER0);
982 kunmap_atomic(dst, KM_USER1);
983 } else {
Rafael J. Wysocki83573762006-12-06 20:34:18 -0800984 if (PageHighMem(d_page)) {
985 /* Page pointed to by src may contain some kernel
986 * data modified by kmap_atomic()
987 */
Rafael J. Wysocki8a235ef2008-02-20 01:47:44 +0100988 safe_copy_page(buffer, s_page);
Rafael J. Wysockibaa58352008-12-08 00:52:49 +0100989 dst = kmap_atomic(d_page, KM_USER0);
Rafael J. Wysocki83573762006-12-06 20:34:18 -0800990 memcpy(dst, buffer, PAGE_SIZE);
991 kunmap_atomic(dst, KM_USER0);
992 } else {
Rafael J. Wysocki8a235ef2008-02-20 01:47:44 +0100993 safe_copy_page(page_address(d_page), s_page);
Rafael J. Wysocki83573762006-12-06 20:34:18 -0800994 }
995 }
996}
997#else
Rafael J. Wysocki846705d2008-11-26 18:00:24 -0500998#define page_is_saveable(zone, pfn) saveable_page(zone, pfn)
Rafael J. Wysocki83573762006-12-06 20:34:18 -0800999
Rafael J. Wysocki8a235ef2008-02-20 01:47:44 +01001000static inline void copy_data_page(unsigned long dst_pfn, unsigned long src_pfn)
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001001{
Rafael J. Wysocki8a235ef2008-02-20 01:47:44 +01001002 safe_copy_page(page_address(pfn_to_page(dst_pfn)),
1003 pfn_to_page(src_pfn));
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001004}
1005#endif /* CONFIG_HIGHMEM */
1006
Rafael J. Wysockib788db72006-09-25 23:32:54 -07001007static void
1008copy_data_pages(struct memory_bitmap *copy_bm, struct memory_bitmap *orig_bm)
Rafael J. Wysocki25761b62005-10-30 14:59:56 -08001009{
1010 struct zone *zone;
Rafael J. Wysockib788db72006-09-25 23:32:54 -07001011 unsigned long pfn;
Rafael J. Wysocki25761b62005-10-30 14:59:56 -08001012
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001013 for_each_zone(zone) {
Rafael J. Wysockib788db72006-09-25 23:32:54 -07001014 unsigned long max_zone_pfn;
1015
Rafael J. Wysocki25761b62005-10-30 14:59:56 -08001016 mark_free_pages(zone);
Rafael J. Wysockiae83c5ee2006-09-25 23:32:45 -07001017 max_zone_pfn = zone->zone_start_pfn + zone->spanned_pages;
Rafael J. Wysockib788db72006-09-25 23:32:54 -07001018 for (pfn = zone->zone_start_pfn; pfn < max_zone_pfn; pfn++)
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001019 if (page_is_saveable(zone, pfn))
Rafael J. Wysockib788db72006-09-25 23:32:54 -07001020 memory_bm_set_bit(orig_bm, pfn);
Rafael J. Wysocki25761b62005-10-30 14:59:56 -08001021 }
Rafael J. Wysockib788db72006-09-25 23:32:54 -07001022 memory_bm_position_reset(orig_bm);
1023 memory_bm_position_reset(copy_bm);
Fengguang Wudf7c4872007-10-20 02:26:04 +02001024 for(;;) {
Rafael J. Wysockib788db72006-09-25 23:32:54 -07001025 pfn = memory_bm_next_pfn(orig_bm);
Fengguang Wudf7c4872007-10-20 02:26:04 +02001026 if (unlikely(pfn == BM_END_OF_MAP))
1027 break;
1028 copy_data_page(memory_bm_next_pfn(copy_bm), pfn);
1029 }
Rafael J. Wysocki25761b62005-10-30 14:59:56 -08001030}
1031
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001032/* Total number of image pages */
1033static unsigned int nr_copy_pages;
1034/* Number of pages needed for saving the original pfns of the image pages */
1035static unsigned int nr_meta_pages;
Rafael J. Wysocki64a473c2009-07-08 13:24:05 +02001036/*
1037 * Numbers of normal and highmem page frames allocated for hibernation image
1038 * before suspending devices.
1039 */
1040unsigned int alloc_normal, alloc_highmem;
1041/*
1042 * Memory bitmap used for marking saveable pages (during hibernation) or
1043 * hibernation image pages (during restore)
1044 */
1045static struct memory_bitmap orig_bm;
1046/*
1047 * Memory bitmap used during hibernation for marking allocated page frames that
1048 * will contain copies of saveable pages. During restore it is initially used
1049 * for marking hibernation image pages, but then the set bits from it are
1050 * duplicated in @orig_bm and it is released. On highmem systems it is next
1051 * used for marking "safe" highmem pages, but it has to be reinitialized for
1052 * this purpose.
1053 */
1054static struct memory_bitmap copy_bm;
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001055
Rafael J. Wysocki25761b62005-10-30 14:59:56 -08001056/**
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001057 * swsusp_free - free pages allocated for the suspend.
Rafael J. Wysockicd560bb2006-09-25 23:32:50 -07001058 *
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001059 * Suspend pages are alocated before the atomic copy is made, so we
1060 * need to release them after the resume.
Rafael J. Wysocki25761b62005-10-30 14:59:56 -08001061 */
1062
1063void swsusp_free(void)
1064{
1065 struct zone *zone;
Rafael J. Wysockiae83c5ee2006-09-25 23:32:45 -07001066 unsigned long pfn, max_zone_pfn;
Rafael J. Wysocki25761b62005-10-30 14:59:56 -08001067
1068 for_each_zone(zone) {
Rafael J. Wysockiae83c5ee2006-09-25 23:32:45 -07001069 max_zone_pfn = zone->zone_start_pfn + zone->spanned_pages;
1070 for (pfn = zone->zone_start_pfn; pfn < max_zone_pfn; pfn++)
1071 if (pfn_valid(pfn)) {
1072 struct page *page = pfn_to_page(pfn);
1073
Rafael J. Wysocki7be98232007-05-06 14:50:42 -07001074 if (swsusp_page_is_forbidden(page) &&
1075 swsusp_page_is_free(page)) {
1076 swsusp_unset_page_forbidden(page);
1077 swsusp_unset_page_free(page);
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001078 __free_page(page);
Rafael J. Wysocki25761b62005-10-30 14:59:56 -08001079 }
1080 }
1081 }
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001082 nr_copy_pages = 0;
1083 nr_meta_pages = 0;
Rafael J. Wysocki75534b52006-09-25 23:32:52 -07001084 restore_pblist = NULL;
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -08001085 buffer = NULL;
Rafael J. Wysocki64a473c2009-07-08 13:24:05 +02001086 alloc_normal = 0;
1087 alloc_highmem = 0;
Rafael J. Wysocki25761b62005-10-30 14:59:56 -08001088}
1089
Rafael J. Wysocki4bb33432009-07-08 13:23:51 +02001090/* Helper functions used for the shrinking of memory. */
Rafael J. Wysockife419532009-06-11 23:11:17 +02001091
Rafael J. Wysocki4bb33432009-07-08 13:23:51 +02001092#define GFP_IMAGE (GFP_KERNEL | __GFP_NOWARN)
1093
1094/**
1095 * preallocate_image_pages - Allocate a number of pages for hibernation image
1096 * @nr_pages: Number of page frames to allocate.
1097 * @mask: GFP flags to use for the allocation.
1098 *
1099 * Return value: Number of page frames actually allocated
1100 */
1101static unsigned long preallocate_image_pages(unsigned long nr_pages, gfp_t mask)
Rafael J. Wysockife419532009-06-11 23:11:17 +02001102{
Rafael J. Wysocki4bb33432009-07-08 13:23:51 +02001103 unsigned long nr_alloc = 0;
1104
1105 while (nr_pages > 0) {
Rafael J. Wysocki64a473c2009-07-08 13:24:05 +02001106 struct page *page;
1107
1108 page = alloc_image_page(mask);
1109 if (!page)
Rafael J. Wysocki4bb33432009-07-08 13:23:51 +02001110 break;
Rafael J. Wysocki64a473c2009-07-08 13:24:05 +02001111 memory_bm_set_bit(&copy_bm, page_to_pfn(page));
1112 if (PageHighMem(page))
1113 alloc_highmem++;
1114 else
1115 alloc_normal++;
Rafael J. Wysocki4bb33432009-07-08 13:23:51 +02001116 nr_pages--;
1117 nr_alloc++;
1118 }
1119
1120 return nr_alloc;
Rafael J. Wysockife419532009-06-11 23:11:17 +02001121}
1122
Rafael J. Wysocki4bb33432009-07-08 13:23:51 +02001123static unsigned long preallocate_image_memory(unsigned long nr_pages)
1124{
1125 return preallocate_image_pages(nr_pages, GFP_IMAGE);
1126}
1127
1128#ifdef CONFIG_HIGHMEM
1129static unsigned long preallocate_image_highmem(unsigned long nr_pages)
1130{
1131 return preallocate_image_pages(nr_pages, GFP_IMAGE | __GFP_HIGHMEM);
1132}
1133
1134/**
1135 * __fraction - Compute (an approximation of) x * (multiplier / base)
1136 */
1137static unsigned long __fraction(u64 x, u64 multiplier, u64 base)
1138{
1139 x *= multiplier;
1140 do_div(x, base);
1141 return (unsigned long)x;
1142}
1143
1144static unsigned long preallocate_highmem_fraction(unsigned long nr_pages,
1145 unsigned long highmem,
1146 unsigned long total)
1147{
1148 unsigned long alloc = __fraction(nr_pages, highmem, total);
1149
1150 return preallocate_image_pages(alloc, GFP_IMAGE | __GFP_HIGHMEM);
1151}
1152#else /* CONFIG_HIGHMEM */
1153static inline unsigned long preallocate_image_highmem(unsigned long nr_pages)
1154{
1155 return 0;
1156}
1157
1158static inline unsigned long preallocate_highmem_fraction(unsigned long nr_pages,
1159 unsigned long highmem,
1160 unsigned long total)
1161{
1162 return 0;
1163}
1164#endif /* CONFIG_HIGHMEM */
1165
1166/**
Rafael J. Wysocki64a473c2009-07-08 13:24:05 +02001167 * free_unnecessary_pages - Release preallocated pages not needed for the image
1168 */
1169static void free_unnecessary_pages(void)
1170{
1171 unsigned long save_highmem, to_free_normal, to_free_highmem;
1172
1173 to_free_normal = alloc_normal - count_data_pages();
1174 save_highmem = count_highmem_pages();
1175 if (alloc_highmem > save_highmem) {
1176 to_free_highmem = alloc_highmem - save_highmem;
1177 } else {
1178 to_free_highmem = 0;
1179 to_free_normal -= save_highmem - alloc_highmem;
1180 }
1181
1182 memory_bm_position_reset(&copy_bm);
1183
1184 while (to_free_normal > 0 && to_free_highmem > 0) {
1185 unsigned long pfn = memory_bm_next_pfn(&copy_bm);
1186 struct page *page = pfn_to_page(pfn);
1187
1188 if (PageHighMem(page)) {
1189 if (!to_free_highmem)
1190 continue;
1191 to_free_highmem--;
1192 alloc_highmem--;
1193 } else {
1194 if (!to_free_normal)
1195 continue;
1196 to_free_normal--;
1197 alloc_normal--;
1198 }
1199 memory_bm_clear_bit(&copy_bm, pfn);
1200 swsusp_unset_page_forbidden(page);
1201 swsusp_unset_page_free(page);
1202 __free_page(page);
1203 }
1204}
1205
1206/**
1207 * hibernate_preallocate_memory - Preallocate memory for hibernation image
Rafael J. Wysocki4bb33432009-07-08 13:23:51 +02001208 *
1209 * To create a hibernation image it is necessary to make a copy of every page
1210 * frame in use. We also need a number of page frames to be free during
1211 * hibernation for allocations made while saving the image and for device
1212 * drivers, in case they need to allocate memory from their hibernation
1213 * callbacks (these two numbers are given by PAGES_FOR_IO and SPARE_PAGES,
1214 * respectively, both of which are rough estimates). To make this happen, we
1215 * compute the total number of available page frames and allocate at least
1216 *
1217 * ([page frames total] + PAGES_FOR_IO + [metadata pages]) / 2 + 2 * SPARE_PAGES
1218 *
1219 * of them, which corresponds to the maximum size of a hibernation image.
1220 *
1221 * If image_size is set below the number following from the above formula,
1222 * the preallocation of memory is continued until the total number of saveable
1223 * pages in the system is below the requested image size or it is impossible to
1224 * allocate more memory, whichever happens first.
1225 */
Rafael J. Wysocki64a473c2009-07-08 13:24:05 +02001226int hibernate_preallocate_memory(void)
Rafael J. Wysockife419532009-06-11 23:11:17 +02001227{
Rafael J. Wysockife419532009-06-11 23:11:17 +02001228 struct zone *zone;
Rafael J. Wysocki4bb33432009-07-08 13:23:51 +02001229 unsigned long saveable, size, max_size, count, highmem, pages = 0;
Rafael J. Wysocki64a473c2009-07-08 13:24:05 +02001230 unsigned long alloc, save_highmem, pages_highmem;
Rafael J. Wysockife419532009-06-11 23:11:17 +02001231 struct timeval start, stop;
Rafael J. Wysocki64a473c2009-07-08 13:24:05 +02001232 int error;
Rafael J. Wysockife419532009-06-11 23:11:17 +02001233
Rafael J. Wysocki64a473c2009-07-08 13:24:05 +02001234 printk(KERN_INFO "PM: Preallocating image memory... ");
Rafael J. Wysockife419532009-06-11 23:11:17 +02001235 do_gettimeofday(&start);
Rafael J. Wysockife419532009-06-11 23:11:17 +02001236
Rafael J. Wysocki64a473c2009-07-08 13:24:05 +02001237 error = memory_bm_create(&orig_bm, GFP_IMAGE, PG_ANY);
1238 if (error)
1239 goto err_out;
1240
1241 error = memory_bm_create(&copy_bm, GFP_IMAGE, PG_ANY);
1242 if (error)
1243 goto err_out;
1244
1245 alloc_normal = 0;
1246 alloc_highmem = 0;
1247
Rafael J. Wysocki4bb33432009-07-08 13:23:51 +02001248 /* Count the number of saveable data pages. */
Rafael J. Wysocki64a473c2009-07-08 13:24:05 +02001249 save_highmem = count_highmem_pages();
Rafael J. Wysocki4bb33432009-07-08 13:23:51 +02001250 saveable = count_data_pages();
Rafael J. Wysockife419532009-06-11 23:11:17 +02001251
Rafael J. Wysocki4bb33432009-07-08 13:23:51 +02001252 /*
1253 * Compute the total number of page frames we can use (count) and the
1254 * number of pages needed for image metadata (size).
1255 */
1256 count = saveable;
Rafael J. Wysocki64a473c2009-07-08 13:24:05 +02001257 saveable += save_highmem;
1258 highmem = save_highmem;
Rafael J. Wysocki4bb33432009-07-08 13:23:51 +02001259 size = 0;
1260 for_each_populated_zone(zone) {
1261 size += snapshot_additional_pages(zone);
1262 if (is_highmem(zone))
1263 highmem += zone_page_state(zone, NR_FREE_PAGES);
1264 else
1265 count += zone_page_state(zone, NR_FREE_PAGES);
1266 }
1267 count += highmem;
1268 count -= totalreserve_pages;
Rafael J. Wysockife419532009-06-11 23:11:17 +02001269
Rafael J. Wysocki4bb33432009-07-08 13:23:51 +02001270 /* Compute the maximum number of saveable pages to leave in memory. */
1271 max_size = (count - (size + PAGES_FOR_IO)) / 2 - 2 * SPARE_PAGES;
1272 size = DIV_ROUND_UP(image_size, PAGE_SIZE);
1273 if (size > max_size)
1274 size = max_size;
1275 /*
1276 * If the maximum is not less than the current number of saveable pages
Rafael J. Wysocki64a473c2009-07-08 13:24:05 +02001277 * in memory, allocate page frames for the image and we're done.
Rafael J. Wysocki4bb33432009-07-08 13:23:51 +02001278 */
Rafael J. Wysocki64a473c2009-07-08 13:24:05 +02001279 if (size >= saveable) {
1280 pages = preallocate_image_highmem(save_highmem);
1281 pages += preallocate_image_memory(saveable - pages);
Rafael J. Wysocki4bb33432009-07-08 13:23:51 +02001282 goto out;
Rafael J. Wysocki64a473c2009-07-08 13:24:05 +02001283 }
Rafael J. Wysocki4bb33432009-07-08 13:23:51 +02001284
1285 /*
1286 * Let the memory management subsystem know that we're going to need a
1287 * large number of page frames to allocate and make it free some memory.
1288 * NOTE: If this is not done, performance will be hurt badly in some
1289 * test cases.
1290 */
1291 shrink_all_memory(saveable - size);
1292
1293 /*
1294 * The number of saveable pages in memory was too high, so apply some
1295 * pressure to decrease it. First, make room for the largest possible
1296 * image and fail if that doesn't work. Next, try to decrease the size
1297 * of the image as much as indicated by image_size using allocations
1298 * from highmem and non-highmem zones separately.
1299 */
1300 pages_highmem = preallocate_image_highmem(highmem / 2);
1301 alloc = (count - max_size) - pages_highmem;
1302 pages = preallocate_image_memory(alloc);
Rafael J. Wysocki64a473c2009-07-08 13:24:05 +02001303 if (pages < alloc)
1304 goto err_out;
Rafael J. Wysocki4bb33432009-07-08 13:23:51 +02001305 size = max_size - size;
1306 alloc = size;
1307 size = preallocate_highmem_fraction(size, highmem, count);
1308 pages_highmem += size;
1309 alloc -= size;
1310 pages += preallocate_image_memory(alloc);
1311 pages += pages_highmem;
1312
Rafael J. Wysocki64a473c2009-07-08 13:24:05 +02001313 /*
1314 * We only need as many page frames for the image as there are saveable
1315 * pages in memory, but we have allocated more. Release the excessive
1316 * ones now.
1317 */
1318 free_unnecessary_pages();
Rafael J. Wysocki4bb33432009-07-08 13:23:51 +02001319
1320 out:
Rafael J. Wysockife419532009-06-11 23:11:17 +02001321 do_gettimeofday(&stop);
Rafael J. Wysocki64a473c2009-07-08 13:24:05 +02001322 printk(KERN_CONT "done (allocated %lu pages)\n", pages);
1323 swsusp_show_speed(&start, &stop, pages, "Allocated");
Rafael J. Wysockife419532009-06-11 23:11:17 +02001324
1325 return 0;
Rafael J. Wysocki64a473c2009-07-08 13:24:05 +02001326
1327 err_out:
1328 printk(KERN_CONT "\n");
1329 swsusp_free();
1330 return -ENOMEM;
Rafael J. Wysockife419532009-06-11 23:11:17 +02001331}
1332
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001333#ifdef CONFIG_HIGHMEM
1334/**
1335 * count_pages_for_highmem - compute the number of non-highmem pages
1336 * that will be necessary for creating copies of highmem pages.
1337 */
1338
1339static unsigned int count_pages_for_highmem(unsigned int nr_highmem)
1340{
Rafael J. Wysocki64a473c2009-07-08 13:24:05 +02001341 unsigned int free_highmem = count_free_highmem_pages() + alloc_highmem;
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001342
1343 if (free_highmem >= nr_highmem)
1344 nr_highmem = 0;
1345 else
1346 nr_highmem -= free_highmem;
1347
1348 return nr_highmem;
1349}
1350#else
1351static unsigned int
1352count_pages_for_highmem(unsigned int nr_highmem) { return 0; }
1353#endif /* CONFIG_HIGHMEM */
Rafael J. Wysocki25761b62005-10-30 14:59:56 -08001354
1355/**
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001356 * enough_free_mem - Make sure we have enough free memory for the
1357 * snapshot image.
Rafael J. Wysocki25761b62005-10-30 14:59:56 -08001358 */
1359
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001360static int enough_free_mem(unsigned int nr_pages, unsigned int nr_highmem)
Rafael J. Wysocki25761b62005-10-30 14:59:56 -08001361{
Rafael J. Wysockie5e2fa72006-01-06 00:14:20 -08001362 struct zone *zone;
Rafael J. Wysocki64a473c2009-07-08 13:24:05 +02001363 unsigned int free = alloc_normal;
Rafael J. Wysockie5e2fa72006-01-06 00:14:20 -08001364
Rafael J. Wysocki64a473c2009-07-08 13:24:05 +02001365 for_each_zone(zone)
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001366 if (!is_highmem(zone))
Christoph Lameterd23ad422007-02-10 01:43:02 -08001367 free += zone_page_state(zone, NR_FREE_PAGES);
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001368
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001369 nr_pages += count_pages_for_highmem(nr_highmem);
Rafael J. Wysocki64a473c2009-07-08 13:24:05 +02001370 pr_debug("PM: Normal pages needed: %u + %u, available pages: %u\n",
1371 nr_pages, PAGES_FOR_IO, free);
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001372
Rafael J. Wysocki64a473c2009-07-08 13:24:05 +02001373 return free > nr_pages + PAGES_FOR_IO;
Rafael J. Wysocki25761b62005-10-30 14:59:56 -08001374}
1375
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001376#ifdef CONFIG_HIGHMEM
1377/**
1378 * get_highmem_buffer - if there are some highmem pages in the suspend
1379 * image, we may need the buffer to copy them and/or load their data.
1380 */
1381
1382static inline int get_highmem_buffer(int safe_needed)
1383{
1384 buffer = get_image_page(GFP_ATOMIC | __GFP_COLD, safe_needed);
1385 return buffer ? 0 : -ENOMEM;
1386}
1387
1388/**
1389 * alloc_highmem_image_pages - allocate some highmem pages for the image.
1390 * Try to allocate as many pages as needed, but if the number of free
1391 * highmem pages is lesser than that, allocate them all.
1392 */
1393
1394static inline unsigned int
Rafael J. Wysocki64a473c2009-07-08 13:24:05 +02001395alloc_highmem_pages(struct memory_bitmap *bm, unsigned int nr_highmem)
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001396{
1397 unsigned int to_alloc = count_free_highmem_pages();
1398
1399 if (to_alloc > nr_highmem)
1400 to_alloc = nr_highmem;
1401
1402 nr_highmem -= to_alloc;
1403 while (to_alloc-- > 0) {
1404 struct page *page;
1405
1406 page = alloc_image_page(__GFP_HIGHMEM);
1407 memory_bm_set_bit(bm, page_to_pfn(page));
1408 }
1409 return nr_highmem;
1410}
1411#else
1412static inline int get_highmem_buffer(int safe_needed) { return 0; }
1413
1414static inline unsigned int
Rafael J. Wysocki64a473c2009-07-08 13:24:05 +02001415alloc_highmem_pages(struct memory_bitmap *bm, unsigned int n) { return 0; }
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001416#endif /* CONFIG_HIGHMEM */
1417
1418/**
1419 * swsusp_alloc - allocate memory for the suspend image
1420 *
1421 * We first try to allocate as many highmem pages as there are
1422 * saveable highmem pages in the system. If that fails, we allocate
1423 * non-highmem pages for the copies of the remaining highmem ones.
1424 *
1425 * In this approach it is likely that the copies of highmem pages will
1426 * also be located in the high memory, because of the way in which
1427 * copy_data_pages() works.
1428 */
1429
Rafael J. Wysockib788db72006-09-25 23:32:54 -07001430static int
1431swsusp_alloc(struct memory_bitmap *orig_bm, struct memory_bitmap *copy_bm,
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001432 unsigned int nr_pages, unsigned int nr_highmem)
Rafael J. Wysocki054bd4c2005-11-08 21:34:39 -08001433{
Rafael J. Wysocki64a473c2009-07-08 13:24:05 +02001434 int error = 0;
Rafael J. Wysockib788db72006-09-25 23:32:54 -07001435
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001436 if (nr_highmem > 0) {
1437 error = get_highmem_buffer(PG_ANY);
1438 if (error)
Rafael J. Wysocki64a473c2009-07-08 13:24:05 +02001439 goto err_out;
1440 if (nr_highmem > alloc_highmem) {
1441 nr_highmem -= alloc_highmem;
1442 nr_pages += alloc_highmem_pages(copy_bm, nr_highmem);
1443 }
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001444 }
Rafael J. Wysocki64a473c2009-07-08 13:24:05 +02001445 if (nr_pages > alloc_normal) {
1446 nr_pages -= alloc_normal;
1447 while (nr_pages-- > 0) {
1448 struct page *page;
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001449
Rafael J. Wysocki64a473c2009-07-08 13:24:05 +02001450 page = alloc_image_page(GFP_ATOMIC | __GFP_COLD);
1451 if (!page)
1452 goto err_out;
1453 memory_bm_set_bit(copy_bm, page_to_pfn(page));
1454 }
Rafael J. Wysocki054bd4c2005-11-08 21:34:39 -08001455 }
Rafael J. Wysocki64a473c2009-07-08 13:24:05 +02001456
Rafael J. Wysocki054bd4c2005-11-08 21:34:39 -08001457 return 0;
Rafael J. Wysockib788db72006-09-25 23:32:54 -07001458
Rafael J. Wysocki64a473c2009-07-08 13:24:05 +02001459 err_out:
Rafael J. Wysockib788db72006-09-25 23:32:54 -07001460 swsusp_free();
Rafael J. Wysocki64a473c2009-07-08 13:24:05 +02001461 return error;
Rafael J. Wysocki054bd4c2005-11-08 21:34:39 -08001462}
Rafael J. Wysocki25761b62005-10-30 14:59:56 -08001463
Rafael J. Wysocki2e32a432005-10-30 15:00:00 -08001464asmlinkage int swsusp_save(void)
Rafael J. Wysocki25761b62005-10-30 14:59:56 -08001465{
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001466 unsigned int nr_pages, nr_highmem;
Rafael J. Wysocki25761b62005-10-30 14:59:56 -08001467
Rafael J. Wysocki23976722007-12-08 02:09:43 +01001468 printk(KERN_INFO "PM: Creating hibernation image: \n");
Rafael J. Wysocki25761b62005-10-30 14:59:56 -08001469
Christoph Lameter9f8f2172008-02-04 22:29:11 -08001470 drain_local_pages(NULL);
Rafael J. Wysockia0f49652005-10-30 14:59:57 -08001471 nr_pages = count_data_pages();
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001472 nr_highmem = count_highmem_pages();
Rafael J. Wysocki23976722007-12-08 02:09:43 +01001473 printk(KERN_INFO "PM: Need to copy %u pages\n", nr_pages + nr_highmem);
Rafael J. Wysocki25761b62005-10-30 14:59:56 -08001474
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001475 if (!enough_free_mem(nr_pages, nr_highmem)) {
Rafael J. Wysocki23976722007-12-08 02:09:43 +01001476 printk(KERN_ERR "PM: Not enough free memory\n");
Rafael J. Wysocki25761b62005-10-30 14:59:56 -08001477 return -ENOMEM;
1478 }
1479
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001480 if (swsusp_alloc(&orig_bm, &copy_bm, nr_pages, nr_highmem)) {
Rafael J. Wysocki23976722007-12-08 02:09:43 +01001481 printk(KERN_ERR "PM: Memory allocation failed\n");
Rafael J. Wysockia0f49652005-10-30 14:59:57 -08001482 return -ENOMEM;
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001483 }
Rafael J. Wysocki25761b62005-10-30 14:59:56 -08001484
1485 /* During allocating of suspend pagedir, new cold pages may appear.
1486 * Kill them.
1487 */
Christoph Lameter9f8f2172008-02-04 22:29:11 -08001488 drain_local_pages(NULL);
Rafael J. Wysockib788db72006-09-25 23:32:54 -07001489 copy_data_pages(&copy_bm, &orig_bm);
Rafael J. Wysocki25761b62005-10-30 14:59:56 -08001490
1491 /*
1492 * End of critical section. From now on, we can write to memory,
1493 * but we should not touch disk. This specially means we must _not_
1494 * touch swap space! Except we must write out our image of course.
1495 */
1496
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001497 nr_pages += nr_highmem;
Rafael J. Wysockia0f49652005-10-30 14:59:57 -08001498 nr_copy_pages = nr_pages;
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001499 nr_meta_pages = DIV_ROUND_UP(nr_pages * sizeof(long), PAGE_SIZE);
Rafael J. Wysockia0f49652005-10-30 14:59:57 -08001500
Rafael J. Wysocki23976722007-12-08 02:09:43 +01001501 printk(KERN_INFO "PM: Hibernation image created (%d pages copied)\n",
1502 nr_pages);
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001503
Rafael J. Wysocki25761b62005-10-30 14:59:56 -08001504 return 0;
1505}
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001506
Rafael J. Wysockid307c4a2007-10-18 03:04:52 -07001507#ifndef CONFIG_ARCH_HIBERNATION_HEADER
1508static int init_header_complete(struct swsusp_info *info)
1509{
1510 memcpy(&info->uts, init_utsname(), sizeof(struct new_utsname));
1511 info->version_code = LINUX_VERSION_CODE;
1512 return 0;
1513}
1514
1515static char *check_image_kernel(struct swsusp_info *info)
1516{
1517 if (info->version_code != LINUX_VERSION_CODE)
1518 return "kernel version";
1519 if (strcmp(info->uts.sysname,init_utsname()->sysname))
1520 return "system type";
1521 if (strcmp(info->uts.release,init_utsname()->release))
1522 return "kernel release";
1523 if (strcmp(info->uts.version,init_utsname()->version))
1524 return "version";
1525 if (strcmp(info->uts.machine,init_utsname()->machine))
1526 return "machine";
1527 return NULL;
1528}
1529#endif /* CONFIG_ARCH_HIBERNATION_HEADER */
1530
Rafael J. Wysockiaf508b32007-10-26 00:59:31 +02001531unsigned long snapshot_get_image_size(void)
1532{
1533 return nr_copy_pages + nr_meta_pages + 1;
1534}
1535
Rafael J. Wysockid307c4a2007-10-18 03:04:52 -07001536static int init_header(struct swsusp_info *info)
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001537{
1538 memset(info, 0, sizeof(struct swsusp_info));
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001539 info->num_physpages = num_physpages;
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001540 info->image_pages = nr_copy_pages;
Rafael J. Wysockiaf508b32007-10-26 00:59:31 +02001541 info->pages = snapshot_get_image_size();
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -08001542 info->size = info->pages;
1543 info->size <<= PAGE_SHIFT;
Rafael J. Wysockid307c4a2007-10-18 03:04:52 -07001544 return init_header_complete(info);
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001545}
1546
1547/**
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001548 * pack_pfns - pfns corresponding to the set bits found in the bitmap @bm
1549 * are stored in the array @buf[] (1 page at a time)
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001550 */
1551
Rafael J. Wysockib788db72006-09-25 23:32:54 -07001552static inline void
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001553pack_pfns(unsigned long *buf, struct memory_bitmap *bm)
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001554{
1555 int j;
1556
Rafael J. Wysockib788db72006-09-25 23:32:54 -07001557 for (j = 0; j < PAGE_SIZE / sizeof(long); j++) {
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001558 buf[j] = memory_bm_next_pfn(bm);
1559 if (unlikely(buf[j] == BM_END_OF_MAP))
Rafael J. Wysockib788db72006-09-25 23:32:54 -07001560 break;
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001561 }
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001562}
1563
1564/**
1565 * snapshot_read_next - used for reading the system memory snapshot.
1566 *
1567 * On the first call to it @handle should point to a zeroed
1568 * snapshot_handle structure. The structure gets updated and a pointer
1569 * to it should be passed to this function every next time.
1570 *
1571 * The @count parameter should contain the number of bytes the caller
1572 * wants to read from the snapshot. It must not be zero.
1573 *
1574 * On success the function returns a positive number. Then, the caller
1575 * is allowed to read up to the returned number of bytes from the memory
1576 * location computed by the data_of() macro. The number returned
1577 * may be smaller than @count, but this only happens if the read would
1578 * cross a page boundary otherwise.
1579 *
1580 * The function returns 0 to indicate the end of data stream condition,
1581 * and a negative number is returned on error. In such cases the
1582 * structure pointed to by @handle is not updated and should not be used
1583 * any more.
1584 */
1585
1586int snapshot_read_next(struct snapshot_handle *handle, size_t count)
1587{
Rafael J. Wysockifb13a282006-09-25 23:32:46 -07001588 if (handle->cur > nr_meta_pages + nr_copy_pages)
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001589 return 0;
Rafael J. Wysockib788db72006-09-25 23:32:54 -07001590
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001591 if (!buffer) {
1592 /* This makes the buffer be freed by swsusp_free() */
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001593 buffer = get_image_page(GFP_ATOMIC, PG_ANY);
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001594 if (!buffer)
1595 return -ENOMEM;
1596 }
1597 if (!handle->offset) {
Rafael J. Wysockid307c4a2007-10-18 03:04:52 -07001598 int error;
1599
1600 error = init_header((struct swsusp_info *)buffer);
1601 if (error)
1602 return error;
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001603 handle->buffer = buffer;
Rafael J. Wysockib788db72006-09-25 23:32:54 -07001604 memory_bm_position_reset(&orig_bm);
1605 memory_bm_position_reset(&copy_bm);
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001606 }
Rafael J. Wysockifb13a282006-09-25 23:32:46 -07001607 if (handle->prev < handle->cur) {
1608 if (handle->cur <= nr_meta_pages) {
Rafael J. Wysockib788db72006-09-25 23:32:54 -07001609 memset(buffer, 0, PAGE_SIZE);
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001610 pack_pfns(buffer, &orig_bm);
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001611 } else {
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001612 struct page *page;
Rafael J. Wysockib788db72006-09-25 23:32:54 -07001613
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001614 page = pfn_to_page(memory_bm_next_pfn(&copy_bm));
1615 if (PageHighMem(page)) {
1616 /* Highmem pages are copied to the buffer,
1617 * because we can't return with a kmapped
1618 * highmem page (we may not be called again).
1619 */
1620 void *kaddr;
1621
1622 kaddr = kmap_atomic(page, KM_USER0);
1623 memcpy(buffer, kaddr, PAGE_SIZE);
1624 kunmap_atomic(kaddr, KM_USER0);
1625 handle->buffer = buffer;
1626 } else {
1627 handle->buffer = page_address(page);
1628 }
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001629 }
Rafael J. Wysockifb13a282006-09-25 23:32:46 -07001630 handle->prev = handle->cur;
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001631 }
Rafael J. Wysockifb13a282006-09-25 23:32:46 -07001632 handle->buf_offset = handle->cur_offset;
1633 if (handle->cur_offset + count >= PAGE_SIZE) {
1634 count = PAGE_SIZE - handle->cur_offset;
1635 handle->cur_offset = 0;
1636 handle->cur++;
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001637 } else {
Rafael J. Wysockifb13a282006-09-25 23:32:46 -07001638 handle->cur_offset += count;
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001639 }
1640 handle->offset += count;
1641 return count;
1642}
1643
1644/**
1645 * mark_unsafe_pages - mark the pages that cannot be used for storing
1646 * the image during resume, because they conflict with the pages that
1647 * had been used before suspend
1648 */
1649
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001650static int mark_unsafe_pages(struct memory_bitmap *bm)
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001651{
1652 struct zone *zone;
Rafael J. Wysockiae83c5ee2006-09-25 23:32:45 -07001653 unsigned long pfn, max_zone_pfn;
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001654
1655 /* Clear page flags */
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001656 for_each_zone(zone) {
Rafael J. Wysockiae83c5ee2006-09-25 23:32:45 -07001657 max_zone_pfn = zone->zone_start_pfn + zone->spanned_pages;
1658 for (pfn = zone->zone_start_pfn; pfn < max_zone_pfn; pfn++)
1659 if (pfn_valid(pfn))
Rafael J. Wysocki7be98232007-05-06 14:50:42 -07001660 swsusp_unset_page_free(pfn_to_page(pfn));
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001661 }
1662
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001663 /* Mark pages that correspond to the "original" pfns as "unsafe" */
1664 memory_bm_position_reset(bm);
1665 do {
1666 pfn = memory_bm_next_pfn(bm);
1667 if (likely(pfn != BM_END_OF_MAP)) {
1668 if (likely(pfn_valid(pfn)))
Rafael J. Wysocki7be98232007-05-06 14:50:42 -07001669 swsusp_set_page_free(pfn_to_page(pfn));
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001670 else
1671 return -EFAULT;
1672 }
1673 } while (pfn != BM_END_OF_MAP);
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001674
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001675 allocated_unsafe_pages = 0;
Rafael J. Wysocki968808b82006-06-23 02:04:48 -07001676
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001677 return 0;
1678}
1679
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001680static void
1681duplicate_memory_bitmap(struct memory_bitmap *dst, struct memory_bitmap *src)
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001682{
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001683 unsigned long pfn;
1684
1685 memory_bm_position_reset(src);
1686 pfn = memory_bm_next_pfn(src);
1687 while (pfn != BM_END_OF_MAP) {
1688 memory_bm_set_bit(dst, pfn);
1689 pfn = memory_bm_next_pfn(src);
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001690 }
1691}
1692
Rafael J. Wysockid307c4a2007-10-18 03:04:52 -07001693static int check_header(struct swsusp_info *info)
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001694{
Rafael J. Wysockid307c4a2007-10-18 03:04:52 -07001695 char *reason;
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001696
Rafael J. Wysockid307c4a2007-10-18 03:04:52 -07001697 reason = check_image_kernel(info);
1698 if (!reason && info->num_physpages != num_physpages)
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001699 reason = "memory size";
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001700 if (reason) {
Rafael J. Wysocki23976722007-12-08 02:09:43 +01001701 printk(KERN_ERR "PM: Image mismatch: %s\n", reason);
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001702 return -EPERM;
1703 }
1704 return 0;
1705}
1706
1707/**
1708 * load header - check the image header and copy data from it
1709 */
1710
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001711static int
1712load_header(struct swsusp_info *info)
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001713{
1714 int error;
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001715
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001716 restore_pblist = NULL;
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001717 error = check_header(info);
1718 if (!error) {
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001719 nr_copy_pages = info->image_pages;
1720 nr_meta_pages = info->pages - info->image_pages - 1;
1721 }
1722 return error;
1723}
1724
1725/**
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001726 * unpack_orig_pfns - for each element of @buf[] (1 page at a time) set
1727 * the corresponding bit in the memory bitmap @bm
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001728 */
Rafael J. Wysocki69643272008-11-11 21:32:44 +01001729static int unpack_orig_pfns(unsigned long *buf, struct memory_bitmap *bm)
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001730{
1731 int j;
1732
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001733 for (j = 0; j < PAGE_SIZE / sizeof(long); j++) {
1734 if (unlikely(buf[j] == BM_END_OF_MAP))
1735 break;
1736
Rafael J. Wysocki69643272008-11-11 21:32:44 +01001737 if (memory_bm_pfn_present(bm, buf[j]))
1738 memory_bm_set_bit(bm, buf[j]);
1739 else
1740 return -EFAULT;
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001741 }
Rafael J. Wysocki69643272008-11-11 21:32:44 +01001742
1743 return 0;
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001744}
1745
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001746/* List of "safe" pages that may be used to store data loaded from the suspend
1747 * image
1748 */
1749static struct linked_page *safe_pages_list;
1750
1751#ifdef CONFIG_HIGHMEM
1752/* struct highmem_pbe is used for creating the list of highmem pages that
1753 * should be restored atomically during the resume from disk, because the page
1754 * frames they have occupied before the suspend are in use.
1755 */
1756struct highmem_pbe {
1757 struct page *copy_page; /* data is here now */
1758 struct page *orig_page; /* data was here before the suspend */
1759 struct highmem_pbe *next;
1760};
1761
1762/* List of highmem PBEs needed for restoring the highmem pages that were
1763 * allocated before the suspend and included in the suspend image, but have
1764 * also been allocated by the "resume" kernel, so their contents cannot be
1765 * written directly to their "original" page frames.
1766 */
1767static struct highmem_pbe *highmem_pblist;
1768
1769/**
1770 * count_highmem_image_pages - compute the number of highmem pages in the
1771 * suspend image. The bits in the memory bitmap @bm that correspond to the
1772 * image pages are assumed to be set.
1773 */
1774
1775static unsigned int count_highmem_image_pages(struct memory_bitmap *bm)
1776{
1777 unsigned long pfn;
1778 unsigned int cnt = 0;
1779
1780 memory_bm_position_reset(bm);
1781 pfn = memory_bm_next_pfn(bm);
1782 while (pfn != BM_END_OF_MAP) {
1783 if (PageHighMem(pfn_to_page(pfn)))
1784 cnt++;
1785
1786 pfn = memory_bm_next_pfn(bm);
1787 }
1788 return cnt;
1789}
1790
1791/**
1792 * prepare_highmem_image - try to allocate as many highmem pages as
1793 * there are highmem image pages (@nr_highmem_p points to the variable
1794 * containing the number of highmem image pages). The pages that are
1795 * "safe" (ie. will not be overwritten when the suspend image is
1796 * restored) have the corresponding bits set in @bm (it must be
1797 * unitialized).
1798 *
1799 * NOTE: This function should not be called if there are no highmem
1800 * image pages.
1801 */
1802
1803static unsigned int safe_highmem_pages;
1804
1805static struct memory_bitmap *safe_highmem_bm;
1806
1807static int
1808prepare_highmem_image(struct memory_bitmap *bm, unsigned int *nr_highmem_p)
1809{
1810 unsigned int to_alloc;
1811
1812 if (memory_bm_create(bm, GFP_ATOMIC, PG_SAFE))
1813 return -ENOMEM;
1814
1815 if (get_highmem_buffer(PG_SAFE))
1816 return -ENOMEM;
1817
1818 to_alloc = count_free_highmem_pages();
1819 if (to_alloc > *nr_highmem_p)
1820 to_alloc = *nr_highmem_p;
1821 else
1822 *nr_highmem_p = to_alloc;
1823
1824 safe_highmem_pages = 0;
1825 while (to_alloc-- > 0) {
1826 struct page *page;
1827
1828 page = alloc_page(__GFP_HIGHMEM);
Rafael J. Wysocki7be98232007-05-06 14:50:42 -07001829 if (!swsusp_page_is_free(page)) {
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001830 /* The page is "safe", set its bit the bitmap */
1831 memory_bm_set_bit(bm, page_to_pfn(page));
1832 safe_highmem_pages++;
1833 }
1834 /* Mark the page as allocated */
Rafael J. Wysocki7be98232007-05-06 14:50:42 -07001835 swsusp_set_page_forbidden(page);
1836 swsusp_set_page_free(page);
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001837 }
1838 memory_bm_position_reset(bm);
1839 safe_highmem_bm = bm;
1840 return 0;
1841}
1842
1843/**
1844 * get_highmem_page_buffer - for given highmem image page find the buffer
1845 * that suspend_write_next() should set for its caller to write to.
1846 *
1847 * If the page is to be saved to its "original" page frame or a copy of
1848 * the page is to be made in the highmem, @buffer is returned. Otherwise,
1849 * the copy of the page is to be made in normal memory, so the address of
1850 * the copy is returned.
1851 *
1852 * If @buffer is returned, the caller of suspend_write_next() will write
1853 * the page's contents to @buffer, so they will have to be copied to the
1854 * right location on the next call to suspend_write_next() and it is done
1855 * with the help of copy_last_highmem_page(). For this purpose, if
1856 * @buffer is returned, @last_highmem page is set to the page to which
1857 * the data will have to be copied from @buffer.
1858 */
1859
1860static struct page *last_highmem_page;
1861
1862static void *
1863get_highmem_page_buffer(struct page *page, struct chain_allocator *ca)
1864{
1865 struct highmem_pbe *pbe;
1866 void *kaddr;
1867
Rafael J. Wysocki7be98232007-05-06 14:50:42 -07001868 if (swsusp_page_is_forbidden(page) && swsusp_page_is_free(page)) {
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001869 /* We have allocated the "original" page frame and we can
1870 * use it directly to store the loaded page.
1871 */
1872 last_highmem_page = page;
1873 return buffer;
1874 }
1875 /* The "original" page frame has not been allocated and we have to
1876 * use a "safe" page frame to store the loaded page.
1877 */
1878 pbe = chain_alloc(ca, sizeof(struct highmem_pbe));
1879 if (!pbe) {
1880 swsusp_free();
Rafael J. Wysocki69643272008-11-11 21:32:44 +01001881 return ERR_PTR(-ENOMEM);
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001882 }
1883 pbe->orig_page = page;
1884 if (safe_highmem_pages > 0) {
1885 struct page *tmp;
1886
1887 /* Copy of the page will be stored in high memory */
1888 kaddr = buffer;
1889 tmp = pfn_to_page(memory_bm_next_pfn(safe_highmem_bm));
1890 safe_highmem_pages--;
1891 last_highmem_page = tmp;
1892 pbe->copy_page = tmp;
1893 } else {
1894 /* Copy of the page will be stored in normal memory */
1895 kaddr = safe_pages_list;
1896 safe_pages_list = safe_pages_list->next;
1897 pbe->copy_page = virt_to_page(kaddr);
1898 }
1899 pbe->next = highmem_pblist;
1900 highmem_pblist = pbe;
1901 return kaddr;
1902}
1903
1904/**
1905 * copy_last_highmem_page - copy the contents of a highmem image from
1906 * @buffer, where the caller of snapshot_write_next() has place them,
1907 * to the right location represented by @last_highmem_page .
1908 */
1909
1910static void copy_last_highmem_page(void)
1911{
1912 if (last_highmem_page) {
1913 void *dst;
1914
1915 dst = kmap_atomic(last_highmem_page, KM_USER0);
1916 memcpy(dst, buffer, PAGE_SIZE);
1917 kunmap_atomic(dst, KM_USER0);
1918 last_highmem_page = NULL;
1919 }
1920}
1921
1922static inline int last_highmem_page_copied(void)
1923{
1924 return !last_highmem_page;
1925}
1926
1927static inline void free_highmem_data(void)
1928{
1929 if (safe_highmem_bm)
1930 memory_bm_free(safe_highmem_bm, PG_UNSAFE_CLEAR);
1931
1932 if (buffer)
1933 free_image_page(buffer, PG_UNSAFE_CLEAR);
1934}
1935#else
1936static inline int get_safe_write_buffer(void) { return 0; }
1937
1938static unsigned int
1939count_highmem_image_pages(struct memory_bitmap *bm) { return 0; }
1940
1941static inline int
1942prepare_highmem_image(struct memory_bitmap *bm, unsigned int *nr_highmem_p)
1943{
1944 return 0;
1945}
1946
1947static inline void *
1948get_highmem_page_buffer(struct page *page, struct chain_allocator *ca)
1949{
Rafael J. Wysocki69643272008-11-11 21:32:44 +01001950 return ERR_PTR(-EINVAL);
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001951}
1952
1953static inline void copy_last_highmem_page(void) {}
1954static inline int last_highmem_page_copied(void) { return 1; }
1955static inline void free_highmem_data(void) {}
1956#endif /* CONFIG_HIGHMEM */
1957
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001958/**
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001959 * prepare_image - use the memory bitmap @bm to mark the pages that will
1960 * be overwritten in the process of restoring the system memory state
1961 * from the suspend image ("unsafe" pages) and allocate memory for the
1962 * image.
Rafael J. Wysocki968808b82006-06-23 02:04:48 -07001963 *
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001964 * The idea is to allocate a new memory bitmap first and then allocate
1965 * as many pages as needed for the image data, but not to assign these
1966 * pages to specific tasks initially. Instead, we just mark them as
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001967 * allocated and create a lists of "safe" pages that will be used
1968 * later. On systems with high memory a list of "safe" highmem pages is
1969 * also created.
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001970 */
1971
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001972#define PBES_PER_LINKED_PAGE (LINKED_PAGE_DATA_SIZE / sizeof(struct pbe))
Rafael J. Wysocki968808b82006-06-23 02:04:48 -07001973
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001974static int
1975prepare_image(struct memory_bitmap *new_bm, struct memory_bitmap *bm)
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001976{
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001977 unsigned int nr_pages, nr_highmem;
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001978 struct linked_page *sp_list, *lp;
1979 int error;
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001980
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001981 /* If there is no highmem, the buffer will not be necessary */
1982 free_image_page(buffer, PG_UNSAFE_CLEAR);
1983 buffer = NULL;
1984
1985 nr_highmem = count_highmem_image_pages(bm);
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001986 error = mark_unsafe_pages(bm);
1987 if (error)
1988 goto Free;
1989
1990 error = memory_bm_create(new_bm, GFP_ATOMIC, PG_SAFE);
1991 if (error)
1992 goto Free;
1993
1994 duplicate_memory_bitmap(new_bm, bm);
1995 memory_bm_free(bm, PG_UNSAFE_KEEP);
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001996 if (nr_highmem > 0) {
1997 error = prepare_highmem_image(bm, &nr_highmem);
1998 if (error)
1999 goto Free;
2000 }
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07002001 /* Reserve some safe pages for potential later use.
2002 *
2003 * NOTE: This way we make sure there will be enough safe pages for the
2004 * chain_alloc() in get_buffer(). It is a bit wasteful, but
2005 * nr_copy_pages cannot be greater than 50% of the memory anyway.
2006 */
2007 sp_list = NULL;
2008 /* nr_copy_pages cannot be lesser than allocated_unsafe_pages */
Rafael J. Wysocki83573762006-12-06 20:34:18 -08002009 nr_pages = nr_copy_pages - nr_highmem - allocated_unsafe_pages;
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07002010 nr_pages = DIV_ROUND_UP(nr_pages, PBES_PER_LINKED_PAGE);
2011 while (nr_pages > 0) {
Rafael J. Wysocki83573762006-12-06 20:34:18 -08002012 lp = get_image_page(GFP_ATOMIC, PG_SAFE);
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07002013 if (!lp) {
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08002014 error = -ENOMEM;
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07002015 goto Free;
Rafael J. Wysocki968808b82006-06-23 02:04:48 -07002016 }
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07002017 lp->next = sp_list;
2018 sp_list = lp;
2019 nr_pages--;
Rafael J. Wysocki968808b82006-06-23 02:04:48 -07002020 }
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07002021 /* Preallocate memory for the image */
2022 safe_pages_list = NULL;
Rafael J. Wysocki83573762006-12-06 20:34:18 -08002023 nr_pages = nr_copy_pages - nr_highmem - allocated_unsafe_pages;
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07002024 while (nr_pages > 0) {
2025 lp = (struct linked_page *)get_zeroed_page(GFP_ATOMIC);
2026 if (!lp) {
2027 error = -ENOMEM;
2028 goto Free;
2029 }
Rafael J. Wysocki7be98232007-05-06 14:50:42 -07002030 if (!swsusp_page_is_free(virt_to_page(lp))) {
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07002031 /* The page is "safe", add it to the list */
2032 lp->next = safe_pages_list;
2033 safe_pages_list = lp;
2034 }
2035 /* Mark the page as allocated */
Rafael J. Wysocki7be98232007-05-06 14:50:42 -07002036 swsusp_set_page_forbidden(virt_to_page(lp));
2037 swsusp_set_page_free(virt_to_page(lp));
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07002038 nr_pages--;
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08002039 }
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07002040 /* Free the reserved safe pages so that chain_alloc() can use them */
2041 while (sp_list) {
2042 lp = sp_list->next;
2043 free_image_page(sp_list, PG_UNSAFE_CLEAR);
2044 sp_list = lp;
2045 }
2046 return 0;
2047
Rafael J. Wysocki59a493352006-12-06 20:34:44 -08002048 Free:
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07002049 swsusp_free();
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08002050 return error;
2051}
2052
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07002053/**
2054 * get_buffer - compute the address that snapshot_write_next() should
2055 * set for its caller to write to.
2056 */
Rafael J. Wysocki968808b82006-06-23 02:04:48 -07002057
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07002058static void *get_buffer(struct memory_bitmap *bm, struct chain_allocator *ca)
2059{
2060 struct pbe *pbe;
Rafael J. Wysocki69643272008-11-11 21:32:44 +01002061 struct page *page;
2062 unsigned long pfn = memory_bm_next_pfn(bm);
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07002063
Rafael J. Wysocki69643272008-11-11 21:32:44 +01002064 if (pfn == BM_END_OF_MAP)
2065 return ERR_PTR(-EFAULT);
2066
2067 page = pfn_to_page(pfn);
Rafael J. Wysocki83573762006-12-06 20:34:18 -08002068 if (PageHighMem(page))
2069 return get_highmem_page_buffer(page, ca);
2070
Rafael J. Wysocki7be98232007-05-06 14:50:42 -07002071 if (swsusp_page_is_forbidden(page) && swsusp_page_is_free(page))
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07002072 /* We have allocated the "original" page frame and we can
2073 * use it directly to store the loaded page.
Rafael J. Wysocki968808b82006-06-23 02:04:48 -07002074 */
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07002075 return page_address(page);
2076
2077 /* The "original" page frame has not been allocated and we have to
2078 * use a "safe" page frame to store the loaded page.
Rafael J. Wysocki968808b82006-06-23 02:04:48 -07002079 */
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07002080 pbe = chain_alloc(ca, sizeof(struct pbe));
2081 if (!pbe) {
2082 swsusp_free();
Rafael J. Wysocki69643272008-11-11 21:32:44 +01002083 return ERR_PTR(-ENOMEM);
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07002084 }
Rafael J. Wysocki83573762006-12-06 20:34:18 -08002085 pbe->orig_address = page_address(page);
2086 pbe->address = safe_pages_list;
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07002087 safe_pages_list = safe_pages_list->next;
2088 pbe->next = restore_pblist;
2089 restore_pblist = pbe;
Rafael J. Wysocki83573762006-12-06 20:34:18 -08002090 return pbe->address;
Rafael J. Wysocki968808b82006-06-23 02:04:48 -07002091}
2092
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08002093/**
2094 * snapshot_write_next - used for writing the system memory snapshot.
2095 *
2096 * On the first call to it @handle should point to a zeroed
2097 * snapshot_handle structure. The structure gets updated and a pointer
2098 * to it should be passed to this function every next time.
2099 *
2100 * The @count parameter should contain the number of bytes the caller
2101 * wants to write to the image. It must not be zero.
2102 *
2103 * On success the function returns a positive number. Then, the caller
2104 * is allowed to write up to the returned number of bytes to the memory
2105 * location computed by the data_of() macro. The number returned
2106 * may be smaller than @count, but this only happens if the write would
2107 * cross a page boundary otherwise.
2108 *
2109 * The function returns 0 to indicate the "end of file" condition,
2110 * and a negative number is returned on error. In such cases the
2111 * structure pointed to by @handle is not updated and should not be used
2112 * any more.
2113 */
2114
2115int snapshot_write_next(struct snapshot_handle *handle, size_t count)
2116{
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07002117 static struct chain_allocator ca;
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08002118 int error = 0;
2119
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07002120 /* Check if we have already loaded the entire image */
Rafael J. Wysockifb13a282006-09-25 23:32:46 -07002121 if (handle->prev && handle->cur > nr_meta_pages + nr_copy_pages)
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08002122 return 0;
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07002123
Rafael J. Wysocki83573762006-12-06 20:34:18 -08002124 if (handle->offset == 0) {
2125 if (!buffer)
2126 /* This makes the buffer be freed by swsusp_free() */
2127 buffer = get_image_page(GFP_ATOMIC, PG_ANY);
2128
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08002129 if (!buffer)
2130 return -ENOMEM;
Rafael J. Wysocki83573762006-12-06 20:34:18 -08002131
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08002132 handle->buffer = buffer;
Rafael J. Wysocki83573762006-12-06 20:34:18 -08002133 }
Andrew Morton546e0d22006-09-25 23:32:44 -07002134 handle->sync_read = 1;
Rafael J. Wysockifb13a282006-09-25 23:32:46 -07002135 if (handle->prev < handle->cur) {
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07002136 if (handle->prev == 0) {
2137 error = load_header(buffer);
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08002138 if (error)
2139 return error;
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07002140
2141 error = memory_bm_create(&copy_bm, GFP_ATOMIC, PG_ANY);
2142 if (error)
2143 return error;
2144
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08002145 } else if (handle->prev <= nr_meta_pages) {
Rafael J. Wysocki69643272008-11-11 21:32:44 +01002146 error = unpack_orig_pfns(buffer, &copy_bm);
2147 if (error)
2148 return error;
2149
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07002150 if (handle->prev == nr_meta_pages) {
2151 error = prepare_image(&orig_bm, &copy_bm);
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08002152 if (error)
2153 return error;
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07002154
2155 chain_init(&ca, GFP_ATOMIC, PG_SAFE);
2156 memory_bm_position_reset(&orig_bm);
2157 restore_pblist = NULL;
2158 handle->buffer = get_buffer(&orig_bm, &ca);
Andrew Morton546e0d22006-09-25 23:32:44 -07002159 handle->sync_read = 0;
Rafael J. Wysocki69643272008-11-11 21:32:44 +01002160 if (IS_ERR(handle->buffer))
2161 return PTR_ERR(handle->buffer);
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08002162 }
2163 } else {
Rafael J. Wysocki83573762006-12-06 20:34:18 -08002164 copy_last_highmem_page();
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07002165 handle->buffer = get_buffer(&orig_bm, &ca);
Rafael J. Wysocki69643272008-11-11 21:32:44 +01002166 if (IS_ERR(handle->buffer))
2167 return PTR_ERR(handle->buffer);
Rafael J. Wysocki83573762006-12-06 20:34:18 -08002168 if (handle->buffer != buffer)
2169 handle->sync_read = 0;
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08002170 }
Rafael J. Wysockifb13a282006-09-25 23:32:46 -07002171 handle->prev = handle->cur;
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08002172 }
Rafael J. Wysockifb13a282006-09-25 23:32:46 -07002173 handle->buf_offset = handle->cur_offset;
2174 if (handle->cur_offset + count >= PAGE_SIZE) {
2175 count = PAGE_SIZE - handle->cur_offset;
2176 handle->cur_offset = 0;
2177 handle->cur++;
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08002178 } else {
Rafael J. Wysockifb13a282006-09-25 23:32:46 -07002179 handle->cur_offset += count;
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08002180 }
2181 handle->offset += count;
2182 return count;
2183}
2184
Rafael J. Wysocki83573762006-12-06 20:34:18 -08002185/**
2186 * snapshot_write_finalize - must be called after the last call to
2187 * snapshot_write_next() in case the last page in the image happens
2188 * to be a highmem page and its contents should be stored in the
2189 * highmem. Additionally, it releases the memory that will not be
2190 * used any more.
2191 */
2192
2193void snapshot_write_finalize(struct snapshot_handle *handle)
2194{
2195 copy_last_highmem_page();
2196 /* Free only if we have loaded the image entirely */
2197 if (handle->prev && handle->cur > nr_meta_pages + nr_copy_pages) {
2198 memory_bm_free(&orig_bm, PG_UNSAFE_CLEAR);
2199 free_highmem_data();
2200 }
2201}
2202
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08002203int snapshot_image_loaded(struct snapshot_handle *handle)
2204{
Rafael J. Wysocki83573762006-12-06 20:34:18 -08002205 return !(!nr_copy_pages || !last_highmem_page_copied() ||
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07002206 handle->cur <= nr_meta_pages + nr_copy_pages);
2207}
2208
Rafael J. Wysocki83573762006-12-06 20:34:18 -08002209#ifdef CONFIG_HIGHMEM
2210/* Assumes that @buf is ready and points to a "safe" page */
2211static inline void
2212swap_two_pages_data(struct page *p1, struct page *p2, void *buf)
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07002213{
Rafael J. Wysocki83573762006-12-06 20:34:18 -08002214 void *kaddr1, *kaddr2;
2215
2216 kaddr1 = kmap_atomic(p1, KM_USER0);
2217 kaddr2 = kmap_atomic(p2, KM_USER1);
2218 memcpy(buf, kaddr1, PAGE_SIZE);
2219 memcpy(kaddr1, kaddr2, PAGE_SIZE);
2220 memcpy(kaddr2, buf, PAGE_SIZE);
2221 kunmap_atomic(kaddr1, KM_USER0);
2222 kunmap_atomic(kaddr2, KM_USER1);
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08002223}
Rafael J. Wysocki83573762006-12-06 20:34:18 -08002224
2225/**
2226 * restore_highmem - for each highmem page that was allocated before
2227 * the suspend and included in the suspend image, and also has been
2228 * allocated by the "resume" kernel swap its current (ie. "before
2229 * resume") contents with the previous (ie. "before suspend") one.
2230 *
2231 * If the resume eventually fails, we can call this function once
2232 * again and restore the "before resume" highmem state.
2233 */
2234
2235int restore_highmem(void)
2236{
2237 struct highmem_pbe *pbe = highmem_pblist;
2238 void *buf;
2239
2240 if (!pbe)
2241 return 0;
2242
2243 buf = get_image_page(GFP_ATOMIC, PG_SAFE);
2244 if (!buf)
2245 return -ENOMEM;
2246
2247 while (pbe) {
2248 swap_two_pages_data(pbe->copy_page, pbe->orig_page, buf);
2249 pbe = pbe->next;
2250 }
2251 free_image_page(buf, PG_UNSAFE_CLEAR);
2252 return 0;
2253}
2254#endif /* CONFIG_HIGHMEM */