Skip to contentSkip to conversation

ksmbd: improve WRITE scheduling and TCP READ sends

RFC PATCH In review Huiwen He 3 participants 10 messages Oct 22 – Oct 29

Subsystems (3)
lore
Share & tools
lore

Huiwen He

author
#0 lore raw

cover ksmbd: improve WRITE scheduling and TCP READ sends

From: Huiwen He <[email protected]>

Hi Namjae,

This series addresses two sources of overhead in ksmbd:

- WRITE scheduling: requests from one connection can accumulate on a busy per-CPU worker pool even when other CPUs are available. Route eligible single-channel WRITEs to a CPU-scoped, non-strict unbound workqueue, while keeping other requests on the per-CPU queue.

- READ sending: TCP copies READ data after the filesystem has filled the response buffer. Use page-backed buffers and MSG_SPLICE_PAGES to avoid this copy, with the existing copy path as a fallback.

Test setup:
Server: Linux 7.3.0-rc3+, dual socket, 128 CPUs, tmpfs.
Client: Linux 7.0.0-38-generic, local 4-vCPU VM; file-data caching
bypassed for both protocols.
NFS: Linux nfsd, NFSv4.2/TCP, sec=sys, 32 threads.
SMB: ksmbd and Samba 4.23.6, SMB3.1.1.
fio: 16 jobs, QD32/job, 1 GiB total, libaio; four 30-second runs
after warmup. Mixed: --rw=rw --rwmixread=50, READ + WRITE.

1. Baseline throughput (median MiB/s)

  Block  Conn  Workload       NFS      Samba      ksmbd
  -----  ----  --------  ---------  ---------  ---------
  4KiB      1  READ         195.59     241.71     292.64
  4KiB      1  WRITE        154.68     228.92     219.55
  4KiB      1  mixed        172.49     232.42     236.85
  4KiB      2  READ         346.23     274.58     401.12
  4KiB      2  WRITE        312.62     292.39     325.75
  4KiB      2  mixed        329.28     286.86     345.04
  4KiB      4  READ         428.34     277.92     463.80
  4KiB      4  WRITE        387.19     285.59     415.65
  4KiB      4  mixed        402.74     284.52     397.76
  64KiB     1  READ        2156.89    1647.55    2152.98
  64KiB     1  WRITE       1664.58    1956.34    1808.18
  64KiB     1  mixed       1790.19    1854.00    1961.47
  64KiB     2  READ        3607.94    1526.36    3345.74
  64KiB     2  WRITE       2969.75    2706.88    3223.25
  64KiB     2  mixed       3469.13    1990.95    3285.02
  64KiB     4  READ        4375.82    1850.92    4213.09
  64KiB     4  WRITE       2622.47    2908.68    3016.62
  64KiB     4  mixed       4293.94    1857.41    3950.03
  1MiB      1  READ        3688.67    2266.23    2142.63
  1MiB      1  WRITE       3750.42    3782.38    2184.30
  1MiB      1  mixed       3986.36    3108.39    2827.65
  1MiB      2  READ        5756.65    2245.04    3960.96
  1MiB      2  WRITE       3858.07    3418.41    4063.92
  1MiB      2  mixed       5347.24    3151.78    4614.56
  1MiB      4  READ        7908.09    2378.32    6750.79
  1MiB      4  WRITE       3422.60    4138.59    3566.57
  1MiB      4  mixed       6161.38    2676.61    5471.26

2. Combined throughput (median MiB/s)

