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, count the posted ones that are not yet completed in mr->reg_pending and wait until it's 0 without holding mr->mutex before the mr is disabled. It's a counter and not a completion, as the IB_WR_REG_MR of an earlier registration of the same mr may not be polled yet, if the peer invalidated the mr remotely and it was registered again. The completions come from the device or from the drain in smbdirect_socket_destroy(), which can take mr->mutex while we wait. 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 | 60 ++++++++++++++++++++++++++++++++++++--- fs/smb/smbdirect/socket.h | 5 ++++ 2 files changed, 61 insertions(+), 4 deletions(-) diff --git a/fs/smb/smbdirect/mr.c b/fs/smb/smbdirect/mr.c index c839bae0d81b..45408750fa1b 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); + atomic_set(&mr->reg_pending, 0); mr->mr = ib_alloc_mr(sc->ib.pd, sc->mr_io.type, @@ -193,6 +194,16 @@ void smbdirect_connection_destroy_mr_list(struct smbdirect_socket *sc) smbdirect_mr_io_disable_locked(mr); list_del(&mr->list); mr->socket = NULL; + /* + * Wake a smbdirect_connection_deregister_mr_io() + * waiting for reg_pending, the mr is disabled now. + * wake_up_var() checks the waitqueue without a lock, + * so the store of SMBDIRECT_MR_DISABLED needs to be + * visible before, otherwise the waiter may miss it + * and the wake up. + */ + smp_mb(); + wake_up_var(&mr->reg_pending); /* * No kref_put_mutex() as it's already locked. @@ -279,6 +290,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); } + atomic_dec_and_wake_up(&mr->reg_pending); } static void smbdirect_connection_mr_io_local_inv_done(struct ib_cq *cq, struct ib_wc *wc) @@ -470,6 +482,7 @@ smbdirect_connection_register_mr_io(struct smbdirect_socket *sc, mr->need_invalidate = true; spin_unlock_irqrestore(&sc->mr_io.all.lock, flags); + atomic_inc(&mr->reg_pending); /* * 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 +503,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); + atomic_dec_and_wake_up(&mr->reg_pending); smbdirect_log_rdma_mr(sc, SMBDIRECT_LOG_ERR, "ib_post_send failed ret=%d (%1pe) reg_wr->key=0x%x\n", @@ -553,7 +567,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 +577,40 @@ 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. + * reg_pending also counts the IB_WR_REG_MR of an + * earlier registration of this mr, whose completion + * may not be polled yet, if the peer invalidated it. + * + * 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. + * smbdirect_connection_destroy_mr_list() also + * wakes us after it disabled the mr, so we + * don't depend on the completions alone. + */ + if (atomic_read(&mr->reg_pending)) { + mutex_unlock(&mr->mutex); + wait_var_event(&mr->reg_pending, + !atomic_read(&mr->reg_pending) || + READ_ONCE(mr->state) == SMBDIRECT_MR_DISABLED); + goto lock_again; + } smbdirect_mr_io_disable_locked(mr); goto put_kref; } @@ -590,9 +637,15 @@ 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; + atomic_dec(&sc->mr_io.used.count); + mutex_unlock(&mr->mutex); + goto lock_again; } /* @@ -621,7 +674,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..285c90a2e36f 100644 --- a/fs/smb/smbdirect/socket.h +++ b/fs/smb/smbdirect/socket.h @@ -878,6 +878,11 @@ struct smbdirect_mr_io { }; bool need_invalidate; + /* + * The number of posted IB_WR_REG_MR that are not yet + * completed, see smbdirect_connection_deregister_mr_io(). + */ + atomic_t reg_pending; struct completion invalidate_done; }; -- 2.43.0