smbdirect_listen_rdma_event_handler() checks first_error of the listener and then warns if the status is not SMBDIRECT_SOCKET_LISTENING, but a concurrent teardown of the listener can change the status in between, so the warning is not unexpected. The teardown also walks the pending list of the listener under listen.lock without the handler lock of the listener, which is held while smbdirect_listen_connect_request() adds a new socket to that list. A socket added after the walk was still accepted with rdma_accept(), although the listener was already shut down. Use SMBDIRECT_CHECK_STATUS_WARN() for the status check, which handles a concurrent teardown without a warning, and check first_error of the listener under listen.lock before adding the new socket to the pending list. The teardown sets first_error before it walks the list under the same lock, so either it finds the new socket and cleans it up, or the new socket is not added and the connect request is rejected. A socket on the pending list that is cleaned up while smbdirect_accept_connect_request() is still running is handled by "smb: smbdirect: don't let the connection setup overwrite a teardown status". Fixes: dc691b91ad16 ("smb: smbdirect: introduce smbdirect_socket_{listen,accept}()") Cc: stable@vger.kernel.org Cc: Namjae Jeon Cc: Paulo Alcantara Cc: Tom Talpey Cc: linux-cifs@vger.kernel.org Cc: samba-technical@lists.samba.org Reported-by: Sashiko Closes: https://sashiko.dev/#/bug/linux-c32e9d93-62f2-4b1e-9c3a-5c3325ce1081 Reviewed-by: Paulo Alcantara Signed-off-by: Stefan Metzmacher --- fs/smb/smbdirect/listen.c | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/fs/smb/smbdirect/listen.c b/fs/smb/smbdirect/listen.c index 56929202041f..e9c857edb4c5 100644 --- a/fs/smb/smbdirect/listen.c +++ b/fs/smb/smbdirect/listen.c @@ -271,7 +271,12 @@ static int smbdirect_listen_rdma_event_handler(struct rdma_cm_id *new_id, switch (event->event) { case RDMA_CM_EVENT_CONNECT_REQUEST: - WARN_ON_ONCE(lsc->status != SMBDIRECT_SOCKET_LISTENING); + /* + * A concurrent teardown of the listener may have + * changed the status since the first_error check above. + */ + if (SMBDIRECT_CHECK_STATUS_WARN(lsc, SMBDIRECT_SOCKET_LISTENING)) + return -ECONNABORTED; /* * In case of error return it and let the caller @@ -395,6 +400,17 @@ static int smbdirect_listen_connect_request(struct smbdirect_socket *lsc, * releases it. */ spin_lock_irqsave(&lsc->listen.lock, flags); + /* + * The teardown of the listener sets first_error before it + * walks the pending list under listen.lock, so either it + * finds nsc there and cleans it up, or we see first_error + * and don't add nsc anymore. + */ + if (READ_ONCE(lsc->first_error)) { + spin_unlock_irqrestore(&lsc->listen.lock, flags); + ret = -ECONNABORTED; + goto listener_teardown; + } list_add_tail(&nsc->accept.list, &lsc->listen.pending); WRITE_ONCE(nsc->accept.listener, lsc); spin_unlock_irqrestore(&lsc->listen.lock, flags); @@ -425,6 +441,7 @@ static int smbdirect_listen_connect_request(struct smbdirect_socket *lsc, */ return 0; +listener_teardown: set_settings_failed: set_params_failed: /* -- 2.43.0