Linux Hardware Monitor development
 help / color / mirror / Atom feed
* [PATCH] hwmon: (nct6775) Clamp fan control times before rounding
@ 2026-11-09 10:16 Zhu Jun
  2026-11-09 10:19 ` sashiko-bot
  0 siblings, 1 reply; 7+ messages in thread
From: Zhu Jun @ 2026-11-09 10:16 UTC (permalink / raw)
  To: linux; +Cc: linux-hwmon, linux-kernel

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")
---
Validation: x86-64 and i386 driver objects built with W=1.
Source-extracted boundary tests passed on both word sizes;
the corresponding test fails on the base revision. I/O is mocked.
No physical sensor or booted-kernel testing was available.
OpenAI Codex assisted analysis, code, tests and changelog drafting.
Please review and add your own Signed-off-by before submitting.

 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);
 }

base-commit: 7475ea8b58616b33c04afb976cf0fbaad9dd63fb
-- 
2.51.0




^ permalink raw reply related	[flat|nested] 7+ messages in thread

* Re: [PATCH] hwmon: (nct6775) Clamp fan control times before rounding
  2026-11-09 10:16 Zhu Jun
@ 2026-11-09 10:19 ` sashiko-bot
  0 siblings, 0 replies; 7+ messages in thread
From: sashiko-bot @ 2026-11-09 10:19 UTC (permalink / raw)
  To: Zhu Jun; +Cc: linux-hwmon

> 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")

Sashiko has reviewed this patch and found no issues.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261109101629.16713-1-zhujun2@cmss.chinamobile.com?part=1


^ permalink raw reply	[flat|nested] 7+ messages in thread

* [PATCH] hwmon: (nct6775) Clamp fan control times before rounding
@ 2026-11-11  2:46 Zhu Jun
  2026-11-11  3:02 ` sashiko-bot
  0 siblings, 1 reply; 7+ messages in thread
From: Zhu Jun @ 2026-11-11  2:46 UTC (permalink / raw)
  To: linux; +Cc: linux-hwmon, linux-kernel, Zhu Jun

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.

Signed-off-by: Zhu Jun <zhujun2@cmss.chinamobile.com>
---
 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);
 }
-- 
2.51.0



^ permalink raw reply related	[flat|nested] 7+ messages in thread

* Re: [PATCH] hwmon: (nct6775) Clamp fan control times before rounding
  2026-11-11  2:46 Zhu Jun
@ 2026-11-11  3:02 ` sashiko-bot
  0 siblings, 0 replies; 7+ messages in thread
From: sashiko-bot @ 2026-11-11  3:02 UTC (permalink / raw)
  To: Zhu Jun; +Cc: linux-hwmon

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Low] hwmon: (nct6775) missing Fixes: trailer for step_time_to_reg()
  bug fix
--

commit befc95af1cfff1fb8b3fc9efb4b1e43ee7673fb9
Author: Zhu Jun <zhujun2@cmss.chinamobile.com>

> hwmon: (nct6775) Clamp fan control times before rounding
>
> 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.
>
> Signed-off-by: Zhu Jun <zhujun2@cmss.chinamobile.com>

[Severity: Low]
This fixes a narrowing and rounding overflow bug in step_time_to_reg().
Should this include a Fixes tag for the commit that introduced
step_time_to_reg() and store_fan_time()?

  Fixes: cdcaeceb74ff ("hwmon: (nct6775) Add support for automatic fan control")

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261111024607.9712-1-zhujun2@cmss.chinamobile.com?part=1

^ permalink raw reply	[flat|nested] 7+ messages in thread

* [PATCH] hwmon: (nct6775) Clamp fan control times before rounding
@ 2026-11-11  3:39 Zhu Jun
  2026-11-11  3:43 ` sashiko-bot
  2026-11-11 10:57 ` David Laight
  0 siblings, 2 replies; 7+ messages in thread
From: Zhu Jun @ 2026-11-11  3:39 UTC (permalink / raw)
  To: linux; +Cc: linux-hwmon, linux-kernel, Zhu Jun

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@cmss.chinamobile.com>
---
 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);
 }
-- 
2.51.0



^ permalink raw reply related	[flat|nested] 7+ messages in thread

* Re: [PATCH] hwmon: (nct6775) Clamp fan control times before rounding
  2026-11-11  3:39 [PATCH] hwmon: (nct6775) Clamp fan control times before rounding Zhu Jun
@ 2026-11-11  3:43 ` sashiko-bot
  2026-11-11 10:57 ` David Laight
  1 sibling, 0 replies; 7+ messages in thread
From: sashiko-bot @ 2026-11-11  3:43 UTC (permalink / raw)
  To: Zhu Jun; +Cc: linux-hwmon

> 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@cmss.chinamobile.com>

Sashiko has reviewed this patch and found no issues.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261111033954.12468-1-zhujun2@cmss.chinamobile.com?part=1


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] hwmon: (nct6775) Clamp fan control times before rounding
  2026-11-11  3:39 [PATCH] hwmon: (nct6775) Clamp fan control times before rounding Zhu Jun
  2026-11-11  3:43 ` sashiko-bot
@ 2026-11-11 10:57 ` David Laight
  1 sibling, 0 replies; 7+ messages in thread
From: David Laight @ 2026-11-11 10:57 UTC (permalink / raw)
  To: Zhu Jun; +Cc: linux, linux-hwmon, linux-kernel

On Wed, 11 Nov 2026 11:39:54 +0800
Zhu Jun <zhujun2@cmss.chinamobile.com> 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@cmss.chinamobile.com>
> ---
>  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

>  }


^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2026-11-11 10:57 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-11  3:39 [PATCH] hwmon: (nct6775) Clamp fan control times before rounding Zhu Jun
2026-11-11  3:43 ` sashiko-bot
2026-11-11 10:57 ` David Laight
  -- strict thread matches above, loose matches on Subject: below --
2026-11-11  2:46 Zhu Jun
2026-11-11  3:02 ` sashiko-bot
2026-11-09 10:16 Zhu Jun
2026-11-09 10:19 ` sashiko-bot

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox