__smbdirect_socket_schedule_cleanup() and smbdirect_socket_cleanup_work() read and change sc->status without any lock, while they may run concurrently, e.g. a send or receive error racing with RDMA_CM_EVENT_DISCONNECTED: CPU0 CPU1 schedule_cleanup() reads CONNECTED schedule_cleanup() status = DISCONNECTED cleanup_work() disable_work(disconnect_work) status = ERROR queue_work(disconnect_work) is a noop The status went backwards and nobody moves it to SMBDIRECT_SOCKET_DISCONNECTING anymore, so smbdirect_socket_destroy() warns about it. If CPU0 overwrites the status before smbdirect_socket_cleanup_work() looks at it, rdma_disconnect() is called for an already disconnected cm_id. Change the status in __smbdirect_socket_schedule_cleanup() and smbdirect_socket_cleanup_work() in the same cleanup_lock section that sets first_error, like smbdirect_socket_set_status() checks first_error and changes the status under that lock. With separate sections, a second caller could still change the status before the caller that set first_error, which then forces its own status, as it was first. Keep the smp_wmb() between the two stores, for the readers without the lock. smbdirect_socket_destroy() also changes the status under cleanup_lock. As rdma_disconnect() may sleep, smbdirect_socket_cleanup_work() only decides under the lock if it's needed and calls it afterwards. Fixes: d85614860184 ("smb: smbdirect: introduce smbdirect_socket_schedule_cleanup[{_lvl,_status}]()") 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-f94f1a82-7d57-4986-8f96-28f3bd02767d Signed-off-by: Stefan Metzmacher --- fs/smb/smbdirect/socket.c | 206 ++++++++++++++++++++++---------------- fs/smb/smbdirect/socket.h | 4 +- 2 files changed, 123 insertions(+), 87 deletions(-) diff --git a/fs/smb/smbdirect/socket.c b/fs/smb/smbdirect/socket.c index 0497a3483e66..07644b20c243 100644 --- a/fs/smb/smbdirect/socket.c +++ b/fs/smb/smbdirect/socket.c @@ -268,6 +268,7 @@ void __smbdirect_socket_schedule_cleanup(struct smbdirect_socket *sc, enum smbdirect_socket_status *force_status) { struct smbdirect_socket *psc, *tsc; + enum smbdirect_socket_status old_status; unsigned long flags; bool was_first = false; @@ -296,7 +297,6 @@ void __smbdirect_socket_schedule_cleanup(struct smbdirect_socket *sc, sc->idle.keepalive = SMBDIRECT_KEEPALIVE_NONE; disable_delayed_work(&sc->idle.timer_work); } - spin_unlock_irqrestore(&sc->cleanup_lock, flags); /* * Callers of SMBDIRECT_CHECK_STATUS_{WARN,DISCONNECT}() @@ -306,53 +306,16 @@ void __smbdirect_socket_schedule_cleanup(struct smbdirect_socket *sc, */ smp_wmb(); - if (was_first) - ___smbdirect_log_generic(sc, func, line, - lvl, - SMBDIRECT_LOG_RDMA_EVENT, - "%s(%1pe%s%s) called from %s in line=%u status=%s\n", - macro_name, - SMBDIRECT_DEBUG_ERR_PTR(error), - force_status ? ", " : "", - force_status ? smbdirect_socket_status_string(*force_status) : "", - func, line, - smbdirect_socket_status_string(sc->status)); - /* - * In case we were a listener we need to - * disconnect all pending and ready sockets - * - * First we move ready sockets to pending again. - * - * Only a socket that was a listener (listen.backlog != -1) owns a - * populated listen.ready/pending list. Guarding on that also keeps - * lockdep quiet: without it, the listener holds sc->listen.lock while - * the loop recurses into each child psc, which takes psc->listen.lock. - * Those are always different instances of the same lock class -- a - * child never listens, so the nesting is strictly listener -> child - * and cannot really deadlock -- but lockdep only sees the class and - * reports "possible recursive locking". A child has empty listen - * lists and nothing to do here, so skipping it loses nothing, and a - * pending child stays on its listener's list for the free path - * (smbdirect_socket_destroy) to reap. + * The status is changed together with first_error under + * cleanup_lock, so concurrent callers and + * smbdirect_socket_cleanup_work() can't move it backwards, + * e.g. from SMBDIRECT_SOCKET_DISCONNECTED to + * SMBDIRECT_SOCKET_ERROR, and only the caller that set + * first_error decides the error status, later callers + * only move it forward. */ - if (sc->listen.backlog != -1) { /* was a listener */ - /* - * We don't move them to the orphaned list here, - * each child does that itself at the end of its - * smbdirect_socket_cleanup_work(), see - * smbdirect_listen_orphan_socket(). Only that checks - * accept.listener, which is still NULL for a child - * that smbdirect_listen_connect_request() is still - * setting up and will release itself on failure. - */ - spin_lock_irqsave(&sc->listen.lock, flags); - list_splice_init(&sc->listen.ready, &sc->listen.pending); - list_for_each_entry_safe(psc, tsc, &sc->listen.pending, accept.list) - smbdirect_socket_schedule_cleanup(psc, sc->first_error); - spin_unlock_irqrestore(&sc->listen.lock, flags); - } - + old_status = sc->status; switch (sc->status) { case SMBDIRECT_SOCKET_RESOLVE_ADDR_FAILED: case SMBDIRECT_SOCKET_RESOLVE_ROUTE_FAILED: @@ -399,6 +362,55 @@ void __smbdirect_socket_schedule_cleanup(struct smbdirect_socket *sc, if (force_status && (was_first || *force_status > sc->status)) sc->status = *force_status; + spin_unlock_irqrestore(&sc->cleanup_lock, flags); + + if (was_first) + ___smbdirect_log_generic(sc, func, line, + lvl, + SMBDIRECT_LOG_RDMA_EVENT, + "%s(%1pe%s%s) called from %s in line=%u status=%s\n", + macro_name, + SMBDIRECT_DEBUG_ERR_PTR(error), + force_status ? ", " : "", + force_status ? smbdirect_socket_status_string(*force_status) : "", + func, line, + smbdirect_socket_status_string(old_status)); + + /* + * In case we were a listener we need to + * disconnect all pending and ready sockets + * + * First we move ready sockets to pending again. + * + * Only a socket that was a listener (listen.backlog != -1) owns a + * populated listen.ready/pending list. Guarding on that also keeps + * lockdep quiet: without it, the listener holds sc->listen.lock while + * the loop recurses into each child psc, which takes psc->listen.lock. + * Those are always different instances of the same lock class -- a + * child never listens, so the nesting is strictly listener -> child + * and cannot really deadlock -- but lockdep only sees the class and + * reports "possible recursive locking". A child has empty listen + * lists and nothing to do here, so skipping it loses nothing, and a + * pending child stays on its listener's list for the free path + * (smbdirect_socket_destroy) to reap. + */ + if (sc->listen.backlog != -1) { /* was a listener */ + /* + * We don't move them to the orphaned list here, + * each child does that itself at the end of its + * smbdirect_socket_cleanup_work(), see + * smbdirect_listen_orphan_socket(). + * smbdirect_listen_connect_request() sets + * accept.listener before it adds a child to the + * pending list, so a child whose setup fails is + * orphaned the same way. + */ + spin_lock_irqsave(&sc->listen.lock, flags); + list_splice_init(&sc->listen.ready, &sc->listen.pending); + list_for_each_entry_safe(psc, tsc, &sc->listen.pending, accept.list) + smbdirect_socket_schedule_cleanup(psc, sc->first_error); + spin_unlock_irqrestore(&sc->listen.lock, flags); + } /* * Wake up all waiters in all wait queues @@ -457,6 +469,8 @@ static void smbdirect_socket_cleanup_work(struct work_struct *work) struct smbdirect_socket *sc = container_of(work, struct smbdirect_socket, disconnect_work); struct smbdirect_socket *psc, *tsc; + enum smbdirect_socket_status old_status; + bool disconnect = false; bool was_first = false; unsigned long flags; @@ -488,19 +502,55 @@ static void smbdirect_socket_cleanup_work(struct work_struct *work) sc->idle.keepalive = SMBDIRECT_KEEPALIVE_NONE; disable_delayed_work(&sc->idle.timer_work); } - spin_unlock_irqrestore(&sc->cleanup_lock, flags); /* * Like in __smbdirect_socket_schedule_cleanup(), * first_error needs to be seen before the status - * changes below. + * changes below and the status is changed together + * with first_error under cleanup_lock. */ smp_wmb(); + old_status = sc->status; + switch (sc->status) { + case SMBDIRECT_SOCKET_NEGOTIATE_NEEDED: + case SMBDIRECT_SOCKET_NEGOTIATE_RUNNING: + case SMBDIRECT_SOCKET_NEGOTIATE_FAILED: + case SMBDIRECT_SOCKET_CONNECTED: + case SMBDIRECT_SOCKET_ERROR: + sc->status = SMBDIRECT_SOCKET_DISCONNECTING; + disconnect = true; + break; + + case SMBDIRECT_SOCKET_CREATED: + case SMBDIRECT_SOCKET_LISTENING: + case SMBDIRECT_SOCKET_RESOLVE_ADDR_NEEDED: + case SMBDIRECT_SOCKET_RESOLVE_ADDR_RUNNING: + case SMBDIRECT_SOCKET_RESOLVE_ADDR_FAILED: + case SMBDIRECT_SOCKET_RESOLVE_ROUTE_NEEDED: + case SMBDIRECT_SOCKET_RESOLVE_ROUTE_RUNNING: + case SMBDIRECT_SOCKET_RESOLVE_ROUTE_FAILED: + case SMBDIRECT_SOCKET_RDMA_CONNECT_NEEDED: + case SMBDIRECT_SOCKET_RDMA_CONNECT_RUNNING: + case SMBDIRECT_SOCKET_RDMA_CONNECT_FAILED: + /* + * rdma_{accept,connect}() never reached + * RDMA_CM_EVENT_ESTABLISHED + */ + sc->status = SMBDIRECT_SOCKET_DISCONNECTED; + break; + + case SMBDIRECT_SOCKET_DISCONNECTING: + case SMBDIRECT_SOCKET_DISCONNECTED: + case SMBDIRECT_SOCKET_DESTROYED: + break; + } + spin_unlock_irqrestore(&sc->cleanup_lock, flags); + if (was_first) smbdirect_log_rdma_event(sc, SMBDIRECT_LOG_ERR, "%s called with first_error==0\n", - smbdirect_socket_status_string(sc->status)); + smbdirect_socket_status_string(old_status)); /* * In case we were a listener we need to @@ -519,10 +569,11 @@ static void smbdirect_socket_cleanup_work(struct work_struct *work) * We don't move them to the orphaned list here, * each child does that itself at the end of its * smbdirect_socket_cleanup_work(), see - * smbdirect_listen_orphan_socket(). Only that checks - * accept.listener, which is still NULL for a child - * that smbdirect_listen_connect_request() is still - * setting up and will release itself on failure. + * smbdirect_listen_orphan_socket(). + * smbdirect_listen_connect_request() sets + * accept.listener before it adds a child to the + * pending list, so a child whose setup fails is + * orphaned the same way. */ spin_lock_irqsave(&sc->listen.lock, flags); list_splice_init(&sc->listen.ready, &sc->listen.pending); @@ -531,13 +582,7 @@ static void smbdirect_socket_cleanup_work(struct work_struct *work) spin_unlock_irqrestore(&sc->listen.lock, flags); } - switch (sc->status) { - case SMBDIRECT_SOCKET_NEGOTIATE_NEEDED: - case SMBDIRECT_SOCKET_NEGOTIATE_RUNNING: - case SMBDIRECT_SOCKET_NEGOTIATE_FAILED: - case SMBDIRECT_SOCKET_CONNECTED: - case SMBDIRECT_SOCKET_ERROR: - sc->status = SMBDIRECT_SOCKET_DISCONNECTING; + if (disconnect) { /* * Make sure we hold the callback lock * im order to coordinate with the @@ -550,32 +595,17 @@ static void smbdirect_socket_cleanup_work(struct work_struct *work) * by the mutex. */ rdma_lock_handler(sc->rdma.cm_id); - rdma_disconnect(sc->rdma.cm_id); - rdma_unlock_handler(sc->rdma.cm_id); - break; - - case SMBDIRECT_SOCKET_CREATED: - case SMBDIRECT_SOCKET_LISTENING: - case SMBDIRECT_SOCKET_RESOLVE_ADDR_NEEDED: - case SMBDIRECT_SOCKET_RESOLVE_ADDR_RUNNING: - case SMBDIRECT_SOCKET_RESOLVE_ADDR_FAILED: - case SMBDIRECT_SOCKET_RESOLVE_ROUTE_NEEDED: - case SMBDIRECT_SOCKET_RESOLVE_ROUTE_RUNNING: - case SMBDIRECT_SOCKET_RESOLVE_ROUTE_FAILED: - case SMBDIRECT_SOCKET_RDMA_CONNECT_NEEDED: - case SMBDIRECT_SOCKET_RDMA_CONNECT_RUNNING: - case SMBDIRECT_SOCKET_RDMA_CONNECT_FAILED: /* - * rdma_{accept,connect}() never reached - * RDMA_CM_EVENT_ESTABLISHED + * The rdma_event handlers run under the handler + * lock and may have moved the status forward to + * SMBDIRECT_SOCKET_DISCONNECTED after we dropped + * cleanup_lock, on RDMA_CM_EVENT_DISCONNECTED or + * RDMA_CM_EVENT_DEVICE_REMOVAL, then + * rdma_disconnect() is not needed anymore. */ - sc->status = SMBDIRECT_SOCKET_DISCONNECTED; - break; - - case SMBDIRECT_SOCKET_DISCONNECTING: - case SMBDIRECT_SOCKET_DISCONNECTED: - case SMBDIRECT_SOCKET_DESTROYED: - break; + if (READ_ONCE(sc->status) == SMBDIRECT_SOCKET_DISCONNECTING) + rdma_disconnect(sc->rdma.cm_id); + rdma_unlock_handler(sc->rdma.cm_id); } /* @@ -605,6 +635,7 @@ static void smbdirect_socket_destroy(struct smbdirect_socket *sc) struct smbdirect_recv_io *recv_tmp; LIST_HEAD(all_list); LIST_HEAD(pending_list); + bool changed; unsigned long flags; smbdirect_log_rdma_event(sc, SMBDIRECT_LOG_INFO, @@ -687,10 +718,13 @@ static void smbdirect_socket_destroy(struct smbdirect_socket *sc) * will be a noop when calling smbdirect_socket_schedule_cleanup* * ib_drain_qp() also won't be called. */ - if (sc->status < SMBDIRECT_SOCKET_DISCONNECTED) { + spin_lock_irqsave(&sc->cleanup_lock, flags); + changed = sc->status < SMBDIRECT_SOCKET_DISCONNECTED; + if (changed) sc->status = SMBDIRECT_SOCKET_DISCONNECTED; + spin_unlock_irqrestore(&sc->cleanup_lock, flags); + if (changed) smbdirect_socket_wake_up_all(sc); - } /* * An IB_WR_REG_MR or IB_WR_LOCAL_INV posted after a status check diff --git a/fs/smb/smbdirect/socket.h b/fs/smb/smbdirect/socket.h index 285c90a2e36f..9203b8d404e3 100644 --- a/fs/smb/smbdirect/socket.h +++ b/fs/smb/smbdirect/socket.h @@ -115,7 +115,9 @@ struct smbdirect_socket { * still 0. INIT_[DELAYED_]WORK() resets the disable count, * so without the lock an activation could undo a disable of * a concurrent teardown. All other writers of first_error - * also hold cleanup_lock. + * also hold cleanup_lock. The teardown also changes the status + * in the same cleanup_lock section that sets first_error, so + * concurrent teardowns can't move it backwards. */ spinlock_t cleanup_lock; -- 2.43.0