Skip to content

Commit fa9e574

Browse files
Paolo Abenikuba-moo
authored andcommitted
mptcp: fix abba deadlock on fastopen
Our CI reported lockdep splat in the fastopen code: ====================================================== WARNING: possible circular locking dependency detected 6.0.0.mptcp_f5e8bfe9878d+ #1558 Not tainted ------------------------------------------------------ packetdrill/1071 is trying to acquire lock: ffff8881bd198140 (sk_lock-AF_INET){+.+.}-{0:0}, at: inet_wait_for_connect+0x19c/0x310 but task is already holding lock: ffff8881b8346540 (k-sk_lock-AF_INET){+.+.}-{0:0}, at: mptcp_sendmsg+0xfdf/0x1740 which lock already depends on the new lock. the existing dependency chain (in reverse order) is: -> #1 (k-sk_lock-AF_INET){+.+.}-{0:0}: __lock_acquire+0xb6d/0x1860 lock_acquire+0x1d8/0x620 lock_sock_nested+0x37/0xd0 inet_stream_connect+0x3f/0xa0 mptcp_connect+0x411/0x800 __inet_stream_connect+0x3ab/0x800 mptcp_stream_connect+0xac/0x110 __sys_connect+0x101/0x130 __x64_sys_connect+0x6e/0xb0 do_syscall_64+0x59/0x90 entry_SYSCALL_64_after_hwframe+0x63/0xcd -> #0 (sk_lock-AF_INET){+.+.}-{0:0}: check_prev_add+0x15e/0x2110 validate_chain+0xace/0xdf0 __lock_acquire+0xb6d/0x1860 lock_acquire+0x1d8/0x620 lock_sock_nested+0x37/0xd0 inet_wait_for_connect+0x19c/0x310 __inet_stream_connect+0x26c/0x800 tcp_sendmsg_fastopen+0x341/0x650 mptcp_sendmsg+0x109d/0x1740 sock_sendmsg+0xe1/0x120 __sys_sendto+0x1c7/0x2a0 __x64_sys_sendto+0xdc/0x1b0 do_syscall_64+0x59/0x90 entry_SYSCALL_64_after_hwframe+0x63/0xcd other info that might help us debug this: Possible unsafe locking scenario: CPU0 CPU1 ---- ---- lock(k-sk_lock-AF_INET); lock(sk_lock-AF_INET); lock(k-sk_lock-AF_INET); lock(sk_lock-AF_INET); *** DEADLOCK *** 1 lock held by packetdrill/1071: #0: ffff8881b8346540 (k-sk_lock-AF_INET){+.+.}-{0:0}, at: mptcp_sendmsg+0xfdf/0x1740 ====================================================== The problem is caused by the blocking inet_wait_for_connect() releasing and re-acquiring the msk socket lock while the subflow socket lock is still held and the MPTCP socket requires that the msk socket lock must be acquired before the subflow socket lock. Address the issue always invoking tcp_sendmsg_fastopen() in an unblocking manner, and later eventually complete the blocking __inet_stream_connect() as needed. Fixes: d98a82a ("mptcp: handle defer connect in mptcp_sendmsg") Reviewed-by: Mat Martineau <[email protected]> Reviewed-by: Matthieu Baerts <[email protected]> Signed-off-by: Paolo Abeni <[email protected]> Signed-off-by: Mat Martineau <[email protected]> Signed-off-by: Jakub Kicinski <[email protected]>
1 parent 54f1944 commit fa9e574

File tree

1 file changed

+34
-15
lines changed

1 file changed

+34
-15
lines changed

net/mptcp/protocol.c

Lines changed: 34 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1673,6 +1673,37 @@ static void mptcp_set_nospace(struct sock *sk)
16731673
set_bit(MPTCP_NOSPACE, &mptcp_sk(sk)->flags);
16741674
}
16751675

1676+
static int mptcp_sendmsg_fastopen(struct sock *sk, struct sock *ssk, struct msghdr *msg,
1677+
size_t len, int *copied_syn)
1678+
{
1679+
unsigned int saved_flags = msg->msg_flags;
1680+
struct mptcp_sock *msk = mptcp_sk(sk);
1681+
int ret;
1682+
1683+
lock_sock(ssk);
1684+
msg->msg_flags |= MSG_DONTWAIT;
1685+
msk->connect_flags = O_NONBLOCK;
1686+
msk->is_sendmsg = 1;
1687+
ret = tcp_sendmsg_fastopen(ssk, msg, copied_syn, len, NULL);
1688+
msk->is_sendmsg = 0;
1689+
msg->msg_flags = saved_flags;
1690+
release_sock(ssk);
1691+
1692+
/* do the blocking bits of inet_stream_connect outside the ssk socket lock */
1693+
if (ret == -EINPROGRESS && !(msg->msg_flags & MSG_DONTWAIT)) {
1694+
ret = __inet_stream_connect(sk->sk_socket, msg->msg_name,
1695+
msg->msg_namelen, msg->msg_flags, 1);
1696+
1697+
/* Keep the same behaviour of plain TCP: zero the copied bytes in
1698+
* case of any error, except timeout or signal
1699+
*/
1700+
if (ret && ret != -EINPROGRESS && ret != -ERESTARTSYS && ret != -EINTR)
1701+
*copied_syn = 0;
1702+
}
1703+
1704+
return ret;
1705+
}
1706+
16761707
static int mptcp_sendmsg(struct sock *sk, struct msghdr *msg, size_t len)
16771708
{
16781709
struct mptcp_sock *msk = mptcp_sk(sk);
@@ -1693,26 +1724,14 @@ static int mptcp_sendmsg(struct sock *sk, struct msghdr *msg, size_t len)
16931724

16941725
ssock = __mptcp_nmpc_socket(msk);
16951726
if (unlikely(ssock && inet_sk(ssock->sk)->defer_connect)) {
1696-
struct sock *ssk = ssock->sk;
16971727
int copied_syn = 0;
16981728

1699-
lock_sock(ssk);
1700-
1701-
msk->connect_flags = (msg->msg_flags & MSG_DONTWAIT) ? O_NONBLOCK : 0;
1702-
msk->is_sendmsg = 1;
1703-
ret = tcp_sendmsg_fastopen(ssk, msg, &copied_syn, len, NULL);
1704-
msk->is_sendmsg = 0;
1729+
ret = mptcp_sendmsg_fastopen(sk, ssock->sk, msg, len, &copied_syn);
17051730
copied += copied_syn;
1706-
if (ret == -EINPROGRESS && copied_syn > 0) {
1707-
/* reflect the new state on the MPTCP socket */
1708-
inet_sk_state_store(sk, inet_sk_state_load(ssk));
1709-
release_sock(ssk);
1731+
if (ret == -EINPROGRESS && copied_syn > 0)
17101732
goto out;
1711-
} else if (ret) {
1712-
release_sock(ssk);
1733+
else if (ret)
17131734
goto do_error;
1714-
}
1715-
release_sock(ssk);
17161735
}
17171736

17181737
timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);

0 commit comments

Comments
 (0)