* [PATCH 0/4] hwmon: Fix underflows seen when writing limit attributes
@ 2024-07-07 14:47 Guenter Roeck
2024-07-07 14:47 ` [PATCH 1/4] hwmon: (adc128d818) " Guenter Roeck
` (3 more replies)
0 siblings, 4 replies; 5+ messages in thread
From: Guenter Roeck @ 2024-07-07 14:47 UTC (permalink / raw)
To: Hardware Monitoring; +Cc: Guenter Roeck
DIV_ROUND_CLOSEST() after kstrtol() results in an underflow if a large
negative number such as -9223372036854775808 is provided by the user.
Fix it by reordering clamp_val() and DIV_ROUND_CLOSEST() operations.
The problem was fixed using the following a coccinelle script. The python
arithmetic is used to fold constant multiplications.
@lc@
expression range, val, err, buf, res;
position p;
expression divisor, l1, l2;
@@
err = \( kstrtol \| kstrtoint \| kstrtos64 \)(buf, range, &val);
... when != val
res = clamp_val(DIV_ROUND_CLOSEST@p(val, divisor), l1, l2);
@script:python mult@
l1 << lc.l1;
l2 << lc.l2;
d << lc.divisor;
m1;
m2;
de;
l1e;
l2e;
@@
coccinelle.dn = d.isnumeric()
coccinelle.l1e = l1.replace(' ', '')
coccinelle.l2e = l2.replace(' ', '')
coccinelle.l1n = l1.isnumeric() or (coccinelle.l1e[0] == '-' and coccinelle.l1e[1:].isnumeric())
coccinelle.l2n = l2.isnumeric() or (coccinelle.l2e[0] == '-' and coccinelle.l2e[1:].isnumeric())
if coccinelle.dn and coccinelle.l1n and coccinelle.l2n:
coccinelle.m1 = str(eval(l1) * int(d))
coccinelle.m2 = str(eval(l2) * int(d))
elif coccinelle.dn and coccinelle.l1n:
coccinelle.m1 = str(eval(l1) * int(d))
coccinelle.m2 = l2 + ' * ' + d
elif coccinelle.dn and coccinelle.l2n:
coccinelle.m1 = l1 + ' * ' + d
coccinelle.m2 = str(eval(l2) * int(d))
else:
coccinelle.m1 = l1 + ' * ' + d
coccinelle.m2 = l2 + ' * ' + d
@@
identifier mult.m1, mult.m2;
position lc.p;
expression res, val;
expression divisor, l1, l2;
@@
- res = clamp_val(DIV_ROUND_CLOSEST@p(val, divisor), l1, l2);
+ res = DIV_ROUND_CLOSEST(clamp_val(val, m1, m2), divisor);
----------------------------------------------------------------
Guenter Roeck (4):
hwmon: (adc128d818) Fix underflows seen when writing limit attributes
hwmon: (lm95234) Fix underflows seen when writing limit attributes
hwmon: (nct6775-core) Fix underflows seen when writing limit attributes
hwmon: (w83627ehf) Fix underflows seen when writing limit attributes
drivers/hwmon/adc128d818.c | 4 ++--
drivers/hwmon/lm95234.c | 9 +++++----
drivers/hwmon/nct6775-core.c | 2 +-
drivers/hwmon/w83627ehf.c | 4 ++--
4 files changed, 10 insertions(+), 9 deletions(-)
^ permalink raw reply [flat|nested] 5+ messages in thread
* [PATCH 1/4] hwmon: (adc128d818) Fix underflows seen when writing limit attributes
2024-07-07 14:47 [PATCH 0/4] hwmon: Fix underflows seen when writing limit attributes Guenter Roeck
@ 2024-07-07 14:47 ` Guenter Roeck
2024-07-07 14:47 ` [PATCH 2/4] hwmon: (lm95234) " Guenter Roeck
` (2 subsequent siblings)
3 siblings, 0 replies; 5+ messages in thread
From: Guenter Roeck @ 2024-07-07 14:47 UTC (permalink / raw)
To: Hardware Monitoring; +Cc: Guenter Roeck
DIV_ROUND_CLOSEST() after kstrtol() results in an underflow if a large
negative number such as -9223372036854775808 is provided by the user.
Fix it by reordering clamp_val() and DIV_ROUND_CLOSEST() operations.
Signed-off-by: Guenter Roeck <linux@roeck-us.net>
---
drivers/hwmon/adc128d818.c | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/drivers/hwmon/adc128d818.c b/drivers/hwmon/adc128d818.c
index 8ac6e735ec5c..5e805d4ee76a 100644
--- a/drivers/hwmon/adc128d818.c
+++ b/drivers/hwmon/adc128d818.c
@@ -175,7 +175,7 @@ static ssize_t adc128_in_store(struct device *dev,
mutex_lock(&data->update_lock);
/* 10 mV LSB on limit registers */
- regval = clamp_val(DIV_ROUND_CLOSEST(val, 10), 0, 255);
+ regval = DIV_ROUND_CLOSEST(clamp_val(val, 0, 2550), 10);
data->in[index][nr] = regval << 4;
reg = index == 1 ? ADC128_REG_IN_MIN(nr) : ADC128_REG_IN_MAX(nr);
i2c_smbus_write_byte_data(data->client, reg, regval);
@@ -213,7 +213,7 @@ static ssize_t adc128_temp_store(struct device *dev,
return err;
mutex_lock(&data->update_lock);
- regval = clamp_val(DIV_ROUND_CLOSEST(val, 1000), -128, 127);
+ regval = DIV_ROUND_CLOSEST(clamp_val(val, -128000, 127000), 1000);
data->temp[index] = regval << 1;
i2c_smbus_write_byte_data(data->client,
index == 1 ? ADC128_REG_TEMP_MAX
--
2.39.2
^ permalink raw reply related [flat|nested] 5+ messages in thread
* [PATCH 2/4] hwmon: (lm95234) Fix underflows seen when writing limit attributes
2024-07-07 14:47 [PATCH 0/4] hwmon: Fix underflows seen when writing limit attributes Guenter Roeck
2024-07-07 14:47 ` [PATCH 1/4] hwmon: (adc128d818) " Guenter Roeck
@ 2024-07-07 14:47 ` Guenter Roeck
2024-07-07 14:47 ` [PATCH 3/4] hwmon: (nct6775-core) " Guenter Roeck
2024-07-07 14:47 ` [PATCH 4/4] hwmon: (w83627ehf) " Guenter Roeck
3 siblings, 0 replies; 5+ messages in thread
From: Guenter Roeck @ 2024-07-07 14:47 UTC (permalink / raw)
To: Hardware Monitoring; +Cc: Guenter Roeck
DIV_ROUND_CLOSEST() after kstrtol() results in an underflow if a large
negative number such as -9223372036854775808 is provided by the user.
Fix it by reordering clamp_val() and DIV_ROUND_CLOSEST() operations.
Signed-off-by: Guenter Roeck <linux@roeck-us.net>
---
drivers/hwmon/lm95234.c | 9 +++++----
1 file changed, 5 insertions(+), 4 deletions(-)
diff --git a/drivers/hwmon/lm95234.c b/drivers/hwmon/lm95234.c
index 67b9d7636ee4..37e8e9679aeb 100644
--- a/drivers/hwmon/lm95234.c
+++ b/drivers/hwmon/lm95234.c
@@ -301,7 +301,8 @@ static ssize_t tcrit2_store(struct device *dev, struct device_attribute *attr,
if (ret < 0)
return ret;
- val = clamp_val(DIV_ROUND_CLOSEST(val, 1000), 0, index ? 255 : 127);
+ val = DIV_ROUND_CLOSEST(clamp_val(val, 0, (index ? 255 : 127) * 1000),
+ 1000);
mutex_lock(&data->update_lock);
data->tcrit2[index] = val;
@@ -350,7 +351,7 @@ static ssize_t tcrit1_store(struct device *dev, struct device_attribute *attr,
if (ret < 0)
return ret;
- val = clamp_val(DIV_ROUND_CLOSEST(val, 1000), 0, 255);
+ val = DIV_ROUND_CLOSEST(clamp_val(val, 0, 255000), 1000);
mutex_lock(&data->update_lock);
data->tcrit1[index] = val;
@@ -391,7 +392,7 @@ static ssize_t tcrit1_hyst_store(struct device *dev,
if (ret < 0)
return ret;
- val = DIV_ROUND_CLOSEST(val, 1000);
+ val = DIV_ROUND_CLOSEST(clamp_val(val, -255000, 255000), 1000);
val = clamp_val((int)data->tcrit1[index] - val, 0, 31);
mutex_lock(&data->update_lock);
@@ -431,7 +432,7 @@ static ssize_t offset_store(struct device *dev, struct device_attribute *attr,
return ret;
/* Accuracy is 1/2 degrees C */
- val = clamp_val(DIV_ROUND_CLOSEST(val, 500), -128, 127);
+ val = DIV_ROUND_CLOSEST(clamp_val(val, -64000, 63500), 500);
mutex_lock(&data->update_lock);
data->toffset[index] = val;
--
2.39.2
^ permalink raw reply related [flat|nested] 5+ messages in thread
* [PATCH 3/4] hwmon: (nct6775-core) Fix underflows seen when writing limit attributes
2024-07-07 14:47 [PATCH 0/4] hwmon: Fix underflows seen when writing limit attributes Guenter Roeck
2024-07-07 14:47 ` [PATCH 1/4] hwmon: (adc128d818) " Guenter Roeck
2024-07-07 14:47 ` [PATCH 2/4] hwmon: (lm95234) " Guenter Roeck
@ 2024-07-07 14:47 ` Guenter Roeck
2024-07-07 14:47 ` [PATCH 4/4] hwmon: (w83627ehf) " Guenter Roeck
3 siblings, 0 replies; 5+ messages in thread
From: Guenter Roeck @ 2024-07-07 14:47 UTC (permalink / raw)
To: Hardware Monitoring; +Cc: Guenter Roeck
DIV_ROUND_CLOSEST() after kstrtol() results in an underflow if a large
negative number such as -9223372036854775808 is provided by the user.
Fix it by reordering clamp_val() and DIV_ROUND_CLOSEST() operations.
Signed-off-by: Guenter Roeck <linux@roeck-us.net>
---
drivers/hwmon/nct6775-core.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/drivers/hwmon/nct6775-core.c b/drivers/hwmon/nct6775-core.c
index 9fbab8f02334..934fed3dd586 100644
--- a/drivers/hwmon/nct6775-core.c
+++ b/drivers/hwmon/nct6775-core.c
@@ -2262,7 +2262,7 @@ store_temp_offset(struct device *dev, struct device_attribute *attr,
if (err < 0)
return err;
- val = clamp_val(DIV_ROUND_CLOSEST(val, 1000), -128, 127);
+ val = DIV_ROUND_CLOSEST(clamp_val(val, -128000, 127000), 1000);
mutex_lock(&data->update_lock);
data->temp_offset[nr] = val;
--
2.39.2
^ permalink raw reply related [flat|nested] 5+ messages in thread
* [PATCH 4/4] hwmon: (w83627ehf) Fix underflows seen when writing limit attributes
2024-07-07 14:47 [PATCH 0/4] hwmon: Fix underflows seen when writing limit attributes Guenter Roeck
` (2 preceding siblings ...)
2024-07-07 14:47 ` [PATCH 3/4] hwmon: (nct6775-core) " Guenter Roeck
@ 2024-07-07 14:47 ` Guenter Roeck
3 siblings, 0 replies; 5+ messages in thread
From: Guenter Roeck @ 2024-07-07 14:47 UTC (permalink / raw)
To: Hardware Monitoring; +Cc: Guenter Roeck
DIV_ROUND_CLOSEST() after kstrtol() results in an underflow if a large
negative number such as -9223372036854775808 is provided by the user.
Fix it by reordering clamp_val() and DIV_ROUND_CLOSEST() operations.
Signed-off-by: Guenter Roeck <linux@roeck-us.net>
---
drivers/hwmon/w83627ehf.c | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/drivers/hwmon/w83627ehf.c b/drivers/hwmon/w83627ehf.c
index fe960c0a624f..7d7d70afde65 100644
--- a/drivers/hwmon/w83627ehf.c
+++ b/drivers/hwmon/w83627ehf.c
@@ -895,7 +895,7 @@ store_target_temp(struct device *dev, struct device_attribute *attr,
if (err < 0)
return err;
- val = clamp_val(DIV_ROUND_CLOSEST(val, 1000), 0, 127);
+ val = DIV_ROUND_CLOSEST(clamp_val(val, 0, 127000), 1000);
mutex_lock(&data->update_lock);
data->target_temp[nr] = val;
@@ -920,7 +920,7 @@ store_tolerance(struct device *dev, struct device_attribute *attr,
return err;
/* Limit the temp to 0C - 15C */
- val = clamp_val(DIV_ROUND_CLOSEST(val, 1000), 0, 15);
+ val = DIV_ROUND_CLOSEST(clamp_val(val, 0, 15000), 1000);
mutex_lock(&data->update_lock);
reg = w83627ehf_read_value(data, W83627EHF_REG_TOLERANCE[nr]);
--
2.39.2
^ permalink raw reply related [flat|nested] 5+ messages in thread
end of thread, other threads:[~2024-07-07 14:47 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-07-07 14:47 [PATCH 0/4] hwmon: Fix underflows seen when writing limit attributes Guenter Roeck
2024-07-07 14:47 ` [PATCH 1/4] hwmon: (adc128d818) " Guenter Roeck
2024-07-07 14:47 ` [PATCH 2/4] hwmon: (lm95234) " Guenter Roeck
2024-07-07 14:47 ` [PATCH 3/4] hwmon: (nct6775-core) " Guenter Roeck
2024-07-07 14:47 ` [PATCH 4/4] hwmon: (w83627ehf) " Guenter Roeck
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox