Skip to content

smb: smbdirect: urgent fixes for teardown races, sends and MR invalidation

Stefan Metzmacher linux-cifs linux-rdma stable 2 versions New

Get series with b4
b4 am [email protected]

Versions newest first · linked by author and title

  1. v2 Latest13 patches 0 repliesNew
  2. v110 patches 0 repliesSuperseded

Changes in v2 from the cover letter

- Patch 1: if smbdirect_connection_destroy_mr_list() already

Patches in v2 No applies recorded yet

#Subject and filesSizeRepliesApplied
1/13smb: smbdirect: don't register MRs on a torn-down connectionfs/smb/smbdirect/mr.c+26 -00 replies
2/13smb: smbdirect: don't post sends on a torn-down connectionfs/smb/smbdirect/accept.c fs/smb/smbdirect/connect.c fs/smb/smbdirect/connection.c +1 more+55 -30 replies
3/13smb: smbdirect: handle errors from smbdirect_iter_to_sgt()fs/smb/smbdirect/mr.c+7 -10 replies
4/13smb: smbdirect: give back the send credit if waiting for a recv credit failsfs/smb/smbdirect/connection.c+2 -10 replies
5/13smb: smbdirect: don't post a partial PDU after a send errorfs/smb/client/smbdirect.c fs/smb/smbdirect/connection.c fs/smb/smbdirect/socket.h +1 more+98 -70 replies
6/13smb: smbdirect: introduce smbdirect_connection_invalidate_rkey() and its logicfs/smb/smbdirect/connection.c fs/smb/smbdirect/internal.h fs/smb/smbdirect/mr.c +2 more+127 -110 replies
7/13smb: smbdirect: use separate cqes for MR registration and invalidationfs/smb/smbdirect/mr.c fs/smb/smbdirect/socket.h+14 -70 replies
8/13smb: smbdirect: don't let INIT_[DELAYED_]WORK() undo a disable from the teardownfs/smb/smbdirect/accept.c fs/smb/smbdirect/connect.c fs/smb/smbdirect/connection.c +4 more+149 -610 replies
9/13smb: smbdirect: don't warn about a status changed by a concurrent teardownfs/smb/smbdirect/socket.c fs/smb/smbdirect/socket.h+33 -60 replies
10/13smb: smbdirect: don't expose DMA addresses as iova of our MRsfs/smb/smbdirect/mr.c+29 -00 replies
11/13smb: smbdirect: don't let MR work requests miss the QP drainfs/smb/smbdirect/internal.h fs/smb/smbdirect/mr.c fs/smb/smbdirect/socket.c+31 -10 replies
12/13smb: smbdirect: wake up the credit waiters after a failed sendfs/smb/smbdirect/connection.c+12 -20 replies
13/13smb: smbdirect: give back granted receive credits if a send failsfs/smb/smbdirect/connection.c+7 -00 replies

Reviews trailers per patch

No Reviewed-by, Acked-by or Tested-by trailers for v2 yet.

Read the v2 thread

Cover letter [PATCH v2 00/13] smb: smbdirect: urgent fixes for teardown races, sends and MR invalidation

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/[email protected]/

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(-)
Signature · 1 line
2.43.0