]> git.ipfire.org Git - thirdparty/kernel/stable.git/commitdiff
tcp: add tcp_done_with_error() helper
authorEric Dumazet <edumazet@google.com>
Tue, 28 May 2024 12:52:50 +0000 (12:52 +0000)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Sat, 3 Aug 2024 06:59:35 +0000 (08:59 +0200)
[ Upstream commit 5e514f1cba090e1c8fff03e92a175eccfe46305f ]

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 <ncardwell@google.com>
Signed-off-by: Eric Dumazet <edumazet@google.com>
Acked-by: Neal Cardwell <ncardwell@google.com>
Link: https://lore.kernel.org/r/20240528125253.1966136-2-edumazet@google.com
Signed-off-by: Jakub Kicinski <kuba@kernel.org>
Stable-dep-of: 853c3bd7b791 ("tcp: fix race in tcp_write_err()")
Signed-off-by: Sasha Levin <sashal@kernel.org>
include/net/tcp.h
net/ipv4/tcp.c
net/ipv4/tcp_input.c

index 060e95b331a286ad7c355be11dc03250d2944920..32815a40dea16637d2cc49d46863b532a44fbab3 100644 (file)
@@ -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);
index e6790ea7487738d8ab825ac0298d15f6744fb3c4..cc36ff797484bddac7b70c79c2eb57172b92691d 100644 (file)
@@ -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))
index 38da23f991d605b2db5eebd876d58765e1605d91..570e87ad9a56e6787219911e6a8c995b6be4419e 100644 (file)
@@ -4469,9 +4469,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,
@@ -4483,24 +4500,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);
 }
 
 /*