locking/qspinlock/x86: Avoid test-and-set when PV_DEDICATED is set
Subsystems (5)
PATCH 1/1 locking/qspinlock/x86: Avoid test-and-set when PV_DEDICATED is set
Currently, the existing qspinlock implementation will fallback to test-and-set if the hypervisor has not set the PV_UNHALT flag.
This patch gives the opportunity to guest kernels to select between test-and-set and the regular queueu fair lock implementation based on the PV_DEDICATED KVM feature flag. When the PV_DEDICATED flag is not set, the code will still fall back to test-and-set, but when the PV_DEDICATED flag is set, the code will use the regular queue spinlock implementation.
--- Documentation/virtual/kvm/cpuid.txt | 6 ++++++ arch/x86/include/asm/qspinlock.h | 4 ++++ arch/x86/include/uapi/asm/kvm_para.h | 1 + 3 files changed, 11 insertions(+)
Documentation/virtual/kvm/cpuid.txt +6 -0
@@ -54,6 +54,12 @@ KVM_FEATURE_PV_UNHALT || 7 || guest checks this feature bit || || before enabling paravirtualized || || spinlock support. ------------------------------------------------------------------------------ +KVM_FEATURE_PV_DEDICATED || 8 || guest checks this feature bit + || || to determine if they run on + || || dedicated vCPUs, allowing opti- + || || mizations such as usage of + || || qspinlocks. +------------------------------------------------------------------------------ KVM_FEATURE_CLOCKSOURCE_STABLE_BIT || 24 || host will warn if no guest-side || || per-cpu warps are expected in || || kvmclock.
arch/x86/include/asm/qspinlock.h +4 -0
@@ -1,6 +1,8 @@#ifndef _ASM_X86_QSPINLOCK_H#define _ASM_X86_QSPINLOCK_H +#include <linux/kvm_para.h> +#include <asm/cpufeature.h> #include <asm-generic/qspinlock_types.h> #include <asm/paravirt.h>
@@ -46,6 +48,8 @@ static inline bool virt_spin_lock(struct qspinlock *lock)if (!static_cpu_has (X86_FEATURE_HYPERVISOR ))return false; +if (kvm_para_has_feature (KVM_FEATURE_PV_DEDICATED )) +return false;/* * On hypervisors without PARAVIRT_SPINLOCKS support we fall * back to a Test-and-Set spinlock, because fair locks have
arch/x86/include/uapi/asm/kvm_para.h +1 -0
@@ -24,6 +24,7 @@#define KVM_FEATURE_STEAL_TIME 5#define KVM_FEATURE_PV_EOI 6#define KVM_FEATURE_PV_UNHALT 7 +#define KVM_FEATURE_PV_DEDICATED 8/* The last 8 bits are used to indicate how to interpret the flags field * in pvclock structure. If no bits are set, all flags are ignored.
Signature · 1 line
2.7.5
arch/x86/include/asm/qspinlock.h static inline bool virt_spin_lock(struct qspinlock *lock) · line 55
Quoted 9 lines · Eduardo Valentin
arch/x86/include/asm/qspinlock.h +2
-0@@ -46,6 +48,8 @@ static inline bool virt_spin_lock(struct qspinlock *lock)if (!static_cpu_has (X86_FEATURE_HYPERVISOR ))return false; +if (kvm_para_has_feature (KVM_FEATURE_PV_DEDICATED )) +return false;/* * On hypervisors without PARAVIRT_SPINLOCKS support we fall * back to a Test-and-Set spinlock, because fair locks have
This does not apply. Much has been changed here recently.
Hello Peter,
Quoted 5 lines · Peter Zijlstra
Quoted 4 lines · Eduardo Valentin
arch/x86/include/asm/qspinlock.h +0
-0@@ -46,6 +48,8 @@ static inline bool virt_spin_lock(struct qspinlock *lock)if (!static_cpu_has (X86_FEATURE_HYPERVISOR ))return false;
Quoted 8 lines
arch/x86/include/asm/qspinlock.h static inline bool virt_spin_lock(struct qspinlock *lock) · line 55
Quoted 5 lines
arch/x86/include/asm/qspinlock.h +2
-0+if (kvm_para_has_feature (KVM_FEATURE_PV_DEDICATED )) +return false;/* * On hypervisors without PARAVIRT_SPINLOCKS support we fall * back to a Test-and-Set spinlock, because fair locks have This does not apply. Much has been changed here recently.
I checked against Linus master branch before sending. Which tree/branch are you referring to / should I based this?
Signature · 2 lines
All the best, Eduardo Valentin
Quoted 7 lines · Eduardo Valentin
Hello Peter,
Quoted 5 lines · Peter Zijlstra
Quoted 4 lines · Eduardo Valentin
arch/x86/include/asm/qspinlock.h +0
-0@@ -46,6 +48,8 @@ static inline bool virt_spin_lock(struct qspinlock *lock)if (!static_cpu_has (X86_FEATURE_HYPERVISOR ))return false;
Quoted 9 lines
Quoted 7 lines
arch/x86/include/asm/qspinlock.h static inline bool virt_spin_lock(struct qspinlock *lock) · line 55
Quoted 5 lines
arch/x86/include/asm/qspinlock.h +2
-0+if (kvm_para_has_feature (KVM_FEATURE_PV_DEDICATED )) +return false;/* * On hypervisors without PARAVIRT_SPINLOCKS support we fall * back to a Test-and-Set spinlock, because fair locks have This does not apply. Much has been changed here recently.
I checked against Linus master branch before sending. Which tree/branch are you referring to / should I based this?
Please check the tip tree
(https://git.kernel.org/pub/scm/linux/kernel/git/tip/tip.git) which has the latest changes in locking code.
Cheers,
Longman
Hey Waiman,
Quoted 15 lines · Waiman Long
Quoted 14 lines · Eduardo Valentin
Hello Peter,
Quoted 12 lines · Peter Zijlstra
arch/x86/include/asm/qspinlock.h static inline bool virt_spin_lock(struct qspinlock *lock) · line 55
Quoted 9 lines · Eduardo Valentin
arch/x86/include/asm/qspinlock.h +2
-0@@ -46,6 +48,8 @@ static inline bool virt_spin_lock(struct qspinlock *lock)if (!static_cpu_has (X86_FEATURE_HYPERVISOR ))return false; +if (kvm_para_has_feature (KVM_FEATURE_PV_DEDICATED )) +return false;/* * On hypervisors without PARAVIRT_SPINLOCKS support we fall * back to a Test-and-Set spinlock, because fair locks have This does not apply. Much has been changed here recently.
Quoted 5 lines
Quoted 2 lines
I checked against Linus master branch before sending. Which tree/branch are you referring to / should I based this?
Please check the tip tree
(https://git.kernel.org/pub/scm/linux/kernel/git/tip/tip.git) which has the latest changes in locking code.
I will rebase the patch on top of the tip tree.
Thanks.
Quoted 4 lines
Cheers,
Longman
Signature · 2 lines
All the best, Eduardo Valentin
2017-10-23 17:44-0700, Eduardo Valentin:
Quoted 2 lines
Currently, the existing qspinlock implementation will fallback to test-and-set if the hypervisor has not set the
PV_UNHALTflag.
Where have you detected the main source of overhead with pinned VCPUs? Makes me wonder if we couldn't improve general PV_UNHALT,
thanks.
Quoted 6 lines
This patch gives the opportunity to guest kernels to select between test-and-set and the regular queueu fair lock implementation based on the
PV_DEDICATEDKVM feature flag. When thePV_DEDICATEDflag is not set, the code will still fall back to test-and-set, but when thePV_DEDICATEDflag is set, the code will use the regular queue spinlock implementation.
Some flag makes sense and we do want to make sure that userspaces don't enable it in pass-through-cpuid mode.
Hello Radim,
Quoted 6 lines · Radim Krčmář
2017-10-23 17:44-0700, Eduardo Valentin:
Quoted 2 lines
Currently, the existing qspinlock implementation will fallback to test-and-set if the hypervisor has not set the
PV_UNHALTflag.Where have you detected the main source of overhead with pinned VCPUs? Makes me wonder if we couldn't improve general
PV_UNHALT,
This is essentially for cases of non-overcommitted vCPUs in which we want the instance vCPUs to run uninterrupted as much as possible. Here by disabling the PV_UNHALT, we avoid the accounting needed to properly do the PV_UNHALT hypercall, as the lock holder won't be preempted anyway for the 1:1 pin case.
Quoted 10 lines
thanks.
Quoted 6 lines
This patch gives the opportunity to guest kernels to select between test-and-set and the regular queueu fair lock implementation based on the
PV_DEDICATEDKVM feature flag. When thePV_DEDICATEDflag is not set, the code will still fall back to test-and-set, but when thePV_DEDICATEDflag is set, the code will use the regular queue spinlock implementation.
Quoted 2 lines
Some flag makes sense and we do want to make sure that userspaces don't enable it in pass-through-cpuid mode.
Did you mean something like:
arch/x86/kvm/cpuid.c +2 -1
@@ -211,7 +211,8 @@ int kvm_vcpu_ioctl_set_cpuid(struct kvm_vcpu *vcpu, }for (i =0 ; i < cpuid->nent; i++) { vcpu->arch.cpuid_entries[i].function = cpuid_entries[i].function; - vcpu->arch.cpuid_entries[i].eax = cpuid_entries[i].eax; + vcpu->arch.cpuid_entries[i].eax = cpuid_entries[i].eax & + ~KVM_FEATURE_PV_DEDICATED ; vcpu->arch.cpuid_entries[i].ebx = cpuid_entries[i].ebx; vcpu->arch.cpuid_entries[i].ecx = cpuid_entries[i].ecx; vcpu->arch.cpuid_entries[i].edx = cpuid_entries[i].edx;
But I do not see any other KVM_FEATURE_* being enforced (e.g. PV_UNHALT). Do you mind elaborating a bit here?
Quoted 1 line
Signature · 2 lines
All the best, Eduardo Valentin
2017-10-31 10:02-0700, Eduardo Valentin:
Quoted 7 lines
Hello Radim,
Quoted 4 lines · Radim Krčmář
2017-10-23 17:44-0700, Eduardo Valentin:
Quoted 2 lines
Currently, the existing qspinlock implementation will fallback to test-and-set if the hypervisor has not set the
PV_UNHALTflag.
Quoted 7 lines
Quoted 2 lines
Where have you detected the main source of overhead with pinned VCPUs? Makes me wonder if we couldn't improve general
PV_UNHALT,This is essentially for cases of non-overcommitted vCPUs in which we want the instance vCPUs to run uninterrupted as much as possible. Here by disabling the
PV_UNHALT, we avoid the accounting needed to properly do thePV_UNHALThypercall, as the lock holder won't be preempted anyway for the 1:1 pin case.
Right, I would expect that the scenario should very rarely go into the halt/kick path -- is SPIN_THRESHOLD too low?
We could also try abolishing the SPIN_THRESHOLD completely and only use vcpu_is_preempted() and state of the previous lock holder to enter the halt/kick path.
(The drawback is that vcpu_is_preempted() currently gets set even when
dropping into userspace.)
Quoted 27 lines
Quoted 9 lines
Quoted 6 lines
This patch gives the opportunity to guest kernels to select between test-and-set and the regular queueu fair lock implementation based on the
PV_DEDICATEDKVM feature flag. When thePV_DEDICATEDflag is not set, the code will still fall back to test-and-set, but when thePV_DEDICATEDflag is set, the code will use the regular queue spinlock implementation.Some flag makes sense and we do want to make sure that userspaces don't enable it in pass-through-cpuid mode.
Did you mean something like:
arch/x86/kvm/cpuid.c +2
-1@@ -211,7 +211,8 @@ int kvm_vcpu_ioctl_set_cpuid(struct kvm_vcpu *vcpu, }for (i =0 ; i < cpuid->nent; i++) { vcpu->arch.cpuid_entries[i].function = cpuid_entries[i].function; - vcpu->arch.cpuid_entries[i].eax = cpuid_entries[i].eax; + vcpu->arch.cpuid_entries[i].eax = cpuid_entries[i].eax & + ~KVM_FEATURE_PV_DEDICATED ; vcpu->arch.cpuid_entries[i].ebx = cpuid_entries[i].ebx; vcpu->arch.cpuid_entries[i].ecx = cpuid_entries[i].ecx; vcpu->arch.cpuid_entries[i].edx = cpuid_entries[i].edx;
Quoted 2 lines
But I do not see any other KVM_FEATURE_* being enforced (e.g.
PV_UNHALT). Do you mind elaborating a bit here?
Sorry, nothing is needed. I somehow though that we need to expose this to the userspace through CPUID, but KVM just needs to consider the flag as reserved.