🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: linux.kernel.netdev
1 messages
1 total messages Started by bot+bpf-ci Tue, 27 Oct 2026 05:30
Re: [PATCH bpf v2 1/1] tcp_bpf: update copied_seq from current value
#7741
Author: bot+bpf-ci
Date: Tue, 27 Oct 2026 05:30
151 lines
14085 bytes
--===============1515168815184980910==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit

> tcp_bpf: update copied_seq from current value

TL;DR:
- In strparser mode, the new copied_seq write-back may over-advance past
  rcv_nxt after multiple rounds, stalling the receiver
- Commit message wrongly says tcp_data_ready() releases the socket lock

> tcp_bpf: update copied_seq from current value
>
> tcp_bpf_recvmsg_parser() snapshots tcp->copied_seq before processing
> queued data and waiting for more. tcp_data_ready() and
> tcp_msg_wait_data() can release the socket lock. While it is released,
> a sockmap verdict can drop or redirect data and advance copied_seq. At
> exit, writing back the old snapshot plus copied_from_self rewinds that
> progress, leaving ordinary TCP receive behind queued data.
> Keep copied_from_self as the count of bytes consumed from this socket,
> but add it to the current tcp->copied_seq at exit instead of writing
> back a stale snapshot. Count the FIN byte in copied_from_self as well.
> Fixes: e5c6de5fa025 ("bpf, sockmap: Incorrectly handling copied_seq")
> Cc: stable@vger.kernel.org
> Reported-by: Vega <vega@nebusec.ai>
> Suggested-by: Alexei Starovoitov <alexei.starovoitov@gmail.com>
> Assisted-by: LLM
> Signed-off-by: Zhiling Zou <zhilinz@nebusec.ai>

The changelog says tcp_data_ready() can release the socket lock.  Is that
accurate?

tcp_data_ready() calls sk->sk_data_ready synchronously while recvmsg still
owns the socket.  In verdict mode the verdict runs inline:

tcp_data_ready()
  sk_psock_verdict_data_ready()
    tcp_read_skb()
      sk_psock_verdict_recv()
        sk_psock_verdict_apply()
          tcp_eat_skb()

and tcp_eat_skb() advances copied_seq after the old snapshot was taken.

In strparser mode, strp_data_ready() sees sock_owned_by_user_nocheck() and
only queues work.

The conclusion that the snapshot is stale still holds, but for
tcp_data_ready() the stated reason does not match the code.  Since this is
tagged for stable, could the message say that the verdict runs inline from
tcp_data_ready(), and that only tcp_msg_wait_data() (via
sk_wait_event() -> release_sock()) drops the lock?

> diff --git a/net/ipv4/tcp_bpf.c b/net/ipv4/tcp_bpf.c
> index 8cf63a9e7fed..a01d6d5449c7 100644
> --- a/net/ipv4/tcp_bpf.c
> +++ b/net/ipv4/tcp_bpf.c

[ ... ]

> @@ -319,7 +316,7 @@ static int tcp_bpf_recvmsg_parser(struct sock *sk,
>  	}
>  out:
>  	if (!peek)
> -		WRITE_ONCE(tcp->copied_seq, seq);
> +		WRITE_ONCE(tcp->copied_seq, tcp->copied_seq + copied_from_self);
>  	tcp_rcv_space_adjust(sk);
>  	if (copied > 0)
>  		__tcp_cleanup_rbuf(sk, copied);

Can this over-advance tcp->copied_seq in strparser mode (stream_parser +
stream_verdict)?

The new write-back assumes that whatever changed tcp->copied_seq while the
socket lock was dropped only added bytes that this recvmsg will not count
again.  That holds in verdict-only mode, where tcp_eat_skb() only adds
skb->len for dropped or redirected skbs.

It does not hold in strparser mode, which also reaches
tcp_bpf_recvmsg_parser() through TCP_BPF_RX.  There
tcp_bpf_strp_read_sock() writes an absolute value:

    psock->ingress_bytes = 0;
    copied = tcp_read_sock_noack(sk, desc, recv_actor, true,
                                 &psock->copied_seq);
    ...
    tp->copied_seq = psock->copied_seq - psock->ingress_bytes;

psock->ingress_bytes only covers the bytes passed to self by the current
read_sock call.  After a second strparser round, tp->copied_seq already
includes the self bytes from the first round, which are still unread in
ingress_msg.

For example, start with tp->copied_seq == psock->copied_seq == P and nothing
queued:

1. The reader blocks in tcp_msg_wait_data() -> sk_wait_event(), which calls
   release_sock().

2. A 100-byte message arrives.  In softirq, tcp_data_queue() ->
   tcp_data_ready() -> sk_psock_strp_data_ready() -> strp_read_sock() ->
   tcp_bpf_strp_read_sock() runs and the verdict passes it to self
   (sk_psock_skb_ingress_self() adds 100 to ingress_bytes).
   psock->copied_seq becomes P+100 and tp->copied_seq becomes P.  The
   reader is woken.

3. A second 100-byte message is processed before the reader takes the lock
   again, e.g. in the same softirq batch.  ingress_bytes is reset to 0 and
   then set to 100, psock->copied_seq becomes P+200 and tp->copied_seq
   becomes P+100.

4. The reader takes the lock, goes to msg_bytes_ready and reads both
   messages (copied_from_self = 200).

The old code wrote back snapshot + 200 = P+200, which is correct.  The new
code writes P+100 + 200 = P+300, which is 100 bytes past rcv_nxt.

While copied_seq is ahead of rcv_nxt:

- tcp_epollin_ready() computes avail = rcv_nxt - copied_seq <= 0 and
  returns false.  tcp_data_ready() then does not call sk_data_ready, so
  the strparser does not run for new data until more than 100 extra bytes
  arrive.  In a request/response exchange, the receiver stalls.

- tcp_bpf_recvmsg_parser() itself sees a non-empty sk_receive_queue that
  tcp_data_ready() will not drain, and returns -EAGAIN.

- tcp_rcv_space_adjust() computes a negative inq.

- If the socket later leaves the sockmap, tcp_recvmsg() starts from an
  advanced copied_seq.

The overcount comes from tcp_bpf_strp_read_sock(), and it also happens with
the old code when the rounds finish before recvmsg takes the lock.  With the
old snapshot, however, rounds that ran while a blocked reader waited came
out right, and with this patch they now overshoot.

Should this patch handle the strparser case, for example by making
tcp_bpf_strp_read_sock() account for all self bytes still unread rather than
only the current call's ingress_bytes?  Otherwise, could the commit message
explain why the strparser path is still correct?


---
AI reviewed your patch. Please fix the bug or email reply why it's not a bug.
See: https://github.com/kernel-patches/vmtest/blob/master/ci/claude/README.md

CI run summary: https://github.com/kernel-patches/bpf/actions/runs/40453304549
--===============1515168815184980910==--
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads