Linux Power Management development
 help / color / mirror / Atom feed
From: Miquel Raynal <miquel.raynal@bootlin.com>
To: Yaohan Li <shunsheng1218@gmail.com>
Cc: rafael@kernel.org,  daniel.lezcano@kernel.org,
	 rui.zhang@intel.com, lukasz.luba@arm.com,
	 linux-pm@vger.kernel.org, linux-kernel@vger.kernel.org,
	 edubezval@gmail.com
Subject: Re: [PATCH v2] thermal/drivers/armada: request the overheat IRQ before enabling the source
Date: Thu, 19 Nov 2026 12:11:23 +0100	[thread overview]
Message-ID: <87o6bldqjo.fsf@bootlin.com> (raw)
In-Reply-To: <20261119022911.1493538-1-shunsheng1218@gmail.com> (Yaohan Li's message of "Thu, 19 Nov 2026 10:29:11 +0800")

On 19/11/2026 at 10:29:11 +08, Yaohan Li <shunsheng1218@gmail.com> wrote:

> armada_thermal_probe() requests the overheat interrupt before it registers
> the thermal zones.  Devres releases resources in the reverse order, so the
> zones are unregistered first and the interrupt is freed afterwards: a
> handler that is still running, or that fires in the window between the
> two, passes a zone that has already been destroyed to
> thermal_zone_device_update().
>
> Request the interrupt after the zone loop instead.  The handler is then
> freed before the zones it updates, and the interrupt is only requested
> when a critical trip point was found, which is the only case in which the
> driver has an overheat source to enable.
>
> The interrupt stays optional: a device without one, or with an invalid
> IRQ number, keeps working without overheat detection instead of failing
> to probe.
>
> The source has to be enabled after the handler is registered, so
> armada_configure_overheat_int() only programs the thresholds now and
> armada_thermal_probe() enables the source once the request has succeeded.
> A failed request then cannot leave an interrupt enabled that nobody
> handles.  The source is masked at probe time as well, in case the firmware
> left it on.
>
> The handler polls its sensor once per second until the temperature is back
> below the hysteresis, and devres waits for it in free_irq().  ->remove()
> masks the source and sets a shutdown flag so that the poll gives up
> instead of keeping the removal blocked until the sensor cools down.
>
> Fixes: 879d7362d1d0 ("thermal: armada: add overheat interrupt support")
> Signed-off-by: Yaohan Li <shunsheng1218@gmail.com>
>
> ---
> Prepared against v7.4-rc1-1969-g666539eecdf4.
>
> Compile tested only: arm64, aarch64-linux-gnu-gcc 11.4,
> CONFIG_ARMADA_THERMAL=y and CONFIG_ARCH_MVEBU=y.  No hardware was available,
> so the removal path was not exercised and no race injection 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 v2:
>  - Request the interrupt after the thermal zones are registered instead of
>    freeing it from ->remove(); Miquel asked for the smaller shape, so the
>    helper and the stored IRQ number are gone.
>  - Enable the source only after the handler is registered, so a failed
>    request cannot leave an interrupt enabled that nobody handles.
>  - Mask the source at probe time, but only on the variants that describe the
>    overheat interrupt as a DFX cause.
> ---
>  drivers/thermal/armada_thermal.c | 69 ++++++++++++++++++++++++--------
>  1 file changed, 52 insertions(+), 17 deletions(-)
>
> diff --git a/drivers/thermal/armada_thermal.c b/drivers/thermal/armada_thermal.c
> index 83672f95b011..23f820415e56 100644
> --- a/drivers/thermal/armada_thermal.c
> +++ b/drivers/thermal/armada_thermal.c
> @@ -68,6 +68,7 @@ struct armada_thermal_priv {
>  	struct thermal_zone_device *overheat_sensor;
>  	int interrupt_source;
>  	int current_channel;
> +	bool shutdown;
>  	long current_threshold;
>  	long current_hysteresis;
>  };
> @@ -297,12 +298,19 @@ static void armada_enable_overheat_interrupt(struct armada_thermal_priv *priv)
>  	regmap_write(priv->syscon, data->syscon_control1_off, reg);
>  }
>  
> -static void __maybe_unused
> -armada_disable_overheat_interrupt(struct armada_thermal_priv *priv)
> +static void armada_disable_overheat_interrupt(struct armada_thermal_priv *priv)
>  {
>  	struct armada_thermal_data *data = priv->data;
>  	u32 reg;
>  
> +	/*
> +	 * Only the variants that describe the overheat interrupt as a DFX
> +	 * cause have this source; leave the control register alone on the
> +	 * others.
> +	 */
> +	if (!data->dfx_overheat_irq)
> +		return;
> +

That's over defensive. This case is unreachable.

>  	regmap_read(priv->syscon, data->syscon_control1_off, &reg);
>  	FIELD_MODIFY(CONTROL1_TSEN_INT_EN, &reg, 0);
>  	regmap_write(priv->syscon, data->syscon_control1_off, reg);
> @@ -538,7 +546,7 @@ static irqreturn_t armada_overheat_isr_thread(int irq, void *blob)
>  		mutex_unlock(&priv->update_lock);
>  		if (ret)
>  			goto enable_irq;
> -	} while (temperature >= low_threshold);
> +	} while (temperature >= low_threshold && !READ_ONCE(priv->shutdown));
>  
>  	regmap_read(priv->syscon, priv->data->dfx_irq_cause_off, &dummy);
>  
> @@ -802,7 +810,6 @@ static int armada_configure_overheat_int(struct armada_thermal_priv *priv,
>  	armada_set_overheat_thresholds(priv, temperature, 0);
>  	priv->overheat_sensor = tz;
>  	priv->interrupt_source = sensor_id;
> -	armada_enable_overheat_interrupt(priv);
>  
>  	return 0;
>  }
> @@ -891,20 +898,16 @@ static int armada_thermal_probe(struct platform_device *pdev)
>  	drvdata->data.priv = priv;
>  	platform_set_drvdata(pdev, drvdata);
>  
> +	/*
> +	 * The firmware may have left the overheat source enabled.  Keep it
> +	 * masked until a handler is in place.
> +	 */
> +	armada_disable_overheat_interrupt(priv);
> +
>  	irq = platform_get_irq(pdev, 0);
>  	if (irq == -EPROBE_DEFER)
>  		return irq;
>  
> -	/* The overheat interrupt feature is not mandatory */
> -	if (irq > 0) {
> -		ret = devm_request_threaded_irq(&pdev->dev, irq,
> -						armada_overheat_isr,
> -						armada_overheat_isr_thread,
> -						0, NULL, priv);
> -		if (ret)
> -			return ret;
> -	}
> -
>  	/*
>  	 * There is one channel for the IC and one per CPU (if any), each
>  	 * channel has one sensor.
> @@ -937,9 +940,29 @@ static int armada_thermal_probe(struct platform_device *pdev)
>  			armada_configure_overheat_int(priv, tz, sensor->id);
>  	}
>  
> -	/* Just complain if no overheat interrupt was set up */
> -	if (!priv->overheat_sensor)
> +	/*
> +	 * The overheat interrupt is optional: a device without one keeps
> +	 * working, and an invalid IRQ number must not be requested.

Here you change a bit the behaviour of the driver. ENXIO is a valid
error indeed, but any other negative value may fail the probe. I don't
mind to keep it that way (not failing in case of wrong IRQ), but a line
in the commit log about it is necessary.

> +	 */
> +	if (irq <= 0 || !priv->overheat_sensor) {
>  		dev_warn(&pdev->dev, "Overheat interrupt not available\n");
> +		return 0;
> +	}

