Skip to content

Commit

Permalink
PM / Hibernate: Fix memory corruption related to swap
Browse files Browse the repository at this point in the history
There is a problem that swap pages allocated before the creation of
a hibernation image can be released and used for storing the contents
of different memory pages while the image is being saved.  Since the
kernel stored in the image doesn't know of that, it causes memory
corruption to occur after resume from hibernation, especially on
systems with relatively small RAM that need to swap often.

This issue can be addressed by keeping the GFP_IOFS bits clear
in gfp_allowed_mask during the entire hibernation, including the
saving of the image, until the system is finally turned off or
the hibernation is aborted.  Unfortunately, for this purpose
it's necessary to rework the way in which the hibernate and
suspend code manipulates gfp_allowed_mask.

This change is based on an earlier patch from Hugh Dickins.

Signed-off-by: Rafael J. Wysocki <[email protected]>
Reported-by: Ondrej Zary <[email protected]>
Acked-by: Hugh Dickins <[email protected]>
Reviewed-by: KAMEZAWA Hiroyuki <[email protected]>
Cc: [email protected]
  • Loading branch information
rjwysocki committed Dec 6, 2010
1 parent 9f339ca commit c9e664f
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 22 deletions.
4 changes: 2 additions & 2 deletions include/linux/gfp.h
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,7 @@ void drain_local_pages(void *dummy);

extern gfp_t gfp_allowed_mask;

extern void set_gfp_allowed_mask(gfp_t mask);
extern gfp_t clear_gfp_allowed_mask(gfp_t mask);
extern void pm_restrict_gfp_mask(void);
extern void pm_restore_gfp_mask(void);

