Received: by 10.223.164.202 with SMTP id h10csp2219820wrb; Mon, 27 Nov 2017 13:44:38 -0800 (PST) X-Google-Smtp-Source: AGs4zMaYUJQJobomV+8MkNQ8PjOg2cZ++rKCpz9/BOTkiWysgXJ8rUWQrCpJRsXE81Mz1N0Ffix5 X-Received: by 10.101.72.76 with SMTP id i12mr19960326pgs.436.1511819078167; Mon, 27 Nov 2017 13:44:38 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1511819078; cv=none; d=google.com; s=arc-20160816; b=udFzGcG1djD3WuNSYq+8IHBAQvprUTKekVqGh9OfyA0XKKVe5kmzU6t7hySRIGLV5i uLnwLKRHWDqGThW64ArKItrbanYvFWFjyupCqeiD7Vz7uPo/7fKW1FmXPfkyvunuOqKp pd5hPk6fwFmuPdJhhrK8GwfPF7UyMD558sX9nVLu+Llb2CxNIl7XcZSsQy59B04NFFhX ulFbSmGT29HuvKGkzTKl6WfTyoEg0+sXuWbYF/ZDabC31JjGq2dI5jWZfc7W1mLj+rei 5W17dk3/lgk1nuqIxjUV01PDthGQuBfuNTSsnQmntOKJb87j/9r+0o1yKJlFHv+ESqKw vPnQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:subject:message-id:date:from :references:in-reply-to:mime-version:dkim-signature :arc-authentication-results; bh=IrF7biGcVq4xkYt9XBkHwQ7iy6LgBuLYvuyuApC3jVE=; b=e3KBAQP41GFy5sX6aFEkc0it9hyDaEVs4x6j8LCh/eiZdcjw0pmx5jUsawpMBDUObk DHnuftMN3QRD3w8F3BgWnVFYyWBN9GVl8il7683qdZ8vOtEFjtM95ShOR4xB6N5HC3yI 0FJpUAqVZv67zqRYll9P6toNyWJRsJ0twRKpsJOQImCDM1VJkwHJGvNxK4Jde7PYeHxO pg3qJx5zsmsKS+NcH9hRUCkoNcU9eIjprBAMb8fESLhc4sReYOovCingllfixM/rOwFf g/49vsbbrxfsv651RYi9enK2Mj+IThV4iPtoyTQVOxoNpmR1ObhMF+WsmmeTuTspKSaQ 6htg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=QhfKhXtH; 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; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id g188si23625831pgc.386.2017.11.27.13.44.26; Mon, 27 Nov 2017 13:44:38 -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; dkim=pass header.i=@google.com header.s=20161025 header.b=QhfKhXtH; 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; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752976AbdK0VnP (ORCPT + 78 others); Mon, 27 Nov 2017 16:43:15 -0500 Received: from mail-qt0-f196.google.com ([209.85.216.196]:40034 "EHLO mail-qt0-f196.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751772AbdK0VnN (ORCPT ); Mon, 27 Nov 2017 16:43:13 -0500 Received: by mail-qt0-f196.google.com with SMTP id u42so41074400qte.7 for ; Mon, 27 Nov 2017 13:43:13 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc; bh=IrF7biGcVq4xkYt9XBkHwQ7iy6LgBuLYvuyuApC3jVE=; b=QhfKhXtHdOzxJOZQMg9rZlHUxLaMepmGbmO5yXV+bZ6u+yYuEBumr8t9K0k9aBZYDc 2JaZACoixBsWuw8CGiDpspANCVjJl4zfbMC86EskNODfiN2mcAeU0FmUrlWLXz+lJ6oj TEmFb03WD0RktmUwpF8KbuQxJ5csbeKa2oNdWhHRqm+O2MbzLOaKzxMCQ8SwA9FgTQk6 MJfGoVXfHsupPeAN52krM3KAegNwl7YL2yVsGgv1GRqhTP31XXmaQgE1ZCIb+Albtq4d 1l4lKxI5UVsTWdiu3ZIKM+9O10LgHGTSwLSZYRG5KddqlDqXAMGVRqoRdM6pYjhZtM9f FI9w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to:cc; bh=IrF7biGcVq4xkYt9XBkHwQ7iy6LgBuLYvuyuApC3jVE=; b=PsDhzz4SHL48VjufPQlo7QgYHpz8ytgkjbCexMVEGBx8wM1qubHg9B3lVd/XMX1Drh O0Jju09Lb9xWmffIjKFLUGwSCPPIxwI6tJbODmw1Xn7pc4fN2A4tW9VBQabr9Z13wNPd R6IcHd98QTe0KuJsgmQbvF5F7QVSiToUsw9Hq7m5CMmuKJrfUTUgmB1jqoAgKf+NWHhk lPS+Y0fK55WLcxQ0uQOKBKXmVuMv8UQV8ys1xj7Nall6ZT9l+lzessg07mCpptG/clxP ZuUMkGrHCFYo2t1+w13gmnQhx143J3vg115Fr99luprglPxMJtlTeKuBXXpR4A6WjFJn z2JA== X-Gm-Message-State: AJaThX5Chps0nhoyj2hT1k3K5StEMlVhOKhdAs3FB/DflvrGbr7Ig7dU jHHLsLjecN0LQjkaAyj2i+q23a2rSA+JLsBsg6jP2w== X-Received: by 10.200.25.134 with SMTP id u6mr58153542qtj.221.1511818992266; Mon, 27 Nov 2017 13:43:12 -0800 (PST) MIME-Version: 1.0 Received: by 10.237.35.17 with HTTP; Mon, 27 Nov 2017 13:42:51 -0800 (PST) In-Reply-To: <20171122222809.GA2951@roeck-us.net> References: <20171122220843.18309-1-rlippert@google.com> <20171122222809.GA2951@roeck-us.net> From: Rob Lippert Date: Mon, 27 Nov 2017 13:42:51 -0800 Message-ID: Subject: Re: [PATCH] hwmon: (pmbus) Use 64bit math for DIRECT format values To: Guenter Roeck Cc: Robert Lippert , linux-hwmon@vger.kernel.org, jdelvare@suse.com, linux-kernel@vger.kernel.org, Xo Wang Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, Nov 22, 2017 at 2:28 PM, Guenter Roeck wrote: > On Wed, Nov 22, 2017 at 02:08:43PM -0800, Robert Lippert wrote: >> 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. >> >> Signed-off-by: Robert Lippert >> --- >> 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_core.c >> index 52a58b8b6e1b..f4efea9f282e 100644 >> --- a/drivers/hwmon/pmbus/pmbus_core.c >> +++ b/drivers/hwmon/pmbus/pmbus_core.c >> @@ -499,8 +499,8 @@ static long pmbus_reg2data_linear(struct pmbus_data *data, >> static long pmbus_reg2data_direct(struct pmbus_data *data, >> struct pmbus_sensor *sensor) >> { >> - long val = (s16) sensor->data; >> - long m, b, R; >> + s64 val = (s16) sensor->data; >> + s64 m, b, R; >> >> m = data->info->m[sensor->class]; >> b = data->info->b[sensor->class]; >> @@ -528,11 +528,13 @@ static long pmbus_reg2data_direct(struct pmbus_data *data, >> R--; >> } >> while (R < 0) { >> - val = DIV_ROUND_CLOSEST(val, 10); >> + do_div(val, 10); > > Any reason to not use DIV_ROUND_CLOSEST_ULL ? That macro doesn't quite work for signed division. v2 changes this to use the signed 64bit division functions and I emulated the "round closest" by just adding 5 before dividing. > >> R++; >> } >> >> - return (val - b) / m; >> + val = val - b; >> + do_div(val, m); >> + return val; > > Can this overflow ? Added clamp() to the return values in v2 to prevent overflow when returning a narrower type. > >> } >> >> /* >> @@ -656,7 +658,8 @@ static u16 pmbus_data2reg_linear(struct pmbus_data *data, >> static u16 pmbus_data2reg_direct(struct pmbus_data *data, >> struct pmbus_sensor *sensor, long val) >> { >> - long m, b, R; >> + s64 m, b, R; >> + s64 val64 = val; >> >> m = data->info->m[sensor->class]; >> b = data->info->b[sensor->class]; >> @@ -673,18 +676,18 @@ static u16 pmbus_data2reg_direct(struct pmbus_data *data, >> 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); >> + do_div(val64, 10); > > Same here. > >> R++; >> } >> >> - return val; >> + return (u16) val64; > > Can this overflow ? > >> } >> >> static u16 pmbus_data2reg_vid(struct pmbus_data *data, >> -- >> 2.15.0.448.gf294e3d99a-goog >> From 1585071317620695234@xxx Sat Nov 25 20:30:05 +0000 2017 X-GM-THRID: 1584805833119531257 X-Gmail-Labels: Inbox,Category Forums,HistoricalUnread