Linux Power Management development
 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; 18+ 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 related	[flat|nested] 18+ 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; 18+ 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] 18+ 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; 18+ 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 related	[flat|nested] 18+ 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; 18+ 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] 18+ 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; 18+ 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] 18+ 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; 18+ 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] 18+ 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; 18+ 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] 18+ 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; 18+ 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] 18+ 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
  2026-11-25  3:03     ` [PATCH v4 0/3] thermal: rcar_gen3: fix the interrupt handler lifetimes Yaohan Li
  1 sibling, 2 replies; 18+ 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 related	[flat|nested] 18+ 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
  2026-11-25  3:03     ` [PATCH v4 0/3] thermal: rcar_gen3: fix the interrupt handler lifetimes Yaohan Li
  1 sibling, 0 replies; 18+ 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] 18+ messages in thread

* [PATCH v4 0/3] thermal: rcar_gen3: fix the interrupt handler lifetimes
  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
@ 2026-11-25  3:03     ` Yaohan Li
  2026-11-25  3:03       ` [PATCH v4 1/3] thermal: rcar_gen3: check for sensors before reading the fuses Yaohan Li
                         ` (2 more replies)
  1 sibling, 3 replies; 18+ messages in thread
From: Yaohan Li @ 2026-11-25  3:03 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

This series fixes the order in which rcar_gen3_thermal_probe() and
->remove() create and release the interrupt handlers and the thermal
zones.

Patch 1 moves an existing check; it stands alone.

Patch 2 releases the handlers from ->remove(), before the runtime PM
reference is dropped and before devres unregisters the zones, and
undoes a partial interrupt request instead of leaving the first handler
behind.

Patch 3 requests the interrupts with IRQF_NO_AUTOEN, initialises every
sensor before the request, and enables the interrupts and their sources
only once all the thermal zones are registered.

The driver keeps its ->remove() callback and the existing polling
fallback when an interrupt cannot be requested.

Changes in v4:
 - Split into three patches.
 - Keep ->remove() and release the handlers explicitly instead of
   handing the teardown to devres.
 - Request the interrupts with IRQF_NO_AUTOEN and enable them after the
   zones are registered.

Compile tested only: arm64, aarch64-linux-gnu-gcc 11.4,
CONFIG_ARCH_RENESAS and CONFIG_RCAR_GEN3_THERMAL.  No R-Car board was
available, so probe, the polling fallback, resume and teardown were not
exercised on hardware.

AI assistance: prepared with Claude (Anthropic) as a coding assistant; the
author reviewed the reasoning and takes responsibility for the content.

Yaohan Li (3):
  thermal: rcar_gen3: check for sensors before reading the fuses
  thermal: rcar_gen3: release the interrupt handlers from ->remove()
  thermal: rcar_gen3: do not enable the interrupts before the zones
    exist

 drivers/thermal/renesas/rcar_gen3_thermal.c | 96 +++++++++++++++------
 1 file changed, 72 insertions(+), 24 deletions(-)

-- 
2.34.1


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

* [PATCH v4 1/3] thermal: rcar_gen3: check for sensors before reading the fuses
  2026-11-25  3:03     ` [PATCH v4 0/3] thermal: rcar_gen3: fix the interrupt handler lifetimes Yaohan Li
@ 2026-11-25  3:03       ` Yaohan Li
  2026-11-25  6:04         ` Niklas Söderlund
  2026-11-25  3:03       ` [PATCH v4 2/3] thermal: rcar_gen3: release the interrupt handlers from ->remove() Yaohan Li
  2026-11-25  3:03       ` [PATCH v4 3/3] thermal: rcar_gen3: do not enable the interrupts before the zones exist Yaohan Li
  2 siblings, 1 reply; 18+ messages in thread
From: Yaohan Li @ 2026-11-25  3:03 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

rcar_gen3_thermal_read_fuses() dereferences tscs[0], but the check that
rejects a device without sensor resources comes after the thermal zones
have been registered.  Move the check up, so that a device exposing no
memory resource is rejected before the fuse reader touches tscs[0].

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 and CONFIG_RCAR_GEN3_THERMAL.

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 | 9 ++++-----
 1 file changed, 4 insertions(+), 5 deletions(-)

diff --git a/drivers/thermal/renesas/rcar_gen3_thermal.c b/drivers/thermal/renesas/rcar_gen3_thermal.c
index 9c64cbc2ee67..a96f02d4a8ad 100644
--- a/drivers/thermal/renesas/rcar_gen3_thermal.c
+++ b/drivers/thermal/renesas/rcar_gen3_thermal.c
@@ -557,6 +557,10 @@ static int rcar_gen3_thermal_probe(struct platform_device *pdev)
 	}
 
 	priv->num_tscs = i;
