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". 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 | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/fs/smb/smbdirect/mr.c b/fs/smb/smbdirect/mr.c index 15c6363a2f97..20620fb043bd 100644 --- a/fs/smb/smbdirect/mr.c +++ b/fs/smb/smbdirect/mr.c @@ -295,6 +295,25 @@ 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, + * the status is no longer CONNECTED and mr->mr would be NULL, so + * bail out. + */ + 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); -- 2.43.0