irqbypass: Cleanups and a perf improvement
Subsystems (9)
Sean Christopherson
maintainerv2 cover irqbypass: Cleanups and a perf improvement
The two primary goals of this series are to make the irqbypass concept easier to understand, and to address the terrible performance that can result from using a list to track connections.
For the first goal, track the producer/consumer "tokens" as eventfd context pointers instead of opaque "void *". Supporting arbitrary token types was dead infrastructure when it was added 10 years ago, and nothing has changed since. Taking an opaque token makes a very simple concept (device signals eventfd; KVM listens to eventfd) unnecessarily difficult to understand.
Burying that simple behind a layer of obfuscation also makes the overall code more brittle, as callers can pass in literally anything. I.e. passing in a token that will never be paired would go unnoticed.
For the performance issue, use an xarray. I'm definitely not wedded to an xarray, but IMO it doesn't add meaningful complexity (even requires less code), and pretty much Just Works. Like tried this a while back[1], but the implementation had undesirable behavior changes and stalled out.
Note, I want to do more aggressive cleanups of irqbypass at some point, e.g. not reporting an error to userspace if connect() fails is awful behavior for environments that want/need irqbypass to always work. And KVM shold probably have a KVM_IRQFD_FLAG_NO_IRQBYPASS if a VM is never going to use device posted interrupts. But those are future problems.
v2:
- Collect reviews. [Kevin, Michael]
- Track the pointer as "struct eventfd_ctx *eventfd" instead of "void *token". [Alex]
- Fix typos and stale comments. [Kevin, Binbin]
- Use "trigger" instead of the null token/eventfd pointer on failure in vfio_msi_set_vector_signal(). [Kevin]
- Drop a redundant "tmp == consumer" check from patch 3. [Kevin]
- Require producers to pass in the line IRQ number.
v1: https://lore.kernel.org/all/[email protected]
[1] https://lore.kernel.org/all/[email protected]
[2] https://lore.kernel.org/all/[email protected]
Sean Christopherson (8):
irqbypass: Drop pointless and misleading THIS_MODULE get/put
irqbypass: Drop superfluous might_sleep() annotations
irqbypass: Take ownership of producer/consumer token tracking
irqbypass: Explicitly track producer and consumer bindings
irqbypass: Use paired consumer/producer to disconnect during
unregister
irqbypass: Use guard(mutex) in lieu of manual lock+unlock
irqbypass: Use xarray to track producers and consumers
irqbypass: Require producers to pass in Linux IRQ number during
registration
arch/x86/kvm/x86.c | 4 +- drivers/vfio/pci/vfio_pci_intrs.c | 10 +- drivers/vhost/vdpa.c | 10 +- include/linux/irqbypass.h | 46 ++++---- virt/kvm/eventfd.c | 7 +- virt/lib/irqbypass.c | 190 +++++++++++------------------- 6 files changed, 107 insertions(+), 160 deletions(-)
base-commit: 7ef51a41466bc846ad794d505e2e34ff97157f7f
Signature · 1 line
2.49.0.1112.g889b7c5bd8-goog
Sean Christopherson
maintainerPATCH v2 1/8 irqbypass: Drop pointless and misleading THIS_MODULE get/put
Drop irqbypass.ko's superfluous and misleading get/put calls on THIS_MODULE. A module taking a reference to itself is useless; no amount of checks will prevent doom and destruction if the caller hasn't already guaranteed the liveliness of the module (this goes for any module). E.g. if try_module_get() fails because irqbypass.ko is being unloaded, then the kernel has already hit a use-after-free by virtue of executing code whose lifecycle is tied to irqbypass.ko.
--- virt/lib/irqbypass.c | 20 -------------------- 1 file changed, 20 deletions(-)
virt/lib/irqbypass.c +0 -20
@@ -92,9 +92,6 @@ int irq_bypass_register_producer(struct irq_bypass_producer *producer)might_sleep (); -if (!try_module_get (THIS_MODULE )) -return -ENODEV ; -mutex_lock (&lock);list_for_each_entry (tmp, &producers, node) {
@@ -120,7 +117,6 @@ int irq_bypass_register_producer(struct irq_bypass_producer *producer)return 0 ; out_err:mutex_unlock (&lock); -module_put (THIS_MODULE );return ret; }EXPORT_SYMBOL_GPL (irq_bypass_register_producer);
@@ -142,9 +138,6 @@ void irq_bypass_unregister_producer(struct irq_bypass_producer *producer)might_sleep (); -if (!try_module_get (THIS_MODULE )) -return ;/* nothing in the list anyway */ -mutex_lock (&lock);list_for_each_entry (tmp, &producers, node) {
@@ -159,13 +152,10 @@ void irq_bypass_unregister_producer(struct irq_bypass_producer *producer) }list_del (&producer->node); -module_put (THIS_MODULE );break ; }mutex_unlock (&lock); - -module_put (THIS_MODULE ); }EXPORT_SYMBOL_GPL (irq_bypass_unregister_producer);
@@ -188,9 +178,6 @@ int irq_bypass_register_consumer(struct irq_bypass_consumer *consumer)might_sleep (); -if (!try_module_get (THIS_MODULE )) -return -ENODEV ; -mutex_lock (&lock);list_for_each_entry (tmp, &consumers, node) {
@@ -216,7 +203,6 @@ int irq_bypass_register_consumer(struct irq_bypass_consumer *consumer)return 0 ; out_err:mutex_unlock (&lock); -module_put (THIS_MODULE );return ret; }EXPORT_SYMBOL_GPL (irq_bypass_register_consumer);
@@ -238,9 +224,6 @@ void irq_bypass_unregister_consumer(struct irq_bypass_consumer *consumer)might_sleep (); -if (!try_module_get (THIS_MODULE )) -return ;/* nothing in the list anyway */ -mutex_lock (&lock);list_for_each_entry (tmp, &consumers, node) {
@@ -255,12 +238,9 @@ void irq_bypass_unregister_consumer(struct irq_bypass_consumer *consumer) }list_del (&consumer->node); -module_put (THIS_MODULE );break ; }mutex_unlock (&lock); - -module_put (THIS_MODULE ); }EXPORT_SYMBOL_GPL (irq_bypass_unregister_consumer);
Signature · 1 line
2.49.0.1112.g889b7c5bd8-goog
Sean Christopherson
maintainerPATCH v2 2/8 irqbypass: Drop superfluous might_sleep() annotations
In reply to Sean ChristophersonDrop superfluous might_sleep() annotations from irqbypass, mutex_lock() provides all of the necessary tracking.
--- virt/lib/irqbypass.c | 8 -------- 1 file changed, 8 deletions(-)
virt/lib/irqbypass.c +0 -8
@@ -90,8 +90,6 @@ int irq_bypass_register_producer(struct irq_bypass_producer *producer)if (!producer->token)return -EINVAL ; -might_sleep (); -mutex_lock (&lock);list_for_each_entry (tmp, &producers, node) {
@@ -136,8 +134,6 @@ void irq_bypass_unregister_producer(struct irq_bypass_producer *producer)if (!producer->token)return ; -might_sleep (); -mutex_lock (&lock);list_for_each_entry (tmp, &producers, node) {
@@ -176,8 +172,6 @@ int irq_bypass_register_consumer(struct irq_bypass_consumer *consumer) !consumer->add_producer || !consumer->del_producer)return -EINVAL ; -might_sleep (); -mutex_lock (&lock);list_for_each_entry (tmp, &consumers, node) {
@@ -222,8 +216,6 @@ void irq_bypass_unregister_consumer(struct irq_bypass_consumer *consumer)if (!consumer->token)return ; -might_sleep (); -mutex_lock (&lock);list_for_each_entry (tmp, &consumers, node) {
Signature · 1 line
2.49.0.1112.g889b7c5bd8-goog
Sean Christopherson
maintainerPATCH v2 3/8 irqbypass: Take ownership of producer/consumer token tracking
In reply to Sean ChristophersonMove ownership of IRQ bypass token tracking into irqbypass.ko, and explicitly require callers to pass an eventfd_ctx structure instead of a completely opaque token. Relying on producers and consumers to set the token appropriately is error prone, and hiding the fact that the token must be an eventfd_ctx pointer (for all intents and purposes) unnecessarily obfuscates the code and makes it more brittle.
--- arch/x86/kvm/x86.c | 4 +-- drivers/vfio/pci/vfio_pci_intrs.c | 9 +++---- drivers/vhost/vdpa.c | 8 +++--- include/linux/irqbypass.h | 35 +++++++++++++----------- virt/kvm/eventfd.c | 7 +++-- virt/lib/irqbypass.c | 44 ++++++++++++++++++++----------- 6 files changed, 58 insertions(+), 49 deletions(-)
arch/x86/kvm/x86.c +2 -2
@@ -13667,8 +13667,8 @@ void kvm_arch_irq_bypass_del_producer(struct irq_bypass_consumer *cons, ret =kvm_x86_call (pi_update_irte)(irqfd->kvm, prod->irq, irqfd->gsi,0 );if (ret) -printk (KERN_INFO "irq bypass consumer (token %p) unregistration" -" fails: %d\n" , irqfd->consumer.token, ret); +printk (KERN_INFO "irq bypass consumer (eventfd %p) unregistration" +" fails: %d\n" , irqfd->consumer.eventfd, ret);spin_unlock_irq (&kvm->irqfds.lock);
drivers/vfio/pci/vfio_pci_intrs.c +3 -6
@@ -505,15 +505,12 @@ static int vfio_msi_set_vector_signal(struct vfio_pci_core_device *vdev,if (ret) goto out_put_eventfd_ctx; - ctx->producer.token = trigger; ctx->producer.irq = irq; - ret =irq_bypass_register_producer (&ctx->producer); + ret =irq_bypass_register_producer (&ctx->producer, trigger);if (unlikely (ret)) {dev_info (&pdev->dev, -"irq bypass producer (token %p) registration fails: %d\n" , - ctx->producer.token, ret); - - ctx->producer.token =NULL ; +"irq bypass producer (eventfd %p) registration fails: %d\n" , + trigger, ret); } ctx->trigger = trigger;
drivers/vhost/vdpa.c +3 -5
@@ -213,10 +213,10 @@ static void vhost_vdpa_setup_vq_irq(struct vhost_vdpa *v, u16 qid)return ; vq->call_ctx.producer.irq = irq; - ret =irq_bypass_register_producer (&vq->call_ctx.producer); + ret =irq_bypass_register_producer (&vq->call_ctx.producer, vq->call_ctx.ctx);if (unlikely (ret)) -dev_info (&v->dev,"vq %u, irq bypass producer (token %p) registration fails, ret = %d\n" , - qid, vq->call_ctx.producer.token, ret); +dev_info (&v->dev,"vq %u, irq bypass producer (eventfd %p) registration fails, ret = %d\n" , + qid, vq->call_ctx.ctx, ret); }static void vhost_vdpa_unsetup_vq_irq (struct vhost_vdpa *v,u16 qid)
@@ -712,7 +712,6 @@ static long vhost_vdpa_vring_ioctl(struct vhost_vdpa *v, unsigned int cmd,if (ops->get_status (vdpa) &VIRTIO_CONFIG_S_DRIVER_OK )vhost_vdpa_unsetup_vq_irq (v, idx); - vq->call_ctx.producer.token =NULL ; }break ; }
@@ -753,7 +752,6 @@ static long vhost_vdpa_vring_ioctl(struct vhost_vdpa *v, unsigned int cmd, cb.callback = vhost_vdpa_virtqueue_cb; cb.private = vq; cb.trigger = vq->call_ctx.ctx; - vq->call_ctx.producer.token = vq->call_ctx.ctx;if (ops->get_status (vdpa) &VIRTIO_CONFIG_S_DRIVER_OK )vhost_vdpa_setup_vq_irq (v, idx);
include/linux/irqbypass.h +19 -16
@@ -10,6 +10,7 @@#include <linux/list.h> +struct eventfd_ctx ;struct irq_bypass_consumer ;/*
@@ -18,20 +19,20 @@ struct irq_bypass_consumer;* The IRQ bypass manager is a simple set of lists and callbacks that allows * IRQ producers (ex. physical interrupt sources) to be matched to IRQ * consumers (ex. virtualization hardware that allows IRQ bypass or offload) -* via a shared token (ex. eventfd_ctx). Producers and consumers register -* independently. When a token match is found, the optional @stop callback -* will be called for each participant. The pair will then be connected via -* the @add_* callbacks, and finally the optional @start callback will allow -* any final coordination. When either participant is unregistered, the -* process is repeated using the @del_* callbacks in place of the @add_* -* callbacks. Match tokens must be unique per producer/consumer, 1:N pairings -* are not supported. +* via a shared eventfd_ctx. Producers and consumers register independently. +* When a producer and consumer are paired, i.e. an eventfd match is found, the +* optional @stop callback will be called for each participant. The pair will +* then be connected via the @add_* callbacks, and finally the optional @start +* callback will allow any final coordination. When either participant is +* unregistered, the process is repeated using the @del_* callbacks in place of +* the @add_* callbacks. eventfds must be unique per producer/consumer, 1:N +* pairings are not supported. */ /** * struct irq_bypass_producer - IRQ bypass producer definition * @node: IRQ bypass manager private list management -* @token: opaque token to match between producer and consumer (non-NULL) +* @eventfd: eventfd context used to match producers and consumers * @irq: Linux IRQ number for the producer device * @add_consumer: Connect the IRQ producer to an IRQ consumer (optional) * @del_consumer: Disconnect the IRQ producer from an IRQ consumer (optional)
@@ -44,7 +45,7 @@ struct irq_bypass_consumer;*/ struct irq_bypass_producer {struct list_head node; -void *token; +struct eventfd_ctx *eventfd;int irq;int (*add_consumer)(struct irq_bypass_producer *,struct irq_bypass_consumer *);
@@ -57,7 +58,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 (non-NULL) +* @eventfd: eventfd context used to match producers and consumers * @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)
@@ -70,7 +71,7 @@ struct irq_bypass_producer {*/ struct irq_bypass_consumer {struct list_head node; -void *token; +struct eventfd_ctx *eventfd;int (*add_producer)(struct irq_bypass_consumer *,struct irq_bypass_producer *); void (*del_producer)(struct irq_bypass_consumer *,
@@ -79,9 +80,11 @@ struct irq_bypass_consumer { void (*start)(struct irq_bypass_consumer *); }; -int irq_bypass_register_producer (struct irq_bypass_producer *); -void irq_bypass_unregister_producer (struct irq_bypass_producer *); -int irq_bypass_register_consumer (struct irq_bypass_consumer *); -void irq_bypass_unregister_consumer (struct irq_bypass_consumer *); +int irq_bypass_register_producer (struct irq_bypass_producer *producer, +struct eventfd_ctx *eventfd); +void irq_bypass_unregister_producer (struct irq_bypass_producer *producer); +int irq_bypass_register_consumer (struct irq_bypass_consumer *consumer, +struct eventfd_ctx *eventfd); +void irq_bypass_unregister_consumer (struct irq_bypass_consumer *consumer);#endif /* IRQBYPASS_H */
virt/kvm/eventfd.c +3 -4
@@ -426,15 +426,14 @@ kvm_irqfd_assign(struct kvm *kvm, struct kvm_irqfd *args)#if IS_ENABLED (CONFIG_HAVE_KVM_IRQ_BYPASS )if (kvm_arch_has_irq_bypass ()) { - irqfd->consumer.token = (void *)irqfd->eventfd; irqfd->consumer.add_producer = kvm_arch_irq_bypass_add_producer; irqfd->consumer.del_producer = kvm_arch_irq_bypass_del_producer; irqfd->consumer.stop = kvm_arch_irq_bypass_stop; irqfd->consumer.start = kvm_arch_irq_bypass_start; - ret =irq_bypass_register_consumer (&irqfd->consumer); + ret =irq_bypass_register_consumer (&irqfd->consumer, irqfd->eventfd);if (ret) -pr_info ("irq bypass consumer (token %p) registration fails: %d\n" , - irqfd->consumer.token, ret); +pr_info ("irq bypass consumer (eventfd %p) registration fails: %d\n" , + irqfd->eventfd, ret); }#endif
virt/lib/irqbypass.c +28 -16
150 lines not on this page · raw message
Signature · 1 line
2.49.0.1112.g889b7c5bd8-goog
Sean Christopherson
maintainerPATCH v2 4/8 irqbypass: Explicitly track producer and consumer bindings
In reply to Sean ChristophersonExplicitly track IRQ bypass producer:consumer bindings. This will allow making removal an O(1) operation; searching through the list to find information that is trivially tracked (and useful for debug) is wasteful.
--- include/linux/irqbypass.h | 7 +++++++ virt/lib/irqbypass.c | 9 +++++++++ 2 files changed, 16 insertions(+)
include/linux/irqbypass.h +7 -0
@@ -29,10 +29,13 @@ struct irq_bypass_consumer;* pairings are not supported. */ +struct irq_bypass_consumer ; +/** * struct irq_bypass_producer - IRQ bypass producer definition * @node: IRQ bypass manager private list management * @eventfd: eventfd context used to match producers and consumers +* @consumer: The connected consumer (NULL if no connection) * @irq: Linux IRQ number for the producer device * @add_consumer: Connect the IRQ producer to an IRQ consumer (optional) * @del_consumer: Disconnect the IRQ producer from an IRQ consumer (optional)
@@ -46,6 +49,7 @@ struct irq_bypass_consumer;struct irq_bypass_producer {struct list_head node;struct eventfd_ctx *eventfd; +struct irq_bypass_consumer *consumer;int irq;int (*add_consumer)(struct irq_bypass_producer *,struct irq_bypass_consumer *);
@@ -59,6 +63,7 @@ struct irq_bypass_producer {* struct irq_bypass_consumer - IRQ bypass consumer definition * @node: IRQ bypass manager private list management * @eventfd: eventfd context used to match producers and consumers +* @producer: The connected producer (NULL if no connection) * @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)
@@ -72,6 +77,8 @@ struct irq_bypass_producer {struct irq_bypass_consumer {struct list_head node;struct eventfd_ctx *eventfd; +struct irq_bypass_producer *producer; +int (*add_producer)(struct irq_bypass_consumer *,struct irq_bypass_producer *); void (*del_producer)(struct irq_bypass_consumer *,
virt/lib/irqbypass.c +9 -0
@@ -51,6 +51,10 @@ static int __connect(struct irq_bypass_producer *prod,if (prod->start) prod->start (prod); +if (!ret) { + prod->consumer = cons; + cons->producer = prod; + }return ret; }
@@ -72,6 +76,9 @@ static void __disconnect(struct irq_bypass_producer *prod, cons->start (cons);if (prod->start) prod->start (prod); + + prod->consumer =NULL ; + cons->producer =NULL ; }/**
@@ -145,6 +152,7 @@ void irq_bypass_unregister_producer(struct irq_bypass_producer *producer)list_for_each_entry (consumer, &consumers, node) {if (consumer->eventfd == producer->eventfd) { +WARN_ON_ONCE (producer->consumer != consumer);__disconnect (producer, consumer);break ; }
@@ -234,6 +242,7 @@ void irq_bypass_unregister_consumer(struct irq_bypass_consumer *consumer)list_for_each_entry (producer, &producers, node) {if (producer->eventfd == consumer->eventfd) { +WARN_ON_ONCE (consumer->producer != producer);__disconnect (producer, consumer);break ; }
Signature · 1 line
2.49.0.1112.g889b7c5bd8-goog
Sean Christopherson
maintainerPATCH v2 5/8 irqbypass: Use paired consumer/producer to disconnect during unregister
In reply to Sean ChristophersonUse the paired consumer/producer information to disconnect IRQ bypass producers/consumers in O(1) time (ignoring the cost of __disconnect()).
--- virt/lib/irqbypass.c | 48 ++++++++------------------------------------ 1 file changed, 8 insertions(+), 40 deletions(-)
virt/lib/irqbypass.c +8 -40
@@ -138,32 +138,16 @@ EXPORT_SYMBOL_GPL(irq_bypass_register_producer);*/ void irq_bypass_unregister_producer (struct irq_bypass_producer *producer) { -struct irq_bypass_producer *tmp; -struct irq_bypass_consumer *consumer; -if (!producer->eventfd)return ;mutex_lock (&lock); -list_for_each_entry (tmp, &producers, node) { -if (tmp->eventfd != producer->eventfd) -continue ; +if (producer->consumer) +__disconnect (producer, producer->consumer); -list_for_each_entry (consumer, &consumers, node) { -if (consumer->eventfd == producer->eventfd) { -WARN_ON_ONCE (producer->consumer != consumer); -__disconnect (producer, consumer); -break ; - } - } - - producer->eventfd =NULL ; -list_del (&producer->node); -break ; - } - -WARN_ON_ONCE (producer->eventfd); + producer->eventfd =NULL ; +list_del (&producer->node);mutex_unlock (&lock); }EXPORT_SYMBOL_GPL (irq_bypass_unregister_producer);
@@ -228,32 +212,16 @@ EXPORT_SYMBOL_GPL(irq_bypass_register_consumer);*/ void irq_bypass_unregister_consumer (struct irq_bypass_consumer *consumer) { -struct irq_bypass_consumer *tmp; -struct irq_bypass_producer *producer; -if (!consumer->eventfd)return ;mutex_lock (&lock); -list_for_each_entry (tmp, &consumers, node) { -if (tmp != consumer) -continue ; +if (consumer->producer) +__disconnect (consumer->producer, consumer); -list_for_each_entry (producer, &producers, node) { -if (producer->eventfd == consumer->eventfd) { -WARN_ON_ONCE (consumer->producer != producer); -__disconnect (producer, consumer); -break ; - } - } - - consumer->eventfd =NULL ; -list_del (&consumer->node); -break ; - } - -WARN_ON_ONCE (consumer->eventfd); + consumer->eventfd =NULL ; +list_del (&consumer->node);mutex_unlock (&lock); }EXPORT_SYMBOL_GPL (irq_bypass_unregister_consumer);
Signature · 1 line
2.49.0.1112.g889b7c5bd8-goog
Sean Christopherson
maintainerPATCH v2 6/8 irqbypass: Use guard(mutex) in lieu of manual lock+unlock
In reply to Sean ChristophersonUse guard(mutex) to clean up irqbypass's error handling.
--- virt/lib/irqbypass.c | 38 ++++++++++---------------------------- 1 file changed, 10 insertions(+), 28 deletions(-)
virt/lib/irqbypass.c +10 -28
@@ -99,33 +99,25 @@ int irq_bypass_register_producer(struct irq_bypass_producer *producer,if (WARN_ON_ONCE (producer->eventfd))return -EINVAL ; -mutex_lock (&lock); +guard (mutex)(&lock);list_for_each_entry (tmp, &producers, node) { -if (tmp->eventfd == eventfd) { - ret = -EBUSY ; - goto out_err; - } +if (tmp->eventfd == eventfd) +return -EBUSY ; }list_for_each_entry (consumer, &consumers, node) {if (consumer->eventfd == eventfd) { ret =__connect (producer, consumer);if (ret) - goto out_err; +return ret;break ; } } producer->eventfd = eventfd;list_add (&producer->node, &producers); - -mutex_unlock (&lock); -return 0 ; -out_err: -mutex_unlock (&lock); -return ret; }EXPORT_SYMBOL_GPL (irq_bypass_register_producer);
@@ -141,14 +133,13 @@ void irq_bypass_unregister_producer(struct irq_bypass_producer *producer)if (!producer->eventfd)return ; -mutex_lock (&lock); +guard (mutex)(&lock);if (producer->consumer)__disconnect (producer, producer->consumer); producer->eventfd =NULL ;list_del (&producer->node); -mutex_unlock (&lock); }EXPORT_SYMBOL_GPL (irq_bypass_unregister_producer);
@@ -173,33 +164,25 @@ int irq_bypass_register_consumer(struct irq_bypass_consumer *consumer,if (!consumer->add_producer || !consumer->del_producer)return -EINVAL ; -mutex_lock (&lock); +guard (mutex)(&lock);list_for_each_entry (tmp, &consumers, node) { -if (tmp->eventfd == eventfd) { - ret = -EBUSY ; - goto out_err; - } +if (tmp->eventfd == eventfd) +return -EBUSY ; }list_for_each_entry (producer, &producers, node) {if (producer->eventfd == eventfd) { ret =__connect (producer, consumer);if (ret) - goto out_err; +return ret;break ; } } consumer->eventfd = eventfd;list_add (&consumer->node, &consumers); - -mutex_unlock (&lock); -return 0 ; -out_err: -mutex_unlock (&lock); -return ret; }EXPORT_SYMBOL_GPL (irq_bypass_register_consumer);
@@ -215,13 +198,12 @@ void irq_bypass_unregister_consumer(struct irq_bypass_consumer *consumer)if (!consumer->eventfd)return ; -mutex_lock (&lock); +guard (mutex)(&lock);if (consumer->producer)__disconnect (consumer->producer, consumer); consumer->eventfd =NULL ;list_del (&consumer->node); -mutex_unlock (&lock); }EXPORT_SYMBOL_GPL (irq_bypass_unregister_consumer);
Signature · 1 line
2.49.0.1112.g889b7c5bd8-goog
Sean Christopherson
maintainerPATCH v2 7/8 irqbypass: Use xarray to track producers and consumers
In reply to Sean ChristophersonTrack IRQ bypass producers and consumers using an xarray to avoid the O(2n) insertion time associated with walking a list to check for duplicate entries, and to search for an partner.
At low (tens or few hundreds) total producer/consumer counts, using a list is faster due to the need to allocate backing storage for xarray. But as count creeps into the thousands, xarray wins easily, and can provide several orders of magnitude better latency at high counts. E.g. hundreds of nanoseconds vs. hundreds of milliseconds.
--- include/linux/irqbypass.h | 4 --- virt/lib/irqbypass.c | 74 ++++++++++++++++++++------------------- 2 files changed, 38 insertions(+), 40 deletions(-)
include/linux/irqbypass.h +0 -4
@@ -33,7 +33,6 @@ struct irq_bypass_consumer;/** * struct irq_bypass_producer - IRQ bypass producer definition -* @node: IRQ bypass manager private list management * @eventfd: eventfd context used to match producers and consumers * @consumer: The connected consumer (NULL if no connection) * @irq: Linux IRQ number for the producer device
@@ -47,7 +46,6 @@ struct irq_bypass_consumer;* for a physical device assigned to a VM. */ struct irq_bypass_producer { -struct list_head node;struct eventfd_ctx *eventfd;struct irq_bypass_consumer *consumer;int irq;
@@ -61,7 +59,6 @@ struct irq_bypass_producer {/** * struct irq_bypass_consumer - IRQ bypass consumer definition -* @node: IRQ bypass manager private list management * @eventfd: eventfd context used to match producers and consumers * @producer: The connected producer (NULL if no connection) * @add_producer: Connect the IRQ consumer to an IRQ producer
@@ -75,7 +72,6 @@ struct irq_bypass_producer {* portions of the interrupt handling to the VM. */ struct irq_bypass_consumer { -struct list_head node;struct eventfd_ctx *eventfd;struct irq_bypass_producer *producer;
virt/lib/irqbypass.c +38 -36
153 lines not on this page · raw message
Signature · 1 line
2.49.0.1112.g889b7c5bd8-goog
Sean Christopherson
maintainerPATCH v2 8/8 irqbypass: Require producers to pass in Linux IRQ number during registration
In reply to Sean ChristophersonPass in the Linux IRQ associated with an IRQ bypass producer instead of relying on the caller to set the field prior to registration, as there's no benefit to relying on callers to do the right thing.
Take care to set producer->irq before __connect(), as KVM expects the IRQ to be valid as soon as a connection is possible.
--- drivers/vfio/pci/vfio_pci_intrs.c | 3 +-- drivers/vhost/vdpa.c | 4 ++-- include/linux/irqbypass.h | 2 +- virt/lib/irqbypass.c | 5 ++++- 4 files changed, 8 insertions(+), 6 deletions(-)
drivers/vfio/pci/vfio_pci_intrs.c +1 -2
@@ -505,8 +505,7 @@ static int vfio_msi_set_vector_signal(struct vfio_pci_core_device *vdev,if (ret) goto out_put_eventfd_ctx; - ctx->producer.irq = irq; - ret =irq_bypass_register_producer (&ctx->producer, trigger); + ret =irq_bypass_register_producer (&ctx->producer, trigger, irq);if (unlikely (ret)) { dev_info(&pdev->dev, "irq bypass producer (eventfd %p) registration fails: %d\n",
drivers/vhost/vdpa.c +2 -2
@@ -212,8 +212,8 @@ static void vhost_vdpa_setup_vq_irq(struct vhost_vdpa *v, u16 qid)if (!vq->call_ctx.ctx)return ; - vq->call_ctx.producer.irq = irq; - ret =irq_bypass_register_producer (&vq->call_ctx.producer, vq->call_ctx.ctx); + ret =irq_bypass_register_producer (&vq->call_ctx.producer, + vq->call_ctx.ctx, irq);if (unlikely (ret))dev_info (&v->dev,"vq %u, irq bypass producer (eventfd %p) registration fails, ret = %d\n" , qid, vq->call_ctx.ctx, ret);
include/linux/irqbypass.h +1 -1
@@ -84,7 +84,7 @@ struct irq_bypass_consumer { };int irq_bypass_register_producer (struct irq_bypass_producer *producer, -struct eventfd_ctx *eventfd); +struct eventfd_ctx *eventfd,int irq);void irq_bypass_unregister_producer (struct irq_bypass_producer *producer);int irq_bypass_register_consumer (struct irq_bypass_consumer *consumer,struct eventfd_ctx *eventfd);
virt/lib/irqbypass.c +4 -1
@@ -85,12 +85,13 @@ static void __disconnect(struct irq_bypass_producer *prod,* irq_bypass_register_producer - register IRQ bypass producer * @producer: pointer to producer structure * @eventfd: pointer to the eventfd context associated with the producer +* @irq: Linux IRQ number of the underlying producer device * * Add the provided IRQ producer to the set of producers and connect with the * consumer with a matching eventfd, if one exists. */ int irq_bypass_register_producer (struct irq_bypass_producer *producer, -struct eventfd_ctx *eventfd) +struct eventfd_ctx *eventfd,int irq) {unsigned long index = (unsigned long )eventfd;struct irq_bypass_consumer *consumer;
@@ -99,6 +100,8 @@ int irq_bypass_register_producer(struct irq_bypass_producer *producer,if (WARN_ON_ONCE (producer->eventfd))return -EINVAL ; + producer->irq = irq; +guard (mutex)(&lock); ret =xa_insert (&producers, index, producer,GFP_KERNEL );
Signature · 1 line
2.49.0.1112.g889b7c5bd8-goog
RE: [PATCH v2 8/8] irqbypass: Require producers to pass in Linux IRQ number during registration
Quoted 11 lines
From: Sean Christopherson <[email protected]>
Sent: Saturday, May 17, 2025 7:08 AMPass in the Linux IRQ associated with an IRQ bypass producer instead of relying on the caller to set the field prior to registration, as there's no benefit to relying on callers to do the right thing.
Take care to set
producer->irqbefore__connect(), as KVM expects the IRQ to be valid as soon as a connection is possible.Signed-off-by Sean Christopherson <[email protected]>
Quoted 25 lines · Sean Christopherson
The two primary goals of this series are to make the irqbypass concept easier to understand, and to address the terrible performance that can result from using a list to track connections.
For the first goal, track the producer/consumer "tokens" as eventfd context pointers instead of opaque "void *". Supporting arbitrary token types was dead infrastructure when it was added 10 years ago, and nothing has changed since. Taking an opaque token makes a very simple concept (device signals eventfd; KVM listens to eventfd) unnecessarily difficult to understand.
Burying that simple behind a layer of obfuscation also makes the overall code more brittle, as callers can pass in literally anything. I.e. passing in a token that will never be paired would go unnoticed.
For the performance issue, use an xarray. I'm definitely not wedded to an xarray, but IMO it doesn't add meaningful complexity (even requires less code), and pretty much Just Works. Like tried this a while back[1], but the implementation had undesirable behavior changes and stalled out.
Note, I want to do more aggressive cleanups of irqbypass at some point, e.g. not reporting an error to userspace if
connect()fails is awful behavior for environments that want/need irqbypass to always work. And KVM shold probably have aKVM_IRQFD_FLAG_NO_IRQBYPASSif a VM is never going to use device posted interrupts. But those are future problems.
Quoted 9 lines
v2:
- Collect reviews. [Kevin, Michael]
- Track the pointer as "structeventfd_ctx*eventfd" instead of "void *token". [Alex]
- Fix typos and stale comments. [Kevin, Binbin]
- Use "trigger" instead of the null token/eventfd pointer on failure invfio_msi_set_vector_signal(). [Kevin]
- Drop a redundant "tmp == consumer" check from patch 3. [Kevin]
- Require producers to pass in the line IRQ number.
VDPA bits:
Quoted 29 lines
v1: https://lore.kernel.org/all/[email protected]
[1] https://lore.kernel.org/all/[email protected]
[2] https://lore.kernel.org/all/[email protected]Sean Christopherson (8):
irqbypass: Drop pointless and misleadingTHIS_MODULEget/put
irqbypass: Drop superfluousmight_sleep()annotations
irqbypass: Take ownership of producer/consumer token tracking
irqbypass: Explicitly track producer and consumer bindings
irqbypass: Use paired consumer/producer to disconnect during
unregister
irqbypass: Use guard(mutex) in lieu of manual lock+unlock
irqbypass: Use xarray to track producers and consumers
irqbypass: Require producers to pass in Linux IRQ number during
registrationarch/x86/kvm/x86.c | 4 +- drivers/vfio/pci/vfio_pci_intrs.c | 10 +- drivers/vhost/vdpa.c | 10 +- include/linux/irqbypass.h | 46 ++++---- virt/kvm/eventfd.c | 7 +- virt/lib/irqbypass.c | 190 +++++++++++------------------- 6 files changed, 107 insertions(+), 160 deletions(-)base-commit:
7ef51a41466bc846ad794d505e2e34ff97157f7fSignature · 1 line
2.49.0.1112.g889b7c5bd8-goog
Quoted 61 lines · On Fri, 16 May 2025 16:07:26 -0700 Sean Christopherson <[email protected]>…
The two primary goals of this series are to make the irqbypass concept easier to understand, and to address the terrible performance that can result from using a list to track connections.
For the first goal, track the producer/consumer "tokens" as eventfd context pointers instead of opaque "void *". Supporting arbitrary token types was dead infrastructure when it was added 10 years ago, and nothing has changed since. Taking an opaque token makes a very simple concept (device signals eventfd; KVM listens to eventfd) unnecessarily difficult to understand.
Burying that simple behind a layer of obfuscation also makes the overall code more brittle, as callers can pass in literally anything. I.e. passing in a token that will never be paired would go unnoticed.
For the performance issue, use an xarray. I'm definitely not wedded to an xarray, but IMO it doesn't add meaningful complexity (even requires less code), and pretty much Just Works. Like tried this a while back[1], but the implementation had undesirable behavior changes and stalled out.
Note, I want to do more aggressive cleanups of irqbypass at some point, e.g. not reporting an error to userspace if
connect()fails is awful behavior for environments that want/need irqbypass to always work. And KVM shold probably have aKVM_IRQFD_FLAG_NO_IRQBYPASSif a VM is never going to use device posted interrupts. But those are future problems.v2:
- Collect reviews. [Kevin, Michael]
- Track the pointer as "structeventfd_ctx*eventfd" instead of "void *token". [Alex]
- Fix typos and stale comments. [Kevin, Binbin]
- Use "trigger" instead of the null token/eventfd pointer on failure invfio_msi_set_vector_signal(). [Kevin]
- Drop a redundant "tmp == consumer" check from patch 3. [Kevin]
- Require producers to pass in the line IRQ number.v1: https://lore.kernel.org/all/[email protected]
[1] https://lore.kernel.org/all/[email protected]
[2] https://lore.kernel.org/all/[email protected]Sean Christopherson (8):
irqbypass: Drop pointless and misleadingTHIS_MODULEget/put
irqbypass: Drop superfluousmight_sleep()annotations
irqbypass: Take ownership of producer/consumer token tracking
irqbypass: Explicitly track producer and consumer bindings
irqbypass: Use paired consumer/producer to disconnect during
unregister
irqbypass: Use guard(mutex) in lieu of manual lock+unlock
irqbypass: Use xarray to track producers and consumers
irqbypass: Require producers to pass in Linux IRQ number during
registrationarch/x86/kvm/x86.c | 4 +- drivers/vfio/pci/vfio_pci_intrs.c | 10 +- drivers/vhost/vdpa.c | 10 +- include/linux/irqbypass.h | 46 ++++---- virt/kvm/eventfd.c | 7 +- virt/lib/irqbypass.c | 190 +++++++++++------------------- 6 files changed, 107 insertions(+), 160 deletions(-)
Quoted 1 line
base-commit:
7ef51a41466bc846ad794d505e2e34ff97157f7f
Sorry for the delay. Do you intend to take this through your trees?
Sean Christopherson
maintainerQuoted 64 lines · Alex Williamson
Quoted 61 lines · On Fri, 16 May 2025 16:07:26 -0700 Sean Christopherson <[email protected]>…
The two primary goals of this series are to make the irqbypass concept easier to understand, and to address the terrible performance that can result from using a list to track connections.
For the first goal, track the producer/consumer "tokens" as eventfd context pointers instead of opaque "void *". Supporting arbitrary token types was dead infrastructure when it was added 10 years ago, and nothing has changed since. Taking an opaque token makes a very simple concept (device signals eventfd; KVM listens to eventfd) unnecessarily difficult to understand.
Burying that simple behind a layer of obfuscation also makes the overall code more brittle, as callers can pass in literally anything. I.e. passing in a token that will never be paired would go unnoticed.
For the performance issue, use an xarray. I'm definitely not wedded to an xarray, but IMO it doesn't add meaningful complexity (even requires less code), and pretty much Just Works. Like tried this a while back[1], but the implementation had undesirable behavior changes and stalled out.
Note, I want to do more aggressive cleanups of irqbypass at some point, e.g. not reporting an error to userspace if
connect()fails is awful behavior for environments that want/need irqbypass to always work. And KVM shold probably have aKVM_IRQFD_FLAG_NO_IRQBYPASSif a VM is never going to use device posted interrupts. But those are future problems.v2:
- Collect reviews. [Kevin, Michael]
- Track the pointer as "structeventfd_ctx*eventfd" instead of "void *token". [Alex]
- Fix typos and stale comments. [Kevin, Binbin]
- Use "trigger" instead of the null token/eventfd pointer on failure invfio_msi_set_vector_signal(). [Kevin]
- Drop a redundant "tmp == consumer" check from patch 3. [Kevin]
- Require producers to pass in the line IRQ number.v1: https://lore.kernel.org/all/[email protected]
[1] https://lore.kernel.org/all/[email protected]
[2] https://lore.kernel.org/all/[email protected]Sean Christopherson (8):
irqbypass: Drop pointless and misleadingTHIS_MODULEget/put
irqbypass: Drop superfluousmight_sleep()annotations
irqbypass: Take ownership of producer/consumer token tracking
irqbypass: Explicitly track producer and consumer bindings
irqbypass: Use paired consumer/producer to disconnect during
unregister
irqbypass: Use guard(mutex) in lieu of manual lock+unlock
irqbypass: Use xarray to track producers and consumers
irqbypass: Require producers to pass in Linux IRQ number during
registrationarch/x86/kvm/x86.c | 4 +- drivers/vfio/pci/vfio_pci_intrs.c | 10 +- drivers/vhost/vdpa.c | 10 +- include/linux/irqbypass.h | 46 ++++---- virt/kvm/eventfd.c | 7 +- virt/lib/irqbypass.c | 190 +++++++++++------------------- 6 files changed, 107 insertions(+), 160 deletions(-)
Quoted 3 lines
Quoted 1 line
base-commit:
7ef51a41466bc846ad794d505e2e34ff97157f7fSorry for the delay.
Heh, no worries. ~2 weeks is downright prompt by my standards ;-)
Quoted 1 line
Do you intend to take this through your trees?
Yes, ideally, it would go into Paolo's kvm/next sooner than later (I'll start poking him if necessary). The s/token/eventfd rename creates an annoying conflict in kvm/x86.c with an in-flight patch (significant code movement between files). It would be nice to be able to rebase the in-flight patch instead of having to resolve a merge confict (the conflict itself isn't difficult to resolve, I just find it hard to visually review/audit the resolution due to the code movement).
Quoted 11 lines · Sean Christopherson
The two primary goals of this series are to make the irqbypass concept easier to understand, and to address the terrible performance that can result from using a list to track connections.
For the first goal, track the producer/consumer "tokens" as eventfd context pointers instead of opaque "void *". Supporting arbitrary token types was dead infrastructure when it was added 10 years ago, and nothing has changed since. Taking an opaque token makes a very simple concept (device signals eventfd; KVM listens to eventfd) unnecessarily difficult to understand.
[...]
Applied to kvm-x86 irqs, thanks!
[1/8] irqbypass: Drop pointless and misleading THIS_MODULE get/put
https://github.com/kvm-x86/linux/commit/fa079a0616ed [2/8] irqbypass: Drop superfluous might_sleep() annotations https://github.com/kvm-x86/linux/commit/07fbc83c0152 [3/8] irqbypass: Take ownership of producer/consumer token tracking https://github.com/kvm-x86/linux/commit/2b521d86ee80 [4/8] irqbypass: Explicitly track producer and consumer bindings https://github.com/kvm-x86/linux/commit/add57f493e08 [5/8] irqbypass: Use paired consumer/producer to disconnect during unregister https://github.com/kvm-x86/linux/commit/5d7dbdce388b [6/8] irqbypass: Use guard(mutex) in lieu of manual lock+unlock https://github.com/kvm-x86/linux/commit/46a4bfd0ae48 [7/8] irqbypass: Use xarray to track producers and consumers https://github.com/kvm-x86/linux/commit/8394b32faecd [8/8] irqbypass: Require producers to pass in Linux IRQ number during registration https://github.com/kvm-x86/linux/commit/23b54381cee2