Skip to contentSkip to conversation

thermal/drivers/qcom/tsens: Release parent resources on probe failure

PATCH Applied Daniel Lezcano 3 participants 7 messages Oct 21 – Nov 5

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

PATCH 1/3 thermal/drivers/qcom/tsens: Release parent resources on probe failure

On legacy MSM8960/APQ8064 device trees without a separate TSENS child node, the Global Clock Controller (GCC) driver creates a qcom-tsens platform device without an OF node. TSENS consequently uses the parent device for firmware lookups and managed resource allocations.

If probing fails, including calibration returning -EPROBE_DEFER, the driver core releases only the child device's managed resources. Allocations attached to the parent remain, accumulating across repeated probe attempts.

Wrap the parent-bound allocations in a devres group and register a cleanup action on the TSENS child. This releases the resources on probe failure and unbind while preserving legacy firmware lookups. Keep the group handling in a probe wrapper so the existing initialization path remains self-contained.

Fixes 9066073c6c27 ("thermal: qcom: tsens: Add a skeletal TSENS drivers")
Cc Rajendra Nayak <[email protected]>
---
 drivers/thermal/qcom/tsens.c | 37 +++++++++++++++++++++++++++++++++++-
 1 file changed, 36 insertions(+), 1 deletion(-)
drivers/thermal/qcom/tsens.c +36 -1
index b5ec70201e2f..4f69ce4976d9 100644
@@ -1374,7 +1374,7 @@ static int tsens_register(struct tsens_priv *priv)
 	return ret;
 }
 
-static int tsens_probe(struct platform_device *pdev)
+static int tsens_probe_common(struct platform_device *pdev)
 {
 	int ret, i;
 	struct device *dev;
@@ -1452,6 +1452,41 @@ static int tsens_probe(struct platform_device *pdev)
 	return ret;
 }
 
+static void tsens_release_resources(void *data)
+{
+	struct device *dev = data;
+
+	devres_release_group(dev->parent, dev);
+}
+
+static int tsens_probe(struct platform_device *pdev)
+{
+	struct device *dev = &pdev->dev;
+	int ret;
+
+	/*
+	 * DT-backed devices own their managed resources directly. Legacy devices
+	 * created by the Global Clock Controller (GCC) driver have no OF node and
+	 * use the parent's firmware data and managed resources instead. Group
+	 * those resources so the child releases them on probe failure or unbind.
+	 */
+	if (dev->of_node)
+		return tsens_probe_common(pdev);
+
+	/* Tie resources allocated on the parent to the TSENS lifetime. */
+	if (!devres_open_group(dev->parent, dev, GFP_KERNEL))
+		return -ENOMEM;
+
+	ret = devm_add_action_or_reset(dev, tsens_release_resources, dev);
+	if (ret)
+		return ret;
+
+	ret = tsens_probe_common(pdev);
+	devres_close_group(dev->parent, dev);
+
+	return ret;
+}
+
 static void tsens_remove(struct platform_device *pdev)
 {
 	struct tsens_priv *priv = platform_get_drvdata(pdev);
Signature · 1 line
2.43.0
#1 lore raw

PATCH 2/3 thermal/drivers/qcom/tsens: Validate the sensor count before initialization

The #qcom,sensors property can override the platform sensor count without an upper-bound check. A count exceeding the IP's maximum causes the driver to access uninitialized regmap fields when handling interrupts. Platforms with an explicit hardware-ID table can also read beyond that table, even when the count remains within the IP's hardware limit.

Reject zero counts and counts exceeding the hardware maximum or, when present, the hardware-ID table length before allocating the private structure and assigning sensor IDs. Preserve the existing fallback to the platform default when the property cannot be read, and document both the fallback and the sensor-count limits.

Fixes 6d7c70d1cd65 ("thermal: qcom: tsens: Allow number of sensors to come from DT")
Cc Bjorn Andersson <[email protected]>
---
 drivers/thermal/qcom/tsens.c | 12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)
drivers/thermal/qcom/tsens.c +11 -1
index 4f69ce4976d9..9134637a3458 100644
@@ -1399,10 +1399,20 @@ static int tsens_probe_common(struct platform_device *pdev)
 
 	num_sensors = data->num_sensors;
 
+	/*
+	 * DT may override the platform sensor count. A missing, empty or too
+	 * short property leaves num_sensors unchanged, retaining the default.
+	 */
 	if (np)
 		of_property_read_u32(np, "#qcom,sensors", &num_sensors);
 
