Skip to contentSkip to conversation

tcp: opportunistic loopback splice for BPF-paired sockets

RFC PATCH In review Cong Wang 7 participants 18 messages Jun 12 – Jun 13

Subsystems (8)
lore
Share & tools
lore

Cong Wang

author
#0 lore raw

cover tcp: opportunistic loopback splice for BPF-paired sockets

This series adds an opportunistic "loopback splice" fast path for two locally-connected TCP sockets that a sock_ops BPF program pairs at handshake completion. Once paired, sendmsg copies the user payload into a per-direction in-kernel byte ring and recvmsg drains it on the other side; both copies happen in their own task's mm, so the fast path incurs no skb construction, no softirq, and no TCP protocol-state processing.

The underlying TCP connection stays fully real: sequence numbers are frozen at post-handshake values, so FIN/RST/keepalive keep flowing through the normal paths and the pair tears down via a regular close. Pairing is opt-in per flow and fallback is per-message - handshake-style traffic takes the TCP path, the bulk phase takes the ring, on the same socket. Nothing leaves the host and applications need no changes: no new address family, no LD_PRELOAD, no source modification.

The target use cases are co-located endpoints that speak plain TCP:
- regular TCP loopback (127.0.0.1) between processes on the same host;
- container sidecar deployments - e.g. a service-mesh sidecar proxy and its application in the same pod, talking over loopback or a veth pair - where the per-skb veth+bridge cost is exactly what the ring sidesteps.

Highlights (TCP_RR, 1 KB request/response, netperf, pinned CPUs, baseline TCP vs splice; full tables across message sizes and TCP_STREAM in patches 1 and 2):

loopback (127.0.0.1):

    without busy-poll:   105.8k -> 235.1k tps  (2.2x)
    with busy-poll 50us: 106.1k -> 713.0k tps  (6.7x)

container (netns + veth + bridge):

    without busy-poll:    99.9k -> 233.9k tps  (2.3x)
    with busy-poll 50us: 100.4k -> 704.9k tps  (7.0x)

Synchronous-RPC (TCP_RR) at a 1 KB message wins ~2.2x without busy polling and ~6.7x with it (the win grows toward smaller messages and narrows toward 64 KB), because the ring removes the per-cycle kernel TCP receive-path cost and the receiver can spin on the ring directly - loopback delivers via the per-CPU backlog and exposes no pollable napi_id, so the generic sk_busy_loop() is a no-op there. Bulk streaming is roughly neutral on bare-metal loopback but wins decisively (up to ~6x) container-to-container, where per-skb veth+bridge cost dominates the path the ring sidesteps.

---
Cong Wang (5):
tcp_bpf: add bpf_sock_splice_pair kfunc for opportunistic loopback
splice
tcp_bpf: busy-poll the splice ring before parking the receiver selftests/bpf: add tcp_splice basic round-trip test
bpf: allow SO_BUSY_POLL in bpf_setsockopt() selftests/bpf: set SO_BUSY_POLL from the tcp_splice sockops prog

 include/linux/skmsg.h                         |   9 +
 include/net/tcp.h                             |   8 +
 net/core/filter.c                             |   1 +
 net/core/skmsg.c                              |   3 +
 net/ipv4/tcp_bpf.c                            | 847 +++++++++++++++++-
 .../selftests/bpf/prog_tests/tcp_splice.c     | 206 +++++
 .../selftests/bpf/progs/test_tcp_splice.c     | 125 +++
 7 files changed, 1198 insertions(+), 1 deletion(-)
 create mode 100644 tools/testing/selftests/bpf/prog_tests/tcp_splice.c
 create mode 100644 tools/testing/selftests/bpf/progs/test_tcp_splice.c

base-commit: 30dee2c176e7954f63d1fa3e52d172f30beb9bfb

Signature · 1 line
2.43.0
author
#1 lore raw

RFC PATCH 1/5 tcp_bpf: add bpf_sock_splice_pair kfunc for opportunistic loopback splice

Two locally-connected TCP sockets can be paired by a sock_ops BPF program at handshake completion. Once paired, sendmsg copies the user payload into a per-direction kernel-side byte ring; recvmsg drains the ring into the user buffer. Both copies happen in their own task's mm, so no cross-mm pin / kmap dance is needed and the splice fast path incurs no skb construction, no softirq, and no TCP protocol-state processing. The TCP wire connection itself never sees the spliced
bytes: sequence numbers stay frozen at post-handshake values, so FIN, RST, and keepalive continue to work through the regular paths and the pair tears down via a normal close handshake.

Per-direction ring layout
-------------------------

The ring is a power-of-two byte buffer (16 KiB by default) backed by order-2 pages from __get_free_pages(), manipulated through include/linux/circ_buf.h macros. Both per-direction rings are allocated when the pair is formed, so the data path never allocates. Producer and consumer are SPSC (one socket each side), so head and tail are updated with smp_store_release() / smp_load_acquire() without a data-path lock. Each side keeps a private cache of the other's cursor (the producer caches ring_tail) and reads the real, cross-CPU cursor only when the cache is exhausted - standard SPSC cursor caching. sendmsg copies from the user iov into the ring at head; recvmsg copies out at tail.

The ring is the queue between sender and receiver - it accumulates across recvmsg calls, so a sequence of small sends amortises into one wake when the receiver isn't draining synchronously. This is what makes the splice path viable for streaming workloads without forcing per-message rendezvous. The cost is one extra in-kernel copy compared to a sender->user-pages direct mechanism, but the benefit is that the splice path can stay engaged across arbitrary phasing between sender and receiver - no app cooperation required.

The sender keeps the peer ring alive across the copy with a per-pair percpu_ref rather than a per-message socket refcount, and validates only its own socket's error/shutdown state, as the rest of tcp_bpf does
- a peer reset reaches it over the still-live TCP connection. Both keep the per-message cost off cross-CPU cachelines.

Sender (splice_send_ring) defers to tcp_sendmsg() when (a) the peer rcv_queue is non-empty (preserving stream ordering against prior TCP fallback) or (b) the ring is full (TCP-level backpressure via sndbuf / snd_wnd absorbs the overflow). Receiver (tcp_bpf_splice_recvmsg) defers to tcp_recvmsg() when the rcv_queue holds data and the ring is empty. The end-to-end ordering invariant is: rcv_queue bytes are always older than any ring bytes drained alongside them, because the sender only writes to the ring while the peer rcv_queue is empty.

For bytes that take the splice fast path, SO_SNDBUF and SO_RCVBUF are not honored - the sndbuf / rcvbuf accounting machinery is exactly what splice intentionally bypasses. The associated infrastructure - sk_mem_charge / sk_mem_uncharge, sk_forward_alloc, prot->memory_allocated, tcp_memory_pressure, the per-cpu reserves - is among the most painful parts of TCP to maintain, and spliced bytes opt out of it as a side effect of having no skb-borne kernel-side bytes to account for. The ring's own capacity is bounded (SPLICE_RING_SIZE), giving a hard upper bound on per-pair memory. SIOCINQ / SIOCOUTQ reflect only the underlying TCP socket's frozen counters, and getsockopt(TCP_INFO) likewise. Bytes that take the TCP fallback go through the regular TCP path with all of its normal accounting.

Pairing is opt-in per flow - the BPF program at handshake decides which connections to splice. Applications that mix handshake-style traffic and bulk streaming on the same paired socket get the right behaviour on both phases automatically: the handshake survives via TCP fallback, the bulk phase runs through the ring.

The receiver parks on the socket waitqueue when the ring is empty. A following patch adds an optional bounded busy-poll of the ring before parking, gated on the socket's SO_BUSY_POLL budget; it is off by default and is what turns the latency-bound TCP_RR case into a large win once enabled. The numbers below are with busy polling disabled.

Microbenchmarks
---------------

Pinned to two adjacent CPUs (sender CPU 1, receiver CPU 0), 10s per run, 3 runs averaged; netperf to 127.0.0.1. Splice without busy polling.

Bare-metal loopback:

  TCP_STREAM  msg=   64 B:   2577 ->  1680  Mbps  (0.65x)
              msg=  256 B:   9336 ->  8640  Mbps  (0.93x)
              msg=    1 KB: 22416 -> 24136  Mbps  (1.08x)
              msg=    4 KB: 37893 -> 52304  Mbps  (1.38x)
              msg=   16 KB: 48019 -> 53235  Mbps  (1.11x)
              msg=   64 KB: 49686 -> 49418  Mbps  (0.99x)
  TCP_RR      sz=     1 B:  110.2k -> 267.0k tps  (2.42x)
              sz=    64 B:  111.6k -> 265.7k tps  (2.38x)
              sz=     1 KB: 105.8k -> 235.1k tps  (2.22x)
              sz=    16 KB:  40.5k ->  89.6k tps  (2.21x)
              sz=    64 KB:  17.8k ->  20.9k tps  (1.17x)

Container-to-container (two network namespaces connected via veth pair plus Linux bridge, processes pinned in the same way):

  TCP_STREAM  msg=   64 B:   1420 ->   1643 Mbps  (1.16x)
              msg=    1 KB:  3710 -> 21326 Mbps  (5.75x)
              msg=    4 KB:  8084 -> 48834 Mbps  (6.04x)
              msg=   16 KB: 26083 -> 27788 Mbps  (1.07x)
              msg=   64 KB: 47659 -> 47507 Mbps  (1.00x)
  TCP_RR      sz=     1 B:  105.0k -> 265.0k tps  (2.52x)
              sz=    64 B:  101.2k -> 264.3k tps  (2.61x)
              sz=     1 KB:  99.9k -> 233.9k tps  (2.34x)
              sz=    16 KB:  44.8k ->  91.1k tps  (2.03x)
              sz=    64 KB:  18.1k ->  23.5k tps  (1.30x)

Synchronous-RPC workloads (TCP_RR) win 2.0-2.6x across both environments because the ring eliminates the per-cycle overhead of the kernel TCP receive path. Mid-message streaming wins on loopback (4-16 KB at 1.1-1.4x). Tiny-message streaming on bare-metal loopback regresses to 0.65x because loopback TCP's TSO super-segments amortise per-batch cost to ~20 ns/msg, below the ring's 2-copy per-cycle floor; this is structural. In containers the same workload wins decisively because per-packet veth+bridge overhead dwarfs the ring's floor:
STREAM-1 KB and STREAM-4 KB go 5.75x and 6.04x because TCP's per-skb cost dominates the container path and the ring sidesteps it entirely.

