smbdirect_connection_post_send_wr() only checked sc->first_error before ib_post_send(sc->ib.qp, ...). A concurrent smbdirect_socket_release() can run the teardown to smbdirect_connection_destroy_qp(), which zeroes sc->ib.qp, in the window between that check and the ib_post_send(), leading to a NULL pointer dereference. Re-check sc->first_error after incrementing send_io.pending.count and pair it with smbdirect_socket_destroy(): first_error is set before the teardown, the QP is drained, and we now wait for send_io.pending.count to reach 0 before destroying the QP. So a sender either observes first_error and backs off before touching sc->ib.qp, or its increment is observed by the wait and the QP stays alive across ib_post_send(). The send completions that drop send_io.pending.count run on the CQ workqueue and don't take the handler lock held during destroy, and the QP is already drained at that point, so the wait does not block. smbdirect_connection_send_io_done() also has to drop send_io.pending.count for a signaled send completing with an error, e.g. after a link down, otherwise smbdirect_socket_destroy() would wait forever. The unsignaled siblings arriving via skip_free are not counted. smbdirect_connect_negotiate_send_done() and smbdirect_accept_negotiate_send_done() now also wake up send_io.pending.zero_wait_queue when the count reaches 0, like all other places. They can't hang smbdirect_socket_destroy(), as it calls ib_drain_qp() before waiting, but we shouldn't depend on that. Fixes: 422a2436697d ("smb: smbdirect: introduce smbdirect_socket_destroy[_sync]()") 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/accept.c | 3 ++- fs/smb/smbdirect/connect.c | 3 ++- fs/smb/smbdirect/connection.c | 38 ++++++++++++++++++++++++++++++++++- fs/smb/smbdirect/socket.c | 14 +++++++++++++ 4 files changed, 55 insertions(+), 3 deletions(-) diff --git a/fs/smb/smbdirect/accept.c b/fs/smb/smbdirect/accept.c index 85e37d11f43d..9fb0676a3035 100644 --- a/fs/smb/smbdirect/accept.c +++ b/fs/smb/smbdirect/accept.c @@ -697,7 +697,8 @@ static void smbdirect_accept_negotiate_send_done(struct ib_cq *cq, struct ib_wc /* Note this frees wc->wr_cqe, but not wc */ smbdirect_connection_free_send_io(send_io); - atomic_dec(&sc->send_io.pending.count); + if (atomic_dec_and_test(&sc->send_io.pending.count)) + wake_up(&sc->send_io.pending.zero_wait_queue); if (unlikely(wc->status != IB_WC_SUCCESS || WARN_ON_ONCE(wc->opcode != IB_WC_SEND))) { if (wc->status != IB_WC_WR_FLUSH_ERR) diff --git a/fs/smb/smbdirect/connect.c b/fs/smb/smbdirect/connect.c index 0d02f03063ac..ce00e6466690 100644 --- a/fs/smb/smbdirect/connect.c +++ b/fs/smb/smbdirect/connect.c @@ -560,7 +560,8 @@ static void smbdirect_connect_negotiate_send_done(struct ib_cq *cq, struct ib_wc /* Note this frees wc->wr_cqe, but not wc */ smbdirect_connection_free_send_io(send_io); - atomic_dec(&sc->send_io.pending.count); + if (atomic_dec_and_test(&sc->send_io.pending.count)) + wake_up(&sc->send_io.pending.zero_wait_queue); if (unlikely(wc->status != IB_WC_SUCCESS || WARN_ON_ONCE(wc->opcode != IB_WC_SEND))) { if (wc->status != IB_WC_WR_FLUSH_ERR) diff --git a/fs/smb/smbdirect/connection.c b/fs/smb/smbdirect/connection.c index e6c7b343fe87..e375584ab447 100644 --- a/fs/smb/smbdirect/connection.c +++ b/fs/smb/smbdirect/connection.c @@ -885,13 +885,32 @@ int smbdirect_connection_post_send_wr(struct smbdirect_socket *sc, return sc->first_error; atomic_inc(&sc->send_io.pending.count); + /* + * Re-check first_error after counting ourselves in + * send_io.pending.count, and pair it with the drain in + * smbdirect_socket_destroy(): that path sets first_error, drains + * the QP and then waits for send_io.pending.count to reach 0 + * before smbdirect_connection_destroy_qp() zeroes sc->ib.qp. So + * either we observe first_error here and back off before touching + * sc->ib.qp, or our increment is observed by that wait and the QP + * stays alive across the ib_post_send() below. + */ + smp_mb__after_atomic(); + if (unlikely(sc->first_error)) { + ret = sc->first_error; + if (atomic_dec_and_test(&sc->send_io.pending.count)) + wake_up(&sc->send_io.pending.zero_wait_queue); + return ret; + } + ret = ib_post_send(sc->ib.qp, wr, NULL); if (ret) { - atomic_dec(&sc->send_io.pending.count); smbdirect_log_rdma_send(sc, SMBDIRECT_LOG_ERR, "ib_post_send() failed %1pe\n", SMBDIRECT_DEBUG_ERR_PTR(ret)); smbdirect_socket_schedule_cleanup(sc, ret); + if (atomic_dec_and_test(&sc->send_io.pending.count)) + wake_up(&sc->send_io.pending.zero_wait_queue); } return ret; @@ -1406,6 +1425,12 @@ static void smbdirect_connection_send_io_done(struct ib_cq *cq, struct ib_wc *wc * of the siblings too, which will cause * use after free problems for the others * triggered from ib_drain_qp(). + * + * We also must not drop send_io.pending.count + * here: smbdirect_connection_post_send_wr() + * counted the whole chain only once and the + * signaled final message drops it, also on + * failure, as all posted messages are flushed. */ if (wc->status != IB_WC_SUCCESS) goto skip_free; @@ -1435,6 +1460,17 @@ static void smbdirect_connection_send_io_done(struct ib_cq *cq, struct ib_wc *wc lcredits += 1; if (unlikely(wc->status != IB_WC_SUCCESS || WARN_ON_ONCE(wc->opcode != IB_WC_SEND))) { + /* + * The signaled send_io was counted in + * send_io.pending.count by + * smbdirect_connection_post_send_wr(), + * so drop it also on failure, otherwise + * smbdirect_socket_destroy() waits forever. + * The unsignaled siblings arriving via + * skip_free were not counted. + */ + if (atomic_dec_and_test(&sc->send_io.pending.count)) + wake_up(&sc->send_io.pending.zero_wait_queue); skip_free: if (wc->status != IB_WC_WR_FLUSH_ERR) smbdirect_log_rdma_send(sc, SMBDIRECT_LOG_ERR, diff --git a/fs/smb/smbdirect/socket.c b/fs/smb/smbdirect/socket.c index e270e36b3145..749a85274f97 100644 --- a/fs/smb/smbdirect/socket.c +++ b/fs/smb/smbdirect/socket.c @@ -669,6 +669,20 @@ static void smbdirect_socket_destroy(struct smbdirect_socket *sc) "freeing mr list\n"); smbdirect_connection_destroy_mr_list(sc); + /* + * first_error is set by now, so no new + * smbdirect_connection_post_send_wr() will proceed past its + * re-check. Wait for any that already counted themselves in + * send_io.pending.count to finish their ib_post_send() before we + * destroy the QP, so none of them runs on a NULL sc->ib.qp. + * + * The send completions that drop the count run on the CQ + * workqueue and don't take the handler lock we hold here, and the + * QP was already drained above, so this does not block. + */ + wait_event(sc->send_io.pending.zero_wait_queue, + atomic_read(&sc->send_io.pending.count) == 0); + if (sc->listen.backlog == -1) /* was not a listener */ smbdirect_log_rdma_event(sc, SMBDIRECT_LOG_INFO, "destroying qp\n"); -- 2.43.0