* [PATCH] compiler.h: Support 64-bit bitwise types in is_signed_type()
@ 2026-11-16 22:26 Bart Van Assche
0 siblings, 0 replies; only message in thread
From: Bart Van Assche @ 2026-11-16 22:26 UTC (permalink / raw)
To: Steven Rostedt
Cc: linux-kernel, Christoph Hellwig, Linus Torvalds, David Laight,
Bart Van Assche
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).
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@acm.org>
---
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);
^ permalink raw reply [flat|nested] only message in thread
only message in thread, other threads:[~2026-11-16 22:26 UTC | newest]
Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-16 22:26 [PATCH] compiler.h: Support 64-bit bitwise types in is_signed_type() Bart Van Assche
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®