git: a3f7b81fdd22 - stable/14 - mbuf: provide m_freemp()

From: Gleb Smirnoff <glebius_at_FreeBSD.org>
Date: Tue, 11 Jun 2024 01:33:50 UTC
The branch stable/14 has been updated by glebius:

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

commit a3f7b81fdd2205207085ba5d038b402aba748c6e
Author:     Gleb Smirnoff <glebius@FreeBSD.org>
AuthorDate: 2024-06-11 01:31:02 +0000
Commit:     Gleb Smirnoff <glebius@FreeBSD.org>
CommitDate: 2024-06-11 01:31:02 +0000

    mbuf: provide m_freemp()
    
    This function follows both m_nextpkt and m_next linkage freeing all mbufs.
    Note that existing m_freem() follows only m_next.
    
    Reviewed by:            khng
    Differential Revision:  https://reviews.freebsd.org/D45477
    
    (cherry picked from commit badf44cc21249a56e7d822a77acfdd21f8352d50)
---
 sys/kern/kern_mbuf.c | 19 +++++++++++++++++++
 sys/kern/uipc_mbuf.c |  3 +++
 sys/sys/mbuf.h       |  2 ++
 3 files changed, 24 insertions(+)

diff --git a/sys/kern/kern_mbuf.c b/sys/kern/kern_mbuf.c
index 0897ac4cc2db..002e508e91cc 100644
--- a/sys/kern/kern_mbuf.c
+++ b/sys/kern/kern_mbuf.c
@@ -1569,6 +1569,25 @@ m_freem(struct mbuf *mb)
 		mb = m_free(mb);
 }
 
+/*
+ * Free an entire chain of mbufs and associated external buffers, following
+ * both m_next and m_nextpkt linkage.
+ * Note: doesn't support NULL argument.
+ */
+void
+m_freemp(struct mbuf *m)
+{
+	struct mbuf *n;
+
+	MBUF_PROBE1(m__freemp, m);
+	do {
+		n = m->m_nextpkt;
+		while (m != NULL)
+			m = m_free(m);
+		m = n;
+	} while (m != NULL);
+}
+
 /*
  * Temporary primitive to allow freeing without going through m_free.
  */
diff --git a/sys/kern/uipc_mbuf.c b/sys/kern/uipc_mbuf.c
index f1e986eaa348..b10e524e6e4b 100644
--- a/sys/kern/uipc_mbuf.c
+++ b/sys/kern/uipc_mbuf.c
@@ -113,6 +113,9 @@ SDT_PROBE_DEFINE1_XLATE(sdt, , , m__free,
 SDT_PROBE_DEFINE1_XLATE(sdt, , , m__freem,
     "struct mbuf *", "mbufinfo_t *");
 
+SDT_PROBE_DEFINE1_XLATE(sdt, , , m__freemp,
+    "struct mbuf *", "mbufinfo_t *");
+
 #include <security/mac/mac_framework.h>
 
 /*
diff --git a/sys/sys/mbuf.h b/sys/sys/mbuf.h
index 757eba3165f9..f7925da795cd 100644
--- a/sys/sys/mbuf.h
+++ b/sys/sys/mbuf.h
@@ -67,6 +67,7 @@ SDT_PROBE_DECLARE(sdt, , , m__cljget);
 SDT_PROBE_DECLARE(sdt, , , m__cljset);
 SDT_PROBE_DECLARE(sdt, , , m__free);
 SDT_PROBE_DECLARE(sdt, , , m__freem);
+SDT_PROBE_DECLARE(sdt, , , m__freemp);
 
 #endif /* _KERNEL */
 
@@ -844,6 +845,7 @@ void		 m_extadd(struct mbuf *, char *, u_int, m_ext_free_t,
 u_int		 m_fixhdr(struct mbuf *);
 struct mbuf	*m_fragment(struct mbuf *, int, int);
 void		 m_freem(struct mbuf *);
+void		 m_freemp(struct mbuf *);
 void		 m_free_raw(struct mbuf *);
 struct mbuf	*m_get2(int, int, short, int);
 struct mbuf	*m_get3(int, int, short, int);