bpf: Add SOCK_OPS hooks for TCP AutoLOWAT.
Subsystems (8)
Kuniyuki Iwashima
reviewerv2 cover bpf: Add SOCK_OPS hooks for TCP AutoLOWAT.
This series introduces BPF_SOCK_OPS_RCVQ_CB, a new type of opt-in hooks for BPF SOCK_OPS prog.
The hooks can be enabled on per-socket basis by bpf_setsockopt():
int flag = BPF_SOCK_OPS_RCVQ_CB_FLAG;
bpf_setsockopt(sk, SOL_TCP, TCP_BPF_SOCK_OPS_CB_FLAGS,
&flags, sizeof(flags));or via the SOCK_OPS specific helper:
bpf_sock_ops_cb_flags_set(skops, BPF_SOCK_OPS_RCVQ_CB_FLAG);
Once activated, the BPF prog will be invoked with bpf_sock_ops.op set to BPF_SOCK_OPS_RCVQ_CB upon the following events:
1. TCP stack enqueues skb to sk->sk_receive_queue
2. TCP recvmsg() completes
This allows the BPF prog to dynamically adjust sk->sk_rcvlowat, suppressing unnecessary EPOLLIN wakeups until sufficient data is available in the receive queue.
This functionality, which we call "TCP AutoLOWAT", was originally developed in 2020 by Tenzin Ukyab with the help of Soheil Hassas Yeganeh, Arjun Roy, and Eric Dumazet. It has served Google RPC workloads for more than 5 years.
Combined with TCP RX zerocopy, this typically allows us to read an entire RPC frame with just a single wakeup and a single system call.
While the original implementation was specialised for our internal RPC format, this series introduces a more flexible version by leveraging BPF.
The BPF SOCK_OPS prog in the last selftest patch closely mirrors the core logic of the original implementation to provide a real-world example.
Overview:
Patch 1 : misc cleanup for testing Patch 2 : Add BPF_SOCK_OPS_RCVQ_CB with no actual hooks Patch 3 - 5 : Add bpf helpers Patch 6 - 8 : Add safe guard for BPF_SOCK_OPS_RCVQ_CB Patch 9 - 10 : Add BPF_SOCK_OPS_RCVQ_CB hooks
Patch 11 : selftest
Changes:
v2:
Add Patch 6 - 8
Patch 2: s/BPF_SOCK_OPS_RCVLOWAT_CB/BPF_SOCK_OPS_RCVQ_CB/g Patch 3: Explain why using ____ version instead of __ version Patch 10: Add explanation of tcp_bpf_rcvlowat() placement. Patch 11: Make copy_len u64 and swap validation order for it
to pass no_alu32 test case
v1: https://lore.kernel.org/bpf/[email protected]/
Kuniyuki Iwashima (11):
selftest: bpf: Use BPF_SOCK_OPS_ALL_CB_FLAGS + 1 for bad_cb_test_rv.
bpf: tcp: Introduce BPF_SOCK_OPS_RCVQ_CB.
bpf: tcp: Support bpf_skb_load_bytes() for BPF_SOCK_OPS_RCVQ_CB.
tcp: Split out __tcp_set_rcvlowat().
bpf: tcp: Add kfunc to adjust sk->sk_rcvlowat.
bpf: tcp: Make BPF_SOCK_OPS_RCVQ_CB and SOCKMAP mutually exclusive.
bpf: mptcp: Don't support BPF_SOCK_OPS_RCVQ_CB.
bpf: tcp: Reject BPF_SOCK_OPS_RCVQ_CB if receive queue is not empty.
bpf: tcp: Factorise bpf_skops_established().
bpf: tcp: Add SOCK_OPS rcvlowat hook.
selftest: bpf: Add test for BPF_SOCK_OPS_RCVQ_CB.
include/net/tcp.h | 13 + include/uapi/linux/bpf.h | 18 +- net/core/filter.c | 89 ++++- net/ipv4/tcp.c | 14 +- net/ipv4/tcp_bpf.c | 2 + net/ipv4/tcp_fastopen.c | 2 + net/ipv4/tcp_input.c | 25 +- tools/include/uapi/linux/bpf.h | 18 +- tools/testing/selftests/bpf/bpf_kfuncs.h | 4 + .../selftests/bpf/prog_tests/tcp_autolowat.c | 350 ++++++++++++++++++ .../selftests/bpf/prog_tests/tcpbpf_user.c | 3 +- .../selftests/bpf/progs/bpf_tracing_net.h | 2 + .../selftests/bpf/progs/tcp_autolowat.c | 326 ++++++++++++++++ .../selftests/bpf/progs/test_tcpbpf_kern.c | 3 +- 14 files changed, 855 insertions(+), 14 deletions(-) create mode 100644 tools/testing/selftests/bpf/prog_tests/tcp_autolowat.c create mode 100644 tools/testing/selftests/bpf/progs/tcp_autolowat.c
Signature · 1 line
2.54.0.746.g67dd491aae-goog
Kuniyuki Iwashima
reviewerPATCH v2 1/11 selftest: bpf: Use BPF_SOCK_OPS_ALL_CB_FLAGS + 1 for bad_cb_test_rv.
Once bpf_sock_ops_cb_flags_set() supports a new flag, tcpbpf_user.c fails due to the hard-coded max value, 0x80.
Let's replace 0x80 with BPF_SOCK_OPS_ALL_CB_FLAGS + 1.
--- tools/testing/selftests/bpf/prog_tests/tcpbpf_user.c | 3 ++- tools/testing/selftests/bpf/progs/test_tcpbpf_kern.c | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-)
tools/testing/selftests/bpf/prog_tests/tcpbpf_user.c +2 -1
@@ -26,7 +26,8 @@ static void verify_result(struct tcpbpf_globals *result)ASSERT_EQ (result->bytes_acked,1002 ,"bytes_acked" );ASSERT_EQ (result->data_segs_in,1 ,"data_segs_in" );ASSERT_EQ (result->data_segs_out,1 ,"data_segs_out" ); -ASSERT_EQ (result->bad_cb_test_rv,0x80 ,"bad_cb_test_rv" ); +ASSERT_EQ (result->bad_cb_test_rv,BPF_SOCK_OPS_ALL_CB_FLAGS +1 , +"bad_cb_test_rv" );ASSERT_EQ (result->good_cb_test_rv,0 ,"good_cb_test_rv" );ASSERT_EQ (result->num_listen,1 ,"num_listen" );
tools/testing/selftests/bpf/progs/test_tcpbpf_kern.c +2 -1
@@ -92,7 +92,8 @@ int bpf_testcb(struct bpf_sock_ops *skops)break ;case BPF_SOCK_OPS_ACTIVE_ESTABLISHED_CB :/* Test failure to set largest cb flag (assumes not defined) */ - global.bad_cb_test_rv =bpf_sock_ops_cb_flags_set (skops,0x80 ); + global.bad_cb_test_rv =bpf_sock_ops_cb_flags_set (skops, +BPF_SOCK_OPS_ALL_CB_FLAGS +1 );/* Set callback */ global.good_cb_test_rv =bpf_sock_ops_cb_flags_set (skops,BPF_SOCK_OPS_STATE_CB_FLAG );
Signature · 1 line
2.54.0.746.g67dd491aae-goog
[email protected]
AI reviewRe: [PATCH v2 bpf-next 01/11] selftest: bpf: Use BPF_SOCK_OPS_ALL_CB_FLAGS + 1 for bad_cb_test_rv.
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] Deriving an invalid bitmask using `BPF_SOCK_OPS_ALL_CB_FLAGS + 1` is mathematically brittle and…
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] Deriving an invalid bitmask using BPF_SOCK_OPS_ALL_CB_FLAGS + 1 is mathematically brittle and breaks binary forward compatibility. --
commit c1e093a27fd28a0f7083a5bd0002abbedd3222a2
Author: Kuniyuki Iwashima <[email protected]>
selftest: bpf: Use BPF_SOCK_OPS_ALL_CB_FLAGS + 1 for bad_cb_test_rv.
This commit replaces a hard-coded 0x80 with BPF_SOCK_OPS_ALL_CB_FLAGS + 1 to test the failure path of bpf_sock_ops_cb_flags_set() in BPF selftests.
tools/testing/selftests/bpf/progs/test_tcpbpf_kern.c int bpf_testcb(struct bpf_sock_ops *skops) · line 96
Quoted 10 lines
tools/testing/selftests/bpf/progs/test_tcpbpf_kern.c +2
-1@@ -92,7 +92,8 @@ int bpf_testcb(struct bpf_sock_ops *skops)break ;case BPF_SOCK_OPS_ACTIVE_ESTABLISHED_CB :/* Test failure to set largest cb flag (assumes not defined) */ - global.bad_cb_test_rv =bpf_sock_ops_cb_flags_set (skops,0x80 ); + global.bad_cb_test_rv =bpf_sock_ops_cb_flags_set (skops, +BPF_SOCK_OPS_ALL_CB_FLAGS +1 );
[Severity: Medium]
Is using BPF_SOCK_OPS_ALL_CB_FLAGS + 1 mathematically brittle here?
If BPF_SOCK_OPS_ALL_CB_FLAGS is not a contiguous sequence of bits starting from bit 0 (for example, if a flag is ever skipped or deprecated), this addition might create a value that overlaps with valid bits. The helper would clear the overlapping valid bits and return a smaller value, which would falsely fail the test.
Also, could this approach break BPF CO-RE binary forward compatibility?
If a test compiled against older headers hardcodes the old BPF_SOCK_OPS_ALL_CB_FLAGS + 1 value into the BPF object, and runs on a newer kernel that implements that bit as a valid flag, the kernel will accept the flag and return 0 instead of the expected failure value.
Would using a statically unassigned high-order bit (e.g., 1 << 30) be a more robust way to test this failure path?
Quoted 3 lines
tools/testing/selftests/bpf/progs/test_tcpbpf_kern.c +0
-0/* Set callback */ global.good_cb_test_rv =bpf_sock_ops_cb_flags_set (skops,BPF_SOCK_OPS_STATE_CB_FLAG );
[ ... ]
Quoted 12 lines
tools/testing/selftests/bpf/prog_tests/tcpbpf_user.c +2
-1@@ -26,7 +26,8 @@ static void verify_result(struct tcpbpf_globals *result)ASSERT_EQ (result->bytes_acked,1002 ,"bytes_acked" );ASSERT_EQ (result->data_segs_in,1 ,"data_segs_in" );ASSERT_EQ (result->data_segs_out,1 ,"data_segs_out" ); -ASSERT_EQ (result->bad_cb_test_rv,0x80 ,"bad_cb_test_rv" ); +ASSERT_EQ (result->bad_cb_test_rv,BPF_SOCK_OPS_ALL_CB_FLAGS +1 , +"bad_cb_test_rv" );ASSERT_EQ (result->good_cb_test_rv,0 ,"good_cb_test_rv" );ASSERT_EQ (result->num_listen,1 ,"num_listen" );
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
[email protected]
AI reviewRe: [PATCH v2 bpf-next 01/11] selftest: bpf: Use BPF_SOCK_OPS_ALL_CB_FLAGS + 1 for bad_cb_test_rv.
In reply to Kuniyuki IwashimaCould BPF_SOCK_OPS_ALL_CB_FLAGS + 1 break on future kernels?
Quoted 14 lines
tools/testing/selftests/bpf/prog_tests/tcpbpf_user.c +2
-1@@ -26,7 +26,8 @@ static void verify_result(struct tcpbpf_globals *result)ASSERT_EQ (result->bytes_acked,1002 ,"bytes_acked" );ASSERT_EQ (result->data_segs_in,1 ,"data_segs_in" );ASSERT_EQ (result->data_segs_out,1 ,"data_segs_out" ); -ASSERT_EQ (result->bad_cb_test_rv,0x80 ,"bad_cb_test_rv" ); +ASSERT_EQ (result->bad_cb_test_rv,BPF_SOCK_OPS_ALL_CB_FLAGS +1 , +"bad_cb_test_rv" );ASSERT_EQ (result->good_cb_test_rv,0 ,"good_cb_test_rv" );ASSERT_EQ (result->num_listen,1 ,"num_listen" );
[ ... ]
tools/testing/selftests/bpf/progs/test_tcpbpf_kern.c int bpf_testcb(struct bpf_sock_ops *skops) · line 96
Quoted 11 lines
tools/testing/selftests/bpf/progs/test_tcpbpf_kern.c +2
-1@@ -92,7 +92,8 @@ int bpf_testcb(struct bpf_sock_ops *skops)break ;case BPF_SOCK_OPS_ACTIVE_ESTABLISHED_CB :/* Test failure to set largest cb flag (assumes not defined) */ - global.bad_cb_test_rv =bpf_sock_ops_cb_flags_set (skops,0x80 ); + global.bad_cb_test_rv =bpf_sock_ops_cb_flags_set (skops, +BPF_SOCK_OPS_ALL_CB_FLAGS +1 );
Could BPF_SOCK_OPS_ALL_CB_FLAGS + 1 break on future kernels?
When this test is compiled, BPF_SOCK_OPS_ALL_CB_FLAGS + 1 evaluates to a hardcoded value. If the compiled test runs on a future kernel where this value is supported, the helper will successfully set the flag and return 0, causing the test to fail.
The test also assumes contiguous bitmasks. If a flag is ever deprecated or skipped, adding 1 could overlap with valid bits.
Would a safely distant bit like 1 << 30 be more robust for forward compatibility?
(Reference: https://lore.kernel.org/bpf/[email protected]/)
tools/testing/selftests/bpf/progs/test_tcpbpf_kern.c int bpf_testcb(struct bpf_sock_ops *skops) · line 99
Quoted 3 lines
tools/testing/selftests/bpf/progs/test_tcpbpf_kern.c +0
-0/* Set callback */ global.good_cb_test_rv =bpf_sock_ops_cb_flags_set (skops,BPF_SOCK_OPS_STATE_CB_FLAG );
---
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/26275674572
Kuniyuki Iwashima
reviewerPATCH v2 2/11 bpf: tcp: Introduce BPF_SOCK_OPS_RCVQ_CB.
In reply to Kuniyuki IwashimaWe will introduce a new type of opt-in hooks for BPF SOCK_OPS prog.
The hooks can be enabled on per-socket basis by bpf_setsockopt():
int flag = BPF_SOCK_OPS_RCVQ_CB_FLAG;
bpf_setsockopt(sk, SOL_TCP, TCP_BPF_SOCK_OPS_CB_FLAGS,
&flags, sizeof(flags));or via the SOCK_OPS specific helper:
bpf_sock_ops_cb_flags_set(skops, BPF_SOCK_OPS_RCVQ_CB_FLAG);
Once activated, the BPF prog will be invoked with bpf_sock_ops.op set to BPF_SOCK_OPS_RCVQ_CB upon the following events:
1. TCP stack enqueues skb to sk->sk_receive_queue
2. TCP recvmsg() completes
This will allow the BPF prog to dynamically adjust sk->sk_rcvlowat, suppressing unnecessary EPOLLIN wakeups until sufficient data (e.g., a full RPC frame) is available in the receive queue.
Note that is_locked_tcp_sock_ops() is left unchanged not to enable bpf_setsockopt() unnecessarily, but bpf_sock_ops_cb_flags_set() is supported at BPF_SOCK_OPS_RCVQ_CB to disable by itself.
---
v2: s/BPF_SOCK_OPS_RCVLOWAT_CB/BPF_SOCK_OPS_RCVQ_CB/g
--- include/uapi/linux/bpf.h | 18 +++++++++++++++++- net/core/filter.c | 3 ++- tools/include/uapi/linux/bpf.h | 18 +++++++++++++++++- 3 files changed, 36 insertions(+), 3 deletions(-)
include/uapi/linux/bpf.h +17 -1
@@ -6960,6 +6960,9 @@ struct bpf_sock_ops {* the 3WHS. * BPF_SOCK_OPS_PASSIVE_ESTABLISHED_CB: The ACK that concludes * the 3WHS. +* BPF_SOCK_OPS_RCVQ_CB : No header included. The payload is only +* accessible by passing bpf_sock_ops to +* bpf_skb_load_bytes(). * * bpf_load_hdr_opt() can also be used to read a particular option. */
@@ -7031,8 +7034,16 @@ enum {* options first before the BPF program does. */ BPF_SOCK_OPS_WRITE_HDR_OPT_CB_FLAG = (1 <<6 ), +/* Call bpf when TCP payload is queued to sk->sk_receive_queue +* and after recvmsg(). The bpf prog will be called under +* sock_ops->op == BPF_SOCK_OPS_RCVQ_CB. +* +* It can be used to adjust sk->sk_rcvlowat and suppress +* unnecessary wakeups before sufficient data is available. +*/ +BPF_SOCK_OPS_RCVQ_CB_FLAG = (1 <<7 ),/* Mask of all currently supported cb flags */ - BPF_SOCK_OPS_ALL_CB_FLAGS = 0x7F, + BPF_SOCK_OPS_ALL_CB_FLAGS = 0xFF, }; enum {
@@ -7176,6 +7187,11 @@ enum {* sendmsg timestamp with corresponding * tskey. */ + BPF_SOCK_OPS_RCVQ_CB, /* Called when TCP payload is queued to +* sk->sk_receive_queue and after recvmsg() +* to allow adjusting sk->sk_rcvlowat and +* to suppress early wakeups. +*/ };/* List of TCP states. There is a build check in net/ipv4/tcp.c to detect
net/core/filter.c +2 -1
@@ -6002,7 +6002,8 @@ BPF_CALL_2(bpf_sock_ops_cb_flags_set, struct bpf_sock_ops_kern *, bpf_sock,struct sock *sk = bpf_sock->sk;int val = argval &BPF_SOCK_OPS_ALL_CB_FLAGS ; -if (!is_locked_tcp_sock_ops (bpf_sock)) +if (!is_locked_tcp_sock_ops (bpf_sock) && + bpf_sock->op !=BPF_SOCK_OPS_RCVQ_CB )return -EOPNOTSUPP ;if (!IS_ENABLED (CONFIG_INET ) || !sk_fullsock (sk))
tools/include/uapi/linux/bpf.h +17 -1
@@ -6960,6 +6960,9 @@ struct bpf_sock_ops {* the 3WHS. * BPF_SOCK_OPS_PASSIVE_ESTABLISHED_CB: The ACK that concludes * the 3WHS. +* BPF_SOCK_OPS_RCVQ_CB : No header included. The payload is only +* accessible by passing bpf_sock_ops to +* bpf_skb_load_bytes(). * * bpf_load_hdr_opt() can also be used to read a particular option. */
@@ -7031,8 +7034,16 @@ enum {* options first before the BPF program does. */ BPF_SOCK_OPS_WRITE_HDR_OPT_CB_FLAG = (1 <<6 ), +/* Call bpf when TCP payload is queued to sk->sk_receive_queue +* and after recvmsg(). The bpf prog will be called under +* sock_ops->op == BPF_SOCK_OPS_RCVQ_CB. +* +* It can be used to adjust sk->sk_rcvlowat and suppress +* unnecessary wakeups before sufficient data is available. +*/ +BPF_SOCK_OPS_RCVQ_CB_FLAG = (1 <<7 ),/* Mask of all currently supported cb flags */ - BPF_SOCK_OPS_ALL_CB_FLAGS = 0x7F, + BPF_SOCK_OPS_ALL_CB_FLAGS = 0xFF, }; enum {
@@ -7176,6 +7187,11 @@ enum {* sendmsg timestamp with corresponding * tskey. */ + BPF_SOCK_OPS_RCVQ_CB, /* Called when TCP payload is queued to +* sk->sk_receive_queue and after recvmsg() +* to allow adjusting sk->sk_rcvlowat and +* to suppress early wakeups. +*/ };/* List of TCP states. There is a build check in net/ipv4/tcp.c to detect
Signature · 1 line
2.54.0.746.g67dd491aae-goog
[email protected]
AI reviewRe: [PATCH v2 bpf-next 02/11] bpf: tcp: Introduce BPF_SOCK_OPS_RCVQ_CB.
The BPF subsystem requires multi-line comments to have the opening /* on its own line. This comment starts with text on the same line as /*:
Quoted 4 lines
include/uapi/linux/bpf.h +0
-0
[ ... ]
Quoted 6 lines
include/uapi/linux/bpf.h +2
-0@@ -7031,8 +7034,16 @@ enum {* options first before the BPF program does. */ BPF_SOCK_OPS_WRITE_HDR_OPT_CB_FLAG = (1<<6), +/* Call bpf when TCP payload is queued to sk->sk_receive_queue +* and after recvmsg(). The bpf prog will be called under
include/uapi/linux/bpf.h enum { · line 7047
Quoted 10 lines
include/uapi/linux/bpf.h +7
-1+* sock_ops->op == BPF_SOCK_OPS_RCVQ_CB. +* +* It can be used to adjust sk->sk_rcvlowat and suppress +* unnecessary wakeups before sufficient data is available. +*/ +BPF_SOCK_OPS_RCVQ_CB_FLAG = (1 <<7 ),/* Mask of all currently supported cb flags */ - BPF_SOCK_OPS_ALL_CB_FLAGS = 0x7F, + BPF_SOCK_OPS_ALL_CB_FLAGS = 0xFF, };
The BPF subsystem requires multi-line comments to have the opening /* on its own line. This comment starts with text on the same line as /*:
/* Call bpf when TCP payload is queued to sk->sk_receive_queue
Should it be formatted as:
/*
* Call bpf when TCP payload is queued to sk->sk_receive_queue
* and after recvmsg(). The bpf prog will be called under
* sock_ops->op == BPF_SOCK_OPS_RCVQ_CB.
*
* It can be used to adjust sk->sk_rcvlowat and suppress
* unnecessary wakeups before sufficient data is available. */
[ ... ]
include/uapi/linux/bpf.h enum { · line 7195
Quoted 10 lines
include/uapi/linux/bpf.h +5
-0@@ -7176,6 +7187,11 @@ enum {* sendmsg timestamp with corresponding * tskey. */ + BPF_SOCK_OPS_RCVQ_CB, /* Called when TCP payload is queued to +* sk->sk_receive_queue and after recvmsg() +* to allow adjusting sk->sk_rcvlowat and +* to suppress early wakeups. +*/ };
The multi-line comment here also starts with text on the same line as /*:
BPF_SOCK_OPS_RCVQ_CB, /* Called when TCP payload is queued to
Should it be reformatted with the opening /* on its own line?
Quoted 14 lines
net/core/filter.c +2
-1@@ -6002,7 +6002,8 @@ BPF_CALL_2(bpf_sock_ops_cb_flags_set, struct bpf_sock_ops_kern *, bpf_sock,struct sock *sk = bpf_sock->sk;int val = argval &BPF_SOCK_OPS_ALL_CB_FLAGS ; -if (!is_locked_tcp_sock_ops (bpf_sock)) +if (!is_locked_tcp_sock_ops (bpf_sock) && + bpf_sock->op !=BPF_SOCK_OPS_RCVQ_CB )return -EOPNOTSUPP ;if (!IS_ENABLED (CONFIG_INET ) || !sk_fullsock (sk))
net/core/filter.c
Quoted 5 lines
net/core/filter.c +0
-0return -EINVAL ;tcp_sk (sk)->bpf_sock_ops_cb_flags = val;return argval & (~BPF_SOCK_OPS_ALL_CB_FLAGS );
The patch allows bpf_sock_ops_cb_flags_set() to modify tcp_sk(sk)->bpf_sock_ops_cb_flags without holding the socket lock when bpf_sock->op == BPF_SOCK_OPS_RCVQ_CB. However, the same field can also be modified through bpf_setsockopt() which requires the socket lock.
Can there be a race between concurrent modifications?
When a BPF program in BPF_SOCK_OPS_RCVQ_CB context (unlocked) calls bpf_sock_ops_cb_flags_set():
bpf_sock_ops_cb_flags_set() tcp_sk(sk)->bpf_sock_ops_cb_flags = val;
Another BPF program in a locked context could simultaneously call bpf_setsockopt():
do_tcp_setsockopt() sock_owned_by_me(sk); ... tp->bpf_sock_ops_cb_flags = flags;
Both perform full writes (= val), not atomic operations. The tcp_sock.bpf_sock_ops_cb_flags field is a plain u8, not atomic_t, and no READ_ONCE/WRITE_ONCE is used. While byte writes are atomic on most architectures, the lack of synchronization means one write can completely overwrite the other.
Would this lead to incorrect callback flag state if two updates race?
tools/include/uapi/linux/bpf.h
Quoted 4 lines
tools/include/uapi/linux/bpf.h +0
-0
[ ... ]
---
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/26275674572
Kuniyuki Iwashima
reviewerRe: [PATCH v2 bpf-next 02/11] bpf: tcp: Introduce BPF_SOCK_OPS_RCVQ_CB.
Quoted 19 lines · On Fri, May 22, 2026 at 1:22 AM <[email protected]> wrote:
Quoted 5 lines
include/uapi/linux/bpf.h +0
-0[ ... ]
Quoted 11 lines
include/uapi/linux/bpf.h +7
-0@@ -7031,8 +7034,16 @@ enum {* options first before the BPF program does. */ BPF_SOCK_OPS_WRITE_HDR_OPT_CB_FLAG = (1 <<6 ), +/* Call bpf when TCP payload is queued to sk->sk_receive_queue +* and after recvmsg(). The bpf prog will be called under +* sock_ops->op == BPF_SOCK_OPS_RCVQ_CB. +* +* It can be used to adjust sk->sk_rcvlowat and suppress +* unnecessary wakeups before sufficient data is available. + */
Quoted 8 lines
include/uapi/linux/bpf.h enum { · line 7047
Quoted 5 lines
include/uapi/linux/bpf.h +2
-1+ q = (1 <<7 ),/* Mask of all currently supported cb flags */ - BPF_SOCK_OPS_ALL_CB_FLAGS = 0x7F, + BPF_SOCK_OPS_ALL_CB_FLAGS = 0xFF, };The BPF subsystem requires multi-line comments to have the opening /* on its own line. This comment starts with text on the same line as /*:
All SOCK_OPS comments are written in the netdev style.
Quoted 16 lines
Quoted 16 lines
net/core/filter.c +2
-1@@ -6002,7 +6002,8 @@ BPF_CALL_2(bpf_sock_ops_cb_flags_set, struct bpf_sock_ops_kern *, bpf_sock,struct sock *sk = bpf_sock->sk;int val = argval &BPF_SOCK_OPS_ALL_CB_FLAGS ; -if (!is_locked_tcp_sock_ops (bpf_sock)) +if (!is_locked_tcp_sock_ops (bpf_sock) && + bpf_sock->op !=BPF_SOCK_OPS_RCVQ_CB )return -EOPNOTSUPP ;if (!IS_ENABLED (CONFIG_INET ) || !sk_fullsock (sk))return -EINVAL;
Quoted 7 lines
Quoted 3 lines
tcp_sk(sk)->bpf_sock_ops_cb_flags = val; return argval & (~BPF_SOCK_OPS_ALL_CB_FLAGS);The patch allows
bpf_sock_ops_cb_flags_set()to modify tcp_sk(sk)->bpf_sock_ops_cb_flags without holding the socket lock whenbpf_sock->op==BPF_SOCK_OPS_RCVQ_CB.
This is clearly wrong. BPF_SOCK_OPS_RCVQ_CB is under lock_sock() or bh_lock_sock().
[email protected]
AI reviewRe: [PATCH v2 bpf-next 02/11] bpf: tcp: Introduce BPF_SOCK_OPS_RCVQ_CB.
In reply to Kuniyuki IwashimaThank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Low] BPF subsystem multi-line comments must have the opening `/*` on its own line. --
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Low] BPF subsystem multi-line comments must have the opening /* on its own line. --
commit 43e79a11591d79f0e01d647cd7fa7c248226e7b6
Author: Kuniyuki Iwashima <[email protected]>
bpf: tcp: Introduce BPF_SOCK_OPS_RCVQ_CB.
This commit introduces a new BPF sock_ops hook, BPF_SOCK_OPS_RCVQ_CB, which is invoked when a TCP payload is queued and after recvmsg() completes. It can be enabled via bpf_setsockopt() or bpf_sock_ops_cb_flags_set() and allows dynamic adjustment of sk_rcvlowat to suppress early wakeups.
include/uapi/linux/bpf.h struct bpf_sock_ops { · line 6963
Quoted 9 lines
include/uapi/linux/bpf.h +1
-0@@ -6960,6 +6960,9 @@ struct bpf_sock_ops {* the 3WHS. * BPF_SOCK_OPS_PASSIVE_ESTABLISHED_CB: The ACK that concludes * the 3WHS. +* BPF_SOCK_OPS_RCVQ_CB : No header included. The payload is only
[Severity: Low]
This isn't a bug, but does this comment align with the preferred BPF multi-line comment style? The BPF subsystem generally expects the opening /* on its own line before the comment text begins.
Quoted 5 lines
include/uapi/linux/bpf.h +2
-0+* accessible by passing bpf_sock_ops to +* bpf_skb_load_bytes(). * * bpf_load_hdr_opt() can also be used to read a particular option. */
[ ... ]
include/uapi/linux/bpf.h enum { · line 7037
Quoted 5 lines
include/uapi/linux/bpf.h +1
-0@@ -7031,8 +7034,16 @@ enum {* options first before the BPF program does. */ BPF_SOCK_OPS_WRITE_HDR_OPT_CB_FLAG = (1<<6), +/* Call bpf when TCP payload is queued to sk->sk_receive_queue
[Severity: Low]
This isn't a bug, but does this multi-line comment also need its opening /* on a separate line to match the BPF style guidelines?
Quoted 7 lines
include/uapi/linux/bpf.h +7
-0+* and after recvmsg(). The bpf prog will be called under +* sock_ops->op == BPF_SOCK_OPS_RCVQ_CB. +* +* It can be used to adjust sk->sk_rcvlowat and suppress +* unnecessary wakeups before sufficient data is available. +*/ + BPF_SOCK_OPS_RCVQ_CB_FLAG = (1<<7),
[ ... ]
include/uapi/linux/bpf.h enum { · line 7190
Quoted 5 lines
include/uapi/linux/bpf.h +1
-0@@ -7176,6 +7187,11 @@ enum {* sendmsg timestamp with corresponding * tskey. */ + BPF_SOCK_OPS_RCVQ_CB, /* Called when TCP payload is queued to
[Severity: Low]
This isn't a bug, but could this multi-line comment be adjusted so the opening /* is on its own line?
Quoted 5 lines
include/uapi/linux/bpf.h +4
-0+* sk->sk_receive_queue and after recvmsg() +* to allow adjusting sk->sk_rcvlowat and +* to suppress early wakeups. +*/ };
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
Kuniyuki Iwashima
reviewerPATCH v2 3/11 bpf: tcp: Support bpf_skb_load_bytes() for BPF_SOCK_OPS_RCVQ_CB.
In reply to Kuniyuki IwashimaWhen a TCP skb is queued to sk->sk_receive_queue, BPF SOCK_OPS prog can be called with BPF_SOCK_OPS_RCVQ_CB.
In this hook, we want to parse the RPC descriptor in the skb and adjust sk->sk_rcvlowat based on the RPC frame size.
However, we cannot access payload via bpf_sock_ops.data on modern NICs with TCP header/data split on as the payload is not placed in the linear area.
Let's support bpf_skb_load_bytes() for BPF_SOCK_OPS_RCVQ_CB.
Three notes:
1) bpf_sock_ops_kern.skb will be NULL when the BPF prog is invoked from recvmsg().
2) Access to bpf_sock_ops.data will be disabled by passing 0 end_offset to bpf_skops_init_skb().
3) ____bpf_skb_load_bytes() is called directly instead of __bpf_skb_load_bytes() to allow compilers to inline it instead of generating a tail-call.
---
v2: Explain why using ____ version instead of __
--- net/core/filter.c | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+)
net/core/filter.c +34 -0
@@ -7760,6 +7760,38 @@ static const struct bpf_func_proto bpf_sk_assign_proto = { .arg3_type = ARG_ANYTHING, }; +BPF_CALL_4(bpf_sock_ops_skb_load_bytes, struct bpf_sock_ops_kern *, bpf_sock, + u32, offset, void *, to, u32, len) +{ +int err; + +if (bpf_sock->op !=BPF_SOCK_OPS_RCVQ_CB ) { + err = -EOPNOTSUPP ; + goto err_clear; + } + +if (!bpf_sock->skb) { + err = -EPERM ; + goto err_clear; + } + +return ____bpf_skb_load_bytes (bpf_sock->skb, offset, to, len); + +err_clear: +memset (to,0 , len); +return err; +} + +static const struct bpf_func_proto bpf_sock_ops_skb_load_bytes_proto = { + .func = bpf_sock_ops_skb_load_bytes, + .gpl_only = false, + .ret_type =RET_INTEGER , + .arg1_type =ARG_PTR_TO_CTX , + .arg2_type =ARG_ANYTHING , + .arg3_type =ARG_PTR_TO_UNINIT_MEM , + .arg4_type =ARG_CONST_SIZE , +}; +static const u8 *bpf_search_tcp_opt (const u8 *op,const u8 *opend,u8 search_kind,const u8 *magic,u8 magic_len,bool *eol)
@@ -8616,6 +8648,8 @@ sock_ops_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)case BPF_FUNC_get_netns_cookie:return &bpf_get_netns_cookie_sock_ops_proto;#ifdef CONFIG_INET +case BPF_FUNC_skb_load_bytes: +return &bpf_sock_ops_skb_load_bytes_proto;case BPF_FUNC_load_hdr_opt:return &bpf_sock_ops_load_hdr_opt_proto;case BPF_FUNC_store_hdr_opt:
Signature · 1 line
2.54.0.746.g67dd491aae-goog
Kuniyuki Iwashima
reviewerPATCH v2 4/11 tcp: Split out __tcp_set_rcvlowat().
In reply to Kuniyuki IwashimaWe will add a kfunc for BPF_SOCK_OPS_RCVQ_CB hooks to adjust sk->sk_rcvlowat.
These hooks will be triggered when:
1. TCP stack enqueues skb to sk->sk_receive_queue
2. TCP recvmsg() completes
In the enqueue path, tcp_data_ready() is always called after the hooks in tcp_queue_rcv() and tcp_ofo_queue().
If tcp_set_rcvlowat() were used as is, tcp_data_ready() could be called twice for the same skb, which is redundant and also confusing.
Let's split out __tcp_set_rcvlowat() and add a flag to control wakeup behaviour.
--- include/net/tcp.h | 1 + net/ipv4/tcp.c | 12 +++++++++--- 2 files changed, 10 insertions(+), 3 deletions(-)
include/net/tcp.h +1 -0
@@ -515,6 +515,7 @@ void tcp_set_keepalive(struct sock *sk, int val);void tcp_syn_ack_timeout (const struct request_sock *req);int tcp_recvmsg (struct sock *sk,struct msghdr *msg,size_t len,int flags); +int __tcp_set_rcvlowat (struct sock *sk,int val,bool wakeup);int tcp_set_rcvlowat (struct sock *sk,int val);void tcp_set_rcvbuf (struct sock *sk,int val);int tcp_set_window_clamp (struct sock *sk,int val);
net/ipv4/tcp.c +9 -3
@@ -1829,8 +1829,7 @@ int tcp_peek_len(struct socket *sock)return tcp_inq (sock->sk); } -/* Make sure sk_rcvbuf is big enough to satisfy SO_RCVLOWAT hint */ -int tcp_set_rcvlowat (struct sock *sk,int val) +int __tcp_set_rcvlowat (struct sock *sk,int val,bool wakeup) {struct tcp_sock *tp =tcp_sk (sk);int space, cap;
@@ -1843,7 +1842,8 @@ int tcp_set_rcvlowat(struct sock *sk, int val)WRITE_ONCE (sk->sk_rcvlowat, val ? :1 );/* Check if we need to signal EPOLLIN right now */ -tcp_data_ready (sk); +if (wakeup) +tcp_data_ready (sk);if (sk->sk_userlocks &SOCK_RCVBUF_LOCK )return 0 ;
@@ -1858,6 +1858,12 @@ int tcp_set_rcvlowat(struct sock *sk, int val)return 0 ; } +/* Make sure sk_rcvbuf is big enough to satisfy SO_RCVLOWAT hint */ +int tcp_set_rcvlowat (struct sock *sk,int val) +{ +return __tcp_set_rcvlowat (sk, val, true); +} +void tcp_set_rcvbuf (struct sock *sk,int val) {tcp_set_window_clamp (sk,tcp_win_from_space (sk, val));
Signature · 1 line
2.54.0.746.g67dd491aae-goog
Kuniyuki Iwashima
reviewerPATCH v2 5/11 bpf: tcp: Add kfunc to adjust sk->sk_rcvlowat.
In reply to Kuniyuki IwashimaWe will invoke BPF SOCK_OPS prog with BPF_SOCK_OPS_RCVQ_CB to adjust sk->sk_rcvlowat when
1. TCP stack enqueues skb to sk->sk_receive_queue
2. TCP recvmsg() completes
Let's provide a kfunc to set sk->sk_rcvlowat.
Negative values are clamped to INT_MAX, consistent with SO_RCVLOWAT.
The wakeup flag is determined based on bpf_sock_ops_kern.skb:
* For the enqueue hook, skb is always non-NULL, and wakeup is set to false because
* tcp_data_ready() is always called after the hooks in tcp_queue_rcv() and tcp_ofo_queue().
* when tcp_fastopen_add_skb() is called for TFO SYN, the socket is not yet accept()ed, and when called for TFO SYN+ACK, the socket is woken up by sk->sk_state_change() anyway.
* For the recvmsg() hook, skb is always NULL, and wakeup is set to true because tcp_data_ready() is not called in the path.
An alternative would be to support bpf_setsockopt() by adding BPF_SOCK_OPS_RCVQ_CB to is_locked_tcp_sock_ops().
However, that approach involves excessive conditionals and an unnecessary memcpy(), costs we do not want to pay for every skb in the TCP fast path.
--- net/core/filter.c | 17 +++++++++++++++++ 1 file changed, 17 insertions(+)
net/core/filter.c +17 -0
@@ -12388,6 +12388,22 @@ __bpf_kfunc int bpf_sk_assign_tcp_reqsk(struct __sk_buff *s, struct sock *sk,#endif } +__bpf_kfunc int bpf_sock_ops_tcp_set_rcvlowat(struct bpf_sock_ops_kern *skops, +int rcvlowat) +{ +#ifdef CONFIG_INET +if (skops->op !=BPF_SOCK_OPS_RCVQ_CB ) +return -EOPNOTSUPP ; + +if (rcvlowat <0 ) + rcvlowat =INT_MAX ; + +return __tcp_set_rcvlowat (skops->sk, rcvlowat, !skops->skb); +#else +return -EOPNOTSUPP ; +#endif +} + __bpf_kfunc int bpf_sock_ops_enable_tx_tstamp(struct bpf_sock_ops_kern *skops,u64 flags) {
@@ -12539,6 +12555,7 @@ BTF_KFUNCS_END(bpf_kfunc_check_set_tcp_reqsk)BTF_KFUNCS_START (bpf_kfunc_check_set_sock_ops)BTF_ID_FLAGS (func ,bpf_sock_ops_enable_tx_tstamp ) +BTF_ID_FLAGS (func, bpf_sock_ops_tcp_set_rcvlowat)BTF_KFUNCS_END (bpf_kfunc_check_set_sock_ops) static const struct btf_kfunc_id_set bpf_kfunc_set_skb = {
Signature · 1 line
2.54.0.746.g67dd491aae-goog
[email protected]
AI reviewRe: [PATCH v2 bpf-next 05/11] bpf: tcp: Add kfunc to adjust sk->sk_rcvlowat.
Could this reproduce the MPTCP subflow issue from commit fcf4692fa39e ("mptcp: prevent BPF accessing lowat from a subflow socket.")?
Quoted 9 lines
net/core/filter.c +1
-0@@ -12388,6 +12388,22 @@ __bpf_kfunc int bpf_sk_assign_tcp_reqsk(struct __sk_buff *s, struct sock *sk,#endif } +__bpf_kfunc int bpf_sock_ops_tcp_set_rcvlowat(struct bpf_sock_ops_kern *skops,
net/core/filter.c __bpf_kfunc int bpf_sk_assign_tcp_reqsk(struct __sk_buff *s, struct sock *sk, · line 12401
Quoted 10 lines
net/core/filter.c +10
-0+ int rcvlowat) +{ +#ifdef CONFIG_INET +if (skops->op !=BPF_SOCK_OPS_RCVQ_CB ) +return -EOPNOTSUPP ; + +if (rcvlowat <0 ) + rcvlowat =INT_MAX ; + +return __tcp_set_rcvlowat (skops->sk, rcvlowat, !skops->skb);
Could this reproduce the MPTCP subflow issue from commit fcf4692fa39e ("mptcp: prevent BPF accessing lowat from a subflow socket.")?
In the v1 review, Björn Töpel asked whether BPF programs could call this kfunc on MPTCP subflow sockets. The response was "I'll rather drop support for MPTCP for now," but there's no check here to prevent this kfunc from being called on MPTCP subflows.
Without a check like:
if (sk_is_mptcp(skops->sk))
return -EOPNOTSUPP;or a protocol check, this code could trigger the same warning that fcf4692fa39e was designed to prevent.
net/core/filter.c __bpf_kfunc int bpf_sk_assign_tcp_reqsk(struct __sk_buff *s, struct sock *sk, · line 12409
Quoted 8 lines
net/core/filter.c +5
-0+#else +return -EOPNOTSUPP ; +#endif +} + __bpf_kfunc int bpf_sock_ops_enable_tx_tstamp(struct bpf_sock_ops_kern *skops,u64 flags) {
[ ... ]
---
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/26275674572
Kuniyuki Iwashima
reviewerRe: [PATCH v2 bpf-next 05/11] bpf: tcp: Add kfunc to adjust sk->sk_rcvlowat.
Quoted 9 lines · On Fri, May 22, 2026 at 1:22 AM <[email protected]> wrote:
Quoted 9 lines
net/core/filter.c +0
-0@@ -12388,6 +12388,22 @@ __bpf_kfunc int bpf_sk_assign_tcp_reqsk(struct __sk_buff *s, struct sock *sk,#endif }
Quoted 14 lines
net/core/filter.c __bpf_kfunc int bpf_sk_assign_tcp_reqsk(struct __sk_buff *s, struct sock *sk, · line 12401
Quoted 11 lines
net/core/filter.c +11
-0+__bpf_kfunc int bpf_sock_ops_tcp_set_rcvlowat(struct bpf_sock_ops_kern *skops, +int rcvlowat) +{ +#ifdef CONFIG_INET +if (skops->op !=BPF_SOCK_OPS_RCVQ_CB ) +return -EOPNOTSUPP ; + +if (rcvlowat <0 ) + rcvlowat =INT_MAX ; + +return __tcp_set_rcvlowat (skops->sk, rcvlowat, !skops->skb);Could this reproduce the MPTCP subflow issue from commit
fcf4692fa39e("mptcp: prevent BPF accessing lowat from a subflow socket.")?
At this point, this kfunc cannot be called since there is no hook placed yet.
Also, a subsequent patch disables BPF_SOCK_OPS_RCVQ_CB for MPTCP subflow.
Quoted 31 lines
In the v1 review, Björn Töpel asked whether BPF programs could call this kfunc on MPTCP subflow sockets. The response was "I'll rather drop support for MPTCP for now," but there's no check here to prevent this kfunc from being called on MPTCP subflows.
Without a check like:
if (sk_is_mptcp(skops->sk)) return -EOPNOTSUPP;or a protocol check, this code could trigger the same warning that
fcf4692fa39ewas designed to prevent.net/core/filter.c __bpf_kfunc int bpf_sk_assign_tcp_reqsk(struct __sk_buff *s, struct sock *sk, · line 12409
Quoted 8 lines
net/core/filter.c +5
-0+#else +return -EOPNOTSUPP ; +#endif +} + __bpf_kfunc int bpf_sock_ops_enable_tx_tstamp(struct bpf_sock_ops_kern *skops,u64 flags) {[ ... ]
---
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.mdCI run summary: https://github.com/kernel-patches/bpf/actions/runs/26275674572
Kuniyuki Iwashima
reviewerPATCH v2 6/11 bpf: tcp: Make BPF_SOCK_OPS_RCVQ_CB and SOCKMAP mutually exclusive.
In reply to Kuniyuki IwashimaBoth BPF_SOCK_OPS_RCVQ_CB and SOCKMAP can intercept and handle socket receive queues, leading to overlapping use cases.
While BPF_SOCK_OPS_RCVQ_CB focuses on optimizing single-socket performance by reducing EPOLLIN wakeups and fully preserves TCP zerocopy support, SOCKMAP is designed to facilitate multi-socket routing at the cost of higher overhead and no zerocopy support.
Enabling both features on the same socket makes no sense and results in unexpected interference between them.
For instance, SOCKMAP calls __tcp_cleanup_rbuf(), where we will add a BPF_SOCK_OPS_RCVQ_CB hook, and bpf_sock_ops_tcp_set_rcvlowat() calls sk->sk_data_ready(), which would trigger SOCKMAP.
Let's make BPF_SOCK_OPS_RCVQ_CB and SOCKMAP mutually exclusive.
Both bpf_sol_tcp_setsockopt(TCP_BPF_SOCK_OPS_CB_FLAGS) and bpf_sock_ops_cb_flags_set() now check if sk->sk_prot is &tcp_prot or tcpv6_prot, while tcp_bpf_update_proto() checks if BPF_SOCK_OPS_RCVQ_CB_FLAG is already set. Both checks are performed under lock_sock().
--- net/core/filter.c | 29 +++++++++++++++++++++++++++-- net/ipv4/tcp_bpf.c | 2 ++ 2 files changed, 29 insertions(+), 2 deletions(-)
net/core/filter.c +27 -2
@@ -5382,12 +5382,27 @@ static int bpf_sol_tcp_getsockopt(struct sock *sk, int optname,return 0 ; } +static int bpf_sock_ops_check_rcvq_cb (struct sock *sk,int val) +{ +if (val &BPF_SOCK_OPS_RCVQ_CB_FLAG ) { +bool not_tcp_prot = sk->sk_prot != &tcp_prot; + +#if IS_ENABLED (CONFIG_IPV6 ) + not_tcp_prot &= sk->sk_prot != &tcpv6_prot; +#endif +if (not_tcp_prot) +return -EBUSY ; + } + +return 0 ; +} +static int bpf_sol_tcp_setsockopt (struct sock *sk,int optname,char *optval,int optlen) {struct tcp_sock *tp =tcp_sk (sk);unsigned long timeout; -int val; +int val, err;if (optlen !=sizeof (int ))return -EINVAL ;
@@ -5424,6 +5439,11 @@ static int bpf_sol_tcp_setsockopt(struct sock *sk, int optname,case TCP_BPF_SOCK_OPS_CB_FLAGS :if (val & ~(BPF_SOCK_OPS_ALL_CB_FLAGS ))return -EINVAL ; + + err =bpf_sock_ops_check_rcvq_cb (sk, val); +if (err) +return err; + tp->bpf_sock_ops_cb_flags = val;break ;default :
@@ -5999,8 +6019,9 @@ static const struct bpf_func_proto bpf_sock_ops_getsockopt_proto = { BPF_CALL_2(bpf_sock_ops_cb_flags_set, struct bpf_sock_ops_kern *, bpf_sock, int, argval) { -struct sock *sk = bpf_sock->sk;int val = argval &BPF_SOCK_OPS_ALL_CB_FLAGS ; +struct sock *sk = bpf_sock->sk; +int err;if (!is_locked_tcp_sock_ops (bpf_sock) && bpf_sock->op !=BPF_SOCK_OPS_RCVQ_CB )
@@ -6009,6 +6030,10 @@ BPF_CALL_2(bpf_sock_ops_cb_flags_set, struct bpf_sock_ops_kern *, bpf_sock,if (!IS_ENABLED (CONFIG_INET ) || !sk_fullsock (sk))return -EINVAL ; + err =bpf_sock_ops_check_rcvq_cb (sk, val); +if (err) +return err; +tcp_sk (sk)->bpf_sock_ops_cb_flags = val;return argval & (~BPF_SOCK_OPS_ALL_CB_FLAGS );
net/ipv4/tcp_bpf.c +2 -0
@@ -729,6 +729,8 @@ int tcp_bpf_update_proto(struct sock *sk, struct sk_psock *psock, bool restore)sock_replace_proto (sk, psock->sk_proto); }return 0 ; + }else if (BPF_SOCK_OPS_TEST_FLAG (tcp_sk (sk),BPF_SOCK_OPS_RCVQ_CB_FLAG )) { +return -EBUSY ; }if (sk->sk_family ==AF_INET6 ) {
Signature · 1 line
2.54.0.746.g67dd491aae-goog
[email protected]
AI reviewRe: [PATCH v2 bpf-next 06/11] bpf: tcp: Make BPF_SOCK_OPS_RCVQ_CB and SOCKMAP mutually exclusive.
Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider:
Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider:
New issues:
- [High] Using sk->sk_prot != &tcp_prot to detect SOCKMAP incorrectly blocks other features (like KTLS and MPTCP) and introduces an order-of-operations bypass.
- [High] The mutual exclusion check is broken due to a TOCTOU race because setsockopt and SOCKMAP map updates use different synchronization primitives.
- [Medium] Lockless double read of sk->sk_prot without READ_ONCE() leads to a data race.
Pre-existing issues:
- [High] A failed map update for an existing psock leaves newly attached BPF programs active in psock->progs. --
commit 5e984d100188fbad217fad5406d80666b989f1b9
Author: Kuniyuki Iwashima <[email protected]>
bpf: tcp: Make BPF_SOCK_OPS_RCVQ_CB and SOCKMAP mutually exclusive.
Both BPF_SOCK_OPS_RCVQ_CB and SOCKMAP can intercept and handle socket receive queues, leading to overlapping use cases.
While BPF_SOCK_OPS_RCVQ_CB focuses on optimizing single-socket performance by reducing EPOLLIN wakeups and fully preserves TCP zerocopy support, SOCKMAP is designed to facilitate multi-socket routing at the cost of higher overhead and no zerocopy support.
Quoted 5 lines
net/core/filter.c +0
-0
net/core/filter.c static int bpf_sol_tcp_getsockopt(struct sock *sk, int optname, · line 5391
Quoted 10 lines
net/core/filter.c +7
-0@@ -5382,12 +5382,27 @@ static int bpf_sol_tcp_getsockopt(struct sock *sk, int optname,return 0 ; } +static int bpf_sock_ops_check_rcvq_cb (struct sock *sk,int val) +{ +if (val &BPF_SOCK_OPS_RCVQ_CB_FLAG ) { +bool not_tcp_prot = sk->sk_prot != &tcp_prot; + +#if IS_ENABLED (CONFIG_IPV6 ) + not_tcp_prot &= sk->sk_prot != &tcpv6_prot;
[Severity: High]
Does this check falsely block other features like KTLS and MPTCP?
Since features like KTLS also override sk->sk_prot, if KTLS is attached first, the check would falsely reject enabling BPF_SOCK_OPS_RCVQ_CB_FLAG with -EBUSY. Conversely, if the BPF flag is set first, the check passes, and a subsequent KTLS attachment would blindly overwrite sk->sk_prot, allowing both features to operate simultaneously and bypassing the mutual exclusion.
[Severity: Medium]
Could this double read of sk->sk_prot result in a data race?
Since sk->sk_prot can be concurrently modified by sock_map_update_elem(), which only holds bh_lock_sock() instead of lock_sock(), the compiler may emit two separate loads without READ_ONCE(). A concurrent modification between the two loads could cause inconsistent evaluation.
Quoted 7 lines
net/core/filter.c +7
-0+#endif +if (not_tcp_prot) +return -EBUSY ; + } + +return 0 ; +}
[ ... ]
net/core/filter.c static int bpf_sol_tcp_setsockopt(struct sock *sk, int optname, · line 5446
Quoted 9 lines
net/core/filter.c +5
-0@@ -5424,6 +5439,11 @@ static int bpf_sol_tcp_setsockopt(struct sock *sk, int optname,case TCP_BPF_SOCK_OPS_CB_FLAGS :if (val & ~(BPF_SOCK_OPS_ALL_CB_FLAGS ))return -EINVAL ; + + err =bpf_sock_ops_check_rcvq_cb (sk, val); +if (err) +return err; +
[Severity: High]
The commit message asserts that checks in setsockopt and SOCKMAP map updates are performed under lock_sock(), but can a TOCTOU race occur here because they use different synchronization primitives?
While bpf_sol_tcp_setsockopt() uses lock_sock(sk), BPF helpers updating SOCKMAP (like bpf_map_update_elem called from BPF_PROG_TYPE_XDP or generic map updates via sock_map_update_elem()) only acquire the spinlock bh_lock_sock(sk) and disable bottom halves. Because bh_lock_sock() does not wait for lock_sock()'s sk_lock.owned flag, these operations can run concurrently.
A concurrent SOCKMAP update could check tp->bpf_sock_ops_cb_flags (seeing 0) and override sk->sk_prot while the user's setsockopt is validating sk->sk_prot and subsequently setting tp->bpf_sock_ops_cb_flags. This allows both features to be successfully attached to the same socket.
Quoted 3 lines
net/core/filter.c +0
-0tp->bpf_sock_ops_cb_flags = val;break ;default :
[ ... ]
net/ipv4/tcp_bpf.c int tcp_bpf_update_proto(struct sock *sk, struct sk_psock *psock, bool restore) · line 734
Quoted 11 lines
net/ipv4/tcp_bpf.c +2
-0@@ -729,6 +729,8 @@ int tcp_bpf_update_proto(struct sock *sk, struct sk_psock *psock, bool restore)sock_replace_proto (sk, psock->sk_proto); }return 0 ; + }else if (BPF_SOCK_OPS_TEST_FLAG (tcp_sk (sk),BPF_SOCK_OPS_RCVQ_CB_FLAG )) { +return -EBUSY ; }
[Severity: High]
This is a pre-existing issue, but does a failed map update for an existing psock leave newly attached BPF programs active in psock->progs?
When adding new programs (e.g., stream_parser) to a socket that already has a psock in sock_map_link(), the program pointers are updated immediately via psock_set_prog(). If a subsequent initialization step like sk_psock_init_strp() fails, the function aborts and calls sk_psock_put().
However, sk_psock_put() only drops the temporary refcount acquired earlier in the function and does not revert the psock->progs changes. As a result, the newly attached BPF programs remain permanently active on the socket despite the map update failing.
Quoted 2 lines
net/ipv4/tcp_bpf.c +0
-0if (sk->sk_family ==AF_INET6 ) {
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=6
Kuniyuki Iwashima
reviewerRe: [PATCH v2 bpf-next 06/11] bpf: tcp: Make BPF_SOCK_OPS_RCVQ_CB and SOCKMAP mutually exclusive.
Quoted 44 lines · On Fri, May 22, 2026 at 1:46 AM <[email protected]> wrote:
Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider:
New issues:
- [High] Usingsk->sk_prot != &tcp_protto detect SOCKMAP incorrectly blocks other features (like KTLS and MPTCP) and introduces an order-of-operations bypass.
- [High] The mutual exclusion check is broken due to a TOCTOU race becausesetsockoptand SOCKMAP map updates use different synchronization primitives.
- [Medium] Lockless double read ofsk->sk_protwithoutREAD_ONCE()leads to a data race.Pre-existing issues:
- [High] A failed map update for an existingpsockleaves newly attached BPF programs active inpsock->progs. --commit
5e984d100188fbad217fad5406d80666b989f1b9
Author: Kuniyuki Iwashima <[email protected]>bpf: tcp: Make
BPF_SOCK_OPS_RCVQ_CBand SOCKMAP mutually exclusive.Both
BPF_SOCK_OPS_RCVQ_CBand SOCKMAP can intercept and handle socket receive queues, leading to overlapping use cases.While
BPF_SOCK_OPS_RCVQ_CBfocuses on optimizing single-socket performance by reducing EPOLLIN wakeups and fully preserves TCP zerocopy support, SOCKMAP is designed to facilitate multi-socket routing at the cost of higher overhead and no zerocopy support.Quoted 5 lines
net/core/filter.c +0
-0net/core/filter.c static int bpf_sol_tcp_getsockopt(struct sock *sk, int optname, · line 5391
Quoted 10 lines
net/core/filter.c +7
-0@@ -5382,12 +5382,27 @@ static int bpf_sol_tcp_getsockopt(struct sock *sk, int optname,return 0 ; } +static int bpf_sock_ops_check_rcvq_cb (struct sock *sk,int val) +{ +if (val &BPF_SOCK_OPS_RCVQ_CB_FLAG ) { +bool not_tcp_prot = sk->sk_prot != &tcp_prot; + +#if IS_ENABLED (CONFIG_IPV6 ) + not_tcp_prot &= sk->sk_prot != &tcpv6_prot;[Severity: High]
Does this check falsely block other features like KTLS and MPTCP?
Quoted 6 lines
Since features like KTLS also override
sk->sk_prot, if KTLS is attached first, the check would falsely reject enablingBPF_SOCK_OPS_RCVQ_CB_FLAGwith-EBUSY. Conversely, if the BPF flag is set first, the check passes, and a subsequent KTLS attachment would blindly overwritesk->sk_prot, allowing both features to operate simultaneously and bypassing the mutual exclusion.
Disabling ktls is okay, but I'll check if sk->sk_prot is in tcp_bpf_prots.
tcp_bpf_prots < sk->sk_prot < &tcp_bpf_prots[TCP_BPF_NUM_PROTS][TCP_BPF_NUM_CFGS]
Quoted 15 lines
net/core/filter.c static int bpf_sol_tcp_setsockopt(struct sock *sk, int optname, · line 5446
Quoted 9 lines
net/core/filter.c +5
-0@@ -5424,6 +5439,11 @@ static int bpf_sol_tcp_setsockopt(struct sock *sk, int optname,case TCP_BPF_SOCK_OPS_CB_FLAGS :if (val & ~(BPF_SOCK_OPS_ALL_CB_FLAGS ))return -EINVAL ; + + err =bpf_sock_ops_check_rcvq_cb (sk, val); +if (err) +return err; +[Severity: High]
The commit message asserts that checks in setsockopt and SOCKMAP map updates are performed underlock_sock(), but can a TOCTOU race occur here because they use different synchronization primitives?
Quoted 4 lines
While
bpf_sol_tcp_setsockopt()useslock_sock(sk), BPF helpers updating SOCKMAP (likebpf_map_update_elemcalled fromBPF_PROG_TYPE_XDPor generic map updates viasock_map_update_elem()) only acquire the spinlockbh_lock_sock(sk)and disable bottom halves.
Hmm.. I'll protect bpf_sock_ops_cb_flags with sk_callback_lock.
Quoted 15 lines
Because
bh_lock_sock()does not
wait for lock_sock()'s sk_lock.owned flag, these operations can run concurrently.A concurrent SOCKMAP update could check
tp->bpf_sock_ops_cb_flags(seeing 0) and overridesk->sk_protwhile the user's setsockopt is validatingsk->sk_protand subsequently settingtp->bpf_sock_ops_cb_flags. This allows both features to be successfully attached to the same socket.Quoted 3 lines
net/core/filter.c +0
-0tp->bpf_sock_ops_cb_flags = val;break ;default :[ ... ]
Quoted 15 lines
net/ipv4/tcp_bpf.c int tcp_bpf_update_proto(struct sock *sk, struct sk_psock *psock, bool restore) · line 734
Quoted 11 lines
net/ipv4/tcp_bpf.c +2
-0@@ -729,6 +729,8 @@ int tcp_bpf_update_proto(struct sock *sk, struct sk_psock *psock, bool restore)sock_replace_proto (sk, psock->sk_proto); }return 0 ; + }else if (BPF_SOCK_OPS_TEST_FLAG (tcp_sk (sk),BPF_SOCK_OPS_RCVQ_CB_FLAG )) { +return -EBUSY ; }[Severity: High]
This is a pre-existing issue, but does a failed map update for an existing psock leave newly attached BPF programs active inpsock->progs?
Orthogonal to this series.
Quoted 16 lines
When adding new programs (e.g.,
stream_parser) to a socket that already has a psock insock_map_link(), the program pointers are updated immediately viapsock_set_prog(). If a subsequent initialization step likesk_psock_init_strp()fails, the function aborts and callssk_psock_put().However,
sk_psock_put()only drops the temporary refcount acquired earlier in the function and does not revert thepsock->progschanges. As a result, the newly attached BPF programs remain permanently active on the socket despite the map update failing.net/ipv4/tcp_bpf.c int tcp_bpf_update_proto(struct sock *sk, struct sk_psock *psock, bool restore) · line 736
Quoted 2 lines
net/ipv4/tcp_bpf.c +0
-0if (sk->sk_family ==AF_INET6 ) {--
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=6
Kuniyuki Iwashima
reviewerPATCH v2 7/11 bpf: mptcp: Don't support BPF_SOCK_OPS_RCVQ_CB.
In reply to Kuniyuki IwashimaMPTCP has its own sock->ops->set_rcvlowat() / mptcp_set_rcvlowat().
We should not allow calling __tcp_set_rcvlowat() for MPTCP subflows.
Let's disable BPF_SOCK_OPS_RCVQ_CB for MPTCP for now.
If needed in the future, bpf_sock_ops_tcp_set_rcvlowat() could be extended to properly support MPTCP.
--- net/core/filter.c | 3 +++ 1 file changed, 3 insertions(+)
net/core/filter.c +3 -0
@@ -5392,6 +5392,9 @@ static int bpf_sock_ops_check_rcvq_cb(struct sock *sk, int val)#endif if (not_tcp_prot)return -EBUSY ; + +if (unlikely (sk_is_mptcp (sk))) +return -EOPNOTSUPP ; }return 0 ;
Signature · 1 line
2.54.0.746.g67dd491aae-goog
Kuniyuki Iwashima
reviewerPATCH v2 8/11 bpf: tcp: Reject BPF_SOCK_OPS_RCVQ_CB if receive queue is not empty.
In reply to Kuniyuki IwashimaUnlike SOCKMAP, BPF_SOCK_OPS_RCVQ_CB does not iterate existing skbs in the receive queue when it is enabled for the first time.
In practical production use cases, this behavior is usually not a problem.
We can safely assume that the upper-layer protocol is designed with specific synchronisation points where the connection is temporarily quiet.
At these points, the application can completely drain the receive queue and safely enable BPF_SOCK_OPS_RCVQ_CB while no skbs are pending.
A prime example is an application transitioning from HTTP to an RPC protocol:
Client Server
| |
| --- HTTP Upgrade request ---------> |
| | [Drain all skbs]
| | [Enable BPF_SOCK_OPS_RCVQ_CB]
| <-- HTTP 200/Switching protocol --- |
| |
| --- RPC Frame 1 ------------------> |However, to strictly prevent any potential race conditions arising from unconventional upper-layer protocol designs, let's explicitly signal a failure if BPF_SOCK_OPS_RCVQ_CB is enabled while the receive queue is not empty.
-EUCLEAN is chosen to indicate that the caller needs to clean up the receive queue before enabling the feature.
--- net/core/filter.c | 3 +++ 1 file changed, 3 insertions(+)
net/core/filter.c +3 -0
@@ -5395,6 +5395,9 @@ static int bpf_sock_ops_check_rcvq_cb(struct sock *sk, int val)if (unlikely (sk_is_mptcp (sk)))return -EOPNOTSUPP ; + +if (!skb_queue_empty (&sk->sk_receive_queue)) +return -EUCLEAN ; }return 0 ;
Signature · 1 line
2.54.0.746.g67dd491aae-goog
[email protected]
AI reviewRe: [PATCH v2 bpf-next 08/11] bpf: tcp: Reject BPF_SOCK_OPS_RCVQ_CB if receive queue is not empty.
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] bpf_sock_ops_check_rcvq_cb incorrectly prevents orthogonal flag updates if…
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] bpf_sock_ops_check_rcvq_cb incorrectly prevents orthogonal flag updates if BPF_SOCK_OPS_RCVQ_CB_FLAG is already enabled. --
commit 684ea4455b7f13af338937adb28643e76ddccaac
Author: Kuniyuki Iwashima <[email protected]>
bpf: tcp: Reject BPF_SOCK_OPS_RCVQ_CB if receive queue is not empty.
This commit modifies BPF_SOCK_OPS_RCVQ_CB flag initialization to explicitly reject enabling it if the socket's receive queue is not empty. It returns -EUCLEAN to indicate that the queue must be cleared before the feature can be enabled.
net/core/filter.c static int bpf_sock_ops_check_rcvq_cb(struct sock *sk, int val) · line 5400
Quoted 11 lines
net/core/filter.c +3
-0@@ -5395,6 +5395,9 @@ static int bpf_sock_ops_check_rcvq_cb(struct sock *sk, int val)if (unlikely (sk_is_mptcp (sk)))return -EOPNOTSUPP ; + +if (!skb_queue_empty (&sk->sk_receive_queue)) +return -EUCLEAN ;
[Severity: High]
Does this check prevent orthogonal flag updates if BPF_SOCK_OPS_RCVQ_CB_FLAG is already enabled?
Because the val parameter represents the new absolute bitmask for bpf_sock_ops_cb_flags, any update to orthogonal flags (such as adding BPF_SOCK_OPS_STATE_CB_FLAG) must include BPF_SOCK_OPS_RCVQ_CB_FLAG in val to keep it enabled.
If such an update occurs while data is in the receive queue, it seems this will unconditionally fail with -EUCLEAN. This might break the ability to dynamically update socket flags, especially from within the BPF_SOCK_OPS_RCVQ_CB hook itself, where the receive queue is inherently non-empty.
Could we verify if the flag is actually being newly enabled before returning the error?
Quoted 3 lines
net/core/filter.c +0
-0}return 0 ;
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=8
Kuniyuki Iwashima
reviewerRe: [PATCH v2 bpf-next 08/11] bpf: tcp: Reject BPF_SOCK_OPS_RCVQ_CB if receive queue is not empty.
Quoted 36 lines · On Fri, May 22, 2026 at 2:18 AM <[email protected]> wrote:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High]bpf_sock_ops_check_rcvq_cbincorrectly prevents orthogonal flag updates ifBPF_SOCK_OPS_RCVQ_CB_FLAGis already enabled. --commit
684ea4455b7f13af338937adb28643e76ddccaac
Author: Kuniyuki Iwashima <[email protected]>bpf: tcp: Reject
BPF_SOCK_OPS_RCVQ_CBif receive queue is not empty.This commit modifies
BPF_SOCK_OPS_RCVQ_CBflag initialization to explicitly reject enabling it if the socket's receive queue is not empty. It returns-EUCLEANto indicate that the queue must be cleared before the feature can be enabled.net/core/filter.c static int bpf_sock_ops_check_rcvq_cb(struct sock *sk, int val) · line 5400
Quoted 11 lines
net/core/filter.c +3
-0@@ -5395,6 +5395,9 @@ static int bpf_sock_ops_check_rcvq_cb(struct sock *sk, int val)if (unlikely (sk_is_mptcp (sk)))return -EOPNOTSUPP ; + +if (!skb_queue_empty (&sk->sk_receive_queue)) +return -EUCLEAN ;[Severity: High]
Does this check prevent orthogonal flag updates ifBPF_SOCK_OPS_RCVQ_CB_FLAGis already enabled?Because the val parameter represents the new absolute bitmask for
bpf_sock_ops_cb_flags, any update to orthogonal flags (such as addingBPF_SOCK_OPS_STATE_CB_FLAG) must includeBPF_SOCK_OPS_RCVQ_CB_FLAGin val to keep it enabled.
Quoted 8 lines
If such an update occurs while data is in the receive queue, it seems this will unconditionally fail with
-EUCLEAN. This might break the ability to dynamically update socket flags, especially from within theBPF_SOCK_OPS_RCVQ_CBhook itself, where the receive queue is inherently non-empty.Could we verify if the flag is actually being newly enabled before returning the error?
Makes sense. I'll apply this check only when BPF_SOCK_OPS_RCVQ_CB_FLAG is not flagged yet.
Quoted 7 lines
net/core/filter.c static int bpf_sock_ops_check_rcvq_cb(struct sock *sk, int val) · line 5403
Quoted 4 lines
net/core/filter.c +0
-0}return 0 ;--
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=8
Kuniyuki Iwashima
reviewerPATCH v2 9/11 bpf: tcp: Factorise bpf_skops_established().
In reply to Kuniyuki IwashimaWe will call BPF SOCK_OPS prog with BPF_SOCK_OPS_RCVQ_CB.
It requires a similar setup to bpf_skops_established(), and the only difference is the skb data length.
Let's factor out the common logic into bpf_skops_common_locked().
--- net/ipv4/tcp_input.c | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-)
net/ipv4/tcp_input.c +11 -4
@@ -179,8 +179,9 @@ static void bpf_skops_parse_hdr(struct sock *sk, struct sk_buff *skb)BPF_CGROUP_RUN_PROG_SOCK_OPS (&sock_ops); } -static void bpf_skops_established (struct sock *sk,int bpf_op, -struct sk_buff *skb) +static void bpf_skops_common_locked (struct sock *sk,int bpf_op, +struct sk_buff *skb, +unsigned int end_offset) {struct bpf_sock_ops_kern sock_ops;
@@ -191,12 +192,18 @@ static void bpf_skops_established(struct sock *sk, int bpf_op, sock_ops.is_fullsock =1 ; sock_ops.is_locked_tcp_sock =1 ; sock_ops.sk = sk; -/* sk with TCP_REPAIR_ON does not have skb in tcp_finish_connect */ if (skb) -bpf_skops_init_skb (&sock_ops, skb,tcp_hdrlen (skb)); +bpf_skops_init_skb (&sock_ops, skb, end_offset); BPF_CGROUP_RUN_PROG_SOCK_OPS(&sock_ops); } + +static void bpf_skops_established (struct sock *sk,int bpf_op, +struct sk_buff *skb) +{ +/* sk with TCP_REPAIR_ON does not have skb in tcp_finish_connect */ +bpf_skops_common_locked (sk, bpf_op, skb, skb ?tcp_hdrlen (skb) :0 ); +}#else static void bpf_skops_parse_hdr (struct sock *sk,struct sk_buff *skb) {
Signature · 1 line
2.54.0.746.g67dd491aae-goog
Kuniyuki Iwashima
reviewerPATCH v2 10/11 bpf: tcp: Add SOCK_OPS rcvlowat hook.
In reply to Kuniyuki IwashimaNow, it is time to add the new hooks for BPF_SOCK_OPS_RCVQ_CB.
Let's invoke the BPF SOCK_OPS prog when
1. TCP stack enqueues skb to sk->sk_receive_queue
-> tcp_queue_rcv(), tcp_ofo_queue(), and tcp_fastopen_add_skb()
2. TCP recvmsg() completes
-> __tcp_cleanup_rbuf()
This will allow the BPF prog to parse each skb and dynamically adjust sk->sk_rcvlowat to suppress unnecessary EPOLLIN wakeups until sufficient data (e.g., a full RPC frame) is available in the receive queue.
Note that the direct access to bpf_sock_ops.data is intentionally disabled by passing 0 as end_offset.
Instead, the BPF prog is supposed to use bpf_skb_load_bytes() with bpf_sock_ops because payload is not in the linear area with TCP header/data split on and skb may contain a RPC descriptor in skb frag. This also simplifies the BPF prog.
The placement of tcp_bpf_rcvlowat() in tcp_ofo_queue() and tcp_fastopen_add_skb() is chosen to provide the same snapshot with tcp_queue_rcv().
For example, if tcp_bpf_rcvlowat() were called before updating TCP_SKB_CB(skb)->seq in tcp_fastopen_add_skb(), BPF prog would need to implement an unlikely if branch to strip SYN.
In addition, TCP stack can queue overlapping skb into recvq. Once rcv_nxt is updated with a new skb, BPF prog cannot infer the previous one from skb->len.
---
v2: Add explanation of tcp_bpf_rcvlowat() placement.
--- include/net/tcp.h | 12 ++++++++++++ net/ipv4/tcp.c | 2 ++ net/ipv4/tcp_fastopen.c | 2 ++ net/ipv4/tcp_input.c | 10 ++++++++++ 4 files changed, 26 insertions(+)
include/net/tcp.h +12 -0
@@ -2888,12 +2888,24 @@ static inline void bpf_skops_init_skb(struct bpf_sock_ops_kern *skops, skops->skb = skb; skops->skb_data_end = skb->data + end_offset; } + +void bpf_skops_rcvlowat (struct sock *sk,struct sk_buff *skb); + +static inline void tcp_bpf_rcvlowat (struct sock *sk,struct sk_buff *skb) +{ +if (BPF_SOCK_OPS_TEST_FLAG (tcp_sk (sk),BPF_SOCK_OPS_RCVQ_CB_FLAG )) +bpf_skops_rcvlowat (sk, skb); +}#else static inline void bpf_skops_init_skb (struct bpf_sock_ops_kern *skops,struct sk_buff *skb,unsigned int end_offset) { } + +static inline void tcp_bpf_rcvlowat (struct sock *sk,struct sk_buff *skb) +{ +}#endif /* Call BPF_SOCK_OPS program that returns an int. If the return value
net/ipv4/tcp.c +2 -0
@@ -1602,6 +1602,8 @@ void __tcp_cleanup_rbuf(struct sock *sk, int copied)tcp_mstamp_refresh (tp);tcp_send_ack (sk); } + +tcp_bpf_rcvlowat (sk,NULL ); }void tcp_cleanup_rbuf (struct sock *sk,int copied)
net/ipv4/tcp_fastopen.c +2 -0
@@ -281,6 +281,8 @@ void tcp_fastopen_add_skb(struct sock *sk, struct sk_buff *skb)TCP_SKB_CB (skb)->seq++;TCP_SKB_CB (skb)->tcp_flags &= ~TCPHDR_SYN ; +tcp_bpf_rcvlowat (sk, skb); + tp->rcv_nxt =TCP_SKB_CB (skb)->end_seq;tcp_add_receive_queue (sk, skb); tp->syn_data_acked =1 ;
net/ipv4/tcp_input.c +10 -0
@@ -204,6 +204,12 @@ static void bpf_skops_established(struct sock *sk, int bpf_op,/* sk with TCP_REPAIR_ON does not have skb in tcp_finish_connect */ bpf_skops_common_locked(sk, bpf_op, skb, skb ? tcp_hdrlen(skb) : 0); } + +void bpf_skops_rcvlowat (struct sock *sk,struct sk_buff *skb) +{ +/* skb is NULL when called from __tcp_cleanup_rbuf(). */ +bpf_skops_common_locked (sk,BPF_SOCK_OPS_RCVQ_CB , skb,0 ); +}#else static void bpf_skops_parse_hdr (struct sock *sk,struct sk_buff *skb) {
@@ -5306,6 +5312,8 @@ static void tcp_ofo_queue(struct sock *sk)continue ; } +tcp_bpf_rcvlowat (sk, skb); + tail =skb_peek_tail (&sk->sk_receive_queue); eaten = tail &&tcp_try_coalesce (sk, tail, skb, &fragstolen);tcp_rcv_nxt_update (tp,TCP_SKB_CB (skb)->end_seq);
@@ -5509,6 +5517,8 @@ static int __must_check tcp_queue_rcv(struct sock *sk, struct sk_buff *skb,int eaten;struct sk_buff *tail =skb_peek_tail (&sk->sk_receive_queue); +tcp_bpf_rcvlowat (sk, skb); + eaten = (tail &&tcp_try_coalesce (sk, tail, skb, fragstolen)) ?1 :0 ;
Signature · 1 line
2.54.0.746.g67dd491aae-goog
Kuniyuki Iwashima
reviewerPATCH v2 11/11 selftest: bpf: Add test for BPF_SOCK_OPS_RCVQ_CB.
In reply to Kuniyuki IwashimaThe test is roughly divided into two stages, and the sequence is as follows:
I) Setup
1. Attach two BPF programs to a cgroup
2. Establish a TCP connection (@client <-> @child) within the cgroup
3. Enable BPF_SOCK_OPS_RCVQ_CB on @child
II) RPC frame exchange in various patterns
4. Send a partial RPC descriptor from @client to @child
5. Verify that epoll does NOT wake up @child
6. Send the remaining data of the RPC frame
7. Verify that epoll finally wakes up @child
During setup, two BPF programs are attached to simulate a real-world scenario; one is SOCK_OPS and the other is CGROUP_SOCKOPT.
While the SOCK_OPS prog handles the dynamic adjustment of sk->sk_rcvlowat, the CGROUP_SOCKOPT prog is used to enable BPF_SOCK_OPS_RCVQ_CB via userspace setsockopt() using pseudo options:
#define SOL_BPF 0xdeadbeef
#define BPF_TCP_AUTOLOWAT 0x8badf00d
setsockopt(fd, SOL_BPF, BPF_TCP_AUTOLOWAT, &(int){1}, sizeof(int));This reflects a common production use case where an application decides to start parsing RPC frames only at a certain point in the stream (e.g., after HTTP Upgrade), rather than immediately after TCP 3WHS (BPF_SOCK_OPS_PASSIVE_ESTABLISHED_CB, etc).
When BPF_TCP_AUTOLOWAT is enabled, the BPF prog initialises sk_local_storage for two sequence numbers to manage its state.
Then, for the RPC frame exchange, this test uses a simple format defined as follows
0 8 16 24 32
+--------+--------+-------+--------+ `. | header size | | +--------+--------+-------+--------+ > RPC descriptor (8 bytes) | payload size | | +--------+--------+-------+--------+ .' ~ header ~ +--------+--------+-------+--------+ ~ payload ~ +--------+--------+-------+--------+
Every time a new skb is enqueued to sk->sk_receive_queue, the SOCK_OPS prog parses it and updates these sequence numbers:
rpc_desc_seq : the SEQ # of the start of the RPC descriptor rpc_end_seq : the SEQ # of the end of the RPC frame
=> rpc_desc_seq + 8 + header size + payload size
Assume we receive two RPC descriptors in the following pattern:
1. When we receive skb-1, only a part of RPC descriptor is parsed. rpc_desc_seq is set to the first byte while rpc_end_seq is unknown. Thus, sk->sk_rcvlowat is set to the size of the RPC descriptor (8 bytes).
<- skb-1 -> <---- skb-2 ----> <------ skb-3 ----->
+-----------+.................+....................+...... | RPC desc 1 | header + payload | RPC desc 2 | ... +-----------+.................+....................+...... ^ ^-. `- rpc_desc_seq `- sk->sk_rcvlowat
2. Next, we receive skb-2, which completes the first RPC descriptor.
Now rpc_end_seq is known, so sk->sk_rcvlowat is advanced to it.
<- skb-1 -> <---- skb-2 ----> <------ skb-3 ----->
+-----------+-----------------+....................+......
| RPC desc 1 | header + payload | RPC desc 2 | ...
+-----------+-----------------+....................+......
^ ^
'- rpc_desc_seq '- rpc_end_seq
& sk->sk_rcvlowat 3. Once we receive skb-3, which contains the next full RPC descriptor, rpc_desc_seq is advanced and rpc_end_seq is updated according to the size of RPC frame 2.
Note that sk->sk_rcvlowat is NOT updated to the new rpc_end_seq yet. This ensures that the application is woken up to read the already complete RPC frame 1.
<- skb-1 -> <---- skb-2 ----> <------ skb-3 ----->
+-----------+-----------------+--------------------+......
| RPC desc 1 | header + payload | RPC desc 2 | ... |
+-----------+-----------------+--------------------+......
^ ^
rpc_desc_seq -----------' rpc_end_seq ----...-'
& sk->sk_rcvlowatThis sequence corresponds to the 4th test case in rpc_test_cases[], and we can see helpful output if we "#define DEBUG":
# cat /sys/kernel/tracing/trace_pipe | \
awk '{ if ($0 ~ /AF_/) sub(/^.*AF_/, "AF_"); print $0 }' & \
BGPID=$!; ./test_progs -t tcp_autolowat; kill -9 -$BGPID
...
AF_INET6 rpc_test_cases[3]: Start parsing skb: seq: 0, end_seq: 1, len: 1, rpc_desc_seq: 0, rpc_end_seq: 0, rpc_buff_len: 0
AF_INET6 rpc_test_cases[3]: Copied 1 bytes: rpc_desc_buff_len: 1
AF_INET6 rpc_test_cases[3]: Setting rcvlowat: tp->copied_seq: 0, rpc_desc_seq: 0, rpc_end_seq: 0, rpc_desc_buff_len: 1
AF_INET6 rpc_test_cases[3]: Set rcvlowat: expected: 8, actual: 8 AF_INET6 rpc_test_cases[3]: Start parsing skb: seq: 1, end_seq: 8, len: 7, rpc_desc_seq: 0, rpc_end_seq: 0, rpc_buff_len: 1 AF_INET6 rpc_test_cases[3]: Copied full descriptor: rpc_desc_seq: 0, rpc_end_seq: 258, header_len: 100, payload_len: 150 AF_INET6 rpc_test_cases[3]: No more descriptor: rpc_end_seq: 258, end_seq: 8
AF_INET6 rpc_test_cases[3]: Setting rcvlowat: tp->copied_seq: 0, rpc_desc_seq: 0, rpc_end_seq: 258, rpc_desc_buff_len: 8 AF_INET6 rpc_test_cases[3]: Set rcvlowat: expected: 258, actual: 258
...
---
v2:
* Make copy_len u64 and swap validation order for it
for no_alu32. --- tools/testing/selftests/bpf/bpf_kfuncs.h | 4 + .../selftests/bpf/prog_tests/tcp_autolowat.c | 350 ++++++++++++++++++ .../selftests/bpf/progs/bpf_tracing_net.h | 2 + .../selftests/bpf/progs/tcp_autolowat.c | 326 ++++++++++++++++ 4 files changed, 682 insertions(+) create mode 100644 tools/testing/selftests/bpf/prog_tests/tcp_autolowat.c create mode 100644 tools/testing/selftests/bpf/progs/tcp_autolowat.c
tools/testing/selftests/bpf/bpf_kfuncs.h +4 -0
@@ -64,6 +64,10 @@ struct bpf_tcp_req_attrs; extern int bpf_sk_assign_tcp_reqsk(struct __sk_buff *skb, struct sock *sk, struct bpf_tcp_req_attrs *attrs, int attrs__sz) __ksym; +struct bpf_sock_ops_kern ; +extern int bpf_sock_ops_tcp_set_rcvlowat(struct bpf_sock_ops_kern *skops_kern, + int rcvlowat) __ksym; +void *bpf_cast_to_kern_ctx (void *) __ksym;extern void *bpf_rdonly_cast (const void *obj,__u32 btf_id) __ksym__weak ;
tools/testing/selftests/bpf/prog_tests/tcp_autolowat.c new file +350 -0
350 lines not on this page · raw message
tools/testing/selftests/bpf/progs/bpf_tracing_net.h +2 -0
@@ -74,6 +74,8 @@#define NEXTHDR_TCP 6 +#define TCPHDR_FIN 0x01 +#define TCPOPT_NOP 1#define TCPOPT_EOL 0#define TCPOPT_MSS 2
tools/testing/selftests/bpf/progs/tcp_autolowat.c new file +326 -0
326 lines not on this page · raw message
Signature · 1 line
2.54.0.746.g67dd491aae-goog
[email protected]
AI reviewRe: [PATCH v2 bpf-next 11/11] selftest: bpf: Add test for BPF_SOCK_OPS_RCVQ_CB.
Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider: - [High] The BPF program sets SO_RCVLOWAT to the total combined size of all unread frames when multiple RPC…
Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider:
- [High] The BPF program sets SO_RCVLOWAT to the total combined size of all unread frames when multiple RPC frames are received in a single skb, causing the application to hang when reading frames sequentially.
- [Medium] Test case 4 incorrectly assumes that calling recv() triggers the BPF program to update SO_RCVLOWAT, which causes a failing assertion.
- [Medium] The selftest assumes synchronous packet delivery over the loopback interface, leading to a race condition where epoll_wait and getsockopt checks occur before the TCP stack and BPF program have processed the packet.
- [Low] Multi-line comments in BPF code do not have the opening '/*' on its own line.
--
commit e3c63e65707f68d5d1680cea7733be1620b9d094
Author: Kuniyuki Iwashima <[email protected]>
selftest: bpf: Add test for BPF_SOCK_OPS_RCVQ_CB.
This commit adds a BPF selftest for BPF_SOCK_OPS_RCVQ_CB, simulating a production scenario where an application uses a BPF program to dynamically adjust SO_RCVLOWAT for RPC frame parsing.
Quoted 5 lines
tools/testing/selftests/bpf/prog_tests/tcp_autolowat.c new file +0
-0
[ ... ]
Quoted 5 lines
tools/testing/selftests/bpf/prog_tests/tcp_autolowat.c new file +5
-0+struct rpc_test_case { +char data[4096 ]; +struct rpc_descriptor desc[32 ]; +struct rpc_event event[32 ]; +} rpc_test_cases[] = {
[ ... ]
Quoted 6 lines
tools/testing/selftests/bpf/prog_tests/tcp_autolowat.c new file +6
-0+ { + .desc = { + {.header_len = 100, .payload_len = 150}, + {.header_len = 200, .payload_len = 500}, + }, + .event = {
[ ... ]
tools/testing/selftests/bpf/prog_tests/tcp_autolowat.c line 119
Quoted 4 lines
tools/testing/selftests/bpf/prog_tests/tcp_autolowat.c new file +4
-0+/* After reading the first RPC message, SO_RCVLOWAT should be RPC_DESC_SIZE. */ + {.type = RPC_EVENT_RECV, .len = RPC_DESC_SIZE + 150 + 100}, + {.type = RPC_EVENT_EPOLL, .nfds = 0}, + {.type = RPC_EVENT_RCVLOWAT, .rcvlowat = RPC_DESC_SIZE},
[Severity: Medium]
Does calling recv() actually trigger the BPF program to update SO_RCVLOWAT?
Since the kernel only invokes BPF_SOCK_OPS_RCVQ_CB when a new skb is added to the receive queue, and not when data is dequeued via recv(), the BPF program doesn't seem to run here.
Could this cause the test to fail unconditionally because sk_rcvlowat retains its previous value?
[ ... ]
Quoted 3 lines
tools/testing/selftests/bpf/prog_tests/tcp_autolowat.c new file +3
-0+static void tcp_autolowat_run_rpc_test (struct tcp_autolowat_test_cb *cb, +struct rpc_test_case *test_case) +{
[ ... ]
Quoted 2 lines
tools/testing/selftests/bpf/prog_tests/tcp_autolowat.c new file +2
-0+while (1 ) { +switch (event->type) {
[ ... ]
Quoted 7 lines
tools/testing/selftests/bpf/prog_tests/tcp_autolowat.c new file +7
-0+case RPC_EVENT_SEND : + err =send (cb->client, ptr, event->len,0 ); +if (!ASSERT_EQ (err, event->len,"send" )) +return ; + + ptr += event->len; +break ;
[ ... ]
Quoted 4 lines
tools/testing/selftests/bpf/prog_tests/tcp_autolowat.c new file +4
-0+case RPC_EVENT_EPOLL : + err =epoll_wait (cb->epoll, &ev,1 ,0 ); +if (!ASSERT_EQ (err, event->nfds,"epoll_wait" )) +return ;
tools/testing/selftests/bpf/prog_tests/tcp_autolowat.c line 281
Quoted 10 lines
tools/testing/selftests/bpf/prog_tests/tcp_autolowat.c new file +10
-0+break ; +case RPC_EVENT_RCVLOWAT : + optval =0 ; + optlen =sizeof (optval); + + err =getsockopt (cb->child,SOL_SOCKET ,SO_RCVLOWAT , &optval, &optlen); +if (!ASSERT_OK (err,"getsockopt" ) || + !ASSERT_EQ (optval, event->rcvlowat,"rcvlowat" )) +return ; +break ;
[Severity: Medium]
Could this lead to race conditions causing flaky tests?
The test calls send() to transmit data and immediately executes the next event. Because network transmission over the loopback interface involves asynchronous softirq processing, send() might return before the packet is fully processed by the receiving socket.
If the next event is RPC_EVENT_EPOLL with a timeout of 0, could epoll_wait() prematurely return 0? Similarly, for RPC_EVENT_RCVLOWAT, could getsockopt() read a stale sk_rcvlowat value before the TCP stack and BPF program have finished processing the packet?
[ ... ]
Quoted 5 lines
tools/testing/selftests/bpf/progs/tcp_autolowat.c new file +0
-0
[ ... ]
Quoted 4 lines
tools/testing/selftests/bpf/progs/tcp_autolowat.c new file +4
-0+static int tcp_parse_descriptor (struct bpf_sock_ops *skops, +struct tcp_autolowat_cb *cb, +u32 seq,u32 end_seq) +{
[ ... ]
tools/testing/selftests/bpf/progs/tcp_autolowat.c line 79
Quoted 9 lines
tools/testing/selftests/bpf/progs/tcp_autolowat.c new file +9
-0+if (before (cb->rpc_desc_seq +RPC_DESC_SIZE , end_seq)) + copy_len =RPC_DESC_SIZE - cb->rpc_desc_buff_len; +else + copy_len = end_seq - rpc_copied_seq; + +/* Since LLVM commit 324e27e8bad83ca23a3cd276d7e2e729b1b0b8c7, +* clang omits the "copy_len == 0" check below, which is necessary +* to satisfy the BPF verifier's range check for bpf_skb_load_bytes(). +*/
[Severity: Low]
This isn't a bug, but multi-line comments in the BPF subsystem are expected to have the opening '/*' on its own line:
/* * Since LLVM commit ... * ... */
[ ... ]
tools/testing/selftests/bpf/progs/tcp_autolowat.c line 142
Quoted 12 lines
tools/testing/selftests/bpf/progs/tcp_autolowat.c new file +12
-0+static void tcp_set_autolowat (struct bpf_sock_ops_kern *skops_kern, +struct tcp_autolowat_cb *cb, +struct tcp_sock *tp) +{ +/* To handle wraparound. */ +u32 val =0 ; + +LOG ("Setting rcvlowat: tp->copied_seq: %u, rpc_desc_seq: %u, rpc_end_seq: %u, rpc_desc_buff_len: %u" , +TP_SEQ (copied_seq),CB_SEQ (rpc_desc_seq),CB_SEQ (rpc_end_seq), cb->rpc_desc_buff_len); + +if (before (tp->copied_seq, cb->rpc_desc_seq)) + val = cb->rpc_desc_seq - tp->copied_seq;
[Severity: High]
Does this code incorrectly set SO_RCVLOWAT to the total combined size of all unread frames when multiple RPC frames are received in a single skb?
If multiple complete frames arrive in the same skb, the parsing loop in tcp_do_autolowat() parses all of them and advances cb->rpc_desc_seq to the end of the last parsed frame. This sets:
val = cb->rpc_desc_seq - tp->copied_seq
which evaluates to the length of all unread frames combined.
If an application sequentially reads one frame at a time, tp->copied_seq advances and the queue size decreases, but sk_rcvlowat is not updated because BPF_SOCK_OPS_RCVQ_CB only fires on packet enqueue.
The remaining queue size will then be strictly less than sk_rcvlowat, causing epoll_wait to block and trapping the remaining fully-received frames in the receive queue.
Could this hang applications that rely on epoll to read one message at a time?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=11