ksmbd: splice READ payloads over TCP
Subsystems (2)
cover ksmbd: splice READ payloads over TCP
From: Huiwen He <[email protected]>
ksmbd reads file data into a private response buffer. TCP then copies the payload into an skb. If the reply must be queued, ksmbd also copies the unsent bytes into a queue buffer.
This series uses MSG_SPLICE_PAGES to avoid those TCP copies for plain, standalone READs: patch 1 splices the initial TCP send from private pages and a header page fragment, and patch 2 retains references to unsent payload folios on the send queue so the send worker can splice them as well.
Summary of fio sequential read results (16 jobs, QD32 per job, SMB3.1.1 tmpfs; each column is relative to the preceding commit):
Channels I/O Throughput change Host Gcycles/GiB change
patch 1 patch 2 patch 1 patch 2
1 4 KiB -0.3% -0.9% +1.8% -1.1%
1 64 KiB +28.4% +0.9% -31.5% -5.9%
1 256 KiB +60.8% +5.3% -40.5% -22.5%
1 1 MiB +51.5% +9.2% -33.1% -32.2%
4 4 KiB +0.2% +0.2% +0.9% +0.2%
4 64 KiB +3.9% +2.6% -6.9% +0.8%
4 256 KiB +12.1% -0.4% -43.0% -0.8%
4 1 MiB +17.6% +7.1% -39.5% -34.7%Huiwen He (2):
ksmbd: send READ payloads with MSG_SPLICE_PAGES
ksmbd: retain unsent READ pages on the TCP send queue
fs/smb/server/connection.c | 18 +++ fs/smb/server/connection.h | 1 + fs/smb/server/ksmbd_work.c | 24 ++-- fs/smb/server/ksmbd_work.h | 8 +- fs/smb/server/smb2pdu.c | 74 +++++++++-- fs/smb/server/transport_tcp.c | 223 ++++++++++++++++++++++++++++++---- fs/smb/server/transport_tcp.h | 1 + 7 files changed, 301 insertions(+), 48 deletions(-)
base-commit: 89d5e8fdd5a74a318d18b7267c4ec5d70b2285f1
Signature · 1 line
2.43.0
PATCH 1/2 ksmbd: send READ payloads with MSG_SPLICE_PAGES
From: Huiwen He <[email protected]>
kernel_read() fills the READ response buffer with file data. TCP then copies the same data into an skb when sending the reply.
For plain, standalone READs, put the payload in private pages and the header in a page fragment. Pass both to TCP with MSG_SPLICE_PAGES so it can attach the pages directly to an skb.
Tested with fio sequential reads (16 jobs, QD32 per job) from a 4-vCPU virtio-net guest. The Xeon Gold 6430 host had 128 logical CPUs and served a tmpfs share over SMB3.1.1.
Channels I/O Throughput (MiB/s) Host Gcycles/GiB
before after change before after change
1 4 KiB 288.3 287.5 -0.3% 27.347 27.850 +1.8%
1 64 KiB 1975.3 2536.1 +28.4% 6.815 4.668 -31.5%
1 256 KiB 2007.2 3228.5 +60.8% 7.515 4.468 -40.5%
1 1 MiB 2058.7 3119.9 +51.5% 5.446 3.646 -33.1%
4 4 KiB 456.2 457.3 +0.2% 23.792 23.997 +0.9%
4 64 KiB 4260.4 4427.3 +3.9% 3.207 2.985 -6.9%
4 256 KiB 6270.3 7026.3 +12.1% 3.418 1.948 -43.0%
4 1 MiB 6660.0 7833.2 +17.6% 3.879 2.347 -39.5%--- fs/smb/server/connection.c | 18 +++++++++ fs/smb/server/connection.h | 1 + fs/smb/server/ksmbd_work.c | 24 +++++++----- fs/smb/server/ksmbd_work.h | 8 +++- fs/smb/server/smb2pdu.c | 74 +++++++++++++++++++++++++++++------ fs/smb/server/transport_tcp.c | 52 ++++++++++++++++++++++-- fs/smb/server/transport_tcp.h | 1 + 7 files changed, 152 insertions(+), 26 deletions(-)
fs/smb/server/connection.c +18 -0
@@ -533,6 +533,23 @@ int ksmbd_conn_wait_idle_sess(struct ksmbd_conn *curr_conn,return 0 ; } +static bool ksmbd_can_splice_read (const struct ksmbd_work *work,int size) +{ +const struct kvec *iov = work->iov; +const struct smb2_read_rsp *rsp; + +/* A credit error or a transform can replace the response iovs. */ +if (!work->splice_read || work->iov_cnt !=3 || + iov[0 ].iov_len !=sizeof (__be32) || + iov[1 ].iov_len != offsetof(struct smb2_read_rsp , Buffer)) +return false; + + rsp = iov[1 ].iov_base; +return !(rsp->hdr.Flags &SMB2_FLAGS_SIGNED ) && + iov[2 ].iov_len ==le32_to_cpu (rsp->DataLength) && + iov[0 ].iov_len + iov[1 ].iov_len + iov[2 ].iov_len == size; +} +static int __ksmbd_conn_write (struct ksmbd_work *work,struct ksmbd_transport_write *tx) {
@@ -553,6 +570,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_read =ksmbd_can_splice_read (work, tx->size); tx->need_invalidate_rkey = work->need_invalidate_rkey; tx->remote_key = work->remote_key;
fs/smb/server/connection.h +1 -0
@@ -175,6 +175,7 @@ struct ksmbd_transport_write {struct kvec *iov;int iov_cnt;int size; +bool splice_read;bool need_invalidate_rkey;unsigned int remote_key;int msg_flags;
fs/smb/server/ksmbd_work.c +14 -10
@@ -65,6 +65,14 @@ struct ksmbd_work *ksmbd_alloc_work_struct(void)return work; } +void ksmbd_free_read_buf (void *buf,struct folio *folio) +{ +if (folio) +folio_put (folio); +else +kvfree (buf); +} +void ksmbd_free_work_struct (struct ksmbd_work *work) {struct aux_read *ar, *tmp;
@@ -74,7 +82,7 @@ void ksmbd_free_work_struct(struct ksmbd_work *work)kvfree (work->response_buf);list_for_each_entry_safe (ar, tmp, &work->aux_read_list, entry) { -kvfree (ar->buf); +ksmbd_free_read_buf (ar->buf, ar->folio);list_del (&ar->entry);kfree (ar); }
@@ -151,8 +159,9 @@ static inline void __ksmbd_iov_pin(struct ksmbd_work *work, void *ib, work->iov_cnt++; } -static int __ksmbd_iov_pin_rsp (struct ksmbd_work *work,void *ib,int len, -void *aux_buf,unsigned int aux_size) +int ksmbd_iov_pin_rsp_read (struct ksmbd_work *work,void *ib,int len, +void *aux_buf,unsigned int aux_size, +struct folio *folio) {struct aux_read *ar =NULL ;int need_iov_cnt =1 ;
@@ -185,6 +194,7 @@ static int __ksmbd_iov_pin_rsp(struct ksmbd_work *work, void *ib, int len,inc_rfc1001_len (work->iov[0 ].iov_base, aux_size); ar->buf = aux_buf; + ar->folio = folio;list_add (&ar->entry, &work->aux_read_list); }
@@ -193,13 +203,7 @@ static int __ksmbd_iov_pin_rsp(struct ksmbd_work *work, void *ib, int len,int ksmbd_iov_pin_rsp (struct ksmbd_work *work,void *ib,int len) { -return __ksmbd_iov_pin_rsp (work, ib, len,NULL ,0 ); -} - -int ksmbd_iov_pin_rsp_read (struct ksmbd_work *work,void *ib,int len, -void *aux_buf,unsigned int aux_size) -{ -return __ksmbd_iov_pin_rsp (work, ib, len, aux_buf, aux_size); +return ksmbd_iov_pin_rsp_read (work, ib, len,NULL ,0 ,NULL ); }int allocate_interim_rsp_buf (struct ksmbd_work *work)
fs/smb/server/ksmbd_work.h +7 -1
@@ -13,6 +13,7 @@ struct ksmbd_conn;struct ksmbd_session ;struct ksmbd_tree_connect ;struct ksmbd_file ; +struct folio ;#define KSMBD_WORK_INLINE_IOVS 4
@@ -24,6 +25,7 @@ enum {struct aux_read {void *buf; +struct folio *folio;struct list_head entry; };
@@ -93,6 +95,8 @@ struct ksmbd_work { bool encrypted:1;/* READ response should be wrapped in a compression transform. */ bool compress_response:1; +/* Standalone READ payload is backed by spliceable pages. */ + bool splice_read:1;/* Is this SYNC or ASYNC ksmbd_work */ bool asynchronous:1; bool need_invalidate_rkey:1;
@@ -166,7 +170,9 @@ bool ksmbd_queue_work(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, -void *aux_buf,unsigned int aux_size); +void *aux_buf,unsigned int aux_size, +struct folio *folio); +void ksmbd_free_read_buf (void *buf,struct folio *folio);int ksmbd_iov_pin_rsp (struct ksmbd_work *work,void *ib,int len);int allocate_interim_rsp_buf (struct ksmbd_work *work);#endif /* __KSMBD_WORK_H__ */
fs/smb/server/smb2pdu.c +63 -11
149 lines not on this page · raw message
fs/smb/server/transport_tcp.c +48 -4
93 lines not on this page · raw message
fs/smb/server/transport_tcp.h +1 -0
@@ -7,6 +7,7 @@#define __KSMBD_TRANSPORT_TCP_H__int ksmbd_tcp_set_interfaces (char *ifc_list,int ifc_list_sz); +bool ksmbd_transport_is_tcp (struct ksmbd_transport *t);struct interface *ksmbd_find_netdev_name_iface_list (char *netdev_name);void ksmbd_free_transport (struct ksmbd_transport *kt);int ksmbd_tcp_init (void );
Signature · 1 line
2.43.0
PATCH 2/2 ksmbd: retain unsent READ pages on the TCP send queue
In reply to Huiwen HeFrom: Huiwen He <[email protected]>
When TCP accepts only part of a READ reply or an earlier reply is already queued, ksmbd copies the unsent bytes into a queue entry. TCP copies the payload again when the send worker runs.
Keep references to the unsent payload folios and store any remaining header bytes in the queue entry. The worker sends the header first, then passes the pages to TCP with MSG_SPLICE_PAGES. Charge each folio in full against the queue limit and copy small tails that would keep a large folio pinned.
Tested with fio sequential reads (16 jobs, QD32 per job) from a 4-vCPU virtio-net guest. The Xeon Gold 6430 host had 128 logical CPUs and served a tmpfs share over SMB3.1.1. The "before" column includes "ksmbd: send READ payloads with MSG_SPLICE_PAGES".
Channels I/O Throughput (MiB/s) Host Gcycles/GiB
before after change before after change
1 4 KiB 284.2 281.6 -0.9% 28.576 28.265 -1.1%
1 64 KiB 2501.8 2524.8 +0.9% 4.552 4.284 -5.9%
1 256 KiB 3243.4 3414.6 +5.3% 4.511 3.497 -22.5%
1 1 MiB 3145.6 3435.5 +9.2% 3.578 2.425 -32.2%
4 4 KiB 454.2 454.9 +0.2% 24.338 24.377 +0.2%
4 64 KiB 4337.1 4448.3 +2.6% 2.977 3.000 +0.8%
4 256 KiB 6969.5 6938.8 -0.4% 1.932 1.916 -0.8%
4 1 MiB 7705.3 8255.6 +7.1% 2.673 1.746 -34.7%--- fs/smb/server/transport_tcp.c | 171 +++++++++++++++++++++++++++++----- 1 file changed, 149 insertions(+), 22 deletions(-)
fs/smb/server/transport_tcp.c +149 -22
@@ -4,9 +4,11 @@* Copyright (C) 2018 Samsung Electronics Co., Ltd. */ +#include <linux/bvec.h> #include <linux/freezer.h> #include <linux/page_frag_cache.h> #include <linux/uio.h> +#include <linux/vmalloc.h> #include "smb_common.h" #include "server.h"
@@ -35,9 +37,15 @@ struct tcp_tx {struct list_head list;size_t len;size_t offset; +size_t charge;unsigned long deadline;int msg_flags; -u8 data[]; +unsigned short nr_vecs; +unsigned short header_len; +union { + DECLARE_FLEX_ARRAY(u8, data); + DECLARE_FLEX_ARRAY(struct bio_vec, bvec); + }; };struct tcp_transport {
@@ -478,12 +486,104 @@ static int ksmbd_tcp_read(struct ksmbd_transport *t, char *buf,return ksmbd_tcp_readv (TCP_TRANS (t), &iov,1 , to_read, max_retries); } -static bool ksmbd_tcp_tx_has_space (struct tcp_transport *t,size_t size) +static bool ksmbd_tcp_tx_has_space (struct tcp_transport *t,size_t pdu_size, +size_t charge) { -size_t limit =max_t (size_t,READ_ONCE (t->sock->sk->sk_sndbuf), size); +size_t limit =max_t (size_t,READ_ONCE (t->sock->sk->sk_sndbuf), pdu_size);/* Allow one large response, but bound the retained response buffers. */ -return READ_ONCE (t->tx_queued) <= limit - size; +return charge <= limit &&READ_ONCE (t->tx_queued) <= limit - charge; +} + +static void *ksmbd_tcp_tx_header (struct tcp_tx *msg) +{ +return msg->bvec + msg->nr_vecs; +} + +static void ksmbd_tcp_free_tx (struct tcp_tx *msg) +{ +unsigned int i; + +for (i =0 ; i < msg->nr_vecs; i++) +folio_put (bvec_folio (&msg->bvec[i])); +kvfree (msg); +} + +static struct tcp_tx *ksmbd_tcp_alloc_read_tx (struct tcp_transport *t, +const struct ksmbd_transport_write *tx, +size_t offset) +{ +struct kvec payload = tx->iov[2 ]; +size_t remaining = tx->size - offset; +size_t payload_left =min (remaining, payload.iov_len); +size_t header_left = remaining - payload_left; +size_t charge = header_left; +struct tcp_tx *msg; +struct iov_iter iter; +unsigned int i, max_vecs; +unsigned short nr =0 ; +bool vmapped; + +if (payload_left <PAGE_SIZE ) +return NULL ; + payload.iov_base += payload.iov_len - payload_left; + payload.iov_len = payload_left; + vmapped =is_vmalloc_addr (payload.iov_base); +if (vmapped) { + max_vecs =DIV_ROUND_UP (offset_in_page (payload.iov_base) + payload_left, +PAGE_SIZE ); +if (max_vecs >USHRT_MAX ) +return NULL ; + }else { +struct folio *folio =virt_to_folio (payload.iov_base); + +/* A small unsent tail must not keep a much larger folio alive. */ + charge +=folio_size (folio); +if (get_order (payload_left) <folio_order (folio) || + !ksmbd_tcp_tx_has_space (t, tx->size, charge)) +return NULL ; + max_vecs =1 ; + } + +/* The unsent header follows the payload vectors in the queue entry. */ + msg =kvmalloc (size_add (struct_size (msg, bvec, max_vecs), header_left), +KSMBD_DEFAULT_GFP ); +if (!msg) +return NULL ; +if (vmapped) { +iov_iter_kvec (&iter,ITER_SOURCE , &payload,1 , payload_left); +if (iov_iter_extract_bvecs (&iter, msg->bvec, payload_left, &nr, + max_vecs,0 ,0 ) != payload_left) + goto fail; +for (i =0 ; i < nr; i++) { +struct folio *folio =bvec_folio (&msg->bvec[i]); + +if (get_order (msg->bvec[i].bv_len) <folio_order (folio)) + goto fail; + charge +=folio_size (folio); + } +if (!ksmbd_tcp_tx_has_space (t, tx->size, charge)) + goto fail; + }else { +bvec_set_virt (&msg->bvec[0 ], payload.iov_base, payload_left); + nr =1 ; + } + + msg->nr_vecs = nr; +if (header_left) { +iov_iter_kvec (&iter,ITER_SOURCE , tx->iov,2 , tx->size - tx->iov[2 ].iov_len); +iov_iter_advance (&iter, offset); +if (!copy_from_iter_full (ksmbd_tcp_tx_header (msg), header_left, &iter)) + goto fail; + } +for (i =0 ; i < nr; i++) +folio_get (bvec_folio (&msg->bvec[i])); + msg->header_len = header_left; + msg->charge = charge; +return msg; +fail: +kvfree (msg); +return NULL ; } static struct tcp_tx *ksmbd_tcp_alloc_tx(const struct ksmbd_transport_write *tx,
@@ -503,9 +603,9 @@ static struct tcp_tx *ksmbd_tcp_alloc_tx(const struct ksmbd_transport_write *tx,kvfree (msg);return NULL ; } - msg->len = remaining; - msg->offset =0 ; - msg->msg_flags = tx->msg_flags; + msg->nr_vecs =0 ; + msg->header_len =0 ; + msg->charge = remaining;return msg; }
@@ -515,7 +615,7 @@ static void ksmbd_tcp_purge_tx(struct tcp_transport *t)list_for_each_entry_safe (msg, next, &t->tx_queue, list) {list_del (&msg->list); -kvfree (msg); +ksmbd_tcp_free_tx (msg); }WRITE_ONCE (t->tx_queued,0 ); }
@@ -526,6 +626,33 @@ static bool ksmbd_tcp_send_retry(int ret) ret == -ENOMEM ; } +static int ksmbd_tcp_send_queued (struct tcp_transport *t,struct tcp_tx *msg) +{ +struct msghdr hdr = { + .msg_flags =MSG_NOSIGNAL |MSG_DONTWAIT | msg->msg_flags, + }; +size_t offset = msg->offset; +struct kvec iov; + +if (!msg->nr_vecs) { + iov.iov_base = msg->data; + iov.iov_len = msg->len; + }else if (offset < msg->header_len) { + iov.iov_base =ksmbd_tcp_tx_header (msg); + iov.iov_len = msg->header_len; + hdr.msg_flags = (hdr.msg_flags & ~MSG_EOR ) |MSG_MORE ; + }else { + hdr.msg_flags |=MSG_SPLICE_PAGES ; +iov_iter_bvec (&hdr.msg_iter,ITER_SOURCE , msg->bvec, msg->nr_vecs, + msg->len - msg->header_len); +iov_iter_advance (&hdr.msg_iter, offset - msg->header_len); +return sock_sendmsg (t->sock, &hdr); + } +iov_iter_kvec (&hdr.msg_iter,ITER_SOURCE , &iov,1 , iov.iov_len); +iov_iter_advance (&hdr.msg_iter, offset); +return sock_sendmsg (t->sock, &hdr); +} +static void ksmbd_tcp_tx_work (struct work_struct *work) { struct tcp_transport *t = container_of(to_delayed_work(work),
@@ -538,13 +665,6 @@ static void ksmbd_tcp_tx_work(struct work_struct *work)mutex_lock (&t->tx_lock);while (!READ_ONCE (t->tx_stopped) && !list_empty (&t->tx_queue) && budget--) { struct tcp_tx *msg = list_first_entry(&t->tx_queue, struct tcp_tx, list); -struct msghdr hdr = { - .msg_flags =MSG_NOSIGNAL |MSG_DONTWAIT | msg->msg_flags, - }; -struct kvec iov = { - .iov_base = msg->data + msg->offset, - .iov_len = msg->len - msg->offset, - };/* MSG_DONTWAIT bypasses sk_sndtimeo, including for retained PDUs. */ if (time_after_eq (jiffies, msg->deadline)) {
@@ -552,16 +672,18 @@ static void ksmbd_tcp_tx_work(struct work_struct *work) failed = true;break ; } - ret =kernel_sendmsg (t->sock, &hdr, &iov,1 , iov.iov_len); + ret =ksmbd_tcp_send_queued (t, msg);if (ret >0 ) { msg->offset += ret; t->tx_sent += ret;if (msg->offset == msg->len) { -WRITE_ONCE (t->tx_queued, t->tx_queued - msg->len); +WRITE_ONCE (t->tx_queued, t->tx_queued - msg->charge);list_del (&msg->list); -kvfree (msg); +ksmbd_tcp_free_tx (msg);continue ; } +if (msg->offset == msg->header_len) +continue ; }else if (!ksmbd_tcp_send_retry (ret)) {WRITE_ONCE (t->tx_stopped, true); failed = true;
@@ -682,7 +804,7 @@ static int ksmbd_tcp_writev(struct ksmbd_transport *t,mutex_unlock (&tt->tx_lock);return -EPIPE ; } -if (ksmbd_tcp_tx_has_space (tt, tx->size)) +if (ksmbd_tcp_tx_has_space (tt, tx->size, tx->size))break ;mutex_unlock (&tt->tx_lock);if (tx->msg_flags &MSG_DONTWAIT )
@@ -693,7 +815,7 @@ static int ksmbd_tcp_writev(struct ksmbd_transport *t, READ_ONCE(tt->tx_stopped) || ksmbd_conn_exiting(t->conn) || ksmbd_conn_releasing(t->conn) || - ksmbd_tcp_tx_has_space(tt, tx->size), + ksmbd_tcp_tx_has_space(tt, tx->size, tx->size), timeout)) {/* A queued PDU may already have a prefix in the socket. */ ksmbd_conn_abort (t->conn);
@@ -719,7 +841,9 @@ static int ksmbd_tcp_writev(struct ksmbd_transport *t, }/* The request buffers can be freed as soon as writev returns. */ - msg =ksmbd_tcp_alloc_tx (tx, sent); + msg = tx->splice_read ?ksmbd_tcp_alloc_read_tx (tt, tx, sent) :NULL ; +if (!msg) + msg =ksmbd_tcp_alloc_tx (tx, sent);if (!msg) {if (sent)WRITE_ONCE (tt->tx_stopped, true);
@@ -728,9 +852,12 @@ static int ksmbd_tcp_writev(struct ksmbd_transport *t,ksmbd_conn_abort (t->conn);return -ENOMEM ; } + msg->len = tx->size - sent; + msg->offset =0 ; + msg->msg_flags = tx->msg_flags; msg->deadline = deadline;list_add_tail (&msg->list, &tt->tx_queue); -WRITE_ONCE (tt->tx_queued, tt->tx_queued + msg->len); +WRITE_ONCE (tt->tx_queued, tt->tx_queued + msg->charge);queue_delayed_work (system_dfl_wq, &tt->tx_work,0 ); accepted:
Signature · 1 line
2.43.0