smb/server: fix some bugs in preparation for change notify support
Subsystems (2)
ChenXiaoSong
reviewerv3 cover smb/server: fix some bugs in preparation for change notify support
From: ChenXiaoSong <[email protected]>
v2->v3:
- Patch#01: use WARN_ON_ONCE(1).
- Patch#02: use list_for_each_entry_safe().
v2: https://lore.kernel.org/linux-cifs/[email protected]/
I have implemented the ksmbd change notify feature, please see the following link:
https://chenxiaosong.com/lkml/ksmbd-notify
However, to make this feature work properly, some pre-existing bugs need to be fixed first.
ChenXiaoSong (3):
smb/server: fix signing when a response uses more than one iov smb/server: cancel async requests when closing connection smb/server: stop new async work when closing connection
fs/smb/server/connection.c | 21 +++++++++++ fs/smb/server/smb2pdu.c | 75 ++++++++++++++++++++++++++------------ 2 files changed, 73 insertions(+), 23 deletions(-)
Signature · 1 line
2.54.0
ChenXiaoSong
reviewerPATCH v3 1/3 smb/server: fix signing when a response uses more than one iov
From: ChenXiaoSong <[email protected]>
Some SMB responses keep their data in another buffer. The SMB header and the data are then in different iovs.
The old code only handled this for SMB2 READ. For other commands, it signed only the last iov. QUERY_INFO and CHANGE_NOTIFY can also use another iov for their data. Their SMB header was not signed, so Windows will client rejected the response.
Find the iov that starts with the current SMB header. Sign this iov and all iovs after it.
--- fs/smb/server/smb2pdu.c | 51 ++++++++++++++++++++++++++++++----------- 1 file changed, 37 insertions(+), 14 deletions(-)
fs/smb/server/smb2pdu.c +37 -14
@@ -10933,6 +10933,39 @@ int smb2_check_sign_req(struct ksmbd_work *work)return 1 ; } +/** +* smb2_get_sign_rsp_iov() - get the iovecs used to sign a response +* @work: work that has the response iovecs +* @hdr: SMB2 header of the response +* @n_vec: set to the number of iovecs to sign +* +* Response data may be in another buffer. In this case, the response uses +* more than one iovec. Find the iovec that starts with @hdr. Sign this +* iovec and all iovecs after it. +* +* Return: The first iovec to sign. +*/ +static struct kvec *smb2_get_sign_rsp_iov (struct ksmbd_work *work, +struct smb2_hdr *hdr,int *n_vec) +{ +int i; + +/* +* iov[0] has the RFC1002 message length. It is not part of the SMB2 +* message, so do not sign it. +*/ +for (i =1 ; i <= work->iov_idx; i++) { +if (work->iov[i].iov_base == hdr) { + *n_vec = work->iov_idx - i +1 ; +return &work->iov[i]; + } + } + +WARN_ON_ONCE (1 ); + *n_vec =1 ; +return &work->iov[work->iov_idx]; +} +/** * smb2_set_sign_rsp() - handler for rsp packet sign processing * @work: smb work containing notify command buffer
@@ -10943,18 +10976,13 @@ void smb2_set_sign_rsp(struct ksmbd_work *work)struct smb2_hdr *hdr;char signature[SMB2_HMACSHA256_SIZE ];struct kvec *iov; -int n_vec =1 ; +int n_vec; hdr =ksmbd_resp_buf_curr (work); hdr->Flags |=SMB2_FLAGS_SIGNED ;memset (hdr->Signature,0 ,SMB2_SIGNATURE_SIZE ); -if (hdr->Command ==SMB2_READ ) { - iov = &work->iov[work->iov_idx -1 ]; - n_vec++; - }else { - iov = &work->iov[work->iov_idx]; - } + iov =smb2_get_sign_rsp_iov (work, hdr, &n_vec);ksmbd_sign_smb2_pdu (work->conn, work->sess->sess_key, iov, n_vec, signature);
@@ -11037,7 +11065,7 @@ void smb3_set_sign_rsp(struct ksmbd_work *work)char signature[SMB2_CMACAES_SIZE ];struct kvec *iov;u16 command = conn->ops->get_cmd_val (work); -int n_vec =1 ; +int n_vec;char *signing_key; hdr =ksmbd_resp_buf_curr (work);
@@ -11059,12 +11087,7 @@ void smb3_set_sign_rsp(struct ksmbd_work *work) hdr->Flags |=SMB2_FLAGS_SIGNED ;memset (hdr->Signature,0 ,SMB2_SIGNATURE_SIZE ); -if (hdr->Command ==SMB2_READ ) { - iov = &work->iov[work->iov_idx -1 ]; - n_vec++; - }else { - iov = &work->iov[work->iov_idx]; - } + iov =smb2_get_sign_rsp_iov (work, hdr, &n_vec);ksmbd_sign_smb3_pdu (conn, signing_key, iov, n_vec, signature);memcpy (hdr->Signature, signature,SMB2_SIGNATURE_SIZE );
Signature · 1 line
2.54.0
ChenXiaoSong
reviewerPATCH v3 2/3 smb/server: cancel async requests when closing connection
In reply to ChenXiaoSongFrom: ChenXiaoSong <[email protected]>
An async request may still be waiting when a connection is closed. This can stop the connection from closing.
Cancel active async requests before waiting for them to finish.
--- fs/smb/server/connection.c | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+)
fs/smb/server/connection.c +21 -0
@@ -371,6 +371,26 @@ void ksmbd_conn_try_dequeue_request(struct ksmbd_work *work)wake_up_all (&conn->req_running_q); } +static void ksmbd_conn_cancel_async_requests (struct ksmbd_conn *conn) +{ +struct ksmbd_work *work, *tmp; + +ksmbd_debug (CONN ,"Cancel pending async requests on releasing connection\n" ); +spin_lock (&conn->request_lock); +list_for_each_entry_safe (work, tmp, &conn->async_requests, + async_request_entry) { +if (work->state !=KSMBD_WORK_ACTIVE ) +continue ; + +ksmbd_debug (CONN ,"Cancel async request id %d\n" , + work->async_id); + work->state =KSMBD_WORK_CANCELLED ; +if (work->cancel_fn) + work->cancel_fn (work->cancel_argv); + } +spin_unlock (&conn->request_lock); +} +void ksmbd_conn_lock (struct ksmbd_conn *conn) {mutex_lock (&conn->srv_mutex);
@@ -669,6 +689,7 @@ int ksmbd_conn_handler_loop(void *p) }ksmbd_conn_set_releasing (conn); +ksmbd_conn_cancel_async_requests (conn);/* Wait till all reference dropped to the Server object*/ ksmbd_debug (CONN ,"Wait for all pending requests(%d)\n" ,atomic_read (&conn->r_count));wait_event (conn->r_count_q,atomic_read (&conn->r_count) ==0 );
Signature · 1 line
2.54.0
ChenXiaoSong
reviewerPATCH v3 3/3 smb/server: stop new async work when closing connection
In reply to ChenXiaoSongFrom: ChenXiaoSong <[email protected]>
A new async request may be added while a connection is closing. The close code may miss this request and wait forever.
Check the connection state before adding the request. Return an error and release the async ID if the connection is closing.
--- fs/smb/server/smb2pdu.c | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-)
fs/smb/server/smb2pdu.c +15 -9
@@ -1001,21 +1001,27 @@ int setup_async_work(struct ksmbd_work *work, void (*fn)(void **), void **arg)pr_err ("Failed to alloc async message id\n" );return id; } - work->asynchronous = true; - work->async_id = id; -ksmbd_debug (SMB , -"Send interim Response to inform async request id : %d\n" , - work->async_id); +spin_lock (&conn->request_lock); +if (ksmbd_conn_exiting (conn) ||ksmbd_conn_releasing (conn)) { +spin_unlock (&conn->request_lock); +pr_err_ratelimited ("Failed to setup async work: connection is exiting\n" ); +ksmbd_release_id (&conn->async_ida, id); +return -ESHUTDOWN ; + } + work->asynchronous = true; + work->async_id = id; work->cancel_fn = fn; work->cancel_argv = arg; -if (list_empty (&work->async_request_entry)) { -spin_lock (&conn->request_lock); +if (list_empty (&work->async_request_entry))list_add_tail (&work->async_request_entry, &conn->async_requests); -spin_unlock (&conn->request_lock); - } +spin_unlock (&conn->request_lock); + +ksmbd_debug (SMB , +"Send interim Response to inform async request id : %d\n" , + work->async_id);return 0 ; }
Signature · 1 line
2.54.0
Namjae Jeon
maintainerRe: [PATCH v3 3/3] smb/server: stop new async work when closing connection
Quoted 10 lines · ChenXiaoSong
From: ChenXiaoSong <[email protected]>
A new async request may be added while a connection is closing. The close code may miss this request and wait forever.
Check the connection state before adding the request. Return an error and release the async ID if the connection is closing.
Signed-off-by ChenXiaoSong <[email protected]>
I would like to apply the attached patch instead of this one, so please check it.
Thanks!
From cf7802b09d66448f2fd8c5f89ab1a3994b0f7190 Mon Sep 17 00:00:00 2001
From: Namjae Jeon <[email protected]>
Date: Thu, 23 Jul 2026 15:24:10 +0900
Subject: [PATCH] smb/server: avoid registering async requests during
connection close
A connection-close scan can miss the synthetic CHANGE_NOTIFY work item because smb2_notify() registers it directly after setup_async_work() has returned. Link both regular and synthetic async work through one helper that checks the connection state under request_lock.
If the connection is already closing, release a newly allocated async ID or complete the synthetic notify work immediately.
--- fs/smb/server/smb2pdu.c | 53 +++++++++++++++++++++++++++++++---------- 1 file changed, 41 insertions(+), 12 deletions(-)
fs/smb/server/smb2pdu.c +41 -12
@@ -991,6 +991,24 @@ smb2_get_name(const char *src, const int maxlen, struct nls_table *local_nls)return name; } +/* Link a fully initialized async work item unless the connection is closing. */ +static bool ksmbd_conn_link_async_request (struct ksmbd_conn *conn, +struct ksmbd_work *work) +{ +bool linked = false; + +spin_lock (&conn->request_lock); +if (!ksmbd_conn_exiting (conn) && !ksmbd_conn_releasing (conn)) { +if (list_empty (&work->async_request_entry)) +list_add_tail (&work->async_request_entry, + &conn->async_requests); + linked = true; + } +spin_unlock (&conn->request_lock); + +return linked; +} +int setup_async_work (struct ksmbd_work *work,void (*fn)(void **),void **arg) {struct ksmbd_conn *conn = work->conn;
@@ -1003,20 +1021,22 @@ int setup_async_work(struct ksmbd_work *work, void (*fn)(void **), void **arg) } work->asynchronous = true; work->async_id = id; - -ksmbd_debug (SMB , -"Send interim Response to inform async request id : %d\n" , - work->async_id); - work->cancel_fn = fn; work->cancel_argv = arg; -if (list_empty (&work->async_request_entry)) { -spin_lock (&conn->request_lock); -list_add_tail (&work->async_request_entry, &conn->async_requests); -spin_unlock (&conn->request_lock); +if (!ksmbd_conn_link_async_request (conn, work)) { + work->asynchronous = false; + work->async_id =0 ; + work->cancel_fn =NULL ; + work->cancel_argv =NULL ; +ksmbd_release_id (&conn->async_ida, id); +return -ESHUTDOWN ; } +ksmbd_debug (SMB , +"Send interim Response to inform async request id : %d\n" , + work->async_id); +return 0 ; }
@@ -11075,9 +11095,18 @@ int smb2_notify(struct ksmbd_work *work) in_work->cancel_argv[1 ] = fp; in_work->cancel_fn = smb2_notify_cancel_fn; } -spin_lock (&work->conn->request_lock); -list_add_tail (&in_work->async_request_entry, &work->conn->async_requests); -spin_unlock (&work->conn->request_lock); + +if (!ksmbd_conn_link_async_request (work->conn, in_work)) { +kfree (in_work->cancel_argv); + in_work->cancel_argv =NULL ; + in_work->cancel_fn =NULL ; + in_work->asynchronous = false; +ksmbd_fd_put (work, fp); +ksmbd_conn_write (in_work); +ksmbd_free_work_struct (in_work); + work->send_no_response =1 ; +return 0 ; + }spin_lock (&fp->f_lock);list_add_tail (&in_work->notify_entry, &fp->notify_pendings);
Signature · 1 line
2.34.1
ChenXiaoSong
reviewerRe: [PATCH v3 3/3] smb/server: stop new async work when closing connection
I agree with your patch. However, could you split the changes in smb2_notify into a separate patch? We don't yet know how Gaël will update his patch, so keeping those changes separate would make things easier to rebase or adjust later.
Does that make sense to you?
Quoted 2 lines · Namjae Jeon
I would like to apply the attached patch instead of this one, so please check it.
Signature · 3 lines
ChenXiaoSong <[email protected]> Chinese Homepage: https://chenxiaosong.com English Homepage: https://chenxiaosong.com/en
Namjae Jeon
maintainerRe: [PATCH v3 3/3] smb/server: stop new async work when closing connection
Quoted 7 lines · ChenXiaoSong
I agree with your patch. However, could you split the changes in
smb2_notifyinto a separate patch? We don't yet know how Gaël will update his patch, so keeping those changes separate would make things easier to rebase or adjust later.Does that make sense to you?
He needs to submit a patch aligned with the current base. We can't just wait since we don't know when Gaël will send the patch. Also, I made a mistake. I should have set the author to you. I just updated it, so please check it.
Thanks!
ChenXiaoSong
reviewerRe: [PATCH v3 3/3] smb/server: stop new async work when closing connection
No worries. Co-developed-by is fine.
Quoted 3 lines · Namjae Jeon
Also, I
made a mistake. I should have set the author to you. I just updated it, so please check it.
Signature · 3 lines
ChenXiaoSong <[email protected]> Chinese Homepage: https://chenxiaosong.com English Homepage: https://chenxiaosong.com/en
Namjae Jeon
maintainerQuoted 12 lines · ChenXiaoSong
From: ChenXiaoSong <[email protected]>
v2->v3:
- Patch#01: useWARN_ON_ONCE(1).
- Patch#02: uselist_for_each_entry_safe().v2: https://lore.kernel.org/linux-cifs/[email protected]/
I have implemented the ksmbd change notify feature, please see the following link:
https://chenxiaosong.com/lkml/ksmbd-notify
Quoted 6 lines
However, to make this feature work properly, some pre-existing bugs need to be fixed first.
ChenXiaoSong (3):
smb/server: fix signing when a response uses more than one iov smb/server: cancel async requests when closing connection smb/server: stop new async work when closing connection
Applied 0001 and 0002 patch to #ksmbd-for-next-next. Thanks!