Threaded MSI interrupt for VFIO PCI device
Subsystems (5)
cover Threaded MSI interrupt for VFIO PCI device
When assigning a VFIO device to a KVM guest with low latency requirement, it is better to handle the interrupt in the hard interrupt context, to reduce the context switch to/from the IRQ thread.
Based on discussion on https://lkml.org/lkml/2015/10/26/764, the VFIO msi interrupt is changed to use request_threaded_irq(). The primary interrupt handler tries to set the guest interrupt atomically. If it fails to achieve it, a threaded interrupt handler will be invoked.
The irq_bypass manager is extended for this purpose. The KVM eventfd will provide a irqbypass consumer to handle the interrupt at hard interrupt context. The producer will invoke the consumer's handler then.
Yunhong Jiang (5):
Extract the irqfd_wakeup_pollin/irqfd_wakeup_pollup Support runtime irq_bypass consumer
Support threaded interrupt handling on VFIO Add the irq handling consumer
Expose x86 kvm_arch_set_irq_inatomic()
arch/x86/kvm/Kconfig | 1 + drivers/vfio/pci/vfio_pci_intrs.c | 39 ++++++++++-- include/linux/irqbypass.h | 8 +++ include/linux/kvm_host.h | 19 +++++- include/linux/kvm_irqfd.h | 1 + virt/kvm/Kconfig | 3 + virt/kvm/eventfd.c | 131 ++++++++++++++++++++++++++------------ virt/lib/irqbypass.c | 82 ++++++++++++++++++------ 8 files changed, 214 insertions(+), 70 deletions(-)
Signature · 1 line
1.8.3.1
PATCH 1/5 KVM: Extract the irqfd_wakeup_pollin/irqfd_wakeup_pollup
Separate the irqfd_wakeup_pollin/irqfd_wakeup_pollup from the irqfd_wakeup, so that we can reuse the logic for MSI fastpath injection.
--- virt/kvm/eventfd.c | 86 ++++++++++++++++++++++++++++++++---------------------- 1 file changed, 51 insertions(+), 35 deletions(-)
virt/kvm/eventfd.c +51 -35
@@ -180,6 +180,53 @@ int __attribute__((weak)) kvm_arch_set_irq_inatomic(return -EWOULDBLOCK ; } +static int +irqfd_wakeup_pollin (struct kvm_kernel_irqfd *irqfd) +{ +struct kvm *kvm = irqfd->kvm; +struct kvm_kernel_irq_routing_entry irq; +unsigned seq; +int idx, ret; + + idx =srcu_read_lock (&kvm->irq_srcu); +do { + seq =read_seqcount_begin (&irqfd->irq_entry_sc); + irq = irqfd->irq_entry; + }while (read_seqcount_retry (&irqfd->irq_entry_sc, seq)); +/* An event has been signaled, inject an interrupt */ + ret =kvm_arch_set_irq_inatomic (&irq, kvm, +KVM_USERSPACE_IRQ_SOURCE_ID ,1 , + false); +srcu_read_unlock (&kvm->irq_srcu, idx); + +return ret; +} + +static int +irqfd_wakeup_pollup (struct kvm_kernel_irqfd *irqfd) +{ +struct kvm *kvm = irqfd->kvm; +unsigned long flags; + +spin_lock_irqsave (&kvm->irqfds.lock, flags); + +/* +* We must check if someone deactivated the irqfd before +* we could acquire the irqfds.lock since the item is +* deactivated from the KVM side before it is unhooked from +* the wait-queue. If it is already deactivated, we can +* simply return knowing the other side will cleanup for us. +* We cannot race against the irqfd going away since the +* other side is required to acquire wqh->lock, which we hold +*/ +if (irqfd_is_active (irqfd)) +irqfd_deactivate (irqfd); + +spin_unlock_irqrestore (&kvm->irqfds.lock, flags); + +return 0 ; +} +/* * Called with wqh->lock held and interrupts disabled */
@@ -189,45 +236,14 @@ irqfd_wakeup(wait_queue_t *wait, unsigned mode, int sync, void *key)struct kvm_kernel_irqfd *irqfd = container_of(wait, struct kvm_kernel_irqfd, wait);unsigned long flags = (unsigned long )key; -struct kvm_kernel_irq_routing_entry irq; -struct kvm *kvm = irqfd->kvm; -unsigned seq; -int idx; -if (flags &POLLIN ) { - idx =srcu_read_lock (&kvm->irq_srcu); -do { - seq =read_seqcount_begin (&irqfd->irq_entry_sc); - irq = irqfd->irq_entry; - }while (read_seqcount_retry (&irqfd->irq_entry_sc, seq)); -/* An event has been signaled, inject an interrupt */ -if (kvm_arch_set_irq_inatomic (&irq, kvm, -KVM_USERSPACE_IRQ_SOURCE_ID ,1 , - false) == -EWOULDBLOCK ) +if (flags &POLLIN ) +if (irqfd_wakeup_pollin (irqfd) == -EWOULDBLOCK )schedule_work (&irqfd->inject); -srcu_read_unlock (&kvm->irq_srcu, idx); - } -if (flags &POLLHUP ) { +if (flags &POLLHUP )/* The eventfd is closing, detach from KVM */ -unsigned long flags; - -spin_lock_irqsave (&kvm->irqfds.lock, flags); - -/* -* We must check if someone deactivated the irqfd before -* we could acquire the irqfds.lock since the item is -* deactivated from the KVM side before it is unhooked from -* the wait-queue. If it is already deactivated, we can -* simply return knowing the other side will cleanup for us. -* We cannot race against the irqfd going away since the -* other side is required to acquire wqh->lock, which we hold -*/ -if (irqfd_is_active (irqfd)) -irqfd_deactivate (irqfd); - -spin_unlock_irqrestore (&kvm->irqfds.lock, flags); - } +irqfd_wakeup_pollup (irqfd);return 0 ; }
Signature · 1 line
1.8.3.1
PATCH 2/5 VIRT: Support runtime irq_bypass consumer
In reply to Yunhong JiangExtend the irq_bypass manager to support runtime consumers. A runtime irq_bypass consumer can handle interrupt when an interrupt triggered. A runtime consumer has it's handle_irq() function set and passing a irq_context for the irq handling.
A producer keep a link for the runtime consumers, so that it can invoke each consumer's handle_irq() when irq invoked.
Currently the irq_bypass manager has several code path assuming there is only one consumer/producer pair for each token. For example, when register the producer, it exits the loop after finding one match consumer. This is updated to support both static consumer (like for Posted Interrupt consumer) and runtime consumer.
--- include/linux/irqbypass.h | 8 +++++ virt/lib/irqbypass.c | 82 +++++++++++++++++++++++++++++++++++------------ 2 files changed, 69 insertions(+), 21 deletions(-)
include/linux/irqbypass.h +8 -0
@@ -12,6 +12,7 @@#define IRQBYPASS_H #include <linux/list.h> +#include <linux/srcu.h> struct irq_bypass_consumer ;
@@ -47,6 +48,9 @@ struct irq_bypass_consumer;*/ struct irq_bypass_producer {struct list_head node; +/* Update side is synchronized by the lock on irqbypass.c */ +struct srcu_struct srcu; +struct list_head consumers;void *token;int irq; int (*add_consumer)(struct irq_bypass_producer *,
@@ -61,6 +65,7 @@ struct irq_bypass_producer {* struct irq_bypass_consumer - IRQ bypass consumer definition * @node: IRQ bypass manager private list management * @token: opaque token to match between producer and consumer +* @sibling: consumers with same token list management * @add_producer: Connect the IRQ consumer to an IRQ producer * @del_producer: Disconnect the IRQ consumer from an IRQ producer * @stop: Perform any quiesce operations necessary prior to add/del (optional)
@@ -73,6 +78,7 @@ struct irq_bypass_producer {*/ struct irq_bypass_consumer {struct list_head node; +struct list_head sibling;void *token;int (*add_producer)(struct irq_bypass_consumer *,struct irq_bypass_producer *);
@@ -80,6 +86,8 @@ struct irq_bypass_consumer { struct irq_bypass_producer *);void (*stop)(struct irq_bypass_consumer *);void (*start)(struct irq_bypass_consumer *); +int (*handle_irq)(void *arg); +void *irq_context; };int irq_bypass_register_producer (struct irq_bypass_producer *);
virt/lib/irqbypass.c +61 -21
174 lines not on this page · raw message
Signature · 1 line
1.8.3.1
PATCH 3/5 VFIO: Support threaded interrupt handling on VFIO
In reply to Yunhong JiangFor VFIO device with MSI interrupt type, it's possible to handle the interrupt on hard interrupt context without invoking the interrupt thread. Handling the interrupt on hard interrupt context reduce the interrupt latency.
--- drivers/vfio/pci/vfio_pci_intrs.c | 39 ++++++++++++++++++++++++++++++++++----- 1 file changed, 34 insertions(+), 5 deletions(-)
drivers/vfio/pci/vfio_pci_intrs.c +34 -5
@@ -236,12 +236,35 @@ static void vfio_intx_disable(struct vfio_pci_device *vdev)kfree (vdev->ctx); } +static irqreturn_t vfio_msihandler (int irq,void *arg) +{ +struct vfio_pci_irq_ctx *ctx = arg; +struct irq_bypass_producer *producer = &ctx->producer; +struct irq_bypass_consumer *consumer; +int ret =IRQ_HANDLED , idx; + + idx =srcu_read_lock (&producer->srcu); + +list_for_each_entry_rcu (consumer, &producer->consumers, sibling) { +/* +* Invoke the thread handler if any consumer would block, but +* finish all consumes. +*/ +if (consumer->handle_irq (consumer->irq_context) == -EWOULDBLOCK ) + ret =IRQ_WAKE_THREAD ; +continue ; + } + +srcu_read_unlock (&producer->srcu, idx); +return ret; +} +/* * MSI/MSI-X */ -static irqreturn_t vfio_msihandler (int irq,void *arg) +static irqreturn_t vfio_msihandler_threaded (int irq,void *arg) { -struct eventfd_ctx *trigger = arg; +struct eventfd_ctx *trigger = ((struct vfio_pci_irq_ctx *)arg)->trigger;eventfd_signal (trigger,1 );return IRQ_HANDLED ;
@@ -318,7 +341,7 @@ static int vfio_msi_set_vector_signal(struct vfio_pci_device *vdev,return -EINVAL ;if (vdev->ctx[vector].trigger) { -free_irq (irq, vdev->ctx[vector].trigger); +free_irq (irq, &vdev->ctx[vector]);irq_bypass_unregister_producer (&vdev->ctx[vector].producer);kfree (vdev->ctx[vector].name);eventfd_ctx_put (vdev->ctx[vector].trigger);
@@ -353,8 +376,14 @@ static int vfio_msi_set_vector_signal(struct vfio_pci_device *vdev,pci_write_msi_msg (irq, &msg); } - ret =request_irq (irq, vfio_msihandler,0 , - vdev->ctx[vector].name, trigger); +/* +* Currently the primary handler for the thread_irq will be invoked on +* a thread, the IRQF_ONESHOT is a hack for it. +*/ + ret =request_threaded_irq (irq, vfio_msihandler, + vfio_msihandler_threaded, +IRQF_ONESHOT , vdev->ctx[vector].name, + &vdev->ctx[vector]);if (ret) {kfree (vdev->ctx[vector].name);eventfd_ctx_put (trigger);
Signature · 1 line
1.8.3.1
PATCH 4/5 KVM: Add the irq handling consumer
In reply to Yunhong JiangAdd an irq_bypass consumer to the KVM eventfd, so that when a MSI interrupt happens and triggerred from VFIO, it can be handled fast.
--- include/linux/kvm_irqfd.h | 1 + virt/kvm/eventfd.c | 42 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+)
include/linux/kvm_irqfd.h +1 -0
@@ -65,6 +65,7 @@ struct kvm_kernel_irqfd {poll_table pt;struct work_struct shutdown;struct irq_bypass_consumer consumer; +struct irq_bypass_consumer fastpath;struct irq_bypass_producer *producer; };
virt/kvm/eventfd.c +42 -0
66 lines not on this page · raw message
Signature · 1 line
1.8.3.1
PATCH 5/5 KVM: Expose x86 kvm_arch_set_irq_inatomic()
In reply to Yunhong JiangThe x86 support setting irq in atomic, expose it to vfio driver.
--- arch/x86/kvm/Kconfig | 1 + include/linux/kvm_host.h | 19 ++++++++++++++++--- virt/kvm/Kconfig | 3 +++ virt/kvm/eventfd.c | 9 --------- 4 files changed, 20 insertions(+), 12 deletions(-)
arch/x86/kvm/Kconfig +1 -0
@@ -30,6 +30,7 @@ config KVM select HAVE_KVM_IRQFD select IRQ_BYPASS_MANAGER select HAVE_KVM_IRQ_BYPASS + select KVM_SET_IRQ_INATOMIC select HAVE_KVM_IRQ_ROUTING select HAVE_KVM_EVENTFD select KVM_APIC_ARCHITECTURE
include/linux/kvm_host.h +16 -3
@@ -852,9 +852,6 @@ int kvm_set_irq(struct kvm *kvm, int irq_source_id, u32 irq, int level, bool line_status);int kvm_set_msi (struct kvm_kernel_irq_routing_entry *irq_entry,struct kvm *kvm,int irq_source_id,int level,bool line_status); -int kvm_arch_set_irq_inatomic (struct kvm_kernel_irq_routing_entry *e, -struct kvm *kvm,int irq_source_id, -int level,bool line_status);bool kvm_irq_has_notifier (struct kvm *kvm,unsigned irqchip,unsigned pin);void kvm_notify_acked_gsi (struct kvm *kvm,int gsi);void kvm_notify_acked_irq (struct kvm *kvm,unsigned irqchip,unsigned pin);
@@ -1207,4 +1204,20 @@ int kvm_arch_update_irqfd_routing(struct kvm *kvm, unsigned int host_irq, uint32_t guest_irq, bool set);#endif /* CONFIG_HAVE_KVM_IRQ_BYPASS */ +#ifndef CONFIG_KVM_SET_IRQ_INATOMIC +int __attribute__((weak))kvm_arch_set_irq_inatomic ( +struct kvm_kernel_irq_routing_entry *irq, +struct kvm *kvm,int irq_source_id, +int level, +bool line_status) +{ +return -EWOULDBLOCK ; +} +#else +extern int kvm_arch_set_irq_inatomic ( +struct kvm_kernel_irq_routing_entry *e, +struct kvm *kvm,int irq_source_id,int level, +bool line_status); +#endif +#endif
virt/kvm/Kconfig +3 -0
@@ -50,3 +50,6 @@ config KVM_COMPAT config HAVE_KVM_IRQ_BYPASS bool + +config KVM_SET_IRQ_INATOMIC + bool
virt/kvm/eventfd.c +0 -9
@@ -173,15 +173,6 @@ irqfd_deactivate(struct kvm_kernel_irqfd *irqfd)queue_work (irqfd_cleanup_wq, &irqfd->shutdown); } -int __attribute__((weak))kvm_arch_set_irq_inatomic ( -struct kvm_kernel_irq_routing_entry *irq, -struct kvm *kvm,int irq_source_id, -int level, -bool line_status) -{ -return -EWOULDBLOCK ; -} -static int irqfd_wakeup_pollin (struct kvm_kernel_irqfd *irqfd) {
Signature · 1 line
1.8.3.1
Quoted 4 lines · Yunhong Jiang
When assigning a VFIO device to a KVM guest with low latency requirement, it is better to handle the interrupt in the hard interrupt context, to reduce the context switch to/from the IRQ thread.
Quoted 8 lines
Based on discussion on https://lkml.org/lkml/2015/10/26/764, the VFIO msi interrupt is changed to use
request_threaded_irq(). The primary interrupt handler tries to set the guest interrupt atomically. If it fails to achieve it, a threaded interrupt handler will be invoked.The
irq_bypassmanager is extended for this purpose. The KVM eventfd will provide a irqbypass consumer to handle the interrupt at hard interrupt context. The producer will invoke the consumer's handler then.
Do you have any performance data? Thanks,
Alex
Quoted 10 lines · Alex Williamson
Quoted 9 lines · Yunhong Jiang
When assigning a VFIO device to a KVM guest with low latency requirement, it is better to handle the interrupt in the hard interrupt context, to reduce the context switch to/from the IRQ thread.
Based on discussion on https://lkml.org/lkml/2015/10/26/764, the VFIO msi interrupt is changed to use
request_threaded_irq(). The primary interrupt handler tries to set the guest interrupt atomically. If it fails to achieve it, a threaded interrupt handler will be invoked.
Quoted 5 lines
Quoted 3 lines
The
irq_bypassmanager is extended for this purpose. The KVM eventfd will provide a irqbypass consumer to handle the interrupt at hard interrupt context. The producer will invoke the consumer's handler then.Do you have any performance data? Thanks,
Sorry, I should include the data on the commit messages.
The test:
Launch a VM with a FPGA device, which triggers an interrpt every 1ms. The VM is launched on a isolated CPU with NONHZ_FULL enabled.
Two data are collected. On the guest, a special program will check the latency from the time the interrupt been triggered to the time the interrupt received by guest IRS. On the host, I use the perf to collect the perf data.
The performance Data with the patch:
Host perf data:
[root@otcnfv02 test-tools]# perf kvm stat record -C 34 sleep 10 [root@otcnfv02 test-tools]# perf kvm stat report Analyze events for all VMs, all VCPUs:
VM-EXIT Samples Samples% Time% Min Time Max Time
Avg time
EXTERNAL_INTERRUPT 9997 98.55% 99.31% 1.73us 17.07us
2.09us ( +- 0.21% )
PAUSE_INSTRUCTION 127 1.25% 0.51% 0.69us 1.20us
0.84us ( +- 1.34% )
MSR_WRITE 20 0.20% 0.18% 1.62us 3.21us
1.93us ( +- 3.95% )
Guest data:
[nfv@rt-test-1 ~]$ ./run-int-test.sh
Latency is Min: 3.74us Max: 20.08us Avg: 4.49us
No of interrupts = 74995
The performance data without the patch:
Host perf data:
[root@otcnfv02 test-tools]# perf kvm stat record -C 34 sleep 10 [root@otcnfv02 test-tools]# perf kvm stat report Analyze events for all VMs, all VCPUs:
VM-EXIT Samples Samples% Time% Min Time Max Time
Avg time
PAUSE_INSTRUCTION 141136 87.74% 50.39% 0.69us 8.51us
0.77us ( +- 0.07% )
EXTERNAL_INTERRUPT 19701 12.25% 49.59% 2.31us 15.93us
5.46us ( +- 0.12% )
MSR_WRITE 24 0.01% 0.02% 1.51us 2.22us
1.91us ( +- 1.91% )
Notice:
The EXTERNAL_INTERRUPT VMExit is different w/ the patch (9997) and w/o the patch (19701). It is because with threaded IRQ, the NOHZ_FULL it not working because two threads on the pCPU, thus we have both the FPGA device interrupt and the tick timer interrupt. After calculation, the average time for the FPGA device interrupt is 4.72 us.
Guest data:
[nfv@rt-test-1 ~]$ ./run-int-test.sh
Latency is Min: 6.70us Max: 50.38us Avg: 7.44us
No of interrupts = 42639
Thanks
--jyh
Quoted 3 lines
Alex
kbuild test robot
build botRe: [PATCH 4/5] KVM: Add the irq handling consumer
In reply to Yunhong Jiangreproduce: wget https://git.kernel.org/cgit/linux/kernel/git/wfg/lkp-tests.git/plain/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross # save the attached .config to linux build tree…
[if your patch is applied to the wrong git tree, please drop us a note to help improving the system] Hi Yunhong,
[auto build test ERROR on kvm/linux-next]
[also build test ERROR on v4.4-rc3 next-20151202]
url: https://github.com/0day-ci/linux/commits/Yunhong-Jiang/KVM-Extract-the-irqfd_wakeup_pollin-irqfd_wakeup_pollup/20151204-024034 base: https://git.kernel.org/pub/scm/virt/kvm/kvm.git linux-next config: arm64-allmodconfig (attached as .config)
reproduce:
wget https://git.kernel.org/cgit/linux/kernel/git/wfg/lkp-tests.git/plain/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# save the attached .config to linux build tree
make.cross ARCH=arm64
All errors (new ones prefixed by >>):
arch/arm64/kvm/built-in.o: In function `kvm_irqfd_assign':
>> :(.text+0x10a70): undefined reference to `irq_bypass_register_consumer' arch/arm64/kvm/built-in.o: In function `irqfd_shutdown': >> :(.text+0x10cd4): undefined reference to `irq_bypass_unregister_consumer'
---
0-DAY kernel test infrastructure Open Source Technology Center
https://lists.01.org/pipermail/kbuild-all Intel Corporation
Paolo Bonzini
maintainerAlex,
can you take a look at the extension to the irq bypass interface in patch 2? I'm not sure I understand what is the case where you have multiple consumers for the same token.
Paolo
Quoted 30 lines · Yunhong Jiang
When assigning a VFIO device to a KVM guest with low latency requirement, it is better to handle the interrupt in the hard interrupt context, to reduce the context switch to/from the IRQ thread.
Based on discussion on https://lkml.org/lkml/2015/10/26/764, the VFIO msi interrupt is changed to use
request_threaded_irq(). The primary interrupt handler tries to set the guest interrupt atomically. If it fails to achieve it, a threaded interrupt handler will be invoked.The
irq_bypassmanager is extended for this purpose. The KVM eventfd will provide a irqbypass consumer to handle the interrupt at hard interrupt context. The producer will invoke the consumer's handler then.Yunhong Jiang (5):
Extract the irqfd_wakeup_pollin/irqfd_wakeup_pollup Support runtimeirq_bypassconsumer
Support threaded interrupt handling on VFIO Add the irq handling consumer
Expose x86kvm_arch_set_irq_inatomic()arch/x86/kvm/Kconfig | 1 + drivers/vfio/pci/vfio_pci_intrs.c | 39 ++++++++++-- include/linux/irqbypass.h | 8 +++ include/linux/kvm_host.h | 19 +++++- include/linux/kvm_irqfd.h | 1 + virt/kvm/Kconfig | 3 + virt/kvm/eventfd.c | 131 ++++++++++++++++++++++++++------------ virt/lib/irqbypass.c | 82 ++++++++++++++++++------ 8 files changed, 214 insertions(+), 70 deletions(-)
Quoted 5 lines · Paolo Bonzini
Alex,
can you take a look at the extension to the irq bypass interface in patch 2? I'm not sure I understand what is the case where you have multiple consumers for the same token.
The consumers would be, for instance, Intel PI + the threaded handler added in this series. These run independently, the PI bypass simply makes the interrupt disappear from the host when it catches it, but if the vCPU isn't running in the right place at the time of the interrupt, it gets delivered to the host, in which case the secondary consumer implementing handle_irq() provides a lower latency injection than the eventfd path. If PI isn't supported, only this latter consumer is registered.
On the surface it seems like a reasonable solution, though having multiple consumers implementing handle_irq() seems problematic. Do we get multiple injections if we call them all? Should we have some way to prioritize one handler versus another? Perhaps KVM should have a single unified consumer that can provide that sort of logic, though we still need the srcu code added here to protect against registration and irq_handler() races. Thanks,
Alex
Quoted 39 lines · Yunhong Jiang
Quoted 38 lines
When assigning a VFIO device to a KVM guest with low latency requirement, it
is better to handle the interrupt in the hard interrupt context, to reduce
the context switch to/from the IRQ thread.Based on discussion on https://lkml.org/lkml/2015/10/26/764, the VFIO msi
interrupt is changed to userequest_threaded_irq(). The primary interrupt
handler tries to set the guest interrupt atomically. If it fails to achieve
it, a threaded interrupt handler will be invoked.The
irq_bypassmanager is extended for this purpose. The KVM eventfd will
provide a irqbypass consumer to handle the interrupt at hard interrupt
context. The producer will invoke the consumer's handler then.Yunhong Jiang (5):
Extract the irqfd_wakeup_pollin/irqfd_wakeup_pollup Support runtimeirq_bypassconsumer
Support threaded interrupt handling on VFIO Add the irq handling consumer
Expose x86kvm_arch_set_irq_inatomic()arch/x86/kvm/Kconfig | 1 +
drivers/vfio/pci/vfio_pci_intrs.c| 39 ++++++++++-- include/linux/irqbypass.h | 8 +++ include/linux/kvm_host.h | 19 +++++- include/linux/kvm_irqfd.h | 1 + virt/kvm/Kconfig | 3 + virt/kvm/eventfd.c | 131
++++++++++++++++++++++++++------------
virt/lib/irqbypass.c | 82 ++++++++++++++++++------8 files changed, 214 insertions(+), 70 deletions(-)
Re: [PATCH 2/5] VIRT: Support runtime irq_bypass consumer
In reply to Yunhong JiangQuoted 33 lines · Yunhong Jiang
Extend the
irq_bypassmanager to support runtime consumers. A runtimeirq_bypassconsumer can handle interrupt when an interrupt triggered. A runtime consumer has it'shandle_irq()function set and passing airq_contextfor the irq handling.A producer keep a link for the runtime consumers, so that it can invoke each consumer's
handle_irq()when irq invoked.Currently the
irq_bypassmanager has several code path assuming there is only one consumer/producer pair for each token. For example, when register the producer, it exits the loop after finding one match consumer. This is updated to support both static consumer (like for Posted Interrupt consumer) and runtime consumer.Signed-off-by Yunhong Jiang <[email protected]>---
include/linux/irqbypass.h | 8 +++++
virt/lib/irqbypass.c | 82 +++++++++++++++++++++++++++++++++++------------2 files changed, 69 insertions(+), 21 deletions(-)include/linux/irqbypass.h +0
-0@@ -12,6 +12,7 @@ #define IRQBYPASS_H
#include
+#include
structirq_bypass_consumer;
include/linux/irqbypass.h struct irq_bypass_consumer; · line 56
Quoted 9 lines
include/linux/irqbypass.h +3
-0@@ -47,6 +48,9 @@ struct irq_bypass_consumer;*/ struct irq_bypass_producer { struct list_head node; +/* Update side is synchronized by the lock on irqbypass.c */ +struct srcu_struct srcu; +struct list_head consumers; void *token; int irq; int (*add_consumer)(struct irq_bypass_producer *,
Documentation?
include/linux/irqbypass.h struct irq_bypass_producer { · line 71
Quoted 8 lines
include/linux/irqbypass.h +1
-0@@ -61,6 +65,7 @@ struct irq_bypass_producer { * struct irq_bypass_consumer - IRQ bypass consumer definition * @node: IRQ bypass manager private list management * @token: opaque token to match between producer and consumer + * @sibling: consumers with same token list management * @add_producer: Connect the IRQ consumer to an IRQ producer * @del_producer: Disconnect the IRQ consumer from an IRQ producer * @stop: Perform any quiesce operations necessary prior to add/del (optional)
What about @handle_irq and @irq_context?
include/linux/irqbypass.h struct irq_bypass_consumer { · line 89
Quoted 13 lines
include/linux/irqbypass.h +2
-0@@ -73,6 +78,7 @@ struct irq_bypass_producer {*/ struct irq_bypass_consumer { struct list_head node; +struct list_head sibling; void *token; int (*add_producer)(struct irq_bypass_consumer *, struct irq_bypass_producer *);@@ -80,6 +86,8 @@ struct irq_bypass_consumer { struct irq_bypass_producer *); void (*stop)(struct irq_bypass_consumer *); void (*start)(struct irq_bypass_consumer *); +int (*handle_irq)(void *arg);
If we called this with a pointer to the consumer, like the other functions, the consumer could embed arg (irq_context) into their own structure, or in this case, do a container_of and avoid storing the irqfd pointer entirely.
Quoted 4 lines
include/linux/irqbypass.h +1
-0+void *irq_context; };
intirq_bypass_register_producer(struct irq_bypass_producer *);
Re: [PATCH 3/5] VFIO: Support threaded interrupt handling on VFIO
In reply to Yunhong JiangQuoted 28 lines · Yunhong Jiang
For VFIO device with MSI interrupt type, it's possible to handle the interrupt on hard interrupt context without invoking the interrupt thread. Handling the interrupt on hard interrupt context reduce the interrupt latency.
Signed-off-by Yunhong Jiang <[email protected]>---
drivers/vfio/pci/vfio_pci_intrs.c | 39 ++++++++++++++++++++++++++++++++++-----1 file changed, 34 insertions(+), 5 deletions(-)drivers/vfio/pci/vfio_pci_intrs.c +0
-0@@ -236,12 +236,35 @@ static void vfio_intx_disable(struct vfio_pci_device *vdev) kfree(vdev->ctx); }drivers/vfio/pci/vfio_pci_intrs.c +10
-0+static irqreturn_t vfio_msihandler (int irq,void *arg) +{ +struct vfio_pci_irq_ctx *ctx = arg; +struct irq_bypass_producer *producer = &ctx->producer; +struct irq_bypass_consumer *consumer; +int ret =IRQ_HANDLED , idx; + + idx =srcu_read_lock (&producer->srcu); + +list_for_each_entry_rcu (consumer, &producer->consumers, sibling) {
drivers/vfio/pci/vfio_pci_intrs.c static void vfio_intx_disable(struct vfio_pci_device *vdev) · line 258
Quoted 10 lines
drivers/vfio/pci/vfio_pci_intrs.c +10
-0+/* +* Invoke the thread handler if any consumer would block, but +* finish all consumes. +*/ +if (consumer->handle_irq (consumer->irq_context) == -EWOULDBLOCK ) + ret =IRQ_WAKE_THREAD ; +continue ; + } + +srcu_read_unlock (&producer->srcu, idx);
There should be an irq bypass manager interface to abstract this.
Paolo Bonzini
maintainerQuoted 8 lines · Alex Williamson
The consumers would be, for instance, Intel PI + the threaded handler added in this series. These run independently, the PI bypass simply makes the interrupt disappear from the host when it catches it, but if the vCPU isn't running in the right place at the time of the interrupt, it gets delivered to the host, in which case the secondary consumer implementing
handle_irq()provides a lower latency injection than the eventfd path. If PI isn't supported, only this latter consumer is registered.
I would implement the two in a single consumer, knowing that only one of the two parts would effectively run. But because of the possibility of multiple consumers implementing handle_irq(), I am not sure if this is feasible.
Quoted 3 lines
On the surface it seems like a reasonable solution, though having multiple consumers implementing
handle_irq()seems problematic. Do we get multiple injections if we call them all?
Indeed.
Quoted 5 lines
Should we have some way
to prioritize one handler versus another? Perhaps KVM should have a single unified consumer that can provide that sort of logic, though we still need the srcu code added here to protect against registration andirq_handler()races. Thanks,
I'm happy to see that we have the same doubts. :)
Paolo
Quoted 7 lines · Alex Williamson
Quoted 5 lines · Paolo Bonzini
Alex,
can you take a look at the extension to the irq bypass interface in patch 2? I'm not sure I understand what is the case where you have multiple consumers for the same token.
Quoted 6 lines
The consumers would be, for instance, Intel PI + the threaded handler added in this series. These run independently, the PI bypass simply makes the interrupt disappear from the host when it catches it, but if the vCPU isn't running in the right place at the time of the interrupt, it gets delivered to the host, in which case the secondary consumer implementing
handle_irq()provides a lower latency injection than the
Sorry for slow response.
If the PI is delivered to the host because guest is not running, I think it will not trigger the secondary consumer. The reason is, with PI, the interrupt will be delivered as the POSTED_INTR_VECTOR or POSTED_INTR_WAKEUP_VECTOR. So for the PI consumer will not be invoked on run time scenario.
Quoted 5 lines
eventfd path. If PI isn't supported, only this latter consumer is registered.
On the surface it seems like a reasonable solution, though having multiple consumers implementing
handle_irq()seems problematic. Do we
Yes, agree that has multiple consumers implementing handle_irq() seems not good. But I do think it can be helpful. A naive case is, a consumer can be created to log all the interrupt event, or to create a pipe for analysis.
Quoted 1 line
get multiple injections if we call them all? Should we have some way
As discussed above, currently I think we have only one consumer to handle_irq(), so it should be ok? We can limit the framework to support only one consumer with handle_irq()?
Quoted 2 lines
to prioritize one handler versus another? Perhaps KVM should have a single unified consumer that can provide that sort of logic, though we
I'd think still different consumer for the PI and this fast_IRQ.
Thanks
--jyh
Quoted 45 lines
still need the srcu code added here to protect against registration and
irq_handler()races. Thanks,Alex
Quoted 39 lines · Yunhong Jiang
Quoted 38 lines
When assigning a VFIO device to a KVM guest with low latency requirement, it
is better to handle the interrupt in the hard interrupt context, to reduce
the context switch to/from the IRQ thread.Based on discussion on https://lkml.org/lkml/2015/10/26/764, the VFIO msi
interrupt is changed to userequest_threaded_irq(). The primary interrupt
handler tries to set the guest interrupt atomically. If it fails to achieve
it, a threaded interrupt handler will be invoked.The
irq_bypassmanager is extended for this purpose. The KVM eventfd will
provide a irqbypass consumer to handle the interrupt at hard interrupt
context. The producer will invoke the consumer's handler then.Yunhong Jiang (5):
Extract the irqfd_wakeup_pollin/irqfd_wakeup_pollup Support runtimeirq_bypassconsumer
Support threaded interrupt handling on VFIO Add the irq handling consumer
Expose x86kvm_arch_set_irq_inatomic()arch/x86/kvm/Kconfig | 1 +
drivers/vfio/pci/vfio_pci_intrs.c| 39 ++++++++++-- include/linux/irqbypass.h | 8 +++ include/linux/kvm_host.h | 19 +++++- include/linux/kvm_irqfd.h | 1 + virt/kvm/Kconfig | 3 + virt/kvm/eventfd.c | 131
++++++++++++++++++++++++++------------
virt/lib/irqbypass.c | 82 ++++++++++++++++++------8 files changed, 214 insertions(+), 70 deletions(-)
Quoted 12 lines · Paolo Bonzini
Quoted 8 lines · Alex Williamson
The consumers would be, for instance, Intel PI + the threaded handler added in this series. These run independently, the PI bypass simply makes the interrupt disappear from the host when it catches it, but if the vCPU isn't running in the right place at the time of the interrupt, it gets delivered to the host, in which case the secondary consumer implementing
handle_irq()provides a lower latency injection than the eventfd path. If PI isn't supported, only this latter consumer is registered.
Quoted 4 lines
I would implement the two in a single consumer, knowing that only one of the two parts would effectively run. But because of the possibility of multiple consumers implementing
handle_irq(), I am not sure if this is feasible.
So is it possible that we limit only one consumer with handle_irq(), as my previous response to Alex? We can extend it in future if we do need support multiple consumder implementing handle_irq()?
Thanks
--jyh
Quoted 21 lines
Quoted 4 lines
On the surface it seems like a reasonable solution, though having multiple consumers implementing
handle_irq()seems problematic. Do we get multiple injections if we call them all?Indeed.
Quoted 5 lines
Should we have some way
to prioritize one handler versus another? Perhaps KVM should have a single unified consumer that can provide that sort of logic, though we still need the srcu code added here to protect against registration andirq_handler()races. Thanks,I'm happy to see that we have the same doubts. :)
Paolo
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/