Skip to content

Commit 5b82572

Browse files
Paolo Abenikuba-moo
authored andcommitted
mptcp: add annotations around msk->subflow accesses
The MPTCP can access the first subflow socket in a few spots outside the socket lock scope. That is actually safe, as MPTCP will delete the socket itself only after the msk sock close(). Still the such accesses causes a few KCSAN splats, as reported by Christoph. Silence the harmless warning adding a few annotation around the relevant accesses. Fixes: 71ba088 ("mptcp: cleanup accept and poll") Reported-by: Christoph Paasch <cpaasch@apple.com> Closes: multipath-tcp/mptcp_net-next#402 Reviewed-by: Mat Martineau <martineau@kernel.org> Signed-off-by: Paolo Abeni <pabeni@redhat.com> Signed-off-by: Mat Martineau <martineau@kernel.org> Signed-off-by: Jakub Kicinski <kuba@kernel.org>
1 parent 786fc12 commit 5b82572

2 files changed

Lines changed: 15 additions & 9 deletions

File tree

net/mptcp/protocol.c

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ static int __mptcp_socket_create(struct mptcp_sock *msk)
9191
return err;
9292

9393
msk->first = ssock->sk;
94-
msk->subflow = ssock;
94+
WRITE_ONCE(msk->subflow, ssock);
9595
subflow = mptcp_subflow_ctx(ssock->sk);
9696
list_add(&subflow->node, &msk->conn_list);
9797
sock_hold(ssock->sk);
@@ -2282,7 +2282,7 @@ static void mptcp_dispose_initial_subflow(struct mptcp_sock *msk)
22822282
{
22832283
if (msk->subflow) {
22842284
iput(SOCK_INODE(msk->subflow));
2285-
msk->subflow = NULL;
2285+
WRITE_ONCE(msk->subflow, NULL);
22862286
}
22872287
}
22882288

@@ -3136,7 +3136,7 @@ struct sock *mptcp_sk_clone(const struct sock *sk,
31363136
msk = mptcp_sk(nsk);
31373137
msk->local_key = subflow_req->local_key;
31383138
msk->token = subflow_req->token;
3139-
msk->subflow = NULL;
3139+
WRITE_ONCE(msk->subflow, NULL);
31403140
msk->in_accept_queue = 1;
31413141
WRITE_ONCE(msk->fully_established, false);
31423142
if (mp_opt->suboptions & OPTION_MPTCP_CSUMREQD)
@@ -3184,7 +3184,7 @@ static struct sock *mptcp_accept(struct sock *sk, int flags, int *err,
31843184
struct socket *listener;
31853185
struct sock *newsk;
31863186

3187-
listener = msk->subflow;
3187+
listener = READ_ONCE(msk->subflow);
31883188
if (WARN_ON_ONCE(!listener)) {
31893189
*err = -EINVAL;
31903190
return NULL;
@@ -3736,10 +3736,10 @@ static int mptcp_stream_accept(struct socket *sock, struct socket *newsock,
37363736

37373737
pr_debug("msk=%p", msk);
37383738

3739-
/* buggy applications can call accept on socket states other then LISTEN
3739+
/* Buggy applications can call accept on socket states other then LISTEN
37403740
* but no need to allocate the first subflow just to error out.
37413741
*/
3742-
ssock = msk->subflow;
3742+
ssock = READ_ONCE(msk->subflow);
37433743
if (!ssock)
37443744
return -EINVAL;
37453745

@@ -3813,10 +3813,12 @@ static __poll_t mptcp_poll(struct file *file, struct socket *sock,
38133813
state = inet_sk_state_load(sk);
38143814
pr_debug("msk=%p state=%d flags=%lx", msk, state, msk->flags);
38153815
if (state == TCP_LISTEN) {
3816-
if (WARN_ON_ONCE(!msk->subflow || !msk->subflow->sk))
3816+
struct socket *ssock = READ_ONCE(msk->subflow);
3817+
3818+
if (WARN_ON_ONCE(!ssock || !ssock->sk))
38173819
return 0;
38183820

3819-
return inet_csk_listen_poll(msk->subflow->sk);
3821+
return inet_csk_listen_poll(ssock->sk);
38203822
}
38213823

38223824
if (state != TCP_SYN_SENT && state != TCP_SYN_RECV) {

net/mptcp/protocol.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,11 @@ struct mptcp_sock {
305305
struct list_head rtx_queue;
306306
struct mptcp_data_frag *first_pending;
307307
struct list_head join_list;
308-
struct socket *subflow; /* outgoing connect/listener/!mp_capable */
308+
struct socket *subflow; /* outgoing connect/listener/!mp_capable
309+
* The mptcp ops can safely dereference, using suitable
310+
* ONCE annotation, the subflow outside the socket
311+
* lock as such sock is freed after close().
312+
*/
309313
struct sock *first;
310314
struct mptcp_pm_data pm;
311315
struct {

0 commit comments

Comments
 (0)