Merge tag 'for-linus' of git://git.kernel.org/pub/scm/virt/kvm/kvm

Pll KVM fixes from Radim Krčmář:
"ARM:
- Fix proxying of GICv2 CPU interface accesses
- Fix crash when switching to BE
- Track source vcpu git GICv2 SGIs
- Fix an outdated bit of documentation

x86:
- Speed up injection of expired timers (for stable)"

* tag 'for-linus' of git://git.kernel.org/pub/scm/virt/kvm/kvm:
KVM: x86: remove APIC Timer periodic/oneshot spikes
arm64: vgic-v2: Fix proxying of cpuif access
KVM: arm/arm64: vgic_init: Cleanup reference to process_maintenance
KVM: arm64: Fix order of vcpu_write_sys_reg() arguments
KVM: arm/arm64: vgic: Fix source vcpu issues for GICv2 SGI

Changed files
+122 -85
arch
arm64
include
kvm
x86
kvm
include
virt
+1 -1
arch/arm64/include/asm/kvm_emulate.h
··· 333 333 } else { 334 334 u64 sctlr = vcpu_read_sys_reg(vcpu, SCTLR_EL1); 335 335 sctlr |= (1 << 25); 336 - vcpu_write_sys_reg(vcpu, SCTLR_EL1, sctlr); 336 + vcpu_write_sys_reg(vcpu, sctlr, SCTLR_EL1); 337 337 } 338 338 } 339 339
+19 -5
arch/arm64/kvm/hyp/vgic-v2-cpuif-proxy.c
··· 18 18 #include <linux/compiler.h> 19 19 #include <linux/irqchip/arm-gic.h> 20 20 #include <linux/kvm_host.h> 21 + #include <linux/swab.h> 21 22 22 23 #include <asm/kvm_emulate.h> 23 24 #include <asm/kvm_hyp.h> 24 25 #include <asm/kvm_mmu.h> 26 + 27 + static bool __hyp_text __is_be(struct kvm_vcpu *vcpu) 28 + { 29 + if (vcpu_mode_is_32bit(vcpu)) 30 + return !!(read_sysreg_el2(spsr) & COMPAT_PSR_E_BIT); 31 + 32 + return !!(read_sysreg(SCTLR_EL1) & SCTLR_ELx_EE); 33 + } 25 34 26 35 /* 27 36 * __vgic_v2_perform_cpuif_access -- perform a GICV access on behalf of the ··· 73 64 addr += fault_ipa - vgic->vgic_cpu_base; 74 65 75 66 if (kvm_vcpu_dabt_iswrite(vcpu)) { 76 - u32 data = vcpu_data_guest_to_host(vcpu, 77 - vcpu_get_reg(vcpu, rd), 78 - sizeof(u32)); 67 + u32 data = vcpu_get_reg(vcpu, rd); 68 + if (__is_be(vcpu)) { 69 + /* guest pre-swabbed data, undo this for writel() */ 70 + data = swab32(data); 71 + } 79 72 writel_relaxed(data, addr); 80 73 } else { 81 74 u32 data = readl_relaxed(addr); 82 - vcpu_set_reg(vcpu, rd, vcpu_data_host_to_guest(vcpu, data, 83 - sizeof(u32))); 75 + if (__is_be(vcpu)) { 76 + /* guest expects swabbed data */ 77 + data = swab32(data); 78 + } 79 + vcpu_set_reg(vcpu, rd, data); 84 80 } 85 81 86 82 return 1;
+20 -17
arch/x86/kvm/lapic.c
··· 1463 1463 local_irq_restore(flags); 1464 1464 } 1465 1465 1466 - static void start_sw_period(struct kvm_lapic *apic) 1467 - { 1468 - if (!apic->lapic_timer.period) 1469 - return; 1470 - 1471 - if (apic_lvtt_oneshot(apic) && 1472 - ktime_after(ktime_get(), 1473 - apic->lapic_timer.target_expiration)) { 1474 - apic_timer_expired(apic); 1475 - return; 1476 - } 1477 - 1478 - hrtimer_start(&apic->lapic_timer.timer, 1479 - apic->lapic_timer.target_expiration, 1480 - HRTIMER_MODE_ABS_PINNED); 1481 - } 1482 - 1483 1466 static void update_target_expiration(struct kvm_lapic *apic, uint32_t old_divisor) 1484 1467 { 1485 1468 ktime_t now, remaining; ··· 1527 1544 apic->lapic_timer.target_expiration = 1528 1545 ktime_add_ns(apic->lapic_timer.target_expiration, 1529 1546 apic->lapic_timer.period); 1547 + } 1548 + 1549 + static void start_sw_period(struct kvm_lapic *apic) 1550 + { 1551 + if (!apic->lapic_timer.period) 1552 + return; 1553 + 1554 + if (ktime_after(ktime_get(), 1555 + apic->lapic_timer.target_expiration)) { 1556 + apic_timer_expired(apic); 1557 + 1558 + if (apic_lvtt_oneshot(apic)) 1559 + return; 1560 + 1561 + advance_periodic_target_expiration(apic); 1562 + } 1563 + 1564 + hrtimer_start(&apic->lapic_timer.timer, 1565 + apic->lapic_timer.target_expiration, 1566 + HRTIMER_MODE_ABS_PINNED); 1530 1567 } 1531 1568 1532 1569 bool kvm_lapic_hv_timer_in_use(struct kvm_vcpu *vcpu)
+1
include/kvm/arm_vgic.h
··· 131 131 u32 mpidr; /* GICv3 target VCPU */ 132 132 }; 133 133 u8 source; /* GICv2 SGIs only */ 134 + u8 active_source; /* GICv2 SGIs only */ 134 135 u8 priority; 135 136 enum vgic_irq_config config; /* Level or edge */ 136 137
+1 -1
virt/kvm/arm/vgic/vgic-init.c
··· 423 423 * We cannot rely on the vgic maintenance interrupt to be 424 424 * delivered synchronously. This means we can only use it to 425 425 * exit the VM, and we perform the handling of EOIed 426 - * interrupts on the exit path (see vgic_process_maintenance). 426 + * interrupts on the exit path (see vgic_fold_lr_state). 427 427 */ 428 428 return IRQ_HANDLED; 429 429 }
+8 -2
virt/kvm/arm/vgic/vgic-mmio.c
··· 289 289 irq->vcpu->cpu != -1) /* VCPU thread is running */ 290 290 cond_resched_lock(&irq->irq_lock); 291 291 292 - if (irq->hw) 292 + if (irq->hw) { 293 293 vgic_hw_irq_change_active(vcpu, irq, active, !requester_vcpu); 294 - else 294 + } else { 295 + u32 model = vcpu->kvm->arch.vgic.vgic_model; 296 + 295 297 irq->active = active; 298 + if (model == KVM_DEV_TYPE_ARM_VGIC_V2 && 299 + active && vgic_irq_is_sgi(irq->intid)) 300 + irq->active_source = requester_vcpu->vcpu_id; 301 + } 296 302 297 303 if (irq->active) 298 304 vgic_queue_irq_unlock(vcpu->kvm, irq, flags);
+22 -16
virt/kvm/arm/vgic/vgic-v2.c
··· 37 37 vgic_v2_write_lr(i, 0); 38 38 } 39 39 40 - void vgic_v2_set_npie(struct kvm_vcpu *vcpu) 41 - { 42 - struct vgic_v2_cpu_if *cpuif = &vcpu->arch.vgic_cpu.vgic_v2; 43 - 44 - cpuif->vgic_hcr |= GICH_HCR_NPIE; 45 - } 46 - 47 40 void vgic_v2_set_underflow(struct kvm_vcpu *vcpu) 48 41 { 49 42 struct vgic_v2_cpu_if *cpuif = &vcpu->arch.vgic_cpu.vgic_v2; ··· 64 71 int lr; 65 72 unsigned long flags; 66 73 67 - cpuif->vgic_hcr &= ~(GICH_HCR_UIE | GICH_HCR_NPIE); 74 + cpuif->vgic_hcr &= ~GICH_HCR_UIE; 68 75 69 76 for (lr = 0; lr < vgic_cpu->used_lrs; lr++) { 70 77 u32 val = cpuif->vgic_lr[lr]; 71 - u32 intid = val & GICH_LR_VIRTUALID; 78 + u32 cpuid, intid = val & GICH_LR_VIRTUALID; 72 79 struct vgic_irq *irq; 80 + 81 + /* Extract the source vCPU id from the LR */ 82 + cpuid = val & GICH_LR_PHYSID_CPUID; 83 + cpuid >>= GICH_LR_PHYSID_CPUID_SHIFT; 84 + cpuid &= 7; 73 85 74 86 /* Notify fds when the guest EOI'ed a level-triggered SPI */ 75 87 if (lr_signals_eoi_mi(val) && vgic_valid_spi(vcpu->kvm, intid)) ··· 88 90 /* Always preserve the active bit */ 89 91 irq->active = !!(val & GICH_LR_ACTIVE_BIT); 90 92 93 + if (irq->active && vgic_irq_is_sgi(intid)) 94 + irq->active_source = cpuid; 95 + 91 96 /* Edge is the only case where we preserve the pending bit */ 92 97 if (irq->config == VGIC_CONFIG_EDGE && 93 98 (val & GICH_LR_PENDING_BIT)) { 94 99 irq->pending_latch = true; 95 100 96 - if (vgic_irq_is_sgi(intid)) { 97 - u32 cpuid = val & GICH_LR_PHYSID_CPUID; 98 - 99 - cpuid >>= GICH_LR_PHYSID_CPUID_SHIFT; 101 + if (vgic_irq_is_sgi(intid)) 100 102 irq->source |= (1 << cpuid); 101 - } 102 103 } 103 104 104 105 /* ··· 149 152 u32 val = irq->intid; 150 153 bool allow_pending = true; 151 154 152 - if (irq->active) 155 + if (irq->active) { 153 156 val |= GICH_LR_ACTIVE_BIT; 157 + if (vgic_irq_is_sgi(irq->intid)) 158 + val |= irq->active_source << GICH_LR_PHYSID_CPUID_SHIFT; 159 + if (vgic_irq_is_multi_sgi(irq)) { 160 + allow_pending = false; 161 + val |= GICH_LR_EOI; 162 + } 163 + } 154 164 155 165 if (irq->hw) { 156 166 val |= GICH_LR_HW; ··· 194 190 BUG_ON(!src); 195 191 val |= (src - 1) << GICH_LR_PHYSID_CPUID_SHIFT; 196 192 irq->source &= ~(1 << (src - 1)); 197 - if (irq->source) 193 + if (irq->source) { 198 194 irq->pending_latch = true; 195 + val |= GICH_LR_EOI; 196 + } 199 197 } 200 198 } 201 199
+29 -20
virt/kvm/arm/vgic/vgic-v3.c
··· 27 27 static bool common_trap; 28 28 static bool gicv4_enable; 29 29 30 - void vgic_v3_set_npie(struct kvm_vcpu *vcpu) 31 - { 32 - struct vgic_v3_cpu_if *cpuif = &vcpu->arch.vgic_cpu.vgic_v3; 33 - 34 - cpuif->vgic_hcr |= ICH_HCR_NPIE; 35 - } 36 - 37 30 void vgic_v3_set_underflow(struct kvm_vcpu *vcpu) 38 31 { 39 32 struct vgic_v3_cpu_if *cpuif = &vcpu->arch.vgic_cpu.vgic_v3; ··· 48 55 int lr; 49 56 unsigned long flags; 50 57 51 - cpuif->vgic_hcr &= ~(ICH_HCR_UIE | ICH_HCR_NPIE); 58 + cpuif->vgic_hcr &= ~ICH_HCR_UIE; 52 59 53 60 for (lr = 0; lr < vgic_cpu->used_lrs; lr++) { 54 61 u64 val = cpuif->vgic_lr[lr]; 55 - u32 intid; 62 + u32 intid, cpuid; 56 63 struct vgic_irq *irq; 64 + bool is_v2_sgi = false; 57 65 58 - if (model == KVM_DEV_TYPE_ARM_VGIC_V3) 66 + cpuid = val & GICH_LR_PHYSID_CPUID; 67 + cpuid >>= GICH_LR_PHYSID_CPUID_SHIFT; 68 + 69 + if (model == KVM_DEV_TYPE_ARM_VGIC_V3) { 59 70 intid = val & ICH_LR_VIRTUAL_ID_MASK; 60 - else 71 + } else { 61 72 intid = val & GICH_LR_VIRTUALID; 73 + is_v2_sgi = vgic_irq_is_sgi(intid); 74 + } 62 75 63 76 /* Notify fds when the guest EOI'ed a level-triggered IRQ */ 64 77 if (lr_signals_eoi_mi(val) && vgic_valid_spi(vcpu->kvm, intid)) ··· 80 81 /* Always preserve the active bit */ 81 82 irq->active = !!(val & ICH_LR_ACTIVE_BIT); 82 83 84 + if (irq->active && is_v2_sgi) 85 + irq->active_source = cpuid; 86 + 83 87 /* Edge is the only case where we preserve the pending bit */ 84 88 if (irq->config == VGIC_CONFIG_EDGE && 85 89 (val & ICH_LR_PENDING_BIT)) { 86 90 irq->pending_latch = true; 87 91 88 - if (vgic_irq_is_sgi(intid) && 89 - model == KVM_DEV_TYPE_ARM_VGIC_V2) { 90 - u32 cpuid = val & GICH_LR_PHYSID_CPUID; 91 - 92 - cpuid >>= GICH_LR_PHYSID_CPUID_SHIFT; 92 + if (is_v2_sgi) 93 93 irq->source |= (1 << cpuid); 94 - } 95 94 } 96 95 97 96 /* ··· 130 133 { 131 134 u32 model = vcpu->kvm->arch.vgic.vgic_model; 132 135 u64 val = irq->intid; 133 - bool allow_pending = true; 136 + bool allow_pending = true, is_v2_sgi; 134 137 135 - if (irq->active) 138 + is_v2_sgi = (vgic_irq_is_sgi(irq->intid) && 139 + model == KVM_DEV_TYPE_ARM_VGIC_V2); 140 + 141 + if (irq->active) { 136 142 val |= ICH_LR_ACTIVE_BIT; 143 + if (is_v2_sgi) 144 + val |= irq->active_source << GICH_LR_PHYSID_CPUID_SHIFT; 145 + if (vgic_irq_is_multi_sgi(irq)) { 146 + allow_pending = false; 147 + val |= ICH_LR_EOI; 148 + } 149 + } 137 150 138 151 if (irq->hw) { 139 152 val |= ICH_LR_HW; ··· 181 174 BUG_ON(!src); 182 175 val |= (src - 1) << GICH_LR_PHYSID_CPUID_SHIFT; 183 176 irq->source &= ~(1 << (src - 1)); 184 - if (irq->source) 177 + if (irq->source) { 185 178 irq->pending_latch = true; 179 + val |= ICH_LR_EOI; 180 + } 186 181 } 187 182 } 188 183
+7 -23
virt/kvm/arm/vgic/vgic.c
··· 725 725 vgic_v3_set_underflow(vcpu); 726 726 } 727 727 728 - static inline void vgic_set_npie(struct kvm_vcpu *vcpu) 729 - { 730 - if (kvm_vgic_global_state.type == VGIC_V2) 731 - vgic_v2_set_npie(vcpu); 732 - else 733 - vgic_v3_set_npie(vcpu); 734 - } 735 - 736 728 /* Requires the ap_list_lock to be held. */ 737 729 static int compute_ap_list_depth(struct kvm_vcpu *vcpu, 738 730 bool *multi_sgi) ··· 738 746 DEBUG_SPINLOCK_BUG_ON(!spin_is_locked(&vgic_cpu->ap_list_lock)); 739 747 740 748 list_for_each_entry(irq, &vgic_cpu->ap_list_head, ap_list) { 749 + int w; 750 + 741 751 spin_lock(&irq->irq_lock); 742 752 /* GICv2 SGIs can count for more than one... */ 743 - if (vgic_irq_is_sgi(irq->intid) && irq->source) { 744 - int w = hweight8(irq->source); 745 - 746 - count += w; 747 - *multi_sgi |= (w > 1); 748 - } else { 749 - count++; 750 - } 753 + w = vgic_irq_get_lr_count(irq); 751 754 spin_unlock(&irq->irq_lock); 755 + 756 + count += w; 757 + *multi_sgi |= (w > 1); 752 758 } 753 759 return count; 754 760 } ··· 757 767 struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu; 758 768 struct vgic_irq *irq; 759 769 int count; 760 - bool npie = false; 761 770 bool multi_sgi; 762 771 u8 prio = 0xff; 763 772 ··· 786 797 if (likely(vgic_target_oracle(irq) == vcpu)) { 787 798 vgic_populate_lr(vcpu, irq, count++); 788 799 789 - if (irq->source) { 790 - npie = true; 800 + if (irq->source) 791 801 prio = irq->priority; 792 - } 793 802 } 794 803 795 804 spin_unlock(&irq->irq_lock); ··· 799 812 break; 800 813 } 801 814 } 802 - 803 - if (npie) 804 - vgic_set_npie(vcpu); 805 815 806 816 vcpu->arch.vgic_cpu.used_lrs = count; 807 817
+14
virt/kvm/arm/vgic/vgic.h
··· 110 110 return irq->config == VGIC_CONFIG_LEVEL && irq->hw; 111 111 } 112 112 113 + static inline int vgic_irq_get_lr_count(struct vgic_irq *irq) 114 + { 115 + /* Account for the active state as an interrupt */ 116 + if (vgic_irq_is_sgi(irq->intid) && irq->source) 117 + return hweight8(irq->source) + irq->active; 118 + 119 + return irq_is_pending(irq) || irq->active; 120 + } 121 + 122 + static inline bool vgic_irq_is_multi_sgi(struct vgic_irq *irq) 123 + { 124 + return vgic_irq_get_lr_count(irq) > 1; 125 + } 126 + 113 127 /* 114 128 * This struct provides an intermediate representation of the fields contained 115 129 * in the GICH_VMCR and ICH_VMCR registers, such that code exporting the GIC