Skip to contentSkip to conversation

KVM: x86: init x2apic_enabled() once

PATCH In review [email protected] 3 participants 3 messages Jul 23, 2019 – Jul 24, 2019

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 KVM: x86: init x2apic_enabled() once

From: luferry <[email protected]>

x2apic_eanbled() costs about 200 cycles
when guest trigger halt pretty high, pi ops in hotpath

---
 arch/x86/kvm/vmx/vmx.c | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)
arch/x86/kvm/vmx/vmx.c +7 -3
index d98eac371c0a..e17dbf011e47 100644
@@ -186,6 +186,8 @@ static DEFINE_STATIC_KEY_FALSE(vmx_l1d_should_flush);
 static DEFINE_STATIC_KEY_FALSE(vmx_l1d_flush_cond);
 static DEFINE_MUTEX(vmx_l1d_flush_mutex);
 
+static int __read_mostly host_x2apic_enabled;
+
 /* Storage for pre module init parameter parsing */
 static enum vmx_l1d_flush_state __read_mostly vmentry_l1d_flush_param = VMENTER_L1D_FLUSH_AUTO;
 
@@ -1204,7 +1206,7 @@ static void vmx_vcpu_pi_load(struct kvm_vcpu *vcpu, int cpu)
 
 		dest = cpu_physical_id(cpu);
 
-		if (x2apic_enabled())
+		if (host_x2apic_enabled)
 			new.ndst = dest;
 		else
 			new.ndst = (dest << 8) & 0xFF00;
@@ -7151,7 +7153,7 @@ static void __pi_post_block(struct kvm_vcpu *vcpu)
 
 		dest = cpu_physical_id(vcpu->cpu);
 
-		if (x2apic_enabled())
+		if (host_x2apic_enabled)
 			new.ndst = dest;
 		else
 			new.ndst = (dest << 8) & 0xFF00;
@@ -7221,7 +7223,7 @@ static int pi_pre_block(struct kvm_vcpu *vcpu)
 		 */
 		dest = cpu_physical_id(vcpu->pre_pcpu);
 
-		if (x2apic_enabled())
+		if (host_x2apic_enabled)
 			new.ndst = dest;
 		else
 			new.ndst = (dest << 8) & 0xFF00;
@@ -7804,6 +7806,8 @@ static int __init vmx_init(void)
 	}
 #endif
 
+	host_x2apic_enabled = x2apic_enabled();
+
 	r = kvm_init(&vmx_x86_ops, sizeof(struct vcpu_vmx),
 		     __alignof__(struct vcpu_vmx), THIS_MODULE);
 	if (r)
Signature · 1 line
2.14.1.40.g8e62ba1
Reviewed-by on v1 1/1
#1 lore raw
Quoted 51 lines · On 07/23/2019 06:06 AM, [email protected] wrote:

From: luferry <[email protected]>

x2apic_eanbled() costs about 200 cycles
when guest trigger halt pretty high, pi ops in hotpath

---
  arch/x86/kvm/vmx/vmx.c | 10 +++++++---
  1 file changed, 7 insertions(+), 3 deletions(-)
arch/x86/kvm/vmx/vmx.c +5 -3
index d98eac371c0a..e17dbf011e47 100644
@@ -186,6 +186,8 @@ static DEFINE_STATIC_KEY_FALSE(vmx_l1d_should_flush);
  static DEFINE_STATIC_KEY_FALSE(vmx_l1d_flush_cond);
  static DEFINE_MUTEX(vmx_l1d_flush_mutex);
 
+static int __read_mostly host_x2apic_enabled;
+
  /* Storage for pre module init parameter parsing */
  static enum vmx_l1d_flush_state __read_mostly vmentry_l1d_flush_param = VMENTER_L1D_FLUSH_AUTO;
 
@@ -1204,7 +1206,7 @@ static void vmx_vcpu_pi_load(struct kvm_vcpu *vcpu, int cpu)
 
  		dest = cpu_physical_id(cpu);
 
-		if (x2apic_enabled())
+		if (host_x2apic_enabled)
  			new.ndst = dest;
  		else
  			new.ndst = (dest << 8) & 0xFF00;
@@ -7151,7 +7153,7 @@ static void __pi_post_block(struct kvm_vcpu *vcpu)
 
  		dest = cpu_physical_id(vcpu->cpu);
 
-		if (x2apic_enabled())
+		if (host_x2apic_enabled)
  			new.ndst = dest;
  		else
  			new.ndst = (dest << 8) & 0xFF00;
@@ -7221,7 +7223,7 @@ static int pi_pre_block(struct kvm_vcpu *vcpu)
  		 */
  		dest = cpu_physical_id(vcpu->pre_pcpu);
 
-		if (x2apic_enabled())
+		if (host_x2apic_enabled)
  			new.ndst = dest;
  		else
  			new.ndst = (dest << 8) & 0xFF00;

arch/x86/kvm/vmx/vmx.c static int __init vmx_init(void) · line 7813

Quoted 8 lines
arch/x86/kvm/vmx/vmx.c +2 -0
@@ -7804,6 +7806,8 @@ static int __init vmx_init(void)
  	}
  #endif
 
+	host_x2apic_enabled = x2apic_enabled();
+
  	r = kvm_init(&vmx_x86_ops, sizeof(struct vcpu_vmx),
  		     __alignof__(struct vcpu_vmx), THIS_MODULE);
  	if (r)
#2 lore raw
In reply to [email protected]
Quoted 24 lines · On Tue, Jul 23, 2019 at 09:06:08PM +0800, [email protected] wrote:

From: luferry <[email protected]>

x2apic_eanbled() costs about 200 cycles
when guest trigger halt pretty high, pi ops in hotpath

---
 arch/x86/kvm/vmx/vmx.c | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)
arch/x86/kvm/vmx/vmx.c +2 -0
index d98eac371c0a..e17dbf011e47 100644
@@ -186,6 +186,8 @@ static DEFINE_STATIC_KEY_FALSE(vmx_l1d_should_flush);
 static DEFINE_STATIC_KEY_FALSE(vmx_l1d_flush_cond);
 static DEFINE_MUTEX(vmx_l1d_flush_mutex);
 
+static int __read_mostly host_x2apic_enabled;
+
 /* Storage for pre module init parameter parsing */
 static enum vmx_l1d_flush_state __read_mostly vmentry_l1d_flush_param = VMENTER_L1D_FLUSH_AUTO;
 

arch/x86/kvm/vmx/vmx.c static void vmx_vcpu_pi_load(struct kvm_vcpu *vcpu, int cpu) · line 1209

Quoted 5 lines
arch/x86/kvm/vmx/vmx.c +1 -1
@@ -1204,7 +1206,7 @@ static void vmx_vcpu_pi_load(struct kvm_vcpu *vcpu, int cpu)
 
 		dest = cpu_physical_id(cpu);
 
-		if (x2apic_enabled())
+		if (host_x2apic_enabled)

Instead of caching x2apic_enabled(), this can be:

	if (x2apic_supported() && x2apic_mode)

which will get compiled out if CONFIG_X86_X2APIC=n.

It's quite suprising (to me) that 2apic_enabled() reads the MSR in the first place, but at a glance the other users of x2apic_enabled() do need to query the MSR and/or may be called before x2apic_mode is set.

Quoted 34 lines
arch/x86/kvm/vmx/vmx.c +4 -2
 			new.ndst = dest;
 		else
 			new.ndst = (dest << 8) & 0xFF00;
@@ -7151,7 +7153,7 @@ static void __pi_post_block(struct kvm_vcpu *vcpu)
 
 		dest = cpu_physical_id(vcpu->cpu);
 
-		if (x2apic_enabled())
+		if (host_x2apic_enabled)
 			new.ndst = dest;
 		else
 			new.ndst = (dest << 8) & 0xFF00;
@@ -7221,7 +7223,7 @@ static int pi_pre_block(struct kvm_vcpu *vcpu)
 		 */
 		dest = cpu_physical_id(vcpu->pre_pcpu);
 
-		if (x2apic_enabled())
+		if (host_x2apic_enabled)
 			new.ndst = dest;
 		else
 			new.ndst = (dest << 8) & 0xFF00;
@@ -7804,6 +7806,8 @@ static int __init vmx_init(void)
 	}
 #endif
 
+	host_x2apic_enabled = x2apic_enabled();
+
 	r = kvm_init(&vmx_x86_ops, sizeof(struct vcpu_vmx),
 		     __alignof__(struct vcpu_vmx), THIS_MODULE);
 	if (r)
Signature · 1 line
2.14.1.40.g8e62ba1