#endif /* __LINUX_GFP_H */
22 changes: 12 additions & 10 deletions kernel/power/hibernate.c
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,6 @@ static int create_image(int platform_mode)
int hibernation_snapshot(int platform_mode)
{
int error;
gfp_t saved_mask;

error = platform_begin(platform_mode);
if (error)
Expand All @@ -339,7 +338,7 @@ int hibernation_snapshot(int platform_mode)
goto Close;

suspend_console();
saved_mask = clear_gfp_allowed_mask(GFP_IOFS);
pm_restrict_gfp_mask();
error = dpm_suspend_start(PMSG_FREEZE);
if (error)
goto Recover_platform;
Expand All @@ -348,7 +347,10 @@ int hibernation_snapshot(int platform_mode)
goto Recover_platform;

error = create_image(platform_mode);
/* Control returns here after successful restore */
/*
* Control returns here (1) after the image has been created or the
* image creation has failed and (2) after a successful restore.
*/

Resume_devices:
/* We may need to release the preallocated image pages here. */
Expand All @@ -357,7 +359,10 @@ int hibernation_snapshot(int platform_mode)

dpm_resume_end(in_suspend ?
(error ? PMSG_RECOVER : PMSG_THAW) : PMSG_RESTORE);
set_gfp_allowed_mask(saved_mask);

if (error || !in_suspend)
pm_restore_gfp_mask();

resume_console();
Close:
platform_end(platform_mode);
Expand Down Expand Up @@ -452,17 +457,16 @@ static int resume_target_kernel(bool platform_mode)
int hibernation_restore(int platform_mode)
{
int error;
gfp_t saved_mask;

pm_prepare_console();
suspend_console();
saved_mask = clear_gfp_allowed_mask(GFP_IOFS);
pm_restrict_gfp_mask();
error = dpm_suspend_start(PMSG_QUIESCE);
if (!error) {
error = resume_target_kernel(platform_mode);
dpm_resume_end(PMSG_RECOVER);
}
set_gfp_allowed_mask(saved_mask);
pm_restore_gfp_mask();
resume_console();
pm_restore_console();
return error;
Expand All @@ -476,7 +480,6 @@ int hibernation_restore(int platform_mode)
int hibernation_platform_enter(void)
{
int error;
gfp_t saved_mask;

if (!hibernation_ops)
return -ENOSYS;
Expand All @@ -492,7 +495,6 @@ int hibernation_platform_enter(void)

entering_platform_hibernation = true;
suspend_console();
saved_mask = clear_gfp_allowed_mask(GFP_IOFS);
error = dpm_suspend_start(PMSG_HIBERNATE);
if (error) {
if (hibernation_ops->recover)
Expand Down Expand Up @@ -536,7 +538,6 @@ int hibernation_platform_enter(void)
Resume_devices:
entering_platform_hibernation = false;
dpm_resume_end(PMSG_RESTORE);
set_gfp_allowed_mask(saved_mask);
resume_console();

Close:
Expand Down Expand Up @@ -646,6 +647,7 @@ int hibernate(void)
swsusp_free();
if (!error)
power_down();
pm_restore_gfp_mask();
} else {
pr_debug("PM: Image restored successfully.\n");
}
Expand Down
5 changes: 2 additions & 3 deletions kernel/power/suspend.c
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,6 @@ static int suspend_enter(suspend_state_t state)
int suspend_devices_and_enter(suspend_state_t state)
{
int error;
gfp_t saved_mask;

if (!suspend_ops)
return -ENOSYS;
Expand All @@ -208,7 +207,7 @@ int suspend_devices_and_enter(suspend_state_t state)
goto Close;
}
suspend_console();
saved_mask = clear_gfp_allowed_mask(GFP_IOFS);
pm_restrict_gfp_mask();
suspend_test_start();
error = dpm_suspend_start(PMSG_SUSPEND);
if (error) {
Expand All @@ -225,7 +224,7 @@ int suspend_devices_and_enter(suspend_state_t state)
suspend_test_start();
dpm_resume_end(PMSG_RESUME);
suspend_test_finish("resume devices");
set_gfp_allowed_mask(saved_mask);
pm_restore_gfp_mask();
resume_console();
Close:
if (suspend_ops->end)
Expand Down
2 changes: 2 additions & 0 deletions kernel/power/user.c
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,7 @@ static long snapshot_ioctl(struct file *filp, unsigned int cmd,
case SNAPSHOT_UNFREEZE:
if (!data->frozen || data->ready)
break;
pm_restore_gfp_mask();
thaw_processes();
usermodehelper_enable();
data->frozen = 0;
Expand All @@ -275,6 +276,7 @@ static long snapshot_ioctl(struct file *filp, unsigned int cmd,
error = -EPERM;
break;
}
pm_restore_gfp_mask();
error = hibernation_snapshot(data->platform_support);
if (!error)
error = put_user(in_suspend, (int __user *)arg);
Expand Down
19 changes: 12 additions & 7 deletions mm/page_alloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -104,19 +104,24 @@ gfp_t gfp_allowed_mask __read_mostly = GFP_BOOT_MASK;
* only be modified with pm_mutex held, unless the suspend/hibernate code is
* guaranteed not to run in parallel with that modification).
*/
void set_gfp_allowed_mask(gfp_t mask)

static gfp_t saved_gfp_mask;

void pm_restore_gfp_mask(void)
{
WARN_ON(!mutex_is_locked(&pm_mutex));
gfp_allowed_mask = mask;
if (saved_gfp_mask) {
gfp_allowed_mask = saved_gfp_mask;
saved_gfp_mask = 0;
}
}

gfp_t clear_gfp_allowed_mask(gfp_t mask)
void pm_restrict_gfp_mask(void)
{
gfp_t ret = gfp_allowed_mask;

WARN_ON(!mutex_is_locked(&pm_mutex));
gfp_allowed_mask &= ~mask;
return ret;
WARN_ON(saved_gfp_mask);
saved_gfp_mask = gfp_allowed_mask;
gfp_allowed_mask &= ~GFP_IOFS;
}
#endif /* CONFIG_PM_SLEEP */

Expand Down

0 comments on commit c9e664f

Please sign in to comment.