2 * linux/kernel/power/snapshot.c
4 * This file provide system snapshot/restore functionality.
6 * Copyright (C) 1998-2005 Pavel Machek <pavel@suse.cz>
8 * This file is released under the GPLv2, and is based on swsusp.c.
13 #include <linux/module.h>
15 #include <linux/suspend.h>
16 #include <linux/smp_lock.h>
17 #include <linux/delay.h>
18 #include <linux/bitops.h>
19 #include <linux/spinlock.h>
20 #include <linux/kernel.h>
22 #include <linux/device.h>
23 #include <linux/bootmem.h>
24 #include <linux/syscalls.h>
25 #include <linux/console.h>
26 #include <linux/highmem.h>
28 #include <asm/uaccess.h>
29 #include <asm/mmu_context.h>
30 #include <asm/pgtable.h>
31 #include <asm/tlbflush.h>
36 struct pbe *pagedir_nosave;
37 unsigned int nr_copy_pages;
43 struct highmem_page *next;
46 static struct highmem_page *highmem_copy;
48 static int save_highmem_zone(struct zone *zone)
50 unsigned long zone_pfn;
51 mark_free_pages(zone);
52 for (zone_pfn = 0; zone_pfn < zone->spanned_pages; ++zone_pfn) {
54 struct highmem_page *save;
56 unsigned long pfn = zone_pfn + zone->zone_start_pfn;
62 page = pfn_to_page(pfn);
64 * This condition results from rvmalloc() sans vmalloc_32()
65 * and architectural memory reservations. This should be
66 * corrected eventually when the cases giving rise to this
67 * are better understood.
69 if (PageReserved(page)) {
70 printk("highmem reserved page?!\n");
73 BUG_ON(PageNosave(page));
74 if (PageNosaveFree(page))
76 save = kmalloc(sizeof(struct highmem_page), GFP_ATOMIC);
79 save->next = highmem_copy;
81 save->data = (void *) get_zeroed_page(GFP_ATOMIC);
86 kaddr = kmap_atomic(page, KM_USER0);
87 memcpy(save->data, kaddr, PAGE_SIZE);
88 kunmap_atomic(kaddr, KM_USER0);
94 int save_highmem(void)
99 pr_debug("swsusp: Saving Highmem\n");
100 for_each_zone (zone) {
101 if (is_highmem(zone))
102 res = save_highmem_zone(zone);
109 int restore_highmem(void)
111 printk("swsusp: Restoring Highmem\n");
112 while (highmem_copy) {
113 struct highmem_page *save = highmem_copy;
115 highmem_copy = save->next;
117 kaddr = kmap_atomic(save->page, KM_USER0);
118 memcpy(kaddr, save->data, PAGE_SIZE);
119 kunmap_atomic(kaddr, KM_USER0);
120 free_page((long) save->data);
127 static int pfn_is_nosave(unsigned long pfn)
129 unsigned long nosave_begin_pfn = __pa(&__nosave_begin) >> PAGE_SHIFT;
130 unsigned long nosave_end_pfn = PAGE_ALIGN(__pa(&__nosave_end)) >> PAGE_SHIFT;
131 return (pfn >= nosave_begin_pfn) && (pfn < nosave_end_pfn);
135 * saveable - Determine whether a page should be cloned or not.
138 * We save a page if it's Reserved, and not in the range of pages
139 * statically defined as 'unsaveable', or if it isn't reserved, and
140 * isn't part of a free chunk of pages.
143 static int saveable(struct zone *zone, unsigned long *zone_pfn)
145 unsigned long pfn = *zone_pfn + zone->zone_start_pfn;
151 page = pfn_to_page(pfn);
152 BUG_ON(PageReserved(page) && PageNosave(page));
153 if (PageNosave(page))
155 if (PageReserved(page) && pfn_is_nosave(pfn)) {
156 pr_debug("[nosave pfn 0x%lx]", pfn);
159 if (PageNosaveFree(page))
165 static unsigned count_data_pages(void)
168 unsigned long zone_pfn;
171 for_each_zone (zone) {
172 if (is_highmem(zone))
174 mark_free_pages(zone);
175 for (zone_pfn = 0; zone_pfn < zone->spanned_pages; ++zone_pfn)
176 n += saveable(zone, &zone_pfn);
181 static void copy_data_pages(struct pbe *pblist)
184 unsigned long zone_pfn;
188 for_each_zone (zone) {
189 if (is_highmem(zone))
191 mark_free_pages(zone);
192 /* This is necessary for swsusp_free() */
193 for_each_pb_page (p, pblist)
194 SetPageNosaveFree(virt_to_page(p));
195 for_each_pbe (p, pblist)
196 SetPageNosaveFree(virt_to_page(p->address));
197 for (zone_pfn = 0; zone_pfn < zone->spanned_pages; ++zone_pfn) {
198 if (saveable(zone, &zone_pfn)) {
200 page = pfn_to_page(zone_pfn + zone->zone_start_pfn);
202 pbe->orig_address = (unsigned long)page_address(page);
203 /* copy_page is not usable for copying task structs. */
204 memcpy((void *)pbe->address, (void *)pbe->orig_address, PAGE_SIZE);
214 * free_pagedir - free pages allocated with alloc_pagedir()
217 void free_pagedir(struct pbe *pblist)
222 pbe = (pblist + PB_PAGE_SKIP)->next;
223 ClearPageNosave(virt_to_page(pblist));
224 ClearPageNosaveFree(virt_to_page(pblist));
225 free_page((unsigned long)pblist);
231 * fill_pb_page - Create a list of PBEs on a given memory page
234 static inline void fill_pb_page(struct pbe *pbpage)
239 pbpage += PB_PAGE_SKIP;
242 while (++p < pbpage);
246 * create_pbe_list - Create a list of PBEs on top of a given chain
247 * of memory pages allocated with alloc_pagedir()
250 static inline void create_pbe_list(struct pbe *pblist, unsigned int nr_pages)
252 struct pbe *pbpage, *p;
253 unsigned int num = PBES_PER_PAGE;
255 for_each_pb_page (pbpage, pblist) {
259 fill_pb_page(pbpage);
260 num += PBES_PER_PAGE;
263 for (num -= PBES_PER_PAGE - 1, p = pbpage; num < nr_pages; p++, num++)
270 * @safe_needed - on resume, for storing the PBE list and the image,
271 * we can only use memory pages that do not conflict with the pages
272 * which had been used before suspend.
274 * The unsafe pages are marked with the PG_nosave_free flag
276 * Allocated but unusable (ie eaten) memory pages should be marked
277 * so that swsusp_free() can release them
280 static inline void *alloc_image_page(gfp_t gfp_mask, int safe_needed)
286 res = (void *)get_zeroed_page(gfp_mask);
287 if (res && PageNosaveFree(virt_to_page(res)))
288 /* This is for swsusp_free() */
289 SetPageNosave(virt_to_page(res));
290 } while (res && PageNosaveFree(virt_to_page(res)));
292 res = (void *)get_zeroed_page(gfp_mask);
294 SetPageNosave(virt_to_page(res));
295 SetPageNosaveFree(virt_to_page(res));
300 unsigned long get_safe_page(gfp_t gfp_mask)
302 return (unsigned long)alloc_image_page(gfp_mask, 1);
306 * alloc_pagedir - Allocate the page directory.
308 * First, determine exactly how many pages we need and
311 * We arrange the pages in a chain: each page is an array of PBES_PER_PAGE
312 * struct pbe elements (pbes) and the last element in the page points
315 * On each page we set up a list of struct_pbe elements.
318 struct pbe *alloc_pagedir(unsigned int nr_pages, gfp_t gfp_mask, int safe_needed)
321 struct pbe *pblist, *pbe;
326 pr_debug("alloc_pagedir(): nr_pages = %d\n", nr_pages);
327 pblist = alloc_image_page(gfp_mask, safe_needed);
328 /* FIXME: rewrite this ugly loop */
329 for (pbe = pblist, num = PBES_PER_PAGE; pbe && num < nr_pages;
330 pbe = pbe->next, num += PBES_PER_PAGE) {
332 pbe->next = alloc_image_page(gfp_mask, safe_needed);
334 if (!pbe) { /* get_zeroed_page() failed */
335 free_pagedir(pblist);
338 create_pbe_list(pblist, nr_pages);
343 * Free pages we allocated for suspend. Suspend pages are alocated
344 * before atomic copy, so we need to free them after resume.
347 void swsusp_free(void)
350 unsigned long zone_pfn;
352 for_each_zone(zone) {
353 for (zone_pfn = 0; zone_pfn < zone->spanned_pages; ++zone_pfn)
354 if (pfn_valid(zone_pfn + zone->zone_start_pfn)) {
356 page = pfn_to_page(zone_pfn + zone->zone_start_pfn);
357 if (PageNosave(page) && PageNosaveFree(page)) {
358 ClearPageNosave(page);
359 ClearPageNosaveFree(page);
360 free_page((long) page_address(page));
368 * enough_free_mem - Make sure we enough free memory to snapshot.
370 * Returns TRUE or FALSE after checking the number of available
374 static int enough_free_mem(unsigned int nr_pages)
376 pr_debug("swsusp: available memory: %u pages\n", nr_free_pages());
377 return nr_free_pages() > (nr_pages + PAGES_FOR_IO +
378 (nr_pages + PBES_PER_PAGE - 1) / PBES_PER_PAGE);
381 int alloc_data_pages(struct pbe *pblist, gfp_t gfp_mask, int safe_needed)
385 for_each_pbe (p, pblist) {
386 p->address = (unsigned long)alloc_image_page(gfp_mask, safe_needed);
393 static struct pbe *swsusp_alloc(unsigned int nr_pages)
397 if (!(pblist = alloc_pagedir(nr_pages, GFP_ATOMIC | __GFP_COLD, 0))) {
398 printk(KERN_ERR "suspend: Allocating pagedir failed.\n");
402 if (alloc_data_pages(pblist, GFP_ATOMIC | __GFP_COLD, 0)) {
403 printk(KERN_ERR "suspend: Allocating image pages failed.\n");
411 asmlinkage int swsusp_save(void)
413 unsigned int nr_pages;
415 pr_debug("swsusp: critical section: \n");
418 nr_pages = count_data_pages();
419 printk("swsusp: Need to copy %u pages\n", nr_pages);
421 pr_debug("swsusp: pages needed: %u + %lu + %u, free: %u\n",
423 (nr_pages + PBES_PER_PAGE - 1) / PBES_PER_PAGE,
424 PAGES_FOR_IO, nr_free_pages());
426 if (!enough_free_mem(nr_pages)) {
427 printk(KERN_ERR "swsusp: Not enough free memory\n");
431 pagedir_nosave = swsusp_alloc(nr_pages);
435 /* During allocating of suspend pagedir, new cold pages may appear.
439 copy_data_pages(pagedir_nosave);
442 * End of critical section. From now on, we can write to memory,
443 * but we should not touch disk. This specially means we must _not_
444 * touch swap space! Except we must write out our image of course.
447 nr_copy_pages = nr_pages;
449 printk("swsusp: critical section/: done (%d pages copied)\n", nr_pages);