From patchwork Tue Nov 12 19:08:39 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Vincent Mailhol X-Patchwork-Id: 13872844 Received: from smtp.smtpout.orange.fr (smtp-14.smtpout.orange.fr [80.12.242.14]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 0C271218316 for ; Tue, 12 Nov 2024 19:09:06 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=80.12.242.14 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731438549; cv=none; b=avqE6ItG8a7so+WCRPg1sz/CZGGw3TlRxHynaTiJin5C33RhEQ/XZM/Z/fAZPmUa9db8ou4zmEibPxcZ9ZGfeMdGIrxyCZz3h33rw43ted/ivRHWDvX9ljTkaxGL7a+pE53mnh6B1va2sBJ7JH/OdintVVYSPT0CycW/bo8xZ98= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731438549; c=relaxed/simple; bh=17mtmBY8ZwUCeRi/t4IdmbeGrefNGpC3DWHRJW4wuu4=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=EYNN+qi4ko4anSFvGt21+vsBS4pQDjzPHA5X0XtmejF9oUdNOD0yBAilkYbMvWupPzLkjrdbhZxREdFlk6AxMXA5OWVWQxhmw99OVViEJr/9JA6ZnGofEcBuVes2UIsrOwz7iJPpwEp2jZUMFbYnJvFlZ6FwOTB4nFp6cz9tKKs= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=wanadoo.fr; spf=pass smtp.mailfrom=wanadoo.fr; dkim=pass (2048-bit key) header.d=wanadoo.fr header.i=@wanadoo.fr header.b=Ghl1rS2I; arc=none smtp.client-ip=80.12.242.14 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=wanadoo.fr Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=wanadoo.fr Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=wanadoo.fr header.i=@wanadoo.fr header.b="Ghl1rS2I" Received: from localhost.localdomain ([106.146.16.70]) by smtp.orange.fr with ESMTPA id AwFntjwiq18HRAwG3t29pO; Tue, 12 Nov 2024 20:09:05 +0100 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=wanadoo.fr; s=t20230301; t=1731438545; bh=P1AYhd2AajanOBGPm1PpNJJsdw+dSp6kxGNZyhNotu4=; h=From:To:Subject:Date:Message-ID:MIME-Version; b=Ghl1rS2I4TfK8LdPDV4uADyga+Sw7s6CC8y7qXWUBuXCAjCX5eIMQAn1pWT66DxSe QN3bidA+V8ryVF6RtnDm+kLl5Z3o6DOeZSk7QQaopKYtbaa+ssl2y2SwIANK8RaePC VsKSZcJMMvt+b+GCvYibwlHxNSmXK9co6K37Ob9plKaoLWALQcNL1//MbK6mxxbzpS NAc8U8vCjNZYDspouWdz1rfFGPV+mUz0CTr6407O+D7IOVRXEdg+d0Am0dqiuZCyBi ZxFltZWm9mY+XalXCX56dBraFBxDoeewSMQ0r4kmg2igSb+QZRvARBZ+C+ple8+UWF CMQQwKC3UMNeg== X-ME-Helo: localhost.localdomain X-ME-Auth: bWFpbGhvbC52aW5jZW50QHdhbmFkb28uZnI= X-ME-Date: Tue, 12 Nov 2024 20:09:05 +0100 X-ME-IP: 106.146.16.70 From: Vincent Mailhol To: Linus Torvalds , Yury Norov , Rasmus Villemoes , Luc Van Oostenryck Cc: linux-kernel@vger.kernel.org, linux-sparse@vger.kernel.org, Rikard Falkeborn , Vincent Mailhol Subject: [RESEND PATCH v3 1/2] compiler.h: add _static_assert() Date: Wed, 13 Nov 2024 04:08:39 +0900 Message-ID: <20241112190840.601378-5-mailhol.vincent@wanadoo.fr> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20241112190840.601378-4-mailhol.vincent@wanadoo.fr> References: <20241112190840.601378-4-mailhol.vincent@wanadoo.fr> Precedence: bulk X-Mailing-List: linux-sparse@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=3465; i=mailhol.vincent@wanadoo.fr; h=from:subject; bh=17mtmBY8ZwUCeRi/t4IdmbeGrefNGpC3DWHRJW4wuu4=; b=owGbwMvMwCV2McXO4Xp97WbG02pJDOnGy3dEtyo9z0/7PSPAKvVr8gfLeYlBmb531jw89kJG8 nyQLP/PjlIWBjEuBlkxRZZl5ZzcCh2F3mGH/lrCzGFlAhnCwMUpABM5uJyRYb7S5Jc/ao1lpAyd jt0Tz220dn0kv6Lb5ZQA5x7PU5HNFgz/q5RVHf5LlD1bZSb9ZHPzs03ss8zbPp09mfReoiHK/LY UNwA= X-Developer-Key: i=mailhol.vincent@wanadoo.fr; a=openpgp; fpr=ED8F700574E67F20E574E8E2AB5FEB886DBB99C2 __builtin_constant_p() is known for not always being able to produce constant expression [1] which lead to the introduction of __is_constexpr() [2]. Because of its dependency on __builtin_constant_p(), statically_true() suffers from the same issues. For example: void foo(int a) { /* fail on GCC */ BUILD_BUG_ON_ZERO(statically_true(a)); /* fail both clang and GCC */ static char arr[statically_true(a) ? 1 : 2]; } For the same reasons why __is_constexpr() was created to cover __builtin_constant_p() edge cases, __is_constexpr() can be used to resolve statically_true() limitations. Note that, somehow, GCC is not always able to fold this: __is_constexpr(x) && (x) It is OK in BUILD_BUG_ON_ZERO() but not in array declarations or in static_assert(): void bar(int a) { /* success */ BUILD_BUG_ON_ZERO(__is_constexpr(a) && (a)); /* fail on GCC */ static char arr[__is_constexpr(a) && (a) ? 1 : 2]; /* fail on GCC */ static_assert(__is_constexpr(a) && (a)); } Encapsulating the expression in a __builtin_choose_expr() switch resolves all these failed test. Declare a new _statically_true() macro which, by making use of the __builtin_choose_expr() and __is_constexpr(x) combo, always produces a constant expression. It should be noted that statically_true() still produces better folding: statically_true(!(var * 8 % 8)) always evaluates to true even if var is unknown, whereas _statically_true(!(var * 8 % 8)) fails to fold the expression and return false. For this reason, usage of _statically_true() be should the exception. Reflect in the documentation that _statically_true() is less powerful and that statically_true() is the overall preferred solution. [1] __builtin_constant_p cannot resolve to const when optimizing Link: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=19449 [2] commit 3c8ba0d61d04 ("kernel.h: Retain constant expression output for max()/min()") Signed-off-by: Vincent Mailhol --- Bonuses: - above examples, and a bit more: https://godbolt.org/z/zzqM1ajPj - a proof that statically_true() does better constant folding than _statically_true() https://godbolt.org/z/vK6KK4hMG --- include/linux/compiler.h | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/include/linux/compiler.h b/include/linux/compiler.h index 4d4e23b6e3e7..c76db8b50202 100644 --- a/include/linux/compiler.h +++ b/include/linux/compiler.h @@ -308,6 +308,20 @@ static inline void *offset_to_ptr(const int *off) */ #define statically_true(x) (__builtin_constant_p(x) && (x)) +/* + * Similar to statically_true() but produces a constant expression + * + * To be used in conjunction with macros, such as BUILD_BUG_ON_ZERO(), + * which require their input to be a constant expression and for which + * statically_true() would otherwise fail. + * + * This is a tradeoff: _statically_true() is less efficient at + * constant folding and will fail to optimize any expressions in which + * at least one of the subcomponent is not constant. For the general + * case, statically_true() is better. + */ +#define _statically_true(x) __builtin_choose_expr(__is_constexpr(x), x, false) + /* * This is needed in functions which generate the stack canary, see * arch/x86/kernel/smpboot.c::start_secondary() for an example. From patchwork Tue Nov 12 19:08:40 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Vincent Mailhol X-Patchwork-Id: 13872846 Received: from smtp.smtpout.orange.fr (smtp-14.smtpout.orange.fr [80.12.242.14]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id AA289218946 for ; Tue, 12 Nov 2024 19:09:10 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=80.12.242.14 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731438552; cv=none; b=ite2w9OZdnnJAtsmkShmpw6e2c5IueRmGGWlnQXKPbFKf4woSvpc9S/pbNFhxAKbX0MlKVOliN//+g2DgpAntnLJXA0X/zywiOoZ4glJqLK5jV1mXa/JVc7zQxth1l9dutm/oA/D87c/+pyti9y68pzAs1jUyA9Trkj5Xn4xwJM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731438552; c=relaxed/simple; bh=1ytyDmqmYGEfJHwfaXioLVwFYj+zkAhZNDBlxyKs8Dw=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=RppY/AVfRcC95wSFQtH7OIAy6cve8dyE6dmJ6KpbR4YQQUdoMu84kAMIvvCQ4btLD4tAagdj3+LVCI7k4e602Qzk4iWNFOQRCdsEjEVZuKh5V2sOid36UE6b3BSgkEOT3m+HCLE3jkv8/Icbbr1aqMz0swTQAbMiyAWIPcQXzmQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=wanadoo.fr; spf=pass smtp.mailfrom=wanadoo.fr; dkim=pass (2048-bit key) header.d=wanadoo.fr header.i=@wanadoo.fr header.b=TinuTRjl; arc=none smtp.client-ip=80.12.242.14 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=wanadoo.fr Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=wanadoo.fr Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=wanadoo.fr header.i=@wanadoo.fr header.b="TinuTRjl" Received: from localhost.localdomain ([106.146.16.70]) by smtp.orange.fr with ESMTPA id AwFntjwiq18HRAwG9t29qp; Tue, 12 Nov 2024 20:09:10 +0100 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=wanadoo.fr; s=t20230301; t=1731438550; bh=0NHpSD6qsurxbYxMcXGV0ZZS/bVySspTWKfjYRHZuDY=; h=From:To:Subject:Date:Message-ID:MIME-Version; b=TinuTRjlMaUiCOOU+hZbqTOjC9r46Zs0iNGlHeQ0iPhUMzkaFmzJECSReUcoWBQ7D rN7WKUqk9KTJJqysH5xzLVW6Gdv2LTnvUmX2fIyLdMb0kTcwYGUAbVz3bSex7WD1gY +Faf8J7j74+2J/sTMRcB/PGML2dNuziF/UhVNWrA2LvKMjgTRAqZskRNuDu0/HOOon 0zfewxJhvvZZuch9zCk9y1ddSGohbAtkrr8Sm/UN7487fqE80v+kz/x9EStv9SGnjj kJeeCeDMK0utEKEzRJaqi7maWuZY3FJKAfBg1XJtBDtURACOrbiwXEmX0ari39WyDZ W62l8GarnqDoQ== X-ME-Helo: localhost.localdomain X-ME-Auth: bWFpbGhvbC52aW5jZW50QHdhbmFkb28uZnI= X-ME-Date: Tue, 12 Nov 2024 20:09:10 +0100 X-ME-IP: 106.146.16.70 From: Vincent Mailhol To: Linus Torvalds , Yury Norov , Rasmus Villemoes , Luc Van Oostenryck Cc: linux-kernel@vger.kernel.org, linux-sparse@vger.kernel.org, Rikard Falkeborn , Vincent Mailhol Subject: [RESEND PATCH v3 2/2] linux/bits.h: simplify GENMASK_INPUT_CHECK() Date: Wed, 13 Nov 2024 04:08:40 +0900 Message-ID: <20241112190840.601378-6-mailhol.vincent@wanadoo.fr> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20241112190840.601378-4-mailhol.vincent@wanadoo.fr> References: <20241112190840.601378-4-mailhol.vincent@wanadoo.fr> Precedence: bulk X-Mailing-List: linux-sparse@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=2142; i=mailhol.vincent@wanadoo.fr; h=from:subject; bh=1ytyDmqmYGEfJHwfaXioLVwFYj+zkAhZNDBlxyKs8Dw=; b=owGbwMvMwCV2McXO4Xp97WbG02pJDOnGy3cca1jFf27D1c3L7ldtbf9yo3qefAXz1RWHUmL/l 975mjTVqqOUhUGMi0FWTJFlWTknt0JHoXfYob+WMHNYmUCGMHBxCsBElP4z/DO62VISH1IZ9dnq +a0kxlkX3JZ+dlvieEyuJ5+J7ZBO9X6G/2V561Q/vVj16sYC1Tyxig7pwBTLll26euuXRRW+VNr 5gwkA X-Developer-Key: i=mailhol.vincent@wanadoo.fr; a=openpgp; fpr=ED8F700574E67F20E574E8E2AB5FEB886DBB99C2 In GENMASK_INPUT_CHECK(), __builtin_choose_expr(__is_constexpr((l) > (h)), (l) > (h), 0) is the exact expansion of: _statically_true((l) > (h)) Apply _statically_true() to simplify GENMASK_INPUT_CHECK(). Signed-off-by: Vincent Mailhol --- This change passes the unit tests from CONFIG_BITS_TEST, including the extra negative tests provided under #ifdef TEST_GENMASK_FAILURES [1]. [1] commit 6d511020e13d ("lib/test_bits.c: add tests of GENMASK") Link: https://git.kernel.org/torvalds/c/6d511020e13d ** Changelog ** v2 -> v3: - split the single patch into a series of two patches. - add greater explanation of why _statically_true() is needed in addition of the existing statically_true(). Explain the pros and cons of both. - use __builtin_choose_expr() in _statically_true(). The _statically_true() of the v2 works perfectly fine when used in conjunction with BUILD_BUG_ON_ZERO() but fails if used in arrays or in static_assert() - update the patch description accordingly Link: https://lore.kernel.org/all/20241111164743.339117-2-mailhol.vincent@wanadoo.fr/ v1 -> v2: - introduce _statically_true(), taking inspiration from statically_true() as introduced in commit 22f546873149 ("minmax: improve macro expansion and type checking"). Link: https://lore.kernel.org/all/20240609073513.256179-1-mailhol.vincent@wanadoo.fr/ --- include/linux/bits.h | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/include/linux/bits.h b/include/linux/bits.h index 60044b608817..01713e1eda56 100644 --- a/include/linux/bits.h +++ b/include/linux/bits.h @@ -20,9 +20,8 @@ */ #if !defined(__ASSEMBLY__) #include -#define GENMASK_INPUT_CHECK(h, l) \ - (BUILD_BUG_ON_ZERO(__builtin_choose_expr( \ - __is_constexpr((l) > (h)), (l) > (h), 0))) +#include +#define GENMASK_INPUT_CHECK(h, l) BUILD_BUG_ON_ZERO(_statically_true((l) > (h))) #else /* * BUILD_BUG_ON_ZERO is not available in h files included from asm files,