smbdirect_connection_register_mr_io() took an mr from smbdirect_connection_get_mr_io() (which checks the status while the connection is still connected), then dropped out to acquire mr->mutex and only afterwards touched mr->mr and sc->ib.qp via ib_map_mr_sg() and ib_post_send(). A concurrent smbdirect_socket_release() can tear the connection down in that window: smbdirect_connection_destroy_mr_list() disables the mr (mr->mr = NULL) and smbdirect_connection_destroy_qp() zeroes sc->ib.qp. The I/O thread would then dereference the NULL mr->mr / sc->ib.qp. Keeping the memory of the socket alive doesn't keep the connection connected, so this race is reachable. Re-check sc->status under mr->mutex before using the mr. While we hold mr->mutex and the status is still CONNECTED, smbdirect_connection_destroy_mr_list() - which runs before smbdirect_connection_destroy_qp() - blocks on this mutex, so the QP can't be destroyed under the ib_post_send() below; if the teardown got there first, the status is no longer CONNECTED and we bail out via the existing error path. The similar unlocked sc->ib.qp access in smbdirect_connection_post_send_wr() is fixed by "smb: smbdirect: don't post sends on a torn-down connection". If smbdirect_connection_destroy_mr_list() got mr->mutex first, it already disabled the mr, detached it from the socket and dropped its reference. Then only our reference is dropped, without setting mr->state to SMBDIRECT_MR_ERROR, otherwise the final kref_put() would call smbdirect_mr_io_disable_locked() again with mr->socket == NULL. An IB_WR_REG_MR posted after smbdirect_socket_destroy() drained the QP can still complete after smbdirect_connection_destroy_mr_list(), that is fixed by "smb: smbdirect: don't let MR work requests miss the QP drain". Fixes: de5ef8ec3c46 ("smb: smbdirect: introduce smbdirect_mr.c with client mr code") Cc: stable@vger.kernel.org Cc: Namjae Jeon Cc: Paulo Alcantara Cc: Tom Talpey Cc: linux-cifs@vger.kernel.org Cc: samba-technical@lists.samba.org Reported-by: Sashiko Closes: https://sashiko.dev/#/patchset/cover.1791225453.git.metze%40samba.org Reviewed-by: Paulo Alcantara Signed-off-by: Stefan Metzmacher --- fs/smb/smbdirect/mr.c | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/fs/smb/smbdirect/mr.c b/fs/smb/smbdirect/mr.c index 15c6363a2f97..af613a7405e7 100644 --- a/fs/smb/smbdirect/mr.c +++ b/fs/smb/smbdirect/mr.c @@ -295,6 +295,31 @@ smbdirect_connection_register_mr_io(struct smbdirect_socket *sc, mr->sgt.nents = 0; mr->sgt.orig_nents = 0; + /* + * smbdirect_connection_get_mr_io() returned this mr while the + * connection was still connected, but smbdirect_socket_release() + * may have started tearing it down since then. Re-check under + * mr->mutex before touching mr->mr and sc->ib.qp below. + * + * While we hold mr->mutex and the status is still CONNECTED, + * smbdirect_connection_destroy_mr_list() (which runs before + * smbdirect_connection_destroy_qp() zeroes sc->ib.qp) blocks on + * this mutex, so the QP can't be destroyed under the + * ib_post_send() below. + * + * If the teardown already disabled this mr, it also detached it + * from the socket and dropped its reference, so we only drop + * ours, without changing mr->state or touching sc. Otherwise the + * status is no longer CONNECTED once the teardown started, so + * bail out. + */ + if (mr->state == SMBDIRECT_MR_DISABLED) + goto put_kref; + if (sc->status != SMBDIRECT_SOCKET_CONNECTED) { + ret = -ECONNABORTED; + goto dma_map_error; + } + smbdirect_log_rdma_mr(sc, SMBDIRECT_LOG_INFO, "num_pages=%u count=%zu depth=%u\n", num_pages, iov_iter_count(iter), sp->max_frmr_depth); @@ -363,6 +388,7 @@ smbdirect_connection_register_mr_io(struct smbdirect_socket *sc, smbdirect_socket_schedule_cleanup(sc, ret); +put_kref: /* * smbdirect_connection_get_mr_io() gave us a reference * via kref_get(&mr->kref), we need to remove it again -- 2.43.0 smbdirect_connection_post_send_wr() only checked sc->first_error before ib_post_send(sc->ib.qp, ...). If smbdirect_socket_release() ran concurrently, the teardown could reach smbdirect_connection_destroy_qp(), which zeroes sc->ib.qp, in the window between that check and the ib_post_send(), leading to a NULL pointer dereference. The owners of the socket don't do that: cifs serializes smbd_send() and smbd_destroy() with cifs_server_lock() and ksmbd waits for all requests before it frees the transport, our own work items that send are disabled at the start of smbdirect_socket_destroy(). But smbdirect neither documented that contract nor protected itself. Document it in smbdirect_socket_destroy() and add a safety net: re-check sc->first_error after incrementing send_io.pending.count, and wait in smbdirect_socket_destroy() for send_io.pending.count to reach 0 before the QP and the memory pools are destroyed. The send completions that drop send_io.pending.count run on the CQ workqueue and don't take the handler lock held during destroy, and the QP is already drained at that point, so the wait does not block. smbdirect_connection_send_io_done() also has to drop send_io.pending.count for a signaled send completing with an error, e.g. after a link down, otherwise smbdirect_socket_destroy() would wait forever. The unsignaled siblings arriving via skip_free are not counted. smbdirect_connect_negotiate_send_done() and smbdirect_accept_negotiate_send_done() now also wake up send_io.pending.zero_wait_queue when the count reaches 0, like all other places. They can't hang smbdirect_socket_destroy(), as it calls ib_drain_qp() before waiting, but we shouldn't depend on that. Fixes: 422a2436697d ("smb: smbdirect: introduce smbdirect_socket_destroy[_sync]()") Cc: stable@vger.kernel.org Cc: Namjae Jeon Cc: Paulo Alcantara Cc: Tom Talpey Cc: linux-cifs@vger.kernel.org Cc: samba-technical@lists.samba.org Reported-by: Sashiko Closes: https://sashiko.dev/#/patchset/cover.1791225453.git.metze%40samba.org Reviewed-by: Paulo Alcantara Signed-off-by: Stefan Metzmacher --- fs/smb/smbdirect/accept.c | 3 ++- fs/smb/smbdirect/connect.c | 3 ++- fs/smb/smbdirect/connection.c | 38 ++++++++++++++++++++++++++++++++++- fs/smb/smbdirect/socket.c | 18 +++++++++++++++++ 4 files changed, 59 insertions(+), 3 deletions(-) diff --git a/fs/smb/smbdirect/accept.c b/fs/smb/smbdirect/accept.c index 85e37d11f43d..9fb0676a3035 100644 --- a/fs/smb/smbdirect/accept.c +++ b/fs/smb/smbdirect/accept.c @@ -697,7 +697,8 @@ static void smbdirect_accept_negotiate_send_done(struct ib_cq *cq, struct ib_wc /* Note this frees wc->wr_cqe, but not wc */ smbdirect_connection_free_send_io(send_io); - atomic_dec(&sc->send_io.pending.count); + if (atomic_dec_and_test(&sc->send_io.pending.count)) + wake_up(&sc->send_io.pending.zero_wait_queue); if (unlikely(wc->status != IB_WC_SUCCESS || WARN_ON_ONCE(wc->opcode != IB_WC_SEND))) { if (wc->status != IB_WC_WR_FLUSH_ERR) diff --git a/fs/smb/smbdirect/connect.c b/fs/smb/smbdirect/connect.c index 0d02f03063ac..ce00e6466690 100644 --- a/fs/smb/smbdirect/connect.c +++ b/fs/smb/smbdirect/connect.c @@ -560,7 +560,8 @@ static void smbdirect_connect_negotiate_send_done(struct ib_cq *cq, struct ib_wc /* Note this frees wc->wr_cqe, but not wc */ smbdirect_connection_free_send_io(send_io); - atomic_dec(&sc->send_io.pending.count); + if (atomic_dec_and_test(&sc->send_io.pending.count)) + wake_up(&sc->send_io.pending.zero_wait_queue); if (unlikely(wc->status != IB_WC_SUCCESS || WARN_ON_ONCE(wc->opcode != IB_WC_SEND))) { if (wc->status != IB_WC_WR_FLUSH_ERR) diff --git a/fs/smb/smbdirect/connection.c b/fs/smb/smbdirect/connection.c index e6c7b343fe87..e375584ab447 100644 --- a/fs/smb/smbdirect/connection.c +++ b/fs/smb/smbdirect/connection.c @@ -885,13 +885,32 @@ int smbdirect_connection_post_send_wr(struct smbdirect_socket *sc, return sc->first_error; atomic_inc(&sc->send_io.pending.count); + /* + * Re-check first_error after counting ourselves in + * send_io.pending.count, and pair it with the drain in + * smbdirect_socket_destroy(): that path sets first_error, drains + * the QP and then waits for send_io.pending.count to reach 0 + * before smbdirect_connection_destroy_qp() zeroes sc->ib.qp. So + * either we observe first_error here and back off before touching + * sc->ib.qp, or our increment is observed by that wait and the QP + * stays alive across the ib_post_send() below. + */ + smp_mb__after_atomic(); + if (unlikely(sc->first_error)) { + ret = sc->first_error; + if (atomic_dec_and_test(&sc->send_io.pending.count)) + wake_up(&sc->send_io.pending.zero_wait_queue); + return ret; + } + ret = ib_post_send(sc->ib.qp, wr, NULL); if (ret) { - atomic_dec(&sc->send_io.pending.count); smbdirect_log_rdma_send(sc, SMBDIRECT_LOG_ERR, "ib_post_send() failed %1pe\n", SMBDIRECT_DEBUG_ERR_PTR(ret)); smbdirect_socket_schedule_cleanup(sc, ret); + if (atomic_dec_and_test(&sc->send_io.pending.count)) + wake_up(&sc->send_io.pending.zero_wait_queue); } return ret; @@ -1406,6 +1425,12 @@ static void smbdirect_connection_send_io_done(struct ib_cq *cq, struct ib_wc *wc * of the siblings too, which will cause * use after free problems for the others * triggered from ib_drain_qp(). + * + * We also must not drop send_io.pending.count + * here: smbdirect_connection_post_send_wr() + * counted the whole chain only once and the + * signaled final message drops it, also on + * failure, as all posted messages are flushed. */ if (wc->status != IB_WC_SUCCESS) goto skip_free; @@ -1435,6 +1460,17 @@ static void smbdirect_connection_send_io_done(struct ib_cq *cq, struct ib_wc *wc lcredits += 1; if (unlikely(wc->status != IB_WC_SUCCESS || WARN_ON_ONCE(wc->opcode != IB_WC_SEND))) { + /* + * The signaled send_io was counted in + * send_io.pending.count by + * smbdirect_connection_post_send_wr(), + * so drop it also on failure, otherwise + * smbdirect_socket_destroy() waits forever. + * The unsignaled siblings arriving via + * skip_free were not counted. + */ + if (atomic_dec_and_test(&sc->send_io.pending.count)) + wake_up(&sc->send_io.pending.zero_wait_queue); skip_free: if (wc->status != IB_WC_WR_FLUSH_ERR) smbdirect_log_rdma_send(sc, SMBDIRECT_LOG_ERR, diff --git a/fs/smb/smbdirect/socket.c b/fs/smb/smbdirect/socket.c index e270e36b3145..5d995694a74e 100644 --- a/fs/smb/smbdirect/socket.c +++ b/fs/smb/smbdirect/socket.c @@ -669,6 +669,24 @@ static void smbdirect_socket_destroy(struct smbdirect_socket *sc) "freeing mr list\n"); smbdirect_connection_destroy_mr_list(sc); + /* + * smbdirect_socket_release() is only called once the owner of + * the socket no longer sends on it: cifs serializes smbd_send() + * and smbd_destroy() with cifs_server_lock() and ksmbd waits for + * all requests before it frees the transport. Our own work items + * that send were disabled above. + * + * So this is only a safety net: first_error is set by now, so + * smbdirect_connection_post_send_wr() backs off after its + * re-check, and the drain above completed all posted messages, + * this only makes sure every send_io.pending.count reference is + * dropped before the QP and the memory pools are destroyed. It + * does not block, the send completions run on the CQ workqueue + * and don't take the handler lock we hold here. + */ + wait_event(sc->send_io.pending.zero_wait_queue, + atomic_read(&sc->send_io.pending.count) == 0); + if (sc->listen.backlog == -1) /* was not a listener */ smbdirect_log_rdma_event(sc, SMBDIRECT_LOG_INFO, "destroying qp\n"); -- 2.43.0 smbdirect_connection_register_mr_io() ignored the return value of smbdirect_iter_to_sgt(), so a failing extract_iter_to_sg() went on with an incomplete or empty sg table. Fail the registration instead. Fixes: d08089f649a0 ("cifs: Change the I/O paths to use an iterator rather than a page list") Cc: stable@vger.kernel.org Cc: Namjae Jeon Cc: Paulo Alcantara Cc: Tom Talpey Cc: David Howells Cc: linux-cifs@vger.kernel.org Cc: samba-technical@lists.samba.org Reviewed-by: Paulo Alcantara Signed-off-by: Stefan Metzmacher --- fs/smb/smbdirect/mr.c | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/fs/smb/smbdirect/mr.c b/fs/smb/smbdirect/mr.c index af613a7405e7..182bf64426ea 100644 --- a/fs/smb/smbdirect/mr.c +++ b/fs/smb/smbdirect/mr.c @@ -242,15 +242,24 @@ static int smbdirect_iter_to_sgt(struct iov_iter *iter, struct sg_table *sgt, unsigned int max_sg) { - int ret; + size_t len = iov_iter_count(iter); + ssize_t ret; memset(sgt->sgl, 0, max_sg * sizeof(struct scatterlist)); - ret = extract_iter_to_sg(iter, iov_iter_count(iter), sgt, max_sg, 0); + ret = extract_iter_to_sg(iter, len, sgt, max_sg, 0); WARN_ON(ret < 0); if (sgt->nents > 0) sg_mark_end(&sgt->sgl[sgt->nents - 1]); + /* + * Kernel iterators return the bytes extracted so far if the + * extraction stopped early, an MR covering only a part of the + * buffer must not be registered. + */ + if (ret >= 0 && ret != len) + return -EIO; + return ret; } @@ -323,7 +332,13 @@ smbdirect_connection_register_mr_io(struct smbdirect_socket *sc, smbdirect_log_rdma_mr(sc, SMBDIRECT_LOG_INFO, "num_pages=%u count=%zu depth=%u\n", num_pages, iov_iter_count(iter), sp->max_frmr_depth); - smbdirect_iter_to_sgt(iter, &mr->sgt, sp->max_frmr_depth); + ret = smbdirect_iter_to_sgt(iter, &mr->sgt, sp->max_frmr_depth); + if (ret < 0) { + smbdirect_log_rdma_mr(sc, SMBDIRECT_LOG_ERR, + "smbdirect_iter_to_sgt() num_pages=%u dir=%x ret=%d (%1pe)\n", + num_pages, mr->dir, ret, SMBDIRECT_DEBUG_ERR_PTR(ret)); + goto dma_map_error; + } num_mapped = ib_dma_map_sg(sc->ib.dev, mr->sgt.sgl, mr->sgt.nents, mr->dir); if (!num_mapped) { -- 2.43.0 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: 8cf2bbac6281 ("smb: server: fix last send credit problem causing disconnects") Fixes: 54abc694ebb8 ("smb: smbdirect: introduce smbdirect_connection_send_iter() and related functions") Cc: stable@vger.kernel.org Cc: Namjae Jeon Cc: Paulo Alcantara Cc: Tom Talpey Cc: linux-cifs@vger.kernel.org Cc: samba-technical@lists.samba.org Reviewed-by: Paulo Alcantara Signed-off-by: Stefan Metzmacher --- fs/smb/smbdirect/connection.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/fs/smb/smbdirect/connection.c b/fs/smb/smbdirect/connection.c index e375584ab447..c50096e605ee 100644 --- a/fs/smb/smbdirect/connection.c +++ b/fs/smb/smbdirect/connection.c @@ -1184,8 +1184,9 @@ int smbdirect_connection_send_single_iter(struct smbdirect_socket *sc, sc->status != SMBDIRECT_SOCKET_CONNECTED); if (sc->status != SMBDIRECT_SOCKET_CONNECTED) ret = -ENOTCONN; + /* we already have a send credit to give back */ if (ret < 0) - goto credit_failed; + goto alloc_failed; new_credits = smbdirect_connection_grant_recv_credits(sc); } -- 2.43.0 All fragments of a PDU are built into the same smbdirect_send_batch by smbdirect_connection_send_single_iter(). If building a fragment failed, e.g. because a credit wait was interrupted by a fatal signal or a DMA mapping failed, smbdirect_connection_send_iter() and the smbd_send() of the client still flushed the batch with is_last=true. That posts the fragments built so far, the last of them with a remaining_data_length that is never satisfied, fragments may also have been posted already by a flush with is_last=false. The peer then appends the next PDU to the incomplete one: cifs reconnects asynchronously, ksmbd keeps the connection and its next response continues the truncated one. Add smbdirect_connection_send_batch_abort() and use it instead of the final flush after an error: if nothing of the PDU was built yet, it only gives back the batch credit, otherwise it frees the messages not posted yet and tears down the connection, as the peer can no longer get a consistent stream. smbdirect_connection_send_wait_zero_pending() now also waits for all posted messages if the connection is broken, before it returned as soon as the status changed, which happens right away in smbdirect_connection_send_batch_abort(), while the device could still read the pages of the caller for fragments posted by a flush with is_last=false. They complete with IB_WC_WR_FLUSH_ERR once the teardown moved the QP to the error state. smbdirect_connection_send_single_iter() gives back the batch credit itself on its error paths, without waking up the waiters, so smbdirect_connection_send_batch_abort() doesn't see it, that is fixed by "smb: smbdirect: wake up the credit waiters after a failed send". Fixes: 2c1ac39ce9cd ("smb: client: use smbdirect_send_batch processing") Fixes: 54abc694ebb8 ("smb: smbdirect: introduce smbdirect_connection_send_iter() and related functions") Cc: stable@vger.kernel.org Cc: Namjae Jeon Cc: Paulo Alcantara Cc: Tom Talpey Cc: Ronnie Sahlberg Cc: Shyam Prasad N Cc: Bharath SM Cc: linux-cifs@vger.kernel.org Cc: samba-technical@lists.samba.org Reviewed-by: Paulo Alcantara Signed-off-by: Stefan Metzmacher --- fs/smb/client/smbdirect.c | 14 +++++- fs/smb/smbdirect/connection.c | 81 ++++++++++++++++++++++++++++++++--- fs/smb/smbdirect/socket.h | 7 +++ include/linux/smbdirect.h | 3 ++ 4 files changed, 98 insertions(+), 7 deletions(-) diff --git a/fs/smb/client/smbdirect.c b/fs/smb/client/smbdirect.c index 563ef488a225..64d27d00079d 100644 --- a/fs/smb/client/smbdirect.c +++ b/fs/smb/client/smbdirect.c @@ -485,9 +485,19 @@ int smbd_send(struct TCP_Server_Info *server, } while (++rqst_idx < num_rqst); - rc = smbdirect_connection_send_batch_flush(sc, batch, true); - if (unlikely(!rc && error)) + if (unlikely(error)) { + /* + * Don't post a partial PDU, if some of it was + * already built, this tears down the connection. + * Fragments may have been posted already, so we + * still wait for them below, they may use the + * pages of the request. + */ + smbdirect_connection_send_batch_abort(sc, batch); rc = error; + } else { + rc = smbdirect_connection_send_batch_flush(sc, batch, true); + } /* * As an optimization, we don't wait for individual I/O to finish diff --git a/fs/smb/smbdirect/connection.c b/fs/smb/smbdirect/connection.c index c50096e605ee..9a824f4f76c5 100644 --- a/fs/smb/smbdirect/connection.c +++ b/fs/smb/smbdirect/connection.c @@ -925,6 +925,7 @@ static void smbdirect_connection_send_batch_init(struct smbdirect_send_batch *ba batch->need_invalidate_rkey = need_invalidate_rkey; batch->remote_key = remote_key; batch->credit = 0; + batch->msg_cnt = 0; } int smbdirect_connection_send_batch_flush(struct smbdirect_socket *sc, @@ -988,6 +989,58 @@ int smbdirect_connection_send_batch_flush(struct smbdirect_socket *sc, } EXPORT_SYMBOL_GPL(smbdirect_connection_send_batch_flush); +/* + * Abort the PDU of a batch after an error, instead of flushing it with + * is_last=true. + * + * All fragments of a PDU use the same batch. If some of them were + * already added to the batch, possibly posted by a flush with + * is_last=false, the peer can no longer get a complete PDU, its + * remaining_data_length would just continue with the next PDU. So the + * messages not posted yet are freed and the connection is torn down. + * + * If nothing of the PDU was built yet, e.g. the wait for the first + * credit failed, only the batch credit is given back. + */ +void smbdirect_connection_send_batch_abort(struct smbdirect_socket *sc, + struct smbdirect_send_batch *batch) +{ + struct smbdirect_send_io *msg, *next; + u32 freed = 0; + + list_for_each_entry_safe(msg, next, &batch->msg_list, sibling_list) { + list_del_init(&msg->sibling_list); + smbdirect_connection_free_send_io(msg); + freed++; + } + batch->wr_cnt = 0; + + /* + * Each message took a local and a send credit, + * see smbdirect_connection_send_single_iter(). + */ + if (freed) { + atomic_add(freed, &sc->send_io.credits.count); + wake_up(&sc->send_io.credits.wait_queue); + atomic_add(freed, &sc->send_io.lcredits.count); + wake_up(&sc->send_io.lcredits.wait_queue); + } + + if (batch->credit) { + atomic_add(batch->credit, &sc->send_io.bcredits.count); + batch->credit = 0; + wake_up(&sc->send_io.bcredits.wait_queue); + } + + if (batch->msg_cnt) { + smbdirect_log_write(sc, SMBDIRECT_LOG_ERR, + "aborting a partial PDU after %u message(s)\n", + batch->msg_cnt); + smbdirect_socket_schedule_cleanup(sc, -ECONNABORTED); + } +} +EXPORT_SYMBOL_GPL(smbdirect_connection_send_batch_abort); + struct smbdirect_send_batch * smbdirect_init_send_batch_storage(struct smbdirect_send_batch_storage *storage, bool need_invalidate_rkey, @@ -1097,6 +1150,7 @@ static int smbdirect_connection_post_send_io(struct smbdirect_socket *sc, } list_add_tail(&msg->sibling_list, &batch->msg_list); batch->wr_cnt++; + batch->msg_cnt++; return 0; } @@ -1303,9 +1357,15 @@ int smbdirect_connection_send_wait_zero_pending(struct smbdirect_socket *sc) * that means all the I/Os have been out and we are good to return */ + /* + * Also wait if the connection is broken: the device may still + * read the pages of the caller for posted messages, e.g. earlier + * fragments of a PDU that smbdirect_connection_send_batch_abort() + * gave up. Once the teardown moved the QP to the error state, + * they complete with IB_WC_WR_FLUSH_ERR. + */ wait_event(sc->send_io.pending.zero_wait_queue, - atomic_read(&sc->send_io.pending.count) == 0 || - sc->status != SMBDIRECT_SOCKET_CONNECTED); + atomic_read(&sc->send_io.pending.count) == 0); if (sc->status != SMBDIRECT_SOCKET_CONNECTED) { smbdirect_log_write(sc, SMBDIRECT_LOG_ERR, "status=%s first_error=%1pe => %1pe\n", @@ -1382,9 +1442,20 @@ int smbdirect_connection_send_iter(struct smbdirect_socket *sc, } } - ret = smbdirect_connection_send_batch_flush(sc, &batch, true); - if (unlikely(ret && !error)) - error = ret; + if (unlikely(error)) { + /* + * Don't post a partial PDU, see + * smbdirect_connection_send_batch_abort(). + * Fragments may have been posted already, so we + * still wait for them below, they may use the + * pages of the caller. + */ + smbdirect_connection_send_batch_abort(sc, &batch); + } else { + ret = smbdirect_connection_send_batch_flush(sc, &batch, true); + if (unlikely(ret)) + error = ret; + } /* * As an optimization, we don't wait for individual I/O to finish diff --git a/fs/smb/smbdirect/socket.h b/fs/smb/smbdirect/socket.h index 14114a2567a2..dc1e906316ff 100644 --- a/fs/smb/smbdirect/socket.h +++ b/fs/smb/smbdirect/socket.h @@ -783,6 +783,13 @@ struct smbdirect_send_batch { u32 remote_key; int credit; + + /* + * Number of messages added to the batch, + * unlike wr_cnt not reset by a flush, see + * smbdirect_connection_send_batch_abort(). + */ + u32 msg_cnt; }; struct smbdirect_recv_io { diff --git a/include/linux/smbdirect.h b/include/linux/smbdirect.h index 97f5ba730fa7..74fff00cdf88 100644 --- a/include/linux/smbdirect.h +++ b/include/linux/smbdirect.h @@ -115,6 +115,9 @@ int smbdirect_connection_send_batch_flush(struct smbdirect_socket *sc, struct smbdirect_send_batch *batch, bool is_last); +void smbdirect_connection_send_batch_abort(struct smbdirect_socket *sc, + struct smbdirect_send_batch *batch); + /* * This is only temporary and only needed * as long as the client still requires -- 2.43.0 We remember all smbdirect_mr_io structures with their base_tag in a per connection xarray to find them again. This keeps track of remote invalidations done by the peer and skips the IB_WR_LOCAL_INV in such a case. Note we now ignore the 'need_invalidate' argument of smbdirect_connection_register_mr_io(), it will be removed in a follow up commit. The logic to let the caller decide and pass in 'need_invalidate' is wrong as there can be errors in the server, so that it may skip the remote invalidation. Now we explicitly track remote invalidations by looking at the IB_WC_WITH_INVALIDATE flag in smbdirect_connection_recv_io_done(). Remote invalidations we can't match are only logged at the info level: they are races with our own deregistration or keys we don't track, the MR is then invalidated locally as before, and as error messages a peer could flood the log with them. smbdirect_mr_io_disable_locked() clears need_invalidate under sc->mr_io.all.lock before it deregisters mr->mr, so a concurrent smbdirect_connection_invalidate_rkey() for a receive with IB_WC_WITH_INVALIDATE never looks at a deregistered mr->mr. Fixes: c7398583340a ("CIFS: SMBD: Implement RDMA memory registration") Cc: stable@vger.kernel.org Cc: Namjae Jeon Cc: Paulo Alcantara Cc: Tom Talpey Cc: linux-cifs@vger.kernel.org Cc: samba-technical@lists.samba.org Reviewed-by: Paulo Alcantara Signed-off-by: Stefan Metzmacher --- fs/smb/smbdirect/connection.c | 12 ++-- fs/smb/smbdirect/internal.h | 2 + fs/smb/smbdirect/mr.c | 119 ++++++++++++++++++++++++++++++++-- fs/smb/smbdirect/socket.h | 3 + include/linux/smbdirect.h | 2 +- 5 files changed, 127 insertions(+), 11 deletions(-) diff --git a/fs/smb/smbdirect/connection.c b/fs/smb/smbdirect/connection.c index 9a824f4f76c5..871750529a98 100644 --- a/fs/smb/smbdirect/connection.c +++ b/fs/smb/smbdirect/connection.c @@ -1646,10 +1646,14 @@ void smbdirect_connection_recv_io_done(struct ib_cq *cq, struct ib_wc *wc) } smbdirect_log_rdma_recv(sc, SMBDIRECT_LOG_INFO, - "recv_io=0x%p type=%d wc status=%s wc opcode %d byte_len=%d pkey_index=%u\n", - recv_io, sc->recv_io.expected, - ib_wc_status_msg(wc->status), wc->opcode, - wc->byte_len, wc->pkey_index); + "recv_io=0x%p status=%s opcode %d byte_len=%d wc_flags=0x%x inv_rkey=0x%x\n", + recv_io, ib_wc_status_msg(wc->status), wc->opcode, + wc->byte_len, wc->wc_flags, + (wc->wc_flags & IB_WC_WITH_INVALIDATE) ? + wc->ex.invalidate_rkey : 0); + + if (wc->wc_flags & IB_WC_WITH_INVALIDATE) + smbdirect_connection_invalidate_rkey(sc, wc->ex.invalidate_rkey); /* * Reset timer to the keepalive interval in diff --git a/fs/smb/smbdirect/internal.h b/fs/smb/smbdirect/internal.h index 69d0c9f899de..fec71278421a 100644 --- a/fs/smb/smbdirect/internal.h +++ b/fs/smb/smbdirect/internal.h @@ -128,6 +128,8 @@ void smbdirect_connection_recv_io_done(struct ib_cq *cq, struct ib_wc *wc); int smbdirect_connection_recv_io_refill(struct smbdirect_socket *sc); +void smbdirect_connection_invalidate_rkey(struct smbdirect_socket *sc, u32 rkey); + int smbdirect_connection_create_mr_list(struct smbdirect_socket *sc); void smbdirect_connection_destroy_mr_list(struct smbdirect_socket *sc); diff --git a/fs/smb/smbdirect/mr.c b/fs/smb/smbdirect/mr.c index 182bf64426ea..775c47c98da7 100644 --- a/fs/smb/smbdirect/mr.c +++ b/fs/smb/smbdirect/mr.c @@ -60,6 +60,25 @@ int smbdirect_connection_create_mr_list(struct smbdirect_socket *sc) mr->socket = sc; list_add_tail(&mr->list, &sc->mr_io.all.list); + + mr->base_tag = mr->mr->rkey & ~U8_MAX; + ret = xa_insert_irq(&sc->mr_io.all.tags, + mr->base_tag, + mr, + GFP_KERNEL); + if (ret) { + smbdirect_log_rdma_mr(sc, SMBDIRECT_LOG_ERR, + "xa_insert_irq() for base_tag=0x%x failed: ret=%d (%1pe)\n", + mr->base_tag, + ret, SMBDIRECT_DEBUG_ERR_PTR(ret)); + /* + * Already added to sc->mr_io.all.list, + * so below we can just call + * smbdirect_connection_destroy_mr_list() + */ + goto xa_insert_failed; + } + atomic_inc(&sc->mr_io.ready.count); } @@ -71,6 +90,7 @@ int smbdirect_connection_create_mr_list(struct smbdirect_socket *sc) mutex_destroy(&mr->mutex); kfree(mr); kzalloc_mr_failed: +xa_insert_failed: smbdirect_connection_destroy_mr_list(sc); return ret; } @@ -78,12 +98,23 @@ int smbdirect_connection_create_mr_list(struct smbdirect_socket *sc) static void smbdirect_mr_io_disable_locked(struct smbdirect_mr_io *mr) { struct smbdirect_socket *sc = mr->socket; + unsigned long flags; lockdep_assert_held(&mr->mutex); if (mr->state == SMBDIRECT_MR_DISABLED) return; + /* + * smbdirect_connection_invalidate_rkey() only looks at mr->mr + * while need_invalidate is set, so clear it before mr->mr + * is deregistered. + */ + spin_lock_irqsave(&sc->mr_io.all.lock, flags); + mr->state = SMBDIRECT_MR_DISABLED; + mr->need_invalidate = false; + spin_unlock_irqrestore(&sc->mr_io.all.lock, flags); + if (mr->mr) ib_dereg_mr(mr->mr); if (mr->sgt.nents) @@ -93,8 +124,6 @@ static void smbdirect_mr_io_disable_locked(struct smbdirect_mr_io *mr) mr->mr = NULL; mr->sgt.sgl = NULL; mr->sgt.nents = 0; - - mr->state = SMBDIRECT_MR_DISABLED; } static void smbdirect_mr_io_free_locked(struct kref *kref) @@ -121,6 +150,11 @@ void smbdirect_connection_destroy_mr_list(struct smbdirect_socket *sc) LIST_HEAD(all_list); unsigned long flags; + /* + * This means we don't need explicit + * xa_erase() calls below. + */ + xa_destroy(&sc->mr_io.all.tags); spin_lock_irqsave(&sc->mr_io.all.lock, flags); list_splice_tail_init(&sc->mr_io.all.list, &all_list); spin_unlock_irqrestore(&sc->mr_io.all.lock, flags); @@ -274,12 +308,13 @@ struct smbdirect_mr_io * smbdirect_connection_register_mr_io(struct smbdirect_socket *sc, struct iov_iter *iter, bool writing, - bool need_invalidate) + bool _ignored_need_invalidate) { const struct smbdirect_socket_parameters *sp = &sc->parameters; struct smbdirect_mr_io *mr; int ret, num_pages, num_mapped; struct ib_reg_wr *reg_wr; + unsigned long flags; num_pages = iov_iter_npages(iter, sp->max_frmr_depth + 1); if (num_pages > sp->max_frmr_depth) { @@ -300,7 +335,6 @@ smbdirect_connection_register_mr_io(struct smbdirect_socket *sc, mutex_lock(&mr->mutex); mr->dir = writing ? DMA_FROM_DEVICE : DMA_TO_DEVICE; - mr->need_invalidate = need_invalidate; mr->sgt.nents = 0; mr->sgt.orig_nents = 0; @@ -360,6 +394,8 @@ smbdirect_connection_register_mr_io(struct smbdirect_socket *sc, } ib_update_fast_reg_key(mr->mr, ib_inc_rkey(mr->mr->rkey)); + if (mr->mr->rkey == mr->base_tag) + ib_update_fast_reg_key(mr->mr, ib_inc_rkey(mr->mr->rkey)); reg_wr = &mr->wr; reg_wr->wr.opcode = IB_WR_REG_MR; mr->cqe.done = smbdirect_connection_mr_io_register_done; @@ -372,6 +408,10 @@ smbdirect_connection_register_mr_io(struct smbdirect_socket *sc, IB_ACCESS_REMOTE_WRITE | IB_ACCESS_LOCAL_WRITE : IB_ACCESS_REMOTE_READ; + spin_lock_irqsave(&sc->mr_io.all.lock, flags); + mr->need_invalidate = true; + spin_unlock_irqrestore(&sc->mr_io.all.lock, flags); + /* * There is no need for waiting for complemtion on ib_post_send * on IB_WR_REG_MR. Hardware enforces a barrier and order of execution @@ -389,6 +429,10 @@ smbdirect_connection_register_mr_io(struct smbdirect_socket *sc, return mr; } + spin_lock_irqsave(&sc->mr_io.all.lock, flags); + mr->need_invalidate = false; + spin_unlock_irqrestore(&sc->mr_io.all.lock, flags); + smbdirect_log_rdma_mr(sc, SMBDIRECT_LOG_ERR, "ib_post_send failed ret=%d (%1pe) reg_wr->key=0x%x\n", ret, SMBDIRECT_DEBUG_ERR_PTR(ret), reg_wr->key); @@ -452,6 +496,8 @@ EXPORT_SYMBOL_GPL(smbdirect_mr_io_fill_buffer_descriptor); void smbdirect_connection_deregister_mr_io(struct smbdirect_mr_io *mr) { struct smbdirect_socket *sc = mr->socket; + bool need_invalidate; + unsigned long flags; int ret = 0; lock_again: @@ -464,7 +510,12 @@ void smbdirect_connection_deregister_mr_io(struct smbdirect_mr_io *mr) goto put_kref; } - if (mr->need_invalidate) { + spin_lock_irqsave(&sc->mr_io.all.lock, flags); + need_invalidate = mr->need_invalidate; + mr->need_invalidate = false; + spin_unlock_irqrestore(&sc->mr_io.all.lock, flags); + + if (need_invalidate) { struct ib_send_wr *wr = &mr->inv_wr; /* Need to finish local invalidation before returning */ @@ -492,7 +543,6 @@ void smbdirect_connection_deregister_mr_io(struct smbdirect_mr_io *mr) */ mutex_unlock(&mr->mutex); wait_for_completion(&mr->invalidate_done); - mr->need_invalidate = false; goto lock_again; } else /* @@ -535,3 +585,60 @@ void smbdirect_connection_deregister_mr_io(struct smbdirect_mr_io *mr) mutex_unlock(&mr->mutex); } EXPORT_SYMBOL_GPL(smbdirect_connection_deregister_mr_io); + +void smbdirect_connection_invalidate_rkey(struct smbdirect_socket *sc, u32 rkey) +{ + u32 base_tag = rkey & ~U8_MAX; + struct smbdirect_mr_io *mr; + unsigned long flags; + + /* + * This might be called from an interrupt context, + * so we can't call mutex_lock(&mr->mutex); here. + * + * We run under the sc->mr_io.all.lock spin_lock + */ + spin_lock_irqsave(&sc->mr_io.all.lock, flags); + + mr = xa_load(&sc->mr_io.all.tags, base_tag); + if (!mr) { + smbdirect_log_rdma_mr(sc, SMBDIRECT_LOG_INFO, + "xa_load() for base_tag=0x%x rkey=0x%x failed\n", + base_tag, rkey); + goto done; + } + + if (!mr->need_invalidate) { + smbdirect_log_rdma_mr(sc, SMBDIRECT_LOG_INFO, + "invalidate rkey=0x%x for base_tag=0x%x not registered\n", + rkey, base_tag); + goto done; + } + + if (WARN_ON_ONCE(mr->base_tag != base_tag)) + goto done; /* internal error */ + + if (WARN_ON_ONCE(!mr->mr)) + goto done; /* internal error */ + + if (mr->mr->rkey != rkey) { + /* + * Maybe a local invalidate was faster + * than the remote invalidate + */ + smbdirect_log_rdma_mr(sc, SMBDIRECT_LOG_INFO, + "invalidate rkey=0x%x for base_tag=0x%x stale rkey=0x%x\n", + rkey, base_tag, mr->mr->rkey); + goto done; + } + + /* + * We can skip IB_WR_LOCAL_INV in + * smbdirect_connection_deregister_mr_io() + */ + mr->state = SMBDIRECT_MR_INVALIDATED; + mr->need_invalidate = false; + +done: + spin_unlock_irqrestore(&sc->mr_io.all.lock, flags); +} diff --git a/fs/smb/smbdirect/socket.h b/fs/smb/smbdirect/socket.h index dc1e906316ff..4dcbd9c86aaf 100644 --- a/fs/smb/smbdirect/socket.h +++ b/fs/smb/smbdirect/socket.h @@ -411,6 +411,7 @@ struct smbdirect_socket { */ struct { struct list_head list; + struct xarray tags; spinlock_t lock; } all; @@ -671,6 +672,7 @@ static __always_inline void smbdirect_socket_init(struct smbdirect_socket *sc) spin_lock_init(&sc->mr_io.all.lock); INIT_LIST_HEAD(&sc->mr_io.all.list); + xa_init_flags(&sc->mr_io.all.tags, XA_FLAGS_LOCK_IRQ); atomic_set(&sc->mr_io.ready.count, 0); init_waitqueue_head(&sc->mr_io.ready.wait_queue); atomic_set(&sc->mr_io.used.count, 0); @@ -837,6 +839,7 @@ struct smbdirect_mr_io { struct list_head list; enum smbdirect_mr_state state; + u32 base_tag; struct ib_mr *mr; struct sg_table sgt; enum dma_data_direction dir; diff --git a/include/linux/smbdirect.h b/include/linux/smbdirect.h index 74fff00cdf88..f797cba849cd 100644 --- a/include/linux/smbdirect.h +++ b/include/linux/smbdirect.h @@ -175,7 +175,7 @@ struct smbdirect_mr_io * smbdirect_connection_register_mr_io(struct smbdirect_socket *sc, struct iov_iter *iter, bool writing, - bool need_invalidate); + bool _ignored_need_invalidate); void smbdirect_mr_io_fill_buffer_descriptor(struct smbdirect_mr_io *mr, struct smbdirect_buffer_descriptor_v1 *v1); -- 2.43.0 smbdirect_connection_register_mr_io() posts a signaled IB_WR_REG_MR with mr->cqe.done = smbdirect_connection_mr_io_register_done, and smbdirect_connection_deregister_mr_io() reused the same mr->cqe for IB_WR_LOCAL_INV, overwriting cqe.done with smbdirect_connection_mr_io_local_inv_done. If the deregistration happens before the REG_MR completion was polled, that completion already ran local_inv_done(): it set the mr to SMBDIRECT_MR_INVALIDATED and completed invalidate_done too early, so the mr went back to SMBDIRECT_MR_READY (and ready.count++) while the LOCAL_INV was still outstanding. Its real completion later set the already reused mr to SMBDIRECT_MR_INVALIDATED again, leaving ready.count counting mrs that are no longer READY, which can make smbdirect_connection_get_mr_io() spin forever. Rename mr->cqe to mr->reg_cqe and add a separate mr->inv_cqe for the LOCAL_INV. Fixes: c7398583340a ("CIFS: SMBD: Implement RDMA memory registration") Cc: stable@vger.kernel.org Cc: Namjae Jeon Cc: Paulo Alcantara Cc: Tom Talpey Cc: Ronnie Sahlberg Cc: linux-cifs@vger.kernel.org Cc: samba-technical@lists.samba.org Reviewed-by: Paulo Alcantara Signed-off-by: Stefan Metzmacher --- fs/smb/smbdirect/mr.c | 12 ++++++------ fs/smb/smbdirect/socket.h | 9 ++++++++- 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/fs/smb/smbdirect/mr.c b/fs/smb/smbdirect/mr.c index 775c47c98da7..6c37206474bb 100644 --- a/fs/smb/smbdirect/mr.c +++ b/fs/smb/smbdirect/mr.c @@ -242,7 +242,7 @@ smbdirect_connection_get_mr_io(struct smbdirect_socket *sc) static void smbdirect_connection_mr_io_register_done(struct ib_cq *cq, struct ib_wc *wc) { struct smbdirect_mr_io *mr = - container_of(wc->wr_cqe, struct smbdirect_mr_io, cqe); + container_of(wc->wr_cqe, struct smbdirect_mr_io, reg_cqe); struct smbdirect_socket *sc = mr->socket; if (wc->status != IB_WC_SUCCESS) { @@ -256,7 +256,7 @@ static void smbdirect_connection_mr_io_register_done(struct ib_cq *cq, struct ib static void smbdirect_connection_mr_io_local_inv_done(struct ib_cq *cq, struct ib_wc *wc) { struct smbdirect_mr_io *mr = - container_of(wc->wr_cqe, struct smbdirect_mr_io, cqe); + container_of(wc->wr_cqe, struct smbdirect_mr_io, inv_cqe); struct smbdirect_socket *sc = mr->socket; mr->state = SMBDIRECT_MR_INVALIDATED; @@ -398,8 +398,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; @@ -520,8 +520,8 @@ void smbdirect_connection_deregister_mr_io(struct smbdirect_mr_io *mr) /* Need to finish local invalidation before returning */ wr->opcode = IB_WR_LOCAL_INV; - mr->cqe.done = smbdirect_connection_mr_io_local_inv_done; - wr->wr_cqe = &mr->cqe; + mr->inv_cqe.done = smbdirect_connection_mr_io_local_inv_done; + wr->wr_cqe = &mr->inv_cqe; wr->num_sge = 0; wr->ex.invalidate_rkey = mr->mr->rkey; wr->send_flags = IB_SEND_SIGNALED; diff --git a/fs/smb/smbdirect/socket.h b/fs/smb/smbdirect/socket.h index 4dcbd9c86aaf..1fc4aebcdda5 100644 --- a/fs/smb/smbdirect/socket.h +++ b/fs/smb/smbdirect/socket.h @@ -826,7 +826,14 @@ enum smbdirect_mr_state { struct smbdirect_mr_io { struct smbdirect_socket *socket; - struct ib_cqe cqe; + /* + * Separate completion entries for IB_WR_REG_MR and IB_WR_LOCAL_INV: + * smbdirect_connection_deregister_mr_io() can post the LOCAL_INV + * before the REG_MR completion was polled, and with a shared entry + * that completion would then already run the LOCAL_INV handler. + */ + struct ib_cqe reg_cqe; + struct ib_cqe inv_cqe; /* * We can have up to two references: -- 2.43.0 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_refill_work() and smbdirect_connection_init_immediate_work(), called before anything can queue them, as INIT_WORK() on a work item that may be queued concurrently corrupts it: smbdirect_connection_recv_io_done() may queue both for the first data transfer message, on the accept side before the send completion of the negotiate response calls smbdirect_connection_negotiation_done(), and on the accept side the idle timer may queue immediate_work as soon as smbdirect_socket_accept() changed the status to SMBDIRECT_SOCKET_CONNECTED. So refill_work is activated right before the receives of the data transfer phase are posted (smbdirect_connection_put_recv_io() also queues it, so not earlier), and immediate_work on the accept side before smbdirect_accept_negotiate_recv_work() arms the idle timer. Fixes: d1f187656797 ("smb: smbdirect: introduce smbdirect_connect[_sync]()") Fixes: eb3ed1e9048c ("smb: smbdirect: introduce smbdirect_accept_connect_request()") Fixes: 099db5093e4d ("smb: smbdirect: introduce smbdirect_connection_negotiation_done()") Cc: stable@vger.kernel.org Cc: Namjae Jeon Cc: Paulo Alcantara Cc: Tom Talpey Cc: linux-cifs@vger.kernel.org Cc: samba-technical@lists.samba.org Reported-by: syzbot+a989758ffc6f93223eb6@syzkaller.appspotmail.com Closes: https://syzkaller.appspot.com/bug?extid=a989758ffc6f93223eb6 Reviewed-by: Paulo Alcantara Signed-off-by: Stefan Metzmacher --- fs/smb/smbdirect/accept.c | 37 +++++++++++--- fs/smb/smbdirect/connect.c | 28 ++++++++--- fs/smb/smbdirect/connection.c | 54 +++++++++++++++----- fs/smb/smbdirect/internal.h | 4 ++ fs/smb/smbdirect/listen.c | 19 ++++++- fs/smb/smbdirect/socket.c | 93 +++++++++++++++++++++++------------ fs/smb/smbdirect/socket.h | 14 +++++- 7 files changed, 188 insertions(+), 61 deletions(-) diff --git a/fs/smb/smbdirect/accept.c b/fs/smb/smbdirect/accept.c index 9fb0676a3035..5a33fcde3653 100644 --- a/fs/smb/smbdirect/accept.c +++ b/fs/smb/smbdirect/accept.c @@ -24,6 +24,7 @@ int smbdirect_accept_connect_request(struct smbdirect_socket *sc, u8 peer_responder_resources; struct rdma_conn_param conn_param; __be32 ird_ord_hdr[2]; + unsigned long flags; int ret; /* @@ -152,11 +153,17 @@ int smbdirect_accept_connect_request(struct smbdirect_socket *sc, /* * start with the negotiate timeout and SMBDIRECT_KEEPALIVE_PENDING * so that the timer will cause a disconnect. + * + * Only if the teardown didn't start, see cleanup_lock. */ - INIT_DELAYED_WORK(&sc->idle.timer_work, smbdirect_connection_idle_timer_work); - sc->idle.keepalive = SMBDIRECT_KEEPALIVE_PENDING; - mod_delayed_work(sc->workqueues.idle, &sc->idle.timer_work, - msecs_to_jiffies(sp->negotiate_timeout_msec)); + spin_lock_irqsave(&sc->cleanup_lock, flags); + if (!sc->first_error) { + INIT_DELAYED_WORK(&sc->idle.timer_work, smbdirect_connection_idle_timer_work); + sc->idle.keepalive = SMBDIRECT_KEEPALIVE_PENDING; + mod_delayed_work(sc->workqueues.idle, &sc->idle.timer_work, + msecs_to_jiffies(sp->negotiate_timeout_msec)); + } + spin_unlock_irqrestore(&sc->cleanup_lock, flags); return 0; @@ -287,13 +294,13 @@ static void smbdirect_accept_negotiate_recv_done(struct ib_cq *cq, struct ib_wc * smbdirect_accept_rdma_event_handler() will do it, when * RDMA_CM_EVENT_ESTABLISHED arrived. */ - spin_lock_irqsave(&sc->connect.lock, flags); + spin_lock_irqsave(&sc->cleanup_lock, flags); if (!sc->first_error) { INIT_WORK(&sc->connect.work, smbdirect_accept_negotiate_recv_work); if (sc->status == SMBDIRECT_SOCKET_NEGOTIATE_NEEDED) queue_work(sc->workqueues.accept, &sc->connect.work); } - spin_unlock_irqrestore(&sc->connect.lock, flags); + spin_unlock_irqrestore(&sc->cleanup_lock, flags); return; @@ -332,6 +339,13 @@ static void smbdirect_accept_negotiate_recv_work(struct work_struct *work) */ disable_work(work); + /* + * The idle timer may queue immediate_work as soon as + * smbdirect_socket_accept() changed the status to + * SMBDIRECT_SOCKET_CONNECTED. + */ + smbdirect_connection_init_immediate_work(sc); + /* * Reset timer to the keepalive interval in * order to trigger our next keepalive message. @@ -551,6 +565,13 @@ void smbdirect_accept_negotiate_finish(struct smbdirect_socket *sc, u32 ntstatus recv_io->cqe.done = smbdirect_connection_recv_io_done; recv_io = NULL; + /* + * The receives posted below may queue refill_work, + * immediate_work was already activated in + * smbdirect_accept_negotiate_recv_work(). + */ + smbdirect_connection_init_refill_work(sc); + /* * We should at least post 1 smbdirect_recv_io! */ @@ -796,10 +817,10 @@ static int smbdirect_accept_rdma_event_handler(struct rdma_cm_id *id, if (SMBDIRECT_CHECK_STATUS_DISCONNECT(sc, SMBDIRECT_SOCKET_RDMA_CONNECT_RUNNING)) return 0; sc->status = SMBDIRECT_SOCKET_NEGOTIATE_NEEDED; - spin_lock_irqsave(&sc->connect.lock, flags); + spin_lock_irqsave(&sc->cleanup_lock, flags); if (!sc->first_error) queue_work(sc->workqueues.accept, &sc->connect.work); - spin_unlock_irqrestore(&sc->connect.lock, flags); + spin_unlock_irqrestore(&sc->cleanup_lock, flags); /* * wait for smbdirect_accept_negotiate_recv_done() diff --git a/fs/smb/smbdirect/connect.c b/fs/smb/smbdirect/connect.c index ce00e6466690..fc834f8960fa 100644 --- a/fs/smb/smbdirect/connect.c +++ b/fs/smb/smbdirect/connect.c @@ -130,6 +130,7 @@ static int smbdirect_connect_rdma_connect(struct smbdirect_socket *sc) struct smbdirect_socket_parameters *sp = &sc->parameters; struct rdma_conn_param conn_param; __be32 ird_ord_hdr[2]; + unsigned long flags; int ret; sc->ib.dev = sc->rdma.cm_id->device; @@ -231,11 +232,17 @@ static int smbdirect_connect_rdma_connect(struct smbdirect_socket *sc) /* * start with the rdma connect timeout and SMBDIRECT_KEEPALIVE_PENDING * so that the timer will cause a disconnect. + * + * Only if the teardown didn't start, see cleanup_lock. */ - INIT_DELAYED_WORK(&sc->idle.timer_work, smbdirect_connection_idle_timer_work); - sc->idle.keepalive = SMBDIRECT_KEEPALIVE_PENDING; - mod_delayed_work(sc->workqueues.idle, &sc->idle.timer_work, - msecs_to_jiffies(sp->rdma_connect_timeout_msec)); + spin_lock_irqsave(&sc->cleanup_lock, flags); + if (!sc->first_error) { + INIT_DELAYED_WORK(&sc->idle.timer_work, smbdirect_connection_idle_timer_work); + sc->idle.keepalive = SMBDIRECT_KEEPALIVE_PENDING; + mod_delayed_work(sc->workqueues.idle, &sc->idle.timer_work, + msecs_to_jiffies(sp->rdma_connect_timeout_msec)); + } + spin_unlock_irqrestore(&sc->cleanup_lock, flags); return 0; } @@ -629,13 +636,13 @@ static void smbdirect_connect_negotiate_recv_done(struct ib_cq *cq, struct ib_wc * So we defer further processing of the negotiation * to smbdirect_connect_negotiate_recv_work(). */ - spin_lock_irqsave(&sc->connect.lock, flags); + spin_lock_irqsave(&sc->cleanup_lock, flags); if (!sc->first_error) { INIT_WORK(&sc->connect.work, smbdirect_connect_negotiate_recv_work); if (sc->status == SMBDIRECT_SOCKET_NEGOTIATE_RUNNING) queue_work(sc->workqueues.connect, &sc->connect.work); } - spin_unlock_irqrestore(&sc->connect.lock, flags); + spin_unlock_irqrestore(&sc->cleanup_lock, flags); return; @@ -876,6 +883,15 @@ static void smbdirect_connect_negotiate_recv_work(struct work_struct *work) recv_io->cqe.done = smbdirect_connection_recv_io_done; recv_io = NULL; + /* + * The receives posted below may queue both work items. The + * status only becomes SMBDIRECT_SOCKET_CONNECTED afterwards in + * smbdirect_connection_negotiation_done(), so the idle timer + * can't queue immediate_work before. + */ + smbdirect_connection_init_immediate_work(sc); + smbdirect_connection_init_refill_work(sc); + /* * We should at least post 1 smbdirect_recv_io! */ diff --git a/fs/smb/smbdirect/connection.c b/fs/smb/smbdirect/connection.c index 871750529a98..0e5bf32d70a6 100644 --- a/fs/smb/smbdirect/connection.c +++ b/fs/smb/smbdirect/connection.c @@ -190,14 +190,7 @@ void smbdirect_connection_negotiation_done(struct smbdirect_socket *sc) SMBDIRECT_DEBUG_ERR_PTR(sc->first_error)); sc->status = SMBDIRECT_SOCKET_CONNECTED; - /* - * We need to setup the refill and send immediate work - * in order to get a working connection. - */ done: - INIT_WORK(&sc->recv_io.posted.refill_work, smbdirect_connection_recv_io_refill_work); - INIT_WORK(&sc->idle.immediate_work, smbdirect_connection_send_immediate_work); - smbdirect_log_rdma_event(sc, SMBDIRECT_LOG_INFO, "negotiated: local: %pISpsfc remote: %pISpsfc\n", &sc->rdma.cm_id->route.addr.src_addr, @@ -206,6 +199,46 @@ void smbdirect_connection_negotiation_done(struct smbdirect_socket *sc) wake_up(&sc->status_wait); } +/* + * INIT_WORK() on a work item that may be queued concurrently corrupts + * it, so the work items of the data transfer phase are activated + * before anything can queue them, and only if the teardown didn't + * start, see cleanup_lock. + * + * immediate_work is queued by smbdirect_connection_recv_io_done() and + * by smbdirect_connection_idle_timer_work() once the status is + * SMBDIRECT_SOCKET_CONNECTED. On the accept side the status changes + * in smbdirect_socket_accept() while the idle timer may already be + * armed, so it's activated before the timer is armed there. + */ +void smbdirect_connection_init_immediate_work(struct smbdirect_socket *sc) +{ + unsigned long flags; + + spin_lock_irqsave(&sc->cleanup_lock, flags); + if (!sc->first_error) + INIT_WORK(&sc->idle.immediate_work, + smbdirect_connection_send_immediate_work); + spin_unlock_irqrestore(&sc->cleanup_lock, flags); +} + +/* + * refill_work is queued by smbdirect_connection_put_recv_io() and + * smbdirect_connection_recv_io_done(), so it's activated right before + * the receives of the data transfer phase are posted, not earlier, + * as it would then post receives for the negotiation phase. + */ +void smbdirect_connection_init_refill_work(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); + 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 +1819,11 @@ void smbdirect_connection_recv_io_done(struct ib_cq *cq, struct ib_wc *wc) error: /* - * Make sure smbdirect_connection_put_recv_io() does not - * start recv_io.posted.refill_work. + * This disables recv_io.posted.refill_work under cleanup_lock, + * so smbdirect_connection_put_recv_io() does not start it. */ - disable_work(&sc->recv_io.posted.refill_work); - smbdirect_connection_put_recv_io(recv_io); smbdirect_socket_schedule_cleanup(sc, -ECONNABORTED); + smbdirect_connection_put_recv_io(recv_io); } int smbdirect_connection_recv_io_refill(struct smbdirect_socket *sc) diff --git a/fs/smb/smbdirect/internal.h b/fs/smb/smbdirect/internal.h index fec71278421a..a1e6a6ec64ec 100644 --- a/fs/smb/smbdirect/internal.h +++ b/fs/smb/smbdirect/internal.h @@ -87,6 +87,10 @@ void smbdirect_connection_rdma_established(struct smbdirect_socket *sc); void smbdirect_connection_negotiation_done(struct smbdirect_socket *sc); +void smbdirect_connection_init_immediate_work(struct smbdirect_socket *sc); + +void smbdirect_connection_init_refill_work(struct smbdirect_socket *sc); + int smbdirect_connection_create_qp(struct smbdirect_socket *sc); void smbdirect_connection_destroy_qp(struct smbdirect_socket *sc); diff --git a/fs/smb/smbdirect/listen.c b/fs/smb/smbdirect/listen.c index 3c91f00d95ce..2c4a2f249f45 100644 --- a/fs/smb/smbdirect/listen.c +++ b/fs/smb/smbdirect/listen.c @@ -124,8 +124,23 @@ int smbdirect_socket_listen(struct smbdirect_socket *sc, int backlog) ret = rdma_listen(sc->rdma.cm_id, backlog); if (ret) { - sc->first_error = ret; - sc->status = SMBDIRECT_SOCKET_DISCONNECTED; + /* + * This sets first_error under cleanup_lock, + * changes the status to SMBDIRECT_SOCKET_DISCONNECTED + * and queues disconnect_work. + * + * It's safe to use sc->rdma.cm_id and + * sc->rdma.cm_id->device afterwards: neither this + * nor smbdirect_socket_cleanup_work() touch the cm_id + * of a socket that never reached + * RDMA_CM_EVENT_ESTABLISHED. Only + * smbdirect_socket_destroy() calls rdma_destroy_id(), + * via smbdirect_socket_release() of our caller, + * which can't run while we are still here. + * listen.backlog is still -1, so there are no + * pending or ready child sockets to clean up either. + */ + smbdirect_socket_schedule_cleanup_lvl(sc, SMBDIRECT_LOG_INFO, ret); if (sc->rdma.cm_id->device) smbdirect_log_rdma_event(sc, SMBDIRECT_LOG_INFO, "listening failed %1pe on addr: %pISpsfc dev: %.*s\n", diff --git a/fs/smb/smbdirect/socket.c b/fs/smb/smbdirect/socket.c index 5d995694a74e..3e6a87cc37a7 100644 --- a/fs/smb/smbdirect/socket.c +++ b/fs/smb/smbdirect/socket.c @@ -271,7 +271,34 @@ void __smbdirect_socket_schedule_cleanup(struct smbdirect_socket *sc, unsigned long flags; bool was_first = false; + /* + * first_error is set under cleanup_lock, together with + * disabling the work items, see cleanup_lock: an activation + * either happened before or sees first_error, so the + * work items only need to be disabled once. + */ + spin_lock_irqsave(&sc->cleanup_lock, flags); if (!sc->first_error) { + if (error) + sc->first_error = error; + else + sc->first_error = -ECONNABORTED; + was_first = true; + + /* + * make sure other work (than disconnect_work) + * is not queued again but here we don't block and avoid + * disable[_delayed]_work_sync() + */ + disable_work(&sc->connect.work); + disable_work(&sc->recv_io.posted.refill_work); + disable_work(&sc->idle.immediate_work); + sc->idle.keepalive = SMBDIRECT_KEEPALIVE_NONE; + disable_delayed_work(&sc->idle.timer_work); + } + spin_unlock_irqrestore(&sc->cleanup_lock, flags); + + if (was_first) ___smbdirect_log_generic(sc, func, line, lvl, SMBDIRECT_LOG_RDMA_EVENT, @@ -282,23 +309,6 @@ void __smbdirect_socket_schedule_cleanup(struct smbdirect_socket *sc, force_status ? smbdirect_socket_status_string(*force_status) : "", func, line, smbdirect_socket_status_string(sc->status)); - if (error) - sc->first_error = error; - else - sc->first_error = -ECONNABORTED; - was_first = true; - } - - /* - * make sure other work (than disconnect_work) - * is not queued again but here we don't block and avoid - * disable[_delayed]_work_sync() - */ - disable_work(&sc->connect.work); - disable_work(&sc->recv_io.posted.refill_work); - disable_work(&sc->idle.immediate_work); - sc->idle.keepalive = SMBDIRECT_KEEPALIVE_NONE; - disable_delayed_work(&sc->idle.timer_work); /* * In case we were a listener we need to @@ -396,6 +406,7 @@ static void smbdirect_socket_cleanup_work(struct work_struct *work) struct smbdirect_socket *sc = container_of(work, struct smbdirect_socket, disconnect_work); struct smbdirect_socket *psc, *tsc; + bool was_first = false; unsigned long flags; /* @@ -403,25 +414,35 @@ static void smbdirect_socket_cleanup_work(struct work_struct *work) */ WARN_ON_ONCE(in_interrupt()); - if (!sc->first_error) { - smbdirect_log_rdma_event(sc, SMBDIRECT_LOG_ERR, - "%s called with first_error==0\n", - smbdirect_socket_status_string(sc->status)); - - sc->first_error = -ECONNABORTED; - } - /* - * make sure this and other work is not queued again + * make sure this work is not queued again * but here we don't block and avoid - * disable[_delayed]_work_sync() + * disable_work_sync() */ disable_work(&sc->disconnect_work); - disable_work(&sc->connect.work); - disable_work(&sc->recv_io.posted.refill_work); - disable_work(&sc->idle.immediate_work); - sc->idle.keepalive = SMBDIRECT_KEEPALIVE_NONE; - disable_delayed_work(&sc->idle.timer_work); + + /* + * Normally __smbdirect_socket_schedule_cleanup() already + * set first_error and disabled the other work items, + * otherwise do it here, see cleanup_lock. + */ + spin_lock_irqsave(&sc->cleanup_lock, flags); + if (!sc->first_error) { + sc->first_error = -ECONNABORTED; + was_first = true; + + disable_work(&sc->connect.work); + disable_work(&sc->recv_io.posted.refill_work); + disable_work(&sc->idle.immediate_work); + sc->idle.keepalive = SMBDIRECT_KEEPALIVE_NONE; + disable_delayed_work(&sc->idle.timer_work); + } + spin_unlock_irqrestore(&sc->cleanup_lock, flags); + + if (was_first) + smbdirect_log_rdma_event(sc, SMBDIRECT_LOG_ERR, + "%s called with first_error==0\n", + smbdirect_socket_status_string(sc->status)); /* * In case we were a listener we need to @@ -569,6 +590,14 @@ static void smbdirect_socket_destroy(struct smbdirect_socket *sc) */ smbdirect_socket_wake_up_all(sc); + /* + * These don't need cleanup_lock: smbdirect_socket_destroy_sync() + * makes sure first_error is set before we get here, so no work + * item can be activated anymore, see cleanup_lock. The work items + * are already disabled, either by the cleanup that set first_error + * or because they were never activated, here we only wait for an + * instance that is still running. + */ disable_work_sync(&sc->disconnect_work); disable_work_sync(&sc->connect.work); disable_work_sync(&sc->recv_io.posted.refill_work); diff --git a/fs/smb/smbdirect/socket.h b/fs/smb/smbdirect/socket.h index 1fc4aebcdda5..1520b6e48e7d 100644 --- a/fs/smb/smbdirect/socket.h +++ b/fs/smb/smbdirect/socket.h @@ -108,6 +108,17 @@ struct smbdirect_socket { wait_queue_head_t status_wait; int first_error; + /* + * The cleanup sets first_error and disables the work items + * under cleanup_lock and the work items are only activated with + * INIT_[DELAYED_]WORK() under cleanup_lock while first_error is + * still 0. INIT_[DELAYED_]WORK() resets the disable count, + * so without the lock an activation could undo a disable of + * a concurrent teardown. All other writers of first_error + * also hold cleanup_lock. + */ + spinlock_t cleanup_lock; + /* * This points to the workqueues to * be used for this socket. @@ -194,7 +205,6 @@ struct smbdirect_socket { * The state for connect/negotiation */ struct { - spinlock_t lock; struct work_struct work; } connect; @@ -598,6 +608,7 @@ static __always_inline void smbdirect_socket_init(struct smbdirect_socket *sc) memset(sc, 0, sizeof(*sc)); init_waitqueue_head(&sc->status_wait); + spin_lock_init(&sc->cleanup_lock); sc->workqueues.accept = smbdirect_globals.workqueues.accept; sc->workqueues.connect = smbdirect_globals.workqueues.connect; @@ -616,7 +627,6 @@ static __always_inline void smbdirect_socket_init(struct smbdirect_socket *sc) sc->ib.poll_ctx = IB_POLL_UNBOUND_WORKQUEUE; - spin_lock_init(&sc->connect.lock); INIT_WORK(&sc->connect.work, __smbdirect_socket_disabled_work); disable_work_sync(&sc->connect.work); -- 2.43.0 syzbot reported this warning: expected[RESOLVE_ADDR_RUNNING] != DISCONNECTED first_error=-ERESTARTSYS local=127.0.0.1:51036 remote=127.0.0.1:445 WARNING: fs/smb/smbdirect/connect.c:307 at smbdirect_connect_rdma_event_handler+0x1e02/0x2820 fs/smb/smbdirect/connect.c:307, CPU#1: kworker/u8:7/198 Workqueue: rdma_cm cma_work_handler Call Trace: cma_cm_event_handler+0xac/0x2c0 drivers/infiniband/core/cma.c:2181 cma_work_handler+0x265/0x520 drivers/infiniband/core/cma.c:3040 The waiter in smbdirect_connection_wait_for_connected() was interrupted by a signal, so it called smbdirect_socket_schedule_cleanup(), which sets first_error and changes the status, without the handler lock of the rdma cm id, while smbdirect_connect_rdma_event_handler() was running. SMBDIRECT_CHECK_STATUS_{WARN,DISCONNECT}() check first_error before the status, but there was nothing that made sure the first_error written before the status is also seen before it, the compiler could even reuse the value of the first_error check at the start of the event handler. So the handler saw first_error == 0 and the status changed by the teardown, which is not unexpected. Now __smbdirect_socket_schedule_cleanup() and, if it sets first_error itself, smbdirect_socket_cleanup_work() write first_error with WRITE_ONCE() and order it before the status changes with smp_wmb() and __SMBDIRECT_CHECK_STATUS_FAILED() reads both with READ_ONCE() and checks first_error again, after smp_rmb(), if the status is not the expected one, so such a status change is handled like an error without a warning. Fixes: 1adb2dab9727 ("smb: smbdirect: introduce SMBDIRECT_CHECK_STATUS_{WARN,DISCONNECT}()") Fixes: d1f187656797 ("smb: smbdirect: introduce smbdirect_connect[_sync]()") Cc: stable@vger.kernel.org Cc: Namjae Jeon Cc: Paulo Alcantara Cc: Tom Talpey Cc: linux-cifs@vger.kernel.org Cc: samba-technical@lists.samba.org Reported-by: syzbot+a989758ffc6f93223eb6@syzkaller.appspotmail.com Closes: https://syzkaller.appspot.com/bug?extid=a989758ffc6f93223eb6 Reviewed-by: Paulo Alcantara Signed-off-by: Stefan Metzmacher --- fs/smb/smbdirect/socket.c | 21 ++++++++++++++++++--- fs/smb/smbdirect/socket.h | 18 +++++++++++++++--- 2 files changed, 33 insertions(+), 6 deletions(-) diff --git a/fs/smb/smbdirect/socket.c b/fs/smb/smbdirect/socket.c index 3e6a87cc37a7..384cf152d40f 100644 --- a/fs/smb/smbdirect/socket.c +++ b/fs/smb/smbdirect/socket.c @@ -280,9 +280,9 @@ void __smbdirect_socket_schedule_cleanup(struct smbdirect_socket *sc, spin_lock_irqsave(&sc->cleanup_lock, flags); if (!sc->first_error) { if (error) - sc->first_error = error; + WRITE_ONCE(sc->first_error, error); else - sc->first_error = -ECONNABORTED; + WRITE_ONCE(sc->first_error, -ECONNABORTED); was_first = true; /* @@ -298,6 +298,14 @@ void __smbdirect_socket_schedule_cleanup(struct smbdirect_socket *sc, } spin_unlock_irqrestore(&sc->cleanup_lock, flags); + /* + * Callers of SMBDIRECT_CHECK_STATUS_{WARN,DISCONNECT}() + * may run concurrently without our locks, they need + * to see first_error before the status changes below, + * see __SMBDIRECT_CHECK_STATUS_FAILED(). + */ + smp_wmb(); + if (was_first) ___smbdirect_log_generic(sc, func, line, lvl, @@ -428,7 +436,7 @@ static void smbdirect_socket_cleanup_work(struct work_struct *work) */ spin_lock_irqsave(&sc->cleanup_lock, flags); if (!sc->first_error) { - sc->first_error = -ECONNABORTED; + WRITE_ONCE(sc->first_error, -ECONNABORTED); was_first = true; disable_work(&sc->connect.work); @@ -439,6 +447,13 @@ static void smbdirect_socket_cleanup_work(struct work_struct *work) } spin_unlock_irqrestore(&sc->cleanup_lock, flags); + /* + * Like in __smbdirect_socket_schedule_cleanup(), + * first_error needs to be seen before the status + * changes below. + */ + smp_wmb(); + if (was_first) smbdirect_log_rdma_event(sc, SMBDIRECT_LOG_ERR, "%s called with first_error==0\n", diff --git a/fs/smb/smbdirect/socket.h b/fs/smb/smbdirect/socket.h index 1520b6e48e7d..c669778fac4b 100644 --- a/fs/smb/smbdirect/socket.h +++ b/fs/smb/smbdirect/socket.h @@ -692,14 +692,26 @@ static __always_inline void smbdirect_socket_init(struct smbdirect_socket *sc) sc->logging.vaprintf = __smbdirect_log_vaprintf; } +/* + * A concurrent __smbdirect_socket_schedule_cleanup() or + * smbdirect_socket_cleanup_work() sets first_error before it changes + * the status, so if the status is not the expected one, it may only + * have changed because of that, which is not unexpected. That's why + * first_error is checked again after smp_rmb(). + */ #define __SMBDIRECT_CHECK_STATUS_FAILED(__sc, __expected_status, __error_cmd, __unexpected_cmd) ({ \ bool __failed = false; \ - if (unlikely((__sc)->first_error)) { \ + if (unlikely(READ_ONCE((__sc)->first_error))) { \ __failed = true; \ __error_cmd \ - } else if (unlikely((__sc)->status != (__expected_status))) { \ + } else if (unlikely(READ_ONCE((__sc)->status) != (__expected_status))) { \ __failed = true; \ - __unexpected_cmd \ + smp_rmb(); /* pairs with smp_wmb() in the cleanup functions */ \ + if (READ_ONCE((__sc)->first_error)) { \ + __error_cmd \ + } else { \ + __unexpected_cmd \ + } \ } \ __failed; \ }) -- 2.43.0 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 Cc: Paulo Alcantara Cc: Tom Talpey Cc: Tatyana Nikolova Cc: Jacob Moroni Cc: linux-cifs@vger.kernel.org Cc: samba-technical@lists.samba.org Cc: linux-rdma@vger.kernel.org Reviewed-by: Paulo Alcantara Signed-off-by: Stefan Metzmacher --- 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 /* * 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 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) mr_io_register_done() schedule_cleanup(mr->socket == NULL) The work request posted after the drain is flushed asynchronously, so its completion handler passes the NULL mr->socket to smbdirect_socket_schedule_cleanup(), and if the caller already dropped the last reference of the mr, the cqe is part of freed memory. The same happens with IB_WR_LOCAL_INV from smbdirect_connection_deregister_mr_io(). Take and release each mr->mutex in smbdirect_socket_destroy() after the status changed and before ib_drain_qp(), so a work request posted after a status check that still saw CONNECTED is posted before the drain, which then also runs its completion while the mr is still attached. Later callers see the new status under the mutex and don't post anything. Fixes: b0432201a11b ("smb: client: let destroy_mr_list() keep smbdirect_mr_io memory if registered") Fixes: de5ef8ec3c46 ("smb: smbdirect: introduce smbdirect_mr.c with client mr code") Cc: stable@vger.kernel.org Cc: Namjae Jeon Cc: Paulo Alcantara Cc: Tom Talpey Cc: linux-cifs@vger.kernel.org Cc: samba-technical@lists.samba.org Reported-by: Sashiko Closes: https://sashiko.dev/#/bug/linux-4461fbd8-a522-43ee-81d1-651432a55de9 Reviewed-by: Paulo Alcantara Signed-off-by: Stefan Metzmacher --- fs/smb/smbdirect/internal.h | 2 ++ fs/smb/smbdirect/mr.c | 31 ++++++++++++++++++++++++++++++- fs/smb/smbdirect/socket.c | 10 ++++++++++ 3 files changed, 42 insertions(+), 1 deletion(-) diff --git a/fs/smb/smbdirect/internal.h b/fs/smb/smbdirect/internal.h index a1e6a6ec64ec..3215f76302f8 100644 --- a/fs/smb/smbdirect/internal.h +++ b/fs/smb/smbdirect/internal.h @@ -136,6 +136,8 @@ void smbdirect_connection_invalidate_rkey(struct smbdirect_socket *sc, u32 rkey) int smbdirect_connection_create_mr_list(struct smbdirect_socket *sc); +void smbdirect_connection_wait_mr_io_posts(struct smbdirect_socket *sc); + void smbdirect_connection_destroy_mr_list(struct smbdirect_socket *sc); int smbdirect_accept_connect_request(struct smbdirect_socket *sc, diff --git a/fs/smb/smbdirect/mr.c b/fs/smb/smbdirect/mr.c index dd64ec50bec1..c839bae0d81b 100644 --- a/fs/smb/smbdirect/mr.c +++ b/fs/smb/smbdirect/mr.c @@ -145,6 +145,33 @@ 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(), + * from connect.work, which smbdirect_socket_destroy() already disabled + * and waited for, and by smbdirect_connection_destroy_mr_list() after + * us. Each mr on the list also holds a reference of the socket until + * 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; @@ -351,7 +378,9 @@ smbdirect_connection_register_mr_io(struct smbdirect_socket *sc, * smbdirect_connection_destroy_mr_list() (which runs before * smbdirect_connection_destroy_qp() zeroes sc->ib.qp) blocks on * this mutex, so the QP can't be destroyed under the - * ib_post_send() below. + * ib_post_send() below. smbdirect_socket_destroy() also waits + * for this mutex before it drains the QP, see + * smbdirect_connection_wait_mr_io_posts(). * * If the teardown already disabled this mr, it also detached it * from the socket and dropped its reference, so we only drop diff --git a/fs/smb/smbdirect/socket.c b/fs/smb/smbdirect/socket.c index 384cf152d40f..a1c0572a79ca 100644 --- a/fs/smb/smbdirect/socket.c +++ b/fs/smb/smbdirect/socket.c @@ -649,6 +649,16 @@ static void smbdirect_socket_destroy(struct smbdirect_socket *sc) smbdirect_socket_wake_up_all(sc); } + /* + * An IB_WR_REG_MR or IB_WR_LOCAL_INV posted after a status check + * that still saw SMBDIRECT_SOCKET_CONNECTED needs to be posted + * before the drain below, so that its completion runs while the + * mr is still attached to the socket. An earlier drain from the + * rdma cm event handler doesn't cover that, as such a post can + * still happen after it. + */ + smbdirect_connection_wait_mr_io_posts(sc); + if (sc->ib.qp) { smbdirect_log_rdma_event(sc, SMBDIRECT_LOG_INFO, "drain qp\n"); -- 2.43.0 The error paths of smbdirect_connection_send_single_iter() give back the send, local and batch credits without waking up the waiters on sc->send_io.{credits,lcredits,bcredits}.wait_queue, so other senders and the immediate send work can sleep while credits are available, until something else wakes them up. It also gives back the batch credit of a batch of the caller and clears batch->credit, so smbdirect_connection_send_batch_abort() doesn't see it and doesn't wake up the waiters either, and the credit can be used for another PDU while the aborted one is still in the batch. Wake up the waiters, like smbdirect_connection_send_batch_abort() does, and leave the batch credit of a batch of the caller to smbdirect_connection_send_batch_abort() or smbdirect_connection_send_batch_flush(). Fixes: 54abc694ebb8 ("smb: smbdirect: introduce smbdirect_connection_send_iter() and related functions") Cc: stable@vger.kernel.org Cc: Namjae Jeon Cc: Paulo Alcantara Cc: Tom Talpey Cc: linux-cifs@vger.kernel.org Cc: samba-technical@lists.samba.org Reported-by: Sashiko Closes: https://sashiko.dev/#/bug/linux-a2180487-c0d3-4f9a-92d7-208e032dd966 Reviewed-by: Paulo Alcantara Signed-off-by: Stefan Metzmacher --- fs/smb/smbdirect/connection.c | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/fs/smb/smbdirect/connection.c b/fs/smb/smbdirect/connection.c index 0e5bf32d70a6..475ec88c238b 100644 --- a/fs/smb/smbdirect/connection.c +++ b/fs/smb/smbdirect/connection.c @@ -1371,11 +1371,21 @@ int smbdirect_connection_send_single_iter(struct smbdirect_socket *sc, flush_failed: alloc_failed: atomic_inc(&sc->send_io.credits.count); + wake_up(&sc->send_io.credits.wait_queue); credit_failed: atomic_inc(&sc->send_io.lcredits.count); + wake_up(&sc->send_io.lcredits.wait_queue); lcredit_failed: - atomic_add(batch->credit, &sc->send_io.bcredits.count); - batch->credit = 0; + /* + * The batch credit of a batch of the caller is given back by + * smbdirect_connection_send_batch_abort() or + * smbdirect_connection_send_batch_flush(). + */ + if (batch == &_batch && batch->credit) { + atomic_add(batch->credit, &sc->send_io.bcredits.count); + batch->credit = 0; + wake_up(&sc->send_io.bcredits.wait_queue); + } bcredit_failed: return ret; } -- 2.43.0 smbdirect_connection_send_single_iter() moves the receive credits it grants in the message from sc->recv_io.credits.available to sc->recv_io.credits.count before the message is allocated, mapped and posted. If that fails, the peer never gets the message, but the credits stay accounted as granted, which permanently shrinks the receive credit window of the connection. Give them back to sc->recv_io.credits.available on the error paths. Fixes: 54abc694ebb8 ("smb: smbdirect: introduce smbdirect_connection_send_iter() and related functions") Cc: stable@vger.kernel.org Cc: Namjae Jeon Cc: Paulo Alcantara Cc: Tom Talpey Cc: linux-cifs@vger.kernel.org Cc: samba-technical@lists.samba.org Reported-by: Sashiko Closes: https://sashiko.dev/#/bug/linux-27f04b17-0356-40b9-94be-b0b865fde826 Reviewed-by: Paulo Alcantara Signed-off-by: Stefan Metzmacher --- fs/smb/smbdirect/connection.c | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/fs/smb/smbdirect/connection.c b/fs/smb/smbdirect/connection.c index 475ec88c238b..40b45759ebb3 100644 --- a/fs/smb/smbdirect/connection.c +++ b/fs/smb/smbdirect/connection.c @@ -1370,6 +1370,13 @@ int smbdirect_connection_send_single_iter(struct smbdirect_socket *sc, smbdirect_connection_free_send_io(msg); flush_failed: alloc_failed: + /* + * The peer never got the credits we granted in this message. + */ + if (new_credits) { + atomic_sub(new_credits, &sc->recv_io.credits.count); + atomic_add(new_credits, &sc->recv_io.credits.available); + } atomic_inc(&sc->send_io.credits.count); wake_up(&sc->send_io.credits.wait_queue); credit_failed: -- 2.43.0 The connect and accept code and their rdma cm event handlers check the status with SMBDIRECT_CHECK_STATUS_{WARN,DISCONNECT}() or WARN_ON_ONCE() and then set the status of the next step, but a concurrent teardown can run in between, e.g. when the waiter of a connect was interrupted by a signal or the listener of an accepted socket is shut down: CPU0 CPU1 CHECK_STATUS_DISCONNECT() __smbdirect_socket_schedule_cleanup() first_error = ... status = ..._FAILED smbdirect_socket_cleanup_work() status = DISCONNECTED status = NEGOTIATE_NEEDED The teardown status is overwritten with a status of the connection setup. smbdirect_socket_cleanup_work() then no longer calls rdma_disconnect() for a connection that was established and smbdirect_socket_destroy() warns about a status below SMBDIRECT_SOCKET_DISCONNECTING. smbdirect_accept_connect_request() even calls rdma_accept() for a socket whose teardown already started. The teardown sets first_error under sc->cleanup_lock before it changes the status. Add smbdirect_socket_set_status(), which only moves the status to the next step under the same lock while first_error is still 0, and use it for all status changes of the connection setup. If the status is not the expected one without a teardown, that's an internal error, it warns and starts the teardown itself, so that first_error is set whenever it returns false. Either the teardown then sees the new status and handles it, or the setup sees first_error and gives up as if the status check had failed. smbdirect_socket_accept() changed the status of a ready child socket with cmpxchg() after checking its first_error, under the listen.lock of the listener, but without the cleanup_lock of the child, so a teardown of the child could start in between and it handed out a socket whose teardown already started. It uses smbdirect_socket_set_status() now as well, under the listen.lock, the same order as when the teardown of the listener cleans up its child sockets. Fixes: 1adb2dab9727 ("smb: smbdirect: introduce SMBDIRECT_CHECK_STATUS_{WARN,DISCONNECT}()") Fixes: dc691b91ad16 ("smb: smbdirect: introduce smbdirect_socket_{listen,accept}()") Fixes: 5c171c90c446 ("smb: smbdirect: don't hand out already failed sockets in smbdirect_socket_accept()") Cc: stable@vger.kernel.org Cc: Namjae Jeon Cc: Paulo Alcantara Cc: Tom Talpey Cc: linux-cifs@vger.kernel.org Cc: samba-technical@lists.samba.org Reported-by: Sashiko Closes: https://sashiko.dev/#/bug/linux-a45eaa5a-b564-4557-bd53-2c0b037a6acb Closes: https://sashiko.dev/#/bug/linux-cadbc11c-02d9-4f3a-93f8-fa3a27f45acd Reviewed-by: Paulo Alcantara Signed-off-by: Stefan Metzmacher --- fs/smb/smbdirect/accept.c | 39 +++++++++++++++++++------------ fs/smb/smbdirect/connect.c | 32 +++++++++++++++++++------- fs/smb/smbdirect/connection.c | 8 +++---- fs/smb/smbdirect/internal.h | 4 ++++ fs/smb/smbdirect/listen.c | 6 ++--- fs/smb/smbdirect/socket.c | 43 +++++++++++++++++++++++++++++++++++ 6 files changed, 102 insertions(+), 30 deletions(-) diff --git a/fs/smb/smbdirect/accept.c b/fs/smb/smbdirect/accept.c index 5a33fcde3653..6ba1f8d8e2e0 100644 --- a/fs/smb/smbdirect/accept.c +++ b/fs/smb/smbdirect/accept.c @@ -114,9 +114,11 @@ int smbdirect_accept_connect_request(struct smbdirect_socket *sc, */ recv_io = NULL; - /* already checked with SMBDIRECT_CHECK_STATUS_WARN above */ - WARN_ON_ONCE(sc->status != SMBDIRECT_SOCKET_CREATED); - sc->status = SMBDIRECT_SOCKET_RDMA_CONNECT_NEEDED; + if (!smbdirect_socket_set_status(sc, SMBDIRECT_SOCKET_CREATED, + SMBDIRECT_SOCKET_RDMA_CONNECT_NEEDED)) { + ret = sc->first_error; + goto cleanup; + } /* * We already negotiated sp->initiator_depth @@ -139,9 +141,11 @@ int smbdirect_accept_connect_request(struct smbdirect_socket *sc, conn_param.rnr_retry_count = SMBDIRECT_RDMA_CM_RNR_RETRY; conn_param.flow_control = 0; - /* explicitly set above */ - WARN_ON_ONCE(sc->status != SMBDIRECT_SOCKET_RDMA_CONNECT_NEEDED); - sc->status = SMBDIRECT_SOCKET_RDMA_CONNECT_RUNNING; + if (!smbdirect_socket_set_status(sc, SMBDIRECT_SOCKET_RDMA_CONNECT_NEEDED, + SMBDIRECT_SOCKET_RDMA_CONNECT_RUNNING)) { + ret = sc->first_error; + goto cleanup; + } ret = rdma_accept(sc->rdma.cm_id, &conn_param); if (ret) { smbdirect_log_rdma_event(sc, SMBDIRECT_LOG_ERR, @@ -371,7 +375,9 @@ static void smbdirect_accept_negotiate_recv_work(struct work_struct *work) if (SMBDIRECT_CHECK_STATUS_DISCONNECT(sc, SMBDIRECT_SOCKET_NEGOTIATE_NEEDED)) return; - sc->status = SMBDIRECT_SOCKET_NEGOTIATE_RUNNING; + if (!smbdirect_socket_set_status(sc, SMBDIRECT_SOCKET_NEGOTIATE_NEEDED, + SMBDIRECT_SOCKET_NEGOTIATE_RUNNING)) + return; /* * Note recv_io is already part of the free list, @@ -816,7 +822,9 @@ 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; + if (!smbdirect_socket_set_status(sc, SMBDIRECT_SOCKET_RDMA_CONNECT_RUNNING, + SMBDIRECT_SOCKET_NEGOTIATE_NEEDED)) + return 0; spin_lock_irqsave(&sc->cleanup_lock, flags); if (!sc->first_error) queue_work(sc->workqueues.accept, &sc->connect.work); @@ -935,12 +943,15 @@ struct smbdirect_socket *smbdirect_socket_accept(struct smbdirect_socket *lsc, * a failed socket to the orphaned list, * like smbdirect_listen_orphan_socket() does, * and try the next one. + * + * smbdirect_socket_set_status() checks first_error + * and changes the status under nsc->cleanup_lock, + * like a concurrent teardown of nsc, so it can't + * start in between. */ - if (!READ_ONCE(nsc->first_error) && - cmpxchg(&nsc->status, - SMBDIRECT_SOCKET_NEGOTIATE_RUNNING, - SMBDIRECT_SOCKET_CONNECTED) == - SMBDIRECT_SOCKET_NEGOTIATE_RUNNING) + if (smbdirect_socket_set_status(nsc, + SMBDIRECT_SOCKET_NEGOTIATE_RUNNING, + SMBDIRECT_SOCKET_CONNECTED)) break; smbdirect_log_rdma_event(nsc, SMBDIRECT_LOG_INFO, @@ -974,7 +985,7 @@ struct smbdirect_socket *smbdirect_socket_accept(struct smbdirect_socket *lsc, * The caller expects a connected socket * now as there are no credits anyway, * above we already changed to SMBDIRECT_SOCKET_CONNECTED - * under the lsc->listen.lock and with cmpxchg. + * under the lsc->listen.lock with smbdirect_socket_set_status(). * * Now we send the negotiation response in * order to grant credits to the peer, diff --git a/fs/smb/smbdirect/connect.c b/fs/smb/smbdirect/connect.c index fc834f8960fa..6c5504c0b0fc 100644 --- a/fs/smb/smbdirect/connect.c +++ b/fs/smb/smbdirect/connect.c @@ -70,7 +70,9 @@ static int smbdirect_connect_setup_connection(struct smbdirect_socket *sc) if (SMBDIRECT_CHECK_STATUS_WARN(sc, SMBDIRECT_SOCKET_CREATED)) return -EINVAL; - sc->status = SMBDIRECT_SOCKET_RESOLVE_ADDR_NEEDED; + if (!smbdirect_socket_set_status(sc, SMBDIRECT_SOCKET_CREATED, + SMBDIRECT_SOCKET_RESOLVE_ADDR_NEEDED)) + return sc->first_error; return 0; } @@ -91,7 +93,9 @@ static int smbdirect_connect_resolve_addr(struct smbdirect_socket *sc, if (SMBDIRECT_CHECK_STATUS_WARN(sc, SMBDIRECT_SOCKET_RESOLVE_ADDR_NEEDED)) return -EINVAL; - sc->status = SMBDIRECT_SOCKET_RESOLVE_ADDR_RUNNING; + if (!smbdirect_socket_set_status(sc, SMBDIRECT_SOCKET_RESOLVE_ADDR_NEEDED, + SMBDIRECT_SOCKET_RESOLVE_ADDR_RUNNING)) + return sc->first_error; sc->rdma.expected_event = RDMA_CM_EVENT_ADDR_RESOLVED; ret = rdma_resolve_addr(sc->rdma.cm_id, src_addr, dst_addr, sp->resolve_addr_timeout_msec); @@ -112,7 +116,9 @@ static int smbdirect_connect_resolve_route(struct smbdirect_socket *sc) if (SMBDIRECT_CHECK_STATUS_DISCONNECT(sc, SMBDIRECT_SOCKET_RESOLVE_ROUTE_NEEDED)) return sc->first_error; - sc->status = SMBDIRECT_SOCKET_RESOLVE_ROUTE_RUNNING; + if (!smbdirect_socket_set_status(sc, SMBDIRECT_SOCKET_RESOLVE_ROUTE_NEEDED, + SMBDIRECT_SOCKET_RESOLVE_ROUTE_RUNNING)) + return sc->first_error; sc->rdma.expected_event = RDMA_CM_EVENT_ROUTE_RESOLVED; ret = rdma_resolve_route(sc->rdma.cm_id, sp->resolve_route_timeout_msec); if (ret) { @@ -219,7 +225,9 @@ static int smbdirect_connect_rdma_connect(struct smbdirect_socket *sc) if (SMBDIRECT_CHECK_STATUS_DISCONNECT(sc, SMBDIRECT_SOCKET_RDMA_CONNECT_NEEDED)) return sc->first_error; - sc->status = SMBDIRECT_SOCKET_RDMA_CONNECT_RUNNING; + if (!smbdirect_socket_set_status(sc, SMBDIRECT_SOCKET_RDMA_CONNECT_NEEDED, + SMBDIRECT_SOCKET_RDMA_CONNECT_RUNNING)) + return sc->first_error; sc->rdma.expected_event = RDMA_CM_EVENT_ESTABLISHED; ret = rdma_connect_locked(sc->rdma.cm_id, &conn_param); if (ret) { @@ -313,7 +321,9 @@ static int smbdirect_connect_rdma_event_handler(struct rdma_cm_id *id, case RDMA_CM_EVENT_ADDR_RESOLVED: if (SMBDIRECT_CHECK_STATUS_DISCONNECT(sc, SMBDIRECT_SOCKET_RESOLVE_ADDR_RUNNING)) return 0; - sc->status = SMBDIRECT_SOCKET_RESOLVE_ROUTE_NEEDED; + if (!smbdirect_socket_set_status(sc, SMBDIRECT_SOCKET_RESOLVE_ADDR_RUNNING, + SMBDIRECT_SOCKET_RESOLVE_ROUTE_NEEDED)) + return 0; ret = smbdirect_connect_resolve_route(sc); if (ret) @@ -323,7 +333,9 @@ static int smbdirect_connect_rdma_event_handler(struct rdma_cm_id *id, case RDMA_CM_EVENT_ROUTE_RESOLVED: if (SMBDIRECT_CHECK_STATUS_DISCONNECT(sc, SMBDIRECT_SOCKET_RESOLVE_ROUTE_RUNNING)) return 0; - sc->status = SMBDIRECT_SOCKET_RDMA_CONNECT_NEEDED; + if (!smbdirect_socket_set_status(sc, SMBDIRECT_SOCKET_RESOLVE_ROUTE_RUNNING, + SMBDIRECT_SOCKET_RDMA_CONNECT_NEEDED)) + return 0; ret = smbdirect_connect_rdma_connect(sc); if (ret) @@ -335,7 +347,9 @@ static int smbdirect_connect_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; + if (!smbdirect_socket_set_status(sc, SMBDIRECT_SOCKET_RDMA_CONNECT_RUNNING, + SMBDIRECT_SOCKET_NEGOTIATE_NEEDED)) + return 0; /* * Here we work around an inconsistency between @@ -392,7 +406,9 @@ static int smbdirect_connect_negotiate_start(struct smbdirect_socket *sc) if (SMBDIRECT_CHECK_STATUS_DISCONNECT(sc, SMBDIRECT_SOCKET_NEGOTIATE_NEEDED)) return sc->first_error; - sc->status = SMBDIRECT_SOCKET_NEGOTIATE_RUNNING; + if (!smbdirect_socket_set_status(sc, SMBDIRECT_SOCKET_NEGOTIATE_NEEDED, + SMBDIRECT_SOCKET_NEGOTIATE_RUNNING)) + return sc->first_error; ret = smbdirect_connection_create_mem_pools(sc); if (ret) { diff --git a/fs/smb/smbdirect/connection.c b/fs/smb/smbdirect/connection.c index 40b45759ebb3..832aef84dfc9 100644 --- a/fs/smb/smbdirect/connection.c +++ b/fs/smb/smbdirect/connection.c @@ -184,11 +184,9 @@ void smbdirect_connection_negotiation_done(struct smbdirect_socket *sc) /* * We are done, so we can wake up the waiter. */ - WARN_ONCE(sc->status == SMBDIRECT_SOCKET_CONNECTED, - "status=%s first_error=%1pe", - smbdirect_socket_status_string(sc->status), - SMBDIRECT_DEBUG_ERR_PTR(sc->first_error)); - sc->status = SMBDIRECT_SOCKET_CONNECTED; + if (!smbdirect_socket_set_status(sc, SMBDIRECT_SOCKET_NEGOTIATE_RUNNING, + SMBDIRECT_SOCKET_CONNECTED)) + return; done: smbdirect_log_rdma_event(sc, SMBDIRECT_LOG_INFO, diff --git a/fs/smb/smbdirect/internal.h b/fs/smb/smbdirect/internal.h index 3215f76302f8..ecaf95f629b2 100644 --- a/fs/smb/smbdirect/internal.h +++ b/fs/smb/smbdirect/internal.h @@ -57,6 +57,10 @@ void __smbdirect_socket_schedule_cleanup(struct smbdirect_socket *sc, unsigned int line, int error, enum smbdirect_socket_status *force_status); +bool smbdirect_socket_set_status(struct smbdirect_socket *sc, + enum smbdirect_socket_status old_status, + enum smbdirect_socket_status new_status); + #define smbdirect_socket_schedule_cleanup(__sc, __error) \ __smbdirect_socket_schedule_cleanup(__sc, \ "smbdirect_socket_schedule_cleanup", SMBDIRECT_LOG_ERR, \ diff --git a/fs/smb/smbdirect/listen.c b/fs/smb/smbdirect/listen.c index 2c4a2f249f45..56929202041f 100644 --- a/fs/smb/smbdirect/listen.c +++ b/fs/smb/smbdirect/listen.c @@ -111,9 +111,9 @@ int smbdirect_socket_listen(struct smbdirect_socket *sc, int backlog) "try to listen on addr: %pISpsfc\n", &sc->rdma.cm_id->route.addr.src_addr); - /* already checked above */ - WARN_ON_ONCE(sc->status != SMBDIRECT_SOCKET_CREATED); - sc->status = SMBDIRECT_SOCKET_LISTENING; + if (!smbdirect_socket_set_status(sc, SMBDIRECT_SOCKET_CREATED, + SMBDIRECT_SOCKET_LISTENING)) + return sc->first_error; sc->rdma.expected_event = RDMA_CM_EVENT_CONNECT_REQUEST; rdma_lock_handler(sc->rdma.cm_id); sc->rdma.cm_id->event_handler = smbdirect_listen_rdma_event_handler; diff --git a/fs/smb/smbdirect/socket.c b/fs/smb/smbdirect/socket.c index a1c0572a79ca..0497a3483e66 100644 --- a/fs/smb/smbdirect/socket.c +++ b/fs/smb/smbdirect/socket.c @@ -409,6 +409,49 @@ void __smbdirect_socket_schedule_cleanup(struct smbdirect_socket *sc, queue_work(sc->workqueues.cleanup, &sc->disconnect_work); } +/* + * Moves sc->status from @old_status to @new_status for the next step + * of the connection setup, unless the teardown already started. The + * teardown sets first_error under cleanup_lock before it changes the + * status, so either it sees @new_status and handles it, or we see + * first_error and leave the status of the teardown alone. + * + * The callers already checked @old_status with + * SMBDIRECT_CHECK_STATUS_*(), only the teardown can change it + * concurrently, anything else is an internal error, then we start + * the teardown ourselves, so that first_error is always set if we + * return false. + */ +bool smbdirect_socket_set_status(struct smbdirect_socket *sc, + enum smbdirect_socket_status old_status, + enum smbdirect_socket_status new_status) +{ + enum smbdirect_socket_status status; + unsigned long flags; + bool unexpected = false; + bool ok = false; + + spin_lock_irqsave(&sc->cleanup_lock, flags); + status = sc->status; + if (!sc->first_error) { + unexpected = status != old_status; + if (likely(!unexpected)) { + WRITE_ONCE(sc->status, new_status); + ok = true; + } + } + spin_unlock_irqrestore(&sc->cleanup_lock, flags); + + if (WARN_ONCE(unexpected, + "status=%s instead of %s before changing to %s\n", + smbdirect_socket_status_string(status), + smbdirect_socket_status_string(old_status), + smbdirect_socket_status_string(new_status))) + smbdirect_socket_schedule_cleanup(sc, -EINVAL); + + return ok; +} + static void smbdirect_socket_cleanup_work(struct work_struct *work) { struct smbdirect_socket *sc = -- 2.43.0 The log calls in __SMBDIRECT_CHECK_STATUS_WARN() used sc instead of the macro argument __sc, so the macro only compiled for callers whose socket variable is named sc, and with a different variable named sc in scope it would log for the wrong socket. All callers so far use sc. Fixes: 2de35d992ba1 ("smb: smbdirect: add some logging to SMBDIRECT_CHECK_STATUS_{WARN,DISCONNECT}()") Cc: stable@vger.kernel.org Cc: Namjae Jeon Cc: Paulo Alcantara Cc: Tom Talpey Cc: linux-cifs@vger.kernel.org Cc: samba-technical@lists.samba.org Reviewed-by: Paulo Alcantara Signed-off-by: Stefan Metzmacher --- fs/smb/smbdirect/socket.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/fs/smb/smbdirect/socket.h b/fs/smb/smbdirect/socket.h index c669778fac4b..a0f8c6a2d57e 100644 --- a/fs/smb/smbdirect/socket.h +++ b/fs/smb/smbdirect/socket.h @@ -725,7 +725,7 @@ static __always_inline void smbdirect_socket_init(struct smbdirect_socket *sc) __src = &(__sc)->rdma.cm_id->route.addr.src_addr; \ __dst = &(__sc)->rdma.cm_id->route.addr.dst_addr; \ } \ - smbdirect_log_rdma_event(sc, SMBDIRECT_LOG_INFO, \ + smbdirect_log_rdma_event(__sc, SMBDIRECT_LOG_INFO, \ "expected[%s] != %s first_error=%1pe local=%pISpsfc remote=%pISpsfc\n", \ smbdirect_socket_status_string(__expected_status), \ smbdirect_socket_status_string((__sc)->status), \ @@ -739,7 +739,7 @@ static __always_inline void smbdirect_socket_init(struct smbdirect_socket *sc) __src = &(__sc)->rdma.cm_id->route.addr.src_addr; \ __dst = &(__sc)->rdma.cm_id->route.addr.dst_addr; \ } \ - smbdirect_log_rdma_event(sc, SMBDIRECT_LOG_ERR, \ + smbdirect_log_rdma_event(__sc, SMBDIRECT_LOG_ERR, \ "expected[%s] != %s first_error=%1pe local=%pISpsfc remote=%pISpsfc\n", \ smbdirect_socket_status_string(__expected_status), \ smbdirect_socket_status_string((__sc)->status), \ -- 2.43.0 smbdirect_listen_rdma_event_handler() checks first_error of the listener and then warns if the status is not SMBDIRECT_SOCKET_LISTENING, but a concurrent teardown of the listener can change the status in between, so the warning is not unexpected. The teardown also walks the pending list of the listener under listen.lock without the handler lock of the listener, which is held while smbdirect_listen_connect_request() adds a new socket to that list. A socket added after the walk was still accepted with rdma_accept(), although the listener was already shut down. Use SMBDIRECT_CHECK_STATUS_WARN() for the status check, which handles a concurrent teardown without a warning, and check first_error of the listener under listen.lock before adding the new socket to the pending list. The teardown sets first_error before it walks the list under the same lock, so either it finds the new socket and cleans it up, or the new socket is not added and the connect request is rejected. A socket on the pending list that is cleaned up while smbdirect_accept_connect_request() is still running is handled by "smb: smbdirect: don't let the connection setup overwrite a teardown status". Fixes: dc691b91ad16 ("smb: smbdirect: introduce smbdirect_socket_{listen,accept}()") Cc: stable@vger.kernel.org Cc: Namjae Jeon Cc: Paulo Alcantara Cc: Tom Talpey Cc: linux-cifs@vger.kernel.org Cc: samba-technical@lists.samba.org Reported-by: Sashiko Closes: https://sashiko.dev/#/bug/linux-c32e9d93-62f2-4b1e-9c3a-5c3325ce1081 Reviewed-by: Paulo Alcantara Signed-off-by: Stefan Metzmacher --- fs/smb/smbdirect/listen.c | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/fs/smb/smbdirect/listen.c b/fs/smb/smbdirect/listen.c index 56929202041f..e9c857edb4c5 100644 --- a/fs/smb/smbdirect/listen.c +++ b/fs/smb/smbdirect/listen.c @@ -271,7 +271,12 @@ static int smbdirect_listen_rdma_event_handler(struct rdma_cm_id *new_id, switch (event->event) { case RDMA_CM_EVENT_CONNECT_REQUEST: - WARN_ON_ONCE(lsc->status != SMBDIRECT_SOCKET_LISTENING); + /* + * A concurrent teardown of the listener may have + * changed the status since the first_error check above. + */ + if (SMBDIRECT_CHECK_STATUS_WARN(lsc, SMBDIRECT_SOCKET_LISTENING)) + return -ECONNABORTED; /* * In case of error return it and let the caller @@ -395,6 +400,17 @@ static int smbdirect_listen_connect_request(struct smbdirect_socket *lsc, * releases it. */ spin_lock_irqsave(&lsc->listen.lock, flags); + /* + * The teardown of the listener sets first_error before it + * walks the pending list under listen.lock, so either it + * finds nsc there and cleans it up, or we see first_error + * and don't add nsc anymore. + */ + if (READ_ONCE(lsc->first_error)) { + spin_unlock_irqrestore(&lsc->listen.lock, flags); + ret = -ECONNABORTED; + goto listener_teardown; + } list_add_tail(&nsc->accept.list, &lsc->listen.pending); WRITE_ONCE(nsc->accept.listener, lsc); spin_unlock_irqrestore(&lsc->listen.lock, flags); @@ -425,6 +441,7 @@ static int smbdirect_listen_connect_request(struct smbdirect_socket *lsc, */ return 0; +listener_teardown: set_settings_failed: set_params_failed: /* -- 2.43.0 If the connection is no longer connected, smbdirect_connection_deregister_mr_io() disables the mr right away, which deregisters mr->mr and unmaps its pages. But the QP is only drained later by smbdirect_socket_destroy(), so the IB_WR_REG_MR that smbdirect_connection_register_mr_io() posted for it may still be pending: CPU0 CPU1 smbdirect_connection_register_mr_io() ib_post_send(IB_WR_REG_MR) smbdirect_socket_schedule_cleanup() sc->status = SMBDIRECT_SOCKET_ERROR smbdirect_connection_deregister_mr_io() smbdirect_mr_io_disable_locked() ib_dereg_mr(mr->mr) ib_dma_unmap_sg() the device processes or flushes the IB_WR_REG_MR of the freed mr E.g. the cifs client deregisters the mr of a read or write that failed to send its request because the connection is being torn down, before smbd_destroy() drained the QP. Providers like rxe keep the ib_mr pointer of a pending IB_WR_REG_MR and use it when processing it. The same happens if posting the IB_WR_LOCAL_INV fails. As the IB_WR_REG_MR is signaled, count the posted ones that are not yet completed in mr->reg_pending and wait until it's 0 without holding mr->mutex before the mr is disabled. It's a counter and not a completion, as the IB_WR_REG_MR of an earlier registration of the same mr may not be polled yet, if the peer invalidated the mr remotely and it was registered again. The completions come from the device or from the drain in smbdirect_socket_destroy(), which can take mr->mutex while we wait. Also read mr->socket only under mr->mutex, after checking that the mr is not disabled, as smbdirect_connection_destroy_mr_list() clears it. Fixes: b0432201a11b ("smb: client: let destroy_mr_list() keep smbdirect_mr_io memory if registered") Cc: stable@vger.kernel.org Cc: Namjae Jeon Cc: Paulo Alcantara Cc: Tom Talpey Cc: linux-cifs@vger.kernel.org Cc: samba-technical@lists.samba.org Reported-by: Sashiko Closes: https://sashiko.dev/#/bug/linux-200f771b-df1a-475d-9279-29e23b8f6c3e Signed-off-by: Stefan Metzmacher --- fs/smb/smbdirect/mr.c | 60 ++++++++++++++++++++++++++++++++++++--- fs/smb/smbdirect/socket.h | 5 ++++ 2 files changed, 61 insertions(+), 4 deletions(-) diff --git a/fs/smb/smbdirect/mr.c b/fs/smb/smbdirect/mr.c index c839bae0d81b..45408750fa1b 100644 --- a/fs/smb/smbdirect/mr.c +++ b/fs/smb/smbdirect/mr.c @@ -37,6 +37,7 @@ int smbdirect_connection_create_mr_list(struct smbdirect_socket *sc) kref_init(&mr->kref); mutex_init(&mr->mutex); + atomic_set(&mr->reg_pending, 0); mr->mr = ib_alloc_mr(sc->ib.pd, sc->mr_io.type, @@ -193,6 +194,16 @@ void smbdirect_connection_destroy_mr_list(struct smbdirect_socket *sc) smbdirect_mr_io_disable_locked(mr); list_del(&mr->list); mr->socket = NULL; + /* + * Wake a smbdirect_connection_deregister_mr_io() + * waiting for reg_pending, the mr is disabled now. + * wake_up_var() checks the waitqueue without a lock, + * so the store of SMBDIRECT_MR_DISABLED needs to be + * visible before, otherwise the waiter may miss it + * and the wake up. + */ + smp_mb(); + wake_up_var(&mr->reg_pending); /* * No kref_put_mutex() as it's already locked. @@ -279,6 +290,7 @@ static void smbdirect_connection_mr_io_register_done(struct ib_cq *cq, struct ib ib_wc_status_msg(wc->status), wc->opcode); smbdirect_socket_schedule_cleanup(sc, -ECONNABORTED); } + atomic_dec_and_wake_up(&mr->reg_pending); } static void smbdirect_connection_mr_io_local_inv_done(struct ib_cq *cq, struct ib_wc *wc) @@ -470,6 +482,7 @@ smbdirect_connection_register_mr_io(struct smbdirect_socket *sc, mr->need_invalidate = true; spin_unlock_irqrestore(&sc->mr_io.all.lock, flags); + atomic_inc(&mr->reg_pending); /* * There is no need for waiting for complemtion on ib_post_send * on IB_WR_REG_MR. Hardware enforces a barrier and order of execution @@ -490,6 +503,7 @@ smbdirect_connection_register_mr_io(struct smbdirect_socket *sc, spin_lock_irqsave(&sc->mr_io.all.lock, flags); mr->need_invalidate = false; spin_unlock_irqrestore(&sc->mr_io.all.lock, flags); + atomic_dec_and_wake_up(&mr->reg_pending); smbdirect_log_rdma_mr(sc, SMBDIRECT_LOG_ERR, "ib_post_send failed ret=%d (%1pe) reg_wr->key=0x%x\n", @@ -553,7 +567,7 @@ EXPORT_SYMBOL_GPL(smbdirect_mr_io_fill_buffer_descriptor); */ void smbdirect_connection_deregister_mr_io(struct smbdirect_mr_io *mr) { - struct smbdirect_socket *sc = mr->socket; + struct smbdirect_socket *sc; bool need_invalidate; unsigned long flags; int ret = 0; @@ -563,7 +577,40 @@ void smbdirect_connection_deregister_mr_io(struct smbdirect_mr_io *mr) if (mr->state == SMBDIRECT_MR_DISABLED) goto put_kref; + /* + * Only valid while the mr is not disabled, + * smbdirect_connection_destroy_mr_list() + * clears it under mr->mutex. + */ + sc = mr->socket; + if (sc->status != SMBDIRECT_SOCKET_CONNECTED) { + /* + * The QP is only drained by smbdirect_socket_destroy(), + * so our IB_WR_REG_MR may still be pending and the + * device may still use mr->mr and the mapped pages. + * Wait for its completion before + * smbdirect_mr_io_disable_locked() + * deregisters mr->mr and unmaps the pages. + * reg_pending also counts the IB_WR_REG_MR of an + * earlier registration of this mr, whose completion + * may not be polled yet, if the peer invalidated it. + * + * We still hold the reference to mr + * so we can unlock while waiting, + * smbdirect_socket_destroy() waits for mr->mutex + * before it drains the QP, which completes it. + * smbdirect_connection_destroy_mr_list() also + * wakes us after it disabled the mr, so we + * don't depend on the completions alone. + */ + if (atomic_read(&mr->reg_pending)) { + mutex_unlock(&mr->mutex); + wait_var_event(&mr->reg_pending, + !atomic_read(&mr->reg_pending) || + READ_ONCE(mr->state) == SMBDIRECT_MR_DISABLED); + goto lock_again; + } smbdirect_mr_io_disable_locked(mr); goto put_kref; } @@ -590,9 +637,15 @@ void smbdirect_connection_deregister_mr_io(struct smbdirect_mr_io *mr) smbdirect_log_rdma_mr(sc, SMBDIRECT_LOG_ERR, "ib_post_send failed ret=%d (%1pe)\n", ret, SMBDIRECT_DEBUG_ERR_PTR(ret)); - smbdirect_mr_io_disable_locked(mr); + /* + * This changes the status, so we disable + * the mr above once our IB_WR_REG_MR + * completed. + */ smbdirect_socket_schedule_cleanup(sc, ret); - goto done; + atomic_dec(&sc->mr_io.used.count); + mutex_unlock(&mr->mutex); + goto lock_again; } /* @@ -621,7 +674,6 @@ void smbdirect_connection_deregister_mr_io(struct smbdirect_mr_io *mr) if (atomic_inc_return(&sc->mr_io.ready.count) == 1) wake_up(&sc->mr_io.ready.wait_queue); -done: atomic_dec(&sc->mr_io.used.count); put_kref: diff --git a/fs/smb/smbdirect/socket.h b/fs/smb/smbdirect/socket.h index a0f8c6a2d57e..285c90a2e36f 100644 --- a/fs/smb/smbdirect/socket.h +++ b/fs/smb/smbdirect/socket.h @@ -878,6 +878,11 @@ struct smbdirect_mr_io { }; bool need_invalidate; + /* + * The number of posted IB_WR_REG_MR that are not yet + * completed, see smbdirect_connection_deregister_mr_io(). + */ + atomic_t reg_pending; struct completion invalidate_done; }; -- 2.43.0 smbdirect_socket_listen() only set sc->listen.backlog after rdma_listen() returned, but rdma_listen() already delivers connect requests to smbdirect_listen_connect_request(). There a backlog of -1 becomes SIZE_MAX, so the backlog limit is not enforced. For a wildcard address rdma_listen() listens on each device and can fail for a later device after a connect request on an earlier one was already queued. Then sc->listen.backlog stays at -1, so the cleanup skips the pending and ready child sockets and smbdirect_socket_release_destroy() frees the listener with kfree() instead of kfree_rcu(), while smbdirect_listen_orphan_socket() of a child may still dereference it under rcu_read_lock(). Set sc->listen.backlog before calling rdma_listen(). Fixes: dc691b91ad16 ("smb: smbdirect: introduce smbdirect_socket_{listen,accept}()") Cc: stable@vger.kernel.org Cc: Namjae Jeon Cc: Paulo Alcantara Cc: Tom Talpey Cc: linux-cifs@vger.kernel.org Cc: samba-technical@lists.samba.org Reported-by: Sashiko Closes: https://sashiko.dev/#/bug/linux-2d69ea06-22d7-411a-9dd0-04928cfb513e Signed-off-by: Stefan Metzmacher --- fs/smb/smbdirect/listen.c | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/fs/smb/smbdirect/listen.c b/fs/smb/smbdirect/listen.c index e9c857edb4c5..30b730540e5f 100644 --- a/fs/smb/smbdirect/listen.c +++ b/fs/smb/smbdirect/listen.c @@ -122,6 +122,18 @@ int smbdirect_socket_listen(struct smbdirect_socket *sc, int backlog) INIT_WORK(&sc->listen.purge_orphaned_work, smbdirect_listen_purge_orphaned_work); + /* + * This is a value > 0, checked above, + * so we are able to use sc->listen.backlog == -1, + * as indication that the socket was never + * a listener. + * + * It needs to be set before rdma_listen(), + * which may already deliver connect requests, + * even if it fails later for another device. + */ + sc->listen.backlog = backlog; + ret = rdma_listen(sc->rdma.cm_id, backlog); if (ret) { /* @@ -137,8 +149,10 @@ int smbdirect_socket_listen(struct smbdirect_socket *sc, int backlog) * 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. + * As listen.backlog is already set, this also + * cleans up the pending and ready child sockets of + * connect requests that arrived before rdma_listen() + * failed. */ smbdirect_socket_schedule_cleanup_lvl(sc, SMBDIRECT_LOG_INFO, ret); if (sc->rdma.cm_id->device) @@ -156,14 +170,6 @@ int smbdirect_socket_listen(struct smbdirect_socket *sc, int backlog) return ret; } - /* - * This is a value > 0, checked above, - * so we are able to use sc->listen.backlog == -1, - * as indication that the socket was never - * a listener. - */ - sc->listen.backlog = backlog; - if (sc->rdma.cm_id->device) smbdirect_log_rdma_event(sc, SMBDIRECT_LOG_INFO, "listening on addr: %pISpsfc dev: %.*s\n", -- 2.43.0 __smbdirect_socket_schedule_cleanup() and smbdirect_socket_cleanup_work() read and change sc->status without any lock, while they may run concurrently, e.g. a send or receive error racing with RDMA_CM_EVENT_DISCONNECTED: CPU0 CPU1 schedule_cleanup() reads CONNECTED schedule_cleanup() status = DISCONNECTED cleanup_work() disable_work(disconnect_work) status = ERROR queue_work(disconnect_work) is a noop The status went backwards and nobody moves it to SMBDIRECT_SOCKET_DISCONNECTING anymore, so smbdirect_socket_destroy() warns about it. If CPU0 overwrites the status before smbdirect_socket_cleanup_work() looks at it, rdma_disconnect() is called for an already disconnected cm_id. Change the status in __smbdirect_socket_schedule_cleanup() and smbdirect_socket_cleanup_work() in the same cleanup_lock section that sets first_error, like smbdirect_socket_set_status() checks first_error and changes the status under that lock. With separate sections, a second caller could still change the status before the caller that set first_error, which then forces its own status, as it was first. Keep the smp_wmb() between the two stores, for the readers without the lock. smbdirect_socket_destroy() also changes the status under cleanup_lock. As rdma_disconnect() may sleep, smbdirect_socket_cleanup_work() only decides under the lock if it's needed and calls it afterwards. Fixes: d85614860184 ("smb: smbdirect: introduce smbdirect_socket_schedule_cleanup[{_lvl,_status}]()") Cc: stable@vger.kernel.org Cc: Namjae Jeon Cc: Paulo Alcantara Cc: Tom Talpey Cc: linux-cifs@vger.kernel.org Cc: samba-technical@lists.samba.org Reported-by: Sashiko Closes: https://sashiko.dev/#/bug/linux-f94f1a82-7d57-4986-8f96-28f3bd02767d Signed-off-by: Stefan Metzmacher --- fs/smb/smbdirect/socket.c | 206 ++++++++++++++++++++++---------------- fs/smb/smbdirect/socket.h | 4 +- 2 files changed, 123 insertions(+), 87 deletions(-) diff --git a/fs/smb/smbdirect/socket.c b/fs/smb/smbdirect/socket.c index 0497a3483e66..07644b20c243 100644 --- a/fs/smb/smbdirect/socket.c +++ b/fs/smb/smbdirect/socket.c @@ -268,6 +268,7 @@ void __smbdirect_socket_schedule_cleanup(struct smbdirect_socket *sc, enum smbdirect_socket_status *force_status) { struct smbdirect_socket *psc, *tsc; + enum smbdirect_socket_status old_status; unsigned long flags; bool was_first = false; @@ -296,7 +297,6 @@ void __smbdirect_socket_schedule_cleanup(struct smbdirect_socket *sc, sc->idle.keepalive = SMBDIRECT_KEEPALIVE_NONE; disable_delayed_work(&sc->idle.timer_work); } - spin_unlock_irqrestore(&sc->cleanup_lock, flags); /* * Callers of SMBDIRECT_CHECK_STATUS_{WARN,DISCONNECT}() @@ -306,53 +306,16 @@ void __smbdirect_socket_schedule_cleanup(struct smbdirect_socket *sc, */ smp_wmb(); - if (was_first) - ___smbdirect_log_generic(sc, func, line, - lvl, - SMBDIRECT_LOG_RDMA_EVENT, - "%s(%1pe%s%s) called from %s in line=%u status=%s\n", - macro_name, - SMBDIRECT_DEBUG_ERR_PTR(error), - force_status ? ", " : "", - force_status ? smbdirect_socket_status_string(*force_status) : "", - func, line, - smbdirect_socket_status_string(sc->status)); - /* - * In case we were a listener we need to - * disconnect all pending and ready sockets - * - * First we move ready sockets to pending again. - * - * Only a socket that was a listener (listen.backlog != -1) owns a - * populated listen.ready/pending list. Guarding on that also keeps - * lockdep quiet: without it, the listener holds sc->listen.lock while - * the loop recurses into each child psc, which takes psc->listen.lock. - * Those are always different instances of the same lock class -- a - * child never listens, so the nesting is strictly listener -> child - * and cannot really deadlock -- but lockdep only sees the class and - * reports "possible recursive locking". A child has empty listen - * lists and nothing to do here, so skipping it loses nothing, and a - * pending child stays on its listener's list for the free path - * (smbdirect_socket_destroy) to reap. + * The status is changed together with first_error under + * cleanup_lock, so concurrent callers and + * smbdirect_socket_cleanup_work() can't move it backwards, + * e.g. from SMBDIRECT_SOCKET_DISCONNECTED to + * SMBDIRECT_SOCKET_ERROR, and only the caller that set + * first_error decides the error status, later callers + * only move it forward. */ - if (sc->listen.backlog != -1) { /* was a listener */ - /* - * We don't move them to the orphaned list here, - * each child does that itself at the end of its - * smbdirect_socket_cleanup_work(), see - * smbdirect_listen_orphan_socket(). Only that checks - * accept.listener, which is still NULL for a child - * that smbdirect_listen_connect_request() is still - * setting up and will release itself on failure. - */ - spin_lock_irqsave(&sc->listen.lock, flags); - list_splice_init(&sc->listen.ready, &sc->listen.pending); - list_for_each_entry_safe(psc, tsc, &sc->listen.pending, accept.list) - smbdirect_socket_schedule_cleanup(psc, sc->first_error); - spin_unlock_irqrestore(&sc->listen.lock, flags); - } - + old_status = sc->status; switch (sc->status) { case SMBDIRECT_SOCKET_RESOLVE_ADDR_FAILED: case SMBDIRECT_SOCKET_RESOLVE_ROUTE_FAILED: @@ -399,6 +362,55 @@ void __smbdirect_socket_schedule_cleanup(struct smbdirect_socket *sc, if (force_status && (was_first || *force_status > sc->status)) sc->status = *force_status; + spin_unlock_irqrestore(&sc->cleanup_lock, flags); + + if (was_first) + ___smbdirect_log_generic(sc, func, line, + lvl, + SMBDIRECT_LOG_RDMA_EVENT, + "%s(%1pe%s%s) called from %s in line=%u status=%s\n", + macro_name, + SMBDIRECT_DEBUG_ERR_PTR(error), + force_status ? ", " : "", + force_status ? smbdirect_socket_status_string(*force_status) : "", + func, line, + smbdirect_socket_status_string(old_status)); + + /* + * In case we were a listener we need to + * disconnect all pending and ready sockets + * + * First we move ready sockets to pending again. + * + * Only a socket that was a listener (listen.backlog != -1) owns a + * populated listen.ready/pending list. Guarding on that also keeps + * lockdep quiet: without it, the listener holds sc->listen.lock while + * the loop recurses into each child psc, which takes psc->listen.lock. + * Those are always different instances of the same lock class -- a + * child never listens, so the nesting is strictly listener -> child + * and cannot really deadlock -- but lockdep only sees the class and + * reports "possible recursive locking". A child has empty listen + * lists and nothing to do here, so skipping it loses nothing, and a + * pending child stays on its listener's list for the free path + * (smbdirect_socket_destroy) to reap. + */ + if (sc->listen.backlog != -1) { /* was a listener */ + /* + * We don't move them to the orphaned list here, + * each child does that itself at the end of its + * smbdirect_socket_cleanup_work(), see + * smbdirect_listen_orphan_socket(). + * smbdirect_listen_connect_request() sets + * accept.listener before it adds a child to the + * pending list, so a child whose setup fails is + * orphaned the same way. + */ + spin_lock_irqsave(&sc->listen.lock, flags); + list_splice_init(&sc->listen.ready, &sc->listen.pending); + list_for_each_entry_safe(psc, tsc, &sc->listen.pending, accept.list) + smbdirect_socket_schedule_cleanup(psc, sc->first_error); + spin_unlock_irqrestore(&sc->listen.lock, flags); + } /* * Wake up all waiters in all wait queues @@ -457,6 +469,8 @@ static void smbdirect_socket_cleanup_work(struct work_struct *work) struct smbdirect_socket *sc = container_of(work, struct smbdirect_socket, disconnect_work); struct smbdirect_socket *psc, *tsc; + enum smbdirect_socket_status old_status; + bool disconnect = false; bool was_first = false; unsigned long flags; @@ -488,19 +502,55 @@ static void smbdirect_socket_cleanup_work(struct work_struct *work) sc->idle.keepalive = SMBDIRECT_KEEPALIVE_NONE; disable_delayed_work(&sc->idle.timer_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. + * changes below and the status is changed together + * with first_error under cleanup_lock. */ smp_wmb(); + old_status = sc->status; + switch (sc->status) { + case SMBDIRECT_SOCKET_NEGOTIATE_NEEDED: + case SMBDIRECT_SOCKET_NEGOTIATE_RUNNING: + case SMBDIRECT_SOCKET_NEGOTIATE_FAILED: + case SMBDIRECT_SOCKET_CONNECTED: + case SMBDIRECT_SOCKET_ERROR: + sc->status = SMBDIRECT_SOCKET_DISCONNECTING; + disconnect = true; + break; + + case SMBDIRECT_SOCKET_CREATED: + case SMBDIRECT_SOCKET_LISTENING: + case SMBDIRECT_SOCKET_RESOLVE_ADDR_NEEDED: + case SMBDIRECT_SOCKET_RESOLVE_ADDR_RUNNING: + case SMBDIRECT_SOCKET_RESOLVE_ADDR_FAILED: + case SMBDIRECT_SOCKET_RESOLVE_ROUTE_NEEDED: + case SMBDIRECT_SOCKET_RESOLVE_ROUTE_RUNNING: + case SMBDIRECT_SOCKET_RESOLVE_ROUTE_FAILED: + case SMBDIRECT_SOCKET_RDMA_CONNECT_NEEDED: + case SMBDIRECT_SOCKET_RDMA_CONNECT_RUNNING: + case SMBDIRECT_SOCKET_RDMA_CONNECT_FAILED: + /* + * rdma_{accept,connect}() never reached + * RDMA_CM_EVENT_ESTABLISHED + */ + sc->status = SMBDIRECT_SOCKET_DISCONNECTED; + break; + + case SMBDIRECT_SOCKET_DISCONNECTING: + case SMBDIRECT_SOCKET_DISCONNECTED: + case SMBDIRECT_SOCKET_DESTROYED: + break; + } + spin_unlock_irqrestore(&sc->cleanup_lock, flags); + if (was_first) smbdirect_log_rdma_event(sc, SMBDIRECT_LOG_ERR, "%s called with first_error==0\n", - smbdirect_socket_status_string(sc->status)); + smbdirect_socket_status_string(old_status)); /* * In case we were a listener we need to @@ -519,10 +569,11 @@ static void smbdirect_socket_cleanup_work(struct work_struct *work) * We don't move them to the orphaned list here, * each child does that itself at the end of its * smbdirect_socket_cleanup_work(), see - * smbdirect_listen_orphan_socket(). Only that checks - * accept.listener, which is still NULL for a child - * that smbdirect_listen_connect_request() is still - * setting up and will release itself on failure. + * smbdirect_listen_orphan_socket(). + * smbdirect_listen_connect_request() sets + * accept.listener before it adds a child to the + * pending list, so a child whose setup fails is + * orphaned the same way. */ spin_lock_irqsave(&sc->listen.lock, flags); list_splice_init(&sc->listen.ready, &sc->listen.pending); @@ -531,13 +582,7 @@ static void smbdirect_socket_cleanup_work(struct work_struct *work) spin_unlock_irqrestore(&sc->listen.lock, flags); } - switch (sc->status) { - case SMBDIRECT_SOCKET_NEGOTIATE_NEEDED: - case SMBDIRECT_SOCKET_NEGOTIATE_RUNNING: - case SMBDIRECT_SOCKET_NEGOTIATE_FAILED: - case SMBDIRECT_SOCKET_CONNECTED: - case SMBDIRECT_SOCKET_ERROR: - sc->status = SMBDIRECT_SOCKET_DISCONNECTING; + if (disconnect) { /* * Make sure we hold the callback lock * im order to coordinate with the @@ -550,32 +595,17 @@ static void smbdirect_socket_cleanup_work(struct work_struct *work) * by the mutex. */ rdma_lock_handler(sc->rdma.cm_id); - rdma_disconnect(sc->rdma.cm_id); - rdma_unlock_handler(sc->rdma.cm_id); - break; - - case SMBDIRECT_SOCKET_CREATED: - case SMBDIRECT_SOCKET_LISTENING: - case SMBDIRECT_SOCKET_RESOLVE_ADDR_NEEDED: - case SMBDIRECT_SOCKET_RESOLVE_ADDR_RUNNING: - case SMBDIRECT_SOCKET_RESOLVE_ADDR_FAILED: - case SMBDIRECT_SOCKET_RESOLVE_ROUTE_NEEDED: - case SMBDIRECT_SOCKET_RESOLVE_ROUTE_RUNNING: - case SMBDIRECT_SOCKET_RESOLVE_ROUTE_FAILED: - case SMBDIRECT_SOCKET_RDMA_CONNECT_NEEDED: - case SMBDIRECT_SOCKET_RDMA_CONNECT_RUNNING: - case SMBDIRECT_SOCKET_RDMA_CONNECT_FAILED: /* - * rdma_{accept,connect}() never reached - * RDMA_CM_EVENT_ESTABLISHED + * The rdma_event handlers run under the handler + * lock and may have moved the status forward to + * SMBDIRECT_SOCKET_DISCONNECTED after we dropped + * cleanup_lock, on RDMA_CM_EVENT_DISCONNECTED or + * RDMA_CM_EVENT_DEVICE_REMOVAL, then + * rdma_disconnect() is not needed anymore. */ - sc->status = SMBDIRECT_SOCKET_DISCONNECTED; - break; - - case SMBDIRECT_SOCKET_DISCONNECTING: - case SMBDIRECT_SOCKET_DISCONNECTED: - case SMBDIRECT_SOCKET_DESTROYED: - break; + if (READ_ONCE(sc->status) == SMBDIRECT_SOCKET_DISCONNECTING) + rdma_disconnect(sc->rdma.cm_id); + rdma_unlock_handler(sc->rdma.cm_id); } /* @@ -605,6 +635,7 @@ static void smbdirect_socket_destroy(struct smbdirect_socket *sc) struct smbdirect_recv_io *recv_tmp; LIST_HEAD(all_list); LIST_HEAD(pending_list); + bool changed; unsigned long flags; smbdirect_log_rdma_event(sc, SMBDIRECT_LOG_INFO, @@ -687,10 +718,13 @@ static void smbdirect_socket_destroy(struct smbdirect_socket *sc) * will be a noop when calling smbdirect_socket_schedule_cleanup* * ib_drain_qp() also won't be called. */ - if (sc->status < SMBDIRECT_SOCKET_DISCONNECTED) { + spin_lock_irqsave(&sc->cleanup_lock, flags); + changed = sc->status < SMBDIRECT_SOCKET_DISCONNECTED; + if (changed) sc->status = SMBDIRECT_SOCKET_DISCONNECTED; + spin_unlock_irqrestore(&sc->cleanup_lock, flags); + if (changed) smbdirect_socket_wake_up_all(sc); - } /* * An IB_WR_REG_MR or IB_WR_LOCAL_INV posted after a status check diff --git a/fs/smb/smbdirect/socket.h b/fs/smb/smbdirect/socket.h index 285c90a2e36f..9203b8d404e3 100644 --- a/fs/smb/smbdirect/socket.h +++ b/fs/smb/smbdirect/socket.h @@ -115,7 +115,9 @@ struct smbdirect_socket { * 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. + * also hold cleanup_lock. The teardown also changes the status + * in the same cleanup_lock section that sets first_error, so + * concurrent teardowns can't move it backwards. */ spinlock_t cleanup_lock; -- 2.43.0 smbdirect_connect_negotiate_start() and smbdirect_accept_connect_request() create the memory pools and the QP, which may sleep, and then take a recv_io from the pool, with a WARN_ON_ONCE() if that fails. But a concurrent teardown can set first_error in the meantime, e.g. when the waiter of a connect times out or is interrupted or the listener is torn down, and then smbdirect_connection_get_recv_io() returns NULL although the pool was just filled: CPU0 CPU1 smbdirect_connect_negotiate_start() smbdirect_connection_create_mem_pools() schedule_cleanup(sc) first_error = ... smbdirect_connection_get_recv_io() returns NULL as first_error is set WARN_ON_ONCE(!recv_io) That's not a bug, so only warn if first_error is not set, and return first_error instead of -EINVAL. Fixes: d1f187656797 ("smb: smbdirect: introduce smbdirect_connect[_sync]()") Fixes: eb3ed1e9048c ("smb: smbdirect: introduce smbdirect_accept_connect_request()") Cc: stable@vger.kernel.org Cc: Namjae Jeon Cc: Paulo Alcantara Cc: Tom Talpey Cc: linux-cifs@vger.kernel.org Cc: samba-technical@lists.samba.org Reported-by: Sashiko Closes: https://sashiko.dev/#/bug/linux-eb934b08-9b80-4e13-a535-6332a3d53abb Signed-off-by: Stefan Metzmacher --- fs/smb/smbdirect/accept.c | 11 +++++++++-- fs/smb/smbdirect/connect.c | 11 +++++++++-- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/fs/smb/smbdirect/accept.c b/fs/smb/smbdirect/accept.c index 6ba1f8d8e2e0..d9e2fea73e38 100644 --- a/fs/smb/smbdirect/accept.c +++ b/fs/smb/smbdirect/accept.c @@ -87,8 +87,15 @@ int smbdirect_accept_connect_request(struct smbdirect_socket *sc, } recv_io = smbdirect_connection_get_recv_io(sc); - if (WARN_ON_ONCE(!recv_io)) { - ret = -EINVAL; + if (!recv_io) { + /* + * The pool was just filled, so only a concurrent + * teardown, which sets first_error, makes + * smbdirect_connection_get_recv_io() fail here. + */ + ret = READ_ONCE(sc->first_error); + if (WARN_ON_ONCE(!ret)) + ret = -EINVAL; smbdirect_log_rdma_event(sc, SMBDIRECT_LOG_ERR, "smbdirect_connection_get_recv_io() failed %1pe\n", SMBDIRECT_DEBUG_ERR_PTR(ret)); diff --git a/fs/smb/smbdirect/connect.c b/fs/smb/smbdirect/connect.c index 6c5504c0b0fc..98ee25c0c334 100644 --- a/fs/smb/smbdirect/connect.c +++ b/fs/smb/smbdirect/connect.c @@ -430,8 +430,15 @@ static int smbdirect_connect_negotiate_start(struct smbdirect_socket *sc) atomic_set(&sc->send_io.lcredits.count, sp->send_credit_target); recv_io = smbdirect_connection_get_recv_io(sc); - if (WARN_ON_ONCE(!recv_io)) { - ret = -EINVAL; + if (!recv_io) { + /* + * The pool was just filled, so only a concurrent + * teardown, which sets first_error, makes + * smbdirect_connection_get_recv_io() fail here. + */ + ret = READ_ONCE(sc->first_error); + if (WARN_ON_ONCE(!ret)) + ret = -EINVAL; smbdirect_log_rdma_event(sc, SMBDIRECT_LOG_ERR, "smbdirect_connection_get_recv_io() failed %1pe\n", SMBDIRECT_DEBUG_ERR_PTR(ret)); -- 2.43.0