Skip to contentSkip to conversation

KVM: VMX: Avoid RDMSRs in PI x2APIC checks

PATCH Applied Sean Christopherson 2 participants 4 messages Jan 15, 2021 – Jan 18, 2021

Subsystems (3)
lore
Share & tools
lore

Sean Christopherson

author maintainer
#0 lore raw

cover KVM: VMX: Avoid RDMSRs in PI x2APIC checks

Resurrect a patch[*] from over a year ago to avoid RDMSR in a fairly hot path in KVM's posted interrupt support. Note, in my response to that patch, I suggested checking x2apic_supported() as well as x2apic_mode. I have no idea why I suggested that; unless I'm missing something, x2apic_mode can never be set if x2apic_supported() is false.

[*] https://lkml.kernel.org/r/[email protected]/

Sean Christopherson (2):
x86/apic: Export x2apic_mode for use by KVM in "warm" path
KVM: VMX: Use x2apic_mode to avoid RDMSR when querying PI state

 arch/x86/kernel/apic/apic.c    | 1 +
 arch/x86/kvm/vmx/posted_intr.c | 6 +++---
 2 files changed, 4 insertions(+), 3 deletions(-)
Signature · 1 line
2.30.0.284.gd98b1dd5eaa7-goog
author maintainer
#1 lore raw

PATCH 1/2 x86/apic: Export x2apic_mode for use by KVM in "warm" path

Export x2apic_mode so that KVM can query whether x2APIC is active without having to incur the RDMSR in x2apic_enabled(). When Posted Interrupts are in use for a guest with an assigned device, KVM ends up checking for x2APIC at least once every time a vCPU halts. KVM could obviously snapshot x2apic_enabled() to avoid the RDMSR, but that's rather silly given that x2apic_mode holds the exact info needed by KVM.

---
 arch/x86/kernel/apic/apic.c | 1 +
 1 file changed, 1 insertion(+)
arch/x86/kernel/apic/apic.c +1 -0
index 6bd20c0de8bc..dea2b44966ca 100644
@@ -1743,6 +1743,7 @@ void apic_ap_setup(void)
 
 #ifdef CONFIG_X86_X2APIC
 int x2apic_mode;
+EXPORT_SYMBOL_GPL(x2apic_mode);
 
 enum {
 	X2APIC_OFF,
Signature · 1 line
2.30.0.284.gd98b1dd5eaa7-goog
author maintainer
#2 lore raw

PATCH 2/2 KVM: VMX: Use x2apic_mode to avoid RDMSR when querying PI state

In reply to Sean Christopherson

Use x2apic_mode instead of x2apic_enabled() when adjusting the destination ID during Posted Interrupt updates. This avoids the costly RDMSR that is hidden behind x2apic_enabled().

---
 arch/x86/kvm/vmx/posted_intr.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
arch/x86/kvm/vmx/posted_intr.c +3 -3
index f02962dcc72c..4831bc44ce66 100644
@@ -54,7 +54,7 @@ void vmx_vcpu_pi_load(struct kvm_vcpu *vcpu, int cpu)
 
 		dest = cpu_physical_id(cpu);
 
-		if (x2apic_enabled())
+		if (x2apic_mode)
 			new.ndst = dest;
 		else
 			new.ndst = (dest << 8) & 0xFF00;
@@ -104,7 +104,7 @@ static void __pi_post_block(struct kvm_vcpu *vcpu)
 
 		dest = cpu_physical_id(vcpu->cpu);
 
-		if (x2apic_enabled())
+		if (x2apic_mode)
 			new.ndst = dest;
 		else
 			new.ndst = (dest << 8) & 0xFF00;
@@ -174,7 +174,7 @@ int pi_pre_block(struct kvm_vcpu *vcpu)
 		 */
 		dest = cpu_physical_id(vcpu->pre_pcpu);
 
-		if (x2apic_enabled())
+		if (x2apic_mode)
 			new.ndst = dest;
 		else
 			new.ndst = (dest << 8) & 0xFF00;
Signature · 1 line
2.30.0.284.gd98b1dd5eaa7-goog
maintainer Applied
#3 lore raw
In reply to Sean Christopherson
Quoted 8 lines · Sean Christopherson

Resurrect a patch[*] from over a year ago to avoid RDMSR in a fairly hot path in KVM's posted interrupt support. Note, in my response to that patch, I suggested checking x2apic_supported() as well as x2apic_mode. I have no idea why I suggested that; unless I'm missing something, x2apic_mode can never be set if x2apic_supported() is false.

[*] https://lkml.kernel.org/r/[email protected]/

Quoted 8 lines

Sean Christopherson (2):
x86/apic: Export x2apic_mode for use by KVM in "warm" path
KVM: VMX: Use x2apic_mode to avoid RDMSR when querying PI state

  arch/x86/kernel/apic/apic.c    | 1 +
  arch/x86/kvm/vmx/posted_intr.c | 6 +++---
  2 files changed, 4 insertions(+), 3 deletions(-)

Queued, thanks.

Paolo