Skip to content

Commit 2aede85

Browse files
committed
PM / Hibernate: Freeze kernel threads after preallocating memory
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>
1 parent 8f88893 commit 2aede85

5 files changed

Lines changed: 33 additions & 20 deletions

File tree

‎Documentation/power/devices.txt‎

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -281,10 +281,6 @@ When the system goes into the standby or memory sleep state, the phases are:
281281
time.) Unlike the other suspend-related phases, during the prepare
282282
phase the device tree is traversed top-down.
283283

284-
In addition to that, if device drivers need to allocate additional
285-
memory to be able to hadle device suspend correctly, that should be
286-
done in the prepare phase.
287-
288284
After the prepare callback method returns, no new children may be
289285
registered below the device. The method may also prepare the device or
290286
driver in some way for the upcoming system power transition (for

‎include/linux/freezer.h‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ extern int thaw_process(struct task_struct *p);
4949

5050
extern void refrigerator(void);
5151
extern int freeze_processes(void);
52+
extern int freeze_kernel_threads(void);
5253
extern void thaw_processes(void);
5354

5455
static inline int try_to_freeze(void)
@@ -171,7 +172,8 @@ static inline void clear_freeze_flag(struct task_struct *p) {}
171172
static inline int thaw_process(struct task_struct *p) { return 1; }
172173

173174
static inline void refrigerator(void) {}
174-
static inline int freeze_processes(void) { BUG(); return 0; }
175+
static inline int freeze_processes(void) { return -ENOSYS; }
176+
static inline int freeze_kernel_threads(void) { return -ENOSYS; }
175177
static inline void thaw_processes(void) {}
176178

177179
static inline int try_to_freeze(void) { return 0; }

‎kernel/power/hibernate.c‎

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -334,12 +334,16 @@ int hibernation_snapshot(int platform_mode)
334334
if (error)
335335
goto Close;
336336

337-
error = dpm_prepare(PMSG_FREEZE);
338-
if (error)
339-
goto Complete_devices;
340-
341337
/* Preallocate image memory before shutting down devices. */
342338
error = hibernate_preallocate_memory();
339+
if (error)
340+
goto Close;
341+
342+
error = freeze_kernel_threads();
343+
if (error)
344+
goto Close;
345+
346+
error = dpm_prepare(PMSG_FREEZE);
343347
if (error)
344348
goto Complete_devices;
345349

‎kernel/power/power.h‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,8 @@ extern int pm_test_level;
228228
#ifdef CONFIG_SUSPEND_FREEZER
229229
static inline int suspend_freeze_processes(void)
230230
{
231-
return freeze_processes();
231+
int error = freeze_processes();
232+
return error ? : freeze_kernel_threads();
232233
}
233234

234235
static inline void suspend_thaw_processes(void)

‎kernel/power/process.c‎

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -135,28 +135,38 @@ static int try_to_freeze_tasks(bool sig_only)
135135
}
136136

137137
/**
138-
* freeze_processes - tell processes to enter the refrigerator
138+
* freeze_processes - Signal user space processes to enter the refrigerator.
139139
*/
140140
int freeze_processes(void)
141141
{
142142
int error;
143143

144144
printk("Freezing user space processes ... ");
145145
error = try_to_freeze_tasks(true);
146-
if (error)
147-
goto Exit;
148-
printk("done.\n");
146+
if (!error) {
147+
printk("done.");
148+
oom_killer_disable();
149+
}
150+
printk("\n");
151+
BUG_ON(in_atomic());
152+
153+
return error;
154+
}
155+
156+
/**
157+
* freeze_kernel_threads - Make freezable kernel threads go to the refrigerator.
158+
*/
159+
int freeze_kernel_threads(void)
160+
{
161+
int error;
149162

150163
printk("Freezing remaining freezable tasks ... ");
151164
error = try_to_freeze_tasks(false);
152-
if (error)
153-
goto Exit;
154-
printk("done.");
165+
if (!error)
166+
printk("done.");
155167

156-
oom_killer_disable();
157-
Exit:
158-
BUG_ON(in_atomic());
159168
printk("\n");
169+
BUG_ON(in_atomic());
160170

161171
return error;
162172
}

0 commit comments

Comments
 (0)