* [PATCH bpf v2 00/10] fix misc sockmap issues
@ 2026-12-01 12:18 Jiayuan Chen
2026-12-01 12:18 ` [PATCH bpf v2 01/10] bpf, sockmap: Fix copied_seq updates with strparser Jiayuan Chen
` (9 more replies)
0 siblings, 10 replies; 11+ messages in thread
From: Jiayuan Chen @ 2026-12-01 12:18 UTC (permalink / raw)
To: bpf
Cc: Jiayuan Chen, John Fastabend, Jakub Sitnicki, David S. Miller,
Eric Dumazet, Jakub Kicinski, Paolo Abeni, Simon Horman,
Kuniyuki Iwashima, Willem de Bruijn, Neal Cardwell,
Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko,
Eduard Zingerman, Kumar Kartikeya Dwivedi, Martin KaFai Lau,
Song Liu, Yonghong Song, Jiri Olsa, Emil Tsalapatis,
Ihor Solodrai, Shuah Khan, Sechang Lim, Mattia Meleleo,
Michal Luczaj, Zijian Zhang, linux-kernel, netdev,
linux-kselftest
This series fixes misc issues in sockmap. Most of them are old.
Patches 1-2 fix copied_seq when a strparser is attached and SK_PASS data
is left unread, with a selftest. It was reported by Vega and comes from
my earlier fix, commit 36b62df5683c ("bpf: Fix wrong copied_seq
calculation").
Patches 3, 4 and 7 are old issues I ran into while reading the code:
03 strparser is initialized again while the old one is still alive,
after the parser link is removed and added back
04 an ingress redirect queues an empty sk_msg
07 sk_psock_backlog() takes the refcnt of the wrong psock
The rest (patches 5, 6 and 8-11) are the old issues that came up in the
review of Alexei's sockmap series [1]. They are not from that series, so
they were left for a follow-up.
Patch 05 returns -EBUSY to a second concurrent sender, and patch 06
rejects disconnect with -EOPNOTSUPP like TLS does. Both change what
user space sees, see the commit messages for why.
[1] https://lore.kernel.org/bpf/20261102072112.4160195-1-alexei.starovoitov@gmail.com/
Jiayuan Chen (10):
bpf, sockmap: Fix copied_seq updates with strparser
selftests/bpf: Add test for unread SK_PASS data with strparser
bpf, sockmap: Fix empty sk_msg queued by ingress redirect
bpf, sockmap: Fix concurrent senders on one psock
bpf, sockmap: Fix reconnect with a psock by rejecting connect
bpf, sockmap: Fix psock refcnt taken in sk_psock_backlog()
bpf, sockmap: Fix socket lock taken on a child before accept()
bpf, sockmap: Fix apply_bytes when a redirect sends only a part
selftests/bpf: Fix errno expected by the wait send mem test of
test_sockmap
selftests/bpf: Add test for apply_bytes when a redirect sends a part
include/linux/skmsg.h | 1 +
net/core/skmsg.c | 30 ++++-
net/core/sock_map.c | 27 +++-
net/ipv4/tcp_bpf.c | 92 ++++++++++---
.../selftests/bpf/prog_tests/sockmap_basic.c | 126 ++++++++++++++++++
.../selftests/bpf/prog_tests/sockmap_strp.c | 109 +++++++++++++++
.../bpf/progs/test_sockmap_msg_apply.c | 46 +++++++
tools/testing/selftests/bpf/test_sockmap.c | 3 +-
8 files changed, 407 insertions(+), 27 deletions(-)
create mode 100644 tools/testing/selftests/bpf/progs/test_sockmap_msg_apply.c
--
2.43.0
^ permalink raw reply [flat|nested] 11+ messages in thread
* [PATCH bpf v2 01/10] bpf, sockmap: Fix copied_seq updates with strparser
2026-12-01 12:18 [PATCH bpf v2 00/10] fix misc sockmap issues Jiayuan Chen
@ 2026-12-01 12:18 ` Jiayuan Chen
2026-12-01 12:18 ` [PATCH bpf v2 02/10] selftests/bpf: Add test for unread SK_PASS data " Jiayuan Chen
` (8 subsequent siblings)
9 siblings, 0 replies; 11+ messages in thread
From: Jiayuan Chen @ 2026-12-01 12:18 UTC (permalink / raw)
To: bpf
Cc: Jiayuan Chen, stable, Vega, Alexei Starovoitov, Zhiling Zou,
John Fastabend, Jakub Sitnicki, David S. Miller, Eric Dumazet,
Jakub Kicinski, Paolo Abeni, Simon Horman, Kuniyuki Iwashima,
Willem de Bruijn, Neal Cardwell, Alexei Starovoitov,
Daniel Borkmann, Andrii Nakryiko, Eduard Zingerman,
Kumar Kartikeya Dwivedi, Martin KaFai Lau, Song Liu,
Yonghong Song, Jiri Olsa, Emil Tsalapatis, Ihor Solodrai,
Shuah Khan, Sechang Lim, Michal Luczaj, Mattia Meleleo,
Zijian Zhang, linux-kernel, netdev, linux-kselftest
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 36b62df5683c
("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: 36b62df5683c ("bpf: Fix wrong copied_seq calculation")
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>
Co-developed-by: Zhiling Zou <zhilinz@nebusec.ai>
Signed-off-by: Zhiling Zou <zhilinz@nebusec.ai>
Signed-off-by: Jiayuan Chen <jiayuan.chen@linux.dev>
---
The BUG_ON(offset < 0) in tcp_collapse() when copied_seq trails the head
of sk_receive_queue is not addressed here.
A message that spans several segments moves copied_seq back when it is
passed. That is fine, the old code did the same and recvmsg() adds the
bytes back.
---
net/core/skmsg.c | 26 ++++++++++++++++++++------
net/ipv4/tcp_bpf.c | 12 ++++++------
2 files changed, 26 insertions(+), 12 deletions(-)
diff --git a/net/core/skmsg.c b/net/core/skmsg.c
index 8a3b8536c0c21..b81abb3da379a 100644
--- a/net/core/skmsg.c
+++ b/net/core/skmsg.c
@@ -574,9 +574,6 @@ static int sk_psock_skb_ingress_enqueue(struct sk_buff *skb,
return num_sge;
}
-#if IS_ENABLED(CONFIG_BPF_STREAM_PARSER)
- psock->ingress_bytes += len;
-#endif
copied = len;
msg->sg.start = 0;
msg->sg.size = copied;
@@ -616,8 +613,11 @@ static int sk_psock_skb_ingress(struct sk_psock *psock, struct sk_buff *skb,
*/
skb_set_owner_r(skb, sk);
err = sk_psock_skb_ingress_enqueue(skb, off, len, psock, sk, msg, true);
- if (err < 0)
+ if (err < 0) {
kfree(msg);
+ /* Same as in sk_psock_skb_ingress_self() */
+ skb_orphan(skb);
+ }
return err;
}
@@ -642,8 +642,21 @@ static int sk_psock_skb_ingress_self(struct sk_psock *psock, struct sk_buff *skb
*/
msg->sk = sk;
err = sk_psock_skb_ingress_enqueue(skb, off, len, psock, sk, msg, take_ref);
- if (err < 0)
+ if (err < 0) {
kfree(msg);
+ /*
+ * A strparser skb comes in without an owner and read_sock counts
+ * it as consumed once it is left to the backlog. Drop the owner
+ * again, or sk_psock_skb_ingress() takes the skb->sk == sk path
+ * and recvmsg() counts it a second time.
+ */
+ if (skb_bpf_strparser(skb))
+ skb_orphan(skb);
+ }
+#if IS_ENABLED(CONFIG_BPF_STREAM_PARSER)
+ else
+ psock->ingress_bytes += len;
+#endif
return err;
}
@@ -1061,8 +1074,9 @@ static int sk_psock_verdict_apply(struct sk_psock *psock, struct sk_buff *skb,
case __SK_DROP:
default:
out_free:
- skb_bpf_redirect_clear(skb);
+ /* tcp_eat_skb() checks the strparser bit, clear it after */
tcp_eat_skb(psock->sk, skb);
+ skb_bpf_redirect_clear(skb);
sock_drop(psock->sk, skb);
}
diff --git a/net/ipv4/tcp_bpf.c b/net/ipv4/tcp_bpf.c
index 788ce08eef4d7..e59103e77aa5f 100644
--- a/net/ipv4/tcp_bpf.c
+++ b/net/ipv4/tcp_bpf.c
@@ -249,7 +249,6 @@ static int tcp_bpf_recvmsg_parser(struct sock *sk,
struct tcp_sock *tcp;
int copied_from_self = 0;
int copied = 0;
- u32 seq;
if (unlikely(flags & MSG_ERRQUEUE))
return inet_recv_error(sk, msg, len);
@@ -263,7 +262,6 @@ static int tcp_bpf_recvmsg_parser(struct sock *sk,
lock_sock(sk);
tcp = tcp_sk(sk);
- seq = tcp->copied_seq;
/* We may have received data on the sk_receive_queue pre-accept and
* then we can not use read_skb in this context because we haven't
* assigned a sk_socket yet so have no link to the ops. The work-around
@@ -295,11 +293,10 @@ static int tcp_bpf_recvmsg_parser(struct sock *sk,
if (is_fin) {
copied = 0;
- seq++;
+ copied_from_self++;
goto out;
}
}
- seq += copied_from_self;
if (!copied) {
long timeo;
int data;
@@ -341,7 +338,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);
/* Data on the psock outlives the connection that it came in on */
if (copied > 0 && tcp_can_send_ack(sk))
@@ -756,6 +753,7 @@ int tcp_bpf_strp_read_sock(struct strparser *strp, read_descriptor_t *desc,
struct sk_psock *psock;
struct tcp_sock *tp;
int copied = 0;
+ u32 seq;
tp = tcp_sk(sk);
rcu_read_lock();
@@ -765,6 +763,7 @@ int tcp_bpf_strp_read_sock(struct strparser *strp, read_descriptor_t *desc,
goto out;
}
+ seq = psock->copied_seq;
psock->ingress_bytes = 0;
copied = tcp_read_sock_noack(sk, desc, recv_actor, true,
&psock->copied_seq);
@@ -775,7 +774,8 @@ int tcp_bpf_strp_read_sock(struct strparser *strp, read_descriptor_t *desc,
* For SK_REDIRECT, we need to ack the frame immediately but for
* SK_PASS, we want to delay the ack until tcp_bpf_recvmsg_parser().
*/
- tp->copied_seq = psock->copied_seq - psock->ingress_bytes;
+ WRITE_ONCE(tp->copied_seq, tp->copied_seq + psock->copied_seq - seq -
+ psock->ingress_bytes);
tcp_rcv_space_adjust(sk);
__tcp_cleanup_rbuf(sk, copied - psock->ingress_bytes);
out:
--
2.43.0
^ permalink raw reply [flat|nested] 11+ messages in thread
* [PATCH bpf v2 02/10] selftests/bpf: Add test for unread SK_PASS data with strparser
2026-12-01 12:18 [PATCH bpf v2 00/10] fix misc sockmap issues Jiayuan Chen
2026-12-01 12:18 ` [PATCH bpf v2 01/10] bpf, sockmap: Fix copied_seq updates with strparser Jiayuan Chen
@ 2026-12-01 12:18 ` Jiayuan Chen
2026-12-01 12:18 ` [PATCH bpf v2 03/10] bpf, sockmap: Fix empty sk_msg queued by ingress redirect Jiayuan Chen
` (7 subsequent siblings)
9 siblings, 0 replies; 11+ messages in thread
From: Jiayuan Chen @ 2026-12-01 12:18 UTC (permalink / raw)
To: bpf
Cc: Jiayuan Chen, John Fastabend, Jakub Sitnicki, David S. Miller,
Eric Dumazet, Jakub Kicinski, Paolo Abeni, Simon Horman,
Kuniyuki Iwashima, Willem de Bruijn, Neal Cardwell,
Andrii Nakryiko, Eduard Zingerman, Ihor Solodrai,
Alexei Starovoitov, Daniel Borkmann, Kumar Kartikeya Dwivedi,
Martin KaFai Lau, Song Liu, Yonghong Song, Jiri Olsa,
Emil Tsalapatis, Shuah Khan, Sechang Lim, Michal Luczaj,
Mattia Meleleo, Zijian Zhang, linux-kernel, netdev,
linux-kselftest
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 <jiayuan.chen@linux.dev>
---
.../selftests/bpf/prog_tests/sockmap_strp.c | 109 ++++++++++++++++++
1 file changed, 109 insertions(+)
diff --git a/tools/testing/selftests/bpf/prog_tests/sockmap_strp.c b/tools/testing/selftests/bpf/prog_tests/sockmap_strp.c
index 1d7231728eafd..56832db867107 100644
--- a/tools/testing/selftests/bpf/prog_tests/sockmap_strp.c
+++ b/tools/testing/selftests/bpf/prog_tests/sockmap_strp.c
@@ -364,6 +364,89 @@ static void test_sockmap_strp_pass(int family, int sotype, bool fionread)
test_sockmap_strp__destroy(strp);
}
+static int sockmap_strp_wait_fionread(int fd, int expected)
+{
+ int err, avail = 0;
+
+ /* No limit on the loop, test_progs has its own watchdog */
+ while (1) {
+ err = ioctl(fd, FIONREAD, &avail);
+ if (!ASSERT_OK(err, "ioctl(FIONREAD) error"))
+ return -1;
+ if (avail >= expected)
+ break;
+ usleep(10000);
+ }
+ return ASSERT_EQ(avail, expected, "ioctl(FIONREAD)") ? 0 : -1;
+}
+
+/*
+ * Let the parser run twice before the reader reads anything. Each round
+ * must keep the SK_PASS bytes of the earlier rounds out of copied_seq,
+ * otherwise copied_seq runs past rcv_nxt and the stream stalls.
+ */
+static void test_sockmap_strp_pass_unread(int family, int sotype)
+{
+ int zero = 0, one = 1, pkt_size = STRP_PKT_FULL_LEN, sent, recvd;
+ int err, map, i;
+ int c = -1, p = -1;
+ struct test_sockmap_strp *strp = NULL;
+ char rcv[STRP_PKT_FULL_LEN * 2 + 1] = "0";
+
+ strp = sockmap_strp_init(&map, true, true);
+ if (!ASSERT_TRUE(strp, "sockmap_strp_init"))
+ return;
+
+ err = create_pair(family, sotype, &c, &p);
+ if (err)
+ goto out;
+
+ err = bpf_map_update_elem(map, &zero, &p, BPF_NOEXIST);
+ if (!ASSERT_OK(err, "bpf_map_update_elem(p)"))
+ goto out_close;
+
+ err = setsockopt(c, IPPROTO_TCP, TCP_NODELAY, &one, sizeof(one));
+ if (!ASSERT_OK(err, "setsockopt(TCP_NODELAY)"))
+ goto out_close;
+
+ /*
+ * wait for each packet to reach ingress_msg before sending the
+ * next one, so that the parser runs once per packet
+ */
+ for (i = 1; i <= 2; i++) {
+ sent = xsend(c, packet, pkt_size, 0);
+ if (!ASSERT_EQ(sent, pkt_size, "xsend(c)"))
+ goto out_close;
+ if (sockmap_strp_wait_fionread(p, pkt_size * i))
+ goto out_close;
+ }
+
+ recvd = recv_timeout(p, rcv, sizeof(rcv), MSG_DONTWAIT, IO_TIMEOUT_SEC);
+ if (!ASSERT_EQ(recvd, pkt_size * 2, "recv_timeout(p)") ||
+ !ASSERT_OK(memcmp(packet, rcv, pkt_size), "memcmp, data mismatch") ||
+ !ASSERT_OK(memcmp(packet, rcv + pkt_size, pkt_size),
+ "memcmp, second data mismatch"))
+ goto out_close;
+
+ /* the next packet must still wake the parser */
+ sent = xsend(c, packet, pkt_size, 0);
+ if (!ASSERT_EQ(sent, pkt_size, "xsend(c) after read"))
+ goto out_close;
+
+ recvd = recv_timeout(p, rcv, sizeof(rcv), MSG_DONTWAIT, IO_TIMEOUT_SEC);
+ if (!ASSERT_EQ(recvd, pkt_size, "recv_timeout(p) after read") ||
+ !ASSERT_OK(memcmp(packet, rcv, pkt_size),
+ "memcmp, data mismatch after read"))
+ goto out_close;
+
+out_close:
+ close(c);
+ close(p);
+
+out:
+ test_sockmap_strp__destroy(strp);
+}
+
/* Test strparser with verdict mode */
static void test_sockmap_strp_verdict(int family, int sotype)
{
@@ -422,6 +505,28 @@ static void test_sockmap_strp_verdict(int family, int sotype)
"partial received data does not match the sent data"))
goto out_close;
+ /* a redirect to an empty slot is SK_DROP, the next one must pass */
+ err = bpf_map_delete_elem(map, &one);
+ if (!ASSERT_OK(err, "bpf_map_delete_elem(p1)"))
+ goto out_close;
+
+ sent = xsend(c0, packet, STRP_PKT_FULL_LEN, 0);
+ if (!ASSERT_EQ(sent, STRP_PKT_FULL_LEN, "dropped xsend(c0)"))
+ goto out_close;
+
+ err = bpf_map_update_elem(map, &one, &p1, BPF_NOEXIST);
+ if (!ASSERT_OK(err, "bpf_map_update_elem(p1) again"))
+ goto out_close;
+
+ sent = xsend(c0, packet, STRP_PKT_FULL_LEN, 0);
+ if (!ASSERT_EQ(sent, STRP_PKT_FULL_LEN, "xsend(c0) after drop"))
+ goto out_close;
+
+ recvd = recv_timeout(c1, rcv, sizeof(rcv), MSG_DONTWAIT,
+ IO_TIMEOUT_SEC);
+ if (!ASSERT_EQ(recvd, STRP_PKT_FULL_LEN, "recv_timeout(c1) after drop"))
+ goto out_close;
+
out_close:
close(c0);
close(c1);
@@ -470,6 +575,10 @@ void test_sockmap_strp(void)
test_sockmap_strp_pass(AF_INET, SOCK_STREAM, true);
if (test__start_subtest("sockmap strp tcp v6 pass fionread"))
test_sockmap_strp_pass(AF_INET6, SOCK_STREAM, true);
+ if (test__start_subtest("sockmap strp tcp pass unread"))
+ test_sockmap_strp_pass_unread(AF_INET, SOCK_STREAM);
+ if (test__start_subtest("sockmap strp tcp v6 pass unread"))
+ test_sockmap_strp_pass_unread(AF_INET6, SOCK_STREAM);
if (test__start_subtest("sockmap strp tcp verdict"))
test_sockmap_strp_verdict(AF_INET, SOCK_STREAM);
if (test__start_subtest("sockmap strp tcp v6 verdict"))
--
2.43.0
^ permalink raw reply [flat|nested] 11+ messages in thread
* [PATCH bpf v2 03/10] bpf, sockmap: Fix empty sk_msg queued by ingress redirect
2026-12-01 12:18 [PATCH bpf v2 00/10] fix misc sockmap issues Jiayuan Chen
2026-12-01 12:18 ` [PATCH bpf v2 01/10] bpf, sockmap: Fix copied_seq updates with strparser Jiayuan Chen
2026-12-01 12:18 ` [PATCH bpf v2 02/10] selftests/bpf: Add test for unread SK_PASS data " Jiayuan Chen
@ 2026-12-01 12:18 ` Jiayuan Chen
2026-12-01 12:18 ` [PATCH bpf v2 04/10] bpf, sockmap: Fix concurrent senders on one psock Jiayuan Chen
` (6 subsequent siblings)
9 siblings, 0 replies; 11+ messages in thread
From: Jiayuan Chen @ 2026-12-01 12:18 UTC (permalink / raw)
To: bpf
Cc: Jiayuan Chen, John Fastabend, Jakub Sitnicki, David S. Miller,
Eric Dumazet, Jakub Kicinski, Paolo Abeni, Simon Horman,
Kuniyuki Iwashima, Willem de Bruijn, Neal Cardwell,
Andrii Nakryiko, Eduard Zingerman, Ihor Solodrai,
Alexei Starovoitov, Daniel Borkmann, Kumar Kartikeya Dwivedi,
Martin KaFai Lau, Song Liu, Yonghong Song, Jiri Olsa,
Emil Tsalapatis, Shuah Khan, Sechang Lim, Mattia Meleleo,
Michal Luczaj, Zijian Zhang, linux-kernel, netdev,
linux-kselftest
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: 5d609ba26247 ("bpf, sockmap: Several fixes to bpf_msg_pop_data")
Signed-off-by: Jiayuan Chen <jiayuan.chen@linux.dev>
---
net/ipv4/tcp_bpf.c | 3 +++
1 file changed, 3 insertions(+)
diff --git a/net/ipv4/tcp_bpf.c b/net/ipv4/tcp_bpf.c
index e59103e77aa5f..7191b95c7b1c7 100644
--- a/net/ipv4/tcp_bpf.c
+++ b/net/ipv4/tcp_bpf.c
@@ -39,6 +39,9 @@ static int bpf_tcp_ingress(struct sock *sk, struct sk_psock *psock,
struct sk_msg *tmp;
int i, ret = 0;
+ if (!msg->sg.size)
+ return 0;
+
tmp = kzalloc_obj(*tmp, __GFP_NOWARN | GFP_KERNEL);
if (unlikely(!tmp))
return -ENOMEM;
--
2.43.0
^ permalink raw reply [flat|nested] 11+ messages in thread
* [PATCH bpf v2 04/10] bpf, sockmap: Fix concurrent senders on one psock
2026-12-01 12:18 [PATCH bpf v2 00/10] fix misc sockmap issues Jiayuan Chen
` (2 preceding siblings ...)
2026-12-01 12:18 ` [PATCH bpf v2 03/10] bpf, sockmap: Fix empty sk_msg queued by ingress redirect Jiayuan Chen
@ 2026-12-01 12:18 ` Jiayuan Chen
2026-12-01 12:18 ` [PATCH bpf v2 05/10] bpf, sockmap: Fix reconnect with a psock by rejecting connect Jiayuan Chen
` (5 subsequent siblings)
9 siblings, 0 replies; 11+ messages in thread
From: Jiayuan Chen @ 2026-12-01 12:18 UTC (permalink / raw)
To: bpf
Cc: Jiayuan Chen, John Fastabend, Jakub Sitnicki, David S. Miller,
Eric Dumazet, Jakub Kicinski, Paolo Abeni, Simon Horman,
Kuniyuki Iwashima, Willem de Bruijn, Neal Cardwell,
Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko,
Eduard Zingerman, Kumar Kartikeya Dwivedi, Martin KaFai Lau,
Song Liu, Yonghong Song, Jiri Olsa, Emil Tsalapatis,
Ihor Solodrai, Shuah Khan, Sechang Lim, Michal Luczaj,
Mattia Meleleo, Zijian Zhang, linux-kernel, netdev,
linux-kselftest
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: 4f738adba30a ("bpf: create tcp_bpf_ulp allowing BPF to monitor socket TX/RX data")
Signed-off-by: Jiayuan Chen <jiayuan.chen@linux.dev>
---
include/linux/skmsg.h | 1 +
net/core/skmsg.c | 2 ++
net/ipv4/tcp_bpf.c | 14 +++++++++++++-
3 files changed, 16 insertions(+), 1 deletion(-)
diff --git a/include/linux/skmsg.h b/include/linux/skmsg.h
index f0e27b4b929f1..a86d58088f2ad 100644
--- a/include/linux/skmsg.h
+++ b/include/linux/skmsg.h
@@ -118,6 +118,7 @@ struct sk_psock {
bool restore);
struct proto *sk_proto;
struct mutex work_mutex;
+ struct mutex tx_mutex;
struct sk_psock_work_state work_state;
struct delayed_work work;
struct sock *sk_pair;
diff --git a/net/core/skmsg.c b/net/core/skmsg.c
index b81abb3da379a..dcae3cd968edc 100644
--- a/net/core/skmsg.c
+++ b/net/core/skmsg.c
@@ -801,6 +801,7 @@ struct sk_psock *sk_psock_init(struct sock *sk, int node)
INIT_DELAYED_WORK(&psock->work, sk_psock_backlog);
mutex_init(&psock->work_mutex);
+ mutex_init(&psock->tx_mutex);
INIT_LIST_HEAD(&psock->ingress_msg);
spin_lock_init(&psock->ingress_lock);
skb_queue_head_init(&psock->ingress_skb);
@@ -889,6 +890,7 @@ static void sk_psock_destroy(struct work_struct *work)
cancel_delayed_work_sync(&psock->work);
__sk_psock_zap_ingress(psock);
mutex_destroy(&psock->work_mutex);
+ mutex_destroy(&psock->tx_mutex);
psock_progs_drop(&psock->progs);
diff --git a/net/ipv4/tcp_bpf.c b/net/ipv4/tcp_bpf.c
index 7191b95c7b1c7..49b34941bf13c 100644
--- a/net/ipv4/tcp_bpf.c
+++ b/net/ipv4/tcp_bpf.c
@@ -602,8 +602,19 @@ static int tcp_bpf_sendmsg(struct sock *sk, struct msghdr *msg, size_t size)
if (unlikely(!psock))
return tcp_sendmsg(sk, msg, size);
- lock_sock(sk);
timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);
+ /*
+ * The verdict, apply_bytes, sk_redir and the cork live in the psock
+ * and span the places where the socket lock is dropped below. They
+ * can only serve one sender at a time.
+ */
+ err = mutex_lock_interruptible(&psock->tx_mutex);
+ if (err) {
+ sk_psock_put(sk, psock);
+ return sock_intr_errno(timeo);
+ }
+
+ lock_sock(sk);
while (msg_data_left(msg)) {
bool enospc = false;
u32 copy, osize;
@@ -677,6 +688,7 @@ static int tcp_bpf_sendmsg(struct sock *sk, struct msghdr *msg, size_t size)
if (err < 0)
err = sk_stream_error(sk, msg->msg_flags, err);
release_sock(sk);
+ mutex_unlock(&psock->tx_mutex);
sk_psock_put(sk, psock);
return copied > 0 ? copied : err;
}
--
2.43.0
^ permalink raw reply [flat|nested] 11+ messages in thread
* [PATCH bpf v2 05/10] bpf, sockmap: Fix reconnect with a psock by rejecting connect
2026-12-01 12:18 [PATCH bpf v2 00/10] fix misc sockmap issues Jiayuan Chen
` (3 preceding siblings ...)
2026-12-01 12:18 ` [PATCH bpf v2 04/10] bpf, sockmap: Fix concurrent senders on one psock Jiayuan Chen
@ 2026-12-01 12:18 ` Jiayuan Chen
2026-12-01 12:18 ` [PATCH bpf v2 06/10] bpf, sockmap: Fix psock refcnt taken in sk_psock_backlog() Jiayuan Chen
` (4 subsequent siblings)
9 siblings, 0 replies; 11+ messages in thread
From: Jiayuan Chen @ 2026-12-01 12:18 UTC (permalink / raw)
To: bpf
Cc: Jiayuan Chen, John Fastabend, Jakub Sitnicki, David S. Miller,
Eric Dumazet, Jakub Kicinski, Paolo Abeni, Simon Horman,
Kuniyuki Iwashima, Willem de Bruijn, Neal Cardwell,
Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko,
Eduard Zingerman, Kumar Kartikeya Dwivedi, Martin KaFai Lau,
Song Liu, Yonghong Song, Jiri Olsa, Emil Tsalapatis,
Ihor Solodrai, Shuah Khan, Sechang Lim, Michal Luczaj,
Mattia Meleleo, Zijian Zhang, linux-kernel, netdev,
linux-kselftest
Commit 8cc3aef0cb19 ("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 5071a1e606b3 ("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: b8b8315e39ff ("bpf, sockmap: Remove unhash handler for BPF sockmap usage")
Signed-off-by: Jiayuan Chen <jiayuan.chen@linux.dev>
---
net/ipv4/tcp_bpf.c | 11 +++++++++++
1 file changed, 11 insertions(+)
diff --git a/net/ipv4/tcp_bpf.c b/net/ipv4/tcp_bpf.c
index 49b34941bf13c..37d90ed5d8298 100644
--- a/net/ipv4/tcp_bpf.c
+++ b/net/ipv4/tcp_bpf.c
@@ -711,12 +711,23 @@ static struct proto *tcpv6_prot_saved __read_mostly;
static DEFINE_SPINLOCK(tcpv6_prot_lock);
static struct proto tcp_bpf_prots[TCP_BPF_NUM_PROTS][TCP_BPF_NUM_CFGS];
+static int tcp_bpf_connect(struct sock *sk, struct sockaddr_unsized *uaddr,
+ int addr_len)
+{
+ /*
+ * Only a socket that was disconnected gets here with a psock. A new
+ * connection must not reuse it.
+ */
+ return -EOPNOTSUPP;
+}
+
static void tcp_bpf_rebuild_protos(struct proto prot[TCP_BPF_NUM_CFGS],
struct proto *base)
{
prot[TCP_BPF_BASE] = *base;
prot[TCP_BPF_BASE].destroy = sock_map_destroy;
prot[TCP_BPF_BASE].close = sock_map_close;
+ prot[TCP_BPF_BASE].connect = tcp_bpf_connect;
prot[TCP_BPF_BASE].recvmsg = tcp_bpf_recvmsg;
prot[TCP_BPF_BASE].sock_is_readable = sk_msg_is_readable;
prot[TCP_BPF_BASE].ioctl = tcp_bpf_ioctl;
--
2.43.0
^ permalink raw reply [flat|nested] 11+ messages in thread
* [PATCH bpf v2 06/10] bpf, sockmap: Fix psock refcnt taken in sk_psock_backlog()
2026-12-01 12:18 [PATCH bpf v2 00/10] fix misc sockmap issues Jiayuan Chen
` (4 preceding siblings ...)
2026-12-01 12:18 ` [PATCH bpf v2 05/10] bpf, sockmap: Fix reconnect with a psock by rejecting connect Jiayuan Chen
@ 2026-12-01 12:18 ` Jiayuan Chen
2026-12-01 12:18 ` [PATCH bpf v2 07/10] bpf, sockmap: Fix socket lock taken on a child before accept() Jiayuan Chen
` (3 subsequent siblings)
9 siblings, 0 replies; 11+ messages in thread
From: Jiayuan Chen @ 2026-12-01 12:18 UTC (permalink / raw)
To: bpf
Cc: Jiayuan Chen, John Fastabend, Jakub Sitnicki, David S. Miller,
Eric Dumazet, Jakub Kicinski, Paolo Abeni, Simon Horman,
Kuniyuki Iwashima, Willem de Bruijn, Neal Cardwell,
Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko,
Eduard Zingerman, Kumar Kartikeya Dwivedi, Martin KaFai Lau,
Song Liu, Yonghong Song, Jiri Olsa, Emil Tsalapatis,
Ihor Solodrai, Shuah Khan, Sechang Lim, Mattia Meleleo,
Michal Luczaj, Zijian Zhang, linux-kernel, netdev,
linux-kselftest
sk_psock_backlog() runs for a given psock, but takes the reference with
sk_psock_get(psock->sk), which returns the psock that sk_user_data points
to now. That is the same psock only while psock->sk->psock == psock.
If sk is removed from the map and added back while the backlog runs, the
old psock is dropped and sk_user_data points to the new one. The backlog
of the old psock then takes a reference on the new psock and puts the old
one, whose refcnt is already 0. The old one hits a refcount underflow and
the new one leaks a reference. The SK_PSOCK_TX_ENABLED check before it
does not close this window.
Take the reference on the psock itself.
Fixes: 8259eb0e06d8 ("bpf, sockmap: Avoid using sk_socket after free when sending")
Signed-off-by: Jiayuan Chen <jiayuan.chen@linux.dev>
---
net/core/skmsg.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/net/core/skmsg.c b/net/core/skmsg.c
index dcae3cd968edc..da9b60f34e2df 100644
--- a/net/core/skmsg.c
+++ b/net/core/skmsg.c
@@ -707,7 +707,7 @@ static void sk_psock_backlog(struct work_struct *work)
* before sk_socket freed. If refcnt increment fails, it indicates
* sock_map_close() completed with sk_socket potentially already freed.
*/
- if (!sk_psock_get(psock->sk))
+ if (!refcount_inc_not_zero(&psock->refcnt))
return;
mutex_lock(&psock->work_mutex);
while ((skb = skb_peek(&psock->ingress_skb))) {
--
2.43.0
^ permalink raw reply [flat|nested] 11+ messages in thread
* [PATCH bpf v2 07/10] bpf, sockmap: Fix socket lock taken on a child before accept()
2026-12-01 12:18 [PATCH bpf v2 00/10] fix misc sockmap issues Jiayuan Chen
` (5 preceding siblings ...)
2026-12-01 12:18 ` [PATCH bpf v2 06/10] bpf, sockmap: Fix psock refcnt taken in sk_psock_backlog() Jiayuan Chen
@ 2026-12-01 12:18 ` Jiayuan Chen
2026-12-01 12:18 ` [PATCH bpf v2 08/10] bpf, sockmap: Fix apply_bytes when a redirect sends only a part Jiayuan Chen
` (2 subsequent siblings)
9 siblings, 0 replies; 11+ messages in thread
From: Jiayuan Chen @ 2026-12-01 12:18 UTC (permalink / raw)
To: bpf
Cc: Jiayuan Chen, John Fastabend, Jakub Sitnicki, David S. Miller,
Eric Dumazet, Jakub Kicinski, Paolo Abeni, Simon Horman,
Kuniyuki Iwashima, Willem de Bruijn, Neal Cardwell,
Andrii Nakryiko, Eduard Zingerman, Ihor Solodrai,
Alexei Starovoitov, Daniel Borkmann, Kumar Kartikeya Dwivedi,
Martin KaFai Lau, Song Liu, Yonghong Song, Jiri Olsa,
Emil Tsalapatis, Shuah Khan, Sechang Lim, Mattia Meleleo,
Michal Luczaj, Zijian Zhang, linux-kernel, netdev,
linux-kselftest
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 4aeaad1c536e ("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: 604326b41a6f ("bpf, sockmap: convert to generic sk_msg interface")
Fixes: 7e81a3530206 ("bpf: Sockmap, ensure sock lock held during tear down")
Signed-off-by: Jiayuan Chen <jiayuan.chen@linux.dev>
---
net/core/sock_map.c | 27 +++++++++++++++++++++++----
net/ipv4/tcp_bpf.c | 30 +++++++++++++++++++++++++++---
2 files changed, 50 insertions(+), 7 deletions(-)
diff --git a/net/core/sock_map.c b/net/core/sock_map.c
index 282c6a4b2b05f..b86b37dd26e9b 100644
--- a/net/core/sock_map.c
+++ b/net/core/sock_map.c
@@ -350,6 +350,19 @@ static int sock_map_link(struct bpf_map *map, struct sock *sk)
return ret;
}
+/*
+ * The listener frees a child that nobody has accepted yet without waiting
+ * for the owner of its socket lock. Unlink such a child without the lock,
+ * as a delete does.
+ */
+static bool sock_map_free_lock_sk(struct sock *sk)
+{
+ if (!READ_ONCE(sk->sk_socket))
+ return false;
+ lock_sock(sk);
+ return true;
+}
+
static void sock_map_free(struct bpf_map *map)
{
struct bpf_stab *stab = container_of(map, struct bpf_stab, map);
@@ -366,12 +379,15 @@ static void sock_map_free(struct bpf_map *map)
sk = xchg(psk, NULL);
if (sk) {
+ bool locked;
+
sock_hold(sk);
- lock_sock(sk);
+ locked = sock_map_free_lock_sk(sk);
rcu_read_lock();
sock_map_unref(sk, psk);
rcu_read_unlock();
- release_sock(sk);
+ if (locked)
+ release_sock(sk);
sock_put(sk);
}
}
@@ -1191,12 +1207,15 @@ static void sock_hash_free(struct bpf_map *map)
* link to sockhash.
*/
hlist_for_each_entry_safe(elem, node, &unlink_list, node) {
+ bool locked;
+
hlist_del(&elem->node);
- lock_sock(elem->sk);
+ locked = sock_map_free_lock_sk(elem->sk);
rcu_read_lock();
sock_map_unref(elem->sk, elem);
rcu_read_unlock();
- release_sock(elem->sk);
+ if (locked)
+ release_sock(elem->sk);
sock_put(elem->sk);
sock_hash_free_elem(htab, elem);
}
diff --git a/net/ipv4/tcp_bpf.c b/net/ipv4/tcp_bpf.c
index 37d90ed5d8298..5b4444ee6d881 100644
--- a/net/ipv4/tcp_bpf.c
+++ b/net/ipv4/tcp_bpf.c
@@ -30,6 +30,21 @@ void tcp_eat_skb(struct sock *sk, struct sk_buff *skb)
__tcp_cleanup_rbuf(sk, skb->len);
}
+/*
+ * A child that nobody has accepted yet is freed by the listener under
+ * bh_lock_sock(), which doesn't expect an owner.
+ */
+static bool bpf_tcp_ingress_lock(struct sock *sk)
+ __acquires(&sk->sk_lock.slock)
+{
+ if (!READ_ONCE(sk->sk_socket))
+ return lock_sock_fast(sk);
+
+ lock_sock(sk);
+ __acquire(&sk->sk_lock.slock);
+ return true;
+}
+
static int bpf_tcp_ingress(struct sock *sk, struct sk_psock *psock,
struct sk_msg *msg, u32 apply_bytes)
{
@@ -38,6 +53,7 @@ static int bpf_tcp_ingress(struct sock *sk, struct sk_psock *psock,
u32 size, copied = 0;
struct sk_msg *tmp;
int i, ret = 0;
+ bool slow;
if (!msg->sg.size)
return 0;
@@ -46,7 +62,7 @@ static int bpf_tcp_ingress(struct sock *sk, struct sk_psock *psock,
if (unlikely(!tmp))
return -ENOMEM;
- lock_sock(sk);
+ slow = bpf_tcp_ingress_lock(sk);
tmp->sg.start = msg->sg.start;
i = msg->sg.start;
do {
@@ -87,7 +103,7 @@ static int bpf_tcp_ingress(struct sock *sk, struct sk_psock *psock,
kfree(tmp);
}
- release_sock(sk);
+ unlock_sock_fast(sk, slow);
return ret;
}
@@ -188,9 +204,17 @@ static int tcp_bpf_push_locked(struct sock *sk, struct sk_msg *msg,
int tcp_bpf_sendmsg_redir(struct sock *sk, bool ingress,
struct sk_msg *msg, u32 bytes, int flags)
{
- struct sk_psock *psock = sk_psock_get(sk);
+ struct sk_psock *psock;
int ret;
+ /*
+ * The listener frees a child that nobody has accepted yet without
+ * waiting for the owner of its socket lock. Ingress doesn't own it.
+ */
+ if (!ingress && !READ_ONCE(sk->sk_socket))
+ return sock_flag(sk, SOCK_DEAD) ? -EPIPE : -EINVAL;
+
+ psock = sk_psock_get(sk);
if (unlikely(!psock))
return -EPIPE;
--
2.43.0
^ permalink raw reply [flat|nested] 11+ messages in thread
* [PATCH bpf v2 08/10] bpf, sockmap: Fix apply_bytes when a redirect sends only a part
2026-12-01 12:18 [PATCH bpf v2 00/10] fix misc sockmap issues Jiayuan Chen
` (6 preceding siblings ...)
2026-12-01 12:18 ` [PATCH bpf v2 07/10] bpf, sockmap: Fix socket lock taken on a child before accept() Jiayuan Chen
@ 2026-12-01 12:18 ` Jiayuan Chen
2026-12-01 12:18 ` [PATCH bpf v2 09/10] selftests/bpf: Fix errno expected by the wait send mem test of test_sockmap Jiayuan Chen
2026-12-01 12:18 ` [PATCH bpf v2 10/10] selftests/bpf: Add test for apply_bytes when a redirect sends a part Jiayuan Chen
9 siblings, 0 replies; 11+ messages in thread
From: Jiayuan Chen @ 2026-12-01 12:18 UTC (permalink / raw)
To: bpf
Cc: Jiayuan Chen, John Fastabend, Jakub Sitnicki, David S. Miller,
Eric Dumazet, Jakub Kicinski, Paolo Abeni, Simon Horman,
Kuniyuki Iwashima, Willem de Bruijn, Neal Cardwell,
Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko,
Eduard Zingerman, Kumar Kartikeya Dwivedi, Martin KaFai Lau,
Song Liu, Yonghong Song, Jiri Olsa, Emil Tsalapatis,
Ihor Solodrai, Shuah Khan, Sechang Lim, Mattia Meleleo,
Michal Luczaj, Zijian Zhang, linux-kernel, netdev,
linux-kselftest
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: 604326b41a6f ("bpf, sockmap: convert to generic sk_msg interface")
Signed-off-by: Jiayuan Chen <jiayuan.chen@linux.dev>
---
net/ipv4/tcp_bpf.c | 22 +++++++++++++++++-----
1 file changed, 17 insertions(+), 5 deletions(-)
diff --git a/net/ipv4/tcp_bpf.c b/net/ipv4/tcp_bpf.c
index 5b4444ee6d881..27e9981698787 100644
--- a/net/ipv4/tcp_bpf.c
+++ b/net/ipv4/tcp_bpf.c
@@ -465,7 +465,7 @@ static int tcp_bpf_send_verdict(struct sock *sk, struct sk_psock *psock,
{
bool cork = false, enospc = sk_msg_full(msg), redir_ingress;
struct sock *sk_redir;
- u32 tosend, origsize, sent, delta = 0;
+ u32 tosend, origsize, sent, apply, delta = 0;
u32 eval;
int ret;
@@ -534,6 +534,7 @@ static int tcp_bpf_send_verdict(struct sock *sk, struct sk_psock *psock,
redir_ingress = psock->redir_ingress;
sk_redir = psock->sk_redir;
sock_hold(sk_redir);
+ apply = psock->apply_bytes;
sk_msg_apply_bytes(psock, tosend);
if (!psock->apply_bytes) {
/* Clean up before releasing the sock lock. */
@@ -556,10 +557,6 @@ static int tcp_bpf_send_verdict(struct sock *sk, struct sk_psock *psock,
msg, tosend, flags);
sent = origsize - msg->sg.size;
- if (eval == __SK_REDIRECT)
- sock_put(sk_redir);
- sock_put(sk_redir);
-
lock_sock(sk);
sk_mem_uncharge(sk, sent);
if (unlikely(ret < 0)) {
@@ -582,6 +579,21 @@ static int tcp_bpf_send_verdict(struct sock *sk, struct sk_psock *psock,
msg = NULL;
ret = 0;
}
+ /*
+ * Bytes that were not sent and come back, in msg or in the
+ * next sendmsg(), are still covered by the verdict.
+ */
+ if (apply && sent < tosend && !cork) {
+ psock->apply_bytes += tosend - sent;
+ if (eval == __SK_REDIRECT) {
+ psock->eval = eval;
+ psock->sk_redir = sk_redir;
+ eval = __SK_NONE;
+ }
+ }
+ if (eval == __SK_REDIRECT)
+ sock_put(sk_redir);
+ sock_put(sk_redir);
break;
case __SK_DROP:
default:
--
2.43.0
^ permalink raw reply [flat|nested] 11+ messages in thread
* [PATCH bpf v2 09/10] selftests/bpf: Fix errno expected by the wait send mem test of test_sockmap
2026-12-01 12:18 [PATCH bpf v2 00/10] fix misc sockmap issues Jiayuan Chen
` (7 preceding siblings ...)
2026-12-01 12:18 ` [PATCH bpf v2 08/10] bpf, sockmap: Fix apply_bytes when a redirect sends only a part Jiayuan Chen
@ 2026-12-01 12:18 ` Jiayuan Chen
2026-12-01 12:18 ` [PATCH bpf v2 10/10] selftests/bpf: Add test for apply_bytes when a redirect sends a part Jiayuan Chen
9 siblings, 0 replies; 11+ messages in thread
From: Jiayuan Chen @ 2026-12-01 12:18 UTC (permalink / raw)
To: bpf
Cc: Jiayuan Chen, John Fastabend, Jakub Sitnicki, David S. Miller,
Eric Dumazet, Jakub Kicinski, Paolo Abeni, Simon Horman,
Kuniyuki Iwashima, Willem de Bruijn, Neal Cardwell,
Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko,
Eduard Zingerman, Kumar Kartikeya Dwivedi, Martin KaFai Lau,
Song Liu, Yonghong Song, Jiri Olsa, Emil Tsalapatis,
Ihor Solodrai, Shuah Khan, Sechang Lim, Michal Luczaj,
Mattia Meleleo, Zijian Zhang, linux-kernel, netdev,
linux-kselftest
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 <jiayuan.chen@linux.dev>
---
tools/testing/selftests/bpf/test_sockmap.c | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)
diff --git a/tools/testing/selftests/bpf/test_sockmap.c b/tools/testing/selftests/bpf/test_sockmap.c
index aaf2050e88450..771146268df77 100644
--- a/tools/testing/selftests/bpf/test_sockmap.c
+++ b/tools/testing/selftests/bpf/test_sockmap.c
@@ -566,7 +566,8 @@ static int msg_loop(int fd, int iov_count, int iov_length, int cnt,
sent = sendmsg(fd, &msg, flags);
if (!drop && sent < 0) {
- if (opt->tx_wait_mem && errno == EACCES) {
+ if (opt->tx_wait_mem &&
+ (errno == EACCES || errno == EPIPE)) {
errno = 0;
goto out_errno;
}
--
2.43.0
^ permalink raw reply [flat|nested] 11+ messages in thread
* [PATCH bpf v2 10/10] selftests/bpf: Add test for apply_bytes when a redirect sends a part
2026-12-01 12:18 [PATCH bpf v2 00/10] fix misc sockmap issues Jiayuan Chen
` (8 preceding siblings ...)
2026-12-01 12:18 ` [PATCH bpf v2 09/10] selftests/bpf: Fix errno expected by the wait send mem test of test_sockmap Jiayuan Chen
@ 2026-12-01 12:18 ` Jiayuan Chen
9 siblings, 0 replies; 11+ messages in thread
From: Jiayuan Chen @ 2026-12-01 12:18 UTC (permalink / raw)
To: bpf
Cc: Jiayuan Chen, John Fastabend, Jakub Sitnicki, David S. Miller,
Eric Dumazet, Jakub Kicinski, Paolo Abeni, Simon Horman,
Kuniyuki Iwashima, Willem de Bruijn, Neal Cardwell,
Andrii Nakryiko, Eduard Zingerman, Ihor Solodrai,
Alexei Starovoitov, Daniel Borkmann, Kumar Kartikeya Dwivedi,
Martin KaFai Lau, Song Liu, Yonghong Song, Jiri Olsa,
Emil Tsalapatis, Shuah Khan, Sechang Lim, Mattia Meleleo,
Michal Luczaj, Zijian Zhang, linux-kernel, netdev,
linux-kselftest
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 <jiayuan.chen@linux.dev>
---
.../selftests/bpf/prog_tests/sockmap_basic.c | 126 ++++++++++++++++++
.../bpf/progs/test_sockmap_msg_apply.c | 46 +++++++
2 files changed, 172 insertions(+)
create mode 100644 tools/testing/selftests/bpf/progs/test_sockmap_msg_apply.c
diff --git a/tools/testing/selftests/bpf/prog_tests/sockmap_basic.c b/tools/testing/selftests/bpf/prog_tests/sockmap_basic.c
index 1fef6ec2ba7ae..3979cbab7dc76 100644
--- a/tools/testing/selftests/bpf/prog_tests/sockmap_basic.c
+++ b/tools/testing/selftests/bpf/prog_tests/sockmap_basic.c
@@ -14,6 +14,7 @@
#include "test_sockmap_drop_prog.skel.h"
#include "test_sockmap_change_tail.skel.h"
#include "test_sockmap_msg_pop_data.skel.h"
+#include "test_sockmap_msg_apply.skel.h"
#include "bpf_iter_sockmap.skel.h"
#include "sockmap_helpers.h"
@@ -1362,6 +1363,129 @@ static void test_sockmap_no_verdict_fionread(void)
test_sockmap_pass_prog__destroy(skel);
}
+#define APPLY_MSG_MAGIC 0xbeefcafe
+
+/*
+ * One message: a header with its length, then the body. The sk_msg prog
+ * applies the verdict to the whole message and redirects it to the egress
+ * of b. b has the smallest send buffer and its peer the smallest receive
+ * buffer, so the first send() stops in the middle of the message. The rest
+ * must still go to b without another run of the prog.
+ */
+static void test_sockmap_msg_verdict_apply_partial(void)
+{
+ int a = -1, a_peer = -1, b = -1, b_peer = -1, s = -1;
+ struct test_sockmap_msg_apply *skel;
+ int err, map, one = 1, zero = 0;
+ struct sockaddr_storage addr;
+ size_t len, off = 0, got = 0;
+ char *buf = NULL, *rbuf = NULL;
+ int sndbuf, rcvbuf;
+ socklen_t optlen;
+ struct {
+ __u32 magic;
+ __u32 len;
+ } hdr;
+ ssize_t n;
+
+ skel = test_sockmap_msg_apply__open_and_load();
+ if (!ASSERT_OK_PTR(skel, "open_and_load"))
+ return;
+ map = bpf_map__fd(skel->maps.sock_map);
+
+ err = bpf_prog_attach(bpf_program__fd(skel->progs.prog_msg_apply_redir),
+ map, BPF_SK_MSG_VERDICT, 0);
+ if (!ASSERT_OK(err, "bpf_prog_attach"))
+ goto out;
+
+ err = create_pair(AF_INET, SOCK_STREAM, &a_peer, &a);
+ if (!ASSERT_OK(err, "create_pair"))
+ goto out;
+
+ /* The peer of b gets its receive buffer before the handshake */
+ s = socket_loopback(AF_INET, SOCK_STREAM);
+ if (!ASSERT_GE(s, 0, "socket_loopback"))
+ goto out_close;
+ if (xsetsockopt(s, SOL_SOCKET, SO_RCVBUF, &one, sizeof(one)))
+ goto out_close;
+ b = xsocket(AF_INET, SOCK_STREAM, 0);
+ if (b < 0)
+ goto out_close;
+ if (xsetsockopt(b, SOL_SOCKET, SO_SNDBUF, &one, sizeof(one)))
+ goto out_close;
+ optlen = sizeof(addr);
+ if (xgetsockname(s, sockaddr(&addr), &optlen))
+ goto out_close;
+ if (xconnect(b, sockaddr(&addr), optlen))
+ goto out_close;
+ b_peer = xaccept_nonblock(s, NULL, NULL);
+ if (b_peer < 0)
+ goto out_close;
+
+ optlen = sizeof(sndbuf);
+ if (xgetsockopt(b, SOL_SOCKET, SO_SNDBUF, &sndbuf, &optlen))
+ goto out_close;
+ optlen = sizeof(rcvbuf);
+ if (xgetsockopt(b_peer, SOL_SOCKET, SO_RCVBUF, &rcvbuf, &optlen))
+ goto out_close;
+ /* Larger than what b and its peer can hold */
+ len = 4 * (sndbuf + rcvbuf);
+
+ err = bpf_map_update_elem(map, &zero, &b, BPF_NOEXIST);
+ if (!ASSERT_OK(err, "bpf_map_update_elem(b)"))
+ goto out_close;
+ err = bpf_map_update_elem(map, &one, &a, BPF_NOEXIST);
+ if (!ASSERT_OK(err, "bpf_map_update_elem(a)"))
+ goto out_close;
+
+ buf = malloc(len);
+ rbuf = calloc(len, 1);
+ if (!ASSERT_OK_PTR(buf, "malloc") || !ASSERT_OK_PTR(rbuf, "calloc"))
+ goto out_close;
+ memset(buf, 0xaa, len);
+ hdr.magic = APPLY_MSG_MAGIC;
+ hdr.len = len;
+ memcpy(buf, &hdr, sizeof(hdr));
+
+ while (got < len) {
+ if (off < len) {
+ n = send(a, buf + off, len - off, MSG_DONTWAIT);
+ if (!ASSERT_TRUE(n >= 0 || errno == EAGAIN, "send"))
+ break;
+ if (n > 0) {
+ if (!off && !ASSERT_LT(n, len, "first send partial"))
+ break;
+ off += n;
+ } else {
+ usleep(1000);
+ }
+ }
+ while (got < off) {
+ n = recv_timeout(b_peer, rbuf + got, off - got, 0,
+ IO_TIMEOUT_SEC);
+ if (!ASSERT_GT(n, 0, "recv_timeout"))
+ goto out_close;
+ got += n;
+ }
+ }
+
+ ASSERT_EQ(got, len, "received");
+ ASSERT_OK(memcmp(buf, rbuf, len), "data");
+ ASSERT_EQ(skel->bss->good_hdrs, 1, "prog runs on the header");
+ ASSERT_EQ(skel->bss->bad_hdrs, 0, "prog runs in the body");
+
+out_close:
+ free(buf);
+ free(rbuf);
+ close(s);
+ close(a);
+ close(a_peer);
+ close(b);
+ close(b_peer);
+out:
+ test_sockmap_msg_apply__destroy(skel);
+}
+
void test_sockmap_basic(void)
{
if (test__start_subtest("sockmap create_update_free"))
@@ -1438,4 +1562,6 @@ void test_sockmap_basic(void)
test_sockmap_multi_channels(SOCK_STREAM);
if (test__start_subtest("sockmap udp multi channels"))
test_sockmap_multi_channels(SOCK_DGRAM);
+ if (test__start_subtest("sockmap msg_verdict apply_bytes partial redirect"))
+ test_sockmap_msg_verdict_apply_partial();
}
diff --git a/tools/testing/selftests/bpf/progs/test_sockmap_msg_apply.c b/tools/testing/selftests/bpf/progs/test_sockmap_msg_apply.c
new file mode 100644
index 0000000000000..6e24117933d03
--- /dev/null
+++ b/tools/testing/selftests/bpf/progs/test_sockmap_msg_apply.c
@@ -0,0 +1,46 @@
+// SPDX-License-Identifier: GPL-2.0
+#include "vmlinux.h"
+#include <bpf/bpf_helpers.h>
+
+struct {
+ __uint(type, BPF_MAP_TYPE_SOCKMAP);
+ __uint(max_entries, 2);
+ __type(key, int);
+ __type(value, int);
+} sock_map SEC(".maps");
+
+#define MSG_MAGIC 0xbeefcafe
+
+struct msg_hdr {
+ __u32 magic;
+ __u32 len;
+};
+
+int good_hdrs;
+int bad_hdrs;
+
+/*
+ * Each message starts with a header. Apply the verdict to the whole
+ * message, so the prog runs again at the next header.
+ */
+SEC("sk_msg")
+int prog_msg_apply_redir(struct sk_msg_md *msg)
+{
+ struct msg_hdr *hdr;
+
+ if (bpf_msg_pull_data(msg, 0, sizeof(*hdr), 0))
+ goto bad;
+
+ hdr = msg->data;
+ if ((void *)(hdr + 1) > msg->data_end || hdr->magic != MSG_MAGIC)
+ goto bad;
+
+ good_hdrs++;
+ bpf_msg_apply_bytes(msg, hdr->len);
+ return bpf_msg_redirect_map(msg, &sock_map, 0, 0);
+bad:
+ bad_hdrs++;
+ return SK_DROP;
+}
+
+char _license[] SEC("license") = "GPL";
--
2.43.0
^ permalink raw reply [flat|nested] 11+ messages in thread
end of thread, other threads:[~2026-12-01 12:25 UTC | newest]
Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-12-01 12:18 [PATCH bpf v2 00/10] fix misc sockmap issues Jiayuan Chen
2026-12-01 12:18 ` [PATCH bpf v2 01/10] bpf, sockmap: Fix copied_seq updates with strparser Jiayuan Chen
2026-12-01 12:18 ` [PATCH bpf v2 02/10] selftests/bpf: Add test for unread SK_PASS data " Jiayuan Chen
2026-12-01 12:18 ` [PATCH bpf v2 03/10] bpf, sockmap: Fix empty sk_msg queued by ingress redirect Jiayuan Chen
2026-12-01 12:18 ` [PATCH bpf v2 04/10] bpf, sockmap: Fix concurrent senders on one psock Jiayuan Chen
2026-12-01 12:18 ` [PATCH bpf v2 05/10] bpf, sockmap: Fix reconnect with a psock by rejecting connect Jiayuan Chen
2026-12-01 12:18 ` [PATCH bpf v2 06/10] bpf, sockmap: Fix psock refcnt taken in sk_psock_backlog() Jiayuan Chen
2026-12-01 12:18 ` [PATCH bpf v2 07/10] bpf, sockmap: Fix socket lock taken on a child before accept() Jiayuan Chen
2026-12-01 12:18 ` [PATCH bpf v2 08/10] bpf, sockmap: Fix apply_bytes when a redirect sends only a part Jiayuan Chen
2026-12-01 12:18 ` [PATCH bpf v2 09/10] selftests/bpf: Fix errno expected by the wait send mem test of test_sockmap Jiayuan Chen
2026-12-01 12:18 ` [PATCH bpf v2 10/10] selftests/bpf: Add test for apply_bytes when a redirect sends a part Jiayuan Chen
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®