mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] KVM: irqfd: Publish the resampler after adding its first member
@ 2026-11-22  9:50 Qi Wu
  0 siblings, 0 replies; only message in thread
From: Qi Wu @ 2026-11-22  9:50 UTC (permalink / raw)
  To: Paolo Bonzini, Sean Christopherson, kvm; +Cc: linux-kernel

kvm_irqfd_assign() publishes a new resampler before adding its first
member and then calls synchronize_srcu_expedited() to drain readers that
may have seen an empty member list.  This per-new-resampler grace period
runs under resampler_lock and adds to VM creation / snapshot restore
latency.

Add the first member while the resampler is private, then publish it to
both global entries.  RCU list insertion guarantees any reader that finds
the resampler also sees its first member, so the empty-list state is never
visible and the first member needs no grace period; later members still
synchronize.  This also prevents kvm_notify_irqfd_resampler() from finding
an empty resampler and dropping a pending interrupt on IOAPIC unmask.

synchronize_srcu() calls per path (regression check; only the first row
changes):

  path                              before  after
  new resampler, first member         1       0
  existing resampler, next member     1       1
  non-resample irqfd assign           0       0
  irqfd_shutdown (UAF fix)            1       1
  deassign, non-last member           1       1
  deassign, last member               1       1

Build-tested with gcc/clang and KASAN+PROVE_RCU+PROVE_LOCKING / KCSAN
(KVM subsystem and selftests compile); checkpatch --strict clean.

This patch was prepared with AI assistance; the author reviewed and owns
the change.

Signed-off-by: Qi Wu <shijing.wuqi@linux.alibaba.com>
---
 virt/kvm/eventfd.c | 16 ++++++++++------
 1 file changed, 10 insertions(+), 6 deletions(-)

diff --git a/virt/kvm/eventfd.c b/virt/kvm/eventfd.c
index f43b8b25bc85..74851accd881 100644
--- a/virt/kvm/eventfd.c
+++ b/virt/kvm/eventfd.c
@@ -444,14 +444,18 @@ kvm_irqfd_assign(struct kvm *kvm, struct kvm_irqfd *args)
 			resampler->notifier.irq_acked = irqfd_resampler_ack;
 			INIT_LIST_HEAD(&resampler->link);
 
-			list_add_rcu(&resampler->link, &kvm->irqfds.resampler_list);
-			kvm_register_irq_ack_notifier(kvm,
-						      &resampler->notifier);
 			irqfd->resampler = resampler;
-		}
 
-		list_add_rcu(&irqfd->resampler_link, &irqfd->resampler->list);
-		synchronize_srcu_expedited(&kvm->irq_srcu);
+			/*
+			 * Publish last so no reader sees an empty member list.
+			 */
+			list_add_rcu(&irqfd->resampler_link, &resampler->list);
+			list_add_rcu(&resampler->link, &kvm->irqfds.resampler_list);
+			kvm_register_irq_ack_notifier(kvm, &resampler->notifier);
+		} else {
+			list_add_rcu(&irqfd->resampler_link, &irqfd->resampler->list);
+			synchronize_srcu_expedited(&kvm->irq_srcu);
+		}
 
 		mutex_unlock(&kvm->irqfds.resampler_lock);
 	}
-- 
2.47.3


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2026-11-22  9:50 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-22  9:50 [PATCH] KVM: irqfd: Publish the resampler after adding its first member Qi Wu

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®