ACPI notifications are delivered in dedicated work contexts and may arrive simultaneously. In the following change, much work will be done while handling the notification, which could lead to potential race conditions. Introduce a new mutex to serialize keyboard backlight tracking to prevent potential race conditions. Acked-by: Ike Panhc Signed-off-by: Rong Zhang --- Changes in v6: - Protects ideapad_kbd_bl_led_cdev_brightness_set() as well Changes in v4: - Remove needless #include (Thanks Ilpo Järvinen) --- drivers/platform/x86/lenovo/ideapad-laptop.c | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/drivers/platform/x86/lenovo/ideapad-laptop.c b/drivers/platform/x86/lenovo/ideapad-laptop.c index 8213524504ee..684421ba53cb 100644 --- a/drivers/platform/x86/lenovo/ideapad-laptop.c +++ b/drivers/platform/x86/lenovo/ideapad-laptop.c @@ -26,6 +26,7 @@ #include #include #include +#include #include #include #include @@ -204,6 +205,7 @@ struct ideapad_private { int type; struct led_classdev led; unsigned int last_brightness; + struct mutex mutex; /* protects brightness tracking */ } kbd_bl; struct { bool initialized; @@ -1643,6 +1645,8 @@ static int ideapad_kbd_bl_brightness_set(struct ideapad_private *priv, unsigned unsigned long value; int type = priv->kbd_bl.type; + guard(mutex)(&priv->kbd_bl.mutex); + if (ideapad_kbd_bl_check_tristate(type)) { if (brightness > priv->kbd_bl.led.max_brightness) return -EINVAL; @@ -1678,6 +1682,8 @@ static void ideapad_kbd_bl_notify(struct ideapad_private *priv) if (!priv->kbd_bl.initialized) return; + guard(mutex)(&priv->kbd_bl.mutex); + brightness = ideapad_kbd_bl_brightness_get(priv); if (brightness < 0) return; @@ -1700,6 +1706,10 @@ static int ideapad_kbd_bl_init(struct ideapad_private *priv) if (WARN_ON(priv->kbd_bl.initialized)) return -EEXIST; + err = devm_mutex_init(&priv->platform_device->dev, &priv->kbd_bl.mutex); + if (err) + return err; + if (ideapad_kbd_bl_check_tristate(priv->kbd_bl.type)) priv->kbd_bl.led.max_brightness = 2; else -- 2.55.0