Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

x86/kvm: Add "nopvspin" parameter to disable PV spinlocks

There are cases where a guest tries to switch spinlocks to bare metal
behavior (e.g. by setting "xen_nopvspin" on XEN platform and
"hv_nopvspin" on HYPER_V).

That feature is missed on KVM, add a new parameter "nopvspin" to disable
PV spinlocks for KVM guest.

The new 'nopvspin' parameter will also replace Xen and Hyper-V specific
parameters in future patches.

Define variable nopvsin as global because it will be used in future
patches as above.

Signed-off-by: Zhenzhong Duan <zhenzhong.duan@oracle.com>
Reviewed-by: Vitaly Kuznetsov <vkuznets@redhat.com>
Cc: Jonathan Corbet <corbet@lwn.net>
Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: Borislav Petkov <bp@alien8.de>
Cc: "H. Peter Anvin" <hpa@zytor.com>
Cc: Paolo Bonzini <pbonzini@redhat.com>
Cc: Radim Krcmar <rkrcmar@redhat.com>
Cc: Sean Christopherson <sean.j.christopherson@intel.com>
Cc: Vitaly Kuznetsov <vkuznets@redhat.com>
Cc: Wanpeng Li <wanpengli@tencent.com>
Cc: Jim Mattson <jmattson@google.com>
Cc: Joerg Roedel <joro@8bytes.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Will Deacon <will@kernel.org>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>

authored by

Zhenzhong Duan and committed by
Paolo Bonzini
05eee619 5aefd786

+47 -9
+5
Documentation/admin-guide/kernel-parameters.txt
··· 5739 5739 as generic guest with no PV drivers. Currently support 5740 5740 XEN HVM, KVM, HYPER_V and VMWARE guest. 5741 5741 5742 + nopvspin [X86,KVM] 5743 + Disables the qspinlock slow path using PV optimizations 5744 + which allow the hypervisor to 'idle' the guest on lock 5745 + contention. 5746 + 5742 5747 xirc2ps_cs= [NET,PCMCIA] 5743 5748 Format: 5744 5749 <irq>,<irq_mask>,<io>,<full_duplex>,<do_sound>,<lockup_hack>[,<irq2>[,<irq3>[,<irq4>]]]
+1
arch/x86/include/asm/qspinlock.h
··· 32 32 extern void __pv_init_lock_hash(void); 33 33 extern void __pv_queued_spin_lock_slowpath(struct qspinlock *lock, u32 val); 34 34 extern void __raw_callee_save___pv_queued_spin_unlock(struct qspinlock *lock); 35 + extern bool nopvspin; 35 36 36 37 #define queued_spin_unlock queued_spin_unlock 37 38 /**
+34 -9
arch/x86/kernel/kvm.c
··· 871 871 */ 872 872 void __init kvm_spinlock_init(void) 873 873 { 874 - /* Does host kernel support KVM_FEATURE_PV_UNHALT? */ 875 - if (!kvm_para_has_feature(KVM_FEATURE_PV_UNHALT)) 876 - return; 877 - 878 - if (kvm_para_has_hint(KVM_HINTS_REALTIME)) { 879 - static_branch_disable(&virt_spin_lock_key); 874 + /* 875 + * In case host doesn't support KVM_FEATURE_PV_UNHALT there is still an 876 + * advantage of keeping virt_spin_lock_key enabled: virt_spin_lock() is 877 + * preferred over native qspinlock when vCPU is preempted. 878 + */ 879 + if (!kvm_para_has_feature(KVM_FEATURE_PV_UNHALT)) { 880 + pr_info("PV spinlocks disabled, no host support\n"); 880 881 return; 881 882 } 882 883 883 - /* Don't use the pvqspinlock code if there is only 1 vCPU. */ 884 - if (num_possible_cpus() == 1) 885 - return; 884 + /* 885 + * Disable PV spinlocks and use native qspinlock when dedicated pCPUs 886 + * are available. 887 + */ 888 + if (kvm_para_has_hint(KVM_HINTS_REALTIME)) { 889 + pr_info("PV spinlocks disabled with KVM_HINTS_REALTIME hints\n"); 890 + goto out; 891 + } 892 + 893 + if (num_possible_cpus() == 1) { 894 + pr_info("PV spinlocks disabled, single CPU\n"); 895 + goto out; 896 + } 897 + 898 + if (nopvspin) { 899 + pr_info("PV spinlocks disabled, forced by \"nopvspin\" parameter\n"); 900 + goto out; 901 + } 902 + 903 + pr_info("PV spinlocks enabled\n"); 886 904 887 905 __pv_init_lock_hash(); 888 906 pv_ops.lock.queued_spin_lock_slowpath = __pv_queued_spin_lock_slowpath; ··· 913 895 pv_ops.lock.vcpu_is_preempted = 914 896 PV_CALLEE_SAVE(__kvm_vcpu_is_preempted); 915 897 } 898 + /* 899 + * When PV spinlock is enabled which is preferred over 900 + * virt_spin_lock(), virt_spin_lock_key's value is meaningless. 901 + * Just disable it anyway. 902 + */ 903 + out: 904 + static_branch_disable(&virt_spin_lock_key); 916 905 } 917 906 918 907 #endif /* CONFIG_PARAVIRT_SPINLOCKS */
+7
kernel/locking/qspinlock.c
··· 581 581 #include "qspinlock_paravirt.h" 582 582 #include "qspinlock.c" 583 583 584 + bool nopvspin __initdata; 585 + static __init int parse_nopvspin(char *arg) 586 + { 587 + nopvspin = true; 588 + return 0; 589 + } 590 + early_param("nopvspin", parse_nopvspin); 584 591 #endif