Skip to contentSkip to conversation

hwmon: (sht15) Stop deferred work before releasing its resources

PATCH Superseded Myeonghun Pak 3 participants 4 messages Oct 16 – Oct 19 Hardware monitoring

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 hwmon: (sht15) Stop deferred work before releasing its resources

A measurement can return while read_work still accesses GPIOs or re-enables the IRQ. Regulator notifications can likewise leave update_supply_work using the regulator after it is released.

On removal and the applicable probe error paths, disable and drain read_work before freeing the IRQ. Unregister the regulator notifier before canceling update_supply_work.

This issue was identified as part of our ongoing research while reviewing kernel code.

Fixes 251eb40f5ccd ("hwmon: sht15 humidity sensor driver")
Assisted-by LLM

---
Compile-tested with GCC, W=1 (x86_64). No runtime testing.

 drivers/hwmon/sht15.c | 16 +++++++++++++---
 1 file changed, 13 insertions(+), 3 deletions(-)
drivers/hwmon/sht15.c +13 -3
index 7bb2105..6c5b2a1 100644
@@ -31,6 +31,7 @@
 #include <linux/bitrev.h>
 #include <linux/gpio/consumer.h>
 #include <linux/of.h>
+#include <linux/workqueue.h>
 
 /* Commands */
 #define SHT15_MEASURE_TEMP		0x03
@@ -986,15 +987,15 @@ static int sht15_probe(struct platform_device *pdev)
 	disable_irq_nosync(gpiod_to_irq(data->data));
 	ret = sht15_connection_reset(data);
 	if (ret)
-		goto err_release_reg;
+		goto err_release_irq;
 	ret = sht15_soft_reset(data);
 	if (ret)
-		goto err_release_reg;
+		goto err_release_irq;
 
 	ret = sysfs_create_group(&pdev->dev.kobj, &sht15_attr_group);
 	if (ret) {
 		dev_err(&pdev->dev, "sysfs create failed\n");
-		goto err_release_reg;
+		goto err_release_irq;
 	}
 
 	data->hwmon_dev = hwmon_device_register(data->dev);
@@ -1007,9 +1008,13 @@ static int sht15_probe(struct platform_device *pdev)
 
 err_release_sysfs_group:
 	sysfs_remove_group(&pdev->dev.kobj, &sht15_attr_group);
+err_release_irq:
+	disable_work_sync(&data->read_work);
+	devm_free_irq(&pdev->dev, gpiod_to_irq(data->data), data);
 err_release_reg:
 	if (!IS_ERR(data->reg)) {
 		regulator_unregister_notifier(data->reg, &data->nb);
+		cancel_work_sync(&data->update_supply_work);
 		regulator_disable(data->reg);
 	}
 	return ret;
@@ -1023,12 +1028,17 @@ static void sht15_remove(struct platform_device *pdev)
 	hwmon_device_unregister(data->hwmon_dev);
 	sysfs_remove_group(&pdev->dev.kobj, &sht15_attr_group);
 
+	/* The read work can re-enable the IRQ, so stop it before freeing IRQ. */
+	disable_work_sync(&data->read_work);
+	devm_free_irq(&pdev->dev, gpiod_to_irq(data->data), data);
+
 	ret = sht15_soft_reset(data);
 	if (ret)
 		dev_err(&pdev->dev, "Failed to reset device (%pe)\n", ERR_PTR(ret));
 
 	if (!IS_ERR(data->reg)) {
 		regulator_unregister_notifier(data->reg, &data->nb);
+		cancel_work_sync(&data->update_supply_work);
 		regulator_disable(data->reg);
 	}
 }
#1 lore raw
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 8 lines

A measurement can return while read_work still accesses GPIOs or re-enables the IRQ. Regulator notifications can likewise leave update_supply_work using the regulator after it is released.

On removal and the applicable probe error paths, disable and drain read_work before freeing the IRQ. Unregister the regulator notifier before canceling update_supply_work.

Quoted 8 lines

