Skip to content

Commit 283ed50

Browse files
committed
KVM: Use a local struct to do the initial vfs_poll() on an irqfd
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>
1 parent 6f34372 commit 283ed50

2 files changed

Lines changed: 17 additions & 10 deletions

File tree

include/linux/kvm_irqfd.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@ struct kvm_kernel_irqfd {
5555
/* Used for setup/shutdown */
5656
struct eventfd_ctx *eventfd;
5757
struct list_head list;
58-
poll_table pt;
5958
struct work_struct shutdown;
6059
struct irq_bypass_consumer consumer;
6160
struct irq_bypass_producer *producer;

virt/kvm/eventfd.c

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -245,12 +245,17 @@ irqfd_wakeup(wait_queue_entry_t *wait, unsigned mode, int sync, void *key)
245245
return ret;
246246
}
247247

248-
static void
249-
irqfd_ptable_queue_proc(struct file *file, wait_queue_head_t *wqh,
250-
poll_table *pt)
248+
struct kvm_irqfd_pt {
249+
struct kvm_kernel_irqfd *irqfd;
250+
poll_table pt;
251+
};
252+
253+
static void kvm_irqfd_register(struct file *file, wait_queue_head_t *wqh,
254+
poll_table *pt)
251255
{
252-
struct kvm_kernel_irqfd *irqfd =
253-
container_of(pt, struct kvm_kernel_irqfd, pt);
256+
struct kvm_irqfd_pt *p = container_of(pt, struct kvm_irqfd_pt, pt);
257+
struct kvm_kernel_irqfd *irqfd = p->irqfd;
258+
254259
add_wait_queue_priority(wqh, &irqfd->wait);
255260
}
256261

@@ -298,6 +303,7 @@ kvm_irqfd_assign(struct kvm *kvm, struct kvm_irqfd *args)
298303
{
299304
struct kvm_kernel_irqfd *irqfd, *tmp;
300305
struct eventfd_ctx *eventfd = NULL, *resamplefd = NULL;
306+
struct kvm_irqfd_pt irqfd_pt;
301307
int ret;
302308
__poll_t events;
303309
int idx;
@@ -387,7 +393,6 @@ kvm_irqfd_assign(struct kvm *kvm, struct kvm_irqfd *args)
387393
* a callback whenever someone signals the underlying eventfd
388394
*/
389395
init_waitqueue_func_entry(&irqfd->wait, irqfd_wakeup);
390-
init_poll_funcptr(&irqfd->pt, irqfd_ptable_queue_proc);
391396

392397
spin_lock_irq(&kvm->irqfds.lock);
393398

@@ -409,11 +414,14 @@ kvm_irqfd_assign(struct kvm *kvm, struct kvm_irqfd *args)
409414
spin_unlock_irq(&kvm->irqfds.lock);
410415

411416
/*
412-
* Check if there was an event already pending on the eventfd
413-
* before we registered, and trigger it as if we didn't miss it.
417+
* Register the irqfd with the eventfd by polling on the eventfd. If
418+
* there was en event pending on the eventfd prior to registering,
419+
* manually trigger IRQ injection.
414420
*/
415-
events = vfs_poll(fd_file(f), &irqfd->pt);
421+
irqfd_pt.irqfd = irqfd;
422+
init_poll_funcptr(&irqfd_pt.pt, kvm_irqfd_register);
416423

424+
events = vfs_poll(fd_file(f), &irqfd_pt.pt);
417425
if (events & EPOLLIN)
418426
schedule_work(&irqfd->inject);
419427

0 commit comments

Comments
 (0)