Skip to content

Commit 1bfcf13

Browse files
rjwysockitorvalds
authored andcommitted
pm: rework disabling of user mode helpers during suspend/hibernation
We currently use a PM notifier to disable user mode helpers before suspend and hibernation and to re-enable them during resume. However, this is not an ideal solution, because if any drivers want to upload firmware into memory before suspend, they have to use a PM notifier for this purpose and there is no guarantee that the ordering of PM notifiers will be as expected (ie. the notifier that disables user mode helpers has to be run after the driver's notifier used for uploading the firmware). For this reason, it seems better to move the disabling and enabling of user mode helpers to separate functions that will be called by the PM core as necessary. [akpm@linux-foundation.org: remove unneeded ifdefs] Signed-off-by: Rafael J. Wysocki <rjw@sisk.pl> Cc: Alan Stern <stern@rowland.harvard.edu> Acked-by: Pavel Machek <pavel@suse.cz> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 574f34c commit 1bfcf13

5 files changed

Lines changed: 58 additions & 38 deletions

File tree

‎include/linux/kmod.h‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,4 +99,7 @@ struct file;
9999
extern int call_usermodehelper_pipe(char *path, char *argv[], char *envp[],
100100
struct file **filp);
101101

102+
extern int usermodehelper_disable(void);
103+
extern void usermodehelper_enable(void);
104+
102105
#endif /* __LINUX_KMOD_H__ */

‎kernel/kmod.c‎

Lines changed: 28 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ static void __call_usermodehelper(struct work_struct *work)
265265
}
266266
}
267267

268-
#ifdef CONFIG_PM
268+
#ifdef CONFIG_PM_SLEEP
269269
/*
270270
* If set, call_usermodehelper_exec() will exit immediately returning -EBUSY
271271
* (used for preventing user land processes from being created after the user
@@ -288,39 +288,37 @@ static DECLARE_WAIT_QUEUE_HEAD(running_helpers_waitq);
288288
*/
289289
#define RUNNING_HELPERS_TIMEOUT (5 * HZ)
290290

291-
static int usermodehelper_pm_callback(struct notifier_block *nfb,
292-
unsigned long action,
293-
void *ignored)
291+
/**
292+
* usermodehelper_disable - prevent new helpers from being started
293+
*/
294+
int usermodehelper_disable(void)
294295
{
295296
long retval;
296297

297-
switch (action) {
298-
case PM_HIBERNATION_PREPARE:
299-
case PM_SUSPEND_PREPARE:
300-
usermodehelper_disabled = 1;
301-
smp_mb();
302-
/*
303-
* From now on call_usermodehelper_exec() won't start any new
304-
* helpers, so it is sufficient if running_helpers turns out to
305-
* be zero at one point (it may be increased later, but that
306-
* doesn't matter).
307-
*/
308-
retval = wait_event_timeout(running_helpers_waitq,
298+
usermodehelper_disabled = 1;
299+
smp_mb();
300+
/*
301+
* From now on call_usermodehelper_exec() won't start any new
302+
* helpers, so it is sufficient if running_helpers turns out to
303+
* be zero at one point (it may be increased later, but that
304+
* doesn't matter).
305+
*/
306+
retval = wait_event_timeout(running_helpers_waitq,
309307
atomic_read(&running_helpers) == 0,
310308
RUNNING_HELPERS_TIMEOUT);
311-
if (retval) {
312-
return NOTIFY_OK;
313-
} else {
314-
usermodehelper_disabled = 0;
315-
return NOTIFY_BAD;
316-
}
317-
case PM_POST_HIBERNATION:
318-
case PM_POST_SUSPEND:
319-
usermodehelper_disabled = 0;
320-
return NOTIFY_OK;
321-
}
309+
if (retval)
310+
return 0;
322311

323-
return NOTIFY_DONE;
312+
usermodehelper_disabled = 0;
313+
return -EAGAIN;
314+
}
315+
316+
/**
317+
* usermodehelper_enable - allow new helpers to be started again
318+
*/
319+
void usermodehelper_enable(void)
320+
{
321+
usermodehelper_disabled = 0;
324322
}
325323

326324
static void helper_lock(void)
@@ -334,18 +332,12 @@ static void helper_unlock(void)
334332
if (atomic_dec_and_test(&running_helpers))
335333
wake_up(&running_helpers_waitq);
336334
}
337-
338-
static void register_pm_notifier_callback(void)
339-
{
340-
pm_notifier(usermodehelper_pm_callback, 0);
341-
}
342-
#else /* CONFIG_PM */
335+
#else /* CONFIG_PM_SLEEP */
343336
#define usermodehelper_disabled 0
344337

