From patchwork Sat May 20 18:25:19 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexey Dobriyan X-Patchwork-Id: 96811 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp518981vqo; Sat, 20 May 2023 12:06:35 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ5oXpFpm76oGpmtsUTbUoSUvuVCxbeZCq+aI39X02iUMGwexxBwhw+iZK4UeoxNFEouewxT X-Received: by 2002:a05:6a21:329f:b0:10b:c48:5456 with SMTP id yt31-20020a056a21329f00b0010b0c485456mr1432909pzb.0.1684609595260; Sat, 20 May 2023 12:06:35 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1684609595; cv=none; d=google.com; s=arc-20160816; b=hYyuGqaJ8dN0SxJFxJEv/gAsZ3yPGLuFzYFyB5YDbAYE8nUZ50n3rJIxfld9M5KTsI Pfp9cDJ3lHzITks3CtDjMvBgtCHbV5ef1SFk02uzemmDnRAX0ZqcxiPk/H3FRBEBmrFs hfxxewvMB/RzgQxvAt1shl6eys3t6EtMHj5ZYecMooo5of5l5z4MyYaUeOxzT5V0Ju1q dIXqVleyxBIT7LdFdALeG+Kkj840eLO6NOVqlgo+W4zYJ2CCQNa4IByGrS13nKt/M5lM /T1FI4aVPOmd3aahPxHIIhgxtWCBMWcwoKGRo5vvHIYy6cutlSICpdvv0DoPlxtS70XR +MYQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=GBfAShbL2IFJ1lSCZxPc0Karf4RuFXcjGgcsz2VTYqI=; b=QyhV4GVCbQeE4LS0g/UppsU5dxvcHMmfX4XyRTs0/U3x3gYw97cVsVA6T7tEjl46xW Pqi/IAXqB0T2wlMvL7/cSdrwhixltxm6YrJtUzGcSwHCgOCrwAcmp4ZvT6gEKALE929M j4MrId/9/uBAsOQ1aPbanl8mS258hP2d7OHkNktRNq1qm+XCfYQuqN6FtVn3xfKUQ7D1 Way1RjEMUNbOmdx2ffFMrDs1nYQhUSWfHmCuy0wnxxz/2O7b5NX0961Vznlf9OrLTxNE ds571vpn12Dxk3AeMfhTTHqEvulWBfEY+G4d7mKADaq5ugz60ZvAmPUzWy0D1p2o01Ic oK6w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20221208 header.b=eoW6hNH8; 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=QUARANTINE dis=NONE) header.from=gmail.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id q197-20020a632ace000000b0053485da2a68si1815152pgq.669.2023.05.20.12.06.14; Sat, 20 May 2023 12:06:35 -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=@gmail.com header.s=20221208 header.b=eoW6hNH8; 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=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232342AbjETS2E (ORCPT + 99 others); Sat, 20 May 2023 14:28:04 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48902 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231945AbjETS1R (ORCPT ); Sat, 20 May 2023 14:27:17 -0400 Received: from mail-wm1-x335.google.com (mail-wm1-x335.google.com [IPv6:2a00:1450:4864:20::335]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 807161BE1 for ; Sat, 20 May 2023 11:26:39 -0700 (PDT) Received: by mail-wm1-x335.google.com with SMTP id 5b1f17b1804b1-3f42769a0c1so44811055e9.2 for ; Sat, 20 May 2023 11:26:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1684607121; x=1687199121; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=GBfAShbL2IFJ1lSCZxPc0Karf4RuFXcjGgcsz2VTYqI=; b=eoW6hNH8Hog/G7dZuz4VVN4Xzi9iExHiFG7RVEeDXHwQvdZtvj0r+chEb/6bgUbhaU 0jMgaX17rfZ7FhIZerDUY8UgVnKETfjoWAbw9+04QvFrtOP9ezYeRd93lBsfS0VXeosC Rf+MNqwoSWbK6FjUffCZYevrsme3ACluQeY4OMske15vBECHVKOgnTxFVw9GlxZCv1yp 5PgmsZf5+kXFVLSIlWS5uZ2fE9iyIDh5NaRs8to/hmpBeXMPU3pV/eHTfeDj+Wo7iJ0I NSTGDbd5liPQC1q6iMOZc5aAVArL9Yz6jVSaLefRytunh6w6a8rzx+PIufKvEM045m8W iMyw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1684607121; x=1687199121; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=GBfAShbL2IFJ1lSCZxPc0Karf4RuFXcjGgcsz2VTYqI=; b=AZw0vsTqmiw+xAaOpoZIAjjrbcHKKxGaugFAaoG06eLub8mbh0C3bUZZC9SKwIYe+g T1yT6FhaR+dEJjdgqz+bV7GT8nb57GBI9uA35ZS3+4uaS99c6H+YbL2w7bVf2G+uGFpf AsOox5onymPMV1biWAO37jq7i4zYpBWm3JNunK04+wkhPPwbkKk5colhlQbAOGMHrcDT TnWDzwI7N+DdosahHxBBez5oCVWcUAqn1GEiywENGeM5z2E2NRU+Ao5KH4GhHTNYj40C mvHdrDTgJMyvoJWm3wdGTEw3pAneIqvSw7LzylyGu74FEZusfNtTbR0mR11U4r1SoJsn KJKg== X-Gm-Message-State: AC+VfDwJzP2X+x7Qq2/Min9ykLVC5X7ZJu7ONtvfSScY2HLBqoSFSMLj QZmNZEzTCq1dPyY7U98QdDTW9/7nkQ== X-Received: by 2002:adf:ee49:0:b0:306:3b78:fe33 with SMTP id w9-20020adfee49000000b003063b78fe33mr4620791wro.32.1684607121387; Sat, 20 May 2023 11:25:21 -0700 (PDT) Received: from p183 ([46.53.251.140]) by smtp.gmail.com with ESMTPSA id n1-20020adfe781000000b002c54c9bd71fsm2575661wrm.93.2023.05.20.11.25.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 20 May 2023 11:25:21 -0700 (PDT) Date: Sat, 20 May 2023 21:25:19 +0300 From: Alexey Dobriyan To: Andrew Morton Cc: linux-kernel@vger.kernel.org Subject: [PATCH v2] fix mult_frac() multiple argument evaluation bug Message-ID: References: <20230519153622.f95f74819ecf467c5291de7d@linux-foundation.org> MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: <20230519153622.f95f74819ecf467c5291de7d@linux-foundation.org> X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1766357521243372837?= X-GMAIL-MSGID: =?utf-8?q?1766441190873793649?= mult_frac() evaluates _all_ arguments multiple times in the body. Clarify comment while I'm at it. Signed-off-by: Alexey Dobriyan --- include/linux/math.h | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) --- 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_; \ +}) #define sector_div(a, b) do_div(a, b)