]> git.ipfire.org Git - thirdparty/kernel/stable.git/commitdiff
tcp: factorize logic into tcp_epollin_ready()
authorEric Dumazet <edumazet@google.com>
Fri, 12 Feb 2021 23:22:14 +0000 (15:22 -0800)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Fri, 27 Jun 2025 10:04:09 +0000 (11:04 +0100)
[ Upstream commit 05dc72aba364d374a27de567fac58c199ff5ee97 ]

Both tcp_data_ready() and tcp_stream_is_readable() share the same logic.

Add tcp_epollin_ready() helper to avoid duplication.

Signed-off-by: Eric Dumazet <edumazet@google.com>
Cc: Arjun Roy <arjunroy@google.com>
Cc: Wei Wang <weiwan@google.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
Stable-dep-of: 2660a544fdc0 ("net: Fix TOCTOU issue in sk_is_readable()")
Signed-off-by: Sasha Levin <sashal@kernel.org>
include/net/tcp.h
net/ipv4/tcp.c
net/ipv4/tcp_input.c

index 2aad2e79ac6add8488b6395d66756a6d282df0e7..41f535dcaa3f98099e54c0f6e2cd428f0b51352e 100644 (file)
@@ -1445,6 +1445,18 @@ static inline bool tcp_rmem_pressure(const struct sock *sk)
        return atomic_read(&sk->sk_rmem_alloc) > threshold;
 }
 
+static inline bool tcp_epollin_ready(const struct sock *sk, int target)
+{
+       const struct tcp_sock *tp = tcp_sk(sk);
+       int avail = READ_ONCE(tp->rcv_nxt) - READ_ONCE(tp->copied_seq);
+
+       if (avail <= 0)
+               return false;
+
+       return (avail >= target) || tcp_rmem_pressure(sk) ||
+              (tcp_receive_window(tp) <= inet_csk(sk)->icsk_ack.rcv_mss);
+}
+
 extern void tcp_openreq_init_rwin(struct request_sock *req,
                                  const struct sock *sk_listener,
                                  const struct dst_entry *dst);
index 24ebd51c5e0b890f63df54a24c7357f976715e8e..0332fdab942db32c0f1d000f7f151508fe1acee5 100644 (file)
@@ -476,19 +476,11 @@ static void tcp_tx_timestamp(struct sock *sk, u16 tsflags)
        }
 }
 
-static inline bool tcp_stream_is_readable(const struct tcp_sock *tp,
-                                         int target, struct sock *sk)
+static bool tcp_stream_is_readable(struct sock *sk, int target)
 {
-       int avail = READ_ONCE(tp->rcv_nxt) - READ_ONCE(tp->copied_seq);
-
-       if (avail > 0) {
-               if (avail >= target)
-                       return true;
-               if (tcp_rmem_pressure(sk))
-                       return true;
-               if (tcp_receive_window(tp) <= inet_csk(sk)->icsk_ack.rcv_mss)
-                       return true;
-       }
+       if (tcp_epollin_ready(sk, target))
+               return true;
+
        if (sk->sk_prot->stream_memory_read)
                return sk->sk_prot->stream_memory_read(sk);
        return false;
@@ -565,7 +557,7 @@ __poll_t tcp_poll(struct file *file, struct socket *sock, poll_table *wait)
                    tp->urg_data)
                        target++;
 
-               if (tcp_stream_is_readable(tp, target, sk))
+               if (tcp_stream_is_readable(sk, target))
                        mask |= EPOLLIN | EPOLLRDNORM;
 
                if (!(shutdown & SEND_SHUTDOWN)) {
index 7c2e714527f6821540e12707438147ef22a36e71..318fdeb1deef3389e2f6fb2adfab3d4bf9f9cf9a 100644 (file)
@@ -5028,15 +5028,8 @@ err:
 
 void tcp_data_ready(struct sock *sk)
 {
-       const struct tcp_sock *tp = tcp_sk(sk);
-       int avail = tp->rcv_nxt - tp->copied_seq;
-
-       if (avail < sk->sk_rcvlowat && !tcp_rmem_pressure(sk) &&
-           !sock_flag(sk, SOCK_DONE) &&
-           tcp_receive_window(tp) > inet_csk(sk)->icsk_ack.rcv_mss)
-               return;
-
-       sk->sk_data_ready(sk);
+       if (tcp_epollin_ready(sk, sk->sk_rcvlowat))
+               sk->sk_data_ready(sk);
 }
 
 static void tcp_data_queue(struct sock *sk, struct sk_buff *skb)