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

From: zhanghao

Date: Sun Apr 19 2026 - 22:31:44 EST


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);

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.

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
--
2.25.1