Before/After: both optimizations off/on in the same kernel.

  Block  Ch  Workload     Before      After    Paired %
  -----  --  --------  ---------  ---------  ----------
  4KiB    1  READ         292.64     282.21      -3.61%
  4KiB    1  WRITE        219.55     192.84     -11.01%
  4KiB    1  mixed        236.85     240.15      +0.36%
  4KiB    2  READ         401.12     404.66      +1.17%
  4KiB    2  WRITE        325.75     342.19      +5.37%
  4KiB    2  mixed        345.04     336.34      -2.12%
  4KiB    4  READ         463.80     456.10      -1.95%
  4KiB    4  WRITE        415.65     417.24      -1.06%
  4KiB    4  mixed        397.76     386.93      -2.74%
  64KiB   1  READ        2152.98    2558.10     +18.34%
  64KiB   1  WRITE       1808.18    2031.92     +11.69%
  64KiB   1  mixed       1961.47    2176.16      +9.53%
  64KiB   2  READ        3345.74    3591.95      +7.32%
  64KiB   2  WRITE       3223.25    3205.70      -0.54%
  64KiB   2  mixed       3285.02    3317.22      +2.39%
  64KiB   4  READ        4213.09    4427.29      +4.65%
  64KiB   4  WRITE       3016.62    3047.30      -0.59%
  64KiB   4  mixed       3950.03    3962.34      +0.44%
  1MiB    1  READ        2142.63    3353.03     +56.51%
  1MiB    1  WRITE       2184.30    3460.33     +58.75%
  1MiB    1  mixed       2827.65    3096.88      +9.71%
  1MiB    2  READ        3960.96    5546.88     +39.95%
  1MiB    2  WRITE       4063.92    4174.43      +2.75%
  1MiB    2  mixed       4614.56    4954.98      +7.37%
  1MiB    4  READ        6750.79    8311.51     +21.79%
  1MiB    4  WRITE       3566.57    3612.88      +0.74%
  1MiB    4  mixed       5471.26    5604.89      +3.85%

4 KiB single-channel WRITE regresses by 11.01%. If no better solution is found, a request-size check could fall back to the per-CPU queue for small WRITEs.

Thanks,
Huiwen

Huiwen He (4):
workqueue: export attribute and sysfs setup helpers
ksmbd: route WRITE requests to an unbound workqueue
ksmbd: complete TCP frames before reporting send success
ksmbd: send READ payloads with MSG_SPLICE_PAGES

 fs/smb/server/connection.c    |   1 +
 fs/smb/server/connection.h    |   6 ++
 fs/smb/server/ksmbd_work.c    | 141 ++++++++++++++++++++++++++++------
 fs/smb/server/ksmbd_work.h    |  11 +++
 fs/smb/server/server.c        |  78 ++++++++++++++++++-
 fs/smb/server/smb2pdu.c       | 126 +++++++++++++++++++++++++-----
 fs/smb/server/smb2pdu.h       |   1 +
 fs/smb/server/transport_tcp.c |  71 ++++++++++++++++-
 fs/smb/server/transport_tcp.h |   1 +
 kernel/workqueue.c            |   4 +
 10 files changed, 395 insertions(+), 45 deletions(-)
Signature · 1 line
2.43.0
author
#1 lore raw

RFC PATCH 1/4 workqueue: export attribute and sysfs setup helpers

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 1ae3732a2c51..ec4709395594 100644
@@ -4822,6 +4822,7 @@ void free_workqueue_attrs(struct workqueue_attrs *attrs)
 		kfree(attrs);
 	}
 }
+EXPORT_SYMBOL_GPL(free_workqueue_attrs);
 
 /**
  * alloc_workqueue_attrs - allocate a workqueue_attrs
@@ -4850,6 +4851,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)
@@ -5618,6 +5620,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
@@ -7746,6 +7749,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()
Signature · 1 line
2.43.0
maintainer Acked-by on v1 1/4
#2 lore raw

Re: [RFC PATCH 1/4] workqueue: export attribute and sysfs setup helpers

Quoted 7 lines · Huiwen He

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.

Thanks.

--
tejun

author
#3 lore raw

RFC PATCH 2/4 ksmbd: route WRITE requests to an unbound workqueue

In reply to Huiwen He

From: Huiwen He <[email protected]>

WRITE requests from one connection can accumulate on a busy per-CPU pool, limiting throughput even when other CPUs are available.

Route eligible single-channel WRITEs to a CPU-scoped, non-strict unbound workqueue, while retaining per-CPU execution for other requests.

Tested with fio on a 128-CPU server with a local 4-vCPU client VM (tmpfs, 16 jobs, QD32 per job, libaio, direct=0, cache=none; mixed=50/50 R/W). Throughput (MiB/s) and paired changes are medians of four runs.

A: write_wq=0; B: write_wq=1.

  Block  Ch  Workload      A MiB/s    B MiB/s  B/A change
  -----  --  ---------  ---------  ---------  ----------
  4KiB    1  READ          292.64     290.73      -0.71%
  4KiB    1  WRITE         219.55     193.01     -12.03%
  4KiB    1  mixed         236.85     238.67      +0.46%
  4KiB    2  READ          401.12     404.46      +2.12%
  4KiB    2  WRITE         325.75     334.67      +4.22%
  4KiB    2  mixed         345.04     338.24      -2.46%
  4KiB    4  READ          463.80     453.28      -1.41%
  4KiB    4  WRITE         415.65     415.69      -0.14%
  4KiB    4  mixed         397.76     396.75      -1.35%
  64KiB   1  READ         2152.98    2162.74      +4.12%
  64KiB   1  WRITE        1808.18    1976.37      +9.34%
  64KiB   1  mixed        1961.47    1974.08      +0.53%
  64KiB   2  READ         3345.74    3246.35      -2.93%
  64KiB   2  WRITE        3223.25    3222.85      -0.14%
  64KiB   2  mixed        3285.02    3283.75      +0.36%
  64KiB   4  READ         4213.09    4227.13      +0.34%
  64KiB   4  WRITE        3016.62    3089.17      +1.82%
  64KiB   4  mixed        3950.03    3877.03      -2.00%
  1MiB    1  READ         2142.63    2126.23      -0.99%
  1MiB    1  WRITE        2184.30    3452.84     +58.30%
  1MiB    1  mixed        2827.65    2961.50      +5.88%
  1MiB    2  READ         3960.96    3887.33      -1.99%
  1MiB    2  WRITE        4063.92    4158.22      +2.37%
  1MiB    2  mixed        4613.54    4631.33      +0.39%
  1MiB    4  READ         6750.79    6707.91      -1.49%
  1MiB    4  WRITE        3566.57    3557.26      +0.64%
  1MiB    4  mixed        5471.26    5444.00      +0.42%

Mixed workloads show no consistent gain: interleaved READs keep later WRITEs per-CPU until the connection becomes idle.

---
 fs/smb/server/connection.h    |  4 ++
 fs/smb/server/ksmbd_work.c    | 53 ++++++++++++++++++++++++
 fs/smb/server/ksmbd_work.h    |  2 +
 fs/smb/server/server.c        | 77 ++++++++++++++++++++++++++++++++++-
 fs/smb/server/smb2pdu.c       |  6 +++
 fs/smb/server/transport_tcp.c |  5 +++
 fs/smb/server/transport_tcp.h |  1 +
 7 files changed, 147 insertions(+), 1 deletion(-)
fs/smb/server/connection.h +4 -0
index 63484c8efbbd..a92e0fea0f06 100644
@@ -59,6 +59,10 @@ struct ksmbd_conn {
 	struct smb_version_cmds		*cmds;
 	unsigned int			max_cmds;
 	struct mutex			srv_mutex;
+	/* RX-only: keep WRITEs per-CPU until idle is observed. */
+	bool				write_wq_blocked;
+	/* Sticky across channel removal; accessed with READ/WRITE_ONCE. */
+	bool				multichannel_seen;
 	int				status;
 	unsigned int			cli_cap;
 	bool				stop_called;
fs/smb/server/ksmbd_work.c +53 -0

79 lines not on this page · raw message

fs/smb/server/ksmbd_work.h +2 -0
index 0844aa929f55..9f87b6c4393a 100644
@@ -156,6 +156,8 @@ int ksmbd_work_pool_init(void);
 int ksmbd_workqueue_init(void);
 void ksmbd_workqueue_destroy(void);
 bool ksmbd_queue_work(struct ksmbd_work *work);
+extern bool ksmbd_write_wq_enabled;
+bool ksmbd_queue_request(struct ksmbd_work *work, bool use_write_wq);
 int ksmbd_iov_pin_rsp_read(struct ksmbd_work *work, void *ib, int len,
 			   void *aux_buf, unsigned int aux_size);
 int ksmbd_iov_pin_rsp(struct ksmbd_work *work, void *ib, int len);
fs/smb/server/server.c +76 -1

101 lines not on this page · raw message

