History

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

Pull KVM fixes from Paolo Bonzini: "ARM: - fixes for ITS init issues, error handling, IRQ leakage, race conditions - an erratum workaround for timers - some removal of misleading use of errors and comments - a fix for GICv3 on 32-bit guests MIPS: - fix for where the guest could wrongly map the first page of physical memory x86: - nested virtualization fixes" * tag 'for-linus' of git://git.kernel.org/pub/scm/virt/kvm/kvm: MIPS: KVM: Check for pfn noslot case kvm: nVMX: fix nested tsc scaling KVM: nVMX: postpone VMCS changes on MSR_IA32_APICBASE write KVM: nVMX: fix msr bitmaps to prevent L2 from accessing L0 x2APIC arm64: KVM: report configured SRE value to 32-bit world arm64: KVM: remove misleading comment on pmu status KVM: arm/arm64: timer: Workaround misconfigured timer interrupt arm64: Document workaround for Cortex-A72 erratum #853709 KVM: arm/arm64: Change misleading use of is_error_pfn KVM: arm64: ITS: avoid re-mapping LPIs KVM: arm64: check for ITS device on MSI injection KVM: arm64: ITS: move ITS registration into first VCPU run KVM: arm64: vgic-its: Make updates to propbaser/pendbaser atomic KVM: arm64: vgic-its: Plug race in vgic_put_irq KVM: arm64: vgic-its: Handle errors from vgic_add_lpi KVM: arm64: ITS: return 1 on successful MSI injection

Linus Torvalds committed 10 years ago

Commit af56ff22 parents 5e608a0 ba913e413 files · +234 −139Browse files

Unified · Split

13 files
  1. Documentation/arm64/silicon-errata.txt +1 −0
  2. arch/arm/kvm/mmu.c +1 −1
  3. arch/arm64/kvm/hyp/switch.c +1 −1
  4. arch/arm64/kvm/sys_regs.c +1 −9
  5. arch/mips/kvm/mmu.c +1 −1
  6. arch/x86/kvm/vmx.c +69 −67
  7. include/linux/irqchip/arm-gic-v3.h +1 −0
  8. virt/kvm/arm/arch_timer.c +10 −1
  9. virt/kvm/arm/vgic/vgic-its.c +114 −44
  10. virt/kvm/arm/vgic/vgic-mmio-v3.c +16 −10
  11. virt/kvm/arm/vgic/vgic-v3.c +8 −0
  12. virt/kvm/arm/vgic/vgic.c +5 −5
  13. virt/kvm/arm/vgic/vgic.h +6 −0
