git: 2ff195d679b9 - main - gve: Propagate m_pullup() mbuf changes in gve_prep_tso()

From: Mark Johnston <markj_at_FreeBSD.org>
Date: Wed, 25 Nov 2026 21:48:51 UTC
The branch main has been updated by markj:

URL: https://cgit.FreeBSD.org/src/commit/?id=2ff195d679b9087d8b27f8545c1f84eb01210344

commit 2ff195d679b9087d8b27f8545c1f84eb01210344
Author:     Tim Hostetler <thostet@google.com>
AuthorDate: 2026-11-25 21:43:55 +0000
Commit:     Mark Johnston <markj@FreeBSD.org>
CommitDate: 2026-11-25 21:44:19 +0000

    gve: Propagate m_pullup() mbuf changes in gve_prep_tso()
    
    gve_prep_tso() uses m_pullup() via PULLUP_HDR() to make protocol headers
    contiguous in the first mbuf, but both gve_prep_tso() and
    gve_tx_fill_ctx_descs() take struct mbuf * by value:
    
    - When m_pullup() allocates a new head mbuf (freeing the old head), the
      new chain is leaked and callers continue reading, copying, and later
      freeing the freed mbuf pointer.
    - When m_pullup() fails, it frees the chain and returns NULL, leaving
      callers with a dangling non-NULL pointer that gve_xmit_br() frees a
      second time on EINVAL.
    - In DQO-RDA mode, gve_xmit_dqo() calls gve_map_mbuf_dqo() before
      gve_prep_tso(), leaving the DMA map pointing at pre-pullup buffers
      without the updated th->th_sum.
    
    Additionally, gve_prep_tso() pulls up sizeof(struct tcphdr *) (8 bytes)
    instead of sizeof(*th) (20 bytes), and gve_xmit_br() falls through to
    BPF_MTAP(ifp, NULL) and a doorbell write when a transmit helper frees
    the chain and sets *mbuf to NULL on error.
    
    Pass struct mbuf ** into gve_prep_tso(), PULLUP_HDR(), and
    gve_xmit_dqo_qpl() (matching gve_xmit_dqo()), and invoke gve_prep_tso()
    before DMA mapping and pending-packet allocation so
    gve_tx_fill_ctx_descs() only writes descriptors. Fix the TCP header
    pullup size to sizeof(*th) followed by *header_len, reject non-IP
    EtherTypes, and handle err != 0 with mbuf == NULL || err == EINVAL in
    gve_xmit_br() before BPF_MTAP().
    
    Tested on a FreeBSD 14.5 N4 instance (DQO QPL) with INVARIANTS and
    MBUF_STRESS_TEST enabled: reproduced the use-after-free panic in
    gve_xmit_dqo_qpl() -> m_copydata() with net.inet.ip.mbuf_frag_size=20 on
    the unpatched driver, and verified single- and multi-stream TSO sweeps
    (mbuf_frag_size = 20, 14, 42, -2, 2048) with tcpdump, DTrace, and
    vmstat -z showing 1-for-1 head-replacement propagation and no panics or
    mbuf leaks.
    
    Signed-off-by: Tim Hostetler <thostet@google.com>
    
    Fixes:  d438b4ef0cfc ("gve: Add DQO RDA support")
    Fixes:  2348ac893d10 ("gve: Add DQO QPL support")
    Reviewed by:    Jasper Tran O'Leary <jtranoleary@google.com>, markj
    MFC after:      2 weeks
    Differential Revision:  https://reviews.freebsd.org/D61966
---
 sys/dev/gve/gve.h        |  2 +-
 sys/dev/gve/gve_tx.c     | 26 ++++++++-------
 sys/dev/gve/gve_tx_dqo.c | 83 +++++++++++++++++++++++++-----------------------
 3 files changed, 60 insertions(+), 51 deletions(-)

diff --git a/sys/dev/gve/gve.h b/sys/dev/gve/gve.h
index 1a163cfcbce0..d11c65f9e37d 100644
--- a/sys/dev/gve/gve.h
+++ b/sys/dev/gve/gve.h
@@ -724,7 +724,7 @@ void gve_clear_tx_ring_dqo(struct gve_priv *priv, int i);
 int gve_check_tx_timeout_dqo(struct gve_priv *priv, struct gve_tx_ring *tx);
 int gve_tx_intr_dqo(void *arg);
 int gve_xmit_dqo(struct gve_tx_ring *tx, struct mbuf **mbuf_ptr);
