Linux CIFS filesystem development
 help / color / mirror / Atom feed
From: Huiwen He <huiwen.he@linux.dev>
To: linkinjeon@kernel.org
Cc: linux-cifs@vger.kernel.org, linux-kernel@vger.kernel.org,
	senozhatsky@chromium.org, tom@talpey.com,
	chenxiaosong@chenxiaosong.com, tj@kernel.org,
	jiangshanlai@gmail.com, stefan.chulski@broadcom.com
Subject: [PATCH 4/4] ksmbd: add request workqueue mode startup option
Date: Tue,  1 Dec 2026 11:27:48 +0800	[thread overview]
Message-ID: <20261201032749.2224354-5-huiwen.he@linux.dev> (raw)
In-Reply-To: <20261201032749.2224354-1-huiwen.he@linux.dev>

From: Huiwen He <hehuiwen@kylinos.cn>

ksmbd currently dispatches incoming SMB requests to a per-CPU
workqueue, queueing each request on the CPU running the connection's
receive thread. This favors cache locality, but pipelined requests can
leave the receiver and workers competing for the same CPU.

An unbound workqueue allows a worker to process one request on another
CPU while the receive thread handles the next. This overlap can reduce
queueing and improve throughput, but cross-CPU costs may also outweigh
the gain. For example, small replies contend more on srv_mutex, and
spreading work can place workers on lower-frequency CPUs.

Since neither mode fits every workload, let ksmbd.mountd select the
request workqueue at server startup. The ksmbd.conf option
"server request workqueue" accepts "percpu" or "unbound". Leaving it
unset preserves the existing per-CPU behavior.
Use the last reserved byte of the startup request for the mode.
ksmbd.mountd already uses the first byte to advertise quota support.

For unbound dispatch, set the affinity scope to CPU before exposing
the queue in sysfs. This favors the CPU that queued the request while
allowing workers to run on other CPUs.

Single-channel fio throughput in MiB/s on tmpfs (schedutil, 128 CPUs,
16 jobs, iodepth 32, mixed 50/50). Both unbound scopes are non-strict
(shard = cache_shard); delta compares unbound (cpu) with per-CPU.

  Size     I/O     per-CPU     unbound     unbound       delta
                               (shard)      (cpu)
  ------------------------------------------------------------
  4 KiB    READ        294         182         243      -17.3%
  4 KiB    WRITE       251         178         177      -29.5%
  4 KiB    MIXED       263         146         199      -24.3%

  64 KiB   READ       2583        1791        2171      -15.9%
  64 KiB   WRITE      1640        1882        2013      +22.8%
  64 KiB   MIXED      2194        1503        1834      -16.4%

  256 KiB  READ       3119        3012        3127       +0.2%
  256 KiB  WRITE      2183        3155        3233      +48.1%
  256 KiB  MIXED      2906        3213        3239      +11.4%

  1 MiB    READ       3398        3259        3413       +0.5%
  1 MiB    WRITE      2047        3032        3091      +51.0%
  1 MiB    MIXED      2859        3367        3416      +19.5%

Signed-off-by: Huiwen He <hehuiwen@kylinos.cn>
---
 fs/smb/server/ksmbd_netlink.h |  8 ++++-
 fs/smb/server/ksmbd_work.c    | 64 +++++++++++++++++++++++++++++++++--
 fs/smb/server/ksmbd_work.h    |  3 +-
 fs/smb/server/server.c        |  9 ++++-
 fs/smb/server/server.h        |  1 +
 fs/smb/server/transport_ipc.c |  5 +++
 6 files changed, 85 insertions(+), 5 deletions(-)

