Skip to content

Bpf base no ci - #3

Open
mrpre wants to merge 9 commits into
bpf_basefrom
bpf_base_no_ci
Open

mrpre wants to merge 9 commits into
bpf_basefrom
bpf_base_no_ci

Conversation

@mrpre

@mrpre mrpre commented Dec 2, 2026

Copy link
Copy Markdown
Owner

No description provided.

mrpre added 9 commits December 2, 2026 20:50
With strparser, read_sock advances tp->copied_seq when it pulls skbs
off sk_receive_queue, and recvmsg() advances it again when the user
reads from ingress_msg. For a skb that the verdict passes back to the
same socket, this counted the bytes twice, so commit 36b62df
("bpf: Fix wrong copied_seq calculation") added psock->ingress_bytes to
record how many bytes went to self and take them out of the read_sock
side.

That works for RPC style traffic, where each reply is read before the
next one arrives. But psock->copied_seq is advanced on every read_sock
call while ingress_bytes is reset on every call, so tp->copied_seq
moves forward whenever a new skb arrives, even if the user never calls
recvmsg(). Once the parser runs twice before a read, the SK_PASS bytes
of the first run are no longer subtracted and tp->copied_seq already
covers them. recvmsg() then adds them again and copied_seq passes
rcv_nxt. The socket now looks empty to TCP, the parser is not woken for
new data anymore and recvmsg() keeps returning -EAGAIN while the skbs
sit on sk_receive_queue.

tcp_bpf_recvmsg_parser() has the same problem from the other side. It
snapshots tp->copied_seq at entry and writes the snapshot plus
copied_from_self back at exit. In between, tcp_msg_wait_data() releases
the socket lock and tcp_data_ready() runs the verdict of read_skb under
it. Both can advance copied_seq after the snapshot. Writing the snapshot
back rewinds that progress and leaves copied_seq behind the queued data.

tp->copied_seq should only cover bytes that have left the socket: what
the verdict redirected or dropped, plus what the user has read. SK_PASS
bytes still queued in ingress_msg must not count until recvmsg() hands
them out. So let both sides add only their own delta to the current
tp->copied_seq instead of recomputing it from a saved value. The
read_sock side advances by what this call consumed minus what it passed
to self, taking the delta from psock->copied_seq rather than the return
value so that the FIN byte is still counted. The recvmsg() side adds
copied_from_self, and counts the FIN there as well. Either change alone
moves the mismatch to the other side, so they go together.

ingress_bytes is also bumped from sk_psock_skb_ingress_enqueue(), which
the backlog work reaches without the socket lock for skbs redirected
from other sockets. Those bytes are not part of this socket's stream
and recvmsg() never adds them back, so subtracting them leaves
copied_seq behind. Count only in sk_psock_skb_ingress_self(), where
msg->sk is set, which is what recvmsg() checks.

A strparser skb has no owner when the verdict runs.
sk_psock_skb_ingress_self() sets one with skb_set_owner_r(), and when
the enqueue fails after that, the skb is left to the backlog with
skb->sk still set. It is not in ingress_bytes, so read_sock has already
counted it as consumed. But the backlog picks the path by skb->sk:

  sk_psock_backlog
    sk_psock_handle_skb
      sk_psock_skb_ingress
        if (skb->sk == sk)
          sk_psock_skb_ingress_self   <- sets msg->sk

and recvmsg() counts the bytes again. So once a strparser skb is left
to the backlog, it must not take the skb->sk == sk path. Drop the owner
with skb_orphan() when the enqueue fails, in sk_psock_skb_ingress() as
well, where the backlog sets the owner before the enqueue. This also
gives the rmem charge back, and the backlog charges the skb again like
any other strparser skb queued there. Picking the path by skb->sk is
nasty and will be reworked later.

The drop path clears the strparser bit before tcp_eat_skb(), so
tcp_eat_skb() no longer skips the skb and adds skb->len to
tp->copied_seq. The old code overwrote that at the end of read_sock.
With the delta it would stay, so call tcp_eat_skb() first.

Fixes: 36b62df ("bpf: Fix wrong copied_seq calculation")
Fixes: e5c6de5 ("bpf, sockmap: Incorrectly handling copied_seq")
Cc: [email protected]
Reported-by: Vega <[email protected]>
Suggested-by: Alexei Starovoitov <[email protected]>
Co-developed-by: Zhiling Zou <[email protected]>
Signed-off-by: Zhiling Zou <[email protected]>
Signed-off-by: Jiayuan Chen <[email protected]>
Send two packets and wait for each to reach ingress_msg before sending
the next, so that the parser runs once per packet with the first one
still unread. Read both, then send a third packet and expect it to be
delivered.

Without the fix the third packet is never read: copied_seq has passed
rcv_nxt after the first recv(), so the parser is not woken anymore.

Also drop a packet in the verdict test by redirecting to an empty slot,
and expect the next packet to be delivered.

