add bpf_iter_task
Subsystems (4)
Geliang Tang
reviewerv3 cover add bpf_iter_task
From: Geliang Tang <[email protected]>
v3:
- add bpf_iter_task in mptcp_sock, no need to add back struct mptcp_sched_data then.
- add sk_lock_sock() and sk_release_sock() for struct sock.
- set and clear bpf_iter_task for MPTCP BPF Cgroup getsockopt and setsockopt.
v2:
- Keep mptcp scheduler API unchanged.
- Add back struct mptcp_sched_data.
- add bpf_iter_task in mptcp_sched_data instead of mptcp_sock
- Add wrapper bpf_iter mptcp_subflow_sched.
- Use mptcp_subflow_sched iter instead of mptcp_subflow.
- https://patchwork.kernel.org/project/mptcp/cover/[email protected]/
v1:
- https://patchwork.kernel.org/project/mptcp/cover/[email protected]/
Geliang Tang (2):
mptcp: add bpf_iter_task for mptcp_sock
bpf: Customize mptcp's own sock lock
include/net/sock.h | 2 ++ kernel/bpf/cgroup.c | 8 ++++---- net/mptcp/bpf.c | 2 ++ net/mptcp/protocol.c | 16 ++++++++++++++++ net/mptcp/protocol.h | 20 ++++++++++++++++++++ net/mptcp/sched.c | 15 +++++++++++---- 6 files changed, 55 insertions(+), 8 deletions(-)
Signature · 1 line
2.43.0
Geliang Tang
reviewerPATCH v3 1/2 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/bpf.c | 2 ++ net/mptcp/protocol.c | 1 + net/mptcp/protocol.h | 20 ++++++++++++++++++++ net/mptcp/sched.c | 15 +++++++++++---- 4 files changed, 34 insertions(+), 4 deletions(-)
net/mptcp/bpf.c +2 -0
@@ -261,6 +261,8 @@ bpf_iter_mptcp_subflow_new(struct bpf_iter_mptcp_subflow *it,return -EINVAL ; msk =mptcp_sk (sk); +if (!mptcp_check_bpf_iter_task (msk)) +return -EINVAL ;msk_owned_by_me (msk);
net/mptcp/protocol.c +1 -0
@@ -2729,6 +2729,7 @@ static void __mptcp_init_sock(struct sock *sk)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); +mptcp_clear_bpf_iter_task (msk); msk->recovery = false; msk->subflow_id =1 ; msk->last_data_sent = tcp_jiffies32;
net/mptcp/protocol.h +20 -0
@@ -334,6 +334,7 @@ struct mptcp_sock {*/ struct mptcp_pm_data pm;struct mptcp_sched_ops *sched; +struct task_struct *bpf_iter_task;struct {u32 space;/* bytes copied in last measurement window */ u32 copied;/* bytes copied in this measurement window */
@@ -1291,4 +1292,23 @@ 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 bool mptcp_check_bpf_iter_task (struct mptcp_sock *msk) +{ +struct task_struct *task =READ_ONCE (msk->bpf_iter_task); + +if (task && task == current) +return true; +return false; +} +#endif /* __MPTCP_PROTOCOL_H */
net/mptcp/sched.c +11 -4
@@ -155,6 +155,7 @@ void mptcp_subflow_set_scheduled(struct mptcp_subflow_context *subflow,int mptcp_sched_get_send (struct mptcp_sock *msk) {struct mptcp_subflow_context *subflow; +int ret;msk_owned_by_me (msk);
@@ -176,12 +177,16 @@ int mptcp_sched_get_send(struct mptcp_sock *msk)if (msk->sched == &mptcp_sched_default || !msk->sched)return mptcp_sched_default_get_send (msk); -return msk->sched->get_send (msk); +mptcp_set_bpf_iter_task (msk); + ret = msk->sched->get_send (msk); +mptcp_clear_bpf_iter_task (msk); +return ret; }int mptcp_sched_get_retrans (struct mptcp_sock *msk) {struct mptcp_subflow_context *subflow; +int ret;msk_owned_by_me (msk);
@@ -196,7 +201,9 @@ int mptcp_sched_get_retrans(struct mptcp_sock *msk)if (msk->sched == &mptcp_sched_default || !msk->sched)return mptcp_sched_default_get_retrans (msk); -if (msk->sched->get_retrans) -return msk->sched->get_retrans (msk); -return msk->sched->get_send (msk); +mptcp_set_bpf_iter_task (msk); + ret = msk->sched->get_retrans ? msk->sched->get_retrans (msk) : + msk->sched->get_send (msk); +mptcp_clear_bpf_iter_task (msk); +return ret; }
Signature · 1 line
2.43.0
Geliang Tang
reviewerRe: [PATCH mptcp-next v3 1/2] mptcp: add bpf_iter_task for mptcp_sock
Quoted 84 lines · Geliang Tang
From: Geliang Tang <[email protected]>
To make sure the
mptcp_subflowbpf_iteris 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 structmptcp_sock.2. Do a
WRITE_ONCE(msk->bpf_iter_task, current)before calling a MPTCP BPF hook, andWRITE_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
-EINVALif it doesn't match.Also creates helpers for setting, clearing and checking that value.
Suggested-by Mat Martineau <[email protected]>Signed-off-by Geliang Tang <[email protected]>---
net/mptcp/bpf.c | 2 ++
net/mptcp/protocol.c | 1 +
net/mptcp/protocol.h | 20 ++++++++++++++++++++
net/mptcp/sched.c | 15 +++++++++++----4 files changed, 34 insertions(+), 4 deletions(-)net/mptcp/bpf.c +0
-0@@ -261,6 +261,8 @@ bpf_iter_mptcp_subflow_new(structbpf_iter_mptcp_subflow *it, return -EINVAL;
msk =mptcp_sk(sk);net/mptcp/bpf.c +2
-0+if (!mptcp_check_bpf_iter_task (msk)) +return -EINVAL ;
msk_owned_by_me(msk);net/mptcp/protocol.c +1
-0@@ -2729,6 +2729,7 @@ static void __mptcp_init_sock(struct sock *sk) 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); +mptcp_clear_bpf_iter_task (msk); msk->recovery = false; msk->subflow_id = 1;
msk->last_data_sent=tcp_jiffies32;net/mptcp/protocol.h +1
-0@@ -334,6 +334,7 @@ struct mptcp_sock {*/ struct mptcp_pm_data pm; struct mptcp_sched_ops *sched; +struct task_struct *bpf_iter_task; struct {u32 space; /* bytes copied in last measurement
window */
u32 copied; /* bytes copied in this measurement
window */net/mptcp/protocol.h +0
-0@@ -1291,4 +1292,23 @@ mptcp_token_join_cookie_init_state(struct
mptcp_subflow_request_sock*subflow_restatic inline voidmptcp_join_cookie_init(void){} #endifnet/mptcp/protocol.h +8
-0+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 );
net/mptcp/protocol.h mptcp_token_join_cookie_init_state(struct mptcp_subflow_request_sock *subflow_re · line 1312
Quoted 10 lines
net/mptcp/protocol.h +10
-0+} + +static inline bool mptcp_check_bpf_iter_task (struct mptcp_sock *msk) +{ +struct task_struct *task =READ_ONCE (msk->bpf_iter_task); + +if (task && task == current) +return true; +return false; +}
This v3 has a bug. When I was testing MPTCP BPF selftests in a loop, I found that the test would break in some cases. After debugging, I found that "task" and "current" were not equal:
[ 520.209749][T11984] MPTCP: bpf_iter_mptcp_subflow_new
msk=00000000fc8f7370 in_interrupt=0 task=00000000ef28139f current=0000000024db2987
I will try to fix it, but haven't found a solution yet.
Thanks,
-Geliang
Quoted 49 lines
+
#endif /*__MPTCP_PROTOCOL_H*/net/mptcp/sched.c +1
-0@@ -155,6 +155,7 @@ void mptcp_subflow_set_scheduled(structmptcp_subflow_context *subflow, int mptcp_sched_get_send(struct mptcp_sock *msk) { struct mptcp_subflow_context *subflow; +int ret;
msk_owned_by_me(msk);net/mptcp/sched.c +0
-0@@ -176,12 +177,16 @@ int mptcp_sched_get_send(struct mptcp_sock *msk)
if (msk->sched==&mptcp_sched_default|| !msk->sched) returnmptcp_sched_default_get_send(msk);net/mptcp/sched.c +4
-1-return msk->sched->get_send (msk); +mptcp_set_bpf_iter_task (msk); + ret = msk->sched->get_send (msk); +mptcp_clear_bpf_iter_task (msk); +return ret;}
intmptcp_sched_get_retrans(struct mptcp_sock *msk){
structmptcp_subflow_context*subflow;net/mptcp/sched.c +1
-0+int ret;
msk_owned_by_me(msk);net/mptcp/sched.c +0
-0@@ -196,7 +201,9 @@ int mptcp_sched_get_retrans(struct mptcp_sock *msk)
if (msk->sched==&mptcp_sched_default|| !msk->sched) returnmptcp_sched_default_get_retrans(msk);net/mptcp/sched.c +2
-3-if (msk->sched->get_retrans) -return msk->sched->get_retrans (msk); -return msk->sched->get_send (msk); +mptcp_set_bpf_iter_task (msk); + ret = msk->sched->get_retrans ? msk->sched->get_retrans(msk):
net/mptcp/sched.c +3
-0+ msk->sched->get_send (msk); +mptcp_clear_bpf_iter_task (msk); +return ret;}
Matthieu Baerts
maintainerRe: [PATCH mptcp-next v3 1/2] mptcp: add bpf_iter_task for mptcp_sock
Hi Geliang, Mat,
Quoted 100 lines · Geliang Tang
Quoted 84 lines · Geliang Tang
From: Geliang Tang <[email protected]>
To make sure the
mptcp_subflowbpf_iteris 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 structmptcp_sock.2. Do a
WRITE_ONCE(msk->bpf_iter_task, current)before calling a MPTCP BPF hook, andWRITE_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
-EINVALif it doesn't match.Also creates helpers for setting, clearing and checking that value.
Suggested-by Mat Martineau <[email protected]>Signed-off-by Geliang Tang <[email protected]>---
net/mptcp/bpf.c | 2 ++
net/mptcp/protocol.c | 1 +
net/mptcp/protocol.h | 20 ++++++++++++++++++++
net/mptcp/sched.c | 15 +++++++++++----4 files changed, 34 insertions(+), 4 deletions(-)net/mptcp/bpf.c +0
-0@@ -261,6 +261,8 @@ bpf_iter_mptcp_subflow_new(structbpf_iter_mptcp_subflow *it, return -EINVAL;
msk =mptcp_sk(sk);net/mptcp/bpf.c +2
-0+if (!mptcp_check_bpf_iter_task (msk)) +return -EINVAL ;
msk_owned_by_me(msk);net/mptcp/protocol.c +1
-0@@ -2729,6 +2729,7 @@ static void __mptcp_init_sock(struct sock *sk) 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); +mptcp_clear_bpf_iter_task (msk); msk->recovery = false; msk->subflow_id = 1;
msk->last_data_sent=tcp_jiffies32;net/mptcp/protocol.h +1
-0@@ -334,6 +334,7 @@ struct mptcp_sock {*/ struct mptcp_pm_data pm; struct mptcp_sched_ops *sched; +struct task_struct *bpf_iter_task; struct {u32 space; /* bytes copied in last measurement
window */
u32 copied; /* bytes copied in this measurement
window */net/mptcp/protocol.h +0
-0@@ -1291,4 +1292,23 @@ mptcp_token_join_cookie_init_state(struct
mptcp_subflow_request_sock*subflow_restatic inline voidmptcp_join_cookie_init(void){} #endifnet/mptcp/protocol.h +8
-0+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 );net/mptcp/protocol.h mptcp_token_join_cookie_init_state(struct mptcp_subflow_request_sock *subflow_re · line 1312
Quoted 10 lines
net/mptcp/protocol.h +10
-0+} + +static inline bool mptcp_check_bpf_iter_task (struct mptcp_sock *msk) +{ +struct task_struct *task =READ_ONCE (msk->bpf_iter_task); + +if (task && task == current) +return true; +return false; +}This v3 has a bug. When I was testing MPTCP BPF selftests in a loop, I found that the test would break in some cases. After debugging, I found that "task" and "current" were not equal:
Quoted 5 lines
[ 520.209749][T11984] MPTCP: bpf_iter_mptcp_subflow_newmsk=00000000fc8f7370 in_interrupt=0 task=00000000ef28139f current=0000000024db2987
I will try to fix it, but haven't found a solution yet.
(sorry for the delay, I need a bit of time to catch up)
I talked a bit to Alexei Starovoitov last week. He told me that with the BPF struct_ops, it is possible to tell the verifier that some locks are taken either by some struct_ops types, or even per callbacks of some specific struct_ops (WIP on sched_ext side). It is also possible to get some locks automatically (polymorphism), and there are examples on VFS side.
In other words, it means we don't need to add this "bpf_iter_task", there are other techniques, but I don't have more details, and I didn't check in the code. If it is not clear for you and you don't find other examples elsewhere (sched_ext? check WIP patches maybe?), then Alexei said we should not hesitate to ask questions on the BPF mailing list.
Cheers,
Matt
Signature · 1 line
Sponsored by the NGI0 Core fund.
Geliang Tang
reviewerRe: [PATCH mptcp-next v3 1/2] mptcp: add bpf_iter_task for mptcp_sock
Hi Matt,
Quoted 121 lines · Matthieu Baerts
Hi Geliang, Mat,
Quoted 110 lines · Geliang Tang
Quoted 97 lines · Geliang Tang
From: Geliang Tang <[email protected]>
To make sure the
mptcp_subflowbpf_iteris 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 structmptcp_sock.2. Do a
WRITE_ONCE(msk->bpf_iter_task, current)before calling a MPTCP BPF hook, andWRITE_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
-EINVALif it doesn't match.Also creates helpers for setting, clearing and checking that value.
Suggested-by Mat Martineau <[email protected]>Signed-off-by Geliang Tang <[email protected]>---
net/mptcp/bpf.c | 2 ++
net/mptcp/protocol.c | 1 +
net/mptcp/protocol.h | 20 ++++++++++++++++++++
net/mptcp/sched.c | 15 +++++++++++----4 files changed, 34 insertions(+), 4 deletions(-)net/mptcp/bpf.c +0
-0@@ -261,6 +261,8 @@ bpf_iter_mptcp_subflow_new(structbpf_iter_mptcp_subflow *it, return -EINVAL;
msk =mptcp_sk(sk);net/mptcp/bpf.c +2
-0+if (!mptcp_check_bpf_iter_task (msk)) +return -EINVAL ;
msk_owned_by_me(msk);net/mptcp/protocol.c +1
-0@@ -2729,6 +2729,7 @@ static void __mptcp_init_sock(struct sock *sk) 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); +mptcp_clear_bpf_iter_task (msk); msk->recovery = false;
msk->subflow_id= 1;
msk->last_data_sent=tcp_jiffies32;net/mptcp/protocol.h +1
-0@@ -334,6 +334,7 @@ struct mptcp_sock {*/ struct mptcp_pm_data pm; struct mptcp_sched_ops *sched; +struct task_struct *bpf_iter_task; struct {u32 space; /* bytes copied in last
measurement
window */
u32 copied; /* bytes copied in this
measurement
window */net/mptcp/protocol.h +0
-0@@ -1291,4 +1292,23 @@ mptcp_token_join_cookie_init_state(struct
mptcp_subflow_request_sock*subflow_re
static inline voidmptcp_join_cookie_init(void){} #endif
+static inline voidmptcp_set_bpf_iter_task(struct mptcp_sock *msk)net/mptcp/protocol.h +5
-0+{ +WRITE_ONCE (msk->bpf_iter_task, current); +} + +static inline void mptcp_clear_bpf_iter_task(struct mptcp_sock*msk)
net/mptcp/protocol.h +5
-0+{ +WRITE_ONCE (msk->bpf_iter_task,NULL ); +} + +static inline bool mptcp_check_bpf_iter_task(struct mptcp_sock*msk)
net/mptcp/protocol.h +2
-0+{ + struct task_struct *task = READ_ONCE(msk-Quoted 1 line
bpf_iter_task);net/mptcp/protocol.h mptcp_token_join_cookie_init_state(struct mptcp_subflow_request_sock *subflow_re · line 1312
Quoted 5 lines
net/mptcp/protocol.h +5
-0+ +if (task && task == current) +return true; +return false; +}This v3 has a bug. When I was testing MPTCP BPF selftests in a loop, I
found that the test would break in some cases. After debugging, I found
that "task" and "current" were not equal:Quoted 5 lines
[ 520.209749][T11984] MPTCP:
bpf_iter_mptcp_subflow_newmsk=00000000fc8f7370 in_interrupt=0 task=00000000ef28139f current=0000000024db2987I will try to fix it, but haven't found a solution yet.
(sorry for the delay, I need a bit of time to catch up)
Quoted 9 lines
I talked a bit to Alexei Starovoitov last week. He told me that with the
BPFstruct_ops, it is possible to tell the verifier that some locks are
taken either by somestruct_opstypes, or even per callbacks of some specificstruct_ops(WIP onsched_extside). It is also possible to get
some locks automatically (polymorphism), and there are examples on VFS side.
Thanks for your reminder, I will look at these BPF codes. Our goal is to make mptcp_subflow bpf_iter only used by struct_ops defined by MPTCP BPF (bpf_mptcp_sched_ops and bpf_mptcp_pm_ops), right? Other struct_ops are not allowed to use mptcp_subflow bpf_iter.
Quoted 8 lines
In other words, it means we don't need to add this "
bpf_iter_task", there are other techniques, but I don't have more details, and I didn't
check in the code. If it is not clear for you and you don't find other
examples elsewhere (sched_ext? check WIP patches maybe?), then Alexei said we should not hesitate to ask questions on the BPF mailing list.
We can send "Add mptcp_subflow bpf_iter support" v3 to the BPF mailing list and ask questions during the reviewing process.
Thanks,
-Geliang
Quoted 3 lines
Cheers,
Matt
Matthieu Baerts
maintainerRe: [PATCH mptcp-next v3 1/2] mptcp: add bpf_iter_task for mptcp_sock
Hi Geliang,
Quoted 28 lines · Geliang Tang
Hi Matt,
Quoted 25 lines · Matthieu Baerts
Hi Geliang, Mat,
Quoted 22 lines · Geliang Tang
Quoted 21 lines · Geliang Tang
From: Geliang Tang <[email protected]>
To make sure the
mptcp_subflowbpf_iteris 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 structmptcp_sock.2. Do a
WRITE_ONCE(msk->bpf_iter_task, current)before calling a MPTCP BPF hook, andWRITE_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
-EINVALif it doesn't match.Also creates helpers for setting, clearing and checking that value.
(...)
Quoted 35 lines
Quoted 25 lines
Quoted 17 lines
net/mptcp/protocol.h mptcp_token_join_cookie_init_state(struct mptcp_subflow_request_sock *subflow_re · line 1312
Quoted 10 lines
+static inline bool
mptcp_check_bpf_iter_task(struct mptcp_sock *msk)(quoted diff) +2
-0+{ + struct task_struct *task = READ_ONCE(msk-Quoted 1 line
bpf_iter_task);net/mptcp/protocol.h +5
-0+ +if (task && task == current) +return true; +return false; +}This v3 has a bug. When I was testing MPTCP BPF selftests in a loop, I
found that the test would break in some cases. After debugging, I found
that "task" and "current" were not equal:Quoted 5 lines
[ 520.209749][T11984] MPTCP:
bpf_iter_mptcp_subflow_newmsk=00000000fc8f7370 in_interrupt=0 task=00000000ef28139f current=0000000024db2987I will try to fix it, but haven't found a solution yet.
(sorry for the delay, I need a bit of time to catch up)
Quoted 9 lines
I talked a bit to Alexei Starovoitov last week. He told me that with the
BPFstruct_ops, it is possible to tell the verifier that some locks are
taken either by somestruct_opstypes, or even per callbacks of some specificstruct_ops(WIP onsched_extside). It is also possible to get
some locks automatically (polymorphism), and there are examples on VFS side.
Quoted 4 lines
Thanks for your reminder, I will look at these BPF codes. Our goal is to make
mptcp_subflowbpf_iteronly used bystruct_opsdefined by MPTCP BPF (bpf_mptcp_sched_opsandbpf_mptcp_pm_ops), right? Otherstruct_opsare not allowed to usemptcp_subflowbpf_iter.
Yes, that's correct. I didn't check, but **maybe** some mptcp_sched_ops and mptcp_pm_ops callback might not be allowed to use bpf_iter. In this case, it might be needed to allow only some of them to use bpf_iter.
Note that it sounds like all mptcp_{pm,sched}_ops callbacks should be done while holding the msk lock, e.g. being called from the worker and not from a subflow event, etc. but maybe there are some exceptions needed.
Quoted 10 lines
Quoted 7 lines
In other words, it means we don't need to add this "
bpf_iter_task", there are other techniques, but I don't have more details, and I didn't
check in the code. If it is not clear for you and you don't find other
examples elsewhere (sched_ext? check WIP patches maybe?), then Alexei said we should not hesitate to ask questions on the BPF mailing list.We can send "Add
mptcp_subflowbpf_itersupport" v3 to the BPF mailing list and ask questions during the reviewing process.
Indeed. I hope to be able to find a bit of time this week to send the v3.
Cheers,
Matt
Signature · 1 line
Sponsored by the NGI0 Core fund.
Mat Martineau
maintainerRe: [PATCH mptcp-next v3 1/2] mptcp: add bpf_iter_task for mptcp_sock
Quoted 74 lines · Matthieu Baerts
Hi Geliang,
Quoted 28 lines · Geliang Tang
Hi Matt,
Quoted 25 lines · Matthieu Baerts
Hi Geliang, Mat,
Quoted 22 lines · Geliang Tang
Quoted 21 lines · Geliang Tang
From: Geliang Tang <[email protected]>
To make sure the
mptcp_subflowbpf_iteris 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 structmptcp_sock.2. Do a
WRITE_ONCE(msk->bpf_iter_task, current)before calling a MPTCP BPF hook, andWRITE_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
-EINVALif it doesn't match.Also creates helpers for setting, clearing and checking that value.
(...)
Quoted 35 lines
Quoted 25 lines
Quoted 17 lines
net/mptcp/protocol.h mptcp_token_join_cookie_init_state(struct mptcp_subflow_request_sock *subflow_re · line 1312
Quoted 10 lines
+static inline bool
mptcp_check_bpf_iter_task(struct mptcp_sock *msk)(quoted diff) +2
-0+{ + struct task_struct *task = READ_ONCE(msk-Quoted 1 line
bpf_iter_task);net/mptcp/protocol.h +5
-0+ +if (task && task == current) +return true; +return false; +}This v3 has a bug. When I was testing MPTCP BPF selftests in a loop, I
found that the test would break in some cases. After debugging, I found
that "task" and "current" were not equal:Quoted 5 lines
[ 520.209749][T11984] MPTCP:
bpf_iter_mptcp_subflow_newmsk=00000000fc8f7370 in_interrupt=0 task=00000000ef28139f current=0000000024db2987I will try to fix it, but haven't found a solution yet.
(sorry for the delay, I need a bit of time to catch up)
Quoted 9 lines
I talked a bit to Alexei Starovoitov last week. He told me that with the
BPFstruct_ops, it is possible to tell the verifier that some locks are
taken either by somestruct_opstypes, or even per callbacks of some specificstruct_ops(WIP onsched_extside). It is also possible to get
some locks automatically (polymorphism), and there are examples on VFS side.Quoted 4 lines
Thanks for your reminder, I will look at these BPF codes. Our goal is to make
mptcp_subflowbpf_iteronly used bystruct_opsdefined by MPTCP BPF (bpf_mptcp_sched_opsandbpf_mptcp_pm_ops), right? Otherstruct_opsare not allowed to usemptcp_subflowbpf_iter.
Quoted 7 lines
Yes, that's correct. I didn't check, but **maybe** some
mptcp_sched_opsandmptcp_pm_opscallback might not be allowed to usebpf_iter. In this case, it might be needed to allow only some of them to usebpf_iter.Note that it sounds like all mptcp_{pm,sched}_ops callbacks should be done while holding the msk lock, e.g. being called from the worker and not from a subflow event, etc. but maybe there are some exceptions needed.
Hi Matthieu -
Is this last paragraph also feedback from Alexei? Can you elaborate a little - is the lock needed for BPF-specific reasons or to avoid having the callback modify msk state?
I'll try to look around at the verifier and automatic locking techniques you mentioned above, it would be great to have a better way to handle socket locks w/ BPF.
- Mat
Quoted 20 lines
Quoted 11 lines
Quoted 8 lines
In other words, it means we don't need to add this "
bpf_iter_task", there are other techniques, but I don't have more details, and I didn't
check in the code. If it is not clear for you and you don't find other
examples elsewhere (sched_ext? check WIP patches maybe?), then Alexei said we should not hesitate to ask questions on the BPF mailing list.We can send "Add
mptcp_subflowbpf_itersupport" v3 to the BPF mailing list and ask questions during the reviewing process.Indeed. I hope to be able to find a bit of time this week to send the v3.
Cheers,
MattSignature · 1 line
Sponsored by the NGI0 Core fund.
Matthieu Baerts
maintainerRe: [PATCH mptcp-next v3 1/2] mptcp: add bpf_iter_task for mptcp_sock
Hi Mat,
Quoted 85 lines · Mat Martineau
Quoted 74 lines · Matthieu Baerts
Hi Geliang,
Quoted 28 lines · Geliang Tang
Hi Matt,
Quoted 25 lines · Matthieu Baerts
Hi Geliang, Mat,
Quoted 22 lines · Geliang Tang
Quoted 21 lines · Geliang Tang
From: Geliang Tang <[email protected]>
To make sure the
mptcp_subflowbpf_iteris 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 structmptcp_sock.2. Do a
WRITE_ONCE(msk->bpf_iter_task, current)before calling a MPTCP BPF hook, andWRITE_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
-EINVALif it doesn't match.Also creates helpers for setting, clearing and checking that value.
(...)
Quoted 35 lines
Quoted 25 lines
Quoted 17 lines
net/mptcp/protocol.h mptcp_token_join_cookie_init_state(struct mptcp_subflow_request_sock *subflow_re · line 1312
Quoted 10 lines
+static inline bool
mptcp_check_bpf_iter_task(struct mptcp_sock *msk)
+{
+ structtask_struct*task = READ_ONCE(msk-Quoted 1 line
bpf_iter_task);net/mptcp/protocol.h +5
-0+ + if (task && task == current) + return true; + return false; +}This v3 has a bug. When I was testing MPTCP BPF selftests in a loop, I
found that the test would break in some cases. After debugging, I found
that "task" and "current" were not equal:Quoted 5 lines
[ 520.209749][T11984] MPTCP:
bpf_iter_mptcp_subflow_newmsk=00000000fc8f7370 in_interrupt=0 task=00000000ef28139f current=0000000024db2987I will try to fix it, but haven't found a solution yet.
(sorry for the delay, I need a bit of time to catch up)
Quoted 9 lines
I talked a bit to Alexei Starovoitov last week. He told me that with the
BPFstruct_ops, it is possible to tell the verifier that some locks are
taken either by somestruct_opstypes, or even per callbacks of some specificstruct_ops(WIP onsched_extside). It is also possible to get
some locks automatically (polymorphism), and there are examples on VFS side.Quoted 4 lines
Thanks for your reminder, I will look at these BPF codes. Our goal is to make
mptcp_subflowbpf_iteronly used bystruct_opsdefined by MPTCP BPF (bpf_mptcp_sched_opsandbpf_mptcp_pm_ops), right? Otherstruct_opsare not allowed to usemptcp_subflowbpf_iter.Quoted 8 lines
Yes, that's correct. I didn't check, but **maybe** some
mptcp_sched_opsandmptcp_pm_opscallback might not be allowed to usebpf_iter. In this case, it might be needed to allow only some of them to usebpf_iter.Note that it sounds like all mptcp_{pm,sched}_ops callbacks should be done while holding the msk lock, e.g. being called from the worker and not from a subflow event, etc. but maybe there are some exceptions needed.
Quoted 5 lines
Hi Matthieu -
Is this last paragraph also feedback from Alexei? Can you elaborate a little - is the lock needed for BPF-specific reasons or to avoid having the callback modify msk state?
No, it is not from Alexei, but from me. Currently, some actions on a subflow will call some code in pm.c that will simply schedule the worker to be called from a different context to do other actions. To convert this to an mptcp_pm_ops, I think we should not have two callbacks -- one to schedule the worker, and one to react from the worker context -- but only one called from the worker context if such callback is set. This should also simplify the BPF PMs if all/most callbacks are done from the same context while owning the msk lock, no?
Quoted 3 lines
I'll try to look around at the verifier and automatic locking techniques you mentioned above, it would be great to have a better way to handle socket locks w/ BPF.
Alexei suggested to look at the VFS code. If I understood correctly, the idea is to have some locks automatically taken for some actions, but without having to dedicate new kfunc, checking if they are already taken, or requiring the BPF code to handle them (auto-releasing them then). But I don't have more details about that.
Cheers,
Matt
Signature · 1 line
Sponsored by the NGI0 Core fund.
Mat Martineau
maintainerRe: [PATCH mptcp-next v3 1/2] mptcp: add bpf_iter_task for mptcp_sock
Quoted 94 lines · Matthieu Baerts
Hi Mat,
Quoted 85 lines · Mat Martineau
Quoted 74 lines · Matthieu Baerts
Hi Geliang,
Quoted 28 lines · Geliang Tang
Hi Matt,
Quoted 25 lines · Matthieu Baerts
Hi Geliang, Mat,
Quoted 22 lines · Geliang Tang
Quoted 21 lines · Geliang Tang
From: Geliang Tang <[email protected]>
To make sure the
mptcp_subflowbpf_iteris 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 structmptcp_sock.2. Do a
WRITE_ONCE(msk->bpf_iter_task, current)before calling a MPTCP BPF hook, andWRITE_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
-EINVALif it doesn't match.Also creates helpers for setting, clearing and checking that value.
(...)
Quoted 35 lines
Quoted 25 lines
Quoted 17 lines
Review
Quoted 10 lines
+static inline bool
mptcp_check_bpf_iter_task(struct mptcp_sock *msk)
+{
+ structtask_struct*task = READ_ONCE(msk-Quoted 1 line
bpf_iter_task);(quoted diff) +5
-0+ + if (task && task == current) + return true; + return false; +}This v3 has a bug. When I was testing MPTCP BPF selftests in a loop, I
found that the test would break in some cases. After debugging, I found
that "task" and "current" were not equal:Quoted 5 lines
[ 520.209749][T11984] MPTCP:
bpf_iter_mptcp_subflow_newmsk=00000000fc8f7370 in_interrupt=0 task=00000000ef28139f current=0000000024db2987I will try to fix it, but haven't found a solution yet.
(sorry for the delay, I need a bit of time to catch up)
Quoted 9 lines
I talked a bit to Alexei Starovoitov last week. He told me that with the
BPFstruct_ops, it is possible to tell the verifier that some locks are
taken either by somestruct_opstypes, or even per callbacks of some specificstruct_ops(WIP onsched_extside). It is also possible to get
some locks automatically (polymorphism), and there are examples on VFS side.Quoted 4 lines
Thanks for your reminder, I will look at these BPF codes. Our goal is to make
mptcp_subflowbpf_iteronly used bystruct_opsdefined by MPTCP BPF (bpf_mptcp_sched_opsandbpf_mptcp_pm_ops), right? Otherstruct_opsare not allowed to usemptcp_subflowbpf_iter.Quoted 8 lines
Yes, that's correct. I didn't check, but **maybe** some
mptcp_sched_opsandmptcp_pm_opscallback might not be allowed to usebpf_iter. In this case, it might be needed to allow only some of them to usebpf_iter.Note that it sounds like all mptcp_{pm,sched}_ops callbacks should be done while holding the msk lock, e.g. being called from the worker and not from a subflow event, etc. but maybe there are some exceptions needed.
Quoted 5 lines
Hi Matthieu -
Is this last paragraph also feedback from Alexei? Can you elaborate a little - is the lock needed for BPF-specific reasons or to avoid having the callback modify msk state?
Quoted 9 lines
No, it is not from Alexei, but from me. Currently, some actions on a subflow will call some code in
pm.cthat will simply schedule the worker to be called from a different context to do other actions. To convert this to anmptcp_pm_ops, I think we should not have two callbacks -- one to schedule the worker, and one to react from the worker context -- but only one called from the worker context if such callback is set. This should also simplify the BPF PMs if all/most callbacks are done from the same context while owning the msk lock, no?
Ok, thanks for the details. I agree, for the PM callbacks it makes sense to only have a worker-context callback.
- Mat
Quoted 10 lines
Quoted 3 lines
I'll try to look around at the verifier and automatic locking techniques you mentioned above, it would be great to have a better way to handle socket locks w/ BPF.
Alexei suggested to look at the VFS code. If I understood correctly, the idea is to have some locks automatically taken for some actions, but without having to dedicate new kfunc, checking if they are already taken, or requiring the BPF code to handle them (auto-releasing them then). But I don't have more details about that.
Geliang Tang
reviewerRe: [PATCH mptcp-next v3 1/2] mptcp: add bpf_iter_task for mptcp_sock
In reply to Matthieu BaertsHi Matt,
Quoted 30 lines · Matthieu Baerts
Hi Geliang,
Quoted 27 lines · Geliang Tang
Hi Matt,
Quoted 24 lines · Matthieu Baerts
Hi Geliang, Mat,
Quoted 21 lines · Geliang Tang
Quoted 20 lines · Geliang Tang
From: Geliang Tang <[email protected]>
To make sure the
mptcp_subflowbpf_iteris 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 structmptcp_sock.2. Do a
WRITE_ONCE(msk->bpf_iter_task, current)before calling
a MPTCP BPF hook, andWRITE_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"
Quoted 57 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
to confirm the correct task, return
-EINVALif it doesn't match.Also creates helpers for setting, clearing and checking that value.
(...)
Quoted 49 lines
Quoted 27 lines
Quoted 19 lines
net/mptcp/protocol.h mptcp_token_join_cookie_init_state(struct mptcp_subflow_request_sock *subflow_re · line 1312
Quoted 11 lines
+static inline bool mptcp_check_bpf_iter_task(struct
mptcp_sock
*msk)(quoted diff) +2
-0+{ + struct task_struct *task = READ_ONCE(msk-Quoted 1 line
bpf_iter_task);net/mptcp/protocol.h +5
-0+ +if (task && task == current) +return true; +return false; +}This v3 has a bug. When I was testing MPTCP BPF selftests in a loop, I
found that the test would break in some cases. After debugging, I
found
that "task" and "current" were not equal:Quoted 5 lines
[ 520.209749][T11984] MPTCP:
bpf_iter_mptcp_subflow_newmsk=00000000fc8f7370 in_interrupt=0 task=00000000ef28139f current=0000000024db2987I will try to fix it, but haven't found a solution yet.
(sorry for the delay, I need a bit of time to catch up)
Quoted 14 lines
I talked a bit to Alexei Starovoitov last week. He told me that with
the
BPFstruct_ops, it is possible to tell the verifier that some locks
are
taken either by somestruct_opstypes, or even per callbacks of some
specificstruct_ops(WIP onsched_extside). It is also possible to
get
some locks automatically (polymorphism), and there are examples on
VFS side.Thanks for your reminder, I will look at these BPF codes. Our goal is
to makemptcp_subflowbpf_iteronly used bystruct_opsdefined by MPTCP
BPF (bpf_mptcp_sched_opsandbpf_mptcp_pm_ops), right? Otherstruct_ops
are not allowed to usemptcp_subflowbpf_iter.
I checked and found that other struct_ops **cannot** use mptcp_subflow bpf_iter. Although we registered this bpf_iter for use with BPF_PROG_TYPE_STRUCT_OPS type, we checked in bpf_iter_mptcp_subflow_new that sk->sk_protocol must be IPPROTO_MPTCP. Does this mean that other struct_ops cannot use mptcp_subflow bpf_iter successfully? I don't know if this check is sufficient.
Quoted 6 lines
Yes, that's correct. I didn't check, but **maybe** some
mptcp_sched_ops
andmptcp_pm_opscallback might not be allowed to usebpf_iter. In this
case, it might be needed to allow only some of them to usebpf_iter.
I guess it's not easy to allow only some callbacks of a struct_ops to access certain functions, because The BPF verifier verifies the struct_ops as a whole. But I will continue to look for a solution.
Quoted 6 lines
Note that it sounds like all mptcp_{pm,sched}_ops callbacks should be done while holding the msk lock, e.g. being called from the worker and
not from a subflow event, etc. but maybe there are some exceptions needed.
I checked the following 19 callbacks of mptcp_{pm,sched}_ops that have been implemented in my code tree. Except for the three exceptions (get_local_id, get_priority and add_addr_received), the other 17 callbacks are all be done while holding the msk lock:
struct mptcp_sched_ops {
int (*get_send)(struct mptcp_sock *msk);
int (*get_retrans)(struct mptcp_sock *msk);
}struct mptcp_pm_ops {
/* required */
int (*get_local_id)(struct mptcp_sock *msk,
struct mptcp_pm_addr_entry *skc);
bool (*get_priority)(struct mptcp_sock *msk,
struct mptcp_addr_info *skc);
/* optional */
void (*established)(struct mptcp_sock *msk);
void (*subflow_established)(struct mptcp_sock *msk);
/* required */
bool (*allow_new_subflow)(struct mptcp_sock *msk);
bool (*accept_new_subflow)(const struct mptcp_sock *msk);
bool (*add_addr_echo)(struct mptcp_sock *msk,
const struct mptcp_addr_info *addr);
/* optional */
int (*add_addr_received)(struct mptcp_sock *msk,
const struct mptcp_addr_info *addr);
void (*rm_addr_received)(struct mptcp_sock *msk);
/* optional */
int (*add_addr)(struct mptcp_sock *msk,
struct mptcp_pm_addr_entry *entry);
int (*del_addr)(struct mptcp_sock *msk,
const struct mptcp_pm_addr_entry *entry);
int (*flush_addrs)(struct mptcp_sock *msk,
struct list_head *rm_list);
/* optional */
int (*address_announce)(struct mptcp_sock *msk,
struct mptcp_pm_addr_entry *local);
int (*address_remove)(struct mptcp_sock *msk,
struct mptcp_pm_addr_entry *local);
int (*subflow_create)(struct mptcp_sock *msk,
struct mptcp_pm_addr_entry *local,
struct mptcp_addr_info *remote);
int (*subflow_destroy)(struct mptcp_sock *msk,
struct mptcp_pm_addr_entry *local,
struct mptcp_addr_info *remote);
/* required */
int (*set_priority)(struct mptcp_sock *msk,
struct mptcp_pm_addr_entry *local,
struct mptcp_pm_addr_entry *remote);
};For these three exceptions, add_addr_received is invoked in mptcp_pm_add_addr_received, here the socket lock of ssk is already holding.
Similarly, in subflow_chk_local_id, get_local_id and get_priority are called, where the socket lock of ssk is already holding too.
In addition, get_local_id and get_priority are also called in subflow_token_join_request, which is in atomic and can hold msk lock through bh_lock_sock. If locking is required here, I can send a patch to do this.
In this way, all callbacks of mptcp_{pm,sched}_ops can be done while holding the msk lock or the ssk lock.
Thanks,
-Geliang
Quoted 21 lines
Quoted 12 lines
Quoted 11 lines
In other words, it means we don't need to add this "
bpf_iter_task",
there are other techniques, but I don't have more details, and I didn't
check in the code. If it is not clear for you and you don't find other
examples elsewhere (sched_ext? check WIP patches maybe?), then Alexei
said we should not hesitate to ask questions on the BPF mailing list.Quoted 3 lines
We can send "Add
mptcp_subflowbpf_itersupport" v3 to the BPF mailing
list and ask questions during the reviewing process.Indeed. I hope to be able to find a bit of time this week to send the v3.
Cheers,
Matt
Matthieu Baerts
maintainerRe: [PATCH mptcp-next v3 1/2] mptcp: add bpf_iter_task for mptcp_sock
Hi Geliang,
Quoted 91 lines · Geliang Tang
Hi Matt,
Quoted 30 lines · Matthieu Baerts
Hi Geliang,
Quoted 27 lines · Geliang Tang
Hi Matt,
Quoted 24 lines · Matthieu Baerts
Hi Geliang, Mat,
Quoted 21 lines · Geliang Tang
Quoted 20 lines · Geliang Tang
From: Geliang Tang <[email protected]>
To make sure the
mptcp_subflowbpf_iteris 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 structmptcp_sock.2. Do a
WRITE_ONCE(msk->bpf_iter_task, current)before calling
a MPTCP BPF hook, andWRITE_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"Quoted 57 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
to confirm the correct task, return
-EINVALif it doesn't match.Also creates helpers for setting, clearing and checking that value.
(...)
Quoted 49 lines
Quoted 27 lines
Quoted 19 lines
net/mptcp/protocol.h mptcp_token_join_cookie_init_state(struct mptcp_subflow_request_sock *subflow_re · line 1312
Quoted 11 lines
+static inline bool mptcp_check_bpf_iter_task(struct
mptcp_sock
*msk)(quoted diff) +2
-0+{ + struct task_struct *task = READ_ONCE(msk-Quoted 1 line
bpf_iter_task);net/mptcp/protocol.h +5
-0+ +if (task && task == current) +return true; +return false; +}This v3 has a bug. When I was testing MPTCP BPF selftests in a loop, I
found that the test would break in some cases. After debugging, I
found
that "task" and "current" were not equal:Quoted 5 lines
[ 520.209749][T11984] MPTCP:
bpf_iter_mptcp_subflow_newmsk=00000000fc8f7370 in_interrupt=0 task=00000000ef28139f current=0000000024db2987I will try to fix it, but haven't found a solution yet.
(sorry for the delay, I need a bit of time to catch up)
Quoted 14 lines
I talked a bit to Alexei Starovoitov last week. He told me that with
the
BPFstruct_ops, it is possible to tell the verifier that some locks
are
taken either by somestruct_opstypes, or even per callbacks of some
specificstruct_ops(WIP onsched_extside). It is also possible to
get
some locks automatically (polymorphism), and there are examples on
VFS side.Thanks for your reminder, I will look at these BPF codes. Our goal is
to makemptcp_subflowbpf_iteronly used bystruct_opsdefined by MPTCP
BPF (bpf_mptcp_sched_opsandbpf_mptcp_pm_ops), right? Otherstruct_ops
are not allowed to usemptcp_subflowbpf_iter.
Quoted 6 lines
I checked and found that other
struct_ops**cannot** usemptcp_subflowbpf_iter. Although we registered thisbpf_iterfor use withBPF_PROG_TYPE_STRUCT_OPStype, we checked inbpf_iter_mptcp_subflow_newthatsk->sk_protocolmust beIPPROTO_MPTCP. Does this mean that otherstruct_opscannot usemptcp_subflowbpf_itersuccessfully? I don't know if this check is sufficient.
Sorry, I don't know. With the current version, I don't see any links between mptcp_subflow bpf_iter and mptcp_{sched,pm}_ops, then I don't know how the restriction works, right? I guess there might be a restriction because "struct mptcp_sock*" are used in arguments? But I guess that's not enough because such structures can be obtained from different struct_ops. Probably something else is missing to have this link then?
Quoted 9 lines
Quoted 5 lines
Yes, that's correct. I didn't check, but **maybe** some
mptcp_sched_ops
andmptcp_pm_opscallback might not be allowed to usebpf_iter. In this
case, it might be needed to allow only some of them to usebpf_iter.I guess it's not easy to allow only some callbacks of a
struct_opsto access certain functions, because The BPF verifier verifies thestruct_opsas a whole. But I will continue to look for a solution.
Alexei told me that this work was in progress for sched_ext, but I don't know more about that, sorry.
Quoted 10 lines
Quoted 5 lines
Note that it sounds like all mptcp_{pm,sched}_ops callbacks should be done while holding the msk lock, e.g. being called from the worker and
not from a subflow event, etc. but maybe there are some exceptions needed.I checked the following 19 callbacks of mptcp_{pm,sched}_ops that have been implemented in my code tree. Except for the three exceptions (
get_local_id,get_priorityandadd_addr_received), the other 17 callbacks are all be done while holding the msk lock:
Thank you for having checked!
Quoted 43 lines
struct mptcp_sched_ops { int (*get_send)(struct mptcp_sock *msk); int (*get_retrans)(struct mptcp_sock *msk); }struct mptcp_pm_ops { /* required */ int (*get_local_id)(struct mptcp_sock *msk, struct mptcp_pm_addr_entry *skc); bool (*get_priority)(struct mptcp_sock *msk, struct mptcp_addr_info *skc); /* optional */ void (*established)(struct mptcp_sock *msk); void (*subflow_established)(struct mptcp_sock *msk); /* required */ bool (*allow_new_subflow)(struct mptcp_sock *msk); bool (*accept_new_subflow)(const struct mptcp_sock *msk); bool (*add_addr_echo)(struct mptcp_sock *msk, const struct mptcp_addr_info *addr); /* optional */ int (*add_addr_received)(struct mptcp_sock *msk, const struct mptcp_addr_info *addr); void (*rm_addr_received)(struct mptcp_sock *msk); /* optional */ int (*add_addr)(struct mptcp_sock *msk, struct mptcp_pm_addr_entry *entry); int (*del_addr)(struct mptcp_sock *msk, const struct mptcp_pm_addr_entry *entry); int (*flush_addrs)(struct mptcp_sock *msk, struct list_head *rm_list); /* optional */ int (*address_announce)(struct mptcp_sock *msk, struct mptcp_pm_addr_entry *local); int (*address_remove)(struct mptcp_sock *msk, struct mptcp_pm_addr_entry *local); int (*subflow_create)(struct mptcp_sock *msk, struct mptcp_pm_addr_entry *local, struct mptcp_addr_info *remote);
Quoted 8 lines
int (*subflow_destroy)(struct mptcp_sock *msk,
struct mptcp_pm_addr_entry *local,
struct mptcp_addr_info *remote);
/* required */
int (*set_priority)(struct mptcp_sock *msk,
struct mptcp_pm_addr_entry *local,
struct mptcp_pm_addr_entry *remote);Mmh, all 8 callbacks from add_addr to here seem to be linked to Netlink commands, right? If yes, they should not be here: they don't make sense for a BPF PM, no? BPF PMs should be configured with BPF, and not via Netlink. Netlink is only for the built-in PMs (in-kernel and userspace PMs)
Quoted 5 lines
};
For these three exceptions,
add_addr_receivedis invoked inmptcp_pm_add_addr_received, here the socket lock of ssk is already holding.
For add_addr_received, I think it is safer to have the callback from the worker context. In other words, when an ADD_ADDR received on a subflow:
- the ADD_ADDR echo should be sent: I don't think it is worth it letting the other peer resending it just in case the userspace PM was not "ready"
- if pm->ops->add_addr_received is set, schedule the worker and set msk->pm.remote
- then pm->ops->add_addr_received will be called from the worker.
Quoted 7 lines
Similarly, in
subflow_chk_local_id,get_local_idandget_priorityare called, where the socket lock of ssk is already holding too.In addition,
get_local_idandget_priorityare also called insubflow_token_join_request, which is in atomic and can hold msk lock throughbh_lock_sock. If locking is required here, I can send a patch to do this.
Yes, for the ID and backup, I guess we will need an exception there, and a way not to let the BPF PMs calling bpf_iter. We should not hold the msk lock here.
I guess the easier would be to ask the BPF maintainers what we should do here. Maybe this can be done after having sent the mptcp_subflow v3 series, or at the same time. I will see what I can do.
Quoted 2 lines
In this way, all callbacks of mptcp_{pm,sched}_ops can be done while holding the msk lock or the ssk lock.
At least on the scheduler side, that will be the case (or maybe not if we change the API to cover more cases :) ).
Anyway, probably best to wait for BPF maintainers recommendations instead of guessing.
Cheers,
Matt
Signature · 1 line
Sponsored by the NGI0 Core fund.
Geliang Tang
reviewerRe: [PATCH mptcp-next v3 1/2] mptcp: add bpf_iter_task for mptcp_sock
Hi Matt,
Quoted 211 lines · Matthieu Baerts
Hi Geliang,
Quoted 102 lines · Geliang Tang
Hi Matt,
Quoted 32 lines · Matthieu Baerts
Hi Geliang,
Quoted 29 lines · Geliang Tang
Hi Matt,
Quoted 26 lines · Matthieu Baerts
Hi Geliang, Mat,
Quoted 23 lines · Geliang Tang
Quoted 22 lines · Geliang Tang
From: Geliang Tang <[email protected]>
To make sure the
mptcp_subflowbpf_iteris 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 structmptcp_sock.2. Do a
WRITE_ONCE(msk->bpf_iter_task, current)before calling
a MPTCP BPF hook, andWRITE_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"Quoted 66 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
to confirm the correct task, return
-EINVALif it doesn't match.Also creates helpers for setting, clearing and checking that
value.(...)
Quoted 57 lines
Quoted 39 lines
Quoted 21 lines
Review
Quoted 11 lines
+static inline bool mptcp_check_bpf_iter_task(struct
mptcp_sock
*msk)(quoted diff) +2
-0+{ + struct task_struct *task = READ_ONCE(msk-Quoted 1 line
bpf_iter_task);(quoted diff) +5
-0+ + if (task && task == current) + return true; + return false; +}This v3 has a bug. When I was testing MPTCP BPF selftests in a
loop, I
found that the test would break in some cases. After debugging,
I
found
that "task" and "current" were not equal:Quoted 5 lines
[ 520.209749][T11984] MPTCP:
bpf_iter_mptcp_subflow_newmsk=00000000fc8f7370 in_interrupt=0 task=00000000ef28139f current=0000000024db2987I will try to fix it, but haven't found a solution yet.
(sorry for the delay, I need a bit of time to catch up)
I talked a bit to Alexei Starovoitov last week. He told me that
with
the
BPFstruct_ops, it is possible to tell the verifier that some locks
are
taken either by somestruct_opstypes, or even per callbacks of
someQuoted 8 lines
specific
struct_ops(WIP onsched_extside). It is also possible
to
get
some locks automatically (polymorphism), and there are examples
on
VFS side.Thanks for your reminder, I will look at these BPF codes. Our goal
is
to makemptcp_subflowbpf_iteronly used bystruct_opsdefined by
MPTCP
BPF (bpf_mptcp_sched_opsandbpf_mptcp_pm_ops), right? Otherstruct_ops
are not allowed to usemptcp_subflowbpf_iter.Quoted 10 lines
I checked and found that other
struct_ops**cannot** usemptcp_subflowbpf_iter. Although we registered thisbpf_iterfor use withBPF_PROG_TYPE_STRUCT_OPStype, we checked inbpf_iter_mptcp_subflow_new
thatsk->sk_protocolmust beIPPROTO_MPTCP. Does this mean that otherstruct_opscannot usemptcp_subflowbpf_itersuccessfully? I don't know
if this check is sufficient.Sorry, I don't know. With the current version, I don't see any links between
mptcp_subflowbpf_iterand mptcp_{sched,pm}_ops, then I don't know how the restriction works, right? I guess there might be a restriction because "struct mptcp_sock*" are used in arguments? But I guess that's not enough because such structures can be obtained from differentstruct_ops. Probably something else is missing to have this link then?Quoted 8 lines
Quoted 7 lines
Yes, that's correct. I didn't check, but **maybe** some
mptcp_sched_ops
andmptcp_pm_opscallback might not be allowed to usebpf_iter. In
this
case, it might be needed to allow only some of them to usebpf_iter.Quoted 4 lines
I guess it's not easy to allow only some callbacks of a
struct_opsto
access certain functions, because The BPF verifier verifies thestruct_opsas a whole. But I will continue to look for a solution.Alexei told me that this work was in progress for
sched_ext, but I don't
know more about that, sorry.Quoted 9 lines
Quoted 8 lines
Note that it sounds like all mptcp_{pm,sched}_ops callbacks should be
done while holding the msk lock, e.g. being called from the worker
and
not from a subflow event, etc. but maybe there are some exceptions
needed.Quoted 5 lines
I checked the following 19 callbacks of mptcp_{pm,sched}_ops that have
been implemented in my code tree. Except for the three exceptions (get_local_id,get_priorityandadd_addr_received), the other 17 callbacks are all be done while holding the msk lock:Thank you for having checked!
Quoted 48 lines
struct
mptcp_sched_ops{
int (*get_send)(structmptcp_sock*msk); int (*get_retrans)(structmptcp_sock*msk); }struct
mptcp_pm_ops{
/* required */
int (*get_local_id)(structmptcp_sock*msk, structmptcp_pm_addr_entry*skc); bool (*get_priority)(structmptcp_sock*msk, structmptcp_addr_info*skc);/* optional */
void (*established)(structmptcp_sock*msk); void (*subflow_established)(structmptcp_sock*msk);/* required */
bool (*allow_new_subflow)(structmptcp_sock*msk); bool (*accept_new_subflow)(const structmptcp_sock*msk); bool (*add_addr_echo)(structmptcp_sock*msk, const structmptcp_addr_info*addr);/* optional */
int (*add_addr_received)(structmptcp_sock*msk, const structmptcp_addr_info*addr);
void (*rm_addr_received)(structmptcp_sock*msk);/* optional */
int (*add_addr)(structmptcp_sock*msk,
structmptcp_pm_addr_entry*entry); int (*del_addr)(structmptcp_sock*msk,
const structmptcp_pm_addr_entry*entry); int (*flush_addrs)(structmptcp_sock*msk, structlist_head*rm_list);/* optional */
int (*address_announce)(structmptcp_sock*msk, structmptcp_pm_addr_entry*local); int (*address_remove)(structmptcp_sock*msk, structmptcp_pm_addr_entry*local); int (*subflow_create)(structmptcp_sock*msk, structmptcp_pm_addr_entry*local, structmptcp_addr_info*remote); int (*subflow_destroy)(structmptcp_sock*msk, structmptcp_pm_addr_entry*local, structmptcp_addr_info*remote);Quoted 4 lines
/* required */
int (*set_priority)(structmptcp_sock*msk, structmptcp_pm_addr_entry*local, structmptcp_pm_addr_entry*remote);
Quoted 5 lines
Mmh, all 8 callbacks from
add_addrto here seem to be linked to Netlink
commands, right? If yes, they should not be here: they don't make sense
for a BPF PM, no? BPF PMs should be configured with BPF, and not via
Do you have any idea how we can pass commands like ADD_ADDR and addresses from user space to BPF?
What I can think of is passing through sockopt, the user space passes the command and the address through setsockopt, and the BPF program handles it through the custom "cgroup/setsockopt". I would like to hear your opinions. If you also agree to use sockopt to implement it, I can write a test program for this.
Thanks,
-Geliang
Quoted 58 lines
Netlink. Netlink is only for the built-in PMs (in-kernel and userspace PMs)
Quoted 5 lines
};
For these three exceptions,
add_addr_receivedis invoked inmptcp_pm_add_addr_received, here the socket lock of ssk is already holding.For
add_addr_received, I think it is safer to have the callback from the
worker context. In other words, when anADD_ADDRreceived on a subflow:- the
ADD_ADDRecho should be sent: I don't think it is worth it letting
the other peer resending it just in case the userspace PM was not "ready"- if
pm->ops->add_addr_receivedis set, schedule the worker and setmsk->pm.remote- then
pm->ops->add_addr_receivedwill be called from the worker.Quoted 10 lines
Similarly, in
subflow_chk_local_id,get_local_idandget_priorityare
called, where the socket lock of ssk is already holding too.In addition,
get_local_idandget_priorityare also called insubflow_token_join_request, which is in atomic and can hold msk lock
throughbh_lock_sock. If locking is required here, I can send a patch
to do this.Yes, for the ID and backup, I guess we will need an exception there, and
a way not to let the BPF PMs callingbpf_iter. We should not hold the msk lock here.I guess the easier would be to ask the BPF maintainers what we should do
here. Maybe this can be done after having sent themptcp_subflowv3 series, or at the same time. I will see what I can do.Quoted 3 lines
In this way, all callbacks of mptcp_{pm,sched}_ops can be done while
holding the msk lock or the ssk lock.At least on the scheduler side, that will be the case (or maybe not if
we change the API to cover more cases :) ).Anyway, probably best to wait for BPF maintainers recommendations instead of guessing.
Cheers,
Matt
Matthieu Baerts
maintainerRe: [PATCH mptcp-next v3 1/2] mptcp: add bpf_iter_task for mptcp_sock
Hi Geliang,
Quoted 1 line · Geliang Tang
On Tue, 2025-03-18 at 12:26 +0100, Matthieu Baerts wrote:
(...)
Quoted 8 lines
Quoted 5 lines
Mmh, all 8 callbacks from
add_addrto here seem to be linked to Netlink
commands, right? If yes, they should not be here: they don't make sense
for a BPF PM, no? BPF PMs should be configured with BPF, and not viaDo you have any idea how we can pass commands like
ADD_ADDRand addresses from user space to BPF?
The userspace can inject any kind of BPF program. Additional addresses can then be hardcoded in this program, or it can load them from eBPF maps, or read skel->bss->(...), etc.
Netlink is then not needed.
Quoted 5 lines
What I can think of is passing through sockopt, the user space passes the command and the address through setsockopt, and the BPF program handles it through the custom "cgroup/setsockopt". I would like to hear your opinions. If you also agree to use sockopt to implement it, I can write a test program for this.
That's a good idea for userspace apps which want to control how the MPTCP PM is acting. I guess MPTCP BPF PM will generally not be linked to network apps: a separate application dedicated to inject and configure the BPF PM, no?
Cheers,
Matt
Signature · 1 line
Sponsored by the NGI0 Core fund.
Geliang Tang
reviewerRe: [PATCH mptcp-next v3 1/2] mptcp: add bpf_iter_task for mptcp_sock
Hi Matt,
Quoted 17 lines · Matthieu Baerts
Hi Geliang,
Quoted 1 line · Geliang Tang
On Tue, 2025-03-18 at 12:26 +0100, Matthieu Baerts wrote:
(...)
Quoted 9 lines
Quoted 6 lines
Mmh, all 8 callbacks from
add_addrto here seem to be linked to Netlink
commands, right? If yes, they should not be here: they don't make sense
for a BPF PM, no? BPF PMs should be configured with BPF, and not viaDo you have any idea how we can pass commands like
ADD_ADDRand addresses from user space to BPF?
Quoted 15 lines
The userspace can inject any kind of BPF program. Additional addresses
can then be hardcoded in this program, or it can load them from eBPF maps, or read skel->bss->(...), etc.Netlink is then not needed.
Quoted 8 lines
What I can think of is passing through sockopt, the user space passes
the command and the address through setsockopt, and the BPF program handles it through the custom "cgroup/setsockopt". I would like to hear
your opinions. If you also agree to use sockopt to implement it, I can
write a test program for this.
I just tested it and found it difficult to use setsockopt.
"cgroup/setsockopt" program doesn't work. We need to invoke sleepable kfuncs in BPF PM:
BTF_ID_FLAGS(func, mptcp_pm_remove_addr_entry, KF_SLEEPABLE) BTF_ID_FLAGS(func, mptcp_pm_addr_send_ack, KF_SLEEPABLE) BTF_ID_FLAGS(func, mptcp_pm_mp_prio_send_ack, KF_SLEEPABLE) BTF_ID_FLAGS(func, bpf_mptcp_subflow_connect, KF_SLEEPABLE) BTF_ID_FLAGS(func, mptcp_subflow_shutdown, KF_SLEEPABLE) BTF_ID_FLAGS(func, mptcp_close_ssk, KF_SLEEPABLE)
They can't be invoked in "cgroup/setsockopt" program. And "cgroup/setsockopt" program can't be set with BPF_F_SLEEPABLE flag:
err = err ?: bpf_program__set_flags(skel->progs.pm_setsockopt,
BPF_F_SLEEPABLE);With this error occurs:
test_bpf_hashmap_pm:PASS:set sleepable flags 0 nsec
libbpf: prog 'pm_setsockopt': BPF program load failed: -EINVAL
libbpf: prog 'pm_setsockopt': -- BEGIN PROG LOAD LOG -- Only fentry/fexit/fmod_ret, lsm, iter, uprobe, and struct_ops programs can be sleepable
processed 0 insns (limit 1000000) max_states_per_insn 0 total_states 0 peak_states 0 mark_read 0
It seems that we can only choose one of fentry/fexit/fmod_ret, lsm, iter, uprobe, or struct_ops type to implement BPF PM.
Thanks,
-Geliang
Quoted 9 lines
That's a good idea for userspace apps which want to control how the MPTCP PM is acting. I guess MPTCP BPF PM will generally not be linked to
network apps: a separate application dedicated to inject and configure
the BPF PM, no?Cheers,
Matt
Matthieu Baerts
maintainerRe: [PATCH mptcp-next v3 1/2] mptcp: add bpf_iter_task for mptcp_sock
Quoted 40 lines · Geliang Tang
Hi Matt,
Quoted 17 lines · Matthieu Baerts
Hi Geliang,
Quoted 1 line · Geliang Tang
On Tue, 2025-03-18 at 12:26 +0100, Matthieu Baerts wrote:
(...)
Quoted 9 lines
Quoted 6 lines
Mmh, all 8 callbacks from
add_addrto here seem to be linked to Netlink
commands, right? If yes, they should not be here: they don't make sense
for a BPF PM, no? BPF PMs should be configured with BPF, and not viaDo you have any idea how we can pass commands like
ADD_ADDRand addresses from user space to BPF?Quoted 15 lines
The userspace can inject any kind of BPF program. Additional addresses
can then be hardcoded in this program, or it can load them from eBPF maps, or read skel->bss->(...), etc.Netlink is then not needed.
Quoted 8 lines
What I can think of is passing through sockopt, the user space passes
the command and the address through setsockopt, and the BPF program handles it through the custom "cgroup/setsockopt". I would like to hear
your opinions. If you also agree to use sockopt to implement it, I can
write a test program for this.I just tested it and found it difficult to use setsockopt.
"cgroup/setsockopt" program doesn't work. We need to invoke sleepable kfuncs in BPF PM:
(...)
Quoted 2 lines
It seems that we can only choose one of fentry/fexit/fmod_ret, lsm, iter, uprobe, or
struct_opstype to implement BPF PM.
Thank you for having checked. I think we can look at that aspect later. I don't think many apps will want this kind of control. They can always set something in a map, and the BPF program can look at it when needed.
Cheers,
Matt
Signature · 1 line
Sponsored by the NGI0 Core fund.
Geliang Tang
reviewerPATCH v3 2/2 bpf: Customize mptcp's own sock lock
In reply to Geliang TangFrom: Geliang Tang <[email protected]>
In order to customize mptcp's own sock lock and release functions, sk_lock_sock() and sk_release_sock() function pointers are added to struct sock, and these pointers are called in BPF Cgroup getsockopt and setsockopt.
--- include/net/sock.h | 2 ++ kernel/bpf/cgroup.c | 8 ++++---- net/mptcp/protocol.c | 15 +++++++++++++++ 3 files changed, 21 insertions(+), 4 deletions(-)
include/net/sock.h +2 -0
@@ -540,6 +540,8 @@ struct sock {int (*sk_backlog_rcv)(struct sock *sk,struct sk_buff *skb);void (*sk_destruct)(struct sock *sk); +void (*sk_lock_sock)(struct sock *sk); +void (*sk_release_sock)(struct sock *sk);struct sock_reuseport __rcu *sk_reuseport_cb;#ifdef CONFIG_BPF_SYSCALL struct bpf_local_storage __rcu *sk_bpf_storage;
kernel/bpf/cgroup.c +4 -4
@@ -1843,10 +1843,10 @@ int __cgroup_bpf_run_filter_setsockopt(struct sock *sk, int *level, goto out; } -lock_sock (sk); + sk->sk_lock_sock ? sk->sk_lock_sock (sk) :lock_sock (sk); ret =bpf_prog_run_array_cg (&cgrp->bpf,CGROUP_SETSOCKOPT , &ctx, bpf_prog_run,0 ,NULL ); -release_sock (sk); + sk->sk_release_sock ? sk->sk_release_sock (sk) :release_sock (sk);if (ret) goto out;
@@ -1952,10 +1952,10 @@ int __cgroup_bpf_run_filter_getsockopt(struct sock *sk, int level, } } -lock_sock (sk); + sk->sk_lock_sock ? sk->sk_lock_sock (sk) :lock_sock (sk); ret =bpf_prog_run_array_cg (&cgrp->bpf,CGROUP_GETSOCKOPT , &ctx, bpf_prog_run, retval,NULL ); -release_sock (sk); + sk->sk_release_sock ? sk->sk_release_sock (sk) :release_sock (sk);if (ret <0 ) goto out;
net/mptcp/protocol.c +15 -0
@@ -2712,6 +2712,18 @@ static void mptcp_worker(struct work_struct *work)sock_put (sk); } +static void mptcp_sk_lock_sock (struct sock *sk) +{ +lock_sock (sk); +mptcp_set_bpf_iter_task (mptcp_sk (sk)); +} + +static void mptcp_sk_release_sock (struct sock *sk) +{ +mptcp_clear_bpf_iter_task (mptcp_sk (sk)); +release_sock (sk); +} +static void __mptcp_init_sock (struct sock *sk) {struct mptcp_sock *msk =mptcp_sk (sk);
@@ -2741,6 +2753,9 @@ static void __mptcp_init_sock(struct sock *sk)/* re-use the csk retrans timer for MPTCP-level retrans */ timer_setup (&msk->sk.icsk_retransmit_timer, mptcp_retransmit_timer,0 );timer_setup (&sk->sk_timer, mptcp_tout_timer,0 ); + + sk->sk_lock_sock = mptcp_sk_lock_sock; + sk->sk_release_sock = mptcp_sk_release_sock; }static void mptcp_ca_reset (struct sock *sk)
Signature · 1 line
2.43.0
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: Critical: Global Timeout ❌
- 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/13756425685
Initiator: Patchew Applier
Commits: https://github.com/multipath-tcp/mptcp_net-next/commits/ae5890ca8bfa
Patchwork: https://patchwork.kernel.org/project/mptcp/list/?series=942077
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)