All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH 0/3] KVM: optimize the kvm_vcpu_on_spin
@ 2017-08-07  8:44 Longpeng(Mike)
  2017-08-07  8:44 ` [PATCH 1/3] KVM: add spinlock-exiting optimize framework Longpeng(Mike)
                   ` (2 more replies)
  0 siblings, 3 replies; 11+ messages in thread
From: Longpeng(Mike) @ 2017-08-07  8:44 UTC (permalink / raw)
  To: pbonzini, rkrcmar
  Cc: agraf, borntraeger, cohuck, christoffer.dall, marc.zyngier,
	james.hogan, kvm, linux-kernel, weidong.huang, arei.gonglei,
	wangxinxin.wang, longpeng.mike, david, Longpeng(Mike)

This is a simple optimization for kvm_vcpu_on_spin, the
main idea is described in patch-1's commit msg.

I did some tests base on the RFC version, the result shows
that it can improves the performance slightly.

== Geekbench-3.4.1 ==
VM1: 	8U,4G, vcpu(0...7) is 1:1 pinned to pcpu(6...11,18,19)
	running Geekbench-3.4.1 *10 truns*
VM2/VM3/VM4: configure is the same as VM1
	stress each vcpu usage(seed by top in guest) to 40%

The comparison of each testcase's score:
(higher is better)
		before		after		improve
Inter
 single		1176.7		1179.0		0.2%
 multi		3459.5		3426.5		-0.9%
Float
 single		1150.5		1150.9		0.0%
 multi		3364.5		3391.9		0.8%
Memory(stream)
 single		1768.7		1773.1		0.2%
 multi		2511.6		2557.2		1.8%
Overall
 single		1284.2		1286.2		0.2%
 multi		3231.4		3238.4		0.2%


== kernbench-0.42 ==
VM1:    8U,12G, vcpu(0...7) is 1:1 pinned to pcpu(6...11,18,19)
        running "kernbench -n 10"
VM2/VM3/VM4: configure is the same as VM1
        stress each vcpu usage(seed by top in guest) to 40%

The comparison of 'Elapsed Time':
(sooner is better)
		before		after		improve
load -j4	12.762		12.751		0.1%
load -j32	9.743		8.955		8.1%
load -j		9.688		9.229		4.7%


Physical Machine:
  Architecture:          x86_64
  CPU op-mode(s):        32-bit, 64-bit
  Byte Order:            Little Endian
  CPU(s):                24
  On-line CPU(s) list:   0-23
  Thread(s) per core:    2
  Core(s) per socket:    6
  Socket(s):             2
  NUMA node(s):          2
  Vendor ID:             GenuineIntel
  CPU family:            6
  Model:                 45
  Model name:            Intel(R) Xeon(R) CPU E5-2640 0 @ 2.50GHz
  Stepping:              7
  CPU MHz:               2799.902
  BogoMIPS:              5004.67
  Virtualization:        VT-x
  L1d cache:             32K
  L1i cache:             32K
  L2 cache:              256K
  L3 cache:              15360K
  NUMA node0 CPU(s):     0-5,12-17
  NUMA node1 CPU(s):     6-11,18-23

---
Changes since RFC:
 - only cache result for X86. [David & Cornlia & Paolo]
 - add performance numbers. [David]
 - impls arm/s390. [Christoffer & David]
 - refactor the impls. [me]

---
Longpeng(Mike) (3):
  KVM: add spinlock-exiting optimize framework
  KVM: X86: implement the logic for spinlock optimization
  KVM: implement spinlock optimization logic for arm/s390

 arch/mips/kvm/mips.c            | 10 ++++++++++
 arch/powerpc/kvm/powerpc.c      | 10 ++++++++++
 arch/s390/kvm/kvm-s390.c        | 10 ++++++++++
 arch/x86/include/asm/kvm_host.h |  5 +++++
 arch/x86/kvm/svm.c              |  6 ++++++
 arch/x86/kvm/vmx.c              | 20 ++++++++++++++++++++
 arch/x86/kvm/x86.c              | 15 +++++++++++++++
 include/linux/kvm_host.h        |  2 ++
 virt/kvm/arm/arm.c              | 10 ++++++++++
 virt/kvm/kvm_main.c             |  4 ++++
 10 files changed, 92 insertions(+)

-- 
1.8.3.1

^ permalink raw reply	[flat|nested] 11+ messages in thread

* [PATCH 1/3] KVM: add spinlock-exiting optimize framework
  2017-08-07  8:44 [PATCH 0/3] KVM: optimize the kvm_vcpu_on_spin Longpeng(Mike)
@ 2017-08-07  8:44 ` Longpeng(Mike)
  2017-08-07  8:55   ` David Hildenbrand
  2017-08-07  8:44 ` [PATCH 2/3] KVM: X86: implement the logic for spinlock optimization Longpeng(Mike)
  2017-08-07  8:44 ` [PATCH 3/3] KVM: implement spinlock optimization logic for arm/s390 Longpeng(Mike)
  2 siblings, 1 reply; 11+ messages in thread
From: Longpeng(Mike) @ 2017-08-07  8:44 UTC (permalink / raw)
  To: pbonzini, rkrcmar
  Cc: agraf, borntraeger, cohuck, christoffer.dall, marc.zyngier,
	james.hogan, kvm, linux-kernel, weidong.huang, arei.gonglei,
	wangxinxin.wang, longpeng.mike, david, Longpeng(Mike)

If the vcpu(me) exit due to request a usermode spinlock, then
the spinlock-holder may be preempted in usermode or kernmode.

But if the vcpu(me) is in kernmode, then the holder must be
preempted in kernmode, so we should choose a vcpu in kernmode
as the most eligible candidate.

For some architecture(e.g. arm/s390), spin/preempt_in_kernel()
are the same, but they are different for X86.

Signed-off-by: Longpeng(Mike) <longpeng2@huawei.com>
---
 arch/mips/kvm/mips.c       | 10 ++++++++++
 arch/powerpc/kvm/powerpc.c | 10 ++++++++++
 arch/s390/kvm/kvm-s390.c   | 10 ++++++++++
 arch/x86/kvm/x86.c         | 10 ++++++++++
 include/linux/kvm_host.h   |  2 ++
 virt/kvm/arm/arm.c         | 10 ++++++++++
 virt/kvm/kvm_main.c        |  4 ++++
 7 files changed, 56 insertions(+)

diff --git a/arch/mips/kvm/mips.c b/arch/mips/kvm/mips.c
index d4b2ad1..e04e6b3 100644
--- a/arch/mips/kvm/mips.c
+++ b/arch/mips/kvm/mips.c
@@ -98,6 +98,16 @@ int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu)
 	return !!(vcpu->arch.pending_exceptions);
 }
 
