From: Bryam Vargas smc_rx_update_consumer() orders the consumer cursor against urg_curs with smc_curs_comp(), which misorders them across the 65535 -> 0 rollover of the RMB wrap. Without SO_OOBINLINE, with the reader on wrap 65535 and the urgent byte on wrap 0, the mark reads as already passed: urg_state becomes SMC_URG_READ before the byte is reached and recv(MSG_OOB) fails with -EINVAL. The urgent byte, which bytes_to_rcv leaves out, is then never skipped. The sender waits for its urgent data to be consumed before it sends more, so the connection stalls. smc_rx_recv_urg() compares the same two cursors with smc_curs_diff(). A consumer cursor that is past the mark on the RMB's next wrap reads as far behind it, and urg_rx_skip_pend is set with nothing left to skip. SIOCATMARK uses smc_curs_diff() as well and does not test urg_state: with urg_curs at the start of the RMB, a reader one byte short of the RMB's end is at the mark only on the wrap before the mark's, but is told it is at the mark on every wrap except the mark's own. That includes a connection that never received urgent data, where urg_curs is zero. In all three places, use smc_curs_sdiff(), which orders the two cursors across the wrap rollover. Let SIOCATMARK report the mark only while it is valid or a skip of its byte is pending. smc_curs_comp() has no other user; remove it. Fixes: de8474eb9d50 ("net/smc: urgent data support") Cc: stable@vger.kernel.org # depends on "net/smc: do not credit stale CDC completions after a link switch" and "net/smc: fix the failover catch-up across the cursor wrap rollover" Signed-off-by: Bryam Vargas --- net/smc/af_smc.c | 6 ++++-- net/smc/smc_cdc.h | 13 ------------- net/smc/smc_rx.c | 11 ++++++----- 3 files changed, 10 insertions(+), 20 deletions(-) diff --git a/net/smc/af_smc.c b/net/smc/af_smc.c index bfc0c7e71a12..3877a3a4cbae 100644 --- a/net/smc/af_smc.c +++ b/net/smc/af_smc.c @@ -3277,8 +3277,10 @@ int smc_ioctl(struct socket *sock, unsigned int cmd, } else { smc_curs_copy(&cons, &conn->local_tx_ctrl.cons, conn); smc_curs_copy(&urg, &conn->urg_curs, conn); - answ = smc_curs_diff(conn->rmb_desc->len, - &cons, &urg) == 1; + answ = (conn->urg_state == SMC_URG_VALID || + conn->urg_rx_skip_pend) && + smc_curs_sdiff(conn->rmb_desc->len, + &cons, &urg) == 1; } break; default: diff --git a/net/smc/smc_cdc.h b/net/smc/smc_cdc.h index 27d24a893e87..ae2cf74a0547 100644 --- a/net/smc/smc_cdc.h +++ b/net/smc/smc_cdc.h @@ -169,19 +169,6 @@ static inline int smc_curs_diff(unsigned int size, return max_t(int, 0, (new->count - old->count)); } -/* calculate cursor difference between old and new - returns negative - * value in case old > new - */ -static inline int smc_curs_comp(unsigned int size, - union smc_host_cursor *old, - union smc_host_cursor *new) -{ - if (old->wrap > new->wrap || - (old->wrap == new->wrap && old->count > new->count)) - return -smc_curs_diff(size, new, old); - return smc_curs_diff(size, old, new); -} - /* signed distance from old to new; the wrap difference is taken as s16, * so the result is correct across the u16 wrap rollover as long as the * wraps of the two cursors are less than 32768 apart diff --git a/net/smc/smc_rx.c b/net/smc/smc_rx.c index 63c178c10bf1..564b976426ca 100644 --- a/net/smc/smc_rx.c +++ b/net/smc/smc_rx.c @@ -62,14 +62,15 @@ static int smc_rx_update_consumer(struct smc_sock *smc, struct smc_connection *conn = &smc->conn; struct sock *sk = &smc->sk; bool force = false; - int diff, rc = 0; + int rc = 0; + s64 diff; smc_curs_add(conn->rmb_desc->len, &cons, len); /* did we process urgent data? */ if (conn->urg_state == SMC_URG_VALID || conn->urg_rx_skip_pend) { - diff = smc_curs_comp(conn->rmb_desc->len, &cons, - &conn->urg_curs); + diff = smc_curs_sdiff(conn->rmb_desc->len, &cons, + &conn->urg_curs); if (sock_flag(sk, SOCK_URGINLINE)) { if (diff == 0) { force = true; @@ -330,8 +331,8 @@ static int smc_rx_recv_urg(struct smc_sock *smc, struct msghdr *msg, int len, rc = memcpy_to_msg(msg, &conn->urg_rx_byte, 1); len = 1; smc_curs_copy(&cons, &conn->local_tx_ctrl.cons, conn); - if (smc_curs_diff(conn->rmb_desc->len, &cons, - &conn->urg_curs) > 1) + if (smc_curs_sdiff(conn->rmb_desc->len, &cons, + &conn->urg_curs) > 1) conn->urg_rx_skip_pend = true; /* Urgent Byte was already accounted for, but trigger * skipping the urgent byte in non-inline case -- 2.56.1