Skip to contentSkip to conversation

bpf sched updates

PATCH Superseded Geliang Tang 2 participants 9 messages Jun 22

Subsystems (5)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Geliang Tang

author reviewer
#0 lore raw

v2 cover bpf sched updates

From: Geliang Tang <[email protected]>

v2:
- new patches 2-7, address Sashiko's comments to drop __ign in arguments of bpf_sk_stream_memory_free() and bpf_mptcp_subflow_ctx().

v1:
- Link: https://patchwork.kernel.org/project/mptcp/patch/10a30ed6484cc4d48009625f1ed8e12802e78e94.1781699193.git.tanggeliang@kylinos.cn/

This series addresses BPF verifier issues in the MPTCP packet scheduler helpers and their selftests.

The initial motivation was to fix an incorrect return value in bpf_sk_stream_memory_free(): the function returns bool but erroneously returned NULL. This was fixed by changing it to false and dropping the KF_RET_NULL flag.

However, further review revealed that both bpf_sk_stream_memory_free() and bpf_mptcp_subflow_ctx() used the __ign suffix on their pointer arguments to bypass verifier checks. This approach is unsafe because it allows untrusted pointers to be passed into kfuncs, potentially leading to verifier rejection or runtime issues. In practice, passing pointers like msk->first (which are not marked as trusted) to these kfuncs triggers the verifier error "R1 must be referenced or trusted".

To resolve this cleanly, the series:

- Removes bpf_sk_stream_memory_free() entirely, as its functionality can be implemented inline in the BPF scheduler (burst) using MPTCP-specific memory checks (msk->notsent_lowat and subflow send buffer).

- Removes the __ign suffix from bpf_mptcp_subflow_ctx() and updates all BPF schedulers (first, rr, burst) to avoid calling it. Instead, they use bpf_for_each(mptcp_subflow) iterations to obtain trusted subflow pointers directly, eliminating the need for reverse lookup from an untrusted sock pointer.

With these changes, all BPF scheduler tests pass verification and function correctly.

Geliang Tang (7):
Squash to "bpf: Export mptcp packet scheduler helpers" Squash to "bpf: Export mptcp packet scheduler helpers" 2 Squash to "selftests/bpf: Add bpf_burst scheduler & test" Squash to "bpf: Export mptcp packet scheduler helpers" 3 Squash to "selftests/bpf: Add bpf_first scheduler & test" Squash to "selftests/bpf: Add bpf_rr scheduler & test" Squash to "selftests/bpf: Add bpf_burst scheduler & test" 2

 net/mptcp/bpf.c                               | 16 +-----
 .../selftests/bpf/progs/mptcp_bpf_burst.c     | 53 +++++++++++++++----
 .../selftests/bpf/progs/mptcp_bpf_first.c     | 17 +++---
 .../selftests/bpf/progs/mptcp_bpf_rr.c        | 30 ++++++-----
 4 files changed, 71 insertions(+), 45 deletions(-)
Signature · 1 line
2.53.0
author reviewer
#1 lore raw

PATCH v2 1/7 Squash to "bpf: Export mptcp packet scheduler helpers"

From: Geliang Tang <[email protected]>

bpf_sk_stream_memory_free() returns bool but erroneously returned NULL. Change to false and drop the KF_RET_NULL flag.

---
 net/mptcp/bpf.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
net/mptcp/bpf.c +2 -2
index 08bb037f0951..259a2ca4fb0b 100644
@@ -297,7 +297,7 @@ __bpf_kfunc static bool bpf_sk_stream_memory_free(const struct sock *sk__ign)
 	    sk->sk_protocol == IPPROTO_TCP && sk_is_mptcp(sk))
 		return sk_stream_memory_free(sk);
 
-	return NULL;
+	return false;
 }
 
 __bpf_kfunc_end_defs();
@@ -320,7 +320,7 @@ BTF_ID_FLAGS(func, mptcp_subflow_set_scheduled)
 BTF_ID_FLAGS(func, mptcp_subflow_active)
 BTF_ID_FLAGS(func, mptcp_set_timeout)
 BTF_ID_FLAGS(func, mptcp_wnd_end)