+bool kvm_arch_vcpu_spin_in_kernel(struct kvm_vcpu *vcpu)
+{
+	return false;
+}
+
+bool kvm_arch_vcpu_preempt_in_kernel(struct kvm_vcpu *vcpu)
+{
+	return false;
+}
+
 int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu)
 {
 	return 1;
diff --git a/arch/powerpc/kvm/powerpc.c b/arch/powerpc/kvm/powerpc.c
index 1a75c0b..c573ddd 100644
--- a/arch/powerpc/kvm/powerpc.c
+++ b/arch/powerpc/kvm/powerpc.c
@@ -58,6 +58,16 @@ int kvm_arch_vcpu_runnable(struct kvm_vcpu *v)
 	return !!(v->arch.pending_exceptions) || kvm_request_pending(v);
 }
 
+bool kvm_arch_vcpu_spin_in_kernel(struct kvm_vcpu *vcpu)
+{
+	return false;
+}
+
+bool kvm_arch_vcpu_preempt_in_kernel(struct kvm_vcpu *vcpu)
+{
+	return false;
+}
+
 int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu)
 {
 	return 1;
diff --git a/arch/s390/kvm/kvm-s390.c b/arch/s390/kvm/kvm-s390.c
index af09d34..f78cdc2 100644
--- a/arch/s390/kvm/kvm-s390.c
+++ b/arch/s390/kvm/kvm-s390.c
@@ -2447,6 +2447,16 @@ int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu)
 	return kvm_s390_vcpu_has_irq(vcpu, 0);
 }
 
+bool kvm_arch_vcpu_spin_in_kernel(struct kvm_vcpu *vcpu)
+{
+	return false;
+}
+
+bool kvm_arch_vcpu_preempt_in_kernel(struct kvm_vcpu *vcpu)
+{
+	return false;
+}
+
 void kvm_s390_vcpu_block(struct kvm_vcpu *vcpu)
 {
 	atomic_or(PROG_BLOCK_SIE, &vcpu->arch.sie_block->prog20);
diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
index 6c97c82..04c6a1f 100644
--- a/arch/x86/kvm/x86.c
+++ b/arch/x86/kvm/x86.c
@@ -8435,6 +8435,16 @@ int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu)
 	return kvm_vcpu_running(vcpu) || kvm_vcpu_has_events(vcpu);
 }
 
+bool kvm_arch_vcpu_spin_in_kernel(struct kvm_vcpu *vcpu)
+{
+	return false;
+}
+
+bool kvm_arch_vcpu_preempt_in_kernel(struct kvm_vcpu *vcpu)
+{
+	return false;
+}
+
 int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu)
 {
 	return kvm_vcpu_exiting_guest_mode(vcpu) == IN_GUEST_MODE;
diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h
index 890b706..9613620 100644
--- a/include/linux/kvm_host.h
+++ b/include/linux/kvm_host.h
@@ -798,6 +798,8 @@ int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu,
 void kvm_arch_hardware_unsetup(void);
 void kvm_arch_check_processor_compat(void *rtn);
 int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu);
+bool kvm_arch_vcpu_spin_in_kernel(struct kvm_vcpu *vcpu);
+bool kvm_arch_vcpu_preempt_in_kernel(struct kvm_vcpu *vcpu);
 int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu);
 
 #ifndef __KVM_HAVE_ARCH_VM_ALLOC
diff --git a/virt/kvm/arm/arm.c b/virt/kvm/arm/arm.c
index a39a1e1..e45f780 100644
--- a/virt/kvm/arm/arm.c
+++ b/virt/kvm/arm/arm.c
@@ -416,6 +416,16 @@ int kvm_arch_vcpu_runnable(struct kvm_vcpu *v)
 		&& !v->arch.power_off && !v->arch.pause);
 }
 
