PM / hibernate: Clean up function headers in snapshot.c
[cascardo/linux.git] / kernel / power / snapshot.c
index 3a97060..1fe0ddb 100644 (file)
@@ -74,6 +74,22 @@ void __init hibernate_image_size_init(void)
  */
 struct pbe *restore_pblist;
 
+/* struct linked_page is used to build chains of pages */
+
+#define LINKED_PAGE_DATA_SIZE  (PAGE_SIZE - sizeof(void *))
+
+struct linked_page {
+       struct linked_page *next;
+       char data[LINKED_PAGE_DATA_SIZE];
+} __packed;
+
+/*
+ * List of "safe" pages (ie. pages that were not used by the image kernel
+ * before hibernation) that may be used as temporary storage for image kernel
+ * memory contents.
+ */
+static struct linked_page *safe_pages_list;
+
 /* Pointer to an auxiliary buffer (1 page) */
 static void *buffer;
 
@@ -113,9 +129,21 @@ static void *get_image_page(gfp_t gfp_mask, int safe_needed)
        return res;
 }
 
+static void *__get_safe_page(gfp_t gfp_mask)
+{
+       if (safe_pages_list) {
+               void *ret = safe_pages_list;
+
+               safe_pages_list = safe_pages_list->next;
+               memset(ret, 0, PAGE_SIZE);
+               return ret;
+       }
+       return get_image_page(gfp_mask, PG_SAFE);
+}
+
 unsigned long get_safe_page(gfp_t gfp_mask)
 {
-       return (unsigned long)get_image_page(gfp_mask, PG_SAFE);
+       return (unsigned long)__get_safe_page(gfp_mask);
 }
 
 static struct page *alloc_image_page(gfp_t gfp_mask)
@@ -130,6 +158,14 @@ static struct page *alloc_image_page(gfp_t gfp_mask)
        return page;
 }
 
