Received: by 2002:a05:7412:d8a:b0:e2:908c:2ebd with SMTP id b10csp2632545rdg; Mon, 16 Oct 2023 09:57:59 -0700 (PDT) X-Google-Smtp-Source: AGHT+IH49gZ8DQtqQ6Q/PfmR9w30l32NbJmZ59FTIR0MulVI2GUcIEeDQQpS1aPlgyb2V0yqhy9y X-Received: by 2002:a05:6a20:258d:b0:161:27c5:9c41 with SMTP id k13-20020a056a20258d00b0016127c59c41mr35118648pzd.28.1697475479425; Mon, 16 Oct 2023 09:57:59 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1697475479; cv=none; d=google.com; s=arc-20160816; b=BEenBNFxDTjQR1SLdviZjGDH8w7bLj5cNoCh2T56RYDV/fSb3/nUCbFZR8kpVyypkl RuR+YCEiLUjkG1jdpYel8kXuAcyMtDbOmIUsEf9nYQyoSB9kHEHP0AWq+XoIM9Bqhx+l sqbIzuuZvSIC8n/712zeO5DUTzic4RpU7fsHYSFsgH2cUt1cg++KtPGL58+QTDjFTDDo tkM0c6n3WAItViRlpkd5Y8NMbZk31sLREaVPOQG+Qy26Yo5KXQhVr3KYvTSmzxDxknMP 5WMuLHyqUEwc2zYAxFjRY1KEKQJqYAIjxyEP2pB59ozIbPIEx2Jv5CjfRGNHe2O+rfmw ozWQ== 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:date:subject:cc:to:from :dkim-signature; bh=BbUmOV7xqUpL/nbqC49ST6a+EFAA9718QWX6/+9GZic=; fh=2kCdHXvcAULJv/9f8X/O+IrT3ddiMaJJNnCkHNJ/OJY=; b=ZmikwyMc1oUnx+tq8iyou+A2thedygvylj4GnH7nxAT4vXtUmxnUUgpy6r2iAfd9ZU jrit8/RoQ2c3iGW4pk1P8JBmBbFNEA7kZBuxgJFfK6DLZpM7/dWq6x1xk3vSY6FciRY3 RlW+OKGiL7Jm1ZrIvndCxplnlwcTCsWpBJRZ2Gx993KvP+XxLeurk8txeBB+7CdfLEJj a2yIwk7xGol87RD4PMwdfs1gLK5r4qkBqkUbiwueyetAJipxifzb2ORCpUHf5BXbSm0W xLyE3WJ4kNZVqaZdcBwqtEaFKGvivh9rcqn5PGsM4H+c7Wu/idXihXD/zoagxLu0Vbhn PNOQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=JayOvFEN; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 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 snail.vger.email (snail.vger.email. [23.128.96.37]) by mx.google.com with ESMTPS id l5-20020a170903120500b001c9abd73cf0si11447942plh.155.2023.10.16.09.57.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 16 Oct 2023 09:57:59 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) client-ip=23.128.96.37; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=JayOvFEN; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by snail.vger.email (Postfix) with ESMTP id 83C4C802A831; Mon, 16 Oct 2023 09:57:58 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at snail.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232802AbjJPQ5x (ORCPT + 99 others); Mon, 16 Oct 2023 12:57:53 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44312 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232482AbjJPQ4q (ORCPT ); Mon, 16 Oct 2023 12:56:46 -0400 Received: from mgamail.intel.com (mgamail.intel.com [192.55.52.136]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1D50F4695; Mon, 16 Oct 2023 09:54:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1697475268; x=1729011268; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=Xdc5rpo81OaAponmKGAjaymk4nUhMXFQPM0JD71hGOM=; b=JayOvFENdva29S+/0qdly6LgqBn0ar7eNjArnXD9q/KR14fVDP3oNcsS xWl8y4790Scq3ot4dzUQdnRshzAl59VH944EEdByFjZnlz7J+9W4L8qv4 Kcx5yq+3bh7ObvE/K71tnj6AXLI9mwrcVp84/HN4TwCQWnwMWjd+5Npyn +2faGE1/rwPkWKth1dISehSotzFl23HFm0M5pYtL94svuDKb9RWhIwiHK /lReYZYGwlmNRbc7tg0AHjc73BrjL703hAULLHDD9owFdq0wctHO8AmfH GY1TOkbB+e4XIFxc5BTOXGk+6IXGUTPqxS7vDbh6Vq4M8R0LpAw5dEO1i Q==; X-IronPort-AV: E=McAfee;i="6600,9927,10865"; a="364937082" X-IronPort-AV: E=Sophos;i="6.03,229,1694761200"; d="scan'208";a="364937082" Received: from fmsmga004.fm.intel.com ([10.253.24.48]) by fmsmga106.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 16 Oct 2023 09:54:27 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10865"; a="826083994" X-IronPort-AV: E=Sophos;i="6.03,229,1694761200"; d="scan'208";a="826083994" Received: from newjersey.igk.intel.com ([10.102.20.203]) by fmsmga004.fm.intel.com with ESMTP; 16 Oct 2023 09:54:24 -0700 From: Alexander Lobakin To: Yury Norov Cc: Alexander Lobakin , Andy Shevchenko , Rasmus Villemoes , Alexander Potapenko , Jakub Kicinski , Eric Dumazet , David Ahern , Przemek Kitszel , Simon Horman , netdev@vger.kernel.org, linux-btrfs@vger.kernel.org, dm-devel@redhat.com, ntfs3@lists.linux.dev, linux-s390@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v2 03/13] bitops: let the compiler optimize {__,}assign_bit() Date: Mon, 16 Oct 2023 18:52:37 +0200 Message-ID: <20231016165247.14212-4-aleksander.lobakin@intel.com> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20231016165247.14212-1-aleksander.lobakin@intel.com> References: <20231016165247.14212-1-aleksander.lobakin@intel.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF, RCVD_IN_DNSWL_BLOCKED,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 X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (snail.vger.email [0.0.0.0]); Mon, 16 Oct 2023 09:57:58 -0700 (PDT) Since commit b03fc1173c0c ("bitops: let optimize out non-atomic bitops on compile-time constants"), the compilers are able to expand inline bitmap operations to compile-time initializers when possible. However, during the round of replacement if-__set-else-__clear with __assign_bit() as per Andy's advice, bloat-o-meter showed +1024 bytes difference in object code size for one module (even one function), where the pattern: DECLARE_BITMAP(foo) = { }; // on the stack, zeroed if (a) __set_bit(const_bit_num, foo); if (b) __set_bit(another_const_bit_num, foo); ... is heavily used, although there should be no difference: the bitmap is zeroed, so the second half of __assign_bit() should be compiled-out as a no-op. I either missed the fact that __assign_bit() has bitmap pointer marked as `volatile` (as we usually do for bitops) or was hoping that the compilers would at least try to look past the `volatile` for __always_inline functions. Anyhow, due to that attribute, the compilers were always compiling the whole expression and no mentioned compile-time optimizations were working. Convert __assign_bit() to a macro since it's a very simple if-else and all of the checks are performed inside __set_bit() and __clear_bit(), thus that wrapper has to be as transparent as possible. After that change, despite it showing only -20 bytes change for vmlinux (due to that it's still relatively unpopular), no drastic code size changes happen when replacing if-set-else-clear for onstack bitmaps with __assign_bit(), meaning the compiler now expands them to the actual operations will all the expected optimizations. Atomic assign_bit() is less affected due to its nature, but let's convert it to a macro as well to keep the code consistent and not leave a place for possible suboptimal codegen. Moreover, with certain kernel configuration it actually gives some saves (x86): do_ip_setsockopt 4154 4099 -55 Suggested-by: Yury Norov # assign_bit() too Cc: Andy Shevchenko Reviewed-by: Przemek Kitszel Signed-off-by: Alexander Lobakin --- include/linux/bitops.h | 20 ++++---------------- 1 file changed, 4 insertions(+), 16 deletions(-) diff --git a/include/linux/bitops.h b/include/linux/bitops.h index e0cd09eb91cd..b25dc8742124 100644 --- a/include/linux/bitops.h +++ b/include/linux/bitops.h @@ -275,23 +275,11 @@ static inline unsigned long fns(unsigned long word, unsigned int n) * @addr: the address to start counting from * @value: the value to assign */ -static __always_inline void assign_bit(long nr, volatile unsigned long *addr, - bool value) -{ - if (value) - set_bit(nr, addr); - else - clear_bit(nr, addr); -} +#define assign_bit(nr, addr, value) \ + ((value) ? set_bit((nr), (addr)) : clear_bit((nr), (addr))) -static __always_inline void __assign_bit(long nr, volatile unsigned long *addr, - bool value) -{ - if (value) - __set_bit(nr, addr); - else - __clear_bit(nr, addr); -} +#define __assign_bit(nr, addr, value) \ + ((value) ? __set_bit((nr), (addr)) : __clear_bit((nr), (addr))) /** * __ptr_set_bit - Set bit in a pointer's value -- 2.41.0