git: 2412b82929a6 - stable/13 - bhyve: Allocate mmio_hint array based on number of guest CPUs.

From: John Baldwin <jhb_at_FreeBSD.org>
Date: Wed, 11 May 2022 18:56:06 UTC
The branch stable/13 has been updated by jhb:

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

commit 2412b82929a6a31d40f396b3c20fc1d2d9e31289
Author:     John Baldwin <jhb@FreeBSD.org>
AuthorDate: 2022-03-09 23:38:49 +0000
Commit:     John Baldwin <jhb@FreeBSD.org>
CommitDate: 2022-05-11 18:47:58 +0000

    bhyve: Allocate mmio_hint array based on number of guest CPUs.
    
    This avoids an instance of hardcoding VM_MAXCPU in userspace.
    
    Reviewed by:    grehan
    Differential Revision:  https://reviews.freebsd.org/D34489
    
    (cherry picked from commit 730510dc1ab2049d707706241d9a6e73a6952a4e)
---
 usr.sbin/bhyve/bhyverun.c |  2 +-
 usr.sbin/bhyve/mem.c      | 11 +++++++----
 usr.sbin/bhyve/mem.h      |  2 +-
 3 files changed, 9 insertions(+), 6 deletions(-)

diff --git a/usr.sbin/bhyve/bhyverun.c b/usr.sbin/bhyve/bhyverun.c
index e4d96d2293fa..8a1a82cefc7c 100644
--- a/usr.sbin/bhyve/bhyverun.c
+++ b/usr.sbin/bhyve/bhyverun.c
@@ -1421,7 +1421,7 @@ main(int argc, char *argv[])
 		exit(4);
 	}
 
-	init_mem();
+	init_mem(guest_ncpus);
 	init_inout();
 	kernemu_dev_init();
 	init_bootrom(ctx);
diff --git a/usr.sbin/bhyve/mem.c b/usr.sbin/bhyve/mem.c
index 90aefe45c856..85c4ad0eaf46 100644
--- a/usr.sbin/bhyve/mem.c
+++ b/usr.sbin/bhyve/mem.c
@@ -68,7 +68,8 @@ RB_HEAD(mmio_rb_tree, mmio_rb_range) mmio_rb_root, mmio_rb_fallback;
  * consecutive addresses in a range, it makes sense to cache the
  * result of a lookup.
  */
-static struct mmio_rb_range	*mmio_hint[VM_MAXCPU];
+static struct mmio_rb_range	**mmio_hint;
+static int mmio_ncpu;
 
 static pthread_rwlock_t mmio_rwlock;
 
@@ -349,8 +350,8 @@ unregister_mem(struct mem_range *memp)
 		assert((mr->flags & MEM_F_IMMUTABLE) == 0);
 		RB_REMOVE(mmio_rb_tree, &mmio_rb_root, entry);
 
-		/* flush Per-vCPU cache */	
-		for (i=0; i < VM_MAXCPU; i++) {
+		/* flush Per-vCPU cache */
+		for (i = 0; i < mmio_ncpu; i++) {
 			if (mmio_hint[i] == entry)
 				mmio_hint[i] = NULL;
 		}
@@ -365,9 +366,11 @@ unregister_mem(struct mem_range *memp)
 }
 
 void
-init_mem(void)
+init_mem(int ncpu)
 {
 
+	mmio_ncpu = ncpu;
+	mmio_hint = calloc(ncpu, sizeof(*mmio_hint));
 	RB_INIT(&mmio_rb_root);
 	RB_INIT(&mmio_rb_fallback);
 	pthread_rwlock_init(&mmio_rwlock, NULL);
diff --git a/usr.sbin/bhyve/mem.h b/usr.sbin/bhyve/mem.h
index 38d773c43fdb..965079107476 100644
--- a/usr.sbin/bhyve/mem.h
+++ b/usr.sbin/bhyve/mem.h
@@ -52,7 +52,7 @@ struct mem_range {
 #define	MEM_F_RW		0x3
 #define	MEM_F_IMMUTABLE		0x4	/* mem_range cannot be unregistered */
 
-void	init_mem(void);
+void	init_mem(int ncpu);
 int     emulate_mem(struct vmctx *, int vcpu, uint64_t paddr, struct vie *vie,
 		    struct vm_guest_paging *paging);