Re: [PATCH] compiler.h: Support 64-bit bitwise types in is_signed_type()
From: David Laight
Date: Tue Nov 17 2026 - 03:06:30 EST
On Mon, 16 Nov 2026 14:26:25 -0800
Bart Van Assche <bvanassche@xxxxxxx> wrote:
> Replace -1 with -1LL in the definition of is_signed_type(type) to
> prevent sparse from emitting "cast to restricted __le64" and
> "restricted __le64 degrades to integer" warnings when evaluating
> is_signed_type() on 64-bit __bitwise types such as __le64 (for example,
> in tracepoint __field() macros in net/mac80211/trace.h).
Don't the trace definitions need fixing?
The trace code prints the numeric value so you really want a host
ordered value in the trace record.
An alternative view is that even asking the question 'is_signed_type'
of a bitwise variable makes no sense.
David
>
> Use a 64-bit all-ones constant (-1LL) rather than a 32-bit int (-1)
> because sparse only permits casting a non-zero integer constant to a
> __bitwise type and ordering it with '<' when the constant has all bits
> set across the full bit width of the target type. Ensure that the
> constant before the cast has 64 bits set so that sparse's
> restricted_value() and can_order() checks succeed for 64-bit __bitwise
> types on both 32-bit and 64-bit architectures.
>
> Add a KUnit test case for is_signed_type(__le64) in
> lib/tests/is_signed_type_kunit.c to exercise 64-bit __bitwise types.
>
> Signed-off-by: Bart Van Assche <bvanassche@xxxxxxx>
> ---
> include/linux/compiler.h | 2 +-
> lib/tests/is_signed_type_kunit.c | 5 +++++
> 2 files changed, 6 insertions(+), 1 deletion(-)
>
> diff --git a/include/linux/compiler.h b/include/linux/compiler.h
> index cb2f6050bdf7..2f133a7c3885 100644
> --- a/include/linux/compiler.h
> +++ b/include/linux/compiler.h
> @@ -326,7 +326,7 @@ static inline void *offset_to_ptr(const int *off)
> * Whether 'type' is a signed type or an unsigned type. Supports scalar types,
> * bool and also pointer types.
> */
> -#define is_signed_type(type) (((type)(-1)) < (__force type)1)
> +#define is_signed_type(type) (((type)(-1LL)) < (__force type)1)
> #define is_unsigned_type(type) (!is_signed_type(type))
>
> /*
> diff --git a/lib/tests/is_signed_type_kunit.c b/lib/tests/is_signed_type_kunit.c
> index 88adbe813f3a..b9c8f7da45e9 100644
> --- a/lib/tests/is_signed_type_kunit.c
> +++ b/lib/tests/is_signed_type_kunit.c
> @@ -18,6 +18,8 @@ enum signed_enum {
>
> static void is_signed_type_test(struct kunit *test)
> {
> + typedef __bitwise long long bitwise_long_long;
> +
> KUNIT_EXPECT_EQ(test, is_signed_type(bool), false);
> KUNIT_EXPECT_EQ(test, is_signed_type(signed char), true);
> KUNIT_EXPECT_EQ(test, is_signed_type(unsigned char), false);
> @@ -27,7 +29,10 @@ static void is_signed_type_test(struct kunit *test)
> KUNIT_EXPECT_EQ(test, is_signed_type(long), true);
> KUNIT_EXPECT_EQ(test, is_signed_type(unsigned long), false);
> KUNIT_EXPECT_EQ(test, is_signed_type(long long), true);
> + KUNIT_EXPECT_EQ(test, is_signed_type(bitwise_long_long), true);
> KUNIT_EXPECT_EQ(test, is_signed_type(unsigned long long), false);
> + KUNIT_EXPECT_EQ(test, is_signed_type(__be64), false);
> + KUNIT_EXPECT_EQ(test, is_signed_type(__le64), false);
> KUNIT_EXPECT_EQ(test, is_signed_type(enum unsigned_enum), false);
> KUNIT_EXPECT_EQ(test, is_signed_type(enum signed_enum), true);
> KUNIT_EXPECT_EQ(test, is_signed_type(void *), false);