Skip to contentSkip to conversation

reconnect fixes and improvements

PATCH Superseded Enzo Matsumiya 1 participant 3 messages Sep 25

Subsystems (2)

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

cover reconnect fixes and improvements

Hi all,

(sending a cover letter to keep commit messages smaller)

This series addresses 2 problems:

1)
cifs_reconnect() races with other tasks to acquire server mutex, so reconnect process is delayed when there are many tasks.

(some tests showed cifs_reconnect() waiting >1h to acquire the mutex, which is beyond absurd)

When a non-cifsd task sets CifsNeedReconnect (cifs_signal_cifsd_for_reconnect()), it can take a varying amount of time to cifs_reconnect() to actually run.

cifs_reconnect() only gets to run after all the other tasks in the mutex wait queue fail/timeout their ops (and then release the mutex).

Also, tasks that enter the send path after CifsNeedReconnect is set will wait on cifs_wait_for_server_reconnect() for up to 10s for reconnect to happen. It's clearly not enough given that cifs_reconnect() might not even have started yet.

2)
Requests are discarded because server was prematurely considered down.

cifs_wait_for_server_reconnect() should only wait for reconnect, and not determine if server is down (not its job).

Furthermore, it returns -EHOSTDOWN when wait times out, which is not replayable, thus requests are discarded (lost).

Fix:
Since there's no proper bug (hang, crash, etc) to fix, the patches fixes reconnect behavior:
- clear up the send path if there's a need to reconnect (fast fail)
- make cifs_wait_for_server_reconnect() wait for cifs_reconnect() to start before waiting for a tcpStatus change
- if cifs_wait_for_server_reconnect() times out, but cifs_reconnect() is still trying, allow callers to retry (-EAGAIN vs -EHOSTDOWN)

Reproducer (writer.py at the end):

  # mount.cifs -o ... //srv/share /mnt
  # for i in $(seq 1 20); do ( python3 writer.py /mnt $i & ) ; done
  # echo_interval=$(mount -t cifs | grep -Po '(?<=echo_interval=)[^,]+')
  # ip_addr=$(mount -t cifs | grep -Po '(?<=addr=)[^,]+')
  # while true \
  do \
  sleep $((echo_interval * 2)) \
  iptables -A OUTPUT -d $ip_addr -p tcp --dport 445 -j DROP \
  sleep $((echo_interval * 3)) \
  iptables -D OUTPUT -d $ip_addr -p tcp --dport 445 -j DROP \
  done

About ~15s after the first drop, dmesg starts showing (added task comm/pid for demonstration purposes):
...

  [81802.584463] CIFS: VFS: \\w25.vm.test sends on sock 00000000462438e7 stuck for 15 seconds (task python3[27248])
  [81817.943889] CIFS: VFS: \\w25.vm.test sends on sock 00000000462438e7 stuck for 15 seconds (task python3[26744])
  [81833.303344] CIFS: VFS: \\w25.vm.test sends on sock 00000000462438e7 stuck for 15 seconds (task python3[27239])
  [81848.662748] CIFS: VFS: \\w25.vm.test sends on sock 00000000462438e7 stuck for 15 seconds (task python3[26738])
  [81864.022183] CIFS: VFS: \\w25.vm.test sends on sock 00000000462438e7 stuck for 15 seconds (task python3[27131])
  [81879.381663] CIFS: VFS: \\w25.vm.test sends on sock 00000000462438e7 stuck for 15 seconds (task python3[27280])
  ...

During this particular test, there were 159 other 'python3' tasks already waiting for the mutex by the time cifs_abort_connection() tried to acquire it.

i.e. cifs_reconnect() only ran after ~40min! Obviously, hung task detector is triggered, if enabled.

Cheers,

Enzo

---- writer.py begin ----
from concurrent.futures import ThreadPoolExecutor
import sys
import os

text = "A" * 128 * 100
base_dir = sys.argv[1]
pfx = sys.argv[2]

def create_write_delete_file(i):

    filename = os.path.join(base_dir, f"{pfx}_file_{i}.txt")
    try:
        with open(filename, "w") as f:
            for j in range(1000):
                f.write(f"file: {j}\n")
                f.flush()
                f.write(text)
                f.flush()
                os.fsync(f.fileno())  
        os.remove(filename)
    except Exception as e:
        print(f"error at file: {filename}: {e}")

def main():
for i in range(5):
print(f"run: {i}")
with ThreadPoolExecutor(max_workers=50) as executor:
executor.map(create_write_delete_file, range(500))

if __name__ == "__main__":

    main()

---- writer.py end ----

Hardcoded for 5 runs of 50 workers, writing 1000 times per file, with 500 files. YMMV, so might require increasing numbers if you can't reproduce it.

Enzo Matsumiya (2):
smb: client: fast fail sends if need to reconnect
smb: client: prevent premature discard of requests if reconnecting

 fs/smb/client/cifsglob.h  |  1 +
 fs/smb/client/connect.c   | 35 +++++++++++++----
 fs/smb/client/misc.c      | 83 +++++++++++++++++++++++++--------------
 fs/smb/client/transport.c |  8 ++++
 4 files changed, 89 insertions(+), 38 deletions(-)
Signature · 1 line
2.55.0
#1 lore raw

PATCH 1/2 smb: client: fast fail sends if need to reconnect

smb_send_kvec() keeps retrying when server needs to reconnect, which makes no sense as cifs_reconnect() will never run in parallel (because both need server mutex).
IOW, retrying will never succeed, but only delay reconnects further.

Bail out early from smb_send_kvec() when need to reconnect.

Also shutdown socket queues when CifsNeedReconnect is first set, so any sends/receives fails immediately (don't wait for socket to timeout).

---
 fs/smb/client/connect.c   | 31 +++++++++++++++++++++++--------
 fs/smb/client/transport.c |  8 ++++++++
 2 files changed, 31 insertions(+), 8 deletions(-)
fs/smb/client/connect.c +23 -8
index 28e1ddeb6182..a19fec8bad64 100644
@@ -127,13 +127,21 @@ void smb2_query_server_interfaces(struct work_struct *work)
 			   (SMB_INTERFACE_POLL_INTERVAL * HZ));
 }
 
-#define set_need_reco(server) \
-do { \
-	spin_lock(&server->srv_lock); \
-	if (server->tcpStatus != CifsExiting) \
-		server->tcpStatus = CifsNeedReconnect; \
-	spin_unlock(&server->srv_lock); \
-} while (0)
+static inline void set_need_reco(struct TCP_Server_Info *server)
+{
+	bool shutdown = false;
+
+	spin_lock(&server->srv_lock);
+	if (server->tcpStatus != CifsExiting) {
+		shutdown = (server->tcpStatus != CifsNeedReconnect && server->ssocket);
+		server->tcpStatus = CifsNeedReconnect;
+	}
+	spin_unlock(&server->srv_lock);
+
+	/* Don't release it yet! */
+	if (shutdown)
+		kernel_sock_shutdown(server->ssocket, SHUT_RDWR);
+}
 
 /*
  * Update the tcpStatus for the server.
@@ -353,6 +361,8 @@ cifs_abort_connection(struct TCP_Server_Info *server)
 
 static bool cifs_tcp_ses_needs_reconnect(struct TCP_Server_Info *server, int num_targets)
 {
+	bool shutdown;
+
 	spin_lock(&server->srv_lock);
 	server->nr_targets = num_targets;
 	if (server->tcpStatus == CifsExiting) {
@@ -365,9 +375,14 @@ static bool cifs_tcp_ses_needs_reconnect(struct TCP_Server_Info *server, int num
 	cifs_dbg(FYI, "Mark tcp session as need reconnect\n");
 	trace_smb3_reconnect(server->current_mid, server->conn_id,
 			     server->hostname);
+	shutdown = (server->tcpStatus != CifsNeedReconnect && server->ssocket);
 	server->tcpStatus = CifsNeedReconnect;
-
 	spin_unlock(&server->srv_lock);
+
+	/* Don't release it yet! */
+	if (shutdown)
+		kernel_sock_shutdown(server->ssocket, SHUT_RDWR);
+
 	return true;
 }
 
fs/smb/client/transport.c +8 -0
index 6e21b5f8754a..c6e0ee6d0ef4 100644
@@ -180,6 +180,10 @@ smb_send_kvec(struct TCP_Server_Info *server, struct msghdr *smb_msg,
 		 */
 		rc = sock_sendmsg(ssocket, smb_msg);
 		if (rc == -EAGAIN || unlikely(rc == -EINTR && task_work_pending(current))) {
+			/* Don't retry if need to reconnect */
+			if (unlikely(server->tcpStatus == CifsNeedReconnect))
+				return -ECONNRESET;
+
 			retries++;
 			if (retries >= 14 ||
 			    (!server->noblocksnd && (retries > 2))) {
@@ -191,6 +195,10 @@ smb_send_kvec(struct TCP_Server_Info *server, struct msghdr *smb_msg,
 			continue;
 		}
 
+		/* Blame the need to reconnect for any non-interrupt error */
+		if (unlikely(server->tcpStatus == CifsNeedReconnect && !is_interrupt_error(rc)))
+			rc = -ECONNRESET;
+
 		if (rc < 0)
 			return rc;
 
Signature · 1 line
2.55.0
#2 lore raw

PATCH 2/2 smb: client: prevent premature discard of requests if reconnecting

In reply to Enzo Matsumiya

Add TCP_Server_Info::reconnecting to track cifs_reconnect() lifetime. Wait for it to be true in cifs_wait_for_server_reconnect(), and only then wait for tcpStatus change.

Also, return -EAGAIN (instead of -EHOSTDOWN) if reconnect is still ongoing at the end, so requests are not discarded unnecessarily.

Changes (refactor cifs_wait_for_server_reconnect()):
- remove unnecessary do/while loop; use wait_event_interruptible() for hard mounts
- increase timeout, and base it on echo_interval to match user preferences
- remove useless debug log for interrupt errors

---
 fs/smb/client/cifsglob.h |  1 +
 fs/smb/client/connect.c  |  4 ++
 fs/smb/client/misc.c     | 83 +++++++++++++++++++++++++---------------
 3 files changed, 58 insertions(+), 30 deletions(-)
fs/smb/client/cifsglob.h +1 -0
index 79e4e84f8985..c4f8ca844be8 100644
@@ -802,6 +802,7 @@ struct TCP_Server_Info {
 	bool	posix_ext_supported;
 	struct delayed_work reconnect; /* reconnect workqueue job */
 	struct mutex reconnect_mutex; /* prevent simultaneous reconnects */
+	bool reconnecting; /* if cifs_reconnect() is indeed running */
 	unsigned long echo_interval;
 
 	/*
fs/smb/client/connect.c +4 -0
index a19fec8bad64..e57194f1172a 100644
@@ -377,6 +377,8 @@ static bool cifs_tcp_ses_needs_reconnect(struct TCP_Server_Info *server, int num
 			     server->hostname);
 	shutdown = (server->tcpStatus != CifsNeedReconnect && server->ssocket);
 	server->tcpStatus = CifsNeedReconnect;
+	server->reconnecting = true;
+	wake_up(&server->response_q);
 	spin_unlock(&server->srv_lock);
 
 	/* Don't release it yet! */
@@ -456,6 +458,7 @@ static int __cifs_reconnect(struct TCP_Server_Info *server,
 	spin_lock(&server->srv_lock);
 	if (server->tcpStatus == CifsNeedNegotiate)
 		mod_delayed_work(cifsiod_wq, &server->echo, 0);
+	server->reconnecting = false;
 	spin_unlock(&server->srv_lock);
 
 	wake_up(&server->response_q);
@@ -598,6 +601,7 @@ static int reconnect_dfs_server(struct TCP_Server_Info *server)
 	spin_lock(&server->srv_lock);
 	if (server->tcpStatus == CifsNeedNegotiate)
 		mod_delayed_work(cifsiod_wq, &server->echo, 0);
+	server->reconnecting = false;
 	spin_unlock(&server->srv_lock);
 
 	wake_up(&server->response_q);
fs/smb/client/misc.c +53 -30

97 lines not on this page · raw message

Signature · 1 line
2.55.0