mptcp: avoid data-races around the sysctls
Subsystems (2)
cover mptcp: avoid data-races around the sysctls
From: Gang Yan <[email protected]>
Hi,
As decribed in [1], this series is for addressing the KCSAN issues in sysctls.
Following the tcp_congestion_control() model, patch 1 and 2 now store pointers to the immutable ops and swap them atomically via xchg().
Patch 3 (from Matthieu) covers the remaining scalar sysctls with READ_ONCE().
[1] https://github.com/multipath-tcp/mptcp_net-next/issues/626
Gang Yan (2):
mptcp: sched: change scheduler sysctl atomically
mptcp: pm: change path_manager sysctl atomically
Matthieu Baerts (NGI0) (1):
mptcp: use READ_ONCE() over sysctls
net/mptcp/ctrl.c | 116 ++++++++++++++++++++++++++++++------------- net/mptcp/pm.c | 3 +- net/mptcp/protocol.c | 5 +- net/mptcp/protocol.h | 6 +-- net/mptcp/sched.c | 2 +- 5 files changed, 91 insertions(+), 41 deletions(-)
Signature · 1 line
2.43.0
PATCH 1/3 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:
- mptcp_set_scheduler() now looks the ops up, takes a module reference, and atomically swaps the pernet pointer with xchg(), releasing the previous reference;
- mptcp_get_scheduler() copies the ops name out under rcu_read_lock();
- the default reference is taken in mptcp_pernet_set_defaults() and released in mptcp_net_exit(), mirroring tcp_sk_init()/tcp_sk_exit().
A pointer store is a single atomic word, so readers always observe a consistent value.
--- net/mptcp/ctrl.c | 50 +++++++++++++++++++++++++++++++++----------- net/mptcp/protocol.c | 5 +++-- net/mptcp/protocol.h | 3 ++- net/mptcp/sched.c | 2 +- 4 files changed, 44 insertions(+), 16 deletions(-)
net/mptcp/ctrl.c +38 -12
@@ -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,14 @@ 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) +void mptcp_get_scheduler (const struct net *net,char *name) { -return mptcp_get_pernet (net)->scheduler; +struct mptcp_sched_ops *sched; + +rcu_read_lock (); + sched =rcu_dereference (mptcp_get_pernet (net)->scheduler); +strscpy (name, sched ? sched->name :"default" ,MPTCP_SCHED_NAME_MAX ); +rcu_read_unlock (); }unsigned int mptcp_add_addr_v6_port_drop_ts (const struct net *net)
@@ -112,23 +117,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 +152,10 @@ 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); +struct mptcp_sched_ops *sched;char val[MPTCP_SCHED_NAME_MAX ];struct ctl_table tbl = { .data = val,
@@ -145,11 +163,14 @@ static int proc_scheduler(const struct ctl_table *ctl, int write, };int ret; -strscpy (val, *scheduler,MPTCP_SCHED_NAME_MAX ); +rcu_read_lock (); + sched =rcu_dereference (pernet->scheduler); +strscpy (val, sched ? sched->name :"default" ,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; }
@@ -573,8 +594,13 @@ static int __net_init mptcp_net_init(struct net *net) 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 +3 -2
@@ -3260,6 +3260,7 @@ static void mptcp_ca_reset(struct sock *sk)static int mptcp_init_sock (struct sock *sk) {struct net *net =sock_net (sk); +char sched_name[MPTCP_SCHED_NAME_MAX ];int ret;__mptcp_init_sock (sk);
@@ -3271,8 +3272,8 @@ 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))); +mptcp_get_scheduler (net, sched_name); + ret =mptcp_init_sched (mptcp_sk (sk),mptcp_sched_find (sched_name));rcu_read_unlock ();if (ret)return ret;
net/mptcp/protocol.h +2 -1
@@ -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); +void mptcp_get_scheduler (const struct net *net,char *name);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
@@ -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
PATCH 2/3 mptcp: pm: change path_manager sysctl atomically
In reply to Gang YanFrom: 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 (and KCSAN flags the race), 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:
- mptcp_set_path_manager() looks the ops up, takes a module reference, and atomically swaps the pernet pointer with xchg(), releasing the previous reference;
- mptcp_get_path_manager() copies the ops name out under rcu_read_lock();
- the pm_type field derived from the name is now written
with WRITE_ONCE() to pair with its READ_ONCE() readers;
- the default reference is taken in mptcp_pernet_set_defaults() and released in mptcp_net_exit().
--- net/mptcp/ctrl.c | 50 +++++++++++++++++++++++++++++++------------- net/mptcp/pm.c | 3 ++- net/mptcp/protocol.h | 3 +-- 3 files changed, 39 insertions(+), 17 deletions(-)
net/mptcp/ctrl.c +36 -14
@@ -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,14 @@ 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) +void mptcp_get_path_manager (const struct net *net,char *name) { -return mptcp_get_pernet (net)->path_manager; +struct mptcp_pm_ops *pm_ops; + +rcu_read_lock (); + pm_ops =rcu_dereference (mptcp_get_pernet (net)->path_manager); +strscpy (name, pm_ops ? pm_ops->name :"kernel" ,MPTCP_PM_NAME_MAX ); +rcu_read_unlock (); }void mptcp_get_scheduler (const struct net *net,char *name)
@@ -121,7 +126,9 @@ 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)); +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 ; }
@@ -209,17 +216,24 @@ 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; +struct mptcp_pm_ops *pm_ops, *prev;int ret =0 ;rcu_read_lock (); pm_ops =mptcp_pm_find (name); -if (pm_ops) -strscpy (path_manager, name,MPTCP_PM_NAME_MAX ); -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;
@@ -231,7 +245,7 @@ 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; +struct mptcp_pm_ops *pm_ops;char pm_name[MPTCP_PM_NAME_MAX ];const struct ctl_table tbl = { .data = pm_name,
@@ -239,11 +253,14 @@ 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 (); + pm_ops =rcu_dereference (pernet->path_manager); +strscpy (pm_name, pm_ops ? pm_ops->name :"kernel" ,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;
@@ -251,7 +268,7 @@ 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; +WRITE_ONCE (pernet->pm_type, pm_type); } }
@@ -275,7 +292,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;
@@ -595,12 +612,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 +2 -1
@@ -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
@@ -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);void mptcp_get_scheduler (const struct net *net,char *name);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
PATCH 3/3 mptcp: use READ_ONCE() over sysctls
In reply to Gang YanFrom: "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
@@ -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); }void mptcp_get_path_manager (const struct net *net,char *name)
@@ -589,7 +589,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
Hi Matthieu,
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! ✅
- Task: https://github.com/multipath-tcp/mptcp_net-next/actions/runs/31986048689
Initiator: Patchew Applier
Commits: https://github.com/multipath-tcp/mptcp_net-next/commits/fa35ee6d384f
Patchwork: https://patchwork.kernel.org/project/mptcp/list/?series=1146872
If there are some issues, you can reproduce them using the same environment as the one used by the CI thanks to a docker image, e.g.:
$ cd [kernel source code]
$ docker run -v "${PWD}:${PWD}:rw" -w "${PWD}" --privileged --rm -it \
--pull always mptcp/mptcp-upstream-virtme-docker:latest \
auto-normalFor more details:
https://github.com/multipath-tcp/mptcp-upstream-virtme-docker
Please note that despite all the efforts that have been already done to have a stable tests suite when executed on a public CI like here, it is possible some reported issues are not due to your modifications. Still, do not hesitate to help us improve that ;-)
Cheers,
MPTCP GH Action bot
Bot operated by Matthieu Baerts (NGI0 Core)
Matthieu Baerts
maintainerHi Gang Yan,
Quoted 7 lines · Gang Yan
From: Gang Yan <[email protected]>
Hi,
As decribed in [1], this series is for addressing the KCSAN issues in sysctls.
Quoted 6 lines
Following the
tcp_congestion_control()model, patch 1 and 2 now store pointers to the immutable ops and swap them atomically viaxchg().Patch 3 (from Matthieu) covers the remaining scalar sysctls with
READ_ONCE().[1] https://github.com/multipath-tcp/mptcp_net-next/issues/626
Thank you for looking at that with Tao Cui.
Globally, it looks good to me, but I have a few small requests:
- Do you mind resending this series, please: for some unknown reasons, Sashiko didn't get it. But before: ↓
- Can you not add Eric in Cc of the 3rd patch? Better not to spam him for dev work → b4 send --no-trailer-to-cc ; or: git send-email --suppress-cc=misc-by
- Could you split the code around bpf module support? I guess these patches will be sent before adding BPF PM/sched support. In this case, you would have squash-to patches for the BPF parts at the end of the series.
- I think you can move the WRITE_ONCE(pernet->pm_type, pm_type); from current patch 2 to 3, with Tao Cui's Co-dev.
Cheers,
Matt
Signature · 1 line
Sponsored by the NGI0 Core fund.