blob: 979096c2777328af7930a185cb82d0713ed30340 [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. Wysockia0f49652005-10-30 14:59:57 -0800211static void copy_data_pages(struct pbe *pblist)
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800212{
213 struct zone *zone;
Rafael J. Wysockiae83c5ee2006-09-25 23:32:45 -0700214 unsigned long pfn, max_zone_pfn;
Rafael J. Wysockia0f49652005-10-30 14:59:57 -0800215 struct pbe *pbe, *p;
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800216
Rafael J. Wysockia0f49652005-10-30 14:59:57 -0800217 pbe = pblist;
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800218 for_each_zone (zone) {
219 if (is_highmem(zone))
220 continue;
221 mark_free_pages(zone);
222 /* This is necessary for swsusp_free() */
Rafael J. Wysockia0f49652005-10-30 14:59:57 -0800223 for_each_pb_page (p, pblist)
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800224 SetPageNosaveFree(virt_to_page(p));
Rafael J. Wysockia0f49652005-10-30 14:59:57 -0800225 for_each_pbe (p, pblist)
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800226 SetPageNosaveFree(virt_to_page(p->address));
Rafael J. Wysockiae83c5ee2006-09-25 23:32:45 -0700227 max_zone_pfn = zone->zone_start_pfn + zone->spanned_pages;
228 for (pfn = zone->zone_start_pfn; pfn < max_zone_pfn; pfn++) {
229 struct page *page = saveable_page(pfn);
230
231 if (page) {
Rafael J. Wysocki95018f72006-07-10 04:45:00 -0700232 long *src, *dst;
233 int n;
234
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800235 BUG_ON(!pbe);
236 pbe->orig_address = (unsigned long)page_address(page);
Rafael J. Wysocki95018f72006-07-10 04:45:00 -0700237 /* copy_page and memcpy are not usable for copying task structs. */
238 dst = (long *)pbe->address;
239 src = (long *)pbe->orig_address;
240 for (n = PAGE_SIZE / sizeof(long); n; n--)
241 *dst++ = *src++;
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800242 pbe = pbe->next;
243 }
244 }
245 }
246 BUG_ON(pbe);
247}
248
249
250/**
251 * free_pagedir - free pages allocated with alloc_pagedir()
252 */
253
Rafael J. Wysocki4a3b98a2006-04-18 22:20:29 -0700254static void free_pagedir(struct pbe *pblist, int clear_nosave_free)
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800255{
256 struct pbe *pbe;
257
258 while (pblist) {
259 pbe = (pblist + PB_PAGE_SKIP)->next;
260 ClearPageNosave(virt_to_page(pblist));
Rafael J. Wysocki4a3b98a2006-04-18 22:20:29 -0700261 if (clear_nosave_free)
262 ClearPageNosaveFree(virt_to_page(pblist));
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800263 free_page((unsigned long)pblist);
264 pblist = pbe;
265 }
266}
267
268/**
269 * fill_pb_page - Create a list of PBEs on a given memory page
270 */
271
272static inline void fill_pb_page(struct pbe *pbpage)
273{
274 struct pbe *p;
275
276 p = pbpage;
277 pbpage += PB_PAGE_SKIP;
278 do
279 p->next = p + 1;
280 while (++p < pbpage);
281}
282
283/**
284 * create_pbe_list - Create a list of PBEs on top of a given chain
285 * of memory pages allocated with alloc_pagedir()
286 */
287
Rafael J. Wysocki7088a5c2006-01-06 00:13:05 -0800288static inline void create_pbe_list(struct pbe *pblist, unsigned int nr_pages)
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800289{
290 struct pbe *pbpage, *p;
Pavel Machekdc19d502005-11-07 00:58:40 -0800291 unsigned int num = PBES_PER_PAGE;
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800292
293 for_each_pb_page (pbpage, pblist) {
294 if (num >= nr_pages)
295 break;
296
297 fill_pb_page(pbpage);
298 num += PBES_PER_PAGE;
299 }
300 if (pbpage) {
301 for (num -= PBES_PER_PAGE - 1, p = pbpage; num < nr_pages; p++, num++)
302 p->next = p + 1;
303 p->next = NULL;
304 }
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800305}
306
Rafael J. Wysocki968808b2006-06-23 02:04:48 -0700307static unsigned int unsafe_pages;
Rafael J. Wysocki72a97e02006-01-06 00:13:46 -0800308
309/**
Rafael J. Wysocki054bd4c2005-11-08 21:34:39 -0800310 * @safe_needed - on resume, for storing the PBE list and the image,
311 * we can only use memory pages that do not conflict with the pages
Rafael J. Wysocki968808b2006-06-23 02:04:48 -0700312 * used before suspend.
Rafael J. Wysocki054bd4c2005-11-08 21:34:39 -0800313 *
314 * The unsafe pages are marked with the PG_nosave_free flag
Rafael J. Wysocki968808b2006-06-23 02:04:48 -0700315 * and we count them using unsafe_pages
Rafael J. Wysocki054bd4c2005-11-08 21:34:39 -0800316 */
317
Andrew Morton546e0d22006-09-25 23:32:44 -0700318static void *alloc_image_page(gfp_t gfp_mask, int safe_needed)
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800319{
Rafael J. Wysocki054bd4c2005-11-08 21:34:39 -0800320 void *res;
321
Rafael J. Wysocki968808b2006-06-23 02:04:48 -0700322 res = (void *)get_zeroed_page(gfp_mask);
Rafael J. Wysocki054bd4c2005-11-08 21:34:39 -0800323 if (safe_needed)
Rafael J. Wysocki968808b2006-06-23 02:04:48 -0700324 while (res && PageNosaveFree(virt_to_page(res))) {
325 /* The page is unsafe, mark it for swsusp_free() */
326 SetPageNosave(virt_to_page(res));
327 unsafe_pages++;
Rafael J. Wysocki054bd4c2005-11-08 21:34:39 -0800328 res = (void *)get_zeroed_page(gfp_mask);
Rafael J. Wysocki968808b2006-06-23 02:04:48 -0700329 }
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800330 if (res) {
331 SetPageNosave(virt_to_page(res));
332 SetPageNosaveFree(virt_to_page(res));
333 }
334 return res;
335}
336
Rafael J. Wysocki054bd4c2005-11-08 21:34:39 -0800337unsigned long get_safe_page(gfp_t gfp_mask)
338{
339 return (unsigned long)alloc_image_page(gfp_mask, 1);
340}
341
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800342/**
343 * alloc_pagedir - Allocate the page directory.
344 *
345 * First, determine exactly how many pages we need and
346 * allocate them.
347 *
348 * We arrange the pages in a chain: each page is an array of PBES_PER_PAGE
349 * struct pbe elements (pbes) and the last element in the page points
350 * to the next page.
351 *
352 * On each page we set up a list of struct_pbe elements.
353 */
354
Adrian Bunk7bff24e2006-06-23 02:04:47 -0700355static struct pbe *alloc_pagedir(unsigned int nr_pages, gfp_t gfp_mask,
356 int safe_needed)
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800357{
Pavel Machekdc19d502005-11-07 00:58:40 -0800358 unsigned int num;
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800359 struct pbe *pblist, *pbe;
360
361 if (!nr_pages)
362 return NULL;
363
Rafael J. Wysocki054bd4c2005-11-08 21:34:39 -0800364 pblist = alloc_image_page(gfp_mask, safe_needed);
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800365 /* FIXME: rewrite this ugly loop */
366 for (pbe = pblist, num = PBES_PER_PAGE; pbe && num < nr_pages;
367 pbe = pbe->next, num += PBES_PER_PAGE) {
368 pbe += PB_PAGE_SKIP;
Rafael J. Wysocki054bd4c2005-11-08 21:34:39 -0800369 pbe->next = alloc_image_page(gfp_mask, safe_needed);
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800370 }
371 if (!pbe) { /* get_zeroed_page() failed */
Rafael J. Wysocki4a3b98a2006-04-18 22:20:29 -0700372 free_pagedir(pblist, 1);
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800373 pblist = NULL;
Rafael J. Wysocki7088a5c2006-01-06 00:13:05 -0800374 } else
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -0800375 create_pbe_list(pblist, nr_pages);
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800376 return pblist;
377}
378
379/**
380 * Free pages we allocated for suspend. Suspend pages are alocated
381 * before atomic copy, so we need to free them after resume.
382 */
383
384void swsusp_free(void)
385{
386 struct zone *zone;
Rafael J. Wysockiae83c5ee2006-09-25 23:32:45 -0700387 unsigned long pfn, max_zone_pfn;
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800388
389 for_each_zone(zone) {
Rafael J. Wysockiae83c5ee2006-09-25 23:32:45 -0700390 max_zone_pfn = zone->zone_start_pfn + zone->spanned_pages;
391 for (pfn = zone->zone_start_pfn; pfn < max_zone_pfn; pfn++)
392 if (pfn_valid(pfn)) {
393 struct page *page = pfn_to_page(pfn);
394
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800395 if (PageNosave(page) && PageNosaveFree(page)) {
396 ClearPageNosave(page);
397 ClearPageNosaveFree(page);
398 free_page((long) page_address(page));
399 }
400 }
401 }
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800402 nr_copy_pages = 0;
403 nr_meta_pages = 0;
404 pagedir_nosave = NULL;
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -0800405 buffer = NULL;
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800406}
407
408
409/**
410 * enough_free_mem - Make sure we enough free memory to snapshot.
411 *
412 * Returns TRUE or FALSE after checking the number of available
413 * free pages.
414 */
415
Pavel Machekdc19d502005-11-07 00:58:40 -0800416static int enough_free_mem(unsigned int nr_pages)
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800417{
Rafael J. Wysockie5e2fa72006-01-06 00:14:20 -0800418 struct zone *zone;
419 unsigned int n = 0;
420
421 for_each_zone (zone)
422 if (!is_highmem(zone))
423 n += zone->free_pages;
424 pr_debug("swsusp: available memory: %u pages\n", n);
425 return n > (nr_pages + PAGES_FOR_IO +
Rafael J. Wysockia0f49652005-10-30 14:59:57 -0800426 (nr_pages + PBES_PER_PAGE - 1) / PBES_PER_PAGE);
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800427}
428
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800429static int alloc_data_pages(struct pbe *pblist, gfp_t gfp_mask, int safe_needed)
Rafael J. Wysocki054bd4c2005-11-08 21:34:39 -0800430{
431 struct pbe *p;
432
433 for_each_pbe (p, pblist) {
434 p->address = (unsigned long)alloc_image_page(gfp_mask, safe_needed);
435 if (!p->address)
436 return -ENOMEM;
437 }
438 return 0;
439}
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800440
Pavel Machekdc19d502005-11-07 00:58:40 -0800441static struct pbe *swsusp_alloc(unsigned int nr_pages)
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800442{
Rafael J. Wysocki054bd4c2005-11-08 21:34:39 -0800443 struct pbe *pblist;
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800444
Rafael J. Wysocki054bd4c2005-11-08 21:34:39 -0800445 if (!(pblist = alloc_pagedir(nr_pages, GFP_ATOMIC | __GFP_COLD, 0))) {
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800446 printk(KERN_ERR "suspend: Allocating pagedir failed.\n");
Rafael J. Wysockia0f49652005-10-30 14:59:57 -0800447 return NULL;
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800448 }
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800449
Rafael J. Wysocki054bd4c2005-11-08 21:34:39 -0800450 if (alloc_data_pages(pblist, GFP_ATOMIC | __GFP_COLD, 0)) {
451 printk(KERN_ERR "suspend: Allocating image pages failed.\n");
452 swsusp_free();
453 return NULL;
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800454 }
455
Rafael J. Wysockia0f49652005-10-30 14:59:57 -0800456 return pblist;
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800457}
458
Rafael J. Wysocki2e32a432005-10-30 15:00:00 -0800459asmlinkage int swsusp_save(void)
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800460{
Pavel Machekdc19d502005-11-07 00:58:40 -0800461 unsigned int nr_pages;
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800462
463 pr_debug("swsusp: critical section: \n");
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800464
465 drain_local_pages();
Rafael J. Wysockia0f49652005-10-30 14:59:57 -0800466 nr_pages = count_data_pages();
467 printk("swsusp: Need to copy %u pages\n", nr_pages);
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800468
469 pr_debug("swsusp: pages needed: %u + %lu + %u, free: %u\n",
Rafael J. Wysockia0f49652005-10-30 14:59:57 -0800470 nr_pages,
471 (nr_pages + PBES_PER_PAGE - 1) / PBES_PER_PAGE,
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800472 PAGES_FOR_IO, nr_free_pages());
473
Rafael J. Wysockia0f49652005-10-30 14:59:57 -0800474 if (!enough_free_mem(nr_pages)) {
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800475 printk(KERN_ERR "swsusp: Not enough free memory\n");
476 return -ENOMEM;
477 }
478
Rafael J. Wysockia0f49652005-10-30 14:59:57 -0800479 pagedir_nosave = swsusp_alloc(nr_pages);
480 if (!pagedir_nosave)
481 return -ENOMEM;
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800482
483 /* During allocating of suspend pagedir, new cold pages may appear.
484 * Kill them.
485 */
486 drain_local_pages();
Rafael J. Wysockia0f49652005-10-30 14:59:57 -0800487 copy_data_pages(pagedir_nosave);
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800488
489 /*
490 * End of critical section. From now on, we can write to memory,
491 * but we should not touch disk. This specially means we must _not_
492 * touch swap space! Except we must write out our image of course.
493 */
494
Rafael J. Wysockia0f49652005-10-30 14:59:57 -0800495 nr_copy_pages = nr_pages;
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800496 nr_meta_pages = (nr_pages * sizeof(long) + PAGE_SIZE - 1) >> PAGE_SHIFT;
Rafael J. Wysockia0f49652005-10-30 14:59:57 -0800497
498 printk("swsusp: critical section/: done (%d pages copied)\n", nr_pages);
Rafael J. Wysocki25761b62005-10-30 14:59:56 -0800499 return 0;
500}
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800501
502static void init_header(struct swsusp_info *info)
503{
504 memset(info, 0, sizeof(struct swsusp_info));
505 info->version_code = LINUX_VERSION_CODE;
506 info->num_physpages = num_physpages;
507 memcpy(&info->uts, &system_utsname, sizeof(system_utsname));
508 info->cpus = num_online_cpus();
509 info->image_pages = nr_copy_pages;
510 info->pages = nr_copy_pages + nr_meta_pages + 1;
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -0800511 info->size = info->pages;
512 info->size <<= PAGE_SHIFT;
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800513}
514
515/**
516 * pack_orig_addresses - the .orig_address fields of the PBEs from the
517 * list starting at @pbe are stored in the array @buf[] (1 page)
518 */
519
520static inline struct pbe *pack_orig_addresses(unsigned long *buf, struct pbe *pbe)
521{
522 int j;
523
524 for (j = 0; j < PAGE_SIZE / sizeof(long) && pbe; j++) {
525 buf[j] = pbe->orig_address;
526 pbe = pbe->next;
527 }
528 if (!pbe)
529 for (; j < PAGE_SIZE / sizeof(long); j++)
530 buf[j] = 0;
531 return pbe;
532}
533
534/**
535 * snapshot_read_next - used for reading the system memory snapshot.
536 *
537 * On the first call to it @handle should point to a zeroed
538 * snapshot_handle structure. The structure gets updated and a pointer
539 * to it should be passed to this function every next time.
540 *
541 * The @count parameter should contain the number of bytes the caller
542 * wants to read from the snapshot. It must not be zero.
543 *
544 * On success the function returns a positive number. Then, the caller
545 * is allowed to read up to the returned number of bytes from the memory
546 * location computed by the data_of() macro. The number returned
547 * may be smaller than @count, but this only happens if the read would
548 * cross a page boundary otherwise.
549 *
550 * The function returns 0 to indicate the end of data stream condition,
551 * and a negative number is returned on error. In such cases the
552 * structure pointed to by @handle is not updated and should not be used
553 * any more.
554 */
555
556int snapshot_read_next(struct snapshot_handle *handle, size_t count)
557{
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800558 if (handle->page > nr_meta_pages + nr_copy_pages)
559 return 0;
560 if (!buffer) {
561 /* This makes the buffer be freed by swsusp_free() */
562 buffer = alloc_image_page(GFP_ATOMIC, 0);
563 if (!buffer)
564 return -ENOMEM;
565 }
566 if (!handle->offset) {
567 init_header((struct swsusp_info *)buffer);
568 handle->buffer = buffer;
569 handle->pbe = pagedir_nosave;
570 }
571 if (handle->prev < handle->page) {
572 if (handle->page <= nr_meta_pages) {
573 handle->pbe = pack_orig_addresses(buffer, handle->pbe);
574 if (!handle->pbe)
575 handle->pbe = pagedir_nosave;
576 } else {
577 handle->buffer = (void *)handle->pbe->address;
578 handle->pbe = handle->pbe->next;
579 }
580 handle->prev = handle->page;
581 }
582 handle->buf_offset = handle->page_offset;
583 if (handle->page_offset + count >= PAGE_SIZE) {
584 count = PAGE_SIZE - handle->page_offset;
585 handle->page_offset = 0;
586 handle->page++;
587 } else {
588 handle->page_offset += count;
589 }
590 handle->offset += count;
591 return count;
592}
593
594/**
595 * mark_unsafe_pages - mark the pages that cannot be used for storing
596 * the image during resume, because they conflict with the pages that
597 * had been used before suspend
598 */
599
600static int mark_unsafe_pages(struct pbe *pblist)
601{
602 struct zone *zone;
Rafael J. Wysockiae83c5ee2006-09-25 23:32:45 -0700603 unsigned long pfn, max_zone_pfn;
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800604 struct pbe *p;
605
606 if (!pblist) /* a sanity check */
607 return -EINVAL;
608
609 /* Clear page flags */
610 for_each_zone (zone) {
Rafael J. Wysockiae83c5ee2006-09-25 23:32:45 -0700611 max_zone_pfn = zone->zone_start_pfn + zone->spanned_pages;
612 for (pfn = zone->zone_start_pfn; pfn < max_zone_pfn; pfn++)
613 if (pfn_valid(pfn))
614 ClearPageNosaveFree(pfn_to_page(pfn));
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800615 }
616
617 /* Mark orig addresses */
618 for_each_pbe (p, pblist) {
619 if (virt_addr_valid(p->orig_address))
620 SetPageNosaveFree(virt_to_page(p->orig_address));
621 else
622 return -EFAULT;
623 }
624
Rafael J. Wysocki968808b2006-06-23 02:04:48 -0700625 unsafe_pages = 0;
626
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800627 return 0;
628}
629
630static void copy_page_backup_list(struct pbe *dst, struct pbe *src)
631{
632 /* We assume both lists contain the same number of elements */
633 while (src) {
634 dst->orig_address = src->orig_address;
635 dst = dst->next;
636 src = src->next;
637 }
638}
639
640static int check_header(struct swsusp_info *info)
641{
642 char *reason = NULL;
643
644 if (info->version_code != LINUX_VERSION_CODE)
645 reason = "kernel version";
646 if (info->num_physpages != num_physpages)
647 reason = "memory size";
648 if (strcmp(info->uts.sysname,system_utsname.sysname))
649 reason = "system type";
650 if (strcmp(info->uts.release,system_utsname.release))
651 reason = "kernel release";
652 if (strcmp(info->uts.version,system_utsname.version))
653 reason = "version";
654 if (strcmp(info->uts.machine,system_utsname.machine))
655 reason = "machine";
656 if (reason) {
657 printk(KERN_ERR "swsusp: Resume mismatch: %s\n", reason);
658 return -EPERM;
659 }
660 return 0;
661}
662
663/**
664 * load header - check the image header and copy data from it
665 */
666
667static int load_header(struct snapshot_handle *handle,
668 struct swsusp_info *info)
669{
670 int error;
671 struct pbe *pblist;
672
673 error = check_header(info);
674 if (!error) {
675 pblist = alloc_pagedir(info->image_pages, GFP_ATOMIC, 0);
676 if (!pblist)
677 return -ENOMEM;
678 pagedir_nosave = pblist;
679 handle->pbe = pblist;
680 nr_copy_pages = info->image_pages;
681 nr_meta_pages = info->pages - info->image_pages - 1;
682 }
683 return error;
684}
685
686/**
687 * unpack_orig_addresses - copy the elements of @buf[] (1 page) to
688 * the PBEs in the list starting at @pbe
689 */
690
691static inline struct pbe *unpack_orig_addresses(unsigned long *buf,
692 struct pbe *pbe)
693{
694 int j;
695
696 for (j = 0; j < PAGE_SIZE / sizeof(long) && pbe; j++) {
697 pbe->orig_address = buf[j];
698 pbe = pbe->next;
699 }
700 return pbe;
701}
702
703/**
Rafael J. Wysocki968808b2006-06-23 02:04:48 -0700704 * prepare_image - use metadata contained in the PBE list
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800705 * pointed to by pagedir_nosave to mark the pages that will
706 * be overwritten in the process of restoring the system
Rafael J. Wysocki968808b2006-06-23 02:04:48 -0700707 * memory state from the image ("unsafe" pages) and allocate
708 * memory for the image
709 *
710 * The idea is to allocate the PBE list first and then
711 * allocate as many pages as it's needed for the image data,
712 * but not to assign these pages to the PBEs initially.
713 * Instead, we just mark them as allocated and create a list
714 * of "safe" which will be used later
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800715 */
716
Rafael J. Wysocki968808b2006-06-23 02:04:48 -0700717struct safe_page {
718 struct safe_page *next;
719 char padding[PAGE_SIZE - sizeof(void *)];
720};
721
722static struct safe_page *safe_pages;
723
724static int prepare_image(struct snapshot_handle *handle)
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800725{
726 int error = 0;
Rafael J. Wysocki968808b2006-06-23 02:04:48 -0700727 unsigned int nr_pages = nr_copy_pages;
728 struct pbe *p, *pblist = NULL;
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800729
730 p = pagedir_nosave;
731 error = mark_unsafe_pages(p);
732 if (!error) {
Rafael J. Wysocki968808b2006-06-23 02:04:48 -0700733 pblist = alloc_pagedir(nr_pages, GFP_ATOMIC, 1);
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800734 if (pblist)
735 copy_page_backup_list(pblist, p);
Rafael J. Wysocki4a3b98a2006-04-18 22:20:29 -0700736 free_pagedir(p, 0);
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800737 if (!pblist)
738 error = -ENOMEM;
739 }
Rafael J. Wysocki968808b2006-06-23 02:04:48 -0700740 safe_pages = NULL;
741 if (!error && nr_pages > unsafe_pages) {
742 nr_pages -= unsafe_pages;
743 while (nr_pages--) {
744 struct safe_page *ptr;
745
746 ptr = (struct safe_page *)get_zeroed_page(GFP_ATOMIC);
747 if (!ptr) {
748 error = -ENOMEM;
749 break;
750 }
751 if (!PageNosaveFree(virt_to_page(ptr))) {
752 /* The page is "safe", add it to the list */
753 ptr->next = safe_pages;
754 safe_pages = ptr;
755 }
756 /* Mark the page as allocated */
757 SetPageNosave(virt_to_page(ptr));
758 SetPageNosaveFree(virt_to_page(ptr));
759 }
760 }
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800761 if (!error) {
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800762 pagedir_nosave = pblist;
763 } else {
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800764 handle->pbe = NULL;
Rafael J. Wysocki968808b2006-06-23 02:04:48 -0700765 swsusp_free();
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800766 }
767 return error;
768}
769
Rafael J. Wysocki968808b2006-06-23 02:04:48 -0700770static void *get_buffer(struct snapshot_handle *handle)
771{
772 struct pbe *pbe = handle->pbe, *last = handle->last_pbe;
773 struct page *page = virt_to_page(pbe->orig_address);
774
775 if (PageNosave(page) && PageNosaveFree(page)) {
776 /*
777 * We have allocated the "original" page frame and we can
778 * use it directly to store the read page
779 */
780 pbe->address = 0;
781 if (last && last->next)
782 last->next = NULL;
783 return (void *)pbe->orig_address;
784 }
785 /*
786 * The "original" page frame has not been allocated and we have to
787 * use a "safe" page frame to store the read page
788 */
789 pbe->address = (unsigned long)safe_pages;
790 safe_pages = safe_pages->next;
791 if (last)
792 last->next = pbe;
793 handle->last_pbe = pbe;
794 return (void *)pbe->address;
795}
796
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800797/**
798 * snapshot_write_next - used for writing the system memory snapshot.
799 *
800 * On the first call to it @handle should point to a zeroed
801 * snapshot_handle structure. The structure gets updated and a pointer
802 * to it should be passed to this function every next time.
803 *
804 * The @count parameter should contain the number of bytes the caller
805 * wants to write to the image. It must not be zero.
806 *
807 * On success the function returns a positive number. Then, the caller
808 * is allowed to write up to the returned number of bytes to the memory
809 * location computed by the data_of() macro. The number returned
810 * may be smaller than @count, but this only happens if the write would
811 * cross a page boundary otherwise.
812 *
813 * The function returns 0 to indicate the "end of file" condition,
814 * and a negative number is returned on error. In such cases the
815 * structure pointed to by @handle is not updated and should not be used
816 * any more.
817 */
818
819int snapshot_write_next(struct snapshot_handle *handle, size_t count)
820{
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800821 int error = 0;
822
823 if (handle->prev && handle->page > nr_meta_pages + nr_copy_pages)
824 return 0;
825 if (!buffer) {
826 /* This makes the buffer be freed by swsusp_free() */
827 buffer = alloc_image_page(GFP_ATOMIC, 0);
828 if (!buffer)
829 return -ENOMEM;
830 }
831 if (!handle->offset)
832 handle->buffer = buffer;
Andrew Morton546e0d22006-09-25 23:32:44 -0700833 handle->sync_read = 1;
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800834 if (handle->prev < handle->page) {
835 if (!handle->prev) {
Andrew Morton546e0d22006-09-25 23:32:44 -0700836 error = load_header(handle,
837 (struct swsusp_info *)buffer);
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800838 if (error)
839 return error;
840 } else if (handle->prev <= nr_meta_pages) {
Andrew Morton546e0d22006-09-25 23:32:44 -0700841 handle->pbe = unpack_orig_addresses(buffer,
842 handle->pbe);
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800843 if (!handle->pbe) {
Rafael J. Wysocki968808b2006-06-23 02:04:48 -0700844 error = prepare_image(handle);
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800845 if (error)
846 return error;
847 handle->pbe = pagedir_nosave;
Rafael J. Wysocki968808b2006-06-23 02:04:48 -0700848 handle->last_pbe = NULL;
849 handle->buffer = get_buffer(handle);
Andrew Morton546e0d22006-09-25 23:32:44 -0700850 handle->sync_read = 0;
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800851 }
852 } else {
853 handle->pbe = handle->pbe->next;
Rafael J. Wysocki968808b2006-06-23 02:04:48 -0700854 handle->buffer = get_buffer(handle);
Andrew Morton546e0d22006-09-25 23:32:44 -0700855 handle->sync_read = 0;
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800856 }
857 handle->prev = handle->page;
858 }
859 handle->buf_offset = handle->page_offset;
860 if (handle->page_offset + count >= PAGE_SIZE) {
861 count = PAGE_SIZE - handle->page_offset;
862 handle->page_offset = 0;
863 handle->page++;
864 } else {
865 handle->page_offset += count;
866 }
867 handle->offset += count;
868 return count;
869}
870
871int snapshot_image_loaded(struct snapshot_handle *handle)
872{
873 return !(!handle->pbe || handle->pbe->next || !nr_copy_pages ||
874 handle->page <= nr_meta_pages + nr_copy_pages);
875}