From: "Niklas Söderlund" <niklas.soderlund@ragnatech.se>
To: Yaohan Li <shunsheng1218@gmail.com>
Cc: rafael@kernel.org, daniel.lezcano@kernel.org,
rui.zhang@intel.com, lukasz.luba@arm.com,
geert+renesas@glider.be, magnus.damm@gmail.com,
linux-renesas-soc@vger.kernel.org, linux-pm@vger.kernel.org,
linux-kernel@vger.kernel.org
Subject: Re: [PATCH v2] thermal: rcar_gen3: release the IRQ handlers before the zones are freed
Date: Sun, 22 Nov 2026 15:48:13 +0100 [thread overview]
Message-ID: <20261122144813.GB3898744@ragnatech.se> (raw)
In-Reply-To: <20261119022926.1493582-1-shunsheng1218@gmail.com>
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
prev parent reply other threads:[~2026-11-22 14:48 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
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 [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261122144813.GB3898744@ragnatech.se \
--to=niklas.soderlund@ragnatech.se \
--cc=daniel.lezcano@kernel.org \
--cc=geert+renesas@glider.be \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-pm@vger.kernel.org \
--cc=linux-renesas-soc@vger.kernel.org \
--cc=lukasz.luba@arm.com \
--cc=magnus.damm@gmail.com \
--cc=rafael@kernel.org \
--cc=rui.zhang@intel.com \
--cc=shunsheng1218@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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®