* [lm-sensors] [PATCH v3] hwmon: (lm92) Prevent overflow problem when writing large limits
@ 2014-08-05 2:08 Axel Lin
2014-08-05 2:40 ` Guenter Roeck
2014-08-05 2:43 ` Axel Lin
0 siblings, 2 replies; 3+ messages in thread
From: Axel Lin @ 2014-08-05 2:08 UTC (permalink / raw)
To: lm-sensors
On platforms with sizeof(int) < sizeof(long), writing a temperature
limit larger than MAXINT will result in unpredictable limit values
written to the chip. Avoid auto-conversion from long to int to fix
the problem.
The hysteresis temperature range depends on the value of
data->temp[attr->index], since val is subtracted from it.
Use a wider clamp, [-120000, 220000] should do to cover the
possible range.
Also uses clamp_val to simplify the code a bit.
Signed-off-by: Axel Lin <axel.lin@ingics.com>
---
drivers/hwmon/lm92.c | 11 +++++------
1 file changed, 5 insertions(+), 6 deletions(-)
diff --git a/drivers/hwmon/lm92.c b/drivers/hwmon/lm92.c
index d2060e2..08a261d 100644
--- a/drivers/hwmon/lm92.c
+++ b/drivers/hwmon/lm92.c
@@ -74,12 +74,9 @@ static inline int TEMP_FROM_REG(s16 reg)
return reg / 8 * 625 / 10;
}
-static inline s16 TEMP_TO_REG(int val)
+static inline s16 TEMP_TO_REG(long val)
{
- if (val <= -60000)
- return -60000 * 10 / 625 * 8;
- if (val >= 160000)
- return 160000 * 10 / 625 * 8;
+ val = clamp_val(val, -60000, 160000);
return val * 10 / 625 * 8;
}
@@ -206,8 +203,10 @@ static ssize_t set_temp_hyst(struct device *dev,
if (err)
return err;
+ val = clamp_val(val, -120000, 220000);
mutex_lock(&data->update_lock);
- data->temp[t_hyst] = TEMP_FROM_REG(data->temp[attr->index]) - val;
+ data->temp[t_hyst] + TEMP_TO_REG(TEMP_FROM_REG(data->temp[attr->index]) - val);
i2c_smbus_write_word_swapped(client, LM92_REG_TEMP_HYST,
TEMP_TO_REG(data->temp[t_hyst]));
mutex_unlock(&data->update_lock);
--
1.9.1
_______________________________________________
lm-sensors mailing list
lm-sensors@lm-sensors.org
http://lists.lm-sensors.org/mailman/listinfo/lm-sensors
^ permalink raw reply related [flat|nested] 3+ messages in thread
* Re: [lm-sensors] [PATCH v3] hwmon: (lm92) Prevent overflow problem when writing large limits
2014-08-05 2:08 [lm-sensors] [PATCH v3] hwmon: (lm92) Prevent overflow problem when writing large limits Axel Lin
@ 2014-08-05 2:40 ` Guenter Roeck
2014-08-05 2:43 ` Axel Lin
1 sibling, 0 replies; 3+ messages in thread
From: Guenter Roeck @ 2014-08-05 2:40 UTC (permalink / raw)
To: lm-sensors
On Tue, Aug 05, 2014 at 10:08:31AM +0800, Axel Lin wrote:
> On platforms with sizeof(int) < sizeof(long), writing a temperature
> limit larger than MAXINT will result in unpredictable limit values
> written to the chip. Avoid auto-conversion from long to int to fix
> the problem.
>
> The hysteresis temperature range depends on the value of
> data->temp[attr->index], since val is subtracted from it.
> Use a wider clamp, [-120000, 220000] should do to cover the
> possible range.
>
> Also uses clamp_val to simplify the code a bit.
>
> Signed-off-by: Axel Lin <axel.lin@ingics.com>
> ---
> drivers/hwmon/lm92.c | 11 +++++------
> 1 file changed, 5 insertions(+), 6 deletions(-)
>
> diff --git a/drivers/hwmon/lm92.c b/drivers/hwmon/lm92.c
> index d2060e2..08a261d 100644
> --- a/drivers/hwmon/lm92.c
> +++ b/drivers/hwmon/lm92.c
> @@ -74,12 +74,9 @@ static inline int TEMP_FROM_REG(s16 reg)
> return reg / 8 * 625 / 10;
> }
>
> -static inline s16 TEMP_TO_REG(int val)
> +static inline s16 TEMP_TO_REG(long val)
> {
> - if (val <= -60000)
> - return -60000 * 10 / 625 * 8;
> - if (val >= 160000)
> - return 160000 * 10 / 625 * 8;
> + val = clamp_val(val, -60000, 160000);
> return val * 10 / 625 * 8;
> }
>
> @@ -206,8 +203,10 @@ static ssize_t set_temp_hyst(struct device *dev,
> if (err)
> return err;
>
> + val = clamp_val(val, -120000, 220000);
> mutex_lock(&data->update_lock);
> - data->temp[t_hyst] = TEMP_FROM_REG(data->temp[attr->index]) - val;
> + data->temp[t_hyst] > + TEMP_TO_REG(TEMP_FROM_REG(data->temp[attr->index]) - val);
> i2c_smbus_write_word_swapped(client, LM92_REG_TEMP_HYST,
> TEMP_TO_REG(data->temp[t_hyst]));
Hi Axel,
Almost ... unfortunately, now you've got TEMP_TO_REG() on data->temp[t_hyst]
twice. No need to resend, I fixed it up myself.
Thanks,
Guenter
_______________________________________________
lm-sensors mailing list
lm-sensors@lm-sensors.org
http://lists.lm-sensors.org/mailman/listinfo/lm-sensors
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [lm-sensors] [PATCH v3] hwmon: (lm92) Prevent overflow problem when writing large limits
2014-08-05 2:08 [lm-sensors] [PATCH v3] hwmon: (lm92) Prevent overflow problem when writing large limits Axel Lin
2014-08-05 2:40 ` Guenter Roeck
@ 2014-08-05 2:43 ` Axel Lin
1 sibling, 0 replies; 3+ messages in thread
From: Axel Lin @ 2014-08-05 2:43 UTC (permalink / raw)
To: lm-sensors
2014-08-05 10:40 GMT+08:00 Guenter Roeck <linux@roeck-us.net>:
> On Tue, Aug 05, 2014 at 10:08:31AM +0800, Axel Lin wrote:
>> On platforms with sizeof(int) < sizeof(long), writing a temperature
>> limit larger than MAXINT will result in unpredictable limit values
>> written to the chip. Avoid auto-conversion from long to int to fix
>> the problem.
>>
>> The hysteresis temperature range depends on the value of
>> data->temp[attr->index], since val is subtracted from it.
>> Use a wider clamp, [-120000, 220000] should do to cover the
>> possible range.
>>
>> Also uses clamp_val to simplify the code a bit.
>>
>> Signed-off-by: Axel Lin <axel.lin@ingics.com>
>> ---
>> drivers/hwmon/lm92.c | 11 +++++------
>> 1 file changed, 5 insertions(+), 6 deletions(-)
>>
>> diff --git a/drivers/hwmon/lm92.c b/drivers/hwmon/lm92.c
>> index d2060e2..08a261d 100644
>> --- a/drivers/hwmon/lm92.c
>> +++ b/drivers/hwmon/lm92.c
>> @@ -74,12 +74,9 @@ static inline int TEMP_FROM_REG(s16 reg)
>> return reg / 8 * 625 / 10;
>> }
>>
>> -static inline s16 TEMP_TO_REG(int val)
>> +static inline s16 TEMP_TO_REG(long val)
>> {
>> - if (val <= -60000)
>> - return -60000 * 10 / 625 * 8;
>> - if (val >= 160000)
>> - return 160000 * 10 / 625 * 8;
>> + val = clamp_val(val, -60000, 160000);
>> return val * 10 / 625 * 8;
>> }
>>
>> @@ -206,8 +203,10 @@ static ssize_t set_temp_hyst(struct device *dev,
>> if (err)
>> return err;
>>
>> + val = clamp_val(val, -120000, 220000);
>> mutex_lock(&data->update_lock);
>> - data->temp[t_hyst] = TEMP_FROM_REG(data->temp[attr->index]) - val;
>> + data->temp[t_hyst] >> + TEMP_TO_REG(TEMP_FROM_REG(data->temp[attr->index]) - val);
>> i2c_smbus_write_word_swapped(client, LM92_REG_TEMP_HYST,
>> TEMP_TO_REG(data->temp[t_hyst]));
>
> Hi Axel,
>
> Almost ... unfortunately, now you've got TEMP_TO_REG() on data->temp[t_hyst]
> twice. No need to resend, I fixed it up myself.
Ah, thanks.
Axel
_______________________________________________
lm-sensors mailing list
lm-sensors@lm-sensors.org
http://lists.lm-sensors.org/mailman/listinfo/lm-sensors
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2014-08-05 2:43 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2014-08-05 2:08 [lm-sensors] [PATCH v3] hwmon: (lm92) Prevent overflow problem when writing large limits Axel Lin
2014-08-05 2:40 ` Guenter Roeck
2014-08-05 2:43 ` Axel Lin
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.