* [PATCH mptcp-next v6 1/6] mptcp: sched: change scheduler sysctl atomically
2026-09-04 9:35 [PATCH mptcp-next v6 0/6] mptcp: avoid data-races around the sysctls Gang Yan
@ 2026-09-04 9:35 ` Gang Yan
2026-09-04 9:35 ` [PATCH mptcp-next v6 2/6] mptcp: pm: change path_manager " Gang Yan
` (5 subsequent siblings)
6 siblings, 0 replies; 11+ messages in thread
From: Gang Yan @ 2026-09-04 9:35 UTC (permalink / raw)
To: mptcp
From: Gang Yan <yangang@kylinos.cn>
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.
Assisted-by: Claude:GLM5.2
Closes: https://github.com/multipath-tcp/mptcp_net-next/issues/626
Co-developed-by: Tao Cui <cuitao@kylinos.cn>
Signed-off-by: Tao Cui <cuitao@kylinos.cn>
Signed-off-by: Gang Yan <yangang@kylinos.cn>
---
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(-)
diff --git a/net/mptcp/ctrl.c b/net/mptcp/ctrl.c
index 63c5747f0f63..7d0f3421bd04 100644
--- a/net/mptcp/ctrl.c
+++ b/net/mptcp/ctrl.c
@@ -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 = {
diff --git a/net/mptcp/protocol.c b/net/mptcp/protocol.c
index 0b24e0afedfb..179eb6bcebf8 100644
--- a/net/mptcp/protocol.c
+++ b/net/mptcp/protocol.c
@@ -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;
diff --git a/net/mptcp/protocol.h b/net/mptcp/protocol.h
index b3121c8c766b..5154c7ce3026 100644
--- a/net/mptcp/protocol.h
+++ b/net/mptcp/protocol.h
@@ -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);
diff --git a/net/mptcp/sched.c b/net/mptcp/sched.c
index 1e59072d478c..0d13ee46ffdf 100644
--- a/net/mptcp/sched.c
+++ b/net/mptcp/sched.c
@@ -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",
--
2.43.0
^ permalink raw reply related [flat|nested] 11+ messages in thread* [PATCH mptcp-next v6 2/6] mptcp: pm: change path_manager sysctl atomically
2026-09-04 9:35 [PATCH mptcp-next v6 0/6] mptcp: avoid data-races around the sysctls Gang Yan
2026-09-04 9:35 ` [PATCH mptcp-next v6 1/6] mptcp: sched: change scheduler sysctl atomically Gang Yan
@ 2026-09-04 9:35 ` Gang Yan
2026-09-04 9:35 ` [PATCH mptcp-next v6 3/6] mptcp: use READ_ONCE() over sysctls Gang Yan
` (4 subsequent siblings)
6 siblings, 0 replies; 11+ messages in thread
From: Gang Yan @ 2026-09-04 9:35 UTC (permalink / raw)
To: mptcp
From: Gang Yan <yangang@kylinos.cn>
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.
Assisted-by: Claude:GLM5.2
Closes: https://github.com/multipath-tcp/mptcp_net-next/issues/626
Co-developed-by: Tao Cui <cuitao@kylinos.cn>
Signed-off-by: Tao Cui <cuitao@kylinos.cn>
Signed-off-by: Gang Yan <yangang@kylinos.cn>
---
net/mptcp/ctrl.c | 33 +++++++++++++++++++++++----------
net/mptcp/pm.c | 3 ++-
net/mptcp/protocol.h | 3 +--
3 files changed, 26 insertions(+), 13 deletions(-)
diff --git a/net/mptcp/ctrl.c b/net/mptcp/ctrl.c
index 7d0f3421bd04..76ff2a41ba38 100644
--- a/net/mptcp/ctrl.c
+++ b/net/mptcp/ctrl.c
@@ -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;
diff --git a/net/mptcp/pm.c b/net/mptcp/pm.c
index d7c5b50b34cc..69a38cb48977 100644
--- a/net/mptcp/pm.c
+++ b/net/mptcp/pm.c
@@ -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();
diff --git a/net/mptcp/protocol.h b/net/mptcp/protocol.h
index 5154c7ce3026..6f796d1c769a 100644
--- a/net/mptcp/protocol.h
+++ b/net/mptcp/protocol.h
@@ -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;
--
2.43.0
^ permalink raw reply related [flat|nested] 11+ messages in thread* [PATCH mptcp-next v6 3/6] mptcp: use READ_ONCE() over sysctls
2026-09-04 9:35 [PATCH mptcp-next v6 0/6] mptcp: avoid data-races around the sysctls Gang Yan
2026-09-04 9:35 ` [PATCH mptcp-next v6 1/6] mptcp: sched: change scheduler sysctl atomically Gang Yan
2026-09-04 9:35 ` [PATCH mptcp-next v6 2/6] mptcp: pm: change path_manager " Gang Yan
@ 2026-09-04 9:35 ` Gang Yan
2026-09-04 9:35 ` [PATCH mptcp-next v6 4/6] mptcp: pm: use WRITE_ONCE() for the pm_type sysctl Gang Yan
` (3 subsequent siblings)
6 siblings, 0 replies; 11+ messages in thread
From: Gang Yan @ 2026-09-04 9:35 UTC (permalink / raw)
To: mptcp
From: "Matthieu Baerts (NGI0)" <matttbe@kernel.org>
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.
Reported-by: Eric Dumazet <edumazet@google.com>
Closes: https://lore.kernel.org/CANn89iL=os-60kDKqMDdyiXuPF5CG=eejS0vmthwpDGXz_Bp8A@mail.gmail.com
Signed-off-by: Matthieu Baerts (NGI0) <matttbe@kernel.org>
---
net/mptcp/ctrl.c | 16 ++++++++--------
1 file changed, 8 insertions(+), 8 deletions(-)
diff --git a/net/mptcp/ctrl.c b/net/mptcp/ctrl.c
index 76ff2a41ba38..5a75f9b76d15 100644
--- a/net/mptcp/ctrl.c
+++ b/net/mptcp/ctrl.c
@@ -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;
--
2.43.0
^ permalink raw reply related [flat|nested] 11+ messages in thread* [PATCH mptcp-next v6 4/6] mptcp: pm: use WRITE_ONCE() for the pm_type sysctl
2026-09-04 9:35 [PATCH mptcp-next v6 0/6] mptcp: avoid data-races around the sysctls Gang Yan
` (2 preceding siblings ...)
2026-09-04 9:35 ` [PATCH mptcp-next v6 3/6] mptcp: use READ_ONCE() over sysctls Gang Yan
@ 2026-09-04 9:35 ` Gang Yan
2026-09-04 9:35 ` [PATCH mptcp-next v6 5/6] Squash-to "mptcp: pm: init and release mptcp_pm_ops" Gang Yan
` (2 subsequent siblings)
6 siblings, 0 replies; 11+ messages in thread
From: Gang Yan @ 2026-09-04 9:35 UTC (permalink / raw)
To: mptcp
From: Gang Yan <yangang@kylinos.cn>
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.
Suggested-by: Matthieu Baerts <matttbe@kernel.org>
Assisted-by: Claude:GLM5.2
Co-developed-by: Tao Cui <cuitao@kylinos.cn>
Signed-off-by: Tao Cui <cuitao@kylinos.cn>
Signed-off-by: Gang Yan <yangang@kylinos.cn>
---
net/mptcp/ctrl.c | 8 +++++++-
1 file changed, 7 insertions(+), 1 deletion(-)
diff --git a/net/mptcp/ctrl.c b/net/mptcp/ctrl.c
index 5a75f9b76d15..87491b961bf2 100644
--- a/net/mptcp/ctrl.c
+++ b/net/mptcp/ctrl.c
@@ -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);
}
}
--
2.43.0
^ permalink raw reply related [flat|nested] 11+ messages in thread* [PATCH mptcp-next v6 5/6] Squash-to "mptcp: pm: init and release mptcp_pm_ops"
2026-09-04 9:35 [PATCH mptcp-next v6 0/6] mptcp: avoid data-races around the sysctls Gang Yan
` (3 preceding siblings ...)
2026-09-04 9:35 ` [PATCH mptcp-next v6 4/6] mptcp: pm: use WRITE_ONCE() for the pm_type sysctl Gang Yan
@ 2026-09-04 9:35 ` Gang Yan
2026-09-04 9:50 ` sashiko-bot
2026-09-04 9:35 ` [PATCH mptcp-next v6 6/6] Squash to previous one Gang Yan
2026-09-04 10:45 ` [PATCH mptcp-next v6 0/6] mptcp: avoid data-races around the sysctls MPTCP CI
6 siblings, 1 reply; 11+ messages in thread
From: Gang Yan @ 2026-09-04 9:35 UTC (permalink / raw)
To: mptcp
From: Gang Yan <yangang@kylinos.cn>
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
Co-developed-by: Tao Cui <cuitao@kylinos.cn>
Signed-off-by: Tao Cui <cuitao@kylinos.cn>
Signed-off-by: Gang Yan <yangang@kylinos.cn>
---
net/mptcp/ctrl.c | 35 +++++++++++++++++++++++++----------
net/mptcp/pm.c | 12 ++++--------
net/mptcp/protocol.h | 2 +-
3 files changed, 30 insertions(+), 19 deletions(-)
diff --git a/net/mptcp/ctrl.c b/net/mptcp/ctrl.c
index 87491b961bf2..6379a9f481ac 100644
--- a/net/mptcp/ctrl.c
+++ b/net/mptcp/ctrl.c
@@ -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 = {
diff --git a/net/mptcp/pm.c b/net/mptcp/pm.c
index 69a38cb48977..64244a1a01bc 100644
--- a/net/mptcp/pm.c
+++ b/net/mptcp/pm.c
@@ -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();
}
diff --git a/net/mptcp/protocol.h b/net/mptcp/protocol.h
index 6f796d1c769a..4a7d7f1ab82e 100644
--- a/net/mptcp/protocol.h
+++ b/net/mptcp/protocol.h
@@ -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);
--
2.43.0
^ permalink raw reply related [flat|nested] 11+ messages in thread* Re: [PATCH mptcp-next v6 5/6] Squash-to "mptcp: pm: init and release mptcp_pm_ops"
2026-09-04 9:35 ` [PATCH mptcp-next v6 5/6] Squash-to "mptcp: pm: init and release mptcp_pm_ops" Gang Yan
@ 2026-09-04 9:50 ` sashiko-bot
0 siblings, 0 replies; 11+ messages in thread
From: sashiko-bot @ 2026-09-04 9:50 UTC (permalink / raw)
To: Gang Yan; +Cc: mptcp
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 <yangang@kylinos.cn>
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.
> 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.
[ ... ]
> diff --git a/net/mptcp/ctrl.c b/net/mptcp/ctrl.c
> index 87491b961bf27..6379a9f481ac6 100644
> --- a/net/mptcp/ctrl.c
> +++ b/net/mptcp/ctrl.c
[ ... ]
> @@ -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);
[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?
> + if (prev)
> + bpf_module_put(prev, prev->owner);
> + } else {
> + ret = -EBUSY;
> + }
> + } else {
> ret = -ENOENT;
> + }
> rcu_read_unlock();
>
> return ret;
--
Sashiko AI review · https://sashiko.dev/#/patchset/20260904093531.20023-1-gang.yan@linux.dev?part=5
^ permalink raw reply [flat|nested] 11+ messages in thread
* [PATCH mptcp-next v6 6/6] Squash to previous one
2026-09-04 9:35 [PATCH mptcp-next v6 0/6] mptcp: avoid data-races around the sysctls Gang Yan
` (4 preceding siblings ...)
2026-09-04 9:35 ` [PATCH mptcp-next v6 5/6] Squash-to "mptcp: pm: init and release mptcp_pm_ops" Gang Yan
@ 2026-09-04 9:35 ` Gang Yan
2026-09-04 9:51 ` sashiko-bot
2026-09-04 10:45 ` [PATCH mptcp-next v6 0/6] mptcp: avoid data-races around the sysctls MPTCP CI
6 siblings, 1 reply; 11+ messages in thread
From: Gang Yan @ 2026-09-04 9:35 UTC (permalink / raw)
To: mptcp
From: Gang Yan <yangang@kylinos.cn>
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/20260819125629.49823-1-gang.yan@linux.dev?part=5
Assisted-by: Claude:GLM5.2
Co-developed-by: Tao Cui <cuitao@kylinos.cn>
Signed-off-by: Tao Cui <cuitao@kylinos.cn>
Signed-off-by: Gang Yan <yangang@kylinos.cn>
---
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(-)
diff --git a/net/mptcp/pm.c b/net/mptcp/pm.c
index 64244a1a01bc..d581d11b350f 100644
--- a/net/mptcp/pm.c
+++ b/net/mptcp/pm.c
@@ -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)
diff --git a/net/mptcp/protocol.c b/net/mptcp/protocol.c
index 179eb6bcebf8..6dbd9a085025 100644
--- a/net/mptcp/protocol.c
+++ b/net/mptcp/protocol.c
@@ -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);
}
diff --git a/net/mptcp/protocol.h b/net/mptcp/protocol.h
index 4a7d7f1ab82e..c5f581acd13d 100644
--- a/net/mptcp/protocol.h
+++ b/net/mptcp/protocol.h
@@ -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,
diff --git a/net/mptcp/subflow.c b/net/mptcp/subflow.c
index 2d7ccb01d234..6cb0ab1fbb0f 100644
--- a/net/mptcp/subflow.c
+++ b/net/mptcp/subflow.c
@@ -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;
}
--
2.43.0
^ permalink raw reply related [flat|nested] 11+ messages in thread* Re: [PATCH mptcp-next v6 6/6] Squash to previous one
2026-09-04 9:35 ` [PATCH mptcp-next v6 6/6] Squash to previous one Gang Yan
@ 2026-09-04 9:51 ` sashiko-bot
2026-09-10 11:14 ` gang.yan
0 siblings, 1 reply; 11+ messages in thread
From: sashiko-bot @ 2026-09-04 9:51 UTC (permalink / raw)
To: Gang Yan; +Cc: mptcp
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 <yangang@kylinos.cn>
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.
> diff --git a/net/mptcp/pm.c b/net/mptcp/pm.c
> index 64244a1a01bca..d581d11b350f2 100644
> --- a/net/mptcp/pm.c
> +++ b/net/mptcp/pm.c
[ ... ]
> @@ -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);
[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.
> + 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.
> + bpf_module_put(old, old->owner);
> + }
> +
> + if (pm_ops->init)
> + pm_ops->init(msk);
>
> pr_debug("pm %s initialized\n", pm_ops->name);
> }
[ ... ]
> @@ -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.
> + }
> rcu_read_unlock();
> +
> + mptcp_pm_ops_init(msk, pm_ops);
> }
--
Sashiko AI review · https://sashiko.dev/#/patchset/20260904093531.20023-1-gang.yan@linux.dev?part=6
^ permalink raw reply [flat|nested] 11+ messages in thread* Re: [PATCH mptcp-next v6 6/6] Squash to previous one
2026-09-04 9:51 ` sashiko-bot
@ 2026-09-10 11:14 ` gang.yan
0 siblings, 0 replies; 11+ messages in thread
From: gang.yan @ 2026-09-10 11:14 UTC (permalink / raw)
To: sashiko-reviews; +Cc: mptcp
September 4, 2026 at 5:51 PM, sashiko-bot@kernel.org mailto:sashiko-bot@kernel.org wrote:
>
> 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 <yangang@kylinos.cn>
>
> 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.
>
> >
> > diff --git a/net/mptcp/pm.c b/net/mptcp/pm.c
> > index 64244a1a01bca..d581d11b350f2 100644
> > --- a/net/mptcp/pm.c
> > +++ b/net/mptcp/pm.c
> >
> [ ... ]
>
> >
> > @@ -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);
> >
> [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.
>
> >
> > + 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.
>
> >
> > + bpf_module_put(old, old->owner);
> > + }
> > +
> > + if (pm_ops->init)
> > + pm_ops->init(msk);
> >
> > pr_debug("pm %s initialized\n", pm_ops->name);
> > }
> >
> [ ... ]
>
> >
> > @@ -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.
>
> >
> > + }
> > rcu_read_unlock();
> > +
> > + mptcp_pm_ops_init(msk, pm_ops);
> > }
Will be handled in the next version.
> >
> --
> Sashiko AI review · https://sashiko.dev/#/patchset/20260904093531.20023-1-gang.yan@linux.dev?part=6
>
^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: [PATCH mptcp-next v6 0/6] mptcp: avoid data-races around the sysctls
2026-09-04 9:35 [PATCH mptcp-next v6 0/6] mptcp: avoid data-races around the sysctls Gang Yan
` (5 preceding siblings ...)
2026-09-04 9:35 ` [PATCH mptcp-next v6 6/6] Squash to previous one Gang Yan
@ 2026-09-04 10:45 ` MPTCP CI
6 siblings, 0 replies; 11+ messages in thread
From: MPTCP CI @ 2026-09-04 10:45 UTC (permalink / raw)
To: Gang Yan; +Cc: mptcp
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)
^ permalink raw reply [flat|nested] 11+ messages in thread