From patchwork Mon Dec 2 17:33:23 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Vincent Mailhol via B4 Relay X-Patchwork-Id: 13891137 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 4C6FE1D90B3; Mon, 2 Dec 2024 17:35:04 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733160904; cv=none; b=hnyINPmjWfycBzQGJe8VPx1ae7P7YpRPojJr3ELsEa2fiGHrrXCcL5yJJdMb9gyvcEVHz13embUNO3QW5YMd/5jBQtNYPHV+C1bvVe9of/Np1JAAp8Qp8oI5UBgjAKc2lPvVNxD5RFFyjmJJIhvt4cbTe+5zOyS8JjZByVNDt/g= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733160904; c=relaxed/simple; bh=5SkdVpyxMkwcefHUvrmeRmooVspzWLLjyCNkLAyYF/4=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=JlYSDkUvLpeILBF+771Th+HASIVKqkavtmXhk0lskC5/c7TAzlz1H41IWjBOoiuTIqP8hkcC8tXAcm961M0aC8P7aCxR0wCvXmOO0hj5i0hEDparm7DRjnEpdRKEj6paLzyIlSMzIlNxFBrIqut93Hk+fP8gLvgrTwdOFd+sNzE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=krYcOchp; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="krYcOchp" Received: by smtp.kernel.org (Postfix) with ESMTPS id EB054C4CED6; Mon, 2 Dec 2024 17:35:03 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1733160904; bh=5SkdVpyxMkwcefHUvrmeRmooVspzWLLjyCNkLAyYF/4=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To:From; b=krYcOchpdZfTOGmmZmYXGkkzng0VBYzBaTIPS/JdWNiJdmt/4S7+V2deZeuJtDiHJ 5bOfjWx9PMCJkw4vWMJA+j7RGJgyQT3MCvZz+ywWGgXkUPQ1I8zhp2RRNDd7wmPgUC KVr3TdRx7r3vS7sNZQQ4VuZSeMJ1Co4MyFc7SbZRWDfa4cIk4lQ19T0FRsrpwOa17T JopKvCO/OnNKT3V7n07PfE9Ll3My7kWEoWbthp4JMgarfO7M35ovTZ+SUQIDeAkdh8 VxQs0KDQFLoqYMNE9XZ9vIlYg+Eia5POewyoXqK+ltXanlAIbCfNk8+zvx51tcJKic OeLS9BafEnm7Q== Received: from aws-us-west-2-korg-lkml-1.web.codeaurora.org (localhost.localdomain [127.0.0.1]) by smtp.lore.kernel.org (Postfix) with ESMTP id D2CC9D7833C; Mon, 2 Dec 2024 17:35:03 +0000 (UTC) From: Vincent Mailhol via B4 Relay Date: Tue, 03 Dec 2024 02:33:23 +0900 Subject: [PATCH 01/10] compiler.h: add statically_false() Precedence: bulk X-Mailing-List: linux-hardening@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20241203-is_constexpr-refactor-v1-1-4e4cbaecc216@wanadoo.fr> References: <20241203-is_constexpr-refactor-v1-0-4e4cbaecc216@wanadoo.fr> In-Reply-To: <20241203-is_constexpr-refactor-v1-0-4e4cbaecc216@wanadoo.fr> To: Linus Torvalds , David Laight , Luc Van Oostenryck , Nathan Chancellor , Nick Desaulniers , Bill Wendling , Justin Stitt , Yury Norov , Rasmus Villemoes , Kees Cook , "Gustavo A. R. Silva" , Jani Nikula , Joonas Lahtinen , Rodrigo Vivi , Tvrtko Ursulin , David Airlie , Simona Vetter , Suzuki K Poulose , Mike Leach , James Clark , Alexander Shishkin , Rikard Falkeborn , Martin Uecker Cc: linux-sparse@vger.kernel.org, linux-kernel@vger.kernel.org, llvm@lists.linux.dev, linux-hardening@vger.kernel.org, intel-gfx@lists.freedesktop.org, dri-devel@lists.freedesktop.org, coresight@lists.linaro.org, linux-arm-kernel@lists.infradead.org, Vincent Mailhol X-Mailer: b4 0.14.2 X-Developer-Signature: v=1; a=openpgp-sha256; l=1089; i=mailhol.vincent@wanadoo.fr; h=from:subject:message-id; bh=MUJOQQOOJL9dYDVaPT65KhZRSmD+ZVJ1pjvxSAixArY=; b=owGbwMvMwCV2McXO4Xp97WbG02pJDOm+74/KH7/U7qegXjXTofXb3TYljXrmuVYbMjW/tLXe4 w0P49vQUcrCIMbFICumyLKsnJNboaPQO+zQX0uYOaxMIEMYuDgFYCKiExj+h59n32w4XUS9Q6Hd rUTL9LrKv/cufhltT6M9vi38WfriOSPDth1s9Y4XTgudV69oFplrmhzR2RRm0jljjlHtl08TxWX ZAQ== X-Developer-Key: i=mailhol.vincent@wanadoo.fr; a=openpgp; fpr=ED8F700574E67F20E574E8E2AB5FEB886DBB99C2 X-Endpoint-Received: by B4 Relay for mailhol.vincent@wanadoo.fr/default with auth_id=291 X-Original-From: Vincent Mailhol Reply-To: mailhol.vincent@wanadoo.fr From: Vincent Mailhol For completion, add statically_false() which is the equivalent of statically_true() except that it will return true only if the input is known to be false at compile time. The == operator is used instead of the ! negation to prevent a -Wint-in-bool-context compiler warning when the argument is not a boolean. For example: statically_false(var * 0) Signed-off-by: Vincent Mailhol --- include/linux/compiler.h | 1 + 1 file changed, 1 insertion(+) diff --git a/include/linux/compiler.h b/include/linux/compiler.h index 469a64dd6495fefab2c85ffc279568a657b72660..a2a56a50dd85227a4fdc62236a2710ca37c5ba52 100644 --- a/include/linux/compiler.h +++ b/include/linux/compiler.h @@ -314,6 +314,7 @@ static inline void *offset_to_ptr(const int *off) * values to determine that the condition is statically true. */ #define statically_true(x) (__builtin_constant_p(x) && (x)) +#define statically_false(x) (__builtin_constant_p(x) && (x) == 0) /* * This is needed in functions which generate the stack canary, see From patchwork Mon Dec 2 17:33:24 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Vincent Mailhol via B4 Relay X-Patchwork-Id: 13891141 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 927D11DB54B; Mon, 2 Dec 2024 17:35:04 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733160904; cv=none; b=idpJD5jANONNlQl39lTVizKa5ffYpzPgZz99TRrmbo0h3RCsvzJgpTXb/TG3IpbmQ4QBBFEGY1dkaXp+LzvNGjUX3GVf4mWOlWCSn278x9A4VRHNEV2h/vB69uClWaiBJzqq7t+ZLIzYhUqGmkbFqBPyIHV/tJAfL+KtUAEb04g= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733160904; c=relaxed/simple; bh=9iW7A+YjBOO9RVnkPZTBuwbq0imIzWIqQzS9oUoJV80=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=Ais69jOnOEsaREge50TMStsPI5heCZ2tv50nRK1iKvXrrhrE+/DpYy5SlgcdJ5bNQi45ckColn99VROOpAXEl4eaGaLLurZnNSZ+EhimIpkWFFH1Tl9yTlqLye21Qk/TxBb2wlXCc5ieu5MHkrmVo9+stVjWGbI5EoGNoX7We5Q= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=roR0wmND; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="roR0wmND" Received: by smtp.kernel.org (Postfix) with ESMTPS id 0E788C4CED9; Mon, 2 Dec 2024 17:35:04 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1733160904; bh=9iW7A+YjBOO9RVnkPZTBuwbq0imIzWIqQzS9oUoJV80=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To:From; b=roR0wmNDodAzyFfPTgFBBp/KqBd/M1Zco1d6OqO8B8sJlOTrYchslvtT08b9PEPAD spi3Zv5Lq4VW/Jihv7VnCNPNtkCFI4MsIeV4lzHFYmMaP7dYZGXCQ8d5TXlEApUNZq 8xoSLxg+uBDYJRHUQDRQQNT2XbDMV9G+MwlYNwxH7cmYQ06PHmtyFfEE6xh1DLG/UW fBq0X8GswKgLTC9l8wZKXKu8xTD6Nt4EJlv1q0krvN4+bcuQhKCJFdz8WlioK9jwoK 9VKeyHkx1LzW8cff48edd8HPqMCYIPhMmgODxWxXnUj/PlHgA8fMAPfb7UemLpzrLf oQq2wYAjdEIDw== Received: from aws-us-west-2-korg-lkml-1.web.codeaurora.org (localhost.localdomain [127.0.0.1]) by smtp.lore.kernel.org (Postfix) with ESMTP id F2538D7833D; Mon, 2 Dec 2024 17:35:03 +0000 (UTC) From: Vincent Mailhol via B4 Relay Date: Tue, 03 Dec 2024 02:33:24 +0900 Subject: [PATCH 02/10] compiler.h: add is_const() as a replacement of __is_constexpr() Precedence: bulk X-Mailing-List: linux-hardening@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20241203-is_constexpr-refactor-v1-2-4e4cbaecc216@wanadoo.fr> References: <20241203-is_constexpr-refactor-v1-0-4e4cbaecc216@wanadoo.fr> In-Reply-To: <20241203-is_constexpr-refactor-v1-0-4e4cbaecc216@wanadoo.fr> To: Linus Torvalds , David Laight , Luc Van Oostenryck , Nathan Chancellor , Nick Desaulniers , Bill Wendling , Justin Stitt , Yury Norov , Rasmus Villemoes , Kees Cook , "Gustavo A. R. Silva" , Jani Nikula , Joonas Lahtinen , Rodrigo Vivi , Tvrtko Ursulin , David Airlie , Simona Vetter , Suzuki K Poulose , Mike Leach , James Clark , Alexander Shishkin , Rikard Falkeborn , Martin Uecker Cc: linux-sparse@vger.kernel.org, linux-kernel@vger.kernel.org, llvm@lists.linux.dev, linux-hardening@vger.kernel.org, intel-gfx@lists.freedesktop.org, dri-devel@lists.freedesktop.org, coresight@lists.linaro.org, linux-arm-kernel@lists.infradead.org, Vincent Mailhol X-Mailer: b4 0.14.2 X-Developer-Signature: v=1; a=openpgp-sha256; l=3781; i=mailhol.vincent@wanadoo.fr; h=from:subject:message-id; bh=ut5SHXlG8TFqM4dSL1voozHgmPfm9PCB5ssUWoNnPGc=; b=owGbwMvMwCV2McXO4Xp97WbG02pJDOm+748Gv37O5/hx2ynzqG0iN+cezF1/Uri8ynPxlkd99 448enNwUUcpC4MYF4OsmCLLsnJOboWOQu+wQ38tYeawMoEMYeDiFICJTCliZNgwxSRxxgaPNqN9 SvblgqYXk6R6q/p2hk3PmPP5/5TfZXaMDPsvHppbuS+xzuf2BZ0oL/Hl4l8WKni3vHLVk9a2ddC S5AQA X-Developer-Key: i=mailhol.vincent@wanadoo.fr; a=openpgp; fpr=ED8F700574E67F20E574E8E2AB5FEB886DBB99C2 X-Endpoint-Received: by B4 Relay for mailhol.vincent@wanadoo.fr/default with auth_id=291 X-Original-From: Vincent Mailhol Reply-To: mailhol.vincent@wanadoo.fr From: Vincent Mailhol __is_constexpr(), while being one of the most glorious one liner hack ever witnessed by mankind, is overly complex. Following the adoption of C11 in the kernel, this macro can be simplified through the use of a _Generic() selection. First, split the macro in two: - __is_const_zero(x): an helper macro; tells whether x is the integer constant expression 0 or something else. - is_const(x): replacement of __is_constexpr(); tells whether x is a integer constant expression. The split serves two purposes: first make it easier to understand; second, __is_const_zero() will be reused as a building block for other is_const_*() macros that will be introduced later on. The core principle of __is_constexpr() to abuse the return type of the ternary operator remains, but all the surrounding sizeof() hack disappear. On a side note, while not relevant to the kernel, __is_constexpr() relied on the GNU extension that sizeof(void) is 1. const_expr() does not use any GNU extensions, making it ISO C compliant. __is_constexpr() is temporarily kept and will be removed once all its users get migrated to is_const() (or one of its friend). Signed-off-by: Vincent Mailhol --- include/linux/compiler.h | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/include/linux/compiler.h b/include/linux/compiler.h index a2a56a50dd85227a4fdc62236a2710ca37c5ba52..30ce06df4153cfdc0fad9bc7bffab9097f8b0450 100644 --- a/include/linux/compiler.h +++ b/include/linux/compiler.h @@ -316,6 +316,47 @@ static inline void *offset_to_ptr(const int *off) #define statically_true(x) (__builtin_constant_p(x) && (x)) #define statically_false(x) (__builtin_constant_p(x) && (x) == 0) +/* + * Whether x is the integer constant expression 0 or something else. + * + * Details: + * - The C11 standard defines in §6.3.2.3.3 + * (void *) + * as a null pointer constant (c.f. the NULL macro). + * - If x evaluates to the integer constant expression 0, + * (void *)(x) + * is a null pointer constant. Else, it is a void * expression. + * - In a ternary expression: + * condition ? operand1 : operand2 + * if one of the two operands is of type void * and the other one + * some other pointer type, the C11 standard defines in §6.5.15.6 + * the resulting type as below: + * if one operand is a null pointer constant, the result has the + * type of the other operand; otherwise [...] the result type is + * a pointer to an appropriately qualified version of void. + * - As such, in + * 0 ? (void *)(x) : (char *)0 + * if x is the integer constant expression 0, operand1 is a null + * pointer constant and the resulting type is that of operand2: + * char *. If x is anything else, the type is void *. + * - The (long) cast silences a compiler warning for when x is not 0. + * - Finally, the _Generic() dispatches the resulting type into a + * Boolean. + * + * Glory to Martin Uecker + */ +#define __is_const_zero(x) \ + _Generic(0 ? (void *)(long)(x) : (char *)0, char *: 1, void *: 0) + +/* + * Returns a constant expression while determining if its argument is a + * constant expression, most importantly without evaluating the argument. + * + * If getting a constant expression is not relevant to you, use the more + * powerful __builtin_constant_p() instead. + */ +#define is_const(x) __is_const_zero(0 * (x)) + /* * This is needed in functions which generate the stack canary, see * arch/x86/kernel/smpboot.c::start_secondary() for an example. From patchwork Mon Dec 2 17:33:25 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Vincent Mailhol via B4 Relay X-Patchwork-Id: 13891140 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 939421DB54C; Mon, 2 Dec 2024 17:35:04 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733160904; cv=none; b=hA2CwraA2X5n/ze2yL2DuYTZwp8dv/sFoM5M5JKtPXt77mpvEXgkMoLITp7KF25EZpieThN/2LEnuBFDbxCyUq1SuSCfr8udld8TQr4MQYO6TwCoGA6B56FMQFkj3i0/MH+eHt2CjpvzO2mjdtv1gVVwIfo6zM9DFiuStuDJ9sg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733160904; c=relaxed/simple; bh=d4JkfBIFjtnH3QVmKyHAXV1Y+cJkoB706DolhEJtz9Q=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=UbR8ADWgZ5G8JzGDSiaDV20po2HFfL9V8OUEGM5xmPChhwZuiBTuDN+qTAVDd4RhyE7dDqdfphg3MndmST1NYv8JU6IeBkVeo2tk1VdJk+K8725UFZTOVzXDblr6EkrSTkPIRfVjV2GLVMpnQ8iERS2/MjujD2BsJjd9/a3UOnc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=IKBHMJsH; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="IKBHMJsH" Received: by smtp.kernel.org (Postfix) with ESMTPS id 2CE44C4CEE0; Mon, 2 Dec 2024 17:35:04 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1733160904; bh=d4JkfBIFjtnH3QVmKyHAXV1Y+cJkoB706DolhEJtz9Q=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To:From; b=IKBHMJsHqq1hMtszvFOcfe9UEP/UCGLNKk9ANnIcCBKQJxvDxJt38mPS8tAgR5c6t j5RlM4BNUTDfxDfI3Avtg0nX8yBTmlggQ5jrzzTBlPG91LszQP7ro4DvKTHMsIFx03 Ax8eQJYEbtpeAxORywU3rX4HP+TuVI/B+I0aVoNVWIPai14lbzwm47hWur1xDIshjw +pk1MzpiSmz0v305U/OGH5TFpUNcCVbn7jPy70aIyrijNwRQAXx8tmMsWl5bYKeSLc HHa0DpnGMURhpv8L6OAhVUs2I0ho4wfjaUlxpY9IPbtnDv+aCGGSr+wGWsqEqAq43Y kFSTBtw2Xu9wg== Received: from aws-us-west-2-korg-lkml-1.web.codeaurora.org (localhost.localdomain [127.0.0.1]) by smtp.lore.kernel.org (Postfix) with ESMTP id 1FFDCD7833E; Mon, 2 Dec 2024 17:35:04 +0000 (UTC) From: Vincent Mailhol via B4 Relay Date: Tue, 03 Dec 2024 02:33:25 +0900 Subject: [PATCH 03/10] compiler.h: add is_const_true() and is_const_false() Precedence: bulk X-Mailing-List: linux-hardening@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20241203-is_constexpr-refactor-v1-3-4e4cbaecc216@wanadoo.fr> References: <20241203-is_constexpr-refactor-v1-0-4e4cbaecc216@wanadoo.fr> In-Reply-To: <20241203-is_constexpr-refactor-v1-0-4e4cbaecc216@wanadoo.fr> To: Linus Torvalds , David Laight , Luc Van Oostenryck , Nathan Chancellor , Nick Desaulniers , Bill Wendling , Justin Stitt , Yury Norov , Rasmus Villemoes , Kees Cook , "Gustavo A. R. Silva" , Jani Nikula , Joonas Lahtinen , Rodrigo Vivi , Tvrtko Ursulin , David Airlie , Simona Vetter , Suzuki K Poulose , Mike Leach , James Clark , Alexander Shishkin , Rikard Falkeborn , Martin Uecker Cc: linux-sparse@vger.kernel.org, linux-kernel@vger.kernel.org, llvm@lists.linux.dev, linux-hardening@vger.kernel.org, intel-gfx@lists.freedesktop.org, dri-devel@lists.freedesktop.org, coresight@lists.linaro.org, linux-arm-kernel@lists.infradead.org, Vincent Mailhol X-Mailer: b4 0.14.2 X-Developer-Signature: v=1; a=openpgp-sha256; l=3669; i=mailhol.vincent@wanadoo.fr; h=from:subject:message-id; bh=vXXScdJ5PminDGoxDgA6Iag0c2X2hzS5qLrxjoqdg1k=; b=owGbwMvMwCV2McXO4Xp97WbG02pJDOm+74+abz5c0vD2uK1kR42Xpca9XMcb2cKG/u3MBSvKH /Ku00juKGVhEONikBVTZFlWzsmt0FHoHXboryXMHFYmkCEMXJwCMBG/KIY/HLZ2z5iFOme/c1oW fOXLsXk2BbHp55dO/BLheFbvzrrtzYwMF/986Qm84Gxx5Kl200c5i3rbi3snbcsKbZlrqLn/5is dBgA= X-Developer-Key: i=mailhol.vincent@wanadoo.fr; a=openpgp; fpr=ED8F700574E67F20E574E8E2AB5FEB886DBB99C2 X-Endpoint-Received: by B4 Relay for mailhol.vincent@wanadoo.fr/default with auth_id=291 X-Original-From: Vincent Mailhol Reply-To: mailhol.vincent@wanadoo.fr From: Vincent Mailhol __builtin_constant_p() is known for not always being able to produce constant expression [1] which led 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 on both clang and GCC */ static char arr[statically_true(a) ? 1 : 2]; } Define a new is_const_true() and is_const_false() pair of macros which, by making use of __is_const_zero(), always produces a constant expression. Note that is_const_false() can not be directly defined as an alias to __is_const_zero(). Otherwise, it could yield some false positives on huge numbers because of a lost of precision when doing the (long) cast in __is_const_zero(). Example: is_const_false((u128)ULONG_MAX << BITS_PER_LONG) Furthermore, using the ! operator like this: #define is_const_true(x) __is_const_zero(!(x)) #define is_const_false(x) __is_const_zero(!!(x)) would yield a -Wint-in-bool-context compiler warning if the argument is not a boolean. Use the == and != operators instead. It should be noted that statically_true/false() are the only ones capable of folding tautologic expressions in which at least one on the operands is not a constant expression. For example: statically_true(true || var) statically_true(var == var) statically_false(var * 0) statically_false(var * 8 % 4) always evaluate to true, whereas all of these would be false under is_const_true/false() if var is not a constant expression [3]. For this reason, usage of const_true/false() should be the exception. Reflect in the documentation that const_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()") Link: https://git.kernel.org/torvalds/c/3c8ba0d61d04 [3] https://godbolt.org/z/E4r7EaxW9 Signed-off-by: Vincent Mailhol --- include/linux/compiler.h | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/include/linux/compiler.h b/include/linux/compiler.h index 30ce06df4153cfdc0fad9bc7bffab9097f8b0450..165aa5b9bc484376087a130a1ac1f3edb50c983d 100644 --- a/include/linux/compiler.h +++ b/include/linux/compiler.h @@ -357,6 +357,29 @@ static inline void *offset_to_ptr(const int *off) */ #define is_const(x) __is_const_zero(0 * (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 trade-off: is_const_true() requires all its operands to + * be compile time constants. Else, it would always returns false even + * on the most trivial cases like: + * + * true || non_const_expr + * + * On the opposite, statically_true() is able to fold more complex + * tautologies and will return true on expressions such as: + * + * !(non_const_expr * 8 % 4) + * + * For the general case, statically_true() is better. + */ +#define is_const_true(x) __is_const_zero((x) == 0) +#define is_const_false(x) __is_const_zero((x) != 0) + /* * This is needed in functions which generate the stack canary, see * arch/x86/kernel/smpboot.c::start_secondary() for an example. From patchwork Mon Dec 2 17:33:26 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Vincent Mailhol via B4 Relay X-Patchwork-Id: 13891139 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 853F11DB548; Mon, 2 Dec 2024 17:35:04 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733160904; cv=none; b=hOuU2ZDsn9e1oO79/kIPotAVAaFEXKiR9gZzUuDZteCb5qwtW+hbVhTNNXxH+pZvLE5lvFQ+x1Z9La7a4sbhAE45HoJ4StmaQGgkemcbzzRDDafcVMNXci3xLIIlEKht4t+ukuAqTXmz5eKxUVCzpOcGNiGg7kJYVlGk1aik8KA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733160904; c=relaxed/simple; bh=xwTNFn3pAcjVSWQWx0fHAn53Pqy1YgPTXPD7CvZlCBE=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=svXudUNAAoW5Mo6kZ3XttlnvfhzsfEBz/1iQ5ZnTfWzbnuP+gjPfhaJeG8wrtY/IGxpDeQSiiwEn0QxgjJIyZkTDe9x+NgnaC0c34TG8EpcIPs+AMPv5TmYi8Vyj+ULYaGCqdLWiew/mZF7OJ6UTQEEA/TheBj3G2/EMmLQ36rk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=Ungmd37L; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="Ungmd37L" Received: by smtp.kernel.org (Postfix) with ESMTPS id 451ECC4CEE2; Mon, 2 Dec 2024 17:35:04 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1733160904; bh=xwTNFn3pAcjVSWQWx0fHAn53Pqy1YgPTXPD7CvZlCBE=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To:From; b=Ungmd37L4dkWQvhcuFclEv+4CcvM6Wg2DepR4tJAgCJBakZJ4OmYWS9Csu9MKZr41 XOv24ODLjWwD+67sbFUifpRNCmPEQhS8JwS9csGxIlgYR/YLsHOkJVI+rsTOnZRPEy yH2cFtTBr9uKzmixHN0oJOTceYmKMqTwlwwO5KPm5TZsGgwOtl4ORdHEwBnZoivBBH bSGeNjS+e1SaQ5F6GGhF5sehAhHeG+tGesILeTagGwibgp+e6zEqRMEwmeSetfdcXn lSorEV45P7zurGKv9mS3+xrhz3uFMSJ+2PzbCz77WDhjVgbfudPNRgeib2oB/A/4F2 ITeXGxWMkkBeQ== Received: from aws-us-west-2-korg-lkml-1.web.codeaurora.org (localhost.localdomain [127.0.0.1]) by smtp.lore.kernel.org (Postfix) with ESMTP id 394CFD7833C; Mon, 2 Dec 2024 17:35:04 +0000 (UTC) From: Vincent Mailhol via B4 Relay Date: Tue, 03 Dec 2024 02:33:26 +0900 Subject: [PATCH 04/10] linux/bits.h: simplify GENMASK_INPUT_CHECK() by using is_const_true() Precedence: bulk X-Mailing-List: linux-hardening@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20241203-is_constexpr-refactor-v1-4-4e4cbaecc216@wanadoo.fr> References: <20241203-is_constexpr-refactor-v1-0-4e4cbaecc216@wanadoo.fr> In-Reply-To: <20241203-is_constexpr-refactor-v1-0-4e4cbaecc216@wanadoo.fr> To: Linus Torvalds , David Laight , Luc Van Oostenryck , Nathan Chancellor , Nick Desaulniers , Bill Wendling , Justin Stitt , Yury Norov , Rasmus Villemoes , Kees Cook , "Gustavo A. R. Silva" , Jani Nikula , Joonas Lahtinen , Rodrigo Vivi , Tvrtko Ursulin , David Airlie , Simona Vetter , Suzuki K Poulose , Mike Leach , James Clark , Alexander Shishkin , Rikard Falkeborn , Martin Uecker Cc: linux-sparse@vger.kernel.org, linux-kernel@vger.kernel.org, llvm@lists.linux.dev, linux-hardening@vger.kernel.org, intel-gfx@lists.freedesktop.org, dri-devel@lists.freedesktop.org, coresight@lists.linaro.org, linux-arm-kernel@lists.infradead.org, Vincent Mailhol X-Mailer: b4 0.14.2 X-Developer-Signature: v=1; a=openpgp-sha256; l=1272; i=mailhol.vincent@wanadoo.fr; h=from:subject:message-id; bh=7pxUmK/6W4pXlFZB1NGxttLkuRalLbmF/D69OP2jG1o=; b=owGbwMvMwCV2McXO4Xp97WbG02pJDOm+749GXp33N/vXlsdiTw9lODzSEugV7vlifP1CSKJpp EL7cw+XjlIWBjEuBlkxRZZl5ZzcCh2F3mGH/lrCzGFlAhnCwMUpABNR2Mnw35utNVRP1Thc1XO/ kFrZj4LtpwX/s53aHOaffebyus1pTgz/82/rzvn0wGB67rdoO1XPe0Viq2NqdiXV19t3Vhuemj6 RGwA= X-Developer-Key: i=mailhol.vincent@wanadoo.fr; a=openpgp; fpr=ED8F700574E67F20E574E8E2AB5FEB886DBB99C2 X-Endpoint-Received: by B4 Relay for mailhol.vincent@wanadoo.fr/default with auth_id=291 X-Original-From: Vincent Mailhol Reply-To: mailhol.vincent@wanadoo.fr From: Vincent Mailhol In GENMASK_INPUT_CHECK(), __builtin_choose_expr(__is_constexpr((l) > (h)), (l) > (h), 0) is equivalent to: is_const_true((l) > (h)) Apply is_const_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 --- 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 60044b6088172b3f26aa3f17cdaede9786863dae..ef0119e6179e1ca95345a3d4d3327ba19633028e 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(is_const_true((l) > (h))) #else /* * BUILD_BUG_ON_ZERO is not available in h files included from asm files, From patchwork Mon Dec 2 17:33:27 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Vincent Mailhol via B4 Relay X-Patchwork-Id: 13891144 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 D870F1DD9A6; Mon, 2 Dec 2024 17:35:04 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733160905; cv=none; b=aC46UuUqnc8+Z1Fl4X5nwgG7lYehxz08NMb34slQswfjfoquaE7Iuzsje3+xIQjYJguaEbCatHcQKMmaXCWDtCxtcgXuNbA1FgcDWq1a2U31Av5Hveg/emFodf3NN3IPI/cCH4UjUMtoo0vkK6jyLihvNd9W92wYCKS0HaL1hWg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733160905; c=relaxed/simple; bh=FZmPlNwEP0dLH9uzEAPVjoA2IENASDBcbOxyxt6OpgQ=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=qrJJWTK9rBQgUTSp5pr+8bOnsLL+YPgDcwFRS/FHeYgGaGjt9I2S171xiu9EXLwHTvGBMvPtuaiN6LBPZokKjpKtrYZF5QBSxIR8d2G4BSyhF3IFX8zX6pECbyL5Z7dztH0ExggQwo4YIhpR/E9Wt6hFg/ngowJ6zgdOgUBh9R8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=Np6Vb9OH; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="Np6Vb9OH" Received: by smtp.kernel.org (Postfix) with ESMTPS id 637F2C4CEE3; Mon, 2 Dec 2024 17:35:04 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1733160904; bh=FZmPlNwEP0dLH9uzEAPVjoA2IENASDBcbOxyxt6OpgQ=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To:From; b=Np6Vb9OHybQfNzDBImwNMJVhDJu/8UXyGodvRv29g6p6oSEeNdnBODBy7/6sK8Mt+ zlRyO0cedmOcRUNaIFPlX4cc9ErbeCj7+h5RT4pjxrGC77hKjrDMpHgNvCTqz/nTsN w2i9wWcIiPMIwKxE9P4tpnswci6LoL102L0oXYpKajA8DGNgirvUJuwLmrOme0joUv au2yD/+s5HolY3ATZHEuyWQNYlk57qFV5d0V2FGBKiMgUjnTAD8aFWym1eWRfJlkBL pu0rc2snu+Ik9dYybACLnKehUFwBD96canGBtO+Btp8w/PUQyUWT4dLC5UwFytqCqY 7y0i7z89Sln0A== Received: from aws-us-west-2-korg-lkml-1.web.codeaurora.org (localhost.localdomain [127.0.0.1]) by smtp.lore.kernel.org (Postfix) with ESMTP id 51D2FD7833D; Mon, 2 Dec 2024 17:35:04 +0000 (UTC) From: Vincent Mailhol via B4 Relay Date: Tue, 03 Dec 2024 02:33:27 +0900 Subject: [PATCH 05/10] minmax: simplify __clamp_once() by using is_const_false() Precedence: bulk X-Mailing-List: linux-hardening@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20241203-is_constexpr-refactor-v1-5-4e4cbaecc216@wanadoo.fr> References: <20241203-is_constexpr-refactor-v1-0-4e4cbaecc216@wanadoo.fr> In-Reply-To: <20241203-is_constexpr-refactor-v1-0-4e4cbaecc216@wanadoo.fr> To: Linus Torvalds , David Laight , Luc Van Oostenryck , Nathan Chancellor , Nick Desaulniers , Bill Wendling , Justin Stitt , Yury Norov , Rasmus Villemoes , Kees Cook , "Gustavo A. R. Silva" , Jani Nikula , Joonas Lahtinen , Rodrigo Vivi , Tvrtko Ursulin , David Airlie , Simona Vetter , Suzuki K Poulose , Mike Leach , James Clark , Alexander Shishkin , Rikard Falkeborn , Martin Uecker Cc: linux-sparse@vger.kernel.org, linux-kernel@vger.kernel.org, llvm@lists.linux.dev, linux-hardening@vger.kernel.org, intel-gfx@lists.freedesktop.org, dri-devel@lists.freedesktop.org, coresight@lists.linaro.org, linux-arm-kernel@lists.infradead.org, Vincent Mailhol X-Mailer: b4 0.14.2 X-Developer-Signature: v=1; a=openpgp-sha256; l=1058; i=mailhol.vincent@wanadoo.fr; h=from:subject:message-id; bh=ktV9jrZU0giaHV9vBo3GiXQr/Qxl6VWxrueXUQe+OfY=; b=owGbwMvMwCV2McXO4Xp97WbG02pJDOm+74+eev1x37SU7/s4fOZsfexXwSuwUZXlqNClRp+XD 71fb+e91lHKwiDGxSArpsiyrJyTW6Gj0Dvs0F9LmDmsTCBDGLg4BWAit84y/BX7aL9/YU3XnR7j ixp8Oy/Oeb/9EFPOXNM1rhuKog5yzH/PyPD2dPyivxyLOtx7gqSDZL8HtSqF7j576ccC5tVXdN5 +EmADAA== X-Developer-Key: i=mailhol.vincent@wanadoo.fr; a=openpgp; fpr=ED8F700574E67F20E574E8E2AB5FEB886DBB99C2 X-Endpoint-Received: by B4 Relay for mailhol.vincent@wanadoo.fr/default with auth_id=291 X-Original-From: Vincent Mailhol Reply-To: mailhol.vincent@wanadoo.fr From: Vincent Mailhol In __clamp_once(), __builtin_choose_expr(__is_constexpr((lo) > (hi)), (lo) <= (hi), true) is equivalent to: !is_const_false((lo) <= (hi)) Apply is_const_false() to simplify __clamp_once(). Signed-off-by: Vincent Mailhol --- include/linux/minmax.h | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/include/linux/minmax.h b/include/linux/minmax.h index 98008dd92153db10c672155bca93201ffabee994..431bf76ac460a11a2e4af23acd90c0d26e99c862 100644 --- a/include/linux/minmax.h +++ b/include/linux/minmax.h @@ -111,8 +111,7 @@ __auto_type uval = (val); \ __auto_type ulo = (lo); \ __auto_type uhi = (hi); \ - static_assert(__builtin_choose_expr(__is_constexpr((lo) > (hi)), \ - (lo) <= (hi), true), \ + static_assert(!is_const_false((lo) <= (hi)), \ "clamp() low limit " #lo " greater than high limit " #hi); \ BUILD_BUG_ON_MSG(!__types_ok3(val,lo,hi,uval,ulo,uhi), \ "clamp("#val", "#lo", "#hi") signedness error"); \ From patchwork Mon Dec 2 17:33:28 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Vincent Mailhol via B4 Relay X-Patchwork-Id: 13891142 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 A5EED1AB51F; Mon, 2 Dec 2024 17:35:04 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733160904; cv=none; b=PGotV4JpsZzbyrbdVf7WcachkGMWWk80fSn8PAgulj9PiZVKguE1pE9WtUaYf+hSVO/orsgdGNdLX2J7EeepDjf1EPlAyc8waFAEl6KXfO7HjEm26Ri0KcQ+P7N3Te1p1Wa/KPhhWC6JYyn0nXT9Cgxuc09g7L6sNV/L3ey4MVE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733160904; c=relaxed/simple; bh=otRfIIpR9ke8jKx0T5rYqZgdrJ75ZS57/Fik5wYJ77g=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=ST88QKS52jUDpnO6NNf62uBPndyJ3su4trElWQ/eqCvR7W4N7zZLxQdZ1eXIFIsiINit528lvO/Dunc7iPxJOnLNJL/c4n3JvjZo6cfPaHRjzK23UoOI7GWyuaW5zzsxTzSeBsy9gss9Qy5DuoJmPsPsgUo3GIkh1itwo+k+msM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=mbv/N+QL; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="mbv/N+QL" Received: by smtp.kernel.org (Postfix) with ESMTPS id 7D9C2C4CED1; Mon, 2 Dec 2024 17:35:04 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1733160904; bh=otRfIIpR9ke8jKx0T5rYqZgdrJ75ZS57/Fik5wYJ77g=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To:From; b=mbv/N+QLcgtpQR8QD3+Ebo0ZkA7L0mv7KOGi59I1rnrkhoHvWWt1ejgoGG0hn3cQH 7vY0bEpghsh4bdGKfDPWkJgjgE9H1kbPj3okgFTgy7GsQ5nHdE7+BlS4CfYCtp+olK IxHhytNs+RjNxOecYykd2M4jLLUkudPct9tMAWZ31vqh51ucXMeaL2xLFjw4duIFDw maNwAvLXVHp5++7gPZAAkp9asgFSy/lVIQgrV/RYVoGKbVqtKt/DGnAwPQJVU+dLvv OQNeCRsZUu9f3KQ7My3boRoQ7P4tB3aYMYuiHyP2HChG+NdQkCk0tLHRZQHijwMmM6 fzSDVM2CqMrSQ== Received: from aws-us-west-2-korg-lkml-1.web.codeaurora.org (localhost.localdomain [127.0.0.1]) by smtp.lore.kernel.org (Postfix) with ESMTP id 6DAD4D7833C; Mon, 2 Dec 2024 17:35:04 +0000 (UTC) From: Vincent Mailhol via B4 Relay Date: Tue, 03 Dec 2024 02:33:28 +0900 Subject: [PATCH 06/10] fortify: replace __is_constexpr() by is_const() in strlen() Precedence: bulk X-Mailing-List: linux-hardening@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20241203-is_constexpr-refactor-v1-6-4e4cbaecc216@wanadoo.fr> References: <20241203-is_constexpr-refactor-v1-0-4e4cbaecc216@wanadoo.fr> In-Reply-To: <20241203-is_constexpr-refactor-v1-0-4e4cbaecc216@wanadoo.fr> To: Linus Torvalds , David Laight , Luc Van Oostenryck , Nathan Chancellor , Nick Desaulniers , Bill Wendling , Justin Stitt , Yury Norov , Rasmus Villemoes , Kees Cook , "Gustavo A. R. Silva" , Jani Nikula , Joonas Lahtinen , Rodrigo Vivi , Tvrtko Ursulin , David Airlie , Simona Vetter , Suzuki K Poulose , Mike Leach , James Clark , Alexander Shishkin , Rikard Falkeborn , Martin Uecker Cc: linux-sparse@vger.kernel.org, linux-kernel@vger.kernel.org, llvm@lists.linux.dev, linux-hardening@vger.kernel.org, intel-gfx@lists.freedesktop.org, dri-devel@lists.freedesktop.org, coresight@lists.linaro.org, linux-arm-kernel@lists.infradead.org, Vincent Mailhol X-Mailer: b4 0.14.2 X-Developer-Signature: v=1; a=openpgp-sha256; l=1072; i=mailhol.vincent@wanadoo.fr; h=from:subject:message-id; bh=jMI6GLWGOq2qwprnzsUGV4xG+9C9iHAQ1IPi4ZG8B0k=; b=owGbwMvMwCV2McXO4Xp97WbG02pJDOm+749+/rqPW6pvpu+W2UzTPlgk/hDbmVItMcnU/8kTl hNartOudJSyMIhxMciKKbIsK+fkVugo9A479NcSZg4rE8gQBi5OAZiIaBsjw/wOdQfrmY9Kw6X+ neVT9cwNVGdRL0772611cXrWBz25mQx/RT0nvEo4NlP8lov1Q74UufJwLg3WJUzZSUkaeZNe+69 lBQA= X-Developer-Key: i=mailhol.vincent@wanadoo.fr; a=openpgp; fpr=ED8F700574E67F20E574E8E2AB5FEB886DBB99C2 X-Endpoint-Received: by B4 Relay for mailhol.vincent@wanadoo.fr/default with auth_id=291 X-Original-From: Vincent Mailhol Reply-To: mailhol.vincent@wanadoo.fr From: Vincent Mailhol is_const() is a one to one replacement of __is_constexpr(). Do the replacement so that __is_constexpr() can be removed. Signed-off-by: Vincent Mailhol --- include/linux/fortify-string.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/include/linux/fortify-string.h b/include/linux/fortify-string.h index 0d99bf11d260a3482bbe46e35c7553c0ccfb8b94..e3f2f772c5439ef71eb4a904b4ce27956bc69743 100644 --- a/include/linux/fortify-string.h +++ b/include/linux/fortify-string.h @@ -254,8 +254,8 @@ __FORTIFY_INLINE __kernel_size_t strnlen(const char * const POS p, __kernel_size * Returns number of characters in @p (NOT including the final NUL). * */ -#define strlen(p) \ - __builtin_choose_expr(__is_constexpr(__builtin_strlen(p)), \ +#define strlen(p) \ + __builtin_choose_expr(is_const(__builtin_strlen(p)), \ __builtin_strlen(p), __fortify_strlen(p)) __FORTIFY_INLINE __diagnose_as(__builtin_strlen, 1) __kernel_size_t __fortify_strlen(const char * const POS p) From patchwork Mon Dec 2 17:33:29 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Vincent Mailhol via B4 Relay X-Patchwork-Id: 13891143 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 BDA951DC1BA; Mon, 2 Dec 2024 17:35:04 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733160904; cv=none; b=YrdnclkP5sAAffXH9Ggoi72ajh4qv5dI+iV0KwsdAP6vwMFazB0J11/VbOaoECEuNFa84G9WkBOJht1LqE3ANv6Bq3s1AZNM14qlzhgVF0vuPTDP/2VtfSp6qdUEn2OZWu4r03MpaRFPncn5/lfiljFzWuoDyi20gimyaJGivkw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733160904; c=relaxed/simple; bh=0n7K8123aVRmHZxGT60ksDzEW/cULJYrbFl/Q86l37M=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=WVWX531HXG4q+utWscENrYLbvZ7/FyRm8+8nen477ChNx9444rci9GrD0KrcmJn/KCnUt+JIn+lih08RRAYi1lbsC++PoyaY/WoEYTxj9tAuxzpMyD+fNISKM+QBNY6vIPXdiSGHStoiJ8n5vSQV3QSEy/ItuJRBQX35x8btGeg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=j/90kkzH; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="j/90kkzH" Received: by smtp.kernel.org (Postfix) with ESMTPS id 9AEC8C4CEE9; Mon, 2 Dec 2024 17:35:04 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1733160904; bh=0n7K8123aVRmHZxGT60ksDzEW/cULJYrbFl/Q86l37M=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To:From; b=j/90kkzHJYpxgw9fCykFpmgCYrZb/HPUY4Fn5AfokJhP84zgU7WPjnPRfcVDKNTZ6 wg+fmlmBVyCt//Ey3/M6ExHqZHnZfnNtjNwYpa7cjYFMZUUr9k/jEbhhMlkpXAEPu8 tyBddS+OV2r4MV7GOACqXDiV2MFmPliFoCKNi3gXMxhlm3mvVMqJV7rk2kHU1Nf1iF Rmspw7y6S/BTMrAVwZfQYYIvu3wu1tA0CWgmUb/hRT/5vecMQRWoCUH10RiN4SK+ik KNISAN2qv9Bv/qfkjhjCxYcPlt6QwRjzMjF4MkKIAmeuKGAEzSHUCnhg3q3mCxkg59 zwzNKjWmBqpZg== Received: from aws-us-west-2-korg-lkml-1.web.codeaurora.org (localhost.localdomain [127.0.0.1]) by smtp.lore.kernel.org (Postfix) with ESMTP id 88EEED7831C; Mon, 2 Dec 2024 17:35:04 +0000 (UTC) From: Vincent Mailhol via B4 Relay Date: Tue, 03 Dec 2024 02:33:29 +0900 Subject: [PATCH 07/10] overflow: replace __is_constexpr() by is_const() Precedence: bulk X-Mailing-List: linux-hardening@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20241203-is_constexpr-refactor-v1-7-4e4cbaecc216@wanadoo.fr> References: <20241203-is_constexpr-refactor-v1-0-4e4cbaecc216@wanadoo.fr> In-Reply-To: <20241203-is_constexpr-refactor-v1-0-4e4cbaecc216@wanadoo.fr> To: Linus Torvalds , David Laight , Luc Van Oostenryck , Nathan Chancellor , Nick Desaulniers , Bill Wendling , Justin Stitt , Yury Norov , Rasmus Villemoes , Kees Cook , "Gustavo A. R. Silva" , Jani Nikula , Joonas Lahtinen , Rodrigo Vivi , Tvrtko Ursulin , David Airlie , Simona Vetter , Suzuki K Poulose , Mike Leach , James Clark , Alexander Shishkin , Rikard Falkeborn , Martin Uecker Cc: linux-sparse@vger.kernel.org, linux-kernel@vger.kernel.org, llvm@lists.linux.dev, linux-hardening@vger.kernel.org, intel-gfx@lists.freedesktop.org, dri-devel@lists.freedesktop.org, coresight@lists.linaro.org, linux-arm-kernel@lists.infradead.org, Vincent Mailhol X-Mailer: b4 0.14.2 X-Developer-Signature: v=1; a=openpgp-sha256; l=2119; i=mailhol.vincent@wanadoo.fr; h=from:subject:message-id; bh=3zhdAHGJKxe/cfP6gSQHSq0jEALX5ywlxJaLknSE8PU=; b=owGbwMvMwCV2McXO4Xp97WbG02pJDOm+74/q876MC9CKYjyo9Ky9cdPpsilBc38vnbBxvs6ZW UF5mtXRHaUsDGJcDLJiiizLyjm5FToKvcMO/bWEmcPKBDKEgYtTACbyQ5fhr7xK1ZyIKSviHgrp 8+d4/NxvmD6DX/jspxv39qf9Nnzx8QYjw5+v16eWdS2+XNXV/TjVMnulR8GKU3Mazly4pdn98Ng OKx4A X-Developer-Key: i=mailhol.vincent@wanadoo.fr; a=openpgp; fpr=ED8F700574E67F20E574E8E2AB5FEB886DBB99C2 X-Endpoint-Received: by B4 Relay for mailhol.vincent@wanadoo.fr/default with auth_id=291 X-Original-From: Vincent Mailhol Reply-To: mailhol.vincent@wanadoo.fr From: Vincent Mailhol is_const() is a one to one replacement of __is_constexpr(). Do the replacement so that __is_constexpr() can be removed. Signed-off-by: Vincent Mailhol --- include/linux/overflow.h | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/include/linux/overflow.h b/include/linux/overflow.h index 0c7e3dcfe8670cb95f371a6f7d41bb8b63b1786c..d69361a3128897d493b5e2ec471f077ca2b9861d 100644 --- a/include/linux/overflow.h +++ b/include/linux/overflow.h @@ -234,7 +234,7 @@ static inline bool __must_check __must_check_overflow(bool overflow) * Returns: true if overflow can occur, false otherwise. */ #define overflows_type(n, T) \ - __builtin_choose_expr(__is_constexpr(n), \ + __builtin_choose_expr(is_const(n), \ __overflows_type_constexpr(n, T), \ __overflows_type(n, T)) @@ -250,7 +250,7 @@ static inline bool __must_check __must_check_overflow(bool overflow) * back to __same_type(). */ #define castable_to_type(n, T) \ - __builtin_choose_expr(__is_constexpr(n), \ + __builtin_choose_expr(is_const(n), \ !__overflows_type_constexpr(n, T), \ __same_type(n, T)) @@ -352,7 +352,7 @@ static inline size_t __must_check size_sub(size_t minuend, size_t subtrahend) * Return: number of bytes needed or SIZE_MAX on overflow. */ #define flex_array_size(p, member, count) \ - __builtin_choose_expr(__is_constexpr(count), \ + __builtin_choose_expr(is_const(count), \ (count) * sizeof(*(p)->member) + __must_be_array((p)->member), \ size_mul(count, sizeof(*(p)->member) + __must_be_array((p)->member))) @@ -368,7 +368,7 @@ static inline size_t __must_check size_sub(size_t minuend, size_t subtrahend) * Return: number of bytes needed or SIZE_MAX on overflow. */ #define struct_size(p, member, count) \ - __builtin_choose_expr(__is_constexpr(count), \ + __builtin_choose_expr(is_const(count), \ sizeof(*(p)) + flex_array_size(p, member, count), \ size_add(sizeof(*(p)), flex_array_size(p, member, count))) From patchwork Mon Dec 2 17:33:30 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Vincent Mailhol via B4 Relay X-Patchwork-Id: 13891145 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 307921DDC13; Mon, 2 Dec 2024 17:35:05 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733160905; cv=none; b=EIpQAh8PV2Msb3s/5SGAvMIB/yThvy4SxIUvECY0sT3id6eeafPZXseZDZbgGXLBi5r5iVqp3uv91qhi3DarnVmVbJkT9WompJ1qQGEDxfBLbJ/cwsuMTj3lD7P0TSpZrb6x+BvzvHLhvgMx4ih+TXjs83o4wv95TR3Iy6EGT5w= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733160905; c=relaxed/simple; bh=XFZ/ReIcnrUD50vCJZHFqXxOz1HrqXseDp2bfbIhVEs=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=a0TYo9cb4oM0e+Xn0Ut9n9R3cBoW+b8ycA9AkGy9LnG2p+Dal35IZJdZr5eqJBk+1jtBRs5oof4npcNV7NOfEsJ2MianQG4X4YS0HlI60W8cRQQ4rFgeZzJrxeeWJD3WWWzwW1i0F5iOfhWkgOr9SKmncTwBWZHBjiyBNJh6iCw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=rJoAowSx; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="rJoAowSx" Received: by smtp.kernel.org (Postfix) with ESMTPS id B6AFFC4CEDF; Mon, 2 Dec 2024 17:35:04 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1733160904; bh=XFZ/ReIcnrUD50vCJZHFqXxOz1HrqXseDp2bfbIhVEs=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To:From; b=rJoAowSx1LVP7Btyr5lB+8xAdHd7iP7/JAEPrrfN3Xk7muPKGT0/iepah+m+/jhba Ar74BLMFdFjoY6XfhjnKewDWfDbbNWm8KYhpPdmtR2c2uI1UoMu+gcUuSlNiVE2y5C cLzPppN5gmQTXbxsUODrezXWUbzKIUKreCWfcnGpNv2fMKpniPb2PWEjRBFVycjnqw +Px5hGZDoxjmwuxyNrSYJlgdwGWpKd7HSDVJ0o3CXNlMYqxpUOmzvzH4PZ+3tlPI/n qt8BcDigGGcCkkUpKS8OkwRHfN7edpW8csZ9LebomUgjbSxuaK0Xl8HZmJN/u+6al8 HnaEvpg18+ZYQ== Received: from aws-us-west-2-korg-lkml-1.web.codeaurora.org (localhost.localdomain [127.0.0.1]) by smtp.lore.kernel.org (Postfix) with ESMTP id A7656D7833E; Mon, 2 Dec 2024 17:35:04 +0000 (UTC) From: Vincent Mailhol via B4 Relay Date: Tue, 03 Dec 2024 02:33:30 +0900 Subject: [PATCH 08/10] drm/i915/reg: replace __is_const_expr() by is_const_true() or is_const() Precedence: bulk X-Mailing-List: linux-hardening@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20241203-is_constexpr-refactor-v1-8-4e4cbaecc216@wanadoo.fr> References: <20241203-is_constexpr-refactor-v1-0-4e4cbaecc216@wanadoo.fr> In-Reply-To: <20241203-is_constexpr-refactor-v1-0-4e4cbaecc216@wanadoo.fr> To: Linus Torvalds , David Laight , Luc Van Oostenryck , Nathan Chancellor , Nick Desaulniers , Bill Wendling , Justin Stitt , Yury Norov , Rasmus Villemoes , Kees Cook , "Gustavo A. R. Silva" , Jani Nikula , Joonas Lahtinen , Rodrigo Vivi , Tvrtko Ursulin , David Airlie , Simona Vetter , Suzuki K Poulose , Mike Leach , James Clark , Alexander Shishkin , Rikard Falkeborn , Martin Uecker Cc: linux-sparse@vger.kernel.org, linux-kernel@vger.kernel.org, llvm@lists.linux.dev, linux-hardening@vger.kernel.org, intel-gfx@lists.freedesktop.org, dri-devel@lists.freedesktop.org, coresight@lists.linaro.org, linux-arm-kernel@lists.infradead.org, Vincent Mailhol X-Mailer: b4 0.14.2 X-Developer-Signature: v=1; a=openpgp-sha256; l=6558; i=mailhol.vincent@wanadoo.fr; h=from:subject:message-id; bh=IT41eIS+OIIsRMdNS4yKlf8V72ofU+yIl1OolUa3zxc=; b=owGbwMvMwCV2McXO4Xp97WbG02pJDOm+748G+l5SvWBYeEcwf3ZH9lTPGKdm67Bt3/tZLGav/ 10qOqOto5SFQYyLQVZMkWVZOSe3Qkehd9ihv5Ywc1iZQIYwcHEKwESexjIyLH55gHdrk2cszxbF xhXdX74eKihrm/W6N9dCbd7DwkLng4wM65yVF6n33pSYytozo6h2wxde748zo9jPf1NZx+O44VI lOwA= X-Developer-Key: i=mailhol.vincent@wanadoo.fr; a=openpgp; fpr=ED8F700574E67F20E574E8E2AB5FEB886DBB99C2 X-Endpoint-Received: by B4 Relay for mailhol.vincent@wanadoo.fr/default with auth_id=291 X-Original-From: Vincent Mailhol Reply-To: mailhol.vincent@wanadoo.fr From: Vincent Mailhol Most of the use of __is_const_expr() in i915_reg_defs.h are just to test whether an expression is known to be true. Because those checks are all done in a BUILD_BUG_ON_ZERO(), replace those with is_const_true(). Replace the few other occurrences of __is_const_expr() with is_const(). Signed-off-by: Vincent Mailhol --- drivers/gpu/drm/i915/i915_reg_defs.h | 47 +++++++++++++++++------------------- 1 file changed, 22 insertions(+), 25 deletions(-) diff --git a/drivers/gpu/drm/i915/i915_reg_defs.h b/drivers/gpu/drm/i915/i915_reg_defs.h index e251bcc0c89f5710125bc70f07851b2cb978c89c..6ed2fb9cf506a3bd6467ba30f9d0e863d62762f3 100644 --- a/drivers/gpu/drm/i915/i915_reg_defs.h +++ b/drivers/gpu/drm/i915/i915_reg_defs.h @@ -19,8 +19,7 @@ */ #define REG_BIT(__n) \ ((u32)(BIT(__n) + \ - BUILD_BUG_ON_ZERO(__is_constexpr(__n) && \ - ((__n) < 0 || (__n) > 31)))) + BUILD_BUG_ON_ZERO(is_const_true((__n) < 0 || (__n) > 31)))) /** * REG_BIT8() - Prepare a u8 bit value @@ -32,8 +31,7 @@ */ #define REG_BIT8(__n) \ ((u8)(BIT(__n) + \ - BUILD_BUG_ON_ZERO(__is_constexpr(__n) && \ - ((__n) < 0 || (__n) > 7)))) + BUILD_BUG_ON_ZERO(is_const_true((__n) < 0 || (__n) > 7)))) /** * REG_GENMASK() - Prepare a continuous u32 bitmask @@ -46,9 +44,9 @@ */ #define REG_GENMASK(__high, __low) \ ((u32)(GENMASK(__high, __low) + \ - BUILD_BUG_ON_ZERO(__is_constexpr(__high) && \ - __is_constexpr(__low) && \ - ((__low) < 0 || (__high) > 31 || (__low) > (__high))))) + BUILD_BUG_ON_ZERO(is_const_true((__low) < 0 || \ + (__high) > 31 || \ + (__low) > (__high))))) /** * REG_GENMASK64() - Prepare a continuous u64 bitmask @@ -61,9 +59,9 @@ */ #define REG_GENMASK64(__high, __low) \ ((u64)(GENMASK_ULL(__high, __low) + \ - BUILD_BUG_ON_ZERO(__is_constexpr(__high) && \ - __is_constexpr(__low) && \ - ((__low) < 0 || (__high) > 63 || (__low) > (__high))))) + BUILD_BUG_ON_ZERO(is_const_true((__low) < 0 || \ + (__high) > 63 || \ + (__low) > (__high))))) /** * REG_GENMASK8() - Prepare a continuous u8 bitmask @@ -76,9 +74,9 @@ */ #define REG_GENMASK8(__high, __low) \ ((u8)(GENMASK(__high, __low) + \ - BUILD_BUG_ON_ZERO(__is_constexpr(__high) && \ - __is_constexpr(__low) && \ - ((__low) < 0 || (__high) > 7 || (__low) > (__high))))) + BUILD_BUG_ON_ZERO(is_const_true((__low) < 0 || \ + (__high) > 7 || \ + (__low) > (__high))))) /* * Local integer constant expression version of is_power_of_2(). @@ -97,10 +95,10 @@ */ #define REG_FIELD_PREP(__mask, __val) \ ((u32)((((typeof(__mask))(__val) << __bf_shf(__mask)) & (__mask)) + \ - BUILD_BUG_ON_ZERO(!__is_constexpr(__mask)) + \ + BUILD_BUG_ON_ZERO(!is_const(__mask)) + \ BUILD_BUG_ON_ZERO((__mask) == 0 || (__mask) > U32_MAX) + \ BUILD_BUG_ON_ZERO(!IS_POWER_OF_2((__mask) + (1ULL << __bf_shf(__mask)))) + \ - BUILD_BUG_ON_ZERO(__builtin_choose_expr(__is_constexpr(__val), (~((__mask) >> __bf_shf(__mask)) & (__val)), 0)))) + BUILD_BUG_ON_ZERO(is_const_true(~((__mask) >> __bf_shf(__mask)) & (__val))))) /** * REG_FIELD_PREP8() - Prepare a u8 bitfield value @@ -114,10 +112,10 @@ */ #define REG_FIELD_PREP8(__mask, __val) \ ((u8)((((typeof(__mask))(__val) << __bf_shf(__mask)) & (__mask)) + \ - BUILD_BUG_ON_ZERO(!__is_constexpr(__mask)) + \ + BUILD_BUG_ON_ZERO(!is_const(__mask)) + \ BUILD_BUG_ON_ZERO((__mask) == 0 || (__mask) > U8_MAX) + \ BUILD_BUG_ON_ZERO(!IS_POWER_OF_2((__mask) + (1ULL << __bf_shf(__mask)))) + \ - BUILD_BUG_ON_ZERO(__builtin_choose_expr(__is_constexpr(__val), (~((__mask) >> __bf_shf(__mask)) & (__val)), 0)))) + BUILD_BUG_ON_ZERO(is_const_true(~((__mask) >> __bf_shf(__mask)) & (__val))))) /** * REG_FIELD_GET() - Extract a u32 bitfield value @@ -154,8 +152,7 @@ */ #define REG_BIT16(__n) \ ((u16)(BIT(__n) + \ - BUILD_BUG_ON_ZERO(__is_constexpr(__n) && \ - ((__n) < 0 || (__n) > 15)))) + BUILD_BUG_ON_ZERO(is_const_true((__n) < 0 || (__n) > 15)))) /** * REG_GENMASK16() - Prepare a continuous u8 bitmask @@ -169,9 +166,9 @@ */ #define REG_GENMASK16(__high, __low) \ ((u16)(GENMASK(__high, __low) + \ - BUILD_BUG_ON_ZERO(__is_constexpr(__high) && \ - __is_constexpr(__low) && \ - ((__low) < 0 || (__high) > 15 || (__low) > (__high))))) + BUILD_BUG_ON_ZERO(is_const_true((__low) < 0 || \ + (__high) > 15 || \ + (__low) > (__high))))) /** * REG_FIELD_PREP16() - Prepare a u16 bitfield value @@ -186,10 +183,10 @@ */ #define REG_FIELD_PREP16(__mask, __val) \ ((u16)((((typeof(__mask))(__val) << __bf_shf(__mask)) & (__mask)) + \ - BUILD_BUG_ON_ZERO(!__is_constexpr(__mask)) + \ + BUILD_BUG_ON_ZERO(!is_const(__mask)) + \ BUILD_BUG_ON_ZERO((__mask) == 0 || (__mask) > U16_MAX) + \ BUILD_BUG_ON_ZERO(!IS_POWER_OF_2((__mask) + (1ULL << __bf_shf(__mask)))) + \ - BUILD_BUG_ON_ZERO(__builtin_choose_expr(__is_constexpr(__val), (~((__mask) >> __bf_shf(__mask)) & (__val)), 0)))) + BUILD_BUG_ON_ZERO(is_const_true(~((__mask) >> __bf_shf(__mask)) & (__val))))) #define __MASKED_FIELD(mask, value) ((mask) << 16 | (value)) #define _MASKED_FIELD(mask, value) ({ \ @@ -237,7 +234,7 @@ * ... */ #define _PICK_EVEN_2RANGES(__index, __c_index, __a, __b, __c, __d) \ - (BUILD_BUG_ON_ZERO(!__is_constexpr(__c_index)) + \ + (BUILD_BUG_ON_ZERO(!is_const(__c_index)) + \ ((__index) < (__c_index) ? _PICK_EVEN(__index, __a, __b) : \ _PICK_EVEN((__index) - (__c_index), __c, __d))) From patchwork Mon Dec 2 17:33:31 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Vincent Mailhol via B4 Relay X-Patchwork-Id: 13891146 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 5420B1B07AE; Mon, 2 Dec 2024 17:35:05 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733160905; cv=none; b=HIkmDi/7MXgDiT/fJpwFO57DNq65fm4/rhrf5/+N9cfMtrN6inmNfQ558FU+odDZBgAH8N519y3FLD+99isD0JpGUE8AzjPn3bsln2fBp8AVriBheTJIlXk+JC4NiG6ic4UhxbAkLvi9OXUVN6GSMbJU0+xp4Yzu/NrUMDVdu+c= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733160905; c=relaxed/simple; bh=yr8nPgorqsKZslW18S7DrfbJzdSBjOy5Dm3FgmZaCBU=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=Kcg11sOe3MO5XqAGKUblfAxwMZDMKwxCjyxbiV2pKZMoypqZrk5I8mLPRlO5i85d1HXviQnFyuWqmkPruUpSX/WXp8TRBqHcczi1w4NZZbrcJmqnV09COkpDMo2etFAKfX4nqMHnD7src/ISlNdsthp8MGqIEGbvack5ouYe6DM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=FXO4sW7K; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="FXO4sW7K" Received: by smtp.kernel.org (Postfix) with ESMTPS id D6DCAC4CEE1; Mon, 2 Dec 2024 17:35:04 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1733160904; bh=yr8nPgorqsKZslW18S7DrfbJzdSBjOy5Dm3FgmZaCBU=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To:From; b=FXO4sW7K+gc2vwM1se4GLS2jISLC4Hv85218fTBlxgqHaepVn0KVqtw5xphianfNI h+wVBnQYK0tMREorbt8f9+hmHMOkOSVx+q+E2s5zbRrnGuoCllEFGiWbUwo6Fjllmy fCUyb8OgeS7twO7222857W4QWof1k1Sa8HgeTQwrLcD1Ei793+c8JCrdZ5RXLygiws ZTk+5w6Tx5013RuYIOLIHRFEV6bNFL/FTUUdlecC4pe15aPkt4CYE2ksT3Nnc0r5qj N5aHHomI55Mhxi7/41e44zKJc3dncVCnCU4vbjgHyJ8SVCvF77uaHJSsGp7/RVdgkQ d6o3sa2MQfa2Q== Received: from aws-us-west-2-korg-lkml-1.web.codeaurora.org (localhost.localdomain [127.0.0.1]) by smtp.lore.kernel.org (Postfix) with ESMTP id C3BE6D7833C; Mon, 2 Dec 2024 17:35:04 +0000 (UTC) From: Vincent Mailhol via B4 Relay Date: Tue, 03 Dec 2024 02:33:31 +0900 Subject: [PATCH 09/10] coresight: etm4x: replace __is_const_expr() by is_const() Precedence: bulk X-Mailing-List: linux-hardening@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20241203-is_constexpr-refactor-v1-9-4e4cbaecc216@wanadoo.fr> References: <20241203-is_constexpr-refactor-v1-0-4e4cbaecc216@wanadoo.fr> In-Reply-To: <20241203-is_constexpr-refactor-v1-0-4e4cbaecc216@wanadoo.fr> To: Linus Torvalds , David Laight , Luc Van Oostenryck , Nathan Chancellor , Nick Desaulniers , Bill Wendling , Justin Stitt , Yury Norov , Rasmus Villemoes , Kees Cook , "Gustavo A. R. Silva" , Jani Nikula , Joonas Lahtinen , Rodrigo Vivi , Tvrtko Ursulin , David Airlie , Simona Vetter , Suzuki K Poulose , Mike Leach , James Clark , Alexander Shishkin , Rikard Falkeborn , Martin Uecker Cc: linux-sparse@vger.kernel.org, linux-kernel@vger.kernel.org, llvm@lists.linux.dev, linux-hardening@vger.kernel.org, intel-gfx@lists.freedesktop.org, dri-devel@lists.freedesktop.org, coresight@lists.linaro.org, linux-arm-kernel@lists.infradead.org, Vincent Mailhol X-Mailer: b4 0.14.2 X-Developer-Signature: v=1; a=openpgp-sha256; l=1152; i=mailhol.vincent@wanadoo.fr; h=from:subject:message-id; bh=2a9e0Sg1+HnRtDgEsb84OXVQoFxUHpuXTO0qI7avR5g=; b=owGbwMvMwCV2McXO4Xp97WbG02pJDOm+749O6FPas2ybatTeWc7prF16x3VX6XbM9TgktOrU7 v8zpTx3dZSyMIhxMciKKbIsK+fkVugo9A479NcSZg4rE8gQBi5OAZjIEUmGf4rH5jmVbpEP/Ddj A+/Sm/sTG/nmCVoanpxpWRL3vYGNfyIjQ/eqI0k+TNe37ZmStZpB6ZREi/BkBbfsadXL0zZxGHr +5wYA X-Developer-Key: i=mailhol.vincent@wanadoo.fr; a=openpgp; fpr=ED8F700574E67F20E574E8E2AB5FEB886DBB99C2 X-Endpoint-Received: by B4 Relay for mailhol.vincent@wanadoo.fr/default with auth_id=291 X-Original-From: Vincent Mailhol Reply-To: mailhol.vincent@wanadoo.fr From: Vincent Mailhol is_const() is a one to one replacement of __is_constexpr(). Do the replacement so that __is_constexpr() can be removed. Refer to [1] for an explaination of why __builtin_constant_p() can not be used as a replacement here. [1] commit 4d45bc82df66 ("coresight: etm4x: avoid build failure with unrolled loops") Link: https://git.kernel.org/torvalds/c/4d45bc82df66 Signed-off-by: Vincent Mailhol --- drivers/hwtracing/coresight/coresight-etm4x.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/drivers/hwtracing/coresight/coresight-etm4x.h b/drivers/hwtracing/coresight/coresight-etm4x.h index 9e9165f62e81fe5a87d35b4e30bc23f93cb211ec..91cf81ac46f44418f76783c102477d08f9abc70f 100644 --- a/drivers/hwtracing/coresight/coresight-etm4x.h +++ b/drivers/hwtracing/coresight/coresight-etm4x.h @@ -492,7 +492,7 @@ ({ \ u64 __val; \ \ - if (__is_constexpr((offset))) \ + if (is_const(offset)) \ __val = read_etm4x_sysreg_const_offset((offset)); \ else \ __val = etm4x_sysreg_read((offset), true, (_64bit)); \ From patchwork Mon Dec 2 17:33:32 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Vincent Mailhol via B4 Relay X-Patchwork-Id: 13891147 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 621B11DDC28; Mon, 2 Dec 2024 17:35:05 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733160905; cv=none; b=i0OVKdbAwLnWB74D244LdFP6vzwPaemWCm2mEKGS19+3GbXIhiLpmKKe2k+R9kgTBsCgJK3WVOJyOrxn0Z3hyFNotMrvcXIWNAnMk++P2ZkdEejSmFkUDwT8hOMeBHJ/b1RMYOqxbcSpNa/MHV8gWybMJin3ZBvyNJN9wcASFUM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733160905; c=relaxed/simple; bh=uAZMv8YsD+O3wlShIE66KqQtYXWRRt5F/8DqFrLry7o=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=WYyBZRxQ3QlyoZtQmFc/K3S6ugFKs9ELIrYZ2MqnJJke3W55P8AGfdSGsjzRZWQllYcJSRMsim4NrCHsCvzy/nngLH0ydzdhr5NagfgiSOAmiRu2pvVfD6tJ11MjEcZq7cUTmFRyLTvUcVxqrdjrCYQwdm/XQCmAz/Cf/hJ15ME= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=bnpeLmKT; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="bnpeLmKT" Received: by smtp.kernel.org (Postfix) with ESMTPS id EE828C4AF0C; Mon, 2 Dec 2024 17:35:04 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1733160905; bh=uAZMv8YsD+O3wlShIE66KqQtYXWRRt5F/8DqFrLry7o=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To:From; b=bnpeLmKTZ0P/1nIlgfq6P/89fGBp79XUw1RuAsTRlXRHCp2izeFvyG+TI1GSO6Kkj +vSOTNpWal9tOo9dbUTbh9ga3NCi42/NymrTLb22Q4h+u/cokTPsWtrlid/tRFM1T1 6dV8OjkixQG4eXBo6SkrUV3AeE/R4D8dTZFDlu9F64ELfy0Kyibo9a/OngDbjQ2PZS fRCmDaWTk6tZnYjK8xTrQZoanmydKBBAOMVkExgd7RD1+VOe0fYp5Yw3cTebcM93v7 5dsvx+CeiiRXTK43GkDDCdvLdYPnost8GIN8ypR562MwwNxO1YQn1QxI+326LAvREB 1xToeg5fGWCxA== Received: from aws-us-west-2-korg-lkml-1.web.codeaurora.org (localhost.localdomain [127.0.0.1]) by smtp.lore.kernel.org (Postfix) with ESMTP id E082BD7833D; Mon, 2 Dec 2024 17:35:04 +0000 (UTC) From: Vincent Mailhol via B4 Relay Date: Tue, 03 Dec 2024 02:33:32 +0900 Subject: [PATCH 10/10] compiler.h: remove __is_constexpr() Precedence: bulk X-Mailing-List: linux-hardening@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20241203-is_constexpr-refactor-v1-10-4e4cbaecc216@wanadoo.fr> References: <20241203-is_constexpr-refactor-v1-0-4e4cbaecc216@wanadoo.fr> In-Reply-To: <20241203-is_constexpr-refactor-v1-0-4e4cbaecc216@wanadoo.fr> To: Linus Torvalds , David Laight , Luc Van Oostenryck , Nathan Chancellor , Nick Desaulniers , Bill Wendling , Justin Stitt , Yury Norov , Rasmus Villemoes , Kees Cook , "Gustavo A. R. Silva" , Jani Nikula , Joonas Lahtinen , Rodrigo Vivi , Tvrtko Ursulin , David Airlie , Simona Vetter , Suzuki K Poulose , Mike Leach , James Clark , Alexander Shishkin , Rikard Falkeborn , Martin Uecker Cc: linux-sparse@vger.kernel.org, linux-kernel@vger.kernel.org, llvm@lists.linux.dev, linux-hardening@vger.kernel.org, intel-gfx@lists.freedesktop.org, dri-devel@lists.freedesktop.org, coresight@lists.linaro.org, linux-arm-kernel@lists.infradead.org, Vincent Mailhol X-Mailer: b4 0.14.2 X-Developer-Signature: v=1; a=openpgp-sha256; l=3686; i=mailhol.vincent@wanadoo.fr; h=from:subject:message-id; bh=KhSEJ/z9NSPJazKznaA4eGwJC3/rNJqN3dS4dHFF/N0=; b=owGbwMvMwCV2McXO4Xp97WbG02pJDOm+7485PP/cFjpv4ddvR2VLOPkvvJfXCD3qLZ3Eld3nt LjUeklRRykLgxgXg6yYIsuyck5uhY5C77BDfy1h5rAygQxh4OIUgImY+zH8T7uwUoiP88gRLpWl X/NO/Lygvi6i7yFjyKWdpdGpMdvv/2P4H5bWr3/0ydJ1OX+dF13m2GEVIfQr88mHOomEbc+bzFv kGAE= X-Developer-Key: i=mailhol.vincent@wanadoo.fr; a=openpgp; fpr=ED8F700574E67F20E574E8E2AB5FEB886DBB99C2 X-Endpoint-Received: by B4 Relay for mailhol.vincent@wanadoo.fr/default with auth_id=291 X-Original-From: Vincent Mailhol Reply-To: mailhol.vincent@wanadoo.fr From: Vincent Mailhol Now that all the users of __is_constexpr() have been migrated to is_const() or one of its friends, remove it. The homage to Martin Uecker's genius hack remains in the documentation of __is_const_zero(). Signed-off-by: Vincent Mailhol --- include/linux/compiler.h | 47 ----------------------------------------------- 1 file changed, 47 deletions(-) diff --git a/include/linux/compiler.h b/include/linux/compiler.h index 165aa5b9bc484376087a130a1ac1f3edb50c983d..7ba75044828129cf1f81f458ade695786dbf132a 100644 --- a/include/linux/compiler.h +++ b/include/linux/compiler.h @@ -252,53 +252,6 @@ static inline void *offset_to_ptr(const int *off) #define __must_be_cstr(p) \ __BUILD_BUG_ON_ZERO_MSG(__annotated(p, nonstring), "must be cstr (NUL-terminated)") -/* - * This returns a constant expression while determining if an argument is - * a constant expression, most importantly without evaluating the argument. - * Glory to Martin Uecker - * - * Details: - * - sizeof() return an integer constant expression, and does not evaluate - * the value of its operand; it only examines the type of its operand. - * - The results of comparing two integer constant expressions is also - * an integer constant expression. - * - The first literal "8" isn't important. It could be any literal value. - * - The second literal "8" is to avoid warnings about unaligned pointers; - * this could otherwise just be "1". - * - (long)(x) is used to avoid warnings about 64-bit types on 32-bit - * architectures. - * - The C Standard defines "null pointer constant", "(void *)0", as - * distinct from other void pointers. - * - If (x) is an integer constant expression, then the "* 0l" resolves - * it into an integer constant expression of value 0. Since it is cast to - * "void *", this makes the second operand a null pointer constant. - * - If (x) is not an integer constant expression, then the second operand - * resolves to a void pointer (but not a null pointer constant: the value - * is not an integer constant 0). - * - The conditional operator's third operand, "(int *)8", is an object - * pointer (to type "int"). - * - The behavior (including the return type) of the conditional operator - * ("operand1 ? operand2 : operand3") depends on the kind of expressions - * given for the second and third operands. This is the central mechanism - * of the macro: - * - When one operand is a null pointer constant (i.e. when x is an integer - * constant expression) and the other is an object pointer (i.e. our - * third operand), the conditional operator returns the type of the - * object pointer operand (i.e. "int *"). Here, within the sizeof(), we - * would then get: - * sizeof(*((int *)(...)) == sizeof(int) == 4 - * - When one operand is a void pointer (i.e. when x is not an integer - * constant expression) and the other is an object pointer (i.e. our - * third operand), the conditional operator returns a "void *" type. - * Here, within the sizeof(), we would then get: - * sizeof(*((void *)(...)) == sizeof(void) == 1 - * - The equality comparison to "sizeof(int)" therefore depends on (x): - * sizeof(int) == sizeof(int) (x) was a constant expression - * sizeof(int) != sizeof(void) (x) was not a constant expression - */ -#define __is_constexpr(x) \ - (sizeof(int) == sizeof(*(8 ? ((void *)((long)(x) * 0l)) : (int *)8))) - /* * Whether 'type' is a signed type or an unsigned type. Supports scalar types, * bool and also pointer types.