Skip to contentSkip to conversation

extcon: cancel wq_detcable delayed work on driver unbind

PATCH In review Tristan Madani 2 participants 7 messages Oct 20 – Nov 4

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

cover extcon: cancel wq_detcable delayed work on driver unbind

From: Tristan Madani <[email protected]>

Several extcon MUIC drivers schedule a delayed work (wq_detcable) during probe for post-boot cable state detection, but never cancel it on driver unbind. If the device is removed before the work fires, the callback dereferences freed memory.

Use devm_delayed_work_autocancel() to tie the work lifecycle to the device. This matches the pattern already used by extcon-gpio, extcon-palmas, extcon-qcom-spmi-misc, and extcon-intel-int3496.

For the Maxim drivers (max14577, max77693, max8997), irq_work was already converted to devm_work_autocancel() but wq_detcable was missed. The rt8973a and sm5502 drivers also need the devm-helpers.h include.

Tristan Madani (5):
extcon: max14577: cancel wq_detcable on driver unbind
extcon: max77693: cancel wq_detcable on driver unbind
extcon: max8997: cancel wq_detcable on driver unbind
extcon: rt8973a: cancel wq_detcable on driver unbind
extcon: sm5502: cancel wq_detcable on driver unbind

 drivers/extcon/extcon-max14577.c | 5 ++++-
 drivers/extcon/extcon-max77693.c | 5 ++++-
 drivers/extcon/extcon-max8997.c  | 5 ++++-
 drivers/extcon/extcon-rt8973a.c  | 6 +++++-
 drivers/extcon/extcon-sm5502.c   | 6 +++++-
 5 files changed, 22 insertions(+), 5 deletions(-)
Signature · 1 line
2.47.3
#1 lore raw

PATCH 1/5 extcon: max14577: cancel wq_detcable on driver unbind

From: Tristan Madani <[email protected]>

The driver schedules wq_detcable as a delayed work during probe but never cancels it. If the device is unbound before the work fires, the callback dereferences freed memory because the driver data is managed by devm.

Use devm_delayed_work_autocancel() so the work is automatically cancelled when the device is removed, matching the existing irq_work conversion done in the same driver.

Fixes 962e56bfcf0b ("extcon: max14577: Add extcon-max14577 driver to support MUIC device")
---
 drivers/extcon/extcon-max14577.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
drivers/extcon/extcon-max14577.c +4 -1
index 5476f48ed74b9..905722ede7ab1 100644
@@ -763,7 +763,10 @@ static int max14577_muic_probe(struct platform_device *pdev)
 	 * After completing the booting of platform, the extcon provider
 	 * driver should notify cable state to upper layer.
 	 */
-	INIT_DELAYED_WORK(&info->wq_detcable, max14577_muic_detect_cable_wq);
+	ret = devm_delayed_work_autocancel(&pdev->dev, &info->wq_detcable,
+					   max14577_muic_detect_cable_wq);
+	if (ret)
+		return ret;
 	queue_delayed_work(system_power_efficient_wq, &info->wq_detcable,
 			delay_jiffies);
 
Signature · 1 line
2.47.3
Tested-by on v1 1/5
#2 lore raw

Re: [PATCH 1/5] extcon: max14577: cancel wq_detcable on driver unbind

Hello Tristan,

We independently reproduced the max14577 delayed-work use-after-free and tested your [PATCH 1/5] ("extcon: max14577: cancel wq_detcable on driver unbind") on a clean Linux v7.4-rc1 tree (6628e7d4c6b2).

Test setup:
- QEMU/KVM with KASAN enabled
- Self-registered I2C adapter exercising the real max14577 MFD and extcon probe/remove paths
- wq_detcable scheduled with the driver's 17-second delay
- Device unregistered before the delayed work would run

Results:
- Unpatched v7.4-rc1: 80 slab-use-after-free reports, starting when the delayed-work timer expires
- Your [1/5] applied to the clean tree: 0 KASAN reports, with the test running more than four seconds beyond the original expiry point
- Control run without early unbind: 0 KASAN reports

This test applied your posted patch rather than only our own equivalent devm_delayed_work_autocancel() change. The result supports the fix for the tested driver-unbind sequence; the synthetic I2C setup has not been checked against physical hardware.

We can provide the complete console logs, configuration, and reproduction details privately if useful. The investigation was AI-assisted, with source analysis and test results reviewed by the human tester.

Best regards,
Yaohan Li

#3 lore raw

PATCH 2/5 extcon: max77693: cancel wq_detcable on driver unbind

In reply to Tristan Madani

From: Tristan Madani <[email protected]>

The driver schedules wq_detcable as a delayed work during probe but never cancels it. If the device is unbound before the work fires, the callback dereferences freed memory because the driver data is managed by devm.

Use devm_delayed_work_autocancel() so the work is automatically cancelled when the device is removed, matching the existing irq_work conversion done in the same driver.

Fixes 297620fd1e14 ("extcon: max77693: Check the state/type of cable after boot completed")
---
 drivers/extcon/extcon-max77693.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
drivers/extcon/extcon-max77693.c +4 -1
index 2c567e0b7b7f0..a76084e4d5512 100644
@@ -1251,7 +1251,10 @@ static int max77693_muic_probe(struct platform_device *pdev)
 	 * After completing the booting of platform, the extcon provider
 	 * driver should notify cable state to upper layer.
 	 */