-	if (num_sensors <= 0) {
+	/*
+	 * Require at least one sensor and stay within the IP's hardware limit.
+	 * Platforms with an explicit hardware-ID table also limit the count to
+	 * its number of entries, which may be smaller than the hardware limit.
+	 */
+	if (!num_sensors || num_sensors > data->feat->max_sensors ||
+	    (data->hw_ids && num_sensors > data->num_sensors)) {
 		dev_err(dev, "%s: invalid number of sensors\n", __func__);
 		return -EINVAL;
 	}
Signature · 1 line
2.43.0
#2 lore raw

[thermal: thermal/linux-next] thermal/drivers/qcom/tsens: Validate the sensor count before initialization

The following commit has been merged into the thermal/linux-next branch of thermal:

The following commit has been merged into the thermal/linux-next branch of thermal:

Commit-ID:     ce7e968a95c4d78717443ec1286ed2b207c74dfd
Gitweb:        https://git.kernel.org/pub/scm/linux/kernel/git/thermal/linux.git//ce7e968a95c4d78717443ec1286ed2b207c74dfd
Author:        Daniel Lezcano <[email protected]>
AuthorDate:    Wed, 21 Oct 2026 10:57:47 +02:00
Committer:     Daniel Lezcano <[email protected]>
CommitterDate: Tue, 03 Nov 2026 19:48:08 +01:00

thermal/drivers/qcom/tsens: Validate the sensor count before initialization

The #qcom,sensors property can override the platform sensor count without an upper-bound check. A count exceeding the IP's maximum causes the driver to access uninitialized regmap fields when handling interrupts. Platforms with an explicit hardware-ID table can also read beyond that table, even when the count remains within the IP's hardware limit.

Reject zero counts and counts exceeding the hardware maximum or, when present, the hardware-ID table length before allocating the private structure and assigning sensor IDs. Preserve the existing fallback to the platform default when the property cannot be read, and document both the fallback and the sensor-count limits.

Fixes 6d7c70d1cd65 ("thermal: qcom: tsens: Allow number of sensors to come from DT")
Cc Bjorn Andersson <[email protected]>
---
 drivers/thermal/qcom/tsens.c | 12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)
drivers/thermal/qcom/tsens.c +11 -1
index 997a0db..4d4997b 100644
@@ -1402,10 +1402,20 @@ static int tsens_probe_common(struct platform_device *pdev)
 
 	num_sensors = data->num_sensors;
 
+	/*
+	 * DT may override the platform sensor count. A missing, empty or too
+	 * short property leaves num_sensors unchanged, retaining the default.
+	 */
 	if (np)
 		of_property_read_u32(np, "#qcom,sensors", &num_sensors);
 
-	if (num_sensors <= 0) {
+	/*
+	 * Require at least one sensor and stay within the IP's hardware limit.
+	 * Platforms with an explicit hardware-ID table also limit the count to
+	 * its number of entries, which may be smaller than the hardware limit.
+	 */
+	if (!num_sensors || num_sensors > data->feat->max_sensors ||
+	    (data->hw_ids && num_sensors > data->num_sensors)) {
 		dev_err(dev, "%s: invalid number of sensors\n", __func__);
 		return -EINVAL;
 	}
#3 lore raw

PATCH 3/3 thermal/drivers/qcom/tsens: Disable hardware on probe failure

In reply to Daniel Lezcano

Hardware initialization enables TSENS interrupts before calibration and thermal zone and IRQ registration have completed. If calibration or registration fails, probe returns without disabling the hardware. Devres then releases any registered IRQ handlers, leaving the interrupt source enabled without a handler.

Factor the hardware shutdown sequence out of the remove callback and invoke it when calibration or registration fails after successful initialization. Disable hardware interrupts and call the optional sensor disable callback before devres releases IRQ handlers and register maps.

Fixes 634e11d5b450 ("drivers: thermal: tsens: Add interrupt support")
Cc Amit Kucheria <[email protected]>
---
 drivers/thermal/qcom/tsens.c | 28 ++++++++++++++++++++--------
 1 file changed, 20 insertions(+), 8 deletions(-)
drivers/thermal/qcom/tsens.c +20 -8
index 9134637a3458..57d71159bd4b 100644
@@ -1374,6 +1374,13 @@ static int tsens_register(struct tsens_priv *priv)
 	return ret;
 }
 
