mirror of
https://git.kernel.org/pub/scm/linux/kernel/git/herbert/cryptodev-2.6.git
synced 2026-04-18 03:23:53 -04:00
irqbypass: Take ownership of producer/consumer token tracking
Move 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. Reviewed-by: Kevin Tian <kevin.tian@intel.com> Acked-by: Michael S. Tsirkin <mst@redhat.com> Reviewed-by: Alex Williamson <alex.williamson@redhat.com> Link: https://lore.kernel.org/r/20250516230734.2564775-4-seanjc@google.com Signed-off-by: Sean Christopherson <seanjc@google.com>
This commit is contained in:
@@ -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
|
||||
|
||||
|
||||
@@ -77,30 +77,32 @@ 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
|
||||
*
|
||||
* Add the provided IRQ producer to the list of producers and connect
|
||||
* with any matching token found on the IRQ consumers list.
|
||||
* with any matching eventfd found on the IRQ consumers list.
|
||||
*/
|
||||
int irq_bypass_register_producer(struct irq_bypass_producer *producer)
|
||||
int irq_bypass_register_producer(struct irq_bypass_producer *producer,
|
||||
struct eventfd_ctx *eventfd)
|
||||
{
|
||||
struct irq_bypass_producer *tmp;
|
||||
struct irq_bypass_consumer *consumer;
|
||||
int ret;
|
||||
|
||||
if (!producer->token)
|
||||
if (WARN_ON_ONCE(producer->eventfd))
|
||||
return -EINVAL;
|
||||
|
||||
mutex_lock(&lock);
|
||||
|
||||
list_for_each_entry(tmp, &producers, node) {
|
||||
if (tmp->token == producer->token) {
|
||||
if (tmp->eventfd == eventfd) {
|
||||
ret = -EBUSY;
|
||||
goto out_err;
|
||||
}
|
||||
}
|
||||
|
||||
list_for_each_entry(consumer, &consumers, node) {
|
||||
if (consumer->token == producer->token) {
|
||||
if (consumer->eventfd == eventfd) {
|
||||
ret = __connect(producer, consumer);
|
||||
if (ret)
|
||||
goto out_err;
|
||||
@@ -108,6 +110,7 @@ int irq_bypass_register_producer(struct irq_bypass_producer *producer)
|
||||
}
|
||||
}
|
||||
|
||||
producer->eventfd = eventfd;
|
||||
list_add(&producer->node, &producers);
|
||||
|
||||
mutex_unlock(&lock);
|
||||
@@ -131,26 +134,28 @@ void irq_bypass_unregister_producer(struct irq_bypass_producer *producer)
|
||||
struct irq_bypass_producer *tmp;
|
||||
struct irq_bypass_consumer *consumer;
|
||||
|
||||
if (!producer->token)
|
||||
if (!producer->eventfd)
|
||||
return;
|
||||
|
||||
mutex_lock(&lock);
|
||||
|
||||
list_for_each_entry(tmp, &producers, node) {
|
||||
if (tmp->token != producer->token)
|
||||
if (tmp->eventfd != producer->eventfd)
|
||||
continue;
|
||||
|
||||
list_for_each_entry(consumer, &consumers, node) {
|
||||
if (consumer->token == producer->token) {
|
||||
if (consumer->eventfd == producer->eventfd) {
|
||||
__disconnect(producer, consumer);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
producer->eventfd = NULL;
|
||||
list_del(&producer->node);
|
||||
break;
|
||||
}
|
||||
|
||||
WARN_ON_ONCE(producer->eventfd);
|
||||
mutex_unlock(&lock);
|
||||
}
|
||||
EXPORT_SYMBOL_GPL(irq_bypass_unregister_producer);
|
||||
@@ -158,31 +163,35 @@ EXPORT_SYMBOL_GPL(irq_bypass_unregister_producer);
|
||||
/**
|
||||
* irq_bypass_register_consumer - register IRQ bypass consumer
|
||||
* @consumer: pointer to consumer structure
|
||||
* @eventfd: pointer to the eventfd context associated with the consumer
|
||||
*
|
||||
* Add the provided IRQ consumer to the list of consumers and connect
|
||||
* with any matching token found on the IRQ producer list.
|
||||
* with any matching eventfd found on the IRQ producer list.
|
||||
*/
|
||||
int irq_bypass_register_consumer(struct irq_bypass_consumer *consumer)
|
||||
int irq_bypass_register_consumer(struct irq_bypass_consumer *consumer,
|
||||
struct eventfd_ctx *eventfd)
|
||||
{
|
||||
struct irq_bypass_consumer *tmp;
|
||||
struct irq_bypass_producer *producer;
|
||||
int ret;
|
||||
|
||||
if (!consumer->token ||
|
||||
!consumer->add_producer || !consumer->del_producer)
|
||||
if (WARN_ON_ONCE(consumer->eventfd))
|
||||
return -EINVAL;
|
||||
|
||||
if (!consumer->add_producer || !consumer->del_producer)
|
||||
return -EINVAL;
|
||||
|
||||
mutex_lock(&lock);
|
||||
|
||||
list_for_each_entry(tmp, &consumers, node) {
|
||||
if (tmp->token == consumer->token || tmp == consumer) {
|
||||
if (tmp->eventfd == eventfd) {
|
||||
ret = -EBUSY;
|
||||
goto out_err;
|
||||
}
|
||||
}
|
||||
|
||||
list_for_each_entry(producer, &producers, node) {
|
||||
if (producer->token == consumer->token) {
|
||||
if (producer->eventfd == eventfd) {
|
||||
ret = __connect(producer, consumer);
|
||||
if (ret)
|
||||
goto out_err;
|
||||
@@ -190,6 +199,7 @@ int irq_bypass_register_consumer(struct irq_bypass_consumer *consumer)
|
||||
}
|
||||
}
|
||||
|
||||
consumer->eventfd = eventfd;
|
||||
list_add(&consumer->node, &consumers);
|
||||
|
||||
mutex_unlock(&lock);
|
||||
@@ -213,7 +223,7 @@ void irq_bypass_unregister_consumer(struct irq_bypass_consumer *consumer)
|
||||
struct irq_bypass_consumer *tmp;
|
||||
struct irq_bypass_producer *producer;
|
||||
|
||||
if (!consumer->token)
|
||||
if (!consumer->eventfd)
|
||||
return;
|
||||
|
||||
mutex_lock(&lock);
|
||||
@@ -223,16 +233,18 @@ void irq_bypass_unregister_consumer(struct irq_bypass_consumer *consumer)
|
||||
continue;
|
||||
|
||||
list_for_each_entry(producer, &producers, node) {
|
||||
if (producer->token == consumer->token) {
|
||||
if (producer->eventfd == consumer->eventfd) {
|
||||
__disconnect(producer, consumer);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
consumer->eventfd = NULL;
|
||||
list_del(&consumer->node);
|
||||
break;
|
||||
}
|
||||
|
||||
WARN_ON_ONCE(consumer->eventfd);
|
||||
mutex_unlock(&lock);
|
||||
}
|
||||
EXPORT_SYMBOL_GPL(irq_bypass_unregister_consumer);
|
||||
|
||||
Reference in New Issue
Block a user