Re: [PATCH v2] thermal: rcar_gen3: release the IRQ handlers before the zones are freed

From: Niklas Söderlund

Date: Sun Nov 22 2026 - 09:48:26 EST


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@xxxxxxxxx>
>
> ---
> 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