mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] hwmon: (w83l786ng) Clamp voltage limits before rounding
@ 2026-11-09 10:19 Zhu Jun
  2026-11-10  9:36 ` David Laight
  0 siblings, 1 reply; 2+ messages in thread
From: Zhu Jun @ 2026-11-09 10:19 UTC (permalink / raw)
  To: linux; +Cc: linux-hwmon, linux-kernel

IN_TO_REG() adds 4 to an unrestricted unsigned long. Inputs close to
ULONG_MAX wrap, so an excessive voltage limit can select register zero
instead of 255.

Clamp to the 0..2040 mV register range before rounding to 8 mV units.

Fixes: 85f03bccd6e0 ("hwmon: Add support for Winbond W83L786NG/NR")
---
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/w83l786ng.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/hwmon/w83l786ng.c b/drivers/hwmon/w83l786ng.c
index d16afda34e41..0cd545011e73 100644
--- a/drivers/hwmon/w83l786ng.c
+++ b/drivers/hwmon/w83l786ng.c
@@ -101,7 +101,7 @@ static int temp_from_reg(int val)
  * in mV as would be measured on the chip input pin, need to just
  * multiply/divide by 8 to translate from/to register values.
  */
-#define IN_TO_REG(val)		(clamp_val((((val) + 4) / 8), 0, 255))
+#define IN_TO_REG(val)		((clamp_val((val), 0, 2040) + 4) / 8)
 #define IN_FROM_REG(val)	((val) * 8)
 
 #define DIV_FROM_REG(val)	(1 << (val))

base-commit: 7475ea8b58616b33c04afb976cf0fbaad9dd63fb
-- 
2.51.0




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

* Re: [PATCH] hwmon: (w83l786ng) Clamp voltage limits before rounding
  2026-11-09 10:19 [PATCH] hwmon: (w83l786ng) Clamp voltage limits before rounding Zhu Jun
@ 2026-11-10  9:36 ` David Laight
  0 siblings, 0 replies; 2+ messages in thread
From: David Laight @ 2026-11-10  9:36 UTC (permalink / raw)
  To: Zhu Jun; +Cc: linux, linux-hwmon, linux-kernel

On Mon,  9 Nov 2026 18:19:16 +0800
Zhu Jun <zhujun2@cmss.chinamobile.com> wrote:

> IN_TO_REG() adds 4 to an unrestricted unsigned long. Inputs close to
> ULONG_MAX wrap, so an excessive voltage limit can select register zero
> instead of 255.

I looked, it really doesn't matter.
The 'unrestricted unsigned long' comes from a write to a sysfs value.
Nothing will write a value that large unless they are trying to break
something, and they can just as well write "0".

David

> 
> Clamp to the 0..2040 mV register range before rounding to 8 mV units.
> 
> Fixes: 85f03bccd6e0 ("hwmon: Add support for Winbond W83L786NG/NR")
> ---
> 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/w83l786ng.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/drivers/hwmon/w83l786ng.c b/drivers/hwmon/w83l786ng.c
> index d16afda34e41..0cd545011e73 100644
> --- a/drivers/hwmon/w83l786ng.c
> +++ b/drivers/hwmon/w83l786ng.c
> @@ -101,7 +101,7 @@ static int temp_from_reg(int val)
>   * in mV as would be measured on the chip input pin, need to just
>   * multiply/divide by 8 to translate from/to register values.
>   */
> -#define IN_TO_REG(val)		(clamp_val((((val) + 4) / 8), 0, 255))
> +#define IN_TO_REG(val)		((clamp_val((val), 0, 2040) + 4) / 8)
>  #define IN_FROM_REG(val)	((val) * 8)
>  
>  #define DIV_FROM_REG(val)	(1 << (val))
> 
> base-commit: 7475ea8b58616b33c04afb976cf0fbaad9dd63fb


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

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

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-09 10:19 [PATCH] hwmon: (w83l786ng) Clamp voltage limits before rounding Zhu Jun
2026-11-10  9:36 ` David Laight

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

all inboxes | Powered by JetHome®