Commit b298d289 authored by Srivatsa S. Bhat's avatar Srivatsa S. Bhat Committed by Rafael J. Wysocki

PM / Sleep: Fix freezer failures due to racy usermodehelper_is_disabled()

Commit a144c6a6 (PM: Print a warning if firmware is requested when tasks
are frozen) introduced usermodehelper_is_disabled() to warn and exit
immediately if firmware is requested when usermodehelpers are disabled.

However, it is racy. Consider the following scenario, currently used in
drivers/base/firmware_class.c:

...
if (usermodehelper_is_disabled())
        goto out;

/* Do actual work */
...

out:
        return err;

Nothing prevents someone from disabling usermodehelpers just after the check
in the 'if' condition, which means that it is quite possible to try doing the
"actual work" with usermodehelpers disabled, leading to undesirable
consequences.

In particular, this race condition in _request_firmware() causes task freezing
failures whenever suspend/hibernation is in progress because, it wrongly waits
to get the firmware/microcode image from userspace when actually the
usermodehelpers are disabled or userspace has been frozen.
Some of the example scenarios that cause freezing failures due to this race
are those that depend on userspace via request_firmware(), such as x86
microcode module initialization and microcode image reload.

Previous discussions about this issue can be found at:
http://thread.gmane.org/gmane.linux.kernel/1198291/focus=1200591

This patch adds proper synchronization to fix this issue.

It is to be noted that this patchset fixes the freezing failures but doesn't
remove the warnings. IOW, it does not attempt to add explicit synchronization
to x86 microcode driver to avoid requesting microcode image at inopportune
moments. Because, the warnings were introduced to highlight such cases, in the
first place. And we need not silence the warnings, since we take care of the
*real* problem (freezing failure) and hence, after that, the warnings are
pretty harmless anyway.
Signed-off-by: default avatarSrivatsa S. Bhat <srivatsa.bhat@linux.vnet.ibm.com>
Signed-off-by: default avatarRafael J. Wysocki <rjw@sisk.pl>
parent cba3176e
...@@ -534,6 +534,8 @@ static int _request_firmware(const struct firmware **firmware_p, ...@@ -534,6 +534,8 @@ static int _request_firmware(const struct firmware **firmware_p,
return 0; return 0;
} }
read_lock_usermodehelper();
if (WARN_ON(usermodehelper_is_disabled())) { if (WARN_ON(usermodehelper_is_disabled())) {
dev_err(device, "firmware: %s will not be loaded\n", name); dev_err(device, "firmware: %s will not be loaded\n", name);
retval = -EBUSY; retval = -EBUSY;
...@@ -572,6 +574,8 @@ static int _request_firmware(const struct firmware **firmware_p, ...@@ -572,6 +574,8 @@ static int _request_firmware(const struct firmware **firmware_p,
fw_destroy_instance(fw_priv); fw_destroy_instance(fw_priv);
out: out:
read_unlock_usermodehelper();
if (retval) { if (retval) {
release_firmware(firmware); release_firmware(firmware);
*firmware_p = NULL; *firmware_p = NULL;
......
...@@ -117,5 +117,7 @@ extern void usermodehelper_init(void); ...@@ -117,5 +117,7 @@ extern void usermodehelper_init(void);
extern int usermodehelper_disable(void); extern int usermodehelper_disable(void);
extern void usermodehelper_enable(void); extern void usermodehelper_enable(void);
extern bool usermodehelper_is_disabled(void); extern bool usermodehelper_is_disabled(void);
extern void read_lock_usermodehelper(void);
extern void read_unlock_usermodehelper(void);
#endif /* __LINUX_KMOD_H__ */ #endif /* __LINUX_KMOD_H__ */
...@@ -36,6 +36,7 @@ ...@@ -36,6 +36,7 @@
#include <linux/resource.h> #include <linux/resource.h>
#include <linux/notifier.h> #include <linux/notifier.h>
#include <linux/suspend.h> #include <linux/suspend.h>
#include <linux/rwsem.h>
#include <asm/uaccess.h> #include <asm/uaccess.h>
#include <trace/events/module.h> #include <trace/events/module.h>
...@@ -50,6 +51,7 @@ static struct workqueue_struct *khelper_wq; ...@@ -50,6 +51,7 @@ static struct workqueue_struct *khelper_wq;
static kernel_cap_t usermodehelper_bset = CAP_FULL_SET; static kernel_cap_t usermodehelper_bset = CAP_FULL_SET;
static kernel_cap_t usermodehelper_inheritable = CAP_FULL_SET; static kernel_cap_t usermodehelper_inheritable = CAP_FULL_SET;
static DEFINE_SPINLOCK(umh_sysctl_lock); static DEFINE_SPINLOCK(umh_sysctl_lock);
static DECLARE_RWSEM(umhelper_sem);
#ifdef CONFIG_MODULES #ifdef CONFIG_MODULES
...@@ -275,6 +277,7 @@ static void __call_usermodehelper(struct work_struct *work) ...@@ -275,6 +277,7 @@ static void __call_usermodehelper(struct work_struct *work)
* If set, call_usermodehelper_exec() will exit immediately returning -EBUSY * If set, call_usermodehelper_exec() will exit immediately returning -EBUSY
* (used for preventing user land processes from being created after the user * (used for preventing user land processes from being created after the user
* land has been frozen during a system-wide hibernation or suspend operation). * land has been frozen during a system-wide hibernation or suspend operation).
* Should always be manipulated under umhelper_sem acquired for write.
*/ */
static int usermodehelper_disabled = 1; static int usermodehelper_disabled = 1;
...@@ -293,6 +296,18 @@ static DECLARE_WAIT_QUEUE_HEAD(running_helpers_waitq); ...@@ -293,6 +296,18 @@ static DECLARE_WAIT_QUEUE_HEAD(running_helpers_waitq);
*/ */
#define RUNNING_HELPERS_TIMEOUT (5 * HZ) #define RUNNING_HELPERS_TIMEOUT (5 * HZ)
void read_lock_usermodehelper(void)
{
down_read(&umhelper_sem);
}
EXPORT_SYMBOL_GPL(read_lock_usermodehelper);
void read_unlock_usermodehelper(void)
{
up_read(&umhelper_sem);
}
EXPORT_SYMBOL_GPL(read_unlock_usermodehelper);
/** /**
* usermodehelper_disable - prevent new helpers from being started * usermodehelper_disable - prevent new helpers from being started
*/ */
...@@ -300,8 +315,10 @@ int usermodehelper_disable(void) ...@@ -300,8 +315,10 @@ int usermodehelper_disable(void)
{ {
long retval; long retval;
down_write(&umhelper_sem);
usermodehelper_disabled = 1; usermodehelper_disabled = 1;
smp_mb(); up_write(&umhelper_sem);
/* /*
* From now on call_usermodehelper_exec() won't start any new * From now on call_usermodehelper_exec() won't start any new
* helpers, so it is sufficient if running_helpers turns out to * helpers, so it is sufficient if running_helpers turns out to
...@@ -314,7 +331,9 @@ int usermodehelper_disable(void) ...@@ -314,7 +331,9 @@ int usermodehelper_disable(void)
if (retval) if (retval)
return 0; return 0;
down_write(&umhelper_sem);
usermodehelper_disabled = 0; usermodehelper_disabled = 0;
up_write(&umhelper_sem);
return -EAGAIN; return -EAGAIN;
} }
...@@ -323,7 +342,9 @@ int usermodehelper_disable(void) ...@@ -323,7 +342,9 @@ int usermodehelper_disable(void)
*/ */
void usermodehelper_enable(void) void usermodehelper_enable(void)
{ {
down_write(&umhelper_sem);
usermodehelper_disabled = 0; usermodehelper_disabled = 0;
up_write(&umhelper_sem);
} }
/** /**
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment