Merge master.kernel.org:/pub/scm/linux/kernel/git/lethal/sh-2.6.23
[sfrench/cifs-2.6.git] / kernel / power / snapshot.c
1 /*
2  * linux/kernel/power/snapshot.c
3  *
4  * This file provides system snapshot/restore functionality for swsusp.
5  *
6  * Copyright (C) 1998-2005 Pavel Machek <pavel@suse.cz>
7  * Copyright (C) 2006 Rafael J. Wysocki <rjw@sisk.pl>
8  *
9  * This file is released under the GPLv2.
10  *
11  */
12
13 #include <linux/version.h>
14 #include <linux/module.h>
15 #include <linux/mm.h>
16 #include <linux/suspend.h>
17 #include <linux/delay.h>
18 #include <linux/bitops.h>
19 #include <linux/spinlock.h>
20 #include <linux/kernel.h>
21 #include <linux/pm.h>
22 #include <linux/device.h>
23 #include <linux/init.h>
24 #include <linux/bootmem.h>
25 #include <linux/syscalls.h>
26 #include <linux/console.h>
27 #include <linux/highmem.h>
28
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>
34
35 #include "power.h"
36
37 static int swsusp_page_is_free(struct page *);
38 static void swsusp_set_page_forbidden(struct page *);
39 static void swsusp_unset_page_forbidden(struct page *);
40
41 /* List of PBEs needed for restoring the pages that were allocated before
42  * the suspend and included in the suspend image, but have also been
43  * allocated by the "resume" kernel, so their contents cannot be written
44  * directly to their "original" page frames.
45  */
46 struct pbe *restore_pblist;
47
48 /* Pointer to an auxiliary buffer (1 page) */
49 static void *buffer;
50
51 /**
52  *      @safe_needed - on resume, for storing the PBE list and the image,
53  *      we can only use memory pages that do not conflict with the pages
54  *      used before suspend.  The unsafe pages have PageNosaveFree set
55  *      and we count them using unsafe_pages.
56  *
57  *      Each allocated image page is marked as PageNosave and PageNosaveFree
58  *      so that swsusp_free() can release it.
59  */
60
61 #define PG_ANY          0
62 #define PG_SAFE         1
63 #define PG_UNSAFE_CLEAR 1
64 #define PG_UNSAFE_KEEP  0
65
66 static unsigned int allocated_unsafe_pages;
67
68 static void *get_image_page(gfp_t gfp_mask, int safe_needed)
69 {
70         void *res;
71
72         res = (void *)get_zeroed_page(gfp_mask);
73         if (safe_needed)
74                 while (res && swsusp_page_is_free(virt_to_page(res))) {
75                         /* The page is unsafe, mark it for swsusp_free() */
76                         swsusp_set_page_forbidden(virt_to_page(res));
77                         allocated_unsafe_pages++;
78                         res = (void *)get_zeroed_page(gfp_mask);
79                 }
80         if (res) {
81                 swsusp_set_page_forbidden(virt_to_page(res));
82                 swsusp_set_page_free(virt_to_page(res));
83         }
84         return res;
85 }
86
87 unsigned long get_safe_page(gfp_t gfp_mask)
88 {
89         return (unsigned long)get_image_page(gfp_mask, PG_SAFE);
90 }
91
92 static struct page *alloc_image_page(gfp_t gfp_mask)
93 {
94         struct page *page;
95
96         page = alloc_page(gfp_mask);
97         if (page) {
98                 swsusp_set_page_forbidden(page);
99                 swsusp_set_page_free(page);
100         }
101         return page;
102 }
103
104 /**
105  *      free_image_page - free page represented by @addr, allocated with
106  *      get_image_page (page flags set by it must be cleared)
107  */
108
109 static inline void free_image_page(void *addr, int clear_nosave_free)
110 {
111         struct page *page;
112
113         BUG_ON(!virt_addr_valid(addr));
114
115         page = virt_to_page(addr);
116
117         swsusp_unset_page_forbidden(page);
118         if (clear_nosave_free)
119                 swsusp_unset_page_free(page);
120
121         __free_page(page);
122 }
123
124 /* struct linked_page is used to build chains of pages */
125
126 #define LINKED_PAGE_DATA_SIZE   (PAGE_SIZE - sizeof(void *))
127
128 struct linked_page {
129         struct linked_page *next;
130         char data[LINKED_PAGE_DATA_SIZE];
131 } __attribute__((packed));
132
133 static inline void
134 free_list_of_pages(struct linked_page *list, int clear_page_nosave)
135 {
136         while (list) {
137                 struct linked_page *lp = list->next;
138
139                 free_image_page(list, clear_page_nosave);
140                 list = lp;
141         }
142 }
143
144 /**
145   *     struct chain_allocator is used for allocating small objects out of
146   *     a linked list of pages called 'the chain'.
147   *
148   *     The chain grows each time when there is no room for a new object in
149   *     the current page.  The allocated objects cannot be freed individually.
150   *     It is only possible to free them all at once, by freeing the entire
151   *     chain.
152   *
153   *     NOTE: The chain allocator may be inefficient if the allocated objects
154   *     are not much smaller than PAGE_SIZE.
155   */
156
157 struct chain_allocator {
158         struct linked_page *chain;      /* the chain */
159         unsigned int used_space;        /* total size of objects allocated out
160                                          * of the current page
161                                          */
162         gfp_t gfp_mask;         /* mask for allocating pages */
163         int safe_needed;        /* if set, only "safe" pages are allocated */
164 };
165
166 static void
167 chain_init(struct chain_allocator *ca, gfp_t gfp_mask, int safe_needed)
168 {
169         ca->chain = NULL;
170         ca->used_space = LINKED_PAGE_DATA_SIZE;
171         ca->gfp_mask = gfp_mask;
172         ca->safe_needed = safe_needed;
173 }
174
175 static void *chain_alloc(struct chain_allocator *ca, unsigned int size)
176 {
177         void *ret;
178
179         if (LINKED_PAGE_DATA_SIZE - ca->used_space < size) {
180                 struct linked_page *lp;
181
182                 lp = get_image_page(ca->gfp_mask, ca->safe_needed);
183                 if (!lp)
184                         return NULL;
185
186                 lp->next = ca->chain;
187                 ca->chain = lp;
188                 ca->used_space = 0;
189         }
190         ret = ca->chain->data + ca->used_space;
191         ca->used_space += size;
192         return ret;
193 }
194
195 static void chain_free(struct chain_allocator *ca, int clear_page_nosave)
196 {
197         free_list_of_pages(ca->chain, clear_page_nosave);
198         memset(ca, 0, sizeof(struct chain_allocator));
199 }
200
201 /**
202  *      Data types related to memory bitmaps.
203  *
204  *      Memory bitmap is a structure consiting of many linked lists of
205  *      objects.  The main list's elements are of type struct zone_bitmap
206  *      and each of them corresonds to one zone.  For each zone bitmap
207  *      object there is a list of objects of type struct bm_block that
208  *      represent each blocks of bit chunks in which information is
209  *      stored.
210  *
211  *      struct memory_bitmap contains a pointer to the main list of zone
212  *      bitmap objects, a struct bm_position used for browsing the bitmap,
213  *      and a pointer to the list of pages used for allocating all of the
214  *      zone bitmap objects and bitmap block objects.
215  *
216  *      NOTE: It has to be possible to lay out the bitmap in memory
217  *      using only allocations of order 0.  Additionally, the bitmap is
218  *      designed to work with arbitrary number of zones (this is over the
219  *      top for now, but let's avoid making unnecessary assumptions ;-).
220  *
221  *      struct zone_bitmap contains a pointer to a list of bitmap block
222  *      objects and a pointer to the bitmap block object that has been
223  *      most recently used for setting bits.  Additionally, it contains the
224  *      pfns that correspond to the start and end of the represented zone.
225  *
226  *      struct bm_block contains a pointer to the memory page in which
227  *      information is stored (in the form of a block of bit chunks
228  *      of type unsigned long each).  It also contains the pfns that
229  *      correspond to the start and end of the represented memory area and
230  *      the number of bit chunks in the block.
231  */
232
233 #define BM_END_OF_MAP   (~0UL)
234
235 #define BM_CHUNKS_PER_BLOCK     (PAGE_SIZE / sizeof(long))
236 #define BM_BITS_PER_CHUNK       (sizeof(long) << 3)
237 #define BM_BITS_PER_BLOCK       (PAGE_SIZE << 3)
238
239 struct bm_block {
240         struct bm_block *next;          /* next element of the list */
241         unsigned long start_pfn;        /* pfn represented by the first bit */
242         unsigned long end_pfn;  /* pfn represented by the last bit plus 1 */
243         unsigned int size;      /* number of bit chunks */
244         unsigned long *data;    /* chunks of bits representing pages */
245 };
246
247 struct zone_bitmap {
248         struct zone_bitmap *next;       /* next element of the list */
249         unsigned long start_pfn;        /* minimal pfn in this zone */
250         unsigned long end_pfn;          /* maximal pfn in this zone plus 1 */
251         struct bm_block *bm_blocks;     /* list of bitmap blocks */
252         struct bm_block *cur_block;     /* recently used bitmap block */
253 };
254
255 /* strcut bm_position is used for browsing memory bitmaps */
256
257 struct bm_position {
258         struct zone_bitmap *zone_bm;
259         struct bm_block *block;
260         int chunk;
261         int bit;
262 };
263
264 struct memory_bitmap {
265         struct zone_bitmap *zone_bm_list;       /* list of zone bitmaps */
266         struct linked_page *p_list;     /* list of pages used to store zone
267                                          * bitmap objects and bitmap block
268                                          * objects
269                                          */
270         struct bm_position cur; /* most recently used bit position */
271 };
272
273 /* Functions that operate on memory bitmaps */
274
275 static inline void memory_bm_reset_chunk(struct memory_bitmap *bm)
276 {
277         bm->cur.chunk = 0;
278         bm->cur.bit = -1;
279 }
280
281 static void memory_bm_position_reset(struct memory_bitmap *bm)
282 {
283         struct zone_bitmap *zone_bm;
284
285         zone_bm = bm->zone_bm_list;
286         bm->cur.zone_bm = zone_bm;
287         bm->cur.block = zone_bm->bm_blocks;
288         memory_bm_reset_chunk(bm);
289 }
290
291 static void memory_bm_free(struct memory_bitmap *bm, int clear_nosave_free);
292
293 /**
294  *      create_bm_block_list - create a list of block bitmap objects
295  */
296
297 static inline struct bm_block *
298 create_bm_block_list(unsigned int nr_blocks, struct chain_allocator *ca)
299 {
300         struct bm_block *bblist = NULL;
301
302         while (nr_blocks-- > 0) {
303                 struct bm_block *bb;
304
305                 bb = chain_alloc(ca, sizeof(struct bm_block));
306                 if (!bb)
307                         return NULL;
308
309                 bb->next = bblist;
310                 bblist = bb;
311         }
312         return bblist;
313 }
314
315 /**
316  *      create_zone_bm_list - create a list of zone bitmap objects
317  */
318
319 static inline struct zone_bitmap *
320 create_zone_bm_list(unsigned int nr_zones, struct chain_allocator *ca)
321 {
322         struct zone_bitmap *zbmlist = NULL;
323
324         while (nr_zones-- > 0) {
325                 struct zone_bitmap *zbm;
326
327                 zbm = chain_alloc(ca, sizeof(struct zone_bitmap));
328                 if (!zbm)
329                         return NULL;
330
331                 zbm->next = zbmlist;
332                 zbmlist = zbm;
333         }
334         return zbmlist;
335 }
336
337 /**
338   *     memory_bm_create - allocate memory for a memory bitmap
339   */
340
341 static int
342 memory_bm_create(struct memory_bitmap *bm, gfp_t gfp_mask, int safe_needed)
343 {
344         struct chain_allocator ca;
345         struct zone *zone;
346         struct zone_bitmap *zone_bm;
347         struct bm_block *bb;
348         unsigned int nr;
349
350         chain_init(&ca, gfp_mask, safe_needed);
351
352         /* Compute the number of zones */
353         nr = 0;
354         for_each_zone(zone)
355                 if (populated_zone(zone))
356                         nr++;
357
358         /* Allocate the list of zones bitmap objects */
359         zone_bm = create_zone_bm_list(nr, &ca);
360         bm->zone_bm_list = zone_bm;
361         if (!zone_bm) {
362                 chain_free(&ca, PG_UNSAFE_CLEAR);
363                 return -ENOMEM;
364         }
365
366         /* Initialize the zone bitmap objects */
367         for_each_zone(zone) {
368                 unsigned long pfn;
369
370                 if (!populated_zone(zone))
371                         continue;
372
373                 zone_bm->start_pfn = zone->zone_start_pfn;
374                 zone_bm->end_pfn = zone->zone_start_pfn + zone->spanned_pages;
375                 /* Allocate the list of bitmap block objects */
376                 nr = DIV_ROUND_UP(zone->spanned_pages, BM_BITS_PER_BLOCK);
377                 bb = create_bm_block_list(nr, &ca);
378                 zone_bm->bm_blocks = bb;
379                 zone_bm->cur_block = bb;
380                 if (!bb)
381                         goto Free;
382
383                 nr = zone->spanned_pages;
384                 pfn = zone->zone_start_pfn;
385                 /* Initialize the bitmap block objects */
386                 while (bb) {
387                         unsigned long *ptr;
388
389                         ptr = get_image_page(gfp_mask, safe_needed);
390                         bb->data = ptr;
391                         if (!ptr)
392                                 goto Free;
393
394                         bb->start_pfn = pfn;
395                         if (nr >= BM_BITS_PER_BLOCK) {
396                                 pfn += BM_BITS_PER_BLOCK;
397                                 bb->size = BM_CHUNKS_PER_BLOCK;
398                                 nr -= BM_BITS_PER_BLOCK;
399                         } else {
400                                 /* This is executed only once in the loop */
401                                 pfn += nr;
402                                 bb->size = DIV_ROUND_UP(nr, BM_BITS_PER_CHUNK);
403                         }
404                         bb->end_pfn = pfn;
405                         bb = bb->next;
406                 }
407                 zone_bm = zone_bm->next;
408         }
409         bm->p_list = ca.chain;
410         memory_bm_position_reset(bm);
411         return 0;
412
413  Free:
414         bm->p_list = ca.chain;
415         memory_bm_free(bm, PG_UNSAFE_CLEAR);
416         return -ENOMEM;
417 }
418
419 /**
420   *     memory_bm_free - free memory occupied by the memory bitmap @bm
421   */
422
423 static void memory_bm_free(struct memory_bitmap *bm, int clear_nosave_free)
424 {
425         struct zone_bitmap *zone_bm;
426
427         /* Free the list of bit blocks for each zone_bitmap object */
428         zone_bm = bm->zone_bm_list;
429         while (zone_bm) {
430                 struct bm_block *bb;
431
432                 bb = zone_bm->bm_blocks;
433                 while (bb) {
434                         if (bb->data)
435                                 free_image_page(bb->data, clear_nosave_free);
436                         bb = bb->next;
437                 }
438                 zone_bm = zone_bm->next;
439         }
440         free_list_of_pages(bm->p_list, clear_nosave_free);
441         bm->zone_bm_list = NULL;
442 }
443
444 /**
445  *      memory_bm_find_bit - find the bit in the bitmap @bm that corresponds
446  *      to given pfn.  The cur_zone_bm member of @bm and the cur_block member
447  *      of @bm->cur_zone_bm are updated.
448  */
449
450 static void memory_bm_find_bit(struct memory_bitmap *bm, unsigned long pfn,
451                                 void **addr, unsigned int *bit_nr)
452 {
453         struct zone_bitmap *zone_bm;
454         struct bm_block *bb;
455
456         /* Check if the pfn is from the current zone */
457         zone_bm = bm->cur.zone_bm;
458         if (pfn < zone_bm->start_pfn || pfn >= zone_bm->end_pfn) {
459                 zone_bm = bm->zone_bm_list;
460                 /* We don't assume that the zones are sorted by pfns */
461                 while (pfn < zone_bm->start_pfn || pfn >= zone_bm->end_pfn) {
462                         zone_bm = zone_bm->next;
463
464                         BUG_ON(!zone_bm);
465                 }
466                 bm->cur.zone_bm = zone_bm;
467         }
468         /* Check if the pfn corresponds to the current bitmap block */
469         bb = zone_bm->cur_block;
470         if (pfn < bb->start_pfn)
471                 bb = zone_bm->bm_blocks;
472
473         while (pfn >= bb->end_pfn) {
474                 bb = bb->next;
475
476                 BUG_ON(!bb);
477         }
478         zone_bm->cur_block = bb;
479         pfn -= bb->start_pfn;
480         *bit_nr = pfn % BM_BITS_PER_CHUNK;
481         *addr = bb->data + pfn / BM_BITS_PER_CHUNK;
482 }
483
484 static void memory_bm_set_bit(struct memory_bitmap *bm, unsigned long pfn)
485 {
486         void *addr;
487         unsigned int bit;
488
489         memory_bm_find_bit(bm, pfn, &addr, &bit);
490         set_bit(bit, addr);
491 }
492
493 static void memory_bm_clear_bit(struct memory_bitmap *bm, unsigned long pfn)
494 {
495         void *addr;
496         unsigned int bit;
497
498         memory_bm_find_bit(bm, pfn, &addr, &bit);
499         clear_bit(bit, addr);
500 }
501
502 static int memory_bm_test_bit(struct memory_bitmap *bm, unsigned long pfn)
503 {
504         void *addr;
505         unsigned int bit;
506
507         memory_bm_find_bit(bm, pfn, &addr, &bit);
508         return test_bit(bit, addr);
509 }
510
511 /* Two auxiliary functions for memory_bm_next_pfn */
512
513 /* Find the first set bit in the given chunk, if there is one */
514
515 static inline int next_bit_in_chunk(int bit, unsigned long *chunk_p)
516 {
517         bit++;
518         while (bit < BM_BITS_PER_CHUNK) {
519                 if (test_bit(bit, chunk_p))
520                         return bit;
521
522                 bit++;
523         }
524         return -1;
525 }
526
527 /* Find a chunk containing some bits set in given block of bits */
528
529 static inline int next_chunk_in_block(int n, struct bm_block *bb)
530 {
531         n++;
532         while (n < bb->size) {
533                 if (bb->data[n])
534                         return n;
535
536                 n++;
537         }
538         return -1;
539 }
540
541 /**
542  *      memory_bm_next_pfn - find the pfn that corresponds to the next set bit
543  *      in the bitmap @bm.  If the pfn cannot be found, BM_END_OF_MAP is
544  *      returned.
545  *
546  *      It is required to run memory_bm_position_reset() before the first call to
547  *      this function.
548  */
549
550 static unsigned long memory_bm_next_pfn(struct memory_bitmap *bm)
551 {
552         struct zone_bitmap *zone_bm;
553         struct bm_block *bb;
554         int chunk;
555         int bit;
556
557         do {
558                 bb = bm->cur.block;
559                 do {
560                         chunk = bm->cur.chunk;
561                         bit = bm->cur.bit;
562                         do {
563                                 bit = next_bit_in_chunk(bit, bb->data + chunk);
564                                 if (bit >= 0)
565                                         goto Return_pfn;
566
567                                 chunk = next_chunk_in_block(chunk, bb);
568                                 bit = -1;
569                         } while (chunk >= 0);
570                         bb = bb->next;
571                         bm->cur.block = bb;
572                         memory_bm_reset_chunk(bm);
573                 } while (bb);
574                 zone_bm = bm->cur.zone_bm->next;
575                 if (zone_bm) {
576                         bm->cur.zone_bm = zone_bm;
577                         bm->cur.block = zone_bm->bm_blocks;
578                         memory_bm_reset_chunk(bm);
579                 }
580         } while (zone_bm);
581         memory_bm_position_reset(bm);
582         return BM_END_OF_MAP;
583
584  Return_pfn:
585         bm->cur.chunk = chunk;
586         bm->cur.bit = bit;
587         return bb->start_pfn + chunk * BM_BITS_PER_CHUNK + bit;
588 }
589
590 /**
591  *      This structure represents a range of page frames the contents of which
592  *      should not be saved during the suspend.
593  */
594
595 struct nosave_region {
596         struct list_head list;
597         unsigned long start_pfn;
598         unsigned long end_pfn;
599 };
600
601 static LIST_HEAD(nosave_regions);
602
603 /**
604  *      register_nosave_region - register a range of page frames the contents
605  *      of which should not be saved during the suspend (to be used in the early
606  *      initialization code)
607  */
608
609 void __init
610 __register_nosave_region(unsigned long start_pfn, unsigned long end_pfn,
611                          int use_kmalloc)
612 {
613         struct nosave_region *region;
614
615         if (start_pfn >= end_pfn)
616                 return;
617
618         if (!list_empty(&nosave_regions)) {
619                 /* Try to extend the previous region (they should be sorted) */
620                 region = list_entry(nosave_regions.prev,
621                                         struct nosave_region, list);
622                 if (region->end_pfn == start_pfn) {
623                         region->end_pfn = end_pfn;
624                         goto Report;
625                 }
626         }
627         if (use_kmalloc) {
628                 /* during init, this shouldn't fail */
629                 region = kmalloc(sizeof(struct nosave_region), GFP_KERNEL);
630                 BUG_ON(!region);
631         } else
632                 /* This allocation cannot fail */
633                 region = alloc_bootmem_low(sizeof(struct nosave_region));
634         region->start_pfn = start_pfn;
635         region->end_pfn = end_pfn;
636         list_add_tail(&region->list, &nosave_regions);
637  Report:
638         printk("swsusp: Registered nosave memory region: %016lx - %016lx\n",
639                 start_pfn << PAGE_SHIFT, end_pfn << PAGE_SHIFT);
640 }
641
642 /*
643  * Set bits in this map correspond to the page frames the contents of which
644  * should not be saved during the suspend.
645  */
646 static struct memory_bitmap *forbidden_pages_map;
647
648 /* Set bits in this map correspond to free page frames. */
649 static struct memory_bitmap *free_pages_map;
650
651 /*
652  * Each page frame allocated for creating the image is marked by setting the
653  * corresponding bits in forbidden_pages_map and free_pages_map simultaneously
654  */
655
656 void swsusp_set_page_free(struct page *page)
657 {
658         if (free_pages_map)
659                 memory_bm_set_bit(free_pages_map, page_to_pfn(page));
660 }
661
662 static int swsusp_page_is_free(struct page *page)
663 {
664         return free_pages_map ?
665                 memory_bm_test_bit(free_pages_map, page_to_pfn(page)) : 0;
666 }
667
668 void swsusp_unset_page_free(struct page *page)
669 {
670         if (free_pages_map)
671                 memory_bm_clear_bit(free_pages_map, page_to_pfn(page));
672 }
673
674 static void swsusp_set_page_forbidden(struct page *page)
675 {
676         if (forbidden_pages_map)
677                 memory_bm_set_bit(forbidden_pages_map, page_to_pfn(page));
678 }
679
680 int swsusp_page_is_forbidden(struct page *page)
681 {
682         return forbidden_pages_map ?
683                 memory_bm_test_bit(forbidden_pages_map, page_to_pfn(page)) : 0;
684 }
685
686 static void swsusp_unset_page_forbidden(struct page *page)
687 {
688         if (forbidden_pages_map)
689                 memory_bm_clear_bit(forbidden_pages_map, page_to_pfn(page));
690 }
691
692 /**
693  *      mark_nosave_pages - set bits corresponding to the page frames the
694  *      contents of which should not be saved in a given bitmap.
695  */
696
697 static void mark_nosave_pages(struct memory_bitmap *bm)
698 {
699         struct nosave_region *region;
700
701         if (list_empty(&nosave_regions))
702                 return;
703
704         list_for_each_entry(region, &nosave_regions, list) {
705                 unsigned long pfn;
706
707                 printk("swsusp: Marking nosave pages: %016lx - %016lx\n",
708                                 region->start_pfn << PAGE_SHIFT,
709                                 region->end_pfn << PAGE_SHIFT);
710
711                 for (pfn = region->start_pfn; pfn < region->end_pfn; pfn++)
712                         if (pfn_valid(pfn))
713                                 memory_bm_set_bit(bm, pfn);
714         }
715 }
716
717 /**
718  *      create_basic_memory_bitmaps - create bitmaps needed for marking page
719  *      frames that should not be saved and free page frames.  The pointers
720  *      forbidden_pages_map and free_pages_map are only modified if everything
721  *      goes well, because we don't want the bits to be used before both bitmaps
722  *      are set up.
723  */
724
725 int create_basic_memory_bitmaps(void)
726 {
727         struct memory_bitmap *bm1, *bm2;
728         int error = 0;
729
730         BUG_ON(forbidden_pages_map || free_pages_map);
731
732         bm1 = kzalloc(sizeof(struct memory_bitmap), GFP_KERNEL);
733         if (!bm1)
734                 return -ENOMEM;
735
736         error = memory_bm_create(bm1, GFP_KERNEL, PG_ANY);
737         if (error)
738                 goto Free_first_object;
739
740         bm2 = kzalloc(sizeof(struct memory_bitmap), GFP_KERNEL);
741         if (!bm2)
742                 goto Free_first_bitmap;
743
744         error = memory_bm_create(bm2, GFP_KERNEL, PG_ANY);
745         if (error)
746                 goto Free_second_object;
747
748         forbidden_pages_map = bm1;
749         free_pages_map = bm2;
750         mark_nosave_pages(forbidden_pages_map);
751
752         printk("swsusp: Basic memory bitmaps created\n");
753
754         return 0;
755
756  Free_second_object:
757         kfree(bm2);
758  Free_first_bitmap:
759         memory_bm_free(bm1, PG_UNSAFE_CLEAR);
760  Free_first_object:
761         kfree(bm1);
762         return -ENOMEM;
763 }
764
765 /**
766  *      free_basic_memory_bitmaps - free memory bitmaps allocated by
767  *      create_basic_memory_bitmaps().  The auxiliary pointers are necessary
768  *      so that the bitmaps themselves are not referred to while they are being
769  *      freed.
770  */
771
772 void free_basic_memory_bitmaps(void)
773 {
774         struct memory_bitmap *bm1, *bm2;
775
776         BUG_ON(!(forbidden_pages_map && free_pages_map));
777
778         bm1 = forbidden_pages_map;
779         bm2 = free_pages_map;
780         forbidden_pages_map = NULL;
781         free_pages_map = NULL;
782         memory_bm_free(bm1, PG_UNSAFE_CLEAR);
783         kfree(bm1);
784         memory_bm_free(bm2, PG_UNSAFE_CLEAR);
785         kfree(bm2);
786
787         printk("swsusp: Basic memory bitmaps freed\n");
788 }
789
790 /**
791  *      snapshot_additional_pages - estimate the number of additional pages
792  *      be needed for setting up the suspend image data structures for given
793  *      zone (usually the returned value is greater than the exact number)
794  */
795
796 unsigned int snapshot_additional_pages(struct zone *zone)
797 {
798         unsigned int res;
799
800         res = DIV_ROUND_UP(zone->spanned_pages, BM_BITS_PER_BLOCK);
801         res += DIV_ROUND_UP(res * sizeof(struct bm_block), PAGE_SIZE);
802         return 2 * res;
803 }
804
805 #ifdef CONFIG_HIGHMEM
806 /**
807  *      count_free_highmem_pages - compute the total number of free highmem
808  *      pages, system-wide.
809  */
810
811 static unsigned int count_free_highmem_pages(void)
812 {
813         struct zone *zone;
814         unsigned int cnt = 0;
815
816         for_each_zone(zone)
817                 if (populated_zone(zone) && is_highmem(zone))
818                         cnt += zone_page_state(zone, NR_FREE_PAGES);
819
820         return cnt;
821 }
822
823 /**
824  *      saveable_highmem_page - Determine whether a highmem page should be
825  *      included in the suspend image.
826  *
827  *      We should save the page if it isn't Nosave or NosaveFree, or Reserved,
828  *      and it isn't a part of a free chunk of pages.
829  */
830
831 static struct page *saveable_highmem_page(unsigned long pfn)
832 {
833         struct page *page;
834
835         if (!pfn_valid(pfn))
836                 return NULL;
837
838         page = pfn_to_page(pfn);
839
840         BUG_ON(!PageHighMem(page));
841
842         if (swsusp_page_is_forbidden(page) ||  swsusp_page_is_free(page) ||
843             PageReserved(page))
844                 return NULL;
845
846         return page;
847 }
848
849 /**
850  *      count_highmem_pages - compute the total number of saveable highmem
851  *      pages.
852  */
853
854 unsigned int count_highmem_pages(void)
855 {
856         struct zone *zone;
857         unsigned int n = 0;
858
859         for_each_zone(zone) {
860                 unsigned long pfn, max_zone_pfn;
861
862                 if (!is_highmem(zone))
863                         continue;
864
865                 mark_free_pages(zone);
866                 max_zone_pfn = zone->zone_start_pfn + zone->spanned_pages;
867                 for (pfn = zone->zone_start_pfn; pfn < max_zone_pfn; pfn++)
868                         if (saveable_highmem_page(pfn))
869                                 n++;
870         }
871         return n;
872 }
873 #else
874 static inline void *saveable_highmem_page(unsigned long pfn) { return NULL; }
875 static inline unsigned int count_highmem_pages(void) { return 0; }
876 #endif /* CONFIG_HIGHMEM */
877
878 /**
879  *      saveable - Determine whether a non-highmem page should be included in
880  *      the suspend image.
881  *
882  *      We should save the page if it isn't Nosave, and is not in the range
883  *      of pages statically defined as 'unsaveable', and it isn't a part of
884  *      a free chunk of pages.
885  */
886
887 static struct page *saveable_page(unsigned long pfn)
888 {
889         struct page *page;
890
891         if (!pfn_valid(pfn))
892                 return NULL;
893
894         page = pfn_to_page(pfn);
895
896         BUG_ON(PageHighMem(page));
897
898         if (swsusp_page_is_forbidden(page) || swsusp_page_is_free(page))
899                 return NULL;
900
901         if (PageReserved(page) && pfn_is_nosave(pfn))
902                 return NULL;
903
904         return page;
905 }
906
907 /**
908  *      count_data_pages - compute the total number of saveable non-highmem
909  *      pages.
910  */
911
912 unsigned int count_data_pages(void)
913 {
914         struct zone *zone;
915         unsigned long pfn, max_zone_pfn;
916         unsigned int n = 0;
917
918         for_each_zone(zone) {
919                 if (is_highmem(zone))
920                         continue;
921
922                 mark_free_pages(zone);
923                 max_zone_pfn = zone->zone_start_pfn + zone->spanned_pages;
924                 for (pfn = zone->zone_start_pfn; pfn < max_zone_pfn; pfn++)
925                         if(saveable_page(pfn))
926                                 n++;
927         }
928         return n;
929 }
930
931 /* This is needed, because copy_page and memcpy are not usable for copying
932  * task structs.
933  */
934 static inline void do_copy_page(long *dst, long *src)
935 {
936         int n;
937
938         for (n = PAGE_SIZE / sizeof(long); n; n--)
939                 *dst++ = *src++;
940 }
941
942 #ifdef CONFIG_HIGHMEM
943 static inline struct page *
944 page_is_saveable(struct zone *zone, unsigned long pfn)
945 {
946         return is_highmem(zone) ?
947                         saveable_highmem_page(pfn) : saveable_page(pfn);
948 }
949
950 static inline void
951 copy_data_page(unsigned long dst_pfn, unsigned long src_pfn)
952 {
953         struct page *s_page, *d_page;
954         void *src, *dst;
955
956         s_page = pfn_to_page(src_pfn);
957         d_page = pfn_to_page(dst_pfn);
958         if (PageHighMem(s_page)) {
959                 src = kmap_atomic(s_page, KM_USER0);
960                 dst = kmap_atomic(d_page, KM_USER1);
961                 do_copy_page(dst, src);
962                 kunmap_atomic(src, KM_USER0);
963                 kunmap_atomic(dst, KM_USER1);
964         } else {
965                 src = page_address(s_page);
966                 if (PageHighMem(d_page)) {
967                         /* Page pointed to by src may contain some kernel
968                          * data modified by kmap_atomic()
969                          */
970                         do_copy_page(buffer, src);
971                         dst = kmap_atomic(pfn_to_page(dst_pfn), KM_USER0);
972                         memcpy(dst, buffer, PAGE_SIZE);
973                         kunmap_atomic(dst, KM_USER0);
974                 } else {
975                         dst = page_address(d_page);
976                         do_copy_page(dst, src);
977                 }
978         }
979 }
980 #else
981 #define page_is_saveable(zone, pfn)     saveable_page(pfn)
982
983 static inline void
984 copy_data_page(unsigned long dst_pfn, unsigned long src_pfn)
985 {
986         do_copy_page(page_address(pfn_to_page(dst_pfn)),
987                         page_address(pfn_to_page(src_pfn)));
988 }
989 #endif /* CONFIG_HIGHMEM */
990
991 static void
992 copy_data_pages(struct memory_bitmap *copy_bm, struct memory_bitmap *orig_bm)
993 {
994         struct zone *zone;
995         unsigned long pfn;
996
997         for_each_zone(zone) {
998                 unsigned long max_zone_pfn;
999
1000                 mark_free_pages(zone);
1001                 max_zone_pfn = zone->zone_start_pfn + zone->spanned_pages;
1002                 for (pfn = zone->zone_start_pfn; pfn < max_zone_pfn; pfn++)
1003                         if (page_is_saveable(zone, pfn))
1004                                 memory_bm_set_bit(orig_bm, pfn);
1005         }
1006         memory_bm_position_reset(orig_bm);
1007         memory_bm_position_reset(copy_bm);
1008         do {
1009                 pfn = memory_bm_next_pfn(orig_bm);
1010                 if (likely(pfn != BM_END_OF_MAP))
1011                         copy_data_page(memory_bm_next_pfn(copy_bm), pfn);
1012         } while (pfn != BM_END_OF_MAP);
1013 }
1014
1015 /* Total number of image pages */
1016 static unsigned int nr_copy_pages;
1017 /* Number of pages needed for saving the original pfns of the image pages */
1018 static unsigned int nr_meta_pages;
1019
1020 /**
1021  *      swsusp_free - free pages allocated for the suspend.
1022  *
1023  *      Suspend pages are alocated before the atomic copy is made, so we
1024  *      need to release them after the resume.
1025  */
1026
1027 void swsusp_free(void)
1028 {
1029         struct zone *zone;
1030         unsigned long pfn, max_zone_pfn;
1031
1032         for_each_zone(zone) {
1033                 max_zone_pfn = zone->zone_start_pfn + zone->spanned_pages;
1034                 for (pfn = zone->zone_start_pfn; pfn < max_zone_pfn; pfn++)
1035                         if (pfn_valid(pfn)) {
1036                                 struct page *page = pfn_to_page(pfn);
1037
1038                                 if (swsusp_page_is_forbidden(page) &&
1039                                     swsusp_page_is_free(page)) {
1040                                         swsusp_unset_page_forbidden(page);
1041                                         swsusp_unset_page_free(page);
1042                                         __free_page(page);
1043                                 }
1044                         }
1045         }
1046         nr_copy_pages = 0;
1047         nr_meta_pages = 0;
1048         restore_pblist = NULL;
1049         buffer = NULL;
1050 }
1051
1052 #ifdef CONFIG_HIGHMEM
1053 /**
1054   *     count_pages_for_highmem - compute the number of non-highmem pages
1055   *     that will be necessary for creating copies of highmem pages.
1056   */
1057
1058 static unsigned int count_pages_for_highmem(unsigned int nr_highmem)
1059 {
1060         unsigned int free_highmem = count_free_highmem_pages();
1061
1062         if (free_highmem >= nr_highmem)
1063                 nr_highmem = 0;
1064         else
1065                 nr_highmem -= free_highmem;
1066
1067         return nr_highmem;
1068 }
1069 #else
1070 static unsigned int
1071 count_pages_for_highmem(unsigned int nr_highmem) { return 0; }
1072 #endif /* CONFIG_HIGHMEM */
1073
1074 /**
1075  *      enough_free_mem - Make sure we have enough free memory for the
1076  *      snapshot image.
1077  */
1078
1079 static int enough_free_mem(unsigned int nr_pages, unsigned int nr_highmem)
1080 {
1081         struct zone *zone;
1082         unsigned int free = 0, meta = 0;
1083
1084         for_each_zone(zone) {
1085                 meta += snapshot_additional_pages(zone);
1086                 if (!is_highmem(zone))
1087                         free += zone_page_state(zone, NR_FREE_PAGES);
1088         }
1089
1090         nr_pages += count_pages_for_highmem(nr_highmem);
1091         pr_debug("swsusp: Normal pages needed: %u + %u + %u, available pages: %u\n",
1092                 nr_pages, PAGES_FOR_IO, meta, free);
1093
1094         return free > nr_pages + PAGES_FOR_IO + meta;
1095 }
1096
1097 #ifdef CONFIG_HIGHMEM
1098 /**
1099  *      get_highmem_buffer - if there are some highmem pages in the suspend
1100  *      image, we may need the buffer to copy them and/or load their data.
1101  */
1102
1103 static inline int get_highmem_buffer(int safe_needed)
1104 {
1105         buffer = get_image_page(GFP_ATOMIC | __GFP_COLD, safe_needed);
1106         return buffer ? 0 : -ENOMEM;
1107 }
1108
1109 /**
1110  *      alloc_highmem_image_pages - allocate some highmem pages for the image.
1111  *      Try to allocate as many pages as needed, but if the number of free
1112  *      highmem pages is lesser than that, allocate them all.
1113  */
1114
1115 static inline unsigned int
1116 alloc_highmem_image_pages(struct memory_bitmap *bm, unsigned int nr_highmem)
1117 {
1118         unsigned int to_alloc = count_free_highmem_pages();
1119
1120         if (to_alloc > nr_highmem)
1121                 to_alloc = nr_highmem;
1122
1123         nr_highmem -= to_alloc;
1124         while (to_alloc-- > 0) {
1125                 struct page *page;
1126
1127                 page = alloc_image_page(__GFP_HIGHMEM);
1128                 memory_bm_set_bit(bm, page_to_pfn(page));
1129         }
1130         return nr_highmem;
1131 }
1132 #else
1133 static inline int get_highmem_buffer(int safe_needed) { return 0; }
1134
1135 static inline unsigned int
1136 alloc_highmem_image_pages(struct memory_bitmap *bm, unsigned int n) { return 0; }
1137 #endif /* CONFIG_HIGHMEM */
1138
1139 /**
1140  *      swsusp_alloc - allocate memory for the suspend image
1141  *
1142  *      We first try to allocate as many highmem pages as there are
1143  *      saveable highmem pages in the system.  If that fails, we allocate
1144  *      non-highmem pages for the copies of the remaining highmem ones.
1145  *
1146  *      In this approach it is likely that the copies of highmem pages will
1147  *      also be located in the high memory, because of the way in which
1148  *      copy_data_pages() works.
1149  */
1150
1151 static int
1152 swsusp_alloc(struct memory_bitmap *orig_bm, struct memory_bitmap *copy_bm,
1153                 unsigned int nr_pages, unsigned int nr_highmem)
1154 {
1155         int error;
1156
1157         error = memory_bm_create(orig_bm, GFP_ATOMIC | __GFP_COLD, PG_ANY);
1158         if (error)
1159                 goto Free;
1160
1161         error = memory_bm_create(copy_bm, GFP_ATOMIC | __GFP_COLD, PG_ANY);
1162         if (error)
1163                 goto Free;
1164
1165         if (nr_highmem > 0) {
1166                 error = get_highmem_buffer(PG_ANY);
1167                 if (error)
1168                         goto Free;
1169
1170                 nr_pages += alloc_highmem_image_pages(copy_bm, nr_highmem);
1171         }
1172         while (nr_pages-- > 0) {
1173                 struct page *page = alloc_image_page(GFP_ATOMIC | __GFP_COLD);
1174
1175                 if (!page)
1176                         goto Free;
1177
1178                 memory_bm_set_bit(copy_bm, page_to_pfn(page));
1179         }
1180         return 0;
1181
1182  Free:
1183         swsusp_free();
1184         return -ENOMEM;
1185 }
1186
1187 /* Memory bitmap used for marking saveable pages (during suspend) or the
1188  * suspend image pages (during resume)
1189  */
1190 static struct memory_bitmap orig_bm;
1191 /* Memory bitmap used on suspend for marking allocated pages that will contain
1192  * the copies of saveable pages.  During resume it is initially used for
1193  * marking the suspend image pages, but then its set bits are duplicated in
1194  * @orig_bm and it is released.  Next, on systems with high memory, it may be
1195  * used for marking "safe" highmem pages, but it has to be reinitialized for
1196  * this purpose.
1197  */
1198 static struct memory_bitmap copy_bm;
1199
1200 asmlinkage int swsusp_save(void)
1201 {
1202         unsigned int nr_pages, nr_highmem;
1203
1204         printk("swsusp: critical section: \n");
1205
1206         drain_local_pages();
1207         nr_pages = count_data_pages();
1208         nr_highmem = count_highmem_pages();
1209         printk("swsusp: Need to copy %u pages\n", nr_pages + nr_highmem);
1210
1211         if (!enough_free_mem(nr_pages, nr_highmem)) {
1212                 printk(KERN_ERR "swsusp: Not enough free memory\n");
1213                 return -ENOMEM;
1214         }
1215
1216         if (swsusp_alloc(&orig_bm, &copy_bm, nr_pages, nr_highmem)) {
1217                 printk(KERN_ERR "swsusp: Memory allocation failed\n");
1218                 return -ENOMEM;
1219         }
1220
1221         /* During allocating of suspend pagedir, new cold pages may appear.
1222          * Kill them.
1223          */
1224         drain_local_pages();
1225         copy_data_pages(&copy_bm, &orig_bm);
1226
1227         /*
1228          * End of critical section. From now on, we can write to memory,
1229          * but we should not touch disk. This specially means we must _not_
1230          * touch swap space! Except we must write out our image of course.
1231          */
1232
1233         nr_pages += nr_highmem;
1234         nr_copy_pages = nr_pages;
1235         nr_meta_pages = DIV_ROUND_UP(nr_pages * sizeof(long), PAGE_SIZE);
1236
1237         printk("swsusp: critical section: done (%d pages copied)\n", nr_pages);
1238
1239         return 0;
1240 }
1241
1242 static void init_header(struct swsusp_info *info)
1243 {
1244         memset(info, 0, sizeof(struct swsusp_info));
1245         info->version_code = LINUX_VERSION_CODE;
1246         info->num_physpages = num_physpages;
1247         memcpy(&info->uts, init_utsname(), sizeof(struct new_utsname));
1248         info->cpus = num_online_cpus();
1249         info->image_pages = nr_copy_pages;
1250         info->pages = nr_copy_pages + nr_meta_pages + 1;
1251         info->size = info->pages;
1252         info->size <<= PAGE_SHIFT;
1253 }
1254
1255 /**
1256  *      pack_pfns - pfns corresponding to the set bits found in the bitmap @bm
1257  *      are stored in the array @buf[] (1 page at a time)
1258  */
1259
1260 static inline void
1261 pack_pfns(unsigned long *buf, struct memory_bitmap *bm)
1262 {
1263         int j;
1264
1265         for (j = 0; j < PAGE_SIZE / sizeof(long); j++) {
1266                 buf[j] = memory_bm_next_pfn(bm);
1267                 if (unlikely(buf[j] == BM_END_OF_MAP))
1268                         break;
1269         }
1270 }
1271
1272 /**
1273  *      snapshot_read_next - used for reading the system memory snapshot.
1274  *
1275  *      On the first call to it @handle should point to a zeroed
1276  *      snapshot_handle structure.  The structure gets updated and a pointer
1277  *      to it should be passed to this function every next time.
1278  *
1279  *      The @count parameter should contain the number of bytes the caller
1280  *      wants to read from the snapshot.  It must not be zero.
1281  *
1282  *      On success the function returns a positive number.  Then, the caller
1283  *      is allowed to read up to the returned number of bytes from the memory
1284  *      location computed by the data_of() macro.  The number returned
1285  *      may be smaller than @count, but this only happens if the read would
1286  *      cross a page boundary otherwise.
1287  *
1288  *      The function returns 0 to indicate the end of data stream condition,
1289  *      and a negative number is returned on error.  In such cases the
1290  *      structure pointed to by @handle is not updated and should not be used
1291  *      any more.
1292  */
1293
1294 int snapshot_read_next(struct snapshot_handle *handle, size_t count)
1295 {
1296         if (handle->cur > nr_meta_pages + nr_copy_pages)
1297                 return 0;
1298
1299         if (!buffer) {
1300                 /* This makes the buffer be freed by swsusp_free() */
1301                 buffer = get_image_page(GFP_ATOMIC, PG_ANY);
1302                 if (!buffer)
1303                         return -ENOMEM;
1304         }
1305         if (!handle->offset) {
1306                 init_header((struct swsusp_info *)buffer);
1307                 handle->buffer = buffer;
1308                 memory_bm_position_reset(&orig_bm);
1309                 memory_bm_position_reset(&copy_bm);
1310         }
1311         if (handle->prev < handle->cur) {
1312                 if (handle->cur <= nr_meta_pages) {
1313                         memset(buffer, 0, PAGE_SIZE);
1314                         pack_pfns(buffer, &orig_bm);
1315                 } else {
1316                         struct page *page;
1317
1318                         page = pfn_to_page(memory_bm_next_pfn(&copy_bm));
1319                         if (PageHighMem(page)) {
1320                                 /* Highmem pages are copied to the buffer,
1321                                  * because we can't return with a kmapped
1322                                  * highmem page (we may not be called again).
1323                                  */
1324                                 void *kaddr;
1325
1326                                 kaddr = kmap_atomic(page, KM_USER0);
1327                                 memcpy(buffer, kaddr, PAGE_SIZE);
1328                                 kunmap_atomic(kaddr, KM_USER0);
1329                                 handle->buffer = buffer;
1330                         } else {
1331                                 handle->buffer = page_address(page);
1332                         }
1333                 }
1334                 handle->prev = handle->cur;
1335         }
1336         handle->buf_offset = handle->cur_offset;
1337         if (handle->cur_offset + count >= PAGE_SIZE) {
1338                 count = PAGE_SIZE - handle->cur_offset;
1339                 handle->cur_offset = 0;
1340                 handle->cur++;
1341         } else {
1342                 handle->cur_offset += count;
1343         }
1344         handle->offset += count;
1345         return count;
1346 }
1347
1348 /**
1349  *      mark_unsafe_pages - mark the pages that cannot be used for storing
1350  *      the image during resume, because they conflict with the pages that
1351  *      had been used before suspend
1352  */
1353
1354 static int mark_unsafe_pages(struct memory_bitmap *bm)
1355 {
1356         struct zone *zone;
1357         unsigned long pfn, max_zone_pfn;
1358
1359         /* Clear page flags */
1360         for_each_zone(zone) {
1361                 max_zone_pfn = zone->zone_start_pfn + zone->spanned_pages;
1362                 for (pfn = zone->zone_start_pfn; pfn < max_zone_pfn; pfn++)
1363                         if (pfn_valid(pfn))
1364                                 swsusp_unset_page_free(pfn_to_page(pfn));
1365         }
1366
1367         /* Mark pages that correspond to the "original" pfns as "unsafe" */
1368         memory_bm_position_reset(bm);
1369         do {
1370                 pfn = memory_bm_next_pfn(bm);
1371                 if (likely(pfn != BM_END_OF_MAP)) {
1372                         if (likely(pfn_valid(pfn)))
1373                                 swsusp_set_page_free(pfn_to_page(pfn));
1374                         else
1375                                 return -EFAULT;
1376                 }
1377         } while (pfn != BM_END_OF_MAP);
1378
1379         allocated_unsafe_pages = 0;
1380
1381         return 0;
1382 }
1383
1384 static void
1385 duplicate_memory_bitmap(struct memory_bitmap *dst, struct memory_bitmap *src)
1386 {
1387         unsigned long pfn;
1388
1389         memory_bm_position_reset(src);
1390         pfn = memory_bm_next_pfn(src);
1391         while (pfn != BM_END_OF_MAP) {
1392                 memory_bm_set_bit(dst, pfn);
1393                 pfn = memory_bm_next_pfn(src);
1394         }
1395 }
1396
1397 static inline int check_header(struct swsusp_info *info)
1398 {
1399         char *reason = NULL;
1400
1401         if (info->version_code != LINUX_VERSION_CODE)
1402                 reason = "kernel version";
1403         if (info->num_physpages != num_physpages)
1404                 reason = "memory size";
1405         if (strcmp(info->uts.sysname,init_utsname()->sysname))
1406                 reason = "system type";
1407         if (strcmp(info->uts.release,init_utsname()->release))
1408                 reason = "kernel release";
1409         if (strcmp(info->uts.version,init_utsname()->version))
1410                 reason = "version";
1411         if (strcmp(info->uts.machine,init_utsname()->machine))
1412                 reason = "machine";
1413         if (reason) {
1414                 printk(KERN_ERR "swsusp: Resume mismatch: %s\n", reason);
1415                 return -EPERM;
1416         }
1417         return 0;
1418 }
1419
1420 /**
1421  *      load header - check the image header and copy data from it
1422  */
1423
1424 static int
1425 load_header(struct swsusp_info *info)
1426 {
1427         int error;
1428
1429         restore_pblist = NULL;
1430         error = check_header(info);
1431         if (!error) {
1432                 nr_copy_pages = info->image_pages;
1433                 nr_meta_pages = info->pages - info->image_pages - 1;
1434         }
1435         return error;
1436 }
1437
1438 /**
1439  *      unpack_orig_pfns - for each element of @buf[] (1 page at a time) set
1440  *      the corresponding bit in the memory bitmap @bm
1441  */
1442
1443 static inline void
1444 unpack_orig_pfns(unsigned long *buf, struct memory_bitmap *bm)
1445 {
1446         int j;
1447
1448         for (j = 0; j < PAGE_SIZE / sizeof(long); j++) {
1449                 if (unlikely(buf[j] == BM_END_OF_MAP))
1450                         break;
1451
1452                 memory_bm_set_bit(bm, buf[j]);
1453         }
1454 }
1455
1456 /* List of "safe" pages that may be used to store data loaded from the suspend
1457  * image
1458  */
1459 static struct linked_page *safe_pages_list;
1460
1461 #ifdef CONFIG_HIGHMEM
1462 /* struct highmem_pbe is used for creating the list of highmem pages that
1463  * should be restored atomically during the resume from disk, because the page
1464  * frames they have occupied before the suspend are in use.
1465  */
1466 struct highmem_pbe {
1467         struct page *copy_page; /* data is here now */
1468         struct page *orig_page; /* data was here before the suspend */
1469         struct highmem_pbe *next;
1470 };
1471
1472 /* List of highmem PBEs needed for restoring the highmem pages that were
1473  * allocated before the suspend and included in the suspend image, but have
1474  * also been allocated by the "resume" kernel, so their contents cannot be
1475  * written directly to their "original" page frames.
1476  */
1477 static struct highmem_pbe *highmem_pblist;
1478
1479 /**
1480  *      count_highmem_image_pages - compute the number of highmem pages in the
1481  *      suspend image.  The bits in the memory bitmap @bm that correspond to the
1482  *      image pages are assumed to be set.
1483  */
1484
1485 static unsigned int count_highmem_image_pages(struct memory_bitmap *bm)
1486 {
1487         unsigned long pfn;
1488         unsigned int cnt = 0;
1489
1490         memory_bm_position_reset(bm);
1491         pfn = memory_bm_next_pfn(bm);
1492         while (pfn != BM_END_OF_MAP) {
1493                 if (PageHighMem(pfn_to_page(pfn)))
1494                         cnt++;
1495
1496                 pfn = memory_bm_next_pfn(bm);
1497         }
1498         return cnt;
1499 }
1500
1501 /**
1502  *      prepare_highmem_image - try to allocate as many highmem pages as
1503  *      there are highmem image pages (@nr_highmem_p points to the variable
1504  *      containing the number of highmem image pages).  The pages that are
1505  *      "safe" (ie. will not be overwritten when the suspend image is
1506  *      restored) have the corresponding bits set in @bm (it must be
1507  *      unitialized).
1508  *
1509  *      NOTE: This function should not be called if there are no highmem
1510  *      image pages.
1511  */
1512
1513 static unsigned int safe_highmem_pages;
1514
1515 static struct memory_bitmap *safe_highmem_bm;
1516
1517 static int
1518 prepare_highmem_image(struct memory_bitmap *bm, unsigned int *nr_highmem_p)
1519 {
1520         unsigned int to_alloc;
1521
1522         if (memory_bm_create(bm, GFP_ATOMIC, PG_SAFE))
1523                 return -ENOMEM;
1524
1525         if (get_highmem_buffer(PG_SAFE))
1526                 return -ENOMEM;
1527
1528         to_alloc = count_free_highmem_pages();
1529         if (to_alloc > *nr_highmem_p)
1530                 to_alloc = *nr_highmem_p;
1531         else
1532                 *nr_highmem_p = to_alloc;
1533
1534         safe_highmem_pages = 0;
1535         while (to_alloc-- > 0) {
1536                 struct page *page;
1537
1538                 page = alloc_page(__GFP_HIGHMEM);
1539                 if (!swsusp_page_is_free(page)) {
1540                         /* The page is "safe", set its bit the bitmap */
1541                         memory_bm_set_bit(bm, page_to_pfn(page));
1542                         safe_highmem_pages++;
1543                 }
1544                 /* Mark the page as allocated */
1545                 swsusp_set_page_forbidden(page);
1546                 swsusp_set_page_free(page);
1547         }
1548         memory_bm_position_reset(bm);
1549         safe_highmem_bm = bm;
1550         return 0;
1551 }
1552
1553 /**
1554  *      get_highmem_page_buffer - for given highmem image page find the buffer
1555  *      that suspend_write_next() should set for its caller to write to.
1556  *
1557  *      If the page is to be saved to its "original" page frame or a copy of
1558  *      the page is to be made in the highmem, @buffer is returned.  Otherwise,
1559  *      the copy of the page is to be made in normal memory, so the address of
1560  *      the copy is returned.
1561  *
1562  *      If @buffer is returned, the caller of suspend_write_next() will write
1563  *      the page's contents to @buffer, so they will have to be copied to the
1564  *      right location on the next call to suspend_write_next() and it is done
1565  *      with the help of copy_last_highmem_page().  For this purpose, if
1566  *      @buffer is returned, @last_highmem page is set to the page to which
1567  *      the data will have to be copied from @buffer.
1568  */
1569
1570 static struct page *last_highmem_page;
1571
1572 static void *
1573 get_highmem_page_buffer(struct page *page, struct chain_allocator *ca)
1574 {
1575         struct highmem_pbe *pbe;
1576         void *kaddr;
1577
1578         if (swsusp_page_is_forbidden(page) && swsusp_page_is_free(page)) {
1579                 /* We have allocated the "original" page frame and we can
1580                  * use it directly to store the loaded page.
1581                  */
1582                 last_highmem_page = page;
1583                 return buffer;
1584         }
1585         /* The "original" page frame has not been allocated and we have to
1586          * use a "safe" page frame to store the loaded page.
1587          */
1588         pbe = chain_alloc(ca, sizeof(struct highmem_pbe));
1589         if (!pbe) {
1590                 swsusp_free();
1591                 return NULL;
1592         }
1593         pbe->orig_page = page;
1594         if (safe_highmem_pages > 0) {
1595                 struct page *tmp;
1596
1597                 /* Copy of the page will be stored in high memory */
1598                 kaddr = buffer;
1599                 tmp = pfn_to_page(memory_bm_next_pfn(safe_highmem_bm));
1600                 safe_highmem_pages--;
1601                 last_highmem_page = tmp;
1602                 pbe->copy_page = tmp;
1603         } else {
1604                 /* Copy of the page will be stored in normal memory */
1605                 kaddr = safe_pages_list;
1606                 safe_pages_list = safe_pages_list->next;
1607                 pbe->copy_page = virt_to_page(kaddr);
1608         }
1609         pbe->next = highmem_pblist;
1610         highmem_pblist = pbe;
1611         return kaddr;
1612 }
1613
1614 /**
1615  *      copy_last_highmem_page - copy the contents of a highmem image from
1616  *      @buffer, where the caller of snapshot_write_next() has place them,
1617  *      to the right location represented by @last_highmem_page .
1618  */
1619
1620 static void copy_last_highmem_page(void)
1621 {
1622         if (last_highmem_page) {
1623                 void *dst;
1624
1625                 dst = kmap_atomic(last_highmem_page, KM_USER0);
1626                 memcpy(dst, buffer, PAGE_SIZE);
1627                 kunmap_atomic(dst, KM_USER0);
1628                 last_highmem_page = NULL;
1629         }
1630 }
1631
1632 static inline int last_highmem_page_copied(void)
1633 {
1634         return !last_highmem_page;
1635 }
1636
1637 static inline void free_highmem_data(void)
1638 {
1639         if (safe_highmem_bm)
1640                 memory_bm_free(safe_highmem_bm, PG_UNSAFE_CLEAR);
1641
1642         if (buffer)
1643                 free_image_page(buffer, PG_UNSAFE_CLEAR);
1644 }
1645 #else
1646 static inline int get_safe_write_buffer(void) { return 0; }
1647
1648 static unsigned int
1649 count_highmem_image_pages(struct memory_bitmap *bm) { return 0; }
1650
1651 static inline int
1652 prepare_highmem_image(struct memory_bitmap *bm, unsigned int *nr_highmem_p)
1653 {
1654         return 0;
1655 }
1656
1657 static inline void *
1658 get_highmem_page_buffer(struct page *page, struct chain_allocator *ca)
1659 {
1660         return NULL;
1661 }
1662
1663 static inline void copy_last_highmem_page(void) {}
1664 static inline int last_highmem_page_copied(void) { return 1; }
1665 static inline void free_highmem_data(void) {}
1666 #endif /* CONFIG_HIGHMEM */
1667
1668 /**
1669  *      prepare_image - use the memory bitmap @bm to mark the pages that will
1670  *      be overwritten in the process of restoring the system memory state
1671  *      from the suspend image ("unsafe" pages) and allocate memory for the
1672  *      image.
1673  *
1674  *      The idea is to allocate a new memory bitmap first and then allocate
1675  *      as many pages as needed for the image data, but not to assign these
1676  *      pages to specific tasks initially.  Instead, we just mark them as
1677  *      allocated and create a lists of "safe" pages that will be used
1678  *      later.  On systems with high memory a list of "safe" highmem pages is
1679  *      also created.
1680  */
1681
1682 #define PBES_PER_LINKED_PAGE    (LINKED_PAGE_DATA_SIZE / sizeof(struct pbe))
1683
1684 static int
1685 prepare_image(struct memory_bitmap *new_bm, struct memory_bitmap *bm)
1686 {
1687         unsigned int nr_pages, nr_highmem;
1688         struct linked_page *sp_list, *lp;
1689         int error;
1690
1691         /* If there is no highmem, the buffer will not be necessary */
1692         free_image_page(buffer, PG_UNSAFE_CLEAR);
1693         buffer = NULL;
1694
1695         nr_highmem = count_highmem_image_pages(bm);
1696         error = mark_unsafe_pages(bm);
1697         if (error)
1698                 goto Free;
1699
1700         error = memory_bm_create(new_bm, GFP_ATOMIC, PG_SAFE);
1701         if (error)
1702                 goto Free;
1703
1704         duplicate_memory_bitmap(new_bm, bm);
1705         memory_bm_free(bm, PG_UNSAFE_KEEP);
1706         if (nr_highmem > 0) {
1707                 error = prepare_highmem_image(bm, &nr_highmem);
1708                 if (error)
1709                         goto Free;
1710         }
1711         /* Reserve some safe pages for potential later use.
1712          *
1713          * NOTE: This way we make sure there will be enough safe pages for the
1714          * chain_alloc() in get_buffer().  It is a bit wasteful, but
1715          * nr_copy_pages cannot be greater than 50% of the memory anyway.
1716          */
1717         sp_list = NULL;
1718         /* nr_copy_pages cannot be lesser than allocated_unsafe_pages */
1719         nr_pages = nr_copy_pages - nr_highmem - allocated_unsafe_pages;
1720         nr_pages = DIV_ROUND_UP(nr_pages, PBES_PER_LINKED_PAGE);
1721         while (nr_pages > 0) {
1722                 lp = get_image_page(GFP_ATOMIC, PG_SAFE);
1723                 if (!lp) {
1724                         error = -ENOMEM;
1725                         goto Free;
1726                 }
1727                 lp->next = sp_list;
1728                 sp_list = lp;
1729                 nr_pages--;
1730         }
1731         /* Preallocate memory for the image */
1732         safe_pages_list = NULL;
1733         nr_pages = nr_copy_pages - nr_highmem - allocated_unsafe_pages;
1734         while (nr_pages > 0) {
1735                 lp = (struct linked_page *)get_zeroed_page(GFP_ATOMIC);
1736                 if (!lp) {
1737                         error = -ENOMEM;
1738                         goto Free;
1739                 }
1740                 if (!swsusp_page_is_free(virt_to_page(lp))) {
1741                         /* The page is "safe", add it to the list */
1742                         lp->next = safe_pages_list;
1743                         safe_pages_list = lp;
1744                 }
1745                 /* Mark the page as allocated */
1746                 swsusp_set_page_forbidden(virt_to_page(lp));
1747                 swsusp_set_page_free(virt_to_page(lp));
1748                 nr_pages--;
1749         }
1750         /* Free the reserved safe pages so that chain_alloc() can use them */
1751         while (sp_list) {
1752                 lp = sp_list->next;
1753                 free_image_page(sp_list, PG_UNSAFE_CLEAR);
1754                 sp_list = lp;
1755         }
1756         return 0;
1757
1758  Free:
1759         swsusp_free();
1760         return error;
1761 }
1762
1763 /**
1764  *      get_buffer - compute the address that snapshot_write_next() should
1765  *      set for its caller to write to.
1766  */
1767
1768 static void *get_buffer(struct memory_bitmap *bm, struct chain_allocator *ca)
1769 {
1770         struct pbe *pbe;
1771         struct page *page = pfn_to_page(memory_bm_next_pfn(bm));
1772
1773         if (PageHighMem(page))
1774                 return get_highmem_page_buffer(page, ca);
1775
1776         if (swsusp_page_is_forbidden(page) && swsusp_page_is_free(page))
1777                 /* We have allocated the "original" page frame and we can
1778                  * use it directly to store the loaded page.
1779                  */
1780                 return page_address(page);
1781
1782         /* The "original" page frame has not been allocated and we have to
1783          * use a "safe" page frame to store the loaded page.
1784          */
1785         pbe = chain_alloc(ca, sizeof(struct pbe));
1786         if (!pbe) {
1787                 swsusp_free();
1788                 return NULL;
1789         }
1790         pbe->orig_address = page_address(page);
1791         pbe->address = safe_pages_list;
1792         safe_pages_list = safe_pages_list->next;
1793         pbe->next = restore_pblist;
1794         restore_pblist = pbe;
1795         return pbe->address;
1796 }
1797
1798 /**
1799  *      snapshot_write_next - used for writing the system memory snapshot.
1800  *
1801  *      On the first call to it @handle should point to a zeroed
1802  *      snapshot_handle structure.  The structure gets updated and a pointer
1803  *      to it should be passed to this function every next time.
1804  *
1805  *      The @count parameter should contain the number of bytes the caller
1806  *      wants to write to the image.  It must not be zero.
1807  *
1808  *      On success the function returns a positive number.  Then, the caller
1809  *      is allowed to write up to the returned number of bytes to the memory
1810  *      location computed by the data_of() macro.  The number returned
1811  *      may be smaller than @count, but this only happens if the write would
1812  *      cross a page boundary otherwise.
1813  *
1814  *      The function returns 0 to indicate the "end of file" condition,
1815  *      and a negative number is returned on error.  In such cases the
1816  *      structure pointed to by @handle is not updated and should not be used
1817  *      any more.
1818  */
1819
1820 int snapshot_write_next(struct snapshot_handle *handle, size_t count)
1821 {
1822         static struct chain_allocator ca;
1823         int error = 0;
1824
1825         /* Check if we have already loaded the entire image */
1826         if (handle->prev && handle->cur > nr_meta_pages + nr_copy_pages)
1827                 return 0;
1828
1829         if (handle->offset == 0) {
1830                 if (!buffer)
1831                         /* This makes the buffer be freed by swsusp_free() */
1832                         buffer = get_image_page(GFP_ATOMIC, PG_ANY);
1833
1834                 if (!buffer)
1835                         return -ENOMEM;
1836
1837                 handle->buffer = buffer;
1838         }
1839         handle->sync_read = 1;
1840         if (handle->prev < handle->cur) {
1841                 if (handle->prev == 0) {
1842                         error = load_header(buffer);
1843                         if (error)
1844                                 return error;
1845
1846                         error = memory_bm_create(&copy_bm, GFP_ATOMIC, PG_ANY);
1847                         if (error)
1848                                 return error;
1849
1850                 } else if (handle->prev <= nr_meta_pages) {
1851                         unpack_orig_pfns(buffer, &copy_bm);
1852                         if (handle->prev == nr_meta_pages) {
1853                                 error = prepare_image(&orig_bm, &copy_bm);
1854                                 if (error)
1855                                         return error;
1856
1857                                 chain_init(&ca, GFP_ATOMIC, PG_SAFE);
1858                                 memory_bm_position_reset(&orig_bm);
1859                                 restore_pblist = NULL;
1860                                 handle->buffer = get_buffer(&orig_bm, &ca);
1861                                 handle->sync_read = 0;
1862                                 if (!handle->buffer)
1863                                         return -ENOMEM;
1864                         }
1865                 } else {
1866                         copy_last_highmem_page();
1867                         handle->buffer = get_buffer(&orig_bm, &ca);
1868                         if (handle->buffer != buffer)
1869                                 handle->sync_read = 0;
1870                 }
1871                 handle->prev = handle->cur;
1872         }
1873         handle->buf_offset = handle->cur_offset;
1874         if (handle->cur_offset + count >= PAGE_SIZE) {
1875                 count = PAGE_SIZE - handle->cur_offset;
1876                 handle->cur_offset = 0;
1877                 handle->cur++;
1878         } else {
1879                 handle->cur_offset += count;
1880         }
1881         handle->offset += count;
1882         return count;
1883 }
1884
1885 /**
1886  *      snapshot_write_finalize - must be called after the last call to
1887  *      snapshot_write_next() in case the last page in the image happens
1888  *      to be a highmem page and its contents should be stored in the
1889  *      highmem.  Additionally, it releases the memory that will not be
1890  *      used any more.
1891  */
1892
1893 void snapshot_write_finalize(struct snapshot_handle *handle)
1894 {
1895         copy_last_highmem_page();
1896         /* Free only if we have loaded the image entirely */
1897         if (handle->prev && handle->cur > nr_meta_pages + nr_copy_pages) {
1898                 memory_bm_free(&orig_bm, PG_UNSAFE_CLEAR);
1899                 free_highmem_data();
1900         }
1901 }
1902
1903 int snapshot_image_loaded(struct snapshot_handle *handle)
1904 {
1905         return !(!nr_copy_pages || !last_highmem_page_copied() ||
1906                         handle->cur <= nr_meta_pages + nr_copy_pages);
1907 }
1908
1909 #ifdef CONFIG_HIGHMEM
1910 /* Assumes that @buf is ready and points to a "safe" page */
1911 static inline void
1912 swap_two_pages_data(struct page *p1, struct page *p2, void *buf)
1913 {
1914         void *kaddr1, *kaddr2;
1915
1916         kaddr1 = kmap_atomic(p1, KM_USER0);
1917         kaddr2 = kmap_atomic(p2, KM_USER1);
1918         memcpy(buf, kaddr1, PAGE_SIZE);
1919         memcpy(kaddr1, kaddr2, PAGE_SIZE);
1920         memcpy(kaddr2, buf, PAGE_SIZE);
1921         kunmap_atomic(kaddr1, KM_USER0);
1922         kunmap_atomic(kaddr2, KM_USER1);
1923 }
1924
1925 /**
1926  *      restore_highmem - for each highmem page that was allocated before
1927  *      the suspend and included in the suspend image, and also has been
1928  *      allocated by the "resume" kernel swap its current (ie. "before
1929  *      resume") contents with the previous (ie. "before suspend") one.
1930  *
1931  *      If the resume eventually fails, we can call this function once
1932  *      again and restore the "before resume" highmem state.
1933  */
1934
1935 int restore_highmem(void)
1936 {
1937         struct highmem_pbe *pbe = highmem_pblist;
1938         void *buf;
1939
1940         if (!pbe)
1941                 return 0;
1942
1943         buf = get_image_page(GFP_ATOMIC, PG_SAFE);
1944         if (!buf)
1945                 return -ENOMEM;
1946
1947         while (pbe) {
1948                 swap_two_pages_data(pbe->copy_page, pbe->orig_page, buf);
1949                 pbe = pbe->next;
1950         }
1951         free_image_page(buf, PG_UNSAFE_CLEAR);
1952         return 0;
1953 }
1954 #endif /* CONFIG_HIGHMEM */