Skip to contentSkip to conversation

vsock/virtio: discard packets only when socket is really closed

PATCH Applied Stefano Garzarella 3 participants 3 messages Nov 20, 2020 – Nov 24, 2020

Subsystems (6)
lore
Share & tools
lore

Stefano Garzarella

author maintainer
#0 lore raw

PATCH 1/1 vsock/virtio: discard packets only when socket is really closed

Starting from commit 8692cefc433f ("virtio_vsock: Fix race condition in virtio_transport_recv_pkt"), we discard packets in virtio_transport_recv_pkt() if the socket has been released.

When the socket is connected, we schedule a delayed work to wait the RST packet from the other peer, also if SHUTDOWN_MASK is set in sk->sk_shutdown.
This is done to complete the virtio-vsock shutdown algorithm, releasing the port assigned to the socket definitively only when the other peer has consumed all the packets.

If we discard the RST packet received, the socket will be closed only when the VSOCK_CLOSE_TIMEOUT is reached.

Sergio discovered the issue while running ab(1) HTTP benchmark using libkrun [1] and observing a latency increase with that commit.

To avoid this issue, we discard packet only if the socket is really closed (SOCK_DONE flag is set).
We also set SOCK_DONE in virtio_transport_release() when we don't need to wait any packets from the other peer (we didn't schedule the delayed work). In this case we remove the socket from the vsock lists, releasing the port assigned.

[1] https://github.com/containers/libkrun

Fixes 8692cefc433f ("virtio_vsock: Fix race condition in virtio_transport_recv_pkt")
---
 net/vmw_vsock/virtio_transport_common.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
net/vmw_vsock/virtio_transport_common.c +5 -3
index 0edda1edf988..5956939eebb7 100644
@@ -841,8 +841,10 @@ void virtio_transport_release(struct vsock_sock *vsk)
 		virtio_transport_free_pkt(pkt);
 	}
 
-	if (remove_sock)
+	if (remove_sock) {
+		sock_set_flag(sk, SOCK_DONE);
 		vsock_remove_sock(vsk);
+	}
 }
 EXPORT_SYMBOL_GPL(virtio_transport_release);
 
@@ -1132,8 +1134,8 @@ void virtio_transport_recv_pkt(struct virtio_transport *t,
 
 	lock_sock(sk);
 
-	/* Check if sk has been released before lock_sock */
-	if (sk->sk_shutdown == SHUTDOWN_MASK) {
+	/* Check if sk has been closed before lock_sock */
+	if (sock_flag(sk, SOCK_DONE)) {
 		(void)virtio_transport_reset_no_sock(t, pkt);
 		release_sock(sk);
 		sock_put(sk);
Signature · 6 lines
2.26.2

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
Acked-by on v1 1/1
#1 lore raw
Quoted 38 lines

-----Original Message-----
From: Stefano Garzarella <[email protected]>
Sent: Friday, November 20, 2020 6:48 PM
To: [email protected]
Cc: Sergio Lopez <[email protected]>; David S. Miller <[email protected]>; Stefano Garzarella <[email protected]>; Justin He <[email protected]>; [email protected]; [email protected]; Stefan Hajnoczi <[email protected]>; [email protected]; Jakub Kicinski <[email protected]>
Subject: [PATCH net] vsock/virtio: discard packets only when socket is really closed

Starting from commit 8692cefc433f ("virtio_vsock: Fix race condition in virtio_transport_recv_pkt"), we discard packets in virtio_transport_recv_pkt() if the socket has been released.

When the socket is connected, we schedule a delayed work to wait the RST packet from the other peer, also if SHUTDOWN_MASK is set in sk->sk_shutdown.
This is done to complete the virtio-vsock shutdown algorithm, releasing the port assigned to the socket definitively only when the other peer has consumed all the packets.

If we discard the RST packet received, the socket will be closed only when the VSOCK_CLOSE_TIMEOUT is reached.

Sergio discovered the issue while running ab(1) HTTP benchmark using libkrun [1] and observing a latency increase with that commit.

To avoid this issue, we discard packet only if the socket is really closed (SOCK_DONE flag is set).
We also set SOCK_DONE in virtio_transport_release() when we don't need to wait any packets from the other peer (we didn't schedule the delayed work). In this case we remove the socket from the vsock lists, releasing the port assigned.

Quoted 4 lines

[1] https://github.com/containers/libkrun

Fixes 8692cefc433f ("virtio_vsock: Fix race condition in

virtio_transport_recv_pkt")

--
Cheers,
Justin (Jia He)

Quoted 36 lines
---
 net/vmw_vsock/virtio_transport_common.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
net/vmw_vsock/virtio_transport_common.c +5 -3
index 0edda1edf988..5956939eebb7 100644
@@ -841,8 +841,10 @@ void virtio_transport_release(struct vsock_sock *vsk)
 virtio_transport_free_pkt(pkt);
 }
 
-if (remove_sock)
+if (remove_sock) {
+sock_set_flag(sk, SOCK_DONE);
 vsock_remove_sock(vsk);
+}
 }
 EXPORT_SYMBOL_GPL(virtio_transport_release);
 
@@ -1132,8 +1134,8 @@ void virtio_transport_recv_pkt(struct
 virtio_transport *t,
 
 lock_sock(sk);
 
-/* Check if sk has been released before lock_sock */
-if (sk->sk_shutdown == SHUTDOWN_MASK) {
+/* Check if sk has been closed before lock_sock */
+if (sock_flag(sk, SOCK_DONE)) {
 (void)virtio_transport_reset_no_sock(t, pkt);
 release_sock(sk);
Quoted 3 lines

sock_put(sk);
--
2.26.2

IMPORTANT NOTICE: The contents of this email and any attachments are confidential and may also be privileged. If you are not the intended recipient, please notify the sender immediately and do not disclose the contents to any other person, use it for any purpose, or store or copy the information in any medium. Thank you.

#2 lore raw
In reply to Stefano Garzarella
This patch was applied to netdev/net.git (refs/heads/master):

Hello:

This patch was applied to netdev/net.git (refs/heads/master):

Quoted 4 lines · On Fri, 20 Nov 2020 11:47:36 +0100 you wrote:

Starting from commit 8692cefc433f ("virtio_vsock: Fix race condition in virtio_transport_recv_pkt"), we discard packets in virtio_transport_recv_pkt() if the socket has been released.

Quoted 8 lines

When the socket is connected, we schedule a delayed work to wait the RST packet from the other peer, also if SHUTDOWN_MASK is set in sk->sk_shutdown.
This is done to complete the virtio-vsock shutdown algorithm, releasing the port assigned to the socket definitively only when the other peer has consumed all the packets.

[...]

Here is the summary with links:
- [net] vsock/virtio: discard packets only when socket is really closed

    https://git.kernel.org/netdev/net/c/3fe356d58efa

You are awesome, thank you!
--
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html