This issue was identified as part of our ongoing research while reviewing kernel code.

Fixes 251eb40f5ccd ("hwmon: sht15 humidity sensor driver")
Assisted-by LLM

Sashiko has reviewed this patch and found no issues. It looks great!

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
maintainer
#2 lore raw
In reply to Myeonghun Pak
Quoted 54 lines · Myeonghun Pak

A measurement can return while read_work still accesses GPIOs or re-enables the IRQ. Regulator notifications can likewise leave update_supply_work using the regulator after it is released.

On removal and the applicable probe error paths, disable and drain read_work before freeing the IRQ. Unregister the regulator notifier before canceling update_supply_work.

This issue was identified as part of our ongoing research while reviewing kernel code.

Fixes 251eb40f5ccd ("hwmon: sht15 humidity sensor driver")
Assisted-by LLM

---
Compile-tested with GCC, W=1 (x86_64). No runtime testing.

 drivers/hwmon/sht15.c | 16 +++++++++++++---
 1 file changed, 13 insertions(+), 3 deletions(-)
drivers/hwmon/sht15.c +4 -3
index 7bb2105..6c5b2a1 100644
@@ -31,6 +31,7 @@
 #include <linux/bitrev.h>
 #include <linux/gpio/consumer.h>
 #include <linux/of.h>
+#include <linux/workqueue.h>
 
 /* Commands */
 #define SHT15_MEASURE_TEMP		0x03
@@ -986,15 +987,15 @@ static int sht15_probe(struct platform_device *pdev)
 	disable_irq_nosync(gpiod_to_irq(data->data));
 	ret = sht15_connection_reset(data);
 	if (ret)
-		goto err_release_reg;
+		goto err_release_irq;
 	ret = sht15_soft_reset(data);
 	if (ret)
-		goto err_release_reg;
+		goto err_release_irq;
 
 	ret = sysfs_create_group(&pdev->dev.kobj, &sht15_attr_group);
 	if (ret) {
 		dev_err(&pdev->dev, "sysfs create failed\n");
-		goto err_release_reg;
+		goto err_release_irq;
 	}
 
 	data->hwmon_dev = hwmon_device_register(data->dev);

drivers/hwmon/sht15.c static int sht15_probe(struct platform_device *pdev) · line 1013

Quoted 6 lines
drivers/hwmon/sht15.c +3 -0
@@ -1007,9 +1008,13 @@ static int sht15_probe(struct platform_device *pdev)
 
 err_release_sysfs_group:
 	sysfs_remove_group(&pdev->dev.kobj, &sht15_attr_group);
+err_release_irq:
+	disable_work_sync(&data->read_work);
+	devm_free_irq(&pdev->dev, gpiod_to_irq(data->data), data);

With this here and in the remove function, what is the point of using devm_request_irq() ?

Guenter

Quoted 25 lines
drivers/hwmon/sht15.c +6 -0
 err_release_reg:
 	if (!IS_ERR(data->reg)) {
 		regulator_unregister_notifier(data->reg, &data->nb);
+		cancel_work_sync(&data->update_supply_work);
 		regulator_disable(data->reg);
 	}
 	return ret;
@@ -1023,12 +1028,17 @@ static void sht15_remove(struct platform_device *pdev)
 	hwmon_device_unregister(data->hwmon_dev);
 	sysfs_remove_group(&pdev->dev.kobj, &sht15_attr_group);
 
+	/* The read work can re-enable the IRQ, so stop it before freeing IRQ. */
+	disable_work_sync(&data->read_work);
+	devm_free_irq(&pdev->dev, gpiod_to_irq(data->data), data);
+
 	ret = sht15_soft_reset(data);
 	if (ret)
 		dev_err(&pdev->dev, "Failed to reset device (%pe)\n", ERR_PTR(ret));
 
 	if (!IS_ERR(data->reg)) {
 		regulator_unregister_notifier(data->reg, &data->nb);
+		cancel_work_sync(&data->update_supply_work);
 		regulator_disable(data->reg);
 	}
 }
