Skip to content

Commit 9605505

Browse files
lrq-maxsean-jc
authored andcommitted
x86/kvm: Prefer native qspinlock for dedicated vCPUs irrespective of PV_UNHALT
The commit b2798ba ("KVM: X86: Choose qspinlock when dedicated physical CPUs are available") states that when PV_DEDICATED=1 (vCPU has dedicated pCPU), qspinlock should be preferred regardless of PV_UNHALT. However, the current implementation doesn't reflect this: when PV_UNHALT=0, we still use virt_spin_lock() even with dedicated pCPUs. This is suboptimal because: 1. Native qspinlocks should outperform virt_spin_lock() for dedicated vCPUs irrespective of HALT exiting 2. virt_spin_lock() should only be preferred when vCPUs may be preempted (non-dedicated case) So reorder the PV spinlock checks to: 1. First handle dedicated pCPU case (disable virt_spin_lock_key) 2. Second check single CPU, and nopvspin configuration 3. Only then check PV_UNHALT support This ensures we always use native qspinlock for dedicated vCPUs, delivering pretty performance gains at high contention levels. Signed-off-by: Li RongQing <[email protected]> Reviewed-by: Sean Christopherson <[email protected]> Tested-by: Wangyang Guo <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Sean Christopherson <[email protected]>
1 parent 657bf70 commit 9605505

File tree

1 file changed

+10
-10
lines changed

1 file changed

+10
-10
lines changed

arch/x86/kernel/kvm.c

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1088,16 +1088,6 @@ static void kvm_wait(u8 *ptr, u8 val)
10881088
*/
10891089
void __init kvm_spinlock_init(void)
10901090
{
1091-
/*
1092-
* In case host doesn't support KVM_FEATURE_PV_UNHALT there is still an
1093-
* advantage of keeping virt_spin_lock_key enabled: virt_spin_lock() is
1094-
* preferred over native qspinlock when vCPU is preempted.
1095-
*/
1096-
if (!kvm_para_has_feature(KVM_FEATURE_PV_UNHALT)) {
1097-
pr_info("PV spinlocks disabled, no host support\n");
1098-
return;
1099-
}
1100-
11011091
/*
11021092
* Disable PV spinlocks and use native qspinlock when dedicated pCPUs
11031093
* are available.
@@ -1117,6 +1107,16 @@ void __init kvm_spinlock_init(void)
11171107
goto out;
11181108
}
11191109

1110+
/*
1111+
* In case host doesn't support KVM_FEATURE_PV_UNHALT there is still an
1112+
* advantage of keeping virt_spin_lock_key enabled: virt_spin_lock() is
1113+
* preferred over native qspinlock when vCPU is preempted.
1114+
*/
1115+
if (!kvm_para_has_feature(KVM_FEATURE_PV_UNHALT)) {
1116+
pr_info("PV spinlocks disabled, no host support\n");
1117+
return;
1118+
}
1119+
11201120
pr_info("PV spinlocks enabled\n");
11211121

11221122
__pv_init_lock_hash();

0 commit comments

Comments
 (0)