Skip to content

Commit 3316a8f

Browse files
LGA1150smfrench
authored andcommitted
ksmbd: server: avoid busy polling in accept loop
The ksmbd listener thread was using busy waiting on a listening socket by calling kernel_accept() with SOCK_NONBLOCK and retrying every 100ms on -EAGAIN. Since this thread is dedicated to accepting new connections, there is no need for non-blocking mode. Switch to a blocking accept() call instead, allowing the thread to sleep until a new connection arrives. This avoids unnecessary wakeups and CPU usage. During teardown, call shutdown() on the listening socket so that accept() returns -EINVAL and the thread exits cleanly. The socket release mutex is redundant because kthread_stop() blocks until the listener thread returns, guaranteeing safe teardown ordering. Also remove sk_rcvtimeo and sk_sndtimeo assignments, which only caused accept() to return -EAGAIN prematurely. Signed-off-by: Qingfang Deng <dqfext@gmail.com> Reviewed-by: Stefan Metzmacher <metze@samba.org> Acked-by: Namjae Jeon <linkinjeon@kernel.org> Signed-off-by: Steve French <stfrench@microsoft.com>
1 parent 5003ad7 commit 3316a8f

1 file changed

Lines changed: 6 additions & 35 deletions

File tree

fs/smb/server/transport_tcp.c

Lines changed: 6 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ struct interface {
2222
struct socket *ksmbd_socket;
2323
struct list_head entry;
2424
char *name;
25-
struct mutex sock_release_lock;
2625
int state;
2726
};
2827

@@ -56,19 +55,6 @@ static inline void ksmbd_tcp_reuseaddr(struct socket *sock)
5655
sock_set_reuseaddr(sock->sk);
5756
}
5857

59-
static inline void ksmbd_tcp_rcv_timeout(struct socket *sock, s64 secs)
60-
{
61-
if (secs && secs < MAX_SCHEDULE_TIMEOUT / HZ - 1)
62-
WRITE_ONCE(sock->sk->sk_rcvtimeo, secs * HZ);
63-
else
64-
WRITE_ONCE(sock->sk->sk_rcvtimeo, MAX_SCHEDULE_TIMEOUT);
65-
}
66-
67-
static inline void ksmbd_tcp_snd_timeout(struct socket *sock, s64 secs)
68-
{
69-
sock_set_sndtimeo(sock->sk, secs);
70-
}
71-
7258
static struct tcp_transport *alloc_transport(struct socket *client_sk)
7359
{
7460
struct tcp_transport *t;
@@ -236,20 +222,14 @@ static int ksmbd_kthread_fn(void *p)
236222
unsigned int max_ip_conns;
237223

238224
while (!kthread_should_stop()) {
239-
mutex_lock(&iface->sock_release_lock);
240225
if (!iface->ksmbd_socket) {
241-
mutex_unlock(&iface->sock_release_lock);
242226
break;
243227
}
244-
ret = kernel_accept(iface->ksmbd_socket, &client_sk,
245-
SOCK_NONBLOCK);
246-
mutex_unlock(&iface->sock_release_lock);
247-
if (ret) {
248-
if (ret == -EAGAIN)
249-
/* check for new connections every 100 msecs */
250-
schedule_timeout_interruptible(HZ / 10);
228+
ret = kernel_accept(iface->ksmbd_socket, &client_sk, 0);
229+
if (ret == -EINVAL)
230+
break;
231+
if (ret)
251232
continue;
252-
}
253233

254234
if (!server_conf.max_ip_connections)
255235
goto skip_max_ip_conns_limit;
@@ -458,10 +438,6 @@ static void tcp_destroy_socket(struct socket *ksmbd_socket)
458438
if (!ksmbd_socket)
459439
return;
460440

461-
/* set zero to timeout */
462-
ksmbd_tcp_rcv_timeout(ksmbd_socket, 0);
463-
ksmbd_tcp_snd_timeout(ksmbd_socket, 0);
464-
465441
ret = kernel_sock_shutdown(ksmbd_socket, SHUT_RDWR);
466442
if (ret)
467443
pr_err("Failed to shutdown socket: %d\n", ret);
@@ -532,9 +508,6 @@ static int create_socket(struct interface *iface)
532508
goto out_error;
533509
}
534510

535-
ksmbd_socket->sk->sk_rcvtimeo = KSMBD_TCP_RECV_TIMEOUT;
536-
ksmbd_socket->sk->sk_sndtimeo = KSMBD_TCP_SEND_TIMEOUT;
537-
538511
ret = kernel_listen(ksmbd_socket, KSMBD_SOCKET_BACKLOG);
539512
if (ret) {
540513
pr_err("Port listen() error: %d\n", ret);
@@ -604,12 +577,11 @@ static int ksmbd_netdev_event(struct notifier_block *nb, unsigned long event,
604577
if (iface && iface->state == IFACE_STATE_CONFIGURED) {
605578
ksmbd_debug(CONN, "netdev-down event: netdev(%s) is going down\n",
606579
iface->name);
580+
kernel_sock_shutdown(iface->ksmbd_socket, SHUT_RDWR);
607581
tcp_stop_kthread(iface->ksmbd_kthread);
608582
iface->ksmbd_kthread = NULL;
609-
mutex_lock(&iface->sock_release_lock);
610-
tcp_destroy_socket(iface->ksmbd_socket);
583+
sock_release(iface->ksmbd_socket);
611584
iface->ksmbd_socket = NULL;
612-
mutex_unlock(&iface->sock_release_lock);
613585

614586
iface->state = IFACE_STATE_DOWN;
615587
break;
@@ -672,7 +644,6 @@ static struct interface *alloc_iface(char *ifname)
672644
iface->name = ifname;
673645
iface->state = IFACE_STATE_DOWN;
674646
list_add(&iface->entry, &iface_list);
675-
mutex_init(&iface->sock_release_lock);
676647
return iface;
677648
}
678649

0 commit comments

Comments
 (0)