From patchwork Tue Nov 12 13:59:23 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Vincent Mailhol X-Patchwork-Id: 13872292 Received: from mail-pf1-f182.google.com (mail-pf1-f182.google.com [209.85.210.182]) (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 D794B1FBF62; Tue, 12 Nov 2024 14:05:10 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.182 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731420312; cv=none; b=SGGhJsfBgXPY48LxQhe2xv7xZxY45IFSqLfNG244jMdTxwpvi02KTct1uOufc3bSEmo9Ye302Y4sLcawnBdkOMc1mrtt86nBc48IJeV/GUQ5b9TGRGn7joM4sagh2INKOcgM98yD4rRQaXohOD9S0heTT//uFyPH6P1RST9YGQk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731420312; c=relaxed/simple; bh=E+yhVoHUHrTpPileTWne1ORFEvQ8LJtTJNjzT3bPXXE=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=j+aL+dUiHmYZXk2YxP0y4U2YG4IZelBWXft5BJTMgAFaNCp3D0YPKJIDBFsGkunhcoA1Jgs4G4vx0qnKpbflCOIn+btSjD9kRC/kB+Q+fdjnJRTFwDp4eWLdcc+x9lhSLo0rvwE8Ss6HYINyhA8tL3qsO63N/3f8NWeQ23/6s94= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=fail (p=quarantine dis=none) header.from=wanadoo.fr; spf=pass smtp.mailfrom=gmail.com; arc=none smtp.client-ip=209.85.210.182 Authentication-Results: smtp.subspace.kernel.org; dmarc=fail (p=quarantine dis=none) header.from=wanadoo.fr Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Received: by mail-pf1-f182.google.com with SMTP id d2e1a72fcca58-7242f559a9fso2857012b3a.1; Tue, 12 Nov 2024 06:05:10 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1731420310; x=1732025110; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=W0iNeks1Xj+YevyLTEj6OBBzVntidHMn90B9+6RuYRs=; b=mhcuZcBvk7zTSJX5fb+av9c0CMZcx78fkq/E/C8r7thedxSC4KiO8ELl0wpP+MB79o yXxdztOhpe9qbaDs5TTnwfH+D+/9ZAtYRlhFcqBGy64NfL7AXXuNS2o+gJqa8ZHI7lMo rNx2JYun9OGQy6mB9jfnbKZcZTQi2BefWNz4+DIaPQb+LZyUKVRpdy27ry1Sj2tuTws0 hJlySxZVCIpxlfK9ZywflwbjxiaiCHeGVrvy6qn9ioy+GaJYqfRFz3d3pDMwH2Nu4hsb TszaXJePVbD98GTWYvdX6HbvK8sDjIRQzXrwvLMBpmnG5T10T6VMRyZYqawPMFtMLTJr 2lIA== X-Forwarded-Encrypted: i=1; AJvYcCUuPjIwA7jItng6uvirQeHBqdW6JEfOLO8cWblcB1kH9WmG8xPKskAHk74KikJ5eQKeyE5g98Ja0ehVgKQ=@vger.kernel.org X-Gm-Message-State: AOJu0YwlPR1Ruz0bxW/Bavq/sGM8zcFewxJl4shxw0t/1QszyrDh0scO PqGHwpJQDSaersP8bZUiY/C8UJYhVyn2m5clZdP3lfYvRU/2DTyV X-Google-Smtp-Source: AGHT+IGkWb6iO4CGt8QCeeIcGW6dDLF/A6x1hKkDJIKvjDH+k5ImbLiEqGHdTyBB0OMeN3Obr58WMQ== X-Received: by 2002:a05:6a20:8421:b0:1d9:c6e8:60fc with SMTP id adf61e73a8af0-1dc228931b2mr25103396637.6.1731420310186; Tue, 12 Nov 2024 06:05:10 -0800 (PST) Received: from localhost.localdomain (124x33x176x97.ap124.ftth.ucom.ne.jp. [124.33.176.97]) by smtp.gmail.com with ESMTPSA id 41be03b00d2f7-7f41f644a23sm10480007a12.53.2024.11.12.06.05.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 12 Nov 2024 06:05:09 -0800 (PST) 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: [PATCH v3 1/2] compiler.h: add _static_assert() Date: Tue, 12 Nov 2024 22:59:23 +0900 Message-ID: <20241112140454.518823-5-mailhol.vincent@wanadoo.fr> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20241112140454.518823-4-mailhol.vincent@wanadoo.fr> References: <20241112140454.518823-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=3513; i=mailhol.vincent@wanadoo.fr; h=from:subject; bh=E+yhVoHUHrTpPileTWne1ORFEvQ8LJtTJNjzT3bPXXE=; b=owGbwMvMwCV2McXO4Xp97WbG02pJDOnGCd0X2o8us1M5wBUVUxZ1QNHGZ3vbhBXuStnOBg/jO l5zfbjfUcrCIMbFICumyLKsnJNboaPQO+zQX0uYOaxMIEMYuDgFYCIF2owMS92mdHp2hCiwrCx/ wvk6wtn/0DHOvL2/DsXucb7xaeXxUwz/vRwU7s7bspV55ooA/ahO5/J10eIddj8ff16S+vXiLol THAA= 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 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]; } For the same reasons why __is_constexpr() was created to remediate __builtin_constant_p() edge cases, __is_constexpr() can be used to resolve statically_true()'s 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 examples. Declare a new _statically_true() macro which, by making use of the __builtin_choose_expr() and __is_constexpr() 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 returns false. For this reason, usage of _statically_true() should be 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) in godbolt: https://godbolt.org/z/GYeEK5d7s - that proof, in godbolt, that statically_true() is bettera at 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 subcomponents 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 13:59:24 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Vincent Mailhol X-Patchwork-Id: 13872293 Received: from mail-pl1-f176.google.com (mail-pl1-f176.google.com [209.85.214.176]) (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 8FDB91FCC55; Tue, 12 Nov 2024 14:05:13 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.176 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731420315; cv=none; b=EiVisdFXJPwYjEZJOZVjdt4OHb8xifGz3rA4qUvr9sDUrIULu9suIu+CbGGMke7fLnlyJx0yL/TDoYnhyhWrSCgNvC3A/JSGHP7b70mLJNJfWzahKi5wy4cC5yEg9V6or2E4iCRxK52fUrTFUIJUEZvvXkMboU94uL6Y+v+3lLo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731420315; c=relaxed/simple; bh=SrJFRuhlzoZ8GAgh2rIJFuamp5YX3FbowBkwkQ/Ewdc=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=N/bigGDoVNVNrUXeGjS/3XW9KIpGSmET3fagzyzyVMbUaYzKyKhp6wYvT+/uKogsJgTvXhfHYshDBjwMbYROElizApnbvKGyxkMgY7Sq0xFZEWZOM5e6071TQvRm2xBZMaddIQeIXQJetk4Xaqcf2lVLeRBAQhaZVyXpMipgAIw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=fail (p=quarantine dis=none) header.from=wanadoo.fr; spf=pass smtp.mailfrom=gmail.com; arc=none smtp.client-ip=209.85.214.176 Authentication-Results: smtp.subspace.kernel.org; dmarc=fail (p=quarantine dis=none) header.from=wanadoo.fr Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Received: by mail-pl1-f176.google.com with SMTP id d9443c01a7336-20cb47387ceso58682055ad.1; Tue, 12 Nov 2024 06:05:13 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1731420313; x=1732025113; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=iLgkTjb8Qax6qcLVJZ4OagY9T3nwD0WZDNygw1a2Krs=; b=XuxqLi8zSVn2mvN9ETYpig1fxvxYu/xa50WQaiZgMk4KpYW+Z/OXYpHYuyvIFP2WKH uvd+MSamKr9JaMAXzNWrlctOxDpXO5/CkrGKrf8kzIYik371oY1U1xCqpONOUZFUdtox uAC6YzhGRkXYosKFL6fd8xG+Y3JoTVUGWpwVeIaxXo/4aIcEUz091b8+VjvZFmazpoj9 Ov8HRg6Zeb3hvezSuup9kpUD3UhjrN7Ih8q7omyXIJx2pjATmP7Wj96hZFOJ68QJyr8N oZZQsim9u4TpxUbFKOvDrDfsAmAm6f3f5NTz3ZFkTzeaDbNfGpiQVQmZAZJvsMqFxpJm 3SfQ== X-Forwarded-Encrypted: i=1; AJvYcCX9DzG18godwmIU3LxXEpUFChcn+CBoNB8dV9UL9vUIiy3j9ewpBnrHciVCUtPM8sFoczHFIX5iThEYhKs=@vger.kernel.org X-Gm-Message-State: AOJu0Yw5izl7vEJ/yki71Bwy1ZwDEqyPUlwhfbSs71sgjlhtHHousI5c 6LSlFPj4cbd/uHqnp9pWXWu2/LDExPB65o4OejIwqD8xrkLxR4PA X-Google-Smtp-Source: AGHT+IHAyO2QTUUO0HaKBXoybzM/XHVUDbmdQqcbH3AkbpQT70i1dhQgvuaRFWMkaVeJcpDf3iHTeQ== X-Received: by 2002:a17:902:e752:b0:20c:8907:908 with SMTP id d9443c01a7336-2118359c216mr209080545ad.44.1731420312602; Tue, 12 Nov 2024 06:05:12 -0800 (PST) Received: from localhost.localdomain (124x33x176x97.ap124.ftth.ucom.ne.jp. [124.33.176.97]) by smtp.gmail.com with ESMTPSA id 41be03b00d2f7-7f41f644a23sm10480007a12.53.2024.11.12.06.05.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 12 Nov 2024 06:05:12 -0800 (PST) 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: [PATCH v3 2/2] linux/bits.h: simplify GENMASK_INPUT_CHECK() Date: Tue, 12 Nov 2024 22:59:24 +0900 Message-ID: <20241112140454.518823-6-mailhol.vincent@wanadoo.fr> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20241112140454.518823-4-mailhol.vincent@wanadoo.fr> References: <20241112140454.518823-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=1232; i=mailhol.vincent@wanadoo.fr; h=from:subject; bh=SrJFRuhlzoZ8GAgh2rIJFuamp5YX3FbowBkwkQ/Ewdc=; b=owGbwMvMwCV2McXO4Xp97WbG02pJDOnGCd13Oq9q39+g/TFD9ZLI4uA77k9nnX63eeHV91fvm Z35tfiKXUcpC4MYF4OsmCLLsnJOboWOQu+wQ38tYeawMoEMYeDiFICJGFky/DNYN931SmLzZYO8 7ftfyi801y0NXpJU/euYTJgLV/yki9MZ/vDO3ztR2/dlyOeiZYcbd3+Oq/p06Gn11mPKR7WmLmT ZKcMAAA== 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 --- 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,