kvm: Add resampling irqfds for level triggered interrupts
Subsystems (4)
PATCH v11 1/1 kvm: Add resampling irqfds for level triggered interrupts
To emulate level triggered interrupts, add a resample option to KVM_IRQFD. When specified, a new resamplefd is provided that notifies the user when the irqchip has been resampled by the VM. This may, for instance, indicate an EOI. Also in this mode, posting of an interrupt through an irqfd only asserts the interrupt. On resampling, the interrupt is automatically de-asserted prior to user notification. This enables level triggered interrupts to be posted and re-enabled from vfio with no userspace intervention.
All resampling irqfds can make use of a single irq source ID, so we reserve a new one for this interface.
---
Taking Michael's idea to create our own lock for resampling irqfds simplifies things significantly. We can now avoid any nesting with the irqfds.lock spinlock so we can call everything we need to directly.
Documentation/virtual/kvm/api.txt | 13 +++ arch/x86/kvm/x86.c | 4 + include/linux/kvm.h | 12 +++ include/linux/kvm_host.h | 5 + virt/kvm/eventfd.c | 150 ++++++++++++++++++++++++++++++++++++- virt/kvm/irq_comm.c | 6 + 6 files changed, 184 insertions(+), 6 deletions(-)
Documentation/virtual/kvm/api.txt +13 -0
@@ -1946,6 +1946,19 @@ the guest using the specified gsi pin. The irqfd is removed using the KVM_IRQFD_FLAG_DEASSIGN flag, specifying both kvm_irqfd.fd and kvm_irqfd.gsi. +With KVM_CAP_IRQFD_RESAMPLE, KVM_IRQFD supports a de-assert and notify +mechanism allowing emulation of level-triggered, irqfd-based +interrupts. When KVM_IRQFD_FLAG_RESAMPLE is set the user must pass an +additional eventfd in the kvm_irqfd.resamplefd field. When operating +in resample mode, posting of an interrupt through kvm_irq.fd asserts +the specified gsi in the irqchip. When the irqchip is resampled, such +as from an EOI, the gsi is de-asserted and the user is notifed via +kvm_irqfd.resamplefd. It is the user's responsibility to re-queue +the interrupt if the device making use of it still requires service. +Note that closing the resamplefd is not sufficient to disable the +irqfd. The KVM_IRQFD_FLAG_RESAMPLE is only necessary on assignment +and need not be specified with KVM_IRQFD_FLAG_DEASSIGN. + 4.76 KVM_PPC_ALLOCATE_HTAB Capability: KVM_CAP_PPC_ALLOC_HTAB
arch/x86/kvm/x86.c +4 -0
@@ -2177,6 +2177,7 @@ int kvm_dev_ioctl_check_extension(long ext)case KVM_CAP_GET_TSC_KHZ :case KVM_CAP_PCI_2_3 :case KVM_CAP_KVMCLOCK_CTRL : +case KVM_CAP_IRQFD_RESAMPLE : r =1 ;break ;case KVM_CAP_COALESCED_MMIO :
@@ -6268,6 +6269,9 @@ int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)/* Reserve bit 0 of irq_sources_bitmap for userspace irq source */ set_bit (KVM_USERSPACE_IRQ_SOURCE_ID , &kvm->arch.irq_sources_bitmap); +/* Reserve bit 1 of irq_sources_bitmap for irqfd-resampler */ +set_bit (KVM_IRQFD_RESAMPLE_IRQ_SOURCE_ID , + &kvm->arch.irq_sources_bitmap);raw_spin_lock_init (&kvm->arch.tsc_write_lock);
include/linux/kvm.h +11 -1
@@ -618,6 +618,7 @@ struct kvm_ppc_smmu_info {#define KVM_CAP_PPC_GET_SMMU_INFO 78#define KVM_CAP_S390_COW 79#define KVM_CAP_PPC_ALLOC_HTAB 80 +#define KVM_CAP_IRQFD_RESAMPLE 81#ifdef KVM_CAP_IRQ_ROUTING
@@ -683,12 +684,21 @@ struct kvm_xen_hvm_config {#endif #define KVM_IRQFD_FLAG_DEASSIGN (1 << 0) +/* +* Available with KVM_CAP_IRQFD_RESAMPLE +* +* KVM_IRQFD_FLAG_RESAMPLE indicates resamplefd is valid and specifies +* the irqfd to operate in resampling mode for level triggered interrupt +* emlation. See Documentation/virtual/kvm/api.txt. +*/ +#define KVM_IRQFD_FLAG_RESAMPLE (1 << 1)struct kvm_irqfd {__u32 fd;__u32 gsi;__u32 flags; -__u8 pad[20 ]; +__u32 resamplefd; +__u8 pad[16 ]; };struct kvm_clock_data {
include/linux/kvm_host.h +4 -1
@@ -70,7 +70,8 @@#define KVM_REQ_PMU 16#define KVM_REQ_PMI 17 -#define KVM_USERSPACE_IRQ_SOURCE_ID 0 +#define KVM_USERSPACE_IRQ_SOURCE_ID 0 +#define KVM_IRQFD_RESAMPLE_IRQ_SOURCE_ID 1struct kvm ;struct kvm_vcpu ;
@@ -283,6 +284,8 @@ struct kvm {struct {spinlock_t lock;struct list_head items; +struct list_head resampler_list; +struct mutex resampler_lock; } irqfds;struct list_head ioeventfds;#endif
virt/kvm/eventfd.c +146 -4
204 lines not on this page · raw message
virt/kvm/irq_comm.c +6 -0
@@ -223,6 +223,9 @@ int kvm_request_irq_source_id(struct kvm *kvm) }ASSERT (irq_source_id !=KVM_USERSPACE_IRQ_SOURCE_ID ); +#ifdef CONFIG_X86 +ASSERT (irq_source_id !=KVM_IRQFD_RESAMPLE_IRQ_SOURCE_ID ); +#endif set_bit (irq_source_id, bitmap); unlock:mutex_unlock (&kvm->irq_lock);
@@ -233,6 +236,9 @@ unlock:void kvm_free_irq_source_id (struct kvm *kvm,int irq_source_id) {ASSERT (irq_source_id !=KVM_USERSPACE_IRQ_SOURCE_ID ); +#ifdef CONFIG_X86 +ASSERT (irq_source_id !=KVM_IRQFD_RESAMPLE_IRQ_SOURCE_ID ); +#endif mutex_lock (&kvm->irq_lock); if (irq_source_id < 0 ||
Avi Kivity
AppliedQuoted 9 lines · Alex Williamson
To emulate level triggered interrupts, add a resample option to
KVM_IRQFD. When specified, a new resamplefd is provided that notifies the user when the irqchip has been resampled by the VM. This may, for instance, indicate an EOI. Also in this mode, posting of an interrupt through an irqfd only asserts the interrupt. On resampling, the interrupt is automatically de-asserted prior to user notification. This enables level triggered interrupts to be posted and re-enabled from vfio with no userspace intervention.
Quoted 3 lines
All resampling irqfds can make use of a single irq source ID, so we reserve a new one for this interface.
Thanks, applied.
Signature · 2 lines
I have a truly marvellous patch that fixes the bug which this signature is too narrow to contain.