From patchwork Thu Feb 1 12:22:01 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexander Lobakin X-Patchwork-Id: 1893891 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=osuosl.org header.i=@osuosl.org header.a=rsa-sha256 header.s=default header.b=Hm4gjW2e; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=osuosl.org (client-ip=140.211.166.133; helo=smtp2.osuosl.org; envelope-from=intel-wired-lan-bounces@osuosl.org; receiver=patchwork.ozlabs.org) Received: from smtp2.osuosl.org (smtp2.osuosl.org [140.211.166.133]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4TQdPQ6GGSz1yQ0 for ; Thu, 1 Feb 2024 23:24:10 +1100 (AEDT) Received: from localhost (localhost [127.0.0.1]) by smtp2.osuosl.org (Postfix) with ESMTP id 1A4EF41D7C; Thu, 1 Feb 2024 12:24:09 +0000 (UTC) DKIM-Filter: OpenDKIM Filter v2.11.0 smtp2.osuosl.org 1A4EF41D7C DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=osuosl.org; s=default; t=1706790249; bh=MrmaB1Vw2GH+mJsScl7fGNJfIBLxys4GTulPMUYoW2M=; h=From:To:Date:In-Reply-To:References:Subject:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: Cc:From; b=Hm4gjW2eZ3C2rQ5B1wfnisGnv3129z+HbKB/H09gH78sMHwtO4RqmtJX0POHWr/US 5qFoa+0dljY5WmyY1vwWaoXy232wXR7QG6mqBaTC15g6fL9VxjezFsUQrm2zzevSZ7 tbmPFG7aeKCFUZmJy78TfUehuWYDwxphQBVtHp4bWclR+dh8daADJz+Tb+GQLrFnKf m11ZyFL7N1DLBMLj9mPKuoNaqhUw5cz67l4bjtzLvueg68UysotfyUE9DG38nQmTfV Wj7nMgiDUOdEuaKcQbaDOB2zfubq8HPoTvWKbYe1FIJwQZxglFriE+cnoIEOAjTC2N jt/059AcQEOng== X-Virus-Scanned: amavisd-new at osuosl.org Received: from smtp2.osuosl.org ([127.0.0.1]) by localhost (smtp2.osuosl.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id fm9eolG0wmA4; Thu, 1 Feb 2024 12:24:07 +0000 (UTC) Received: from ash.osuosl.org (ash.osuosl.org [140.211.166.34]) by smtp2.osuosl.org (Postfix) with ESMTP id 61E93422C4; Thu, 1 Feb 2024 12:24:07 +0000 (UTC) DKIM-Filter: OpenDKIM Filter v2.11.0 smtp2.osuosl.org 61E93422C4 X-Original-To: intel-wired-lan@lists.osuosl.org Delivered-To: intel-wired-lan@lists.osuosl.org Received: from smtp1.osuosl.org (smtp1.osuosl.org [140.211.166.138]) by ash.osuosl.org (Postfix) with ESMTP id D068E1BF98D for ; Thu, 1 Feb 2024 12:24:03 +0000 (UTC) Received: from localhost (localhost [127.0.0.1]) by smtp1.osuosl.org (Postfix) with ESMTP id B72C183D5E for ; Thu, 1 Feb 2024 12:24:03 +0000 (UTC) DKIM-Filter: OpenDKIM Filter v2.11.0 smtp1.osuosl.org B72C183D5E X-Virus-Scanned: amavisd-new at osuosl.org Received: from smtp1.osuosl.org ([127.0.0.1]) by localhost (smtp1.osuosl.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id ZCKYPKb3qA45 for ; Thu, 1 Feb 2024 12:24:03 +0000 (UTC) Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.15]) by smtp1.osuosl.org (Postfix) with ESMTPS id DDAFE81951 for ; Thu, 1 Feb 2024 12:24:02 +0000 (UTC) DKIM-Filter: OpenDKIM Filter v2.11.0 smtp1.osuosl.org DDAFE81951 X-IronPort-AV: E=McAfee;i="6600,9927,10969"; a="3746991" X-IronPort-AV: E=Sophos;i="6.05,234,1701158400"; d="scan'208";a="3746991" Received: from orviesa004.jf.intel.com ([10.64.159.144]) by orvoesa107.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 01 Feb 2024 04:24:03 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.05,234,1701158400"; d="scan'208";a="4499104" Received: from newjersey.igk.intel.com ([10.102.20.203]) by orviesa004.jf.intel.com with ESMTP; 01 Feb 2024 04:23:57 -0800 From: Alexander Lobakin To: "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni Date: Thu, 1 Feb 2024 13:22:01 +0100 Message-ID: <20240201122216.2634007-7-aleksander.lobakin@intel.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240201122216.2634007-1-aleksander.lobakin@intel.com> References: <20240201122216.2634007-1-aleksander.lobakin@intel.com> MIME-Version: 1.0 X-Mailman-Original-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1706790243; x=1738326243; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=SoA/u9+ab8IWfX0PrdnuNf/ZyTjLUt7GJssFy85j9T4=; b=l1UCv6MO0PiwpAdzp5/SorOgiePRZVHc61Wg/ken5IRWJVuTc4rmrg5B TLkOMHh6o34ya9yNoAYeGZmTI3XRwJ+wxAwjKyQm3qAw89T/OigaGOFE/ TjXt0o3f/qKYzVfHNpHrtq2ypvnslsBCgR1Gb9QBwbCbarUWpZMSOl7dJ bvWBfDmXUVx2xLCxeXbeeWH8tS+dcfv2zR9L7zu8PmrVmyTngJisUjOY9 MrzHtTvv4USb8MLkQZE1tnidQZtyog4UPH8s9zTd/BXX2yS8gZXTgVaaD vh1fHJR8VUnaQGp+tz94IJYLgKOkKcxSmzkUstuwwbSHPUxDL1VIe874G g==; X-Mailman-Original-Authentication-Results: smtp1.osuosl.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.a=rsa-sha256 header.s=Intel header.b=l1UCv6MO Subject: [Intel-wired-lan] [PATCH net-next v5 06/21] bitops: let the compiler optimize {__, }assign_bit() X-BeenThere: intel-wired-lan@osuosl.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Intel Wired Ethernet Linux Kernel Driver Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Andy Shevchenko , linux-s390@vger.kernel.org, Andy Shevchenko , Wojciech Drewek , Yury Norov , Ido Schimmel , Rasmus Villemoes , linux-kernel@vger.kernel.org, Alexander Lobakin , dm-devel@redhat.com, Marcin Szycik , Alexander Potapenko , Simon Horman , Przemek Kitszel , Michal Swiatkowski , netdev@vger.kernel.org, ntfs3@lists.linux.dev, Jiri Pirko , linux-btrfs@vger.kernel.org, intel-wired-lan@lists.osuosl.org Errors-To: intel-wired-lan-bounces@osuosl.org Sender: "Intel-wired-lan" 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 Acked-by: Yury Norov --- 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