Devicetree
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "HaoNing Cheng" <Haoning.CHENG@cn.bosch.com>
Cc: Frank.Li@kernel.org, imx@lists.linux.dev, robh@kernel.org,
	conor+dt@kernel.org, devicetree@vger.kernel.org
Subject: Re: [PATCH v2 2/2] thermal/drivers/imx: Add calibration offset support
Date: Thu, 09 Jul 2026 08:22:20 +0000	[thread overview]
Message-ID: <20260709082220.D4D6E1F000E9@smtp.kernel.org> (raw)
In-Reply-To: <20260709-b4-symana21-11221-imx-thermal-support-upstream-6-18-v2-2-00ff72495e24@cn.bosch.com>

Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:

Pre-existing issues:
- [High] Premature thermal zone registration exposes sysfs before the IRQ is requested, allowing unbalanced IRQ enable/disable operations.
- [High] Data race on `data->irq_enabled` between process context and hardirq context leads to unbalanced IRQ disable depths and permanently disabled interrupts.
- [High] Use-After-Free of `data->tz` in the thermal alarm IRQ thread during driver removal due to skipped `disable_irq()` synchronization.
--

--- Patch [2]: [PATCH v2 2/2] thermal/drivers/imx: Add calibration offset support ---
commit 7cfd21023fa90494618a836d7b36a5a78f4f8447
Author: HaoNing Cheng <Haoning.CHENG@cn.bosch.com>

thermal/drivers/imx: Add calibration offset support
    
Some boards need a small per-design correction to align the reported CPU
temperature with board-level measurements.

Read the optional fsl,temp-calibration-offset-millicelsius property from
DT and apply it to the i.MX6/6SX/7D calibration formulas.

> diff --git a/drivers/thermal/imx_thermal.c b/drivers/thermal/imx_thermal.c
> index 5aaacbc534786..971d6b4737c68 100644
> --- a/drivers/thermal/imx_thermal.c
> +++ b/drivers/thermal/imx_thermal.c

[ ... ]

> @@ -274,7 +278,7 @@ static int imx_get_temp(struct thermal_zone_device *tz, int *temp)
>  
>  	/* See imx_init_calib() for formula derivation */
>  	if (data->socdata->version == TEMPMON_IMX7D)
> -		*temp = (n_meas - data->c1 + 25) * 1000;
> +		*temp = (n_meas - data->c1 + 25) * 1000 + data->calibration_offset;
>  	else
>  		*temp = data->c2 - n_meas * data->c1;

[Severity: High]
This isn't a bug introduced by this patch, but I noticed a potential race
condition involving the sysfs exposure of this temperature read.

In imx_thermal_probe(), the thermal zone is registered before the IRQ is
requested:

imx_thermal_probe() {
    data->tz = thermal_zone_device_register_with_trips(...)
    ...
    ret = devm_request_threaded_irq(...)
}

Could userspace trigger a read through sysfs immediately after registration,
calling into imx_get_temp() before the IRQ is fully requested?

If so, and if it reaches this path:

imx_get_temp() {
    if (!data->irq_enabled && *temp < data->alarm_temp) {
        data->irq_enabled = true;
        enable_irq(data->irq);
    }
}

Would it call enable_irq() on an unrequested IRQ, leading to unbalanced IRQ
enable/disable operations?

Furthermore, if devm_request_threaded_irq() fails, the error path
unregisters the thermal zone, which calls
imx_change_mode(THERMAL_DEVICE_DISABLED).  Since data->irq_enabled was set
to true, would it deterministically call disable_irq() on the unrequested IRQ?

[ ... ]

> @@ -629,6 +633,10 @@ static int imx_thermal_probe(struct platform_device *pdev)
>  
>  	platform_set_drvdata(pdev, data);
>  
> +	of_property_read_s32(dev->of_node,
> +			     "fsl,temp-calibration-offset-millicelsius",
> +			     &data->calibration_offset);
> +

[Severity: High]
This is a pre-existing issue, but while reviewing the IRQ handling in this
driver, I noticed a potential data race on data->irq_enabled between process
context and hardirq context.

The flag is accessed and modified in imx_change_mode() without
synchronization:

imx_change_mode() {
    if (data->irq_enabled) {
        disable_irq(data->irq);
        data->irq_enabled = false;
    }
}

It is also concurrently modified in hardirq context:

imx_thermal_alarm_irq() {
    disable_irq_nosync(data->irq);
    data->irq_enabled = false;
}

If imx_change_mode(THERMAL_DEVICE_DISABLED) reads data->irq_enabled as true,
and an alarm interrupt fires immediately after on another CPU, the hardirq
handler will run, call disable_irq_nosync(), and set the flag to false.

When the hardirq completes, would imx_change_mode() resume and
unconditionally call disable_irq(), incrementing the IRQ disable depth to 2?

Would subsequent reenables only decrement it by 1, leaving the hardware IRQ
permanently disabled?

[Severity: High]
This is another pre-existing issue related to the IRQ handling, but I
noticed a potential use-after-free of data->tz in the thermal alarm IRQ
thread during driver removal.

During imx_thermal_remove(), thermal_zone_device_unregister() is called,
which internally sets the mode to disabled by calling
imx_change_mode(THERMAL_DEVICE_DISABLED).

If a thermal alarm interrupt fired just prior, the hardirq handler will have
set data->irq_enabled = false and woken up the IRQ thread.  Because
data->irq_enabled is now false, imx_change_mode() skips calling disable_irq():

imx_change_mode() {
    if (data->irq_enabled) {
        disable_irq(data->irq);
    }
}

By skipping disable_irq(), does it fail to provide the required
synchronization barrier to wait for the IRQ thread to complete?

If thermal_zone_device_unregister() proceeds to free data->tz, could the
still-running imx_thermal_alarm_irq_thread() then dereference the freed
pointer:

imx_thermal_alarm_irq_thread() {
    thermal_zone_device_update(data->tz, THERMAL_EVENT_UNSPECIFIED);
}

Would this lead to a use-after-free?

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20260709-b4-symana21-11221-imx-thermal-support-upstream-6-18-v2-0-00ff72495e24@cn.bosch.com?part=2

      reply	other threads:[~2026-07-09  8:22 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-07-09  8:10 [PATCH v2 0/2] thermal: imx: Add calibration offset support HaoNing Cheng via B4 Relay
2026-07-09  8:10 ` [PATCH v2 1/2] dt-bindings: thermal: imx: Document calibration offset property HaoNing Cheng via B4 Relay
2026-07-09 15:07   ` Frank Li
2026-07-09 17:47     ` Conor Dooley
2026-07-09  8:10 ` [PATCH v2 2/2] thermal/drivers/imx: Add calibration offset support HaoNing Cheng via B4 Relay
2026-07-09  8:22   ` sashiko-bot [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=20260709082220.D4D6E1F000E9@smtp.kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=Frank.Li@kernel.org \
    --cc=Haoning.CHENG@cn.bosch.com \
    --cc=conor+dt@kernel.org \
    --cc=devicetree@vger.kernel.org \
    --cc=imx@lists.linux.dev \
    --cc=robh@kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    /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