Change the behaviour of a lingering close(): instead of waiting for all
data to be consumed, block until data is considered sent, i.e. until worker
picks the packets and decrements virtio_vsock_sock::bytes_unsent down to 0.

Do linger on shutdown() just as well.

Signed-off-by: Michal Luczaj <m...@rbox.co>
---
 include/net/af_vsock.h                  |  1 +
 net/vmw_vsock/af_vsock.c                | 25 +++++++++++++++++++++++++
 net/vmw_vsock/virtio_transport_common.c | 25 +++----------------------
 3 files changed, 29 insertions(+), 22 deletions(-)

diff --git a/include/net/af_vsock.h b/include/net/af_vsock.h
index 
9e85424c834353d016a527070dd62e15ff3bfce1..bd8b88d70423051dd05fc445fe37971af631ba03
 100644
--- a/include/net/af_vsock.h
+++ b/include/net/af_vsock.h
@@ -221,6 +221,7 @@ void vsock_for_each_connected_socket(struct vsock_transport 
*transport,
                                     void (*fn)(struct sock *sk));
 int vsock_assign_transport(struct vsock_sock *vsk, struct vsock_sock *psk);
 bool vsock_find_cid(unsigned int cid);
+void vsock_linger(struct sock *sk, long timeout);
 
 /**** TAP ****/
 
diff --git a/net/vmw_vsock/af_vsock.c b/net/vmw_vsock/af_vsock.c
index 
fc6afbc8d6806a4d98c66abc3af4bd139c583b08..383c6644d047589035c0439c47d1440273e67ea9
 100644
--- a/net/vmw_vsock/af_vsock.c
+++ b/net/vmw_vsock/af_vsock.c
@@ -1013,6 +1013,29 @@ static int vsock_getname(struct socket *sock,
        return err;
 }
 
+void vsock_linger(struct sock *sk, long timeout)
+{
+       if (timeout) {
+               DEFINE_WAIT_FUNC(wait, woken_wake_function);
+               ssize_t (*unsent)(struct vsock_sock *vsk);
+               struct vsock_sock *vsk = vsock_sk(sk);
+
+               unsent = vsk->transport->unsent_bytes;
+               if (!unsent)
+                       return;
+
+               add_wait_queue(sk_sleep(sk), &wait);
+
+               do {
+                       if (sk_wait_event(sk, &timeout, unsent(vsk) == 0, 
&wait))
+                               break;
+               } while (!signal_pending(current) && timeout);
+
+               remove_wait_queue(sk_sleep(sk), &wait);
+       }
+}
+EXPORT_SYMBOL_GPL(vsock_linger);
+
 static int vsock_shutdown(struct socket *sock, int mode)
 {
        int err;
@@ -1056,6 +1079,8 @@ static int vsock_shutdown(struct socket *sock, int mode)
                if (sock_type_connectible(sk->sk_type)) {
                        sock_reset_flag(sk, SOCK_DONE);
                        vsock_send_shutdown(sk, mode);
+                       if (sock_flag(sk, SOCK_LINGER))
+                               vsock_linger(sk, sk->sk_lingertime);
                }
        }
 
diff --git a/net/vmw_vsock/virtio_transport_common.c 
b/net/vmw_vsock/virtio_transport_common.c
index 
7f7de6d8809655fe522749fbbc9025df71f071bd..66ff2e694e474ad16f70248cc1dc235f4e1ebaa1
 100644
--- a/net/vmw_vsock/virtio_transport_common.c
+++ b/net/vmw_vsock/virtio_transport_common.c
@@ -1192,23 +1192,6 @@ static void virtio_transport_remove_sock(struct 
vsock_sock *vsk)
        vsock_remove_sock(vsk);
 }
 
-static void virtio_transport_wait_close(struct sock *sk, long timeout)
-{
-       if (timeout) {
-               DEFINE_WAIT_FUNC(wait, woken_wake_function);
-
-               add_wait_queue(sk_sleep(sk), &wait);
-
-               do {
-                       if (sk_wait_event(sk, &timeout,
-                                         sock_flag(sk, SOCK_DONE), &wait))
-                               break;
-               } while (!signal_pending(current) && timeout);
-
-               remove_wait_queue(sk_sleep(sk), &wait);
-       }
-}
-
 static void virtio_transport_cancel_close_work(struct vsock_sock *vsk,
                                               bool cancel_timeout)
 {
@@ -1279,15 +1262,13 @@ static bool virtio_transport_close(struct vsock_sock 
*vsk)
                (void)virtio_transport_shutdown(vsk, SHUTDOWN_MASK);
 
        if (sock_flag(sk, SOCK_LINGER) && !(current->flags & PF_EXITING))
-               virtio_transport_wait_close(sk, sk->sk_lingertime);
+               vsock_linger(sk, sk->sk_lingertime);
 
-       if (sock_flag(sk, SOCK_DONE)) {
+       if (sock_flag(sk, SOCK_DONE))
                return true;
-       }
 
        sock_hold(sk);
-       INIT_DELAYED_WORK(&vsk->close_work,
-                         virtio_transport_close_timeout);
+       INIT_DELAYED_WORK(&vsk->close_work, virtio_transport_close_timeout);
        vsk->close_work_scheduled = true;
        schedule_delayed_work(&vsk->close_work, VSOCK_CLOSE_TIMEOUT);
        return false;

-- 
2.49.0


Reply via email to