Skip to contentSkip to conversation

KVM: x86: inhibit APICv when KVM_GUESTDBG_BLOCKIRQ active

PATCH Superseded Maxim Levitsky 2 participants 3 messages Nov 3, 2021 – Nov 8, 2021

Subsystems (3)

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 KVM: x86: inhibit APICv when KVM_GUESTDBG_BLOCKIRQ active

KVM_GUESTDBG_BLOCKIRQ relies on interrupts being injected using standard kvm's inject_pending_event, and not via APICv/AVIC.

Since this is a debug feature, just inhibit APICv/AVIC while KVM_GUESTDBG_BLOCKIRQ is in use on at least one vCPU.

Fixes 61e5f69ef0837 ("KVM: x86: implement KVM_GUESTDBG_BLOCKIRQ")
---
 arch/x86/include/asm/kvm_host.h |  1 +
 arch/x86/kvm/svm/avic.c         |  3 ++-
 arch/x86/kvm/vmx/vmx.c          |  3 ++-
 arch/x86/kvm/x86.c              | 21 +++++++++++++++++++++
 4 files changed, 26 insertions(+), 2 deletions(-)
arch/x86/include/asm/kvm_host.h +1 -0
index 88fce6ab4bbd7..8f6e15b95a4d8 100644
@@ -1034,6 +1034,7 @@ struct kvm_x86_msr_filter {
 #define APICV_INHIBIT_REASON_IRQWIN     3
 #define APICV_INHIBIT_REASON_PIT_REINJ  4
 #define APICV_INHIBIT_REASON_X2APIC	5
+#define APICV_INHIBIT_REASON_BLOCKIRQ	6
 
 struct kvm_arch {
 	unsigned long n_used_mmu_pages;
arch/x86/kvm/svm/avic.c +2 -1
index 8052d92069e01..affc0ea98d302 100644
@@ -904,7 +904,8 @@ bool svm_check_apicv_inhibit_reasons(ulong bit)
 			  BIT(APICV_INHIBIT_REASON_NESTED) |
 			  BIT(APICV_INHIBIT_REASON_IRQWIN) |
 			  BIT(APICV_INHIBIT_REASON_PIT_REINJ) |
-			  BIT(APICV_INHIBIT_REASON_X2APIC);
+			  BIT(APICV_INHIBIT_REASON_X2APIC) |
+			  BIT(APICV_INHIBIT_REASON_BLOCKIRQ);
 
 	return supported & BIT(bit);
 }
arch/x86/kvm/vmx/vmx.c +2 -1
index 71f54d85f104c..e4fc9ff7cd944 100644
@@ -7565,7 +7565,8 @@ static void hardware_unsetup(void)
 static bool vmx_check_apicv_inhibit_reasons(ulong bit)
 {
 	ulong supported = BIT(APICV_INHIBIT_REASON_DISABLE) |
-			  BIT(APICV_INHIBIT_REASON_HYPERV);
+			  BIT(APICV_INHIBIT_REASON_HYPERV) |
+			  BIT(APICV_INHIBIT_REASON_BLOCKIRQ);
 
 	return supported & BIT(bit);
 }
arch/x86/kvm/x86.c +21 -0
index ac83d873d65b0..5d30cea58182e 100644
@@ -10703,6 +10703,25 @@ int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
 	return ret;
 }
 
+static void kvm_arch_vcpu_guestdbg_update_apicv_inhibit(struct kvm *kvm)
+{
+	struct kvm_vcpu *vcpu = NULL;
+	int i;
+	bool block_irq_used = false;
+
+	down_write(&kvm->arch.apicv_update_lock);
+
+	kvm_for_each_vcpu(i, vcpu, kvm) {
+		if (vcpu->guest_debug & KVM_GUESTDBG_BLOCKIRQ) {
+			block_irq_used = true;
+			break;
+		}
+	}
+	__kvm_request_apicv_update(kvm, !block_irq_used,
+					       APICV_INHIBIT_REASON_BLOCKIRQ);
+	up_write(&kvm->arch.apicv_update_lock);
+}
+
 int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu,
 					struct kvm_guest_debug *dbg)
 {
@@ -10755,6 +10774,8 @@ int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu,
 
 	static_call(kvm_x86_update_exception_bitmap)(vcpu);
 
+	kvm_arch_vcpu_guestdbg_update_apicv_inhibit(vcpu->kvm);
+
 	r = 0;
 
 out:
Signature · 1 line
2.26.3
#1 lore raw
Quoted 5 lines · Maxim Levitsky

KVM_GUESTDBG_BLOCKIRQ relies on interrupts being injected using standard kvm's inject_pending_event, and not via APICv/AVIC.

Since this is a debug feature, just inhibit APICv/AVIC while KVM_GUESTDBG_BLOCKIRQ is in use on at least one vCPU.

Very clever!

Quoted 4 lines
Fixes 61e5f69ef0837 ("KVM: x86: implement KVM_GUESTDBG_BLOCKIRQ")

---

With the below nits resolved (tested on Intel w/ APICv):

arch/x86/kvm/x86.c int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu, · line 10708

Quoted 11 lines
arch/x86/kvm/x86.c +3 -0
index ac83d873d65b0..5d30cea58182e 100644
@@ -10703,6 +10703,25 @@ int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
 	return ret;
 }
 
+static void kvm_arch_vcpu_guestdbg_update_apicv_inhibit(struct kvm *kvm)
+{
+	struct kvm_vcpu *vcpu = NULL;

vcpu doesn't need to be initialized.

arch/x86/kvm/x86.c int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu, · line 10709

Quoted 1 line
arch/x86/kvm/x86.c +1 -0
+	int i;

Nit, I'd prefer we use reverse fir tree when it's convenient, i.e.

	bool block_irq_used = false;
	struct kvm_vcpu *vcpu;
	int i;

arch/x86/kvm/x86.c int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu, · line 10721

Quoted 12 lines
arch/x86/kvm/x86.c +12 -0
+	bool block_irq_used = false;
+
+	down_write(&kvm->arch.apicv_update_lock);
+
+	kvm_for_each_vcpu(i, vcpu, kvm) {
+		if (vcpu->guest_debug & KVM_GUESTDBG_BLOCKIRQ) {
+			block_irq_used = true;
+			break;
+		}
+	}
+	__kvm_request_apicv_update(kvm, !block_irq_used,
+					       APICV_INHIBIT_REASON_BLOCKIRQ);

Heh, this indentation is still messed up, I think you need to change your

	if (r == -ENOCOFFEE)
		maxim_get_coffee();

to

	while (r == -ENOCOFFEE)
		r = maxim_get_coffee();
Quoted 18 lines
arch/x86/kvm/x86.c +5 -0
+	up_write(&kvm->arch.apicv_update_lock);
+}
+
 int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu,
 					struct kvm_guest_debug *dbg)
 {
@@ -10755,6 +10774,8 @@ int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu,
 
 	static_call(kvm_x86_update_exception_bitmap)(vcpu);
 
+	kvm_arch_vcpu_guestdbg_update_apicv_inhibit(vcpu->kvm);
+
 	r = 0;
 
 out:
Signature · 1 line
2.26.3
#2 lore raw
Quoted 8 lines · Sean Christopherson
Quoted 5 lines · Maxim Levitsky

KVM_GUESTDBG_BLOCKIRQ relies on interrupts being injected using standard kvm's inject_pending_event, and not via APICv/AVIC.

Since this is a debug feature, just inhibit APICv/AVIC while KVM_GUESTDBG_BLOCKIRQ is in use on at least one vCPU.

Very clever!

Thanks! It is now possible to enjoy this,
after we broke our back making APICv/AVIC inhibition actually work...

Quoted 18 lines
Quoted 5 lines
Fixes 61e5f69ef0837 ("KVM: x86: implement KVM_GUESTDBG_BLOCKIRQ")

---

With the below nits resolved (tested on Intel w/ APICv):

Quoted 8 lines
arch/x86/kvm/x86.c +0 -0
index ac83d873d65b0..5d30cea58182e 100644
@@ -10703,6 +10703,25 @@ int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
 	return ret;
 }
 
Quoted 5 lines

arch/x86/kvm/x86.c int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu, · line 10708

Quoted 3 lines
arch/x86/kvm/x86.c +3 -0
+static void kvm_arch_vcpu_guestdbg_update_apicv_inhibit(struct kvm *kvm)
+{
+	struct kvm_vcpu *vcpu = NULL;

vcpu doesn't need to be initialized.

True, fixed in v4

Quoted 4 lines

arch/x86/kvm/x86.c int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu, · line 10709

Quoted 2 lines
arch/x86/kvm/x86.c +1 -0
+	int i;

Nit, I'd prefer we use reverse fir tree when it's convenient, i.e.

Fixed in v4

Quoted 20 lines
	bool block_irq_used = false;
	struct kvm_vcpu *vcpu;
	int i;

arch/x86/kvm/x86.c int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu, · line 10721

Quoted 12 lines
arch/x86/kvm/x86.c +12 -0
+	bool block_irq_used = false;
+
+	down_write(&kvm->arch.apicv_update_lock);
+
+	kvm_for_each_vcpu(i, vcpu, kvm) {
+		if (vcpu->guest_debug & KVM_GUESTDBG_BLOCKIRQ) {
+			block_irq_used = true;
+			break;
+		}
+	}
+	__kvm_request_apicv_update(kvm, !block_irq_used,
+					       APICV_INHIBIT_REASON_BLOCKIRQ);

Heh, this indentation is still messed up, I think you need to change your

Quoted 7 lines
	if (r == -ENOCOFFEE)
		maxim_get_coffee();

to

	while (r == -ENOCOFFEE)
		r = maxim_get_coffee();

Yep :-)

Quoted 11 lines
Quoted 11 lines
arch/x86/kvm/x86.c +3 -0
+	up_write(&kvm->arch.apicv_update_lock);
+}
+
 int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu,
 					struct kvm_guest_debug *dbg)
 {
@@ -10755,6 +10774,8 @@ int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu,
 
 	static_call(kvm_x86_update_exception_bitmap)(vcpu);
 
Quoted 8 lines
Quoted 8 lines
arch/x86/kvm/x86.c +2 -0
+	kvm_arch_vcpu_guestdbg_update_apicv_inhibit(vcpu->kvm);
+
 	r = 0;
 
 out:
Signature · 1 line
2.26.3

Thanks for the review,
Best regards,
Maxim Levitsky