-BTF_ID_FLAGS(func, bpf_sk_stream_memory_free, KF_RET_NULL)
+BTF_ID_FLAGS(func, bpf_sk_stream_memory_free)
 BTF_ID_FLAGS(func, bpf_mptcp_subflow_queues_empty)
 BTF_ID_FLAGS(func, mptcp_pm_subflow_chk_stale, KF_SLEEPABLE)
 BTF_KFUNCS_END(bpf_mptcp_common_kfunc_ids)
Signature · 1 line
2.53.0
author reviewer
#2 lore raw

PATCH v2 2/7 Squash to "bpf: Export mptcp packet scheduler helpers" 2

In reply to Geliang Tang

From: Geliang Tang <[email protected]>

Remove bpf_sk_stream_memory_free() kfunc entirely. The function is no longer used by any BPF scheduler, and its implementation will be moved inline into the burst scheduler to avoid verifier trust issues.

---
 net/mptcp/bpf.c | 12 ------------
 1 file changed, 12 deletions(-)
net/mptcp/bpf.c +0 -12
index 259a2ca4fb0b..938aa6399608 100644
@@ -289,17 +289,6 @@ __bpf_kfunc static bool bpf_mptcp_subflow_queues_empty(struct sock *sk)
 	return tcp_rtx_queue_empty(sk);
 }
 
-__bpf_kfunc static bool bpf_sk_stream_memory_free(const struct sock *sk__ign)
-{
-	const struct sock *sk = sk__ign;
-
-	if (sk && sk_fullsock(sk) &&
-	    sk->sk_protocol == IPPROTO_TCP && sk_is_mptcp(sk))
-		return sk_stream_memory_free(sk);
-
-	return false;
-}
-
 __bpf_kfunc_end_defs();
 
 BTF_KFUNCS_START(bpf_mptcp_iter_kfunc_ids)
@@ -320,7 +309,6 @@ BTF_ID_FLAGS(func, mptcp_subflow_set_scheduled)
 BTF_ID_FLAGS(func, mptcp_subflow_active)
 BTF_ID_FLAGS(func, mptcp_set_timeout)
 BTF_ID_FLAGS(func, mptcp_wnd_end)
-BTF_ID_FLAGS(func, bpf_sk_stream_memory_free)
 BTF_ID_FLAGS(func, bpf_mptcp_subflow_queues_empty)
 BTF_ID_FLAGS(func, mptcp_pm_subflow_chk_stale, KF_SLEEPABLE)
 BTF_KFUNCS_END(bpf_mptcp_common_kfunc_ids)
Signature · 1 line
2.53.0
author reviewer
#3 lore raw

PATCH v2 3/7 Squash to "selftests/bpf: Add bpf_burst scheduler & test"

In reply to Geliang Tang

From: Geliang Tang <[email protected]>

Implement sk_stream_memory_free() logic inline in bpf_burst_get_send. The original approach relied on the bpf_sk_stream_memory_free() kfunc, which is being removed. The new implementation:
- Checks subflow send buffer (sk_wmem_queued < sk_sndbuf)
- Checks MPTCP notsent_bytes against msk->notsent_lowat
- Follows the same semantics as the kernel's __sk_stream_memory_free()

---
 .../selftests/bpf/progs/mptcp_bpf_burst.c     | 33 +++++++++++++++++--
 1 file changed, 31 insertions(+), 2 deletions(-)
tools/testing/selftests/bpf/progs/mptcp_bpf_burst.c +31 -2
index 693fab55f7be..bd258776076c 100644
@@ -18,10 +18,39 @@ char _license[] SEC("license") = "GPL";
 extern bool mptcp_subflow_active(struct mptcp_subflow_context *subflow) __ksym;
 extern void mptcp_set_timeout(struct sock *sk) __ksym;
 extern __u64 mptcp_wnd_end(const struct mptcp_sock *msk) __ksym;
-extern bool bpf_sk_stream_memory_free(const struct sock *sk) __ksym;
 extern bool bpf_mptcp_subflow_queues_empty(struct sock *sk) __ksym;
 extern void mptcp_pm_subflow_chk_stale(const struct mptcp_sock *msk, struct sock *ssk) __ksym;
 
