From: David Matlack <dmatlack@google.com>
To: Alex Williamson <alex.williamson@redhat.com>
Cc: Aaron Lewis <aaronlewis@google.com>,
Adhemerval Zanella <adhemerval.zanella@linaro.org>,
Adithya Jayachandran <ajayachandra@nvidia.com>,
Andrew Jones <ajones@ventanamicro.com>,
Ard Biesheuvel <ardb@kernel.org>,
Arnaldo Carvalho de Melo <acme@redhat.com>,
Bibo Mao <maobibo@loongson.cn>,
Claudio Imbrenda <imbrenda@linux.ibm.com>,
Dan Williams <dan.j.williams@intel.com>,
Dave Jiang <dave.jiang@intel.com>,
David Matlack <dmatlack@google.com>,
dmaengine@vger.kernel.org, Huacai Chen <chenhuacai@kernel.org>,
James Houghton <jthoughton@google.com>,
Jason Gunthorpe <jgg@nvidia.com>,
Joel Granados <joel.granados@kernel.org>,
Josh Hilke <jrhilke@google.com>,
Kevin Tian <kevin.tian@intel.com>,
kvm@vger.kernel.org, linux-kselftest@vger.kernel.org,
"Mike Rapoport (Microsoft)" <rppt@kernel.org>,
Paolo Bonzini <pbonzini@redhat.com>,
Pasha Tatashin <pasha.tatashin@soleen.com>,
"Pratik R. Sampat" <prsampat@amd.com>,
Saeed Mahameed <saeedm@nvidia.com>,
Sean Christopherson <seanjc@google.com>,
Shuah Khan <shuah@kernel.org>,
Vinicius Costa Gomes <vinicius.gomes@intel.com>,
Vipin Sharma <vipinsh@google.com>,
Wei Yang <richard.weiyang@gmail.com>,
"Yury Norov [NVIDIA]" <yury.norov@gmail.com>
Subject: [PATCH 32/33] KVM: selftests: Test sending a vfio-pci device IRQ to a VM
Date: Fri, 20 Jun 2025 23:20:30 +0000 [thread overview]
Message-ID: <20250620232031.2705638-33-dmatlack@google.com> (raw)
In-Reply-To: <20250620232031.2705638-1-dmatlack@google.com>
Add a new selftest called vfio_pci_device_irq_test that routes and
delivers an MSI from a vfio-pci device into a guest.
Note that this test currently uses vfio_pci_irq_trigger(), in which VFIO
emulates a device sending the MSI. In the future we want to replace this
with a real MSI so we can test the hardware path (e.g. VT-d on Intel).
This test only supports x86_64 for now, but can be ported to other
architectures in the future.
Signed-off-by: David Matlack <dmatlack@google.com>
---
tools/testing/selftests/kvm/Makefile.kvm | 1 +
.../testing/selftests/kvm/include/kvm_util.h | 4 +
tools/testing/selftests/kvm/lib/kvm_util.c | 21 +++
.../selftests/kvm/vfio_pci_device_irq_test.c | 123 ++++++++++++++++++
4 files changed, 149 insertions(+)
create mode 100644 tools/testing/selftests/kvm/vfio_pci_device_irq_test.c
diff --git a/tools/testing/selftests/kvm/Makefile.kvm b/tools/testing/selftests/kvm/Makefile.kvm
index 5a9644ce910b..ba6727dcf59d 100644
--- a/tools/testing/selftests/kvm/Makefile.kvm
+++ b/tools/testing/selftests/kvm/Makefile.kvm
@@ -146,6 +146,7 @@ TEST_GEN_PROGS_x86 += rseq_test
TEST_GEN_PROGS_x86 += steal_time
TEST_GEN_PROGS_x86 += system_counter_offset_test
TEST_GEN_PROGS_x86 += pre_fault_memory_test
+TEST_GEN_PROGS_x86 += vfio_pci_device_irq_test
# Compiled outputs used by test targets
TEST_GEN_PROGS_EXTENDED_x86 += x86/nx_huge_pages_test
diff --git a/tools/testing/selftests/kvm/include/kvm_util.h b/tools/testing/selftests/kvm/include/kvm_util.h
index bee65ca08721..8afa6d6f1435 100644
--- a/tools/testing/selftests/kvm/include/kvm_util.h
+++ b/tools/testing/selftests/kvm/include/kvm_util.h
@@ -907,6 +907,8 @@ void vcpu_args_set(struct kvm_vcpu *vcpu, unsigned int num, ...);
void kvm_irq_line(struct kvm_vm *vm, uint32_t irq, int level);
int _kvm_irq_line(struct kvm_vm *vm, uint32_t irq, int level);
+void kvm_add_irqfd(struct kvm_vm *vm, u32 gsi, int fd);
+
#define KVM_MAX_IRQ_ROUTES 4096
struct kvm_irq_routing *kvm_gsi_routing_create(void);
@@ -915,6 +917,8 @@ void kvm_gsi_routing_irqchip_add(struct kvm_irq_routing *routing,
int _kvm_gsi_routing_write(struct kvm_vm *vm, struct kvm_irq_routing *routing);
void kvm_gsi_routing_write(struct kvm_vm *vm, struct kvm_irq_routing *routing);
+void kvm_route_gsi(struct kvm_vm *vm, struct kvm_irq_routing_entry *entry);
+
const char *exit_reason_str(unsigned int exit_reason);
vm_paddr_t vm_phy_page_alloc(struct kvm_vm *vm, vm_paddr_t paddr_min,
diff --git a/tools/testing/selftests/kvm/lib/kvm_util.c b/tools/testing/selftests/kvm/lib/kvm_util.c
index a055343a7bf7..737252812d15 100644
--- a/tools/testing/selftests/kvm/lib/kvm_util.c
+++ b/tools/testing/selftests/kvm/lib/kvm_util.c
@@ -1891,6 +1891,16 @@ void kvm_irq_line(struct kvm_vm *vm, uint32_t irq, int level)
TEST_ASSERT(ret >= 0, KVM_IOCTL_ERROR(KVM_IRQ_LINE, ret));
}
+void kvm_add_irqfd(struct kvm_vm *vm, u32 gsi, int fd)
+{
+ struct kvm_irqfd arg = {
+ .gsi = gsi,
+ .fd = fd,
+ };
+
+ vm_ioctl(vm, KVM_IRQFD, &arg);
+}
+
struct kvm_irq_routing *kvm_gsi_routing_create(void)
{
struct kvm_irq_routing *routing;
@@ -1941,6 +1951,17 @@ void kvm_gsi_routing_write(struct kvm_vm *vm, struct kvm_irq_routing *routing)
TEST_ASSERT(!ret, KVM_IOCTL_ERROR(KVM_SET_GSI_ROUTING, ret));
}
+void kvm_route_gsi(struct kvm_vm *vm, struct kvm_irq_routing_entry *entry)
+{
+ u8 buf[sizeof(struct kvm_irq_routing) + sizeof(*entry)] = {};
+ struct kvm_irq_routing *routes = (void *)&buf;
+
+ routes->nr = 1;
+ routes->entries[0] = *entry;
+
+ vm_ioctl(vm, KVM_SET_GSI_ROUTING, routes);
+}
+
/*
* VM Dump
*
diff --git a/tools/testing/selftests/kvm/vfio_pci_device_irq_test.c b/tools/testing/selftests/kvm/vfio_pci_device_irq_test.c
new file mode 100644
index 000000000000..9b90cf9dd38f
--- /dev/null
+++ b/tools/testing/selftests/kvm/vfio_pci_device_irq_test.c
@@ -0,0 +1,123 @@
+// SPDX-License-Identifier: GPL-2.0
+#include "kvm_util.h"
+#include "test_util.h"
+#include "apic.h"
+#include "processor.h"
+
+#include <pthread.h>
+#include <time.h>
+#include <linux/vfio.h>
+#include <vfio_util.h>
+
+static bool guest_ready_for_irq;
+static bool guest_received_irq;
+
+#define TIMEOUT_NS (10ULL * 1000 * 1000 * 1000)
+
+static void guest_enable_interrupts(void)
+{
+ x2apic_enable();
+ asm volatile ("sti");
+}
+
+void kvm_route_msi(struct kvm_vm *vm, u32 gsi, struct kvm_vcpu *vcpu, u8 vector)
+{
+ struct kvm_irq_routing_entry entry = {
+ .gsi = gsi,
+ .type = KVM_IRQ_ROUTING_MSI,
+ .u.msi.address_lo = 0xFEE00000 | (vcpu->id << 12),
+ .u.msi.data = vector,
+ };
+
+ kvm_route_gsi(vm, &entry);
+}
+
+static void guest_irq_handler(struct ex_regs *regs)
+{
+ WRITE_ONCE(guest_received_irq, true);
+ GUEST_DONE();
+}
+
+static void guest_code(void)
+{
+ guest_enable_interrupts();
+ WRITE_ONCE(guest_ready_for_irq, true);
+
+ for (;;)
+ continue;
+}
+
+void *vcpu_thread_main(void *arg)
+{
+ struct kvm_vcpu *vcpu = arg;
+ struct ucall uc;
+
+ vcpu_run(vcpu);
+ TEST_ASSERT_EQ(UCALL_DONE, get_ucall(vcpu, &uc));
+
+ return NULL;
+}
+
+static void help(const char *name)
+{
+ printf("Usage: %s [-i iommu_mode] [segment:bus:device.function]\n", name);
+ exit(KSFT_FAIL);
+}
+
+int main(int argc, char **argv)
+{
+ /* Random non-reserved vector and GSI to use for the device IRQ */
+ const u8 vector = 0xe0;
+ const u32 gsi = 32;
+
+ struct timespec start, elapsed;
+ struct vfio_pci_device *device;
+ const char *iommu_mode = NULL;
+ const char *device_bdf;
+ struct kvm_vcpu *vcpu;
+ pthread_t vcpu_thread;
+ struct kvm_vm *vm;
+ int c;
+
+ device_bdf = vfio_selftests_get_bdf(&argc, argv);
+
+ while ((c = getopt(argc, argv, "i:")) != -1) {
+ switch (c) {
+ case 'i':
+ iommu_mode = optarg;
+ break;
+ default:
+ help(argv[0]);
+ }
+ }
+
+ vm = vm_create_with_one_vcpu(&vcpu, guest_code);
+ vm_install_exception_handler(vm, vector, guest_irq_handler);
+
+ device = vfio_pci_device_init(device_bdf, iommu_mode);
+ TEST_REQUIRE(device->msix_info.count > 0);
+
+ vfio_pci_msix_enable(device, 0, 1);
+ kvm_add_irqfd(vm, gsi, device->msi_eventfds[0]);
+ kvm_route_msi(vm, gsi, vcpu, vector);
+
+ pthread_create(&vcpu_thread, NULL, vcpu_thread_main, vcpu);
+
+ while (!READ_ONCE(guest_ready_for_irq))
+ sync_global_from_guest(vm, guest_ready_for_irq);
+
+ vfio_pci_irq_trigger(device, VFIO_PCI_MSIX_IRQ_INDEX, 0);
+
+ clock_gettime(CLOCK_MONOTONIC, &start);
+
+ while (!READ_ONCE(guest_received_irq)) {
+ elapsed = timespec_elapsed(start);
+ TEST_ASSERT(timespec_to_ns(elapsed) < TIMEOUT_NS, "vCPU never received IRQ\n");
+ sync_global_from_guest(vm, guest_received_irq);
+ }
+
+ pthread_join(vcpu_thread, NULL);
+ vfio_pci_device_cleanup(device);
+
+ return 0;
+}
--
2.50.0.rc2.701.gf1e915cc24-goog
next prev parent reply other threads:[~2025-06-20 23:21 UTC|newest]
Thread overview: 61+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-06-20 23:19 [PATCH 00/33] vfio: Introduce selftests for VFIO David Matlack
2025-06-20 23:19 ` [PATCH 01/33] selftests: Create tools/testing/selftests/vfio David Matlack
2025-06-20 23:20 ` [PATCH 02/33] vfio: selftests: Add a helper library for VFIO selftests David Matlack
2025-06-20 23:20 ` [PATCH 03/33] vfio: selftests: Introduce vfio_pci_device_test David Matlack
2025-06-26 11:27 ` Sairaj Kodilkar
2025-06-26 11:44 ` Sairaj Kodilkar
2025-06-26 16:29 ` David Matlack
2025-06-27 4:57 ` Sairaj Kodilkar
2025-06-27 23:08 ` David Matlack
2025-06-30 6:18 ` Sairaj Kodilkar
2025-06-20 23:20 ` [PATCH 04/33] vfio: selftests: Test basic VFIO and IOMMUFD integration David Matlack
2025-06-20 23:20 ` [PATCH 05/33] vfio: selftests: Move vfio dma mapping test to their own file David Matlack
2025-06-20 23:20 ` [PATCH 06/33] vfio: selftests: Add test to reset vfio device David Matlack
2025-06-20 23:20 ` [PATCH 07/33] vfio: selftests: Add DMA mapping tests for 2M and 1G HugeTLB David Matlack
2025-06-20 23:20 ` [PATCH 08/33] vfio: selftests: Validate 2M/1G HugeTLB are mapped as 2M/1G in IOMMU David Matlack
2025-06-20 23:20 ` [PATCH 09/33] tools headers: Add stub definition for __iomem David Matlack
2025-06-20 23:20 ` [PATCH 10/33] tools headers: Import asm-generic MMIO helpers David Matlack
2025-06-20 23:20 ` [PATCH 11/33] tools headers: Import x86 MMIO helper overrides David Matlack
2025-06-20 23:20 ` [PATCH 12/33] tools headers: Import iosubmit_cmds512() David Matlack
2025-08-18 23:25 ` Vinicius Costa Gomes
2025-08-18 23:46 ` David Matlack
2025-06-20 23:20 ` [PATCH 13/33] tools headers: Add symlink to linux/pci_ids.h David Matlack
2025-06-20 23:20 ` [PATCH 14/33] vfio: selftests: Keep track of DMA regions mapped into the device David Matlack
2025-06-20 23:20 ` [PATCH 15/33] vfio: selftests: Enable asserting MSI eventfds not firing David Matlack
2025-06-20 23:20 ` [PATCH 16/33] vfio: selftests: Add a helper for matching vendor+device IDs David Matlack
2025-06-20 23:20 ` [PATCH 17/33] vfio: selftests: Add driver framework David Matlack
2025-06-20 23:20 ` [PATCH 18/33] vfio: sefltests: Add vfio_pci_driver_test David Matlack
2025-06-20 23:20 ` [PATCH 19/33] dmaengine: ioat: Move system_has_dca_enabled() to dma.h David Matlack
2025-08-19 22:07 ` Dave Jiang
2025-06-20 23:20 ` [PATCH 20/33] vfio: selftests: Add driver for Intel CBDMA David Matlack
2025-08-19 22:07 ` Dave Jiang
2025-06-20 23:20 ` [PATCH 21/33] dmaengine: idxd: Allow registers.h to be included from tools/ David Matlack
2025-08-18 23:26 ` Vinicius Costa Gomes
2025-06-20 23:20 ` [PATCH 22/33] vfio: selftests: Add driver for Intel DSA David Matlack
2025-08-18 23:41 ` Vinicius Costa Gomes
2025-08-19 16:31 ` David Matlack
2025-08-19 18:52 ` Vinicius Costa Gomes
2025-06-20 23:20 ` [PATCH 23/33] vfio: selftests: Move helper to get cdev path to libvfio David Matlack
2025-06-20 23:20 ` [PATCH 24/33] vfio: selftests: Encapsulate IOMMU mode David Matlack
2025-06-20 23:20 ` [PATCH 25/33] vfio: selftests: Replicate tests across all iommu_modes David Matlack
2025-06-20 23:20 ` [PATCH 26/33] vfio: selftests: Add vfio_type1v2_mode David Matlack
2025-06-20 23:20 ` [PATCH 27/33] vfio: selftests: Add iommufd_compat_type1{,v2} modes David Matlack
2025-06-20 23:20 ` [PATCH 28/33] vfio: selftests: Add iommufd mode David Matlack
2025-06-20 23:20 ` [PATCH 29/33] vfio: selftests: Make iommufd the default iommu_mode David Matlack
2025-06-20 23:20 ` [PATCH 30/33] vfio: selftests: Add a script to help with running VFIO selftests David Matlack
2025-06-20 23:20 ` [PATCH 31/33] KVM: selftests: Build and link sefltests/vfio/lib into KVM selftests David Matlack
2025-06-20 23:20 ` David Matlack [this message]
2025-06-20 23:20 ` [PATCH 33/33] KVM: selftests: Add -d option to vfio_pci_device_irq_test for device-sent MSIs David Matlack
2025-07-25 16:47 ` [PATCH 00/33] vfio: Introduce selftests for VFIO David Matlack
2025-07-28 16:27 ` Alex Williamson
2025-07-29 22:26 ` Jason Gunthorpe
2025-07-31 20:55 ` David Matlack
2025-08-18 18:59 ` David Matlack
2025-08-18 19:37 ` Alex Williamson
2025-08-18 20:33 ` David Matlack
2025-08-21 20:10 ` Alex Williamson
2025-08-21 21:03 ` David Matlack
2025-08-19 14:50 ` Shuah Khan
2025-08-05 15:08 ` Joel Granados
2025-08-12 15:04 ` David Matlack
2025-08-19 17:48 ` David Matlack
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=20250620232031.2705638-33-dmatlack@google.com \
--to=dmatlack@google.com \
--cc=aaronlewis@google.com \
--cc=acme@redhat.com \
--cc=adhemerval.zanella@linaro.org \
--cc=ajayachandra@nvidia.com \
--cc=ajones@ventanamicro.com \
--cc=alex.williamson@redhat.com \
--cc=ardb@kernel.org \
--cc=chenhuacai@kernel.org \
--cc=dan.j.williams@intel.com \
--cc=dave.jiang@intel.com \
--cc=dmaengine@vger.kernel.org \
--cc=imbrenda@linux.ibm.com \
--cc=jgg@nvidia.com \
--cc=joel.granados@kernel.org \
--cc=jrhilke@google.com \
--cc=jthoughton@google.com \
--cc=kevin.tian@intel.com \
--cc=kvm@vger.kernel.org \
--cc=linux-kselftest@vger.kernel.org \
--cc=maobibo@loongson.cn \
--cc=pasha.tatashin@soleen.com \
--cc=pbonzini@redhat.com \
--cc=prsampat@amd.com \
--cc=richard.weiyang@gmail.com \
--cc=rppt@kernel.org \
--cc=saeedm@nvidia.com \
--cc=seanjc@google.com \
--cc=shuah@kernel.org \
--cc=vinicius.gomes@intel.com \
--cc=vipinsh@google.com \
--cc=yury.norov@gmail.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;
as well as URLs for NNTP newsgroup(s).