mptcp: fix duplicate subflow creation
Subsystems (4)
Paolo Abeni
maintainercover mptcp: fix duplicate subflow creation
As reported by Mat, the in kernel PM can, in some edge scenarios, unexpectedly create multiple subflows with the same local and remote address.
The real fix is implemented by patch 4/4 with some more accurate check at subflow creation time.
Patches 1-3 are roughly optional pre-requisities, added to avoid introducing more data-races with the actual fix. Patch 1/4 is a bit debatable, as it changes the existing ULP API, but I could not find a better solution and there is some similar prior art:
commit 0df48c26d841 ("tcp: add tcpi_bytes_acked to tcp_info")
Paolo Abeni (4):
mptcp: fix lockless access in subflow ULP diag
mptcp: fix data races on local_id
mptcp: fix data races on remote_id
mptcp: fix duplicate subflow creation
include/net/tcp.h | 2 +- net/mptcp/diag.c | 6 +++++- net/mptcp/pm_netlink.c | 43 ++++++++++++++++++++++-------------------- net/mptcp/protocol.c | 2 +- net/mptcp/protocol.h | 15 ++++++++++++--- net/mptcp/subflow.c | 15 ++++++++------- net/tls/tls_main.c | 2 +- 7 files changed, 51 insertions(+), 34 deletions(-)
Signature · 1 line
2.43.0
Paolo Abeni
maintainerPATCH 1/4 mptcp: fix lockless access in subflow ULP diag
Since the introduction of the subflow ULP diag interface, the dump callback accessed all the subflow data with lockless.
We need either to annotate all the read and write operation accordingly, or acquire the subflow socket lock. Let's do latter, even if slower, to avoid a diffstat havoc.
---
note: tls ulp diag has likely the same issue.
--- include/net/tcp.h | 2 +- net/mptcp/diag.c | 6 +++++- net/tls/tls_main.c | 2 +- 3 files changed, 7 insertions(+), 3 deletions(-)
include/net/tcp.h +1 -1
@@ -2551,7 +2551,7 @@ struct tcp_ulp_ops {/* cleanup ulp */ void (*release)(struct sock *sk);/* diagnostic */ -int (*get_info)(const struct sock *sk,struct sk_buff *skb); +int (*get_info)(struct sock *sk,struct sk_buff *skb);size_t (*get_info_size)(const struct sock *sk);/* clone ulp */ void (*clone)(const struct request_sock *req, struct sock *newsk,
net/mptcp/diag.c +5 -1
@@ -13,17 +13,19 @@#include <uapi/linux/mptcp.h> #include "protocol.h" -static int subflow_get_info (const struct sock *sk,struct sk_buff *skb) +static int subflow_get_info (struct sock *sk,struct sk_buff *skb) {struct mptcp_subflow_context *sf;struct nlattr *start;u32 flags =0 ; +bool slow;int err; start =nla_nest_start_noflag (skb,INET_ULP_INFO_MPTCP );if (!start)return -EMSGSIZE ; + slow =lock_sock_fast (sk);rcu_read_lock (); sf =rcu_dereference (inet_csk (sk)->icsk_ulp_data);if (!sf) {
@@ -69,11 +71,13 @@ static int subflow_get_info(const struct sock *sk, struct sk_buff *skb) }rcu_read_unlock (); +unlock_sock_fast (sk, slow);nla_nest_end (skb, start);return 0 ; nla_failure:rcu_read_unlock (); +unlock_sock_fast (sk, slow);nla_nest_cancel (skb, start);return err; }
net/tls/tls_main.c +1 -1
@@ -1003,7 +1003,7 @@ static u16 tls_user_config(struct tls_context *ctx, bool tx)return 0 ; } -static int tls_get_info (const struct sock *sk,struct sk_buff *skb) +static int tls_get_info (struct sock *sk,struct sk_buff *skb) {u16 version, cipher_type;struct tls_context *ctx;
Signature · 1 line
2.43.0
Paolo Abeni
maintainerPATCH 2/4 mptcp: fix data races on local_id
In reply to Paolo AbeniThe local address id is accessed lockless by the NL PM, add all the required ONCE annotation. There is a caveat: the local id can be initialized late in the subflow life-cycle, and its validity is controlled by the local_id_valid flag.
Remove such flag and encode the validity in the local_id field itself with negative value before initialization. That allows accessing the field consistently with a single read operation.
--- net/mptcp/pm_netlink.c | 4 ++-- net/mptcp/protocol.c | 2 +- net/mptcp/protocol.h | 15 ++++++++++++--- net/mptcp/subflow.c | 9 +++++---- 4 files changed, 20 insertions(+), 10 deletions(-)
net/mptcp/pm_netlink.c +2 -2
@@ -800,7 +800,7 @@ static void mptcp_pm_nl_rm_addr_or_subflow(struct mptcp_sock *msk,mptcp_for_each_subflow_safe (msk, subflow, tmp) {struct sock *ssk =mptcp_subflow_tcp_sock (subflow);int how =RCV_SHUTDOWN |SEND_SHUTDOWN ; -u8 id = subflow->local_id; +u8 id =subflow_get_local_id (subflow);if (rm_type ==MPTCP_MIB_RMADDR && subflow->remote_id != rm_id)continue ;
@@ -809,7 +809,7 @@ static void mptcp_pm_nl_rm_addr_or_subflow(struct mptcp_sock *msk,pr_debug (" -> %s rm_list_ids[%d]=%u local_id=%u remote_id=%u mpc_id=%u" , rm_type ==MPTCP_MIB_RMADDR ?"address" :"subflow" , - i, rm_id, subflow->local_id, subflow->remote_id, + i, rm_id, id, subflow->remote_id, msk->mpc_endpoint_id);spin_unlock_bh (&msk->pm.lock);mptcp_subflow_shutdown (sk, ssk, how);
net/mptcp/protocol.c +1 -1
@@ -85,7 +85,7 @@ static int __mptcp_socket_create(struct mptcp_sock *msk) subflow->subflow_id = msk->subflow_id++;/* This is the first subflow, always with id 0 */ - subflow->local_id_valid =1 ; +WRITE_ONCE (subflow->local_id,0 );mptcp_sock_graft (msk->first, sk->sk_socket);iput (SOCK_INODE (ssock));
net/mptcp/protocol.h +12 -3
@@ -493,10 +493,9 @@ struct mptcp_subflow_context { remote_key_valid : 1, /* received the peer key from */ disposable : 1, /* ctx can be free at ulp release time */ stale : 1, /* unable to snd/rcv data, do not use for xmit */ - local_id_valid : 1, /* local_id is correctly initialized */ valid_csum_seen : 1, /* at least one csum validated */ is_mptfo : 1, /* subflow is doing TFO */ - __unused : 9; + __unused : 10;bool data_avail;bool scheduled;u32 remote_nonce;
@@ -507,7 +506,7 @@ struct mptcp_subflow_context {u8 hmac[MPTCPOPT_HMAC_LEN ];/* MPJ subflow only */ u64 iasn;/* initial ack sequence number, MPC subflows only */ }; -u8 local_id; +s16 local_id;/* if negative not initialized yet */ u8 remote_id; u8 reset_seen:1; u8 reset_transient:1;
@@ -558,6 +557,7 @@ mptcp_subflow_ctx_reset(struct mptcp_subflow_context *subflow) {memset (&subflow->reset,0 ,sizeof (subflow->reset)); subflow->request_mptcp =1 ; +WRITE_ONCE (subflow->local_id,-1 ); } static inline u64
@@ -1064,6 +1064,15 @@ int mptcp_pm_get_local_id(struct mptcp_sock *msk, struct sock_common *skc);int mptcp_pm_nl_get_local_id (struct mptcp_sock *msk,struct mptcp_addr_info *skc);int mptcp_userspace_pm_get_local_id (struct mptcp_sock *msk,struct mptcp_addr_info *skc); +static inline int subflow_get_local_id (struct mptcp_subflow_context *subflow) +{ +int local_id =READ_ONCE (subflow->local_id); + +if (local_id <0 ) +return 0 ; +return local_id; +} +void __init mptcp_pm_nl_init (void );void mptcp_pm_nl_work (struct mptcp_sock *msk); void mptcp_pm_nl_rm_subflow_received(struct mptcp_sock *msk,
net/mptcp/subflow.c +5 -4
@@ -578,8 +578,8 @@ static void subflow_finish_connect(struct sock *sk, const struct sk_buff *skb)static void subflow_set_local_id (struct mptcp_subflow_context *subflow,int local_id) { - subflow->local_id = local_id; - subflow->local_id_valid =1 ; +WARN_ON_ONCE (local_id <0 || local_id >255 ); +WRITE_ONCE (subflow->local_id, local_id); }static int subflow_chk_local_id (struct sock *sk)
@@ -588,7 +588,7 @@ static int subflow_chk_local_id(struct sock *sk)struct mptcp_sock *msk =mptcp_sk (subflow->conn);int err; -if (likely (subflow->local_id_valid)) +if (likely (subflow->local_id >=0 ))return 0 ; err =mptcp_pm_get_local_id (msk, (struct sock_common *)sk);
@@ -1733,6 +1733,7 @@ static struct mptcp_subflow_context *subflow_create_ctx(struct sock *sk,pr_debug ("subflow=%p" , ctx); ctx->tcp_sock = sk; +WRITE_ONCE (ctx->local_id,-1 );return ctx; }
@@ -1968,7 +1969,7 @@ static void subflow_ulp_clone(const struct request_sock *req, new_ctx->idsn = subflow_req->idsn;/* this is the first subflow, id is always 0 */ - new_ctx->local_id_valid =1 ; +subflow_set_local_id (new_ctx,0 ); }else if (subflow_req->mp_join) { new_ctx->ssn_offset = subflow_req->ssn_offset; new_ctx->mp_join =1 ;
Signature · 1 line
2.43.0
Paolo Abeni
maintainerPATCH 3/4 mptcp: fix data races on remote_id
In reply to Paolo AbeniSimilar to the previous patch, address the data race on remote_id, adding the suitable ONCE annotations.
--- net/mptcp/pm_netlink.c | 8 ++++---- net/mptcp/subflow.c | 6 +++--- 2 files changed, 7 insertions(+), 7 deletions(-)
net/mptcp/pm_netlink.c +4 -4
@@ -443,7 +443,7 @@ static unsigned int fill_remote_addresses_vec(struct mptcp_sock *msk,mptcp_for_each_subflow (msk, subflow) { ssk =mptcp_subflow_tcp_sock (subflow);remote_address ((struct sock_common *)ssk, &addrs[i]); - addrs[i].id = subflow->remote_id; + addrs[i].id =READ_ONCE (subflow->remote_id);if (deny_id0 && !addrs[i].id)continue ;
@@ -800,17 +800,17 @@ static void mptcp_pm_nl_rm_addr_or_subflow(struct mptcp_sock *msk,mptcp_for_each_subflow_safe (msk, subflow, tmp) {struct sock *ssk =mptcp_subflow_tcp_sock (subflow);int how =RCV_SHUTDOWN |SEND_SHUTDOWN ; +u8 srm_id =READ_ONCE (subflow->remote_id);u8 id =subflow_get_local_id (subflow); -if (rm_type ==MPTCP_MIB_RMADDR && subflow->remote_id != rm_id) +if (rm_type ==MPTCP_MIB_RMADDR && srm_id != rm_id)continue ;if (rm_type ==MPTCP_MIB_RMSUBFLOW && !mptcp_local_id_match (msk, id, rm_id))continue ;pr_debug (" -> %s rm_list_ids[%d]=%u local_id=%u remote_id=%u mpc_id=%u" , rm_type ==MPTCP_MIB_RMADDR ?"address" :"subflow" , - i, rm_id, id, subflow->remote_id, - msk->mpc_endpoint_id); + i, rm_id, id, srm_id, msk->mpc_endpoint_id);spin_unlock_bh (&msk->pm.lock);mptcp_subflow_shutdown (sk, ssk, how);
net/mptcp/subflow.c +3 -3
@@ -536,7 +536,7 @@ static void subflow_finish_connect(struct sock *sk, const struct sk_buff *skb) subflow->backup = mp_opt.backup; subflow->thmac = mp_opt.thmac; subflow->remote_nonce = mp_opt.nonce; - subflow->remote_id = mp_opt.join_id; +WRITE_ONCE (subflow->remote_id, mp_opt.join_id);pr_debug ("subflow=%p, thmac=%llu, remote_nonce=%u backup=%d" , subflow, subflow->thmac, subflow->remote_nonce, subflow->backup);
@@ -1569,7 +1569,7 @@ int __mptcp_subflow_connect(struct sock *sk, const struct mptcp_addr_info *loc,pr_debug ("msk=%p remote_token=%u local_id=%d remote_id=%d" , msk, remote_token, local_id, remote_id); subflow->remote_token = remote_token; - subflow->remote_id = remote_id; +WRITE_ONCE (subflow->remote_id, remote_id); subflow->request_join =1 ; subflow->request_bkup = !!(flags &MPTCP_PM_ADDR_FLAG_BACKUP ); subflow->subflow_id = msk->subflow_id++;
@@ -1976,7 +1976,7 @@ static void subflow_ulp_clone(const struct request_sock *req, new_ctx->fully_established =1 ; new_ctx->remote_key_valid =1 ; new_ctx->backup = subflow_req->backup; - new_ctx->remote_id = subflow_req->remote_id; +WRITE_ONCE (new_ctx->remote_id, subflow_req->remote_id); new_ctx->token = subflow_req->token; new_ctx->thmac = subflow_req->thmac;
Signature · 1 line
2.43.0
Paolo Abeni
maintainerPATCH 4/4 mptcp: fix duplicate subflow creation
In reply to Paolo AbeniFullmesh endpoints could end-up unexpectedly generating duplicate subflows - same local and remote addresses - when multiple incoming ADD_ADDR are processed before the PM creates the subflow for the local endpoints.
Address the issue explicitly checking for duplicates at subflow creation time.
To avoid a quadratic computational complexity, track the unavailable remote address ids in a temporary bitmap and initialize such bitmap with the remote ids of all the existing subflows matching the local address currently processed.
The above allows additionally replacing the existing code checking for duplicate entry in the current set with a simple bit test operation.
---
Note that there is no problem for the opposite event sequence.
--- net/mptcp/pm_netlink.c | 33 ++++++++++++++++++--------------- 1 file changed, 18 insertions(+), 15 deletions(-)
net/mptcp/pm_netlink.c +18 -15
@@ -396,19 +396,6 @@ void mptcp_pm_free_anno_list(struct mptcp_sock *msk) } } -static bool lookup_address_in_vec (const struct mptcp_addr_info *addrs,unsigned int nr, -const struct mptcp_addr_info *addr) -{ -int i; - -for (i =0 ; i < nr; i++) { -if (addrs[i].id == addr->id) -return true; - } - -return false; -} -/* Fill all the remote addresses into the array addrs[], * and return the array size. */
@@ -440,6 +427,16 @@ static unsigned int fill_remote_addresses_vec(struct mptcp_sock *msk, msk->pm.subflows++; addrs[i++] = remote; }else { +DECLARE_BITMAP (unavail_id,MPTCP_PM_MAX_ADDR_ID +1 ); + +/* Forbit creation of new subflows matching existing +* ones, possibly already created by incoming ADD_ADDR +*/ +bitmap_zero (unavail_id,MPTCP_PM_MAX_ADDR_ID +1 ); +mptcp_for_each_subflow (msk, subflow) +if (READ_ONCE (subflow->local_id) == local->id) +__set_bit (subflow->remote_id, unavail_id); +mptcp_for_each_subflow (msk, subflow) { ssk =mptcp_subflow_tcp_sock (subflow);remote_address ((struct sock_common *)ssk, &addrs[i]);
@@ -447,11 +444,17 @@ static unsigned int fill_remote_addresses_vec(struct mptcp_sock *msk,if (deny_id0 && !addrs[i].id)continue ; +if (test_bit (addrs[i].id, unavail_id)) +continue ; +if (!mptcp_pm_addr_families_match (sk, local, &addrs[i]))continue ; -if (!lookup_address_in_vec (addrs, i, &addrs[i]) && - msk->pm.subflows < subflows_max) { +if (msk->pm.subflows < subflows_max) { +/* forbit creating multiple address towards +* this id +*/ +__set_bit (addrs[i].id, unavail_id); msk->pm.subflows++; i++; }
Signature · 1 line
2.43.0
Re: mptcp: fix duplicate subflow creation: Tests Results
Hi Paolo,
Thank you for your modifications, that's great!
Our CI (GitHub Action) did some validations and here is its report:
- KVM Validation: normal:
- Success! ✅:
- Task: https://github.com/multipath-tcp/mptcp_net-next/actions/runs/7787400213
Initiator: Patchew Applier
Commits: https://github.com/multipath-tcp/mptcp_net-next/commits/75f455f724d8
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)
Re: mptcp: fix duplicate subflow creation: Tests Results
In reply to Paolo AbeniHi Paolo,
Thank you for your modifications, that's great!
Our CI (Cirrus) did some validations with a debug kernel and here is its report:
- KVM Validation: debug (except selftest_mptcp_join):
- Unstable: 1 failed test(s): packetdrill_regressions 🔴:
- Task: https://cirrus-ci.com/task/5560160119160832
- Summary: https://api.cirrus-ci.com/v1/artifact/task/5560160119160832/summary/summary.txt
- KVM Validation: debug (only selftest_mptcp_join):
- Success! ✅:
- Task: https://cirrus-ci.com/task/6686060026003456
- Summary: https://api.cirrus-ci.com/v1/artifact/task/6686060026003456/summary/summary.txt
Initiator: Patchew Applier
Commits: https://github.com/multipath-tcp/mptcp_net-next/commits/75f455f724d8
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-debugFor 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)
Mat Martineau
maintainerRe: [PATCH mptcp-net 2/4] mptcp: fix data races on local_id
In reply to Paolo AbeniQuoted 87 lines · Paolo Abeni
The local address id is accessed lockless by the NL PM, add all the required ONCE annotation. There is a caveat: the local id can be initialized late in the subflow life-cycle, and its validity is controlled by the
local_id_validflag.Remove such flag and encode the validity in the
local_idfield itself with negative value before initialization. That allows accessing the field consistently with a single read operation.Fixes 0ee4261a3681 ("mptcp: implement mptcp_pm_remove_subflow")Signed-off-by Paolo Abeni <[email protected]>---
net/mptcp/pm_netlink.c | 4 ++-- net/mptcp/protocol.c | 2 +- net/mptcp/protocol.h | 15 ++++++++++++--- net/mptcp/subflow.c | 9 +++++---- 4 files changed, 20 insertions(+), 10 deletions(-)net/mptcp/pm_netlink.c +2
-2@@ -800,7 +800,7 @@ static void mptcp_pm_nl_rm_addr_or_subflow(struct mptcp_sock *msk,mptcp_for_each_subflow_safe (msk, subflow, tmp) {struct sock *ssk =mptcp_subflow_tcp_sock (subflow);int how =RCV_SHUTDOWN |SEND_SHUTDOWN ; -u8 id = subflow->local_id; +u8 id =subflow_get_local_id (subflow);if (rm_type ==MPTCP_MIB_RMADDR && subflow->remote_id != rm_id)continue ;@@ -809,7 +809,7 @@ static void mptcp_pm_nl_rm_addr_or_subflow(struct mptcp_sock *msk,pr_debug (" -> %s rm_list_ids[%d]=%u local_id=%u remote_id=%u mpc_id=%u" , rm_type ==MPTCP_MIB_RMADDR ?"address" :"subflow" , - i, rm_id, subflow->local_id, subflow->remote_id, + i, rm_id, id, subflow->remote_id, msk->mpc_endpoint_id);spin_unlock_bh (&msk->pm.lock);mptcp_subflow_shutdown (sk, ssk, how);net/mptcp/protocol.c +1
-1@@ -85,7 +85,7 @@ static int __mptcp_socket_create(struct mptcp_sock *msk) subflow->subflow_id = msk->subflow_id++;/* This is the first subflow, always with id 0 */ - subflow->local_id_valid =1 ; +WRITE_ONCE (subflow->local_id,0 );mptcp_sock_graft (msk->first, sk->sk_socket);iput (SOCK_INODE (ssock));net/mptcp/protocol.h +3
-3@@ -493,10 +493,9 @@ struct mptcp_subflow_context { remote_key_valid : 1, /* received the peer key from */ disposable : 1, /* ctx can be free at ulp release time */ stale : 1, /* unable to snd/rcv data, do not use for xmit */ - local_id_valid : 1, /* local_id is correctly initialized */ valid_csum_seen : 1, /* at least one csum validated */ is_mptfo : 1, /* subflow is doing TFO */ - __unused : 9; + __unused : 10;bool data_avail;bool scheduled;u32 remote_nonce;@@ -507,7 +506,7 @@ struct mptcp_subflow_context {u8 hmac[MPTCPOPT_HMAC_LEN ];/* MPJ subflow only */ u64 iasn;/* initial ack sequence number, MPC subflows only */ }; -u8 local_id; +s16 local_id;/* if negative not initialized yet */ u8 remote_id; u8 reset_seen:1; u8 reset_transient:1;@@ -558,6 +557,7 @@ mptcp_subflow_ctx_reset(struct mptcp_subflow_context *subflow) {memset (&subflow->reset,0 ,sizeof (subflow->reset)); subflow->request_mptcp =1 ; +WRITE_ONCE (subflow->local_id,-1 ); }static inline u64
net/mptcp/protocol.h int mptcp_pm_get_local_id(struct mptcp_sock *msk, struct sock_common *skc); · line 1067
Quoted 4 lines
net/mptcp/protocol.h +1
-0@@ -1064,6 +1064,15 @@ int mptcp_pm_get_local_id(struct mptcp_sock *msk, struct sock_common *skc);int mptcp_pm_nl_get_local_id (struct mptcp_sock *msk,struct mptcp_addr_info *skc);int mptcp_userspace_pm_get_local_id (struct mptcp_sock *msk,struct mptcp_addr_info *skc); +static inline int subflow_get_local_id (struct mptcp_subflow_context *subflow)
Hi Paolo -
Better to return a u8?
net/mptcp/protocol.h int mptcp_pm_get_local_id(struct mptcp_sock *msk, struct sock_common *skc); · line 1074
Quoted 7 lines
net/mptcp/protocol.h +7
-0+{ +int local_id =READ_ONCE (subflow->local_id); + +if (local_id <0 ) +return 0 ; +return local_id; +}
Looking around at code that still directly reads local_id from subflow contexts:
Should this also be used at line 236 in pm_userspace.c?
What about the places where local_id is passed in to nla_put_u8? (diag.c:68 and pm_netlink.c:1986?)
- Mat
Quoted 50 lines
net/mptcp/protocol.h +1
-0+void __init mptcp_pm_nl_init (void );void mptcp_pm_nl_work (struct mptcp_sock *msk); void mptcp_pm_nl_rm_subflow_received(struct mptcp_sock *msk,net/mptcp/subflow.c +5
-4@@ -578,8 +578,8 @@ static void subflow_finish_connect(struct sock *sk, const struct sk_buff *skb)static void subflow_set_local_id (struct mptcp_subflow_context *subflow,int local_id) { - subflow->local_id = local_id; - subflow->local_id_valid =1 ; +WARN_ON_ONCE (local_id <0 || local_id >255 ); +WRITE_ONCE (subflow->local_id, local_id); }static int subflow_chk_local_id (struct sock *sk)@@ -588,7 +588,7 @@ static int subflow_chk_local_id(struct sock *sk)struct mptcp_sock *msk =mptcp_sk (subflow->conn);int err; -if (likely (subflow->local_id_valid)) +if (likely (subflow->local_id >=0 ))return 0 ; err =mptcp_pm_get_local_id (msk, (struct sock_common *)sk);@@ -1733,6 +1733,7 @@ static struct mptcp_subflow_context *subflow_create_ctx(struct sock *sk,pr_debug ("subflow=%p" , ctx); ctx->tcp_sock = sk; +WRITE_ONCE (ctx->local_id,-1 );return ctx; }@@ -1968,7 +1969,7 @@ static void subflow_ulp_clone(const struct request_sock *req, new_ctx->idsn = subflow_req->idsn;/* this is the first subflow, id is always 0 */ - new_ctx->local_id_valid =1 ; +subflow_set_local_id (new_ctx,0 ); }else if (subflow_req->mp_join) { new_ctx->ssn_offset = subflow_req->ssn_offset; new_ctx->mp_join =1 ;Signature · 1 line
2.43.0
Mat Martineau
maintainerRe: [PATCH mptcp-net 3/4] mptcp: fix data races on remote_id
In reply to Paolo AbeniQuoted 24 lines · Paolo Abeni
Similar to the previous patch, address the data race on
remote_id, adding the suitable ONCE annotations.Fixes bedee0b56113 ("mptcp: address lookup improvements")Signed-off-by Paolo Abeni <[email protected]>---
net/mptcp/pm_netlink.c| 8 ++++----net/mptcp/subflow.c| 6 +++---2 files changed, 7 insertions(+), 7 deletions(-)net/mptcp/pm_netlink.c +1
-1@@ -443,7 +443,7 @@ static unsigned int fill_remote_addresses_vec(struct mptcp_sock *msk,mptcp_for_each_subflow (msk, subflow) { ssk =mptcp_subflow_tcp_sock (subflow);remote_address ((struct sock_common *)ssk, &addrs[i]); - addrs[i].id = subflow->remote_id; + addrs[i].id =READ_ONCE (subflow->remote_id);if (deny_id0 && !addrs[i].id)continue ;
net/mptcp/pm_netlink.c static void mptcp_pm_nl_rm_addr_or_subflow(struct mptcp_sock *msk, · line 803
Quoted 4 lines
net/mptcp/pm_netlink.c +1
-0@@ -800,17 +800,17 @@ static void mptcp_pm_nl_rm_addr_or_subflow(struct mptcp_sock *msk,mptcp_for_each_subflow_safe (msk, subflow, tmp) {struct sock *ssk =mptcp_subflow_tcp_sock (subflow);int how =RCV_SHUTDOWN |SEND_SHUTDOWN ; +u8 srm_id =READ_ONCE (subflow->remote_id);
Paolo -
"srm_id" reads oddly in statements like "srm_id != rm_id" since it creates some confusion between "remote" and "remove". Please rename to make the "remote" more obvious!
- Mat
Quoted 52 lines
net/mptcp/pm_netlink.c +2
-3u8 id =subflow_get_local_id (subflow); -if (rm_type ==MPTCP_MIB_RMADDR && subflow->remote_id != rm_id) +if (rm_type ==MPTCP_MIB_RMADDR && srm_id != rm_id)continue ;if (rm_type ==MPTCP_MIB_RMSUBFLOW && !mptcp_local_id_match (msk, id, rm_id))continue ;pr_debug (" -> %s rm_list_ids[%d]=%u local_id=%u remote_id=%u mpc_id=%u" , rm_type ==MPTCP_MIB_RMADDR ?"address" :"subflow" , - i, rm_id, id, subflow->remote_id, - msk->mpc_endpoint_id); + i, rm_id, id, srm_id, msk->mpc_endpoint_id);spin_unlock_bh (&msk->pm.lock);mptcp_subflow_shutdown (sk, ssk, how);net/mptcp/subflow.c +3
-3@@ -536,7 +536,7 @@ static void subflow_finish_connect(struct sock *sk, const struct sk_buff *skb) subflow->backup = mp_opt.backup; subflow->thmac = mp_opt.thmac; subflow->remote_nonce = mp_opt.nonce; - subflow->remote_id = mp_opt.join_id; +WRITE_ONCE (subflow->remote_id, mp_opt.join_id);pr_debug ("subflow=%p, thmac=%llu, remote_nonce=%u backup=%d" , subflow, subflow->thmac, subflow->remote_nonce, subflow->backup);@@ -1569,7 +1569,7 @@ int __mptcp_subflow_connect(struct sock *sk, const struct mptcp_addr_info *loc,pr_debug ("msk=%p remote_token=%u local_id=%d remote_id=%d" , msk, remote_token, local_id, remote_id); subflow->remote_token = remote_token; - subflow->remote_id = remote_id; +WRITE_ONCE (subflow->remote_id, remote_id); subflow->request_join =1 ; subflow->request_bkup = !!(flags &MPTCP_PM_ADDR_FLAG_BACKUP ); subflow->subflow_id = msk->subflow_id++;@@ -1976,7 +1976,7 @@ static void subflow_ulp_clone(const struct request_sock *req, new_ctx->fully_established =1 ; new_ctx->remote_key_valid =1 ; new_ctx->backup = subflow_req->backup; - new_ctx->remote_id = subflow_req->remote_id; +WRITE_ONCE (new_ctx->remote_id, subflow_req->remote_id); new_ctx->token = subflow_req->token; new_ctx->thmac = subflow_req->thmac;Signature · 1 line
2.43.0
Mat Martineau
maintainerQuoted 51 lines · Paolo Abeni
Fullmesh endpoints could end-up unexpectedly generating duplicate subflows - same local and remote addresses - when multiple incoming
ADD_ADDRare processed before the PM creates the subflow for the local endpoints.Address the issue explicitly checking for duplicates at subflow creation time.
To avoid a quadratic computational complexity, track the unavailable remote address ids in a temporary bitmap and initialize such bitmap with the remote ids of all the existing subflows matching the local address currently processed.
The above allows additionally replacing the existing code checking for duplicate entry in the current set with a simple bit test operation.
Fixes 2843ff6f36db ("mptcp: remote addresses fullmesh")Signed-off-by Paolo Abeni <[email protected]>---
Note that there is no problem for the opposite event sequence.
---net/mptcp/pm_netlink.c| 33 ++++++++++++++++++---------------1 file changed, 18 insertions(+), 15 deletions(-)net/mptcp/pm_netlink.c +0
-13@@ -396,19 +396,6 @@ void mptcp_pm_free_anno_list(struct mptcp_sock *msk) } } -static bool lookup_address_in_vec (const struct mptcp_addr_info *addrs,unsigned int nr, -const struct mptcp_addr_info *addr) -{ -int i; - -for (i =0 ; i < nr; i++) { -if (addrs[i].id == addr->id) -return true; - } - -return false; -} -/* Fill all the remote addresses into the array addrs[], * and return the array size. */
net/mptcp/pm_netlink.c static unsigned int fill_remote_addresses_vec(struct mptcp_sock *msk, · line 432
Quoted 6 lines
net/mptcp/pm_netlink.c +3
-0@@ -440,6 +427,16 @@ static unsigned int fill_remote_addresses_vec(struct mptcp_sock *msk, msk->pm.subflows++; addrs[i++] = remote; }else { +DECLARE_BITMAP (unavail_id,MPTCP_PM_MAX_ADDR_ID +1 ); + +/* Forbit creation of new subflows matching existing
Looks ok code-wise, but s/forbit/forbid/ here and...
Quoted 14 lines
net/mptcp/pm_netlink.c +7
-0+* ones, possibly already created by incoming ADD_ADDR +*/ +bitmap_zero (unavail_id,MPTCP_PM_MAX_ADDR_ID +1 ); +mptcp_for_each_subflow (msk, subflow) +if (READ_ONCE (subflow->local_id) == local->id) +__set_bit (subflow->remote_id, unavail_id); +mptcp_for_each_subflow (msk, subflow) { ssk =mptcp_subflow_tcp_sock (subflow);remote_address ((struct sock_common *)ssk, &addrs[i]);@@ -447,11 +444,17 @@ static unsigned int fill_remote_addresses_vec(struct mptcp_sock *msk,if (deny_id0 && !addrs[i].id)continue ;
net/mptcp/pm_netlink.c static unsigned int fill_remote_addresses_vec(struct mptcp_sock *msk, · line 454
Quoted 10 lines
net/mptcp/pm_netlink.c +5
-2+if (test_bit (addrs[i].id, unavail_id)) +continue ; +if (!mptcp_pm_addr_families_match (sk, local, &addrs[i]))continue ; -if (!lookup_address_in_vec (addrs, i, &addrs[i]) && - msk->pm.subflows < subflows_max) { +if (msk->pm.subflows < subflows_max) { +/* forbit creating multiple address towards
...here too. Or "Prevent" works too :)
- Mat
Quoted 11 lines
net/mptcp/pm_netlink.c +3
-0+* this id +*/ +__set_bit (addrs[i].id, unavail_id); msk->pm.subflows++; i++; }Signature · 1 line
2.43.0