Documentation/arm64/silicon-errata.txt+1−0
@@ -53,6 +53,7 @@ stable kernels. · 52 unmodified lines
5353 | ARM | Cortex-A57 | #832075 | ARM64_ERRATUM_832075 |
5454 | ARM | Cortex-A57 | #852523 | N/A |
5555 | ARM | Cortex-A57 | #834220 | ARM64_ERRATUM_834220 |
56+| ARM | Cortex-A72 | #853709 | N/A |
5657 | ARM | MMU-500 | #841119,#826419 | N/A |
5758 | | | | |
5859 | Cavium | ThunderX ITS | #22375, #24313 | CAVIUM_ERRATUM_22375 |
arch/arm/kvm/mmu.c+1−1
@@ -1309,7 +1309,7 @@ static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa, · 1308 unmodified lines
13091309 smp_rmb();
13101310
13111311 pfn = gfn_to_pfn_prot(kvm, gfn, write_fault, &writable);
1312− if (is_error_pfn(pfn))
1312+ if (is_error_noslot_pfn(pfn))
13131313 return -EFAULT;
13141314
13151315 if (kvm_is_device_pfn(pfn)) {
arch/arm64/kvm/hyp/switch.c+1−1
@@ -256,7 +256,7 @@ static int __hyp_text __guest_run(struct kvm_vcpu *vcpu) · 255 unmodified lines
256256
257257 /*
258258 * We must restore the 32-bit state before the sysregs, thanks
259− * to Cortex-A57 erratum #852523.
259+ * to erratum #852523 (Cortex-A57) or #853709 (Cortex-A72).
260260 */
261261 __sysreg32_restore_state(vcpu);
262262 __sysreg_restore_guest_state(guest_ctxt);
arch/arm64/kvm/sys_regs.c+1−9
@@ -823,14 +823,6 @@ static bool access_pmuserenr(struct kvm_vcpu *vcpu, struct sys_reg_params *p, · 822 unmodified lines
823823 * Architected system registers.
824824 * Important: Must be sorted ascending by Op0, Op1, CRn, CRm, Op2
825825 *
826− * We could trap ID_DFR0 and tell the guest we don't support performance
827− * monitoring. Unfortunately the patch to make the kernel check ID_DFR0 was
828− * NAKed, so it will read the PMCR anyway.
829− *
830− * Therefore we tell the guest we have 0 counters. Unfortunately, we
831− * must always support PMCCNTR (the cycle counter): we just RAZ/WI for
832− * all PM registers, which doesn't crash the guest kernel at least.
833− *
834826 * Debug handling: We do trap most, if not all debug related system
835827 * registers. The implementation is good enough to ensure that a guest
836828 * can use these with minimal performance degradation. The drawback is
@@ -1360,7 +1352,7 @@ static const struct sys_reg_desc cp15_regs[] = { · 523 unmodified lines
13601352 { Op1( 0), CRn(10), CRm( 3), Op2( 1), access_vm_reg, NULL, c10_AMAIR1 },
13611353
13621354 /* ICC_SRE */
1363− { Op1( 0), CRn(12), CRm(12), Op2( 5), trap_raz_wi },
1355+ { Op1( 0), CRn(12), CRm(12), Op2( 5), access_gic_sre },
13641356
13651357 { Op1( 0), CRn(13), CRm( 0), Op2( 1), access_vm_reg, NULL, c13_CID },
13661358
arch/mips/kvm/mmu.c+1−1
@@ -40,7 +40,7 @@ static int kvm_mips_map_page(struct kvm *kvm, gfn_t gfn) · 39 unmodified lines
4040 srcu_idx = srcu_read_lock(&kvm->srcu);
4141 pfn = gfn_to_pfn(kvm, gfn);
4242
43− if (is_error_pfn(pfn)) {
43+ if (is_error_noslot_pfn(pfn)) {
4444 kvm_err("Couldn't get pfn for gfn %#llx!\n", gfn);
4545 err = -EFAULT;
4646 goto out;
arch/x86/kvm/vmx.c+69−67
@@ -422,6 +422,7 @@ struct nested_vmx { · 421 unmodified lines
422422 struct list_head vmcs02_pool;
423423 int vmcs02_num;
424424 u64 vmcs01_tsc_offset;
425+ bool change_vmcs01_virtual_x2apic_mode;
425426 /* L2 must run next, and mustn't decide to exit to L1. */
426427 bool nested_run_pending;
427428 /*
@@ -435,6 +436,8 @@ struct nested_vmx { · 7 unmodified lines
435436 bool pi_pending;
436437 u16 posted_intr_nv;
437438
439+ unsigned long *msr_bitmap;
440+
438441 struct hrtimer preemption_timer;
439442 bool preemption_timer_expired;
440443
@@ -924,7 +927,6 @@ static unsigned long *vmx_msr_bitmap_legacy; · 483 unmodified lines
924927 static unsigned long *vmx_msr_bitmap_longmode;
925928 static unsigned long *vmx_msr_bitmap_legacy_x2apic;
926929 static unsigned long *vmx_msr_bitmap_longmode_x2apic;
927−static unsigned long *vmx_msr_bitmap_nested;
928930 static unsigned long *vmx_vmread_bitmap;
929931 static unsigned long *vmx_vmwrite_bitmap;
930932
@@ -2198,6 +2200,12 @@ static void vmx_vcpu_pi_load(struct kvm_vcpu *vcpu, int cpu) · 1267 unmodified lines
21982200 new.control) != old.control);
21992201 }
22002202
2203+static void decache_tsc_multiplier(struct vcpu_vmx *vmx)
2204+{
2205+ vmx->current_tsc_ratio = vmx->vcpu.arch.tsc_scaling_ratio;
2206+ vmcs_write64(TSC_MULTIPLIER, vmx->current_tsc_ratio);
2207+}
2208+
22012209 /*
22022210 * Switches to specified vcpu, until a matching vcpu_put(), but assumes
22032211 * vcpu mutex is already taken.
@@ -2256,10 +2264,8 @@ static void vmx_vcpu_load(struct kvm_vcpu *vcpu, int cpu) · 52 unmodified lines
22562264
22572265 /* Setup TSC multiplier */
22582266 if (kvm_has_tsc_control &&
2259− vmx->current_tsc_ratio != vcpu->arch.tsc_scaling_ratio) {
2260− vmx->current_tsc_ratio = vcpu->arch.tsc_scaling_ratio;
2261− vmcs_write64(TSC_MULTIPLIER, vmx->current_tsc_ratio);
2262− }
2267+ vmx->current_tsc_ratio != vcpu->arch.tsc_scaling_ratio)
2268+ decache_tsc_multiplier(vmx);
22632269
22642270 vmx_vcpu_pi_load(vcpu, cpu);
22652271 vmx->host_pkru = read_pkru();
@@ -2508,7 +2514,7 @@ static void vmx_set_msr_bitmap(struct kvm_vcpu *vcpu) · 242 unmodified lines
25082514 unsigned long *msr_bitmap;
25092515
25102516 if (is_guest_mode(vcpu))
2511− msr_bitmap = vmx_msr_bitmap_nested;
2517+ msr_bitmap = to_vmx(vcpu)->nested.msr_bitmap;
25122518 else if (cpu_has_secondary_exec_ctrls() &&
25132519 (vmcs_read32(SECONDARY_VM_EXEC_CONTROL) &
25142520 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE)) {
@@ -6363,13 +6369,6 @@ static __init int hardware_setup(void) · 3848 unmodified lines
63636369 if (!vmx_msr_bitmap_longmode_x2apic)
63646370 goto out4;
63656371
6366− if (nested) {
6367− vmx_msr_bitmap_nested =
6368− (unsigned long *)__get_free_page(GFP_KERNEL);
6369− if (!vmx_msr_bitmap_nested)
6370− goto out5;
6371− }
6372−
63736372 vmx_vmread_bitmap = (unsigned long *)__get_free_page(GFP_KERNEL);
63746373 if (!vmx_vmread_bitmap)
63756374 goto out6;
@@ -6392,8 +6391,6 @@ static __init int hardware_setup(void) · 16 unmodified lines
63926391
63936392 memset(vmx_msr_bitmap_legacy, 0xff, PAGE_SIZE);
63946393 memset(vmx_msr_bitmap_longmode, 0xff, PAGE_SIZE);
6395− if (nested)
6396− memset(vmx_msr_bitmap_nested, 0xff, PAGE_SIZE);
63976394
63986395 if (setup_vmcs_config(&vmcs_config) < 0) {
63996396 r = -EIO;
@@ -6529,9 +6526,6 @@ out8: · 129 unmodified lines
65296526 out7:
65306527 free_page((unsigned long)vmx_vmread_bitmap);
65316528 out6:
6532− if (nested)
6533− free_page((unsigned long)vmx_msr_bitmap_nested);
6534−out5:
65356529 free_page((unsigned long)vmx_msr_bitmap_longmode_x2apic);
65366530 out4:
65376531 free_page((unsigned long)vmx_msr_bitmap_longmode);
@@ -6557,8 +6551,6 @@ static __exit void hardware_unsetup(void) · 19 unmodified lines
65576551 free_page((unsigned long)vmx_io_bitmap_a);
65586552 free_page((unsigned long)vmx_vmwrite_bitmap);
65596553 free_page((unsigned long)vmx_vmread_bitmap);
6560− if (nested)
6561− free_page((unsigned long)vmx_msr_bitmap_nested);
65626554
65636555 free_kvm_area();
65646556 }
@@ -6995,16 +6987,21 @@ static int handle_vmon(struct kvm_vcpu *vcpu) · 430 unmodified lines
69956987 return 1;
69966988 }
69976989
6990+ if (cpu_has_vmx_msr_bitmap()) {
6991+ vmx->nested.msr_bitmap =
6992+ (unsigned long *)__get_free_page(GFP_KERNEL);
6993+ if (!vmx->nested.msr_bitmap)
6994+ goto out_msr_bitmap;
6995+ }
6996+
69986997 vmx->nested.cached_vmcs12 = kmalloc(VMCS12_SIZE, GFP_KERNEL);
69996998 if (!vmx->nested.cached_vmcs12)
7000− return -ENOMEM;
6999+ goto out_cached_vmcs12;
70017000
70027001 if (enable_shadow_vmcs) {
70037002 shadow_vmcs = alloc_vmcs();
7004− if (!shadow_vmcs) {
7005− kfree(vmx->nested.cached_vmcs12);
7006− return -ENOMEM;
7007− }
7003+ if (!shadow_vmcs)
7004+ goto out_shadow_vmcs;
70087005 /* mark vmcs as shadow */
70097006 shadow_vmcs->revision_id |= (1u << 31);
70107007 /* init shadow vmcs */
@@ -7024,6 +7021,15 @@ static int handle_vmon(struct kvm_vcpu *vcpu) · 13 unmodified lines
70247021 skip_emulated_instruction(vcpu);
70257022 nested_vmx_succeed(vcpu);
70267023 return 1;
7024+
7025+out_shadow_vmcs:
7026+ kfree(vmx->nested.cached_vmcs12);
7027+
7028+out_cached_vmcs12:
7029+ free_page((unsigned long)vmx->nested.msr_bitmap);
7030+
7031+out_msr_bitmap:
7032+ return -ENOMEM;
70277033 }
70287034
70297035 /*
@@ -7098,6 +7104,10 @@ static void free_nested(struct vcpu_vmx *vmx) · 68 unmodified lines
70987104 vmx->nested.vmxon = false;
70997105 free_vpid(vmx->nested.vpid02);
71007106 nested_release_vmcs12(vmx);
7107+ if (vmx->nested.msr_bitmap) {
7108+ free_page((unsigned long)vmx->nested.msr_bitmap);
7109+ vmx->nested.msr_bitmap = NULL;
7110+ }
71017111 if (enable_shadow_vmcs)
71027112 free_vmcs(vmx->nested.current_shadow_vmcs);
71037113 kfree(vmx->nested.cached_vmcs12);
@@ -8419,6 +8429,12 @@ static void vmx_set_virtual_x2apic_mode(struct kvm_vcpu *vcpu, bool set) · 1315 unmodified lines
84198429 {
84208430 u32 sec_exec_control;
84218431
8432+ /* Postpone execution until vmcs01 is the current VMCS. */
8433+ if (is_guest_mode(vcpu)) {
8434+ to_vmx(vcpu)->nested.change_vmcs01_virtual_x2apic_mode = true;
8435+ return;
8436+ }
8437+
84228438 /*
84238439 * There is not point to enable virtualize x2apic without enable
84248440 * apicv
@@ -9472,8 +9488,10 @@ static inline bool nested_vmx_merge_msr_bitmap(struct kvm_vcpu *vcpu, · 1047 unmodified lines
94729488 {
94739489 int msr;
94749490 struct page *page;
9475− unsigned long *msr_bitmap;
9491+ unsigned long *msr_bitmap_l1;
9492+ unsigned long *msr_bitmap_l0 = to_vmx(vcpu)->nested.msr_bitmap;
94769493
9494+ /* This shortcut is ok because we support only x2APIC MSRs so far. */
94779495 if (!nested_cpu_has_virt_x2apic_mode(vmcs12))
94789496 return false;
94799497
@@ -9482,63 +9500,37 @@ static inline bool nested_vmx_merge_msr_bitmap(struct kvm_vcpu *vcpu, · 2 unmodified lines
94829500 WARN_ON(1);
94839501 return false;
94849502 }
9485− msr_bitmap = (unsigned long *)kmap(page);
9486− if (!msr_bitmap) {
9503+ msr_bitmap_l1 = (unsigned long *)kmap(page);
9504+ if (!msr_bitmap_l1) {
94879505 nested_release_page_clean(page);
94889506 WARN_ON(1);
94899507 return false;
94909508 }
94919509
9510+ memset(msr_bitmap_l0, 0xff, PAGE_SIZE);
9511+
94929512 if (nested_cpu_has_virt_x2apic_mode(vmcs12)) {
94939513 if (nested_cpu_has_apic_reg_virt(vmcs12))
94949514 for (msr = 0x800; msr <= 0x8ff; msr++)
94959515 nested_vmx_disable_intercept_for_msr(
9496− msr_bitmap,
9497− vmx_msr_bitmap_nested,
9516+ msr_bitmap_l1, msr_bitmap_l0,
94989517 msr, MSR_TYPE_R);
9499− /* TPR is allowed */
9500− nested_vmx_disable_intercept_for_msr(msr_bitmap,
9501− vmx_msr_bitmap_nested,
9518+
9519+ nested_vmx_disable_intercept_for_msr(
9520+ msr_bitmap_l1, msr_bitmap_l0,
95029521 APIC_BASE_MSR + (APIC_TASKPRI >> 4),
95039522 MSR_TYPE_R | MSR_TYPE_W);
9523+
95049524 if (nested_cpu_has_vid(vmcs12)) {
9505− /* EOI and self-IPI are allowed */
95069525 nested_vmx_disable_intercept_for_msr(
9507− msr_bitmap,
9508− vmx_msr_bitmap_nested,
9526+ msr_bitmap_l1, msr_bitmap_l0,
95099527 APIC_BASE_MSR + (APIC_EOI >> 4),
95109528 MSR_TYPE_W);
95119529 nested_vmx_disable_intercept_for_msr(
9512− msr_bitmap,
9513− vmx_msr_bitmap_nested,
9530+ msr_bitmap_l1, msr_bitmap_l0,
95149531 APIC_BASE_MSR + (APIC_SELF_IPI >> 4),
95159532 MSR_TYPE_W);
95169533 }
9517− } else {
9518− /*
9519− * Enable reading intercept of all the x2apic
9520− * MSRs. We should not rely on vmcs12 to do any
9521− * optimizations here, it may have been modified
9522− * by L1.
9523− */
9524− for (msr = 0x800; msr <= 0x8ff; msr++)
9525− __vmx_enable_intercept_for_msr(
9526− vmx_msr_bitmap_nested,
9527− msr,
9528− MSR_TYPE_R);
9529−
9530− __vmx_enable_intercept_for_msr(
9531− vmx_msr_bitmap_nested,
9532− APIC_BASE_MSR + (APIC_TASKPRI >> 4),
9533− MSR_TYPE_W);
9534− __vmx_enable_intercept_for_msr(
9535− vmx_msr_bitmap_nested,
9536− APIC_BASE_MSR + (APIC_EOI >> 4),
9537− MSR_TYPE_W);
9538− __vmx_enable_intercept_for_msr(
9539− vmx_msr_bitmap_nested,
9540− APIC_BASE_MSR + (APIC_SELF_IPI >> 4),
9541− MSR_TYPE_W);
95429534 }
95439535 kunmap(page);
95449536 nested_release_page_clean(page);
@@ -9957,10 +9949,10 @@ static void prepare_vmcs02(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12) · 412 unmodified lines
99579949 }
99589950
99599951 if (cpu_has_vmx_msr_bitmap() &&
9960− exec_control & CPU_BASED_USE_MSR_BITMAPS) {
9961− nested_vmx_merge_msr_bitmap(vcpu, vmcs12);
9962− /* MSR_BITMAP will be set by following vmx_set_efer. */
9963− } else
9952+ exec_control & CPU_BASED_USE_MSR_BITMAPS &&
9953+ nested_vmx_merge_msr_bitmap(vcpu, vmcs12))
9954+ ; /* MSR_BITMAP will be set by following vmx_set_efer. */
9955+ else
99649956 exec_control &= ~CPU_BASED_USE_MSR_BITMAPS;
99659957
99669958 /*
@@ -10011,6 +10003,8 @@ static void prepare_vmcs02(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12) · 44 unmodified lines
1001110003 vmx->nested.vmcs01_tsc_offset + vmcs12->tsc_offset);
1001210004 else
1001310005 vmcs_write64(TSC_OFFSET, vmx->nested.vmcs01_tsc_offset);
10006+ if (kvm_has_tsc_control)
10007+ decache_tsc_multiplier(vmx);
1001410008
1001510009 if (enable_vpid) {
1001610010 /*
@@ -10767,6 +10761,14 @@ static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason, · 750 unmodified lines
1076710761 else
1076810762 vmcs_set_bits(PIN_BASED_VM_EXEC_CONTROL,
1076910763 PIN_BASED_VMX_PREEMPTION_TIMER);
10764+ if (kvm_has_tsc_control)
10765+ decache_tsc_multiplier(vmx);
10766+
10767+ if (vmx->nested.change_vmcs01_virtual_x2apic_mode) {
10768+ vmx->nested.change_vmcs01_virtual_x2apic_mode = false;
10769+ vmx_set_virtual_x2apic_mode(vcpu,
10770+ vcpu->arch.apic_base & X2APIC_ENABLE);
10771+ }
1077010772
1077110773 /* This is needed for same reason as it was needed in prepare_vmcs02 */
1077210774 vmx->host_rsp = 0;
include/linux/irqchip/arm-gic-v3.h+1−0
@@ -337,6 +337,7 @@ · 336 unmodified lines
337337 */
338338 #define E_ITS_MOVI_UNMAPPED_INTERRUPT 0x010107
339339 #define E_ITS_MOVI_UNMAPPED_COLLECTION 0x010109
340+#define E_ITS_INT_UNMAPPED_INTERRUPT 0x010307
340341 #define E_ITS_CLEAR_UNMAPPED_INTERRUPT 0x010507
341342 #define E_ITS_MAPD_DEVICE_OOR 0x010801
342343 #define E_ITS_MAPC_PROCNUM_OOR 0x010902
virt/kvm/arm/arch_timer.c+10−1
@@ -33,6 +33,7 @@ · 32 unmodified lines
3333 static struct timecounter *timecounter;
3434 static struct workqueue_struct *wqueue;
3535 static unsigned int host_vtimer_irq;
36+static u32 host_vtimer_irq_flags;
3637
3738 void kvm_timer_vcpu_put(struct kvm_vcpu *vcpu)
3839 {
@@ -365,7 +366,7 @@ void kvm_timer_vcpu_init(struct kvm_vcpu *vcpu) · 326 unmodified lines
365366
366367 static void kvm_timer_init_interrupt(void *info)
367368 {
368− enable_percpu_irq(host_vtimer_irq, 0);
369+ enable_percpu_irq(host_vtimer_irq, host_vtimer_irq_flags);
369370 }
370371
371372 int kvm_arm_timer_set_reg(struct kvm_vcpu *vcpu, u64 regid, u64 value)
@@ -432,6 +433,14 @@ int kvm_timer_hyp_init(void) · 60 unmodified lines
432433 }
433434 host_vtimer_irq = info->virtual_irq;
434435
436+ host_vtimer_irq_flags = irq_get_trigger_type(host_vtimer_irq);
437+ if (host_vtimer_irq_flags != IRQF_TRIGGER_HIGH &&
438+ host_vtimer_irq_flags != IRQF_TRIGGER_LOW) {
439+ kvm_err("Invalid trigger for IRQ%d, assuming level low\n",
440+ host_vtimer_irq);
441+ host_vtimer_irq_flags = IRQF_TRIGGER_LOW;
442+ }
443+
435444 err = request_percpu_irq(host_vtimer_irq, kvm_arch_timer_handler,
436445 "kvm guest timer", kvm_get_running_vcpus());
437446 if (err) {
virt/kvm/arm/vgic/vgic-its.c+114−44
@@ -51,7 +51,7 @@ static struct vgic_irq *vgic_add_lpi(struct kvm *kvm, u32 intid) · 50 unmodified lines
5151
5252 irq = kzalloc(sizeof(struct vgic_irq), GFP_KERNEL);
5353 if (!irq)
54− return NULL;
54+ return ERR_PTR(-ENOMEM);
5555
5656 INIT_LIST_HEAD(&irq->lpi_list);
5757 INIT_LIST_HEAD(&irq->ap_list);
@@ -441,39 +441,63 @@ static unsigned long vgic_mmio_read_its_idregs(struct kvm *kvm, · 383 unmodified lines
441441 * Find the target VCPU and the LPI number for a given devid/eventid pair
442442 * and make this IRQ pending, possibly injecting it.
443443 * Must be called with the its_lock mutex held.
444+ * Returns 0 on success, a positive error value for any ITS mapping
445+ * related errors and negative error values for generic errors.
444446 */
445−static void vgic_its_trigger_msi(struct kvm *kvm, struct vgic_its *its,
446− u32 devid, u32 eventid)
447+static int vgic_its_trigger_msi(struct kvm *kvm, struct vgic_its *its,
448+ u32 devid, u32 eventid)
447449 {
450+ struct kvm_vcpu *vcpu;
448451 struct its_itte *itte;
449452
450453 if (!its->enabled)
451− return;
454+ return -EBUSY;
452455
453456 itte = find_itte(its, devid, eventid);
454− /* Triggering an unmapped IRQ gets silently dropped. */
455− if (itte && its_is_collection_mapped(itte->collection)) {
456− struct kvm_vcpu *vcpu;
457−
458− vcpu = kvm_get_vcpu(kvm, itte->collection->target_addr);
459− if (vcpu && vcpu->arch.vgic_cpu.lpis_enabled) {
460− spin_lock(&itte->irq->irq_lock);
461− itte->irq->pending = true;
462− vgic_queue_irq_unlock(kvm, itte->irq);
463− }
464− }
457+ if (!itte || !its_is_collection_mapped(itte->collection))
458+ return E_ITS_INT_UNMAPPED_INTERRUPT;
459+
460+ vcpu = kvm_get_vcpu(kvm, itte->collection->target_addr);
461+ if (!vcpu)
462+ return E_ITS_INT_UNMAPPED_INTERRUPT;
463+
464+ if (!vcpu->arch.vgic_cpu.lpis_enabled)
465+ return -EBUSY;
466+
467+ spin_lock(&itte->irq->irq_lock);
468+ itte->irq->pending = true;
469+ vgic_queue_irq_unlock(kvm, itte->irq);
470+
471+ return 0;
472+}
473+
474+static struct vgic_io_device *vgic_get_its_iodev(struct kvm_io_device *dev)
475+{
476+ struct vgic_io_device *iodev;
477+
478+ if (dev->ops != &kvm_io_gic_ops)
479+ return NULL;
480+
481+ iodev = container_of(dev, struct vgic_io_device, dev);
482+
483+ if (iodev->iodev_type != IODEV_ITS)
484+ return NULL;
485+
486+ return iodev;
465487 }
466488
467489 /*
468490 * Queries the KVM IO bus framework to get the ITS pointer from the given
469491 * doorbell address.
470492 * We then call vgic_its_trigger_msi() with the decoded data.
493+ * According to the KVM_SIGNAL_MSI API description returns 1 on success.
471494 */
472495 int vgic_its_inject_msi(struct kvm *kvm, struct kvm_msi *msi)
473496 {
474497 u64 address;
475498 struct kvm_io_device *kvm_io_dev;
476499 struct vgic_io_device *iodev;
500+ int ret;
477501
478502 if (!vgic_has_its(kvm))
479503 return -ENODEV;
@@ -485,15 +509,28 @@ int vgic_its_inject_msi(struct kvm *kvm, struct kvm_msi *msi) · 5 unmodified lines
485509
486510 kvm_io_dev = kvm_io_bus_get_dev(kvm, KVM_MMIO_BUS, address);
487511 if (!kvm_io_dev)
488− return -ENODEV;
512+ return -EINVAL;
489513
490− iodev = container_of(kvm_io_dev, struct vgic_io_device, dev);
514+ iodev = vgic_get_its_iodev(kvm_io_dev);
515+ if (!iodev)
516+ return -EINVAL;
491517
492518 mutex_lock(&iodev->its->its_lock);
493− vgic_its_trigger_msi(kvm, iodev->its, msi->devid, msi->data);
519+ ret = vgic_its_trigger_msi(kvm, iodev->its, msi->devid, msi->data);
494520 mutex_unlock(&iodev->its->its_lock);
495521
496− return 0;
522+ if (ret < 0)
523+ return ret;
524+
525+ /*
526+ * KVM_SIGNAL_MSI demands a return value > 0 for success and 0
527+ * if the guest has blocked the MSI. So we map any LPI mapping
528+ * related error to that.
529+ */
530+ if (ret)
531+ return 0;
532+ else
533+ return 1;
497534 }
498535
499536 /* Requires the its_lock to be held. */
@@ -502,7 +539,8 @@ static void its_free_itte(struct kvm *kvm, struct its_itte *itte) · 2 unmodified lines
502539 list_del(&itte->itte_list);
503540
504541 /* This put matches the get in vgic_add_lpi. */
505− vgic_put_irq(kvm, itte->irq);
542+ if (itte->irq)
543+ vgic_put_irq(kvm, itte->irq);
506544
507545 kfree(itte);
508546 }
@@ -697,6 +735,7 @@ static int vgic_its_cmd_handle_mapi(struct kvm *kvm, struct vgic_its *its, · 188 unmodified lines
697735 struct its_device *device;
698736 struct its_collection *collection, *new_coll = NULL;
699737 int lpi_nr;
738+ struct vgic_irq *irq;
700739
701740 device = find_its_device(its, device_id);
702741 if (!device)
@@ -710,6 +749,10 @@ static int vgic_its_cmd_handle_mapi(struct kvm *kvm, struct vgic_its *its, · 7 unmodified lines
710749 lpi_nr >= max_lpis_propbaser(kvm->arch.vgic.propbaser))
711750 return E_ITS_MAPTI_PHYSICALID_OOR;
712751
752+ /* If there is an existing mapping, behavior is UNPREDICTABLE. */
753+ if (find_itte(its, device_id, event_id))
754+ return 0;
755+
713756 collection = find_collection(its, coll_id);
714757 if (!collection) {
715758 int ret = vgic_its_alloc_collection(its, &collection, coll_id);
@@ -718,22 +761,28 @@ static int vgic_its_cmd_handle_mapi(struct kvm *kvm, struct vgic_its *its, · 2 unmodified lines
718761 new_coll = collection;
719762 }
720763
721− itte = find_itte(its, device_id, event_id);
764+ itte = kzalloc(sizeof(struct its_itte), GFP_KERNEL);
722765 if (!itte) {
723− itte = kzalloc(sizeof(struct its_itte), GFP_KERNEL);
724− if (!itte) {
725− if (new_coll)
726− vgic_its_free_collection(its, coll_id);
727− return -ENOMEM;
728− }
729−
730− itte->event_id = event_id;
731− list_add_tail(&itte->itte_list, &device->itt_head);
766+ if (new_coll)
767+ vgic_its_free_collection(its, coll_id);
768+ return -ENOMEM;
732769 }
733770
771+ itte->event_id = event_id;
772+ list_add_tail(&itte->itte_list, &device->itt_head);
773+
734774 itte->collection = collection;
735775 itte->lpi = lpi_nr;
736− itte->irq = vgic_add_lpi(kvm, lpi_nr);
776+
777+ irq = vgic_add_lpi(kvm, lpi_nr);
778+ if (IS_ERR(irq)) {
779+ if (new_coll)
780+ vgic_its_free_collection(its, coll_id);
781+ its_free_itte(kvm, itte);
782+ return PTR_ERR(irq);
783+ }
784+ itte->irq = irq;
785+
737786 update_affinity_itte(kvm, itte);
738787
739788 /*
@@ -981,9 +1030,7 @@ static int vgic_its_cmd_handle_int(struct kvm *kvm, struct vgic_its *its, · 241 unmodified lines
9811030 u32 msi_data = its_cmd_get_id(its_cmd);
9821031 u64 msi_devid = its_cmd_get_deviceid(its_cmd);
9831032
984− vgic_its_trigger_msi(kvm, its, msi_devid, msi_data);
985−
986− return 0;
1033+ return vgic_its_trigger_msi(kvm, its, msi_devid, msi_data);
9871034 }
9881035
9891036 /*
@@ -1288,13 +1335,13 @@ void vgic_enable_lpis(struct kvm_vcpu *vcpu) · 298 unmodified lines
12881335 its_sync_lpi_pending_table(vcpu);
12891336 }
12901337
1291−static int vgic_its_init_its(struct kvm *kvm, struct vgic_its *its)
1338+static int vgic_register_its_iodev(struct kvm *kvm, struct vgic_its *its)
12921339 {
12931340 struct vgic_io_device *iodev = &its->iodev;
12941341 int ret;
12951342
1296− if (its->initialized)
1297− return 0;
1343+ if (!its->initialized)
1344+ return -EBUSY;
12981345
12991346 if (IS_VGIC_ADDR_UNDEF(its->vgic_its_base))
13001347 return -ENXIO;
@@ -1311,9 +1358,6 @@ static int vgic_its_init_its(struct kvm *kvm, struct vgic_its *its) · 10 unmodified lines
13111358 KVM_VGIC_V3_ITS_SIZE, &iodev->dev);
13121359 mutex_unlock(&kvm->slots_lock);
13131360
1314− if (!ret)
1315− its->initialized = true;
1316−
13171361 return ret;
13181362 }
13191363
@@ -1435,9 +1479,6 @@ static int vgic_its_set_attr(struct kvm_device *dev, · 115 unmodified lines
14351479 if (type != KVM_VGIC_ITS_ADDR_TYPE)
14361480 return -ENODEV;
14371481
1438− if (its->initialized)
1439− return -EBUSY;
1440−
14411482 if (copy_from_user(&addr, uaddr, sizeof(addr)))
14421483 return -EFAULT;
14431484
@@ -1453,7 +1494,9 @@ static int vgic_its_set_attr(struct kvm_device *dev, · 9 unmodified lines
14531494 case KVM_DEV_ARM_VGIC_GRP_CTRL:
14541495 switch (attr->attr) {
14551496 case KVM_DEV_ARM_VGIC_CTRL_INIT:
1456− return vgic_its_init_its(dev->kvm, its);
1497+ its->initialized = true;
1498+
1499+ return 0;
14571500 }
14581501 break;
14591502 }
@@ -1498,3 +1541,30 @@ int kvm_vgic_register_its_device(void) · 38 unmodified lines
14981541 return kvm_register_device_ops(&kvm_arm_vgic_its_ops,
14991542 KVM_DEV_TYPE_ARM_VGIC_ITS);
15001543 }
1544+
1545+/*
1546+ * Registers all ITSes with the kvm_io_bus framework.
1547+ * To follow the existing VGIC initialization sequence, this has to be
1548+ * done as late as possible, just before the first VCPU runs.
1549+ */
1550+int vgic_register_its_iodevs(struct kvm *kvm)
1551+{
1552+ struct kvm_device *dev;
1553+ int ret = 0;
1554+
1555+ list_for_each_entry(dev, &kvm->devices, vm_node) {
1556+ if (dev->ops != &kvm_arm_vgic_its_ops)
1557+ continue;
1558+
1559+ ret = vgic_register_its_iodev(kvm, dev->private);
1560+ if (ret)
1561+ return ret;
1562+ /*
1563+ * We don't need to care about tearing down previously
1564+ * registered ITSes, as the kvm_io_bus framework removes
1565+ * them for us if the VM gets destroyed.
1566+ */
1567+ }
1568+
1569+ return ret;
1570+}
virt/kvm/arm/vgic/vgic-mmio-v3.c+16−10
@@ -306,16 +306,19 @@ static void vgic_mmio_write_propbase(struct kvm_vcpu *vcpu, · 305 unmodified lines
306306 {
307307 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
308308 struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
309− u64 propbaser = dist->propbaser;
309+ u64 old_propbaser, propbaser;
310310
311311 /* Storing a value with LPIs already enabled is undefined */
312312 if (vgic_cpu->lpis_enabled)
313313 return;
314314
315− propbaser = update_64bit_reg(propbaser, addr & 4, len, val);
316− propbaser = vgic_sanitise_propbaser(propbaser);
317−
318− dist->propbaser = propbaser;
315+ do {
316+ old_propbaser = dist->propbaser;
317+ propbaser = old_propbaser;
318+ propbaser = update_64bit_reg(propbaser, addr & 4, len, val);
319+ propbaser = vgic_sanitise_propbaser(propbaser);
320+ } while (cmpxchg64(&dist->propbaser, old_propbaser,
321+ propbaser) != old_propbaser);
319322 }
320323
321324 static unsigned long vgic_mmio_read_pendbase(struct kvm_vcpu *vcpu,
@@ -331,16 +334,19 @@ static void vgic_mmio_write_pendbase(struct kvm_vcpu *vcpu, · 9 unmodified lines
331334 unsigned long val)
332335 {
333336 struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
334− u64 pendbaser = vgic_cpu->pendbaser;
337+ u64 old_pendbaser, pendbaser;
335338
336339 /* Storing a value with LPIs already enabled is undefined */
337340 if (vgic_cpu->lpis_enabled)
338341 return;
339342
340− pendbaser = update_64bit_reg(pendbaser, addr & 4, len, val);
341− pendbaser = vgic_sanitise_pendbaser(pendbaser);
342−
343− vgic_cpu->pendbaser = pendbaser;
343+ do {
344+ old_pendbaser = vgic_cpu->pendbaser;
345+ pendbaser = old_pendbaser;
346+ pendbaser = update_64bit_reg(pendbaser, addr & 4, len, val);
347+ pendbaser = vgic_sanitise_pendbaser(pendbaser);
348+ } while (cmpxchg64(&vgic_cpu->pendbaser, old_pendbaser,
349+ pendbaser) != old_pendbaser);
344350 }
345351
346352 /*
virt/kvm/arm/vgic/vgic-v3.c+8−0
@@ -289,6 +289,14 @@ int vgic_v3_map_resources(struct kvm *kvm) · 288 unmodified lines
289289 goto out;
290290 }
291291
292+ if (vgic_has_its(kvm)) {
293+ ret = vgic_register_its_iodevs(kvm);
294+ if (ret) {
295+ kvm_err("Unable to register VGIC ITS MMIO regions\n");
296+ goto out;
297+ }
298+ }
299+
292300 dist->ready = true;
293301
294302 out:
virt/kvm/arm/vgic/vgic.c+5−5
@@ -117,17 +117,17 @@ static void vgic_irq_release(struct kref *ref) · 116 unmodified lines
117117
118118 void vgic_put_irq(struct kvm *kvm, struct vgic_irq *irq)
119119 {
120− struct vgic_dist *dist;
120+ struct vgic_dist *dist = &kvm->arch.vgic;
121121
122122 if (irq->intid < VGIC_MIN_LPI)
123123 return;
124124
125− if (!kref_put(&irq->refcount, vgic_irq_release))
125+ spin_lock(&dist->lpi_list_lock);
126+ if (!kref_put(&irq->refcount, vgic_irq_release)) {
127+ spin_unlock(&dist->lpi_list_lock);
126128 return;
129+ };
127130
128− dist = &kvm->arch.vgic;
129−
130− spin_lock(&dist->lpi_list_lock);
131131 list_del(&irq->lpi_list);
132132 dist->lpi_list_count--;
133133 spin_unlock(&dist->lpi_list_lock);
virt/kvm/arm/vgic/vgic.h+6−0
@@ -84,6 +84,7 @@ void vgic_v3_enable(struct kvm_vcpu *vcpu); · 83 unmodified lines
8484 int vgic_v3_probe(const struct gic_kvm_info *info);
8585 int vgic_v3_map_resources(struct kvm *kvm);
8686 int vgic_register_redist_iodevs(struct kvm *kvm, gpa_t dist_base_address);
87+int vgic_register_its_iodevs(struct kvm *kvm);
8788 bool vgic_has_its(struct kvm *kvm);
8889 int kvm_vgic_register_its_device(void);
8990 void vgic_enable_lpis(struct kvm_vcpu *vcpu);
@@ -140,6 +141,11 @@ static inline int vgic_register_redist_iodevs(struct kvm *kvm, · 50 unmodified lines
140141 return -ENODEV;
141142 }
142143
144+static inline int vgic_register_its_iodevs(struct kvm *kvm)
145+{
146+ return -ENODEV;
147+}
148+
143149 static inline bool vgic_has_its(struct kvm *kvm)
144150 {
145151 return false;