Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp85278rwd; Fri, 19 May 2023 15:54:20 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ4IC1MDwZlLWdOBb1q5qi5+TlXjvtf03mrU3GrSKkePyNTtBea0v/1rMgZPnrd2qNSCy6IS X-Received: by 2002:a17:90a:2c4d:b0:253:2ee7:bbd with SMTP id p13-20020a17090a2c4d00b002532ee70bbdmr3329359pjm.11.1684536859900; Fri, 19 May 2023 15:54:19 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1684536859; cv=none; d=google.com; s=arc-20160816; b=tctAar7xCAnCC1iOjAsoMNglK34uAUgU0ngzQ3Nv2MLuUSEdEOop7SSob023ardMPm MMj1vGLZVmAgwnuFOmOmyZsvWDpCasiTj/LhvViCme6XEtjbBba/z8F7Lq4d83v6nTXT ISHz+YFXrBjbL34FOWcZTm/jxgVFYRMweZnX4pAL4zDzoXa+NI63JKetVOt3dWGEvJz7 ocpDGeNEaWhpchzdojdBJMY9QsfugZEMz1CUDk90IT7cuX4awnveI/eA6QM0boabWJX/ 0ZpmV14egAsJwuhusO2XQBiD7kJJhJQe82CCY5vAdLnw+vuwW609EUr5Tzg3bWezrafQ IKBQ== 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 :references:in-reply-to:message-id:subject:cc:to:from:date :dkim-signature; bh=+9p4haR/SbT1xWOo1ue8M1lG/4f0Ks3vJQNyTDcdTuY=; b=0YqqWaMzQktasapXtQXXeDf3FRkM9wJWN3plWxL4v8Kx68SPe4dKtPHW7yCf7BsKjX obv4RFKo6pcHwtDs2zg8DDRPxi9XLiaDjXfs5jvpfxG52cBMFJ1McG9SJ1lW8qSVBdma FKnTznBoqCbN6Tgir+ReQTkxiMmNLKFooA/cVPZT7EIFjbjqyj/ltoOJsiGsbkuDqJsN 3PGUBTp20ey9w6egC9mwki/C2CHJF56HyTuiBIW7+xPHnaW/UkuGaGGvhT/u+q/HxSl4 F3ybrtqAHaUldlfQI4rtReiM7gYesaW9Y82H560VI/O5kMgGcKjl2YABPrNQa4w/R4f9 IlGQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linux-foundation.org header.s=korg header.b=rHEm03DX; 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 Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id x92-20020a17090a38e500b00250a2e130ecsi2296016pjb.87.2023.05.19.15.53.58; Fri, 19 May 2023 15:54:19 -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=@linux-foundation.org header.s=korg header.b=rHEm03DX; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231500AbjESWg0 (ORCPT + 99 others); Fri, 19 May 2023 18:36:26 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53340 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229557AbjESWgZ (ORCPT ); Fri, 19 May 2023 18:36:25 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9D4171BD for ; Fri, 19 May 2023 15:36:24 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 37E7765AF3 for ; Fri, 19 May 2023 22:36:24 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 85AFEC433EF; Fri, 19 May 2023 22:36:23 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linux-foundation.org; s=korg; t=1684535783; bh=13ItZfCP8MHViSXswO3xyLCnVZA5On30GzWwaZ2N7Y4=; h=Date:From:To:Cc:Subject:In-Reply-To:References:From; b=rHEm03DXcJdL7PktvKkr2JfOCBjU3KdqptRFDz3saO3dKHMW53SKZdJUFnJEhzy0Y 4Icf2Js5DKZNX+Nr/1l3WnUK/7Dq7A6yOoh/Rfek6C9R6ZkBE1Tu8Y6d7W7N1dTlEC /+HKe3eQZZOyveL95eU7ddqBmpyJMtB/REwQ/CCg= Date: Fri, 19 May 2023 15:36:22 -0700 From: Andrew Morton To: Alexey Dobriyan Cc: linux-kernel@vger.kernel.org Subject: Re: [PATCH] fix mult_frac() multiple argument evaluation bug Message-Id: <20230519153622.f95f74819ecf467c5291de7d@linux-foundation.org> In-Reply-To: References: X-Mailer: Sylpheed 3.8.0beta1 (GTK+ 2.24.33; x86_64-pc-linux-gnu) Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-8.6 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,NICE_REPLY_A,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE,URIBL_BLOCKED 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 Fri, 19 May 2023 23:24:54 +0300 Alexey Dobriyan wrote: > mult_frac() evaluates _all_ arguments multiple times in the body. > > --- a/include/linux/math.h > +++ b/include/linux/math.h > @@ -118,17 +118,17 @@ __STRUCT_FRACT(s32) > __STRUCT_FRACT(u32) > #undef __STRUCT_FRACT > > -/* > - * Multiplies an integer by a fraction, while avoiding unnecessary > - * overflow or loss of precision. > - */ > -#define mult_frac(x, numer, denom)( \ > -{ \ > - typeof(x) quot = (x) / (denom); \ > - typeof(x) rem = (x) % (denom); \ > - (quot * (numer)) + ((rem * (numer)) / (denom)); \ > -} \ > -) > +/* Calculate "x * n / d" without unnecessary overflow or loss of precision. */ > +#define mult_frac(x, n, d) \ > +({ \ > + typeof(x) x_ = (x); \ > + typeof(n) n_ = (n); \ > + typeof(d) d_ = (d); \ > + \ > + typeof(x) q = x_ / d_; \ > + typeof(x) r = x_ % d_; \ > + q * n_ + r * n_ / d_; \ > +}) > I like, but the compiler doesn't. x86_64 allmodconfig, gcc-12.2.0. In file included from ./include/linux/math64.h:6, from ./include/linux/time.h:6, from ./include/linux/ktime.h:24, from ./include/linux/timer.h:6, from drivers/target/target_core_iblock.c:16: drivers/target/target_core_iblock.c: In function 'iblock_configure_device': drivers/target/target_core_iblock.c:127:73: error: passing argument 1 of 'queue_max_hw_sectors' makes pointer from integer without a cast [-Werror=int-conversion] 127 | dev->dev_attrib.hw_max_sectors = mult_frac(queue_max_hw_sectors(q), | ^ | | | unsigned int ./include/linux/math.h:129:16: note: in definition of macro 'mult_frac' 129 | typeof(x) r = x_ % d_; \ | ^ In file included from drivers/target/target_core_iblock.c:18: ./include/linux/blkdev.h:1112:77: note: expected 'const struct request_queue *' but argument is of type 'unsigned int' 1112 | static inline unsigned int queue_max_hw_sectors(const struct request_queue *q) | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~^ cc1: all warnings being treated as errors make[3]: *** [scripts/Makefile.build:252: drivers/target/target_core_iblock.o] Error 1 make[3]: *** Waiting for unfinished jobs.... make[2]: *** [scripts/Makefile.build:494: drivers/target] Error 2 make[2]: *** Waiting for unfinished jobs.... make[1]: *** [scripts/Makefile.build:494: drivers] Error 2 make: *** [Makefile:2026: .] Error 2