Linux CIFS filesystem development
 help / color / mirror / Atom feed
* [PATCH v2 00/13] smb: smbdirect: urgent fixes for teardown races, sends and MR invalidation
@ 2026-11-27 16:40 Stefan Metzmacher
  2026-11-27 16:40 ` [PATCH v2 01/13] smb: smbdirect: don't register MRs on a torn-down connection Stefan Metzmacher
                   ` (12 more replies)
  0 siblings, 13 replies; 14+ messages in thread
From: Stefan Metzmacher @ 2026-11-27 16:40 UTC (permalink / raw)
  To: Namjae Jeon, Paulo Alcantara
  Cc: metze, Ronnie Sahlberg, Shyam Prasad N, Tom Talpey, Bharath SM,
	linux-cifs, samba-technical, stable

Hi Namjae, hi Paulo,

these are fixes for fs/smb/smbdirect:

  - Don't register MRs or post sends on a connection that is being
    torn down concurrently (NULL pointer dereferences of mr->mr and
    sc->ib.qp, reported by Sashiko).

  - Handle errors of smbdirect_iter_to_sgt(), an incomplete sg table
    was registered before.

  - A send credit leaked if the wait for a recv credit to grant
    failed, so all following sends waited until the connection was
    torn down.

  - After an error in the middle of a PDU, the fragments built so far
    were still posted, so the peer appended the next PDU to the
    incomplete one. Now the connection is torn down instead, also
    for smbd_send() of the client.

  - If the peer didn't invalidate a remotely invalidated MR, e.g.
    because of an error in the server, the client also skipped the
    local invalidation, so the peer kept access to the memory.
    smbdirect now tracks the remote invalidations itself.

  - The LOCAL_INV of an MR reused the cqe of the REG_MR and
    overwrote its done handler, so a not yet polled REG_MR completion
    finished the invalidation too early and the MR went back to the
    ready list while the LOCAL_INV was still outstanding. Now the
    LOCAL_INV has its own cqe.

  - INIT_[DELAYED_]WORK() in the connect and accept code reset the
    disable count of the work items, so a disable from a concurrent
    teardown was lost and e.g. the idle timer could be queued again
    after the socket was freed. Now the teardown sets first_error
    and disables the work items under a new lock and they are only
    activated under that lock while first_error is still 0.

  - SMBDIRECT_CHECK_STATUS_{WARN,DISCONNECT}() warned about a status
    changed by a concurrent teardown, e.g. when the waiter of a
    connect was interrupted by a signal (reported by syzbot).

  - An IB_WR_REG_MR or IB_WR_LOCAL_INV posted after a status check
    that raced with smbdirect_socket_destroy() could miss the drain
    of the QP, its completion then used the mr after it was detached
    from the socket (reported by Sashiko).

  - The error paths of smbdirect_connection_send_single_iter() gave
    back credits without waking up the waiters and didn't give back
    the receive credits granted in the message that wasn't sent
    (reported by Sashiko).

  - The DMA address of the first page was used as iova of our MRs and
    sent to the peer as offset of the buffer descriptor, without an
    IOMMU these are physical addresses. Now only the offset within
    the first page is kept in the low 24 bits, the bits above up to
    bit 55 are random and the top byte stays 0. This also avoids irdma
    rejecting an MR ending exactly at 2^64 (IRDMA_AE_AMP_TO_WRAP),
    which happens with an IOMMU allocating the first 64-bit IOVA
    range at the very top.

Reports fixed by this series:

  - https://sashiko.dev/#/patchset/cover.1791225453.git.metze%40samba.org
      smb: smbdirect: don't register MRs on a torn-down connection
      smb: smbdirect: don't post sends on a torn-down connection

  - https://syzkaller.appspot.com/bug?extid=a989758ffc6f93223eb6
      smb: smbdirect: don't let INIT_[DELAYED_]WORK() undo a disable from the teardown
      smb: smbdirect: don't warn about a status changed by a concurrent teardown

  - https://sashiko.dev/#/bug/linux-4461fbd8-a522-43ee-81d1-651432a55de9
      smb: smbdirect: don't let MR work requests miss the QP drain

  - https://sashiko.dev/#/bug/linux-a2180487-c0d3-4f9a-92d7-208e032dd966
      smb: smbdirect: wake up the credit waiters after a failed send

  - https://sashiko.dev/#/bug/linux-27f04b17-0356-40b9-94be-b0b865fde826
      smb: smbdirect: give back granted receive credits if a send fails

The urgent client fixes and the ksmbd fixes are posted separately,
they are independent of these, the remaining smbdirect fixes follow
on top of all of them.

These patches were developed with the help of Claude (Anthropic),
which was used for code analysis, writing parts of the code and the
commit messages. I reviewed all changes and take full responsibility
for them.

Testing: v1 and again v2 on real hardware, client and server running
the urgent client, smbdirect and ksmbd series together, over RoCE with
mlx5 (ConnectX), irdma (Intel E810) and rxe:

  - xfstests (59 tests of the cifs and generic groups) and O_DIRECT
    reads/writes with data verification
  - removing the local address or setting the link down while a
    connection is established, the umount finishes in time
  - 10 ksmbd restarts per device while I/O is running and
    /proc/fs/cifs/DebugData is read in a loop, each restart reconnects
  - mounts killed by a signal while their RDMA connect is pending
    (the syzbot case), no tasks in D state and no warnings

Note I didn't test with iWarp as there are existing problems with it,
I have patches to fix iWarp, but I'll post them later, as they
also rely on some rdma-core changes.

As we are already late in the 7.4 cycle, I leave it up to you two to
decide if they should still go into 7.4 or into the next merge
window, all of them are marked for stable.

Changes in v2 (thanks to the review by Sashiko):

  - Patch 1: if smbdirect_connection_destroy_mr_list() already
    disabled the mr, only drop our reference, without setting
    mr->state to SMBDIRECT_MR_ERROR, which led to a second
    smbdirect_mr_io_disable_locked() with mr->socket == NULL.
  - Patch 3: fix the function name in the error message.
  - Patch 5: smbdirect_connection_send_wait_zero_pending() also waits
    for the posted messages if the connection is broken, so the
    device no longer reads the pages of the caller after it returned.
  - Patch 6: clear need_invalidate before mr->mr is deregistered, so
    smbdirect_connection_invalidate_rkey() never uses a deregistered
    mr->mr.
  - Patch 8: smbdirect_connection_recv_io_done() leaves disabling
    refill_work to smbdirect_socket_schedule_cleanup(), and refill_work
    and immediate_work are activated before the receives of the data
    transfer phase are posted, not in
    smbdirect_connection_negotiation_done(), as a receive completion
    may already queue them.
  - New patches 11-13 for the problems found by Sashiko in the
    unchanged code, the commit messages of patches 1, 4 and 5 point
    to them.
  - Dropped the Cc of Long Li, his address bounces.

v1: https://lore.kernel.org/linux-cifs/cover.1795781300.git.metze@samba.org/

Base: v7.4-rc4, like the other smb series. The patches also apply
cleanly (git am) on cifs-next (61c581ea62ee), on ksmbd-for-next
(3c31349456c5) and on both merged, with or without the urgent client
series, and build on both merged.


Stefan Metzmacher (13):
  smb: smbdirect: don't register MRs on a torn-down connection
  smb: smbdirect: don't post sends on a torn-down connection
  smb: smbdirect: handle errors from smbdirect_iter_to_sgt()
  smb: smbdirect: give back the send credit if waiting for a recv credit
    fails
  smb: smbdirect: don't post a partial PDU after a send error
  smb: smbdirect: introduce smbdirect_connection_invalidate_rkey() and
    its logic
  smb: smbdirect: use separate cqes for MR registration and invalidation
  smb: smbdirect: don't let INIT_[DELAYED_]WORK() undo a disable from
    the teardown
  smb: smbdirect: don't warn about a status changed by a concurrent
    teardown
  smb: smbdirect: don't expose DMA addresses as iova of our MRs
  smb: smbdirect: don't let MR work requests miss the QP drain
  smb: smbdirect: wake up the credit waiters after a failed send
  smb: smbdirect: give back granted receive credits if a send fails

 fs/smb/client/smbdirect.c     |  14 ++-
 fs/smb/smbdirect/accept.c     |  28 +++--
 fs/smb/smbdirect/connect.c    |  24 ++--
 fs/smb/smbdirect/connection.c | 191 +++++++++++++++++++++++++----
 fs/smb/smbdirect/internal.h   |   6 +
 fs/smb/smbdirect/listen.c     |  19 ++-
 fs/smb/smbdirect/mr.c         | 220 ++++++++++++++++++++++++++++++++--
 fs/smb/smbdirect/socket.c     | 120 ++++++++++++++-----
 fs/smb/smbdirect/socket.h     |  51 +++++++-
 include/linux/smbdirect.h     |   5 +-
 10 files changed, 584 insertions(+), 94 deletions(-)

-- 
2.43.0


^ permalink raw reply	[flat|nested] 14+ messages in thread

* [PATCH v2 01/13] smb: smbdirect: don't register MRs on a torn-down connection
  2026-11-27 16:40 [PATCH v2 00/13] smb: smbdirect: urgent fixes for teardown races, sends and MR invalidation Stefan Metzmacher
@ 2026-11-27 16:40 ` Stefan Metzmacher
  2026-11-27 16:40 ` [PATCH v2 02/13] smb: smbdirect: don't post sends " Stefan Metzmacher
                   ` (11 subsequent siblings)
  12 siblings, 0 replies; 14+ messages in thread
From: Stefan Metzmacher @ 2026-11-27 16:40 UTC (permalink / raw)
  To: linux-cifs, samba-technical
  Cc: metze, stable, Namjae Jeon, Paulo Alcantara, Tom Talpey, Sashiko

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 <linkinjeon@kernel.org>
Cc: Paulo Alcantara <pc@manguebit.org>
Cc: Tom Talpey <tom@talpey.com>
Cc: linux-cifs@vger.kernel.org
Cc: samba-technical@lists.samba.org
Reported-by: Sashiko <sashiko-bot+sashiko@kernel.org>
Closes: https://sashiko.dev/#/patchset/cover.1791225453.git.metze%40samba.org
Signed-off-by: Stefan Metzmacher <metze@samba.org>
---
 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


^ permalink raw reply related	[flat|nested] 14+ messages in thread

* [PATCH v2 02/13] smb: smbdirect: don't post sends on a torn-down connection
  2026-11-27 16:40 [PATCH v2 00/13] smb: smbdirect: urgent fixes for teardown races, sends and MR invalidation Stefan Metzmacher
  2026-11-27 16:40 ` [PATCH v2 01/13] smb: smbdirect: don't register MRs on a torn-down connection Stefan Metzmacher
@ 2026-11-27 16:40 ` Stefan Metzmacher
  2026-11-27 16:40 ` [PATCH v2 03/13] smb: smbdirect: handle errors from smbdirect_iter_to_sgt() Stefan Metzmacher
                   ` (10 subsequent siblings)
  12 siblings, 0 replies; 14+ messages in thread
From: Stefan Metzmacher @ 2026-11-27 16:40 UTC (permalink / raw)
  To: linux-cifs, samba-technical
  Cc: metze, stable, Namjae Jeon, Paulo Alcantara, Tom Talpey, Sashiko

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 <linkinjeon@kernel.org>
Cc: Paulo Alcantara <pc@manguebit.org>
Cc: Tom Talpey <tom@talpey.com>
Cc: linux-cifs@vger.kernel.org
Cc: samba-technical@lists.samba.org
Reported-by: Sashiko <sashiko-bot+sashiko@kernel.org>
Closes: https://sashiko.dev/#/patchset/cover.1791225453.git.metze%40samba.org
Signed-off-by: Stefan Metzmacher <metze@samba.org>
---
 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


^ permalink raw reply related	[flat|nested] 14+ messages in thread

* [PATCH v2 03/13] smb: smbdirect: handle errors from smbdirect_iter_to_sgt()
  2026-11-27 16:40 [PATCH v2 00/13] smb: smbdirect: urgent fixes for teardown races, sends and MR invalidation Stefan Metzmacher
  2026-11-27 16:40 ` [PATCH v2 01/13] smb: smbdirect: don't register MRs on a torn-down connection Stefan Metzmacher
  2026-11-27 16:40 ` [PATCH v2 02/13] smb: smbdirect: don't post sends " Stefan Metzmacher
@ 2026-11-27 16:40 ` Stefan Metzmacher
  2026-11-27 16:40 ` [PATCH v2 04/13] smb: smbdirect: give back the send credit if waiting for a recv credit fails Stefan Metzmacher
                   ` (9 subsequent siblings)
  12 siblings, 0 replies; 14+ messages in thread
From: Stefan Metzmacher @ 2026-11-27 16:40 UTC (permalink / raw)
  To: linux-cifs, samba-technical
  Cc: metze, stable, Namjae Jeon, Paulo Alcantara, Tom Talpey,
	David Howells

smbdirect_connection_register_mr_io() ignored the return value of
smbdirect_iter_to_sgt(), so a failing extract_iter_to_sg() went on
with an incomplete or empty sg table. Fail the registration instead.

Fixes: d08089f649a0 ("cifs: Change the I/O paths to use an iterator rather than a page list")
Cc: stable@vger.kernel.org
Cc: Namjae Jeon <linkinjeon@kernel.org>
Cc: Paulo Alcantara <pc@manguebit.org>
Cc: Tom Talpey <tom@talpey.com>
Cc: David Howells <dhowells@redhat.com>
Cc: linux-cifs@vger.kernel.org
Cc: samba-technical@lists.samba.org
Signed-off-by: Stefan Metzmacher <metze@samba.org>
---
 fs/smb/smbdirect/mr.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)

