* [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; 3+ messages in thread
From: Yaohan Li @ 2026-11-17 14:53 UTC (permalink / raw)
To: niklas.soderlund, rafael, daniel.lezcano
Cc: rui.zhang, lukasz.luba, geert+renesas, magnus.damm,
linux-renesas-soc, linux-pm, linux-kernel
rcar_gen3_thermal_remove() only drops the runtime PM reference. The two
threaded interrupts are left to devres, which releases them after the
TSC objects and the thermal zones have already been freed, and the
device keeps raising interrupts because nothing clears REG_GEN3_IRQEN.
rcar_gen3_thermal_irq() walks priv->tscs[i], reads REG_GEN3_IRQSTR
through tsc->base and calls thermal_zone_device_update(tsc->zone), so a
handler that the hardirq has already woken can run after those objects
are gone.
Disable the interrupt source for every TSC first, then release the
interrupt handlers, and only then drop the runtime PM reference.
REG_GEN3_IRQEN is only ever written by rcar_gen3_thermal_init(); a
concurrent set_trips() writes REG_GEN3_IRQMSK alone, so it cannot undo
the disable.
Remember the interrupt numbers that were successfully requested, so that
the partial-probe path, which also ends in .remove(), only releases what
was actually registered.
Signed-off-by: Yaohan Li <shunsheng1218@gmail.com>
---
Prepared against v7.4-rc1-1969-g666539eecdf4.
AI assistance: the analysis, the change and its review notes were
prepared with Claude (Anthropic) as a coding assistant. The author
reviewed the reasoning and takes responsibility for the content.
This revision also clears REG_GEN3_IRQEN for every TSC before the
handlers are released. rcar_gen3_thermal_init() is the only writer of
IRQEN; set_trips() writes IRQMSK only, so a concurrent trip update
cannot re-enable the interrupt source.
Testing: compile tested only (arm64, gcc 11.4). No R-Car hardware was
available. The partial-probe path that also ends in .remove() was not
exercised.
---
drivers/thermal/renesas/rcar_gen3_thermal.c | 25 +++++++++++++++++++++
1 file changed, 25 insertions(+)
diff --git a/drivers/thermal/renesas/rcar_gen3_thermal.c b/drivers/thermal/renesas/rcar_gen3_thermal.c
index 9c64cbc2ee67..35bb49915a75 100644
--- a/drivers/thermal/renesas/rcar_gen3_thermal.c
+++ b/drivers/thermal/renesas/rcar_gen3_thermal.c
@@ -110,6 +110,8 @@ struct rcar_gen3_thermal_priv {
int ptat[3];
int tj_t;
const struct rcar_thermal_info *info;
+ int irqs[2];
+ unsigned int num_irqs;
};
static inline u32 rcar_gen3_thermal_read(struct rcar_gen3_thermal_tsc *tsc,
@@ -468,7 +470,28 @@ MODULE_DEVICE_TABLE(of, rcar_gen3_thermal_dt_ids);
static void rcar_gen3_thermal_remove(struct platform_device *pdev)
{
+ struct rcar_gen3_thermal_priv *priv = platform_get_drvdata(pdev);
struct device *dev = &pdev->dev;
+ unsigned int i;
+
+ /*
+ * Disable the interrupt source while the device is still runtime
+ * resumed. REG_GEN3_IRQEN is only ever written by
+ * rcar_gen3_thermal_init(); a concurrent set_trips() writes
+ * REG_GEN3_IRQMSK alone, so it cannot re-enable the source here.
+ */
+ for (i = 0; i < priv->num_tscs; i++) {
+ rcar_gen3_thermal_write(priv->tscs[i], REG_GEN3_IRQEN, 0);
+ rcar_gen3_thermal_write(priv->tscs[i], REG_GEN3_IRQMSK, 0);
+ }
+
+ /*
+ * Release the interrupts before the TSCs and zones are freed:
+ * free_irq() waits for an executing threaded handler, and the handler
+ * dereferences both priv->tscs[i] and priv->tscs[i]->zone.
+ */
+ for (i = 0; i < priv->num_irqs; i++)
+ devm_free_irq(dev, priv->irqs[i], priv);
pm_runtime_put(dev);
pm_runtime_disable(dev);
@@ -504,6 +527,8 @@ static int rcar_gen3_thermal_request_irqs(struct rcar_gen3_thermal_priv *priv,
IRQF_ONESHOT, irqname, priv);
if (ret)
return ret;
+
+ priv->irqs[priv->num_irqs++] = irq;
}
return 0;
--
2.34.1
^ permalink raw reply [flat|nested] 3+ 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; 3+ 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] 3+ 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
1 sibling, 0 replies; 3+ messages in thread
From: Yaohan Li @ 2026-11-19 2:29 UTC (permalink / raw)
To: niklas.soderlund, rafael, daniel.lezcano
Cc: rui.zhang, lukasz.luba, geert+renesas, magnus.damm,
linux-renesas-soc, linux-pm, linux-kernel
The interrupt handlers are requested before the thermal zones are
registered, so devres releases the zones first. When ->remove() returns,
the zones are gone while the handlers are still installed until devres
gets to them, and a handler that runs in that window walks priv->tscs[]
and updates a zone that no longer exists. The source keeps asserting
because nothing clears REG_GEN3_IRQEN before the hardware goes away.
synchronize_irq() alone does not close that window. It waits for a
handler that is running at that moment, but the zones are freed before
the handlers are, so an interrupt that is raised just after the wait, or
that is still pending in the interrupt controller, runs a handler
against freed memory. Removing the handler is what actually closes it,
so ->remove() masks the source and calls devm_free_irq() for every
interrupt that was requested, while the device is still runtime
resumed.
The interrupt numbers live in an array whose unused slots stay negative,
so the partial probe path - which also ends in ->remove() - releases
exactly what was requested without a second counter to keep in sync.
Signed-off-by: Yaohan Li <shunsheng1218@gmail.com>
---
Compile tested only: arm64 defconfig, aarch64-linux-gnu-gcc 11.4. The
board was not available, so the removal path was not exercised and no
race injection was run.
Changes in v2:
- Mask the source and release the handlers in ->remove() instead of
relying on devres plus synchronize_irq(), and explain in the message
why a single synchronize_irq() cannot close the window.
- Keep the interrupt numbers in an array whose unused slots stay
negative instead of a separate count.
- Use a loop-local index and a named maximum instead of literals.
---
drivers/thermal/renesas/rcar_gen3_thermal.c | 33 ++++++++++++++++++++-
1 file changed, 32 insertions(+), 1 deletion(-)
diff --git a/drivers/thermal/renesas/rcar_gen3_thermal.c b/drivers/thermal/renesas/rcar_gen3_thermal.c
index 9c64cbc2ee67..2a6890d2a5da 100644
--- a/drivers/thermal/renesas/rcar_gen3_thermal.c
+++ b/drivers/thermal/renesas/rcar_gen3_thermal.c
@@ -103,6 +103,8 @@ struct rcar_gen3_thermal_tsc {
int thcode[3];
};
+#define RCAR_GEN3_THERMAL_MAX_IRQ 2
+
struct rcar_gen3_thermal_priv {
struct rcar_gen3_thermal_tsc *tscs[TSC_MAX_NUM];
struct thermal_zone_device_ops ops;
@@ -110,6 +112,7 @@ struct rcar_gen3_thermal_priv {
int ptat[3];
int tj_t;
const struct rcar_thermal_info *info;
+ int irqs[RCAR_GEN3_THERMAL_MAX_IRQ];
};
static inline u32 rcar_gen3_thermal_read(struct rcar_gen3_thermal_tsc *tsc,
@@ -468,8 +471,31 @@ MODULE_DEVICE_TABLE(of, rcar_gen3_thermal_dt_ids);
static void rcar_gen3_thermal_remove(struct platform_device *pdev)
{
+ struct rcar_gen3_thermal_priv *priv = platform_get_drvdata(pdev);
struct device *dev = &pdev->dev;
+ /*
+ * Disable the interrupt source while the device is still runtime
+ * resumed. REG_GEN3_IRQEN is only ever written by
+ * rcar_gen3_thermal_init(); a concurrent set_trips() writes
+ * REG_GEN3_IRQMSK alone, so it cannot re-enable the source here.
+ */
+ for (unsigned int i = 0; i < priv->num_tscs; i++) {
+ rcar_gen3_thermal_write(priv->tscs[i], REG_GEN3_IRQEN, 0);
+ rcar_gen3_thermal_write(priv->tscs[i], REG_GEN3_IRQMSK, 0);
+ }
+
+ /*
+ * Release the handlers here instead of leaving that to devres. They
+ * were requested before the thermal zones, so devres would free the
+ * zones first, and a handler that runs in between would use a zone
+ * that is already gone. free_irq() also waits for the threaded
+ * handler that may be running right now.
+ */
+ for (unsigned int i = 0; i < ARRAY_SIZE(priv->irqs); i++)
+ if (priv->irqs[i] >= 0)
+ devm_free_irq(dev, priv->irqs[i], priv);
+
pm_runtime_put(dev);
pm_runtime_disable(dev);
}
@@ -489,7 +515,7 @@ static int rcar_gen3_thermal_request_irqs(struct rcar_gen3_thermal_priv *priv,
char *irqname;
int ret, irq;
- for (i = 0; i < 2; i++) {
+ for (i = 0; i < RCAR_GEN3_THERMAL_MAX_IRQ; i++) {
irq = platform_get_irq_optional(pdev, i);
if (irq < 0)
return irq;
@@ -504,6 +530,8 @@ static int rcar_gen3_thermal_request_irqs(struct rcar_gen3_thermal_priv *priv,
IRQF_ONESHOT, irqname, priv);
if (ret)
return ret;
+
+ priv->irqs[i] = irq;
}
return 0;
@@ -525,6 +553,9 @@ static int rcar_gen3_thermal_probe(struct platform_device *pdev)
priv->ops = rcar_gen3_tz_of_ops;
priv->info = of_device_get_match_data(dev);
+
+ for (i = 0; i < ARRAY_SIZE(priv->irqs); i++)
+ priv->irqs[i] = -ENOENT;
platform_set_drvdata(pdev, priv);
if (rcar_gen3_thermal_request_irqs(priv, pdev))
--
2.34.1
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2026-11-19 2:29 UTC | newest]
Thread overview: 3+ 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
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®