]> Pileus Git - ~andy/linux/commitdiff
PM / Hibernate: Freeze kernel threads after preallocating memory
authorRafael J. Wysocki <rjw@sisk.pl>
Mon, 26 Sep 2011 18:32:27 +0000 (20:32 +0200)
committerRafael J. Wysocki <rjw@sisk.pl>
Sun, 16 Oct 2011 21:28:52 +0000 (23:28 +0200)
There is a problem with the current ordering of hibernate code which
leads to deadlocks in some filesystems' memory shrinkers.  Namely,
some filesystems use freezable kernel threads that are inactive when
the hibernate memory preallocation is carried out.  Those same
filesystems use memory shrinkers that may be triggered by the
hibernate memory preallocation.  If those memory shrinkers wait for
the frozen kernel threads, the hibernate process deadlocks (this
happens with XFS, for one example).

Apparently, it is not technically viable to redesign the filesystems
in question to avoid the situation described above, so the only
possible solution of this issue is to defer the freezing of kernel
threads until the hibernate memory preallocation is done, which is
implemented by this change.

Unfortunately, this requires the memory preallocation to be done
before the "prepare" stage of device freeze, so after this change the
only way drivers can allocate additional memory for their freeze
routines in a clean way is to use PM notifiers.

Reported-by: Christoph <cr2005@u-club.de>
Signed-off-by: Rafael J. Wysocki <rjw@sisk.pl>
Documentation/power/devices.txt
include/linux/freezer.h
kernel/power/hibernate.c
kernel/power/power.h
kernel/power/process.c

index 29b7a9817f5aa6a5c45ea4989a15fd716571ef63..646a89e0c07d50c682912ef0200dc087363cd37f 100644 (file)
@@ -281,10 +281,6 @@ When the system goes into the standby or memory sleep state, the phases are:
        time.)  Unlike the other suspend-related phases, during the prepare
        phase the device tree is traversed top-down.
 
-       In addition to that, if device drivers need to allocate additional
-       memory to be able to hadle device suspend correctly, that should be
-       done in the prepare phase.
-
        After the prepare callback method returns, no new children may be
        registered below the device.  The method may also prepare the device or
        driver in some way for the upcoming system power transition (for
index 1effc8b56b4e44fa169b4538b64f22e53c8b8bef..aa56cf31f7ff747a4242d7ef55508622e65741ce 100644 (file)
@@ -49,6 +49,7 @@ extern int thaw_process(struct task_struct *p);
 
 extern void refrigerator(void);
 extern int freeze_processes(void);
+extern int freeze_kernel_threads(void);
 extern void thaw_processes(void);
 
 static inline int try_to_freeze(void)
@@ -171,7 +172,8 @@ static inline void clear_freeze_flag(struct task_struct *p) {}
 static inline int thaw_process(struct task_struct *p) { return 1; }
 
 static inline void refrigerator(void) {}
-static inline int freeze_processes(void) { BUG(); return 0; }
+static inline int freeze_processes(void) { return -ENOSYS; }
+static inline int freeze_kernel_threads(void) { return -ENOSYS; }
 static inline void thaw_processes(void) {}
 
 static inline int try_to_freeze(void) { return 0; }
index 8f7b1db1ece1b3273f9eebbc128d7c884390c638..3a20466015f88bafad538cf8af7b7248dd0432c9 100644 (file)
@@ -334,12 +334,16 @@ int hibernation_snapshot(int platform_mode)
        if (error)
                goto Close;
 
-       error = dpm_prepare(PMSG_FREEZE);
-       if (error)
-               goto Complete_devices;
-
        /* Preallocate image memory before shutting down devices. */
        error = hibernate_preallocate_memory();
+       if (error)
+               goto Close;
+
+       error = freeze_kernel_threads();
+       if (error)
+               goto Close;
+
+       error = dpm_prepare(PMSG_FREEZE);
        if (error)
                goto Complete_devices;
 
index 9a00a0a262806b11905a84fb85c5a26c7e99592b..e6206397ce67c89022ab6e2e750896623a23fdce 100644 (file)
@@ -228,7 +228,8 @@ extern int pm_test_level;
 #ifdef CONFIG_SUSPEND_FREEZER
 static inline int suspend_freeze_processes(void)
 {
-       return freeze_processes();
+       int error = freeze_processes();
+       return error ? : freeze_kernel_threads();
 }
 
 static inline void suspend_thaw_processes(void)
index 0cf3a27a6c9d53e32ca449ea6a350770f4caa039..addbbe5531bc42634c78844f5d93796526cb13ec 100644 (file)
@@ -135,7 +135,7 @@ static int try_to_freeze_tasks(bool sig_only)
 }
 
 /**
- *     freeze_processes - tell processes to enter the refrigerator
+ * freeze_processes - Signal user space processes to enter the refrigerator.
  */
 int freeze_processes(void)
 {
@@ -143,20 +143,30 @@ int freeze_processes(void)
 
        printk("Freezing user space processes ... ");
        error = try_to_freeze_tasks(true);
-       if (error)
-               goto Exit;
-       printk("done.\n");
+       if (!error) {
+               printk("done.");
+               oom_killer_disable();
+       }
+       printk("\n");
+       BUG_ON(in_atomic());
+
+       return error;
+}
+
+/**
+ * freeze_kernel_threads - Make freezable kernel threads go to the refrigerator.
+ */
+int freeze_kernel_threads(void)
+{
+       int error;
 
        printk("Freezing remaining freezable tasks ... ");
        error = try_to_freeze_tasks(false);
-       if (error)
-               goto Exit;
-       printk("done.");
+       if (!error)
+               printk("done.");
 
-       oom_killer_disable();
- Exit:
-       BUG_ON(in_atomic());
        printk("\n");
+       BUG_ON(in_atomic());
 
        return error;
 }