Re: [PATCH v3] thermal/drivers/rcar_thermal: disable the work before masking the interrupt

From: Niklas Söderlund

Date: Tue Nov 24 2026 - 08:25:30 EST


Hi Yaohan,

Thanks for your work.

On 2026-11-24 10:22:19 +0800, Yaohan Li wrote:
> rcar_thermal_remove() masks the interrupt sources and then cancels the
> delayed work of each sensor. rcar_thermal_irq() drops common->lock before
> it walks the sensors and queues priv->work for those whose status changed,
> so a handler that read the status before the mask was written can still
> queue the work after cancel_delayed_work_sync() has returned. The work
> then runs after ->remove() has finished and devres has freed common, priv
> and the thermal zone.
>
> Waiting for the handler alone is not enough either: rcar_thermal_work()
> re-enables its interrupt source once it has updated the temperature, so a
> work that is still running when the removal starts undoes the mask that
> ->remove() has just written.
>
> Disable the work before the source is masked. That makes the
> queue_delayed_work() calls in rcar_thermal_irq() no-ops and guarantees
> that the re-enable in rcar_thermal_work() has already happened, so the
> mask that follows cannot be undone.
>
> For the handler itself, synchronize_irq() is not enough on a shared line:
> it waits for the invocation that is running, but the line stays registered
> and another user of it can take the interrupt again afterwards, which calls
> our handler once more - with the sensors and the zone already unregistered.
> devm_free_irq() removes the handler from the shared descriptor first and
> waits for the invocation that is in progress second, so after it returns
> neither a running nor a new invocation can touch the driver state.
>
> Every sensor that the removal walks has been through INIT_DELAYED_WORK()
> before it is linked into common->head, so no uninitialised work is touched.
>
> Fixes: 697ee786f15d ("thermal: rcar_thermal: Prevent doing work after unbind")
> Signed-off-by: Yaohan Li <shunsheng1218@xxxxxxxxx>
>
> ---
> Prepared against v7.4-rc1-1969-g666539eecdf4.
>
> Compile tested: arm64, aarch64-linux-gnu-gcc 11.4, CONFIG_RCAR_THERMAL=y.
>
> The ordering was exercised by a controlled test on a KASAN kernel built from
> this tree (x86_64, QEMU/KVM; 17 checks, all passing). The test uses the real
> delayed work, workqueue, wait and signal machinery and a simulated interrupt
> line shared by two handlers (irq_sim):
>
> - a work queued after cancel_delayed_work_sync() has returned is accepted
> and runs; the same queue attempt after disable_delayed_work_sync() is
> rejected and the work never runs;
> - a work that is running when the mask is written re-enables its source
> afterwards, so the mask is undone; disabling the work first and masking
> afterwards leaves the mask in place;
> - after synchronize_irq() returns, another user of the shared line raises
> it again, our handler is invoked and queues the work, which then runs;
> after devm_free_irq() the handler is not invoked again, while the other
> user still receives the line.
>
> The test re-implements this removal ordering rather than compiling the
> driver, which would need a real platform device, MMIO and a thermal zone, so
> rcar_thermal's own removal path was not executed. No rcar hardware was
> available and no race injection beyond those constructed interleavings was
> run.
>
> AI assistance: prepared with Claude (Anthropic) as a coding assistant; the
> author reviewed the reasoning and takes responsibility for the content.
>
> Changes in v3:
> - Reword the RCAR_THERMAL_MAX_IRQ comment without referring to .nirqs.
> - Declare the loop index in the for statement and make it unsigned.
> - Reduce both comments in rcar_thermal_remove() to one line each.
> - Explain in the commit message why a shared line needs devm_free_irq()
> and not synchronize_irq(), and point at the test that exercises it.
>
> Changes in v2:
> - Drop the shutdown flag and the separate IRQ count. Niklas asked for less
> state, so the removal now disables the delayed work before the source is
> masked and unregisters the shared IRQ instead of waiting for it once.
> - Keep the interrupt numbers in an array whose unused slots stay negative.
> ---
> drivers/thermal/renesas/rcar_thermal.c | 26 ++++++++++++++++++++++++--
> 1 file changed, 24 insertions(+), 2 deletions(-)
>
> diff --git a/drivers/thermal/renesas/rcar_thermal.c b/drivers/thermal/renesas/rcar_thermal.c
> index af6d09a75bb6..374c953471e0 100644
> --- a/drivers/thermal/renesas/rcar_thermal.c
> +++ b/drivers/thermal/renesas/rcar_thermal.c
> @@ -39,11 +39,15 @@
> /* THSSR */
> #define CTEMP 0x3f
>
> +/* Maximum number of interrupts supported by any of the chips */
> +#define RCAR_THERMAL_MAX_IRQ 2
> +
> struct rcar_thermal_common {
> void __iomem *base;
> struct device *dev;
> struct list_head head;
> spinlock_t lock;
> + int irqs[RCAR_THERMAL_MAX_IRQ];
> };
>
> struct rcar_thermal_chip {
> @@ -377,9 +381,22 @@ static void rcar_thermal_remove(struct platform_device *pdev)
> struct device *dev = &pdev->dev;
> struct rcar_thermal_priv *priv;
>
> - rcar_thermal_for_each_priv(priv, common) {
> + /* Drain the work before masking the source it can re-enable. */
> + rcar_thermal_for_each_priv(priv, common)
> + disable_delayed_work_sync(&priv->work);
> +
> + rcar_thermal_for_each_priv(priv, common)
> rcar_thermal_irq_disable(priv);
> - cancel_delayed_work_sync(&priv->work);
> +
> + /* Unregister shared IRQ handlers before releasing the zones. */
> + for (unsigned int i = 0; i < RCAR_THERMAL_MAX_IRQ; i++) {
> + if (common->irqs[i] < 0)
> + continue;
> + devm_free_irq(dev, common->irqs[i], common);
> + common->irqs[i] = -ENOENT;
> + }

I don't understand why this last loop with devm_free_irq() is needed.

First we disable drain and prevent any new work being scheduled on the
interrupt top-half with disable_delayed_work_sync(). Then we disable the
the interrupts with rcar_thermal_irq_disable(). So why do we need to
explicitly call devm_free_irq()?

If it is to sync that we have no pending interrupts for the bottom half?
If so would it not be cleaner to add a call to sync the interrupts in
the loop calling rcar_thermal_irq_disable() ?

> +
> + rcar_thermal_for_each_priv(priv, common) {
> if (priv->chip->use_of_thermal)
> thermal_remove_hwmon_sysfs(priv->zone);
> else
> @@ -413,6 +430,9 @@ static int rcar_thermal_probe(struct platform_device *pdev)
> spin_lock_init(&common->lock);
> common->dev = dev;
>
> + for (i = 0; i < RCAR_THERMAL_MAX_IRQ; i++)
> + common->irqs[i] = -ENOENT;
> +
> pm_runtime_enable(dev);
> pm_runtime_get_sync(dev);
>
> @@ -449,6 +469,8 @@ static int rcar_thermal_probe(struct platform_device *pdev)
> if (ret)
> goto error_unregister;
>
> + common->irqs[i] = irq;
> +
> /* update ENR bits */
> if (chip->irq_per_ch)
> enr_bits |= 1 << i;
> --
> 2.34.1
>

--
Kind Regards,
Niklas Söderlund