[PATCH] suspend: make progress printing prettier
[linux-2.6/mini2440.git] / kernel / power / snapshot.c
blob1b46c2da5a50566ff7a1997b9c83581d5f9c9554
1 /*
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>
15 #include <linux/mm.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>
22 #include <linux/pm.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>
33 #include <asm/io.h>
35 #include "power.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 #ifdef CONFIG_HIGHMEM
43 unsigned int count_highmem_pages(void)
45 struct zone *zone;
46 unsigned long zone_pfn;
47 unsigned int n = 0;
49 for_each_zone (zone)
50 if (is_highmem(zone)) {
51 mark_free_pages(zone);
52 for (zone_pfn = 0; zone_pfn < zone->spanned_pages; zone_pfn++) {
53 struct page *page;
54 unsigned long pfn = zone_pfn + zone->zone_start_pfn;
55 if (!pfn_valid(pfn))
56 continue;
57 page = pfn_to_page(pfn);
58 if (PageReserved(page))
59 continue;
60 if (PageNosaveFree(page))
61 continue;
62 n++;
65 return n;
68 struct highmem_page {
69 char *data;
70 struct page *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) {
81 struct page *page;
82 struct highmem_page *save;
83 void *kaddr;
84 unsigned long pfn = zone_pfn + zone->zone_start_pfn;
86 if (!(pfn%10000))
87 printk(".");
88 if (!pfn_valid(pfn))
89 continue;
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))
98 continue;
99 BUG_ON(PageNosave(page));
100 if (PageNosaveFree(page))
101 continue;
102 save = kmalloc(sizeof(struct highmem_page), GFP_ATOMIC);
103 if (!save)
104 return -ENOMEM;
105 save->next = highmem_copy;
106 save->page = page;
107 save->data = (void *) get_zeroed_page(GFP_ATOMIC);
108 if (!save->data) {
109 kfree(save);
110 return -ENOMEM;
112 kaddr = kmap_atomic(page, KM_USER0);
113 memcpy(save->data, kaddr, PAGE_SIZE);
114 kunmap_atomic(kaddr, KM_USER0);
115 highmem_copy = save;
117 return 0;
120 int save_highmem(void)
122 struct zone *zone;
123 int res = 0;
125 pr_debug("swsusp: Saving Highmem");
126 for_each_zone (zone) {
127 if (is_highmem(zone))
128 res = save_highmem_zone(zone);
129 if (res)
130 return res;
132 printk("\n");
133 return 0;
136 int restore_highmem(void)
138 printk("swsusp: Restoring Highmem\n");
139 while (highmem_copy) {
140 struct highmem_page *save = highmem_copy;
141 void *kaddr;
142 highmem_copy = save->next;
144 kaddr = kmap_atomic(save->page, KM_USER0);
145 memcpy(kaddr, save->data, PAGE_SIZE);
146 kunmap_atomic(kaddr, KM_USER0);
147 free_page((long) save->data);
148 kfree(save);
150 return 0;
152 #endif
154 static int pfn_is_nosave(unsigned long pfn)
156 unsigned long nosave_begin_pfn = __pa(&__nosave_begin) >> PAGE_SHIFT;
157 unsigned long nosave_end_pfn = PAGE_ALIGN(__pa(&__nosave_end)) >> PAGE_SHIFT;
158 return (pfn >= nosave_begin_pfn) && (pfn < nosave_end_pfn);
162 * saveable - Determine whether a page should be cloned or not.
163 * @pfn: The page
165 * We save a page if it's Reserved, and not in the range of pages
166 * statically defined as 'unsaveable', or if it isn't reserved, and
167 * isn't part of a free chunk of pages.
170 static int saveable(struct zone *zone, unsigned long *zone_pfn)
172 unsigned long pfn = *zone_pfn + zone->zone_start_pfn;
173 struct page *page;
175 if (!pfn_valid(pfn))
176 return 0;
178 page = pfn_to_page(pfn);
179 BUG_ON(PageReserved(page) && PageNosave(page));
180 if (PageNosave(page))
181 return 0;
182 if (PageReserved(page) && pfn_is_nosave(pfn))
183 return 0;
184 if (PageNosaveFree(page))
185 return 0;
187 return 1;
190 unsigned int count_data_pages(void)
192 struct zone *zone;
193 unsigned long zone_pfn;
194 unsigned int n = 0;
196 for_each_zone (zone) {
197 if (is_highmem(zone))
198 continue;
199 mark_free_pages(zone);
200 for (zone_pfn = 0; zone_pfn < zone->spanned_pages; ++zone_pfn)
201 n += saveable(zone, &zone_pfn);
203 return n;
206 static void copy_data_pages(struct pbe *pblist)
208 struct zone *zone;
209 unsigned long zone_pfn;
210 struct pbe *pbe, *p;
212 pbe = pblist;
213 for_each_zone (zone) {
214 if (is_highmem(zone))
215 continue;
216 mark_free_pages(zone);
217 /* This is necessary for swsusp_free() */
218 for_each_pb_page (p, pblist)
219 SetPageNosaveFree(virt_to_page(p));
220 for_each_pbe (p, pblist)
221 SetPageNosaveFree(virt_to_page(p->address));
222 for (zone_pfn = 0; zone_pfn < zone->spanned_pages; ++zone_pfn) {
223 if (saveable(zone, &zone_pfn)) {
224 struct page *page;
225 page = pfn_to_page(zone_pfn + zone->zone_start_pfn);
226 BUG_ON(!pbe);
227 pbe->orig_address = (unsigned long)page_address(page);
228 /* copy_page is not usable for copying task structs. */
229 memcpy((void *)pbe->address, (void *)pbe->orig_address, PAGE_SIZE);
230 pbe = pbe->next;
234 BUG_ON(pbe);
239 * free_pagedir - free pages allocated with alloc_pagedir()
242 static void free_pagedir(struct pbe *pblist)
244 struct pbe *pbe;
246 while (pblist) {
247 pbe = (pblist + PB_PAGE_SKIP)->next;
248 ClearPageNosave(virt_to_page(pblist));
249 ClearPageNosaveFree(virt_to_page(pblist));
250 free_page((unsigned long)pblist);
251 pblist = pbe;
256 * fill_pb_page - Create a list of PBEs on a given memory page
259 static inline void fill_pb_page(struct pbe *pbpage)
261 struct pbe *p;
263 p = pbpage;
264 pbpage += PB_PAGE_SKIP;
266 p->next = p + 1;
267 while (++p < pbpage);
271 * create_pbe_list - Create a list of PBEs on top of a given chain
272 * of memory pages allocated with alloc_pagedir()
275 static inline void create_pbe_list(struct pbe *pblist, unsigned int nr_pages)
277 struct pbe *pbpage, *p;
278 unsigned int num = PBES_PER_PAGE;
280 for_each_pb_page (pbpage, pblist) {
281 if (num >= nr_pages)
282 break;
284 fill_pb_page(pbpage);
285 num += PBES_PER_PAGE;
287 if (pbpage) {
288 for (num -= PBES_PER_PAGE - 1, p = pbpage; num < nr_pages; p++, num++)
289 p->next = p + 1;
290 p->next = NULL;
295 * On resume it is necessary to trace and eventually free the unsafe
296 * pages that have been allocated, because they are needed for I/O
297 * (on x86-64 we likely will "eat" these pages once again while
298 * creating the temporary page translation tables)
301 struct eaten_page {
302 struct eaten_page *next;
303 char padding[PAGE_SIZE - sizeof(void *)];
306 static struct eaten_page *eaten_pages = NULL;
308 static void release_eaten_pages(void)
310 struct eaten_page *p, *q;
312 p = eaten_pages;
313 while (p) {
314 q = p->next;
315 /* We don't want swsusp_free() to free this page again */
316 ClearPageNosave(virt_to_page(p));
317 free_page((unsigned long)p);
318 p = q;
320 eaten_pages = NULL;
324 * @safe_needed - on resume, for storing the PBE list and the image,
325 * we can only use memory pages that do not conflict with the pages
326 * which had been used before suspend.
328 * The unsafe pages are marked with the PG_nosave_free flag
330 * Allocated but unusable (ie eaten) memory pages should be marked
331 * so that swsusp_free() can release them
334 static inline void *alloc_image_page(gfp_t gfp_mask, int safe_needed)
336 void *res;
338 if (safe_needed)
339 do {
340 res = (void *)get_zeroed_page(gfp_mask);
341 if (res && PageNosaveFree(virt_to_page(res))) {
342 /* This is for swsusp_free() */
343 SetPageNosave(virt_to_page(res));
344 ((struct eaten_page *)res)->next = eaten_pages;
345 eaten_pages = res;
347 } while (res && PageNosaveFree(virt_to_page(res)));
348 else
349 res = (void *)get_zeroed_page(gfp_mask);
350 if (res) {
351 SetPageNosave(virt_to_page(res));
352 SetPageNosaveFree(virt_to_page(res));
354 return res;
357 unsigned long get_safe_page(gfp_t gfp_mask)
359 return (unsigned long)alloc_image_page(gfp_mask, 1);
363 * alloc_pagedir - Allocate the page directory.
365 * First, determine exactly how many pages we need and
366 * allocate them.
368 * We arrange the pages in a chain: each page is an array of PBES_PER_PAGE
369 * struct pbe elements (pbes) and the last element in the page points
370 * to the next page.
372 * On each page we set up a list of struct_pbe elements.
375 struct pbe *alloc_pagedir(unsigned int nr_pages, gfp_t gfp_mask, int safe_needed)
377 unsigned int num;
378 struct pbe *pblist, *pbe;
380 if (!nr_pages)
381 return NULL;
383 pblist = alloc_image_page(gfp_mask, safe_needed);
384 /* FIXME: rewrite this ugly loop */
385 for (pbe = pblist, num = PBES_PER_PAGE; pbe && num < nr_pages;
386 pbe = pbe->next, num += PBES_PER_PAGE) {
387 pbe += PB_PAGE_SKIP;
388 pbe->next = alloc_image_page(gfp_mask, safe_needed);
390 if (!pbe) { /* get_zeroed_page() failed */
391 free_pagedir(pblist);
392 pblist = NULL;
393 } else
394 create_pbe_list(pblist, nr_pages);
395 return pblist;
399 * Free pages we allocated for suspend. Suspend pages are alocated
400 * before atomic copy, so we need to free them after resume.
403 void swsusp_free(void)
405 struct zone *zone;
406 unsigned long zone_pfn;
408 for_each_zone(zone) {
409 for (zone_pfn = 0; zone_pfn < zone->spanned_pages; ++zone_pfn)
410 if (pfn_valid(zone_pfn + zone->zone_start_pfn)) {
411 struct page *page;
412 page = pfn_to_page(zone_pfn + zone->zone_start_pfn);
413 if (PageNosave(page) && PageNosaveFree(page)) {
414 ClearPageNosave(page);
415 ClearPageNosaveFree(page);
416 free_page((long) page_address(page));
420 nr_copy_pages = 0;
421 nr_meta_pages = 0;
422 pagedir_nosave = NULL;
423 buffer = NULL;
428 * enough_free_mem - Make sure we enough free memory to snapshot.
430 * Returns TRUE or FALSE after checking the number of available
431 * free pages.
434 static int enough_free_mem(unsigned int nr_pages)
436 struct zone *zone;
437 unsigned int n = 0;
439 for_each_zone (zone)
440 if (!is_highmem(zone))
441 n += zone->free_pages;
442 pr_debug("swsusp: available memory: %u pages\n", n);
443 return n > (nr_pages + PAGES_FOR_IO +
444 (nr_pages + PBES_PER_PAGE - 1) / PBES_PER_PAGE);
447 static int alloc_data_pages(struct pbe *pblist, gfp_t gfp_mask, int safe_needed)
449 struct pbe *p;
451 for_each_pbe (p, pblist) {
452 p->address = (unsigned long)alloc_image_page(gfp_mask, safe_needed);
453 if (!p->address)
454 return -ENOMEM;
456 return 0;
459 static struct pbe *swsusp_alloc(unsigned int nr_pages)
461 struct pbe *pblist;
463 if (!(pblist = alloc_pagedir(nr_pages, GFP_ATOMIC | __GFP_COLD, 0))) {
464 printk(KERN_ERR "suspend: Allocating pagedir failed.\n");
465 return NULL;
468 if (alloc_data_pages(pblist, GFP_ATOMIC | __GFP_COLD, 0)) {
469 printk(KERN_ERR "suspend: Allocating image pages failed.\n");
470 swsusp_free();
471 return NULL;
474 return pblist;
477 asmlinkage int swsusp_save(void)
479 unsigned int nr_pages;
481 pr_debug("swsusp: critical section: \n");
483 drain_local_pages();
484 nr_pages = count_data_pages();
485 printk("swsusp: Need to copy %u pages\n", nr_pages);
487 pr_debug("swsusp: pages needed: %u + %lu + %u, free: %u\n",
488 nr_pages,
489 (nr_pages + PBES_PER_PAGE - 1) / PBES_PER_PAGE,
490 PAGES_FOR_IO, nr_free_pages());
492 if (!enough_free_mem(nr_pages)) {
493 printk(KERN_ERR "swsusp: Not enough free memory\n");
494 return -ENOMEM;
497 pagedir_nosave = swsusp_alloc(nr_pages);
498 if (!pagedir_nosave)
499 return -ENOMEM;
501 /* During allocating of suspend pagedir, new cold pages may appear.
502 * Kill them.
504 drain_local_pages();
505 copy_data_pages(pagedir_nosave);
508 * End of critical section. From now on, we can write to memory,
509 * but we should not touch disk. This specially means we must _not_
510 * touch swap space! Except we must write out our image of course.
513 nr_copy_pages = nr_pages;
514 nr_meta_pages = (nr_pages * sizeof(long) + PAGE_SIZE - 1) >> PAGE_SHIFT;
516 printk("swsusp: critical section/: done (%d pages copied)\n", nr_pages);
517 return 0;
520 static void init_header(struct swsusp_info *info)
522 memset(info, 0, sizeof(struct swsusp_info));
523 info->version_code = LINUX_VERSION_CODE;
524 info->num_physpages = num_physpages;
525 memcpy(&info->uts, &system_utsname, sizeof(system_utsname));
526 info->cpus = num_online_cpus();
527 info->image_pages = nr_copy_pages;
528 info->pages = nr_copy_pages + nr_meta_pages + 1;
529 info->size = info->pages;
530 info->size <<= PAGE_SHIFT;
534 * pack_orig_addresses - the .orig_address fields of the PBEs from the
535 * list starting at @pbe are stored in the array @buf[] (1 page)
538 static inline struct pbe *pack_orig_addresses(unsigned long *buf, struct pbe *pbe)
540 int j;
542 for (j = 0; j < PAGE_SIZE / sizeof(long) && pbe; j++) {
543 buf[j] = pbe->orig_address;
544 pbe = pbe->next;
546 if (!pbe)
547 for (; j < PAGE_SIZE / sizeof(long); j++)
548 buf[j] = 0;
549 return pbe;
553 * snapshot_read_next - used for reading the system memory snapshot.
555 * On the first call to it @handle should point to a zeroed
556 * snapshot_handle structure. The structure gets updated and a pointer
557 * to it should be passed to this function every next time.
559 * The @count parameter should contain the number of bytes the caller
560 * wants to read from the snapshot. It must not be zero.
562 * On success the function returns a positive number. Then, the caller
563 * is allowed to read up to the returned number of bytes from the memory
564 * location computed by the data_of() macro. The number returned
565 * may be smaller than @count, but this only happens if the read would
566 * cross a page boundary otherwise.
568 * The function returns 0 to indicate the end of data stream condition,
569 * and a negative number is returned on error. In such cases the
570 * structure pointed to by @handle is not updated and should not be used
571 * any more.
574 int snapshot_read_next(struct snapshot_handle *handle, size_t count)
576 if (handle->page > nr_meta_pages + nr_copy_pages)
577 return 0;
578 if (!buffer) {
579 /* This makes the buffer be freed by swsusp_free() */
580 buffer = alloc_image_page(GFP_ATOMIC, 0);
581 if (!buffer)
582 return -ENOMEM;
584 if (!handle->offset) {
585 init_header((struct swsusp_info *)buffer);
586 handle->buffer = buffer;
587 handle->pbe = pagedir_nosave;
589 if (handle->prev < handle->page) {
590 if (handle->page <= nr_meta_pages) {
591 handle->pbe = pack_orig_addresses(buffer, handle->pbe);
592 if (!handle->pbe)
593 handle->pbe = pagedir_nosave;
594 } else {
595 handle->buffer = (void *)handle->pbe->address;
596 handle->pbe = handle->pbe->next;
598 handle->prev = handle->page;
600 handle->buf_offset = handle->page_offset;
601 if (handle->page_offset + count >= PAGE_SIZE) {
602 count = PAGE_SIZE - handle->page_offset;
603 handle->page_offset = 0;
604 handle->page++;
605 } else {
606 handle->page_offset += count;
608 handle->offset += count;
609 return count;
613 * mark_unsafe_pages - mark the pages that cannot be used for storing
614 * the image during resume, because they conflict with the pages that
615 * had been used before suspend
618 static int mark_unsafe_pages(struct pbe *pblist)
620 struct zone *zone;
621 unsigned long zone_pfn;
622 struct pbe *p;
624 if (!pblist) /* a sanity check */
625 return -EINVAL;
627 /* Clear page flags */
628 for_each_zone (zone) {
629 for (zone_pfn = 0; zone_pfn < zone->spanned_pages; ++zone_pfn)
630 if (pfn_valid(zone_pfn + zone->zone_start_pfn))
631 ClearPageNosaveFree(pfn_to_page(zone_pfn +
632 zone->zone_start_pfn));
635 /* Mark orig addresses */
636 for_each_pbe (p, pblist) {
637 if (virt_addr_valid(p->orig_address))
638 SetPageNosaveFree(virt_to_page(p->orig_address));
639 else
640 return -EFAULT;
643 return 0;
646 static void copy_page_backup_list(struct pbe *dst, struct pbe *src)
648 /* We assume both lists contain the same number of elements */
649 while (src) {
650 dst->orig_address = src->orig_address;
651 dst = dst->next;
652 src = src->next;
656 static int check_header(struct swsusp_info *info)
658 char *reason = NULL;
660 if (info->version_code != LINUX_VERSION_CODE)
661 reason = "kernel version";
662 if (info->num_physpages != num_physpages)
663 reason = "memory size";
664 if (strcmp(info->uts.sysname,system_utsname.sysname))
665 reason = "system type";
666 if (strcmp(info->uts.release,system_utsname.release))
667 reason = "kernel release";
668 if (strcmp(info->uts.version,system_utsname.version))
669 reason = "version";
670 if (strcmp(info->uts.machine,system_utsname.machine))
671 reason = "machine";
672 if (reason) {
673 printk(KERN_ERR "swsusp: Resume mismatch: %s\n", reason);
674 return -EPERM;
676 return 0;
680 * load header - check the image header and copy data from it
683 static int load_header(struct snapshot_handle *handle,
684 struct swsusp_info *info)
686 int error;
687 struct pbe *pblist;
689 error = check_header(info);
690 if (!error) {
691 pblist = alloc_pagedir(info->image_pages, GFP_ATOMIC, 0);
692 if (!pblist)
693 return -ENOMEM;
694 pagedir_nosave = pblist;
695 handle->pbe = pblist;
696 nr_copy_pages = info->image_pages;
697 nr_meta_pages = info->pages - info->image_pages - 1;
699 return error;
703 * unpack_orig_addresses - copy the elements of @buf[] (1 page) to
704 * the PBEs in the list starting at @pbe
707 static inline struct pbe *unpack_orig_addresses(unsigned long *buf,
708 struct pbe *pbe)
710 int j;
712 for (j = 0; j < PAGE_SIZE / sizeof(long) && pbe; j++) {
713 pbe->orig_address = buf[j];
714 pbe = pbe->next;
716 return pbe;
720 * create_image - use metadata contained in the PBE list
721 * pointed to by pagedir_nosave to mark the pages that will
722 * be overwritten in the process of restoring the system
723 * memory state from the image and allocate memory for
724 * the image avoiding these pages
727 static int create_image(struct snapshot_handle *handle)
729 int error = 0;
730 struct pbe *p, *pblist;
732 p = pagedir_nosave;
733 error = mark_unsafe_pages(p);
734 if (!error) {
735 pblist = alloc_pagedir(nr_copy_pages, GFP_ATOMIC, 1);
736 if (pblist)
737 copy_page_backup_list(pblist, p);
738 free_pagedir(p);
739 if (!pblist)
740 error = -ENOMEM;
742 if (!error)
743 error = alloc_data_pages(pblist, GFP_ATOMIC, 1);
744 if (!error) {
745 release_eaten_pages();
746 pagedir_nosave = pblist;
747 } else {
748 pagedir_nosave = NULL;
749 handle->pbe = NULL;
750 nr_copy_pages = 0;
751 nr_meta_pages = 0;
753 return error;
757 * snapshot_write_next - used for writing the system memory snapshot.
759 * On the first call to it @handle should point to a zeroed
760 * snapshot_handle structure. The structure gets updated and a pointer
761 * to it should be passed to this function every next time.
763 * The @count parameter should contain the number of bytes the caller
764 * wants to write to the image. It must not be zero.
766 * On success the function returns a positive number. Then, the caller
767 * is allowed to write up to the returned number of bytes to the memory
768 * location computed by the data_of() macro. The number returned
769 * may be smaller than @count, but this only happens if the write would
770 * cross a page boundary otherwise.
772 * The function returns 0 to indicate the "end of file" condition,
773 * and a negative number is returned on error. In such cases the
774 * structure pointed to by @handle is not updated and should not be used
775 * any more.
778 int snapshot_write_next(struct snapshot_handle *handle, size_t count)
780 int error = 0;
782 if (handle->prev && handle->page > nr_meta_pages + nr_copy_pages)
783 return 0;
784 if (!buffer) {
785 /* This makes the buffer be freed by swsusp_free() */
786 buffer = alloc_image_page(GFP_ATOMIC, 0);
787 if (!buffer)
788 return -ENOMEM;
790 if (!handle->offset)
791 handle->buffer = buffer;
792 if (handle->prev < handle->page) {
793 if (!handle->prev) {
794 error = load_header(handle, (struct swsusp_info *)buffer);
795 if (error)
796 return error;
797 } else if (handle->prev <= nr_meta_pages) {
798 handle->pbe = unpack_orig_addresses(buffer, handle->pbe);
799 if (!handle->pbe) {
800 error = create_image(handle);
801 if (error)
802 return error;
803 handle->pbe = pagedir_nosave;
804 handle->buffer = (void *)handle->pbe->address;
806 } else {
807 handle->pbe = handle->pbe->next;
808 handle->buffer = (void *)handle->pbe->address;
810 handle->prev = handle->page;
812 handle->buf_offset = handle->page_offset;
813 if (handle->page_offset + count >= PAGE_SIZE) {
814 count = PAGE_SIZE - handle->page_offset;
815 handle->page_offset = 0;
816 handle->page++;
817 } else {
818 handle->page_offset += count;
820 handle->offset += count;
821 return count;
824 int snapshot_image_loaded(struct snapshot_handle *handle)
826 return !(!handle->pbe || handle->pbe->next || !nr_copy_pages ||
827 handle->page <= nr_meta_pages + nr_copy_pages);