kvm: level irqfd and new eoifd
Subsystems (4)
v6 cover kvm: level irqfd and new eoifd
v6:
So we're back to just the first two patches, unfortunately the diffstat got bigger though. The reason for that is that I discovered we don't do anything on release of an eoifd. We cleanup if the kvm vm is released, but we're dealing with a constrained resource of irq source IDs, so I think it's best that we cleanup to make sure those are returned. To do that we need nearly the same infrastructure as irqfd, we just only watch for POLLHUP. So while there's more code here, the structure and function names line up identically to irqfd.
The other big change here is that KVM_IRQFD returns a token when setting up a level irqfd. We use this token to associate the eoifd with the right source. This means we have to put the struct _source_ids on a list so we can find them. This removes the weird interaction we were headed to where the eoifd is associated with the eventfd of the irqfd. There's potentially more flexibility for the future here too as we might come up with other interfaces that can return a source ID "key". Perhaps some future KVM_IRQFD will allow specifying a key for re-attaching. Anyway, the sequence Michael pointed out where an irqfd is de-assigned then re-assigned now results in a new key instead of leaving the user wondering if it re-associates back to the eoifd.
Also added workqueue flushes on assign since releasing either object now results in a lazy release via workqueue. This ensures we re-claim any source IDs we can. Thanks,
Alex
---
Alex Williamson (2):
kvm: KVM_EOIFD, an eventfd for EOIs
kvm: Extend irqfd to support level interrupts
Documentation/virtual/kvm/api.txt | 32 ++- arch/x86/kvm/x86.c | 3 include/linux/kvm.h | 18 + include/linux/kvm_host.h | 17 + virt/kvm/eventfd.c | 463 ++++++++++++++++++++++++++++++++++++- virt/kvm/kvm_main.c | 11 + 6 files changed, 536 insertions(+), 8 deletions(-)
PATCH v6 1/2 kvm: Extend irqfd to support level interrupts
In order to inject a level interrupt from an external source using an irqfd, we need to allocate a new irq_source_id. This allows us to assert and (later) de-assert an interrupt line independently from users of KVM_IRQ_LINE and avoid lost interrupts.
We also add what may appear like a bit of excessive infrastructure around an object for storing this irq_source_id. However, notice that we only provide a way to assert the interrupt here. A follow-on interface will make use of the same irq_source_id to allow de-assert.
---
Documentation/virtual/kvm/api.txt | 11 +++ arch/x86/kvm/x86.c | 1 include/linux/kvm.h | 3 + include/linux/kvm_host.h | 4 + virt/kvm/eventfd.c | 128 +++++++++++++++++++++++++++++++++++-- 5 files changed, 139 insertions(+), 8 deletions(-)
Documentation/virtual/kvm/api.txt +10 -1
@@ -1936,7 +1936,7 @@ Capability: KVM_CAP_IRQFD Architectures: x86 Type: vm ioctl Parameters: struct kvm_irqfd (in) -Returns: 0 on success, -1 on error +Returns: 0 (or >= 0) on success, -1 on error Allows setting an eventfd to directly trigger a guest interrupt. kvm_irqfd.fd specifies the file descriptor to use as the eventfd and
@@ -1946,6 +1946,15 @@ 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. +The KVM_IRQFD_FLAG_LEVEL flag indicates the gsi input is for a level +triggered interrupt. In this case a new irqchip input is allocated +which is logically OR'd with other inputs allowing multiple sources +to independently assert level interrupts. The KVM_IRQFD_FLAG_LEVEL +is only necessary on setup, teardown is identical to that above. The +return value when called with this flag is a key (>= 0) which may be +used to associate this irqfd with other ioctls. KVM_IRQFD_FLAG_LEVEL +support is indicated by KVM_CAP_IRQFD_LEVEL. + 4.76 KVM_PPC_ALLOCATE_HTAB Capability: KVM_CAP_PPC_ALLOC_HTAB
arch/x86/kvm/x86.c +1 -0
@@ -2170,6 +2170,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_LEVEL : r =1 ;break ;case KVM_CAP_COALESCED_MMIO :
include/linux/kvm.h +3 -0
@@ -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_LEVEL 81#ifdef KVM_CAP_IRQ_ROUTING
@@ -683,6 +684,8 @@ struct kvm_xen_hvm_config {#endif #define KVM_IRQFD_FLAG_DEASSIGN (1 << 0) +/* Available with KVM_CAP_IRQFD_LEVEL */ +#define KVM_IRQFD_FLAG_LEVEL (1 << 1)struct kvm_irqfd {__u32 fd;
include/linux/kvm_host.h +4 -0
@@ -285,6 +285,10 @@ struct kvm { struct list_head items; } irqfds;struct list_head ioeventfds; +struct { +struct mutex lock; +struct list_head items; + } irqsources;#endif struct kvm_vm_stat stat;struct kvm_arch arch;
virt/kvm/eventfd.c +121 -7
201 lines not on this page · raw message
PATCH v6 2/2 kvm: KVM_EOIFD, an eventfd for EOIs
In reply to Alex WilliamsonThis new ioctl enables an eventfd to be triggered when an EOI is written for a specified irqchip pin. The first user of this will be external device assignment through VFIO, using a level irqfd for asserting a PCI INTx interrupt and this interface for de-assert and notification once the interrupt is serviced.
Here we make use of the reference counting of the _irq_source object allowing us to share it with an irqfd and cleanup regardless of the release order.
---
Documentation/virtual/kvm/api.txt | 21 ++ arch/x86/kvm/x86.c | 2 include/linux/kvm.h | 15 ++ include/linux/kvm_host.h | 13 + virt/kvm/eventfd.c | 335 +++++++++++++++++++++++++++++++++++++ virt/kvm/kvm_main.c | 11 + 6 files changed, 397 insertions(+)
Documentation/virtual/kvm/api.txt +21 -0
@@ -1989,6 +1989,27 @@ return the hash table order in the parameter. (If the guest is using the virtualized real-mode area (VRMA) facility, the kernel will re-create the VMRA HPTEs on the next KVM_RUN of any vcpu.) +4.77 KVM_EOIFD + +Capability: KVM_CAP_EOIFD +Architectures: x86 +Type: vm ioctl +Parameters: struct kvm_eoifd (in) +Returns: 0 on success, < 0 on error + +KVM_EOIFD allows userspace to receive interrupt EOI notification +through an eventfd. kvm_eoifd.fd specifies the eventfd used for +notification. KVM_EOIFD_FLAG_DEASSIGN is used to de-assign an eoifd +once assigned. KVM_EOIFD also requires additional bits set in +kvm_eoifd.flags to bind to the proper interrupt line. The +KVM_EOIFD_FLAG_LEVEL_IRQFD indicates that kvm_eoifd.key is provided +and is a key from a level triggered interrupt (configured from +KVM_IRQFD using KVM_IRQFD_FLAG_LEVEL). The EOI notification is bound +to the same GSI and irqchip input as the irqfd. Both kvm_eoifd.key +and KVM_EOIFD_FLAG_LEVEL_IRQFD must be specified on assignment and +de-assignment of KVM_EOIFD. A level irqfd may only be bound to a +single eoifd. KVM_CAP_EOIFD_LEVEL_IRQFD indicates support of +KVM_EOIFD_FLAG_LEVEL_IRQFD. 5. The kvm_run structure ------------------------
arch/x86/kvm/x86.c +2 -0
@@ -2171,6 +2171,8 @@ int kvm_dev_ioctl_check_extension(long ext)case KVM_CAP_PCI_2_3 :case KVM_CAP_KVMCLOCK_CTRL :case KVM_CAP_IRQFD_LEVEL : +case KVM_CAP_EOIFD : +case KVM_CAP_EOIFD_LEVEL_IRQFD : r =1 ;break ;case KVM_CAP_COALESCED_MMIO :
include/linux/kvm.h +15 -0
@@ -619,6 +619,8 @@ struct kvm_ppc_smmu_info {#define KVM_CAP_S390_COW 79#define KVM_CAP_PPC_ALLOC_HTAB 80#define KVM_CAP_IRQFD_LEVEL 81 +#define KVM_CAP_EOIFD 82 +#define KVM_CAP_EOIFD_LEVEL_IRQFD 83#ifdef KVM_CAP_IRQ_ROUTING
@@ -694,6 +696,17 @@ struct kvm_irqfd {__u8 pad[20 ]; }; +#define KVM_EOIFD_FLAG_DEASSIGN (1 << 0) +/* Available with KVM_CAP_EOIFD_LEVEL_IRQFD */ +#define KVM_EOIFD_FLAG_LEVEL_IRQFD (1 << 1) + +struct kvm_eoifd { +__u32 fd; +__u32 flags; +__u32 key; +__u8 pad[20 ]; +}; +struct kvm_clock_data {__u64 clock;__u32 flags;
@@ -834,6 +847,8 @@ struct kvm_s390_ucas_mapping {#define KVM_PPC_GET_SMMU_INFO _IOR(KVMIO, 0xa6, struct kvm_ppc_smmu_info)/* Available with KVM_CAP_PPC_ALLOC_HTAB */ #define KVM_PPC_ALLOCATE_HTAB _IOWR(KVMIO, 0xa7, __u32) +/* Available with KVM_CAP_EOIFD */ +#define KVM_EOIFD _IOW(KVMIO, 0xa8, struct kvm_eoifd)/* * ioctls for vcpu fds
include/linux/kvm_host.h +13 -0
@@ -289,6 +289,10 @@ struct kvm {struct mutex lock; struct list_head items; } irqsources; +struct { +spinlock_t lock; +struct list_head items; + } eoifds;#endif struct kvm_vm_stat stat;struct kvm_arch arch;
@@ -832,6 +836,8 @@ int kvm_irqfd(struct kvm *kvm, struct kvm_irqfd *args);void kvm_irqfd_release (struct kvm *kvm);void kvm_irq_routing_update (struct kvm *,struct kvm_irq_routing_table *);int kvm_ioeventfd (struct kvm *kvm,struct kvm_ioeventfd *args); +int kvm_eoifd (struct kvm *kvm,struct kvm_eoifd *args); +void kvm_eoifd_release (struct kvm *kvm);#else
@@ -857,6 +863,13 @@ static inline int kvm_ioeventfd(struct kvm *kvm, struct kvm_ioeventfd *args) return -ENOSYS; } +static inline int kvm_eoifd (struct kvm *kvm,struct kvm_eoifd *args) +{ +return -ENOSYS ; +} + +static inline void kvm_eoifd_release (struct kvm *kvm) {} +#endif /* CONFIG_HAVE_KVM_EVENTFD */ #ifdef CONFIG_KVM_APIC_ARCHITECTURE
virt/kvm/eventfd.c +335 -0
350 lines not on this page · raw message
virt/kvm/kvm_main.c +11 -0
@@ -620,6 +620,8 @@ static int kvm_vm_release(struct inode *inode, struct file *filp)kvm_irqfd_release (kvm); +kvm_eoifd_release (kvm); +kvm_put_kvm (kvm);return 0 ; }
@@ -2093,6 +2095,15 @@ static long kvm_vm_ioctl(struct file *filp, break; }#endif +case KVM_EOIFD : { +struct kvm_eoifd data; + + r = -EFAULT ; +if (copy_from_user (&data, argp,sizeof data)) + goto out; + r =kvm_eoifd (kvm, &data); +break ; + }default : r =kvm_arch_vm_ioctl (filp, ioctl, arg);if (r == -ENOTTY )
Quoted 24 lines · Alex Williamson
v6:
So we're back to just the first two patches, unfortunately the diffstat got bigger though. The reason for that is that I discovered we don't do anything on release of an eoifd. We cleanup if the kvm vm is released, but we're dealing with a constrained resource of irq source IDs, so I think it's best that we cleanup to make sure those are returned. To do that we need nearly the same infrastructure as irqfd, we just only watch for POLLHUP. So while there's more code here, the structure and function names line up identically to irqfd.
The other big change here is that
KVM_IRQFDreturns a token when setting up a level irqfd. We use this token to associate the eoifd with the right source. This means we have to put the struct_source_idson a list so we can find them. This removes the weird interaction we were headed to where the eoifd is associated with the eventfd of the irqfd. There's potentially more flexibility for the future here too as we might come up with other interfaces that can return a source ID "key". Perhaps some futureKVM_IRQFDwill allow specifying a key for re-attaching. Anyway, the sequence Michael pointed out where an irqfd is de-assigned then re-assigned now results in a new key instead of leaving the user wondering if it re-associates back to the eoifd.
Quoted 3 lines
Also added workqueue flushes on assign since releasing either object now results in a lazy release via workqueue. This ensures we re-claim any source IDs we can. Thanks,
FYI, I seem to have found a new locking issue in this version. I'll send an update when I find it. Thanks,
Alex
Quoted 14 lines
---
Alex Williamson (2):
kvm:KVM_EOIFD, an eventfd for EOIs
kvm: Extend irqfd to support level interruptsDocumentation/virtual/kvm/api.txt | 32 ++- arch/x86/kvm/x86.c | 3 include/linux/kvm.h | 18 + include/linux/kvm_host.h | 17 + virt/kvm/eventfd.c | 463 ++++++++++++++++++++++++++++++++++++- virt/kvm/kvm_main.c | 11 + 6 files changed, 536 insertions(+), 8 deletions(-)