tcp: add missing annotations
Subsystems (3)
cover tcp: add missing annotations
This series was inspired by one syzbot (KCSAN) report.
do_tcp_getsockopt() does not lock the socket, we need to annotate most of the reads there (and other places as well).
This is a first round, another series will come later.
Eric Dumazet (11):
tcp: annotate data-races around tp->tcp_tx_delay
tcp: annotate data-races around tp->tsoffset
tcp: annotate data-races around tp->keepalive_time
tcp: annotate data-races around tp->keepalive_intvl
tcp: annotate data-races around tp->keepalive_probes
tcp: annotate data-races around icsk->icsk_syn_retries
tcp: annotate data-races around tp->linger2
tcp: annotate data-races around rskq_defer_accept
tcp: annotate data-races around tp->notsent_lowat
tcp: annotate data-races around icsk->icsk_user_timeout
tcp: annotate data-races around fastopenq.max_qlen
include/linux/tcp.h | 2 +- include/net/tcp.h | 31 ++++++++++++++---- net/ipv4/inet_connection_sock.c | 2 +- net/ipv4/tcp.c | 57 +++++++++++++++++---------------- net/ipv4/tcp_fastopen.c | 6 ++-- net/ipv4/tcp_ipv4.c | 5 +-- 6 files changed, 63 insertions(+), 40 deletions(-)
Signature · 1 line
2.41.0.255.g8b1d071c50-goog
PATCH 1/11 tcp: annotate data-races around tp->tcp_tx_delay
do_tcp_getsockopt() reads tp->tcp_tx_delay while another cpu might change its value.
--- net/ipv4/tcp.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
net/ipv4/tcp.c +2 -2
@@ -3674,7 +3674,7 @@ int do_tcp_setsockopt(struct sock *sk, int level, int optname,case TCP_TX_DELAY :if (val)tcp_enable_tx_delay (); - tp->tcp_tx_delay = val; +WRITE_ONCE (tp->tcp_tx_delay, val);break ;default : err = -ENOPROTOOPT ;
@@ -4154,7 +4154,7 @@ int do_tcp_getsockopt(struct sock *sk, int level,break ;case TCP_TX_DELAY : - val = tp->tcp_tx_delay; + val =READ_ONCE (tp->tcp_tx_delay);break ;case TCP_TIMESTAMP :
Signature · 1 line
2.41.0.255.g8b1d071c50-goog
PATCH 2/11 tcp: annotate data-races around tp->tsoffset
In reply to Eric Dumazetdo_tcp_getsockopt() reads tp->tsoffset while another cpu might change its value.
--- net/ipv4/tcp.c | 4 ++-- net/ipv4/tcp_ipv4.c | 5 +++-- 2 files changed, 5 insertions(+), 4 deletions(-)
net/ipv4/tcp.c +2 -2
@@ -3656,7 +3656,7 @@ int do_tcp_setsockopt(struct sock *sk, int level, int optname,if (!tp->repair) err = -EPERM ;else - tp->tsoffset = val -tcp_time_stamp_raw (); +WRITE_ONCE (tp->tsoffset, val -tcp_time_stamp_raw ());break ;case TCP_REPAIR_WINDOW : err =tcp_repair_set_window (tp, optval, optlen);
@@ -4158,7 +4158,7 @@ int do_tcp_getsockopt(struct sock *sk, int level,break ;case TCP_TIMESTAMP : - val =tcp_time_stamp_raw () + tp->tsoffset; + val =tcp_time_stamp_raw () +READ_ONCE (tp->tsoffset);break ;case TCP_NOTSENT_LOWAT : val = tp->notsent_lowat;
net/ipv4/tcp_ipv4.c +3 -2
@@ -307,8 +307,9 @@ int tcp_v4_connect(struct sock *sk, struct sockaddr *uaddr, int addr_len) inet->inet_daddr, inet->inet_sport, usin->sin_port)); - tp->tsoffset =secure_tcp_ts_off (net, inet->inet_saddr, - inet->inet_daddr); +WRITE_ONCE (tp->tsoffset, +secure_tcp_ts_off (net, inet->inet_saddr, + inet->inet_daddr)); } inet->inet_id =get_random_u16 ();
Signature · 1 line
2.41.0.255.g8b1d071c50-goog
PATCH 3/11 tcp: annotate data-races around tp->keepalive_time
In reply to Eric Dumazetdo_tcp_getsockopt() reads tp->keepalive_time while another cpu might change its value.
--- include/net/tcp.h | 7 +++++-- net/ipv4/tcp.c | 3 ++- 2 files changed, 7 insertions(+), 3 deletions(-)
include/net/tcp.h +5 -2
@@ -1515,9 +1515,12 @@ static inline int keepalive_intvl_when(const struct tcp_sock *tp)static inline int keepalive_time_when (const struct tcp_sock *tp) {struct net *net =sock_net ((struct sock *)tp); +int val; -return tp->keepalive_time ? : -READ_ONCE (net->ipv4.sysctl_tcp_keepalive_time); +/* Paired with WRITE_ONCE() in tcp_sock_set_keepidle_locked() */ + val =READ_ONCE (tp->keepalive_time); + +return val ? :READ_ONCE (net->ipv4.sysctl_tcp_keepalive_time); }static inline int keepalive_probes (const struct tcp_sock *tp)
net/ipv4/tcp.c +2 -1
@@ -3312,7 +3312,8 @@ int tcp_sock_set_keepidle_locked(struct sock *sk, int val)if (val <1 || val >MAX_TCP_KEEPIDLE )return -EINVAL ; - tp->keepalive_time = val *HZ ; +/* Paired with WRITE_ONCE() in keepalive_time_when() */ +WRITE_ONCE (tp->keepalive_time, val *HZ );if (sock_flag (sk,SOCK_KEEPOPEN ) && !((1 << sk->sk_state) & (TCPF_CLOSE |TCPF_LISTEN ))) {u32 elapsed =keepalive_time_elapsed (tp);
Signature · 1 line
2.41.0.255.g8b1d071c50-goog
PATCH 4/11 tcp: annotate data-races around tp->keepalive_intvl
In reply to Eric Dumazetdo_tcp_getsockopt() reads tp->keepalive_intvl while another cpu might change its value.
--- include/net/tcp.h | 9 +++++++-- net/ipv4/tcp.c | 4 ++-- 2 files changed, 9 insertions(+), 4 deletions(-)
include/net/tcp.h +7 -2
@@ -1507,9 +1507,14 @@ void tcp_leave_memory_pressure(struct sock *sk);static inline int keepalive_intvl_when (const struct tcp_sock *tp) {struct net *net =sock_net ((struct sock *)tp); +int val; + +/* Paired with WRITE_ONCE() in tcp_sock_set_keepintvl() +* and do_tcp_setsockopt(). +*/ + val =READ_ONCE (tp->keepalive_intvl); -return tp->keepalive_intvl ? : -READ_ONCE (net->ipv4.sysctl_tcp_keepalive_intvl); +return val ? :READ_ONCE (net->ipv4.sysctl_tcp_keepalive_intvl); }static inline int keepalive_time_when (const struct tcp_sock *tp)
net/ipv4/tcp.c +2 -2
@@ -3345,7 +3345,7 @@ int tcp_sock_set_keepintvl(struct sock *sk, int val)return -EINVAL ;lock_sock (sk); -tcp_sk (sk)->keepalive_intvl = val *HZ ; +WRITE_ONCE (tcp_sk (sk)->keepalive_intvl, val *HZ );release_sock (sk);return 0 ; }
@@ -3559,7 +3559,7 @@ int do_tcp_setsockopt(struct sock *sk, int level, int optname,if (val <1 || val >MAX_TCP_KEEPINTVL ) err = -EINVAL ;else - tp->keepalive_intvl = val *HZ ; +WRITE_ONCE (tp->keepalive_intvl, val *HZ );break ;case TCP_KEEPCNT :if (val <1 || val >MAX_TCP_KEEPCNT )
Signature · 1 line
2.41.0.255.g8b1d071c50-goog
PATCH 5/11 tcp: annotate data-races around tp->keepalive_probes
In reply to Eric Dumazetdo_tcp_getsockopt() reads tp->keepalive_probes while another cpu might change its value.
--- include/net/tcp.h | 9 +++++++-- net/ipv4/tcp.c | 5 +++-- 2 files changed, 10 insertions(+), 4 deletions(-)
include/net/tcp.h +7 -2
@@ -1531,9 +1531,14 @@ static inline int keepalive_time_when(const struct tcp_sock *tp)static inline int keepalive_probes (const struct tcp_sock *tp) {struct net *net =sock_net ((struct sock *)tp); +int val; + +/* Paired with WRITE_ONCE() in tcp_sock_set_keepcnt() +* and do_tcp_setsockopt(). +*/ + val =READ_ONCE (tp->keepalive_probes); -return tp->keepalive_probes ? : -READ_ONCE (net->ipv4.sysctl_tcp_keepalive_probes); +return val ? :READ_ONCE (net->ipv4.sysctl_tcp_keepalive_probes); }static inline u32 keepalive_time_elapsed (const struct tcp_sock *tp)
net/ipv4/tcp.c +3 -2
@@ -3357,7 +3357,8 @@ int tcp_sock_set_keepcnt(struct sock *sk, int val)return -EINVAL ;lock_sock (sk); -tcp_sk (sk)->keepalive_probes = val; +/* Paired with READ_ONCE() in keepalive_probes() */ +WRITE_ONCE (tcp_sk (sk)->keepalive_probes, val);release_sock (sk);return 0 ; }
@@ -3565,7 +3566,7 @@ int do_tcp_setsockopt(struct sock *sk, int level, int optname,if (val <1 || val >MAX_TCP_KEEPCNT ) err = -EINVAL ;else - tp->keepalive_probes = val; +WRITE_ONCE (tp->keepalive_probes, val);break ;case TCP_SYNCNT :if (val <1 || val >MAX_TCP_SYNCNT )
Signature · 1 line
2.41.0.255.g8b1d071c50-goog
PATCH 6/11 tcp: annotate data-races around icsk->icsk_syn_retries
In reply to Eric Dumazetdo_tcp_getsockopt() and reqsk_timer_handler() read icsk->icsk_syn_retries while another cpu might change its value.
--- net/ipv4/inet_connection_sock.c | 2 +- net/ipv4/tcp.c | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-)
net/ipv4/inet_connection_sock.c +1 -1
@@ -1019,7 +1019,7 @@ static void reqsk_timer_handler(struct timer_list *t) icsk =inet_csk (sk_listener); net =sock_net (sk_listener); - max_syn_ack_retries = icsk->icsk_syn_retries ? : + max_syn_ack_retries =READ_ONCE (icsk->icsk_syn_retries) ? :READ_ONCE (net->ipv4.sysctl_tcp_synack_retries);/* Normally all the openreqs are young and become mature * (i.e. converted to established socket) for first timeout.
net/ipv4/tcp.c +3 -3
@@ -3291,7 +3291,7 @@ int tcp_sock_set_syncnt(struct sock *sk, int val)return -EINVAL ;lock_sock (sk); -inet_csk (sk)->icsk_syn_retries = val; +WRITE_ONCE (inet_csk (sk)->icsk_syn_retries, val);release_sock (sk);return 0 ; }
@@ -3572,7 +3572,7 @@ int do_tcp_setsockopt(struct sock *sk, int level, int optname,if (val <1 || val >MAX_TCP_SYNCNT ) err = -EINVAL ;else - icsk->icsk_syn_retries = val; +WRITE_ONCE (icsk->icsk_syn_retries, val);break ;case TCP_SAVE_SYN :
@@ -3993,7 +3993,7 @@ int do_tcp_getsockopt(struct sock *sk, int level, val =keepalive_probes (tp);break ;case TCP_SYNCNT : - val = icsk->icsk_syn_retries ? : + val =READ_ONCE (icsk->icsk_syn_retries) ? :READ_ONCE (net->ipv4.sysctl_tcp_syn_retries);break ;case TCP_LINGER2 :
Signature · 1 line
2.41.0.255.g8b1d071c50-goog
PATCH 7/11 tcp: annotate data-races around tp->linger2
In reply to Eric Dumazetdo_tcp_getsockopt() reads tp->linger2 while another cpu might change its value.
--- net/ipv4/tcp.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)
net/ipv4/tcp.c +4 -4
@@ -3585,11 +3585,11 @@ int do_tcp_setsockopt(struct sock *sk, int level, int optname,case TCP_LINGER2 :if (val <0 ) - tp->linger2 =-1 ; +WRITE_ONCE (tp->linger2,-1 );else if (val >TCP_FIN_TIMEOUT_MAX /HZ ) - tp->linger2 =TCP_FIN_TIMEOUT_MAX ; +WRITE_ONCE (tp->linger2,TCP_FIN_TIMEOUT_MAX );else - tp->linger2 = val *HZ ; +WRITE_ONCE (tp->linger2, val *HZ );break ;case TCP_DEFER_ACCEPT :
@@ -3997,7 +3997,7 @@ int do_tcp_getsockopt(struct sock *sk, int level,READ_ONCE (net->ipv4.sysctl_tcp_syn_retries);break ;case TCP_LINGER2 : - val = tp->linger2; + val =READ_ONCE (tp->linger2);if (val >=0 ) val = (val ? :READ_ONCE (net->ipv4.sysctl_tcp_fin_timeout)) /HZ ;break ;
Signature · 1 line
2.41.0.255.g8b1d071c50-goog
PATCH 8/11 tcp: annotate data-races around rskq_defer_accept
In reply to Eric Dumazetdo_tcp_getsockopt() reads rskq_defer_accept while another cpu might change its value.
--- net/ipv4/tcp.c | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-)
net/ipv4/tcp.c +6 -5
@@ -3594,9 +3594,9 @@ int do_tcp_setsockopt(struct sock *sk, int level, int optname,case TCP_DEFER_ACCEPT :/* Translate value in seconds to number of retransmits */ - icsk->icsk_accept_queue.rskq_defer_accept = -secs_to_retrans (val,TCP_TIMEOUT_INIT /HZ , -TCP_RTO_MAX /HZ ); +WRITE_ONCE (icsk->icsk_accept_queue.rskq_defer_accept, +secs_to_retrans (val,TCP_TIMEOUT_INIT /HZ , +TCP_RTO_MAX /HZ ));break ;case TCP_WINDOW_CLAMP :
@@ -4002,8 +4002,9 @@ int do_tcp_getsockopt(struct sock *sk, int level, val = (val ? :READ_ONCE (net->ipv4.sysctl_tcp_fin_timeout)) /HZ ;break ;case TCP_DEFER_ACCEPT : - val =retrans_to_secs (icsk->icsk_accept_queue.rskq_defer_accept, -TCP_TIMEOUT_INIT /HZ ,TCP_RTO_MAX /HZ ); + val =READ_ONCE (icsk->icsk_accept_queue.rskq_defer_accept); + val =retrans_to_secs (val,TCP_TIMEOUT_INIT /HZ , +TCP_RTO_MAX /HZ );break ;case TCP_WINDOW_CLAMP : val = tp->window_clamp;
Signature · 1 line
2.41.0.255.g8b1d071c50-goog
PATCH 9/11 tcp: annotate data-races around tp->notsent_lowat
In reply to Eric Dumazettp->notsent_lowat can be read locklessly from do_tcp_getsockopt() and tcp_poll().
--- include/net/tcp.h | 6 +++++- net/ipv4/tcp.c | 4 ++-- 2 files changed, 7 insertions(+), 3 deletions(-)
include/net/tcp.h +5 -1
@@ -2059,7 +2059,11 @@ void __tcp_v4_send_check(struct sk_buff *skb, __be32 saddr, __be32 daddr);static inline u32 tcp_notsent_lowat (const struct tcp_sock *tp) {struct net *net =sock_net ((struct sock *)tp); -return tp->notsent_lowat ?:READ_ONCE (net->ipv4.sysctl_tcp_notsent_lowat); +u32 val; + + val =READ_ONCE (tp->notsent_lowat); + +return val ?:READ_ONCE (net->ipv4.sysctl_tcp_notsent_lowat); }bool tcp_stream_memory_free (const struct sock *sk,int wake);
net/ipv4/tcp.c +2 -2
@@ -3664,7 +3664,7 @@ int do_tcp_setsockopt(struct sock *sk, int level, int optname, err =tcp_repair_set_window (tp, optval, optlen);break ;case TCP_NOTSENT_LOWAT : - tp->notsent_lowat = val; +WRITE_ONCE (tp->notsent_lowat, val); sk->sk_write_space (sk);break ;case TCP_INQ :
@@ -4164,7 +4164,7 @@ int do_tcp_getsockopt(struct sock *sk, int level, val =tcp_time_stamp_raw () +READ_ONCE (tp->tsoffset);break ;case TCP_NOTSENT_LOWAT : - val = tp->notsent_lowat; + val =READ_ONCE (tp->notsent_lowat);break ;case TCP_INQ : val = tp->recvmsg_inq;
Signature · 1 line
2.41.0.255.g8b1d071c50-goog
PATCH 10/11 tcp: annotate data-races around icsk->icsk_user_timeout
In reply to Eric DumazetThis field can be read locklessly from do_tcp_getsockopt()
--- net/ipv4/tcp.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
net/ipv4/tcp.c +3 -3
@@ -3300,7 +3300,7 @@ EXPORT_SYMBOL(tcp_sock_set_syncnt);void tcp_sock_set_user_timeout (struct sock *sk,u32 val) {lock_sock (sk); -inet_csk (sk)->icsk_user_timeout = val; +WRITE_ONCE (inet_csk (sk)->icsk_user_timeout, val);release_sock (sk); }EXPORT_SYMBOL (tcp_sock_set_user_timeout);
@@ -3620,7 +3620,7 @@ int do_tcp_setsockopt(struct sock *sk, int level, int optname,if (val <0 ) err = -EINVAL ;else - icsk->icsk_user_timeout = val; +WRITE_ONCE (icsk->icsk_user_timeout, val);break ;case TCP_FASTOPEN :
@@ -4141,7 +4141,7 @@ int do_tcp_getsockopt(struct sock *sk, int level,break ;case TCP_USER_TIMEOUT : - val = icsk->icsk_user_timeout; + val =READ_ONCE (icsk->icsk_user_timeout);break ;case TCP_FASTOPEN :
Signature · 1 line
2.41.0.255.g8b1d071c50-goog
PATCH 11/11 tcp: annotate data-races around fastopenq.max_qlen
In reply to Eric DumazetThis field can be read locklessly.
--- include/linux/tcp.h | 2 +- net/ipv4/tcp.c | 2 +- net/ipv4/tcp_fastopen.c | 6 ++++-- 3 files changed, 6 insertions(+), 4 deletions(-)
include/linux/tcp.h +1 -1
@@ -513,7 +513,7 @@ static inline void fastopen_queue_tune(struct sock *sk, int backlog)struct request_sock_queue *queue = &inet_csk (sk)->icsk_accept_queue;int somaxconn =READ_ONCE (sock_net (sk)->core.sysctl_somaxconn); - queue->fastopenq.max_qlen = min_t(unsigned int, backlog, somaxconn); + WRITE_ONCE(queue->fastopenq.max_qlen, min_t(unsigned int, backlog, somaxconn)); } static inline void tcp_move_syn(struct tcp_sock *tp,
net/ipv4/tcp.c +1 -1
@@ -4145,7 +4145,7 @@ int do_tcp_getsockopt(struct sock *sk, int level,break ;case TCP_FASTOPEN : - val = icsk->icsk_accept_queue.fastopenq.max_qlen; + val =READ_ONCE (icsk->icsk_accept_queue.fastopenq.max_qlen);break ;case TCP_FASTOPEN_CONNECT :
net/ipv4/tcp_fastopen.c +4 -2
@@ -296,6 +296,7 @@ static struct sock *tcp_fastopen_create_child(struct sock *sk,static bool tcp_fastopen_queue_check (struct sock *sk) {struct fastopen_queue *fastopenq; +int max_qlen;/* Make sure the listener has enabled fastopen, and we don't * exceed the max # of pending TFO requests allowed before trying
@@ -308,10 +309,11 @@ static bool tcp_fastopen_queue_check(struct sock *sk)* temporarily vs a server not supporting Fast Open at all. */ fastopenq = &inet_csk (sk)->icsk_accept_queue.fastopenq; -if (fastopenq->max_qlen ==0 ) + max_qlen =READ_ONCE (fastopenq->max_qlen); +if (max_qlen ==0 )return false; -if (fastopenq->qlen >= fastopenq->max_qlen) { +if (fastopenq->qlen >= max_qlen) {struct request_sock *req1;spin_lock (&fastopenq->lock); req1 = fastopenq->rskq_rst_head;
Signature · 1 line
2.41.0.255.g8b1d071c50-goog
This series was applied to netdev/net.git (main) by Jakub Kicinski <[email protected]>:
Hello:
This series was applied to netdev/net.git (main)
by Jakub Kicinski <[email protected]>:
Quoted 8 lines · On Wed, 19 Jul 2023 21:28:46 +0000 you wrote:
This series was inspired by one syzbot (KCSAN) report.
do_tcp_getsockopt()does not lock the socket, we need to annotate most of the reads there (and other places as well).This is a first round, another series will come later.
[...]
Here is the summary with links:
- [net,01/11] tcp: annotate data-races around tp->tcp_tx_delay
https://git.kernel.org/netdev/net/c/348b81b68b13 - [net,02/11] tcp: annotate data-races around tp->tsoffset https://git.kernel.org/netdev/net/c/dd23c9f1e8d5 - [net,03/11] tcp: annotate data-races around tp->keepalive_time https://git.kernel.org/netdev/net/c/4164245c76ff - [net,04/11] tcp: annotate data-races around tp->keepalive_intvl https://git.kernel.org/netdev/net/c/5ecf9d4f52ff - [net,05/11] tcp: annotate data-races around tp->keepalive_probes https://git.kernel.org/netdev/net/c/6e5e1de616bf - [net,06/11] tcp: annotate data-races around icsk->icsk_syn_retries https://git.kernel.org/netdev/net/c/3a037f0f3c4b - [net,07/11] tcp: annotate data-races around tp->linger2 https://git.kernel.org/netdev/net/c/9df5335ca974 - [net,08/11] tcp: annotate data-races around rskq_defer_accept https://git.kernel.org/netdev/net/c/ae488c74422f - [net,09/11] tcp: annotate data-races around tp->notsent_lowat https://git.kernel.org/netdev/net/c/1aeb87bc1440 - [net,10/11] tcp: annotate data-races around icsk->icsk_user_timeout https://git.kernel.org/netdev/net/c/26023e91e12c - [net,11/11] tcp: annotate data-races around fastopenq.max_qlen https://git.kernel.org/netdev/net/c/70f360dd7042
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html