Signed-off-by: Jiayuan Chen <[email protected]>
An SK_MSG program can pop the whole message with bpf_msg_pop_data() and
then redirect it with BPF_F_INGRESS. tcp_bpf_send_verdict() does not
check for that and calls bpf_tcp_ingress() with nothing to send. Its
do-while loop still walks the empty ring, charges zero bytes and queues
a zero sized sk_msg on the peer's ingress_msg.

__sk_msg_recvmsg() hits the zero length element first, returns -EFAULT
and never dequeues it, so every later message on that socket sits
behind it and recvmsg() keeps failing. Only tearing down the psock gets
rid of it.

Return early from bpf_tcp_ingress() when the message is empty. SK_PASS
and egress redirect already send nothing in that case.

Fixes: 5d609ba ("bpf, sockmap: Several fixes to bpf_msg_pop_data")
Signed-off-by: Jiayuan Chen <[email protected]>
tcp_bpf_sendmsg() keeps the state of the current send in the psock:
the verdict in eval, the byte budget in apply_bytes, the redirect
target in sk_redir and the message being built in cork. The socket lock
does not protect that state all the way through. tcp_bpf_push() drops
it in sk_stream_wait_memory(), and the SK_REDIRECT branch of
tcp_bpf_send_verdict() drops it around tcp_bpf_sendmsg_redir(). A
second sendmsg() on the same socket then runs with the state of the
first one.

The cork is the worst case. The SK_PASS branch pushes psock->cork while
it is still attached. When the push sleeps, the second sender picks the
same cork up through psock->cork, appends to it, runs the prog on it,
and on SK_REDIRECT frees it. The first sender wakes up in
tcp_bpf_push() and writes to a freed sk_msg. The other fields are not
freed, but the second sender sends without a verdict, or with the
verdict and the budget of the first one.

Plain TCP has no state of this kind, and two senders on one stream
socket only get their bytes interleaved. Add a mutex to the psock and
hold it for the whole tcp_bpf_sendmsg(). A second sender waits for it.

tls_sw_sendmsg() does the same with tx_lock, and the wait is the same
as there. The mutex is taken with MSG_DONTWAIT as well, and SO_SNDTIMEO
does not apply to it. A trylock that returns -EAGAIN has nothing to wake
the sender up later, since the send buffer is not full.

sk_psock_backlog() is a sender too when an skb is redirected to the
egress of a socket with an sk_msg prog. So a bit in psock->state that
fails the second sender with -EBUSY is smaller, but it breaks the
backlog and a single user sender that runs into it.

Redirects to another socket go through tcp_bpf_push_locked() and
bpf_tcp_ingress() and do not touch the target's send state, so they are
not affected.

tcp_bpf_send_verdict() already has a few checks for a second sender,
added by earlier fixes. They do no harm with the mutex and are left
as they are, comments included, to keep this patch small.

