From: Bryam Vargas <hexl…@proton.me> smc_cdc_msg_recv_action() sets a new urgent mark, byte and state under the socket spinlock, even while a reader owns the socket. The reader tests and changes urg_state under the socket lock only. In smc_rx_recv_urg(), a second urgent byte that arrives after the state was set to SMC_URG_READ is returned in place of the first, and the next recv(MSG_OOB) returns it again. In smc_rx_update_consumer(), a mark set after the test is overwritten with SMC_URG_READ: its byte is neither returned nor skipped, and the sender, which waits for its urgent data to be consumed, stalls. Take the socket spinlock around both test-and-set sequences. smc_rx_update_consumer() takes it only after a test without the lock finds a mark or a pending skip, so a read with no urgent data is unchanged. The lines in smc_rx_recv_urg() after its SMC_URG_VALID block could only be reached through this race; drop them. SIOCATMARK copies urg_curs and then tests urg_state: take the spinlock there as well, so that a mark set between the two is not paired with the old cursor. The first test in smc_rx_update_consumer() stays without the lock. Mark it, and the other reads of urg_state outside the spinlock, with READ_ONCE(); the writes that can race with them use WRITE_ONCE(). Fixes: de8474eb9d50 ("net/smc: urgent data support") Closes: https://lore.kernel.org/all/sash…@kernel.org/ Cc: stab…@vger.kernel.org Signed-off-by: Bryam Vargas <hexl…@proton.me> --- net/smc/af_smc.c | 10 ++++- net/smc/smc_cdc.c | 4 +- net/smc/smc_rx.c | 111 +++++++++++++++++++++++++++++++++--------------------- 3 files changed, 79 insertions(+), 46 deletions(-) diff --git a/net/smc/af_smc.c b/net/smc/af_smc.c index 3877a3a4cbae..2c4824c6f245 100644 --- a/net/smc/af_smc.c +++ b/net/smc/af_smc.c @@ -2946,7 +2946,7 @@ __poll_t smc_poll(struct file *file, struct socket *sock, mask |= EPOLLIN | EPOLLRDNORM | EPOLLRDHUP; if (sk->sk_state == SMC_APPCLOSEWAIT1) mask |= EPOLLIN; - if (smc->conn.urg_state == SMC_URG_VALID) + if (READ_ONCE(smc->conn.urg_state) == SMC_URG_VALID) mask |= EPOLLPRI; } } @@ -3275,12 +3275,20 @@ int smc_ioctl(struct socket *sock, unsigned int cmd, smc->sk.sk_state == SMC_CLOSED) { answ = 0; } else { + /* smc_cdc_msg_recv_action() sets a new mark under + * the socket spinlock, even while we own the socket: + * read the mark together with its state. + */ + local_bh_disable(); + bh_lock_sock(&smc->sk); smc_curs_copy(&cons, &conn->local_tx_ctrl.cons, conn); smc_curs_copy(&urg, &conn->urg_curs, conn); answ = (conn->urg_state == SMC_URG_VALID || conn->urg_rx_skip_pend) && smc_curs_sdiff(conn->rmb_desc->len, &cons, &urg) == 1; + bh_unlock_sock(&smc->sk); + local_bh_enable(); } break; default: diff --git a/net/smc/smc_cdc.c b/net/smc/smc_cdc.c index 91e1636021b7..6eccb5c879c3 100644 --- a/net/smc/smc_cdc.c +++ b/net/smc/smc_cdc.c @@ -302,7 +302,7 @@ static void smc_cdc_handle_urg_data_arrival(struct smc_sock *smc, /* new data included urgent business */ smc_curs_copy(&conn->urg_curs, &conn->local_rx_ctrl.prod, conn); - conn->urg_state = SMC_URG_VALID; + WRITE_ONCE(conn->urg_state, SMC_URG_VALID); if (!sock_flag(&smc->sk, SOCK_URGINLINE)) /* we'll skip the urgent byte, so don't account for it */ (*diff_prod)--; @@ -397,7 +397,7 @@ static void smc_cdc_msg_recv_action(struct smc_sock *smc, if (conn->local_rx_ctrl.prod_flags.write_blocked) smc->sk.sk_data_ready(&smc->sk); if (conn->local_rx_ctrl.prod_flags.urg_data_pending) - conn->urg_state = SMC_URG_NOTYET; + WRITE_ONCE(conn->urg_state, SMC_URG_NOTYET); } /* trigger sndbuf consumer: RDMA write into peer RMBE and CDC */ diff --git a/net/smc/smc_rx.c b/net/smc/smc_rx.c index 564b976426ca..fd58d4cb6594 100644 --- a/net/smc/smc_rx.c +++ b/net/smc/smc_rx.c @@ -67,26 +67,41 @@ static int smc_rx_update_consumer(struct smc_sock *smc, 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_sdiff(conn->rmb_desc->len, &cons, - &conn->urg_curs); - if (sock_flag(sk, SOCK_URGINLINE)) { - if (diff == 0) { - force = true; - rc = 1; - conn->urg_state = SMC_URG_READ; + /* did we process urgent data? smc_cdc_msg_recv_action() sets a new + * mark under the socket spinlock, even while we own the socket: + * test again under the spinlock. A mark set after the first test is + * left to the next call, like one set after the unlock. + */ + if (READ_ONCE(conn->urg_state) == SMC_URG_VALID || + conn->urg_rx_skip_pend) { + local_bh_disable(); + bh_lock_sock(sk); + if (conn->urg_state == SMC_URG_VALID || + conn->urg_rx_skip_pend) { + diff = smc_curs_sdiff(conn->rmb_desc->len, &cons, + &conn->urg_curs); + if (sock_flag(sk, SOCK_URGINLINE)) { + if (diff == 0) { + force = true; + rc = 1; + WRITE_ONCE(conn->urg_state, + SMC_URG_READ); + } + } else { + if (diff == 1) { + /* skip urgent byte */ + force = true; + smc_curs_add(conn->rmb_desc->len, + &cons, 1); + conn->urg_rx_skip_pend = false; + } else if (diff < -1) + /* we read past urgent byte */ + WRITE_ONCE(conn->urg_state, + SMC_URG_READ); } - } else { - if (diff == 1) { - /* skip urgent byte */ - force = true; - smc_curs_add(conn->rmb_desc->len, &cons, 1); - conn->urg_rx_skip_pend = false; - } else if (diff < -1) - /* we read past urgent byte */ - conn->urg_state = SMC_URG_READ; } + bh_unlock_sock(sk); + local_bh_enable(); } smc_curs_copy(&conn->local_tx_ctrl.cons, &cons, conn); @@ -314,42 +329,52 @@ static int smc_rx_recv_urg(struct smc_sock *smc, struct msghdr *msg, int len, struct smc_connection *conn = &smc->conn; union smc_host_cursor cons; struct sock *sk = &smc->sk; + bool valid; + char byte; int rc = 0; - if (sock_flag(sk, SOCK_URGINLINE) || - !(conn->urg_state == SMC_URG_VALID) || - conn->urg_state == SMC_URG_READ) + if (sock_flag(sk, SOCK_URGINLINE)) return -EINVAL; - SMC_STAT_INC(smc, urg_data_cnt); - if (conn->urg_state == SMC_URG_VALID) { - if (!(flags & MSG_PEEK)) - smc->conn.urg_state = SMC_URG_READ; - msg->msg_flags |= MSG_OOB; + /* smc_cdc_msg_recv_action() sets a new mark and byte under the + * socket spinlock, even while we own the socket: take the byte + * together with the state it belongs to. + */ + local_bh_disable(); + bh_lock_sock(sk); + valid = conn->urg_state == SMC_URG_VALID; + if (valid) { + byte = conn->urg_rx_byte; if (len > 0) { - if (!(flags & MSG_TRUNC)) - 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_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 - */ - if (!(flags & MSG_PEEK)) - smc_rx_update_consumer(smc, cons, 0); - } else { - msg->msg_flags |= MSG_TRUNC; } - - return rc ? -EFAULT : len; + if (!(flags & MSG_PEEK)) + WRITE_ONCE(conn->urg_state, SMC_URG_READ); } + bh_unlock_sock(sk); + local_bh_enable(); + if (!valid) + return -EINVAL; - if (sk->sk_state == SMC_CLOSED || sk->sk_shutdown & RCV_SHUTDOWN) - return 0; + SMC_STAT_INC(smc, urg_data_cnt); + msg->msg_flags |= MSG_OOB; + if (len > 0) { + if (!(flags & MSG_TRUNC)) + rc = memcpy_to_msg(msg, &byte, 1); + len = 1; + /* Urgent Byte was already accounted for, but trigger + * skipping the urgent byte in non-inline case + */ + if (!(flags & MSG_PEEK)) + smc_rx_update_consumer(smc, cons, 0); + } else { + msg->msg_flags |= MSG_TRUNC; + } - return -EAGAIN; + return rc ? -EFAULT : len; } static bool smc_rx_recvmsg_data_available(struct smc_sock *smc, size_t peeked) @@ -358,7 +383,7 @@ static bool smc_rx_recvmsg_data_available(struct smc_sock *smc, size_t peeked) if (smc_rx_data_available(conn, peeked)) return true; - else if (conn->urg_state == SMC_URG_VALID) + else if (READ_ONCE(conn->urg_state) == SMC_URG_VALID) /* we received a single urgent Byte - skip */ smc_rx_update_cons(smc, 0); return false; @@ -496,7 +521,7 @@ int smc_rx_recvmsg(struct smc_sock *smc, struct msghdr *msg, /* subsequent splice() calls pick up where previous left */ if (splbytes) smc_curs_add(conn->rmb_desc->len, &cons, splbytes); - if (conn->urg_state == SMC_URG_VALID && + if (READ_ONCE(conn->urg_state) == SMC_URG_VALID && sock_flag(&smc->sk, SOCK_URGINLINE) && readable > 1) readable--; /* always stop at urgent Byte */
-- 2.56.1