Skip to contentSkip to conversation

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

PATCH New Stefan Metzmacher 1 participant 14 messages Nov 27

Subsystems (5)
lore
Share & tools
lore

Stefan Metzmacher

author reviewer
#0 lore raw

v2 cover 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
author reviewer
#1 lore raw

PATCH v2 1/13 smb: smbdirect: don't register MRs on a torn-down connection

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 Namjae Jeon <[email protected]>
Cc Paulo Alcantara <[email protected]>
Cc Tom Talpey <[email protected]>
---
 fs/smb/smbdirect/mr.c | 26 ++++++++++++++++++++++++++
 1 file changed, 26 insertions(+)
fs/smb/smbdirect/mr.c +26 -0
index 15c6363a2f97..af613a7405e7 100644
@@ -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
Signature · 1 line
2.43.0
author reviewer
#2 lore raw

PATCH v2 2/13 smb: smbdirect: don't post sends on a torn-down connection

In reply to Stefan Metzmacher

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 Namjae Jeon <[email protected]>
Cc Paulo Alcantara <[email protected]>
Cc Tom Talpey <[email protected]>
---
 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(-)
fs/smb/smbdirect/accept.c +2 -1
index 85e37d11f43d..9fb0676a3035 100644
@@ -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)
fs/smb/smbdirect/connect.c +2 -1
index 0d02f03063ac..ce00e6466690 100644
@@ -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)
fs/smb/smbdirect/connection.c +37 -1
index e6c7b343fe87..e375584ab447 100644
@@ -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,
fs/smb/smbdirect/socket.c +14 -0
index e270e36b3145..749a85274f97 100644
@@ -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");
Signature · 1 line
2.43.0
author reviewer
#3 lore raw

PATCH v2 3/13 smb: smbdirect: handle errors from smbdirect_iter_to_sgt()

In reply to Stefan Metzmacher

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 Namjae Jeon <[email protected]>
Cc Paulo Alcantara <[email protected]>
Cc Tom Talpey <[email protected]>
Cc David Howells <[email protected]>
---
 fs/smb/smbdirect/mr.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
fs/smb/smbdirect/mr.c +7 -1
index af613a7405e7..c50cdb1b6cc4 100644
@@ -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) {
Signature · 1 line
2.43.0
author reviewer
#4 lore raw

PATCH v2 4/13 smb: smbdirect: give back the send credit if waiting for a recv credit fails

In reply to Stefan Metzmacher

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 Namjae Jeon <[email protected]>
Cc Paulo Alcantara <[email protected]>
Cc Tom Talpey <[email protected]>
---
 fs/smb/smbdirect/connection.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
fs/smb/smbdirect/connection.c +2 -1
index e375584ab447..c50096e605ee 100644
@@ -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);
 	}
Signature · 1 line
2.43.0
author reviewer
#5 lore raw

PATCH v2 5/13 smb: smbdirect: don't post a partial PDU after a send error

In reply to Stefan Metzmacher

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 Namjae Jeon <[email protected]>
Cc Paulo Alcantara <[email protected]>
Cc Tom Talpey <[email protected]>
Cc Ronnie Sahlberg <[email protected]>
Cc Shyam Prasad N <[email protected]>
Cc Bharath SM <[email protected]>
---
 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(-)
fs/smb/client/smbdirect.c +12 -2
index 563ef488a225..64d27d00079d 100644
@@ -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
fs/smb/smbdirect/connection.c +76 -5

112 lines not on this page · raw message

fs/smb/smbdirect/socket.h +7 -0
index 14114a2567a2..dc1e906316ff 100644
@@ -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 {
include/linux/smbdirect.h +3 -0
index 97f5ba730fa7..74fff00cdf88 100644
@@ -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
Signature · 1 line
2.43.0
author reviewer
#6 lore raw

PATCH v2 6/13 smb: smbdirect: introduce smbdirect_connection_invalidate_rkey() and its logic

In reply to Stefan Metzmacher

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 Namjae Jeon <[email protected]>
Cc Paulo Alcantara <[email protected]>
Cc Tom Talpey <[email protected]>
---
 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(-)
fs/smb/smbdirect/connection.c +8 -4
index 9a824f4f76c5..871750529a98 100644
@@ -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
fs/smb/smbdirect/internal.h +2 -0
index 69d0c9f899de..fec71278421a 100644
@@ -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);
fs/smb/smbdirect/mr.c +113 -6

211 lines not on this page · raw message

fs/smb/smbdirect/socket.h +3 -0
index dc1e906316ff..4dcbd9c86aaf 100644
@@ -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;
include/linux/smbdirect.h +1 -1
index 74fff00cdf88..f797cba849cd 100644
@@ -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);
Signature · 1 line
2.43.0
author reviewer
#7 lore raw

PATCH v2 7/13 smb: smbdirect: use separate cqes for MR registration and invalidation

In reply to Stefan Metzmacher

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 Namjae Jeon <[email protected]>
Cc Paulo Alcantara <[email protected]>
Cc Tom Talpey <[email protected]>
Cc Ronnie Sahlberg <[email protected]>
---
 fs/smb/smbdirect/mr.c     | 12 ++++++------
 fs/smb/smbdirect/socket.h |  9 ++++++++-
 2 files changed, 14 insertions(+), 7 deletions(-)
