git: 14ee31bcccfb - stable/13 - pseudofs: Microoptimize struct pfs_node

From: Dmitry Chagin <dchagin_at_FreeBSD.org>
Date: Thu, 29 Jun 2023 08:19:58 UTC
The branch stable/13 has been updated by dchagin:

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

commit 14ee31bcccfbd6ef14548f0129fcaa5a113e4c82
Author:     Dmitry Chagin <dchagin@FreeBSD.org>
AuthorDate: 2023-04-02 08:20:07 +0000
Commit:     Dmitry Chagin <dchagin@FreeBSD.org>
CommitDate: 2023-06-29 08:15:17 +0000

    pseudofs: Microoptimize struct pfs_node
    
    Since 81167243b the size of struct pfs_node is 280 bytes, so the kernel
    memory allocator takes memory from 384 bytes sized bucket. However, the
    length of the node name is mostly short, e.g., for Linux emulation layer
    it is up to 16 bytes. The size of struct pfs_node w/o pfs_name is 152
    bytes, i.e., we have 104 bytes left to fit the node name into the 256
    bytes-sized bucket.
    
    Reviewed by:            des
    Differential revision:  https://reviews.freebsd.org/D39381
    MFC after:              1 month
    
    (cherry picked from commit 7f72324346ea154a92671fc913dbe82c0287501a)
---
 sys/fs/pseudofs/pseudofs.c | 8 +++++---
 sys/fs/pseudofs/pseudofs.h | 2 +-
 2 files changed, 6 insertions(+), 4 deletions(-)

diff --git a/sys/fs/pseudofs/pseudofs.c b/sys/fs/pseudofs/pseudofs.c
index 3c82021a8148..15a714e23bc6 100644
--- a/sys/fs/pseudofs/pseudofs.c
+++ b/sys/fs/pseudofs/pseudofs.c
@@ -72,18 +72,20 @@ pfs_alloc_node_flags(struct pfs_info *pi, const char *name, pfs_type_t type, int
 {
 	struct pfs_node *pn;
 	int malloc_flags;
+	size_t len;
 
-	KASSERT(strlen(name) < PFS_NAMELEN,
+	len = strlen(name);
+	KASSERT(len < PFS_NAMELEN,
 	    ("%s(): node name is too long", __func__));
 	if (flags & PFS_NOWAIT)
 		malloc_flags = M_NOWAIT | M_ZERO;
 	else
 		malloc_flags = M_WAITOK | M_ZERO;
-	pn = malloc(sizeof *pn, M_PFSNODES, malloc_flags);
+	pn = malloc(sizeof(*pn) + len + 1, M_PFSNODES, malloc_flags);
 	if (pn == NULL)
 		return (NULL);
 	mtx_init(&pn->pn_mutex, "pfs_node", NULL, MTX_DEF | MTX_DUPOK);
-	strlcpy(pn->pn_name, name, sizeof pn->pn_name);
+	memcpy(pn->pn_name, name, len);
 	pn->pn_type = type;
 	pn->pn_info = pi;
 	return (pn);
diff --git a/sys/fs/pseudofs/pseudofs.h b/sys/fs/pseudofs/pseudofs.h
index 158f74575e33..e355e5f5609f 100644
--- a/sys/fs/pseudofs/pseudofs.h
+++ b/sys/fs/pseudofs/pseudofs.h
@@ -219,7 +219,6 @@ struct pfs_info {
  * is not enforcable by WITNESS.
  */
 struct pfs_node {
-	char			 pn_name[PFS_NAMELEN];
 	pfs_type_t		 pn_type;
 	int			 pn_flags;
 	struct mtx		 pn_mutex;
@@ -240,6 +239,7 @@ struct pfs_node {
 	struct pfs_node		*pn_nodes;		/* (o) */
 	struct pfs_node		*pn_last_node;		/* (o) */
 	struct pfs_node		*pn_next;		/* (p) */
+	char			 pn_name[];		/* Keep it last */
 };
 
 /*