+static void tsens_disable(struct tsens_priv *priv)
+{
+	tsens_disable_irq(priv);
+	if (priv->ops->disable)
+		priv->ops->disable(priv);
+}
+
 static int tsens_probe_common(struct platform_device *pdev)
 {
 	int ret, i;
@@ -1450,15 +1457,22 @@ static int tsens_probe_common(struct platform_device *pdev)
 
 	if (priv->ops->calibrate) {
 		ret = priv->ops->calibrate(priv);
-		if (ret < 0)
-			return dev_err_probe(dev, ret, "%s: calibration failed\n",
-					     __func__);
+		if (ret < 0) {
+			dev_err_probe(dev, ret, "%s: calibration failed\n", __func__);
+			goto err_disable;
+		}
 	}
 
 	ret = tsens_register(priv);
-	if (!ret)
-		tsens_debug_init(pdev);
+	if (ret)
+		goto err_disable;
+
+	tsens_debug_init(pdev);
+	return 0;
 
+err_disable:
+	/* Stop hardware events before devres releases IRQs and register maps. */
+	tsens_disable(priv);
 	return ret;
 }
 
@@ -1502,9 +1516,7 @@ static void tsens_remove(struct platform_device *pdev)
 	struct tsens_priv *priv = platform_get_drvdata(pdev);
 
 	debugfs_remove_recursive(priv->debug_root);
-	tsens_disable_irq(priv);
-	if (priv->ops->disable)
-		priv->ops->disable(priv);
+	tsens_disable(priv);
 }
 
 static struct platform_driver tsens_driver = {
Signature · 1 line
2.43.0
Acked-by on v1 3/3
#4 lore raw

Re: [PATCH 3/3] thermal/drivers/qcom/tsens: Disable hardware on probe failure

Quoted 11 lines · Daniel Lezcano

Hardware initialization enables TSENS interrupts before calibration and thermal zone and IRQ registration have completed. If calibration or registration fails, probe returns without disabling the hardware. Devres then releases any registered IRQ handlers, leaving the interrupt source enabled without a handler.

Factor the hardware shutdown sequence out of the remove callback and invoke it when calibration or registration fails after successful initialization. Disable hardware interrupts and call the optional sensor disable callback before devres releases IRQ handlers and register maps.

Quoted 5 lines
Fixes 634e11d5b450 ("drivers: thermal: tsens: Add interrupt support")
Cc Amit Kucheria <[email protected]>

---

Konrad

#5 lore raw

[thermal: thermal/linux-next] thermal/drivers/qcom/tsens: Disable hardware on probe failure

In reply to Daniel Lezcano
The following commit has been merged into the thermal/linux-next branch of thermal:

The following commit has been merged into the thermal/linux-next branch of thermal:

Commit-ID:     d7292b66872c7bb820ce64e27f60ff7cca6925b3
Gitweb:        https://git.kernel.org/pub/scm/linux/kernel/git/thermal/linux.git//d7292b66872c7bb820ce64e27f60ff7cca6925b3
Author:        Daniel Lezcano <[email protected]>
AuthorDate:    Wed, 21 Oct 2026 10:57:48 +02:00
Committer:     Daniel Lezcano <[email protected]>
CommitterDate: Tue, 03 Nov 2026 19:48:33 +01:00

thermal/drivers/qcom/tsens: Disable hardware on probe failure

Hardware initialization enables TSENS interrupts before calibration and thermal zone and IRQ registration have completed. If calibration or registration fails, probe returns without disabling the hardware. Devres then releases any registered IRQ handlers, leaving the interrupt source enabled without a handler.

Factor the hardware shutdown sequence out of the remove callback and invoke it when calibration or registration fails after successful initialization. Disable hardware interrupts and call the optional sensor disable callback before devres releases IRQ handlers and register maps.

---
 drivers/thermal/qcom/tsens.c | 28 ++++++++++++++++++++--------
 1 file changed, 20 insertions(+), 8 deletions(-)
drivers/thermal/qcom/tsens.c +20 -8
index 4d4997b..3e5a501 100644
@@ -1377,6 +1377,13 @@ static int tsens_register(struct tsens_priv *priv)
 	return ret;
 }
 
