Return-Path: Received: from mail-sn1nam01on0105.outbound.protection.outlook.com ([104.47.32.105]:55328 "EHLO NAM01-SN1-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S932427AbeAXEP5 (ORCPT ); Tue, 23 Jan 2018 23:15:57 -0500 From: Sasha Levin To: "linux-kernel@vger.kernel.org" , "stable@vger.kernel.org" CC: Robert Lippert , Robert Lippert , Guenter Roeck , Sasha Levin Subject: [PATCH AUTOSEL for 4.14 063/100] hwmon: (pmbus) Use 64bit math for DIRECT format values Date: Wed, 24 Jan 2018 04:14:56 +0000 Message-ID: <20180124041414.32065-63-alexander.levin@microsoft.com> References: <20180124041414.32065-1-alexander.levin@microsoft.com> In-Reply-To: <20180124041414.32065-1-alexander.levin@microsoft.com> Content-Language: en-US Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Sender: stable-owner@vger.kernel.org List-ID: From: Robert Lippert [ Upstream commit bd467e4eababe4c04272c1e646f066db02734c79 ] Power values in the 100s of watt range can easily blow past 32bit math limits when processing everything in microwatts. Use 64bit math instead to avoid these issues on common 32bit ARM BMC platforms. Fixes: 442aba78728e ("hwmon: PMBus device driver") Signed-off-by: Robert Lippert Signed-off-by: Guenter Roeck Signed-off-by: Sasha Levin --- drivers/hwmon/pmbus/pmbus_core.c | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/drivers/hwmon/pmbus/pmbus_core.c b/drivers/hwmon/pmbus/pmbus_c= ore.c index 52a58b8b6e1b..a139940cd991 100644 --- a/drivers/hwmon/pmbus/pmbus_core.c +++ b/drivers/hwmon/pmbus/pmbus_core.c @@ -21,6 +21,7 @@ =20 #include #include +#include #include #include #include @@ -499,8 +500,8 @@ static long pmbus_reg2data_linear(struct pmbus_data *da= ta, static long pmbus_reg2data_direct(struct pmbus_data *data, struct pmbus_sensor *sensor) { - long val =3D (s16) sensor->data; - long m, b, R; + s64 b, val =3D (s16)sensor->data; + s32 m, R; =20 m =3D data->info->m[sensor->class]; b =3D data->info->b[sensor->class]; @@ -528,11 +529,12 @@ static long pmbus_reg2data_direct(struct pmbus_data *= data, R--; } while (R < 0) { - val =3D DIV_ROUND_CLOSEST(val, 10); + val =3D div_s64(val + 5LL, 10L); /* round closest */ R++; } =20 - return (val - b) / m; + val =3D div_s64(val - b, m); + return clamp_val(val, LONG_MIN, LONG_MAX); } =20 /* @@ -656,7 +658,8 @@ static u16 pmbus_data2reg_linear(struct pmbus_data *dat= a, static u16 pmbus_data2reg_direct(struct pmbus_data *data, struct pmbus_sensor *sensor, long val) { - long m, b, R; + s64 b, val64 =3D val; + s32 m, R; =20 m =3D data->info->m[sensor->class]; b =3D data->info->b[sensor->class]; @@ -673,18 +676,18 @@ static u16 pmbus_data2reg_direct(struct pmbus_data *d= ata, R -=3D 3; /* Adjust R and b for data in milli-units */ b *=3D 1000; } - val =3D val * m + b; + val64 =3D val64 * m + b; =20 while (R > 0) { - val *=3D 10; + val64 *=3D 10; R--; } while (R < 0) { - val =3D DIV_ROUND_CLOSEST(val, 10); + val64 =3D div_s64(val64 + 5LL, 10L); /* round closest */ R++; } =20 - return val; + return (u16)clamp_val(val64, S16_MIN, S16_MAX); } =20 static u16 pmbus_data2reg_vid(struct pmbus_data *data, --=20 2.11.0