git: 9978c6289d62 - main - cxgbe tom: Handle a race condition when enabling TLS offload

From: John Baldwin <jhb_at_FreeBSD.org>
Date: Wed, 20 Mar 2024 22:34:40 UTC
The branch main has been updated by jhb:

URL: https://cgit.FreeBSD.org/src/commit/?id=9978c6289d621ac9edc95acb4e0f527a62a49b03

commit 9978c6289d621ac9edc95acb4e0f527a62a49b03
Author:     John Baldwin <jhb@FreeBSD.org>
AuthorDate: 2024-03-20 22:30:09 +0000
Commit:     John Baldwin <jhb@FreeBSD.org>
CommitDate: 2024-03-20 22:30:09 +0000

    cxgbe tom: Handle a race condition when enabling TLS offload
    
    Use a separate state for when a request to set RX_QUIESCE has been
    sent but the resulting TCB reply has not been received.  In
    particular, this correctly handles the case where data has been
    received and queued in the receive queue before the quiesce request
    takes effect.
    
    Reviewed by:    np
    Sponsored by:   Chelsio Communications
    Differential Revision:  https://reviews.freebsd.org/D44435
---
 sys/dev/cxgbe/tom/t4_tls.c | 12 ++++++++++--
 sys/dev/cxgbe/tom/t4_tom.h |  5 +++--
 2 files changed, 13 insertions(+), 4 deletions(-)

diff --git a/sys/dev/cxgbe/tom/t4_tls.c b/sys/dev/cxgbe/tom/t4_tls.c
index 3a0c1a392e6c..bdd03edd3a6f 100644
--- a/sys/dev/cxgbe/tom/t4_tls.c
+++ b/sys/dev/cxgbe/tom/t4_tls.c
@@ -315,7 +315,7 @@ tls_alloc_ktls(struct toepcb *toep, struct ktls_session *tls, int direction)
 		    tls->params.max_frame_len;
 		toep->tls.tx_key_info_size = t4_tls_key_info_size(tls);
 	} else {
-		toep->flags |= TPF_TLS_STARTING | TPF_TLS_RX_QUIESCED;
+		toep->flags |= TPF_TLS_STARTING | TPF_TLS_RX_QUIESCING;
 		toep->tls.rx_version = tls->params.tls_vmajor << 8 |
 		    tls->params.tls_vminor;
 
@@ -1243,6 +1243,10 @@ tls_received_starting_data(struct adapter *sc, struct toepcb *toep,
 {
 	MPASS(toep->flags & TPF_TLS_STARTING);
 
+	/* Data was received before quiescing took effect. */
+	if ((toep->flags & TPF_TLS_RX_QUIESCING) != 0)
+		return;
+
 	/*
 	 * A previous call to tls_check_rx_sockbuf needed more data.
 	 * Now that more data has arrived, quiesce receive again and
@@ -1250,7 +1254,7 @@ tls_received_starting_data(struct adapter *sc, struct toepcb *toep,
 	 */
 	if ((toep->flags & TPF_TLS_RX_QUIESCED) == 0) {
 		CTR(KTR_CXGBE, "%s: tid %d quiescing", __func__, toep->tid);
-		toep->flags |= TPF_TLS_RX_QUIESCED;
+		toep->flags |= TPF_TLS_RX_QUIESCING;
 		t4_set_rx_quiesce(toep);
 		return;
 	}
@@ -1287,6 +1291,10 @@ do_tls_tcb_rpl(struct sge_iq *iq, const struct rss_header *rss, struct mbuf *m)
 		if ((toep->flags & TPF_TLS_STARTING) == 0)
 			panic("%s: connection is not starting TLS RX\n",
 			    __func__);
+		MPASS((toep->flags & TPF_TLS_RX_QUIESCING) != 0);
+
+		toep->flags &= ~TPF_TLS_RX_QUIESCING;
+		toep->flags |= TPF_TLS_RX_QUIESCED;
 
 		so = inp->inp_socket;
 		sb = &so->so_rcv;
diff --git a/sys/dev/cxgbe/tom/t4_tom.h b/sys/dev/cxgbe/tom/t4_tom.h
index be810d6364b1..805d8bc0d644 100644
--- a/sys/dev/cxgbe/tom/t4_tom.h
+++ b/sys/dev/cxgbe/tom/t4_tom.h
@@ -73,8 +73,9 @@ enum {
 	TPF_KTLS           = (1 << 11), /* send TLS records from KTLS */
 	TPF_INITIALIZED    = (1 << 12), /* init_toepcb has been called */
 	TPF_TLS_RECEIVE	   = (1 << 13), /* should receive TLS records */
-	TPF_TLS_RX_QUIESCED = (1 << 14), /* RX quiesced for TLS RX startup */
-	TPF_WAITING_FOR_FINAL = (1<< 15), /* waiting for wakeup on final CPL */
+	TPF_TLS_RX_QUIESCING = (1 << 14), /* RX quiesced for TLS RX startup */
+	TPF_TLS_RX_QUIESCED = (1 << 15), /* RX quiesced for TLS RX startup */
+	TPF_WAITING_FOR_FINAL = (1<< 16), /* waiting for wakeup on final CPL */
 };
 
 enum {