Commit | Line | Data |
---|---|---|
25761b6e | 1 | /* |
96bc7aec | 2 | * linux/kernel/power/snapshot.c |
25761b6e | 3 | * |
96bc7aec | 4 | * This file provide system snapshot/restore functionality. |
25761b6e RW |
5 | * |
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 | ||
f577eb30 | 13 | #include <linux/version.h> |
25761b6e RW |
14 | #include <linux/module.h> |
15 | #include <linux/mm.h> | |
16 | #include <linux/suspend.h> | |
17 | #include <linux/smp_lock.h> | |
25761b6e | 18 | #include <linux/delay.h> |
25761b6e | 19 | #include <linux/bitops.h> |
25761b6e | 20 | #include <linux/spinlock.h> |
25761b6e | 21 | #include <linux/kernel.h> |
25761b6e RW |
22 | #include <linux/pm.h> |
23 | #include <linux/device.h> | |
25761b6e RW |
24 | #include <linux/bootmem.h> |
25 | #include <linux/syscalls.h> | |
26 | #include <linux/console.h> | |
27 | #include <linux/highmem.h> | |
25761b6e RW |
28 | |
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 | ||
25761b6e RW |
35 | #include "power.h" |
36 | ||
7088a5c0 | 37 | struct pbe *pagedir_nosave; |
f577eb30 RW |
38 | static unsigned int nr_copy_pages; |
39 | static unsigned int nr_meta_pages; | |
6e1819d6 | 40 | static unsigned long *buffer; |
7088a5c0 | 41 | |
25761b6e | 42 | #ifdef CONFIG_HIGHMEM |
3448097f | 43 | unsigned int count_highmem_pages(void) |
72a97e08 RW |
44 | { |
45 | struct zone *zone; | |
46 | unsigned long zone_pfn; | |
47 | unsigned int n = 0; | |
48 | ||
49 | for_each_zone (zone) | |
50 | if (is_highmem(zone)) { | |
51 | mark_free_pages(zone); | |
52 | for (zone_pfn = 0; zone_pfn < zone->spanned_pages; zone_pfn++) { | |
53 | struct page *page; | |
54 | unsigned long pfn = zone_pfn + zone->zone_start_pfn; | |
55 | if (!pfn_valid(pfn)) | |
56 | continue; | |
57 | page = pfn_to_page(pfn); | |
58 | if (PageReserved(page)) | |
59 | continue; | |
60 | if (PageNosaveFree(page)) | |
61 | continue; | |
62 | n++; | |
63 | } | |
64 | } | |
65 | return n; | |
66 | } | |
67 | ||
25761b6e RW |
68 | struct highmem_page { |
69 | char *data; | |
70 | struct page *page; | |
71 | struct highmem_page *next; | |
72 | }; | |
73 | ||
74 | static struct highmem_page *highmem_copy; | |
75 | ||
76 | static 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 | ||
ce6ed29f | 86 | if (!(pfn%10000)) |
25761b6e RW |
87 | printk("."); |
88 | if (!pfn_valid(pfn)) | |
89 | continue; | |
90 | page = pfn_to_page(pfn); | |
91 | /* | |
92 | * This condition results from rvmalloc() sans vmalloc_32() | |
93 | * and architectural memory reservations. This should be | |
94 | * corrected eventually when the cases giving rise to this | |
95 | * are better understood. | |
96 | */ | |
c8adb494 | 97 | if (PageReserved(page)) |
25761b6e | 98 | continue; |
25761b6e RW |
99 | 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 | } | |
25761b6e | 119 | |
3448097f | 120 | int save_highmem(void) |
25761b6e | 121 | { |
25761b6e RW |
122 | struct zone *zone; |
123 | int res = 0; | |
124 | ||
ce6ed29f | 125 | pr_debug("swsusp: Saving Highmem"); |
e4e4d665 | 126 | drain_local_pages(); |
25761b6e RW |
127 | for_each_zone (zone) { |
128 | if (is_highmem(zone)) | |
129 | res = save_highmem_zone(zone); | |
130 | if (res) | |
131 | return res; | |
132 | } | |
ce6ed29f | 133 | printk("\n"); |
25761b6e RW |
134 | return 0; |
135 | } | |
136 | ||
3448097f | 137 | int restore_highmem(void) |
25761b6e | 138 | { |
25761b6e RW |
139 | 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 | } | |
25761b6e RW |
151 | return 0; |
152 | } | |
ce4ab001 | 153 | #else |
7bff24e2 AB |
154 | static inline unsigned int count_highmem_pages(void) {return 0;} |
155 | static inline int save_highmem(void) {return 0;} | |
156 | static inline int restore_highmem(void) {return 0;} | |
0fbeb5a4 | 157 | #endif |
25761b6e | 158 | |
ae83c5ee | 159 | static inline int pfn_is_nosave(unsigned long pfn) |
25761b6e RW |
160 | { |
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 | * | |
ae83c5ee RW |
170 | * 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. | |
25761b6e RW |
173 | */ |
174 | ||
ae83c5ee | 175 | static struct page *saveable_page(unsigned long pfn) |
25761b6e | 176 | { |
de491861 | 177 | struct page *page; |
25761b6e RW |
178 | |
179 | if (!pfn_valid(pfn)) | |
ae83c5ee | 180 | return NULL; |
25761b6e RW |
181 | |
182 | page = pfn_to_page(pfn); | |
ae83c5ee | 183 | |
25761b6e | 184 | if (PageNosave(page)) |
ae83c5ee | 185 | return NULL; |
72a97e08 | 186 | if (PageReserved(page) && pfn_is_nosave(pfn)) |
ae83c5ee | 187 | return NULL; |
25761b6e | 188 | if (PageNosaveFree(page)) |
ae83c5ee | 189 | return NULL; |
25761b6e | 190 | |
ae83c5ee | 191 | return page; |
25761b6e RW |
192 | } |
193 | ||
72a97e08 | 194 | unsigned int count_data_pages(void) |
25761b6e RW |
195 | { |
196 | struct zone *zone; | |
ae83c5ee | 197 | unsigned long pfn, max_zone_pfn; |
dc19d507 | 198 | unsigned int n = 0; |
25761b6e | 199 | |
25761b6e RW |
200 | for_each_zone (zone) { |
201 | if (is_highmem(zone)) | |
202 | continue; | |
203 | mark_free_pages(zone); | |
ae83c5ee RW |
204 | 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); | |
25761b6e | 207 | } |
a0f49651 | 208 | return n; |
25761b6e RW |
209 | } |
210 | ||
a0f49651 | 211 | static void copy_data_pages(struct pbe *pblist) |
25761b6e RW |
212 | { |
213 | struct zone *zone; | |
ae83c5ee | 214 | unsigned long pfn, max_zone_pfn; |
a0f49651 | 215 | struct pbe *pbe, *p; |
25761b6e | 216 | |
a0f49651 | 217 | pbe = pblist; |
25761b6e RW |
218 | for_each_zone (zone) { |
219 | if (is_highmem(zone)) | |
220 | continue; | |
221 | mark_free_pages(zone); | |
222 | /* This is necessary for swsusp_free() */ | |
a0f49651 | 223 | for_each_pb_page (p, pblist) |
25761b6e | 224 | SetPageNosaveFree(virt_to_page(p)); |
a0f49651 | 225 | for_each_pbe (p, pblist) |
25761b6e | 226 | SetPageNosaveFree(virt_to_page(p->address)); |
ae83c5ee RW |
227 | 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) { | |
95018f7c RW |
232 | long *src, *dst; |
233 | int n; | |
234 | ||
25761b6e RW |
235 | BUG_ON(!pbe); |
236 | pbe->orig_address = (unsigned long)page_address(page); | |
95018f7c RW |
237 | /* 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++; | |
25761b6e RW |
242 | 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 | ||
4a3b98a4 | 254 | static void free_pagedir(struct pbe *pblist, int clear_nosave_free) |
25761b6e RW |
255 | { |
256 | struct pbe *pbe; | |
257 | ||
258 | while (pblist) { | |
259 | pbe = (pblist + PB_PAGE_SKIP)->next; | |
260 | ClearPageNosave(virt_to_page(pblist)); | |
4a3b98a4 RW |
261 | if (clear_nosave_free) |
262 | ClearPageNosaveFree(virt_to_page(pblist)); | |
25761b6e RW |
263 | 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 | ||
272 | static 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 | ||
7088a5c0 | 288 | static inline void create_pbe_list(struct pbe *pblist, unsigned int nr_pages) |
25761b6e RW |
289 | { |
290 | struct pbe *pbpage, *p; | |
dc19d507 | 291 | unsigned int num = PBES_PER_PAGE; |
25761b6e RW |
292 | |
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 | } | |
25761b6e RW |
305 | } |
306 | ||
968808b8 | 307 | static unsigned int unsafe_pages; |
72a97e08 | 308 | |
054bd4c1 RW |
309 | /** |
310 | * @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 | |
968808b8 | 312 | * used before suspend. |
054bd4c1 RW |
313 | * |
314 | * The unsafe pages are marked with the PG_nosave_free flag | |
968808b8 | 315 | * and we count them using unsafe_pages |
054bd4c1 RW |
316 | */ |
317 | ||
546e0d27 | 318 | static void *alloc_image_page(gfp_t gfp_mask, int safe_needed) |
25761b6e | 319 | { |
054bd4c1 RW |
320 | void *res; |
321 | ||
968808b8 | 322 | res = (void *)get_zeroed_page(gfp_mask); |
054bd4c1 | 323 | if (safe_needed) |
968808b8 RW |
324 | 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++; | |
054bd4c1 | 328 | res = (void *)get_zeroed_page(gfp_mask); |
968808b8 | 329 | } |
25761b6e RW |
330 | if (res) { |
331 | SetPageNosave(virt_to_page(res)); | |
332 | SetPageNosaveFree(virt_to_page(res)); | |
333 | } | |
334 | return res; | |
335 | } | |
336 | ||
054bd4c1 RW |
337 | unsigned long get_safe_page(gfp_t gfp_mask) |
338 | { | |
339 | return (unsigned long)alloc_image_page(gfp_mask, 1); | |
340 | } | |
341 | ||
25761b6e RW |
342 | /** |
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 | ||
7bff24e2 AB |
355 | static struct pbe *alloc_pagedir(unsigned int nr_pages, gfp_t gfp_mask, |
356 | int safe_needed) | |
25761b6e | 357 | { |
dc19d507 | 358 | unsigned int num; |
25761b6e RW |
359 | struct pbe *pblist, *pbe; |
360 | ||
361 | if (!nr_pages) | |
362 | return NULL; | |
363 | ||
054bd4c1 | 364 | pblist = alloc_image_page(gfp_mask, safe_needed); |
25761b6e RW |
365 | /* 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; | |
054bd4c1 | 369 | pbe->next = alloc_image_page(gfp_mask, safe_needed); |
25761b6e RW |
370 | } |
371 | if (!pbe) { /* get_zeroed_page() failed */ | |
4a3b98a4 | 372 | free_pagedir(pblist, 1); |
25761b6e | 373 | pblist = NULL; |
7088a5c0 | 374 | } else |
6e1819d6 | 375 | create_pbe_list(pblist, nr_pages); |
25761b6e RW |
376 | 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 | ||
384 | void swsusp_free(void) | |
385 | { | |
386 | struct zone *zone; | |
ae83c5ee | 387 | unsigned long pfn, max_zone_pfn; |
25761b6e RW |
388 | |
389 | for_each_zone(zone) { | |
ae83c5ee RW |
390 | 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 | ||
25761b6e RW |
395 | if (PageNosave(page) && PageNosaveFree(page)) { |
396 | ClearPageNosave(page); | |
397 | ClearPageNosaveFree(page); | |
398 | free_page((long) page_address(page)); | |
399 | } | |
400 | } | |
401 | } | |
f577eb30 RW |
402 | nr_copy_pages = 0; |
403 | nr_meta_pages = 0; | |
404 | pagedir_nosave = NULL; | |
6e1819d6 | 405 | buffer = NULL; |
25761b6e RW |
406 | } |
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 | ||
dc19d507 | 416 | static int enough_free_mem(unsigned int nr_pages) |
25761b6e | 417 | { |
e5e2fa78 RW |
418 | 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 + | |
a0f49651 | 426 | (nr_pages + PBES_PER_PAGE - 1) / PBES_PER_PAGE); |
25761b6e RW |
427 | } |
428 | ||
f577eb30 | 429 | static int alloc_data_pages(struct pbe *pblist, gfp_t gfp_mask, int safe_needed) |
054bd4c1 RW |
430 | { |
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 | } | |
25761b6e | 440 | |
dc19d507 | 441 | static struct pbe *swsusp_alloc(unsigned int nr_pages) |
25761b6e | 442 | { |
054bd4c1 | 443 | struct pbe *pblist; |
25761b6e | 444 | |
054bd4c1 | 445 | if (!(pblist = alloc_pagedir(nr_pages, GFP_ATOMIC | __GFP_COLD, 0))) { |
25761b6e | 446 | printk(KERN_ERR "suspend: Allocating pagedir failed.\n"); |
a0f49651 | 447 | return NULL; |
25761b6e | 448 | } |
25761b6e | 449 | |
054bd4c1 RW |
450 | 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; | |
25761b6e RW |
454 | } |
455 | ||
a0f49651 | 456 | return pblist; |
25761b6e RW |
457 | } |
458 | ||
2e32a43e | 459 | asmlinkage int swsusp_save(void) |
25761b6e | 460 | { |
dc19d507 | 461 | unsigned int nr_pages; |
25761b6e RW |
462 | |
463 | pr_debug("swsusp: critical section: \n"); | |
25761b6e RW |
464 | |
465 | drain_local_pages(); | |
a0f49651 RW |
466 | nr_pages = count_data_pages(); |
467 | printk("swsusp: Need to copy %u pages\n", nr_pages); | |
25761b6e RW |
468 | |
469 | pr_debug("swsusp: pages needed: %u + %lu + %u, free: %u\n", | |
a0f49651 RW |
470 | nr_pages, |
471 | (nr_pages + PBES_PER_PAGE - 1) / PBES_PER_PAGE, | |
25761b6e RW |
472 | PAGES_FOR_IO, nr_free_pages()); |
473 | ||
a0f49651 | 474 | if (!enough_free_mem(nr_pages)) { |
25761b6e RW |
475 | printk(KERN_ERR "swsusp: Not enough free memory\n"); |
476 | return -ENOMEM; | |
477 | } | |
478 | ||
a0f49651 RW |
479 | pagedir_nosave = swsusp_alloc(nr_pages); |
480 | if (!pagedir_nosave) | |
481 | return -ENOMEM; | |
25761b6e RW |
482 | |
483 | /* During allocating of suspend pagedir, new cold pages may appear. | |
484 | * Kill them. | |
485 | */ | |
486 | drain_local_pages(); | |
a0f49651 | 487 | copy_data_pages(pagedir_nosave); |
25761b6e RW |
488 | |
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 | ||
a0f49651 | 495 | nr_copy_pages = nr_pages; |
f577eb30 | 496 | nr_meta_pages = (nr_pages * sizeof(long) + PAGE_SIZE - 1) >> PAGE_SHIFT; |
a0f49651 RW |
497 | |
498 | printk("swsusp: critical section/: done (%d pages copied)\n", nr_pages); | |
25761b6e RW |
499 | return 0; |
500 | } | |
f577eb30 RW |
501 | |
502 | static 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; | |
6e1819d6 RW |
511 | info->size = info->pages; |
512 | info->size <<= PAGE_SHIFT; | |
f577eb30 RW |
513 | } |
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 | ||
520 | static 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 | ||
556 | int snapshot_read_next(struct snapshot_handle *handle, size_t count) | |
557 | { | |
fb13a28b | 558 | if (handle->cur > nr_meta_pages + nr_copy_pages) |
f577eb30 RW |
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 | } | |
fb13a28b RW |
571 | if (handle->prev < handle->cur) { |
572 | if (handle->cur <= nr_meta_pages) { | |
f577eb30 RW |
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 | } | |
fb13a28b | 580 | handle->prev = handle->cur; |
f577eb30 | 581 | } |
fb13a28b RW |
582 | handle->buf_offset = handle->cur_offset; |
583 | if (handle->cur_offset + count >= PAGE_SIZE) { | |
584 | count = PAGE_SIZE - handle->cur_offset; | |
585 | handle->cur_offset = 0; | |
586 | handle->cur++; | |
f577eb30 | 587 | } else { |
fb13a28b | 588 | handle->cur_offset += count; |
f577eb30 RW |
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 | ||
600 | static int mark_unsafe_pages(struct pbe *pblist) | |
601 | { | |
602 | struct zone *zone; | |
ae83c5ee | 603 | unsigned long pfn, max_zone_pfn; |
f577eb30 RW |
604 | struct pbe *p; |
605 | ||
606 | if (!pblist) /* a sanity check */ | |
607 | return -EINVAL; | |
608 | ||
609 | /* Clear page flags */ | |
610 | for_each_zone (zone) { | |
ae83c5ee RW |
611 | 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)); | |
f577eb30 RW |
615 | } |
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 | ||
968808b8 RW |
625 | unsafe_pages = 0; |
626 | ||
f577eb30 RW |
627 | return 0; |
628 | } | |
629 | ||
630 | static 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 | ||
640 | static 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 | ||
667 | static 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 | ||
691 | static 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 | /** | |
968808b8 | 704 | * prepare_image - use metadata contained in the PBE list |
f577eb30 RW |
705 | * pointed to by pagedir_nosave to mark the pages that will |
706 | * be overwritten in the process of restoring the system | |
968808b8 RW |
707 | * 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 | |
f577eb30 RW |
715 | */ |
716 | ||
968808b8 RW |
717 | struct safe_page { |
718 | struct safe_page *next; | |
719 | char padding[PAGE_SIZE - sizeof(void *)]; | |
720 | }; | |
721 | ||
722 | static struct safe_page *safe_pages; | |
723 | ||
724 | static int prepare_image(struct snapshot_handle *handle) | |
f577eb30 RW |
725 | { |
726 | int error = 0; | |
968808b8 RW |
727 | unsigned int nr_pages = nr_copy_pages; |
728 | struct pbe *p, *pblist = NULL; | |
f577eb30 RW |
729 | |
730 | p = pagedir_nosave; | |
731 | error = mark_unsafe_pages(p); | |
732 | if (!error) { | |
968808b8 | 733 | pblist = alloc_pagedir(nr_pages, GFP_ATOMIC, 1); |
f577eb30 RW |
734 | if (pblist) |
735 | copy_page_backup_list(pblist, p); | |
4a3b98a4 | 736 | free_pagedir(p, 0); |
f577eb30 RW |
737 | if (!pblist) |
738 | error = -ENOMEM; | |
739 | } | |
968808b8 RW |
740 | 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 | } | |
f577eb30 | 761 | if (!error) { |
f577eb30 RW |
762 | pagedir_nosave = pblist; |
763 | } else { | |
f577eb30 | 764 | handle->pbe = NULL; |
968808b8 | 765 | swsusp_free(); |
f577eb30 RW |
766 | } |
767 | return error; | |
768 | } | |
769 | ||
968808b8 RW |
770 | static 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 | ||
f577eb30 RW |
797 | /** |
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 | ||
819 | int snapshot_write_next(struct snapshot_handle *handle, size_t count) | |
820 | { | |
f577eb30 RW |
821 | int error = 0; |
822 | ||
fb13a28b | 823 | if (handle->prev && handle->cur > nr_meta_pages + nr_copy_pages) |
f577eb30 RW |
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; | |
546e0d27 | 833 | handle->sync_read = 1; |
fb13a28b | 834 | if (handle->prev < handle->cur) { |
f577eb30 | 835 | if (!handle->prev) { |
546e0d27 AM |
836 | error = load_header(handle, |
837 | (struct swsusp_info *)buffer); | |
f577eb30 RW |
838 | if (error) |
839 | return error; | |
840 | } else if (handle->prev <= nr_meta_pages) { | |
546e0d27 AM |
841 | handle->pbe = unpack_orig_addresses(buffer, |
842 | handle->pbe); | |
f577eb30 | 843 | if (!handle->pbe) { |
968808b8 | 844 | error = prepare_image(handle); |
f577eb30 RW |
845 | if (error) |
846 | return error; | |
847 | handle->pbe = pagedir_nosave; | |
968808b8 RW |
848 | handle->last_pbe = NULL; |
849 | handle->buffer = get_buffer(handle); | |
546e0d27 | 850 | handle->sync_read = 0; |
f577eb30 RW |
851 | } |
852 | } else { | |
853 | handle->pbe = handle->pbe->next; | |
968808b8 | 854 | handle->buffer = get_buffer(handle); |
546e0d27 | 855 | handle->sync_read = 0; |
f577eb30 | 856 | } |
fb13a28b | 857 | handle->prev = handle->cur; |
f577eb30 | 858 | } |
fb13a28b RW |
859 | handle->buf_offset = handle->cur_offset; |
860 | if (handle->cur_offset + count >= PAGE_SIZE) { | |
861 | count = PAGE_SIZE - handle->cur_offset; | |
862 | handle->cur_offset = 0; | |
863 | handle->cur++; | |
f577eb30 | 864 | } else { |
fb13a28b | 865 | handle->cur_offset += count; |
f577eb30 RW |
866 | } |
867 | handle->offset += count; | |
868 | return count; | |
869 | } | |
870 | ||
871 | int snapshot_image_loaded(struct snapshot_handle *handle) | |
872 | { | |
873 | return !(!handle->pbe || handle->pbe->next || !nr_copy_pages || | |
fb13a28b | 874 | handle->cur <= nr_meta_pages + nr_copy_pages); |
f577eb30 | 875 | } |