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");
127 for_each_zone (zone) {
128 if (is_highmem(zone))
129 res = save_highmem_zone(zone);
137 int restore_highmem(void)
139 printk("swsusp: Restoring Highmem\n");
140 while (highmem_copy) {
141 struct highmem_page *save = highmem_copy;
143 highmem_copy = save->next;
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);
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;}
159 static inline int pfn_is_nosave(unsigned long pfn)
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);
167 * saveable - Determine whether a page should be cloned or not.
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.
175 static struct page *saveable_page(unsigned long pfn)
182 page = pfn_to_page(pfn);
184 if (PageNosave(page))
186 if (PageReserved(page) && pfn_is_nosave(pfn))
188 if (PageNosaveFree(page))
194 unsigned int count_data_pages(void)
197 unsigned long pfn, max_zone_pfn;
200 for_each_zone (zone) {
201 if (is_highmem(zone))
203 mark_free_pages(zone);
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);
211 static inline void copy_data_page(long *dst, long *src)
215 /* copy_page and memcpy are not usable for copying task structs. */
216 for (n = PAGE_SIZE / sizeof(long); n; n--)
220 static void copy_data_pages(struct pbe *pblist)
223 unsigned long pfn, max_zone_pfn;
227 for_each_zone (zone) {
228 if (is_highmem(zone))
230 mark_free_pages(zone);
231 max_zone_pfn = zone->zone_start_pfn + zone->spanned_pages;
232 for (pfn = zone->zone_start_pfn; pfn < max_zone_pfn; pfn++) {
233 struct page *page = saveable_page(pfn);
236 void *ptr = page_address(page);
239 copy_data_page((void *)pbe->address, ptr);
240 pbe->orig_address = (unsigned long)ptr;
250 * free_pagedir - free pages allocated with alloc_pagedir()
253 static void free_pagedir(struct pbe *pblist, int clear_nosave_free)
258 pbe = (pblist + PB_PAGE_SKIP)->next;
259 ClearPageNosave(virt_to_page(pblist));
260 if (clear_nosave_free)
261 ClearPageNosaveFree(virt_to_page(pblist));
262 free_page((unsigned long)pblist);
268 * fill_pb_page - Create a list of PBEs on a given memory page
271 static inline void fill_pb_page(struct pbe *pbpage)
276 pbpage += PB_PAGE_SKIP;
279 while (++p < pbpage);
283 * create_pbe_list - Create a list of PBEs on top of a given chain
284 * of memory pages allocated with alloc_pagedir()
287 static inline void create_pbe_list(struct pbe *pblist, unsigned int nr_pages)
289 struct pbe *pbpage, *p;
290 unsigned int num = PBES_PER_PAGE;
292 for_each_pb_page (pbpage, pblist) {
296 fill_pb_page(pbpage);
297 num += PBES_PER_PAGE;
300 for (num -= PBES_PER_PAGE - 1, p = pbpage; num < nr_pages; p++, num++)
306 static unsigned int unsafe_pages;
309 * @safe_needed - on resume, for storing the PBE list and the image,
310 * we can only use memory pages that do not conflict with the pages
311 * used before suspend.
313 * The unsafe pages are marked with the PG_nosave_free flag
314 * and we count them using unsafe_pages
317 static void *alloc_image_page(gfp_t gfp_mask, int safe_needed)
321 res = (void *)get_zeroed_page(gfp_mask);
323 while (res && PageNosaveFree(virt_to_page(res))) {
324 /* The page is unsafe, mark it for swsusp_free() */
325 SetPageNosave(virt_to_page(res));
327 res = (void *)get_zeroed_page(gfp_mask);
330 SetPageNosave(virt_to_page(res));
331 SetPageNosaveFree(virt_to_page(res));
336 unsigned long get_safe_page(gfp_t gfp_mask)
338 return (unsigned long)alloc_image_page(gfp_mask, 1);
342 * alloc_pagedir - Allocate the page directory.
344 * First, determine exactly how many pages we need and
347 * We arrange the pages in a chain: each page is an array of PBES_PER_PAGE
348 * struct pbe elements (pbes) and the last element in the page points
351 * On each page we set up a list of struct_pbe elements.
354 static struct pbe *alloc_pagedir(unsigned int nr_pages, gfp_t gfp_mask,
358 struct pbe *pblist, *pbe;
363 pblist = alloc_image_page(gfp_mask, safe_needed);
364 /* FIXME: rewrite this ugly loop */
365 for (pbe = pblist, num = PBES_PER_PAGE; pbe && num < nr_pages;
366 pbe = pbe->next, num += PBES_PER_PAGE) {
368 pbe->next = alloc_image_page(gfp_mask, safe_needed);
370 if (!pbe) { /* get_zeroed_page() failed */
371 free_pagedir(pblist, 1);
374 create_pbe_list(pblist, nr_pages);
379 * Free pages we allocated for suspend. Suspend pages are alocated
380 * before atomic copy, so we need to free them after resume.
383 void swsusp_free(void)
386 unsigned long pfn, max_zone_pfn;
388 for_each_zone(zone) {
389 max_zone_pfn = zone->zone_start_pfn + zone->spanned_pages;
390 for (pfn = zone->zone_start_pfn; pfn < max_zone_pfn; pfn++)
391 if (pfn_valid(pfn)) {
392 struct page *page = pfn_to_page(pfn);
394 if (PageNosave(page) && PageNosaveFree(page)) {
395 ClearPageNosave(page);
396 ClearPageNosaveFree(page);
397 free_page((long) page_address(page));
403 pagedir_nosave = NULL;
409 * enough_free_mem - Make sure we enough free memory to snapshot.
411 * Returns TRUE or FALSE after checking the number of available
415 static int enough_free_mem(unsigned int nr_pages)
421 if (!is_highmem(zone))
422 n += zone->free_pages;
423 pr_debug("swsusp: available memory: %u pages\n", n);
424 return n > (nr_pages + PAGES_FOR_IO +
425 (nr_pages + PBES_PER_PAGE - 1) / PBES_PER_PAGE);
428 static int alloc_data_pages(struct pbe *pblist, gfp_t gfp_mask, int safe_needed)
432 for_each_pbe (p, pblist) {
433 p->address = (unsigned long)alloc_image_page(gfp_mask, safe_needed);
440 static struct pbe *swsusp_alloc(unsigned int nr_pages)
444 if (!(pblist = alloc_pagedir(nr_pages, GFP_ATOMIC | __GFP_COLD, 0))) {
445 printk(KERN_ERR "suspend: Allocating pagedir failed.\n");
449 if (alloc_data_pages(pblist, GFP_ATOMIC | __GFP_COLD, 0)) {
450 printk(KERN_ERR "suspend: Allocating image pages failed.\n");
458 asmlinkage int swsusp_save(void)
460 unsigned int nr_pages;
462 pr_debug("swsusp: critical section: \n");
465 nr_pages = count_data_pages();
466 printk("swsusp: Need to copy %u pages\n", nr_pages);
468 pr_debug("swsusp: pages needed: %u + %lu + %u, free: %u\n",
470 (nr_pages + PBES_PER_PAGE - 1) / PBES_PER_PAGE,
471 PAGES_FOR_IO, nr_free_pages());
473 if (!enough_free_mem(nr_pages)) {
474 printk(KERN_ERR "swsusp: Not enough free memory\n");
478 pagedir_nosave = swsusp_alloc(nr_pages);
482 /* During allocating of suspend pagedir, new cold pages may appear.
486 copy_data_pages(pagedir_nosave);
489 * End of critical section. From now on, we can write to memory,
490 * but we should not touch disk. This specially means we must _not_
491 * touch swap space! Except we must write out our image of course.
494 nr_copy_pages = nr_pages;
495 nr_meta_pages = (nr_pages * sizeof(long) + PAGE_SIZE - 1) >> PAGE_SHIFT;
497 printk("swsusp: critical section/: done (%d pages copied)\n", nr_pages);
501 static void init_header(struct swsusp_info *info)
503 memset(info, 0, sizeof(struct swsusp_info));
504 info->version_code = LINUX_VERSION_CODE;
505 info->num_physpages = num_physpages;
506 memcpy(&info->uts, &system_utsname, sizeof(system_utsname));
507 info->cpus = num_online_cpus();
508 info->image_pages = nr_copy_pages;
509 info->pages = nr_copy_pages + nr_meta_pages + 1;
510 info->size = info->pages;
511 info->size <<= PAGE_SHIFT;
515 * pack_orig_addresses - the .orig_address fields of the PBEs from the
516 * list starting at @pbe are stored in the array @buf[] (1 page)
519 static inline struct pbe *pack_orig_addresses(unsigned long *buf, struct pbe *pbe)
523 for (j = 0; j < PAGE_SIZE / sizeof(long) && pbe; j++) {
524 buf[j] = pbe->orig_address;
528 for (; j < PAGE_SIZE / sizeof(long); j++)
534 * snapshot_read_next - used for reading the system memory snapshot.
536 * On the first call to it @handle should point to a zeroed
537 * snapshot_handle structure. The structure gets updated and a pointer
538 * to it should be passed to this function every next time.
540 * The @count parameter should contain the number of bytes the caller
541 * wants to read from the snapshot. It must not be zero.
543 * On success the function returns a positive number. Then, the caller
544 * is allowed to read up to the returned number of bytes from the memory
545 * location computed by the data_of() macro. The number returned
546 * may be smaller than @count, but this only happens if the read would
547 * cross a page boundary otherwise.
549 * The function returns 0 to indicate the end of data stream condition,
550 * and a negative number is returned on error. In such cases the
551 * structure pointed to by @handle is not updated and should not be used
555 int snapshot_read_next(struct snapshot_handle *handle, size_t count)
557 if (handle->cur > nr_meta_pages + nr_copy_pages)
560 /* This makes the buffer be freed by swsusp_free() */
561 buffer = alloc_image_page(GFP_ATOMIC, 0);
565 if (!handle->offset) {
566 init_header((struct swsusp_info *)buffer);
567 handle->buffer = buffer;
568 handle->pbe = pagedir_nosave;
570 if (handle->prev < handle->cur) {
571 if (handle->cur <= nr_meta_pages) {
572 handle->pbe = pack_orig_addresses(buffer, handle->pbe);
574 handle->pbe = pagedir_nosave;
576 handle->buffer = (void *)handle->pbe->address;
577 handle->pbe = handle->pbe->next;
579 handle->prev = handle->cur;
581 handle->buf_offset = handle->cur_offset;
582 if (handle->cur_offset + count >= PAGE_SIZE) {
583 count = PAGE_SIZE - handle->cur_offset;
584 handle->cur_offset = 0;
587 handle->cur_offset += count;
589 handle->offset += count;
594 * mark_unsafe_pages - mark the pages that cannot be used for storing
595 * the image during resume, because they conflict with the pages that
596 * had been used before suspend
599 static int mark_unsafe_pages(struct pbe *pblist)
602 unsigned long pfn, max_zone_pfn;
605 if (!pblist) /* a sanity check */
608 /* Clear page flags */
609 for_each_zone (zone) {
610 max_zone_pfn = zone->zone_start_pfn + zone->spanned_pages;
611 for (pfn = zone->zone_start_pfn; pfn < max_zone_pfn; pfn++)
613 ClearPageNosaveFree(pfn_to_page(pfn));
616 /* Mark orig addresses */
617 for_each_pbe (p, pblist) {
618 if (virt_addr_valid(p->orig_address))
619 SetPageNosaveFree(virt_to_page(p->orig_address));
629 static void copy_page_backup_list(struct pbe *dst, struct pbe *src)
631 /* We assume both lists contain the same number of elements */
633 dst->orig_address = src->orig_address;
639 static int check_header(struct swsusp_info *info)
643 if (info->version_code != LINUX_VERSION_CODE)
644 reason = "kernel version";
645 if (info->num_physpages != num_physpages)
646 reason = "memory size";
647 if (strcmp(info->uts.sysname,system_utsname.sysname))
648 reason = "system type";
649 if (strcmp(info->uts.release,system_utsname.release))
650 reason = "kernel release";
651 if (strcmp(info->uts.version,system_utsname.version))
653 if (strcmp(info->uts.machine,system_utsname.machine))
656 printk(KERN_ERR "swsusp: Resume mismatch: %s\n", reason);
663 * load header - check the image header and copy data from it
666 static int load_header(struct snapshot_handle *handle,
667 struct swsusp_info *info)
672 error = check_header(info);
674 pblist = alloc_pagedir(info->image_pages, GFP_ATOMIC, 0);
677 pagedir_nosave = pblist;
678 handle->pbe = pblist;
679 nr_copy_pages = info->image_pages;
680 nr_meta_pages = info->pages - info->image_pages - 1;
686 * unpack_orig_addresses - copy the elements of @buf[] (1 page) to
687 * the PBEs in the list starting at @pbe
690 static inline struct pbe *unpack_orig_addresses(unsigned long *buf,
695 for (j = 0; j < PAGE_SIZE / sizeof(long) && pbe; j++) {
696 pbe->orig_address = buf[j];
703 * prepare_image - use metadata contained in the PBE list
704 * pointed to by pagedir_nosave to mark the pages that will
705 * be overwritten in the process of restoring the system
706 * memory state from the image ("unsafe" pages) and allocate
707 * memory for the image
709 * The idea is to allocate the PBE list first and then
710 * allocate as many pages as it's needed for the image data,
711 * but not to assign these pages to the PBEs initially.
712 * Instead, we just mark them as allocated and create a list
713 * of "safe" which will be used later
717 struct safe_page *next;
718 char padding[PAGE_SIZE - sizeof(void *)];
721 static struct safe_page *safe_pages;
723 static int prepare_image(struct snapshot_handle *handle)
726 unsigned int nr_pages = nr_copy_pages;
727 struct pbe *p, *pblist = NULL;
730 error = mark_unsafe_pages(p);
732 pblist = alloc_pagedir(nr_pages, GFP_ATOMIC, 1);
734 copy_page_backup_list(pblist, p);
740 if (!error && nr_pages > unsafe_pages) {
741 nr_pages -= unsafe_pages;
743 struct safe_page *ptr;
745 ptr = (struct safe_page *)get_zeroed_page(GFP_ATOMIC);
750 if (!PageNosaveFree(virt_to_page(ptr))) {
751 /* The page is "safe", add it to the list */
752 ptr->next = safe_pages;
755 /* Mark the page as allocated */
756 SetPageNosave(virt_to_page(ptr));
757 SetPageNosaveFree(virt_to_page(ptr));
761 pagedir_nosave = pblist;
769 static void *get_buffer(struct snapshot_handle *handle)
771 struct pbe *pbe = handle->pbe, *last = handle->last_pbe;
772 struct page *page = virt_to_page(pbe->orig_address);
774 if (PageNosave(page) && PageNosaveFree(page)) {
776 * We have allocated the "original" page frame and we can
777 * use it directly to store the read page
780 if (last && last->next)
782 return (void *)pbe->orig_address;
785 * The "original" page frame has not been allocated and we have to
786 * use a "safe" page frame to store the read page
788 pbe->address = (unsigned long)safe_pages;
789 safe_pages = safe_pages->next;
792 handle->last_pbe = pbe;
793 return (void *)pbe->address;
797 * snapshot_write_next - used for writing the system memory snapshot.
799 * On the first call to it @handle should point to a zeroed
800 * snapshot_handle structure. The structure gets updated and a pointer
801 * to it should be passed to this function every next time.
803 * The @count parameter should contain the number of bytes the caller
804 * wants to write to the image. It must not be zero.
806 * On success the function returns a positive number. Then, the caller
807 * is allowed to write up to the returned number of bytes to the memory
808 * location computed by the data_of() macro. The number returned
809 * may be smaller than @count, but this only happens if the write would
810 * cross a page boundary otherwise.
812 * The function returns 0 to indicate the "end of file" condition,
813 * and a negative number is returned on error. In such cases the
814 * structure pointed to by @handle is not updated and should not be used
818 int snapshot_write_next(struct snapshot_handle *handle, size_t count)
822 if (handle->prev && handle->cur > nr_meta_pages + nr_copy_pages)
825 /* This makes the buffer be freed by swsusp_free() */
826 buffer = alloc_image_page(GFP_ATOMIC, 0);
831 handle->buffer = buffer;
832 handle->sync_read = 1;
833 if (handle->prev < handle->cur) {
835 error = load_header(handle,
836 (struct swsusp_info *)buffer);
839 } else if (handle->prev <= nr_meta_pages) {
840 handle->pbe = unpack_orig_addresses(buffer,
843 error = prepare_image(handle);
846 handle->pbe = pagedir_nosave;
847 handle->last_pbe = NULL;
848 handle->buffer = get_buffer(handle);
849 handle->sync_read = 0;
852 handle->pbe = handle->pbe->next;
853 handle->buffer = get_buffer(handle);
854 handle->sync_read = 0;
856 handle->prev = handle->cur;
858 handle->buf_offset = handle->cur_offset;
859 if (handle->cur_offset + count >= PAGE_SIZE) {
860 count = PAGE_SIZE - handle->cur_offset;
861 handle->cur_offset = 0;
864 handle->cur_offset += count;
866 handle->offset += count;
870 int snapshot_image_loaded(struct snapshot_handle *handle)
872 return !(!handle->pbe || handle->pbe->next || !nr_copy_pages ||
873 handle->cur <= nr_meta_pages + nr_copy_pages);