Make symbol exports GPL.
[linux-2.6/suspend2-2.6.18.git] / kernel / power / prepare_image.c
blob96d2c9c805fecc67beb8a558633d760c247e8d59
1 /*
2 * kernel/power/prepare_image.c
4 * Copyright (C) 2003-2007 Nigel Cunningham (nigel at suspend2 net)
6 * This file is released under the GPLv2.
8 * We need to eat memory until we can:
9 * 1. Perform the save without changing anything (RAM_NEEDED < #pages)
10 * 2. Fit it all in available space (suspendActiveAllocator->available_space() >=
11 * main_storage_needed())
12 * 3. Reload the pagedir and pageset1 to places that don't collide with their
13 * final destinations, not knowing to what extent the resumed kernel will
14 * overlap with the one loaded at boot time. I think the resumed kernel
15 * should overlap completely, but I don't want to rely on this as it is
16 * an unproven assumption. We therefore assume there will be no overlap at
17 * all (worse case).
18 * 4. Meet the user's requested limit (if any) on the size of the image.
19 * The limit is in MB, so pages/256 (assuming 4K pages).
23 #include <linux/module.h>
24 #include <linux/highmem.h>
25 #include <linux/freezer.h>
26 #include <linux/hardirq.h>
28 #include "pageflags.h"
29 #include "modules.h"
30 #include "io.h"
31 #include "ui.h"
32 #include "extent.h"
33 #include "prepare_image.h"
34 #include "block_io.h"
35 #include "suspend.h"
36 #include "checksum.h"
37 #include "sysfs.h"
39 static int are_frozen = 0, num_nosave = 0;
40 static int header_space_allocated = 0;
41 static int storage_allocated = 0;
42 static int storage_available = 0;
43 int extra_pd1_pages_allowance = MIN_EXTRA_PAGES_ALLOWANCE;
45 static int extra_pages_allocated;
47 struct extras {
48 struct page *page;
49 int order;
50 struct extras *next;
53 static struct extras *extras_list;
56 * real_nr_free_pages: Count pcp pages for a zone type or all zones
57 * (-1 for all, otherwise zone_idx() result desired).
59 int real_nr_free_pages(unsigned long zone_idx_mask)
61 struct zone *zone;
62 int result = 0, i = 0, cpu;
64 /* PCP lists */
65 for_each_zone(zone) {
66 if (!populated_zone(zone))
67 continue;
69 if (!(zone_idx_mask & (1 << zone_idx(zone))))
70 continue;
72 for_each_online_cpu(cpu) {
73 struct per_cpu_pageset *pset = zone_pcp(zone, cpu);
75 for (i = 0; i < ARRAY_SIZE(pset->pcp); i++) {
76 struct per_cpu_pages *pcp;
78 pcp = &pset->pcp[i];
79 result += pcp->count;
83 result += zone->free_pages;
85 return result;
89 * Discover how much extra memory will be required by the drivers
90 * when they're asked to suspend. We can then ensure that amount
91 * of memory is available when we really want it.
93 static void get_extra_pd1_allowance(void)
95 int orig_num_free = real_nr_free_pages(all_zones_mask), final;
97 suspend_prepare_status(CLEAR_BAR, "Finding allowance for drivers.");
99 device_suspend(PMSG_FREEZE);
100 local_irq_disable(); /* irqs might have been re-enabled on us */
101 device_power_down(PMSG_FREEZE);
103 final = real_nr_free_pages(all_zones_mask);
105 device_power_up();
106 local_irq_enable();
107 device_resume();
109 extra_pd1_pages_allowance = max(
110 orig_num_free - final + MIN_EXTRA_PAGES_ALLOWANCE,
111 MIN_EXTRA_PAGES_ALLOWANCE);
114 /* suspend_free_extra_pagedir_memory
116 * Description: Free previously allocated extra pagedir memory.
118 void suspend_free_extra_pagedir_memory(void)
120 /* Free allocated pages */
121 while (extras_list) {
122 struct extras *this = extras_list;
123 int i;
125 extras_list = this->next;
127 for (i = 0; i < (1 << this->order); i++)
128 ClearPageNosave(this->page + i);
130 __free_pages(this->page, this->order);
131 kfree(this);
134 extra_pages_allocated = 0;
137 /* suspend_allocate_extra_pagedir_memory
139 * Description: Allocate memory for making the atomic copy of pagedir1 in the
140 * case where it is bigger than pagedir2.
141 * Arguments: int num_to_alloc: Number of extra pages needed.
142 * Result: int. Zero on success. One if unable to allocate enough memory.
144 static int suspend_allocate_extra_pagedir_memory(int extra_pages_needed, int highmem)
146 int j, order, num_to_alloc = extra_pages_needed - extra_pages_allocated;
147 int allocd = 0;
148 unsigned long flags = GFP_ATOMIC | __GFP_NOWARN;
150 if (num_to_alloc < 1)
151 return 0;
153 if (highmem)
154 flags |= __GFP_HIGHMEM;
156 order = fls(num_to_alloc);
157 if (order >= MAX_ORDER)
158 order = MAX_ORDER - 1;
160 while (num_to_alloc) {
161 struct page *newpage;
162 unsigned long virt;
163 struct extras *extras_entry;
165 while ((1 << order) > num_to_alloc)
166 order--;
168 virt = __get_free_pages(flags, order);
169 while (!virt && order) {
170 order--;
171 virt = __get_free_pages(flags, order);
174 if (!virt)
175 return allocd;
177 newpage = virt_to_page(virt);
179 extras_entry = (struct extras *) kmalloc(sizeof(struct extras),
180 GFP_ATOMIC);
182 if (!extras_entry) {
183 __free_pages(newpage, order);
184 return allocd;
187 extras_entry->page = newpage;
188 extras_entry->order = order;
189 extras_entry->next = NULL;
191 if (extras_list)
192 extras_entry->next = extras_list;
194 extras_list = extras_entry;
196 for (j = 0; j < (1 << order); j++) {
197 SetPageNosave(newpage + j);
198 SetPagePageset1Copy(newpage + j);
201 allocd += (1 << order);
202 extra_pages_allocated += (1 << order);
203 num_to_alloc -= (1 << order);
206 return allocd;
210 * Amount of storage needed, possibly taking into account the
211 * expected compression ratio and possibly also ignoring our
212 * allowance for extra pages.
214 static int main_storage_needed(int use_ecr,
215 int ignore_extra_pd1_allow)
217 return ((pagedir1.size + pagedir2.size +
218 (ignore_extra_pd1_allow ? 0 : extra_pd1_pages_allowance)) *
219 (use_ecr ? suspend_expected_compression_ratio() : 100) / 100);
223 * Storage needed for the image header, in bytes until the return.
225 static int header_storage_needed(void)
227 int bytes = (int) sizeof(struct suspend_header) +
228 suspend_header_storage_for_modules() +
229 suspend_pageflags_space_needed();
231 return DIV_ROUND_UP(bytes, PAGE_SIZE);
234 #ifdef CONFIG_HIGHMEM
235 static int highpages_needed_to_suspend(void)
237 int result = max_t(int,
238 (get_highmem_size(pagedir1) - get_highmem_size(pagedir2)) / 2 -
239 real_nr_free_high_pages(),
242 return result;
245 static int lowpages_needed_to_suspend(void)
247 int result = max_t(int, (get_lowmem_size(pagedir1) -
248 get_lowmem_size(pagedir2) + extra_pd1_pages_allowance)/2 -
249 real_nr_free_low_pages(), 0);
251 return result;
254 static int ram_to_suspend(void)
256 int first, second;
258 first = (1 + max_t(int, (pagedir1.size + extra_pd1_pages_allowance -
259 get_lowmem_size(pagedir2)) / 2, 0) +
260 MIN_FREE_RAM + suspend_memory_for_modules());
262 second = highpages_needed_to_suspend() + lowpages_needed_to_suspend();
264 return max(first, second);
266 #else
267 static inline int highpages_needed_to_suspend(void)
269 return 0;
272 static int ram_to_suspend(void)
274 return (1 + max_t(int, (pagedir1.size + extra_pd1_pages_allowance -
275 get_lowmem_size(pagedir2)) / 2, 0) +
276 MIN_FREE_RAM + suspend_memory_for_modules());
279 static int lowpages_needed_to_suspend(void)
281 return ram_to_suspend();
284 #endif
286 static void display_stats(int always, int sub_extra_pd1_allow)
288 char buffer[255];
289 snprintf(buffer, 254,
290 "Free:%d(%d). Sets:%d(%d),%d(%d). Header:%d. Nosave:%d-%d"
291 "=%d. Storage:%u/%u(%u). Needed:(%d+%d)%d-%d-%d)%d|(%d-%d)%d|"
292 "%d.\n",
294 /* Free */
295 real_nr_free_pages(all_zones_mask),
296 real_nr_free_low_pages(),
298 /* Sets */
299 pagedir1.size, pagedir1.size - get_highmem_size(pagedir1),
300 pagedir2.size, pagedir2.size - get_highmem_size(pagedir2),
302 /* Header */
303 header_storage_needed(),
305 /* Nosave */
306 num_nosave, extra_pages_allocated,
307 num_nosave - extra_pages_allocated,
309 /* Storage - converted to pages for comparison */
310 storage_allocated,
311 main_storage_needed(1, sub_extra_pd1_allow),
312 storage_available,
314 /* Needed */
315 MIN_FREE_RAM, suspend_memory_for_modules(),
316 ram_to_suspend(), real_nr_free_low_pages(),
317 extra_pages_allocated,
318 ram_to_suspend() - real_nr_free_low_pages() -
319 extra_pages_allocated,
320 main_storage_needed(1, sub_extra_pd1_allow), storage_available,
321 main_storage_needed(1, sub_extra_pd1_allow) - storage_available,
322 (image_size_limit > 0) ? (main_storage_needed(1,
323 sub_extra_pd1_allow) - (image_size_limit << 8)) : 0);
325 if (always)
326 printk(buffer);
327 else
328 suspend_message(SUSPEND_EAT_MEMORY, SUSPEND_MEDIUM, 1, buffer);
331 /* generate_free_page_map
333 * Description: This routine generates a bitmap of free pages from the
334 * lists used by the memory manager. We then use the bitmap
335 * to quickly calculate which pages to save and in which
336 * pagesets.
338 static void generate_free_page_map(void)
340 int order, loop, cpu;
341 struct page *page;
342 unsigned long flags, i;
343 struct zone *zone;
345 for_each_zone(zone) {
346 if (!populated_zone(zone))
347 continue;
349 spin_lock_irqsave(&zone->lock, flags);
351 for(i=0; i < zone->spanned_pages; i++)
352 ClearPageNosaveFree(pfn_to_page(
353 zone->zone_start_pfn + i));
355 for (order = MAX_ORDER - 1; order >= 0; --order)
356 list_for_each_entry(page,
357 &zone->free_area[order].free_list, lru)
358 for(loop=0; loop < (1 << order); loop++)
359 SetPageNosaveFree(page+loop);
362 for_each_online_cpu(cpu) {
363 struct per_cpu_pageset *pset = zone_pcp(zone, cpu);
365 for (i = 0; i < ARRAY_SIZE(pset->pcp); i++) {
366 struct per_cpu_pages *pcp;
367 struct page *page;
369 pcp = &pset->pcp[i];
370 list_for_each_entry(page, &pcp->list, lru)
371 SetPageNosaveFree(page);
375 spin_unlock_irqrestore(&zone->lock, flags);
379 /* size_of_free_region
381 * Description: Return the number of pages that are free, beginning with and
382 * including this one.
384 static int size_of_free_region(struct page *page)
386 struct zone *zone = page_zone(page);
387 struct page *posn = page, *last_in_zone =
388 pfn_to_page(zone->zone_start_pfn) + zone->spanned_pages - 1;
390 while (posn <= last_in_zone && PageNosaveFree(posn))
391 posn++;
392 return (posn - page);
395 /* flag_image_pages
397 * This routine generates our lists of pages to be stored in each
398 * pageset. Since we store the data using extents, and adding new
399 * extents might allocate a new extent page, this routine may well
400 * be called more than once.
402 static void flag_image_pages(void)
404 int num_free = 0;
405 unsigned long loop;
406 struct zone *zone;
408 pagedir1.size = 0;
409 pagedir2.size = 0;
411 set_highmem_size(pagedir1, 0);
412 set_highmem_size(pagedir2, 0);
414 num_nosave = 0;
416 clear_dyn_pageflags(pageset1_map);
418 generate_free_page_map();
421 * Pages not to be saved are marked Nosave irrespective of being reserved
423 for_each_zone(zone) {
424 if (!populated_zone(zone))
425 continue;
427 for (loop = 0; loop < zone->spanned_pages; loop++) {
428 unsigned long pfn = zone->zone_start_pfn + loop;
429 struct page *page;
430 int chunk_size;
432 if (!pfn_valid(pfn))
433 continue;
435 page = pfn_to_page(pfn);
437 chunk_size = size_of_free_region(page);
438 if (chunk_size) {
439 num_free += chunk_size;
440 loop += chunk_size - 1;
441 continue;
444 if (!saveable(zone, &loop)) {
445 num_nosave++;
446 continue;
449 if (PagePageset2(page)) {
450 pagedir2.size++;
451 if (PageHighMem(page))
452 inc_highmem_size(pagedir2);
453 else
454 SetPagePageset1Copy(page);
455 if (PageResave(page)) {
456 SetPagePageset1(page);
457 ClearPagePageset1Copy(page);
458 pagedir1.size++;
459 if (PageHighMem(page))
460 inc_highmem_size(pagedir1);
462 } else {
463 pagedir1.size++;
464 SetPagePageset1(page);
465 if (PageHighMem(page))
466 inc_highmem_size(pagedir1);
471 suspend_message(SUSPEND_EAT_MEMORY, SUSPEND_MEDIUM, 0,
472 "Count data pages: Set1 (%d) + Set2 (%d) + Nosave (%d) + "
473 "NumFree (%d) = %d.\n",
474 pagedir1.size, pagedir2.size, num_nosave, num_free,
475 pagedir1.size + pagedir2.size + num_nosave + num_free);
478 /* amount_needed
480 * Calculates the amount by which the image size needs to be reduced to meet
481 * our constraints.
483 static int amount_needed(int use_image_size_limit)
485 int max1 = max(ram_to_suspend() - real_nr_free_low_pages() -
486 extra_pages_allocated,
487 main_storage_needed(1, 0) - storage_available);
489 if (!use_image_size_limit || image_size_limit < 1)
490 return max1;
492 return max(max1, main_storage_needed(1, 0) - (image_size_limit << 8));
495 void suspend_recalculate_image_contents(int atomic_copy)
497 clear_dyn_pageflags(pageset1_map);
498 if (!atomic_copy) {
499 int pfn;
500 BITMAP_FOR_EACH_SET(pageset2_map, pfn)
501 ClearPagePageset1Copy(pfn_to_page(pfn));
502 /* Need to call this before getting pageset1_size! */
503 suspend_mark_pages_for_pageset2();
505 flag_image_pages();
507 if (!atomic_copy) {
508 storage_available = suspendActiveAllocator->storage_available();
509 display_stats(0, 0);
511 return;
514 /* update_image
516 * Allocate [more] memory and storage for the image.
518 static int update_image(void)
520 int result2, param_used, wanted;
522 suspend_recalculate_image_contents(0);
524 /* Include allowance for growth in pagedir1 while writing pagedir 2 */
525 wanted = pagedir1.size + extra_pd1_pages_allowance -
526 get_lowmem_size(pagedir2);
527 if (suspend_allocate_extra_pagedir_memory(wanted, 0) < wanted) {
528 suspend_message(SUSPEND_EAT_MEMORY, SUSPEND_LOW, 1,
529 "Still need to get more pages for pagedir 1.\n");
530 return 1;
533 thaw_kernel_threads();
535 param_used = main_storage_needed(1, 0);
536 if ((result2 = suspendActiveAllocator->allocate_storage(param_used))) {
537 suspend_message(SUSPEND_EAT_MEMORY, SUSPEND_LOW, 1,
538 "Allocate storage returned %d. Still need to get more"
539 " storage space for the image proper.\n",
540 result2);
541 storage_allocated = suspendActiveAllocator->storage_allocated();
542 if (freeze_processes()) {
543 set_result_state(SUSPEND_FREEZING_FAILED);
544 set_result_state(SUSPEND_ABORTED);
546 return 1;
550 * Allocate remaining storage space, if possible, up to the
551 * maximum we know we'll need. It's okay to allocate the
552 * maximum if the writer is the swapwriter, but
553 * we don't want to grab all available space on an NFS share.
554 * We therefore ignore the expected compression ratio here,
555 * thereby trying to allocate the maximum image size we could
556 * need (assuming compression doesn't expand the image), but
557 * don't complain if we can't get the full amount we're after.
560 suspendActiveAllocator->allocate_storage(
561 min(storage_available, main_storage_needed(0, 0)));
563 storage_allocated = suspendActiveAllocator->storage_allocated();
565 /* Allocate the header storage after allocating main storage
566 * so that the overhead for metadata doesn't change the amount
567 * of storage needed for the header itself.
570 param_used = header_storage_needed();
572 result2 = suspendActiveAllocator->allocate_header_space(param_used);
574 if (freeze_processes()) {
575 set_result_state(SUSPEND_FREEZING_FAILED);
576 set_result_state(SUSPEND_ABORTED);
579 if (result2) {
580 suspend_message(SUSPEND_EAT_MEMORY, SUSPEND_LOW, 1,
581 "Still need to get more storage space for header.\n");
582 return 1;
585 header_space_allocated = param_used;
587 allocate_checksum_pages();
589 suspend_recalculate_image_contents(0);
591 suspend_message(SUSPEND_EAT_MEMORY, SUSPEND_LOW, 1,
592 "Amount still needed (%d) > 0:%d. Header: %d < %d: %d,"
593 " Storage allocd: %d < %d: %d.\n",
594 amount_needed(1),
595 (amount_needed(1) > 0),
596 header_space_allocated, header_storage_needed(),
597 header_space_allocated < header_storage_needed(),
598 storage_allocated,
599 main_storage_needed(1, 1),
600 storage_allocated < main_storage_needed(1, 1));
602 suspend_cond_pause(0, NULL);
604 return ((amount_needed(1) > 0) ||
605 header_space_allocated < header_storage_needed() ||
606 storage_allocated < main_storage_needed(1, 1));
609 /* attempt_to_freeze
611 * Try to freeze processes.
614 static int attempt_to_freeze(void)
616 int result;
618 /* Stop processes before checking again */
619 thaw_processes();
620 suspend_prepare_status(CLEAR_BAR, "Freezing processes & syncing filesystems.");
621 result = freeze_processes();
623 if (result) {
624 set_result_state(SUSPEND_ABORTED);
625 set_result_state(SUSPEND_FREEZING_FAILED);
626 } else
627 are_frozen = 1;
629 return result;
632 /* eat_memory
634 * Try to free some memory, either to meet hard or soft constraints on the image
635 * characteristics.
637 * Hard constraints:
638 * - Pageset1 must be < half of memory;
639 * - We must have enough memory free at resume time to have pageset1
640 * be able to be loaded in pages that don't conflict with where it has to
641 * be restored.
642 * Soft constraints
643 * - User specificied image size limit.
645 static void eat_memory(void)
647 int amount_wanted = 0;
648 int free_flags = 0, did_eat_memory = 0;
651 * Note that if we have enough storage space and enough free memory, we
652 * may exit without eating anything. We give up when the last 10
653 * iterations ate no extra pages because we're not going to get much
654 * more anyway, but the few pages we get will take a lot of time.
656 * We freeze processes before beginning, and then unfreeze them if we
657 * need to eat memory until we think we have enough. If our attempts
658 * to freeze fail, we give up and abort.
661 suspend_recalculate_image_contents(0);
662 amount_wanted = amount_needed(1);
664 switch (image_size_limit) {
665 case -1: /* Don't eat any memory */
666 if (amount_wanted > 0) {
667 set_result_state(SUSPEND_ABORTED);
668 set_result_state(SUSPEND_WOULD_EAT_MEMORY);
669 return;
671 break;
672 case -2: /* Free caches only */
673 drop_pagecache();
674 suspend_recalculate_image_contents(0);
675 amount_wanted = amount_needed(1);
676 did_eat_memory = 1;
677 break;
678 default:
679 free_flags = GFP_ATOMIC | __GFP_HIGHMEM;
682 thaw_kernel_threads();
684 if (amount_wanted > 0 && !test_result_state(SUSPEND_ABORTED) &&
685 image_size_limit != -1) {
686 struct zone *zone;
687 int zone_idx, to_free = amount_needed(1);
689 suspend_prepare_status(CLEAR_BAR, "Seeking to free %dMB of memory.", MB(amount_wanted));
690 for (zone_idx = 0; zone_idx < MAX_NR_ZONES; zone_idx++) {
691 int zone_type_free = max_t(int, (zone_idx == ZONE_HIGHMEM) ?
692 highpages_needed_to_suspend() :
693 lowpages_needed_to_suspend(), to_free);
695 if (zone_type_free < 0)
696 break;
698 for_each_zone(zone) {
699 if (zone_idx(zone) != zone_idx)
700 continue;
702 shrink_one_zone(zone, zone_type_free);
704 did_eat_memory = 1;
706 suspend_recalculate_image_contents(0);
708 to_free = amount_needed(1);
709 zone_type_free = max_t(int, (zone_idx == ZONE_HIGHMEM) ?
710 highpages_needed_to_suspend() :
711 lowpages_needed_to_suspend(), to_free);
713 if (zone_type_free < 0)
714 break;
718 suspend_cond_pause(0, NULL);
721 if (freeze_processes()) {
722 set_result_state(SUSPEND_FREEZING_FAILED);
723 set_result_state(SUSPEND_ABORTED);
726 if (did_eat_memory) {
727 unsigned long orig_state = get_suspend_state();
728 /* Freeze_processes will call sys_sync too */
729 restore_suspend_state(orig_state);
730 suspend_recalculate_image_contents(0);
733 /* Blank out image size display */
734 suspend_update_status(100, 100, NULL);
737 /* suspend_prepare_image
739 * Entry point to the whole image preparation section.
741 * We do four things:
742 * - Freeze processes;
743 * - Ensure image size constraints are met;
744 * - Complete all the preparation for saving the image,
745 * including allocation of storage. The only memory
746 * that should be needed when we're finished is that
747 * for actually storing the image (and we know how
748 * much is needed for that because the modules tell
749 * us).
750 * - Make sure that all dirty buffers are written out.
752 #define MAX_TRIES 2
753 int suspend_prepare_image(void)
755 int result = 1, tries = 1;
757 are_frozen = 0;
759 header_space_allocated = 0;
760 storage_allocated = 0;
762 if (attempt_to_freeze())
763 return 1;
765 if (!extra_pd1_pages_allowance)
766 get_extra_pd1_allowance();
768 storage_available = suspendActiveAllocator->storage_available();
770 if (!storage_available) {
771 printk(KERN_ERR "You need some storage available to be able to suspend.\n");
772 set_result_state(SUSPEND_ABORTED);
773 set_result_state(SUSPEND_NOSTORAGE_AVAILABLE);
774 return 1;
777 do {
778 suspend_prepare_status(CLEAR_BAR, "Preparing Image. Try %d.", tries);
780 eat_memory();
782 if (test_result_state(SUSPEND_ABORTED))
783 break;
785 result = update_image();
787 suspend_cond_pause(0, NULL);
789 tries++;
791 } while (result && (tries <= MAX_TRIES) && (!test_result_state(SUSPEND_ABORTED)) &&
792 (!test_result_state(SUSPEND_UNABLE_TO_FREE_ENOUGH_MEMORY)));
794 if (!test_result_state(SUSPEND_ABORTED)) {
795 if (amount_needed(0) > 0) {
796 display_stats(1, 0);
797 abort_suspend(SUSPEND_UNABLE_TO_PREPARE_IMAGE,
798 "Unable to successfully prepare the image.\n");
799 } else {
800 result = 0;
801 suspend_cond_pause(1, "Image preparation complete.");
805 return result;
808 #ifdef CONFIG_SUSPEND2_EXPORTS
809 EXPORT_SYMBOL_GPL(real_nr_free_pages);
810 #endif