From: Bryam Vargas <hexl…@proton.me> smc_switch_cursor() orders the peer's consumer cursor against local_tx_ctrl_fin with smc_curs_comp(), which misorders them across the 65535 -> 0 rollover of the u16 wrap. A consumer cursor that is behind reads as ahead: the catch-up can credit sndbuf_space for bytes never sent and tell the peer they are in its RMB, so the next send() can copy past the end of the send buffer. One that is ahead reads as behind and the catch-up is skipped. With a send buffer larger than the peer's RMB, the catch-up can also be short once the peer is more than one RMB past local_tx_ctrl_fin, and the connection then stalls. Use the signed cursor distance, advance the RMB cursors across any number of wraps, and bound the catch-up by tx_curs_prep: a conforming peer has read no further, and, unlike tx_curs_sent, no failover rewinds it. A forged consumer cursor is taken off the wire with no check against what was sent; the bound also caps what it can add to sndbuf_space. Fixes: c6f02ebeea3a ("net/smc: switch connections to alternate link") Closes: https://netdev-ai.bots.linux.dev/sashiko/#/patchset/2026…%40proton.me?part=4 Cc: stab…@vger.kernel.org Signed-off-by: Bryam Vargas <hexl…@proton.me> --- net/smc/smc_cdc.h | 12 ++++++++++++ net/smc/smc_core.c | 16 +++++++++++----- 2 files changed, 23 insertions(+), 5 deletions(-) diff --git a/net/smc/smc_cdc.h b/net/smc/smc_cdc.h index 74e675407410..27d24a893e87 100644 --- a/net/smc/smc_cdc.h +++ b/net/smc/smc_cdc.h @@ -13,6 +13,7 @@ #define SMC_CDC_H #include <linux/kernel.h> /* max_t */ +#include <linux/math64.h> #include <linux/atomic.h> #include <linux/in.h> #include <linux/compiler.h> @@ -97,6 +98,17 @@ static inline void smc_curs_add(int size, union smc_host_cursor *curs, } } +/* smc_curs_add() for a value that may exceed size */ +static inline void smc_curs_advance(unsigned int size, + union smc_host_cursor *curs, u32 value) +{ + u64 count = (u64)curs->count + value; + u32 rem; + + curs->wrap += div_u64_rem(count, size, &rem); + curs->count = rem; +} + /* Copy cursor src into tgt */ static inline void smc_curs_copy(union smc_host_cursor *tgt, union smc_host_cursor *src, diff --git a/net/smc/smc_core.c b/net/smc/smc_core.c index 303c31db3529..a32f676b19f8 100644 --- a/net/smc/smc_core.c +++ b/net/smc/smc_core.c @@ -1054,9 +1054,9 @@ 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, prod, sent; + union smc_host_cursor cons, prep, prod, sent; int rc = 0; - int diff = 0; + s64 diff; conn->tx_switch_gen++; /* completions of earlier sends: no credit */ /* The cursors of the connection have readers outside send_lock and @@ -1065,14 +1065,20 @@ static int smc_switch_cursor(struct smc_sock *smc, struct smc_cdc_tx_pend *pend, 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); + smc_curs_copy(&prep, &conn->tx_curs_prep, conn); - if (smc_curs_comp(conn->peer_rmbe_size, &cons, &prod) < 0) { + /* cons comes off the wire: go no further than tx_curs_prep, which + * a conforming peer has not read past and no failover rewinds. That + * also keeps diff within one send buffer for smc_curs_add(). + */ + diff = min(smc_curs_sdiff(conn->peer_rmbe_size, &prod, &cons), + smc_curs_sdiff(conn->sndbuf_desc->len, &sent, &prep)); + if (diff > 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_advance(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); }
-- 2.56.1