-int gve_xmit_dqo_qpl(struct gve_tx_ring *tx, struct mbuf *mbuf);
+int gve_xmit_dqo_qpl(struct gve_tx_ring *tx, struct mbuf **mbuf_ptr);
 void gve_tx_cleanup_tq_dqo(void *arg, int pending);
 
 /* RX functions defined in gve_rx.c */
diff --git a/sys/dev/gve/gve_tx.c b/sys/dev/gve/gve_tx.c
index 53451acc1934..2984a4c69e09 100644
--- a/sys/dev/gve/gve_tx.c
+++ b/sys/dev/gve/gve_tx.c
@@ -780,14 +780,15 @@ gve_xmit_mbuf(struct gve_tx_ring *tx,
 	if (gve_is_gqi(tx->com.priv))
 		return (gve_xmit(tx, *mbuf));
 
-	if (gve_is_qpl(tx->com.priv))
-		return (gve_xmit_dqo_qpl(tx, *mbuf));
-
 	/*
-	 * gve_xmit_dqo might attempt to defrag the mbuf chain.
-	 * The reference is passed in so that in the case of
-	 * errors, the new mbuf chain is what's put back on the br.
+	 * Both DQO transmit paths may replace or free the mbuf chain
+	 * (via m_pullup in gve_prep_tso, or m_defrag in gve_map_mbuf_dqo).
+	 * Pass the mbuf pointer by reference so callers observe the new
+	 * head (or NULL if freed on error).
 	 */
+	if (gve_is_qpl(tx->com.priv))
+		return (gve_xmit_dqo_qpl(tx, mbuf));
+
 	return (gve_xmit_dqo(tx, mbuf));
 }
 
@@ -847,12 +848,15 @@ gve_xmit_br(struct gve_tx_ring *tx)
 		if (__predict_false(mbuf != NULL && err == ENOBUFS))
 			err = gve_xmit_retry_enobuf_mbuf(tx, &mbuf);
 
-		if (__predict_false(err != 0 && mbuf != NULL)) {
-			if (err == EINVAL) {
+		if (__predict_false(err != 0)) {
+			if (mbuf == NULL || err == EINVAL) {
 				drbr_advance(ifp, tx->br);
-				m_freem(mbuf);
-			} else
-				drbr_putback(ifp, tx->br, mbuf);
+				if (mbuf != NULL)
+					m_freem(mbuf);
+				counter_u64_add(tx->stats.tx_dropped_pkt, 1);
+				continue;
+			}
+			drbr_putback(ifp, tx->br, mbuf);
 			break;
 		}
 
diff --git a/sys/dev/gve/gve_tx_dqo.c b/sys/dev/gve/gve_tx_dqo.c
index cac82a7ca75e..3cbc21430006 100644
--- a/sys/dev/gve/gve_tx_dqo.c
+++ b/sys/dev/gve/gve_tx_dqo.c
@@ -330,25 +330,25 @@ gve_tx_fill_general_ctx_desc(struct gve_tx_general_context_desc_dqo *desc,
 	};
 }
 
-#define PULLUP_HDR(m, len)				\
+#define PULLUP_HDR(mp, len)				\
 do {							\
-	if (__predict_false((m)->m_len < (len))) {	\
-		(m) = m_pullup((m), (len));		\
-		if ((m) == NULL)			\
+	if (__predict_false((*(mp))->m_len < (len))) {	\
+		*(mp) = m_pullup(*(mp), (len));		\
+		if (*(mp) == NULL)			\
 			return (EINVAL);		\
 	}						\
 } while (0)
 
 static int
-gve_prep_tso(struct mbuf *mbuf, int *header_len)
+gve_prep_tso(struct mbuf **mbuf_ptr, int *header_len)
 {
 	uint8_t l3_off, l4_off = 0;
 	struct ether_header *eh;
 	struct tcphdr *th;
 	u_short csum;
 
-	PULLUP_HDR(mbuf, sizeof(*eh));
-	eh = mtod(mbuf, struct ether_header *);
+	PULLUP_HDR(mbuf_ptr, sizeof(*eh));
+	eh = mtod(*mbuf_ptr, struct ether_header *);
 	KASSERT(eh->ether_type != ETHERTYPE_VLAN,
 	    ("VLAN-tagged packets not supported"));
 	l3_off = ETHER_HDR_LEN;
@@ -357,8 +357,8 @@ gve_prep_tso(struct mbuf *mbuf, int *header_len)
 	if (ntohs(eh->ether_type) == ETHERTYPE_IPV6) {
 		struct ip6_hdr *ip6;
 
-		PULLUP_HDR(mbuf, l3_off + sizeof(*ip6));
-		ip6 = (struct ip6_hdr *)(mtodo(mbuf, l3_off));
+		PULLUP_HDR(mbuf_ptr, l3_off + sizeof(*ip6));
+		ip6 = (struct ip6_hdr *)(mtodo(*mbuf_ptr, l3_off));
 		l4_off = l3_off + sizeof(struct ip6_hdr);
 		csum = in6_cksum_pseudo(ip6, /*len=*/0, IPPROTO_TCP,
 		    /*csum=*/0);
@@ -367,16 +367,20 @@ gve_prep_tso(struct mbuf *mbuf, int *header_len)
 	if (ntohs(eh->ether_type) == ETHERTYPE_IP) {
 		struct ip *ip;
 
-		PULLUP_HDR(mbuf, l3_off + sizeof(*ip));
-		ip = (struct ip *)(mtodo(mbuf, l3_off));
+		PULLUP_HDR(mbuf_ptr, l3_off + sizeof(*ip));
+		ip = (struct ip *)(mtodo(*mbuf_ptr, l3_off));
 		l4_off = l3_off + (ip->ip_hl << 2);
 		csum = in_pseudo(ip->ip_src.s_addr, ip->ip_dst.s_addr,
 		    htons(IPPROTO_TCP));
+	} else {
+		return (EINVAL);
 	}
 
-	PULLUP_HDR(mbuf, l4_off + sizeof(struct tcphdr *));
-	th = (struct tcphdr *)(mtodo(mbuf, l4_off));
+	PULLUP_HDR(mbuf_ptr, l4_off + sizeof(*th));
+	th = (struct tcphdr *)(mtodo(*mbuf_ptr, l4_off));
 	*header_len = l4_off + (th->th_off << 2);
+	PULLUP_HDR(mbuf_ptr, *header_len);
+	th = (struct tcphdr *)(mtodo(*mbuf_ptr, l4_off));
 
 	/*
 	 * Hardware requires the th->th_sum to not include the TCP payload,
@@ -387,29 +391,18 @@ gve_prep_tso(struct mbuf *mbuf, int *header_len)
 	return (0);
 }
 
-static int
-gve_tx_fill_ctx_descs(struct gve_tx_ring *tx, struct mbuf *mbuf,
-    bool is_tso, uint32_t *desc_idx)
+static void
+gve_tx_fill_ctx_descs(struct gve_tx_ring *tx, const struct mbuf *mbuf,
+    bool is_tso, int header_len, uint32_t *desc_idx)
 {
 	struct gve_tx_general_context_desc_dqo *gen_desc;
 	struct gve_tx_tso_context_desc_dqo *tso_desc;
 	struct gve_tx_metadata_dqo metadata;
-	int header_len;
-	int err;
 
 	metadata = (struct gve_tx_metadata_dqo){0};
 	gve_extract_tx_metadata_dqo(mbuf, &metadata);
 
 	if (is_tso) {
-		err = gve_prep_tso(mbuf, &header_len);
-		if (__predict_false(err)) {
-			counter_enter();
-			counter_u64_add_protected(
-			    tx->stats.tx_delayed_pkt_tsoerr, 1);
-			counter_exit();
-			return (err);
-		}
-
 		tso_desc = &tx->dqo.desc_ring[*desc_idx].tso_ctx;
 		gve_tx_fill_tso_ctx_desc(tso_desc, mbuf, &metadata, header_len);
 
@@ -422,7 +415,6 @@ gve_tx_fill_ctx_descs(struct gve_tx_ring *tx, struct mbuf *mbuf,
 	gen_desc = &tx->dqo.desc_ring[*desc_idx].general_ctx;
 	gve_tx_fill_general_ctx_desc(gen_desc, &metadata);
 	*desc_idx = (*desc_idx + 1) & tx->dqo.desc_mask;
-	return (0);
 }
 
 static int
@@ -725,12 +717,14 @@ gve_tx_copy_mbuf_and_write_pkt_descs(struct gve_tx_ring *tx,
 }
 
 int
-gve_xmit_dqo_qpl(struct gve_tx_ring *tx, struct mbuf *mbuf)
+gve_xmit_dqo_qpl(struct gve_tx_ring *tx, struct mbuf **mbuf_ptr)
 {
 	uint32_t desc_idx = tx->dqo.desc_tail;
 	struct gve_tx_pending_pkt_dqo *pkt;
+	struct mbuf *mbuf = *mbuf_ptr;
 	int total_descs_needed;
 	int16_t completion_tag;
+	int header_len = 0;
 	bool has_csum_flag;
 	int csum_flags;
 	bool is_tso;
@@ -758,6 +752,15 @@ gve_xmit_dqo_qpl(struct gve_tx_ring *tx, struct mbuf *mbuf)
 		return (ENOBUFS);
 	}
 
+	if (is_tso) {
+		err = gve_prep_tso(mbuf_ptr, &header_len);
+		if (__predict_false(err != 0)) {
+			counter_u64_add(tx->stats.tx_delayed_pkt_tsoerr, 1);
+			return (err);
+		}
+		mbuf = *mbuf_ptr;
+	}
+
 	pkt = gve_alloc_pending_packet(tx);
 	if (pkt == NULL) {
 		counter_enter();
@@ -768,9 +771,7 @@ gve_xmit_dqo_qpl(struct gve_tx_ring *tx, struct mbuf *mbuf)
 	}
 	completion_tag = pkt - tx->dqo.pending_pkts;
 
-	err = gve_tx_fill_ctx_descs(tx, mbuf, is_tso, &desc_idx);
-	if (err)
-		goto abort;
+	gve_tx_fill_ctx_descs(tx, mbuf, is_tso, header_len, &desc_idx);
 
 	pkt->mbuf = mbuf;
 	pkt->state = GVE_PACKET_STATE_PENDING_DATA_COMPL;
@@ -790,10 +791,6 @@ gve_xmit_dqo_qpl(struct gve_tx_ring *tx, struct mbuf *mbuf)
 
 	tx->req += total_descs_needed; /* tx->req is just a sysctl counter */
 	return (0);
-
-abort:
-	gve_free_pending_packet(tx, pkt);
-	return (err);
 }
 
 int
@@ -805,6 +802,7 @@ gve_xmit_dqo(struct gve_tx_ring *tx, struct mbuf **mbuf_ptr)
 	struct mbuf *mbuf = *mbuf_ptr;
 	int total_descs_needed;
 	int16_t completion_tag;
+	int header_len = 0;
 	bool has_csum_flag;
 	int csum_flags;
 	bool is_tso;
@@ -829,6 +827,15 @@ gve_xmit_dqo(struct gve_tx_ring *tx, struct mbuf **mbuf_ptr)
 	if (__predict_false(!gve_tx_has_desc_room_dqo(tx, total_descs_needed)))
 		return (ENOBUFS);
 
+	if (is_tso) {
+		err = gve_prep_tso(mbuf_ptr, &header_len);
+		if (__predict_false(err != 0)) {
+			counter_u64_add(tx->stats.tx_delayed_pkt_tsoerr, 1);
+			return (err);
+		}
+		mbuf = *mbuf_ptr;
+	}
+
 	pkt = gve_alloc_pending_packet(tx);
 	if (pkt == NULL) {
 		counter_enter();
@@ -854,9 +861,7 @@ gve_xmit_dqo(struct gve_tx_ring *tx, struct mbuf **mbuf_ptr)
 		goto abort_with_dma;
 	}
 
-	err = gve_tx_fill_ctx_descs(tx, mbuf, is_tso, &desc_idx);
-	if (err)
-		goto abort_with_dma;
+	gve_tx_fill_ctx_descs(tx, mbuf, is_tso, header_len, &desc_idx);
 
 	pkt->mbuf = mbuf;
 	pkt->state = GVE_PACKET_STATE_PENDING_DATA_COMPL;