* [PATCH v5 00/20] smb: smbdirect: urgent fixes for teardown races, sends and MR invalidation
@ 2026-11-30 22:28 Stefan Metzmacher
2026-11-30 22:29 ` [PATCH v5 10/20] smb: smbdirect: don't expose DMA addresses as iova of our MRs Stefan Metzmacher
2026-11-30 22:50 ` [PATCH v5 00/20] smb: smbdirect: urgent fixes for teardown races, sends and MR invalidation sashiko-bot
0 siblings, 2 replies; 3+ messages in thread
From: Stefan Metzmacher @ 2026-11-30 22:28 UTC (permalink / raw)
To: Namjae Jeon, Paulo Alcantara
Cc: metze, Ronnie Sahlberg, Shyam Prasad N, Tom Talpey, Bharath SM,
David Howells, Tatyana Nikolova, Jacob Moroni, linux-cifs,
linux-rdma, 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 connect and accept code checked the status and then set the
status of the next step, a concurrent teardown in between was
overwritten, so rdma_disconnect() could be skipped and
rdma_accept() was called for a socket being torn down. A listener
being torn down could still accept new connections (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.
- If the connection was torn down while an IB_WR_REG_MR was still
pending, deregistering the mr didn't wait for it, so mr->mr was
deregistered and the pages unmapped before the QP was drained
(reported by Sashiko).
- The backlog of a listener was only set after rdma_listen()
returned, so early connect requests ignored the backlog and if
rdma_listen() failed for a later device, the cleanup skipped the
child sockets and the listener was freed without RCU (reported by
Sashiko).
- Concurrent teardowns changed the status without a lock, so it
could move backwards, e.g. from SMBDIRECT_SOCKET_DISCONNECTED to
SMBDIRECT_SOCKET_ERROR (reported by Sashiko).
- smbdirect_socket_accept() changed the status of a ready socket
without the lock of that socket, so it could hand out a socket whose
teardown had just started (reported by Sashiko).
- A teardown racing with the connection setup triggered a spurious
WARN_ON_ONCE(!recv_io) (reported by Sashiko).
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
- https://sashiko.dev/#/bug/linux-a45eaa5a-b564-4557-bd53-2c0b037a6acb
smb: smbdirect: don't let the connection setup overwrite a teardown status
- https://sashiko.dev/#/bug/linux-c32e9d93-62f2-4b1e-9c3a-5c3325ce1081
smb: smbdirect: don't let the connection setup overwrite a teardown status
smb: smbdirect: use the socket argument in __SMBDIRECT_CHECK_STATUS_WARN()
smb: smbdirect: don't accept new connections on a listener being torn down
- https://sashiko.dev/#/bug/linux-200f771b-df1a-475d-9279-29e23b8f6c3e
smb: smbdirect: don't deregister an MR before its registration completed
- https://sashiko.dev/#/bug/linux-2d69ea06-22d7-411a-9dd0-04928cfb513e
smb: smbdirect: set the listen backlog before calling rdma_listen()
- https://sashiko.dev/#/bug/linux-f94f1a82-7d57-4986-8f96-28f3bd02767d
smb: smbdirect: don't let concurrent teardowns move the status backwards
- https://sashiko.dev/#/bug/linux-cadbc11c-02d9-4f3a-93f8-fa3a27f45acd
smb: smbdirect: don't let the connection setup overwrite a teardown status
- https://sashiko.dev/#/bug/linux-eb934b08-9b80-4e13-a535-6332a3d53abb
smb: smbdirect: don't warn if a teardown races with the connection setup
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, v2, v3, v4 and again v5 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 v5 (thanks to the review by Sashiko):
- Patch 14: smbdirect_socket_accept() also uses
smbdirect_socket_set_status() now, instead of checking first_error
and using cmpxchg() without the cleanup_lock of the child socket.
- Patch 17: count the pending IB_WR_REG_MR of an mr instead of using a
completion: wait_for_completion() consumed the single complete(),
so the wait after the retry hung, and the IB_WR_REG_MR of an
earlier registration of the mr, that the peer invalidated, could
complete the wait for the next one.
smbdirect_connection_destroy_mr_list() also wakes the waiter once
the mr is disabled, and the IB_WR_LOCAL_INV post failure keeps the
used.count accounting.
- Patch 19: smbdirect_socket_cleanup_work() only calls
rdma_disconnect() if the status is still
SMBDIRECT_SOCKET_DISCONNECTING under the handler lock, an
RDMA_CM_EVENT_DISCONNECTED or RDMA_CM_EVENT_DEVICE_REMOVAL may have
moved it to SMBDIRECT_SOCKET_DISCONNECTED after cleanup_lock was
dropped. Also fixed stale comments about accept.listener.
- New patch 20 for a spurious WARN_ON_ONCE() found by Sashiko.
v4: https://lore.kernel.org/linux-cifs/cover.1796036054.git.metze@samba.org/
Changes in v4:
- Added the Reviewed-by of Paulo to patches 1-16, thanks!
- Patch 4: the code comes from the server side, so the Fixes: tags
are 8cf2bbac6281 and 54abc694ebb8 instead of 93ac432274e1.
- Patch 15: the Fixes: tag is 2de35d992ba1, which added the log calls
using sc, instead of 1adb2dab9727.
- New patches 17-19 for more problems found by Sashiko in the
unchanged code.
v3: https://lore.kernel.org/linux-cifs/cover.1795891083.git.metze@samba.org/
Changes in v3 (thanks to the review by Sashiko):
- Patch 2: document in smbdirect_socket_destroy() that the owners of
the socket don't send while they call smbdirect_socket_release(),
the re-check of first_error and the wait for
send_io.pending.count are only a safety net, the commit message
says that too.
- Patch 3: also fail if smbdirect_iter_to_sgt() extracted less than
the whole buffer, an MR covering only a part of the buffer must
not be registered.
- Patch 8: immediate_work and refill_work are activated separately,
immediate_work on the accept side before the idle timer is armed,
as the timer may queue it as soon as smbdirect_socket_accept()
changed the status to SMBDIRECT_SOCKET_CONNECTED.
- New patches 14 and 16 for the status races with a concurrent
teardown found by Sashiko in the unchanged code, patch 15 fixes
__SMBDIRECT_CHECK_STATUS_WARN() to use its socket argument, which
patch 16 needs.
v2: https://lore.kernel.org/linux-cifs/cover.1795794871.git.metze@samba.org/
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-rc5, like the other smb series. The patches also apply
cleanly (git am) on cifs-next (00f21240b7d3, which has the urgent
client series), on ksmbd-for-next (c486f982a188) and on both merged,
and build on both merged.
Stefan Metzmacher (20):
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
smb: smbdirect: don't let the connection setup overwrite a teardown
status
smb: smbdirect: use the socket argument in
__SMBDIRECT_CHECK_STATUS_WARN()
smb: smbdirect: don't accept new connections on a listener being torn
down
smb: smbdirect: don't deregister an MR before its registration
completed
smb: smbdirect: set the listen backlog before calling rdma_listen()
smb: smbdirect: don't let concurrent teardowns move the status
backwards
smb: smbdirect: don't warn if a teardown races with the connection
setup
fs/smb/client/smbdirect.c | 14 +-
fs/smb/smbdirect/accept.c | 90 ++++++---
fs/smb/smbdirect/connect.c | 74 +++++--
fs/smb/smbdirect/connection.c | 217 ++++++++++++++++++---
fs/smb/smbdirect/internal.h | 12 ++
fs/smb/smbdirect/listen.c | 66 +++++--
fs/smb/smbdirect/mr.c | 296 ++++++++++++++++++++++++++--
fs/smb/smbdirect/socket.c | 355 ++++++++++++++++++++++++----------
fs/smb/smbdirect/socket.h | 62 +++++-
include/linux/smbdirect.h | 5 +-
10 files changed, 973 insertions(+), 218 deletions(-)
--
2.43.0
^ permalink raw reply [flat|nested] 3+ messages in thread
* [PATCH v5 10/20] smb: smbdirect: don't expose DMA addresses as iova of our MRs
2026-11-30 22:28 [PATCH v5 00/20] smb: smbdirect: urgent fixes for teardown races, sends and MR invalidation Stefan Metzmacher
@ 2026-11-30 22:29 ` Stefan Metzmacher
2026-11-30 22:50 ` [PATCH v5 00/20] smb: smbdirect: urgent fixes for teardown races, sends and MR invalidation sashiko-bot
1 sibling, 0 replies; 3+ messages in thread
From: Stefan Metzmacher @ 2026-11-30 22:29 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
Reviewed-by: Paulo Alcantara <pc@manguebit.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 6c37206474bb..dd64ec50bec1 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
@@ -314,6 +315,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);
@@ -393,6 +396,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] 3+ messages in thread
* Re: [PATCH v5 00/20] smb: smbdirect: urgent fixes for teardown races, sends and MR invalidation
2026-11-30 22:28 [PATCH v5 00/20] smb: smbdirect: urgent fixes for teardown races, sends and MR invalidation Stefan Metzmacher
2026-11-30 22:29 ` [PATCH v5 10/20] smb: smbdirect: don't expose DMA addresses as iova of our MRs Stefan Metzmacher
@ 2026-11-30 22:50 ` sashiko-bot
1 sibling, 0 replies; 3+ messages in thread
From: sashiko-bot @ 2026-11-30 22:50 UTC (permalink / raw)
To: Stefan Metzmacher; +Cc: linux-rdma
Sashiko has reviewed this 20-patch series and found no issues.
--
Sashiko AI review · https://sashiko.dev/#/patchset/cover.1796077472.git.metze@samba.org
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2026-11-30 22:50 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-30 22:28 [PATCH v5 00/20] smb: smbdirect: urgent fixes for teardown races, sends and MR invalidation Stefan Metzmacher
2026-11-30 22:29 ` [PATCH v5 10/20] smb: smbdirect: don't expose DMA addresses as iova of our MRs Stefan Metzmacher
2026-11-30 22:50 ` [PATCH v5 00/20] smb: smbdirect: urgent fixes for teardown races, sends and MR invalidation sashiko-bot
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox