From: Guenter Roeck <linux@roeck-us.net>
To: Jean Delvare <jdelvare@suse.de>
Cc: Hardware Monitoring <linux-hwmon@vger.kernel.org>
Subject: Re: [PATCH 01/17] hwmon: (adm9240) Fix overflows seen when writing into limit attributes
Date: Thu, 8 Dec 2016 07:18:37 -0800 [thread overview]
Message-ID: <157c8cef-2628-3355-51d0-fb45672391f8@roeck-us.net> (raw)
In-Reply-To: <20161208142910.425f1b0f@endymion>
On 12/08/2016 05:29 AM, Jean Delvare wrote:
> Hi Guenter,
>
> On Sun, 4 Dec 2016 20:55:24 -0800, Guenter Roeck wrote:
>> Module test reports:
>>
>> in0_min: Suspected overflow: [3320 vs. 0]
>> in0_max: Suspected overflow: [3320 vs. 0]
>> in4_min: Suspected overflow: [15938 vs. 0]
>> in4_max: Suspected overflow: [15938 vs. 0]
>> temp1_max: Suspected overflow: [127000 vs. 0]
>> temp1_max_hyst: Suspected overflow: [127000 vs. 0]
>> aout_output: Suspected overflow: [1250 vs. 0]
>>
>> Code analysis reveals that the overflows are caused by conversions
>> from unsigned long to long to int, combined with multiplications on
>> passed values.
>>
>> Signed-off-by: Guenter Roeck <linux@roeck-us.net>
>> ---
>> drivers/hwmon/adm9240.c | 3 +++
>> 1 file changed, 3 insertions(+)
>>
>> diff --git a/drivers/hwmon/adm9240.c b/drivers/hwmon/adm9240.c
>> index 2fe1828bd10b..347afacedcf5 100644
>> --- a/drivers/hwmon/adm9240.c
>> +++ b/drivers/hwmon/adm9240.c
>> @@ -98,12 +98,14 @@ static inline unsigned int IN_FROM_REG(u8 reg, int n)
>>
>> static inline u8 IN_TO_REG(unsigned long val, int n)
>> {
>> + val = clamp_val(val, 0, INT_MAX / 192 - 12000);
>> return clamp_val(SCALE(val, 192, nom_mv[n]), 0, 255);
>> }
>
> I understand the idea of clamping before the conversion to avoid the
> overflow. However I would have hoped that clamping the input makes
> clamping the output unneeded. Clamping is full of tests, which aren't
> cheap as they break the CPU instruction prediction, so we should not
> abuse it.
>
I am not that much concerned about this here, since the limits are not
usually set continuously. I agree though, since it is always better
to keep the code as simple as possible.
> Would the following work?
>
> static inline u8 IN_TO_REG(unsigned long val, int n)
> {
> val = clamp_val(val, 0, nom_mv[n] * 255 / 192);
> return SCALE(val, 192, nom_mv[n]);
> }
>
> This should be more compact and faster.
>
>>
>> /* temperature range: -40..125, 127 disables temperature alarm */
>> static inline s8 TEMP_TO_REG(long val)
>> {
>> + val = clamp_val(val, INT_MIN + 1000, INT_MAX - 1000);
>> return clamp_val(SCALE(val, 1, 1000), -40, 127);
>> }
>>
>> @@ -122,6 +124,7 @@ static inline unsigned int FAN_FROM_REG(u8 reg, u8 div)
>> /* analog out 0..1250mV */
>> static inline u8 AOUT_TO_REG(unsigned long val)
>> {
>> + val = clamp_val(val, 0, INT_MAX / 255 - 1250);
>> return clamp_val(SCALE(val, 255, 1250), 0, 255);
>> }
>>
>
> Same comment and same suggested solution for these two functions:
>
> /* temperature range: -40..125, 127 disables temperature alarm */
> static inline s8 TEMP_TO_REG(long val)
> {
> val = clamp_val(val, -40000, 127000);
> return SCALE(val, 1, 1000);
> }
>
> /* analog out 0..1250mV */
> static inline u8 AOUT_TO_REG(unsigned long val)
> {
> val = clamp_val(val, 0, 1250);
> return SCALE(val, 255, 1250);
> }
>
Should work. I'll give it a try.
Guenter
prev parent reply other threads:[~2016-12-08 15:18 UTC|newest]
Thread overview: 48+ messages / expand[flat|nested] mbox.gz Atom feed top
2016-12-05 4:55 [PATCH 01/17] hwmon: (adm9240) Fix overflows seen when writing into limit attributes Guenter Roeck
2016-12-05 4:55 ` [PATCH 02/17] hwmon: (ds620) Fix overflows seen when writing temperature limits Guenter Roeck
2016-12-08 13:47 ` Jean Delvare
2016-12-05 4:55 ` [PATCH 03/17] hwmon: (lm93) Fix overflows seen when writing into limit attributes Guenter Roeck
2016-12-13 14:01 ` Jean Delvare
2016-12-13 14:52 ` Guenter Roeck
2016-12-05 4:55 ` [PATCH 04/17] hwmon: (smsc47m192) " Guenter Roeck
2016-12-08 13:57 ` Jean Delvare
2016-12-08 19:24 ` Guenter Roeck
2016-12-05 4:55 ` [PATCH 05/17] hwmon: (adm1025) Fix overflows seen when writing voltage limits Guenter Roeck
2016-12-08 14:04 ` Jean Delvare
2016-12-05 4:55 ` [PATCH 06/17] hwmon: (adm1026) Fix overflows seen when writing into limit attributes Guenter Roeck
2016-12-08 14:33 ` Jean Delvare
2016-12-08 15:34 ` Guenter Roeck
2016-12-09 9:29 ` Jean Delvare
2016-12-05 4:55 ` [PATCH 07/17] hwmon: (adt7462) " Guenter Roeck
2016-12-08 15:08 ` Jean Delvare
2016-12-05 4:55 ` [PATCH 08/17] hwmon: (adt7470) " Guenter Roeck
2016-12-08 15:14 ` Jean Delvare
2016-12-08 18:14 ` Guenter Roeck
2016-12-05 4:55 ` [PATCH 09/17] hwmon: (nct7802) " Guenter Roeck
2016-12-09 9:49 ` Jean Delvare
2016-12-09 14:22 ` Guenter Roeck
2016-12-09 15:25 ` Jean Delvare
2016-12-09 18:11 ` Guenter Roeck
2016-12-05 4:55 ` [PATCH 10/17] hwmon: (lm87) Fix overflow seen when writing voltage " Guenter Roeck
2016-12-09 15:07 ` Jean Delvare
2016-12-05 4:55 ` [PATCH 11/17] hwmon: (lm85) Fix overflows " Guenter Roeck
2016-12-09 16:07 ` Jean Delvare
2016-12-05 4:55 ` [PATCH 12/17] hwmon: (dme1737) Fix overflows seen when writing into " Guenter Roeck
2016-12-12 9:33 ` Jean Delvare
2016-12-12 14:21 ` Guenter Roeck
2016-12-05 4:55 ` [PATCH 13/17] hwmon: (emc2103) Fix overflows seen when temperature " Guenter Roeck
2016-12-12 10:44 ` Jean Delvare
2016-12-05 4:55 ` [PATCH 14/17] hwmon: (emcw201) Fix overflows seen when writing into " Guenter Roeck
2016-12-12 10:48 ` Jean Delvare
2016-12-12 14:23 ` Guenter Roeck
2016-12-05 4:55 ` [PATCH 15/17] hwmln: (g762) Fix overflows and crash seen when writing " Guenter Roeck
2016-12-12 11:14 ` Jean Delvare
2016-12-12 14:19 ` Guenter Roeck
2016-12-05 4:55 ` [PATCH 16/17] hwmon: (gl518sm) Fix overflows seen when writing into " Guenter Roeck
2016-12-13 9:48 ` Jean Delvare
2016-12-13 21:56 ` Guenter Roeck
2016-12-05 4:55 ` [PATCH 17/17] hwmon: (gl520sm) " Guenter Roeck
2016-12-13 9:56 ` Jean Delvare
2016-12-13 14:49 ` Guenter Roeck
2016-12-08 13:29 ` [PATCH 01/17] hwmon: (adm9240) " Jean Delvare
2016-12-08 15:18 ` Guenter Roeck [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=157c8cef-2628-3355-51d0-fb45672391f8@roeck-us.net \
--to=linux@roeck-us.net \
--cc=jdelvare@suse.de \
--cc=linux-hwmon@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).