Squash to "Add mptcp_subflow bpf_iter support"
Subsystems (5)
Geliang Tang
reviewerv5 cover Squash to "Add mptcp_subflow bpf_iter support"
From: Geliang Tang <[email protected]>
v5:
- check bpf_iter_task in mptcp_subflow_new() as Mat suggested.
v4:
- drop sock_owned_by_user_nocheck and spin_is_locked. According to comments from Mat and Martin, in this set mptcp_subflow bpf_iter only used from a cg sockopt bpf prog, no need to add these check at this moment.
v3:
- patch 3, continue to use sock_owned_by_user_nocheck() and spin_is_locked()
checks instead of using msk_owned_by_me().
- patch 5, drop declaration of bpf_mptcp_subflow_tcp_sock. It's no longer
used.
- patch 5, update the comment for mptcp_subflow_tcp_sock(), which is a BPF
helper, not a kfunc.
The commit log of "bpf: Register mptcp common kfunc set" doesn't match the code, please update it as:
'''
bpf: Register mptcp common kfunc set
MPTCP helper mptcp_subflow_ctx() is used to convert struct sock to struct mptcp_subflow_context. It will be used in MPTCP BPF programs.
This patch defines corresponding wrapper of this helper, and put it into the newly defined mptcp common kfunc set and register this set with the flag BPF_PROG_TYPE_CGROUP_SOCKOPT to let it accessible to the 'cgroup/getsockopt' type of BPF programs.
'''
v2:
- Drop bpf_skc_to_mptcp_sock
- Check the owner before assigning the msk as Mat suggested.
- Use bpf_core_cast() in mptcp_subflow bpf_iter subtest instead of
using bpf_skc_to_mptcp_sock().
Address Martin's suggestions for "Add mptcp_subflow bpf_iter support" v2.
Geliang Tang (5):
mptcp: add bpf_iter_task for mptcp_sock
Squash to "bpf: Extend bpf_skc_to_mptcp_sock to MPTCP sock" Squash to "bpf: Add mptcp_subflow bpf_iter"
Revert "bpf: Acquire and release mptcp socket" Squash to "selftests/bpf: Add mptcp_subflow bpf_iter subtest"
net/mptcp/bpf.c | 56 +++++++++---------- net/mptcp/protocol.c | 1 + net/mptcp/protocol.h | 16 ++++++ .../testing/selftests/bpf/bpf_experimental.h | 2 +- tools/testing/selftests/bpf/progs/mptcp_bpf.h | 5 -- .../selftests/bpf/progs/mptcp_bpf_iters.c | 8 +-- 6 files changed, 47 insertions(+), 41 deletions(-)
Signature · 1 line
2.43.0
Geliang Tang
reviewerPATCH v5 1/5 mptcp: add bpf_iter_task for mptcp_sock
From: Geliang Tang <[email protected]>
To make sure the mptcp_subflow bpf_iter is running in the MPTCP context. This patch adds a simplified version of tracking for it:
1. Add a 'struct task_struct *bpf_iter_task' field to struct mptcp_sock.
2. Do a WRITE_ONCE(msk->bpf_iter_task, current) before calling a MPTCP BPF hook, and WRITE_ONCE(msk->bpf_iter_task, NULL) after the hook returns.
3. In bpf_iter_mptcp_subflow_new(), check
"READ_ONCE(msk->bpf_scheduler_task) == current"
to confirm the correct task, return -EINVAL if it doesn't match.
Also creates helpers for setting, clearing and checking that value.
--- net/mptcp/protocol.c | 1 + net/mptcp/protocol.h | 16 ++++++++++++++++ 2 files changed, 17 insertions(+)
net/mptcp/protocol.c +1 -0
@@ -2726,6 +2726,7 @@ static void __mptcp_init_sock(struct sock *sk) msk->scaling_ratio =TCP_DEFAULT_SCALING_RATIO ;WRITE_ONCE (msk->first,NULL ); +WRITE_ONCE (msk->bpf_iter_task,NULL );inet_csk (sk)->icsk_sync_mss = mptcp_sync_mss;WRITE_ONCE (msk->csum_enabled,mptcp_is_checksum_enabled (sock_net (sk)));WRITE_ONCE (msk->allow_infinite_fallback, true);
net/mptcp/protocol.h +16 -0
@@ -327,6 +327,7 @@ struct mptcp_sock {struct list_head conn_list;struct list_head rtx_queue;struct mptcp_data_frag *first_pending; +struct task_struct *bpf_iter_task;struct list_head join_list;struct sock *first;/* The mptcp ops can safely dereference, using suitable * ONCE annotation, the subflow outside the socket
@@ -1291,4 +1292,19 @@ mptcp_token_join_cookie_init_state(struct mptcp_subflow_request_sock *subflow_restatic inline void mptcp_join_cookie_init (void ) {}#endif +static inline void mptcp_set_bpf_iter_task (struct mptcp_sock *msk) +{ +WRITE_ONCE (msk->bpf_iter_task, current); +} + +static inline void mptcp_clear_bpf_iter_task (struct mptcp_sock *msk) +{ +WRITE_ONCE (msk->bpf_iter_task,NULL ); +} + +static inline struct task_struct *mptcp_get_bpf_iter_task (struct mptcp_sock *msk) +{ +return READ_ONCE (msk->bpf_iter_task); +} +#endif /* __MPTCP_PROTOCOL_H */
Signature · 1 line
2.43.0
Geliang Tang
reviewerPATCH v5 2/5 Squash to "bpf: Extend bpf_skc_to_mptcp_sock to MPTCP sock"
In reply to Geliang TangFrom: Geliang Tang <[email protected]>
Set msk->bpf_iter_task in bpf_mptcp_sock_from_sock() to allow mptcp_subflow bpt_iter can be used in cgroup/getsockopt, otherwise, the selftest in this set fails.
--- net/mptcp/bpf.c | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-)
net/mptcp/bpf.c +12 -4
@@ -197,14 +197,22 @@ static struct bpf_struct_ops bpf_mptcp_sched_ops = {struct mptcp_sock *bpf_mptcp_sock_from_sock (struct sock *sk) { +struct mptcp_sock *msk; +if (unlikely (!sk || !sk_fullsock (sk)))return NULL ; -if (sk->sk_protocol ==IPPROTO_MPTCP ) -return mptcp_sk (sk); +if (sk->sk_protocol ==IPPROTO_MPTCP ) { + msk =mptcp_sk (sk); +mptcp_set_bpf_iter_task (msk); +return msk; + } -if (sk->sk_protocol ==IPPROTO_TCP &&sk_is_mptcp (sk)) -return mptcp_sk (mptcp_subflow_ctx (sk)->conn); +if (sk->sk_protocol ==IPPROTO_TCP &&sk_is_mptcp (sk)) { + msk =mptcp_sk (mptcp_subflow_ctx (sk)->conn); +mptcp_set_bpf_iter_task (msk); +return msk; + }return NULL ; }
Signature · 1 line
2.43.0
Mat Martineau
maintainerRe: [PATCH mptcp-next v5 2/5] Squash to "bpf: Extend bpf_skc_to_mptcp_sock to MPTCP sock"
Quoted 19 lines · Geliang Tang
From: Geliang Tang <[email protected]>
Set
msk->bpf_iter_taskinbpf_mptcp_sock_from_sock()to allowmptcp_subflowbpt_itercan be used in cgroup/getsockopt, otherwise, the selftest in this set fails.Signed-off-by Geliang Tang <[email protected]>---
net/mptcp/bpf.c| 16 ++++++++++++----1 file changed, 12 insertions(+), 4 deletions(-)net/mptcp/bpf.c +0
-0@@ -197,14 +197,22 @@ static struct bpf_struct_ops bpf_mptcp_sched_ops = {struct mptcp_sock *bpf_mptcp_sock_from_sock (struct sock *sk) {
net/mptcp/bpf.c static struct bpf_struct_ops bpf_mptcp_sched_ops = { · line 207
Quoted 10 lines
net/mptcp/bpf.c +5
-2+struct mptcp_sock *msk; +if (unlikely (!sk || !sk_fullsock (sk)))return NULL ; -if (sk->sk_protocol ==IPPROTO_MPTCP ) -return mptcp_sk (sk); +if (sk->sk_protocol ==IPPROTO_MPTCP ) { + msk =mptcp_sk (sk); +mptcp_set_bpf_iter_task (msk);
Hi Geliang -
The important part of the bpf_iter_task approach is to only set the task_struct pointer when the msk lock is already held, and to clear it before the msk lock is released. When used this way, the check ensures that the iterator code is both *running with the socket locked* and *in the same context where the lock is held*.
The code above will set msk->bpf_iter_task even when the lock is not held, and then it is never cleared.
To set/clear as expected for get/setsockopt I think the task pointer would have to be set in places where the locks are acquired and released, like these:
https://elixir.bootlin.com/linux/v6.14-rc5/source/kernel/bpf/cgroup.c#L1955
https://elixir.bootlin.com/linux/v6.14-rc5/source/kernel/bpf/cgroup.c#L1846
- Mat
Quoted 18 lines
net/mptcp/bpf.c +7
-2+return msk; + } -if (sk->sk_protocol ==IPPROTO_TCP &&sk_is_mptcp (sk)) -return mptcp_sk (mptcp_subflow_ctx (sk)->conn); +if (sk->sk_protocol ==IPPROTO_TCP &&sk_is_mptcp (sk)) { + msk =mptcp_sk (mptcp_subflow_ctx (sk)->conn); +mptcp_set_bpf_iter_task (msk); +return msk; + }return NULL ; }Signature · 1 line
2.43.0
Geliang Tang
reviewerRe: [PATCH mptcp-next v5 2/5] Squash to "bpf: Extend bpf_skc_to_mptcp_sock to MPTCP sock"
Hi Mat,
Thanks for the review.
Quoted 35 lines · Mat Martineau
Quoted 20 lines · Geliang Tang
From: Geliang Tang <[email protected]>
Set
msk->bpf_iter_taskinbpf_mptcp_sock_from_sock()to allowmptcp_subflowbpt_itercan be used in cgroup/getsockopt, otherwise, the selftest in this set fails.Signed-off-by Geliang Tang <[email protected]>---
net/mptcp/bpf.c| 16 ++++++++++++----1 file changed, 12 insertions(+), 4 deletions(-)net/mptcp/bpf.c +0
-0@@ -197,14 +197,22 @@ static struct bpf_struct_ops bpf_mptcp_sched_ops = {struct mptcp_sock *bpf_mptcp_sock_from_sock (struct sock *sk) {net/mptcp/bpf.c static struct bpf_struct_ops · line 208
Quoted 10 lines
net/mptcp/bpf.c +5
-2+struct mptcp_sock *msk; +if (unlikely (!sk || !sk_fullsock (sk)))return NULL ; -if (sk->sk_protocol ==IPPROTO_MPTCP ) -return mptcp_sk (sk); +if (sk->sk_protocol ==IPPROTO_MPTCP ) { + msk =mptcp_sk (sk); +mptcp_set_bpf_iter_task (msk);Hi Geliang -
Quoted 8 lines
The important part of the
bpf_iter_taskapproach is to only set thetask_structpointer when the msk lock is already held, and to clear it
before the msk lock is released. When used this way, the check ensures
that the iterator code is both *running with the socket locked* and *in
the same context where the lock is held*.
Got it, thanks for your explanation.
Quoted 5 lines
The code above will set
msk->bpf_iter_taskeven when the lock is not held,
and then it is never cleared.
Quoted 9 lines
To set/clear as expected for get/setsockopt I think the task pointer would
have to be set in places where the locks are acquired and released, like
these:https://elixir.bootlin.com/linux/v6.14-rc5/source/kernel/bpf/cgroup.c#L1955
https://elixir.bootlin.com/linux/v6.14-rc5/source/kernel/bpf/cgroup.c#L1846
The get/setsockopt scenario is a bit complicated. I think we can use the bpf_iter_task check only for bpf schedulers, keep the get/setsockopt scenario unchanged, and continue to use the msk_owned_by_me(msk) check. What do you think?
In addition, I think it's better to add bpf_iter_task to struct mptcp_sched_data instead in struct mptcp_sock, since it's more related to mptcp scheduler. For specific usage, please see [1]. What do you think of this?
Thanks,
-Geliang
[1]
https://patchwork.kernel.org/project/mptcp/cover/[email protected]/
Quoted 21 lines
- Mat
Quoted 18 lines
net/mptcp/bpf.c +7
-2+return msk; + } -if (sk->sk_protocol ==IPPROTO_TCP &&sk_is_mptcp (sk)) -return mptcp_sk (mptcp_subflow_ctx (sk)->conn); +if (sk->sk_protocol ==IPPROTO_TCP &&sk_is_mptcp (sk)) { + msk =mptcp_sk (mptcp_subflow_ctx (sk)->conn); +mptcp_set_bpf_iter_task (msk); +return msk; + }return NULL ;}
Signature · 1 line
2.43.0
Geliang Tang
reviewerRe: [PATCH mptcp-next v5 2/5] Squash to "bpf: Extend bpf_skc_to_mptcp_sock to MPTCP sock"
In reply to Mat MartineauHi Mat,
Thanks for the review.
Quoted 35 lines · Mat Martineau
Quoted 20 lines · Geliang Tang
From: Geliang Tang <[email protected]>
Set
msk->bpf_iter_taskinbpf_mptcp_sock_from_sock()to allowmptcp_subflowbpt_itercan be used in cgroup/getsockopt, otherwise, the selftest in this set fails.Signed-off-by Geliang Tang <[email protected]>---
net/mptcp/bpf.c| 16 ++++++++++++----1 file changed, 12 insertions(+), 4 deletions(-)net/mptcp/bpf.c +0
-0@@ -197,14 +197,22 @@ static struct bpf_struct_ops bpf_mptcp_sched_ops = {struct mptcp_sock *bpf_mptcp_sock_from_sock (struct sock *sk) {net/mptcp/bpf.c static struct bpf_struct_ops · line 208
Quoted 10 lines
net/mptcp/bpf.c +5
-2+struct mptcp_sock *msk; +if (unlikely (!sk || !sk_fullsock (sk)))return NULL ; -if (sk->sk_protocol ==IPPROTO_MPTCP ) -return mptcp_sk (sk); +if (sk->sk_protocol ==IPPROTO_MPTCP ) { + msk =mptcp_sk (sk); +mptcp_set_bpf_iter_task (msk);Hi Geliang -
Quoted 8 lines
The important part of the
bpf_iter_taskapproach is to only set thetask_structpointer when the msk lock is already held, and to clear it
before the msk lock is released. When used this way, the check ensures
that the iterator code is both *running with the socket locked* and *in
the same context where the lock is held*.
Got it, thanks for your explanation.
Quoted 5 lines
The code above will set
msk->bpf_iter_taskeven when the lock is not held,
and then it is never cleared.
Quoted 9 lines
To set/clear as expected for get/setsockopt I think the task pointer would
have to be set in places where the locks are acquired and released, like
these:https://elixir.bootlin.com/linux/v6.14-rc5/source/kernel/bpf/cgroup.c#L1955
https://elixir.bootlin.com/linux/v6.14-rc5/source/kernel/bpf/cgroup.c#L1846
The get/setsockopt scenario is a bit complicated. I think we can use the bpf_iter_task check only for bpf schedulers, keep the get/setsockopt scenario unchanged, and continue to use the msk_owned_by_me(msk) check. What do you think?
In addition, I think it's better to add bpf_iter_task to struct mptcp_sched_data instead in struct mptcp_sock, since it's more related to mptcp scheduler. For specific usage, please see [1]. What do you think of this?
Thanks,
-Geliang
[1]
https://patchwork.kernel.org/project/mptcp/cover/[email protected]/
Quoted 21 lines
- Mat
Quoted 18 lines
net/mptcp/bpf.c +7
-2+return msk; + } -if (sk->sk_protocol ==IPPROTO_TCP &&sk_is_mptcp (sk)) -return mptcp_sk (mptcp_subflow_ctx (sk)->conn); +if (sk->sk_protocol ==IPPROTO_TCP &&sk_is_mptcp (sk)) { + msk =mptcp_sk (mptcp_subflow_ctx (sk)->conn); +mptcp_set_bpf_iter_task (msk); +return msk; + }return NULL ;}
Signature · 1 line
2.43.0
Matthieu Baerts
maintainerRe: [PATCH mptcp-next v5 2/5] Squash to "bpf: Extend bpf_skc_to_mptcp_sock to MPTCP sock"
Hi Geliang,
Quoted 1 line · Geliang Tang
On Mon, 2025-03-03 at 17:37 -0800, Mat Martineau wrote:
(...)
Quoted 10 lines
Quoted 10 lines
The code above will set
msk->bpf_iter_taskeven when the lock is not held,
and then it is never cleared.To set/clear as expected for get/setsockopt I think the task pointer would
have to be set in places where the locks are acquired and released, like
these:
Quoted 8 lines
Quoted 3 lines
https://elixir.bootlin.com/linux/v6.14-rc5/source/kernel/bpf/cgroup.c#L1955
https://elixir.bootlin.com/linux/v6.14-rc5/source/kernel/bpf/cgroup.c#L1846The get/setsockopt scenario is a bit complicated. I think we can use the
bpf_iter_taskcheck only for bpf schedulers, keep the get/setsockopt scenario unchanged, and continue to use themsk_owned_by_me(msk)check. What do you think?
I think in any case, it doesn't hurt to keep msk_owned_by_me(msk) for lockdep.
If we think it is difficult to have the bpf_iter for both CG [gs]etsockopt and struct_ops, then we can also drop the patches for the [gs]etsockopt.
Or, from a kfunc, is it easy to check if we are called from a struct_ops?
Quoted 4 lines
In addition, I think it's better to add
bpf_iter_taskto structmptcp_sched_datainstead in structmptcp_sock, since it's more related to mptcp scheduler. For specific usage, please see [1]. What do you think of this?
Please see my reply to your comment on this thread. In short, we can always add new parameters later, when we need them, no?
https://lore.kernel.org/[email protected]
Quoted 21 lines
Thanks,
-Geliang[1]
https://patchwork.kernel.org/project/mptcp/cover/[email protected]/Quoted 14 lines
- Mat
Quoted 11 lines
net/mptcp/bpf.c +7
-2+return msk; + } -if (sk->sk_protocol ==IPPROTO_TCP &&sk_is_mptcp (sk)) -return mptcp_sk (mptcp_subflow_ctx (sk)->conn); +if (sk->sk_protocol ==IPPROTO_TCP &&sk_is_mptcp (sk)) { + msk =mptcp_sk (mptcp_subflow_ctx (sk)->conn); +mptcp_set_bpf_iter_task (msk); +return msk; + }
Quoted 9 lines
Quoted 7 lines
Quoted 7 lines
return NULL; }Signature · 1 line
2.43.0
Cheers,
Matt
Signature · 1 line
Sponsored by the NGI0 Core fund.
Geliang Tang
reviewerPATCH v5 3/5 Squash to "bpf: Add mptcp_subflow bpf_iter"
In reply to Geliang TangFrom: Geliang Tang <[email protected]>
Drop the NULL check for 'msk' as Martin suggested, add more checks for 'sk'.
Use the "struct sock *sk" instead of "struct mptcp-sock *msk" as the argument in the bpf_iter_mptcp_subflow_new as Martin suggested.
v5:
- check bpf_iter_task in mptcp_subflow_new()
v4:
- drop sock_owned_by_user_nocheck and spin_is_locked. According to comments from Mat [2] and Martin [1], in this set mptcp_subflow bpf_iter only used from a cg sockopt bpf prog, no need to add these check at this moment.
[1]
https://lore.kernel.org/all/[email protected]/
[2]
https://patchwork.kernel.org/project/mptcp/patch/f6469225598beecbf0bda12a4c33fafa86c0ff15.1739787744.git.tanggeliang@kylinos.cn/
v3:
- continue to use sock_owned_by_user_nocheck and spin_is_locked
checks instead of using msk_owned_by_me().
v2:
- check the owner before assigning the msk as Mat suggested.
--- net/mptcp/bpf.c | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-)
net/mptcp/bpf.c +15 -6
@@ -249,24 +249,33 @@ bpf_mptcp_subflow_ctx(const struct sock *sk) __bpf_kfunc static intbpf_iter_mptcp_subflow_new (struct bpf_iter_mptcp_subflow *it, -struct mptcp_sock *msk) +struct sock *sk) {struct bpf_iter_mptcp_subflow_kern *kit = (void *)it; -struct sock *sk = (struct sock *)msk; +struct task_struct *task; +struct mptcp_sock *msk;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 )); - kit->msk = msk; -if (!msk) +if (unlikely (!sk || !sk_fullsock (sk)))return -EINVAL ; -if (!sock_owned_by_user_nocheck (sk) && - !spin_is_locked (&sk->sk_lock.slock)) +if (sk->sk_protocol !=IPPROTO_MPTCP )return -EINVAL ; + msk =mptcp_sk (sk); + task =mptcp_get_bpf_iter_task (msk); +if (!task || task != current) +return -EINVAL ; + +mptcp_clear_bpf_iter_task (msk); + +msk_owned_by_me (msk); + + kit->msk = msk; kit->pos = &msk->conn_list;return 0 ; }
Signature · 1 line
2.43.0
Mat Martineau
maintainerRe: [PATCH mptcp-next v5 3/5] Squash to "bpf: Add mptcp_subflow bpf_iter"
Quoted 65 lines · Geliang Tang
From: Geliang Tang <[email protected]>
Drop the NULL check for 'msk' as Martin suggested, add more checks for 'sk'.
Use the "struct sock *sk" instead of "struct mptcp-sock *msk" as the argument in the
bpf_iter_mptcp_subflow_newas Martin suggested.v5:
- checkbpf_iter_taskinmptcp_subflow_new()v4:
- dropsock_owned_by_user_nocheckandspin_is_locked. According to comments from Mat [2] and Martin [1], in this setmptcp_subflowbpf_iteronly used from a cg sockopt bpf prog, no need to add these check at this moment.[1]
https://lore.kernel.org/all/[email protected]/
[2]
https://patchwork.kernel.org/project/mptcp/patch/f6469225598beecbf0bda12a4c33fafa86c0ff15.1739787744.git.tanggeliang@kylinos.cn/v3:
- continue to usesock_owned_by_user_nocheckandspin_is_lockedchecks instead of usingmsk_owned_by_me().v2:
- check the owner before assigning the msk as Mat suggested.Signed-off-by Geliang Tang <[email protected]>---
net/mptcp/bpf.c| 21 +++++++++++++++------1 file changed, 15 insertions(+), 6 deletions(-)net/mptcp/bpf.c +0
-0@@ -249,24 +249,33 @@ bpf_mptcp_subflow_ctx(const struct sock *sk)
__bpf_kfuncstatic int bpf_iter_mptcp_subflow_new(structbpf_iter_mptcp_subflow*it,net/mptcp/bpf.c +1
-1- struct mptcp_sock *msk) + struct sock *sk){ struct bpf_iter_mptcp_subflow_kern *kit = (void *)it;net/mptcp/bpf.c +2
-1-struct sock *sk = (struct sock *)msk; +struct task_struct *task; +struct mptcp_sock *msk;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));net/mptcp/bpf.c +1
-2- kit->msk = msk; -if (!msk) +if (unlikely (!sk || !sk_fullsock (sk)))return -EINVAL;net/mptcp/bpf.c +1
-2-if (!sock_owned_by_user_nocheck (sk) && - !spin_is_locked (&sk->sk_lock.slock)) +if (sk->sk_protocol !=IPPROTO_MPTCP )return -EINVAL;
net/mptcp/bpf.c bpf_mptcp_subflow_ctx(const struct sock *sk) · line 272
Quoted 4 lines
net/mptcp/bpf.c +4
-0+ msk =mptcp_sk (sk); + task =mptcp_get_bpf_iter_task (msk); +if (!task || task != current) +return -EINVAL ;
Hi Geliang -
The task checking logic would fit better inside the helper, since every caller will be checking to see if it matches 'current'.
I also think it would read better as (task && task == current)
net/mptcp/bpf.c bpf_mptcp_subflow_ctx(const struct sock *sk) · line 274
Quoted 2 lines
net/mptcp/bpf.c +2
-0+ +mptcp_clear_bpf_iter_task (msk);
This needs to be cleared where the lock is released, not where the task is checked.
- Mat
Quoted 12 lines
net/mptcp/bpf.c +4
-0+ +msk_owned_by_me (msk); + + kit->msk = msk;kit->pos = &msk->conn_list; return 0; }Signature · 1 line
2.43.0
Geliang Tang
reviewerRe: [PATCH mptcp-next v5 3/5] Squash to "bpf: Add mptcp_subflow bpf_iter"
Hi Mat,
Thanks for the review.
Quoted 77 lines · Mat Martineau
Quoted 68 lines · Geliang Tang
From: Geliang Tang <[email protected]>
Drop the NULL check for 'msk' as Martin suggested, add more checks for 'sk'.
Use the "struct sock *sk" instead of "struct mptcp-sock *msk" as the
argument in thebpf_iter_mptcp_subflow_newas Martin suggested.v5:
- checkbpf_iter_taskinmptcp_subflow_new()v4:
- dropsock_owned_by_user_nocheckandspin_is_locked. According to comments from Mat [2] and Martin [1], in this setmptcp_subflowbpf_iteronly used from a cg sockopt bpf prog, no need to add these
check at this moment.[1]
https://lore.kernel.org/all/[email protected]/
[2]
https://patchwork.kernel.org/project/mptcp/patch/f6469225598beecbf0bda12a4c33fafa86c0ff15.1739787744.git.tanggeliang@kylinos.cn/v3:
- continue to usesock_owned_by_user_nocheckandspin_is_lockedchecks instead of usingmsk_owned_by_me().v2:
- check the owner before assigning the msk as Mat suggested.Signed-off-by Geliang Tang <[email protected]>---
net/mptcp/bpf.c| 21 +++++++++++++++------1 file changed, 15 insertions(+), 6 deletions(-)net/mptcp/bpf.c +0
-0@@ -249,24 +249,33 @@ bpf_mptcp_subflow_ctx(const struct sock *sk)
__bpf_kfuncstatic int bpf_iter_mptcp_subflow_new(structbpf_iter_mptcp_subflow*it,net/mptcp/bpf.c +1
-1- struct mptcp_sock *msk) + struct sock *sk){ struct bpf_iter_mptcp_subflow_kern *kit = (void *)it;net/mptcp/bpf.c +2
-1-struct sock *sk = (struct sock *)msk; +struct task_struct *task; +struct mptcp_sock *msk;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));net/mptcp/bpf.c +1
-2- kit->msk = msk; -if (!msk) +if (unlikely (!sk || !sk_fullsock (sk)))return -EINVAL;net/mptcp/bpf.c +1
-2-if (!sock_owned_by_user_nocheck (sk) && - !spin_is_locked(&sk->sk_lock.slock)) +if (sk->sk_protocol !=IPPROTO_MPTCP )return -EINVAL;net/mptcp/bpf.c bpf_mptcp_subflow_ctx(const struct sock *sk) · line 272
Quoted 4 lines
net/mptcp/bpf.c +4
-0+ msk =mptcp_sk (sk); + task =mptcp_get_bpf_iter_task (msk); +if (!task || task != current) +return -EINVAL ;Hi Geliang -
Quoted 5 lines
The task checking logic would fit better inside the helper, since every
caller will be checking to see if it matches 'current'.I also think it would read better as (task && task == current)
Yes, indeed.
Quoted 8 lines
net/mptcp/bpf.c bpf_mptcp_subflow_ctx(const struct sock *sk) · line 274
Quoted 4 lines
net/mptcp/bpf.c +2
-0+ +mptcp_clear_bpf_iter_task (msk);This needs to be cleared where the lock is released, not where the task is
checked.
Yes, indeed, I'll do that in the next version.
Thanks,
-Geliang
Quoted 15 lines
- Mat
Quoted 12 lines
net/mptcp/bpf.c +4
-0+ +msk_owned_by_me (msk); + + kit->msk = msk;kit->pos = &msk->conn_list; return 0; }Signature · 1 line
2.43.0
Geliang Tang
reviewerPATCH v5 4/5 Revert "bpf: Acquire and release mptcp socket"
In reply to Geliang TangFrom: Geliang Tang <[email protected]>
Drop this patch as Martin suggested.
From Martin's review [1], this mptcp_sock_acquire() helper was a workaround only to please the verifier, but they were not needed.
[1]
https://lore.kernel.org/[email protected]
--- net/mptcp/bpf.c | 19 ------------------- 1 file changed, 19 deletions(-)
net/mptcp/bpf.c +0 -19
@@ -297,23 +297,6 @@ bpf_iter_mptcp_subflow_destroy(struct bpf_iter_mptcp_subflow *it) { } -__bpf_kfunc static struct -mptcp_sock *bpf_mptcp_sock_acquire (struct mptcp_sock *msk) -{ -struct sock *sk = (struct sock *)msk; - -if (sk &&refcount_inc_not_zero (&sk->sk_refcnt)) -return msk; -return NULL ; -} - -__bpf_kfunc static void bpf_mptcp_sock_release(struct mptcp_sock *msk) -{ -struct sock *sk = (struct sock *)msk; - -WARN_ON_ONCE (!sk || !refcount_dec_not_one (&sk->sk_refcnt)); -} - __bpf_kfunc struct mptcp_subflow_context *bpf_mptcp_subflow_ctx_by_pos (const struct mptcp_sched_data *data,unsigned int pos) {
@@ -334,8 +317,6 @@ BTF_ID_FLAGS(func, bpf_mptcp_subflow_ctx, 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) -BTF_ID_FLAGS(func, bpf_mptcp_sock_acquire, KF_ACQUIRE | KF_RET_NULL) -BTF_ID_FLAGS(func, bpf_mptcp_sock_release, KF_RELEASE)BTF_KFUNCS_END (bpf_mptcp_common_kfunc_ids) static const struct btf_kfunc_id_set bpf_mptcp_common_kfunc_set = {
Signature · 1 line
2.43.0
Geliang Tang
reviewerPATCH v5 5/5 Squash to "selftests/bpf: Add mptcp_subflow bpf_iter subtest"
In reply to Geliang TangFrom: Geliang Tang <[email protected]>
Use bpf_core_cast() instead of bpf_skc_to_mptcp_sock(). Change the 2nd parameter type of bpf_for_each() as 'struct sock'. Drop use of bpf_mptcp_sock_acquire/release.
Drop declaration of bpf_mptcp_subflow_tcp_sock. It's no longer used. Update the comment for mptcp_subflow_tcp_sock(), which is a BPF helper, not a kfunc.
Please update the commit log as:
'''
This patch adds a "cgroup/getsockopt" program "iters_subflow" to test the newly added mptcp_subflow bpf_iter.
Export mptcp_subflow helpers bpf_iter_mptcp_subflow_new/_next/_destroy and other helpers into bpf_experimental.h.
Use bpf_for_each() to walk the subflow list of this msk. MPTCP-specific packet scheduler kfunc can be called in the loop. In this test, just add all subflow ids to local variable local_ids, then invoke the helper mptcp_subflow_tcp_sock() in the loop to pick a subsocket.
Out of the loop, use bpf_mptcp_subflow_ctx() to get the subflow context of the picked subsocket and do some verification. Finally, assign local_ids to global variable ids so that the application can obtain this value.
Add a subtest named test_iters_subflow to load and verify the newly added mptcp_subflow type bpf_iter example in test_mptcp. Use the helper endpoint_init() to add 3 new subflow endpoints. Send a byte of message to start the mptcp connection, and wait for new subflows to be added. getsockopt() is invoked to trigger the "cgroup/getsockopt" test program "iters_subflow". Check if skel->bss->ids equals 10 to verify whether this mptcp_subflow bpf_iter loops correctly as expected.
'''
--- tools/testing/selftests/bpf/bpf_experimental.h | 2 +- tools/testing/selftests/bpf/progs/mptcp_bpf.h | 5 ----- tools/testing/selftests/bpf/progs/mptcp_bpf_iters.c | 8 ++------ 3 files changed, 3 insertions(+), 12 deletions(-)
tools/testing/selftests/bpf/bpf_experimental.h +1 -1
@@ -577,7 +577,7 @@ extern void bpf_iter_css_destroy(struct bpf_iter_css *it) __weak __ksym;struct bpf_iter_mptcp_subflow ; extern int bpf_iter_mptcp_subflow_new(struct bpf_iter_mptcp_subflow *it, - struct mptcp_sock *msk) __weak __ksym; + struct sock *sk) __weak __ksym;extern struct mptcp_subflow_context *bpf_iter_mptcp_subflow_next (struct bpf_iter_mptcp_subflow *it)__weak __ksym; extern void
tools/testing/selftests/bpf/progs/mptcp_bpf.h +0 -5
@@ -43,13 +43,8 @@ mptcp_subflow_tcp_sock(const struct mptcp_subflow_context *subflow) }/* ksym */ -extern struct mptcp_sock *bpf_mptcp_sock_acquire (struct mptcp_sock *msk) __ksym; -extern void bpf_mptcp_sock_release(struct mptcp_sock *msk) __ksym; -extern struct mptcp_subflow_context *bpf_mptcp_subflow_ctx (const struct sock *sk) __ksym; -extern struct sock * -bpf_mptcp_subflow_tcp_sock (const struct mptcp_subflow_context *subflow) __ksym; extern void mptcp_subflow_set_scheduled(struct mptcp_subflow_context *subflow, bool scheduled) __ksym;
tools/testing/selftests/bpf/progs/mptcp_bpf_iters.c +2 -6
@@ -28,10 +28,7 @@ int iters_subflow(struct bpf_sockopt *ctx)if (!msk || msk->pm.server_side || !msk->pm.subflows)return 1 ; - msk =bpf_mptcp_sock_acquire (msk); -if (!msk) -return 1 ; -bpf_for_each (mptcp_subflow, subflow, msk) { +bpf_for_each (mptcp_subflow, subflow, (struct sock *)sk) {/* Here MPTCP-specific packet scheduler kfunc can be called: * this test is not doing anything really useful, only to * verify the iteration works.
@@ -39,7 +36,7 @@ int iters_subflow(struct bpf_sockopt *ctx) local_ids += subflow->subflow_id; -/* only to check the following kfunc works */ +/* only to check the following helper works */ ssk =mptcp_subflow_tcp_sock (subflow); }
@@ -58,6 +55,5 @@ int iters_subflow(struct bpf_sockopt *ctx) ids = local_ids; out: -bpf_mptcp_sock_release (msk);return 1 ; }
Signature · 1 line
2.43.0
Matthieu Baerts
maintainerHi Geliang, Mat,
Quoted 4 lines · Geliang Tang
From: Geliang Tang <[email protected]>
v5:
- checkbpf_iter_taskinmptcp_subflow_new()as Mat suggested.
Thank you for the modification, but would it not be easier not to do that in this series, not to increase the complexity?
This can be done in the "use bpf_iter in bpf schedulers" instead, no?
For me, the v4 (with my squash-to patch) is enough to be used with CG [gs]etsocktopt.
Also, I think the idea is set this marker before calling the future struct_ops, in places where we know the msk is owned, but not in bpf_mptcp_sock_from_sock() which doesn't check if the msk is owned, no?
Cheers,
Matt
Signature · 1 line
Sponsored by the NGI0 Core fund.
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/13629090242
Initiator: Patchew Applier
Commits: https://github.com/multipath-tcp/mptcp_net-next/commits/db85e099ebc4
Patchwork: https://patchwork.kernel.org/project/mptcp/list/?series=939536
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-normalFor 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)