public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Yury Norov <ynorov@nvidia.com>
To: david.laight.linux@gmail.com
Cc: Nathan Chancellor <nathan@kernel.org>,
	Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
	Thomas Gleixner <tglx@linutronix.de>,
	Peter Zijlstra <peterz@infradead.org>,
	Ingo Molnar <mingo@kernel.org>,
	Mathieu Desnoyers <mathieu.desnoyers@efficios.com>,
	Arnd Bergmann <arnd@arndb.de>,
	linux-arch@vger.kernel.org, linux-kernel@vger.kernel.org,
	Yury Norov <yury.norov@gmail.com>,
	Lucas De Marchi <lucas.demarchi@intel.com>,
	Jani Nikula <jani.nikula@intel.com>,
	Vincent Mailhol <mailhol.vincent@wanadoo.fr>,
	Andy Shevchenko <andriy.shevchenko@linux.intel.com>,
	Kees Cook <keescook@chromium.org>,
	Andrew Morton <akpm@linux-foundation.org>
Subject: Re: [PATCH next 13/14] test_bits: Change all the tests to be compile-time tests
Date: Sat, 7 Feb 2026 23:37:49 -0500	[thread overview]
Message-ID: <aYgTHfhxDws-D6yl@yury> (raw)
In-Reply-To: <20260121145731.3623-14-david.laight.linux@gmail.com>

On Wed, Jan 21, 2026 at 02:57:30PM +0000, david.laight.linux@gmail.com wrote:
> From: David Laight <david.laight.linux@gmail.com>
> 
> Since all the GENMASK() values are compile-time constants they can
> be tested with BUILD_BUG_ON() rather than KUNIT_EXPECT_EQ().
> 
> Signed-off-by: David Laight <david.laight.linux@gmail.com>

I thought, KUNIT invented this EXPECT_EQ macro for a nice printing
and some accounting. If you want to make sure that __GENMASK() is a
compile-time expression, it's OK. But I'd rather keep the existing
KUNIT_EXPECT_EQ() and add

        BUILD_BUG_ON(!__builtin_constant_p(GENMASK()))

next to that. This is actually how test_bitmap_const_eval() works.

Thanks,
Yury

