From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 3D3AF268685; Tue, 8 Apr 2025 11:03:08 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1744110188; cv=none; b=T0YSI0gaimWu1KhAOp3MiteweA8XY/9DWCcjVGFa0ha0MFSMVg0E8DrvbsdfPHsyiqfsRugGXFJV0Sa0sYNst2OJ0SFq2qs9K0KHdx4mEuJGlRzaRxKhTzTiTwYjTcT9sGwmMUDTPOVdrC2LjdQBnRYih5qCt5i21cN3s7c9xH8= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1744110188; c=relaxed/simple; bh=P0cZ1pGOFBOh5iW7vTxzgk6OYs+hndCGiJKta8iXwMs=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Vtxb2cbjLN6OyE/mUuuH5YyOP6ahd0SHc1CbjZncVUKiG6Euae9ghH6QAZ0cVq0hdP/JQcYT0k6EVHR6H3Q+j7hTcvQPNG2O6iMu+mXDjKSu507CVwhPyHwERvwrjjT1WStmQd72m4Sw/4/qzk5NoUZh7dXGE09VbkPcfWs6M2A= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.b=DFDwGfrl; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.b="DFDwGfrl" Received: by smtp.kernel.org (Postfix) with ESMTPSA id C2375C4CEE5; Tue, 8 Apr 2025 11:03:07 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1744110188; bh=P0cZ1pGOFBOh5iW7vTxzgk6OYs+hndCGiJKta8iXwMs=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=DFDwGfrllakAyxZhZipuLkepQE0Xbtj2DGl/s/t/SkH9Gy+I/OHfIVLkASY/O9Ilg W3YHigy7wcKU4Q/vzCB5SGP5Tfsk2MbzQiSnrlmWHSV5CLIc5IToeic4xxUaG3auiu bZRSkzZ0zhQtwv2Ys3f3q9q+53eUZ3nz3wvFzdXI= From: Greg Kroah-Hartman To: stable@vger.kernel.org Cc: Greg Kroah-Hartman , patches@lists.linux.dev, Luigi Leonardi , Stefano Garzarella , Paolo Abeni , Jakub Kicinski , Sasha Levin Subject: [PATCH 5.10 199/227] vsock: avoid timeout during connect() if the socket is closing Date: Tue, 8 Apr 2025 12:49:37 +0200 Message-ID: <20250408104826.271273950@linuxfoundation.org> X-Mailer: git-send-email 2.49.0 In-Reply-To: <20250408104820.353768086@linuxfoundation.org> References: <20250408104820.353768086@linuxfoundation.org> User-Agent: quilt/0.68 X-stable: review X-Patchwork-Hint: ignore Precedence: bulk X-Mailing-List: patches@lists.linux.dev List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 5.10-stable review patch. If anyone has any objections, please let me know. ------------------ From: Stefano Garzarella [ Upstream commit fccd2b711d9628c7ce0111d5e4938652101ee30a ] When a peer attempts to establish a connection, vsock_connect() contains a loop that waits for the state to be TCP_ESTABLISHED. However, the other peer can be fast enough to accept the connection and close it immediately, thus moving the state to TCP_CLOSING. When this happens, the peer in the vsock_connect() is properly woken up, but since the state is not TCP_ESTABLISHED, it goes back to sleep until the timeout expires, returning -ETIMEDOUT. If the socket state is TCP_CLOSING, waiting for the timeout is pointless. vsock_connect() can return immediately without errors or delay since the connection actually happened. The socket will be in a closing state, but this is not an issue, and subsequent calls will fail as expected. We discovered this issue while developing a test that accepts and immediately closes connections to stress the transport switch between two connect() calls, where the first one was interrupted by a signal (see Closes link). Reported-by: Luigi Leonardi Closes: https://lore.kernel.org/virtualization/bq6hxrolno2vmtqwcvb5bljfpb7mvwb3kohrvaed6auz5vxrfv@ijmd2f3grobn/ Fixes: d021c344051a ("VSOCK: Introduce VM Sockets") Signed-off-by: Stefano Garzarella Acked-by: Paolo Abeni Tested-by: Luigi Leonardi Reviewed-by: Luigi Leonardi Link: https://patch.msgid.link/20250328141528.420719-1-sgarzare@redhat.com Signed-off-by: Jakub Kicinski Signed-off-by: Sasha Levin --- net/vmw_vsock/af_vsock.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/net/vmw_vsock/af_vsock.c b/net/vmw_vsock/af_vsock.c index d7395601a0e30..fc0306ba2d43e 100644 --- a/net/vmw_vsock/af_vsock.c +++ b/net/vmw_vsock/af_vsock.c @@ -1382,7 +1382,11 @@ static int vsock_stream_connect(struct socket *sock, struct sockaddr *addr, timeout = vsk->connect_timeout; prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE); - while (sk->sk_state != TCP_ESTABLISHED && sk->sk_err == 0) { + /* If the socket is already closing or it is in an error state, there + * is no point in waiting. + */ + while (sk->sk_state != TCP_ESTABLISHED && + sk->sk_state != TCP_CLOSING && sk->sk_err == 0) { if (flags & O_NONBLOCK) { /* If we're not going to block, we schedule a timeout * function to generate a timeout on the connection -- 2.39.5