fs/smb/server/smb2pdu.c +6 -0
index b7ce67094626..54ecdd096bd2 100644
@@ -125,6 +125,12 @@ static int register_session_channel(struct ksmbd_session *sess,
 	if (xa_is_err(old)) {
 		kfree_sensitive(chann);
 		rc = xa_err(old);
+		goto out;
+	}
+	if (ksmbd_write_wq_enabled && count) {
+		/* Remember the extra channel even if binding later fails. */
+		xa_for_each(&sess->ksmbd_chann_list, index, chann)
+			WRITE_ONCE(chann->conn->multichannel_seen, true);
 	}
 out:
 	up_write(&sess->chann_lock);
fs/smb/server/transport_tcp.c +5 -0
index 4968cfc1a572..5a2aad35739d 100644
@@ -719,3 +719,8 @@ static const struct ksmbd_transport_ops ksmbd_tcp_transport_ops = {
 	.shutdown	= ksmbd_tcp_shutdown,
 	.free_transport = ksmbd_tcp_free_transport,
 };
+
+bool ksmbd_transport_is_tcp(struct ksmbd_transport *t)
+{
+	return t->ops == &ksmbd_tcp_transport_ops;
+}
fs/smb/server/transport_tcp.h +1 -0
index 1e51675ee1b2..4095854ee91d 100644
@@ -9,6 +9,7 @@
 int ksmbd_tcp_set_interfaces(char *ifc_list, int ifc_list_sz);
 struct interface *ksmbd_find_netdev_name_iface_list(char *netdev_name);
 void ksmbd_free_transport(struct ksmbd_transport *kt);
+bool ksmbd_transport_is_tcp(struct ksmbd_transport *t);
 int ksmbd_tcp_init(void);
 void ksmbd_tcp_destroy(void);
 
Signature · 1 line
2.43.0
maintainer
#4 lore raw

Re: [RFC PATCH 2/4] ksmbd: route WRITE requests to an unbound workqueue

fs/smb/server/ksmbd_work.c line 26

Quoted 10 lines
fs/smb/server/ksmbd_work.c +6 -0
@@ -17,6 +18,23 @@
 static struct kmem_cache *work_cache;
 static struct workqueue_struct *ksmbd_wq;
 
+/* Load-time switch for the WRITE request routing policy. */
+bool ksmbd_write_wq_enabled;
+module_param_named(write_wq, ksmbd_write_wq_enabled, bool, 0444);
+MODULE_PARM_DESC(write_wq,
+                "Route eligible WRITEs to an unbound workqueue; load-time only");
+

Since this improves performance, why isn't it enabled by default? Also, I would like this setting to be on/off via ksmbd.conf.

Quoted 17 lines
fs/smb/server/server.c +17 -0
+enum ksmbd_wq_class {
+       KSMBD_WQ_LOCAL,
+       KSMBD_WQ_WRITE,
+       KSMBD_WQ_BLOCK_WRITE,
+};
+
+/*
+ * Classify a complete RFC1002-framed request for workqueue placement only.
+ * Check bounds before inspecting fields; leave protocol validation to the worker.
+ */
+static enum ksmbd_wq_class ksmbd_smb2_wq_classify(void *buf)
+{
+       const struct smb2_hdr *hdr = smb_get_msg(buf);
+       const struct smb2_write_req *req = (const void *)hdr;
+       unsigned int len = get_rfc1002_len(buf);
+
+       if (len < sizeof(*hdr) || hdr->ProtocolId != SMB2_PROTO_NUMBER ||

fs/smb/server/server.c static void handle_ksmbd_work(struct work_struct *wk) · line 403

Quoted 10 lines
fs/smb/server/server.c +10 -0
+           hdr->NextCommand ||
+           (hdr->Flags & (SMB2_FLAGS_SERVER_TO_REDIR | SMB2_FLAGS_ASYNC_COMMAND |
+                          SMB2_FLAGS_RELATED_OPERATIONS | SMB2_FLAGS_SIGNED)))
+               return KSMBD_WQ_BLOCK_WRITE;
+
+       switch (get_unaligned_le16(&hdr->Command)) {
+       case SMB2_WRITE_HE:
+               if (len < sizeof(*req) || !req->Length ||
+                   req->Channel != SMB2_CHANNEL_NONE || req->Flags)
+                       return KSMBD_WQ_BLOCK_WRITE;

For 4 KiB single-channel WRITEs, throughput drops from 219.55 to 193.01 MiB/s. Should we add a size check here to keep small WRITEs on the per-CPU queue?

Quoted 25 lines
fs/smb/server/server.c +20 -0
+               return KSMBD_WQ_WRITE;
+       case SMB2_NEGOTIATE_HE:
+       case SMB2_SESSION_SETUP_HE:
+       case SMB2_LOGOFF_HE:
+       case SMB2_TREE_CONNECT_HE:
+       case SMB2_TREE_DISCONNECT_HE:
+       case SMB2_CREATE_HE:
+       case SMB2_CLOSE_HE:
+       case SMB2_FLUSH_HE:
+       case SMB2_CANCEL_HE:
+       case SMB2_ECHO_HE:
+       case SMB2_QUERY_INFO_HE:
+       case SMB2_SET_INFO_HE:
+               /* Keep these commands per-CPU without blocking the WRITE queue. */
+               return KSMBD_WQ_LOCAL;
+       default:
+               /* READ and unclassified traffic also keep later WRITEs per-CPU. */
+               return KSMBD_WQ_BLOCK_WRITE;
+       }
+}
fs/smb/server/smb2pdu.c +0 -0
index b7ce67094626..54ecdd096bd2 100644

fs/smb/server/smb2pdu.c static int register_session_channel(struct ksmbd_session *sess, · line 133

Quoted 9 lines
fs/smb/server/smb2pdu.c +6 -0
@@ -125,6 +125,12 @@ static int register_session_channel(struct ksmbd_session *sess,
        if (xa_is_err(old)) {
                kfree_sensitive(chann);
                rc = xa_err(old);
+               goto out;
+       }
+       if (ksmbd_write_wq_enabled && count) {
+               /* Remember the extra channel even if binding later fails. */
+               xa_for_each(&sess->ksmbd_chann_list, index, chann)
+                       WRITE_ONCE(chann->conn->multichannel_seen, true);

This walks the entire channel list and sets multichannel_seen again whenever an additional channel is registered. Since the flag is sticky, all existing channels are already marked when count > 1. Could we walk the list only when count == 1, marking both the existing and newly added connections, and update only conn->multichannel_seen when count > 1? This would avoid the repeated traversal and stores.

author
#5 lore raw

Re: [RFC PATCH 2/4] ksmbd: route WRITE requests to an unbound workqueue

Hi Namjae,

Quoted 2 lines · Namjae Jeon

Since this improves performance, why isn't it enabled by default? Also, I would like this setting to be on/off via ksmbd.conf.

I left it off by default for now, since I've noticed some regressions.I'm testing more cases and trying to find a general fix. If those regressions can't be avoided, a ksmbd.conf option would be needed.

Quoted 3 lines

For 4 KiB single-channel WRITEs, throughput drops from 219.55 to 193.01 MiB/s. Should we add a size check here to keep small WRITEs on the per-CPU queue?

I tried keeping small WRITEs on PERCPU and routing only larger ones to UNBOUND. However, this did not work well for workloads with mixed request sizes. For example, a workload mixing 8 KiB and 64 KiB WRITEs still regressed. Small and large WRITEs share the same connection and send lock, so a size check alone does not avoid contention.

I then changed the send path to let one worker send several completed WRITE responses together, without waiting for more responses. This seems to work better:

Single-channel WRITE, 16 jobs, QD32 per job, tmpfs on a 128-CPU server, Throughput is in MiB/s.

Size PERCPU UNBOUND+batch Change
4 KiB 228.12 248.89 +6.96%

64 KiB     1797.26          2091.17   +15.34%
1 MiB      2188.42          3330.72   +53.47%

However, I found that there is still a performance regression at QD1.

1 MiB WRITE, one channel, one job:

With schedutil:

QD PERCPU UNBOUND+batch Change

 1   1945.94          1021.28   -47.52%
 2   2844.66          2567.18    -9.75%
 4   2338.03          3547.42   +51.73%

Performance governor on all host CPUs:

QD    PERCPU    UNBOUND+batch    Change
 1   1974.35          1785.16    -9.58%
 2   2904.12          3038.52    +4.63%

4 2418.45 3659.93 +51.33%

CPU frequency scaling makes the QD1 regression larger.

The main challenge seems to be balancing CPU locality and parallelism. Keeping the work local helps QD1, while allowing it to run on another CPU helps when receiving and processing requests in parallel. I have not yet found a suitable policy that works well for both.

Any ideas about this?

Quoted 6 lines

This walks the entire channel list and sets multichannel_seen again whenever an additional channel is registered. Since the flag is sticky, all existing channels are already marked when count > 1. Could we walk the list only when count == 1, marking both the existing and newly added connections, and update only conn->multichannel_seen when count > 1? This would avoid the repeated traversal and stores.

Agreed. Thanks for pointing this out.

Best regards,
Huiwen

author
#6 lore raw

RFC PATCH 3/4 ksmbd: complete TCP frames before reporting send success

In reply to Huiwen He

From: Huiwen He <[email protected]>

A positive kernel_sendmsg() return may cover only part of an SMB frame. Reporting success lets the next response follow an incomplete frame.

Retry short sends with the same iterator while holding srv_mutex. On zero progress or error, shut down the socket before releasing the lock so no later response can follow an incomplete frame.

---
 fs/smb/server/transport_tcp.c | 17 +++++++++++++++--
 1 file changed, 15 insertions(+), 2 deletions(-)
fs/smb/server/transport_tcp.c +15 -2
index 5a2aad35739d..1183bb75aa24 100644
@@ -437,9 +437,22 @@ static int ksmbd_tcp_writev(struct ksmbd_transport *t,
 	struct msghdr smb_msg = {
 		.msg_flags = MSG_NOSIGNAL | tx->msg_flags,
 	};
+	struct socket *sock = TCP_TRANS(t)->sock;
+	int ret;
+
+	iov_iter_kvec(&smb_msg.msg_iter, ITER_SOURCE, tx->iov,
+		      tx->iov_cnt, tx->size);
+	/* The caller holds srv_mutex across the entire frame. */
+	while (iov_iter_count(&smb_msg.msg_iter)) {
+		ret = sock_sendmsg(sock, &smb_msg);
+		if (ret <= 0) {
+			/* Never append another SMB frame to an incomplete one. */
+			kernel_sock_shutdown(sock, SHUT_RDWR);
+			return ret ?: -EIO;
+		}
+	}
 
-	return kernel_sendmsg(TCP_TRANS(t)->sock, &smb_msg, tx->iov,
-			      tx->iov_cnt, tx->size);
+	return tx->size;
 }
 
 static void ksmbd_tcp_disconnect(struct ksmbd_transport *t)
Signature · 1 line
2.43.0
author
#7 lore raw

RFC PATCH 4/4 ksmbd: send READ payloads with MSG_SPLICE_PAGES

In reply to Huiwen He

From: Huiwen He <[email protected]>

The TCP send path copies READ payloads that ksmbd has already read into its response buffer, adding CPU and memory bandwidth overhead.

Use page-backed READ buffers and MSG_SPLICE_PAGES to avoid this copy. Keep the existing copy path as a fallback when page-backed sending is not applicable.

Tested with fio on a 128-CPU server with a local 4-vCPU client VM (tmpfs, 16 jobs, QD32 per job, libaio, direct=0, cache=none; mixed=50/50 R/W). Throughput (MiB/s) and paired changes are medians of four runs.

A: read_splice=0; B: read_splice=1.

  Block  Ch  Workload      A MiB/s    B MiB/s  B/A change
  -----  --  ---------  ---------  ---------  ----------
  4KiB    1  READ          290.73     282.21      -2.94%
  4KiB    1  WRITE         193.01     192.84      +1.17%
  4KiB    1  mixed         238.67     240.15      +0.55%
  4KiB    2  READ          404.46     404.66      +1.17%
  4KiB    2  WRITE         334.67     342.19      +0.34%
  4KiB    2  mixed         338.24     336.34      -0.38%
  4KiB    4  READ          453.28     456.10      -0.87%
  4KiB    4  WRITE         415.69     417.24      +0.37%
  4KiB    4  mixed         396.75     386.93      -1.41%
  64KiB   1  READ         2162.74    2558.10     +19.56%
  64KiB   1  WRITE        1976.37    2031.92      +1.26%
  64KiB   1  mixed        1974.08    2176.16     +10.13%
  64KiB   2  READ         3246.35    3591.95     +10.58%
  64KiB   2  WRITE        3222.85    3205.70      -0.66%
  64KiB   2  mixed        3283.75    3317.22      +0.86%
  64KiB   4  READ         4227.13    4427.29      +5.01%
  64KiB   4  WRITE        3089.17    3047.30      -1.36%
  64KiB   4  mixed        3877.03    3962.34      +2.09%
  1MiB    1  READ         2126.23    3353.03     +59.40%
  1MiB    1  WRITE        3452.84    3460.33      +0.25%
  1MiB    1  mixed        2961.50    3096.88      +4.64%
  1MiB    2  READ         3887.33    5546.88     +42.10%
  1MiB    2  WRITE        4158.22    4174.43      +0.18%
  1MiB    2  mixed        4335.01    4954.98     +18.71%
  1MiB    4  READ         6707.91    8311.51     +23.28%
  1MiB    4  WRITE        3557.26    3612.88      +0.38%
  1MiB    4  mixed        5444.00    5604.89      +3.12%
---
 fs/smb/server/connection.c    |   1 +
 fs/smb/server/connection.h    |   2 +
 fs/smb/server/ksmbd_work.c    |  88 ++++++++++++++++++-------
 fs/smb/server/ksmbd_work.h    |   9 +++
 fs/smb/server/server.c        |   1 +
 fs/smb/server/smb2pdu.c       | 120 ++++++++++++++++++++++++++++------
 fs/smb/server/smb2pdu.h       |   1 +
 fs/smb/server/transport_tcp.c |  61 +++++++++++++++--
 8 files changed, 235 insertions(+), 48 deletions(-)
fs/smb/server/connection.c +1 -0
index 4cb92d6599ee..4eb59d81f966 100644
@@ -518,6 +518,7 @@ static int __ksmbd_conn_write(struct ksmbd_work *work,
 	tx->iov = work->iov;
 	tx->iov_cnt = work->iov_cnt;
 	tx->size = get_rfc1002_len(work->iov[0].iov_base) + 4;
+	tx->splice_header_len = work->splice_header_len;
 	tx->need_invalidate_rkey = work->need_invalidate_rkey;
 	tx->remote_key = work->remote_key;
 
fs/smb/server/connection.h +2 -0
index a92e0fea0f06..27a8b17bcbb7 100644
@@ -166,6 +166,8 @@ struct ksmbd_transport_write {
 	struct kvec	*iov;
 	int		iov_cnt;
 	int		size;
+	/* Prefix to copy into a page fragment before the page-backed last iov. */
+	unsigned int	splice_header_len;
 	bool		need_invalidate_rkey;
 	unsigned int	remote_key;
 	int		msg_flags;
fs/smb/server/ksmbd_work.c +65 -23

128 lines not on this page · raw message

fs/smb/server/ksmbd_work.h +9 -0
index 9f87b6c4393a..26b3d57a8429 100644
@@ -13,6 +13,7 @@ struct ksmbd_conn;
 struct ksmbd_session;
 struct ksmbd_tree_connect;
 struct ksmbd_file;
+struct page;
 
 #define KSMBD_WORK_INLINE_IOVS	4
 
@@ -24,6 +25,8 @@ enum {
 
 struct aux_read {
 	void *buf;
+	/* Non-NULL for a buffer owned through a compound-page reference. */
+	struct page *page;
 	struct list_head entry;
 };
 
@@ -45,6 +48,8 @@ struct ksmbd_work {
 	int				iov_alloc_cnt;
 	int				iov_cnt;
 	int				iov_idx;
+	/* Final response prefix before a page-backed last iov; zero disables splice. */
+	unsigned int		splice_header_len;
 	struct kvec			iov_inline[KSMBD_WORK_INLINE_IOVS];
 
 	/* Next cmd hdr in compound req buf*/
@@ -149,6 +154,8 @@ static inline void *ksmbd_req_buf_next(struct ksmbd_work *work)
 
 struct ksmbd_work *ksmbd_alloc_work_struct(void);
 void ksmbd_free_work_struct(struct ksmbd_work *work);
+struct aux_read *ksmbd_alloc_read_buf(size_t length, bool allow_splice);
+void ksmbd_free_read_buf(struct aux_read *ar);
 
 void ksmbd_work_pool_destroy(void);
 int ksmbd_work_pool_init(void);
@@ -159,6 +166,8 @@ bool ksmbd_queue_work(struct ksmbd_work *work);
 extern bool ksmbd_write_wq_enabled;
 bool ksmbd_queue_request(struct ksmbd_work *work, bool use_write_wq);
 int ksmbd_iov_pin_rsp_read(struct ksmbd_work *work, void *ib, int len,
+			   struct aux_read *ar, unsigned int aux_size);
+int ksmbd_iov_pin_rsp_data(struct ksmbd_work *work, void *ib, int len,
 			   void *aux_buf, unsigned int aux_size);
 int ksmbd_iov_pin_rsp(struct ksmbd_work *work, void *ib, int len);
 int allocate_interim_rsp_buf(struct ksmbd_work *work);
fs/smb/server/server.c +1 -0
index 5347eba916ed..70cad4e0e8e2 100644
@@ -351,6 +351,7 @@ static void __handle_ksmbd_work(struct ksmbd_work *work,
 	if (work->sess)
 		ksmbd_user_session_put(work->sess);
 
+	smb2_set_read_splice(work);
 	ksmbd_conn_write(work);
 }
 
fs/smb/server/smb2pdu.c +100 -20

213 lines not on this page · raw message

fs/smb/server/smb2pdu.h +1 -0
index 3f08d1ca5a38..27e60f9ff4db 100644
@@ -429,6 +429,7 @@ bool smb3_encryption_negotiated(struct ksmbd_conn *conn);
 /* smb2 misc functions */
 int ksmbd_smb2_check_message(struct ksmbd_work *work);
 void smb2_complete_request_open(struct ksmbd_work *work);
+void smb2_set_read_splice(struct ksmbd_work *work);
 
 /* smb2 command handlers */
 int smb2_handle_negotiate(struct ksmbd_work *work);
fs/smb/server/transport_tcp.c +56 -5

107 lines not on this page · raw message

Signature · 1 line
2.43.0
maintainer
#8 lore raw

Re: [RFC PATCH 4/4] ksmbd: send READ payloads with MSG_SPLICE_PAGES

Quoted 10 lines
fs/smb/server/ksmbd_work.c +6 -0
@@ -35,6 +35,47 @@ bool ksmbd_queue_request(struct ksmbd_work *work, bool use_write_wq)
        return queue_work(wq, &work->work);
 }
 
+struct aux_read *ksmbd_alloc_read_buf(size_t length, bool allow_splice)
+{
+       struct aux_read *ar;
+       size_t alloc_size = ALIGN(length, 8);
+
+       ar = kzalloc_obj(*ar, KSMBD_DEFAULT_GFP);

fs/smb/server/ksmbd_work.c bool ksmbd_queue_request(struct ksmbd_work *work, bool use_write_wq) · line 53

Quoted 10 lines
fs/smb/server/ksmbd_work.c +10 -0
+       if (!ar)
+               return NULL;
+
+       if (allow_splice && length) {
+               unsigned int order = get_order(alloc_size);
+
+               /* TCP references to tails must keep the whole allocation alive. */
+               if (order <= MAX_PAGE_ORDER)
+                       ar->page = alloc_pages(GFP_KERNEL | __GFP_COMP |
+                                              __GFP_NORETRY | __GFP_NOWARN, order);

Order-8 and order-10 allocations can easily fail when memory is fragmented. Since MSG_SPLICE_PAGES does not require physically contiguous pages, could we allocate individual pages and use a bvec iterator instead? Please see drivers/nvme/host/tcp.c as an example.

Quoted 29 lines
fs/smb/server/ksmbd_work.c +24 -0
+       }
+       if (ar->page)
+               ar->buf = page_address(ar->page);
+       else
+               ar->buf = kvmalloc(alloc_size, KSMBD_DEFAULT_GFP);
+       if (!ar->buf) {
+               kfree(ar);
+               return NULL;
+       }
+       return ar;
+}
+
 
@@ -47,6 +49,29 @@
 #include "transport_tcp.h"
 #include "compress.h"
 
+/* Switching affects allocation, not ownership of existing READ buffers. */
+static bool read_splice;
+
+static int read_splice_set(const char *val, const struct kernel_param *kp)
+{
+       bool enabled;
+       int ret;
+
+       ret = kstrtobool(val ?: "1", &enabled);
+       if (!ret)
+               WRITE_ONCE(*(bool *)kp->arg, enabled);
+       return ret;

fs/smb/server/ksmbd_work.c line 73

Quoted 10 lines
fs/smb/server/ksmbd_work.c +10 -0
+}
+
+static const struct kernel_param_ops read_splice_ops = {
+       .flags = KERNEL_PARAM_OPS_FL_NOARG,
+       .set = read_splice_set,
+       .get = param_get_bool,
+};
+module_param_cb(read_splice, &read_splice_ops, &read_splice, 0644);
+__MODULE_PARM_TYPE(read_splice, "bool");
+MODULE_PARM_DESC(read_splice, "Page-backed ordinary TCP READ sends");

Can we set enable/disable it via ksmbd.conf also ?

author
#9 lore raw

Re: [RFC PATCH 4/4] ksmbd: send READ payloads with MSG_SPLICE_PAGES

Quoted 4 lines · Namjae Jeon

Order-8 and order-10 allocations can easily fail when memory is fragmented. Since MSG_SPLICE_PAGES does not require physically contiguous pages, could we allocate individual pages and use a bvec iterator instead? Please see drivers/nvme/host/tcp.c as an example.

Yeah, I agree with this. But I tried individual pages with a bvec iterator. It was about 9% slower than the high-order page version. Anyway, it still faster than the original copy-based implementation.

Thanks,
Huiwen