blob: 1d276b3ae15292df2e92c64de9069c8a13975b14 [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. Wysocki75534b52006-09-25 23:32:52 -070037/* List of PBEs used for creating and restoring the suspend image */
38struct pbe *restore_pblist;
39
Rafael J. Wysockif577eb32006-03-23 02:59:59 -080040static unsigned int nr_copy_pages;
41static unsigned int nr_meta_pages;
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -080042static unsigned long *buffer;
Rafael J. Wysocki7088a5c2006-01-06 00:13:05 -080043
Rafael J. Wysocki25761b62005-10-30 14:59:56 -080044#ifdef CONFIG_HIGHMEM
Linus Torvalds34480972006-06-25 18:41:00 -070045unsigned int count_highmem_pages(void)
Rafael J. Wysocki72a97e02006-01-06 00:13:46 -080046{
47 struct zone *zone;
48 unsigned long zone_pfn;
49 unsigned int n = 0;
50
51 for_each_zone (zone)
52 if (is_highmem(zone)) {
53 mark_free_pages(zone);
54 for (zone_pfn = 0; zone_pfn < zone->spanned_pages; zone_pfn++) {
55 struct page *page;
56 unsigned long pfn = zone_pfn + zone->zone_start_pfn;
57 if (!pfn_valid(pfn))
58 continue;
59 page = pfn_to_page(pfn);
60 if (PageReserved(page))
61 continue;
62 if (PageNosaveFree(page))
63 continue;
64 n++;
65 }
66 }
67 return n;
68}
69
Rafael J. Wysocki25761b62005-10-30 14:59:56 -080070struct highmem_page {
71 char *data;
72 struct page *page;
73 struct highmem_page *next;
74};
75
76static struct highmem_page *highmem_copy;
77
78static int save_highmem_zone(struct zone *zone)
79{
80 unsigned long zone_pfn;
81 mark_free_pages(zone);
82 for (zone_pfn = 0; zone_pfn < zone->spanned_pages; ++zone_pfn) {
83 struct page *page;
84 struct highmem_page *save;
85 void *kaddr;
86 unsigned long pfn = zone_pfn + zone->zone_start_pfn;
87
Pavel Machekce6ed292006-03-23 03:00:05 -080088 if (!(pfn%10000))
Rafael J. Wysocki25761b62005-10-30 14:59:56 -080089 printk(".");
90 if (!pfn_valid(pfn))
91 continue;
92 page = pfn_to_page(pfn);
93 /*
94 * This condition results from rvmalloc() sans vmalloc_32()
95 * and architectural memory reservations. This should be
96 * corrected eventually when the cases giving rise to this
97 * are better understood.
98 */
Andrew Mortonc8adb492006-02-15 15:17:42 -080099 if (PageReserved(page))
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800100 continue;
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800101 BUG_ON(PageNosave(page));
102 if (PageNosaveFree(page))
103 continue;
104 save = kmalloc(sizeof(struct highmem_page), GFP_ATOMIC);
105 if (!save)
106 return -ENOMEM;
107 save->next = highmem_copy;
108 save->page = page;
109 save->data = (void *) get_zeroed_page(GFP_ATOMIC);
110 if (!save->data) {
111 kfree(save);
112 return -ENOMEM;
113 }
114 kaddr = kmap_atomic(page, KM_USER0);
115 memcpy(save->data, kaddr, PAGE_SIZE);
116 kunmap_atomic(kaddr, KM_USER0);
117 highmem_copy = save;
118 }
119 return 0;
120}
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800121
Linus Torvalds34480972006-06-25 18:41:00 -0700122int save_highmem(void)
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800123{
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800124 struct zone *zone;
125 int res = 0;
126
Pavel Machekce6ed292006-03-23 03:00:05 -0800127 pr_debug("swsusp: Saving Highmem");
Shaohua Lie4e4d662006-03-23 03:00:06 -0800128 drain_local_pages();
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800129 for_each_zone (zone) {
130 if (is_highmem(zone))
131 res = save_highmem_zone(zone);
132 if (res)
133 return res;
134 }
Pavel Machekce6ed292006-03-23 03:00:05 -0800135 printk("\n");
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800136 return 0;
137}
138
Linus Torvalds34480972006-06-25 18:41:00 -0700139int restore_highmem(void)
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800140{
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800141 printk("swsusp: Restoring Highmem\n");
142 while (highmem_copy) {
143 struct highmem_page *save = highmem_copy;
144 void *kaddr;
145 highmem_copy = save->next;
146
147 kaddr = kmap_atomic(save->page, KM_USER0);
148 memcpy(kaddr, save->data, PAGE_SIZE);
149 kunmap_atomic(kaddr, KM_USER0);
150 free_page((long) save->data);
151 kfree(save);
152 }
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800153 return 0;
154}
Shaohua Lice4ab002006-06-23 02:04:44 -0700155#else
Adrian Bunk7bff24e2006-06-23 02:04:47 -0700156static inline unsigned int count_highmem_pages(void) {return 0;}
157static inline int save_highmem(void) {return 0;}
158static inline int restore_highmem(void) {return 0;}
Rafael J. Wysocki0fbeb5a2005-11-08 21:34:41 -0800159#endif
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800160
Rafael J. Wysockif6143aa2006-09-25 23:32:50 -0700161/**
162 * @safe_needed - on resume, for storing the PBE list and the image,
163 * we can only use memory pages that do not conflict with the pages
164 * used before suspend.
165 *
166 * The unsafe pages are marked with the PG_nosave_free flag
167 * and we count them using unsafe_pages
168 */
169
170static unsigned int unsafe_pages;
171
172static void *alloc_image_page(gfp_t gfp_mask, int safe_needed)
173{
174 void *res;
175
176 res = (void *)get_zeroed_page(gfp_mask);
177 if (safe_needed)
178 while (res && PageNosaveFree(virt_to_page(res))) {
179 /* The page is unsafe, mark it for swsusp_free() */
180 SetPageNosave(virt_to_page(res));
181 unsafe_pages++;
182 res = (void *)get_zeroed_page(gfp_mask);
183 }
184 if (res) {
185 SetPageNosave(virt_to_page(res));
186 SetPageNosaveFree(virt_to_page(res));
187 }
188 return res;
189}
190
191unsigned long get_safe_page(gfp_t gfp_mask)
192{
193 return (unsigned long)alloc_image_page(gfp_mask, 1);
194}
195
196/**
197 * free_image_page - free page represented by @addr, allocated with
198 * alloc_image_page (page flags set by it must be cleared)
199 */
200
201static inline void free_image_page(void *addr, int clear_nosave_free)
202{
203 ClearPageNosave(virt_to_page(addr));
204 if (clear_nosave_free)
205 ClearPageNosaveFree(virt_to_page(addr));
206 free_page((unsigned long)addr);
207}
208
209/**
210 * pfn_is_nosave - check if given pfn is in the 'nosave' section
211 */
212
Rafael J. Wysockiae83c5ee2006-09-25 23:32:45 -0700213static inline int pfn_is_nosave(unsigned long pfn)
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800214{
215 unsigned long nosave_begin_pfn = __pa(&__nosave_begin) >> PAGE_SHIFT;
216 unsigned long nosave_end_pfn = PAGE_ALIGN(__pa(&__nosave_end)) >> PAGE_SHIFT;
217 return (pfn >= nosave_begin_pfn) && (pfn < nosave_end_pfn);
218}
219
220/**
221 * saveable - Determine whether a page should be cloned or not.
222 * @pfn: The page
223 *
Rafael J. Wysockiae83c5ee2006-09-25 23:32:45 -0700224 * We save a page if it isn't Nosave, and is not in the range of pages
225 * statically defined as 'unsaveable', and it
226 * isn't a part of a free chunk of pages.
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800227 */
228
Rafael J. Wysockiae83c5ee2006-09-25 23:32:45 -0700229static struct page *saveable_page(unsigned long pfn)
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800230{
Pavel Machekde491862005-10-30 14:59:59 -0800231 struct page *page;
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800232
233 if (!pfn_valid(pfn))
Rafael J. Wysockiae83c5ee2006-09-25 23:32:45 -0700234 return NULL;
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800235
236 page = pfn_to_page(pfn);
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800237
Rafael J. Wysockiae83c5ee2006-09-25 23:32:45 -0700238 if (PageNosave(page))
239 return NULL;
240 if (PageReserved(page) && pfn_is_nosave(pfn))
241 return NULL;
242 if (PageNosaveFree(page))
243 return NULL;
244
245 return page;
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800246}
247
Rafael J. Wysocki72a97e02006-01-06 00:13:46 -0800248unsigned int count_data_pages(void)
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800249{
250 struct zone *zone;
Rafael J. Wysockiae83c5ee2006-09-25 23:32:45 -0700251 unsigned long pfn, max_zone_pfn;
Pavel Machekdc19d502005-11-07 00:58:40 -0800252 unsigned int n = 0;
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800253
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800254 for_each_zone (zone) {
255 if (is_highmem(zone))
256 continue;
257 mark_free_pages(zone);
Rafael J. Wysockiae83c5ee2006-09-25 23:32:45 -0700258 max_zone_pfn = zone->zone_start_pfn + zone->spanned_pages;
259 for (pfn = zone->zone_start_pfn; pfn < max_zone_pfn; pfn++)
260 n += !!saveable_page(pfn);
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800261 }
Rafael J. Wysockia0f49652005-10-30 14:59:57 -0800262 return n;
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800263}
264
Rafael J. Wysockif623f0d2006-09-25 23:32:49 -0700265static inline void copy_data_page(long *dst, long *src)
266{
267 int n;
268
269 /* copy_page and memcpy are not usable for copying task structs. */
270 for (n = PAGE_SIZE / sizeof(long); n; n--)
271 *dst++ = *src++;
272}
273
Rafael J. Wysockia0f49652005-10-30 14:59:57 -0800274static void copy_data_pages(struct pbe *pblist)
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800275{
276 struct zone *zone;
Rafael J. Wysockiae83c5ee2006-09-25 23:32:45 -0700277 unsigned long pfn, max_zone_pfn;
Rafael J. Wysockif623f0d2006-09-25 23:32:49 -0700278 struct pbe *pbe;
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800279
Rafael J. Wysockia0f49652005-10-30 14:59:57 -0800280 pbe = pblist;
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800281 for_each_zone (zone) {
282 if (is_highmem(zone))
283 continue;
284 mark_free_pages(zone);
Rafael J. Wysockiae83c5ee2006-09-25 23:32:45 -0700285 max_zone_pfn = zone->zone_start_pfn + zone->spanned_pages;
286 for (pfn = zone->zone_start_pfn; pfn < max_zone_pfn; pfn++) {
287 struct page *page = saveable_page(pfn);
288
289 if (page) {
Rafael J. Wysockif623f0d2006-09-25 23:32:49 -0700290 void *ptr = page_address(page);
Rafael J. Wysocki95018f72006-07-10 04:45:00 -0700291
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800292 BUG_ON(!pbe);
Rafael J. Wysockif623f0d2006-09-25 23:32:49 -0700293 copy_data_page((void *)pbe->address, ptr);
294 pbe->orig_address = (unsigned long)ptr;
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800295 pbe = pbe->next;
296 }
297 }
298 }
299 BUG_ON(pbe);
300}
301
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800302/**
303 * free_pagedir - free pages allocated with alloc_pagedir()
304 */
305
Rafael J. Wysocki4a3b98a2006-04-18 22:20:29 -0700306static void free_pagedir(struct pbe *pblist, int clear_nosave_free)
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800307{
308 struct pbe *pbe;
309
310 while (pblist) {
311 pbe = (pblist + PB_PAGE_SKIP)->next;
Rafael J. Wysockif6143aa2006-09-25 23:32:50 -0700312 free_image_page(pblist, clear_nosave_free);
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800313 pblist = pbe;
314 }
315}
316
317/**
318 * fill_pb_page - Create a list of PBEs on a given memory page
319 */
320
Rafael J. Wysockicd560bb2006-09-25 23:32:50 -0700321static inline void fill_pb_page(struct pbe *pbpage, unsigned int n)
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800322{
323 struct pbe *p;
324
325 p = pbpage;
Rafael J. Wysockicd560bb2006-09-25 23:32:50 -0700326 pbpage += n - 1;
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800327 do
328 p->next = p + 1;
329 while (++p < pbpage);
330}
331
332/**
333 * create_pbe_list - Create a list of PBEs on top of a given chain
334 * of memory pages allocated with alloc_pagedir()
Rafael J. Wysockicd560bb2006-09-25 23:32:50 -0700335 *
336 * This function assumes that pages allocated by alloc_image_page() will
337 * always be zeroed.
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800338 */
339
Rafael J. Wysocki7088a5c2006-01-06 00:13:05 -0800340static inline void create_pbe_list(struct pbe *pblist, unsigned int nr_pages)
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800341{
Rafael J. Wysockicd560bb2006-09-25 23:32:50 -0700342 struct pbe *pbpage;
Pavel Machekdc19d502005-11-07 00:58:40 -0800343 unsigned int num = PBES_PER_PAGE;
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800344
345 for_each_pb_page (pbpage, pblist) {
346 if (num >= nr_pages)
347 break;
348
Rafael J. Wysockicd560bb2006-09-25 23:32:50 -0700349 fill_pb_page(pbpage, PBES_PER_PAGE);
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800350 num += PBES_PER_PAGE;
351 }
352 if (pbpage) {
Rafael J. Wysockicd560bb2006-09-25 23:32:50 -0700353 num -= PBES_PER_PAGE;
354 fill_pb_page(pbpage, nr_pages - num);
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800355 }
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800356}
357
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800358/**
359 * alloc_pagedir - Allocate the page directory.
360 *
361 * First, determine exactly how many pages we need and
362 * allocate them.
363 *
364 * We arrange the pages in a chain: each page is an array of PBES_PER_PAGE
365 * struct pbe elements (pbes) and the last element in the page points
366 * to the next page.
367 *
368 * On each page we set up a list of struct_pbe elements.
369 */
370
Adrian Bunk7bff24e2006-06-23 02:04:47 -0700371static struct pbe *alloc_pagedir(unsigned int nr_pages, gfp_t gfp_mask,
372 int safe_needed)
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800373{
Pavel Machekdc19d502005-11-07 00:58:40 -0800374 unsigned int num;
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800375 struct pbe *pblist, *pbe;
376
377 if (!nr_pages)
378 return NULL;
379
Rafael J. Wysocki054bd4c2005-11-08 21:34:39 -0800380 pblist = alloc_image_page(gfp_mask, safe_needed);
Rafael J. Wysockicd560bb2006-09-25 23:32:50 -0700381 pbe = pblist;
382 for (num = PBES_PER_PAGE; num < nr_pages; num += PBES_PER_PAGE) {
383 if (!pbe) {
384 free_pagedir(pblist, 1);
385 return NULL;
386 }
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800387 pbe += PB_PAGE_SKIP;
Rafael J. Wysocki054bd4c2005-11-08 21:34:39 -0800388 pbe->next = alloc_image_page(gfp_mask, safe_needed);
Rafael J. Wysockicd560bb2006-09-25 23:32:50 -0700389 pbe = pbe->next;
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800390 }
Rafael J. Wysockicd560bb2006-09-25 23:32:50 -0700391 create_pbe_list(pblist, nr_pages);
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800392 return pblist;
393}
394
395/**
396 * Free pages we allocated for suspend. Suspend pages are alocated
397 * before atomic copy, so we need to free them after resume.
398 */
399
400void swsusp_free(void)
401{
402 struct zone *zone;
Rafael J. Wysockiae83c5ee2006-09-25 23:32:45 -0700403 unsigned long pfn, max_zone_pfn;
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800404
405 for_each_zone(zone) {
Rafael J. Wysockiae83c5ee2006-09-25 23:32:45 -0700406 max_zone_pfn = zone->zone_start_pfn + zone->spanned_pages;
407 for (pfn = zone->zone_start_pfn; pfn < max_zone_pfn; pfn++)
408 if (pfn_valid(pfn)) {
409 struct page *page = pfn_to_page(pfn);
410
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800411 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;
Rafael J. Wysocki75534b52006-09-25 23:32:52 -0700420 restore_pblist = NULL;
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -0800421 buffer = NULL;
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800422}
423
424
425/**
426 * enough_free_mem - Make sure we enough free memory to snapshot.
427 *
428 * Returns TRUE or FALSE after checking the number of available
429 * free pages.
430 */
431
Pavel Machekdc19d502005-11-07 00:58:40 -0800432static int enough_free_mem(unsigned int nr_pages)
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800433{
Rafael J. Wysockie5e2fa72006-01-06 00:14:20 -0800434 struct zone *zone;
435 unsigned int n = 0;
436
437 for_each_zone (zone)
438 if (!is_highmem(zone))
439 n += zone->free_pages;
440 pr_debug("swsusp: available memory: %u pages\n", n);
441 return n > (nr_pages + PAGES_FOR_IO +
Rafael J. Wysockia0f49652005-10-30 14:59:57 -0800442 (nr_pages + PBES_PER_PAGE - 1) / PBES_PER_PAGE);
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800443}
444
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800445static int alloc_data_pages(struct pbe *pblist, gfp_t gfp_mask, int safe_needed)
Rafael J. Wysocki054bd4c2005-11-08 21:34:39 -0800446{
447 struct pbe *p;
448
449 for_each_pbe (p, pblist) {
450 p->address = (unsigned long)alloc_image_page(gfp_mask, safe_needed);
451 if (!p->address)
452 return -ENOMEM;
453 }
454 return 0;
455}
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800456
Pavel Machekdc19d502005-11-07 00:58:40 -0800457static struct pbe *swsusp_alloc(unsigned int nr_pages)
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800458{
Rafael J. Wysocki054bd4c2005-11-08 21:34:39 -0800459 struct pbe *pblist;
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800460
Rafael J. Wysocki054bd4c2005-11-08 21:34:39 -0800461 if (!(pblist = alloc_pagedir(nr_pages, GFP_ATOMIC | __GFP_COLD, 0))) {
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800462 printk(KERN_ERR "suspend: Allocating pagedir failed.\n");
Rafael J. Wysockia0f49652005-10-30 14:59:57 -0800463 return NULL;
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800464 }
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800465
Rafael J. Wysocki054bd4c2005-11-08 21:34:39 -0800466 if (alloc_data_pages(pblist, GFP_ATOMIC | __GFP_COLD, 0)) {
467 printk(KERN_ERR "suspend: Allocating image pages failed.\n");
468 swsusp_free();
469 return NULL;
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800470 }
471
Rafael J. Wysockia0f49652005-10-30 14:59:57 -0800472 return pblist;
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800473}
474
Rafael J. Wysocki2e32a432005-10-30 15:00:00 -0800475asmlinkage int swsusp_save(void)
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800476{
Pavel Machekdc19d502005-11-07 00:58:40 -0800477 unsigned int nr_pages;
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800478
479 pr_debug("swsusp: critical section: \n");
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800480
481 drain_local_pages();
Rafael J. Wysockia0f49652005-10-30 14:59:57 -0800482 nr_pages = count_data_pages();
483 printk("swsusp: Need to copy %u pages\n", nr_pages);
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800484
485 pr_debug("swsusp: pages needed: %u + %lu + %u, free: %u\n",
Rafael J. Wysockia0f49652005-10-30 14:59:57 -0800486 nr_pages,
487 (nr_pages + PBES_PER_PAGE - 1) / PBES_PER_PAGE,
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800488 PAGES_FOR_IO, nr_free_pages());
489
Rafael J. Wysockia0f49652005-10-30 14:59:57 -0800490 if (!enough_free_mem(nr_pages)) {
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800491 printk(KERN_ERR "swsusp: Not enough free memory\n");
492 return -ENOMEM;
493 }
494
Rafael J. Wysocki75534b52006-09-25 23:32:52 -0700495 restore_pblist = swsusp_alloc(nr_pages);
496 if (!restore_pblist)
Rafael J. Wysockia0f49652005-10-30 14:59:57 -0800497 return -ENOMEM;
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800498
499 /* During allocating of suspend pagedir, new cold pages may appear.
500 * Kill them.
501 */
502 drain_local_pages();
Rafael J. Wysocki75534b52006-09-25 23:32:52 -0700503 copy_data_pages(restore_pblist);
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800504
505 /*
506 * End of critical section. From now on, we can write to memory,
507 * but we should not touch disk. This specially means we must _not_
508 * touch swap space! Except we must write out our image of course.
509 */
510
Rafael J. Wysockia0f49652005-10-30 14:59:57 -0800511 nr_copy_pages = nr_pages;
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800512 nr_meta_pages = (nr_pages * sizeof(long) + PAGE_SIZE - 1) >> PAGE_SHIFT;
Rafael J. Wysockia0f49652005-10-30 14:59:57 -0800513
514 printk("swsusp: critical section/: done (%d pages copied)\n", nr_pages);
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800515 return 0;
516}
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800517
518static void init_header(struct swsusp_info *info)
519{
520 memset(info, 0, sizeof(struct swsusp_info));
521 info->version_code = LINUX_VERSION_CODE;
522 info->num_physpages = num_physpages;
523 memcpy(&info->uts, &system_utsname, sizeof(system_utsname));
524 info->cpus = num_online_cpus();
525 info->image_pages = nr_copy_pages;
526 info->pages = nr_copy_pages + nr_meta_pages + 1;
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -0800527 info->size = info->pages;
528 info->size <<= PAGE_SHIFT;
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800529}
530
531/**
532 * pack_orig_addresses - the .orig_address fields of the PBEs from the
533 * list starting at @pbe are stored in the array @buf[] (1 page)
534 */
535
536static inline struct pbe *pack_orig_addresses(unsigned long *buf, struct pbe *pbe)
537{
538 int j;
539
540 for (j = 0; j < PAGE_SIZE / sizeof(long) && pbe; j++) {
541 buf[j] = pbe->orig_address;
542 pbe = pbe->next;
543 }
544 if (!pbe)
545 for (; j < PAGE_SIZE / sizeof(long); j++)
546 buf[j] = 0;
547 return pbe;
548}
549
550/**
551 * snapshot_read_next - used for reading the system memory snapshot.
552 *
553 * On the first call to it @handle should point to a zeroed
554 * snapshot_handle structure. The structure gets updated and a pointer
555 * to it should be passed to this function every next time.
556 *
557 * The @count parameter should contain the number of bytes the caller
558 * wants to read from the snapshot. It must not be zero.
559 *
560 * On success the function returns a positive number. Then, the caller
561 * is allowed to read up to the returned number of bytes from the memory
562 * location computed by the data_of() macro. The number returned
563 * may be smaller than @count, but this only happens if the read would
564 * cross a page boundary otherwise.
565 *
566 * The function returns 0 to indicate the end of data stream condition,
567 * and a negative number is returned on error. In such cases the
568 * structure pointed to by @handle is not updated and should not be used
569 * any more.
570 */
571
572int snapshot_read_next(struct snapshot_handle *handle, size_t count)
573{
Rafael J. Wysockifb13a282006-09-25 23:32:46 -0700574 if (handle->cur > nr_meta_pages + nr_copy_pages)
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800575 return 0;
576 if (!buffer) {
577 /* This makes the buffer be freed by swsusp_free() */
578 buffer = alloc_image_page(GFP_ATOMIC, 0);
579 if (!buffer)
580 return -ENOMEM;
581 }
582 if (!handle->offset) {
583 init_header((struct swsusp_info *)buffer);
584 handle->buffer = buffer;
Rafael J. Wysocki75534b52006-09-25 23:32:52 -0700585 handle->pbe = restore_pblist;
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800586 }
Rafael J. Wysockifb13a282006-09-25 23:32:46 -0700587 if (handle->prev < handle->cur) {
588 if (handle->cur <= nr_meta_pages) {
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800589 handle->pbe = pack_orig_addresses(buffer, handle->pbe);
590 if (!handle->pbe)
Rafael J. Wysocki75534b52006-09-25 23:32:52 -0700591 handle->pbe = restore_pblist;
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800592 } else {
593 handle->buffer = (void *)handle->pbe->address;
594 handle->pbe = handle->pbe->next;
595 }
Rafael J. Wysockifb13a282006-09-25 23:32:46 -0700596 handle->prev = handle->cur;
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800597 }
Rafael J. Wysockifb13a282006-09-25 23:32:46 -0700598 handle->buf_offset = handle->cur_offset;
599 if (handle->cur_offset + count >= PAGE_SIZE) {
600 count = PAGE_SIZE - handle->cur_offset;
601 handle->cur_offset = 0;
602 handle->cur++;
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800603 } else {
Rafael J. Wysockifb13a282006-09-25 23:32:46 -0700604 handle->cur_offset += count;
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800605 }
606 handle->offset += count;
607 return count;
608}
609
610/**
611 * mark_unsafe_pages - mark the pages that cannot be used for storing
612 * the image during resume, because they conflict with the pages that
613 * had been used before suspend
614 */
615
616static int mark_unsafe_pages(struct pbe *pblist)
617{
618 struct zone *zone;
Rafael J. Wysockiae83c5ee2006-09-25 23:32:45 -0700619 unsigned long pfn, max_zone_pfn;
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800620 struct pbe *p;
621
622 if (!pblist) /* a sanity check */
623 return -EINVAL;
624
625 /* Clear page flags */
626 for_each_zone (zone) {
Rafael J. Wysockiae83c5ee2006-09-25 23:32:45 -0700627 max_zone_pfn = zone->zone_start_pfn + zone->spanned_pages;
628 for (pfn = zone->zone_start_pfn; pfn < max_zone_pfn; pfn++)
629 if (pfn_valid(pfn))
630 ClearPageNosaveFree(pfn_to_page(pfn));
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800631 }
632
633 /* Mark orig addresses */
634 for_each_pbe (p, pblist) {
635 if (virt_addr_valid(p->orig_address))
636 SetPageNosaveFree(virt_to_page(p->orig_address));
637 else
638 return -EFAULT;
639 }
640
Rafael J. Wysocki968808b2006-06-23 02:04:48 -0700641 unsafe_pages = 0;
642
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800643 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;
Rafael J. Wysocki75534b52006-09-25 23:32:52 -0700694 restore_pblist = pblist;
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800695 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/**
Rafael J. Wysocki968808b2006-06-23 02:04:48 -0700720 * prepare_image - use metadata contained in the PBE list
Rafael J. Wysocki75534b52006-09-25 23:32:52 -0700721 * pointed to by restore_pblist to mark the pages that will
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800722 * be overwritten in the process of restoring the system
Rafael J. Wysocki968808b2006-06-23 02:04:48 -0700723 * memory state from the image ("unsafe" pages) and allocate
724 * memory for the image
725 *
726 * The idea is to allocate the PBE list first and then
727 * allocate as many pages as it's needed for the image data,
728 * but not to assign these pages to the PBEs initially.
729 * Instead, we just mark them as allocated and create a list
730 * of "safe" which will be used later
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800731 */
732
Rafael J. Wysocki968808b2006-06-23 02:04:48 -0700733struct safe_page {
734 struct safe_page *next;
735 char padding[PAGE_SIZE - sizeof(void *)];
736};
737
738static struct safe_page *safe_pages;
739
740static int prepare_image(struct snapshot_handle *handle)
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800741{
742 int error = 0;
Rafael J. Wysocki968808b2006-06-23 02:04:48 -0700743 unsigned int nr_pages = nr_copy_pages;
744 struct pbe *p, *pblist = NULL;
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800745
Rafael J. Wysocki75534b52006-09-25 23:32:52 -0700746 p = restore_pblist;
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800747 error = mark_unsafe_pages(p);
748 if (!error) {
Rafael J. Wysocki968808b2006-06-23 02:04:48 -0700749 pblist = alloc_pagedir(nr_pages, GFP_ATOMIC, 1);
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800750 if (pblist)
751 copy_page_backup_list(pblist, p);
Rafael J. Wysocki4a3b98a2006-04-18 22:20:29 -0700752 free_pagedir(p, 0);
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800753 if (!pblist)
754 error = -ENOMEM;
755 }
Rafael J. Wysocki968808b2006-06-23 02:04:48 -0700756 safe_pages = NULL;
757 if (!error && nr_pages > unsafe_pages) {
758 nr_pages -= unsafe_pages;
759 while (nr_pages--) {
760 struct safe_page *ptr;
761
762 ptr = (struct safe_page *)get_zeroed_page(GFP_ATOMIC);
763 if (!ptr) {
764 error = -ENOMEM;
765 break;
766 }
767 if (!PageNosaveFree(virt_to_page(ptr))) {
768 /* The page is "safe", add it to the list */
769 ptr->next = safe_pages;
770 safe_pages = ptr;
771 }
772 /* Mark the page as allocated */
773 SetPageNosave(virt_to_page(ptr));
774 SetPageNosaveFree(virt_to_page(ptr));
775 }
776 }
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800777 if (!error) {
Rafael J. Wysocki75534b52006-09-25 23:32:52 -0700778 restore_pblist = pblist;
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800779 } else {
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800780 handle->pbe = NULL;
Rafael J. Wysocki968808b2006-06-23 02:04:48 -0700781 swsusp_free();
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800782 }
783 return error;
784}
785
Rafael J. Wysocki968808b2006-06-23 02:04:48 -0700786static void *get_buffer(struct snapshot_handle *handle)
787{
788 struct pbe *pbe = handle->pbe, *last = handle->last_pbe;
789 struct page *page = virt_to_page(pbe->orig_address);
790
791 if (PageNosave(page) && PageNosaveFree(page)) {
792 /*
793 * We have allocated the "original" page frame and we can
794 * use it directly to store the read page
795 */
796 pbe->address = 0;
797 if (last && last->next)
798 last->next = NULL;
799 return (void *)pbe->orig_address;
800 }
801 /*
802 * The "original" page frame has not been allocated and we have to
803 * use a "safe" page frame to store the read page
804 */
805 pbe->address = (unsigned long)safe_pages;
806 safe_pages = safe_pages->next;
807 if (last)
808 last->next = pbe;
809 handle->last_pbe = pbe;
810 return (void *)pbe->address;
811}
812
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800813/**
814 * snapshot_write_next - used for writing the system memory snapshot.
815 *
816 * On the first call to it @handle should point to a zeroed
817 * snapshot_handle structure. The structure gets updated and a pointer
818 * to it should be passed to this function every next time.
819 *
820 * The @count parameter should contain the number of bytes the caller
821 * wants to write to the image. It must not be zero.
822 *
823 * On success the function returns a positive number. Then, the caller
824 * is allowed to write up to the returned number of bytes to the memory
825 * location computed by the data_of() macro. The number returned
826 * may be smaller than @count, but this only happens if the write would
827 * cross a page boundary otherwise.
828 *
829 * The function returns 0 to indicate the "end of file" condition,
830 * and a negative number is returned on error. In such cases the
831 * structure pointed to by @handle is not updated and should not be used
832 * any more.
833 */
834
835int snapshot_write_next(struct snapshot_handle *handle, size_t count)
836{
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800837 int error = 0;
838
Rafael J. Wysockifb13a282006-09-25 23:32:46 -0700839 if (handle->prev && handle->cur > nr_meta_pages + nr_copy_pages)
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800840 return 0;
841 if (!buffer) {
842 /* This makes the buffer be freed by swsusp_free() */
843 buffer = alloc_image_page(GFP_ATOMIC, 0);
844 if (!buffer)
845 return -ENOMEM;
846 }
847 if (!handle->offset)
848 handle->buffer = buffer;
Andrew Morton546e0d22006-09-25 23:32:44 -0700849 handle->sync_read = 1;
Rafael J. Wysockifb13a282006-09-25 23:32:46 -0700850 if (handle->prev < handle->cur) {
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800851 if (!handle->prev) {
Andrew Morton546e0d22006-09-25 23:32:44 -0700852 error = load_header(handle,
853 (struct swsusp_info *)buffer);
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800854 if (error)
855 return error;
856 } else if (handle->prev <= nr_meta_pages) {
Andrew Morton546e0d22006-09-25 23:32:44 -0700857 handle->pbe = unpack_orig_addresses(buffer,
858 handle->pbe);
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800859 if (!handle->pbe) {
Rafael J. Wysocki968808b2006-06-23 02:04:48 -0700860 error = prepare_image(handle);
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800861 if (error)
862 return error;
Rafael J. Wysocki75534b52006-09-25 23:32:52 -0700863 handle->pbe = restore_pblist;
Rafael J. Wysocki968808b2006-06-23 02:04:48 -0700864 handle->last_pbe = NULL;
865 handle->buffer = get_buffer(handle);
Andrew Morton546e0d22006-09-25 23:32:44 -0700866 handle->sync_read = 0;
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800867 }
868 } else {
869 handle->pbe = handle->pbe->next;
Rafael J. Wysocki968808b2006-06-23 02:04:48 -0700870 handle->buffer = get_buffer(handle);
Andrew Morton546e0d22006-09-25 23:32:44 -0700871 handle->sync_read = 0;
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800872 }
Rafael J. Wysockifb13a282006-09-25 23:32:46 -0700873 handle->prev = handle->cur;
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800874 }
Rafael J. Wysockifb13a282006-09-25 23:32:46 -0700875 handle->buf_offset = handle->cur_offset;
876 if (handle->cur_offset + count >= PAGE_SIZE) {
877 count = PAGE_SIZE - handle->cur_offset;
878 handle->cur_offset = 0;
879 handle->cur++;
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800880 } else {
Rafael J. Wysockifb13a282006-09-25 23:32:46 -0700881 handle->cur_offset += count;
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800882 }
883 handle->offset += count;
884 return count;
885}
886
887int snapshot_image_loaded(struct snapshot_handle *handle)
888{
889 return !(!handle->pbe || handle->pbe->next || !nr_copy_pages ||
Rafael J. Wysockifb13a282006-09-25 23:32:46 -0700890 handle->cur <= nr_meta_pages + nr_copy_pages);
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800891}