tcp: fix tcp_poll() races
Subsystems (3)
v2 cover tcp: fix tcp_poll() races
Flakes in packetdrill tests stressing epoll_wait() were root caused to bad ordering in tcp_write_err()
Precisely, we have to call sk_error_report() after tcp_done().
When fixing this issue, we discovered tcp_abort(), tcp_v4_err() and tcp_v6_err() had similar issues.
Since tcp_reset() has the correct ordering, first patch takes part of it and creates tcp_done_with_error() helper.
v2: added @err parameter to tcp_done_with_error()
as suggested by David Laight.
Eric Dumazet (4):
tcp: add tcp_done_with_error() helper
tcp: fix race in tcp_write_err()
tcp: fix races in tcp_abort()
tcp: fix races in tcp_v[46]_err()
include/net/tcp.h | 1 + net/ipv4/tcp.c | 8 ++------ net/ipv4/tcp_input.c | 32 +++++++++++++++++++++----------- net/ipv4/tcp_ipv4.c | 11 +++-------- net/ipv4/tcp_timer.c | 6 +----- net/ipv6/tcp_ipv6.c | 10 +++------- 6 files changed, 31 insertions(+), 37 deletions(-)
Signature · 1 line
2.45.1.288.g0e0cd299f1-goog
PATCH v2 1/4 tcp: add tcp_done_with_error() helper
tcp_reset() ends with a sequence that is carefuly ordered.
We need to fix [e]poll bugs in the following patches, it makes sense to use a common helper.
--- include/net/tcp.h | 1 + net/ipv4/tcp.c | 2 +- net/ipv4/tcp_input.c | 32 +++++++++++++++++++++----------- 3 files changed, 23 insertions(+), 12 deletions(-)
include/net/tcp.h +1 -0
@@ -677,6 +677,7 @@ void tcp_skb_collapse_tstamp(struct sk_buff *skb,/* tcp_input.c */ void tcp_rearm_rto (struct sock *sk);void tcp_synack_rtt_meas (struct sock *sk,struct request_sock *req); +void tcp_done_with_error (struct sock *sk,int err);void tcp_reset (struct sock *sk,struct sk_buff *skb);void tcp_fin (struct sock *sk);void tcp_check_space (struct sock *sk);
net/ipv4/tcp.c +1 -1
@@ -598,7 +598,7 @@ __poll_t tcp_poll(struct file *file, struct socket *sock, poll_table *wait)*/ mask |=EPOLLOUT |EPOLLWRNORM ; } -/* This barrier is coupled with smp_wmb() in tcp_reset() */ +/* This barrier is coupled with smp_wmb() in tcp_done_with_error() */ smp_rmb ();if (READ_ONCE (sk->sk_err) || !skb_queue_empty_lockless (&sk->sk_error_queue))
net/ipv4/tcp_input.c +21 -11
@@ -4436,9 +4436,26 @@ static enum skb_drop_reason tcp_sequence(const struct tcp_sock *tp,return SKB_NOT_DROPPED_YET ; } + +void tcp_done_with_error (struct sock *sk,int err) +{ +/* This barrier is coupled with smp_rmb() in tcp_poll() */ +WRITE_ONCE (sk->sk_err, err); +smp_wmb (); + +tcp_write_queue_purge (sk); +tcp_done (sk); + +if (!sock_flag (sk,SOCK_DEAD )) +sk_error_report (sk); +} +EXPORT_SYMBOL (tcp_done_with_error); +/* When we get a reset we do this. */ void tcp_reset (struct sock *sk,struct sk_buff *skb) { +int err; +trace_tcp_receive_reset (sk);/* mptcp can't tell us to ignore reset pkts,
@@ -4450,24 +4467,17 @@ void tcp_reset(struct sock *sk, struct sk_buff *skb)/* We want the right error as BSD sees it (and indeed as we do). */ switch (sk->sk_state) {case TCP_SYN_SENT : -WRITE_ONCE (sk->sk_err,ECONNREFUSED ); + err =ECONNREFUSED ;break ;case TCP_CLOSE_WAIT : -WRITE_ONCE (sk->sk_err,EPIPE ); + err =EPIPE ;break ;case TCP_CLOSE :return ;default : -WRITE_ONCE (sk->sk_err,ECONNRESET ); + err =ECONNRESET ; } -/* This barrier is coupled with smp_rmb() in tcp_poll() */ -smp_wmb (); - -tcp_write_queue_purge (sk); -tcp_done (sk); - -if (!sock_flag (sk,SOCK_DEAD )) -sk_error_report (sk); +tcp_done_with_error (sk, err); }/*
Signature · 1 line
2.45.1.288.g0e0cd299f1-goog
Re: [PATCH v2 net-next 1/4] tcp: add tcp_done_with_error() helper
Quoted 9 lines · Eric Dumazet
tcp_reset()ends with a sequence that is carefuly ordered.We need to fix [e]poll bugs in the following patches, it makes sense to use a common helper.
Suggested-by Neal Cardwell <[email protected]>Signed-off-by Eric Dumazet <[email protected]>---
Thanks, Eric!
neal
PATCH v2 2/4 tcp: fix race in tcp_write_err()
In reply to Eric DumazetI noticed flakes in a packetdrill test, expecting an epoll_wait() to return EPOLLERR | EPOLLHUP on a failed connect() attempt, after multiple SYN retransmits. It sometimes return EPOLLERR only.
The issue is that tcp_write_err():
1) writes an error in sk->sk_err,
2) calls sk_error_report(),
3) then calls tcp_done().
tcp_done() is writing SHUTDOWN_MASK into sk->sk_shutdown, among other things.
Problem is that the awaken user thread (from 2) sk_error_report()) might call tcp_poll() before tcp_done() has written sk->sk_shutdown.
tcp_poll() only sees a non zero sk->sk_err and returns EPOLLERR.
This patch fixes the issue by making sure to call sk_error_report() after tcp_done().
tcp_write_err() also lacks an smp_wmb().
We can reuse tcp_done_with_error() to factor out the details, as Neal suggested.
--- net/ipv4/tcp_timer.c | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-)
net/ipv4/tcp_timer.c +1 -5
@@ -74,11 +74,7 @@ u32 tcp_clamp_probe0_to_user_timeout(const struct sock *sk, u32 when)static void tcp_write_err (struct sock *sk) { -WRITE_ONCE (sk->sk_err,READ_ONCE (sk->sk_err_soft) ? :ETIMEDOUT ); -sk_error_report (sk); - -tcp_write_queue_purge (sk); -tcp_done (sk); +tcp_done_with_error (sk,READ_ONCE (sk->sk_err_soft) ? :ETIMEDOUT );__NET_INC_STATS (sock_net (sk),LINUX_MIB_TCPABORTONTIMEOUT ); }
Signature · 1 line
2.45.1.288.g0e0cd299f1-goog
Re: [PATCH v2 net-next 2/4] tcp: fix race in tcp_write_err()
Quoted 21 lines · Eric Dumazet
I noticed flakes in a packetdrill test, expecting an
epoll_wait()to return EPOLLERR | EPOLLHUP on a failedconnect()attempt, after multiple SYN retransmits. It sometimes return EPOLLERR only.The issue is that
tcp_write_err():
1) writes an error insk->sk_err,
2) callssk_error_report(),
3) then callstcp_done().
tcp_done()is writingSHUTDOWN_MASKintosk->sk_shutdown, among other things.Problem is that the awaken user thread (from 2)
sk_error_report()) might calltcp_poll()beforetcp_done()has writtensk->sk_shutdown.
tcp_poll()only sees a non zerosk->sk_errand returns EPOLLERR.This patch fixes the issue by making sure to call
sk_error_report()aftertcp_done().
Quoted 8 lines
tcp_write_err()also lacks ansmp_wmb().We can reuse
tcp_done_with_error()to factor out the details, as Neal suggested.Fixes 1da177e4c3f4 ("Linux-2.6.12-rc2")Signed-off-by Eric Dumazet <[email protected]>---
Thanks, Eric!
neal
PATCH v2 3/4 tcp: fix races in tcp_abort()
In reply to Eric Dumazettcp_abort() has the same issue than the one fixed in the prior patch in tcp_write_err().
In order to get consistent results from tcp_poll(), we must call sk_error_report() after tcp_done().
We can use tcp_done_with_error() to centralize this logic.
--- net/ipv4/tcp.c | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-)
net/ipv4/tcp.c +1 -5
@@ -4576,14 +4576,10 @@ int tcp_abort(struct sock *sk, int err)bh_lock_sock (sk);if (!sock_flag (sk,SOCK_DEAD )) { -WRITE_ONCE (sk->sk_err, err); -/* This barrier is coupled with smp_rmb() in tcp_poll() */ -smp_wmb (); -sk_error_report (sk);if (tcp_need_reset (sk->sk_state))tcp_send_active_reset (sk,GFP_ATOMIC ,SK_RST_REASON_NOT_SPECIFIED ); -tcp_done (sk); +tcp_done_with_error (sk, err); }bh_unlock_sock (sk);
Signature · 1 line
2.45.1.288.g0e0cd299f1-goog
Re: [PATCH v2 net-next 3/4] tcp: fix races in tcp_abort()
Quoted 4 lines · Eric Dumazet
tcp_abort()has the same issue than the one fixed in the prior patch intcp_write_err().
Quoted 8 lines
In order to get consistent results from
tcp_poll(), we must callsk_error_report()aftertcp_done().We can use
tcp_done_with_error()to centralize this logic.Fixes c1e64e298b8c ("net: diag: Support destroying TCP sockets.")Signed-off-by Eric Dumazet <[email protected]>---
Thanks, Eric!
neal
PATCH v2 4/4 tcp: fix races in tcp_v[46]_err()
In reply to Eric DumazetThese functions have races when they:
1) Write sk->sk_err
2) call sk_error_report(sk)
3) call tcp_done(sk)
As described in prior patches in this series:
An smp_wmb() is missing.
We should call tcp_done() before sk_error_report(sk) to have consistent tcp_poll() results on SMP hosts.
Use tcp_done_with_error() where we centralized the correct sequence.
--- net/ipv4/tcp_ipv4.c | 11 +++-------- net/ipv6/tcp_ipv6.c | 10 +++------- 2 files changed, 6 insertions(+), 15 deletions(-)
net/ipv4/tcp_ipv4.c +3 -8
@@ -611,15 +611,10 @@ int tcp_v4_err(struct sk_buff *skb, u32 info)ip_icmp_error (sk, skb, err, th->dest, info, (u8 *)th); -if (!sock_owned_by_user (sk)) { -WRITE_ONCE (sk->sk_err, err); - -sk_error_report (sk); - -tcp_done (sk); - }else { +if (!sock_owned_by_user (sk)) +tcp_done_with_error (sk, err); +else WRITE_ONCE (sk->sk_err_soft, err); - } goto out; }
net/ipv6/tcp_ipv6.c +3 -7
@@ -490,14 +490,10 @@ static int tcp_v6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,ipv6_icmp_error (sk, skb, err, th->dest,ntohl (info), (u8 *)th); -if (!sock_owned_by_user (sk)) { -WRITE_ONCE (sk->sk_err, err); -sk_error_report (sk);/* Wake people up to see the error (see connect in sock.c) */ - -tcp_done (sk); - }else { +if (!sock_owned_by_user (sk)) +tcp_done_with_error (sk, err); +else WRITE_ONCE (sk->sk_err_soft, err); - } goto out;case TCP_LISTEN :break ;
Signature · 1 line
2.45.1.288.g0e0cd299f1-goog
Re: [PATCH v2 net-next 4/4] tcp: fix races in tcp_v[46]_err()
Quoted 13 lines · Eric Dumazet
These functions have races when they:
1) Write
sk->sk_err
2) callsk_error_report(sk)
3) calltcp_done(sk)As described in prior patches in this series:
An
smp_wmb()is missing.
We should calltcp_done()beforesk_error_report(sk)to have consistenttcp_poll()results on SMP hosts.
Quoted 6 lines
Use
tcp_done_with_error()where we centralized the correct sequence.Fixes 1da177e4c3f4 ("Linux-2.6.12-rc2")Signed-off-by Eric Dumazet <[email protected]>---
Thanks, Eric!
neal
This series was applied to netdev/net-next.git (main) by Jakub Kicinski <[email protected]>:
Hello:
This series was applied to netdev/net-next.git (main) by Jakub Kicinski <[email protected]>:
Quoted 10 lines · On Tue, 28 May 2024 12:52:49 +0000 you wrote:
Flakes in packetdrill tests stressing
epoll_wait()were root caused to bad ordering intcp_write_err()Precisely, we have to call
sk_error_report()aftertcp_done().When fixing this issue, we discovered
tcp_abort(),tcp_v4_err()andtcp_v6_err()had similar issues.[...]
Here is the summary with links:
- [v2,net-next,1/4] tcp: add tcp_done_with_error() helper
https://git.kernel.org/netdev/net-next/c/5e514f1cba09 - [v2,net-next,2/4] tcp: fix race in tcp_write_err() https://git.kernel.org/netdev/net-next/c/853c3bd7b791 - [v2,net-next,3/4] tcp: fix races in tcp_abort() https://git.kernel.org/netdev/net-next/c/5ce4645c23cf - [v2,net-next,4/4] tcp: fix races in tcp_v[46]_err() https://git.kernel.org/netdev/net-next/c/fde6f897f2a1
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html