mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] thermal: rcar_gen3: quiesce the interrupt source before releasing it
@ 2026-11-17 14:53 Yaohan Li
  2026-11-18  9:31 ` Niklas Söderlund
  2026-11-19  2:29 ` [PATCH v2] thermal: rcar_gen3: release the IRQ handlers before the zones are freed Yaohan Li
  0 siblings, 2 replies; 10+ messages in thread
From: Yaohan Li @ 2026-11-17 14:53 UTC (permalink / raw)
  To: niklas.soderlund, rafael, daniel.lezcano
  Cc: rui.zhang, lukasz.luba, geert+renesas, magnus.damm,
	linux-renesas-soc, linux-pm, linux-kernel

rcar_gen3_thermal_remove() only drops the runtime PM reference.  The two
threaded interrupts are left to devres, which releases them after the
TSC objects and the thermal zones have already been freed, and the
device keeps raising interrupts because nothing clears REG_GEN3_IRQEN.

rcar_gen3_thermal_irq() walks priv->tscs[i], reads REG_GEN3_IRQSTR
through tsc->base and calls thermal_zone_device_update(tsc->zone), so a
handler that the hardirq has already woken can run after those objects
are gone.

Disable the interrupt source for every TSC first, then release the
interrupt handlers, and only then drop the runtime PM reference.
REG_GEN3_IRQEN is only ever written by rcar_gen3_thermal_init(); a
concurrent set_trips() writes REG_GEN3_IRQMSK alone, so it cannot undo
the disable.

Remember the interrupt numbers that were successfully requested, so that
the partial-probe path, which also ends in .remove(), only releases what
was actually registered.

Signed-off-by: Yaohan Li <shunsheng1218@gmail.com>
---
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 also clears REG_GEN3_IRQEN for every TSC before the
handlers are released.  rcar_gen3_thermal_init() is the only writer of
IRQEN; set_trips() writes IRQMSK only, so a concurrent trip update
cannot re-enable the interrupt source.

Testing: compile tested only (arm64, gcc 11.4).  No R-Car hardware was
available.  The partial-probe path that also ends in .remove() was not
exercised.
---
 drivers/thermal/renesas/rcar_gen3_thermal.c | 25 +++++++++++++++++++++
 1 file changed, 25 insertions(+)

diff --git a/drivers/thermal/renesas/rcar_gen3_thermal.c b/drivers/thermal/renesas/rcar_gen3_thermal.c
index 9c64cbc2ee67..35bb49915a75 100644
--- a/drivers/thermal/renesas/rcar_gen3_thermal.c
+++ b/drivers/thermal/renesas/rcar_gen3_thermal.c
@@ -110,6 +110,8 @@ struct rcar_gen3_thermal_priv {
 	int ptat[3];
 	int tj_t;
 	const struct rcar_thermal_info *info;
+	int irqs[2];
+	unsigned int num_irqs;
 };
 
 static inline u32 rcar_gen3_thermal_read(struct rcar_gen3_thermal_tsc *tsc,
@@ -468,7 +470,28 @@ MODULE_DEVICE_TABLE(of, rcar_gen3_thermal_dt_ids);
 
 static void rcar_gen3_thermal_remove(struct platform_device *pdev)
 {
+	struct rcar_gen3_thermal_priv *priv = platform_get_drvdata(pdev);
 	struct device *dev = &pdev->dev;
+	unsigned int i;
+
+	/*
+	 * Disable the interrupt source while the device is still runtime
+	 * resumed.  REG_GEN3_IRQEN is only ever written by
+	 * rcar_gen3_thermal_init(); a concurrent set_trips() writes
+	 * REG_GEN3_IRQMSK alone, so it cannot re-enable the source here.
+	 */
+	for (i = 0; i < priv->num_tscs; i++) {
+		rcar_gen3_thermal_write(priv->tscs[i], REG_GEN3_IRQEN, 0);
+		rcar_gen3_thermal_write(priv->tscs[i], REG_GEN3_IRQMSK, 0);
+	}
+
+	/*
+	 * Release the interrupts before the TSCs and zones are freed:
+	 * free_irq() waits for an executing threaded handler, and the handler
+	 * dereferences both priv->tscs[i] and priv->tscs[i]->zone.
+	 */
+	for (i = 0; i < priv->num_irqs; i++)
+		devm_free_irq(dev, priv->irqs[i], priv);
 
 	pm_runtime_put(dev);
 	pm_runtime_disable(dev);
@@ -504,6 +527,8 @@ static int rcar_gen3_thermal_request_irqs(struct rcar_gen3_thermal_priv *priv,
 						IRQF_ONESHOT, irqname, priv);
 		if (ret)
 			return ret;
+
+		priv->irqs[priv->num_irqs++] = irq;
 	}
 
 	return 0;
-- 
2.34.1


^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH] thermal: rcar_gen3: quiesce the interrupt source before releasing it
  2026-11-17 14:53 [PATCH] thermal: rcar_gen3: quiesce the interrupt source before releasing it Yaohan Li
@ 2026-11-18  9:31 ` Niklas Söderlund
  2026-11-19  2:29 ` [PATCH v2] thermal: rcar_gen3: release the IRQ handlers before the zones are freed Yaohan Li
  1 sibling, 0 replies; 10+ messages in thread
From: Niklas Söderlund @ 2026-11-18  9:31 UTC (permalink / raw)
  To: Yaohan Li
  Cc: rafael, daniel.lezcano, rui.zhang, lukasz.luba, geert+renesas,
	magnus.damm, linux-renesas-soc, linux-pm, linux-kernel

Hi Yaohan,

Thanks for your work.

On 2026-11-17 22:53:48 +0800, Yaohan Li wrote:
> rcar_gen3_thermal_remove() only drops the runtime PM reference.  The two
> threaded interrupts are left to devres, which releases them after the
> TSC objects and the thermal zones have already been freed, and the
> device keeps raising interrupts because nothing clears REG_GEN3_IRQEN.
> 
> rcar_gen3_thermal_irq() walks priv->tscs[i], reads REG_GEN3_IRQSTR
> through tsc->base and calls thermal_zone_device_update(tsc->zone), so a
> handler that the hardirq has already woken can run after those objects
> are gone.
> 
> Disable the interrupt source for every TSC first, then release the
> interrupt handlers, and only then drop the runtime PM reference.
> REG_GEN3_IRQEN is only ever written by rcar_gen3_thermal_init(); a
> concurrent set_trips() writes REG_GEN3_IRQMSK alone, so it cannot undo
> the disable.
> 
> Remember the interrupt numbers that were successfully requested, so that
> the partial-probe path, which also ends in .remove(), only releases what
> was actually registered.

Could this be re-written by a human to describe they "why" and not the 
"how"?