diff --git a/fs/smb/server/ksmbd_netlink.h b/fs/smb/server/ksmbd_netlink.h
index ad050988659e..d47ac9237bdc 100644
--- a/fs/smb/server/ksmbd_netlink.h
+++ b/fs/smb/server/ksmbd_netlink.h
@@ -80,6 +80,11 @@ struct ksmbd_heartbeat {
 #define KSMBD_GLOBAL_FLAG_SMB2_ENCRYPTION_OFF	BIT(3)
 #define KSMBD_GLOBAL_FLAG_DURABLE_HANDLE	BIT(4)
 
+enum ksmbd_wq_mode {
+	KSMBD_WQ_PERCPU = 0,
+	KSMBD_WQ_UNBOUND = 1,
+};
+
 /*
  * IPC request for ksmbd server startup
  */
@@ -115,7 +120,8 @@ struct ksmbd_startup_request {
 	__u32	max_ip_connections;	/* Number of maximum connection per ip address */
 	__s8	aapl_model[32];		/* AAPL model string for Finder icon, e.g. "Xserve" */
 	__u32	max_sessions_per_connection; /* Maximum sessions per connection */
-	__s8	reserved[463];		/* Reserved room */
+	__s8	reserved[462];		/* Reserved room */
+	__u8	request_wq_mode;	/* enum ksmbd_wq_mode */
 	__u32	ifc_list_sz;		/* interfaces list size */
 	__s8	____payload[];
 } __packed;
diff --git a/fs/smb/server/ksmbd_work.c b/fs/smb/server/ksmbd_work.c
index ad576683493f..33e930a62b56 100644
--- a/fs/smb/server/ksmbd_work.c
+++ b/fs/smb/server/ksmbd_work.c
@@ -11,12 +11,15 @@
 #include "server.h"
 #include "connection.h"
 #include "ksmbd_work.h"
+#include "ksmbd_netlink.h"
 #include "vfs_cache.h"
 #include "mgmt/ksmbd_ida.h"
 
 static struct kmem_cache *work_cache;
 static struct workqueue_struct *ksmbd_wq;
 static struct workqueue_struct *ksmbd_break_wq;
+static struct workqueue_struct *ksmbd_unbound_wq;
+static struct workqueue_struct *ksmbd_req_wq;
 
 static int ksmbd_reserve_iov(struct ksmbd_work *work, int need_iov_cnt)
 {
@@ -114,6 +117,57 @@ int ksmbd_work_pool_init(void)
 	return 0;
 }
 
+static int ksmbd_init_unbound_wq(void)
+{
+	struct workqueue_struct *wq;
+	struct workqueue_attrs *attrs;
+	int ret = -ENOMEM;
+
+	/* Reuse the queue on subsequent server starts. */
+	if (ksmbd_unbound_wq)
+		return 0;
+
+	wq = alloc_workqueue("ksmbd-io-unbound", WQ_UNBOUND, 0);
+	if (!wq)
+		return -ENOMEM;
+	attrs = alloc_workqueue_attrs();
+	if (!attrs)
+		goto destroy_unbound_wq;
+	attrs->affn_scope = WQ_AFFN_CPU;
+	attrs->affn_strict = false;
+	ret = apply_workqueue_attrs(wq, attrs);
+	free_workqueue_attrs(attrs);
+	if (ret)
+		goto destroy_unbound_wq;
+
+	/* Set the initial policy before exposing writable sysfs attributes. */
+	ret = workqueue_sysfs_register(wq);
+	if (ret)
+		goto destroy_unbound_wq;
+	ksmbd_unbound_wq = wq;
+	return 0;
+
+destroy_unbound_wq:
+	destroy_workqueue(wq);
+	return ret;
+}
+
+/* Called under ctrl_lock before starting listeners. */
+int ksmbd_prepare_request_wq(void)
+{
+	struct workqueue_struct *wq = ksmbd_wq;
+	int ret;
+
+	if (READ_ONCE(server_conf.request_wq_mode) == KSMBD_WQ_UNBOUND) {
+		ret = ksmbd_init_unbound_wq();
+		if (ret)
+			return ret;
+		wq = ksmbd_unbound_wq;
+	}
+	WRITE_ONCE(ksmbd_req_wq, wq);
+	return 0;
+}
+
 int ksmbd_workqueue_init(void)
 {
 	ksmbd_wq = alloc_workqueue("ksmbd-io", WQ_PERCPU, 0);
@@ -125,20 +179,26 @@ int ksmbd_workqueue_init(void)
 		ksmbd_wq = NULL;
 		return -ENOMEM;
 	}
+	WRITE_ONCE(ksmbd_req_wq, ksmbd_wq);
 	return 0;
 }
 
 void ksmbd_workqueue_destroy(void)
 {
+	WRITE_ONCE(ksmbd_req_wq, NULL);
+	if (ksmbd_unbound_wq) {
+		destroy_workqueue(ksmbd_unbound_wq);
+		ksmbd_unbound_wq = NULL;
+	}
 	destroy_workqueue(ksmbd_wq);
 	ksmbd_wq = NULL;
 	destroy_workqueue(ksmbd_break_wq);
 	ksmbd_break_wq = NULL;
 }
 
-bool ksmbd_queue_work(struct ksmbd_work *work)
+bool ksmbd_queue_request(struct ksmbd_work *work)
 {
-	return queue_work(ksmbd_wq, &work->work);
+	return queue_work(READ_ONCE(ksmbd_req_wq), &work->work);
 }
 
 void ksmbd_queue_io_work(struct work_struct *work)
diff --git a/fs/smb/server/ksmbd_work.h b/fs/smb/server/ksmbd_work.h
index 9fce93582eda..ef9fd66f3f7f 100644
--- a/fs/smb/server/ksmbd_work.h
+++ b/fs/smb/server/ksmbd_work.h
@@ -165,8 +165,9 @@ void ksmbd_work_pool_destroy(void);
 int ksmbd_work_pool_init(void);
 
 int ksmbd_workqueue_init(void);
