[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <CAHk-=wiPUfe8aji5KojAhDKjWhJJU2F9kfzyL660=jRkY+Uzyg@mail.gmail.com>
Date: Wed, 15 Mar 2023 16:21:17 -0700
From: Linus Torvalds <torvalds@...ux-foundation.org>
To: Eric Dumazet <edumazet@...gle.com>
Cc: Jakub Kicinski <kuba@...nel.org>,
"David S . Miller" <davem@...emloft.net>,
Paolo Abeni <pabeni@...hat.com>,
David Ahern <dsahern@...nel.org>, netdev@...r.kernel.org,
eric.dumazet@...il.com,
Greg Kroah-Hartman <gregkh@...uxfoundation.org>
Subject: Re: [PATCH net-next 1/8] inet: preserve const qualifier in inet_sk()
On Wed, Mar 15, 2023 at 3:38 PM Eric Dumazet <edumazet@...gle.com> wrote:
>
> Maybe something like this?
Please no.
> +#define promote_to_type(ptr, oldtype, newtype) \
> + _Generic(ptr, \
> + const oldtype *: ((const newtype *)(ptr)), \
> + oldtype *: ((newtype *)(ptr)) \
> + )
That's just a very ugly way to just do a cast. It's wrong.
> +#define inet_sk(sk) promote_to_type(sk, struct sock, struct inet_sock)
This is horrid.
Why isn't this just doing
#define inet_sk(ptr) container_of(ptr, struct inet_sock, sk)
which is different from a plain cast in that it actually checks that
"yes, struct inet_sock has a member called 'sk' that has the right
type, so now we can convert from that sk to the containing structure".
That's very different from just randomly casting a pointer to another
pointer, like the current inet_sk() does, and like that disgusting
promote_to_type() macro does.
We really strive for proper type safety in the kernel. And that very
much means *not* doing random casts.
At least that "inet_sk(sk)" version using generics didn't take random
pointer types. But I really don't see why you don't just use
"container_of()", which is actually type-safe, and would allow "struct
inet_sock" to contain the "struct sock" somewhere else than in the
first field.
Hmm? Am I missing something that is happening in linux-next?
Linus
Powered by blists - more mailing lists