Skip to contentSkip to conversation

cifs: fix potential race with cifsd thread

PATCH In review Paulo Alcantara 3 participants 9 messages Mar 31, 2022 – Apr 1, 2022

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/2 cifs: fix potential race with cifsd thread

To avoid racing with demultiplex thread while it is handling data on socket, use cifs_signal_cifsd_for_reconnect() helper for marking current server to reconnect and let the demultiplex thread handle the rest.

Fixes dca65818c80c ("cifs: use a different reconnect helper for non-cifsd threads")
---
 fs/cifs/connect.c | 2 +-
 fs/cifs/netmisc.c | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)
fs/cifs/connect.c +1 -1
index ee3b7c15e884..3ca06bd88b6e 100644
@@ -4465,7 +4465,7 @@ static int tree_connect_dfs_target(const unsigned int xid, struct cifs_tcon *tco
 	 */
 	if (rc && server->current_fullpath != server->origin_fullpath) {
 		server->current_fullpath = server->origin_fullpath;
-		cifs_reconnect(tcon->ses->server, true);
+		cifs_signal_cifsd_for_reconnect(server, true);
 	}
 
 	dfs_cache_free_tgts(tl);
fs/cifs/netmisc.c +1 -1
index ebe236b9d9f5..235aa1b395eb 100644
@@ -896,7 +896,7 @@ map_and_check_smb_error(struct mid_q_entry *mid, bool logErr)
 		if (class == ERRSRV && code == ERRbaduid) {
 			cifs_dbg(FYI, "Server returned 0x%x, reconnecting session...\n",
 				code);
-			cifs_reconnect(mid->server, false);
+			cifs_signal_cifsd_for_reconnect(mid->server, false);
 		}
 	}
 
Signature · 1 line
2.35.1
#1 lore raw

PATCH 2/2 cifs: force new session setup and tcon for dfs

Do not reuse existing sessions and tcons in DFS failover as it might connect to different servers and shares.

---
 fs/cifs/connect.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
fs/cifs/connect.c +5 -2
index 3ca06bd88b6e..3956672a11ae 100644
@@ -536,8 +536,11 @@ int cifs_reconnect(struct TCP_Server_Info *server, bool mark_smb_session)
 		return __cifs_reconnect(server, mark_smb_session);
 	}
 	spin_unlock(&cifs_tcp_ses_lock);
-
-	return reconnect_dfs_server(server, mark_smb_session);
+	/*
+	 * Ignore @mark_smb_session and invalidate all sessions & tcons as we might be connecting to
+	 * a different server or share during failover.
+	 */
+	return reconnect_dfs_server(server, true);
 }
 #else
 int cifs_reconnect(struct TCP_Server_Info *server, bool mark_smb_session)
Signature · 1 line
2.35.1
#2 lore raw

Re: [PATCH 2/2] cifs: force new session setup and tcon for dfs

Quoted 13 lines · Paulo Alcantara

Do not reuse existing sessions and tcons in DFS failover as it might connect to different servers and shares.

---
fs/cifs/connect.c | 7 +++++--

1 file changed, 5 insertions(+), 2 deletions(-)
fs/cifs/connect.c +0 -0
index 3ca06bd88b6e..3956672a11ae 100644

fs/cifs/connect.c int cifs_reconnect(struct TCP_Server_Info *server, bool mark_smb_session) · line 543

Quoted 10 lines
fs/cifs/connect.c +5 -2
@@ -536,8 +536,11 @@ int cifs_reconnect(struct TCP_Server_Info *server, bool mark_smb_session)
 		return __cifs_reconnect(server, mark_smb_session);
 	}
 	spin_unlock(&cifs_tcp_ses_lock);
-
-	return reconnect_dfs_server(server, mark_smb_session);
+	/*
+	 * Ignore @mark_smb_session and invalidate all sessions & tcons as we might be connecting to
+	 * a different server or share during failover.
+	 */
+	return reconnect_dfs_server(server, true);

If you're ignoring @mark_smb_session, why not just leave @server for reconnect_dfs_server()?

Cheers,

Enzo

#3 lore raw

Re: [PATCH 2/2] cifs: force new session setup and tcon for dfs

Quoted 2 lines · Enzo Matsumiya

If you're ignoring @mark_smb_session, why not just leave @server for reconnect_dfs_server()?

Good point, yes. I'll send v2 with it removed.

Reviewed-by on v1 2/2
#4 lore raw

Re: [PATCH 2/2] cifs: force new session setup and tcon for dfs

In reply to Paulo Alcantara
Quoted 28 lines · Paulo Alcantara

Do not reuse existing sessions and tcons in DFS failover as it might connect to different servers and shares.

---
 fs/cifs/connect.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
fs/cifs/connect.c +5 -3
index 3ca06bd88b6e..3956672a11ae 100644
@@ -536,8 +536,11 @@ int cifs_reconnect(struct TCP_Server_Info *server, bool mark_smb_session)
                return __cifs_reconnect(server, mark_smb_session);
        }
        spin_unlock(&cifs_tcp_ses_lock);
