KVM: x86: inhibit APICv when KVM_GUESTDBG_BLOCKIRQ active
PATCH v2 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 it while it is in use.
--- 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 | 3 +++ 4 files changed, 8 insertions(+), 2 deletions(-)
arch/x86/include/asm/kvm_host.h +1 -0
@@ -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 6struct kvm_arch {unsigned long n_used_mmu_pages;
arch/x86/kvm/svm/avic.c +2 -1
@@ -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
@@ -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 +3 -0
@@ -10747,6 +10747,9 @@ int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu,if (vcpu->guest_debug &KVM_GUESTDBG_SINGLESTEP ) vcpu->arch.singlestep_rip =kvm_get_linear_rip (vcpu); +kvm_request_apicv_update (vcpu->kvm, + !(vcpu->guest_debug &KVM_GUESTDBG_BLOCKIRQ ), +APICV_INHIBIT_REASON_BLOCKIRQ );/* * Trigger an rflags update that will inject or remove the trace * flags.
Signature · 1 line
2.26.3
Quoted 61 lines · Maxim Levitsky
KVM_GUESTDBG_BLOCKIRQrelies on interrupts being injected using standard kvm'sinject_pending_event, and not via APICv/AVIC.Since this is a debug feature, just inhibit it while it is in use.
Fixes 61e5f69ef0837 ("KVM: x86: implement KVM_GUESTDBG_BLOCKIRQ")Signed-off-by Maxim Levitsky <[email protected]>--- 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 | 3 +++ 4 files changed, 8 insertions(+), 2 deletions(-)arch/x86/include/asm/kvm_host.h +1
-0@@ -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 6struct kvm_arch {unsigned long n_used_mmu_pages;arch/x86/kvm/svm/avic.c +2
-1@@ -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@@ -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 +0
-0
arch/x86/kvm/x86.c int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu, · line 10755
Quoted 9 lines
arch/x86/kvm/x86.c +3
-0@@ -10747,6 +10747,9 @@ int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu,if (vcpu->guest_debug &KVM_GUESTDBG_SINGLESTEP ) vcpu->arch.singlestep_rip =kvm_get_linear_rip (vcpu); +kvm_request_apicv_update (vcpu->kvm, + !(vcpu->guest_debug &KVM_GUESTDBG_BLOCKIRQ ), +APICV_INHIBIT_REASON_BLOCKIRQ );/* * Trigger an rflags update that will inject or remove the trace * flags.
This fixes the problem for me!
Signature · 1 line
Vitaly
Quoted 75 lines · Vitaly Kuznetsov
Quoted 61 lines · Maxim Levitsky
KVM_GUESTDBG_BLOCKIRQrelies on interrupts being injected using standard kvm'sinject_pending_event, and not via APICv/AVIC.Since this is a debug feature, just inhibit it while it is in use.
Fixes 61e5f69ef0837 ("KVM: x86: implement KVM_GUESTDBG_BLOCKIRQ")Signed-off-by Maxim Levitsky <[email protected]>--- 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 | 3 +++ 4 files changed, 8 insertions(+), 2 deletions(-)arch/x86/include/asm/kvm_host.h +1
-0@@ -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 6struct kvm_arch {unsigned long n_used_mmu_pages;arch/x86/kvm/svm/avic.c +2
-1@@ -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@@ -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 +0
-0arch/x86/kvm/x86.c int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu, · line 10755
Quoted 9 lines
arch/x86/kvm/x86.c +3
-0@@ -10747,6 +10747,9 @@ int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu,if (vcpu->guest_debug &KVM_GUESTDBG_SINGLESTEP ) vcpu->arch.singlestep_rip =kvm_get_linear_rip (vcpu); +kvm_request_apicv_update (vcpu->kvm, + !(vcpu->guest_debug &KVM_GUESTDBG_BLOCKIRQ ), +APICV_INHIBIT_REASON_BLOCKIRQ );/* * Trigger an rflags update that will inject or remove the trace * flags. This fixes the problem for me!
Quoted 1 line
Reviewed-by Vitaly Kuznetsov <[email protected]>
Cool!
Quoted 1 line
Now that I think about it, since guest debug flags are per-vcpu, this code won't
work if there are multiple vCPUs and you enable KVM_GUESTDBG_BLOCKIRQ on all of them
and then disable on this flag on just one of vCPUs, because this code will re-enable APICv/AVIC in this case. A counter is needed, like you did in synic/autoeoi case.
I'll send a V3 soon.
Best regards,
Maxim Levitsky
Quoted 78 lines · Maxim Levitsky
Quoted 75 lines · Vitaly Kuznetsov
Quoted 61 lines · Maxim Levitsky
KVM_GUESTDBG_BLOCKIRQrelies on interrupts being injected using standard kvm'sinject_pending_event, and not via APICv/AVIC.Since this is a debug feature, just inhibit it while it is in use.
Fixes 61e5f69ef0837 ("KVM: x86: implement KVM_GUESTDBG_BLOCKIRQ")Signed-off-by Maxim Levitsky <[email protected]>--- 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 | 3 +++ 4 files changed, 8 insertions(+), 2 deletions(-)arch/x86/include/asm/kvm_host.h +1
-0@@ -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 6struct kvm_arch {unsigned long n_used_mmu_pages;arch/x86/kvm/svm/avic.c +2
-1@@ -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@@ -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 +0
-0arch/x86/kvm/x86.c int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu, · line 10755
Quoted 9 lines
arch/x86/kvm/x86.c +3
-0@@ -10747,6 +10747,9 @@ int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu,if (vcpu->guest_debug &KVM_GUESTDBG_SINGLESTEP ) vcpu->arch.singlestep_rip =kvm_get_linear_rip (vcpu); +kvm_request_apicv_update (vcpu->kvm, + !(vcpu->guest_debug &KVM_GUESTDBG_BLOCKIRQ ), +APICV_INHIBIT_REASON_BLOCKIRQ );/* * Trigger an rflags update that will inject or remove the trace * flags. This fixes the problem for me!
Quoted 1 line
Reviewed-by Vitaly Kuznetsov <[email protected]>
Quoted 8 lines
Cool!
Quoted 1 line
Now that I think about it, since guest debug flags are per-vcpu, this code won't
work if there are multiple vCPUs and you enableKVM_GUESTDBG_BLOCKIRQon all of them
and then disable on this flag on just one of vCPUs, because this code will re-enable APICv/AVIC in this case. A counter is needed, like you did in synic/autoeoi case.
Right, I completely forgot about this peculiarity!
Signature · 1 line
Vitaly
Quoted 82 lines · Vitaly Kuznetsov
Quoted 80 lines · Maxim Levitsky
Quoted 75 lines · Vitaly Kuznetsov
Quoted 61 lines · Maxim Levitsky
KVM_GUESTDBG_BLOCKIRQrelies on interrupts being injected using standard kvm'sinject_pending_event, and not via APICv/AVIC.Since this is a debug feature, just inhibit it while it is in use.
Fixes 61e5f69ef0837 ("KVM: x86: implement KVM_GUESTDBG_BLOCKIRQ")Signed-off-by Maxim Levitsky <[email protected]>--- 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 | 3 +++ 4 files changed, 8 insertions(+), 2 deletions(-)arch/x86/include/asm/kvm_host.h +1
-0@@ -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 6struct kvm_arch {unsigned long n_used_mmu_pages;arch/x86/kvm/svm/avic.c +2
-1@@ -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@@ -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 +0
-0arch/x86/kvm/x86.c int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu, · line 10755
Quoted 9 lines
arch/x86/kvm/x86.c +3
-0@@ -10747,6 +10747,9 @@ int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu,if (vcpu->guest_debug &KVM_GUESTDBG_SINGLESTEP ) vcpu->arch.singlestep_rip =kvm_get_linear_rip (vcpu); +kvm_request_apicv_update (vcpu->kvm, + !(vcpu->guest_debug &KVM_GUESTDBG_BLOCKIRQ ), +APICV_INHIBIT_REASON_BLOCKIRQ );/* * Trigger an rflags update that will inject or remove the trace * flags. This fixes the problem for me!
Quoted 1 line
Reviewed-by Vitaly Kuznetsov <[email protected]>Cool!
Quoted 7 lines
Quoted 5 lines
Now that I think about it, since guest debug flags are per-vcpu, this code won't
work if there are multiple vCPUs and you enableKVM_GUESTDBG_BLOCKIRQon all of them
and then disable on this flag on just one of vCPUs, because this code will re-enable APICv/AVIC in this case. A counter is needed, like you did in synic/autoeoi case.Right, I completely forgot about this peculiarity!
I am sending v3 of this patch, I decided to avoid a counter and instead just use kvm_for_each_vcpu to see if other vCPUs have that bit set, on the ground of this feature beeing only a debug feature.
Best regards,
Maxim Levitsky