Skip to contentSkip to conversation

thermal: db8500: register the zone before requesting the interrupts

PATCH New Yaohan Li 1 participant 1 message Nov 17 Thermal

lore
Share & tools
lore

Yaohan Li

author
#0 lore raw

PATCH 1/1 thermal: db8500: register the zone before requesting the interrupts

Both threaded handlers call thermal_zone_device_update(th->tz, ...), but the thermal zone is registered last, after both devm_request_threaded_irq() calls.

That leaves two windows. During probe an interrupt can be served while th->tz is still NULL or holds an error pointer. On removal devres runs in reverse order, so the zone is unregistered and freed before the two IRQs are released, and a handler that the hardirq has already woken can then operate on a freed thermal_zone_device.

Register the zone first and request the interrupts afterwards, so devres releases the IRQs before the zone.

Stopping the PRCMU measurement from .remove() would not be enough on its
own: db8500_thermal_update_config(), which both handlers call, stops the sensing and starts it again, so a handler still running when .remove() returns restarts it. Register the stop as a devres action placed between the zone and the interrupts instead. It then runs after both handlers have been released and before the zone goes away, and it also covers a probe failure past that point.

---
Prepared against v7.4-rc1-1969-g666539eecdf4.

AI assistance: the analysis, the change and its review notes were prepared with Claude (Anthropic) as a coding assistant. The author reviewed the reasoning and takes responsibility for the content.

This revision registers the thermal zone before the interrupts and puts the PRCMU stop into a devres action placed between them, so devres releases both handlers first, then stops the measurement, then unregisters the zone. A stop issued from .remove() alone would be undone by a handler still running db8500_thermal_update_config(), which stops the sensing and starts it again.

Testing: compiled for ARCH=arm with u8500_defconfig (gcc 11.4, arm-linux-gnueabi). No Ux500 hardware was available, so neither the normal removal nor the second-IRQ-failure path was exercised.

---
 drivers/thermal/db8500_thermal.c | 30 ++++++++++++++++++++++--------
 1 file changed, 22 insertions(+), 8 deletions(-)
drivers/thermal/db8500_thermal.c +22 -8
index 48981247763f..b8f75838cc4d 100644
@@ -148,6 +148,11 @@ static irqreturn_t prcmu_high_irq_handler(int irq, void *irq_data)
 	return IRQ_HANDLED;
 }
 
+static void db8500_thermal_stop_action(void *data)
+{
+	db8500_prcmu_stop_temp_sense();
+}
+
 static int db8500_thermal_probe(struct platform_device *pdev)
 {
 	struct db8500_thermal_zone *th = NULL;
@@ -160,6 +165,23 @@ static int db8500_thermal_probe(struct platform_device *pdev)
 
 	th->dev = dev;
 
+	/* register of thermal sensor and get info from DT */
+	th->tz = devm_thermal_of_zone_register(dev, 0, th, &thdev_ops);
+	if (IS_ERR(th->tz)) {
+		dev_err(dev, "register thermal zone sensor failed\n");
+		return PTR_ERR(th->tz);
+	}
+	dev_info(dev, "thermal zone sensor registered\n");
+
+	/*
+	 * Registered after the zone and before the interrupts, so devres runs
+	 * it after both handlers have been released: a handler cannot restart
+	 * the measurement behind our back.
+	 */
+	ret = devm_add_action_or_reset(dev, db8500_thermal_stop_action, th);
+	if (ret)
+		return ret;
+
 	low_irq = platform_get_irq_byname(pdev, "IRQ_HOTMON_LOW");
 	if (low_irq < 0)
 		return low_irq;
@@ -180,14 +202,6 @@ static int db8500_thermal_probe(struct platform_device *pdev)
 	if (ret < 0)
 		return ret;
 
-	/* register of thermal sensor and get info from DT */
-	th->tz = devm_thermal_of_zone_register(dev, 0, th, &thdev_ops);
-	if (IS_ERR(th->tz)) {
-		dev_err(dev, "register thermal zone sensor failed\n");
-		return PTR_ERR(th->tz);
-	}
-	dev_info(dev, "thermal zone sensor registered\n");
-
 	/* Start measuring at the lowest point */
 	db8500_thermal_update_config(th, 0, PRCMU_DEFAULT_LOW_TEMP,
 				     db8500_thermal_points[0]);
Signature · 1 line
2.34.1