smbdirect_connection_register_mr_io() took an mr from smbdirect_connection_get_mr_io() (which checks the status while the connection is still connected), then dropped out to acquire mr->mutex and only afterwards touched mr->mr and sc->ib.qp via ib_map_mr_sg() and ib_post_send(). A concurrent smbdirect_socket_release() can tear the connection down in that window: smbdirect_connection_destroy_mr_list() disables the mr (mr->mr = NULL) and smbdirect_connection_destroy_qp() zeroes sc->ib.qp. The I/O thread would then dereference the NULL mr->mr / sc->ib.qp. Keeping the memory of the socket alive doesn't keep the connection connected, so this race is reachable. Re-check sc->status under mr->mutex before using the mr. While we hold mr->mutex and the status is still CONNECTED, smbdirect_connection_destroy_mr_list() - which runs before smbdirect_connection_destroy_qp() - blocks on this mutex, so the QP can't be destroyed under the ib_post_send() below; if the teardown got there first, the status is no longer CONNECTED and we bail out via the existing error path. The similar unlocked sc->ib.qp access in smbdirect_connection_post_send_wr() is fixed by "smb: smbdirect: don't post sends on a torn-down connection". If smbdirect_connection_destroy_mr_list() got mr->mutex first, it already disabled the mr, detached it from the socket and dropped its reference. Then only our reference is dropped, without setting mr->state to SMBDIRECT_MR_ERROR, otherwise the final kref_put() would call smbdirect_mr_io_disable_locked() again with mr->socket == NULL. An IB_WR_REG_MR posted after smbdirect_socket_destroy() drained the QP can still complete after smbdirect_connection_destroy_mr_list(), that is fixed by "smb: smbdirect: don't let MR work requests miss the QP drain". 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/#/patchset/cover.1791225453.git.metze%40samba.org Signed-off-by: Stefan Metzmacher --- fs/smb/smbdirect/mr.c | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/fs/smb/smbdirect/mr.c b/fs/smb/smbdirect/mr.c index 15c6363a2f97..af613a7405e7 100644 --- a/fs/smb/smbdirect/mr.c +++ b/fs/smb/smbdirect/mr.c @@ -295,6 +295,31 @@ smbdirect_connection_register_mr_io(struct smbdirect_socket *sc, mr->sgt.nents = 0; mr->sgt.orig_nents = 0; + /* + * smbdirect_connection_get_mr_io() returned this mr while the + * connection was still connected, but smbdirect_socket_release() + * may have started tearing it down since then. Re-check under + * mr->mutex before touching mr->mr and sc->ib.qp below. + * + * While we hold mr->mutex and the status is still CONNECTED, + * 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. + * + * If the teardown already disabled this mr, it also detached it + * from the socket and dropped its reference, so we only drop + * ours, without changing mr->state or touching sc. Otherwise the + * status is no longer CONNECTED once the teardown started, so + * bail out. + */ + if (mr->state == SMBDIRECT_MR_DISABLED) + goto put_kref; + if (sc->status != SMBDIRECT_SOCKET_CONNECTED) { + ret = -ECONNABORTED; + goto dma_map_error; + } + smbdirect_log_rdma_mr(sc, SMBDIRECT_LOG_INFO, "num_pages=%u count=%zu depth=%u\n", num_pages, iov_iter_count(iter), sp->max_frmr_depth); @@ -363,6 +388,7 @@ smbdirect_connection_register_mr_io(struct smbdirect_socket *sc, smbdirect_socket_schedule_cleanup(sc, ret); +put_kref: /* * smbdirect_connection_get_mr_io() gave us a reference * via kref_get(&mr->kref), we need to remove it again -- 2.43.0