Skip to contentSkip to conversation

include/net/sock.h:2100:16: sparse: sparse: cast to non-scalar

DISCUSSION kernel test robot 7 participants 17 messages Jan 10 – Jan 13

lore
Share & tools
lore

kernel test robot

author build bot
#0 lore raw
tree:   https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git master
head:   97313d6113ab4362ad15076f65560e65288fbcb8
commit: e84a4927a404f369c842c19de93b216627fcc690 net: annotate races around sk->sk_uid
date:   7 months ago
config: alpha-randconfig-r112-20260110 (https://download.01.org/0day-ci/archive/20260111/[email protected]/config)
compiler: alpha-linux-gcc (GCC) 8.5.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20260111/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

sparse warnings: (new ones prefixed by >>)
net/ipv4/datagram.c: note: in included file (through include/net/inet_sock.h, include/net/ip.h):

>> include/net/sock.h:2100:16: sparse: sparse: cast to non-scalar
   include/net/sock.h:2100:16: sparse: sparse: cast from non-scalar
>> include/net/sock.h:2100:16: sparse: sparse: cast to non-scalar
   include/net/sock.h:2100:16: sparse: sparse: cast from non-scalar
--
   net/ipv4/raw.c: note: in included file (through include/linux/mroute_base.h, include/linux/mroute.h):
>> include/net/sock.h:2100:16: sparse: sparse: cast to non-scalar

include/net/sock.h:2100:16: sparse: sparse: cast from non-scalar
--
net/ipv4/inet_connection_sock.c:328:36: sparse: sparse: context imbalance in 'inet_csk_find_open_port' - wrong count at exit net/ipv4/inet_connection_sock.c:605:23: sparse: sparse: context imbalance in 'inet_csk_get_port' - unexpected unlock net/ipv4/inet_connection_sock.c: note: in included file (through include/net/inet_sock.h, include/net/inet_connection_sock.h):

>> include/net/sock.h:2100:16: sparse: sparse: cast to non-scalar
   include/net/sock.h:2100:16: sparse: sparse: cast from non-scalar
>> include/net/sock.h:2100:16: sparse: sparse: cast to non-scalar
   include/net/sock.h:2100:16: sparse: sparse: cast from non-scalar
   net/ipv4/inet_connection_sock.c:1467:9: sparse: sparse: context imbalance in 'inet_csk_complete_hashdance' - unexpected unlock
>> include/net/sock.h:2100:16: sparse: sparse: cast to non-scalar
   include/net/sock.h:2100:16: sparse: sparse: cast from non-scalar
--
   net/ipv4/route.c: note: in included file (through include/linux/mroute_base.h, include/linux/mroute.h):
>> include/net/sock.h:2100:16: sparse: sparse: cast to non-scalar
   include/net/sock.h:2100:16: sparse: sparse: cast from non-scalar
>> include/net/sock.h:2100:16: sparse: sparse: cast to non-scalar
   include/net/sock.h:2100:16: sparse: sparse: cast from non-scalar
--
   net/ipv4/ip_output.c: note: in included file (through include/net/inet_sock.h, include/net/ip.h):
>> include/net/sock.h:2100:16: sparse: sparse: cast to non-scalar
   include/net/sock.h:2100:16: sparse: sparse: cast from non-scalar
--
   net/ipv4/af_inet.c: note: in included file (through include/net/inet_sock.h, include/net/ip.h):
>> include/net/sock.h:2100:16: sparse: sparse: cast to non-scalar
   include/net/sock.h:2100:16: sparse: sparse: cast from non-scalar
>> include/net/sock.h:2100:16: sparse: sparse: cast to non-scalar

include/net/sock.h:2100:16: sparse: sparse: cast from non-scalar
--
net/ipv4/tcp_ipv4.c:3415:41: sparse: sparse: incorrect type in argument 1 (different address spaces) @@ expected void const *data @@ got struct tcp_congestion_ops const [noderef] __rcu *tcp_congestion_control @@
net/ipv4/tcp_ipv4.c:3415:41: sparse: expected void const *data
net/ipv4/tcp_ipv4.c:3415:41: sparse: got struct tcp_congestion_ops const [noderef] __rcu *tcp_congestion_control
net/ipv4/tcp_ipv4.c:3537:45: sparse: sparse: incorrect type in argument 1 (different address spaces) @@ expected void const *data @@ got struct tcp_congestion_ops const [noderef] __rcu *extern [addressable] [toplevel] tcp_congestion_control @@
net/ipv4/tcp_ipv4.c:3537:45: sparse: expected void const *data
net/ipv4/tcp_ipv4.c:3537:45: sparse: got struct tcp_congestion_ops const [noderef] __rcu *extern [addressable] [toplevel] tcp_congestion_control
net/ipv4/tcp_ipv4.c:3541:50: sparse: sparse: incorrect type in assignment (different address spaces) @@ expected struct tcp_congestion_ops const [noderef] __rcu *tcp_congestion_control @@ got struct tcp_congestion_ops * @@
net/ipv4/tcp_ipv4.c:3541:50: sparse: expected struct tcp_congestion_ops const [noderef] __rcu *tcp_congestion_control
net/ipv4/tcp_ipv4.c:3541:50: sparse: got struct tcp_congestion_ops *
net/ipv4/tcp_ipv4.c: note: in included file (through include/net/inet_sock.h, include/net/icmp.h):

>> include/net/sock.h:2100:16: sparse: sparse: cast to non-scalar
   include/net/sock.h:2100:16: sparse: sparse: cast from non-scalar
>> include/net/sock.h:2100:16: sparse: sparse: cast to non-scalar
   include/net/sock.h:2100:16: sparse: sparse: cast from non-scalar
>> include/net/sock.h:2100:16: sparse: sparse: cast to non-scalar

include/net/sock.h:2100:16: sparse: sparse: cast from non-scalar
net/ipv4/tcp_ipv4.c:1846:25: sparse: sparse: context imbalance in 'tcp_v4_syn_recv_sock' - unexpected unlock net/ipv4/tcp_ipv4.c:2139:17: sparse: sparse: context imbalance in 'tcp_add_backlog' - unexpected unlock net/ipv4/tcp_ipv4.c:2401:21: sparse: sparse: context imbalance in 'tcp_v4_rcv' - different lock contexts for basic block net/ipv4/tcp_ipv4.c:2612:13: sparse: sparse: context imbalance in 'listening_get_first' - wrong count at exit net/ipv4/tcp_ipv4.c:2660:29: sparse: sparse: context imbalance in 'listening_get_next' - unexpected unlock net/ipv4/tcp_ipv4.c:2693:13: sparse: sparse: context imbalance in 'established_get_first' - wrong count at exit net/ipv4/tcp_ipv4.c:2738:40: sparse: sparse: context imbalance in 'established_get_next' - unexpected unlock net/ipv4/tcp_ipv4.c:2870:36: sparse: sparse: context imbalance in 'tcp_seq_stop' - unexpected unlock net/ipv4/tcp_ipv4.c:3091:20: sparse: sparse: context imbalance in 'bpf_iter_tcp_listening_batch' - unexpected unlock net/ipv4/tcp_ipv4.c:3119:40: sparse: sparse: context imbalance in 'bpf_iter_tcp_established_batch' - unexpected unlock net/ipv4/tcp_ipv4.c:3416:41: sparse: sparse: dereference of noderef expression net/ipv4/tcp_ipv4.c:3416:41: sparse: sparse: dereference of noderef expression net/ipv4/tcp_ipv4.c:3538:45: sparse: sparse: dereference of noderef expression net/ipv4/tcp_ipv4.c:3538:45: sparse: sparse: dereference of noderef expression
--
net/ipv4/ping.c: note: in included file (through include/net/inet_sock.h, include/net/ip.h):

>> include/net/sock.h:2100:16: sparse: sparse: cast to non-scalar
   include/net/sock.h:2100:16: sparse: sparse: cast from non-scalar
--
   net/ipv4/udp.c: note: in included file (through include/linux/bpf-cgroup.h):
>> include/net/sock.h:2100:16: sparse: sparse: cast to non-scalar

include/net/sock.h:2100:16: sparse: sparse: cast from non-scalar net/ipv4/udp.c:1668:28: sparse: sparse: context imbalance in 'udp_rmem_release' - unexpected unlock net/ipv4/udp.c:1700:19: sparse: sparse: context imbalance in 'busylock_acquire' - wrong count at exit net/ipv4/udp.c:1712:28: sparse: sparse: context imbalance in 'busylock_release' - unexpected unlock net/ipv4/udp.c:3305:9: sparse: sparse: context imbalance in 'udp_get_first' - wrong count at exit net/ipv4/udp.c:3322:39: sparse: sparse: context imbalance in 'udp_get_next' - unexpected unlock net/ipv4/udp.c:3370:31: sparse: sparse: context imbalance in 'udp_seq_stop' - unexpected unlock

vim +2100 include/net/sock.h

2096
2097 static inline kuid_t sk_uid(const struct sock *sk)
2098 {
2099 /* Paired with WRITE_ONCE() in sockfs_setattr() */

> 2100		return READ_ONCE(sk->sk_uid);

2101 }
2102

Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
#1 lore raw

On Sun, Jan 11, 2026 at 05:06:31AM +0800, kernel test robot wrote:

Folks, could your bot be taught to recognize that kind of situation? It's very clear what's going on - an inline function that gets the same warning with this config every time it is called. Commit in question has added several calls of that thing; the origin of warning has nothing whatsoever to do with it, and that can be guessed without even looking at the source - the quoted diff between the logs is quite enough for that.

Looking at the function in question,

Quoted 97 lines

sparse warnings: (new ones prefixed by >>)
net/ipv4/datagram.c: note: in included file (through include/net/inet_sock.h, include/net/ip.h):

Quoted 1 line
Quoted 1 line

include/net/sock.h:2100:16: sparse: sparse: cast to non-scalar

include/net/sock.h:2100:16: sparse: sparse: cast from non-scalar

Quoted 1 line
Quoted 1 line

include/net/sock.h:2100:16: sparse: sparse: cast to non-scalar

include/net/sock.h:2100:16: sparse: sparse: cast from non-scalar
--
net/ipv4/raw.c: note: in included file (through include/linux/mroute_base.h, include/linux/mroute.h):

Quoted 1 line
Quoted 1 line

include/net/sock.h:2100:16: sparse: sparse: cast to non-scalar

include/net/sock.h:2100:16: sparse: sparse: cast from non-scalar
--
net/ipv4/inet_connection_sock.c:328:36: sparse: sparse: context imbalance in 'inet_csk_find_open_port' - wrong count at exit net/ipv4/inet_connection_sock.c:605:23: sparse: sparse: context imbalance in 'inet_csk_get_port' - unexpected unlock net/ipv4/inet_connection_sock.c: note: in included file (through include/net/inet_sock.h, include/net/inet_connection_sock.h):

Quoted 1 line
Quoted 1 line

include/net/sock.h:2100:16: sparse: sparse: cast to non-scalar

include/net/sock.h:2100:16: sparse: sparse: cast from non-scalar

Quoted 1 line
Quoted 1 line

include/net/sock.h:2100:16: sparse: sparse: cast to non-scalar

include/net/sock.h:2100:16: sparse: sparse: cast from non-scalar
net/ipv4/inet_connection_sock.c:1467:9: sparse: sparse: context imbalance in 'inet_csk_complete_hashdance' - unexpected unlock

Quoted 1 line
Quoted 1 line

include/net/sock.h:2100:16: sparse: sparse: cast to non-scalar

include/net/sock.h:2100:16: sparse: sparse: cast from non-scalar
--
net/ipv4/route.c: note: in included file (through include/linux/mroute_base.h, include/linux/mroute.h):

Quoted 1 line
Quoted 1 line

include/net/sock.h:2100:16: sparse: sparse: cast to non-scalar

include/net/sock.h:2100:16: sparse: sparse: cast from non-scalar

Quoted 1 line
Quoted 1 line

include/net/sock.h:2100:16: sparse: sparse: cast to non-scalar

include/net/sock.h:2100:16: sparse: sparse: cast from non-scalar
--
net/ipv4/ip_output.c: note: in included file (through include/net/inet_sock.h, include/net/ip.h):

Quoted 1 line
Quoted 1 line

include/net/sock.h:2100:16: sparse: sparse: cast to non-scalar

include/net/sock.h:2100:16: sparse: sparse: cast from non-scalar
--
net/ipv4/af_inet.c: note: in included file (through include/net/inet_sock.h, include/net/ip.h):

Quoted 1 line
Quoted 1 line

include/net/sock.h:2100:16: sparse: sparse: cast to non-scalar

include/net/sock.h:2100:16: sparse: sparse: cast from non-scalar

Quoted 1 line
Quoted 1 line

include/net/sock.h:2100:16: sparse: sparse: cast to non-scalar

include/net/sock.h:2100:16: sparse: sparse: cast from non-scalar
--
net/ipv4/tcp_ipv4.c:3415:41: sparse: sparse: incorrect type in argument 1 (different address spaces) @@ expected void const *data @@ got struct tcp_congestion_ops const [noderef] __rcu *tcp_congestion_control @@
net/ipv4/tcp_ipv4.c:3415:41: sparse: expected void const *data
net/ipv4/tcp_ipv4.c:3415:41: sparse: got struct tcp_congestion_ops const [noderef] __rcu *tcp_congestion_control
net/ipv4/tcp_ipv4.c:3537:45: sparse: sparse: incorrect type in argument 1 (different address spaces) @@ expected void const *data @@ got struct tcp_congestion_ops const [noderef] __rcu *extern [addressable] [toplevel] tcp_congestion_control @@
net/ipv4/tcp_ipv4.c:3537:45: sparse: expected void const *data
net/ipv4/tcp_ipv4.c:3537:45: sparse: got struct tcp_congestion_ops const [noderef] __rcu *extern [addressable] [toplevel] tcp_congestion_control
net/ipv4/tcp_ipv4.c:3541:50: sparse: sparse: incorrect type in assignment (different address spaces) @@ expected struct tcp_congestion_ops const [noderef] __rcu *tcp_congestion_control @@ got struct tcp_congestion_ops * @@
net/ipv4/tcp_ipv4.c:3541:50: sparse: expected struct tcp_congestion_ops const [noderef] __rcu *tcp_congestion_control
net/ipv4/tcp_ipv4.c:3541:50: sparse: got struct tcp_congestion_ops *
net/ipv4/tcp_ipv4.c: note: in included file (through include/net/inet_sock.h, include/net/icmp.h):

Quoted 1 line
Quoted 1 line

include/net/sock.h:2100:16: sparse: sparse: cast to non-scalar

include/net/sock.h:2100:16: sparse: sparse: cast from non-scalar

Quoted 1 line
Quoted 1 line

include/net/sock.h:2100:16: sparse: sparse: cast to non-scalar

include/net/sock.h:2100:16: sparse: sparse: cast from non-scalar

Quoted 1 line
Quoted 1 line

include/net/sock.h:2100:16: sparse: sparse: cast to non-scalar

include/net/sock.h:2100:16: sparse: sparse: cast from non-scalar
net/ipv4/tcp_ipv4.c:1846:25: sparse: sparse: context imbalance in 'tcp_v4_syn_recv_sock' - unexpected unlock net/ipv4/tcp_ipv4.c:2139:17: sparse: sparse: context imbalance in 'tcp_add_backlog' - unexpected unlock net/ipv4/tcp_ipv4.c:2401:21: sparse: sparse: context imbalance in 'tcp_v4_rcv' - different lock contexts for basic block net/ipv4/tcp_ipv4.c:2612:13: sparse: sparse: context imbalance in 'listening_get_first' - wrong count at exit net/ipv4/tcp_ipv4.c:2660:29: sparse: sparse: context imbalance in 'listening_get_next' - unexpected unlock net/ipv4/tcp_ipv4.c:2693:13: sparse: sparse: context imbalance in 'established_get_first' - wrong count at exit net/ipv4/tcp_ipv4.c:2738:40: sparse: sparse: context imbalance in 'established_get_next' - unexpected unlock net/ipv4/tcp_ipv4.c:2870:36: sparse: sparse: context imbalance in 'tcp_seq_stop' - unexpected unlock net/ipv4/tcp_ipv4.c:3091:20: sparse: sparse: context imbalance in 'bpf_iter_tcp_listening_batch' - unexpected unlock net/ipv4/tcp_ipv4.c:3119:40: sparse: sparse: context imbalance in 'bpf_iter_tcp_established_batch' - unexpected unlock net/ipv4/tcp_ipv4.c:3416:41: sparse: sparse: dereference of noderef expression net/ipv4/tcp_ipv4.c:3416:41: sparse: sparse: dereference of noderef expression net/ipv4/tcp_ipv4.c:3538:45: sparse: sparse: dereference of noderef expression net/ipv4/tcp_ipv4.c:3538:45: sparse: sparse: dereference of noderef expression
--
net/ipv4/ping.c: note: in included file (through include/net/inet_sock.h, include/net/ip.h):

Quoted 1 line
Quoted 1 line

include/net/sock.h:2100:16: sparse: sparse: cast to non-scalar

include/net/sock.h:2100:16: sparse: sparse: cast from non-scalar
--
net/ipv4/udp.c: note: in included file (through include/linux/bpf-cgroup.h):

Quoted 1 line
Quoted 1 line

include/net/sock.h:2100:16: sparse: sparse: cast to non-scalar

include/net/sock.h:2100:16: sparse: sparse: cast from non-scalar net/ipv4/udp.c:1668:28: sparse: sparse: context imbalance in 'udp_rmem_release' - unexpected unlock net/ipv4/udp.c:1700:19: sparse: sparse: context imbalance in 'busylock_acquire' - wrong count at exit net/ipv4/udp.c:1712:28: sparse: sparse: context imbalance in 'busylock_release' - unexpected unlock net/ipv4/udp.c:3305:9: sparse: sparse: context imbalance in 'udp_get_first' - wrong count at exit net/ipv4/udp.c:3322:39: sparse: sparse: context imbalance in 'udp_get_next' - unexpected unlock net/ipv4/udp.c:3370:31: sparse: sparse: context imbalance in 'udp_seq_stop' - unexpected unlock

vim +2100 include/net/sock.h

2096
2097 static inline kuid_t sk_uid(const struct sock *sk)
2098 {
2099 /* Paired with WRITE_ONCE() in sockfs_setattr() */

Quoted 1 line

2100 return READ_ONCE(sk->sk_uid);

2101 }
2102

Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
#2 lore raw
Quoted 11 lines · Al Viro

On Sun, Jan 11, 2026 at 05:06:31AM +0800, kernel test robot wrote:

Folks, could your bot be taught to recognize that kind of situation? It's very clear what's going on - an inline function that gets the same warning with this config every time it is called. Commit in question has added several calls of that thing; the origin of warning has nothing whatsoever to do with it, and that can be guessed without even looking at the source - the quoted diff between the logs is quite enough for that.

Looking at the function in question,

[accidentally sent halfway through editing, sorry]

e84a4927a404f (Eric Dumazet               2025-06-20 13:30:00 +0000 2097) static inline kuid_t sk_uid(const struct sock *sk)
e84a4927a404f (Eric Dumazet               2025-06-20 13:30:00 +0000 2098) {
e84a4927a404f (Eric Dumazet               2025-06-20 13:30:00 +0000 2099)       /* Paired with WRITE_ONCE() in sockfs_setattr() */
e84a4927a404f (Eric Dumazet               2025-06-20 13:30:00 +0000 2100)       return READ_ONCE(sk->sk_uid);
e84a4927a404f (Eric Dumazet               2025-06-20 13:30:00 +0000 2101) }

we see that warning in question almost certainly comes from
READ_ONCE(some_kuid_t_object)
and looking at alpha we see this:

#define __READ_ONCE(x)                                                  \
({                                                                      \
        __unqual_scalar_typeof(x) __x =                                 \
		(*(volatile typeof(__x) *)(&(x)));                      \
	mb();                                                           \
	(typeof(x))__x;                                                 \
})
combined with
typedef struct {
        uid_t val;
} kuid_t;

IOW, it complains about a cast from structure to itself, which is fair enough - C is pretty clear about not allowing any typecasts to or from non-scalar types, tautological or not.

Why do we even need that cast? Seeing that generic __READ_ONCE() is #define __READ_ONCE(x) (*(const volatile __unqual_scalar_typeof(x) *)&(x)) the cast added on alpha seems to be pointless.

Folks involved in putting that cast in arch/alpha/include/asm/rwonce.h Cc'd...

#3 lore raw
Quoted 41 lines · Al Viro
Quoted 11 lines · Al Viro

On Sun, Jan 11, 2026 at 05:06:31AM +0800, kernel test robot wrote:

Folks, could your bot be taught to recognize that kind of situation? It's very clear what's going on - an inline function that gets the same warning with this config every time it is called. Commit in question has added several calls of that thing; the origin of warning has nothing whatsoever to do with it, and that can be guessed without even looking at the source - the quoted diff between the logs is quite enough for that.

Looking at the function in question,

[accidentally sent halfway through editing, sorry]

e84a4927a404f (Eric Dumazet               2025-06-20 13:30:00 +0000 2097) static inline kuid_t sk_uid(const struct sock *sk)
e84a4927a404f (Eric Dumazet               2025-06-20 13:30:00 +0000 2098) {
e84a4927a404f (Eric Dumazet               2025-06-20 13:30:00 +0000 2099)       /* Paired with WRITE_ONCE() in sockfs_setattr() */
e84a4927a404f (Eric Dumazet               2025-06-20 13:30:00 +0000 2100)       return READ_ONCE(sk->sk_uid);
e84a4927a404f (Eric Dumazet               2025-06-20 13:30:00 +0000 2101) }

we see that warning in question almost certainly comes from
READ_ONCE(some_kuid_t_object)
and looking at alpha we see this:

#define __READ_ONCE(x)                                                  \
({                                                                      \
        __unqual_scalar_typeof(x) __x =                                 \
                (*(volatile typeof(__x) *)(&(x)));                      \
        mb();                                                           \
        (typeof(x))__x;                                                 \
})
combined with
typedef struct {
        uid_t val;
} kuid_t;

IOW, it complains about a cast from structure to itself, which is fair enough - C is pretty clear about not allowing any typecasts to or from non-scalar types, tautological or not.

Quoted 5 lines

Why do we even need that cast? Seeing that generic __READ_ONCE() is #define __READ_ONCE(x) (*(const volatile __unqual_scalar_typeof(x) *)&(x)) the cast added on alpha seems to be pointless.

Folks involved in putting that cast in arch/alpha/include/asm/rwonce.h Cc'd...

I have sent a fix, but got no reply from any alpha users.

https://lists.openwall.net/linux-kernel/2025/11/03/350

My understanding is that this arch is no longer used.

#5 lore raw
In reply to Al Viro
Quoted 1 line · Al Viro

Folks involved in putting that cast in arch/alpha/include/asm/rwonce.h Cc'd...

FWIW, there's a way to strip qualifiers from *any* non-array type. Look:

void f(void)

{
	const int x;
	x = 1;		// an error
	typeof(((typeof(x)(*)(void))0)()) y;
	y = 2;		// perfectly fine
}

The way it works is that qualifiers are stripped from return type when deriving a function type. That was spelled out only in C17; 6.7.6.3[5]

| If, in the declaration "T D1", D1 has the form
| D ( parameter-type-list )
|  or
| D ( identifier-list[opt] )
| and the type specified for ident in the declaration "T D" is
| "derived-declarator-type-list T", then the type specified for ident
| is "derived-declarator-type-list function returning the unqualified version
| of T".

but that "unqualified version of..." matched the common practice in earlier variants of standard; they stopped issuing TCs by that point (~2014), but both clang and gcc behave that way with any variant of standard.

IOW, this

#define unqual_non_array(T) __typeof__(((T(*)(void))0)())

would do the right thing without that _Generic cascade and it'll work just fine for e.g. kuid_t. Using it for an array would trigger an error, array-returning functions being forbidden...

Guys, do you have any problems with replacing __unqual_scalar_typeof() uses with that thing?

As in,#ifndef __smp_load_acquire

#define __smp_load_acquire(p)                                           \
({                                                                      \
        unqual_non_array(__typeof__(*p)) ___p1 = READ_ONCE(*p);         \

...

Objections? IMO it's more palatable than current __unqual_scalar_typeof()...

#6 lore raw
Quoted 10 lines · Al Viro

Guys, do you have any problems with replacing __unqual_scalar_typeof() uses with that thing?

As in,#ifndef __smp_load_acquire

#define __smp_load_acquire(p)                                           \
({                                                                      \
        unqual_non_array(__typeof__(*p)) ___p1 = READ_ONCE(*p);         \

...

Objections? IMO it's more palatable than current __unqual_scalar_typeof()...

Argh... gcc prior to 8.4 fucks it up, and our minimum is 8.1 ;-/ Pity, that... I still think it's worth keeping in mind for when we bump the minimal gcc revision, though.

FWIW, with clang it works all way back at least to 3.1; icc - at least to 13.0.1...

#16 lore raw
In reply to Al Viro
Quoted 3 lines · Al Viro

On Sun, Jan 11, 2026 at 05:06:31AM +0800, kernel test robot wrote:

Folks, could your bot be taught to recognize that kind of situation?

Got it, we will follow up to update the bot logic to ignore further reports for this pattern.

Quoted 107 lines

It's very clear what's going on - an inline function that gets the same warning with this config every time it is called. Commit in question has added several calls of that thing; the origin of warning has nothing whatsoever to do with it, and that can be guessed without even looking at the source - the quoted diff between the logs is quite enough for that.

Looking at the function in question,

Quoted 97 lines

sparse warnings: (new ones prefixed by >>)
net/ipv4/datagram.c: note: in included file (through include/net/inet_sock.h, include/net/ip.h):

Quoted 1 line
Quoted 1 line

include/net/sock.h:2100:16: sparse: sparse: cast to non-scalar

include/net/sock.h:2100:16: sparse: sparse: cast from non-scalar

Quoted 1 line
Quoted 1 line

include/net/sock.h:2100:16: sparse: sparse: cast to non-scalar

include/net/sock.h:2100:16: sparse: sparse: cast from non-scalar
--
net/ipv4/raw.c: note: in included file (through include/linux/mroute_base.h, include/linux/mroute.h):

Quoted 1 line
Quoted 1 line

include/net/sock.h:2100:16: sparse: sparse: cast to non-scalar

include/net/sock.h:2100:16: sparse: sparse: cast from non-scalar
--
net/ipv4/inet_connection_sock.c:328:36: sparse: sparse: context imbalance in 'inet_csk_find_open_port' - wrong count at exit net/ipv4/inet_connection_sock.c:605:23: sparse: sparse: context imbalance in 'inet_csk_get_port' - unexpected unlock net/ipv4/inet_connection_sock.c: note: in included file (through include/net/inet_sock.h, include/net/inet_connection_sock.h):

Quoted 1 line
Quoted 1 line

include/net/sock.h:2100:16: sparse: sparse: cast to non-scalar

include/net/sock.h:2100:16: sparse: sparse: cast from non-scalar

Quoted 1 line
Quoted 1 line

include/net/sock.h:2100:16: sparse: sparse: cast to non-scalar

include/net/sock.h:2100:16: sparse: sparse: cast from non-scalar
net/ipv4/inet_connection_sock.c:1467:9: sparse: sparse: context imbalance in 'inet_csk_complete_hashdance' - unexpected unlock

Quoted 1 line
Quoted 1 line

include/net/sock.h:2100:16: sparse: sparse: cast to non-scalar

include/net/sock.h:2100:16: sparse: sparse: cast from non-scalar
--
net/ipv4/route.c: note: in included file (through include/linux/mroute_base.h, include/linux/mroute.h):

Quoted 1 line
Quoted 1 line

include/net/sock.h:2100:16: sparse: sparse: cast to non-scalar

include/net/sock.h:2100:16: sparse: sparse: cast from non-scalar

Quoted 1 line
Quoted 1 line

include/net/sock.h:2100:16: sparse: sparse: cast to non-scalar

include/net/sock.h:2100:16: sparse: sparse: cast from non-scalar
--
net/ipv4/ip_output.c: note: in included file (through include/net/inet_sock.h, include/net/ip.h):

Quoted 1 line
Quoted 1 line

include/net/sock.h:2100:16: sparse: sparse: cast to non-scalar

include/net/sock.h:2100:16: sparse: sparse: cast from non-scalar
--
net/ipv4/af_inet.c: note: in included file (through include/net/inet_sock.h, include/net/ip.h):

Quoted 1 line
Quoted 1 line

include/net/sock.h:2100:16: sparse: sparse: cast to non-scalar

include/net/sock.h:2100:16: sparse: sparse: cast from non-scalar

Quoted 1 line
Quoted 1 line

include/net/sock.h:2100:16: sparse: sparse: cast to non-scalar

include/net/sock.h:2100:16: sparse: sparse: cast from non-scalar
--
net/ipv4/tcp_ipv4.c:3415:41: sparse: sparse: incorrect type in argument 1 (different address spaces) @@ expected void const *data @@ got struct tcp_congestion_ops const [noderef] __rcu *tcp_congestion_control @@
net/ipv4/tcp_ipv4.c:3415:41: sparse: expected void const *data
net/ipv4/tcp_ipv4.c:3415:41: sparse: got struct tcp_congestion_ops const [noderef] __rcu *tcp_congestion_control
net/ipv4/tcp_ipv4.c:3537:45: sparse: sparse: incorrect type in argument 1 (different address spaces) @@ expected void const *data @@ got struct tcp_congestion_ops const [noderef] __rcu *extern [addressable] [toplevel] tcp_congestion_control @@
net/ipv4/tcp_ipv4.c:3537:45: sparse: expected void const *data
net/ipv4/tcp_ipv4.c:3537:45: sparse: got struct tcp_congestion_ops const [noderef] __rcu *extern [addressable] [toplevel] tcp_congestion_control
net/ipv4/tcp_ipv4.c:3541:50: sparse: sparse: incorrect type in assignment (different address spaces) @@ expected struct tcp_congestion_ops const [noderef] __rcu *tcp_congestion_control @@ got struct tcp_congestion_ops * @@
net/ipv4/tcp_ipv4.c:3541:50: sparse: expected struct tcp_congestion_ops const [noderef] __rcu *tcp_congestion_control
net/ipv4/tcp_ipv4.c:3541:50: sparse: got struct tcp_congestion_ops *
net/ipv4/tcp_ipv4.c: note: in included file (through include/net/inet_sock.h, include/net/icmp.h):

Quoted 1 line
Quoted 1 line

include/net/sock.h:2100:16: sparse: sparse: cast to non-scalar

include/net/sock.h:2100:16: sparse: sparse: cast from non-scalar

Quoted 1 line
Quoted 1 line

include/net/sock.h:2100:16: sparse: sparse: cast to non-scalar

include/net/sock.h:2100:16: sparse: sparse: cast from non-scalar

Quoted 1 line
Quoted 1 line

include/net/sock.h:2100:16: sparse: sparse: cast to non-scalar

include/net/sock.h:2100:16: sparse: sparse: cast from non-scalar
net/ipv4/tcp_ipv4.c:1846:25: sparse: sparse: context imbalance in 'tcp_v4_syn_recv_sock' - unexpected unlock net/ipv4/tcp_ipv4.c:2139:17: sparse: sparse: context imbalance in 'tcp_add_backlog' - unexpected unlock net/ipv4/tcp_ipv4.c:2401:21: sparse: sparse: context imbalance in 'tcp_v4_rcv' - different lock contexts for basic block net/ipv4/tcp_ipv4.c:2612:13: sparse: sparse: context imbalance in 'listening_get_first' - wrong count at exit net/ipv4/tcp_ipv4.c:2660:29: sparse: sparse: context imbalance in 'listening_get_next' - unexpected unlock net/ipv4/tcp_ipv4.c:2693:13: sparse: sparse: context imbalance in 'established_get_first' - wrong count at exit net/ipv4/tcp_ipv4.c:2738:40: sparse: sparse: context imbalance in 'established_get_next' - unexpected unlock net/ipv4/tcp_ipv4.c:2870:36: sparse: sparse: context imbalance in 'tcp_seq_stop' - unexpected unlock net/ipv4/tcp_ipv4.c:3091:20: sparse: sparse: context imbalance in 'bpf_iter_tcp_listening_batch' - unexpected unlock net/ipv4/tcp_ipv4.c:3119:40: sparse: sparse: context imbalance in 'bpf_iter_tcp_established_batch' - unexpected unlock net/ipv4/tcp_ipv4.c:3416:41: sparse: sparse: dereference of noderef expression net/ipv4/tcp_ipv4.c:3416:41: sparse: sparse: dereference of noderef expression net/ipv4/tcp_ipv4.c:3538:45: sparse: sparse: dereference of noderef expression net/ipv4/tcp_ipv4.c:3538:45: sparse: sparse: dereference of noderef expression
--
net/ipv4/ping.c: note: in included file (through include/net/inet_sock.h, include/net/ip.h):

Quoted 1 line
Quoted 1 line

include/net/sock.h:2100:16: sparse: sparse: cast to non-scalar

include/net/sock.h:2100:16: sparse: sparse: cast from non-scalar
--
net/ipv4/udp.c: note: in included file (through include/linux/bpf-cgroup.h):

Quoted 1 line
Quoted 1 line

include/net/sock.h:2100:16: sparse: sparse: cast to non-scalar

include/net/sock.h:2100:16: sparse: sparse: cast from non-scalar net/ipv4/udp.c:1668:28: sparse: sparse: context imbalance in 'udp_rmem_release' - unexpected unlock net/ipv4/udp.c:1700:19: sparse: sparse: context imbalance in 'busylock_acquire' - wrong count at exit net/ipv4/udp.c:1712:28: sparse: sparse: context imbalance in 'busylock_release' - unexpected unlock net/ipv4/udp.c:3305:9: sparse: sparse: context imbalance in 'udp_get_first' - wrong count at exit net/ipv4/udp.c:3322:39: sparse: sparse: context imbalance in 'udp_get_next' - unexpected unlock net/ipv4/udp.c:3370:31: sparse: sparse: context imbalance in 'udp_seq_stop' - unexpected unlock

vim +2100 include/net/sock.h

2096
2097 static inline kuid_t sk_uid(const struct sock *sk)
2098 {
2099 /* Paired with WRITE_ONCE() in sockfs_setattr() */

Quoted 1 line

2100 return READ_ONCE(sk->sk_uid);

2101 }
2102

Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
#13 lore raw
In reply to Al Viro
Quoted 12 lines · Al Viro
#define __READ_ONCE(x)                                                  \
({                                                                      \
        __unqual_scalar_typeof(x) __x =                                 \
		(*(volatile typeof(__x) *)(&(x)));                      \
	mb();                                                           \
	(typeof(x))__x;                                                 \
})
combined with
typedef struct {
        uid_t val;
} kuid_t;
Quoted 7 lines

IOW, it complains about a cast from structure to itself, which is fair enough - C is pretty clear about not allowing any typecasts to or from non-scalar types, tautological or not.

Why do we even need that cast? Seeing that generic __READ_ONCE() is #define __READ_ONCE(x) (*(const volatile __unqual_scalar_typeof(x) *)&(x)) the cast added on alpha seems to be pointless.

The problem was things like test_bit() that take a volatile argument, doing READ_ONCE() on them would instantiate a volatile temporary and GCC would end up generating shit code.

The __unqual_scalar_typeof() was the result of trying to remove CV qualifiers from a type.

#7 lore raw
In reply to Al Viro
Quoted 32 lines · Al Viro
Quoted 1 line · Al Viro

Folks involved in putting that cast in arch/alpha/include/asm/rwonce.h Cc'd...

FWIW, there's a way to strip qualifiers from *any* non-array type. Look:

void f(void)

{
	const int x;
	x = 1;		// an error
	typeof(((typeof(x)(*)(void))0)()) y;
	y = 2;		// perfectly fine
}

The way it works is that qualifiers are stripped from return type when deriving a function type. That was spelled out only in C17; 6.7.6.3[5]

| If, in the declaration "T D1", D1 has the form
| D ( parameter-type-list )
|  or
| D ( identifier-list[opt] )
| and the type specified for ident in the declaration "T D" is
| "derived-declarator-type-list T", then the type specified for ident
| is "derived-declarator-type-list function returning the unqualified version
| of T".

but that "unqualified version of..." matched the common practice in earlier variants of standard; they stopped issuing TCs by that point (~2014), but both clang and gcc behave that way with any variant of standard.

IOW, this

Quoted 8 lines

#define unqual_non_array(T) __typeof__(((T(*)(void))0)())

would do the right thing without that _Generic cascade and it'll work just fine for e.g. kuid_t. Using it for an array would trigger an error, array-returning functions being forbidden...

Guys, do you have any problems with replacing __unqual_scalar_typeof() uses with that thing?

There is also __typeof_unqual__, but I do not know if that is now supported by all compilers, if so that is the better option. If not, your function return type thing is awesome.

#8 lore raw
Quoted 39 lines · Peter Zijlstra
Quoted 38 lines · Al Viro
Quoted 1 line · Al Viro

Folks involved in putting that cast in arch/alpha/include/asm/rwonce.h Cc'd...

FWIW, there's a way to strip qualifiers from *any* non-array type. Look:

void f(void)

{
	const int x;
	x = 1;		// an error
	typeof(((typeof(x)(*)(void))0)()) y;
	y = 2;		// perfectly fine
}

The way it works is that qualifiers are stripped from return type when deriving a function type. That was spelled out only in C17; 6.7.6.3[5]

| If, in the declaration "T D1", D1 has the form
| D ( parameter-type-list )
|  or
| D ( identifier-list[opt] )
| and the type specified for ident in the declaration "T D" is
| "derived-declarator-type-list T", then the type specified for ident
| is "derived-declarator-type-list function returning the unqualified version
| of T".

but that "unqualified version of..." matched the common practice in earlier variants of standard; they stopped issuing TCs by that point (~2014), but both clang and gcc behave that way with any variant of standard.

IOW, this

#define unqual_non_array(T) __typeof__(((T(*)(void))0)())

would do the right thing without that _Generic cascade and it'll work just fine for e.g. kuid_t. Using it for an array would trigger an error, array-returning functions being forbidden...

Quoted 6 lines
Quoted 2 lines

Guys, do you have any problems with replacing __unqual_scalar_typeof() uses with that thing?

There is also __typeof_unqual__, but I do not know if that is now supported by all compilers, if so that is the better option. If not, your function return type thing is awesome.

Agreed, that's a really neat trick. I wouldn't put it past somebody to use READ_ONCE() on an array, but the build systems should shake any of those out pretty quickly.

Will

#9 lore raw
In reply to Peter Zijlstra
Quoted 6 lines · Peter Zijlstra
Quoted 6 lines

#define unqual_non_array(T) __typeof__(((T(*)(void))0)())

would do the right thing without that _Generic cascade and it'll work just fine for e.g. kuid_t. Using it for an array would trigger an error, array-returning functions being forbidden...

Quoted 6 lines
Quoted 2 lines

Guys, do you have any problems with replacing __unqual_scalar_typeof() uses with that thing?

There is also __typeof_unqual__, but I do not know if that is now supported by all compilers, if so that is the better option. If not, your function return type thing is awesome.

From experimenting with godbolt.org:

			clang		gcc		icc

__typeof_unqual__ >= 19.0.1 >= 14.1 no
this trick >= 3.0.0 >= 8.4 >= 13.0.1
our minima 15.0.0 8.1

So __typeof_unqual__ is well out of our range; this trick is slightly out of range, but nowhere near as bad. Prior to 8.4 gcc had a bug in that area, unfortunately ;-/

Might make sense to reconsider it next time we bump gcc minimum...

#14 lore raw
In reply to Peter Zijlstra
Quoted 22 lines · Peter Zijlstra
Quoted 12 lines · Al Viro
#define __READ_ONCE(x)                                                  \
({                                                                      \
        __unqual_scalar_typeof(x) __x =                                 \
		(*(volatile typeof(__x) *)(&(x)));                      \
	mb();                                                           \
	(typeof(x))__x;                                                 \
})
combined with
typedef struct {
        uid_t val;
} kuid_t;
Quoted 7 lines

IOW, it complains about a cast from structure to itself, which is fair enough - C is pretty clear about not allowing any typecasts to or from non-scalar types, tautological or not.

Why do we even need that cast? Seeing that generic __READ_ONCE() is #define __READ_ONCE(x) (*(const volatile __unqual_scalar_typeof(x) *)&(x)) the cast added on alpha seems to be pointless.

Quoted 6 lines

The problem was things like test_bit() that take a volatile argument, doing READ_ONCE() on them would instantiate a volatile temporary and GCC would end up generating shit code.

The __unqual_scalar_typeof() was the result of trying to remove CV qualifiers from a type.

Sure, but what does that have to do with the final cast in there? Note that generic __READ_ONCE() ends up with __unqual_scalar_typeof(x) for the type of result and it seems to be working fine.

Why would the same type of result be a problem for alpha? Sure, we need a barrier there, so it can't be literal copy of generic __READ_ONCE(), but what's the problem with using

#define __READ_ONCE(x) \

({									\
	__unqual_scalar_typeof(x) __x =					\
 		(*(volatile typeof(__x) *)(&(x)));			\
	mb();								\
	__x;								\
})

there? What could possibly need those qualifiers added back to the result? It is, after all, an r-value, so...

IDGI...

#10 lore raw
In reply to Al Viro
Quoted 21 lines · Al Viro
Quoted 6 lines · Peter Zijlstra
Quoted 6 lines

#define unqual_non_array(T) __typeof__(((T(*)(void))0)())

would do the right thing without that _Generic cascade and it'll work just fine for e.g. kuid_t. Using it for an array would trigger an error, array-returning functions being forbidden...

Quoted 8 lines
Quoted 2 lines

Guys, do you have any problems with replacing __unqual_scalar_typeof() uses with that thing?

There is also __typeof_unqual__, but I do not know if that is now supported by all compilers, if so that is the better option. If not, your function return type thing is awesome.

From experimenting with godbolt.org:

			clang		gcc		icc

__typeof_unqual__ >= 19.0.1 >= 14.1 no
this trick >= 3.0.0 >= 8.4 >= 13.0.1
our minima 15.0.0 8.1

Quoted 5 lines

So __typeof_unqual__ is well out of our range; this trick is slightly out of range, but nowhere near as bad. Prior to 8.4 gcc had a bug in that area, unfortunately ;-/

Might make sense to reconsider it next time we bump gcc minimum...

Speaking of fun gcc bugs: prior to 11.1 gcc would not strip qualifiers in conditional operator; I hadn't tried to RTFS, but it almost looks like they took the union of qualifiers on the second and the third arguments of ?:

That's a direct violation of standard, all way back to C90 - the type of 0 ? x : x where x is an l-value of qualified type *is* explicitly required to be the unqualified version of that type; C90#6.2.2.1 does list the contexts where l-value is not converted to non-l-value and ?:
arguments are not among those, with clearly stated requirement to strip qualifiers when converting to non-l-value.

Once upon a time gcc used to have a weird extension that made (a ? b : c) an l-value if both b and c had been, which might explain the origin of that bug, but that went further - even in cases like

	const int x;
	__typeof__(0 ? x : 1) y;

they ended with const leaking to y, which would be a bug even in C++, where that extension for ?: originated (prvalue int as the third argument ends up with lvalue-to-rvalue conversions applied to the second one, stripping any qualifiers from it)...

#11 lore raw
Quoted 43 lines · On Mon, 12 Jan 2026 21:16:25 +0000 Al Viro <[email protected]> wrote:
Quoted 21 lines · Al Viro
Quoted 6 lines · Peter Zijlstra
Quoted 6 lines

#define unqual_non_array(T) __typeof__(((T(*)(void))0)())

would do the right thing without that _Generic cascade and it'll work just fine for e.g. kuid_t. Using it for an array would trigger an error, array-returning functions being forbidden...

Quoted 8 lines
Quoted 2 lines

Guys, do you have any problems with replacing __unqual_scalar_typeof() uses with that thing?

There is also __typeof_unqual__, but I do not know if that is now supported by all compilers, if so that is the better option. If not, your function return type thing is awesome.

From experimenting with godbolt.org:

			clang		gcc		icc

__typeof_unqual__ >= 19.0.1 >= 14.1 no
this trick >= 3.0.0 >= 8.4 >= 13.0.1
our minima 15.0.0 8.1

Quoted 5 lines

So __typeof_unqual__ is well out of our range; this trick is slightly out of range, but nowhere near as bad. Prior to 8.4 gcc had a bug in that area, unfortunately ;-/

Might make sense to reconsider it next time we bump gcc minimum...

Speaking of fun gcc bugs: prior to 11.1 gcc would not strip qualifiers in conditional operator; I hadn't tried to RTFS, but it almost looks like they took the union of qualifiers on the second and the third arguments of ?:

That's a direct violation of standard, all way back to C90 - the type of 0 ? x : x where x is an l-value of qualified type *is* explicitly required to be the unqualified version of that type; C90#6.2.2.1 does list the contexts where l-value is not converted to non-l-value and ?:
arguments are not among those, with clearly stated requirement to strip qualifiers when converting to non-l-value.

Once upon a time gcc used to have a weird extension that made (a ? b : c) an l-value if both b and c had been, which might explain the origin of that bug, but that went further - even in cases like

Quoted 7 lines
	const int x;
	__typeof__(0 ? x : 1) y;

they ended with const leaking to y, which would be a bug even in C++, where that extension for ?: originated (prvalue int as the third argument ends up with lvalue-to-rvalue conversions applied to the second one, stripping any qualifiers from it)...

I got a warning from gcc for your example (massaged a bit to compile):
<source>: In function 'f':
<source>:5:18: warning: type qualifiers ignored on function return type [-Wignored-qualifiers]

    5 |         typeof(((typeof(x)(*)(void))0)()) y;

Using __auto_type y = (1 ? 0 : 0+x) gives a non-const 'y' even with gcc 10. The 0 seems to be needed, none of +x, -x or ~x 'lose' the constness. Note that char/short get promoted to int - but that is hard to avoid, and happens as soon as you use the value of a char/short variable, ?: should perform integer promotion.

I've a grep of __unqual_scalar_typeof() on my 'other monitor' (one of them) from earlier. I'm sure most of them aren't needed at all. arm64/..../barrier.h looks very strange - the actual accesses just depend on the size of the item (even the union looks odd to me).

David

#12 lore raw
Quoted 1 line · David Laight

Using __auto_type y = (1 ? 0 : 0+x) gives a non-const 'y' even with gcc 10.

... which does not work at all for the example that has started that thread:

typedef struct {
        uid_t val;
} kuid_t;

Arithmetics is not an option for existing uses. , and ?: would be OK, but they are even worse than return type - at least that got fixed in 8.4.

As for the -Wignored-qualifiers... Sure, that's included into -Wextra, along with many other insanities - -Wunused-parameter, for example. Grep for -Wextra and you'll see...

#15 lore raw
In reply to Al Viro
Quoted 16 lines · Al Viro

Sure, but what does that have to do with the final cast in there? Note that generic __READ_ONCE() ends up with __unqual_scalar_typeof(x) for the type of result and it seems to be working fine.

Why would the same type of result be a problem for alpha? Sure, we need a barrier there, so it can't be literal copy of generic __READ_ONCE(), but what's the problem with using

#define __READ_ONCE(x) \

({									\
	__unqual_scalar_typeof(x) __x =					\
 		(*(volatile typeof(__x) *)(&(x)));			\
	mb();								\
	__x;								\
})
Quoted 2 lines

there? What could possibly need those qualifiers added back to the result? It is, after all, an r-value, so...

Yeah, I'm not sure about the final cast, that was probably trying to add 'const' back.