[PATCH mptcp-next v2 03/11] mptcp: add bpf_iter_task for mptcp_sched_data

Geliang Tang posted 11 patches 1 month ago
There is a newer version of this series
[PATCH mptcp-next v2 03/11] mptcp: add bpf_iter_task for mptcp_sched_data
Posted by Geliang Tang 1 month ago
From: Geliang Tang <tanggeliang@kylinos.cn>

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_sched_data.

2. Do a WRITE_ONCE(data->bpf_iter_task, current) before calling
a MPTCP BPF hook, and WRITE_ONCE(data->bpf_iter_task, NULL) after
the hook returns.

3. In bpf_iter_mptcp_subflow_new(), check

	"READ_ONCE(data->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.

Suggested-by: Mat Martineau <martineau@kernel.org>
Signed-off-by: Geliang Tang <tanggeliang@kylinos.cn>
---
 include/net/mptcp.h  |  1 +
 net/mptcp/protocol.h | 19 +++++++++++++++++++
 2 files changed, 20 insertions(+)

diff --git a/include/net/mptcp.h b/include/net/mptcp.h
index 2c85ca92bb1c..bd3d1b3654dd 100644
--- a/include/net/mptcp.h
+++ b/include/net/mptcp.h
@@ -105,6 +105,7 @@ struct mptcp_out_options {
 struct mptcp_sched_data {
 	u8	subflows;
 	struct mptcp_subflow_context *contexts[MPTCP_SUBFLOWS_MAX];
+	struct task_struct *bpf_iter_task;
 };
 
 struct mptcp_sched_ops {
diff --git a/net/mptcp/protocol.h b/net/mptcp/protocol.h
index 3492b256ecba..0875b3cf4aba 100644
--- a/net/mptcp/protocol.h
+++ b/net/mptcp/protocol.h
@@ -1291,4 +1291,23 @@ mptcp_token_join_cookie_init_state(struct mptcp_subflow_request_sock *subflow_re
 static inline void mptcp_join_cookie_init(void) {}
 #endif
 
+static inline void mptcp_sched_set_bpf_iter_task(struct mptcp_sched_data *data)
+{
+	WRITE_ONCE(data->bpf_iter_task, current);
+}
+
+static inline void mptcp_sched_clear_bpf_iter_task(struct mptcp_sched_data *data)
+{
+	WRITE_ONCE(data->bpf_iter_task, NULL);
+}
+
+static inline bool mptcp_sched_check_bpf_iter_task(struct mptcp_sched_data *data)
+{
+	struct task_struct *task = READ_ONCE(data->bpf_iter_task);
+
+	if (task && task == current)
+		return true;
+	return false;
+}
+
 #endif /* __MPTCP_PROTOCOL_H */
-- 
2.43.0
Re: [PATCH mptcp-next v2 03/11] mptcp: add bpf_iter_task for mptcp_sched_data
Posted by Matthieu Baerts 4 weeks, 1 day ago
Hi Geliang,

On 07/03/2025 12:36, Geliang Tang wrote:
> From: Geliang Tang <tanggeliang@kylinos.cn>
> 
> 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_sched_data.
> 
> 2. Do a WRITE_ONCE(data->bpf_iter_task, current) before calling
> a MPTCP BPF hook, and WRITE_ONCE(data->bpf_iter_task, NULL) after
> the hook returns.
> 
> 3. In bpf_iter_mptcp_subflow_new(), check
> 
> 	"READ_ONCE(data->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.

I think it is better not to have dedicated patches introducing helpers
without using them: for the reviewers, it is not clear how they are
exactly going to be used, then it is needed to check the next patch(es),
go back to this one, etc.

Maybe better to introduce the helper when you actually use them?
Especially for such simple helpers.

Cheers,
Matt
-- 
Sponsored by the NGI0 Core fund.