-
-       return reconnect_dfs_server(server, mark_smb_session);
+       /*
+        * Ignore @mark_smb_session and invalidate all sessions & tcons as we might be connecting to
+        * a different server or share during failover.
+        */
+       return reconnect_dfs_server(server, true);
 }
 #else
 int cifs_reconnect(struct TCP_Server_Info *server, bool mark_smb_session)
--
Quoted 2 lines

2.35.1

This makes sense.
Wondering if you could check if the reconnect happens to a new server/share and then change mark_smb_session? Maybe that complicates the logic.

Looks good to me.

Signature · 2 lines
Regards,
Shyam
#5 lore raw

Re: [PATCH 2/2] cifs: force new session setup and tcon for dfs

Quoted 4 lines · Shyam Prasad N

This makes sense.
Wondering if you could check if the reconnect happens to a new server/share and then change mark_smb_session? Maybe that complicates the logic.

Yes, it would complicate alot. We expect failover to not occur quite often, so leaving it without such logic would be OK for now, IMO. I'm trying to get all reconnect issues fixed and then we can talk about possible improvements, obviously.

#6 lore raw

PATCH v2 1/1 cifs: force new session setup and tcon for dfs

In reply to Paulo Alcantara

Do not reuse existing sessions and tcons in DFS failover as it might connect to different servers and shares.

---
 fs/cifs/connect.c | 13 ++++++++-----
 1 file changed, 8 insertions(+), 5 deletions(-)
fs/cifs/connect.c +8 -5
index 3ca06bd88b6e..54155eb4faac 100644
@@ -453,9 +453,7 @@ static int reconnect_target_unlocked(struct TCP_Server_Info *server, struct dfs_
 	return rc;
 }
 
-static int
-reconnect_dfs_server(struct TCP_Server_Info *server,
-		     bool mark_smb_session)
+static int reconnect_dfs_server(struct TCP_Server_Info *server)
 {
 	int rc = 0;
 	const char *refpath = server->current_fullpath + 1;
@@ -479,7 +477,12 @@ reconnect_dfs_server(struct TCP_Server_Info *server,
 	if (!cifs_tcp_ses_needs_reconnect(server, num_targets))
 		return 0;
 
-	cifs_mark_tcp_ses_conns_for_reconnect(server, mark_smb_session);
+	/*
+	 * Unconditionally mark all sessions & tcons for reconnect as we might be connecting to a
+	 * different server or share during failover.  It could be improved by adding some logic to
+	 * only do that in case it connects to a different server or share, though.
+	 */
+	cifs_mark_tcp_ses_conns_for_reconnect(server, true);
 
 	cifs_abort_connection(server);
 
@@ -537,7 +540,7 @@ int cifs_reconnect(struct TCP_Server_Info *server, bool mark_smb_session)
 	}
 	spin_unlock(&cifs_tcp_ses_lock);
 
-	return reconnect_dfs_server(server, mark_smb_session);
+	return reconnect_dfs_server(server);
 }
 #else
 int cifs_reconnect(struct TCP_Server_Info *server, bool mark_smb_session)
Signature · 1 line
2.35.1
Reviewed-by on v1 1/2
#7 lore raw
In reply to Paulo Alcantara
Quoted 40 lines · Paulo Alcantara

To avoid racing with demultiplex thread while it is handling data on socket, use cifs_signal_cifsd_for_reconnect() helper for marking current server to reconnect and let the demultiplex thread handle the rest.

Fixes dca65818c80c ("cifs: use a different reconnect helper for non-cifsd threads")
---
 fs/cifs/connect.c | 2 +-
 fs/cifs/netmisc.c | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)
fs/cifs/connect.c +1 -1
index ee3b7c15e884..3ca06bd88b6e 100644
@@ -4465,7 +4465,7 @@ static int tree_connect_dfs_target(const unsigned int xid, struct cifs_tcon *tco
         */
        if (rc && server->current_fullpath != server->origin_fullpath) {
                server->current_fullpath = server->origin_fullpath;
-               cifs_reconnect(tcon->ses->server, true);
+               cifs_signal_cifsd_for_reconnect(server, true);
        }
 
        dfs_cache_free_tgts(tl);
fs/cifs/netmisc.c +1 -2
index ebe236b9d9f5..235aa1b395eb 100644
@@ -896,7 +896,7 @@ map_and_check_smb_error(struct mid_q_entry *mid, bool logErr)
                if (class == ERRSRV && code == ERRbaduid) {
                        cifs_dbg(FYI, "Server returned 0x%x, reconnecting session...\n",
                                code);
-                       cifs_reconnect(mid->server, false);
+                       cifs_signal_cifsd_for_reconnect(mid->server, false);
                }
        }
 
--
Quoted 2 lines

2.35.1

Oh. Did I miss these?
Looks good to me.

Signature · 2 lines
Regards,
Shyam