Received: by 2002:ab2:6991:0:b0:1f2:fff1:ace7 with SMTP id v17csp124397lqo; Wed, 27 Mar 2024 08:32:46 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCVngoXdNZb14UYX3ClGIimLS0WeCWqfhlDroCWC8hO9eryNbefuSWyN/qYRFKElXfQtB11WtrSFDr+0xueZTIDKUBSGvJCWVfA4AW/QxA== X-Google-Smtp-Source: AGHT+IGd9Nxl864fVmZKKsFGTjVmr6jy4vFgAsH2oSAg7RhBK45KlRxWoxBFRyoF59/xP2gyU3KL X-Received: by 2002:a9d:4b0f:0:b0:6e6:8c4a:1aad with SMTP id q15-20020a9d4b0f000000b006e68c4a1aadmr312961otf.8.1711553566023; Wed, 27 Mar 2024 08:32:46 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1711553565; cv=pass; d=google.com; s=arc-20160816; b=cPHx7WkUdtqs5e/KexjynrMiozQmZqIeGQ+lOCFnQ1EuCnv3yWeY+M1xj0Pnuu5gqv IsUobiiTnY7axQHIF3UE7DkPj79pbT65JAm804gQ3zhOYMPaq3sAX3USUa7Gb7Atu6kq VArqogHOKMAG3487bIhntRXDVC6+45GgaxZDAf3cAgi9xi2dgvsKWTJHEKs11LqamzxJ S+5Jj9JzeJBTeajdCQrQn9ju6jdIShpjVWwMcolFaA3NeAx0ZqM/h/wKGkW74rzdRieK qON+ca11d+iZn4wr6cb2CLjVkrtOCU2gW7QNpHFyhaKdz7oeHJbpDLpl49jwhVUGYRcA JxAg== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=WFFFpYUy8YH1KZ1/CKj0274wR2f3ZdEdr4/xWK9GmzM=; fh=3EtiAqn1jgEE+m5MDm3JLOF5m5gJaoH7dDDA4VZkK+Q=; b=KhcnofUCyNtk4fjtWOqCFPnRifQqWkfMURTS1vTwP9oSZHWbvH7p/FQJj4aALabFYR P8czXhqdBOk0rWXeSUuiLWxgLNCoRFBbzAmE5As3QAup1OSk8ZNhHjNgMSeYxtliwQMR N6OXPMiLevpB48/yUQ46owXQzM76U2jdWYIjf8T3rB0Su4nIfalW114f3JySOlL++sUz IYUerlp/XkXGM9cH4oZL/TlqGuQ29APAn4VJNw8FhXAcO3mmUYa3cETgmU4raVW5e1pe rZgw0r7wIgdhsK2XDqZvXT5sE+dfGiICN/XcFgnmh4OUleHJXNinRKDj4Hc+nuIVJ6uC Cbtw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=PWhuNDWf; arc=pass (i=1 spf=pass spfdomain=intel.com dkim=pass dkdomain=intel.com dmarc=pass fromdomain=intel.com); spf=pass (google.com: domain of linux-kernel+bounces-121516-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-121516-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [147.75.199.223]) by mx.google.com with ESMTPS id f17-20020a05620a20d100b0078a45417393si3207103qka.563.2024.03.27.08.32.45 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 27 Mar 2024 08:32:45 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-121516-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) client-ip=147.75.199.223; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=PWhuNDWf; arc=pass (i=1 spf=pass spfdomain=intel.com dkim=pass dkdomain=intel.com dmarc=pass fromdomain=intel.com); spf=pass (google.com: domain of linux-kernel+bounces-121516-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-121516-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id B925E1C2FCBC for ; Wed, 27 Mar 2024 15:32:45 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 5D977146015; Wed, 27 Mar 2024 15:24:57 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="PWhuNDWf" Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.19]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 7DBF013FD67; Wed, 27 Mar 2024 15:24:54 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.175.65.19 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711553096; cv=none; b=pZvJfHm04HYkbkHF8Ed6NNA75zq2hj9jrE+2cogDBDqHYA/iZxcoxEMXat4PSU9eDIhJIoYjdP5PjT+FDegeV0wN8MJ/GZYvAT/wnmgxiR3AUN2jie/i6bpQjOzU3rIghl6EjkbtgDIhzNaiNBI/EqDcXMsmFW4hu5sqG8Xot1I= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711553096; c=relaxed/simple; bh=AjC3bYJ69IG/k83edT1SGLXaLYwzTvFtmxenQQc9rVU=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=tx4jEZzsN4Xb8huIqKowFypiPTTSVNy4LGL/jC7w3NJK0+oAIrugj8i1/irPLAFdDZIZQq0wtKO0Tj7N0xiNofbFoguLMXRUCVF9u3a+MSd/Qjy8kUe4K8tD3AEypZGtXvAaZSflr9mAnG2icUX4Gav/ola2wIGNBzWD9ZQfKks= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com; spf=pass smtp.mailfrom=intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=PWhuNDWf; arc=none smtp.client-ip=198.175.65.19 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=intel.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1711553094; x=1743089094; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=AjC3bYJ69IG/k83edT1SGLXaLYwzTvFtmxenQQc9rVU=; b=PWhuNDWfJctHg+ANIIrkXbCaCJff3mGPHO/9wCH5aboX+w3OtklPrP1T /03ccU4dmJFvuNUolLW+HC2M+ut6flTLcbrWiad352jVHa/a+pX6oPpwl e6iRsnwVDKc0z5zBUlZXnFkcWFKRTU+s3RbvdWEofNEALpgKLcK3vT50C rn7G36yrOfAK6OxC24/uBe9dOk7eOFvVNF+dO8f4Y7DwZhhfvPWmdctvK HwmIYYtmg4nDKFq6Ppz976JqqJaIhYuZ6k4DOPUD2gKgTHvRzYPAZ5k1Z lmZnJZ61M2ShSbHBWLvhicJIiStjZVZ/F7IwJOWldiYVnejXkNLWrOvMb g==; X-CSE-ConnectionGUID: 3s6xO5XfQ/a0ihmEejz2Vg== X-CSE-MsgGUID: +BhSoLzkScGCV6VcjMvCrQ== X-IronPort-AV: E=McAfee;i="6600,9927,11026"; a="6518143" X-IronPort-AV: E=Sophos;i="6.07,159,1708416000"; d="scan'208";a="6518143" Received: from fmviesa008.fm.intel.com ([10.60.135.148]) by orvoesa111.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 27 Mar 2024 08:24:54 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.07,159,1708416000"; d="scan'208";a="16414168" Received: from newjersey.igk.intel.com ([10.102.20.203]) by fmviesa008.fm.intel.com with ESMTP; 27 Mar 2024 08:24:51 -0700 From: Alexander Lobakin To: "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni Cc: Alexander Lobakin , Yury Norov , Alexander Potapenko , nex.sw.ncis.osdt.itp.upstreaming@intel.com, intel-wired-lan@lists.osuosl.org, netdev@vger.kernel.org, linux-kernel@vger.kernel.org, Andy Shevchenko , Przemek Kitszel Subject: [PATCH net-next v6 06/21] bitops: let the compiler optimize {__,}assign_bit() Date: Wed, 27 Mar 2024 16:23:43 +0100 Message-ID: <20240327152358.2368467-7-aleksander.lobakin@intel.com> X-Mailer: git-send-email 2.44.0 In-Reply-To: <20240327152358.2368467-1-aleksander.lobakin@intel.com> References: <20240327152358.2368467-1-aleksander.lobakin@intel.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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 Acked-by: Yury Norov 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.44.0