From: Bryam Vargas smc_switch_cursor() rewinds tx_curs_sent and local_tx_ctrl.prod, then advances them and the two fin cursors in place with smc_curs_add(), which updates count and wrap in separate steps. smc_tx_prepared_sends() reads tx_curs_sent without send_lock or the socket spinlock, e.g. from smc_close_stream_wait(): a cursor caught between the wrap increment and the reduction of the count reads as nothing left to send, and close() stops waiting while data is still queued. Compute the cursors on local copies and store each of them once with smc_curs_copy(), as smc_tx_rdma_writes() does. Keep the store of tx_curs_sent before the credit to sndbuf_space, as it is now: smc_tx_sendmsg() can take that space while the switch runs, and smc_tx_prepared_sends() needs tx_curs_prep within one send buffer of tx_curs_sent. Fixes: c6f02ebeea3a ("net/smc: switch connections to alternate link") Closes: https://lore.kernel.org/all/sashiko-outbox-215218@kernel.org/ Cc: stable@vger.kernel.org Signed-off-by: Bryam Vargas --- net/smc/smc_core.c | 46 ++++++++++++++++++++++++++-------------------- 1 file changed, 26 insertions(+), 20 deletions(-) diff --git a/net/smc/smc_core.c b/net/smc/smc_core.c index fb9f6a4eb683..303c31db3529 100644 --- a/net/smc/smc_core.c +++ b/net/smc/smc_core.c @@ -1054,35 +1054,41 @@ static int smc_switch_cursor(struct smc_sock *smc, struct smc_cdc_tx_pend *pend, struct smc_wr_buf *wr_buf) { struct smc_connection *conn = &smc->conn; - union smc_host_cursor cons, fin; + union smc_host_cursor cons, prod, sent; int rc = 0; - int diff; + int diff = 0; conn->tx_switch_gen++; /* completions of earlier sends: no credit */ - smc_curs_copy(&conn->tx_curs_sent, &conn->tx_curs_fin, conn); - smc_curs_copy(&fin, &conn->local_tx_ctrl_fin, conn); - /* set prod cursor to old state, enforce tx_rdma_writes() */ - smc_curs_copy(&conn->local_tx_ctrl.prod, &fin, conn); + /* The cursors of the connection have readers outside send_lock and + * the socket spinlock: work on copies and store each cursor once. + */ + smc_curs_copy(&sent, &conn->tx_curs_fin, conn); + smc_curs_copy(&prod, &conn->local_tx_ctrl_fin, conn); smc_curs_copy(&cons, &conn->local_rx_ctrl.cons, conn); - if (smc_curs_comp(conn->peer_rmbe_size, &cons, &fin) < 0) { - /* cons cursor advanced more than fin, and prod was set - * fin above, so now prod is smaller than cons. Fix that. + if (smc_curs_comp(conn->peer_rmbe_size, &cons, &prod) < 0) { + /* cons cursor advanced more than fin: the peer has read + * that far, so resend from there and count it as confirmed. + */ + diff = smc_curs_diff(conn->peer_rmbe_size, &prod, &cons); + smc_curs_add(conn->sndbuf_desc->len, &sent, diff); + smc_curs_add(conn->peer_rmbe_size, &prod, diff); + smc_curs_copy(&conn->tx_curs_fin, &sent, conn); + smc_curs_copy(&conn->local_tx_ctrl_fin, &prod, conn); + } + /* set the sent and prod cursors to the resend's start, enforce + * tx_rdma_writes() + */ + smc_curs_copy(&conn->tx_curs_sent, &sent, conn); + smc_curs_copy(&conn->local_tx_ctrl.prod, &prod, conn); + if (diff > 0) { + /* after tx_curs_sent is stored: a sender that takes this + * space moves tx_curs_prep, which has to stay within one + * send buffer of tx_curs_sent */ - diff = smc_curs_diff(conn->peer_rmbe_size, &fin, &cons); - smc_curs_add(conn->sndbuf_desc->len, - &conn->tx_curs_sent, diff); - smc_curs_add(conn->sndbuf_desc->len, - &conn->tx_curs_fin, diff); - smp_mb__before_atomic(); atomic_add(diff, &conn->sndbuf_space); smp_mb__after_atomic(); - - smc_curs_add(conn->peer_rmbe_size, - &conn->local_tx_ctrl.prod, diff); - smc_curs_add(conn->peer_rmbe_size, - &conn->local_tx_ctrl_fin, diff); } /* recalculate, value is used by tx_rdma_writes() */ atomic_set(&smc->conn.peer_rmbe_space, smc_write_space(conn)); -- 2.56.1