From: Jakub Sitnicki <jakub@cloudflare.com>
To: Cong Wang <xiyou.wangcong@gmail.com>
Cc: netdev@vger.kernel.org, bpf@vger.kernel.org,
Cong Wang <cong.wang@bytedance.com>,
Eric Dumazet <edumazet@google.com>,
John Fastabend <john.fastabend@gmail.com>
Subject: Re: [Patch net v3 2/4] tcp: fix tcp_cleanup_rbuf() for tcp_read_skb()
Date: Thu, 25 Aug 2022 10:31:15 +0200 [thread overview]
Message-ID: <87mtbsafi4.fsf@cloudflare.com> (raw)
In-Reply-To: <20220817195445.151609-3-xiyou.wangcong@gmail.com>
On Wed, Aug 17, 2022 at 12:54 PM -07, Cong Wang wrote:
> From: Cong Wang <cong.wang@bytedance.com>
>
> tcp_cleanup_rbuf() retrieves the skb from sk_receive_queue, it
> assumes the skb is not yet dequeued. This is no longer true for
> tcp_read_skb() case where we dequeue the skb first.
>
> Fix this by introducing a helper __tcp_cleanup_rbuf() which does
> not require any skb and calling it in tcp_read_skb().
>
> Fixes: 04919bed948d ("tcp: Introduce tcp_read_skb()")
> Cc: Eric Dumazet <edumazet@google.com>
> Cc: John Fastabend <john.fastabend@gmail.com>
> Cc: Jakub Sitnicki <jakub@cloudflare.com>
> Signed-off-by: Cong Wang <cong.wang@bytedance.com>
> ---
> net/ipv4/tcp.c | 24 ++++++++++++++----------
> 1 file changed, 14 insertions(+), 10 deletions(-)
>
> diff --git a/net/ipv4/tcp.c b/net/ipv4/tcp.c
> index 05da5cac080b..181a0d350123 100644
> --- a/net/ipv4/tcp.c
> +++ b/net/ipv4/tcp.c
> @@ -1567,17 +1567,11 @@ static int tcp_peek_sndq(struct sock *sk, struct msghdr *msg, int len)
> * calculation of whether or not we must ACK for the sake of
> * a window update.
> */
> -void tcp_cleanup_rbuf(struct sock *sk, int copied)
> +static void __tcp_cleanup_rbuf(struct sock *sk, int copied)
> {
> struct tcp_sock *tp = tcp_sk(sk);
> bool time_to_ack = false;
>
> - struct sk_buff *skb = skb_peek(&sk->sk_receive_queue);
> -
> - WARN(skb && !before(tp->copied_seq, TCP_SKB_CB(skb)->end_seq),
> - "cleanup rbuf bug: copied %X seq %X rcvnxt %X\n",
> - tp->copied_seq, TCP_SKB_CB(skb)->end_seq, tp->rcv_nxt);
> -
> if (inet_csk_ack_scheduled(sk)) {
> const struct inet_connection_sock *icsk = inet_csk(sk);
>
> @@ -1623,6 +1617,17 @@ void tcp_cleanup_rbuf(struct sock *sk, int copied)
> tcp_send_ack(sk);
> }
>
> +void tcp_cleanup_rbuf(struct sock *sk, int copied)
> +{
> + struct sk_buff *skb = skb_peek(&sk->sk_receive_queue);
> + struct tcp_sock *tp = tcp_sk(sk);
> +
> + WARN(skb && !before(tp->copied_seq, TCP_SKB_CB(skb)->end_seq),
> + "cleanup rbuf bug: copied %X seq %X rcvnxt %X\n",
> + tp->copied_seq, TCP_SKB_CB(skb)->end_seq, tp->rcv_nxt);
> + __tcp_cleanup_rbuf(sk, copied);
> +}
> +
> static void tcp_eat_recv_skb(struct sock *sk, struct sk_buff *skb)
> {
> __skb_unlink(skb, &sk->sk_receive_queue);
> @@ -1771,20 +1776,19 @@ int tcp_read_skb(struct sock *sk, skb_read_actor_t recv_actor)
> copied += used;
>
> if (TCP_SKB_CB(skb)->tcp_flags & TCPHDR_FIN) {
> - consume_skb(skb);
> ++seq;
> break;
> }
> - consume_skb(skb);
> break;
> }
> + consume_skb(skb);
> WRITE_ONCE(tp->copied_seq, seq);
>
> tcp_rcv_space_adjust(sk);
>
> /* Clean up data we have read: This will do ACK frames. */
> if (copied > 0)
> - tcp_cleanup_rbuf(sk, copied);
> + __tcp_cleanup_rbuf(sk, copied);
>
> return copied;
> }
This seems to be fixing 2 different problems, but the commit description
mentions just one.
consume_skb() got pulled out of the `while' body. And thanks to that we
are not leaving a dangling skb ref if recv_actor, sk_psock_verdict_recv
in this case, returns 0.
next prev parent reply other threads:[~2022-08-25 8:34 UTC|newest]
Thread overview: 13+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-08-17 19:54 [Patch net v3 0/4] tcp: some bug fixes for tcp_read_skb() Cong Wang
2022-08-17 19:54 ` [Patch net v3 1/4] tcp: fix sock skb accounting in tcp_read_skb() Cong Wang
2022-08-24 8:17 ` Jakub Sitnicki
2022-09-08 23:15 ` [PATCH net] tcp: Use WARN_ON_ONCE() " Peilin Ye
2022-09-13 18:40 ` [PATCH net v2] net: Use WARN_ON_ONCE() in {tcp,udp}_read_skb() Peilin Ye
2022-09-13 19:30 ` Peilin Ye
2022-09-14 7:51 ` Peilin Ye
2022-09-16 14:40 ` [PATCH net] tcp: Use WARN_ON_ONCE() in tcp_read_skb() patchwork-bot+netdevbpf
2022-08-17 19:54 ` [Patch net v3 2/4] tcp: fix tcp_cleanup_rbuf() for tcp_read_skb() Cong Wang
2022-08-25 8:31 ` Jakub Sitnicki [this message]
2022-08-17 19:54 ` [Patch net v3 3/4] tcp: refactor tcp_read_skb() a bit Cong Wang
2022-08-17 19:54 ` [Patch net v3 4/4] tcp: handle pure FIN case correctly Cong Wang
2022-08-18 18:30 ` [Patch net v3 0/4] tcp: some bug fixes for tcp_read_skb() patchwork-bot+netdevbpf
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=87mtbsafi4.fsf@cloudflare.com \
--to=jakub@cloudflare.com \
--cc=bpf@vger.kernel.org \
--cc=cong.wang@bytedance.com \
--cc=edumazet@google.com \
--cc=john.fastabend@gmail.com \
--cc=netdev@vger.kernel.org \
--cc=xiyou.wangcong@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).