From: Bryam Vargas A successful completion on the old link handled after smc_switch_cursor() credits bytes that were rewound for resend, so the application can overwrite send buffer data before it is resent. A connection in a closing state is moved to the new link without a rewind; when completions on the two links are then handled out of order, one whose cursor is behind tx_curs_fin moves tx_curs_fin back and the same bytes are credited again, which can take sndbuf_space past the send buffer size. Tag each CDC pend with a switch generation that smc_switch_cursor() bumps, and credit a completion only if it is of the current generation and its cursor is not behind tx_curs_fin. Fixes: c6f02ebeea3a ("net/smc: switch connections to alternate link") Cc: stable@vger.kernel.org Signed-off-by: Bryam Vargas --- net/smc/smc.h | 3 +++ net/smc/smc_cdc.c | 11 ++++++++++- net/smc/smc_cdc.h | 13 +++++++++++++ net/smc/smc_core.c | 1 + 4 files changed, 27 insertions(+), 1 deletion(-) diff --git a/net/smc/smc.h b/net/smc/smc.h index 36b243b34a9a..96f017938b9c 100644 --- a/net/smc/smc.h +++ b/net/smc/smc.h @@ -233,6 +233,9 @@ struct smc_connection { atomic_t sndbuf_space; /* remaining space in sndbuf */ u16 tx_cdc_seq; /* sequence # for CDC send */ u16 tx_cdc_seq_fin; /* sequence # - tx completed */ + u16 tx_switch_gen; /* bumped when a link switch + * rewinds the tx cursors + */ spinlock_t send_lock; /* protect wr_sends */ atomic_t cdc_pend_tx_wr; /* number of pending tx CDC wqe * - inc when post wqe, diff --git a/net/smc/smc_cdc.c b/net/smc/smc_cdc.c index 32d6d03df321..d1f346376c08 100644 --- a/net/smc/smc_cdc.c +++ b/net/smc/smc_cdc.c @@ -36,7 +36,15 @@ static void smc_cdc_tx_handler(struct smc_wr_tx_pend_priv *pnd_snd, sndbuf_desc = conn->sndbuf_desc; smc = container_of(conn, struct smc_sock, conn); bh_lock_sock(&smc->sk); - if (!wc_status && sndbuf_desc) { + /* A CDC posted before a link switch rewound the cursors: its bytes + * are resent on the new link or credited by the switch's catch-up. + * A cursor behind tx_curs_fin (completions on two links in any + * order) was credited already. + */ + if (!wc_status && sndbuf_desc && + cdcpend->switch_gen == conn->tx_switch_gen && + smc_curs_sdiff(sndbuf_desc->len, &conn->tx_curs_fin, + &cdcpend->cursor) >= 0) { diff = smc_curs_diff(sndbuf_desc->len, &cdcpend->conn->tx_curs_fin, &cdcpend->cursor); @@ -107,6 +115,7 @@ static inline void smc_cdc_add_pending_send(struct smc_connection *conn, pend->cursor = conn->tx_curs_sent; pend->p_cursor = conn->local_tx_ctrl.prod; pend->ctrl_seq = conn->tx_cdc_seq; + pend->switch_gen = conn->tx_switch_gen; } int smc_cdc_msg_send(struct smc_connection *conn, diff --git a/net/smc/smc_cdc.h b/net/smc/smc_cdc.h index 696cc11f2303..597acb2e123f 100644 --- a/net/smc/smc_cdc.h +++ b/net/smc/smc_cdc.h @@ -170,6 +170,18 @@ static inline int smc_curs_comp(unsigned int size, return smc_curs_diff(size, old, new); } +/* signed distance from old to new; the wrap difference is taken as s16, + * so it holds across the u16 wrap rollover, for cursors less than 32768 + * rings apart + */ +static inline s64 smc_curs_sdiff(unsigned int size, + union smc_host_cursor *old, + union smc_host_cursor *new) +{ + return (s64)(s16)(new->wrap - old->wrap) * size + + (s64)new->count - old->count; +} + /* calculate cursor difference between old and new, where old <= new and * difference may exceed size */ @@ -284,6 +296,7 @@ struct smc_cdc_tx_pend { union smc_host_cursor cursor; /* tx sndbuf cursor sent */ union smc_host_cursor p_cursor; /* rx RMBE cursor produced */ u16 ctrl_seq; /* conn. tx sequence # */ + u16 switch_gen; /* tx_switch_gen at send */ }; int smc_cdc_get_free_slot(struct smc_connection *conn, diff --git a/net/smc/smc_core.c b/net/smc/smc_core.c index 73e86100e6db..fb9f6a4eb683 100644 --- a/net/smc/smc_core.c +++ b/net/smc/smc_core.c @@ -1058,6 +1058,7 @@ static int smc_switch_cursor(struct smc_sock *smc, struct smc_cdc_tx_pend *pend, int rc = 0; int diff; + 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() */ -- 2.56.0