Skip to contentSkip to conversation

Squash to "Add mptcp_subflow bpf_iter support"

PATCH Superseded Geliang Tang 3 participants 8 messages Nov 21, 2024 – Dec 5, 2024

Subsystems (5)

v2 of 5 Superseded Latest is v5 →

lore
Share & tools
lore

Geliang Tang

author reviewer
#0 lore raw

v2 cover Squash to "Add mptcp_subflow bpf_iter support"

From: Geliang Tang <[email protected]>

v2:
- add CONFIG_LOCKDEP check in patch 2 to fix the build error reported by CI.

Address Martin's comments in v1.

Geliang Tang (3):
Squash to "bpf: Register mptcp common kfunc set" Squash to "bpf: Add mptcp_subflow bpf_iter"
Squash to "selftests/bpf: Add mptcp_subflow bpf_iter subtest"

 net/mptcp/bpf.c                               | 22 ++++++++++++++-----
 .../selftests/bpf/progs/mptcp_bpf_iters.c     |  7 +++---
 2 files changed, 20 insertions(+), 9 deletions(-)
Signature · 1 line
2.45.2
author reviewer
#1 lore raw

PATCH v2 1/3 Squash to "bpf: Register mptcp common kfunc set"

From: Geliang Tang <[email protected]>

Address Martin's comments in v1:

- check IPPROTO_MPTCP in bpf_mptcp_sk.
- add null-checks for the wrappers.
- add more BPF flags for the wrappers.
- register this kfunc set to BPF_PROG_TYPE_CGROUP_SOCKOPT only, not BPF_PROG_TYPE_UNSPEC.

---
 net/mptcp/bpf.c | 17 +++++++++++++----
 1 file changed, 13 insertions(+), 4 deletions(-)
net/mptcp/bpf.c +13 -4
index e9db856972cb..02038db59956 100644
@@ -218,18 +218,27 @@ __bpf_kfunc_start_defs();
 
 __bpf_kfunc static struct mptcp_sock *bpf_mptcp_sk(struct sock *sk)
 {
+	if (!sk || sk->sk_protocol != IPPROTO_MPTCP)
+		return NULL;
+
 	return mptcp_sk(sk);
 }
 
 __bpf_kfunc static struct mptcp_subflow_context *
 bpf_mptcp_subflow_ctx(const struct sock *sk)
 {
+	if (!sk)
+		return NULL;
+
 	return mptcp_subflow_ctx(sk);
 }
 
 __bpf_kfunc static struct sock *
 bpf_mptcp_subflow_tcp_sock(const struct mptcp_subflow_context *subflow)
 {
+	if (!subflow)
+		return NULL;
+
 	return mptcp_subflow_tcp_sock(subflow);
 }
 
@@ -299,9 +308,9 @@ __bpf_kfunc static bool bpf_mptcp_subflow_queues_empty(struct sock *sk)
 __bpf_kfunc_end_defs();
 
 BTF_KFUNCS_START(bpf_mptcp_common_kfunc_ids)
-BTF_ID_FLAGS(func, bpf_mptcp_sk)
-BTF_ID_FLAGS(func, bpf_mptcp_subflow_ctx)
-BTF_ID_FLAGS(func, bpf_mptcp_subflow_tcp_sock)
+BTF_ID_FLAGS(func, bpf_mptcp_sk, KF_TRUSTED_ARGS | KF_RET_NULL)
+BTF_ID_FLAGS(func, bpf_mptcp_subflow_ctx, KF_RET_NULL)
+BTF_ID_FLAGS(func, bpf_mptcp_subflow_tcp_sock, KF_RET_NULL)
 BTF_ID_FLAGS(func, bpf_iter_mptcp_subflow_new, KF_ITER_NEW | KF_TRUSTED_ARGS)
 BTF_ID_FLAGS(func, bpf_iter_mptcp_subflow_next, KF_ITER_NEXT | KF_RET_NULL)
 BTF_ID_FLAGS(func, bpf_iter_mptcp_subflow_destroy, KF_ITER_DESTROY)
@@ -335,7 +344,7 @@ static int __init bpf_mptcp_kfunc_init(void)
 	int ret;
 
 	ret = register_btf_fmodret_id_set(&bpf_mptcp_fmodret_set);