345338
static inline void helper_lock(void) {}
346339
static inline void helper_unlock(void) {}
347-
static inline void register_pm_notifier_callback(void) {}
348-
#endif /* CONFIG_PM */
340+
#endif /* CONFIG_PM_SLEEP */
349341

350342
/**
351343
* call_usermodehelper_setup - prepare to call a usermode helper
@@ -515,5 +507,4 @@ void __init usermodehelper_init(void)
515507
{
516508
khelper_wq = create_singlethread_workqueue("khelper");
517509
BUG_ON(!khelper_wq);
518-
register_pm_notifier_callback();
519510
}

‎kernel/power/disk.c‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include <linux/reboot.h>
1515
#include <linux/string.h>
1616
#include <linux/device.h>
17+
#include <linux/kmod.h>
1718
#include <linux/delay.h>
1819
#include <linux/fs.h>
1920
#include <linux/mount.h>
@@ -520,6 +521,10 @@ int hibernate(void)
520521
if (error)
521522
goto Exit;
522523

524+
error = usermodehelper_disable();
525+
if (error)
526+
goto Exit;
527+
523528
/* Allocate memory management structures */
524529
error = create_basic_memory_bitmaps();
525530
if (error)
@@ -558,6 +563,7 @@ int hibernate(void)
558563
thaw_processes();
559564
Finish:
560565
free_basic_memory_bitmaps();
566+
usermodehelper_enable();
561567
Exit:
562568
pm_notifier_call_chain(PM_POST_HIBERNATION);
563569
pm_restore_console();
@@ -634,6 +640,10 @@ static int software_resume(void)
634640
if (error)
635641
goto Finish;
636642

643+
error = usermodehelper_disable();
644+
if (error)
645+
goto Finish;
646+
637647
error = create_basic_memory_bitmaps();
638648
if (error)
639649
goto Finish;
@@ -656,6 +666,7 @@ static int software_resume(void)
656666
thaw_processes();
657667
Done:
658668
free_basic_memory_bitmaps();
669+
usermodehelper_enable();
659670
Finish:
660671
pm_notifier_call_chain(PM_POST_RESTORE);
661672
pm_restore_console();

‎kernel/power/main.c‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include <linux/string.h>
1515
#include <linux/delay.h>
1616
#include <linux/errno.h>
17+
#include <linux/kmod.h>
1718
#include <linux/init.h>
1819
#include <linux/console.h>
1920
#include <linux/cpu.h>
@@ -237,6 +238,10 @@ static int suspend_prepare(void)
237238
if (error)
238239
goto Finish;
239240

241+
error = usermodehelper_disable();
242+
if (error)
243+
goto Finish;
244+
240245
if (suspend_freeze_processes()) {
241246
error = -EAGAIN;
242247
goto Thaw;
@@ -256,6 +261,7 @@ static int suspend_prepare(void)
256261

257262
Thaw:
258263
suspend_thaw_processes();
264+
usermodehelper_enable();
259265
Finish:
260266
pm_notifier_call_chain(PM_POST_SUSPEND);
261267
pm_restore_console();
@@ -376,6 +382,7 @@ int suspend_devices_and_enter(suspend_state_t state)
376382
static void suspend_finish(void)
377383
{
378384
suspend_thaw_processes();
385+
usermodehelper_enable();
379386
pm_notifier_call_chain(PM_POST_SUSPEND);
380387
pm_restore_console();
381388
}

‎kernel/power/user.c‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,13 +212,20 @@ static long snapshot_ioctl(struct file *filp, unsigned int cmd,
212212
case SNAPSHOT_FREEZE:
213213
if (data->frozen)
214214
break;
215+
215216
printk("Syncing filesystems ... ");
216217
sys_sync();
217218
printk("done.\n");
218219

219-
error = freeze_processes();
220+
error = usermodehelper_disable();
220221
if (error)
222+
break;
223+
224+
error = freeze_processes();
225+
if (error) {
221226
thaw_processes();
227+
usermodehelper_enable();
228+
}
222229
if (!error)
223230
data->frozen = 1;
224231
break;
@@ -227,6 +234,7 @@ static long snapshot_ioctl(struct file *filp, unsigned int cmd,
227234
if (!data->frozen || data->ready)
228235
break;
229236
thaw_processes();
237+
usermodehelper_enable();
230238
data->frozen = 0;
231239
break;
232240

0 commit comments

Comments
 (0)