Skip to contentSkip to conversation

FAILED: patch "[PATCH] smb: client: hold server ref while queueing reconnect work" failed to apply to 6.18-stable tree

PATCH New [email protected] 2 participants 3 messages Nov 25 – Nov 26

Subsystems (3)
lore
Share & tools
lore

[email protected]

author maintainer
#0 lore raw

The patch below does not apply to the 6.18-stable tree.
If someone wants it applied there, or to any other stable or longterm tree, then please email the backport, including the original git commit id to <[email protected]>.

To reproduce the conflict and resubmit, you may use the following commands:

git fetch https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/ linux-6.18.y
git checkout FETCH_HEAD
git cherry-pick -x f02d249e9756b78990aaaa1635e47e06deb2cb51
# <resolve conflicts, build, test, etc.>
git commit -s
git send-email --to '<[email protected]>' --in-reply-to '2026112544-groin-automatic-abac@gregkh' --subject-prefix 'PATCH 6.18.y' 'HEAD^..'

Possible dependencies:

thanks,

greg k-h

------------------ original commit in Linus's tree ------------------

From f02d249e9756b78990aaaa1635e47e06deb2cb51 Mon Sep 17 00:00:00 2001
From: Joseph Qi <[email protected]>
Date: Tue, 3 Nov 2026 17:15:57 +0800
Subject: [PATCH] smb: client: hold server ref while queueing reconnect work

When query_server_interfaces() returns -EOPNOTSUPP on a secondary channel, smb2_reconnect() calls smb3_update_ses_channels() which for a channel runs cifs_chan_skip_or_disable(). That clears the channel's slot in the session and calls cifs_put_tcp_session(), which can drop the last reference of the channel and tear it down.

smb2_reconnect() then falls through to skip_add_channels and queues reconnect work on that server, using a server that may already be freed. The send path obtained the channel from cifs_pick_channel(), which does not take a reference, so the session's channel reference is the only one.

Take our own reference on the channel before calling smb3_update_ses_channels() and drop it after the reconnect work has been queued, so the channel cannot be freed while it is still used.