+int ksmbd_prepare_request_wq(void);
 void ksmbd_workqueue_destroy(void);
-bool ksmbd_queue_work(struct ksmbd_work *work);
+bool ksmbd_queue_request(struct ksmbd_work *work);
 void ksmbd_queue_io_work(struct work_struct *work);
 void ksmbd_queue_break_work(struct work_struct *work);
 int ksmbd_iov_pin_rsp_read(struct ksmbd_work *work, void *ib, int len,
diff --git a/fs/smb/server/server.c b/fs/smb/server/server.c
index af3d2a81157a..a376e2bed59b 100644
--- a/fs/smb/server/server.c
+++ b/fs/smb/server/server.c
@@ -539,7 +539,7 @@ static int queue_ksmbd_work(struct ksmbd_conn *conn)
 	/* update activity on connection */
 	conn->last_active = jiffies;
 	INIT_WORK(&work->work, handle_ksmbd_work);
-	ksmbd_queue_work(work);
+	ksmbd_queue_request(work);
 	/* Negotiation can change conn->ops, finish it before reading another PDU. */
 	if (wait_for_neg)
 		wait_event(conn->req_running_q,
@@ -580,6 +580,7 @@ static void server_conf_free(void)
 
 static int server_conf_init(void)
 {
+	WRITE_ONCE(server_conf.request_wq_mode, KSMBD_WQ_PERCPU);
 	server_conf.enforced_signing = 0;
 	server_conf.min_protocol = ksmbd_min_protocol();
 	server_conf.max_protocol = ksmbd_max_protocol();
@@ -600,6 +601,12 @@ static int server_ctrl_handle_init(void)
 {
 	int ret;
 
+	ret = ksmbd_prepare_request_wq();
+	if (ret) {
+		pr_err("Failed to configure request workqueue: %d\n", ret);
+		return ret;
+	}
+
 	ksmbd_proc_reset();
 	ret = ksmbd_conn_transport_init();
 	if (ret)
diff --git a/fs/smb/server/server.h b/fs/smb/server/server.h
index 28704174d8df..2a54102040f0 100644
--- a/fs/smb/server/server.h
+++ b/fs/smb/server/server.h
@@ -51,6 +51,7 @@ struct ksmbd_server_config {
 	char			*conf[SERVER_CONF_WORK_GROUP + 1];
 	struct task_struct	*dh_task;
 	bool			bind_interfaces_only;
+	u8			request_wq_mode;
 	/* AAPL model string for Finder icon, e.g. "Xserve" */
 	char			aapl_model[32];
 };
diff --git a/fs/smb/server/transport_ipc.c b/fs/smb/server/transport_ipc.c
index 3c38d4a1e9cc..0539afddbd0b 100644
--- a/fs/smb/server/transport_ipc.c
+++ b/fs/smb/server/transport_ipc.c
@@ -318,6 +318,10 @@ static int ipc_server_config_on_startup(struct ksmbd_startup_request *req)
 	    !memchr(req->server_string, '\0', sizeof(req->server_string)))
 		return -EINVAL;
 
+	if (req->request_wq_mode != KSMBD_WQ_PERCPU &&
+	    req->request_wq_mode != KSMBD_WQ_UNBOUND)
+		return -EINVAL;
+
 	ksmbd_set_fd_limit(req->file_max);
 	server_conf.flags = req->flags;
 	server_conf.signing = req->signing;
@@ -392,6 +396,7 @@ static int ipc_server_config_on_startup(struct ksmbd_startup_request *req)
 			server_conf.max_protocol = ret;
 	}
 
+	WRITE_ONCE(server_conf.request_wq_mode, req->request_wq_mode);
 	if (server_conf.ipc_timeout)
 		schedule_delayed_work(&ipc_timer_work, server_conf.ipc_timeout);
 	return 0;

      parent reply	other threads:[~2026-12-01  3:28 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-01  3:27 [PATCH 0/4] ksmbd: allow request dispatch on an unbound workqueue Huiwen He
2026-12-01  3:27 ` [PATCH 1/4] ksmbd: keep server in RESETTING until defaults are restored Huiwen He
2026-12-01  3:27 ` [PATCH 2/4] ksmbd: reset the server directly on startup failure Huiwen He
2026-12-01  3:27 ` [PATCH 3/4] workqueue: export attribute and sysfs setup helpers Huiwen He
2026-12-01  3:27 ` Huiwen He [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261201032749.2224354-5-huiwen.he@linux.dev \
    --to=huiwen.he@linux.dev \
    --cc=chenxiaosong@chenxiaosong.com \
    --cc=jiangshanlai@gmail.com \
    --cc=linkinjeon@kernel.org \
    --cc=linux-cifs@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=senozhatsky@chromium.org \
    --cc=stefan.chulski@broadcom.com \
    --cc=tj@kernel.org \
    --cc=tom@talpey.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox