git: 730510dc1ab2 - main - bhyve: Allocate mmio_hint array based on number of guest CPUs.

From: John Baldwin <jhb_at_FreeBSD.org>
Date: Wed, 09 Mar 2022 23:57:31 UTC
The branch main has been updated by jhb:

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

commit 730510dc1ab2049d707706241d9a6e73a6952a4e
Author:     John Baldwin <jhb@FreeBSD.org>
AuthorDate: 2022-03-09 23:38:49 +0000
Commit:     John Baldwin <jhb@FreeBSD.org>
CommitDate: 2022-03-09 23:38:49 +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
---
 usr.sbin/bhyve/bhyverun.c | 2 +-
 usr.sbin/bhyve/mem.c      | 9 ++++++---
 usr.sbin/bhyve/mem.h      | 2 +-
 3 files changed, 8 insertions(+), 5 deletions(-)

diff --git a/usr.sbin/bhyve/bhyverun.c b/usr.sbin/bhyve/bhyverun.c
index 734239b38a8b..0f3c6e60e1d9 100644
--- a/usr.sbin/bhyve/bhyverun.c
+++ b/usr.sbin/bhyve/bhyverun.c
@@ -1445,7 +1445,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 49a468305769..0a1c4e29972b 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;
 
@@ -352,7 +353,7 @@ unregister_mem(struct mem_range *memp)
 		RB_REMOVE(mmio_rb_tree, &mmio_rb_root, entry);
 
 		/* flush Per-vCPU cache */
-		for (i=0; i < VM_MAXCPU; i++) {
+		for (i = 0; i < mmio_ncpu; i++) {
 			if (mmio_hint[i] == entry)
 				mmio_hint[i] = NULL;
 		}
@@ -367,9 +368,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);