Skip to content
/ src Public

Commit 505ca64

Browse files
committed
vmd(8): fix race in vm pause barrier usage.
Moves the barrier initialization and destruction outside of the pause_vm function to eliminate a race between the vcpu thread waiting on the barrier and the event thread processing the pause request creating the barrier. ok mlarkin@
1 parent b46498d commit 505ca64

1 file changed

Lines changed: 12 additions & 17 deletions

File tree

usr.sbin/vmd/vm.c

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* $OpenBSD: vm.c,v 1.119 2025/11/25 14:20:33 dv Exp $ */
1+
/* $OpenBSD: vm.c,v 1.120 2025/12/01 15:12:44 dv Exp $ */
22

33
/*
44
* Copyright (c) 2015 Mike Larkin <mlarkin@openbsd.org>
@@ -442,14 +442,6 @@ pause_vm(struct vmd_vm *vm)
442442
current_vm->vm_state |= VM_STATE_PAUSED;
443443
mutex_unlock(&vm_mtx);
444444

445-
ret = pthread_barrier_init(&vm_pause_barrier, NULL,
446-
vm->vm_params.vmc_params.vcp_ncpus + 1);
447-
if (ret) {
448-
log_warnx("%s: cannot initialize pause barrier (%d)",
449-
__progname, ret);
450-
return;
451-
}
452-
453445
for (n = 0; n < vm->vm_params.vmc_params.vcp_ncpus; n++) {
454446
ret = pthread_cond_broadcast(&vcpu_run_cond[n]);
455447
if (ret) {
@@ -465,13 +457,6 @@ pause_vm(struct vmd_vm *vm)
465457
return;
466458
}
467459

468-
ret = pthread_barrier_destroy(&vm_pause_barrier);
469-
if (ret) {
470-
log_warnx("%s: could not destroy pause barrier (%d)",
471-
__progname, ret);
472-
return;
473-
}
474-
475460
pause_vm_md(vm);
476461
}
477462

@@ -623,6 +608,12 @@ run_vm(struct vmop_create_params *vmc, struct vcpu_reg_state *vrs)
623608
return (ENOMEM);
624609
}
625610

611+
ret = pthread_barrier_init(&vm_pause_barrier, NULL, vcp->vcp_ncpus + 1);
612+
if (ret) {
613+
log_warnx("cannot initialize pause barrier (%d)", ret);
614+
return (ret);
615+
}
616+
626617
log_debug("%s: starting %zu vcpu thread(s) for vm %s", __func__,
627618
vcp->vcp_ncpus, vcp->vcp_name);
628619

@@ -781,11 +772,15 @@ run_vm(struct vmop_create_params *vmc, struct vcpu_reg_state *vrs)
781772
}
782773
mutex_unlock(&vm_mtx);
783774
if (i == vcp->vcp_ncpus)
784-
return (ret);
775+
break;
785776

786777
/* Some more threads to wait for, start over */
787778
}
788779

780+
ret = pthread_barrier_destroy(&vm_pause_barrier);
781+
if (ret)
782+
log_warnx("could not destroy pause barrier (%d)", ret);
783+
789784
return (ret);
790785
}
791786

0 commit comments

Comments
 (0)