From: Jean Delvare <jdelvare@suse.de>
To: Guenter Roeck <linux@roeck-us.net>
Cc: Hardware Monitoring <linux-hwmon@vger.kernel.org>
Subject: Re: [PATCH 06/17] hwmon: (adm1026) Fix overflows seen when writing into limit attributes
Date: Fri, 9 Dec 2016 10:29:18 +0100 [thread overview]
Message-ID: <20161209102918.689d5095@endymion> (raw)
In-Reply-To: <0c8198ed-492f-faa1-1610-69ef2b98dc5e@roeck-us.net>
Hi Guenter,
On Thu, 8 Dec 2016 07:34:35 -0800, Guenter Roeck wrote:
> On 12/08/2016 06:33 AM, Jean Delvare wrote:
> > On Sun, 4 Dec 2016 20:55:29 -0800, Guenter Roeck wrote:
> >> @@ -215,11 +216,11 @@ static int adm1026_scaling[] = { /* .001 Volts */
> >> #define DIV_TO_REG(val) ((val) >= 8 ? 3 : (val) >= 4 ? 2 : (val) >= 2 ? 1 : 0)
> >>
> >> /* Temperature is reported in 1 degC increments */
> >> -#define TEMP_TO_REG(val) (clamp_val(((val) + ((val) < 0 ? -500 : 500)) \
> >> - / 1000, -127, 127))
> >> +#define TEMP_TO_REG(val) DIV_ROUND_CLOSEST(clamp_val(val, -128000, 127000), \
> >> + 1000)
> >> #define TEMP_FROM_REG(val) ((val) * 1000)
> >> -#define OFFSET_TO_REG(val) (clamp_val(((val) + ((val) < 0 ? -500 : 500)) \
> >> - / 1000, -127, 127))
> >> +#define OFFSET_TO_REG(val) DIV_ROUND_CLOSEST(clamp_val(val, -128000, 127000), \
> >> + 1000)
> >
> > Sorry for nitpicking but the original code had -127 °C as the negative
> > limit. You are changing it to -128 °C without a justification. If it
> > matters, it should be at least documented in the commit message. If
> > not, it should be left as it was.
>
> I had seen -128 as value reported by the chip with one of my register dumps,
> which messes up my module tests because it tries to rewrite the value it read
> into the attribute, and that fails. Also, the datasheet lists -128 degrees C
> as a valid register value.
OK, I understand.
> This is one of those philosophical questions, for which I don't have a really
> good answer. Should we clamp to the register limits or to the chip specification ?
> I tend to clamp to register limits, because I think that it should always be possible
> to write back what was read, but we are highly inconsistent in the various drivers.
> Any opinion ?
I have noticed that inconsistency too. Given that we do not have
attributes to expose the limits to user-space, I consider it a nice
feature to clamp the requested limits to what the chip is actually
specified for. But I don't have a strong opinion on it either, and am
not going to spend time "fixing" all drivers not doing it. And you have
a point with being able to write back what was read, especially if the
power-on value isn't withing the specified limits.
> >> (...)
> >> @@ -593,7 +595,10 @@ static ssize_t set_in16_min(struct device *dev, struct device_attribute *attr,
> >> return err;
> >>
> >> mutex_lock(&data->update_lock);
> >> - data->in_min[16] = INS_TO_REG(16, val + NEG12_OFFSET);
> >> + data->in_min[16] = INS_TO_REG(16,
> >> + clamp_val(val, INT_MIN,
> >> + INT_MAX - NEG12_OFFSET) +
> >> + NEG12_OFFSET);
> >> adm1026_write_value(client, ADM1026_REG_IN_MIN[16], data->in_min[16]);
> >> mutex_unlock(&data->update_lock);
> >> return count;
> >> @@ -618,7 +623,10 @@ static ssize_t set_in16_max(struct device *dev, struct device_attribute *attr,
> >> return err;
> >>
> >> mutex_lock(&data->update_lock);
> >> - data->in_max[16] = INS_TO_REG(16, val+NEG12_OFFSET);
> >> + data->in_max[16] = INS_TO_REG(16,
> >> + clamp_val(val, INT_MIN,
> >> + INT_MAX - NEG12_OFFSET) +
> >> + NEG12_OFFSET);
> >> adm1026_write_value(client, ADM1026_REG_IN_MAX[16], data->in_max[16]);
> >> mutex_unlock(&data->update_lock);
> >> return count;
> >
> > On these code paths, you end up calling clamp_val() twice. This could
> > certainly be avoided, but I'm too lazy to do the math ;-)
> >
> I know. Problem here is that there are two overflows, one in the calling code
> (since it does arithmetic on the input value), and another one in INS_TO_REG().
> When I wrote this, I didn't have a good idea how to avoid the first overflow
> without a second clamp.
>
> One possibility would be to define INS_TO_REG_NOCLAMP(). Would that be worth it ?
No, don't bother. God only knows if there's any user left for this
driver ;-)
--
Jean Delvare
SUSE L3 Support
next prev parent reply other threads:[~2016-12-09 9:29 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 [this message]
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
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=20161209102918.689d5095@endymion \
--to=jdelvare@suse.de \
--cc=linux-hwmon@vger.kernel.org \
--cc=linux@roeck-us.net \
/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