Skip to contentSkip to conversation

KVM: vmx/pmu: Fix dummy check if lbr_desc->event is created

PATCH Applied Like Xu 4 participants 9 messages Feb 23, 2021 – Feb 26, 2021

Subsystems (3)
lore
Share & tools
lore

Like Xu

author
#0 lore raw

PATCH 1/2 KVM: vmx/pmu: Fix dummy check if lbr_desc->event is created

If lbr_desc->event is successfully created, the intel_pmu_create_ guest_lbr_event() will return 0, otherwise it will return -ENOENT, and then jump to LBR msrs dummy handling.

Fixes 1b5ac3226a1a ("KVM: vmx/pmu: Pass-through LBR msrs when the guest LBR event is ACTIVE")
---
 arch/x86/kvm/vmx/pmu_intel.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/kvm/vmx/pmu_intel.c +1 -1
index d1df618cb7de..d6a5fe19ff09 100644
@@ -320,7 +320,7 @@ static bool intel_pmu_handle_lbr_msrs_access(struct kvm_vcpu *vcpu,
 	if (!intel_pmu_is_valid_lbr_msr(vcpu, index))
 		return false;
 
-	if (!lbr_desc->event && !intel_pmu_create_guest_lbr_event(vcpu))
+	if (!lbr_desc->event && intel_pmu_create_guest_lbr_event(vcpu))
 		goto dummy;
 
 	/*
Signature · 1 line
2.29.2
author
#1 lore raw

PATCH 2/2 KVM: vmx/pmu: Clear DEBUGCTLMSR_LBR bit on the debug breakpoint event

When the processor that support model-specific LBR generates a debug breakpoint event, it automatically clears the LBR flag. This action does not clear previously stored LBR stack MSRs. (Intel SDM 17.4.2)

---
 arch/x86/kvm/vmx/vmx.c | 5 +++++
 1 file changed, 5 insertions(+)
arch/x86/kvm/vmx/vmx.c +5 -0
index e0a3a9be654b..4951b535eb7f 100644
@@ -4795,6 +4795,7 @@ static int handle_exception_nmi(struct kvm_vcpu *vcpu)
 	u32 intr_info, ex_no, error_code;
 	unsigned long cr2, rip, dr6;
 	u32 vect_info;
+	u64 lbr_ctl;
 
 	vect_info = vmx->idt_vectoring_info;
 	intr_info = vmx_get_intr_info(vcpu);
@@ -4886,6 +4887,10 @@ static int handle_exception_nmi(struct kvm_vcpu *vcpu)
 		rip = kvm_rip_read(vcpu);
 		kvm_run->debug.arch.pc = vmcs_readl(GUEST_CS_BASE) + rip;
 		kvm_run->debug.arch.exception = ex_no;
+		/* On the debug breakpoint event, the LBREn bit is cleared. */
+		lbr_ctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
+		if (lbr_ctl & DEBUGCTLMSR_LBR)
+			vmcs_write64(GUEST_IA32_DEBUGCTL, lbr_ctl & ~DEBUGCTLMSR_LBR);
 		break;
 	case AC_VECTOR:
 		if (guest_inject_ac(vcpu)) {
Signature · 1 line
2.29.2
#2 lore raw

Re: [PATCH 2/2] KVM: vmx/pmu: Clear DEBUGCTLMSR_LBR bit on the debug breakpoint event

Quoted 22 lines · Like Xu

When the processor that support model-specific LBR generates a debug breakpoint event, it automatically clears the LBR flag. This action does not clear previously stored LBR stack MSRs. (Intel SDM 17.4.2)

---
 arch/x86/kvm/vmx/vmx.c | 5 +++++
 1 file changed, 5 insertions(+)
arch/x86/kvm/vmx/vmx.c +1 -0
index e0a3a9be654b..4951b535eb7f 100644
@@ -4795,6 +4795,7 @@ static int handle_exception_nmi(struct kvm_vcpu *vcpu)
 	u32 intr_info, ex_no, error_code;
 	unsigned long cr2, rip, dr6;
 	u32 vect_info;
+	u64 lbr_ctl;
 
 	vect_info = vmx->idt_vectoring_info;
 	intr_info = vmx_get_intr_info(vcpu);

arch/x86/kvm/vmx/vmx.c static int handle_exception_nmi(struct kvm_vcpu *vcpu) · line 4890

Quoted 4 lines
arch/x86/kvm/vmx/vmx.c +1 -0
@@ -4886,6 +4887,10 @@ static int handle_exception_nmi(struct kvm_vcpu *vcpu)
 		rip = kvm_rip_read(vcpu);
 		kvm_run->debug.arch.pc = vmcs_readl(GUEST_CS_BASE) + rip;
 		kvm_run->debug.arch.exception = ex_no;
+		/* On the debug breakpoint event, the LBREn bit is cleared. */

Except this code is in BP_VECTOR, not DB_VECTOR as it should be.

When the processor generates a debug exception (#DB), it automatically clears the LBR flag before executing the exception handler. This action does not clear previously stored LBR stack MSRs.

Quoted 9 lines
arch/x86/kvm/vmx/vmx.c +3 -0
+		lbr_ctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
+		if (lbr_ctl & DEBUGCTLMSR_LBR)
+			vmcs_write64(GUEST_IA32_DEBUGCTL, lbr_ctl & ~DEBUGCTLMSR_LBR);
 		break;
 	case AC_VECTOR:
 		if (guest_inject_ac(vcpu)) {
Signature · 1 line
2.29.2
maintainer
#3 lore raw

Re: [PATCH 2/2] KVM: vmx/pmu: Clear DEBUGCTLMSR_LBR bit on the debug breakpoint event

Quoted 30 lines · Sean Christopherson
Quoted 22 lines · Like Xu

When the processor that support model-specific LBR generates a debug breakpoint event, it automatically clears the LBR flag. This action does not clear previously stored LBR stack MSRs. (Intel SDM 17.4.2)

---
  arch/x86/kvm/vmx/vmx.c | 5 +++++
  1 file changed, 5 insertions(+)
arch/x86/kvm/vmx/vmx.c +1 -0
index e0a3a9be654b..4951b535eb7f 100644
@@ -4795,6 +4795,7 @@ static int handle_exception_nmi(struct kvm_vcpu *vcpu)
  	u32 intr_info, ex_no, error_code;
  	unsigned long cr2, rip, dr6;
  	u32 vect_info;
+	u64 lbr_ctl;
 
  	vect_info = vmx->idt_vectoring_info;
  	intr_info = vmx_get_intr_info(vcpu);

arch/x86/kvm/vmx/vmx.c static int handle_exception_nmi(struct kvm_vcpu *vcpu) · line 4890

Quoted 4 lines
arch/x86/kvm/vmx/vmx.c +1 -0
@@ -4886,6 +4887,10 @@ static int handle_exception_nmi(struct kvm_vcpu *vcpu)
  		rip = kvm_rip_read(vcpu);
  		kvm_run->debug.arch.pc = vmcs_readl(GUEST_CS_BASE) + rip;
  		kvm_run->debug.arch.exception = ex_no;
+		/* On the debug breakpoint event, the LBREn bit is cleared. */

Except this code is in BP_VECTOR, not DB_VECTOR as it should be.

Quoted 3 lines

When the processor generates a debug exception (#DB), it automatically clears the LBR flag before executing the exception handler. This action does not clear previously stored LBR stack MSRs.

Also, this should come with a testcase.

Paolo

maintainer Applied
#4 lore raw
In reply to Like Xu
Quoted 15 lines · Like Xu

If lbr_desc->event is successfully created, the intel_pmu_create_ guest_lbr_event() will return 0, otherwise it will return -ENOENT, and then jump to LBR msrs dummy handling.

Fixes 1b5ac3226a1a ("KVM: vmx/pmu: Pass-through LBR msrs when the guest LBR event is ACTIVE")
---
  arch/x86/kvm/vmx/pmu_intel.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/kvm/vmx/pmu_intel.c +0 -0
index d1df618cb7de..d6a5fe19ff09 100644

arch/x86/kvm/vmx/pmu_intel.c static bool intel_pmu_handle_lbr_msrs_access(struct kvm_vcpu *vcpu, · line 324

Quoted 6 lines
arch/x86/kvm/vmx/pmu_intel.c +1 -1
@@ -320,7 +320,7 @@ static bool intel_pmu_handle_lbr_msrs_access(struct kvm_vcpu *vcpu,
  	if (!intel_pmu_is_valid_lbr_msr(vcpu, index))
  		return false;
 
-	if (!lbr_desc->event && !intel_pmu_create_guest_lbr_event(vcpu))
+	if (!lbr_desc->event && intel_pmu_create_guest_lbr_event(vcpu))
  		goto dummy;

Queued, adding a "< 0" to clarify the semantics of the function.

Paolo

#5 lore raw
In reply to Like Xu
Quoted 15 lines · Like Xu

If lbr_desc->event is successfully created, the intel_pmu_create_ guest_lbr_event() will return 0, otherwise it will return -ENOENT, and then jump to LBR msrs dummy handling.

Fixes 1b5ac3226a1a ("KVM: vmx/pmu: Pass-through LBR msrs when the guest LBR event is ACTIVE")
---
 arch/x86/kvm/vmx/pmu_intel.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/kvm/vmx/pmu_intel.c +0 -0
index d1df618cb7de..d6a5fe19ff09 100644

arch/x86/kvm/vmx/pmu_intel.c static bool intel_pmu_handle_lbr_msrs_access(struct kvm_vcpu *vcpu, · line 324

Quoted 6 lines
arch/x86/kvm/vmx/pmu_intel.c +1 -1
@@ -320,7 +320,7 @@ static bool intel_pmu_handle_lbr_msrs_access(struct kvm_vcpu *vcpu,
 	if (!intel_pmu_is_valid_lbr_msr(vcpu, index))
 		return false;
 
-	if (!lbr_desc->event && !intel_pmu_create_guest_lbr_event(vcpu))
+	if (!lbr_desc->event && intel_pmu_create_guest_lbr_event(vcpu))
 		goto dummy;

Wouldn't it be better to create an event only on write? And really, why create the event in this flow in the first place? In normal operation, can't event creation be deferred until GUEST_IA32_DEBUGCTL.DEBUGCTLMSR_LBR=1? If event creation fails in that flow, I would think KVM would do its best to create an event in future runs without waiting for additional actions from the guest.

Also, this bug suggests there's a big gaping hole in the test coverage. AFAICT, event contention would lead to a #GP crash in the host due to lbr_desc->event being dereferenced, no?

Quoted 5 lines
arch/x86/kvm/vmx/pmu_intel.c +0 -0
 	/*
Signature · 1 line
2.29.2
maintainer
#6 lore raw
Quoted 7 lines · Sean Christopherson

If event
creation fails in that flow, I would think KVM would do its best to create an event in future runs without waiting for additional actions from the guest.

Also, this bug suggests there's a big gaping hole in the test coverage. AFAICT, event contention would lead to a #GP crash in the host due to lbr_desc->event being dereferenced, no?

Yes, testing contention would use the tools/testing/selftests/kvm framework rather than just kvm-unit-tests.

Paolo

#7 lore raw
In reply to Sean Christopherson
Quoted 19 lines · Sean Christopherson
Quoted 18 lines · Like Xu

If lbr_desc->event is successfully created, the intel_pmu_create_ guest_lbr_event() will return 0, otherwise it will return -ENOENT, and then jump to LBR msrs dummy handling.

Fixes 1b5ac3226a1a ("KVM: vmx/pmu: Pass-through LBR msrs when the guest LBR event is ACTIVE")
---
  arch/x86/kvm/vmx/pmu_intel.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/kvm/vmx/pmu_intel.c +0 -0
index d1df618cb7de..d6a5fe19ff09 100644
@@ -320,7 +320,7 @@ static bool intel_pmu_handle_lbr_msrs_access(struct kvm_vcpu *vcpu,
  	if (!intel_pmu_is_valid_lbr_msr(vcpu, index))
  		return false;
 
Quoted 6 lines

arch/x86/kvm/vmx/pmu_intel.c static bool intel_pmu_handle_lbr_msrs_access(struct kvm_vcpu *vcpu, · line 324

Quoted 3 lines
arch/x86/kvm/vmx/pmu_intel.c +1 -1
-	if (!lbr_desc->event && !intel_pmu_create_guest_lbr_event(vcpu))
+	if (!lbr_desc->event && intel_pmu_create_guest_lbr_event(vcpu))
  		goto dummy;

Wouldn't it be better to create an event only on write? And really, why create the event in this flow in the first place? In normal operation, can't event creation be deferred until GUEST_IA32_DEBUGCTL.DEBUGCTLMSR_LBR=1?

We need event creation and pass-through for both read and write.

The LBR driver would firstly access the MSR_LBR_SELECT to configure branch types and we also create LBR event for GUEST_IA32_DEBUGCTL.DEBUGCTLMSR_LBR=1 trap. A lazy approach requests more cached values and more traps.

Quoted 3 lines

If event
creation fails in that flow, I would think KVM would do its best to create an event in future runs without waiting for additional actions from the guest.

We have done this via releasing the LBR event for next creation and pass-through try.

Quoted 2 lines

Also, this bug suggests there's a big gaping hole in the test coverage.

Not a big but concern one. To hit that, we need run LBR agent on the host and grab LBR from the guest. And it's not covered in the current test cases since we do not recommend this kind of usage in the comment.

Quoted 3 lines

AFAICT,
event contention would lead to a #GP crash in the host due to lbr_desc->event being dereferenced, no?

a #GP crash in the host ?Can you share more understanding about it ?

Quoted 6 lines
Quoted 6 lines
arch/x86/kvm/vmx/pmu_intel.c +0 -0
  	/*
Signature · 1 line
2.29.2
#8 lore raw
Quoted 22 lines · Like
Quoted 21 lines · Sean Christopherson
Quoted 20 lines · Like Xu

If lbr_desc->event is successfully created, the intel_pmu_create_ guest_lbr_event() will return 0, otherwise it will return -ENOENT, and then jump to LBR msrs dummy handling.

Fixes 1b5ac3226a1a ("KVM: vmx/pmu: Pass-through LBR msrs when the guest LBR event is ACTIVE")
---
  arch/x86/kvm/vmx/pmu_intel.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/kvm/vmx/pmu_intel.c +1 -1
index d1df618cb7de..d6a5fe19ff09 100644
@@ -320,7 +320,7 @@ static bool intel_pmu_handle_lbr_msrs_access(struct kvm_vcpu *vcpu,
  	if (!intel_pmu_is_valid_lbr_msr(vcpu, index))
  		return false;
-	if (!lbr_desc->event && !intel_pmu_create_guest_lbr_event(vcpu))
+	if (!lbr_desc->event && intel_pmu_create_guest_lbr_event(vcpu))
  		goto dummy;

...

Quoted 4 lines
Quoted 2 lines

AFAICT, event contention would lead to a #GP crash in the host due to lbr_desc->event being dereferenced, no?

a #GP crash in the host ?Can you share more understanding about it ?

The original code is will dereference a null lbr_desc->event if intel_pmu_create_guest_lbr_event() fails.

	if (!lbr_desc->event && intel_pmu_create_guest_lbr_event(vcpu))  <- falls through
		goto dummy;

	/*
	 * Disable irq to ensure the LBR feature doesn't get reclaimed by the
	 * host at the time the value is read from the msr, and this avoids the
	 * host LBR value to be leaked to the guest. If LBR has been reclaimed,
	 * return 0 on guest reads.
	 */
	local_irq_disable();
	if (lbr_desc->event->state == PERF_EVENT_STATE_ACTIVE) { <--------- kaboom
		if (read)
			rdmsrl(index, msr_info->data);
		else
			wrmsrl(index, msr_info->data);
		__set_bit(INTEL_PMC_IDX_FIXED_VLBR, vcpu_to_pmu(vcpu)->pmc_in_use);
		local_irq_enable();
		return true;
	}