+static void tsens_disable(struct tsens_priv *priv)
+{
+	tsens_disable_irq(priv);
+	if (priv->ops->disable)
+		priv->ops->disable(priv);
+}
+
 static int tsens_probe_common(struct platform_device *pdev)
 {
 	int ret, i;
@@ -1453,15 +1460,22 @@ static int tsens_probe_common(struct platform_device *pdev)
 
 	if (priv->ops->calibrate) {
 		ret = priv->ops->calibrate(priv);
-		if (ret < 0)
-			return dev_err_probe(dev, ret, "%s: calibration failed\n",
-					     __func__);
+		if (ret < 0) {
+			dev_err_probe(dev, ret, "%s: calibration failed\n", __func__);
+			goto err_disable;
+		}
 	}
 
 	ret = tsens_register(priv);
-	if (!ret)
-		tsens_debug_init(pdev);
+	if (ret)
+		goto err_disable;
+
+	tsens_debug_init(pdev);
+	return 0;
 
+err_disable:
+	/* Stop hardware events before devres releases IRQs and register maps. */
+	tsens_disable(priv);
 	return ret;
 }
 
@@ -1505,9 +1519,7 @@ static void tsens_remove(struct platform_device *pdev)
 	struct tsens_priv *priv = platform_get_drvdata(pdev);
 
 	debugfs_remove_recursive(priv->debug_root);
-	tsens_disable_irq(priv);
-	if (priv->ops->disable)
-		priv->ops->disable(priv);
+	tsens_disable(priv);
 }
 
 static struct platform_driver tsens_driver = {
#6 lore raw
In reply to Daniel Lezcano
The following commit has been merged into the thermal/linux-next branch of thermal:

The following commit has been merged into the thermal/linux-next branch of thermal:

Commit-ID:     2fef2bb0ca32c937b848ba946d07662b1e08d036
Gitweb:        https://git.kernel.org/pub/scm/linux/kernel/git/thermal/linux.git//2fef2bb0ca32c937b848ba946d07662b1e08d036
Author:        Daniel Lezcano <[email protected]>
AuthorDate:    Wed, 21 Oct 2026 10:57:46 +02:00
Committer:     Daniel Lezcano <[email protected]>
CommitterDate: Tue, 03 Nov 2026 19:47:27 +01:00

thermal/drivers/qcom/tsens: Release parent resources on probe failure

On legacy MSM8960/APQ8064 device trees without a separate TSENS child node, the Global Clock Controller (GCC) driver creates a qcom-tsens platform device without an OF node. TSENS consequently uses the parent device for firmware lookups and managed resource allocations.

If probing fails, including calibration returning -EPROBE_DEFER, the driver core releases only the child device's managed resources. Allocations attached to the parent remain, accumulating across repeated probe attempts.

Wrap the parent-bound allocations in a devres group and register a cleanup action on the TSENS child. This releases the resources on probe failure and unbind while preserving legacy firmware lookups. Keep the group handling in a probe wrapper so the existing initialization path remains self-contained.

---
 drivers/thermal/qcom/tsens.c | 37 ++++++++++++++++++++++++++++++++++-
 1 file changed, 36 insertions(+), 1 deletion(-)
drivers/thermal/qcom/tsens.c +36 -1
index 4d7013f..997a0db 100644
@@ -1377,7 +1377,7 @@ static int tsens_register(struct tsens_priv *priv)
 	return ret;
 }
 
-static int tsens_probe(struct platform_device *pdev)
+static int tsens_probe_common(struct platform_device *pdev)
 {
 	int ret, i;
 	struct device *dev;
@@ -1455,6 +1455,41 @@ static int tsens_probe(struct platform_device *pdev)
 	return ret;
 }
 
+static void tsens_release_resources(void *data)
+{
+	struct device *dev = data;
+
+	devres_release_group(dev->parent, dev);
+}
+
+static int tsens_probe(struct platform_device *pdev)
+{
+	struct device *dev = &pdev->dev;
+	int ret;
+
+	/*
+	 * DT-backed devices own their managed resources directly. Legacy devices
+	 * created by the Global Clock Controller (GCC) driver have no OF node and
+	 * use the parent's firmware data and managed resources instead. Group
+	 * those resources so the child releases them on probe failure or unbind.
+	 */
+	if (dev->of_node)
+		return tsens_probe_common(pdev);
+
+	/* Tie resources allocated on the parent to the TSENS lifetime. */
+	if (!devres_open_group(dev->parent, dev, GFP_KERNEL))
+		return -ENOMEM;
+
+	ret = devm_add_action_or_reset(dev, tsens_release_resources, dev);
+	if (ret)
+		return ret;
+
+	ret = tsens_probe_common(pdev);
+	devres_close_group(dev->parent, dev);
+
+	return ret;
+}
+
 static void tsens_remove(struct platform_device *pdev)
 {
 	struct tsens_priv *priv = platform_get_drvdata(pdev);