Fixes: 4f738ad ("bpf: create tcp_bpf_ulp allowing BPF to monitor socket TX/RX data")
Signed-off-by: Jiayuan Chen <[email protected]>
Commit 8cc3aef ("tcp: Do not allow buggy transitions between ehash
and lhash2.") rejects connect() -> disconnect -> listen(), but
connect() -> disconnect -> connect() is still allowed.

A socket in a sockmap keeps its psock across tcp_disconnect(), and the
new connection runs into what the old one left there:

 - recvmsg() returns the old data still queued on psock->ingress_msg.
 - psock->copied_seq and psock->ingress_bytes still account for it, so
   copied_seq runs ahead of rcv_nxt for the rest of the connection.
 - sk_psock_backlog() does not take the socket lock for ingress, so it
   can queue old skbs from psock->ingress_skb after the reconnect.

Cleaning all of this up on disconnect is hard.

TLS rejects the disconnect instead, see commit 5071a1e ("net: tls:
explicitly disallow disconnect"). That does not work here.
sk_prot->disconnect is not only called for connect(AF_UNSPEC). close()
with a zero linger time and inet_child_forget() call it too and do not
look at the return value. tcp_bpf_disconnect() has no good way to tell
them apart. A bit set on the psock by sock_map_close() is not enough,
a BPF prog can add the socket back to a map in the middle of close()
and it gets a new psock. Rejecting the disconnect of close() by mistake
leaves a socket that is never reset and never freed.

So let the disconnect through and reject the connect() after it. TCP
does the same for listen() in the commit above, the disconnect succeeds
and it is the listen() after it that fails. A TCP socket in TCP_CLOSE
can't be added to a sockmap, and __inet_stream_connect() only calls
sk_prot->connect in TCP_CLOSE. A socket with a psock gets there only
after a disconnect, so no other connect() is affected.

Fixes: b8b8315 ("bpf, sockmap: Remove unhash handler for BPF sockmap usage")
Signed-off-by: Jiayuan Chen <[email protected]>
A sockops prog can add a child that is not accepted yet to a sockmap at
BPF_SOCK_OPS_PASSIVE_ESTABLISHED_CB. An sk_msg redirect to this child,
ingress or egress, takes lock_sock() on it. If the listener is closed at
that time, inet_csk_listen_stop() hits:

	WARN_ON(sock_owned_by_user(child));

WARNING: net/ipv4/inet_connection_sock.c:1492 at inet_csk_listen_stop+0x7ba/0xc40
Call Trace:
 <TASK>
 __tcp_close+0xb64/0xee0
 tcp_close+0x24/0xf0
 inet_release+0x10a/0x240
 __sock_release+0xaf/0x270
 sock_close+0x18/0x30
 __fput+0x360/0xa90
 fput_close_sync+0xe6/0x1d0
 __x64_sys_close+0x80/0xe0
 x64_sys_call+0x2680/0x27e0
 do_syscall_64+0xe5/0x610
 entry_SYSCALL_64_after_hwframe+0x76/0x7e
 </TASK>

Commit 4aeaad1 ("bpf, sockmap: Check the target of an egress
redirect before each send") already refuses an egress redirect to such a
child, but only after lock_sock(). Do it before taking the lock: a child
has no sk_socket until accept() has taken it off the queue.

An ingress redirect to such a child works today and the data is read
after accept(), so it can't be refused. It only queues the data and
never sleeps, so take the lock of a child with lock_sock_fast() instead.
The child is not owned then and inet_csk_listen_stop() waits on
bh_lock_sock(). An accepted socket keeps lock_sock(). Under
lock_sock_fast() its memcg charge would be atomic and forced at the
limit. A child has no memcg until accept().

sock_map_free() and sock_hash_free() also lock the sockets in the map.
They can't refuse, so unlink such a child without the lock, as a delete
from the map already does.

A child with a strparser is still locked by strp_done() and the
strparser work. That is in the strparser code and is left for later.

Fixes: 604326b ("bpf, sockmap: convert to generic sk_msg interface")
Fixes: 7e81a35 ("bpf: Sockmap, ensure sock lock held during tear down")
Signed-off-by: Jiayuan Chen <[email protected]>
An sk_msg prog can parse the header of an application message and call
bpf_msg_apply_bytes() with the message length. The verdict then covers
the whole message, and the prog runs again at the next header:

  +-----+-----------+-----+-------+-----+------
  | hdr |   body    | hdr | body  | hdr | ...
  +-----+-----------+-----+-------+-----+------
  ^                 ^             ^
  prog: apply(len1) prog: apply(len2)
                                  prog: apply(len3)

For SK_REDIRECT, tcp_bpf_send_verdict() takes tosend bytes off
apply_bytes before the redirect. But the redirect can send only a part:
the ingress target can't get more memory, or the egress target has no
send buffer left. The rest stays in msg, or goes back to the user who
sends it again. The verdict has already used up these bytes, so the prog
runs on the rest, in the middle of a message:

  +-----+-----------+-----+-------+------
  | hdr |   body    | hdr | body  | ...
  +-----+-----------+-----+-------+------
  |<- apply(len1) ->|
  |<-- sent -->|
               ^
               prog runs here and takes body as a header

From then on the message boundaries are wrong.

Give back the bytes that were not sent, and keep the verdict while
apply_bytes is not 0. The bytes of a cork that fails are freed and
reported as sent, so they stay used.

A cork that is sent only in part is not fixed here. The rest is corked
again and the prog runs on it later, in the middle of the message as
before. That needs more work on the cork and is left for later.

This is for a target that is busy. If the target is dead, the verdict
is kept too and sendmsg() keeps failing. A part of the message is
already at the target and the rest can't go anywhere else, so the
message is lost anyway. Return the error and let the user decide.

Fixes: 604326b ("bpf, sockmap: convert to generic sk_msg interface")
Signed-off-by: Jiayuan Chen <[email protected]>
…ockmap

The test closes the redirect target while the sender waits for its send
memory. A part of the data has gone out, so sendmsg() returns that part
and the test calls sendmsg() again.

With apply_bytes, the previous patch keeps the verdict for the bytes that
were not sent. The next sendmsg() sends to the same target and returns
-EPIPE, instead of running the prog again, which no longer finds the
target in the map and returns SK_DROP, -EACCES.

Accept -EPIPE as well.

Signed-off-by: Jiayuan Chen <[email protected]>
An sk_msg prog parses the header of a message and applies its verdict
to the whole message with bpf_msg_apply_bytes(), so it runs once per
message, at the header:

  +-----+-----------+-----+-------+------
  | hdr |   body    | hdr | body  | ...
  +-----+-----------+-----+-------+------
  ^                 ^
  prog: apply(len1) prog: apply(len2)

The test sends one such message and redirects it to the egress of a
socket that has the smallest send buffer, whose peer has the smallest
receive buffer. The redirect sends only a part, so send() returns
early, and the test sends the rest:

  +-----+-----------+
  | hdr |   body    |
  +-----+-----------+
  |<-- send -->|
               |<-->| send again

Check that the prog ran once, at the header, and that the peer gets the
whole message.

Signed-off-by: Jiayuan Chen <[email protected]>
@mrpre

mrpre commented Dec 2, 2026

Copy link
Copy Markdown
Owner Author

@claude

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant