Commit 054bd4c1 authored by Rafael J. Wysocki's avatar Rafael J. Wysocki Committed by Linus Torvalds

[PATCH] swsusp: reduce code duplication

The changes made by this patch are necessary for the pagedir relocation
simplification in the next patch.  Additionally, these changes allow us to
drop check_pagedir() and make get_safe_page() be a one-line wrapper around
alloc_image_page() (get_safe_page() goes to snapshot.c, because
alloc_image_page() is static and it does not make sense to export it).
Signed-off-by: default avatarRafael J. Wysocki <rjw@sisk.pl>
Cc: Pavel Machek <pavel@ucw.cz>
Signed-off-by: default avatarAndrew Morton <akpm@osdl.org>
Signed-off-by: default avatarLinus Torvalds <torvalds@osdl.org>
parent a31751e0
...@@ -66,7 +66,8 @@ extern asmlinkage int swsusp_arch_suspend(void); ...@@ -66,7 +66,8 @@ extern asmlinkage int swsusp_arch_suspend(void);
extern asmlinkage int swsusp_arch_resume(void); extern asmlinkage int swsusp_arch_resume(void);
extern int restore_highmem(void); extern int restore_highmem(void);
extern struct pbe * alloc_pagedir(unsigned nr_pages); extern struct pbe *alloc_pagedir(unsigned nr_pages, gfp_t gfp_mask, int safe_needed);
extern void create_pbe_list(struct pbe *pblist, unsigned nr_pages); extern void create_pbe_list(struct pbe *pblist, unsigned nr_pages);
extern void swsusp_free(void); extern void swsusp_free(void);
extern int alloc_data_pages(struct pbe *pblist, gfp_t gfp_mask, int safe_needed);
extern int enough_swap(unsigned nr_pages); extern int enough_swap(unsigned nr_pages);
...@@ -269,9 +269,30 @@ void create_pbe_list(struct pbe *pblist, unsigned int nr_pages) ...@@ -269,9 +269,30 @@ void create_pbe_list(struct pbe *pblist, unsigned int nr_pages)
pr_debug("create_pbe_list(): initialized %d PBEs\n", num); pr_debug("create_pbe_list(): initialized %d PBEs\n", num);
} }
static void *alloc_image_page(void) /**
* @safe_needed - on resume, for storing the PBE list and the image,
* we can only use memory pages that do not conflict with the pages
* which had been used before suspend.
*
* The unsafe pages are marked with the PG_nosave_free flag
*
* Allocated but unusable (ie eaten) memory pages should be marked
* so that swsusp_free() can release them
*/
static inline void *alloc_image_page(gfp_t gfp_mask, int safe_needed)
{ {
void *res = (void *)get_zeroed_page(GFP_ATOMIC | __GFP_COLD); void *res;
if (safe_needed)
do {
res = (void *)get_zeroed_page(gfp_mask);
if (res && PageNosaveFree(virt_to_page(res)))
/* This is for swsusp_free() */
SetPageNosave(virt_to_page(res));
} while (res && PageNosaveFree(virt_to_page(res)));
else
res = (void *)get_zeroed_page(gfp_mask);
if (res) { if (res) {
SetPageNosave(virt_to_page(res)); SetPageNosave(virt_to_page(res));
SetPageNosaveFree(virt_to_page(res)); SetPageNosaveFree(virt_to_page(res));
...@@ -279,6 +300,11 @@ static void *alloc_image_page(void) ...@@ -279,6 +300,11 @@ static void *alloc_image_page(void)
return res; return res;
} }
unsigned long get_safe_page(gfp_t gfp_mask)
{
return (unsigned long)alloc_image_page(gfp_mask, 1);
}
/** /**
* alloc_pagedir - Allocate the page directory. * alloc_pagedir - Allocate the page directory.
* *
...@@ -292,7 +318,7 @@ static void *alloc_image_page(void) ...@@ -292,7 +318,7 @@ static void *alloc_image_page(void)
* On each page we set up a list of struct_pbe elements. * On each page we set up a list of struct_pbe elements.
*/ */
struct pbe *alloc_pagedir(unsigned int nr_pages) struct pbe *alloc_pagedir(unsigned int nr_pages, gfp_t gfp_mask, int safe_needed)
{ {
unsigned int num; unsigned int num;
struct pbe *pblist, *pbe; struct pbe *pblist, *pbe;
...@@ -301,12 +327,12 @@ struct pbe *alloc_pagedir(unsigned int nr_pages) ...@@ -301,12 +327,12 @@ struct pbe *alloc_pagedir(unsigned int nr_pages)
return NULL; return NULL;
pr_debug("alloc_pagedir(): nr_pages = %d\n", nr_pages); pr_debug("alloc_pagedir(): nr_pages = %d\n", nr_pages);
pblist = alloc_image_page(); pblist = alloc_image_page(gfp_mask, safe_needed);
/* FIXME: rewrite this ugly loop */ /* FIXME: rewrite this ugly loop */
for (pbe = pblist, num = PBES_PER_PAGE; pbe && num < nr_pages; for (pbe = pblist, num = PBES_PER_PAGE; pbe && num < nr_pages;
pbe = pbe->next, num += PBES_PER_PAGE) { pbe = pbe->next, num += PBES_PER_PAGE) {
pbe += PB_PAGE_SKIP; pbe += PB_PAGE_SKIP;
pbe->next = alloc_image_page(); pbe->next = alloc_image_page(gfp_mask, safe_needed);
} }
if (!pbe) { /* get_zeroed_page() failed */ if (!pbe) { /* get_zeroed_page() failed */
free_pagedir(pblist); free_pagedir(pblist);
...@@ -354,25 +380,33 @@ static int enough_free_mem(unsigned int nr_pages) ...@@ -354,25 +380,33 @@ static int enough_free_mem(unsigned int nr_pages)
(nr_pages + PBES_PER_PAGE - 1) / PBES_PER_PAGE); (nr_pages + PBES_PER_PAGE - 1) / PBES_PER_PAGE);
} }
int alloc_data_pages(struct pbe *pblist, gfp_t gfp_mask, int safe_needed)
{
struct pbe *p;
for_each_pbe (p, pblist) {
p->address = (unsigned long)alloc_image_page(gfp_mask, safe_needed);
if (!p->address)
return -ENOMEM;
}
return 0;
}
static struct pbe *swsusp_alloc(unsigned int nr_pages) static struct pbe *swsusp_alloc(unsigned int nr_pages)
{ {
struct pbe *pblist, *p; struct pbe *pblist;
if (!(pblist = alloc_pagedir(nr_pages))) { if (!(pblist = alloc_pagedir(nr_pages, GFP_ATOMIC | __GFP_COLD, 0))) {
printk(KERN_ERR "suspend: Allocating pagedir failed.\n"); printk(KERN_ERR "suspend: Allocating pagedir failed.\n");
return NULL; return NULL;
} }
create_pbe_list(pblist, nr_pages); create_pbe_list(pblist, nr_pages);
for_each_pbe (p, pblist) { if (alloc_data_pages(pblist, GFP_ATOMIC | __GFP_COLD, 0)) {
p->address = (unsigned long)alloc_image_page();
if (!p->address) {
printk(KERN_ERR "suspend: Allocating image pages failed.\n"); printk(KERN_ERR "suspend: Allocating image pages failed.\n");
swsusp_free(); swsusp_free();
return NULL; return NULL;
} }
}
return pblist; return pblist;
} }
......
...@@ -628,59 +628,6 @@ int swsusp_resume(void) ...@@ -628,59 +628,6 @@ int swsusp_resume(void)
return error; return error;
} }
/**
* On resume, for storing the PBE list and the image,
* we can only use memory pages that do not conflict with the pages
* which had been used before suspend.
*
* We don't know which pages are usable until we allocate them.
*
* Allocated but unusable (ie eaten) memory pages are marked so that
* swsusp_free() can release them
*/
unsigned long get_safe_page(gfp_t gfp_mask)
{
unsigned long m;
do {
m = get_zeroed_page(gfp_mask);
if (m && PageNosaveFree(virt_to_page(m)))
/* This is for swsusp_free() */
SetPageNosave(virt_to_page(m));
} while (m && PageNosaveFree(virt_to_page(m)));
if (m) {
/* This is for swsusp_free() */
SetPageNosave(virt_to_page(m));
SetPageNosaveFree(virt_to_page(m));
}
return m;
}
/**
* check_pagedir - We ensure here that pages that the PBEs point to
* won't collide with pages where we're going to restore from the loaded
* pages later
*/
static int check_pagedir(struct pbe *pblist)
{
struct pbe *p;
/* This is necessary, so that we can free allocated pages
* in case of failure
*/
for_each_pbe (p, pblist)
p->address = 0UL;
for_each_pbe (p, pblist) {
p->address = get_safe_page(GFP_ATOMIC);
if (!p->address)
return -ENOMEM;
}
return 0;
}
/** /**
* swsusp_pagedir_relocate - It is possible, that some memory pages * swsusp_pagedir_relocate - It is possible, that some memory pages
* occupied by the list of PBEs collide with pages where we're going to * occupied by the list of PBEs collide with pages where we're going to
...@@ -990,7 +937,7 @@ static int read_suspend_image(void) ...@@ -990,7 +937,7 @@ static int read_suspend_image(void)
int error = 0; int error = 0;
struct pbe *p; struct pbe *p;
if (!(p = alloc_pagedir(nr_copy_pages))) if (!(p = alloc_pagedir(nr_copy_pages, GFP_ATOMIC, 0)))
return -ENOMEM; return -ENOMEM;
if ((error = read_pagedir(p))) if ((error = read_pagedir(p)))
...@@ -1003,7 +950,7 @@ static int read_suspend_image(void) ...@@ -1003,7 +950,7 @@ static int read_suspend_image(void)
/* Allocate memory for the image and read the data from swap */ /* Allocate memory for the image and read the data from swap */
error = check_pagedir(pagedir_nosave); error = alloc_data_pages(pagedir_nosave, GFP_ATOMIC, 1);
if (!error) if (!error)
error = data_read(pagedir_nosave); error = data_read(pagedir_nosave);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment