blob: 5e7edfb05e66cff0d2c99d5fc8fddfde03e372c3 [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 *
Pavel Macheka2531292010-07-18 14:27:13 +02006 * Copyright (C) 1998-2005 Pavel Machek <pavel@ucw.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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090029#include <linux/slab.h>
Rafael J. Wysocki25761b62005-10-30 14:59:56 -080030
31#include <asm/uaccess.h>
32#include <asm/mmu_context.h>
33#include <asm/pgtable.h>
34#include <asm/tlbflush.h>
35#include <asm/io.h>
36
Rafael J. Wysocki25761b62005-10-30 14:59:56 -080037#include "power.h"
38
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -070039static int swsusp_page_is_free(struct page *);
40static void swsusp_set_page_forbidden(struct page *);
41static void swsusp_unset_page_forbidden(struct page *);
42
Rafael J. Wysockife419532009-06-11 23:11:17 +020043/*
44 * Preferred image size in bytes (tunable via /sys/power/image_size).
45 * When it is set to N, swsusp will do its best to ensure the image
46 * size will not exceed N bytes, but if that is impossible, it will
47 * try to create the smallest image possible.
48 */
49unsigned long image_size = 500 * 1024 * 1024;
50
Rafael J. Wysocki83573762006-12-06 20:34:18 -080051/* List of PBEs needed for restoring the pages that were allocated before
52 * the suspend and included in the suspend image, but have also been
53 * allocated by the "resume" kernel, so their contents cannot be written
54 * directly to their "original" page frames.
55 */
Rafael J. Wysocki75534b52006-09-25 23:32:52 -070056struct pbe *restore_pblist;
57
Rafael J. Wysocki83573762006-12-06 20:34:18 -080058/* Pointer to an auxiliary buffer (1 page) */
Rafael J. Wysocki940864d2006-09-25 23:32:55 -070059static void *buffer;
Rafael J. Wysocki7088a5c2006-01-06 00:13:05 -080060
Rafael J. Wysockif6143aa2006-09-25 23:32:50 -070061/**
62 * @safe_needed - on resume, for storing the PBE list and the image,
63 * we can only use memory pages that do not conflict with the pages
Rafael J. Wysocki83573762006-12-06 20:34:18 -080064 * used before suspend. The unsafe pages have PageNosaveFree set
65 * and we count them using unsafe_pages.
Rafael J. Wysockif6143aa2006-09-25 23:32:50 -070066 *
Rafael J. Wysocki83573762006-12-06 20:34:18 -080067 * Each allocated image page is marked as PageNosave and PageNosaveFree
68 * so that swsusp_free() can release it.
Rafael J. Wysockif6143aa2006-09-25 23:32:50 -070069 */
70
Rafael J. Wysocki0bcd8882006-09-25 23:32:52 -070071#define PG_ANY 0
72#define PG_SAFE 1
73#define PG_UNSAFE_CLEAR 1
74#define PG_UNSAFE_KEEP 0
75
Rafael J. Wysocki940864d2006-09-25 23:32:55 -070076static unsigned int allocated_unsafe_pages;
Rafael J. Wysockif6143aa2006-09-25 23:32:50 -070077
Rafael J. Wysocki83573762006-12-06 20:34:18 -080078static void *get_image_page(gfp_t gfp_mask, int safe_needed)
Rafael J. Wysockif6143aa2006-09-25 23:32:50 -070079{
80 void *res;
81
82 res = (void *)get_zeroed_page(gfp_mask);
83 if (safe_needed)
Rafael J. Wysocki7be98232007-05-06 14:50:42 -070084 while (res && swsusp_page_is_free(virt_to_page(res))) {
Rafael J. Wysockif6143aa2006-09-25 23:32:50 -070085 /* The page is unsafe, mark it for swsusp_free() */
Rafael J. Wysocki7be98232007-05-06 14:50:42 -070086 swsusp_set_page_forbidden(virt_to_page(res));
Rafael J. Wysocki940864d2006-09-25 23:32:55 -070087 allocated_unsafe_pages++;
Rafael J. Wysockif6143aa2006-09-25 23:32:50 -070088 res = (void *)get_zeroed_page(gfp_mask);
89 }
90 if (res) {
Rafael J. Wysocki7be98232007-05-06 14:50:42 -070091 swsusp_set_page_forbidden(virt_to_page(res));
92 swsusp_set_page_free(virt_to_page(res));
Rafael J. Wysockif6143aa2006-09-25 23:32:50 -070093 }
94 return res;
95}
96
97unsigned long get_safe_page(gfp_t gfp_mask)
98{
Rafael J. Wysocki83573762006-12-06 20:34:18 -080099 return (unsigned long)get_image_page(gfp_mask, PG_SAFE);
100}
101
Rafael J. Wysocki5b6d15d2006-12-06 20:34:43 -0800102static struct page *alloc_image_page(gfp_t gfp_mask)
103{
Rafael J. Wysocki83573762006-12-06 20:34:18 -0800104 struct page *page;
105
106 page = alloc_page(gfp_mask);
107 if (page) {
Rafael J. Wysocki7be98232007-05-06 14:50:42 -0700108 swsusp_set_page_forbidden(page);
109 swsusp_set_page_free(page);
Rafael J. Wysocki83573762006-12-06 20:34:18 -0800110 }
111 return page;
Rafael J. Wysockif6143aa2006-09-25 23:32:50 -0700112}
113
114/**
115 * free_image_page - free page represented by @addr, allocated with
Rafael J. Wysocki83573762006-12-06 20:34:18 -0800116 * get_image_page (page flags set by it must be cleared)
Rafael J. Wysockif6143aa2006-09-25 23:32:50 -0700117 */
118
119static inline void free_image_page(void *addr, int clear_nosave_free)
120{
Rafael J. Wysocki83573762006-12-06 20:34:18 -0800121 struct page *page;
122
123 BUG_ON(!virt_addr_valid(addr));
124
125 page = virt_to_page(addr);
126
Rafael J. Wysocki7be98232007-05-06 14:50:42 -0700127 swsusp_unset_page_forbidden(page);
Rafael J. Wysockif6143aa2006-09-25 23:32:50 -0700128 if (clear_nosave_free)
Rafael J. Wysocki7be98232007-05-06 14:50:42 -0700129 swsusp_unset_page_free(page);
Rafael J. Wysocki83573762006-12-06 20:34:18 -0800130
131 __free_page(page);
Rafael J. Wysockif6143aa2006-09-25 23:32:50 -0700132}
133
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700134/* struct linked_page is used to build chains of pages */
135
136#define LINKED_PAGE_DATA_SIZE (PAGE_SIZE - sizeof(void *))
137
138struct linked_page {
139 struct linked_page *next;
140 char data[LINKED_PAGE_DATA_SIZE];
141} __attribute__((packed));
142
143static inline void
144free_list_of_pages(struct linked_page *list, int clear_page_nosave)
145{
146 while (list) {
147 struct linked_page *lp = list->next;
148
149 free_image_page(list, clear_page_nosave);
150 list = lp;
151 }
152}
153
154/**
155 * struct chain_allocator is used for allocating small objects out of
156 * a linked list of pages called 'the chain'.
157 *
158 * The chain grows each time when there is no room for a new object in
159 * the current page. The allocated objects cannot be freed individually.
160 * It is only possible to free them all at once, by freeing the entire
161 * chain.
162 *
163 * NOTE: The chain allocator may be inefficient if the allocated objects
164 * are not much smaller than PAGE_SIZE.
165 */
166
167struct chain_allocator {
168 struct linked_page *chain; /* the chain */
169 unsigned int used_space; /* total size of objects allocated out
170 * of the current page
171 */
172 gfp_t gfp_mask; /* mask for allocating pages */
173 int safe_needed; /* if set, only "safe" pages are allocated */
174};
175
176static void
177chain_init(struct chain_allocator *ca, gfp_t gfp_mask, int safe_needed)
178{
179 ca->chain = NULL;
180 ca->used_space = LINKED_PAGE_DATA_SIZE;
181 ca->gfp_mask = gfp_mask;
182 ca->safe_needed = safe_needed;
183}
184
185static void *chain_alloc(struct chain_allocator *ca, unsigned int size)
186{
187 void *ret;
188
189 if (LINKED_PAGE_DATA_SIZE - ca->used_space < size) {
190 struct linked_page *lp;
191
Rafael J. Wysocki83573762006-12-06 20:34:18 -0800192 lp = get_image_page(ca->gfp_mask, ca->safe_needed);
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700193 if (!lp)
194 return NULL;
195
196 lp->next = ca->chain;
197 ca->chain = lp;
198 ca->used_space = 0;
199 }
200 ret = ca->chain->data + ca->used_space;
201 ca->used_space += size;
202 return ret;
203}
204
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700205/**
206 * Data types related to memory bitmaps.
207 *
208 * Memory bitmap is a structure consiting of many linked lists of
209 * objects. The main list's elements are of type struct zone_bitmap
210 * and each of them corresonds to one zone. For each zone bitmap
211 * object there is a list of objects of type struct bm_block that
Akinobu Mita0d833042008-07-23 21:28:38 -0700212 * represent each blocks of bitmap in which information is stored.
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700213 *
214 * struct memory_bitmap contains a pointer to the main list of zone
215 * bitmap objects, a struct bm_position used for browsing the bitmap,
216 * and a pointer to the list of pages used for allocating all of the
217 * zone bitmap objects and bitmap block objects.
218 *
219 * NOTE: It has to be possible to lay out the bitmap in memory
220 * using only allocations of order 0. Additionally, the bitmap is
221 * designed to work with arbitrary number of zones (this is over the
222 * top for now, but let's avoid making unnecessary assumptions ;-).
223 *
224 * struct zone_bitmap contains a pointer to a list of bitmap block
225 * objects and a pointer to the bitmap block object that has been
226 * most recently used for setting bits. Additionally, it contains the
227 * pfns that correspond to the start and end of the represented zone.
228 *
229 * struct bm_block contains a pointer to the memory page in which
Akinobu Mita0d833042008-07-23 21:28:38 -0700230 * information is stored (in the form of a block of bitmap)
231 * It also contains the pfns that correspond to the start and end of
232 * the represented memory area.
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700233 */
234
235#define BM_END_OF_MAP (~0UL)
236
Wu Fengguang8de03072009-07-22 19:56:10 +0200237#define BM_BITS_PER_BLOCK (PAGE_SIZE * BITS_PER_BYTE)
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700238
239struct bm_block {
Rafael J. Wysocki846705d2008-11-26 18:00:24 -0500240 struct list_head hook; /* hook into a list of bitmap blocks */
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700241 unsigned long start_pfn; /* pfn represented by the first bit */
242 unsigned long end_pfn; /* pfn represented by the last bit plus 1 */
Akinobu Mita0d833042008-07-23 21:28:38 -0700243 unsigned long *data; /* bitmap representing pages */
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700244};
245
Akinobu Mita0d833042008-07-23 21:28:38 -0700246static inline unsigned long bm_block_bits(struct bm_block *bb)
247{
248 return bb->end_pfn - bb->start_pfn;
249}
250
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700251/* strcut bm_position is used for browsing memory bitmaps */
252
253struct bm_position {
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700254 struct bm_block *block;
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700255 int bit;
256};
257
258struct memory_bitmap {
Rafael J. Wysocki846705d2008-11-26 18:00:24 -0500259 struct list_head blocks; /* list of bitmap blocks */
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700260 struct linked_page *p_list; /* list of pages used to store zone
261 * bitmap objects and bitmap block
262 * objects
263 */
264 struct bm_position cur; /* most recently used bit position */
265};
266
267/* Functions that operate on memory bitmaps */
268
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700269static void memory_bm_position_reset(struct memory_bitmap *bm)
270{
Rafael J. Wysocki846705d2008-11-26 18:00:24 -0500271 bm->cur.block = list_entry(bm->blocks.next, struct bm_block, hook);
Akinobu Mita0d833042008-07-23 21:28:38 -0700272 bm->cur.bit = 0;
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700273}
274
275static void memory_bm_free(struct memory_bitmap *bm, int clear_nosave_free);
276
277/**
278 * create_bm_block_list - create a list of block bitmap objects
Wu Fengguang8de03072009-07-22 19:56:10 +0200279 * @pages - number of pages to track
Rafael J. Wysocki846705d2008-11-26 18:00:24 -0500280 * @list - list to put the allocated blocks into
281 * @ca - chain allocator to be used for allocating memory
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700282 */
Rafael J. Wysocki846705d2008-11-26 18:00:24 -0500283static int create_bm_block_list(unsigned long pages,
284 struct list_head *list,
285 struct chain_allocator *ca)
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700286{
Rafael J. Wysocki846705d2008-11-26 18:00:24 -0500287 unsigned int nr_blocks = DIV_ROUND_UP(pages, BM_BITS_PER_BLOCK);
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700288
289 while (nr_blocks-- > 0) {
290 struct bm_block *bb;
291
292 bb = chain_alloc(ca, sizeof(struct bm_block));
293 if (!bb)
Rafael J. Wysocki846705d2008-11-26 18:00:24 -0500294 return -ENOMEM;
295 list_add(&bb->hook, list);
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700296 }
Rafael J. Wysocki846705d2008-11-26 18:00:24 -0500297
298 return 0;
299}
300
301struct mem_extent {
302 struct list_head hook;
303 unsigned long start;
304 unsigned long end;
305};
306
307/**
308 * free_mem_extents - free a list of memory extents
309 * @list - list of extents to empty
310 */
311static void free_mem_extents(struct list_head *list)
312{
313 struct mem_extent *ext, *aux;
314
315 list_for_each_entry_safe(ext, aux, list, hook) {
316 list_del(&ext->hook);
317 kfree(ext);
318 }
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700319}
320
321/**
Rafael J. Wysocki846705d2008-11-26 18:00:24 -0500322 * create_mem_extents - create a list of memory extents representing
323 * contiguous ranges of PFNs
324 * @list - list to put the extents into
325 * @gfp_mask - mask to use for memory allocations
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700326 */
Rafael J. Wysocki846705d2008-11-26 18:00:24 -0500327static int create_mem_extents(struct list_head *list, gfp_t gfp_mask)
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700328{
Rafael J. Wysocki846705d2008-11-26 18:00:24 -0500329 struct zone *zone;
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700330
Rafael J. Wysocki846705d2008-11-26 18:00:24 -0500331 INIT_LIST_HEAD(list);
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700332
KOSAKI Motohiroee99c712009-03-31 15:19:31 -0700333 for_each_populated_zone(zone) {
Rafael J. Wysocki846705d2008-11-26 18:00:24 -0500334 unsigned long zone_start, zone_end;
335 struct mem_extent *ext, *cur, *aux;
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700336
Rafael J. Wysocki846705d2008-11-26 18:00:24 -0500337 zone_start = zone->zone_start_pfn;
338 zone_end = zone->zone_start_pfn + zone->spanned_pages;
339
340 list_for_each_entry(ext, list, hook)
341 if (zone_start <= ext->end)
342 break;
343
344 if (&ext->hook == list || zone_end < ext->start) {
345 /* New extent is necessary */
346 struct mem_extent *new_ext;
347
348 new_ext = kzalloc(sizeof(struct mem_extent), gfp_mask);
349 if (!new_ext) {
350 free_mem_extents(list);
351 return -ENOMEM;
352 }
353 new_ext->start = zone_start;
354 new_ext->end = zone_end;
355 list_add_tail(&new_ext->hook, &ext->hook);
356 continue;
357 }
358
359 /* Merge this zone's range of PFNs with the existing one */
360 if (zone_start < ext->start)
361 ext->start = zone_start;
362 if (zone_end > ext->end)
363 ext->end = zone_end;
364
365 /* More merging may be possible */
366 cur = ext;
367 list_for_each_entry_safe_continue(cur, aux, list, hook) {
368 if (zone_end < cur->start)
369 break;
370 if (zone_end < cur->end)
371 ext->end = cur->end;
372 list_del(&cur->hook);
373 kfree(cur);
374 }
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700375 }
Rafael J. Wysocki846705d2008-11-26 18:00:24 -0500376
377 return 0;
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700378}
379
380/**
381 * memory_bm_create - allocate memory for a memory bitmap
382 */
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700383static int
384memory_bm_create(struct memory_bitmap *bm, gfp_t gfp_mask, int safe_needed)
385{
386 struct chain_allocator ca;
Rafael J. Wysocki846705d2008-11-26 18:00:24 -0500387 struct list_head mem_extents;
388 struct mem_extent *ext;
389 int error;
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700390
391 chain_init(&ca, gfp_mask, safe_needed);
Rafael J. Wysocki846705d2008-11-26 18:00:24 -0500392 INIT_LIST_HEAD(&bm->blocks);
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700393
Rafael J. Wysocki846705d2008-11-26 18:00:24 -0500394 error = create_mem_extents(&mem_extents, gfp_mask);
395 if (error)
396 return error;
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700397
Rafael J. Wysocki846705d2008-11-26 18:00:24 -0500398 list_for_each_entry(ext, &mem_extents, hook) {
399 struct bm_block *bb;
400 unsigned long pfn = ext->start;
401 unsigned long pages = ext->end - ext->start;
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700402
Rafael J. Wysocki846705d2008-11-26 18:00:24 -0500403 bb = list_entry(bm->blocks.prev, struct bm_block, hook);
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700404
Rafael J. Wysocki846705d2008-11-26 18:00:24 -0500405 error = create_bm_block_list(pages, bm->blocks.prev, &ca);
406 if (error)
407 goto Error;
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700408
Rafael J. Wysocki846705d2008-11-26 18:00:24 -0500409 list_for_each_entry_continue(bb, &bm->blocks, hook) {
410 bb->data = get_image_page(gfp_mask, safe_needed);
411 if (!bb->data) {
412 error = -ENOMEM;
413 goto Error;
414 }
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700415
416 bb->start_pfn = pfn;
Rafael J. Wysocki846705d2008-11-26 18:00:24 -0500417 if (pages >= BM_BITS_PER_BLOCK) {
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700418 pfn += BM_BITS_PER_BLOCK;
Rafael J. Wysocki846705d2008-11-26 18:00:24 -0500419 pages -= BM_BITS_PER_BLOCK;
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700420 } else {
421 /* This is executed only once in the loop */
Rafael J. Wysocki846705d2008-11-26 18:00:24 -0500422 pfn += pages;
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700423 }
424 bb->end_pfn = pfn;
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700425 }
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700426 }
Rafael J. Wysocki846705d2008-11-26 18:00:24 -0500427
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700428 bm->p_list = ca.chain;
429 memory_bm_position_reset(bm);
Rafael J. Wysocki846705d2008-11-26 18:00:24 -0500430 Exit:
431 free_mem_extents(&mem_extents);
432 return error;
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700433
Rafael J. Wysocki846705d2008-11-26 18:00:24 -0500434 Error:
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700435 bm->p_list = ca.chain;
436 memory_bm_free(bm, PG_UNSAFE_CLEAR);
Rafael J. Wysocki846705d2008-11-26 18:00:24 -0500437 goto Exit;
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700438}
439
440/**
441 * memory_bm_free - free memory occupied by the memory bitmap @bm
442 */
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700443static void memory_bm_free(struct memory_bitmap *bm, int clear_nosave_free)
444{
Rafael J. Wysocki846705d2008-11-26 18:00:24 -0500445 struct bm_block *bb;
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700446
Rafael J. Wysocki846705d2008-11-26 18:00:24 -0500447 list_for_each_entry(bb, &bm->blocks, hook)
448 if (bb->data)
449 free_image_page(bb->data, clear_nosave_free);
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700450
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700451 free_list_of_pages(bm->p_list, clear_nosave_free);
Rafael J. Wysocki846705d2008-11-26 18:00:24 -0500452
453 INIT_LIST_HEAD(&bm->blocks);
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700454}
455
456/**
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -0700457 * memory_bm_find_bit - find the bit in the bitmap @bm that corresponds
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700458 * to given pfn. The cur_zone_bm member of @bm and the cur_block member
459 * of @bm->cur_zone_bm are updated.
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700460 */
Rafael J. Wysockia82f7112008-03-12 00:34:57 +0100461static int memory_bm_find_bit(struct memory_bitmap *bm, unsigned long pfn,
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -0700462 void **addr, unsigned int *bit_nr)
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700463{
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700464 struct bm_block *bb;
465
Rafael J. Wysocki846705d2008-11-26 18:00:24 -0500466 /*
467 * Check if the pfn corresponds to the current bitmap block and find
468 * the block where it fits if this is not the case.
469 */
470 bb = bm->cur.block;
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700471 if (pfn < bb->start_pfn)
Rafael J. Wysocki846705d2008-11-26 18:00:24 -0500472 list_for_each_entry_continue_reverse(bb, &bm->blocks, hook)
473 if (pfn >= bb->start_pfn)
474 break;
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700475
Rafael J. Wysocki846705d2008-11-26 18:00:24 -0500476 if (pfn >= bb->end_pfn)
477 list_for_each_entry_continue(bb, &bm->blocks, hook)
478 if (pfn >= bb->start_pfn && pfn < bb->end_pfn)
479 break;
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -0700480
Rafael J. Wysocki846705d2008-11-26 18:00:24 -0500481 if (&bb->hook == &bm->blocks)
482 return -EFAULT;
483
484 /* The block has been found */
485 bm->cur.block = bb;
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700486 pfn -= bb->start_pfn;
Rafael J. Wysocki846705d2008-11-26 18:00:24 -0500487 bm->cur.bit = pfn + 1;
Akinobu Mita0d833042008-07-23 21:28:38 -0700488 *bit_nr = pfn;
489 *addr = bb->data;
Rafael J. Wysockia82f7112008-03-12 00:34:57 +0100490 return 0;
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -0700491}
492
493static void memory_bm_set_bit(struct memory_bitmap *bm, unsigned long pfn)
494{
495 void *addr;
496 unsigned int bit;
Rafael J. Wysockia82f7112008-03-12 00:34:57 +0100497 int error;
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -0700498
Rafael J. Wysockia82f7112008-03-12 00:34:57 +0100499 error = memory_bm_find_bit(bm, pfn, &addr, &bit);
500 BUG_ON(error);
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -0700501 set_bit(bit, addr);
502}
503
Rafael J. Wysockia82f7112008-03-12 00:34:57 +0100504static int mem_bm_set_bit_check(struct memory_bitmap *bm, unsigned long pfn)
505{
506 void *addr;
507 unsigned int bit;
508 int error;
509
510 error = memory_bm_find_bit(bm, pfn, &addr, &bit);
511 if (!error)
512 set_bit(bit, addr);
513 return error;
514}
515
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -0700516static void memory_bm_clear_bit(struct memory_bitmap *bm, unsigned long pfn)
517{
518 void *addr;
519 unsigned int bit;
Rafael J. Wysockia82f7112008-03-12 00:34:57 +0100520 int error;
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -0700521
Rafael J. Wysockia82f7112008-03-12 00:34:57 +0100522 error = memory_bm_find_bit(bm, pfn, &addr, &bit);
523 BUG_ON(error);
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -0700524 clear_bit(bit, addr);
525}
526
527static int memory_bm_test_bit(struct memory_bitmap *bm, unsigned long pfn)
528{
529 void *addr;
530 unsigned int bit;
Rafael J. Wysockia82f7112008-03-12 00:34:57 +0100531 int error;
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -0700532
Rafael J. Wysockia82f7112008-03-12 00:34:57 +0100533 error = memory_bm_find_bit(bm, pfn, &addr, &bit);
534 BUG_ON(error);
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -0700535 return test_bit(bit, addr);
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700536}
537
Rafael J. Wysocki69643272008-11-11 21:32:44 +0100538static bool memory_bm_pfn_present(struct memory_bitmap *bm, unsigned long pfn)
539{
540 void *addr;
541 unsigned int bit;
542
543 return !memory_bm_find_bit(bm, pfn, &addr, &bit);
544}
545
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700546/**
547 * memory_bm_next_pfn - find the pfn that corresponds to the next set bit
548 * in the bitmap @bm. If the pfn cannot be found, BM_END_OF_MAP is
549 * returned.
550 *
551 * It is required to run memory_bm_position_reset() before the first call to
552 * this function.
553 */
554
555static unsigned long memory_bm_next_pfn(struct memory_bitmap *bm)
556{
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700557 struct bm_block *bb;
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700558 int bit;
559
Rafael J. Wysocki846705d2008-11-26 18:00:24 -0500560 bb = bm->cur.block;
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700561 do {
Rafael J. Wysocki846705d2008-11-26 18:00:24 -0500562 bit = bm->cur.bit;
563 bit = find_next_bit(bb->data, bm_block_bits(bb), bit);
564 if (bit < bm_block_bits(bb))
565 goto Return_pfn;
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700566
Rafael J. Wysocki846705d2008-11-26 18:00:24 -0500567 bb = list_entry(bb->hook.next, struct bm_block, hook);
568 bm->cur.block = bb;
569 bm->cur.bit = 0;
570 } while (&bb->hook != &bm->blocks);
571
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700572 memory_bm_position_reset(bm);
573 return BM_END_OF_MAP;
574
Rafael J. Wysocki59a49332006-12-06 20:34:44 -0800575 Return_pfn:
Akinobu Mita0d833042008-07-23 21:28:38 -0700576 bm->cur.bit = bit + 1;
577 return bb->start_pfn + bit;
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700578}
579
580/**
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -0700581 * This structure represents a range of page frames the contents of which
582 * should not be saved during the suspend.
583 */
584
585struct nosave_region {
586 struct list_head list;
587 unsigned long start_pfn;
588 unsigned long end_pfn;
589};
590
591static LIST_HEAD(nosave_regions);
592
593/**
594 * register_nosave_region - register a range of page frames the contents
595 * of which should not be saved during the suspend (to be used in the early
596 * initialization code)
597 */
598
599void __init
Johannes Berg940d67f2007-05-08 19:23:49 +1000600__register_nosave_region(unsigned long start_pfn, unsigned long end_pfn,
601 int use_kmalloc)
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -0700602{
603 struct nosave_region *region;
604
605 if (start_pfn >= end_pfn)
606 return;
607
608 if (!list_empty(&nosave_regions)) {
609 /* Try to extend the previous region (they should be sorted) */
610 region = list_entry(nosave_regions.prev,
611 struct nosave_region, list);
612 if (region->end_pfn == start_pfn) {
613 region->end_pfn = end_pfn;
614 goto Report;
615 }
616 }
Johannes Berg940d67f2007-05-08 19:23:49 +1000617 if (use_kmalloc) {
618 /* during init, this shouldn't fail */
619 region = kmalloc(sizeof(struct nosave_region), GFP_KERNEL);
620 BUG_ON(!region);
621 } else
622 /* This allocation cannot fail */
Jan Beulich3c1596e2009-09-21 17:03:06 -0700623 region = alloc_bootmem(sizeof(struct nosave_region));
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -0700624 region->start_pfn = start_pfn;
625 region->end_pfn = end_pfn;
626 list_add_tail(&region->list, &nosave_regions);
627 Report:
Rafael J. Wysocki23976722007-12-08 02:09:43 +0100628 printk(KERN_INFO "PM: Registered nosave memory: %016lx - %016lx\n",
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -0700629 start_pfn << PAGE_SHIFT, end_pfn << PAGE_SHIFT);
630}
631
632/*
633 * Set bits in this map correspond to the page frames the contents of which
634 * should not be saved during the suspend.
635 */
636static struct memory_bitmap *forbidden_pages_map;
637
638/* Set bits in this map correspond to free page frames. */
639static struct memory_bitmap *free_pages_map;
640
641/*
642 * Each page frame allocated for creating the image is marked by setting the
643 * corresponding bits in forbidden_pages_map and free_pages_map simultaneously
644 */
645
646void swsusp_set_page_free(struct page *page)
647{
648 if (free_pages_map)
649 memory_bm_set_bit(free_pages_map, page_to_pfn(page));
650}
651
652static int swsusp_page_is_free(struct page *page)
653{
654 return free_pages_map ?
655 memory_bm_test_bit(free_pages_map, page_to_pfn(page)) : 0;
656}
657
658void swsusp_unset_page_free(struct page *page)
659{
660 if (free_pages_map)
661 memory_bm_clear_bit(free_pages_map, page_to_pfn(page));
662}
663
664static void swsusp_set_page_forbidden(struct page *page)
665{
666 if (forbidden_pages_map)
667 memory_bm_set_bit(forbidden_pages_map, page_to_pfn(page));
668}
669
670int swsusp_page_is_forbidden(struct page *page)
671{
672 return forbidden_pages_map ?
673 memory_bm_test_bit(forbidden_pages_map, page_to_pfn(page)) : 0;
674}
675
676static void swsusp_unset_page_forbidden(struct page *page)
677{
678 if (forbidden_pages_map)
679 memory_bm_clear_bit(forbidden_pages_map, page_to_pfn(page));
680}
681
682/**
683 * mark_nosave_pages - set bits corresponding to the page frames the
684 * contents of which should not be saved in a given bitmap.
685 */
686
687static void mark_nosave_pages(struct memory_bitmap *bm)
688{
689 struct nosave_region *region;
690
691 if (list_empty(&nosave_regions))
692 return;
693
694 list_for_each_entry(region, &nosave_regions, list) {
695 unsigned long pfn;
696
Rafael J. Wysocki23976722007-12-08 02:09:43 +0100697 pr_debug("PM: Marking nosave pages: %016lx - %016lx\n",
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -0700698 region->start_pfn << PAGE_SHIFT,
699 region->end_pfn << PAGE_SHIFT);
700
701 for (pfn = region->start_pfn; pfn < region->end_pfn; pfn++)
Rafael J. Wysockia82f7112008-03-12 00:34:57 +0100702 if (pfn_valid(pfn)) {
703 /*
704 * It is safe to ignore the result of
705 * mem_bm_set_bit_check() here, since we won't
706 * touch the PFNs for which the error is
707 * returned anyway.
708 */
709 mem_bm_set_bit_check(bm, pfn);
710 }
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -0700711 }
712}
713
714/**
715 * create_basic_memory_bitmaps - create bitmaps needed for marking page
716 * frames that should not be saved and free page frames. The pointers
717 * forbidden_pages_map and free_pages_map are only modified if everything
718 * goes well, because we don't want the bits to be used before both bitmaps
719 * are set up.
720 */
721
722int create_basic_memory_bitmaps(void)
723{
724 struct memory_bitmap *bm1, *bm2;
725 int error = 0;
726
727 BUG_ON(forbidden_pages_map || free_pages_map);
728
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700729 bm1 = kzalloc(sizeof(struct memory_bitmap), GFP_KERNEL);
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -0700730 if (!bm1)
731 return -ENOMEM;
732
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700733 error = memory_bm_create(bm1, GFP_KERNEL, PG_ANY);
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -0700734 if (error)
735 goto Free_first_object;
736
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700737 bm2 = kzalloc(sizeof(struct memory_bitmap), GFP_KERNEL);
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -0700738 if (!bm2)
739 goto Free_first_bitmap;
740
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700741 error = memory_bm_create(bm2, GFP_KERNEL, PG_ANY);
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -0700742 if (error)
743 goto Free_second_object;
744
745 forbidden_pages_map = bm1;
746 free_pages_map = bm2;
747 mark_nosave_pages(forbidden_pages_map);
748
Rafael J. Wysocki23976722007-12-08 02:09:43 +0100749 pr_debug("PM: Basic memory bitmaps created\n");
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -0700750
751 return 0;
752
753 Free_second_object:
754 kfree(bm2);
755 Free_first_bitmap:
756 memory_bm_free(bm1, PG_UNSAFE_CLEAR);
757 Free_first_object:
758 kfree(bm1);
759 return -ENOMEM;
760}
761
762/**
763 * free_basic_memory_bitmaps - free memory bitmaps allocated by
764 * create_basic_memory_bitmaps(). The auxiliary pointers are necessary
765 * so that the bitmaps themselves are not referred to while they are being
766 * freed.
767 */
768
769void free_basic_memory_bitmaps(void)
770{
771 struct memory_bitmap *bm1, *bm2;
772
773 BUG_ON(!(forbidden_pages_map && free_pages_map));
774
775 bm1 = forbidden_pages_map;
776 bm2 = free_pages_map;
777 forbidden_pages_map = NULL;
778 free_pages_map = NULL;
779 memory_bm_free(bm1, PG_UNSAFE_CLEAR);
780 kfree(bm1);
781 memory_bm_free(bm2, PG_UNSAFE_CLEAR);
782 kfree(bm2);
783
Rafael J. Wysocki23976722007-12-08 02:09:43 +0100784 pr_debug("PM: Basic memory bitmaps freed\n");
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -0700785}
786
787/**
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700788 * snapshot_additional_pages - estimate the number of additional pages
789 * be needed for setting up the suspend image data structures for given
790 * zone (usually the returned value is greater than the exact number)
791 */
792
793unsigned int snapshot_additional_pages(struct zone *zone)
794{
795 unsigned int res;
796
797 res = DIV_ROUND_UP(zone->spanned_pages, BM_BITS_PER_BLOCK);
798 res += DIV_ROUND_UP(res * sizeof(struct bm_block), PAGE_SIZE);
Rafael J. Wysocki83573762006-12-06 20:34:18 -0800799 return 2 * res;
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700800}
801
Rafael J. Wysocki83573762006-12-06 20:34:18 -0800802#ifdef CONFIG_HIGHMEM
803/**
804 * count_free_highmem_pages - compute the total number of free highmem
805 * pages, system-wide.
806 */
807
808static unsigned int count_free_highmem_pages(void)
809{
810 struct zone *zone;
811 unsigned int cnt = 0;
812
KOSAKI Motohiroee99c712009-03-31 15:19:31 -0700813 for_each_populated_zone(zone)
814 if (is_highmem(zone))
Christoph Lameterd23ad422007-02-10 01:43:02 -0800815 cnt += zone_page_state(zone, NR_FREE_PAGES);
Rafael J. Wysocki83573762006-12-06 20:34:18 -0800816
817 return cnt;
818}
819
820/**
821 * saveable_highmem_page - Determine whether a highmem page should be
822 * included in the suspend image.
823 *
824 * We should save the page if it isn't Nosave or NosaveFree, or Reserved,
825 * and it isn't a part of a free chunk of pages.
826 */
Rafael J. Wysocki846705d2008-11-26 18:00:24 -0500827static struct page *saveable_highmem_page(struct zone *zone, unsigned long pfn)
Rafael J. Wysocki83573762006-12-06 20:34:18 -0800828{
829 struct page *page;
830
831 if (!pfn_valid(pfn))
832 return NULL;
833
834 page = pfn_to_page(pfn);
Rafael J. Wysocki846705d2008-11-26 18:00:24 -0500835 if (page_zone(page) != zone)
836 return NULL;
Rafael J. Wysocki83573762006-12-06 20:34:18 -0800837
838 BUG_ON(!PageHighMem(page));
839
Rafael J. Wysocki7be98232007-05-06 14:50:42 -0700840 if (swsusp_page_is_forbidden(page) || swsusp_page_is_free(page) ||
841 PageReserved(page))
Rafael J. Wysocki83573762006-12-06 20:34:18 -0800842 return NULL;
843
844 return page;
845}
846
847/**
848 * count_highmem_pages - compute the total number of saveable highmem
849 * pages.
850 */
851
Rafael J. Wysockife419532009-06-11 23:11:17 +0200852static unsigned int count_highmem_pages(void)
Rafael J. Wysocki83573762006-12-06 20:34:18 -0800853{
854 struct zone *zone;
855 unsigned int n = 0;
856
Gerald Schaefer98e73dc2009-07-22 00:36:56 +0200857 for_each_populated_zone(zone) {
Rafael J. Wysocki83573762006-12-06 20:34:18 -0800858 unsigned long pfn, max_zone_pfn;
859
860 if (!is_highmem(zone))
861 continue;
862
863 mark_free_pages(zone);
864 max_zone_pfn = zone->zone_start_pfn + zone->spanned_pages;
865 for (pfn = zone->zone_start_pfn; pfn < max_zone_pfn; pfn++)
Rafael J. Wysocki846705d2008-11-26 18:00:24 -0500866 if (saveable_highmem_page(zone, pfn))
Rafael J. Wysocki83573762006-12-06 20:34:18 -0800867 n++;
868 }
869 return n;
870}
871#else
Rafael J. Wysocki846705d2008-11-26 18:00:24 -0500872static inline void *saveable_highmem_page(struct zone *z, unsigned long p)
873{
874 return NULL;
875}
Rafael J. Wysocki83573762006-12-06 20:34:18 -0800876#endif /* CONFIG_HIGHMEM */
877
Rafael J. Wysockif6143aa2006-09-25 23:32:50 -0700878/**
Rafael J. Wysocki8a235ef2008-02-20 01:47:44 +0100879 * saveable_page - Determine whether a non-highmem page should be included
880 * in the suspend image.
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800881 *
Rafael J. Wysocki83573762006-12-06 20:34:18 -0800882 * We should save the page if it isn't Nosave, and is not in the range
883 * of pages statically defined as 'unsaveable', and it isn't a part of
884 * a free chunk of pages.
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800885 */
Rafael J. Wysocki846705d2008-11-26 18:00:24 -0500886static struct page *saveable_page(struct zone *zone, unsigned long pfn)
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800887{
Pavel Machekde491862005-10-30 14:59:59 -0800888 struct page *page;
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800889
890 if (!pfn_valid(pfn))
Rafael J. Wysockiae83c5ee2006-09-25 23:32:45 -0700891 return NULL;
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800892
893 page = pfn_to_page(pfn);
Rafael J. Wysocki846705d2008-11-26 18:00:24 -0500894 if (page_zone(page) != zone)
895 return NULL;
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800896
Rafael J. Wysocki83573762006-12-06 20:34:18 -0800897 BUG_ON(PageHighMem(page));
898
Rafael J. Wysocki7be98232007-05-06 14:50:42 -0700899 if (swsusp_page_is_forbidden(page) || swsusp_page_is_free(page))
Rafael J. Wysockiae83c5ee2006-09-25 23:32:45 -0700900 return NULL;
Rafael J. Wysocki83573762006-12-06 20:34:18 -0800901
Rafael J. Wysocki8a235ef2008-02-20 01:47:44 +0100902 if (PageReserved(page)
903 && (!kernel_page_present(page) || pfn_is_nosave(pfn)))
Rafael J. Wysockiae83c5ee2006-09-25 23:32:45 -0700904 return NULL;
Rafael J. Wysockiae83c5ee2006-09-25 23:32:45 -0700905
906 return page;
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800907}
908
Rafael J. Wysocki83573762006-12-06 20:34:18 -0800909/**
910 * count_data_pages - compute the total number of saveable non-highmem
911 * pages.
912 */
913
Rafael J. Wysockife419532009-06-11 23:11:17 +0200914static unsigned int count_data_pages(void)
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800915{
916 struct zone *zone;
Rafael J. Wysockiae83c5ee2006-09-25 23:32:45 -0700917 unsigned long pfn, max_zone_pfn;
Pavel Machekdc19d502005-11-07 00:58:40 -0800918 unsigned int n = 0;
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800919
Gerald Schaefer98e73dc2009-07-22 00:36:56 +0200920 for_each_populated_zone(zone) {
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800921 if (is_highmem(zone))
922 continue;
Rafael J. Wysocki83573762006-12-06 20:34:18 -0800923
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800924 mark_free_pages(zone);
Rafael J. Wysockiae83c5ee2006-09-25 23:32:45 -0700925 max_zone_pfn = zone->zone_start_pfn + zone->spanned_pages;
926 for (pfn = zone->zone_start_pfn; pfn < max_zone_pfn; pfn++)
Rafael J. Wysocki846705d2008-11-26 18:00:24 -0500927 if (saveable_page(zone, pfn))
Rafael J. Wysocki83573762006-12-06 20:34:18 -0800928 n++;
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800929 }
Rafael J. Wysockia0f49652005-10-30 14:59:57 -0800930 return n;
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800931}
932
Rafael J. Wysocki83573762006-12-06 20:34:18 -0800933/* This is needed, because copy_page and memcpy are not usable for copying
934 * task structs.
935 */
936static inline void do_copy_page(long *dst, long *src)
Rafael J. Wysockif623f0d2006-09-25 23:32:49 -0700937{
938 int n;
939
Rafael J. Wysockif623f0d2006-09-25 23:32:49 -0700940 for (n = PAGE_SIZE / sizeof(long); n; n--)
941 *dst++ = *src++;
942}
943
Rafael J. Wysocki8a235ef2008-02-20 01:47:44 +0100944
945/**
946 * safe_copy_page - check if the page we are going to copy is marked as
947 * present in the kernel page tables (this always is the case if
948 * CONFIG_DEBUG_PAGEALLOC is not set and in that case
949 * kernel_page_present() always returns 'true').
950 */
951static void safe_copy_page(void *dst, struct page *s_page)
952{
953 if (kernel_page_present(s_page)) {
954 do_copy_page(dst, page_address(s_page));
955 } else {
956 kernel_map_pages(s_page, 1, 1);
957 do_copy_page(dst, page_address(s_page));
958 kernel_map_pages(s_page, 1, 0);
959 }
960}
961
962
Rafael J. Wysocki83573762006-12-06 20:34:18 -0800963#ifdef CONFIG_HIGHMEM
964static inline struct page *
965page_is_saveable(struct zone *zone, unsigned long pfn)
966{
967 return is_highmem(zone) ?
Rafael J. Wysocki846705d2008-11-26 18:00:24 -0500968 saveable_highmem_page(zone, pfn) : saveable_page(zone, pfn);
Rafael J. Wysocki83573762006-12-06 20:34:18 -0800969}
970
Rafael J. Wysocki8a235ef2008-02-20 01:47:44 +0100971static void copy_data_page(unsigned long dst_pfn, unsigned long src_pfn)
Rafael J. Wysocki83573762006-12-06 20:34:18 -0800972{
973 struct page *s_page, *d_page;
974 void *src, *dst;
975
976 s_page = pfn_to_page(src_pfn);
977 d_page = pfn_to_page(dst_pfn);
978 if (PageHighMem(s_page)) {
979 src = kmap_atomic(s_page, KM_USER0);
980 dst = kmap_atomic(d_page, KM_USER1);
981 do_copy_page(dst, src);
982 kunmap_atomic(src, KM_USER0);
983 kunmap_atomic(dst, KM_USER1);
984 } else {
Rafael J. Wysocki83573762006-12-06 20:34:18 -0800985 if (PageHighMem(d_page)) {
986 /* Page pointed to by src may contain some kernel
987 * data modified by kmap_atomic()
988 */
Rafael J. Wysocki8a235ef2008-02-20 01:47:44 +0100989 safe_copy_page(buffer, s_page);
Rafael J. Wysockibaa58352008-12-08 00:52:49 +0100990 dst = kmap_atomic(d_page, KM_USER0);
Rafael J. Wysocki83573762006-12-06 20:34:18 -0800991 memcpy(dst, buffer, PAGE_SIZE);
992 kunmap_atomic(dst, KM_USER0);
993 } else {
Rafael J. Wysocki8a235ef2008-02-20 01:47:44 +0100994 safe_copy_page(page_address(d_page), s_page);
Rafael J. Wysocki83573762006-12-06 20:34:18 -0800995 }
996 }
997}
998#else
Rafael J. Wysocki846705d2008-11-26 18:00:24 -0500999#define page_is_saveable(zone, pfn) saveable_page(zone, pfn)
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001000
Rafael J. Wysocki8a235ef2008-02-20 01:47:44 +01001001static inline void copy_data_page(unsigned long dst_pfn, unsigned long src_pfn)
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001002{
Rafael J. Wysocki8a235ef2008-02-20 01:47:44 +01001003 safe_copy_page(page_address(pfn_to_page(dst_pfn)),
1004 pfn_to_page(src_pfn));
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001005}
1006#endif /* CONFIG_HIGHMEM */
1007
Rafael J. Wysockib788db72006-09-25 23:32:54 -07001008static void
1009copy_data_pages(struct memory_bitmap *copy_bm, struct memory_bitmap *orig_bm)
Rafael J. Wysocki25761b62005-10-30 14:59:56 -08001010{
1011 struct zone *zone;
Rafael J. Wysockib788db72006-09-25 23:32:54 -07001012 unsigned long pfn;
Rafael J. Wysocki25761b62005-10-30 14:59:56 -08001013
Gerald Schaefer98e73dc2009-07-22 00:36:56 +02001014 for_each_populated_zone(zone) {
Rafael J. Wysockib788db72006-09-25 23:32:54 -07001015 unsigned long max_zone_pfn;
1016
Rafael J. Wysocki25761b62005-10-30 14:59:56 -08001017 mark_free_pages(zone);
Rafael J. Wysockiae83c5ee2006-09-25 23:32:45 -07001018 max_zone_pfn = zone->zone_start_pfn + zone->spanned_pages;
Rafael J. Wysockib788db72006-09-25 23:32:54 -07001019 for (pfn = zone->zone_start_pfn; pfn < max_zone_pfn; pfn++)
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001020 if (page_is_saveable(zone, pfn))
Rafael J. Wysockib788db72006-09-25 23:32:54 -07001021 memory_bm_set_bit(orig_bm, pfn);
Rafael J. Wysocki25761b62005-10-30 14:59:56 -08001022 }
Rafael J. Wysockib788db72006-09-25 23:32:54 -07001023 memory_bm_position_reset(orig_bm);
1024 memory_bm_position_reset(copy_bm);
Fengguang Wudf7c4872007-10-20 02:26:04 +02001025 for(;;) {
Rafael J. Wysockib788db72006-09-25 23:32:54 -07001026 pfn = memory_bm_next_pfn(orig_bm);
Fengguang Wudf7c4872007-10-20 02:26:04 +02001027 if (unlikely(pfn == BM_END_OF_MAP))
1028 break;
1029 copy_data_page(memory_bm_next_pfn(copy_bm), pfn);
1030 }
Rafael J. Wysocki25761b62005-10-30 14:59:56 -08001031}
1032
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001033/* Total number of image pages */
1034static unsigned int nr_copy_pages;
1035/* Number of pages needed for saving the original pfns of the image pages */
1036static unsigned int nr_meta_pages;
Rafael J. Wysocki64a473c2009-07-08 13:24:05 +02001037/*
1038 * Numbers of normal and highmem page frames allocated for hibernation image
1039 * before suspending devices.
1040 */
1041unsigned int alloc_normal, alloc_highmem;
1042/*
1043 * Memory bitmap used for marking saveable pages (during hibernation) or
1044 * hibernation image pages (during restore)
1045 */
1046static struct memory_bitmap orig_bm;
1047/*
1048 * Memory bitmap used during hibernation for marking allocated page frames that
1049 * will contain copies of saveable pages. During restore it is initially used
1050 * for marking hibernation image pages, but then the set bits from it are
1051 * duplicated in @orig_bm and it is released. On highmem systems it is next
1052 * used for marking "safe" highmem pages, but it has to be reinitialized for
1053 * this purpose.
1054 */
1055static struct memory_bitmap copy_bm;
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001056
Rafael J. Wysocki25761b62005-10-30 14:59:56 -08001057/**
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001058 * swsusp_free - free pages allocated for the suspend.
Rafael J. Wysockicd560bb2006-09-25 23:32:50 -07001059 *
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001060 * Suspend pages are alocated before the atomic copy is made, so we
1061 * need to release them after the resume.
Rafael J. Wysocki25761b62005-10-30 14:59:56 -08001062 */
1063
1064void swsusp_free(void)
1065{
1066 struct zone *zone;
Rafael J. Wysockiae83c5ee2006-09-25 23:32:45 -07001067 unsigned long pfn, max_zone_pfn;
Rafael J. Wysocki25761b62005-10-30 14:59:56 -08001068
Gerald Schaefer98e73dc2009-07-22 00:36:56 +02001069 for_each_populated_zone(zone) {
Rafael J. Wysockiae83c5ee2006-09-25 23:32:45 -07001070 max_zone_pfn = zone->zone_start_pfn + zone->spanned_pages;
1071 for (pfn = zone->zone_start_pfn; pfn < max_zone_pfn; pfn++)
1072 if (pfn_valid(pfn)) {
1073 struct page *page = pfn_to_page(pfn);
1074
Rafael J. Wysocki7be98232007-05-06 14:50:42 -07001075 if (swsusp_page_is_forbidden(page) &&
1076 swsusp_page_is_free(page)) {
1077 swsusp_unset_page_forbidden(page);
1078 swsusp_unset_page_free(page);
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001079 __free_page(page);
Rafael J. Wysocki25761b62005-10-30 14:59:56 -08001080 }
1081 }
1082 }
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001083 nr_copy_pages = 0;
1084 nr_meta_pages = 0;
Rafael J. Wysocki75534b52006-09-25 23:32:52 -07001085 restore_pblist = NULL;
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -08001086 buffer = NULL;
Rafael J. Wysocki64a473c2009-07-08 13:24:05 +02001087 alloc_normal = 0;
1088 alloc_highmem = 0;
KAMEZAWA Hiroyukid2997b12010-08-09 17:20:11 -07001089 hibernation_thaw_swap();
Rafael J. Wysocki25761b62005-10-30 14:59:56 -08001090}
1091
Rafael J. Wysocki4bb33432009-07-08 13:23:51 +02001092/* Helper functions used for the shrinking of memory. */
Rafael J. Wysockife419532009-06-11 23:11:17 +02001093
Rafael J. Wysocki4bb33432009-07-08 13:23:51 +02001094#define GFP_IMAGE (GFP_KERNEL | __GFP_NOWARN)
1095
1096/**
1097 * preallocate_image_pages - Allocate a number of pages for hibernation image
1098 * @nr_pages: Number of page frames to allocate.
1099 * @mask: GFP flags to use for the allocation.
1100 *
1101 * Return value: Number of page frames actually allocated
1102 */
1103static unsigned long preallocate_image_pages(unsigned long nr_pages, gfp_t mask)
Rafael J. Wysockife419532009-06-11 23:11:17 +02001104{
Rafael J. Wysocki4bb33432009-07-08 13:23:51 +02001105 unsigned long nr_alloc = 0;
1106
1107 while (nr_pages > 0) {
Rafael J. Wysocki64a473c2009-07-08 13:24:05 +02001108 struct page *page;
1109
1110 page = alloc_image_page(mask);
1111 if (!page)
Rafael J. Wysocki4bb33432009-07-08 13:23:51 +02001112 break;
Rafael J. Wysocki64a473c2009-07-08 13:24:05 +02001113 memory_bm_set_bit(&copy_bm, page_to_pfn(page));
1114 if (PageHighMem(page))
1115 alloc_highmem++;
1116 else
1117 alloc_normal++;
Rafael J. Wysocki4bb33432009-07-08 13:23:51 +02001118 nr_pages--;
1119 nr_alloc++;
1120 }
1121
1122 return nr_alloc;
Rafael J. Wysockife419532009-06-11 23:11:17 +02001123}
1124
Rafael J. Wysocki4bb33432009-07-08 13:23:51 +02001125static unsigned long preallocate_image_memory(unsigned long nr_pages)
1126{
1127 return preallocate_image_pages(nr_pages, GFP_IMAGE);
1128}
1129
1130#ifdef CONFIG_HIGHMEM
1131static unsigned long preallocate_image_highmem(unsigned long nr_pages)
1132{
1133 return preallocate_image_pages(nr_pages, GFP_IMAGE | __GFP_HIGHMEM);
1134}
1135
1136/**
1137 * __fraction - Compute (an approximation of) x * (multiplier / base)
1138 */
1139static unsigned long __fraction(u64 x, u64 multiplier, u64 base)
1140{
1141 x *= multiplier;
1142 do_div(x, base);
1143 return (unsigned long)x;
1144}
1145
1146static unsigned long preallocate_highmem_fraction(unsigned long nr_pages,
1147 unsigned long highmem,
1148 unsigned long total)
1149{
1150 unsigned long alloc = __fraction(nr_pages, highmem, total);
1151
1152 return preallocate_image_pages(alloc, GFP_IMAGE | __GFP_HIGHMEM);
1153}
1154#else /* CONFIG_HIGHMEM */
1155static inline unsigned long preallocate_image_highmem(unsigned long nr_pages)
1156{
1157 return 0;
1158}
1159
1160static inline unsigned long preallocate_highmem_fraction(unsigned long nr_pages,
1161 unsigned long highmem,
1162 unsigned long total)
1163{
1164 return 0;
1165}
1166#endif /* CONFIG_HIGHMEM */
1167
1168/**
Rafael J. Wysocki64a473c2009-07-08 13:24:05 +02001169 * free_unnecessary_pages - Release preallocated pages not needed for the image
1170 */
1171static void free_unnecessary_pages(void)
1172{
1173 unsigned long save_highmem, to_free_normal, to_free_highmem;
1174
1175 to_free_normal = alloc_normal - count_data_pages();
1176 save_highmem = count_highmem_pages();
1177 if (alloc_highmem > save_highmem) {
1178 to_free_highmem = alloc_highmem - save_highmem;
1179 } else {
1180 to_free_highmem = 0;
1181 to_free_normal -= save_highmem - alloc_highmem;
1182 }
1183
1184 memory_bm_position_reset(&copy_bm);
1185
Rafael J. Wysockia9c9b442010-02-25 22:32:37 +01001186 while (to_free_normal > 0 || to_free_highmem > 0) {
Rafael J. Wysocki64a473c2009-07-08 13:24:05 +02001187 unsigned long pfn = memory_bm_next_pfn(&copy_bm);
1188 struct page *page = pfn_to_page(pfn);
1189
1190 if (PageHighMem(page)) {
1191 if (!to_free_highmem)
1192 continue;
1193 to_free_highmem--;
1194 alloc_highmem--;
1195 } else {
1196 if (!to_free_normal)
1197 continue;
1198 to_free_normal--;
1199 alloc_normal--;
1200 }
1201 memory_bm_clear_bit(&copy_bm, pfn);
1202 swsusp_unset_page_forbidden(page);
1203 swsusp_unset_page_free(page);
1204 __free_page(page);
1205 }
1206}
1207
1208/**
Rafael J. Wysockief4aede2009-07-08 13:24:12 +02001209 * minimum_image_size - Estimate the minimum acceptable size of an image
1210 * @saveable: Number of saveable pages in the system.
1211 *
1212 * We want to avoid attempting to free too much memory too hard, so estimate the
1213 * minimum acceptable size of a hibernation image to use as the lower limit for
1214 * preallocating memory.
1215 *
1216 * We assume that the minimum image size should be proportional to
1217 *
1218 * [number of saveable pages] - [number of pages that can be freed in theory]
1219 *
1220 * where the second term is the sum of (1) reclaimable slab pages, (2) active
1221 * and (3) inactive anonymouns pages, (4) active and (5) inactive file pages,
1222 * minus mapped file pages.
1223 */
1224static unsigned long minimum_image_size(unsigned long saveable)
1225{
1226 unsigned long size;
1227
1228 size = global_page_state(NR_SLAB_RECLAIMABLE)
1229 + global_page_state(NR_ACTIVE_ANON)
1230 + global_page_state(NR_INACTIVE_ANON)
1231 + global_page_state(NR_ACTIVE_FILE)
1232 + global_page_state(NR_INACTIVE_FILE)
1233 - global_page_state(NR_FILE_MAPPED);
1234
1235 return saveable <= size ? 0 : saveable - size;
1236}
1237
1238/**
Rafael J. Wysocki64a473c2009-07-08 13:24:05 +02001239 * hibernate_preallocate_memory - Preallocate memory for hibernation image
Rafael J. Wysocki4bb33432009-07-08 13:23:51 +02001240 *
1241 * To create a hibernation image it is necessary to make a copy of every page
1242 * frame in use. We also need a number of page frames to be free during
1243 * hibernation for allocations made while saving the image and for device
1244 * drivers, in case they need to allocate memory from their hibernation
1245 * callbacks (these two numbers are given by PAGES_FOR_IO and SPARE_PAGES,
1246 * respectively, both of which are rough estimates). To make this happen, we
1247 * compute the total number of available page frames and allocate at least
1248 *
1249 * ([page frames total] + PAGES_FOR_IO + [metadata pages]) / 2 + 2 * SPARE_PAGES
1250 *
1251 * of them, which corresponds to the maximum size of a hibernation image.
1252 *
1253 * If image_size is set below the number following from the above formula,
1254 * the preallocation of memory is continued until the total number of saveable
Rafael J. Wysockief4aede2009-07-08 13:24:12 +02001255 * pages in the system is below the requested image size or the minimum
1256 * acceptable image size returned by minimum_image_size(), whichever is greater.
Rafael J. Wysocki4bb33432009-07-08 13:23:51 +02001257 */
Rafael J. Wysocki64a473c2009-07-08 13:24:05 +02001258int hibernate_preallocate_memory(void)
Rafael J. Wysockife419532009-06-11 23:11:17 +02001259{
Rafael J. Wysockife419532009-06-11 23:11:17 +02001260 struct zone *zone;
Rafael J. Wysocki4bb33432009-07-08 13:23:51 +02001261 unsigned long saveable, size, max_size, count, highmem, pages = 0;
Rafael J. Wysocki64a473c2009-07-08 13:24:05 +02001262 unsigned long alloc, save_highmem, pages_highmem;
Rafael J. Wysockife419532009-06-11 23:11:17 +02001263 struct timeval start, stop;
Rafael J. Wysocki64a473c2009-07-08 13:24:05 +02001264 int error;
Rafael J. Wysockife419532009-06-11 23:11:17 +02001265
Rafael J. Wysocki64a473c2009-07-08 13:24:05 +02001266 printk(KERN_INFO "PM: Preallocating image memory... ");
Rafael J. Wysockife419532009-06-11 23:11:17 +02001267 do_gettimeofday(&start);
Rafael J. Wysockife419532009-06-11 23:11:17 +02001268
Rafael J. Wysocki64a473c2009-07-08 13:24:05 +02001269 error = memory_bm_create(&orig_bm, GFP_IMAGE, PG_ANY);
1270 if (error)
1271 goto err_out;
1272
1273 error = memory_bm_create(&copy_bm, GFP_IMAGE, PG_ANY);
1274 if (error)
1275 goto err_out;
1276
1277 alloc_normal = 0;
1278 alloc_highmem = 0;
1279
Rafael J. Wysocki4bb33432009-07-08 13:23:51 +02001280 /* Count the number of saveable data pages. */
Rafael J. Wysocki64a473c2009-07-08 13:24:05 +02001281 save_highmem = count_highmem_pages();
Rafael J. Wysocki4bb33432009-07-08 13:23:51 +02001282 saveable = count_data_pages();
Rafael J. Wysockife419532009-06-11 23:11:17 +02001283
Rafael J. Wysocki4bb33432009-07-08 13:23:51 +02001284 /*
1285 * Compute the total number of page frames we can use (count) and the
1286 * number of pages needed for image metadata (size).
1287 */
1288 count = saveable;
Rafael J. Wysocki64a473c2009-07-08 13:24:05 +02001289 saveable += save_highmem;
1290 highmem = save_highmem;
Rafael J. Wysocki4bb33432009-07-08 13:23:51 +02001291 size = 0;
1292 for_each_populated_zone(zone) {
1293 size += snapshot_additional_pages(zone);
1294 if (is_highmem(zone))
1295 highmem += zone_page_state(zone, NR_FREE_PAGES);
1296 else
1297 count += zone_page_state(zone, NR_FREE_PAGES);
1298 }
1299 count += highmem;
1300 count -= totalreserve_pages;
Rafael J. Wysockife419532009-06-11 23:11:17 +02001301
Rafael J. Wysocki4bb33432009-07-08 13:23:51 +02001302 /* Compute the maximum number of saveable pages to leave in memory. */
1303 max_size = (count - (size + PAGES_FOR_IO)) / 2 - 2 * SPARE_PAGES;
1304 size = DIV_ROUND_UP(image_size, PAGE_SIZE);
1305 if (size > max_size)
1306 size = max_size;
1307 /*
1308 * If the maximum is not less than the current number of saveable pages
Rafael J. Wysocki64a473c2009-07-08 13:24:05 +02001309 * in memory, allocate page frames for the image and we're done.
Rafael J. Wysocki4bb33432009-07-08 13:23:51 +02001310 */
Rafael J. Wysocki64a473c2009-07-08 13:24:05 +02001311 if (size >= saveable) {
1312 pages = preallocate_image_highmem(save_highmem);
1313 pages += preallocate_image_memory(saveable - pages);
Rafael J. Wysocki4bb33432009-07-08 13:23:51 +02001314 goto out;
Rafael J. Wysocki64a473c2009-07-08 13:24:05 +02001315 }
Rafael J. Wysocki4bb33432009-07-08 13:23:51 +02001316
Rafael J. Wysockief4aede2009-07-08 13:24:12 +02001317 /* Estimate the minimum size of the image. */
1318 pages = minimum_image_size(saveable);
1319 if (size < pages)
1320 size = min_t(unsigned long, pages, max_size);
1321
Rafael J. Wysocki4bb33432009-07-08 13:23:51 +02001322 /*
1323 * Let the memory management subsystem know that we're going to need a
1324 * large number of page frames to allocate and make it free some memory.
1325 * NOTE: If this is not done, performance will be hurt badly in some
1326 * test cases.
1327 */
1328 shrink_all_memory(saveable - size);
1329
1330 /*
1331 * The number of saveable pages in memory was too high, so apply some
1332 * pressure to decrease it. First, make room for the largest possible
1333 * image and fail if that doesn't work. Next, try to decrease the size
Rafael J. Wysockief4aede2009-07-08 13:24:12 +02001334 * of the image as much as indicated by 'size' using allocations from
1335 * highmem and non-highmem zones separately.
Rafael J. Wysocki4bb33432009-07-08 13:23:51 +02001336 */
1337 pages_highmem = preallocate_image_highmem(highmem / 2);
1338 alloc = (count - max_size) - pages_highmem;
1339 pages = preallocate_image_memory(alloc);
Rafael J. Wysocki64a473c2009-07-08 13:24:05 +02001340 if (pages < alloc)
1341 goto err_out;
Rafael J. Wysocki4bb33432009-07-08 13:23:51 +02001342 size = max_size - size;
1343 alloc = size;
1344 size = preallocate_highmem_fraction(size, highmem, count);
1345 pages_highmem += size;
1346 alloc -= size;
1347 pages += preallocate_image_memory(alloc);
1348 pages += pages_highmem;
1349
Rafael J. Wysocki64a473c2009-07-08 13:24:05 +02001350 /*
1351 * We only need as many page frames for the image as there are saveable
1352 * pages in memory, but we have allocated more. Release the excessive
1353 * ones now.
1354 */
1355 free_unnecessary_pages();
Rafael J. Wysocki4bb33432009-07-08 13:23:51 +02001356
1357 out:
Rafael J. Wysockife419532009-06-11 23:11:17 +02001358 do_gettimeofday(&stop);
Rafael J. Wysocki64a473c2009-07-08 13:24:05 +02001359 printk(KERN_CONT "done (allocated %lu pages)\n", pages);
1360 swsusp_show_speed(&start, &stop, pages, "Allocated");
Rafael J. Wysockife419532009-06-11 23:11:17 +02001361
1362 return 0;
Rafael J. Wysocki64a473c2009-07-08 13:24:05 +02001363
1364 err_out:
1365 printk(KERN_CONT "\n");
1366 swsusp_free();
1367 return -ENOMEM;
Rafael J. Wysockife419532009-06-11 23:11:17 +02001368}
1369
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001370#ifdef CONFIG_HIGHMEM
1371/**
1372 * count_pages_for_highmem - compute the number of non-highmem pages
1373 * that will be necessary for creating copies of highmem pages.
1374 */
1375
1376static unsigned int count_pages_for_highmem(unsigned int nr_highmem)
1377{
Rafael J. Wysocki64a473c2009-07-08 13:24:05 +02001378 unsigned int free_highmem = count_free_highmem_pages() + alloc_highmem;
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001379
1380 if (free_highmem >= nr_highmem)
1381 nr_highmem = 0;
1382 else
1383 nr_highmem -= free_highmem;
1384
1385 return nr_highmem;
1386}
1387#else
1388static unsigned int
1389count_pages_for_highmem(unsigned int nr_highmem) { return 0; }
1390#endif /* CONFIG_HIGHMEM */
Rafael J. Wysocki25761b62005-10-30 14:59:56 -08001391
1392/**
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001393 * enough_free_mem - Make sure we have enough free memory for the
1394 * snapshot image.
Rafael J. Wysocki25761b62005-10-30 14:59:56 -08001395 */
1396
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001397static int enough_free_mem(unsigned int nr_pages, unsigned int nr_highmem)
Rafael J. Wysocki25761b62005-10-30 14:59:56 -08001398{
Rafael J. Wysockie5e2fa72006-01-06 00:14:20 -08001399 struct zone *zone;
Rafael J. Wysocki64a473c2009-07-08 13:24:05 +02001400 unsigned int free = alloc_normal;
Rafael J. Wysockie5e2fa72006-01-06 00:14:20 -08001401
Gerald Schaefer98e73dc2009-07-22 00:36:56 +02001402 for_each_populated_zone(zone)
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001403 if (!is_highmem(zone))
Christoph Lameterd23ad422007-02-10 01:43:02 -08001404 free += zone_page_state(zone, NR_FREE_PAGES);
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001405
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001406 nr_pages += count_pages_for_highmem(nr_highmem);
Rafael J. Wysocki64a473c2009-07-08 13:24:05 +02001407 pr_debug("PM: Normal pages needed: %u + %u, available pages: %u\n",
1408 nr_pages, PAGES_FOR_IO, free);
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001409
Rafael J. Wysocki64a473c2009-07-08 13:24:05 +02001410 return free > nr_pages + PAGES_FOR_IO;
Rafael J. Wysocki25761b62005-10-30 14:59:56 -08001411}
1412
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001413#ifdef CONFIG_HIGHMEM
1414/**
1415 * get_highmem_buffer - if there are some highmem pages in the suspend
1416 * image, we may need the buffer to copy them and/or load their data.
1417 */
1418
1419static inline int get_highmem_buffer(int safe_needed)
1420{
1421 buffer = get_image_page(GFP_ATOMIC | __GFP_COLD, safe_needed);
1422 return buffer ? 0 : -ENOMEM;
1423}
1424
1425/**
1426 * alloc_highmem_image_pages - allocate some highmem pages for the image.
1427 * Try to allocate as many pages as needed, but if the number of free
1428 * highmem pages is lesser than that, allocate them all.
1429 */
1430
1431static inline unsigned int
Rafael J. Wysocki64a473c2009-07-08 13:24:05 +02001432alloc_highmem_pages(struct memory_bitmap *bm, unsigned int nr_highmem)
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001433{
1434 unsigned int to_alloc = count_free_highmem_pages();
1435
1436 if (to_alloc > nr_highmem)
1437 to_alloc = nr_highmem;
1438
1439 nr_highmem -= to_alloc;
1440 while (to_alloc-- > 0) {
1441 struct page *page;
1442
1443 page = alloc_image_page(__GFP_HIGHMEM);
1444 memory_bm_set_bit(bm, page_to_pfn(page));
1445 }
1446 return nr_highmem;
1447}
1448#else
1449static inline int get_highmem_buffer(int safe_needed) { return 0; }
1450
1451static inline unsigned int
Rafael J. Wysocki64a473c2009-07-08 13:24:05 +02001452alloc_highmem_pages(struct memory_bitmap *bm, unsigned int n) { return 0; }
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001453#endif /* CONFIG_HIGHMEM */
1454
1455/**
1456 * swsusp_alloc - allocate memory for the suspend image
1457 *
1458 * We first try to allocate as many highmem pages as there are
1459 * saveable highmem pages in the system. If that fails, we allocate
1460 * non-highmem pages for the copies of the remaining highmem ones.
1461 *
1462 * In this approach it is likely that the copies of highmem pages will
1463 * also be located in the high memory, because of the way in which
1464 * copy_data_pages() works.
1465 */
1466
Rafael J. Wysockib788db72006-09-25 23:32:54 -07001467static int
1468swsusp_alloc(struct memory_bitmap *orig_bm, struct memory_bitmap *copy_bm,
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001469 unsigned int nr_pages, unsigned int nr_highmem)
Rafael J. Wysocki054bd4c2005-11-08 21:34:39 -08001470{
Rafael J. Wysocki64a473c2009-07-08 13:24:05 +02001471 int error = 0;
Rafael J. Wysockib788db72006-09-25 23:32:54 -07001472
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001473 if (nr_highmem > 0) {
1474 error = get_highmem_buffer(PG_ANY);
1475 if (error)
Rafael J. Wysocki64a473c2009-07-08 13:24:05 +02001476 goto err_out;
1477 if (nr_highmem > alloc_highmem) {
1478 nr_highmem -= alloc_highmem;
1479 nr_pages += alloc_highmem_pages(copy_bm, nr_highmem);
1480 }
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001481 }
Rafael J. Wysocki64a473c2009-07-08 13:24:05 +02001482 if (nr_pages > alloc_normal) {
1483 nr_pages -= alloc_normal;
1484 while (nr_pages-- > 0) {
1485 struct page *page;
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001486
Rafael J. Wysocki64a473c2009-07-08 13:24:05 +02001487 page = alloc_image_page(GFP_ATOMIC | __GFP_COLD);
1488 if (!page)
1489 goto err_out;
1490 memory_bm_set_bit(copy_bm, page_to_pfn(page));
1491 }
Rafael J. Wysocki054bd4c2005-11-08 21:34:39 -08001492 }
Rafael J. Wysocki64a473c2009-07-08 13:24:05 +02001493
Rafael J. Wysocki054bd4c2005-11-08 21:34:39 -08001494 return 0;
Rafael J. Wysockib788db72006-09-25 23:32:54 -07001495
Rafael J. Wysocki64a473c2009-07-08 13:24:05 +02001496 err_out:
Rafael J. Wysockib788db72006-09-25 23:32:54 -07001497 swsusp_free();
Rafael J. Wysocki64a473c2009-07-08 13:24:05 +02001498 return error;
Rafael J. Wysocki054bd4c2005-11-08 21:34:39 -08001499}
Rafael J. Wysocki25761b62005-10-30 14:59:56 -08001500
Rafael J. Wysocki2e32a432005-10-30 15:00:00 -08001501asmlinkage int swsusp_save(void)
Rafael J. Wysocki25761b62005-10-30 14:59:56 -08001502{
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001503 unsigned int nr_pages, nr_highmem;
Rafael J. Wysocki25761b62005-10-30 14:59:56 -08001504
Frans Pop07c3bb52010-02-11 23:09:08 +01001505 printk(KERN_INFO "PM: Creating hibernation image:\n");
Rafael J. Wysocki25761b62005-10-30 14:59:56 -08001506
Christoph Lameter9f8f2172008-02-04 22:29:11 -08001507 drain_local_pages(NULL);
Rafael J. Wysockia0f49652005-10-30 14:59:57 -08001508 nr_pages = count_data_pages();
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001509 nr_highmem = count_highmem_pages();
Rafael J. Wysocki23976722007-12-08 02:09:43 +01001510 printk(KERN_INFO "PM: Need to copy %u pages\n", nr_pages + nr_highmem);
Rafael J. Wysocki25761b62005-10-30 14:59:56 -08001511
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001512 if (!enough_free_mem(nr_pages, nr_highmem)) {
Rafael J. Wysocki23976722007-12-08 02:09:43 +01001513 printk(KERN_ERR "PM: Not enough free memory\n");
Rafael J. Wysocki25761b62005-10-30 14:59:56 -08001514 return -ENOMEM;
1515 }
1516
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001517 if (swsusp_alloc(&orig_bm, &copy_bm, nr_pages, nr_highmem)) {
Rafael J. Wysocki23976722007-12-08 02:09:43 +01001518 printk(KERN_ERR "PM: Memory allocation failed\n");
Rafael J. Wysockia0f49652005-10-30 14:59:57 -08001519 return -ENOMEM;
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001520 }
Rafael J. Wysocki25761b62005-10-30 14:59:56 -08001521
1522 /* During allocating of suspend pagedir, new cold pages may appear.
1523 * Kill them.
1524 */
Christoph Lameter9f8f2172008-02-04 22:29:11 -08001525 drain_local_pages(NULL);
Rafael J. Wysockib788db72006-09-25 23:32:54 -07001526 copy_data_pages(&copy_bm, &orig_bm);
Rafael J. Wysocki25761b62005-10-30 14:59:56 -08001527
1528 /*
1529 * End of critical section. From now on, we can write to memory,
1530 * but we should not touch disk. This specially means we must _not_
1531 * touch swap space! Except we must write out our image of course.
1532 */
1533
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001534 nr_pages += nr_highmem;
Rafael J. Wysockia0f49652005-10-30 14:59:57 -08001535 nr_copy_pages = nr_pages;
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001536 nr_meta_pages = DIV_ROUND_UP(nr_pages * sizeof(long), PAGE_SIZE);
Rafael J. Wysockia0f49652005-10-30 14:59:57 -08001537
Rafael J. Wysocki23976722007-12-08 02:09:43 +01001538 printk(KERN_INFO "PM: Hibernation image created (%d pages copied)\n",
1539 nr_pages);
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001540
Rafael J. Wysocki25761b62005-10-30 14:59:56 -08001541 return 0;
1542}
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001543
Rafael J. Wysockid307c4a2007-10-18 03:04:52 -07001544#ifndef CONFIG_ARCH_HIBERNATION_HEADER
1545static int init_header_complete(struct swsusp_info *info)
1546{
1547 memcpy(&info->uts, init_utsname(), sizeof(struct new_utsname));
1548 info->version_code = LINUX_VERSION_CODE;
1549 return 0;
1550}
1551
1552static char *check_image_kernel(struct swsusp_info *info)
1553{
1554 if (info->version_code != LINUX_VERSION_CODE)
1555 return "kernel version";
1556 if (strcmp(info->uts.sysname,init_utsname()->sysname))
1557 return "system type";
1558 if (strcmp(info->uts.release,init_utsname()->release))
1559 return "kernel release";
1560 if (strcmp(info->uts.version,init_utsname()->version))
1561 return "version";
1562 if (strcmp(info->uts.machine,init_utsname()->machine))
1563 return "machine";
1564 return NULL;
1565}
1566#endif /* CONFIG_ARCH_HIBERNATION_HEADER */
1567
Rafael J. Wysockiaf508b32007-10-26 00:59:31 +02001568unsigned long snapshot_get_image_size(void)
1569{
1570 return nr_copy_pages + nr_meta_pages + 1;
1571}
1572
Rafael J. Wysockid307c4a2007-10-18 03:04:52 -07001573static int init_header(struct swsusp_info *info)
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001574{
1575 memset(info, 0, sizeof(struct swsusp_info));
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001576 info->num_physpages = num_physpages;
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001577 info->image_pages = nr_copy_pages;
Rafael J. Wysockiaf508b32007-10-26 00:59:31 +02001578 info->pages = snapshot_get_image_size();
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -08001579 info->size = info->pages;
1580 info->size <<= PAGE_SHIFT;
Rafael J. Wysockid307c4a2007-10-18 03:04:52 -07001581 return init_header_complete(info);
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001582}
1583
1584/**
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001585 * pack_pfns - pfns corresponding to the set bits found in the bitmap @bm
1586 * are stored in the array @buf[] (1 page at a time)
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001587 */
1588
Rafael J. Wysockib788db72006-09-25 23:32:54 -07001589static inline void
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001590pack_pfns(unsigned long *buf, struct memory_bitmap *bm)
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001591{
1592 int j;
1593
Rafael J. Wysockib788db72006-09-25 23:32:54 -07001594 for (j = 0; j < PAGE_SIZE / sizeof(long); j++) {
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001595 buf[j] = memory_bm_next_pfn(bm);
1596 if (unlikely(buf[j] == BM_END_OF_MAP))
Rafael J. Wysockib788db72006-09-25 23:32:54 -07001597 break;
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001598 }
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001599}
1600
1601/**
1602 * snapshot_read_next - used for reading the system memory snapshot.
1603 *
1604 * On the first call to it @handle should point to a zeroed
1605 * snapshot_handle structure. The structure gets updated and a pointer
1606 * to it should be passed to this function every next time.
1607 *
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001608 * On success the function returns a positive number. Then, the caller
1609 * is allowed to read up to the returned number of bytes from the memory
Jiri Slabyd3c1b242010-05-01 23:52:02 +02001610 * location computed by the data_of() macro.
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001611 *
1612 * The function returns 0 to indicate the end of data stream condition,
1613 * and a negative number is returned on error. In such cases the
1614 * structure pointed to by @handle is not updated and should not be used
1615 * any more.
1616 */
1617
Jiri Slabyd3c1b242010-05-01 23:52:02 +02001618int snapshot_read_next(struct snapshot_handle *handle)
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001619{
Rafael J. Wysockifb13a282006-09-25 23:32:46 -07001620 if (handle->cur > nr_meta_pages + nr_copy_pages)
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001621 return 0;
Rafael J. Wysockib788db72006-09-25 23:32:54 -07001622
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001623 if (!buffer) {
1624 /* This makes the buffer be freed by swsusp_free() */
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001625 buffer = get_image_page(GFP_ATOMIC, PG_ANY);
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001626 if (!buffer)
1627 return -ENOMEM;
1628 }
Jiri Slabyd3c1b242010-05-01 23:52:02 +02001629 if (!handle->cur) {
Rafael J. Wysockid307c4a2007-10-18 03:04:52 -07001630 int error;
1631
1632 error = init_header((struct swsusp_info *)buffer);
1633 if (error)
1634 return error;
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001635 handle->buffer = buffer;
Rafael J. Wysockib788db72006-09-25 23:32:54 -07001636 memory_bm_position_reset(&orig_bm);
1637 memory_bm_position_reset(&copy_bm);
Jiri Slabyd3c1b242010-05-01 23:52:02 +02001638 } else if (handle->cur <= nr_meta_pages) {
1639 memset(buffer, 0, PAGE_SIZE);
1640 pack_pfns(buffer, &orig_bm);
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001641 } else {
Jiri Slabyd3c1b242010-05-01 23:52:02 +02001642 struct page *page;
1643
1644 page = pfn_to_page(memory_bm_next_pfn(&copy_bm));
1645 if (PageHighMem(page)) {
1646 /* Highmem pages are copied to the buffer,
1647 * because we can't return with a kmapped
1648 * highmem page (we may not be called again).
1649 */
1650 void *kaddr;
1651
1652 kaddr = kmap_atomic(page, KM_USER0);
1653 memcpy(buffer, kaddr, PAGE_SIZE);
1654 kunmap_atomic(kaddr, KM_USER0);
1655 handle->buffer = buffer;
1656 } else {
1657 handle->buffer = page_address(page);
1658 }
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001659 }
Jiri Slabyd3c1b242010-05-01 23:52:02 +02001660 handle->cur++;
1661 return PAGE_SIZE;
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001662}
1663
1664/**
1665 * mark_unsafe_pages - mark the pages that cannot be used for storing
1666 * the image during resume, because they conflict with the pages that
1667 * had been used before suspend
1668 */
1669
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001670static int mark_unsafe_pages(struct memory_bitmap *bm)
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001671{
1672 struct zone *zone;
Rafael J. Wysockiae83c5ee2006-09-25 23:32:45 -07001673 unsigned long pfn, max_zone_pfn;
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001674
1675 /* Clear page flags */
Gerald Schaefer98e73dc2009-07-22 00:36:56 +02001676 for_each_populated_zone(zone) {
Rafael J. Wysockiae83c5ee2006-09-25 23:32:45 -07001677 max_zone_pfn = zone->zone_start_pfn + zone->spanned_pages;
1678 for (pfn = zone->zone_start_pfn; pfn < max_zone_pfn; pfn++)
1679 if (pfn_valid(pfn))
Rafael J. Wysocki7be98232007-05-06 14:50:42 -07001680 swsusp_unset_page_free(pfn_to_page(pfn));
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001681 }
1682
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001683 /* Mark pages that correspond to the "original" pfns as "unsafe" */
1684 memory_bm_position_reset(bm);
1685 do {
1686 pfn = memory_bm_next_pfn(bm);
1687 if (likely(pfn != BM_END_OF_MAP)) {
1688 if (likely(pfn_valid(pfn)))
Rafael J. Wysocki7be98232007-05-06 14:50:42 -07001689 swsusp_set_page_free(pfn_to_page(pfn));
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001690 else
1691 return -EFAULT;
1692 }
1693 } while (pfn != BM_END_OF_MAP);
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001694
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001695 allocated_unsafe_pages = 0;
Rafael J. Wysocki968808b2006-06-23 02:04:48 -07001696
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001697 return 0;
1698}
1699
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001700static void
1701duplicate_memory_bitmap(struct memory_bitmap *dst, struct memory_bitmap *src)
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001702{
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001703 unsigned long pfn;
1704
1705 memory_bm_position_reset(src);
1706 pfn = memory_bm_next_pfn(src);
1707 while (pfn != BM_END_OF_MAP) {
1708 memory_bm_set_bit(dst, pfn);
1709 pfn = memory_bm_next_pfn(src);
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001710 }
1711}
1712
Rafael J. Wysockid307c4a2007-10-18 03:04:52 -07001713static int check_header(struct swsusp_info *info)
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001714{
Rafael J. Wysockid307c4a2007-10-18 03:04:52 -07001715 char *reason;
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001716
Rafael J. Wysockid307c4a2007-10-18 03:04:52 -07001717 reason = check_image_kernel(info);
1718 if (!reason && info->num_physpages != num_physpages)
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001719 reason = "memory size";
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001720 if (reason) {
Rafael J. Wysocki23976722007-12-08 02:09:43 +01001721 printk(KERN_ERR "PM: Image mismatch: %s\n", reason);
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001722 return -EPERM;
1723 }
1724 return 0;
1725}
1726
1727/**
1728 * load header - check the image header and copy data from it
1729 */
1730
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001731static int
1732load_header(struct swsusp_info *info)
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001733{
1734 int error;
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001735
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001736 restore_pblist = NULL;
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001737 error = check_header(info);
1738 if (!error) {
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001739 nr_copy_pages = info->image_pages;
1740 nr_meta_pages = info->pages - info->image_pages - 1;
1741 }
1742 return error;
1743}
1744
1745/**
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001746 * unpack_orig_pfns - for each element of @buf[] (1 page at a time) set
1747 * the corresponding bit in the memory bitmap @bm
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001748 */
Rafael J. Wysocki69643272008-11-11 21:32:44 +01001749static int unpack_orig_pfns(unsigned long *buf, struct memory_bitmap *bm)
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001750{
1751 int j;
1752
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001753 for (j = 0; j < PAGE_SIZE / sizeof(long); j++) {
1754 if (unlikely(buf[j] == BM_END_OF_MAP))
1755 break;
1756
Rafael J. Wysocki69643272008-11-11 21:32:44 +01001757 if (memory_bm_pfn_present(bm, buf[j]))
1758 memory_bm_set_bit(bm, buf[j]);
1759 else
1760 return -EFAULT;
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001761 }
Rafael J. Wysocki69643272008-11-11 21:32:44 +01001762
1763 return 0;
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001764}
1765
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001766/* List of "safe" pages that may be used to store data loaded from the suspend
1767 * image
1768 */
1769static struct linked_page *safe_pages_list;
1770
1771#ifdef CONFIG_HIGHMEM
1772/* struct highmem_pbe is used for creating the list of highmem pages that
1773 * should be restored atomically during the resume from disk, because the page
1774 * frames they have occupied before the suspend are in use.
1775 */
1776struct highmem_pbe {
1777 struct page *copy_page; /* data is here now */
1778 struct page *orig_page; /* data was here before the suspend */
1779 struct highmem_pbe *next;
1780};
1781
1782/* List of highmem PBEs needed for restoring the highmem pages that were
1783 * allocated before the suspend and included in the suspend image, but have
1784 * also been allocated by the "resume" kernel, so their contents cannot be
1785 * written directly to their "original" page frames.
1786 */
1787static struct highmem_pbe *highmem_pblist;
1788
1789/**
1790 * count_highmem_image_pages - compute the number of highmem pages in the
1791 * suspend image. The bits in the memory bitmap @bm that correspond to the
1792 * image pages are assumed to be set.
1793 */
1794
1795static unsigned int count_highmem_image_pages(struct memory_bitmap *bm)
1796{
1797 unsigned long pfn;
1798 unsigned int cnt = 0;
1799
1800 memory_bm_position_reset(bm);
1801 pfn = memory_bm_next_pfn(bm);
1802 while (pfn != BM_END_OF_MAP) {
1803 if (PageHighMem(pfn_to_page(pfn)))
1804 cnt++;
1805
1806 pfn = memory_bm_next_pfn(bm);
1807 }
1808 return cnt;
1809}
1810
1811/**
1812 * prepare_highmem_image - try to allocate as many highmem pages as
1813 * there are highmem image pages (@nr_highmem_p points to the variable
1814 * containing the number of highmem image pages). The pages that are
1815 * "safe" (ie. will not be overwritten when the suspend image is
1816 * restored) have the corresponding bits set in @bm (it must be
1817 * unitialized).
1818 *
1819 * NOTE: This function should not be called if there are no highmem
1820 * image pages.
1821 */
1822
1823static unsigned int safe_highmem_pages;
1824
1825static struct memory_bitmap *safe_highmem_bm;
1826
1827static int
1828prepare_highmem_image(struct memory_bitmap *bm, unsigned int *nr_highmem_p)
1829{
1830 unsigned int to_alloc;
1831
1832 if (memory_bm_create(bm, GFP_ATOMIC, PG_SAFE))
1833 return -ENOMEM;
1834
1835 if (get_highmem_buffer(PG_SAFE))
1836 return -ENOMEM;
1837
1838 to_alloc = count_free_highmem_pages();
1839 if (to_alloc > *nr_highmem_p)
1840 to_alloc = *nr_highmem_p;
1841 else
1842 *nr_highmem_p = to_alloc;
1843
1844 safe_highmem_pages = 0;
1845 while (to_alloc-- > 0) {
1846 struct page *page;
1847
1848 page = alloc_page(__GFP_HIGHMEM);
Rafael J. Wysocki7be98232007-05-06 14:50:42 -07001849 if (!swsusp_page_is_free(page)) {
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001850 /* The page is "safe", set its bit the bitmap */
1851 memory_bm_set_bit(bm, page_to_pfn(page));
1852 safe_highmem_pages++;
1853 }
1854 /* Mark the page as allocated */
Rafael J. Wysocki7be98232007-05-06 14:50:42 -07001855 swsusp_set_page_forbidden(page);
1856 swsusp_set_page_free(page);
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001857 }
1858 memory_bm_position_reset(bm);
1859 safe_highmem_bm = bm;
1860 return 0;
1861}
1862
1863/**
1864 * get_highmem_page_buffer - for given highmem image page find the buffer
1865 * that suspend_write_next() should set for its caller to write to.
1866 *
1867 * If the page is to be saved to its "original" page frame or a copy of
1868 * the page is to be made in the highmem, @buffer is returned. Otherwise,
1869 * the copy of the page is to be made in normal memory, so the address of
1870 * the copy is returned.
1871 *
1872 * If @buffer is returned, the caller of suspend_write_next() will write
1873 * the page's contents to @buffer, so they will have to be copied to the
1874 * right location on the next call to suspend_write_next() and it is done
1875 * with the help of copy_last_highmem_page(). For this purpose, if
1876 * @buffer is returned, @last_highmem page is set to the page to which
1877 * the data will have to be copied from @buffer.
1878 */
1879
1880static struct page *last_highmem_page;
1881
1882static void *
1883get_highmem_page_buffer(struct page *page, struct chain_allocator *ca)
1884{
1885 struct highmem_pbe *pbe;
1886 void *kaddr;
1887
Rafael J. Wysocki7be98232007-05-06 14:50:42 -07001888 if (swsusp_page_is_forbidden(page) && swsusp_page_is_free(page)) {
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001889 /* We have allocated the "original" page frame and we can
1890 * use it directly to store the loaded page.
1891 */
1892 last_highmem_page = page;
1893 return buffer;
1894 }
1895 /* The "original" page frame has not been allocated and we have to
1896 * use a "safe" page frame to store the loaded page.
1897 */
1898 pbe = chain_alloc(ca, sizeof(struct highmem_pbe));
1899 if (!pbe) {
1900 swsusp_free();
Rafael J. Wysocki69643272008-11-11 21:32:44 +01001901 return ERR_PTR(-ENOMEM);
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001902 }
1903 pbe->orig_page = page;
1904 if (safe_highmem_pages > 0) {
1905 struct page *tmp;
1906
1907 /* Copy of the page will be stored in high memory */
1908 kaddr = buffer;
1909 tmp = pfn_to_page(memory_bm_next_pfn(safe_highmem_bm));
1910 safe_highmem_pages--;
1911 last_highmem_page = tmp;
1912 pbe->copy_page = tmp;
1913 } else {
1914 /* Copy of the page will be stored in normal memory */
1915 kaddr = safe_pages_list;
1916 safe_pages_list = safe_pages_list->next;
1917 pbe->copy_page = virt_to_page(kaddr);
1918 }
1919 pbe->next = highmem_pblist;
1920 highmem_pblist = pbe;
1921 return kaddr;
1922}
1923
1924/**
1925 * copy_last_highmem_page - copy the contents of a highmem image from
1926 * @buffer, where the caller of snapshot_write_next() has place them,
1927 * to the right location represented by @last_highmem_page .
1928 */
1929
1930static void copy_last_highmem_page(void)
1931{
1932 if (last_highmem_page) {
1933 void *dst;
1934
1935 dst = kmap_atomic(last_highmem_page, KM_USER0);
1936 memcpy(dst, buffer, PAGE_SIZE);
1937 kunmap_atomic(dst, KM_USER0);
1938 last_highmem_page = NULL;
1939 }
1940}
1941
1942static inline int last_highmem_page_copied(void)
1943{
1944 return !last_highmem_page;
1945}
1946
1947static inline void free_highmem_data(void)
1948{
1949 if (safe_highmem_bm)
1950 memory_bm_free(safe_highmem_bm, PG_UNSAFE_CLEAR);
1951
1952 if (buffer)
1953 free_image_page(buffer, PG_UNSAFE_CLEAR);
1954}
1955#else
1956static inline int get_safe_write_buffer(void) { return 0; }
1957
1958static unsigned int
1959count_highmem_image_pages(struct memory_bitmap *bm) { return 0; }
1960
1961static inline int
1962prepare_highmem_image(struct memory_bitmap *bm, unsigned int *nr_highmem_p)
1963{
1964 return 0;
1965}
1966
1967static inline void *
1968get_highmem_page_buffer(struct page *page, struct chain_allocator *ca)
1969{
Rafael J. Wysocki69643272008-11-11 21:32:44 +01001970 return ERR_PTR(-EINVAL);
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001971}
1972
1973static inline void copy_last_highmem_page(void) {}
1974static inline int last_highmem_page_copied(void) { return 1; }
1975static inline void free_highmem_data(void) {}
1976#endif /* CONFIG_HIGHMEM */
1977
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001978/**
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001979 * prepare_image - use the memory bitmap @bm to mark the pages that will
1980 * be overwritten in the process of restoring the system memory state
1981 * from the suspend image ("unsafe" pages) and allocate memory for the
1982 * image.
Rafael J. Wysocki968808b2006-06-23 02:04:48 -07001983 *
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001984 * The idea is to allocate a new memory bitmap first and then allocate
1985 * as many pages as needed for the image data, but not to assign these
1986 * pages to specific tasks initially. Instead, we just mark them as
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001987 * allocated and create a lists of "safe" pages that will be used
1988 * later. On systems with high memory a list of "safe" highmem pages is
1989 * also created.
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001990 */
1991
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001992#define PBES_PER_LINKED_PAGE (LINKED_PAGE_DATA_SIZE / sizeof(struct pbe))
Rafael J. Wysocki968808b2006-06-23 02:04:48 -07001993
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001994static int
1995prepare_image(struct memory_bitmap *new_bm, struct memory_bitmap *bm)
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001996{
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001997 unsigned int nr_pages, nr_highmem;
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001998 struct linked_page *sp_list, *lp;
1999 int error;
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08002000
Rafael J. Wysocki83573762006-12-06 20:34:18 -08002001 /* If there is no highmem, the buffer will not be necessary */
2002 free_image_page(buffer, PG_UNSAFE_CLEAR);
2003 buffer = NULL;
2004
2005 nr_highmem = count_highmem_image_pages(bm);
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07002006 error = mark_unsafe_pages(bm);
2007 if (error)
2008 goto Free;
2009
2010 error = memory_bm_create(new_bm, GFP_ATOMIC, PG_SAFE);
2011 if (error)
2012 goto Free;
2013
2014 duplicate_memory_bitmap(new_bm, bm);
2015 memory_bm_free(bm, PG_UNSAFE_KEEP);
Rafael J. Wysocki83573762006-12-06 20:34:18 -08002016 if (nr_highmem > 0) {
2017 error = prepare_highmem_image(bm, &nr_highmem);
2018 if (error)
2019 goto Free;
2020 }
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07002021 /* Reserve some safe pages for potential later use.
2022 *
2023 * NOTE: This way we make sure there will be enough safe pages for the
2024 * chain_alloc() in get_buffer(). It is a bit wasteful, but
2025 * nr_copy_pages cannot be greater than 50% of the memory anyway.
2026 */
2027 sp_list = NULL;
2028 /* nr_copy_pages cannot be lesser than allocated_unsafe_pages */
Rafael J. Wysocki83573762006-12-06 20:34:18 -08002029 nr_pages = nr_copy_pages - nr_highmem - allocated_unsafe_pages;
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07002030 nr_pages = DIV_ROUND_UP(nr_pages, PBES_PER_LINKED_PAGE);
2031 while (nr_pages > 0) {
Rafael J. Wysocki83573762006-12-06 20:34:18 -08002032 lp = get_image_page(GFP_ATOMIC, PG_SAFE);
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07002033 if (!lp) {
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08002034 error = -ENOMEM;
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07002035 goto Free;
Rafael J. Wysocki968808b2006-06-23 02:04:48 -07002036 }
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07002037 lp->next = sp_list;
2038 sp_list = lp;
2039 nr_pages--;
Rafael J. Wysocki968808b2006-06-23 02:04:48 -07002040 }
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07002041 /* Preallocate memory for the image */
2042 safe_pages_list = NULL;
Rafael J. Wysocki83573762006-12-06 20:34:18 -08002043 nr_pages = nr_copy_pages - nr_highmem - allocated_unsafe_pages;
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07002044 while (nr_pages > 0) {
2045 lp = (struct linked_page *)get_zeroed_page(GFP_ATOMIC);
2046 if (!lp) {
2047 error = -ENOMEM;
2048 goto Free;
2049 }
Rafael J. Wysocki7be98232007-05-06 14:50:42 -07002050 if (!swsusp_page_is_free(virt_to_page(lp))) {
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07002051 /* The page is "safe", add it to the list */
2052 lp->next = safe_pages_list;
2053 safe_pages_list = lp;
2054 }
2055 /* Mark the page as allocated */
Rafael J. Wysocki7be98232007-05-06 14:50:42 -07002056 swsusp_set_page_forbidden(virt_to_page(lp));
2057 swsusp_set_page_free(virt_to_page(lp));
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07002058 nr_pages--;
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08002059 }
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07002060 /* Free the reserved safe pages so that chain_alloc() can use them */
2061 while (sp_list) {
2062 lp = sp_list->next;
2063 free_image_page(sp_list, PG_UNSAFE_CLEAR);
2064 sp_list = lp;
2065 }
2066 return 0;
2067
Rafael J. Wysocki59a49332006-12-06 20:34:44 -08002068 Free:
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07002069 swsusp_free();
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08002070 return error;
2071}
2072
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07002073/**
2074 * get_buffer - compute the address that snapshot_write_next() should
2075 * set for its caller to write to.
2076 */
Rafael J. Wysocki968808b2006-06-23 02:04:48 -07002077
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07002078static void *get_buffer(struct memory_bitmap *bm, struct chain_allocator *ca)
2079{
2080 struct pbe *pbe;
Rafael J. Wysocki69643272008-11-11 21:32:44 +01002081 struct page *page;
2082 unsigned long pfn = memory_bm_next_pfn(bm);
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07002083
Rafael J. Wysocki69643272008-11-11 21:32:44 +01002084 if (pfn == BM_END_OF_MAP)
2085 return ERR_PTR(-EFAULT);
2086
2087 page = pfn_to_page(pfn);
Rafael J. Wysocki83573762006-12-06 20:34:18 -08002088 if (PageHighMem(page))
2089 return get_highmem_page_buffer(page, ca);
2090
Rafael J. Wysocki7be98232007-05-06 14:50:42 -07002091 if (swsusp_page_is_forbidden(page) && swsusp_page_is_free(page))
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07002092 /* We have allocated the "original" page frame and we can
2093 * use it directly to store the loaded page.
Rafael J. Wysocki968808b2006-06-23 02:04:48 -07002094 */
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07002095 return page_address(page);
2096
2097 /* The "original" page frame has not been allocated and we have to
2098 * use a "safe" page frame to store the loaded page.
Rafael J. Wysocki968808b2006-06-23 02:04:48 -07002099 */
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07002100 pbe = chain_alloc(ca, sizeof(struct pbe));
2101 if (!pbe) {
2102 swsusp_free();
Rafael J. Wysocki69643272008-11-11 21:32:44 +01002103 return ERR_PTR(-ENOMEM);
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07002104 }
Rafael J. Wysocki83573762006-12-06 20:34:18 -08002105 pbe->orig_address = page_address(page);
2106 pbe->address = safe_pages_list;
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07002107 safe_pages_list = safe_pages_list->next;
2108 pbe->next = restore_pblist;
2109 restore_pblist = pbe;
Rafael J. Wysocki83573762006-12-06 20:34:18 -08002110 return pbe->address;
Rafael J. Wysocki968808b2006-06-23 02:04:48 -07002111}
2112
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08002113/**
2114 * snapshot_write_next - used for writing the system memory snapshot.
2115 *
2116 * On the first call to it @handle should point to a zeroed
2117 * snapshot_handle structure. The structure gets updated and a pointer
2118 * to it should be passed to this function every next time.
2119 *
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08002120 * On success the function returns a positive number. Then, the caller
2121 * is allowed to write up to the returned number of bytes to the memory
Jiri Slabyd3c1b242010-05-01 23:52:02 +02002122 * location computed by the data_of() macro.
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08002123 *
2124 * The function returns 0 to indicate the "end of file" condition,
2125 * and a negative number is returned on error. In such cases the
2126 * structure pointed to by @handle is not updated and should not be used
2127 * any more.
2128 */
2129
Jiri Slabyd3c1b242010-05-01 23:52:02 +02002130int snapshot_write_next(struct snapshot_handle *handle)
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08002131{
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07002132 static struct chain_allocator ca;
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08002133 int error = 0;
2134
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07002135 /* Check if we have already loaded the entire image */
Jiri Slabyd3c1b242010-05-01 23:52:02 +02002136 if (handle->cur > 1 && handle->cur > nr_meta_pages + nr_copy_pages)
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08002137 return 0;
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07002138
Jiri Slabyd3c1b242010-05-01 23:52:02 +02002139 handle->sync_read = 1;
2140
2141 if (!handle->cur) {
Rafael J. Wysocki83573762006-12-06 20:34:18 -08002142 if (!buffer)
2143 /* This makes the buffer be freed by swsusp_free() */
2144 buffer = get_image_page(GFP_ATOMIC, PG_ANY);
2145
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08002146 if (!buffer)
2147 return -ENOMEM;
Rafael J. Wysocki83573762006-12-06 20:34:18 -08002148
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08002149 handle->buffer = buffer;
Jiri Slabyd3c1b242010-05-01 23:52:02 +02002150 } else if (handle->cur == 1) {
2151 error = load_header(buffer);
2152 if (error)
2153 return error;
2154
2155 error = memory_bm_create(&copy_bm, GFP_ATOMIC, PG_ANY);
2156 if (error)
2157 return error;
2158
2159 } else if (handle->cur <= nr_meta_pages + 1) {
2160 error = unpack_orig_pfns(buffer, &copy_bm);
2161 if (error)
2162 return error;
2163
2164 if (handle->cur == nr_meta_pages + 1) {
2165 error = prepare_image(&orig_bm, &copy_bm);
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08002166 if (error)
2167 return error;
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07002168
Jiri Slabyd3c1b242010-05-01 23:52:02 +02002169 chain_init(&ca, GFP_ATOMIC, PG_SAFE);
2170 memory_bm_position_reset(&orig_bm);
2171 restore_pblist = NULL;
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07002172 handle->buffer = get_buffer(&orig_bm, &ca);
Jiri Slabyd3c1b242010-05-01 23:52:02 +02002173 handle->sync_read = 0;
Rafael J. Wysocki69643272008-11-11 21:32:44 +01002174 if (IS_ERR(handle->buffer))
2175 return PTR_ERR(handle->buffer);
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08002176 }
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08002177 } else {
Jiri Slabyd3c1b242010-05-01 23:52:02 +02002178 copy_last_highmem_page();
2179 handle->buffer = get_buffer(&orig_bm, &ca);
2180 if (IS_ERR(handle->buffer))
2181 return PTR_ERR(handle->buffer);
2182 if (handle->buffer != buffer)
2183 handle->sync_read = 0;
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08002184 }
Jiri Slabyd3c1b242010-05-01 23:52:02 +02002185 handle->cur++;
2186 return PAGE_SIZE;
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08002187}
2188
Rafael J. Wysocki83573762006-12-06 20:34:18 -08002189/**
2190 * snapshot_write_finalize - must be called after the last call to
2191 * snapshot_write_next() in case the last page in the image happens
2192 * to be a highmem page and its contents should be stored in the
2193 * highmem. Additionally, it releases the memory that will not be
2194 * used any more.
2195 */
2196
2197void snapshot_write_finalize(struct snapshot_handle *handle)
2198{
2199 copy_last_highmem_page();
2200 /* Free only if we have loaded the image entirely */
Jiri Slabyd3c1b242010-05-01 23:52:02 +02002201 if (handle->cur > 1 && handle->cur > nr_meta_pages + nr_copy_pages) {
Rafael J. Wysocki83573762006-12-06 20:34:18 -08002202 memory_bm_free(&orig_bm, PG_UNSAFE_CLEAR);
2203 free_highmem_data();
2204 }
2205}
2206
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08002207int snapshot_image_loaded(struct snapshot_handle *handle)
2208{
Rafael J. Wysocki83573762006-12-06 20:34:18 -08002209 return !(!nr_copy_pages || !last_highmem_page_copied() ||
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07002210 handle->cur <= nr_meta_pages + nr_copy_pages);
2211}
2212
Rafael J. Wysocki83573762006-12-06 20:34:18 -08002213#ifdef CONFIG_HIGHMEM
2214/* Assumes that @buf is ready and points to a "safe" page */
2215static inline void
2216swap_two_pages_data(struct page *p1, struct page *p2, void *buf)
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07002217{
Rafael J. Wysocki83573762006-12-06 20:34:18 -08002218 void *kaddr1, *kaddr2;
2219
2220 kaddr1 = kmap_atomic(p1, KM_USER0);
2221 kaddr2 = kmap_atomic(p2, KM_USER1);
2222 memcpy(buf, kaddr1, PAGE_SIZE);
2223 memcpy(kaddr1, kaddr2, PAGE_SIZE);
2224 memcpy(kaddr2, buf, PAGE_SIZE);
2225 kunmap_atomic(kaddr1, KM_USER0);
2226 kunmap_atomic(kaddr2, KM_USER1);
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08002227}
Rafael J. Wysocki83573762006-12-06 20:34:18 -08002228
2229/**
2230 * restore_highmem - for each highmem page that was allocated before
2231 * the suspend and included in the suspend image, and also has been
2232 * allocated by the "resume" kernel swap its current (ie. "before
2233 * resume") contents with the previous (ie. "before suspend") one.
2234 *
2235 * If the resume eventually fails, we can call this function once
2236 * again and restore the "before resume" highmem state.
2237 */
2238
2239int restore_highmem(void)
2240{
2241 struct highmem_pbe *pbe = highmem_pblist;
2242 void *buf;
2243
2244 if (!pbe)
2245 return 0;
2246
2247 buf = get_image_page(GFP_ATOMIC, PG_SAFE);
2248 if (!buf)
2249 return -ENOMEM;
2250
2251 while (pbe) {
2252 swap_two_pages_data(pbe->copy_page, pbe->orig_page, buf);
2253 pbe = pbe->next;
2254 }
2255 free_image_page(buf, PG_UNSAFE_CLEAR);
2256 return 0;
2257}
2258#endif /* CONFIG_HIGHMEM */