blob: 24c96f3542318da918879ca45655afef95d55ce8 [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 *
Pavel Machek96bc7ae2005-10-30 14:59:58 -08004 * This file provide system snapshot/restore functionality.
Rafael J. Wysocki25761b62005-10-30 14:59:56 -08005 *
6 * Copyright (C) 1998-2005 Pavel Machek <pavel@suse.cz>
7 *
8 * This file is released under the GPLv2, and is based on swsusp.c.
9 *
10 */
11
12
Rafael J. Wysockif577eb32006-03-23 02:59:59 -080013#include <linux/version.h>
Rafael J. Wysocki25761b62005-10-30 14:59:56 -080014#include <linux/module.h>
15#include <linux/mm.h>
16#include <linux/suspend.h>
17#include <linux/smp_lock.h>
Rafael J. Wysocki25761b62005-10-30 14:59:56 -080018#include <linux/delay.h>
Rafael J. Wysocki25761b62005-10-30 14:59:56 -080019#include <linux/bitops.h>
Rafael J. Wysocki25761b62005-10-30 14:59:56 -080020#include <linux/spinlock.h>
Rafael J. Wysocki25761b62005-10-30 14:59:56 -080021#include <linux/kernel.h>
Rafael J. Wysocki25761b62005-10-30 14:59:56 -080022#include <linux/pm.h>
23#include <linux/device.h>
Rafael J. Wysocki25761b62005-10-30 14:59:56 -080024#include <linux/bootmem.h>
25#include <linux/syscalls.h>
26#include <linux/console.h>
27#include <linux/highmem.h>
Rafael J. Wysocki25761b62005-10-30 14:59:56 -080028
29#include <asm/uaccess.h>
30#include <asm/mmu_context.h>
31#include <asm/pgtable.h>
32#include <asm/tlbflush.h>
33#include <asm/io.h>
34
Rafael J. Wysocki25761b62005-10-30 14:59:56 -080035#include "power.h"
36
Rafael J. Wysocki7088a5c2006-01-06 00:13:05 -080037struct pbe *pagedir_nosave;
Rafael J. Wysockif577eb32006-03-23 02:59:59 -080038static unsigned int nr_copy_pages;
39static unsigned int nr_meta_pages;
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -080040static unsigned long *buffer;
Rafael J. Wysocki7088a5c2006-01-06 00:13:05 -080041
Rafael J. Wysocki25761b62005-10-30 14:59:56 -080042#ifdef CONFIG_HIGHMEM
Linus Torvalds34480972006-06-25 18:41:00 -070043unsigned int count_highmem_pages(void)
Rafael J. Wysocki72a97e02006-01-06 00:13:46 -080044{
45 struct zone *zone;
46 unsigned long zone_pfn;
47 unsigned int n = 0;
48
49 for_each_zone (zone)
50 if (is_highmem(zone)) {
51 mark_free_pages(zone);
52 for (zone_pfn = 0; zone_pfn < zone->spanned_pages; zone_pfn++) {
53 struct page *page;
54 unsigned long pfn = zone_pfn + zone->zone_start_pfn;
55 if (!pfn_valid(pfn))
56 continue;
57 page = pfn_to_page(pfn);
58 if (PageReserved(page))
59 continue;
60 if (PageNosaveFree(page))
61 continue;
62 n++;
63 }
64 }
65 return n;
66}
67
Rafael J. Wysocki25761b62005-10-30 14:59:56 -080068struct highmem_page {
69 char *data;
70 struct page *page;
71 struct highmem_page *next;
72};
73
74static struct highmem_page *highmem_copy;
75
76static int save_highmem_zone(struct zone *zone)
77{
78 unsigned long zone_pfn;
79 mark_free_pages(zone);
80 for (zone_pfn = 0; zone_pfn < zone->spanned_pages; ++zone_pfn) {
81 struct page *page;
82 struct highmem_page *save;
83 void *kaddr;
84 unsigned long pfn = zone_pfn + zone->zone_start_pfn;
85
Pavel Machekce6ed292006-03-23 03:00:05 -080086 if (!(pfn%10000))
Rafael J. Wysocki25761b62005-10-30 14:59:56 -080087 printk(".");
88 if (!pfn_valid(pfn))
89 continue;
90 page = pfn_to_page(pfn);
91 /*
92 * This condition results from rvmalloc() sans vmalloc_32()
93 * and architectural memory reservations. This should be
94 * corrected eventually when the cases giving rise to this
95 * are better understood.
96 */
Andrew Mortonc8adb492006-02-15 15:17:42 -080097 if (PageReserved(page))
Rafael J. Wysocki25761b62005-10-30 14:59:56 -080098 continue;
Rafael J. Wysocki25761b62005-10-30 14:59:56 -080099 BUG_ON(PageNosave(page));
100 if (PageNosaveFree(page))
101 continue;
102 save = kmalloc(sizeof(struct highmem_page), GFP_ATOMIC);
103 if (!save)
104 return -ENOMEM;
105 save->next = highmem_copy;
106 save->page = page;
107 save->data = (void *) get_zeroed_page(GFP_ATOMIC);
108 if (!save->data) {
109 kfree(save);
110 return -ENOMEM;
111 }
112 kaddr = kmap_atomic(page, KM_USER0);
113 memcpy(save->data, kaddr, PAGE_SIZE);
114 kunmap_atomic(kaddr, KM_USER0);
115 highmem_copy = save;
116 }
117 return 0;
118}
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800119
Linus Torvalds34480972006-06-25 18:41:00 -0700120int save_highmem(void)
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800121{
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800122 struct zone *zone;
123 int res = 0;
124
Pavel Machekce6ed292006-03-23 03:00:05 -0800125 pr_debug("swsusp: Saving Highmem");
Shaohua Lie4e4d662006-03-23 03:00:06 -0800126 drain_local_pages();
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800127 for_each_zone (zone) {
128 if (is_highmem(zone))
129 res = save_highmem_zone(zone);
130 if (res)
131 return res;
132 }
Pavel Machekce6ed292006-03-23 03:00:05 -0800133 printk("\n");
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800134 return 0;
135}
136
Linus Torvalds34480972006-06-25 18:41:00 -0700137int restore_highmem(void)
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800138{
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800139 printk("swsusp: Restoring Highmem\n");
140 while (highmem_copy) {
141 struct highmem_page *save = highmem_copy;
142 void *kaddr;
143 highmem_copy = save->next;
144
145 kaddr = kmap_atomic(save->page, KM_USER0);
146 memcpy(kaddr, save->data, PAGE_SIZE);
147 kunmap_atomic(kaddr, KM_USER0);
148 free_page((long) save->data);
149 kfree(save);
150 }
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800151 return 0;
152}
Shaohua Lice4ab002006-06-23 02:04:44 -0700153#else
Adrian Bunk7bff24e2006-06-23 02:04:47 -0700154static inline unsigned int count_highmem_pages(void) {return 0;}
155static inline int save_highmem(void) {return 0;}
156static inline int restore_highmem(void) {return 0;}
Rafael J. Wysocki0fbeb5a2005-11-08 21:34:41 -0800157#endif
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800158
159static int pfn_is_nosave(unsigned long pfn)
160{
161 unsigned long nosave_begin_pfn = __pa(&__nosave_begin) >> PAGE_SHIFT;
162 unsigned long nosave_end_pfn = PAGE_ALIGN(__pa(&__nosave_end)) >> PAGE_SHIFT;
163 return (pfn >= nosave_begin_pfn) && (pfn < nosave_end_pfn);
164}
165
166/**
167 * saveable - Determine whether a page should be cloned or not.
168 * @pfn: The page
169 *
170 * We save a page if it's Reserved, and not in the range of pages
171 * statically defined as 'unsaveable', or if it isn't reserved, and
172 * isn't part of a free chunk of pages.
173 */
174
Pavel Machekde491862005-10-30 14:59:59 -0800175static int saveable(struct zone *zone, unsigned long *zone_pfn)
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800176{
177 unsigned long pfn = *zone_pfn + zone->zone_start_pfn;
Pavel Machekde491862005-10-30 14:59:59 -0800178 struct page *page;
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800179
180 if (!pfn_valid(pfn))
181 return 0;
182
183 page = pfn_to_page(pfn);
Linus Torvalds34480972006-06-25 18:41:00 -0700184 BUG_ON(PageReserved(page) && PageNosave(page));
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800185 if (PageNosave(page))
186 return 0;
Rafael J. Wysocki72a97e02006-01-06 00:13:46 -0800187 if (PageReserved(page) && pfn_is_nosave(pfn))
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800188 return 0;
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800189 if (PageNosaveFree(page))
190 return 0;
191
192 return 1;
193}
194
Rafael J. Wysocki72a97e02006-01-06 00:13:46 -0800195unsigned int count_data_pages(void)
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800196{
197 struct zone *zone;
198 unsigned long zone_pfn;
Pavel Machekdc19d502005-11-07 00:58:40 -0800199 unsigned int n = 0;
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800200
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800201 for_each_zone (zone) {
202 if (is_highmem(zone))
203 continue;
204 mark_free_pages(zone);
205 for (zone_pfn = 0; zone_pfn < zone->spanned_pages; ++zone_pfn)
Rafael J. Wysockia0f49652005-10-30 14:59:57 -0800206 n += saveable(zone, &zone_pfn);
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800207 }
Rafael J. Wysockia0f49652005-10-30 14:59:57 -0800208 return n;
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800209}
210
Rafael J. Wysockia0f49652005-10-30 14:59:57 -0800211static void copy_data_pages(struct pbe *pblist)
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800212{
213 struct zone *zone;
214 unsigned long zone_pfn;
Rafael J. Wysockia0f49652005-10-30 14:59:57 -0800215 struct pbe *pbe, *p;
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800216
Rafael J. Wysockia0f49652005-10-30 14:59:57 -0800217 pbe = pblist;
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800218 for_each_zone (zone) {
219 if (is_highmem(zone))
220 continue;
221 mark_free_pages(zone);
222 /* This is necessary for swsusp_free() */
Rafael J. Wysockia0f49652005-10-30 14:59:57 -0800223 for_each_pb_page (p, pblist)
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800224 SetPageNosaveFree(virt_to_page(p));
Rafael J. Wysockia0f49652005-10-30 14:59:57 -0800225 for_each_pbe (p, pblist)
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800226 SetPageNosaveFree(virt_to_page(p->address));
227 for (zone_pfn = 0; zone_pfn < zone->spanned_pages; ++zone_pfn) {
228 if (saveable(zone, &zone_pfn)) {
Pavel Machekde491862005-10-30 14:59:59 -0800229 struct page *page;
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800230 page = pfn_to_page(zone_pfn + zone->zone_start_pfn);
231 BUG_ON(!pbe);
232 pbe->orig_address = (unsigned long)page_address(page);
233 /* copy_page is not usable for copying task structs. */
234 memcpy((void *)pbe->address, (void *)pbe->orig_address, PAGE_SIZE);
235 pbe = pbe->next;
236 }
237 }
238 }
239 BUG_ON(pbe);
240}
241
242
243/**
244 * free_pagedir - free pages allocated with alloc_pagedir()
245 */
246
Rafael J. Wysocki4a3b98a2006-04-18 22:20:29 -0700247static void free_pagedir(struct pbe *pblist, int clear_nosave_free)
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800248{
249 struct pbe *pbe;
250
251 while (pblist) {
252 pbe = (pblist + PB_PAGE_SKIP)->next;
253 ClearPageNosave(virt_to_page(pblist));
Rafael J. Wysocki4a3b98a2006-04-18 22:20:29 -0700254 if (clear_nosave_free)
255 ClearPageNosaveFree(virt_to_page(pblist));
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800256 free_page((unsigned long)pblist);
257 pblist = pbe;
258 }
259}
260
261/**
262 * fill_pb_page - Create a list of PBEs on a given memory page
263 */
264
265static inline void fill_pb_page(struct pbe *pbpage)
266{
267 struct pbe *p;
268
269 p = pbpage;
270 pbpage += PB_PAGE_SKIP;
271 do
272 p->next = p + 1;
273 while (++p < pbpage);
274}
275
276/**
277 * create_pbe_list - Create a list of PBEs on top of a given chain
278 * of memory pages allocated with alloc_pagedir()
279 */
280
Rafael J. Wysocki7088a5c2006-01-06 00:13:05 -0800281static inline void create_pbe_list(struct pbe *pblist, unsigned int nr_pages)
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800282{
283 struct pbe *pbpage, *p;
Pavel Machekdc19d502005-11-07 00:58:40 -0800284 unsigned int num = PBES_PER_PAGE;
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800285
286 for_each_pb_page (pbpage, pblist) {
287 if (num >= nr_pages)
288 break;
289
290 fill_pb_page(pbpage);
291 num += PBES_PER_PAGE;
292 }
293 if (pbpage) {
294 for (num -= PBES_PER_PAGE - 1, p = pbpage; num < nr_pages; p++, num++)
295 p->next = p + 1;
296 p->next = NULL;
297 }
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800298}
299
Rafael J. Wysocki968808b82006-06-23 02:04:48 -0700300static unsigned int unsafe_pages;
Rafael J. Wysocki72a97e02006-01-06 00:13:46 -0800301
302/**
Rafael J. Wysocki054bd4c2005-11-08 21:34:39 -0800303 * @safe_needed - on resume, for storing the PBE list and the image,
304 * we can only use memory pages that do not conflict with the pages
Rafael J. Wysocki968808b82006-06-23 02:04:48 -0700305 * used before suspend.
Rafael J. Wysocki054bd4c2005-11-08 21:34:39 -0800306 *
307 * The unsafe pages are marked with the PG_nosave_free flag
Rafael J. Wysocki968808b82006-06-23 02:04:48 -0700308 * and we count them using unsafe_pages
Rafael J. Wysocki054bd4c2005-11-08 21:34:39 -0800309 */
310
311static inline void *alloc_image_page(gfp_t gfp_mask, int safe_needed)
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800312{
Rafael J. Wysocki054bd4c2005-11-08 21:34:39 -0800313 void *res;
314
Rafael J. Wysocki968808b82006-06-23 02:04:48 -0700315 res = (void *)get_zeroed_page(gfp_mask);
Rafael J. Wysocki054bd4c2005-11-08 21:34:39 -0800316 if (safe_needed)
Rafael J. Wysocki968808b82006-06-23 02:04:48 -0700317 while (res && PageNosaveFree(virt_to_page(res))) {
318 /* The page is unsafe, mark it for swsusp_free() */
319 SetPageNosave(virt_to_page(res));
320 unsafe_pages++;
Rafael J. Wysocki054bd4c2005-11-08 21:34:39 -0800321 res = (void *)get_zeroed_page(gfp_mask);
Rafael J. Wysocki968808b82006-06-23 02:04:48 -0700322 }
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800323 if (res) {
324 SetPageNosave(virt_to_page(res));
325 SetPageNosaveFree(virt_to_page(res));
326 }
327 return res;
328}
329
Rafael J. Wysocki054bd4c2005-11-08 21:34:39 -0800330unsigned long get_safe_page(gfp_t gfp_mask)
331{
332 return (unsigned long)alloc_image_page(gfp_mask, 1);
333}
334
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800335/**
336 * alloc_pagedir - Allocate the page directory.
337 *
338 * First, determine exactly how many pages we need and
339 * allocate them.
340 *
341 * We arrange the pages in a chain: each page is an array of PBES_PER_PAGE
342 * struct pbe elements (pbes) and the last element in the page points
343 * to the next page.
344 *
345 * On each page we set up a list of struct_pbe elements.
346 */
347
Adrian Bunk7bff24e2006-06-23 02:04:47 -0700348static struct pbe *alloc_pagedir(unsigned int nr_pages, gfp_t gfp_mask,
349 int safe_needed)
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800350{
Pavel Machekdc19d502005-11-07 00:58:40 -0800351 unsigned int num;
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800352 struct pbe *pblist, *pbe;
353
354 if (!nr_pages)
355 return NULL;
356
Rafael J. Wysocki054bd4c2005-11-08 21:34:39 -0800357 pblist = alloc_image_page(gfp_mask, safe_needed);
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800358 /* FIXME: rewrite this ugly loop */
359 for (pbe = pblist, num = PBES_PER_PAGE; pbe && num < nr_pages;
360 pbe = pbe->next, num += PBES_PER_PAGE) {
361 pbe += PB_PAGE_SKIP;
Rafael J. Wysocki054bd4c2005-11-08 21:34:39 -0800362 pbe->next = alloc_image_page(gfp_mask, safe_needed);
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800363 }
364 if (!pbe) { /* get_zeroed_page() failed */
Rafael J. Wysocki4a3b98a2006-04-18 22:20:29 -0700365 free_pagedir(pblist, 1);
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800366 pblist = NULL;
Rafael J. Wysocki7088a5c2006-01-06 00:13:05 -0800367 } else
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -0800368 create_pbe_list(pblist, nr_pages);
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800369 return pblist;
370}
371
372/**
373 * Free pages we allocated for suspend. Suspend pages are alocated
374 * before atomic copy, so we need to free them after resume.
375 */
376
377void swsusp_free(void)
378{
379 struct zone *zone;
380 unsigned long zone_pfn;
381
382 for_each_zone(zone) {
383 for (zone_pfn = 0; zone_pfn < zone->spanned_pages; ++zone_pfn)
384 if (pfn_valid(zone_pfn + zone->zone_start_pfn)) {
Pavel Machekdc19d502005-11-07 00:58:40 -0800385 struct page *page;
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800386 page = pfn_to_page(zone_pfn + zone->zone_start_pfn);
387 if (PageNosave(page) && PageNosaveFree(page)) {
388 ClearPageNosave(page);
389 ClearPageNosaveFree(page);
390 free_page((long) page_address(page));
391 }
392 }
393 }
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800394 nr_copy_pages = 0;
395 nr_meta_pages = 0;
396 pagedir_nosave = NULL;
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -0800397 buffer = NULL;
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800398}
399
400
401/**
402 * enough_free_mem - Make sure we enough free memory to snapshot.
403 *
404 * Returns TRUE or FALSE after checking the number of available
405 * free pages.
406 */
407
Pavel Machekdc19d502005-11-07 00:58:40 -0800408static int enough_free_mem(unsigned int nr_pages)
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800409{
Rafael J. Wysockie5e2fa72006-01-06 00:14:20 -0800410 struct zone *zone;
411 unsigned int n = 0;
412
413 for_each_zone (zone)
414 if (!is_highmem(zone))
415 n += zone->free_pages;
416 pr_debug("swsusp: available memory: %u pages\n", n);
417 return n > (nr_pages + PAGES_FOR_IO +
Rafael J. Wysockia0f49652005-10-30 14:59:57 -0800418 (nr_pages + PBES_PER_PAGE - 1) / PBES_PER_PAGE);
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800419}
420
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800421static int alloc_data_pages(struct pbe *pblist, gfp_t gfp_mask, int safe_needed)
Rafael J. Wysocki054bd4c2005-11-08 21:34:39 -0800422{
423 struct pbe *p;
424
425 for_each_pbe (p, pblist) {
426 p->address = (unsigned long)alloc_image_page(gfp_mask, safe_needed);
427 if (!p->address)
428 return -ENOMEM;
429 }
430 return 0;
431}
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800432
Pavel Machekdc19d502005-11-07 00:58:40 -0800433static struct pbe *swsusp_alloc(unsigned int nr_pages)
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800434{
Rafael J. Wysocki054bd4c2005-11-08 21:34:39 -0800435 struct pbe *pblist;
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800436
Rafael J. Wysocki054bd4c2005-11-08 21:34:39 -0800437 if (!(pblist = alloc_pagedir(nr_pages, GFP_ATOMIC | __GFP_COLD, 0))) {
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800438 printk(KERN_ERR "suspend: Allocating pagedir failed.\n");
Rafael J. Wysockia0f49652005-10-30 14:59:57 -0800439 return NULL;
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800440 }
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800441
Rafael J. Wysocki054bd4c2005-11-08 21:34:39 -0800442 if (alloc_data_pages(pblist, GFP_ATOMIC | __GFP_COLD, 0)) {
443 printk(KERN_ERR "suspend: Allocating image pages failed.\n");
444 swsusp_free();
445 return NULL;
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800446 }
447
Rafael J. Wysockia0f49652005-10-30 14:59:57 -0800448 return pblist;
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800449}
450
Rafael J. Wysocki2e32a432005-10-30 15:00:00 -0800451asmlinkage int swsusp_save(void)
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800452{
Pavel Machekdc19d502005-11-07 00:58:40 -0800453 unsigned int nr_pages;
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800454
455 pr_debug("swsusp: critical section: \n");
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800456
457 drain_local_pages();
Rafael J. Wysockia0f49652005-10-30 14:59:57 -0800458 nr_pages = count_data_pages();
459 printk("swsusp: Need to copy %u pages\n", nr_pages);
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800460
461 pr_debug("swsusp: pages needed: %u + %lu + %u, free: %u\n",
Rafael J. Wysockia0f49652005-10-30 14:59:57 -0800462 nr_pages,
463 (nr_pages + PBES_PER_PAGE - 1) / PBES_PER_PAGE,
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800464 PAGES_FOR_IO, nr_free_pages());
465
Rafael J. Wysockia0f49652005-10-30 14:59:57 -0800466 if (!enough_free_mem(nr_pages)) {
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800467 printk(KERN_ERR "swsusp: Not enough free memory\n");
468 return -ENOMEM;
469 }
470
Rafael J. Wysockia0f49652005-10-30 14:59:57 -0800471 pagedir_nosave = swsusp_alloc(nr_pages);
472 if (!pagedir_nosave)
473 return -ENOMEM;
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800474
475 /* During allocating of suspend pagedir, new cold pages may appear.
476 * Kill them.
477 */
478 drain_local_pages();
Rafael J. Wysockia0f49652005-10-30 14:59:57 -0800479 copy_data_pages(pagedir_nosave);
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800480
481 /*
482 * End of critical section. From now on, we can write to memory,
483 * but we should not touch disk. This specially means we must _not_
484 * touch swap space! Except we must write out our image of course.
485 */
486
Rafael J. Wysockia0f49652005-10-30 14:59:57 -0800487 nr_copy_pages = nr_pages;
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800488 nr_meta_pages = (nr_pages * sizeof(long) + PAGE_SIZE - 1) >> PAGE_SHIFT;
Rafael J. Wysockia0f49652005-10-30 14:59:57 -0800489
490 printk("swsusp: critical section/: done (%d pages copied)\n", nr_pages);
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800491 return 0;
492}
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800493
494static void init_header(struct swsusp_info *info)
495{
496 memset(info, 0, sizeof(struct swsusp_info));
497 info->version_code = LINUX_VERSION_CODE;
498 info->num_physpages = num_physpages;
499 memcpy(&info->uts, &system_utsname, sizeof(system_utsname));
500 info->cpus = num_online_cpus();
501 info->image_pages = nr_copy_pages;
502 info->pages = nr_copy_pages + nr_meta_pages + 1;
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -0800503 info->size = info->pages;
504 info->size <<= PAGE_SHIFT;
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800505}
506
507/**
508 * pack_orig_addresses - the .orig_address fields of the PBEs from the
509 * list starting at @pbe are stored in the array @buf[] (1 page)
510 */
511
512static inline struct pbe *pack_orig_addresses(unsigned long *buf, struct pbe *pbe)
513{
514 int j;
515
516 for (j = 0; j < PAGE_SIZE / sizeof(long) && pbe; j++) {
517 buf[j] = pbe->orig_address;
518 pbe = pbe->next;
519 }
520 if (!pbe)
521 for (; j < PAGE_SIZE / sizeof(long); j++)
522 buf[j] = 0;
523 return pbe;
524}
525
526/**
527 * snapshot_read_next - used for reading the system memory snapshot.
528 *
529 * On the first call to it @handle should point to a zeroed
530 * snapshot_handle structure. The structure gets updated and a pointer
531 * to it should be passed to this function every next time.
532 *
533 * The @count parameter should contain the number of bytes the caller
534 * wants to read from the snapshot. It must not be zero.
535 *
536 * On success the function returns a positive number. Then, the caller
537 * is allowed to read up to the returned number of bytes from the memory
538 * location computed by the data_of() macro. The number returned
539 * may be smaller than @count, but this only happens if the read would
540 * cross a page boundary otherwise.
541 *
542 * The function returns 0 to indicate the end of data stream condition,
543 * and a negative number is returned on error. In such cases the
544 * structure pointed to by @handle is not updated and should not be used
545 * any more.
546 */
547
548int snapshot_read_next(struct snapshot_handle *handle, size_t count)
549{
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800550 if (handle->page > nr_meta_pages + nr_copy_pages)
551 return 0;
552 if (!buffer) {
553 /* This makes the buffer be freed by swsusp_free() */
554 buffer = alloc_image_page(GFP_ATOMIC, 0);
555 if (!buffer)
556 return -ENOMEM;
557 }
558 if (!handle->offset) {
559 init_header((struct swsusp_info *)buffer);
560 handle->buffer = buffer;
561 handle->pbe = pagedir_nosave;
562 }
563 if (handle->prev < handle->page) {
564 if (handle->page <= nr_meta_pages) {
565 handle->pbe = pack_orig_addresses(buffer, handle->pbe);
566 if (!handle->pbe)
567 handle->pbe = pagedir_nosave;
568 } else {
569 handle->buffer = (void *)handle->pbe->address;
570 handle->pbe = handle->pbe->next;
571 }
572 handle->prev = handle->page;
573 }
574 handle->buf_offset = handle->page_offset;
575 if (handle->page_offset + count >= PAGE_SIZE) {
576 count = PAGE_SIZE - handle->page_offset;
577 handle->page_offset = 0;
578 handle->page++;
579 } else {
580 handle->page_offset += count;
581 }
582 handle->offset += count;
583 return count;
584}
585
586/**
587 * mark_unsafe_pages - mark the pages that cannot be used for storing
588 * the image during resume, because they conflict with the pages that
589 * had been used before suspend
590 */
591
592static int mark_unsafe_pages(struct pbe *pblist)
593{
594 struct zone *zone;
595 unsigned long zone_pfn;
596 struct pbe *p;
597
598 if (!pblist) /* a sanity check */
599 return -EINVAL;
600
601 /* Clear page flags */
602 for_each_zone (zone) {
603 for (zone_pfn = 0; zone_pfn < zone->spanned_pages; ++zone_pfn)
604 if (pfn_valid(zone_pfn + zone->zone_start_pfn))
605 ClearPageNosaveFree(pfn_to_page(zone_pfn +
606 zone->zone_start_pfn));
607 }
608
609 /* Mark orig addresses */
610 for_each_pbe (p, pblist) {
611 if (virt_addr_valid(p->orig_address))
612 SetPageNosaveFree(virt_to_page(p->orig_address));
613 else
614 return -EFAULT;
615 }
616
Rafael J. Wysocki968808b82006-06-23 02:04:48 -0700617 unsafe_pages = 0;
618
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800619 return 0;
620}
621
622static void copy_page_backup_list(struct pbe *dst, struct pbe *src)
623{
624 /* We assume both lists contain the same number of elements */
625 while (src) {
626 dst->orig_address = src->orig_address;
627 dst = dst->next;
628 src = src->next;
629 }
630}
631
632static int check_header(struct swsusp_info *info)
633{
634 char *reason = NULL;
635
636 if (info->version_code != LINUX_VERSION_CODE)
637 reason = "kernel version";
638 if (info->num_physpages != num_physpages)
639 reason = "memory size";
640 if (strcmp(info->uts.sysname,system_utsname.sysname))
641 reason = "system type";
642 if (strcmp(info->uts.release,system_utsname.release))
643 reason = "kernel release";
644 if (strcmp(info->uts.version,system_utsname.version))
645 reason = "version";
646 if (strcmp(info->uts.machine,system_utsname.machine))
647 reason = "machine";
648 if (reason) {
649 printk(KERN_ERR "swsusp: Resume mismatch: %s\n", reason);
650 return -EPERM;
651 }
652 return 0;
653}
654
655/**
656 * load header - check the image header and copy data from it
657 */
658
659static int load_header(struct snapshot_handle *handle,
660 struct swsusp_info *info)
661{
662 int error;
663 struct pbe *pblist;
664
665 error = check_header(info);
666 if (!error) {
667 pblist = alloc_pagedir(info->image_pages, GFP_ATOMIC, 0);
668 if (!pblist)
669 return -ENOMEM;
670 pagedir_nosave = pblist;
671 handle->pbe = pblist;
672 nr_copy_pages = info->image_pages;
673 nr_meta_pages = info->pages - info->image_pages - 1;
674 }
675 return error;
676}
677
678/**
679 * unpack_orig_addresses - copy the elements of @buf[] (1 page) to
680 * the PBEs in the list starting at @pbe
681 */
682
683static inline struct pbe *unpack_orig_addresses(unsigned long *buf,
684 struct pbe *pbe)
685{
686 int j;
687
688 for (j = 0; j < PAGE_SIZE / sizeof(long) && pbe; j++) {
689 pbe->orig_address = buf[j];
690 pbe = pbe->next;
691 }
692 return pbe;
693}
694
695/**
Rafael J. Wysocki968808b82006-06-23 02:04:48 -0700696 * prepare_image - use metadata contained in the PBE list
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800697 * pointed to by pagedir_nosave to mark the pages that will
698 * be overwritten in the process of restoring the system
Rafael J. Wysocki968808b82006-06-23 02:04:48 -0700699 * memory state from the image ("unsafe" pages) and allocate
700 * memory for the image
701 *
702 * The idea is to allocate the PBE list first and then
703 * allocate as many pages as it's needed for the image data,
704 * but not to assign these pages to the PBEs initially.
705 * Instead, we just mark them as allocated and create a list
706 * of "safe" which will be used later
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800707 */
708
Rafael J. Wysocki968808b82006-06-23 02:04:48 -0700709struct safe_page {
710 struct safe_page *next;
711 char padding[PAGE_SIZE - sizeof(void *)];
712};
713
714static struct safe_page *safe_pages;
715
716static int prepare_image(struct snapshot_handle *handle)
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800717{
718 int error = 0;
Rafael J. Wysocki968808b82006-06-23 02:04:48 -0700719 unsigned int nr_pages = nr_copy_pages;
720 struct pbe *p, *pblist = NULL;
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800721
722 p = pagedir_nosave;
723 error = mark_unsafe_pages(p);
724 if (!error) {
Rafael J. Wysocki968808b82006-06-23 02:04:48 -0700725 pblist = alloc_pagedir(nr_pages, GFP_ATOMIC, 1);
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800726 if (pblist)
727 copy_page_backup_list(pblist, p);
Rafael J. Wysocki4a3b98a2006-04-18 22:20:29 -0700728 free_pagedir(p, 0);
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800729 if (!pblist)
730 error = -ENOMEM;
731 }
Rafael J. Wysocki968808b82006-06-23 02:04:48 -0700732 safe_pages = NULL;
733 if (!error && nr_pages > unsafe_pages) {
734 nr_pages -= unsafe_pages;
735 while (nr_pages--) {
736 struct safe_page *ptr;
737
738 ptr = (struct safe_page *)get_zeroed_page(GFP_ATOMIC);
739 if (!ptr) {
740 error = -ENOMEM;
741 break;
742 }
743 if (!PageNosaveFree(virt_to_page(ptr))) {
744 /* The page is "safe", add it to the list */
745 ptr->next = safe_pages;
746 safe_pages = ptr;
747 }
748 /* Mark the page as allocated */
749 SetPageNosave(virt_to_page(ptr));
750 SetPageNosaveFree(virt_to_page(ptr));
751 }
752 }
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800753 if (!error) {
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800754 pagedir_nosave = pblist;
755 } else {
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800756 handle->pbe = NULL;
Rafael J. Wysocki968808b82006-06-23 02:04:48 -0700757 swsusp_free();
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800758 }
759 return error;
760}
761
Rafael J. Wysocki968808b82006-06-23 02:04:48 -0700762static void *get_buffer(struct snapshot_handle *handle)
763{
764 struct pbe *pbe = handle->pbe, *last = handle->last_pbe;
765 struct page *page = virt_to_page(pbe->orig_address);
766
767 if (PageNosave(page) && PageNosaveFree(page)) {
768 /*
769 * We have allocated the "original" page frame and we can
770 * use it directly to store the read page
771 */
772 pbe->address = 0;
773 if (last && last->next)
774 last->next = NULL;
775 return (void *)pbe->orig_address;
776 }
777 /*
778 * The "original" page frame has not been allocated and we have to
779 * use a "safe" page frame to store the read page
780 */
781 pbe->address = (unsigned long)safe_pages;
782 safe_pages = safe_pages->next;
783 if (last)
784 last->next = pbe;
785 handle->last_pbe = pbe;
786 return (void *)pbe->address;
787}
788
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800789/**
790 * snapshot_write_next - used for writing the system memory snapshot.
791 *
792 * On the first call to it @handle should point to a zeroed
793 * snapshot_handle structure. The structure gets updated and a pointer
794 * to it should be passed to this function every next time.
795 *
796 * The @count parameter should contain the number of bytes the caller
797 * wants to write to the image. It must not be zero.
798 *
799 * On success the function returns a positive number. Then, the caller
800 * is allowed to write up to the returned number of bytes to the memory
801 * location computed by the data_of() macro. The number returned
802 * may be smaller than @count, but this only happens if the write would
803 * cross a page boundary otherwise.
804 *
805 * The function returns 0 to indicate the "end of file" condition,
806 * and a negative number is returned on error. In such cases the
807 * structure pointed to by @handle is not updated and should not be used
808 * any more.
809 */
810
811int snapshot_write_next(struct snapshot_handle *handle, size_t count)
812{
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800813 int error = 0;
814
815 if (handle->prev && handle->page > nr_meta_pages + nr_copy_pages)
816 return 0;
817 if (!buffer) {
818 /* This makes the buffer be freed by swsusp_free() */
819 buffer = alloc_image_page(GFP_ATOMIC, 0);
820 if (!buffer)
821 return -ENOMEM;
822 }
823 if (!handle->offset)
824 handle->buffer = buffer;
825 if (handle->prev < handle->page) {
826 if (!handle->prev) {
827 error = load_header(handle, (struct swsusp_info *)buffer);
828 if (error)
829 return error;
830 } else if (handle->prev <= nr_meta_pages) {
831 handle->pbe = unpack_orig_addresses(buffer, handle->pbe);
832 if (!handle->pbe) {
Rafael J. Wysocki968808b82006-06-23 02:04:48 -0700833 error = prepare_image(handle);
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800834 if (error)
835 return error;
836 handle->pbe = pagedir_nosave;
Rafael J. Wysocki968808b82006-06-23 02:04:48 -0700837 handle->last_pbe = NULL;
838 handle->buffer = get_buffer(handle);
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800839 }
840 } else {
841 handle->pbe = handle->pbe->next;
Rafael J. Wysocki968808b82006-06-23 02:04:48 -0700842 handle->buffer = get_buffer(handle);
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800843 }
844 handle->prev = handle->page;
845 }
846 handle->buf_offset = handle->page_offset;
847 if (handle->page_offset + count >= PAGE_SIZE) {
848 count = PAGE_SIZE - handle->page_offset;
849 handle->page_offset = 0;
850 handle->page++;
851 } else {
852 handle->page_offset += count;
853 }
854 handle->offset += count;
855 return count;
856}
857
858int snapshot_image_loaded(struct snapshot_handle *handle)
859{
860 return !(!handle->pbe || handle->pbe->next || !nr_copy_pages ||
861 handle->page <= nr_meta_pages + nr_copy_pages);
862}