I personally don't like early returns when everything is fine. I would
prefer that you just keep the whole irq handling inside an if clause.

> +
> +	/*
> +	 * Request the interrupt only now that the thermal zones are
> +	 * registered: devres releases what was registered last first, so the
> +	 * handler is freed before the zones it updates.  The source stays
> +	 * masked until the handler is in place, so the device cannot raise an
> +	 * interrupt that nobody handles.
> +	 */

This comment is not needed, please drop.

> +	ret = devm_request_threaded_irq(&pdev->dev, irq, armada_overheat_isr,
> +					armada_overheat_isr_thread, 0, NULL,
> +					priv);
> +	if (ret)
> +		return ret;
> +
> +	armada_enable_overheat_interrupt(priv);
>  
>  	return 0;
>  }
> @@ -948,8 +971,20 @@ static void armada_thermal_exit(struct platform_device *pdev)
>  {
>  	struct armada_drvdata *drvdata = platform_get_drvdata(pdev);
>  
> -	if (drvdata->type == LEGACY)
> +	if (drvdata->type == LEGACY) {
>  		thermal_zone_device_unregister(drvdata->data.tz);
> +		return;
> +	}
> +
> +	/*
> +	 * devres frees the interrupt before the thermal zones, so the thread
> +	 * is waited for in free_irq().  Mask the source so that no new
> +	 * interrupt is raised for a handler that is about to go away, and let
> +	 * the poll loop give up instead of keeping ->remove() blocked until
> +	 * the sensor cools down.
> +	 */

This paragraph is also not needed in my opinion.

> +	WRITE_ONCE(drvdata->data.priv->shutdown, true);
> +	armada_disable_overheat_interrupt(drvdata->data.priv);
>  }
>  
>  static struct platform_driver armada_thermal_driver = {

Thanks,
Miquèl

      reply	other threads:[~2026-11-19 11:11 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-18  7:23 [PATCH] thermal/drivers/armada: free the overheat IRQ before the zones are gone Yaohan Li
2026-11-18  9:29 ` Miquel Raynal
2026-11-19  2:29 ` [PATCH v2] thermal/drivers/armada: request the overheat IRQ before enabling the source Yaohan Li
2026-11-19 11:11   ` Miquel Raynal [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=87o6bldqjo.fsf@bootlin.com \
    --to=miquel.raynal@bootlin.com \
    --cc=daniel.lezcano@kernel.org \
    --cc=edubezval@gmail.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-pm@vger.kernel.org \
    --cc=lukasz.luba@arm.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