+static __always_inline u32 mptcp_notsent_lowat(struct mptcp_sock *msk)
+{
+	return msk->notsent_lowat ?: UINT_MAX;
+}
+
+static __always_inline bool mptcp_stream_memory_free(struct mptcp_sock *msk,
+						     int wake)
+{
+	u32 notsent_bytes;
+
+	notsent_bytes = msk->write_seq - msk->snd_nxt;
+	return (notsent_bytes << wake) < mptcp_notsent_lowat(msk);
+}
+
+static __always_inline bool __sk_stream_memory_free(struct mptcp_sock *msk,
+						    const struct sock *ssk,
+						    int wake)
+{
+	if (ssk->sk_wmem_queued >= ssk->sk_sndbuf)
+		return false;
+
+	return mptcp_stream_memory_free(msk, wake);
+}
+
+static __always_inline bool sk_stream_memory_free(struct mptcp_sock *msk,
+						  const struct sock *ssk)
+{
+	return __sk_stream_memory_free(msk, ssk, 0);
+}
+
 static __always_inline __u64 div_u64(__u64 dividend, __u32 divisor)
 {
 	return dividend / divisor;
@@ -96,7 +125,7 @@ int BPF_PROG(bpf_burst_get_send, struct mptcp_sock *msk)
 		send_info[SSK_MODE_ACTIVE].ssk = send_info[SSK_MODE_BACKUP].ssk;
 
 	ssk = send_info[SSK_MODE_ACTIVE].ssk;
-	if (!ssk || !bpf_sk_stream_memory_free(ssk))
+	if (!ssk || !sk_stream_memory_free(msk, ssk))
 		return -1;
 
 	subflow = bpf_mptcp_subflow_ctx(ssk);
Signature · 1 line
2.53.0
author reviewer
#4 lore raw

PATCH v2 4/7 Squash to "bpf: Export mptcp packet scheduler helpers" 3

In reply to Geliang Tang

From: Geliang Tang <[email protected]>

Remove the unsafe __ign suffix from the sk parameter of bpf_mptcp_subflow_ctx(). This suffix was used to bypass verifier checks but is no longer effective in newer kernels. The function now uses a normal parameter name, though it will soon be replaced by inline iterations in BPF programs.

---
 net/mptcp/bpf.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
net/mptcp/bpf.c +1 -3
index 938aa6399608..57ee40077a48 100644
@@ -220,10 +220,8 @@ struct bpf_iter_mptcp_subflow_kern {
 __bpf_kfunc_start_defs();
 
 __bpf_kfunc static struct mptcp_subflow_context *
-bpf_mptcp_subflow_ctx(const struct sock *sk__ign)
+bpf_mptcp_subflow_ctx(const struct sock *sk)
 {
-	const struct sock *sk = sk__ign;
-
 	if (sk && sk_fullsock(sk) &&
 	    sk->sk_protocol == IPPROTO_TCP && sk_is_mptcp(sk))
 		return mptcp_subflow_ctx(sk);
Signature · 1 line
2.53.0
author reviewer
#5 lore raw

PATCH v2 5/7 Squash to "selftests/bpf: Add bpf_first scheduler & test"

In reply to Geliang Tang

From: Geliang Tang <[email protected]>

Replace bpf_mptcp_subflow_ctx(msk->first) with a bpf_for_each iteration over subflows to find the one whose tcp_sock matches msk->first. This avoids passing an untrusted pointer (msk->first) to a kfunc that requires trusted/referenced arguments, resolving the verifier error "R1 must be referenced or trusted".

---
 .../selftests/bpf/progs/mptcp_bpf_first.c       | 17 ++++++++++-------
 1 file changed, 10 insertions(+), 7 deletions(-)
tools/testing/selftests/bpf/progs/mptcp_bpf_first.c +10 -7
index 96b002808ce7..13ecee9833cf 100644
@@ -20,13 +20,16 @@ SEC("struct_ops")
 int BPF_PROG(bpf_first_get_send, struct mptcp_sock *msk)
 {
 	struct mptcp_subflow_context *subflow;
-
-	subflow = bpf_mptcp_subflow_ctx(msk->first);
-	if (!subflow)
-		return -1;
-
-	mptcp_subflow_set_scheduled(subflow, true);
-	return 0;
+	struct sock *ssk;
+
+	bpf_for_each(mptcp_subflow, subflow, (struct sock *)msk) {
+		ssk = bpf_mptcp_subflow_tcp_sock(subflow);
+		if (ssk == msk->first) {
+			mptcp_subflow_set_scheduled(subflow, true);
+			return 0;
+		}
+	}
+	return -1;
 }
 
 SEC(".struct_ops.link")
Signature · 1 line
2.53.0
author reviewer
#6 lore raw

PATCH v2 6/7 Squash to "selftests/bpf: Add bpf_rr scheduler & test"

In reply to Geliang Tang

From: Geliang Tang <[email protected]>

Rewrite bpf_rr_get_send to use a single traversal over subflows instead of calling bpf_mptcp_subflow_ctx() and bpf_iter_mptcp_subflow_next(). The new implementation:
- Records the first subflow as a fallback
- Uses a flag to find the subflow after ptr->last_snd
- Falls back to first when last_snd is NULL or not found

This eliminates unsafe pointer handling and avoids verifier trust issues.

---
 .../selftests/bpf/progs/mptcp_bpf_rr.c        | 30 +++++++++++--------
 1 file changed, 17 insertions(+), 13 deletions(-)
tools/testing/selftests/bpf/progs/mptcp_bpf_rr.c +17 -13
index 3c685c77f67f..10cef8abf206 100644
@@ -33,33 +33,37 @@ void BPF_PROG(mptcp_sched_rr_release, struct mptcp_sock *msk)
 SEC("struct_ops")
 int BPF_PROG(bpf_rr_get_send, struct mptcp_sock *msk)
 {
-	struct mptcp_subflow_context *subflow, *next;
+	struct mptcp_subflow_context *subflow, *first = NULL, *next = NULL;
 	struct mptcp_rr_storage *ptr;
+	bool found = false;
 
 	ptr = bpf_sk_storage_get(&mptcp_rr_map, msk, 0,
 				 BPF_LOCAL_STORAGE_GET_F_CREATE);
 	if (!ptr)
 		return -1;
 
-	next = bpf_mptcp_subflow_ctx(msk->first);
-	if (!next)
-		return -1;
-
-	if (!ptr->last_snd)
-		goto out;
-
 	bpf_for_each(mptcp_subflow, subflow, (struct sock *)msk) {
-		if (mptcp_subflow_tcp_sock(subflow) == ptr->last_snd) {
-			subflow = bpf_iter_mptcp_subflow_next(&___it);
-			if (!subflow)
-				break;
+		if (!first)
+			first = subflow;
+
+		if (!ptr->last_snd)
+			break;
 
+		if (found) {
 			next = subflow;
 			break;
 		}
+
+		if (mptcp_subflow_tcp_sock(subflow) == ptr->last_snd)
+			found = true;
 	}
 
-out:
+	if (!first)
+		return -1;
+
+	if (!next)
+		next = first;
+
 	mptcp_subflow_set_scheduled(next, true);
 	ptr->last_snd = mptcp_subflow_tcp_sock(next);
 	return 0;
Signature · 1 line
2.53.0
author reviewer
#7 lore raw

PATCH v2 7/7 Squash to "selftests/bpf: Add bpf_burst scheduler & test" 2

In reply to Geliang Tang

From: Geliang Tang <[email protected]>

Modify bpf_burst_get_send to store subflow pointers instead of sock pointers in the send_info array, eliminating the need to call bpf_mptcp_subflow_ctx() for reverse lookup. The selected subflow pointer from the iterator is trusted, so it can be used directly for scheduling and member updates. This resolves the verifier error caused by passing untrusted sock pointers to kfuncs.

---
 .../selftests/bpf/progs/mptcp_bpf_burst.c     | 22 +++++++++++--------
 1 file changed, 13 insertions(+), 9 deletions(-)
tools/testing/selftests/bpf/progs/mptcp_bpf_burst.c +13 -9
index bd258776076c..585d643d6e31 100644
@@ -9,6 +9,11 @@ char _license[] SEC("license") = "GPL";
 
 #define MPTCP_SEND_BURST_SIZE	65428
 
+struct bpf_subflow_send_info {
+	struct mptcp_subflow_context *subflow;
+	u64 linger_time;
+};
+
 #define SSK_MODE_ACTIVE	0
 #define SSK_MODE_BACKUP	1
 #define SSK_MODE_MAX	2
@@ -81,7 +86,7 @@ void BPF_PROG(mptcp_sched_burst_release, struct mptcp_sock *msk)
 SEC("struct_ops")
 int BPF_PROG(bpf_burst_get_send, struct mptcp_sock *msk)
 {
-	struct subflow_send_info send_info[SSK_MODE_MAX];
+	struct bpf_subflow_send_info send_info[SSK_MODE_MAX];
 	struct mptcp_subflow_context *subflow;
 	struct sock *sk = (struct sock *)msk;
 	__u32 pace, burst, wmem;
@@ -91,7 +96,7 @@ int BPF_PROG(bpf_burst_get_send, struct mptcp_sock *msk)
 
 	/* pick the subflow with the lower wmem/wspace ratio */
 	for (i = 0; i < SSK_MODE_MAX; ++i) {
-		send_info[i].ssk = NULL;
+		send_info[i].subflow = NULL;
 		send_info[i].linger_time = -1;
 	}
 
@@ -114,7 +119,7 @@ int BPF_PROG(bpf_burst_get_send, struct mptcp_sock *msk)
 
 		linger_time = div_u64((__u64)ssk->sk_wmem_queued << 32, pace);
 		if (linger_time < send_info[backup].linger_time) {
-			send_info[backup].ssk = ssk;
+			send_info[backup].subflow = subflow;
 			send_info[backup].linger_time = linger_time;
 		}
 	}
@@ -122,18 +127,17 @@ int BPF_PROG(bpf_burst_get_send, struct mptcp_sock *msk)
 
 	/* pick the best backup if no other subflow is active */
 	if (!nr_active)
-		send_info[SSK_MODE_ACTIVE].ssk = send_info[SSK_MODE_BACKUP].ssk;
+		send_info[SSK_MODE_ACTIVE].subflow = send_info[SSK_MODE_BACKUP].subflow;
 
-	ssk = send_info[SSK_MODE_ACTIVE].ssk;
-	if (!ssk || !sk_stream_memory_free(msk, ssk))
+	subflow = send_info[SSK_MODE_ACTIVE].subflow;
+	if (!subflow)
 		return -1;
 
-	subflow = bpf_mptcp_subflow_ctx(ssk);
-	if (!subflow)
+	ssk = mptcp_subflow_tcp_sock(subflow);
+	if (!ssk || !sk_stream_memory_free(msk, ssk))
 		return -1;
 
 	burst = min(MPTCP_SEND_BURST_SIZE, mptcp_wnd_end(msk) - msk->snd_nxt);
-	ssk = bpf_core_cast(ssk, struct sock);
 	wmem = ssk->sk_wmem_queued;
 	if (!burst)
 		goto out;
Signature · 1 line
2.53.0
#8 lore raw
In reply to Geliang Tang

Hi Geliang,

Thank you for your modifications, that's great!

Our CI did some validations and here is its report:

- KVM Validation: normal (except selftest_mptcp_join): Success! ✅
- KVM Validation: normal (only selftest_mptcp_join): Success! ✅
- KVM Validation: debug (except selftest_mptcp_join): Notice: Call Traces at boot time, rebooted and continued ⚠️
- KVM Validation: debug (only selftest_mptcp_join): Notice: Call Traces at boot time, rebooted and continued ⚠️
- KVM Validation: btf-normal (only bpftest_all): Success! ✅
- KVM Validation: btf-debug (only bpftest_all): Notice: Call Traces at boot time, rebooted and continued ⚠️
- Task: https://github.com/multipath-tcp/mptcp_net-next/actions/runs/27933086171

Initiator: Patchew Applier
Commits: https://github.com/multipath-tcp/mptcp_net-next/commits/09e091cc5517
Patchwork: https://patchwork.kernel.org/project/mptcp/list/?series=1114517

If there are some issues, you can reproduce them using the same environment as the one used by the CI thanks to a docker image, e.g.:

    $ cd [kernel source code]
    $ docker run -v "${PWD}:${PWD}:rw" -w "${PWD}" --privileged --rm -it \
        --pull always mptcp/mptcp-upstream-virtme-docker:latest \
        auto-normal

For more details:

https://github.com/multipath-tcp/mptcp-upstream-virtme-docker

Please note that despite all the efforts that have been already done to have a stable tests suite when executed on a public CI like here, it is possible some reported issues are not due to your modifications. Still, do not hesitate to help us improve that ;-)

Cheers,
MPTCP GH Action bot
Bot operated by Matthieu Baerts (NGI0 Core)