[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <CANn89iLbOqjWVmgZKdGjbdsHw1EwO9d_w+dgKsyzLoq9pOsurQ@mail.gmail.com>
Date: Wed, 15 Mar 2023 15:37:50 -0700
From: Eric Dumazet <edumazet@...gle.com>
To: Jakub Kicinski <kuba@...nel.org>
Cc: "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>,
Linus Torvalds <torvalds@...ux-foundation.org>
Subject: Re: [PATCH net-next 1/8] inet: preserve const qualifier in inet_sk()
On Wed, Mar 15, 2023 at 2:28 PM Jakub Kicinski <kuba@...nel.org> wrote:
>
> On Wed, 15 Mar 2023 15:42:38 +0000 Eric Dumazet wrote:
> > -static inline struct inet_sock *inet_sk(const struct sock *sk)
> > -{
> > - return (struct inet_sock *)sk;
> > -}
> > +#define inet_sk(sk) \
> > + _Generic(sk, \
> > + const struct sock * : ((const struct inet_sock *)(sk)), \
> > + struct sock * : ((struct inet_sock *)(sk)) \
> > + )
>
> Could we possibly use container_of_const() or define a new common
> macro for this downcast? I'm worried it will spread and it's a bit
> verbose.
I did see container_of_const() but the default: case was not appealing to me.
Maybe something like this?
diff --git a/include/linux/container_of.h b/include/linux/container_of.h
index 713890c867bea78804defe1a015e3c362f40f85d..9a24d8db1f4c46166c07589bb084eda9b9ede8ba
100644
--- a/include/linux/container_of.h
+++ b/include/linux/container_of.h
@@ -35,4 +35,10 @@
default: ((type *)container_of(ptr, type, member)) \
)
+#define promote_to_type(ptr, oldtype, newtype) \
+ _Generic(ptr, \
+ const oldtype *: ((const newtype *)(ptr)), \
+ oldtype *: ((newtype *)(ptr)) \
+ )
+
#endif /* _LINUX_CONTAINER_OF_H */
diff --git a/include/net/inet_sock.h b/include/net/inet_sock.h
index 6eb8235be67f8b8265cd86782aed2b489e8850ee..96f41b3c1a3e4b6ad1c6d2058ca46686be282c03
100644
--- a/include/net/inet_sock.h
+++ b/include/net/inet_sock.h
@@ -305,11 +305,7 @@ static inline struct sock *skb_to_full_sk(const
struct sk_buff *skb)
return sk_to_full_sk(skb->sk);
}
-#define inet_sk(sk) \
- _Generic(sk, \
- const struct sock * : ((const struct inet_sock
*)(sk)), \
- struct sock * : ((struct inet_sock *)(sk)) \
- )
+#define inet_sk(sk) promote_to_type(sk, struct sock, struct inet_sock)
static inline void __inet_sk_copy_descendant(struct sock *sk_to,
const struct sock *sk_from,
Powered by blists - more mailing lists