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