Fixes f591062bdbf4 ("cifs: handle servers that still advertise multichannel after disabling")
fs/smb/client/smb2pdu.c +18 -1
index 7de6b0b2d116..cefbb245edcb 100644
@@ -250,9 +250,10 @@ static int
 smb2_reconnect(__le16 smb2_command, struct cifs_tcon *tcon,
 	       struct TCP_Server_Info *server, bool from_reconnect)
 {
+	bool hold_server_ref = false;
 	struct cifs_ses *ses;
-	int xid;
 	int rc = 0;
+	int xid;
 
 	/*
 	 * SMB2s NegProt, SessSetup, Logoff do not have tcon yet so
@@ -470,6 +471,20 @@ smb2_reconnect(__le16 smb2_command, struct cifs_tcon *tcon,
 			 * treat this as server not supporting multichannel
 			 */
 
+			/*
+			 * If server is a secondary channel,
+			 * cifs_chan_skip_or_disable() called below may
+			 * drop its last reference, so hold our own
+			 * reference until the reconnect work below has
+			 * been queued.
+			 */
+			spin_lock(&cifs_tcp_ses_lock);
+			if (server->srv_count) {
+				server->srv_count++;
+				hold_server_ref = true;
+			}
+			spin_unlock(&cifs_tcp_ses_lock);
+
 			rc = smb3_update_ses_channels(ses, server,
 						       from_reconnect,
 						       true /* disable_mchan */);
@@ -498,6 +513,8 @@ smb2_reconnect(__le16 smb2_command, struct cifs_tcon *tcon,
 
 	if (smb2_command != SMB2_INTERNAL_CMD)
 		cifs_queue_server_reconn(server);
+	if (hold_server_ref)
+		cifs_put_tcp_session(server, from_reconnect);
 
 	atomic_inc(&tconInfoReconnectCount);
 out:
maintainer
#1 lore raw

PATCH 1/2 smb: client: relax session and tcon reconnect attempts

From: Paulo Alcantara <[email protected]>

[ Upstream commit 855982a52ff7d188188f0ecf86c2ce95957202c6 ]

When the client re-establishes connection to the server, it will queue a worker thread that will attempt to reconnect sessions and tcons on every two seconds, which is kinda overkill as it is a very common scenario when having expired passwords or KRB5 TGT tickets, or deleted shares.

Use an exponential backoff strategy to handle session/tcon reconnect attempts in the worker thread to prevent the client from overloading the system when it is very unlikely to re-establish any session/tcon soon while client is idle.

Stable-dep-of: f02d249e9756 ("smb: client: hold server ref while queueing reconnect work")

---
 fs/smb/client/cifsglob.h | 21 +++++++++++++++++++++
 fs/smb/client/connect.c  |  4 ++--
 fs/smb/client/smb2pdu.c  |  6 +++---
 3 files changed, 26 insertions(+), 5 deletions(-)
fs/smb/client/cifsglob.h +21 -0
index ea6533d057acf..68d664e3415ee 100644
@@ -747,6 +747,7 @@ struct TCP_Server_Info {
 	struct session_key session_key;
 	unsigned long lstrp; /* when we got last response from this server */
 	unsigned long neg_start; /* when negotiate started (jiffies) */
+	unsigned long reconn_delay; /* when resched session and tcon reconnect */
 	struct cifs_secmech secmech; /* crypto sec mech functs, descriptors */
 #define	CIFS_NEGFLAVOR_UNENCAP	1	/* wct == 17, but no ext_sec */
 #define	CIFS_NEGFLAVOR_EXTENDED	2	/* wct == 17, ext_sec bit set */
@@ -2398,4 +2399,24 @@ struct cifs_calc_sig_ctx {
 	struct shash_desc *shash;
 };
 
+#define CIFS_RECONN_DELAY_SECS	30
+#define CIFS_MAX_RECONN_DELAY	(4 * CIFS_RECONN_DELAY_SECS)
+
+static inline void cifs_queue_server_reconn(struct TCP_Server_Info *server)
+{
+	if (!delayed_work_pending(&server->reconnect)) {
+		WRITE_ONCE(server->reconn_delay, 0);
+		mod_delayed_work(cifsiod_wq, &server->reconnect, 0);
+	}
+}
+
+static inline void cifs_requeue_server_reconn(struct TCP_Server_Info *server)
+{
+	unsigned long delay = READ_ONCE(server->reconn_delay);
+
+	delay = umin(delay + CIFS_RECONN_DELAY_SECS, CIFS_MAX_RECONN_DELAY);
+	WRITE_ONCE(server->reconn_delay, delay);
+	queue_delayed_work(cifsiod_wq, &server->reconnect, delay * HZ);
+}
+
 #endif	/* _CIFS_GLOB_H */
fs/smb/client/connect.c +2 -2
index 4864fe6b19e30..caad568c6daed 100644
@@ -431,7 +431,7 @@ static int __cifs_reconnect(struct TCP_Server_Info *server,
 			spin_unlock(&server->srv_lock);
 			cifs_swn_reset_server_dstaddr(server);
 			cifs_server_unlock(server);
-			mod_delayed_work(cifsiod_wq, &server->reconnect, 0);
+			cifs_queue_server_reconn(server);
 		}
 	} while (server->tcpStatus == CifsNeedReconnect);
 
@@ -570,7 +570,7 @@ static int reconnect_dfs_server(struct TCP_Server_Info *server)
 		spin_unlock(&server->srv_lock);
 		cifs_swn_reset_server_dstaddr(server);
 		cifs_server_unlock(server);
-		mod_delayed_work(cifsiod_wq, &server->reconnect, 0);
+		cifs_queue_server_reconn(server);
 	} while (server->tcpStatus == CifsNeedReconnect);
 
 	dfs_cache_noreq_update_tgthint(ref_path, target_hint);
fs/smb/client/smb2pdu.c +3 -3
index 7b95493b94642..01e582967df47 100644
@@ -464,7 +464,7 @@ smb2_reconnect(__le16 smb2_command, struct cifs_tcon *tcon,
 	spin_unlock(&ses->ses_lock);
 
 	if (smb2_command != SMB2_INTERNAL_CMD)
-		mod_delayed_work(cifsiod_wq, &server->reconnect, 0);
+		cifs_queue_server_reconn(server);
 
 	atomic_inc(&tconInfoReconnectCount);
 out:
@@ -4342,7 +4342,7 @@ void smb2_reconnect_server(struct work_struct *work)
 done:
 	cifs_dbg(FYI, "Reconnecting tcons and channels finished\n");
 	if (resched)
-		queue_delayed_work(cifsiod_wq, &server->reconnect, 2 * HZ);
+		cifs_requeue_server_reconn(server);
 	mutex_unlock(&pserver->reconnect_mutex);
 
 	/* now we can safely release srv struct */
@@ -4366,7 +4366,7 @@ SMB2_echo(struct TCP_Server_Info *server)
 	    server->ops->need_neg(server)) {
 		spin_unlock(&server->srv_lock);
 		/* No need to send echo on newly established connections */
-		mod_delayed_work(cifsiod_wq, &server->reconnect, 0);
+		cifs_queue_server_reconn(server);
 		return rc;
 	}
 	spin_unlock(&server->srv_lock);
Signature · 1 line
2.53.0
maintainer
#2 lore raw

PATCH 2/2 smb: client: hold server ref while queueing reconnect work

From: Joseph Qi <[email protected]>

[ Upstream commit f02d249e9756b78990aaaa1635e47e06deb2cb51 ]

When query_server_interfaces() returns -EOPNOTSUPP on a secondary channel, smb2_reconnect() calls smb3_update_ses_channels() which for a channel runs cifs_chan_skip_or_disable(). That clears the channel's slot in the session and calls cifs_put_tcp_session(), which can drop the last reference of the channel and tear it down.

smb2_reconnect() then falls through to skip_add_channels and queues reconnect work on that server, using a server that may already be freed. The send path obtained the channel from cifs_pick_channel(), which does not take a reference, so the session's channel reference is the only one.

Take our own reference on the channel before calling smb3_update_ses_channels() and drop it after the reconnect work has been queued, so the channel cannot be freed while it is still used.

Fixes f591062bdbf4 ("cifs: handle servers that still advertise multichannel after disabling")
[ sashal: Used cifs_chan_skip_or_disable() in place of smb3_update_ses_channels(), which doesn't exist in this tree, while keeping the same extra-reference locking and put as upstream. ]
---
 fs/smb/client/smb2pdu.c | 19 ++++++++++++++++++-
 1 file changed, 18 insertions(+), 1 deletion(-)
fs/smb/client/smb2pdu.c +18 -1
index 01e582967df47..d0b1b4c430a78 100644
@@ -219,9 +219,10 @@ static int
 smb2_reconnect(__le16 smb2_command, struct cifs_tcon *tcon,
 	       struct TCP_Server_Info *server, bool from_reconnect)
 {
+	bool hold_server_ref = false;
 	struct cifs_ses *ses;
-	int xid;
 	int rc = 0;
+	int xid;
 
 	/*
 	 * SMB2s NegProt, SessSetup, Logoff do not have tcon yet so
@@ -439,6 +440,20 @@ smb2_reconnect(__le16 smb2_command, struct cifs_tcon *tcon,
 			 * treat this as server not supporting multichannel
 			 */
 
+			/*
+			 * If server is a secondary channel,
+			 * cifs_chan_skip_or_disable() called below may
+			 * drop its last reference, so hold our own
+			 * reference until the reconnect work below has
+			 * been queued.
+			 */
+			spin_lock(&cifs_tcp_ses_lock);
+			if (server->srv_count) {
+				server->srv_count++;
+				hold_server_ref = true;
+			}
+			spin_unlock(&cifs_tcp_ses_lock);
+
 			rc = cifs_chan_skip_or_disable(ses, server,
 						       from_reconnect);
 			goto skip_add_channels;
@@ -465,6 +480,8 @@ smb2_reconnect(__le16 smb2_command, struct cifs_tcon *tcon,
 
 	if (smb2_command != SMB2_INTERNAL_CMD)
 		cifs_queue_server_reconn(server);
+	if (hold_server_ref)
+		cifs_put_tcp_session(server, from_reconnect);
 
 	atomic_inc(&tconInfoReconnectCount);
 out:
Signature · 1 line
2.53.0