From: Josh Hilke <jrhilke@google.com>
To: Paolo Bonzini <pbonzini@redhat.com>,
Sean Christopherson <seanjc@google.com>
Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org,
David Matlack <dmatlack@google.com>,
Alex Williamson <alex@shazbot.org>,
Josh Hilke <jrhilke@google.com>
Subject: [PATCH v4 07/19] KVM: selftests: Verify IRQ bypass works in IRQ test
Date: Sat, 30 May 2026 00:21:22 +0000 [thread overview]
Message-ID: <20260530002134.558837-8-jrhilke@google.com> (raw)
In-Reply-To: <20260530002134.558837-1-jrhilke@google.com>
From: David Matlack <dmatlack@google.com>
Trigger interrupts from a VFIO device instead of emulating interrupts
using KVM eventfds. This verifies that guests receive interrupts via IRQ
bypass.
Add a '-d' argument to tools/testing/selftests/kvm/irq_test which
takes the segment:bus:device.function number of a PCI device bound to
VFIO which will trigger interrupts. The device must have a VFIO selftest
driver in order to work with the test.
Example:
$ ./tools/testing/selftests/kvm/irq_test -d 0000:06:0a.1
Co-developed-by: Josh Hilke <jrhilke@google.com>
Signed-off-by: Josh Hilke <jrhilke@google.com>
Signed-off-by: David Matlack <dmatlack@google.com>
---
tools/testing/selftests/kvm/irq_test.c | 61 +++++++++++++++++++++++---
1 file changed, 55 insertions(+), 6 deletions(-)
diff --git a/tools/testing/selftests/kvm/irq_test.c b/tools/testing/selftests/kvm/irq_test.c
index d3fee51cf451..616eb38c05b8 100644
--- a/tools/testing/selftests/kvm/irq_test.c
+++ b/tools/testing/selftests/kvm/irq_test.c
@@ -1,14 +1,17 @@
// SPDX-License-Identifier: GPL-2.0
#include "kvm_util.h"
#include "test_util.h"
+#include <linux/sizes.h>
#include "apic.h"
#include "processor.h"
+#include "proc_util.h"
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
#include <sys/eventfd.h>
+#include <libvfio.h>
static u64 timeout_ns = 2ULL * 1000 * 1000 * 1000;
static bool guest_ready_for_irqs[KVM_MAX_VCPUS];
@@ -55,6 +58,36 @@ static void *vcpu_thread_main(void *arg)
return NULL;
}
+static int vfio_setup_msi(struct vfio_pci_device *device)
+{
+ const int flags = MAP_SHARED | MAP_ANONYMOUS;
+ const int prot = PROT_READ | PROT_WRITE;
+ struct dma_region *region;
+
+ /* A driver is required to generate an MSI. */
+ TEST_REQUIRE(device->driver.ops);
+
+ /* Set up a DMA-able region for the driver to use. */
+ region = &device->driver.region;
+ region->iova = 0;
+ region->size = SZ_2M;
+ region->vaddr = kvm_mmap(region->size, prot, flags, -1);
+ TEST_ASSERT(region->vaddr != MAP_FAILED, "mmap() failed\n");
+ iommu_map(device->iommu, region);
+
+ vfio_pci_driver_init(device);
+ return device->driver.msi;
+}
+
+static void trigger_interrupt(struct vfio_pci_device *device, int eventfd)
+{
+ if (device)
+ vfio_pci_driver_send_msi(device);
+ else
+ eventfd_write(eventfd, 1);
+}
+
+
static void kvm_route_msi(struct kvm_vm *vm, u32 gsi, struct kvm_vcpu *vcpu,
u8 vector)
{
@@ -76,9 +109,10 @@ static void kvm_route_msi(struct kvm_vm *vm, u32 gsi, struct kvm_vcpu *vcpu,
static void help(const char *name)
{
- printf("Usage: %s [-h]\n", name);
+ printf("Usage: %s [-d <segment:bus:device.function>] [-h]\n", name);
printf("\n");
printf("Tests KVM IRQ injection via irqfd using an emulated eventfd.\n");
+ printf("-d Use a VFIO device to send MSI-X interrupts instead of using an emulated eventfd\n");
printf("\n");
exit(KSFT_FAIL);
}
@@ -102,12 +136,18 @@ int main(int argc, char **argv)
struct kvm_vcpu *vcpus[KVM_MAX_VCPUS];
pthread_t vcpu_threads[KVM_MAX_VCPUS];
+ struct vfio_pci_device *device = NULL;
int nr_irqs = 1000, nr_vcpus = 1;
- int i, j, c, eventfd;
+ int i, j, c, msi, irq, eventfd;
+ const char *device_bdf = NULL;
+ struct iommu *iommu;
struct kvm_vm *vm;
- while ((c = getopt(argc, argv, "h")) != -1) {
+ while ((c = getopt(argc, argv, "d:h")) != -1) {
switch (c) {
+ case 'd':
+ device_bdf = optarg;
+ break;
case 'h':
default:
help(argv[0]);
@@ -119,7 +159,17 @@ int main(int argc, char **argv)
vm = vm_create_with_vcpus(nr_vcpus, guest_code, vcpus);
vm_install_exception_handler(vm, vector, guest_irq_handler);
- eventfd = kvm_new_eventfd();
+ if (device_bdf) {
+ iommu = iommu_init(default_iommu_mode);
+ device = vfio_pci_device_init(device_bdf, iommu);
+ msi = vfio_setup_msi(device);
+ irq = get_proc_vfio_irq_number(device_bdf, msi);
+ eventfd = device->msi_eventfds[msi];
+ printf("Using device %s MSI-X[%d] (IRQ-%d)\n", device_bdf, msi,
+ irq);
+ } else {
+ eventfd = kvm_new_eventfd();
+ }
printf("Injecting interrupts for GSI %d (Vector 0x%x) %d times\n",
gsi, vector, nr_irqs);
@@ -147,8 +197,7 @@ int main(int argc, char **argv)
"IRQ flag for vCPU %d not clear prior to test",
vcpus[j]->id);
- /* Trigger interrupt */
- eventfd_write(eventfd, 1);
+ trigger_interrupt(device, eventfd);
clock_gettime(CLOCK_MONOTONIC, &start);
for (;;) {
--
2.54.0.929.g9b7fa37559-goog
next prev parent reply other threads:[~2026-05-30 0:23 UTC|newest]
Thread overview: 25+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-05-30 0:21 [PATCH v4 00/19] KVM: selftests: Link with VFIO selftests lib Josh Hilke
2026-05-30 0:21 ` [PATCH v4 01/19] KVM: selftests: Build and link selftests/vfio/lib into KVM selftests Josh Hilke
2026-05-30 0:21 ` [PATCH v4 02/19] KVM: selftests: Add /proc/interrupts parsing helpers for IRQ test Josh Hilke
2026-06-01 19:19 ` Sean Christopherson
2026-05-30 0:21 ` [PATCH v4 03/19] KVM: selftests: Add guest read/write macros Josh Hilke
2026-05-30 0:21 ` [PATCH v4 04/19] KVM: selftests: Rename guest_rng to kvm_rng Josh Hilke
2026-05-30 0:21 ` [PATCH v4 05/19] KVM: selftests: Add helper to generate random u64 in range [min,max] Josh Hilke
2026-05-30 0:21 ` [PATCH v4 06/19] KVM: selftests: Add IRQ injection test Josh Hilke
2026-05-30 0:21 ` Josh Hilke [this message]
2026-05-30 0:21 ` [PATCH v4 08/19] KVM: selftests: Verify interrupts are received when IRQ affinity changes in IRQ test Josh Hilke
2026-06-01 20:02 ` Sean Christopherson
2026-05-30 0:21 ` [PATCH v4 09/19] KVM: selftests: Verify IRQs wake up halted vCPUs " Josh Hilke
2026-05-30 0:21 ` [PATCH v4 10/19] KVM: selftests: Verify interrupts are received after modifying IRQ routes " Josh Hilke
2026-05-30 0:21 ` [PATCH v4 11/19] KVM: selftests: Make number of IRQs configurable " Josh Hilke
2026-05-30 0:21 ` [PATCH v4 12/19] KVM: selftests: Verify non-postable IRQ remapping " Josh Hilke
2026-05-30 0:21 ` [PATCH v4 13/19] KVM: selftests: Add kvm_gettid() wrapper and convert users Josh Hilke
2026-05-30 0:21 ` [PATCH v4 14/19] KVM: selftests: Add kvm_sched_getaffinity() " Josh Hilke
2026-06-01 19:27 ` Sean Christopherson
2026-05-30 0:21 ` [PATCH v4 15/19] KVM: selftests: Add pin_task_to_random_cpu() helper function Josh Hilke
2026-05-30 0:21 ` [PATCH v4 16/19] KVM: selftests: Verify vCPU migration during IRQ delivery in IRQ test Josh Hilke
2026-05-30 0:21 ` [PATCH v4 17/19] KVM: selftests: Print vCPU affinity on timeout during " Josh Hilke
2026-05-30 0:21 ` [PATCH v4 18/19] KVM: selftests: Make number of vCPUs configurable in " Josh Hilke
2026-05-30 0:21 ` [PATCH v4 19/19] KVM: selftests: Add xAPIC support " Josh Hilke
2026-05-30 0:26 ` [PATCH v4 00/19] KVM: selftests: Link with VFIO selftests lib Josh Hilke
2026-06-01 19:07 ` Sean Christopherson
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20260530002134.558837-8-jrhilke@google.com \
--to=jrhilke@google.com \
--cc=alex@shazbot.org \
--cc=dmatlack@google.com \
--cc=kvm@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=pbonzini@redhat.com \
--cc=seanjc@google.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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®