Skip to contentSkip to conversation

KVM: x86: Fix the intel_pt PMI handling wrongly considered from guest

PATCH Superseded Yanfei Xu 2 participants 6 messages May 15, 2022 – May 21, 2022

Subsystems (3)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Yanfei Xu

author
#0 lore raw

PATCH 1/1 KVM: x86: Fix the intel_pt PMI handling wrongly considered from guest

When kernel handles the vm-exit caused by external interrupts and PMI, it always set a type of kvm_intr_type to handling_intr_from_guest to tell if it's dealing an IRQ or NMI.
However, the further type judgment is missing in kvm_arch_pmi_in_guest(). It could make the PMI of intel_pt wrongly considered it comes from a guest once the PMI breaks the handling of vm-exit of external interrupts.

Fixes db215756ae59 ("KVM: x86: More precisely identify NMI from guest when handling PMI")
---
 arch/x86/include/asm/kvm_host.h | 8 +++++++-
 arch/x86/kvm/x86.h              | 6 ------
 2 files changed, 7 insertions(+), 7 deletions(-)
arch/x86/include/asm/kvm_host.h +7 -1
index 4ff36610af6a..308cf19f123d 100644
@@ -1582,8 +1582,14 @@ static inline int kvm_arch_flush_remote_tlb(struct kvm *kvm)
 		return -ENOTSUPP;
 }
 
+enum kvm_intr_type {
+	/* Values are arbitrary, but must be non-zero. */
+	KVM_HANDLING_IRQ = 1,
+	KVM_HANDLING_NMI,
+};
+
 #define kvm_arch_pmi_in_guest(vcpu) \
-	((vcpu) && (vcpu)->arch.handling_intr_from_guest)
+	((vcpu) && (vcpu)->arch.handling_intr_from_guest == KVM_HANDLING_NMI)
 
 void kvm_mmu_x86_module_init(void);
 int kvm_mmu_vendor_module_init(void);
arch/x86/kvm/x86.h +0 -6
index 588792f00334..3bdf1bc76863 100644
@@ -344,12 +344,6 @@ static inline bool kvm_cstate_in_guest(struct kvm *kvm)
 	return kvm->arch.cstate_in_guest;
 }
 
-enum kvm_intr_type {
-	/* Values are arbitrary, but must be non-zero. */
-	KVM_HANDLING_IRQ = 1,
-	KVM_HANDLING_NMI,
-};
-
 static inline void kvm_before_interrupt(struct kvm_vcpu *vcpu,
 					enum kvm_intr_type intr)
 {
Signature · 1 line
2.32.0
#1 lore raw
Quoted 21 lines · Yanfei Xu

When kernel handles the vm-exit caused by external interrupts and PMI, it always set a type of kvm_intr_type to handling_intr_from_guest to tell if it's dealing an IRQ or NMI.
However, the further type judgment is missing in kvm_arch_pmi_in_guest(). It could make the PMI of intel_pt wrongly considered it comes from a guest once the PMI breaks the handling of vm-exit of external interrupts.

Fixes db215756ae59 ("KVM: x86: More precisely identify NMI from guest when handling PMI")
---
 arch/x86/include/asm/kvm_host.h | 8 +++++++-
 arch/x86/kvm/x86.h              | 6 ------
 2 files changed, 7 insertions(+), 7 deletions(-)
arch/x86/include/asm/kvm_host.h +0 -0
index 4ff36610af6a..308cf19f123d 100644
@@ -1582,8 +1582,14 @@ static inline int kvm_arch_flush_remote_tlb(struct kvm *kvm)
 		return -ENOTSUPP;
 }

arch/x86/include/asm/kvm_host.h static inline int kvm_arch_flush_remote_tlb(struct kvm *kvm) · line 1592

Quoted 10 lines
arch/x86/include/asm/kvm_host.h +7 -1
 
+enum kvm_intr_type {
+	/* Values are arbitrary, but must be non-zero. */
+	KVM_HANDLING_IRQ = 1,
+	KVM_HANDLING_NMI,
+};
+
 #define kvm_arch_pmi_in_guest(vcpu) \
-	((vcpu) && (vcpu)->arch.handling_intr_from_guest)
+	((vcpu) && (vcpu)->arch.handling_intr_from_guest == KVM_HANDLING_NMI)

My understanding is that this isn't correct as a general change, as perf events can use regular IRQs in some cases. See commit dd60d217062f4 ("KVM: x86: Fix perf timer mode IP reporting").

I assume there's got to be a way to know which mode perf is using, e.g. we should be able to make this look something like:

	((vcpu) && (vcpu)->arch.handling_intr_from_guest == kvm_pmi_vector)
Quoted 22 lines
arch/x86/include/asm/kvm_host.h +0 -0
 void kvm_mmu_x86_module_init(void);
 int kvm_mmu_vendor_module_init(void);
arch/x86/kvm/x86.h +0 -6
index 588792f00334..3bdf1bc76863 100644
@@ -344,12 +344,6 @@ static inline bool kvm_cstate_in_guest(struct kvm *kvm)
 	return kvm->arch.cstate_in_guest;
 }
 
-enum kvm_intr_type {
-	/* Values are arbitrary, but must be non-zero. */
-	KVM_HANDLING_IRQ = 1,
-	KVM_HANDLING_NMI,
-};
-
 static inline void kvm_before_interrupt(struct kvm_vcpu *vcpu,
 					enum kvm_intr_type intr)
 {
Signature · 1 line
2.32.0
author
#2 lore raw
Quoted 36 lines · Sean Christopherson
Quoted 21 lines · Yanfei Xu

When kernel handles the vm-exit caused by external interrupts and PMI, it always set a type of kvm_intr_type to handling_intr_from_guest to tell if it's dealing an IRQ or NMI.
However, the further type judgment is missing in kvm_arch_pmi_in_guest(). It could make the PMI of intel_pt wrongly considered it comes from a guest once the PMI breaks the handling of vm-exit of external interrupts.

Fixes db215756ae59 ("KVM: x86: More precisely identify NMI from guest when handling PMI")
---
  arch/x86/include/asm/kvm_host.h | 8 +++++++-
  arch/x86/kvm/x86.h              | 6 ------
  2 files changed, 7 insertions(+), 7 deletions(-)
arch/x86/include/asm/kvm_host.h +0 -0
index 4ff36610af6a..308cf19f123d 100644
@@ -1582,8 +1582,14 @@ static inline int kvm_arch_flush_remote_tlb(struct kvm *kvm)
  		return -ENOTSUPP;
  }

arch/x86/include/asm/kvm_host.h static inline int kvm_arch_flush_remote_tlb(struct kvm *kvm) · line 1592

Quoted 10 lines
arch/x86/include/asm/kvm_host.h +7 -1
 
+enum kvm_intr_type {
+	/* Values are arbitrary, but must be non-zero. */
+	KVM_HANDLING_IRQ = 1,
+	KVM_HANDLING_NMI,
+};
+
  #define kvm_arch_pmi_in_guest(vcpu) \
-	((vcpu) && (vcpu)->arch.handling_intr_from_guest)
+	((vcpu) && (vcpu)->arch.handling_intr_from_guest == KVM_HANDLING_NMI)

My understanding is that this isn't correct as a general change, as perf events can use regular IRQs in some cases. See commit dd60d217062f4 ("KVM: x86: Fix perf timer mode IP reporting").

Quoted 4 lines

I assume there's got to be a way to know which mode perf is using, e.g. we should be able to make this look something like:

	((vcpu) && (vcpu)->arch.handling_intr_from_guest == kvm_pmi_vector)

Thanks for your comments, I am going to understand these clearly and then reply or give a next version.

Regards,
Yanfei

Quoted 23 lines
Quoted 23 lines
arch/x86/include/asm/kvm_host.h +0 -0
  void kvm_mmu_x86_module_init(void);
  int kvm_mmu_vendor_module_init(void);
arch/x86/kvm/x86.h +0 -6
index 588792f00334..3bdf1bc76863 100644
@@ -344,12 +344,6 @@ static inline bool kvm_cstate_in_guest(struct kvm *kvm)
  	return kvm->arch.cstate_in_guest;
  }
 
-enum kvm_intr_type {
-	/* Values are arbitrary, but must be non-zero. */
-	KVM_HANDLING_IRQ = 1,
-	KVM_HANDLING_NMI,
-};
-
  static inline void kvm_before_interrupt(struct kvm_vcpu *vcpu,
  					enum kvm_intr_type intr)
  {
Signature · 1 line
2.32.0
author
#3 lore raw
In reply to Sean Christopherson

Hi Sean,
You are right, the change of kvm_arch_pmi_in_guest is incorrect, because it should cover two cases of PMI. For the PMI of intel pt, it certainly is the NMI PMI. So how about fixing it like below?

arch/x86/kvm/vmx/vmx.c +1 -1
index 610355b9ccce..378036c1cf94 100644
@@ -7856,7 +7856,7 @@ static unsigned int vmx_handle_intel_pt_intr(void)
        struct kvm_vcpu *vcpu = kvm_get_running_vcpu();
 
        /* '0' on failure so that the !PT case can use a RET0 static call. */
-       if (!kvm_arch_pmi_in_guest(vcpu))
+       if (!kvm_handling_nmi_from_guest(vcpu))
                return 0;
 
        kvm_make_request(KVM_REQ_PMI, vcpu);

Thanks,
Yanfei

-----Original Message-----
From: Sean Christopherson <[email protected]>
Sent: Monday, May 16, 2022 9:58 PM
To: Xu, Yanfei <[email protected]>
Cc: [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]
Subject: Re: [PATCH] KVM: x86: Fix the intel_pt PMI handling wrongly considered from guest

Quoted 24 lines · Yanfei Xu

When kernel handles the vm-exit caused by external interrupts and PMI, it always set a type of kvm_intr_type to handling_intr_from_guest to tell if it's dealing an IRQ or NMI.
However, the further type judgment is missing in kvm_arch_pmi_in_guest(). It could make the PMI of intel_pt wrongly considered it comes from a guest once the PMI breaks the handling of vm-exit of external interrupts.

Fixes db215756ae59 ("KVM: x86: More precisely identify NMI from guest

when handling PMI")

---
 arch/x86/include/asm/kvm_host.h | 8 +++++++-
 arch/x86/kvm/x86.h              | 6 ------
 2 files changed, 7 insertions(+), 7 deletions(-)
(quoted diff) +0 -0

b/arch/x86/include/asm/kvm_host.h index 4ff36610af6a..308cf19f123d 100644

arch/x86/include/asm/kvm_host.h +0 -0
@@ -1582,8 +1582,14 @@ static inline int kvm_arch_flush_remote_tlb(struct kvm *kvm)
 		return -ENOTSUPP;
 }
 

arch/x86/include/asm/kvm_host.h static inline int kvm_arch_flush_remote_tlb(struct kvm *kvm) · line 1593

Quoted 10 lines
arch/x86/include/asm/kvm_host.h +8 -1
+enum kvm_intr_type {
+	/* Values are arbitrary, but must be non-zero. */
+	KVM_HANDLING_IRQ = 1,
+	KVM_HANDLING_NMI,
+};
+
 #define kvm_arch_pmi_in_guest(vcpu) \
-	((vcpu) && (vcpu)->arch.handling_intr_from_guest)
+	((vcpu) && (vcpu)->arch.handling_intr_from_guest == 
+KVM_HANDLING_NMI)

My understanding is that this isn't correct as a general change, as perf events can use regular IRQs in some cases. See commit dd60d217062f4 ("KVM: x86: Fix perf timer mode IP reporting").

I assume there's got to be a way to know which mode perf is using, e.g. we should be able to make this look something like:

	((vcpu) && (vcpu)->arch.handling_intr_from_guest == kvm_pmi_vector)
Quoted 21 lines
arch/x86/include/asm/kvm_host.h +0 -0
 void kvm_mmu_x86_module_init(void);
 int kvm_mmu_vendor_module_init(void); diff --git a/arch/x86/kvm/x86.h 

b/arch/x86/kvm/x86.h index 588792f00334..3bdf1bc76863 100644

arch/x86/kvm/x86.h +0 -7
@@ -344,12 +344,6 @@ static inline bool kvm_cstate_in_guest(struct kvm *kvm)
 	return kvm->arch.cstate_in_guest;
 }
 
-enum kvm_intr_type {
-	/* Values are arbitrary, but must be non-zero. */
-	KVM_HANDLING_IRQ = 1,
-	KVM_HANDLING_NMI,
-};
-
 static inline void kvm_before_interrupt(struct kvm_vcpu *vcpu,
 					enum kvm_intr_type intr)
 {
--

2.32.0

#4 lore raw

Please don't top-post.

Quoted 26 lines · Yanfei

From: Sean Christopherson <[email protected]>

Quoted 9 lines · Yanfei Xu
(quoted diff) +0 -0

b/arch/x86/include/asm/kvm_host.h index 4ff36610af6a..308cf19f123d 100644

arch/x86/include/asm/kvm_host.h +0 -0
@@ -1582,8 +1582,14 @@ static inline int kvm_arch_flush_remote_tlb(struct kvm *kvm)
 		return -ENOTSUPP;
 }
 

arch/x86/include/asm/kvm_host.h static inline int kvm_arch_flush_remote_tlb(struct kvm *kvm) · line 1593

