From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-by2nam01on0116.outbound.protection.outlook.com ([104.47.34.116]:58271 "EHLO NAM01-BY2-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1032305AbeCAPcv (ORCPT ); Thu, 1 Mar 2018 10:32:51 -0500 From: Sasha Levin To: "stable@vger.kernel.org" , "stable-commits@vger.kernel.org" CC: Robert Lippert , Robert Lippert , Guenter Roeck , Sasha Levin Subject: [added to the 4.1 stable tree] hwmon: (pmbus) Use 64bit math for DIRECT format values Date: Thu, 1 Mar 2018 15:25:29 +0000 Message-ID: <20180301152116.1486-271-alexander.levin@microsoft.com> References: <20180301152116.1486-1-alexander.levin@microsoft.com> In-Reply-To: <20180301152116.1486-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 This patch has been added to the 4.1 stable tree. If you have any objections, please let us know. =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D [ 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 f2e47c7dd808..1362de353076 100644 --- a/drivers/hwmon/pmbus/pmbus_core.c +++ b/drivers/hwmon/pmbus/pmbus_core.c @@ -20,6 +20,7 @@ */ =20 #include +#include #include #include #include @@ -476,8 +477,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]; @@ -505,11 +506,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 /* @@ -621,7 +623,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]; @@ -638,18 +641,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.14.1