Linux Hardware Monitor development
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Shirong Zhao" <shxzhaosr@163.com>
Cc: linux-hwmon@vger.kernel.org
Subject: Re: [PATCH] hwmon: (tmp513) fix division by zero when calibrating a large shunt
Date: Sat, 05 Dec 2026 01:37:18 +0000	[thread overview]
Message-ID: <sashiko-outbox-250900@kernel.org> (raw)
In-Reply-To: <20261205012428.120-1-shxzhaosr@163.com>

Thank you for your contribution! Sashiko AI review found 3 potential
issues to consider:
- [High] hwmon: (tmp513) divide-by-zero in tmp51x_set_value() when div
  == 0
- [Medium] hwmon: (tmp513) tmp51x_calibrate() disables sensing for valid
  large shunts
- [Low] hwmon: (tmp513) inaccurate shunt bound units and direction in
  commit message
--

commit a7739395a7938b1d11a4a6e3d2b569de06b0cc02
Author: Shirong Zhao <shxzhaosr@163.com>

hwmon: (tmp513) fix division by zero when calibrating a large shunt

This commit adds a check for div == 0 in tmp51x_calibrate() and writes 0 to
TMP51X_SHUNT_CALIBRATION to disable the current and power measurement engine
instead of dividing by zero.

> The shunt value comes from the "shunt-resistor-micro-ohms" device-tree /
> firmware property (tmp51x_read_properties()), so this is a device
> configuration issue, not a runtime userspace input.  It is limited only
> from above (shunt_uohms > pga_gain * 40 uOhm is rejected); there is no
> lower bound that keeps curr_lsb_ua nonzero.  For the default pga_gain of 8
> the trap triggers for shunt resistors from about 19.4 Ohm up to the 320
> Ohm upper bound, i.e. for perfectly ordinary shunt values.

[Severity: Low]
In tmp51x_read_properties(), the rejected threshold is
pga_gain * 40 * MICRO uOhm (pga_gain * 40 Ohm), not pga_gain * 40 uOhm.
Also, since curr_lsb_ua decreases as shunt_uohms grows, keeping curr_lsb_ua
nonzero would require a tighter upper bound on shunt_uohms (or a lower bound
on max_curr_ma), not a lower bound on shunt_uohms.

> diff --git a/drivers/hwmon/tmp513.c b/drivers/hwmon/tmp513.c
> index a3a4772a37e93..b7eb6143faff3 100644
> --- a/drivers/hwmon/tmp513.c
> +++ b/drivers/hwmon/tmp513.c
> @@ -565,6 +565,14 @@ static int tmp51x_calibrate(struct tmp51x_data *data)
>  
>  	div = DIV_ROUND_CLOSEST_ULL(data->curr_lsb_ua * data->shunt_uohms, MICRO);
>  
> +	/*
> +	 * A large shunt resistor can round curr_lsb_ua down to 0, which makes
> +	 * div 0 as well.  Just like the shunt_uohms == 0 case above, disable the
> +	 * current/power engine rather than dividing by zero.
> +	 */
> +	if (div == 0)
> +		return regmap_write(data->regmap, TMP51X_SHUNT_CALIBRATION, 0);

[Severity: High]
Can writing to power1_crit still trigger a divide-by-zero when div == 0?

When div == 0, data->curr_lsb_ua and data->pwr_lsb_uw are 0 while
data->shunt_uohms stays non-zero.  tmp51x_is_visible() only checks
!data->shunt_uohms for hwmon_curr and hwmon_power, so power1_crit and the
other disabled current/power attributes remain exposed in sysfs.

drivers/hwmon/tmp513.c:tmp51x_set_value() {
    ...
	case TMP51X_POWER_LIMIT:
		regval = clamp_val(DIV_ROUND_CLOSEST(val, data->pwr_lsb_uw), 0,
				   U16_MAX);
		break;
    ...
}

Any write to power1_crit calls tmp51x_write() -> tmp51x_set_value() and
divides by data->pwr_lsb_uw == 0, crashing the kernel.

[Severity: Medium]
Should tmp51x_calibrate() round curr_lsb_ua up instead of disabling sensing
for valid large shunts?

Documentation/devicetree/bindings/hwmon/ti,tmp513.yaml specifies that only
0 disables calibration and values up to pga-gain * 40 * 1000 * 1000 uOhm
are valid.  Disabling the engine on div == 0 breaks current and power
sensing from roughly 19.4 Ohm to 320 Ohm at the default pga_gain = 8.

curr_lsb_ua is a minimum lower bound so the signed current register does
not overflow, and only rounds to 0 because of DIV_ROUND_CLOSEST_ULL().
Using DIV_ROUND_UP_ULL() keeps curr_lsb_ua >= 1 uA and div in [1, 320]
across the entire valid range so large shunts calibrate properly.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261205012428.120-1-shxzhaosr@163.com?part=1

  reply	other threads:[~2026-12-05  1:37 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-05  1:24 [PATCH] hwmon: (tmp513) fix division by zero when calibrating a large shunt Shirong Zhao
2026-12-05  1:37 ` sashiko-bot [this message]
2026-12-05  1:45 ` [PATCH v2] hwmon: (tmp513) avoid divide by zero from rounded-down current LSB Shirong Zhao
2026-12-05  1:57   ` sashiko-bot
2026-12-05  4:29   ` Guenter Roeck
2026-12-05  4:37   ` [PATCH v3] " Shirong Zhao
2026-12-05  4:48     ` sashiko-bot
2026-12-05 16:28     ` Guenter Roeck

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-250900@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=linux-hwmon@vger.kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    --cc=shxzhaosr@163.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