Skip to content

Commit ebb127f

Browse files
author
Christoffer Dall
committed
KVM: arm/arm64: Don't assume initialized vgic when setting PMU IRQ
The PMU IRQ number is set through the VCPU device's KVM_SET_DEVICE_ATTR ioctl handler for the KVM_ARM_VCPU_PMU_V3_IRQ attribute, but there is no enforced or stated requirement that this must happen after initializing the VGIC. As a result, calling vgic_valid_spi() which relies on the nr_spis being set during the VGIC init can incorrectly fail. Introduce irq_is_spi, which determines if an IRQ number is within the SPI range without verifying it against the actual VGIC properties. Signed-off-by: Christoffer Dall <cdall@linaro.org> Reviewed-by: Marc Zyngier <marc.zyngier@arm.com>
1 parent cb3f0ad commit ebb127f

2 files changed

Lines changed: 20 additions & 4 deletions

File tree

‎include/kvm/arm_vgic.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@
3939
#define KVM_IRQCHIP_NUM_PINS (1020 - 32)
4040

4141
#define irq_is_ppi(irq) ((irq) >= VGIC_NR_SGIS && (irq) < VGIC_NR_PRIVATE_IRQS)
42+
#define irq_is_spi(irq) ((irq) >= VGIC_NR_PRIVATE_IRQS && \
43+
(irq) <= VGIC_MAX_SPI)
4244

4345
enum vgic_type {
4446
VGIC_V2, /* Good ol' GICv2 */

‎virt/kvm/arm/pmu.c‎

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -458,10 +458,24 @@ int kvm_arm_pmu_v3_enable(struct kvm_vcpu *vcpu)
458458
/*
459459
* A valid interrupt configuration for the PMU is either to have a
460460
* properly configured interrupt number and using an in-kernel
461-
* irqchip, or to neither set an IRQ nor create an in-kernel irqchip.
461+
* irqchip, or to not have an in-kernel GIC and not set an IRQ.
462462
*/
463-
if (kvm_arm_pmu_irq_initialized(vcpu) != irqchip_in_kernel(vcpu->kvm))
464-
return -EINVAL;
463+
if (irqchip_in_kernel(vcpu->kvm)) {
464+
int irq = vcpu->arch.pmu.irq_num;
465+
if (!kvm_arm_pmu_irq_initialized(vcpu))
466+
return -EINVAL;
467+
468+
/*
469+
* If we are using an in-kernel vgic, at this point we know
470+
* the vgic will be initialized, so we can check the PMU irq
471+
* number against the dimensions of the vgic and make sure
472+
* it's valid.
473+
*/
474+
if (!irq_is_ppi(irq) && !vgic_valid_spi(vcpu->kvm, irq))
475+
return -EINVAL;
476+
} else if (kvm_arm_pmu_irq_initialized(vcpu)) {
477+
return -EINVAL;
478+
}
465479

466480
kvm_pmu_vcpu_reset(vcpu);
467481
vcpu->arch.pmu.ready = true;
@@ -547,7 +561,7 @@ int kvm_arm_pmu_v3_set_attr(struct kvm_vcpu *vcpu, struct kvm_device_attr *attr)
547561
return -EFAULT;
548562

549563
/* The PMU overflow interrupt can be a PPI or a valid SPI. */
550-
if (!(irq_is_ppi(irq) || vgic_valid_spi(vcpu->kvm, irq)))
564+
if (!(irq_is_ppi(irq) || irq_is_spi(irq)))
551565
return -EINVAL;
552566

553567
if (!pmu_irq_is_valid(vcpu->kvm, irq))

0 commit comments

Comments
 (0)