git: 44df82b11c53 - main - iflib: simplify TXQ_AVAIL

From: Andrew Gallatin <gallatin_at_FreeBSD.org>
Date: Tue, 10 Nov 2026 15:51:46 UTC
The branch main has been updated by gallatin:

URL: https://cgit.FreeBSD.org/src/commit/?id=44df82b11c535ae50b1e5888a361a2bd15d4182b

commit 44df82b11c535ae50b1e5888a361a2bd15d4182b
Author:     Andrew Gallatin <gallatin@FreeBSD.org>
AuthorDate: 2026-11-10 15:37:00 +0000
Commit:     Andrew Gallatin <gallatin@FreeBSD.org>
CommitDate: 2026-11-10 15:37:00 +0000

    iflib: simplify TXQ_AVAIL
    
    Simply the TXQ_AVAIL() macro to avoid using
    get_inuse(txq->ift_size, txq->ift_cidx, txq->ift_pidx, txq->ift_gen)
    and instead define it in terms of txq->ift_in_use.
    
    Note that in_use is equivalent, because it is updated at the same time
    as cidx/pidx and gen, and under the same serialization. The advantage
    is that its cheaper due to using one read and no branches, and
    safer, since unlocked reads get an atomic snapshot of availability,
    but an unlocked read of multiple counters can produce inconsistent
    results.
    
    Reviewed by: kbowling, kgalazka
    Sponsored by: Netflix
    Differential Revision: https://reviews.freebsd.org/D61279
---
 sys/net/iflib.c | 17 +++++++----------
 1 file changed, 7 insertions(+), 10 deletions(-)

diff --git a/sys/net/iflib.c b/sys/net/iflib.c
index 8ab5ecb32b4e..7f1e16b183ac 100644
--- a/sys/net/iflib.c
+++ b/sys/net/iflib.c
@@ -409,7 +409,7 @@ struct iflib_txq {
 	qidx_t		ift_cidx;
 	qidx_t		ift_cidx_processed;
 	qidx_t		ift_pidx;
-	uint8_t		ift_gen;
+	uint8_t		ift_spare0;
 	uint8_t		ift_br_offset:1,
 			ift_defer_mfree:1,
 			ift_sparse_check:1,
@@ -475,7 +475,7 @@ struct iflib_txq {
 	uint16_t	ift_wdog_armed;
 	uint8_t		ift_closed;
 	uint8_t		ift_update_freq;
-	uint8_t		ift_spare0[2];	/* pad to the next pointer boundary */
+	uint8_t		ift_spare1[2];	/* pad to the next pointer boundary */
 	struct iflib_filter_info ift_filter_info;
 	bus_dma_tag_t	ift_buf_tag;
 	iflib_dma_info_t	ift_ifdi;
@@ -516,6 +516,7 @@ struct iflib_fl {
 	qidx_t		ifl_rxd_idxs[IFLIB_MAX_RX_REFRESH];
 }  __aligned(CACHE_LINE_SIZE);
 
+#ifdef INVARIANTS
 static inline qidx_t
 get_inuse(int size, qidx_t cidx, qidx_t pidx, uint8_t gen)
 {
@@ -534,9 +535,9 @@ get_inuse(int size, qidx_t cidx, qidx_t pidx, uint8_t gen)
 
 	return (used);
 }
+#endif
 
-#define TXQ_AVAIL(txq) ((txq->ift_size - txq->ift_pad) -\
-	    get_inuse(txq->ift_size, txq->ift_cidx, txq->ift_pidx, txq->ift_gen))
+#define TXQ_AVAIL(txq) (txq->ift_size - txq->ift_pad - txq->ift_in_use)
 
 #define	MAX_TX_DESC(ctx) MAX((ctx)->ifc_softc_ctx.isc_tx_tso_segments_max, \
     (ctx)->ifc_softc_ctx.isc_tx_nsegments)
@@ -2915,7 +2916,7 @@ iflib_stop(if_ctx_t ctx)
 		txq->ift_processed_prev = 0;
 		txq->ift_outstanding_prev = 0;
 		txq->ift_wdog_armed = 0;
-		txq->ift_in_use = txq->ift_gen = txq->ift_no_desc_avail = 0;
+		txq->ift_in_use = txq->ift_no_desc_avail = 0;
 		txq->ift_npending = txq->ift_db_pending = 0;
 		txq->ift_rs_pending = 0;
 		if (sctx->isc_flags & IFLIB_PRESERVE_TX_INDICES)
@@ -3959,7 +3960,6 @@ defrag:
 		ndesc = pi.ipi_new_pidx - pi.ipi_pidx;
 		if (pi.ipi_new_pidx < pi.ipi_pidx) {
 			ndesc += txq->ift_size;
-			txq->ift_gen = 1;
 		}
 
 		if (pi.ipi_flags & IPI_TX_INTR)
@@ -4031,11 +4031,10 @@ out_with_error:
 static void
 iflib_tx_desc_free(iflib_txq_t txq, int n, struct mbuf **m_defer)
 {
-	uint32_t qsize, cidx, gen;
+	uint32_t qsize, cidx;
 	struct mbuf *m, **ifsd_m;
 
 	cidx = txq->ift_cidx;
-	gen = txq->ift_gen;
 	qsize = txq->ift_size;
 	ifsd_m =txq->ift_sds.ifsd_m;
 
@@ -4061,11 +4060,9 @@ iflib_tx_desc_free(iflib_txq_t txq, int n, struct mbuf **m_defer)
 		}
 		if (__predict_false(++cidx == qsize)) {
 			cidx = 0;
-			gen = 0;
 		}
 	}
 	txq->ift_cidx = cidx;
-	txq->ift_gen = gen;
 }
 
 static __inline int