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/version.h>
14 #include <linux/module.h>
16 #include <linux/suspend.h>
17 #include <linux/smp_lock.h>
18 #include <linux/delay.h>
19 #include <linux/bitops.h>
20 #include <linux/spinlock.h>
21 #include <linux/kernel.h>
23 #include <linux/device.h>
24 #include <linux/bootmem.h>
25 #include <linux/syscalls.h>
26 #include <linux/console.h>
27 #include <linux/highmem.h>
29 #include <asm/uaccess.h>
30 #include <asm/mmu_context.h>
31 #include <asm/pgtable.h>
32 #include <asm/tlbflush.h>
37 struct pbe *pagedir_nosave;
38 static unsigned int nr_copy_pages;
39 static unsigned int nr_meta_pages;
40 static unsigned long *buffer;
43 unsigned int count_highmem_pages(void)
46 unsigned long zone_pfn;
50 if (is_highmem(zone)) {
51 mark_free_pages(zone);
52 for (zone_pfn = 0; zone_pfn < zone->spanned_pages; zone_pfn++) {
54 unsigned long pfn = zone_pfn + zone->zone_start_pfn;
57 page = pfn_to_page(pfn);
58 if (PageReserved(page))
60 if (PageNosaveFree(page))
71 struct highmem_page *next;
74 static struct highmem_page *highmem_copy;
76 static int save_highmem_zone(struct zone *zone)
78 unsigned long zone_pfn;
79 mark_free_pages(zone);
80 for (zone_pfn = 0; zone_pfn < zone->spanned_pages; ++zone_pfn) {
82 struct highmem_page *save;
84 unsigned long pfn = zone_pfn + zone->zone_start_pfn;
90 page = pfn_to_page(pfn);
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.
97 if (PageReserved(page))
99 BUG_ON(PageNosave(page));
100 if (PageNosaveFree(page))
102 save = kmalloc(sizeof(struct highmem_page), GFP_ATOMIC);
105 save->next = highmem_copy;
107 save->data = (void *) get_zeroed_page(GFP_ATOMIC);
112 kaddr = kmap_atomic(page, KM_USER0);
113 memcpy(save->data, kaddr, PAGE_SIZE);
114 kunmap_atomic(kaddr, KM_USER0);
120 int save_highmem(void)
125 pr_debug("swsusp: Saving Highmem\n");
126 for_each_zone (zone) {
127 if (is_highmem(zone))
128 res = save_highmem_zone(zone);
135 int restore_highmem(void)
137 printk("swsusp: Restoring Highmem\n");
138 while (highmem_copy) {
139 struct highmem_page *save = highmem_copy;
141 highmem_copy = save->next;
143 kaddr = kmap_atomic(save->page, KM_USER0);
144 memcpy(kaddr, save->data, PAGE_SIZE);
145 kunmap_atomic(kaddr, KM_USER0);
146 free_page((long) save->data);
153 static int pfn_is_nosave(unsigned long pfn)
155 unsigned long nosave_begin_pfn = __pa(&__nosave_begin) >> PAGE_SHIFT;
156 unsigned long nosave_end_pfn = PAGE_ALIGN(__pa(&__nosave_end)) >> PAGE_SHIFT;
157 return (pfn >= nosave_begin_pfn) && (pfn < nosave_end_pfn);
161 * saveable - Determine whether a page should be cloned or not.
164 * We save a page if it's Reserved, and not in the range of pages
165 * statically defined as 'unsaveable', or if it isn't reserved, and
166 * isn't part of a free chunk of pages.
169 static int saveable(struct zone *zone, unsigned long *zone_pfn)
171 unsigned long pfn = *zone_pfn + zone->zone_start_pfn;
177 page = pfn_to_page(pfn);
178 BUG_ON(PageReserved(page) && PageNosave(page));
179 if (PageNosave(page))
181 if (PageReserved(page) && pfn_is_nosave(pfn))
183 if (PageNosaveFree(page))
189 unsigned int count_data_pages(void)
192 unsigned long zone_pfn;
195 for_each_zone (zone) {
196 if (is_highmem(zone))
198 mark_free_pages(zone);
199 for (zone_pfn = 0; zone_pfn < zone->spanned_pages; ++zone_pfn)
200 n += saveable(zone, &zone_pfn);
205 static void copy_data_pages(struct pbe *pblist)
208 unsigned long zone_pfn;
212 for_each_zone (zone) {
213 if (is_highmem(zone))
215 mark_free_pages(zone);
216 /* This is necessary for swsusp_free() */
217 for_each_pb_page (p, pblist)
218 SetPageNosaveFree(virt_to_page(p));
219 for_each_pbe (p, pblist)
220 SetPageNosaveFree(virt_to_page(p->address));
221 for (zone_pfn = 0; zone_pfn < zone->spanned_pages; ++zone_pfn) {
222 if (saveable(zone, &zone_pfn)) {
224 page = pfn_to_page(zone_pfn + zone->zone_start_pfn);
226 pbe->orig_address = (unsigned long)page_address(page);
227 /* copy_page is not usable for copying task structs. */
228 memcpy((void *)pbe->address, (void *)pbe->orig_address, PAGE_SIZE);
238 * free_pagedir - free pages allocated with alloc_pagedir()
241 static void free_pagedir(struct pbe *pblist)
246 pbe = (pblist + PB_PAGE_SKIP)->next;
247 ClearPageNosave(virt_to_page(pblist));
248 ClearPageNosaveFree(virt_to_page(pblist));
249 free_page((unsigned long)pblist);
255 * fill_pb_page - Create a list of PBEs on a given memory page
258 static inline void fill_pb_page(struct pbe *pbpage)
263 pbpage += PB_PAGE_SKIP;
266 while (++p < pbpage);
270 * create_pbe_list - Create a list of PBEs on top of a given chain
271 * of memory pages allocated with alloc_pagedir()
274 static inline void create_pbe_list(struct pbe *pblist, unsigned int nr_pages)
276 struct pbe *pbpage, *p;
277 unsigned int num = PBES_PER_PAGE;
279 for_each_pb_page (pbpage, pblist) {
283 fill_pb_page(pbpage);
284 num += PBES_PER_PAGE;
287 for (num -= PBES_PER_PAGE - 1, p = pbpage; num < nr_pages; p++, num++)
294 * On resume it is necessary to trace and eventually free the unsafe
295 * pages that have been allocated, because they are needed for I/O
296 * (on x86-64 we likely will "eat" these pages once again while
297 * creating the temporary page translation tables)
301 struct eaten_page *next;
302 char padding[PAGE_SIZE - sizeof(void *)];
305 static struct eaten_page *eaten_pages = NULL;
307 static void release_eaten_pages(void)
309 struct eaten_page *p, *q;
314 /* We don't want swsusp_free() to free this page again */
315 ClearPageNosave(virt_to_page(p));
316 free_page((unsigned long)p);
323 * @safe_needed - on resume, for storing the PBE list and the image,
324 * we can only use memory pages that do not conflict with the pages
325 * which had been used before suspend.
327 * The unsafe pages are marked with the PG_nosave_free flag
329 * Allocated but unusable (ie eaten) memory pages should be marked
330 * so that swsusp_free() can release them
333 static inline void *alloc_image_page(gfp_t gfp_mask, int safe_needed)
339 res = (void *)get_zeroed_page(gfp_mask);
340 if (res && PageNosaveFree(virt_to_page(res))) {
341 /* This is for swsusp_free() */
342 SetPageNosave(virt_to_page(res));
343 ((struct eaten_page *)res)->next = eaten_pages;
346 } while (res && PageNosaveFree(virt_to_page(res)));
348 res = (void *)get_zeroed_page(gfp_mask);
350 SetPageNosave(virt_to_page(res));
351 SetPageNosaveFree(virt_to_page(res));
356 unsigned long get_safe_page(gfp_t gfp_mask)
358 return (unsigned long)alloc_image_page(gfp_mask, 1);
362 * alloc_pagedir - Allocate the page directory.
364 * First, determine exactly how many pages we need and
367 * We arrange the pages in a chain: each page is an array of PBES_PER_PAGE
368 * struct pbe elements (pbes) and the last element in the page points
371 * On each page we set up a list of struct_pbe elements.
374 struct pbe *alloc_pagedir(unsigned int nr_pages, gfp_t gfp_mask, int safe_needed)
377 struct pbe *pblist, *pbe;
382 pblist = alloc_image_page(gfp_mask, safe_needed);
383 /* FIXME: rewrite this ugly loop */
384 for (pbe = pblist, num = PBES_PER_PAGE; pbe && num < nr_pages;
385 pbe = pbe->next, num += PBES_PER_PAGE) {
387 pbe->next = alloc_image_page(gfp_mask, safe_needed);
389 if (!pbe) { /* get_zeroed_page() failed */
390 free_pagedir(pblist);
393 create_pbe_list(pblist, nr_pages);
398 * Free pages we allocated for suspend. Suspend pages are alocated
399 * before atomic copy, so we need to free them after resume.
402 void swsusp_free(void)
405 unsigned long zone_pfn;
407 for_each_zone(zone) {
408 for (zone_pfn = 0; zone_pfn < zone->spanned_pages; ++zone_pfn)
409 if (pfn_valid(zone_pfn + zone->zone_start_pfn)) {
411 page = pfn_to_page(zone_pfn + zone->zone_start_pfn);
412 if (PageNosave(page) && PageNosaveFree(page)) {
413 ClearPageNosave(page);
414 ClearPageNosaveFree(page);
415 free_page((long) page_address(page));
421 pagedir_nosave = NULL;
427 * enough_free_mem - Make sure we enough free memory to snapshot.
429 * Returns TRUE or FALSE after checking the number of available
433 static int enough_free_mem(unsigned int nr_pages)
439 if (!is_highmem(zone))
440 n += zone->free_pages;
441 pr_debug("swsusp: available memory: %u pages\n", n);
442 return n > (nr_pages + PAGES_FOR_IO +
443 (nr_pages + PBES_PER_PAGE - 1) / PBES_PER_PAGE);
446 static int alloc_data_pages(struct pbe *pblist, gfp_t gfp_mask, int safe_needed)
450 for_each_pbe (p, pblist) {
451 p->address = (unsigned long)alloc_image_page(gfp_mask, safe_needed);
458 static struct pbe *swsusp_alloc(unsigned int nr_pages)
462 if (!(pblist = alloc_pagedir(nr_pages, GFP_ATOMIC | __GFP_COLD, 0))) {
463 printk(KERN_ERR "suspend: Allocating pagedir failed.\n");
467 if (alloc_data_pages(pblist, GFP_ATOMIC | __GFP_COLD, 0)) {
468 printk(KERN_ERR "suspend: Allocating image pages failed.\n");
476 asmlinkage int swsusp_save(void)
478 unsigned int nr_pages;
480 pr_debug("swsusp: critical section: \n");
483 nr_pages = count_data_pages();
484 printk("swsusp: Need to copy %u pages\n", nr_pages);
486 pr_debug("swsusp: pages needed: %u + %lu + %u, free: %u\n",
488 (nr_pages + PBES_PER_PAGE - 1) / PBES_PER_PAGE,
489 PAGES_FOR_IO, nr_free_pages());
491 if (!enough_free_mem(nr_pages)) {
492 printk(KERN_ERR "swsusp: Not enough free memory\n");
496 pagedir_nosave = swsusp_alloc(nr_pages);
500 /* During allocating of suspend pagedir, new cold pages may appear.
504 copy_data_pages(pagedir_nosave);
507 * End of critical section. From now on, we can write to memory,
508 * but we should not touch disk. This specially means we must _not_
509 * touch swap space! Except we must write out our image of course.
512 nr_copy_pages = nr_pages;
513 nr_meta_pages = (nr_pages * sizeof(long) + PAGE_SIZE - 1) >> PAGE_SHIFT;
515 printk("swsusp: critical section/: done (%d pages copied)\n", nr_pages);
519 static void init_header(struct swsusp_info *info)
521 memset(info, 0, sizeof(struct swsusp_info));
522 info->version_code = LINUX_VERSION_CODE;
523 info->num_physpages = num_physpages;
524 memcpy(&info->uts, &system_utsname, sizeof(system_utsname));
525 info->cpus = num_online_cpus();
526 info->image_pages = nr_copy_pages;
527 info->pages = nr_copy_pages + nr_meta_pages + 1;
528 info->size = info->pages;
529 info->size <<= PAGE_SHIFT;
533 * pack_orig_addresses - the .orig_address fields of the PBEs from the
534 * list starting at @pbe are stored in the array @buf[] (1 page)
537 static inline struct pbe *pack_orig_addresses(unsigned long *buf, struct pbe *pbe)
541 for (j = 0; j < PAGE_SIZE / sizeof(long) && pbe; j++) {
542 buf[j] = pbe->orig_address;
546 for (; j < PAGE_SIZE / sizeof(long); j++)
552 * snapshot_read_next - used for reading the system memory snapshot.
554 * On the first call to it @handle should point to a zeroed
555 * snapshot_handle structure. The structure gets updated and a pointer
556 * to it should be passed to this function every next time.
558 * The @count parameter should contain the number of bytes the caller
559 * wants to read from the snapshot. It must not be zero.
561 * On success the function returns a positive number. Then, the caller
562 * is allowed to read up to the returned number of bytes from the memory
563 * location computed by the data_of() macro. The number returned
564 * may be smaller than @count, but this only happens if the read would
565 * cross a page boundary otherwise.
567 * The function returns 0 to indicate the end of data stream condition,
568 * and a negative number is returned on error. In such cases the
569 * structure pointed to by @handle is not updated and should not be used
573 int snapshot_read_next(struct snapshot_handle *handle, size_t count)
575 if (handle->page > nr_meta_pages + nr_copy_pages)
578 /* This makes the buffer be freed by swsusp_free() */
579 buffer = alloc_image_page(GFP_ATOMIC, 0);
583 if (!handle->offset) {
584 init_header((struct swsusp_info *)buffer);
585 handle->buffer = buffer;
586 handle->pbe = pagedir_nosave;
588 if (handle->prev < handle->page) {
589 if (handle->page <= nr_meta_pages) {
590 handle->pbe = pack_orig_addresses(buffer, handle->pbe);
592 handle->pbe = pagedir_nosave;
594 handle->buffer = (void *)handle->pbe->address;
595 handle->pbe = handle->pbe->next;
597 handle->prev = handle->page;
599 handle->buf_offset = handle->page_offset;
600 if (handle->page_offset + count >= PAGE_SIZE) {
601 count = PAGE_SIZE - handle->page_offset;
602 handle->page_offset = 0;
605 handle->page_offset += count;
607 handle->offset += count;
612 * mark_unsafe_pages - mark the pages that cannot be used for storing
613 * the image during resume, because they conflict with the pages that
614 * had been used before suspend
617 static int mark_unsafe_pages(struct pbe *pblist)
620 unsigned long zone_pfn;
623 if (!pblist) /* a sanity check */
626 /* Clear page flags */
627 for_each_zone (zone) {
628 for (zone_pfn = 0; zone_pfn < zone->spanned_pages; ++zone_pfn)
629 if (pfn_valid(zone_pfn + zone->zone_start_pfn))
630 ClearPageNosaveFree(pfn_to_page(zone_pfn +
631 zone->zone_start_pfn));
634 /* Mark orig addresses */
635 for_each_pbe (p, pblist) {
636 if (virt_addr_valid(p->orig_address))
637 SetPageNosaveFree(virt_to_page(p->orig_address));
645 static void copy_page_backup_list(struct pbe *dst, struct pbe *src)
647 /* We assume both lists contain the same number of elements */
649 dst->orig_address = src->orig_address;
655 static int check_header(struct swsusp_info *info)
659 if (info->version_code != LINUX_VERSION_CODE)
660 reason = "kernel version";
661 if (info->num_physpages != num_physpages)
662 reason = "memory size";
663 if (strcmp(info->uts.sysname,system_utsname.sysname))
664 reason = "system type";
665 if (strcmp(info->uts.release,system_utsname.release))
666 reason = "kernel release";
667 if (strcmp(info->uts.version,system_utsname.version))
669 if (strcmp(info->uts.machine,system_utsname.machine))
672 printk(KERN_ERR "swsusp: Resume mismatch: %s\n", reason);
679 * load header - check the image header and copy data from it
682 static int load_header(struct snapshot_handle *handle,
683 struct swsusp_info *info)
688 error = check_header(info);
690 pblist = alloc_pagedir(info->image_pages, GFP_ATOMIC, 0);
693 pagedir_nosave = pblist;
694 handle->pbe = pblist;
695 nr_copy_pages = info->image_pages;
696 nr_meta_pages = info->pages - info->image_pages - 1;
702 * unpack_orig_addresses - copy the elements of @buf[] (1 page) to
703 * the PBEs in the list starting at @pbe
706 static inline struct pbe *unpack_orig_addresses(unsigned long *buf,
711 for (j = 0; j < PAGE_SIZE / sizeof(long) && pbe; j++) {
712 pbe->orig_address = buf[j];
719 * create_image - use metadata contained in the PBE list
720 * pointed to by pagedir_nosave to mark the pages that will
721 * be overwritten in the process of restoring the system
722 * memory state from the image and allocate memory for
723 * the image avoiding these pages
726 static int create_image(struct snapshot_handle *handle)
729 struct pbe *p, *pblist;
732 error = mark_unsafe_pages(p);
734 pblist = alloc_pagedir(nr_copy_pages, GFP_ATOMIC, 1);
736 copy_page_backup_list(pblist, p);
742 error = alloc_data_pages(pblist, GFP_ATOMIC, 1);
744 release_eaten_pages();
745 pagedir_nosave = pblist;
747 pagedir_nosave = NULL;
756 * snapshot_write_next - used for writing the system memory snapshot.
758 * On the first call to it @handle should point to a zeroed
759 * snapshot_handle structure. The structure gets updated and a pointer
760 * to it should be passed to this function every next time.
762 * The @count parameter should contain the number of bytes the caller
763 * wants to write to the image. It must not be zero.
765 * On success the function returns a positive number. Then, the caller
766 * is allowed to write up to the returned number of bytes to the memory
767 * location computed by the data_of() macro. The number returned
768 * may be smaller than @count, but this only happens if the write would
769 * cross a page boundary otherwise.
771 * The function returns 0 to indicate the "end of file" condition,
772 * and a negative number is returned on error. In such cases the
773 * structure pointed to by @handle is not updated and should not be used
777 int snapshot_write_next(struct snapshot_handle *handle, size_t count)
781 if (handle->prev && handle->page > nr_meta_pages + nr_copy_pages)
784 /* This makes the buffer be freed by swsusp_free() */
785 buffer = alloc_image_page(GFP_ATOMIC, 0);
790 handle->buffer = buffer;
791 if (handle->prev < handle->page) {
793 error = load_header(handle, (struct swsusp_info *)buffer);
796 } else if (handle->prev <= nr_meta_pages) {
797 handle->pbe = unpack_orig_addresses(buffer, handle->pbe);
799 error = create_image(handle);
802 handle->pbe = pagedir_nosave;
803 handle->buffer = (void *)handle->pbe->address;
806 handle->pbe = handle->pbe->next;
807 handle->buffer = (void *)handle->pbe->address;
809 handle->prev = handle->page;
811 handle->buf_offset = handle->page_offset;
812 if (handle->page_offset + count >= PAGE_SIZE) {
813 count = PAGE_SIZE - handle->page_offset;
814 handle->page_offset = 0;
817 handle->page_offset += count;
819 handle->offset += count;
823 int snapshot_image_loaded(struct snapshot_handle *handle)
825 return !(!handle->pbe || handle->pbe->next || !nr_copy_pages ||
826 handle->page <= nr_meta_pages + nr_copy_pages);