[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <CAHk-=wjgW-aFo3qLyssg+76-XkYbeMaH58FwW5Bd3-baqfXqrQ@mail.gmail.com>
Date: Wed, 15 Mar 2023 16:39:38 -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 4:21 PM Linus Torvalds
<torvalds@...ux-foundation.org> wrote:
>
> 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.
I guess that doesn't work, because of how sk_alloc() and friends are set up.
But I do think the networking code should strive to avoid those
"randomly cast one socket type to another".
For example, when you have a TCP socket, instead of casting from
"struct sock *" to "struct tcp_sock *" in tcp_sk(), and getting it all
wrong wrt const, I think you should do the same thing:
#define tcp_sock(ptr) container_of(ptr, struct tcp_sock,
inet_conn.icsk_inet.sk)
to really *show* that you know that "yes, struct tcp_sock contains a
'sk' embedded three structures down!".
And no, I did not actually *test* that at all. But it should get
'const' right (because container_of() gets const right these days),
_and_ it actually verifies that the type you claim has another type
embedded in it actually does so.
Linus
Powered by blists - more mailing lists