> 
> Signed-off-by: Yaohan Li <shunsheng1218@gmail.com>
> ---
> 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 also clears REG_GEN3_IRQEN for every TSC before the
> handlers are released.  rcar_gen3_thermal_init() is the only writer of
> IRQEN; set_trips() writes IRQMSK only, so a concurrent trip update
> cannot re-enable the interrupt source.
> 
> Testing: compile tested only (arm64, gcc 11.4).  No R-Car hardware was
> available.  The partial-probe path that also ends in .remove() was not
> exercised.
> ---
>  drivers/thermal/renesas/rcar_gen3_thermal.c | 25 +++++++++++++++++++++
>  1 file changed, 25 insertions(+)
> 
> diff --git a/drivers/thermal/renesas/rcar_gen3_thermal.c b/drivers/thermal/renesas/rcar_gen3_thermal.c
> index 9c64cbc2ee67..35bb49915a75 100644
> --- a/drivers/thermal/renesas/rcar_gen3_thermal.c
> +++ b/drivers/thermal/renesas/rcar_gen3_thermal.c
> @@ -110,6 +110,8 @@ struct rcar_gen3_thermal_priv {
>  	int ptat[3];
>  	int tj_t;
>  	const struct rcar_thermal_info *info;
> +	int irqs[2];
> +	unsigned int num_irqs;
>  };
>  
>  static inline u32 rcar_gen3_thermal_read(struct rcar_gen3_thermal_tsc *tsc,
> @@ -468,7 +470,28 @@ MODULE_DEVICE_TABLE(of, rcar_gen3_thermal_dt_ids);
>  
>  static void rcar_gen3_thermal_remove(struct platform_device *pdev)
>  {
> +	struct rcar_gen3_thermal_priv *priv = platform_get_drvdata(pdev);
>  	struct device *dev = &pdev->dev;
> +	unsigned int i;
> +
> +	/*
> +	 * Disable the interrupt source while the device is still runtime
> +	 * resumed.  REG_GEN3_IRQEN is only ever written by
> +	 * rcar_gen3_thermal_init(); a concurrent set_trips() writes
> +	 * REG_GEN3_IRQMSK alone, so it cannot re-enable the source here.
> +	 */
> +	for (i = 0; i < priv->num_tscs; i++) {

Move the loop variable declaration here.

> +		rcar_gen3_thermal_write(priv->tscs[i], REG_GEN3_IRQEN, 0);
> +		rcar_gen3_thermal_write(priv->tscs[i], REG_GEN3_IRQMSK, 0);
> +	}
> +
> +	/*
> +	 * Release the interrupts before the TSCs and zones are freed:
> +	 * free_irq() waits for an executing threaded handler, and the handler
> +	 * dereferences both priv->tscs[i] and priv->tscs[i]->zone.
> +	 */
> +	for (i = 0; i < priv->num_irqs; i++)

Can't you store a negative error code in riv->irqs[] to indicate no IRQ 
and avoid adding the booking variable num_irqs? One variable can never 
go out of sync.

While at it you could create a new define for the maximum number of 
IRQ's and replace the loop limit in rcar_gen3_thermal_request_irqs() and 
the number of elements in irqs[].

> +		devm_free_irq(dev, priv->irqs[i], priv);

Why free it and just not sync the interrupt?

>  
>  	pm_runtime_put(dev);
>  	pm_runtime_disable(dev);
> @@ -504,6 +527,8 @@ static int rcar_gen3_thermal_request_irqs(struct rcar_gen3_thermal_priv *priv,
>  						IRQF_ONESHOT, irqname, priv);
>  		if (ret)
>  			return ret;
> +
> +		priv->irqs[priv->num_irqs++] = irq;
>  	}
>  
>  	return 0;
> -- 
> 2.34.1
> 

-- 
Kind Regards,
Niklas Söderlund

^ permalink raw reply	[flat|nested] 10+ messages in thread

* [PATCH v2] thermal: rcar_gen3: release the IRQ handlers before the zones are freed
  2026-11-17 14:53 [PATCH] thermal: rcar_gen3: quiesce the interrupt source before releasing it Yaohan Li
  2026-11-18  9:31 ` Niklas Söderlund
@ 2026-11-19  2:29 ` Yaohan Li
  2026-11-22 14:48   ` Niklas Söderlund
  2026-11-24  9:51   ` [PATCH v3] thermal: rcar_gen3: register the interrupt handlers after the zones Yaohan Li
  1 sibling, 2 replies; 10+ messages in thread
From: Yaohan Li @ 2026-11-19  2:29 UTC (permalink / raw)
  To: niklas.soderlund, rafael, daniel.lezcano
  Cc: rui.zhang, lukasz.luba, geert+renesas, magnus.damm,
	linux-renesas-soc, linux-pm, linux-kernel

The interrupt handlers are requested before the thermal zones are
registered, so devres releases the zones first.  When ->remove() returns,
the zones are gone while the handlers are still installed until devres
gets to them, and a handler that runs in that window walks priv->tscs[]
and updates a zone that no longer exists.  The source keeps asserting
because nothing clears REG_GEN3_IRQEN before the hardware goes away.

synchronize_irq() alone does not close that window.  It waits for a
handler that is running at that moment, but the zones are freed before
the handlers are, so an interrupt that is raised just after the wait, or
that is still pending in the interrupt controller, runs a handler
against freed memory.  Removing the handler is what actually closes it,
so ->remove() masks the source and calls devm_free_irq() for every
interrupt that was requested, while the device is still runtime
resumed.

The interrupt numbers live in an array whose unused slots stay negative,
so the partial probe path - which also ends in ->remove() - releases
exactly what was requested without a second counter to keep in sync.

Signed-off-by: Yaohan Li <shunsheng1218@gmail.com>

---
Compile tested only: arm64 defconfig, aarch64-linux-gnu-gcc 11.4.  The
board was not available, so the removal path was not exercised and no
race injection was run.

Changes in v2:
 - Mask the source and release the handlers in ->remove() instead of
   relying on devres plus synchronize_irq(), and explain in the message
   why a single synchronize_irq() cannot close the window.
 - Keep the interrupt numbers in an array whose unused slots stay
   negative instead of a separate count.
 - Use a loop-local index and a named maximum instead of literals.
---
 drivers/thermal/renesas/rcar_gen3_thermal.c | 33 ++++++++++++++++++++-
 1 file changed, 32 insertions(+), 1 deletion(-)

diff --git a/drivers/thermal/renesas/rcar_gen3_thermal.c b/drivers/thermal/renesas/rcar_gen3_thermal.c
index 9c64cbc2ee67..2a6890d2a5da 100644
--- a/drivers/thermal/renesas/rcar_gen3_thermal.c
+++ b/drivers/thermal/renesas/rcar_gen3_thermal.c
@@ -103,6 +103,8 @@ struct rcar_gen3_thermal_tsc {
 	int thcode[3];
 };
 
+#define RCAR_GEN3_THERMAL_MAX_IRQ	2
+
 struct rcar_gen3_thermal_priv {
 	struct rcar_gen3_thermal_tsc *tscs[TSC_MAX_NUM];
 	struct thermal_zone_device_ops ops;
@@ -110,6 +112,7 @@ struct rcar_gen3_thermal_priv {
 	int ptat[3];
 	int tj_t;
 	const struct rcar_thermal_info *info;
+	int irqs[RCAR_GEN3_THERMAL_MAX_IRQ];
 };
 
 static inline u32 rcar_gen3_thermal_read(struct rcar_gen3_thermal_tsc *tsc,
@@ -468,8 +471,31 @@ MODULE_DEVICE_TABLE(of, rcar_gen3_thermal_dt_ids);
 
 static void rcar_gen3_thermal_remove(struct platform_device *pdev)
 {
+	struct rcar_gen3_thermal_priv *priv = platform_get_drvdata(pdev);
 	struct device *dev = &pdev->dev;
 
+	/*
+	 * Disable the interrupt source while the device is still runtime
+	 * resumed.  REG_GEN3_IRQEN is only ever written by
+	 * rcar_gen3_thermal_init(); a concurrent set_trips() writes
+	 * REG_GEN3_IRQMSK alone, so it cannot re-enable the source here.
+	 */
+	for (unsigned int i = 0; i < priv->num_tscs; i++) {
+		rcar_gen3_thermal_write(priv->tscs[i], REG_GEN3_IRQEN, 0);
+		rcar_gen3_thermal_write(priv->tscs[i], REG_GEN3_IRQMSK, 0);
+	}
+
+	/*
+	 * Release the handlers here instead of leaving that to devres.  They
+	 * were requested before the thermal zones, so devres would free the
+	 * zones first, and a handler that runs in between would use a zone
+	 * that is already gone.  free_irq() also waits for the threaded
+	 * handler that may be running right now.
+	 */
+	for (unsigned int i = 0; i < ARRAY_SIZE(priv->irqs); i++)
+		if (priv->irqs[i] >= 0)
+			devm_free_irq(dev, priv->irqs[i], priv);
+
 	pm_runtime_put(dev);
 	pm_runtime_disable(dev);
 }
@@ -489,7 +515,7 @@ static int rcar_gen3_thermal_request_irqs(struct rcar_gen3_thermal_priv *priv,
 	char *irqname;
 	int ret, irq;
 
-	for (i = 0; i < 2; i++) {
+	for (i = 0; i < RCAR_GEN3_THERMAL_MAX_IRQ; i++) {
 		irq = platform_get_irq_optional(pdev, i);
 		if (irq < 0)
 			return irq;
@@ -504,6 +530,8 @@ static int rcar_gen3_thermal_request_irqs(struct rcar_gen3_thermal_priv *priv,
 						IRQF_ONESHOT, irqname, priv);
 		if (ret)
 			return ret;
+
+		priv->irqs[i] = irq;
 	}
 
 	return 0;
@@ -525,6 +553,9 @@ static int rcar_gen3_thermal_probe(struct platform_device *pdev)
 	priv->ops = rcar_gen3_tz_of_ops;
 
 	priv->info = of_device_get_match_data(dev);
+
+	for (i = 0; i < ARRAY_SIZE(priv->irqs); i++)
+		priv->irqs[i] = -ENOENT;
 	platform_set_drvdata(pdev, priv);
 
 	if (rcar_gen3_thermal_request_irqs(priv, pdev))
-- 
2.34.1


^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH v2] thermal: rcar_gen3: release the IRQ handlers before the zones are freed
  2026-11-19  2:29 ` [PATCH v2] thermal: rcar_gen3: release the IRQ handlers before the zones are freed Yaohan Li
@ 2026-11-22 14:48   ` Niklas Söderlund
  2026-11-23  2:21     ` Yaohan Li
  2026-11-24  9:51   ` [PATCH v3] thermal: rcar_gen3: register the interrupt handlers after the zones Yaohan Li
  1 sibling, 1 reply; 10+ messages in thread
From: Niklas Söderlund @ 2026-11-22 14:48 UTC (permalink / raw)
  To: Yaohan Li
  Cc: rafael, daniel.lezcano, rui.zhang, lukasz.luba, geert+renesas,
	magnus.damm, linux-renesas-soc, linux-pm, linux-kernel

Hi Yaohan,

Thanks for your work.

On 2026-11-19 10:29:26 +0800, Yaohan Li wrote:
> The interrupt handlers are requested before the thermal zones are
> registered, so devres releases the zones first.  When ->remove() returns,
> the zones are gone while the handlers are still installed until devres
> gets to them, and a handler that runs in that window walks priv->tscs[]
> and updates a zone that no longer exists.  The source keeps asserting
> because nothing clears REG_GEN3_IRQEN before the hardware goes away.
> 
> synchronize_irq() alone does not close that window.  It waits for a
> handler that is running at that moment, but the zones are freed before
> the handlers are, so an interrupt that is raised just after the wait, or
> that is still pending in the interrupt controller, runs a handler
> against freed memory.  Removing the handler is what actually closes it,
> so ->remove() masks the source and calls devm_free_irq() for every
> interrupt that was requested, while the device is still runtime
> resumed.
> 
> The interrupt numbers live in an array whose unused slots stay negative,
> so the partial probe path - which also ends in ->remove() - releases
> exactly what was requested without a second counter to keep in sync.
> 
> Signed-off-by: Yaohan Li <shunsheng1218@gmail.com>
> 
> ---
> Compile tested only: arm64 defconfig, aarch64-linux-gnu-gcc 11.4.  The
> board was not available, so the removal path was not exercised and no
> race injection was run.
> 
> Changes in v2:
>  - Mask the source and release the handlers in ->remove() instead of
>    relying on devres plus synchronize_irq(), and explain in the message
>    why a single synchronize_irq() cannot close the window.
>  - Keep the interrupt numbers in an array whose unused slots stay
>    negative instead of a separate count.
>  - Use a loop-local index and a named maximum instead of literals.
> ---
>  drivers/thermal/renesas/rcar_gen3_thermal.c | 33 ++++++++++++++++++++-
>  1 file changed, 32 insertions(+), 1 deletion(-)
> 
> diff --git a/drivers/thermal/renesas/rcar_gen3_thermal.c b/drivers/thermal/renesas/rcar_gen3_thermal.c
> index 9c64cbc2ee67..2a6890d2a5da 100644
> --- a/drivers/thermal/renesas/rcar_gen3_thermal.c
> +++ b/drivers/thermal/renesas/rcar_gen3_thermal.c
> @@ -103,6 +103,8 @@ struct rcar_gen3_thermal_tsc {
>  	int thcode[3];
>  };
>  
> +#define RCAR_GEN3_THERMAL_MAX_IRQ	2

I like this!

> +
>  struct rcar_gen3_thermal_priv {
>  	struct rcar_gen3_thermal_tsc *tscs[TSC_MAX_NUM];
>  	struct thermal_zone_device_ops ops;
> @@ -110,6 +112,7 @@ struct rcar_gen3_thermal_priv {
>  	int ptat[3];
>  	int tj_t;
>  	const struct rcar_thermal_info *info;
> +	int irqs[RCAR_GEN3_THERMAL_MAX_IRQ];
>  };
>  
>  static inline u32 rcar_gen3_thermal_read(struct rcar_gen3_thermal_tsc *tsc,
> @@ -468,8 +471,31 @@ MODULE_DEVICE_TABLE(of, rcar_gen3_thermal_dt_ids);
>  
>  static void rcar_gen3_thermal_remove(struct platform_device *pdev)
>  {
> +	struct rcar_gen3_thermal_priv *priv = platform_get_drvdata(pdev);
>  	struct device *dev = &pdev->dev;
>  
> +	/*
> +	 * Disable the interrupt source while the device is still runtime
> +	 * resumed.  REG_GEN3_IRQEN is only ever written by
> +	 * rcar_gen3_thermal_init(); a concurrent set_trips() writes
> +	 * REG_GEN3_IRQMSK alone, so it cannot re-enable the source here.
> +	 */

Is this not a tad verbose? How about just

        /* Disable interrupts. */

> +	for (unsigned int i = 0; i < priv->num_tscs; i++) {
> +		rcar_gen3_thermal_write(priv->tscs[i], REG_GEN3_IRQEN, 0);
> +		rcar_gen3_thermal_write(priv->tscs[i], REG_GEN3_IRQMSK, 0);
> +	}
> +
> +	/*
> +	 * Release the handlers here instead of leaving that to devres.  They
> +	 * were requested before the thermal zones, so devres would free the
> +	 * zones first, and a handler that runs in between would use a zone
> +	 * that is already gone.  free_irq() also waits for the threaded
> +	 * handler that may be running right now.
> +	 */
> +	for (unsigned int i = 0; i < ARRAY_SIZE(priv->irqs); i++)
> +		if (priv->irqs[i] >= 0)
> +			devm_free_irq(dev, priv->irqs[i], priv);
> +

I don't understand why this explicitly is needed, can't we let devres 
sort this out?

>  	pm_runtime_put(dev);
>  	pm_runtime_disable(dev);
>  }
> @@ -489,7 +515,7 @@ static int rcar_gen3_thermal_request_irqs(struct rcar_gen3_thermal_priv *priv,
>  	char *irqname;
>  	int ret, irq;
>  
> -	for (i = 0; i < 2; i++) {
> +	for (i = 0; i < RCAR_GEN3_THERMAL_MAX_IRQ; i++) {
>  		irq = platform_get_irq_optional(pdev, i);
>  		if (irq < 0)
>  			return irq;
> @@ -504,6 +530,8 @@ static int rcar_gen3_thermal_request_irqs(struct rcar_gen3_thermal_priv *priv,
>  						IRQF_ONESHOT, irqname, priv);
>  		if (ret)
>  			return ret;
> +
> +		priv->irqs[i] = irq;
>  	}
>  
>  	return 0;
> @@ -525,6 +553,9 @@ static int rcar_gen3_thermal_probe(struct platform_device *pdev)
>  	priv->ops = rcar_gen3_tz_of_ops;
>  
>  	priv->info = of_device_get_match_data(dev);
> +
> +	for (i = 0; i < ARRAY_SIZE(priv->irqs); i++)
> +		priv->irqs[i] = -ENOENT;
>  	platform_set_drvdata(pdev, priv);
>  
>  	if (rcar_gen3_thermal_request_irqs(priv, pdev))
> -- 
> 2.34.1
> 

-- 
Kind Regards,
Niklas Söderlund

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH v2] thermal: rcar_gen3: release the IRQ handlers before the zones are freed
  2026-11-22 14:48   ` Niklas Söderlund
@ 2026-11-23  2:21     ` Yaohan Li
  2026-11-23  6:47       ` Niklas Söderlund
  0 siblings, 1 reply; 10+ messages in thread
From: Yaohan Li @ 2026-11-23  2:21 UTC (permalink / raw)
  To: Niklas Söderlund
  Cc: Rafael J. Wysocki, Daniel Lezcano, Zhang Rui, Lukasz Luba,
	Magnus Damm, linux-renesas-soc, linux-pm, linux-kernel

Hi Niklas,

The explicit devm_free_irq() calls are needed because of the current devres
registration order. rcar_gen3_thermal_request_irqs() runs before the loop
that calls devm_thermal_of_zone_register(). On teardown, devres therefore
unregisters the zones before freeing the IRQ handlers. During that interval,
rcar_gen3_thermal_irq() can still call thermal_zone_device_update() through
tsc->zone, which is not cleared when the zone is unregistered.

Masking the source and freeing the IRQs in ->remove() closes that window;
free_irq() also waits for a threaded handler that is already running.

Requesting the IRQs after the zones would make the automatic release order
correct. But probe currently uses the return value of
rcar_gen3_thermal_request_irqs() to select priv->ops.set_trips before zone
registration, and rcar_gen3_thermal_init() enables the source depending on
that callback. Reordering would therefore need changes to probe setup and
failure handling, rather than just moving one call. I kept this revision
focused on teardown.

I have shortened the two comments as suggested. I will hold off on v3 until
we have agreed on the approach.

Thanks,
Yaohan

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH v2] thermal: rcar_gen3: release the IRQ handlers before the zones are freed
  2026-11-23  2:21     ` Yaohan Li
@ 2026-11-23  6:47       ` Niklas Söderlund
  2026-11-24  2:22         ` Yaohan Li
  0 siblings, 1 reply; 10+ messages in thread
From: Niklas Söderlund @ 2026-11-23  6:47 UTC (permalink / raw)
  To: Yaohan Li
  Cc: Rafael J. Wysocki, Daniel Lezcano, Zhang Rui, Lukasz Luba,
	Magnus Damm, linux-renesas-soc, linux-pm, linux-kernel

Hi Yaohan,

On 2026-11-23 10:21:46 +0800, Yaohan Li wrote:
> Hi Niklas,
> 
> The explicit devm_free_irq() calls are needed because of the current devres
> registration order. rcar_gen3_thermal_request_irqs() runs before the loop
> that calls devm_thermal_of_zone_register(). On teardown, devres therefore
> unregisters the zones before freeing the IRQ handlers. During that interval,
> rcar_gen3_thermal_irq() can still call thermal_zone_device_update() through
> tsc->zone, which is not cleared when the zone is unregistered.

Ahh I see, thanks for explaining it. Will that window not also exist 
during probe? Would not a better solution be to rework the order in 
probe to eliminate the issue completely?

> 
> Masking the source and freeing the IRQs in ->remove() closes that window;
> free_irq() also waits for a threaded handler that is already running.
> 
> Requesting the IRQs after the zones would make the automatic release order
> correct. But probe currently uses the return value of
> rcar_gen3_thermal_request_irqs() to select priv->ops.set_trips before zone
> registration, and rcar_gen3_thermal_init() enables the source depending on
> that callback. Reordering would therefore need changes to probe setup and
> failure handling, rather than just moving one call. I kept this revision
> focused on teardown.
> 
> I have shortened the two comments as suggested. I will hold off on v3 until
> we have agreed on the approach.
> 
> Thanks,
> Yaohan

-- 
Kind Regards,
Niklas Söderlund

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH v2] thermal: rcar_gen3: release the IRQ handlers before the zones are freed
  2026-11-23  6:47       ` Niklas Söderlund
@ 2026-11-24  2:22         ` Yaohan Li
  2026-11-24  6:40           ` Niklas Söderlund
  0 siblings, 1 reply; 10+ messages in thread
From: Yaohan Li @ 2026-11-24  2:22 UTC (permalink / raw)
  To: Niklas Söderlund
  Cc: Rafael J. Wysocki, Daniel Lezcano, Zhang Rui, Lukasz Luba,
	Magnus Damm, linux-renesas-soc, linux-pm, linux-kernel

Hi Niklas,

Yes, I agree.  I have reworked the probe to look up the IRQs first,
initialize the sensors with IRQEN cleared, and request the handlers after
all the thermal zones have been registered.  A devres action disables the
interrupt sources before the handlers are freed, so ->remove() is no
longer needed.

Since the thermal core copies the zone operations during registration,
set_trips() still has to be selected beforehand.

The current version retains the no-IRQ fallback for -ENXIO and propagates
-EPROBE_DEFER and other lookup errors.  However, if IRQ lookup succeeds
but requesting a handler fails, probe now fails instead of falling back
to polling.

Would you be happy with that change, or would you prefer to retain the
original fallback on IRQ request failure as well?

The reworked patch builds on arm64, but I have not tested it on hardware.

Thanks,
Yaohan

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH v2] thermal: rcar_gen3: release the IRQ handlers before the zones are freed
  2026-11-24  2:22         ` Yaohan Li
@ 2026-11-24  6:40           ` Niklas Söderlund
  0 siblings, 0 replies; 10+ messages in thread
From: Niklas Söderlund @ 2026-11-24  6:40 UTC (permalink / raw)
  To: Yaohan Li
  Cc: Rafael J. Wysocki, Daniel Lezcano, Zhang Rui, Lukasz Luba,
	Magnus Damm, linux-renesas-soc, linux-pm, linux-kernel

Hi Yaohan,

On 2026-11-24 10:22:31 +0800, Yaohan Li wrote:
> Hi Niklas,
> 
> Yes, I agree.  I have reworked the probe to look up the IRQs first,
> initialize the sensors with IRQEN cleared, and request the handlers after
> all the thermal zones have been registered.  A devres action disables the
> interrupt sources before the handlers are freed, so ->remove() is no
> longer needed.
> 
> Since the thermal core copies the zone operations during registration,
> set_trips() still has to be selected beforehand.
> 
> The current version retains the no-IRQ fallback for -ENXIO and propagates
> -EPROBE_DEFER and other lookup errors.  However, if IRQ lookup succeeds
> but requesting a handler fails, probe now fails instead of falling back
> to polling.

It needs to revert back to polling. IIRC the interrupts are used to get 
quick notification of a trip point but if interrupts are not 
enabled/supported it can still find it using the polling method.

> 
> Would you be happy with that change, or would you prefer to retain the
> original fallback on IRQ request failure as well?

A bug fix should not alter behavior, other then fixing the bug ;-)

> 
> The reworked patch builds on arm64, but I have not tested it on hardware.
> 
> Thanks,
> Yaohan

-- 
Kind Regards,
Niklas Söderlund

^ permalink raw reply	[flat|nested] 10+ messages in thread

* [PATCH v3] thermal: rcar_gen3: register the interrupt handlers after the zones
  2026-11-19  2:29 ` [PATCH v2] thermal: rcar_gen3: release the IRQ handlers before the zones are freed Yaohan Li
  2026-11-22 14:48   ` Niklas Söderlund
@ 2026-11-24  9:51   ` Yaohan Li
  2026-11-24 13:15     ` Niklas Söderlund
  1 sibling, 1 reply; 10+ messages in thread
From: Yaohan Li @ 2026-11-24  9:51 UTC (permalink / raw)
  To: Niklas Söderlund, Rafael J . Wysocki, Daniel Lezcano
  Cc: Zhang Rui, Lukasz Luba, Magnus Damm, linux-renesas-soc, linux-pm,
	linux-kernel

The interrupt handlers are requested before the thermal zones are
registered, and devres releases resources in the reverse order.  That
leaves the same object exposed at both ends: during probe the handler
exists while part of the sensors and zones are not initialised yet, and
during teardown it outlives the zones it reports to.  The handler walks
every sensor and calls thermal_zone_device_update() through tsc->zone,
which is never cleared when the zone is unregistered.

Fix the order rather than the removal path.  Look the interrupts up
first, keep the source masked while the sensors are initialised, and
register the handlers only once every zone exists.  A devres action
registered after the handlers masks the source again, and the source is
enabled last, so the teardown order comes from devres itself: source
off, handlers, hwmon, zones, MMIO mappings, runtime PM.  ->remove() has
nothing left to do and is gone.

The thermal core copies the zone operations when a zone is registered,
so whether set_trips() is available has to be decided before the zones
are created.  Registering the handlers last moves that decision after
the zones exist, so when the interrupts cannot be requested the zones
are released and registered again without set_trips(): the driver keeps
the polling configuration it had before instead of failing the probe.
The zones are created and dropped once more in that path only.

rcar_gen3_thermal_init() clears REG_GEN3_IRQEN as well.  Nothing but
rcar_gen3_thermal_enable_irqs() writes the register again, at the end of
the probe and from ->resume(), so a source that the firmware or an
earlier bind left enabled does not stay on while the zones are being
created.

Check the sensor count before reading the fuses, because the fuse
reader dereferences tscs[0].  Return -ENODEV when no sensor resources
are present.

Signed-off-by: Yaohan Li <shunsheng1218@gmail.com>

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

Compile tested only: arm64, aarch64-linux-gnu-gcc 11.4,
CONFIG_ARCH_RENESAS, CONFIG_ARCH_RCAR_GEN3 and CONFIG_RCAR_GEN3_THERMAL.
No board was available, so probe, the polling fallback, resume and
teardown were not exercised on hardware, and there was no way to make an
interrupt request fail on a real device.

Changes in v3:
 - Register the interrupt handlers after the thermal zones and let devres
   own the whole teardown, as suggested; ->remove() is gone.
 - Look the interrupts up before the zones are registered, because the
   thermal core copies the zone operations at registration time.
 - Split the sensor initialisation from enabling the interrupt source and
   re-enable it from ->resume().
 - Clear REG_GEN3_IRQEN in the sensor initialisation, so that the source
   stays masked until it is enabled on purpose.
 - Keep the polling fallback when the handlers cannot be requested:
   release the zones through a devres group and register them again
   without set_trips().
 - Check for a device without sensors before the fuses are read.
 - Drop the has_irqs() helper.

AI assistance: prepared with Claude (Anthropic) as a coding assistant; the
author reviewed the reasoning and takes responsibility for the content.
---
 drivers/thermal/renesas/rcar_gen3_thermal.c | 197 ++++++++++++++------
 1 file changed, 145 insertions(+), 52 deletions(-)

diff --git a/drivers/thermal/renesas/rcar_gen3_thermal.c b/drivers/thermal/renesas/rcar_gen3_thermal.c
index 9c64cbc2ee67..7e4d6aa6e5dd 100644
--- a/drivers/thermal/renesas/rcar_gen3_thermal.c
+++ b/drivers/thermal/renesas/rcar_gen3_thermal.c
@@ -103,6 +103,8 @@ struct rcar_gen3_thermal_tsc {
 	int thcode[3];
 };
 
+#define RCAR_GEN3_THERMAL_MAX_IRQ	2
+
 struct rcar_gen3_thermal_priv {
 	struct rcar_gen3_thermal_tsc *tscs[TSC_MAX_NUM];
 	struct thermal_zone_device_ops ops;
@@ -110,6 +112,7 @@ struct rcar_gen3_thermal_priv {
 	int ptat[3];
 	int tj_t;
 	const struct rcar_thermal_info *info;
+	int irqs[RCAR_GEN3_THERMAL_MAX_IRQ];
 };
 
 static inline u32 rcar_gen3_thermal_read(struct rcar_gen3_thermal_tsc *tsc,
@@ -324,8 +327,7 @@ static bool rcar_gen3_thermal_read_fuses(struct rcar_gen3_thermal_priv *priv)
 	return true;
 }
 
-static void rcar_gen3_thermal_init(struct rcar_gen3_thermal_priv *priv,
-				   struct rcar_gen3_thermal_tsc *tsc)
+static void rcar_gen3_thermal_init(struct rcar_gen3_thermal_tsc *tsc)
 {
 	u32 reg_val;
 
@@ -335,11 +337,9 @@ static void rcar_gen3_thermal_init(struct rcar_gen3_thermal_priv *priv,
 
 	usleep_range(1000, 2000);
 
+	rcar_gen3_thermal_write(tsc, REG_GEN3_IRQEN, 0);
 	rcar_gen3_thermal_write(tsc, REG_GEN3_IRQCTL, 0);
 	rcar_gen3_thermal_write(tsc, REG_GEN3_IRQMSK, 0);
-	if (priv->ops.set_trips)
-		rcar_gen3_thermal_write(tsc, REG_GEN3_IRQEN,
-					IRQ_TEMPD1 | IRQ_TEMP2);
 
 	reg_val = rcar_gen3_thermal_read(tsc, REG_GEN3_THCTR);
 	reg_val |= THCTR_THSST;
@@ -466,9 +466,9 @@ static const struct of_device_id rcar_gen3_thermal_dt_ids[] = {
 };
 MODULE_DEVICE_TABLE(of, rcar_gen3_thermal_dt_ids);
 
-static void rcar_gen3_thermal_remove(struct platform_device *pdev)
+static void rcar_gen3_thermal_pm_put(void *data)
 {
-	struct device *dev = &pdev->dev;
+	struct device *dev = data;
 
 	pm_runtime_put(dev);
 	pm_runtime_disable(dev);
@@ -481,25 +481,66 @@ static void rcar_gen3_hwmon_action(void *data)
 	thermal_remove_hwmon_sysfs(zone);
 }
 
-static int rcar_gen3_thermal_request_irqs(struct rcar_gen3_thermal_priv *priv,
-					  struct platform_device *pdev)
+static int rcar_gen3_thermal_register_zones(struct rcar_gen3_thermal_priv *priv,
+					    struct device *dev)
 {
-	struct device *dev = &pdev->dev;
 	unsigned int i;
-	char *irqname;
-	int ret, irq;
+	int ret;
+
+	for (i = 0; i < priv->num_tscs; i++) {
+		struct rcar_gen3_thermal_tsc *tsc = priv->tscs[i];
+		struct thermal_zone_device *zone;
 
-	for (i = 0; i < 2; i++) {
+		zone = devm_thermal_of_zone_register(dev, i, tsc, &priv->ops);
+		if (IS_ERR(zone)) {
+			dev_err(dev, "Sensor %u: Can't register thermal zone\n", i);
+			return PTR_ERR(zone);
+		}
+		tsc->zone = zone;
+
+		ret = thermal_add_hwmon_sysfs(zone);
+		if (ret)
+			return ret;
+
+		ret = devm_add_action_or_reset(dev, rcar_gen3_hwmon_action, zone);
+		if (ret)
+			return ret;
+	}
+
+	return 0;
+}
+
+static int rcar_gen3_thermal_get_irqs(struct rcar_gen3_thermal_priv *priv,
+				      struct platform_device *pdev)
+{
+	unsigned int i;
+	int irq;
+
+	for (i = 0; i < RCAR_GEN3_THERMAL_MAX_IRQ; i++) {
 		irq = platform_get_irq_optional(pdev, i);
 		if (irq < 0)
 			return irq;
 
+		priv->irqs[i] = irq;
+	}
+
+	return 0;
+}
+
+static int rcar_gen3_thermal_request_irqs(struct rcar_gen3_thermal_priv *priv,
+					  struct device *dev)
+{
+	unsigned int i;
+	char *irqname;
+	int ret;
+
+	for (i = 0; i < RCAR_GEN3_THERMAL_MAX_IRQ; i++) {
 		irqname = devm_kasprintf(dev, GFP_KERNEL, "%s:ch%d",
 					 dev_name(dev), i);
 		if (!irqname)
 			return -ENOMEM;
 
-		ret = devm_request_threaded_irq(dev, irq, NULL,
+		ret = devm_request_threaded_irq(dev, priv->irqs[i], NULL,
 						rcar_gen3_thermal_irq,
 						IRQF_ONESHOT, irqname, priv);
 		if (ret)
@@ -509,14 +550,34 @@ static int rcar_gen3_thermal_request_irqs(struct rcar_gen3_thermal_priv *priv,
 	return 0;
 }
 
+static void rcar_gen3_thermal_enable_irqs(struct rcar_gen3_thermal_priv *priv)
+{
+	unsigned int i;
+
+	for (i = 0; i < priv->num_tscs; i++)
+		rcar_gen3_thermal_write(priv->tscs[i], REG_GEN3_IRQEN,
+					IRQ_TEMPD1 | IRQ_TEMP2);
+}
+
+static void rcar_gen3_thermal_disable_irqs(void *data)
+{
+	struct rcar_gen3_thermal_priv *priv = data;
+	unsigned int i;
+
+	for (i = 0; i < priv->num_tscs; i++) {
+		rcar_gen3_thermal_write(priv->tscs[i], REG_GEN3_IRQEN, 0);
+		rcar_gen3_thermal_write(priv->tscs[i], REG_GEN3_IRQMSK, 0);
+	}
+}
+
 static int rcar_gen3_thermal_probe(struct platform_device *pdev)
 {
 	struct rcar_gen3_thermal_priv *priv;
 	struct device *dev = &pdev->dev;
 	struct resource *res;
-	struct thermal_zone_device *zone;
 	unsigned int i;
 	int ret;
+	void *group;
 
 	priv = devm_kzalloc_obj(dev, *priv);
 	if (!priv)
@@ -525,14 +586,25 @@ static int rcar_gen3_thermal_probe(struct platform_device *pdev)
 	priv->ops = rcar_gen3_tz_of_ops;
 
 	priv->info = of_device_get_match_data(dev);
+
 	platform_set_drvdata(pdev, priv);
 
-	if (rcar_gen3_thermal_request_irqs(priv, pdev))
+	/*
+	 * The interrupts are looked up here but their handlers are registered
+	 * only once the thermal zones exist.  The thermal core copies the zone
+	 * operations when a zone is registered, so whether set_trips() is
+	 * available has to be decided before the zones are created.
+	 */
+	if (rcar_gen3_thermal_get_irqs(priv, pdev))
 		priv->ops.set_trips = NULL;
 
 	pm_runtime_enable(dev);
 	pm_runtime_get_sync(dev);
 
+	ret = devm_add_action_or_reset(dev, rcar_gen3_thermal_pm_put, dev);
+	if (ret)
+		return ret;
+
 	for (i = 0; i < TSC_MAX_NUM; i++) {
 		struct rcar_gen3_thermal_tsc *tsc;
 
@@ -541,22 +613,20 @@ static int rcar_gen3_thermal_probe(struct platform_device *pdev)
 			break;
 
 		tsc = devm_kzalloc_obj(dev, *tsc);
-		if (!tsc) {
-			ret = -ENOMEM;
-			goto error_unregister;
-		}
+		if (!tsc)
+			return -ENOMEM;
 
 		tsc->priv = priv;
 		tsc->base = devm_ioremap_resource(dev, res);
-		if (IS_ERR(tsc->base)) {
-			ret = PTR_ERR(tsc->base);
-			goto error_unregister;
-		}
+		if (IS_ERR(tsc->base))
+			return PTR_ERR(tsc->base);
 
 		priv->tscs[i] = tsc;
 	}
 
 	priv->num_tscs = i;
+	if (!priv->num_tscs)
+		return -ENODEV;
 
 	if (!rcar_gen3_thermal_read_fuses(priv))
 		dev_info(dev, "No calibration values fused, fallback to driver values\n");
@@ -564,40 +634,64 @@ static int rcar_gen3_thermal_probe(struct platform_device *pdev)
 	rcar_gen3_thermal_shared_coefs(priv);
 
 	for (i = 0; i < priv->num_tscs; i++) {
-		struct rcar_gen3_thermal_tsc *tsc = priv->tscs[i];
+		rcar_gen3_thermal_init(priv->tscs[i]);
+		rcar_gen3_thermal_tsc_coefs(priv, priv->tscs[i]);
+	}
 
-		rcar_gen3_thermal_init(priv, tsc);
-		rcar_gen3_thermal_tsc_coefs(priv, tsc);
+register_zones:
+	group = devres_open_group(dev, NULL, GFP_KERNEL);
+	if (!group)
+		return -ENOMEM;
 
-		zone = devm_thermal_of_zone_register(dev, i, tsc, &priv->ops);
-		if (IS_ERR(zone)) {
-			dev_err(dev, "Sensor %u: Can't register thermal zone\n", i);
-			ret = PTR_ERR(zone);
-			goto error_unregister;
+	ret = rcar_gen3_thermal_register_zones(priv, dev);
+	if (ret)
+		goto release_group;
+
+	/*
+	 * Handlers last and the source enabled after them, so devres runs the
+	 * teardown in the opposite order: source off, handlers, hwmon, zones,
+	 * MMIO mappings, runtime PM.
+	 */
+	if (priv->ops.set_trips) {
+		ret = rcar_gen3_thermal_request_irqs(priv, dev);
+		if (ret) {
+			dev_info(dev, "Failed to request interrupts (%d), polling instead\n",
+				 ret);
+
+			/*
+			 * The thermal core copies the zone operations when a
+			 * zone is registered, so the zones above cannot be
+			 * switched to polling afterwards.  Release them and
+			 * register them again without set_trips().
+			 */
+			devres_release_group(dev, group);
+
+			for (i = 0; i < priv->num_tscs; i++)
+				priv->tscs[i]->zone = NULL;
+
+			rcar_gen3_thermal_disable_irqs(priv);
+			priv->ops.set_trips = NULL;
+			goto register_zones;
 		}
-		tsc->zone = zone;
 
-		ret = thermal_add_hwmon_sysfs(tsc->zone);
+		ret = devm_add_action_or_reset(dev,
+					       rcar_gen3_thermal_disable_irqs,
+					       priv);
 		if (ret)
-			goto error_unregister;
+			goto release_group;
 
-		ret = devm_add_action_or_reset(dev, rcar_gen3_hwmon_action, zone);
-		if (ret)
-			goto error_unregister;
-
-		dev_info(dev, "Sensor %u: Loaded\n", i);
+		rcar_gen3_thermal_enable_irqs(priv);
 	}
 
-	if (!priv->num_tscs) {
-		ret = -ENODEV;
-		goto error_unregister;
-	}
+	devres_remove_group(dev, group);
 
-	return 0;
+	for (i = 0; i < priv->num_tscs; i++)
+		dev_info(dev, "Sensor %u: Loaded\n", i);
 
-error_unregister:
-	rcar_gen3_thermal_remove(pdev);
+	return 0;
 
+release_group:
+	devres_release_group(dev, group);
 	return ret;
 }
 
@@ -606,11 +700,11 @@ static int rcar_gen3_thermal_resume(struct device *dev)
 	struct rcar_gen3_thermal_priv *priv = dev_get_drvdata(dev);
 	unsigned int i;
 
-	for (i = 0; i < priv->num_tscs; i++) {
-		struct rcar_gen3_thermal_tsc *tsc = priv->tscs[i];
+	for (i = 0; i < priv->num_tscs; i++)
+		rcar_gen3_thermal_init(priv->tscs[i]);
 
-		rcar_gen3_thermal_init(priv, tsc);
-	}
+	if (priv->ops.set_trips)
+		rcar_gen3_thermal_enable_irqs(priv);
 
 	return 0;
 }
@@ -625,7 +719,6 @@ static struct platform_driver rcar_gen3_thermal_driver = {
 		.of_match_table = rcar_gen3_thermal_dt_ids,
 	},
 	.probe		= rcar_gen3_thermal_probe,
-	.remove		= rcar_gen3_thermal_remove,
 };
 module_platform_driver(rcar_gen3_thermal_driver);
 
-- 
2.34.1


^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH v3] thermal: rcar_gen3: register the interrupt handlers after the zones
  2026-11-24  9:51   ` [PATCH v3] thermal: rcar_gen3: register the interrupt handlers after the zones Yaohan Li
@ 2026-11-24 13:15     ` Niklas Söderlund
  0 siblings, 0 replies; 10+ messages in thread
From: Niklas Söderlund @ 2026-11-24 13:15 UTC (permalink / raw)
  To: Yaohan Li
  Cc: Rafael J . Wysocki, Daniel Lezcano, Zhang Rui, Lukasz Luba,
	Magnus Damm, linux-renesas-soc, linux-pm, linux-kernel

Hi Yaohan,

This is a very large patch doing lots of different things and needs to 
be broken up. And I'm concerned about removing the remove() function in 
favor of devm_add_action_or_reset() design.

When I suggested altering the probe order I had in mind moving the init 
order of things to make sure it was cleaned up in the right order not 
making it depend on devm. Maybe that is not possible, but if this is the 
only solution it needs to be broke apart in discreet steps that can be 
reviewed.

On 2026-11-24 17:51:10 +0800, Yaohan Li wrote:
> The interrupt handlers are requested before the thermal zones are
> registered, and devres releases resources in the reverse order.  That
> leaves the same object exposed at both ends: during probe the handler
> exists while part of the sensors and zones are not initialised yet, and
> during teardown it outlives the zones it reports to.  The handler walks
> every sensor and calls thermal_zone_device_update() through tsc->zone,
> which is never cleared when the zone is unregistered.
> 
> Fix the order rather than the removal path.  Look the interrupts up
> first, keep the source masked while the sensors are initialised, and
> register the handlers only once every zone exists.  A devres action
> registered after the handlers masks the source again, and the source is
> enabled last, so the teardown order comes from devres itself: source
> off, handlers, hwmon, zones, MMIO mappings, runtime PM.  ->remove() has
> nothing left to do and is gone.
> 
> The thermal core copies the zone operations when a zone is registered,
> so whether set_trips() is available has to be decided before the zones
> are created.  Registering the handlers last moves that decision after
> the zones exist, so when the interrupts cannot be requested the zones
> are released and registered again without set_trips(): the driver keeps
> the polling configuration it had before instead of failing the probe.
> The zones are created and dropped once more in that path only.
> 
> rcar_gen3_thermal_init() clears REG_GEN3_IRQEN as well.  Nothing but
> rcar_gen3_thermal_enable_irqs() writes the register again, at the end of
> the probe and from ->resume(), so a source that the firmware or an
> earlier bind left enabled does not stay on while the zones are being
> created.
> 
> Check the sensor count before reading the fuses, because the fuse
> reader dereferences tscs[0].  Return -ENODEV when no sensor resources
> are present.
> 
> Signed-off-by: Yaohan Li <shunsheng1218@gmail.com>
> 
> ---
> Prepared against v7.4-rc1-1969-g666539eecdf4.
> 
> Compile tested only: arm64, aarch64-linux-gnu-gcc 11.4,
> CONFIG_ARCH_RENESAS, CONFIG_ARCH_RCAR_GEN3 and CONFIG_RCAR_GEN3_THERMAL.
> No board was available, so probe, the polling fallback, resume and
> teardown were not exercised on hardware, and there was no way to make an
> interrupt request fail on a real device.
> 
> Changes in v3:
>  - Register the interrupt handlers after the thermal zones and let devres
>    own the whole teardown, as suggested; ->remove() is gone.
>  - Look the interrupts up before the zones are registered, because the
>    thermal core copies the zone operations at registration time.
>  - Split the sensor initialisation from enabling the interrupt source and
>    re-enable it from ->resume().
>  - Clear REG_GEN3_IRQEN in the sensor initialisation, so that the source
>    stays masked until it is enabled on purpose.
>  - Keep the polling fallback when the handlers cannot be requested:
>    release the zones through a devres group and register them again
>    without set_trips().
>  - Check for a device without sensors before the fuses are read.
>  - Drop the has_irqs() helper.
> 
> AI assistance: prepared with Claude (Anthropic) as a coding assistant; the
> author reviewed the reasoning and takes responsibility for the content.
> ---
>  drivers/thermal/renesas/rcar_gen3_thermal.c | 197 ++++++++++++++------
>  1 file changed, 145 insertions(+), 52 deletions(-)
> 
> diff --git a/drivers/thermal/renesas/rcar_gen3_thermal.c b/drivers/thermal/renesas/rcar_gen3_thermal.c
> index 9c64cbc2ee67..7e4d6aa6e5dd 100644
> --- a/drivers/thermal/renesas/rcar_gen3_thermal.c
> +++ b/drivers/thermal/renesas/rcar_gen3_thermal.c
> @@ -103,6 +103,8 @@ struct rcar_gen3_thermal_tsc {
>  	int thcode[3];
>  };
>  
> +#define RCAR_GEN3_THERMAL_MAX_IRQ	2
> +
>  struct rcar_gen3_thermal_priv {
>  	struct rcar_gen3_thermal_tsc *tscs[TSC_MAX_NUM];
>  	struct thermal_zone_device_ops ops;
> @@ -110,6 +112,7 @@ struct rcar_gen3_thermal_priv {
>  	int ptat[3];
>  	int tj_t;
>  	const struct rcar_thermal_info *info;
> +	int irqs[RCAR_GEN3_THERMAL_MAX_IRQ];
>  };
>  
>  static inline u32 rcar_gen3_thermal_read(struct rcar_gen3_thermal_tsc *tsc,
> @@ -324,8 +327,7 @@ static bool rcar_gen3_thermal_read_fuses(struct rcar_gen3_thermal_priv *priv)
>  	return true;
>  }
>  
> -static void rcar_gen3_thermal_init(struct rcar_gen3_thermal_priv *priv,
> -				   struct rcar_gen3_thermal_tsc *tsc)
> +static void rcar_gen3_thermal_init(struct rcar_gen3_thermal_tsc *tsc)
>  {
>  	u32 reg_val;
>  
> @@ -335,11 +337,9 @@ static void rcar_gen3_thermal_init(struct rcar_gen3_thermal_priv *priv,
>  
>  	usleep_range(1000, 2000);
>  
> +	rcar_gen3_thermal_write(tsc, REG_GEN3_IRQEN, 0);
>  	rcar_gen3_thermal_write(tsc, REG_GEN3_IRQCTL, 0);
>  	rcar_gen3_thermal_write(tsc, REG_GEN3_IRQMSK, 0);
> -	if (priv->ops.set_trips)
> -		rcar_gen3_thermal_write(tsc, REG_GEN3_IRQEN,
> -					IRQ_TEMPD1 | IRQ_TEMP2);
>  
>  	reg_val = rcar_gen3_thermal_read(tsc, REG_GEN3_THCTR);
>  	reg_val |= THCTR_THSST;
> @@ -466,9 +466,9 @@ static const struct of_device_id rcar_gen3_thermal_dt_ids[] = {
>  };
>  MODULE_DEVICE_TABLE(of, rcar_gen3_thermal_dt_ids);
>  
> -static void rcar_gen3_thermal_remove(struct platform_device *pdev)
> +static void rcar_gen3_thermal_pm_put(void *data)
>  {
> -	struct device *dev = &pdev->dev;
> +	struct device *dev = data;
>  
>  	pm_runtime_put(dev);
>  	pm_runtime_disable(dev);
> @@ -481,25 +481,66 @@ static void rcar_gen3_hwmon_action(void *data)
>  	thermal_remove_hwmon_sysfs(zone);
>  }
>  
> -static int rcar_gen3_thermal_request_irqs(struct rcar_gen3_thermal_priv *priv,
> -					  struct platform_device *pdev)
> +static int rcar_gen3_thermal_register_zones(struct rcar_gen3_thermal_priv *priv,
> +					    struct device *dev)
>  {
> -	struct device *dev = &pdev->dev;
>  	unsigned int i;
> -	char *irqname;
> -	int ret, irq;
> +	int ret;
> +
> +	for (i = 0; i < priv->num_tscs; i++) {
> +		struct rcar_gen3_thermal_tsc *tsc = priv->tscs[i];
> +		struct thermal_zone_device *zone;
>  
> -	for (i = 0; i < 2; i++) {
> +		zone = devm_thermal_of_zone_register(dev, i, tsc, &priv->ops);
> +		if (IS_ERR(zone)) {
> +			dev_err(dev, "Sensor %u: Can't register thermal zone\n", i);
> +			return PTR_ERR(zone);
> +		}
> +		tsc->zone = zone;
> +
> +		ret = thermal_add_hwmon_sysfs(zone);
> +		if (ret)
> +			return ret;
> +
> +		ret = devm_add_action_or_reset(dev, rcar_gen3_hwmon_action, zone);
> +		if (ret)
> +			return ret;
> +	}
> +
> +	return 0;
> +}
> +
> +static int rcar_gen3_thermal_get_irqs(struct rcar_gen3_thermal_priv *priv,
> +				      struct platform_device *pdev)
> +{
> +	unsigned int i;
> +	int irq;
> +
> +	for (i = 0; i < RCAR_GEN3_THERMAL_MAX_IRQ; i++) {
>  		irq = platform_get_irq_optional(pdev, i);
>  		if (irq < 0)
>  			return irq;
>  
> +		priv->irqs[i] = irq;
> +	}
> +
> +	return 0;
> +}
> +
> +static int rcar_gen3_thermal_request_irqs(struct rcar_gen3_thermal_priv *priv,
> +					  struct device *dev)
> +{
> +	unsigned int i;
> +	char *irqname;
> +	int ret;
> +
> +	for (i = 0; i < RCAR_GEN3_THERMAL_MAX_IRQ; i++) {
>  		irqname = devm_kasprintf(dev, GFP_KERNEL, "%s:ch%d",
>  					 dev_name(dev), i);
>  		if (!irqname)
>  			return -ENOMEM;
>  
> -		ret = devm_request_threaded_irq(dev, irq, NULL,
> +		ret = devm_request_threaded_irq(dev, priv->irqs[i], NULL,
>  						rcar_gen3_thermal_irq,
>  						IRQF_ONESHOT, irqname, priv);
>  		if (ret)
> @@ -509,14 +550,34 @@ static int rcar_gen3_thermal_request_irqs(struct rcar_gen3_thermal_priv *priv,
>  	return 0;
>  }
>  
> +static void rcar_gen3_thermal_enable_irqs(struct rcar_gen3_thermal_priv *priv)
> +{
> +	unsigned int i;
> +
> +	for (i = 0; i < priv->num_tscs; i++)
> +		rcar_gen3_thermal_write(priv->tscs[i], REG_GEN3_IRQEN,
> +					IRQ_TEMPD1 | IRQ_TEMP2);
> +}
> +
> +static void rcar_gen3_thermal_disable_irqs(void *data)
> +{
> +	struct rcar_gen3_thermal_priv *priv = data;
> +	unsigned int i;
> +
> +	for (i = 0; i < priv->num_tscs; i++) {
> +		rcar_gen3_thermal_write(priv->tscs[i], REG_GEN3_IRQEN, 0);
> +		rcar_gen3_thermal_write(priv->tscs[i], REG_GEN3_IRQMSK, 0);
> +	}
> +}
> +
>  static int rcar_gen3_thermal_probe(struct platform_device *pdev)
>  {
>  	struct rcar_gen3_thermal_priv *priv;
>  	struct device *dev = &pdev->dev;
>  	struct resource *res;
> -	struct thermal_zone_device *zone;
>  	unsigned int i;
>  	int ret;
> +	void *group;
>  
>  	priv = devm_kzalloc_obj(dev, *priv);
>  	if (!priv)
> @@ -525,14 +586,25 @@ static int rcar_gen3_thermal_probe(struct platform_device *pdev)
>  	priv->ops = rcar_gen3_tz_of_ops;
>  
>  	priv->info = of_device_get_match_data(dev);
> +
>  	platform_set_drvdata(pdev, priv);
>  
> -	if (rcar_gen3_thermal_request_irqs(priv, pdev))
> +	/*
> +	 * The interrupts are looked up here but their handlers are registered
> +	 * only once the thermal zones exist.  The thermal core copies the zone
> +	 * operations when a zone is registered, so whether set_trips() is
> +	 * available has to be decided before the zones are created.
> +	 */
> +	if (rcar_gen3_thermal_get_irqs(priv, pdev))
>  		priv->ops.set_trips = NULL;
>  
>  	pm_runtime_enable(dev);
>  	pm_runtime_get_sync(dev);
>  
> +	ret = devm_add_action_or_reset(dev, rcar_gen3_thermal_pm_put, dev);
> +	if (ret)
> +		return ret;
> +
>  	for (i = 0; i < TSC_MAX_NUM; i++) {
>  		struct rcar_gen3_thermal_tsc *tsc;
>  
> @@ -541,22 +613,20 @@ static int rcar_gen3_thermal_probe(struct platform_device *pdev)
>  			break;
>  
>  		tsc = devm_kzalloc_obj(dev, *tsc);
> -		if (!tsc) {
> -			ret = -ENOMEM;
> -			goto error_unregister;
> -		}
> +		if (!tsc)
> +			return -ENOMEM;
>  
>  		tsc->priv = priv;
>  		tsc->base = devm_ioremap_resource(dev, res);
> -		if (IS_ERR(tsc->base)) {
> -			ret = PTR_ERR(tsc->base);
> -			goto error_unregister;
> -		}
> +		if (IS_ERR(tsc->base))
> +			return PTR_ERR(tsc->base);
>  
>  		priv->tscs[i] = tsc;
>  	}
>  
>  	priv->num_tscs = i;
> +	if (!priv->num_tscs)
> +		return -ENODEV;
>  
>  	if (!rcar_gen3_thermal_read_fuses(priv))
>  		dev_info(dev, "No calibration values fused, fallback to driver values\n");
> @@ -564,40 +634,64 @@ static int rcar_gen3_thermal_probe(struct platform_device *pdev)
>  	rcar_gen3_thermal_shared_coefs(priv);
>  
>  	for (i = 0; i < priv->num_tscs; i++) {
> -		struct rcar_gen3_thermal_tsc *tsc = priv->tscs[i];
> +		rcar_gen3_thermal_init(priv->tscs[i]);
> +		rcar_gen3_thermal_tsc_coefs(priv, priv->tscs[i]);
> +	}
>  
> -		rcar_gen3_thermal_init(priv, tsc);
> -		rcar_gen3_thermal_tsc_coefs(priv, tsc);
> +register_zones:
> +	group = devres_open_group(dev, NULL, GFP_KERNEL);
> +	if (!group)
> +		return -ENOMEM;
>  
> -		zone = devm_thermal_of_zone_register(dev, i, tsc, &priv->ops);
> -		if (IS_ERR(zone)) {
> -			dev_err(dev, "Sensor %u: Can't register thermal zone\n", i);
> -			ret = PTR_ERR(zone);
> -			goto error_unregister;
> +	ret = rcar_gen3_thermal_register_zones(priv, dev);
> +	if (ret)
> +		goto release_group;
> +
> +	/*
> +	 * Handlers last and the source enabled after them, so devres runs the
> +	 * teardown in the opposite order: source off, handlers, hwmon, zones,
> +	 * MMIO mappings, runtime PM.
> +	 */
> +	if (priv->ops.set_trips) {
> +		ret = rcar_gen3_thermal_request_irqs(priv, dev);
> +		if (ret) {
> +			dev_info(dev, "Failed to request interrupts (%d), polling instead\n",
> +				 ret);
> +
> +			/*
> +			 * The thermal core copies the zone operations when a
> +			 * zone is registered, so the zones above cannot be
> +			 * switched to polling afterwards.  Release them and
> +			 * register them again without set_trips().
> +			 */
> +			devres_release_group(dev, group);
> +
> +			for (i = 0; i < priv->num_tscs; i++)
> +				priv->tscs[i]->zone = NULL;
> +
> +			rcar_gen3_thermal_disable_irqs(priv);
> +			priv->ops.set_trips = NULL;
> +			goto register_zones;
>  		}
> -		tsc->zone = zone;
>  
> -		ret = thermal_add_hwmon_sysfs(tsc->zone);
> +		ret = devm_add_action_or_reset(dev,
> +					       rcar_gen3_thermal_disable_irqs,
> +					       priv);
>  		if (ret)
> -			goto error_unregister;
> +			goto release_group;
>  
> -		ret = devm_add_action_or_reset(dev, rcar_gen3_hwmon_action, zone);
> -		if (ret)
> -			goto error_unregister;
> -
> -		dev_info(dev, "Sensor %u: Loaded\n", i);
> +		rcar_gen3_thermal_enable_irqs(priv);
>  	}
>  
> -	if (!priv->num_tscs) {
> -		ret = -ENODEV;
> -		goto error_unregister;
> -	}
> +	devres_remove_group(dev, group);
>  
> -	return 0;
> +	for (i = 0; i < priv->num_tscs; i++)
> +		dev_info(dev, "Sensor %u: Loaded\n", i);
>  
> -error_unregister:
> -	rcar_gen3_thermal_remove(pdev);
> +	return 0;
>  
> +release_group:
> +	devres_release_group(dev, group);
>  	return ret;
>  }
>  
> @@ -606,11 +700,11 @@ static int rcar_gen3_thermal_resume(struct device *dev)
>  	struct rcar_gen3_thermal_priv *priv = dev_get_drvdata(dev);
>  	unsigned int i;
>  
> -	for (i = 0; i < priv->num_tscs; i++) {
> -		struct rcar_gen3_thermal_tsc *tsc = priv->tscs[i];
> +	for (i = 0; i < priv->num_tscs; i++)
> +		rcar_gen3_thermal_init(priv->tscs[i]);
>  
> -		rcar_gen3_thermal_init(priv, tsc);
> -	}
> +	if (priv->ops.set_trips)
> +		rcar_gen3_thermal_enable_irqs(priv);
>  
>  	return 0;
>  }
> @@ -625,7 +719,6 @@ static struct platform_driver rcar_gen3_thermal_driver = {
>  		.of_match_table = rcar_gen3_thermal_dt_ids,
>  	},
>  	.probe		= rcar_gen3_thermal_probe,
> -	.remove		= rcar_gen3_thermal_remove,
>  };
>  module_platform_driver(rcar_gen3_thermal_driver);
>  
> -- 
> 2.34.1
> 

-- 
Kind Regards,
Niklas Söderlund

^ permalink raw reply	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2026-11-24 13:15 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-17 14:53 [PATCH] thermal: rcar_gen3: quiesce the interrupt source before releasing it Yaohan Li
2026-11-18  9:31 ` Niklas Söderlund
2026-11-19  2:29 ` [PATCH v2] thermal: rcar_gen3: release the IRQ handlers before the zones are freed Yaohan Li
2026-11-22 14:48   ` Niklas Söderlund
2026-11-23  2:21     ` Yaohan Li
2026-11-23  6:47       ` Niklas Söderlund
2026-11-24  2:22         ` Yaohan Li
2026-11-24  6:40           ` Niklas Söderlund
2026-11-24  9:51   ` [PATCH v3] thermal: rcar_gen3: register the interrupt handlers after the zones Yaohan Li
2026-11-24 13:15     ` Niklas Söderlund

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®