blob: 33e2e4a819f97014a6e70e5f06d52e246ba783b3 [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. Wysocki83573762006-12-06 20:34:18 -080042/* List of PBEs needed for restoring the pages that were allocated before
43 * the suspend and included in the suspend image, but have also been
44 * allocated by the "resume" kernel, so their contents cannot be written
45 * directly to their "original" page frames.
46 */
Rafael J. Wysocki75534b52006-09-25 23:32:52 -070047struct pbe *restore_pblist;
48
Rafael J. Wysocki83573762006-12-06 20:34:18 -080049/* Pointer to an auxiliary buffer (1 page) */
Rafael J. Wysocki940864d2006-09-25 23:32:55 -070050static void *buffer;
Rafael J. Wysocki7088a5c2006-01-06 00:13:05 -080051
Rafael J. Wysockif6143aa2006-09-25 23:32:50 -070052/**
53 * @safe_needed - on resume, for storing the PBE list and the image,
54 * we can only use memory pages that do not conflict with the pages
Rafael J. Wysocki83573762006-12-06 20:34:18 -080055 * used before suspend. The unsafe pages have PageNosaveFree set
56 * and we count them using unsafe_pages.
Rafael J. Wysockif6143aa2006-09-25 23:32:50 -070057 *
Rafael J. Wysocki83573762006-12-06 20:34:18 -080058 * Each allocated image page is marked as PageNosave and PageNosaveFree
59 * so that swsusp_free() can release it.
Rafael J. Wysockif6143aa2006-09-25 23:32:50 -070060 */
61
Rafael J. Wysocki0bcd8882006-09-25 23:32:52 -070062#define PG_ANY 0
63#define PG_SAFE 1
64#define PG_UNSAFE_CLEAR 1
65#define PG_UNSAFE_KEEP 0
66
Rafael J. Wysocki940864d2006-09-25 23:32:55 -070067static unsigned int allocated_unsafe_pages;
Rafael J. Wysockif6143aa2006-09-25 23:32:50 -070068
Rafael J. Wysocki83573762006-12-06 20:34:18 -080069static void *get_image_page(gfp_t gfp_mask, int safe_needed)
Rafael J. Wysockif6143aa2006-09-25 23:32:50 -070070{
71 void *res;
72
73 res = (void *)get_zeroed_page(gfp_mask);
74 if (safe_needed)
Rafael J. Wysocki7be98232007-05-06 14:50:42 -070075 while (res && swsusp_page_is_free(virt_to_page(res))) {
Rafael J. Wysockif6143aa2006-09-25 23:32:50 -070076 /* The page is unsafe, mark it for swsusp_free() */
Rafael J. Wysocki7be98232007-05-06 14:50:42 -070077 swsusp_set_page_forbidden(virt_to_page(res));
Rafael J. Wysocki940864d2006-09-25 23:32:55 -070078 allocated_unsafe_pages++;
Rafael J. Wysockif6143aa2006-09-25 23:32:50 -070079 res = (void *)get_zeroed_page(gfp_mask);
80 }
81 if (res) {
Rafael J. Wysocki7be98232007-05-06 14:50:42 -070082 swsusp_set_page_forbidden(virt_to_page(res));
83 swsusp_set_page_free(virt_to_page(res));
Rafael J. Wysockif6143aa2006-09-25 23:32:50 -070084 }
85 return res;
86}
87
88unsigned long get_safe_page(gfp_t gfp_mask)
89{
Rafael J. Wysocki83573762006-12-06 20:34:18 -080090 return (unsigned long)get_image_page(gfp_mask, PG_SAFE);
91}
92
Rafael J. Wysocki5b6d15d2006-12-06 20:34:43 -080093static struct page *alloc_image_page(gfp_t gfp_mask)
94{
Rafael J. Wysocki83573762006-12-06 20:34:18 -080095 struct page *page;
96
97 page = alloc_page(gfp_mask);
98 if (page) {
Rafael J. Wysocki7be98232007-05-06 14:50:42 -070099 swsusp_set_page_forbidden(page);
100 swsusp_set_page_free(page);
Rafael J. Wysocki83573762006-12-06 20:34:18 -0800101 }
102 return page;
Rafael J. Wysockif6143aa2006-09-25 23:32:50 -0700103}
104
105/**
106 * free_image_page - free page represented by @addr, allocated with
Rafael J. Wysocki83573762006-12-06 20:34:18 -0800107 * get_image_page (page flags set by it must be cleared)
Rafael J. Wysockif6143aa2006-09-25 23:32:50 -0700108 */
109
110static inline void free_image_page(void *addr, int clear_nosave_free)
111{
Rafael J. Wysocki83573762006-12-06 20:34:18 -0800112 struct page *page;
113
114 BUG_ON(!virt_addr_valid(addr));
115
116 page = virt_to_page(addr);
117
Rafael J. Wysocki7be98232007-05-06 14:50:42 -0700118 swsusp_unset_page_forbidden(page);
Rafael J. Wysockif6143aa2006-09-25 23:32:50 -0700119 if (clear_nosave_free)
Rafael J. Wysocki7be98232007-05-06 14:50:42 -0700120 swsusp_unset_page_free(page);
Rafael J. Wysocki83573762006-12-06 20:34:18 -0800121
122 __free_page(page);
Rafael J. Wysockif6143aa2006-09-25 23:32:50 -0700123}
124
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700125/* struct linked_page is used to build chains of pages */
126
127#define LINKED_PAGE_DATA_SIZE (PAGE_SIZE - sizeof(void *))
128
129struct linked_page {
130 struct linked_page *next;
131 char data[LINKED_PAGE_DATA_SIZE];
132} __attribute__((packed));
133
134static inline void
135free_list_of_pages(struct linked_page *list, int clear_page_nosave)
136{
137 while (list) {
138 struct linked_page *lp = list->next;
139
140 free_image_page(list, clear_page_nosave);
141 list = lp;
142 }
143}
144
145/**
146 * struct chain_allocator is used for allocating small objects out of
147 * a linked list of pages called 'the chain'.
148 *
149 * The chain grows each time when there is no room for a new object in
150 * the current page. The allocated objects cannot be freed individually.
151 * It is only possible to free them all at once, by freeing the entire
152 * chain.
153 *
154 * NOTE: The chain allocator may be inefficient if the allocated objects
155 * are not much smaller than PAGE_SIZE.
156 */
157
158struct chain_allocator {
159 struct linked_page *chain; /* the chain */
160 unsigned int used_space; /* total size of objects allocated out
161 * of the current page
162 */
163 gfp_t gfp_mask; /* mask for allocating pages */
164 int safe_needed; /* if set, only "safe" pages are allocated */
165};
166
167static void
168chain_init(struct chain_allocator *ca, gfp_t gfp_mask, int safe_needed)
169{
170 ca->chain = NULL;
171 ca->used_space = LINKED_PAGE_DATA_SIZE;
172 ca->gfp_mask = gfp_mask;
173 ca->safe_needed = safe_needed;
174}
175
176static void *chain_alloc(struct chain_allocator *ca, unsigned int size)
177{
178 void *ret;
179
180 if (LINKED_PAGE_DATA_SIZE - ca->used_space < size) {
181 struct linked_page *lp;
182
Rafael J. Wysocki83573762006-12-06 20:34:18 -0800183 lp = get_image_page(ca->gfp_mask, ca->safe_needed);
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700184 if (!lp)
185 return NULL;
186
187 lp->next = ca->chain;
188 ca->chain = lp;
189 ca->used_space = 0;
190 }
191 ret = ca->chain->data + ca->used_space;
192 ca->used_space += size;
193 return ret;
194}
195
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700196/**
197 * Data types related to memory bitmaps.
198 *
199 * Memory bitmap is a structure consiting of many linked lists of
200 * objects. The main list's elements are of type struct zone_bitmap
201 * and each of them corresonds to one zone. For each zone bitmap
202 * object there is a list of objects of type struct bm_block that
Akinobu Mita0d833042008-07-23 21:28:38 -0700203 * represent each blocks of bitmap in which information is stored.
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700204 *
205 * struct memory_bitmap contains a pointer to the main list of zone
206 * bitmap objects, a struct bm_position used for browsing the bitmap,
207 * and a pointer to the list of pages used for allocating all of the
208 * zone bitmap objects and bitmap block objects.
209 *
210 * NOTE: It has to be possible to lay out the bitmap in memory
211 * using only allocations of order 0. Additionally, the bitmap is
212 * designed to work with arbitrary number of zones (this is over the
213 * top for now, but let's avoid making unnecessary assumptions ;-).
214 *
215 * struct zone_bitmap contains a pointer to a list of bitmap block
216 * objects and a pointer to the bitmap block object that has been
217 * most recently used for setting bits. Additionally, it contains the
218 * pfns that correspond to the start and end of the represented zone.
219 *
220 * struct bm_block contains a pointer to the memory page in which
Akinobu Mita0d833042008-07-23 21:28:38 -0700221 * information is stored (in the form of a block of bitmap)
222 * It also contains the pfns that correspond to the start and end of
223 * the represented memory area.
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700224 */
225
226#define BM_END_OF_MAP (~0UL)
227
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700228#define BM_BITS_PER_BLOCK (PAGE_SIZE << 3)
229
230struct bm_block {
Rafael J. Wysocki846705d2008-11-26 18:00:24 -0500231 struct list_head hook; /* hook into a list of bitmap blocks */
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700232 unsigned long start_pfn; /* pfn represented by the first bit */
233 unsigned long end_pfn; /* pfn represented by the last bit plus 1 */
Akinobu Mita0d833042008-07-23 21:28:38 -0700234 unsigned long *data; /* bitmap representing pages */
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700235};
236
Akinobu Mita0d833042008-07-23 21:28:38 -0700237static inline unsigned long bm_block_bits(struct bm_block *bb)
238{
239 return bb->end_pfn - bb->start_pfn;
240}
241
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700242/* strcut bm_position is used for browsing memory bitmaps */
243
244struct bm_position {
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700245 struct bm_block *block;
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700246 int bit;
247};
248
249struct memory_bitmap {
Rafael J. Wysocki846705d2008-11-26 18:00:24 -0500250 struct list_head blocks; /* list of bitmap blocks */
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700251 struct linked_page *p_list; /* list of pages used to store zone
252 * bitmap objects and bitmap block
253 * objects
254 */
255 struct bm_position cur; /* most recently used bit position */
256};
257
258/* Functions that operate on memory bitmaps */
259
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700260static void memory_bm_position_reset(struct memory_bitmap *bm)
261{
Rafael J. Wysocki846705d2008-11-26 18:00:24 -0500262 bm->cur.block = list_entry(bm->blocks.next, struct bm_block, hook);
Akinobu Mita0d833042008-07-23 21:28:38 -0700263 bm->cur.bit = 0;
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700264}
265
266static void memory_bm_free(struct memory_bitmap *bm, int clear_nosave_free);
267
268/**
269 * create_bm_block_list - create a list of block bitmap objects
Rafael J. Wysocki846705d2008-11-26 18:00:24 -0500270 * @nr_blocks - number of blocks to allocate
271 * @list - list to put the allocated blocks into
272 * @ca - chain allocator to be used for allocating memory
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700273 */
Rafael J. Wysocki846705d2008-11-26 18:00:24 -0500274static int create_bm_block_list(unsigned long pages,
275 struct list_head *list,
276 struct chain_allocator *ca)
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700277{
Rafael J. Wysocki846705d2008-11-26 18:00:24 -0500278 unsigned int nr_blocks = DIV_ROUND_UP(pages, BM_BITS_PER_BLOCK);
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700279
280 while (nr_blocks-- > 0) {
281 struct bm_block *bb;
282
283 bb = chain_alloc(ca, sizeof(struct bm_block));
284 if (!bb)
Rafael J. Wysocki846705d2008-11-26 18:00:24 -0500285 return -ENOMEM;
286 list_add(&bb->hook, list);
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700287 }
Rafael J. Wysocki846705d2008-11-26 18:00:24 -0500288
289 return 0;
290}
291
292struct mem_extent {
293 struct list_head hook;
294 unsigned long start;
295 unsigned long end;
296};
297
298/**
299 * free_mem_extents - free a list of memory extents
300 * @list - list of extents to empty
301 */
302static void free_mem_extents(struct list_head *list)
303{
304 struct mem_extent *ext, *aux;
305
306 list_for_each_entry_safe(ext, aux, list, hook) {
307 list_del(&ext->hook);
308 kfree(ext);
309 }
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700310}
311
312/**
Rafael J. Wysocki846705d2008-11-26 18:00:24 -0500313 * create_mem_extents - create a list of memory extents representing
314 * contiguous ranges of PFNs
315 * @list - list to put the extents into
316 * @gfp_mask - mask to use for memory allocations
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700317 */
Rafael J. Wysocki846705d2008-11-26 18:00:24 -0500318static int create_mem_extents(struct list_head *list, gfp_t gfp_mask)
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700319{
Rafael J. Wysocki846705d2008-11-26 18:00:24 -0500320 struct zone *zone;
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700321
Rafael J. Wysocki846705d2008-11-26 18:00:24 -0500322 INIT_LIST_HEAD(list);
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700323
KOSAKI Motohiroee99c712009-03-31 15:19:31 -0700324 for_each_populated_zone(zone) {
Rafael J. Wysocki846705d2008-11-26 18:00:24 -0500325 unsigned long zone_start, zone_end;
326 struct mem_extent *ext, *cur, *aux;
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700327
Rafael J. Wysocki846705d2008-11-26 18:00:24 -0500328 zone_start = zone->zone_start_pfn;
329 zone_end = zone->zone_start_pfn + zone->spanned_pages;
330
331 list_for_each_entry(ext, list, hook)
332 if (zone_start <= ext->end)
333 break;
334
335 if (&ext->hook == list || zone_end < ext->start) {
336 /* New extent is necessary */
337 struct mem_extent *new_ext;
338
339 new_ext = kzalloc(sizeof(struct mem_extent), gfp_mask);
340 if (!new_ext) {
341 free_mem_extents(list);
342 return -ENOMEM;
343 }
344 new_ext->start = zone_start;
345 new_ext->end = zone_end;
346 list_add_tail(&new_ext->hook, &ext->hook);
347 continue;
348 }
349
350 /* Merge this zone's range of PFNs with the existing one */
351 if (zone_start < ext->start)
352 ext->start = zone_start;
353 if (zone_end > ext->end)
354 ext->end = zone_end;
355
356 /* More merging may be possible */
357 cur = ext;
358 list_for_each_entry_safe_continue(cur, aux, list, hook) {
359 if (zone_end < cur->start)
360 break;
361 if (zone_end < cur->end)
362 ext->end = cur->end;
363 list_del(&cur->hook);
364 kfree(cur);
365 }
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700366 }
Rafael J. Wysocki846705d2008-11-26 18:00:24 -0500367
368 return 0;
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700369}
370
371/**
372 * memory_bm_create - allocate memory for a memory bitmap
373 */
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700374static int
375memory_bm_create(struct memory_bitmap *bm, gfp_t gfp_mask, int safe_needed)
376{
377 struct chain_allocator ca;
Rafael J. Wysocki846705d2008-11-26 18:00:24 -0500378 struct list_head mem_extents;
379 struct mem_extent *ext;
380 int error;
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700381
382 chain_init(&ca, gfp_mask, safe_needed);
Rafael J. Wysocki846705d2008-11-26 18:00:24 -0500383 INIT_LIST_HEAD(&bm->blocks);
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700384
Rafael J. Wysocki846705d2008-11-26 18:00:24 -0500385 error = create_mem_extents(&mem_extents, gfp_mask);
386 if (error)
387 return error;
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700388
Rafael J. Wysocki846705d2008-11-26 18:00:24 -0500389 list_for_each_entry(ext, &mem_extents, hook) {
390 struct bm_block *bb;
391 unsigned long pfn = ext->start;
392 unsigned long pages = ext->end - ext->start;
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700393
Rafael J. Wysocki846705d2008-11-26 18:00:24 -0500394 bb = list_entry(bm->blocks.prev, struct bm_block, hook);
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700395
Rafael J. Wysocki846705d2008-11-26 18:00:24 -0500396 error = create_bm_block_list(pages, bm->blocks.prev, &ca);
397 if (error)
398 goto Error;
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700399
Rafael J. Wysocki846705d2008-11-26 18:00:24 -0500400 list_for_each_entry_continue(bb, &bm->blocks, hook) {
401 bb->data = get_image_page(gfp_mask, safe_needed);
402 if (!bb->data) {
403 error = -ENOMEM;
404 goto Error;
405 }
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700406
407 bb->start_pfn = pfn;
Rafael J. Wysocki846705d2008-11-26 18:00:24 -0500408 if (pages >= BM_BITS_PER_BLOCK) {
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700409 pfn += BM_BITS_PER_BLOCK;
Rafael J. Wysocki846705d2008-11-26 18:00:24 -0500410 pages -= BM_BITS_PER_BLOCK;
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700411 } else {
412 /* This is executed only once in the loop */
Rafael J. Wysocki846705d2008-11-26 18:00:24 -0500413 pfn += pages;
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700414 }
415 bb->end_pfn = pfn;
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700416 }
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700417 }
Rafael J. Wysocki846705d2008-11-26 18:00:24 -0500418
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700419 bm->p_list = ca.chain;
420 memory_bm_position_reset(bm);
Rafael J. Wysocki846705d2008-11-26 18:00:24 -0500421 Exit:
422 free_mem_extents(&mem_extents);
423 return error;
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700424
Rafael J. Wysocki846705d2008-11-26 18:00:24 -0500425 Error:
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700426 bm->p_list = ca.chain;
427 memory_bm_free(bm, PG_UNSAFE_CLEAR);
Rafael J. Wysocki846705d2008-11-26 18:00:24 -0500428 goto Exit;
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700429}
430
431/**
432 * memory_bm_free - free memory occupied by the memory bitmap @bm
433 */
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700434static void memory_bm_free(struct memory_bitmap *bm, int clear_nosave_free)
435{
Rafael J. Wysocki846705d2008-11-26 18:00:24 -0500436 struct bm_block *bb;
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700437
Rafael J. Wysocki846705d2008-11-26 18:00:24 -0500438 list_for_each_entry(bb, &bm->blocks, hook)
439 if (bb->data)
440 free_image_page(bb->data, clear_nosave_free);
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700441
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700442 free_list_of_pages(bm->p_list, clear_nosave_free);
Rafael J. Wysocki846705d2008-11-26 18:00:24 -0500443
444 INIT_LIST_HEAD(&bm->blocks);
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700445}
446
447/**
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -0700448 * memory_bm_find_bit - find the bit in the bitmap @bm that corresponds
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700449 * to given pfn. The cur_zone_bm member of @bm and the cur_block member
450 * of @bm->cur_zone_bm are updated.
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700451 */
Rafael J. Wysockia82f7112008-03-12 00:34:57 +0100452static int memory_bm_find_bit(struct memory_bitmap *bm, unsigned long pfn,
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -0700453 void **addr, unsigned int *bit_nr)
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700454{
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700455 struct bm_block *bb;
456
Rafael J. Wysocki846705d2008-11-26 18:00:24 -0500457 /*
458 * Check if the pfn corresponds to the current bitmap block and find
459 * the block where it fits if this is not the case.
460 */
461 bb = bm->cur.block;
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700462 if (pfn < bb->start_pfn)
Rafael J. Wysocki846705d2008-11-26 18:00:24 -0500463 list_for_each_entry_continue_reverse(bb, &bm->blocks, hook)
464 if (pfn >= bb->start_pfn)
465 break;
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700466
Rafael J. Wysocki846705d2008-11-26 18:00:24 -0500467 if (pfn >= bb->end_pfn)
468 list_for_each_entry_continue(bb, &bm->blocks, hook)
469 if (pfn >= bb->start_pfn && pfn < bb->end_pfn)
470 break;
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -0700471
Rafael J. Wysocki846705d2008-11-26 18:00:24 -0500472 if (&bb->hook == &bm->blocks)
473 return -EFAULT;
474
475 /* The block has been found */
476 bm->cur.block = bb;
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700477 pfn -= bb->start_pfn;
Rafael J. Wysocki846705d2008-11-26 18:00:24 -0500478 bm->cur.bit = pfn + 1;
Akinobu Mita0d833042008-07-23 21:28:38 -0700479 *bit_nr = pfn;
480 *addr = bb->data;
Rafael J. Wysockia82f7112008-03-12 00:34:57 +0100481 return 0;
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -0700482}
483
484static void memory_bm_set_bit(struct memory_bitmap *bm, unsigned long pfn)
485{
486 void *addr;
487 unsigned int bit;
Rafael J. Wysockia82f7112008-03-12 00:34:57 +0100488 int error;
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -0700489
Rafael J. Wysockia82f7112008-03-12 00:34:57 +0100490 error = memory_bm_find_bit(bm, pfn, &addr, &bit);
491 BUG_ON(error);
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -0700492 set_bit(bit, addr);
493}
494
Rafael J. Wysockia82f7112008-03-12 00:34:57 +0100495static int mem_bm_set_bit_check(struct memory_bitmap *bm, unsigned long pfn)
496{
497 void *addr;
498 unsigned int bit;
499 int error;
500
501 error = memory_bm_find_bit(bm, pfn, &addr, &bit);
502 if (!error)
503 set_bit(bit, addr);
504 return error;
505}
506
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -0700507static void memory_bm_clear_bit(struct memory_bitmap *bm, unsigned long pfn)
508{
509 void *addr;
510 unsigned int bit;
Rafael J. Wysockia82f7112008-03-12 00:34:57 +0100511 int error;
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -0700512
Rafael J. Wysockia82f7112008-03-12 00:34:57 +0100513 error = memory_bm_find_bit(bm, pfn, &addr, &bit);
514 BUG_ON(error);
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -0700515 clear_bit(bit, addr);
516}
517
518static int memory_bm_test_bit(struct memory_bitmap *bm, unsigned long pfn)
519{
520 void *addr;
521 unsigned int bit;
Rafael J. Wysockia82f7112008-03-12 00:34:57 +0100522 int error;
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -0700523
Rafael J. Wysockia82f7112008-03-12 00:34:57 +0100524 error = memory_bm_find_bit(bm, pfn, &addr, &bit);
525 BUG_ON(error);
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -0700526 return test_bit(bit, addr);
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700527}
528
Rafael J. Wysocki69643272008-11-11 21:32:44 +0100529static bool memory_bm_pfn_present(struct memory_bitmap *bm, unsigned long pfn)
530{
531 void *addr;
532 unsigned int bit;
533
534 return !memory_bm_find_bit(bm, pfn, &addr, &bit);
535}
536
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700537/**
538 * memory_bm_next_pfn - find the pfn that corresponds to the next set bit
539 * in the bitmap @bm. If the pfn cannot be found, BM_END_OF_MAP is
540 * returned.
541 *
542 * It is required to run memory_bm_position_reset() before the first call to
543 * this function.
544 */
545
546static unsigned long memory_bm_next_pfn(struct memory_bitmap *bm)
547{
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700548 struct bm_block *bb;
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700549 int bit;
550
Rafael J. Wysocki846705d2008-11-26 18:00:24 -0500551 bb = bm->cur.block;
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700552 do {
Rafael J. Wysocki846705d2008-11-26 18:00:24 -0500553 bit = bm->cur.bit;
554 bit = find_next_bit(bb->data, bm_block_bits(bb), bit);
555 if (bit < bm_block_bits(bb))
556 goto Return_pfn;
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700557
Rafael J. Wysocki846705d2008-11-26 18:00:24 -0500558 bb = list_entry(bb->hook.next, struct bm_block, hook);
559 bm->cur.block = bb;
560 bm->cur.bit = 0;
561 } while (&bb->hook != &bm->blocks);
562
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700563 memory_bm_position_reset(bm);
564 return BM_END_OF_MAP;
565
Rafael J. Wysocki59a493352006-12-06 20:34:44 -0800566 Return_pfn:
Akinobu Mita0d833042008-07-23 21:28:38 -0700567 bm->cur.bit = bit + 1;
568 return bb->start_pfn + bit;
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700569}
570
571/**
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -0700572 * This structure represents a range of page frames the contents of which
573 * should not be saved during the suspend.
574 */
575
576struct nosave_region {
577 struct list_head list;
578 unsigned long start_pfn;
579 unsigned long end_pfn;
580};
581
582static LIST_HEAD(nosave_regions);
583
584/**
585 * register_nosave_region - register a range of page frames the contents
586 * of which should not be saved during the suspend (to be used in the early
587 * initialization code)
588 */
589
590void __init
Johannes Berg940d67f2007-05-08 19:23:49 +1000591__register_nosave_region(unsigned long start_pfn, unsigned long end_pfn,
592 int use_kmalloc)
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -0700593{
594 struct nosave_region *region;
595
596 if (start_pfn >= end_pfn)
597 return;
598
599 if (!list_empty(&nosave_regions)) {
600 /* Try to extend the previous region (they should be sorted) */
601 region = list_entry(nosave_regions.prev,
602 struct nosave_region, list);
603 if (region->end_pfn == start_pfn) {
604 region->end_pfn = end_pfn;
605 goto Report;
606 }
607 }
Johannes Berg940d67f2007-05-08 19:23:49 +1000608 if (use_kmalloc) {
609 /* during init, this shouldn't fail */
610 region = kmalloc(sizeof(struct nosave_region), GFP_KERNEL);
611 BUG_ON(!region);
612 } else
613 /* This allocation cannot fail */
614 region = alloc_bootmem_low(sizeof(struct nosave_region));
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -0700615 region->start_pfn = start_pfn;
616 region->end_pfn = end_pfn;
617 list_add_tail(&region->list, &nosave_regions);
618 Report:
Rafael J. Wysocki23976722007-12-08 02:09:43 +0100619 printk(KERN_INFO "PM: Registered nosave memory: %016lx - %016lx\n",
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -0700620 start_pfn << PAGE_SHIFT, end_pfn << PAGE_SHIFT);
621}
622
623/*
624 * Set bits in this map correspond to the page frames the contents of which
625 * should not be saved during the suspend.
626 */
627static struct memory_bitmap *forbidden_pages_map;
628
629/* Set bits in this map correspond to free page frames. */
630static struct memory_bitmap *free_pages_map;
631
632/*
633 * Each page frame allocated for creating the image is marked by setting the
634 * corresponding bits in forbidden_pages_map and free_pages_map simultaneously
635 */
636
637void swsusp_set_page_free(struct page *page)
638{
639 if (free_pages_map)
640 memory_bm_set_bit(free_pages_map, page_to_pfn(page));
641}
642
643static int swsusp_page_is_free(struct page *page)
644{
645 return free_pages_map ?
646 memory_bm_test_bit(free_pages_map, page_to_pfn(page)) : 0;
647}
648
649void swsusp_unset_page_free(struct page *page)
650{
651 if (free_pages_map)
652 memory_bm_clear_bit(free_pages_map, page_to_pfn(page));
653}
654
655static void swsusp_set_page_forbidden(struct page *page)
656{
657 if (forbidden_pages_map)
658 memory_bm_set_bit(forbidden_pages_map, page_to_pfn(page));
659}
660
661int swsusp_page_is_forbidden(struct page *page)
662{
663 return forbidden_pages_map ?
664 memory_bm_test_bit(forbidden_pages_map, page_to_pfn(page)) : 0;
665}
666
667static void swsusp_unset_page_forbidden(struct page *page)
668{
669 if (forbidden_pages_map)
670 memory_bm_clear_bit(forbidden_pages_map, page_to_pfn(page));
671}
672
673/**
674 * mark_nosave_pages - set bits corresponding to the page frames the
675 * contents of which should not be saved in a given bitmap.
676 */
677
678static void mark_nosave_pages(struct memory_bitmap *bm)
679{
680 struct nosave_region *region;
681
682 if (list_empty(&nosave_regions))
683 return;
684
685 list_for_each_entry(region, &nosave_regions, list) {
686 unsigned long pfn;
687
Rafael J. Wysocki23976722007-12-08 02:09:43 +0100688 pr_debug("PM: Marking nosave pages: %016lx - %016lx\n",
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -0700689 region->start_pfn << PAGE_SHIFT,
690 region->end_pfn << PAGE_SHIFT);
691
692 for (pfn = region->start_pfn; pfn < region->end_pfn; pfn++)
Rafael J. Wysockia82f7112008-03-12 00:34:57 +0100693 if (pfn_valid(pfn)) {
694 /*
695 * It is safe to ignore the result of
696 * mem_bm_set_bit_check() here, since we won't
697 * touch the PFNs for which the error is
698 * returned anyway.
699 */
700 mem_bm_set_bit_check(bm, pfn);
701 }
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -0700702 }
703}
704
705/**
706 * create_basic_memory_bitmaps - create bitmaps needed for marking page
707 * frames that should not be saved and free page frames. The pointers
708 * forbidden_pages_map and free_pages_map are only modified if everything
709 * goes well, because we don't want the bits to be used before both bitmaps
710 * are set up.
711 */
712
713int create_basic_memory_bitmaps(void)
714{
715 struct memory_bitmap *bm1, *bm2;
716 int error = 0;
717
718 BUG_ON(forbidden_pages_map || free_pages_map);
719
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700720 bm1 = kzalloc(sizeof(struct memory_bitmap), GFP_KERNEL);
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -0700721 if (!bm1)
722 return -ENOMEM;
723
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700724 error = memory_bm_create(bm1, GFP_KERNEL, PG_ANY);
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -0700725 if (error)
726 goto Free_first_object;
727
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700728 bm2 = kzalloc(sizeof(struct memory_bitmap), GFP_KERNEL);
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -0700729 if (!bm2)
730 goto Free_first_bitmap;
731
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700732 error = memory_bm_create(bm2, GFP_KERNEL, PG_ANY);
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -0700733 if (error)
734 goto Free_second_object;
735
736 forbidden_pages_map = bm1;
737 free_pages_map = bm2;
738 mark_nosave_pages(forbidden_pages_map);
739
Rafael J. Wysocki23976722007-12-08 02:09:43 +0100740 pr_debug("PM: Basic memory bitmaps created\n");
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -0700741
742 return 0;
743
744 Free_second_object:
745 kfree(bm2);
746 Free_first_bitmap:
747 memory_bm_free(bm1, PG_UNSAFE_CLEAR);
748 Free_first_object:
749 kfree(bm1);
750 return -ENOMEM;
751}
752
753/**
754 * free_basic_memory_bitmaps - free memory bitmaps allocated by
755 * create_basic_memory_bitmaps(). The auxiliary pointers are necessary
756 * so that the bitmaps themselves are not referred to while they are being
757 * freed.
758 */
759
760void free_basic_memory_bitmaps(void)
761{
762 struct memory_bitmap *bm1, *bm2;
763
764 BUG_ON(!(forbidden_pages_map && free_pages_map));
765
766 bm1 = forbidden_pages_map;
767 bm2 = free_pages_map;
768 forbidden_pages_map = NULL;
769 free_pages_map = NULL;
770 memory_bm_free(bm1, PG_UNSAFE_CLEAR);
771 kfree(bm1);
772 memory_bm_free(bm2, PG_UNSAFE_CLEAR);
773 kfree(bm2);
774
Rafael J. Wysocki23976722007-12-08 02:09:43 +0100775 pr_debug("PM: Basic memory bitmaps freed\n");
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -0700776}
777
778/**
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700779 * snapshot_additional_pages - estimate the number of additional pages
780 * be needed for setting up the suspend image data structures for given
781 * zone (usually the returned value is greater than the exact number)
782 */
783
784unsigned int snapshot_additional_pages(struct zone *zone)
785{
786 unsigned int res;
787
788 res = DIV_ROUND_UP(zone->spanned_pages, BM_BITS_PER_BLOCK);
789 res += DIV_ROUND_UP(res * sizeof(struct bm_block), PAGE_SIZE);
Rafael J. Wysocki83573762006-12-06 20:34:18 -0800790 return 2 * res;
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700791}
792
Rafael J. Wysocki83573762006-12-06 20:34:18 -0800793#ifdef CONFIG_HIGHMEM
794/**
795 * count_free_highmem_pages - compute the total number of free highmem
796 * pages, system-wide.
797 */
798
799static unsigned int count_free_highmem_pages(void)
800{
801 struct zone *zone;
802 unsigned int cnt = 0;
803
KOSAKI Motohiroee99c712009-03-31 15:19:31 -0700804 for_each_populated_zone(zone)
805 if (is_highmem(zone))
Christoph Lameterd23ad422007-02-10 01:43:02 -0800806 cnt += zone_page_state(zone, NR_FREE_PAGES);
Rafael J. Wysocki83573762006-12-06 20:34:18 -0800807
808 return cnt;
809}
810
811/**
812 * saveable_highmem_page - Determine whether a highmem page should be
813 * included in the suspend image.
814 *
815 * We should save the page if it isn't Nosave or NosaveFree, or Reserved,
816 * and it isn't a part of a free chunk of pages.
817 */
Rafael J. Wysocki846705d2008-11-26 18:00:24 -0500818static struct page *saveable_highmem_page(struct zone *zone, unsigned long pfn)
Rafael J. Wysocki83573762006-12-06 20:34:18 -0800819{
820 struct page *page;
821
822 if (!pfn_valid(pfn))
823 return NULL;
824
825 page = pfn_to_page(pfn);
Rafael J. Wysocki846705d2008-11-26 18:00:24 -0500826 if (page_zone(page) != zone)
827 return NULL;
Rafael J. Wysocki83573762006-12-06 20:34:18 -0800828
829 BUG_ON(!PageHighMem(page));
830
Rafael J. Wysocki7be98232007-05-06 14:50:42 -0700831 if (swsusp_page_is_forbidden(page) || swsusp_page_is_free(page) ||
832 PageReserved(page))
Rafael J. Wysocki83573762006-12-06 20:34:18 -0800833 return NULL;
834
835 return page;
836}
837
838/**
839 * count_highmem_pages - compute the total number of saveable highmem
840 * pages.
841 */
842
843unsigned int count_highmem_pages(void)
844{
845 struct zone *zone;
846 unsigned int n = 0;
847
848 for_each_zone(zone) {
849 unsigned long pfn, max_zone_pfn;
850
851 if (!is_highmem(zone))
852 continue;
853
854 mark_free_pages(zone);
855 max_zone_pfn = zone->zone_start_pfn + zone->spanned_pages;
856 for (pfn = zone->zone_start_pfn; pfn < max_zone_pfn; pfn++)
Rafael J. Wysocki846705d2008-11-26 18:00:24 -0500857 if (saveable_highmem_page(zone, pfn))
Rafael J. Wysocki83573762006-12-06 20:34:18 -0800858 n++;
859 }
860 return n;
861}
862#else
Rafael J. Wysocki846705d2008-11-26 18:00:24 -0500863static inline void *saveable_highmem_page(struct zone *z, unsigned long p)
864{
865 return NULL;
866}
Rafael J. Wysocki83573762006-12-06 20:34:18 -0800867#endif /* CONFIG_HIGHMEM */
868
Rafael J. Wysockif6143aa2006-09-25 23:32:50 -0700869/**
Rafael J. Wysocki8a235ef2008-02-20 01:47:44 +0100870 * saveable_page - Determine whether a non-highmem page should be included
871 * in the suspend image.
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800872 *
Rafael J. Wysocki83573762006-12-06 20:34:18 -0800873 * We should save the page if it isn't Nosave, and is not in the range
874 * of pages statically defined as 'unsaveable', and it isn't a part of
875 * a free chunk of pages.
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800876 */
Rafael J. Wysocki846705d2008-11-26 18:00:24 -0500877static struct page *saveable_page(struct zone *zone, unsigned long pfn)
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800878{
Pavel Machekde491862005-10-30 14:59:59 -0800879 struct page *page;
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800880
881 if (!pfn_valid(pfn))
Rafael J. Wysockiae83c5ee2006-09-25 23:32:45 -0700882 return NULL;
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800883
884 page = pfn_to_page(pfn);
Rafael J. Wysocki846705d2008-11-26 18:00:24 -0500885 if (page_zone(page) != zone)
886 return NULL;
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800887
Rafael J. Wysocki83573762006-12-06 20:34:18 -0800888 BUG_ON(PageHighMem(page));
889
Rafael J. Wysocki7be98232007-05-06 14:50:42 -0700890 if (swsusp_page_is_forbidden(page) || swsusp_page_is_free(page))
Rafael J. Wysockiae83c5ee2006-09-25 23:32:45 -0700891 return NULL;
Rafael J. Wysocki83573762006-12-06 20:34:18 -0800892
Rafael J. Wysocki8a235ef2008-02-20 01:47:44 +0100893 if (PageReserved(page)
894 && (!kernel_page_present(page) || pfn_is_nosave(pfn)))
Rafael J. Wysockiae83c5ee2006-09-25 23:32:45 -0700895 return NULL;
Rafael J. Wysockiae83c5ee2006-09-25 23:32:45 -0700896
897 return page;
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800898}
899
Rafael J. Wysocki83573762006-12-06 20:34:18 -0800900/**
901 * count_data_pages - compute the total number of saveable non-highmem
902 * pages.
903 */
904
Rafael J. Wysocki72a97e02006-01-06 00:13:46 -0800905unsigned int count_data_pages(void)
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800906{
907 struct zone *zone;
Rafael J. Wysockiae83c5ee2006-09-25 23:32:45 -0700908 unsigned long pfn, max_zone_pfn;
Pavel Machekdc19d502005-11-07 00:58:40 -0800909 unsigned int n = 0;
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800910
Rafael J. Wysocki83573762006-12-06 20:34:18 -0800911 for_each_zone(zone) {
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800912 if (is_highmem(zone))
913 continue;
Rafael J. Wysocki83573762006-12-06 20:34:18 -0800914
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800915 mark_free_pages(zone);
Rafael J. Wysockiae83c5ee2006-09-25 23:32:45 -0700916 max_zone_pfn = zone->zone_start_pfn + zone->spanned_pages;
917 for (pfn = zone->zone_start_pfn; pfn < max_zone_pfn; pfn++)
Rafael J. Wysocki846705d2008-11-26 18:00:24 -0500918 if (saveable_page(zone, pfn))
Rafael J. Wysocki83573762006-12-06 20:34:18 -0800919 n++;
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800920 }
Rafael J. Wysockia0f49652005-10-30 14:59:57 -0800921 return n;
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800922}
923
Rafael J. Wysocki83573762006-12-06 20:34:18 -0800924/* This is needed, because copy_page and memcpy are not usable for copying
925 * task structs.
926 */
927static inline void do_copy_page(long *dst, long *src)
Rafael J. Wysockif623f0d2006-09-25 23:32:49 -0700928{
929 int n;
930
Rafael J. Wysockif623f0d2006-09-25 23:32:49 -0700931 for (n = PAGE_SIZE / sizeof(long); n; n--)
932 *dst++ = *src++;
933}
934
Rafael J. Wysocki8a235ef2008-02-20 01:47:44 +0100935
936/**
937 * safe_copy_page - check if the page we are going to copy is marked as
938 * present in the kernel page tables (this always is the case if
939 * CONFIG_DEBUG_PAGEALLOC is not set and in that case
940 * kernel_page_present() always returns 'true').
941 */
942static void safe_copy_page(void *dst, struct page *s_page)
943{
944 if (kernel_page_present(s_page)) {
945 do_copy_page(dst, page_address(s_page));
946 } else {
947 kernel_map_pages(s_page, 1, 1);
948 do_copy_page(dst, page_address(s_page));
949 kernel_map_pages(s_page, 1, 0);
950 }
951}
952
953
Rafael J. Wysocki83573762006-12-06 20:34:18 -0800954#ifdef CONFIG_HIGHMEM
955static inline struct page *
956page_is_saveable(struct zone *zone, unsigned long pfn)
957{
958 return is_highmem(zone) ?
Rafael J. Wysocki846705d2008-11-26 18:00:24 -0500959 saveable_highmem_page(zone, pfn) : saveable_page(zone, pfn);
Rafael J. Wysocki83573762006-12-06 20:34:18 -0800960}
961
Rafael J. Wysocki8a235ef2008-02-20 01:47:44 +0100962static void copy_data_page(unsigned long dst_pfn, unsigned long src_pfn)
Rafael J. Wysocki83573762006-12-06 20:34:18 -0800963{
964 struct page *s_page, *d_page;
965 void *src, *dst;
966
967 s_page = pfn_to_page(src_pfn);
968 d_page = pfn_to_page(dst_pfn);
969 if (PageHighMem(s_page)) {
970 src = kmap_atomic(s_page, KM_USER0);
971 dst = kmap_atomic(d_page, KM_USER1);
972 do_copy_page(dst, src);
973 kunmap_atomic(src, KM_USER0);
974 kunmap_atomic(dst, KM_USER1);
975 } else {
Rafael J. Wysocki83573762006-12-06 20:34:18 -0800976 if (PageHighMem(d_page)) {
977 /* Page pointed to by src may contain some kernel
978 * data modified by kmap_atomic()
979 */
Rafael J. Wysocki8a235ef2008-02-20 01:47:44 +0100980 safe_copy_page(buffer, s_page);
Rafael J. Wysockibaa58352008-12-08 00:52:49 +0100981 dst = kmap_atomic(d_page, KM_USER0);
Rafael J. Wysocki83573762006-12-06 20:34:18 -0800982 memcpy(dst, buffer, PAGE_SIZE);
983 kunmap_atomic(dst, KM_USER0);
984 } else {
Rafael J. Wysocki8a235ef2008-02-20 01:47:44 +0100985 safe_copy_page(page_address(d_page), s_page);
Rafael J. Wysocki83573762006-12-06 20:34:18 -0800986 }
987 }
988}
989#else
Rafael J. Wysocki846705d2008-11-26 18:00:24 -0500990#define page_is_saveable(zone, pfn) saveable_page(zone, pfn)
Rafael J. Wysocki83573762006-12-06 20:34:18 -0800991
Rafael J. Wysocki8a235ef2008-02-20 01:47:44 +0100992static inline void copy_data_page(unsigned long dst_pfn, unsigned long src_pfn)
Rafael J. Wysocki83573762006-12-06 20:34:18 -0800993{
Rafael J. Wysocki8a235ef2008-02-20 01:47:44 +0100994 safe_copy_page(page_address(pfn_to_page(dst_pfn)),
995 pfn_to_page(src_pfn));
Rafael J. Wysocki83573762006-12-06 20:34:18 -0800996}
997#endif /* CONFIG_HIGHMEM */
998
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700999static void
1000copy_data_pages(struct memory_bitmap *copy_bm, struct memory_bitmap *orig_bm)
Rafael J. Wysocki25761b62005-10-30 14:59:56 -08001001{
1002 struct zone *zone;
Rafael J. Wysockib788db72006-09-25 23:32:54 -07001003 unsigned long pfn;
Rafael J. Wysocki25761b62005-10-30 14:59:56 -08001004
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001005 for_each_zone(zone) {
Rafael J. Wysockib788db72006-09-25 23:32:54 -07001006 unsigned long max_zone_pfn;
1007
Rafael J. Wysocki25761b62005-10-30 14:59:56 -08001008 mark_free_pages(zone);
Rafael J. Wysockiae83c5ee2006-09-25 23:32:45 -07001009 max_zone_pfn = zone->zone_start_pfn + zone->spanned_pages;
Rafael J. Wysockib788db72006-09-25 23:32:54 -07001010 for (pfn = zone->zone_start_pfn; pfn < max_zone_pfn; pfn++)
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001011 if (page_is_saveable(zone, pfn))
Rafael J. Wysockib788db72006-09-25 23:32:54 -07001012 memory_bm_set_bit(orig_bm, pfn);
Rafael J. Wysocki25761b62005-10-30 14:59:56 -08001013 }
Rafael J. Wysockib788db72006-09-25 23:32:54 -07001014 memory_bm_position_reset(orig_bm);
1015 memory_bm_position_reset(copy_bm);
Fengguang Wudf7c4872007-10-20 02:26:04 +02001016 for(;;) {
Rafael J. Wysockib788db72006-09-25 23:32:54 -07001017 pfn = memory_bm_next_pfn(orig_bm);
Fengguang Wudf7c4872007-10-20 02:26:04 +02001018 if (unlikely(pfn == BM_END_OF_MAP))
1019 break;
1020 copy_data_page(memory_bm_next_pfn(copy_bm), pfn);
1021 }
Rafael J. Wysocki25761b62005-10-30 14:59:56 -08001022}
1023
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001024/* Total number of image pages */
1025static unsigned int nr_copy_pages;
1026/* Number of pages needed for saving the original pfns of the image pages */
1027static unsigned int nr_meta_pages;
1028
Rafael J. Wysocki25761b62005-10-30 14:59:56 -08001029/**
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001030 * swsusp_free - free pages allocated for the suspend.
Rafael J. Wysockicd560bb2006-09-25 23:32:50 -07001031 *
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001032 * Suspend pages are alocated before the atomic copy is made, so we
1033 * need to release them after the resume.
Rafael J. Wysocki25761b62005-10-30 14:59:56 -08001034 */
1035
1036void swsusp_free(void)
1037{
1038 struct zone *zone;
Rafael J. Wysockiae83c5ee2006-09-25 23:32:45 -07001039 unsigned long pfn, max_zone_pfn;
Rafael J. Wysocki25761b62005-10-30 14:59:56 -08001040
1041 for_each_zone(zone) {
Rafael J. Wysockiae83c5ee2006-09-25 23:32:45 -07001042 max_zone_pfn = zone->zone_start_pfn + zone->spanned_pages;
1043 for (pfn = zone->zone_start_pfn; pfn < max_zone_pfn; pfn++)
1044 if (pfn_valid(pfn)) {
1045 struct page *page = pfn_to_page(pfn);
1046
Rafael J. Wysocki7be98232007-05-06 14:50:42 -07001047 if (swsusp_page_is_forbidden(page) &&
1048 swsusp_page_is_free(page)) {
1049 swsusp_unset_page_forbidden(page);
1050 swsusp_unset_page_free(page);
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001051 __free_page(page);
Rafael J. Wysocki25761b62005-10-30 14:59:56 -08001052 }
1053 }
1054 }
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001055 nr_copy_pages = 0;
1056 nr_meta_pages = 0;
Rafael J. Wysocki75534b52006-09-25 23:32:52 -07001057 restore_pblist = NULL;
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -08001058 buffer = NULL;
Rafael J. Wysocki25761b62005-10-30 14:59:56 -08001059}
1060
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001061#ifdef CONFIG_HIGHMEM
1062/**
1063 * count_pages_for_highmem - compute the number of non-highmem pages
1064 * that will be necessary for creating copies of highmem pages.
1065 */
1066
1067static unsigned int count_pages_for_highmem(unsigned int nr_highmem)
1068{
1069 unsigned int free_highmem = count_free_highmem_pages();
1070
1071 if (free_highmem >= nr_highmem)
1072 nr_highmem = 0;
1073 else
1074 nr_highmem -= free_highmem;
1075
1076 return nr_highmem;
1077}
1078#else
1079static unsigned int
1080count_pages_for_highmem(unsigned int nr_highmem) { return 0; }
1081#endif /* CONFIG_HIGHMEM */
Rafael J. Wysocki25761b62005-10-30 14:59:56 -08001082
1083/**
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001084 * enough_free_mem - Make sure we have enough free memory for the
1085 * snapshot image.
Rafael J. Wysocki25761b62005-10-30 14:59:56 -08001086 */
1087
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001088static int enough_free_mem(unsigned int nr_pages, unsigned int nr_highmem)
Rafael J. Wysocki25761b62005-10-30 14:59:56 -08001089{
Rafael J. Wysockie5e2fa72006-01-06 00:14:20 -08001090 struct zone *zone;
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001091 unsigned int free = 0, meta = 0;
Rafael J. Wysockie5e2fa72006-01-06 00:14:20 -08001092
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001093 for_each_zone(zone) {
1094 meta += snapshot_additional_pages(zone);
1095 if (!is_highmem(zone))
Christoph Lameterd23ad422007-02-10 01:43:02 -08001096 free += zone_page_state(zone, NR_FREE_PAGES);
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001097 }
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001098
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001099 nr_pages += count_pages_for_highmem(nr_highmem);
Rafael J. Wysocki23976722007-12-08 02:09:43 +01001100 pr_debug("PM: Normal pages needed: %u + %u + %u, available pages: %u\n",
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001101 nr_pages, PAGES_FOR_IO, meta, free);
1102
1103 return free > nr_pages + PAGES_FOR_IO + meta;
Rafael J. Wysocki25761b62005-10-30 14:59:56 -08001104}
1105
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001106#ifdef CONFIG_HIGHMEM
1107/**
1108 * get_highmem_buffer - if there are some highmem pages in the suspend
1109 * image, we may need the buffer to copy them and/or load their data.
1110 */
1111
1112static inline int get_highmem_buffer(int safe_needed)
1113{
1114 buffer = get_image_page(GFP_ATOMIC | __GFP_COLD, safe_needed);
1115 return buffer ? 0 : -ENOMEM;
1116}
1117
1118/**
1119 * alloc_highmem_image_pages - allocate some highmem pages for the image.
1120 * Try to allocate as many pages as needed, but if the number of free
1121 * highmem pages is lesser than that, allocate them all.
1122 */
1123
1124static inline unsigned int
1125alloc_highmem_image_pages(struct memory_bitmap *bm, unsigned int nr_highmem)
1126{
1127 unsigned int to_alloc = count_free_highmem_pages();
1128
1129 if (to_alloc > nr_highmem)
1130 to_alloc = nr_highmem;
1131
1132 nr_highmem -= to_alloc;
1133 while (to_alloc-- > 0) {
1134 struct page *page;
1135
1136 page = alloc_image_page(__GFP_HIGHMEM);
1137 memory_bm_set_bit(bm, page_to_pfn(page));
1138 }
1139 return nr_highmem;
1140}
1141#else
1142static inline int get_highmem_buffer(int safe_needed) { return 0; }
1143
1144static inline unsigned int
1145alloc_highmem_image_pages(struct memory_bitmap *bm, unsigned int n) { return 0; }
1146#endif /* CONFIG_HIGHMEM */
1147
1148/**
1149 * swsusp_alloc - allocate memory for the suspend image
1150 *
1151 * We first try to allocate as many highmem pages as there are
1152 * saveable highmem pages in the system. If that fails, we allocate
1153 * non-highmem pages for the copies of the remaining highmem ones.
1154 *
1155 * In this approach it is likely that the copies of highmem pages will
1156 * also be located in the high memory, because of the way in which
1157 * copy_data_pages() works.
1158 */
1159
Rafael J. Wysockib788db72006-09-25 23:32:54 -07001160static int
1161swsusp_alloc(struct memory_bitmap *orig_bm, struct memory_bitmap *copy_bm,
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001162 unsigned int nr_pages, unsigned int nr_highmem)
Rafael J. Wysocki054bd4c2005-11-08 21:34:39 -08001163{
Rafael J. Wysockib788db72006-09-25 23:32:54 -07001164 int error;
Rafael J. Wysocki054bd4c2005-11-08 21:34:39 -08001165
Rafael J. Wysockib788db72006-09-25 23:32:54 -07001166 error = memory_bm_create(orig_bm, GFP_ATOMIC | __GFP_COLD, PG_ANY);
1167 if (error)
1168 goto Free;
1169
1170 error = memory_bm_create(copy_bm, GFP_ATOMIC | __GFP_COLD, PG_ANY);
1171 if (error)
1172 goto Free;
1173
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001174 if (nr_highmem > 0) {
1175 error = get_highmem_buffer(PG_ANY);
1176 if (error)
1177 goto Free;
1178
1179 nr_pages += alloc_highmem_image_pages(copy_bm, nr_highmem);
1180 }
Rafael J. Wysockib788db72006-09-25 23:32:54 -07001181 while (nr_pages-- > 0) {
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001182 struct page *page = alloc_image_page(GFP_ATOMIC | __GFP_COLD);
1183
Rafael J. Wysockib788db72006-09-25 23:32:54 -07001184 if (!page)
1185 goto Free;
1186
Rafael J. Wysockib788db72006-09-25 23:32:54 -07001187 memory_bm_set_bit(copy_bm, page_to_pfn(page));
Rafael J. Wysocki054bd4c2005-11-08 21:34:39 -08001188 }
1189 return 0;
Rafael J. Wysockib788db72006-09-25 23:32:54 -07001190
Rafael J. Wysocki59a493352006-12-06 20:34:44 -08001191 Free:
Rafael J. Wysockib788db72006-09-25 23:32:54 -07001192 swsusp_free();
1193 return -ENOMEM;
Rafael J. Wysocki054bd4c2005-11-08 21:34:39 -08001194}
Rafael J. Wysocki25761b62005-10-30 14:59:56 -08001195
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001196/* Memory bitmap used for marking saveable pages (during suspend) or the
1197 * suspend image pages (during resume)
1198 */
Rafael J. Wysockib788db72006-09-25 23:32:54 -07001199static struct memory_bitmap orig_bm;
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001200/* Memory bitmap used on suspend for marking allocated pages that will contain
1201 * the copies of saveable pages. During resume it is initially used for
1202 * marking the suspend image pages, but then its set bits are duplicated in
1203 * @orig_bm and it is released. Next, on systems with high memory, it may be
1204 * used for marking "safe" highmem pages, but it has to be reinitialized for
1205 * this purpose.
Rafael J. Wysockib788db72006-09-25 23:32:54 -07001206 */
1207static struct memory_bitmap copy_bm;
Rafael J. Wysocki25761b62005-10-30 14:59:56 -08001208
Rafael J. Wysocki2e32a432005-10-30 15:00:00 -08001209asmlinkage int swsusp_save(void)
Rafael J. Wysocki25761b62005-10-30 14:59:56 -08001210{
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001211 unsigned int nr_pages, nr_highmem;
Rafael J. Wysocki25761b62005-10-30 14:59:56 -08001212
Rafael J. Wysocki23976722007-12-08 02:09:43 +01001213 printk(KERN_INFO "PM: Creating hibernation image: \n");
Rafael J. Wysocki25761b62005-10-30 14:59:56 -08001214
Christoph Lameter9f8f2172008-02-04 22:29:11 -08001215 drain_local_pages(NULL);
Rafael J. Wysockia0f49652005-10-30 14:59:57 -08001216 nr_pages = count_data_pages();
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001217 nr_highmem = count_highmem_pages();
Rafael J. Wysocki23976722007-12-08 02:09:43 +01001218 printk(KERN_INFO "PM: Need to copy %u pages\n", nr_pages + nr_highmem);
Rafael J. Wysocki25761b62005-10-30 14:59:56 -08001219
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001220 if (!enough_free_mem(nr_pages, nr_highmem)) {
Rafael J. Wysocki23976722007-12-08 02:09:43 +01001221 printk(KERN_ERR "PM: Not enough free memory\n");
Rafael J. Wysocki25761b62005-10-30 14:59:56 -08001222 return -ENOMEM;
1223 }
1224
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001225 if (swsusp_alloc(&orig_bm, &copy_bm, nr_pages, nr_highmem)) {
Rafael J. Wysocki23976722007-12-08 02:09:43 +01001226 printk(KERN_ERR "PM: Memory allocation failed\n");
Rafael J. Wysockia0f49652005-10-30 14:59:57 -08001227 return -ENOMEM;
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001228 }
Rafael J. Wysocki25761b62005-10-30 14:59:56 -08001229
1230 /* During allocating of suspend pagedir, new cold pages may appear.
1231 * Kill them.
1232 */
Christoph Lameter9f8f2172008-02-04 22:29:11 -08001233 drain_local_pages(NULL);
Rafael J. Wysockib788db72006-09-25 23:32:54 -07001234 copy_data_pages(&copy_bm, &orig_bm);
Rafael J. Wysocki25761b62005-10-30 14:59:56 -08001235
1236 /*
1237 * End of critical section. From now on, we can write to memory,
1238 * but we should not touch disk. This specially means we must _not_
1239 * touch swap space! Except we must write out our image of course.
1240 */
1241
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001242 nr_pages += nr_highmem;
Rafael J. Wysockia0f49652005-10-30 14:59:57 -08001243 nr_copy_pages = nr_pages;
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001244 nr_meta_pages = DIV_ROUND_UP(nr_pages * sizeof(long), PAGE_SIZE);
Rafael J. Wysockia0f49652005-10-30 14:59:57 -08001245
Rafael J. Wysocki23976722007-12-08 02:09:43 +01001246 printk(KERN_INFO "PM: Hibernation image created (%d pages copied)\n",
1247 nr_pages);
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001248
Rafael J. Wysocki25761b62005-10-30 14:59:56 -08001249 return 0;
1250}
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001251
Rafael J. Wysockid307c4a2007-10-18 03:04:52 -07001252#ifndef CONFIG_ARCH_HIBERNATION_HEADER
1253static int init_header_complete(struct swsusp_info *info)
1254{
1255 memcpy(&info->uts, init_utsname(), sizeof(struct new_utsname));
1256 info->version_code = LINUX_VERSION_CODE;
1257 return 0;
1258}
1259
1260static char *check_image_kernel(struct swsusp_info *info)
1261{
1262 if (info->version_code != LINUX_VERSION_CODE)
1263 return "kernel version";
1264 if (strcmp(info->uts.sysname,init_utsname()->sysname))
1265 return "system type";
1266 if (strcmp(info->uts.release,init_utsname()->release))
1267 return "kernel release";
1268 if (strcmp(info->uts.version,init_utsname()->version))
1269 return "version";
1270 if (strcmp(info->uts.machine,init_utsname()->machine))
1271 return "machine";
1272 return NULL;
1273}
1274#endif /* CONFIG_ARCH_HIBERNATION_HEADER */
1275
Rafael J. Wysockiaf508b32007-10-26 00:59:31 +02001276unsigned long snapshot_get_image_size(void)
1277{
1278 return nr_copy_pages + nr_meta_pages + 1;
1279}
1280
Rafael J. Wysockid307c4a2007-10-18 03:04:52 -07001281static int init_header(struct swsusp_info *info)
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001282{
1283 memset(info, 0, sizeof(struct swsusp_info));
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001284 info->num_physpages = num_physpages;
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001285 info->image_pages = nr_copy_pages;
Rafael J. Wysockiaf508b32007-10-26 00:59:31 +02001286 info->pages = snapshot_get_image_size();
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -08001287 info->size = info->pages;
1288 info->size <<= PAGE_SHIFT;
Rafael J. Wysockid307c4a2007-10-18 03:04:52 -07001289 return init_header_complete(info);
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001290}
1291
1292/**
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001293 * pack_pfns - pfns corresponding to the set bits found in the bitmap @bm
1294 * are stored in the array @buf[] (1 page at a time)
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001295 */
1296
Rafael J. Wysockib788db72006-09-25 23:32:54 -07001297static inline void
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001298pack_pfns(unsigned long *buf, struct memory_bitmap *bm)
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001299{
1300 int j;
1301
Rafael J. Wysockib788db72006-09-25 23:32:54 -07001302 for (j = 0; j < PAGE_SIZE / sizeof(long); j++) {
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001303 buf[j] = memory_bm_next_pfn(bm);
1304 if (unlikely(buf[j] == BM_END_OF_MAP))
Rafael J. Wysockib788db72006-09-25 23:32:54 -07001305 break;
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001306 }
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001307}
1308
1309/**
1310 * snapshot_read_next - used for reading the system memory snapshot.
1311 *
1312 * On the first call to it @handle should point to a zeroed
1313 * snapshot_handle structure. The structure gets updated and a pointer
1314 * to it should be passed to this function every next time.
1315 *
1316 * The @count parameter should contain the number of bytes the caller
1317 * wants to read from the snapshot. It must not be zero.
1318 *
1319 * On success the function returns a positive number. Then, the caller
1320 * is allowed to read up to the returned number of bytes from the memory
1321 * location computed by the data_of() macro. The number returned
1322 * may be smaller than @count, but this only happens if the read would
1323 * cross a page boundary otherwise.
1324 *
1325 * The function returns 0 to indicate the end of data stream condition,
1326 * and a negative number is returned on error. In such cases the
1327 * structure pointed to by @handle is not updated and should not be used
1328 * any more.
1329 */
1330
1331int snapshot_read_next(struct snapshot_handle *handle, size_t count)
1332{
Rafael J. Wysockifb13a282006-09-25 23:32:46 -07001333 if (handle->cur > nr_meta_pages + nr_copy_pages)
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001334 return 0;
Rafael J. Wysockib788db72006-09-25 23:32:54 -07001335
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001336 if (!buffer) {
1337 /* This makes the buffer be freed by swsusp_free() */
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001338 buffer = get_image_page(GFP_ATOMIC, PG_ANY);
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001339 if (!buffer)
1340 return -ENOMEM;
1341 }
1342 if (!handle->offset) {
Rafael J. Wysockid307c4a2007-10-18 03:04:52 -07001343 int error;
1344
1345 error = init_header((struct swsusp_info *)buffer);
1346 if (error)
1347 return error;
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001348 handle->buffer = buffer;
Rafael J. Wysockib788db72006-09-25 23:32:54 -07001349 memory_bm_position_reset(&orig_bm);
1350 memory_bm_position_reset(&copy_bm);
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001351 }
Rafael J. Wysockifb13a282006-09-25 23:32:46 -07001352 if (handle->prev < handle->cur) {
1353 if (handle->cur <= nr_meta_pages) {
Rafael J. Wysockib788db72006-09-25 23:32:54 -07001354 memset(buffer, 0, PAGE_SIZE);
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001355 pack_pfns(buffer, &orig_bm);
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001356 } else {
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001357 struct page *page;
Rafael J. Wysockib788db72006-09-25 23:32:54 -07001358
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001359 page = pfn_to_page(memory_bm_next_pfn(&copy_bm));
1360 if (PageHighMem(page)) {
1361 /* Highmem pages are copied to the buffer,
1362 * because we can't return with a kmapped
1363 * highmem page (we may not be called again).
1364 */
1365 void *kaddr;
1366
1367 kaddr = kmap_atomic(page, KM_USER0);
1368 memcpy(buffer, kaddr, PAGE_SIZE);
1369 kunmap_atomic(kaddr, KM_USER0);
1370 handle->buffer = buffer;
1371 } else {
1372 handle->buffer = page_address(page);
1373 }
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001374 }
Rafael J. Wysockifb13a282006-09-25 23:32:46 -07001375 handle->prev = handle->cur;
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001376 }
Rafael J. Wysockifb13a282006-09-25 23:32:46 -07001377 handle->buf_offset = handle->cur_offset;
1378 if (handle->cur_offset + count >= PAGE_SIZE) {
1379 count = PAGE_SIZE - handle->cur_offset;
1380 handle->cur_offset = 0;
1381 handle->cur++;
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001382 } else {
Rafael J. Wysockifb13a282006-09-25 23:32:46 -07001383 handle->cur_offset += count;
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001384 }
1385 handle->offset += count;
1386 return count;
1387}
1388
1389/**
1390 * mark_unsafe_pages - mark the pages that cannot be used for storing
1391 * the image during resume, because they conflict with the pages that
1392 * had been used before suspend
1393 */
1394
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001395static int mark_unsafe_pages(struct memory_bitmap *bm)
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001396{
1397 struct zone *zone;
Rafael J. Wysockiae83c5ee2006-09-25 23:32:45 -07001398 unsigned long pfn, max_zone_pfn;
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001399
1400 /* Clear page flags */
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001401 for_each_zone(zone) {
Rafael J. Wysockiae83c5ee2006-09-25 23:32:45 -07001402 max_zone_pfn = zone->zone_start_pfn + zone->spanned_pages;
1403 for (pfn = zone->zone_start_pfn; pfn < max_zone_pfn; pfn++)
1404 if (pfn_valid(pfn))
Rafael J. Wysocki7be98232007-05-06 14:50:42 -07001405 swsusp_unset_page_free(pfn_to_page(pfn));
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001406 }
1407
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001408 /* Mark pages that correspond to the "original" pfns as "unsafe" */
1409 memory_bm_position_reset(bm);
1410 do {
1411 pfn = memory_bm_next_pfn(bm);
1412 if (likely(pfn != BM_END_OF_MAP)) {
1413 if (likely(pfn_valid(pfn)))
Rafael J. Wysocki7be98232007-05-06 14:50:42 -07001414 swsusp_set_page_free(pfn_to_page(pfn));
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001415 else
1416 return -EFAULT;
1417 }
1418 } while (pfn != BM_END_OF_MAP);
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001419
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001420 allocated_unsafe_pages = 0;
Rafael J. Wysocki968808b82006-06-23 02:04:48 -07001421
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001422 return 0;
1423}
1424
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001425static void
1426duplicate_memory_bitmap(struct memory_bitmap *dst, struct memory_bitmap *src)
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001427{
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001428 unsigned long pfn;
1429
1430 memory_bm_position_reset(src);
1431 pfn = memory_bm_next_pfn(src);
1432 while (pfn != BM_END_OF_MAP) {
1433 memory_bm_set_bit(dst, pfn);
1434 pfn = memory_bm_next_pfn(src);
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001435 }
1436}
1437
Rafael J. Wysockid307c4a2007-10-18 03:04:52 -07001438static int check_header(struct swsusp_info *info)
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001439{
Rafael J. Wysockid307c4a2007-10-18 03:04:52 -07001440 char *reason;
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001441
Rafael J. Wysockid307c4a2007-10-18 03:04:52 -07001442 reason = check_image_kernel(info);
1443 if (!reason && info->num_physpages != num_physpages)
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001444 reason = "memory size";
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001445 if (reason) {
Rafael J. Wysocki23976722007-12-08 02:09:43 +01001446 printk(KERN_ERR "PM: Image mismatch: %s\n", reason);
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001447 return -EPERM;
1448 }
1449 return 0;
1450}
1451
1452/**
1453 * load header - check the image header and copy data from it
1454 */
1455
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001456static int
1457load_header(struct swsusp_info *info)
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001458{
1459 int error;
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001460
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001461 restore_pblist = NULL;
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001462 error = check_header(info);
1463 if (!error) {
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001464 nr_copy_pages = info->image_pages;
1465 nr_meta_pages = info->pages - info->image_pages - 1;
1466 }
1467 return error;
1468}
1469
1470/**
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001471 * unpack_orig_pfns - for each element of @buf[] (1 page at a time) set
1472 * the corresponding bit in the memory bitmap @bm
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001473 */
Rafael J. Wysocki69643272008-11-11 21:32:44 +01001474static int unpack_orig_pfns(unsigned long *buf, struct memory_bitmap *bm)
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001475{
1476 int j;
1477
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001478 for (j = 0; j < PAGE_SIZE / sizeof(long); j++) {
1479 if (unlikely(buf[j] == BM_END_OF_MAP))
1480 break;
1481
Rafael J. Wysocki69643272008-11-11 21:32:44 +01001482 if (memory_bm_pfn_present(bm, buf[j]))
1483 memory_bm_set_bit(bm, buf[j]);
1484 else
1485 return -EFAULT;
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001486 }
Rafael J. Wysocki69643272008-11-11 21:32:44 +01001487
1488 return 0;
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001489}
1490
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001491/* List of "safe" pages that may be used to store data loaded from the suspend
1492 * image
1493 */
1494static struct linked_page *safe_pages_list;
1495
1496#ifdef CONFIG_HIGHMEM
1497/* struct highmem_pbe is used for creating the list of highmem pages that
1498 * should be restored atomically during the resume from disk, because the page
1499 * frames they have occupied before the suspend are in use.
1500 */
1501struct highmem_pbe {
1502 struct page *copy_page; /* data is here now */
1503 struct page *orig_page; /* data was here before the suspend */
1504 struct highmem_pbe *next;
1505};
1506
1507/* List of highmem PBEs needed for restoring the highmem pages that were
1508 * allocated before the suspend and included in the suspend image, but have
1509 * also been allocated by the "resume" kernel, so their contents cannot be
1510 * written directly to their "original" page frames.
1511 */
1512static struct highmem_pbe *highmem_pblist;
1513
1514/**
1515 * count_highmem_image_pages - compute the number of highmem pages in the
1516 * suspend image. The bits in the memory bitmap @bm that correspond to the
1517 * image pages are assumed to be set.
1518 */
1519
1520static unsigned int count_highmem_image_pages(struct memory_bitmap *bm)
1521{
1522 unsigned long pfn;
1523 unsigned int cnt = 0;
1524
1525 memory_bm_position_reset(bm);
1526 pfn = memory_bm_next_pfn(bm);
1527 while (pfn != BM_END_OF_MAP) {
1528 if (PageHighMem(pfn_to_page(pfn)))
1529 cnt++;
1530
1531 pfn = memory_bm_next_pfn(bm);
1532 }
1533 return cnt;
1534}
1535
1536/**
1537 * prepare_highmem_image - try to allocate as many highmem pages as
1538 * there are highmem image pages (@nr_highmem_p points to the variable
1539 * containing the number of highmem image pages). The pages that are
1540 * "safe" (ie. will not be overwritten when the suspend image is
1541 * restored) have the corresponding bits set in @bm (it must be
1542 * unitialized).
1543 *
1544 * NOTE: This function should not be called if there are no highmem
1545 * image pages.
1546 */
1547
1548static unsigned int safe_highmem_pages;
1549
1550static struct memory_bitmap *safe_highmem_bm;
1551
1552static int
1553prepare_highmem_image(struct memory_bitmap *bm, unsigned int *nr_highmem_p)
1554{
1555 unsigned int to_alloc;
1556
1557 if (memory_bm_create(bm, GFP_ATOMIC, PG_SAFE))
1558 return -ENOMEM;
1559
1560 if (get_highmem_buffer(PG_SAFE))
1561 return -ENOMEM;
1562
1563 to_alloc = count_free_highmem_pages();
1564 if (to_alloc > *nr_highmem_p)
1565 to_alloc = *nr_highmem_p;
1566 else
1567 *nr_highmem_p = to_alloc;
1568
1569 safe_highmem_pages = 0;
1570 while (to_alloc-- > 0) {
1571 struct page *page;
1572
1573 page = alloc_page(__GFP_HIGHMEM);
Rafael J. Wysocki7be98232007-05-06 14:50:42 -07001574 if (!swsusp_page_is_free(page)) {
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001575 /* The page is "safe", set its bit the bitmap */
1576 memory_bm_set_bit(bm, page_to_pfn(page));
1577 safe_highmem_pages++;
1578 }
1579 /* Mark the page as allocated */
Rafael J. Wysocki7be98232007-05-06 14:50:42 -07001580 swsusp_set_page_forbidden(page);
1581 swsusp_set_page_free(page);
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001582 }
1583 memory_bm_position_reset(bm);
1584 safe_highmem_bm = bm;
1585 return 0;
1586}
1587
1588/**
1589 * get_highmem_page_buffer - for given highmem image page find the buffer
1590 * that suspend_write_next() should set for its caller to write to.
1591 *
1592 * If the page is to be saved to its "original" page frame or a copy of
1593 * the page is to be made in the highmem, @buffer is returned. Otherwise,
1594 * the copy of the page is to be made in normal memory, so the address of
1595 * the copy is returned.
1596 *
1597 * If @buffer is returned, the caller of suspend_write_next() will write
1598 * the page's contents to @buffer, so they will have to be copied to the
1599 * right location on the next call to suspend_write_next() and it is done
1600 * with the help of copy_last_highmem_page(). For this purpose, if
1601 * @buffer is returned, @last_highmem page is set to the page to which
1602 * the data will have to be copied from @buffer.
1603 */
1604
1605static struct page *last_highmem_page;
1606
1607static void *
1608get_highmem_page_buffer(struct page *page, struct chain_allocator *ca)
1609{
1610 struct highmem_pbe *pbe;
1611 void *kaddr;
1612
Rafael J. Wysocki7be98232007-05-06 14:50:42 -07001613 if (swsusp_page_is_forbidden(page) && swsusp_page_is_free(page)) {
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001614 /* We have allocated the "original" page frame and we can
1615 * use it directly to store the loaded page.
1616 */
1617 last_highmem_page = page;
1618 return buffer;
1619 }
1620 /* The "original" page frame has not been allocated and we have to
1621 * use a "safe" page frame to store the loaded page.
1622 */
1623 pbe = chain_alloc(ca, sizeof(struct highmem_pbe));
1624 if (!pbe) {
1625 swsusp_free();
Rafael J. Wysocki69643272008-11-11 21:32:44 +01001626 return ERR_PTR(-ENOMEM);
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001627 }
1628 pbe->orig_page = page;
1629 if (safe_highmem_pages > 0) {
1630 struct page *tmp;
1631
1632 /* Copy of the page will be stored in high memory */
1633 kaddr = buffer;
1634 tmp = pfn_to_page(memory_bm_next_pfn(safe_highmem_bm));
1635 safe_highmem_pages--;
1636 last_highmem_page = tmp;
1637 pbe->copy_page = tmp;
1638 } else {
1639 /* Copy of the page will be stored in normal memory */
1640 kaddr = safe_pages_list;
1641 safe_pages_list = safe_pages_list->next;
1642 pbe->copy_page = virt_to_page(kaddr);
1643 }
1644 pbe->next = highmem_pblist;
1645 highmem_pblist = pbe;
1646 return kaddr;
1647}
1648
1649/**
1650 * copy_last_highmem_page - copy the contents of a highmem image from
1651 * @buffer, where the caller of snapshot_write_next() has place them,
1652 * to the right location represented by @last_highmem_page .
1653 */
1654
1655static void copy_last_highmem_page(void)
1656{
1657 if (last_highmem_page) {
1658 void *dst;
1659
1660 dst = kmap_atomic(last_highmem_page, KM_USER0);
1661 memcpy(dst, buffer, PAGE_SIZE);
1662 kunmap_atomic(dst, KM_USER0);
1663 last_highmem_page = NULL;
1664 }
1665}
1666
1667static inline int last_highmem_page_copied(void)
1668{
1669 return !last_highmem_page;
1670}
1671
1672static inline void free_highmem_data(void)
1673{
1674 if (safe_highmem_bm)
1675 memory_bm_free(safe_highmem_bm, PG_UNSAFE_CLEAR);
1676
1677 if (buffer)
1678 free_image_page(buffer, PG_UNSAFE_CLEAR);
1679}
1680#else
1681static inline int get_safe_write_buffer(void) { return 0; }
1682
1683static unsigned int
1684count_highmem_image_pages(struct memory_bitmap *bm) { return 0; }
1685
1686static inline int
1687prepare_highmem_image(struct memory_bitmap *bm, unsigned int *nr_highmem_p)
1688{
1689 return 0;
1690}
1691
1692static inline void *
1693get_highmem_page_buffer(struct page *page, struct chain_allocator *ca)
1694{
Rafael J. Wysocki69643272008-11-11 21:32:44 +01001695 return ERR_PTR(-EINVAL);
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001696}
1697
1698static inline void copy_last_highmem_page(void) {}
1699static inline int last_highmem_page_copied(void) { return 1; }
1700static inline void free_highmem_data(void) {}
1701#endif /* CONFIG_HIGHMEM */
1702
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001703/**
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001704 * prepare_image - use the memory bitmap @bm to mark the pages that will
1705 * be overwritten in the process of restoring the system memory state
1706 * from the suspend image ("unsafe" pages) and allocate memory for the
1707 * image.
Rafael J. Wysocki968808b82006-06-23 02:04:48 -07001708 *
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001709 * The idea is to allocate a new memory bitmap first and then allocate
1710 * as many pages as needed for the image data, but not to assign these
1711 * pages to specific tasks initially. Instead, we just mark them as
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001712 * allocated and create a lists of "safe" pages that will be used
1713 * later. On systems with high memory a list of "safe" highmem pages is
1714 * also created.
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001715 */
1716
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001717#define PBES_PER_LINKED_PAGE (LINKED_PAGE_DATA_SIZE / sizeof(struct pbe))
Rafael J. Wysocki968808b82006-06-23 02:04:48 -07001718
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001719static int
1720prepare_image(struct memory_bitmap *new_bm, struct memory_bitmap *bm)
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001721{
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001722 unsigned int nr_pages, nr_highmem;
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001723 struct linked_page *sp_list, *lp;
1724 int error;
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001725
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001726 /* If there is no highmem, the buffer will not be necessary */
1727 free_image_page(buffer, PG_UNSAFE_CLEAR);
1728 buffer = NULL;
1729
1730 nr_highmem = count_highmem_image_pages(bm);
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001731 error = mark_unsafe_pages(bm);
1732 if (error)
1733 goto Free;
1734
1735 error = memory_bm_create(new_bm, GFP_ATOMIC, PG_SAFE);
1736 if (error)
1737 goto Free;
1738
1739 duplicate_memory_bitmap(new_bm, bm);
1740 memory_bm_free(bm, PG_UNSAFE_KEEP);
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001741 if (nr_highmem > 0) {
1742 error = prepare_highmem_image(bm, &nr_highmem);
1743 if (error)
1744 goto Free;
1745 }
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001746 /* Reserve some safe pages for potential later use.
1747 *
1748 * NOTE: This way we make sure there will be enough safe pages for the
1749 * chain_alloc() in get_buffer(). It is a bit wasteful, but
1750 * nr_copy_pages cannot be greater than 50% of the memory anyway.
1751 */
1752 sp_list = NULL;
1753 /* nr_copy_pages cannot be lesser than allocated_unsafe_pages */
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001754 nr_pages = nr_copy_pages - nr_highmem - allocated_unsafe_pages;
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001755 nr_pages = DIV_ROUND_UP(nr_pages, PBES_PER_LINKED_PAGE);
1756 while (nr_pages > 0) {
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001757 lp = get_image_page(GFP_ATOMIC, PG_SAFE);
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001758 if (!lp) {
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001759 error = -ENOMEM;
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001760 goto Free;
Rafael J. Wysocki968808b82006-06-23 02:04:48 -07001761 }
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001762 lp->next = sp_list;
1763 sp_list = lp;
1764 nr_pages--;
Rafael J. Wysocki968808b82006-06-23 02:04:48 -07001765 }
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001766 /* Preallocate memory for the image */
1767 safe_pages_list = NULL;
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001768 nr_pages = nr_copy_pages - nr_highmem - allocated_unsafe_pages;
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001769 while (nr_pages > 0) {
1770 lp = (struct linked_page *)get_zeroed_page(GFP_ATOMIC);
1771 if (!lp) {
1772 error = -ENOMEM;
1773 goto Free;
1774 }
Rafael J. Wysocki7be98232007-05-06 14:50:42 -07001775 if (!swsusp_page_is_free(virt_to_page(lp))) {
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001776 /* The page is "safe", add it to the list */
1777 lp->next = safe_pages_list;
1778 safe_pages_list = lp;
1779 }
1780 /* Mark the page as allocated */
Rafael J. Wysocki7be98232007-05-06 14:50:42 -07001781 swsusp_set_page_forbidden(virt_to_page(lp));
1782 swsusp_set_page_free(virt_to_page(lp));
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001783 nr_pages--;
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001784 }
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001785 /* Free the reserved safe pages so that chain_alloc() can use them */
1786 while (sp_list) {
1787 lp = sp_list->next;
1788 free_image_page(sp_list, PG_UNSAFE_CLEAR);
1789 sp_list = lp;
1790 }
1791 return 0;
1792
Rafael J. Wysocki59a493352006-12-06 20:34:44 -08001793 Free:
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001794 swsusp_free();
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001795 return error;
1796}
1797
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001798/**
1799 * get_buffer - compute the address that snapshot_write_next() should
1800 * set for its caller to write to.
1801 */
Rafael J. Wysocki968808b82006-06-23 02:04:48 -07001802
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001803static void *get_buffer(struct memory_bitmap *bm, struct chain_allocator *ca)
1804{
1805 struct pbe *pbe;
Rafael J. Wysocki69643272008-11-11 21:32:44 +01001806 struct page *page;
1807 unsigned long pfn = memory_bm_next_pfn(bm);
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001808
Rafael J. Wysocki69643272008-11-11 21:32:44 +01001809 if (pfn == BM_END_OF_MAP)
1810 return ERR_PTR(-EFAULT);
1811
1812 page = pfn_to_page(pfn);
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001813 if (PageHighMem(page))
1814 return get_highmem_page_buffer(page, ca);
1815
Rafael J. Wysocki7be98232007-05-06 14:50:42 -07001816 if (swsusp_page_is_forbidden(page) && swsusp_page_is_free(page))
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001817 /* We have allocated the "original" page frame and we can
1818 * use it directly to store the loaded page.
Rafael J. Wysocki968808b82006-06-23 02:04:48 -07001819 */
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001820 return page_address(page);
1821
1822 /* The "original" page frame has not been allocated and we have to
1823 * use a "safe" page frame to store the loaded page.
Rafael J. Wysocki968808b82006-06-23 02:04:48 -07001824 */
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001825 pbe = chain_alloc(ca, sizeof(struct pbe));
1826 if (!pbe) {
1827 swsusp_free();
Rafael J. Wysocki69643272008-11-11 21:32:44 +01001828 return ERR_PTR(-ENOMEM);
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001829 }
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001830 pbe->orig_address = page_address(page);
1831 pbe->address = safe_pages_list;
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001832 safe_pages_list = safe_pages_list->next;
1833 pbe->next = restore_pblist;
1834 restore_pblist = pbe;
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001835 return pbe->address;
Rafael J. Wysocki968808b82006-06-23 02:04:48 -07001836}
1837
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001838/**
1839 * snapshot_write_next - used for writing the system memory snapshot.
1840 *
1841 * On the first call to it @handle should point to a zeroed
1842 * snapshot_handle structure. The structure gets updated and a pointer
1843 * to it should be passed to this function every next time.
1844 *
1845 * The @count parameter should contain the number of bytes the caller
1846 * wants to write to the image. It must not be zero.
1847 *
1848 * On success the function returns a positive number. Then, the caller
1849 * is allowed to write up to the returned number of bytes to the memory
1850 * location computed by the data_of() macro. The number returned
1851 * may be smaller than @count, but this only happens if the write would
1852 * cross a page boundary otherwise.
1853 *
1854 * The function returns 0 to indicate the "end of file" condition,
1855 * and a negative number is returned on error. In such cases the
1856 * structure pointed to by @handle is not updated and should not be used
1857 * any more.
1858 */
1859
1860int snapshot_write_next(struct snapshot_handle *handle, size_t count)
1861{
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001862 static struct chain_allocator ca;
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001863 int error = 0;
1864
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001865 /* Check if we have already loaded the entire image */
Rafael J. Wysockifb13a282006-09-25 23:32:46 -07001866 if (handle->prev && handle->cur > nr_meta_pages + nr_copy_pages)
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001867 return 0;
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001868
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001869 if (handle->offset == 0) {
1870 if (!buffer)
1871 /* This makes the buffer be freed by swsusp_free() */
1872 buffer = get_image_page(GFP_ATOMIC, PG_ANY);
1873
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001874 if (!buffer)
1875 return -ENOMEM;
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001876
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001877 handle->buffer = buffer;
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001878 }
Andrew Morton546e0d22006-09-25 23:32:44 -07001879 handle->sync_read = 1;
Rafael J. Wysockifb13a282006-09-25 23:32:46 -07001880 if (handle->prev < handle->cur) {
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001881 if (handle->prev == 0) {
1882 error = load_header(buffer);
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001883 if (error)
1884 return error;
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001885
1886 error = memory_bm_create(&copy_bm, GFP_ATOMIC, PG_ANY);
1887 if (error)
1888 return error;
1889
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001890 } else if (handle->prev <= nr_meta_pages) {
Rafael J. Wysocki69643272008-11-11 21:32:44 +01001891 error = unpack_orig_pfns(buffer, &copy_bm);
1892 if (error)
1893 return error;
1894
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001895 if (handle->prev == nr_meta_pages) {
1896 error = prepare_image(&orig_bm, &copy_bm);
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001897 if (error)
1898 return error;
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001899
1900 chain_init(&ca, GFP_ATOMIC, PG_SAFE);
1901 memory_bm_position_reset(&orig_bm);
1902 restore_pblist = NULL;
1903 handle->buffer = get_buffer(&orig_bm, &ca);
Andrew Morton546e0d22006-09-25 23:32:44 -07001904 handle->sync_read = 0;
Rafael J. Wysocki69643272008-11-11 21:32:44 +01001905 if (IS_ERR(handle->buffer))
1906 return PTR_ERR(handle->buffer);
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001907 }
1908 } else {
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001909 copy_last_highmem_page();
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001910 handle->buffer = get_buffer(&orig_bm, &ca);
Rafael J. Wysocki69643272008-11-11 21:32:44 +01001911 if (IS_ERR(handle->buffer))
1912 return PTR_ERR(handle->buffer);
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001913 if (handle->buffer != buffer)
1914 handle->sync_read = 0;
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001915 }
Rafael J. Wysockifb13a282006-09-25 23:32:46 -07001916 handle->prev = handle->cur;
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001917 }
Rafael J. Wysockifb13a282006-09-25 23:32:46 -07001918 handle->buf_offset = handle->cur_offset;
1919 if (handle->cur_offset + count >= PAGE_SIZE) {
1920 count = PAGE_SIZE - handle->cur_offset;
1921 handle->cur_offset = 0;
1922 handle->cur++;
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001923 } else {
Rafael J. Wysockifb13a282006-09-25 23:32:46 -07001924 handle->cur_offset += count;
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001925 }
1926 handle->offset += count;
1927 return count;
1928}
1929
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001930/**
1931 * snapshot_write_finalize - must be called after the last call to
1932 * snapshot_write_next() in case the last page in the image happens
1933 * to be a highmem page and its contents should be stored in the
1934 * highmem. Additionally, it releases the memory that will not be
1935 * used any more.
1936 */
1937
1938void snapshot_write_finalize(struct snapshot_handle *handle)
1939{
1940 copy_last_highmem_page();
1941 /* Free only if we have loaded the image entirely */
1942 if (handle->prev && handle->cur > nr_meta_pages + nr_copy_pages) {
1943 memory_bm_free(&orig_bm, PG_UNSAFE_CLEAR);
1944 free_highmem_data();
1945 }
1946}
1947
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001948int snapshot_image_loaded(struct snapshot_handle *handle)
1949{
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001950 return !(!nr_copy_pages || !last_highmem_page_copied() ||
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001951 handle->cur <= nr_meta_pages + nr_copy_pages);
1952}
1953
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001954#ifdef CONFIG_HIGHMEM
1955/* Assumes that @buf is ready and points to a "safe" page */
1956static inline void
1957swap_two_pages_data(struct page *p1, struct page *p2, void *buf)
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001958{
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001959 void *kaddr1, *kaddr2;
1960
1961 kaddr1 = kmap_atomic(p1, KM_USER0);
1962 kaddr2 = kmap_atomic(p2, KM_USER1);
1963 memcpy(buf, kaddr1, PAGE_SIZE);
1964 memcpy(kaddr1, kaddr2, PAGE_SIZE);
1965 memcpy(kaddr2, buf, PAGE_SIZE);
1966 kunmap_atomic(kaddr1, KM_USER0);
1967 kunmap_atomic(kaddr2, KM_USER1);
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001968}
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001969
1970/**
1971 * restore_highmem - for each highmem page that was allocated before
1972 * the suspend and included in the suspend image, and also has been
1973 * allocated by the "resume" kernel swap its current (ie. "before
1974 * resume") contents with the previous (ie. "before suspend") one.
1975 *
1976 * If the resume eventually fails, we can call this function once
1977 * again and restore the "before resume" highmem state.
1978 */
1979
1980int restore_highmem(void)
1981{
1982 struct highmem_pbe *pbe = highmem_pblist;
1983 void *buf;
1984
1985 if (!pbe)
1986 return 0;
1987
1988 buf = get_image_page(GFP_ATOMIC, PG_SAFE);
1989 if (!buf)
1990 return -ENOMEM;
1991
1992 while (pbe) {
1993 swap_two_pages_data(pbe->copy_page, pbe->orig_page, buf);
1994 pbe = pbe->next;
1995 }
1996 free_image_page(buf, PG_UNSAFE_CLEAR);
1997 return 0;
1998}
1999#endif /* CONFIG_HIGHMEM */