#3 lore raw

Hi Guenter,

You're right. I've switched to request_irq()/free_irq() in v2, with read_work disabled and drained before freeing the IRQ.

Thanks for the review.

Best regards,
Myeonghun Pak

2026년 10월 18일 (일) 오전 11:57, Guenter Roeck <[email protected]>님이 작성:

Quoted 93 lines · Myeonghun Pak
Quoted 54 lines

A measurement can return while read_work still accesses GPIOs or re-enables the IRQ. Regulator notifications can likewise leave update_supply_work using the regulator after it is released.

On removal and the applicable probe error paths, disable and drain read_work before freeing the IRQ. Unregister the regulator notifier before canceling update_supply_work.

This issue was identified as part of our ongoing research while reviewing kernel code.

Fixes 251eb40f5ccd ("hwmon: sht15 humidity sensor driver")
Assisted-by LLM

---
Compile-tested with GCC, W=1 (x86_64). No runtime testing.

 drivers/hwmon/sht15.c | 16 +++++++++++++---
 1 file changed, 13 insertions(+), 3 deletions(-)
drivers/hwmon/sht15.c +4 -3
index 7bb2105..6c5b2a1 100644
@@ -31,6 +31,7 @@
 #include <linux/bitrev.h>
 #include <linux/gpio/consumer.h>
 #include <linux/of.h>
+#include <linux/workqueue.h>
 
 /* Commands */
 #define SHT15_MEASURE_TEMP           0x03
@@ -986,15 +987,15 @@ static int sht15_probe(struct platform_device *pdev)
      disable_irq_nosync(gpiod_to_irq(data->data));
      ret = sht15_connection_reset(data);
      if (ret)
-             goto err_release_reg;
+             goto err_release_irq;
      ret = sht15_soft_reset(data);
      if (ret)
-             goto err_release_reg;
+             goto err_release_irq;
 
      ret = sysfs_create_group(&pdev->dev.kobj, &sht15_attr_group);
      if (ret) {
              dev_err(&pdev->dev, "sysfs create failed\n");
-             goto err_release_reg;
+             goto err_release_irq;
      }
 
      data->hwmon_dev = hwmon_device_register(data->dev);

drivers/hwmon/sht15.c static int sht15_probe(struct platform_device *pdev) · line 1013

Quoted 6 lines
drivers/hwmon/sht15.c +3 -0
@@ -1007,9 +1008,13 @@ static int sht15_probe(struct platform_device *pdev)
 
 err_release_sysfs_group:
      sysfs_remove_group(&pdev->dev.kobj, &sht15_attr_group);
+err_release_irq:
+     disable_work_sync(&data->read_work);
+     devm_free_irq(&pdev->dev, gpiod_to_irq(data->data), data);

With this here and in the remove function, what is the point of using devm_request_irq() ?

Guenter

Quoted 25 lines
drivers/hwmon/sht15.c +6 -0
 err_release_reg:
      if (!IS_ERR(data->reg)) {
              regulator_unregister_notifier(data->reg, &data->nb);
+             cancel_work_sync(&data->update_supply_work);
              regulator_disable(data->reg);
      }
      return ret;
@@ -1023,12 +1028,17 @@ static void sht15_remove(struct platform_device *pdev)
      hwmon_device_unregister(data->hwmon_dev);
      sysfs_remove_group(&pdev->dev.kobj, &sht15_attr_group);
 
+     /* The read work can re-enable the IRQ, so stop it before freeing IRQ. */
+     disable_work_sync(&data->read_work);
+     devm_free_irq(&pdev->dev, gpiod_to_irq(data->data), data);
+
      ret = sht15_soft_reset(data);
      if (ret)
              dev_err(&pdev->dev, "Failed to reset device (%pe)\n", ERR_PTR(ret));
 
      if (!IS_ERR(data->reg)) {
              regulator_unregister_notifier(data->reg, &data->nb);
+             cancel_work_sync(&data->update_supply_work);
              regulator_disable(data->reg);
      }
 }