target/i386/whpx: let a vCPU run on without the BQL when nothing is pending

whpx_vcpu_pre_run took the BQL before every WHvRunVirtualProcessor, so a
vCPU waited for the BQL's holder after any exit, including an MMIO write to
a lockless region such as reims-graphics-pci's doorbell. A QMP screendump in
PNG holds the BQL in png_save for about 120 ms at 2880x1620 on a Mac mini 2018
(UHD 630). For that long the guest's submitting vCPU stood still, and an
on-screen Metal app's nextDrawable blocked 116-133 ms in every run, whether
or not the device's interrupts went off the main loop (the commit before
this). Measured 11-16 with present_probe and a PNG screendump at +6 s, runs 2
on: before both commits, gap max 128-148 ms; with the commit before this
alone, 116.6-133.7 ms. Under a build that logged pre_run's waits, the vCPUs
waited 111-123 ms for the BQL at each screendump, each after a
MemoryAccess exit to the device's BARs, while 8191 of 8192 interrupts went
out directly. With this skip on top: 33.7-34.0 ms, 60.0 fps, 0 dropped, and
no wait over 20 ms, the same as KVM's on ms01's Iris Xe (33.3-33.7 ms).

kvm_arch_pre_run takes the BQL only to inject a pending NMI or SMI, and,
when the PIC is not in the kernel, for the userspace PIC and TPR; with KVM's
in-kernel irqchip and nothing pending it takes none. This matches that: with
the hypervisor's APIC (kernel-irqchip) and no NMI, SMI, INIT, TPR or HARD
request pending, the locked section has nothing to do, so it is skipped and
only ready_for_pic_interrupt is reset. Its TPR sync and PIC injection are
kernel-irqchip=off's or wait for CPU_INTERRUPT_HARD. kernel-irqchip=off, and
any pending request, take the BQL as before.

The kick-after-check window: a request raised after the check goes through
cpu_interrupt (generic_handle_interrupt), which kicks the vCPU through
qemu_cpu_kick, whpx_kick_vcpu_thread and whpx_vcpu_kick, that is
WHvCancelRunVirtualProcessor. That brings the vCPU back to pre_run, where
the next check sees the request. A request raised between the old
bql_unlock and the run already had this window, and the loop's own
exit_request kick before WHvRunVirtualProcessor relies on the same cancel.
Microsoft's reference for WHvCancelRunVirtualProcessor does not say whether
a cancel made before the run returns it at once (read).
This commit is contained in:
Anees Iqbal committed 2026-11-16 12:27:58 -08:00
1 parent 243971f2cd
commit 0fdbf3810d
1 file changed
+21
+21
View File
@@ -2072,6 +2072,27 @@ static void whpx_vcpu_pre_run(CPUState *cpu)
memset(&new_int, 0, sizeof(new_int));
memset(reg_values, 0, sizeof(reg_values));
/*
* With the hypervisor's APIC (kernel-irqchip) and no request pending,
* nothing below reads or writes what the BQL guards: the TPR sync and the
* PIC's injection are kernel-irqchip=off's or wait for CPU_INTERRUPT_HARD.
* So the vCPU goes on without the BQL, as kvm_arch_pre_run does with
* KVM's in-kernel irqchip, and an exit (an MMIO write to a lockless
* region) does not wait for the BQL's holder: a PNG screendump held it
* for about 120 ms in png_save. A request raised after this check kicks
* the vCPU (cpu_interrupt, qemu_cpu_kick, whpx_vcpu_kick: the cancel of
* its run), which brings it back here; a request raised between the
* bql_unlock below and the run always had that window, and the
* exit_request kick before the run relies on the same cancel.
*/
if (whpx_irqchip_in_kernel() &&
!cpu_test_interrupt(cpu, CPU_INTERRUPT_NMI | CPU_INTERRUPT_SMI |
CPU_INTERRUPT_INIT | CPU_INTERRUPT_TPR |
CPU_INTERRUPT_HARD)) {
vcpu->ready_for_pic_interrupt = false;
return;
}
bql_lock();
/* Inject NMI */