mm/pm: force GFP_NOIO during suspend/hibernation and resume

There are quite a few GFP_KERNEL memory allocations made during
suspend/hibernation and resume that may cause the system to hang, because
the I/O operations they depend on cannot be completed due to the
underlying devices being suspended.

Avoid this problem by clearing the __GFP_IO and __GFP_FS bits in
gfp_allowed_mask before suspend/hibernation and restoring the original
values of these bits in gfp_allowed_mask durig the subsequent resume.

[akpm@linux-foundation.org: fix CONFIG_PM=n linkage]
Signed-off-by: Rafael J. Wysocki <rjw@sisk.pl>
Reported-by: Maxim Levitsky <maximlevitsky@gmail.com>
Cc: Sebastian Ott <sebott@linux.vnet.ibm.com>
Cc: Benjamin Herrenschmidt <benh@kernel.crashing.org>
Cc: KOSAKI Motohiro <kosaki.motohiro@jp.fujitsu.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
This commit is contained in:
Rafael J. Wysocki 2010-03-05 13:42:13 -08:00 committed by Linus Torvalds
parent ad2bd7e0e9
commit 452aa6999e
5 changed files with 41 additions and 5 deletions

View File

@ -83,6 +83,7 @@ struct vm_area_struct;
#define GFP_HIGHUSER_MOVABLE (__GFP_WAIT | __GFP_IO | __GFP_FS | \
__GFP_HARDWALL | __GFP_HIGHMEM | \
__GFP_MOVABLE)
#define GFP_IOFS (__GFP_IO | __GFP_FS)
#ifdef CONFIG_NUMA
#define GFP_THISNODE (__GFP_THISNODE | __GFP_NOWARN | __GFP_NORETRY)
@ -337,9 +338,7 @@ void drain_local_pages(void *dummy);
extern gfp_t gfp_allowed_mask;
static inline void set_gfp_allowed_mask(gfp_t mask)
{
gfp_allowed_mask = mask;
}
extern void set_gfp_allowed_mask(gfp_t mask);
extern gfp_t clear_gfp_allowed_mask(gfp_t mask);
#endif /* __LINUX_GFP_H */

View File

@ -618,7 +618,7 @@ asmlinkage void __init start_kernel(void)
local_irq_enable();
/* Interrupts are enabled now so all GFP allocations are safe. */
set_gfp_allowed_mask(__GFP_BITS_MASK);
gfp_allowed_mask = __GFP_BITS_MASK;
kmem_cache_init_late();

View File

@ -323,6 +323,7 @@ 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)
@ -334,6 +335,7 @@ int hibernation_snapshot(int platform_mode)
goto Close;
suspend_console();
saved_mask = clear_gfp_allowed_mask(GFP_IOFS);
error = dpm_suspend_start(PMSG_FREEZE);
if (error)
goto Recover_platform;
@ -351,6 +353,7 @@ int hibernation_snapshot(int platform_mode)
dpm_resume_end(in_suspend ?
(error ? PMSG_RECOVER : PMSG_THAW) : PMSG_RESTORE);
set_gfp_allowed_mask(saved_mask);
resume_console();
Close:
platform_end(platform_mode);
@ -445,14 +448,17 @@ 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);
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);
resume_console();
pm_restore_console();
return error;
@ -466,6 +472,7 @@ int hibernation_restore(int platform_mode)
int hibernation_platform_enter(void)
{
int error;
gfp_t saved_mask;
if (!hibernation_ops)
return -ENOSYS;
@ -481,6 +488,7 @@ 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)
@ -518,6 +526,7 @@ 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:

View File

@ -189,6 +189,7 @@ 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;
@ -199,6 +200,7 @@ int suspend_devices_and_enter(suspend_state_t state)
goto Close;
}
suspend_console();
saved_mask = clear_gfp_allowed_mask(GFP_IOFS);
suspend_test_start();
error = dpm_suspend_start(PMSG_SUSPEND);
if (error) {
@ -215,6 +217,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);
resume_console();
Close:
if (suspend_ops->end)

View File

@ -76,6 +76,31 @@ unsigned long totalreserve_pages __read_mostly;
int percpu_pagelist_fraction;
gfp_t gfp_allowed_mask __read_mostly = GFP_BOOT_MASK;
#ifdef CONFIG_PM_SLEEP
/*
* The following functions are used by the suspend/hibernate code to temporarily
* change gfp_allowed_mask in order to avoid using I/O during memory allocations
* while devices are suspended. To avoid races with the suspend/hibernate code,
* they should always be called with pm_mutex held (gfp_allowed_mask also should
* 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)
{
WARN_ON(!mutex_is_locked(&pm_mutex));
gfp_allowed_mask = mask;
}
gfp_t clear_gfp_allowed_mask(gfp_t mask)
{
gfp_t ret = gfp_allowed_mask;
WARN_ON(!mutex_is_locked(&pm_mutex));
gfp_allowed_mask &= ~mask;
return ret;
}
#endif /* CONFIG_PM_SLEEP */
#ifdef CONFIG_HUGETLB_PAGE_SIZE_VARIABLE
int pageblock_order __read_mostly;
#endif