]> git.ipfire.org Git - thirdparty/kernel/stable.git/commitdiff
x86/kvm: Prefer native qspinlock for dedicated vCPUs irrespective of PV_UNHALT
authorLi RongQing <lirongqing@baidu.com>
Tue, 22 Jul 2025 11:00:05 +0000 (19:00 +0800)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Thu, 13 Nov 2025 20:37:20 +0000 (15:37 -0500)
[ Upstream commit 960550503965094b0babd7e8c83ec66c8a763b0b ]

The commit b2798ba0b876 ("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 <lirongqing@baidu.com>
Reviewed-by: Sean Christopherson <seanjc@google.com>
Tested-by: Wangyang Guo <wangyang.guo@intel.com>
Link: https://lore.kernel.org/r/20250722110005.4988-1-lirongqing@baidu.com
Signed-off-by: Sean Christopherson <seanjc@google.com>
Signed-off-by: Sasha Levin <sashal@kernel.org>
arch/x86/kernel/kvm.c

index 57379698015ed314cda52bfe731206abf9d1df6c..2ecb2ec06aebcc622cb88276368019262073d0a0 100644 (file)
@@ -1089,16 +1089,6 @@ static void kvm_wait(u8 *ptr, u8 val)
  */
 void __init kvm_spinlock_init(void)
 {
-       /*
-        * In case host doesn't support KVM_FEATURE_PV_UNHALT there is still an
-        * advantage of keeping virt_spin_lock_key enabled: virt_spin_lock() is
-        * preferred over native qspinlock when vCPU is preempted.
-        */
-       if (!kvm_para_has_feature(KVM_FEATURE_PV_UNHALT)) {
-               pr_info("PV spinlocks disabled, no host support\n");
-               return;
-       }
-
        /*
         * Disable PV spinlocks and use native qspinlock when dedicated pCPUs
         * are available.
@@ -1118,6 +1108,16 @@ void __init kvm_spinlock_init(void)
                goto out;
        }
 
+       /*
+        * In case host doesn't support KVM_FEATURE_PV_UNHALT there is still an
+        * advantage of keeping virt_spin_lock_key enabled: virt_spin_lock() is
+        * preferred over native qspinlock when vCPU is preempted.
+        */
+       if (!kvm_para_has_feature(KVM_FEATURE_PV_UNHALT)) {
+               pr_info("PV spinlocks disabled, no host support\n");
+               return;
+       }
+
        pr_info("PV spinlocks enabled\n");
 
        __pv_init_lock_hash();