Lingering should be transport-independent in the long run. In preparation
for supporting other transports, as well as the linger on shutdown(), move
code to core.
Generalize by querying vsock_transport::unsent_bytes(), guard against the
callback being unimplemented. Do not pass sk_lingertime explicitly. Pull
SOCK_LINGER check into vsock_linger().
Flatten the function. Remove the nested block by inverting the condition:
return early on !timeout.
Suggested-by: Stefano Garzarella <sgarzare@redhat.com>
Reviewed-by: Stefano Garzarella <sgarzare@redhat.com>
Signed-off-by: Michal Luczaj <mhal@rbox.co>
Link: https://patch.msgid.link/20250522-vsock-linger-v6-2-2ad00b0e447e@rbox.co
Signed-off-by: Paolo Abeni <pabeni@redhat.com>
 
                                     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);
 
 /**** TAP ****/
 
 
        return err;
 }
 
+void vsock_linger(struct sock *sk)
+{
+       DEFINE_WAIT_FUNC(wait, woken_wake_function);
+       ssize_t (*unsent)(struct vsock_sock *vsk);
+       struct vsock_sock *vsk = vsock_sk(sk);
+       long timeout;
+
+       if (!sock_flag(sk, SOCK_LINGER))
+               return;
+
+       timeout = sk->sk_lingertime;
+       if (!timeout)
+               return;
+
+       /* Transports must implement `unsent_bytes` if they want to support
+        * SOCK_LINGER through `vsock_linger()` since we use it to check when
+        * the socket can be closed.
+        */
+       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;
 
        vsock_remove_sock(vsk);
 }
 
-static void virtio_transport_wait_close(struct sock *sk, long timeout)
-{
-       if (timeout) {
-               DEFINE_WAIT_FUNC(wait, woken_wake_function);
-               struct vsock_sock *vsk = vsock_sk(sk);
-
-               add_wait_queue(sk_sleep(sk), &wait);
-
-               do {
-                       if (sk_wait_event(sk, &timeout,
-                                         virtio_transport_unsent_bytes(vsk) == 0,
-                                         &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)
 {
        if ((sk->sk_shutdown & SHUTDOWN_MASK) != SHUTDOWN_MASK)
                (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);
+       if (!(current->flags & PF_EXITING))
+               vsock_linger(sk);
 
        if (sock_flag(sk, SOCK_DONE)) {
                return true;