All of lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Cong Nguyen" <congnt264@gmail.com>
Cc: linux-hwmon@vger.kernel.org
Subject: Re: [PATCH] hwmon: (max6621) fix negative temperature offset and crit readings
Date: Sat, 08 Aug 2026 08:58:11 +0000	[thread overview]
Message-ID: <20260808085811.ACB651F000E9@smtp.kernel.org> (raw)
In-Reply-To: <20260808083718.2612205-1-congnt264@gmail.com>

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

Pre-existing issues:
- [High] Writing the maximum allowed temperature of 128°C to hwmon_temp_crit silently wraps to -128°C in hardware.
- [Medium] Fractional temperature precision is discarded due to shifting and integer division before scaling.
--

--- Patch [1]: [PATCH] hwmon: (max6621) fix negative temperature offset and crit readings ---
commit a073f1d4838dda93adf9b46f8e3cc16f1b662f82
Author: Cong Nguyen <congnt264@gmail.com>

hwmon: (max6621) fix negative temperature offset and crit readings

This patch fixes temperature reporting for the max6621 driver. It
addresses an issue where negative temperatures were reported as large
positive numbers due to missing sign extension before scaling the 16-bit
register values.

Cc: stable@vger.kernel.org

>       - temp_input used an s8 intermediate, which is correct for the
>         -127..127 range but reports the documented +128 degrees C maximum as
>         -128 degrees C.

[Severity: High]
This is a pre-existing issue, but while fixing the read path for the +128
degrees C maximum, is it possible that the write path still wraps to -128
degrees C?

When writing to hwmon_temp_crit, max6621_write() in drivers/hwmon/max6621.c
clamps the input to MAX6621_TEMP_INPUT_MAX (128000) and divides by 1000L:

drivers/hwmon/max6621.c:max6621_write() {
    ...
    case hwmon_temp_crit:
        ...
        /* Clamp to allowed range to prevent overflow. */
        val = clamp_val(val, MAX6621_TEMP_INPUT_MIN,
                MAX6621_TEMP_INPUT_MAX);
        val = val / 1000L;

        return regmap_write(data->regmap, reg, val);
}

If a user writes 128000 to temp_crit, it gets clamped and divided to 128
(0x80). For an 8-bit two's complement alert register in the MAX6621
hardware, 0x80 is interpreted as -128 degrees C.

Could this cause continuous false thermal alerts and potential unintended
system throttling?

> diff --git a/drivers/hwmon/max6621.c b/drivers/hwmon/max6621.c
> index e86ec6d237ca6..9becd098d840b 100644
> --- a/drivers/hwmon/max6621.c
> +++ b/drivers/hwmon/max6621.c
[ ... ]
> @@ -225,8 +224,8 @@ max6621_read(struct device *dev, enum hwmon_sensor_types type, u32 attr,
>  			 * The temperature is given in two's complement and 8
>  			 * bits is used for the register conversion.
>  			 */
> -			temp = (regval >> MAX6621_REG_TEMP_SHIFT);
> -			*val = temp * 1000L;
> +			*val = (sign_extend32(regval, 15) >>
> +				MAX6621_REG_TEMP_SHIFT) * 1000L;

[Severity: Medium]
This is a pre-existing issue, but does this arithmetic right shift discard
the hardware's fractional temperature precision?

By shifting right by MAX6621_REG_TEMP_SHIFT (6 bits) before multiplying by
1000L, the lower 6 fractional bits are lost, dropping up to 0.984 degrees C
of precision.

Would it be better to multiply by 1000L first and then shift or divide to
expose the full fractional precision provided by the MAX6621 device?

The same precision loss also happens when writing hwmon_temp_offset in
drivers/hwmon/max6621.c:

drivers/hwmon/max6621.c:max6621_temp_mc2reg() {
    return (val / 1000L) << MAX6621_REG_TEMP_SHIFT;
}

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20260808083718.2612205-1-congnt264@gmail.com?part=1

  reply	other threads:[~2026-08-08  8:58 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-08-08  8:37 [PATCH] hwmon: (max6621) fix negative temperature offset and crit readings Cong Nguyen
2026-08-08  8:58 ` sashiko-bot [this message]
2026-08-08  9:19   ` Nguyễn Công
2026-08-08  9:38     ` Nguyễn Công
2026-08-09  0:19       ` Guenter Roeck
2026-08-08 14:48 ` Guenter Roeck
2026-08-08 15:54   ` Nguyễn Công
2026-08-09  2:45     ` Guenter Roeck
2026-08-10  4:33       ` Nguyễn Công

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=20260808085811.ACB651F000E9@smtp.kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=congnt264@gmail.com \
    --cc=linux-hwmon@vger.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.