[PATCH v2 07/12] smb: client: wait for pending mids instead of sleeping 46 seconds

From: Stefan Metzmacher

Date: Fri Dec 04 2026 - 08:34:36 EST


If there are still mids on pending_mid_q after the demultiplex thread
shut down the others, clean_demultiplex_info() did msleep(46000) in
order to give the waiting threads time to exit, even if they removed
their mids much earlier.

Wait on server->response_q for pending_mid_q to become empty instead,
still with 46 seconds as maximum. delete_mid(),
cifs_sync_mid_result() and the dequeue in __smb2_find_mid(), e.g.
from smb2_decrypt_offload(), wake it up once they removed the last mid
while the server is exiting.

This relies on "smb: client: let in-flight requests hold a
TCP_Server_Info reference": the thread that removes the last mid wakes
cifsd while it still uses the server, which is only safe because its
mid and the request itself hold server references.

Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2")
Cc: stable@xxxxxxxxxxxxxxx
Cc: Paulo Alcantara <pc@xxxxxxxxxxxxx>
Cc: Namjae Jeon <linkinjeon@xxxxxxxxxx>
Cc: Ronnie Sahlberg <ronniesahlberg@xxxxxxxxx>
Cc: Shyam Prasad N <sprasad@xxxxxxxxxxxxx>
Cc: Tom Talpey <tom@xxxxxxxxxx>
Cc: Bharath SM <bharathsm@xxxxxxxxxxxxx>
Cc: linux-cifs@xxxxxxxxxxxxxxx
Cc: samba-technical@xxxxxxxxxxxxxxx
Cc: linux-kernel@xxxxxxxxxxxxxxx
Signed-off-by: Stefan Metzmacher <metze@xxxxxxxxx>
---
fs/smb/client/cifsproto.h | 1 +
fs/smb/client/connect.c | 4 +++-
fs/smb/client/smb2ops.c | 2 ++
fs/smb/client/transport.c | 24 ++++++++++++++++++++++++
4 files changed, 30 insertions(+), 1 deletion(-)

diff --git a/fs/smb/client/cifsproto.h b/fs/smb/client/cifsproto.h
index f6c95a47525b..402446435105 100644
--- a/fs/smb/client/cifsproto.h
+++ b/fs/smb/client/cifsproto.h
@@ -82,6 +82,7 @@ char *cifs_build_path_to_root(struct smb3_fs_context *ctx,
struct cifs_tcon *tcon, int add_treename);
char *cifs_build_devname(char *nodename, const char *prepath);
void delete_mid(struct TCP_Server_Info *server, struct mid_q_entry *mid);
+void cifs_wake_up_mid_queue_empty_locked(struct TCP_Server_Info *server);
void __release_mid(struct TCP_Server_Info *server,
struct mid_q_entry *midEntry);
void cifs_wake_up_task(struct TCP_Server_Info *server,
diff --git a/fs/smb/client/connect.c b/fs/smb/client/connect.c
index a9531b487e82..ff26b1980bb7 100644
--- a/fs/smb/client/connect.c
+++ b/fs/smb/client/connect.c
@@ -1211,7 +1211,9 @@ clean_demultiplex_info(struct TCP_Server_Info *server)
* response and going ahead and killing cifsd.
*/
cifs_dbg(FYI, "Wait for exit from demultiplex thread\n");
- msleep(46000);
+ wait_event_timeout(server->response_q,
+ list_empty_careful(&server->pending_mid_q),
+ 46 * HZ);
/*
* If threads still have not exited they are probably never
* coming home not much else we can do but free the memory.
diff --git a/fs/smb/client/smb2ops.c b/fs/smb/client/smb2ops.c
index 3dbc70cb52ff..0ba6345ed656 100644
--- a/fs/smb/client/smb2ops.c
+++ b/fs/smb/client/smb2ops.c
@@ -426,6 +426,8 @@ __smb2_find_mid(struct TCP_Server_Info *server, char *buf, bool dequeue)
if (dequeue) {
list_del_init(&mid->qhead);
mid->deleted_from_q = true;
+ /* e.g. smb2_decrypt_offload() while exiting */
+ cifs_wake_up_mid_queue_empty_locked(server);
}
spin_unlock(&server->mid_queue_lock);
return mid;
diff --git a/fs/smb/client/transport.c b/fs/smb/client/transport.c
index 0fefb84fcd0e..04e1e0f668e8 100644
--- a/fs/smb/client/transport.c
+++ b/fs/smb/client/transport.c
@@ -120,6 +120,28 @@ void __release_mid(struct TCP_Server_Info *server, struct mid_q_entry *midEntry)
cifs_tcp_server_put(server);
}

+/*
+ * clean_demultiplex_info() waits for the pending mids to
+ * be removed once the server is exiting.
+ */
+void
+cifs_wake_up_mid_queue_empty_locked(struct TCP_Server_Info *server)
+{
+ lockdep_assert_held(&server->mid_queue_lock);
+
+ /*
+ * Order the removal of the mid before reading tcpStatus,
+ * clean_demultiplex_info() sets CifsExiting before it checks
+ * pending_mid_q in wait_event_timeout(), which has the barrier
+ * on its side, otherwise both may see the old values and it
+ * would wait the full time.
+ */
+ smp_mb();
+ if (READ_ONCE(server->tcpStatus) == CifsExiting &&
+ list_empty(&server->pending_mid_q))
+ wake_up_all(&server->response_q);
+}
+
void
delete_mid(struct TCP_Server_Info *server, struct mid_q_entry *mid)
{
@@ -128,6 +150,7 @@ delete_mid(struct TCP_Server_Info *server, struct mid_q_entry *mid)
if (!mid->deleted_from_q) {
list_del_init(&mid->qhead);
mid->deleted_from_q = true;
+ cifs_wake_up_mid_queue_empty_locked(server);
}
spin_unlock(&server->mid_queue_lock);

@@ -832,6 +855,7 @@ int cifs_sync_mid_result(struct mid_q_entry *mid, struct TCP_Server_Info *server
if (mid->deleted_from_q == false) {
list_del_init(&mid->qhead);
mid->deleted_from_q = true;
+ cifs_wake_up_mid_queue_empty_locked(server);
}
spin_unlock(&server->mid_queue_lock);
cifs_server_dbg(VFS, "%s: invalid mid state mid=%llu state=%d\n",
--
2.43.0