diff --git a/fs/smb/smbdirect/mr.c b/fs/smb/smbdirect/mr.c
index af613a7405e7..c50cdb1b6cc4 100644
--- a/fs/smb/smbdirect/mr.c
+++ b/fs/smb/smbdirect/mr.c
@@ -323,7 +323,13 @@ smbdirect_connection_register_mr_io(struct smbdirect_socket *sc,
 	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);
-	smbdirect_iter_to_sgt(iter, &mr->sgt, sp->max_frmr_depth);
+	ret = smbdirect_iter_to_sgt(iter, &mr->sgt, sp->max_frmr_depth);
+	if (ret < 0) {
+		smbdirect_log_rdma_mr(sc, SMBDIRECT_LOG_ERR,
+			"smbdirect_iter_to_sgt() num_pages=%u dir=%x ret=%d (%1pe)\n",
+			num_pages, mr->dir, ret, SMBDIRECT_DEBUG_ERR_PTR(ret));
+		goto dma_map_error;
+	}
 
 	num_mapped = ib_dma_map_sg(sc->ib.dev, mr->sgt.sgl, mr->sgt.nents, mr->dir);
 	if (!num_mapped) {
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 14+ messages in thread

* [PATCH v2 04/13] smb: smbdirect: give back the send credit if waiting for a recv credit fails
  2026-11-27 16:40 [PATCH v2 00/13] smb: smbdirect: urgent fixes for teardown races, sends and MR invalidation Stefan Metzmacher
                   ` (2 preceding siblings ...)
  2026-11-27 16:40 ` [PATCH v2 03/13] smb: smbdirect: handle errors from smbdirect_iter_to_sgt() Stefan Metzmacher
@ 2026-11-27 16:40 ` Stefan Metzmacher
  2026-11-27 16:40 ` [PATCH v2 05/13] smb: smbdirect: don't post a partial PDU after a send error Stefan Metzmacher
                   ` (8 subsequent siblings)
  12 siblings, 0 replies; 14+ messages in thread
From: Stefan Metzmacher @ 2026-11-27 16:40 UTC (permalink / raw)
  To: linux-cifs, samba-technical
  Cc: metze, stable, Namjae Jeon, Paulo Alcantara, Tom Talpey

If smbdirect_connection_send_single_iter() got the last send credit
and has no recv credits to grant, it waits for the refill work or for
the peer to grant new credits. If that wait failed, it jumped to
credit_failed, which only gives back the local credit, so the send
credit it already took was lost.

As it was the last send credit, sc->send_io.credits.count stays at 0,
so every following send waits until the connection is torn down,
e.g. by the keepalive timeout.

Jump to alloc_failed instead, which also gives back the send credit.

The error paths of smbdirect_connection_send_single_iter() also don't
wake up the credit waiters and don't give back granted receive
credits, that is fixed by "smb: smbdirect: wake up the credit waiters
after a failed send" and "smb: smbdirect: give back granted receive
credits if a send fails".

Fixes: 93ac432274e1 ("smb: client: fix last send credit problem causing disconnects")
Cc: stable@vger.kernel.org
Cc: Namjae Jeon <linkinjeon@kernel.org>
Cc: Paulo Alcantara <pc@manguebit.org>
Cc: Tom Talpey <tom@talpey.com>
Cc: linux-cifs@vger.kernel.org
Cc: samba-technical@lists.samba.org
Signed-off-by: Stefan Metzmacher <metze@samba.org>
---
 fs/smb/smbdirect/connection.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/fs/smb/smbdirect/connection.c b/fs/smb/smbdirect/connection.c
index e375584ab447..c50096e605ee 100644
--- a/fs/smb/smbdirect/connection.c
+++ b/fs/smb/smbdirect/connection.c
@@ -1184,8 +1184,9 @@ int smbdirect_connection_send_single_iter(struct smbdirect_socket *sc,
 					       sc->status != SMBDIRECT_SOCKET_CONNECTED);
 		if (sc->status != SMBDIRECT_SOCKET_CONNECTED)
 			ret = -ENOTCONN;
+		/* we already have a send credit to give back */
 		if (ret < 0)
-			goto credit_failed;
+			goto alloc_failed;
 
 		new_credits = smbdirect_connection_grant_recv_credits(sc);
 	}
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 14+ messages in thread

* [PATCH v2 05/13] smb: smbdirect: don't post a partial PDU after a send error
  2026-11-27 16:40 [PATCH v2 00/13] smb: smbdirect: urgent fixes for teardown races, sends and MR invalidation Stefan Metzmacher
                   ` (3 preceding siblings ...)
  2026-11-27 16:40 ` [PATCH v2 04/13] smb: smbdirect: give back the send credit if waiting for a recv credit fails Stefan Metzmacher
@ 2026-11-27 16:40 ` Stefan Metzmacher
  2026-11-27 16:40 ` [PATCH v2 06/13] smb: smbdirect: introduce smbdirect_connection_invalidate_rkey() and its logic Stefan Metzmacher
                   ` (7 subsequent siblings)
  12 siblings, 0 replies; 14+ messages in thread
From: Stefan Metzmacher @ 2026-11-27 16:40 UTC (permalink / raw)
  To: linux-cifs, samba-technical
  Cc: metze, stable, Namjae Jeon, Paulo Alcantara, Tom Talpey,
	Ronnie Sahlberg, Shyam Prasad N, Bharath SM

All fragments of a PDU are built into the same smbdirect_send_batch
by smbdirect_connection_send_single_iter(). If building a fragment
failed, e.g. because a credit wait was interrupted by a fatal signal
or a DMA mapping failed, smbdirect_connection_send_iter() and the
smbd_send() of the client still flushed the batch with is_last=true.

That posts the fragments built so far, the last of them with a
remaining_data_length that is never satisfied, fragments may also
have been posted already by a flush with is_last=false. The peer
then appends the next PDU to the incomplete one: cifs reconnects
asynchronously, ksmbd keeps the connection and its next response
continues the truncated one.

Add smbdirect_connection_send_batch_abort() and use it instead of
the final flush after an error: if nothing of the PDU was built yet,
it only gives back the batch credit, otherwise it frees the messages
not posted yet and tears down the connection, as the peer can no
longer get a consistent stream.

smbdirect_connection_send_wait_zero_pending() now also waits for all
posted messages if the connection is broken, before it returned as
soon as the status changed, which happens right away in
smbdirect_connection_send_batch_abort(), while the device could still
read the pages of the caller for fragments posted by a flush with
is_last=false. They complete with IB_WC_WR_FLUSH_ERR once the teardown
moved the QP to the error state.

smbdirect_connection_send_single_iter() gives back the batch credit
itself on its error paths, without waking up the waiters, so
smbdirect_connection_send_batch_abort() doesn't see it, that is fixed
by "smb: smbdirect: wake up the credit waiters after a failed send".

Fixes: 2c1ac39ce9cd ("smb: client: use smbdirect_send_batch processing")
Fixes: 54abc694ebb8 ("smb: smbdirect: introduce smbdirect_connection_send_iter() and related functions")
Cc: stable@vger.kernel.org
Cc: Namjae Jeon <linkinjeon@kernel.org>
Cc: Paulo Alcantara <pc@manguebit.org>
Cc: Tom Talpey <tom@talpey.com>
Cc: Ronnie Sahlberg <ronniesahlberg@gmail.com>
Cc: Shyam Prasad N <sprasad@microsoft.com>
Cc: Bharath SM <bharathsm@microsoft.com>
Cc: linux-cifs@vger.kernel.org
Cc: samba-technical@lists.samba.org
Signed-off-by: Stefan Metzmacher <metze@samba.org>
---
 fs/smb/client/smbdirect.c     | 14 +++++-
 fs/smb/smbdirect/connection.c | 81 ++++++++++++++++++++++++++++++++---
 fs/smb/smbdirect/socket.h     |  7 +++
 include/linux/smbdirect.h     |  3 ++
 4 files changed, 98 insertions(+), 7 deletions(-)

diff --git a/fs/smb/client/smbdirect.c b/fs/smb/client/smbdirect.c
index 563ef488a225..64d27d00079d 100644
--- a/fs/smb/client/smbdirect.c
+++ b/fs/smb/client/smbdirect.c
@@ -485,9 +485,19 @@ int smbd_send(struct TCP_Server_Info *server,
 
 	} while (++rqst_idx < num_rqst);
 
-	rc = smbdirect_connection_send_batch_flush(sc, batch, true);
-	if (unlikely(!rc && error))
+	if (unlikely(error)) {
+		/*
+		 * Don't post a partial PDU, if some of it was
+		 * already built, this tears down the connection.
+		 * Fragments may have been posted already, so we
+		 * still wait for them below, they may use the
+		 * pages of the request.
+		 */
+		smbdirect_connection_send_batch_abort(sc, batch);
 		rc = error;
+	} else {
+		rc = smbdirect_connection_send_batch_flush(sc, batch, true);
+	}
 
 	/*
 	 * As an optimization, we don't wait for individual I/O to finish
diff --git a/fs/smb/smbdirect/connection.c b/fs/smb/smbdirect/connection.c
index c50096e605ee..9a824f4f76c5 100644
--- a/fs/smb/smbdirect/connection.c
+++ b/fs/smb/smbdirect/connection.c
@@ -925,6 +925,7 @@ static void smbdirect_connection_send_batch_init(struct smbdirect_send_batch *ba
 	batch->need_invalidate_rkey = need_invalidate_rkey;
 	batch->remote_key = remote_key;
 	batch->credit = 0;
+	batch->msg_cnt = 0;
 }
 
 int smbdirect_connection_send_batch_flush(struct smbdirect_socket *sc,
@@ -988,6 +989,58 @@ int smbdirect_connection_send_batch_flush(struct smbdirect_socket *sc,
 }
 EXPORT_SYMBOL_GPL(smbdirect_connection_send_batch_flush);
 
+/*
+ * Abort the PDU of a batch after an error, instead of flushing it with
+ * is_last=true.
+ *
+ * All fragments of a PDU use the same batch. If some of them were
+ * already added to the batch, possibly posted by a flush with
+ * is_last=false, the peer can no longer get a complete PDU, its
+ * remaining_data_length would just continue with the next PDU. So the
+ * messages not posted yet are freed and the connection is torn down.
+ *
+ * If nothing of the PDU was built yet, e.g. the wait for the first
+ * credit failed, only the batch credit is given back.
+ */
+void smbdirect_connection_send_batch_abort(struct smbdirect_socket *sc,
+					   struct smbdirect_send_batch *batch)
+{
+	struct smbdirect_send_io *msg, *next;
+	u32 freed = 0;
+
+	list_for_each_entry_safe(msg, next, &batch->msg_list, sibling_list) {
+		list_del_init(&msg->sibling_list);
+		smbdirect_connection_free_send_io(msg);
+		freed++;
+	}
+	batch->wr_cnt = 0;
+
+	/*
+	 * Each message took a local and a send credit,
+	 * see smbdirect_connection_send_single_iter().
+	 */
+	if (freed) {
+		atomic_add(freed, &sc->send_io.credits.count);
+		wake_up(&sc->send_io.credits.wait_queue);
+		atomic_add(freed, &sc->send_io.lcredits.count);
+		wake_up(&sc->send_io.lcredits.wait_queue);
+	}
+
+	if (batch->credit) {
+		atomic_add(batch->credit, &sc->send_io.bcredits.count);
+		batch->credit = 0;
+		wake_up(&sc->send_io.bcredits.wait_queue);
+	}
+
+	if (batch->msg_cnt) {
+		smbdirect_log_write(sc, SMBDIRECT_LOG_ERR,
+			"aborting a partial PDU after %u message(s)\n",
+			batch->msg_cnt);
+		smbdirect_socket_schedule_cleanup(sc, -ECONNABORTED);
+	}
+}
+EXPORT_SYMBOL_GPL(smbdirect_connection_send_batch_abort);
+
 struct smbdirect_send_batch *
 smbdirect_init_send_batch_storage(struct smbdirect_send_batch_storage *storage,
 				  bool need_invalidate_rkey,
@@ -1097,6 +1150,7 @@ static int smbdirect_connection_post_send_io(struct smbdirect_socket *sc,
 		}
 		list_add_tail(&msg->sibling_list, &batch->msg_list);
 		batch->wr_cnt++;
+		batch->msg_cnt++;
 		return 0;
 	}
 
@@ -1303,9 +1357,15 @@ int smbdirect_connection_send_wait_zero_pending(struct smbdirect_socket *sc)
 	 * that means all the I/Os have been out and we are good to return
 	 */
 
+	/*
+	 * Also wait if the connection is broken: the device may still
+	 * read the pages of the caller for posted messages, e.g. earlier
+	 * fragments of a PDU that smbdirect_connection_send_batch_abort()
+	 * gave up. Once the teardown moved the QP to the error state,
+	 * they complete with IB_WC_WR_FLUSH_ERR.
+	 */
 	wait_event(sc->send_io.pending.zero_wait_queue,
-		   atomic_read(&sc->send_io.pending.count) == 0 ||
-		   sc->status != SMBDIRECT_SOCKET_CONNECTED);
+		   atomic_read(&sc->send_io.pending.count) == 0);
 	if (sc->status != SMBDIRECT_SOCKET_CONNECTED) {
 		smbdirect_log_write(sc, SMBDIRECT_LOG_ERR,
 			"status=%s first_error=%1pe => %1pe\n",
@@ -1382,9 +1442,20 @@ int smbdirect_connection_send_iter(struct smbdirect_socket *sc,
 		}
 	}
 
-	ret = smbdirect_connection_send_batch_flush(sc, &batch, true);
-	if (unlikely(ret && !error))
-		error = ret;
+	if (unlikely(error)) {
+		/*
+		 * Don't post a partial PDU, see
+		 * smbdirect_connection_send_batch_abort().
+		 * Fragments may have been posted already, so we
+		 * still wait for them below, they may use the
+		 * pages of the caller.
+		 */
+		smbdirect_connection_send_batch_abort(sc, &batch);
+	} else {
+		ret = smbdirect_connection_send_batch_flush(sc, &batch, true);
+		if (unlikely(ret))
+			error = ret;
+	}
 
 	/*
 	 * As an optimization, we don't wait for individual I/O to finish
diff --git a/fs/smb/smbdirect/socket.h b/fs/smb/smbdirect/socket.h
index 14114a2567a2..dc1e906316ff 100644
--- a/fs/smb/smbdirect/socket.h
+++ b/fs/smb/smbdirect/socket.h
@@ -783,6 +783,13 @@ struct smbdirect_send_batch {
 	u32 remote_key;
 
 	int credit;
+
+	/*
+	 * Number of messages added to the batch,
+	 * unlike wr_cnt not reset by a flush, see
+	 * smbdirect_connection_send_batch_abort().
+	 */
+	u32 msg_cnt;
 };
 
 struct smbdirect_recv_io {
diff --git a/include/linux/smbdirect.h b/include/linux/smbdirect.h
index 97f5ba730fa7..74fff00cdf88 100644
--- a/include/linux/smbdirect.h
+++ b/include/linux/smbdirect.h
@@ -115,6 +115,9 @@ int smbdirect_connection_send_batch_flush(struct smbdirect_socket *sc,
 					  struct smbdirect_send_batch *batch,
 					  bool is_last);
 
+void smbdirect_connection_send_batch_abort(struct smbdirect_socket *sc,
+					   struct smbdirect_send_batch *batch);
+
 /*
  * This is only temporary and only needed
  * as long as the client still requires
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 14+ messages in thread

* [PATCH v2 06/13] smb: smbdirect: introduce smbdirect_connection_invalidate_rkey() and its logic
  2026-11-27 16:40 [PATCH v2 00/13] smb: smbdirect: urgent fixes for teardown races, sends and MR invalidation Stefan Metzmacher
                   ` (4 preceding siblings ...)
  2026-11-27 16:40 ` [PATCH v2 05/13] smb: smbdirect: don't post a partial PDU after a send error Stefan Metzmacher
@ 2026-11-27 16:40 ` Stefan Metzmacher
  2026-11-27 16:40 ` [PATCH v2 07/13] smb: smbdirect: use separate cqes for MR registration and invalidation Stefan Metzmacher
                   ` (6 subsequent siblings)
  12 siblings, 0 replies; 14+ messages in thread
From: Stefan Metzmacher @ 2026-11-27 16:40 UTC (permalink / raw)
  To: linux-cifs, samba-technical
  Cc: metze, stable, Namjae Jeon, Paulo Alcantara, Tom Talpey

We remember all smbdirect_mr_io structures with their base_tag
in a per connection xarray to find them again.

This keeps track of remote invalidations done by the peer
and skips the IB_WR_LOCAL_INV in such a case.

Note we now ignore the 'need_invalidate' argument
of smbdirect_connection_register_mr_io(), it will
be removed in a follow up commit.

The logic to let the caller decide and pass in
'need_invalidate' is wrong as there can be errors
in the server, so that it may skip the remote invalidation.

Now we explicitly track remote invalidations by
looking at the IB_WC_WITH_INVALIDATE flag in
smbdirect_connection_recv_io_done().

Remote invalidations we can't match are only logged at the info
level: they are races with our own deregistration or keys we don't
track, the MR is then invalidated locally as before, and as error
messages a peer could flood the log with them.

smbdirect_mr_io_disable_locked() clears need_invalidate under
sc->mr_io.all.lock before it deregisters mr->mr, so a concurrent
smbdirect_connection_invalidate_rkey() for a receive with
IB_WC_WITH_INVALIDATE never looks at a deregistered mr->mr.

Fixes: c7398583340a ("CIFS: SMBD: Implement RDMA memory registration")
Cc: stable@vger.kernel.org
Cc: Namjae Jeon <linkinjeon@kernel.org>
Cc: Paulo Alcantara <pc@manguebit.org>
Cc: Tom Talpey <tom@talpey.com>
Cc: linux-cifs@vger.kernel.org
Cc: samba-technical@lists.samba.org
Signed-off-by: Stefan Metzmacher <metze@samba.org>
---
 fs/smb/smbdirect/connection.c |  12 ++--
 fs/smb/smbdirect/internal.h   |   2 +
 fs/smb/smbdirect/mr.c         | 119 ++++++++++++++++++++++++++++++++--
 fs/smb/smbdirect/socket.h     |   3 +
 include/linux/smbdirect.h     |   2 +-
 5 files changed, 127 insertions(+), 11 deletions(-)

diff --git a/fs/smb/smbdirect/connection.c b/fs/smb/smbdirect/connection.c
index 9a824f4f76c5..871750529a98 100644
--- a/fs/smb/smbdirect/connection.c
+++ b/fs/smb/smbdirect/connection.c
@@ -1646,10 +1646,14 @@ void smbdirect_connection_recv_io_done(struct ib_cq *cq, struct ib_wc *wc)
 	}
 
 	smbdirect_log_rdma_recv(sc, SMBDIRECT_LOG_INFO,
-		"recv_io=0x%p type=%d wc status=%s wc opcode %d byte_len=%d pkey_index=%u\n",
-		recv_io, sc->recv_io.expected,
-		ib_wc_status_msg(wc->status), wc->opcode,
-		wc->byte_len, wc->pkey_index);
+		"recv_io=0x%p status=%s opcode %d byte_len=%d wc_flags=0x%x inv_rkey=0x%x\n",
+		recv_io, ib_wc_status_msg(wc->status), wc->opcode,
+		wc->byte_len, wc->wc_flags,
+		(wc->wc_flags & IB_WC_WITH_INVALIDATE) ?
+		wc->ex.invalidate_rkey : 0);
+
+	if (wc->wc_flags & IB_WC_WITH_INVALIDATE)
+		smbdirect_connection_invalidate_rkey(sc, wc->ex.invalidate_rkey);
 
 	/*
 	 * Reset timer to the keepalive interval in
diff --git a/fs/smb/smbdirect/internal.h b/fs/smb/smbdirect/internal.h
index 69d0c9f899de..fec71278421a 100644
--- a/fs/smb/smbdirect/internal.h
+++ b/fs/smb/smbdirect/internal.h
@@ -128,6 +128,8 @@ void smbdirect_connection_recv_io_done(struct ib_cq *cq, struct ib_wc *wc);
 
 int smbdirect_connection_recv_io_refill(struct smbdirect_socket *sc);
 
+void smbdirect_connection_invalidate_rkey(struct smbdirect_socket *sc, u32 rkey);
+
 int smbdirect_connection_create_mr_list(struct smbdirect_socket *sc);
 
 void smbdirect_connection_destroy_mr_list(struct smbdirect_socket *sc);
diff --git a/fs/smb/smbdirect/mr.c b/fs/smb/smbdirect/mr.c
index c50cdb1b6cc4..7c7607c26b0b 100644
--- a/fs/smb/smbdirect/mr.c
+++ b/fs/smb/smbdirect/mr.c
@@ -60,6 +60,25 @@ int smbdirect_connection_create_mr_list(struct smbdirect_socket *sc)
 		mr->socket = sc;
 
 		list_add_tail(&mr->list, &sc->mr_io.all.list);
+
+		mr->base_tag = mr->mr->rkey & ~U8_MAX;
+		ret = xa_insert_irq(&sc->mr_io.all.tags,
+				    mr->base_tag,
+				    mr,
+				    GFP_KERNEL);
+		if (ret) {
+			smbdirect_log_rdma_mr(sc, SMBDIRECT_LOG_ERR,
+				"xa_insert_irq() for base_tag=0x%x failed: ret=%d (%1pe)\n",
+				mr->base_tag,
+				ret, SMBDIRECT_DEBUG_ERR_PTR(ret));
+			/*
+			 * Already added to sc->mr_io.all.list,
+			 * so below we can just call
+			 * smbdirect_connection_destroy_mr_list()
+			 */
+			goto xa_insert_failed;
+		}
+
 		atomic_inc(&sc->mr_io.ready.count);
 	}
 
@@ -71,6 +90,7 @@ int smbdirect_connection_create_mr_list(struct smbdirect_socket *sc)
 	mutex_destroy(&mr->mutex);
 	kfree(mr);
 kzalloc_mr_failed:
+xa_insert_failed:
 	smbdirect_connection_destroy_mr_list(sc);
 	return ret;
 }
@@ -78,12 +98,23 @@ int smbdirect_connection_create_mr_list(struct smbdirect_socket *sc)
 static void smbdirect_mr_io_disable_locked(struct smbdirect_mr_io *mr)
 {
 	struct smbdirect_socket *sc = mr->socket;
+	unsigned long flags;
 
 	lockdep_assert_held(&mr->mutex);
 
 	if (mr->state == SMBDIRECT_MR_DISABLED)
 		return;
 
+	/*
+	 * smbdirect_connection_invalidate_rkey() only looks at mr->mr
+	 * while need_invalidate is set, so clear it before mr->mr
+	 * is deregistered.
+	 */
+	spin_lock_irqsave(&sc->mr_io.all.lock, flags);
+	mr->state = SMBDIRECT_MR_DISABLED;
+	mr->need_invalidate = false;
+	spin_unlock_irqrestore(&sc->mr_io.all.lock, flags);
+
 	if (mr->mr)
 		ib_dereg_mr(mr->mr);
 	if (mr->sgt.nents)
@@ -93,8 +124,6 @@ static void smbdirect_mr_io_disable_locked(struct smbdirect_mr_io *mr)
 	mr->mr = NULL;
 	mr->sgt.sgl = NULL;
 	mr->sgt.nents = 0;
-
-	mr->state = SMBDIRECT_MR_DISABLED;
 }
 
 static void smbdirect_mr_io_free_locked(struct kref *kref)
@@ -121,6 +150,11 @@ void smbdirect_connection_destroy_mr_list(struct smbdirect_socket *sc)
 	LIST_HEAD(all_list);
 	unsigned long flags;
 
+	/*
+	 * This means we don't need explicit
+	 * xa_erase() calls below.
+	 */
+	xa_destroy(&sc->mr_io.all.tags);
 	spin_lock_irqsave(&sc->mr_io.all.lock, flags);
 	list_splice_tail_init(&sc->mr_io.all.list, &all_list);
 	spin_unlock_irqrestore(&sc->mr_io.all.lock, flags);
