KVM: x86: APIC and I/O APIC cleanups
Subsystems (5)
Sean Christopherson
maintainercover KVM: x86: APIC and I/O APIC cleanups
Drop a bunch of _really_ old dead code (ASSERT() buried behind a DEBUG macro that probably hasn't been enabled in 15+ years), clean up the bizarre and confusing "dest_map" pointer that gets passed all of the place but is only actually used for in-kernel RTC emulation, and the bury almost all of ioapic.h behind CONFIG_KVM_IOAPIC=y.
I'm not entirely sure why I started poking at this. I think I got mad at the dest_map code, and then things snowballed...
Sean Christopherson (9):
KVM: x86: Drop ASSERT()s on APIC/vCPU being non-NULL
KVM: x86: Drop guest/user-triggerable asserts on IRR/ISR vectors
KVM: x86: Drop ASSERT() on I/O APIC EOIs being only for LEVEL_to
WARN_ON_ONCE
KVM: x86: Drop guest-triggerable ASSERT()s on I/O APIC access
alignment
KVM: x86: Drop MAX_NR_RESERVED_IOAPIC_PINS, use KVM_MAX_IRQ_ROUTES
directly
KVM: x86: Add a wrapper to handle common case of IRQ delivery without
dest_map
KVM: x86: Fold "struct dest_map" into "struct rtc_status"
KVM: x86: Bury ioapic.h definitions behind CONFIG_KVM_IOAPIC
KVM: x86: Hide KVM_IRQCHIP_KERNEL behind CONFIG_KVM_IOAPIC=y
arch/x86/include/asm/kvm_host.h | 2 + arch/x86/kvm/hyperv.c | 2 +- arch/x86/kvm/ioapic.c | 43 ++++++++------------- arch/x86/kvm/ioapic.h | 38 ++++++------------- arch/x86/kvm/irq.c | 4 +- arch/x86/kvm/lapic.c | 66 ++++++++++++++++----------------- arch/x86/kvm/lapic.h | 20 +++++++--- arch/x86/kvm/x86.c | 4 +- arch/x86/kvm/xen.c | 2 +- 9 files changed, 81 insertions(+), 100 deletions(-)
base-commit: 5d3e2d9ba9ed68576c70c127e4f7446d896f2af2
Signature · 1 line
2.52.0.223.gf5cc29aaa4-goog
Sean Christopherson
maintainerPATCH 1/9 KVM: x86: Drop ASSERT()s on APIC/vCPU being non-NULL
Remove ASSERT()s on vCPU and APIC structures being non-NULL in the local APIC code as the DEBUG=1 path of ASSERT() ends with BUG(), i.e. isn't meaningfully better for debugging than a NULL pointer dereference.
For all intents and purposes, no functional change intended.
--- arch/x86/kvm/lapic.c | 5 ----- 1 file changed, 5 deletions(-)
arch/x86/kvm/lapic.c +0 -5
@@ -1048,7 +1048,6 @@ bool kvm_apic_match_dest(struct kvm_vcpu *vcpu, struct kvm_lapic *source,struct kvm_lapic *target = vcpu->arch.apic;u32 mda =kvm_apic_mda (vcpu, dest, source, target); -ASSERT (target);switch (shorthand) {case APIC_DEST_NOSHORT :if (dest_mode ==APIC_DEST_PHYSICAL )
@@ -1607,8 +1606,6 @@ static u32 apic_get_tmcct(struct kvm_lapic *apic)ktime_t remaining, now;s64 ns; -ASSERT (apic !=NULL ); -/* if initial count is 0, current count should also be 0 */ if (kvm_lapic_get_reg (apic,APIC_TMICT ) ==0 || apic->lapic_timer.period ==0 )
@@ -3000,8 +2997,6 @@ int kvm_create_lapic(struct kvm_vcpu *vcpu) {struct kvm_lapic *apic; -ASSERT (vcpu !=NULL ); -if (!irqchip_in_kernel (vcpu->kvm)) {static_branch_inc (&kvm_has_noapic_vcpu);return 0 ;
Signature · 1 line
2.52.0.223.gf5cc29aaa4-goog
Sean Christopherson
maintainerPATCH 2/9 KVM: x86: Drop guest/user-triggerable asserts on IRR/ISR vectors
In reply to Sean ChristophersonRemove the ASSERT()s in apic_find_highest_i{r,s}r() that exist to detect illegal vectors (0-15 are reserved and never recognized by the local APIC), as the asserts, if they were ever to be enabled by #defining DEBUG, can be trivially triggered from both the guest and from userspace, and ultimately because the ASSERT()s are useless.
In large part due to lack of emulation for the Error Status Register and its "delayed" read semantics, KVM doesn't filter out bad IRQs (IPIs or otherwise) when IRQs are sent or received. Instead, probably by dumb luck on KVM's part, KVM effectively ignores pending illegal vectors in the IRR due vector 0-15 having priority '0', and thus never being higher priority than PPR.
As for ISR, a misbehaving userspace could stuff illegal vector bits, but again the end result is mostly benign (aside from userspace likely breaking the VM), as processing illegal vectors "works" and doesn't cause functional problems.
Regardless of the safety and correctness of KVM's illegal vector handling, one thing is for certain: the ASSERT()s have done absolutely nothing to help detect such issues since they were added 18+ years ago by commit 97222cc83163 ("KVM: Emulate local APIC in kernel").
For all intents and purposes, no functional change intended.
--- arch/x86/kvm/lapic.c | 14 ++------------ 1 file changed, 2 insertions(+), 12 deletions(-)
arch/x86/kvm/lapic.c +2 -12
@@ -666,8 +666,6 @@ static inline int apic_search_irr(struct kvm_lapic *apic)static inline int apic_find_highest_irr (struct kvm_lapic *apic) { -int result; -/* * Note that irr_pending is just a hint. It will be always * true with virtual interrupt delivery enabled.
@@ -675,10 +673,7 @@ static inline int apic_find_highest_irr(struct kvm_lapic *apic)if (!apic->irr_pending)return -1 ; - result =apic_search_irr (apic); -ASSERT (result ==-1 || result >=16 ); - -return result; +return apic_search_irr (apic); }static inline void apic_clear_irr (int vec,struct kvm_lapic *apic)
@@ -731,8 +726,6 @@ static inline void apic_set_isr(int vec, struct kvm_lapic *apic)static inline int apic_find_highest_isr (struct kvm_lapic *apic) { -int result; -/* * Note that isr_count is always 1, and highest_isr_cache * is always -1, with APIC virtualization enabled.
@@ -742,10 +735,7 @@ static inline int apic_find_highest_isr(struct kvm_lapic *apic)if (likely (apic->highest_isr_cache !=-1 ))return apic->highest_isr_cache; - result =apic_find_highest_vector (apic->regs +APIC_ISR ); -ASSERT (result ==-1 || result >=16 ); - -return result; +return apic_find_highest_vector (apic->regs +APIC_ISR ); }static inline void apic_clear_isr (int vec,struct kvm_lapic *apic)
Signature · 1 line
2.52.0.223.gf5cc29aaa4-goog
Sean Christopherson
maintainerPATCH 3/9 KVM: x86: Drop ASSERT() on I/O APIC EOIs being only for LEVEL_to WARN_ON_ONCE
In reply to Sean ChristophersonRemove kvm_ioapic_update_eoi_one()'s ASSERT() that the vector's entry is configured to be level-triggered, as KVM intercepts and forward EOIs to the I/O APIC even for edge-triggered IRQs (see kvm_ioapic_scan_entry()), and nothing guarantees the local APIC's TMR register is synchronized with the I/O APIC redirection table, i.e. the @trigger_mode check just out of sight doesn't provide any meaningful protection.
Given that roughly half of the historic ASSERT()s are/were guest- and/or user-triggerable, it's safe to assume no one has run meaningful workloads with DEBUG=1, i.e. that the ASSERT() has been dead code since it was added 18+ years ago.
Opportunistically drop the unnecessary forward declaration of kvm_ioapic_update_eoi_one().
For all intents and purposes, no functional change intended.
--- arch/x86/kvm/ioapic.c | 6 ------ 1 file changed, 6 deletions(-)
arch/x86/kvm/ioapic.c +0 -6
@@ -37,11 +37,6 @@static int ioapic_service (struct kvm_ioapic *vioapic,int irq,bool line_status); -static void kvm_ioapic_update_eoi_one (struct kvm_vcpu *vcpu, -struct kvm_ioapic *ioapic, -int trigger_mode, -int pin); -static unsigned long ioapic_read_indirect (struct kvm_ioapic *ioapic) {unsigned long result =0 ;
@@ -564,7 +559,6 @@ static void kvm_ioapic_update_eoi_one(struct kvm_vcpu *vcpu, kvm_lapic_get_reg(apic, APIC_SPIV) & APIC_SPIV_DIRECTED_EOI)return ; -ASSERT (ent->fields.trig_mode ==IOAPIC_LEVEL_TRIG ); ent->fields.remote_irr =0 ;if (!ent->fields.mask && (ioapic->irr & (1 << pin))) { ++ioapic->irq_eoi[pin];
Signature · 1 line
2.52.0.223.gf5cc29aaa4-goog
Sean Christopherson
maintainerPATCH 4/9 KVM: x86: Drop guest-triggerable ASSERT()s on I/O APIC access alignment
In reply to Sean ChristophersonDrop the asserts on the guest-controlled address being 16-byte aligned when emulating I/O APIC accesses, as the ASSERT()s are guest-triggerable and ultimately pointless since KVM requires exact register matches, i.e. will ultimately ignore unaligned accesses anyways.
Drop the ASSERT() definition itself now that all users are gone.
For all intents and purposes, no functional change intended.
--- arch/x86/kvm/ioapic.c | 4 ---- arch/x86/kvm/ioapic.h | 13 ------------- 2 files changed, 17 deletions(-)
arch/x86/kvm/ioapic.c +0 -4
@@ -618,8 +618,6 @@ static int ioapic_mmio_read(struct kvm_vcpu *vcpu, struct kvm_io_device *this,if (!ioapic_in_range (ioapic, addr))return -EOPNOTSUPP ; -ASSERT (!(addr &0xf ));/* check alignment */ - addr &=0xff ;spin_lock (&ioapic->lock);switch (addr) {
@@ -660,8 +658,6 @@ static int ioapic_mmio_write(struct kvm_vcpu *vcpu, struct kvm_io_device *this,if (!ioapic_in_range (ioapic, addr))return -EOPNOTSUPP ; -ASSERT (!(addr &0xf ));/* check alignment */ -switch (len) {case 8 :case 4 :
arch/x86/kvm/ioapic.h +0 -13
@@ -104,19 +104,6 @@ void kvm_unregister_irq_mask_notifier(struct kvm *kvm, int irq,void kvm_fire_mask_notifiers (struct kvm *kvm,unsigned irqchip,unsigned pin,bool mask); -#ifdef DEBUG -#define ASSERT (x) \ -do { \ - if (!(x)) { \ - printk(KERN_EMERG "assertion failed %s: %d: %s\n", \ - __FILE__, __LINE__, #x); \ - BUG(); \ - } \ -} while (0) -#else -#define ASSERT (x) do { } while (0) -#endif -static inline int ioapic_in_kernel (struct kvm *kvm) {return irqchip_full (kvm);
Signature · 1 line
2.52.0.223.gf5cc29aaa4-goog
Sean Christopherson
maintainerPATCH 5/9 KVM: x86: Drop MAX_NR_RESERVED_IOAPIC_PINS, use KVM_MAX_IRQ_ROUTES directly
In reply to Sean ChristophersonDirectly use KVM_MAX_IRQ_ROUTES when checking the number of routes being defined by userspace when creating a split IRQCHIP. The restriction has nothing to do with the I/O APIC, e.g. most modern userspace usage is for routing MSIs. Breaking the unnecessary dependency on the I/O APIC will allow burying all of ioapic.h behind CONFIG_KVM_IOAPIC=y.
No functional change intended.
--- arch/x86/kvm/ioapic.h | 1 - arch/x86/kvm/x86.c | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-)
arch/x86/kvm/ioapic.h +0 -1
@@ -10,7 +10,6 @@ struct kvm;struct kvm_vcpu ;#define IOAPIC_NUM_PINS KVM_IOAPIC_NUM_PINS -#define MAX_NR_RESERVED_IOAPIC_PINS KVM_MAX_IRQ_ROUTES#define IOAPIC_VERSION_ID 0x11/* IOAPIC version */ #define IOAPIC_EDGE_TRIG 0#define IOAPIC_LEVEL_TRIG 1
arch/x86/kvm/x86.c +1 -1
@@ -6717,7 +6717,7 @@ int kvm_vm_ioctl_enable_cap(struct kvm *kvm,case KVM_CAP_SPLIT_IRQCHIP : {mutex_lock (&kvm->lock); r = -EINVAL ; -if (cap->args[0 ] >MAX_NR_RESERVED_IOAPIC_PINS ) +if (cap->args[0 ] >KVM_MAX_IRQ_ROUTES ) goto split_irqchip_unlock; r = -EEXIST ;if (irqchip_in_kernel (kvm))
Signature · 1 line
2.52.0.223.gf5cc29aaa4-goog
Sean Christopherson
maintainerPATCH 6/9 KVM: x86: Add a wrapper to handle common case of IRQ delivery without dest_map
In reply to Sean ChristophersonTurn kvm_irq_delivery_to_apic() into a wrapper that passes NULL for the @dest_map param, as only the ugly I/O APIC RTC hackery needs to know which vCPUs received the IRQ.
No functional change intended.
--- arch/x86/kvm/hyperv.c | 2 +- arch/x86/kvm/ioapic.c | 6 +++--- arch/x86/kvm/irq.c | 4 ++-- arch/x86/kvm/lapic.c | 23 ++++++++++++++++------- arch/x86/kvm/lapic.h | 16 ++++++++++++---- arch/x86/kvm/x86.c | 2 +- arch/x86/kvm/xen.c | 2 +- 7 files changed, 36 insertions(+), 19 deletions(-)
arch/x86/kvm/hyperv.c +1 -1
@@ -492,7 +492,7 @@ static int synic_set_irq(struct kvm_vcpu_hv_synic *synic, u32 sint) irq.vector = vector; irq.level =1 ; - ret =kvm_irq_delivery_to_apic (vcpu->kvm, vcpu->arch.apic, &irq,NULL ); + ret =kvm_irq_delivery_to_apic (vcpu->kvm, vcpu->arch.apic, &irq);trace_kvm_hv_synic_set_irq (vcpu->vcpu_id, sint, irq.vector, ret);return ret; }
arch/x86/kvm/ioapic.c +3 -3
@@ -485,11 +485,11 @@ static int ioapic_service(struct kvm_ioapic *ioapic, int irq, bool line_status)* if rtc_irq_check_coalesced returns false). */ BUG_ON (ioapic->rtc_status.pending_eoi !=0 ); - ret =kvm_irq_delivery_to_apic (ioapic->kvm,NULL , &irqe, - &ioapic->rtc_status.dest_map); + ret =__kvm_irq_delivery_to_apic (ioapic->kvm,NULL , &irqe, + &ioapic->rtc_status.dest_map); ioapic->rtc_status.pending_eoi = (ret <0 ?0 : ret); }else - ret =kvm_irq_delivery_to_apic (ioapic->kvm,NULL , &irqe,NULL ); + ret =kvm_irq_delivery_to_apic (ioapic->kvm,NULL , &irqe);if (ret && irqe.trig_mode ==IOAPIC_LEVEL_TRIG ) entry->fields.remote_irr =1 ;
arch/x86/kvm/irq.c +2 -2
@@ -235,7 +235,7 @@ int kvm_set_msi(struct kvm_kernel_irq_routing_entry *e,kvm_msi_to_lapic_irq (kvm, e, &irq); -return kvm_irq_delivery_to_apic (kvm,NULL , &irq,NULL ); +return kvm_irq_delivery_to_apic (kvm,NULL , &irq); } int kvm_arch_set_irq_inatomic(struct kvm_kernel_irq_routing_entry *e,
@@ -258,7 +258,7 @@ int kvm_arch_set_irq_inatomic(struct kvm_kernel_irq_routing_entry *e,kvm_msi_to_lapic_irq (kvm, e, &irq); -if (kvm_irq_delivery_to_apic_fast (kvm,NULL , &irq, &r,NULL )) +if (kvm_irq_delivery_to_apic_fast (kvm,NULL , &irq, &r))return r;break ;
arch/x86/kvm/lapic.c +16 -7
@@ -1175,8 +1175,9 @@ static inline bool kvm_apic_map_get_dest_lapic(struct kvm *kvm,return true; } -bool kvm_irq_delivery_to_apic_fast (struct kvm *kvm,struct kvm_lapic *src, -struct kvm_lapic_irq *irq,int *r,struct dest_map *dest_map) +static bool __kvm_irq_delivery_to_apic_fast (struct kvm *kvm,struct kvm_lapic *src, +struct kvm_lapic_irq *irq,int *r, +struct dest_map *dest_map) {struct kvm_apic_map *map;unsigned long bitmap;
@@ -1212,6 +1213,13 @@ bool kvm_irq_delivery_to_apic_fast(struct kvm *kvm, struct kvm_lapic *src,return ret; } + +bool kvm_irq_delivery_to_apic_fast (struct kvm *kvm,struct kvm_lapic *src, +struct kvm_lapic_irq *irq,int *r) +{ +return __kvm_irq_delivery_to_apic_fast (kvm, src, irq, r,NULL ); +} +/* * This routine tries to handle interrupts in posted mode, here is how * it deals with different cases:
@@ -1283,15 +1291,16 @@ bool kvm_intr_is_single_vcpu(struct kvm *kvm, struct kvm_lapic_irq *irq, }EXPORT_SYMBOL_FOR_KVM_INTERNAL (kvm_intr_is_single_vcpu); -int kvm_irq_delivery_to_apic (struct kvm *kvm,struct kvm_lapic *src, -struct kvm_lapic_irq *irq,struct dest_map *dest_map) +int __kvm_irq_delivery_to_apic (struct kvm *kvm,struct kvm_lapic *src, +struct kvm_lapic_irq *irq, +struct dest_map *dest_map) {int r =-1 ;struct kvm_vcpu *vcpu, *lowest =NULL ;unsigned long i, dest_vcpu_bitmap[BITS_TO_LONGS (KVM_MAX_VCPUS )];unsigned int dest_vcpus =0 ; -if (kvm_irq_delivery_to_apic_fast (kvm, src, irq, &r, dest_map)) +if (__kvm_irq_delivery_to_apic_fast (kvm, src, irq, &r, dest_map))return r; if (irq->dest_mode == APIC_DEST_PHYSICAL &&
@@ -1587,7 +1596,7 @@ void kvm_apic_send_ipi(struct kvm_lapic *apic, u32 icr_low, u32 icr_high)trace_kvm_apic_ipi (icr_low, irq.dest_id); -kvm_irq_delivery_to_apic (apic->vcpu->kvm, apic, &irq,NULL ); +kvm_irq_delivery_to_apic (apic->vcpu->kvm, apic, &irq); }EXPORT_SYMBOL_FOR_KVM_INTERNAL (kvm_apic_send_ipi);
@@ -2556,7 +2565,7 @@ static int __kvm_x2apic_icr_write(struct kvm_lapic *apic, u64 data, bool fast)kvm_icr_to_lapic_irq (apic, (u32 )data, (u32)(data >>32 ), &irq);if (!kvm_irq_delivery_to_apic_fast (apic->vcpu->kvm, apic, &irq, - &ignored,NULL )) + &ignored))return -EWOULDBLOCK ;trace_kvm_apic_ipi ((u32 )data, irq.dest_id);
arch/x86/kvm/lapic.h +12 -4
@@ -117,10 +117,18 @@ int kvm_alloc_apic_access_page(struct kvm *kvm);void kvm_inhibit_apic_access_page (struct kvm_vcpu *vcpu);bool kvm_irq_delivery_to_apic_fast (struct kvm *kvm,struct kvm_lapic *src, -struct kvm_lapic_irq *irq,int *r,struct dest_map *dest_map); -int kvm_irq_delivery_to_apic (struct kvm *kvm,struct kvm_lapic *src, -struct kvm_lapic_irq *irq, -struct dest_map *dest_map); +struct kvm_lapic_irq *irq,int *r); +int __kvm_irq_delivery_to_apic (struct kvm *kvm,struct kvm_lapic *src, +struct kvm_lapic_irq *irq, +struct dest_map *dest_map); + +static inline int kvm_irq_delivery_to_apic (struct kvm *kvm, +struct kvm_lapic *src, +struct kvm_lapic_irq *irq) +{ +return __kvm_irq_delivery_to_apic (kvm, src, irq,NULL ); +} +void kvm_apic_send_ipi (struct kvm_lapic *apic,u32 icr_low,u32 icr_high);int kvm_apic_set_base (struct kvm_vcpu *vcpu,u64 value,bool host_initiated);
arch/x86/kvm/x86.c +1 -1
@@ -10245,7 +10245,7 @@ static void kvm_pv_kick_cpu_op(struct kvm *kvm, int apicid) .dest_id = apicid, }; -kvm_irq_delivery_to_apic (kvm,NULL , &lapic_irq,NULL ); +kvm_irq_delivery_to_apic (kvm,NULL , &lapic_irq); }bool kvm_apicv_activated (struct kvm *kvm)
arch/x86/kvm/xen.c +1 -1
@@ -626,7 +626,7 @@ void kvm_xen_inject_vcpu_vector(struct kvm_vcpu *v) irq.delivery_mode =APIC_DM_FIXED ; irq.level =1 ; -kvm_irq_delivery_to_apic (v->kvm,NULL , &irq,NULL ); +kvm_irq_delivery_to_apic (v->kvm,NULL , &irq); }/*
Signature · 1 line
2.52.0.223.gf5cc29aaa4-goog
Sean Christopherson
maintainerPATCH 7/9 KVM: x86: Fold "struct dest_map" into "struct rtc_status"
In reply to Sean ChristophersonDrop "struct dest_map" and fold its members into its one and only user, "struct rtc_status". Tracking "pending" EOIs and associated vCPUs is very much a hack for legacy RTC behavior, and should never be needed for other IRQ delivery. In addition to making it more obvious why KVM tracks target vCPUs, this will allow burying the "struct rtc_status" definition behind CONFIG_KVM_IOAPIC=y, which in turn will make it even harder for KVM to misuse the structure.
No functional change intended.
--- arch/x86/kvm/ioapic.c | 29 ++++++++++++++--------------- arch/x86/kvm/ioapic.h | 10 +++------- arch/x86/kvm/lapic.c | 28 ++++++++++++++-------------- arch/x86/kvm/lapic.h | 6 +++--- 4 files changed, 34 insertions(+), 39 deletions(-)
arch/x86/kvm/ioapic.c +14 -15
@@ -77,7 +77,7 @@ static unsigned long ioapic_read_indirect(struct kvm_ioapic *ioapic)static void rtc_irq_eoi_tracking_reset (struct kvm_ioapic *ioapic) { ioapic->rtc_status.pending_eoi =0 ; -bitmap_zero (ioapic->rtc_status.dest_map.map,KVM_MAX_VCPU_IDS ); +bitmap_zero (ioapic->rtc_status.map,KVM_MAX_VCPU_IDS ); }static void kvm_rtc_eoi_tracking_restore_all (struct kvm_ioapic *ioapic);
@@ -92,7 +92,7 @@ static void __rtc_irq_eoi_tracking_restore_one(struct kvm_vcpu *vcpu) {bool new_val, old_val;struct kvm_ioapic *ioapic = vcpu->kvm->arch.vioapic; -struct dest_map *dest_map = &ioapic->rtc_status.dest_map; +struct rtc_status *status = &ioapic->rtc_status;union kvm_ioapic_redirect_entry *e; e = &ioapic->redirtbl[RTC_GSI ];
@@ -102,17 +102,17 @@ static void __rtc_irq_eoi_tracking_restore_one(struct kvm_vcpu *vcpu)return ; new_val =kvm_apic_pending_eoi (vcpu, e->fields.vector); - old_val =test_bit (vcpu->vcpu_id, dest_map->map); + old_val =test_bit (vcpu->vcpu_id, status->map);if (new_val == old_val)return ;if (new_val) { -__set_bit (vcpu->vcpu_id, dest_map->map); - dest_map->vectors[vcpu->vcpu_id] = e->fields.vector; +__set_bit (vcpu->vcpu_id, status->map); + status->vectors[vcpu->vcpu_id] = e->fields.vector; ioapic->rtc_status.pending_eoi++; }else { -__clear_bit (vcpu->vcpu_id, dest_map->map); +__clear_bit (vcpu->vcpu_id, status->map); ioapic->rtc_status.pending_eoi--;rtc_status_pending_eoi_check_valid (ioapic); }
@@ -143,13 +143,12 @@ static void kvm_rtc_eoi_tracking_restore_all(struct kvm_ioapic *ioapic)static void rtc_irq_eoi (struct kvm_ioapic *ioapic,struct kvm_vcpu *vcpu,int vector) { -struct dest_map *dest_map = &ioapic->rtc_status.dest_map; +struct rtc_status *status = &ioapic->rtc_status;/* RTC special handling */ -if (test_bit (vcpu->vcpu_id, dest_map->map) && - (vector == dest_map->vectors[vcpu->vcpu_id]) && - (test_and_clear_bit (vcpu->vcpu_id, - ioapic->rtc_status.dest_map.map))) { +if (test_bit (vcpu->vcpu_id, status->map) && + (vector == status->vectors[vcpu->vcpu_id]) && + (test_and_clear_bit (vcpu->vcpu_id, status->map))) { --ioapic->rtc_status.pending_eoi;rtc_status_pending_eoi_check_valid (ioapic); }
@@ -260,15 +259,15 @@ static void kvm_ioapic_inject_all(struct kvm_ioapic *ioapic, unsigned long irr)void kvm_ioapic_scan_entry (struct kvm_vcpu *vcpu,ulong *ioapic_handled_vectors) {struct kvm_ioapic *ioapic = vcpu->kvm->arch.vioapic; -struct dest_map *dest_map = &ioapic->rtc_status.dest_map; +struct rtc_status *status = &ioapic->rtc_status;union kvm_ioapic_redirect_entry *e;int index;spin_lock (&ioapic->lock);/* Make sure we see any missing RTC EOI */ -if (test_bit (vcpu->vcpu_id, dest_map->map)) -__set_bit (dest_map->vectors[vcpu->vcpu_id], +if (test_bit (vcpu->vcpu_id, status->map)) +__set_bit (status->vectors[vcpu->vcpu_id], ioapic_handled_vectors);for (index =0 ; index <IOAPIC_NUM_PINS ; index++) {
@@ -486,7 +485,7 @@ static int ioapic_service(struct kvm_ioapic *ioapic, int irq, bool line_status)*/ BUG_ON (ioapic->rtc_status.pending_eoi !=0 ); ret =__kvm_irq_delivery_to_apic (ioapic->kvm,NULL , &irqe, - &ioapic->rtc_status.dest_map); + &ioapic->rtc_status); ioapic->rtc_status.pending_eoi = (ret <0 ?0 : ret); }else ret =kvm_irq_delivery_to_apic (ioapic->kvm,NULL , &irqe);
arch/x86/kvm/ioapic.h +3 -7
@@ -36,7 +36,9 @@ struct kvm_vcpu;#define RTC_GSI 8 -struct dest_map { +struct rtc_status { +int pending_eoi; +/* vcpu bitmap where IRQ has been sent */ DECLARE_BITMAP(map, KVM_MAX_VCPU_IDS);
@@ -47,12 +49,6 @@ struct dest_map {u8 vectors[KVM_MAX_VCPU_IDS ]; }; - -struct rtc_status { -int pending_eoi; -struct dest_map dest_map; -}; -union kvm_ioapic_redirect_entry {u64 bits;struct {
arch/x86/kvm/lapic.c +14 -14
@@ -784,15 +784,15 @@ EXPORT_SYMBOL_FOR_KVM_INTERNAL(kvm_lapic_find_highest_irr);static int __apic_accept_irq (struct kvm_lapic *apic,int delivery_mode,int vector,int level,int trig_mode, -struct dest_map *dest_map); +struct rtc_status *rtc_status);int kvm_apic_set_irq (struct kvm_vcpu *vcpu,struct kvm_lapic_irq *irq, -struct dest_map *dest_map) +struct rtc_status *rtc_status) {struct kvm_lapic *apic = vcpu->arch.apic;return __apic_accept_irq (apic, irq->delivery_mode, irq->vector, - irq->level, irq->trig_mode, dest_map); + irq->level, irq->trig_mode, rtc_status); } static int __pv_send_ipi(unsigned long *ipi_bitmap, struct kvm_apic_map *map,
@@ -1177,7 +1177,7 @@ static inline bool kvm_apic_map_get_dest_lapic(struct kvm *kvm,static bool __kvm_irq_delivery_to_apic_fast (struct kvm *kvm,struct kvm_lapic *src,struct kvm_lapic_irq *irq,int *r, -struct dest_map *dest_map) +struct rtc_status *rtc_status) {struct kvm_apic_map *map;unsigned long bitmap;
@@ -1192,7 +1192,7 @@ static bool __kvm_irq_delivery_to_apic_fast(struct kvm *kvm, struct kvm_lapic *s *r =0 ;return true; } - *r =kvm_apic_set_irq (src->vcpu, irq, dest_map); + *r =kvm_apic_set_irq (src->vcpu, irq, rtc_status);return true; }
@@ -1205,7 +1205,7 @@ static bool __kvm_irq_delivery_to_apic_fast(struct kvm *kvm, struct kvm_lapic *sfor_each_set_bit (i, &bitmap,16 ) {if (!dst[i])continue ; - *r +=kvm_apic_set_irq (dst[i]->vcpu, irq, dest_map); + *r +=kvm_apic_set_irq (dst[i]->vcpu, irq, rtc_status); } }
@@ -1293,14 +1293,14 @@ EXPORT_SYMBOL_FOR_KVM_INTERNAL(kvm_intr_is_single_vcpu);int __kvm_irq_delivery_to_apic (struct kvm *kvm,struct kvm_lapic *src,struct kvm_lapic_irq *irq, -struct dest_map *dest_map) +struct rtc_status *rtc_status) {int r =-1 ;struct kvm_vcpu *vcpu, *lowest =NULL ;unsigned long i, dest_vcpu_bitmap[BITS_TO_LONGS (KVM_MAX_VCPUS )];unsigned int dest_vcpus =0 ; -if (__kvm_irq_delivery_to_apic_fast (kvm, src, irq, &r, dest_map)) +if (__kvm_irq_delivery_to_apic_fast (kvm, src, irq, &r, rtc_status))return r; if (irq->dest_mode == APIC_DEST_PHYSICAL &&
@@ -1322,7 +1322,7 @@ int __kvm_irq_delivery_to_apic(struct kvm *kvm, struct kvm_lapic *src,if (!kvm_lowest_prio_delivery (irq)) {if (r <0 ) r =0 ; - r +=kvm_apic_set_irq (vcpu, irq, dest_map); + r +=kvm_apic_set_irq (vcpu, irq, rtc_status); }else if (kvm_apic_sw_enabled (vcpu->arch.apic)) {if (!vector_hashing_enabled) {if (!lowest)
@@ -1344,7 +1344,7 @@ int __kvm_irq_delivery_to_apic(struct kvm *kvm, struct kvm_lapic *src, }if (lowest) - r =kvm_apic_set_irq (lowest, irq, dest_map); + r =kvm_apic_set_irq (lowest, irq, rtc_status);return r; }
@@ -1355,7 +1355,7 @@ int __kvm_irq_delivery_to_apic(struct kvm *kvm, struct kvm_lapic *src,*/ static int __apic_accept_irq (struct kvm_lapic *apic,int delivery_mode,int vector,int level,int trig_mode, -struct dest_map *dest_map) +struct rtc_status *rtc_status) {int result =0 ;struct kvm_vcpu *vcpu = apic->vcpu;
@@ -1376,9 +1376,9 @@ static int __apic_accept_irq(struct kvm_lapic *apic, int delivery_mode, result =1 ; -if (dest_map) { -__set_bit (vcpu->vcpu_id, dest_map->map); - dest_map->vectors[vcpu->vcpu_id] = vector; +if (rtc_status) { +__set_bit (vcpu->vcpu_id, rtc_status->map); + rtc_status->vectors[vcpu->vcpu_id] = vector; }if (apic_test_vector (vector, apic->regs +APIC_TMR ) != !!trig_mode) {
arch/x86/kvm/lapic.h +3 -3
@@ -88,7 +88,7 @@ struct kvm_lapic {int nr_lvt_entries; }; -struct dest_map ; +struct rtc_status ;int kvm_create_lapic (struct kvm_vcpu *vcpu);void kvm_free_lapic (struct kvm_vcpu *vcpu);
@@ -110,7 +110,7 @@ bool __kvm_apic_update_irr(unsigned long *pir, void *regs, int *max_irr);bool kvm_apic_update_irr (struct kvm_vcpu *vcpu,unsigned long *pir,int *max_irr);void kvm_apic_update_ppr (struct kvm_vcpu *vcpu);int kvm_apic_set_irq (struct kvm_vcpu *vcpu,struct kvm_lapic_irq *irq, -struct dest_map *dest_map); +struct rtc_status *rtc_status);int kvm_apic_local_deliver (struct kvm_lapic *apic,int lvt_type);void kvm_apic_update_apicv (struct kvm_vcpu *vcpu);int kvm_alloc_apic_access_page (struct kvm *kvm);
@@ -120,7 +120,7 @@ bool kvm_irq_delivery_to_apic_fast(struct kvm *kvm, struct kvm_lapic *src, struct kvm_lapic_irq *irq, int *r);int __kvm_irq_delivery_to_apic (struct kvm *kvm,struct kvm_lapic *src,struct kvm_lapic_irq *irq, -struct dest_map *dest_map); +struct rtc_status *rtc_status); static inline int kvm_irq_delivery_to_apic(struct kvm *kvm, struct kvm_lapic *src,
Signature · 1 line
2.52.0.223.gf5cc29aaa4-goog
Sean Christopherson
maintainerPATCH 8/9 KVM: x86: Bury ioapic.h definitions behind CONFIG_KVM_IOAPIC
In reply to Sean ChristophersonNow that almost everything in ioapic.h is used only by code guarded by CONFIG_KVM_IOAPIC=y, bury (almost) the entire thing behind the Kconfig.
--- arch/x86/kvm/ioapic.h | 14 +++++++++----- arch/x86/kvm/lapic.c | 2 ++ 2 files changed, 11 insertions(+), 5 deletions(-)
arch/x86/kvm/ioapic.h +9 -5
@@ -6,6 +6,8 @@#include <kvm/iodev.h> #include "irq.h" +#ifdef CONFIG_KVM_IOAPIC +struct kvm ;struct kvm_vcpu ;
@@ -99,11 +101,6 @@ void kvm_unregister_irq_mask_notifier(struct kvm *kvm, int irq,void kvm_fire_mask_notifiers (struct kvm *kvm,unsigned irqchip,unsigned pin,bool mask); -static inline int ioapic_in_kernel (struct kvm *kvm) -{ -return irqchip_full (kvm); -} -void kvm_rtc_eoi_tracking_restore_one (struct kvm_vcpu *vcpu);void kvm_ioapic_update_eoi (struct kvm_vcpu *vcpu,int vector,int trigger_mode);
@@ -116,6 +113,13 @@ void kvm_get_ioapic(struct kvm *kvm, struct kvm_ioapic_state *state);void kvm_set_ioapic (struct kvm *kvm,struct kvm_ioapic_state *state);void kvm_ioapic_scan_entry (struct kvm_vcpu *vcpu,ulong *ioapic_handled_vectors); +#endif /* CONFIG_KVM_IOAPIC */ + +static inline int ioapic_in_kernel (struct kvm *kvm) +{ +return irqchip_full (kvm); +} +void kvm_scan_ioapic_routes (struct kvm_vcpu *vcpu,ulong *ioapic_handled_vectors); void kvm_scan_ioapic_irq(struct kvm_vcpu *vcpu, u32 dest_id, u16 dest_mode,
arch/x86/kvm/lapic.c +2 -0
@@ -1376,10 +1376,12 @@ static int __apic_accept_irq(struct kvm_lapic *apic, int delivery_mode, result =1 ; +#ifdef CONFIG_KVM_IOAPIC if (rtc_status) {__set_bit (vcpu->vcpu_id, rtc_status->map); rtc_status->vectors[vcpu->vcpu_id] = vector; } +#endif if (apic_test_vector (vector, apic->regs +APIC_TMR ) != !!trig_mode) {if (trig_mode)
Signature · 1 line
2.52.0.223.gf5cc29aaa4-goog
Sean Christopherson
maintainerPATCH 9/9 KVM: x86: Hide KVM_IRQCHIP_KERNEL behind CONFIG_KVM_IOAPIC=y
In reply to Sean ChristophersonEnumerate KVM_IRQCHIP_KERNEL if and only if support for an in-kernel I/O APIC is enabled, as all usage is likewise guarded by CONFIG_KVM_IOAPIC=y.
--- arch/x86/include/asm/kvm_host.h | 2 ++ 1 file changed, 2 insertions(+)
arch/x86/include/asm/kvm_host.h +2 -0
@@ -1222,7 +1222,9 @@ struct kvm_xen {enum kvm_irqchip_mode {KVM_IRQCHIP_NONE , +#ifdef CONFIG_KVM_IOAPIC KVM_IRQCHIP_KERNEL ,/* created with KVM_CREATE_IRQCHIP */ +#endif KVM_IRQCHIP_SPLIT ,/* created with KVM_CAP_SPLIT_IRQCHIP */ };
Signature · 1 line
2.52.0.223.gf5cc29aaa4-goog
Quoted 10 lines · Sean Christopherson
Drop a bunch of _really_ old dead code (
ASSERT()buried behind a DEBUG macro that probably hasn't been enabled in 15+ years), clean up the bizarre and confusing "dest_map" pointer that gets passed all of the place but is only actually used for in-kernel RTC emulation, and the bury almost all ofioapic.hbehindCONFIG_KVM_IOAPIC=y.I'm not entirely sure why I started poking at this. I think I got mad at the
dest_mapcode, and then things snowballed...[...]
Applied to kvm-x86 apic, thanks!
[1/9] KVM: x86: Drop ASSERT()s on APIC/vCPU being non-NULL
https://github.com/kvm-x86/linux/commit/a4978324e4bd [2/9] KVM: x86: Drop guest/user-triggerable asserts on IRR/ISR vectors https://github.com/kvm-x86/linux/commit/37187992dd82 [3/9] KVM: x86: Drop ASSERT() on I/O APIC EOIs being only for LEVEL_to WARN_ON_ONCE https://github.com/kvm-x86/linux/commit/ca909f9ea8cb [4/9] KVM: x86: Drop guest-triggerable ASSERT()s on I/O APIC access alignment https://github.com/kvm-x86/linux/commit/9eabb2a5e499 [5/9] KVM: x86: Drop MAX_NR_RESERVED_IOAPIC_PINS, use KVM_MAX_IRQ_ROUTES directly https://github.com/kvm-x86/linux/commit/4d846f183897 [6/9] KVM: x86: Add a wrapper to handle common case of IRQ delivery without dest_map https://github.com/kvm-x86/linux/commit/1a5d7f9540af [7/9] KVM: x86: Fold "struct dest_map" into "struct rtc_status" https://github.com/kvm-x86/linux/commit/5cd6b1a6eebd [8/9] KVM: x86: Bury ioapic.h definitions behind CONFIG_KVM_IOAPIC https://github.com/kvm-x86/linux/commit/59c3e0603d86 [9/9] KVM: x86: Hide KVM_IRQCHIP_KERNEL behind CONFIG_KVM_IOAPIC=y https://github.com/kvm-x86/linux/commit/fd09d259c161