Received: by 2002:a05:6358:f14:b0:e5:3b68:ec04 with SMTP id b20csp6216637rwj; Wed, 21 Dec 2022 12:20:09 -0800 (PST) X-Google-Smtp-Source: AMrXdXsKAAVbjriM+79W/FGmSRE37gHQ6kuTXzwyjK6EyEQtxhbOQw21i1Yp6jgdOJS1KRDleAVm X-Received: by 2002:aa7:cd09:0:b0:470:72d9:a9ed with SMTP id b9-20020aa7cd09000000b0047072d9a9edmr2630545edw.7.1671654009189; Wed, 21 Dec 2022 12:20:09 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1671654009; cv=none; d=google.com; s=arc-20160816; b=f/B5nPQcsKaKAz7kFjbZHi0GesuHzDwYk60r9JnSPAgE394MevEAqu1pSpv92k9EcV pzQ6SomIIcFPN5JJb5d6ih1Zq7uR2pwxtDJZMIbswyeHuhNhlxaL3TrwUsfRoydzwU1t VA6KCRWdSbQWjZWD3mKxsS01wRm4Y6OYL3b6CbJQkaAY9rPDJGi2PJVKaDGA+cnkbiX2 nwrBkL+aGpJ42eNb4r0goWjyEEHzJQbHa2Ph3rEfAm/YhX5OkyEYcO/BfnyJYmjo/gTo D3Z5MxCmEunejilD1m/G8V9rEavSRFEvZhuNbNmxlS2TqviIJ92Eccdn+sGNhsLQg+BE YJYg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:organization:in-reply-to:content-disposition :mime-version:references:message-id:subject:cc:to:from:date :dkim-signature; bh=cTUZTRwe3Vrdcam9sOIFUZT/WBR3EK6Gnm8vt3OLhUk=; b=m22uZjDl4VNu2MNcZ+yLPJUxKa4oAJOmIXtJ8eBKk0stEo1f7P60pbb9A54tJSr3+C hWQzyPO5M4fPyR0XD4WpPJT75vW+om5Es4Ao6AC7qrj2SXTwOHiZBoMAHodzosblOtf4 1AWuku2rGSZmR1nYt92vaNfES85Uqk5C4gN3nyvKUgTu6mU9inhSLbkyjFM2CfeBAXSt hxpiqwtpRvxkxS41PnGjGm/fG86hRVvw77fI/Smgnw033X7kwP1j03x/b9haWZUX5SeD O1RFCjXEDb/hancUiwYQAUOddCWiEQVb4FkbMha0qJY333e2cN5Q+H4DJFjS0lC6viO8 ggFA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=fDlPFtav; 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=intel.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id cy8-20020a0564021c8800b004674c93ecb8si13121579edb.326.2022.12.21.12.19.51; Wed, 21 Dec 2022 12:20:09 -0800 (PST) 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=@intel.com header.s=Intel header.b=fDlPFtav; 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=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234764AbiLUUID (ORCPT + 67 others); Wed, 21 Dec 2022 15:08:03 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50248 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229578AbiLUUIB (ORCPT ); Wed, 21 Dec 2022 15:08:01 -0500 Received: from mga02.intel.com (mga02.intel.com [134.134.136.20]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8D18020BD5; Wed, 21 Dec 2022 12:08:00 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1671653280; x=1703189280; h=date:from:to:cc:subject:message-id:references: mime-version:in-reply-to; bh=aLxKIfV0JnJSlwyEArfN5S1veu6mHIJ+yOsHfgJEft8=; b=fDlPFtavXjaDAxhIqOJhG1t3pDxZUViXNuFek+qgwsB57OdXy5UZ4qlm HcGOZz6JafT/qt8/763JVe10SR+DmxqpAy3xAP7u6j6VwpA3SWsgwVl/2 5M9MAZ32asX1BLocK2oJHPgyJp2ik8tf0G+ZyUP6dKmT0ruBJKZ+2E4rF qA3SvclHb4S/tlGThs3peo1JD+4ZseoUSTAyPlozYCsOxRLSGaal23JVl 841PFxeJ7nRfEarYyunnVYUxaBfQ9ChT/JlUdnU73fem79g5e8/B4CQzZ i1/FC6qqEXitqkcpQITmfQEDxj1HqFG3ofM+utlylFpVtV9eduLBcwVDD w==; X-IronPort-AV: E=McAfee;i="6500,9779,10568"; a="307647026" X-IronPort-AV: E=Sophos;i="5.96,263,1665471600"; d="scan'208";a="307647026" Received: from orsmga001.jf.intel.com ([10.7.209.18]) by orsmga101.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 21 Dec 2022 12:07:51 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6500,9779,10568"; a="683932518" X-IronPort-AV: E=Sophos;i="5.96,263,1665471600"; d="scan'208";a="683932518" Received: from smile.fi.intel.com ([10.237.72.54]) by orsmga001.jf.intel.com with ESMTP; 21 Dec 2022 12:07:47 -0800 Received: from andy by smile.fi.intel.com with local (Exim 4.96) (envelope-from ) id 1p85NQ-00De9t-3B; Wed, 21 Dec 2022 22:07:44 +0200 Date: Wed, 21 Dec 2022 22:07:44 +0200 From: Andy Shevchenko To: Hanna Hawa Cc: wsa@kernel.org, jarkko.nikula@linux.intel.com, mika.westerberg@linux.intel.com, jsd@semihalf.com, linus.walleij@linaro.org, ben-linux@fluff.org, linux-i2c@vger.kernel.org, linux-kernel@vger.kernel.org, dwmw@amazon.co.uk, benh@amazon.com, ronenk@amazon.com, talel@amazon.com, jonnyc@amazon.com, hanochu@amazon.com, farbere@amazon.com, itamark@amazon.com, lareine@amazon.com Subject: Re: [PATCH v5 1/1] i2c: designware: use casting of u64 in clock multiplication to avoid overflow Message-ID: References: <20221221195900.23276-1-hhhawa@amazon.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20221221195900.23276-1-hhhawa@amazon.com> Organization: Intel Finland Oy - BIC 0357606-4 - Westendinkatu 7, 02160 Espoo X-Spam-Status: No, score=-4.3 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_EF,RCVD_IN_DNSWL_MED, RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE,SPF_NONE 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: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, Dec 21, 2022 at 07:59:00PM +0000, Hanna Hawa wrote: > From: Lareine Khawaly > > In functions i2c_dw_scl_lcnt() and i2c_dw_scl_hcnt() may have overflow > by depending on the values of the given parameters including the ic_clk. > For example in our use case where ic_clk is larger than one million, > multiplication of ic_clk * 4700 will result in 32 bit overflow. > > Add cast of u64 to the calculation to avoid multiplication overflow, and > use the corresponding define for divide. Perfect, thank you! Reviewed-by: Andy Shevchenko > Fixes: 2373f6b9744d ("i2c-designware: split of i2c-designware.c into core and bus specific parts") > Signed-off-by: Lareine Khawaly > Signed-off-by: Hanna Hawa > --- > Change log v4->v5: > - Fix indentation > > Change Log v3->v4: > - update line length when possible > - fix change log location in the patch > > Change Log v2->v3: > - Avoid changing the ic_clk parameter to u64, and do casting in the > calculation itself instead. > - i2c_dw_clk_rate() returns unsigned long which is confusing because the > function return the value of get_clk_rate_khz() which returns u32. > This is not effect the overflow issue, pushed change in separated > patch. > - use DIV_ROUND_CLOSEST_ULL instead of DIV_ROUND_CLOSEST > > Change Log v1->v2: > - Update commit message and add fix tag. > > drivers/i2c/busses/i2c-designware-common.c | 9 ++++++--- > 1 file changed, 6 insertions(+), 3 deletions(-) > > diff --git a/drivers/i2c/busses/i2c-designware-common.c b/drivers/i2c/busses/i2c-designware-common.c > index e0a46dfd1c15..6fdb25a5f801 100644 > --- a/drivers/i2c/busses/i2c-designware-common.c > +++ b/drivers/i2c/busses/i2c-designware-common.c > @@ -351,7 +351,8 @@ u32 i2c_dw_scl_hcnt(u32 ic_clk, u32 tSYMBOL, u32 tf, int cond, int offset) > * > * If your hardware is free from tHD;STA issue, try this one. > */ > - return DIV_ROUND_CLOSEST(ic_clk * tSYMBOL, MICRO) - 8 + offset; > + return DIV_ROUND_CLOSEST_ULL((u64)ic_clk * tSYMBOL, MICRO) - > + 8 + offset; > else > /* > * Conditional expression: > @@ -367,7 +368,8 @@ u32 i2c_dw_scl_hcnt(u32 ic_clk, u32 tSYMBOL, u32 tf, int cond, int offset) > * The reason why we need to take into account "tf" here, > * is the same as described in i2c_dw_scl_lcnt(). > */ > - return DIV_ROUND_CLOSEST(ic_clk * (tSYMBOL + tf), MICRO) - 3 + offset; > + return DIV_ROUND_CLOSEST_ULL((u64)ic_clk * (tSYMBOL + tf), MICRO) - > + 3 + offset; > } > > u32 i2c_dw_scl_lcnt(u32 ic_clk, u32 tLOW, u32 tf, int offset) > @@ -383,7 +385,8 @@ u32 i2c_dw_scl_lcnt(u32 ic_clk, u32 tLOW, u32 tf, int offset) > * account the fall time of SCL signal (tf). Default tf value > * should be 0.3 us, for safety. > */ > - return DIV_ROUND_CLOSEST(ic_clk * (tLOW + tf), MICRO) - 1 + offset; > + return DIV_ROUND_CLOSEST_ULL((u64)ic_clk * (tLOW + tf), MICRO) - > + 1 + offset; > } > > int i2c_dw_set_sda_hold(struct dw_i2c_dev *dev) > -- > 2.38.1 > -- With Best Regards, Andy Shevchenko