Skip to contentSkip to conversation

platform/x86: lenovo: cancel fastcharger work on detach

PATCH In review Hongyan Xu 2 participants 2 messages Aug 6 – Nov 9

Subsystems (2)
lore
Share & tools
lore

Hongyan Xu

author
#0 lore raw

PATCH 1/1 platform/x86: lenovo: cancel fastcharger work on detach

The extcon notifier and probe queue yt2_1380_fc_worker(), which can sleep for several seconds while accessing managed pinctrl, GPIO and serdev resources. Driver detach does not drain this work before those resources are released, allowing the worker to use freed state.

Use devm_work_autocancel() between opening the managed serdev device and registering the managed notifier. Reverse devres release order then unregisters the notifier, cancels the work and only afterwards closes and releases the resources used by the worker.

Fixes b2ed33e8d486 ("platform/x86: Add lenovo-yoga-tab2-pro-1380-fastcharger driver")
---
 .../platform/x86/lenovo/yoga-tab2-pro-1380-fastcharger.c    | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
drivers/platform/x86/lenovo/yoga-tab2-pro-1380-fastcharger.c +5 -1
index 8551ab4d2c7d..cce677b5db48 100644
@@ -8,6 +8,7 @@
 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
 
 #include <linux/delay.h>
+#include <linux/devm-helpers.h>
 #include <linux/err.h>
 #include <linux/errno.h>
 #include <linux/extcon.h>
@@ -160,7 +161,6 @@ static int yt2_1380_fc_serdev_probe(struct serdev_device *serdev)
 
 	fc->dev = dev;
 	fc->nb.notifier_call = yt2_1380_fc_extcon_evt;
-	INIT_WORK(&fc->work, yt2_1380_fc_worker);
 
 	/*
 	 * Do this first since it may return -EPROBE_DEFER.
@@ -209,6 +209,10 @@ static int yt2_1380_fc_serdev_probe(struct serdev_device *serdev)
 	serdev_device_set_baudrate(serdev, 600);
 	serdev_device_set_flow_control(serdev, false);
 
+	ret = devm_work_autocancel(dev, &fc->work, yt2_1380_fc_worker);
+	if (ret)
+		return ret;
+
 	ret = devm_extcon_register_notifier_all(dev, fc->extcon, &fc->nb);
 	if (ret)
 		return dev_err_probe(dev, ret, "registering extcon notifier\n");
Signature · 1 line
2.50.1.windows.1
maintainer
#1 lore raw
Quoted 37 lines · Hongyan Xu

The extcon notifier and probe queue yt2_1380_fc_worker(), which can sleep for several seconds while accessing managed pinctrl, GPIO and serdev resources. Driver detach does not drain this work before those resources are released, allowing the worker to use freed state.

Use devm_work_autocancel() between opening the managed serdev device and registering the managed notifier. Reverse devres release order then unregisters the notifier, cancels the work and only afterwards closes and releases the resources used by the worker.

Fixes b2ed33e8d486 ("platform/x86: Add lenovo-yoga-tab2-pro-1380-fastcharger driver")
---
 .../platform/x86/lenovo/yoga-tab2-pro-1380-fastcharger.c    | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
drivers/platform/x86/lenovo/yoga-tab2-pro-1380-fastcharger.c +1 -1
index 8551ab4d2c7d..cce677b5db48 100644
@@ -8,6 +8,7 @@
 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
 
 #include <linux/delay.h>
+#include <linux/devm-helpers.h>
 #include <linux/err.h>
 #include <linux/errno.h>
 #include <linux/extcon.h>
@@ -160,7 +161,6 @@ static int yt2_1380_fc_serdev_probe(struct serdev_device *serdev)
 
 	fc->dev = dev;
 	fc->nb.notifier_call = yt2_1380_fc_extcon_evt;
-	INIT_WORK(&fc->work, yt2_1380_fc_worker);
 
 	/*
 	 * Do this first since it may return -EPROBE_DEFER.

drivers/platform/x86/lenovo/yoga-tab2-pro-1380-fastcharger.c static int yt2_1380_fc_serdev_probe(struct serdev_device *serdev) · line 218

Quoted 10 lines
drivers/platform/x86/lenovo/yoga-tab2-pro-1380-fastcharger.c +4 -0
@@ -209,6 +209,10 @@ static int yt2_1380_fc_serdev_probe(struct serdev_device *serdev)
 	serdev_device_set_baudrate(serdev, 600);
 	serdev_device_set_flow_control(serdev, false);
 
+	ret = devm_work_autocancel(dev, &fc->work, yt2_1380_fc_worker);
+	if (ret)
+		return ret;
+
 	ret = devm_extcon_register_notifier_all(dev, fc->extcon, &fc->nb);
 	if (ret)
 		return dev_err_probe(dev, ret, "registering extcon notifier\n");

While this looks an improvement from driver's perspective, sashiko is still worried extcon doesn't properly synchronize because it's using raw notifiers.

Signature · 1 line
 i.