@@ -265,12 +299,13 @@ struct smbdirect_mr_io *
 smbdirect_connection_register_mr_io(struct smbdirect_socket *sc,
 				    struct iov_iter *iter,
 				    bool writing,
-				    bool need_invalidate)
+				    bool _ignored_need_invalidate)
 {
 	const struct smbdirect_socket_parameters *sp = &sc->parameters;
 	struct smbdirect_mr_io *mr;
 	int ret, num_pages, num_mapped;
 	struct ib_reg_wr *reg_wr;
+	unsigned long flags;
 
 	num_pages = iov_iter_npages(iter, sp->max_frmr_depth + 1);
 	if (num_pages > sp->max_frmr_depth) {
@@ -291,7 +326,6 @@ smbdirect_connection_register_mr_io(struct smbdirect_socket *sc,
 	mutex_lock(&mr->mutex);
 
 	mr->dir = writing ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
-	mr->need_invalidate = need_invalidate;
 	mr->sgt.nents = 0;
 	mr->sgt.orig_nents = 0;
 
@@ -351,6 +385,8 @@ smbdirect_connection_register_mr_io(struct smbdirect_socket *sc,
 	}
 
 	ib_update_fast_reg_key(mr->mr, ib_inc_rkey(mr->mr->rkey));
+	if (mr->mr->rkey == mr->base_tag)
+		ib_update_fast_reg_key(mr->mr, ib_inc_rkey(mr->mr->rkey));
 	reg_wr = &mr->wr;
 	reg_wr->wr.opcode = IB_WR_REG_MR;
 	mr->cqe.done = smbdirect_connection_mr_io_register_done;
@@ -363,6 +399,10 @@ smbdirect_connection_register_mr_io(struct smbdirect_socket *sc,
 			IB_ACCESS_REMOTE_WRITE | IB_ACCESS_LOCAL_WRITE :
 			IB_ACCESS_REMOTE_READ;
 
+	spin_lock_irqsave(&sc->mr_io.all.lock, flags);
+	mr->need_invalidate = true;
+	spin_unlock_irqrestore(&sc->mr_io.all.lock, flags);
+
 	/*
 	 * 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
@@ -380,6 +420,10 @@ smbdirect_connection_register_mr_io(struct smbdirect_socket *sc,
 		return mr;
 	}
 
+	spin_lock_irqsave(&sc->mr_io.all.lock, flags);
+	mr->need_invalidate = false;
+	spin_unlock_irqrestore(&sc->mr_io.all.lock, flags);
+
 	smbdirect_log_rdma_mr(sc, SMBDIRECT_LOG_ERR,
 		"ib_post_send failed ret=%d (%1pe) reg_wr->key=0x%x\n",
 		ret, SMBDIRECT_DEBUG_ERR_PTR(ret), reg_wr->key);
@@ -443,6 +487,8 @@ 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;
+	bool need_invalidate;
+	unsigned long flags;
 	int ret = 0;
 
 lock_again:
@@ -455,7 +501,12 @@ void smbdirect_connection_deregister_mr_io(struct smbdirect_mr_io *mr)
 		goto put_kref;
 	}
 
-	if (mr->need_invalidate) {
+	spin_lock_irqsave(&sc->mr_io.all.lock, flags);
+	need_invalidate = mr->need_invalidate;
+	mr->need_invalidate = false;
+	spin_unlock_irqrestore(&sc->mr_io.all.lock, flags);
+
+	if (need_invalidate) {
 		struct ib_send_wr *wr = &mr->inv_wr;
 
 		/* Need to finish local invalidation before returning */
@@ -483,7 +534,6 @@ void smbdirect_connection_deregister_mr_io(struct smbdirect_mr_io *mr)
 		 */
 		mutex_unlock(&mr->mutex);
 		wait_for_completion(&mr->invalidate_done);
-		mr->need_invalidate = false;
 		goto lock_again;
 	} else
 		/*
@@ -526,3 +576,60 @@ void smbdirect_connection_deregister_mr_io(struct smbdirect_mr_io *mr)
 		mutex_unlock(&mr->mutex);
 }
 EXPORT_SYMBOL_GPL(smbdirect_connection_deregister_mr_io);
+
+void smbdirect_connection_invalidate_rkey(struct smbdirect_socket *sc, u32 rkey)
+{
+	u32 base_tag = rkey & ~U8_MAX;
+	struct smbdirect_mr_io *mr;
+	unsigned long flags;
+
+	/*
+	 * This might be called from an interrupt context,
+	 * so we can't call mutex_lock(&mr->mutex); here.
+	 *
+	 * We run under the sc->mr_io.all.lock spin_lock
+	 */
+	spin_lock_irqsave(&sc->mr_io.all.lock, flags);
+
+	mr = xa_load(&sc->mr_io.all.tags, base_tag);
+	if (!mr) {
+		smbdirect_log_rdma_mr(sc, SMBDIRECT_LOG_INFO,
+			"xa_load() for base_tag=0x%x rkey=0x%x failed\n",
+			base_tag, rkey);
+		goto done;
+	}
+
+	if (!mr->need_invalidate) {
+		smbdirect_log_rdma_mr(sc, SMBDIRECT_LOG_INFO,
+			"invalidate rkey=0x%x for base_tag=0x%x not registered\n",
+			rkey, base_tag);
+		goto done;
+	}
+
+	if (WARN_ON_ONCE(mr->base_tag != base_tag))
+		goto done; /* internal error */
+
+	if (WARN_ON_ONCE(!mr->mr))
+		goto done; /* internal error */
+
+	if (mr->mr->rkey != rkey) {
+		/*
+		 * Maybe a local invalidate was faster
+		 * than the remote invalidate
+		 */
+		smbdirect_log_rdma_mr(sc, SMBDIRECT_LOG_INFO,
+			"invalidate rkey=0x%x for base_tag=0x%x stale rkey=0x%x\n",
+			rkey, base_tag, mr->mr->rkey);
+		goto done;
+	}
+
+	/*
+	 * We can skip IB_WR_LOCAL_INV in
+	 * smbdirect_connection_deregister_mr_io()
+	 */
+	mr->state = SMBDIRECT_MR_INVALIDATED;
+	mr->need_invalidate = false;
+
+done:
+	spin_unlock_irqrestore(&sc->mr_io.all.lock, flags);
+}
diff --git a/fs/smb/smbdirect/socket.h b/fs/smb/smbdirect/socket.h
index dc1e906316ff..4dcbd9c86aaf 100644
--- a/fs/smb/smbdirect/socket.h
+++ b/fs/smb/smbdirect/socket.h
@@ -411,6 +411,7 @@ struct smbdirect_socket {
 		 */
 		struct {
 			struct list_head list;
+			struct xarray tags;
 			spinlock_t lock;
 		} all;
 
@@ -671,6 +672,7 @@ static __always_inline void smbdirect_socket_init(struct smbdirect_socket *sc)
 
 	spin_lock_init(&sc->mr_io.all.lock);
 	INIT_LIST_HEAD(&sc->mr_io.all.list);
+	xa_init_flags(&sc->mr_io.all.tags, XA_FLAGS_LOCK_IRQ);
 	atomic_set(&sc->mr_io.ready.count, 0);
 	init_waitqueue_head(&sc->mr_io.ready.wait_queue);
 	atomic_set(&sc->mr_io.used.count, 0);
@@ -837,6 +839,7 @@ struct smbdirect_mr_io {
 	struct list_head list;
 
 	enum smbdirect_mr_state state;
+	u32 base_tag;
 	struct ib_mr *mr;
 	struct sg_table sgt;
 	enum dma_data_direction dir;
diff --git a/include/linux/smbdirect.h b/include/linux/smbdirect.h
index 74fff00cdf88..f797cba849cd 100644
--- a/include/linux/smbdirect.h
+++ b/include/linux/smbdirect.h
@@ -175,7 +175,7 @@ struct smbdirect_mr_io *
 smbdirect_connection_register_mr_io(struct smbdirect_socket *sc,
 				    struct iov_iter *iter,
 				    bool writing,
-				    bool need_invalidate);
+				    bool _ignored_need_invalidate);
 
 void smbdirect_mr_io_fill_buffer_descriptor(struct smbdirect_mr_io *mr,
 					    struct smbdirect_buffer_descriptor_v1 *v1);
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 14+ messages in thread

* [PATCH v2 07/13] smb: smbdirect: use separate cqes for MR registration and invalidation
  2026-11-27 16:40 [PATCH v2 00/13] smb: smbdirect: urgent fixes for teardown races, sends and MR invalidation Stefan Metzmacher
                   ` (5 preceding siblings ...)
  2026-11-27 16:40 ` [PATCH v2 06/13] smb: smbdirect: introduce smbdirect_connection_invalidate_rkey() and its logic Stefan Metzmacher
@ 2026-11-27 16:40 ` Stefan Metzmacher
  2026-11-27 16:40 ` [PATCH v2 08/13] smb: smbdirect: don't let INIT_[DELAYED_]WORK() undo a disable from the teardown Stefan Metzmacher
                   ` (5 subsequent siblings)
  12 siblings, 0 replies; 14+ messages in thread
From: Stefan Metzmacher @ 2026-11-27 16:40 UTC (permalink / raw)
  To: linux-cifs, samba-technical
  Cc: metze, stable, Namjae Jeon, Paulo Alcantara, Tom Talpey,
	Ronnie Sahlberg

smbdirect_connection_register_mr_io() posts a signaled IB_WR_REG_MR
with mr->cqe.done = smbdirect_connection_mr_io_register_done, and
smbdirect_connection_deregister_mr_io() reused the same mr->cqe for
IB_WR_LOCAL_INV, overwriting cqe.done with
smbdirect_connection_mr_io_local_inv_done.

If the deregistration happens before the REG_MR completion was
polled, that completion already ran local_inv_done(): it set the mr
to SMBDIRECT_MR_INVALIDATED and completed invalidate_done too early,
so the mr went back to SMBDIRECT_MR_READY (and ready.count++) while
the LOCAL_INV was still outstanding. Its real completion later set the
already reused mr to SMBDIRECT_MR_INVALIDATED again, leaving
ready.count counting mrs that are no longer READY, which can make
smbdirect_connection_get_mr_io() spin forever.

Rename mr->cqe to mr->reg_cqe and add a separate mr->inv_cqe for the
LOCAL_INV.

Fixes: c7398583340a ("CIFS: SMBD: Implement RDMA memory registration")
Cc: stable@vger.kernel.org
Cc: Namjae Jeon <linkinjeon@kernel.org>
Cc: Paulo Alcantara <pc@manguebit.org>
Cc: Tom Talpey <tom@talpey.com>
Cc: Ronnie Sahlberg <ronniesahlberg@gmail.com>
Cc: linux-cifs@vger.kernel.org
Cc: samba-technical@lists.samba.org
Signed-off-by: Stefan Metzmacher <metze@samba.org>
---
 fs/smb/smbdirect/mr.c     | 12 ++++++------
 fs/smb/smbdirect/socket.h |  9 ++++++++-
 2 files changed, 14 insertions(+), 7 deletions(-)

diff --git a/fs/smb/smbdirect/mr.c b/fs/smb/smbdirect/mr.c
index 7c7607c26b0b..68b9ae5ec0c1 100644
--- a/fs/smb/smbdirect/mr.c
+++ b/fs/smb/smbdirect/mr.c
@@ -242,7 +242,7 @@ smbdirect_connection_get_mr_io(struct smbdirect_socket *sc)
 static void smbdirect_connection_mr_io_register_done(struct ib_cq *cq, struct ib_wc *wc)
 {
 	struct smbdirect_mr_io *mr =
-		container_of(wc->wr_cqe, struct smbdirect_mr_io, cqe);
+		container_of(wc->wr_cqe, struct smbdirect_mr_io, reg_cqe);
 	struct smbdirect_socket *sc = mr->socket;
 
 	if (wc->status != IB_WC_SUCCESS) {
@@ -256,7 +256,7 @@ static void smbdirect_connection_mr_io_register_done(struct ib_cq *cq, struct ib
 static void smbdirect_connection_mr_io_local_inv_done(struct ib_cq *cq, struct ib_wc *wc)
 {
 	struct smbdirect_mr_io *mr =
-		container_of(wc->wr_cqe, struct smbdirect_mr_io, cqe);
+		container_of(wc->wr_cqe, struct smbdirect_mr_io, inv_cqe);
 	struct smbdirect_socket *sc = mr->socket;
 
 	mr->state = SMBDIRECT_MR_INVALIDATED;
@@ -389,8 +389,8 @@ smbdirect_connection_register_mr_io(struct smbdirect_socket *sc,
 		ib_update_fast_reg_key(mr->mr, ib_inc_rkey(mr->mr->rkey));
 	reg_wr = &mr->wr;
 	reg_wr->wr.opcode = IB_WR_REG_MR;
-	mr->cqe.done = smbdirect_connection_mr_io_register_done;
-	reg_wr->wr.wr_cqe = &mr->cqe;
+	mr->reg_cqe.done = smbdirect_connection_mr_io_register_done;
+	reg_wr->wr.wr_cqe = &mr->reg_cqe;
 	reg_wr->wr.num_sge = 0;
 	reg_wr->wr.send_flags = IB_SEND_SIGNALED;
 	reg_wr->mr = mr->mr;
@@ -511,8 +511,8 @@ void smbdirect_connection_deregister_mr_io(struct smbdirect_mr_io *mr)
 
 		/* Need to finish local invalidation before returning */
 		wr->opcode = IB_WR_LOCAL_INV;
-		mr->cqe.done = smbdirect_connection_mr_io_local_inv_done;
-		wr->wr_cqe = &mr->cqe;
+		mr->inv_cqe.done = smbdirect_connection_mr_io_local_inv_done;
+		wr->wr_cqe = &mr->inv_cqe;
 		wr->num_sge = 0;
 		wr->ex.invalidate_rkey = mr->mr->rkey;
 		wr->send_flags = IB_SEND_SIGNALED;
diff --git a/fs/smb/smbdirect/socket.h b/fs/smb/smbdirect/socket.h
index 4dcbd9c86aaf..1fc4aebcdda5 100644
--- a/fs/smb/smbdirect/socket.h
+++ b/fs/smb/smbdirect/socket.h
@@ -826,7 +826,14 @@ enum smbdirect_mr_state {
 
 struct smbdirect_mr_io {
 	struct smbdirect_socket *socket;
-	struct ib_cqe cqe;
+	/*
+	 * Separate completion entries for IB_WR_REG_MR and IB_WR_LOCAL_INV:
+	 * smbdirect_connection_deregister_mr_io() can post the LOCAL_INV
+	 * before the REG_MR completion was polled, and with a shared entry
+	 * that completion would then already run the LOCAL_INV handler.
+	 */
+	struct ib_cqe reg_cqe;
+	struct ib_cqe inv_cqe;
 
 	/*
 	 * We can have up to two references:
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 14+ messages in thread

* [PATCH v2 08/13] smb: smbdirect: don't let INIT_[DELAYED_]WORK() undo a disable from the teardown
  2026-11-27 16:40 [PATCH v2 00/13] smb: smbdirect: urgent fixes for teardown races, sends and MR invalidation Stefan Metzmacher
                   ` (6 preceding siblings ...)
  2026-11-27 16:40 ` [PATCH v2 07/13] smb: smbdirect: use separate cqes for MR registration and invalidation Stefan Metzmacher
@ 2026-11-27 16:40 ` Stefan Metzmacher
  2026-11-27 16:40 ` [PATCH v2 09/13] smb: smbdirect: don't warn about a status changed by a concurrent teardown Stefan Metzmacher
                   ` (4 subsequent siblings)
  12 siblings, 0 replies; 14+ messages in thread
From: Stefan Metzmacher @ 2026-11-27 16:40 UTC (permalink / raw)
  To: linux-cifs, samba-technical
  Cc: metze, stable, Namjae Jeon, Paulo Alcantara, Tom Talpey,
	syzbot+a989758ffc6f93223eb6

smbdirect_socket_init() initializes all work items as disabled and
the connect and accept code activates them with INIT_WORK() or
INIT_DELAYED_WORK() once they are needed, e.g.
smbdirect_connect_rdma_connect() for the idle timer.

But INIT_[DELAYED_]WORK() also resets the disable count. The teardown
in __smbdirect_socket_schedule_cleanup() and smbdirect_socket_destroy()
disables the work items without holding the locks of the rdma cm event
handler or the recv done handlers, so it can disable a work item
between the status check of a handler and its INIT_[DELAYED_]WORK().
Then the work item is enabled again and queued, e.g. the idle timer by
mod_delayed_work() in smbdirect_connect_rdma_connect(), after
smbdirect_socket_destroy() already disabled it with
disable_delayed_work_sync(), so it can run after the socket is freed.

syzbot found this together with a warning in
smbdirect_connect_rdma_event_handler(), which is fixed in the next
commit.

So set first_error and disable the work items together under the new
sc->cleanup_lock in the teardown and only activate them under the same
lock while first_error is still 0: either the activation happens
before the disable or it sees first_error and leaves the work item
disabled. So the teardown only needs to disable them once, when it
sets first_error. The final disable_[delayed_]work_sync() in
smbdirect_socket_destroy() doesn't need the lock, as first_error is
always set before. The failure of rdma_listen() in
smbdirect_socket_listen() now uses smbdirect_socket_schedule_cleanup()
instead of setting first_error and the status itself, so that
first_error is always only written with cleanup_lock held.

sc->cleanup_lock replaces sc->connect.lock, which was only used for the
activation of sc->connect.work, but the teardown didn't take it.

smbdirect_connection_recv_io_done() no longer disables refill_work on
its own before smbdirect_socket_schedule_cleanup(), a concurrent
activation would undo that too, it calls
smbdirect_socket_schedule_cleanup() first, which disables it under
cleanup_lock before smbdirect_connection_put_recv_io() could queue it.

The activation of refill_work and immediate_work moves from
smbdirect_connection_negotiation_done() to the new
smbdirect_connection_init_data_transfer_works(), called before the
receives of the data transfer phase are posted:
smbdirect_connection_recv_io_done() may queue them for the first data
transfer message, on the accept side before the send completion of the
negotiate response calls smbdirect_connection_negotiation_done(), and
INIT_WORK() on a work item that may be queued concurrently corrupts it.

Fixes: d1f187656797 ("smb: smbdirect: introduce smbdirect_connect[_sync]()")
Fixes: eb3ed1e9048c ("smb: smbdirect: introduce smbdirect_accept_connect_request()")
Fixes: 099db5093e4d ("smb: smbdirect: introduce smbdirect_connection_negotiation_done()")
Cc: stable@vger.kernel.org
Cc: Namjae Jeon <linkinjeon@kernel.org>
Cc: Paulo Alcantara <pc@manguebit.org>
Cc: Tom Talpey <tom@talpey.com>
Cc: linux-cifs@vger.kernel.org
Cc: samba-technical@lists.samba.org
Reported-by: syzbot+a989758ffc6f93223eb6@syzkaller.appspotmail.com
Closes: https://syzkaller.appspot.com/bug?extid=a989758ffc6f93223eb6
Signed-off-by: Stefan Metzmacher <metze@samba.org>
---
 fs/smb/smbdirect/accept.c     | 25 +++++++---
 fs/smb/smbdirect/connect.c    | 21 +++++---
 fs/smb/smbdirect/connection.c | 36 +++++++++-----
 fs/smb/smbdirect/internal.h   |  2 +
 fs/smb/smbdirect/listen.c     | 19 ++++++-
 fs/smb/smbdirect/socket.c     | 93 +++++++++++++++++++++++------------
 fs/smb/smbdirect/socket.h     | 14 +++++-
 7 files changed, 149 insertions(+), 61 deletions(-)

diff --git a/fs/smb/smbdirect/accept.c b/fs/smb/smbdirect/accept.c
index 9fb0676a3035..ddee9438bebf 100644
--- a/fs/smb/smbdirect/accept.c
+++ b/fs/smb/smbdirect/accept.c
@@ -24,6 +24,7 @@ int smbdirect_accept_connect_request(struct smbdirect_socket *sc,
 	u8 peer_responder_resources;
 	struct rdma_conn_param conn_param;
 	__be32 ird_ord_hdr[2];
+	unsigned long flags;
 	int ret;
 
 	/*
@@ -152,11 +153,17 @@ int smbdirect_accept_connect_request(struct smbdirect_socket *sc,
 	/*
 	 * start with the negotiate timeout and SMBDIRECT_KEEPALIVE_PENDING
 	 * so that the timer will cause a disconnect.
+	 *
+	 * Only if the teardown didn't start, see cleanup_lock.
 	 */
-	INIT_DELAYED_WORK(&sc->idle.timer_work, smbdirect_connection_idle_timer_work);
-	sc->idle.keepalive = SMBDIRECT_KEEPALIVE_PENDING;
-	mod_delayed_work(sc->workqueues.idle, &sc->idle.timer_work,
-			 msecs_to_jiffies(sp->negotiate_timeout_msec));
+	spin_lock_irqsave(&sc->cleanup_lock, flags);
+	if (!sc->first_error) {
+		INIT_DELAYED_WORK(&sc->idle.timer_work, smbdirect_connection_idle_timer_work);
+		sc->idle.keepalive = SMBDIRECT_KEEPALIVE_PENDING;
+		mod_delayed_work(sc->workqueues.idle, &sc->idle.timer_work,
+				 msecs_to_jiffies(sp->negotiate_timeout_msec));
+	}
+	spin_unlock_irqrestore(&sc->cleanup_lock, flags);
 
 	return 0;
 
@@ -287,13 +294,13 @@ static void smbdirect_accept_negotiate_recv_done(struct ib_cq *cq, struct ib_wc
 	 * smbdirect_accept_rdma_event_handler() will do it, when
 	 * RDMA_CM_EVENT_ESTABLISHED arrived.
 	 */
-	spin_lock_irqsave(&sc->connect.lock, flags);
+	spin_lock_irqsave(&sc->cleanup_lock, flags);
 	if (!sc->first_error) {
 		INIT_WORK(&sc->connect.work, smbdirect_accept_negotiate_recv_work);
 		if (sc->status == SMBDIRECT_SOCKET_NEGOTIATE_NEEDED)
 			queue_work(sc->workqueues.accept, &sc->connect.work);
 	}
-	spin_unlock_irqrestore(&sc->connect.lock, flags);
+	spin_unlock_irqrestore(&sc->cleanup_lock, flags);
 
 	return;
 
@@ -551,6 +558,8 @@ void smbdirect_accept_negotiate_finish(struct smbdirect_socket *sc, u32 ntstatus
 		recv_io->cqe.done = smbdirect_connection_recv_io_done;
 	recv_io = NULL;
 
+	smbdirect_connection_init_data_transfer_works(sc);
+
 	/*
 	 * We should at least post 1 smbdirect_recv_io!
 	 */
@@ -796,10 +805,10 @@ static int smbdirect_accept_rdma_event_handler(struct rdma_cm_id *id,
 		if (SMBDIRECT_CHECK_STATUS_DISCONNECT(sc, SMBDIRECT_SOCKET_RDMA_CONNECT_RUNNING))
 			return 0;
 		sc->status = SMBDIRECT_SOCKET_NEGOTIATE_NEEDED;
-		spin_lock_irqsave(&sc->connect.lock, flags);
+		spin_lock_irqsave(&sc->cleanup_lock, flags);
 		if (!sc->first_error)
 			queue_work(sc->workqueues.accept, &sc->connect.work);
-		spin_unlock_irqrestore(&sc->connect.lock, flags);
+		spin_unlock_irqrestore(&sc->cleanup_lock, flags);
 
 		/*
 		 * wait for smbdirect_accept_negotiate_recv_done()
diff --git a/fs/smb/smbdirect/connect.c b/fs/smb/smbdirect/connect.c
index ce00e6466690..be174af0f259 100644
--- a/fs/smb/smbdirect/connect.c
+++ b/fs/smb/smbdirect/connect.c
@@ -130,6 +130,7 @@ static int smbdirect_connect_rdma_connect(struct smbdirect_socket *sc)
 	struct smbdirect_socket_parameters *sp = &sc->parameters;
 	struct rdma_conn_param conn_param;
 	__be32 ird_ord_hdr[2];
+	unsigned long flags;
 	int ret;
 
 	sc->ib.dev = sc->rdma.cm_id->device;
@@ -231,11 +232,17 @@ static int smbdirect_connect_rdma_connect(struct smbdirect_socket *sc)
 	/*
 	 * start with the rdma connect timeout and SMBDIRECT_KEEPALIVE_PENDING
 	 * so that the timer will cause a disconnect.
+	 *
+	 * Only if the teardown didn't start, see cleanup_lock.
 	 */
-	INIT_DELAYED_WORK(&sc->idle.timer_work, smbdirect_connection_idle_timer_work);
-	sc->idle.keepalive = SMBDIRECT_KEEPALIVE_PENDING;
-	mod_delayed_work(sc->workqueues.idle, &sc->idle.timer_work,
-			 msecs_to_jiffies(sp->rdma_connect_timeout_msec));
+	spin_lock_irqsave(&sc->cleanup_lock, flags);
+	if (!sc->first_error) {
+		INIT_DELAYED_WORK(&sc->idle.timer_work, smbdirect_connection_idle_timer_work);
+		sc->idle.keepalive = SMBDIRECT_KEEPALIVE_PENDING;
+		mod_delayed_work(sc->workqueues.idle, &sc->idle.timer_work,
+				 msecs_to_jiffies(sp->rdma_connect_timeout_msec));
+	}
+	spin_unlock_irqrestore(&sc->cleanup_lock, flags);
 
 	return 0;
 }
@@ -629,13 +636,13 @@ static void smbdirect_connect_negotiate_recv_done(struct ib_cq *cq, struct ib_wc
 	 * So we defer further processing of the negotiation
 	 * to smbdirect_connect_negotiate_recv_work().
 	 */
-	spin_lock_irqsave(&sc->connect.lock, flags);
+	spin_lock_irqsave(&sc->cleanup_lock, flags);
 	if (!sc->first_error) {
 		INIT_WORK(&sc->connect.work, smbdirect_connect_negotiate_recv_work);
 		if (sc->status == SMBDIRECT_SOCKET_NEGOTIATE_RUNNING)
 			queue_work(sc->workqueues.connect, &sc->connect.work);
 	}
-	spin_unlock_irqrestore(&sc->connect.lock, flags);
+	spin_unlock_irqrestore(&sc->cleanup_lock, flags);
 
 	return;
 
@@ -876,6 +883,8 @@ static void smbdirect_connect_negotiate_recv_work(struct work_struct *work)
 		recv_io->cqe.done = smbdirect_connection_recv_io_done;
 	recv_io = NULL;
 
+	smbdirect_connection_init_data_transfer_works(sc);
+
 	/*
 	 * We should at least post 1 smbdirect_recv_io!
 	 */
diff --git a/fs/smb/smbdirect/connection.c b/fs/smb/smbdirect/connection.c
index 871750529a98..e84f488b83a3 100644
--- a/fs/smb/smbdirect/connection.c
+++ b/fs/smb/smbdirect/connection.c
@@ -190,14 +190,7 @@ void smbdirect_connection_negotiation_done(struct smbdirect_socket *sc)
 		  SMBDIRECT_DEBUG_ERR_PTR(sc->first_error));
 	sc->status = SMBDIRECT_SOCKET_CONNECTED;
 
-	/*
-	 * We need to setup the refill and send immediate work
-	 * in order to get a working connection.
-	 */
 done:
-	INIT_WORK(&sc->recv_io.posted.refill_work, smbdirect_connection_recv_io_refill_work);
-	INIT_WORK(&sc->idle.immediate_work, smbdirect_connection_send_immediate_work);
-
 	smbdirect_log_rdma_event(sc, SMBDIRECT_LOG_INFO,
 		"negotiated: local: %pISpsfc remote: %pISpsfc\n",
 		&sc->rdma.cm_id->route.addr.src_addr,
@@ -206,6 +199,28 @@ void smbdirect_connection_negotiation_done(struct smbdirect_socket *sc)
 	wake_up(&sc->status_wait);
 }
 
+/*
+ * smbdirect_connection_recv_io_done() may queue the refill and the
+ * immediate send work for every data transfer message it receives,
+ * so they need to be activated before the receives are posted, as
+ * INIT_WORK() on a work item that may be queued concurrently
+ * corrupts it. And only if the teardown didn't start, see
+ * cleanup_lock.
+ */
+void smbdirect_connection_init_data_transfer_works(struct smbdirect_socket *sc)
+{
+	unsigned long flags;
+
+	spin_lock_irqsave(&sc->cleanup_lock, flags);
+	if (!sc->first_error) {
+		INIT_WORK(&sc->recv_io.posted.refill_work,
+			  smbdirect_connection_recv_io_refill_work);
+		INIT_WORK(&sc->idle.immediate_work,
+			  smbdirect_connection_send_immediate_work);
+	}
+	spin_unlock_irqrestore(&sc->cleanup_lock, flags);
+}
+
 static u32 smbdirect_rdma_rw_send_wrs(struct ib_device *dev,
 				      const struct ib_qp_init_attr *attr)
 {
@@ -1786,12 +1801,11 @@ void smbdirect_connection_recv_io_done(struct ib_cq *cq, struct ib_wc *wc)
 
 error:
 	/*
-	 * Make sure smbdirect_connection_put_recv_io() does not
-	 * start recv_io.posted.refill_work.
+	 * This disables recv_io.posted.refill_work under cleanup_lock,
+	 * so smbdirect_connection_put_recv_io() does not start it.
 	 */
-	disable_work(&sc->recv_io.posted.refill_work);
-	smbdirect_connection_put_recv_io(recv_io);
 	smbdirect_socket_schedule_cleanup(sc, -ECONNABORTED);
+	smbdirect_connection_put_recv_io(recv_io);
 }
 
 int smbdirect_connection_recv_io_refill(struct smbdirect_socket *sc)
diff --git a/fs/smb/smbdirect/internal.h b/fs/smb/smbdirect/internal.h
index fec71278421a..7044dd47c339 100644
--- a/fs/smb/smbdirect/internal.h
+++ b/fs/smb/smbdirect/internal.h
@@ -87,6 +87,8 @@ void smbdirect_connection_rdma_established(struct smbdirect_socket *sc);
 
 void smbdirect_connection_negotiation_done(struct smbdirect_socket *sc);
 
+void smbdirect_connection_init_data_transfer_works(struct smbdirect_socket *sc);
+
 int smbdirect_connection_create_qp(struct smbdirect_socket *sc);
 
 void smbdirect_connection_destroy_qp(struct smbdirect_socket *sc);
diff --git a/fs/smb/smbdirect/listen.c b/fs/smb/smbdirect/listen.c
index 3c91f00d95ce..2c4a2f249f45 100644
--- a/fs/smb/smbdirect/listen.c
+++ b/fs/smb/smbdirect/listen.c
@@ -124,8 +124,23 @@ int smbdirect_socket_listen(struct smbdirect_socket *sc, int backlog)
 
 	ret = rdma_listen(sc->rdma.cm_id, backlog);
 	if (ret) {
-		sc->first_error = ret;
-		sc->status = SMBDIRECT_SOCKET_DISCONNECTED;
+		/*
+		 * This sets first_error under cleanup_lock,
+		 * changes the status to SMBDIRECT_SOCKET_DISCONNECTED
+		 * and queues disconnect_work.
+		 *
+		 * It's safe to use sc->rdma.cm_id and
+		 * sc->rdma.cm_id->device afterwards: neither this
+		 * nor smbdirect_socket_cleanup_work() touch the cm_id
+		 * of a socket that never reached
+		 * RDMA_CM_EVENT_ESTABLISHED. Only
+		 * smbdirect_socket_destroy() calls rdma_destroy_id(),
+		 * via smbdirect_socket_release() of our caller,
+		 * which can't run while we are still here.
+		 * listen.backlog is still -1, so there are no
+		 * pending or ready child sockets to clean up either.
+		 */
+		smbdirect_socket_schedule_cleanup_lvl(sc, SMBDIRECT_LOG_INFO, ret);
 		if (sc->rdma.cm_id->device)
 			smbdirect_log_rdma_event(sc, SMBDIRECT_LOG_INFO,
 				"listening failed %1pe on addr: %pISpsfc dev: %.*s\n",
diff --git a/fs/smb/smbdirect/socket.c b/fs/smb/smbdirect/socket.c
index 749a85274f97..cb090c558321 100644
--- a/fs/smb/smbdirect/socket.c
+++ b/fs/smb/smbdirect/socket.c
@@ -271,7 +271,34 @@ void __smbdirect_socket_schedule_cleanup(struct smbdirect_socket *sc,
 	unsigned long flags;
 	bool was_first = false;
 
+	/*
+	 * first_error is set under cleanup_lock, together with
+	 * disabling the work items, see cleanup_lock: an activation
+	 * either happened before or sees first_error, so the
+	 * work items only need to be disabled once.
+	 */
+	spin_lock_irqsave(&sc->cleanup_lock, flags);
 	if (!sc->first_error) {
+		if (error)
+			sc->first_error = error;
+		else
+			sc->first_error = -ECONNABORTED;
+		was_first = true;
+
+		/*
+		 * make sure other work (than disconnect_work)
+		 * is not queued again but here we don't block and avoid
+		 * disable[_delayed]_work_sync()
+		 */
+		disable_work(&sc->connect.work);
+		disable_work(&sc->recv_io.posted.refill_work);
+		disable_work(&sc->idle.immediate_work);
+		sc->idle.keepalive = SMBDIRECT_KEEPALIVE_NONE;
+		disable_delayed_work(&sc->idle.timer_work);
+	}
+	spin_unlock_irqrestore(&sc->cleanup_lock, flags);
+
+	if (was_first)
 		___smbdirect_log_generic(sc, func, line,
 			lvl,
 			SMBDIRECT_LOG_RDMA_EVENT,
@@ -282,23 +309,6 @@ void __smbdirect_socket_schedule_cleanup(struct smbdirect_socket *sc,
 			force_status ? smbdirect_socket_status_string(*force_status) : "",
 			func, line,
 			smbdirect_socket_status_string(sc->status));
-		if (error)
-			sc->first_error = error;
-		else
-			sc->first_error = -ECONNABORTED;
-		was_first = true;
-	}
-
-	/*
-	 * make sure other work (than disconnect_work)
-	 * is not queued again but here we don't block and avoid
-	 * disable[_delayed]_work_sync()
-	 */
-	disable_work(&sc->connect.work);
-	disable_work(&sc->recv_io.posted.refill_work);
-	disable_work(&sc->idle.immediate_work);
-	sc->idle.keepalive = SMBDIRECT_KEEPALIVE_NONE;
-	disable_delayed_work(&sc->idle.timer_work);
 
 	/*
 	 * In case we were a listener we need to
@@ -396,6 +406,7 @@ static void smbdirect_socket_cleanup_work(struct work_struct *work)
 	struct smbdirect_socket *sc =
 		container_of(work, struct smbdirect_socket, disconnect_work);
 	struct smbdirect_socket *psc, *tsc;
+	bool was_first = false;
 	unsigned long flags;
 
 	/*
@@ -403,25 +414,35 @@ static void smbdirect_socket_cleanup_work(struct work_struct *work)
 	 */
 	WARN_ON_ONCE(in_interrupt());
 
-	if (!sc->first_error) {
-		smbdirect_log_rdma_event(sc, SMBDIRECT_LOG_ERR,
-			"%s called with first_error==0\n",
-			smbdirect_socket_status_string(sc->status));
-
-		sc->first_error = -ECONNABORTED;
-	}
-
 	/*
-	 * make sure this and other work is not queued again
+	 * make sure this work is not queued again
 	 * but here we don't block and avoid
-	 * disable[_delayed]_work_sync()
+	 * disable_work_sync()
 	 */
 	disable_work(&sc->disconnect_work);
-	disable_work(&sc->connect.work);
-	disable_work(&sc->recv_io.posted.refill_work);
-	disable_work(&sc->idle.immediate_work);
-	sc->idle.keepalive = SMBDIRECT_KEEPALIVE_NONE;
-	disable_delayed_work(&sc->idle.timer_work);
+
+	/*
+	 * Normally __smbdirect_socket_schedule_cleanup() already
+	 * set first_error and disabled the other work items,
+	 * otherwise do it here, see cleanup_lock.
+	 */
+	spin_lock_irqsave(&sc->cleanup_lock, flags);
+	if (!sc->first_error) {
+		sc->first_error = -ECONNABORTED;
+		was_first = true;
+
+		disable_work(&sc->connect.work);
+		disable_work(&sc->recv_io.posted.refill_work);
+		disable_work(&sc->idle.immediate_work);
+		sc->idle.keepalive = SMBDIRECT_KEEPALIVE_NONE;
+		disable_delayed_work(&sc->idle.timer_work);
+	}
+	spin_unlock_irqrestore(&sc->cleanup_lock, flags);
+
+	if (was_first)
+		smbdirect_log_rdma_event(sc, SMBDIRECT_LOG_ERR,
+			"%s called with first_error==0\n",
+			smbdirect_socket_status_string(sc->status));
 
 	/*
 	 * In case we were a listener we need to
@@ -569,6 +590,14 @@ static void smbdirect_socket_destroy(struct smbdirect_socket *sc)
 	 */
 	smbdirect_socket_wake_up_all(sc);
 
+	/*
+	 * These don't need cleanup_lock: smbdirect_socket_destroy_sync()
+	 * makes sure first_error is set before we get here, so no work
+	 * item can be activated anymore, see cleanup_lock. The work items
+	 * are already disabled, either by the cleanup that set first_error
+	 * or because they were never activated, here we only wait for an
+	 * instance that is still running.
+	 */
 	disable_work_sync(&sc->disconnect_work);
 	disable_work_sync(&sc->connect.work);
 	disable_work_sync(&sc->recv_io.posted.refill_work);
diff --git a/fs/smb/smbdirect/socket.h b/fs/smb/smbdirect/socket.h
index 1fc4aebcdda5..1520b6e48e7d 100644
--- a/fs/smb/smbdirect/socket.h
+++ b/fs/smb/smbdirect/socket.h
@@ -108,6 +108,17 @@ struct smbdirect_socket {
 	wait_queue_head_t status_wait;
 	int first_error;
 
+	/*
+	 * The cleanup sets first_error and disables the work items
+	 * under cleanup_lock and the work items are only activated with
+	 * INIT_[DELAYED_]WORK() under cleanup_lock while first_error is
+	 * still 0. INIT_[DELAYED_]WORK() resets the disable count,
+	 * so without the lock an activation could undo a disable of
+	 * a concurrent teardown. All other writers of first_error
+	 * also hold cleanup_lock.
+	 */
+	spinlock_t cleanup_lock;
+
 	/*
 	 * This points to the workqueues to
 	 * be used for this socket.
@@ -194,7 +205,6 @@ struct smbdirect_socket {
 	 * The state for connect/negotiation
 	 */
 	struct {
-		spinlock_t lock;
 		struct work_struct work;
 	} connect;
 
@@ -598,6 +608,7 @@ static __always_inline void smbdirect_socket_init(struct smbdirect_socket *sc)
 	memset(sc, 0, sizeof(*sc));
 
 	init_waitqueue_head(&sc->status_wait);
+	spin_lock_init(&sc->cleanup_lock);
 
 	sc->workqueues.accept = smbdirect_globals.workqueues.accept;
 	sc->workqueues.connect = smbdirect_globals.workqueues.connect;
@@ -616,7 +627,6 @@ static __always_inline void smbdirect_socket_init(struct smbdirect_socket *sc)
 
 	sc->ib.poll_ctx = IB_POLL_UNBOUND_WORKQUEUE;
 
-	spin_lock_init(&sc->connect.lock);
 	INIT_WORK(&sc->connect.work, __smbdirect_socket_disabled_work);
 	disable_work_sync(&sc->connect.work);
 
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 14+ messages in thread

* [PATCH v2 09/13] smb: smbdirect: don't warn about a status changed by a concurrent teardown
  2026-11-27 16:40 [PATCH v2 00/13] smb: smbdirect: urgent fixes for teardown races, sends and MR invalidation Stefan Metzmacher
                   ` (7 preceding siblings ...)
  2026-11-27 16:40 ` [PATCH v2 08/13] smb: smbdirect: don't let INIT_[DELAYED_]WORK() undo a disable from the teardown Stefan Metzmacher
@ 2026-11-27 16:40 ` Stefan Metzmacher
  2026-11-27 16:40 ` [PATCH v2 10/13] smb: smbdirect: don't expose DMA addresses as iova of our MRs Stefan Metzmacher
                   ` (3 subsequent siblings)
  12 siblings, 0 replies; 14+ messages in thread
From: Stefan Metzmacher @ 2026-11-27 16:40 UTC (permalink / raw)
  To: linux-cifs, samba-technical
  Cc: metze, stable, Namjae Jeon, Paulo Alcantara, Tom Talpey,
	syzbot+a989758ffc6f93223eb6

syzbot reported this warning:

  expected[RESOLVE_ADDR_RUNNING] != DISCONNECTED first_error=-ERESTARTSYS local=127.0.0.1:51036 remote=127.0.0.1:445
  WARNING: fs/smb/smbdirect/connect.c:307 at smbdirect_connect_rdma_event_handler+0x1e02/0x2820 fs/smb/smbdirect/connect.c:307, CPU#1: kworker/u8:7/198
  Workqueue: rdma_cm cma_work_handler
  Call Trace:
   cma_cm_event_handler+0xac/0x2c0 drivers/infiniband/core/cma.c:2181
   cma_work_handler+0x265/0x520 drivers/infiniband/core/cma.c:3040

The waiter in smbdirect_connection_wait_for_connected() was interrupted
by a signal, so it called smbdirect_socket_schedule_cleanup(), which
sets first_error and changes the status, without the handler lock of
the rdma cm id, while smbdirect_connect_rdma_event_handler() was
running.

SMBDIRECT_CHECK_STATUS_{WARN,DISCONNECT}() check first_error before
the status, but there was nothing that made sure the first_error
written before the status is also seen before it, the compiler could
even reuse the value of the first_error check at the start of the
event handler. So the handler saw first_error == 0 and the status
changed by the teardown, which is not unexpected.

Now __smbdirect_socket_schedule_cleanup() and, if it sets first_error
itself, smbdirect_socket_cleanup_work() write first_error with
WRITE_ONCE() and order it before the status changes with smp_wmb() and
__SMBDIRECT_CHECK_STATUS_FAILED() reads both with READ_ONCE() and
checks first_error again, after smp_rmb(), if the status is not the
expected one, so such a status change is handled like an error
without a warning.

Fixes: 1adb2dab9727 ("smb: smbdirect: introduce SMBDIRECT_CHECK_STATUS_{WARN,DISCONNECT}()")
Fixes: d1f187656797 ("smb: smbdirect: introduce smbdirect_connect[_sync]()")
Cc: stable@vger.kernel.org
Cc: Namjae Jeon <linkinjeon@kernel.org>
Cc: Paulo Alcantara <pc@manguebit.org>
Cc: Tom Talpey <tom@talpey.com>
Cc: linux-cifs@vger.kernel.org
Cc: samba-technical@lists.samba.org
Reported-by: syzbot+a989758ffc6f93223eb6@syzkaller.appspotmail.com
Closes: https://syzkaller.appspot.com/bug?extid=a989758ffc6f93223eb6
Signed-off-by: Stefan Metzmacher <metze@samba.org>
---
 fs/smb/smbdirect/socket.c | 21 ++++++++++++++++++---
 fs/smb/smbdirect/socket.h | 18 +++++++++++++++---
 2 files changed, 33 insertions(+), 6 deletions(-)

diff --git a/fs/smb/smbdirect/socket.c b/fs/smb/smbdirect/socket.c
index cb090c558321..53be81077adb 100644
--- a/fs/smb/smbdirect/socket.c
+++ b/fs/smb/smbdirect/socket.c
@@ -280,9 +280,9 @@ void __smbdirect_socket_schedule_cleanup(struct smbdirect_socket *sc,
 	spin_lock_irqsave(&sc->cleanup_lock, flags);
 	if (!sc->first_error) {
 		if (error)
-			sc->first_error = error;
+			WRITE_ONCE(sc->first_error, error);
 		else
-			sc->first_error = -ECONNABORTED;
+			WRITE_ONCE(sc->first_error, -ECONNABORTED);
 		was_first = true;
 
 		/*
@@ -298,6 +298,14 @@ void __smbdirect_socket_schedule_cleanup(struct smbdirect_socket *sc,
 	}
 	spin_unlock_irqrestore(&sc->cleanup_lock, flags);
 
+	/*
+	 * Callers of SMBDIRECT_CHECK_STATUS_{WARN,DISCONNECT}()
+	 * may run concurrently without our locks, they need
+	 * to see first_error before the status changes below,
+	 * see __SMBDIRECT_CHECK_STATUS_FAILED().
+	 */
+	smp_wmb();
+
 	if (was_first)
 		___smbdirect_log_generic(sc, func, line,
 			lvl,
@@ -428,7 +436,7 @@ static void smbdirect_socket_cleanup_work(struct work_struct *work)
 	 */
 	spin_lock_irqsave(&sc->cleanup_lock, flags);
 	if (!sc->first_error) {
-		sc->first_error = -ECONNABORTED;
+		WRITE_ONCE(sc->first_error, -ECONNABORTED);
 		was_first = true;
 
 		disable_work(&sc->connect.work);
@@ -439,6 +447,13 @@ static void smbdirect_socket_cleanup_work(struct work_struct *work)
 	}
 	spin_unlock_irqrestore(&sc->cleanup_lock, flags);
 
+	/*
+	 * Like in __smbdirect_socket_schedule_cleanup(),
+	 * first_error needs to be seen before the status
+	 * changes below.
+	 */
+	smp_wmb();
+
 	if (was_first)
 		smbdirect_log_rdma_event(sc, SMBDIRECT_LOG_ERR,
 			"%s called with first_error==0\n",
diff --git a/fs/smb/smbdirect/socket.h b/fs/smb/smbdirect/socket.h
index 1520b6e48e7d..c669778fac4b 100644
--- a/fs/smb/smbdirect/socket.h
+++ b/fs/smb/smbdirect/socket.h
@@ -692,14 +692,26 @@ static __always_inline void smbdirect_socket_init(struct smbdirect_socket *sc)
 	sc->logging.vaprintf = __smbdirect_log_vaprintf;
 }
 
+/*
+ * A concurrent __smbdirect_socket_schedule_cleanup() or
+ * smbdirect_socket_cleanup_work() sets first_error before it changes
+ * the status, so if the status is not the expected one, it may only
+ * have changed because of that, which is not unexpected. That's why
+ * first_error is checked again after smp_rmb().
+ */
 #define __SMBDIRECT_CHECK_STATUS_FAILED(__sc, __expected_status, __error_cmd, __unexpected_cmd) ({ \
 	bool __failed = false; \
-	if (unlikely((__sc)->first_error)) { \
+	if (unlikely(READ_ONCE((__sc)->first_error))) { \
 		__failed = true; \
 		__error_cmd \
-	} else if (unlikely((__sc)->status != (__expected_status))) { \
+	} else if (unlikely(READ_ONCE((__sc)->status) != (__expected_status))) { \
 		__failed = true; \
-		__unexpected_cmd \
+		smp_rmb(); /* pairs with smp_wmb() in the cleanup functions */ \
+		if (READ_ONCE((__sc)->first_error)) { \
+			__error_cmd \
+		} else { \
+			__unexpected_cmd \
+		} \
 	} \
 	__failed; \
 })
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 14+ messages in thread

* [PATCH v2 10/13] smb: smbdirect: don't expose DMA addresses as iova of our MRs
  2026-11-27 16:40 [PATCH v2 00/13] smb: smbdirect: urgent fixes for teardown races, sends and MR invalidation Stefan Metzmacher
                   ` (8 preceding siblings ...)
  2026-11-27 16:40 ` [PATCH v2 09/13] smb: smbdirect: don't warn about a status changed by a concurrent teardown Stefan Metzmacher
@ 2026-11-27 16:40 ` Stefan Metzmacher
  2026-11-27 16:40 ` [PATCH v2 11/13] smb: smbdirect: don't let MR work requests miss the QP drain Stefan Metzmacher
                   ` (2 subsequent siblings)
  12 siblings, 0 replies; 14+ messages in thread
From: Stefan Metzmacher @ 2026-11-27 16:40 UTC (permalink / raw)
  To: linux-cifs, samba-technical
  Cc: metze, stable, Namjae Jeon, Paulo Alcantara, Tom Talpey,
	Tatyana Nikolova, Jacob Moroni, linux-rdma

ib_map_mr_sg() uses the DMA address of the first page as iova of the
MR and smbdirect sends that iova to the peer as offset of the buffer
descriptor, so the peer learns our DMA addresses, without an IOMMU
these are the physical addresses of the buffers.

The peer just uses the offset as base address of its RDMA READs and
WRITEs, so the value is up to us, the hardware only needs the offset
within the first page in the low bits of the iova. So keep these bits,
leave the rest of the low 24 bits zero (more for a page size above
16 MiB), so that the addresses the peer uses show the position within
the MR in traces, and put random bits above them up to bit 55, which
also makes it harder to reuse an old offset with a token whose key
wrapped around.

The top byte is always 0, so iova + length stays far below 2^64 even
for a length of U32_MAX. This also avoids a problem with irdma: with
an IOMMU that allocates IOVAs top-down, the first 64-bit IOVA range of
a device ends exactly at 2^64. If that is the buffer of an MR,
iova + length is 0 and irdma rejects the RDMA READ of the peer with
IRDMA_AE_AMP_TO_WRAP (ae_id 0x10a), even if the last byte of the MR is
at 2^64 - 1, so the connection is torn down.

Fixes: c7398583340a ("CIFS: SMBD: Implement RDMA memory registration")
Cc: stable@vger.kernel.org
Cc: Namjae Jeon <linkinjeon@kernel.org>
Cc: Paulo Alcantara <pc@manguebit.org>
Cc: Tom Talpey <tom@talpey.com>
Cc: Tatyana Nikolova <tatyana.e.nikolova@intel.com>
Cc: Jacob Moroni <jmoroni@google.com>
Cc: linux-cifs@vger.kernel.org
Cc: samba-technical@lists.samba.org
Cc: linux-rdma@vger.kernel.org
Signed-off-by: Stefan Metzmacher <metze@samba.org>
---
 fs/smb/smbdirect/mr.c | 29 +++++++++++++++++++++++++++++
 1 file changed, 29 insertions(+)

diff --git a/fs/smb/smbdirect/mr.c b/fs/smb/smbdirect/mr.c
index 68b9ae5ec0c1..760a6049182a 100644
--- a/fs/smb/smbdirect/mr.c
+++ b/fs/smb/smbdirect/mr.c
@@ -5,6 +5,7 @@
  */
 
 #include "internal.h"
+#include <linux/random.h>
 
 /*
  * Allocate MRs used for RDMA read/write
@@ -305,6 +306,8 @@ smbdirect_connection_register_mr_io(struct smbdirect_socket *sc,
 	struct smbdirect_mr_io *mr;
 	int ret, num_pages, num_mapped;
 	struct ib_reg_wr *reg_wr;
+	u64 random_bits, page_offset;
+	unsigned int shift;
 	unsigned long flags;
 
 	num_pages = iov_iter_npages(iter, sp->max_frmr_depth + 1);
@@ -384,6 +387,32 @@ smbdirect_connection_register_mr_io(struct smbdirect_socket *sc,
 		goto map_mr_error;
 	}
 
+	/*
+	 * ib_map_mr_sg() uses the DMA address of the first page as
+	 * iova, which goes to the peer as offset of the buffer
+	 * descriptor. Don't expose DMA addresses (without an IOMMU
+	 * physical addresses) to the peer, the hardware only needs
+	 * the offset within the first page in the low bits, all
+	 * other bits are up to us.
+	 *
+	 * The low 24 bits (or more for a page size above 16 MiB) are
+	 * only that page offset, so in traces they show the position
+	 * within the MR plus that offset (an SMB3 READ/WRITE is at
+	 * most 16 MiB). The bits above up to bit 55 are random, which
+	 * makes it harder to reuse an old offset with a token whose
+	 * key wrapped around.
+	 *
+	 * The top byte is always 0, so iova + length stays far below
+	 * 2^64 even for a length of U32_MAX. irdma rejects the access
+	 * to an MR ending exactly at 2^64 with IRDMA_AE_AMP_TO_WRAP,
+	 * which happened with an IOMMU that allocates the first
+	 * 64-bit IOVA range at the very top.
+	 */
+	shift = max_t(unsigned int, 24, ilog2(mr->mr->page_size));
+	random_bits = ((u64)get_random_u32() << shift) & GENMASK_ULL(55, 24);
+	page_offset = mr->mr->iova & (mr->mr->page_size - 1);
+	mr->mr->iova = random_bits | page_offset;
+
 	ib_update_fast_reg_key(mr->mr, ib_inc_rkey(mr->mr->rkey));
 	if (mr->mr->rkey == mr->base_tag)
 		ib_update_fast_reg_key(mr->mr, ib_inc_rkey(mr->mr->rkey));
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 14+ messages in thread

* [PATCH v2 11/13] smb: smbdirect: don't let MR work requests miss the QP drain
  2026-11-27 16:40 [PATCH v2 00/13] smb: smbdirect: urgent fixes for teardown races, sends and MR invalidation Stefan Metzmacher
                   ` (9 preceding siblings ...)
  2026-11-27 16:40 ` [PATCH v2 10/13] smb: smbdirect: don't expose DMA addresses as iova of our MRs Stefan Metzmacher
@ 2026-11-27 16:40 ` Stefan Metzmacher
  2026-11-27 16:40 ` [PATCH v2 12/13] smb: smbdirect: wake up the credit waiters after a failed send Stefan Metzmacher
  2026-11-27 16:40 ` [PATCH v2 13/13] smb: smbdirect: give back granted receive credits if a send fails Stefan Metzmacher
  12 siblings, 0 replies; 14+ messages in thread
From: Stefan Metzmacher @ 2026-11-27 16:40 UTC (permalink / raw)
  To: linux-cifs, samba-technical
  Cc: metze, stable, Namjae Jeon, Paulo Alcantara, Tom Talpey, Sashiko

smbdirect_connection_register_mr_io() and
smbdirect_connection_deregister_mr_io() check sc->status under
mr->mutex and then post IB_WR_REG_MR or IB_WR_LOCAL_INV.
smbdirect_socket_destroy() changes the status and drains the QP
without that mutex, it only waits for it later in
smbdirect_connection_destroy_mr_list():

CPU0                     CPU1
register_mr_io()
  mutex_lock(&mr->mutex)
  status == CONNECTED
                         smbdirect_socket_destroy()
                           status = DISCONNECTED
                           ib_drain_qp()
                           destroy_mr_list()
                             mutex_lock(&mr->mutex) [waits]
  ib_post_send(REG_MR)
  mutex_unlock(&mr->mutex)
                             disable_locked(mr)
                             mr->socket = NULL
                             kref_put(&mr->kref)
<send cq>
mr_io_register_done()
  schedule_cleanup(mr->socket == NULL)

The work request posted after the drain is flushed asynchronously, so
its completion handler passes the NULL mr->socket to
smbdirect_socket_schedule_cleanup(), and if the caller already dropped
the last reference of the mr, the cqe is part of freed memory. The
same happens with IB_WR_LOCAL_INV from
smbdirect_connection_deregister_mr_io().

Take and release each mr->mutex in smbdirect_socket_destroy() after
the status changed and before ib_drain_qp(), so a work request posted
after a status check that still saw CONNECTED is posted before the
drain, which then also runs its completion while the mr is still
attached. Later callers see the new status under the mutex and don't
post anything.

Fixes: b0432201a11b ("smb: client: let destroy_mr_list() keep smbdirect_mr_io memory if registered")
Fixes: de5ef8ec3c46 ("smb: smbdirect: introduce smbdirect_mr.c with client mr code")
Cc: stable@vger.kernel.org
Cc: Namjae Jeon <linkinjeon@kernel.org>
Cc: Paulo Alcantara <pc@manguebit.org>
Cc: Tom Talpey <tom@talpey.com>
Cc: linux-cifs@vger.kernel.org
Cc: samba-technical@lists.samba.org
Reported-by: Sashiko <sashiko-bot@kernel.org>
Closes: https://sashiko.dev/#/bug/linux-4461fbd8-a522-43ee-81d1-651432a55de9
Signed-off-by: Stefan Metzmacher <metze@samba.org>
---
 fs/smb/smbdirect/internal.h |  2 ++
 fs/smb/smbdirect/mr.c       | 28 +++++++++++++++++++++++++++-
 fs/smb/smbdirect/socket.c   |  2 ++
 3 files changed, 31 insertions(+), 1 deletion(-)

diff --git a/fs/smb/smbdirect/internal.h b/fs/smb/smbdirect/internal.h
index 7044dd47c339..1ce89bbfaf03 100644
--- a/fs/smb/smbdirect/internal.h
+++ b/fs/smb/smbdirect/internal.h
@@ -134,6 +134,8 @@ void smbdirect_connection_invalidate_rkey(struct smbdirect_socket *sc, u32 rkey)
 
 int smbdirect_connection_create_mr_list(struct smbdirect_socket *sc);
 
+void smbdirect_connection_wait_mr_io_posts(struct smbdirect_socket *sc);
+
 void smbdirect_connection_destroy_mr_list(struct smbdirect_socket *sc);
 
 int smbdirect_accept_connect_request(struct smbdirect_socket *sc,
diff --git a/fs/smb/smbdirect/mr.c b/fs/smb/smbdirect/mr.c
index 760a6049182a..c56cea10bb73 100644
--- a/fs/smb/smbdirect/mr.c
+++ b/fs/smb/smbdirect/mr.c
@@ -145,6 +145,30 @@ static void smbdirect_mr_io_free_locked(struct kref *kref)
 	kfree(mr);
 }
 
+/*
+ * smbdirect_connection_register_mr_io() and
+ * smbdirect_connection_deregister_mr_io() check the status under
+ * mr->mutex before they post IB_WR_REG_MR or IB_WR_LOCAL_INV.
+ * smbdirect_socket_destroy() calls this once the status is no longer
+ * CONNECTED and before ib_drain_qp(), so every work request posted
+ * after a check that still saw CONNECTED is posted before the drain
+ * and its completion runs while the mr is still attached to the
+ * socket. Later callers see the new status and don't post anything.
+ *
+ * The list is only changed by smbdirect_connection_create_mr_list()
+ * and smbdirect_connection_destroy_mr_list(), so we can walk it
+ * without sc->mr_io.all.lock and sleep on the mutexes.
+ */
+void smbdirect_connection_wait_mr_io_posts(struct smbdirect_socket *sc)
+{
+	struct smbdirect_mr_io *mr;
+
+	list_for_each_entry(mr, &sc->mr_io.all.list, list) {
+		mutex_lock(&mr->mutex);
+		mutex_unlock(&mr->mutex);
+	}
+}
+
 void smbdirect_connection_destroy_mr_list(struct smbdirect_socket *sc)
 {
 	struct smbdirect_mr_io *mr, *tmp;
@@ -342,7 +366,9 @@ smbdirect_connection_register_mr_io(struct smbdirect_socket *sc,
 	 * 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.
+	 * ib_post_send() below. smbdirect_socket_destroy() also waits
+	 * for this mutex before it drains the QP, see
+	 * smbdirect_connection_wait_mr_io_posts().
 	 *
 	 * If the teardown already disabled this mr, it also detached it
 	 * from the socket and dropped its reference, so we only drop
diff --git a/fs/smb/smbdirect/socket.c b/fs/smb/smbdirect/socket.c
index 53be81077adb..f4eae1792790 100644
--- a/fs/smb/smbdirect/socket.c
+++ b/fs/smb/smbdirect/socket.c
@@ -649,6 +649,8 @@ static void smbdirect_socket_destroy(struct smbdirect_socket *sc)
 		smbdirect_socket_wake_up_all(sc);
 	}
 
+	smbdirect_connection_wait_mr_io_posts(sc);
+
 	if (sc->ib.qp) {
 		smbdirect_log_rdma_event(sc, SMBDIRECT_LOG_INFO,
 			"drain qp\n");
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 14+ messages in thread

* [PATCH v2 12/13] smb: smbdirect: wake up the credit waiters after a failed send
  2026-11-27 16:40 [PATCH v2 00/13] smb: smbdirect: urgent fixes for teardown races, sends and MR invalidation Stefan Metzmacher
                   ` (10 preceding siblings ...)
  2026-11-27 16:40 ` [PATCH v2 11/13] smb: smbdirect: don't let MR work requests miss the QP drain Stefan Metzmacher
@ 2026-11-27 16:40 ` Stefan Metzmacher
  2026-11-27 16:40 ` [PATCH v2 13/13] smb: smbdirect: give back granted receive credits if a send fails Stefan Metzmacher
  12 siblings, 0 replies; 14+ messages in thread
From: Stefan Metzmacher @ 2026-11-27 16:40 UTC (permalink / raw)
  To: linux-cifs, samba-technical
  Cc: metze, stable, Namjae Jeon, Paulo Alcantara, Tom Talpey, Sashiko

The error paths of smbdirect_connection_send_single_iter() give back
the send, local and batch credits without waking up the waiters on
sc->send_io.{credits,lcredits,bcredits}.wait_queue, so other senders
and the immediate send work can sleep while credits are available,
until something else wakes them up.

It also gives back the batch credit of a batch of the caller and
clears batch->credit, so smbdirect_connection_send_batch_abort()
doesn't see it and doesn't wake up the waiters either, and the credit
can be used for another PDU while the aborted one is still in the
batch.

Wake up the waiters, like smbdirect_connection_send_batch_abort()
does, and leave the batch credit of a batch of the caller to
smbdirect_connection_send_batch_abort() or
smbdirect_connection_send_batch_flush().

Fixes: 54abc694ebb8 ("smb: smbdirect: introduce smbdirect_connection_send_iter() and related functions")
Cc: stable@vger.kernel.org
Cc: Namjae Jeon <linkinjeon@kernel.org>
Cc: Paulo Alcantara <pc@manguebit.org>
Cc: Tom Talpey <tom@talpey.com>
Cc: linux-cifs@vger.kernel.org
Cc: samba-technical@lists.samba.org
Reported-by: Sashiko <sashiko-bot@kernel.org>
Closes: https://sashiko.dev/#/bug/linux-a2180487-c0d3-4f9a-92d7-208e032dd966
Signed-off-by: Stefan Metzmacher <metze@samba.org>
---
 fs/smb/smbdirect/connection.c | 14 ++++++++++++--
 1 file changed, 12 insertions(+), 2 deletions(-)

diff --git a/fs/smb/smbdirect/connection.c b/fs/smb/smbdirect/connection.c
index e84f488b83a3..837de712db65 100644
--- a/fs/smb/smbdirect/connection.c
+++ b/fs/smb/smbdirect/connection.c
@@ -1353,11 +1353,21 @@ int smbdirect_connection_send_single_iter(struct smbdirect_socket *sc,
 flush_failed:
 alloc_failed:
 	atomic_inc(&sc->send_io.credits.count);
+	wake_up(&sc->send_io.credits.wait_queue);
 credit_failed:
 	atomic_inc(&sc->send_io.lcredits.count);
+	wake_up(&sc->send_io.lcredits.wait_queue);
 lcredit_failed:
-	atomic_add(batch->credit, &sc->send_io.bcredits.count);
-	batch->credit = 0;
+	/*
+	 * The batch credit of a batch of the caller is given back by
+	 * smbdirect_connection_send_batch_abort() or
+	 * smbdirect_connection_send_batch_flush().
+	 */
+	if (batch == &_batch && batch->credit) {
+		atomic_add(batch->credit, &sc->send_io.bcredits.count);
+		batch->credit = 0;
+		wake_up(&sc->send_io.bcredits.wait_queue);
+	}
 bcredit_failed:
 	return ret;
 }
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 14+ messages in thread

* [PATCH v2 13/13] smb: smbdirect: give back granted receive credits if a send fails
  2026-11-27 16:40 [PATCH v2 00/13] smb: smbdirect: urgent fixes for teardown races, sends and MR invalidation Stefan Metzmacher
                   ` (11 preceding siblings ...)
  2026-11-27 16:40 ` [PATCH v2 12/13] smb: smbdirect: wake up the credit waiters after a failed send Stefan Metzmacher
@ 2026-11-27 16:40 ` Stefan Metzmacher
  12 siblings, 0 replies; 14+ messages in thread
From: Stefan Metzmacher @ 2026-11-27 16:40 UTC (permalink / raw)
  To: linux-cifs, samba-technical
  Cc: metze, stable, Namjae Jeon, Paulo Alcantara, Tom Talpey, Sashiko

smbdirect_connection_send_single_iter() moves the receive credits it
grants in the message from sc->recv_io.credits.available to
sc->recv_io.credits.count before the message is allocated, mapped and
posted. If that fails, the peer never gets the message, but the
credits stay accounted as granted, which permanently shrinks the
receive credit window of the connection.

Give them back to sc->recv_io.credits.available on the error paths.

Fixes: 54abc694ebb8 ("smb: smbdirect: introduce smbdirect_connection_send_iter() and related functions")
Cc: stable@vger.kernel.org
Cc: Namjae Jeon <linkinjeon@kernel.org>
Cc: Paulo Alcantara <pc@manguebit.org>
Cc: Tom Talpey <tom@talpey.com>
Cc: linux-cifs@vger.kernel.org
Cc: samba-technical@lists.samba.org
Reported-by: Sashiko <sashiko-bot@kernel.org>
Closes: https://sashiko.dev/#/bug/linux-27f04b17-0356-40b9-94be-b0b865fde826
Signed-off-by: Stefan Metzmacher <metze@samba.org>
---
 fs/smb/smbdirect/connection.c | 7 +++++++
 1 file changed, 7 insertions(+)

diff --git a/fs/smb/smbdirect/connection.c b/fs/smb/smbdirect/connection.c
index 837de712db65..9d054eb81d81 100644
--- a/fs/smb/smbdirect/connection.c
+++ b/fs/smb/smbdirect/connection.c
@@ -1352,6 +1352,13 @@ int smbdirect_connection_send_single_iter(struct smbdirect_socket *sc,
 	smbdirect_connection_free_send_io(msg);
 flush_failed:
 alloc_failed:
+	/*
+	 * The peer never got the credits we granted in this message.
+	 */
+	if (new_credits) {
+		atomic_sub(new_credits, &sc->recv_io.credits.count);
+		atomic_add(new_credits, &sc->recv_io.credits.available);
+	}
 	atomic_inc(&sc->send_io.credits.count);
 	wake_up(&sc->send_io.credits.wait_queue);
 credit_failed:
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 14+ messages in thread

end of thread, other threads:[~2026-11-27 16:41 UTC | newest]

Thread overview: 14+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-27 16:40 [PATCH v2 00/13] smb: smbdirect: urgent fixes for teardown races, sends and MR invalidation Stefan Metzmacher
2026-11-27 16:40 ` [PATCH v2 01/13] smb: smbdirect: don't register MRs on a torn-down connection Stefan Metzmacher
2026-11-27 16:40 ` [PATCH v2 02/13] smb: smbdirect: don't post sends " Stefan Metzmacher
2026-11-27 16:40 ` [PATCH v2 03/13] smb: smbdirect: handle errors from smbdirect_iter_to_sgt() Stefan Metzmacher
2026-11-27 16:40 ` [PATCH v2 04/13] smb: smbdirect: give back the send credit if waiting for a recv credit fails Stefan Metzmacher
2026-11-27 16:40 ` [PATCH v2 05/13] smb: smbdirect: don't post a partial PDU after a send error Stefan Metzmacher
2026-11-27 16:40 ` [PATCH v2 06/13] smb: smbdirect: introduce smbdirect_connection_invalidate_rkey() and its logic Stefan Metzmacher
2026-11-27 16:40 ` [PATCH v2 07/13] smb: smbdirect: use separate cqes for MR registration and invalidation Stefan Metzmacher
2026-11-27 16:40 ` [PATCH v2 08/13] smb: smbdirect: don't let INIT_[DELAYED_]WORK() undo a disable from the teardown Stefan Metzmacher
2026-11-27 16:40 ` [PATCH v2 09/13] smb: smbdirect: don't warn about a status changed by a concurrent teardown Stefan Metzmacher
2026-11-27 16:40 ` [PATCH v2 10/13] smb: smbdirect: don't expose DMA addresses as iova of our MRs Stefan Metzmacher
2026-11-27 16:40 ` [PATCH v2 11/13] smb: smbdirect: don't let MR work requests miss the QP drain Stefan Metzmacher
2026-11-27 16:40 ` [PATCH v2 12/13] smb: smbdirect: wake up the credit waiters after a failed send Stefan Metzmacher
2026-11-27 16:40 ` [PATCH v2 13/13] smb: smbdirect: give back granted receive credits if a send fails Stefan Metzmacher

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox