If the connection is no longer connected, smbdirect_connection_deregister_mr_io() disables the mr right away, which deregisters mr->mr and unmaps its pages. But the QP is only drained later by smbdirect_socket_destroy(), so the IB_WR_REG_MR that smbdirect_connection_register_mr_io() posted for it may still be pending: CPU0 CPU1 smbdirect_connection_register_mr_io() ib_post_send(IB_WR_REG_MR) smbdirect_socket_schedule_cleanup() sc->status = SMBDIRECT_SOCKET_ERROR smbdirect_connection_deregister_mr_io() smbdirect_mr_io_disable_locked() ib_dereg_mr(mr->mr) ib_dma_unmap_sg() the device processes or flushes the IB_WR_REG_MR of the freed mr E.g. the cifs client deregisters the mr of a read or write that failed to send its request because the connection is being torn down, before smbd_destroy() drained the QP. Providers like rxe keep the ib_mr pointer of a pending IB_WR_REG_MR and use it when processing it. The same happens if posting the IB_WR_LOCAL_INV fails. As the IB_WR_REG_MR is signaled, let smbdirect_connection_mr_io_register_done() complete mr->reg_done and wait for it without holding mr->mutex before the mr is disabled. The completion comes from the device or from the drain in smbdirect_socket_destroy(), which can take mr->mutex while we wait. For a failed IB_WR_LOCAL_INV post, schedule the cleanup and go through the same path. Also read mr->socket only under mr->mutex, after checking that the mr is not disabled, as smbdirect_connection_destroy_mr_list() clears it. Fixes: b0432201a11b ("smb: client: let destroy_mr_list() keep smbdirect_mr_io memory if registered") 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-200f771b-df1a-475d-9279-29e23b8f6c3e Signed-off-by: Stefan Metzmacher --- fs/smb/smbdirect/mr.c | 41 +++++++++++++++++++++++++++++++++++---- fs/smb/smbdirect/socket.h | 1 + 2 files changed, 38 insertions(+), 4 deletions(-) diff --git a/fs/smb/smbdirect/mr.c b/fs/smb/smbdirect/mr.c index c839bae0d81b..0d11bcbe0b94 100644 --- a/fs/smb/smbdirect/mr.c +++ b/fs/smb/smbdirect/mr.c @@ -37,6 +37,7 @@ int smbdirect_connection_create_mr_list(struct smbdirect_socket *sc) kref_init(&mr->kref); mutex_init(&mr->mutex); + init_completion(&mr->reg_done); mr->mr = ib_alloc_mr(sc->ib.pd, sc->mr_io.type, @@ -279,6 +280,7 @@ static void smbdirect_connection_mr_io_register_done(struct ib_cq *cq, struct ib ib_wc_status_msg(wc->status), wc->opcode); smbdirect_socket_schedule_cleanup(sc, -ECONNABORTED); } + complete(&mr->reg_done); } static void smbdirect_connection_mr_io_local_inv_done(struct ib_cq *cq, struct ib_wc *wc) @@ -470,6 +472,7 @@ smbdirect_connection_register_mr_io(struct smbdirect_socket *sc, mr->need_invalidate = true; spin_unlock_irqrestore(&sc->mr_io.all.lock, flags); + reinit_completion(&mr->reg_done); /* * There is no need for waiting for complemtion on ib_post_send * on IB_WR_REG_MR. Hardware enforces a barrier and order of execution @@ -490,6 +493,7 @@ smbdirect_connection_register_mr_io(struct smbdirect_socket *sc, spin_lock_irqsave(&sc->mr_io.all.lock, flags); mr->need_invalidate = false; spin_unlock_irqrestore(&sc->mr_io.all.lock, flags); + complete(&mr->reg_done); smbdirect_log_rdma_mr(sc, SMBDIRECT_LOG_ERR, "ib_post_send failed ret=%d (%1pe) reg_wr->key=0x%x\n", @@ -553,7 +557,7 @@ EXPORT_SYMBOL_GPL(smbdirect_mr_io_fill_buffer_descriptor); */ void smbdirect_connection_deregister_mr_io(struct smbdirect_mr_io *mr) { - struct smbdirect_socket *sc = mr->socket; + struct smbdirect_socket *sc; bool need_invalidate; unsigned long flags; int ret = 0; @@ -563,7 +567,32 @@ void smbdirect_connection_deregister_mr_io(struct smbdirect_mr_io *mr) if (mr->state == SMBDIRECT_MR_DISABLED) goto put_kref; + /* + * Only valid while the mr is not disabled, + * smbdirect_connection_destroy_mr_list() + * clears it under mr->mutex. + */ + sc = mr->socket; + if (sc->status != SMBDIRECT_SOCKET_CONNECTED) { + /* + * The QP is only drained by smbdirect_socket_destroy(), + * so our IB_WR_REG_MR may still be pending and the + * device may still use mr->mr and the mapped pages. + * Wait for its completion before + * smbdirect_mr_io_disable_locked() + * deregisters mr->mr and unmaps the pages. + * + * We still hold the reference to mr + * so we can unlock while waiting, + * smbdirect_socket_destroy() waits for mr->mutex + * before it drains the QP, which completes it. + */ + if (!completion_done(&mr->reg_done)) { + mutex_unlock(&mr->mutex); + wait_for_completion(&mr->reg_done); + goto lock_again; + } smbdirect_mr_io_disable_locked(mr); goto put_kref; } @@ -590,9 +619,14 @@ void smbdirect_connection_deregister_mr_io(struct smbdirect_mr_io *mr) smbdirect_log_rdma_mr(sc, SMBDIRECT_LOG_ERR, "ib_post_send failed ret=%d (%1pe)\n", ret, SMBDIRECT_DEBUG_ERR_PTR(ret)); - smbdirect_mr_io_disable_locked(mr); + /* + * This changes the status, so we disable + * the mr above once our IB_WR_REG_MR + * completed. + */ smbdirect_socket_schedule_cleanup(sc, ret); - goto done; + mutex_unlock(&mr->mutex); + goto lock_again; } /* @@ -621,7 +655,6 @@ void smbdirect_connection_deregister_mr_io(struct smbdirect_mr_io *mr) if (atomic_inc_return(&sc->mr_io.ready.count) == 1) wake_up(&sc->mr_io.ready.wait_queue); -done: atomic_dec(&sc->mr_io.used.count); put_kref: diff --git a/fs/smb/smbdirect/socket.h b/fs/smb/smbdirect/socket.h index a0f8c6a2d57e..ae1a4a978460 100644 --- a/fs/smb/smbdirect/socket.h +++ b/fs/smb/smbdirect/socket.h @@ -878,6 +878,7 @@ struct smbdirect_mr_io { }; bool need_invalidate; + struct completion reg_done; struct completion invalidate_done; }; -- 2.43.0