lists.openwall.net   lists  /  announce  owl-users  owl-dev  john-users  john-dev  passwdqc-users  yescrypt  popa3d-users  /  oss-security  kernel-hardening  musl  sabotage  tlsify  passwords  /  crypt-dev  xvendor  /  Bugtraq  Full-Disclosure  linux-kernel  linux-netdev  linux-ext4  linux-hardening  linux-cve-announce  PHC 
Open Source and information security mailing list archives
 
Hash Suite: Windows password security audit tool. GUI, reports in PDF.
[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <29fa5c01-aad0-04ff-e1a9-1510858eff7e@amazon.com>
Date:   Tue, 6 Sep 2022 16:27:13 +0300
From:   "Farber, Eliav" <farbere@...zon.com>
To:     Andy Shevchenko <andriy.shevchenko@...el.com>
CC:     <jdelvare@...e.com>, <linux@...ck-us.net>, <robh+dt@...nel.org>,
        <p.zabel@...gutronix.de>, <rtanwar@...linear.com>,
        <linux-hwmon@...r.kernel.org>, <devicetree@...r.kernel.org>,
        <linux-kernel@...r.kernel.org>, <hhhawa@...zon.com>,
        <jonnyc@...zon.com>, "Farber, Eliav" <farbere@...zon.com>
Subject: Re: [PATCH v4 05/21] hwmon: (mr75203) fix voltage equation for negative
 source input

On 9/6/2022 3:03 PM, Andy Shevchenko wrote:
> On Tue, Sep 06, 2022 at 08:33:40AM +0000, Eliav Farber wrote:
>> According to Moortec Embedded Voltage Monitor (MEVM) series 3 data
>> sheet, the minimum input signal is -100mv and maximum input signal
>> is +1000mv.
>>
>> The equation used to convert the digital word to voltage uses mixed
>> types (*val signed and n unsigned), and on 64 bit machines also has
>> different size, since sizeof(u32) = 4 and sizeof(long) = 8.
>>
>> So when measuring a negative input, n will be small enough, such that
>> PVT_N_CONST * n < PVT_R_CONST, and the result of
>> (PVT_N_CONST * n - PVT_R_CONST) will overflow to a very big positive
>> 32 bit number. Then when storing the result in *val it will be the same
>> value just in 64 bit (instead of it representing a negative number which
>> will what happen when sizeof(long) = 4).
>>
>> When -1023 <= (PVT_N_CONST * n - PVT_R_CONST) <= -1
>> dividing the number by 1024 should result of in 0, but because ">> 10"
>> is used it results in -1 (0xf...fffff).
>>
>> This change fixes the sign problem and supports negative values by
>> casting n to long and replacing the shift right with div operation.
>
> This is really downside of C...
>
> ...
>
>> -             *val = (PVT_N_CONST * n - PVT_R_CONST) >> PVT_CONV_BITS;
>> +             *val = (PVT_N_CONST * (long)n - PVT_R_CONST) / (1 << 
>> PVT_CONV_BITS);
>
> Wondering if we can use BIT(PVT_CONV_BITS) for two (quite unlikely to 
> happen,
> I hope) purposes:
>
> 1) Somebody copies such code where PVT_CONV_BITS analogue can be 31,
>   which is according to C standard is UB (undefined behaviour).
>
> 2) It makes shorter the line and also drops the pattern where some
>   dumb robot may propose a patch to basically revert the division
>   change. 
I originally tried to use BIT(PVT_CONV_BITS) but it gave a different
result.
e.g.
If n = 2720
*val = (PVT_N_CONST * (long)n - PVT_R_CONST) / (1 << PVT_CONV_BITS) = 0
*val = (PVT_N_CONST * (long)n - PVT_R_CONST) / BIT(PVT_CONV_BITS) = 
18014398509481983

I can try fitting it in one line, either by adding a define for
(1 << PVT_CONV_BITS) or exceeding 80 characters, but keep in mind that
in a later patch (#15) it gets even longer (and I must use more than
one line) since it is multiplied by a pre-scaler factor.

--
Regards, Eliav

Powered by blists - more mailing lists

Powered by Openwall GNU/*/Linux Powered by OpenVZ