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_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.
Suggested-by: Mat Martineau <martineau@kernel.org>
Signed-off-by: Geliang Tang <tanggeliang@kylinos.cn>
---
net/mptcp/protocol.c | 1 +
net/mptcp/protocol.h | 16 ++++++++++++++++
2 files changed, 17 insertions(+)
diff --git a/net/mptcp/protocol.c b/net/mptcp/protocol.c
index 2b48cf648346..fec776c23fcd 100644
--- a/net/mptcp/protocol.c
+++ b/net/mptcp/protocol.c
@@ -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);
diff --git a/net/mptcp/protocol.h b/net/mptcp/protocol.h
index ef1d43406f9b..836891bc28d5 100644
--- a/net/mptcp/protocol.h
+++ b/net/mptcp/protocol.h
@@ -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_re
static 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 */
--
2.43.0