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>
41 struct highmem_page *next;
44 static struct highmem_page *highmem_copy;
46 static int save_highmem_zone(struct zone *zone)
48 unsigned long zone_pfn;
49 mark_free_pages(zone);
50 for (zone_pfn = 0; zone_pfn < zone->spanned_pages; ++zone_pfn) {
52 struct highmem_page *save;
54 unsigned long pfn = zone_pfn + zone->zone_start_pfn;
60 page = pfn_to_page(pfn);
62 * This condition results from rvmalloc() sans vmalloc_32()
63 * and architectural memory reservations. This should be
64 * corrected eventually when the cases giving rise to this
65 * are better understood.
67 if (PageReserved(page)) {
68 printk("highmem reserved page?!\n");
71 BUG_ON(PageNosave(page));
72 if (PageNosaveFree(page))
74 save = kmalloc(sizeof(struct highmem_page), GFP_ATOMIC);
77 save->next = highmem_copy;
79 save->data = (void *) get_zeroed_page(GFP_ATOMIC);
84 kaddr = kmap_atomic(page, KM_USER0);
85 memcpy(save->data, kaddr, PAGE_SIZE);
86 kunmap_atomic(kaddr, KM_USER0);
91 #endif /* CONFIG_HIGHMEM */
94 static int save_highmem(void)
100 pr_debug("swsusp: Saving Highmem\n");
101 for_each_zone (zone) {
102 if (is_highmem(zone))
103 res = save_highmem_zone(zone);
111 int restore_highmem(void)
113 #ifdef CONFIG_HIGHMEM
114 printk("swsusp: Restoring Highmem\n");
115 while (highmem_copy) {
116 struct highmem_page *save = highmem_copy;
118 highmem_copy = save->next;
120 kaddr = kmap_atomic(save->page, KM_USER0);
121 memcpy(kaddr, save->data, PAGE_SIZE);
122 kunmap_atomic(kaddr, KM_USER0);
123 free_page((long) save->data);
131 static int pfn_is_nosave(unsigned long pfn)
133 unsigned long nosave_begin_pfn = __pa(&__nosave_begin) >> PAGE_SHIFT;
134 unsigned long nosave_end_pfn = PAGE_ALIGN(__pa(&__nosave_end)) >> PAGE_SHIFT;
135 return (pfn >= nosave_begin_pfn) && (pfn < nosave_end_pfn);
139 * saveable - Determine whether a page should be cloned or not.
142 * We save a page if it's Reserved, and not in the range of pages
143 * statically defined as 'unsaveable', or if it isn't reserved, and
144 * isn't part of a free chunk of pages.
147 static int saveable(struct zone * zone, unsigned long * zone_pfn)
149 unsigned long pfn = *zone_pfn + zone->zone_start_pfn;
155 page = pfn_to_page(pfn);
156 BUG_ON(PageReserved(page) && PageNosave(page));
157 if (PageNosave(page))
159 if (PageReserved(page) && pfn_is_nosave(pfn)) {
160 pr_debug("[nosave pfn 0x%lx]", pfn);
163 if (PageNosaveFree(page))
169 static unsigned count_data_pages(void)
172 unsigned long zone_pfn;
176 for_each_zone (zone) {
177 if (is_highmem(zone))
179 mark_free_pages(zone);
180 for (zone_pfn = 0; zone_pfn < zone->spanned_pages; ++zone_pfn)
181 n += saveable(zone, &zone_pfn);
186 static void copy_data_pages(struct pbe *pblist)
189 unsigned long zone_pfn;
193 for_each_zone (zone) {
194 if (is_highmem(zone))
196 mark_free_pages(zone);
197 /* This is necessary for swsusp_free() */
198 for_each_pb_page (p, pblist)
199 SetPageNosaveFree(virt_to_page(p));
200 for_each_pbe (p, pblist)
201 SetPageNosaveFree(virt_to_page(p->address));
202 for (zone_pfn = 0; zone_pfn < zone->spanned_pages; ++zone_pfn) {
203 if (saveable(zone, &zone_pfn)) {
205 page = pfn_to_page(zone_pfn + zone->zone_start_pfn);
207 pbe->orig_address = (unsigned long)page_address(page);
208 /* copy_page is not usable for copying task structs. */
209 memcpy((void *)pbe->address, (void *)pbe->orig_address, PAGE_SIZE);
219 * free_pagedir - free pages allocated with alloc_pagedir()
222 static void free_pagedir(struct pbe *pblist)
227 pbe = (pblist + PB_PAGE_SKIP)->next;
228 ClearPageNosave(virt_to_page(pblist));
229 ClearPageNosaveFree(virt_to_page(pblist));
230 free_page((unsigned long)pblist);
236 * fill_pb_page - Create a list of PBEs on a given memory page
239 static inline void fill_pb_page(struct pbe *pbpage)
244 pbpage += PB_PAGE_SKIP;
247 while (++p < pbpage);
251 * create_pbe_list - Create a list of PBEs on top of a given chain
252 * of memory pages allocated with alloc_pagedir()
255 void create_pbe_list(struct pbe *pblist, unsigned nr_pages)
257 struct pbe *pbpage, *p;
258 unsigned num = PBES_PER_PAGE;
260 for_each_pb_page (pbpage, pblist) {
264 fill_pb_page(pbpage);
265 num += PBES_PER_PAGE;
268 for (num -= PBES_PER_PAGE - 1, p = pbpage; num < nr_pages; p++, num++)
272 pr_debug("create_pbe_list(): initialized %d PBEs\n", num);
275 static void *alloc_image_page(void)
277 void *res = (void *)get_zeroed_page(GFP_ATOMIC | __GFP_COLD);
279 SetPageNosave(virt_to_page(res));
280 SetPageNosaveFree(virt_to_page(res));
286 * alloc_pagedir - Allocate the page directory.
288 * First, determine exactly how many pages we need and
291 * We arrange the pages in a chain: each page is an array of PBES_PER_PAGE
292 * struct pbe elements (pbes) and the last element in the page points
295 * On each page we set up a list of struct_pbe elements.
298 struct pbe * alloc_pagedir(unsigned nr_pages)
301 struct pbe *pblist, *pbe;
306 pr_debug("alloc_pagedir(): nr_pages = %d\n", nr_pages);
307 pblist = (struct pbe *)alloc_image_page();
308 /* FIXME: rewrite this ugly loop */
309 for (pbe = pblist, num = PBES_PER_PAGE; pbe && num < nr_pages;
310 pbe = pbe->next, num += PBES_PER_PAGE) {
312 pbe->next = (struct pbe *)alloc_image_page();
314 if (!pbe) { /* get_zeroed_page() failed */
315 free_pagedir(pblist);
322 * Free pages we allocated for suspend. Suspend pages are alocated
323 * before atomic copy, so we need to free them after resume.
326 void swsusp_free(void)
329 unsigned long zone_pfn;
331 for_each_zone(zone) {
332 for (zone_pfn = 0; zone_pfn < zone->spanned_pages; ++zone_pfn)
333 if (pfn_valid(zone_pfn + zone->zone_start_pfn)) {
335 page = pfn_to_page(zone_pfn + zone->zone_start_pfn);
336 if (PageNosave(page) && PageNosaveFree(page)) {
337 ClearPageNosave(page);
338 ClearPageNosaveFree(page);
339 free_page((long) page_address(page));
347 * enough_free_mem - Make sure we enough free memory to snapshot.
349 * Returns TRUE or FALSE after checking the number of available
353 static int enough_free_mem(unsigned nr_pages)
355 pr_debug("swsusp: available memory: %u pages\n", nr_free_pages());
356 return nr_free_pages() > (nr_pages + PAGES_FOR_IO +
357 (nr_pages + PBES_PER_PAGE - 1) / PBES_PER_PAGE);
361 static struct pbe *swsusp_alloc(unsigned nr_pages)
363 struct pbe *pblist, *p;
365 if (!(pblist = alloc_pagedir(nr_pages))) {
366 printk(KERN_ERR "suspend: Allocating pagedir failed.\n");
369 create_pbe_list(pblist, nr_pages);
371 for_each_pbe (p, pblist) {
372 p->address = (unsigned long)alloc_image_page();
374 printk(KERN_ERR "suspend: Allocating image pages failed.\n");
383 static int suspend_prepare_image(void)
387 pr_debug("swsusp: critical section: \n");
388 if (save_highmem()) {
389 printk(KERN_CRIT "swsusp: Not enough free pages for highmem\n");
395 nr_pages = count_data_pages();
396 printk("swsusp: Need to copy %u pages\n", nr_pages);
398 pr_debug("swsusp: pages needed: %u + %lu + %u, free: %u\n",
400 (nr_pages + PBES_PER_PAGE - 1) / PBES_PER_PAGE,
401 PAGES_FOR_IO, nr_free_pages());
403 /* This is needed because of the fixed size of swsusp_info */
404 if (MAX_PBES < (nr_pages + PBES_PER_PAGE - 1) / PBES_PER_PAGE)
407 if (!enough_free_mem(nr_pages)) {
408 printk(KERN_ERR "swsusp: Not enough free memory\n");
412 if (!enough_swap(nr_pages)) {
413 printk(KERN_ERR "swsusp: Not enough free swap\n");
417 pagedir_nosave = swsusp_alloc(nr_pages);
421 /* During allocating of suspend pagedir, new cold pages may appear.
425 copy_data_pages(pagedir_nosave);
428 * End of critical section. From now on, we can write to memory,
429 * but we should not touch disk. This specially means we must _not_
430 * touch swap space! Except we must write out our image of course.
433 nr_copy_pages = nr_pages;
435 printk("swsusp: critical section/: done (%d pages copied)\n", nr_pages);
440 asmlinkage int swsusp_save(void)
442 return suspend_prepare_image();