blob: 4ca372f2bc1454a5e7aa0870aa37bff57f21f8d2 [file] [log] [blame]
Rafael J. Wysocki25761b62005-10-30 14:59:56 -08001/*
Pavel Machek96bc7ae2005-10-30 14:59:58 -08002 * linux/kernel/power/snapshot.c
Rafael J. Wysocki25761b62005-10-30 14:59:56 -08003 *
Pavel Machek96bc7ae2005-10-30 14:59:58 -08004 * This file provide system snapshot/restore functionality.
Rafael J. Wysocki25761b62005-10-30 14:59:56 -08005 *
6 * Copyright (C) 1998-2005 Pavel Machek <pavel@suse.cz>
7 *
8 * This file is released under the GPLv2, and is based on swsusp.c.
9 *
10 */
11
12
Rafael J. Wysockif577eb32006-03-23 02:59:59 -080013#include <linux/version.h>
Rafael J. Wysocki25761b62005-10-30 14:59:56 -080014#include <linux/module.h>
15#include <linux/mm.h>
16#include <linux/suspend.h>
17#include <linux/smp_lock.h>
Rafael J. Wysocki25761b62005-10-30 14:59:56 -080018#include <linux/delay.h>
Rafael J. Wysocki25761b62005-10-30 14:59:56 -080019#include <linux/bitops.h>
Rafael J. Wysocki25761b62005-10-30 14:59:56 -080020#include <linux/spinlock.h>
Rafael J. Wysocki25761b62005-10-30 14:59:56 -080021#include <linux/kernel.h>
Rafael J. Wysocki25761b62005-10-30 14:59:56 -080022#include <linux/pm.h>
23#include <linux/device.h>
Rafael J. Wysocki25761b62005-10-30 14:59:56 -080024#include <linux/bootmem.h>
25#include <linux/syscalls.h>
26#include <linux/console.h>
27#include <linux/highmem.h>
Rafael J. Wysocki25761b62005-10-30 14:59:56 -080028
29#include <asm/uaccess.h>
30#include <asm/mmu_context.h>
31#include <asm/pgtable.h>
32#include <asm/tlbflush.h>
33#include <asm/io.h>
34
Rafael J. Wysocki25761b62005-10-30 14:59:56 -080035#include "power.h"
36
Rafael J. Wysocki7088a5c2006-01-06 00:13:05 -080037struct pbe *pagedir_nosave;
Rafael J. Wysockif577eb32006-03-23 02:59:59 -080038static unsigned int nr_copy_pages;
39static unsigned int nr_meta_pages;
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -080040static unsigned long *buffer;
Rafael J. Wysocki7088a5c2006-01-06 00:13:05 -080041
Rafael J. Wysocki25761b62005-10-30 14:59:56 -080042#ifdef CONFIG_HIGHMEM
Linus Torvalds34480972006-06-25 18:41:00 -070043unsigned int count_highmem_pages(void)
Rafael J. Wysocki72a97e02006-01-06 00:13:46 -080044{
45 struct zone *zone;
46 unsigned long zone_pfn;
47 unsigned int n = 0;
48
49 for_each_zone (zone)
50 if (is_highmem(zone)) {
51 mark_free_pages(zone);
52 for (zone_pfn = 0; zone_pfn < zone->spanned_pages; zone_pfn++) {
53 struct page *page;
54 unsigned long pfn = zone_pfn + zone->zone_start_pfn;
55 if (!pfn_valid(pfn))
56 continue;
57 page = pfn_to_page(pfn);
58 if (PageReserved(page))
59 continue;
60 if (PageNosaveFree(page))
61 continue;
62 n++;
63 }
64 }
65 return n;
66}
67
Rafael J. Wysocki25761b62005-10-30 14:59:56 -080068struct highmem_page {
69 char *data;
70 struct page *page;
71 struct highmem_page *next;
72};
73
74static struct highmem_page *highmem_copy;
75
76static int save_highmem_zone(struct zone *zone)
77{
78 unsigned long zone_pfn;
79 mark_free_pages(zone);
80 for (zone_pfn = 0; zone_pfn < zone->spanned_pages; ++zone_pfn) {
81 struct page *page;
82 struct highmem_page *save;
83 void *kaddr;
84 unsigned long pfn = zone_pfn + zone->zone_start_pfn;
85
Pavel Machekce6ed292006-03-23 03:00:05 -080086 if (!(pfn%10000))
Rafael J. Wysocki25761b62005-10-30 14:59:56 -080087 printk(".");
88 if (!pfn_valid(pfn))
89 continue;
90 page = pfn_to_page(pfn);
91 /*
92 * This condition results from rvmalloc() sans vmalloc_32()
93 * and architectural memory reservations. This should be
94 * corrected eventually when the cases giving rise to this
95 * are better understood.
96 */
Andrew Mortonc8adb492006-02-15 15:17:42 -080097 if (PageReserved(page))
Rafael J. Wysocki25761b62005-10-30 14:59:56 -080098 continue;
Rafael J. Wysocki25761b62005-10-30 14:59:56 -080099 BUG_ON(PageNosave(page));
100 if (PageNosaveFree(page))
101 continue;
102 save = kmalloc(sizeof(struct highmem_page), GFP_ATOMIC);
103 if (!save)
104 return -ENOMEM;
105 save->next = highmem_copy;
106 save->page = page;
107 save->data = (void *) get_zeroed_page(GFP_ATOMIC);
108 if (!save->data) {
109 kfree(save);
110 return -ENOMEM;
111 }
112 kaddr = kmap_atomic(page, KM_USER0);
113 memcpy(save->data, kaddr, PAGE_SIZE);
114 kunmap_atomic(kaddr, KM_USER0);
115 highmem_copy = save;
116 }
117 return 0;
118}
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800119
Linus Torvalds34480972006-06-25 18:41:00 -0700120int save_highmem(void)
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800121{
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800122 struct zone *zone;
123 int res = 0;
124
Pavel Machekce6ed292006-03-23 03:00:05 -0800125 pr_debug("swsusp: Saving Highmem");
Shaohua Lie4e4d662006-03-23 03:00:06 -0800126 drain_local_pages();
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800127 for_each_zone (zone) {
128 if (is_highmem(zone))
129 res = save_highmem_zone(zone);
130 if (res)
131 return res;
132 }
Pavel Machekce6ed292006-03-23 03:00:05 -0800133 printk("\n");
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800134 return 0;
135}
136
Linus Torvalds34480972006-06-25 18:41:00 -0700137int restore_highmem(void)
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800138{
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800139 printk("swsusp: Restoring Highmem\n");
140 while (highmem_copy) {
141 struct highmem_page *save = highmem_copy;
142 void *kaddr;
143 highmem_copy = save->next;
144
145 kaddr = kmap_atomic(save->page, KM_USER0);
146 memcpy(kaddr, save->data, PAGE_SIZE);
147 kunmap_atomic(kaddr, KM_USER0);
148 free_page((long) save->data);
149 kfree(save);
150 }
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800151 return 0;
152}
Shaohua Lice4ab002006-06-23 02:04:44 -0700153#else
Adrian Bunk7bff24e2006-06-23 02:04:47 -0700154static inline unsigned int count_highmem_pages(void) {return 0;}
155static inline int save_highmem(void) {return 0;}
156static inline int restore_highmem(void) {return 0;}
Rafael J. Wysocki0fbeb5a2005-11-08 21:34:41 -0800157#endif
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800158
Rafael J. Wysockif6143aa2006-09-25 23:32:50 -0700159/**
160 * @safe_needed - on resume, for storing the PBE list and the image,
161 * we can only use memory pages that do not conflict with the pages
162 * used before suspend.
163 *
164 * The unsafe pages are marked with the PG_nosave_free flag
165 * and we count them using unsafe_pages
166 */
167
168static unsigned int unsafe_pages;
169
170static void *alloc_image_page(gfp_t gfp_mask, int safe_needed)
171{
172 void *res;
173
174 res = (void *)get_zeroed_page(gfp_mask);
175 if (safe_needed)
176 while (res && PageNosaveFree(virt_to_page(res))) {
177 /* The page is unsafe, mark it for swsusp_free() */
178 SetPageNosave(virt_to_page(res));
179 unsafe_pages++;
180 res = (void *)get_zeroed_page(gfp_mask);
181 }
182 if (res) {
183 SetPageNosave(virt_to_page(res));
184 SetPageNosaveFree(virt_to_page(res));
185 }
186 return res;
187}
188
189unsigned long get_safe_page(gfp_t gfp_mask)
190{
191 return (unsigned long)alloc_image_page(gfp_mask, 1);
192}
193
194/**
195 * free_image_page - free page represented by @addr, allocated with
196 * alloc_image_page (page flags set by it must be cleared)
197 */
198
199static inline void free_image_page(void *addr, int clear_nosave_free)
200{
201 ClearPageNosave(virt_to_page(addr));
202 if (clear_nosave_free)
203 ClearPageNosaveFree(virt_to_page(addr));
204 free_page((unsigned long)addr);
205}
206
207/**
208 * pfn_is_nosave - check if given pfn is in the 'nosave' section
209 */
210
Rafael J. Wysockiae83c5ee2006-09-25 23:32:45 -0700211static inline int pfn_is_nosave(unsigned long pfn)
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800212{
213 unsigned long nosave_begin_pfn = __pa(&__nosave_begin) >> PAGE_SHIFT;
214 unsigned long nosave_end_pfn = PAGE_ALIGN(__pa(&__nosave_end)) >> PAGE_SHIFT;
215 return (pfn >= nosave_begin_pfn) && (pfn < nosave_end_pfn);
216}
217
218/**
219 * saveable - Determine whether a page should be cloned or not.
220 * @pfn: The page
221 *
Rafael J. Wysockiae83c5ee2006-09-25 23:32:45 -0700222 * We save a page if it isn't Nosave, and is not in the range of pages
223 * statically defined as 'unsaveable', and it
224 * isn't a part of a free chunk of pages.
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800225 */
226
Rafael J. Wysockiae83c5ee2006-09-25 23:32:45 -0700227static struct page *saveable_page(unsigned long pfn)
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800228{
Pavel Machekde491862005-10-30 14:59:59 -0800229 struct page *page;
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800230
231 if (!pfn_valid(pfn))
Rafael J. Wysockiae83c5ee2006-09-25 23:32:45 -0700232 return NULL;
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800233
234 page = pfn_to_page(pfn);
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800235
Rafael J. Wysockiae83c5ee2006-09-25 23:32:45 -0700236 if (PageNosave(page))
237 return NULL;
238 if (PageReserved(page) && pfn_is_nosave(pfn))
239 return NULL;
240 if (PageNosaveFree(page))
241 return NULL;
242
243 return page;
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800244}
245
Rafael J. Wysocki72a97e02006-01-06 00:13:46 -0800246unsigned int count_data_pages(void)
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800247{
248 struct zone *zone;
Rafael J. Wysockiae83c5ee2006-09-25 23:32:45 -0700249 unsigned long pfn, max_zone_pfn;
Pavel Machekdc19d502005-11-07 00:58:40 -0800250 unsigned int n = 0;
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800251
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800252 for_each_zone (zone) {
253 if (is_highmem(zone))
254 continue;
255 mark_free_pages(zone);
Rafael J. Wysockiae83c5ee2006-09-25 23:32:45 -0700256 max_zone_pfn = zone->zone_start_pfn + zone->spanned_pages;
257 for (pfn = zone->zone_start_pfn; pfn < max_zone_pfn; pfn++)
258 n += !!saveable_page(pfn);
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800259 }
Rafael J. Wysockia0f49652005-10-30 14:59:57 -0800260 return n;
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800261}
262
Rafael J. Wysockif623f0d2006-09-25 23:32:49 -0700263static inline void copy_data_page(long *dst, long *src)
264{
265 int n;
266
267 /* copy_page and memcpy are not usable for copying task structs. */
268 for (n = PAGE_SIZE / sizeof(long); n; n--)
269 *dst++ = *src++;
270}
271
Rafael J. Wysockia0f49652005-10-30 14:59:57 -0800272static void copy_data_pages(struct pbe *pblist)
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800273{
274 struct zone *zone;
Rafael J. Wysockiae83c5ee2006-09-25 23:32:45 -0700275 unsigned long pfn, max_zone_pfn;
Rafael J. Wysockif623f0d2006-09-25 23:32:49 -0700276 struct pbe *pbe;
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800277
Rafael J. Wysockia0f49652005-10-30 14:59:57 -0800278 pbe = pblist;
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800279 for_each_zone (zone) {
280 if (is_highmem(zone))
281 continue;
282 mark_free_pages(zone);
Rafael J. Wysockiae83c5ee2006-09-25 23:32:45 -0700283 max_zone_pfn = zone->zone_start_pfn + zone->spanned_pages;
284 for (pfn = zone->zone_start_pfn; pfn < max_zone_pfn; pfn++) {
285 struct page *page = saveable_page(pfn);
286
287 if (page) {
Rafael J. Wysockif623f0d2006-09-25 23:32:49 -0700288 void *ptr = page_address(page);
Rafael J. Wysocki95018f72006-07-10 04:45:00 -0700289
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800290 BUG_ON(!pbe);
Rafael J. Wysockif623f0d2006-09-25 23:32:49 -0700291 copy_data_page((void *)pbe->address, ptr);
292 pbe->orig_address = (unsigned long)ptr;
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800293 pbe = pbe->next;
294 }
295 }
296 }
297 BUG_ON(pbe);
298}
299
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800300/**
301 * free_pagedir - free pages allocated with alloc_pagedir()
302 */
303
Rafael J. Wysocki4a3b98a2006-04-18 22:20:29 -0700304static void free_pagedir(struct pbe *pblist, int clear_nosave_free)
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800305{
306 struct pbe *pbe;
307
308 while (pblist) {
309 pbe = (pblist + PB_PAGE_SKIP)->next;
Rafael J. Wysockif6143aa2006-09-25 23:32:50 -0700310 free_image_page(pblist, clear_nosave_free);
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800311 pblist = pbe;
312 }
313}
314
315/**
316 * fill_pb_page - Create a list of PBEs on a given memory page
317 */
318
Rafael J. Wysockicd560bb2006-09-25 23:32:50 -0700319static inline void fill_pb_page(struct pbe *pbpage, unsigned int n)
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800320{
321 struct pbe *p;
322
323 p = pbpage;
Rafael J. Wysockicd560bb2006-09-25 23:32:50 -0700324 pbpage += n - 1;
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800325 do
326 p->next = p + 1;
327 while (++p < pbpage);
328}
329
330/**
331 * create_pbe_list - Create a list of PBEs on top of a given chain
332 * of memory pages allocated with alloc_pagedir()
Rafael J. Wysockicd560bb2006-09-25 23:32:50 -0700333 *
334 * This function assumes that pages allocated by alloc_image_page() will
335 * always be zeroed.
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800336 */
337
Rafael J. Wysocki7088a5c2006-01-06 00:13:05 -0800338static inline void create_pbe_list(struct pbe *pblist, unsigned int nr_pages)
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800339{
Rafael J. Wysockicd560bb2006-09-25 23:32:50 -0700340 struct pbe *pbpage;
Pavel Machekdc19d502005-11-07 00:58:40 -0800341 unsigned int num = PBES_PER_PAGE;
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800342
343 for_each_pb_page (pbpage, pblist) {
344 if (num >= nr_pages)
345 break;
346
Rafael J. Wysockicd560bb2006-09-25 23:32:50 -0700347 fill_pb_page(pbpage, PBES_PER_PAGE);
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800348 num += PBES_PER_PAGE;
349 }
350 if (pbpage) {
Rafael J. Wysockicd560bb2006-09-25 23:32:50 -0700351 num -= PBES_PER_PAGE;
352 fill_pb_page(pbpage, nr_pages - num);
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800353 }
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800354}
355
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800356/**
357 * alloc_pagedir - Allocate the page directory.
358 *
359 * First, determine exactly how many pages we need and
360 * allocate them.
361 *
362 * We arrange the pages in a chain: each page is an array of PBES_PER_PAGE
363 * struct pbe elements (pbes) and the last element in the page points
364 * to the next page.
365 *
366 * On each page we set up a list of struct_pbe elements.
367 */
368
Adrian Bunk7bff24e2006-06-23 02:04:47 -0700369static struct pbe *alloc_pagedir(unsigned int nr_pages, gfp_t gfp_mask,
370 int safe_needed)
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800371{
Pavel Machekdc19d502005-11-07 00:58:40 -0800372 unsigned int num;
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800373 struct pbe *pblist, *pbe;
374
375 if (!nr_pages)
376 return NULL;
377
Rafael J. Wysocki054bd4c2005-11-08 21:34:39 -0800378 pblist = alloc_image_page(gfp_mask, safe_needed);
Rafael J. Wysockicd560bb2006-09-25 23:32:50 -0700379 pbe = pblist;
380 for (num = PBES_PER_PAGE; num < nr_pages; num += PBES_PER_PAGE) {
381 if (!pbe) {
382 free_pagedir(pblist, 1);
383 return NULL;
384 }
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800385 pbe += PB_PAGE_SKIP;
Rafael J. Wysocki054bd4c2005-11-08 21:34:39 -0800386 pbe->next = alloc_image_page(gfp_mask, safe_needed);
Rafael J. Wysockicd560bb2006-09-25 23:32:50 -0700387 pbe = pbe->next;
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800388 }
Rafael J. Wysockicd560bb2006-09-25 23:32:50 -0700389 create_pbe_list(pblist, nr_pages);
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800390 return pblist;
391}
392
393/**
394 * Free pages we allocated for suspend. Suspend pages are alocated
395 * before atomic copy, so we need to free them after resume.
396 */
397
398void swsusp_free(void)
399{
400 struct zone *zone;
Rafael J. Wysockiae83c5ee2006-09-25 23:32:45 -0700401 unsigned long pfn, max_zone_pfn;
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800402
403 for_each_zone(zone) {
Rafael J. Wysockiae83c5ee2006-09-25 23:32:45 -0700404 max_zone_pfn = zone->zone_start_pfn + zone->spanned_pages;
405 for (pfn = zone->zone_start_pfn; pfn < max_zone_pfn; pfn++)
406 if (pfn_valid(pfn)) {
407 struct page *page = pfn_to_page(pfn);
408
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800409 if (PageNosave(page) && PageNosaveFree(page)) {
410 ClearPageNosave(page);
411 ClearPageNosaveFree(page);
412 free_page((long) page_address(page));
413 }
414 }
415 }
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800416 nr_copy_pages = 0;
417 nr_meta_pages = 0;
418 pagedir_nosave = NULL;
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -0800419 buffer = NULL;
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800420}
421
422
423/**
424 * enough_free_mem - Make sure we enough free memory to snapshot.
425 *
426 * Returns TRUE or FALSE after checking the number of available
427 * free pages.
428 */
429
Pavel Machekdc19d502005-11-07 00:58:40 -0800430static int enough_free_mem(unsigned int nr_pages)
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800431{
Rafael J. Wysockie5e2fa72006-01-06 00:14:20 -0800432 struct zone *zone;
433 unsigned int n = 0;
434
435 for_each_zone (zone)
436 if (!is_highmem(zone))
437 n += zone->free_pages;
438 pr_debug("swsusp: available memory: %u pages\n", n);
439 return n > (nr_pages + PAGES_FOR_IO +
Rafael J. Wysockia0f49652005-10-30 14:59:57 -0800440 (nr_pages + PBES_PER_PAGE - 1) / PBES_PER_PAGE);
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800441}
442
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800443static int alloc_data_pages(struct pbe *pblist, gfp_t gfp_mask, int safe_needed)
Rafael J. Wysocki054bd4c2005-11-08 21:34:39 -0800444{
445 struct pbe *p;
446
447 for_each_pbe (p, pblist) {
448 p->address = (unsigned long)alloc_image_page(gfp_mask, safe_needed);
449 if (!p->address)
450 return -ENOMEM;
451 }
452 return 0;
453}
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800454
Pavel Machekdc19d502005-11-07 00:58:40 -0800455static struct pbe *swsusp_alloc(unsigned int nr_pages)
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800456{
Rafael J. Wysocki054bd4c2005-11-08 21:34:39 -0800457 struct pbe *pblist;
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800458
Rafael J. Wysocki054bd4c2005-11-08 21:34:39 -0800459 if (!(pblist = alloc_pagedir(nr_pages, GFP_ATOMIC | __GFP_COLD, 0))) {
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800460 printk(KERN_ERR "suspend: Allocating pagedir failed.\n");
Rafael J. Wysockia0f49652005-10-30 14:59:57 -0800461 return NULL;
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800462 }
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800463
Rafael J. Wysocki054bd4c2005-11-08 21:34:39 -0800464 if (alloc_data_pages(pblist, GFP_ATOMIC | __GFP_COLD, 0)) {
465 printk(KERN_ERR "suspend: Allocating image pages failed.\n");
466 swsusp_free();
467 return NULL;
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800468 }
469
Rafael J. Wysockia0f49652005-10-30 14:59:57 -0800470 return pblist;
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800471}
472
Rafael J. Wysocki2e32a432005-10-30 15:00:00 -0800473asmlinkage int swsusp_save(void)
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800474{
Pavel Machekdc19d502005-11-07 00:58:40 -0800475 unsigned int nr_pages;
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800476
477 pr_debug("swsusp: critical section: \n");
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800478
479 drain_local_pages();
Rafael J. Wysockia0f49652005-10-30 14:59:57 -0800480 nr_pages = count_data_pages();
481 printk("swsusp: Need to copy %u pages\n", nr_pages);
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800482
483 pr_debug("swsusp: pages needed: %u + %lu + %u, free: %u\n",
Rafael J. Wysockia0f49652005-10-30 14:59:57 -0800484 nr_pages,
485 (nr_pages + PBES_PER_PAGE - 1) / PBES_PER_PAGE,
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800486 PAGES_FOR_IO, nr_free_pages());
487
Rafael J. Wysockia0f49652005-10-30 14:59:57 -0800488 if (!enough_free_mem(nr_pages)) {
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800489 printk(KERN_ERR "swsusp: Not enough free memory\n");
490 return -ENOMEM;
491 }
492
Rafael J. Wysockia0f49652005-10-30 14:59:57 -0800493 pagedir_nosave = swsusp_alloc(nr_pages);
494 if (!pagedir_nosave)
495 return -ENOMEM;
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800496
497 /* During allocating of suspend pagedir, new cold pages may appear.
498 * Kill them.
499 */
500 drain_local_pages();
Rafael J. Wysockia0f49652005-10-30 14:59:57 -0800501 copy_data_pages(pagedir_nosave);
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800502
503 /*
504 * End of critical section. From now on, we can write to memory,
505 * but we should not touch disk. This specially means we must _not_
506 * touch swap space! Except we must write out our image of course.
507 */
508
Rafael J. Wysockia0f49652005-10-30 14:59:57 -0800509 nr_copy_pages = nr_pages;
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800510 nr_meta_pages = (nr_pages * sizeof(long) + PAGE_SIZE - 1) >> PAGE_SHIFT;
Rafael J. Wysockia0f49652005-10-30 14:59:57 -0800511
512 printk("swsusp: critical section/: done (%d pages copied)\n", nr_pages);
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800513 return 0;
514}
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800515
516static void init_header(struct swsusp_info *info)
517{
518 memset(info, 0, sizeof(struct swsusp_info));
519 info->version_code = LINUX_VERSION_CODE;
520 info->num_physpages = num_physpages;
521 memcpy(&info->uts, &system_utsname, sizeof(system_utsname));
522 info->cpus = num_online_cpus();
523 info->image_pages = nr_copy_pages;
524 info->pages = nr_copy_pages + nr_meta_pages + 1;
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -0800525 info->size = info->pages;
526 info->size <<= PAGE_SHIFT;
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800527}
528
529/**
530 * pack_orig_addresses - the .orig_address fields of the PBEs from the
531 * list starting at @pbe are stored in the array @buf[] (1 page)
532 */
533
534static inline struct pbe *pack_orig_addresses(unsigned long *buf, struct pbe *pbe)
535{
536 int j;
537
538 for (j = 0; j < PAGE_SIZE / sizeof(long) && pbe; j++) {
539 buf[j] = pbe->orig_address;
540 pbe = pbe->next;
541 }
542 if (!pbe)
543 for (; j < PAGE_SIZE / sizeof(long); j++)
544 buf[j] = 0;
545 return pbe;
546}
547
548/**
549 * snapshot_read_next - used for reading the system memory snapshot.
550 *
551 * On the first call to it @handle should point to a zeroed
552 * snapshot_handle structure. The structure gets updated and a pointer
553 * to it should be passed to this function every next time.
554 *
555 * The @count parameter should contain the number of bytes the caller
556 * wants to read from the snapshot. It must not be zero.
557 *
558 * On success the function returns a positive number. Then, the caller
559 * is allowed to read up to the returned number of bytes from the memory
560 * location computed by the data_of() macro. The number returned
561 * may be smaller than @count, but this only happens if the read would
562 * cross a page boundary otherwise.
563 *
564 * The function returns 0 to indicate the end of data stream condition,
565 * and a negative number is returned on error. In such cases the
566 * structure pointed to by @handle is not updated and should not be used
567 * any more.
568 */
569
570int snapshot_read_next(struct snapshot_handle *handle, size_t count)
571{
Rafael J. Wysockifb13a282006-09-25 23:32:46 -0700572 if (handle->cur > nr_meta_pages + nr_copy_pages)
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800573 return 0;
574 if (!buffer) {
575 /* This makes the buffer be freed by swsusp_free() */
576 buffer = alloc_image_page(GFP_ATOMIC, 0);
577 if (!buffer)
578 return -ENOMEM;
579 }
580 if (!handle->offset) {
581 init_header((struct swsusp_info *)buffer);
582 handle->buffer = buffer;
583 handle->pbe = pagedir_nosave;
584 }
Rafael J. Wysockifb13a282006-09-25 23:32:46 -0700585 if (handle->prev < handle->cur) {
586 if (handle->cur <= nr_meta_pages) {
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800587 handle->pbe = pack_orig_addresses(buffer, handle->pbe);
588 if (!handle->pbe)
589 handle->pbe = pagedir_nosave;
590 } else {
591 handle->buffer = (void *)handle->pbe->address;
592 handle->pbe = handle->pbe->next;
593 }
Rafael J. Wysockifb13a282006-09-25 23:32:46 -0700594 handle->prev = handle->cur;
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800595 }
Rafael J. Wysockifb13a282006-09-25 23:32:46 -0700596 handle->buf_offset = handle->cur_offset;
597 if (handle->cur_offset + count >= PAGE_SIZE) {
598 count = PAGE_SIZE - handle->cur_offset;
599 handle->cur_offset = 0;
600 handle->cur++;
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800601 } else {
Rafael J. Wysockifb13a282006-09-25 23:32:46 -0700602 handle->cur_offset += count;
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800603 }
604 handle->offset += count;
605 return count;
606}
607
608/**
609 * mark_unsafe_pages - mark the pages that cannot be used for storing
610 * the image during resume, because they conflict with the pages that
611 * had been used before suspend
612 */
613
614static int mark_unsafe_pages(struct pbe *pblist)
615{
616 struct zone *zone;
Rafael J. Wysockiae83c5ee2006-09-25 23:32:45 -0700617 unsigned long pfn, max_zone_pfn;
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800618 struct pbe *p;
619
620 if (!pblist) /* a sanity check */
621 return -EINVAL;
622
623 /* Clear page flags */
624 for_each_zone (zone) {
Rafael J. Wysockiae83c5ee2006-09-25 23:32:45 -0700625 max_zone_pfn = zone->zone_start_pfn + zone->spanned_pages;
626 for (pfn = zone->zone_start_pfn; pfn < max_zone_pfn; pfn++)
627 if (pfn_valid(pfn))
628 ClearPageNosaveFree(pfn_to_page(pfn));
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800629 }
630
631 /* Mark orig addresses */
632 for_each_pbe (p, pblist) {
633 if (virt_addr_valid(p->orig_address))
634 SetPageNosaveFree(virt_to_page(p->orig_address));
635 else
636 return -EFAULT;
637 }
638
Rafael J. Wysocki968808b82006-06-23 02:04:48 -0700639 unsafe_pages = 0;
640
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800641 return 0;
642}
643
644static void copy_page_backup_list(struct pbe *dst, struct pbe *src)
645{
646 /* We assume both lists contain the same number of elements */
647 while (src) {
648 dst->orig_address = src->orig_address;
649 dst = dst->next;
650 src = src->next;
651 }
652}
653
654static int check_header(struct swsusp_info *info)
655{
656 char *reason = NULL;
657
658 if (info->version_code != LINUX_VERSION_CODE)
659 reason = "kernel version";
660 if (info->num_physpages != num_physpages)
661 reason = "memory size";
662 if (strcmp(info->uts.sysname,system_utsname.sysname))
663 reason = "system type";
664 if (strcmp(info->uts.release,system_utsname.release))
665 reason = "kernel release";
666 if (strcmp(info->uts.version,system_utsname.version))
667 reason = "version";
668 if (strcmp(info->uts.machine,system_utsname.machine))
669 reason = "machine";
670 if (reason) {
671 printk(KERN_ERR "swsusp: Resume mismatch: %s\n", reason);
672 return -EPERM;
673 }
674 return 0;
675}
676
677/**
678 * load header - check the image header and copy data from it
679 */
680
681static int load_header(struct snapshot_handle *handle,
682 struct swsusp_info *info)
683{
684 int error;
685 struct pbe *pblist;
686
687 error = check_header(info);
688 if (!error) {
689 pblist = alloc_pagedir(info->image_pages, GFP_ATOMIC, 0);
690 if (!pblist)
691 return -ENOMEM;
692 pagedir_nosave = pblist;
693 handle->pbe = pblist;
694 nr_copy_pages = info->image_pages;
695 nr_meta_pages = info->pages - info->image_pages - 1;
696 }
697 return error;
698}
699
700/**
701 * unpack_orig_addresses - copy the elements of @buf[] (1 page) to
702 * the PBEs in the list starting at @pbe
703 */
704
705static inline struct pbe *unpack_orig_addresses(unsigned long *buf,
706 struct pbe *pbe)
707{
708 int j;
709
710 for (j = 0; j < PAGE_SIZE / sizeof(long) && pbe; j++) {
711 pbe->orig_address = buf[j];
712 pbe = pbe->next;
713 }
714 return pbe;
715}
716
717/**
Rafael J. Wysocki968808b82006-06-23 02:04:48 -0700718 * prepare_image - use metadata contained in the PBE list
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800719 * pointed to by pagedir_nosave to mark the pages that will
720 * be overwritten in the process of restoring the system
Rafael J. Wysocki968808b82006-06-23 02:04:48 -0700721 * memory state from the image ("unsafe" pages) and allocate
722 * memory for the image
723 *
724 * The idea is to allocate the PBE list first and then
725 * allocate as many pages as it's needed for the image data,
726 * but not to assign these pages to the PBEs initially.
727 * Instead, we just mark them as allocated and create a list
728 * of "safe" which will be used later
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800729 */
730
Rafael J. Wysocki968808b82006-06-23 02:04:48 -0700731struct safe_page {
732 struct safe_page *next;
733 char padding[PAGE_SIZE - sizeof(void *)];
734};
735
736static struct safe_page *safe_pages;
737
738static int prepare_image(struct snapshot_handle *handle)
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800739{
740 int error = 0;
Rafael J. Wysocki968808b82006-06-23 02:04:48 -0700741 unsigned int nr_pages = nr_copy_pages;
742 struct pbe *p, *pblist = NULL;
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800743
744 p = pagedir_nosave;
745 error = mark_unsafe_pages(p);
746 if (!error) {
Rafael J. Wysocki968808b82006-06-23 02:04:48 -0700747 pblist = alloc_pagedir(nr_pages, GFP_ATOMIC, 1);
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800748 if (pblist)
749 copy_page_backup_list(pblist, p);
Rafael J. Wysocki4a3b98a2006-04-18 22:20:29 -0700750 free_pagedir(p, 0);
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800751 if (!pblist)
752 error = -ENOMEM;
753 }
Rafael J. Wysocki968808b82006-06-23 02:04:48 -0700754 safe_pages = NULL;
755 if (!error && nr_pages > unsafe_pages) {
756 nr_pages -= unsafe_pages;
757 while (nr_pages--) {
758 struct safe_page *ptr;
759
760 ptr = (struct safe_page *)get_zeroed_page(GFP_ATOMIC);
761 if (!ptr) {
762 error = -ENOMEM;
763 break;
764 }
765 if (!PageNosaveFree(virt_to_page(ptr))) {
766 /* The page is "safe", add it to the list */
767 ptr->next = safe_pages;
768 safe_pages = ptr;
769 }
770 /* Mark the page as allocated */
771 SetPageNosave(virt_to_page(ptr));
772 SetPageNosaveFree(virt_to_page(ptr));
773 }
774 }
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800775 if (!error) {
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800776 pagedir_nosave = pblist;
777 } else {
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800778 handle->pbe = NULL;
Rafael J. Wysocki968808b82006-06-23 02:04:48 -0700779 swsusp_free();
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800780 }
781 return error;
782}
783
Rafael J. Wysocki968808b82006-06-23 02:04:48 -0700784static void *get_buffer(struct snapshot_handle *handle)
785{
786 struct pbe *pbe = handle->pbe, *last = handle->last_pbe;
787 struct page *page = virt_to_page(pbe->orig_address);
788
789 if (PageNosave(page) && PageNosaveFree(page)) {
790 /*
791 * We have allocated the "original" page frame and we can
792 * use it directly to store the read page
793 */
794 pbe->address = 0;
795 if (last && last->next)
796 last->next = NULL;
797 return (void *)pbe->orig_address;
798 }
799 /*
800 * The "original" page frame has not been allocated and we have to
801 * use a "safe" page frame to store the read page
802 */
803 pbe->address = (unsigned long)safe_pages;
804 safe_pages = safe_pages->next;
805 if (last)
806 last->next = pbe;
807 handle->last_pbe = pbe;
808 return (void *)pbe->address;
809}
810
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800811/**
812 * snapshot_write_next - used for writing the system memory snapshot.
813 *
814 * On the first call to it @handle should point to a zeroed
815 * snapshot_handle structure. The structure gets updated and a pointer
816 * to it should be passed to this function every next time.
817 *
818 * The @count parameter should contain the number of bytes the caller
819 * wants to write to the image. It must not be zero.
820 *
821 * On success the function returns a positive number. Then, the caller
822 * is allowed to write up to the returned number of bytes to the memory
823 * location computed by the data_of() macro. The number returned
824 * may be smaller than @count, but this only happens if the write would
825 * cross a page boundary otherwise.
826 *
827 * The function returns 0 to indicate the "end of file" condition,
828 * and a negative number is returned on error. In such cases the
829 * structure pointed to by @handle is not updated and should not be used
830 * any more.
831 */
832
833int snapshot_write_next(struct snapshot_handle *handle, size_t count)
834{
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800835 int error = 0;
836
Rafael J. Wysockifb13a282006-09-25 23:32:46 -0700837 if (handle->prev && handle->cur > nr_meta_pages + nr_copy_pages)
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800838 return 0;
839 if (!buffer) {
840 /* This makes the buffer be freed by swsusp_free() */
841 buffer = alloc_image_page(GFP_ATOMIC, 0);
842 if (!buffer)
843 return -ENOMEM;
844 }
845 if (!handle->offset)
846 handle->buffer = buffer;
Andrew Morton546e0d22006-09-25 23:32:44 -0700847 handle->sync_read = 1;
Rafael J. Wysockifb13a282006-09-25 23:32:46 -0700848 if (handle->prev < handle->cur) {
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800849 if (!handle->prev) {
Andrew Morton546e0d22006-09-25 23:32:44 -0700850 error = load_header(handle,
851 (struct swsusp_info *)buffer);
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800852 if (error)
853 return error;
854 } else if (handle->prev <= nr_meta_pages) {
Andrew Morton546e0d22006-09-25 23:32:44 -0700855 handle->pbe = unpack_orig_addresses(buffer,
856 handle->pbe);
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800857 if (!handle->pbe) {
Rafael J. Wysocki968808b82006-06-23 02:04:48 -0700858 error = prepare_image(handle);
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800859 if (error)
860 return error;
861 handle->pbe = pagedir_nosave;
Rafael J. Wysocki968808b82006-06-23 02:04:48 -0700862 handle->last_pbe = NULL;
863 handle->buffer = get_buffer(handle);
Andrew Morton546e0d22006-09-25 23:32:44 -0700864 handle->sync_read = 0;
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800865 }
866 } else {
867 handle->pbe = handle->pbe->next;
Rafael J. Wysocki968808b82006-06-23 02:04:48 -0700868 handle->buffer = get_buffer(handle);
Andrew Morton546e0d22006-09-25 23:32:44 -0700869 handle->sync_read = 0;
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800870 }
Rafael J. Wysockifb13a282006-09-25 23:32:46 -0700871 handle->prev = handle->cur;
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800872 }
Rafael J. Wysockifb13a282006-09-25 23:32:46 -0700873 handle->buf_offset = handle->cur_offset;
874 if (handle->cur_offset + count >= PAGE_SIZE) {
875 count = PAGE_SIZE - handle->cur_offset;
876 handle->cur_offset = 0;
877 handle->cur++;
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800878 } else {
Rafael J. Wysockifb13a282006-09-25 23:32:46 -0700879 handle->cur_offset += count;
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800880 }
881 handle->offset += count;
882 return count;
883}
884
885int snapshot_image_loaded(struct snapshot_handle *handle)
886{
887 return !(!handle->pbe || handle->pbe->next || !nr_copy_pages ||
Rafael J. Wysockifb13a282006-09-25 23:32:46 -0700888 handle->cur <= nr_meta_pages + nr_copy_pages);
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800889}