-	ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_UNSPEC,
+	ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_CGROUP_SOCKOPT,
 					       &bpf_mptcp_common_kfunc_set);
 	ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_STRUCT_OPS,
 					       &bpf_mptcp_sched_kfunc_set);
Signature · 1 line
2.45.2
author reviewer
#2 lore raw

PATCH v2 2/3 Squash to "bpf: Add mptcp_subflow bpf_iter"

In reply to Geliang Tang

From: Geliang Tang <[email protected]>

Address Martin's comments in v1:

- bpf_iter_mptcp_subflow_new returns -EINVAL when msk socket lock isn't held.

---
 net/mptcp/bpf.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
net/mptcp/bpf.c +4 -1
index 02038db59956..89a7a482368d 100644
@@ -252,7 +252,10 @@ bpf_iter_mptcp_subflow_new(struct bpf_iter_mptcp_subflow *it,
 	if (!msk)
 		return -EINVAL;
 
-	msk_owned_by_me(msk);
+#ifdef CONFIG_LOCKDEP
+	if (!lockdep_sock_is_held((const struct sock *)msk))
+		return -EINVAL;
+#endif
 
 	kit->pos = &msk->conn_list;
 	return 0;
Signature · 1 line
2.45.2
maintainer
#3 lore raw

Re: [PATCH mptcp-next v2 2/3] Squash to "bpf: Add mptcp_subflow bpf_iter"

Hi Geliang,

Quoted 17 lines · Geliang Tang

From: Geliang Tang <[email protected]>

Address Martin's comments in v1:

- bpf_iter_mptcp_subflow_new returns -EINVAL when msk socket lock isn't held.

---
 net/mptcp/bpf.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
net/mptcp/bpf.c +0 -0
index 02038db59956..89a7a482368d 100644

net/mptcp/bpf.c bpf_iter_mptcp_subflow_new(struct bpf_iter_mptcp_subflow *it, · line 257

Quoted 7 lines
net/mptcp/bpf.c +3 -1
@@ -252,7 +252,10 @@ bpf_iter_mptcp_subflow_new(struct bpf_iter_mptcp_subflow *it,
 	if (!msk)
 		return -EINVAL;
 
-	msk_owned_by_me(msk);
+#ifdef CONFIG_LOCKDEP
+	if (!lockdep_sock_is_held((const struct sock *)msk))
+		return -EINVAL;

This feels wrong: lockdep is a debugging tool, the goal is usually to WARN in case of issue, not to take a different path.

Maybe you want to use spin_is_locked(), but still, I'm not sure whether that's the direction to take: it means the msk always needs to be locked when iterating over the different subflow → is it always the case? Maybe yes if it is used only to modify update it?

(If not, maybe you just need to check the sk_refcnt as a safety measure? But that's maybe not enough for all cases.)

Did you check what was being done for the BPF TCP CA? I guess they need a way to prevent calling some kfunc without the required locks or refcount, no?

Cheers,
Matt

Signature · 1 line
Sponsored by the NGI0 Core fund.
author reviewer
#4 lore raw

Re: [PATCH mptcp-next v2 2/3] Squash to "bpf: Add mptcp_subflow bpf_iter"

Hi Matt,

Quoted 16 lines · Matthieu Baerts

Hi Geliang,

Quoted 13 lines · Geliang Tang

From: Geliang Tang <[email protected]>

Address Martin's comments in v1:

- bpf_iter_mptcp_subflow_new returns -EINVAL when msk socket lock isn't
  held.

---
 net/mptcp/bpf.c | 5 ++++-

 1 file changed, 4 insertions(+), 1 deletion(-)
Quoted 6 lines

net/mptcp/bpf.c bpf_iter_mptcp_subflow_new(struct · line 252

Quoted 6 lines
net/mptcp/bpf.c +0 -0
index 02038db59956..89a7a482368d 100644
@@ -252,7 +252,10 @@ bpf_iter_mptcp_subflow_new(struct
 bpf_iter_mptcp_subflow *it,

Other bpf_iter have added "sizeof" and "alignof" checks, I think mptcp_subflow bpf_iter also needs to add them too:

      BUILD_BUG_ON(sizeof(struct bpf_iter_mptcp_subflow_kern) !=
                   sizeof(struct bpf_iter_mptcp_subflow));
      BUILD_BUG_ON(__alignof__(struct bpf_iter_mptcp_subflow_kern) !=
                   __alignof__(struct bpf_iter_mptcp_subflow));
Quoted 11 lines

net/mptcp/bpf.c bpf_iter_mptcp_subflow_new(struct bpf_iter_mptcp_subflow *it, · line 257

Quoted 7 lines
net/mptcp/bpf.c +0 -0
  	if (!msk)
  		return -EINVAL;

net/mptcp/bpf.c +3 -1
-	msk_owned_by_me(msk);
+#ifdef CONFIG_LOCKDEP
+	if (!lockdep_sock_is_held((const struct sock *)msk))
+		return -EINVAL;

This feels wrong: lockdep is a debugging tool, the goal is usually to WARN in case of issue, not to take a different path.

Quoted 2 lines

Maybe you want to use spin_is_locked(), but still, I'm not sure whether

spin_is_locked() doesn't work, since this msk socket lock isn't a spinlock, right?

Quoted 9 lines

that's the direction to take: it means the msk always needs to be locked
when iterating over the different subflow → is it always the case? Maybe
yes if it is used only to modify update it?

(If not, maybe you just need to check the sk_refcnt as a safety measure?
But that's maybe not enough for all cases.)

I think the answer is no, sometimes locking is not necessary when iterating over the different subflows. Does this mean we can simply drop msk_owned_by_me() here?

Quoted 5 lines

Did you check what was being done for the BPF TCP CA? I guess they need
a way to prevent calling some kfunc without the required locks or refcount, no?

Not found in BPF TCP CA.

Thanks,
-Geliang

Quoted 3 lines

Cheers,
Matt

maintainer
#5 lore raw

Re: [PATCH mptcp-next v2 2/3] Squash to "bpf: Add mptcp_subflow bpf_iter"

Hi Geliang,

We briefly talked about this patch yesterday with Mat.

Quoted 29 lines · Geliang Tang

Hi Matt,

Quoted 16 lines · Matthieu Baerts

Hi Geliang,

Quoted 13 lines · Geliang Tang

From: Geliang Tang <[email protected]>

Address Martin's comments in v1:

- bpf_iter_mptcp_subflow_new returns -EINVAL when msk socket lock isn't
  held.

---
 net/mptcp/bpf.c | 5 ++++-

 1 file changed, 4 insertions(+), 1 deletion(-)
Quoted 6 lines

net/mptcp/bpf.c bpf_iter_mptcp_subflow_new(struct · line 252

Quoted 6 lines
net/mptcp/bpf.c +0 -0
index 02038db59956..89a7a482368d 100644
@@ -252,7 +252,10 @@ bpf_iter_mptcp_subflow_new(struct
 bpf_iter_mptcp_subflow *it,

Other bpf_iter have added "sizeof" and "alignof" checks, I think mptcp_subflow bpf_iter also needs to add them too:

Quoted 4 lines
      BUILD_BUG_ON(sizeof(struct bpf_iter_mptcp_subflow_kern) !=
                   sizeof(struct bpf_iter_mptcp_subflow));
      BUILD_BUG_ON(__alignof__(struct bpf_iter_mptcp_subflow_kern) !=
                   __alignof__(struct bpf_iter_mptcp_subflow));

If it is something that is done with other bpf_iter, I guess we should indeed do that, especially for a check done at build time!

Quoted 11 lines
Quoted 11 lines

net/mptcp/bpf.c bpf_iter_mptcp_subflow_new(struct bpf_iter_mptcp_subflow *it, · line 257

Quoted 7 lines
net/mptcp/bpf.c +0 -0
  	if (!msk)
  		return -EINVAL;

net/mptcp/bpf.c +3 -1
-	msk_owned_by_me(msk);
+#ifdef CONFIG_LOCKDEP
+	if (!lockdep_sock_is_held((const struct sock *)msk))
+		return -EINVAL;

This feels wrong: lockdep is a debugging tool, the goal is usually to WARN in case of issue, not to take a different path.

Quoted 5 lines
Quoted 2 lines

Maybe you want to use spin_is_locked(), but still, I'm not sure whether

spin_is_locked() doesn't work, since this msk socket lock isn't a spinlock, right?

(I was talking about sk->sk_lock.slock, but while not being sure that's what you need here)

Quoted 6 lines
Quoted 6 lines

that's the direction to take: it means the msk always needs to be locked
when iterating over the different subflow → is it always the case? Maybe
yes if it is used only to modify update it?

Quoted 7 lines
Quoted 3 lines

(If not, maybe you just need to check the sk_refcnt as a safety measure?
But that's maybe not enough for all cases.)

I think the answer is no, sometimes locking is not necessary when iterating over the different subflows. Does this mean we can simply drop msk_owned_by_me() here?

I guess it is safer to add a check here, to avoid random BPF programs calling this kfunc in other context that could cause issues.

At the meeting yesterday, it was not clear what would be best: in the context of the packet scheduler and path-manager, do you know if this helper will always be called when the msk is locked? (And owned by the user?) If it is a hook to modify msk or subflows, I guess yes, but there are many hooks, we didn't check everything.

So if the msk is locked (and owned by the user?), then that's good to add such check. (If we don't need the lock, maybe we don't need bpf_iter because we only need to read stuff?)

In this case, maybe enough to look around sock_owned_by_user_nocheck() and/or spin_is_locked(&sk->sk_lock.slock) helpers? (We didn't check if they were making sense in this context, to be checked first!)

Also, we were wondering what was done in other cases with BPF. If a check is needed to verify a lock is held, how can the verifier make sure the lock is held by the same context. For example, how can it make sure it is not going to be released during the kfunc operation. (Maybe that's more a question for BPF maintainers)

Quoted 6 lines
Quoted 4 lines

Did you check what was being done for the BPF TCP CA? I guess they need
a way to prevent calling some kfunc without the required locks or refcount, no?

Not found in BPF TCP CA.

Strange, I wonder what safeguards they have then.

Or maybe it is possible to say that some kfunc can only be called with a specific struct ops?

Cheers,
Matt

Signature · 1 line
Sponsored by the NGI0 Core fund.
author reviewer
#6 lore raw

PATCH v2 3/3 Squash to "selftests/bpf: Add mptcp_subflow bpf_iter subtest"

In reply to Geliang Tang

From: Geliang Tang <[email protected]>

IPPROTO_MPTCP is checked in bpf_mptcp_sk(), no need to check it in BPF program.

bpf_mptcp_sk() and bpf_mptcp_subflow_ctx() may return NULL, need to check the return values.

---
 tools/testing/selftests/bpf/progs/mptcp_bpf_iters.c | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)
tools/testing/selftests/bpf/progs/mptcp_bpf_iters.c +3 -4
index 1bede22a7e3d..5e475edf37b3 100644
@@ -21,12 +21,11 @@ int iters_subflow(struct bpf_sockopt *ctx)
 	struct mptcp_sock *msk;
 	int local_ids = 0;
 
-	if (!sk || sk->protocol != IPPROTO_MPTCP ||
-	    ctx->level != SOL_TCP || ctx->optname != TCP_IS_MPTCP)
+	if (ctx->level != SOL_TCP || ctx->optname != TCP_IS_MPTCP)
 		return 1;
 
 	msk = bpf_mptcp_sk((struct sock *)sk);
-	if (msk->pm.server_side || !msk->pm.subflows)
+	if (!msk || msk->pm.server_side || !msk->pm.subflows)
 		return 1;
 
 	msk = bpf_mptcp_sock_acquire(msk);
@@ -53,7 +52,7 @@ int iters_subflow(struct bpf_sockopt *ctx)
 
 	/* only to check the following kfunc works */
 	subflow = bpf_mptcp_subflow_ctx(ssk);
-	if (subflow->token != msk->token)
+	if (!subflow || subflow->token != msk->token)
 		goto out;
 
 	ids = local_ids;
Signature · 1 line
2.45.2
#7 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: Success! ✅
- KVM Validation: debug: Success! ✅
- KVM Validation: btf-normal (only bpftest_all): Success! ✅
- KVM Validation: btf-debug (only bpftest_all): Success! ✅
- Task: https://github.com/multipath-tcp/mptcp_net-next/actions/runs/11950722365

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

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)