Skip to contentSkip to conversation

kvm, selftests: ioctl to handle MSIs injected from userspace as software-bypassing vLPIs

PATCH In review Maximilian Dittgen 2 participants 2 messages Sep 25, 2025

Subsystems (5)
KVM Kselftest arm64 KVM arm64 +1 more subsystems
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 kvm, selftests: ioctl to handle MSIs injected from userspace as software-bypassing vLPIs

From: Maximilian Dittgen <[email protected]>

At the moment, all MSIs injected from userspace using KVM_SIGNAL_MSI are processed as LPIs in software with a hypervisor trap and exit. To properly test GICv4 direct vLPI injection from KVM selftests, we write a KVM_DEBUG_GIC_MSI_SETUP ioctl that manually creates an IRQ routing table entry for the specified MSI, and populates ITS structures (device, collection, and interrupt translation table entries) to map the MSI to a vLPI. We then call GICv4 kvm_vgic_v4_set_forwarding to let the vLPI bypass hypervisor traps and inject directly to the vCPU.

To demonstrate the use of this ioctl, we implement a -D flag to the vgic_lpi_stress.c selftest that runs the stress test using direct vLPI injection rather than software-emulated LPI handling.

---
 arch/arm64/kvm/arm.c                          |  37 +++++
 arch/arm64/kvm/vgic/vgic-its.c                | 133 ++++++++++++++++++
 arch/arm64/kvm/vgic/vgic.h                    |   2 +
 include/linux/irqchip/arm-gic-v3.h            |   1 +
 include/uapi/linux/kvm.h                      |  15 ++
 .../selftests/kvm/arm64/vgic_lpi_stress.c     |  52 ++++++-
 6 files changed, 238 insertions(+), 2 deletions(-)
arch/arm64/kvm/arm.c +37 -0
index 5bf101c869c9..e18f5ff68274 100644
@@ -46,6 +46,8 @@
 #include <kvm/arm_pmu.h>
 #include <kvm/arm_psci.h>
 
+#include <vgic/vgic.h>
+
 #include "sys_regs.h"
 
 static enum kvm_mode kvm_mode = KVM_MODE_DEFAULT;
@@ -1927,6 +1929,41 @@ int kvm_arch_vm_ioctl(struct file *filp, unsigned int ioctl, unsigned long arg)
 			return -EFAULT;
 		return kvm_vm_ioctl_get_reg_writable_masks(kvm, &range);
 	}
+	case KVM_DEBUG_GIC_MSI_SETUP: {
+		/* Define interrupt ID boundaries for input validation */
+		#define GIC_LPI_OFFSET    8192
+		#define GIC_LPI_MAX       65535
+		#define SPI_INTID_MIN     32
+		#define SPI_INTID_MAX     1019
+
+		struct kvm_debug_gic_msi_setup params;
+		struct kvm_vcpu *vcpu;
+
+		if (copy_from_user(&params, argp, sizeof(params)))
+			return -EFAULT;
+
+		/* validate vcpu_id is in range and exists */
+		if (params.vcpu_id >= atomic_read(&kvm->online_vcpus))
+			return -EINVAL;
+
+		vcpu = kvm_get_vcpu(kvm, params.vcpu_id);
+		if (!vcpu)
+			return -EINVAL;
+
+		/* validate vintid is in LPI range */
+		if (params.vintid < GIC_LPI_OFFSET || params.vintid > GIC_LPI_MAX)
+			return -EINVAL;
+
+		/*
+		 * Validate host_irq is in safe range -- we use SPI range since
+		 * selftests guests will have no shared peripheral devices
+		 */
+		if (params.host_irq < SPI_INTID_MIN || params.host_irq > SPI_INTID_MAX)
+			return -EINVAL;
+
+		/* Mock single MSI for testing */
+		return debug_gic_msi_setup_mock_msi(kvm, &params);
+	}
 	default:
 		return -EINVAL;
 	}
arch/arm64/kvm/vgic/vgic-its.c +133 -0

