Received: by 2002:a05:6a10:f3d0:0:0:0:0 with SMTP id a16csp924672pxv; Fri, 9 Jul 2021 12:24:10 -0700 (PDT) X-Google-Smtp-Source: ABdhPJz+tYEdz8AA6aoocmBaEyYNM2knaVU5gPEHvS/UjW6XqCrymEOyAg9cvM6uTS9lA8oX2uir X-Received: by 2002:a17:906:bc8b:: with SMTP id lv11mr12926401ejb.331.1625858650050; Fri, 09 Jul 2021 12:24:10 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1625858650; cv=none; d=google.com; s=arc-20160816; b=QCFj9rmTtdqs19YIgZR6+6vElj9jXIKKVttjH0HMmYaQevyVYsb/LjFLX2pRbxVFfS 8JyS/iFpIZNg3mqufa6BZZNP0TOGRDr7+SMqL+bYPn4ZKjjzpqf7uQUsf7SFNn+TicTt 0XjelJy7YLN3s3D7OzPtW0jKQ75DZwsMTBDmWCil/S+gIFUWsQGb2dmgdTQe8gXzK8Zk QHuSybZvB9n1E24yyYSwEo9At3OZpN4g5GqjNq+A8xhDg/TjBonEb6laABnLduWDphtK iukouCRE8EEn1NIhsL55nrFWbGl8VGtogpJ9BXSWhn27dsaPxNQPuQVrK9lR84g+ODx7 h9UA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:references:to:from:subject:cc :message-id:date:content-transfer-encoding:mime-version :dkim-signature; bh=vQkGzinhkqT/iZv4cDuuh/s29v6DWdWXkPVZxeAxu60=; b=nP/82fw8QE6dTPfPlxzUNPc531UjuKsExOeE4E0K2WL712FK+tPaOTbe9MLHnH8+9h qe8JY9++UESy7veCwYbRLyUs9T2xjqHYgIZMU60CXe7y4jgjfOlMzwhr5VifWO+wormS L/lD5kW/jHSeTstsavleQb8vBIjrZgZBwpvgf/YYNmjQr041llhV40D9X1i97tEZ5z2d Z7HPWzcdVA0ZDermJXuFz8KAmH0kCYxfckQNKB53yLcJCxP2OUGTUgsFsR6zohrgZWUz DKbsjQxoO3ebdTWfkxqXWjQ40NYhH1+WbsUW86EnALY7tgRoNT4Etz1+Y+lTMiYXtRv0 AldQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=Bm0yOPvH; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id e22si6782594eds.201.2021.07.09.12.23.47; Fri, 09 Jul 2021 12:24:10 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=Bm0yOPvH; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229921AbhGITZg (ORCPT + 99 others); Fri, 9 Jul 2021 15:25:36 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42284 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229506AbhGITZg (ORCPT ); Fri, 9 Jul 2021 15:25:36 -0400 Received: from mail-qk1-x729.google.com (mail-qk1-x729.google.com [IPv6:2607:f8b0:4864:20::729]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 66230C0613DD; Fri, 9 Jul 2021 12:22:51 -0700 (PDT) Received: by mail-qk1-x729.google.com with SMTP id t19so10400280qkg.7; Fri, 09 Jul 2021 12:22:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:content-transfer-encoding:date:message-id:cc:subject :from:to:references:in-reply-to; bh=vQkGzinhkqT/iZv4cDuuh/s29v6DWdWXkPVZxeAxu60=; b=Bm0yOPvHvaihbWpTL3H1l+ncNdcvgWc4HLdCWsrd/V09y4anp3ctzRfTDNaMa5yz60 24ZGQLQVCFbYmj1Qg/jlkTrxjdAzafdpzNMro9XGXCVRNLvjKC5Qs7OOAnaR3W1SczDC +Dz4I4u9MJq2FMdkVxcTyj2s+T76og+T6HGuRHxRQWiB6OGMt65syWouXHWFUPnI6kVJ MjjSunieobs3Je53tubo1UvhKsCdxH4N3hQMBYM2NfzYja7l3UQ7nMfscgfrgT39rYQl BdXCg6mtIjfHLGStXTe+53AD8nk87apX37lrtA2xG2vm5bDP20+e1Yb2GC/ES5JGJ5LK CQQw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:content-transfer-encoding:date :message-id:cc:subject:from:to:references:in-reply-to; bh=vQkGzinhkqT/iZv4cDuuh/s29v6DWdWXkPVZxeAxu60=; b=Yy3IwEclB+MKed7mdt8v/plL6XWY3Gt1DQbt/mteQuWn6MBkZudA3CdDA8zpRexs9W DYMyVcDFCeblBDU8r1pIuJNSz4XkfJU87PK7OeZajdbRSPiSRmZPkcCYEZZr4AgzNOZq j1O0qKXGOzJW4uAqZCPp22jtkFLYjigQxwXhDO2t7qHHkmhnklnLW2eJx2N4HK13VI6o JV3EHMQ1pIWhPGziUzmEKL1SYvmBWOFMhft/nXla1cgbEKNo51F+70eqii6xNu+YyVne O+VkW2ob1Vr6vjlY/8CRfGJJNac00QkR7la2VDf1HRV0NsVHldAUPcsusv1t/0cTC9XU JhnQ== X-Gm-Message-State: AOAM5319UQOuiQZ+hlnp6R/HWrIkvCCdNmPcc22PI8aPN5X7DGoRM4li 8y8oYu72D8RW0cc47IVTpXw= X-Received: by 2002:a37:a402:: with SMTP id n2mr29946484qke.103.1625858570562; Fri, 09 Jul 2021 12:22:50 -0700 (PDT) Received: from localhost (198-48-202-89.cpe.pppoe.ca. [198.48.202.89]) by smtp.gmail.com with ESMTPSA id a20sm2905063qkg.44.2021.07.09.12.22.49 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Fri, 09 Jul 2021 12:22:49 -0700 (PDT) Mime-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=UTF-8 Date: Fri, 09 Jul 2021 15:22:48 -0400 Message-Id: Cc: , , , Subject: Re: [PATCH v4 04/10] iio: afe: rescale: reduce risk of integer overflow From: "Liam Beguin" To: "Peter Rosin" , , , References: <20210706160942.3181474-1-liambeguin@gmail.com> <20210706160942.3181474-5-liambeguin@gmail.com> <13409f37-ecd4-5afb-e1ca-59f1f1f805b8@axentia.se> In-Reply-To: <13409f37-ecd4-5afb-e1ca-59f1f1f805b8@axentia.se> Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Fri Jul 9, 2021 at 12:24 PM EDT, Peter Rosin wrote: > > On 2021-07-06 18:09, Liam Beguin wrote: > > From: Liam Beguin > >=20 > > Reduce the risk of integer overflow by doing the scale calculation with > > 64bit integers and looking for a Greatest Common Divider for both parts > > of the fractional value. > >=20 > > Signed-off-by: Liam Beguin > > --- > > drivers/iio/afe/iio-rescale.c | 12 +++++++++--- > > 1 file changed, 9 insertions(+), 3 deletions(-) > >=20 > > diff --git a/drivers/iio/afe/iio-rescale.c b/drivers/iio/afe/iio-rescal= e.c > > index 774eb3044edd..ba3bdcc69b16 100644 > > --- a/drivers/iio/afe/iio-rescale.c > > +++ b/drivers/iio/afe/iio-rescale.c > > @@ -39,7 +39,8 @@ static int rescale_read_raw(struct iio_dev *indio_dev= , > > int *val, int *val2, long mask) > > { > > struct rescale *rescale =3D iio_priv(indio_dev); > > - unsigned long long tmp; > > + s64 tmp, tmp2; > > + u32 factor; > > int ret; > > =20 > > switch (mask) { > > @@ -67,8 +68,13 @@ static int rescale_read_raw(struct iio_dev *indio_de= v, > > } > > switch (ret) { > > case IIO_VAL_FRACTIONAL: > > - *val *=3D rescale->numerator; > > - *val2 *=3D rescale->denominator; > > + tmp =3D (s64)*val * rescale->numerator; > > + tmp2 =3D (s64)*val2 * rescale->denominator; > > + factor =3D gcd(tmp, tmp2); Hi Peter, > > Hi! > > gcd() isn't exactly free. I do not think it is suitable to call it for > each > and every value. So, if you really need it, then it should only be used > when there is an actual overflow (or if there is a high risk if that's > somehow easier). Understood, digging into this a little bit, it seems like check_mul_overflow() could be used here. I'll give it a try and will look at implementing Jonathan's suggestion in case we're dealing with a case where gcd() returns 1. Thanks, Liam > > Cheers, > Peter > > > + do_div(tmp, factor); > > + *val =3D tmp; > > + do_div(tmp2, factor); > > + *val2 =3D tmp2; > > return ret; > > case IIO_VAL_INT: > > *val *=3D rescale->numerator; > >=20