blob: 1b46c2da5a50566ff7a1997b9c83581d5f9c9554 [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
Rafael J. Wysocki72a97e02006-01-06 00:13:46 -080043unsigned int count_highmem_pages(void)
44{
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
Rafael J. Wysocki0fbeb5a2005-11-08 21:34:41 -0800120int 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");
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800126 for_each_zone (zone) {
127 if (is_highmem(zone))
128 res = save_highmem_zone(zone);
129 if (res)
130 return res;
131 }
Pavel Machekce6ed292006-03-23 03:00:05 -0800132 printk("\n");
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800133 return 0;
134}
135
136int restore_highmem(void)
137{
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800138 printk("swsusp: Restoring Highmem\n");
139 while (highmem_copy) {
140 struct highmem_page *save = highmem_copy;
141 void *kaddr;
142 highmem_copy = save->next;
143
144 kaddr = kmap_atomic(save->page, KM_USER0);
145 memcpy(kaddr, save->data, PAGE_SIZE);
146 kunmap_atomic(kaddr, KM_USER0);
147 free_page((long) save->data);
148 kfree(save);
149 }
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800150 return 0;
151}
Rafael J. Wysocki0fbeb5a2005-11-08 21:34:41 -0800152#endif
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800153
154static int pfn_is_nosave(unsigned long pfn)
155{
156 unsigned long nosave_begin_pfn = __pa(&__nosave_begin) >> PAGE_SHIFT;
157 unsigned long nosave_end_pfn = PAGE_ALIGN(__pa(&__nosave_end)) >> PAGE_SHIFT;
158 return (pfn >= nosave_begin_pfn) && (pfn < nosave_end_pfn);
159}
160
161/**
162 * saveable - Determine whether a page should be cloned or not.
163 * @pfn: The page
164 *
165 * We save a page if it's Reserved, and not in the range of pages
166 * statically defined as 'unsaveable', or if it isn't reserved, and
167 * isn't part of a free chunk of pages.
168 */
169
Pavel Machekde491862005-10-30 14:59:59 -0800170static int saveable(struct zone *zone, unsigned long *zone_pfn)
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800171{
172 unsigned long pfn = *zone_pfn + zone->zone_start_pfn;
Pavel Machekde491862005-10-30 14:59:59 -0800173 struct page *page;
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800174
175 if (!pfn_valid(pfn))
176 return 0;
177
178 page = pfn_to_page(pfn);
179 BUG_ON(PageReserved(page) && PageNosave(page));
180 if (PageNosave(page))
181 return 0;
Rafael J. Wysocki72a97e02006-01-06 00:13:46 -0800182 if (PageReserved(page) && pfn_is_nosave(pfn))
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800183 return 0;
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800184 if (PageNosaveFree(page))
185 return 0;
186
187 return 1;
188}
189
Rafael J. Wysocki72a97e02006-01-06 00:13:46 -0800190unsigned int count_data_pages(void)
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800191{
192 struct zone *zone;
193 unsigned long zone_pfn;
Pavel Machekdc19d502005-11-07 00:58:40 -0800194 unsigned int n = 0;
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800195
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800196 for_each_zone (zone) {
197 if (is_highmem(zone))
198 continue;
199 mark_free_pages(zone);
200 for (zone_pfn = 0; zone_pfn < zone->spanned_pages; ++zone_pfn)
Rafael J. Wysockia0f49652005-10-30 14:59:57 -0800201 n += saveable(zone, &zone_pfn);
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800202 }
Rafael J. Wysockia0f49652005-10-30 14:59:57 -0800203 return n;
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800204}
205
Rafael J. Wysockia0f49652005-10-30 14:59:57 -0800206static void copy_data_pages(struct pbe *pblist)
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800207{
208 struct zone *zone;
209 unsigned long zone_pfn;
Rafael J. Wysockia0f49652005-10-30 14:59:57 -0800210 struct pbe *pbe, *p;
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800211
Rafael J. Wysockia0f49652005-10-30 14:59:57 -0800212 pbe = pblist;
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800213 for_each_zone (zone) {
214 if (is_highmem(zone))
215 continue;
216 mark_free_pages(zone);
217 /* This is necessary for swsusp_free() */
Rafael J. Wysockia0f49652005-10-30 14:59:57 -0800218 for_each_pb_page (p, pblist)
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800219 SetPageNosaveFree(virt_to_page(p));
Rafael J. Wysockia0f49652005-10-30 14:59:57 -0800220 for_each_pbe (p, pblist)
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800221 SetPageNosaveFree(virt_to_page(p->address));
222 for (zone_pfn = 0; zone_pfn < zone->spanned_pages; ++zone_pfn) {
223 if (saveable(zone, &zone_pfn)) {
Pavel Machekde491862005-10-30 14:59:59 -0800224 struct page *page;
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800225 page = pfn_to_page(zone_pfn + zone->zone_start_pfn);
226 BUG_ON(!pbe);
227 pbe->orig_address = (unsigned long)page_address(page);
228 /* copy_page is not usable for copying task structs. */
229 memcpy((void *)pbe->address, (void *)pbe->orig_address, PAGE_SIZE);
230 pbe = pbe->next;
231 }
232 }
233 }
234 BUG_ON(pbe);
235}
236
237
238/**
239 * free_pagedir - free pages allocated with alloc_pagedir()
240 */
241
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800242static void free_pagedir(struct pbe *pblist)
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800243{
244 struct pbe *pbe;
245
246 while (pblist) {
247 pbe = (pblist + PB_PAGE_SKIP)->next;
248 ClearPageNosave(virt_to_page(pblist));
249 ClearPageNosaveFree(virt_to_page(pblist));
250 free_page((unsigned long)pblist);
251 pblist = pbe;
252 }
253}
254
255/**
256 * fill_pb_page - Create a list of PBEs on a given memory page
257 */
258
259static inline void fill_pb_page(struct pbe *pbpage)
260{
261 struct pbe *p;
262
263 p = pbpage;
264 pbpage += PB_PAGE_SKIP;
265 do
266 p->next = p + 1;
267 while (++p < pbpage);
268}
269
270/**
271 * create_pbe_list - Create a list of PBEs on top of a given chain
272 * of memory pages allocated with alloc_pagedir()
273 */
274
Rafael J. Wysocki7088a5c2006-01-06 00:13:05 -0800275static inline void create_pbe_list(struct pbe *pblist, unsigned int nr_pages)
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800276{
277 struct pbe *pbpage, *p;
Pavel Machekdc19d502005-11-07 00:58:40 -0800278 unsigned int num = PBES_PER_PAGE;
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800279
280 for_each_pb_page (pbpage, pblist) {
281 if (num >= nr_pages)
282 break;
283
284 fill_pb_page(pbpage);
285 num += PBES_PER_PAGE;
286 }
287 if (pbpage) {
288 for (num -= PBES_PER_PAGE - 1, p = pbpage; num < nr_pages; p++, num++)
289 p->next = p + 1;
290 p->next = NULL;
291 }
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800292}
293
Rafael J. Wysocki054bd4c2005-11-08 21:34:39 -0800294/**
Rafael J. Wysocki72a97e02006-01-06 00:13:46 -0800295 * On resume it is necessary to trace and eventually free the unsafe
296 * pages that have been allocated, because they are needed for I/O
297 * (on x86-64 we likely will "eat" these pages once again while
298 * creating the temporary page translation tables)
299 */
300
301struct eaten_page {
302 struct eaten_page *next;
303 char padding[PAGE_SIZE - sizeof(void *)];
304};
305
306static struct eaten_page *eaten_pages = NULL;
307
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800308static void release_eaten_pages(void)
Rafael J. Wysocki72a97e02006-01-06 00:13:46 -0800309{
310 struct eaten_page *p, *q;
311
312 p = eaten_pages;
313 while (p) {
314 q = p->next;
315 /* We don't want swsusp_free() to free this page again */
316 ClearPageNosave(virt_to_page(p));
317 free_page((unsigned long)p);
318 p = q;
319 }
320 eaten_pages = NULL;
321}
322
323/**
Rafael J. Wysocki054bd4c2005-11-08 21:34:39 -0800324 * @safe_needed - on resume, for storing the PBE list and the image,
325 * we can only use memory pages that do not conflict with the pages
326 * which had been used before suspend.
327 *
328 * The unsafe pages are marked with the PG_nosave_free flag
329 *
330 * Allocated but unusable (ie eaten) memory pages should be marked
331 * so that swsusp_free() can release them
332 */
333
334static inline void *alloc_image_page(gfp_t gfp_mask, int safe_needed)
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800335{
Rafael J. Wysocki054bd4c2005-11-08 21:34:39 -0800336 void *res;
337
338 if (safe_needed)
339 do {
340 res = (void *)get_zeroed_page(gfp_mask);
Rafael J. Wysocki72a97e02006-01-06 00:13:46 -0800341 if (res && PageNosaveFree(virt_to_page(res))) {
Rafael J. Wysocki054bd4c2005-11-08 21:34:39 -0800342 /* This is for swsusp_free() */
343 SetPageNosave(virt_to_page(res));
Rafael J. Wysocki72a97e02006-01-06 00:13:46 -0800344 ((struct eaten_page *)res)->next = eaten_pages;
345 eaten_pages = res;
346 }
Rafael J. Wysocki054bd4c2005-11-08 21:34:39 -0800347 } while (res && PageNosaveFree(virt_to_page(res)));
348 else
349 res = (void *)get_zeroed_page(gfp_mask);
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800350 if (res) {
351 SetPageNosave(virt_to_page(res));
352 SetPageNosaveFree(virt_to_page(res));
353 }
354 return res;
355}
356
Rafael J. Wysocki054bd4c2005-11-08 21:34:39 -0800357unsigned long get_safe_page(gfp_t gfp_mask)
358{
359 return (unsigned long)alloc_image_page(gfp_mask, 1);
360}
361
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800362/**
363 * alloc_pagedir - Allocate the page directory.
364 *
365 * First, determine exactly how many pages we need and
366 * allocate them.
367 *
368 * We arrange the pages in a chain: each page is an array of PBES_PER_PAGE
369 * struct pbe elements (pbes) and the last element in the page points
370 * to the next page.
371 *
372 * On each page we set up a list of struct_pbe elements.
373 */
374
Rafael J. Wysocki054bd4c2005-11-08 21:34:39 -0800375struct pbe *alloc_pagedir(unsigned int nr_pages, gfp_t gfp_mask, int safe_needed)
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800376{
Pavel Machekdc19d502005-11-07 00:58:40 -0800377 unsigned int num;
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800378 struct pbe *pblist, *pbe;
379
380 if (!nr_pages)
381 return NULL;
382
Rafael J. Wysocki054bd4c2005-11-08 21:34:39 -0800383 pblist = alloc_image_page(gfp_mask, safe_needed);
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800384 /* FIXME: rewrite this ugly loop */
385 for (pbe = pblist, num = PBES_PER_PAGE; pbe && num < nr_pages;
386 pbe = pbe->next, num += PBES_PER_PAGE) {
387 pbe += PB_PAGE_SKIP;
Rafael J. Wysocki054bd4c2005-11-08 21:34:39 -0800388 pbe->next = alloc_image_page(gfp_mask, safe_needed);
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800389 }
390 if (!pbe) { /* get_zeroed_page() failed */
391 free_pagedir(pblist);
392 pblist = NULL;
Rafael J. Wysocki7088a5c2006-01-06 00:13:05 -0800393 } else
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -0800394 create_pbe_list(pblist, nr_pages);
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800395 return pblist;
396}
397
398/**
399 * Free pages we allocated for suspend. Suspend pages are alocated
400 * before atomic copy, so we need to free them after resume.
401 */
402
403void swsusp_free(void)
404{
405 struct zone *zone;
406 unsigned long zone_pfn;
407
408 for_each_zone(zone) {
409 for (zone_pfn = 0; zone_pfn < zone->spanned_pages; ++zone_pfn)
410 if (pfn_valid(zone_pfn + zone->zone_start_pfn)) {
Pavel Machekdc19d502005-11-07 00:58:40 -0800411 struct page *page;
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800412 page = pfn_to_page(zone_pfn + zone->zone_start_pfn);
413 if (PageNosave(page) && PageNosaveFree(page)) {
414 ClearPageNosave(page);
415 ClearPageNosaveFree(page);
416 free_page((long) page_address(page));
417 }
418 }
419 }
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800420 nr_copy_pages = 0;
421 nr_meta_pages = 0;
422 pagedir_nosave = NULL;
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -0800423 buffer = NULL;
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800424}
425
426
427/**
428 * enough_free_mem - Make sure we enough free memory to snapshot.
429 *
430 * Returns TRUE or FALSE after checking the number of available
431 * free pages.
432 */
433
Pavel Machekdc19d502005-11-07 00:58:40 -0800434static int enough_free_mem(unsigned int nr_pages)
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800435{
Rafael J. Wysockie5e2fa72006-01-06 00:14:20 -0800436 struct zone *zone;
437 unsigned int n = 0;
438
439 for_each_zone (zone)
440 if (!is_highmem(zone))
441 n += zone->free_pages;
442 pr_debug("swsusp: available memory: %u pages\n", n);
443 return n > (nr_pages + PAGES_FOR_IO +
Rafael J. Wysockia0f49652005-10-30 14:59:57 -0800444 (nr_pages + PBES_PER_PAGE - 1) / PBES_PER_PAGE);
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800445}
446
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800447static int alloc_data_pages(struct pbe *pblist, gfp_t gfp_mask, int safe_needed)
Rafael J. Wysocki054bd4c2005-11-08 21:34:39 -0800448{
449 struct pbe *p;
450
451 for_each_pbe (p, pblist) {
452 p->address = (unsigned long)alloc_image_page(gfp_mask, safe_needed);
453 if (!p->address)
454 return -ENOMEM;
455 }
456 return 0;
457}
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800458
Pavel Machekdc19d502005-11-07 00:58:40 -0800459static struct pbe *swsusp_alloc(unsigned int nr_pages)
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800460{
Rafael J. Wysocki054bd4c2005-11-08 21:34:39 -0800461 struct pbe *pblist;
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800462
Rafael J. Wysocki054bd4c2005-11-08 21:34:39 -0800463 if (!(pblist = alloc_pagedir(nr_pages, GFP_ATOMIC | __GFP_COLD, 0))) {
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800464 printk(KERN_ERR "suspend: Allocating pagedir failed.\n");
Rafael J. Wysockia0f49652005-10-30 14:59:57 -0800465 return NULL;
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800466 }
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800467
Rafael J. Wysocki054bd4c2005-11-08 21:34:39 -0800468 if (alloc_data_pages(pblist, GFP_ATOMIC | __GFP_COLD, 0)) {
469 printk(KERN_ERR "suspend: Allocating image pages failed.\n");
470 swsusp_free();
471 return NULL;
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800472 }
473
Rafael J. Wysockia0f49652005-10-30 14:59:57 -0800474 return pblist;
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800475}
476
Rafael J. Wysocki2e32a432005-10-30 15:00:00 -0800477asmlinkage int swsusp_save(void)
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800478{
Pavel Machekdc19d502005-11-07 00:58:40 -0800479 unsigned int nr_pages;
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800480
481 pr_debug("swsusp: critical section: \n");
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800482
483 drain_local_pages();
Rafael J. Wysockia0f49652005-10-30 14:59:57 -0800484 nr_pages = count_data_pages();
485 printk("swsusp: Need to copy %u pages\n", nr_pages);
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800486
487 pr_debug("swsusp: pages needed: %u + %lu + %u, free: %u\n",
Rafael J. Wysockia0f49652005-10-30 14:59:57 -0800488 nr_pages,
489 (nr_pages + PBES_PER_PAGE - 1) / PBES_PER_PAGE,
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800490 PAGES_FOR_IO, nr_free_pages());
491
Rafael J. Wysockia0f49652005-10-30 14:59:57 -0800492 if (!enough_free_mem(nr_pages)) {
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800493 printk(KERN_ERR "swsusp: Not enough free memory\n");
494 return -ENOMEM;
495 }
496
Rafael J. Wysockia0f49652005-10-30 14:59:57 -0800497 pagedir_nosave = swsusp_alloc(nr_pages);
498 if (!pagedir_nosave)
499 return -ENOMEM;
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800500
501 /* During allocating of suspend pagedir, new cold pages may appear.
502 * Kill them.
503 */
504 drain_local_pages();
Rafael J. Wysockia0f49652005-10-30 14:59:57 -0800505 copy_data_pages(pagedir_nosave);
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800506
507 /*
508 * End of critical section. From now on, we can write to memory,
509 * but we should not touch disk. This specially means we must _not_
510 * touch swap space! Except we must write out our image of course.
511 */
512
Rafael J. Wysockia0f49652005-10-30 14:59:57 -0800513 nr_copy_pages = nr_pages;
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800514 nr_meta_pages = (nr_pages * sizeof(long) + PAGE_SIZE - 1) >> PAGE_SHIFT;
Rafael J. Wysockia0f49652005-10-30 14:59:57 -0800515
516 printk("swsusp: critical section/: done (%d pages copied)\n", nr_pages);
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800517 return 0;
518}
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800519
520static void init_header(struct swsusp_info *info)
521{
522 memset(info, 0, sizeof(struct swsusp_info));
523 info->version_code = LINUX_VERSION_CODE;
524 info->num_physpages = num_physpages;
525 memcpy(&info->uts, &system_utsname, sizeof(system_utsname));
526 info->cpus = num_online_cpus();
527 info->image_pages = nr_copy_pages;
528 info->pages = nr_copy_pages + nr_meta_pages + 1;
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -0800529 info->size = info->pages;
530 info->size <<= PAGE_SHIFT;
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800531}
532
533/**
534 * pack_orig_addresses - the .orig_address fields of the PBEs from the
535 * list starting at @pbe are stored in the array @buf[] (1 page)
536 */
537
538static inline struct pbe *pack_orig_addresses(unsigned long *buf, struct pbe *pbe)
539{
540 int j;
541
542 for (j = 0; j < PAGE_SIZE / sizeof(long) && pbe; j++) {
543 buf[j] = pbe->orig_address;
544 pbe = pbe->next;
545 }
546 if (!pbe)
547 for (; j < PAGE_SIZE / sizeof(long); j++)
548 buf[j] = 0;
549 return pbe;
550}
551
552/**
553 * snapshot_read_next - used for reading the system memory snapshot.
554 *
555 * On the first call to it @handle should point to a zeroed
556 * snapshot_handle structure. The structure gets updated and a pointer
557 * to it should be passed to this function every next time.
558 *
559 * The @count parameter should contain the number of bytes the caller
560 * wants to read from the snapshot. It must not be zero.
561 *
562 * On success the function returns a positive number. Then, the caller
563 * is allowed to read up to the returned number of bytes from the memory
564 * location computed by the data_of() macro. The number returned
565 * may be smaller than @count, but this only happens if the read would
566 * cross a page boundary otherwise.
567 *
568 * The function returns 0 to indicate the end of data stream condition,
569 * and a negative number is returned on error. In such cases the
570 * structure pointed to by @handle is not updated and should not be used
571 * any more.
572 */
573
574int snapshot_read_next(struct snapshot_handle *handle, size_t count)
575{
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800576 if (handle->page > nr_meta_pages + nr_copy_pages)
577 return 0;
578 if (!buffer) {
579 /* This makes the buffer be freed by swsusp_free() */
580 buffer = alloc_image_page(GFP_ATOMIC, 0);
581 if (!buffer)
582 return -ENOMEM;
583 }
584 if (!handle->offset) {
585 init_header((struct swsusp_info *)buffer);
586 handle->buffer = buffer;
587 handle->pbe = pagedir_nosave;
588 }
589 if (handle->prev < handle->page) {
590 if (handle->page <= nr_meta_pages) {
591 handle->pbe = pack_orig_addresses(buffer, handle->pbe);
592 if (!handle->pbe)
593 handle->pbe = pagedir_nosave;
594 } else {
595 handle->buffer = (void *)handle->pbe->address;
596 handle->pbe = handle->pbe->next;
597 }
598 handle->prev = handle->page;
599 }
600 handle->buf_offset = handle->page_offset;
601 if (handle->page_offset + count >= PAGE_SIZE) {
602 count = PAGE_SIZE - handle->page_offset;
603 handle->page_offset = 0;
604 handle->page++;
605 } else {
606 handle->page_offset += count;
607 }
608 handle->offset += count;
609 return count;
610}
611
612/**
613 * mark_unsafe_pages - mark the pages that cannot be used for storing
614 * the image during resume, because they conflict with the pages that
615 * had been used before suspend
616 */
617
618static int mark_unsafe_pages(struct pbe *pblist)
619{
620 struct zone *zone;
621 unsigned long zone_pfn;
622 struct pbe *p;
623
624 if (!pblist) /* a sanity check */
625 return -EINVAL;
626
627 /* Clear page flags */
628 for_each_zone (zone) {
629 for (zone_pfn = 0; zone_pfn < zone->spanned_pages; ++zone_pfn)
630 if (pfn_valid(zone_pfn + zone->zone_start_pfn))
631 ClearPageNosaveFree(pfn_to_page(zone_pfn +
632 zone->zone_start_pfn));
633 }
634
635 /* Mark orig addresses */
636 for_each_pbe (p, pblist) {
637 if (virt_addr_valid(p->orig_address))
638 SetPageNosaveFree(virt_to_page(p->orig_address));
639 else
640 return -EFAULT;
641 }
642
643 return 0;
644}
645
646static void copy_page_backup_list(struct pbe *dst, struct pbe *src)
647{
648 /* We assume both lists contain the same number of elements */
649 while (src) {
650 dst->orig_address = src->orig_address;
651 dst = dst->next;
652 src = src->next;
653 }
654}
655
656static int check_header(struct swsusp_info *info)
657{
658 char *reason = NULL;
659
660 if (info->version_code != LINUX_VERSION_CODE)
661 reason = "kernel version";
662 if (info->num_physpages != num_physpages)
663 reason = "memory size";
664 if (strcmp(info->uts.sysname,system_utsname.sysname))
665 reason = "system type";
666 if (strcmp(info->uts.release,system_utsname.release))
667 reason = "kernel release";
668 if (strcmp(info->uts.version,system_utsname.version))
669 reason = "version";
670 if (strcmp(info->uts.machine,system_utsname.machine))
671 reason = "machine";
672 if (reason) {
673 printk(KERN_ERR "swsusp: Resume mismatch: %s\n", reason);
674 return -EPERM;
675 }
676 return 0;
677}
678
679/**
680 * load header - check the image header and copy data from it
681 */
682
683static int load_header(struct snapshot_handle *handle,
684 struct swsusp_info *info)
685{
686 int error;
687 struct pbe *pblist;
688
689 error = check_header(info);
690 if (!error) {
691 pblist = alloc_pagedir(info->image_pages, GFP_ATOMIC, 0);
692 if (!pblist)
693 return -ENOMEM;
694 pagedir_nosave = pblist;
695 handle->pbe = pblist;
696 nr_copy_pages = info->image_pages;
697 nr_meta_pages = info->pages - info->image_pages - 1;
698 }
699 return error;
700}
701
702/**
703 * unpack_orig_addresses - copy the elements of @buf[] (1 page) to
704 * the PBEs in the list starting at @pbe
705 */
706
707static inline struct pbe *unpack_orig_addresses(unsigned long *buf,
708 struct pbe *pbe)
709{
710 int j;
711
712 for (j = 0; j < PAGE_SIZE / sizeof(long) && pbe; j++) {
713 pbe->orig_address = buf[j];
714 pbe = pbe->next;
715 }
716 return pbe;
717}
718
719/**
720 * create_image - use metadata contained in the PBE list
721 * pointed to by pagedir_nosave to mark the pages that will
722 * be overwritten in the process of restoring the system
723 * memory state from the image and allocate memory for
724 * the image avoiding these pages
725 */
726
727static int create_image(struct snapshot_handle *handle)
728{
729 int error = 0;
730 struct pbe *p, *pblist;
731
732 p = pagedir_nosave;
733 error = mark_unsafe_pages(p);
734 if (!error) {
735 pblist = alloc_pagedir(nr_copy_pages, GFP_ATOMIC, 1);
736 if (pblist)
737 copy_page_backup_list(pblist, p);
738 free_pagedir(p);
739 if (!pblist)
740 error = -ENOMEM;
741 }
742 if (!error)
743 error = alloc_data_pages(pblist, GFP_ATOMIC, 1);
744 if (!error) {
745 release_eaten_pages();
746 pagedir_nosave = pblist;
747 } else {
748 pagedir_nosave = NULL;
749 handle->pbe = NULL;
750 nr_copy_pages = 0;
751 nr_meta_pages = 0;
752 }
753 return error;
754}
755
756/**
757 * snapshot_write_next - used for writing the system memory snapshot.
758 *
759 * On the first call to it @handle should point to a zeroed
760 * snapshot_handle structure. The structure gets updated and a pointer
761 * to it should be passed to this function every next time.
762 *
763 * The @count parameter should contain the number of bytes the caller
764 * wants to write to the image. It must not be zero.
765 *
766 * On success the function returns a positive number. Then, the caller
767 * is allowed to write up to the returned number of bytes to the memory
768 * location computed by the data_of() macro. The number returned
769 * may be smaller than @count, but this only happens if the write would
770 * cross a page boundary otherwise.
771 *
772 * The function returns 0 to indicate the "end of file" condition,
773 * and a negative number is returned on error. In such cases the
774 * structure pointed to by @handle is not updated and should not be used
775 * any more.
776 */
777
778int snapshot_write_next(struct snapshot_handle *handle, size_t count)
779{
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800780 int error = 0;
781
782 if (handle->prev && handle->page > nr_meta_pages + nr_copy_pages)
783 return 0;
784 if (!buffer) {
785 /* This makes the buffer be freed by swsusp_free() */
786 buffer = alloc_image_page(GFP_ATOMIC, 0);
787 if (!buffer)
788 return -ENOMEM;
789 }
790 if (!handle->offset)
791 handle->buffer = buffer;
792 if (handle->prev < handle->page) {
793 if (!handle->prev) {
794 error = load_header(handle, (struct swsusp_info *)buffer);
795 if (error)
796 return error;
797 } else if (handle->prev <= nr_meta_pages) {
798 handle->pbe = unpack_orig_addresses(buffer, handle->pbe);
799 if (!handle->pbe) {
800 error = create_image(handle);
801 if (error)
802 return error;
803 handle->pbe = pagedir_nosave;
804 handle->buffer = (void *)handle->pbe->address;
805 }
806 } else {
807 handle->pbe = handle->pbe->next;
808 handle->buffer = (void *)handle->pbe->address;
809 }
810 handle->prev = handle->page;
811 }
812 handle->buf_offset = handle->page_offset;
813 if (handle->page_offset + count >= PAGE_SIZE) {
814 count = PAGE_SIZE - handle->page_offset;
815 handle->page_offset = 0;
816 handle->page++;
817 } else {
818 handle->page_offset += count;
819 }
820 handle->offset += count;
821 return count;
822}
823
824int snapshot_image_loaded(struct snapshot_handle *handle)
825{
826 return !(!handle->pbe || handle->pbe->next || !nr_copy_pages ||
827 handle->page <= nr_meta_pages + nr_copy_pages);
828}