Skip to contentSkip to conversation

smb/server: improve async interim response handling for compound requests

PATCH Applied ChenXiaoSong 2 participants 5 messages Jul 8

Subsystems (3)
lore
Share & tools
lore

ChenXiaoSong

author reviewer
#0 lore raw

v2 cover smb/server: improve async interim response handling for compound requests

From: ChenXiaoSong <[email protected]>

v1->v2: send compound prefix before async pending response,
then send STATUS_PENDING as a standalone response.

v1: https://lore.kernel.org/linux-cifs/[email protected]/

I am currently implementing the ksmbd change notify feature, and I have encountered the following issue.

Before this patchset, no CREATE response:

Windows client compound request:
CREATE dir; CHANGE_NOTIFY, fid = compound fid

Server response:
CHANGE_NOTIFY STATUS_PENDING, AsyncId = N

After this patchset:

Server response:
CREATE dir SUCCESS, fid = X
CHANGE_NOTIFY STATUS_PENDING, AsyncId = N

ChenXiaoSong (3):
smb/server: send compound prefix before async pending response smb/server: introduce struct ksmbd_transport_write smb/server: use MSG_EOR for async interim response

 fs/smb/server/connection.c     | 31 +++++++++++++++++-----
 fs/smb/server/connection.h     | 15 ++++++++---
 fs/smb/server/smb2pdu.c        | 47 +++++++++++++++++++++++++++++++++-
 fs/smb/server/transport_rdma.c |  9 ++++---
 fs/smb/server/transport_tcp.c  | 13 +++++-----
 5 files changed, 95 insertions(+), 20 deletions(-)
Signature · 1 line
2.54.0
author reviewer
#1 lore raw

PATCH v2 1/3 smb/server: send compound prefix before async pending response

From: ChenXiaoSong <[email protected]>

When the last request in a compound request becomes async, ksmbd sends a STATUS_PENDING response for it. But the responses for previous requests in the same compound request are still kept in the same response buffer.

Send these previous responses first. Clear NextCommand for the last response in this part, sign it again if needed, and reset the iov state. After that, the async request sends STATUS_PENDING first, and sends the real response later. Both are separate responses.

Example:

  smbtorture //${server_ip}/export -U${username}%${password} smb2.compound_async.write_write

Client request:

Write Request Len:64 Off:0, File: compound_async_write_write; Write Request Len:64 Off:64

Before this patch, STATUS_PENDING Write Response is the first of several responses:

Write Response, Error: STATUS_PENDING
Write Response, File: compound_async_write_write; Write Response

But STATUS_PENDING Write Response should be in the middle of several responses, after this patch:

Write Response, File: compound_async_write_write Write Response
SMB2, STATUS_PENDING, Write Response, MessageId 7 SMB2, Write Response, MessageId 7

---
 fs/smb/server/smb2pdu.c | 45 +++++++++++++++++++++++++++++++++++++++++
 1 file changed, 45 insertions(+)
fs/smb/server/smb2pdu.c +45 -0
index 0ab05a8e575e..35f8476bbb6d 100644
@@ -839,6 +839,48 @@ void release_async_work(struct ksmbd_work *work)
 	}
 }
 
+static void smb2_send_interim_compound_prefix(struct ksmbd_work *work)
+{
+	struct smb2_hdr *req_hdr;
+	struct smb2_hdr *rsp_hdr;
+	int err;
+
+	if (!work->next_smb2_rcv_hdr_off ||
+	    !work->next_smb2_rsp_hdr_off ||
+	    work->curr_smb2_rsp_hdr_off == work->next_smb2_rsp_hdr_off ||
+	    !work->iov_idx)
+		return;
+
+	req_hdr = ksmbd_req_buf_next(work);
+	/* Detach only the final async command from the completed prefix. */
+	if (req_hdr->NextCommand)
+		return;
+
+	/*
+	 * The responses before the async command are sent as a standalone
+	 * compound response. The last response in this prefix must terminate
+	 * the chain.
+	 */
+	rsp_hdr = ksmbd_resp_buf_curr(work);
+	rsp_hdr->NextCommand = 0;
+	if ((rsp_hdr->Flags & SMB2_FLAGS_SIGNED) && work->sess &&
+	    work->conn->ops->set_sign_rsp)
+		work->conn->ops->set_sign_rsp(work);
+
+	err = ksmbd_conn_write(work);
+	if (err)
+		ksmbd_debug(SMB, "failed to send compound interim prefix: %d\n",
+			    err);
+
+	work->iov_idx = 0;
+	work->iov_cnt = 0;
+	work->curr_smb2_rsp_hdr_off = work->next_smb2_rsp_hdr_off;
+	*(__be32 *)work->response_buf = 0;
+
+	rsp_hdr = ksmbd_resp_buf_next(work);
+	rsp_hdr->Flags &= ~SMB2_FLAGS_RELATED_OPERATIONS;
+}
+
 void smb2_send_interim_resp(struct ksmbd_work *work, __le32 status)
 {
 	struct smb2_hdr *rsp_hdr;
@@ -853,6 +895,9 @@ void smb2_send_interim_resp(struct ksmbd_work *work, __le32 status)
 		return;
 	}
 
