blob: 0b7f93498077706827600686ba51fdff2630e6ce [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>
Gideon Israel Dsouza52f5684c2014-04-07 15:39:20 -070030#include <linux/compiler.h>
Rafael J. Wysocki25761b62005-10-30 14:59:56 -080031
32#include <asm/uaccess.h>
33#include <asm/mmu_context.h>
34#include <asm/pgtable.h>
35#include <asm/tlbflush.h>
36#include <asm/io.h>
37
Rafael J. Wysocki25761b62005-10-30 14:59:56 -080038#include "power.h"
39
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -070040static int swsusp_page_is_free(struct page *);
41static void swsusp_set_page_forbidden(struct page *);
42static void swsusp_unset_page_forbidden(struct page *);
43
Rafael J. Wysockife419532009-06-11 23:11:17 +020044/*
Rafael J. Wysockiddeb6482011-05-15 11:38:48 +020045 * Number of bytes to reserve for memory allocations made by device drivers
46 * from their ->freeze() and ->freeze_noirq() callbacks so that they don't
47 * cause image creation to fail (tunable via /sys/power/reserved_size).
48 */
49unsigned long reserved_size;
50
51void __init hibernate_reserved_size_init(void)
52{
53 reserved_size = SPARE_PAGES * PAGE_SIZE;
54}
55
56/*
Rafael J. Wysockife419532009-06-11 23:11:17 +020057 * Preferred image size in bytes (tunable via /sys/power/image_size).
Rafael J. Wysocki1c1be3a2011-05-15 11:39:48 +020058 * When it is set to N, swsusp will do its best to ensure the image
59 * size will not exceed N bytes, but if that is impossible, it will
60 * try to create the smallest image possible.
Rafael J. Wysockife419532009-06-11 23:11:17 +020061 */
Rafael J. Wysockiac5c24ec2010-09-20 19:44:56 +020062unsigned long image_size;
63
64void __init hibernate_image_size_init(void)
65{
Rafael J. Wysocki1c1be3a2011-05-15 11:39:48 +020066 image_size = ((totalram_pages * 2) / 5) * PAGE_SIZE;
Rafael J. Wysockiac5c24ec2010-09-20 19:44:56 +020067}
Rafael J. Wysockife419532009-06-11 23:11:17 +020068
Rafael J. Wysocki83573762006-12-06 20:34:18 -080069/* List of PBEs needed for restoring the pages that were allocated before
70 * the suspend and included in the suspend image, but have also been
71 * allocated by the "resume" kernel, so their contents cannot be written
72 * directly to their "original" page frames.
73 */
Rafael J. Wysocki75534b52006-09-25 23:32:52 -070074struct pbe *restore_pblist;
75
Rafael J. Wysocki83573762006-12-06 20:34:18 -080076/* Pointer to an auxiliary buffer (1 page) */
Rafael J. Wysocki940864d2006-09-25 23:32:55 -070077static void *buffer;
Rafael J. Wysocki7088a5c2006-01-06 00:13:05 -080078
Rafael J. Wysockif6143aa2006-09-25 23:32:50 -070079/**
80 * @safe_needed - on resume, for storing the PBE list and the image,
81 * we can only use memory pages that do not conflict with the pages
Rafael J. Wysocki83573762006-12-06 20:34:18 -080082 * used before suspend. The unsafe pages have PageNosaveFree set
83 * and we count them using unsafe_pages.
Rafael J. Wysockif6143aa2006-09-25 23:32:50 -070084 *
Rafael J. Wysocki83573762006-12-06 20:34:18 -080085 * Each allocated image page is marked as PageNosave and PageNosaveFree
86 * so that swsusp_free() can release it.
Rafael J. Wysockif6143aa2006-09-25 23:32:50 -070087 */
88
Rafael J. Wysocki0bcd8882006-09-25 23:32:52 -070089#define PG_ANY 0
90#define PG_SAFE 1
91#define PG_UNSAFE_CLEAR 1
92#define PG_UNSAFE_KEEP 0
93
Rafael J. Wysocki940864d2006-09-25 23:32:55 -070094static unsigned int allocated_unsafe_pages;
Rafael J. Wysockif6143aa2006-09-25 23:32:50 -070095
Rafael J. Wysocki83573762006-12-06 20:34:18 -080096static void *get_image_page(gfp_t gfp_mask, int safe_needed)
Rafael J. Wysockif6143aa2006-09-25 23:32:50 -070097{
98 void *res;
99
100 res = (void *)get_zeroed_page(gfp_mask);
101 if (safe_needed)
Rafael J. Wysocki7be98232007-05-06 14:50:42 -0700102 while (res && swsusp_page_is_free(virt_to_page(res))) {
Rafael J. Wysockif6143aa2006-09-25 23:32:50 -0700103 /* The page is unsafe, mark it for swsusp_free() */
Rafael J. Wysocki7be98232007-05-06 14:50:42 -0700104 swsusp_set_page_forbidden(virt_to_page(res));
Rafael J. Wysocki940864d2006-09-25 23:32:55 -0700105 allocated_unsafe_pages++;
Rafael J. Wysockif6143aa2006-09-25 23:32:50 -0700106 res = (void *)get_zeroed_page(gfp_mask);
107 }
108 if (res) {
Rafael J. Wysocki7be98232007-05-06 14:50:42 -0700109 swsusp_set_page_forbidden(virt_to_page(res));
110 swsusp_set_page_free(virt_to_page(res));
Rafael J. Wysockif6143aa2006-09-25 23:32:50 -0700111 }
112 return res;
113}
114
115unsigned long get_safe_page(gfp_t gfp_mask)
116{
Rafael J. Wysocki83573762006-12-06 20:34:18 -0800117 return (unsigned long)get_image_page(gfp_mask, PG_SAFE);
118}
119
Rafael J. Wysocki5b6d15d2006-12-06 20:34:43 -0800120static struct page *alloc_image_page(gfp_t gfp_mask)
121{
Rafael J. Wysocki83573762006-12-06 20:34:18 -0800122 struct page *page;
123
124 page = alloc_page(gfp_mask);
125 if (page) {
Rafael J. Wysocki7be98232007-05-06 14:50:42 -0700126 swsusp_set_page_forbidden(page);
127 swsusp_set_page_free(page);
Rafael J. Wysocki83573762006-12-06 20:34:18 -0800128 }
129 return page;
Rafael J. Wysockif6143aa2006-09-25 23:32:50 -0700130}
131
132/**
133 * free_image_page - free page represented by @addr, allocated with
Rafael J. Wysocki83573762006-12-06 20:34:18 -0800134 * get_image_page (page flags set by it must be cleared)
Rafael J. Wysockif6143aa2006-09-25 23:32:50 -0700135 */
136
137static inline void free_image_page(void *addr, int clear_nosave_free)
138{
Rafael J. Wysocki83573762006-12-06 20:34:18 -0800139 struct page *page;
140
141 BUG_ON(!virt_addr_valid(addr));
142
143 page = virt_to_page(addr);
144
Rafael J. Wysocki7be98232007-05-06 14:50:42 -0700145 swsusp_unset_page_forbidden(page);
Rafael J. Wysockif6143aa2006-09-25 23:32:50 -0700146 if (clear_nosave_free)
Rafael J. Wysocki7be98232007-05-06 14:50:42 -0700147 swsusp_unset_page_free(page);
Rafael J. Wysocki83573762006-12-06 20:34:18 -0800148
149 __free_page(page);
Rafael J. Wysockif6143aa2006-09-25 23:32:50 -0700150}
151
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700152/* struct linked_page is used to build chains of pages */
153
154#define LINKED_PAGE_DATA_SIZE (PAGE_SIZE - sizeof(void *))
155
156struct linked_page {
157 struct linked_page *next;
158 char data[LINKED_PAGE_DATA_SIZE];
Gideon Israel Dsouza52f5684c2014-04-07 15:39:20 -0700159} __packed;
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700160
161static inline void
162free_list_of_pages(struct linked_page *list, int clear_page_nosave)
163{
164 while (list) {
165 struct linked_page *lp = list->next;
166
167 free_image_page(list, clear_page_nosave);
168 list = lp;
169 }
170}
171
172/**
173 * struct chain_allocator is used for allocating small objects out of
174 * a linked list of pages called 'the chain'.
175 *
176 * The chain grows each time when there is no room for a new object in
177 * the current page. The allocated objects cannot be freed individually.
178 * It is only possible to free them all at once, by freeing the entire
179 * chain.
180 *
181 * NOTE: The chain allocator may be inefficient if the allocated objects
182 * are not much smaller than PAGE_SIZE.
183 */
184
185struct chain_allocator {
186 struct linked_page *chain; /* the chain */
187 unsigned int used_space; /* total size of objects allocated out
188 * of the current page
189 */
190 gfp_t gfp_mask; /* mask for allocating pages */
191 int safe_needed; /* if set, only "safe" pages are allocated */
192};
193
194static void
195chain_init(struct chain_allocator *ca, gfp_t gfp_mask, int safe_needed)
196{
197 ca->chain = NULL;
198 ca->used_space = LINKED_PAGE_DATA_SIZE;
199 ca->gfp_mask = gfp_mask;
200 ca->safe_needed = safe_needed;
201}
202
203static void *chain_alloc(struct chain_allocator *ca, unsigned int size)
204{
205 void *ret;
206
207 if (LINKED_PAGE_DATA_SIZE - ca->used_space < size) {
208 struct linked_page *lp;
209
Rafael J. Wysocki83573762006-12-06 20:34:18 -0800210 lp = get_image_page(ca->gfp_mask, ca->safe_needed);
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700211 if (!lp)
212 return NULL;
213
214 lp->next = ca->chain;
215 ca->chain = lp;
216 ca->used_space = 0;
217 }
218 ret = ca->chain->data + ca->used_space;
219 ca->used_space += size;
220 return ret;
221}
222
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700223/**
224 * Data types related to memory bitmaps.
225 *
226 * Memory bitmap is a structure consiting of many linked lists of
227 * objects. The main list's elements are of type struct zone_bitmap
228 * and each of them corresonds to one zone. For each zone bitmap
229 * object there is a list of objects of type struct bm_block that
Akinobu Mita0d833042008-07-23 21:28:38 -0700230 * represent each blocks of bitmap in which information is stored.
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700231 *
232 * struct memory_bitmap contains a pointer to the main list of zone
233 * bitmap objects, a struct bm_position used for browsing the bitmap,
234 * and a pointer to the list of pages used for allocating all of the
235 * zone bitmap objects and bitmap block objects.
236 *
237 * NOTE: It has to be possible to lay out the bitmap in memory
238 * using only allocations of order 0. Additionally, the bitmap is
239 * designed to work with arbitrary number of zones (this is over the
240 * top for now, but let's avoid making unnecessary assumptions ;-).
241 *
242 * struct zone_bitmap contains a pointer to a list of bitmap block
243 * objects and a pointer to the bitmap block object that has been
244 * most recently used for setting bits. Additionally, it contains the
245 * pfns that correspond to the start and end of the represented zone.
246 *
247 * struct bm_block contains a pointer to the memory page in which
Akinobu Mita0d833042008-07-23 21:28:38 -0700248 * information is stored (in the form of a block of bitmap)
249 * It also contains the pfns that correspond to the start and end of
250 * the represented memory area.
Joerg Roedelf469f022014-07-21 12:26:57 +0200251 *
252 * The memory bitmap is organized as a radix tree to guarantee fast random
253 * access to the bits. There is one radix tree for each zone (as returned
254 * from create_mem_extents).
255 *
256 * One radix tree is represented by one struct mem_zone_bm_rtree. There are
257 * two linked lists for the nodes of the tree, one for the inner nodes and
258 * one for the leave nodes. The linked leave nodes are used for fast linear
259 * access of the memory bitmap.
260 *
261 * The struct rtree_node represents one node of the radix tree.
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700262 */
263
264#define BM_END_OF_MAP (~0UL)
265
Wu Fengguang8de03072009-07-22 19:56:10 +0200266#define BM_BITS_PER_BLOCK (PAGE_SIZE * BITS_PER_BYTE)
Joerg Roedelf469f022014-07-21 12:26:57 +0200267#define BM_BLOCK_SHIFT (PAGE_SHIFT + 3)
268#define BM_BLOCK_MASK ((1UL << BM_BLOCK_SHIFT) - 1)
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700269
270struct bm_block {
Rafael J. Wysocki846705d2008-11-26 18:00:24 -0500271 struct list_head hook; /* hook into a list of bitmap blocks */
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700272 unsigned long start_pfn; /* pfn represented by the first bit */
273 unsigned long end_pfn; /* pfn represented by the last bit plus 1 */
Akinobu Mita0d833042008-07-23 21:28:38 -0700274 unsigned long *data; /* bitmap representing pages */
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700275};
276
Akinobu Mita0d833042008-07-23 21:28:38 -0700277static inline unsigned long bm_block_bits(struct bm_block *bb)
278{
279 return bb->end_pfn - bb->start_pfn;
280}
281
Joerg Roedelf469f022014-07-21 12:26:57 +0200282/*
283 * struct rtree_node is a wrapper struct to link the nodes
284 * of the rtree together for easy linear iteration over
285 * bits and easy freeing
286 */
287struct rtree_node {
288 struct list_head list;
289 unsigned long *data;
290};
291
292/*
293 * struct mem_zone_bm_rtree represents a bitmap used for one
294 * populated memory zone.
295 */
296struct mem_zone_bm_rtree {
297 struct list_head list; /* Link Zones together */
298 struct list_head nodes; /* Radix Tree inner nodes */
299 struct list_head leaves; /* Radix Tree leaves */
300 unsigned long start_pfn; /* Zone start page frame */
301 unsigned long end_pfn; /* Zone end page frame + 1 */
302 struct rtree_node *rtree; /* Radix Tree Root */
303 int levels; /* Number of Radix Tree Levels */
304 unsigned int blocks; /* Number of Bitmap Blocks */
305};
306
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700307/* strcut bm_position is used for browsing memory bitmaps */
308
309struct bm_position {
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700310 struct bm_block *block;
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700311 int bit;
312};
313
314struct memory_bitmap {
Joerg Roedelf469f022014-07-21 12:26:57 +0200315 struct list_head zones;
Rafael J. Wysocki846705d2008-11-26 18:00:24 -0500316 struct list_head blocks; /* list of bitmap blocks */
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700317 struct linked_page *p_list; /* list of pages used to store zone
318 * bitmap objects and bitmap block
319 * objects
320 */
321 struct bm_position cur; /* most recently used bit position */
322};
323
324/* Functions that operate on memory bitmaps */
325
Joerg Roedelf469f022014-07-21 12:26:57 +0200326#define BM_ENTRIES_PER_LEVEL (PAGE_SIZE / sizeof(unsigned long))
327#if BITS_PER_LONG == 32
328#define BM_RTREE_LEVEL_SHIFT (PAGE_SHIFT - 2)
329#else
330#define BM_RTREE_LEVEL_SHIFT (PAGE_SHIFT - 3)
331#endif
332#define BM_RTREE_LEVEL_MASK ((1UL << BM_RTREE_LEVEL_SHIFT) - 1)
333
334/*
335 * alloc_rtree_node - Allocate a new node and add it to the radix tree.
336 *
337 * This function is used to allocate inner nodes as well as the
338 * leave nodes of the radix tree. It also adds the node to the
339 * corresponding linked list passed in by the *list parameter.
340 */
341static struct rtree_node *alloc_rtree_node(gfp_t gfp_mask, int safe_needed,
342 struct chain_allocator *ca,
343 struct list_head *list)
344{
345 struct rtree_node *node;
346
347 node = chain_alloc(ca, sizeof(struct rtree_node));
348 if (!node)
349 return NULL;
350
351 node->data = get_image_page(gfp_mask, safe_needed);
352 if (!node->data)
353 return NULL;
354
355 list_add_tail(&node->list, list);
356
357 return node;
358}
359
360/*
361 * add_rtree_block - Add a new leave node to the radix tree
362 *
363 * The leave nodes need to be allocated in order to keep the leaves
364 * linked list in order. This is guaranteed by the zone->blocks
365 * counter.
366 */
367static int add_rtree_block(struct mem_zone_bm_rtree *zone, gfp_t gfp_mask,
368 int safe_needed, struct chain_allocator *ca)
369{
370 struct rtree_node *node, *block, **dst;
371 unsigned int levels_needed, block_nr;
372 int i;
373
374 block_nr = zone->blocks;
375 levels_needed = 0;
376
377 /* How many levels do we need for this block nr? */
378 while (block_nr) {
379 levels_needed += 1;
380 block_nr >>= BM_RTREE_LEVEL_SHIFT;
381 }
382
383 /* Make sure the rtree has enough levels */
384 for (i = zone->levels; i < levels_needed; i++) {
385 node = alloc_rtree_node(gfp_mask, safe_needed, ca,
386 &zone->nodes);
387 if (!node)
388 return -ENOMEM;
389
390 node->data[0] = (unsigned long)zone->rtree;
391 zone->rtree = node;
392 zone->levels += 1;
393 }
394
395 /* Allocate new block */
396 block = alloc_rtree_node(gfp_mask, safe_needed, ca, &zone->leaves);
397 if (!block)
398 return -ENOMEM;
399
400 /* Now walk the rtree to insert the block */
401 node = zone->rtree;
402 dst = &zone->rtree;
403 block_nr = zone->blocks;
404 for (i = zone->levels; i > 0; i--) {
405 int index;
406
407 if (!node) {
408 node = alloc_rtree_node(gfp_mask, safe_needed, ca,
409 &zone->nodes);
410 if (!node)
411 return -ENOMEM;
412 *dst = node;
413 }
414
415 index = block_nr >> ((i - 1) * BM_RTREE_LEVEL_SHIFT);
416 index &= BM_RTREE_LEVEL_MASK;
417 dst = (struct rtree_node **)&((*dst)->data[index]);
418 node = *dst;
419 }
420
421 zone->blocks += 1;
422 *dst = block;
423
424 return 0;
425}
426
427static void free_zone_bm_rtree(struct mem_zone_bm_rtree *zone,
428 int clear_nosave_free);
429
430/*
431 * create_zone_bm_rtree - create a radix tree for one zone
432 *
433 * Allocated the mem_zone_bm_rtree structure and initializes it.
434 * This function also allocated and builds the radix tree for the
435 * zone.
436 */
437static struct mem_zone_bm_rtree *
438create_zone_bm_rtree(gfp_t gfp_mask, int safe_needed,
439 struct chain_allocator *ca,
440 unsigned long start, unsigned long end)
441{
442 struct mem_zone_bm_rtree *zone;
443 unsigned int i, nr_blocks;
444 unsigned long pages;
445
446 pages = end - start;
447 zone = chain_alloc(ca, sizeof(struct mem_zone_bm_rtree));
448 if (!zone)
449 return NULL;
450
451 INIT_LIST_HEAD(&zone->nodes);
452 INIT_LIST_HEAD(&zone->leaves);
453 zone->start_pfn = start;
454 zone->end_pfn = end;
455 nr_blocks = DIV_ROUND_UP(pages, BM_BITS_PER_BLOCK);
456
457 for (i = 0; i < nr_blocks; i++) {
458 if (add_rtree_block(zone, gfp_mask, safe_needed, ca)) {
459 free_zone_bm_rtree(zone, PG_UNSAFE_CLEAR);
460 return NULL;
461 }
462 }
463
464 return zone;
465}
466
467/*
468 * free_zone_bm_rtree - Free the memory of the radix tree
469 *
470 * Free all node pages of the radix tree. The mem_zone_bm_rtree
471 * structure itself is not freed here nor are the rtree_node
472 * structs.
473 */
474static void free_zone_bm_rtree(struct mem_zone_bm_rtree *zone,
475 int clear_nosave_free)
476{
477 struct rtree_node *node;
478
479 list_for_each_entry(node, &zone->nodes, list)
480 free_image_page(node->data, clear_nosave_free);
481
482 list_for_each_entry(node, &zone->leaves, list)
483 free_image_page(node->data, clear_nosave_free);
484}
485
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700486static void memory_bm_position_reset(struct memory_bitmap *bm)
487{
Rafael J. Wysocki846705d2008-11-26 18:00:24 -0500488 bm->cur.block = list_entry(bm->blocks.next, struct bm_block, hook);
Akinobu Mita0d833042008-07-23 21:28:38 -0700489 bm->cur.bit = 0;
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700490}
491
492static void memory_bm_free(struct memory_bitmap *bm, int clear_nosave_free);
493
494/**
495 * create_bm_block_list - create a list of block bitmap objects
Wu Fengguang8de03072009-07-22 19:56:10 +0200496 * @pages - number of pages to track
Rafael J. Wysocki846705d2008-11-26 18:00:24 -0500497 * @list - list to put the allocated blocks into
498 * @ca - chain allocator to be used for allocating memory
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700499 */
Rafael J. Wysocki846705d2008-11-26 18:00:24 -0500500static int create_bm_block_list(unsigned long pages,
501 struct list_head *list,
502 struct chain_allocator *ca)
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700503{
Rafael J. Wysocki846705d2008-11-26 18:00:24 -0500504 unsigned int nr_blocks = DIV_ROUND_UP(pages, BM_BITS_PER_BLOCK);
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700505
506 while (nr_blocks-- > 0) {
507 struct bm_block *bb;
508
509 bb = chain_alloc(ca, sizeof(struct bm_block));
510 if (!bb)
Rafael J. Wysocki846705d2008-11-26 18:00:24 -0500511 return -ENOMEM;
512 list_add(&bb->hook, list);
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700513 }
Rafael J. Wysocki846705d2008-11-26 18:00:24 -0500514
515 return 0;
516}
517
518struct mem_extent {
519 struct list_head hook;
520 unsigned long start;
521 unsigned long end;
522};
523
524/**
525 * free_mem_extents - free a list of memory extents
526 * @list - list of extents to empty
527 */
528static void free_mem_extents(struct list_head *list)
529{
530 struct mem_extent *ext, *aux;
531
532 list_for_each_entry_safe(ext, aux, list, hook) {
533 list_del(&ext->hook);
534 kfree(ext);
535 }
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700536}
537
538/**
Rafael J. Wysocki846705d2008-11-26 18:00:24 -0500539 * create_mem_extents - create a list of memory extents representing
540 * contiguous ranges of PFNs
541 * @list - list to put the extents into
542 * @gfp_mask - mask to use for memory allocations
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700543 */
Rafael J. Wysocki846705d2008-11-26 18:00:24 -0500544static int create_mem_extents(struct list_head *list, gfp_t gfp_mask)
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700545{
Rafael J. Wysocki846705d2008-11-26 18:00:24 -0500546 struct zone *zone;
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700547
Rafael J. Wysocki846705d2008-11-26 18:00:24 -0500548 INIT_LIST_HEAD(list);
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700549
KOSAKI Motohiroee99c712009-03-31 15:19:31 -0700550 for_each_populated_zone(zone) {
Rafael J. Wysocki846705d2008-11-26 18:00:24 -0500551 unsigned long zone_start, zone_end;
552 struct mem_extent *ext, *cur, *aux;
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700553
Rafael J. Wysocki846705d2008-11-26 18:00:24 -0500554 zone_start = zone->zone_start_pfn;
Xishi Qiuc33bc312013-09-11 14:21:44 -0700555 zone_end = zone_end_pfn(zone);
Rafael J. Wysocki846705d2008-11-26 18:00:24 -0500556
557 list_for_each_entry(ext, list, hook)
558 if (zone_start <= ext->end)
559 break;
560
561 if (&ext->hook == list || zone_end < ext->start) {
562 /* New extent is necessary */
563 struct mem_extent *new_ext;
564
565 new_ext = kzalloc(sizeof(struct mem_extent), gfp_mask);
566 if (!new_ext) {
567 free_mem_extents(list);
568 return -ENOMEM;
569 }
570 new_ext->start = zone_start;
571 new_ext->end = zone_end;
572 list_add_tail(&new_ext->hook, &ext->hook);
573 continue;
574 }
575
576 /* Merge this zone's range of PFNs with the existing one */
577 if (zone_start < ext->start)
578 ext->start = zone_start;
579 if (zone_end > ext->end)
580 ext->end = zone_end;
581
582 /* More merging may be possible */
583 cur = ext;
584 list_for_each_entry_safe_continue(cur, aux, list, hook) {
585 if (zone_end < cur->start)
586 break;
587 if (zone_end < cur->end)
588 ext->end = cur->end;
589 list_del(&cur->hook);
590 kfree(cur);
591 }
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700592 }
Rafael J. Wysocki846705d2008-11-26 18:00:24 -0500593
594 return 0;
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700595}
596
597/**
598 * memory_bm_create - allocate memory for a memory bitmap
599 */
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700600static int
601memory_bm_create(struct memory_bitmap *bm, gfp_t gfp_mask, int safe_needed)
602{
603 struct chain_allocator ca;
Rafael J. Wysocki846705d2008-11-26 18:00:24 -0500604 struct list_head mem_extents;
605 struct mem_extent *ext;
606 int error;
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700607
608 chain_init(&ca, gfp_mask, safe_needed);
Rafael J. Wysocki846705d2008-11-26 18:00:24 -0500609 INIT_LIST_HEAD(&bm->blocks);
Joerg Roedelf469f022014-07-21 12:26:57 +0200610 INIT_LIST_HEAD(&bm->zones);
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700611
Rafael J. Wysocki846705d2008-11-26 18:00:24 -0500612 error = create_mem_extents(&mem_extents, gfp_mask);
613 if (error)
614 return error;
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700615
Rafael J. Wysocki846705d2008-11-26 18:00:24 -0500616 list_for_each_entry(ext, &mem_extents, hook) {
Joerg Roedelf469f022014-07-21 12:26:57 +0200617 struct mem_zone_bm_rtree *zone;
Rafael J. Wysocki846705d2008-11-26 18:00:24 -0500618 struct bm_block *bb;
619 unsigned long pfn = ext->start;
620 unsigned long pages = ext->end - ext->start;
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700621
Rafael J. Wysocki846705d2008-11-26 18:00:24 -0500622 bb = list_entry(bm->blocks.prev, struct bm_block, hook);
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700623
Rafael J. Wysocki846705d2008-11-26 18:00:24 -0500624 error = create_bm_block_list(pages, bm->blocks.prev, &ca);
625 if (error)
626 goto Error;
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700627
Rafael J. Wysocki846705d2008-11-26 18:00:24 -0500628 list_for_each_entry_continue(bb, &bm->blocks, hook) {
629 bb->data = get_image_page(gfp_mask, safe_needed);
630 if (!bb->data) {
631 error = -ENOMEM;
632 goto Error;
633 }
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700634
635 bb->start_pfn = pfn;
Rafael J. Wysocki846705d2008-11-26 18:00:24 -0500636 if (pages >= BM_BITS_PER_BLOCK) {
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700637 pfn += BM_BITS_PER_BLOCK;
Rafael J. Wysocki846705d2008-11-26 18:00:24 -0500638 pages -= BM_BITS_PER_BLOCK;
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700639 } else {
640 /* This is executed only once in the loop */
Rafael J. Wysocki846705d2008-11-26 18:00:24 -0500641 pfn += pages;
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700642 }
643 bb->end_pfn = pfn;
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700644 }
Joerg Roedelf469f022014-07-21 12:26:57 +0200645
646 zone = create_zone_bm_rtree(gfp_mask, safe_needed, &ca,
647 ext->start, ext->end);
648 if (!zone)
649 goto Error;
650 list_add_tail(&zone->list, &bm->zones);
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700651 }
Rafael J. Wysocki846705d2008-11-26 18:00:24 -0500652
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700653 bm->p_list = ca.chain;
654 memory_bm_position_reset(bm);
Rafael J. Wysocki846705d2008-11-26 18:00:24 -0500655 Exit:
656 free_mem_extents(&mem_extents);
657 return error;
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700658
Rafael J. Wysocki846705d2008-11-26 18:00:24 -0500659 Error:
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700660 bm->p_list = ca.chain;
661 memory_bm_free(bm, PG_UNSAFE_CLEAR);
Rafael J. Wysocki846705d2008-11-26 18:00:24 -0500662 goto Exit;
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700663}
664
665/**
666 * memory_bm_free - free memory occupied by the memory bitmap @bm
667 */
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700668static void memory_bm_free(struct memory_bitmap *bm, int clear_nosave_free)
669{
Joerg Roedelf469f022014-07-21 12:26:57 +0200670 struct mem_zone_bm_rtree *zone;
Rafael J. Wysocki846705d2008-11-26 18:00:24 -0500671 struct bm_block *bb;
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700672
Rafael J. Wysocki846705d2008-11-26 18:00:24 -0500673 list_for_each_entry(bb, &bm->blocks, hook)
674 if (bb->data)
675 free_image_page(bb->data, clear_nosave_free);
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700676
Joerg Roedelf469f022014-07-21 12:26:57 +0200677 list_for_each_entry(zone, &bm->zones, list)
678 free_zone_bm_rtree(zone, clear_nosave_free);
679
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700680 free_list_of_pages(bm->p_list, clear_nosave_free);
Rafael J. Wysocki846705d2008-11-26 18:00:24 -0500681
Joerg Roedelf469f022014-07-21 12:26:57 +0200682 INIT_LIST_HEAD(&bm->zones);
Rafael J. Wysocki846705d2008-11-26 18:00:24 -0500683 INIT_LIST_HEAD(&bm->blocks);
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700684}
685
686/**
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -0700687 * memory_bm_find_bit - find the bit in the bitmap @bm that corresponds
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700688 * to given pfn. The cur_zone_bm member of @bm and the cur_block member
689 * of @bm->cur_zone_bm are updated.
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700690 */
Rafael J. Wysockia82f7112008-03-12 00:34:57 +0100691static int memory_bm_find_bit(struct memory_bitmap *bm, unsigned long pfn,
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -0700692 void **addr, unsigned int *bit_nr)
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700693{
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700694 struct bm_block *bb;
695
Rafael J. Wysocki846705d2008-11-26 18:00:24 -0500696 /*
697 * Check if the pfn corresponds to the current bitmap block and find
698 * the block where it fits if this is not the case.
699 */
700 bb = bm->cur.block;
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700701 if (pfn < bb->start_pfn)
Rafael J. Wysocki846705d2008-11-26 18:00:24 -0500702 list_for_each_entry_continue_reverse(bb, &bm->blocks, hook)
703 if (pfn >= bb->start_pfn)
704 break;
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700705
Rafael J. Wysocki846705d2008-11-26 18:00:24 -0500706 if (pfn >= bb->end_pfn)
707 list_for_each_entry_continue(bb, &bm->blocks, hook)
708 if (pfn >= bb->start_pfn && pfn < bb->end_pfn)
709 break;
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -0700710
Rafael J. Wysocki846705d2008-11-26 18:00:24 -0500711 if (&bb->hook == &bm->blocks)
712 return -EFAULT;
713
714 /* The block has been found */
715 bm->cur.block = bb;
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700716 pfn -= bb->start_pfn;
Rafael J. Wysocki846705d2008-11-26 18:00:24 -0500717 bm->cur.bit = pfn + 1;
Akinobu Mita0d833042008-07-23 21:28:38 -0700718 *bit_nr = pfn;
719 *addr = bb->data;
Rafael J. Wysockia82f7112008-03-12 00:34:57 +0100720 return 0;
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -0700721}
722
Joerg Roedel07a33822014-07-21 12:26:58 +0200723/*
724 * memory_rtree_find_bit - Find the bit for pfn in the memory
725 * bitmap
726 *
727 * Walks the radix tree to find the page which contains the bit for
728 * pfn and returns the bit position in **addr and *bit_nr.
729 */
730static int memory_rtree_find_bit(struct memory_bitmap *bm, unsigned long pfn,
731 void **addr, unsigned int *bit_nr)
732{
733 struct mem_zone_bm_rtree *curr, *zone;
734 struct rtree_node *node;
735 int i, block_nr;
736
737 zone = NULL;
738
739 /* Find the right zone */
740 list_for_each_entry(curr, &bm->zones, list) {
741 if (pfn >= curr->start_pfn && pfn < curr->end_pfn) {
742 zone = curr;
743 break;
744 }
745 }
746
747 if (!zone)
748 return -EFAULT;
749
750 /*
751 * We have a zone. Now walk the radix tree to find the leave
752 * node for our pfn.
753 */
754 node = zone->rtree;
755 block_nr = (pfn - zone->start_pfn) >> BM_BLOCK_SHIFT;
756
757 for (i = zone->levels; i > 0; i--) {
758 int index;
759
760 index = block_nr >> ((i - 1) * BM_RTREE_LEVEL_SHIFT);
761 index &= BM_RTREE_LEVEL_MASK;
762 BUG_ON(node->data[index] == 0);
763 node = (struct rtree_node *)node->data[index];
764 }
765
766 /* Set return values */
767 *addr = node->data;
768 *bit_nr = (pfn - zone->start_pfn) & BM_BLOCK_MASK;
769
770 return 0;
771}
772
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -0700773static void memory_bm_set_bit(struct memory_bitmap *bm, unsigned long pfn)
774{
775 void *addr;
776 unsigned int bit;
Rafael J. Wysockia82f7112008-03-12 00:34:57 +0100777 int error;
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -0700778
Rafael J. Wysockia82f7112008-03-12 00:34:57 +0100779 error = memory_bm_find_bit(bm, pfn, &addr, &bit);
780 BUG_ON(error);
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -0700781 set_bit(bit, addr);
Joerg Roedel07a33822014-07-21 12:26:58 +0200782
783 error = memory_rtree_find_bit(bm, pfn, &addr, &bit);
784 BUG_ON(error);
785 set_bit(bit, addr);
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -0700786}
787
Rafael J. Wysockia82f7112008-03-12 00:34:57 +0100788static int mem_bm_set_bit_check(struct memory_bitmap *bm, unsigned long pfn)
789{
790 void *addr;
791 unsigned int bit;
792 int error;
793
794 error = memory_bm_find_bit(bm, pfn, &addr, &bit);
795 if (!error)
796 set_bit(bit, addr);
Joerg Roedel07a33822014-07-21 12:26:58 +0200797 else
798 return error;
799
800 error = memory_rtree_find_bit(bm, pfn, &addr, &bit);
801 if (!error)
802 set_bit(bit, addr);
803
Rafael J. Wysockia82f7112008-03-12 00:34:57 +0100804 return error;
805}
806
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -0700807static void memory_bm_clear_bit(struct memory_bitmap *bm, unsigned long pfn)
808{
809 void *addr;
810 unsigned int bit;
Rafael J. Wysockia82f7112008-03-12 00:34:57 +0100811 int error;
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -0700812
Rafael J. Wysockia82f7112008-03-12 00:34:57 +0100813 error = memory_bm_find_bit(bm, pfn, &addr, &bit);
814 BUG_ON(error);
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -0700815 clear_bit(bit, addr);
Joerg Roedel07a33822014-07-21 12:26:58 +0200816
817 error = memory_rtree_find_bit(bm, pfn, &addr, &bit);
818 BUG_ON(error);
819 clear_bit(bit, addr);
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -0700820}
821
822static int memory_bm_test_bit(struct memory_bitmap *bm, unsigned long pfn)
823{
824 void *addr;
825 unsigned int bit;
Joerg Roedel07a33822014-07-21 12:26:58 +0200826 int error, error2;
827 int v;
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -0700828
Rafael J. Wysockia82f7112008-03-12 00:34:57 +0100829 error = memory_bm_find_bit(bm, pfn, &addr, &bit);
830 BUG_ON(error);
Joerg Roedel07a33822014-07-21 12:26:58 +0200831 v = test_bit(bit, addr);
832
833 error2 = memory_rtree_find_bit(bm, pfn, &addr, &bit);
834 BUG_ON(error2);
835
836 WARN_ON_ONCE(v != test_bit(bit, addr));
837
838 return v;
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700839}
840
Rafael J. Wysocki69643272008-11-11 21:32:44 +0100841static bool memory_bm_pfn_present(struct memory_bitmap *bm, unsigned long pfn)
842{
843 void *addr;
844 unsigned int bit;
Joerg Roedel07a33822014-07-21 12:26:58 +0200845 int present;
Rafael J. Wysocki69643272008-11-11 21:32:44 +0100846
Joerg Roedel07a33822014-07-21 12:26:58 +0200847 present = !memory_bm_find_bit(bm, pfn, &addr, &bit);
848
849 WARN_ON_ONCE(present != !memory_rtree_find_bit(bm, pfn, &addr, &bit));
850
851 return present;
Rafael J. Wysocki69643272008-11-11 21:32:44 +0100852}
853
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700854/**
855 * memory_bm_next_pfn - find the pfn that corresponds to the next set bit
856 * in the bitmap @bm. If the pfn cannot be found, BM_END_OF_MAP is
857 * returned.
858 *
859 * It is required to run memory_bm_position_reset() before the first call to
860 * this function.
861 */
862
863static unsigned long memory_bm_next_pfn(struct memory_bitmap *bm)
864{
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700865 struct bm_block *bb;
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700866 int bit;
867
Rafael J. Wysocki846705d2008-11-26 18:00:24 -0500868 bb = bm->cur.block;
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700869 do {
Rafael J. Wysocki846705d2008-11-26 18:00:24 -0500870 bit = bm->cur.bit;
871 bit = find_next_bit(bb->data, bm_block_bits(bb), bit);
872 if (bit < bm_block_bits(bb))
873 goto Return_pfn;
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700874
Rafael J. Wysocki846705d2008-11-26 18:00:24 -0500875 bb = list_entry(bb->hook.next, struct bm_block, hook);
876 bm->cur.block = bb;
877 bm->cur.bit = 0;
878 } while (&bb->hook != &bm->blocks);
879
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700880 memory_bm_position_reset(bm);
881 return BM_END_OF_MAP;
882
Rafael J. Wysocki59a493352006-12-06 20:34:44 -0800883 Return_pfn:
Akinobu Mita0d833042008-07-23 21:28:38 -0700884 bm->cur.bit = bit + 1;
885 return bb->start_pfn + bit;
Rafael J. Wysockib788db72006-09-25 23:32:54 -0700886}
887
888/**
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -0700889 * This structure represents a range of page frames the contents of which
890 * should not be saved during the suspend.
891 */
892
893struct nosave_region {
894 struct list_head list;
895 unsigned long start_pfn;
896 unsigned long end_pfn;
897};
898
899static LIST_HEAD(nosave_regions);
900
901/**
902 * register_nosave_region - register a range of page frames the contents
903 * of which should not be saved during the suspend (to be used in the early
904 * initialization code)
905 */
906
907void __init
Johannes Berg940d67f2007-05-08 19:23:49 +1000908__register_nosave_region(unsigned long start_pfn, unsigned long end_pfn,
909 int use_kmalloc)
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -0700910{
911 struct nosave_region *region;
912
913 if (start_pfn >= end_pfn)
914 return;
915
916 if (!list_empty(&nosave_regions)) {
917 /* Try to extend the previous region (they should be sorted) */
918 region = list_entry(nosave_regions.prev,
919 struct nosave_region, list);
920 if (region->end_pfn == start_pfn) {
921 region->end_pfn = end_pfn;
922 goto Report;
923 }
924 }
Johannes Berg940d67f2007-05-08 19:23:49 +1000925 if (use_kmalloc) {
926 /* during init, this shouldn't fail */
927 region = kmalloc(sizeof(struct nosave_region), GFP_KERNEL);
928 BUG_ON(!region);
929 } else
930 /* This allocation cannot fail */
Santosh Shilimkarc2f69cd2014-01-21 15:50:27 -0800931 region = memblock_virt_alloc(sizeof(struct nosave_region), 0);
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -0700932 region->start_pfn = start_pfn;
933 region->end_pfn = end_pfn;
934 list_add_tail(&region->list, &nosave_regions);
935 Report:
Bjorn Helgaascd38ca82013-06-03 18:20:29 +0000936 printk(KERN_INFO "PM: Registered nosave memory: [mem %#010llx-%#010llx]\n",
937 (unsigned long long) start_pfn << PAGE_SHIFT,
938 ((unsigned long long) end_pfn << PAGE_SHIFT) - 1);
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -0700939}
940
941/*
942 * Set bits in this map correspond to the page frames the contents of which
943 * should not be saved during the suspend.
944 */
945static struct memory_bitmap *forbidden_pages_map;
946
947/* Set bits in this map correspond to free page frames. */
948static struct memory_bitmap *free_pages_map;
949
950/*
951 * Each page frame allocated for creating the image is marked by setting the
952 * corresponding bits in forbidden_pages_map and free_pages_map simultaneously
953 */
954
955void swsusp_set_page_free(struct page *page)
956{
957 if (free_pages_map)
958 memory_bm_set_bit(free_pages_map, page_to_pfn(page));
959}
960
961static int swsusp_page_is_free(struct page *page)
962{
963 return free_pages_map ?
964 memory_bm_test_bit(free_pages_map, page_to_pfn(page)) : 0;
965}
966
967void swsusp_unset_page_free(struct page *page)
968{
969 if (free_pages_map)
970 memory_bm_clear_bit(free_pages_map, page_to_pfn(page));
971}
972
973static void swsusp_set_page_forbidden(struct page *page)
974{
975 if (forbidden_pages_map)
976 memory_bm_set_bit(forbidden_pages_map, page_to_pfn(page));
977}
978
979int swsusp_page_is_forbidden(struct page *page)
980{
981 return forbidden_pages_map ?
982 memory_bm_test_bit(forbidden_pages_map, page_to_pfn(page)) : 0;
983}
984
985static void swsusp_unset_page_forbidden(struct page *page)
986{
987 if (forbidden_pages_map)
988 memory_bm_clear_bit(forbidden_pages_map, page_to_pfn(page));
989}
990
991/**
992 * mark_nosave_pages - set bits corresponding to the page frames the
993 * contents of which should not be saved in a given bitmap.
994 */
995
996static void mark_nosave_pages(struct memory_bitmap *bm)
997{
998 struct nosave_region *region;
999
1000 if (list_empty(&nosave_regions))
1001 return;
1002
1003 list_for_each_entry(region, &nosave_regions, list) {
1004 unsigned long pfn;
1005
Bjorn Helgaas69f1d472012-02-14 22:20:52 +01001006 pr_debug("PM: Marking nosave pages: [mem %#010llx-%#010llx]\n",
1007 (unsigned long long) region->start_pfn << PAGE_SHIFT,
1008 ((unsigned long long) region->end_pfn << PAGE_SHIFT)
1009 - 1);
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -07001010
1011 for (pfn = region->start_pfn; pfn < region->end_pfn; pfn++)
Rafael J. Wysockia82f7112008-03-12 00:34:57 +01001012 if (pfn_valid(pfn)) {
1013 /*
1014 * It is safe to ignore the result of
1015 * mem_bm_set_bit_check() here, since we won't
1016 * touch the PFNs for which the error is
1017 * returned anyway.
1018 */
1019 mem_bm_set_bit_check(bm, pfn);
1020 }
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -07001021 }
1022}
1023
1024/**
1025 * create_basic_memory_bitmaps - create bitmaps needed for marking page
1026 * frames that should not be saved and free page frames. The pointers
1027 * forbidden_pages_map and free_pages_map are only modified if everything
1028 * goes well, because we don't want the bits to be used before both bitmaps
1029 * are set up.
1030 */
1031
1032int create_basic_memory_bitmaps(void)
1033{
1034 struct memory_bitmap *bm1, *bm2;
1035 int error = 0;
1036
Rafael J. Wysockiaab17282013-09-30 19:40:56 +02001037 if (forbidden_pages_map && free_pages_map)
1038 return 0;
1039 else
1040 BUG_ON(forbidden_pages_map || free_pages_map);
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -07001041
Rafael J. Wysocki0709db62007-05-06 14:50:45 -07001042 bm1 = kzalloc(sizeof(struct memory_bitmap), GFP_KERNEL);
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -07001043 if (!bm1)
1044 return -ENOMEM;
1045
Rafael J. Wysocki0709db62007-05-06 14:50:45 -07001046 error = memory_bm_create(bm1, GFP_KERNEL, PG_ANY);
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -07001047 if (error)
1048 goto Free_first_object;
1049
Rafael J. Wysocki0709db62007-05-06 14:50:45 -07001050 bm2 = kzalloc(sizeof(struct memory_bitmap), GFP_KERNEL);
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -07001051 if (!bm2)
1052 goto Free_first_bitmap;
1053
Rafael J. Wysocki0709db62007-05-06 14:50:45 -07001054 error = memory_bm_create(bm2, GFP_KERNEL, PG_ANY);
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -07001055 if (error)
1056 goto Free_second_object;
1057
1058 forbidden_pages_map = bm1;
1059 free_pages_map = bm2;
1060 mark_nosave_pages(forbidden_pages_map);
1061
Rafael J. Wysocki23976722007-12-08 02:09:43 +01001062 pr_debug("PM: Basic memory bitmaps created\n");
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -07001063
1064 return 0;
1065
1066 Free_second_object:
1067 kfree(bm2);
1068 Free_first_bitmap:
1069 memory_bm_free(bm1, PG_UNSAFE_CLEAR);
1070 Free_first_object:
1071 kfree(bm1);
1072 return -ENOMEM;
1073}
1074
1075/**
1076 * free_basic_memory_bitmaps - free memory bitmaps allocated by
1077 * create_basic_memory_bitmaps(). The auxiliary pointers are necessary
1078 * so that the bitmaps themselves are not referred to while they are being
1079 * freed.
1080 */
1081
1082void free_basic_memory_bitmaps(void)
1083{
1084 struct memory_bitmap *bm1, *bm2;
1085
Rafael J. Wysocki6a0c7cd2013-11-14 23:26:58 +01001086 if (WARN_ON(!(forbidden_pages_map && free_pages_map)))
1087 return;
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -07001088
1089 bm1 = forbidden_pages_map;
1090 bm2 = free_pages_map;
1091 forbidden_pages_map = NULL;
1092 free_pages_map = NULL;
1093 memory_bm_free(bm1, PG_UNSAFE_CLEAR);
1094 kfree(bm1);
1095 memory_bm_free(bm2, PG_UNSAFE_CLEAR);
1096 kfree(bm2);
1097
Rafael J. Wysocki23976722007-12-08 02:09:43 +01001098 pr_debug("PM: Basic memory bitmaps freed\n");
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -07001099}
1100
1101/**
Rafael J. Wysockib788db72006-09-25 23:32:54 -07001102 * snapshot_additional_pages - estimate the number of additional pages
1103 * be needed for setting up the suspend image data structures for given
1104 * zone (usually the returned value is greater than the exact number)
1105 */
1106
1107unsigned int snapshot_additional_pages(struct zone *zone)
1108{
Joerg Roedelf469f022014-07-21 12:26:57 +02001109 unsigned int rtree, nodes;
Rafael J. Wysockib788db72006-09-25 23:32:54 -07001110 unsigned int res;
1111
1112 res = DIV_ROUND_UP(zone->spanned_pages, BM_BITS_PER_BLOCK);
Namhyung Kim160cb5a2012-01-19 23:23:10 +01001113 res += DIV_ROUND_UP(res * sizeof(struct bm_block),
1114 LINKED_PAGE_DATA_SIZE);
Joerg Roedelf469f022014-07-21 12:26:57 +02001115 rtree = nodes = DIV_ROUND_UP(zone->spanned_pages, BM_BITS_PER_BLOCK);
1116 rtree += DIV_ROUND_UP(rtree * sizeof(struct rtree_node),
1117 LINKED_PAGE_DATA_SIZE);
1118 while (nodes > 1) {
1119 nodes = DIV_ROUND_UP(nodes, BM_ENTRIES_PER_LEVEL);
1120 rtree += nodes;
1121 }
1122
1123 return 2 * (res + rtree);
Rafael J. Wysockib788db72006-09-25 23:32:54 -07001124}
1125
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001126#ifdef CONFIG_HIGHMEM
1127/**
1128 * count_free_highmem_pages - compute the total number of free highmem
1129 * pages, system-wide.
1130 */
1131
1132static unsigned int count_free_highmem_pages(void)
1133{
1134 struct zone *zone;
1135 unsigned int cnt = 0;
1136
KOSAKI Motohiroee99c712009-03-31 15:19:31 -07001137 for_each_populated_zone(zone)
1138 if (is_highmem(zone))
Christoph Lameterd23ad422007-02-10 01:43:02 -08001139 cnt += zone_page_state(zone, NR_FREE_PAGES);
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001140
1141 return cnt;
1142}
1143
1144/**
1145 * saveable_highmem_page - Determine whether a highmem page should be
1146 * included in the suspend image.
1147 *
1148 * We should save the page if it isn't Nosave or NosaveFree, or Reserved,
1149 * and it isn't a part of a free chunk of pages.
1150 */
Rafael J. Wysocki846705d2008-11-26 18:00:24 -05001151static struct page *saveable_highmem_page(struct zone *zone, unsigned long pfn)
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001152{
1153 struct page *page;
1154
1155 if (!pfn_valid(pfn))
1156 return NULL;
1157
1158 page = pfn_to_page(pfn);
Rafael J. Wysocki846705d2008-11-26 18:00:24 -05001159 if (page_zone(page) != zone)
1160 return NULL;
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001161
1162 BUG_ON(!PageHighMem(page));
1163
Rafael J. Wysocki7be98232007-05-06 14:50:42 -07001164 if (swsusp_page_is_forbidden(page) || swsusp_page_is_free(page) ||
1165 PageReserved(page))
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001166 return NULL;
1167
Stanislaw Gruszkac6968e72012-01-10 15:07:31 -08001168 if (page_is_guard(page))
1169 return NULL;
1170
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001171 return page;
1172}
1173
1174/**
1175 * count_highmem_pages - compute the total number of saveable highmem
1176 * pages.
1177 */
1178
Rafael J. Wysockife419532009-06-11 23:11:17 +02001179static unsigned int count_highmem_pages(void)
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001180{
1181 struct zone *zone;
1182 unsigned int n = 0;
1183
Gerald Schaefer98e73dc2009-07-22 00:36:56 +02001184 for_each_populated_zone(zone) {
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001185 unsigned long pfn, max_zone_pfn;
1186
1187 if (!is_highmem(zone))
1188 continue;
1189
1190 mark_free_pages(zone);
Xishi Qiuc33bc312013-09-11 14:21:44 -07001191 max_zone_pfn = zone_end_pfn(zone);
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001192 for (pfn = zone->zone_start_pfn; pfn < max_zone_pfn; pfn++)
Rafael J. Wysocki846705d2008-11-26 18:00:24 -05001193 if (saveable_highmem_page(zone, pfn))
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001194 n++;
1195 }
1196 return n;
1197}
1198#else
Rafael J. Wysocki846705d2008-11-26 18:00:24 -05001199static inline void *saveable_highmem_page(struct zone *z, unsigned long p)
1200{
1201 return NULL;
1202}
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001203#endif /* CONFIG_HIGHMEM */
1204
Rafael J. Wysockif6143aa2006-09-25 23:32:50 -07001205/**
Rafael J. Wysocki8a235ef2008-02-20 01:47:44 +01001206 * saveable_page - Determine whether a non-highmem page should be included
1207 * in the suspend image.
Rafael J. Wysocki25761b62005-10-30 14:59:56 -08001208 *
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001209 * We should save the page if it isn't Nosave, and is not in the range
1210 * of pages statically defined as 'unsaveable', and it isn't a part of
1211 * a free chunk of pages.
Rafael J. Wysocki25761b62005-10-30 14:59:56 -08001212 */
Rafael J. Wysocki846705d2008-11-26 18:00:24 -05001213static struct page *saveable_page(struct zone *zone, unsigned long pfn)
Rafael J. Wysocki25761b62005-10-30 14:59:56 -08001214{
Pavel Machekde491862005-10-30 14:59:59 -08001215 struct page *page;
Rafael J. Wysocki25761b62005-10-30 14:59:56 -08001216
1217 if (!pfn_valid(pfn))
Rafael J. Wysockiae83c5ee2006-09-25 23:32:45 -07001218 return NULL;
Rafael J. Wysocki25761b62005-10-30 14:59:56 -08001219
1220 page = pfn_to_page(pfn);
Rafael J. Wysocki846705d2008-11-26 18:00:24 -05001221 if (page_zone(page) != zone)
1222 return NULL;
Rafael J. Wysocki25761b62005-10-30 14:59:56 -08001223
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001224 BUG_ON(PageHighMem(page));
1225
Rafael J. Wysocki7be98232007-05-06 14:50:42 -07001226 if (swsusp_page_is_forbidden(page) || swsusp_page_is_free(page))
Rafael J. Wysockiae83c5ee2006-09-25 23:32:45 -07001227 return NULL;
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001228
Rafael J. Wysocki8a235ef2008-02-20 01:47:44 +01001229 if (PageReserved(page)
1230 && (!kernel_page_present(page) || pfn_is_nosave(pfn)))
Rafael J. Wysockiae83c5ee2006-09-25 23:32:45 -07001231 return NULL;
Rafael J. Wysockiae83c5ee2006-09-25 23:32:45 -07001232
Stanislaw Gruszkac6968e72012-01-10 15:07:31 -08001233 if (page_is_guard(page))
1234 return NULL;
1235
Rafael J. Wysockiae83c5ee2006-09-25 23:32:45 -07001236 return page;
Rafael J. Wysocki25761b62005-10-30 14:59:56 -08001237}
1238
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001239/**
1240 * count_data_pages - compute the total number of saveable non-highmem
1241 * pages.
1242 */
1243
Rafael J. Wysockife419532009-06-11 23:11:17 +02001244static unsigned int count_data_pages(void)
Rafael J. Wysocki25761b62005-10-30 14:59:56 -08001245{
1246 struct zone *zone;
Rafael J. Wysockiae83c5ee2006-09-25 23:32:45 -07001247 unsigned long pfn, max_zone_pfn;
Pavel Machekdc19d502005-11-07 00:58:40 -08001248 unsigned int n = 0;
Rafael J. Wysocki25761b62005-10-30 14:59:56 -08001249
Gerald Schaefer98e73dc2009-07-22 00:36:56 +02001250 for_each_populated_zone(zone) {
Rafael J. Wysocki25761b62005-10-30 14:59:56 -08001251 if (is_highmem(zone))
1252 continue;
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001253
Rafael J. Wysocki25761b62005-10-30 14:59:56 -08001254 mark_free_pages(zone);
Xishi Qiuc33bc312013-09-11 14:21:44 -07001255 max_zone_pfn = zone_end_pfn(zone);
Rafael J. Wysockiae83c5ee2006-09-25 23:32:45 -07001256 for (pfn = zone->zone_start_pfn; pfn < max_zone_pfn; pfn++)
Rafael J. Wysocki846705d2008-11-26 18:00:24 -05001257 if (saveable_page(zone, pfn))
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001258 n++;
Rafael J. Wysocki25761b62005-10-30 14:59:56 -08001259 }
Rafael J. Wysockia0f49652005-10-30 14:59:57 -08001260 return n;
Rafael J. Wysocki25761b62005-10-30 14:59:56 -08001261}
1262
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001263/* This is needed, because copy_page and memcpy are not usable for copying
1264 * task structs.
1265 */
1266static inline void do_copy_page(long *dst, long *src)
Rafael J. Wysockif623f0d2006-09-25 23:32:49 -07001267{
1268 int n;
1269
Rafael J. Wysockif623f0d2006-09-25 23:32:49 -07001270 for (n = PAGE_SIZE / sizeof(long); n; n--)
1271 *dst++ = *src++;
1272}
1273
Rafael J. Wysocki8a235ef2008-02-20 01:47:44 +01001274
1275/**
1276 * safe_copy_page - check if the page we are going to copy is marked as
1277 * present in the kernel page tables (this always is the case if
1278 * CONFIG_DEBUG_PAGEALLOC is not set and in that case
1279 * kernel_page_present() always returns 'true').
1280 */
1281static void safe_copy_page(void *dst, struct page *s_page)
1282{
1283 if (kernel_page_present(s_page)) {
1284 do_copy_page(dst, page_address(s_page));
1285 } else {
1286 kernel_map_pages(s_page, 1, 1);
1287 do_copy_page(dst, page_address(s_page));
1288 kernel_map_pages(s_page, 1, 0);
1289 }
1290}
1291
1292
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001293#ifdef CONFIG_HIGHMEM
1294static inline struct page *
1295page_is_saveable(struct zone *zone, unsigned long pfn)
1296{
1297 return is_highmem(zone) ?
Rafael J. Wysocki846705d2008-11-26 18:00:24 -05001298 saveable_highmem_page(zone, pfn) : saveable_page(zone, pfn);
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001299}
1300
Rafael J. Wysocki8a235ef2008-02-20 01:47:44 +01001301static void copy_data_page(unsigned long dst_pfn, unsigned long src_pfn)
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001302{
1303 struct page *s_page, *d_page;
1304 void *src, *dst;
1305
1306 s_page = pfn_to_page(src_pfn);
1307 d_page = pfn_to_page(dst_pfn);
1308 if (PageHighMem(s_page)) {
Cong Wang0de9a1e2011-11-25 23:14:38 +08001309 src = kmap_atomic(s_page);
1310 dst = kmap_atomic(d_page);
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001311 do_copy_page(dst, src);
Cong Wang0de9a1e2011-11-25 23:14:38 +08001312 kunmap_atomic(dst);
1313 kunmap_atomic(src);
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001314 } else {
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001315 if (PageHighMem(d_page)) {
1316 /* Page pointed to by src may contain some kernel
1317 * data modified by kmap_atomic()
1318 */
Rafael J. Wysocki8a235ef2008-02-20 01:47:44 +01001319 safe_copy_page(buffer, s_page);
Cong Wang0de9a1e2011-11-25 23:14:38 +08001320 dst = kmap_atomic(d_page);
Jan Beulich3ecb01d2010-10-26 14:22:27 -07001321 copy_page(dst, buffer);
Cong Wang0de9a1e2011-11-25 23:14:38 +08001322 kunmap_atomic(dst);
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001323 } else {
Rafael J. Wysocki8a235ef2008-02-20 01:47:44 +01001324 safe_copy_page(page_address(d_page), s_page);
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001325 }
1326 }
1327}
1328#else
Rafael J. Wysocki846705d2008-11-26 18:00:24 -05001329#define page_is_saveable(zone, pfn) saveable_page(zone, pfn)
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001330
Rafael J. Wysocki8a235ef2008-02-20 01:47:44 +01001331static inline void copy_data_page(unsigned long dst_pfn, unsigned long src_pfn)
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001332{
Rafael J. Wysocki8a235ef2008-02-20 01:47:44 +01001333 safe_copy_page(page_address(pfn_to_page(dst_pfn)),
1334 pfn_to_page(src_pfn));
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001335}
1336#endif /* CONFIG_HIGHMEM */
1337
Rafael J. Wysockib788db72006-09-25 23:32:54 -07001338static void
1339copy_data_pages(struct memory_bitmap *copy_bm, struct memory_bitmap *orig_bm)
Rafael J. Wysocki25761b62005-10-30 14:59:56 -08001340{
1341 struct zone *zone;
Rafael J. Wysockib788db72006-09-25 23:32:54 -07001342 unsigned long pfn;
Rafael J. Wysocki25761b62005-10-30 14:59:56 -08001343
Gerald Schaefer98e73dc2009-07-22 00:36:56 +02001344 for_each_populated_zone(zone) {
Rafael J. Wysockib788db72006-09-25 23:32:54 -07001345 unsigned long max_zone_pfn;
1346
Rafael J. Wysocki25761b62005-10-30 14:59:56 -08001347 mark_free_pages(zone);
Xishi Qiuc33bc312013-09-11 14:21:44 -07001348 max_zone_pfn = zone_end_pfn(zone);
Rafael J. Wysockib788db72006-09-25 23:32:54 -07001349 for (pfn = zone->zone_start_pfn; pfn < max_zone_pfn; pfn++)
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001350 if (page_is_saveable(zone, pfn))
Rafael J. Wysockib788db72006-09-25 23:32:54 -07001351 memory_bm_set_bit(orig_bm, pfn);
Rafael J. Wysocki25761b62005-10-30 14:59:56 -08001352 }
Rafael J. Wysockib788db72006-09-25 23:32:54 -07001353 memory_bm_position_reset(orig_bm);
1354 memory_bm_position_reset(copy_bm);
Fengguang Wudf7c4872007-10-20 02:26:04 +02001355 for(;;) {
Rafael J. Wysockib788db72006-09-25 23:32:54 -07001356 pfn = memory_bm_next_pfn(orig_bm);
Fengguang Wudf7c4872007-10-20 02:26:04 +02001357 if (unlikely(pfn == BM_END_OF_MAP))
1358 break;
1359 copy_data_page(memory_bm_next_pfn(copy_bm), pfn);
1360 }
Rafael J. Wysocki25761b62005-10-30 14:59:56 -08001361}
1362
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001363/* Total number of image pages */
1364static unsigned int nr_copy_pages;
1365/* Number of pages needed for saving the original pfns of the image pages */
1366static unsigned int nr_meta_pages;
Rafael J. Wysocki64a473c2009-07-08 13:24:05 +02001367/*
1368 * Numbers of normal and highmem page frames allocated for hibernation image
1369 * before suspending devices.
1370 */
1371unsigned int alloc_normal, alloc_highmem;
1372/*
1373 * Memory bitmap used for marking saveable pages (during hibernation) or
1374 * hibernation image pages (during restore)
1375 */
1376static struct memory_bitmap orig_bm;
1377/*
1378 * Memory bitmap used during hibernation for marking allocated page frames that
1379 * will contain copies of saveable pages. During restore it is initially used
1380 * for marking hibernation image pages, but then the set bits from it are
1381 * duplicated in @orig_bm and it is released. On highmem systems it is next
1382 * used for marking "safe" highmem pages, but it has to be reinitialized for
1383 * this purpose.
1384 */
1385static struct memory_bitmap copy_bm;
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001386
Rafael J. Wysocki25761b62005-10-30 14:59:56 -08001387/**
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001388 * swsusp_free - free pages allocated for the suspend.
Rafael J. Wysockicd560bb2006-09-25 23:32:50 -07001389 *
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001390 * Suspend pages are alocated before the atomic copy is made, so we
1391 * need to release them after the resume.
Rafael J. Wysocki25761b62005-10-30 14:59:56 -08001392 */
1393
1394void swsusp_free(void)
1395{
1396 struct zone *zone;
Rafael J. Wysockiae83c5ee2006-09-25 23:32:45 -07001397 unsigned long pfn, max_zone_pfn;
Rafael J. Wysocki25761b62005-10-30 14:59:56 -08001398
Gerald Schaefer98e73dc2009-07-22 00:36:56 +02001399 for_each_populated_zone(zone) {
Xishi Qiuc33bc312013-09-11 14:21:44 -07001400 max_zone_pfn = zone_end_pfn(zone);
Rafael J. Wysockiae83c5ee2006-09-25 23:32:45 -07001401 for (pfn = zone->zone_start_pfn; pfn < max_zone_pfn; pfn++)
1402 if (pfn_valid(pfn)) {
1403 struct page *page = pfn_to_page(pfn);
1404
Rafael J. Wysocki7be98232007-05-06 14:50:42 -07001405 if (swsusp_page_is_forbidden(page) &&
1406 swsusp_page_is_free(page)) {
1407 swsusp_unset_page_forbidden(page);
1408 swsusp_unset_page_free(page);
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001409 __free_page(page);
Rafael J. Wysocki25761b62005-10-30 14:59:56 -08001410 }
1411 }
1412 }
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001413 nr_copy_pages = 0;
1414 nr_meta_pages = 0;
Rafael J. Wysocki75534b52006-09-25 23:32:52 -07001415 restore_pblist = NULL;
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -08001416 buffer = NULL;
Rafael J. Wysocki64a473c2009-07-08 13:24:05 +02001417 alloc_normal = 0;
1418 alloc_highmem = 0;
Rafael J. Wysocki25761b62005-10-30 14:59:56 -08001419}
1420
Rafael J. Wysocki4bb33432009-07-08 13:23:51 +02001421/* Helper functions used for the shrinking of memory. */
Rafael J. Wysockife419532009-06-11 23:11:17 +02001422
Rafael J. Wysocki4bb33432009-07-08 13:23:51 +02001423#define GFP_IMAGE (GFP_KERNEL | __GFP_NOWARN)
1424
1425/**
1426 * preallocate_image_pages - Allocate a number of pages for hibernation image
1427 * @nr_pages: Number of page frames to allocate.
1428 * @mask: GFP flags to use for the allocation.
1429 *
1430 * Return value: Number of page frames actually allocated
1431 */
1432static unsigned long preallocate_image_pages(unsigned long nr_pages, gfp_t mask)
Rafael J. Wysockife419532009-06-11 23:11:17 +02001433{
Rafael J. Wysocki4bb33432009-07-08 13:23:51 +02001434 unsigned long nr_alloc = 0;
1435
1436 while (nr_pages > 0) {
Rafael J. Wysocki64a473c2009-07-08 13:24:05 +02001437 struct page *page;
1438
1439 page = alloc_image_page(mask);
1440 if (!page)
Rafael J. Wysocki4bb33432009-07-08 13:23:51 +02001441 break;
Rafael J. Wysocki64a473c2009-07-08 13:24:05 +02001442 memory_bm_set_bit(&copy_bm, page_to_pfn(page));
1443 if (PageHighMem(page))
1444 alloc_highmem++;
1445 else
1446 alloc_normal++;
Rafael J. Wysocki4bb33432009-07-08 13:23:51 +02001447 nr_pages--;
1448 nr_alloc++;
1449 }
1450
1451 return nr_alloc;
Rafael J. Wysockife419532009-06-11 23:11:17 +02001452}
1453
Rafael J. Wysocki67150452010-09-11 20:58:27 +02001454static unsigned long preallocate_image_memory(unsigned long nr_pages,
1455 unsigned long avail_normal)
Rafael J. Wysocki4bb33432009-07-08 13:23:51 +02001456{
Rafael J. Wysocki67150452010-09-11 20:58:27 +02001457 unsigned long alloc;
1458
1459 if (avail_normal <= alloc_normal)
1460 return 0;
1461
1462 alloc = avail_normal - alloc_normal;
1463 if (nr_pages < alloc)
1464 alloc = nr_pages;
1465
1466 return preallocate_image_pages(alloc, GFP_IMAGE);
Rafael J. Wysocki4bb33432009-07-08 13:23:51 +02001467}
1468
1469#ifdef CONFIG_HIGHMEM
1470static unsigned long preallocate_image_highmem(unsigned long nr_pages)
1471{
1472 return preallocate_image_pages(nr_pages, GFP_IMAGE | __GFP_HIGHMEM);
1473}
1474
1475/**
1476 * __fraction - Compute (an approximation of) x * (multiplier / base)
1477 */
1478static unsigned long __fraction(u64 x, u64 multiplier, u64 base)
1479{
1480 x *= multiplier;
1481 do_div(x, base);
1482 return (unsigned long)x;
1483}
1484
1485static unsigned long preallocate_highmem_fraction(unsigned long nr_pages,
1486 unsigned long highmem,
1487 unsigned long total)
1488{
1489 unsigned long alloc = __fraction(nr_pages, highmem, total);
1490
1491 return preallocate_image_pages(alloc, GFP_IMAGE | __GFP_HIGHMEM);
1492}
1493#else /* CONFIG_HIGHMEM */
1494static inline unsigned long preallocate_image_highmem(unsigned long nr_pages)
1495{
1496 return 0;
1497}
1498
1499static inline unsigned long preallocate_highmem_fraction(unsigned long nr_pages,
1500 unsigned long highmem,
1501 unsigned long total)
1502{
1503 return 0;
1504}
1505#endif /* CONFIG_HIGHMEM */
1506
1507/**
Rafael J. Wysocki64a473c2009-07-08 13:24:05 +02001508 * free_unnecessary_pages - Release preallocated pages not needed for the image
1509 */
1510static void free_unnecessary_pages(void)
1511{
Rafael J. Wysocki67150452010-09-11 20:58:27 +02001512 unsigned long save, to_free_normal, to_free_highmem;
Rafael J. Wysocki64a473c2009-07-08 13:24:05 +02001513
Rafael J. Wysocki67150452010-09-11 20:58:27 +02001514 save = count_data_pages();
1515 if (alloc_normal >= save) {
1516 to_free_normal = alloc_normal - save;
1517 save = 0;
1518 } else {
1519 to_free_normal = 0;
1520 save -= alloc_normal;
1521 }
1522 save += count_highmem_pages();
1523 if (alloc_highmem >= save) {
1524 to_free_highmem = alloc_highmem - save;
Rafael J. Wysocki64a473c2009-07-08 13:24:05 +02001525 } else {
1526 to_free_highmem = 0;
Rafael J. Wysocki4d4cf232011-07-06 20:15:23 +02001527 save -= alloc_highmem;
1528 if (to_free_normal > save)
1529 to_free_normal -= save;
1530 else
1531 to_free_normal = 0;
Rafael J. Wysocki64a473c2009-07-08 13:24:05 +02001532 }
1533
1534 memory_bm_position_reset(&copy_bm);
1535
Rafael J. Wysockia9c9b442010-02-25 22:32:37 +01001536 while (to_free_normal > 0 || to_free_highmem > 0) {
Rafael J. Wysocki64a473c2009-07-08 13:24:05 +02001537 unsigned long pfn = memory_bm_next_pfn(&copy_bm);
1538 struct page *page = pfn_to_page(pfn);
1539
1540 if (PageHighMem(page)) {
1541 if (!to_free_highmem)
1542 continue;
1543 to_free_highmem--;
1544 alloc_highmem--;
1545 } else {
1546 if (!to_free_normal)
1547 continue;
1548 to_free_normal--;
1549 alloc_normal--;
1550 }
1551 memory_bm_clear_bit(&copy_bm, pfn);
1552 swsusp_unset_page_forbidden(page);
1553 swsusp_unset_page_free(page);
1554 __free_page(page);
1555 }
1556}
1557
1558/**
Rafael J. Wysockief4aede2009-07-08 13:24:12 +02001559 * minimum_image_size - Estimate the minimum acceptable size of an image
1560 * @saveable: Number of saveable pages in the system.
1561 *
1562 * We want to avoid attempting to free too much memory too hard, so estimate the
1563 * minimum acceptable size of a hibernation image to use as the lower limit for
1564 * preallocating memory.
1565 *
1566 * We assume that the minimum image size should be proportional to
1567 *
1568 * [number of saveable pages] - [number of pages that can be freed in theory]
1569 *
1570 * where the second term is the sum of (1) reclaimable slab pages, (2) active
Geert Uytterhoeven4d434822014-03-11 11:23:41 +01001571 * and (3) inactive anonymous pages, (4) active and (5) inactive file pages,
Rafael J. Wysockief4aede2009-07-08 13:24:12 +02001572 * minus mapped file pages.
1573 */
1574static unsigned long minimum_image_size(unsigned long saveable)
1575{
1576 unsigned long size;
1577
1578 size = global_page_state(NR_SLAB_RECLAIMABLE)
1579 + global_page_state(NR_ACTIVE_ANON)
1580 + global_page_state(NR_INACTIVE_ANON)
1581 + global_page_state(NR_ACTIVE_FILE)
1582 + global_page_state(NR_INACTIVE_FILE)
1583 - global_page_state(NR_FILE_MAPPED);
1584
1585 return saveable <= size ? 0 : saveable - size;
1586}
1587
1588/**
Rafael J. Wysocki64a473c2009-07-08 13:24:05 +02001589 * hibernate_preallocate_memory - Preallocate memory for hibernation image
Rafael J. Wysocki4bb33432009-07-08 13:23:51 +02001590 *
1591 * To create a hibernation image it is necessary to make a copy of every page
1592 * frame in use. We also need a number of page frames to be free during
1593 * hibernation for allocations made while saving the image and for device
1594 * drivers, in case they need to allocate memory from their hibernation
Rafael J. Wysockiddeb6482011-05-15 11:38:48 +02001595 * callbacks (these two numbers are given by PAGES_FOR_IO (which is a rough
1596 * estimate) and reserverd_size divided by PAGE_SIZE (which is tunable through
1597 * /sys/power/reserved_size, respectively). To make this happen, we compute the
1598 * total number of available page frames and allocate at least
Rafael J. Wysocki4bb33432009-07-08 13:23:51 +02001599 *
Rafael J. Wysockiddeb6482011-05-15 11:38:48 +02001600 * ([page frames total] + PAGES_FOR_IO + [metadata pages]) / 2
1601 * + 2 * DIV_ROUND_UP(reserved_size, PAGE_SIZE)
Rafael J. Wysocki4bb33432009-07-08 13:23:51 +02001602 *
1603 * of them, which corresponds to the maximum size of a hibernation image.
1604 *
1605 * If image_size is set below the number following from the above formula,
1606 * the preallocation of memory is continued until the total number of saveable
Rafael J. Wysockief4aede2009-07-08 13:24:12 +02001607 * pages in the system is below the requested image size or the minimum
1608 * acceptable image size returned by minimum_image_size(), whichever is greater.
Rafael J. Wysocki4bb33432009-07-08 13:23:51 +02001609 */
Rafael J. Wysocki64a473c2009-07-08 13:24:05 +02001610int hibernate_preallocate_memory(void)
Rafael J. Wysockife419532009-06-11 23:11:17 +02001611{
Rafael J. Wysockife419532009-06-11 23:11:17 +02001612 struct zone *zone;
Rafael J. Wysocki4bb33432009-07-08 13:23:51 +02001613 unsigned long saveable, size, max_size, count, highmem, pages = 0;
Rafael J. Wysocki67150452010-09-11 20:58:27 +02001614 unsigned long alloc, save_highmem, pages_highmem, avail_normal;
Rafael J. Wysockife419532009-06-11 23:11:17 +02001615 struct timeval start, stop;
Rafael J. Wysocki64a473c2009-07-08 13:24:05 +02001616 int error;
Rafael J. Wysockife419532009-06-11 23:11:17 +02001617
Rafael J. Wysocki64a473c2009-07-08 13:24:05 +02001618 printk(KERN_INFO "PM: Preallocating image memory... ");
Rafael J. Wysockife419532009-06-11 23:11:17 +02001619 do_gettimeofday(&start);
Rafael J. Wysockife419532009-06-11 23:11:17 +02001620
Rafael J. Wysocki64a473c2009-07-08 13:24:05 +02001621 error = memory_bm_create(&orig_bm, GFP_IMAGE, PG_ANY);
1622 if (error)
1623 goto err_out;
1624
1625 error = memory_bm_create(&copy_bm, GFP_IMAGE, PG_ANY);
1626 if (error)
1627 goto err_out;
1628
1629 alloc_normal = 0;
1630 alloc_highmem = 0;
1631
Rafael J. Wysocki4bb33432009-07-08 13:23:51 +02001632 /* Count the number of saveable data pages. */
Rafael J. Wysocki64a473c2009-07-08 13:24:05 +02001633 save_highmem = count_highmem_pages();
Rafael J. Wysocki4bb33432009-07-08 13:23:51 +02001634 saveable = count_data_pages();
Rafael J. Wysockife419532009-06-11 23:11:17 +02001635
Rafael J. Wysocki4bb33432009-07-08 13:23:51 +02001636 /*
1637 * Compute the total number of page frames we can use (count) and the
1638 * number of pages needed for image metadata (size).
1639 */
1640 count = saveable;
Rafael J. Wysocki64a473c2009-07-08 13:24:05 +02001641 saveable += save_highmem;
1642 highmem = save_highmem;
Rafael J. Wysocki4bb33432009-07-08 13:23:51 +02001643 size = 0;
1644 for_each_populated_zone(zone) {
1645 size += snapshot_additional_pages(zone);
1646 if (is_highmem(zone))
1647 highmem += zone_page_state(zone, NR_FREE_PAGES);
1648 else
1649 count += zone_page_state(zone, NR_FREE_PAGES);
1650 }
Rafael J. Wysocki67150452010-09-11 20:58:27 +02001651 avail_normal = count;
Rafael J. Wysocki4bb33432009-07-08 13:23:51 +02001652 count += highmem;
1653 count -= totalreserve_pages;
Rafael J. Wysockife419532009-06-11 23:11:17 +02001654
Martin Schwidefsky85055dd2011-08-17 20:42:24 +02001655 /* Add number of pages required for page keys (s390 only). */
1656 size += page_key_additional_pages(saveable);
1657
Rafael J. Wysocki4bb33432009-07-08 13:23:51 +02001658 /* Compute the maximum number of saveable pages to leave in memory. */
Rafael J. Wysockiddeb6482011-05-15 11:38:48 +02001659 max_size = (count - (size + PAGES_FOR_IO)) / 2
1660 - 2 * DIV_ROUND_UP(reserved_size, PAGE_SIZE);
Rafael J. Wysocki266f1a22010-09-20 19:44:38 +02001661 /* Compute the desired number of image pages specified by image_size. */
Rafael J. Wysocki4bb33432009-07-08 13:23:51 +02001662 size = DIV_ROUND_UP(image_size, PAGE_SIZE);
1663 if (size > max_size)
1664 size = max_size;
1665 /*
Rafael J. Wysocki266f1a22010-09-20 19:44:38 +02001666 * If the desired number of image pages is at least as large as the
1667 * current number of saveable pages in memory, allocate page frames for
1668 * the image and we're done.
Rafael J. Wysocki4bb33432009-07-08 13:23:51 +02001669 */
Rafael J. Wysocki64a473c2009-07-08 13:24:05 +02001670 if (size >= saveable) {
1671 pages = preallocate_image_highmem(save_highmem);
Rafael J. Wysocki67150452010-09-11 20:58:27 +02001672 pages += preallocate_image_memory(saveable - pages, avail_normal);
Rafael J. Wysocki4bb33432009-07-08 13:23:51 +02001673 goto out;
Rafael J. Wysocki64a473c2009-07-08 13:24:05 +02001674 }
Rafael J. Wysocki4bb33432009-07-08 13:23:51 +02001675
Rafael J. Wysockief4aede2009-07-08 13:24:12 +02001676 /* Estimate the minimum size of the image. */
1677 pages = minimum_image_size(saveable);
Rafael J. Wysocki67150452010-09-11 20:58:27 +02001678 /*
1679 * To avoid excessive pressure on the normal zone, leave room in it to
1680 * accommodate an image of the minimum size (unless it's already too
1681 * small, in which case don't preallocate pages from it at all).
1682 */
1683 if (avail_normal > pages)
1684 avail_normal -= pages;
1685 else
1686 avail_normal = 0;
Rafael J. Wysockief4aede2009-07-08 13:24:12 +02001687 if (size < pages)
1688 size = min_t(unsigned long, pages, max_size);
1689
Rafael J. Wysocki4bb33432009-07-08 13:23:51 +02001690 /*
1691 * Let the memory management subsystem know that we're going to need a
1692 * large number of page frames to allocate and make it free some memory.
1693 * NOTE: If this is not done, performance will be hurt badly in some
1694 * test cases.
1695 */
1696 shrink_all_memory(saveable - size);
1697
1698 /*
1699 * The number of saveable pages in memory was too high, so apply some
1700 * pressure to decrease it. First, make room for the largest possible
1701 * image and fail if that doesn't work. Next, try to decrease the size
Rafael J. Wysockief4aede2009-07-08 13:24:12 +02001702 * of the image as much as indicated by 'size' using allocations from
1703 * highmem and non-highmem zones separately.
Rafael J. Wysocki4bb33432009-07-08 13:23:51 +02001704 */
1705 pages_highmem = preallocate_image_highmem(highmem / 2);
Aaron Lufd432b92013-11-06 08:41:31 +08001706 alloc = count - max_size;
1707 if (alloc > pages_highmem)
1708 alloc -= pages_highmem;
1709 else
1710 alloc = 0;
Rafael J. Wysocki67150452010-09-11 20:58:27 +02001711 pages = preallocate_image_memory(alloc, avail_normal);
1712 if (pages < alloc) {
1713 /* We have exhausted non-highmem pages, try highmem. */
1714 alloc -= pages;
1715 pages += pages_highmem;
1716 pages_highmem = preallocate_image_highmem(alloc);
1717 if (pages_highmem < alloc)
1718 goto err_out;
1719 pages += pages_highmem;
1720 /*
1721 * size is the desired number of saveable pages to leave in
1722 * memory, so try to preallocate (all memory - size) pages.
1723 */
1724 alloc = (count - pages) - size;
1725 pages += preallocate_image_highmem(alloc);
1726 } else {
1727 /*
1728 * There are approximately max_size saveable pages at this point
1729 * and we want to reduce this number down to size.
1730 */
1731 alloc = max_size - size;
1732 size = preallocate_highmem_fraction(alloc, highmem, count);
1733 pages_highmem += size;
1734 alloc -= size;
1735 size = preallocate_image_memory(alloc, avail_normal);
1736 pages_highmem += preallocate_image_highmem(alloc - size);
1737 pages += pages_highmem + size;
1738 }
Rafael J. Wysocki4bb33432009-07-08 13:23:51 +02001739
Rafael J. Wysocki64a473c2009-07-08 13:24:05 +02001740 /*
1741 * We only need as many page frames for the image as there are saveable
1742 * pages in memory, but we have allocated more. Release the excessive
1743 * ones now.
1744 */
1745 free_unnecessary_pages();
Rafael J. Wysocki4bb33432009-07-08 13:23:51 +02001746
1747 out:
Rafael J. Wysockife419532009-06-11 23:11:17 +02001748 do_gettimeofday(&stop);
Rafael J. Wysocki64a473c2009-07-08 13:24:05 +02001749 printk(KERN_CONT "done (allocated %lu pages)\n", pages);
1750 swsusp_show_speed(&start, &stop, pages, "Allocated");
Rafael J. Wysockife419532009-06-11 23:11:17 +02001751
1752 return 0;
Rafael J. Wysocki64a473c2009-07-08 13:24:05 +02001753
1754 err_out:
1755 printk(KERN_CONT "\n");
1756 swsusp_free();
1757 return -ENOMEM;
Rafael J. Wysockife419532009-06-11 23:11:17 +02001758}
1759
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001760#ifdef CONFIG_HIGHMEM
1761/**
1762 * count_pages_for_highmem - compute the number of non-highmem pages
1763 * that will be necessary for creating copies of highmem pages.
1764 */
1765
1766static unsigned int count_pages_for_highmem(unsigned int nr_highmem)
1767{
Rafael J. Wysocki64a473c2009-07-08 13:24:05 +02001768 unsigned int free_highmem = count_free_highmem_pages() + alloc_highmem;
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001769
1770 if (free_highmem >= nr_highmem)
1771 nr_highmem = 0;
1772 else
1773 nr_highmem -= free_highmem;
1774
1775 return nr_highmem;
1776}
1777#else
1778static unsigned int
1779count_pages_for_highmem(unsigned int nr_highmem) { return 0; }
1780#endif /* CONFIG_HIGHMEM */
Rafael J. Wysocki25761b62005-10-30 14:59:56 -08001781
1782/**
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001783 * enough_free_mem - Make sure we have enough free memory for the
1784 * snapshot image.
Rafael J. Wysocki25761b62005-10-30 14:59:56 -08001785 */
1786
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001787static int enough_free_mem(unsigned int nr_pages, unsigned int nr_highmem)
Rafael J. Wysocki25761b62005-10-30 14:59:56 -08001788{
Rafael J. Wysockie5e2fa72006-01-06 00:14:20 -08001789 struct zone *zone;
Rafael J. Wysocki64a473c2009-07-08 13:24:05 +02001790 unsigned int free = alloc_normal;
Rafael J. Wysockie5e2fa72006-01-06 00:14:20 -08001791
Gerald Schaefer98e73dc2009-07-22 00:36:56 +02001792 for_each_populated_zone(zone)
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001793 if (!is_highmem(zone))
Christoph Lameterd23ad422007-02-10 01:43:02 -08001794 free += zone_page_state(zone, NR_FREE_PAGES);
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001795
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001796 nr_pages += count_pages_for_highmem(nr_highmem);
Rafael J. Wysocki64a473c2009-07-08 13:24:05 +02001797 pr_debug("PM: Normal pages needed: %u + %u, available pages: %u\n",
1798 nr_pages, PAGES_FOR_IO, free);
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001799
Rafael J. Wysocki64a473c2009-07-08 13:24:05 +02001800 return free > nr_pages + PAGES_FOR_IO;
Rafael J. Wysocki25761b62005-10-30 14:59:56 -08001801}
1802
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001803#ifdef CONFIG_HIGHMEM
1804/**
1805 * get_highmem_buffer - if there are some highmem pages in the suspend
1806 * image, we may need the buffer to copy them and/or load their data.
1807 */
1808
1809static inline int get_highmem_buffer(int safe_needed)
1810{
1811 buffer = get_image_page(GFP_ATOMIC | __GFP_COLD, safe_needed);
1812 return buffer ? 0 : -ENOMEM;
1813}
1814
1815/**
1816 * alloc_highmem_image_pages - allocate some highmem pages for the image.
1817 * Try to allocate as many pages as needed, but if the number of free
1818 * highmem pages is lesser than that, allocate them all.
1819 */
1820
1821static inline unsigned int
Rafael J. Wysocki64a473c2009-07-08 13:24:05 +02001822alloc_highmem_pages(struct memory_bitmap *bm, unsigned int nr_highmem)
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001823{
1824 unsigned int to_alloc = count_free_highmem_pages();
1825
1826 if (to_alloc > nr_highmem)
1827 to_alloc = nr_highmem;
1828
1829 nr_highmem -= to_alloc;
1830 while (to_alloc-- > 0) {
1831 struct page *page;
1832
1833 page = alloc_image_page(__GFP_HIGHMEM);
1834 memory_bm_set_bit(bm, page_to_pfn(page));
1835 }
1836 return nr_highmem;
1837}
1838#else
1839static inline int get_highmem_buffer(int safe_needed) { return 0; }
1840
1841static inline unsigned int
Rafael J. Wysocki64a473c2009-07-08 13:24:05 +02001842alloc_highmem_pages(struct memory_bitmap *bm, unsigned int n) { return 0; }
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001843#endif /* CONFIG_HIGHMEM */
1844
1845/**
1846 * swsusp_alloc - allocate memory for the suspend image
1847 *
1848 * We first try to allocate as many highmem pages as there are
1849 * saveable highmem pages in the system. If that fails, we allocate
1850 * non-highmem pages for the copies of the remaining highmem ones.
1851 *
1852 * In this approach it is likely that the copies of highmem pages will
1853 * also be located in the high memory, because of the way in which
1854 * copy_data_pages() works.
1855 */
1856
Rafael J. Wysockib788db72006-09-25 23:32:54 -07001857static int
1858swsusp_alloc(struct memory_bitmap *orig_bm, struct memory_bitmap *copy_bm,
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001859 unsigned int nr_pages, unsigned int nr_highmem)
Rafael J. Wysocki054bd4c2005-11-08 21:34:39 -08001860{
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001861 if (nr_highmem > 0) {
Stanislaw Gruszka2e725a02011-02-12 21:06:51 +01001862 if (get_highmem_buffer(PG_ANY))
Rafael J. Wysocki64a473c2009-07-08 13:24:05 +02001863 goto err_out;
1864 if (nr_highmem > alloc_highmem) {
1865 nr_highmem -= alloc_highmem;
1866 nr_pages += alloc_highmem_pages(copy_bm, nr_highmem);
1867 }
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001868 }
Rafael J. Wysocki64a473c2009-07-08 13:24:05 +02001869 if (nr_pages > alloc_normal) {
1870 nr_pages -= alloc_normal;
1871 while (nr_pages-- > 0) {
1872 struct page *page;
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001873
Rafael J. Wysocki64a473c2009-07-08 13:24:05 +02001874 page = alloc_image_page(GFP_ATOMIC | __GFP_COLD);
1875 if (!page)
1876 goto err_out;
1877 memory_bm_set_bit(copy_bm, page_to_pfn(page));
1878 }
Rafael J. Wysocki054bd4c2005-11-08 21:34:39 -08001879 }
Rafael J. Wysocki64a473c2009-07-08 13:24:05 +02001880
Rafael J. Wysocki054bd4c2005-11-08 21:34:39 -08001881 return 0;
Rafael J. Wysockib788db72006-09-25 23:32:54 -07001882
Rafael J. Wysocki64a473c2009-07-08 13:24:05 +02001883 err_out:
Rafael J. Wysockib788db72006-09-25 23:32:54 -07001884 swsusp_free();
Stanislaw Gruszka2e725a02011-02-12 21:06:51 +01001885 return -ENOMEM;
Rafael J. Wysocki054bd4c2005-11-08 21:34:39 -08001886}
Rafael J. Wysocki25761b62005-10-30 14:59:56 -08001887
Andi Kleen722a9f92014-05-02 00:44:38 +02001888asmlinkage __visible int swsusp_save(void)
Rafael J. Wysocki25761b62005-10-30 14:59:56 -08001889{
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001890 unsigned int nr_pages, nr_highmem;
Rafael J. Wysocki25761b62005-10-30 14:59:56 -08001891
Frans Pop07c3bb52010-02-11 23:09:08 +01001892 printk(KERN_INFO "PM: Creating hibernation image:\n");
Rafael J. Wysocki25761b62005-10-30 14:59:56 -08001893
Christoph Lameter9f8f2172008-02-04 22:29:11 -08001894 drain_local_pages(NULL);
Rafael J. Wysockia0f49652005-10-30 14:59:57 -08001895 nr_pages = count_data_pages();
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001896 nr_highmem = count_highmem_pages();
Rafael J. Wysocki23976722007-12-08 02:09:43 +01001897 printk(KERN_INFO "PM: Need to copy %u pages\n", nr_pages + nr_highmem);
Rafael J. Wysocki25761b62005-10-30 14:59:56 -08001898
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001899 if (!enough_free_mem(nr_pages, nr_highmem)) {
Rafael J. Wysocki23976722007-12-08 02:09:43 +01001900 printk(KERN_ERR "PM: Not enough free memory\n");
Rafael J. Wysocki25761b62005-10-30 14:59:56 -08001901 return -ENOMEM;
1902 }
1903
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001904 if (swsusp_alloc(&orig_bm, &copy_bm, nr_pages, nr_highmem)) {
Rafael J. Wysocki23976722007-12-08 02:09:43 +01001905 printk(KERN_ERR "PM: Memory allocation failed\n");
Rafael J. Wysockia0f49652005-10-30 14:59:57 -08001906 return -ENOMEM;
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001907 }
Rafael J. Wysocki25761b62005-10-30 14:59:56 -08001908
1909 /* During allocating of suspend pagedir, new cold pages may appear.
1910 * Kill them.
1911 */
Christoph Lameter9f8f2172008-02-04 22:29:11 -08001912 drain_local_pages(NULL);
Rafael J. Wysockib788db72006-09-25 23:32:54 -07001913 copy_data_pages(&copy_bm, &orig_bm);
Rafael J. Wysocki25761b62005-10-30 14:59:56 -08001914
1915 /*
1916 * End of critical section. From now on, we can write to memory,
1917 * but we should not touch disk. This specially means we must _not_
1918 * touch swap space! Except we must write out our image of course.
1919 */
1920
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001921 nr_pages += nr_highmem;
Rafael J. Wysockia0f49652005-10-30 14:59:57 -08001922 nr_copy_pages = nr_pages;
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001923 nr_meta_pages = DIV_ROUND_UP(nr_pages * sizeof(long), PAGE_SIZE);
Rafael J. Wysockia0f49652005-10-30 14:59:57 -08001924
Rafael J. Wysocki23976722007-12-08 02:09:43 +01001925 printk(KERN_INFO "PM: Hibernation image created (%d pages copied)\n",
1926 nr_pages);
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001927
Rafael J. Wysocki25761b62005-10-30 14:59:56 -08001928 return 0;
1929}
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001930
Rafael J. Wysockid307c4a2007-10-18 03:04:52 -07001931#ifndef CONFIG_ARCH_HIBERNATION_HEADER
1932static int init_header_complete(struct swsusp_info *info)
1933{
1934 memcpy(&info->uts, init_utsname(), sizeof(struct new_utsname));
1935 info->version_code = LINUX_VERSION_CODE;
1936 return 0;
1937}
1938
1939static char *check_image_kernel(struct swsusp_info *info)
1940{
1941 if (info->version_code != LINUX_VERSION_CODE)
1942 return "kernel version";
1943 if (strcmp(info->uts.sysname,init_utsname()->sysname))
1944 return "system type";
1945 if (strcmp(info->uts.release,init_utsname()->release))
1946 return "kernel release";
1947 if (strcmp(info->uts.version,init_utsname()->version))
1948 return "version";
1949 if (strcmp(info->uts.machine,init_utsname()->machine))
1950 return "machine";
1951 return NULL;
1952}
1953#endif /* CONFIG_ARCH_HIBERNATION_HEADER */
1954
Rafael J. Wysockiaf508b32007-10-26 00:59:31 +02001955unsigned long snapshot_get_image_size(void)
1956{
1957 return nr_copy_pages + nr_meta_pages + 1;
1958}
1959
Rafael J. Wysockid307c4a2007-10-18 03:04:52 -07001960static int init_header(struct swsusp_info *info)
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001961{
1962 memset(info, 0, sizeof(struct swsusp_info));
Jiang Liu0ed5fd12013-07-03 15:03:43 -07001963 info->num_physpages = get_num_physpages();
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001964 info->image_pages = nr_copy_pages;
Rafael J. Wysockiaf508b32007-10-26 00:59:31 +02001965 info->pages = snapshot_get_image_size();
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -08001966 info->size = info->pages;
1967 info->size <<= PAGE_SHIFT;
Rafael J. Wysockid307c4a2007-10-18 03:04:52 -07001968 return init_header_complete(info);
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001969}
1970
1971/**
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001972 * pack_pfns - pfns corresponding to the set bits found in the bitmap @bm
1973 * are stored in the array @buf[] (1 page at a time)
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001974 */
1975
Rafael J. Wysockib788db72006-09-25 23:32:54 -07001976static inline void
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001977pack_pfns(unsigned long *buf, struct memory_bitmap *bm)
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001978{
1979 int j;
1980
Rafael J. Wysockib788db72006-09-25 23:32:54 -07001981 for (j = 0; j < PAGE_SIZE / sizeof(long); j++) {
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07001982 buf[j] = memory_bm_next_pfn(bm);
1983 if (unlikely(buf[j] == BM_END_OF_MAP))
Rafael J. Wysockib788db72006-09-25 23:32:54 -07001984 break;
Martin Schwidefsky85055dd2011-08-17 20:42:24 +02001985 /* Save page key for data page (s390 only). */
1986 page_key_read(buf + j);
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001987 }
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001988}
1989
1990/**
1991 * snapshot_read_next - used for reading the system memory snapshot.
1992 *
1993 * On the first call to it @handle should point to a zeroed
1994 * snapshot_handle structure. The structure gets updated and a pointer
1995 * to it should be passed to this function every next time.
1996 *
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08001997 * On success the function returns a positive number. Then, the caller
1998 * is allowed to read up to the returned number of bytes from the memory
Jiri Slabyd3c1b242010-05-01 23:52:02 +02001999 * location computed by the data_of() macro.
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08002000 *
2001 * The function returns 0 to indicate the end of data stream condition,
2002 * and a negative number is returned on error. In such cases the
2003 * structure pointed to by @handle is not updated and should not be used
2004 * any more.
2005 */
2006
Jiri Slabyd3c1b242010-05-01 23:52:02 +02002007int snapshot_read_next(struct snapshot_handle *handle)
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08002008{
Rafael J. Wysockifb13a282006-09-25 23:32:46 -07002009 if (handle->cur > nr_meta_pages + nr_copy_pages)
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08002010 return 0;
Rafael J. Wysockib788db72006-09-25 23:32:54 -07002011
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08002012 if (!buffer) {
2013 /* This makes the buffer be freed by swsusp_free() */
Rafael J. Wysocki83573762006-12-06 20:34:18 -08002014 buffer = get_image_page(GFP_ATOMIC, PG_ANY);
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08002015 if (!buffer)
2016 return -ENOMEM;
2017 }
Jiri Slabyd3c1b242010-05-01 23:52:02 +02002018 if (!handle->cur) {
Rafael J. Wysockid307c4a2007-10-18 03:04:52 -07002019 int error;
2020
2021 error = init_header((struct swsusp_info *)buffer);
2022 if (error)
2023 return error;
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08002024 handle->buffer = buffer;
Rafael J. Wysockib788db72006-09-25 23:32:54 -07002025 memory_bm_position_reset(&orig_bm);
2026 memory_bm_position_reset(&copy_bm);
Jiri Slabyd3c1b242010-05-01 23:52:02 +02002027 } else if (handle->cur <= nr_meta_pages) {
Jan Beulich3ecb01d2010-10-26 14:22:27 -07002028 clear_page(buffer);
Jiri Slabyd3c1b242010-05-01 23:52:02 +02002029 pack_pfns(buffer, &orig_bm);
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08002030 } else {
Jiri Slabyd3c1b242010-05-01 23:52:02 +02002031 struct page *page;
2032
2033 page = pfn_to_page(memory_bm_next_pfn(&copy_bm));
2034 if (PageHighMem(page)) {
2035 /* Highmem pages are copied to the buffer,
2036 * because we can't return with a kmapped
2037 * highmem page (we may not be called again).
2038 */
2039 void *kaddr;
2040
Cong Wang0de9a1e2011-11-25 23:14:38 +08002041 kaddr = kmap_atomic(page);
Jan Beulich3ecb01d2010-10-26 14:22:27 -07002042 copy_page(buffer, kaddr);
Cong Wang0de9a1e2011-11-25 23:14:38 +08002043 kunmap_atomic(kaddr);
Jiri Slabyd3c1b242010-05-01 23:52:02 +02002044 handle->buffer = buffer;
2045 } else {
2046 handle->buffer = page_address(page);
2047 }
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08002048 }
Jiri Slabyd3c1b242010-05-01 23:52:02 +02002049 handle->cur++;
2050 return PAGE_SIZE;
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08002051}
2052
2053/**
2054 * mark_unsafe_pages - mark the pages that cannot be used for storing
2055 * the image during resume, because they conflict with the pages that
2056 * had been used before suspend
2057 */
2058
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07002059static int mark_unsafe_pages(struct memory_bitmap *bm)
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08002060{
2061 struct zone *zone;
Rafael J. Wysockiae83c5ee2006-09-25 23:32:45 -07002062 unsigned long pfn, max_zone_pfn;
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08002063
2064 /* Clear page flags */
Gerald Schaefer98e73dc2009-07-22 00:36:56 +02002065 for_each_populated_zone(zone) {
Xishi Qiuc33bc312013-09-11 14:21:44 -07002066 max_zone_pfn = zone_end_pfn(zone);
Rafael J. Wysockiae83c5ee2006-09-25 23:32:45 -07002067 for (pfn = zone->zone_start_pfn; pfn < max_zone_pfn; pfn++)
2068 if (pfn_valid(pfn))
Rafael J. Wysocki7be98232007-05-06 14:50:42 -07002069 swsusp_unset_page_free(pfn_to_page(pfn));
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08002070 }
2071
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07002072 /* Mark pages that correspond to the "original" pfns as "unsafe" */
2073 memory_bm_position_reset(bm);
2074 do {
2075 pfn = memory_bm_next_pfn(bm);
2076 if (likely(pfn != BM_END_OF_MAP)) {
2077 if (likely(pfn_valid(pfn)))
Rafael J. Wysocki7be98232007-05-06 14:50:42 -07002078 swsusp_set_page_free(pfn_to_page(pfn));
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07002079 else
2080 return -EFAULT;
2081 }
2082 } while (pfn != BM_END_OF_MAP);
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08002083
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07002084 allocated_unsafe_pages = 0;
Rafael J. Wysocki968808b82006-06-23 02:04:48 -07002085
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08002086 return 0;
2087}
2088
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07002089static void
2090duplicate_memory_bitmap(struct memory_bitmap *dst, struct memory_bitmap *src)
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08002091{
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07002092 unsigned long pfn;
2093
2094 memory_bm_position_reset(src);
2095 pfn = memory_bm_next_pfn(src);
2096 while (pfn != BM_END_OF_MAP) {
2097 memory_bm_set_bit(dst, pfn);
2098 pfn = memory_bm_next_pfn(src);
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08002099 }
2100}
2101
Rafael J. Wysockid307c4a2007-10-18 03:04:52 -07002102static int check_header(struct swsusp_info *info)
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08002103{
Rafael J. Wysockid307c4a2007-10-18 03:04:52 -07002104 char *reason;
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08002105
Rafael J. Wysockid307c4a2007-10-18 03:04:52 -07002106 reason = check_image_kernel(info);
Jiang Liu0ed5fd12013-07-03 15:03:43 -07002107 if (!reason && info->num_physpages != get_num_physpages())
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08002108 reason = "memory size";
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08002109 if (reason) {
Rafael J. Wysocki23976722007-12-08 02:09:43 +01002110 printk(KERN_ERR "PM: Image mismatch: %s\n", reason);
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08002111 return -EPERM;
2112 }
2113 return 0;
2114}
2115
2116/**
2117 * load header - check the image header and copy data from it
2118 */
2119
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07002120static int
2121load_header(struct swsusp_info *info)
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08002122{
2123 int error;
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08002124
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07002125 restore_pblist = NULL;
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08002126 error = check_header(info);
2127 if (!error) {
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08002128 nr_copy_pages = info->image_pages;
2129 nr_meta_pages = info->pages - info->image_pages - 1;
2130 }
2131 return error;
2132}
2133
2134/**
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07002135 * unpack_orig_pfns - for each element of @buf[] (1 page at a time) set
2136 * the corresponding bit in the memory bitmap @bm
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08002137 */
Rafael J. Wysocki69643272008-11-11 21:32:44 +01002138static int unpack_orig_pfns(unsigned long *buf, struct memory_bitmap *bm)
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08002139{
2140 int j;
2141
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07002142 for (j = 0; j < PAGE_SIZE / sizeof(long); j++) {
2143 if (unlikely(buf[j] == BM_END_OF_MAP))
2144 break;
2145
Martin Schwidefsky85055dd2011-08-17 20:42:24 +02002146 /* Extract and buffer page key for data page (s390 only). */
2147 page_key_memorize(buf + j);
2148
Rafael J. Wysocki69643272008-11-11 21:32:44 +01002149 if (memory_bm_pfn_present(bm, buf[j]))
2150 memory_bm_set_bit(bm, buf[j]);
2151 else
2152 return -EFAULT;
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08002153 }
Rafael J. Wysocki69643272008-11-11 21:32:44 +01002154
2155 return 0;
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08002156}
2157
Rafael J. Wysocki83573762006-12-06 20:34:18 -08002158/* List of "safe" pages that may be used to store data loaded from the suspend
2159 * image
2160 */
2161static struct linked_page *safe_pages_list;
2162
2163#ifdef CONFIG_HIGHMEM
2164/* struct highmem_pbe is used for creating the list of highmem pages that
2165 * should be restored atomically during the resume from disk, because the page
2166 * frames they have occupied before the suspend are in use.
2167 */
2168struct highmem_pbe {
2169 struct page *copy_page; /* data is here now */
2170 struct page *orig_page; /* data was here before the suspend */
2171 struct highmem_pbe *next;
2172};
2173
2174/* List of highmem PBEs needed for restoring the highmem pages that were
2175 * allocated before the suspend and included in the suspend image, but have
2176 * also been allocated by the "resume" kernel, so their contents cannot be
2177 * written directly to their "original" page frames.
2178 */
2179static struct highmem_pbe *highmem_pblist;
2180
2181/**
2182 * count_highmem_image_pages - compute the number of highmem pages in the
2183 * suspend image. The bits in the memory bitmap @bm that correspond to the
2184 * image pages are assumed to be set.
2185 */
2186
2187static unsigned int count_highmem_image_pages(struct memory_bitmap *bm)
2188{
2189 unsigned long pfn;
2190 unsigned int cnt = 0;
2191
2192 memory_bm_position_reset(bm);
2193 pfn = memory_bm_next_pfn(bm);
2194 while (pfn != BM_END_OF_MAP) {
2195 if (PageHighMem(pfn_to_page(pfn)))
2196 cnt++;
2197
2198 pfn = memory_bm_next_pfn(bm);
2199 }
2200 return cnt;
2201}
2202
2203/**
2204 * prepare_highmem_image - try to allocate as many highmem pages as
2205 * there are highmem image pages (@nr_highmem_p points to the variable
2206 * containing the number of highmem image pages). The pages that are
2207 * "safe" (ie. will not be overwritten when the suspend image is
2208 * restored) have the corresponding bits set in @bm (it must be
2209 * unitialized).
2210 *
2211 * NOTE: This function should not be called if there are no highmem
2212 * image pages.
2213 */
2214
2215static unsigned int safe_highmem_pages;
2216
2217static struct memory_bitmap *safe_highmem_bm;
2218
2219static int
2220prepare_highmem_image(struct memory_bitmap *bm, unsigned int *nr_highmem_p)
2221{
2222 unsigned int to_alloc;
2223
2224 if (memory_bm_create(bm, GFP_ATOMIC, PG_SAFE))
2225 return -ENOMEM;
2226
2227 if (get_highmem_buffer(PG_SAFE))
2228 return -ENOMEM;
2229
2230 to_alloc = count_free_highmem_pages();
2231 if (to_alloc > *nr_highmem_p)
2232 to_alloc = *nr_highmem_p;
2233 else
2234 *nr_highmem_p = to_alloc;
2235
2236 safe_highmem_pages = 0;
2237 while (to_alloc-- > 0) {
2238 struct page *page;
2239
2240 page = alloc_page(__GFP_HIGHMEM);
Rafael J. Wysocki7be98232007-05-06 14:50:42 -07002241 if (!swsusp_page_is_free(page)) {
Rafael J. Wysocki83573762006-12-06 20:34:18 -08002242 /* The page is "safe", set its bit the bitmap */
2243 memory_bm_set_bit(bm, page_to_pfn(page));
2244 safe_highmem_pages++;
2245 }
2246 /* Mark the page as allocated */
Rafael J. Wysocki7be98232007-05-06 14:50:42 -07002247 swsusp_set_page_forbidden(page);
2248 swsusp_set_page_free(page);
Rafael J. Wysocki83573762006-12-06 20:34:18 -08002249 }
2250 memory_bm_position_reset(bm);
2251 safe_highmem_bm = bm;
2252 return 0;
2253}
2254
2255/**
2256 * get_highmem_page_buffer - for given highmem image page find the buffer
2257 * that suspend_write_next() should set for its caller to write to.
2258 *
2259 * If the page is to be saved to its "original" page frame or a copy of
2260 * the page is to be made in the highmem, @buffer is returned. Otherwise,
2261 * the copy of the page is to be made in normal memory, so the address of
2262 * the copy is returned.
2263 *
2264 * If @buffer is returned, the caller of suspend_write_next() will write
2265 * the page's contents to @buffer, so they will have to be copied to the
2266 * right location on the next call to suspend_write_next() and it is done
2267 * with the help of copy_last_highmem_page(). For this purpose, if
2268 * @buffer is returned, @last_highmem page is set to the page to which
2269 * the data will have to be copied from @buffer.
2270 */
2271
2272static struct page *last_highmem_page;
2273
2274static void *
2275get_highmem_page_buffer(struct page *page, struct chain_allocator *ca)
2276{
2277 struct highmem_pbe *pbe;
2278 void *kaddr;
2279
Rafael J. Wysocki7be98232007-05-06 14:50:42 -07002280 if (swsusp_page_is_forbidden(page) && swsusp_page_is_free(page)) {
Rafael J. Wysocki83573762006-12-06 20:34:18 -08002281 /* We have allocated the "original" page frame and we can
2282 * use it directly to store the loaded page.
2283 */
2284 last_highmem_page = page;
2285 return buffer;
2286 }
2287 /* The "original" page frame has not been allocated and we have to
2288 * use a "safe" page frame to store the loaded page.
2289 */
2290 pbe = chain_alloc(ca, sizeof(struct highmem_pbe));
2291 if (!pbe) {
2292 swsusp_free();
Rafael J. Wysocki69643272008-11-11 21:32:44 +01002293 return ERR_PTR(-ENOMEM);
Rafael J. Wysocki83573762006-12-06 20:34:18 -08002294 }
2295 pbe->orig_page = page;
2296 if (safe_highmem_pages > 0) {
2297 struct page *tmp;
2298
2299 /* Copy of the page will be stored in high memory */
2300 kaddr = buffer;
2301 tmp = pfn_to_page(memory_bm_next_pfn(safe_highmem_bm));
2302 safe_highmem_pages--;
2303 last_highmem_page = tmp;
2304 pbe->copy_page = tmp;
2305 } else {
2306 /* Copy of the page will be stored in normal memory */
2307 kaddr = safe_pages_list;
2308 safe_pages_list = safe_pages_list->next;
2309 pbe->copy_page = virt_to_page(kaddr);
2310 }
2311 pbe->next = highmem_pblist;
2312 highmem_pblist = pbe;
2313 return kaddr;
2314}
2315
2316/**
2317 * copy_last_highmem_page - copy the contents of a highmem image from
2318 * @buffer, where the caller of snapshot_write_next() has place them,
2319 * to the right location represented by @last_highmem_page .
2320 */
2321
2322static void copy_last_highmem_page(void)
2323{
2324 if (last_highmem_page) {
2325 void *dst;
2326
Cong Wang0de9a1e2011-11-25 23:14:38 +08002327 dst = kmap_atomic(last_highmem_page);
Jan Beulich3ecb01d2010-10-26 14:22:27 -07002328 copy_page(dst, buffer);
Cong Wang0de9a1e2011-11-25 23:14:38 +08002329 kunmap_atomic(dst);
Rafael J. Wysocki83573762006-12-06 20:34:18 -08002330 last_highmem_page = NULL;
2331 }
2332}
2333
2334static inline int last_highmem_page_copied(void)
2335{
2336 return !last_highmem_page;
2337}
2338
2339static inline void free_highmem_data(void)
2340{
2341 if (safe_highmem_bm)
2342 memory_bm_free(safe_highmem_bm, PG_UNSAFE_CLEAR);
2343
2344 if (buffer)
2345 free_image_page(buffer, PG_UNSAFE_CLEAR);
2346}
2347#else
2348static inline int get_safe_write_buffer(void) { return 0; }
2349
2350static unsigned int
2351count_highmem_image_pages(struct memory_bitmap *bm) { return 0; }
2352
2353static inline int
2354prepare_highmem_image(struct memory_bitmap *bm, unsigned int *nr_highmem_p)
2355{
2356 return 0;
2357}
2358
2359static inline void *
2360get_highmem_page_buffer(struct page *page, struct chain_allocator *ca)
2361{
Rafael J. Wysocki69643272008-11-11 21:32:44 +01002362 return ERR_PTR(-EINVAL);
Rafael J. Wysocki83573762006-12-06 20:34:18 -08002363}
2364
2365static inline void copy_last_highmem_page(void) {}
2366static inline int last_highmem_page_copied(void) { return 1; }
2367static inline void free_highmem_data(void) {}
2368#endif /* CONFIG_HIGHMEM */
2369
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08002370/**
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07002371 * prepare_image - use the memory bitmap @bm to mark the pages that will
2372 * be overwritten in the process of restoring the system memory state
2373 * from the suspend image ("unsafe" pages) and allocate memory for the
2374 * image.
Rafael J. Wysocki968808b82006-06-23 02:04:48 -07002375 *
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07002376 * The idea is to allocate a new memory bitmap first and then allocate
2377 * as many pages as needed for the image data, but not to assign these
2378 * pages to specific tasks initially. Instead, we just mark them as
Rafael J. Wysocki83573762006-12-06 20:34:18 -08002379 * allocated and create a lists of "safe" pages that will be used
2380 * later. On systems with high memory a list of "safe" highmem pages is
2381 * also created.
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08002382 */
2383
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07002384#define PBES_PER_LINKED_PAGE (LINKED_PAGE_DATA_SIZE / sizeof(struct pbe))
Rafael J. Wysocki968808b82006-06-23 02:04:48 -07002385
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07002386static int
2387prepare_image(struct memory_bitmap *new_bm, struct memory_bitmap *bm)
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08002388{
Rafael J. Wysocki83573762006-12-06 20:34:18 -08002389 unsigned int nr_pages, nr_highmem;
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07002390 struct linked_page *sp_list, *lp;
2391 int error;
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08002392
Rafael J. Wysocki83573762006-12-06 20:34:18 -08002393 /* If there is no highmem, the buffer will not be necessary */
2394 free_image_page(buffer, PG_UNSAFE_CLEAR);
2395 buffer = NULL;
2396
2397 nr_highmem = count_highmem_image_pages(bm);
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07002398 error = mark_unsafe_pages(bm);
2399 if (error)
2400 goto Free;
2401
2402 error = memory_bm_create(new_bm, GFP_ATOMIC, PG_SAFE);
2403 if (error)
2404 goto Free;
2405
2406 duplicate_memory_bitmap(new_bm, bm);
2407 memory_bm_free(bm, PG_UNSAFE_KEEP);
Rafael J. Wysocki83573762006-12-06 20:34:18 -08002408 if (nr_highmem > 0) {
2409 error = prepare_highmem_image(bm, &nr_highmem);
2410 if (error)
2411 goto Free;
2412 }
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07002413 /* Reserve some safe pages for potential later use.
2414 *
2415 * NOTE: This way we make sure there will be enough safe pages for the
2416 * chain_alloc() in get_buffer(). It is a bit wasteful, but
2417 * nr_copy_pages cannot be greater than 50% of the memory anyway.
2418 */
2419 sp_list = NULL;
2420 /* nr_copy_pages cannot be lesser than allocated_unsafe_pages */
Rafael J. Wysocki83573762006-12-06 20:34:18 -08002421 nr_pages = nr_copy_pages - nr_highmem - allocated_unsafe_pages;
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07002422 nr_pages = DIV_ROUND_UP(nr_pages, PBES_PER_LINKED_PAGE);
2423 while (nr_pages > 0) {
Rafael J. Wysocki83573762006-12-06 20:34:18 -08002424 lp = get_image_page(GFP_ATOMIC, PG_SAFE);
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07002425 if (!lp) {
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08002426 error = -ENOMEM;
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07002427 goto Free;
Rafael J. Wysocki968808b82006-06-23 02:04:48 -07002428 }
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07002429 lp->next = sp_list;
2430 sp_list = lp;
2431 nr_pages--;
Rafael J. Wysocki968808b82006-06-23 02:04:48 -07002432 }
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07002433 /* Preallocate memory for the image */
2434 safe_pages_list = NULL;
Rafael J. Wysocki83573762006-12-06 20:34:18 -08002435 nr_pages = nr_copy_pages - nr_highmem - allocated_unsafe_pages;
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07002436 while (nr_pages > 0) {
2437 lp = (struct linked_page *)get_zeroed_page(GFP_ATOMIC);
2438 if (!lp) {
2439 error = -ENOMEM;
2440 goto Free;
2441 }
Rafael J. Wysocki7be98232007-05-06 14:50:42 -07002442 if (!swsusp_page_is_free(virt_to_page(lp))) {
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07002443 /* The page is "safe", add it to the list */
2444 lp->next = safe_pages_list;
2445 safe_pages_list = lp;
2446 }
2447 /* Mark the page as allocated */
Rafael J. Wysocki7be98232007-05-06 14:50:42 -07002448 swsusp_set_page_forbidden(virt_to_page(lp));
2449 swsusp_set_page_free(virt_to_page(lp));
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07002450 nr_pages--;
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08002451 }
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07002452 /* Free the reserved safe pages so that chain_alloc() can use them */
2453 while (sp_list) {
2454 lp = sp_list->next;
2455 free_image_page(sp_list, PG_UNSAFE_CLEAR);
2456 sp_list = lp;
2457 }
2458 return 0;
2459
Rafael J. Wysocki59a493352006-12-06 20:34:44 -08002460 Free:
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07002461 swsusp_free();
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08002462 return error;
2463}
2464
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07002465/**
2466 * get_buffer - compute the address that snapshot_write_next() should
2467 * set for its caller to write to.
2468 */
Rafael J. Wysocki968808b82006-06-23 02:04:48 -07002469
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07002470static void *get_buffer(struct memory_bitmap *bm, struct chain_allocator *ca)
2471{
2472 struct pbe *pbe;
Rafael J. Wysocki69643272008-11-11 21:32:44 +01002473 struct page *page;
2474 unsigned long pfn = memory_bm_next_pfn(bm);
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07002475
Rafael J. Wysocki69643272008-11-11 21:32:44 +01002476 if (pfn == BM_END_OF_MAP)
2477 return ERR_PTR(-EFAULT);
2478
2479 page = pfn_to_page(pfn);
Rafael J. Wysocki83573762006-12-06 20:34:18 -08002480 if (PageHighMem(page))
2481 return get_highmem_page_buffer(page, ca);
2482
Rafael J. Wysocki7be98232007-05-06 14:50:42 -07002483 if (swsusp_page_is_forbidden(page) && swsusp_page_is_free(page))
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07002484 /* We have allocated the "original" page frame and we can
2485 * use it directly to store the loaded page.
Rafael J. Wysocki968808b82006-06-23 02:04:48 -07002486 */
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07002487 return page_address(page);
2488
2489 /* The "original" page frame has not been allocated and we have to
2490 * use a "safe" page frame to store the loaded page.
Rafael J. Wysocki968808b82006-06-23 02:04:48 -07002491 */
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07002492 pbe = chain_alloc(ca, sizeof(struct pbe));
2493 if (!pbe) {
2494 swsusp_free();
Rafael J. Wysocki69643272008-11-11 21:32:44 +01002495 return ERR_PTR(-ENOMEM);
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07002496 }
Rafael J. Wysocki83573762006-12-06 20:34:18 -08002497 pbe->orig_address = page_address(page);
2498 pbe->address = safe_pages_list;
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07002499 safe_pages_list = safe_pages_list->next;
2500 pbe->next = restore_pblist;
2501 restore_pblist = pbe;
Rafael J. Wysocki83573762006-12-06 20:34:18 -08002502 return pbe->address;
Rafael J. Wysocki968808b82006-06-23 02:04:48 -07002503}
2504
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08002505/**
2506 * snapshot_write_next - used for writing the system memory snapshot.
2507 *
2508 * On the first call to it @handle should point to a zeroed
2509 * snapshot_handle structure. The structure gets updated and a pointer
2510 * to it should be passed to this function every next time.
2511 *
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08002512 * On success the function returns a positive number. Then, the caller
2513 * is allowed to write up to the returned number of bytes to the memory
Jiri Slabyd3c1b242010-05-01 23:52:02 +02002514 * location computed by the data_of() macro.
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08002515 *
2516 * The function returns 0 to indicate the "end of file" condition,
2517 * and a negative number is returned on error. In such cases the
2518 * structure pointed to by @handle is not updated and should not be used
2519 * any more.
2520 */
2521
Jiri Slabyd3c1b242010-05-01 23:52:02 +02002522int snapshot_write_next(struct snapshot_handle *handle)
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08002523{
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07002524 static struct chain_allocator ca;
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08002525 int error = 0;
2526
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07002527 /* Check if we have already loaded the entire image */
Jiri Slabyd3c1b242010-05-01 23:52:02 +02002528 if (handle->cur > 1 && handle->cur > nr_meta_pages + nr_copy_pages)
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08002529 return 0;
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07002530
Jiri Slabyd3c1b242010-05-01 23:52:02 +02002531 handle->sync_read = 1;
2532
2533 if (!handle->cur) {
Rafael J. Wysocki83573762006-12-06 20:34:18 -08002534 if (!buffer)
2535 /* This makes the buffer be freed by swsusp_free() */
2536 buffer = get_image_page(GFP_ATOMIC, PG_ANY);
2537
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08002538 if (!buffer)
2539 return -ENOMEM;
Rafael J. Wysocki83573762006-12-06 20:34:18 -08002540
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08002541 handle->buffer = buffer;
Jiri Slabyd3c1b242010-05-01 23:52:02 +02002542 } else if (handle->cur == 1) {
2543 error = load_header(buffer);
2544 if (error)
2545 return error;
2546
2547 error = memory_bm_create(&copy_bm, GFP_ATOMIC, PG_ANY);
2548 if (error)
2549 return error;
2550
Martin Schwidefsky85055dd2011-08-17 20:42:24 +02002551 /* Allocate buffer for page keys. */
2552 error = page_key_alloc(nr_copy_pages);
2553 if (error)
2554 return error;
2555
Jiri Slabyd3c1b242010-05-01 23:52:02 +02002556 } else if (handle->cur <= nr_meta_pages + 1) {
2557 error = unpack_orig_pfns(buffer, &copy_bm);
2558 if (error)
2559 return error;
2560
2561 if (handle->cur == nr_meta_pages + 1) {
2562 error = prepare_image(&orig_bm, &copy_bm);
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08002563 if (error)
2564 return error;
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07002565
Jiri Slabyd3c1b242010-05-01 23:52:02 +02002566 chain_init(&ca, GFP_ATOMIC, PG_SAFE);
2567 memory_bm_position_reset(&orig_bm);
2568 restore_pblist = NULL;
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07002569 handle->buffer = get_buffer(&orig_bm, &ca);
Jiri Slabyd3c1b242010-05-01 23:52:02 +02002570 handle->sync_read = 0;
Rafael J. Wysocki69643272008-11-11 21:32:44 +01002571 if (IS_ERR(handle->buffer))
2572 return PTR_ERR(handle->buffer);
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08002573 }
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08002574 } else {
Jiri Slabyd3c1b242010-05-01 23:52:02 +02002575 copy_last_highmem_page();
Martin Schwidefsky85055dd2011-08-17 20:42:24 +02002576 /* Restore page key for data page (s390 only). */
2577 page_key_write(handle->buffer);
Jiri Slabyd3c1b242010-05-01 23:52:02 +02002578 handle->buffer = get_buffer(&orig_bm, &ca);
2579 if (IS_ERR(handle->buffer))
2580 return PTR_ERR(handle->buffer);
2581 if (handle->buffer != buffer)
2582 handle->sync_read = 0;
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08002583 }
Jiri Slabyd3c1b242010-05-01 23:52:02 +02002584 handle->cur++;
2585 return PAGE_SIZE;
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08002586}
2587
Rafael J. Wysocki83573762006-12-06 20:34:18 -08002588/**
2589 * snapshot_write_finalize - must be called after the last call to
2590 * snapshot_write_next() in case the last page in the image happens
2591 * to be a highmem page and its contents should be stored in the
2592 * highmem. Additionally, it releases the memory that will not be
2593 * used any more.
2594 */
2595
2596void snapshot_write_finalize(struct snapshot_handle *handle)
2597{
2598 copy_last_highmem_page();
Martin Schwidefsky85055dd2011-08-17 20:42:24 +02002599 /* Restore page key for data page (s390 only). */
2600 page_key_write(handle->buffer);
2601 page_key_free();
Rafael J. Wysocki83573762006-12-06 20:34:18 -08002602 /* Free only if we have loaded the image entirely */
Jiri Slabyd3c1b242010-05-01 23:52:02 +02002603 if (handle->cur > 1 && handle->cur > nr_meta_pages + nr_copy_pages) {
Rafael J. Wysocki83573762006-12-06 20:34:18 -08002604 memory_bm_free(&orig_bm, PG_UNSAFE_CLEAR);
2605 free_highmem_data();
2606 }
2607}
2608
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08002609int snapshot_image_loaded(struct snapshot_handle *handle)
2610{
Rafael J. Wysocki83573762006-12-06 20:34:18 -08002611 return !(!nr_copy_pages || !last_highmem_page_copied() ||
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07002612 handle->cur <= nr_meta_pages + nr_copy_pages);
2613}
2614
Rafael J. Wysocki83573762006-12-06 20:34:18 -08002615#ifdef CONFIG_HIGHMEM
2616/* Assumes that @buf is ready and points to a "safe" page */
2617static inline void
2618swap_two_pages_data(struct page *p1, struct page *p2, void *buf)
Rafael J. Wysocki940864d2006-09-25 23:32:55 -07002619{
Rafael J. Wysocki83573762006-12-06 20:34:18 -08002620 void *kaddr1, *kaddr2;
2621
Cong Wang0de9a1e2011-11-25 23:14:38 +08002622 kaddr1 = kmap_atomic(p1);
2623 kaddr2 = kmap_atomic(p2);
Jan Beulich3ecb01d2010-10-26 14:22:27 -07002624 copy_page(buf, kaddr1);
2625 copy_page(kaddr1, kaddr2);
2626 copy_page(kaddr2, buf);
Cong Wang0de9a1e2011-11-25 23:14:38 +08002627 kunmap_atomic(kaddr2);
2628 kunmap_atomic(kaddr1);
Rafael J. Wysockif577eb32006-03-23 02:59:59 -08002629}
Rafael J. Wysocki83573762006-12-06 20:34:18 -08002630
2631/**
2632 * restore_highmem - for each highmem page that was allocated before
2633 * the suspend and included in the suspend image, and also has been
2634 * allocated by the "resume" kernel swap its current (ie. "before
2635 * resume") contents with the previous (ie. "before suspend") one.
2636 *
2637 * If the resume eventually fails, we can call this function once
2638 * again and restore the "before resume" highmem state.
2639 */
2640
2641int restore_highmem(void)
2642{
2643 struct highmem_pbe *pbe = highmem_pblist;
2644 void *buf;
2645
2646 if (!pbe)
2647 return 0;
2648
2649 buf = get_image_page(GFP_ATOMIC, PG_SAFE);
2650 if (!buf)
2651 return -ENOMEM;
2652
2653 while (pbe) {
2654 swap_two_pages_data(pbe->copy_page, pbe->orig_page, buf);
2655 pbe = pbe->next;
2656 }
2657 free_image_page(buf, PG_UNSAFE_CLEAR);
2658 return 0;
2659}
2660#endif /* CONFIG_HIGHMEM */