> ---
>  lib/tests/test_bits.c | 90 +++++++++++++++++++++----------------------
>  1 file changed, 45 insertions(+), 45 deletions(-)
> 
> diff --git a/lib/tests/test_bits.c b/lib/tests/test_bits.c
> index 36eb4661e78b..4d3a895f490c 100644
> --- a/lib/tests/test_bits.c
> +++ b/lib/tests/test_bits.c
> @@ -32,30 +32,30 @@ static_assert(assert_type(u64, GENMASK_U64(63, 0)) == U64_MAX);
>  
>  static void __genmask_test(struct kunit *test)
>  {
> -	KUNIT_EXPECT_EQ(test, 1ul, __GENMASK(0, 0));
> -	KUNIT_EXPECT_EQ(test, 3ul, __GENMASK(1, 0));
> -	KUNIT_EXPECT_EQ(test, 6ul, __GENMASK(2, 1));
> -	KUNIT_EXPECT_EQ(test, 0xFFFFFFFFul, __GENMASK(31, 0));
> +	BUILD_BUG_ON(__GENMASK(0, 0) != 1ul);
> +	BUILD_BUG_ON(__GENMASK(1, 0) != 3ul);
> +	BUILD_BUG_ON(__GENMASK(2, 1) != 6ul);
> +	BUILD_BUG_ON(__GENMASK(31, 0) != 0xFFFFFFFFul);
>  }
>  
>  static void __genmask_ull_test(struct kunit *test)
>  {
> -	KUNIT_EXPECT_EQ(test, 1ull, __GENMASK_ULL(0, 0));
> -	KUNIT_EXPECT_EQ(test, 3ull, __GENMASK_ULL(1, 0));
> -	KUNIT_EXPECT_EQ(test, 0x000000ffffe00000ull, __GENMASK_ULL(39, 21));
> -	KUNIT_EXPECT_EQ(test, 0xffffffffffffffffull, __GENMASK_ULL(63, 0));
> +	BUILD_BUG_ON(__GENMASK_ULL(0, 0) != 1ull);
> +	BUILD_BUG_ON(__GENMASK_ULL(1, 0) != 3ull);
> +	BUILD_BUG_ON(__GENMASK_ULL(39, 21) != 0x000000ffffe00000ull);
> +	BUILD_BUG_ON(__GENMASK_ULL(63, 0) != 0xffffffffffffffffull);
>  }
>  
>  static void genmask_test(struct kunit *test)
>  {
> -	KUNIT_EXPECT_EQ(test, 1ul, GENMASK(0, 0));
> -	KUNIT_EXPECT_EQ(test, 3ul, GENMASK(1, 0));
> -	KUNIT_EXPECT_EQ(test, 6ul, GENMASK(2, 1));
> -	KUNIT_EXPECT_EQ(test, 0xFFFFFFFFul, GENMASK(31, 0));
> +	BUILD_BUG_ON(GENMASK(0, 0) != 1ul);
> +	BUILD_BUG_ON(GENMASK(1, 0) != 3ul);
> +	BUILD_BUG_ON(GENMASK(2, 1) != 6ul);
> +	BUILD_BUG_ON(GENMASK(31, 0) != 0xFFFFFFFFul);
>  
> -	KUNIT_EXPECT_EQ(test, 1u, GENMASK_U8(0, 0));
> -	KUNIT_EXPECT_EQ(test, 3u, GENMASK_U16(1, 0));
> -	KUNIT_EXPECT_EQ(test, 0x10000, GENMASK_U32(16, 16));
> +	BUILD_BUG_ON(GENMASK_U8(0, 0) != 1u);
> +	BUILD_BUG_ON(GENMASK_U16(1, 0) != 3u);
> +	BUILD_BUG_ON(GENMASK_U32(16, 16) != 0x10000);
>  
>  #ifdef TEST_GENMASK_FAILURES
>  	/* these should fail compilation */
> @@ -75,10 +75,10 @@ static void genmask_test(struct kunit *test)
>  
>  static void genmask_ull_test(struct kunit *test)
>  {
> -	KUNIT_EXPECT_EQ(test, 1ull, GENMASK_ULL(0, 0));
> -	KUNIT_EXPECT_EQ(test, 3ull, GENMASK_ULL(1, 0));
> -	KUNIT_EXPECT_EQ(test, 0x000000ffffe00000ull, GENMASK_ULL(39, 21));
> -	KUNIT_EXPECT_EQ(test, 0xffffffffffffffffull, GENMASK_ULL(63, 0));
> +	BUILD_BUG_ON(GENMASK_ULL(0, 0) != 1ull);
> +	BUILD_BUG_ON(GENMASK_ULL(1, 0) != 3ull);
> +	BUILD_BUG_ON(GENMASK_ULL(39, 21) != 0x000000ffffe00000ull);
> +	BUILD_BUG_ON(GENMASK_ULL(63, 0) != 0xffffffffffffffffull);
>  
>  #ifdef TEST_GENMASK_FAILURES
>  	/* these should fail compilation */
> @@ -92,23 +92,23 @@ static void genmask_u128_test(struct kunit *test)
>  {
>  #ifdef CONFIG_ARCH_SUPPORTS_INT128
>  	/* Below 64 bit masks */
> -	KUNIT_EXPECT_EQ(test, 0x0000000000000001ull, GENMASK_U128(0, 0));
> -	KUNIT_EXPECT_EQ(test, 0x0000000000000003ull, GENMASK_U128(1, 0));
> -	KUNIT_EXPECT_EQ(test, 0x0000000000000006ull, GENMASK_U128(2, 1));
> -	KUNIT_EXPECT_EQ(test, 0x00000000ffffffffull, GENMASK_U128(31, 0));
> -	KUNIT_EXPECT_EQ(test, 0x000000ffffe00000ull, GENMASK_U128(39, 21));
> -	KUNIT_EXPECT_EQ(test, 0xffffffffffffffffull, GENMASK_U128(63, 0));
> +	BUILD_BUG_ON(GENMASK_U128(0, 0) != 0x0000000000000001ull);
> +	BUILD_BUG_ON(GENMASK_U128(1, 0) != 0x0000000000000003ull);
> +	BUILD_BUG_ON(GENMASK_U128(2, 1) != 0x0000000000000006ull);
> +	BUILD_BUG_ON(GENMASK_U128(31, 0) != 0x00000000ffffffffull);
> +	BUILD_BUG_ON(GENMASK_U128(39, 21) != 0x000000ffffe00000ull);
> +	BUILD_BUG_ON(GENMASK_U128(63, 0) != 0xffffffffffffffffull);
>  
>  	/* Above 64 bit masks - only 64 bit portion can be validated once */
> -	KUNIT_EXPECT_EQ(test, 0xffffffffffffffffull, GENMASK_U128(64, 0) >> 1);
> -	KUNIT_EXPECT_EQ(test, 0x00000000ffffffffull, GENMASK_U128(81, 50) >> 50);
> -	KUNIT_EXPECT_EQ(test, 0x0000000000ffffffull, GENMASK_U128(87, 64) >> 64);
> -	KUNIT_EXPECT_EQ(test, 0x0000000000ff0000ull, GENMASK_U128(87, 80) >> 64);
> -
> -	KUNIT_EXPECT_EQ(test, 0xffffffffffffffffull, GENMASK_U128(127, 0) >> 64);
> -	KUNIT_EXPECT_EQ(test, 0xffffffffffffffffull, (u64)GENMASK_U128(127, 0));
> -	KUNIT_EXPECT_EQ(test, 0x0000000000000003ull, GENMASK_U128(127, 126) >> 126);
> -	KUNIT_EXPECT_EQ(test, 0x0000000000000001ull, GENMASK_U128(127, 127) >> 127);
> +	BUILD_BUG_ON(GENMASK_U128(64, 0) >> 1 != 0xffffffffffffffffull);
> +	BUILD_BUG_ON(GENMASK_U128(81, 50) >> 50 != 0x00000000ffffffffull);
> +	BUILD_BUG_ON(GENMASK_U128(87, 64) >> 64 != 0x0000000000ffffffull);
> +	BUILD_BUG_ON(GENMASK_U128(87, 80) >> 64 != 0x0000000000ff0000ull);
> +
> +	BUILD_BUG_ON(GENMASK_U128(127, 0) >> 64 != 0xffffffffffffffffull);
> +	BUILD_BUG_ON((u64)GENMASK_U128(127, 0) != 0xffffffffffffffffull);
> +	BUILD_BUG_ON(GENMASK_U128(127, 126) >> 126 != 0x0000000000000003ull);
> +	BUILD_BUG_ON(GENMASK_U128(127, 127) >> 127 != 0x0000000000000001ull);
>  #ifdef TEST_GENMASK_FAILURES
>  	/* these should fail compilation */
>  	GENMASK_U128(0, 1);
> @@ -129,21 +129,21 @@ static void genmask_input_check_test(struct kunit *test)
>  	OPTIMIZER_HIDE_VAR(w);
>  
>  	/* Unknown input */
> -	KUNIT_EXPECT_EQ(test, 0, GENMASK_INPUT_CHECK(x, 0, 32));
> -	KUNIT_EXPECT_EQ(test, 0, GENMASK_INPUT_CHECK(0, x, 32));
> -	KUNIT_EXPECT_EQ(test, 0, GENMASK_INPUT_CHECK(x, y, 32));
> +	BUILD_BUG_ON(GENMASK_INPUT_CHECK(x, 0, 32) != 0);
> +	BUILD_BUG_ON(GENMASK_INPUT_CHECK(0, x, 32) != 0);
> +	BUILD_BUG_ON(GENMASK_INPUT_CHECK(x, y, 32) != 0);
>  
> -	KUNIT_EXPECT_EQ(test, 0, GENMASK_INPUT_CHECK(z, 0, 32));
> -	KUNIT_EXPECT_EQ(test, 0, GENMASK_INPUT_CHECK(0, z, 32));
> -	KUNIT_EXPECT_EQ(test, 0, GENMASK_INPUT_CHECK(z, w, 32));
> +	BUILD_BUG_ON(GENMASK_INPUT_CHECK(z, 0, 32) != 0);
> +	BUILD_BUG_ON(GENMASK_INPUT_CHECK(0, z, 32) != 0);
> +	BUILD_BUG_ON(GENMASK_INPUT_CHECK(z, w, 32) != 0);
>  
>  	/* Valid input */
> -	KUNIT_EXPECT_EQ(test, 0, GENMASK_INPUT_CHECK(1, 1, 32));
> -	KUNIT_EXPECT_EQ(test, 0, GENMASK_INPUT_CHECK(39, 21, 64));
> +	BUILD_BUG_ON(GENMASK_INPUT_CHECK(1, 1, 32) != 0);
> +	BUILD_BUG_ON(GENMASK_INPUT_CHECK(39, 21, 64) != 0);
>  
> -	KUNIT_EXPECT_EQ(test, 0, GENMASK_INPUT_CHECK(100, 80, 128));
> -	KUNIT_EXPECT_EQ(test, 0, GENMASK_INPUT_CHECK(110, 65, 128));
> -	KUNIT_EXPECT_EQ(test, 0, GENMASK_INPUT_CHECK(127, 0, 128));
> +	BUILD_BUG_ON(GENMASK_INPUT_CHECK(100, 80, 128) != 0);
> +	BUILD_BUG_ON(GENMASK_INPUT_CHECK(110, 65, 128) != 0);
> +	BUILD_BUG_ON(GENMASK_INPUT_CHECK(127, 0, 128) != 0);
>  }
>  
>  
> -- 
> 2.39.5

  reply	other threads:[~2026-02-08  4:39 UTC|newest]

Thread overview: 56+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-01-21 14:57 [PATCH next 00/14] bits: De-bloat expansion of GENMASK() david.laight.linux
2026-01-21 14:57 ` [PATCH next 01/14] overflow: Reduce expansion of __type_max() david.laight.linux
2026-01-21 20:59   ` Kees Cook
2026-02-02 16:45   ` Yury Norov
2026-01-21 14:57 ` [PATCH next 02/14] kbuild: Add W=c for additional compile time checks david.laight.linux
2026-02-02 18:33   ` Yury Norov
2026-02-02 20:07     ` David Laight
2026-02-03  4:47       ` Nathan Chancellor
2026-02-03 11:14         ` David Laight
2026-02-03 19:41       ` Yury Norov
2026-01-21 14:57 ` [PATCH next 03/14] media: videobuf2-core: Use static_assert() for sanity check david.laight.linux
2026-01-21 14:57 ` [PATCH next 04/14] media: atomisp: " david.laight.linux
2026-01-21 14:57 ` [PATCH next 05/14] ixgbevf: Use C test for PAGE_SIZE > IXGBE_MAX_DATA_PER_TXD david.laight.linux
2026-01-23 15:44   ` Simon Horman
2026-01-21 14:57 ` [PATCH next 06/14] asm-generic: include linux/bits.h not vdso/bits.h david.laight.linux
2026-01-21 14:57 ` [PATCH next 07/14] x86/tlb: " david.laight.linux
2026-01-21 14:57 ` [PATCH next 08/14] bits: simplify GENMASK_TYPE() david.laight.linux
2026-02-08  2:36   ` Yury Norov
2026-02-09  9:42     ` David Laight
2026-01-21 14:57 ` [PATCH next 09/14] bits: Change BIT_U8/16() and GENMASK_U8/16() to have unsigned values david.laight.linux
2026-01-21 14:57 ` [PATCH next 10/14] bits: Fix assmebler expansions of GENMASK_Uxx() and BIT_Uxx() david.laight.linux
2026-02-08  3:31   ` Yury Norov
2026-02-08 11:42     ` David Laight
2026-02-08 21:20       ` Yury Norov
2026-02-08 22:27         ` David Laight
2026-01-21 14:57 ` [PATCH next 11/14] bit: Strengthen compile-time tests in GENMASK() and BIT() david.laight.linux
2026-01-21 18:43   ` Vincent Mailhol
2026-01-21 19:14     ` David Laight
2026-01-22  1:11   ` kernel test robot
2026-01-22 10:25     ` David Laight
2026-01-22 20:10       ` David Laight
2026-01-22  4:41   ` kernel test robot
2026-01-22 10:33     ` David Laight
2026-01-22 14:26       ` Andy Shevchenko
2026-01-22 14:55         ` David Laight
2026-01-23  1:25         ` Philip Li
2026-01-23  8:01           ` Vincent Mailhol
2026-01-23  8:11             ` Andy Shevchenko
2026-01-23  8:20               ` Al Viro
2026-01-23  8:24                 ` Andy Shevchenko
2026-01-23  8:32                   ` Vincent Mailhol
2026-01-23  8:46                     ` Andy Shevchenko
2026-01-23  1:24       ` Philip Li
2026-01-21 14:57 ` [PATCH next 12/14] bits: move the defitions of BIT() and BIT_ULL() back to linux/bits.h david.laight.linux
2026-01-21 15:17   ` Thomas Weißschuh
2026-01-21 19:24     ` David Laight
2026-01-22  7:39       ` Thomas Weißschuh
2026-01-22  0:50   ` kernel test robot
2026-01-22  1:23   ` kernel test robot
2026-01-22 10:30     ` David Laight
2026-02-07 22:40   ` Thomas Gleixner
2026-02-08  4:23     ` Yury Norov
2026-01-21 14:57 ` [PATCH next 13/14] test_bits: Change all the tests to be compile-time tests david.laight.linux
2026-02-08  4:37   ` Yury Norov [this message]
2026-02-08 11:32     ` David Laight
2026-01-21 14:57 ` [PATCH next 14/14] test_bits: include some invalid input tests for GENMASK_INPUT_CHECK() david.laight.linux

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=aYgTHfhxDws-D6yl@yury \
    --to=ynorov@nvidia.com \
    --cc=akpm@linux-foundation.org \
    --cc=andriy.shevchenko@linux.intel.com \
    --cc=arnd@arndb.de \
    --cc=david.laight.linux@gmail.com \
    --cc=gregkh@linuxfoundation.org \
    --cc=jani.nikula@intel.com \
    --cc=keescook@chromium.org \
    --cc=linux-arch@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=lucas.demarchi@intel.com \
    --cc=mailhol.vincent@wanadoo.fr \
    --cc=mathieu.desnoyers@efficios.com \
    --cc=mingo@kernel.org \
    --cc=nathan@kernel.org \
    --cc=peterz@infradead.org \
    --cc=tglx@linutronix.de \
    --cc=yury.norov@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox