Skip to contentSkip to conversation

locking/qspinlock/x86: Avoid test-and-set when PV_DEDICATED is set

PATCH Superseded Eduardo Valentin 4 participants 8 messages Oct 24, 2017 – Nov 8, 2017

Subsystems (5)
Documentation x86 KVM KVM x86 +1 more subsystems

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

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.

Cc Paolo Bonzini <[email protected]>
Cc "Radim Krčmář" <[email protected]>
Cc Jonathan Corbet <[email protected]>
Cc Thomas Gleixner <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc "H. Peter Anvin" <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Waiman Long <[email protected]>
Cc Jan H. Schoenherr <[email protected]>
Cc Anthony Liguori <[email protected]>
---
 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
index 3c65feb..117066a 100644
@@ -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
index eaba080..f89b469 100644
@@ -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
index 94dc8ca..ad2e8fe 100644
@@ -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
#1 lore raw

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.

#2 lore raw

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
#3 lore raw
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

#4 lore raw

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
#5 lore raw
In reply to 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_UNHALT flag.

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_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.

Some flag makes sense and we do want to make sure that userspaces don't enable it in pass-through-cpuid mode.

#6 lore raw

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_UNHALT flag.

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_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.

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
index 0099e10..8ceb503 100644
@@ -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
#7 lore raw

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_UNHALT flag.

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 the PV_UNHALT hypercall, 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_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.

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
index 0099e10..8ceb503 100644
@@ -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.