blob: cc349437fb727db782945c4fbd04c5b0fca85ab0 [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. Wysocki7088a5c2006-01-06 00:13:05 -080040
Rafael J. Wysocki25761b62005-10-30 14:59:56 -080041#ifdef CONFIG_HIGHMEM
Rafael J. Wysocki72a97e02006-01-06 00:13:46 -080042unsigned int count_highmem_pages(void)
43{
44 struct zone *zone;
45 unsigned long zone_pfn;
46 unsigned int n = 0;
47
48 for_each_zone (zone)
49 if (is_highmem(zone)) {
50 mark_free_pages(zone);
51 for (zone_pfn = 0; zone_pfn < zone->spanned_pages; zone_pfn++) {
52 struct page *page;
53 unsigned long pfn = zone_pfn + zone->zone_start_pfn;
54 if (!pfn_valid(pfn))
55 continue;
56 page = pfn_to_page(pfn);
57 if (PageReserved(page))
58 continue;
59 if (PageNosaveFree(page))
60 continue;
61 n++;
62 }
63 }
64 return n;
65}
66
Rafael J. Wysocki25761b62005-10-30 14:59:56 -080067struct highmem_page {
68 char *data;
69 struct page *page;
70 struct highmem_page *next;
71};
72
73static struct highmem_page *highmem_copy;
74
75static int save_highmem_zone(struct zone *zone)
76{
77 unsigned long zone_pfn;
78 mark_free_pages(zone);
79 for (zone_pfn = 0; zone_pfn < zone->spanned_pages; ++zone_pfn) {
80 struct page *page;
81 struct highmem_page *save;
82 void *kaddr;
83 unsigned long pfn = zone_pfn + zone->zone_start_pfn;
84
85 if (!(pfn%1000))
86 printk(".");
87 if (!pfn_valid(pfn))
88 continue;
89 page = pfn_to_page(pfn);
90 /*
91 * This condition results from rvmalloc() sans vmalloc_32()
92 * and architectural memory reservations. This should be
93 * corrected eventually when the cases giving rise to this
94 * are better understood.
95 */
Andrew Mortonc8adb492006-02-15 15:17:42 -080096 if (PageReserved(page))
Rafael J. Wysocki25761b62005-10-30 14:59:56 -080097 continue;
Rafael J. Wysocki25761b62005-10-30 14:59:56 -080098 BUG_ON(PageNosave(page));
99 if (PageNosaveFree(page))
100 continue;
101 save = kmalloc(sizeof(struct highmem_page), GFP_ATOMIC);
102 if (!save)
103 return -ENOMEM;
104 save->next = highmem_copy;
105 save->page = page;
106 save->data = (void *) get_zeroed_page(GFP_ATOMIC);
107 if (!save->data) {
108 kfree(save);
109 return -ENOMEM;
110 }
111 kaddr = kmap_atomic(page, KM_USER0);
112 memcpy(save->data, kaddr, PAGE_SIZE);
113 kunmap_atomic(kaddr, KM_USER0);
114 highmem_copy = save;
115 }
116 return 0;
117}
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800118
Rafael J. Wysocki0fbeb5a2005-11-08 21:34:41 -0800119int save_highmem(void)
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800120{
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800121 struct zone *zone;
122 int res = 0;
123
124 pr_debug("swsusp: Saving Highmem\n");
125 for_each_zone (zone) {
126 if (is_highmem(zone))
127 res = save_highmem_zone(zone);
128 if (res)
129 return res;
130 }
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800131 return 0;
132}
133
134int restore_highmem(void)
135{
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800136 printk("swsusp: Restoring Highmem\n");
137 while (highmem_copy) {
138 struct highmem_page *save = highmem_copy;
139 void *kaddr;
140 highmem_copy = save->next;
141
142 kaddr = kmap_atomic(save->page, KM_USER0);
143 memcpy(kaddr, save->data, PAGE_SIZE);
144 kunmap_atomic(kaddr, KM_USER0);
145 free_page((long) save->data);
146 kfree(save);
147 }
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800148 return 0;
149}
Rafael J. Wysocki0fbeb5a2005-11-08 21:34:41 -0800150#endif
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800151
152static int pfn_is_nosave(unsigned long pfn)
153{
154 unsigned long nosave_begin_pfn = __pa(&__nosave_begin) >> PAGE_SHIFT;
155 unsigned long nosave_end_pfn = PAGE_ALIGN(__pa(&__nosave_end)) >> PAGE_SHIFT;
156 return (pfn >= nosave_begin_pfn) && (pfn < nosave_end_pfn);
157}
158
159/**
160 * saveable - Determine whether a page should be cloned or not.
161 * @pfn: The page
162 *
163 * We save a page if it's Reserved, and not in the range of pages
164 * statically defined as 'unsaveable', or if it isn't reserved, and
165 * isn't part of a free chunk of pages.
166 */
167
Pavel Machekde491862005-10-30 14:59:59 -0800168static int saveable(struct zone *zone, unsigned long *zone_pfn)
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800169{
170 unsigned long pfn = *zone_pfn + zone->zone_start_pfn;
Pavel Machekde491862005-10-30 14:59:59 -0800171 struct page *page;
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800172
173 if (!pfn_valid(pfn))
174 return 0;
175
176 page = pfn_to_page(pfn);
177 BUG_ON(PageReserved(page) && PageNosave(page));
178 if (PageNosave(page))
179 return 0;
Rafael J. Wysocki72a97e02006-01-06 00:13:46 -0800180 if (PageReserved(page) && pfn_is_nosave(pfn))
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800181 return 0;
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800182 if (PageNosaveFree(page))
183 return 0;
184
185 return 1;
186}
187
Rafael J. Wysocki72a97e02006-01-06 00:13:46 -0800188unsigned int count_data_pages(void)
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800189{
190 struct zone *zone;
191 unsigned long zone_pfn;
Pavel Machekdc19d502005-11-07 00:58:40 -0800192 unsigned int n = 0;
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800193
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800194 for_each_zone (zone) {
195 if (is_highmem(zone))
196 continue;
197 mark_free_pages(zone);
198 for (zone_pfn = 0; zone_pfn < zone->spanned_pages; ++zone_pfn)
Rafael J. Wysockia0f49652005-10-30 14:59:57 -0800199 n += saveable(zone, &zone_pfn);
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800200 }
Rafael J. Wysockia0f49652005-10-30 14:59:57 -0800201 return n;
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800202}
203
Rafael J. Wysockia0f49652005-10-30 14:59:57 -0800204static void copy_data_pages(struct pbe *pblist)
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800205{
206 struct zone *zone;
207 unsigned long zone_pfn;
Rafael J. Wysockia0f49652005-10-30 14:59:57 -0800208 struct pbe *pbe, *p;
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800209
Rafael J. Wysockia0f49652005-10-30 14:59:57 -0800210 pbe = pblist;
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800211 for_each_zone (zone) {
212 if (is_highmem(zone))
213 continue;
214 mark_free_pages(zone);
215 /* This is necessary for swsusp_free() */
Rafael J. Wysockia0f49652005-10-30 14:59:57 -0800216 for_each_pb_page (p, pblist)
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800217 SetPageNosaveFree(virt_to_page(p));
Rafael J. Wysockia0f49652005-10-30 14:59:57 -0800218 for_each_pbe (p, pblist)
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800219 SetPageNosaveFree(virt_to_page(p->address));
220 for (zone_pfn = 0; zone_pfn < zone->spanned_pages; ++zone_pfn) {
221 if (saveable(zone, &zone_pfn)) {
Pavel Machekde491862005-10-30 14:59:59 -0800222 struct page *page;
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800223 page = pfn_to_page(zone_pfn + zone->zone_start_pfn);
224 BUG_ON(!pbe);
225 pbe->orig_address = (unsigned long)page_address(page);
226 /* copy_page is not usable for copying task structs. */
227 memcpy((void *)pbe->address, (void *)pbe->orig_address, PAGE_SIZE);
228 pbe = pbe->next;
229 }
230 }
231 }
232 BUG_ON(pbe);
233}
234
235
236/**
237 * free_pagedir - free pages allocated with alloc_pagedir()
238 */
239
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800240static void free_pagedir(struct pbe *pblist)
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800241{
242 struct pbe *pbe;
243
244 while (pblist) {
245 pbe = (pblist + PB_PAGE_SKIP)->next;
246 ClearPageNosave(virt_to_page(pblist));
247 ClearPageNosaveFree(virt_to_page(pblist));
248 free_page((unsigned long)pblist);
249 pblist = pbe;
250 }
251}
252
253/**
254 * fill_pb_page - Create a list of PBEs on a given memory page
255 */
256
257static inline void fill_pb_page(struct pbe *pbpage)
258{
259 struct pbe *p;
260
261 p = pbpage;
262 pbpage += PB_PAGE_SKIP;
263 do
264 p->next = p + 1;
265 while (++p < pbpage);
266}
267
268/**
269 * create_pbe_list - Create a list of PBEs on top of a given chain
270 * of memory pages allocated with alloc_pagedir()
271 */
272
Rafael J. Wysocki7088a5c2006-01-06 00:13:05 -0800273static inline void create_pbe_list(struct pbe *pblist, unsigned int nr_pages)
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800274{
275 struct pbe *pbpage, *p;
Pavel Machekdc19d502005-11-07 00:58:40 -0800276 unsigned int num = PBES_PER_PAGE;
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800277
278 for_each_pb_page (pbpage, pblist) {
279 if (num >= nr_pages)
280 break;
281
282 fill_pb_page(pbpage);
283 num += PBES_PER_PAGE;
284 }
285 if (pbpage) {
286 for (num -= PBES_PER_PAGE - 1, p = pbpage; num < nr_pages; p++, num++)
287 p->next = p + 1;
288 p->next = NULL;
289 }
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800290}
291
Rafael J. Wysocki054bd4c2005-11-08 21:34:39 -0800292/**
Rafael J. Wysocki72a97e02006-01-06 00:13:46 -0800293 * On resume it is necessary to trace and eventually free the unsafe
294 * pages that have been allocated, because they are needed for I/O
295 * (on x86-64 we likely will "eat" these pages once again while
296 * creating the temporary page translation tables)
297 */
298
299struct eaten_page {
300 struct eaten_page *next;
301 char padding[PAGE_SIZE - sizeof(void *)];
302};
303
304static struct eaten_page *eaten_pages = NULL;
305
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800306static void release_eaten_pages(void)
Rafael J. Wysocki72a97e02006-01-06 00:13:46 -0800307{
308 struct eaten_page *p, *q;
309
310 p = eaten_pages;
311 while (p) {
312 q = p->next;
313 /* We don't want swsusp_free() to free this page again */
314 ClearPageNosave(virt_to_page(p));
315 free_page((unsigned long)p);
316 p = q;
317 }
318 eaten_pages = NULL;
319}
320
321/**
Rafael J. Wysocki054bd4c2005-11-08 21:34:39 -0800322 * @safe_needed - on resume, for storing the PBE list and the image,
323 * we can only use memory pages that do not conflict with the pages
324 * which had been used before suspend.
325 *
326 * The unsafe pages are marked with the PG_nosave_free flag
327 *
328 * Allocated but unusable (ie eaten) memory pages should be marked
329 * so that swsusp_free() can release them
330 */
331
332static inline void *alloc_image_page(gfp_t gfp_mask, int safe_needed)
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800333{
Rafael J. Wysocki054bd4c2005-11-08 21:34:39 -0800334 void *res;
335
336 if (safe_needed)
337 do {
338 res = (void *)get_zeroed_page(gfp_mask);
Rafael J. Wysocki72a97e02006-01-06 00:13:46 -0800339 if (res && PageNosaveFree(virt_to_page(res))) {
Rafael J. Wysocki054bd4c2005-11-08 21:34:39 -0800340 /* This is for swsusp_free() */
341 SetPageNosave(virt_to_page(res));
Rafael J. Wysocki72a97e02006-01-06 00:13:46 -0800342 ((struct eaten_page *)res)->next = eaten_pages;
343 eaten_pages = res;
344 }
Rafael J. Wysocki054bd4c2005-11-08 21:34:39 -0800345 } while (res && PageNosaveFree(virt_to_page(res)));
346 else
347 res = (void *)get_zeroed_page(gfp_mask);
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800348 if (res) {
349 SetPageNosave(virt_to_page(res));
350 SetPageNosaveFree(virt_to_page(res));
351 }
352 return res;
353}
354
Rafael J. Wysocki054bd4c2005-11-08 21:34:39 -0800355unsigned long get_safe_page(gfp_t gfp_mask)
356{
357 return (unsigned long)alloc_image_page(gfp_mask, 1);
358}
359
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800360/**
361 * alloc_pagedir - Allocate the page directory.
362 *
363 * First, determine exactly how many pages we need and
364 * allocate them.
365 *
366 * We arrange the pages in a chain: each page is an array of PBES_PER_PAGE
367 * struct pbe elements (pbes) and the last element in the page points
368 * to the next page.
369 *
370 * On each page we set up a list of struct_pbe elements.
371 */
372
Rafael J. Wysocki054bd4c2005-11-08 21:34:39 -0800373struct pbe *alloc_pagedir(unsigned int nr_pages, gfp_t gfp_mask, int safe_needed)
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800374{
Pavel Machekdc19d502005-11-07 00:58:40 -0800375 unsigned int num;
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800376 struct pbe *pblist, *pbe;
377
378 if (!nr_pages)
379 return NULL;
380
Rafael J. Wysocki054bd4c2005-11-08 21:34:39 -0800381 pblist = alloc_image_page(gfp_mask, safe_needed);
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800382 /* FIXME: rewrite this ugly loop */
383 for (pbe = pblist, num = PBES_PER_PAGE; pbe && num < nr_pages;
384 pbe = pbe->next, num += PBES_PER_PAGE) {
385 pbe += PB_PAGE_SKIP;
Rafael J. Wysocki054bd4c2005-11-08 21:34:39 -0800386 pbe->next = alloc_image_page(gfp_mask, safe_needed);
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800387 }
388 if (!pbe) { /* get_zeroed_page() failed */
389 free_pagedir(pblist);
390 pblist = NULL;
Rafael J. Wysocki7088a5c2006-01-06 00:13:05 -0800391 } else
392 create_pbe_list(pblist, nr_pages);
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800393 return pblist;
394}
395
396/**
397 * Free pages we allocated for suspend. Suspend pages are alocated
398 * before atomic copy, so we need to free them after resume.
399 */
400
401void swsusp_free(void)
402{
403 struct zone *zone;
404 unsigned long zone_pfn;
405
406 for_each_zone(zone) {
407 for (zone_pfn = 0; zone_pfn < zone->spanned_pages; ++zone_pfn)
408 if (pfn_valid(zone_pfn + zone->zone_start_pfn)) {
Pavel Machekdc19d502005-11-07 00:58:40 -0800409 struct page *page;
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800410 page = pfn_to_page(zone_pfn + zone->zone_start_pfn);
411 if (PageNosave(page) && PageNosaveFree(page)) {
412 ClearPageNosave(page);
413 ClearPageNosaveFree(page);
414 free_page((long) page_address(page));
415 }
416 }
417 }
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800418 nr_copy_pages = 0;
419 nr_meta_pages = 0;
420 pagedir_nosave = NULL;
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800421}
422
423
424/**
425 * enough_free_mem - Make sure we enough free memory to snapshot.
426 *
427 * Returns TRUE or FALSE after checking the number of available
428 * free pages.
429 */
430
Pavel Machekdc19d502005-11-07 00:58:40 -0800431static int enough_free_mem(unsigned int nr_pages)
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800432{
Rafael J. Wysockie5e2fa72006-01-06 00:14:20 -0800433 struct zone *zone;
434 unsigned int n = 0;
435
436 for_each_zone (zone)
437 if (!is_highmem(zone))
438 n += zone->free_pages;
439 pr_debug("swsusp: available memory: %u pages\n", n);
440 return n > (nr_pages + PAGES_FOR_IO +
Rafael J. Wysockia0f49652005-10-30 14:59:57 -0800441 (nr_pages + PBES_PER_PAGE - 1) / PBES_PER_PAGE);
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800442}
443
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800444static int alloc_data_pages(struct pbe *pblist, gfp_t gfp_mask, int safe_needed)
Rafael J. Wysocki054bd4c2005-11-08 21:34:39 -0800445{
446 struct pbe *p;
447
448 for_each_pbe (p, pblist) {
449 p->address = (unsigned long)alloc_image_page(gfp_mask, safe_needed);
450 if (!p->address)
451 return -ENOMEM;
452 }
453 return 0;
454}
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800455
Pavel Machekdc19d502005-11-07 00:58:40 -0800456static struct pbe *swsusp_alloc(unsigned int nr_pages)
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800457{
Rafael J. Wysocki054bd4c2005-11-08 21:34:39 -0800458 struct pbe *pblist;
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800459
Rafael J. Wysocki054bd4c2005-11-08 21:34:39 -0800460 if (!(pblist = alloc_pagedir(nr_pages, GFP_ATOMIC | __GFP_COLD, 0))) {
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800461 printk(KERN_ERR "suspend: Allocating pagedir failed.\n");
Rafael J. Wysockia0f49652005-10-30 14:59:57 -0800462 return NULL;
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800463 }
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800464
Rafael J. Wysocki054bd4c2005-11-08 21:34:39 -0800465 if (alloc_data_pages(pblist, GFP_ATOMIC | __GFP_COLD, 0)) {
466 printk(KERN_ERR "suspend: Allocating image pages failed.\n");
467 swsusp_free();
468 return NULL;
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800469 }
470
Rafael J. Wysockia0f49652005-10-30 14:59:57 -0800471 return pblist;
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800472}
473
Rafael J. Wysocki2e32a432005-10-30 15:00:00 -0800474asmlinkage int swsusp_save(void)
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800475{
Pavel Machekdc19d502005-11-07 00:58:40 -0800476 unsigned int nr_pages;
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800477
478 pr_debug("swsusp: critical section: \n");
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800479
480 drain_local_pages();
Rafael J. Wysockia0f49652005-10-30 14:59:57 -0800481 nr_pages = count_data_pages();
482 printk("swsusp: Need to copy %u pages\n", nr_pages);
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800483
484 pr_debug("swsusp: pages needed: %u + %lu + %u, free: %u\n",
Rafael J. Wysockia0f49652005-10-30 14:59:57 -0800485 nr_pages,
486 (nr_pages + PBES_PER_PAGE - 1) / PBES_PER_PAGE,
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800487 PAGES_FOR_IO, nr_free_pages());
488
Rafael J. Wysockia0f49652005-10-30 14:59:57 -0800489 if (!enough_free_mem(nr_pages)) {
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800490 printk(KERN_ERR "swsusp: Not enough free memory\n");
491 return -ENOMEM;
492 }
493
Rafael J. Wysockia0f49652005-10-30 14:59:57 -0800494 pagedir_nosave = swsusp_alloc(nr_pages);
495 if (!pagedir_nosave)
496 return -ENOMEM;
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800497
498 /* During allocating of suspend pagedir, new cold pages may appear.
499 * Kill them.
500 */
501 drain_local_pages();
Rafael J. Wysockia0f49652005-10-30 14:59:57 -0800502 copy_data_pages(pagedir_nosave);
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800503
504 /*
505 * End of critical section. From now on, we can write to memory,
506 * but we should not touch disk. This specially means we must _not_
507 * touch swap space! Except we must write out our image of course.
508 */
509
Rafael J. Wysockia0f49652005-10-30 14:59:57 -0800510 nr_copy_pages = nr_pages;
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800511 nr_meta_pages = (nr_pages * sizeof(long) + PAGE_SIZE - 1) >> PAGE_SHIFT;
Rafael J. Wysockia0f49652005-10-30 14:59:57 -0800512
513 printk("swsusp: critical section/: done (%d pages copied)\n", nr_pages);
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800514 return 0;
515}
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800516
517static void init_header(struct swsusp_info *info)
518{
519 memset(info, 0, sizeof(struct swsusp_info));
520 info->version_code = LINUX_VERSION_CODE;
521 info->num_physpages = num_physpages;
522 memcpy(&info->uts, &system_utsname, sizeof(system_utsname));
523 info->cpus = num_online_cpus();
524 info->image_pages = nr_copy_pages;
525 info->pages = nr_copy_pages + nr_meta_pages + 1;
526}
527
528/**
529 * pack_orig_addresses - the .orig_address fields of the PBEs from the
530 * list starting at @pbe are stored in the array @buf[] (1 page)
531 */
532
533static inline struct pbe *pack_orig_addresses(unsigned long *buf, struct pbe *pbe)
534{
535 int j;
536
537 for (j = 0; j < PAGE_SIZE / sizeof(long) && pbe; j++) {
538 buf[j] = pbe->orig_address;
539 pbe = pbe->next;
540 }
541 if (!pbe)
542 for (; j < PAGE_SIZE / sizeof(long); j++)
543 buf[j] = 0;
544 return pbe;
545}
546
547/**
548 * snapshot_read_next - used for reading the system memory snapshot.
549 *
550 * On the first call to it @handle should point to a zeroed
551 * snapshot_handle structure. The structure gets updated and a pointer
552 * to it should be passed to this function every next time.
553 *
554 * The @count parameter should contain the number of bytes the caller
555 * wants to read from the snapshot. It must not be zero.
556 *
557 * On success the function returns a positive number. Then, the caller
558 * is allowed to read up to the returned number of bytes from the memory
559 * location computed by the data_of() macro. The number returned
560 * may be smaller than @count, but this only happens if the read would
561 * cross a page boundary otherwise.
562 *
563 * The function returns 0 to indicate the end of data stream condition,
564 * and a negative number is returned on error. In such cases the
565 * structure pointed to by @handle is not updated and should not be used
566 * any more.
567 */
568
569int snapshot_read_next(struct snapshot_handle *handle, size_t count)
570{
571 static unsigned long *buffer;
572
573 if (handle->page > nr_meta_pages + nr_copy_pages)
574 return 0;
575 if (!buffer) {
576 /* This makes the buffer be freed by swsusp_free() */
577 buffer = alloc_image_page(GFP_ATOMIC, 0);
578 if (!buffer)
579 return -ENOMEM;
580 }
581 if (!handle->offset) {
582 init_header((struct swsusp_info *)buffer);
583 handle->buffer = buffer;
584 handle->pbe = pagedir_nosave;
585 }
586 if (handle->prev < handle->page) {
587 if (handle->page <= nr_meta_pages) {
588 handle->pbe = pack_orig_addresses(buffer, handle->pbe);
589 if (!handle->pbe)
590 handle->pbe = pagedir_nosave;
591 } else {
592 handle->buffer = (void *)handle->pbe->address;
593 handle->pbe = handle->pbe->next;
594 }
595 handle->prev = handle->page;
596 }
597 handle->buf_offset = handle->page_offset;
598 if (handle->page_offset + count >= PAGE_SIZE) {
599 count = PAGE_SIZE - handle->page_offset;
600 handle->page_offset = 0;
601 handle->page++;
602 } else {
603 handle->page_offset += count;
604 }
605 handle->offset += count;
606 return count;
607}
608
609/**
610 * mark_unsafe_pages - mark the pages that cannot be used for storing
611 * the image during resume, because they conflict with the pages that
612 * had been used before suspend
613 */
614
615static int mark_unsafe_pages(struct pbe *pblist)
616{
617 struct zone *zone;
618 unsigned long zone_pfn;
619 struct pbe *p;
620
621 if (!pblist) /* a sanity check */
622 return -EINVAL;
623
624 /* Clear page flags */
625 for_each_zone (zone) {
626 for (zone_pfn = 0; zone_pfn < zone->spanned_pages; ++zone_pfn)
627 if (pfn_valid(zone_pfn + zone->zone_start_pfn))
628 ClearPageNosaveFree(pfn_to_page(zone_pfn +
629 zone->zone_start_pfn));
630 }
631
632 /* Mark orig addresses */
633 for_each_pbe (p, pblist) {
634 if (virt_addr_valid(p->orig_address))
635 SetPageNosaveFree(virt_to_page(p->orig_address));
636 else
637 return -EFAULT;
638 }
639
640 return 0;
641}
642
643static void copy_page_backup_list(struct pbe *dst, struct pbe *src)
644{
645 /* We assume both lists contain the same number of elements */
646 while (src) {
647 dst->orig_address = src->orig_address;
648 dst = dst->next;
649 src = src->next;
650 }
651}
652
653static int check_header(struct swsusp_info *info)
654{
655 char *reason = NULL;
656
657 if (info->version_code != LINUX_VERSION_CODE)
658 reason = "kernel version";
659 if (info->num_physpages != num_physpages)
660 reason = "memory size";
661 if (strcmp(info->uts.sysname,system_utsname.sysname))
662 reason = "system type";
663 if (strcmp(info->uts.release,system_utsname.release))
664 reason = "kernel release";
665 if (strcmp(info->uts.version,system_utsname.version))
666 reason = "version";
667 if (strcmp(info->uts.machine,system_utsname.machine))
668 reason = "machine";
669 if (reason) {
670 printk(KERN_ERR "swsusp: Resume mismatch: %s\n", reason);
671 return -EPERM;
672 }
673 return 0;
674}
675
676/**
677 * load header - check the image header and copy data from it
678 */
679
680static int load_header(struct snapshot_handle *handle,
681 struct swsusp_info *info)
682{
683 int error;
684 struct pbe *pblist;
685
686 error = check_header(info);
687 if (!error) {
688 pblist = alloc_pagedir(info->image_pages, GFP_ATOMIC, 0);
689 if (!pblist)
690 return -ENOMEM;
691 pagedir_nosave = pblist;
692 handle->pbe = pblist;
693 nr_copy_pages = info->image_pages;
694 nr_meta_pages = info->pages - info->image_pages - 1;
695 }
696 return error;
697}
698
699/**
700 * unpack_orig_addresses - copy the elements of @buf[] (1 page) to
701 * the PBEs in the list starting at @pbe
702 */
703
704static inline struct pbe *unpack_orig_addresses(unsigned long *buf,
705 struct pbe *pbe)
706{
707 int j;
708
709 for (j = 0; j < PAGE_SIZE / sizeof(long) && pbe; j++) {
710 pbe->orig_address = buf[j];
711 pbe = pbe->next;
712 }
713 return pbe;
714}
715
716/**
717 * create_image - use metadata contained in the PBE list
718 * pointed to by pagedir_nosave to mark the pages that will
719 * be overwritten in the process of restoring the system
720 * memory state from the image and allocate memory for
721 * the image avoiding these pages
722 */
723
724static int create_image(struct snapshot_handle *handle)
725{
726 int error = 0;
727 struct pbe *p, *pblist;
728
729 p = pagedir_nosave;
730 error = mark_unsafe_pages(p);
731 if (!error) {
732 pblist = alloc_pagedir(nr_copy_pages, GFP_ATOMIC, 1);
733 if (pblist)
734 copy_page_backup_list(pblist, p);
735 free_pagedir(p);
736 if (!pblist)
737 error = -ENOMEM;
738 }
739 if (!error)
740 error = alloc_data_pages(pblist, GFP_ATOMIC, 1);
741 if (!error) {
742 release_eaten_pages();
743 pagedir_nosave = pblist;
744 } else {
745 pagedir_nosave = NULL;
746 handle->pbe = NULL;
747 nr_copy_pages = 0;
748 nr_meta_pages = 0;
749 }
750 return error;
751}
752
753/**
754 * snapshot_write_next - used for writing the system memory snapshot.
755 *
756 * On the first call to it @handle should point to a zeroed
757 * snapshot_handle structure. The structure gets updated and a pointer
758 * to it should be passed to this function every next time.
759 *
760 * The @count parameter should contain the number of bytes the caller
761 * wants to write to the image. It must not be zero.
762 *
763 * On success the function returns a positive number. Then, the caller
764 * is allowed to write up to the returned number of bytes to the memory
765 * location computed by the data_of() macro. The number returned
766 * may be smaller than @count, but this only happens if the write would
767 * cross a page boundary otherwise.
768 *
769 * The function returns 0 to indicate the "end of file" condition,
770 * and a negative number is returned on error. In such cases the
771 * structure pointed to by @handle is not updated and should not be used
772 * any more.
773 */
774
775int snapshot_write_next(struct snapshot_handle *handle, size_t count)
776{
777 static unsigned long *buffer;
778 int error = 0;
779
780 if (handle->prev && handle->page > nr_meta_pages + nr_copy_pages)
781 return 0;
782 if (!buffer) {
783 /* This makes the buffer be freed by swsusp_free() */
784 buffer = alloc_image_page(GFP_ATOMIC, 0);
785 if (!buffer)
786 return -ENOMEM;
787 }
788 if (!handle->offset)
789 handle->buffer = buffer;
790 if (handle->prev < handle->page) {
791 if (!handle->prev) {
792 error = load_header(handle, (struct swsusp_info *)buffer);
793 if (error)
794 return error;
795 } else if (handle->prev <= nr_meta_pages) {
796 handle->pbe = unpack_orig_addresses(buffer, handle->pbe);
797 if (!handle->pbe) {
798 error = create_image(handle);
799 if (error)
800 return error;
801 handle->pbe = pagedir_nosave;
802 handle->buffer = (void *)handle->pbe->address;
803 }
804 } else {
805 handle->pbe = handle->pbe->next;
806 handle->buffer = (void *)handle->pbe->address;
807 }
808 handle->prev = handle->page;
809 }
810 handle->buf_offset = handle->page_offset;
811 if (handle->page_offset + count >= PAGE_SIZE) {
812 count = PAGE_SIZE - handle->page_offset;
813 handle->page_offset = 0;
814 handle->page++;
815 } else {
816 handle->page_offset += count;
817 }
818 handle->offset += count;
819 return count;
820}
821
822int snapshot_image_loaded(struct snapshot_handle *handle)
823{
824 return !(!handle->pbe || handle->pbe->next || !nr_copy_pages ||
825 handle->page <= nr_meta_pages + nr_copy_pages);
826}