Skip to contentSkip to conversation

ksmbd: allow request dispatch on an unbound workqueue

PATCH New Huiwen He 1 participant 5 messages Dec 1

Subsystems (3)
lore
Share & tools
lore

Huiwen He

author
#0 lore raw

cover ksmbd: allow request dispatch on an unbound workqueue

From: Huiwen He <[email protected]>

ksmbd currently dispatches incoming requests to a per-CPU workqueue. Pipelined writes on a single connection can leave the receive thread and workers competing for the same CPU. An unbound workqueue lets workers use other CPUs, but the cost of cross-CPU handoffs can outweigh the gain from parallelism.

Patch 1 fixes a race that can let ksmbd accept a new startup request before reset finishes restoring the server defaults.

Patch 2 handles startup failures in the current control work item, so recovery does not depend on allocating another one.

Patch 3 exports the helpers needed to set CPU scope before sysfs registration.

Patch 4 lets ksmbd.mountd select the workqueue for all incoming SMB requests at server startup. With the companion ksmbd-tools patch, administrators can set "server request workqueue = unbound" in ksmbd.conf. The default is "percpu".

Single-channel fio throughput in MiB/s on tmpfs (schedutil, 128 CPUs, 16 jobs, iodepth 32, mixed 50/50 read/write). 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%

The October RFC explored using an unbound workqueue for selected WRITE requests [1].

As a follow-up, I am looking into an adaptive mode that weighs the benefits of parallelism against the costs of spreading work across CPUs. The goal is to outperform both fixed modes on most workloads and become the default.

[1] https://lore.kernel.org/r/[email protected]

Huiwen He (4):
ksmbd: keep server in RESETTING until defaults are restored
ksmbd: reset the server directly on startup failure
workqueue: export attribute and sysfs setup helpers
ksmbd: add request workqueue mode startup option

 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        | 27 +++++++++------
 fs/smb/server/server.h        |  4 ++-
 fs/smb/server/transport_ipc.c |  5 +++
 kernel/workqueue.c            |  4 +++
 7 files changed, 100 insertions(+), 15 deletions(-)

base-commit: 87ff512becd45a08c44fd47a9f03e607af9ef6bd

author
#1 lore raw

PATCH 1/4 ksmbd: keep server in RESETTING until defaults are restored

From: Huiwen He <[email protected]>

server_ctrl_handle_reset() can expose STARTING_UP before reset finishes:
a failed init leaves the server in STARTING_UP when reset clears the mountd PID, and server_conf_init() sets STARTING_UP before restoring defaults. Either window lets a new mountd startup race with teardown and default restoration.

Enter RESETTING before clearing the mountd PID, and publish STARTING_UP with release/acquire ordering only after defaults are restored.

Fixes 0626e6641f6b ("cifsd: add server handler for central processing and tranport layers")
---
 fs/smb/server/server.c | 6 +++---
 fs/smb/server/server.h | 3 ++-
 2 files changed, 5 insertions(+), 4 deletions(-)
fs/smb/server/server.c +3 -3
index 962f12e969ba..32a2201284f3 100644
@@ -580,7 +580,6 @@ static void server_conf_free(void)
 
 static int server_conf_init(void)
 {
-	WRITE_ONCE(server_conf.state, SERVER_STATE_STARTING_UP);
 	server_conf.enforced_signing = 0;
 	server_conf.min_protocol = ksmbd_min_protocol();
 	server_conf.max_protocol = ksmbd_max_protocol();
@@ -592,6 +591,8 @@ static int server_conf_init(void)
 	server_conf.max_inflight_req = SMB2_MAX_CREDITS;
 	server_conf.max_sessions_per_connection =
 		KSMBD_DEFAULT_MAX_SESSIONS_PER_CONNECTION;
+	/* Publish the defaults before accepting a mountd startup event. */
+	smp_store_release(&server_conf.state, SERVER_STATE_STARTING_UP);
 	return 0;
 }
 
@@ -612,12 +613,12 @@ static void server_ctrl_handle_init(struct server_ctrl_struct *ctrl)
 
 static void server_ctrl_handle_reset(struct server_ctrl_struct *ctrl)
 {
+	WRITE_ONCE(server_conf.state, SERVER_STATE_RESETTING);
 	ksmbd_ipc_soft_reset();
 	ksmbd_conn_transport_destroy();
 	ksmbd_stop_durable_scavenger();
 	server_conf_free();
 	server_conf_init();
-	WRITE_ONCE(server_conf.state, SERVER_STATE_STARTING_UP);
 }
 
 static void server_ctrl_handle_work(struct work_struct *work)
