Skip to content

Commit 3fe356d

Browse files
stefano-garzarellakuba-moo
authored andcommitted
vsock/virtio: discard packets only when socket is really closed
Starting from commit 8692cef ("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: 8692cef ("virtio_vsock: Fix race condition in virtio_transport_recv_pkt") Cc: [email protected] Reported-by: Sergio Lopez <[email protected]> Tested-by: Sergio Lopez <[email protected]> Signed-off-by: Stefano Garzarella <[email protected]> Acked-by: Jia He <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Jakub Kicinski <[email protected]>
1 parent 01770a1 commit 3fe356d

File tree

1 file changed

+5
-3
lines changed

1 file changed

+5
-3
lines changed

net/vmw_vsock/virtio_transport_common.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -841,8 +841,10 @@ void virtio_transport_release(struct vsock_sock *vsk)
841841
virtio_transport_free_pkt(pkt);
842842
}
843843

844-
if (remove_sock)
844+
if (remove_sock) {
845+
sock_set_flag(sk, SOCK_DONE);
845846
vsock_remove_sock(vsk);
847+
}
846848
}
847849
EXPORT_SYMBOL_GPL(virtio_transport_release);
848850

@@ -1132,8 +1134,8 @@ void virtio_transport_recv_pkt(struct virtio_transport *t,
11321134

11331135
lock_sock(sk);
11341136

1135-
/* Check if sk has been released before lock_sock */
1136-
if (sk->sk_shutdown == SHUTDOWN_MASK) {
1137+
/* Check if sk has been closed before lock_sock */
1138+
if (sock_flag(sk, SOCK_DONE)) {
11371139
(void)virtio_transport_reset_no_sock(t, pkt);
11381140
release_sock(sk);
11391141
sock_put(sk);

0 commit comments

Comments
 (0)