git: 9ff3e8b7f090 - main - bhyve: fix resume for vms with guest_ncpus > 1

From: Corvin Köhne <corvink_at_FreeBSD.org>
Date: Tue, 28 Feb 2023 12:38:40 UTC
The branch main has been updated by corvink:

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

commit 9ff3e8b7f09034202ad1f24f4fbbe52cf7f56117
Author:     Vitaliy Gusev <gusev.vitaliy@gmail.com>
AuthorDate: 2023-02-28 10:28:40 +0000
Commit:     Corvin Köhne <corvink@FreeBSD.org>
CommitDate: 2023-02-28 12:37:58 +0000

    bhyve: fix resume for vms with guest_ncpus > 1
    
    This error occurs because vm->vcpu[1] has not been allocated yet when
    vm_snapshot_vm() is called.
    
    To fix this, move spinup_vcpu() before restore code.
    
    Reviewed by:            corvink, markj
    MFC after:              2 weeks
    Sponsored by:           vStack
    Differential Revision:  https://reviews.freebsd.org/D38477
---
 usr.sbin/bhyve/bhyverun.c | 46 +++++++++++++++++++++++-----------------------
 1 file changed, 23 insertions(+), 23 deletions(-)

diff --git a/usr.sbin/bhyve/bhyverun.c b/usr.sbin/bhyve/bhyverun.c
index caa284a8dd63..3ff83f55474b 100644
--- a/usr.sbin/bhyve/bhyverun.c
+++ b/usr.sbin/bhyve/bhyverun.c
@@ -552,7 +552,7 @@ fbsdrun_start_thread(void *param)
 }
 
 static void
-fbsdrun_addcpu(struct vmctx *ctx, int newcpu, bool suspend)
+fbsdrun_addcpu(struct vmctx *ctx, int newcpu)
 {
 	int error;
 
@@ -562,8 +562,7 @@ fbsdrun_addcpu(struct vmctx *ctx, int newcpu, bool suspend)
 
 	CPU_SET_ATOMIC(newcpu, &cpumask);
 
-	if (suspend)
-		vm_suspend_cpu(ctx, newcpu);
+	vm_suspend_cpu(ctx, newcpu);
 
 	mt_vmm_info[newcpu].mt_ctx = ctx;
 	mt_vmm_info[newcpu].mt_vcpu = newcpu;
@@ -1138,7 +1137,7 @@ do_open(const char *vmname)
 }
 
 static void
-spinup_vcpu(struct vmctx *ctx, int vcpu, bool suspend)
+spinup_vcpu(struct vmctx *ctx, int vcpu)
 {
 	int error;
 
@@ -1154,7 +1153,7 @@ spinup_vcpu(struct vmctx *ctx, int vcpu, bool suspend)
 		assert(error == 0);
 	}
 
-	fbsdrun_addcpu(ctx, vcpu, suspend);
+	fbsdrun_addcpu(ctx, vcpu);
 }
 
 static bool
@@ -1488,6 +1487,16 @@ main(int argc, char *argv[])
 		assert(error == 0);
 	}
 
+	/* Allocate per-VCPU resources. */
+	mt_vmm_info = calloc(guest_ncpus, sizeof(*mt_vmm_info));
+
+	/*
+	 * Add all vCPUs.
+	 */
+	for (int vcpu = 0; vcpu < guest_ncpus; vcpu++) {
+		spinup_vcpu(ctx, vcpu);
+	}
+
 #ifdef BHYVE_SNAPSHOT
 	if (restore_file != NULL) {
 		fprintf(stdout, "Pausing pci devs...\r\n");
@@ -1564,9 +1573,6 @@ main(int argc, char *argv[])
 #endif
 
 #ifdef BHYVE_SNAPSHOT
-	if (restore_file != NULL)
-		destroy_restore_state(&rstate);
-
 	/* initialize mutex/cond variables */
 	init_snapshot();
 
@@ -1576,24 +1582,18 @@ main(int argc, char *argv[])
 	if (init_checkpoint_thread(ctx) < 0)
 		printf("Failed to start checkpoint thread!\r\n");
 
-	if (restore_file != NULL)
+	if (restore_file != NULL) {
+		destroy_restore_state(&rstate);
 		vm_restore_time(ctx);
-#endif
-
-	/* Allocate per-VCPU resources. */
-	mt_vmm_info = calloc(guest_ncpus, sizeof(*mt_vmm_info));
 
-	/*
-	 * Add all vCPUs.
-	 */
-	for (int vcpu = 0; vcpu < guest_ncpus; vcpu++) {
-		bool suspend = (vcpu != BSP);
-#ifdef BHYVE_SNAPSHOT
-		if (restore_file != NULL)
-			suspend = false;
-#endif
-		spinup_vcpu(ctx, vcpu, suspend);
+		for (int i = 0; i < guest_ncpus; i++) {
+			if (i == BSP)
+				continue;
+			vm_resume_cpu(ctx, i);
+		}
 	}
+#endif
+	vm_resume_cpu(ctx, BSP);
 
 	/*
 	 * Head off to the main event dispatch loop