ARM: KVM: Enable in-kernel timers with user space gic
PATCH 1/1 ARM: KVM: Enable in-kernel timers with user space gic
When running with KVM enabled, you can choose between emulating the gic in kernel or user space. If the kernel supports in-kernel virtualization of the interrupt controller, it will default to that. If not, if will default to user space emulation.
Unfortunately when running in user mode gic emulation, we miss out on interrupt events which are only available from kernel space, such as the timer. This patch leverages the new kernel/user space pending line synchronization for timer events. It does not handle PMU events yet.
--- accel/kvm/kvm-all.c | 5 +++++ accel/stubs/kvm-stub.c | 5 +++++ hw/intc/arm_gic.c | 7 +++++++ include/sysemu/kvm.h | 11 +++++++++++ target/arm/cpu.h | 3 +++ target/arm/kvm.c | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 80 insertions(+)
accel/kvm/kvm-all.c +5 -0
@@ -2285,6 +2285,11 @@ int kvm_has_intx_set_mask(void)return kvm_state->intx_set_mask; } +bool kvm_arm_supports_user_irq (void ) +{ +return kvm_check_extension (kvm_state,KVM_CAP_ARM_USER_IRQ ); +} +#ifdef KVM_CAP_SET_GUEST_DEBUG struct kvm_sw_breakpoint *kvm_find_sw_breakpoint (CPUState *cpu,target_ulong pc)
accel/stubs/kvm-stub.c +5 -0
@@ -155,4 +155,9 @@ void kvm_init_cpu_signals(CPUState *cpu) {abort (); } + +bool kvm_arm_supports_user_irq (void ) +{ +return false; +}#endif
hw/intc/arm_gic.c +7 -0
@@ -25,6 +25,7 @@#include "qom/cpu.h" #include "qemu/log.h" #include "trace.h" +#include "sysemu/kvm.h" /* #define DEBUG_GIC */
@@ -1412,6 +1413,12 @@ static void arm_gic_realize(DeviceState *dev, Error **errp)return ; } +if (kvm_enabled () && !kvm_arm_supports_user_irq ()) { +error_setg (errp,"KVM with user space irqchip only works when the " +"host kernel supports KVM_CAP_ARM_USER_IRQ" ); +return ; + } +/* This creates distributor and main CPU interface (s->cpuiomem[0]) */ gic_init_irqs_and_mmio (s, gic_set_irq, gic_ops);
include/sysemu/kvm.h +11 -0
@@ -227,6 +227,17 @@ int kvm_init_vcpu(CPUState *cpu);int kvm_cpu_exec (CPUState *cpu);int kvm_destroy_vcpu (CPUState *cpu); +/** +* kvm_arm_supports_user_irq +* +* Not all KVM implementations support notifications for kernel generated +* interrupt events to user space. This function indicates whether the current +* KVM implementation does support them. +* +* Returns: true if KVM supports using kernel generated IRQs from user space +*/ +bool kvm_arm_supports_user_irq (void ); +#ifdef NEED_CPU_H #include "cpu.h"
target/arm/cpu.h +3 -0
@@ -706,6 +706,9 @@ struct ARMCPU {void *el_change_hook_opaque;int32_t node_id;/* NUMA node this CPU belongs to */ + +/* Used to synchronize KVM and QEMU timer levels */ +uint8_t device_irq_level; };static inline ARMCPU *arm_env_get_cpu (CPUARMState *env)
target/arm/kvm.c +49 -0
61 lines not on this page · raw message
Signature · 1 line
1.8.5.6
Quoted 63 lines · Alexander Graf
When running with KVM enabled, you can choose between emulating the gic in kernel or user space. If the kernel supports in-kernel virtualization of the interrupt controller, it will default to that. If not, if will default to user space emulation.
Unfortunately when running in user mode gic emulation, we miss out on interrupt events which are only available from kernel space, such as the timer. This patch leverages the new kernel/user space pending line synchronization for timer events. It does not handle PMU events yet.
Signed-off-by Alexander Graf <[email protected]>--- accel/kvm/kvm-all.c | 5 +++++ accel/stubs/kvm-stub.c | 5 +++++ hw/intc/arm_gic.c | 7 +++++++ include/sysemu/kvm.h | 11 +++++++++++ target/arm/cpu.h | 3 +++ target/arm/kvm.c | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 80 insertions(+)accel/kvm/kvm-all.c +5
-0@@ -2285,6 +2285,11 @@ int kvm_has_intx_set_mask(void)return kvm_state->intx_set_mask; } +bool kvm_arm_supports_user_irq (void ) +{ +return kvm_check_extension (kvm_state,KVM_CAP_ARM_USER_IRQ ); +} +#ifdef KVM_CAP_SET_GUEST_DEBUG struct kvm_sw_breakpoint *kvm_find_sw_breakpoint (CPUState *cpu,target_ulong pc)accel/stubs/kvm-stub.c +5
-0@@ -155,4 +155,9 @@ void kvm_init_cpu_signals(CPUState *cpu) {abort (); } + +bool kvm_arm_supports_user_irq (void ) +{ +return false; +}#endif hw/intc/arm_gic.c +1
-0@@ -25,6 +25,7 @@#include "qom/cpu.h" #include "qemu/log.h" #include "trace.h" +#include "sysemu/kvm.h" /* #define DEBUG_GIC */
hw/intc/arm_gic.c static void arm_gic_realize(DeviceState *dev, Error **errp) · line 1419
Quoted 7 lines
hw/intc/arm_gic.c +4
-0@@ -1412,6 +1413,12 @@ static void arm_gic_realize(DeviceState *dev, Error **errp)return ; } +if (kvm_enabled () && !kvm_arm_supports_user_irq ()) { +error_setg (errp,"KVM with user space irqchip only works when the " +"host kernel supports KVM_CAP_ARM_USER_IRQ" ); +return ;
extra spaces here
Quoted 32 lines
hw/intc/arm_gic.c +2
-0+ } +/* This creates distributor and main CPU interface (s->cpuiomem[0]) */ gic_init_irqs_and_mmio (s, gic_set_irq, gic_ops);include/sysemu/kvm.h +11
-0@@ -227,6 +227,17 @@ int kvm_init_vcpu(CPUState *cpu);int kvm_cpu_exec (CPUState *cpu);int kvm_destroy_vcpu (CPUState *cpu); +/** +* kvm_arm_supports_user_irq +* +* Not all KVM implementations support notifications for kernel generated +* interrupt events to user space. This function indicates whether the current +* KVM implementation does support them. +* +* Returns: true if KVM supports using kernel generated IRQs from user space +*/ +bool kvm_arm_supports_user_irq (void ); +#ifdef NEED_CPU_H #include "cpu.h" target/arm/cpu.h +0
-0
target/arm/cpu.h struct ARMCPU { · line 710
Quoted 5 lines
target/arm/cpu.h +2
-0@@ -706,6 +706,9 @@ struct ARMCPU {void *el_change_hook_opaque;int32_t node_id;/* NUMA node this CPU belongs to */ + +/* Used to synchronize KVM and QEMU timer levels */
Eventually it won't only be for timer levels
Quoted 37 lines
target/arm/cpu.h +1
-0+uint8_t device_irq_level; };static inline ARMCPU *arm_env_get_cpu (CPUARMState *env)target/arm/kvm.c +18
-0@@ -174,6 +174,12 @@ int kvm_arch_init(MachineState *ms, KVMState *s)*/ kvm_async_interrupts_allowed = true; +/* +* PSCI wakes up secondary cores, so we always need to +* have vCPUs waiting in kernel space +*/ + kvm_halt_in_kernel_allowed = true; + cap_has_mp_state =kvm_check_extension (s,KVM_CAP_MP_STATE );type_register_static (&host_arm_cpu_type_info);@@ -528,6 +534,49 @@ void kvm_arch_pre_run(CPUState *cs, struct kvm_run *run)MemTxAttrs kvm_arch_post_run (CPUState *cs,struct kvm_run *run) { +ARMCPU *cpu; +uint32_t switched_level; + +if (kvm_irqchip_in_kernel ()) { +/* +* We only need to sync timer states with user-space interrupt +* controllers, so return early and save cycles if we don't. +*/ +return MEMTXATTRS_UNSPECIFIED ; + } + + cpu =ARM_CPU (cs);
target/arm/kvm.c void kvm_arch_pre_run(CPUState *cs, struct kvm_run *run) · line 558
Quoted 10 lines
target/arm/kvm.c +10
-0+ +/* Synchronize our internal timer irq lines with the kvm ones */ +if (run->s.regs.device_irq_level != cpu->device_irq_level) { + switched_level = cpu->device_irq_level ^ run->s.regs.device_irq_level; + +qemu_mutex_lock_iothread (); + +if (switched_level &KVM_ARM_DEV_EL1_VTIMER ) { +qemu_set_irq (cpu->gt_timer_outputs[GTIMER_VIRT ], + run->s.regs.device_irq_level &KVM_ARM_DEV_EL1_VTIMER );
Should be !!(run->s.regs.device_irq_level & KVM_ARM_DEV_EL1_VTIMER)
target/arm/kvm.c void kvm_arch_pre_run(CPUState *cs, struct kvm_run *run) · line 564
Quoted 6 lines
target/arm/kvm.c +6
-0+ switched_level &= ~KVM_ARM_DEV_EL1_VTIMER ; + } + +if (switched_level &KVM_ARM_DEV_EL1_PTIMER ) { +qemu_set_irq (cpu->gt_timer_outputs[GTIMER_PHYS ], + run->s.regs.device_irq_level &KVM_ARM_DEV_EL1_PTIMER );
Also need the !!
target/arm/kvm.c void kvm_arch_pre_run(CPUState *cs, struct kvm_run *run) · line 568
Quoted 4 lines
target/arm/kvm.c +4
-0+ switched_level &= ~KVM_ARM_DEV_EL1_PTIMER ; + } + +/* XXX PMU IRQ is missing */
So I was wondering why you chose not to wire up the PMU in order to finish this off, since I knew KVM has commit 3dbbdf78636e "KVM: arm/arm64: Report PMU overflow interrupts to userspace irqchip", which I presume should allow the PMU to work too. However, attempting to start a guest with -machine virt,accel=kvm,kernel-irqchip=off -cpu host,pmu=on doesn't provide it a PMU. AFAICT, that's only because KVM still has
/*
* We currently require an in-kernel VGIC to use the PMU emulation,
* because we do not support forwarding PMU overflow interrupts to
* userspace yet.
*/
if (!irqchip_in_kernel(vcpu->kvm) || !vgic_initialized(vcpu->kvm))
return -ENODEV;in kvm_arm_pmu_v3_init(). Anyone know why that wasn't removed with 3dbbdf78636e?
target/arm/kvm.c void kvm_arch_pre_run(CPUState *cs, struct kvm_run *run) · line 571
Quoted 3 lines
target/arm/kvm.c +3
-0+ +if (switched_level) { + qemu_log_mask(LOG_UNIMP, "%s: unhandled device IRQ %x\n",
Misleading message here. It's not a device IRQ that switched_level contains.
Quoted 15 lines
target/arm/kvm.c +8
-0+ __func__, switched_level); + } + +/* We also mark unknown levels as processed to not waste cycles */ + cpu->device_irq_level = run->s.regs.device_irq_level; +qemu_mutex_unlock_iothread (); + } +return MEMTXATTRS_UNSPECIFIED ; }Signature · 1 line
1.8.5.6
Thanks,
drew
Quoted 67 lines · Andrew Jones
Quoted 66 lines · Alexander Graf
When running with KVM enabled, you can choose between emulating the gic in kernel or user space. If the kernel supports in-kernel virtualization of the interrupt controller, it will default to that. If not, if will default to user space emulation.
Unfortunately when running in user mode gic emulation, we miss out on interrupt events which are only available from kernel space, such as the timer. This patch leverages the new kernel/user space pending line synchronization for timer events. It does not handle PMU events yet.
Signed-off-by Alexander Graf <[email protected]>--- accel/kvm/kvm-all.c | 5 +++++ accel/stubs/kvm-stub.c | 5 +++++ hw/intc/arm_gic.c | 7 +++++++ include/sysemu/kvm.h | 11 +++++++++++ target/arm/cpu.h | 3 +++ target/arm/kvm.c | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 80 insertions(+)accel/kvm/kvm-all.c +5
-0@@ -2285,6 +2285,11 @@ int kvm_has_intx_set_mask(void)return kvm_state->intx_set_mask; } +bool kvm_arm_supports_user_irq (void ) +{ +return kvm_check_extension (kvm_state,KVM_CAP_ARM_USER_IRQ ); +} +#ifdef KVM_CAP_SET_GUEST_DEBUG struct kvm_sw_breakpoint *kvm_find_sw_breakpoint (CPUState *cpu,target_ulong pc)accel/stubs/kvm-stub.c +5
-0@@ -155,4 +155,9 @@ void kvm_init_cpu_signals(CPUState *cpu) {abort (); } + +bool kvm_arm_supports_user_irq (void ) +{ +return false; +}#endif hw/intc/arm_gic.c +1
-0@@ -25,6 +25,7 @@#include "qom/cpu.h" #include "qemu/log.h" #include "trace.h" +#include "sysemu/kvm.h" /* #define DEBUG_GIC */ @@ -1412,6 +1413,12 @@ static void arm_gic_realize(DeviceState *dev, Error **errp)return ; }
Quoted 6 lines
hw/intc/arm_gic.c static void arm_gic_realize(DeviceState *dev, Error **errp) · line 1419
Quoted 4 lines
hw/intc/arm_gic.c +4
-0+if (kvm_enabled () && !kvm_arm_supports_user_irq ()) { +error_setg (errp,"KVM with user space irqchip only works when the " +"host kernel supports KVM_CAP_ARM_USER_IRQ" ); +return ;extra spaces here
Fixed
Quoted 35 lines
Quoted 35 lines
hw/intc/arm_gic.c +2
-0+ } +/* This creates distributor and main CPU interface (s->cpuiomem[0]) */ gic_init_irqs_and_mmio (s, gic_set_irq, gic_ops);include/sysemu/kvm.h +11
-0@@ -227,6 +227,17 @@ int kvm_init_vcpu(CPUState *cpu);int kvm_cpu_exec (CPUState *cpu);int kvm_destroy_vcpu (CPUState *cpu); +/** +* kvm_arm_supports_user_irq +* +* Not all KVM implementations support notifications for kernel generated +* interrupt events to user space. This function indicates whether the current +* KVM implementation does support them. +* +* Returns: true if KVM supports using kernel generated IRQs from user space +*/ +bool kvm_arm_supports_user_irq (void ); +#ifdef NEED_CPU_H #include "cpu.h" target/arm/cpu.h +0
-0@@ -706,6 +706,9 @@ struct ARMCPU {void *el_change_hook_opaque;
Quoted 5 lines
target/arm/cpu.h struct ARMCPU { · line 710
Quoted 3 lines
target/arm/cpu.h +2
-0int32_t node_id;/* NUMA node this CPU belongs to */ + +/* Used to synchronize KVM and QEMU timer levels */ Eventually it won't only be for timer levels
Ah, one place I forgot to fix up :). Fixed.
Quoted 42 lines
Quoted 42 lines
target/arm/cpu.h +1
-0+uint8_t device_irq_level; };static inline ARMCPU *arm_env_get_cpu (CPUARMState *env)target/arm/kvm.c +22
-0@@ -174,6 +174,12 @@ int kvm_arch_init(MachineState *ms, KVMState *s)*/ kvm_async_interrupts_allowed = true; +/* +* PSCI wakes up secondary cores, so we always need to +* have vCPUs waiting in kernel space +*/ + kvm_halt_in_kernel_allowed = true; + cap_has_mp_state =kvm_check_extension (s,KVM_CAP_MP_STATE );type_register_static (&host_arm_cpu_type_info);@@ -528,6 +534,49 @@ void kvm_arch_pre_run(CPUState *cs, struct kvm_run *run)MemTxAttrs kvm_arch_post_run (CPUState *cs,struct kvm_run *run) { +ARMCPU *cpu; +uint32_t switched_level; + +if (kvm_irqchip_in_kernel ()) { +/* +* We only need to sync timer states with user-space interrupt +* controllers, so return early and save cycles if we don't. +*/ +return MEMTXATTRS_UNSPECIFIED ; + } + + cpu =ARM_CPU (cs); + +/* Synchronize our internal timer irq lines with the kvm ones */ +if (run->s.regs.device_irq_level != cpu->device_irq_level) { + switched_level = cpu->device_irq_level ^ run->s.regs.device_irq_level;
Quoted 8 lines
target/arm/kvm.c void kvm_arch_pre_run(CPUState *cs, struct kvm_run *run) · line 558
Quoted 6 lines
target/arm/kvm.c +6
-0+ +qemu_mutex_lock_iothread (); + +if (switched_level &KVM_ARM_DEV_EL1_VTIMER ) { +qemu_set_irq (cpu->gt_timer_outputs[GTIMER_VIRT ], + run->s.regs.device_irq_level &KVM_ARM_DEV_EL1_VTIMER );Should be !!(run->s.regs.device_irq_level &
KVM_ARM_DEV_EL1_VTIMER)
Yup.
Quoted 9 lines
target/arm/kvm.c void kvm_arch_pre_run(CPUState *cs, struct kvm_run *run) · line 564
Quoted 7 lines
target/arm/kvm.c +6
-0+ switched_level &= ~KVM_ARM_DEV_EL1_VTIMER ; + } + +if (switched_level &KVM_ARM_DEV_EL1_PTIMER ) { +qemu_set_irq (cpu->gt_timer_outputs[GTIMER_PHYS ], + run->s.regs.device_irq_level &KVM_ARM_DEV_EL1_PTIMER );Also need the !!
Yup.
Quoted 7 lines
target/arm/kvm.c void kvm_arch_pre_run(CPUState *cs, struct kvm_run *run) · line 568
Quoted 5 lines
target/arm/kvm.c +4
-0+ switched_level &= ~KVM_ARM_DEV_EL1_PTIMER ; + } + +/* XXX PMU IRQ is missing */ So I was wondering why you chose not to wire up the PMU in order to finish
Because the PMU IRQ is only available from the machine file, so it'll need some more plumbing to get the qemu_irq object available from kvm.c.
Quoted 14 lines
this off, since I knew KVM has commit
3dbbdf78636e"KVM: arm/arm64: Report PMU overflow interrupts to userspace irqchip", which I presume should allow the PMU to work too. However, attempting to start a guest with -machine virt,accel=kvm,kernel-irqchip=off -cpu host,pmu=on doesn't provide it a PMU. AFAICT, that's only because KVM still has/* * We currently require an in-kernel VGIC to use the PMU emulation, * because we do not support forwarding PMU overflow interrupts to * userspace yet. */ if (!irqchip_in_kernel(vcpu->kvm) || !vgic_initialized(vcpu->kvm)) return -ENODEV;
Quoted 2 lines
in
kvm_arm_pmu_v3_init(). Anyone know why that wasn't removed with3dbbdf78636e?
I guess it should get removed when the PMU sync works?
Quoted 7 lines
target/arm/kvm.c void kvm_arch_pre_run(CPUState *cs, struct kvm_run *run) · line 571
Quoted 4 lines
target/arm/kvm.c +3
-0+ +if (switched_level) { + qemu_log_mask(LOG_UNIMP, "%s: unhandled device IRQ %x\n",Misleading message here. It's not a device IRQ that
switched_levelcontains.
Ok, changed to "in-kernel device" as that really is what it is about.
Alex
Quoted 6 lines · Alexander Graf
Quoted 3 lines
Quoted 1 line
+ /* XXX PMU IRQ is missing */
So I was wondering why you chose not to wire up the PMU in order to finish
Because the PMU IRQ is only available from the machine file, so it'll need some more plumbing to get the
qemu_irqobject available fromkvm.c.
OK, I just wrote a patch based on this patch for the PMU and will post it in a second.
Quoted 15 lines
Quoted 15 lines
this off, since I knew KVM has commit
3dbbdf78636e"KVM: arm/arm64: Report PMU overflow interrupts to userspace irqchip", which I presume should allow the PMU to work too. However, attempting to start a guest with -machine virt,accel=kvm,kernel-irqchip=off -cpu host,pmu=on doesn't provide it a PMU. AFAICT, that's only because KVM still has/* * We currently require an in-kernel VGIC to use the PMU emulation, * because we do not support forwarding PMU overflow interrupts to * userspace yet. */ if (!irqchip_in_kernel(vcpu->kvm) || !vgic_initialized(vcpu->kvm)) return -ENODEV;
Quoted 4 lines
Quoted 2 lines
in
kvm_arm_pmu_v3_init(). Anyone know why that wasn't removed with3dbbdf78636e?I guess it should get removed when the PMU sync works?
OK, I'll post the KVM patch at the same time.
I noticed another thing while testing with a virtio-scsi disk; I get this on output when booting with userspace irqchip
virtio-scsi: Failed to set guest notifiers (-11), ensure -enable-kvm is set
qemu-system-aarch64: virtio_bus_start_ioeventfd: failed. Fallback to userspace (slower).
Just a warning, but I guess it should be removed, at least when the user has explicitly asked for a userspace irqchip with kernel-irqchip=no.
Thanks,
drew
Quoted 28 lines · Andrew Jones
Quoted 6 lines · Alexander Graf
Quoted 3 lines
Quoted 1 line
+ /* XXX PMU IRQ is missing */
So I was wondering why you chose not to wire up the PMU in order to finish
Because the PMU IRQ is only available from the machine file, so it'll need some more plumbing to get the
qemu_irqobject available fromkvm.c.OK, I just wrote a patch based on this patch for the PMU and will post it in a second.
Quoted 17 lines
Quoted 17 lines
this off, since I knew KVM has commit
3dbbdf78636e"KVM: arm/arm64: Report PMU overflow interrupts to userspace irqchip", which I presume should allow the PMU to work too. However, attempting to start a guest with -machine virt,accel=kvm,kernel-irqchip=off -cpu host,pmu=on doesn't provide it a PMU. AFAICT, that's only because KVM still has/* * We currently require an in-kernel VGIC to use the PMU emulation, * because we do not support forwarding PMU overflow interrupts to * userspace yet. */ if (!irqchip_in_kernel(vcpu->kvm) || !vgic_initialized(vcpu->kvm)) return -ENODEV;in
kvm_arm_pmu_v3_init(). Anyone know why that wasn't removed with3dbbdf78636e?
Eh, I missed a2befacf5094 "KVM: arm64: Allow creating the PMU without the in-kernel GIC", which is still in kvmarm/queue, not yet in Linus master. I'll have to revisit the QEMU patch I wrote to make sure it still makes sense.
drew