+	if (status == STATUS_PENDING)
+		smb2_send_interim_compound_prefix(work);
+
 	in_work->conn = work->conn;
 	memcpy(smb_get_msg(in_work->response_buf), ksmbd_resp_buf_next(work),
 	       __SMB2_HEADER_STRUCTURE_SIZE);
Signature · 1 line
2.54.0
author reviewer
#2 lore raw

PATCH v2 2/3 smb/server: introduce struct ksmbd_transport_write

In reply to ChenXiaoSong

From: ChenXiaoSong <[email protected]>

Put the arguments of ksmbd_transport_ops ->writev() into a struct. This makes the function call shorter and easier to read.

Add __ksmbd_conn_write() for the common write code. A later patch will use it for another write helper.

No functional change.

---
 fs/smb/server/connection.c     | 22 ++++++++++++++++------
 fs/smb/server/connection.h     | 13 ++++++++++---
 fs/smb/server/transport_rdma.c |  9 +++++----
 fs/smb/server/transport_tcp.c  | 13 +++++++------
 4 files changed, 38 insertions(+), 19 deletions(-)
fs/smb/server/connection.c +16 -6
index dee8e4aced99..d5b35087556e 100644
@@ -350,7 +350,8 @@ int ksmbd_conn_wait_idle_sess_id(struct ksmbd_conn *curr_conn, u64 sess_id)
 	return 0;
 }
 