+	if (!priv->num_tscs) {
+		ret = -ENODEV;
+		goto error_unregister;
+	}
 
 	if (!rcar_gen3_thermal_read_fuses(priv))
 		dev_info(dev, "No calibration values fused, fallback to driver values\n");
@@ -588,11 +592,6 @@ static int rcar_gen3_thermal_probe(struct platform_device *pdev)
 		dev_info(dev, "Sensor %u: Loaded\n", i);
 	}
 
-	if (!priv->num_tscs) {
-		ret = -ENODEV;
-		goto error_unregister;
-	}
-
 	return 0;
 
 error_unregister:
-- 
2.34.1


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

* [PATCH v4 2/3] thermal: rcar_gen3: release the interrupt handlers from ->remove()
  2026-11-25  3:03     ` [PATCH v4 0/3] thermal: rcar_gen3: fix the interrupt handler lifetimes Yaohan Li
  2026-11-25  3:03       ` [PATCH v4 1/3] thermal: rcar_gen3: check for sensors before reading the fuses Yaohan Li
@ 2026-11-25  3:03       ` Yaohan Li
  2026-11-25  7:33         ` Niklas Söderlund
  2026-11-25  3:03       ` [PATCH v4 3/3] thermal: rcar_gen3: do not enable the interrupts before the zones exist Yaohan Li
  2 siblings, 1 reply; 18+ messages in thread
From: Yaohan Li @ 2026-11-25  3:03 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, and
devres releases the managed resources in the reverse order, so the
handlers outlive the zones they report to.  The handler walks every
sensor and calls thermal_zone_device_update() through tsc->zone, which
is not cleared when the zone is unregistered.  Masking the interrupt
sources neither waits for a threaded handler that is already running
nor removes the action.

Release the handlers from ->remove(), which runs before the runtime PM
reference is dropped and before devres unregisters the zones.
free_irq() also waits for a running handler, and after it returns the
handler cannot be invoked again.

Record the interrupt numbers as they are requested, so that a partial
request is undone as well: if the second handler cannot be registered,
the first one is released and the driver falls back to polling, as
before.

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 and CONFIG_RCAR_GEN3_THERMAL.

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 | 38 ++++++++++++++++++---
 1 file changed, 33 insertions(+), 5 deletions(-)

diff --git a/drivers/thermal/renesas/rcar_gen3_thermal.c b/drivers/thermal/renesas/rcar_gen3_thermal.c
index a96f02d4a8ad..37348f4413b6 100644
--- a/drivers/thermal/renesas/rcar_gen3_thermal.c
+++ b/drivers/thermal/renesas/rcar_gen3_thermal.c
@@ -107,6 +107,8 @@ struct rcar_gen3_thermal_priv {
 	struct rcar_gen3_thermal_tsc *tscs[TSC_MAX_NUM];
 	struct thermal_zone_device_ops ops;
 	unsigned int num_tscs;
+	int irqs[2];
+	unsigned int num_irqs;
 	int ptat[3];
 	int tj_t;
 	const struct rcar_thermal_info *info;
@@ -466,9 +468,25 @@ 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_free_irqs(struct device *dev,
+					struct rcar_gen3_thermal_priv *priv)
+{
+	while (priv->num_irqs)
+		devm_free_irq(dev, priv->irqs[--priv->num_irqs], priv);
+}
+
 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;
+
+	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);
+	}
+
+	rcar_gen3_thermal_free_irqs(dev, priv);
 
 	pm_runtime_put(dev);
 	pm_runtime_disable(dev);
@@ -491,22 +509,32 @@ static int rcar_gen3_thermal_request_irqs(struct rcar_gen3_thermal_priv *priv,
 
 	for (i = 0; i < 2; i++) {
 		irq = platform_get_irq_optional(pdev, i);
-		if (irq < 0)
-			return irq;
+		if (irq < 0) {
+			ret = irq;
+			goto release;
+		}
 
 		irqname = devm_kasprintf(dev, GFP_KERNEL, "%s:ch%d",
 					 dev_name(dev), i);
-		if (!irqname)
-			return -ENOMEM;
+		if (!irqname) {
+			ret = -ENOMEM;
+			goto release;
+		}
 
 		ret = devm_request_threaded_irq(dev, irq, NULL,
 						rcar_gen3_thermal_irq,
 						IRQF_ONESHOT, irqname, priv);
 		if (ret)
-			return ret;
+			goto release;
+
+		priv->irqs[priv->num_irqs++] = irq;
 	}
 
 	return 0;
+
+release:
+	rcar_gen3_thermal_free_irqs(dev, priv);
+	return ret;
 }
 
 static int rcar_gen3_thermal_probe(struct platform_device *pdev)
-- 
2.34.1


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

* [PATCH v4 3/3] thermal: rcar_gen3: do not enable the interrupts before the zones exist
  2026-11-25  3:03     ` [PATCH v4 0/3] thermal: rcar_gen3: fix the interrupt handler lifetimes Yaohan Li
  2026-11-25  3:03       ` [PATCH v4 1/3] thermal: rcar_gen3: check for sensors before reading the fuses Yaohan Li
  2026-11-25  3:03       ` [PATCH v4 2/3] thermal: rcar_gen3: release the interrupt handlers from ->remove() Yaohan Li
@ 2026-11-25  3:03       ` Yaohan Li
  2 siblings, 0 replies; 18+ messages in thread
From: Yaohan Li @ 2026-11-25  3:03 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

rcar_gen3_thermal_probe() requests the interrupt handlers before the
sensors are initialised.  Once num_tscs is set, the handler can access
the sensor registers while sensor initialisation and thermal zone
registration are still in progress.

The handler checks whether tsc->zone is NULL, but this does not prevent
it from accessing the sensor registers during initialisation.  Masking
the device interrupt sources alone does not prevent delivery of an
interrupt that is already pending.

Request the interrupts with IRQF_NO_AUTOEN, so that the handlers cannot
run until the driver enables them, and initialise every sensor before
the request.  Enable the interrupts and their sources only after all
the thermal zones are registered.

rcar_gen3_thermal_resume() re-initialises the sensors and the sources;
the enable state of the interrupt itself is left to probe.

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 and CONFIG_RCAR_GEN3_THERMAL.

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 | 53 ++++++++++++++-------
 1 file changed, 37 insertions(+), 16 deletions(-)

diff --git a/drivers/thermal/renesas/rcar_gen3_thermal.c b/drivers/thermal/renesas/rcar_gen3_thermal.c
index 37348f4413b6..e6682c5759aa 100644
--- a/drivers/thermal/renesas/rcar_gen3_thermal.c
+++ b/drivers/thermal/renesas/rcar_gen3_thermal.c
@@ -326,8 +326,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;
 
@@ -337,11 +336,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;
@@ -350,6 +347,15 @@ static void rcar_gen3_thermal_init(struct rcar_gen3_thermal_priv *priv,
 	usleep_range(1000, 2000);
 }
 
+static void rcar_gen3_thermal_enable_sources(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 const struct rcar_gen3_thermal_fuse_info rcar_gen3_thermal_fuse_info_gen3 = {
 	.ptat = { REG_GEN3_PTAT1, REG_GEN3_PTAT2, REG_GEN3_PTAT3 },
 	.thcode = { REG_GEN3_THCODE1, REG_GEN3_THCODE2, REG_GEN3_THCODE3 },
@@ -523,7 +529,8 @@ static int rcar_gen3_thermal_request_irqs(struct rcar_gen3_thermal_priv *priv,
 
 		ret = devm_request_threaded_irq(dev, irq, NULL,
 						rcar_gen3_thermal_irq,
-						IRQF_ONESHOT, irqname, priv);
+						IRQF_ONESHOT | IRQF_NO_AUTOEN,
+						irqname, priv);
 		if (ret)
 			goto release;
 
@@ -555,9 +562,6 @@ static int rcar_gen3_thermal_probe(struct platform_device *pdev)
 	priv->info = of_device_get_match_data(dev);
 	platform_set_drvdata(pdev, priv);
 
-	if (rcar_gen3_thermal_request_irqs(priv, pdev))
-		priv->ops.set_trips = NULL;
-
 	pm_runtime_enable(dev);
 	pm_runtime_get_sync(dev);
 
@@ -596,10 +600,20 @@ 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);
+	/*
+	 * The handler walks the sensor array, so every sensor has to be
+	 * initialised before the request.  IRQF_NO_AUTOEN keeps the
+	 * handlers from running until the zones exist.
+	 */
+	if (rcar_gen3_thermal_request_irqs(priv, pdev))
+		priv->ops.set_trips = NULL;
+
+	for (i = 0; i < priv->num_tscs; i++) {
+		struct rcar_gen3_thermal_tsc *tsc = priv->tscs[i];
 
 		zone = devm_thermal_of_zone_register(dev, i, tsc, &priv->ops);
 		if (IS_ERR(zone)) {
@@ -620,6 +634,13 @@ static int rcar_gen3_thermal_probe(struct platform_device *pdev)
 		dev_info(dev, "Sensor %u: Loaded\n", i);
 	}
 
+	if (priv->ops.set_trips) {
+		for (i = 0; i < priv->num_irqs; i++)
+			enable_irq(priv->irqs[i]);
+
+		rcar_gen3_thermal_enable_sources(priv);
+	}
+
 	return 0;
 
 error_unregister:
@@ -633,11 +654,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_sources(priv);
 
 	return 0;
 }
-- 
2.34.1


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

* Re: [PATCH v4 1/3] thermal: rcar_gen3: check for sensors before reading the fuses
  2026-11-25  3:03       ` [PATCH v4 1/3] thermal: rcar_gen3: check for sensors before reading the fuses Yaohan Li
@ 2026-11-25  6:04         ` Niklas Söderlund
  0 siblings, 0 replies; 18+ messages in thread
From: Niklas Söderlund @ 2026-11-25  6:04 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-25 11:03:25 +0800, Yaohan Li wrote:
> rcar_gen3_thermal_read_fuses() dereferences tscs[0], but the check that
> rejects a device without sensor resources comes after the thermal zones
> have been registered.  Move the check up, so that a device exposing no
> memory resource is rejected before the fuse reader touches tscs[0].

This commit message makes little sens, how about:

When checking if a device have the thermal calibration fuses set in 
rcar_gen3_thermal_read_fuses() the first TSC is used to read the THSCP 
register. This read is done even if no TSCs are initialized and if so 
will lead to a null pointer dereference.

Fix this my checking that we have at least on TSC initialized directly 
after probing each TSC. Before adding thermal calibration fuses support 
this was geared against that each loop was over priv->num_tscs but that 
assumption is not true for rcar_gen3_thermal_read_fuses().

Fixes: c3131bd5586d ("thermal: rcar_gen3_thermal: Read calibration from hardware

> 
> 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 and CONFIG_RCAR_GEN3_THERMAL.
> 
> 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 | 9 ++++-----
>  1 file changed, 4 insertions(+), 5 deletions(-)
> 
> diff --git a/drivers/thermal/renesas/rcar_gen3_thermal.c b/drivers/thermal/renesas/rcar_gen3_thermal.c
> index 9c64cbc2ee67..a96f02d4a8ad 100644
> --- a/drivers/thermal/renesas/rcar_gen3_thermal.c
> +++ b/drivers/thermal/renesas/rcar_gen3_thermal.c
> @@ -557,6 +557,10 @@ static int rcar_gen3_thermal_probe(struct platform_device *pdev)
>  	}
>  
>  	priv->num_tscs = i;
> +	if (!priv->num_tscs) {
> +		ret = -ENODEV;
> +		goto error_unregister;
> +	}
>  
>  	if (!rcar_gen3_thermal_read_fuses(priv))
>  		dev_info(dev, "No calibration values fused, fallback to driver values\n");
> @@ -588,11 +592,6 @@ static int rcar_gen3_thermal_probe(struct platform_device *pdev)
>  		dev_info(dev, "Sensor %u: Loaded\n", i);
>  	}
>  
> -	if (!priv->num_tscs) {
> -		ret = -ENODEV;
> -		goto error_unregister;
> -	}
> -
>  	return 0;
>  
>  error_unregister:
> -- 
> 2.34.1
> 

-- 
Kind Regards,
Niklas Söderlund

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

* Re: [PATCH v4 2/3] thermal: rcar_gen3: release the interrupt handlers from ->remove()
  2026-11-25  3:03       ` [PATCH v4 2/3] thermal: rcar_gen3: release the interrupt handlers from ->remove() Yaohan Li
@ 2026-11-25  7:33         ` Niklas Söderlund
  2026-11-25 11:18           ` zhang san
  0 siblings, 1 reply; 18+ messages in thread
From: Niklas Söderlund @ 2026-11-25  7:33 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,

Thanks for your work.

