KVM: ensure APICv is considered inactive if there is no APIC
Paolo Bonzini
maintainerPATCH 1/1 KVM: ensure APICv is considered inactive if there is no APIC
kvm_vcpu_apicv_active() returns false if a virtual machine has no in-kernel local APIC, however kvm_apicv_activated might still be true if there are no reasons to disable APICv; in fact it is quite likely that there is none because APICv is inhibited by specific configurations of the local APIC and those configurations cannot be programmed. This triggers a WARN:
WARN_ON_ONCE(kvm_apicv_activated(vcpu->kvm) != kvm_vcpu_apicv_active(vcpu));
To avoid this, introduce another cause for APICv inhibition, namely the absence of an in-kernel local APIC. This cause is enabled by default, and is dropped by either KVM_CREATE_IRQCHIP or the enabling of KVM_CAP_IRQCHIP_SPLIT.
--- arch/x86/include/asm/kvm_host.h | 1 + arch/x86/kvm/svm/avic.c | 1 + arch/x86/kvm/vmx/vmx.c | 1 + arch/x86/kvm/x86.c | 9 +++++---- 4 files changed, 8 insertions(+), 4 deletions(-)
arch/x86/include/asm/kvm_host.h +1 -0
@@ -1036,6 +1036,7 @@ struct kvm_x86_msr_filter {#define APICV_INHIBIT_REASON_PIT_REINJ 4#define APICV_INHIBIT_REASON_X2APIC 5#define APICV_INHIBIT_REASON_BLOCKIRQ 6 +#define APICV_INHIBIT_REASON_ABSENT 7struct kvm_arch {unsigned long n_used_mmu_pages;
arch/x86/kvm/svm/avic.c +1 -0
@@ -900,6 +900,7 @@ int svm_update_pi_irte(struct kvm *kvm, unsigned int host_irq,bool svm_check_apicv_inhibit_reasons (ulong bit) {ulong supported =BIT (APICV_INHIBIT_REASON_DISABLE ) | +BIT (APICV_INHIBIT_REASON_ABSENT ) |BIT (APICV_INHIBIT_REASON_HYPERV ) |BIT (APICV_INHIBIT_REASON_NESTED ) | BIT(APICV_INHIBIT_REASON_IRQWIN) |
arch/x86/kvm/vmx/vmx.c +1 -0
@@ -7525,6 +7525,7 @@ 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_ABSENT ) |BIT (APICV_INHIBIT_REASON_HYPERV ) |BIT (APICV_INHIBIT_REASON_BLOCKIRQ );
arch/x86/kvm/x86.c +5 -4
@@ -5740,6 +5740,7 @@ int kvm_vm_ioctl_enable_cap(struct kvm *kvm,smp_wmb (); kvm->arch.irqchip_mode =KVM_IRQCHIP_SPLIT ; kvm->arch.nr_reserved_ioapic_pins = cap->args[0 ]; +kvm_request_apicv_update (kvm, true,APICV_INHIBIT_REASON_ABSENT ); r =0 ; split_irqchip_unlock:mutex_unlock (&kvm->lock);
@@ -6120,6 +6121,7 @@ long kvm_arch_vm_ioctl(struct file *filp,/* Write kvm->irq_routing before enabling irqchip_in_kernel. */ smp_wmb (); kvm->arch.irqchip_mode =KVM_IRQCHIP_KERNEL ; +kvm_request_apicv_update (kvm, true,APICV_INHIBIT_REASON_ABSENT ); create_irqchip_unlock:mutex_unlock (&kvm->lock);break ;
@@ -8818,10 +8820,9 @@ static void kvm_apicv_init(struct kvm *kvm) {init_rwsem (&kvm->arch.apicv_update_lock); -if (enable_apicv) -clear_bit (APICV_INHIBIT_REASON_DISABLE , - &kvm->arch.apicv_inhibit_reasons); -else +set_bit (APICV_INHIBIT_REASON_ABSENT , + &kvm->arch.apicv_inhibit_reasons); +if (!enable_apicv)set_bit (APICV_INHIBIT_REASON_DISABLE , &kvm->arch.apicv_inhibit_reasons); }
Signature · 1 line
2.31.1
Quoted 95 lines · Paolo Bonzini
kvm_vcpu_apicv_active()returns false if a virtual machine has no in-kernel local APIC, howeverkvm_apicv_activatedmight still be true if there are no reasons to disable APICv; in fact it is quite likely that there is none because APICv is inhibited by specific configurations of the local APIC and those configurations cannot be programmed. This triggers a WARN:WARN_ON_ONCE(kvm_apicv_activated(vcpu->kvm) != kvm_vcpu_apicv_active(vcpu));To avoid this, introduce another cause for APICv inhibition, namely the absence of an in-kernel local APIC. This cause is enabled by default, and is dropped by either
KVM_CREATE_IRQCHIPor the enabling ofKVM_CAP_IRQCHIP_SPLIT.Reported-by Ignat Korchagin <[email protected]>Fixes ee49a8932971 ("KVM: x86: Move SVM's APICv sanity check to common x86", 2021-10-22)Signed-off-by Paolo Bonzini <[email protected]>--- arch/x86/include/asm/kvm_host.h | 1 + arch/x86/kvm/svm/avic.c | 1 + arch/x86/kvm/vmx/vmx.c | 1 + arch/x86/kvm/x86.c | 9 +++++---- 4 files changed, 8 insertions(+), 4 deletions(-)arch/x86/include/asm/kvm_host.h +1
-0@@ -1036,6 +1036,7 @@ struct kvm_x86_msr_filter {#define APICV_INHIBIT_REASON_PIT_REINJ 4#define APICV_INHIBIT_REASON_X2APIC 5#define APICV_INHIBIT_REASON_BLOCKIRQ 6 +#define APICV_INHIBIT_REASON_ABSENT 7struct kvm_arch {unsigned long n_used_mmu_pages;arch/x86/kvm/svm/avic.c +1
-0@@ -900,6 +900,7 @@ int svm_update_pi_irte(struct kvm *kvm, unsigned int host_irq,bool svm_check_apicv_inhibit_reasons (ulong bit) {ulong supported =BIT (APICV_INHIBIT_REASON_DISABLE ) | +BIT (APICV_INHIBIT_REASON_ABSENT ) |BIT (APICV_INHIBIT_REASON_HYPERV ) |BIT (APICV_INHIBIT_REASON_NESTED ) | BIT(APICV_INHIBIT_REASON_IRQWIN) |arch/x86/kvm/vmx/vmx.c +1
-0@@ -7525,6 +7525,7 @@ 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_ABSENT ) |BIT (APICV_INHIBIT_REASON_HYPERV ) |BIT (APICV_INHIBIT_REASON_BLOCKIRQ );arch/x86/kvm/x86.c +5
-5@@ -5740,6 +5740,7 @@ int kvm_vm_ioctl_enable_cap(struct kvm *kvm,smp_wmb (); kvm->arch.irqchip_mode =KVM_IRQCHIP_SPLIT ; kvm->arch.nr_reserved_ioapic_pins = cap->args[0 ]; +kvm_request_apicv_update (kvm, true,APICV_INHIBIT_REASON_ABSENT ); r =0 ; split_irqchip_unlock:mutex_unlock (&kvm->lock);@@ -6120,6 +6121,7 @@ long kvm_arch_vm_ioctl(struct file *filp,/* Write kvm->irq_routing before enabling irqchip_in_kernel. */ smp_wmb (); kvm->arch.irqchip_mode =KVM_IRQCHIP_KERNEL ; +kvm_request_apicv_update (kvm, true,APICV_INHIBIT_REASON_ABSENT ); create_irqchip_unlock:mutex_unlock (&kvm->lock);break ;@@ -8818,10 +8820,9 @@ static void kvm_apicv_init(struct kvm *kvm) {init_rwsem (&kvm->arch.apicv_update_lock); -if (enable_apicv) -clear_bit (APICV_INHIBIT_REASON_DISABLE , - &kvm->arch.apicv_inhibit_reasons); -else +set_bit (APICV_INHIBIT_REASON_ABSENT , + &kvm->arch.apicv_inhibit_reasons); +if (!enable_apicv)set_bit (APICV_INHIBIT_REASON_DISABLE , &kvm->arch.apicv_inhibit_reasons); } --
Quoted 2 lines
2.31.1
Quoted 13 lines · Paolo Bonzini
kvm_vcpu_apicv_active()returns false if a virtual machine has no in-kernel local APIC, howeverkvm_apicv_activatedmight still be true if there are no reasons to disable APICv; in fact it is quite likely that there is none because APICv is inhibited by specific configurations of the local APIC and those configurations cannot be programmed. This triggers a WARN:WARN_ON_ONCE(kvm_apicv_activated(vcpu->kvm) != kvm_vcpu_apicv_active(vcpu));To avoid this, introduce another cause for APICv inhibition, namely the absence of an in-kernel local APIC. This cause is enabled by default, and is dropped by either
KVM_CREATE_IRQCHIPor the enabling ofKVM_CAP_IRQCHIP_SPLIT.
Quoted 4 lines
Reported-by Ignat Korchagin <[email protected]>Fixes ee49a8932971 ("KVM: x86: Move SVM's APICv sanity check to common x86", 2021-10-22)Signed-off-by Paolo Bonzini <[email protected]>---
Quoted 13 lines
arch/x86/kvm/x86.c +1
-0@@ -5740,6 +5740,7 @@ int kvm_vm_ioctl_enable_cap(struct kvm *kvm,smp_wmb (); kvm->arch.irqchip_mode =KVM_IRQCHIP_SPLIT ; kvm->arch.nr_reserved_ioapic_pins = cap->args[0 ]; +kvm_request_apicv_update (kvm, true,APICV_INHIBIT_REASON_ABSENT ); r =0 ; split_irqchip_unlock:mutex_unlock (&kvm->lock);
arch/x86/kvm/x86.c long kvm_arch_vm_ioctl(struct file *filp, · line 6124
Quoted 4 lines
arch/x86/kvm/x86.c +1
-0@@ -6120,6 +6121,7 @@ long kvm_arch_vm_ioctl(struct file *filp,/* Write kvm->irq_routing before enabling irqchip_in_kernel. */ smp_wmb (); kvm->arch.irqchip_mode =KVM_IRQCHIP_KERNEL ; +kvm_request_apicv_update (kvm, true,APICV_INHIBIT_REASON_ABSENT );
Blech, kvm_request_apicv_update() is very counter-intuitive, true == clear. :-/ Wrappers along the lines of kvm_{set,clear}_apicv_inhibit() would help a lot, and would likely avoid a handful of newlines as well. I'll send a patch on top of this, unless you want to do it while pushing this one out.
arch/x86/kvm/x86.c static void kvm_apicv_init(struct kvm *kvm) · line 8824
Quoted 13 lines
arch/x86/kvm/x86.c +2
-4create_irqchip_unlock:mutex_unlock (&kvm->lock);break ;@@ -8818,10 +8820,9 @@ static void kvm_apicv_init(struct kvm *kvm) {init_rwsem (&kvm->arch.apicv_update_lock); -if (enable_apicv) -clear_bit (APICV_INHIBIT_REASON_DISABLE , - &kvm->arch.apicv_inhibit_reasons); -else +set_bit (APICV_INHIBIT_REASON_ABSENT , + &kvm->arch.apicv_inhibit_reasons);
Nit, this one fits on a single line.
Quoted 7 lines
arch/x86/kvm/x86.c +1
-0+if (!enable_apicv)set_bit (APICV_INHIBIT_REASON_DISABLE , &kvm->arch.apicv_inhibit_reasons); }Signature · 1 line
2.31.1
Maxim Levitsky
Reviewed-byQuoted 35 lines · Sean Christopherson
Quoted 13 lines · Paolo Bonzini
kvm_vcpu_apicv_active()returns false if a virtual machine has no in-kernel local APIC, howeverkvm_apicv_activatedmight still be true if there are no reasons to disable APICv; in fact it is quite likely that there is none because APICv is inhibited by specific configurations of the local APIC and those configurations cannot be programmed. This triggers a WARN:WARN_ON_ONCE(kvm_apicv_activated(vcpu->kvm) != kvm_vcpu_apicv_active(vcpu));To avoid this, introduce another cause for APICv inhibition, namely the absence of an in-kernel local APIC. This cause is enabled by default, and is dropped by either
KVM_CREATE_IRQCHIPor the enabling ofKVM_CAP_IRQCHIP_SPLIT.Quoted 4 lines
Reported-by Ignat Korchagin <[email protected]>Fixes ee49a8932971 ("KVM: x86: Move SVM's APICv sanity check to common x86", 2021-10-22)Signed-off-by Paolo Bonzini <[email protected]>---
Reviewed-by Sean Christopherson <[email protected]>Quoted 14 lines
arch/x86/kvm/x86.c +1
-0@@ -5740,6 +5740,7 @@ int kvm_vm_ioctl_enable_cap(struct kvm *kvm,smp_wmb (); kvm->arch.irqchip_mode =KVM_IRQCHIP_SPLIT ; kvm->arch.nr_reserved_ioapic_pins = cap->args[0 ]; +kvm_request_apicv_update (kvm, true,APICV_INHIBIT_REASON_ABSENT ); r =0 ; split_irqchip_unlock:mutex_unlock (&kvm->lock);@@ -6120,6 +6121,7 @@ long kvm_arch_vm_ioctl(struct file *filp,/* Write kvm->irq_routing before enabling irqchip_in_kernel. */
Quoted 8 lines
arch/x86/kvm/x86.c long kvm_arch_vm_ioctl(struct file *filp, · line 6124
Quoted 3 lines
arch/x86/kvm/x86.c +1
-0smp_wmb (); kvm->arch.irqchip_mode =KVM_IRQCHIP_KERNEL ; +kvm_request_apicv_update (kvm, true,APICV_INHIBIT_REASON_ABSENT );Blech,
kvm_request_apicv_update()is very counter-intuitive, true == clear. :-/ Wrappers along the lines of kvm_{set,clear}_apicv_inhibit() would help a lot, and would likely avoid a handful of newlines as well. I'll send a patch on top of this, unless you want to do it while pushing this one out.
100% agree that kvm_request_apicv_update() is very counter-intuitive!
Best regards,
Maxim Levitsky
Quoted 24 lines
Quoted 5 lines
arch/x86/kvm/x86.c +0
-0create_irqchip_unlock:mutex_unlock (&kvm->lock);break ;arch/x86/kvm/x86.c static void kvm_apicv_init(struct kvm *kvm) · line 8824
Quoted 9 lines
arch/x86/kvm/x86.c +2
-4@@ -8818,10 +8820,9 @@ static void kvm_apicv_init(struct kvm *kvm) {init_rwsem (&kvm->arch.apicv_update_lock); -if (enable_apicv) -clear_bit (APICV_INHIBIT_REASON_DISABLE , - &kvm->arch.apicv_inhibit_reasons); -else +set_bit (APICV_INHIBIT_REASON_ABSENT , + &kvm->arch.apicv_inhibit_reasons);Nit, this one fits on a single line.
Quoted 7 lines
arch/x86/kvm/x86.c +1
-0+if (!enable_apicv)set_bit (APICV_INHIBIT_REASON_DISABLE , &kvm->arch.apicv_inhibit_reasons); }Signature · 1 line
2.31.1
Quoted 9 lines · Paolo Bonzini
kvm_vcpu_apicv_active()returns false if a virtual machine has no in-kernel local APIC, howeverkvm_apicv_activatedmight still be true if there are no reasons to disable APICv; in fact it is quite likely that there is none because APICv is inhibited by specific configurations of the local APIC and those configurations cannot be programmed. This triggers a WARN:WARN_ON_ONCE(kvm_apicv_activated(vcpu->kvm) != kvm_vcpu_apicv_active(vcpu));
Quoted 8 lines
To avoid this, introduce another cause for APICv inhibition, namely the absence of an in-kernel local APIC. This cause is enabled by default, and is dropped by either
KVM_CREATE_IRQCHIPor the enabling ofKVM_CAP_IRQCHIP_SPLIT.Reported-by Ignat Korchagin <[email protected]>Fixes ee49a8932971 ("KVM: x86: Move SVM's APICv sanity check to common x86", 2021-10-22)Signed-off-by Paolo Bonzini <[email protected]>
Quoted 80 lines
--- arch/x86/include/asm/kvm_host.h | 1 + arch/x86/kvm/svm/avic.c | 1 + arch/x86/kvm/vmx/vmx.c | 1 + arch/x86/kvm/x86.c | 9 +++++---- 4 files changed, 8 insertions(+), 4 deletions(-)arch/x86/include/asm/kvm_host.h +1
-0@@ -1036,6 +1036,7 @@ struct kvm_x86_msr_filter {#define APICV_INHIBIT_REASON_PIT_REINJ 4#define APICV_INHIBIT_REASON_X2APIC 5#define APICV_INHIBIT_REASON_BLOCKIRQ 6 +#define APICV_INHIBIT_REASON_ABSENT 7struct kvm_arch {unsigned long n_used_mmu_pages;arch/x86/kvm/svm/avic.c +1
-0@@ -900,6 +900,7 @@ int svm_update_pi_irte(struct kvm *kvm, unsigned int host_irq,bool svm_check_apicv_inhibit_reasons (ulong bit) {ulong supported =BIT (APICV_INHIBIT_REASON_DISABLE ) | +BIT (APICV_INHIBIT_REASON_ABSENT ) |BIT (APICV_INHIBIT_REASON_HYPERV ) |BIT (APICV_INHIBIT_REASON_NESTED ) | BIT(APICV_INHIBIT_REASON_IRQWIN) |arch/x86/kvm/vmx/vmx.c +1
-0@@ -7525,6 +7525,7 @@ 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_ABSENT ) |BIT (APICV_INHIBIT_REASON_HYPERV ) |BIT (APICV_INHIBIT_REASON_BLOCKIRQ );arch/x86/kvm/x86.c +5
-5@@ -5740,6 +5740,7 @@ int kvm_vm_ioctl_enable_cap(struct kvm *kvm,smp_wmb (); kvm->arch.irqchip_mode =KVM_IRQCHIP_SPLIT ; kvm->arch.nr_reserved_ioapic_pins = cap->args[0 ]; +kvm_request_apicv_update (kvm, true,APICV_INHIBIT_REASON_ABSENT ); r =0 ; split_irqchip_unlock:mutex_unlock (&kvm->lock);@@ -6120,6 +6121,7 @@ long kvm_arch_vm_ioctl(struct file *filp,/* Write kvm->irq_routing before enabling irqchip_in_kernel. */ smp_wmb (); kvm->arch.irqchip_mode =KVM_IRQCHIP_KERNEL ; +kvm_request_apicv_update (kvm, true,APICV_INHIBIT_REASON_ABSENT ); create_irqchip_unlock:mutex_unlock (&kvm->lock);break ;@@ -8818,10 +8820,9 @@ static void kvm_apicv_init(struct kvm *kvm) {init_rwsem (&kvm->arch.apicv_update_lock); -if (enable_apicv) -clear_bit (APICV_INHIBIT_REASON_DISABLE , - &kvm->arch.apicv_inhibit_reasons); -else +set_bit (APICV_INHIBIT_REASON_ABSENT , + &kvm->arch.apicv_inhibit_reasons); +if (!enable_apicv)set_bit (APICV_INHIBIT_REASON_DISABLE , &kvm->arch.apicv_inhibit_reasons); } --2.31.1