Re: [PATCH] hwmon: (nct6775) Clamp fan control times before rounding
From: David Laight
Date: Wed Nov 11 2026 - 05:58:42 EST
On Wed, 11 Nov 2026 11:39:54 +0800
Zhu Jun <zhujun2@xxxxxxxxxxxxxxxxxxxx> wrote:
> store_fan_time() accepts an unsigned long, but step_time_to_reg()
> narrows it to unsigned int and adds the rounding adjustment before
> clamping. For example, 4294967295 ms wraps and selects register 1
> instead of 255 in either mode.
>
> Preserve the input width and clamp to the maximum duration for the
> selected mode before rounding.
>
> Fixes: cdcaeceb74ff ("hwmon: (nct6775) Add support for automatic fan control")
> Signed-off-by: Zhu Jun <zhujun2@xxxxxxxxxxxxxxxxxxxx>
> ---
> drivers/hwmon/nct6775-core.c | 3 ++-
> 1 file changed, 2 insertions(+), 1 deletion(-)
>
> diff --git a/drivers/hwmon/nct6775-core.c b/drivers/hwmon/nct6775-core.c
> index 6e97e4f08b90..76b2c9551a1c 100644
> --- a/drivers/hwmon/nct6775-core.c
> +++ b/drivers/hwmon/nct6775-core.c
> @@ -949,8 +949,9 @@ static unsigned int step_time_from_reg(u8 reg, u8 mode)
> return mode ? 400 * reg : 100 * reg;
> }
>
> -static u8 step_time_to_reg(unsigned int msec, u8 mode)
> +static u8 step_time_to_reg(unsigned long msec, u8 mode)
> {
> + msec = min(msec, 255UL * (mode ? 400 : 100));
> return clamp_val((mode ? (msec + 200) / 400 :
> (msec + 50) / 100), 1, 255);
Just do most of the division before doing the add:
return clamp((msec / (mode ? 200 : 50) + 1) / 2, 1, 255);
(It should never have been clamp_val(), the correct change was 1u, 255u.
Not needed any more.)
David
> }