+static void recycle_safe_page(void *page_address)
+{
+       struct linked_page *lp = page_address;
+
+       lp->next = safe_pages_list;
+       safe_pages_list = lp;
+}
+
 /**
  *     free_image_page - free page represented by @addr, allocated with
  *     get_image_page (page flags set by it must be cleared)
@@ -150,17 +186,8 @@ static inline void free_image_page(void *addr, int clear_nosave_free)
        __free_page(page);
 }
 
-/* struct linked_page is used to build chains of pages */
-
-#define LINKED_PAGE_DATA_SIZE  (PAGE_SIZE - sizeof(void *))
-
-struct linked_page {
-       struct linked_page *next;
-       char data[LINKED_PAGE_DATA_SIZE];
-} __packed;
-
-static inline void
-free_list_of_pages(struct linked_page *list, int clear_page_nosave)
+static inline void free_list_of_pages(struct linked_page *list,
+                                     int clear_page_nosave)
 {
        while (list) {
                struct linked_page *lp = list->next;
@@ -192,8 +219,8 @@ struct chain_allocator {
        int safe_needed;        /* if set, only "safe" pages are allocated */
 };
 
-static void
-chain_init(struct chain_allocator *ca, gfp_t gfp_mask, int safe_needed)
+static void chain_init(struct chain_allocator *ca, gfp_t gfp_mask,
+                      int safe_needed)
 {
        ca->chain = NULL;
        ca->used_space = LINKED_PAGE_DATA_SIZE;
@@ -208,7 +235,8 @@ static void *chain_alloc(struct chain_allocator *ca, unsigned int size)
        if (LINKED_PAGE_DATA_SIZE - ca->used_space < size) {
                struct linked_page *lp;
 
-               lp = get_image_page(ca->gfp_mask, ca->safe_needed);
+               lp = ca->safe_needed ? __get_safe_page(ca->gfp_mask) :
+                                       get_image_page(ca->gfp_mask, PG_ANY);
                if (!lp)
                        return NULL;
 
@@ -424,10 +452,11 @@ static void free_zone_bm_rtree(struct mem_zone_bm_rtree *zone,
  *     This function also allocated and builds the radix tree for the
  *     zone.
  */
-static struct mem_zone_bm_rtree *
-create_zone_bm_rtree(gfp_t gfp_mask, int safe_needed,
-                    struct chain_allocator *ca,
-                    unsigned long start, unsigned long end)
+static struct mem_zone_bm_rtree *create_zone_bm_rtree(gfp_t gfp_mask,
+                                                     int safe_needed,
+                                                     struct chain_allocator *ca,
+                                                     unsigned long start,
+                                                     unsigned long end)
 {
        struct mem_zone_bm_rtree *zone;
        unsigned int i, nr_blocks;
@@ -567,8 +596,8 @@ static int create_mem_extents(struct list_head *list, gfp_t gfp_mask)
 /**
   *    memory_bm_create - allocate memory for a memory bitmap
   */
-static int
-memory_bm_create(struct memory_bitmap *bm, gfp_t gfp_mask, int safe_needed)
+static int memory_bm_create(struct memory_bitmap *bm, gfp_t gfp_mask,
+                           int safe_needed)
 {
        struct chain_allocator ca;
        struct list_head mem_extents;
@@ -832,15 +861,42 @@ struct nosave_region {
 
 static LIST_HEAD(nosave_regions);
 
+static void recycle_zone_bm_rtree(struct mem_zone_bm_rtree *zone)
+{
+       struct rtree_node *node;
+
+       list_for_each_entry(node, &zone->nodes, list)
+               recycle_safe_page(node->data);
+
+       list_for_each_entry(node, &zone->leaves, list)
+               recycle_safe_page(node->data);
+}
+
+static void memory_bm_recycle(struct memory_bitmap *bm)
+{
+       struct mem_zone_bm_rtree *zone;
+       struct linked_page *p_list;
+
+       list_for_each_entry(zone, &bm->zones, list)
+               recycle_zone_bm_rtree(zone);
+
+       p_list = bm->p_list;
+       while (p_list) {
+               struct linked_page *lp = p_list;
+
+               p_list = lp->next;
+               recycle_safe_page(lp);
+       }
+}
+
 /**
  *     register_nosave_region - register a range of page frames the contents
  *     of which should not be saved during the suspend (to be used in the early
  *     initialization code)
  */
 
-void __init
-__register_nosave_region(unsigned long start_pfn, unsigned long end_pfn,
-                        int use_kmalloc)
+void __init __register_nosave_region(unsigned long start_pfn,
+                                    unsigned long end_pfn, int use_kmalloc)
 {
        struct nosave_region *region;
 
@@ -1221,8 +1277,7 @@ static void safe_copy_page(void *dst, struct page *s_page)
 
 
 #ifdef CONFIG_HIGHMEM
-static inline struct page *
-page_is_saveable(struct zone *zone, unsigned long pfn)
+static inline struct page *page_is_saveable(struct zone *zone, unsigned long pfn)
 {
        return is_highmem(zone) ?
                saveable_highmem_page(zone, pfn) : saveable_page(zone, pfn);
@@ -1265,8 +1320,8 @@ static inline void copy_data_page(unsigned long dst_pfn, unsigned long src_pfn)
 }
 #endif /* CONFIG_HIGHMEM */
 
-static void
-copy_data_pages(struct memory_bitmap *copy_bm, struct memory_bitmap *orig_bm)
+static void copy_data_pages(struct memory_bitmap *copy_bm,
+                           struct memory_bitmap *orig_bm)
 {
        struct zone *zone;
        unsigned long pfn;
@@ -1429,8 +1484,8 @@ static unsigned long __fraction(u64 x, u64 multiplier, u64 base)
 }
 
 static unsigned long preallocate_highmem_fraction(unsigned long nr_pages,
-                                               unsigned long highmem,
-                                               unsigned long total)
+                                                 unsigned long highmem,
+                                                 unsigned long total)
 {
        unsigned long alloc = __fraction(nr_pages, highmem, total);
 
@@ -1443,8 +1498,8 @@ static inline unsigned long preallocate_image_highmem(unsigned long nr_pages)
 }
 
 static inline unsigned long preallocate_highmem_fraction(unsigned long nr_pages,
-                                               unsigned long highmem,
-                                               unsigned long total)
+                                                        unsigned long highmem,
+                                                        unsigned long total)
 {
        return 0;
 }
@@ -1724,8 +1779,7 @@ static unsigned int count_pages_for_highmem(unsigned int nr_highmem)
        return nr_highmem;
 }
 #else
-static unsigned int
-count_pages_for_highmem(unsigned int nr_highmem) { return 0; }
+static unsigned int count_pages_for_highmem(unsigned int nr_highmem) { return 0; }
 #endif /* CONFIG_HIGHMEM */
 
 /**
@@ -1767,8 +1821,8 @@ static inline int get_highmem_buffer(int safe_needed)
  *     highmem pages is lesser than that, allocate them all.
  */
 
-static inline unsigned int
-alloc_highmem_pages(struct memory_bitmap *bm, unsigned int nr_highmem)
+static inline unsigned int alloc_highmem_pages(struct memory_bitmap *bm,
+                                              unsigned int nr_highmem)
 {
        unsigned int to_alloc = count_free_highmem_pages();
 
@@ -1787,8 +1841,8 @@ alloc_highmem_pages(struct memory_bitmap *bm, unsigned int nr_highmem)
 #else
 static inline int get_highmem_buffer(int safe_needed) { return 0; }
 
-static inline unsigned int
-alloc_highmem_pages(struct memory_bitmap *bm, unsigned int n) { return 0; }
+static inline unsigned int alloc_highmem_pages(struct memory_bitmap *bm,
+                                              unsigned int n) { return 0; }
 #endif /* CONFIG_HIGHMEM */
 
 /**
@@ -1803,9 +1857,9 @@ alloc_highmem_pages(struct memory_bitmap *bm, unsigned int n) { return 0; }
  *     copy_data_pages() works.
  */
 
-static int
-swsusp_alloc(struct memory_bitmap *orig_bm, struct memory_bitmap *copy_bm,
-               unsigned int nr_pages, unsigned int nr_highmem)
+static int swsusp_alloc(struct memory_bitmap *orig_bm,
+                       struct memory_bitmap *copy_bm,
+                       unsigned int nr_pages, unsigned int nr_highmem)
 {
        if (nr_highmem > 0) {
                if (get_highmem_buffer(PG_ANY))
@@ -1922,8 +1976,7 @@ static int init_header(struct swsusp_info *info)
  *     are stored in the array @buf[] (1 page at a time)
  */
 
-static inline void
-pack_pfns(unsigned long *buf, struct memory_bitmap *bm)
+static inline void pack_pfns(unsigned long *buf, struct memory_bitmap *bm)
 {
        int j;
 
@@ -1999,53 +2052,41 @@ int snapshot_read_next(struct snapshot_handle *handle)
        return PAGE_SIZE;
 }
 
+static void duplicate_memory_bitmap(struct memory_bitmap *dst,
+                                   struct memory_bitmap *src)
+{
+       unsigned long pfn;
+
+       memory_bm_position_reset(src);
+       pfn = memory_bm_next_pfn(src);
+       while (pfn != BM_END_OF_MAP) {
+               memory_bm_set_bit(dst, pfn);
+               pfn = memory_bm_next_pfn(src);
+       }
+}
+
 /**
  *     mark_unsafe_pages - mark the pages that cannot be used for storing
  *     the image during resume, because they conflict with the pages that
  *     had been used before suspend
  */
 
-static int mark_unsafe_pages(struct memory_bitmap *bm)
+static void mark_unsafe_pages(struct memory_bitmap *bm)
 {
-       struct zone *zone;
-       unsigned long pfn, max_zone_pfn;
+       unsigned long pfn;
 
-       /* Clear page flags */
-       for_each_populated_zone(zone) {
-               max_zone_pfn = zone_end_pfn(zone);
-               for (pfn = zone->zone_start_pfn; pfn < max_zone_pfn; pfn++)
-                       if (pfn_valid(pfn))
-                               swsusp_unset_page_free(pfn_to_page(pfn));
+       /* Clear the "free"/"unsafe" bit for all PFNs */
+       memory_bm_position_reset(free_pages_map);
+       pfn = memory_bm_next_pfn(free_pages_map);
+       while (pfn != BM_END_OF_MAP) {
+               memory_bm_clear_current(free_pages_map);
+               pfn = memory_bm_next_pfn(free_pages_map);
        }
 
-       /* Mark pages that correspond to the "original" pfns as "unsafe" */
-       memory_bm_position_reset(bm);
-       do {
-               pfn = memory_bm_next_pfn(bm);
-               if (likely(pfn != BM_END_OF_MAP)) {
-                       if (likely(pfn_valid(pfn)))
-                               swsusp_set_page_free(pfn_to_page(pfn));
-                       else
-                               return -EFAULT;
-               }
-       } while (pfn != BM_END_OF_MAP);
+       /* Mark pages that correspond to the "original" PFNs as "unsafe" */
+       duplicate_memory_bitmap(free_pages_map, bm);
 
        allocated_unsafe_pages = 0;
-
-       return 0;
-}
-
-static void
-duplicate_memory_bitmap(struct memory_bitmap *dst, struct memory_bitmap *src)
-{
-       unsigned long pfn;
-
-       memory_bm_position_reset(src);
-       pfn = memory_bm_next_pfn(src);
-       while (pfn != BM_END_OF_MAP) {
-               memory_bm_set_bit(dst, pfn);
-               pfn = memory_bm_next_pfn(src);
-       }
 }
 
 static int check_header(struct swsusp_info *info)
@@ -2066,8 +2107,7 @@ static int check_header(struct swsusp_info *info)
  *     load header - check the image header and copy data from it
  */
 
-static int
-load_header(struct swsusp_info *info)
+static int load_header(struct swsusp_info *info)
 {
        int error;
 
@@ -2095,7 +2135,7 @@ static int unpack_orig_pfns(unsigned long *buf, struct memory_bitmap *bm)
                /* Extract and buffer page key for data page (s390 only). */
                page_key_memorize(buf + j);
 
-               if (memory_bm_pfn_present(bm, buf[j]))
+               if (pfn_valid(buf[j]) && memory_bm_pfn_present(bm, buf[j]))
                        memory_bm_set_bit(bm, buf[j]);
                else
                        return -EFAULT;
@@ -2104,11 +2144,6 @@ static int unpack_orig_pfns(unsigned long *buf, struct memory_bitmap *bm)
        return 0;
 }
 
-/* List of "safe" pages that may be used to store data loaded from the suspend
- * image
- */
-static struct linked_page *safe_pages_list;
-
 #ifdef CONFIG_HIGHMEM
 /* struct highmem_pbe is used for creating the list of highmem pages that
  * should be restored atomically during the resume from disk, because the page
@@ -2165,8 +2200,8 @@ static unsigned int safe_highmem_pages;
 
 static struct memory_bitmap *safe_highmem_bm;
 
-static int
-prepare_highmem_image(struct memory_bitmap *bm, unsigned int *nr_highmem_p)
+static int prepare_highmem_image(struct memory_bitmap *bm,
+                                unsigned int *nr_highmem_p)
 {
        unsigned int to_alloc;
 
@@ -2220,8 +2255,8 @@ prepare_highmem_image(struct memory_bitmap *bm, unsigned int *nr_highmem_p)
 
 static struct page *last_highmem_page;
 
-static void *
-get_highmem_page_buffer(struct page *page, struct chain_allocator *ca)
+static void *get_highmem_page_buffer(struct page *page,
+                                    struct chain_allocator *ca)
 {
        struct highmem_pbe *pbe;
        void *kaddr;
@@ -2294,17 +2329,13 @@ static inline void free_highmem_data(void)
                free_image_page(buffer, PG_UNSAFE_CLEAR);
 }
 #else
-static unsigned int
-count_highmem_image_pages(struct memory_bitmap *bm) { return 0; }
+static unsigned int count_highmem_image_pages(struct memory_bitmap *bm) { return 0; }
 
-static inline int
-prepare_highmem_image(struct memory_bitmap *bm, unsigned int *nr_highmem_p)
-{
-       return 0;
-}
+static inline int prepare_highmem_image(struct memory_bitmap *bm,
+                                       unsigned int *nr_highmem_p) { return 0; }
 
-static inline void *
-get_highmem_page_buffer(struct page *page, struct chain_allocator *ca)
+static inline void *get_highmem_page_buffer(struct page *page,
+                                           struct chain_allocator *ca)
 {
        return ERR_PTR(-EINVAL);
 }
@@ -2330,11 +2361,10 @@ static inline void free_highmem_data(void) {}
 
 #define PBES_PER_LINKED_PAGE   (LINKED_PAGE_DATA_SIZE / sizeof(struct pbe))
 
-static int
-prepare_image(struct memory_bitmap *new_bm, struct memory_bitmap *bm)
+static int prepare_image(struct memory_bitmap *new_bm, struct memory_bitmap *bm)
 {
        unsigned int nr_pages, nr_highmem;
-       struct linked_page *sp_list, *lp;
+       struct linked_page *lp;
        int error;
 
        /* If there is no highmem, the buffer will not be necessary */
@@ -2342,9 +2372,7 @@ prepare_image(struct memory_bitmap *new_bm, struct memory_bitmap *bm)
        buffer = NULL;
 
        nr_highmem = count_highmem_image_pages(bm);
-       error = mark_unsafe_pages(bm);
-       if (error)
-               goto Free;
+       mark_unsafe_pages(bm);
 
        error = memory_bm_create(new_bm, GFP_ATOMIC, PG_SAFE);
        if (error)
@@ -2362,9 +2390,9 @@ prepare_image(struct memory_bitmap *new_bm, struct memory_bitmap *bm)
         * NOTE: This way we make sure there will be enough safe pages for the
         * chain_alloc() in get_buffer().  It is a bit wasteful, but
         * nr_copy_pages cannot be greater than 50% of the memory anyway.
+        *
+        * nr_copy_pages cannot be less than allocated_unsafe_pages too.
         */
-       sp_list = NULL;
-       /* nr_copy_pages cannot be lesser than allocated_unsafe_pages */
        nr_pages = nr_copy_pages - nr_highmem - allocated_unsafe_pages;
        nr_pages = DIV_ROUND_UP(nr_pages, PBES_PER_LINKED_PAGE);
        while (nr_pages > 0) {
@@ -2373,12 +2401,11 @@ prepare_image(struct memory_bitmap *new_bm, struct memory_bitmap *bm)
                        error = -ENOMEM;
                        goto Free;
                }
-               lp->next = sp_list;
-               sp_list = lp;
+               lp->next = safe_pages_list;
+               safe_pages_list = lp;
                nr_pages--;
        }
        /* Preallocate memory for the image */
-       safe_pages_list = NULL;
        nr_pages = nr_copy_pages - nr_highmem - allocated_unsafe_pages;
        while (nr_pages > 0) {
                lp = (struct linked_page *)get_zeroed_page(GFP_ATOMIC);
@@ -2396,12 +2423,6 @@ prepare_image(struct memory_bitmap *new_bm, struct memory_bitmap *bm)
                swsusp_set_page_free(virt_to_page(lp));
                nr_pages--;
        }
-       /* Free the reserved safe pages so that chain_alloc() can use them */
-       while (sp_list) {
-               lp = sp_list->next;
-               free_image_page(sp_list, PG_UNSAFE_CLEAR);
-               sp_list = lp;
-       }
        return 0;
 
  Free:
@@ -2491,6 +2512,8 @@ int snapshot_write_next(struct snapshot_handle *handle)
                if (error)
                        return error;
 
+               safe_pages_list = NULL;
+
                error = memory_bm_create(&copy_bm, GFP_ATOMIC, PG_ANY);
                if (error)
                        return error;
@@ -2546,9 +2569,9 @@ void snapshot_write_finalize(struct snapshot_handle *handle)
        /* Restore page key for data page (s390 only). */
        page_key_write(handle->buffer);
        page_key_free();
-       /* Free only if we have loaded the image entirely */
+       /* Do that only if we have loaded the image entirely */
        if (handle->cur > 1 && handle->cur > nr_meta_pages + nr_copy_pages) {
-               memory_bm_free(&orig_bm, PG_UNSAFE_CLEAR);
+               memory_bm_recycle(&orig_bm);
                free_highmem_data();
        }
 }
@@ -2561,8 +2584,8 @@ int snapshot_image_loaded(struct snapshot_handle *handle)
 
 #ifdef CONFIG_HIGHMEM
 /* Assumes that @buf is ready and points to a "safe" page */
-static inline void
-swap_two_pages_data(struct page *p1, struct page *p2, void *buf)
+static inline void swap_two_pages_data(struct page *p1, struct page *p2,
+                                      void *buf)
 {
        void *kaddr1, *kaddr2;