-int ksmbd_conn_write(struct ksmbd_work *work)
+static int __ksmbd_conn_write(struct ksmbd_work *work,
+			      struct ksmbd_transport_write *write)
 {
 	struct ksmbd_conn *conn = work->conn;
 	int sent;
@@ -366,12 +367,14 @@ int ksmbd_conn_write(struct ksmbd_work *work)
 	if (!work->iov_idx)
 		return -EINVAL;
 
+	write->iov = work->iov;
+	write->iov_cnt = work->iov_cnt;
+	write->size = get_rfc1002_len(work->iov[0].iov_base) + 4;
+	write->need_invalidate_rkey = work->need_invalidate_rkey;
+	write->remote_key = work->remote_key;
+
 	ksmbd_conn_lock(conn);
-	sent = conn->transport->ops->writev(conn->transport, work->iov,
-			work->iov_cnt,
-			get_rfc1002_len(work->iov[0].iov_base) + 4,
-			work->need_invalidate_rkey,
-			work->remote_key);
+	sent = conn->transport->ops->writev(conn->transport, write);
 	ksmbd_conn_unlock(conn);
 
 	if (sent < 0) {
@@ -382,6 +385,13 @@ int ksmbd_conn_write(struct ksmbd_work *work)
 	return 0;
 }
 
+int ksmbd_conn_write(struct ksmbd_work *work)
+{
+	struct ksmbd_transport_write write = {};
+
+	return __ksmbd_conn_write(work, &write);
+}
+
 int ksmbd_conn_rdma_read(struct ksmbd_conn *conn,
 			 void *buf, unsigned int buflen,
 			 struct smbdirect_buffer_descriptor_v1 *desc,
fs/smb/server/connection.h +10 -3
index ec75633b7da0..62df151ca554 100644
@@ -132,14 +132,21 @@ struct ksmbd_conn_ops {
 	int	(*terminate_fn)(struct ksmbd_conn *conn);
 };
 
+struct ksmbd_transport_write {
+	struct kvec	*iov;
+	int		iov_cnt;
+	int		size;
+	bool		need_invalidate_rkey;
+	unsigned int	remote_key;
+};
+
 struct ksmbd_transport_ops {
 	void (*disconnect)(struct ksmbd_transport *t);
 	void (*shutdown)(struct ksmbd_transport *t);
 	int (*read)(struct ksmbd_transport *t, char *buf,
 		    unsigned int size, int max_retries);
-	int (*writev)(struct ksmbd_transport *t, struct kvec *iovs, int niov,
-		      int size, bool need_invalidate_rkey,
-		      unsigned int remote_key);
+	int (*writev)(struct ksmbd_transport *t,
+		      const struct ksmbd_transport_write *write);
 	int (*rdma_read)(struct ksmbd_transport *t,
 			 void *buf, unsigned int len,
 			 struct smbdirect_buffer_descriptor_v1 *desc,
fs/smb/server/transport_rdma.c +5 -4
index b6d63ff8a8a3..b2a882421823 100644
@@ -239,17 +239,18 @@ static int smb_direct_read(struct ksmbd_transport *t, char *buf,
 }
 
 static int smb_direct_writev(struct ksmbd_transport *t,
-			     struct kvec *iov, int niovs, int buflen,
-			     bool need_invalidate, unsigned int remote_key)
+			     const struct ksmbd_transport_write *write)
 {
 	struct smb_direct_transport *st = SMBD_TRANS(t);
 	struct smbdirect_socket *sc = st->socket;
 	struct iov_iter iter;
 
-	iov_iter_kvec(&iter, ITER_SOURCE, iov, niovs, buflen);
+	iov_iter_kvec(&iter, ITER_SOURCE, write->iov, write->iov_cnt,
+		      write->size);
 
 	return smbdirect_connection_send_iter(sc, &iter, 0,
-					      need_invalidate, remote_key);
+					      write->need_invalidate_rkey,
+					      write->remote_key);
 }
 
 static int smb_direct_rdma_write(struct ksmbd_transport *t,
fs/smb/server/transport_tcp.c +7 -6
index 13b711ea575d..448f24d44b6b 100644
@@ -417,14 +417,15 @@ 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 int ksmbd_tcp_writev(struct ksmbd_transport *t, struct kvec *iov,
-			    int nvecs, int size, bool need_invalidate,
-			    unsigned int remote_key)
-
+static int ksmbd_tcp_writev(struct ksmbd_transport *t,
+			    const struct ksmbd_transport_write *write)
 {
-	struct msghdr smb_msg = {.msg_flags = MSG_NOSIGNAL};
+	struct msghdr smb_msg = {
+		.msg_flags = MSG_NOSIGNAL,
+	};
 
-	return kernel_sendmsg(TCP_TRANS(t)->sock, &smb_msg, iov, nvecs, size);
+	return kernel_sendmsg(TCP_TRANS(t)->sock, &smb_msg, write->iov,
+			      write->iov_cnt, write->size);
 }
 
 static void ksmbd_tcp_disconnect(struct ksmbd_transport *t)
Signature · 1 line
2.54.0
author reviewer
#3 lore raw

PATCH v2 3/3 smb/server: use MSG_EOR for async interim response

In reply to ChenXiaoSong

From: ChenXiaoSong <[email protected]>

Two kernel_sendmsg() calls can still use the same TCP skb if the first skb can take more data. This can happen when ksmbd sends two SMB2 responses very close to each other.

Without MSG_EOR, TCP can append the next sendmsg data to the previous skb. Then STATUS_PENDING and the later response can be put into the same TCP skb. MSG_EOR marks the skb as end of record, so TCP will not collapse the next sendmsg data into it.

Example:

  smbtorture //${server_ip}/export -U${username}%${password} smb2.compound_async.write_write

Client request:

Write Request Len:64 Off:0, File: compound_async_write_write; Write Request Len:64 Off:64

Before this patch, server responses:

Write Response, File: compound_async_write_write Write Response
SMB2, STATUS_PENDING, Write Response, MessageId 7 SMB2, Write Response, MessageId 7

After this patch:

Write Response, File: compound_async_write_write Write Response, Error: STATUS_PENDING Write Response

---
 fs/smb/server/connection.c    | 9 +++++++++
 fs/smb/server/connection.h    | 2 ++
 fs/smb/server/smb2pdu.c       | 4 ++--
 fs/smb/server/transport_tcp.c | 2 +-
 4 files changed, 14 insertions(+), 3 deletions(-)
fs/smb/server/connection.c +9 -0
index d5b35087556e..89746284fd7b 100644
@@ -392,6 +392,15 @@ int ksmbd_conn_write(struct ksmbd_work *work)
 	return __ksmbd_conn_write(work, &write);
 }
 
+int ksmbd_conn_write_eor(struct ksmbd_work *work)
+{
+	struct ksmbd_transport_write write = {
+		.msg_flags = MSG_EOR,
+	};
+
+	return __ksmbd_conn_write(work, &write);
+}
+
 int ksmbd_conn_rdma_read(struct ksmbd_conn *conn,
 			 void *buf, unsigned int buflen,
 			 struct smbdirect_buffer_descriptor_v1 *desc,
fs/smb/server/connection.h +2 -0
index 62df151ca554..fa09d37d5745 100644
@@ -138,6 +138,7 @@ struct ksmbd_transport_write {
 	int		size;
 	bool		need_invalidate_rkey;
 	unsigned int	remote_key;
+	int		msg_flags;
 };
 
 struct ksmbd_transport_ops {
@@ -182,6 +183,7 @@ int ksmbd_conn_wq_init(void);
 void ksmbd_conn_wq_destroy(void);
 bool ksmbd_conn_lookup_dialect(struct ksmbd_conn *c);
 int ksmbd_conn_write(struct ksmbd_work *work);
+int ksmbd_conn_write_eor(struct ksmbd_work *work);
 int ksmbd_conn_rdma_read(struct ksmbd_conn *conn,
 			 void *buf, unsigned int buflen,
 			 struct smbdirect_buffer_descriptor_v1 *desc,
fs/smb/server/smb2pdu.c +2 -2
index 35f8476bbb6d..d644134d50f0 100644
@@ -867,7 +867,7 @@ static void smb2_send_interim_compound_prefix(struct ksmbd_work *work)
 	    work->conn->ops->set_sign_rsp)
 		work->conn->ops->set_sign_rsp(work);
 
-	err = ksmbd_conn_write(work);
+	err = ksmbd_conn_write_eor(work);
 	if (err)
 		ksmbd_debug(SMB, "failed to send compound interim prefix: %d\n",
 			    err);
@@ -908,7 +908,7 @@ void smb2_send_interim_resp(struct ksmbd_work *work, __le32 status)
 	smb2_set_err_rsp(in_work);
 	rsp_hdr->Status = status;
 
-	ksmbd_conn_write(in_work);
+	ksmbd_conn_write_eor(in_work);
 	ksmbd_free_work_struct(in_work);
 }
 
fs/smb/server/transport_tcp.c +1 -1
index 448f24d44b6b..cf81585d6861 100644
@@ -421,7 +421,7 @@ static int ksmbd_tcp_writev(struct ksmbd_transport *t,
 			    const struct ksmbd_transport_write *write)
 {
 	struct msghdr smb_msg = {
-		.msg_flags = MSG_NOSIGNAL,
+		.msg_flags = MSG_NOSIGNAL | write->msg_flags,
 	};
 
 	return kernel_sendmsg(TCP_TRANS(t)->sock, &smb_msg, write->iov,
Signature · 1 line
2.54.0
maintainer Applied
#4 lore raw
In reply to ChenXiaoSong
Quoted 21 lines · ChenXiaoSong

From: ChenXiaoSong <[email protected]>

v1->v2: send compound prefix before async pending response,
then send STATUS_PENDING as a standalone response.

v1: https://lore.kernel.org/linux-cifs/[email protected]/

I am currently implementing the ksmbd change notify feature, and I have encountered the following issue.

Before this patchset, no CREATE response:

Windows client compound request:
CREATE dir; CHANGE_NOTIFY, fid = compound fid

Server response:
CHANGE_NOTIFY STATUS_PENDING, AsyncId = N

After this patchset:

Quoted 8 lines

Server response:
CREATE dir SUCCESS, fid = X
CHANGE_NOTIFY STATUS_PENDING, AsyncId = N

ChenXiaoSong (3):
smb/server: send compound prefix before async pending response smb/server: introduce struct ksmbd_transport_write smb/server: use MSG_EOR for async interim response

Applied them to #ksmbd-for-next-next.
Thanks!