Devicetree
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Jeroen Trappers" <jeroen.trappers@gmail.com>
Cc: devicetree@vger.kernel.org, robh@kernel.org, conor+dt@kernel.org
Subject: Re: [PATCH 2/3] thermal/drivers/apple: Add T7000 (A8) on-die temperature sensor driver
Date: Thu, 03 Dec 2026 16:32:42 +0000	[thread overview]
Message-ID: <sashiko-outbox-248199@kernel.org> (raw)
In-Reply-To: <20261203155600.3911447-3-jeroen.trappers@gmail.com>

Thank you for your contribution! Sashiko AI review found 3 potential
issues to consider:
- [High] thermal: rate-limit bypass and accumulator starvation in
  t7000_get_temp()
- [Medium] thermal: unbounded stale temp masking and disabled restart in
  t7000_sample()
- [Medium] thermal: unused sensors started and stopped in
  t7000_thermal_probe()
--

commit e51c3a5c5a92b6f8f1aba9692e7009a4e0f3f77d
Author: Jeroen Trappers <jeroen.trappers@gmail.com>

thermal/drivers/apple: Add T7000 (A8) on-die temperature sensor driver

This commit adds a thermal zone driver for the Apple T7000 (A8) on-die
temperature sensors, reading accumulated samples from the boot-calibrated
SoC and CPU complex register pages and exposing them through thermal zones
and hwmon.

> diff --git a/drivers/thermal/apple_t7000_thermal.c b/drivers/thermal/apple_t7000_thermal.c
> new file mode 100644
> index 0000000000000..7d80ddf6e2f56
> --- /dev/null
> +++ b/drivers/thermal/apple_t7000_thermal.c

[ ... ]

> +static int t7000_sample(struct t7000_sensor *s)
> +{
> +	const struct t7000_sensor_desc *d = s->desc;
> +	u32 ctl, bk, count;
> +	int temp;
> +
> +	writel(T7000_CTL0_ACCUMULATE, s->base + d->ctl_clear);
> +	ctl = readl(s->base + d->ctl_set);
> +	bk = readl(s->base + d->bk);
> +	count = readl(s->base + d->count);
> +	writel(T7000_CTL0_ACCUMULATE, s->base + d->ctl_set);
> +
> +	if (!(ctl & T7000_CTL0_ENABLE) || !(bk & T7000_BK_VALID) || !count)
> +		return -EAGAIN;
> +
> +	temp = DIV_ROUND_CLOSEST(sign_extend32(bk, d->sign_bit) * 1000, 64);
> +	/* An impossible code is a glitch; iOS substitutes 30 C instead. */
> +	if (temp < -40000 || temp > 150000)
> +		return -EAGAIN;
> +
> +	s->temp = temp;
> +	s->sampled = jiffies;
> +	s->valid = true;
> +	return 0;
> +}
> +
> +static int t7000_get_temp(struct thermal_zone_device *tz, int *temp)
> +{
> +	struct t7000_sensor *s = thermal_zone_device_priv(tz);
> +	int ret = 0;
> +
> +	if (!s->valid ||
> +	    time_after_eq(jiffies, s->sampled + msecs_to_jiffies(T7000_MIN_INTERVAL_MS)))
> +		ret = t7000_sample(s);

[Severity: High]
Can rapid reads bypass T7000_MIN_INTERVAL_MS and starve the accumulator?

t7000_sample() always freezes and restarts T7000_CTL0_ACCUMULATE, but skips
updating s->sampled on -EAGAIN.  An unprivileged user polling
/sys/class/thermal/thermal_zone*/temp faster than ~2.5 ms can therefore
keep resetting the accumulator with count == 0.

At probe, t7000_thermal_probe() starts accumulation without setting
s->sampled, and !s->valid skips T7000_MIN_INTERVAL_MS, so rapid polling keeps
s->valid false.  After any runtime -EAGAIN, s->sampled stays expired and
t7000_get_temp() returns stale s->temp forever, blocking thermal trips.

Updating s->sampled = jiffies whenever ctl_set is written (in probe and
unconditionally in t7000_sample()) and enforcing T7000_MIN_INTERVAL_MS before
s->valid is set would prevent starvation.

> +	/* No new sample (an interval without one, or a glitch): keep the last one. */
> +	if (ret == -EAGAIN && s->valid)
> +		ret = 0;
> +	if (!ret)
> +		*temp = s->temp;

[Severity: Medium]
Should a disabled or persistently failing sensor be masked behind stale
s->temp forever?

Once s->valid is true, t7000_get_temp() turns every -EAGAIN into 0 with no
staleness limit or retry bound.  Since t7000_sample() only returns 0 or
-EAGAIN, thermal_core never sees a hard error to trigger
thermal_zone_broken_disable(), leaving thermal trips on frozen data.

t7000_sample() also writes ctl_set before checking T7000_CTL0_ENABLE,
restarting accumulation on a disabled sensor.  Returning -ENODEV or -EIO
without writing ctl_set when disabled, and expiring s->valid after bounded
failures or age, would let thermal_core detect a broken sensor.

[ ... ]

> +		/* Start accumulating, as iOS does, before the zone's first read. */
> +		writel(T7000_CTL0_ACCUMULATE, s->base + d->ctl_set);
> +		ret = devm_add_action_or_reset(dev, t7000_sensor_stop, s);
> +		if (ret)
> +			return ret;
> +
> +		tz = devm_thermal_of_zone_register(dev, i, s, &t7000_thermal_ops);
> +		if (IS_ERR(tz)) {
> +			/* No zone uses this sensor: leave it alone. */
> +			if (PTR_ERR(tz) == -ENODEV) {
> +				devm_release_action(dev, t7000_sensor_stop, s);
> +				continue;
> +			}

[Severity: Medium]
Does writing ctl_set before devm_thermal_of_zone_register() disturb sensors
that no thermal zone uses?

When a boot-enabled sensor has no thermal zone,
devm_thermal_of_zone_register() returns -ENODEV and devm_release_action()
runs t7000_sensor_stop().  That writes ctl_set then ctl_clear, clearing
T7000_CTL0_ACCUMULATE even if firmware left it running.

Registering t7000_sensor_stop() without pre-writing ctl_set, starting
accumulation only after registration succeeds, and calling
devm_remove_action() on -ENODEV would keep unbind order while leaving unused
sensors untouched.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261203155600.3911447-1-jeroen.trappers@gmail.com?part=2

  reply	other threads:[~2026-12-03 16:32 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-03 15:55 [PATCH 0/3] Apple T7000 (A8) die temperature sensors and thermal zones Jeroen Trappers
2026-12-03 15:55 ` [PATCH 1/3] dt-bindings: thermal: Add Apple T7000 die temperature sensors Jeroen Trappers
2026-12-03 16:12   ` sashiko-bot
2026-12-03 15:55 ` [PATCH 2/3] thermal/drivers/apple: Add T7000 (A8) on-die temperature sensor driver Jeroen Trappers
2026-12-03 16:32   ` sashiko-bot [this message]
2026-12-03 15:56 ` [PATCH 3/3] arm64: dts: apple: t7000: Add die temperature sensors and thermal zones Jeroen Trappers
2026-12-03 16:18   ` sashiko-bot

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=sashiko-outbox-248199@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=conor+dt@kernel.org \
    --cc=devicetree@vger.kernel.org \
    --cc=jeroen.trappers@gmail.com \
    --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