KVM: arm/arm64: Route vtimer events to user space
Subsystems (5)
PATCH v6 1/1 KVM: arm/arm64: Route vtimer events to user space
We have 2 modes for dealing with interrupts in the ARM world. We can either handle them all using hardware acceleration through the vgic or we can emulate a gic in user space and only drive CPU IRQ pins from there.
Unfortunately, when driving IRQs from user space, we never tell user space about timer events that may result in interrupt line state changes, so we lose out on timer events if we run with user space gic emulation.
This patch fixes that by exposing kvm's view of the vtimer irq line to user space and giving it a chance to sync that when it changes.
With this patch I can successfully run edk2 and Linux with user space gic emulation.
---
v1 -> v2:
- Add back curly brace that got lost
v2 -> v3:
- Split into patch set
v3 -> v4:
- Improve documentation
v4 -> v5:
- Rewrite to use pending state sync in sregs (marc)
- Remove redundant checks of vgic_initialized()
v5 -> v6:
- s/pending/asserted/
- split kvm_timer_flush_hwstate function
- remove user_timer_asserted
- rename kernel_timer_asserted to timer_irq_level
- exit only once per level change
- qemu tree to try this out: https://github.com/agraf/qemu.git no-kvm-irqchip-for-v6
--- Documentation/virtual/kvm/api.txt | 27 ++++++++ arch/arm/include/uapi/asm/kvm.h | 2 + arch/arm/kvm/arm.c | 26 ++++---- arch/arm64/include/uapi/asm/kvm.h | 2 + include/kvm/arm_arch_timer.h | 4 +- include/uapi/linux/kvm.h | 6 ++ virt/kvm/arm/arch_timer.c | 133 +++++++++++++++++++++++++++++++------- 7 files changed, 164 insertions(+), 36 deletions(-)
Documentation/virtual/kvm/api.txt +27 -0
@@ -3928,3 +3928,30 @@ In order to use SynIC, it has to be activated by setting this capability via KVM_ENABLE_CAP ioctl on the vcpu fd. Note that this will disable the use of APIC hardware virtualization even if supported by the CPU, as it's incompatible with SynIC auto-EOI behavior. + +8.3 KVM_CAP_ARM_TIMER + +Architectures: arm, arm64 +This capability, if KVM_CHECK_EXTENSION indicates that it is available and no +in-kernel interrupt controller is in use, means that that the kernel populates +the vcpu's run->s.regs.timer_irq_level field with timers that are currently +considered asserted by kvm. + +If active, kvm guarantees at least one exit happens after it detects an +assertion change. This exit could either be a KVM_EXIT_INTR or any other exit +event, like KVM_EXIT_MMIO. This way kvm gives user space a chance to update its +own interrupt asserted status. This usually involves triggering an interrupt +line on a user space emulated interrupt controller, so user space should always +check the state of run->s.regs.timer_irq_level on every kvm exit. + +The field run->s.regs.timer_irq_level is available independent of +run->kvm_valid_regs or run->kvm_dirty_regs bits. If no in-kernel interrupt +controller is used and the capability exists, it will always be available and +populated. + +Currently the following bits are defined for the timer_irq_level bitmap: + + KVM_ARM_TIMER_VTIMER - virtual timer + +Future versions of kvm may implement additional timer events. These will get +indicated by additional KVM_CAP extensions.
arch/arm/include/uapi/asm/kvm.h +2 -0
@@ -105,6 +105,8 @@ struct kvm_debug_exit_arch { };struct kvm_sync_regs { +/* Used with KVM_CAP_ARM_TIMER */ +u8 timer_irq_level; };struct kvm_arch_memory_slot {
arch/arm/kvm/arm.c +15 -11
@@ -187,6 +187,7 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)case KVM_CAP_ARM_PSCI_0_2 :case KVM_CAP_READONLY_MEM :case KVM_CAP_MP_STATE : +case KVM_CAP_ARM_TIMER : r =1 ;break ;case KVM_CAP_COALESCED_MMIO :
@@ -474,13 +475,7 @@ static int kvm_vcpu_first_run_init(struct kvm_vcpu *vcpu)return ret; } -/* -* Enable the arch timers only if we have an in-kernel VGIC -* and it has been properly initialized, since we cannot handle -* interrupts from the virtual timer with a userspace gic. -*/ -if (irqchip_in_kernel (kvm) &&vgic_initialized (kvm)) - ret =kvm_timer_enable (vcpu); + ret =kvm_timer_enable (vcpu);return ret; }
@@ -549,7 +544,7 @@ static int kvm_vcpu_initialized(struct kvm_vcpu *vcpu)*/ int kvm_arch_vcpu_ioctl_run (struct kvm_vcpu *vcpu,struct kvm_run *run) { -int ret; +int ret, timer_ret;sigset_t sigsaved;if (unlikely (!kvm_vcpu_initialized (vcpu)))
@@ -588,7 +583,7 @@ int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *run)*/ preempt_disable ();kvm_pmu_flush_hwstate (vcpu); -kvm_timer_flush_hwstate (vcpu); + timer_ret =kvm_timer_flush_hwstate (vcpu);kvm_vgic_flush_hwstate (vcpu);local_irq_disable ();
@@ -596,7 +591,7 @@ int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *run)/* * Re-check atomic conditions */ -if (signal_pending (current)) { +if (timer_ret ||signal_pending (current)) { ret = -EINTR ; run->exit_reason =KVM_EXIT_INTR ; }
@@ -659,13 +654,22 @@ int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *run)* interrupt line. */ kvm_pmu_sync_hwstate (vcpu); -kvm_timer_sync_hwstate (vcpu); + timer_ret =kvm_timer_sync_hwstate (vcpu);kvm_vgic_sync_hwstate (vcpu);preempt_enable (); ret =handle_exit (vcpu, run, ret); + +if ((ret ==1 ) && timer_ret) { +/* +* We have to exit straight away to ensure that we only +* ever notify user space once about a level change +*/ + ret = -EINTR ; + run->exit_reason =KVM_EXIT_INTR ; + } }if (vcpu->sigset_active)
arch/arm64/include/uapi/asm/kvm.h +2 -0
@@ -143,6 +143,8 @@ struct kvm_debug_exit_arch {#define KVM_GUESTDBG_USE_HW (1 << 17)struct kvm_sync_regs { +/* Used with KVM_CAP_ARM_TIMER */ +u8 timer_irq_level; };struct kvm_arch_memory_slot {
include/kvm/arm_arch_timer.h +2 -2
@@ -63,8 +63,8 @@ void kvm_timer_init(struct kvm *kvm);int kvm_timer_vcpu_reset (struct kvm_vcpu *vcpu,const struct kvm_irq_level *irq);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); +int kvm_timer_flush_hwstate (struct kvm_vcpu *vcpu); +int kvm_timer_sync_hwstate (struct kvm_vcpu *vcpu);void kvm_timer_vcpu_terminate (struct kvm_vcpu *vcpu);u64 kvm_arm_timer_get_reg (struct kvm_vcpu *,u64 regid);
include/uapi/linux/kvm.h +6 -0
@@ -870,6 +870,7 @@ struct kvm_ppc_smmu_info {#define KVM_CAP_S390_USER_INSTR0 130#define KVM_CAP_MSI_DEVID 131#define KVM_CAP_PPC_HTM 132 +#define KVM_CAP_ARM_TIMER 133#ifdef KVM_CAP_IRQ_ROUTING
@@ -1327,4 +1328,9 @@ struct kvm_assigned_msix_entry {#define KVM_X2APIC_API_USE_32BIT_IDS (1ULL << 0)#define KVM_X2APIC_API_DISABLE_BROADCAST_QUIRK (1ULL << 1) +/* Available with KVM_CAP_ARM_TIMER */ + +/* Bits for run->s.regs.{user,kernel}_timer_asserted */ +#define KVM_ARM_TIMER_VTIMER (1 << 0) +#endif /* __LINUX_KVM_H */
virt/kvm/arm/arch_timer.c +110 -23
202 lines not on this page · raw message
Signature · 1 line
1.8.5.6
Paolo Bonzini
maintainerarch/arm/kvm/arm.c int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *run) · line 669
Quoted 13 lines · Alexander Graf
arch/arm/kvm/arm.c +7
-0+ timer_ret =kvm_timer_sync_hwstate (vcpu);kvm_vgic_sync_hwstate (vcpu);preempt_enable (); ret =handle_exit (vcpu, run, ret); + +if ((ret ==1 ) && timer_ret) { +/* +* We have to exit straight away to ensure that we only +* ever notify user space once about a level change +*/
Is this really a requirement? It complicates the logic noticeably.
Paolo
Quoted 4 lines
arch/arm/kvm/arm.c +3
-0+ ret = -EINTR ; + run->exit_reason =KVM_EXIT_INTR ; + } }
Quoted 21 lines
Am 23.09.2016 um 14:11 schrieb Paolo Bonzini <[email protected]>:
Quoted 4 lines · Alexander Graf
arch/arm/kvm/arm.c +1
-0+ timer_ret =kvm_timer_sync_hwstate (vcpu);kvm_vgic_sync_hwstate (vcpu);arch/arm/kvm/arm.c int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *run) · line 669
Quoted 10 lines
arch/arm/kvm/arm.c +6
-0preempt_enable (); ret =handle_exit (vcpu, run, ret); + +if ((ret ==1 ) && timer_ret) { +/* +* We have to exit straight away to ensure that we only +* ever notify user space once about a level change +*/
Quoted 1 line
Is this really a requirement? It complicates the logic noticeably.
The alternative would be to track the state on ioctl entry, which Christoffer disliked :).
Alex
_______________________________________________
kvmarm mailing list
[email protected]
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
Quoted 9 lines · Paolo Bonzini
Quoted 6 lines · Alexander Graf
arch/arm/kvm/arm.c +1
-0+ timer_ret =kvm_timer_sync_hwstate (vcpu);kvm_vgic_sync_hwstate (vcpu);preempt_enable ();
Quoted 10 lines
arch/arm/kvm/arm.c int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *run) · line 669
Quoted 7 lines
arch/arm/kvm/arm.c +6
-0ret =handle_exit (vcpu, run, ret); + +if ((ret ==1 ) && timer_ret) { +/* +* We have to exit straight away to ensure that we only +* ever notify user space once about a level change +*/ Is this really a requirement? It complicates the logic noticeably.
If we skip this, then we have to somehow remember that the sync may have updated the line level when we reach the flush state (and didn't exit), and I would like maintaining that state even less than doing this check.
What we could do is to compare the timer->irq.level with the kvm_run state outside of the run loop (on the way to userspace) and update the kvm_run state if there's a discrepancy. That way, we can maintain the 'timer->irq.level != kvm_run' means, we have to exit, and whenever we are exiting, we are reporting the most recent state to user space anyway.
Would that work?
(By the way, the check should be via a call into the arch timer code, kvm_timer_update_user_state() or something like that).
Also, the comment above is confusing, because that's not why this check is here, the check is here so that we don't loose track of the timer output level change if there's no exit to userspace.
Thanks,
-Christoffer
Paolo Bonzini
maintainerQuoted 11 lines · Alexander Graf
Quoted 8 lines
arch/arm/kvm/arm.c int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *run) · line 669
Quoted 6 lines
arch/arm/kvm/arm.c +5
-0+if ((ret ==1 ) && timer_ret) { +/* +* We have to exit straight away to ensure that we only +* ever notify user space once about a level change +*/ Is this really a requirement? It complicates the logic noticeably.
The alternative would be to track the state on ioctl entry, which Christoffer disliked :).
Ok, I see. Maybe he's convinced by the ugly code. :)
It's not an architectural concept, it's a KVM API concept. Tracking the state on entry just the equivalent of making this vmexit edge-triggered (both rising and falling edge).
Paolo
Paolo Bonzini
maintainerQuoted 25 lines · Christoffer Dall
Quoted 9 lines · Paolo Bonzini
Quoted 6 lines · Alexander Graf
arch/arm/kvm/arm.c +1
-0+ timer_ret =kvm_timer_sync_hwstate (vcpu);kvm_vgic_sync_hwstate (vcpu);preempt_enable ();Quoted 10 lines
arch/arm/kvm/arm.c int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *run) · line 669
Quoted 7 lines
arch/arm/kvm/arm.c +6
-0ret =handle_exit (vcpu, run, ret); + +if ((ret ==1 ) && timer_ret) { +/* +* We have to exit straight away to ensure that we only +* ever notify user space once about a level change +*/ Is this really a requirement? It complicates the logic noticeably.
If we skip this, then we have to somehow remember that the sync may have updated the line level when we reach the flush state (and didn't exit), and I would like maintaining that state even less than doing this check.
Quoted 8 lines
What we could do is to compare the
timer->irq.levelwith thekvm_runstate outside of the run loop (on the way to userspace) and update thekvm_runstate if there's a discrepancy. That way, we can maintain the 'timer->irq.level!=kvm_run' means, we have to exit, and whenever we are exiting, we are reporting the most recent state to user space anyway.Would that work?
It seems clearer at least.
Quoted 2 lines
(By the way, the check should be via a call into the arch timer code,
kvm_timer_update_user_state()or something like that).
Makes sense.
Paolo
Quoted 11 lines
Also, the comment above is confusing, because that's not why this check is here, the check is here so that we don't loose track of the timer output level change if there's no exit to userspace.
Thanks,
-Christoffer
--
To unsubscribe from this list: send the line "unsubscribe kvm" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html