git: e560cb46edd4 - main - bhyve/arm64: Fix a race in PSCI CPU_ON

From: Andrew Turner <andrew_at_FreeBSD.org>
Date: Thu, 07 Aug 2025 09:51:45 UTC
The branch main has been updated by andrew:

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

commit e560cb46edd4898bdb77e82a4b5e53956357a48d
Author:     Andrew Turner <andrew@FreeBSD.org>
AuthorDate: 2025-08-07 09:31:48 +0000
Commit:     Andrew Turner <andrew@FreeBSD.org>
CommitDate: 2025-08-07 09:45:15 +0000

    bhyve/arm64: Fix a race in PSCI CPU_ON
    
    When multiple CPUs try to start the same CPU only one should return
    success, the other CPUs should see either ON_PENDING or ALREADY_ON.
    There was a race between checking if the CPU is on and marking it as
    on in the running_cpumask CPU set.
    
    Fix the race by using CPU_TEST_SET_ATOMIC to both check and set the
    state in running_cpumask.
    
    Reviewed by:    markj
    Sponsored by:   Arm Ltd
    Differential Revision:  https://reviews.freebsd.org/D51766
---
 usr.sbin/bhyve/aarch64/vmexit.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)

diff --git a/usr.sbin/bhyve/aarch64/vmexit.c b/usr.sbin/bhyve/aarch64/vmexit.c
index 9ecf25c04e41..6297a01d427f 100644
--- a/usr.sbin/bhyve/aarch64/vmexit.c
+++ b/usr.sbin/bhyve/aarch64/vmexit.c
@@ -216,7 +216,7 @@ vmexit_smccc(struct vmctx *ctx, struct vcpu *vcpu, struct vm_run *vmrun)
 			break;
 		}
 
-		if (CPU_ISSET(newcpu, &running_cpumask)) {
+		if (CPU_TEST_SET_ATOMIC(newcpu, &running_cpumask)) {
 			smccc_rv = PSCI_RETVAL_ALREADY_ON;
 			break;
 		}
@@ -235,7 +235,6 @@ vmexit_smccc(struct vmctx *ctx, struct vcpu *vcpu, struct vm_run *vmrun)
 		assert(error == 0);
 
 		vm_resume_cpu(newvcpu);
-		CPU_SET_ATOMIC(newcpu, &running_cpumask);
 
 		smccc_rv = PSCI_RETVAL_SUCCESS;
 		break;