mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] compiler.h: Support 64-bit bitwise types in is_signed_type()
@ 2026-11-16 22:26 Bart Van Assche
  2026-11-17  8:06 ` David Laight
  0 siblings, 1 reply; 3+ messages 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] 3+ messages in thread

* Re: [PATCH] compiler.h: Support 64-bit bitwise types in is_signed_type()
  2026-11-16 22:26 [PATCH] compiler.h: Support 64-bit bitwise types in is_signed_type() Bart Van Assche
@ 2026-11-17  8:06 ` David Laight
  2026-11-17 16:32   ` Bart Van Assche
  0 siblings, 1 reply; 3+ messages in thread
From: David Laight @ 2026-11-17  8:06 UTC (permalink / raw)
  To: Bart Van Assche
  Cc: Steven Rostedt, linux-kernel, Christoph Hellwig, Linus Torvalds

On Mon, 16 Nov 2026 14:26:25 -0800
Bart Van Assche <bvanassche@acm.org> 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@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] 3+ messages in thread

* Re: [PATCH] compiler.h: Support 64-bit bitwise types in is_signed_type()
  2026-11-17  8:06 ` David Laight
@ 2026-11-17 16:32   ` Bart Van Assche
  0 siblings, 0 replies; 3+ messages in thread
From: Bart Van Assche @ 2026-11-17 16:32 UTC (permalink / raw)
  To: David Laight
  Cc: Steven Rostedt, linux-kernel, Christoph Hellwig, Linus Torvalds

On 11/17/26 12:06 AM, David Laight wrote:
> 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.
Are you perhaps suggesting that we should tell ftrace users that it is
no longer allowed to declare __le64 fields, something they probably
would not consider reasonable? Please let me know if I misunderstood
your question. Below is an incomplete overview of the examples of __le64
tracing fields (__le64 typedefs have been ignored):

$ git grep -nHF '__field(__le64'
net/ieee802154/trace.h:74:		__field(__le64, extended_addr)
net/ieee802154/trace.h:367:		__field(__le64, addr)
net/ieee802154/trace.h:386:		__field(__le64, addr)
net/mac80211/trace.h:2507:		__field(__le64, twt)
net/mac802154/trace.h:191:		__field(__le64, extended_addr)
net/mac802154/trace.h:272:		__field(__le64, addr)

Thanks,

Bart.

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2026-11-17 16:32 UTC | newest]

Thread overview: 3+ messages (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
2026-11-17  8:06 ` David Laight
2026-11-17 16:32   ` 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®