git: 6ed68e6f5d47 - main - vm_map: overlap system map mutex and user man sx

From: Konstantin Belousov <kib_at_FreeBSD.org>
Date: Mon, 09 Dec 2024 03:28:33 UTC
The branch main has been updated by kib:

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

commit 6ed68e6f5d47ca30716d4e06c1452e68530970db
Author:     Konstantin Belousov <kib@FreeBSD.org>
AuthorDate: 2024-12-05 22:44:08 +0000
Commit:     Konstantin Belousov <kib@FreeBSD.org>
CommitDate: 2024-12-09 03:27:44 +0000

    vm_map: overlap system map mutex and user man sx
    
    This saves 616-584 = 32 bytes per struct vmspace on amd64, which allows
    to pack 7 vmspaces per page vs. 6 for non-overlapping layout.
    
    I used anonymous union member feature to avoid too much churn.
    
    Reviewed by:    alc, markj
    Sponsored by:   The FreeBSD Foundation
    MFC after:      1 week
    Differential revision:  https://reviews.freebsd.org/D47934
---
 sys/vm/vm_kern.c |  3 +--
 sys/vm/vm_map.c  | 18 +++++++++++-------
 sys/vm/vm_map.h  |  7 +++++--
 3 files changed, 17 insertions(+), 11 deletions(-)

diff --git a/sys/vm/vm_kern.c b/sys/vm/vm_kern.c
index a748a6c57d65..29a64a0fe6d8 100644
--- a/sys/vm/vm_kern.c
+++ b/sys/vm/vm_kern.c
@@ -830,8 +830,7 @@ kmem_init(vm_offset_t start, vm_offset_t end)
 	vm_size_t quantum;
 	int domain;
 
-	vm_map_init(kernel_map, kernel_pmap, VM_MIN_KERNEL_ADDRESS, end);
-	kernel_map->system_map = 1;
+	vm_map_init_system(kernel_map, kernel_pmap, VM_MIN_KERNEL_ADDRESS, end);
 	vm_map_lock(kernel_map);
 	/* N.B.: cannot use kgdb to debug, starting with this assignment ... */
 	(void)vm_map_insert(kernel_map, NULL, 0,
diff --git a/sys/vm/vm_map.c b/sys/vm/vm_map.c
index 910ebfb6dc4e..c3aa499e0dc7 100644
--- a/sys/vm/vm_map.c
+++ b/sys/vm/vm_map.c
@@ -292,9 +292,7 @@ vmspace_zinit(void *mem, int size, int flags)
 	vm = (struct vmspace *)mem;
 	map = &vm->vm_map;
 
-	memset(map, 0, sizeof(*map));
-	mtx_init(&map->system_mtx, "vm map (system)", NULL,
-	    MTX_DEF | MTX_DUPOK);
+	memset(map, 0, sizeof(*map));	/* set map->system_map = false */
 	sx_init(&map->lock, "vm map (user)");
 	PMAP_LOCK_INIT(vmspace_pmap(vm));
 	return (0);
@@ -890,7 +888,6 @@ _vm_map_init(vm_map_t map, pmap_t pmap, vm_offset_t min, vm_offset_t max)
 
 	map->header.eflags = MAP_ENTRY_HEADER;
 	map->needs_wakeup = FALSE;
-	map->system_map = 0;
 	map->pmap = pmap;
 	map->header.end = min;
 	map->header.start = max;
@@ -908,13 +905,20 @@ _vm_map_init(vm_map_t map, pmap_t pmap, vm_offset_t min, vm_offset_t max)
 void
 vm_map_init(vm_map_t map, pmap_t pmap, vm_offset_t min, vm_offset_t max)
 {
-
 	_vm_map_init(map, pmap, min, max);
-	mtx_init(&map->system_mtx, "vm map (system)", NULL,
-	    MTX_DEF | MTX_DUPOK);
+	map->system_map = false;
 	sx_init(&map->lock, "vm map (user)");
 }
 
+void
+vm_map_init_system(vm_map_t map, pmap_t pmap, vm_offset_t min, vm_offset_t max)
+{
+	_vm_map_init(map, pmap, min, max);
+	map->system_map = true;
+	mtx_init(&map->system_mtx, "vm map (system)", NULL, MTX_DEF |
+	    MTX_DUPOK);
+}
+
 /*
  *	vm_map_entry_dispose:	[ internal use only ]
  *
diff --git a/sys/vm/vm_map.h b/sys/vm/vm_map.h
index d0b4bb52968e..5ecec0531e1c 100644
--- a/sys/vm/vm_map.h
+++ b/sys/vm/vm_map.h
@@ -203,8 +203,10 @@ vm_map_entry_system_wired_count(vm_map_entry_t entry)
  */
 struct vm_map {
 	struct vm_map_entry header;	/* List of entries */
-	struct sx lock;			/* Lock for map data */
-	struct mtx system_mtx;
+	union {
+		struct sx lock;			/* Lock for map data */
+		struct mtx system_mtx;
+	};
 	int nentries;			/* Number of entries */
 	vm_size_t size;			/* virtual size */
 	u_int timestamp;		/* Version number */
@@ -484,6 +486,7 @@ int vm_map_fixed(vm_map_t, vm_object_t, vm_ooffset_t, vm_offset_t, vm_size_t,
 vm_offset_t vm_map_findspace(vm_map_t, vm_offset_t, vm_size_t);
 int vm_map_inherit (vm_map_t, vm_offset_t, vm_offset_t, vm_inherit_t);
 void vm_map_init(vm_map_t, pmap_t, vm_offset_t, vm_offset_t);
+void vm_map_init_system(vm_map_t, pmap_t, vm_offset_t, vm_offset_t);
 int vm_map_insert (vm_map_t, vm_object_t, vm_ooffset_t, vm_offset_t, vm_offset_t, vm_prot_t, vm_prot_t, int);
 int vm_map_lookup (vm_map_t *, vm_offset_t, vm_prot_t, vm_map_entry_t *, vm_object_t *,
     vm_pindex_t *, vm_prot_t *, boolean_t *);