Skip to contentSkip to conversation

mptcp: avoid data-races around the sysctls

PATCH Superseded Gang Yan 3 participants 12 messages Sep 4 – Sep 10

Subsystems (2)

v6 of 7 Superseded Latest is v7 →

lore
Share & tools
lore

Gang Yan

author
#0 lore raw

v6 cover mptcp: avoid data-races around the sysctls

From: Gang Yan <[email protected]>

Changelog:
v6:
- Patch 6 adds old->release when the path manager changed to address sashiko's comments in [1].

v5:

v4:

v3:

v2:

v2:

[1] https://sashiko.dev/#/patchset/[email protected]?part=6

Gang Yan (5):
mptcp: sched: change scheduler sysctl atomically
mptcp: pm: change path_manager sysctl atomically
mptcp: pm: use WRITE_ONCE() for the pm_type sysctl Squash-to "mptcp: pm: init and release mptcp_pm_ops" Squash to previous one

Matthieu Baerts (NGI0) (1):
mptcp: use READ_ONCE() over sysctls

 net/mptcp/ctrl.c     | 141 ++++++++++++++++++++++++++++++++-----------
 net/mptcp/pm.c       |  68 +++++++++++++++------
 net/mptcp/protocol.c |   7 ++-
 net/mptcp/protocol.h |   9 +--
 net/mptcp/sched.c    |   2 +-
 net/mptcp/subflow.c  |   9 ++-
 6 files changed, 175 insertions(+), 61 deletions(-)
Signature · 1 line
2.43.0
author
#1 lore raw

PATCH v6 1/6 mptcp: sched: change scheduler sysctl atomically

From: Gang Yan <[email protected]>

The per-netns scheduler name is stored as an inline char[] buffer and updated via strscpy() from the sysctl handler. A concurrent reader (e.g. mptcp_init_sock() resolving the default scheduler) can observe a half-written name, which is also flagged by KCSAN. READ_ONCE() does not help here as it cannot read a multi-byte string atomically.

Following the tcp_congestion_control() model, store a pointer to the immutable struct mptcp_sched_ops instead of the name string. A pointer store is a single atomic word, so readers always observe a consistent value, and mptcp_get_scheduler() can now return the ops directly instead of going through mptcp_sched_find() again.

---
 net/mptcp/ctrl.c     | 63 +++++++++++++++++++++++++++++++++++---------
 net/mptcp/protocol.c |  3 +--
 net/mptcp/protocol.h |  3 ++-
 net/mptcp/sched.c    |  2 +-
 4 files changed, 54 insertions(+), 17 deletions(-)
net/mptcp/ctrl.c +50 -13
index 63c5747f0f63..7d0f3421bd04 100644
@@ -39,7 +39,7 @@ struct mptcp_pernet {
 	u8 allow_join_initial_addr_port;
 	u8 pm_type;
 	u8 add_addr_v6_port_drop_ts;
-	char scheduler[MPTCP_SCHED_NAME_MAX];
+	struct mptcp_sched_ops __rcu *scheduler;
 	char path_manager[MPTCP_PM_NAME_MAX];
 };
 
@@ -90,9 +90,17 @@ const char *mptcp_get_path_manager(const struct net *net)
 	return mptcp_get_pernet(net)->path_manager;
 }
 
-const char *mptcp_get_scheduler(const struct net *net)
+static struct mptcp_sched_ops *mptcp_pernet_sched(struct mptcp_pernet *pernet)
 {
-	return mptcp_get_pernet(net)->scheduler;
+	struct mptcp_sched_ops *sched;
+
+	sched = rcu_dereference(pernet->scheduler);
+	return sched ? sched : &mptcp_sched_default;
+}
+
+struct mptcp_sched_ops *mptcp_get_scheduler(const struct net *net)
+{
+	return mptcp_pernet_sched(mptcp_get_pernet(net));
 }
 
 unsigned int mptcp_add_addr_v6_port_drop_ts(const struct net *net)
@@ -112,23 +120,33 @@ static void mptcp_pernet_set_defaults(struct mptcp_pernet *pernet)
 	pernet->allow_join_initial_addr_port = 1;
 	pernet->stale_loss_cnt = 4;
 	pernet->pm_type = MPTCP_PM_TYPE_KERNEL;
-	strscpy(pernet->scheduler, "default", sizeof(pernet->scheduler));
+
+	if (bpf_try_module_get(&mptcp_sched_default, mptcp_sched_default.owner))
+		RCU_INIT_POINTER(pernet->scheduler, &mptcp_sched_default);
+
 	strscpy(pernet->path_manager, "kernel", sizeof(pernet->path_manager));
 	pernet->add_addr_v6_port_drop_ts = 1;
 }
 
 #ifdef CONFIG_SYSCTL