+bool kvm_arch_vcpu_spin_in_kernel(struct kvm_vcpu *vcpu)
+{
+	return false;
+}
+
+bool kvm_arch_vcpu_preempt_in_kernel(struct kvm_vcpu *vcpu)
+{
+	return false;
+}
+
 /* Just ensure a guest exit from a particular CPU */
 static void exit_vm_noop(void *info)
 {
diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c
index f3f7427..0d0527b 100644
--- a/virt/kvm/kvm_main.c
+++ b/virt/kvm/kvm_main.c
@@ -2324,12 +2324,14 @@ void kvm_vcpu_on_spin(struct kvm_vcpu *me)
 {
 	struct kvm *kvm = me->kvm;
 	struct kvm_vcpu *vcpu;
+	bool in_kern;
 	int last_boosted_vcpu = me->kvm->last_boosted_vcpu;
 	int yielded = 0;
 	int try = 3;
 	int pass;
 	int i;
 
+	in_kern = kvm_arch_vcpu_spin_in_kernel(me);
 	kvm_vcpu_set_in_spin_loop(me, true);
 	/*
 	 * We boost the priority of a VCPU that is runnable but not
@@ -2351,6 +2353,8 @@ void kvm_vcpu_on_spin(struct kvm_vcpu *me)
 				continue;
 			if (swait_active(&vcpu->wq) && !kvm_arch_vcpu_runnable(vcpu))
 				continue;
+			if (in_kern && !kvm_arch_vcpu_preempt_in_kernel(vcpu))
+				continue;
 			if (!kvm_vcpu_eligible_for_directed_yield(vcpu))
 				continue;
 
-- 
1.8.3.1

^ permalink raw reply related	[flat|nested] 11+ messages in thread

* [PATCH 2/3] KVM: X86: implement the logic for spinlock optimization
  2017-08-07  8:44 [PATCH 0/3] KVM: optimize the kvm_vcpu_on_spin Longpeng(Mike)
  2017-08-07  8:44 ` [PATCH 1/3] KVM: add spinlock-exiting optimize framework Longpeng(Mike)
@ 2017-08-07  8:44 ` Longpeng(Mike)
  2017-08-07 10:45   ` Paolo Bonzini
  2017-08-07  8:44 ` [PATCH 3/3] KVM: implement spinlock optimization logic for arm/s390 Longpeng(Mike)
  2 siblings, 1 reply; 11+ messages in thread
From: Longpeng(Mike) @ 2017-08-07  8:44 UTC (permalink / raw)
  To: pbonzini, rkrcmar
  Cc: agraf, borntraeger, cohuck, christoffer.dall, marc.zyngier,
	james.hogan, kvm, linux-kernel, weidong.huang, arei.gonglei,
	wangxinxin.wang, longpeng.mike, david, Longpeng(Mike)

Implements the kvm_arch_vcpu_spin/preempt_in_kernel(), because get_cpl
requires vcpu_load, so we must cache the result(whether the vcpu was
preempted when its cpl=0) in kvm_arch_vcpu.

Signed-off-by: Longpeng(Mike) <longpeng2@huawei.com>
---
 arch/x86/include/asm/kvm_host.h |  5 +++++
 arch/x86/kvm/svm.c              |  6 ++++++
 arch/x86/kvm/vmx.c              | 20 ++++++++++++++++++++
 arch/x86/kvm/x86.c              |  9 +++++++--
 4 files changed, 38 insertions(+), 2 deletions(-)

diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h
index 87ac4fb..d2b2d57 100644
--- a/arch/x86/include/asm/kvm_host.h
+++ b/arch/x86/include/asm/kvm_host.h
@@ -688,6 +688,9 @@ struct kvm_vcpu_arch {
 
 	/* GPA available (AMD only) */
 	bool gpa_available;
+
+	/* be preempted when it's in kernel-mode(cpl=0) */
+	bool preempted_in_kernel;
 };
 
 struct kvm_lpage_info {
@@ -1057,6 +1060,8 @@ struct kvm_x86_ops {
 	void (*cancel_hv_timer)(struct kvm_vcpu *vcpu);
 
 	void (*setup_mce)(struct kvm_vcpu *vcpu);
+
+	bool (*spin_in_kernel)(struct kvm_vcpu *vcpu);
 };
 
 struct kvm_arch_async_pf {
diff --git a/arch/x86/kvm/svm.c b/arch/x86/kvm/svm.c
index 4d8141e..552ab4c 100644
--- a/arch/x86/kvm/svm.c
+++ b/arch/x86/kvm/svm.c
@@ -5352,6 +5352,11 @@ static void svm_setup_mce(struct kvm_vcpu *vcpu)
 	vcpu->arch.mcg_cap &= 0x1ff;
 }
 
+static bool svm_spin_in_kernel(struct kvm_vcpu *vcpu)
+{
+	return svm_get_cpl(vcpu) == 0;
+}
+
 static struct kvm_x86_ops svm_x86_ops __ro_after_init = {
 	.cpu_has_kvm_support = has_svm,
 	.disabled_by_bios = is_disabled,
@@ -5464,6 +5469,7 @@ static void svm_setup_mce(struct kvm_vcpu *vcpu)
 	.deliver_posted_interrupt = svm_deliver_avic_intr,
 	.update_pi_irte = svm_update_pi_irte,
 	.setup_mce = svm_setup_mce,
+	.spin_in_kernel = svm_spin_in_kernel,
 };
 
 static int __init svm_init(void)
diff --git a/arch/x86/kvm/vmx.c b/arch/x86/kvm/vmx.c
index 39a6222..d0dfe2e 100644
--- a/arch/x86/kvm/vmx.c
+++ b/arch/x86/kvm/vmx.c
@@ -11547,6 +11547,25 @@ static void vmx_setup_mce(struct kvm_vcpu *vcpu)
 			~FEATURE_CONTROL_LMCE;
 }
 
+static bool vmx_spin_in_kernel(struct kvm_vcpu *vcpu)
+{
+	u32 secondary_exec_ctrl = 0;
+
+	/*
+	 * Intel sdm vol3 ch-25.1.3 says: The “PAUSE-loop exiting”
+	 * VM-execution control is ignored if CPL > 0. So the vcpu
+	 * is always exiting with CPL=0 if it uses PLE.
+	 *
+	 * The following block needs less cycles than vmx_get_cpl().
+	 */
+	if (cpu_has_secondary_exec_ctrls())
+		secondary_exec_ctrl = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
+	if (secondary_exec_ctrl & SECONDARY_EXEC_PAUSE_LOOP_EXITING)
+		return true;
+
+	return vmx_get_cpl(vcpu) == 0;
+}
+
 static struct kvm_x86_ops vmx_x86_ops __ro_after_init = {
 	.cpu_has_kvm_support = cpu_has_kvm_support,
 	.disabled_by_bios = vmx_disabled_by_bios,
@@ -11674,6 +11693,7 @@ static void vmx_setup_mce(struct kvm_vcpu *vcpu)
 #endif
 
 	.setup_mce = vmx_setup_mce,
+	.spin_in_kernel = vmx_spin_in_kernel,
 };
 
 static int __init vmx_init(void)
diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
index 04c6a1f..fa79a60 100644
--- a/arch/x86/kvm/x86.c
+++ b/arch/x86/kvm/x86.c
@@ -2881,6 +2881,10 @@ static void kvm_steal_time_set_preempted(struct kvm_vcpu *vcpu)
 void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
 {
 	int idx;
+
+	if (vcpu->preempted)
+		vcpu->arch.preempted_in_kernel = !kvm_x86_ops->get_cpl(vcpu);
+
 	/*
 	 * Disable page faults because we're in atomic context here.
 	 * kvm_write_guest_offset_cached() would call might_fault()
@@ -7988,6 +7992,7 @@ int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu)
 	kvm_pmu_init(vcpu);
 
 	vcpu->arch.pending_external_vector = -1;
+	vcpu->arch.preempted_in_kernel = false;
 
 	kvm_hv_vcpu_init(vcpu);
 
@@ -8437,12 +8442,12 @@ int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu)
 
 bool kvm_arch_vcpu_spin_in_kernel(struct kvm_vcpu *vcpu)
 {
-	return false;
+	return kvm_x86_ops->spin_in_kernel(vcpu);
 }
 
 bool kvm_arch_vcpu_preempt_in_kernel(struct kvm_vcpu *vcpu)
 {
-	return false;
+	return vcpu->arch.preempted_in_kernel;
 }
 
 int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu)
-- 
1.8.3.1

^ permalink raw reply related	[flat|nested] 11+ messages in thread

* [PATCH 3/3] KVM: implement spinlock optimization logic for arm/s390
  2017-08-07  8:44 [PATCH 0/3] KVM: optimize the kvm_vcpu_on_spin Longpeng(Mike)
  2017-08-07  8:44 ` [PATCH 1/3] KVM: add spinlock-exiting optimize framework Longpeng(Mike)
  2017-08-07  8:44 ` [PATCH 2/3] KVM: X86: implement the logic for spinlock optimization Longpeng(Mike)
@ 2017-08-07  8:44 ` Longpeng(Mike)
  2017-08-07  8:52   ` David Hildenbrand
  2 siblings, 1 reply; 11+ messages in thread
From: Longpeng(Mike) @ 2017-08-07  8:44 UTC (permalink / raw)
  To: pbonzini, rkrcmar
  Cc: agraf, borntraeger, cohuck, christoffer.dall, marc.zyngier,
	james.hogan, kvm, linux-kernel, weidong.huang, arei.gonglei,
	wangxinxin.wang, longpeng.mike, david, Longpeng(Mike)

Implements the kvm_arch_vcpu_spin/preempt_in_kernel() for arm/s390,
they needn't cache the result.

Signed-off-by: Longpeng(Mike) <longpeng2@huawei.com>
---
 arch/s390/kvm/kvm-s390.c | 4 ++--
 virt/kvm/arm/arm.c       | 4 ++--
 2 files changed, 4 insertions(+), 4 deletions(-)

diff --git a/arch/s390/kvm/kvm-s390.c b/arch/s390/kvm/kvm-s390.c
index f78cdc2..49b9178 100644
--- a/arch/s390/kvm/kvm-s390.c
+++ b/arch/s390/kvm/kvm-s390.c
@@ -2449,12 +2449,12 @@ int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu)
 
 bool kvm_arch_vcpu_spin_in_kernel(struct kvm_vcpu *vcpu)
 {
-	return false;
+	return !(vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE);
 }
 
 bool kvm_arch_vcpu_preempt_in_kernel(struct kvm_vcpu *vcpu)
 {
-	return false;
+	return !(vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE);
 }
 
 void kvm_s390_vcpu_block(struct kvm_vcpu *vcpu)
diff --git a/virt/kvm/arm/arm.c b/virt/kvm/arm/arm.c
index e45f780..956f025 100644
--- a/virt/kvm/arm/arm.c
+++ b/virt/kvm/arm/arm.c
@@ -418,12 +418,12 @@ int kvm_arch_vcpu_runnable(struct kvm_vcpu *v)
 
 bool kvm_arch_vcpu_spin_in_kernel(struct kvm_vcpu *vcpu)
 {
-	return false;
+	return vcpu_mode_priv(vcpu);
 }
 
 bool kvm_arch_vcpu_preempt_in_kernel(struct kvm_vcpu *vcpu)
 {
-	return false;
+	return vcpu_mode_priv(vcpu);
 }
 
 /* Just ensure a guest exit from a particular CPU */
-- 
1.8.3.1

^ permalink raw reply related	[flat|nested] 11+ messages in thread

* Re: [PATCH 3/3] KVM: implement spinlock optimization logic for arm/s390
  2017-08-07  8:44 ` [PATCH 3/3] KVM: implement spinlock optimization logic for arm/s390 Longpeng(Mike)
@ 2017-08-07  8:52   ` David Hildenbrand
  2017-08-07  8:54     ` Longpeng (Mike)
  0 siblings, 1 reply; 11+ messages in thread
From: David Hildenbrand @ 2017-08-07  8:52 UTC (permalink / raw)
  To: Longpeng(Mike), pbonzini, rkrcmar
  Cc: agraf, borntraeger, cohuck, christoffer.dall, marc.zyngier,
	james.hogan, kvm, linux-kernel, weidong.huang, arei.gonglei,
	wangxinxin.wang, longpeng.mike

On 07.08.2017 10:44, Longpeng(Mike) wrote:
> Implements the kvm_arch_vcpu_spin/preempt_in_kernel() for arm/s390,
> they needn't cache the result.
> 
> Signed-off-by: Longpeng(Mike) <longpeng2@huawei.com>
> ---
>  arch/s390/kvm/kvm-s390.c | 4 ++--
>  virt/kvm/arm/arm.c       | 4 ++--
>  2 files changed, 4 insertions(+), 4 deletions(-)
> 
> diff --git a/arch/s390/kvm/kvm-s390.c b/arch/s390/kvm/kvm-s390.c
> index f78cdc2..49b9178 100644
> --- a/arch/s390/kvm/kvm-s390.c
> +++ b/arch/s390/kvm/kvm-s390.c
> @@ -2449,12 +2449,12 @@ int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu)
>  
>  bool kvm_arch_vcpu_spin_in_kernel(struct kvm_vcpu *vcpu)
>  {
> -	return false;
> +	return !(vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE);
>  }
>  
>  bool kvm_arch_vcpu_preempt_in_kernel(struct kvm_vcpu *vcpu)
>  {
> -	return false;
> +	return !(vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE);
>  }
>  
>  void kvm_s390_vcpu_block(struct kvm_vcpu *vcpu)
> diff --git a/virt/kvm/arm/arm.c b/virt/kvm/arm/arm.c
> index e45f780..956f025 100644
> --- a/virt/kvm/arm/arm.c
> +++ b/virt/kvm/arm/arm.c
> @@ -418,12 +418,12 @@ int kvm_arch_vcpu_runnable(struct kvm_vcpu *v)
>  
>  bool kvm_arch_vcpu_spin_in_kernel(struct kvm_vcpu *vcpu)
>  {
> -	return false;
> +	return vcpu_mode_priv(vcpu);
>  }
>  
>  bool kvm_arch_vcpu_preempt_in_kernel(struct kvm_vcpu *vcpu)
>  {
> -	return false;
> +	return vcpu_mode_priv(vcpu);
>  }
>  
>  /* Just ensure a guest exit from a particular CPU */
> 

Can you split that into two parts? (arm and s390x?)

-- 

Thanks,

David

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH 3/3] KVM: implement spinlock optimization logic for arm/s390
  2017-08-07  8:52   ` David Hildenbrand
@ 2017-08-07  8:54     ` Longpeng (Mike)
  0 siblings, 0 replies; 11+ messages in thread
From: Longpeng (Mike) @ 2017-08-07  8:54 UTC (permalink / raw)
  To: David Hildenbrand
  Cc: pbonzini, rkrcmar, agraf, borntraeger, cohuck, christoffer.dall,
	marc.zyngier, james.hogan, kvm, linux-kernel, weidong.huang,
	arei.gonglei, wangxinxin.wang, longpeng.mike


On 2017/8/7 16:52, David Hildenbrand wrote:

> On 07.08.2017 10:44, Longpeng(Mike) wrote:
>> Implements the kvm_arch_vcpu_spin/preempt_in_kernel() for arm/s390,
>> they needn't cache the result.
>>
>> Signed-off-by: Longpeng(Mike) <longpeng2@huawei.com>
>> ---
>>  arch/s390/kvm/kvm-s390.c | 4 ++--
>>  virt/kvm/arm/arm.c       | 4 ++--
>>  2 files changed, 4 insertions(+), 4 deletions(-)
>>
>> diff --git a/arch/s390/kvm/kvm-s390.c b/arch/s390/kvm/kvm-s390.c
>> index f78cdc2..49b9178 100644
>> --- a/arch/s390/kvm/kvm-s390.c
>> +++ b/arch/s390/kvm/kvm-s390.c
>> @@ -2449,12 +2449,12 @@ int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu)
>>  
>>  bool kvm_arch_vcpu_spin_in_kernel(struct kvm_vcpu *vcpu)
>>  {
>> -	return false;
>> +	return !(vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE);
>>  }
>>  
>>  bool kvm_arch_vcpu_preempt_in_kernel(struct kvm_vcpu *vcpu)
>>  {
>> -	return false;
>> +	return !(vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE);
>>  }
>>  
>>  void kvm_s390_vcpu_block(struct kvm_vcpu *vcpu)
>> diff --git a/virt/kvm/arm/arm.c b/virt/kvm/arm/arm.c
>> index e45f780..956f025 100644
>> --- a/virt/kvm/arm/arm.c
>> +++ b/virt/kvm/arm/arm.c
>> @@ -418,12 +418,12 @@ int kvm_arch_vcpu_runnable(struct kvm_vcpu *v)
>>  
>>  bool kvm_arch_vcpu_spin_in_kernel(struct kvm_vcpu *vcpu)
>>  {
>> -	return false;
>> +	return vcpu_mode_priv(vcpu);
>>  }
>>  
>>  bool kvm_arch_vcpu_preempt_in_kernel(struct kvm_vcpu *vcpu)
>>  {
>> -	return false;
>> +	return vcpu_mode_priv(vcpu);
>>  }
>>  
>>  /* Just ensure a guest exit from a particular CPU */
>>
> 
> Can you split that into two parts? (arm and s390x?)


OK, I'll split in V2. :)

> 


-- 
Regards,
Longpeng(Mike)

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH 1/3] KVM: add spinlock-exiting optimize framework
  2017-08-07  8:44 ` [PATCH 1/3] KVM: add spinlock-exiting optimize framework Longpeng(Mike)
@ 2017-08-07  8:55   ` David Hildenbrand
  2017-08-07  9:04     ` Longpeng (Mike)
  0 siblings, 1 reply; 11+ messages in thread
From: David Hildenbrand @ 2017-08-07  8:55 UTC (permalink / raw)
  To: Longpeng(Mike), pbonzini, rkrcmar
  Cc: agraf, borntraeger, cohuck, christoffer.dall, marc.zyngier,
	james.hogan, kvm, linux-kernel, weidong.huang, arei.gonglei,
	wangxinxin.wang, longpeng.mike

On 07.08.2017 10:44, Longpeng(Mike) wrote:
> If the vcpu(me) exit due to request a usermode spinlock, then
> the spinlock-holder may be preempted in usermode or kernmode.
> 
> But if the vcpu(me) is in kernmode, then the holder must be
> preempted in kernmode, so we should choose a vcpu in kernmode
> as the most eligible candidate.
> 
> For some architecture(e.g. arm/s390), spin/preempt_in_kernel()
> are the same, but they are different for X86.
> 
> Signed-off-by: Longpeng(Mike) <longpeng2@huawei.com>
> ---
>  arch/mips/kvm/mips.c       | 10 ++++++++++
>  arch/powerpc/kvm/powerpc.c | 10 ++++++++++
>  arch/s390/kvm/kvm-s390.c   | 10 ++++++++++
>  arch/x86/kvm/x86.c         | 10 ++++++++++
>  include/linux/kvm_host.h   |  2 ++
>  virt/kvm/arm/arm.c         | 10 ++++++++++
>  virt/kvm/kvm_main.c        |  4 ++++
>  7 files changed, 56 insertions(+)
> 
> diff --git a/arch/mips/kvm/mips.c b/arch/mips/kvm/mips.c
> index d4b2ad1..e04e6b3 100644
> --- a/arch/mips/kvm/mips.c
> +++ b/arch/mips/kvm/mips.c
> @@ -98,6 +98,16 @@ int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu)
>  	return !!(vcpu->arch.pending_exceptions);
>  }
>  
> +bool kvm_arch_vcpu_spin_in_kernel(struct kvm_vcpu *vcpu)
> +{
> +	return false;
> +}
> +
> +bool kvm_arch_vcpu_preempt_in_kernel(struct kvm_vcpu *vcpu)
> +{
> +	return false;
> +}
> +
>  int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu)
>  {
>  	return 1;
> diff --git a/arch/powerpc/kvm/powerpc.c b/arch/powerpc/kvm/powerpc.c
> index 1a75c0b..c573ddd 100644
> --- a/arch/powerpc/kvm/powerpc.c
> +++ b/arch/powerpc/kvm/powerpc.c
> @@ -58,6 +58,16 @@ int kvm_arch_vcpu_runnable(struct kvm_vcpu *v)
>  	return !!(v->arch.pending_exceptions) || kvm_request_pending(v);
>  }
>  
> +bool kvm_arch_vcpu_spin_in_kernel(struct kvm_vcpu *vcpu)
> +{
> +	return false;
> +}
> +
> +bool kvm_arch_vcpu_preempt_in_kernel(struct kvm_vcpu *vcpu)
> +{
> +	return false;
> +}
> +
>  int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu)
>  {
>  	return 1;
> diff --git a/arch/s390/kvm/kvm-s390.c b/arch/s390/kvm/kvm-s390.c
> index af09d34..f78cdc2 100644
> --- a/arch/s390/kvm/kvm-s390.c
> +++ b/arch/s390/kvm/kvm-s390.c
> @@ -2447,6 +2447,16 @@ int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu)
>  	return kvm_s390_vcpu_has_irq(vcpu, 0);
>  }
>  
> +bool kvm_arch_vcpu_spin_in_kernel(struct kvm_vcpu *vcpu)
> +{
> +	return false;
> +}
> +
> +bool kvm_arch_vcpu_preempt_in_kernel(struct kvm_vcpu *vcpu)
> +{
> +	return false;
> +}
> +
>  void kvm_s390_vcpu_block(struct kvm_vcpu *vcpu)
>  {
>  	atomic_or(PROG_BLOCK_SIE, &vcpu->arch.sie_block->prog20);
> diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
> index 6c97c82..04c6a1f 100644
> --- a/arch/x86/kvm/x86.c
> +++ b/arch/x86/kvm/x86.c
> @@ -8435,6 +8435,16 @@ int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu)
>  	return kvm_vcpu_running(vcpu) || kvm_vcpu_has_events(vcpu);
>  }
>  
> +bool kvm_arch_vcpu_spin_in_kernel(struct kvm_vcpu *vcpu)
> +{
> +	return false;
> +}
> +
> +bool kvm_arch_vcpu_preempt_in_kernel(struct kvm_vcpu *vcpu)
> +{
> +	return false;
> +}
> +
>  int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu)
>  {
>  	return kvm_vcpu_exiting_guest_mode(vcpu) == IN_GUEST_MODE;
> diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h
> index 890b706..9613620 100644
> --- a/include/linux/kvm_host.h
> +++ b/include/linux/kvm_host.h
> @@ -798,6 +798,8 @@ int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu,
>  void kvm_arch_hardware_unsetup(void);
>  void kvm_arch_check_processor_compat(void *rtn);
>  int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu);
> +bool kvm_arch_vcpu_spin_in_kernel(struct kvm_vcpu *vcpu);
> +bool kvm_arch_vcpu_preempt_in_kernel(struct kvm_vcpu *vcpu);
>  int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu);
>  
>  #ifndef __KVM_HAVE_ARCH_VM_ALLOC
> diff --git a/virt/kvm/arm/arm.c b/virt/kvm/arm/arm.c
> index a39a1e1..e45f780 100644
> --- a/virt/kvm/arm/arm.c
> +++ b/virt/kvm/arm/arm.c
> @@ -416,6 +416,16 @@ int kvm_arch_vcpu_runnable(struct kvm_vcpu *v)
>  		&& !v->arch.power_off && !v->arch.pause);
>  }
>  
> +bool kvm_arch_vcpu_spin_in_kernel(struct kvm_vcpu *vcpu)
> +{
> +	return false;
> +}
> +
> +bool kvm_arch_vcpu_preempt_in_kernel(struct kvm_vcpu *vcpu)
> +{
> +	return false;
> +}

Is the differentiation really necessary?

Can't you cache for x86 in all scenarios and simply introduce
kvm_arch_vcpu_in_kernel() ?

Otherwise, we have complexity that might just be avoided (e.g.
kvm_arch_vcpu_spin_in_kernel must only be called on the loaded VCPU)

> +
>  /* Just ensure a guest exit from a particular CPU */
>  static void exit_vm_noop(void *info)
>  {
> diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c
> index f3f7427..0d0527b 100644
> --- a/virt/kvm/kvm_main.c
> +++ b/virt/kvm/kvm_main.c
> @@ -2324,12 +2324,14 @@ void kvm_vcpu_on_spin(struct kvm_vcpu *me)
>  {
>  	struct kvm *kvm = me->kvm;
>  	struct kvm_vcpu *vcpu;
> +	bool in_kern;
>  	int last_boosted_vcpu = me->kvm->last_boosted_vcpu;
>  	int yielded = 0;
>  	int try = 3;
>  	int pass;
>  	int i;
>  
> +	in_kern = kvm_arch_vcpu_spin_in_kernel(me);
>  	kvm_vcpu_set_in_spin_loop(me, true);
>  	/*
>  	 * We boost the priority of a VCPU that is runnable but not
> @@ -2351,6 +2353,8 @@ void kvm_vcpu_on_spin(struct kvm_vcpu *me)
>  				continue;
>  			if (swait_active(&vcpu->wq) && !kvm_arch_vcpu_runnable(vcpu))
>  				continue;
> +			if (in_kern && !kvm_arch_vcpu_preempt_in_kernel(vcpu))
> +				continue;
>  			if (!kvm_vcpu_eligible_for_directed_yield(vcpu))
>  				continue;
>  
> 


-- 

Thanks,

David

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH 1/3] KVM: add spinlock-exiting optimize framework
  2017-08-07  8:55   ` David Hildenbrand
@ 2017-08-07  9:04     ` Longpeng (Mike)
  0 siblings, 0 replies; 11+ messages in thread
From: Longpeng (Mike) @ 2017-08-07  9:04 UTC (permalink / raw)
  To: David Hildenbrand
  Cc: pbonzini, rkrcmar, agraf, borntraeger, cohuck, christoffer.dall,
	marc.zyngier, james.hogan, kvm, linux-kernel, weidong.huang,
	arei.gonglei, wangxinxin.wang, longpeng.mike



On 2017/8/7 16:55, David Hildenbrand wrote:

> On 07.08.2017 10:44, Longpeng(Mike) wrote:
>> If the vcpu(me) exit due to request a usermode spinlock, then
>> the spinlock-holder may be preempted in usermode or kernmode.
>>
>> But if the vcpu(me) is in kernmode, then the holder must be
>> preempted in kernmode, so we should choose a vcpu in kernmode
>> as the most eligible candidate.
>>
>> For some architecture(e.g. arm/s390), spin/preempt_in_kernel()
>> are the same, but they are different for X86.
>>
>> Signed-off-by: Longpeng(Mike) <longpeng2@huawei.com>
>> ---
>>  arch/mips/kvm/mips.c       | 10 ++++++++++
>>  arch/powerpc/kvm/powerpc.c | 10 ++++++++++
>>  arch/s390/kvm/kvm-s390.c   | 10 ++++++++++
>>  arch/x86/kvm/x86.c         | 10 ++++++++++
>>  include/linux/kvm_host.h   |  2 ++
>>  virt/kvm/arm/arm.c         | 10 ++++++++++
>>  virt/kvm/kvm_main.c        |  4 ++++
>>  7 files changed, 56 insertions(+)
>>
>> diff --git a/arch/mips/kvm/mips.c b/arch/mips/kvm/mips.c
>> index d4b2ad1..e04e6b3 100644
>> --- a/arch/mips/kvm/mips.c
>> +++ b/arch/mips/kvm/mips.c
>> @@ -98,6 +98,16 @@ int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu)
>>  	return !!(vcpu->arch.pending_exceptions);
>>  }
>>  
>> +bool kvm_arch_vcpu_spin_in_kernel(struct kvm_vcpu *vcpu)
>> +{
>> +	return false;
>> +}
>> +
>> +bool kvm_arch_vcpu_preempt_in_kernel(struct kvm_vcpu *vcpu)
>> +{
>> +	return false;
>> +}
>> +
>>  int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu)
>>  {
>>  	return 1;
>> diff --git a/arch/powerpc/kvm/powerpc.c b/arch/powerpc/kvm/powerpc.c
>> index 1a75c0b..c573ddd 100644
>> --- a/arch/powerpc/kvm/powerpc.c
>> +++ b/arch/powerpc/kvm/powerpc.c
>> @@ -58,6 +58,16 @@ int kvm_arch_vcpu_runnable(struct kvm_vcpu *v)
>>  	return !!(v->arch.pending_exceptions) || kvm_request_pending(v);
>>  }
>>  
>> +bool kvm_arch_vcpu_spin_in_kernel(struct kvm_vcpu *vcpu)
>> +{
>> +	return false;
>> +}
>> +
>> +bool kvm_arch_vcpu_preempt_in_kernel(struct kvm_vcpu *vcpu)
>> +{
>> +	return false;
>> +}
>> +
>>  int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu)
>>  {
>>  	return 1;
>> diff --git a/arch/s390/kvm/kvm-s390.c b/arch/s390/kvm/kvm-s390.c
>> index af09d34..f78cdc2 100644
>> --- a/arch/s390/kvm/kvm-s390.c
>> +++ b/arch/s390/kvm/kvm-s390.c
>> @@ -2447,6 +2447,16 @@ int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu)
>>  	return kvm_s390_vcpu_has_irq(vcpu, 0);
>>  }
>>  
>> +bool kvm_arch_vcpu_spin_in_kernel(struct kvm_vcpu *vcpu)
>> +{
>> +	return false;
>> +}
>> +
>> +bool kvm_arch_vcpu_preempt_in_kernel(struct kvm_vcpu *vcpu)
>> +{
>> +	return false;
>> +}
>> +
>>  void kvm_s390_vcpu_block(struct kvm_vcpu *vcpu)
>>  {
>>  	atomic_or(PROG_BLOCK_SIE, &vcpu->arch.sie_block->prog20);
>> diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
>> index 6c97c82..04c6a1f 100644
>> --- a/arch/x86/kvm/x86.c
>> +++ b/arch/x86/kvm/x86.c
>> @@ -8435,6 +8435,16 @@ int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu)
>>  	return kvm_vcpu_running(vcpu) || kvm_vcpu_has_events(vcpu);
>>  }
>>  
>> +bool kvm_arch_vcpu_spin_in_kernel(struct kvm_vcpu *vcpu)
>> +{
>> +	return false;
>> +}
>> +
>> +bool kvm_arch_vcpu_preempt_in_kernel(struct kvm_vcpu *vcpu)
>> +{
>> +	return false;
>> +}
>> +
>>  int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu)
>>  {
>>  	return kvm_vcpu_exiting_guest_mode(vcpu) == IN_GUEST_MODE;
>> diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h
>> index 890b706..9613620 100644
>> --- a/include/linux/kvm_host.h
>> +++ b/include/linux/kvm_host.h
>> @@ -798,6 +798,8 @@ int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu,
>>  void kvm_arch_hardware_unsetup(void);
>>  void kvm_arch_check_processor_compat(void *rtn);
>>  int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu);
>> +bool kvm_arch_vcpu_spin_in_kernel(struct kvm_vcpu *vcpu);
>> +bool kvm_arch_vcpu_preempt_in_kernel(struct kvm_vcpu *vcpu);
>>  int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu);
>>  
>>  #ifndef __KVM_HAVE_ARCH_VM_ALLOC
>> diff --git a/virt/kvm/arm/arm.c b/virt/kvm/arm/arm.c
>> index a39a1e1..e45f780 100644
>> --- a/virt/kvm/arm/arm.c
>> +++ b/virt/kvm/arm/arm.c
>> @@ -416,6 +416,16 @@ int kvm_arch_vcpu_runnable(struct kvm_vcpu *v)
>>  		&& !v->arch.power_off && !v->arch.pause);
>>  }
>>  
>> +bool kvm_arch_vcpu_spin_in_kernel(struct kvm_vcpu *vcpu)
>> +{
>> +	return false;
>> +}
>> +
>> +bool kvm_arch_vcpu_preempt_in_kernel(struct kvm_vcpu *vcpu)
>> +{
>> +	return false;
>> +}
> 
> Is the differentiation really necessary?
> 
> Can't you cache for x86 in all scenarios and simply introduce
> kvm_arch_vcpu_in_kernel() ?
> 


For X86 this is necessary,  I have no idea how to avoid this, hopes
someone could give me some suggestion. :)

> Otherwise, we have complexity that might just be avoided (e.g.
> kvm_arch_vcpu_spin_in_kernel must only be called on the loaded VCPU)
> 
>> +
>>  /* Just ensure a guest exit from a particular CPU */
>>  static void exit_vm_noop(void *info)
>>  {
>> diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c
>> index f3f7427..0d0527b 100644
>> --- a/virt/kvm/kvm_main.c
>> +++ b/virt/kvm/kvm_main.c
>> @@ -2324,12 +2324,14 @@ void kvm_vcpu_on_spin(struct kvm_vcpu *me)
>>  {
>>  	struct kvm *kvm = me->kvm;
>>  	struct kvm_vcpu *vcpu;
>> +	bool in_kern;
>>  	int last_boosted_vcpu = me->kvm->last_boosted_vcpu;
>>  	int yielded = 0;
>>  	int try = 3;
>>  	int pass;
>>  	int i;
>>  
>> +	in_kern = kvm_arch_vcpu_spin_in_kernel(me);
>>  	kvm_vcpu_set_in_spin_loop(me, true);
>>  	/*
>>  	 * We boost the priority of a VCPU that is runnable but not
>> @@ -2351,6 +2353,8 @@ void kvm_vcpu_on_spin(struct kvm_vcpu *me)
>>  				continue;
>>  			if (swait_active(&vcpu->wq) && !kvm_arch_vcpu_runnable(vcpu))
>>  				continue;
>> +			if (in_kern && !kvm_arch_vcpu_preempt_in_kernel(vcpu))
>> +				continue;
>>  			if (!kvm_vcpu_eligible_for_directed_yield(vcpu))
>>  				continue;
>>  
>>
> 
> 


-- 
Regards,
Longpeng(Mike)

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH 2/3] KVM: X86: implement the logic for spinlock optimization
  2017-08-07  8:44 ` [PATCH 2/3] KVM: X86: implement the logic for spinlock optimization Longpeng(Mike)
@ 2017-08-07 10:45   ` Paolo Bonzini
  2017-08-07 12:28     ` Longpeng(Mike)
  0 siblings, 1 reply; 11+ messages in thread
From: Paolo Bonzini @ 2017-08-07 10:45 UTC (permalink / raw)
  To: Longpeng(Mike), rkrcmar
  Cc: agraf, borntraeger, cohuck, christoffer.dall, marc.zyngier,
	james.hogan, kvm, linux-kernel, weidong.huang, arei.gonglei,
	wangxinxin.wang, longpeng.mike, david

On 07/08/2017 10:44, Longpeng(Mike) wrote:
> +
> +	/*
> +	 * Intel sdm vol3 ch-25.1.3 says: The “PAUSE-loop exiting”
> +	 * VM-execution control is ignored if CPL > 0. So the vcpu
> +	 * is always exiting with CPL=0 if it uses PLE.

This is not true (how can it be?).  What 25.1.3 says is, the VCPU is
always at CPL=0 if you get a PAUSE exit (reason 40) and PAUSE exiting is
0 (it always is for KVM).  But here you're looking for a VCPU that
didn't get a PAUSE exit, so the CPL can certainly be 3.

However, I understand that vmx_get_cpl can be a bit slow here.  You can
actually read SS's access rights directly in this function and get the
DPL from there, that's going to be just a single VMREAD.

The only difference is when vmx->rmode.vm86_active=1.  However,
pause-loop exiting is not working properly anyway if
vmx->rmode.vm86_active=1, because CPL=3 according to the processor.

Paolo

> +	 * The following block needs less cycles than vmx_get_cpl().
> +	 */
> +	if (cpu_has_secondary_exec_ctrls())
> +		secondary_exec_ctrl = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
> +	if (secondary_exec_ctrl & SECONDARY_EXEC_PAUSE_LOOP_EXITING)
> +		return true;
> +

Paolo

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH 2/3] KVM: X86: implement the logic for spinlock optimization
  2017-08-07 10:45   ` Paolo Bonzini
@ 2017-08-07 12:28     ` Longpeng(Mike)
  2017-08-07 13:16       ` Paolo Bonzini
  0 siblings, 1 reply; 11+ messages in thread
From: Longpeng(Mike) @ 2017-08-07 12:28 UTC (permalink / raw)
  To: Paolo Bonzini
  Cc: Longpeng(Mike),
	rkrcmar, agraf, borntraeger, cohuck, christoffer.dall,
	marc.zyngier, james.hogan, kvm, linux-kernel, weidong.huang,
	arei.gonglei, wangxinxin.wang, david



On 08/07/2017 06:45 PM, Paolo Bonzini wrote:
> On 07/08/2017 10:44, Longpeng(Mike) wrote:
>> +
>> +	/*
>> +	 * Intel sdm vol3 ch-25.1.3 says: The “PAUSE-loop exiting”
>> +	 * VM-execution control is ignored if CPL > 0. So the vcpu
>> +	 * is always exiting with CPL=0 if it uses PLE.
> 
> This is not true (how can it be?).  What 25.1.3 says is, the VCPU is
> always at CPL=0 if you get a PAUSE exit (reason 40) and PAUSE exiting is
> 0 (it always is for KVM).  But here you're looking for a VCPU that
> didn't get a PAUSE exit, so the CPL can certainly be 3.
> 

Hi Paolo,

My comment above is something wrong(please forgive my poor English), my 
origin meaning is:
	The “PAUSE-loop exiting” VM-execution control is ignored if
	CPL > 0. So the vcpu's CPL is must 0 if it exits due to PLE.

* kvm_arch_spin_in_kernel() returns whether the vcpu(which exits due to 
spinlock) is CPL=0. It only be called by kvm_vcpu_on_spin(), and the 
input vcpu is 'me' which get a PAUSE exit now. *

I split kvm_arch_vcpu_in_kernel(in RFC) into two functions: 
kvm_arch_spin_in_kernel and kvm_arch_preempt_in_kernel


Because of KVM/VMX L1 never set CPU_BASED_PAUSE_EXITING and only set
SECONDARY_EXEC_PAUSE_LOOP_EXITING if supported, so for L1:
1. get a PAUSE exit with CPL=0 if PLE is supported
2. never get a PAUSE exit if don't support PLE

So, I think it can direct return true(CPL=0) if supports PLE.

But for nested KVM/VMX(I'm not familiar with nested), it could set 
CPU_BASED_PAUSE_EXITING, so I think get_cpl() is also needed.


If the above is correct, what about this way( we can save a vmcs_read 
opeartion for L1):

kvm_arch_vcpu_spin_in_kernel(vcpu)
{
	if (!is_guest_mode(vcpu))
		return true;

	return vmx_get_cpl(vcpu) == 0;
}

kvm_vcpu_on_spin()
{
	/* @me get a PAUSE exit */
	me_in_kernel = kvm_arch_vcpu_spin_in_kernel(me);
	...
	for each vcpu {
		...
		if (me_in_kernel && !...preempt_in_kernel(vcpu))
			continue;
		...
	}
	...
}

---
Regards,
Longpeng(Mike)

> However, I understand that vmx_get_cpl can be a bit slow here.  You can
> actually read SS's access rights directly in this function and get the
> DPL from there, that's going to be just a single VMREAD.
> 
> The only difference is when vmx->rmode.vm86_active=1.  However,
> pause-loop exiting is not working properly anyway if
> vmx->rmode.vm86_active=1, because CPL=3 according to the processor.
> 
> Paolo
> 
>> +	 * The following block needs less cycles than vmx_get_cpl().
>> +	 */
>> +	if (cpu_has_secondary_exec_ctrls())
>> +		secondary_exec_ctrl = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
>> +	if (secondary_exec_ctrl & SECONDARY_EXEC_PAUSE_LOOP_EXITING)
>> +		return true;
>> +
> 
> Paolo
> 

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH 2/3] KVM: X86: implement the logic for spinlock optimization
  2017-08-07 12:28     ` Longpeng(Mike)
@ 2017-08-07 13:16       ` Paolo Bonzini
  0 siblings, 0 replies; 11+ messages in thread
From: Paolo Bonzini @ 2017-08-07 13:16 UTC (permalink / raw)
  To: Longpeng(Mike)
  Cc: Longpeng(Mike),
	rkrcmar, agraf, borntraeger, cohuck, christoffer.dall,
	marc.zyngier, james.hogan, kvm, linux-kernel, weidong.huang,
	arei.gonglei, wangxinxin.wang, david

On 07/08/2017 14:28, Longpeng(Mike) wrote:
> * kvm_arch_spin_in_kernel() returns whether the vcpu (which exits due to
> spinlock) is CPL=0. It only be called by kvm_vcpu_on_spin(), and the
> input vcpu is 'me' which get a PAUSE exit now. *
> 
> I split kvm_arch_vcpu_in_kernel(in RFC) into two functions:
> kvm_arch_spin_in_kernel and kvm_arch_preempt_in_kernel
> 
> Because of KVM/VMX L1 never set CPU_BASED_PAUSE_EXITING and only set
> SECONDARY_EXEC_PAUSE_LOOP_EXITING if supported, so for L1:

I understand better now.  I think vmx.c should just return true from
vmx_spin_in_kernel.  However, kvm_arch_vcpu_spin_in_kernel is not
necessary.  Instead you should make "in_kern" an argument to
kvm_vcpu_on_spin (maybe renamed to "yield_to_kernel_mode_vcpu").

Then vmx.c can just call "kvm_vcpu_on_spin(vcpu, true)".

> 1. get a PAUSE exit with CPL=0 if PLE is supported
> 2. never get a PAUSE exit if don't support PLE
> 
> So, I think it can direct return true(CPL=0) if supports PLE.
> 
> But for nested KVM/VMX(I'm not familiar with nested), it could set
> CPU_BASED_PAUSE_EXITING, so I think get_cpl() is also needed.

If the nested hypervisor sets CPU_BASED_PAUSE_EXITING, a PAUSE vmexit
while running a nested guest would be reflected to the nested
hypervisor.  So you wouldn't get to handle_pause and thus to
kvm_vcpu_on_spin.

Thanks,

Paolo

^ permalink raw reply	[flat|nested] 11+ messages in thread

end of thread, other threads:[~2017-08-07 13:16 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-08-07  8:44 [PATCH 0/3] KVM: optimize the kvm_vcpu_on_spin Longpeng(Mike)
2017-08-07  8:44 ` [PATCH 1/3] KVM: add spinlock-exiting optimize framework Longpeng(Mike)
2017-08-07  8:55   ` David Hildenbrand
2017-08-07  9:04     ` Longpeng (Mike)
2017-08-07  8:44 ` [PATCH 2/3] KVM: X86: implement the logic for spinlock optimization Longpeng(Mike)
2017-08-07 10:45   ` Paolo Bonzini
2017-08-07 12:28     ` Longpeng(Mike)
2017-08-07 13:16       ` Paolo Bonzini
2017-08-07  8:44 ` [PATCH 3/3] KVM: implement spinlock optimization logic for arm/s390 Longpeng(Mike)
2017-08-07  8:52   ` David Hildenbrand
2017-08-07  8:54     ` Longpeng (Mike)

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.