]> git.ipfire.org Git - thirdparty/linux.git/commitdiff
KVM: Use a local struct to do the initial vfs_poll() on an irqfd
authorSean Christopherson <seanjc@google.com>
Thu, 22 May 2025 23:52:11 +0000 (16:52 -0700)
committerSean Christopherson <seanjc@google.com>
Mon, 23 Jun 2025 16:50:53 +0000 (09:50 -0700)
Use a function-local struct for the poll_table passed to vfs_poll(), as
nothing in the vfs_poll() callchain grabs a long-term reference to the
structure, i.e. its lifetime doesn't need to be tied to the irqfd.  Using
a local structure will also allow propagating failures out of the polling
callback without further polluting kvm_kernel_irqfd.

Opportunstically rename irqfd_ptable_queue_proc() to kvm_irqfd_register()
to capture what it actually does.

Tested-by: K Prateek Nayak <kprateek.nayak@amd.com>
Acked-by: Peter Zijlstra (Intel) <peterz@infradead.org>
Link: https://lore.kernel.org/r/20250522235223.3178519-2-seanjc@google.com
Signed-off-by: Sean Christopherson <seanjc@google.com>
include/linux/kvm_irqfd.h
virt/kvm/eventfd.c

index 361c07f4466d15df3f55d0efab86b6691c991b84..ef8c134ded8a313bc418f1b2b4506be9fe02a6aa 100644 (file)
@@ -55,7 +55,6 @@ struct kvm_kernel_irqfd {
        /* Used for setup/shutdown */
        struct eventfd_ctx *eventfd;
        struct list_head list;
-       poll_table pt;
        struct work_struct shutdown;
        struct irq_bypass_consumer consumer;
        struct irq_bypass_producer *producer;
index 59b1e64697f15f567dbad8970c7c49da0c9cc5d8..0b655376734e444b5b1ec3e2855eb2aec8b18290 100644 (file)
@@ -245,12 +245,17 @@ irqfd_wakeup(wait_queue_entry_t *wait, unsigned mode, int sync, void *key)
        return ret;
 }
 
-static void
-irqfd_ptable_queue_proc(struct file *file, wait_queue_head_t *wqh,
-                       poll_table *pt)
+struct kvm_irqfd_pt {
+       struct kvm_kernel_irqfd *irqfd;
+       poll_table pt;
+};
+
+static void kvm_irqfd_register(struct file *file, wait_queue_head_t *wqh,
+                              poll_table *pt)
 {
-       struct kvm_kernel_irqfd *irqfd =
-               container_of(pt, struct kvm_kernel_irqfd, pt);
+       struct kvm_irqfd_pt *p = container_of(pt, struct kvm_irqfd_pt, pt);
+       struct kvm_kernel_irqfd *irqfd = p->irqfd;
+
        add_wait_queue_priority(wqh, &irqfd->wait);
 }
 
@@ -298,6 +303,7 @@ kvm_irqfd_assign(struct kvm *kvm, struct kvm_irqfd *args)
 {
        struct kvm_kernel_irqfd *irqfd, *tmp;
        struct eventfd_ctx *eventfd = NULL, *resamplefd = NULL;
+       struct kvm_irqfd_pt irqfd_pt;
        int ret;
        __poll_t events;
        int idx;
@@ -387,7 +393,6 @@ kvm_irqfd_assign(struct kvm *kvm, struct kvm_irqfd *args)
         * a callback whenever someone signals the underlying eventfd
         */
        init_waitqueue_func_entry(&irqfd->wait, irqfd_wakeup);
-       init_poll_funcptr(&irqfd->pt, irqfd_ptable_queue_proc);
 
        spin_lock_irq(&kvm->irqfds.lock);
 
@@ -409,11 +414,14 @@ kvm_irqfd_assign(struct kvm *kvm, struct kvm_irqfd *args)
        spin_unlock_irq(&kvm->irqfds.lock);
 
        /*
-        * Check if there was an event already pending on the eventfd
-        * before we registered, and trigger it as if we didn't miss it.
+        * Register the irqfd with the eventfd by polling on the eventfd.  If
+        * there was en event pending on the eventfd prior to registering,
+        * manually trigger IRQ injection.
         */
-       events = vfs_poll(fd_file(f), &irqfd->pt);
+       irqfd_pt.irqfd = irqfd;
+       init_poll_funcptr(&irqfd_pt.pt, kvm_irqfd_register);
 
+       events = vfs_poll(fd_file(f), &irqfd_pt.pt);
        if (events & EPOLLIN)
                schedule_work(&irqfd->inject);