Re: [PATCH] KVM: Dynamically scale directed-yield retries in kvm_vcpu_on_spin()

From: Wei Wang

Date: Tue Apr 21 2026 - 05:05:36 EST


On 4/20/26 10:31 AM, zhanghao wrote:
kvm_vcpu_on_spin() currently uses a fixed retry budget (3) when
kvm_vcpu_yield_to() returns a negative value. That fixed limit is
often too small on large VMs, where candidate selection is more likely
to race with scheduler state and transiently fail.

Replace the constant retry count with a vCPU-count-based budget:

try = min(nr_vcpus - 1, 8);

Why is 8 the right value here?

Also, the probability of hitting these races depends more on host
overcommitment than on the VM’s vCPU count.


This keeps retries bounded while allowing more opportunities to find a
yieldable target as VM size grows. Small VMs keep near-identical
behavior, while larger VMs get a better chance of making directed-yield
progress under contention.

Increasing the retry count also adds overhead. Do you have data showing
that this could provide a measurable performance benefit?


No functional change is intended beyond retry-budget tuning.

Signed-off-by: Hao Zhang <zhanghao1@xxxxxxxxxx>
---
virt/kvm/kvm_main.c | 4 +++-
1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c
index 89489996fbc1..cbe53cea10a3 100644
--- a/virt/kvm/kvm_main.c
+++ b/virt/kvm/kvm_main.c
@@ -3961,12 +3961,14 @@ void kvm_vcpu_on_spin(struct kvm_vcpu *me, bool yield_to_kernel_mode)
int nr_vcpus, start, i, idx, yielded;
struct kvm *kvm = me->kvm;
struct kvm_vcpu *vcpu;
- int try = 3;
+ int try = 0;
nr_vcpus = atomic_read(&kvm->online_vcpus);
if (nr_vcpus < 2)
return;
+ try = min(nr_vcpus - 1, 8);
+
/* Pairs with the smp_wmb() in kvm_vm_ioctl_create_vcpu(). */
smp_rmb();

base-commit: c1f49dea2b8f335813d3b348fd39117fb8efb428