blob: 4afb7900002bafc7e8d07a9db3c141db4dfa975a [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. Wysockiae83c5ee2006-09-25 23:32:45 -0700159static inline int pfn_is_nosave(unsigned long pfn)
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800160{
161 unsigned long nosave_begin_pfn = __pa(&__nosave_begin) >> PAGE_SHIFT;
162 unsigned long nosave_end_pfn = PAGE_ALIGN(__pa(&__nosave_end)) >> PAGE_SHIFT;
163 return (pfn >= nosave_begin_pfn) && (pfn < nosave_end_pfn);
164}
165
166/**
167 * saveable - Determine whether a page should be cloned or not.
168 * @pfn: The page
169 *
Rafael J. Wysockiae83c5ee2006-09-25 23:32:45 -0700170 * We save a page if it isn't Nosave, and is not in the range of pages
171 * statically defined as 'unsaveable', and it
172 * isn't a part of a free chunk of pages.
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800173 */
174
Rafael J. Wysockiae83c5ee2006-09-25 23:32:45 -0700175static struct page *saveable_page(unsigned long pfn)
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800176{
Pavel Machekde491862005-10-30 14:59:59 -0800177 struct page *page;
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800178
179 if (!pfn_valid(pfn))
Rafael J. Wysockiae83c5ee2006-09-25 23:32:45 -0700180 return NULL;
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800181
182 page = pfn_to_page(pfn);
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800183
Rafael J. Wysockiae83c5ee2006-09-25 23:32:45 -0700184 if (PageNosave(page))
185 return NULL;
186 if (PageReserved(page) && pfn_is_nosave(pfn))
187 return NULL;
188 if (PageNosaveFree(page))
189 return NULL;
190
191 return page;
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800192}
193
Rafael J. Wysocki72a97e02006-01-06 00:13:46 -0800194unsigned int count_data_pages(void)
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800195{
196 struct zone *zone;
Rafael J. Wysockiae83c5ee2006-09-25 23:32:45 -0700197 unsigned long pfn, max_zone_pfn;
Pavel Machekdc19d502005-11-07 00:58:40 -0800198 unsigned int n = 0;
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800199
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800200 for_each_zone (zone) {
201 if (is_highmem(zone))
202 continue;
203 mark_free_pages(zone);
Rafael J. Wysockiae83c5ee2006-09-25 23:32:45 -0700204 max_zone_pfn = zone->zone_start_pfn + zone->spanned_pages;
205 for (pfn = zone->zone_start_pfn; pfn < max_zone_pfn; pfn++)
206 n += !!saveable_page(pfn);
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800207 }
Rafael J. Wysockia0f49652005-10-30 14:59:57 -0800208 return n;
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800209}
210
Rafael J. Wysockif623f0d2006-09-25 23:32:49 -0700211static inline void copy_data_page(long *dst, long *src)
212{
213 int n;
214
215 /* copy_page and memcpy are not usable for copying task structs. */
216 for (n = PAGE_SIZE / sizeof(long); n; n--)
217 *dst++ = *src++;
218}
219
Rafael J. Wysockia0f49652005-10-30 14:59:57 -0800220static void copy_data_pages(struct pbe *pblist)
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800221{
222 struct zone *zone;
Rafael J. Wysockiae83c5ee2006-09-25 23:32:45 -0700223 unsigned long pfn, max_zone_pfn;
Rafael J. Wysockif623f0d2006-09-25 23:32:49 -0700224 struct pbe *pbe;
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800225
Rafael J. Wysockia0f49652005-10-30 14:59:57 -0800226 pbe = pblist;
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800227 for_each_zone (zone) {
228 if (is_highmem(zone))
229 continue;
230 mark_free_pages(zone);
Rafael J. Wysockiae83c5ee2006-09-25 23:32:45 -0700231 max_zone_pfn = zone->zone_start_pfn + zone->spanned_pages;
232 for (pfn = zone->zone_start_pfn; pfn < max_zone_pfn; pfn++) {
233 struct page *page = saveable_page(pfn);
234
235 if (page) {
Rafael J. Wysockif623f0d2006-09-25 23:32:49 -0700236 void *ptr = page_address(page);
Rafael J. Wysocki95018f72006-07-10 04:45:00 -0700237
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800238 BUG_ON(!pbe);
Rafael J. Wysockif623f0d2006-09-25 23:32:49 -0700239 copy_data_page((void *)pbe->address, ptr);
240 pbe->orig_address = (unsigned long)ptr;
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800241 pbe = pbe->next;
242 }
243 }
244 }
245 BUG_ON(pbe);
246}
247
248
249/**
250 * free_pagedir - free pages allocated with alloc_pagedir()
251 */
252
Rafael J. Wysocki4a3b98a2006-04-18 22:20:29 -0700253static void free_pagedir(struct pbe *pblist, int clear_nosave_free)
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800254{
255 struct pbe *pbe;
256
257 while (pblist) {
258 pbe = (pblist + PB_PAGE_SKIP)->next;
259 ClearPageNosave(virt_to_page(pblist));
Rafael J. Wysocki4a3b98a2006-04-18 22:20:29 -0700260 if (clear_nosave_free)
261 ClearPageNosaveFree(virt_to_page(pblist));
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800262 free_page((unsigned long)pblist);
263 pblist = pbe;
264 }
265}
266
267/**
268 * fill_pb_page - Create a list of PBEs on a given memory page
269 */
270
271static inline void fill_pb_page(struct pbe *pbpage)
272{
273 struct pbe *p;
274
275 p = pbpage;
276 pbpage += PB_PAGE_SKIP;
277 do
278 p->next = p + 1;
279 while (++p < pbpage);
280}
281
282/**
283 * create_pbe_list - Create a list of PBEs on top of a given chain
284 * of memory pages allocated with alloc_pagedir()
285 */
286
Rafael J. Wysocki7088a5c2006-01-06 00:13:05 -0800287static inline void create_pbe_list(struct pbe *pblist, unsigned int nr_pages)
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800288{
289 struct pbe *pbpage, *p;
Pavel Machekdc19d502005-11-07 00:58:40 -0800290 unsigned int num = PBES_PER_PAGE;
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800291
292 for_each_pb_page (pbpage, pblist) {
293 if (num >= nr_pages)
294 break;
295
296 fill_pb_page(pbpage);
297 num += PBES_PER_PAGE;
298 }
299 if (pbpage) {
300 for (num -= PBES_PER_PAGE - 1, p = pbpage; num < nr_pages; p++, num++)
301 p->next = p + 1;
302 p->next = NULL;
303 }
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800304}
305
Rafael J. Wysocki968808b82006-06-23 02:04:48 -0700306static unsigned int unsafe_pages;
Rafael J. Wysocki72a97e02006-01-06 00:13:46 -0800307
308/**
Rafael J. Wysocki054bd4c2005-11-08 21:34:39 -0800309 * @safe_needed - on resume, for storing the PBE list and the image,
310 * we can only use memory pages that do not conflict with the pages
Rafael J. Wysocki968808b82006-06-23 02:04:48 -0700311 * used before suspend.
Rafael J. Wysocki054bd4c2005-11-08 21:34:39 -0800312 *
313 * The unsafe pages are marked with the PG_nosave_free flag
Rafael J. Wysocki968808b82006-06-23 02:04:48 -0700314 * and we count them using unsafe_pages
Rafael J. Wysocki054bd4c2005-11-08 21:34:39 -0800315 */
316
Andrew Morton546e0d22006-09-25 23:32:44 -0700317static void *alloc_image_page(gfp_t gfp_mask, int safe_needed)
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800318{
Rafael J. Wysocki054bd4c2005-11-08 21:34:39 -0800319 void *res;
320
Rafael J. Wysocki968808b82006-06-23 02:04:48 -0700321 res = (void *)get_zeroed_page(gfp_mask);
Rafael J. Wysocki054bd4c2005-11-08 21:34:39 -0800322 if (safe_needed)
Rafael J. Wysocki968808b82006-06-23 02:04:48 -0700323 while (res && PageNosaveFree(virt_to_page(res))) {
324 /* The page is unsafe, mark it for swsusp_free() */
325 SetPageNosave(virt_to_page(res));
326 unsafe_pages++;
Rafael J. Wysocki054bd4c2005-11-08 21:34:39 -0800327 res = (void *)get_zeroed_page(gfp_mask);
Rafael J. Wysocki968808b82006-06-23 02:04:48 -0700328 }
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800329 if (res) {
330 SetPageNosave(virt_to_page(res));
331 SetPageNosaveFree(virt_to_page(res));
332 }
333 return res;
334}
335
Rafael J. Wysocki054bd4c2005-11-08 21:34:39 -0800336unsigned long get_safe_page(gfp_t gfp_mask)
337{
338 return (unsigned long)alloc_image_page(gfp_mask, 1);
339}
340
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800341/**
342 * alloc_pagedir - Allocate the page directory.
343 *
344 * First, determine exactly how many pages we need and
345 * allocate them.
346 *
347 * We arrange the pages in a chain: each page is an array of PBES_PER_PAGE
348 * struct pbe elements (pbes) and the last element in the page points
349 * to the next page.
350 *
351 * On each page we set up a list of struct_pbe elements.
352 */
353
Adrian Bunk7bff24e2006-06-23 02:04:47 -0700354static struct pbe *alloc_pagedir(unsigned int nr_pages, gfp_t gfp_mask,
355 int safe_needed)
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800356{
Pavel Machekdc19d502005-11-07 00:58:40 -0800357 unsigned int num;
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800358 struct pbe *pblist, *pbe;
359
360 if (!nr_pages)
361 return NULL;
362
Rafael J. Wysocki054bd4c2005-11-08 21:34:39 -0800363 pblist = alloc_image_page(gfp_mask, safe_needed);
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800364 /* FIXME: rewrite this ugly loop */
365 for (pbe = pblist, num = PBES_PER_PAGE; pbe && num < nr_pages;
366 pbe = pbe->next, num += PBES_PER_PAGE) {
367 pbe += PB_PAGE_SKIP;
Rafael J. Wysocki054bd4c2005-11-08 21:34:39 -0800368 pbe->next = alloc_image_page(gfp_mask, safe_needed);
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800369 }
370 if (!pbe) { /* get_zeroed_page() failed */
Rafael J. Wysocki4a3b98a2006-04-18 22:20:29 -0700371 free_pagedir(pblist, 1);
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800372 pblist = NULL;
Rafael J. Wysocki7088a5c2006-01-06 00:13:05 -0800373 } else
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -0800374 create_pbe_list(pblist, nr_pages);
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800375 return pblist;
376}
377
378/**
379 * Free pages we allocated for suspend. Suspend pages are alocated
380 * before atomic copy, so we need to free them after resume.
381 */
382
383void swsusp_free(void)
384{
385 struct zone *zone;
Rafael J. Wysockiae83c5ee2006-09-25 23:32:45 -0700386 unsigned long pfn, max_zone_pfn;
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800387
388 for_each_zone(zone) {
Rafael J. Wysockiae83c5ee2006-09-25 23:32:45 -0700389 max_zone_pfn = zone->zone_start_pfn + zone->spanned_pages;
390 for (pfn = zone->zone_start_pfn; pfn < max_zone_pfn; pfn++)
391 if (pfn_valid(pfn)) {
392 struct page *page = pfn_to_page(pfn);
393
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800394 if (PageNosave(page) && PageNosaveFree(page)) {
395 ClearPageNosave(page);
396 ClearPageNosaveFree(page);
397 free_page((long) page_address(page));
398 }
399 }
400 }
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800401 nr_copy_pages = 0;
402 nr_meta_pages = 0;
403 pagedir_nosave = NULL;
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -0800404 buffer = NULL;
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800405}
406
407
408/**
409 * enough_free_mem - Make sure we enough free memory to snapshot.
410 *
411 * Returns TRUE or FALSE after checking the number of available
412 * free pages.
413 */
414
Pavel Machekdc19d502005-11-07 00:58:40 -0800415static int enough_free_mem(unsigned int nr_pages)
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800416{
Rafael J. Wysockie5e2fa72006-01-06 00:14:20 -0800417 struct zone *zone;
418 unsigned int n = 0;
419
420 for_each_zone (zone)
421 if (!is_highmem(zone))
422 n += zone->free_pages;
423 pr_debug("swsusp: available memory: %u pages\n", n);
424 return n > (nr_pages + PAGES_FOR_IO +
Rafael J. Wysockia0f49652005-10-30 14:59:57 -0800425 (nr_pages + PBES_PER_PAGE - 1) / PBES_PER_PAGE);
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800426}
427
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800428static int alloc_data_pages(struct pbe *pblist, gfp_t gfp_mask, int safe_needed)
Rafael J. Wysocki054bd4c2005-11-08 21:34:39 -0800429{
430 struct pbe *p;
431
432 for_each_pbe (p, pblist) {
433 p->address = (unsigned long)alloc_image_page(gfp_mask, safe_needed);
434 if (!p->address)
435 return -ENOMEM;
436 }
437 return 0;
438}
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800439
Pavel Machekdc19d502005-11-07 00:58:40 -0800440static struct pbe *swsusp_alloc(unsigned int nr_pages)
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800441{
Rafael J. Wysocki054bd4c2005-11-08 21:34:39 -0800442 struct pbe *pblist;
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800443
Rafael J. Wysocki054bd4c2005-11-08 21:34:39 -0800444 if (!(pblist = alloc_pagedir(nr_pages, GFP_ATOMIC | __GFP_COLD, 0))) {
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800445 printk(KERN_ERR "suspend: Allocating pagedir failed.\n");
Rafael J. Wysockia0f49652005-10-30 14:59:57 -0800446 return NULL;
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800447 }
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800448
Rafael J. Wysocki054bd4c2005-11-08 21:34:39 -0800449 if (alloc_data_pages(pblist, GFP_ATOMIC | __GFP_COLD, 0)) {
450 printk(KERN_ERR "suspend: Allocating image pages failed.\n");
451 swsusp_free();
452 return NULL;
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800453 }
454
Rafael J. Wysockia0f49652005-10-30 14:59:57 -0800455 return pblist;
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800456}
457
Rafael J. Wysocki2e32a432005-10-30 15:00:00 -0800458asmlinkage int swsusp_save(void)
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800459{
Pavel Machekdc19d502005-11-07 00:58:40 -0800460 unsigned int nr_pages;
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800461
462 pr_debug("swsusp: critical section: \n");
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800463
464 drain_local_pages();
Rafael J. Wysockia0f49652005-10-30 14:59:57 -0800465 nr_pages = count_data_pages();
466 printk("swsusp: Need to copy %u pages\n", nr_pages);
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800467
468 pr_debug("swsusp: pages needed: %u + %lu + %u, free: %u\n",
Rafael J. Wysockia0f49652005-10-30 14:59:57 -0800469 nr_pages,
470 (nr_pages + PBES_PER_PAGE - 1) / PBES_PER_PAGE,
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800471 PAGES_FOR_IO, nr_free_pages());
472
Rafael J. Wysockia0f49652005-10-30 14:59:57 -0800473 if (!enough_free_mem(nr_pages)) {
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800474 printk(KERN_ERR "swsusp: Not enough free memory\n");
475 return -ENOMEM;
476 }
477
Rafael J. Wysockia0f49652005-10-30 14:59:57 -0800478 pagedir_nosave = swsusp_alloc(nr_pages);
479 if (!pagedir_nosave)
480 return -ENOMEM;
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800481
482 /* During allocating of suspend pagedir, new cold pages may appear.
483 * Kill them.
484 */
485 drain_local_pages();
Rafael J. Wysockia0f49652005-10-30 14:59:57 -0800486 copy_data_pages(pagedir_nosave);
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800487
488 /*
489 * End of critical section. From now on, we can write to memory,
490 * but we should not touch disk. This specially means we must _not_
491 * touch swap space! Except we must write out our image of course.
492 */
493
Rafael J. Wysockia0f49652005-10-30 14:59:57 -0800494 nr_copy_pages = nr_pages;
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800495 nr_meta_pages = (nr_pages * sizeof(long) + PAGE_SIZE - 1) >> PAGE_SHIFT;
Rafael J. Wysockia0f49652005-10-30 14:59:57 -0800496
497 printk("swsusp: critical section/: done (%d pages copied)\n", nr_pages);
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800498 return 0;
499}
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800500
501static void init_header(struct swsusp_info *info)
502{
503 memset(info, 0, sizeof(struct swsusp_info));
504 info->version_code = LINUX_VERSION_CODE;
505 info->num_physpages = num_physpages;
506 memcpy(&info->uts, &system_utsname, sizeof(system_utsname));
507 info->cpus = num_online_cpus();
508 info->image_pages = nr_copy_pages;
509 info->pages = nr_copy_pages + nr_meta_pages + 1;
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -0800510 info->size = info->pages;
511 info->size <<= PAGE_SHIFT;
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800512}
513
514/**
515 * pack_orig_addresses - the .orig_address fields of the PBEs from the
516 * list starting at @pbe are stored in the array @buf[] (1 page)
517 */
518
519static inline struct pbe *pack_orig_addresses(unsigned long *buf, struct pbe *pbe)
520{
521 int j;
522
523 for (j = 0; j < PAGE_SIZE / sizeof(long) && pbe; j++) {
524 buf[j] = pbe->orig_address;
525 pbe = pbe->next;
526 }
527 if (!pbe)
528 for (; j < PAGE_SIZE / sizeof(long); j++)
529 buf[j] = 0;
530 return pbe;
531}
532
533/**
534 * snapshot_read_next - used for reading the system memory snapshot.
535 *
536 * On the first call to it @handle should point to a zeroed
537 * snapshot_handle structure. The structure gets updated and a pointer
538 * to it should be passed to this function every next time.
539 *
540 * The @count parameter should contain the number of bytes the caller
541 * wants to read from the snapshot. It must not be zero.
542 *
543 * On success the function returns a positive number. Then, the caller
544 * is allowed to read up to the returned number of bytes from the memory
545 * location computed by the data_of() macro. The number returned
546 * may be smaller than @count, but this only happens if the read would
547 * cross a page boundary otherwise.
548 *
549 * The function returns 0 to indicate the end of data stream condition,
550 * and a negative number is returned on error. In such cases the
551 * structure pointed to by @handle is not updated and should not be used
552 * any more.
553 */
554
555int snapshot_read_next(struct snapshot_handle *handle, size_t count)
556{
Rafael J. Wysockifb13a282006-09-25 23:32:46 -0700557 if (handle->cur > nr_meta_pages + nr_copy_pages)
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800558 return 0;
559 if (!buffer) {
560 /* This makes the buffer be freed by swsusp_free() */
561 buffer = alloc_image_page(GFP_ATOMIC, 0);
562 if (!buffer)
563 return -ENOMEM;
564 }
565 if (!handle->offset) {
566 init_header((struct swsusp_info *)buffer);
567 handle->buffer = buffer;
568 handle->pbe = pagedir_nosave;
569 }
Rafael J. Wysockifb13a282006-09-25 23:32:46 -0700570 if (handle->prev < handle->cur) {
571 if (handle->cur <= nr_meta_pages) {
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800572 handle->pbe = pack_orig_addresses(buffer, handle->pbe);
573 if (!handle->pbe)
574 handle->pbe = pagedir_nosave;
575 } else {
576 handle->buffer = (void *)handle->pbe->address;
577 handle->pbe = handle->pbe->next;
578 }
Rafael J. Wysockifb13a282006-09-25 23:32:46 -0700579 handle->prev = handle->cur;
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800580 }
Rafael J. Wysockifb13a282006-09-25 23:32:46 -0700581 handle->buf_offset = handle->cur_offset;
582 if (handle->cur_offset + count >= PAGE_SIZE) {
583 count = PAGE_SIZE - handle->cur_offset;
584 handle->cur_offset = 0;
585 handle->cur++;
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800586 } else {
Rafael J. Wysockifb13a282006-09-25 23:32:46 -0700587 handle->cur_offset += count;
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800588 }
589 handle->offset += count;
590 return count;
591}
592
593/**
594 * mark_unsafe_pages - mark the pages that cannot be used for storing
595 * the image during resume, because they conflict with the pages that
596 * had been used before suspend
597 */
598
599static int mark_unsafe_pages(struct pbe *pblist)
600{
601 struct zone *zone;
Rafael J. Wysockiae83c5ee2006-09-25 23:32:45 -0700602 unsigned long pfn, max_zone_pfn;
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800603 struct pbe *p;
604
605 if (!pblist) /* a sanity check */
606 return -EINVAL;
607
608 /* Clear page flags */
609 for_each_zone (zone) {
Rafael J. Wysockiae83c5ee2006-09-25 23:32:45 -0700610 max_zone_pfn = zone->zone_start_pfn + zone->spanned_pages;
611 for (pfn = zone->zone_start_pfn; pfn < max_zone_pfn; pfn++)
612 if (pfn_valid(pfn))
613 ClearPageNosaveFree(pfn_to_page(pfn));
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800614 }
615
616 /* Mark orig addresses */
617 for_each_pbe (p, pblist) {
618 if (virt_addr_valid(p->orig_address))
619 SetPageNosaveFree(virt_to_page(p->orig_address));
620 else
621 return -EFAULT;
622 }
623
Rafael J. Wysocki968808b82006-06-23 02:04:48 -0700624 unsafe_pages = 0;
625
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800626 return 0;
627}
628
629static void copy_page_backup_list(struct pbe *dst, struct pbe *src)
630{
631 /* We assume both lists contain the same number of elements */
632 while (src) {
633 dst->orig_address = src->orig_address;
634 dst = dst->next;
635 src = src->next;
636 }
637}
638
639static int check_header(struct swsusp_info *info)
640{
641 char *reason = NULL;
642
643 if (info->version_code != LINUX_VERSION_CODE)
644 reason = "kernel version";
645 if (info->num_physpages != num_physpages)
646 reason = "memory size";
647 if (strcmp(info->uts.sysname,system_utsname.sysname))
648 reason = "system type";
649 if (strcmp(info->uts.release,system_utsname.release))
650 reason = "kernel release";
651 if (strcmp(info->uts.version,system_utsname.version))
652 reason = "version";
653 if (strcmp(info->uts.machine,system_utsname.machine))
654 reason = "machine";
655 if (reason) {
656 printk(KERN_ERR "swsusp: Resume mismatch: %s\n", reason);
657 return -EPERM;
658 }
659 return 0;
660}
661
662/**
663 * load header - check the image header and copy data from it
664 */
665
666static int load_header(struct snapshot_handle *handle,
667 struct swsusp_info *info)
668{
669 int error;
670 struct pbe *pblist;
671
672 error = check_header(info);
673 if (!error) {
674 pblist = alloc_pagedir(info->image_pages, GFP_ATOMIC, 0);
675 if (!pblist)
676 return -ENOMEM;
677 pagedir_nosave = pblist;
678 handle->pbe = pblist;
679 nr_copy_pages = info->image_pages;
680 nr_meta_pages = info->pages - info->image_pages - 1;
681 }
682 return error;
683}
684
685/**
686 * unpack_orig_addresses - copy the elements of @buf[] (1 page) to
687 * the PBEs in the list starting at @pbe
688 */
689
690static inline struct pbe *unpack_orig_addresses(unsigned long *buf,
691 struct pbe *pbe)
692{
693 int j;
694
695 for (j = 0; j < PAGE_SIZE / sizeof(long) && pbe; j++) {
696 pbe->orig_address = buf[j];
697 pbe = pbe->next;
698 }
699 return pbe;
700}
701
702/**
Rafael J. Wysocki968808b82006-06-23 02:04:48 -0700703 * prepare_image - use metadata contained in the PBE list
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800704 * pointed to by pagedir_nosave to mark the pages that will
705 * be overwritten in the process of restoring the system
Rafael J. Wysocki968808b82006-06-23 02:04:48 -0700706 * memory state from the image ("unsafe" pages) and allocate
707 * memory for the image
708 *
709 * The idea is to allocate the PBE list first and then
710 * allocate as many pages as it's needed for the image data,
711 * but not to assign these pages to the PBEs initially.
712 * Instead, we just mark them as allocated and create a list
713 * of "safe" which will be used later
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800714 */
715
Rafael J. Wysocki968808b82006-06-23 02:04:48 -0700716struct safe_page {
717 struct safe_page *next;
718 char padding[PAGE_SIZE - sizeof(void *)];
719};
720
721static struct safe_page *safe_pages;
722
723static int prepare_image(struct snapshot_handle *handle)
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800724{
725 int error = 0;
Rafael J. Wysocki968808b82006-06-23 02:04:48 -0700726 unsigned int nr_pages = nr_copy_pages;
727 struct pbe *p, *pblist = NULL;
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800728
729 p = pagedir_nosave;
730 error = mark_unsafe_pages(p);
731 if (!error) {
Rafael J. Wysocki968808b82006-06-23 02:04:48 -0700732 pblist = alloc_pagedir(nr_pages, GFP_ATOMIC, 1);
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800733 if (pblist)
734 copy_page_backup_list(pblist, p);
Rafael J. Wysocki4a3b98a2006-04-18 22:20:29 -0700735 free_pagedir(p, 0);
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800736 if (!pblist)
737 error = -ENOMEM;
738 }
Rafael J. Wysocki968808b82006-06-23 02:04:48 -0700739 safe_pages = NULL;
740 if (!error && nr_pages > unsafe_pages) {
741 nr_pages -= unsafe_pages;
742 while (nr_pages--) {
743 struct safe_page *ptr;
744
745 ptr = (struct safe_page *)get_zeroed_page(GFP_ATOMIC);
746 if (!ptr) {
747 error = -ENOMEM;
748 break;
749 }
750 if (!PageNosaveFree(virt_to_page(ptr))) {
751 /* The page is "safe", add it to the list */
752 ptr->next = safe_pages;
753 safe_pages = ptr;
754 }
755 /* Mark the page as allocated */
756 SetPageNosave(virt_to_page(ptr));
757 SetPageNosaveFree(virt_to_page(ptr));
758 }
759 }
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800760 if (!error) {
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800761 pagedir_nosave = pblist;
762 } else {
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800763 handle->pbe = NULL;
Rafael J. Wysocki968808b82006-06-23 02:04:48 -0700764 swsusp_free();
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800765 }
766 return error;
767}
768
Rafael J. Wysocki968808b82006-06-23 02:04:48 -0700769static void *get_buffer(struct snapshot_handle *handle)
770{
771 struct pbe *pbe = handle->pbe, *last = handle->last_pbe;
772 struct page *page = virt_to_page(pbe->orig_address);
773
774 if (PageNosave(page) && PageNosaveFree(page)) {
775 /*
776 * We have allocated the "original" page frame and we can
777 * use it directly to store the read page
778 */
779 pbe->address = 0;
780 if (last && last->next)
781 last->next = NULL;
782 return (void *)pbe->orig_address;
783 }
784 /*
785 * The "original" page frame has not been allocated and we have to
786 * use a "safe" page frame to store the read page
787 */
788 pbe->address = (unsigned long)safe_pages;
789 safe_pages = safe_pages->next;
790 if (last)
791 last->next = pbe;
792 handle->last_pbe = pbe;
793 return (void *)pbe->address;
794}
795
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800796/**
797 * snapshot_write_next - used for writing the system memory snapshot.
798 *
799 * On the first call to it @handle should point to a zeroed
800 * snapshot_handle structure. The structure gets updated and a pointer
801 * to it should be passed to this function every next time.
802 *
803 * The @count parameter should contain the number of bytes the caller
804 * wants to write to the image. It must not be zero.
805 *
806 * On success the function returns a positive number. Then, the caller
807 * is allowed to write up to the returned number of bytes to the memory
808 * location computed by the data_of() macro. The number returned
809 * may be smaller than @count, but this only happens if the write would
810 * cross a page boundary otherwise.
811 *
812 * The function returns 0 to indicate the "end of file" condition,
813 * and a negative number is returned on error. In such cases the
814 * structure pointed to by @handle is not updated and should not be used
815 * any more.
816 */
817
818int snapshot_write_next(struct snapshot_handle *handle, size_t count)
819{
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800820 int error = 0;
821
Rafael J. Wysockifb13a282006-09-25 23:32:46 -0700822 if (handle->prev && handle->cur > nr_meta_pages + nr_copy_pages)
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800823 return 0;
824 if (!buffer) {
825 /* This makes the buffer be freed by swsusp_free() */
826 buffer = alloc_image_page(GFP_ATOMIC, 0);
827 if (!buffer)
828 return -ENOMEM;
829 }
830 if (!handle->offset)
831 handle->buffer = buffer;
Andrew Morton546e0d22006-09-25 23:32:44 -0700832 handle->sync_read = 1;
Rafael J. Wysockifb13a282006-09-25 23:32:46 -0700833 if (handle->prev < handle->cur) {
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800834 if (!handle->prev) {
Andrew Morton546e0d22006-09-25 23:32:44 -0700835 error = load_header(handle,
836 (struct swsusp_info *)buffer);
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800837 if (error)
838 return error;
839 } else if (handle->prev <= nr_meta_pages) {
Andrew Morton546e0d22006-09-25 23:32:44 -0700840 handle->pbe = unpack_orig_addresses(buffer,
841 handle->pbe);
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800842 if (!handle->pbe) {
Rafael J. Wysocki968808b82006-06-23 02:04:48 -0700843 error = prepare_image(handle);
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800844 if (error)
845 return error;
846 handle->pbe = pagedir_nosave;
Rafael J. Wysocki968808b82006-06-23 02:04:48 -0700847 handle->last_pbe = NULL;
848 handle->buffer = get_buffer(handle);
Andrew Morton546e0d22006-09-25 23:32:44 -0700849 handle->sync_read = 0;
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800850 }
851 } else {
852 handle->pbe = handle->pbe->next;
Rafael J. Wysocki968808b82006-06-23 02:04:48 -0700853 handle->buffer = get_buffer(handle);
Andrew Morton546e0d22006-09-25 23:32:44 -0700854 handle->sync_read = 0;
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800855 }
Rafael J. Wysockifb13a282006-09-25 23:32:46 -0700856 handle->prev = handle->cur;
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800857 }
Rafael J. Wysockifb13a282006-09-25 23:32:46 -0700858 handle->buf_offset = handle->cur_offset;
859 if (handle->cur_offset + count >= PAGE_SIZE) {
860 count = PAGE_SIZE - handle->cur_offset;
861 handle->cur_offset = 0;
862 handle->cur++;
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800863 } else {
Rafael J. Wysockifb13a282006-09-25 23:32:46 -0700864 handle->cur_offset += count;
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800865 }
866 handle->offset += count;
867 return count;
868}
869
870int snapshot_image_loaded(struct snapshot_handle *handle)
871{
872 return !(!handle->pbe || handle->pbe->next || !nr_copy_pages ||
Rafael J. Wysockifb13a282006-09-25 23:32:46 -0700873 handle->cur <= nr_meta_pages + nr_copy_pages);
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800874}