smbdirect_connection_post_send_wr() only checked sc->first_error before ib_post_send(sc->ib.qp, ...). If smbdirect_socket_release() ran concurrently, the teardown could reach 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. The owners of the socket don't do that: cifs serializes smbd_send() and smbd_destroy() with cifs_server_lock() and ksmbd waits for all requests before it frees the transport, our own work items that send are disabled at the start of smbdirect_socket_destroy(). But smbdirect neither documented that contract nor protected itself. Document it in smbdirect_socket_destroy() and add a safety net: re-check sc->first_error after incrementing send_io.pending.count, and wait in smbdirect_socket_destroy() for send_io.pending.count to reach 0 before the QP and the memory pools are destroyed. 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 Reviewed-by: Paulo Alcantara 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 | 18 +++++++++++++++++ 4 files changed, 59 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..5d995694a74e 100644 --- a/fs/smb/smbdirect/socket.c +++ b/fs/smb/smbdirect/socket.c @@ -669,6 +669,24 @@ static void smbdirect_socket_destroy(struct smbdirect_socket *sc) "freeing mr list\n"); smbdirect_connection_destroy_mr_list(sc); + /* + * smbdirect_socket_release() is only called once the owner of + * the socket no longer sends on it: cifs serializes smbd_send() + * and smbd_destroy() with cifs_server_lock() and ksmbd waits for + * all requests before it frees the transport. Our own work items + * that send were disabled above. + * + * So this is only a safety net: first_error is set by now, so + * smbdirect_connection_post_send_wr() backs off after its + * re-check, and the drain above completed all posted messages, + * this only makes sure every send_io.pending.count reference is + * dropped before the QP and the memory pools are destroyed. It + * does not block, the send completions run on the CQ workqueue + * and don't take the handler lock we hold here. + */ + 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