-static int mptcp_set_scheduler(char *scheduler, const char *name)
+static int mptcp_set_scheduler(struct mptcp_pernet *pernet, const char *name)
 {
-	struct mptcp_sched_ops *sched;
+	struct mptcp_sched_ops *sched, *prev;
 	int ret = 0;
 
 	rcu_read_lock();
 	sched = mptcp_sched_find(name);
-	if (sched)
-		strscpy(scheduler, name, MPTCP_SCHED_NAME_MAX);
-	else
+	if (sched) {
+		if (bpf_try_module_get(sched, sched->owner)) {
+			prev = xchg(&pernet->scheduler, sched);
+			if (prev)
+				bpf_module_put(prev, prev->owner);
+		} else {
+			ret = -EBUSY;
+		}
+	} else {
 		ret = -ENOENT;
+	}
 	rcu_read_unlock();
 
 	return ret;
@@ -137,7 +155,9 @@ static int mptcp_set_scheduler(char *scheduler, const char *name)
 static int proc_scheduler(const struct ctl_table *ctl, int write,
 			  void *buffer, size_t *lenp, loff_t *ppos)
 {
-	char (*scheduler)[MPTCP_SCHED_NAME_MAX] = ctl->data;
+	struct mptcp_pernet *pernet = container_of(ctl->data,
+						   struct mptcp_pernet,
+						   scheduler);
 	char val[MPTCP_SCHED_NAME_MAX];
 	struct ctl_table tbl = {
 		.data = val,
@@ -145,11 +165,13 @@ static int proc_scheduler(const struct ctl_table *ctl, int write,
 	};
 	int ret;
 
-	strscpy(val, *scheduler, MPTCP_SCHED_NAME_MAX);
+	rcu_read_lock();
+	strscpy(val, mptcp_pernet_sched(pernet)->name, MPTCP_SCHED_NAME_MAX);
+	rcu_read_unlock();
 
 	ret = proc_dostring(&tbl, write, buffer, lenp, ppos);
 	if (write && ret == 0)
-		ret = mptcp_set_scheduler(*scheduler, val);
+		ret = mptcp_set_scheduler(pernet, val);
 
 	return ret;
 }
@@ -563,18 +585,33 @@ void mptcp_active_detect_blackhole(struct sock *ssk, bool expired)
 static int __net_init mptcp_net_init(struct net *net)
 {
 	struct mptcp_pernet *pernet = mptcp_get_pernet(net);
+	int ret;
 
 	mptcp_pernet_set_defaults(pernet);
 
-	return mptcp_pernet_new_table(net, pernet);
+	ret = mptcp_pernet_new_table(net, pernet);
+	if (ret) {
+		struct mptcp_sched_ops *sched;
+
+		sched = rcu_dereference_protected(pernet->scheduler, true);
+		if (sched)
+			bpf_module_put(sched, sched->owner);
+	}
+
+	return ret;
 }
 
 /* Note: the callback will only be called per extra netns */
 static void __net_exit mptcp_net_exit(struct net *net)
 {
 	struct mptcp_pernet *pernet = mptcp_get_pernet(net);
+	struct mptcp_sched_ops *sched;
 
 	mptcp_pernet_del_table(pernet);
+
+	sched = rcu_dereference_protected(pernet->scheduler, true);
+	if (sched)
+		bpf_module_put(sched, sched->owner);
 }
 
 static struct pernet_operations mptcp_pernet_ops = {
net/mptcp/protocol.c +1 -2
index 0b24e0afedfb..179eb6bcebf8 100644
@@ -3272,8 +3272,7 @@ static int mptcp_init_sock(struct sock *sk)
 		return -ENOMEM;
 
 	rcu_read_lock();
-	ret = mptcp_init_sched(mptcp_sk(sk),
-			       mptcp_sched_find(mptcp_get_scheduler(net)));
+	ret = mptcp_init_sched(mptcp_sk(sk), mptcp_get_scheduler(net));
 	rcu_read_unlock();
 	if (ret)
 		return ret;
net/mptcp/protocol.h +2 -1
index b3121c8c766b..5154c7ce3026 100644
@@ -803,7 +803,7 @@ unsigned int mptcp_stale_loss_cnt(const struct net *net);
 unsigned int mptcp_close_timeout(const struct sock *sk);
 int mptcp_get_pm_type(const struct net *net);
 const char *mptcp_get_path_manager(const struct net *net);
-const char *mptcp_get_scheduler(const struct net *net);
+struct mptcp_sched_ops *mptcp_get_scheduler(const struct net *net);
 unsigned int mptcp_add_addr_v6_port_drop_ts(const struct net *net);
 
 void mptcp_active_disable(struct sock *sk);
@@ -1155,6 +1155,7 @@ int mptcp_pm_remove_addr(struct mptcp_sock *msk, const struct mptcp_rm_list *rm_
 
 /* the default path manager, used in mptcp_pm_unregister */
 extern struct mptcp_pm_ops mptcp_pm_kernel;
+extern struct mptcp_sched_ops mptcp_sched_default;
 
 struct mptcp_pm_ops *mptcp_pm_find(const char *name);
 int mptcp_pm_register(struct mptcp_pm_ops *pm_ops);
net/mptcp/sched.c +1 -1
index 1e59072d478c..0d13ee46ffdf 100644
@@ -40,7 +40,7 @@ static int mptcp_sched_default_get_retrans(struct mptcp_sock *msk)
 	return 0;
 }
 
-static struct mptcp_sched_ops mptcp_sched_default = {
+struct mptcp_sched_ops mptcp_sched_default = {
 	.get_send	= mptcp_sched_default_get_send,
 	.get_retrans	= mptcp_sched_default_get_retrans,
 	.name		= "default",
Signature · 1 line
2.43.0
author
#2 lore raw

PATCH v6 2/6 mptcp: pm: change path_manager sysctl atomically

In reply to Gang Yan

From: Gang Yan <[email protected]>

The per-netns path manager name is stored as an inline char[] buffer and updated via strscpy() from the sysctl handler. A concurrent reader can observe a half-written name (KCSAN), which READ_ONCE() cannot fix for a multi-byte string.

Following the tcp_congestion_control() model (and the scheduler change in the previous patch), store a pointer to the immutable struct mptcp_pm_ops instead of the name string.

No module reference is taken on the path manager ops for now, as they can only be registered from built-in code on one side, and on the other side the reference counting will be introduced by the last patch of this series, together with the BPF path manager support.

---
 net/mptcp/ctrl.c     | 33 +++++++++++++++++++++++----------
 net/mptcp/pm.c       |  3 ++-
 net/mptcp/protocol.h |  3 +--
 3 files changed, 26 insertions(+), 13 deletions(-)
net/mptcp/ctrl.c +23 -10
index 7d0f3421bd04..76ff2a41ba38 100644
@@ -40,7 +40,7 @@ struct mptcp_pernet {
 	u8 pm_type;
 	u8 add_addr_v6_port_drop_ts;
 	struct mptcp_sched_ops __rcu *scheduler;
-	char path_manager[MPTCP_PM_NAME_MAX];
+	struct mptcp_pm_ops __rcu *path_manager;
 };
 
 static struct mptcp_pernet *mptcp_get_pernet(const struct net *net)
@@ -85,9 +85,20 @@ int mptcp_get_pm_type(const struct net *net)
 	return mptcp_get_pernet(net)->pm_type;
 }
 
-const char *mptcp_get_path_manager(const struct net *net)
+static struct mptcp_pm_ops *mptcp_pernet_pm(struct mptcp_pernet *pernet)
 {
-	return mptcp_get_pernet(net)->path_manager;
+	struct mptcp_pm_ops *pm_ops;
+
+	pm_ops = rcu_dereference(pernet->path_manager);
+	return pm_ops ? pm_ops : &mptcp_pm_kernel;
+}
+
+void mptcp_get_path_manager(const struct net *net, char *name)
+{
+	rcu_read_lock();
+	strscpy(name, mptcp_pernet_pm(mptcp_get_pernet(net))->name,
+		MPTCP_PM_NAME_MAX);
+	rcu_read_unlock();
 }
 
 static struct mptcp_sched_ops *mptcp_pernet_sched(struct mptcp_pernet *pernet)
@@ -124,7 +135,8 @@ static void mptcp_pernet_set_defaults(struct mptcp_pernet *pernet)
 	if (bpf_try_module_get(&mptcp_sched_default, mptcp_sched_default.owner))
 		RCU_INIT_POINTER(pernet->scheduler, &mptcp_sched_default);
 
-	strscpy(pernet->path_manager, "kernel", sizeof(pernet->path_manager));
+	RCU_INIT_POINTER(pernet->path_manager, &mptcp_pm_kernel);
+
 	pernet->add_addr_v6_port_drop_ts = 1;
 }
 
@@ -210,7 +222,7 @@ static int proc_blackhole_detect_timeout(const struct ctl_table *table,
 	return ret;
 }
 
-static int mptcp_set_path_manager(char *path_manager, const char *name)
+static int mptcp_set_path_manager(struct mptcp_pernet *pernet, const char *name)
 {
 	struct mptcp_pm_ops *pm_ops;
 	int ret = 0;
@@ -218,7 +230,7 @@ static int mptcp_set_path_manager(char *path_manager, const char *name)
 	rcu_read_lock();
 	pm_ops = mptcp_pm_find(name);
 	if (pm_ops)
-		strscpy(path_manager, name, MPTCP_PM_NAME_MAX);
+		xchg(&pernet->path_manager, pm_ops);
 	else
 		ret = -ENOENT;
 	rcu_read_unlock();
@@ -232,7 +244,6 @@ static int proc_path_manager(const struct ctl_table *ctl, int write,
 	struct mptcp_pernet *pernet = container_of(ctl->data,
 						   struct mptcp_pernet,
 						   path_manager);
-	char (*path_manager)[MPTCP_PM_NAME_MAX] = ctl->data;
 	char pm_name[MPTCP_PM_NAME_MAX];
 	const struct ctl_table tbl = {
 		.data = pm_name,
@@ -240,11 +251,13 @@ static int proc_path_manager(const struct ctl_table *ctl, int write,
 	};
 	int ret;
 
-	strscpy(pm_name, *path_manager, MPTCP_PM_NAME_MAX);
+	rcu_read_lock();
+	strscpy(pm_name, mptcp_pernet_pm(pernet)->name, MPTCP_PM_NAME_MAX);
+	rcu_read_unlock();
 
 	ret = proc_dostring(&tbl, write, buffer, lenp, ppos);
 	if (write && ret == 0) {
-		ret = mptcp_set_path_manager(*path_manager, pm_name);
+		ret = mptcp_set_path_manager(pernet, pm_name);
 		if (ret == 0) {
 			u8 pm_type = __MPTCP_PM_TYPE_NR;
 
@@ -276,7 +289,7 @@ static int proc_pm_type(const struct ctl_table *ctl, int write,
 			pm_name = "kernel";
 		else if (pm_type == MPTCP_PM_TYPE_USERSPACE)
 			pm_name = "userspace";
-		mptcp_set_path_manager(pernet->path_manager, pm_name);
+		mptcp_set_path_manager(pernet, pm_name);
 	}
 
 	return ret;
net/mptcp/pm.c +2 -1
index d7c5b50b34cc..69a38cb48977 100644
@@ -1204,7 +1204,7 @@ void mptcp_pm_destroy(struct mptcp_sock *msk)
 void mptcp_pm_data_reset(struct mptcp_sock *msk)
 {
 	const struct net *net = sock_net((struct sock *)msk);
-	const char *pm_name = mptcp_get_path_manager(net);
+	char pm_name[MPTCP_PM_NAME_MAX];
 	u8 pm_type = mptcp_get_pm_type(net);
 	struct mptcp_pm_data *pm = &msk->pm;
 
@@ -1213,6 +1213,7 @@ void mptcp_pm_data_reset(struct mptcp_sock *msk)
 	pm->rm_list_rx.nr = 0;
 	WRITE_ONCE(pm->pm_type, pm_type);
 
+	mptcp_get_path_manager(net, pm_name);
 	rcu_read_lock();
 	mptcp_pm_ops_init(msk, pm_name);
 	rcu_read_unlock();
net/mptcp/protocol.h +1 -2
index 5154c7ce3026..6f796d1c769a 100644
@@ -802,7 +802,7 @@ int mptcp_allow_join_id0(const struct net *net);
 unsigned int mptcp_stale_loss_cnt(const struct net *net);
 unsigned int mptcp_close_timeout(const struct sock *sk);
 int mptcp_get_pm_type(const struct net *net);
-const char *mptcp_get_path_manager(const struct net *net);
+void mptcp_get_path_manager(const struct net *net, char *name);
 struct mptcp_sched_ops *mptcp_get_scheduler(const struct net *net);
 unsigned int mptcp_add_addr_v6_port_drop_ts(const struct net *net);
 
@@ -1153,7 +1153,6 @@ int mptcp_pm_announce_addr(struct mptcp_sock *msk,
 			   bool echo);
 int mptcp_pm_remove_addr(struct mptcp_sock *msk, const struct mptcp_rm_list *rm_list);
 
-/* the default path manager, used in mptcp_pm_unregister */
 extern struct mptcp_pm_ops mptcp_pm_kernel;
 extern struct mptcp_sched_ops mptcp_sched_default;
 
Signature · 1 line
2.43.0
author
#3 lore raw

PATCH v6 3/6 mptcp: use READ_ONCE() over sysctls

In reply to Gang Yan

From: "Matthieu Baerts (NGI0)" <[email protected]>

To avoid KCSAN issues.

This patch is in theory for -net, and will need to be split in multiple patches, with different Fixes tags. But I prefer to wait for Eric's patches, as I noticed he already started to modify mptcp_is_enabled:

https://lore.kernel.org/CANn89iLdwhhwLyO6zRjWMEY3t9g60ZE8ZhOVx33ucg_uRETbmQ@mail.gmail.com

Still, keeping this patch in this series, not to forget about it.

---
 net/mptcp/ctrl.c | 16 ++++++++--------
 1 file changed, 8 insertions(+), 8 deletions(-)
net/mptcp/ctrl.c +8 -8
index 76ff2a41ba38..5a75f9b76d15 100644
@@ -50,39 +50,39 @@ static struct mptcp_pernet *mptcp_get_pernet(const struct net *net)
 
 int mptcp_is_enabled(const struct net *net)
 {
-	return mptcp_get_pernet(net)->mptcp_enabled;
+	return READ_ONCE(mptcp_get_pernet(net)->mptcp_enabled);
 }
 
 unsigned int mptcp_get_add_addr_timeout(const struct net *net)
 {
-	return mptcp_get_pernet(net)->add_addr_timeout;
+	return READ_ONCE(mptcp_get_pernet(net)->add_addr_timeout);
 }
 
 int mptcp_is_checksum_enabled(const struct net *net)
 {
-	return mptcp_get_pernet(net)->checksum_enabled;
+	return READ_ONCE(mptcp_get_pernet(net)->checksum_enabled);
 }
 
 int mptcp_allow_join_id0(const struct net *net)
 {
-	return mptcp_get_pernet(net)->allow_join_initial_addr_port;
+	return READ_ONCE(mptcp_get_pernet(net)->allow_join_initial_addr_port);
 }
 
 unsigned int mptcp_stale_loss_cnt(const struct net *net)
 {
-	return mptcp_get_pernet(net)->stale_loss_cnt;
+	return READ_ONCE(mptcp_get_pernet(net)->stale_loss_cnt);
 }
 
 unsigned int mptcp_close_timeout(const struct sock *sk)
 {
 	if (sock_flag(sk, SOCK_DEAD))
 		return TCP_TIMEWAIT_LEN;
-	return mptcp_get_pernet(sock_net(sk))->close_timeout;
+	return READ_ONCE(mptcp_get_pernet(sock_net(sk))->close_timeout);
 }
 
 int mptcp_get_pm_type(const struct net *net)
 {
-	return mptcp_get_pernet(net)->pm_type;
+	return READ_ONCE(mptcp_get_pernet(net)->pm_type);
 }
 
 static struct mptcp_pm_ops *mptcp_pernet_pm(struct mptcp_pernet *pernet)
@@ -586,7 +586,7 @@ void mptcp_active_detect_blackhole(struct sock *ssk, bool expired)
 
 	net = sock_net(ssk);
 	timeouts = inet_csk(ssk)->icsk_retransmits;
-	to_max = mptcp_get_pernet(net)->syn_retrans_before_tcp_fallback;
+	to_max = READ_ONCE(mptcp_get_pernet(net)->syn_retrans_before_tcp_fallback);
 
 	if (timeouts == to_max || (timeouts < to_max && expired)) {
 		subflow->mpc_drop = 1;
Signature · 1 line
2.43.0
author
#4 lore raw

PATCH v6 4/6 mptcp: pm: use WRITE_ONCE() for the pm_type sysctl

In reply to Gang Yan

From: Gang Yan <[email protected]>

Write pernet->pm_type with WRITE_ONCE() in proc_path_manager(), pairing it with the READ_ONCE() readers introduced earlier in this series.

Note that updating net.mptcp.path_manager swaps the ops pointer first, then writes the derived pm_type: a socket created in between may see the new ops with the old pm_type. As the net.mptcp.pm_type knob is deprecated since v6.15 and will be removed, the race is not fixed on purpose; document it above the assignment so it does not get reported again.

Assisted-by Claude:GLM5.2
---
 net/mptcp/ctrl.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
net/mptcp/ctrl.c +7 -1
index 5a75f9b76d15..87491b961bf2 100644
@@ -265,7 +265,13 @@ static int proc_path_manager(const struct ctl_table *ctl, int write,
 				pm_type = MPTCP_PM_TYPE_KERNEL;
 			else if (strncmp(pm_name, "userspace", MPTCP_PM_NAME_MAX) == 0)
 				pm_type = MPTCP_PM_TYPE_USERSPACE;
-			pernet->pm_type = pm_type;
+
+			/* Pre-existing race: two sequential writes, a socket
+			 * created in between may see the new ops with the old
+			 * pm_type. The knob is deprecated since v6.15 and will
+			 * be removed: not fixed on purpose.
+			 */
+			WRITE_ONCE(pernet->pm_type, pm_type);
 		}
 	}
 
Signature · 1 line
2.43.0
author
#5 lore raw

PATCH v6 5/6 Squash-to "mptcp: pm: init and release mptcp_pm_ops"

In reply to Gang Yan

From: Gang Yan <[email protected]>

This commit introduces the mptcp_pm_ops lifetime handling on sockets (mptcp_pm_ops_init/release taking a module reference), and would then be the first one whose per-net path managers can be unloaded while a pernet still stores them.

mptcp_pm_ops_init() also takes the ops pointer directly instead of the name, and mptcp_get_path_manager() returns the ops: the redundant mptcp_pm_find() list walk from the name is avoided, as done for the scheduler side earlier in this series.

Assisted-by Claude:GLM5.2
---
 net/mptcp/ctrl.c     | 35 +++++++++++++++++++++++++----------
 net/mptcp/pm.c       | 12 ++++--------
 net/mptcp/protocol.h |  2 +-
 3 files changed, 30 insertions(+), 19 deletions(-)
net/mptcp/ctrl.c +25 -10
index 87491b961bf2..6379a9f481ac 100644
@@ -93,12 +93,9 @@ static struct mptcp_pm_ops *mptcp_pernet_pm(struct mptcp_pernet *pernet)
 	return pm_ops ? pm_ops : &mptcp_pm_kernel;
 }
 
-void mptcp_get_path_manager(const struct net *net, char *name)
+struct mptcp_pm_ops *mptcp_get_path_manager(const struct net *net)
 {
-	rcu_read_lock();
-	strscpy(name, mptcp_pernet_pm(mptcp_get_pernet(net))->name,
-		MPTCP_PM_NAME_MAX);
-	rcu_read_unlock();
+	return mptcp_pernet_pm(mptcp_get_pernet(net));
 }
 
 static struct mptcp_sched_ops *mptcp_pernet_sched(struct mptcp_pernet *pernet)
@@ -135,7 +132,8 @@ static void mptcp_pernet_set_defaults(struct mptcp_pernet *pernet)
 	if (bpf_try_module_get(&mptcp_sched_default, mptcp_sched_default.owner))
 		RCU_INIT_POINTER(pernet->scheduler, &mptcp_sched_default);
 
-	RCU_INIT_POINTER(pernet->path_manager, &mptcp_pm_kernel);
+	if (bpf_try_module_get(&mptcp_pm_kernel, mptcp_pm_kernel.owner))
+		RCU_INIT_POINTER(pernet->path_manager, &mptcp_pm_kernel);
 
 	pernet->add_addr_v6_port_drop_ts = 1;
 }
@@ -224,15 +222,22 @@ static int proc_blackhole_detect_timeout(const struct ctl_table *table,
 
 static int mptcp_set_path_manager(struct mptcp_pernet *pernet, const char *name)
 {
-	struct mptcp_pm_ops *pm_ops;
+	struct mptcp_pm_ops *pm_ops, *prev;
 	int ret = 0;
 
 	rcu_read_lock();
 	pm_ops = mptcp_pm_find(name);
-	if (pm_ops)
-		xchg(&pernet->path_manager, pm_ops);
-	else
+	if (pm_ops) {
+		if (bpf_try_module_get(pm_ops, pm_ops->owner)) {
+			prev = xchg(&pernet->path_manager, pm_ops);
+			if (prev)
+				bpf_module_put(prev, prev->owner);
+		} else {
+			ret = -EBUSY;
+		}
+	} else {
 		ret = -ENOENT;
+	}
 	rcu_read_unlock();
 
 	return ret;
@@ -611,10 +616,15 @@ static int __net_init mptcp_net_init(struct net *net)
 	ret = mptcp_pernet_new_table(net, pernet);
 	if (ret) {
 		struct mptcp_sched_ops *sched;
+		struct mptcp_pm_ops *pm;
 
 		sched = rcu_dereference_protected(pernet->scheduler, true);
 		if (sched)
 			bpf_module_put(sched, sched->owner);
+
+		pm = rcu_dereference_protected(pernet->path_manager, true);
+		if (pm)
+			bpf_module_put(pm, pm->owner);
 	}
 
 	return ret;
@@ -625,12 +635,17 @@ static void __net_exit mptcp_net_exit(struct net *net)
 {
 	struct mptcp_pernet *pernet = mptcp_get_pernet(net);
 	struct mptcp_sched_ops *sched;
+	struct mptcp_pm_ops *pm;
 
 	mptcp_pernet_del_table(pernet);
 
 	sched = rcu_dereference_protected(pernet->scheduler, true);
 	if (sched)
 		bpf_module_put(sched, sched->owner);
+
+	pm = rcu_dereference_protected(pernet->path_manager, true);
+	if (pm)
+		bpf_module_put(pm, pm->owner);
 }
 
 static struct pernet_operations mptcp_pernet_ops = {
net/mptcp/pm.c +4 -8
index 69a38cb48977..64244a1a01bc 100644
@@ -1155,13 +1155,11 @@ void mptcp_pm_worker(struct mptcp_sock *msk)
 	spin_unlock_bh(&msk->pm.lock);
 }
 
-static void mptcp_pm_ops_init(struct mptcp_sock *msk, const char *pm_name)
+static void mptcp_pm_ops_init(struct mptcp_sock *msk,
+			      struct mptcp_pm_ops *pm_ops)
 {
-	struct mptcp_pm_ops *pm_ops;
-
-	pm_ops = mptcp_pm_find(pm_name);
 	if (!pm_ops || !bpf_try_module_get(pm_ops, pm_ops->owner)) {
-		pr_warn_once("pm %s fails, fallback to default pm", pm_name);
+		pr_warn_once("pm %s fails, fallback to default pm", pm_ops->name);
 		pm_ops = &mptcp_pm_kernel;
 	}
 
@@ -1204,7 +1202,6 @@ void mptcp_pm_destroy(struct mptcp_sock *msk)
 void mptcp_pm_data_reset(struct mptcp_sock *msk)
 {
 	const struct net *net = sock_net((struct sock *)msk);
-	char pm_name[MPTCP_PM_NAME_MAX];
 	u8 pm_type = mptcp_get_pm_type(net);
 	struct mptcp_pm_data *pm = &msk->pm;
 
@@ -1213,9 +1210,8 @@ void mptcp_pm_data_reset(struct mptcp_sock *msk)
 	pm->rm_list_rx.nr = 0;
 	WRITE_ONCE(pm->pm_type, pm_type);
 
-	mptcp_get_path_manager(net, pm_name);
 	rcu_read_lock();
-	mptcp_pm_ops_init(msk, pm_name);
+	mptcp_pm_ops_init(msk, mptcp_get_path_manager(net));
 	rcu_read_unlock();
 }
 
net/mptcp/protocol.h +1 -1
index 6f796d1c769a..4a7d7f1ab82e 100644
@@ -802,7 +802,7 @@ int mptcp_allow_join_id0(const struct net *net);
 unsigned int mptcp_stale_loss_cnt(const struct net *net);
 unsigned int mptcp_close_timeout(const struct sock *sk);
 int mptcp_get_pm_type(const struct net *net);
-void mptcp_get_path_manager(const struct net *net, char *name);
+struct mptcp_pm_ops *mptcp_get_path_manager(const struct net *net);
 struct mptcp_sched_ops *mptcp_get_scheduler(const struct net *net);
 unsigned int mptcp_add_addr_v6_port_drop_ts(const struct net *net);
 
Signature · 1 line
2.43.0
#6 lore raw

Re: [PATCH mptcp-next v6 5/6] Squash-to "mptcp: pm: init and release mptcp_pm_ops"

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] Contradictory claim in the commit message regarding module unloading. --

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] Contradictory claim in the commit message regarding module unloading. --

commit 954ec9b5378fa18a4b6badb35d44bfbe2edbd12a
Author: Gang Yan <[email protected]>

Squash-to "mptcp: pm: init and release mptcp_pm_ops"

This commit introduces mptcp_pm_ops lifetime handling on sockets and per-net path managers. It modifies path manager initialization and cleanup routines to use operations structs directly and take module references.

Quoted 4 lines

This commit introduces the mptcp_pm_ops lifetime handling on sockets (mptcp_pm_ops_init/release taking a module reference), and would then be the first one whose per-net path managers can be unloaded while a pernet still stores them.

[ ... ]

Quoted 4 lines
net/mptcp/ctrl.c +0 -0
index 87491b961bf27..6379a9f481ac6 100644

[ ... ]

Quoted 6 lines
net/mptcp/ctrl.c +1 -1
@@ -224,15 +222,22 @@ static int proc_blackhole_detect_timeout(const struct ctl_table *table,
 
 static int mptcp_set_path_manager(struct mptcp_pernet *pernet, const char *name)
 {
-	struct mptcp_pm_ops *pm_ops;
+	struct mptcp_pm_ops *pm_ops, *prev;

net/mptcp/ctrl.c static int proc_blackhole_detect_timeout(const struct ctl_table *table, · line 232

Quoted 10 lines
net/mptcp/ctrl.c +3 -3
 	int ret = 0;
 
 	rcu_read_lock();
 	pm_ops = mptcp_pm_find(name);
-	if (pm_ops)
-		xchg(&pernet->path_manager, pm_ops);
-	else
+	if (pm_ops) {
+		if (bpf_try_module_get(pm_ops, pm_ops->owner)) {
+			prev = xchg(&pernet->path_manager, pm_ops);

[Severity: Medium]
Does this code contradict the commit message regarding module unloading?

The commit message claims that per-net path managers "can be unloaded while a pernet still stores them." However, here in mptcp_set_path_manager() (as well as in mptcp_pernet_set_defaults()), bpf_try_module_get() is explicitly called before storing the pm_ops pointer in the per-net structure.

This increments the module's reference count, which fundamentally prevents the module from being unloaded (e.g., via rmmod) as long as the netns uses it.

Could the commit message or the implementation be clarified to resolve this mismatch?

Quoted 11 lines
net/mptcp/ctrl.c +7 -0
+			if (prev)
+				bpf_module_put(prev, prev->owner);
+		} else {
+			ret = -EBUSY;
+		}
+	} else {
 		ret = -ENOENT;
+	}
 	rcu_read_unlock();
 
 	return ret;
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=5
#7 lore raw

Re: [PATCH mptcp-next v6 5/6] Squash-to "mptcp: pm: init and release mptcp_pm_ops"

Quoted 56 lines · September 4, 2026 at 5:50 PM, [email protected]…

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] Contradictory claim in the commit message regarding module unloading. --

commit 954ec9b5378fa18a4b6badb35d44bfbe2edbd12a
Author: Gang Yan <[email protected]>

Squash-to "mptcp: pm: init and release mptcp_pm_ops"

This commit introduces mptcp_pm_ops lifetime handling on sockets and per-net path managers. It modifies path manager initialization and cleanup routines to use operations structs directly and take module references.

Quoted 6 lines

This commit introduces the mptcp_pm_ops lifetime handling on sockets
(mptcp_pm_ops_init/release taking a module reference), and would then be the first one whose per-net path managers can be unloaded while a pernet still stores them.

[ ... ]

Quoted 6 lines
net/mptcp/ctrl.c +0 -0
 index 87491b961bf27..6379a9f481ac6 100644
 --- a/net/mptcp/ctrl.c
 +++ b/net/mptcp/ctrl.c

[ ... ]

Quoted 8 lines
net/mptcp/ctrl.c +0 -0
@@ -224,15 +222,22 @@ static int proc_blackhole_detect_timeout(const struct ctl_table *table,
 
 static int mptcp_set_path_manager(struct mptcp_pernet *pernet, const char *name)
 {
 - struct mptcp_pm_ops *pm_ops;
 + struct mptcp_pm_ops *pm_ops, *prev;

net/mptcp/ctrl.c static int proc_blackhole_detect_timeout(const struct ctl_table *table, · line 236

Quoted 10 lines
net/mptcp/ctrl.c +0 -0
 int ret = 0;
 
 rcu_read_lock();
 pm_ops = mptcp_pm_find(name);
 - if (pm_ops)
 - xchg(&pernet->path_manager, pm_ops);
 - else
 + if (pm_ops) {
 + if (bpf_try_module_get(pm_ops, pm_ops->owner)) {
 + prev = xchg(&pernet->path_manager, pm_ops);

[Severity: Medium]
Does this code contradict the commit message regarding module unloading?

The commit message claims that per-net path managers "can be unloaded while a pernet still stores them." However, here in mptcp_set_path_manager() (as well as in mptcp_pernet_set_defaults()), bpf_try_module_get() is explicitly called before storing the pm_ops pointer in the per-net structure.

Quoted 5 lines

This increments the module's reference count, which fundamentally prevents the module from being unloaded (e.g., via rmmod) as long as the netns uses it.

Could the commit message or the implementation be clarified to resolve this mismatch?

This seems an error in commit message, will fix in next version.

Quoted 17 lines
Quoted 14 lines
net/mptcp/ctrl.c +1 -0
+ if (prev)
 + bpf_module_put(prev, prev->owner);
 + } else {
 + ret = -EBUSY;
 + }
 + } else {
 ret = -ENOENT;
 + }
 rcu_read_unlock();
 
 return ret;
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=5
author
#8 lore raw

PATCH v6 6/6 Squash to previous one

In reply to Gang Yan

From: Gang Yan <[email protected]>

Fix the issue sashiko mentioned in [1]. This patch applies the usual RCU discipline to the pointer: mark it __rcu, read it via rcu_dereference() inside RCU read sections, assign it via rcu_assign_pointer(), wait for a grace period before dropping the reference on the ops being replaced, and do the final release from mptcp_destroy() -- after the last msk reference -- instead of mptcp_destroy_common(), which is also reached on mptcp_disconnect().

Also, we need to hold the pm.lock before modify the pm.ops, and using the status of lock to tell rcu it's safe.

The previous ops is released (old->release() and the module reference, after the grace period) not only when it is replaced, but also when it is kept across a reset: this way a PM defining both init() and release() callbacks stays balanced over repeated disconnect/reuse cycles, instead of having release() deferred to the final destruction.

Note that rcu_assign_pointer() before pm_ops->init(msk) does not publish a partially initialised object: pm_ops->init() prepares the per-socket state, the ops themselves are registered immutable, and the msk is not reachable by readers until its token is registered.

For the current in-tree path managers, none of this changes
behaviour: neither defines release(), the per-socket state (announced list, userspace local address list) is freed unconditionally by mptcp_pm_destroy() on every disconnect, and mptcp_pm_kernel_init() only re-sets flags from the current sysctl, it does not allocate.

Keep it as a separate patch to ease the review, but to be squashed into the previous one, which is itself a squash-to for "mptcp: pm: init and release mptcp_pm_ops".

[1] https://sashiko.dev/#/patchset/[email protected]?part=5

Assisted-by Claude:GLM5.2
---
 net/mptcp/pm.c       | 63 ++++++++++++++++++++++++++++++++++----------
 net/mptcp/protocol.c |  4 +++
 net/mptcp/protocol.h |  3 ++-
 net/mptcp/subflow.c  |  9 ++++++-
 4 files changed, 63 insertions(+), 16 deletions(-)
net/mptcp/pm.c +49 -14
index 64244a1a01bc..d581d11b350f 100644
@@ -27,6 +27,14 @@ static LIST_HEAD(mptcp_pm_list);
 
 /* path manager helpers */
 
+static struct mptcp_pm_ops *mptcp_pm_rcu_deref(struct mptcp_sock *msk)
+{
+	struct mptcp_pm_ops *pm_ops;
+
+	pm_ops = rcu_dereference(msk->pm.ops);
+	return pm_ops ? pm_ops : &mptcp_pm_kernel;
+}
+
 /* if sk is ipv4 or ipv6_only allows only same-family local and remote addresses,
  * otherwise allow any matching local/remote pair
  */
@@ -1049,7 +1057,7 @@ int mptcp_pm_get_local_id(struct mptcp_sock *msk, struct sock_common *skc)
 	skc_local.addr.id = 0;
 	skc_local.flags = MPTCP_PM_ADDR_FLAG_IMPLICIT;
 
-	return msk->pm.ops->get_local_id(msk, &skc_local);
+	return mptcp_pm_rcu_deref(msk)->get_local_id(msk, &skc_local);
 }
 
 bool mptcp_pm_is_backup(struct mptcp_sock *msk, struct sock_common *skc)
@@ -1058,7 +1066,7 @@ bool mptcp_pm_is_backup(struct mptcp_sock *msk, struct sock_common *skc)
 
 	mptcp_local_address((struct sock_common *)skc, &skc_local);
 
-	return msk->pm.ops->get_priority(msk, &skc_local);
+	return mptcp_pm_rcu_deref(msk)->get_priority(msk, &skc_local);
 }
 
 static void
@@ -1158,23 +1166,44 @@ void mptcp_pm_worker(struct mptcp_sock *msk)
 static void mptcp_pm_ops_init(struct mptcp_sock *msk,
 			      struct mptcp_pm_ops *pm_ops)
 {
-	if (!pm_ops || !bpf_try_module_get(pm_ops, pm_ops->owner)) {
-		pr_warn_once("pm %s fails, fallback to default pm", pm_ops->name);
-		pm_ops = &mptcp_pm_kernel;
+	struct mptcp_pm_ops *old;
+	bool need_sync = false;
+
+	spin_lock_bh(&msk->pm.lock);
+	old = rcu_dereference_protected(msk->pm.ops,
+					lockdep_is_held(&msk->pm.lock));
+	if (old == pm_ops) {
+		need_sync = false;
+	} else {
+		rcu_assign_pointer(msk->pm.ops, pm_ops);
+		need_sync = !!old;
 	}
+	spin_unlock_bh(&msk->pm.lock);
 
-	msk->pm.ops = pm_ops;
-	if (msk->pm.ops->init)
-		msk->pm.ops->init(msk);
+	if (need_sync)
+		synchronize_rcu();
+	if (old) {
+		if (old->release)
+			old->release(msk);
+		bpf_module_put(old, old->owner);
+	}
+
+	if (pm_ops->init)
+		pm_ops->init(msk);
 
 	pr_debug("pm %s initialized\n", pm_ops->name);
 }
 
-static void mptcp_pm_ops_release(struct mptcp_sock *msk)
+void mptcp_pm_ops_release(struct mptcp_sock *msk)
 {
-	struct mptcp_pm_ops *pm_ops = msk->pm.ops;
+	struct mptcp_pm_ops *pm_ops;
+
+	spin_lock_bh(&msk->pm.lock);
+	pm_ops = rcu_dereference_protected(msk->pm.ops,
+					   lockdep_is_held(&msk->pm.lock));
+	rcu_assign_pointer(msk->pm.ops, NULL);
+	spin_unlock_bh(&msk->pm.lock);
 
-	msk->pm.ops = NULL;
 	if (pm_ops->release)
 		pm_ops->release(msk);
 
@@ -1195,8 +1224,6 @@ void mptcp_pm_destroy(struct mptcp_sock *msk)
 	 * can be reused (mptcp_disconnect()) and re-selected to a different PM
 	 */
 	mptcp_userspace_pm_free_local_addr_list(msk);
-
-	mptcp_pm_ops_release(msk);
 }
 
 void mptcp_pm_data_reset(struct mptcp_sock *msk)
@@ -1204,6 +1231,7 @@ void mptcp_pm_data_reset(struct mptcp_sock *msk)
 	const struct net *net = sock_net((struct sock *)msk);
 	u8 pm_type = mptcp_get_pm_type(net);
 	struct mptcp_pm_data *pm = &msk->pm;
+	struct mptcp_pm_ops *pm_ops;
 
 	memset(&pm->reset, 0, sizeof(pm->reset));
 	pm->rm_list_tx.nr = 0;
@@ -1211,8 +1239,15 @@ void mptcp_pm_data_reset(struct mptcp_sock *msk)
 	WRITE_ONCE(pm->pm_type, pm_type);
 
 	rcu_read_lock();
-	mptcp_pm_ops_init(msk, mptcp_get_path_manager(net));
+	pm_ops = mptcp_get_path_manager(net);
+	if (!pm_ops || !bpf_try_module_get(pm_ops, pm_ops->owner)) {
+		pr_warn_once("pm %s fails, fallback to default pm",
+			     pm_ops ? pm_ops->name : NULL);
+		pm_ops = &mptcp_pm_kernel;
+	}
 	rcu_read_unlock();
+
+	mptcp_pm_ops_init(msk, pm_ops);
 }
 
 void mptcp_pm_data_init(struct mptcp_sock *msk)
net/mptcp/protocol.c +4 -0
index 179eb6bcebf8..6dbd9a085025 100644
@@ -3756,6 +3756,9 @@ struct sock *mptcp_sk_clone_init(const struct sock *sk,
 		inet_sk(nsk)->pinet6 = mptcp_inet6_sk(nsk);
 #endif
 
+	msk = mptcp_sk(nsk);
+	RCU_INIT_POINTER(msk->pm.ops, NULL);
+
 	__mptcp_init_sock(nsk);
 
 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
@@ -3825,6 +3828,7 @@ static void mptcp_destroy(struct sock *sk)
 	/* allow the following to close even the initial subflow */
 	msk->free_first = 1;
 	mptcp_destroy_common(msk);
+	mptcp_pm_ops_release(msk);
 	sk_sockets_allocated_dec(sk);
 }
 
net/mptcp/protocol.h +2 -1
index 4a7d7f1ab82e..c5f581acd13d 100644
@@ -222,7 +222,7 @@ struct mptcp_pm_data {
 	struct mptcp_addr_info remote;
 	struct list_head anno_list;
 	struct list_head userspace_pm_local_addr_list;
-	struct mptcp_pm_ops *ops;
+	struct mptcp_pm_ops __rcu *ops;	/* RCU: read via mptcp_pm_rcu_deref() */
 
 	spinlock_t	lock;		/*protects the whole PM data */
 
@@ -1101,6 +1101,7 @@ void __init mptcp_pm_init(void);
 void mptcp_pm_data_init(struct mptcp_sock *msk);
 void mptcp_pm_data_reset(struct mptcp_sock *msk);
 void mptcp_pm_destroy(struct mptcp_sock *msk);
+void mptcp_pm_ops_release(struct mptcp_sock *msk);
 int mptcp_pm_parse_addr(struct nlattr *attr, struct genl_info *info,
 			struct mptcp_addr_info *addr);
 int mptcp_pm_parse_entry(struct nlattr *attr, struct genl_info *info,
net/mptcp/subflow.c +8 -1
index 2d7ccb01d234..6cb0ab1fbb0f 100644
@@ -94,14 +94,17 @@ static struct mptcp_sock *subflow_token_join_request(struct request_sock *req)
 		return NULL;
 	}
 
+	rcu_read_lock();
 	local_id = mptcp_pm_get_local_id(msk, (struct sock_common *)req);
 	if (local_id < 0) {
 		SUBFLOW_REQ_INC_STATS(req, MPTCP_MIB_MPJOINNOIDFOUND);
+		rcu_read_unlock();
 		sock_put((struct sock *)msk);
 		return NULL;
 	}
 	subflow_req->local_id = local_id;
 	subflow_req->request_bkup = mptcp_pm_is_backup(msk, (struct sock_common *)req);
+	rcu_read_unlock();
 
 	return msk;
 }
@@ -634,12 +637,16 @@ static int subflow_chk_local_id(struct sock *sk)
 	if (likely(subflow->local_id >= 0))
 		return 0;
 
+	rcu_read_lock();
 	err = mptcp_pm_get_local_id(msk, (struct sock_common *)sk);
-	if (err < 0)
+	if (err < 0) {
+		rcu_read_unlock();
 		return err;
+	}
 
 	subflow_set_local_id(subflow, err);
 	subflow->request_bkup = mptcp_pm_is_backup(msk, (struct sock_common *)sk);
+	rcu_read_unlock();
 
 	return 0;
 }
Signature · 1 line
2.43.0
#9 lore raw

Re: [PATCH mptcp-next v6 6/6] Squash to previous one

Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:

Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:

New issues:
- [High] Bypassing RCU grace period in mptcp_pm_ops_init() when the PM remains unchanged causes Use-After-Free and data races.
- [High] Publishing new PM ops before initializing its per-socket state exposes uninitialized memory to RCU readers.

Pre-existing issues:
- [High] Module reference count underflow when falling back to the default path manager. --

commit 261318f67b36cf52f532df8226134e77ddd65cd0
Author: Gang Yan <[email protected]>

Squash to previous one

This patch updates the MPTCP path manager operations to use RCU for concurrent reads. It modifies mptcp_pm_ops_init() and mptcp_pm_ops_release() to safely handle the assignment and release of the ops pointer, and adds RCU read locks in the subflow paths.

Quoted 4 lines
net/mptcp/pm.c +0 -0
index 64244a1a01bca..d581d11b350f2 100644

[ ... ]

Quoted 7 lines
net/mptcp/pm.c +0 -3
@@ -1158,23 +1166,44 @@ void mptcp_pm_worker(struct mptcp_sock *msk)
 static void mptcp_pm_ops_init(struct mptcp_sock *msk,
 			      struct mptcp_pm_ops *pm_ops)
 {
-	if (!pm_ops || !bpf_try_module_get(pm_ops, pm_ops->owner)) {
-		pr_warn_once("pm %s fails, fallback to default pm", pm_ops->name);
-		pm_ops = &mptcp_pm_kernel;

net/mptcp/pm.c void mptcp_pm_worker(struct mptcp_sock *msk) · line 1178

Quoted 10 lines
net/mptcp/pm.c +10 -0
+	struct mptcp_pm_ops *old;
+	bool need_sync = false;
+
+	spin_lock_bh(&msk->pm.lock);
+	old = rcu_dereference_protected(msk->pm.ops,
+					lockdep_is_held(&msk->pm.lock));
+	if (old == pm_ops) {
+		need_sync = false;
+	} else {
+		rcu_assign_pointer(msk->pm.ops, pm_ops);

[Severity: High]
Does this code publish the new PM operations pointer to readers before its per-socket state is initialized?

If msk->pm.ops is updated via rcu_assign_pointer() here, existing RCU readers like subflow_token_join_request() could dereference the new pm_ops and execute its methods before pm_ops->init(msk) has run below. For example, if a reader calls mptcp_pm_get_local_id() and then mptcp_pm_is_backup() in the same RCU section, it might see the new uninitialized pm_ops for the second call.

net/mptcp/pm.c void mptcp_pm_worker(struct mptcp_sock *msk) · line 1187

Quoted 12 lines
net/mptcp/pm.c +7 -3
+		need_sync = !!old;
 	}
+	spin_unlock_bh(&msk->pm.lock);
 
-	msk->pm.ops = pm_ops;
-	if (msk->pm.ops->init)
-		msk->pm.ops->init(msk);
+	if (need_sync)
+		synchronize_rcu();
+	if (old) {
+		if (old->release)
+			old->release(msk);

[Severity: High]
Could this bypass the required RCU grace period and cause a use-after-free?

If old == pm_ops, need_sync is set to false, skipping synchronize_rcu(). However, the code still calls old->release(msk) immediately here. If concurrent lockless RCU readers (e.g., in subflow_token_join_request()) are actively executing path manager methods on the socket, old->release(msk) can free the per-socket state while they are using it.

Quoted 8 lines
net/mptcp/pm.c +5 -0
+		bpf_module_put(old, old->owner);
+	}
+
+	if (pm_ops->init)
+		pm_ops->init(msk);
 
 	pr_debug("pm %s initialized\n", pm_ops->name);
 }

[ ... ]

net/mptcp/pm.c void mptcp_pm_data_reset(struct mptcp_sock *msk) · line 1246

Quoted 10 lines
net/mptcp/pm.c +5 -1
@@ -1211,8 +1239,15 @@ void mptcp_pm_data_reset(struct mptcp_sock *msk)
 	WRITE_ONCE(pm->pm_type, pm_type);
 
 	rcu_read_lock();
-	mptcp_pm_ops_init(msk, mptcp_get_path_manager(net));
+	pm_ops = mptcp_get_path_manager(net);
+	if (!pm_ops || !bpf_try_module_get(pm_ops, pm_ops->owner)) {
+		pr_warn_once("pm %s fails, fallback to default pm",
+			     pm_ops ? pm_ops->name : NULL);
+		pm_ops = &mptcp_pm_kernel;

[Severity: High]
This is a pre-existing issue, but could this fallback cause a module reference count underflow?

If bpf_try_module_get() fails or the requested PM is NULL, pm_ops is set to &mptcp_pm_kernel without incrementing its module reference count.

Later, mptcp_pm_ops_release() unconditionally calls bpf_module_put(pm_ops, pm_ops->owner). Since the reference was never acquired, this causes an underflow of the MPTCP module's reference count. If MPTCP is built as a module, this could allow the module to be unloaded while sockets are still alive.

Quoted 5 lines
net/mptcp/pm.c +3 -0
+	}
 	rcu_read_unlock();
+
+	mptcp_pm_ops_init(msk, pm_ops);
 }
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=6
#10 lore raw

Re: [PATCH mptcp-next v6 6/6] Squash to previous one

Quoted 109 lines · September 4, 2026 at 5:51 PM, [email protected]…

Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:

New issues:
- [High] Bypassing RCU grace period in mptcp_pm_ops_init() when the PM remains unchanged causes Use-After-Free and data races.
- [High] Publishing new PM ops before initializing its per-socket state exposes uninitialized memory to RCU readers.

Pre-existing issues:
- [High] Module reference count underflow when falling back to the default path manager. --

commit 261318f67b36cf52f532df8226134e77ddd65cd0
Author: Gang Yan <[email protected]>

Squash to previous one

This patch updates the MPTCP path manager operations to use RCU for concurrent reads. It modifies mptcp_pm_ops_init() and mptcp_pm_ops_release() to safely handle the assignment and release of the ops pointer, and adds RCU read locks in the subflow paths.

Quoted 6 lines
net/mptcp/pm.c +0 -0
 index 64244a1a01bca..d581d11b350f2 100644
 --- a/net/mptcp/pm.c
 +++ b/net/mptcp/pm.c

[ ... ]

Quoted 9 lines
net/mptcp/pm.c +0 -0
@@ -1158,23 +1166,44 @@ void mptcp_pm_worker(struct mptcp_sock *msk)
 static void mptcp_pm_ops_init(struct mptcp_sock *msk,
 struct mptcp_pm_ops *pm_ops)
 {
 - if (!pm_ops || !bpf_try_module_get(pm_ops, pm_ops->owner)) {
 - pr_warn_once("pm %s fails, fallback to default pm", pm_ops->name);
 - pm_ops = &mptcp_pm_kernel;
 + struct mptcp_pm_ops *old;

net/mptcp/pm.c void mptcp_pm_worker(struct mptcp_sock *msk) · line 1182

Quoted 10 lines
net/mptcp/pm.c +0 -0
 + bool need_sync = false;
 +
 + spin_lock_bh(&msk->pm.lock);
 + old = rcu_dereference_protected(msk->pm.ops,
 + lockdep_is_held(&msk->pm.lock));
 + if (old == pm_ops) {
 + need_sync = false;
 + } else {
 + rcu_assign_pointer(msk->pm.ops, pm_ops);
 

[Severity: High]
Does this code publish the new PM operations pointer to readers before its per-socket state is initialized?

If msk->pm.ops is updated via rcu_assign_pointer() here, existing RCU readers like subflow_token_join_request() could dereference the new pm_ops and execute its methods before pm_ops->init(msk) has run below. For example, if a reader calls mptcp_pm_get_local_id() and then mptcp_pm_is_backup() in the same RCU section, it might see the new uninitialized pm_ops for the second call.

Quoted 4 lines
net/mptcp/pm.c +1 -0
+ need_sync = !!old;
 }

net/mptcp/pm.c void mptcp_pm_worker(struct mptcp_sock *msk)

Quoted 10 lines
net/mptcp/pm.c +0 -0
 + spin_unlock_bh(&msk->pm.lock);
 
 - msk->pm.ops = pm_ops;
 - if (msk->pm.ops->init)
 - msk->pm.ops->init(msk);
 + if (need_sync)
 + synchronize_rcu();
 + if (old) {
 + if (old->release)
 + old->release(msk);

[Severity: High]
Could this bypass the required RCU grace period and cause a use-after-free?

If old == pm_ops, need_sync is set to false, skipping synchronize_rcu(). However, the code still calls old->release(msk) immediately here. If concurrent lockless RCU readers (e.g., in subflow_token_join_request()) are actively executing path manager methods on the socket, old->release(msk) can free the per-socket state while they are using it.

Quoted 10 lines

+ bpf_module_put(old, old->owner);
+ }
+
+ if (pm_ops->init)
+ pm_ops->init(msk);

pr_debug("pm %s initialized\n", pm_ops->name);
}

[ ... ]

net/mptcp/pm.c void mptcp_pm_data_reset(struct mptcp_sock *msk)

Quoted 12 lines
net/mptcp/pm.c +0 -0
@@ -1211,8 +1239,15 @@ void mptcp_pm_data_reset(struct mptcp_sock *msk)
 WRITE_ONCE(pm->pm_type, pm_type);
 
 rcu_read_lock();
 - mptcp_pm_ops_init(msk, mptcp_get_path_manager(net));
 + pm_ops = mptcp_get_path_manager(net);
 + if (!pm_ops || !bpf_try_module_get(pm_ops, pm_ops->owner)) {
 + pr_warn_once("pm %s fails, fallback to default pm",
 + pm_ops ? pm_ops->name : NULL);
 + pm_ops = &mptcp_pm_kernel;

[Severity: High]
This is a pre-existing issue, but could this fallback cause a module reference count underflow?

Quoted 15 lines

If bpf_try_module_get() fails or the requested PM is NULL, pm_ops is set to &mptcp_pm_kernel without incrementing its module reference count.

Later, mptcp_pm_ops_release() unconditionally calls bpf_module_put(pm_ops, pm_ops->owner). Since the reference was never acquired, this causes an underflow of the MPTCP module's reference count. If MPTCP is built as a module, this could allow the module to be unloaded while sockets are still alive.

Quoted 6 lines

+ }
rcu_read_unlock();
+
+ mptcp_pm_ops_init(msk, pm_ops);
}

Will be handled in the next version.

Quoted 4 lines
Quoted 1 line
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=6
#11 lore raw
In reply to Gang Yan

Hi Gang,

Thank you for your modifications, that's great!

Our CI did some validations and here is its report:

- KVM Validation: normal (except selftest_mptcp_join): Success! ✅
- KVM Validation: normal (only selftest_mptcp_join): Success! ✅
- KVM Validation: debug (except selftest_mptcp_join): Success! ✅
- KVM Validation: debug (only selftest_mptcp_join): Success! ✅
- KVM Validation: btf-normal (only bpftest_all): Success! ✅
- KVM Validation: btf-debug (only bpftest_all): Success! ✅
- Perf:
- Task: https://github.com/multipath-tcp/mptcp_net-next/actions/runs/33860787080

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

If there are some issues, you can reproduce them using the same environment as the one used by the CI thanks to a docker image, e.g.:

    $ cd [kernel source code]
    $ docker run -v "${PWD}:${PWD}:rw" -w "${PWD}" --privileged --rm -it \
        --pull always mptcp/mptcp-upstream-virtme-docker:latest \
        auto-normal

For more details:

https://github.com/multipath-tcp/mptcp-upstream-virtme-docker

Please note that despite all the efforts that have been already done to have a stable tests suite when executed on a public CI like here, it is possible some reported issues are not due to your modifications. Still, do not hesitate to help us improve that ;-)

Cheers,
MPTCP GH Action bot
Bot operated by Matthieu Baerts (NGI0 Core)