If handle_read_data() fails in smb2_decrypt_offload(), e.g. for a STATUS_PENDING interim response, the mid is added back to pending_mid_q, unless the server needs a reconnect. The decrypt offload work runs on decrypt_wq, so this can happen after clean_demultiplex_info() completed all pending mids with MID_SHUTDOWN. Nobody completes the mid added back then, so the request waits forever, and the mid keeps pointing to the server, which clean_demultiplex_info() frees after its 46 second wait. While the server is exiting, complete the mid with MID_SHUTDOWN, as clean_demultiplex_info() does, instead of adding it back. The status is checked under srv_lock, and cifs_put_tcp_session() sets CifsExiting under srv_lock before cifsd exits, so a mid added back before that is still completed by clean_demultiplex_info(). Fixes: 1254100030b3 ("smb3: Handle error case during offload read path") Cc: stable@vger.kernel.org Cc: Paulo Alcantara Cc: Namjae Jeon Cc: Ronnie Sahlberg Cc: Shyam Prasad N Cc: Tom Talpey Cc: Bharath SM Cc: linux-cifs@vger.kernel.org Cc: samba-technical@lists.samba.org Cc: linux-kernel@vger.kernel.org Signed-off-by: Stefan Metzmacher --- fs/smb/client/smb2ops.c | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/fs/smb/client/smb2ops.c b/fs/smb/client/smb2ops.c index 8783738a31ad..91a32920aeab 100644 --- a/fs/smb/client/smb2ops.c +++ b/fs/smb/client/smb2ops.c @@ -5384,6 +5384,18 @@ static void smb2_decrypt_offload(struct work_struct *work) spin_unlock(&dw->server->mid_queue_lock); spin_unlock(&dw->server->srv_lock); mid_execute_callback(dw->server, mid); + } else if (dw->server->tcpStatus == CifsExiting) { + /* + * clean_demultiplex_info() may already have + * completed the pending mids, so a mid added + * back now would never complete, finish it + * like clean_demultiplex_info() does. + */ + spin_lock(&dw->server->mid_queue_lock); + mid->mid_state = MID_SHUTDOWN; + spin_unlock(&dw->server->mid_queue_lock); + spin_unlock(&dw->server->srv_lock); + mid_execute_callback(dw->server, mid); } else { spin_lock(&dw->server->mid_queue_lock); mid->mid_state = MID_REQUEST_SUBMITTED; -- 2.43.0 The struct TCP_Server_Info lifetime references (obj_refs) currently have exactly two holders, the session side and the cifsd thread, both in connect.c. Convert obj_refs to struct kref, move the free code into a release function and add cifs_tcp_server_get() and cifs_tcp_server_put(), so that the next commit can let in-flight requests hold a reference from other files. No functional change. Fixes: 482c117c6dbe ("smb: client: hold server info until cifsd exits") Cc: stable@vger.kernel.org Cc: Eduard Zateev Cc: Paulo Alcantara Cc: Namjae Jeon Cc: Ronnie Sahlberg Cc: Shyam Prasad N Cc: Tom Talpey Cc: Bharath SM Cc: linux-cifs@vger.kernel.org Cc: samba-technical@lists.samba.org Cc: linux-kernel@vger.kernel.org Signed-off-by: Stefan Metzmacher --- fs/smb/client/cifsglob.h | 3 ++- fs/smb/client/cifsproto.h | 2 ++ fs/smb/client/connect.c | 27 ++++++++++++++++++++++----- 3 files changed, 26 insertions(+), 6 deletions(-) diff --git a/fs/smb/client/cifsglob.h b/fs/smb/client/cifsglob.h index 3bfa9b1bacf5..51c987806fd7 100644 --- a/fs/smb/client/cifsglob.h +++ b/fs/smb/client/cifsglob.h @@ -16,6 +16,7 @@ #include #include #include +#include #include #include #include @@ -682,7 +683,7 @@ struct TCP_Server_Info { spinlock_t srv_lock; /* protect anything here that is not protected */ __u64 conn_id; /* connection identifier (useful for debugging) */ int srv_count; /* reference counter */ - refcount_t obj_refs; /* struct lifetime: srv_count users + cifsd */ + struct kref obj_refs; /* struct lifetime, see cifs_tcp_server_get() */ int rfc1001_sessinit; /* whether to estasblish netbios session */ bool with_rfc1001; /* if netbios session is used */ /* 15 character server name + 0x20 16th byte indicating type = srv */ diff --git a/fs/smb/client/cifsproto.h b/fs/smb/client/cifsproto.h index 15123c7b9373..f6c95a47525b 100644 --- a/fs/smb/client/cifsproto.h +++ b/fs/smb/client/cifsproto.h @@ -278,6 +278,8 @@ void cifs_mark_open_handles_for_deleted_file(struct inode *inode, struct TCP_Server_Info *cifs_get_tcp_session(struct smb3_fs_context *ctx, struct TCP_Server_Info *primary_server); void cifs_put_tcp_session(struct TCP_Server_Info *server, int from_reconnect); +void cifs_tcp_server_get(struct TCP_Server_Info *server); +void cifs_tcp_server_put(struct TCP_Server_Info *server); void cifs_put_tcon(struct cifs_tcon *tcon, enum smb3_tcon_ref_trace trace); void cifs_release_automount_timer(void); diff --git a/fs/smb/client/connect.c b/fs/smb/client/connect.c index 064b70bf1fdf..3766f486727a 100644 --- a/fs/smb/client/connect.c +++ b/fs/smb/client/connect.c @@ -1084,10 +1084,10 @@ cifs_enable_signing(struct TCP_Server_Info *server, bool mnt_sign_required) return 0; } -static void cifs_tcp_server_put(struct TCP_Server_Info *server) +static void cifs_tcp_server_release(struct kref *kref) { - if (!refcount_dec_and_test(&server->obj_refs)) - return; + struct TCP_Server_Info *server = + container_of(kref, struct TCP_Server_Info, obj_refs); cifs_crypto_secmech_release(server); kfree_sensitive(server->session_key.response); @@ -1097,6 +1097,23 @@ static void cifs_tcp_server_put(struct TCP_Server_Info *server) kfree_sensitive(server); } +/* + * References of the struct TCP_Server_Info memory, unlike srv_count, + * which controls the connection and the cifsd thread. Holders are the + * session side (dropped by cifs_put_tcp_session()), the cifsd thread + * (dropped at the end of clean_demultiplex_info()) and anything else + * that may still use the struct without holding srv_count. + */ +void cifs_tcp_server_get(struct TCP_Server_Info *server) +{ + kref_get(&server->obj_refs); +} + +void cifs_tcp_server_put(struct TCP_Server_Info *server) +{ + kref_put(&server->obj_refs, cifs_tcp_server_release); +} + static noinline_for_stack void clean_demultiplex_info(struct TCP_Server_Info *server) { @@ -1796,7 +1813,7 @@ cifs_get_tcp_session(struct smb3_fs_context *ctx, rc = -ENOMEM; goto out_err; } - refcount_set(&tcp_ses->obj_refs, 1); + kref_init(&tcp_ses->obj_refs); tcp_ses->hostname = kstrdup(ctx->server_hostname, GFP_KERNEL); if (!tcp_ses->hostname) { @@ -1939,7 +1956,7 @@ cifs_get_tcp_session(struct smb3_fs_context *ctx, goto out_err_crypto_release; } /* Keep the server alive until the demultiplex thread has exited */ - refcount_inc(&tcp_ses->obj_refs); + cifs_tcp_server_get(tcp_ses); /* thread created, put it on the list */ spin_lock(&cifs_tcp_ses_lock); list_add(&tcp_ses->tcp_ses_list, &cifs_tcp_ses_list); -- 2.43.0 Since commit 482c117c6dbe ("smb: client: hold server info until cifsd exits") struct TCP_Server_Info is freed by the last of two holders: the session side (srv_count) and the cifsd demultiplex thread at the end of clean_demultiplex_info(). But in-flight requests don't hold a reference. clean_demultiplex_info() completes the pending mids, sleeps 46 seconds if pending_mid_q is still not empty and then drops its reference, normally the last one. A request can still use the server at that point, e.g. a thread that just removed its mid from pending_mid_q, so that the unlocked list_empty() check skips the 46 second sleep: CPU0 (request) CPU1 (cifsd) compound_send_recv() delete_mid() spin_lock(&mid_queue_lock) list_del_init(&mid->qhead) clean_demultiplex_info() list_empty(&pending_mid_q) == true cifs_tcp_server_put(server) kfree_sensitive(server) spin_unlock(&mid_queue_lock) <- use after free release_mid() __release_mid() server->vals, server->ops <- use after free The same applies to everything a request does after removing its mid: cifs_server_unlock(), cond_sock_shutdown() and add_credits() in compound_send_recv() and cifs_call_async(), and to requests that are still running after the 46 seconds. No time based wait can be correct here, it only makes the race window smaller. struct TCP_Server_Info has to stay alive as long as anything still uses it. So let each mid hold a server reference: alloc_mid() and smb2_mid_entry_alloc() take one and __release_mid() drops it as its very last action. The mid callbacks and their callers dereference the server after the callback released its mid reference, but all callers of mid_execute_callback() hold their own mid reference across the callback, so that is covered by the mid reference as well. compound_send_recv() and cifs_call_async() use the server after deleting their mids (on send failure) and after the response of an async request may already have been processed, so they hold their own reference across the whole request. Now the struct lives until the last request using it is done, so clean_demultiplex_info() dropping its reference is no longer a problem. The 46 second sleep is kept for now. Fixes: 482c117c6dbe ("smb: client: hold server info until cifsd exits") Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2") Cc: stable@vger.kernel.org Cc: Eduard Zateev Cc: Paulo Alcantara Cc: Namjae Jeon Cc: Ronnie Sahlberg Cc: Shyam Prasad N Cc: Tom Talpey Cc: Bharath SM Cc: linux-cifs@vger.kernel.org Cc: samba-technical@lists.samba.org Cc: linux-kernel@vger.kernel.org Signed-off-by: Stefan Metzmacher --- fs/smb/client/smb1transport.c | 2 + fs/smb/client/smb2transport.c | 2 + fs/smb/client/transport.c | 82 +++++++++++++++++++++++++++++------ 3 files changed, 72 insertions(+), 14 deletions(-) diff --git a/fs/smb/client/smb1transport.c b/fs/smb/client/smb1transport.c index 42e95cc1bd89..4ba3a3316288 100644 --- a/fs/smb/client/smb1transport.c +++ b/fs/smb/client/smb1transport.c @@ -47,6 +47,8 @@ alloc_mid(const struct smb_hdr *smb_buffer, struct TCP_Server_Info *server) memset(temp, 0, sizeof(struct mid_q_entry)); refcount_set(&temp->refcount, 1); spin_lock_init(&temp->mid_lock); + /* dropped as the very last step of __release_mid() */ + cifs_tcp_server_get(server); temp->mid = get_mid(smb_buffer); temp->pid = current->pid; temp->command = cpu_to_le16(smb_buffer->Command); diff --git a/fs/smb/client/smb2transport.c b/fs/smb/client/smb2transport.c index 7c4b5faf9de6..53ef87091f47 100644 --- a/fs/smb/client/smb2transport.c +++ b/fs/smb/client/smb2transport.c @@ -627,6 +627,8 @@ smb2_mid_entry_alloc(const struct smb2_hdr *shdr, memset(temp, 0, sizeof(struct mid_q_entry)); refcount_set(&temp->refcount, 1); spin_lock_init(&temp->mid_lock); + /* dropped as the very last step of __release_mid() */ + cifs_tcp_server_get(server); temp->mid = le64_to_cpu(shdr->MessageId); temp->credits = credits > 0 ? credits : 1; temp->pid = current->pid; diff --git a/fs/smb/client/transport.c b/fs/smb/client/transport.c index ea1f4fe42ee5..0fefb84fcd0e 100644 --- a/fs/smb/client/transport.c +++ b/fs/smb/client/transport.c @@ -111,6 +111,13 @@ void __release_mid(struct TCP_Server_Info *server, struct mid_q_entry *midEntry) put_task_struct(midEntry->creator); mempool_free(midEntry, &cifs_mid_pool); + + /* + * Drop the server reference taken when the mid was allocated. + * This might be the last reference, so @server must not be + * touched after this. + */ + cifs_tcp_server_put(server); } void @@ -694,15 +701,11 @@ static void cond_sock_shutdown(struct TCP_Server_Info *server) kernel_sock_shutdown(server->ssocket, SHUT_RDWR); } -/* - * Send a SMB request and set the callback function in the mid to handle - * the result. Caller is responsible for dealing with timeouts. - */ -int -cifs_call_async(struct TCP_Server_Info *server, struct smb_rqst *rqst, - mid_receive_t receive, mid_callback_t callback, - mid_handle_t handle, void *cbdata, const int flags, - const struct cifs_credits *exist_credits) +static int +__cifs_call_async(struct TCP_Server_Info *server, struct smb_rqst *rqst, + mid_receive_t receive, mid_callback_t callback, + mid_handle_t handle, void *cbdata, const int flags, + const struct cifs_credits *exist_credits) { int rc; struct mid_q_entry *mid; @@ -776,6 +779,31 @@ cifs_call_async(struct TCP_Server_Info *server, struct smb_rqst *rqst, return rc; } +/* + * Send a SMB request and set the callback function in the mid to handle + * the result. Caller is responsible for dealing with timeouts. + */ +int +cifs_call_async(struct TCP_Server_Info *server, struct smb_rqst *rqst, + mid_receive_t receive, mid_callback_t callback, + mid_handle_t handle, void *cbdata, const int flags, + const struct cifs_credits *exist_credits) +{ + int rc; + + /* + * Once the request is sent, the response can be processed and + * the mid released before we return. Hold our own reference, + * as we still unlock the server and return credits after that. + */ + cifs_tcp_server_get(server); + rc = __cifs_call_async(server, rqst, receive, callback, handle, + cbdata, flags, exist_credits); + cifs_tcp_server_put(server); + + return rc; +} + int cifs_sync_mid_result(struct mid_q_entry *mid, struct TCP_Server_Info *server) { int rc = 0; @@ -911,11 +939,11 @@ struct TCP_Server_Info *cifs_pick_channel(struct cifs_ses *ses) return server; } -int -compound_send_recv(const unsigned int xid, struct cifs_ses *ses, - struct TCP_Server_Info *server, - const int flags, const int num_rqst, struct smb_rqst *rqst, - int *resp_buf_type, struct kvec *resp_iov) +static int +__compound_send_recv(const unsigned int xid, struct cifs_ses *ses, + struct TCP_Server_Info *server, + const int flags, const int num_rqst, struct smb_rqst *rqst, + int *resp_buf_type, struct kvec *resp_iov) { int i, j, optype, rc = 0; int num_processed = 0; @@ -1188,6 +1216,32 @@ compound_send_recv(const unsigned int xid, struct cifs_ses *ses, return rc; } +int +compound_send_recv(const unsigned int xid, struct cifs_ses *ses, + struct TCP_Server_Info *server, + const int flags, const int num_rqst, struct smb_rqst *rqst, + int *resp_buf_type, struct kvec *resp_iov) +{ + int rc; + + /* + * The mids hold server references, but we still use the server + * after deleting them (e.g. unlock, cond_sock_shutdown() and + * add_credits() on send failure) and cancelled mids are + * released by the demultiplex thread. Hold our own reference. + * + * __compound_send_recv() reports a NULL server. + */ + if (server) + cifs_tcp_server_get(server); + rc = __compound_send_recv(xid, ses, server, flags, num_rqst, rqst, + resp_buf_type, resp_iov); + if (server) + cifs_tcp_server_put(server); + + return rc; +} + int cifs_send_recv(const unsigned int xid, struct cifs_ses *ses, struct TCP_Server_Info *server, -- 2.43.0 receive_encrypted_read() queues smb2_decrypt_offload() on decrypt_wq with a bare server pointer. The work doesn't own a mid reference until smb2_find_dequeue_mid() found the mid, but it uses the server before that for decrypt_raw_data() and server->lstrp. clean_demultiplex_info() doesn't wait for decrypt_wq, so cifsd can drop its server reference, which can be the last one, before the work runs or while it runs. Take a server reference before queueing the work and drop it at the end of the work. Fixes: 482c117c6dbe ("smb: client: hold server info until cifsd exits") Fixes: 35cf94a39728 ("smb3: allow parallelizing decryption of reads") Cc: stable@vger.kernel.org Cc: Eduard Zateev Cc: Paulo Alcantara Cc: Namjae Jeon Cc: Ronnie Sahlberg Cc: Shyam Prasad N Cc: Tom Talpey Cc: Bharath SM Cc: linux-cifs@vger.kernel.org Cc: samba-technical@lists.samba.org Cc: linux-kernel@vger.kernel.org Signed-off-by: Stefan Metzmacher --- fs/smb/client/smb2ops.c | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/fs/smb/client/smb2ops.c b/fs/smb/client/smb2ops.c index 91a32920aeab..3dbc70cb52ff 100644 --- a/fs/smb/client/smb2ops.c +++ b/fs/smb/client/smb2ops.c @@ -5344,6 +5344,7 @@ static void smb2_decrypt_offload(struct work_struct *work) { struct smb2_decrypt_work *dw = container_of(work, struct smb2_decrypt_work, decrypt); + struct TCP_Server_Info *server = dw->server; int rc; struct mid_q_entry *mid; struct iov_iter iter; @@ -5413,6 +5414,8 @@ static void smb2_decrypt_offload(struct work_struct *work) netfs_free_folioq_buffer(dw->buffer); cifs_small_buf_release(dw->buf); kfree(dw); + /* taken by receive_encrypted_read() */ + cifs_tcp_server_put(server); } @@ -5491,6 +5494,11 @@ receive_encrypted_read(struct TCP_Server_Info *server, struct mid_q_entry **mid, dw->buf = server->smallbuf; server->smallbuf = (char *)cifs_small_buf_get(); + /* + * The worker runs independently of cifsd, which can + * exit and drop its server reference meanwhile. + */ + cifs_tcp_server_get(server); queue_work(decrypt_wq, &dw->decrypt); *num_mids = 0; /* worker thread takes care of finding mid */ return -1; -- 2.43.0 A secondary channel holds a srv_count reference of its primary server, which cifs_put_tcp_session() drops as soon as the channel's srv_count reaches 0. But the channel struct lives longer: its cifsd thread keeps running until it notices the signal and clean_demultiplex_info() is done, and in-flight requests on the channel now also hold struct references. These paths use server->primary_server, e.g. smb2_is_valid_lease_break() walking pserver->smb_ses_list, the signing key lookup in smb2transport.c or smb2_set_credits(). If the primary server's last references go away at the same time, the channel dereferences freed memory. Let the channel also hold a struct reference of the primary server, taken in cifs_get_tcp_session() and dropped when the channel struct is released. Fixes: 482c117c6dbe ("smb: client: hold server info until cifsd exits") Fixes: 0f2b305af944 ("cifs: connect individual channel servers to primary channel server") Cc: stable@vger.kernel.org Cc: Eduard Zateev Cc: Paulo Alcantara Cc: Namjae Jeon Cc: Ronnie Sahlberg Cc: Shyam Prasad N Cc: Tom Talpey Cc: Bharath SM Cc: linux-cifs@vger.kernel.org Cc: samba-technical@lists.samba.org Cc: linux-kernel@vger.kernel.org Signed-off-by: Stefan Metzmacher --- fs/smb/client/connect.c | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/fs/smb/client/connect.c b/fs/smb/client/connect.c index 3766f486727a..97b06d24dfa5 100644 --- a/fs/smb/client/connect.c +++ b/fs/smb/client/connect.c @@ -1094,6 +1094,9 @@ static void cifs_tcp_server_release(struct kref *kref) put_net(cifs_net_ns(server)); kfree(server->leaf_fullpath); kfree(server->hostname); + /* taken in cifs_get_tcp_session() */ + if (SERVER_IS_CHAN(server)) + cifs_tcp_server_put(server->primary_server); kfree_sensitive(server); } @@ -1853,6 +1856,13 @@ cifs_get_tcp_session(struct smb3_fs_context *ctx, spin_lock(&cifs_tcp_ses_lock); ++primary_server->srv_count; spin_unlock(&cifs_tcp_ses_lock); + /* + * The channel struct can outlive its srv_count reference + * of the primary server (cifsd and in-flight requests), + * so also keep the primary struct alive until our own + * struct is released. + */ + cifs_tcp_server_get(primary_server); tcp_ses->primary_server = primary_server; } init_waitqueue_head(&tcp_ses->response_q); @@ -1980,8 +1990,10 @@ cifs_get_tcp_session(struct smb3_fs_context *ctx, out_err: if (tcp_ses) { - if (SERVER_IS_CHAN(tcp_ses)) + if (SERVER_IS_CHAN(tcp_ses)) { cifs_put_tcp_session(tcp_ses->primary_server, false); + cifs_tcp_server_put(tcp_ses->primary_server); + } kfree(tcp_ses->hostname); kfree(tcp_ses->leaf_fullpath); if (tcp_ses->ssocket) -- 2.43.0 cifs_reconnect() and the DFS reconnect loop used msleep(3000) between failed reconnect attempts. Once cifs_put_tcp_session() marks the server as CifsExiting, e.g. during umount, the loop still slept the full 3 seconds before noticing it. The SIGKILL sent to cifsd doesn't interrupt msleep() either. Use wait_event_timeout() on server->response_q instead, in the new helper cifs_reconnect_wait_before_retry() used by both loops, so the wait ends as soon as tcpStatus isn't CifsNeedReconnect anymore, and wake up response_q in cifs_put_tcp_session() after setting CifsExiting. It's not interruptible on purpose, as cifs_reconnect() is also called from user processes and a pending signal would otherwise remove the delay between the attempts. Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2") Cc: stable@vger.kernel.org Cc: Paulo Alcantara Cc: Namjae Jeon Cc: Ronnie Sahlberg Cc: Shyam Prasad N Cc: Tom Talpey Cc: Bharath SM Cc: linux-cifs@vger.kernel.org Cc: samba-technical@lists.samba.org Cc: linux-kernel@vger.kernel.org Signed-off-by: Stefan Metzmacher --- fs/smb/client/connect.c | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/fs/smb/client/connect.c b/fs/smb/client/connect.c index 97b06d24dfa5..a9531b487e82 100644 --- a/fs/smb/client/connect.c +++ b/fs/smb/client/connect.c @@ -393,6 +393,22 @@ static bool cifs_tcp_ses_needs_reconnect(struct TCP_Server_Info *server, int num return true; } +/* + * Wait before the next reconnect attempt, but stop waiting once the + * server is exiting, cifs_put_tcp_session() wakes up response_q after + * setting CifsExiting. + * + * It's not interruptible on purpose, as cifs_reconnect() is also called + * from user processes and a pending signal would remove the delay + * between the attempts. + */ +static void cifs_reconnect_wait_before_retry(struct TCP_Server_Info *server) +{ + wait_event_timeout(server->response_q, + READ_ONCE(server->tcpStatus) != CifsNeedReconnect, + 3 * HZ); +} + /* * cifs tcp session reconnection * @@ -446,7 +462,7 @@ static int __cifs_reconnect(struct TCP_Server_Info *server, /* If was asked to reconnect only once, do not try it more times */ if (once) break; - msleep(3000); + cifs_reconnect_wait_before_retry(server); } else { atomic_inc(&tcpSesReconnectCount); set_credits(server, 1); @@ -579,7 +595,7 @@ static int reconnect_dfs_server(struct TCP_Server_Info *server) /* Failed to reconnect socket */ cifs_server_unlock(server); cifs_dbg(FYI, "%s: reconnect error %d\n", __func__, rc); - msleep(3000); + cifs_reconnect_wait_before_retry(server); continue; } /* @@ -1789,6 +1805,8 @@ cifs_put_tcp_session(struct TCP_Server_Info *server, int from_reconnect) spin_lock(&server->srv_lock); server->tcpStatus = CifsExiting; spin_unlock(&server->srv_lock); + /* stop a reconnect waiting before its next try */ + wake_up_all(&server->response_q); task = xchg(&server->tsk, NULL); if (task) -- 2.43.0 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@vger.kernel.org Cc: Paulo Alcantara Cc: Namjae Jeon Cc: Ronnie Sahlberg Cc: Shyam Prasad N Cc: Tom Talpey Cc: Bharath SM Cc: linux-cifs@vger.kernel.org Cc: samba-technical@lists.samba.org Cc: linux-kernel@vger.kernel.org Signed-off-by: Stefan Metzmacher --- 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 It sleeps for at most the given time, but stops early once the server is exiting, as cifs_put_tcp_session() and clean_demultiplex_info() wake up server->response_q after setting CifsExiting. Use it to replace the msleep() calls in the demultiplex thread, which retries after a failed buffer allocation and waits after a negative RFC 1002 session response, so that they don't delay the exit of the server. This is also a prerequisite for "smb: client: use cifs_server_wait_for_exit_ms() for the replay back-off", so it carries the Fixes tag of that patch as well. Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2") Fixes: 665e18794804 ("cifs: Improve handling of NetBIOS packets") Fixes: 4f1fffa23769 ("cifs: commands that are retried should have replay flag set") Cc: stable@vger.kernel.org Cc: Paulo Alcantara Cc: Namjae Jeon Cc: Ronnie Sahlberg Cc: Shyam Prasad N Cc: Tom Talpey Cc: Bharath SM Cc: linux-cifs@vger.kernel.org Cc: samba-technical@lists.samba.org Cc: linux-kernel@vger.kernel.org Signed-off-by: Stefan Metzmacher --- fs/smb/client/cifsproto.h | 2 ++ fs/smb/client/connect.c | 6 +++--- fs/smb/client/misc.c | 17 +++++++++++++++++ 3 files changed, 22 insertions(+), 3 deletions(-) diff --git a/fs/smb/client/cifsproto.h b/fs/smb/client/cifsproto.h index 402446435105..c9ae85422f10 100644 --- a/fs/smb/client/cifsproto.h +++ b/fs/smb/client/cifsproto.h @@ -450,6 +450,8 @@ void cifs_wake_ses_waiters(struct cifs_ses *ses); int cifs_wait_for_server_reconnect(struct TCP_Server_Info *server, struct cifs_tcon *tcon, int aborts, bool nowait); +bool cifs_server_wait_for_exit_ms(struct TCP_Server_Info *server, + unsigned int msecs); static inline void cifs_put_smb_ses(struct cifs_ses *ses) { diff --git a/fs/smb/client/connect.c b/fs/smb/client/connect.c index ff26b1980bb7..c5fa675e6a11 100644 --- a/fs/smb/client/connect.c +++ b/fs/smb/client/connect.c @@ -690,7 +690,7 @@ allocate_buffers(struct TCP_Server_Info *server) server->bigbuf = (char *)cifs_buf_get(); if (!server->bigbuf) { cifs_server_dbg(VFS, "No memory for large SMB response\n"); - msleep(3000); + cifs_server_wait_for_exit_ms(server, 3000); /* retry will check if exiting */ return false; } @@ -703,7 +703,7 @@ allocate_buffers(struct TCP_Server_Info *server) server->smallbuf = (char *)cifs_small_buf_get(); if (!server->smallbuf) { cifs_server_dbg(VFS, "No memory for SMB response\n"); - msleep(1000); + cifs_server_wait_for_exit_ms(server, 1000); /* retry will check if exiting */ return false; } @@ -976,7 +976,7 @@ is_smb_response(struct TCP_Server_Info *server, unsigned char type) * mount context will fail at all. */ if (rc != 0) - msleep(2000); + cifs_server_wait_for_exit_ms(server, 2000); } else { cifs_server_dbg(VFS, "RFC 1002 negative session response (unexpected)\n"); cifs_reconnect(server, true); diff --git a/fs/smb/client/misc.c b/fs/smb/client/misc.c index a0e7818d3c8d..197f39c23119 100644 --- a/fs/smb/client/misc.c +++ b/fs/smb/client/misc.c @@ -1078,6 +1078,23 @@ int cifs_inval_name_dfs_link_error(const unsigned int xid, } #endif +/* + * Sleep for at most @msecs, but stop early once the server is exiting, + * cifs_put_tcp_session() and clean_demultiplex_info() wake up + * server->response_q after setting CifsExiting. + * + * Returns true if the server is exiting. + */ +bool cifs_server_wait_for_exit_ms(struct TCP_Server_Info *server, + unsigned int msecs) +{ + wait_event_timeout(server->response_q, + READ_ONCE(server->tcpStatus) == CifsExiting, + msecs_to_jiffies(msecs)); + + return READ_ONCE(server->tcpStatus) == CifsExiting; +} + static bool reco_wait_done(struct TCP_Server_Info *server, struct cifs_tcon *tcon, int aborts) { -- 2.43.0 The replay of a request after smb2_should_replay() backs off with msleep(cur_sleep), up to CIFS_MAX_SLEEP per retry. Use cifs_server_wait_for_exit_ms() instead, so the back-off ends once the server is exiting and the replay fails right away instead of sleeping first. The back-off of a write retry pass is shared by its subrequests, it is only recorded in req->slept if it wasn't ended early, so the subrequests on other channels still back off. The reopen in cifs_reopen_file_retry() picks a channel of the session for each attempt, so its back-off waits on the primary server of the session, which exits together with the session, instead of the channel of the subrequest. Fixes: 4f1fffa23769 ("cifs: commands that are retried should have replay flag set") Cc: stable@vger.kernel.org Cc: Paulo Alcantara Cc: Namjae Jeon Cc: Ronnie Sahlberg Cc: Shyam Prasad N Cc: Tom Talpey Cc: Bharath SM Cc: linux-cifs@vger.kernel.org Cc: samba-technical@lists.samba.org Cc: linux-kernel@vger.kernel.org Signed-off-by: Stefan Metzmacher --- fs/smb/client/cached_dir.c | 2 +- fs/smb/client/file.c | 20 +++++++++++++++----- fs/smb/client/link.c | 2 +- fs/smb/client/smb2inode.c | 6 +++--- fs/smb/client/smb2ops.c | 8 ++++---- fs/smb/client/smb2pdu.c | 28 ++++++++++++++-------------- 6 files changed, 38 insertions(+), 28 deletions(-) diff --git a/fs/smb/client/cached_dir.c b/fs/smb/client/cached_dir.c index 50217f47a0ed..cde48aa13710 100644 --- a/fs/smb/client/cached_dir.c +++ b/fs/smb/client/cached_dir.c @@ -315,7 +315,7 @@ int open_cached_dir(unsigned int xid, struct cifs_tcon *tcon, if (retries) { /* Back-off before retry */ if (cur_sleep) - msleep(cur_sleep); + cifs_server_wait_for_exit_ms(server, cur_sleep); smb2_set_replay(server, &rqst[0]); smb2_set_replay(server, &rqst[1]); diff --git a/fs/smb/client/file.c b/fs/smb/client/file.c index e7ed1ad58f37..4863b7f8ddbb 100644 --- a/fs/smb/client/file.c +++ b/fs/smb/client/file.c @@ -188,7 +188,12 @@ static int cifs_reopen_file_retry(struct cifsFileInfo *cfile, bool can_flush, } if (!cifs_io_should_replay(subreq)) return rc; - msleep(subreq->cur_sleep); + /* + * The reopen picks a channel of the session for each + * attempt, so wait on the primary server, which exits + * together with the session. + */ + cifs_server_wait_for_exit_ms(ses->server, subreq->cur_sleep); } } @@ -472,10 +477,15 @@ static void cifs_issue_write(struct netfs_io_subrequest *subreq) container_of(subreq, struct cifs_io_subrequest, subreq); struct cifs_io_request *req = wdata->req; - if (wdata->cur_sleep > req->slept) { - msleep(wdata->cur_sleep - req->slept); + /* + * Only record the back-off for the other subrequests of this + * pass if it really happened, not if it ended early, because + * the server of this subrequest is exiting. + */ + if (wdata->cur_sleep > req->slept && + !cifs_server_wait_for_exit_ms(wdata->server, + wdata->cur_sleep - req->slept)) req->slept = wdata->cur_sleep; - } if (unlikely(wdata->deferred || cifs_writeback_dying(subreq))) { subreq->work.func = cifs_issue_write_worker; @@ -647,7 +657,7 @@ static void cifs_issue_read(struct netfs_io_subrequest *subreq) goto not_sent; if (rdata->replay && rdata->cur_sleep) { - msleep(rdata->cur_sleep); + cifs_server_wait_for_exit_ms(server, rdata->cur_sleep); rc = -EIO; if (cifs_io_aborted(rdata)) goto failed; diff --git a/fs/smb/client/link.c b/fs/smb/client/link.c index e865aeea8dbb..b26fb2b0f997 100644 --- a/fs/smb/client/link.c +++ b/fs/smb/client/link.c @@ -422,7 +422,7 @@ int smb2_query_mf_symlink(const unsigned int xid, if (retries) { if (cur_sleep) - msleep(cur_sleep); + cifs_server_wait_for_exit_ms(server, cur_sleep); for (int i = 0; i < num_rqst; i++) smb2_set_replay(server, &rqst[i]); } diff --git a/fs/smb/client/smb2inode.c b/fs/smb/client/smb2inode.c index cc29ff042c56..5e9a18b50ae2 100644 --- a/fs/smb/client/smb2inode.c +++ b/fs/smb/client/smb2inode.c @@ -541,7 +541,7 @@ static int smb2_compound_op(const unsigned int xid, struct cifs_tcon *tcon, if (retries) { /* Back-off before retry */ if (cur_sleep) - msleep(cur_sleep); + cifs_server_wait_for_exit_ms(server, cur_sleep); for (i = 1; i < num_rqst - 2; i++) smb2_set_replay(server, &rqst[i]); } @@ -554,7 +554,7 @@ static int smb2_compound_op(const unsigned int xid, struct cifs_tcon *tcon, if (retries) { /* Back-off before retry */ if (cur_sleep) - msleep(cur_sleep); + cifs_server_wait_for_exit_ms(server, cur_sleep); for (i = 0; i < num_rqst; i++) smb2_set_replay(server, &rqst[i]); } @@ -1176,7 +1176,7 @@ smb2_unlink(const unsigned int xid, struct cifs_tcon *tcon, const char *name, if (retries) { /* Back-off before retry */ if (cur_sleep) - msleep(cur_sleep); + cifs_server_wait_for_exit_ms(server, cur_sleep); for (int i = 0; i < ARRAY_SIZE(rqst); i++) smb2_set_replay(server, &rqst[i]); } diff --git a/fs/smb/client/smb2ops.c b/fs/smb/client/smb2ops.c index 0ba6345ed656..ab0b0baaa448 100644 --- a/fs/smb/client/smb2ops.c +++ b/fs/smb/client/smb2ops.c @@ -1362,7 +1362,7 @@ static int smb2_set_ea(const unsigned int xid, struct cifs_tcon *tcon, if (retries) { /* Back-off before retry */ if (cur_sleep) - msleep(cur_sleep); + cifs_server_wait_for_exit_ms(server, cur_sleep); smb2_set_replay(server, &rqst[0]); smb2_set_replay(server, &rqst[1]); smb2_set_replay(server, &rqst[2]); @@ -1781,7 +1781,7 @@ smb2_ioctl_query_info(const unsigned int xid, if (retries) { /* Back-off before retry */ if (cur_sleep) - msleep(cur_sleep); + cifs_server_wait_for_exit_ms(server, cur_sleep); smb2_set_replay(server, &rqst[0]); smb2_set_replay(server, &rqst[1]); smb2_set_replay(server, &rqst[2]); @@ -2674,7 +2674,7 @@ smb2_query_dir_first(const unsigned int xid, struct cifs_tcon *tcon, if (retries) { /* Back-off before retry */ if (cur_sleep) - msleep(cur_sleep); + cifs_server_wait_for_exit_ms(server, cur_sleep); smb2_set_replay(server, &rqst[0]); smb2_set_replay(server, &rqst[1]); } @@ -3081,7 +3081,7 @@ smb2_query_info_compound(const unsigned int xid, struct cifs_tcon *tcon, if (retries) { /* Back-off before retry */ if (cur_sleep) - msleep(cur_sleep); + cifs_server_wait_for_exit_ms(server, cur_sleep); if (!cfid) { smb2_set_replay(server, &rqst[0]); smb2_set_replay(server, &rqst[2]); diff --git a/fs/smb/client/smb2pdu.c b/fs/smb/client/smb2pdu.c index 8d6c11099f6a..4134705b3cbc 100644 --- a/fs/smb/client/smb2pdu.c +++ b/fs/smb/client/smb2pdu.c @@ -3124,7 +3124,7 @@ int smb311_posix_mkdir(const unsigned int xid, struct inode *inode, if (retries) { /* Back-off before retry */ if (cur_sleep) - msleep(cur_sleep); + cifs_server_wait_for_exit_ms(server, cur_sleep); smb2_set_replay(server, &rqst); } @@ -3425,7 +3425,7 @@ SMB2_open(const unsigned int xid, struct cifs_open_parms *oparms, __le16 *path, /* Back-off before retry */ if (cur_sleep) - msleep(cur_sleep); + cifs_server_wait_for_exit_ms(server, cur_sleep); if (oparms->replay) smb2_set_replay(server, &rqst); @@ -3690,7 +3690,7 @@ SMB2_ioctl(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid, if (retries) { /* Back-off before retry */ if (cur_sleep) - msleep(cur_sleep); + cifs_server_wait_for_exit_ms(server, cur_sleep); smb2_set_replay(server, &rqst); } @@ -3885,7 +3885,7 @@ __SMB2_close(const unsigned int xid, struct cifs_tcon *tcon, if (retries) { /* Back-off before retry */ if (cur_sleep) - msleep(cur_sleep); + cifs_server_wait_for_exit_ms(server, cur_sleep); smb2_set_replay(server, &rqst); } @@ -4096,7 +4096,7 @@ query_info(const unsigned int xid, struct cifs_tcon *tcon, if (retries) { /* Back-off before retry */ if (cur_sleep) - msleep(cur_sleep); + cifs_server_wait_for_exit_ms(server, cur_sleep); smb2_set_replay(server, &rqst); } @@ -4281,7 +4281,7 @@ SMB2_change_notify(const unsigned int xid, struct cifs_tcon *tcon, if (retries) { /* Back-off before retry */ if (cur_sleep) - msleep(cur_sleep); + cifs_server_wait_for_exit_ms(server, cur_sleep); smb2_set_replay(server, &rqst); } @@ -4623,7 +4623,7 @@ SMB2_flush(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid, if (retries) { /* Back-off before retry */ if (cur_sleep) - msleep(cur_sleep); + cifs_server_wait_for_exit_ms(server, cur_sleep); smb2_set_replay(server, &rqst); } @@ -5541,7 +5541,7 @@ SMB2_write(const unsigned int xid, struct cifs_io_parms *io_parms, if (retries) { /* Back-off before retry */ if (cur_sleep) - msleep(cur_sleep); + cifs_server_wait_for_exit_ms(server, cur_sleep); smb2_set_replay(server, &rqst); } @@ -5932,7 +5932,7 @@ SMB2_query_directory(const unsigned int xid, struct cifs_tcon *tcon, if (retries) { /* Back-off before retry */ if (cur_sleep) - msleep(cur_sleep); + cifs_server_wait_for_exit_ms(server, cur_sleep); smb2_set_replay(server, &rqst); } @@ -6079,7 +6079,7 @@ send_set_info(const unsigned int xid, struct cifs_tcon *tcon, if (retries) { /* Back-off before retry */ if (cur_sleep) - msleep(cur_sleep); + cifs_server_wait_for_exit_ms(server, cur_sleep); smb2_set_replay(server, &rqst); } @@ -6212,7 +6212,7 @@ SMB2_oplock_break(const unsigned int xid, struct cifs_tcon *tcon, if (retries) { /* Back-off before retry */ if (cur_sleep) - msleep(cur_sleep); + cifs_server_wait_for_exit_ms(server, cur_sleep); smb2_set_replay(server, &rqst); } @@ -6340,7 +6340,7 @@ SMB311_posix_qfs_info(const unsigned int xid, struct cifs_tcon *tcon, if (retries) { /* Back-off before retry */ if (cur_sleep) - msleep(cur_sleep); + cifs_server_wait_for_exit_ms(server, cur_sleep); smb2_set_replay(server, &rqst); } @@ -6425,7 +6425,7 @@ SMB2_QFS_attr(const unsigned int xid, struct cifs_tcon *tcon, if (retries) { /* Back-off before retry */ if (cur_sleep) - msleep(cur_sleep); + cifs_server_wait_for_exit_ms(server, cur_sleep); smb2_set_replay(server, &rqst); } @@ -6528,7 +6528,7 @@ smb2_lockv(const unsigned int xid, struct cifs_tcon *tcon, if (retries) { /* Back-off before retry */ if (cur_sleep) - msleep(cur_sleep); + cifs_server_wait_for_exit_ms(server, cur_sleep); smb2_set_replay(server, &rqst); } -- 2.43.0 If the socket is bound to an address of another family than the destination, rdma_resolve_addr() fails with -EINVAL. connect() of a TCP socket fails with -EAFNOSUPPORT in that case, e.g. if the server name resolves to an IPv6 address after a DNS change while the client uses an IPv4 srcaddr=. Check it in smbdirect_connect() and return -EAFNOSUPPORT, so that the callers can handle it like for TCP. This is a prerequisite for the next patch, which lets SMB Direct connections of cifs use the srcaddr= mount option, and so carries its Fixes tag. Fixes: 2f8946464b11 ("CIFS: SMBD: Upper layer connects to SMBDirect session") Cc: stable@vger.kernel.org Cc: Namjae Jeon Cc: Paulo Alcantara Cc: Ronnie Sahlberg Cc: Shyam Prasad N Cc: Tom Talpey Cc: Bharath SM Cc: linux-cifs@vger.kernel.org Cc: samba-technical@lists.samba.org Cc: linux-kernel@vger.kernel.org Signed-off-by: Stefan Metzmacher --- fs/smb/smbdirect/connect.c | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/fs/smb/smbdirect/connect.c b/fs/smb/smbdirect/connect.c index 0d02f03063ac..ef03b70836ab 100644 --- a/fs/smb/smbdirect/connect.c +++ b/fs/smb/smbdirect/connect.c @@ -42,6 +42,13 @@ int smbdirect_connect(struct smbdirect_socket *sc, const struct sockaddr *dst) if (src_addr.sa.sa_family != AF_UNSPEC) src = &src_addr.sa; + /* + * Like connect() of a socket bound to an address of another + * family, rdma_resolve_addr() would fail with -EINVAL. + */ + if (src && src->sa_family != dst->sa_family) + return -EAFNOSUPPORT; + smbdirect_log_rdma_event(sc, SMBDIRECT_LOG_INFO, "connect: src: %pISpsfc dst: %pISpsfc\n", src, dst); -- 2.43.0 The TCP connect binds the socket to the local address given with the srcaddr= mount option in bind_socket(), but SMB Direct ignored it, so rdma_resolve_addr() picked the source address and with it the RDMA device from the routing table. Bind the smbdirect socket to srcaddr before connecting, so that rdma_resolve_addr() uses it. rdma_bind_addr() fails if it isn't a local address (anymore), like the bind() of the TCP connect, or if there is no RDMA device for it, and smbdirect_connect() fails with -EAFNOSUPPORT if it has another family than the server address, since the previous patch. Note that this changes the behavior of existing mounts with rdma and a srcaddr= that has no RDMA device, e.g. an address of another NIC: they connected before, as srcaddr= was ignored, now they fail with -ENODEV. As such a srcaddr= never had an effect, this should be rare. This also lets the following patch, which checks whether there is a usable route to the server with the source address after a reconnect attempt failed, look at the address actually used for SMB Direct. Fixes: 2f8946464b11 ("CIFS: SMBD: Upper layer connects to SMBDirect session") Cc: stable@vger.kernel.org Cc: Paulo Alcantara Cc: Namjae Jeon Cc: Ronnie Sahlberg Cc: Shyam Prasad N Cc: Tom Talpey Cc: Bharath SM Cc: linux-cifs@vger.kernel.org Cc: samba-technical@lists.samba.org Cc: linux-kernel@vger.kernel.org Signed-off-by: Stefan Metzmacher --- fs/smb/client/smbdirect.c | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/fs/smb/client/smbdirect.c b/fs/smb/client/smbdirect.c index 5cc199fca558..bb2ffb1601c5 100644 --- a/fs/smb/client/smbdirect.c +++ b/fs/smb/client/smbdirect.c @@ -343,6 +343,21 @@ static struct smbd_connection *_smbd_get_connection( *sport = htons(port); + /* + * Use the local address given with the srcaddr= mount option, + * like the TCP connect in bind_socket(). rdma_bind_addr() fails + * if it's not a local address (anymore). + */ + if (server->srcaddr.ss_family != AF_UNSPEC) { + ret = smbdirect_socket_bind(sc, + (struct sockaddr *)&server->srcaddr); + if (ret) { + log_rdma_event(ERR, "bind to %pISpsfc failed: %1pe\n", + &server->srcaddr, ERR_PTR(ret)); + goto bind_failed; + } + } + ret = smbdirect_connect_sync(sc, dstaddr); if (ret) { log_rdma_event(ERR, "connect to %pISpsfc failed: %1pe\n", @@ -354,6 +369,7 @@ static struct smbd_connection *_smbd_get_connection( return info; connect_failed: +bind_failed: set_settings_failed: set_params_failed: smbdirect_socket_release(sc); -- 2.43.0 On soft mounts cifs_wait_for_server_reconnect() waited up to 10 seconds (per target) for the reconnect, even if the reconnect attempts failed right away, e.g. because the local address used for the connection was removed. This delays e.g. the statfs() umount(8) does before calling umount(2) by 10 seconds. Count the reconnect attempts that failed with an error indicating a local problem, -ENODEV if the network device used for the connection is gone, -ENETDOWN if the local network is down, -EAFNOSUPPORT if the address family isn't available, e.g. IPv6 is disabled and the server's name resolves to an IPv6 address after a DNS change, and -EPERM if a cgroup BPF program for connect or bind denied it (ICMP errors never map to -EPERM), in server->reconnect_local_failures and wake up server->response_q after each of them, so that requests on soft mounts give up once the next such reconnect attempt failed. It's an atomic_t, as SMB1 also reconnects in the context of a request, not only in cifsd. For SMB Direct the route check is only an approximation, as it uses the inputs of the TCP connect, while iWARP connects to port 5445, and an RDMA device removal is reported as -ENETDOWN. The source address is the same since the previous patch. -EADDRNOTAVAIL, -ENETUNREACH, -EHOSTUNREACH and -EACCES are only counted if there is no usable route to the server, e.g. the local address and its route were removed, or there is an unreachable or prohibit route for the server. They also come from ICMP errors of a router on the way, which abort the connect, as -EHOSTUNREACH if the server doesn't answer ARP or neighbour discovery, e.g. while it restarts, or as -EADDRNOTAVAIL if no local port is free, and then a route lookup to the server still succeeds. The lookup uses the same inputs as the TCP connect: the protocol and port for policy routing, IPv4 routing for an IPv4-mapped address and the scope id only for a link-local address, with a snapshot of the addresses taken under srv_lock. As the IPv6 lookup doesn't check whether the source address is still a local one, like the IPv4 lookup does, a removed IPv6 srcaddr is checked explicitly. A local firewall that rejects the connection with an ICMP error looks the same and keeps waiting, which only costs the waiting time as before. Note that a short local outage, e.g. while the address or route is removed or the interface is set down for a few seconds, now also makes requests on soft mounts fail after about 3 seconds instead of 10. That's what soft mounts are for, hard mounts hide it as before. Just losing the carrier, e.g. a cable replug, keeps the address and route, so the requests keep waiting as before. For a DFS mount the reconnect tries all targets in one attempt, so it only counts as failed locally if every target failed locally, as the error of the last target alone says nothing about the others, e.g. one target may be restarting while another one is unreachable from here. IPC tcons, e.g. the one of a session used for DFS referrals, have no retry setting of their own, so they keep waiting as before, also on soft mounts. For other errors the requests wait up to 10 seconds as before, e.g. while the server restarts the first attempt may fail with -ECONNREFUSED and the next one succeeds, so the 10 second wait still hides such a short outage from the applications. Hard mounts still keep waiting. For stable: before v7.1 CONFIG_IPV6 can be a module, so a backport has to use IS_REACHABLE(CONFIG_IPV6) for the IPv6 route lookup or ipv6_stub, and the route lookup helpers changed their arguments over time. Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2") Fixes: aa24d1e96924 ("CIFS: Process reconnects for SMB2 shares") Cc: stable@vger.kernel.org Cc: Paulo Alcantara Cc: Namjae Jeon Cc: Ronnie Sahlberg Cc: Shyam Prasad N Cc: Tom Talpey Cc: Bharath SM Cc: linux-cifs@vger.kernel.org Cc: samba-technical@lists.samba.org Cc: linux-kernel@vger.kernel.org Signed-off-by: Stefan Metzmacher --- fs/smb/client/cifsglob.h | 5 + fs/smb/client/connect.c | 193 ++++++++++++++++++++++++++++++++++++++- fs/smb/client/misc.c | 31 +++++-- 3 files changed, 218 insertions(+), 11 deletions(-) diff --git a/fs/smb/client/cifsglob.h b/fs/smb/client/cifsglob.h index 51c987806fd7..58ad71d20044 100644 --- a/fs/smb/client/cifsglob.h +++ b/fs/smb/client/cifsglob.h @@ -743,6 +743,11 @@ struct TCP_Server_Info { char workstation_RFC1001_name[RFC1001_NAME_LEN_WITH_NULL]; __u32 sequence_number; /* for signing, protected by srv_mutex */ __u32 reconnect_instance; /* incremented on each reconnect */ + /* + * incremented on each reconnect attempt that failed because + * of a local problem + */ + atomic_t reconnect_local_failures; __le32 session_key_id; /* retrieved from negotiate response and send in session setup request */ struct session_key session_key; unsigned long lstrp; /* when we got last response from this server */ diff --git a/fs/smb/client/connect.c b/fs/smb/client/connect.c index c5fa675e6a11..865051e579db 100644 --- a/fs/smb/client/connect.c +++ b/fs/smb/client/connect.c @@ -30,6 +30,8 @@ #include #include #include +#include +#include #include #include #include "cifsglob.h" @@ -409,6 +411,167 @@ static void cifs_reconnect_wait_before_retry(struct TCP_Server_Info *server) 3 * HZ); } +static bool cifs_route4_missing(struct net *net, __be32 daddr, __be32 saddr, + __be16 dport) +{ + struct flowi4 fl4; + struct rtable *rt; + + rt = ip_route_output_ports(net, &fl4, NULL, daddr, saddr, dport, 0, + IPPROTO_TCP, 0, 0); + if (IS_ERR(rt)) + return true; + ip_rt_put(rt); + return false; +} + +/* + * Returns true if there is no usable route to the server, e.g. as the + * local address and its route were removed, or there is an unreachable + * or prohibit route for the server. The lookup uses the same inputs as + * the TCP connect: protocol and port for policy routing, IPv4 routing + * for an IPv4-mapped address and the scope id only for a link-local + * address. + */ +static bool cifs_server_route_missing(struct TCP_Server_Info *server) +{ + struct net *net = cifs_net_ns(server); + struct sockaddr_storage dst, src; + struct sockaddr_in *dst4 = (struct sockaddr_in *)&dst; + struct sockaddr_in *src4 = (struct sockaddr_in *)&src; + + /* the interface query of a channel can change them */ + spin_lock(&server->srv_lock); + memcpy(&dst, &server->dstaddr, sizeof(dst)); + memcpy(&src, &server->srcaddr, sizeof(src)); + spin_unlock(&server->srv_lock); + + switch (dst.ss_family) { + case AF_INET: + return cifs_route4_missing(net, dst4->sin_addr.s_addr, + src.ss_family == AF_INET ? + src4->sin_addr.s_addr : 0, + dst4->sin_port); +#if IS_ENABLED(CONFIG_IPV6) + case AF_INET6: { + struct sockaddr_in6 *dst6 = (struct sockaddr_in6 *)&dst; + struct sockaddr_in6 *src6 = (struct sockaddr_in6 *)&src; + struct flowi6 fl6 = { + .flowi6_proto = IPPROTO_TCP, + .daddr = dst6->sin6_addr, + .fl6_dport = dst6->sin6_port, + }; + struct dst_entry *dst_entry; + + if (ipv6_addr_v4mapped(&dst6->sin6_addr)) { + __be32 saddr = 0; + + if (src.ss_family == AF_INET6 && + ipv6_addr_v4mapped(&src6->sin6_addr)) + saddr = src6->sin6_addr.s6_addr32[3]; + else if (src.ss_family == AF_INET) + saddr = src4->sin_addr.s_addr; + return cifs_route4_missing(net, + dst6->sin6_addr.s6_addr32[3], + saddr, dst6->sin6_port); + } + + /* connect failed with -EAFNOSUPPORT anyway */ + if (!ipv6_mod_enabled()) + return false; + + if (ipv6_addr_type(&dst6->sin6_addr) & IPV6_ADDR_LINKLOCAL) + fl6.flowi6_oif = dst6->sin6_scope_id; + if (src.ss_family == AF_INET6) { + /* + * Unlike the IPv4 lookup, the IPv6 one doesn't + * check whether the source address is still a + * local one, the bind() fails if it's gone. + */ + if (!ipv6_addr_any(&src6->sin6_addr) && + !ipv6_chk_addr(net, &src6->sin6_addr, NULL, 0)) + return true; + fl6.saddr = src6->sin6_addr; + } + + dst_entry = ip6_dst_lookup_flow(net, NULL, &fl6, NULL); + if (IS_ERR(dst_entry)) + return true; + dst_release(dst_entry); + return false; + } +#endif + } + + return false; +} + +/* + * Errors that indicate a local problem, which is not expected to go + * away within a few seconds, e.g. the local address or the network + * device used for the connection is gone. Other errors, e.g. + * -ECONNREFUSED while the server restarts, may be transient. + * + * The route check uses the inputs of the TCP connect, iWARP connects + * to port 5445 instead, and an RDMA device removal, e.g. during a + * device reset, is reported as -ENETDOWN, so for SMB Direct this is + * only an approximation. + */ +static bool cifs_reconnect_error_is_local(struct TCP_Server_Info *server, + int rc) +{ + switch (rc) { + case -ENETDOWN: + case -ENODEV: + return true; + case -EAFNOSUPPORT: + /* + * E.g. IPv6 is disabled and the server's name resolves to + * an IPv6 address after a DNS change. + */ + return true; + case -EPERM: + /* + * A cgroup BPF program for connect or bind denied it, ICMP + * errors never map to -EPERM. + */ + return true; + case -EADDRNOTAVAIL: + case -ENETUNREACH: + case -EHOSTUNREACH: + case -EACCES: + /* + * These also come from ICMP errors of a router on the + * way, or -EHOSTUNREACH if the server doesn't answer + * ARP or neighbour discovery, e.g. while it restarts, + * and -EADDRNOTAVAIL if no local port is free. They are + * only local if there is no usable route to the server + * here, e.g. as the source address is gone. + */ + return cifs_server_route_missing(server); + } + + return false; +} + +/* + * Let the requests waiting in cifs_wait_for_server_reconnect() + * on soft mounts know that a reconnect attempt failed because + * of a local problem. + */ +static void cifs_reconnect_failed(struct TCP_Server_Info *server, bool local) +{ + if (!local) + return; + + /* + * Not only cifsd, SMB1 also reconnects from cifs_check_receive() + * in the context of a request. + */ + atomic_inc(&server->reconnect_local_failures); + wake_up_all(&server->response_q); +} + /* * cifs tcp session reconnection * @@ -459,6 +622,8 @@ static int __cifs_reconnect(struct TCP_Server_Info *server, if (rc) { cifs_server_unlock(server); cifs_dbg(FYI, "%s: reconnect error %d\n", __func__, rc); + cifs_reconnect_failed(server, + cifs_reconnect_error_is_local(server, rc)); /* If was asked to reconnect only once, do not try it more times */ if (once) break; @@ -520,35 +685,51 @@ static int __reconnect_target_locked(struct TCP_Server_Info *server, return rc; } +/* + * *all_local tells whether every target failed because of a local + * problem, see cifs_reconnect_error_is_local(), it has to be checked + * after each target, as server->dstaddr changes with the target. + */ static int reconnect_target_locked(struct TCP_Server_Info *server, struct dfs_cache_tgt_list *tl, - struct dfs_cache_tgt_iterator **target_hint) + struct dfs_cache_tgt_iterator **target_hint, + bool *all_local) { struct dfs_cache_tgt_iterator *tit; int rc; *target_hint = NULL; + *all_local = false; /* If dfs target list is empty, then reconnect to last server */ tit = dfs_cache_get_tgt_iterator(tl); - if (!tit) - return __reconnect_target_locked(server, server->hostname); + if (!tit) { + rc = __reconnect_target_locked(server, server->hostname); + if (rc) + *all_local = cifs_reconnect_error_is_local(server, rc); + return rc; + } /* Otherwise, try every dfs target in @tl */ + *all_local = true; do { const char *target = dfs_cache_get_tgt_name(tit); spin_lock(&server->srv_lock); if (server->tcpStatus != CifsNeedReconnect) { spin_unlock(&server->srv_lock); + *all_local = false; return -ECONNRESET; } spin_unlock(&server->srv_lock); rc = __reconnect_target_locked(server, target); if (!rc) { *target_hint = tit; + *all_local = false; break; } + if (!cifs_reconnect_error_is_local(server, rc)) + *all_local = false; } while ((tit = dfs_cache_get_next_tgt(tl, tit))); return rc; } @@ -559,6 +740,7 @@ static int reconnect_dfs_server(struct TCP_Server_Info *server) const char *ref_path = server->leaf_fullpath + 1; DFS_CACHE_TGT_LIST(tl); int num_targets = 0; + bool all_local; int rc = 0; /* @@ -590,11 +772,14 @@ static int reconnect_dfs_server(struct TCP_Server_Info *server) try_to_freeze(); cifs_server_lock(server); - rc = reconnect_target_locked(server, &tl, &target_hint); + rc = reconnect_target_locked(server, &tl, &target_hint, + &all_local); if (rc) { /* Failed to reconnect socket */ cifs_server_unlock(server); cifs_dbg(FYI, "%s: reconnect error %d\n", __func__, rc); + /* only if all targets failed locally */ + cifs_reconnect_failed(server, all_local); cifs_reconnect_wait_before_retry(server); continue; } diff --git a/fs/smb/client/misc.c b/fs/smb/client/misc.c index 197f39c23119..5ee6414e2aae 100644 --- a/fs/smb/client/misc.c +++ b/fs/smb/client/misc.c @@ -1096,17 +1096,29 @@ bool cifs_server_wait_for_exit_ms(struct TCP_Server_Info *server, } static bool reco_wait_done(struct TCP_Server_Info *server, - struct cifs_tcon *tcon, int aborts) + struct cifs_tcon *tcon, int aborts, + int failures) { - return server->tcpStatus != CifsNeedReconnect || - atomic_read(&tcon->umount_aborts) != aborts || - cifs_tcon_dying(tcon); + if (server->tcpStatus != CifsNeedReconnect || + atomic_read(&tcon->umount_aborts) != aborts || + cifs_tcon_dying(tcon)) + return true; + + /* + * soft mounts only wait until the next reconnect attempt failed + * because of a local problem, not IPC tcons, e.g. the one of a + * session used for DFS referrals, they have no retry setting of + * their own and wait as before + */ + return !tcon->retry && !tcon->ipc && + atomic_read(&server->reconnect_local_failures) != failures; } int cifs_wait_for_server_reconnect(struct TCP_Server_Info *server, struct cifs_tcon *tcon, int aborts, bool nowait) { + int failures; int timeout = 10; int rc; @@ -1120,6 +1132,7 @@ int cifs_wait_for_server_reconnect(struct TCP_Server_Info *server, return -EAGAIN; } timeout *= server->nr_targets; + failures = atomic_read(&server->reconnect_local_failures); spin_unlock(&server->srv_lock); /* @@ -1127,14 +1140,18 @@ int cifs_wait_for_server_reconnect(struct TCP_Server_Info *server, * reconnect -- should be greater than cifs socket timeout which is 7 * seconds. * - * On "soft" mounts we wait once. Hard mounts keep retrying until - * process is killed or server comes back on-line. + * On "soft" mounts we wait once, but only until the next reconnect + * attempt failed because of a local problem, e.g. it can't succeed + * if the local address is gone. + * Hard mounts keep retrying until process is killed or server comes + * back on-line. */ do { rc = wait_event_interruptible_timeout(server->response_q, reco_wait_done(server, tcon, - aborts), + aborts, + failures), timeout * HZ); if (rc < 0) { cifs_dbg(FYI, "%s: aborting reconnect due to received signal\n", -- 2.43.0