On 2026-11-25 11:03:26 +0800, Yaohan Li wrote:
> The interrupt handlers are requested before the thermal zones, and
> devres releases the managed resources in the reverse order, so the
> handlers outlive the zones they report to.  The handler walks every
> sensor and calls thermal_zone_device_update() through tsc->zone, which
> is not cleared when the zone is unregistered.  Masking the interrupt
> sources neither waits for a threaded handler that is already running
> nor removes the action.
> 
> Release the handlers from ->remove(), which runs before the runtime PM
> reference is dropped and before devres unregisters the zones.
> free_irq() also waits for a running handler, and after it returns the
> handler cannot be invoked again.
> 
> Record the interrupt numbers as they are requested, so that a partial
> request is undone as well: if the second handler cannot be registered,
> the first one is released and the driver falls back to polling, as
> before.
> 
> 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 and CONFIG_RCAR_GEN3_THERMAL.
> 
> 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 | 38 ++++++++++++++++++---
>  1 file changed, 33 insertions(+), 5 deletions(-)
> 
> diff --git a/drivers/thermal/renesas/rcar_gen3_thermal.c b/drivers/thermal/renesas/rcar_gen3_thermal.c
> index a96f02d4a8ad..37348f4413b6 100644
> --- a/drivers/thermal/renesas/rcar_gen3_thermal.c
> +++ b/drivers/thermal/renesas/rcar_gen3_thermal.c
> @@ -107,6 +107,8 @@ struct rcar_gen3_thermal_priv {
>  	struct rcar_gen3_thermal_tsc *tscs[TSC_MAX_NUM];
>  	struct thermal_zone_device_ops ops;
>  	unsigned int num_tscs;
> +	int irqs[2];

What happened to RCAR_GEN3_THERMAL_MAX_IRQ ?

Please stop thorowing this code at an LLM it comes up with new code 
ignore past ideas.

> +	unsigned int num_irqs;
>  	int ptat[3];
>  	int tj_t;
>  	const struct rcar_thermal_info *info;
> @@ -466,9 +468,25 @@ 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_free_irqs(struct device *dev,
> +					struct rcar_gen3_thermal_priv *priv)
> +{
> +	while (priv->num_irqs)

Please use a for loop not modifyig priv->num_irqs, this looks dangerous.

> +		devm_free_irq(dev, priv->irqs[--priv->num_irqs], priv);
> +}
> +
>  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;
> +
> +	for (i = 0; i < priv->num_tscs; i++) {

Declare loop variable in for loop, where possible.

> +		rcar_gen3_thermal_write(priv->tscs[i], REG_GEN3_IRQEN, 0);
> +		rcar_gen3_thermal_write(priv->tscs[i], REG_GEN3_IRQMSK, 0);
> +	}
> +
> +	rcar_gen3_thermal_free_irqs(dev, priv);
>  
>  	pm_runtime_put(dev);
>  	pm_runtime_disable(dev);
> @@ -491,22 +509,32 @@ static int rcar_gen3_thermal_request_irqs(struct rcar_gen3_thermal_priv *priv,
>  
>  	for (i = 0; i < 2; i++) {
>  		irq = platform_get_irq_optional(pdev, i);
> -		if (irq < 0)
> -			return irq;
> +		if (irq < 0) {
> +			ret = irq;
> +			goto release;
> +		}

This changes the behavior of the driver...

Please stop using an LLM to code this. As you don't have hardware to 
test with throwing LLM spaghetti over the wall and see if it sticks is 
getting tedious. Please read, understand and write the code yourself as 
coding for gear you can't test is hard.

I will not review another set of LLM slop, especially as the good parts 
like RCAR_GEN3_THERMAL_MAX_IRQ are dropped between revisions.

Nacked-by: Niklas Söderlund <niklas.soderlund+renesas@ragnatech.se>

>  
>  		irqname = devm_kasprintf(dev, GFP_KERNEL, "%s:ch%d",
>  					 dev_name(dev), i);
> -		if (!irqname)
> -			return -ENOMEM;
> +		if (!irqname) {
> +			ret = -ENOMEM;
> +			goto release;
> +		}
>  
>  		ret = devm_request_threaded_irq(dev, irq, NULL,
>  						rcar_gen3_thermal_irq,
>  						IRQF_ONESHOT, irqname, priv);
>  		if (ret)
> -			return ret;
> +			goto release;
> +
> +		priv->irqs[priv->num_irqs++] = irq;
>  	}
>  
>  	return 0;
> +
> +release:
> +	rcar_gen3_thermal_free_irqs(dev, priv);
> +	return ret;
>  }
>  
>  static int rcar_gen3_thermal_probe(struct platform_device *pdev)
> -- 
> 2.34.1
> 

-- 
Kind Regards,
Niklas Söderlund

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

* Re: [PATCH v4 2/3] thermal: rcar_gen3: release the interrupt handlers from ->remove()
  2026-11-25  7:33         ` Niklas Söderlund
@ 2026-11-25 11:18           ` zhang san
  2026-11-25 11:31             ` Niklas Söderlund
  0 siblings, 1 reply; 18+ messages in thread
From: zhang san @ 2026-11-25 11:18 UTC (permalink / raw)
  To: niklas.soderlund
  Cc: rafael, daniel.lezcano, rui.zhang, lukasz.luba, magnus.damm,
	linux-renesas-soc, linux-pm, linux-kernel

Hi Niklas,

Fair enough, and sorry for wasting your review time. Dropping
RCAR_GEN3_THERMAL_MAX_IRQ and your earlier style feedback across revisions
was my fault.

I'm dropping patches 2/3 and 3/3 and won't send any further IRQ lifetime
changes here unless I've worked through the driver properly on my own.
I'll look at patch 1/3 separately on its own using your feedback.

Thanks,
Yaohan

On Wed, 25 Nov 2026 08:33:42 +0100, "Niklas Söderlund"
<niklas.soderlund@ragnatech.se> wrote:
> Hi Yaohan,
>
> Thanks for your work.
>
> On 2026-11-25 11:03:26 +0800, Yaohan Li wrote:
> > The interrupt handlers are requested before the thermal zones, and
> > devres releases the managed resources in the reverse order, so the
> > handlers outlive the zones they report to. The handler walks every
> > sensor and calls thermal_zone_device_update() through tsc->zone, which
> > is not cleared when the zone is unregistered. Masking the interrupt
> > sources neither waits for a threaded handler that is already running
> > nor removes the action.
> >
> > Release the handlers from ->remove(), which runs before the runtime PM
> > reference is dropped and before devres unregisters the zones.
> > free_irq() also waits for a running handler, and after it returns the
> > handler cannot be invoked again.
> >
> > Record the interrupt numbers as they are requested, so that a partial
> > request is undone as well: if the second handler cannot be registered,
> > the first one is released and the driver falls back to polling, as
> > before.
> >
> > 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 and CONFIG_RCAR_GEN3_THERMAL.
> >
> > 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 | 38 ++++++++++++++++++---
> > 1 file changed, 33 insertions(+), 5 deletions(-)
> >
> > diff --git a/drivers/thermal/renesas/rcar_gen3_thermal.c b/drivers/thermal/renesas/rcar_gen3_thermal.c
> > index a96f02d4a8ad..37348f4413b6 100644
> > --- a/drivers/thermal/renesas/rcar_gen3_thermal.c
> > +++ b/drivers/thermal/renesas/rcar_gen3_thermal.c
> > @@ -107,6 +107,8 @@ struct rcar_gen3_thermal_priv {
> > struct rcar_gen3_thermal_tsc *tscs[TSC_MAX_NUM];
> > struct thermal_zone_device_ops ops;
> > unsigned int num_tscs;
> > + int irqs[2];
>
> What happened to RCAR_GEN3_THERMAL_MAX_IRQ ?
>
> Please stop thorowing this code at an LLM it comes up with new code
> ignore past ideas.
>
> > + unsigned int num_irqs;
> > int ptat[3];
> > int tj_t;
> > const struct rcar_thermal_info *info;
> > @@ -466,9 +468,25 @@ 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_free_irqs(struct device *dev,
> > + struct rcar_gen3_thermal_priv *priv)
> > +{
> > + while (priv->num_irqs)
>
> Please use a for loop not modifyig priv->num_irqs, this looks dangerous.
>
> > + devm_free_irq(dev, priv->irqs[--priv->num_irqs], priv);
> > +}
> > +
> > 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;
> > +
> > + for (i = 0; i < priv->num_tscs; i++) {
>
> Declare loop variable in for loop, where possible.
>
> > + rcar_gen3_thermal_write(priv->tscs[i], REG_GEN3_IRQEN, 0);
> > + rcar_gen3_thermal_write(priv->tscs[i], REG_GEN3_IRQMSK, 0);
> > + }
> > +
> > + rcar_gen3_thermal_free_irqs(dev, priv);
> >
> > pm_runtime_put(dev);
> > pm_runtime_disable(dev);
> > @@ -491,22 +509,32 @@ static int rcar_gen3_thermal_request_irqs(struct rcar_gen3_thermal_priv *priv,
> >
> > for (i = 0; i < 2; i++) {
> > irq = platform_get_irq_optional(pdev, i);
> > - if (irq < 0)
> > - return irq;
> > + if (irq < 0) {
> > + ret = irq;
> > + goto release;
> > + }
>
> This changes the behavior of the driver...
>
> Please stop using an LLM to code this. As you don't have hardware to
> test with throwing LLM spaghetti over the wall and see if it sticks is
> getting tedious. Please read, understand and write the code yourself as
> coding for gear you can't test is hard.
>
> I will not review another set of LLM slop, especially as the good parts
> like RCAR_GEN3_THERMAL_MAX_IRQ are dropped between revisions.
>
> Nacked-by: Niklas Söderlund <niklas.soderlund+renesas@ragnatech.se>
>
> >
> > irqname = devm_kasprintf(dev, GFP_KERNEL, "%s:ch%d",
> > dev_name(dev), i);
> > - if (!irqname)
> > - return -ENOMEM;
> > + if (!irqname) {
> > + ret = -ENOMEM;
> > + goto release;
> > + }
> >
> > ret = devm_request_threaded_irq(dev, irq, NULL,
> > rcar_gen3_thermal_irq,
> > IRQF_ONESHOT, irqname, priv);
> > if (ret)
> > - return ret;
> > + goto release;
> > +
> > + priv->irqs[priv->num_irqs++] = irq;
> > }
> >
> > return 0;
> > +
> > +release:
> > + rcar_gen3_thermal_free_irqs(dev, priv);
> > + return ret;
> > }
> >
> > static int rcar_gen3_thermal_probe(struct platform_device *pdev)
> > --
> > 2.34.1
> >
>
> --
> Kind Regards,
> Niklas Söderlund

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

* Re: [PATCH v4 2/3] thermal: rcar_gen3: release the interrupt handlers from ->remove()
  2026-11-25 11:18           ` zhang san
@ 2026-11-25 11:31             ` Niklas Söderlund
  0 siblings, 0 replies; 18+ messages in thread
From: Niklas Söderlund @ 2026-11-25 11:31 UTC (permalink / raw)
  To: zhang san
  Cc: rafael, daniel.lezcano, rui.zhang, lukasz.luba, magnus.damm,
	linux-renesas-soc, linux-pm, linux-kernel

Hi Yaohan,

On 2026-11-25 05:18:20 -0600, zhang san wrote:
> Hi Niklas,
> 
> Fair enough, and sorry for wasting your review time. Dropping
> RCAR_GEN3_THERMAL_MAX_IRQ and your earlier style feedback across revisions
> was my fault.

I'm happy to keep reviewing your work. And I think the issue you are 
working on is valid and worth fixing. I don't see it as wasted as at the 
very least patch 1/3 is good code and more or less ready to go and fixes 
a real issue.

I was just, a bit blunt, asking for you to work thru the changes 
yourself a bit more. From my side it reads as a copy-past from LLM and 
having to point out the same thing in multiple iterations is what's 
tedious as an LLM can't learn, but you can! And I'm happy to support you 
if you want to.

> 
> I'm dropping patches 2/3 and 3/3 and won't send any further IRQ lifetime
> changes here unless I've worked through the driver properly on my own.
> I'll look at patch 1/3 separately on its own using your feedback.

Thanks for breaking out 1/3 it is good and fixes a real issue. As stated 
above if you want to keep working on 2/3 and 3/3 with more human power* 
then LLM, I'm happy to keep reviewing it.

* I don't care if you use an LLM or not, but you need to workthru the
  LLM suggestions some more before posting it IMHO.

> 
> Thanks,
> Yaohan
> 
> On Wed, 25 Nov 2026 08:33:42 +0100, "Niklas Söderlund"
> <niklas.soderlund@ragnatech.se> wrote:
> > Hi Yaohan,
> >
> > Thanks for your work.
> >
> > On 2026-11-25 11:03:26 +0800, Yaohan Li wrote:
> > > The interrupt handlers are requested before the thermal zones, and
> > > devres releases the managed resources in the reverse order, so the
> > > handlers outlive the zones they report to. The handler walks every
> > > sensor and calls thermal_zone_device_update() through tsc->zone, which
> > > is not cleared when the zone is unregistered. Masking the interrupt
> > > sources neither waits for a threaded handler that is already running
> > > nor removes the action.
> > >
> > > Release the handlers from ->remove(), which runs before the runtime PM
> > > reference is dropped and before devres unregisters the zones.
> > > free_irq() also waits for a running handler, and after it returns the
> > > handler cannot be invoked again.
> > >
> > > Record the interrupt numbers as they are requested, so that a partial
> > > request is undone as well: if the second handler cannot be registered,
> > > the first one is released and the driver falls back to polling, as
> > > before.
> > >
> > > 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 and CONFIG_RCAR_GEN3_THERMAL.
> > >
> > > 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 | 38 ++++++++++++++++++---
> > > 1 file changed, 33 insertions(+), 5 deletions(-)
> > >
> > > diff --git a/drivers/thermal/renesas/rcar_gen3_thermal.c b/drivers/thermal/renesas/rcar_gen3_thermal.c
> > > index a96f02d4a8ad..37348f4413b6 100644
> > > --- a/drivers/thermal/renesas/rcar_gen3_thermal.c
> > > +++ b/drivers/thermal/renesas/rcar_gen3_thermal.c
> > > @@ -107,6 +107,8 @@ struct rcar_gen3_thermal_priv {
> > > struct rcar_gen3_thermal_tsc *tscs[TSC_MAX_NUM];
> > > struct thermal_zone_device_ops ops;
> > > unsigned int num_tscs;
> > > + int irqs[2];
> >
> > What happened to RCAR_GEN3_THERMAL_MAX_IRQ ?
> >
> > Please stop thorowing this code at an LLM it comes up with new code
> > ignore past ideas.
> >
> > > + unsigned int num_irqs;
> > > int ptat[3];
> > > int tj_t;
> > > const struct rcar_thermal_info *info;
> > > @@ -466,9 +468,25 @@ 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_free_irqs(struct device *dev,
> > > + struct rcar_gen3_thermal_priv *priv)
> > > +{
> > > + while (priv->num_irqs)
> >
> > Please use a for loop not modifyig priv->num_irqs, this looks dangerous.
> >
> > > + devm_free_irq(dev, priv->irqs[--priv->num_irqs], priv);
> > > +}
> > > +
> > > 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;
> > > +
> > > + for (i = 0; i < priv->num_tscs; i++) {
> >
> > Declare loop variable in for loop, where possible.
> >
> > > + rcar_gen3_thermal_write(priv->tscs[i], REG_GEN3_IRQEN, 0);
> > > + rcar_gen3_thermal_write(priv->tscs[i], REG_GEN3_IRQMSK, 0);
> > > + }
> > > +
> > > + rcar_gen3_thermal_free_irqs(dev, priv);
> > >
> > > pm_runtime_put(dev);
> > > pm_runtime_disable(dev);
> > > @@ -491,22 +509,32 @@ static int rcar_gen3_thermal_request_irqs(struct rcar_gen3_thermal_priv *priv,
> > >
> > > for (i = 0; i < 2; i++) {
> > > irq = platform_get_irq_optional(pdev, i);
> > > - if (irq < 0)
> > > - return irq;
> > > + if (irq < 0) {
> > > + ret = irq;
> > > + goto release;
> > > + }
> >
> > This changes the behavior of the driver...
> >
> > Please stop using an LLM to code this. As you don't have hardware to
> > test with throwing LLM spaghetti over the wall and see if it sticks is
> > getting tedious. Please read, understand and write the code yourself as
> > coding for gear you can't test is hard.
> >
> > I will not review another set of LLM slop, especially as the good parts
> > like RCAR_GEN3_THERMAL_MAX_IRQ are dropped between revisions.
> >
> > Nacked-by: Niklas Söderlund <niklas.soderlund+renesas@ragnatech.se>
> >
> > >
> > > irqname = devm_kasprintf(dev, GFP_KERNEL, "%s:ch%d",
> > > dev_name(dev), i);
> > > - if (!irqname)
> > > - return -ENOMEM;
> > > + if (!irqname) {
> > > + ret = -ENOMEM;
> > > + goto release;
> > > + }
> > >
> > > ret = devm_request_threaded_irq(dev, irq, NULL,
> > > rcar_gen3_thermal_irq,
> > > IRQF_ONESHOT, irqname, priv);
> > > if (ret)
> > > - return ret;
> > > + goto release;
> > > +
> > > + priv->irqs[priv->num_irqs++] = irq;
> > > }
> > >
> > > return 0;
> > > +
> > > +release:
> > > + rcar_gen3_thermal_free_irqs(dev, priv);
> > > + return ret;
> > > }
> > >
> > > static int rcar_gen3_thermal_probe(struct platform_device *pdev)
> > > --
> > > 2.34.1
> > >
> >
> > --
> > Kind Regards,
> > Niklas Söderlund

-- 
Kind Regards,
Niklas Söderlund

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

end of thread, other threads:[~2026-11-25 11:31 UTC | newest]

Thread overview: 18+ 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
2026-11-25  3:03     ` [PATCH v4 0/3] thermal: rcar_gen3: fix the interrupt handler lifetimes Yaohan Li
2026-11-25  3:03       ` [PATCH v4 1/3] thermal: rcar_gen3: check for sensors before reading the fuses Yaohan Li
2026-11-25  6:04         ` Niklas Söderlund
2026-11-25  3:03       ` [PATCH v4 2/3] thermal: rcar_gen3: release the interrupt handlers from ->remove() Yaohan Li
2026-11-25  7:33         ` Niklas Söderlund
2026-11-25 11:18           ` zhang san
2026-11-25 11:31             ` Niklas Söderlund
2026-11-25  3:03       ` [PATCH v4 3/3] thermal: rcar_gen3: do not enable the interrupts before the zones exist Yaohan Li

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