git: 9b841b0e2390 - main - m_uiotombuf: write total memory length of the allocated chain in pkthdr

From: Gleb Smirnoff <glebius_at_FreeBSD.org>
Date: Fri, 24 Jun 2022 16:10:37 UTC
The branch main has been updated by glebius:

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

commit 9b841b0e239027a4966005cad4d32cbefb52e906
Author:     Gleb Smirnoff <glebius@FreeBSD.org>
AuthorDate: 2022-06-24 16:09:11 +0000
Commit:     Gleb Smirnoff <glebius@FreeBSD.org>
CommitDate: 2022-06-24 16:09:11 +0000

    m_uiotombuf: write total memory length of the allocated chain in pkthdr
    
    Data allocated by m_uiotombuf() usually goes into a socket buffer.
    We are interested in the length of useful data to be added to sb_acc,
    as well as total memory used by mbufs.  The later would be added to
    sb_mbcnt.  Calculating this value at allocation time allows to save
    on extra traversal of the mbuf chain.
    
    Reviewed by:            markj
    Differential revision:  https://reviews.freebsd.org/D35301
---
 sys/kern/uipc_mbuf.c   |  6 +++++-
 sys/kern/uipc_usrreq.c |  2 +-
 sys/sys/mbuf.h         | 18 +++++++++++-------
 3 files changed, 17 insertions(+), 9 deletions(-)

diff --git a/sys/kern/uipc_mbuf.c b/sys/kern/uipc_mbuf.c
index 02a9a120b970..17fa670c9406 100644
--- a/sys/kern/uipc_mbuf.c
+++ b/sys/kern/uipc_mbuf.c
@@ -1899,8 +1899,12 @@ m_uiotombuf(struct uio *uio, int how, int len, int align, int flags)
 
 		mb->m_len = length;
 		progress += length;
-		if (flags & M_PKTHDR)
+		if (flags & M_PKTHDR) {
 			m->m_pkthdr.len += length;
+			m->m_pkthdr.memlen += MSIZE;
+			if (mb->m_flags & M_EXT)
+				m->m_pkthdr.memlen += mb->m_ext.ext_size;
+		}
 	}
 	KASSERT(progress == total, ("%s: progress != total", __func__));
 
diff --git a/sys/kern/uipc_usrreq.c b/sys/kern/uipc_usrreq.c
index 553d0293770c..d1c87865c632 100644
--- a/sys/kern/uipc_usrreq.c
+++ b/sys/kern/uipc_usrreq.c
@@ -1268,7 +1268,7 @@ uipc_sosend_dgram(struct socket *so, struct sockaddr *addr, struct uio *uio,
 	if (cc <= sbspace(sb)) {
 		STAILQ_INSERT_TAIL(&sb->uxdg_mb, f, m_stailqpkt);
 		/* XXX: would be nice if m_uiotombuf() returns count. */
-		for (; f != NULL ; f = f->m_next) {
+		for (; f != NULL; f = f->m_next) {
 			if (f->m_type != MT_DATA)
 				sb->sb_ctl += f->m_len;
 			sb->sb_mbcnt += MSIZE;
diff --git a/sys/sys/mbuf.h b/sys/sys/mbuf.h
index 8f377d580aa4..48ed63ef67b8 100644
--- a/sys/sys/mbuf.h
+++ b/sys/sys/mbuf.h
@@ -207,13 +207,17 @@ struct pkthdr {
 
 	/* Layer specific non-persistent local storage for reassembly, etc. */
 	union {
-		uint8_t  eight[8];
-		uint16_t sixteen[4];
-		uint32_t thirtytwo[2];
-		uint64_t sixtyfour[1];
-		uintptr_t unintptr[1];
-		void 	*ptr;
-	} PH_loc;
+		union {
+			uint8_t  eight[8];
+			uint16_t sixteen[4];
+			uint32_t thirtytwo[2];
+			uint64_t sixtyfour[1];
+			uintptr_t unintptr[1];
+			void 	*ptr;
+		} PH_loc;
+		/* Upon allocation: total packet memory consumption. */
+		u_int	memlen;
+	};
 };
 #define	ether_vtag	PH_per.sixteen[0]
 #define tcp_tun_port	PH_per.sixteen[0] /* outbound */