Userspace timer IRQ number control and PMU with userspace-gic
Subsystems (5)
v3 cover Userspace timer IRQ number control and PMU with userspace-gic
So far we have been getting away with letting the kernel choosing interrupt numbers for the timers in the kernel and we have crossed our fingers in hoping that the DT/ACPI provided by userspace matches with the interrupt number we use in the kernel for a given VCPU type.
But as we are generally moving towards letting userspace be in more fine grained control of what is being emulated, let userspace decide the irq numbers for the timers.
Slightly related, and therefore included in this series, we have recently added support for a userspace GIC while still supporting the generic timers and the PMU from inside the VM. Unfortunately we forgot to rework the code to actually let userspace create the PMU device without creating an in-kernel GIC.
Patches 6-9 could be omitted and still support the basic functionality targeted by this series if preferred.
Tested on APM X-Gene, Thunder-X, and TC2.
QEMU patches used for testing can be found here:
https://git.linaro.org/people/christoffer.dall/qemu-arm.git timer-pmu-irqs
Changes since v2:
- Grammer change
- Move PMU irq consistency check to when we are about to run a VCPU, ensuring that we do not allow an unconfigure irq line with an in-kernel gic.
- Rebased on kvmarm/queue.
- Acked reviewed-by and acked-by from Marc on some patches
Changes since v1:
- Rebased on v4.12-rc1
- Check for irqchip_in_kernel() in kvm_arm_pmu_v3_get_attr
- Slightly change the wording around default timer IRQs
- Add fix for the PMU IRQ setting that assumes an initialized vgic
- Introduce an allocator for IRQ lines so that in-kernel owned IRQ lines cannot be signalled from userspace and such that an IRQ line can only be assigned to a single device.
Christoffer Dall (9):
KVM: arm64: Allow creating the PMU without the in-kernel GIC
KVM: arm: Handle VCPU device attributes in guest.c
KVM: arm/arm64: Move irq_is_ppi() to header file
KVM: arm/arm64: Move timer IRQ default init to arch_timer.c
KVM: arm/arm64: Allow setting the timer IRQ numbers from userspace
KVM: arm/arm64: Introduce an allocator for in-kernel irq lines
KVM: arm/arm64: Check if irq lines to the GIC are already used
KVM: arm/arm64: Disallow userspace control of in-kernel IRQ lines
KVM: arm/arm64: Don't assume initialized vgic when setting PMU IRQ
Documentation/virtual/kvm/devices/vcpu.txt | 41 +++++++-- arch/arm/include/asm/kvm_host.h | 22 ++--- arch/arm/include/uapi/asm/kvm.h | 8 ++ arch/arm/kvm/guest.c | 51 +++++++++++ arch/arm/kvm/reset.c | 16 +--- arch/arm64/include/uapi/asm/kvm.h | 3 + arch/arm64/kvm/guest.c | 9 ++ arch/arm64/kvm/reset.c | 16 +--- include/kvm/arm_arch_timer.h | 8 +- include/kvm/arm_pmu.h | 6 ++ include/kvm/arm_vgic.h | 13 ++- virt/kvm/arm/arch_timer.c | 137 ++++++++++++++++++++++++++--- virt/kvm/arm/arm.c | 8 +- virt/kvm/arm/pmu.c | 65 ++++++++++---- virt/kvm/arm/vgic/vgic-irqfd.c | 2 +- virt/kvm/arm/vgic/vgic.c | 48 +++++++++- 16 files changed, 359 insertions(+), 94 deletions(-)
Signature · 1 line
2.9.0
PATCH v3 1/9 KVM: arm64: Allow creating the PMU without the in-kernel GIC
Since we got support for devices in userspace which allows reporting the PMU overflow output status to userspace, we should actually allow creating the PMU on systems without an in-kernel irqchip, which in turn requires us to slightly clarify error codes for the ABI and move things around for the initialization phase.
--- Documentation/virtual/kvm/devices/vcpu.txt | 16 ++++++---- include/kvm/arm_pmu.h | 6 ++++ virt/kvm/arm/arm.c | 4 +++ virt/kvm/arm/pmu.c | 51 +++++++++++++++++++++++------- 4 files changed, 58 insertions(+), 19 deletions(-)
Documentation/virtual/kvm/devices/vcpu.txt +9 -7
@@ -16,7 +16,9 @@ Parameters: in kvm_device_attr.addr the address for PMU overflow interrupt is a Returns: -EBUSY: The PMU overflow interrupt is already set -ENXIO: The overflow interrupt not set when attempting to get it -ENODEV: PMUv3 not supported - -EINVAL: Invalid PMU overflow interrupt number supplied + -EINVAL: Invalid PMU overflow interrupt number supplied or + trying to set the IRQ number without using an in-kernel + irqchip. A value describing the PMUv3 (Performance Monitor Unit v3) overflow interrupt number for this vcpu. This interrupt could be a PPI or SPI, but the interrupt
@@ -25,11 +27,11 @@ all vcpus, while as an SPI it must be a separate number per vcpu. 1.2 ATTRIBUTE: KVM_ARM_VCPU_PMU_V3_INIT Parameters: no additional parameter in kvm_device_attr.addr -Returns: -ENODEV: PMUv3 not supported - -ENXIO: PMUv3 not properly configured as required prior to calling this - attribute +Returns: -ENODEV: PMUv3 not supported or GIC not initialized + -ENXIO: PMUv3 not properly configured or in-kernel irqchip not + configured as required prior to calling this attribute -EBUSY: PMUv3 already initialized -Request the initialization of the PMUv3. This must be done after creating the -in-kernel irqchip. Creating a PMU with a userspace irqchip is currently not -supported. +Request the initialization of the PMUv3. If using the PMUv3 with an in-kernel +virtual GIC implementation, this must be done after initializing the in-kernel +irqchip.
include/kvm/arm_pmu.h +6 -0
@@ -35,6 +35,7 @@ struct kvm_pmu {int irq_num;struct kvm_pmc pmc[ARMV8_PMU_MAX_COUNTERS ];bool ready; +bool created;bool irq_level; };
@@ -63,6 +64,7 @@ int kvm_arm_pmu_v3_get_attr(struct kvm_vcpu *vcpu, struct kvm_device_attr *attr);int kvm_arm_pmu_v3_has_attr (struct kvm_vcpu *vcpu,struct kvm_device_attr *attr); +int kvm_arm_pmu_v3_enable (struct kvm_vcpu *vcpu);#else struct kvm_pmu { };
@@ -112,6 +114,10 @@ static inline int kvm_arm_pmu_v3_has_attr(struct kvm_vcpu *vcpu, {return -ENXIO ; } +static inline int kvm_arm_pmu_v3_enable (struct kvm_vcpu *vcpu) +{ +return 0 ; +}#endif #endif
virt/kvm/arm/arm.c +4 -0
@@ -527,6 +527,10 @@ static int kvm_vcpu_first_run_init(struct kvm_vcpu *vcpu) } ret =kvm_timer_enable (vcpu); +if (ret) +return ret; + + ret =kvm_arm_pmu_v3_enable (vcpu);return ret; }
virt/kvm/arm/pmu.c +39 -12
80 lines not on this page · raw message
Signature · 1 line
2.9.0
PATCH v3 2/9 KVM: arm: Handle VCPU device attributes in guest.c
In reply to Christoffer DallAs we are about to support VCPU attributes to set the timer IRQ numbers in guest.c, move the static inlines for the VCPU attributes handlers from the header file to guest.c.
--- arch/arm/include/asm/kvm_host.h | 22 +++++++-------------- arch/arm/kvm/guest.c | 42 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 15 deletions(-)
arch/arm/include/asm/kvm_host.h +7 -15
@@ -291,20 +291,12 @@ static inline void kvm_arm_init_debug(void) {}static inline void kvm_arm_setup_debug (struct kvm_vcpu *vcpu) {}static inline void kvm_arm_clear_debug (struct kvm_vcpu *vcpu) {}static inline void kvm_arm_reset_debug_ptr (struct kvm_vcpu *vcpu) {} -static inline int kvm_arm_vcpu_arch_set_attr (struct kvm_vcpu *vcpu, -struct kvm_device_attr *attr) -{ -return -ENXIO ; -} -static inline int kvm_arm_vcpu_arch_get_attr (struct kvm_vcpu *vcpu, -struct kvm_device_attr *attr) -{ -return -ENXIO ; -} -static inline int kvm_arm_vcpu_arch_has_attr (struct kvm_vcpu *vcpu, -struct kvm_device_attr *attr) -{ -return -ENXIO ; -} + +int kvm_arm_vcpu_arch_set_attr (struct kvm_vcpu *vcpu, +struct kvm_device_attr *attr); +int kvm_arm_vcpu_arch_get_attr (struct kvm_vcpu *vcpu, +struct kvm_device_attr *attr); +int kvm_arm_vcpu_arch_has_attr (struct kvm_vcpu *vcpu, +struct kvm_device_attr *attr);#endif /* __ARM_KVM_HOST_H__ */
arch/arm/kvm/guest.c +42 -0
45 lines not on this page · raw message
Signature · 1 line
2.9.0
PATCH v3 4/9 KVM: arm/arm64: Move timer IRQ default init to arch_timer.c
In reply to Christoffer DallWe currently initialize the arch timer IRQ numbers from the reset code, presumably because we once intended to model multiple CPU or SoC types from within the kernel and have hard-coded reset values in the reset code.
As we are moving towards userspace being in charge of more fine-grained CPU emulation and stitching together the pieces needed to emulate a particular type of CPU, we should no longer have a tight coupling between resetting a VCPU and setting IRQ numbers.
Therefore, move the logic to define and use the default IRQ numbers to the timer code and set the IRQ number immediately when creating the VCPU.
--- arch/arm/kvm/reset.c | 16 +--------------- arch/arm64/kvm/reset.c | 16 +--------------- include/kvm/arm_arch_timer.h | 4 +--- virt/kvm/arm/arch_timer.c | 28 ++++++++++++++++------------ 4 files changed, 19 insertions(+), 45 deletions(-)
arch/arm/kvm/reset.c +1 -15
@@ -37,16 +37,6 @@ static struct kvm_regs cortexa_regs_reset = { .usr_regs.ARM_cpsr = SVC_MODE | PSR_A_BIT | PSR_I_BIT | PSR_F_BIT, }; -static const struct kvm_irq_level cortexa_ptimer_irq = { - { .irq =30 }, - .level =1 , -}; - -static const struct kvm_irq_level cortexa_vtimer_irq = { - { .irq =27 }, - .level =1 , -}; -/******************************************************************************* * Exported reset function
@@ -62,16 +52,12 @@ static const struct kvm_irq_level cortexa_vtimer_irq = {int kvm_reset_vcpu (struct kvm_vcpu *vcpu) {struct kvm_regs *reset_regs; -const struct kvm_irq_level *cpu_vtimer_irq; -const struct kvm_irq_level *cpu_ptimer_irq;switch (vcpu->arch.target) {case KVM_ARM_TARGET_CORTEX_A7 :case KVM_ARM_TARGET_CORTEX_A15 : reset_regs = &cortexa_regs_reset; vcpu->arch.midr =read_cpuid_id (); - cpu_vtimer_irq = &cortexa_vtimer_irq; - cpu_ptimer_irq = &cortexa_ptimer_irq;break ;default :return -ENODEV ;
@@ -84,5 +70,5 @@ int kvm_reset_vcpu(struct kvm_vcpu *vcpu)kvm_reset_coprocs (vcpu);/* Reset arch_timer context */ -return kvm_timer_vcpu_reset (vcpu, cpu_vtimer_irq, cpu_ptimer_irq); +return kvm_timer_vcpu_reset (vcpu); }
arch/arm64/kvm/reset.c +1 -15
@@ -46,16 +46,6 @@ static const struct kvm_regs default_regs_reset32 = { COMPAT_PSR_I_BIT | COMPAT_PSR_F_BIT), }; -static const struct kvm_irq_level default_ptimer_irq = { - .irq =30 , - .level =1 , -}; - -static const struct kvm_irq_level default_vtimer_irq = { - .irq =27 , - .level =1 , -}; -static bool cpu_has_32bit_el1 (void ) {u64 pfr0;
@@ -108,8 +98,6 @@ int kvm_arch_dev_ioctl_check_extension(struct kvm *kvm, long ext)*/ int kvm_reset_vcpu (struct kvm_vcpu *vcpu) { -const struct kvm_irq_level *cpu_vtimer_irq; -const struct kvm_irq_level *cpu_ptimer_irq;const struct kvm_regs *cpu_reset;switch (vcpu->arch.target) {
@@ -122,8 +110,6 @@ int kvm_reset_vcpu(struct kvm_vcpu *vcpu) cpu_reset = &default_regs_reset; } - cpu_vtimer_irq = &default_vtimer_irq; - cpu_ptimer_irq = &default_ptimer_irq;break ; }
@@ -137,5 +123,5 @@ int kvm_reset_vcpu(struct kvm_vcpu *vcpu)kvm_pmu_vcpu_reset (vcpu);/* Reset timer */ -return kvm_timer_vcpu_reset (vcpu, cpu_vtimer_irq, cpu_ptimer_irq); +return kvm_timer_vcpu_reset (vcpu); }
include/kvm/arm_arch_timer.h +1 -3
@@ -57,9 +57,7 @@ struct arch_timer_cpu {int kvm_timer_hyp_init (void );int kvm_timer_enable (struct kvm_vcpu *vcpu); -int kvm_timer_vcpu_reset (struct kvm_vcpu *vcpu, -const struct kvm_irq_level *virt_irq, -const struct kvm_irq_level *phys_irq); +int kvm_timer_vcpu_reset (struct kvm_vcpu *vcpu);void kvm_timer_vcpu_init (struct kvm_vcpu *vcpu);void kvm_timer_flush_hwstate (struct kvm_vcpu *vcpu);void kvm_timer_sync_hwstate (struct kvm_vcpu *vcpu);
virt/kvm/arm/arch_timer.c +16 -12
@@ -35,6 +35,16 @@ static struct timecounter *timecounter;static unsigned int host_vtimer_irq;static u32 host_vtimer_irq_flags; +static const struct kvm_irq_level default_ptimer_irq = { + .irq =30 , + .level =1 , +}; + +static const struct kvm_irq_level default_vtimer_irq = { + .irq =27 , + .level =1 , +}; +void kvm_timer_vcpu_put (struct kvm_vcpu *vcpu) {vcpu_vtimer (vcpu)->active_cleared_last = false;
@@ -445,23 +455,12 @@ void kvm_timer_sync_hwstate(struct kvm_vcpu *vcpu)kvm_timer_update_state (vcpu); } -int kvm_timer_vcpu_reset (struct kvm_vcpu *vcpu, -const struct kvm_irq_level *virt_irq, -const struct kvm_irq_level *phys_irq) +int kvm_timer_vcpu_reset (struct kvm_vcpu *vcpu) {struct arch_timer_context *vtimer =vcpu_vtimer (vcpu);struct arch_timer_context *ptimer =vcpu_ptimer (vcpu);/* -* The vcpu timer irq number cannot be determined in -* kvm_timer_vcpu_init() because it is called much before -* kvm_vcpu_set_target(). To handle this, we determine -* vcpu timer irq number when the vcpu is reset. -*/ - vtimer->irq.irq = virt_irq->irq; - ptimer->irq.irq = phys_irq->irq; - -/* * The bits in CNTV_CTL are architecturally reset to UNKNOWN for ARMv8 * and to 0 for ARMv7. We provide an implementation that always * resets the timer to be disabled and unmasked and is compliant with
@@ -496,6 +495,8 @@ static void update_vtimer_cntvoff(struct kvm_vcpu *vcpu, u64 cntvoff)void kvm_timer_vcpu_init (struct kvm_vcpu *vcpu) {struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu; +struct arch_timer_context *vtimer =vcpu_vtimer (vcpu); +struct arch_timer_context *ptimer =vcpu_ptimer (vcpu);/* Synchronize cntvoff across all vtimers of a VM. */ update_vtimer_cntvoff (vcpu,kvm_phys_timer_read ());
@@ -504,6 +505,9 @@ void kvm_timer_vcpu_init(struct kvm_vcpu *vcpu)INIT_WORK (&timer->expired, kvm_timer_inject_irq_work);hrtimer_init (&timer->timer,CLOCK_MONOTONIC ,HRTIMER_MODE_ABS ); timer->timer.function = kvm_timer_expire; + + vtimer->irq.irq = default_vtimer_irq.irq; + ptimer->irq.irq = default_ptimer_irq.irq; }static void kvm_timer_init_interrupt (void *info)
Signature · 1 line
2.9.0
PATCH v3 5/9 KVM: arm/arm64: Allow setting the timer IRQ numbers from userspace
In reply to Christoffer DallFirst we define an ABI using the vcpu devices that lets userspace set the interrupt numbers for the various timers on both the 32-bit and 64-bit KVM/ARM implementations.
Second, we add the definitions for the groups and attributes introduced by the above ABI. (We add the PMU define on the 32-bit side as well for symmetry and it may get used some day.)
Third, we set up the arch-specific vcpu device operation handlers to call into the timer code for anything related to the KVM_ARM_VCPU_TIMER_CTRL group.
Fourth, we implement support for getting and setting the timer interrupt numbers using the above defined ABI in the arch timer code.
Fifth, we introduce error checking upon enabling the arch timer (which is called when first running a VCPU) to check that all VCPUs are configured to use the same PPI for the timer (as mandated by the architecture) and that the virtual and physical timers are not configured to use the same IRQ number.
--- Documentation/virtual/kvm/devices/vcpu.txt | 25 +++++++ arch/arm/include/uapi/asm/kvm.h | 8 +++ arch/arm/kvm/guest.c | 9 +++ arch/arm64/include/uapi/asm/kvm.h | 3 + arch/arm64/kvm/guest.c | 9 +++ include/kvm/arm_arch_timer.h | 4 ++ virt/kvm/arm/arch_timer.c | 104 +++++++++++++++++++++++++++++ 7 files changed, 162 insertions(+)
Documentation/virtual/kvm/devices/vcpu.txt +25 -0
@@ -35,3 +35,28 @@ Returns: -ENODEV: PMUv3 not supported or GIC not initialized Request the initialization of the PMUv3. If using the PMUv3 with an in-kernel virtual GIC implementation, this must be done after initializing the in-kernel irqchip. + + +2. GROUP: KVM_ARM_VCPU_TIMER_CTRL +Architectures: ARM,ARM64 + +2.1. ATTRIBUTE: KVM_ARM_VCPU_TIMER_IRQ_VTIMER +2.2. ATTRIBUTE: KVM_ARM_VCPU_TIMER_IRQ_PTIMER +Parameters: in kvm_device_attr.addr the address for the timer interrupt is a + pointer to an int +Returns: -EINVAL: Invalid timer interrupt number + -EBUSY: One or more VCPUs has already run + +A value describing the architected timer interrupt number when connected to an +in-kernel virtual GIC. These must be a PPI (16 <= intid < 32). Setting the +attribute overrides the default values (see below). + +KVM_ARM_VCPU_TIMER_IRQ_VTIMER: The EL1 virtual timer intid (default: 27) +KVM_ARM_VCPU_TIMER_IRQ_PTIMER: The EL1 physical timer intid (default: 30) + +Setting the same PPI for different timers will prevent the VCPUs from running. +Setting the interrupt number on a VCPU configures all VCPUs created at that +time to use the number provided for a given timer, overwriting any previously +configured values on other VCPUs. Userspace should configure the interrupt +numbers on at least one VCPU after creating all VCPUs and before running any +VCPUs.
arch/arm/include/uapi/asm/kvm.h +8 -0
@@ -203,6 +203,14 @@ struct kvm_arch_memory_slot {#define KVM_DEV_ARM_VGIC_LINE_LEVEL_INTID_MASK 0x3ff#define VGIC_LEVEL_INFO_LINE_LEVEL 0 +/* Device Control API on vcpu fd */ +#define KVM_ARM_VCPU_PMU_V3_CTRL 0 +#define KVM_ARM_VCPU_PMU_V3_IRQ 0 +#define KVM_ARM_VCPU_PMU_V3_INIT 1 +#define KVM_ARM_VCPU_TIMER_CTRL 1 +#define KVM_ARM_VCPU_TIMER_IRQ_VTIMER 0 +#define KVM_ARM_VCPU_TIMER_IRQ_PTIMER 1 +#define KVM_DEV_ARM_VGIC_CTRL_INIT 0#define KVM_DEV_ARM_ITS_SAVE_TABLES 1#define KVM_DEV_ARM_ITS_RESTORE_TABLES 2
arch/arm/kvm/guest.c +9 -0
@@ -308,6 +308,9 @@ int kvm_arm_vcpu_arch_set_attr(struct kvm_vcpu *vcpu,int ret;switch (attr->group) { +case KVM_ARM_VCPU_TIMER_CTRL : + ret =kvm_arm_timer_set_attr (vcpu, attr); +break ;default : ret = -ENXIO ;break ;
@@ -322,6 +325,9 @@ int kvm_arm_vcpu_arch_get_attr(struct kvm_vcpu *vcpu,int ret;switch (attr->group) { +case KVM_ARM_VCPU_TIMER_CTRL : + ret =kvm_arm_timer_get_attr (vcpu, attr); +break ;default : ret = -ENXIO ;break ;
@@ -336,6 +342,9 @@ int kvm_arm_vcpu_arch_has_attr(struct kvm_vcpu *vcpu,int ret;switch (attr->group) { +case KVM_ARM_VCPU_TIMER_CTRL : + ret =kvm_arm_timer_has_attr (vcpu, attr); +break ;default : ret = -ENXIO ;break ;
arch/arm64/include/uapi/asm/kvm.h +3 -0
@@ -232,6 +232,9 @@ struct kvm_arch_memory_slot {#define KVM_ARM_VCPU_PMU_V3_CTRL 0#define KVM_ARM_VCPU_PMU_V3_IRQ 0#define KVM_ARM_VCPU_PMU_V3_INIT 1 +#define KVM_ARM_VCPU_TIMER_CTRL 1 +#define KVM_ARM_VCPU_TIMER_IRQ_VTIMER 0 +#define KVM_ARM_VCPU_TIMER_IRQ_PTIMER 1/* KVM_IRQ_LINE irq field index values */ #define KVM_ARM_IRQ_TYPE_SHIFT 24
arch/arm64/kvm/guest.c +9 -0
@@ -390,6 +390,9 @@ int kvm_arm_vcpu_arch_set_attr(struct kvm_vcpu *vcpu,case KVM_ARM_VCPU_PMU_V3_CTRL : ret =kvm_arm_pmu_v3_set_attr (vcpu, attr);break ; +case KVM_ARM_VCPU_TIMER_CTRL : + ret =kvm_arm_timer_set_attr (vcpu, attr); +break ;default : ret = -ENXIO ;break ;
@@ -407,6 +410,9 @@ int kvm_arm_vcpu_arch_get_attr(struct kvm_vcpu *vcpu,case KVM_ARM_VCPU_PMU_V3_CTRL : ret =kvm_arm_pmu_v3_get_attr (vcpu, attr);break ; +case KVM_ARM_VCPU_TIMER_CTRL : + ret =kvm_arm_timer_get_attr (vcpu, attr); +break ;default : ret = -ENXIO ;break ;
@@ -424,6 +430,9 @@ int kvm_arm_vcpu_arch_has_attr(struct kvm_vcpu *vcpu,case KVM_ARM_VCPU_PMU_V3_CTRL : ret =kvm_arm_pmu_v3_has_attr (vcpu, attr);break ; +case KVM_ARM_VCPU_TIMER_CTRL : + ret =kvm_arm_timer_has_attr (vcpu, attr); +break ;default : ret = -ENXIO ;break ;
include/kvm/arm_arch_timer.h +4 -0
@@ -68,6 +68,10 @@ void kvm_timer_vcpu_terminate(struct kvm_vcpu *vcpu);u64 kvm_arm_timer_get_reg (struct kvm_vcpu *,u64 regid);int kvm_arm_timer_set_reg (struct kvm_vcpu *,u64 regid,u64 value); +int kvm_arm_timer_set_attr (struct kvm_vcpu *vcpu,struct kvm_device_attr *attr); +int kvm_arm_timer_get_attr (struct kvm_vcpu *vcpu,struct kvm_device_attr *attr); +int kvm_arm_timer_has_attr (struct kvm_vcpu *vcpu,struct kvm_device_attr *attr); +bool kvm_timer_should_fire (struct arch_timer_context *timer_ctx);void kvm_timer_schedule (struct kvm_vcpu *vcpu);void kvm_timer_unschedule (struct kvm_vcpu *vcpu);
virt/kvm/arm/arch_timer.c +104 -0
125 lines not on this page · raw message
Signature · 1 line
2.9.0
PATCH v3 6/9 KVM: arm/arm64: Introduce an allocator for in-kernel irq lines
In reply to Christoffer DallHaving multiple devices being able to signal the same interrupt line is very confusing and almost certainly guarantees a configuration error.
Therefore, introduce a very simple allocator which allows a device to claim an interrupt line from the vgic for a given VM.
--- include/kvm/arm_vgic.h | 5 +++++ virt/kvm/arm/vgic/vgic.c | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+)
include/kvm/arm_vgic.h +5 -0
@@ -121,6 +121,9 @@ struct vgic_irq {u8 source;/* GICv2 SGIs only */ u8 priority;enum vgic_irq_config config;/* Level or edge */ + +void *owner;/* Opaque pointer to reserve an interrupt +for in-kernel devices. */ };struct vgic_register_region ;
@@ -340,4 +343,6 @@ int kvm_send_userspace_msi(struct kvm *kvm, struct kvm_msi *msi);*/ int kvm_vgic_setup_default_irq_routing (struct kvm *kvm); +int kvm_vgic_set_owner (struct kvm_vcpu *vcpu,unsigned int intid,void *owner); +#endif /* __KVM_ARM_VGIC_H */
virt/kvm/arm/vgic/vgic.c +33 -0
@@ -435,6 +435,39 @@ int kvm_vgic_unmap_phys_irq(struct kvm_vcpu *vcpu, unsigned int virt_irq) }/** +* kvm_vgic_set_owner - Set the owner of an interrupt for a VM +* +* @vcpu: Pointer to the VCPU (used for PPIs) +* @intid: The virtual INTID identifying the interrupt (PPI or SPI) +* @owner: Opaque pointer to the owner +* +* Returns 0 if intid is not already used by another in-kernel device and the +* owner is set, otherwise returns an error code. +*/ +int kvm_vgic_set_owner (struct kvm_vcpu *vcpu,unsigned int intid,void *owner) +{ +struct vgic_irq *irq; +int ret =0 ; + +if (!vgic_initialized (vcpu->kvm)) +return -EAGAIN ; + +/* SGIs and LPIs cannot be wired up to any device */ +if (!irq_is_ppi (intid) && !vgic_valid_spi (vcpu->kvm, intid)) +return -EINVAL ; + + irq =vgic_get_irq (vcpu->kvm, vcpu, intid); +spin_lock (&irq->irq_lock); +if (irq->owner && irq->owner != owner) + ret = -EEXIST ; +else + irq->owner = owner; +spin_unlock (&irq->irq_lock); + +return ret; +} + +/** * vgic_prune_ap_list - Remove non-relevant interrupts from the list * * @vcpu: The VCPU pointer
Signature · 1 line
2.9.0
PATCH v3 7/9 KVM: arm/arm64: Check if irq lines to the GIC are already used
In reply to Christoffer DallWe check if other in-kernel devices have already been connected to the GIC for a particular interrupt line when possible.
For the PMU, we can do this whenever setting the PMU interrupt number from userspace.
For the timers, we have to wait until we try to enable the timer, because we have a concept of default IRQ numbers that userspace shouldn't have to work around in the initialization phase.
--- virt/kvm/arm/arch_timer.c | 18 ++++++++++-------- virt/kvm/arm/pmu.c | 7 +++++++ 2 files changed, 17 insertions(+), 8 deletions(-)
virt/kvm/arm/arch_timer.c +10 -8
@@ -618,20 +618,22 @@ void kvm_timer_vcpu_terminate(struct kvm_vcpu *vcpu)kvm_vgic_unmap_phys_irq (vcpu, vtimer->irq.irq); } -static bool timer_irqs_are_valid (struct kvm *kvm) +static bool timer_irqs_are_valid (struct kvm_vcpu *vcpu) { -struct kvm_vcpu *vcpu;int vtimer_irq, ptimer_irq; -int i; +int i, ret; - vcpu =kvm_get_vcpu (kvm,0 ); vtimer_irq =vcpu_vtimer (vcpu)->irq.irq; - ptimer_irq =vcpu_ptimer (vcpu)->irq.irq; + ret =kvm_vgic_set_owner (vcpu, vtimer_irq,vcpu_vtimer (vcpu)); +if (ret) +return false; -if (vtimer_irq == ptimer_irq) + ptimer_irq =vcpu_ptimer (vcpu)->irq.irq; + ret =kvm_vgic_set_owner (vcpu, ptimer_irq,vcpu_ptimer (vcpu)); +if (ret)return false; -kvm_for_each_vcpu (i, vcpu, kvm) { +kvm_for_each_vcpu (i, vcpu, vcpu->kvm) {if (vcpu_vtimer (vcpu)->irq.irq != vtimer_irq ||vcpu_ptimer (vcpu)->irq.irq != ptimer_irq)return false;
@@ -659,7 +661,7 @@ int kvm_timer_enable(struct kvm_vcpu *vcpu)if (!vgic_initialized (vcpu->kvm))return -ENODEV ; -if (!timer_irqs_are_valid (vcpu->kvm)) { +if (!timer_irqs_are_valid (vcpu)) {kvm_debug ("incorrectly configured timer irqs\n" );return -EINVAL ; }
virt/kvm/arm/pmu.c +7 -0
@@ -480,6 +480,8 @@ static int kvm_arm_pmu_v3_init(struct kvm_vcpu *vcpu)return -EBUSY ;if (irqchip_in_kernel (vcpu->kvm)) { +int ret; +/* * If using the PMU with an in-kernel virtual GIC * implementation, we require the GIC to be already
@@ -490,6 +492,11 @@ static int kvm_arm_pmu_v3_init(struct kvm_vcpu *vcpu)if (!kvm_arm_pmu_irq_initialized (vcpu))return -ENXIO ; + + ret =kvm_vgic_set_owner (vcpu, vcpu->arch.pmu.irq_num, + &vcpu->arch.pmu); +if (ret) +return ret; } vcpu->arch.pmu.created = true;
Signature · 1 line
2.9.0
PATCH v3 8/9 KVM: arm/arm64: Disallow userspace control of in-kernel IRQ lines
In reply to Christoffer DallWhen injecting an IRQ to the VGIC, you now have to present an owner token for that IRQ line to show that you are the owner of that line.
IRQ lines driven from userspace or via an irqfd do not have an owner and will simply pass a NULL pointer.
Also get rid of the unused kvm_vgic_inject_mapped_irq prototype.
--- include/kvm/arm_vgic.h | 4 +--- virt/kvm/arm/arch_timer.c | 3 ++- virt/kvm/arm/arm.c | 4 ++-- virt/kvm/arm/pmu.c | 3 ++- virt/kvm/arm/vgic/vgic-irqfd.c | 2 +- virt/kvm/arm/vgic/vgic.c | 15 +++++++++++---- 6 files changed, 19 insertions(+), 12 deletions(-)
include/kvm/arm_vgic.h +1 -3
@@ -300,9 +300,7 @@ int kvm_vgic_hyp_init(void);void kvm_vgic_init_cpu_hardware (void );int kvm_vgic_inject_irq (struct kvm *kvm,int cpuid,unsigned int intid, -bool level); -int kvm_vgic_inject_mapped_irq (struct kvm *kvm,int cpuid,unsigned int intid, -bool level); +bool level,void *owner);int kvm_vgic_map_phys_irq (struct kvm_vcpu *vcpu,u32 virt_irq,u32 phys_irq);int kvm_vgic_unmap_phys_irq (struct kvm_vcpu *vcpu,unsigned int virt_irq);bool kvm_vgic_map_is_active (struct kvm_vcpu *vcpu,unsigned int virt_irq);
virt/kvm/arm/arch_timer.c +2 -1
@@ -226,7 +226,8 @@ static void kvm_timer_update_irq(struct kvm_vcpu *vcpu, bool new_level,if (likely (irqchip_in_kernel (vcpu->kvm))) { ret =kvm_vgic_inject_irq (vcpu->kvm, vcpu->vcpu_id, timer_ctx->irq.irq, - timer_ctx->irq.level); + timer_ctx->irq.level, + timer_ctx);WARN_ON (ret); } }
virt/kvm/arm/arm.c +2 -2
@@ -832,7 +832,7 @@ int kvm_vm_ioctl_irq_line(struct kvm *kvm, struct kvm_irq_level *irq_level,if (irq_num <VGIC_NR_SGIS || irq_num >=VGIC_NR_PRIVATE_IRQS )return -EINVAL ; -return kvm_vgic_inject_irq (kvm, vcpu->vcpu_id, irq_num, level); +return kvm_vgic_inject_irq (kvm, vcpu->vcpu_id, irq_num, level,NULL );case KVM_ARM_IRQ_TYPE_SPI :if (!irqchip_in_kernel (kvm))return -ENXIO ;
@@ -840,7 +840,7 @@ int kvm_vm_ioctl_irq_line(struct kvm *kvm, struct kvm_irq_level *irq_level,if (irq_num <VGIC_NR_PRIVATE_IRQS )return -EINVAL ; -return kvm_vgic_inject_irq (kvm,0 , irq_num, level); +return kvm_vgic_inject_irq (kvm,0 , irq_num, level,NULL ); }return -EINVAL ;
virt/kvm/arm/pmu.c +2 -1
@@ -215,7 +215,8 @@ static void kvm_pmu_check_overflow(struct kvm_vcpu *vcpu)if (likely (irqchip_in_kernel (vcpu->kvm))) {int ret =kvm_vgic_inject_irq (vcpu->kvm, vcpu->vcpu_id, - pmu->irq_num, overflow); + pmu->irq_num, overflow, + vcpu->arch.pmu);WARN_ON (ret); } }
virt/kvm/arm/vgic/vgic-irqfd.c +1 -1
@@ -34,7 +34,7 @@ static int vgic_irqfd_set_irq(struct kvm_kernel_irq_routing_entry *e,if (!vgic_valid_spi (kvm, spi_id))return -EINVAL ; -return kvm_vgic_inject_irq (kvm,0 , spi_id, level); +return kvm_vgic_inject_irq (kvm,0 , spi_id, level,NULL ); }/**
virt/kvm/arm/vgic/vgic.c +11 -4
@@ -235,10 +235,14 @@ static void vgic_sort_ap_list(struct kvm_vcpu *vcpu)/* * Only valid injection if changing level for level-triggered IRQs or for a -* rising edge. +* rising edge, and in-kernel connected IRQ lines can only be controlled by +* their owner. */ -static bool vgic_validate_injection (struct vgic_irq *irq,bool level) +static bool vgic_validate_injection (struct vgic_irq *irq,bool level,void *owner) { +if (irq->owner != owner) +return false; +switch (irq->config) {case VGIC_CONFIG_LEVEL :return irq->line_level != level;
@@ -350,13 +354,16 @@ bool vgic_queue_irq_unlock(struct kvm *kvm, struct vgic_irq *irq)* false: to ignore the call * Level-sensitive true: raise the input signal * false: lower the input signal +* @owner: The opaque pointer to the owner of the IRQ being raised to verify +* that the caller is allowed to inject this IRQ. Userspace +* injections will have owner == NULL. * * The VGIC is not concerned with devices being active-LOW or active-HIGH for * level-sensitive interrupts. You can think of the level parameter as 1 * being HIGH and 0 being LOW and all devices being active-HIGH. */ int kvm_vgic_inject_irq (struct kvm *kvm,int cpuid,unsigned int intid, -bool level) +bool level,void *owner) {struct kvm_vcpu *vcpu;struct vgic_irq *irq;
@@ -378,7 +385,7 @@ int kvm_vgic_inject_irq(struct kvm *kvm, int cpuid, unsigned int intid,spin_lock (&irq->irq_lock); -if (!vgic_validate_injection (irq, level)) { +if (!vgic_validate_injection (irq, level, owner)) {/* Nothing to see here, move along... */ spin_unlock (&irq->irq_lock);vgic_put_irq (kvm, irq);
Signature · 1 line
2.9.0
PATCH v3 9/9 KVM: arm/arm64: Don't assume initialized vgic when setting PMU IRQ
In reply to Christoffer DallThe 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.
--- include/kvm/arm_vgic.h | 2 ++ virt/kvm/arm/pmu.c | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-)
include/kvm/arm_vgic.h +2 -0
@@ -39,6 +39,8 @@#define KVM_IRQCHIP_NUM_PINS (1020 - 32)#define irq_is_ppi (irq) ((irq) >= VGIC_NR_SGIS && (irq) < VGIC_NR_PRIVATE_IRQS) +#define irq_is_spi (irq) ((irq) >= VGIC_NR_PRIVATE_IRQS && \ + (irq) <= VGIC_MAX_SPI)enum vgic_type {VGIC_V2 ,/* Good ol' GICv2 */
virt/kvm/arm/pmu.c +1 -1
@@ -547,7 +547,7 @@ int kvm_arm_pmu_v3_set_attr(struct kvm_vcpu *vcpu, struct kvm_device_attr *attr)return -EFAULT ;/* The PMU overflow interrupt can be a PPI or a valid SPI. */ -if (!(irq_is_ppi (irq) ||vgic_valid_spi (vcpu->kvm, irq))) +if (!(irq_is_ppi (irq) ||irq_is_spi (irq)))return -EINVAL ;if (!pmu_irq_is_valid (vcpu->kvm, irq))
Signature · 1 line
2.9.0
PATCH v3 3/9 KVM: arm/arm64: Move irq_is_ppi() to header file
In reply to Christoffer DallWe are about to need this define in the arch timer code as well so move it to a common location.
--- include/kvm/arm_vgic.h | 2 ++ virt/kvm/arm/pmu.c | 2 -- 2 files changed, 2 insertions(+), 2 deletions(-)
include/kvm/arm_vgic.h +2 -0
@@ -38,6 +38,8 @@#define VGIC_MIN_LPI 8192#define KVM_IRQCHIP_NUM_PINS (1020 - 32) +#define irq_is_ppi (irq) ((irq) >= VGIC_NR_SGIS && (irq) < VGIC_NR_PRIVATE_IRQS) +enum vgic_type {VGIC_V2 ,/* Good ol' GICv2 */ VGIC_V3 ,/* New fancy GICv3 */
virt/kvm/arm/pmu.c +0 -2
@@ -496,8 +496,6 @@ static int kvm_arm_pmu_v3_init(struct kvm_vcpu *vcpu)return 0 ; } -#define irq_is_ppi (irq) ((irq) >= VGIC_NR_SGIS && (irq) < VGIC_NR_PRIVATE_IRQS) -/* * For one VM the interrupt type must be same for each vcpu. * As a PPI, the interrupt number is the same for all vcpus,
Signature · 1 line
2.9.0
Re: [PATCH v3 1/9] KVM: arm64: Allow creating the PMU without the in-kernel GIC
In reply to Christoffer DallQuoted 7 lines · Christoffer Dall
Since we got support for devices in userspace which allows reporting the PMU overflow output status to userspace, we should actually allow creating the PMU on systems without an in-kernel irqchip, which in turn requires us to slightly clarify error codes for the ABI and move things around for the initialization phase.
Signed-off-by Christoffer Dall <[email protected]>
M.
Signature · 1 line
Jazz is not dead, it just smell funny.
Re: [PATCH v3 6/9] KVM: arm/arm64: Introduce an allocator for in-kernel irq lines
In reply to Christoffer DallQuoted 58 lines · Christoffer Dall
Having multiple devices being able to signal the same interrupt line is very confusing and almost certainly guarantees a configuration error.
Therefore, introduce a very simple allocator which allows a device to claim an interrupt line from the vgic for a given VM.
Signed-off-by Christoffer Dall <[email protected]>--- include/kvm/arm_vgic.h | 5 +++++ virt/kvm/arm/vgic/vgic.c | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+)include/kvm/arm_vgic.h +5
-0@@ -121,6 +121,9 @@ struct vgic_irq {u8 source;/* GICv2 SGIs only */ u8 priority;enum vgic_irq_config config;/* Level or edge */ + +void *owner;/* Opaque pointer to reserve an interrupt +for in-kernel devices. */ };struct vgic_register_region ;@@ -340,4 +343,6 @@ int kvm_send_userspace_msi(struct kvm *kvm, struct kvm_msi *msi);*/ int kvm_vgic_setup_default_irq_routing (struct kvm *kvm); +int kvm_vgic_set_owner (struct kvm_vcpu *vcpu,unsigned int intid,void *owner); +#endif /* __KVM_ARM_VGIC_H */ virt/kvm/arm/vgic/vgic.c +17
-0@@ -435,6 +435,39 @@ int kvm_vgic_unmap_phys_irq(struct kvm_vcpu *vcpu, unsigned int virt_irq) }/** +* kvm_vgic_set_owner - Set the owner of an interrupt for a VM +* +* @vcpu: Pointer to the VCPU (used for PPIs) +* @intid: The virtual INTID identifying the interrupt (PPI or SPI) +* @owner: Opaque pointer to the owner +* +* Returns 0 if intid is not already used by another in-kernel device and the +* owner is set, otherwise returns an error code. +*/ +int kvm_vgic_set_owner (struct kvm_vcpu *vcpu,unsigned int intid,void *owner) +{ +struct vgic_irq *irq; +int ret =0 ; + +if (!vgic_initialized (vcpu->kvm)) +return -EAGAIN ; +
virt/kvm/arm/vgic/vgic.c int kvm_vgic_unmap_phys_irq(struct kvm_vcpu *vcpu, unsigned int virt_irq) · line 464
Quoted 10 lines
virt/kvm/arm/vgic/vgic.c +10
-0+/* SGIs and LPIs cannot be wired up to any device */ +if (!irq_is_ppi (intid) && !vgic_valid_spi (vcpu->kvm, intid)) +return -EINVAL ; + + irq =vgic_get_irq (vcpu->kvm, vcpu, intid); +spin_lock (&irq->irq_lock); +if (irq->owner && irq->owner != owner) + ret = -EEXIST ; +else + irq->owner = owner;
Is there any case where you'd end-up setting the owner multiple times? Or is that just convenient?
virt/kvm/arm/vgic/vgic.c int kvm_vgic_unmap_phys_irq(struct kvm_vcpu *vcpu, unsigned int virt_irq) · line 473
Quoted 9 lines
virt/kvm/arm/vgic/vgic.c +6
-0+spin_unlock (&irq->irq_lock); + +return ret; +} + +/** * vgic_prune_ap_list - Remove non-relevant interrupts from the list * * @vcpu: The VCPU pointer
Otherwise,
M.
Signature · 1 line
Jazz is not dead, it just smell funny.
Re: [PATCH v3 7/9] KVM: arm/arm64: Check if irq lines to the GIC are already used
In reply to Christoffer DallQuoted 74 lines · Christoffer Dall
We check if other in-kernel devices have already been connected to the GIC for a particular interrupt line when possible.
For the PMU, we can do this whenever setting the PMU interrupt number from userspace.
For the timers, we have to wait until we try to enable the timer, because we have a concept of default IRQ numbers that userspace shouldn't have to work around in the initialization phase.
Signed-off-by Christoffer Dall <[email protected]>--- virt/kvm/arm/arch_timer.c | 18 ++++++++++-------- virt/kvm/arm/pmu.c | 7 +++++++ 2 files changed, 17 insertions(+), 8 deletions(-)virt/kvm/arm/arch_timer.c +10
-8@@ -618,20 +618,22 @@ void kvm_timer_vcpu_terminate(struct kvm_vcpu *vcpu)kvm_vgic_unmap_phys_irq (vcpu, vtimer->irq.irq); } -static bool timer_irqs_are_valid (struct kvm *kvm) +static bool timer_irqs_are_valid (struct kvm_vcpu *vcpu) { -struct kvm_vcpu *vcpu;int vtimer_irq, ptimer_irq; -int i; +int i, ret; - vcpu =kvm_get_vcpu (kvm,0 ); vtimer_irq =vcpu_vtimer (vcpu)->irq.irq; - ptimer_irq =vcpu_ptimer (vcpu)->irq.irq; + ret =kvm_vgic_set_owner (vcpu, vtimer_irq,vcpu_vtimer (vcpu)); +if (ret) +return false; -if (vtimer_irq == ptimer_irq) + ptimer_irq =vcpu_ptimer (vcpu)->irq.irq; + ret =kvm_vgic_set_owner (vcpu, ptimer_irq,vcpu_ptimer (vcpu)); +if (ret)return false; -kvm_for_each_vcpu (i, vcpu, kvm) { +kvm_for_each_vcpu (i, vcpu, vcpu->kvm) {if (vcpu_vtimer (vcpu)->irq.irq != vtimer_irq ||vcpu_ptimer (vcpu)->irq.irq != ptimer_irq)return false;@@ -659,7 +661,7 @@ int kvm_timer_enable(struct kvm_vcpu *vcpu)if (!vgic_initialized (vcpu->kvm))return -ENODEV ; -if (!timer_irqs_are_valid (vcpu->kvm)) { +if (!timer_irqs_are_valid (vcpu)) {kvm_debug ("incorrectly configured timer irqs\n" );return -EINVAL ; }virt/kvm/arm/pmu.c +2
-0@@ -480,6 +480,8 @@ static int kvm_arm_pmu_v3_init(struct kvm_vcpu *vcpu)return -EBUSY ;if (irqchip_in_kernel (vcpu->kvm)) { +int ret; +/* * If using the PMU with an in-kernel virtual GIC * implementation, we require the GIC to be already @@ -490,6 +492,11 @@ static int kvm_arm_pmu_v3_init(struct kvm_vcpu *vcpu)
virt/kvm/arm/pmu.c static int kvm_arm_pmu_v3_init(struct kvm_vcpu *vcpu) · line 502
Quoted 10 lines
virt/kvm/arm/pmu.c +5
-0if (!kvm_arm_pmu_irq_initialized (vcpu))return -ENXIO ; + + ret =kvm_vgic_set_owner (vcpu, vcpu->arch.pmu.irq_num, + &vcpu->arch.pmu); +if (ret) +return ret; } vcpu->arch.pmu.created = true;
M.
Signature · 1 line
Jazz is not dead, it just smell funny.
Re: [PATCH v3 8/9] KVM: arm/arm64: Disallow userspace control of in-kernel IRQ lines
In reply to Christoffer DallQuoted 9 lines · Christoffer Dall
When injecting an IRQ to the VGIC, you now have to present an owner token for that IRQ line to show that you are the owner of that line.
IRQ lines driven from userspace or via an irqfd do not have an owner and will simply pass a NULL pointer.
Also get rid of the unused
kvm_vgic_inject_mapped_irqprototype.Signed-off-by Christoffer Dall <[email protected]>
M.
Signature · 1 line
Jazz is not dead, it just smell funny.
Re: [PATCH v3 9/9] KVM: arm/arm64: Don't assume initialized vgic when setting PMU IRQ
In reply to Christoffer DallQuoted 33 lines · Christoffer Dall
The PMU IRQ number is set through the VCPU device's
KVM_SET_DEVICE_ATTRioctl handler for theKVM_ARM_VCPU_PMU_V3_IRQattribute, but there is no enforced or stated requirement that this must happen after initializing the VGIC. As a result, callingvgic_valid_spi()which relies on thenr_spisbeing 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 <[email protected]>--- include/kvm/arm_vgic.h | 2 ++ virt/kvm/arm/pmu.c | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-)include/kvm/arm_vgic.h +2
-0@@ -39,6 +39,8 @@#define KVM_IRQCHIP_NUM_PINS (1020 - 32)#define irq_is_ppi (irq) ((irq) >= VGIC_NR_SGIS && (irq) < VGIC_NR_PRIVATE_IRQS) +#define irq_is_spi (irq) ((irq) >= VGIC_NR_PRIVATE_IRQS && \ + (irq) <= VGIC_MAX_SPI)enum vgic_type {VGIC_V2 ,/* Good ol' GICv2 */ virt/kvm/arm/pmu.c +0
-0
virt/kvm/arm/pmu.c int kvm_arm_pmu_v3_set_attr(struct kvm_vcpu *vcpu, struct kvm_device_attr *attr) · line 553
Quoted 8 lines
virt/kvm/arm/pmu.c +1
-1@@ -547,7 +547,7 @@ int kvm_arm_pmu_v3_set_attr(struct kvm_vcpu *vcpu, struct kvm_device_attr *attr)return -EFAULT ;/* The PMU overflow interrupt can be a PPI or a valid SPI. */ -if (!(irq_is_ppi (irq) ||vgic_valid_spi (vcpu->kvm, irq))) +if (!(irq_is_ppi (irq) ||irq_is_spi (irq)))return -EINVAL ;if (!pmu_irq_is_valid (vcpu->kvm, irq))
Does it mean that we can now fail an injection if the SPI is out of the range of configured SPIs?
If that's the case, the WARN_ON() in kvm_pmu_update_state() is going to fire badly, and that's going to be ugly. Should we add a check for this case in kvm_arm_pmu_v3_init()?
Thanks,
M.
Signature · 1 line
Jazz is not dead, it just smell funny.
Re: [PATCH v3 6/9] KVM: arm/arm64: Introduce an allocator for in-kernel irq lines
In reply to Marc ZyngierQuoted 64 lines · Marc Zyngier
Quoted 63 lines · Christoffer Dall
Having multiple devices being able to signal the same interrupt line is very confusing and almost certainly guarantees a configuration error.
Therefore, introduce a very simple allocator which allows a device to claim an interrupt line from the vgic for a given VM.
Signed-off-by Christoffer Dall <[email protected]>--- include/kvm/arm_vgic.h | 5 +++++ virt/kvm/arm/vgic/vgic.c | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+)include/kvm/arm_vgic.h +5
-0@@ -121,6 +121,9 @@ struct vgic_irq {u8 source;/* GICv2 SGIs only */ u8 priority;enum vgic_irq_config config;/* Level or edge */ + +void *owner;/* Opaque pointer to reserve an interrupt +for in-kernel devices. */ };struct vgic_register_region ;@@ -340,4 +343,6 @@ int kvm_send_userspace_msi(struct kvm *kvm, struct kvm_msi *msi);*/ int kvm_vgic_setup_default_irq_routing (struct kvm *kvm); +int kvm_vgic_set_owner (struct kvm_vcpu *vcpu,unsigned int intid,void *owner); +#endif /* __KVM_ARM_VGIC_H */ virt/kvm/arm/vgic/vgic.c +22
-0@@ -435,6 +435,39 @@ int kvm_vgic_unmap_phys_irq(struct kvm_vcpu *vcpu, unsigned int virt_irq) }/** +* kvm_vgic_set_owner - Set the owner of an interrupt for a VM +* +* @vcpu: Pointer to the VCPU (used for PPIs) +* @intid: The virtual INTID identifying the interrupt (PPI or SPI) +* @owner: Opaque pointer to the owner +* +* Returns 0 if intid is not already used by another in-kernel device and the +* owner is set, otherwise returns an error code. +*/ +int kvm_vgic_set_owner (struct kvm_vcpu *vcpu,unsigned int intid,void *owner) +{ +struct vgic_irq *irq; +int ret =0 ; + +if (!vgic_initialized (vcpu->kvm)) +return -EAGAIN ; + +/* SGIs and LPIs cannot be wired up to any device */ +if (!irq_is_ppi (intid) && !vgic_valid_spi (vcpu->kvm, intid)) +return -EINVAL ; + + irq =vgic_get_irq (vcpu->kvm, vcpu, intid);
Quoted 9 lines
virt/kvm/arm/vgic/vgic.c int kvm_vgic_unmap_phys_irq(struct kvm_vcpu *vcpu, unsigned int virt_irq) · line 464
Quoted 5 lines
virt/kvm/arm/vgic/vgic.c +5
-0+spin_lock (&irq->irq_lock); +if (irq->owner && irq->owner != owner) + ret = -EEXIST ; +else + irq->owner = owner;Is there any case where you'd end-up setting the owner multiple times? Or is that just convenient?
I think the timer code ended up doing that in some sequence, but maybe not. In any case, I'm not sure if it makes much of a difference?
Quoted 12 lines
virt/kvm/arm/vgic/vgic.c int kvm_vgic_unmap_phys_irq(struct kvm_vcpu *vcpu, unsigned int virt_irq) · line 473
Quoted 9 lines
virt/kvm/arm/vgic/vgic.c +6
-0+spin_unlock (&irq->irq_lock); + +return ret; +} + +/** * vgic_prune_ap_list - Remove non-relevant interrupts from the list * * @vcpu: The VCPU pointer Otherwise,
Quoted 2 lines
Acked-by Marc Zyngier <[email protected]>
Thanks,
-Christoffer
Re: [PATCH v3 6/9] KVM: arm/arm64: Introduce an allocator for in-kernel irq lines
Quoted 75 lines · Christoffer Dall
Quoted 64 lines · Marc Zyngier
Quoted 63 lines · Christoffer Dall
Having multiple devices being able to signal the same interrupt line is very confusing and almost certainly guarantees a configuration error.
Therefore, introduce a very simple allocator which allows a device to claim an interrupt line from the vgic for a given VM.
Signed-off-by Christoffer Dall <[email protected]>--- include/kvm/arm_vgic.h | 5 +++++ virt/kvm/arm/vgic/vgic.c | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+)include/kvm/arm_vgic.h +5
-0@@ -121,6 +121,9 @@ struct vgic_irq {u8 source;/* GICv2 SGIs only */ u8 priority;enum vgic_irq_config config;/* Level or edge */ + +void *owner;/* Opaque pointer to reserve an interrupt +for in-kernel devices. */ };struct vgic_register_region ;@@ -340,4 +343,6 @@ int kvm_send_userspace_msi(struct kvm *kvm, struct kvm_msi *msi);*/ int kvm_vgic_setup_default_irq_routing (struct kvm *kvm); +int kvm_vgic_set_owner (struct kvm_vcpu *vcpu,unsigned int intid,void *owner); +#endif /* __KVM_ARM_VGIC_H */ virt/kvm/arm/vgic/vgic.c +22
-0@@ -435,6 +435,39 @@ int kvm_vgic_unmap_phys_irq(struct kvm_vcpu *vcpu, unsigned int virt_irq) }/** +* kvm_vgic_set_owner - Set the owner of an interrupt for a VM +* +* @vcpu: Pointer to the VCPU (used for PPIs) +* @intid: The virtual INTID identifying the interrupt (PPI or SPI) +* @owner: Opaque pointer to the owner +* +* Returns 0 if intid is not already used by another in-kernel device and the +* owner is set, otherwise returns an error code. +*/ +int kvm_vgic_set_owner (struct kvm_vcpu *vcpu,unsigned int intid,void *owner) +{ +struct vgic_irq *irq; +int ret =0 ; + +if (!vgic_initialized (vcpu->kvm)) +return -EAGAIN ; + +/* SGIs and LPIs cannot be wired up to any device */ +if (!irq_is_ppi (intid) && !vgic_valid_spi (vcpu->kvm, intid)) +return -EINVAL ; + + irq =vgic_get_irq (vcpu->kvm, vcpu, intid);Quoted 9 lines
virt/kvm/arm/vgic/vgic.c int kvm_vgic_unmap_phys_irq(struct kvm_vcpu *vcpu, unsigned int virt_irq) · line 464
Quoted 5 lines
virt/kvm/arm/vgic/vgic.c +5
-0+spin_lock (&irq->irq_lock); +if (irq->owner && irq->owner != owner) + ret = -EEXIST ; +else + irq->owner = owner;Is there any case where you'd end-up setting the owner multiple times? Or is that just convenient?
Quoted 2 lines
I think the timer code ended up doing that in some sequence, but maybe not. In any case, I'm not sure if it makes much of a difference?
None whatsoever.
Thanks,
M.
Signature · 1 line
Jazz is not dead. It just smells funny...
Re: [PATCH v3 9/9] KVM: arm/arm64: Don't assume initialized vgic when setting PMU IRQ
In reply to Marc ZyngierQuoted 41 lines · Marc Zyngier
Quoted 40 lines · Christoffer Dall
The PMU IRQ number is set through the VCPU device's
KVM_SET_DEVICE_ATTRioctl handler for theKVM_ARM_VCPU_PMU_V3_IRQattribute, but there is no enforced or stated requirement that this must happen after initializing the VGIC. As a result, callingvgic_valid_spi()which relies on thenr_spisbeing 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 <[email protected]>--- include/kvm/arm_vgic.h | 2 ++ virt/kvm/arm/pmu.c | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-)include/kvm/arm_vgic.h +2
-0@@ -39,6 +39,8 @@#define KVM_IRQCHIP_NUM_PINS (1020 - 32)#define irq_is_ppi (irq) ((irq) >= VGIC_NR_SGIS && (irq) < VGIC_NR_PRIVATE_IRQS) +#define irq_is_spi (irq) ((irq) >= VGIC_NR_PRIVATE_IRQS && \ + (irq) <= VGIC_MAX_SPI)enum vgic_type {VGIC_V2 ,/* Good ol' GICv2 */ virt/kvm/arm/pmu.c +1
-1@@ -547,7 +547,7 @@ int kvm_arm_pmu_v3_set_attr(struct kvm_vcpu *vcpu, struct kvm_device_attr *attr)return -EFAULT ;/* The PMU overflow interrupt can be a PPI or a valid SPI. */ -if (!(irq_is_ppi (irq) ||vgic_valid_spi (vcpu->kvm, irq))) +if (!(irq_is_ppi (irq) ||irq_is_spi (irq)))return -EINVAL ;
Quoted 9 lines
virt/kvm/arm/pmu.c int kvm_arm_pmu_v3_set_attr(struct kvm_vcpu *vcpu, struct kvm_device_attr *attr) · line 553
Quoted 1 line
virt/kvm/arm/pmu.c +0
-0if (!pmu_irq_is_valid (vcpu->kvm, irq))Does it mean that we can now fail an injection if the SPI is out of the range of configured SPIs?
If that's the case, the
WARN_ON()inkvm_pmu_update_state()is going to fire badly, and that's going to be ugly. Should we add a check for this case inkvm_arm_pmu_v3_init()?
Yes, we should. How about this fixup?
virt/kvm/arm/pmu.c +17 -3
@@ -458,10 +458,24 @@ int kvm_arm_pmu_v3_enable(struct kvm_vcpu *vcpu)/* * A valid interrupt configuration for the PMU is either to have a * properly configured interrupt number and using an in-kernel -* irqchip, or to neither set an IRQ nor create an in-kernel irqchip. +* irqchip, or to not have an in-kernel GIC and not set an IRQ. */ -if (kvm_arm_pmu_irq_initialized (vcpu) !=irqchip_in_kernel (vcpu->kvm)) -return -EINVAL ; +if (irqchip_in_kernel (vcpu->kvm)) { +int irq = vcpu->arch.pmu.irq_num; +if (!kvm_arm_pmu_irq_initialized (vcpu)) +return -EINVAL ; + +/* +* If we are using an in-kernel vgic, at this point we know +* the vgic will be initialized, so we can check the PMU irq +* number against the dimensions of the vgic and make sure +* it's valid. +*/ +if (!irq_is_ppi (irq) && !vgic_valid_spi (vcpu->kvm, irq)) +return -EINVAL ; + }else if (kvm_arm_pmu_irq_initialized (vcpu)) { +return -EINVAL ; + }kvm_pmu_vcpu_reset (vcpu); vcpu->arch.pmu.ready = true;
If you're happy with that, I'll just squash that into the current patch before applying the series.
Thanks,
-Christoffer
Marc Zyngier
Reviewed-byRe: [PATCH v3 9/9] KVM: arm/arm64: Don't assume initialized vgic when setting PMU IRQ
Quoted 89 lines · Christoffer Dall
Quoted 41 lines · Marc Zyngier
Quoted 40 lines · Christoffer Dall
The PMU IRQ number is set through the VCPU device's
KVM_SET_DEVICE_ATTRioctl handler for theKVM_ARM_VCPU_PMU_V3_IRQattribute, but there is no enforced or stated requirement that this must happen after initializing the VGIC. As a result, callingvgic_valid_spi()which relies on thenr_spisbeing 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 <[email protected]>--- include/kvm/arm_vgic.h | 2 ++ virt/kvm/arm/pmu.c | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-)include/kvm/arm_vgic.h +2
-0@@ -39,6 +39,8 @@#define KVM_IRQCHIP_NUM_PINS (1020 - 32)#define irq_is_ppi (irq) ((irq) >= VGIC_NR_SGIS && (irq) < VGIC_NR_PRIVATE_IRQS) +#define irq_is_spi (irq) ((irq) >= VGIC_NR_PRIVATE_IRQS && \ + (irq) <= VGIC_MAX_SPI)enum vgic_type {VGIC_V2 ,/* Good ol' GICv2 */ virt/kvm/arm/pmu.c +1
-1@@ -547,7 +547,7 @@ int kvm_arm_pmu_v3_set_attr(struct kvm_vcpu *vcpu, struct kvm_device_attr *attr)return -EFAULT ;/* The PMU overflow interrupt can be a PPI or a valid SPI. */ -if (!(irq_is_ppi (irq) ||vgic_valid_spi (vcpu->kvm, irq))) +if (!(irq_is_ppi (irq) ||irq_is_spi (irq)))return -EINVAL ;Quoted 9 lines
virt/kvm/arm/pmu.c int kvm_arm_pmu_v3_set_attr(struct kvm_vcpu *vcpu, struct kvm_device_attr *attr) · line 553
Quoted 1 line
virt/kvm/arm/pmu.c +0
-0if (!pmu_irq_is_valid (vcpu->kvm, irq))Does it mean that we can now fail an injection if the SPI is out of the range of configured SPIs?
If that's the case, the
WARN_ON()inkvm_pmu_update_state()is going to fire badly, and that's going to be ugly. Should we add a check for this case inkvm_arm_pmu_v3_init()?Yes, we should. How about this fixup?
virt/kvm/arm/pmu.c +17
-3@@ -458,10 +458,24 @@ int kvm_arm_pmu_v3_enable(struct kvm_vcpu *vcpu)/* * A valid interrupt configuration for the PMU is either to have a * properly configured interrupt number and using an in-kernel -* irqchip, or to neither set an IRQ nor create an in-kernel irqchip. +* irqchip, or to not have an in-kernel GIC and not set an IRQ. */ -if (kvm_arm_pmu_irq_initialized (vcpu) !=irqchip_in_kernel (vcpu->kvm)) -return -EINVAL ; +if (irqchip_in_kernel (vcpu->kvm)) { +int irq = vcpu->arch.pmu.irq_num; +if (!kvm_arm_pmu_irq_initialized (vcpu)) +return -EINVAL ; + +/* +* If we are using an in-kernel vgic, at this point we know +* the vgic will be initialized, so we can check the PMU irq +* number against the dimensions of the vgic and make sure +* it's valid. +*/ +if (!irq_is_ppi (irq) && !vgic_valid_spi (vcpu->kvm, irq)) +return -EINVAL ; + }else if (kvm_arm_pmu_irq_initialized (vcpu)) { +return -EINVAL ; + }kvm_pmu_vcpu_reset (vcpu); vcpu->arch.pmu.ready = true;
Quoted 2 lines
If you're happy with that, I'll just squash that into the current patch before applying the series.
Yup, looks good to me! For the squashed version:
Thanks,
M.
Signature · 1 line
Jazz is not dead. It just smells funny...