Received: by 10.223.185.116 with SMTP id b49csp6352500wrg; Wed, 28 Feb 2018 08:02:51 -0800 (PST) X-Google-Smtp-Source: AH8x224pFpj+KHVT6inaIJsbt2mfIZWh/CuYI9MBnByB73ljcwH4IxVcwcjXIhl/ZsqNnrhR9Af4 X-Received: by 10.99.139.199 with SMTP id j190mr14767594pge.334.1519833771288; Wed, 28 Feb 2018 08:02:51 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1519833771; cv=none; d=google.com; s=arc-20160816; b=QaKZtH0I45vr3POJP+RlDWXsoFRFtYQRJNmR+6k2OWZkHu0D8slblQOJV6usVw+Mur KRYMS5uhaqJvVnFtVmNUyvEhYip3QBHFFVAxejNRk/9mbuhtey4bpacMCQ0spSHz5EDE tA07y2U4XG7RIn008aeZhobtoiqQXpxCid8v4kQEIsiPENyUyHxt5d+PHzSaFBhtBYfJ CdsaFRDWn2Fj+v9//TzYcdUcyB6Ykr6GCagvDBl8uSaHjl1RPc0AV3612UXt9JkvslNc d99MpDB/WNcqHRwx1NPcjRIRffUz4us55S8r+1MfN+yG29pza4PfAXf640tBg0z+y9s0 zlXA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:in-reply-to:subject:message-id:date:cc:to :from:mime-version:content-transfer-encoding:content-disposition :arc-authentication-results; bh=hjj4kWbvf3qXgomW8QYXAJadkPJlylyPIxqPWJIDCno=; b=MBkGNgwWlEi5+CgplSwOjs2TfGRPMKdeTPheTtUcxd8BDGSHF8uYkEqHzGQSyEh5k7 mm4ceDBF9GUS84Lz2mq/kC0a62QGkn/bVi/q+UZOcClA6SyNnkaRFSXYxwh1SPpy8b30 jATrrMcLfcJ1YVfn4T+KMR9wGS9hYkMT4NAWI3tsP14HgNeUp57hm2eYfJovIgP9kBUW Hcb/Ov5vgtt/8nGNz295DPojPKQIXCVUVqOuhJDib6KkebIuOsBCscwldU2BQBsULMef LssPW00K9Kwv9toFgTaROjtn500/FyIroogU0IR2/QTn+iQeV9QQvpbj/Idv++gJTtf+ NBBA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id e6-v6si1471937plt.123.2018.02.28.08.02.32; Wed, 28 Feb 2018 08:02:51 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752679AbeB1QBX (ORCPT + 99 others); Wed, 28 Feb 2018 11:01:23 -0500 Received: from shadbolt.e.decadent.org.uk ([88.96.1.126]:34689 "EHLO shadbolt.e.decadent.org.uk" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S932146AbeB1QBI (ORCPT ); Wed, 28 Feb 2018 11:01:08 -0500 Received: from [2a02:8011:400e:2:6f00:88c8:c921:d332] (helo=deadeye) by shadbolt.decadent.org.uk with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1er3Yv-0006Xc-EA; Wed, 28 Feb 2018 15:22:33 +0000 Received: from ben by deadeye with local (Exim 4.90_1) (envelope-from ) id 1er3Yd-0008NL-CD; Wed, 28 Feb 2018 15:22:15 +0000 Content-Type: text/plain; charset="UTF-8" Content-Disposition: inline Content-Transfer-Encoding: 8bit MIME-Version: 1.0 From: Ben Hutchings To: linux-kernel@vger.kernel.org, stable@vger.kernel.org CC: akpm@linux-foundation.org, "Robert Lippert" , "Robert Lippert" , "Guenter Roeck" Date: Wed, 28 Feb 2018 15:20:18 +0000 Message-ID: X-Mailer: LinuxStableQueue (scripts by bwh) Subject: [PATCH 3.16 017/254] hwmon: (pmbus) Use 64bit math for DIRECT format values In-Reply-To: X-SA-Exim-Connect-IP: 2a02:8011:400e:2:6f00:88c8:c921:d332 X-SA-Exim-Mail-From: ben@decadent.org.uk X-SA-Exim-Scanned: No (on shadbolt.decadent.org.uk); SAEximRunCond expanded to false Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 3.16.55-rc1 review patch. If anyone has any objections, please let me know. ------------------ From: Robert Lippert commit bd467e4eababe4c04272c1e646f066db02734c79 upstream. 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: Ben Hutchings --- drivers/hwmon/pmbus/pmbus_core.c | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) --- a/drivers/hwmon/pmbus/pmbus_core.c +++ b/drivers/hwmon/pmbus/pmbus_core.c @@ -20,6 +20,7 @@ */ #include +#include #include #include #include @@ -443,8 +444,8 @@ static long pmbus_reg2data_linear(struct static long pmbus_reg2data_direct(struct pmbus_data *data, struct pmbus_sensor *sensor) { - long val = (s16) sensor->data; - long m, b, R; + s64 b, val = (s16)sensor->data; + s32 m, R; m = data->info->m[sensor->class]; b = data->info->b[sensor->class]; @@ -472,11 +473,12 @@ static long pmbus_reg2data_direct(struct R--; } while (R < 0) { - val = DIV_ROUND_CLOSEST(val, 10); + val = div_s64(val + 5LL, 10L); /* round closest */ R++; } - return (val - b) / m; + val = div_s64(val - b, m); + return clamp_val(val, LONG_MIN, LONG_MAX); } /* @@ -588,7 +590,8 @@ static u16 pmbus_data2reg_linear(struct static u16 pmbus_data2reg_direct(struct pmbus_data *data, struct pmbus_sensor *sensor, long val) { - long m, b, R; + s64 b, val64 = val; + s32 m, R; m = data->info->m[sensor->class]; b = data->info->b[sensor->class]; @@ -605,18 +608,18 @@ static u16 pmbus_data2reg_direct(struct R -= 3; /* Adjust R and b for data in milli-units */ b *= 1000; } - val = val * m + b; + val64 = val64 * m + b; while (R > 0) { - val *= 10; + val64 *= 10; R--; } while (R < 0) { - val = DIV_ROUND_CLOSEST(val, 10); + val64 = div_s64(val64 + 5LL, 10L); /* round closest */ R++; } - return val; + return (u16)clamp_val(val64, S16_MIN, S16_MAX); } static u16 pmbus_data2reg_vid(struct pmbus_data *data,