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;
42 struct arch_saveable_page {
46 struct arch_saveable_page *next;
48 static struct arch_saveable_page *arch_pages;
50 int swsusp_add_arch_pages(unsigned long start, unsigned long end)
52 struct arch_saveable_page *tmp;
55 tmp = kzalloc(sizeof(struct arch_saveable_page), GFP_KERNEL);
59 tmp->end = ((start >> PAGE_SHIFT) + 1) << PAGE_SHIFT;
62 tmp->next = arch_pages;
69 static unsigned int count_arch_pages(void)
71 unsigned int count = 0;
72 struct arch_saveable_page *tmp = arch_pages;
80 static int save_arch_mem(void)
83 struct arch_saveable_page *tmp = arch_pages;
86 pr_debug("swsusp: Saving arch specific memory");
88 tmp->data = (char *)__get_free_page(GFP_ATOMIC);
91 offset = tmp->start - (tmp->start & PAGE_MASK);
92 /* arch pages might haven't a 'struct page' */
93 kaddr = kmap_atomic_pfn(tmp->start >> PAGE_SHIFT, KM_USER0);
94 memcpy(tmp->data + offset, kaddr + offset,
95 tmp->end - tmp->start);
96 kunmap_atomic(kaddr, KM_USER0);
103 static int restore_arch_mem(void)
106 struct arch_saveable_page *tmp = arch_pages;
112 offset = tmp->start - (tmp->start & PAGE_MASK);
113 kaddr = kmap_atomic_pfn(tmp->start >> PAGE_SHIFT, KM_USER0);
114 memcpy(kaddr + offset, tmp->data + offset,
115 tmp->end - tmp->start);
116 kunmap_atomic(kaddr, KM_USER0);
117 free_page((long)tmp->data);
124 #ifdef CONFIG_HIGHMEM
125 static unsigned int count_highmem_pages(void)
128 unsigned long zone_pfn;
132 if (is_highmem(zone)) {
133 mark_free_pages(zone);
134 for (zone_pfn = 0; zone_pfn < zone->spanned_pages; zone_pfn++) {
136 unsigned long pfn = zone_pfn + zone->zone_start_pfn;
139 page = pfn_to_page(pfn);
140 if (PageReserved(page))
142 if (PageNosaveFree(page))
150 struct highmem_page {
153 struct highmem_page *next;
156 static struct highmem_page *highmem_copy;
158 static int save_highmem_zone(struct zone *zone)
160 unsigned long zone_pfn;
161 mark_free_pages(zone);
162 for (zone_pfn = 0; zone_pfn < zone->spanned_pages; ++zone_pfn) {
164 struct highmem_page *save;
166 unsigned long pfn = zone_pfn + zone->zone_start_pfn;
172 page = pfn_to_page(pfn);
174 * This condition results from rvmalloc() sans vmalloc_32()
175 * and architectural memory reservations. This should be
176 * corrected eventually when the cases giving rise to this
177 * are better understood.
179 if (PageReserved(page))
181 BUG_ON(PageNosave(page));
182 if (PageNosaveFree(page))
184 save = kmalloc(sizeof(struct highmem_page), GFP_ATOMIC);
187 save->next = highmem_copy;
189 save->data = (void *) get_zeroed_page(GFP_ATOMIC);
194 kaddr = kmap_atomic(page, KM_USER0);
195 memcpy(save->data, kaddr, PAGE_SIZE);
196 kunmap_atomic(kaddr, KM_USER0);
202 static int save_highmem(void)
207 pr_debug("swsusp: Saving Highmem");
209 for_each_zone (zone) {
210 if (is_highmem(zone))
211 res = save_highmem_zone(zone);
219 static int restore_highmem(void)
221 printk("swsusp: Restoring Highmem\n");
222 while (highmem_copy) {
223 struct highmem_page *save = highmem_copy;
225 highmem_copy = save->next;
227 kaddr = kmap_atomic(save->page, KM_USER0);
228 memcpy(kaddr, save->data, PAGE_SIZE);
229 kunmap_atomic(kaddr, KM_USER0);
230 free_page((long) save->data);
236 static inline unsigned int count_highmem_pages(void) {return 0;}
237 static inline int save_highmem(void) {return 0;}
238 static inline int restore_highmem(void) {return 0;}
241 unsigned int count_special_pages(void)
243 return count_arch_pages() + count_highmem_pages();
246 int save_special_mem(void)
249 ret = save_arch_mem();
251 ret = save_highmem();
255 int restore_special_mem(void)
258 ret = restore_arch_mem();
260 ret = restore_highmem();
264 static int pfn_is_nosave(unsigned long pfn)
266 unsigned long nosave_begin_pfn = __pa(&__nosave_begin) >> PAGE_SHIFT;
267 unsigned long nosave_end_pfn = PAGE_ALIGN(__pa(&__nosave_end)) >> PAGE_SHIFT;
268 return (pfn >= nosave_begin_pfn) && (pfn < nosave_end_pfn);
272 * saveable - Determine whether a page should be cloned or not.
275 * We save a page if it's Reserved, and not in the range of pages
276 * statically defined as 'unsaveable', or if it isn't reserved, and
277 * isn't part of a free chunk of pages.
280 static int saveable(struct zone *zone, unsigned long *zone_pfn)
282 unsigned long pfn = *zone_pfn + zone->zone_start_pfn;
288 page = pfn_to_page(pfn);
289 if (PageNosave(page))
291 if (PageReserved(page) && pfn_is_nosave(pfn))
293 if (PageNosaveFree(page))
299 unsigned int count_data_pages(void)
302 unsigned long zone_pfn;
305 for_each_zone (zone) {
306 if (is_highmem(zone))
308 mark_free_pages(zone);
309 for (zone_pfn = 0; zone_pfn < zone->spanned_pages; ++zone_pfn)
310 n += saveable(zone, &zone_pfn);
315 static void copy_data_pages(struct pbe *pblist)
318 unsigned long zone_pfn;
322 for_each_zone (zone) {
323 if (is_highmem(zone))
325 mark_free_pages(zone);
326 /* This is necessary for swsusp_free() */
327 for_each_pb_page (p, pblist)
328 SetPageNosaveFree(virt_to_page(p));
329 for_each_pbe (p, pblist)
330 SetPageNosaveFree(virt_to_page(p->address));
331 for (zone_pfn = 0; zone_pfn < zone->spanned_pages; ++zone_pfn) {
332 if (saveable(zone, &zone_pfn)) {
334 page = pfn_to_page(zone_pfn + zone->zone_start_pfn);
336 pbe->orig_address = (unsigned long)page_address(page);
337 /* copy_page is not usable for copying task structs. */
338 memcpy((void *)pbe->address, (void *)pbe->orig_address, PAGE_SIZE);
348 * free_pagedir - free pages allocated with alloc_pagedir()
351 static void free_pagedir(struct pbe *pblist, int clear_nosave_free)
356 pbe = (pblist + PB_PAGE_SKIP)->next;
357 ClearPageNosave(virt_to_page(pblist));
358 if (clear_nosave_free)
359 ClearPageNosaveFree(virt_to_page(pblist));
360 free_page((unsigned long)pblist);
366 * fill_pb_page - Create a list of PBEs on a given memory page
369 static inline void fill_pb_page(struct pbe *pbpage)
374 pbpage += PB_PAGE_SKIP;
377 while (++p < pbpage);
381 * create_pbe_list - Create a list of PBEs on top of a given chain
382 * of memory pages allocated with alloc_pagedir()
385 static inline void create_pbe_list(struct pbe *pblist, unsigned int nr_pages)
387 struct pbe *pbpage, *p;
388 unsigned int num = PBES_PER_PAGE;
390 for_each_pb_page (pbpage, pblist) {
394 fill_pb_page(pbpage);
395 num += PBES_PER_PAGE;
398 for (num -= PBES_PER_PAGE - 1, p = pbpage; num < nr_pages; p++, num++)
404 static unsigned int unsafe_pages;
407 * @safe_needed - on resume, for storing the PBE list and the image,
408 * we can only use memory pages that do not conflict with the pages
409 * used before suspend.
411 * The unsafe pages are marked with the PG_nosave_free flag
412 * and we count them using unsafe_pages
415 static inline void *alloc_image_page(gfp_t gfp_mask, int safe_needed)
419 res = (void *)get_zeroed_page(gfp_mask);
421 while (res && PageNosaveFree(virt_to_page(res))) {
422 /* The page is unsafe, mark it for swsusp_free() */
423 SetPageNosave(virt_to_page(res));
425 res = (void *)get_zeroed_page(gfp_mask);
428 SetPageNosave(virt_to_page(res));
429 SetPageNosaveFree(virt_to_page(res));
434 unsigned long get_safe_page(gfp_t gfp_mask)
436 return (unsigned long)alloc_image_page(gfp_mask, 1);
440 * alloc_pagedir - Allocate the page directory.
442 * First, determine exactly how many pages we need and
445 * We arrange the pages in a chain: each page is an array of PBES_PER_PAGE
446 * struct pbe elements (pbes) and the last element in the page points
449 * On each page we set up a list of struct_pbe elements.
452 static struct pbe *alloc_pagedir(unsigned int nr_pages, gfp_t gfp_mask,
456 struct pbe *pblist, *pbe;
461 pblist = alloc_image_page(gfp_mask, safe_needed);
462 /* FIXME: rewrite this ugly loop */
463 for (pbe = pblist, num = PBES_PER_PAGE; pbe && num < nr_pages;
464 pbe = pbe->next, num += PBES_PER_PAGE) {
466 pbe->next = alloc_image_page(gfp_mask, safe_needed);
468 if (!pbe) { /* get_zeroed_page() failed */
469 free_pagedir(pblist, 1);
472 create_pbe_list(pblist, nr_pages);
477 * Free pages we allocated for suspend. Suspend pages are alocated
478 * before atomic copy, so we need to free them after resume.
481 void swsusp_free(void)
484 unsigned long zone_pfn;
486 for_each_zone(zone) {
487 for (zone_pfn = 0; zone_pfn < zone->spanned_pages; ++zone_pfn)
488 if (pfn_valid(zone_pfn + zone->zone_start_pfn)) {
490 page = pfn_to_page(zone_pfn + zone->zone_start_pfn);
491 if (PageNosave(page) && PageNosaveFree(page)) {
492 ClearPageNosave(page);
493 ClearPageNosaveFree(page);
494 free_page((long) page_address(page));
500 pagedir_nosave = NULL;
506 * enough_free_mem - Make sure we enough free memory to snapshot.
508 * Returns TRUE or FALSE after checking the number of available
512 static int enough_free_mem(unsigned int nr_pages)
518 if (!is_highmem(zone))
519 n += zone->free_pages;
520 pr_debug("swsusp: available memory: %u pages\n", n);
521 return n > (nr_pages + PAGES_FOR_IO +
522 (nr_pages + PBES_PER_PAGE - 1) / PBES_PER_PAGE);
525 static int alloc_data_pages(struct pbe *pblist, gfp_t gfp_mask, int safe_needed)
529 for_each_pbe (p, pblist) {
530 p->address = (unsigned long)alloc_image_page(gfp_mask, safe_needed);
537 static struct pbe *swsusp_alloc(unsigned int nr_pages)
541 if (!(pblist = alloc_pagedir(nr_pages, GFP_ATOMIC | __GFP_COLD, 0))) {
542 printk(KERN_ERR "suspend: Allocating pagedir failed.\n");
546 if (alloc_data_pages(pblist, GFP_ATOMIC | __GFP_COLD, 0)) {
547 printk(KERN_ERR "suspend: Allocating image pages failed.\n");
555 asmlinkage int swsusp_save(void)
557 unsigned int nr_pages;
559 pr_debug("swsusp: critical section: \n");
562 nr_pages = count_data_pages();
563 printk("swsusp: Need to copy %u pages\n", nr_pages);
565 pr_debug("swsusp: pages needed: %u + %lu + %u, free: %u\n",
567 (nr_pages + PBES_PER_PAGE - 1) / PBES_PER_PAGE,
568 PAGES_FOR_IO, nr_free_pages());
570 if (!enough_free_mem(nr_pages)) {
571 printk(KERN_ERR "swsusp: Not enough free memory\n");
575 pagedir_nosave = swsusp_alloc(nr_pages);
579 /* During allocating of suspend pagedir, new cold pages may appear.
583 copy_data_pages(pagedir_nosave);
586 * End of critical section. From now on, we can write to memory,
587 * but we should not touch disk. This specially means we must _not_
588 * touch swap space! Except we must write out our image of course.
591 nr_copy_pages = nr_pages;
592 nr_meta_pages = (nr_pages * sizeof(long) + PAGE_SIZE - 1) >> PAGE_SHIFT;
594 printk("swsusp: critical section/: done (%d pages copied)\n", nr_pages);
598 static void init_header(struct swsusp_info *info)
600 memset(info, 0, sizeof(struct swsusp_info));
601 info->version_code = LINUX_VERSION_CODE;
602 info->num_physpages = num_physpages;
603 memcpy(&info->uts, &system_utsname, sizeof(system_utsname));
604 info->cpus = num_online_cpus();
605 info->image_pages = nr_copy_pages;
606 info->pages = nr_copy_pages + nr_meta_pages + 1;
607 info->size = info->pages;
608 info->size <<= PAGE_SHIFT;
612 * pack_orig_addresses - the .orig_address fields of the PBEs from the
613 * list starting at @pbe are stored in the array @buf[] (1 page)
616 static inline struct pbe *pack_orig_addresses(unsigned long *buf, struct pbe *pbe)
620 for (j = 0; j < PAGE_SIZE / sizeof(long) && pbe; j++) {
621 buf[j] = pbe->orig_address;
625 for (; j < PAGE_SIZE / sizeof(long); j++)
631 * snapshot_read_next - used for reading the system memory snapshot.
633 * On the first call to it @handle should point to a zeroed
634 * snapshot_handle structure. The structure gets updated and a pointer
635 * to it should be passed to this function every next time.
637 * The @count parameter should contain the number of bytes the caller
638 * wants to read from the snapshot. It must not be zero.
640 * On success the function returns a positive number. Then, the caller
641 * is allowed to read up to the returned number of bytes from the memory
642 * location computed by the data_of() macro. The number returned
643 * may be smaller than @count, but this only happens if the read would
644 * cross a page boundary otherwise.
646 * The function returns 0 to indicate the end of data stream condition,
647 * and a negative number is returned on error. In such cases the
648 * structure pointed to by @handle is not updated and should not be used
652 int snapshot_read_next(struct snapshot_handle *handle, size_t count)
654 if (handle->page > nr_meta_pages + nr_copy_pages)
657 /* This makes the buffer be freed by swsusp_free() */
658 buffer = alloc_image_page(GFP_ATOMIC, 0);
662 if (!handle->offset) {
663 init_header((struct swsusp_info *)buffer);
664 handle->buffer = buffer;
665 handle->pbe = pagedir_nosave;
667 if (handle->prev < handle->page) {
668 if (handle->page <= nr_meta_pages) {
669 handle->pbe = pack_orig_addresses(buffer, handle->pbe);
671 handle->pbe = pagedir_nosave;
673 handle->buffer = (void *)handle->pbe->address;
674 handle->pbe = handle->pbe->next;
676 handle->prev = handle->page;
678 handle->buf_offset = handle->page_offset;
679 if (handle->page_offset + count >= PAGE_SIZE) {
680 count = PAGE_SIZE - handle->page_offset;
681 handle->page_offset = 0;
684 handle->page_offset += count;
686 handle->offset += count;
691 * mark_unsafe_pages - mark the pages that cannot be used for storing
692 * the image during resume, because they conflict with the pages that
693 * had been used before suspend
696 static int mark_unsafe_pages(struct pbe *pblist)
699 unsigned long zone_pfn;
702 if (!pblist) /* a sanity check */
705 /* Clear page flags */
706 for_each_zone (zone) {
707 for (zone_pfn = 0; zone_pfn < zone->spanned_pages; ++zone_pfn)
708 if (pfn_valid(zone_pfn + zone->zone_start_pfn))
709 ClearPageNosaveFree(pfn_to_page(zone_pfn +
710 zone->zone_start_pfn));
713 /* Mark orig addresses */
714 for_each_pbe (p, pblist) {
715 if (virt_addr_valid(p->orig_address))
716 SetPageNosaveFree(virt_to_page(p->orig_address));
726 static void copy_page_backup_list(struct pbe *dst, struct pbe *src)
728 /* We assume both lists contain the same number of elements */
730 dst->orig_address = src->orig_address;
736 static int check_header(struct swsusp_info *info)
740 if (info->version_code != LINUX_VERSION_CODE)
741 reason = "kernel version";
742 if (info->num_physpages != num_physpages)
743 reason = "memory size";
744 if (strcmp(info->uts.sysname,system_utsname.sysname))
745 reason = "system type";
746 if (strcmp(info->uts.release,system_utsname.release))
747 reason = "kernel release";
748 if (strcmp(info->uts.version,system_utsname.version))
750 if (strcmp(info->uts.machine,system_utsname.machine))
753 printk(KERN_ERR "swsusp: Resume mismatch: %s\n", reason);
760 * load header - check the image header and copy data from it
763 static int load_header(struct snapshot_handle *handle,
764 struct swsusp_info *info)
769 error = check_header(info);
771 pblist = alloc_pagedir(info->image_pages, GFP_ATOMIC, 0);
774 pagedir_nosave = pblist;
775 handle->pbe = pblist;
776 nr_copy_pages = info->image_pages;
777 nr_meta_pages = info->pages - info->image_pages - 1;
783 * unpack_orig_addresses - copy the elements of @buf[] (1 page) to
784 * the PBEs in the list starting at @pbe
787 static inline struct pbe *unpack_orig_addresses(unsigned long *buf,
792 for (j = 0; j < PAGE_SIZE / sizeof(long) && pbe; j++) {
793 pbe->orig_address = buf[j];
800 * prepare_image - use metadata contained in the PBE list
801 * pointed to by pagedir_nosave to mark the pages that will
802 * be overwritten in the process of restoring the system
803 * memory state from the image ("unsafe" pages) and allocate
804 * memory for the image
806 * The idea is to allocate the PBE list first and then
807 * allocate as many pages as it's needed for the image data,
808 * but not to assign these pages to the PBEs initially.
809 * Instead, we just mark them as allocated and create a list
810 * of "safe" which will be used later
814 struct safe_page *next;
815 char padding[PAGE_SIZE - sizeof(void *)];
818 static struct safe_page *safe_pages;
820 static int prepare_image(struct snapshot_handle *handle)
823 unsigned int nr_pages = nr_copy_pages;
824 struct pbe *p, *pblist = NULL;
827 error = mark_unsafe_pages(p);
829 pblist = alloc_pagedir(nr_pages, GFP_ATOMIC, 1);
831 copy_page_backup_list(pblist, p);
837 if (!error && nr_pages > unsafe_pages) {
838 nr_pages -= unsafe_pages;
840 struct safe_page *ptr;
842 ptr = (struct safe_page *)get_zeroed_page(GFP_ATOMIC);
847 if (!PageNosaveFree(virt_to_page(ptr))) {
848 /* The page is "safe", add it to the list */
849 ptr->next = safe_pages;
852 /* Mark the page as allocated */
853 SetPageNosave(virt_to_page(ptr));
854 SetPageNosaveFree(virt_to_page(ptr));
858 pagedir_nosave = pblist;
866 static void *get_buffer(struct snapshot_handle *handle)
868 struct pbe *pbe = handle->pbe, *last = handle->last_pbe;
869 struct page *page = virt_to_page(pbe->orig_address);
871 if (PageNosave(page) && PageNosaveFree(page)) {
873 * We have allocated the "original" page frame and we can
874 * use it directly to store the read page
877 if (last && last->next)
879 return (void *)pbe->orig_address;
882 * The "original" page frame has not been allocated and we have to
883 * use a "safe" page frame to store the read page
885 pbe->address = (unsigned long)safe_pages;
886 safe_pages = safe_pages->next;
889 handle->last_pbe = pbe;
890 return (void *)pbe->address;
894 * snapshot_write_next - used for writing the system memory snapshot.
896 * On the first call to it @handle should point to a zeroed
897 * snapshot_handle structure. The structure gets updated and a pointer
898 * to it should be passed to this function every next time.
900 * The @count parameter should contain the number of bytes the caller
901 * wants to write to the image. It must not be zero.
903 * On success the function returns a positive number. Then, the caller
904 * is allowed to write up to the returned number of bytes to the memory
905 * location computed by the data_of() macro. The number returned
906 * may be smaller than @count, but this only happens if the write would
907 * cross a page boundary otherwise.
909 * The function returns 0 to indicate the "end of file" condition,
910 * and a negative number is returned on error. In such cases the
911 * structure pointed to by @handle is not updated and should not be used
915 int snapshot_write_next(struct snapshot_handle *handle, size_t count)
919 if (handle->prev && handle->page > nr_meta_pages + nr_copy_pages)
922 /* This makes the buffer be freed by swsusp_free() */
923 buffer = alloc_image_page(GFP_ATOMIC, 0);
928 handle->buffer = buffer;
929 if (handle->prev < handle->page) {
931 error = load_header(handle, (struct swsusp_info *)buffer);
934 } else if (handle->prev <= nr_meta_pages) {
935 handle->pbe = unpack_orig_addresses(buffer, handle->pbe);
937 error = prepare_image(handle);
940 handle->pbe = pagedir_nosave;
941 handle->last_pbe = NULL;
942 handle->buffer = get_buffer(handle);
945 handle->pbe = handle->pbe->next;
946 handle->buffer = get_buffer(handle);
948 handle->prev = handle->page;
950 handle->buf_offset = handle->page_offset;
951 if (handle->page_offset + count >= PAGE_SIZE) {
952 count = PAGE_SIZE - handle->page_offset;
953 handle->page_offset = 0;
956 handle->page_offset += count;
958 handle->offset += count;
962 int snapshot_image_loaded(struct snapshot_handle *handle)
964 return !(!handle->pbe || handle->pbe->next || !nr_copy_pages ||
965 handle->page <= nr_meta_pages + nr_copy_pages);