public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: David Laight <david.laight.linux@gmail.com>
To: Al Viro <viro@zeniv.linux.org.uk>
Cc: "Peter Zijlstra" <peterz@infradead.org>,
	"Linus Torvalds" <torvalds@linux-foundation.org>,
	"Eric Dumazet" <edumazet@google.com>,
	oe-kbuild-all@lists.linux.dev, linux-kernel@vger.kernel.org,
	"Jakub Kicinski" <kuba@kernel.org>,
	"Maciej Żenczykowski" <maze@google.com>,
	"Will Deacon" <will@kernel.org>,
	"Paul E. McKenney" <paulmck@kernel.org>
Subject: Re: include/net/sock.h:2100:16: sparse: sparse: cast to non-scalar
Date: Mon, 12 Jan 2026 22:39:23 +0000	[thread overview]
Message-ID: <20260112223923.78784af1@pumpkin> (raw)
In-Reply-To: <20260112211625.GL3634291@ZenIV>

On Mon, 12 Jan 2026 21:16:25 +0000
Al Viro <viro@zeniv.linux.org.uk> wrote:

> On Mon, Jan 12, 2026 at 07:21:26PM +0000, Al Viro wrote:
> > On Mon, Jan 12, 2026 at 01:37:22PM +0100, Peter Zijlstra wrote:
> >   
> > > > #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.  
> >   
> > >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...  
> 
> 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)...
> 

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


  reply	other threads:[~2026-01-13  0:15 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-01-10 21:06 include/net/sock.h:2100:16: sparse: sparse: cast to non-scalar kernel test robot
2026-01-10 22:15 ` Al Viro
2026-01-10 22:35   ` Al Viro
2026-01-11 10:08     ` Eric Dumazet
2026-01-12 12:33       ` Peter Zijlstra
2026-01-11 18:20     ` Al Viro
2026-01-11 18:51       ` Al Viro
2026-01-12 12:37       ` Peter Zijlstra
2026-01-12 15:02         ` Will Deacon
2026-01-12 19:21         ` Al Viro
2026-01-12 21:16           ` Al Viro
2026-01-12 22:39             ` David Laight [this message]
2026-01-13  0:28               ` Al Viro
2026-01-12 12:32     ` Peter Zijlstra
2026-01-12 19:30       ` Al Viro
2026-01-13 15:27         ` Peter Zijlstra
2026-01-12  0:49   ` Philip Li
  -- strict thread matches above, loose matches on Subject: below --
2025-12-06 10:09 kernel test robot
2025-08-25  4:45 kernel test robot

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=20260112223923.78784af1@pumpkin \
    --to=david.laight.linux@gmail.com \
    --cc=edumazet@google.com \
    --cc=kuba@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=maze@google.com \
    --cc=oe-kbuild-all@lists.linux.dev \
    --cc=paulmck@kernel.org \
    --cc=peterz@infradead.org \
    --cc=torvalds@linux-foundation.org \
    --cc=viro@zeniv.linux.org.uk \
    --cc=will@kernel.org \
    /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