136 lines not on this page · raw message

arch/arm64/kvm/vgic/vgic.h +2 -0
index de1c1d3261c3..8c8f1e963884 100644
@@ -432,4 +432,6 @@ static inline bool vgic_is_v3(struct kvm *kvm)
 int vgic_its_debug_init(struct kvm_device *dev);
 void vgic_its_debug_destroy(struct kvm_device *dev);
 
+int debug_gic_msi_setup_mock_msi(struct kvm *kvm, struct kvm_debug_gic_msi_setup *params);
+
 #endif
include/linux/irqchip/arm-gic-v3.h +1 -0
index 70c0948f978e..76beac55cb69 100644
@@ -378,6 +378,7 @@
 #define GITS_CIDR3			0xfffc
 
 #define GITS_TRANSLATER			0x10040
+#define GITS_BASE_GPA           0x8000000ULL
 
 #define GITS_SGIR			0x20020
 
include/uapi/linux/kvm.h +15 -0
index f0f0d49d2544..a655bbb70e99 100644
@@ -1440,6 +1440,21 @@ struct kvm_enc_region {
 #define KVM_GET_SREGS2             _IOR(KVMIO,  0xcc, struct kvm_sregs2)
 #define KVM_SET_SREGS2             _IOW(KVMIO,  0xcd, struct kvm_sregs2)
 
+/*
+ * Generate an IRQ routing entry and vLPI tables for userspace-sourced
+ * MSI, enabling direct vLPI injection testing from selftests
+ */
+#define KVM_DEBUG_GIC_MSI_SETUP    _IOW(KVMIO, 0xf0, struct kvm_debug_gic_msi_setup)
+
+struct kvm_debug_gic_msi_setup {
+	__u32 device_id;
+	__u32 event_id;
+	__u32 vcpu_id;
+	__u32 vintid;
+	__u32 host_irq;
+	__u64 itt_addr;
+};
+
 #define KVM_DIRTY_LOG_MANUAL_PROTECT_ENABLE    (1 << 0)
 #define KVM_DIRTY_LOG_INITIALLY_SET            (1 << 1)
 
tools/testing/selftests/kvm/arm64/vgic_lpi_stress.c +50 -2

87 lines not on this page · raw message

Signature · 12 lines
2.50.1 (Apple Git-155)




Amazon Web Services Development Center Germany GmbH
Tamara-Danz-Str. 13
10243 Berlin
Geschaeftsfuehrung: Christian Schlaeger
Eingetragen am Amtsgericht Charlottenburg unter HRB 257764 B
Sitz: Berlin
Ust-ID: DE 365 538 597
maintainer
#1 lore raw
Quoted 5 lines · Maximilian Dittgen

From: Maximilian Dittgen <[email protected]>

At the moment, all MSIs injected from userspace using KVM_SIGNAL_MSI are processed as LPIs in software with a hypervisor trap and exit.

Not really. Injecting an interrupt preempts the guest injecting a host IPI, but there is no trap.

Quoted 7 lines

To
properly test GICv4 direct vLPI injection from KVM selftests, we write a KVM_DEBUG_GIC_MSI_SETUP ioctl that manually creates an IRQ routing table entry for the specified MSI, and populates ITS structures (device, collection, and interrupt translation table entries) to map the MSI to a vLPI. We then call GICv4 kvm_vgic_v4_set_forwarding to let the vLPI bypass hypervisor traps and inject directly to the vCPU.

I think that's totally overkill, and there is at least two ways to achieve the same thing without adding any additional code to the kernel:

- your test can simulate the restore of a guest with pending interrupts in the in-memory tables, start it, see the expected interrupts in the guest. Additional benefit: you can now test LPI restore.

- you use the interrupt injection mechanism that has been in the core code since 536e2e34bd0022, and let the GIC inject the interrupt for you. In case you wonder why it is there: for the exact purpose you describe.

Thanks,

	M.
Signature · 1 line
Without deviation from the norm, progress is not possible.