Message ID | 20221028224052.293693-1-megi@xff.cz |
---|---|
State | New |
Headers |
Return-Path: <linux-kernel-owner@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp1079189wru; Fri, 28 Oct 2022 15:45:47 -0700 (PDT) X-Google-Smtp-Source: AMsMyM63QOrJQyYx+yY+Dg3pa8EYkn2daEpgXD435QzzifJ1+EnA0cvpbmILrZiJB6pzl7tqomwX X-Received: by 2002:a05:6402:26ca:b0:462:7f27:a0dd with SMTP id x10-20020a05640226ca00b004627f27a0ddmr1668979edd.132.1666997147022; Fri, 28 Oct 2022 15:45:47 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1666997147; cv=none; d=google.com; s=arc-20160816; b=ItS7SyUmcmT9ftKem8q/mIDlSpTCtkQLAcL6hFWVSMPA+Sx3kipxTrwXiYzezC+3gE dtvIc/Q+ThI4yUKrkd6YUqvUNZP2qcBdiCLBUxsNQK1m37S2KfUZ2C8i7p1g+0ti/rSo yGPTJLm8Kb5BYBQBQDckrAe4mB9vUVjIEEL7zvZJy64cBBOcElRcBUh0xCsv9yJCCkly fCGUMeuug3huO2VY6Z1F0tm91jHI6Pc7TH+1GzB/egWFNhmKrHGDd+PE67bP+bGMBnGI CFPHyLNMmm94TaKtABQr10c7jU8NbdHNHpzKUEd0K2DcxG+I+bLtH8txL1flxb6rZCOE y/iw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from:dkim-signature; bh=sijITe/ZpsFfdWris7uNJS58AyGje2V80AYUTpGPGE8=; b=u8k+2r0NBUG0zqHW6+XJyMlTqRfQAP/Y45fIvTkRdHNB+jAZUWryvZ4d/JThxPbxbE /TeaBBjGpGRD6MyjnMudju3brrubL5rSIPp034hJuziSEJ1HgOzx/EU9NvntEV7gDuFv Jm+71+ZWEQdXoCRzll9WkiMjAd6wZK72A+rHLhPRG0+CYU9hVliTGOqiuDICdLqP8y9a xWVl5Tl6fcBPYSFRKbg13ss9sJ1blKl7XSbxSlnpTSaTeZRmgCIyX4TuRDG7SUVCScpP VjzBxF/brCJ1iSiBiuRgAUgFAqu73CDo9dLzgdHg44NiFRK1Nuv/TgRTXIghQ/bPdzar VaCw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@xff.cz header.s=mail header.b=VTvQGgAf; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=xff.cz Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id z2-20020a50cd02000000b0045c93142111si3687646edi.70.2022.10.28.15.45.23; Fri, 28 Oct 2022 15:45:47 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@xff.cz header.s=mail header.b=VTvQGgAf; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=xff.cz Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229727AbiJ1WlU (ORCPT <rfc822;pusanteemu@gmail.com> + 99 others); Fri, 28 Oct 2022 18:41:20 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42478 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229833AbiJ1WlQ (ORCPT <rfc822;linux-kernel@vger.kernel.org>); Fri, 28 Oct 2022 18:41:16 -0400 Received: from vps.xff.cz (vps.xff.cz [195.181.215.36]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 52C11DBBD2 for <linux-kernel@vger.kernel.org>; Fri, 28 Oct 2022 15:41:11 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=xff.cz; s=mail; t=1666996863; bh=9d6DFM/pqmRLeIc4cMgiNgVS1Vl84NV0vez6YtnLyiE=; h=From:To:Cc:Subject:Date:From; b=VTvQGgAfRWqUs+Btn43EM6g7Yo+S/b8Q0eBsjl3FEoDRNIdM2lNzPegowq/mKD7Ao vFNmfipTSDFS7ZGzsKQ4hE1Qe6nmGpiQ6d8QP2cVYn2cKNAqSTeFrlpbdgg74kcz3f XirwgNtITZGvMC9FvEx8WVwZpIUkG6l1T8jErpFc= From: Ondrej Jirman <megi@xff.cz> To: linux-rockchip@lists.infradead.org Cc: Ondrej Jirman <megi@xff.cz>, Samuel Holland <samuel@sholland.org>, Sebastian Reichel <sre@kernel.org>, linux-pm@vger.kernel.org (open list:POWER SUPPLY CLASS/SUBSYSTEM and DRIVERS), linux-kernel@vger.kernel.org (open list) Subject: [PATCH] power: supply: ip5xxx: Fix integer overflow in current_now calculation Date: Sat, 29 Oct 2022 00:40:52 +0200 Message-Id: <20221028224052.293693-1-megi@xff.cz> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,SPF_PASS,T_SPF_HELO_TEMPERROR autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: <linux-kernel.vger.kernel.org> X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1747973200209141892?= X-GMAIL-MSGID: =?utf-8?q?1747973200209141892?= |
Series |
power: supply: ip5xxx: Fix integer overflow in current_now calculation
|
|
Commit Message
Ondřej Jirman
Oct. 28, 2022, 10:40 p.m. UTC
When current is larger than ~2A, the multiplication in current_now
property overflows and the kernel reports invalid negative current
value. Change the numerator and denominator while preserving their
ratio to allow up to +-6A before the overflow.
Fixes: 75853406fa27 ("power: supply: Add a driver for Injoinic power bank ICs")
Signed-off-by: Ondrej Jirman <megi@xff.cz>
---
drivers/power/supply/ip5xxx_power.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
Comments
On 10/28/22 17:40, Ondrej Jirman wrote: > When current is larger than ~2A, the multiplication in current_now > property overflows and the kernel reports invalid negative current > value. Change the numerator and denominator while preserving their > ratio to allow up to +-6A before the overflow. > > Fixes: 75853406fa27 ("power: supply: Add a driver for Injoinic power bank ICs") > Signed-off-by: Ondrej Jirman <megi@xff.cz> Reviewed-by: Samuel Holland <samuel@sholland.org> > --- > drivers/power/supply/ip5xxx_power.c | 2 +- > 1 file changed, 1 insertion(+), 1 deletion(-) > > diff --git a/drivers/power/supply/ip5xxx_power.c b/drivers/power/supply/ip5xxx_power.c > index 02ee4d252a3e..f39cb2f7aa11 100644 > --- a/drivers/power/supply/ip5xxx_power.c > +++ b/drivers/power/supply/ip5xxx_power.c > @@ -398,7 +398,7 @@ static int ip5xxx_battery_get_property(struct power_supply *psy, > ret = ip5xxx_battery_read_adc(ip5xxx, IP5XXX_BATIADC_DAT0, > IP5XXX_BATIADC_DAT1, &raw); > > - val->intval = DIV_ROUND_CLOSEST(raw * 745985, 1000); > + val->intval = DIV_ROUND_CLOSEST(raw * 261095, 350); DIV_ROUND_CLOSEST(raw * 149197, 200) would be marginally more accurate, but it doesn't matter in practice. > return 0; > > case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT:
Hi, On Fri, Oct 28, 2022 at 06:06:07PM -0500, Samuel Holland wrote: > On 10/28/22 17:40, Ondrej Jirman wrote: > > When current is larger than ~2A, the multiplication in current_now > > property overflows and the kernel reports invalid negative current > > value. Change the numerator and denominator while preserving their > > ratio to allow up to +-6A before the overflow. > > > > Fixes: 75853406fa27 ("power: supply: Add a driver for Injoinic power bank ICs") > > Signed-off-by: Ondrej Jirman <megi@xff.cz> > > Reviewed-by: Samuel Holland <samuel@sholland.org> > > > --- > > drivers/power/supply/ip5xxx_power.c | 2 +- > > 1 file changed, 1 insertion(+), 1 deletion(-) > > > > diff --git a/drivers/power/supply/ip5xxx_power.c b/drivers/power/supply/ip5xxx_power.c > > index 02ee4d252a3e..f39cb2f7aa11 100644 > > --- a/drivers/power/supply/ip5xxx_power.c > > +++ b/drivers/power/supply/ip5xxx_power.c > > @@ -398,7 +398,7 @@ static int ip5xxx_battery_get_property(struct power_supply *psy, > > ret = ip5xxx_battery_read_adc(ip5xxx, IP5XXX_BATIADC_DAT0, > > IP5XXX_BATIADC_DAT1, &raw); > > > > - val->intval = DIV_ROUND_CLOSEST(raw * 745985, 1000); > > + val->intval = DIV_ROUND_CLOSEST(raw * 261095, 350); > > DIV_ROUND_CLOSEST(raw * 149197, 200) would be marginally more accurate, > but it doesn't matter in practice. Thanks, I queued it with that change included. -- Sebastian > > > return 0; > > > > case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT: >
diff --git a/drivers/power/supply/ip5xxx_power.c b/drivers/power/supply/ip5xxx_power.c index 02ee4d252a3e..f39cb2f7aa11 100644 --- a/drivers/power/supply/ip5xxx_power.c +++ b/drivers/power/supply/ip5xxx_power.c @@ -398,7 +398,7 @@ static int ip5xxx_battery_get_property(struct power_supply *psy, ret = ip5xxx_battery_read_adc(ip5xxx, IP5XXX_BATIADC_DAT0, IP5XXX_BATIADC_DAT1, &raw); - val->intval = DIV_ROUND_CLOSEST(raw * 745985, 1000); + val->intval = DIV_ROUND_CLOSEST(raw * 261095, 350); return 0; case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT: