Re: [PATCH] thermal: rcar_gen3: quiesce the interrupt source before releasing it

From: Niklas Söderlund

Date: Wed Nov 18 2026 - 04:31:18 EST


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