net: better const qualifier awareness
Subsystems (9)
cover net: better const qualifier awareness
This is a follow-up of d27d367d3b78 ("inet: better const qualifier awareness")
Adopting container_of_const() to perform (struct sock *)->(protocol sock *) operation is allowing us to propagate const qualifier and thus detect misuses at compile time.
Most conversions are trivial, because most protocols did not adopt yet const sk pointers where it could make sense.
Only mptcp and tcp patches (end of this series) are requiring small adjustments.
Thanks !
Eric Dumazet (10):
udp: preserve const qualifier in udp_sk()
af_packet: preserve const qualifier in pkt_sk()
raw: preserve const qualifier in raw_sk() ipv6: raw: preserve const qualifier in raw6_sk() dccp: preserve const qualifier in dccp_sk() af_unix: preserve const qualifier in unix_sk() smc: preserve const qualifier in smc_sk() x25: preserve const qualifier in [a]x25_sk() mptcp: preserve const qualifier in mptcp_sk() tcp: preserve const qualifier in tcp_sk()
include/linux/dccp.h | 6 ++---- include/linux/ipv6.h | 5 +---- include/linux/tcp.h | 10 ++++++---- include/linux/udp.h | 5 +---- include/net/af_unix.h | 5 +---- include/net/ax25.h | 5 +---- include/net/raw.h | 5 +---- include/net/tcp.h | 2 +- include/net/x25.h | 5 +---- net/ipv4/tcp.c | 2 +- net/ipv4/tcp_input.c | 4 ++-- net/ipv4/tcp_minisocks.c | 5 +++-- net/ipv4/tcp_output.c | 9 +++++++-- net/ipv4/tcp_recovery.c | 2 +- net/mptcp/protocol.c | 2 +- net/mptcp/protocol.h | 9 +++------ net/packet/internal.h | 5 +---- net/smc/smc.h | 5 +---- security/lsm_audit.c | 2 +- 19 files changed, 36 insertions(+), 57 deletions(-)
Signature · 1 line
2.40.0.rc2.332.ga46443480c-goog
PATCH 1/10 udp: preserve const qualifier in udp_sk()
We can change udp_sk() to propagate const qualifier of its argument, thanks to container_of_const()
This should avoid some potential errors caused by accidental (const -> not_const) promotion.
--- include/linux/udp.h | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-)
include/linux/udp.h +1 -4
@@ -97,10 +97,7 @@ struct udp_sock {#define UDP_MAX_SEGMENTS (1 << 6UL) -static inline struct udp_sock *udp_sk (const struct sock *sk) -{ -return (struct udp_sock *)sk; -} +#define udp_sk (ptr) container_of_const(ptr, struct udp_sock, inet.sk)static inline void udp_set_no_check6_tx (struct sock *sk,bool val) {
Signature · 1 line
2.40.0.rc2.332.ga46443480c-goog
Re: [PATCH net-next 01/10] udp: preserve const qualifier in udp_sk()
Quoted 8 lines · Eric Dumazet
We can change
udp_sk()to propagate const qualifier of its argument, thanks tocontainer_of_const()This should avoid some potential errors caused by accidental (const ->
not_const) promotion.Signed-off-by Eric Dumazet <[email protected]>Cc Willem de Bruijn <[email protected]>
PATCH 2/10 af_packet: preserve const qualifier in pkt_sk()
In reply to Eric DumazetWe can change pkt_sk() to propagate const qualifier of its argument, thanks to container_of_const()
This should avoid some potential errors caused by accidental (const -> not_const) promotion.
--- net/packet/internal.h | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-)
net/packet/internal.h +1 -4
@@ -133,10 +133,7 @@ struct packet_sock {atomic_t tp_drops____cacheline_aligned_in_smp ; }; -static inline struct packet_sock *pkt_sk (struct sock *sk) -{ -return (struct packet_sock *)sk; -} +#define pkt_sk (ptr) container_of_const(ptr, struct packet_sock, sk)enum packet_sock_flags {PACKET_SOCK_ORIGDEV ,
Signature · 1 line
2.40.0.rc2.332.ga46443480c-goog
Re: [PATCH net-next 02/10] af_packet: preserve const qualifier in pkt_sk()
Quoted 8 lines · Eric Dumazet
We can change
pkt_sk()to propagate const qualifier of its argument, thanks tocontainer_of_const()This should avoid some potential errors caused by accidental (const ->
not_const) promotion.Signed-off-by Eric Dumazet <[email protected]>Cc Willem de Bruijn <[email protected]>
PATCH 3/10 raw: preserve const qualifier in raw_sk()
In reply to Eric DumazetWe can change raw_sk() to propagate const qualifier of its argument, thanks to container_of_const()
--- include/net/raw.h | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-)
include/net/raw.h +1 -4
@@ -83,10 +83,7 @@ struct raw_sock {u32 ipmr_table; }; -static inline struct raw_sock *raw_sk (const struct sock *sk) -{ -return (struct raw_sock *)sk; -} +#define raw_sk (ptr) container_of_const(ptr, struct raw_sock, inet.sk)static inline bool raw_sk_bound_dev_eq (struct net *net,int bound_dev_if,int dif,int sdif)
Signature · 1 line
2.40.0.rc2.332.ga46443480c-goog
Re: [PATCH net-next 03/10] raw: preserve const qualifier in raw_sk()
Quoted 4 lines · Eric Dumazet
We can change
raw_sk()to propagate const qualifier of its argument, thanks tocontainer_of_const()Signed-off-by Eric Dumazet <[email protected]>
PATCH 4/10 ipv6: raw: preserve const qualifier in raw6_sk()
In reply to Eric DumazetWe can change raw6_sk() to propagate its argument const qualifier, thanks to container_of_const().
--- include/linux/ipv6.h | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-)
include/linux/ipv6.h +1 -4
@@ -336,10 +336,7 @@ static inline struct ipv6_pinfo *inet6_sk(const struct sock *__sk)return sk_fullsock (__sk) ?inet_sk (__sk)->pinet6 :NULL ; } -static inline struct raw6_sock *raw6_sk (const struct sock *sk) -{ -return (struct raw6_sock *)sk; -} +#define raw6_sk (ptr) container_of_const(ptr, struct raw6_sock, inet.sk)#define ipv6_only_sock (sk) (sk->sk_ipv6only)#define ipv6_sk_rxinfo (sk) ((sk)->sk_family == PF_INET6 && \
Signature · 1 line
2.40.0.rc2.332.ga46443480c-goog
Re: [PATCH net-next 04/10] ipv6: raw: preserve const qualifier in raw6_sk()
Quoted 4 lines · Eric Dumazet
We can change
raw6_sk()to propagate its argument const qualifier, thanks tocontainer_of_const().Signed-off-by Eric Dumazet <[email protected]>
PATCH 5/10 dccp: preserve const qualifier in dccp_sk()
In reply to Eric DumazetWe can change dccp_sk() to propagate its argument const qualifier, thanks to container_of_const().
--- include/linux/dccp.h | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-)
include/linux/dccp.h +2 -4
@@ -305,10 +305,8 @@ struct dccp_sock {struct timer_list dccps_xmit_timer; }; -static inline struct dccp_sock *dccp_sk (const struct sock *sk) -{ -return (struct dccp_sock *)sk; -} +#define dccp_sk (ptr) container_of_const(ptr, struct dccp_sock, \ + dccps_inet_connection.icsk_inet.sk)static inline const char *dccp_role (const struct sock *sk) {
Signature · 1 line
2.40.0.rc2.332.ga46443480c-goog
Re: [PATCH net-next 05/10] dccp: preserve const qualifier in dccp_sk()
Quoted 4 lines · Eric Dumazet
We can change
dccp_sk()to propagate its argument const qualifier, thanks tocontainer_of_const().Signed-off-by Eric Dumazet <[email protected]>
PATCH 6/10 af_unix: preserve const qualifier in unix_sk()
In reply to Eric DumazetWe can change unix_sk() to propagate its argument const qualifier, thanks to container_of_const().
We need to change dump_common_audit_data() 'struct unix_sock *u' local var to get a const attribute.
--- include/net/af_unix.h | 5 +---- security/lsm_audit.c | 2 +- 2 files changed, 2 insertions(+), 5 deletions(-)
include/net/af_unix.h +1 -4
@@ -74,10 +74,7 @@ struct unix_sock {#endif }; -static inline struct unix_sock *unix_sk (const struct sock *sk) -{ -return (struct unix_sock *)sk; -} +#define unix_sk (ptr) container_of_const(ptr, struct unix_sock, sk)#define peer_wait peer_wq.wait
security/lsm_audit.c +1 -1
@@ -310,7 +310,7 @@ static void dump_common_audit_data(struct audit_buffer *ab,case LSM_AUDIT_DATA_NET :if (a->u.net->sk) {const struct sock *sk = a->u.net->sk; -struct unix_sock *u; +const struct unix_sock *u;struct unix_address *addr;int len =0 ;char *p =NULL ;
Signature · 1 line
2.40.0.rc2.332.ga46443480c-goog
Re: [PATCH net-next 06/10] af_unix: preserve const qualifier in unix_sk()
Quoted 7 lines · Eric Dumazet
We can change
unix_sk()to propagate its argument const qualifier, thanks tocontainer_of_const().We need to change
dump_common_audit_data()'structunix_sock*u' local var to get a const attribute.Signed-off-by Eric Dumazet <[email protected]>
PATCH 7/10 smc: preserve const qualifier in smc_sk()
In reply to Eric DumazetWe can change smc_sk() to propagate its argument const qualifier, thanks to container_of_const().
--- net/smc/smc.h | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-)
net/smc/smc.h +1 -4
@@ -283,10 +283,7 @@ struct smc_sock { /* smc sock container */* */ }; -static inline struct smc_sock *smc_sk (const struct sock *sk) -{ -return (struct smc_sock *)sk; -} +#define smc_sk (ptr) container_of_const(ptr, struct smc_sock, sk)static inline void smc_init_saved_callbacks (struct smc_sock *smc) {
Signature · 1 line
2.40.0.rc2.332.ga46443480c-goog
Re: [PATCH net-next 07/10] smc: preserve const qualifier in smc_sk()
Quoted 7 lines · Eric Dumazet
We can change
smc_sk()to propagate its argument const qualifier, thanks tocontainer_of_const().Signed-off-by Eric Dumazet <[email protected]>Cc Karsten Graul <[email protected]>Cc Wenjia Zhang <[email protected]>Cc Jan Karcher <[email protected]>
Re: [PATCH net-next 07/10] smc: preserve const qualifier in smc_sk()
In reply to Eric DumazetQuoted 7 lines · Eric Dumazet
We can change
smc_sk()to propagate its argument const qualifier, thanks tocontainer_of_const().Signed-off-by Eric Dumazet <[email protected]>Cc Karsten Graul <[email protected]>Cc Wenjia Zhang <[email protected]>Cc Jan Karcher <[email protected]>
Quoted 20 lines
--- net/smc/smc.h | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-)net/smc/smc.h +1
-4@@ -283,10 +283,7 @@ struct smc_sock { /* smc sock container */* */ }; -static inline struct smc_sock *smc_sk (const struct sock *sk) -{ -return (struct smc_sock *)sk; -} +#define smc_sk (ptr) container_of_const(ptr, struct smc_sock, sk)static inline void smc_init_saved_callbacks (struct smc_sock *smc) {
PATCH 8/10 x25: preserve const qualifier in [a]x25_sk()
In reply to Eric DumazetWe can change [a]x25_sk() to propagate their argument const qualifier, thanks to container_of_const().
--- include/net/ax25.h | 5 +---- include/net/x25.h | 5 +---- 2 files changed, 2 insertions(+), 8 deletions(-)
include/net/ax25.h +1 -4
@@ -260,10 +260,7 @@ struct ax25_sock {struct ax25_cb *cb; }; -static inline struct ax25_sock *ax25_sk (const struct sock *sk) -{ -return (struct ax25_sock *) sk; -} +#define ax25_sk (ptr) container_of_const(ptr, struct ax25_sock, sk)static inline struct ax25_cb *sk_to_ax25 (const struct sock *sk) {
include/net/x25.h +1 -4
@@ -177,10 +177,7 @@ struct x25_forward {atomic_t refcnt; }; -static inline struct x25_sock *x25_sk (const struct sock *sk) -{ -return (struct x25_sock *)sk; -} +#define x25_sk (ptr) container_of_const(ptr, struct x25_sock, sk)/* af_x25.c */ extern int sysctl_x25_restart_request_timeout;
Signature · 1 line
2.40.0.rc2.332.ga46443480c-goog
Re: [PATCH net-next 08/10] x25: preserve const qualifier in [a]x25_sk()
Quoted 4 lines · Eric Dumazet
We can change [a]x25_sk() to propagate their argument const qualifier, thanks to
container_of_const().Signed-off-by Eric Dumazet <[email protected]>
PATCH 9/10 mptcp: preserve const qualifier in mptcp_sk()
In reply to Eric DumazetWe can change mptcp_sk() to propagate its argument const qualifier, thanks to container_of_const().
We need to change few things to avoid build errors:
mptcp_set_datafin_timeout() and mptcp_rtx_head() have to accept non-const sk pointers.
@msk local variable in mptcp_pending_tail() must be const.
--- net/mptcp/protocol.c | 2 +- net/mptcp/protocol.h | 9 +++------ 2 files changed, 4 insertions(+), 7 deletions(-)
net/mptcp/protocol.c +1 -1
@@ -459,7 +459,7 @@ static bool mptcp_pending_data_fin(struct sock *sk, u64 *seq)return false; } -static void mptcp_set_datafin_timeout (const struct sock *sk) +static void mptcp_set_datafin_timeout (struct sock *sk) {struct inet_connection_sock *icsk =inet_csk (sk);u32 retransmits;
net/mptcp/protocol.h +3 -6
@@ -333,10 +333,7 @@ static inline void msk_owned_by_me(const struct mptcp_sock *msk)sock_owned_by_me ((const struct sock *)msk); } -static inline struct mptcp_sock *mptcp_sk (const struct sock *sk) -{ -return (struct mptcp_sock *)sk; -} +#define mptcp_sk (ptr) container_of_const(ptr, struct mptcp_sock, sk.icsk_inet.sk)/* the msk socket don't use the backlog, also account for the bulk * free memory
@@ -370,7 +367,7 @@ static inline struct mptcp_data_frag *mptcp_send_next(struct sock *sk)static inline struct mptcp_data_frag *mptcp_pending_tail (const struct sock *sk) { -struct mptcp_sock *msk =mptcp_sk (sk); +const struct mptcp_sock *msk =mptcp_sk (sk);if (!msk->first_pending)return NULL ;
@@ -381,7 +378,7 @@ static inline struct mptcp_data_frag *mptcp_pending_tail(const struct sock *sk) return list_last_entry(&msk->rtx_queue, struct mptcp_data_frag, list); } -static inline struct mptcp_data_frag *mptcp_rtx_head (const struct sock *sk) +static inline struct mptcp_data_frag *mptcp_rtx_head (struct sock *sk) {struct mptcp_sock *msk =mptcp_sk (sk);
Signature · 1 line
2.40.0.rc2.332.ga46443480c-goog
Re: [PATCH net-next 09/10] mptcp: preserve const qualifier in mptcp_sk()
Quoted 5 lines · Eric Dumazet
We can change
mptcp_sk()to propagate its argument const qualifier, thanks tocontainer_of_const().We need to change few things to avoid build errors:
Quoted 7 lines
mptcp_set_datafin_timeout()andmptcp_rtx_head()have to accept non-const sk pointers.@msk local variable in
mptcp_pending_tail()must be const.Signed-off-by Eric Dumazet <[email protected]>Cc Matthieu Baerts <[email protected]>
Re: [PATCH net-next 09/10] mptcp: preserve const qualifier in mptcp_sk()
In reply to Eric DumazetHi Eric,
Quoted 5 lines · Eric Dumazet
We can change
mptcp_sk()to propagate its argument const qualifier, thanks tocontainer_of_const().We need to change few things to avoid build errors:
Quoted 7 lines
mptcp_set_datafin_timeout()andmptcp_rtx_head()have to accept non-const sk pointers.@msk local variable in
mptcp_pending_tail()must be const.Signed-off-by Eric Dumazet <[email protected]>Cc Matthieu Baerts <[email protected]>
Good idea!
Thank you for this patch and for having Cced me.
It looks good to me. I just have one question below if you don't mind.
(...)
Quoted 4 lines
net/mptcp/protocol.h +0
-0
(...)
net/mptcp/protocol.h static inline struct mptcp_data_frag *mptcp_pending_tail(const struct sock *sk) · line 381
Quoted 6 lines
net/mptcp/protocol.h +1
-1@@ -381,7 +378,7 @@ static inline struct mptcp_data_frag *mptcp_pending_tail(const struct sock *sk) return list_last_entry(&msk->rtx_queue, struct mptcp_data_frag, list); } -static inline struct mptcp_data_frag *mptcp_rtx_head (const struct sock *sk) +static inline struct mptcp_data_frag *mptcp_rtx_head (struct sock *sk)
It was not clear to me why you had to remove the "const" qualifier here and not just have to add one when assigning the msk just below. But then I looked at what was behind the list_first_entry_or_null() macro used in this function and understood what was the issue.
My naive approach would be to modify this macro but I guess we don't want to go down that road, right?
-------------------- 8< --------------------
include/linux/list.h +0 -0
@@ -550,7 +550,7 @@ static inline void list_splice_tail_init(struct
list_head *list,
* Note that if the list is empty, it returns NULL. */
#define list_first_entry_or_null(ptr, type, member) ({ \
- struct list_head *head__ = (ptr); \
+ const struct list_head *head__ = (ptr); \
struct list_head *pos__ = READ_ONCE(head__->next); \
pos__ != head__ ? list_entry(pos__, type, member) : NULL; \ })
-------------------- 8< --------------------
It looks safe to me to do that but I would not trust myself on a Friday evening :)
(I'm sure I'm missing something, I'm sorry if it is completely wrong)
Anyway if we cannot modify list_first_entry_or_null() one way or another, I'm fine with your modification!
Cheers,
Matt
Quoted 3 lines
net/mptcp/protocol.h +0
-0{struct mptcp_sock *msk =mptcp_sk (sk);
Signature · 2 lines
Tessares | Belgium | Hybrid Access Solutions www.tessares.net
Re: [PATCH net-next 09/10] mptcp: preserve const qualifier in mptcp_sk()
Quoted 62 lines · Matthieu Baerts
Hi Eric,
Quoted 5 lines · Eric Dumazet
We can change
mptcp_sk()to propagate its argument const qualifier, thanks tocontainer_of_const().We need to change few things to avoid build errors:
Quoted 7 lines
mptcp_set_datafin_timeout()andmptcp_rtx_head()have to accept non-const sk pointers.@msk local variable in
mptcp_pending_tail()must be const.Signed-off-by Eric Dumazet <[email protected]>Cc Matthieu Baerts <[email protected]>Good idea!
Thank you for this patch and for having Cced me.
It looks good to me. I just have one question below if you don't mind.
(...)
Quoted 4 lines
net/mptcp/protocol.h +0
-0(...)
net/mptcp/protocol.h static inline struct mptcp_data_frag *mptcp_pending_tail(const struct sock *sk) · line 381
Quoted 6 lines
net/mptcp/protocol.h +1
-1@@ -381,7 +378,7 @@ static inline struct mptcp_data_frag *mptcp_pending_tail(const struct sock *sk) return list_last_entry(&msk->rtx_queue, struct mptcp_data_frag, list); } -static inline struct mptcp_data_frag *mptcp_rtx_head (const struct sock *sk) +static inline struct mptcp_data_frag *mptcp_rtx_head (struct sock *sk)It was not clear to me why you had to remove the "const" qualifier here and not just have to add one when assigning the msk just below. But then I looked at what was behind the
list_first_entry_or_null()macro used in this function and understood what was the issue.My naive approach would be to modify this macro but I guess we don't want to go down that road, right?
-------------------- 8< --------------------
include/linux/list.h +1
-1@@ -550,7 +550,7 @@ static inline void list_splice_tail_init(structlist_head *list, * Note that if the list is empty, it returns NULL. */ #define list_first_entry_or_null (ptr, type, member) ({ \ - struct list_head *head__ = (ptr); \ + const struct list_head *head__ = (ptr); \ struct list_head *pos__ = READ_ONCE(head__->next); \ pos__ != head__ ? list_entry(pos__, type, member) : NULL; \
Quoted 2 lines
})
-------------------- 8< --------------------
This could work, but it is a bit awkward.
mptcp_rtx_head() is used in a context where we are changing the socket, not during a readonly lookup ?
Quoted 11 lines
It looks safe to me to do that but I would not trust myself on a Friday evening :)
(I'm sure I'm missing something, I'm sorry if it is completely wrong)Anyway if we cannot modify
list_first_entry_or_null()one way or another, I'm fine with your modification!Reviewed-by Matthieu Baerts <[email protected]>
Thanks !
Re: [PATCH net-next 09/10] mptcp: preserve const qualifier in mptcp_sk()
Hi Eric,
Thank you for your quick reply!
Quoted 48 lines · Eric Dumazet
Quoted 41 lines · Matthieu Baerts
On 17/03/2023 16:55, Eric Dumazet wrote:
(...)
Quoted 4 lines
net/mptcp/protocol.h +0
-0(...)
net/mptcp/protocol.h static inline struct mptcp_data_frag *mptcp_pending_tail(const struct sock *sk) · line 381
Quoted 6 lines
net/mptcp/protocol.h +1
-1@@ -381,7 +378,7 @@ static inline struct mptcp_data_frag *mptcp_pending_tail(const struct sock *sk) return list_last_entry(&msk->rtx_queue, struct mptcp_data_frag, list); } -static inline struct mptcp_data_frag *mptcp_rtx_head (const struct sock *sk) +static inline struct mptcp_data_frag *mptcp_rtx_head (struct sock *sk)It was not clear to me why you had to remove the "const" qualifier here and not just have to add one when assigning the msk just below. But then I looked at what was behind the
list_first_entry_or_null()macro used in this function and understood what was the issue.My naive approach would be to modify this macro but I guess we don't want to go down that road, right?
-------------------- 8< --------------------
include/linux/list.h +1
-1@@ -550,7 +550,7 @@ static inline void list_splice_tail_init(structlist_head *list, * Note that if the list is empty, it returns NULL. */ #define list_first_entry_or_null (ptr, type, member) ({ \ - struct list_head *head__ = (ptr); \ + const struct list_head *head__ = (ptr); \ struct list_head *pos__ = READ_ONCE(head__->next); \ pos__ != head__ ? list_entry(pos__, type, member) : NULL; \Quoted 2 lines
})
-------------------- 8< --------------------This could work, but it is a bit awkward.
Quoted 2 lines
mptcp_rtx_head()is used in a context where we are changing the socket, not during a readonly lookup ?
Indeed, you are right. It is currently only used in a context where we are changing the socket.
I can see cases where a new packets scheduler might just need to check if the rtx queue is empty but then we should probably add a new helper using list_empty() instead.
So yes, no need to change anything!
Cheers,
Matt
Signature · 2 lines
Tessares | Belgium | Hybrid Access Solutions www.tessares.net
PATCH 10/10 tcp: preserve const qualifier in tcp_sk()
In reply to Eric DumazetWe can change tcp_sk() to propagate its argument const qualifier, thanks to container_of_const().
We have two places where a const sock pointer has to be upgraded to a write one. We have been using const qualifier for lockless listeners to clearly identify points where writes could happen.
Add tcp_sk_rw() helper to better document these.
tcp_inbound_md5_hash(), __tcp_grow_window(), tcp_reset_check() and tcp_rack_reo_wnd() get an additional const qualififer for their @tp local variables.
smc_check_reset_syn_req() also needs a similar change.
--- include/linux/tcp.h | 10 ++++++---- include/net/tcp.h | 2 +- net/ipv4/tcp.c | 2 +- net/ipv4/tcp_input.c | 4 ++-- net/ipv4/tcp_minisocks.c | 5 +++-- net/ipv4/tcp_output.c | 9 +++++++-- net/ipv4/tcp_recovery.c | 2 +- 7 files changed, 21 insertions(+), 13 deletions(-)
include/linux/tcp.h +6 -4
@@ -472,10 +472,12 @@ enum tsq_flags { TCPF_MTU_REDUCED_DEFERRED = (1UL << TCP_MTU_REDUCED_DEFERRED), }; -static inline struct tcp_sock *tcp_sk (const struct sock *sk) -{ -return (struct tcp_sock *)sk; -} +#define tcp_sk (ptr) container_of_const(ptr, struct tcp_sock, inet_conn.icsk_inet.sk) + +/* Variant of tcp_sk() upgrading a const sock to a read/write tcp socket. +* Used in context of (lockless) tcp listeners. +*/ +#define tcp_sk_rw (ptr) container_of(ptr, struct tcp_sock, inet_conn.icsk_inet.sk)struct tcp_timewait_sock {struct inet_timewait_sock tw_sk;
include/net/tcp.h +1 -1
@@ -529,7 +529,7 @@ static inline void tcp_synq_overflow(const struct sock *sk) last_overflow =READ_ONCE (tcp_sk (sk)->rx_opt.ts_recent_stamp);if (!time_between32 (now, last_overflow, last_overflow +HZ )) -WRITE_ONCE (tcp_sk (sk)->rx_opt.ts_recent_stamp, now); +WRITE_ONCE (tcp_sk_rw (sk)->rx_opt.ts_recent_stamp, now); }/* syncookies: no recent synqueue overflow on this listening socket? */
net/ipv4/tcp.c +1 -1
@@ -4570,7 +4570,7 @@ tcp_inbound_md5_hash(const struct sock *sk, const struct sk_buff *skb,const __u8 *hash_location =NULL ;struct tcp_md5sig_key *hash_expected;const struct tcphdr *th =tcp_hdr (skb); -struct tcp_sock *tp =tcp_sk (sk); +const struct tcp_sock *tp =tcp_sk (sk);int genhash, l3index;u8 newhash[16 ];
net/ipv4/tcp_input.c +2 -2
@@ -458,7 +458,7 @@ static void tcp_sndbuf_expand(struct sock *sk)static int __tcp_grow_window (const struct sock *sk,const struct sk_buff *skb,unsigned int skbtruesize) { -struct tcp_sock *tp =tcp_sk (sk); +const struct tcp_sock *tp =tcp_sk (sk);/* Optimize this! */ int truesize =tcp_win_from_space (sk, skbtruesize) >>1 ;int window =tcp_win_from_space (sk,READ_ONCE (sock_net (sk)->ipv4.sysctl_tcp_rmem[2 ])) >>1 ;
@@ -5693,7 +5693,7 @@ static void tcp_urg(struct sock *sk, struct sk_buff *skb, const struct tcphdr *t*/ static bool tcp_reset_check(const struct sock *sk, const struct sk_buff *skb) { - struct tcp_sock *tp = tcp_sk(sk); + const struct tcp_sock *tp = tcp_sk(sk); return unlikely(TCP_SKB_CB(skb)->seq == (tp->rcv_nxt - 1) && (1 << sk->sk_state) & (TCPF_CLOSE_WAIT | TCPF_LAST_ACK |
net/ipv4/tcp_minisocks.c +3 -2
@@ -463,7 +463,7 @@ void tcp_ca_openreq_child(struct sock *sk, const struct dst_entry *dst) }EXPORT_SYMBOL_GPL (tcp_ca_openreq_child); -static void smc_check_reset_syn_req (struct tcp_sock *oldtp, +static void smc_check_reset_syn_req (const struct tcp_sock *oldtp,struct request_sock *req,struct tcp_sock *newtp) {
@@ -492,7 +492,8 @@ struct sock *tcp_create_openreq_child(const struct sock *sk,const struct inet_request_sock *ireq =inet_rsk (req);struct tcp_request_sock *treq =tcp_rsk (req);struct inet_connection_sock *newicsk; -struct tcp_sock *oldtp, *newtp; +const struct tcp_sock *oldtp; +struct tcp_sock *newtp;u32 seq;if (!newsk)
net/ipv4/tcp_output.c +7 -2
@@ -4127,8 +4127,13 @@ int tcp_rtx_synack(const struct sock *sk, struct request_sock *req)if (!res) {TCP_INC_STATS (sock_net (sk),TCP_MIB_RETRANSSEGS );NET_INC_STATS (sock_net (sk),LINUX_MIB_TCPSYNRETRANS ); -if (unlikely (tcp_passive_fastopen (sk))) -tcp_sk (sk)->total_retrans++; +if (unlikely (tcp_passive_fastopen (sk))) { +/* sk has const attribute because listeners are lockless. +* However in this case, we are dealing with a passive fastopen +* socket thus we can change total_retrans value. +*/ +tcp_sk_rw (sk)->total_retrans++; + }trace_tcp_retransmit_synack (sk, req); }return res;
net/ipv4/tcp_recovery.c +1 -1
@@ -4,7 +4,7 @@static u32 tcp_rack_reo_wnd (const struct sock *sk) { -struct tcp_sock *tp =tcp_sk (sk); +const struct tcp_sock *tp =tcp_sk (sk);if (!tp->reord_seen) {/* If reordering has not been observed, be aggressive during
Signature · 1 line
2.40.0.rc2.332.ga46443480c-goog
Re: [PATCH net-next 10/10] tcp: preserve const qualifier in tcp_sk()
Quoted 9 lines · Eric Dumazet
We can change
tcp_sk()to propagate its argument const qualifier, thanks tocontainer_of_const().We have two places where a const sock pointer has to be upgraded to a write one. We have been using const qualifier for lockless listeners to clearly identify points where writes could happen.
Add
tcp_sk_rw()helper to better document these.
Quoted 7 lines
tcp_inbound_md5_hash(),__tcp_grow_window(),tcp_reset_check()andtcp_rack_reo_wnd()get an additional const qualififer for their @tp local variables.
smc_check_reset_syn_req()also needs a similar change.Signed-off-by Eric Dumazet <[email protected]>
Upgrading gives me the heebie-jeebies, but ok.
This series was applied to netdev/net-next.git (main) by David S. Miller <[email protected]>:
Hello:
This series was applied to netdev/net-next.git (main) by David S. Miller <[email protected]>:
Quoted 10 lines · On Fri, 17 Mar 2023 15:55:29 +0000 you wrote:
This is a follow-up of
d27d367d3b78("inet: better const qualifier awareness")Adopting
container_of_const()to perform (struct sock *)->(protocol sock *) operation is allowing us to propagate const qualifier and thus detect misuses at compile time.Most conversions are trivial, because most protocols did not adopt yet const sk pointers where it could make sense.
[...]
Here is the summary with links:
- [net-next,01/10] udp: preserve const qualifier in udp_sk()
https://git.kernel.org/netdev/net-next/c/94c540fbfc80 - [net-next,02/10] af_packet: preserve const qualifier in pkt_sk() https://git.kernel.org/netdev/net-next/c/68ac9a8b6e65 - [net-next,03/10] raw: preserve const qualifier in raw_sk() https://git.kernel.org/netdev/net-next/c/0a2db4630b72 - [net-next,04/10] ipv6: raw: preserve const qualifier in raw6_sk() https://git.kernel.org/netdev/net-next/c/47fcae28b9ec - [net-next,05/10] dccp: preserve const qualifier in dccp_sk() https://git.kernel.org/netdev/net-next/c/ae6084b73992 - [net-next,06/10] af_unix: preserve const qualifier in unix_sk() https://git.kernel.org/netdev/net-next/c/b064ba9c3cfa - [net-next,07/10] smc: preserve const qualifier in smc_sk() https://git.kernel.org/netdev/net-next/c/407db475d505 - [net-next,08/10] x25: preserve const qualifier in [a]x25_sk() https://git.kernel.org/netdev/net-next/c/c7154ca8e075 - [net-next,09/10] mptcp: preserve const qualifier in mptcp_sk() https://git.kernel.org/netdev/net-next/c/403a40f2304d - [net-next,10/10] tcp: preserve const qualifier in tcp_sk() https://git.kernel.org/netdev/net-next/c/e9d9da91548b
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html