* [PATCH net] vsock: avoid timeout during connect() if the socket is closing
@ 2025-03-28 14:15 Stefano Garzarella
2025-04-01 11:56 ` Paolo Abeni
` (2 more replies)
0 siblings, 3 replies; 4+ messages in thread
From: Stefano Garzarella @ 2025-03-28 14:15 UTC (permalink / raw)
To: netdev
Cc: Michal Luczaj, Paolo Abeni, George Zhang, virtualization,
linux-kernel, Andy King, Stefano Garzarella, Eric Dumazet,
David S. Miller, Jakub Kicinski, Dmitry Torokhov, Simon Horman,
Luigi Leonardi
From: Stefano Garzarella <sgarzare@redhat.com>
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 <leonardi@redhat.com>
Closes: https://lore.kernel.org/virtualization/bq6hxrolno2vmtqwcvb5bljfpb7mvwb3kohrvaed6auz5vxrfv@ijmd2f3grobn/
Fixes: d021c344051a ("VSOCK: Introduce VM Sockets")
Signed-off-by: Stefano Garzarella <sgarzare@redhat.com>
---
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 7e3db87ae433..fc6afbc8d680 100644
--- a/net/vmw_vsock/af_vsock.c
+++ b/net/vmw_vsock/af_vsock.c
@@ -1551,7 +1551,11 @@ static int vsock_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.49.0
^ permalink raw reply [flat|nested] 4+ messages in thread* Re: [PATCH net] vsock: avoid timeout during connect() if the socket is closing
2025-03-28 14:15 [PATCH net] vsock: avoid timeout during connect() if the socket is closing Stefano Garzarella
@ 2025-04-01 11:56 ` Paolo Abeni
2025-04-01 12:27 ` Luigi Leonardi
2025-04-03 0:30 ` patchwork-bot+netdevbpf
2 siblings, 0 replies; 4+ messages in thread
From: Paolo Abeni @ 2025-04-01 11:56 UTC (permalink / raw)
To: Stefano Garzarella, netdev
Cc: Michal Luczaj, George Zhang, virtualization, linux-kernel,
Andy King, Eric Dumazet, David S. Miller, Jakub Kicinski,
Dmitry Torokhov, Simon Horman, Luigi Leonardi
On 3/28/25 3:15 PM, Stefano Garzarella wrote:
> From: Stefano Garzarella <sgarzare@redhat.com>
>
> 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 <leonardi@redhat.com>
> Closes: https://lore.kernel.org/virtualization/bq6hxrolno2vmtqwcvb5bljfpb7mvwb3kohrvaed6auz5vxrfv@ijmd2f3grobn/
> Fixes: d021c344051a ("VSOCK: Introduce VM Sockets")
> Signed-off-by: Stefano Garzarella <sgarzare@redhat.com>
Acked-by: Paolo Abeni <pabeni@redhat.com>
^ permalink raw reply [flat|nested] 4+ messages in thread* Re: [PATCH net] vsock: avoid timeout during connect() if the socket is closing
2025-03-28 14:15 [PATCH net] vsock: avoid timeout during connect() if the socket is closing Stefano Garzarella
2025-04-01 11:56 ` Paolo Abeni
@ 2025-04-01 12:27 ` Luigi Leonardi
2025-04-03 0:30 ` patchwork-bot+netdevbpf
2 siblings, 0 replies; 4+ messages in thread
From: Luigi Leonardi @ 2025-04-01 12:27 UTC (permalink / raw)
To: Stefano Garzarella
Cc: netdev, Michal Luczaj, Paolo Abeni, George Zhang, virtualization,
linux-kernel, Andy King, Eric Dumazet, David S. Miller,
Jakub Kicinski, Dmitry Torokhov, Simon Horman
On Fri, Mar 28, 2025 at 03:15:28PM +0100, Stefano Garzarella wrote:
>From: Stefano Garzarella <sgarzare@redhat.com>
>
>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 <leonardi@redhat.com>
>Closes: https://lore.kernel.org/virtualization/bq6hxrolno2vmtqwcvb5bljfpb7mvwb3kohrvaed6auz5vxrfv@ijmd2f3grobn/
>Fixes: d021c344051a ("VSOCK: Introduce VM Sockets")
>Signed-off-by: Stefano Garzarella <sgarzare@redhat.com>
>---
> 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 7e3db87ae433..fc6afbc8d680 100644
>--- a/net/vmw_vsock/af_vsock.c
>+++ b/net/vmw_vsock/af_vsock.c
>@@ -1551,7 +1551,11 @@ static int vsock_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.49.0
>
Just tested and fixes the issue! Thanks Stefano!
Tested-by: Luigi Leonardi <leonardi@redhat.com>
Reviewed-by: Luigi Leonardi <leonardi@redhat.com>
^ permalink raw reply [flat|nested] 4+ messages in thread* Re: [PATCH net] vsock: avoid timeout during connect() if the socket is closing
2025-03-28 14:15 [PATCH net] vsock: avoid timeout during connect() if the socket is closing Stefano Garzarella
2025-04-01 11:56 ` Paolo Abeni
2025-04-01 12:27 ` Luigi Leonardi
@ 2025-04-03 0:30 ` patchwork-bot+netdevbpf
2 siblings, 0 replies; 4+ messages in thread
From: patchwork-bot+netdevbpf @ 2025-04-03 0:30 UTC (permalink / raw)
To: Stefano Garzarella
Cc: netdev, mhal, pabeni, georgezhang, virtualization, linux-kernel,
acking, edumazet, davem, kuba, dtor, horms, leonardi
Hello:
This patch was applied to netdev/net.git (main)
by Jakub Kicinski <kuba@kernel.org>:
On Fri, 28 Mar 2025 15:15:28 +0100 you wrote:
> From: Stefano Garzarella <sgarzare@redhat.com>
>
> 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.
>
> [...]
Here is the summary with links:
- [net] vsock: avoid timeout during connect() if the socket is closing
https://git.kernel.org/netdev/net/c/fccd2b711d96
You are awesome, thank you!
--
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2025-04-03 0:29 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2025-03-28 14:15 [PATCH net] vsock: avoid timeout during connect() if the socket is closing Stefano Garzarella
2025-04-01 11:56 ` Paolo Abeni
2025-04-01 12:27 ` Luigi Leonardi
2025-04-03 0:30 ` patchwork-bot+netdevbpf
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®