-	INIT_DELAYED_WORK(&info->wq_detcable, max77693_muic_detect_cable_wq);
+	ret = devm_delayed_work_autocancel(&pdev->dev, &info->wq_detcable,
+					   max77693_muic_detect_cable_wq);
+	if (ret)
+		return ret;
 	queue_delayed_work(system_power_efficient_wq, &info->wq_detcable,
 			delay_jiffies);
 
Signature · 1 line
2.47.3
#4 lore raw

PATCH 3/5 extcon: max8997: cancel wq_detcable on driver unbind

In reply to Tristan Madani

From: Tristan Madani <[email protected]>

The driver schedules wq_detcable as a delayed work during probe but never cancels it. If the device is unbound before the work fires, the callback dereferences freed memory because the driver data is managed by devm.

Use devm_delayed_work_autocancel() so the work is automatically cancelled when the device is removed, matching the existing irq_work conversion done in the same driver.

Fixes af5eb1a13273 ("extcon: max8997: Use workqueue to check cable state after completing boot of platform")
---
 drivers/extcon/extcon-max8997.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
drivers/extcon/extcon-max8997.c +4 -1
index 9cddf08e06966..7048b9f6d8443 100644
@@ -754,7 +754,10 @@ static int max8997_muic_probe(struct platform_device *pdev)
 	 * After completing the booting of platform, the extcon provider
 	 * driver should notify cable state to upper layer.
 	 */
-	INIT_DELAYED_WORK(&info->wq_detcable, max8997_muic_detect_cable_wq);
+	ret = devm_delayed_work_autocancel(&pdev->dev, &info->wq_detcable,
+					   max8997_muic_detect_cable_wq);
+	if (ret)
+		return ret;
 	queue_delayed_work(system_power_efficient_wq, &info->wq_detcable,
 			delay_jiffies);
 
Signature · 1 line
2.47.3
#5 lore raw

PATCH 4/5 extcon: rt8973a: cancel wq_detcable on driver unbind

In reply to Tristan Madani

From: Tristan Madani <[email protected]>

The driver schedules wq_detcable as a delayed work during probe but the remove callback only tears down the regmap IRQ chip and never cancels it. If the device is unbound before the work fires, the callback dereferences freed memory because the driver data is managed by devm.

Use devm_delayed_work_autocancel() so the work is automatically cancelled when the device is removed.

Fixes c03e017c4d3d ("extcon: rt8973a: Add Richtek RT8973A extcon driver")
---
 drivers/extcon/extcon-rt8973a.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
drivers/extcon/extcon-rt8973a.c +5 -1
index 19bb49f13fb08..184e9f4c16ce3 100644
@@ -6,6 +6,7 @@
  * Author: Chanwoo Choi <[email protected]>
  */
 
+#include <linux/devm-helpers.h>
 #include <linux/err.h>
 #include <linux/i2c.h>
 #include <linux/input.h>
@@ -635,7 +636,10 @@ static int rt8973a_muic_i2c_probe(struct i2c_client *i2c)
 	 * After completing the booting of platform, the extcon provider
 	 * driver should notify cable state to upper layer.
 	 */
-	INIT_DELAYED_WORK(&info->wq_detcable, rt8973a_muic_detect_cable_wq);
+	ret = devm_delayed_work_autocancel(&i2c->dev, &info->wq_detcable,
+					   rt8973a_muic_detect_cable_wq);
+	if (ret)
+		return ret;
 	queue_delayed_work(system_power_efficient_wq, &info->wq_detcable,
 			msecs_to_jiffies(DELAY_MS_DEFAULT));
 
Signature · 1 line
2.47.3
#6 lore raw

PATCH 5/5 extcon: sm5502: cancel wq_detcable on driver unbind

In reply to Tristan Madani

From: Tristan Madani <[email protected]>

sm5502_muic_i2c_probe() schedules a delayed work (wq_detcable) for post-boot cable state detection but the driver has no .remove callback and never cancels it. If the device is unbound before the work fires, the callback dereferences freed memory.

Use devm_delayed_work_autocancel() so the work is automatically cancelled when the device is removed.

Fixes e1954452f500 ("extcon: sm5502: Detect cable state after completing platform booting")
---
 drivers/extcon/extcon-sm5502.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
drivers/extcon/extcon-sm5502.c +5 -1
index c8c4b9ef72aa1..5257cb3ca2266 100644
@@ -6,6 +6,7 @@
  * Author: Chanwoo Choi <[email protected]>
  */
 
+#include <linux/devm-helpers.h>
 #include <linux/err.h>
 #include <linux/i2c.h>
 #include <linux/interrupt.h>
@@ -763,7 +764,10 @@ static int sm5022_muic_i2c_probe(struct i2c_client *i2c)
 	 * After completing the booting of platform, the extcon provider
 	 * driver should notify cable state to upper layer.
 	 */
-	INIT_DELAYED_WORK(&info->wq_detcable, sm5502_muic_detect_cable_wq);
+	ret = devm_delayed_work_autocancel(&i2c->dev, &info->wq_detcable,
+					   sm5502_muic_detect_cable_wq);
+	if (ret)
+		return ret;
 	queue_delayed_work(system_power_efficient_wq, &info->wq_detcable,
 			msecs_to_jiffies(DELAY_MS_DEFAULT));
 
Signature · 1 line
2.47.3