Quoted 10 lines
arch/x86/include/asm/kvm_host.h +8 -1
+enum kvm_intr_type {
+	/* Values are arbitrary, but must be non-zero. */
+	KVM_HANDLING_IRQ = 1,
+	KVM_HANDLING_NMI,
+};
+
 #define kvm_arch_pmi_in_guest(vcpu) \
-	((vcpu) && (vcpu)->arch.handling_intr_from_guest)
+	((vcpu) && (vcpu)->arch.handling_intr_from_guest == 
+KVM_HANDLING_NMI)

My understanding is that this isn't correct as a general change, as perf events can use regular IRQs in some cases. See commit dd60d217062f4 ("KVM:
x86: Fix perf timer mode IP reporting").

Quoted 8 lines

I assume there's got to be a way to know which mode perf is using, e.g. we should be able to make this look something like:

	((vcpu) && (vcpu)->arch.handling_intr_from_guest == kvm_pmi_vector)

Hi Sean,
You are right, the change of kvm_arch_pmi_in_guest is incorrect, because it should cover two cases of PMI. For the PMI of intel pt, it certainly is the NMI PMI. So how about fixing it like below?

Yep, that works. I did enough spelunking to figure out how we can fix the generic issue, but it's per-event and requires a decent amount of plumbing in perf.

perf_guest_handle_intel_pt_intr() doesn't bother with perf_guest_state() since it's such a specialized event, so fixing this in vmx_handle_intel_pt_intr() would likely be the long-term solution even if/when the generic case is fixed.

author
#5 lore raw
Quoted 14 lines

-----Original Message-----
From: Sean Christopherson <[email protected]>
Sent: Saturday, May 21, 2022 12:32 AM
To: Xu, Yanfei <[email protected]>
Cc: [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; Wang, Wei W <[email protected]>; Liang, Kan <[email protected]>
Subject: Re: [PATCH] KVM: x86: Fix the intel_pt PMI handling wrongly considered from guest

Quoted 1 line

Please don't top-post.

Got it, just correctly configured my mailbox with prefix each line of original message.

Quoted 37 lines · Yanfei
Quoted 8 lines

From: Sean Christopherson <[email protected]> On Mon, May 16, 2022,

arch/x86/include/asm/kvm_host.h static inline int

Quoted 6 lines · Yanfei Xu
(quoted diff) +0 -0

b/arch/x86/include/asm/kvm_host.h index 4ff36610af6a..308cf19f123d 100644

arch/x86/include/asm/kvm_host.h +0 -0
@@ -1582,8 +1582,14 @@ static inline int

kvm_arch_flush_remote_tlb(struct kvm *kvm)

Quoted 16 lines

arch/x86/include/asm/kvm_host.h static inline int · line 1593

Quoted 13 lines
arch/x86/include/asm/kvm_host.h +8 -1
 		return -ENOTSUPP;
 }
 
+enum kvm_intr_type {
+	/* Values are arbitrary, but must be non-zero. */
+	KVM_HANDLING_IRQ = 1,
+	KVM_HANDLING_NMI,
+};
+
 #define kvm_arch_pmi_in_guest(vcpu) \
-	((vcpu) && (vcpu)->arch.handling_intr_from_guest)
+	((vcpu) && (vcpu)->arch.handling_intr_from_guest ==
+KVM_HANDLING_NMI)

My understanding is that this isn't correct as a general change, as perf events can use regular IRQs in some cases. See commit dd60d217062f4

("KVM:

Quoted 9 lines

x86: Fix perf timer mode IP reporting").

I assume there's got to be a way to know which mode perf is using, e.g. we should be able to make this look something like:

	((vcpu) && (vcpu)->arch.handling_intr_from_guest == kvm_pmi_vector)

Hi Sean,
You are right, the change of kvm_arch_pmi_in_guest is incorrect, because it

Quoted 7 lines

should cover two cases of PMI.

Quoted 1 line

For the PMI of intel pt, it certainly is the NMI PMI. So how about fixing it like

below?

Yep, that works. I did enough spelunking to figure out how we can fix the generic issue, but it's per-event and requires a decent amount of plumbing in perf.

Agree, it's per-event for the generic issue...

Quoted 4 lines

perf_guest_handle_intel_pt_intr() doesn't bother with perf_guest_state() since it's such a specialized event, so fixing this in vmx_handle_intel_pt_intr() would likely be the long-term solution even if/when the generic case is fixed.

Will send the v2.

Thanks,
Yanfei