Assisted-by Claude:claude-opus-4.8
---
 include/linux/skmsg.h |   9 +
 include/net/tcp.h     |   8 +
 net/core/skmsg.c      |   3 +
 net/ipv4/tcp_bpf.c    | 809 +++++++++++++++++++++++++++++++++++++++++-
 4 files changed, 828 insertions(+), 1 deletion(-)
include/linux/skmsg.h +9 -0
index 19f4f253b4f9..c9b7144cc846 100644
@@ -80,6 +80,8 @@ struct sk_psock_work_state {
 	u32				off;
 };
 
+struct sk_psock_splice;	/* defined in net/ipv4/tcp_bpf.c */
+
 struct sk_psock {
 	struct sock			*sk;
 	struct sock			*sk_redir;
@@ -121,6 +123,13 @@ struct sk_psock {
 	struct delayed_work		work;
 	struct sock			*sk_pair;
 	struct rcu_work			rwork;
+
+	/* Loopback splice state for paired stream sockets. NULL until the
+	 * first bpf_sock_splice_pair() call on this psock; lazily allocated
+	 * and kept for the lifetime of the psock so that sender/receiver
+	 * paths don't need to revalidate the pointer mid-flight.
+	 */
+	struct sk_psock_splice __rcu	*splice;
 };
 
 int sk_msg_alloc(struct sock *sk, struct sk_msg *msg, int len,
include/net/tcp.h +8 -0
index 98848db62894..c1597accdac9 100644
@@ -2855,6 +2855,8 @@ struct sk_psock;
 #ifdef CONFIG_BPF_SYSCALL
 int tcp_bpf_update_proto(struct sock *sk, struct sk_psock *psock, bool restore);
 void tcp_bpf_clone(const struct sock *sk, struct sock *newsk);
+void tcp_bpf_splice_unpair(struct sk_psock *psock);
+void tcp_bpf_splice_destroy(struct sk_psock *psock);
 #ifdef CONFIG_BPF_STREAM_PARSER
 struct strparser;
 int tcp_bpf_strp_read_sock(struct strparser *strp, read_descriptor_t *desc,
@@ -2880,6 +2882,12 @@ static inline void tcp_bpf_clone(const struct sock *sk, struct sock *newsk)
 }
 #endif
 
+#if !defined(CONFIG_BPF_SYSCALL)
+struct sk_psock;
+static inline void tcp_bpf_splice_unpair(struct sk_psock *psock) {}
+static inline void tcp_bpf_splice_destroy(struct sk_psock *psock) {}
+#endif
+
 #ifdef CONFIG_CGROUP_BPF
 static inline void bpf_skops_init_skb(struct bpf_sock_ops_kern *skops,
 				      struct sk_buff *skb,
net/core/skmsg.c +3 -0
index e1850caf1a71..b39fc249a18d 100644
@@ -881,12 +881,15 @@ static void sk_psock_destroy(struct work_struct *work)
 		sock_put(psock->sk_redir);
 	if (psock->sk_pair)
 		sock_put(psock->sk_pair);
+	tcp_bpf_splice_destroy(psock);
 	sock_put(psock->sk);
 	kfree(psock);
 }
 
 void sk_psock_drop(struct sock *sk, struct sk_psock *psock)
 {
+	tcp_bpf_splice_unpair(psock);
+
 	write_lock_bh(&sk->sk_callback_lock);
 	sk_psock_restore_proto(sk, psock);
 	rcu_assign_sk_user_data(sk, NULL);
net/ipv4/tcp_bpf.c +808 -1

840 lines not on this page · raw message

Signature · 1 line
2.43.0
#2 lore raw

Re: [RFC PATCH bpf-next 1/5] tcp_bpf: add bpf_sock_splice_pair kfunc for opportunistic loopback splice

Thank you for your contribution! Sashiko AI review found 10 potential issue(s) to consider: - [High] Missing socket lock in spliced sendmsg/recvmsg leading to SPSC ring buffer corruption. - [High]…

Thank you for your contribution! Sashiko AI review found 10 potential issue(s) to consider:
- [High] Missing socket lock in spliced sendmsg/recvmsg leading to SPSC ring buffer corruption.
- [High] Permanent memory leak of sockets and psocks due to circular references.
- [High] TCP stream ordering violation due to fast path racing with in-flight TCP data.
- [High] Use-after-free of socket waitqueue due to missing RCU read lock in splice_wake.
- [High] RCU API Violations and lockless data races when accessing psock->splice.
- [High] Silent data blackhole when the receiving spliced socket has an ingress BPF program.
- [High] Refcount leak and Use-After-Free due to ignoring the return value of sk_psock_get().
- [Medium] Missing __GFP_ACCOUNT on ring buffer allocations allows memcg limits bypass.
- [Medium] MSG_PEEK semantics broken for spliced sockets, causing hangs or silent data omission.
- [Medium] Socket errors are not cleared when fetched, leading to permanent error state. --

commit 2f2595b5999c95fe8a664fc82201339a3716f692
Author: Cong Wang <[email protected]>

tcp_bpf: add bpf_sock_splice_pair kfunc for opportunistic loopback splice

Two locally-connected TCP sockets can be paired by a sock_ops BPF program at handshake completion. Once paired, sendmsg copies the user payload into a per-direction kernel-side byte ring; recvmsg drains the ring into the user buffer. Both copies happen in their own task's mm, so no cross-mm pin / kmap dance is needed and the splice fast path incurs no skb construction, no softirq, and no TCP protocol-state processing. The TCP wire connection itself never sees the spliced
bytes: sequence numbers stay frozen at post-handshake values, so FIN, RST, and keepalive continue to work through the regular paths and the pair tears down via a normal close handshake.

Quoted 4 lines
net/core/skmsg.c +0 -0
index e1850caf1a71a..b39fc249a18d5 100644

[ ... ]

net/core/skmsg.c static void sk_psock_destroy(struct work_struct *work) · line 892

Quoted 13 lines
net/core/skmsg.c +3 -0
@@ -881,12 +881,15 @@ static void sk_psock_destroy(struct work_struct *work)
 		sock_put(psock->sk_redir);
 	if (psock->sk_pair)
 		sock_put(psock->sk_pair);
+	tcp_bpf_splice_destroy(psock);
 	sock_put(psock->sk);
 	kfree(psock);
 }
 
 void sk_psock_drop(struct sock *sk, struct sk_psock *psock)
 {
+	tcp_bpf_splice_unpair(psock);
+

[Severity: High]
Does this code create a permanent memory leak due to a circular reference?

In bpf_sock_splice_pair(), both sides of the paired connection acquire a reference to the other's sk_psock via sk_psock_get(). This establishes a circular reference.

Since the cross-references are only dropped in tcp_bpf_splice_unpair(), and this is only called from sk_psock_drop() which executes when a psock's reference count reaches 0, the circular reference guarantees neither refcount will ever reach 0.

Quoted 4 lines
net/ipv4/tcp_bpf.c +0 -0
index cc0bd73f36b6d..549f370772446 100644

[ ... ]

net/ipv4/tcp_bpf.c static void tcp_bpf_rebuild_protos(struct proto prot[TCP_BPF_NUM_CFGS], · line 689

Quoted 9 lines
net/ipv4/tcp_bpf.c +2 -1
@@ -626,8 +683,9 @@ static void tcp_bpf_rebuild_protos(struct proto prot[TCP_BPF_NUM_CFGS],
 	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].sendmsg		= tcp_bpf_splice_sendmsg;
 	prot[TCP_BPF_BASE].recvmsg		= tcp_bpf_recvmsg;
-	prot[TCP_BPF_BASE].sock_is_readable	= sk_msg_is_readable;
+	prot[TCP_BPF_BASE].sock_is_readable	= tcp_bpf_is_readable;
 	prot[TCP_BPF_BASE].ioctl		= tcp_bpf_ioctl;

[Severity: High]
Does this cause a silent data blackhole when the receiving spliced socket has an ingress BPF program?

The TCP_BPF_RX protocol configuration inherits its base configuration here, so its sendmsg is assigned tcp_bpf_splice_sendmsg. However, its recvmsg is explicitly overridden to tcp_bpf_recvmsg_parser, which lacks the splice ring-buffer logic.

A sender will correctly push data into the splice ring buffer, but the receiver will only poll the standard sk_receive_queue and ingress_msg queue, never reading the ring buffer.

[ ... ]

net/ipv4/tcp_bpf.c void tcp_bpf_clone(const struct sock *sk, struct sock *newsk) · line 858

Quoted 8 lines
net/ipv4/tcp_bpf.c +8 -0
+static inline void splice_wake(struct sock *sk)
+{
+	wait_queue_head_t *wq = sk_sleep(sk);
+
+	smp_mb();
+	if (wq && waitqueue_active(wq))
+		wake_up_interruptible_all(wq);
+}

[Severity: High]
Is it possible for sk_sleep(sk) to access a freed waitqueue here?

This function uses sk_sleep(sk) to get the socket's waitqueue, which uses rcu_dereference_raw(). Without the caller holding the RCU read lock, the memory backing the waitqueue could be concurrently freed by sock_release().

[ ... ]

net/ipv4/tcp_bpf.c void tcp_bpf_clone(const struct sock *sk, struct sock *newsk) · line 885

Quoted 6 lines
net/ipv4/tcp_bpf.c +6 -0
+static bool sk_psock_is_spliced(const struct sk_psock *psock)
+{
+	struct sk_psock_splice *s = rcu_dereference(psock->splice);
+
+	return s && rcu_access_pointer(s->peer);
+}

[Severity: High]
Is this missing an RCU read lock?

sk_psock_is_spliced() calls rcu_dereference(psock->splice) from tcp_bpf_splice_sendmsg() and tcp_bpf_recvmsg() without an active rcu_read_lock(), which can trigger a lockdep warning.

[ ... ]

net/ipv4/tcp_bpf.c void tcp_bpf_clone(const struct sock *sk, struct sock *newsk) · line 892

Quoted 6 lines
net/ipv4/tcp_bpf.c +6 -0
+static int tcp_bpf_splice_sendmsg(struct sock *sk, struct msghdr *msg,
+				  size_t size)
+{
+	struct sk_psock *psock;
+	int spliced = 0;
+	int ret;

[Severity: High]
Could the lack of socket locking here lead to SPSC ring buffer corruption?

tcp_bpf_splice_sendmsg() bypasses the standard socket lock (lock_sock(sk)). Since userspace can call sendmsg or recvmsg from multiple threads concurrently on the same socket descriptor, multiple producers or consumers will race when accessing the SPSC ring buffer logic in splice_ring_write() and tcp_bpf_splice_recvmsg(). This can lead to non-atomic cursor updates and memory corruption.

[ ... ]

Quoted 7 lines
net/ipv4/tcp_bpf.c +7 -0
+void tcp_bpf_splice_unpair(struct sk_psock *psock)
+{
+	struct sk_psock_splice *self_s, *peer_s;
+	struct sk_psock *peer;
+	bool was_paired = false;
+
+	self_s = rcu_dereference_protected(psock->splice, 1);

net/ipv4/tcp_bpf.c void tcp_bpf_clone(const struct sock *sk, struct sock *newsk) · line 1007

Quoted 10 lines
net/ipv4/tcp_bpf.c +10 -0
+	if (!self_s)
+		return;
+
+	rcu_read_lock();
+	peer = rcu_dereference(self_s->peer);
+	if (!peer) {
+		rcu_read_unlock();
+		return;
+	}
+	if (!sk_psock_get(peer->sk)) {

[Severity: High]
Does ignoring the return value of sk_psock_get() here cause a refcount leak and use-after-free?

If a socket's psock is concurrently replaced, sk_psock_get() returns and increments the refcount of the new psock. Because this code continues using the original peer pointer to interact with and ultimately drop the refcount (via sk_psock_put()), it creates an unbalanced increment on the new psock (memory leak) and an unbalanced decrement on the old psock (use-after-free).

[ ... ]

Quoted 1 line
net/ipv4/tcp_bpf.c +1 -0
+	rcu_read_unlock();

[ ... ]

net/ipv4/tcp_bpf.c void tcp_bpf_clone(const struct sock *sk, struct sock *newsk) · line 1031

Quoted 5 lines
net/ipv4/tcp_bpf.c +5 -0
+	/* Wake any blocked rendezvous waiters on either side. They will
+	 * re-check the predicate, see splice->peer == NULL, and exit.
+	 */
+	splice_wake(psock->sk);
+	splice_wake(peer->sk);

[Severity: High]
Is it safe to call splice_wake() here after dropping the RCU read lock?

splice_wake() requires the RCU read lock to safely access the socket's waitqueue without it being concurrently freed.

[ ... ]

net/ipv4/tcp_bpf.c void tcp_bpf_clone(const struct sock *sk, struct sock *newsk) · line 1096

Quoted 9 lines
net/ipv4/tcp_bpf.c +9 -0
+static int splice_ring_alloc(struct sk_psock_splice *s)
+{
+	void *buf;
+
+	if (READ_ONCE(s->ring_buf))
+		return 0;
+
+	buf = (void *)__get_free_pages(GFP_ATOMIC | __GFP_NOWARN,
+				       get_order(SPLICE_RING_SIZE));

[Severity: Medium]
Should this allocation use the __GFP_ACCOUNT flag?

Because this 16 KiB allocation is not charged to the memory cgroup, a local application could create and pair an arbitrary number of sockets to intentionally exhaust host memory without being restricted by its container's memory limits.

[ ... ]

Quoted 7 lines
net/ipv4/tcp_bpf.c +7 -0
+static int tcp_bpf_splice_recvmsg(struct sock *sk,
+				  struct sk_psock *psock,
+				  struct msghdr *msg, size_t len,
+				  int flags, int *err)
+{
+	struct sk_psock_splice *s;
+	size_t copied;

net/ipv4/tcp_bpf.c void tcp_bpf_clone(const struct sock *sk, struct sock *newsk) · line 1314

Quoted 10 lines
net/ipv4/tcp_bpf.c +10 -0
+	long timeo;
+
+	*err = 0;
+	/* PEEK is not implemented against the ring (no peek-without-advance
+	 * helper). Return 0 with no error so the dispatcher defers to the
+	 * TCP path; ring contents are invisible to PEEK but the socket
+	 * continues to work for normal apps.
+	 */
+	if (flags & MSG_PEEK)
+		return 0;

[Severity: Medium]
Does returning 0 here break MSG_PEEK semantics?

Since the data has already been spliced directly into the ring buffer, it is completely invisible to the TCP stack. A MSG_PEEK call falling back to the standard TCP sk_receive_queue will falsely report that no data is available, and a blocking MSG_PEEK could hang indefinitely waiting for normal TCP data.

[ ... ]

net/ipv4/tcp_bpf.c

Quoted 1 line
net/ipv4/tcp_bpf.c +1 -0
+	s = rcu_dereference_protected(psock->splice, 1);

[Severity: High]
Can using a hardcoded 1 here cause data races?

Using rcu_dereference_protected() with a hardcoded 1 in a lockless, concurrently accessible path suppresses lockdep checks and omits the data dependency barriers provided by rcu_dereference() or READ_ONCE(). In a concurrent environment, this creates data races and risks observing uninitialized ring buffer state on architectures that require strict data dependency barriers.

[ ... ]

net/ipv4/tcp_bpf.c void tcp_bpf_clone(const struct sock *sk, struct sock *newsk) · line 1338

Quoted 4 lines
net/ipv4/tcp_bpf.c +4 -0
+		if (sk->sk_err) {
+			*err = -sk->sk_err;
+			return 0;
+		}

[Severity: Medium]
Should the socket error be cleared when it is fetched?

When this observes an error in sk->sk_err, it sets *err and returns, which propagates the error to user space. However, sk->sk_err is never cleared (e.g., via sock_error(sk) or xchg(&sk->sk_err, 0)). Failing to clear the error means every subsequent recvmsg() call will erroneously fail with the exact same error, permanently breaking the socket.

[ ... ]

Quoted 3 lines
net/ipv4/tcp_bpf.c +3 -0
+static int splice_send_ring(struct sock *sk, struct sk_psock *psock,
+			    struct msghdr *msg, size_t size, int flags)
+{

[ ... ]

net/ipv4/tcp_bpf.c void tcp_bpf_clone(const struct sock *sk, struct sock *newsk) · line 1388

Quoted 3 lines
net/ipv4/tcp_bpf.c +3 -0
+			if (ps && READ_ONCE(ps->ring_buf) &&
+			    !sk->sk_err && !(sk->sk_shutdown & SEND_SHUTDOWN) &&
+			    skb_queue_empty(&peer_sk->sk_receive_queue)) {

[Severity: High]
Does this check violate TCP stream ordering by ignoring in-flight TCP data?

To preserve stream ordering when falling back to the standard TCP stack, the sender relies on skb_queue_empty(&peer_sk->sk_receive_queue) to determine if it is safe to write to the ring buffer. However, this check ignores data that is actively in-flight (queued in the sender's sk_write_queue, the local qdisc, or softirq backlog). A subsequent sendmsg will see an empty peer_sk->sk_receive_queue and splice new data directly into the ring buffer, allowing the receiver to read it before the older in-flight TCP data arrives.

[ ... ]

Quoted 3 lines
net/ipv4/tcp_bpf.c +3 -0
+__bpf_kfunc int bpf_sock_splice_pair(struct sock *peer,
+				     struct bpf_sock_ops_kern *skops)
+{

[ ... ]

net/ipv4/tcp_bpf.c void tcp_bpf_clone(const struct sock *sk, struct sock *newsk) · line 1524

Quoted 6 lines
net/ipv4/tcp_bpf.c +6 -0
+	/* Each side keeps a psock ref on the other for the duration. */
+	if (!sk_psock_get(sk)) {
+		ret = -ENOENT;
+		goto out_unlock;
+	}
+	if (!sk_psock_get(peer)) {

[Severity: High]
Could ignoring the return value of sk_psock_get() here cause a refcount leak and use-after-free?

Similar to tcp_bpf_splice_unpair(), if a socket's psock is concurrently replaced, sk_psock_get() returns and increments the refcount of the new psock. Because this code continues using the original peer pointer to interact with it, it creates an unbalanced increment on the new psock and an unbalanced decrement on the old psock.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#3 lore raw

Re: [RFC PATCH bpf-next 1/5] tcp_bpf: add bpf_sock_splice_pair kfunc for opportunistic loopback splice

In reply to Cong Wang
Is the flags parameter needed? The function signature includes an int flags parameter, but the function body never references it. The only flag checked is msg->msg_flags for MSG_OOB.
Quoted 4 lines
net/ipv4/tcp_bpf.c +0 -0
index cc0bd73f36b6..549f37077244 100644

[ ... ]

Quoted 63 lines
net/ipv4/tcp_bpf.c +59 -0
@@ -756,4 +814,753 @@ void tcp_bpf_clone(const struct sock *sk, struct sock *newsk)
 		newsk->sk_prot = sk->sk_prot_creator;
 }
 
+static int splice_send_ring(struct sock *sk, struct sk_psock *psock,
+			    struct msghdr *msg, size_t size, int flags)
+{
+	struct sk_psock_splice *self_s, *peer_s;
+	struct sk_psock *peer;
+	int total = 0;
+
+	if (msg->msg_flags & MSG_OOB)
+		return 0;
+
+	self_s = rcu_dereference_protected(psock->splice, 1);
+	if (!self_s)
+		return 0;
+
+	while (size > 0) {
+		size_t done, space = 0;
+
+		/* All peer / peer->sk accesses happen under RCU. If the ring
+		 * has space, grab the peer's ring_ref before dropping RCU: that
+		 * pins peer_s (and its ring) so the copy below can run outside
+		 * RCU and fault/sleep normally. peer_sk is *not* pinned by the
+		 * ref, so it must not be touched after rcu_read_unlock().
+		 */
+		peer_s = NULL;
+		rcu_read_lock();
+		peer = rcu_dereference(self_s->peer);
+		if (peer) {
+			struct sock *peer_sk = peer->sk;
+			struct sk_psock_splice *ps = rcu_dereference(peer->splice);
+
+			if (ps && READ_ONCE(ps->ring_buf) &&
+			    !sk->sk_err && !(sk->sk_shutdown & SEND_SHUTDOWN) &&
+			    skb_queue_empty(&peer_sk->sk_receive_queue)) {
+				space = splice_ring_space(ps);
+				if (space && percpu_ref_tryget_live(&ps->ring_ref))
+					peer_s = ps;
+			}
+		}
+		rcu_read_unlock();
+		if (!peer_s)
+			break;
+
+		/* Holding peer_s->ring_ref: peer_s and its ring stay alive.
+		 * The copy touches only the ring, never peer_sk, so a normal
+		 * faulting copy is safe here.
+		 */
+		done = splice_ring_write(peer_s, &msg->msg_iter,
+					 min(size, space));
+		percpu_ref_put(&peer_s->ring_ref);
+
+		if (!done)
+			break;
+		total += done;
+		size  -= done;
+	}
+
+	/* Wake exactly once, after the loop, re-deref'ing peer under RCU.
+	 * Doing this inside the loop would carry the _sync hint repeatedly
+	 * and cost a redundant wake per wraparound iteration.

net/ipv4/tcp_bpf.c void tcp_bpf_clone(const struct sock *sk, struct sock *newsk) · line 885

Quoted 10 lines
net/ipv4/tcp_bpf.c +10 -0
+	 */
+	if (total) {
+		rcu_read_lock();
+		peer = rcu_dereference(self_s->peer);
+		if (peer)
+			splice_wake_sync(peer->sk);
+		rcu_read_unlock();
+	}
+	return total;
+}

Is the flags parameter needed? The function signature includes an int flags parameter, but the function body never references it. The only flag checked is msg->msg_flags for MSG_OOB.

The caller tcp_bpf_splice_sendmsg computes this value as:

int flags = (msg->msg_flags & ~MSG_SENDPAGE_DECRYPTED) |

            MSG_NO_SHARED_FRAGS;

but it appears to go unused. Should it be dropped from the signature, or is there intended handling that was omitted?

---
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/27388683867

author
#4 lore raw

RFC PATCH 2/5 tcp_bpf: busy-poll the splice ring before parking the receiver

In reply to Cong Wang

When a paired-splice receiver finds the ring empty it parks on the socket waitqueue. For latency-bound synchronous-RPC workloads that is a wakeup per request-response cycle, which dominates the per-cycle cost.

Add an optional bounded busy-poll of the ring before parking, reusing the socket's SO_BUSY_POLL budget (sk_ll_usec) via sk_can_busy_loop() and sk_busy_loop_timeout(). The default budget of 0 leaves sk_can_busy_loop() false, so this is a no-op unless the application (or net.core.busy_read) opted in.

Unlike sk_busy_loop() / napi_busy_loop(), splice_busy_loop() spins on the in-kernel ring directly rather than polling a NAPI instance, so it is effective on loopback - which delivers via the per-CPU backlog and exposes no pollable napi_id. Keeping the receiver hot lets a synchronous sender's small writes accumulate in the ring without a wakeup per message; this is what turns the latency-bound TCP_RR case into a large win once enabled.

A BPF program enables the budget by setting SO_BUSY_POLL via bpf_setsockopt() (see the following patch). netperf, pinned CPUs, 3x10s, 50 us budget, baseline TCP vs splice + busy-poll:

  TCP_RR (loopback)    1 B    111.9k -> 1113.8k tps  (9.96x)
                       64 B   111.7k -> 1073.3k tps  (9.61x)
                       1 KB   106.1k ->  713.0k tps  (6.72x)
                       16 KB   40.3k ->  123.7k tps  (3.07x)
                       64 KB   17.8k ->   40.5k tps  (2.28x)
  TCP_RR (container)   1 B    105.6k -> 1103.7k tps  (10.45x)
                       64 B   105.5k -> 1103.9k tps  (10.46x)
                       1 KB   100.4k ->  704.9k tps  (7.02x)
                       16 KB   45.1k ->  114.8k tps  (2.54x)
                       64 KB   18.2k ->   38.8k tps  (2.13x)

Busy polling contributes ~4.2x of the 1 B loopback win (splice without it is 267.0k tps; see the splice patch). Baseline TCP is unchanged by busy_read on both loopback and default (non-XDP) veth: both deliver via the per-CPU backlog, which has no pollable napi_id, so SO_BUSY_POLL is a no-op for them (the container baseline TCP_RR measures the same at busy_read 0 and 50). The gain therefore comes from the splice ring spin, not from busy_read itself.

Assisted-by Claude:claude-opus-4.8
---
 net/ipv4/tcp_bpf.c | 38 ++++++++++++++++++++++++++++++++++++++
 1 file changed, 38 insertions(+)
net/ipv4/tcp_bpf.c +38 -0
index 549f37077244..9c4421a74225 100644
@@ -13,6 +13,7 @@
 #include <linux/util_macros.h>
 #include <linux/percpu-refcount.h>
 
+#include <net/busy_poll.h>
 #include <net/inet_common.h>
 #include <net/inet_sock.h>
 #include <net/tls.h>
@@ -1255,6 +1256,33 @@ static long splice_recv_wait(struct sock *sk, struct sk_psock_splice *s,
 					splice_recv_ready(sk, s), timeo);
 }
 
+/* Bounded busy-poll on the ring before parking the receiver. Reuses the
+ * socket's SO_BUSY_POLL budget (sk_ll_usec) via sk_can_busy_loop() and
+ * sk_busy_loop_timeout(); the default budget of 0 makes sk_can_busy_loop()
+ * false so this is a no-op unless the application (or net.core.busy_read)
+ * opted in.
+ *
+ * Unlike sk_busy_loop() / napi_busy_loop(), this spins on the in-kernel
+ * ring directly rather than polling a NAPI instance, so it is effective on
+ * loopback - which delivers via the per-CPU backlog and exposes no
+ * pollable napi_id. Keeping the receiver hot lets a synchronous sender's
+ * small writes accumulate in the ring without a wakeup per message.
+ */
+static void splice_busy_loop(struct sock *sk, struct sk_psock_splice *s)
+{
+	unsigned long start;
+
+	if (!sk_can_busy_loop(sk))
+		return;
+
+	start = busy_loop_current_time();
+	do {
+		cpu_relax();
+		if (splice_recv_ready(sk, s) || signal_pending(current))
+			return;
+	} while (!sk_busy_loop_timeout(sk, start));
+}
+
 /* prot->sock_is_readable for paired-splice sockets. tcp_stream_is_readable()
  * (via tcp_poll() / select() / epoll) consults this to mark POLLIN when
  * sk_receive_queue is empty - we must also report data sitting in the
@@ -1349,6 +1377,16 @@ static int tcp_bpf_splice_recvmsg(struct sock *sk,
 			return 0;
 		}
 
+		/* Spin on the ring for the SO_BUSY_POLL budget before
+		 * sleeping. If the spin observes data, re-read from the
+		 * loop head; otherwise (budget expired or a terminal
+		 * condition) proceed to park - splice_recv_wait() returns
+		 * immediately for terminal conditions.
+		 */
+		splice_busy_loop(sk, s);
+		if (splice_ring_has_data(s))
+			continue;
+
 		timeo = splice_recv_wait(sk, s, timeo);
 	}
 }
Signature · 1 line
2.43.0
#5 lore raw

Re: [RFC PATCH bpf-next 2/5] tcp_bpf: busy-poll the splice ring before parking the receiver

Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider: - [High] Missing `need_resched()` check in `splice_busy_loop()` allows an unprivileged user to monopolize a…

Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
- [High] Missing need_resched() check in splice_busy_loop() allows an unprivileged user to monopolize a CPU, causing soft lockups and denial of service.
- [High] tcp_bpf_splice_recvmsg incorrectly returns -EINTR instead of -ERESTARTSYS for blocking sockets when interrupted by a signal, breaking SA_RESTART semantics.
- [High] Providing an invalid memory buffer to recvmsg on a spliced socket causes an uninterruptible infinite loop in kernel space, leading to Denial of Service. --

commit 8f0193a527ddda6d7d50681be8a081f8bc267f16
Author: Cong Wang <[email protected]>

tcp_bpf: busy-poll the splice ring before parking the receiver

This commit adds an optional bounded busy-poll of the ring before parking, reusing the socket's SO_BUSY_POLL budget via sk_can_busy_loop() and sk_busy_loop_timeout(). It spins directly on the in-kernel ring without polling a NAPI instance.

Quoted 4 lines
net/ipv4/tcp_bpf.c +0 -0
index 549f370772446..9c4421a74225b 100644

[ ... ]

Quoted 19 lines
net/ipv4/tcp_bpf.c +15 -0
@@ -1255,6 +1256,33 @@ static long splice_recv_wait(struct sock *sk, struct sk_psock_splice *s,
 					splice_recv_ready(sk, s), timeo);
 }
 
+/* Bounded busy-poll on the ring before parking the receiver. Reuses the
+ * socket's SO_BUSY_POLL budget (sk_ll_usec) via sk_can_busy_loop() and
+ * sk_busy_loop_timeout(); the default budget of 0 makes sk_can_busy_loop()
+ * false so this is a no-op unless the application (or net.core.busy_read)
+ * opted in.
+ *
+ * Unlike sk_busy_loop() / napi_busy_loop(), this spins on the in-kernel
+ * ring directly rather than polling a NAPI instance, so it is effective on
+ * loopback - which delivers via the per-CPU backlog and exposes no
+ * pollable napi_id. Keeping the receiver hot lets a synchronous sender's
+ * small writes accumulate in the ring without a wakeup per message.
+ */
+static void splice_busy_loop(struct sock *sk, struct sk_psock_splice *s)
+{
+	unsigned long start;

net/ipv4/tcp_bpf.c static long splice_recv_wait(struct sock *sk, struct sk_psock_splice *s, · line 1283

Quoted 10 lines
net/ipv4/tcp_bpf.c +10 -0
+
+	if (!sk_can_busy_loop(sk))
+		return;
+
+	start = busy_loop_current_time();
+	do {
+		cpu_relax();
+		if (splice_recv_ready(sk, s) || signal_pending(current))
+			return;
+	} while (!sk_busy_loop_timeout(sk, start));

[Severity: High]
Does this loop need a need_resched() check or a call to cond_resched()?

Because an unprivileged user can set SO_BUSY_POLL to an extremely large value (up to INT_MAX microseconds), triggering this path via recvmsg() could cause the thread to spin on the CPU without yielding.

On non-preemptible kernels, this might completely starve other tasks, stall RCU grace periods, and trigger watchdog soft lockups.

Quoted 3 lines
net/ipv4/tcp_bpf.c +2 -0
+}
+
 /* prot->sock_is_readable for paired-splice sockets. tcp_stream_is_readable()

[ ... ]

net/ipv4/tcp_bpf.c static int tcp_bpf_splice_recvmsg(struct sock *sk, · line 1388

Quoted 13 lines
net/ipv4/tcp_bpf.c +9 -0
@@ -1349,6 +1377,16 @@ static int tcp_bpf_splice_recvmsg(struct sock *sk,
 			return 0;
 		}
 
+		/* Spin on the ring for the SO_BUSY_POLL budget before
+		 * sleeping. If the spin observes data, re-read from the
+		 * loop head; otherwise (budget expired or a terminal
+		 * condition) proceed to park - splice_recv_wait() returns
+		 * immediately for terminal conditions.
+		 */
+		splice_busy_loop(sk, s);
+		if (splice_ring_has_data(s))
+			continue;

[Severity: High]
Could this result in an infinite loop if a user provides an invalid buffer to recvmsg?

If the user buffer is unmapped memory, copy_to_iter() in splice_ring_read() called earlier in the loop might safely fault and return 0. If splice_ring_read() returns 0, the ring's tail pointer isn't advanced, and the data remains in the ring buffer.

tcp_bpf_splice_recvmsg() seems to treat the 0 return as an empty read rather than an -EFAULT error. When it subsequently checks splice_ring_has_data() here, it will evaluate to true because the data is still present.

This triggers the continue statement, which loops back immediately, potentially creating a tight infinite loop in the kernel.

net/ipv4/tcp_bpf.c static int tcp_bpf_splice_recvmsg(struct sock *sk, · line 1390

Quoted 2 lines
net/ipv4/tcp_bpf.c +1 -0
+
 		timeo = splice_recv_wait(sk, s, timeo);

[Severity: High]
Could this cause the system call to incorrectly return -EINTR instead of -ERESTARTSYS when interrupted by a signal?

When splice_recv_wait() uses wait_event_interruptible_timeout() and is interrupted, it returns -ERESTARTSYS (-512). This return value directly overwrites the timeo variable here.

On the next loop iteration, signal_pending(current) would evaluate to true, and the error code is computed via sock_intr_errno(timeo). Because timeo is now -512 instead of MAX_SCHEDULE_TIMEOUT, sock_intr_errno() might incorrectly return -EINTR instead of -ERESTARTSYS, which breaks SA_RESTART semantics.

Quoted 2 lines
net/ipv4/tcp_bpf.c +0 -0
 	}
 }
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
author
#6 lore raw

RFC PATCH 3/5 selftests/bpf: add tcp_splice basic round-trip test

In reply to Cong Wang

Loads a sock_ops BPF program that, on each ESTABLISHED callback, inserts self into a sockhash keyed by the local 4-tuple, looks up the peer using the swapped 4-tuple, and calls the new bpf_sock_splice_pair kfunc on whichever peer it finds. Counters track how many calls returned 0 (winner) vs -EEXIST (race loser) vs other errors.

Userspace creates a loopback TCP pair, waits for both ESTABLISHED callbacks to land, then verifies pair_ok >= 1 and pair_other_err == 0. A receiver thread blocks in recv() before the main thread sends; the test asserts the bytes round-trip through the rendezvous data plane.

Assisted-by Claude:claude-opus-4.8
---
 .../selftests/bpf/prog_tests/tcp_splice.c     | 206 ++++++++++++++++++
 .../selftests/bpf/progs/test_tcp_splice.c     | 101 +++++++++
 2 files changed, 307 insertions(+)
 create mode 100644 tools/testing/selftests/bpf/prog_tests/tcp_splice.c
 create mode 100644 tools/testing/selftests/bpf/progs/test_tcp_splice.c
tools/testing/selftests/bpf/prog_tests/tcp_splice.c new file +206 -0
new file mode 100644
index 000000000000..b80a1129c6aa
@@ -0,0 +1,206 @@
+// SPDX-License-Identifier: GPL-2.0
+#define _GNU_SOURCE
+#include <test_progs.h>
+#include "cgroup_helpers.h"
+#include "network_helpers.h"
+#include "test_tcp_splice.skel.h"
+
+#include <pthread.h>
+#include <sys/wait.h>
+#include <unistd.h>
+
+#define MSG "hello rendezvous"
+#define CLIENT_BANNER "client-banner"
+#define SERVER_BANNER "server-banner"
+
+struct recv_arg {
+	int fd;
+	char buf[64];
+	int n;
+	int err;
+};
+
+static void *recv_thread(void *p)
+{
+	struct recv_arg *a = p;
+
+	a->n = recv(a->fd, a->buf, sizeof(a->buf) - 1, 0);
+	a->err = errno;
+	return NULL;
+}
+
+struct send_arg {
+	int fd;
+	const char *buf;
+	size_t len;
+	int n;
+	int err;
+};
+
+static void *send_thread(void *p)
+{
+	struct send_arg *a = p;
+
+	a->n = send(a->fd, a->buf, a->len, 0);
+	a->err = errno;
+	return NULL;
+}
+
+static int run_basic(int cgroup_fd, struct test_tcp_splice *skel)
+{
+	pthread_t tid;
+	struct recv_arg a = {};
+	int sfd = -1, cfd = -1, lfd = -1;
+	int n, err = -1;
+
+	lfd = start_server(AF_INET, SOCK_STREAM, NULL, 0, 0);
+	if (!ASSERT_GE(lfd, 0, "start_server"))
+		return -1;
+
+	cfd = connect_to_fd(lfd, 0);
+	if (!ASSERT_GE(cfd, 0, "connect_to_fd"))
+		goto out;
+
+	sfd = accept(lfd, NULL, NULL);
+	if (!ASSERT_GE(sfd, 0, "accept"))
+		goto out;
+
+	/* Give both ESTABLISHED sock_ops callbacks a moment to run. */
+	usleep(50 * 1000);
+
+	if (!ASSERT_GE(skel->bss->pair_ok, 1, "splice paired"))
+		goto out;
+	ASSERT_EQ(skel->bss->pair_other_err, 0, "no unexpected pair errors");
+
+	/* Drive the splice fast path: receiver enters recv() and publishes
+	 * its bvec, sender then writes directly into it.
+	 */
+	a.fd = sfd;
+	if (!ASSERT_OK(pthread_create(&tid, NULL, recv_thread, &a),
+		       "pthread_create"))
+		goto out;
+	usleep(20 * 1000); /* let recv block */
+
+	n = send(cfd, MSG, strlen(MSG), 0);
+	ASSERT_EQ(n, (int)strlen(MSG), "send length");
+
+	pthread_join(tid, NULL);
+	ASSERT_EQ(a.n, (int)strlen(MSG), "recv length");
+	a.buf[a.n > 0 ? a.n : 0] = 0;
+	ASSERT_STREQ(a.buf, MSG, "recv contents");
+
+	err = 0;
+out:
+	if (cfd >= 0)
+		close(cfd);
+	if (sfd >= 0)
+		close(sfd);
+	if (lfd >= 0)
+		close(lfd);
+	return err;
+}
+
+/* Bidirectional-write deadlock-avoidance test.
+ *
+ * Both sides issue send() before either calls recv(), the classic
+ * pattern that used to deadlock under synchronous rendezvous (and
+ * the actual cause of "kex_exchange_identification: write: Broken
+ * pipe" with SSH on loopback). The bounded-wait fallback in
+ * tcp_bpf_splice_sendmsg() must let both writes complete via the
+ * normal TCP path within ~1 ms, and the banners must arrive intact
+ * on the other side when recv() is called next.
+ */
+static int run_bidir_write(int cgroup_fd, struct test_tcp_splice *skel)
+{
+	pthread_t client_send_tid, server_send_tid;
+	struct send_arg cs = { .buf = CLIENT_BANNER,
+			       .len = sizeof(CLIENT_BANNER) - 1 };
+	struct send_arg ss = { .buf = SERVER_BANNER,
+			       .len = sizeof(SERVER_BANNER) - 1 };
+	struct recv_arg cr = {}, sr = {};
+	int sfd = -1, cfd = -1, lfd = -1;
+	int err = -1;
+
+	lfd = start_server(AF_INET, SOCK_STREAM, NULL, 0, 0);
+	if (!ASSERT_GE(lfd, 0, "start_server"))
+		return -1;
+	cfd = connect_to_fd(lfd, 0);
+	if (!ASSERT_GE(cfd, 0, "connect_to_fd"))
+		goto out;
+	sfd = accept(lfd, NULL, NULL);
+	if (!ASSERT_GE(sfd, 0, "accept"))
+		goto out;
+
+	usleep(50 * 1000); /* let pair complete */
+
+	/* Both sides write first, neither reads yet. Both must return
+	 * within bounded time (no deadlock).
+	 */
+	cs.fd = cfd;
+	ss.fd = sfd;
+	if (!ASSERT_OK(pthread_create(&client_send_tid, NULL, send_thread, &cs),
+		       "client send thread"))
+		goto out;
+	if (!ASSERT_OK(pthread_create(&server_send_tid, NULL, send_thread, &ss),
+		       "server send thread"))
+		goto out;
+
+	pthread_join(client_send_tid, NULL);
+	pthread_join(server_send_tid, NULL);
+	ASSERT_EQ(cs.n, (int)cs.len, "client send length");
+	ASSERT_EQ(ss.n, (int)ss.len, "server send length");
+
+	/* Now read on each side - the bytes the peer wrote should have
+	 * landed via the TCP fallback path.
+	 */
+	cr.fd = cfd;
+	cr.n = recv(cr.fd, cr.buf, sizeof(cr.buf) - 1, 0);
+	ASSERT_EQ(cr.n, (int)ss.len, "client recv length");
+	cr.buf[cr.n > 0 ? cr.n : 0] = 0;
+	ASSERT_STREQ(cr.buf, SERVER_BANNER, "client got server banner");
+
+	sr.fd = sfd;
+	sr.n = recv(sr.fd, sr.buf, sizeof(sr.buf) - 1, 0);
+	ASSERT_EQ(sr.n, (int)cs.len, "server recv length");
+	sr.buf[sr.n > 0 ? sr.n : 0] = 0;
+	ASSERT_STREQ(sr.buf, CLIENT_BANNER, "server got client banner");
+
+	err = 0;
+out:
+	if (cfd >= 0)
+		close(cfd);
+	if (sfd >= 0)
+		close(sfd);
+	if (lfd >= 0)
+		close(lfd);
+	return err;
+}
+
+void test_tcp_splice(void)
+{
+	struct test_tcp_splice *skel;
+	int cgroup_fd, prog_fd;
+
+	cgroup_fd = test__join_cgroup("/tcp_splice");
+	if (!ASSERT_GE(cgroup_fd, 0, "join_cgroup"))
+		return;
+
+	skel = test_tcp_splice__open_and_load();
+	if (!ASSERT_OK_PTR(skel, "skel_open_load"))
+		goto close_cgroup;
+
+	prog_fd = bpf_program__fd(skel->progs.sockops_splice);
+	if (!ASSERT_OK(bpf_prog_attach(prog_fd, cgroup_fd, BPF_CGROUP_SOCK_OPS, 0),
+		       "attach sockops"))
+		goto destroy_skel;
+
+	if (test__start_subtest("basic"))
+		run_basic(cgroup_fd, skel);
+	if (test__start_subtest("bidir_write"))
+		run_bidir_write(cgroup_fd, skel);
+
+destroy_skel:
+	test_tcp_splice__destroy(skel);
+close_cgroup:
+	close(cgroup_fd);
+}
tools/testing/selftests/bpf/progs/test_tcp_splice.c new file +101 -0

101 lines not on this page · raw message

Signature · 1 line
2.43.0
#7 lore raw

Re: [RFC PATCH bpf-next 3/5] selftests/bpf: add tcp_splice basic round-trip test

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [Medium] If the second `pthread_create()` fails in `run_bidir_write`, the function returns early without…

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [Medium] If the second pthread_create() fails in run_bidir_write, the function returns early without joining the first thread, leading to a leaked thread and a Use-After-Free on the stack.
- [Low] Multi-line comments violate the required BPF subsystem comment style.
--

commit 2de2ff2207c8434507b69004f912f22fbc96a325
Author: Cong Wang <[email protected]>

selftests/bpf: add tcp_splice basic round-trip test

Adds a test for the tcp_splice BPF functionality using a sock_ops program to pair local TCP sockets via bpf_sock_splice_pair. The test includes basic round-trip and bidirectional-write subtests.

Quoted 5 lines
tools/testing/selftests/bpf/prog_tests/tcp_splice.c new file +0 -0
new file mode 100644
index 0000000000000..b80a1129c6aa8

[ ... ]

tools/testing/selftests/bpf/prog_tests/tcp_splice.c line 77

Quoted 3 lines
tools/testing/selftests/bpf/prog_tests/tcp_splice.c new file +3 -0
+	/* Drive the splice fast path: receiver enters recv() and publishes
+	 * its bvec, sender then writes directly into it.
+	 */

[Severity: Low]
This isn't a bug, but does this comment style violate the BPF subsystem guidelines? Multi-line comments should have the opening /* on its own line.

[ ... ]

Quoted 3 lines

+/* Bidirectional-write deadlock-avoidance test.
+ *
+ * Both sides issue send() before either calls recv(), the classic

[Severity: Low]
This isn't a bug, but shouldn't the opening /* be on its own line for multi-line comments in the BPF subsystem?

Quoted 9 lines

+ * pattern that used to deadlock under synchronous rendezvous (and
+ * the actual cause of "kex_exchange_identification: write: Broken
+ * pipe" with SSH on loopback). The bounded-wait fallback in
+ * tcp_bpf_splice_sendmsg() must let both writes complete via the
+ * normal TCP path within ~1 ms, and the banners must arrive intact
+ * on the other side when recv() is called next.
+ */

tools/testing/selftests/bpf/prog_tests/tcp_splice.c new file +2 -0
+static int run_bidir_write(int cgroup_fd, struct test_tcp_splice *skel)
+{

[ ... ]

tools/testing/selftests/bpf/prog_tests/tcp_splice.c line 146

Quoted 11 lines
tools/testing/selftests/bpf/prog_tests/tcp_splice.c new file +11 -0
+	/* Both sides write first, neither reads yet. Both must return
+	 * within bounded time (no deadlock).
+	 */
+	cs.fd = cfd;
+	ss.fd = sfd;
+	if (!ASSERT_OK(pthread_create(&client_send_tid, NULL, send_thread, &cs),
+		       "client send thread"))
+		goto out;
+	if (!ASSERT_OK(pthread_create(&server_send_tid, NULL, send_thread, &ss),
+		       "server send thread"))
+		goto out;

[Severity: Medium]
If the second pthread_create() fails, will this jump to out and return, leaving client_send_tid running in the background?

Since cs is allocated on the stack, could the background thread's execution of send_thread() write to cs.n and cs.err after the stack frame is destroyed, causing a use-after-free memory corruption in the test runner?

Quoted 3 lines
tools/testing/selftests/bpf/prog_tests/tcp_splice.c new file +3 -0
+
+	pthread_join(client_send_tid, NULL);
+	pthread_join(server_send_tid, NULL);

[ ... ]

tools/testing/selftests/bpf/progs/test_tcp_splice.c line 3

Quoted 9 lines
tools/testing/selftests/bpf/progs/test_tcp_splice.c new file +3 -0
new file mode 100644
index 0000000000000..09c7f0f9e3110
@@ -0,0 +1,101 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Sock_ops BPF program that pairs locally-connected TCP sockets via the
+ * bpf_sock_splice_pair kfunc. Each side of an established loopback

[Severity: Low]
This isn't a bug, but shouldn't the opening /* be on its own line for multi-line comments in the BPF subsystem?

Quoted 4 lines
tools/testing/selftests/bpf/progs/test_tcp_splice.c new file +4 -0
+ * connection inserts itself into a sockhash keyed by its 4-tuple and
+ * looks up the peer using the swapped tuple. Whichever side finds the
+ * peer attempts to splice; the race loser sees -EEXIST.
+ */

[ ... ]

tools/testing/selftests/bpf/progs/test_tcp_splice.c line 41

Quoted 5 lines
tools/testing/selftests/bpf/progs/test_tcp_splice.c new file +5 -0
+static __always_inline void mk_key(struct bpf_sock_ops *s,
+				   struct flow_key *k, int swap)
+{
+	/* skops->local_port is already in host byte order. skops->remote_port
+	 * is laid out as the network-order 16-bit port in the upper half of

[Severity: Low]
This isn't a bug, but shouldn't the opening /* be on its own line for multi-line comments in the BPF subsystem?

Quoted 5 lines
tools/testing/selftests/bpf/progs/test_tcp_splice.c new file +5 -0
+	 * a u32 (see sock_ops_convert_ctx_access); bpf_ntohl produces the
+	 * host-order port directly - no further shift.
+	 */
+	__u16 lport = (__u16)s->local_port;
+	__u16 rport = bpf_ntohl(s->remote_port);
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
author
#8 lore raw

RFC PATCH 4/5 bpf: allow SO_BUSY_POLL in bpf_setsockopt()

In reply to Cong Wang

Add SO_BUSY_POLL to the SOL_SOCKET allowlist in sol_socket_sockopt() so a sock_ops or cgroup BPF program can enable busy polling on a socket (set sk->sk_ll_usec) without an application setsockopt or the global net.core.busy_read sysctl. SO_BUSY_POLL needs no CAP_NET_ADMIN in sk_setsockopt(), so no privilege gating is added; the value is an int and joins the existing optlen == sizeof(int) group.

This lets a BPF program opt specific flows into busy polling at the point it has the context to decide. The TCP loopback splice path (bpf_sock_splice_pair) uses it: the splice receiver busy-polls the ring instead of parking, turning the latency-bound TCP_RR case into a large win (numbers are in the splice busy-poll patch).

Assisted-by Claude:claude-opus-4.8
---
 net/core/filter.c | 1 +
 1 file changed, 1 insertion(+)
net/core/filter.c +1 -0
index 9590877b0714..302dfaf03f39 100644
@@ -5325,6 +5325,7 @@ static int sol_socket_sockopt(struct sock *sk, int optname,
 	case SO_MAX_PACING_RATE:
 	case SO_BINDTOIFINDEX:
 	case SO_TXREHASH:
+	case SO_BUSY_POLL:
 	case SK_BPF_CB_FLAGS:
 		if (*optlen != sizeof(int))
 			return -EINVAL;
Signature · 1 line
2.43.0
author
#9 lore raw

RFC PATCH 5/5 selftests/bpf: set SO_BUSY_POLL from the tcp_splice sockops prog

In reply to Cong Wang

Set SO_BUSY_POLL (busy_poll_us) on each paired socket via bpf_setsockopt() so the splice receiver busy-polls the ring instead of parking - without net.core.busy_read or an application setsockopt.

The sock_ops prog runs for both the active and passive established callbacks, so each endpoint sets its own socket. This is done before the peer-not-found early return: pairing is asymmetric (only the second side to establish finds a peer and calls bpf_sock_splice_pair), so setting it only on the pairing side would leave the other end without busy-poll. bpf_setsockopt acts on skops->sk; the peer sets itself on its own callback. Busy polling is a receive-path optimization (splice_busy_loop() in tcp_bpf_splice_recvmsg()); TCP is full-duplex so both ends are receivers and both need it, which the per-endpoint setting provides.

Assisted-by Claude:claude-opus-4.8
---
 .../selftests/bpf/progs/test_tcp_splice.c     | 24 +++++++++++++++++++
 1 file changed, 24 insertions(+)
tools/testing/selftests/bpf/progs/test_tcp_splice.c +24 -0
index 09c7f0f9e311..da43f00046c0 100644
@@ -9,6 +9,13 @@
 #include <bpf/bpf_helpers.h>
 #include <bpf/bpf_endian.h>
 
+#ifndef SOL_SOCKET
+#define SOL_SOCKET 1
+#endif
+#ifndef SO_BUSY_POLL
+#define SO_BUSY_POLL 46
+#endif
+
 struct flow_key {
 	__u32	saddr;
 	__u32	daddr;
@@ -29,6 +36,8 @@ void *bpf_cast_to_kern_ctx(void *obj) __ksym;
 __u32 pair_ok;
 __u32 pair_other_err;
 
+__u32 busy_poll_us;
+
 /* IPv4 only: the verifier doesn't accept memcpy from sock_ops ctx
  * because it lowers to "ctx + reg" pointer arithmetic. IPv6 support
  * would need explicit field-by-field reads of local_ip6[i] /
@@ -71,6 +80,21 @@ int sockops_splice(struct bpf_sock_ops *skops)
 	if (skops->family != 2 /* AF_INET */)
 		return 0;
 
+	/* Enable busy-poll on this socket. Both endpoints run this callback,
+	 * so each sets its own socket; this must happen here, before the
+	 * peer-not-found early return below, because pairing is asymmetric -
+	 * only the second side to establish finds a peer and calls
+	 * bpf_sock_splice_pair. Setting it only on the pairing side would
+	 * leave the other side without busy-poll. bpf_setsockopt acts on
+	 * skops->sk only - there is no variant to set the peer - but the peer
+	 * sets itself when its own ESTABLISHED callback fires.
+	 */
+	if (busy_poll_us) {
+		int us = busy_poll_us;
+
+		bpf_setsockopt(skops, SOL_SOCKET, SO_BUSY_POLL, &us, sizeof(us));
+	}
+
 	mk_key(skops, &self_key, 0);
 	mk_key(skops, &peer_key, 1);
 
Signature · 1 line
2.43.0
#10 lore raw

Re: [RFC PATCH bpf-next 5/5] selftests/bpf: set SO_BUSY_POLL from the tcp_splice sockops prog

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [Medium] The BPF global variable `busy_poll_us` is never initialized by the userspace test runner…

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [Medium] The BPF global variable busy_poll_us is never initialized by the userspace test runner, rendering the newly added SO_BUSY_POLL test logic unreachable dead code.
- [Low] Multi-line comment does not follow BPF subsystem guidelines.
--

commit c9396935bece9a3118a940f3f318b60c9109d778
Author: Cong Wang <[email protected]>

selftests/bpf: set SO_BUSY_POLL from the tcp_splice sockops prog

Set SO_BUSY_POLL (busy_poll_us) on each paired socket via bpf_setsockopt() so the splice receiver busy-polls the ring instead of parking - without net.core.busy_read or an application setsockopt.

Quoted 4 lines
tools/testing/selftests/bpf/progs/test_tcp_splice.c +0 -0
index 09c7f0f9e3110..da43f00046c0f 100644

[ ... ]

tools/testing/selftests/bpf/progs/test_tcp_splice.c int sockops_splice(struct bpf_sock_ops *skops) · line 84

Quoted 6 lines
tools/testing/selftests/bpf/progs/test_tcp_splice.c +2 -0
@@ -71,6 +80,21 @@ int sockops_splice(struct bpf_sock_ops *skops)
 	if (skops->family != 2 /* AF_INET */)
 		return 0;
 
+	/* Enable busy-poll on this socket. Both endpoints run this callback,
+	 * so each sets its own socket; this must happen here, before the

[Severity: Low]
This isn't a bug, but the BPF subsystem multi-line comment style requires the opening /* to be on its own line. Should this be updated to match the modern kernel comment style?

tools/testing/selftests/bpf/progs/test_tcp_splice.c int sockops_splice(struct bpf_sock_ops *skops) · line 96

Quoted 12 lines
tools/testing/selftests/bpf/progs/test_tcp_splice.c +12 -0
+	 * peer-not-found early return below, because pairing is asymmetric -
+	 * only the second side to establish finds a peer and calls
+	 * bpf_sock_splice_pair. Setting it only on the pairing side would
+	 * leave the other side without busy-poll. bpf_setsockopt acts on
+	 * skops->sk only - there is no variant to set the peer - but the peer
+	 * sets itself when its own ESTABLISHED callback fires.
+	 */
+	if (busy_poll_us) {
+		int us = busy_poll_us;
+
+		bpf_setsockopt(skops, SOL_SOCKET, SO_BUSY_POLL, &us, sizeof(us));
+	}

[Severity: Medium]
Is the global variable busy_poll_us ever initialized by the userspace test runner?

Since BPF .bss variables are zero-initialized by default, it appears that skel->bss->busy_poll_us is never assigned a value in the userspace setup code (like prog_tests/tcp_splice.c). Does this mean the if (busy_poll_us) condition will always evaluate to false, leaving the new SO_BUSY_POLL logic unreachable and untested?

Quoted 3 lines
tools/testing/selftests/bpf/progs/test_tcp_splice.c +1 -0
+
 	mk_key(skops, &self_key, 0);
 	mk_key(skops, &peer_key, 1);
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=5
#11 lore raw
In reply to Cong Wang
Quoted 53 lines · Cong Wang

This series adds an opportunistic "loopback splice" fast path for two locally-connected TCP sockets that a sock_ops BPF program pairs at handshake completion. Once paired, sendmsg copies the user payload into a per-direction in-kernel byte ring and recvmsg drains it on the other side; both copies happen in their own task's mm, so the fast path incurs no skb construction, no softirq, and no TCP protocol-state processing.

The underlying TCP connection stays fully real: sequence numbers are frozen at post-handshake values, so FIN/RST/keepalive keep flowing through the normal paths and the pair tears down via a regular close. Pairing is opt-in per flow and fallback is per-message - handshake-style traffic takes the TCP path, the bulk phase takes the ring, on the same socket. Nothing leaves the host and applications need no changes: no new address family, no LD_PRELOAD, no source modification.

The target use cases are co-located endpoints that speak plain TCP:
- regular TCP loopback (127.0.0.1) between processes on the same host;
- container sidecar deployments - e.g. a service-mesh sidecar proxy and its application in the same pod, talking over loopback or a veth pair - where the per-skb veth+bridge cost is exactly what the ring sidesteps.

Highlights (TCP_RR, 1 KB request/response, netperf, pinned CPUs, baseline TCP vs splice; full tables across message sizes and TCP_STREAM in patches 1 and 2):

loopback (127.0.0.1):

    without busy-poll:   105.8k -> 235.1k tps  (2.2x)
    with busy-poll 50us: 106.1k -> 713.0k tps  (6.7x)

container (netns + veth + bridge):

    without busy-poll:    99.9k -> 233.9k tps  (2.3x)
    with busy-poll 50us: 100.4k -> 704.9k tps  (7.0x)

Synchronous-RPC (TCP_RR) at a 1 KB message wins ~2.2x without busy polling and ~6.7x with it (the win grows toward smaller messages and narrows toward 64 KB), because the ring removes the per-cycle kernel TCP receive-path cost and the receiver can spin on the ring directly - loopback delivers via the per-CPU backlog and exposes no pollable napi_id, so the generic sk_busy_loop() is a no-op there. Bulk streaming is roughly neutral on bare-metal loopback but wins decisively (up to ~6x) container-to-container, where per-skb veth+bridge cost dominates the path the ring sidesteps.

---
Cong Wang (5):
tcp_bpf: add bpf_sock_splice_pair kfunc for opportunistic loopback
splice
tcp_bpf: busy-poll the splice ring before parking the receiver selftests/bpf: add tcp_splice basic round-trip test
bpf: allow SO_BUSY_POLL in bpf_setsockopt() selftests/bpf: set SO_BUSY_POLL from the tcp_splice sockops prog

Quoted 8 lines
 include/linux/skmsg.h                         |   9 +
 include/net/tcp.h                             |   8 +
 net/core/filter.c                             |   1 +
 net/core/skmsg.c                              |   3 +
 net/ipv4/tcp_bpf.c                            | 847 +++++++++++++++++-
 .../selftests/bpf/prog_tests/tcp_splice.c     | 206 +++++
 .../selftests/bpf/progs/test_tcp_splice.c     | 125 +++
 7 files changed, 1198 insertions(+), 1 deletion(-)

Just saying that the code is free nowadays, so whether it's 1k lines or 10 lines is irrelevant for the discussion.

As far as the idea goes, I think, it would be interesting in pre-AI era, but today splice and friends are a prime target for bugs and more bugs. skmsg and tcp_bpf are reeling from unfixed bugs too,
so my take is that we should not add any new features to skmsg and instead deprecate what is already there.

#12 lore raw
Quoted 8 lines · Alexei Starovoitov

Just saying that the code is free nowadays, so whether it's 1k lines or 10 lines is irrelevant for the discussion.

As far as the idea goes, I think, it would be interesting in pre-AI era, but today splice and friends are a prime target for bugs and more bugs. skmsg and tcp_bpf are reeling from unfixed bugs too,
so my take is that we should not add any new features to skmsg and instead deprecate what is already there.

I guess maybe the name misleads you, it has nothing related to splice() syscall. Its ring buffer was developed on top of include/linux/circ_buf.h which again has nothing related to splice()/vmsplice()/pipe().

In case it is not obvious, this patchset does not add any new user-space interface, only a kfunc which is visible to only sockmap eBPF programs which already require CAP_BPF privilege.

If you have a better name on your mind, I am happy to change it.

Thanks.

#13 lore raw
Quoted 11 lines · Cong Wang
Quoted 8 lines · Alexei Starovoitov

Just saying that the code is free nowadays, so whether it's 1k lines or 10 lines is irrelevant for the discussion.

As far as the idea goes, I think, it would be interesting in pre-AI era, but today splice and friends are a prime target for bugs and more bugs. skmsg and tcp_bpf are reeling from unfixed bugs too,
so my take is that we should not add any new features to skmsg and instead deprecate what is already there.

Quoted 7 lines

I guess maybe the name misleads you, it has nothing related to splice() syscall. Its ring buffer was developed on top of include/linux/circ_buf.h which again has nothing related to splice()/vmsplice()/pipe().

In case it is not obvious, this patchset does not add any new user-space interface, only a kfunc which is visible to only sockmap eBPF programs which already require CAP_BPF privilege.

Not the name, but the concept. Taking from one socket and feeding into another already caused a ton of issues for the networking stack. If you can convince Kuba we can entertain it.

#14 lore raw
Quoted 16 lines · Alexei Starovoitov
Quoted 15 lines · Cong Wang
Quoted 8 lines · Alexei Starovoitov

Just saying that the code is free nowadays, so whether it's 1k lines or 10 lines is irrelevant for the discussion.

As far as the idea goes, I think, it would be interesting in pre-AI era, but today splice and friends are a prime target for bugs and more bugs. skmsg and tcp_bpf are reeling from unfixed bugs too,
so my take is that we should not add any new features to skmsg and instead deprecate what is already there.

I guess maybe the name misleads you, it has nothing related to splice() syscall. Its ring buffer was developed on top of include/linux/circ_buf.h which again has nothing related to splice()/vmsplice()/pipe().

Quoted 7 lines
Quoted 3 lines

In case it is not obvious, this patchset does not add any new user-space interface, only a kfunc which is visible to only sockmap eBPF programs which already require CAP_BPF privilege.

Not the name, but the concept. Taking from one socket and feeding into another already caused a ton of issues for the networking stack. If you can convince Kuba we can entertain it.

If you could be specific and provide examples, I could provide better answer and take better actions.

Until that, all I can say is Copy Fail leverages page *references*, bpf_sock_splice_pair() shares no pages, it is a private kernel allocation, with no pipe_buffer or page-cache involvement at all. Probably the most common thing between these 2 is the name "splice".

In fact, it has 2 copies (not 0, not 1) by design, see details here:
https://multikernel.io/2026/06/11/bpf-sock-splice-pair-two-copies/

Or if you mean skmsg or sockmap has a lot of bugs, this is true but it is mostly due to TLS (which codebase is already a mess) and the complication of skmsg itself, none of them is related to bpf_sock_splice_pair().

For your reference, this is the data sheet I collected with AI:

  ┌─────────────────────┬─────────┬──────────┬
  │ Code path the fixes │  ~Fix   │ Splice   │
  │       live in       │ commits │  ring    │
  │                     │         │ uses it? │
  ├─────────────────────┼─────────┼──────────┼
  │ sk_msg / verdict /  │         │          │
  │ strparser / skb     │     ~59 │    No    │
  │ redirect            │         │          │
  ├─────────────────────┼─────────┼──────────┼
  │ TLS / ULP layering  │       8 │    No    │
  ├─────────────────────┼─────────┼──────────┼
  │ psock / sock_map    │         │          │
  │ teardown (close,    │     ~10 │   Yes    │
  │ unhash, destroy,    │         │          │
  │ replace, free)      │         │          │
  └─────────────────────┴─────────┴──────────┴

Thanks for your comments!
Cong

maintainer
#15 lore raw
In reply to Alexei Starovoitov
Quoted 8 lines · On Fri, 12 Jun 2026 09:01:43 -0700 Alexei Starovoitov wrote:

Just saying that the code is free nowadays, so whether it's 1k lines or 10 lines is irrelevant for the discussion.

As far as the idea goes, I think, it would be interesting in pre-AI era, but today splice and friends are a prime target for bugs and more bugs. skmsg and tcp_bpf are reeling from unfixed bugs too,
so my take is that we should not add any new features to skmsg and instead deprecate what is already there.

100% agreed. There are so many unfixed skmsg bugs it's hard to know were to start :( Kernel "intelligence" to help unoptimized applications is particularly unappealing right now.

#16 lore raw
Quoted 10 lines · Jakub Kicinski
Quoted 8 lines · On Fri, 12 Jun 2026 09:01:43 -0700 Alexei Starovoitov wrote:

Just saying that the code is free nowadays, so whether it's 1k lines or 10 lines is irrelevant for the discussion.

As far as the idea goes, I think, it would be interesting in pre-AI era, but today splice and friends are a prime target for bugs and more bugs. skmsg and tcp_bpf are reeling from unfixed bugs too,
so my take is that we should not add any new features to skmsg and instead deprecate what is already there.

Quoted 3 lines

100% agreed. There are so many unfixed skmsg bugs it's hard to know were to start :( Kernel "intelligence" to help unoptimized applications is particularly unappealing right now.

You are absolutely right. :)

Thanks for offering opportunity for me to make profit out of it.

syzbot Tested-by [email protected] on the v1 series
#17 lore raw
In reply to Cong Wang
syzbot ci has tested the following series

syzbot ci has tested the following series

[v1] tcp: opportunistic loopback splice for BPF-paired sockets
https://lore.kernel.org/all/[email protected]
* [RFC PATCH bpf-next 1/5] tcp_bpf: add bpf_sock_splice_pair kfunc for opportunistic loopback splice
* [RFC PATCH bpf-next 2/5] tcp_bpf: busy-poll the splice ring before parking the receiver
* [RFC PATCH bpf-next 3/5] selftests/bpf: add tcp_splice basic round-trip test
* [RFC PATCH bpf-next 4/5] bpf: allow SO_BUSY_POLL in bpf_setsockopt()
* [RFC PATCH bpf-next 5/5] selftests/bpf: set SO_BUSY_POLL from the tcp_splice sockops prog

and found the following issues:
* WARNING: suspicious RCU usage in tcp_bpf_recvmsg
* WARNING: suspicious RCU usage in tcp_bpf_splice_sendmsg

Full report is available here:
https://ci.syzbot.org/series/7c43d5ae-cb19-4b2b-96ad-f7f0806ac63c

***

WARNING: suspicious RCU usage in tcp_bpf_recvmsg

tree:      bpf-next
URL:       https://kernel.googlesource.com/pub/scm/linux/kernel/git/bpf/bpf-next.git
base:      30dee2c176e7954f63d1fa3e52d172f30beb9bfb
arch:      amd64
compiler:  Debian clang version 21.1.8 (++20251221033036+2078da43e25a-1~exp1~20251221153213.50), Debian LLD 21.1.8
config:    https://ci.syzbot.org/builds/09e43fc4-ebab-492e-b1de-15bb86aa4588/config
syz repro: https://ci.syzbot.org/findings/79db1500-f71c-4550-8525-89bf06044d60/syz_repro

=============================
WARNING: suspicious RCU usage
syzkaller #0 Not tainted
-----------------------------
net/ipv4/tcp_bpf.c:883 suspicious rcu_dereference_check() usage!

other info that might help us debug this:

rcu_scheduler_active = 2, debug_locks = 1
no locks held by syz.0.17/5822.

stack backtrace:
CPU: 1 UID: 0 PID: 5822 Comm: syz.0.17 Not tainted syzkaller #0 PREEMPT(full) 
Hardware name: QEMU Standard PC (Q35 + ICH9, 2009), BIOS 1.16.2-debian-1.16.2-1 04/01/2014
Call Trace:
 <TASK>
 dump_stack_lvl+0xe8/0x150 lib/dump_stack.c:120
 lockdep_rcu_suspicious+0x13f/0x1d0 kernel/locking/lockdep.c:6876
 sk_psock_is_spliced net/ipv4/tcp_bpf.c:883 [inline]
 tcp_bpf_recvmsg+0x1780/0x1980 net/ipv4/tcp_bpf.c:405
 sock_recvmsg_nosec+0xee/0x140 net/socket.c:1137
 ____sys_recvmsg+0x3e3/0x4a0 net/socket.c:2916
 ___sys_recvmsg+0x215/0x590 net/socket.c:2960
 do_recvmmsg+0x334/0x800 net/socket.c:3055
 __sys_recvmmsg net/socket.c:3129 [inline]
 __do_sys_recvmmsg net/socket.c:3152 [inline]
 __se_sys_recvmmsg net/socket.c:3145 [inline]
 __x64_sys_recvmmsg+0x198/0x250 net/socket.c:3145
 do_syscall_x64 arch/x86/entry/syscall_64.c:63 [inline]
 do_syscall_64+0x174/0x580 arch/x86/entry/syscall_64.c:94
 entry_SYSCALL_64_after_hwframe+0x77/0x7f
RIP: 0033:0x7f1af799ce59
Code: ff c3 66 2e 0f 1f 84 00 00 00 00 00 0f 1f 44 00 00 48 89 f8 48 89 f7 48 89 d6 48 89 ca 4d 89 c2 4d 89 c8 4c 8b 4c 24 08 0f 05 <48> 3d 01 f0 ff ff 73 01 c3 48 c7 c1 e8 ff ff ff f7 d8 64 89 01 48
RSP: 002b:00007f1af87bc028 EFLAGS: 00000246 ORIG_RAX: 000000000000012b
RAX: ffffffffffffffda RBX: 00007f1af7c15fa0 RCX: 00007f1af799ce59
RDX: 0000000000000002 RSI: 0000200000000400 RDI: 0000000000000003
RBP: 00007f1af7a32d6f R08: 0000000000000000 R09: 0000000000000000
R10: 0000000000010051 R11: 0000000000000246 R12: 0000000000000000
R13: 00007f1af7c16038 R14: 00007f1af7c15fa0 R15: 00007ffdef205588
 </TASK>

***

WARNING: suspicious RCU usage in tcp_bpf_splice_sendmsg

tree:      bpf-next
URL:       https://kernel.googlesource.com/pub/scm/linux/kernel/git/bpf/bpf-next.git
base:      30dee2c176e7954f63d1fa3e52d172f30beb9bfb
arch:      amd64
compiler:  Debian clang version 21.1.8 (++20251221033036+2078da43e25a-1~exp1~20251221153213.50), Debian LLD 21.1.8
config:    https://ci.syzbot.org/builds/09e43fc4-ebab-492e-b1de-15bb86aa4588/config
syz repro: https://ci.syzbot.org/findings/8144de7d-1a2e-454e-ab17-08d1c6586df2/syz_repro

=============================
WARNING: suspicious RCU usage
syzkaller #0 Not tainted
-----------------------------
net/ipv4/tcp_bpf.c:883 suspicious rcu_dereference_check() usage!

other info that might help us debug this:

rcu_scheduler_active = 2, debug_locks = 1
no locks held by syz.2.19/5817.

stack backtrace:
CPU: 0 UID: 0 PID: 5817 Comm: syz.2.19 Not tainted syzkaller #0 PREEMPT(full) 
Hardware name: QEMU Standard PC (Q35 + ICH9, 2009), BIOS 1.16.2-debian-1.16.2-1 04/01/2014
Call Trace:
 <TASK>
 dump_stack_lvl+0xe8/0x150 lib/dump_stack.c:120
 lockdep_rcu_suspicious+0x13f/0x1d0 kernel/locking/lockdep.c:6876
 sk_psock_is_spliced net/ipv4/tcp_bpf.c:883 [inline]
 tcp_bpf_splice_sendmsg+0x1165/0x1490 net/ipv4/tcp_bpf.c:897
 sock_sendmsg_nosec net/socket.c:787 [inline]
 __sock_sendmsg net/socket.c:802 [inline]
 ____sys_sendmsg+0x80a/0x9f0 net/socket.c:2698
 ___sys_sendmsg+0x2a5/0x360 net/socket.c:2752
 __sys_sendmmsg+0x27c/0x4e0 net/socket.c:2841
 __do_sys_sendmmsg net/socket.c:2868 [inline]
 __se_sys_sendmmsg net/socket.c:2865 [inline]
 __x64_sys_sendmmsg+0xa0/0xc0 net/socket.c:2865
 do_syscall_x64 arch/x86/entry/syscall_64.c:63 [inline]
 do_syscall_64+0x174/0x580 arch/x86/entry/syscall_64.c:94
 entry_SYSCALL_64_after_hwframe+0x77/0x7f
RIP: 0033:0x7fd1c339ce59
Code: ff c3 66 2e 0f 1f 84 00 00 00 00 00 0f 1f 44 00 00 48 89 f8 48 89 f7 48 89 d6 48 89 ca 4d 89 c2 4d 89 c8 4c 8b 4c 24 08 0f 05 <48> 3d 01 f0 ff ff 73 01 c3 48 c7 c1 e8 ff ff ff f7 d8 64 89 01 48
RSP: 002b:00007fd1c42e3028 EFLAGS: 00000246 ORIG_RAX: 0000000000000133
RAX: ffffffffffffffda RBX: 00007fd1c3615fa0 RCX: 00007fd1c339ce59
RDX: 0000000000000001 RSI: 0000200000001000 RDI: 0000000000000003
RBP: 00007fd1c3432d6f R08: 0000000000000000 R09: 0000000000000000
R10: 0000000004008005 R11: 0000000000000246 R12: 0000000000000000
R13: 00007fd1c3616038 R14: 00007fd1c3615fa0 R15: 00007ffdcdbac378
 </TASK>

***

If these findings have caused you to resend the series or submit a separate fix, please add the following tag to your commit message:

---
This report is generated by a bot. It may contain errors. syzbot ci engineers can be reached at [email protected].

To test a patch for this bug, please reply with #syz test (should be on a separate line).

The patch should be attached to the email.
Note: arguments like custom git repos and branches are not supported.