smbdirect_connection_register_mr_io() and smbdirect_connection_deregister_mr_io() check sc->status under mr->mutex and then post IB_WR_REG_MR or IB_WR_LOCAL_INV. smbdirect_socket_destroy() changes the status and drains the QP without that mutex, it only waits for it later in smbdirect_connection_destroy_mr_list(): CPU0 CPU1 register_mr_io() mutex_lock(&mr->mutex) status == CONNECTED smbdirect_socket_destroy() status = DISCONNECTED ib_drain_qp() destroy_mr_list() mutex_lock(&mr->mutex) [waits] ib_post_send(REG_MR) mutex_unlock(&mr->mutex) disable_locked(mr) mr->socket = NULL kref_put(&mr->kref) mr_io_register_done() schedule_cleanup(mr->socket == NULL) The work request posted after the drain is flushed asynchronously, so its completion handler passes the NULL mr->socket to smbdirect_socket_schedule_cleanup(), and if the caller already dropped the last reference of the mr, the cqe is part of freed memory. The same happens with IB_WR_LOCAL_INV from smbdirect_connection_deregister_mr_io(). Take and release each mr->mutex in smbdirect_socket_destroy() after the status changed and before ib_drain_qp(), so a work request posted after a status check that still saw CONNECTED is posted before the drain, which then also runs its completion while the mr is still attached. Later callers see the new status under the mutex and don't post anything. Fixes: b0432201a11b ("smb: client: let destroy_mr_list() keep smbdirect_mr_io memory if registered") Fixes: de5ef8ec3c46 ("smb: smbdirect: introduce smbdirect_mr.c with client mr code") 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-4461fbd8-a522-43ee-81d1-651432a55de9 Reviewed-by: Paulo Alcantara Signed-off-by: Stefan Metzmacher --- fs/smb/smbdirect/internal.h | 2 ++ fs/smb/smbdirect/mr.c | 31 ++++++++++++++++++++++++++++++- fs/smb/smbdirect/socket.c | 10 ++++++++++ 3 files changed, 42 insertions(+), 1 deletion(-) diff --git a/fs/smb/smbdirect/internal.h b/fs/smb/smbdirect/internal.h index a1e6a6ec64ec..3215f76302f8 100644 --- a/fs/smb/smbdirect/internal.h +++ b/fs/smb/smbdirect/internal.h @@ -136,6 +136,8 @@ void smbdirect_connection_invalidate_rkey(struct smbdirect_socket *sc, u32 rkey) int smbdirect_connection_create_mr_list(struct smbdirect_socket *sc); +void smbdirect_connection_wait_mr_io_posts(struct smbdirect_socket *sc); + void smbdirect_connection_destroy_mr_list(struct smbdirect_socket *sc); int smbdirect_accept_connect_request(struct smbdirect_socket *sc, diff --git a/fs/smb/smbdirect/mr.c b/fs/smb/smbdirect/mr.c index dd64ec50bec1..c839bae0d81b 100644 --- a/fs/smb/smbdirect/mr.c +++ b/fs/smb/smbdirect/mr.c @@ -145,6 +145,33 @@ static void smbdirect_mr_io_free_locked(struct kref *kref) kfree(mr); } +/* + * smbdirect_connection_register_mr_io() and + * smbdirect_connection_deregister_mr_io() check the status under + * mr->mutex before they post IB_WR_REG_MR or IB_WR_LOCAL_INV. + * smbdirect_socket_destroy() calls this once the status is no longer + * CONNECTED and before ib_drain_qp(), so every work request posted + * after a check that still saw CONNECTED is posted before the drain + * and its completion runs while the mr is still attached to the + * socket. Later callers see the new status and don't post anything. + * + * The list is only changed by smbdirect_connection_create_mr_list(), + * from connect.work, which smbdirect_socket_destroy() already disabled + * and waited for, and by smbdirect_connection_destroy_mr_list() after + * us. Each mr on the list also holds a reference of the socket until + * smbdirect_connection_destroy_mr_list(), so we can walk it without + * sc->mr_io.all.lock and sleep on the mutexes. + */ +void smbdirect_connection_wait_mr_io_posts(struct smbdirect_socket *sc) +{ + struct smbdirect_mr_io *mr; + + list_for_each_entry(mr, &sc->mr_io.all.list, list) { + mutex_lock(&mr->mutex); + mutex_unlock(&mr->mutex); + } +} + void smbdirect_connection_destroy_mr_list(struct smbdirect_socket *sc) { struct smbdirect_mr_io *mr, *tmp; @@ -351,7 +378,9 @@ smbdirect_connection_register_mr_io(struct smbdirect_socket *sc, * smbdirect_connection_destroy_mr_list() (which runs before * smbdirect_connection_destroy_qp() zeroes sc->ib.qp) blocks on * this mutex, so the QP can't be destroyed under the - * ib_post_send() below. + * ib_post_send() below. smbdirect_socket_destroy() also waits + * for this mutex before it drains the QP, see + * smbdirect_connection_wait_mr_io_posts(). * * If the teardown already disabled this mr, it also detached it * from the socket and dropped its reference, so we only drop diff --git a/fs/smb/smbdirect/socket.c b/fs/smb/smbdirect/socket.c index 384cf152d40f..a1c0572a79ca 100644 --- a/fs/smb/smbdirect/socket.c +++ b/fs/smb/smbdirect/socket.c @@ -649,6 +649,16 @@ static void smbdirect_socket_destroy(struct smbdirect_socket *sc) smbdirect_socket_wake_up_all(sc); } + /* + * An IB_WR_REG_MR or IB_WR_LOCAL_INV posted after a status check + * that still saw SMBDIRECT_SOCKET_CONNECTED needs to be posted + * before the drain below, so that its completion runs while the + * mr is still attached to the socket. An earlier drain from the + * rdma cm event handler doesn't cover that, as such a post can + * still happen after it. + */ + smbdirect_connection_wait_mr_io_posts(sc); + if (sc->ib.qp) { smbdirect_log_rdma_event(sc, SMBDIRECT_LOG_INFO, "drain qp\n"); -- 2.43.0