]> git.ipfire.org Git - thirdparty/linux.git/commitdiff
vsock: avoid timeout for non-blocking accept() with empty backlog
authorLaurence Rowe <laurencerowe@gmail.com>
Thu, 2 Apr 2026 20:49:18 +0000 (13:49 -0700)
committerJakub Kicinski <kuba@kernel.org>
Tue, 7 Apr 2026 01:29:01 +0000 (18:29 -0700)
A common pattern in epoll network servers is to eagerly accept all
pending connections from the non-blocking listening socket after
epoll_wait indicates the socket is ready by calling accept in a loop
until EAGAIN is returned indicating that the backlog is empty.

Scheduling a timeout for a non-blocking accept with an empty backlog
meant AF_VSOCK sockets used by epoll network servers incurred hundreds
of microseconds of additional latency per accept loop compared to
AF_INET or AF_UNIX sockets.

Signed-off-by: Laurence Rowe <laurencerowe@gmail.com>
Reviewed-by: Bobby Eshleman <bobbyeshleman@meta.com>
Reviewed-by: Stefano Garzarella <sgarzare@redhat.com>
Link: https://patch.msgid.link/20260402204918.130395-1-laurencerowe@gmail.com
Signed-off-by: Jakub Kicinski <kuba@kernel.org>
net/vmw_vsock/af_vsock.c

index e4756604d50b9ca2e2e138bfd40b6522430913df..b8794ea0f01e9aea58afa3667a03cdcba67b880b 100644 (file)
@@ -1864,10 +1864,10 @@ static int vsock_accept(struct socket *sock, struct socket *newsock,
         * created upon connection establishment.
         */
        timeout = sock_rcvtimeo(listener, arg->flags & O_NONBLOCK);
-       prepare_to_wait(sk_sleep(listener), &wait, TASK_INTERRUPTIBLE);
 
        while ((connected = vsock_dequeue_accept(listener)) == NULL &&
-              listener->sk_err == 0) {
+              listener->sk_err == 0 && timeout != 0) {
+               prepare_to_wait(sk_sleep(listener), &wait, TASK_INTERRUPTIBLE);
                release_sock(listener);
                timeout = schedule_timeout(timeout);
                finish_wait(sk_sleep(listener), &wait);
@@ -1876,17 +1876,14 @@ static int vsock_accept(struct socket *sock, struct socket *newsock,
                if (signal_pending(current)) {
                        err = sock_intr_errno(timeout);
                        goto out;
-               } else if (timeout == 0) {
-                       err = -EAGAIN;
-                       goto out;
                }
-
-               prepare_to_wait(sk_sleep(listener), &wait, TASK_INTERRUPTIBLE);
        }
-       finish_wait(sk_sleep(listener), &wait);
 
-       if (listener->sk_err)
+       if (listener->sk_err) {
                err = -listener->sk_err;
+       } else if (!connected) {
+               err = -EAGAIN;
+       }
 
        if (connected) {
                sk_acceptq_removed(listener);