mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
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, edubezval@gmail.com
Subject: Re: [PATCH] thermal/drivers/rcar_thermal: wait for IRQs before cancelling work
Date: Wed, 18 Nov 2026 10:00:14 +0100	[thread overview]
Message-ID: <20261118090014.GB2300348@ragnatech.se> (raw)
In-Reply-To: <20261118072321.1259639-1-shunsheng1218@gmail.com>

Hi Yaohan,

Thanks for your work.

On 2026-11-18 15:23:21 +0800, Yaohan Li wrote:
> rcar_thermal_remove() masks the interrupt sources and cancels the
> delayed work of each sensor.  rcar_thermal_irq() reads INTMSK and STR
> under common->lock, drops the lock, and only then walks the sensors and
> queues priv->work for every sensor whose status changed.
> 
> A handler that sampled the status before the mask was written can
> therefore reach queue_delayed_work() after cancel_delayed_work_sync()
> has returned.  The work stays pending past the end of ->remove(), devres
> then frees common, priv and the thermal zone, and rcar_thermal_work()
> runs 300 ms later on freed memory.
> 
> Waiting for the handler is not enough on its own: rcar_thermal_work()
> calls rcar_thermal_irq_enable() once it has updated the temperature, so
> a work that is still running when the removal starts re-enables its
> source and undoes the mask that ->remove() has just written.
> 
> Record the shutdown in rcar_thermal_common under common->lock and make
> _rcar_thermal_irq_ctrl() refuse to enable anything once that state is
> visible.  The removal then publishes the shutdown, masks all sources,
> waits for the handlers with synchronize_irq() and only then cancels the
> work, so neither a source nor a work item can be brought back after it
> returns.
> 
> Fixes: 697ee786f15d ("thermal: rcar_thermal: Prevent doing work after unbind")
> 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.
> 
> Testing: compile tested only (arm64, gcc 11.4).  No R-Car hardware was
> available, so the unbind race was not run.
>  drivers/thermal/renesas/rcar_thermal.c | 43 +++++++++++++++++++++++++-
>  1 file changed, 42 insertions(+), 1 deletion(-)
> 
> diff --git a/drivers/thermal/renesas/rcar_thermal.c b/drivers/thermal/renesas/rcar_thermal.c
> index af6d09a75bb6..1d6ee37968e2 100644
> --- a/drivers/thermal/renesas/rcar_thermal.c
> +++ b/drivers/thermal/renesas/rcar_thermal.c
> @@ -39,11 +39,17 @@
>  /* THSSR */
>  #define CTEMP	0x3f
>  
> +/* .nirqs of every supported chip is at most this */
> +#define RCAR_THERMAL_MAX_IRQ	2
> +
>  struct rcar_thermal_common {
>  	void __iomem *base;
>  	struct device *dev;
>  	struct list_head head;
>  	spinlock_t lock;
> +	bool shutdown;
> +	int irqs[RCAR_THERMAL_MAX_IRQ];
> +	int nirqs;
>  };
>  
>  struct rcar_thermal_chip {
> @@ -301,6 +307,15 @@ static void _rcar_thermal_irq_ctrl(struct rcar_thermal_priv *priv, int enable)
>  
>  	spin_lock_irqsave(&common->lock, flags);
>  
> +	/*
> +	 * rcar_thermal_work() re-enables its source when it finishes, so a
> +	 * work that was still running when the device went away would undo
> +	 * the mask that rcar_thermal_remove() writes.  Once the shutdown
> +	 * state is visible nothing may enable the sources again.
> +	 */
> +	if (common->shutdown)
> +		enable = 0;
> +
>  	rcar_thermal_common_bset(common, INTMSK, mask, enable ? 0 : mask);
>  
>  	spin_unlock_irqrestore(&common->lock, flags);
> @@ -376,9 +391,33 @@ static void rcar_thermal_remove(struct platform_device *pdev)
>  	struct rcar_thermal_common *common = platform_get_drvdata(pdev);
>  	struct device *dev = &pdev->dev;
>  	struct rcar_thermal_priv *priv;
> +	unsigned long flags;
> +	int i;
>  
> -	rcar_thermal_for_each_priv(priv, common) {
> +	/*
> +	 * Publish the shutdown state before anything else: a work that is
> +	 * still running re-enables its interrupt source on its way out, and
> +	 * has to be prevented from undoing the mask written below.
> +	 */
> +	spin_lock_irqsave(&common->lock, flags);
> +	common->shutdown = true;
> +	spin_unlock_irqrestore(&common->lock, flags);

Adding a shutdown flag seems like a lot of complexity for somehting like 
this. Would not being more clever here avoid that? Maybe something like 
this,

    /* No more work will be able to queued hence the irq can not rearm 
     * itself from the top half.
     */
    disable_work_sync()

    /* Disable any new interrupts in case they currently are enabled. */
    rcar_thermal_irq_disable()

    /* Wait for any bottom half interrupt to complete. */
    synchronize_irq()

    Now there can't be any races!

For this flag,

Nacked-by: Niklas Söderlund <niklas.soderlund+renesas@ragnatech.se>

> +
> +	/*
> +	 * Mask the sources, then wait for a handler that sampled the status
> +	 * before the mask was written.  The handler drops common->lock
> +	 * before it queues the work, so without the wait it could queue
> +	 * priv->work after cancel_delayed_work_sync() has returned and leave
> +	 * the work running on a priv that devres frees once ->remove() has
> +	 * finished.
> +	 */
> +	rcar_thermal_for_each_priv(priv, common)
>  		rcar_thermal_irq_disable(priv);
> +
> +	for (i = 0; i < common->nirqs; i++)
> +		synchronize_irq(common->irqs[i]);
> +
> +	rcar_thermal_for_each_priv(priv, common) {
>  		cancel_delayed_work_sync(&priv->work);
>  		if (priv->chip->use_of_thermal)
>  			thermal_remove_hwmon_sysfs(priv->zone);
> @@ -449,6 +488,8 @@ static int rcar_thermal_probe(struct platform_device *pdev)
>  		if (ret)
>  			goto error_unregister;
>  
> +		common->irqs[common->nirqs++] = irq;
> +
>  		/* update ENR bits */
>  		if (chip->irq_per_ch)
>  			enr_bits |= 1 << i;
> -- 
> 2.34.1
> 

-- 
Kind Regards,
Niklas Söderlund

      reply	other threads:[~2026-11-18  9:00 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-18  7:23 Yaohan Li
2026-11-18  9:00 ` 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=20261118090014.GB2300348@ragnatech.se \
    --to=niklas.soderlund@ragnatech.se \
    --cc=daniel.lezcano@kernel.org \
    --cc=edubezval@gmail.com \
    --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®