@@ -695,7 +696,6 @@ static ssize_t kill_server_store(const struct class *class,
 
 	pr_info("kill command received\n");
 	mutex_lock(&ctrl_lock);
-	WRITE_ONCE(server_conf.state, SERVER_STATE_RESETTING);
 	__module_get(THIS_MODULE);
 	server_ctrl_handle_reset(NULL);
 	module_put(THIS_MODULE);
fs/smb/server/server.h +2 -1
index 7b7df3fafe5b..28704174d8df 100644
@@ -72,7 +72,8 @@ static inline int ksmbd_server_running(void)
 
 static inline int ksmbd_server_configurable(void)
 {
-	return READ_ONCE(server_conf.state) < SERVER_STATE_RESETTING;
+	/* Pairs with the release in server_conf_init(). */
+	return smp_load_acquire(&server_conf.state) < SERVER_STATE_RESETTING;
 }
 
 int server_queue_ctrl_init_work(void);
author
#2 lore raw

PATCH 2/4 ksmbd: reset the server directly on startup failure

In reply to Huiwen He

From: Huiwen He <[email protected]>

If transport initialization fails, ksmbd queues another control work item to reset the server. If allocating that item fails, the server never schedules the reset.

Return the initialization error to the control worker and run the reset there while it still holds ctrl_lock.

Fixes 0626e6641f6b ("cifsd: add server handler for central processing and tranport layers")
---
 fs/smb/server/server.c | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)
fs/smb/server/server.c +6 -6
index 32a2201284f3..af3d2a81157a 100644
@@ -596,19 +596,18 @@ static int server_conf_init(void)
 	return 0;
 }
 
-static void server_ctrl_handle_init(struct server_ctrl_struct *ctrl)
+static int server_ctrl_handle_init(void)
 {
 	int ret;
 
 	ksmbd_proc_reset();
 	ret = ksmbd_conn_transport_init();
-	if (ret) {
-		server_queue_ctrl_reset_work();
-		return;
-	}
+	if (ret)
+		return ret;
 
 	pr_info("running\n");
 	WRITE_ONCE(server_conf.state, SERVER_STATE_RUNNING);
+	return 0;
 }
 
 static void server_ctrl_handle_reset(struct server_ctrl_struct *ctrl)
@@ -630,7 +629,8 @@ static void server_ctrl_handle_work(struct work_struct *work)
 	mutex_lock(&ctrl_lock);
 	switch (ctrl->type) {
 	case SERVER_CTRL_TYPE_INIT:
-		server_ctrl_handle_init(ctrl);
+		if (server_ctrl_handle_init())
+			server_ctrl_handle_reset(ctrl);
 		break;
 	case SERVER_CTRL_TYPE_RESET:
 		server_ctrl_handle_reset(ctrl);
author
#3 lore raw

PATCH 3/4 workqueue: export attribute and sysfs setup helpers

In reply to Huiwen He

From: Huiwen He <[email protected]>

The workqueue attribute helpers are not available to modules.

Export the allocation, application and release helpers for GPL modules. Also export workqueue_sysfs_register() so a module can apply attributes before exposing its queue in sysfs.

---
 kernel/workqueue.c | 4 ++++
 1 file changed, 4 insertions(+)
kernel/workqueue.c +4 -0
index a2a441837a9d..f525618afdb5 100644
@@ -5012,6 +5012,7 @@ void free_workqueue_attrs(struct workqueue_attrs *attrs)
 		kfree(attrs);
 	}
 }
+EXPORT_SYMBOL_GPL(free_workqueue_attrs);
 
 /**
  * alloc_workqueue_attrs - allocate a workqueue_attrs
@@ -5040,6 +5041,7 @@ struct workqueue_attrs *alloc_workqueue_attrs_noprof(void)
 	free_workqueue_attrs(attrs);
 	return NULL;
 }
+EXPORT_SYMBOL_GPL(alloc_workqueue_attrs_noprof);
 
 static void copy_workqueue_attrs(struct workqueue_attrs *to,
 				 const struct workqueue_attrs *from)
@@ -5878,6 +5880,7 @@ int apply_workqueue_attrs(struct workqueue_struct *wq,
 
 	return ret;
 }
+EXPORT_SYMBOL_GPL(apply_workqueue_attrs);
 
 /**
  * unbound_wq_update_pwq - update a pwq slot for CPU hot[un]plug
@@ -8001,6 +8004,7 @@ int workqueue_sysfs_register(struct workqueue_struct *wq)
 	kobject_uevent(&wq_dev->dev.kobj, KOBJ_ADD);
 	return 0;
 }
+EXPORT_SYMBOL_GPL(workqueue_sysfs_register);
 
 /**
  * workqueue_sysfs_unregister - undo workqueue_sysfs_register()
author
#4 lore raw

PATCH 4/4 ksmbd: add request workqueue mode startup option

In reply to Huiwen He

From: Huiwen He <[email protected]>

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%
---
 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(-)
fs/smb/server/ksmbd_netlink.h +7 -1
index ad050988659e..d47ac9237bdc 100644
@@ -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;
fs/smb/server/ksmbd_work.c +62 -2

100 lines not on this page · raw message

fs/smb/server/ksmbd_work.h +2 -1
index 9fce93582eda..ef9fd66f3f7f 100644
@@ -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,
fs/smb/server/server.c +8 -1
index af3d2a81157a..a376e2bed59b 100644
@@ -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)
fs/smb/server/server.h +1 -0
index 28704174d8df..2a54102040f0 100644
@@ -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];
 };
fs/smb/server/transport_ipc.c +5 -0
index 3c38d4a1e9cc..0539afddbd0b 100644
@@ -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;