Skip to content

Commit e639222

Browse files
yu-chen-surfKAGA-KOKO
authored andcommitted
x86/paravirt: Fix incorrect virt spinlock setting on bare metal
The kernel can change spinlock behavior when running as a guest. But this guest-friendly behavior causes performance problems on bare metal. The kernel uses a static key to switch between the two modes. In theory, the static key is enabled by default (run in guest mode) and should be disabled for bare metal (and in some guests that want native behavior or paravirt spinlock). A performance drop is reported when running encode/decode workload and BenchSEE cache sub-workload. Bisect points to commit ce0a1b6 ("x86/paravirt: Silence unused native_pv_lock_init() function warning"). When CONFIG_PARAVIRT_SPINLOCKS is disabled the virt_spin_lock_key is incorrectly set to true on bare metal. The qspinlock degenerates to test-and-set spinlock, which decreases the performance on bare metal. Set the default value of virt_spin_lock_key to false. If booting in a VM, enable this key. Later during the VM initialization, if other high-efficient spinlock is preferred (e.g. paravirt-spinlock), or the user wants the native qspinlock (via nopvspin boot commandline), the virt_spin_lock_key is disabled accordingly. This results in the following decision matrix: X86_FEATURE_HYPERVISOR Y Y Y N CONFIG_PARAVIRT_SPINLOCKS Y Y N Y/N PV spinlock Y N N Y/N virt_spin_lock_key N Y/N Y N Fixes: ce0a1b6 ("x86/paravirt: Silence unused native_pv_lock_init() function warning") Reported-by: Prem Nath Dey <[email protected]> Reported-by: Xiaoping Zhou <[email protected]> Suggested-by: Dave Hansen <[email protected]> Suggested-by: Qiuxu Zhuo <[email protected]> Suggested-by: Nikolay Borisov <[email protected]> Signed-off-by: Chen Yu <[email protected]> Signed-off-by: Thomas Gleixner <[email protected]> Reviewed-by: Nikolay Borisov <[email protected]> Cc: [email protected] Link: https://lore.kernel.org/all/[email protected]
1 parent ab84ba6 commit e639222

File tree

2 files changed

+10
-9
lines changed

2 files changed

+10
-9
lines changed

arch/x86/include/asm/qspinlock.h

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,13 +66,15 @@ static inline bool vcpu_is_preempted(long cpu)
6666

6767
#ifdef CONFIG_PARAVIRT
6868
/*
69-
* virt_spin_lock_key - enables (by default) the virt_spin_lock() hijack.
69+
* virt_spin_lock_key - disables by default the virt_spin_lock() hijack.
7070
*
71-
* Native (and PV wanting native due to vCPU pinning) should disable this key.
72-
* It is done in this backwards fashion to only have a single direction change,
73-
* which removes ordering between native_pv_spin_init() and HV setup.
71+
* Native (and PV wanting native due to vCPU pinning) should keep this key
72+
* disabled. Native does not touch the key.
73+
*
74+
* When in a guest then native_pv_lock_init() enables the key first and
75+
* KVM/XEN might conditionally disable it later in the boot process again.
7476
*/
75-
DECLARE_STATIC_KEY_TRUE(virt_spin_lock_key);
77+
DECLARE_STATIC_KEY_FALSE(virt_spin_lock_key);
7678

7779
/*
7880
* Shortcut for the queued_spin_lock_slowpath() function that allows

arch/x86/kernel/paravirt.c

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,12 @@ DEFINE_ASM_FUNC(pv_native_irq_enable, "sti", .noinstr.text);
5151
DEFINE_ASM_FUNC(pv_native_read_cr2, "mov %cr2, %rax", .noinstr.text);
5252
#endif
5353

54-
DEFINE_STATIC_KEY_TRUE(virt_spin_lock_key);
54+
DEFINE_STATIC_KEY_FALSE(virt_spin_lock_key);
5555

5656
void __init native_pv_lock_init(void)
5757
{
58-
if (IS_ENABLED(CONFIG_PARAVIRT_SPINLOCKS) &&
59-
!boot_cpu_has(X86_FEATURE_HYPERVISOR))
60-
static_branch_disable(&virt_spin_lock_key);
58+
if (boot_cpu_has(X86_FEATURE_HYPERVISOR))
59+
static_branch_enable(&virt_spin_lock_key);
6160
}
6261

6362
static void native_tlb_remove_table(struct mmu_gather *tlb, void *table)

0 commit comments

Comments
 (0)