fs/smb/smbdirect/mr.c +6 -6
index 7c7607c26b0b..68b9ae5ec0c1 100644
@@ -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;
fs/smb/smbdirect/socket.h +8 -1
index 4dcbd9c86aaf..1fc4aebcdda5 100644
@@ -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:
Signature · 1 line
2.43.0
author reviewer
#8 lore raw

PATCH v2 8/13 smb: smbdirect: don't let INIT_[DELAYED_]WORK() undo a disable from the teardown

In reply to Stefan Metzmacher

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 Namjae Jeon <[email protected]>
Cc Paulo Alcantara <[email protected]>
Cc Tom Talpey <[email protected]>
Reported-by [email protected]
---
 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(-)
fs/smb/smbdirect/accept.c +17 -8
index 9fb0676a3035..ddee9438bebf 100644
@@ -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()
fs/smb/smbdirect/connect.c +15 -6
index ce00e6466690..be174af0f259 100644
@@ -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!
 	 */
fs/smb/smbdirect/connection.c +25 -11
index 871750529a98..e84f488b83a3 100644
@@ -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)
fs/smb/smbdirect/internal.h +2 -0
index fec71278421a..7044dd47c339 100644
@@ -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);
fs/smb/smbdirect/listen.c +17 -2
index 3c91f00d95ce..2c4a2f249f45 100644
@@ -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",
fs/smb/smbdirect/socket.c +61 -32

128 lines not on this page · raw message

fs/smb/smbdirect/socket.h +12 -2
index 1fc4aebcdda5..1520b6e48e7d 100644
@@ -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);
 
Signature · 1 line
2.43.0
author reviewer
#9 lore raw

PATCH v2 9/13 smb: smbdirect: don't warn about a status changed by a concurrent teardown

In reply to Stefan Metzmacher

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 Namjae Jeon <[email protected]>
Cc Paulo Alcantara <[email protected]>
Cc Tom Talpey <[email protected]>
Reported-by [email protected]
---
 fs/smb/smbdirect/socket.c | 21 ++++++++++++++++++---
 fs/smb/smbdirect/socket.h | 18 +++++++++++++++---
 2 files changed, 33 insertions(+), 6 deletions(-)
fs/smb/smbdirect/socket.c +18 -3
index cb090c558321..53be81077adb 100644
@@ -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",
fs/smb/smbdirect/socket.h +15 -3
index 1520b6e48e7d..c669778fac4b 100644
@@ -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; \
 })
Signature · 1 line
2.43.0
author reviewer
#10 lore raw

PATCH v2 10/13 smb: smbdirect: don't expose DMA addresses as iova of our MRs

In reply to Stefan Metzmacher

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 Namjae Jeon <[email protected]>
Cc Paulo Alcantara <[email protected]>
Cc Tom Talpey <[email protected]>
Cc Tatyana Nikolova <[email protected]>
Cc Jacob Moroni <[email protected]>
---
 fs/smb/smbdirect/mr.c | 29 +++++++++++++++++++++++++++++
 1 file changed, 29 insertions(+)
fs/smb/smbdirect/mr.c +29 -0
index 68b9ae5ec0c1..760a6049182a 100644
@@ -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));
Signature · 1 line
2.43.0
author reviewer
#11 lore raw

PATCH v2 11/13 smb: smbdirect: don't let MR work requests miss the QP drain

In reply to Stefan Metzmacher

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 Namjae Jeon <[email protected]>
Cc Paulo Alcantara <[email protected]>
Cc Tom Talpey <[email protected]>
---
 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(-)
fs/smb/smbdirect/internal.h +2 -0
index 7044dd47c339..1ce89bbfaf03 100644
@@ -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,
fs/smb/smbdirect/mr.c +27 -1
index 760a6049182a..c56cea10bb73 100644
@@ -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
fs/smb/smbdirect/socket.c +2 -0
index 53be81077adb..f4eae1792790 100644
@@ -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");
Signature · 1 line
2.43.0
author reviewer
#12 lore raw

PATCH v2 12/13 smb: smbdirect: wake up the credit waiters after a failed send

In reply to Stefan Metzmacher

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 Namjae Jeon <[email protected]>
Cc Paulo Alcantara <[email protected]>
Cc Tom Talpey <[email protected]>
---
 fs/smb/smbdirect/connection.c | 14 ++++++++++++--
 1 file changed, 12 insertions(+), 2 deletions(-)
fs/smb/smbdirect/connection.c +12 -2
index e84f488b83a3..837de712db65 100644
@@ -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;
 }
Signature · 1 line
2.43.0
author reviewer
#13 lore raw

PATCH v2 13/13 smb: smbdirect: give back granted receive credits if a send fails

In reply to Stefan Metzmacher

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 Namjae Jeon <[email protected]>
Cc Paulo Alcantara <[email protected]>
Cc Tom Talpey <[email protected]>
---
 fs/smb/smbdirect/connection.c | 7 +++++++
 1 file changed, 7 insertions(+)
fs/smb/smbdirect/connection.c +7 -0
index 837de712db65..9d054eb81d81 100644
@@ -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:
Signature · 1 line
2.43.0