Introduce per-vCPU vLPI injection control API
Subsystems (7)
cover Introduce per-vCPU vLPI injection control API
At the moment, the ability to direct-inject vLPIs is only enableable on an all-or-nothing per-VM basis, causing unnecessary I/O performance loss in cases where a VM's vCPU count exceeds available vPEs. This RFC introduces per-vCPU control over vLPI injection to realize potential I/O performance gain in such situations.
Background
----------
The value of dynamically enabling the direct injection of vLPIs on a per-vCPU basis is the ability to run guest VMs with simultaneous hardware-forwarded and software-forwarded message-signaled interrupts.
Currently, hardware-forwarded vLPI direct injection on a KVM guest requires GICv4 and is enabled on a per-VM, all-or-nothing basis. vLPI injection enablment happens in two stages:
1) At vGIC initialization, allocate direct injection structures for each vCPU (doorbell IRQ, vPE table entry, virtual pending table, vPEID).
2) When a PCI device is configured for passthrough, map its MSIs to vLPIs using the structures allocated in step 1.
Step 1 is all-or-nothing; if any vCPU cannot be configured with the vPE structures necessary for direct injection, the vPEs of all vCPUs are torn down and direct injection is disabled VM-wide.
This universality of direct vLPI injection enablement sparks several issues, with the most pressing being performance degradation on overcommitted hosts.
VM-wide vLPI enablement creates resource inefficiency when guest VMs have more vCPUs than the host has available vPEIDs. The amount of vPEIDs (and consequently, vPEs) a host can allocate is constrained by hardware and defined by GICD_TYPER2.VID + 1 (ITS_MAX_VPEID). Since direct injection requires a vCPU to be assigned a vPEID, at most ITS_MAX_VPEID vCPUs can be configured for direct injection at a time. Because vLPI direct injection is all-or-nothing on a VM, if a new guest VM would exhaust remaining vPEIDs, all vCPUs on that VM would fall back to hypervisor-forwarded LPIs, causing considerable I/O performance degradation.
Such performance degradation is exemplified on hosts with CPU overcommitment. Overcommitting an arbitrarily high number of vCPUs enables a VM's vCPU count to easily exceed the host's available vPEIDs. Even with marginally more vCPUs than vPEIDs, the current all-or-nothing vLPI paradigm disables direct injection entirely. This creates two
problems: first, a single many-vCPU overcommitted VM loses all direct injection despite having vPEIDs available; second, on multi-tenant hosts, VMs booted first consume all vPEIDs, leaving later VMs without direct injection regardless of their I/O intensity. Per-vCPU control would allow userspace to allocate available vPEIDs across VMs based on I/O workload rather than boot order or per-VM vCPU count. This per-vCPU granularity recovers most of the direct injection performance benefit instead of losing it completely.
To allow this per-vCPU granularity, this RFC introduces three new ioctls to the KVM API that enables userspace the ability to activate/deactivate direct vLPI injection capability and resources to vCPUs ad-hoc during VM runtime.
This RFC proposes userspace control, rather than kernel control, over vPEID allocation for simplicity of implementation, ease of testability, and autonomy over resource usage. In the future, the vLPI enable/disable building blocks from this RFC may be used to implement a full vPE allocation policy in the kernel.
The solution comes in several parts
-----------------------------------
1) [P 1] General declarations (ioctl definitions/stubs, kconfig option)
2) [P 2] Conditionally disable auto vLPI injection init routines
To prevent vCPUs from exceeding vPEID allocation limits upon VM boot, disable automatic vPEID allocation in the GICv4 initialization routine when the per-vCPU kconfig is active. Likewise, disable automatic hardware forwarding for PCI device-backed MSIs upon device registration.
3) [P 3-6] Implement per-vCPU vLPI enablement routine, which:
a) Creates per-vCPU doorbell IRQ on new vCPU-scoped, rather than VM-scoped, interrupt domain hierarchies.
b) Allocates per-vCPU vPE table entries and virtual pending table, linking them to the vCPU's doorbell IRQ.
c) Iterates through interrupt translation table to set hardware forwarding for all PCI device–backed interrupts targeting the specific vCPU.
3) [P 7-8] Implement per-vCPU vLPI disablement routine, which
a) Iterates through interrupt translation table to unset hardware forwarding for all interrupts targeting the specific vCPU.
b) Frees per-vCPU vPE table entries, virtual pending table, and doorbell IRQ, then removes vgic_dist's pointer to the vCPU's freed vPE.
4) [P 9] Couple vSGI enablement with per-vCPU vPE allocation
Since vSGIs cannot be direct-injected without an allocated vPE on the receiving vCPU, couple vSGI enablement with vLPI enablement on GICv4.1.
5) [P 10-13] Write selftests for vLPI direct injection
PCI devices cannot be passed through to selftest guests, so define an ioctl that mocks a hardware source for software-defined MSI interrupts and sets vLPI "hardware" forwarding for the MSIs. Use these vLPIs to selftest per-vCPU vLPI enablement/disablement ioctls.
Testing
-------
Testing has been carried out via selftests and QEMU-emulated guests.
Selftests have covered diverse vLPI configurations and race conditions. These include:
1) Stress testing LPI injection across multiple vCPUs while concurrently and repeatedly toggling the vCPUs' vLPI injection capability.
2) Enabling/disabling vLPI direct injection while scheduling or unscheduling a vCPU.
3) Allocating and freeing a single vPEID to multiple vCPUs, ensuring reusability.
4) Attempting to allocate a vPEID when all are already allocated, validating an error is thrown.
5) Calling enable/disable vLPI ioctls when GIC is not initialized.
6) Idempotent ioctl calls.
PCI device passthrough and interrupt injection to QEMU guest demonstrated:
1) Complete hypervisor circumvention when vLPI injection is enabled on a vCPU, hypervisor forwarding when vLPI injection is disabled.
2) Interrupts are not lost when received during per-vCPU vLPI state transitions.
Caveats
-------
1) Pending interrupts are flushed when vLPI injection is disabled for a vCPU; hardware pending state is not transfered to software. This may cause pending interrupts to be lost upon vPE disablement.
Unlike vSGIs, vLPIs do not expose their pending state through a GICD_ISPENDR register. Thus, we would need to read the pending state of the vLPI from the vPT. To read the pending status of the vLPI from vPT, we would need to invalidate any vPT cache associated with the vCPU's vPE. This requires unmapping the vPE and halting the vCPU, which would be incredibly expensive and unecessary given that MSIs are usually recoverable by the driver.
2) Direct-injected vSGIs (GICv4.1) require vCPUs to have associated vPEs. Since disabling vLPI injection on a vCPU frees its vPE, vSGI direct injection must simultaenously be disabled as well. At the moment, we use the per-vCPU vSGI toggle mechanism introduced in commit bacf2c6 to enable/disable vSGI injection alongside vLPI injection.
Maximilian Dittgen (13):
KVM: Introduce config option for per-vCPU vLPI enablement
KVM: arm64: Disable auto vCPU vPE assignment with per-vCPU vLPI config
KVM: arm64: Refactor out locked section of
kvm_vgic_v4_set_forwarding()
KVM: arm64: Implement vLPI QUERY ioctl for per-vCPU vLPI injection API
KVM: arm64: Implement vLPI ENABLE ioctl for per-vCPU vLPI injection
API
KVM: arm64: Resolve race between vCPU scheduling and vLPI enablement
KVM: arm64: Implement vLPI DISABLE ioctl for per-vCPU vLPI Injection
API
KVM: arm64: Make per-vCPU vLPI control ioctls atomic
KVM: arm64: Couple vSGI enablement with per-vCPU vPE allocation
KVM: selftests: fix MAPC RDbase target formatting in vgic_lpi_stress
KVM: Ioctl to set up userspace-injected MSIs as software-bypassing
vLPIs
KVM: arm64: selftests: Add support for stress testing direct-injected
vLPIs
KVM: arm64: selftests: Add test for per-vCPU vLPI control API
Documentation/virt/kvm/api.rst | 56 +++ arch/arm64/kvm/arm.c | 89 +++++ arch/arm64/kvm/vgic/vgic-its.c | 142 ++++++- arch/arm64/kvm/vgic/vgic-v3.c | 14 +- arch/arm64/kvm/vgic/vgic-v4.c | 370 +++++++++++++++++- arch/arm64/kvm/vgic/vgic.h | 10 + drivers/irqchip/Kconfig | 13 + drivers/irqchip/irq-gic-v3-its.c | 58 ++- drivers/irqchip/irq-gic-v4.c | 75 +++- include/kvm/arm_vgic.h | 8 + include/linux/irqchip/arm-gic-v3.h | 5 + include/linux/irqchip/arm-gic-v4.h | 10 +- include/linux/kvm_host.h | 11 + include/uapi/linux/kvm.h | 22 ++ tools/testing/selftests/kvm/Makefile.kvm | 1 + .../selftests/kvm/arm64/per_vcpu_vlpi.c | 274 +++++++++++++ .../selftests/kvm/arm64/vgic_lpi_stress.c | 181 ++++++++- .../selftests/kvm/lib/arm64/gic_v3_its.c | 9 +- 18 files changed, 1307 insertions(+), 41 deletions(-) create mode 100644 tools/testing/selftests/kvm/arm64/per_vcpu_vlpi.c
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, Christof Hellmis Eingetragen am Amtsgericht Charlottenburg unter HRB 257764 B Sitz: Berlin Ust-ID: DE 365 538 597
RFC PATCH 1/13 KVM: Introduce config option for per-vCPU vLPI enablement
Add CONFIG_ARM_GIC_V3_PER_VCPU_VLPI to control whether vLPI direct injection is to be enabled on a system-wide or a per-vCPU basis.
When enabled, vPEs can be allocated/deallocated to vCPUs on an ad-hoc, per-vCPU basis in runtime. When disabled, keep current vgic_v4_init behavior of automatic vCPU vPE allocation upon VM initialization.
We declare three ioctls numbers to manage per-vCPU vLPI enablement:
- KVM_ENABLE_VCPU_VLPI, which given a vCPU ID, allocates a vPE and initializes the vCPU for receiving direct vLPI interrupts.
- KVM_DISABLE_VCPU_VLPI, which given a vCPU ID, disables the vCPU’s ability to receive direct vLPI interrupts and frees its underlying vPE structure.
- KVM_QUERY_VCPU_VLPI, which given a vCPU ID, returns a boolean describing whether the vCPU is configured to receive direct vLPI interrupts.
This commit declares the kconfig, ioctl numbers, and documentation. Implementation will come throughout this patch set.
--- Documentation/virt/kvm/api.rst | 56 ++++++++++++++++++++++++++++++++++ arch/arm64/kvm/arm.c | 15 +++++++++ arch/arm64/kvm/vgic/vgic-v4.c | 9 ++++++ arch/arm64/kvm/vgic/vgic.h | 2 ++ drivers/irqchip/Kconfig | 13 ++++++++ include/uapi/linux/kvm.h | 6 ++++ 6 files changed, 101 insertions(+)
Documentation/virt/kvm/api.rst +56 -0
@@ -6517,6 +6517,62 @@ the capability to be present. `flags` must currently be zero. +4.XXX KVM_ENABLE_VCPU_VLPI +-------------------------- + +:Capability: KVM_CAP_ARM_PER_VCPU_VLPI +:Architectures: arm64 +:Type: vm ioctl +:Parameters: int vcpu_id (in) +:Returns: 0 on success, negative value on error + +This ioctl enables GICv4 direct vLPI injection for the specified vCPU. +Allocates vPE structures (doorbell IRQ, vPE table entry, virtual pending +table, vPEID) and upgrades existing software-forwarded LPIs targeting +this vCPU to hardware-forwarded vLPIs. + +If GICv4.1 is supported and vSGIs are disabled on the specified vCPU, +this ioctl enables vCPU vSGI support. + +Requires CONFIG_ARM_GIC_V3_PER_VCPU_VLPI and GICv4 hardware support. + +Returns -EINVAL if vGICv4 is not initialized or if the passed vcpu_id +does not map to a vCPU. + +4.XXX KVM_DISABLE_VCPU_VLPI +--------------------------- + +:Capability: KVM_CAP_ARM_PER_VCPU_VLPI +:Architectures: arm64 +:Type: vm ioctl +:Parameters: int vcpu_id (in) +:Returns: 0 on success, negative value on error + +This ioctl disables GICv4 direct vLPI injection for the specified vCPU. +Downgrades hardware-forwarded vLPIs to software-forwarded LPIs and frees +vPE structures. Pending interrupts in the virtual pending table may be +lost. + +If vSGIs are enabled on the specified vCPU, this ioctl disables them. + +Returns -EINVAL if vGICv4 is not initialized or if the passed vcpu_id +does not map to a vCPU. + +4.XXX KVM_QUERY_VCPU_VLPI +------------------------- + +:Capability: KVM_CAP_ARM_PER_VCPU_VLPI +:Architectures: arm64 +:Type: vm ioctl +:Parameters: int vcpu_id (in) +:Returns: 1 if enabled, 0 if disabled, negative value on error + +This ioctl queries whether GICv4 direct vLPI injection is enabled for +the specified vCPU. + +Returns -EINVAL if vGICv4 is not initialized or if the passed vcpu_id +does not map to a vCPU. + .. _kvm_run:
arch/arm64/kvm/arm.c +15 -0
@@ -424,6 +424,9 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)else r =kvm_supports_cacheable_pfnmap ();break ; +case KVM_CAP_ARM_PER_VCPU_VLPI : + r =kvm_per_vcpu_vlpi_supported (); +break ;default : r =0 ;
@@ -1947,6 +1950,18 @@ 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_ENABLE_VCPU_VLPI : { +/* TODO: create ioctl handler function */ +return -ENOSYS ; + } +case KVM_DISABLE_VCPU_VLPI : { +/* TODO: create ioctl handler function */ +return -ENOSYS ; + } +case KVM_QUERY_VCPU_VLPI : { +/* TODO: create ioctl handler function */ +return -ENOSYS ; + }default :return -EINVAL ; }
arch/arm64/kvm/vgic/vgic-v4.c +9 -0
@@ -226,6 +226,15 @@ void vgic_v4_get_vlpi_state(struct vgic_irq *irq, bool *val) *val = !!(*ptr & mask); } +bool kvm_per_vcpu_vlpi_supported (void ) +{ +#ifdef CONFIG_ARM_GIC_V3_PER_VCPU_VLPI +return kvm_vgic_global_state.has_gicv4; +#else +return false; +#endif +} +int vgic_v4_request_vpe_irq (struct kvm_vcpu *vcpu,int irq) {return request_irq (irq, vgic_v4_doorbell_handler,0 ,"vcpu" , vcpu);
arch/arm64/kvm/vgic/vgic.h +2 -0
@@ -467,4 +467,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); +bool kvm_per_vcpu_vlpi_supported (void ); +#endif
drivers/irqchip/Kconfig +13 -0
@@ -52,6 +52,19 @@ config ARM_GIC_V3_ITS default ARM_GIC_V3 select IRQ_MSI_IOMMU +config ARM_GIC_V3_PER_VCPU_VLPI + bool "ARM GICv4 per-vCPU vLPI direct injection support" + depends on ARM_GIC_V3_ITS + default n + help + Enable GICv4 direct injection of MSIs as vLPIs on a per vCPU + basis. Enables partial vLPI enablement on systems with more + vCPU capacity than vPE capacity. When enabled, all vCPUs + will boot without GICv4 vPE structures and handle interrupts + as software LPIs. KVM_ENABLE_VCPU_VLPI ioctl must then be called on + individual vCPUs to initialize their GICv4 structs and upgrade + targeting LPIs to vLPIs. + config ARM_GIC_V3_ITS_FSL_MC bool depends on ARM_GIC_V3_ITS
include/uapi/linux/kvm.h +6 -0
@@ -973,6 +973,7 @@ struct kvm_enable_cap {#define KVM_CAP_ARM_CACHEABLE_PFNMAP_SUPPORTED 243#define KVM_CAP_GUEST_MEMFD_FLAGS 244#define KVM_CAP_ARM_SEA_TO_USER 245 +#define KVM_CAP_ARM_PER_VCPU_VLPI 246struct kvm_irq_routing_irqchip {__u32 irqchip;
@@ -1451,6 +1452,11 @@ struct kvm_enc_region {#define KVM_GET_SREGS2 _IOR(KVMIO, 0xcc, struct kvm_sregs2)#define KVM_SET_SREGS2 _IOW(KVMIO, 0xcd, struct kvm_sregs2) +/* Per-vCPU vLPI enablement/disablement */ +#define KVM_ENABLE_VCPU_VLPI _IOW(KVMIO, 0xf0, int) +#define KVM_DISABLE_VCPU_VLPI _IOW(KVMIO, 0xf1, int) +#define KVM_QUERY_VCPU_VLPI _IOR(KVMIO, 0xf2, int) +#define KVM_DIRTY_LOG_MANUAL_PROTECT_ENABLE (1 << 0)#define KVM_DIRTY_LOG_INITIALLY_SET (1 << 1)
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, Christof Hellmis Eingetragen am Amtsgericht Charlottenburg unter HRB 257764 B Sitz: Berlin Ust-ID: DE 365 538 597
RFC PATCH 2/13 KVM: arm64: Disable auto vCPU vPE assignment with per-vCPU vLPI config
In reply to Maximilian DittgenThe first step in implementing per-vCPU vLPI enablement ensuring vCPUs are not automatically assigned vPEs upon GICv4 VM boot. This is a) so that new VMs on a host do not selfishly grab all available vPEs when existing VMs are resource sharing, and b) to not crash hosts in which the number of launchable vCPUs can exceed the number of vPEIDs available in hardware.
When CONFIG_ARM_GIC_V3_PER_VCPU_VLPI kconfig is enabled, skip vPE initialization portion of vgic_v4_init routine. Note we continue to allocate memory for an array of vPE pointers for future initialization. This will allow us to easily track which vCPUs are vLPI-enabled by simply null-checking the vpes[vcpu_id] entry.
Disable automatic kvm_vgic_v4_set_forwarding() upon PCI endpoint configuration since vCPUs no longer have vPEs mapped by default. Instead, store the host_irq mapping so set_forwarding() can be called later upon per-vCPU vLPI enablement.
We must work towards modifying vPE allocation/freeing functions to work on a vCPU rather than a VM level. This commit modifies vPE unmap/map to function on a per-vCPU basis, and disables IRQ allocation/freeing functionality for now since it is currently implemented on a per-VM level. Per-vCPU IRQ allocation/freeing will come in a later patch.
--- arch/arm64/kvm/arm.c | 6 ++++ arch/arm64/kvm/vgic/vgic-v3.c | 12 ++++++-- arch/arm64/kvm/vgic/vgic-v4.c | 55 ++++++++++++++++++++++++++++++++--- include/kvm/arm_vgic.h | 2 ++ 4 files changed, 69 insertions(+), 6 deletions(-)
arch/arm64/kvm/arm.c +6 -0
@@ -2798,8 +2798,14 @@ int kvm_arch_irq_bypass_add_producer(struct irq_bypass_consumer *cons,if (irq_entry->type !=KVM_IRQ_ROUTING_MSI )return 0 ; +#ifndef CONFIG_ARM_GIC_V3_PER_VCPU_VLPI return kvm_vgic_v4_set_forwarding (irqfd->kvm, prod->irq, &irqfd->irq_entry); +#else +/* Set forwarding later, ad-hoc upon per-vCPU vLPI enable request */ +return kvm_vgic_v4_map_irq_to_host (irqfd->kvm, prod->irq, + &irqfd->irq_entry); +#endif } void kvm_arch_irq_bypass_del_producer(struct irq_bypass_consumer *cons,
arch/arm64/kvm/vgic/vgic-v3.c +10 -2
@@ -566,8 +566,12 @@ static void unmap_all_vpes(struct kvm *kvm)struct vgic_dist *dist = &kvm->arch.vgic;int i; -for (i =0 ; i < dist->its_vm.nr_vpes; i++) +for (i =0 ; i < dist->its_vm.nr_vpes; i++) { +if (!dist->its_vm.vpes[i])/* Skip uninitialized vPEs */ +continue ; +free_irq (dist->its_vm.vpes[i]->irq,kvm_get_vcpu (kvm, i)); + } }static void map_all_vpes (struct kvm *kvm)
@@ -575,9 +579,13 @@ static void map_all_vpes(struct kvm *kvm)struct vgic_dist *dist = &kvm->arch.vgic;int i; -for (i =0 ; i < dist->its_vm.nr_vpes; i++) +for (i =0 ; i < dist->its_vm.nr_vpes; i++) { +if (!dist->its_vm.vpes[i]) +continue ; +WARN_ON (vgic_v4_request_vpe_irq (kvm_get_vcpu (kvm, i), dist->its_vm.vpes[i]->irq)); + } }/*
arch/arm64/kvm/vgic/vgic-v4.c +51 -4
110 lines not on this page · raw message
include/kvm/arm_vgic.h +2 -0
@@ -450,6 +450,8 @@ int kvm_vgic_set_owner(struct kvm_vcpu *vcpu, unsigned int intid, void *owner);struct kvm_kernel_irq_routing_entry ; +int kvm_vgic_v4_map_irq_to_host (struct kvm *kvm,int virq, +struct kvm_kernel_irq_routing_entry *irq_entry);int kvm_vgic_v4_set_forwarding (struct kvm *kvm,int irq,struct kvm_kernel_irq_routing_entry *irq_entry);
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, Christof Hellmis Eingetragen am Amtsgericht Charlottenburg unter HRB 257764 B Sitz: Berlin Ust-ID: DE 365 538 597
RFC PATCH 3/13 KVM: arm64: Refactor out locked section of kvm_vgic_v4_set_forwarding()
In reply to Maximilian Dittgenkvm_vgic_v4_set_forwarding() acquires its_lock to safely map guest LPIs to host IRQs for vLPI upgrades. Future per-vCPU direct vLPI injection requires atomically upgrading multiple LPIs while holding its_lock, which would cause recursive locking when calling kvm_vgic_v4_set_forwarding().
Extract the locked portion to kvm_vgic_v4_set_forwarding_locked() to allow callers already holding its_lock to perform vLPI upgrades without recursive locking.
No functional change.
--- arch/arm64/kvm/vgic/vgic-v4.c | 38 +++++++++++++++++++++-------------- include/kvm/arm_vgic.h | 3 +++ 2 files changed, 26 insertions(+), 15 deletions(-)
arch/arm64/kvm/vgic/vgic-v4.c +23 -15
@@ -483,27 +483,15 @@ int kvm_vgic_v4_map_irq_to_host(struct kvm *kvm, int virq,return 0 ; } -int kvm_vgic_v4_set_forwarding (struct kvm *kvm,int virq, -struct kvm_kernel_irq_routing_entry *irq_entry) +int kvm_vgic_v4_set_forwarding_locked (struct kvm *kvm,int virq, +struct kvm_kernel_irq_routing_entry *irq_entry,struct vgic_its *its) { -struct vgic_its *its;struct vgic_irq *irq;struct its_vlpi_map map;unsigned long flags;int ret =0 ; -if (!vgic_supports_direct_msis (kvm)) -return 0 ; - -/* -* Get the ITS, and escape early on error (not a valid -* doorbell for any of our vITSs). -*/ - its =vgic_get_its (kvm, irq_entry); -if (IS_ERR (its)) -return 0 ; - -guard (mutex)(&its->its_lock); +lockdep_assert_held (&its->its_lock);/* * Perform the actual DevID/EventID -> LPI translation.
@@ -567,6 +555,26 @@ int kvm_vgic_v4_set_forwarding(struct kvm *kvm, int virq,return ret; } +int kvm_vgic_v4_set_forwarding (struct kvm *kvm,int virq, +struct kvm_kernel_irq_routing_entry *irq_entry) +{ +struct vgic_its *its; + +if (!vgic_supports_direct_msis (kvm)) +return 0 ; + +/* +* Get the ITS, and escape early on error (not a valid +* doorbell for any of our vITSs). +*/ + its =vgic_get_its (kvm, irq_entry); +if (IS_ERR (its)) +return 0 ; + +guard (mutex)(&its->its_lock); +return kvm_vgic_v4_set_forwarding_locked (kvm, virq, irq_entry, its); +} +static struct vgic_irq *__vgic_host_irq_get_vlpi (struct kvm *kvm,int host_irq) {struct vgic_irq *irq;
include/kvm/arm_vgic.h +3 -0
@@ -454,6 +454,9 @@ int kvm_vgic_v4_map_irq_to_host(struct kvm *kvm, int virq, struct kvm_kernel_irq_routing_entry *irq_entry);int kvm_vgic_v4_set_forwarding (struct kvm *kvm,int irq,struct kvm_kernel_irq_routing_entry *irq_entry); +int kvm_vgic_v4_set_forwarding_locked (struct kvm *kvm,int virq, +struct kvm_kernel_irq_routing_entry *irq_entry, +struct vgic_its *its);void kvm_vgic_v4_unset_forwarding (struct kvm *kvm,int host_irq);
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, Christof Hellmis Eingetragen am Amtsgericht Charlottenburg unter HRB 257764 B Sitz: Berlin Ust-ID: DE 365 538 597
RFC PATCH 4/13 KVM: arm64: Implement vLPI QUERY ioctl for per-vCPU vLPI injection API
In reply to Maximilian DittgenImplement kvm_vgic_query_vcpu_vlpi, which handles the KVM_QUERY_VCPU_VLPI ioctl to query whether a vCPU is currently initialized to handle LPIs via direct vLPI injection. This function checks whether the vCPU's entry in the VM's vPE array is populated.
--- arch/arm64/kvm/arm.c | 13 +++++++++++-- arch/arm64/kvm/vgic/vgic-v4.c | 15 +++++++++++++++ arch/arm64/kvm/vgic/vgic.h | 1 + include/linux/kvm_host.h | 11 +++++++++++ 4 files changed, 38 insertions(+), 2 deletions(-)
arch/arm64/kvm/arm.c +11 -2
@@ -1959,8 +1959,17 @@ int kvm_arch_vm_ioctl(struct file *filp, unsigned int ioctl, unsigned long arg)return -ENOSYS ; }case KVM_QUERY_VCPU_VLPI : { -/* TODO: create ioctl handler function */ -return -ENOSYS ; +int vcpu_id; +struct kvm_vcpu *vcpu; + +if (copy_from_user (&vcpu_id, argp,sizeof (vcpu_id))) +return -EFAULT ; + + vcpu =kvm_get_vcpu_by_id (kvm, vcpu_id); +if (!vcpu) +return -EINVAL ; + +return kvm_vgic_query_vcpu_vlpi (vcpu); }default :return -EINVAL ;
arch/arm64/kvm/vgic/vgic-v4.c +15 -0
@@ -617,3 +617,18 @@ void kvm_vgic_v4_unset_forwarding(struct kvm *kvm, int host_irq)raw_spin_unlock_irqrestore (&irq->irq_lock, flags);vgic_put_irq (kvm, irq); } + +/* query whether vLPI direct injection is enabled on a specific vCPU. +* return 0 if disabled, 1 if enabled, -EINVAL if vCPU non-existant or GIC +* uninitialized +*/ +int kvm_vgic_query_vcpu_vlpi (struct kvm_vcpu *vcpu) +{ +struct kvm *kvm = vcpu->kvm; +struct vgic_dist *dist = &kvm->arch.vgic; +int i =kvm_idx_from_vcpu (kvm, vcpu); + +if (i ==UINT_MAX || !dist->its_vm.vpes) +return -EINVAL ;/* vCPU non-existant or uninitialized */ +return dist->its_vm.vpes[i] !=NULL ; +}
arch/arm64/kvm/vgic/vgic.h +1 -0
@@ -468,5 +468,6 @@ int vgic_its_debug_init(struct kvm_device *dev);void vgic_its_debug_destroy (struct kvm_device *dev);bool kvm_per_vcpu_vlpi_supported (void ); +int kvm_vgic_query_vcpu_vlpi (struct kvm_vcpu *vcpu);#endif
include/linux/kvm_host.h +11 -0
@@ -1030,6 +1030,17 @@ static inline struct kvm_vcpu *kvm_get_vcpu_by_id(struct kvm *kvm, int id)return NULL ; } +static inline unsigned int kvm_idx_from_vcpu (struct kvm *kvm,struct kvm_vcpu *target_vcpu) +{ +struct kvm_vcpu *vcpu; +unsigned long i; + +kvm_for_each_vcpu (i, vcpu, kvm) +if (vcpu == target_vcpu) +return i; +return UINT_MAX ; +} +void kvm_destroy_vcpus (struct kvm *kvm);int kvm_trylock_all_vcpus (struct kvm *kvm);
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, Christof Hellmis Eingetragen am Amtsgericht Charlottenburg unter HRB 257764 B Sitz: Berlin Ust-ID: DE 365 538 597
RFC PATCH 5/13 KVM: arm64: Implement vLPI ENABLE ioctl for per-vCPU vLPI injection API
In reply to Maximilian DittgenImplement kvm_vgic_enable_vcpu_vlpi, which handles the KVM_ENABLE_VCPU_VLPI ioctl to enable direct vLPI injection on a specific vCPU. The function has two components: a call to vgic_v4_cpu_init and a call to upgrade_existing_lpis_to_vlpis:
- vgic_v4_vcpu_init() is the per-vCPU corrolary to vgic_cpu_init, and
initializes all of the GIC structures a vCPU needs to handle LPI interrupts via direct injection. While IRQ domains are usually allocated on a per-VM basis, vgic_v4_cpu_init() creates a per-vPE IRQ domain and fwnode to decouple vLPI doorbell allocation across separate vCPUs. The domain allocation routine in its_vpe_irq_domain_alloc() also allocates a vPE table entry and virtual pending table for the vCPU.
- upgrade_existing_lpis_to_vlpis() iterates through all of the LPIs
targeting the vCPU and initializes hardware forwarding to process them as direct vLPIs. This includes updating the LPIs ITE to hold a vPE's vPEID instead of a Collection table's collection ID. It also toggles each interrupt's irq->hw flag to true to notify the ITS to handle the interrupt via direct injection.
--- arch/arm64/kvm/arm.c | 13 ++- arch/arm64/kvm/vgic/vgic-its.c | 4 +- arch/arm64/kvm/vgic/vgic-v4.c | 157 ++++++++++++++++++++++++++++- arch/arm64/kvm/vgic/vgic.h | 4 + drivers/irqchip/irq-gic-v3-its.c | 48 ++++++++- drivers/irqchip/irq-gic-v4.c | 56 ++++++++-- include/linux/irqchip/arm-gic-v3.h | 4 + include/linux/irqchip/arm-gic-v4.h | 8 +- 8 files changed, 277 insertions(+), 17 deletions(-)
arch/arm64/kvm/arm.c +11 -2
@@ -1951,8 +1951,17 @@ int kvm_arch_vm_ioctl(struct file *filp, unsigned int ioctl, unsigned long arg)return kvm_vm_ioctl_get_reg_writable_masks (kvm, &range); }case KVM_ENABLE_VCPU_VLPI : { -/* TODO: create ioctl handler function */ -return -ENOSYS ; +int vcpu_id; +struct kvm_vcpu *vcpu; + +if (copy_from_user (&vcpu_id, argp,sizeof (vcpu_id))) +return -EFAULT ; + + vcpu =kvm_get_vcpu_by_id (kvm, vcpu_id); +if (!vcpu) +return -EINVAL ; + +return kvm_vgic_enable_vcpu_vlpi (vcpu); }case KVM_DISABLE_VCPU_VLPI : {/* TODO: create ioctl handler function */
arch/arm64/kvm/vgic/vgic-its.c +2 -2
@@ -23,7 +23,7 @@#include "vgic.h" #include "vgic-mmio.h" -static struct kvm_device_ops kvm_arm_vgic_its_ops; +struct kvm_device_ops kvm_arm_vgic_its_ops;static int vgic_its_save_tables_v0 (struct vgic_its *its);static int vgic_its_restore_tables_v0 (struct vgic_its *its);
@@ -2801,7 +2801,7 @@ static int vgic_its_get_attr(struct kvm_device *dev,return 0 ; } -static struct kvm_device_ops kvm_arm_vgic_its_ops = { +struct kvm_device_ops kvm_arm_vgic_its_ops = { .name ="kvm-arm-vgic-its" , .create = vgic_its_create, .destroy = vgic_its_destroy,
arch/arm64/kvm/vgic/vgic-v4.c +155 -2
182 lines not on this page · raw message
arch/arm64/kvm/vgic/vgic.h +4 -0
@@ -251,6 +251,8 @@ struct ap_list_summary {#define irqs_active_outside_lrs (s) \ ((s)->nr_act && irqs_outside_lrs(s)) +extern struct kvm_device_ops kvm_arm_vgic_its_ops; +int vgic_v3_parse_attr (struct kvm_device *dev,struct kvm_device_attr *attr,struct vgic_reg_attr *reg_attr); int vgic_v2_parse_attr(struct kvm_device *dev, struct kvm_device_attr *attr,
@@ -434,6 +436,7 @@ static inline bool vgic_supports_direct_irqs(struct kvm *kvm) }int vgic_v4_init (struct kvm *kvm); +int vgic_v4_vcpu_init (struct kvm_vcpu *vcpu);void vgic_v4_teardown (struct kvm *kvm);void vgic_v4_configure_vsgis (struct kvm *kvm);void vgic_v4_get_vlpi_state (struct vgic_irq *irq,bool *val);
@@ -468,6 +471,7 @@ int vgic_its_debug_init(struct kvm_device *dev);void vgic_its_debug_destroy (struct kvm_device *dev);bool kvm_per_vcpu_vlpi_supported (void ); +int kvm_vgic_enable_vcpu_vlpi (struct kvm_vcpu *vcpu);int kvm_vgic_query_vcpu_vlpi (struct kvm_vcpu *vcpu);#endif
drivers/irqchip/irq-gic-v3-its.c +44 -4
90 lines not on this page · raw message
drivers/irqchip/irq-gic-v4.c +50 -6
91 lines not on this page · raw message
include/linux/irqchip/arm-gic-v3.h +4 -0
@@ -641,6 +641,10 @@ int its_init(struct fwnode_handle *handle, struct rdists *rdists, struct irq_domain *domain, u8 irq_prio);int mbi_init (struct fwnode_handle *fwnode,struct irq_domain *parent); +/* Enable prop table alloc/free on vGIC init/destroy when per-vCPU vLPI is enabled */ +struct page *its_allocate_prop_table (gfp_t gfp_flags); +void its_free_prop_table (struct page *prop_page); +static inline bool gic_enable_sre (void ) {u32 val;
include/linux/irqchip/arm-gic-v4.h +7 -1
@@ -8,6 +8,7 @@#define __LINUX_IRQCHIP_ARM_GIC_V4_Hstruct its_vpe ; +struct kvm_vcpu ;/* * Maximum number of ITTs when GITS_TYPER.VMOVP == 0, using the
@@ -42,6 +43,10 @@ struct its_vpe {struct its_vm *its_vm;/* per-vPE VLPI tracking */ atomic_t vlpi_count; +/* per-vPE domain for per-vCPU VLPI enablement */ +struct irq_domain *lpi_domain; +/* enables per-vPE vLPI IRQ Domains during per-vCPU VLPI enablement */ +struct fwnode_handle *lpi_fwnode;/* Doorbell interrupt */ int irq;irq_hw_number_t vpe_db_lpi;
@@ -59,7 +64,7 @@ struct its_vpe { };/* GICv4.1 implementations */ struct { -struct fwnode_handle *fwnode; +struct fwnode_handle *sgi_fwnode;struct irq_domain *sgi_domain;struct {u8 priority;
@@ -139,6 +144,7 @@ struct its_cmd_info { };int its_alloc_vcpu_irqs (struct its_vm *vm); +int its_alloc_vcpu_irq (struct kvm_vcpu *vcpu);void its_free_vcpu_irqs (struct its_vm *vm);int its_make_vpe_resident (struct its_vpe *vpe,bool g0en,bool g1en);int its_make_vpe_non_resident (struct its_vpe *vpe,bool db);
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, Christof Hellmis Eingetragen am Amtsgericht Charlottenburg unter HRB 257764 B Sitz: Berlin Ust-ID: DE 365 538 597
RFC PATCH 6/13 KVM: arm64: Resolve race between vCPU scheduling and vLPI enablement
In reply to Maximilian DittgenIf vgic_v4_load() is called to schedule GICv4 on a vCPU at the same time that kvm_vgic_enable_vcpu_vlpi() is called to enable vLPI direct injection on the vCPU, vgic_v4_load() will attempt to map the vCPU's doorbell IRQ to the physical processor while kvm_vgic_enable_vcpu_vlpi() is still creating the doorbell IRQ.
This race will cause vgic_v4_load()'s mapping operation to fail, triggering a WARN_ON in vgic_v3_load().
Fix by checking for the presence of a doorbell IRQ before attempting to load GICv4. Remove WARN_ON to remove verbosity of GICv4 load failures resulting from this race; failure to load GICv4 is not breaking behavior.
--- arch/arm64/kvm/vgic/vgic-v3.c | 2 +- arch/arm64/kvm/vgic/vgic-v4.c | 1 + 2 files changed, 2 insertions(+), 1 deletion(-)
arch/arm64/kvm/vgic/vgic-v3.c +1 -1
@@ -995,7 +995,7 @@ void vgic_v3_load(struct kvm_vcpu *vcpu)if (has_vhe ())__vgic_v3_activate_traps (cpu_if); -WARN_ON (vgic_v4_load (vcpu)); +vgic_v4_load (vcpu); }void vgic_v3_put (struct kvm_vcpu *vcpu)
arch/arm64/kvm/vgic/vgic-v4.c +1 -0
@@ -454,6 +454,7 @@ int vgic_v4_load(struct kvm_vcpu *vcpu)if (!vgic_supports_direct_irqs (vcpu->kvm) || !vpe->its_vm || + !vpe->irq ||/* check if irq has been allocated yet */ vpe->resident)return 0 ;
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, Christof Hellmis Eingetragen am Amtsgericht Charlottenburg unter HRB 257764 B Sitz: Berlin Ust-ID: DE 365 538 597
RFC PATCH 7/13 KVM: arm64: Implement vLPI DISABLE ioctl for per-vCPU vLPI Injection API
In reply to Maximilian DittgenImplement kvm_vgic_disable_vcpu_vlpi(), which handles the KVM_DISABLE_VCPU_VLPI ioctl to disable direct vLPI injection on a specific vCPU. The function has two components: a call to vgic_v4_vcpu_teardown() and a call to downgrade_existing_vlpis_to_lpis():
- vgic_v4_vcpu_teardown() is the per-vCPU corrolary to vgic_v4_teardown()
and frees all of the GIC structures a vCPU needs to handle LPI interrupts via direct injection. While vgic_v4_teardown operates on a per-VM basis, vgic_v4_vcpu_teardown() frees the IRQ, LPI domain, and fwnode of the single targeted vCPU. The domain free routine in this function frees the vPE table entry and virtual pending table fo the vCPU.
- downgrade_existing_lpis_to_vlpis() iterates through all of the LPIs
targeting the vCPU and tears down the hardware forwarding that processes them as vLPIs. Uses kvm_vgic_v4_unset_forwarding() to unmap direct injection.
--- arch/arm64/kvm/arm.c | 13 +++- arch/arm64/kvm/vgic/vgic-v4.c | 105 ++++++++++++++++++++++++++--- arch/arm64/kvm/vgic/vgic.h | 2 + drivers/irqchip/irq-gic-v3-its.c | 4 +- drivers/irqchip/irq-gic-v4.c | 19 ++++++ include/linux/irqchip/arm-gic-v4.h | 1 + 6 files changed, 131 insertions(+), 13 deletions(-)
arch/arm64/kvm/arm.c +11 -2
@@ -1964,8 +1964,17 @@ int kvm_arch_vm_ioctl(struct file *filp, unsigned int ioctl, unsigned long arg)return kvm_vgic_enable_vcpu_vlpi (vcpu); }case KVM_DISABLE_VCPU_VLPI : { -/* TODO: create ioctl handler function */ -return -ENOSYS ; +int vcpu_id; +struct kvm_vcpu *vcpu; + +if (copy_from_user (&vcpu_id, argp,sizeof (vcpu_id))) +return -EFAULT ; + + vcpu =kvm_get_vcpu_by_id (kvm, vcpu_id); +if (!vcpu) +return -EINVAL ; + +return kvm_vgic_disable_vcpu_vlpi (vcpu); }case KVM_QUERY_VCPU_VLPI : {int vcpu_id;
arch/arm64/kvm/vgic/vgic-v4.c +95 -10
148 lines not on this page · raw message
arch/arm64/kvm/vgic/vgic.h +2 -0
@@ -438,6 +438,7 @@ static inline bool vgic_supports_direct_irqs(struct kvm *kvm)int vgic_v4_init (struct kvm *kvm);int vgic_v4_vcpu_init (struct kvm_vcpu *vcpu);void vgic_v4_teardown (struct kvm *kvm); +int vgic_v4_vcpu_teardown (struct kvm_vcpu *vcpu);void vgic_v4_configure_vsgis (struct kvm *kvm);void vgic_v4_get_vlpi_state (struct vgic_irq *irq,bool *val);int vgic_v4_request_vpe_irq (struct kvm_vcpu *vcpu,int irq);
@@ -472,6 +473,7 @@ void vgic_its_debug_destroy(struct kvm_device *dev);bool kvm_per_vcpu_vlpi_supported (void );int kvm_vgic_enable_vcpu_vlpi (struct kvm_vcpu *vcpu); +int kvm_vgic_disable_vcpu_vlpi (struct kvm_vcpu *vcpu);int kvm_vgic_query_vcpu_vlpi (struct kvm_vcpu *vcpu);#endif
drivers/irqchip/irq-gic-v3-its.c +3 -1
@@ -4600,7 +4600,9 @@ static void its_vpe_irq_domain_free(struct irq_domain *domain, unsigned int virq, unsigned int nr_irqs) { +#ifndef CONFIG_ARM_GIC_V3_PER_VCPU_VLPI struct its_vm *vm = domain->host_data; +#endif int i;irq_domain_free_irqs_parent (domain, virq, nr_irqs);
@@ -4610,11 +4612,11 @@ static void its_vpe_irq_domain_free(struct irq_domain *domain, virq + i);struct its_vpe *vpe =irq_data_get_irq_chip_data (data); -BUG_ON (vm != vpe->its_vm);#ifdef CONFIG_ARM_GIC_V3_PER_VCPU_VLPI free_lpi_range (vpe->vpe_db_lpi,1 );#else +BUG_ON (vm != vpe->its_vm);clear_bit (data->hwirq, vm->db_bitmap);#endif its_vpe_teardown (vpe);
drivers/irqchip/irq-gic-v4.c +19 -0
@@ -270,6 +270,25 @@ void its_free_vcpu_irqs(struct its_vm *vm)irq_domain_free_fwnode (vm->fwnode); } +void its_free_vcpu_irq (struct kvm_vcpu *vcpu) +{ +struct its_vpe *vpe = &vcpu->arch.vgic_cpu.vgic_v3.its_vpe; +unsigned int irq =irq_find_mapping (vpe->lpi_domain,0 ); + +if (WARN_ON (!irq)) +return ; + +irq_domain_free_irqs (irq,1 ); +irq_domain_remove (vpe->lpi_domain); +irq_domain_free_fwnode (vpe->lpi_fwnode); + +/* Reset vPE fields to prevent stale references during re-enablement */ + vpe->its_vm =NULL ; + vpe->irq =0 ; + vpe->lpi_domain =NULL ; + vpe->lpi_fwnode =NULL ; +} +static int its_send_vpe_cmd (struct its_vpe *vpe,struct its_cmd_info *info) {return irq_set_vcpu_affinity (vpe->irq, info);
include/linux/irqchip/arm-gic-v4.h +1 -0
@@ -146,6 +146,7 @@ struct its_cmd_info {int its_alloc_vcpu_irqs (struct its_vm *vm);int its_alloc_vcpu_irq (struct kvm_vcpu *vcpu);void its_free_vcpu_irqs (struct its_vm *vm); +void its_free_vcpu_irq (struct kvm_vcpu *vcpu);int its_make_vpe_resident (struct its_vpe *vpe,bool g0en,bool g1en);int its_make_vpe_non_resident (struct its_vpe *vpe,bool db);int its_commit_vpe (struct its_vpe *vpe);
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, Christof Hellmis Eingetragen am Amtsgericht Charlottenburg unter HRB 257764 B Sitz: Berlin Ust-ID: DE 365 538 597
RFC PATCH 8/13 KVM: arm64: Make per-vCPU vLPI control ioctls atomic
In reply to Maximilian DittgenPer-vCPU vLPI enable, disable, and query ioctls should finish reading or writing the state of their target vCPU before another operation does the same.
Implement a vlpi_toggle_mutex to be acquired whenever KVM_ENABLE_VCPU_VLPI, KVM_DISABLE_VCPU_VLPI, or KVM_QUERY_VCPU_VLPI ioctls are handled.
--- arch/arm64/kvm/arm.c | 3 +++ arch/arm64/kvm/vgic/vgic-v4.c | 5 +++++ include/kvm/arm_vgic.h | 3 +++ 3 files changed, 11 insertions(+)
arch/arm64/kvm/arm.c +3 -0
@@ -1961,6 +1961,7 @@ int kvm_arch_vm_ioctl(struct file *filp, unsigned int ioctl, unsigned long arg)if (!vcpu)return -EINVAL ; +guard (mutex)(&vcpu->arch.vgic_cpu.vlpi_toggle_mutex);return kvm_vgic_enable_vcpu_vlpi (vcpu); }case KVM_DISABLE_VCPU_VLPI : {
@@ -1974,6 +1975,7 @@ int kvm_arch_vm_ioctl(struct file *filp, unsigned int ioctl, unsigned long arg)if (!vcpu)return -EINVAL ; +guard (mutex)(&vcpu->arch.vgic_cpu.vlpi_toggle_mutex);return kvm_vgic_disable_vcpu_vlpi (vcpu); }case KVM_QUERY_VCPU_VLPI : {
@@ -1987,6 +1989,7 @@ int kvm_arch_vm_ioctl(struct file *filp, unsigned int ioctl, unsigned long arg)if (!vcpu)return -EINVAL ; +guard (mutex)(&vcpu->arch.vgic_cpu.vlpi_toggle_mutex);return kvm_vgic_query_vcpu_vlpi (vcpu); }default :
arch/arm64/kvm/vgic/vgic-v4.c +5 -0
@@ -325,6 +325,11 @@ int vgic_v4_init(struct kvm *kvm)if (!dist->its_vm.vprop_page) ret = -ENOMEM; } + +/* vLPI toggle mutex */ +kvm_for_each_vcpu (i, vcpu, kvm) +mutex_init (&vcpu->arch.vgic_cpu.vlpi_toggle_mutex); +#endif if (ret)vgic_v4_teardown (kvm);
include/kvm/arm_vgic.h +3 -0
@@ -372,6 +372,9 @@ struct vgic_cpu {u32 rdreg_index;atomic_t syncr_busy; +/* Ensure atomicity of per-vCPU vLPI enable/disable/query operations */ +struct mutex vlpi_toggle_mutex; +/* Contains the attributes and gpa of the LPI pending tables. */ u64 pendbaser;/* GICR_CTLR.{ENABLE_LPIS,RWP} */
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, Christof Hellmis Eingetragen am Amtsgericht Charlottenburg unter HRB 257764 B Sitz: Berlin Ust-ID: DE 365 538 597
RFC PATCH 9/13 KVM: arm64: Couple vSGI enablement with per-vCPU vPE allocation
In reply to Maximilian DittgenWhen KVM_ENABLE_VCPU_VLPI is enabled, vCPU vPEs are dynamically allocated and dislocated ad-hoc. vSGI direct injection requires receiving vCPUs to have vPEs, meaning we must couple vSGI enablement with vPE allocation to avoid injecting vSGIs to nonexistent vPEs.
Modify vgic_v4_configure_sgis() to validate whether a target vCPU has an assigned vPE before calling vgic_v4_enable_vsgis() on boot. Call vgic_v4_enable_vsgis() and vgic_v4_disable_vsgis() during vCPU vPE alloc and free within vLPI enablement and disablement functions.
--- arch/arm64/kvm/vgic/vgic-v4.c | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-)
arch/arm64/kvm/vgic/vgic-v4.c +10 -1
@@ -198,7 +198,7 @@ void vgic_v4_configure_vsgis(struct kvm *kvm)kvm_arm_halt_guest (kvm);kvm_for_each_vcpu (i, vcpu, kvm) { -if (dist->nassgireq) +if (dist->nassgireq &&kvm_vgic_query_vcpu_vlpi (vcpu) >0 )vgic_v4_enable_vsgis (vcpu);else vgic_v4_disable_vsgis (vcpu);
@@ -838,6 +838,13 @@ int kvm_vgic_enable_vcpu_vlpi(struct kvm_vcpu *vcpu)if (ret)return ret; +/* Enable direct vSGIs */ +if (kvm_vgic_global_state.has_gicv4_1 && vcpu->kvm->arch.vgic.nassgireq) { +mutex_lock (&vcpu->kvm->arch.config_lock); +vgic_v4_enable_vsgis (vcpu); +mutex_unlock (&vcpu->kvm->arch.config_lock); + } +/* * Upgrade existing LPIs to vLPIs. We * do not need to error check since
@@ -859,6 +866,8 @@ int kvm_vgic_disable_vcpu_vlpi(struct kvm_vcpu *vcpu)downgrade_existing_vlpis_to_lpis (vcpu); +vgic_v4_disable_vsgis (vcpu); +return vgic_v4_vcpu_teardown (vcpu); }
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, Christof Hellmis Eingetragen am Amtsgericht Charlottenburg unter HRB 257764 B Sitz: Berlin Ust-ID: DE 365 538 597
RFC PATCH 10/13 KVM: selftests: fix MAPC RDbase target formatting in vgic_lpi_stress
In reply to Maximilian DittgenSince GITS_TYPER.PTA == 0, the ITS MAPC command demands a CPU ID, rather than a physical redistributor address, for its RDbase command argument.
As such, when MAPC-ing guest ITS collections, vgic_lpi_stress iterates over CPU IDs in the range [0, nr_cpus), passing them as the RDbase vcpu_id argument to its_send_mapc_cmd().
However, its_encode_target() in the its_send_mapc_cmd() selftest handler expects RDbase arguments to have a 16 bit offset, as shown by the 16-bit target_addr right shift its implementation:
its_mask_encode(&cmd->raw_cmd[2], target_addr >> 16, 51, 16)
At the moment, all CPU IDs passed into its_send_mapc_cmd() have no offset, therefore becoming 0x0 after the bit shift. Thus, when vgic_its_cmd_handle_mapc() receives the ITS command in vgic-its.c, it always interprets the RDbase target CPU as CPU 0. All interrupts sent to collections will be processed by vCPU 0, which defeats the purpose of this multi-vCPU test.
Fix by creating procnum_to_rdbase() helper function, which left-shifts the vCPU parameter received by its_send_mapc_cmd 16 bits before passing it to its_encode_target for encoding.
---
This patch has already been merged as a fix in Linux 6.18-rc6 as a24f7af.
--- tools/testing/selftests/kvm/lib/arm64/gic_v3_its.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-)
tools/testing/selftests/kvm/lib/arm64/gic_v3_its.c +8 -1
@@ -15,6 +15,8 @@#include "gic_v3.h" #include "processor.h" +#define GITS_COLLECTION_TARGET_SHIFT 16 +static u64 its_read_u64 (unsigned long offset) {return readq_relaxed (GITS_BASE_GVA + offset);
@@ -163,6 +165,11 @@ static void its_encode_collection(struct its_cmd_block *cmd, u16 col)its_mask_encode (&cmd->raw_cmd[2 ], col,15 ,0 ); } +static u64 procnum_to_rdbase (u32 vcpu_id) +{ +return vcpu_id <<GITS_COLLECTION_TARGET_SHIFT ; +} +#define GITS_CMDQ_POLL_ITERATIONS 0static void its_send_cmd (void *cmdq_base,struct its_cmd_block *cmd)
@@ -217,7 +224,7 @@ void its_send_mapc_cmd(void *cmdq_base, u32 vcpu_id, u32 collection_id, bool valits_encode_cmd (&cmd,GITS_CMD_MAPC );its_encode_collection (&cmd, collection_id); -its_encode_target (&cmd, vcpu_id); +its_encode_target (&cmd,procnum_to_rdbase (vcpu_id));its_encode_valid (&cmd, valid);its_send_cmd (cmdq_base, &cmd);
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, Christof Hellmis Eingetragen am Amtsgericht Charlottenburg unter HRB 257764 B Sitz: Berlin Ust-ID: DE 365 538 597
RFC PATCH 11/13 KVM: Ioctl to set up userspace-injected MSIs as software-bypassing vLPIs
In reply to Maximilian DittgenAt the moment, all MSIs injected from userspace using KVM_SIGNAL_MSI are preempted by the hypervisor and handled by software. 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.
--- arch/arm64/kvm/arm.c | 34 +++++++ arch/arm64/kvm/vgic/vgic-its.c | 138 +++++++++++++++++++++++++++++ arch/arm64/kvm/vgic/vgic.h | 1 + include/linux/irqchip/arm-gic-v3.h | 1 + include/uapi/linux/kvm.h | 15 ++++ 5 files changed, 189 insertions(+)
arch/arm64/kvm/arm.c +34 -0
@@ -45,6 +45,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 ;
@@ -1992,6 +1994,38 @@ int kvm_arch_vm_ioctl(struct file *filp, unsigned int ioctl, unsigned long arg)guard (mutex)(&vcpu->arch.vgic_cpu.vlpi_toggle_mutex);return kvm_vgic_query_vcpu_vlpi (vcpu); } +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 (¶ms, argp,sizeof (params))) +return -EFAULT ; + +/* validate vcpu_id is in range and exists */ + vcpu =kvm_get_vcpu_by_id (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, ¶ms); + }default :return -EINVAL ; }
arch/arm64/kvm/vgic/vgic-its.c +138 -0
141 lines not on this page · raw message
arch/arm64/kvm/vgic/vgic.h +1 -0
@@ -475,5 +475,6 @@ bool kvm_per_vcpu_vlpi_supported(void);int kvm_vgic_enable_vcpu_vlpi (struct kvm_vcpu *vcpu);int kvm_vgic_disable_vcpu_vlpi (struct kvm_vcpu *vcpu);int kvm_vgic_query_vcpu_vlpi (struct kvm_vcpu *vcpu); +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
@@ -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
@@ -1457,6 +1457,21 @@ struct kvm_enc_region {#define KVM_DISABLE_VCPU_VLPI _IOW(KVMIO, 0xf1, int)#define KVM_QUERY_VCPU_VLPI _IOR(KVMIO, 0xf2, int) +/* +* 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, 0xf3, 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)
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, Christof Hellmis Eingetragen am Amtsgericht Charlottenburg unter HRB 257764 B Sitz: Berlin Ust-ID: DE 365 538 597
RFC PATCH 12/13 KVM: arm64: selftests: Add support for stress testing direct-injected vLPIs
In reply to Maximilian Dittgenvgic_lpi_stress stress tests LPIs by injecting MSIs to a guest's GIC. Since these MSIs are purely defined in userspace, they are handled as LPIs by the hypervisor as software interrupts.
We provide two ways to stress test direct-injected vLPIs.
- When per-vCPU vLPI injection is disabled, use -D flag to upgrade
all LPIs fired by the stress test to vLPIs. This flag mocks a host_irq for each MSI and calls KVM_DEBUG_GIC_MSI_SETUP to create and map vITS data structures needed for direct injection.
- When per-vCPU vLPI injection is enabled, use -s flag to pass
per-vCPU command strings to control the state of vPE initialization on each vCPU throughout the test. Allows stress testing vLPI injection on partially-enabled VMs.
--- .../selftests/kvm/arm64/vgic_lpi_stress.c | 181 +++++++++++++++++- 1 file changed, 177 insertions(+), 4 deletions(-)
tools/testing/selftests/kvm/arm64/vgic_lpi_stress.c +177 -4
@@ -18,9 +18,17 @@#include "ucall.h" #include "vgic.h" +#define KVM_DEBUG_GIC_MSI_SETUP _IOW(KVMIO, 0xf3, struct kvm_debug_gic_msi_setup) +#define TEST_MEMSLOT_INDEX 1#define GIC_LPI_OFFSET 8192 +#define SPI_IRQ_RANGE_OFFSET 32 + +static bool vlpi_enabled; +static bool string_mode; +static char **vcpu_strings; +static bool *vcpu_enabled;static size_t nr_iterations =1000 ;static vm_paddr_t gpa_base;
@@ -222,6 +230,79 @@ static void setup_gic(void) its_fd =vgic_its_setup (vm); } +static int enable_msi_vlpi_injection (u32 device_id,u32 event_id, +u32 vcpu_id,u32 vintid,u32 host_irq) +{ +struct kvm_debug_gic_msi_setup params = { + .device_id = device_id, + .event_id = event_id, + .vcpu_id = vcpu_id, + .vintid = vintid, + .host_irq = host_irq, + .itt_addr = test_data.itt_tables + (device_id *SZ_64K ) + }; + +return __vm_ioctl (vm,KVM_DEBUG_GIC_MSI_SETUP , ¶ms); +} + +static void upgrade_vcpu_lpis (struct kvm_vcpu *vcpu) +{ +u32 intid =GIC_LPI_OFFSET ; +u32 target_vcpu =0 ;/* Start round-robin from vCPU 0 */ +u32 device_id, event_id; + +for (device_id =0 ; device_id < test_data.nr_devices; device_id++) { +for (event_id =0 ; event_id < test_data.nr_event_ids; + event_id++) { +/* +* Only setup vLPI mapping if this is the target vCPU +* for this interrupt +*/ +if (target_vcpu == vcpu->id) { +/* +* we mock host_irqs in the SPI interrupt range +* of 100-1020 since selftest guests have no +* hardware devices +*/ +int ret =enable_msi_vlpi_injection (device_id, + event_id, vcpu->id, intid, + intid -GIC_LPI_OFFSET +SPI_IRQ_RANGE_OFFSET ); + +if (ret == -ENXIO || ret ==-1 ) { +pr_info ("Direct vLPI injection is disabled for vCPU %d, defaulting to software LPI handling\n" , + vcpu->id); +return ; + } +TEST_ASSERT (ret ==0 , +"KVM_DEBUG_GIC_MSI_SETUP failed: %d\n" , + ret); + } + + intid++; + target_vcpu = (target_vcpu +1 ) % test_data.nr_cpus; + } + } +} + + + +static void enable_vcpu_vlpi_injection (int vcpu_id) +{ +int ret =ioctl (vm->fd,KVM_ENABLE_VCPU_VLPI , &vcpu_id); + +TEST_ASSERT (ret ==0 ,"KVM_ENABLE_VCPU_VLPI failed: %d" , ret); +pr_info ("Enabled vLPI injection on vCPU %d\n" , vcpu_id); +upgrade_vcpu_lpis (vcpus[vcpu_id]); +} + +static void disable_vcpu_vlpi_injection (int vcpu_id) +{ +int ret =ioctl (vm->fd,KVM_DISABLE_VCPU_VLPI , &vcpu_id); + +TEST_ASSERT (ret ==0 ,"KVM_DISABLE_VCPU_VLPI failed: %d" , ret); +pr_info ("Disabled vLPI injection on vCPU %d\n" , vcpu_id); +} +static void signal_lpi (u32 device_id,u32 event_id) {vm_paddr_t db_addr =GITS_BASE_GPA +GITS_TRANSLATER ;
@@ -243,18 +324,36 @@ static void signal_lpi(u32 device_id, u32 event_id) }static pthread_barrier_t test_setup_barrier; +static pthread_barrier_t vlpi_upgrade_barrier;static void *lpi_worker_thread (void *data) {u32 device_id = (size_t )data;u32 event_id;size_t i; +int vcpu_id;pthread_barrier_wait (&test_setup_barrier); - -for (i =0 ; i < nr_iterations; i++) +pthread_barrier_wait (&vlpi_upgrade_barrier); + +for (i =0 ; i < nr_iterations; i++) { +/* conduct per-vCPU vLPI enablement/disablement */ +if (string_mode) { +for (vcpu_id =0 ; vcpu_id < test_data.nr_cpus; vcpu_id++) { +char action = vcpu_strings[vcpu_id][i]; + +if (action =='e' && !vcpu_enabled[vcpu_id]) { +enable_vcpu_vlpi_injection (vcpu_id); + vcpu_enabled[vcpu_id] = true; + }else if (action =='d' && vcpu_enabled[vcpu_id]) { +disable_vcpu_vlpi_injection (vcpu_id); + vcpu_enabled[vcpu_id] = false; + } + } + }for (event_id =0 ; event_id < test_data.nr_event_ids; event_id++)signal_lpi (device_id, event_id); + }return NULL ; }
@@ -270,6 +369,10 @@ static void *vcpu_worker_thread(void *data)switch (get_ucall (vcpu, &uc)) {case UCALL_SYNC :pthread_barrier_wait (&test_setup_barrier); +/* if flag is set, set direct injection mappings for MSIs */ +if (vlpi_enabled) +upgrade_vcpu_lpis (vcpu); +pthread_barrier_wait (&vlpi_upgrade_barrier);continue ;case UCALL_DONE :return NULL ;
@@ -309,6 +412,7 @@ static void run_test(void)TEST_ASSERT (lpi_threads && vcpu_threads,"Failed to allocate pthread arrays" );pthread_barrier_init (&test_setup_barrier,NULL , nr_vcpus + nr_devices +1 ); +pthread_barrier_init (&vlpi_upgrade_barrier,NULL , nr_vcpus + nr_devices +1 );for (i =0 ; i < nr_vcpus; i++)pthread_create (&vcpu_threads[i],NULL , vcpu_worker_thread, vcpus[i]);
@@ -317,6 +421,7 @@ static void run_test(void)pthread_create (&lpi_threads[i],NULL , lpi_worker_thread, (void *)i);pthread_barrier_wait (&test_setup_barrier); +pthread_barrier_wait (&vlpi_upgrade_barrier);/* Wait for all vLPI upgrades */ clock_gettime (CLOCK_MONOTONIC , &start);
@@ -361,13 +466,71 @@ static void destroy_vm(void)free (vcpus); } +static int parse_vcpu_strings (const char *str) +{ +char *token, *saveptr, *str_copy; +int count =0 , len =-1 , i; + + str_copy =strdup (str); +TEST_ASSERT (str_copy,"Failed to allocate string copy" ); + + token =strtok_r (str_copy,"," , &saveptr); +while (token) { + count++; + token =strtok_r (NULL ,"," , &saveptr); + } +free (str_copy); + + vcpu_strings =malloc (count *sizeof (char *)); + vcpu_enabled =calloc (count,sizeof (bool )); +TEST_ASSERT (vcpu_strings && vcpu_enabled,"Failed to allocate arrays" ); + + str_copy =strdup (str); + token =strtok_r (str_copy,"," , &saveptr); +for (i =0 ; i < count; i++) { +int token_len =strlen (token); + +if (len ==-1 ) + len = token_len; +else if (len != token_len) +TEST_FAIL ("All strings must have same length" ); + +TEST_ASSERT (len >0 ,"Strings cannot be empty" ); + +for (int j =0 ; j < token_len; j++) +if (token[j] !='d' && token[j] !='e' ) +TEST_FAIL ("Strings can only contain 'd' and 'e'" ); + + vcpu_strings[i] =strdup (token); +TEST_ASSERT (vcpu_strings[i],"Failed to allocate string" ); + token =strtok_r (NULL ,"," , &saveptr); + } +free (str_copy); + + test_data.nr_cpus = count; + test_data.nr_devices =1 ; + test_data.nr_event_ids = count; + nr_iterations = len; + +return 0 ; +} +static void pr_usage (const char *name) { -pr_info ("%s [-v NR_VCPUS] [-d NR_DEVICES] [-e NR_EVENTS] [-i ITERS] -h\n" , name); +pr_info ("%s -D [-v NR_VCPUS] [-d NR_DEVICES] [-e NR_EVENTS] [-i ITERS] | -s STRINGS -h\n" , + name); +pr_info (" -D:\tenable direct vLPI injection (default: %s)\n" , + vlpi_enabled ?"true" :"false" );pr_info (" -v:\tnumber of vCPUs (default: %u)\n" , test_data.nr_cpus);pr_info (" -d:\tnumber of devices (default: %u)\n" , test_data.nr_devices);pr_info (" -e:\tnumber of event IDs per device (default: %u)\n" , test_data.nr_event_ids);pr_info (" -i:\tnumber of iterations (default: %lu)\n" , nr_iterations); +pr_info (" -s:\tvCPU control strings (comma-separated, e.g., \"dede,eede\"),\n" ); +pr_info (" \twhere each string corresponds to the per-iteration vLPI status\n" ); +pr_info (" \tof a single vCPU. \"ddeed\" means a vCPU will be vLPI-disabled for two\n" ); +pr_info (" \titerations, enabled for two iterations, then disabled for one iteration.\n" ); +pr_info (" \tNumber of strings corresponds to the number of vCPUs, and all strings must\n" ); +pr_info (" \tbe of the same size. Cannot be used in conjunction with other flags.\n" ); }int main (int argc,char **argv)
@@ -377,8 +540,11 @@ int main(int argc, char **argv)TEST_REQUIRE (kvm_supports_vgic_v3 ()); -while ((c =getopt (argc, argv,"hv:d:e:i:" )) !=-1 ) { +while ((c =getopt (argc, argv,"hDv:d:e:i:s:" )) !=-1 ) {switch (c) { +case 'D' : + vlpi_enabled = true; +break ;case 'v' : test_data.nr_cpus =atoi (optarg);break ;
@@ -391,6 +557,10 @@ int main(int argc, char **argv)case 'i' : nr_iterations =strtoul (optarg,NULL ,0 );break ; +case 's' : + string_mode = true; +parse_vcpu_strings (optarg); +break ;case 'h' :default :pr_usage (argv[0 ]);
@@ -398,6 +568,9 @@ int main(int argc, char **argv) } } +if (string_mode && argc >3 ) +TEST_FAIL ("-s cannot be used with other flags" ); + nr_threads = test_data.nr_cpus + test_data.nr_devices;if (nr_threads >get_nprocs ()) pr_info("WARNING: running %u threads on %d CPUs; performance is degraded.\n",
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, Christof Hellmis Eingetragen am Amtsgericht Charlottenburg unter HRB 257764 B Sitz: Berlin Ust-ID: DE 365 538 597
RFC PATCH 13/13 KVM: arm64: selftests: Add test for per-vCPU vLPI control API
In reply to Maximilian DittgenAdd a selftest for KVM API ioctls for enabling, disabling, and querying direct vLPI injection capability on a per-vCPU basis. Ensure that ITS data structures remain correct, vPEIDs can be reused by different vCPUs, and ioctl behavior works as intended in corner cases (idempotent behavior, vGIC uninitialized).
--- arch/arm64/kvm/arm.c | 4 + drivers/irqchip/irq-gic-v3-its.c | 6 + include/linux/irqchip/arm-gic-v4.h | 1 + include/uapi/linux/kvm.h | 1 + tools/testing/selftests/kvm/Makefile.kvm | 1 + .../selftests/kvm/arm64/per_vcpu_vlpi.c | 274 ++++++++++++++++++ 6 files changed, 287 insertions(+) create mode 100644 tools/testing/selftests/kvm/arm64/per_vcpu_vlpi.c
arch/arm64/kvm/arm.c +4 -0
@@ -21,6 +21,7 @@#include <linux/sched/stat.h> #include <linux/psci.h> #include <trace/events/kvm.h> +#include <linux/irqchip/arm-gic-v4.h> #define CREATE_TRACE_POINTS #include "trace_arm.h"
@@ -429,6 +430,9 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)case KVM_CAP_ARM_PER_VCPU_VLPI : r =kvm_per_vcpu_vlpi_supported ();break ; +case KVM_CAP_ARM_MAX_VPEID : + r =its_get_max_vpeid (); +break ;default : r =0 ;
drivers/irqchip/irq-gic-v3-its.c +6 -0
@@ -4546,6 +4546,12 @@ static const struct irq_domain_ops its_sgi_domain_ops = { .deactivate = its_sgi_irq_domain_deactivate, }; +int its_get_max_vpeid (void ) +{ +return ITS_MAX_VPEID ; +} +EXPORT_SYMBOL_GPL (its_get_max_vpeid); +static int its_vpe_id_alloc (void ) {return ida_alloc_max (&its_vpeid_ida,ITS_MAX_VPEID -1 ,GFP_KERNEL );
include/linux/irqchip/arm-gic-v4.h +1 -0
@@ -147,6 +147,7 @@ int its_alloc_vcpu_irqs(struct its_vm *vm);int its_alloc_vcpu_irq (struct kvm_vcpu *vcpu);void its_free_vcpu_irqs (struct its_vm *vm);void its_free_vcpu_irq (struct kvm_vcpu *vcpu); +int its_get_max_vpeid (void );int its_make_vpe_resident (struct its_vpe *vpe,bool g0en,bool g1en);int its_make_vpe_non_resident (struct its_vpe *vpe,bool db);int its_commit_vpe (struct its_vpe *vpe);
include/uapi/linux/kvm.h +1 -0
@@ -974,6 +974,7 @@ struct kvm_enable_cap {#define KVM_CAP_GUEST_MEMFD_FLAGS 244#define KVM_CAP_ARM_SEA_TO_USER 245#define KVM_CAP_ARM_PER_VCPU_VLPI 246 +#define KVM_CAP_ARM_MAX_VPEID 247struct kvm_irq_routing_irqchip {__u32 irqchip;
tools/testing/selftests/kvm/Makefile.kvm +1 -0
@@ -162,6 +162,7 @@ TEST_GEN_PROGS_arm64 += arm64/host_sve TEST_GEN_PROGS_arm64 += arm64/hypercalls TEST_GEN_PROGS_arm64 += arm64/external_aborts TEST_GEN_PROGS_arm64 += arm64/page_fault_test +TEST_GEN_PROGS_arm64 += arm64/per_vcpu_vlpi TEST_GEN_PROGS_arm64 += arm64/psci_test TEST_GEN_PROGS_arm64 += arm64/sea_to_user TEST_GEN_PROGS_arm64 += arm64/set_id_regs
tools/testing/selftests/kvm/arm64/per_vcpu_vlpi.c new file +274 -0
274 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, Christof Hellmis Eingetragen am Amtsgericht Charlottenburg unter HRB 257764 B Sitz: Berlin Ust-ID: DE 365 538 597
Marc Zyngier
maintainerMaximilian: you keep ignoring the reviewers that are listed in MAINTAINERS. This isn't acceptable. Next time, I will simply ignore your patches.
Quoted 43 lines · Maximilian Dittgen
At the moment, the ability to direct-inject vLPIs is only enableable on an all-or-nothing per-VM basis, causing unnecessary I/O performance loss in cases where a VM's vCPU count exceeds available vPEs. This RFC introduces per-vCPU control over vLPI injection to realize potential I/O performance gain in such situations.
Background
----------The value of dynamically enabling the direct injection of vLPIs on a per-vCPU basis is the ability to run guest VMs with simultaneous hardware-forwarded and software-forwarded message-signaled interrupts.
Currently, hardware-forwarded vLPI direct injection on a KVM guest requires GICv4 and is enabled on a per-VM, all-or-nothing basis. vLPI injection enablment happens in two stages:
1) At vGIC initialization, allocate direct injection structures for each vCPU (doorbell IRQ, vPE table entry, virtual pending table, vPEID).
2) When a PCI device is configured for passthrough, map its MSIs to vLPIs using the structures allocated in step 1.Step 1 is all-or-nothing; if any vCPU cannot be configured with the vPE structures necessary for direct injection, the vPEs of all vCPUs are torn down and direct injection is disabled VM-wide.
This universality of direct vLPI injection enablement sparks several issues, with the most pressing being performance degradation on overcommitted hosts.
VM-wide vLPI enablement creates resource inefficiency when guest VMs have more vCPUs than the host has available vPEIDs. The amount of vPEIDs (and consequently, vPEs) a host can allocate is constrained by hardware and defined by GICD_TYPER2.VID + 1 (
ITS_MAX_VPEID). Since direct injection requires a vCPU to be assigned a vPEID, at mostITS_MAX_VPEIDvCPUs can be configured for direct injection at a time. Because vLPI direct injection is all-or-nothing on a VM, if a new guest VM would exhaust remaining vPEIDs, all vCPUs on that VM would fall back to hypervisor-forwarded LPIs, causing considerable I/O performance degradation.
Quoted 3 lines
Such performance degradation is exemplified on hosts with CPU overcommitment. Overcommitting an arbitrarily high number of vCPUs enables a VM's vCPU count to easily exceed the host's available vPEIDs.
Let it be crystal clear: GICv4 and overcommitment is a non-story. It isn't designed for that. If that's what you are trying to achieve, you clearly didn't get the memo.
Quoted 4 lines
Even with marginally more vCPUs than vPEIDs, the current all-or-nothing vLPI paradigm disables direct injection entirely. This creates two
problems: first, a single many-vCPU overcommitted VM loses all direct injection despite having vPEIDs available;
Are you saying that your HW is so undersized that you cannot create a *single VM* with direct injection? You really have fewer than 9 bit worth of VPEIDs? I'm sorry, but that's laughable. Even a $200 dev board does better.
Quoted 8 lines
second, on multi-tenant
hosts, VMs booted first consume all vPEIDs, leaving later VMs without direct injection regardless of their I/O intensity. Per-vCPU control would allow userspace to allocate available vPEIDs across VMs based on I/O workload rather than boot order or per-VM vCPU count. This per-vCPU granularity recovers most of the direct injection performance benefit instead of losing it completely.
Quoted 4 lines
To allow this per-vCPU granularity, this RFC introduces three new ioctls to the KVM API that enables userspace the ability to activate/deactivate direct vLPI injection capability and resources to vCPUs ad-hoc during VM runtime.
How can that even work when changing the affinity of a vLPI (directly injected) to a vcpu that doesn't have direct injection enabled? You'd have to unmap the vLPI, and plug it back as a normal LPI. Not only this is absolutely ridiculous from a performance perspective, but you are also guaranteed to lose interrupts that would have fired in the meantime. Losing interrupts in a total no-go.
Before I even look at the code, I you to explain how you are dealing with this.
M.
Signature · 1 line
Without deviation from the norm, progress is not possible.
Marc Zyngier
maintainerRe: [RFC PATCH 01/13] KVM: Introduce config option for per-vCPU vLPI enablement
In reply to Maximilian DittgenQuoted 64 lines · Maximilian Dittgen
Add
CONFIG_ARM_GIC_V3_PER_VCPU_VLPIto control whether vLPI direct injection is to be enabled on a system-wide or a per-vCPU basis.When enabled, vPEs can be allocated/deallocated to vCPUs on an ad-hoc, per-vCPU basis in runtime. When disabled, keep current
vgic_v4_initbehavior of automatic vCPU vPE allocation upon VM initialization.We declare three ioctls numbers to manage per-vCPU vLPI enablement:
-KVM_ENABLE_VCPU_VLPI, which given a vCPU ID, allocates a vPE and initializes the vCPU for receiving direct vLPI interrupts.
-KVM_DISABLE_VCPU_VLPI, which given a vCPU ID, disables the vCPU’s ability to receive direct vLPI interrupts and frees its underlying vPE structure.
-KVM_QUERY_VCPU_VLPI, which given a vCPU ID, returns a boolean describing whether the vCPU is configured to receive direct vLPI interrupts.This commit declares the kconfig, ioctl numbers, and documentation. Implementation will come throughout this patch set.
Signed-off-by Maximilian Dittgen <[email protected]>--- Documentation/virt/kvm/api.rst | 56 ++++++++++++++++++++++++++++++++++ arch/arm64/kvm/arm.c | 15 +++++++++ arch/arm64/kvm/vgic/vgic-v4.c | 9 ++++++ arch/arm64/kvm/vgic/vgic.h | 2 ++ drivers/irqchip/Kconfig | 13 ++++++++ include/uapi/linux/kvm.h | 6 ++++ 6 files changed, 101 insertions(+)Documentation/virt/kvm/api.rst +25
-0@@ -6517,6 +6517,62 @@ the capability to be present. `flags` must currently be zero. +4.XXX KVM_ENABLE_VCPU_VLPI +-------------------------- + +:Capability: KVM_CAP_ARM_PER_VCPU_VLPI +:Architectures: arm64 +:Type: vm ioctl +:Parameters: int vcpu_id (in) +:Returns: 0 on success, negative value on error + +This ioctl enables GICv4 direct vLPI injection for the specified vCPU. +Allocates vPE structures (doorbell IRQ, vPE table entry, virtual pending +table, vPEID) and upgrades existing software-forwarded LPIs targeting +this vCPU to hardware-forwarded vLPIs. + +If GICv4.1 is supported and vSGIs are disabled on the specified vCPU, +this ioctl enables vCPU vSGI support. + +Requires CONFIG_ARM_GIC_V3_PER_VCPU_VLPI and GICv4 hardware support. + +Returns -EINVAL if vGICv4 is not initialized or if the passed vcpu_id +does not map to a vCPU. + +4.XXX KVM_DISABLE_VCPU_VLPI +--------------------------- +
Documentation/virt/kvm/api.rst the capability to be present. · line 6554
Quoted 10 lines
Documentation/virt/kvm/api.rst +10
-0+:Capability: KVM_CAP_ARM_PER_VCPU_VLPI +:Architectures: arm64 +:Type: vm ioctl +:Parameters: int vcpu_id (in) +:Returns: 0 on success, negative value on error + +This ioctl disables GICv4 direct vLPI injection for the specified vCPU. +Downgrades hardware-forwarded vLPIs to software-forwarded LPIs and frees +vPE structures. Pending interrupts in the virtual pending table may be +lost.
I'm going to put my foot down on that immediately.
There is no conceivable case where losing interrupts in acceptable. Ever. If that's what you want, please write your own hypervisor. I wish you luck!
Documentation/virt/kvm/api.rst the capability to be present. · line 6556
Quoted 2 lines
Documentation/virt/kvm/api.rst +2
-0+ +If vSGIs are enabled on the specified vCPU, this ioctl disables them.
So what? Something that didn't have an active state now has one that the guest doesn't know about? There is exactly *one* bit that defines that, and it doesn't exist in some quantum superposition.
This whole thing is completely insane, has not been thought out at all, is ignoring the basis of the architecture, and I'm really sorry that you wasted your time on that.
M.
Signature · 1 line
Without deviation from the norm, progress is not possible.
kernel test robot
build botRe: [RFC PATCH 05/13] KVM: arm64: Implement vLPI ENABLE ioctl for per-vCPU vLPI injection API
In reply to Maximilian Dittgenkernel test robot noticed the following build errors:
Hi Maximilian,
[This is a private test report for your RFC patch.] kernel test robot noticed the following build errors:
[auto build test ERROR on kvmarm/next]
[cannot apply to kvm/queue kvm/next tip/irq/core linus/master kvm/linux-next v6.18-rc6 next-20251121] [If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]
url: https://github.com/intel-lab-lkp/linux/commits/Maximilian-Dittgen/KVM-Introduce-config-option-for-per-vCPU-vLPI-enablement/20251120-221612 base: https://git.kernel.org/pub/scm/linux/kernel/git/kvmarm/kvmarm.git next patch link: https://lore.kernel.org/r/20251120140305.63515-6-mdittgen%40amazon.de patch subject: [RFC PATCH 05/13] KVM: arm64: Implement vLPI ENABLE ioctl for per-vCPU vLPI injection API config: arm-randconfig-001-20251123 (https://download.01.org/0day-ci/archive/20251123/[email protected]/config) compiler: clang version 22.0.0git (https://github.com/llvm/llvm-project 9e9fe08b16ea2c4d9867fb4974edf2a3776d6ece) reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20251123/[email protected]/reproduce)
If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/
All errors (new ones prefixed by >>):
In file included from drivers/irqchip/irq-gic-v4.c:10:
In file included from include/linux/kvm_host.h:40:
>> include/uapi/linux/kvm.h:15:10: fatal error: 'asm/kvm.h' file not found
15 | #include <asm/kvm.h>
| ^~~~~~~~~~~1 error generated.
vim +15 include/uapi/linux/kvm.h
6aa8b732ca01c3 include/linux/kvm.h Avi Kivity 2006-12-10 4 6aa8b732ca01c3 include/linux/kvm.h Avi Kivity 2006-12-10 5 /* 6aa8b732ca01c3 include/linux/kvm.h Avi Kivity 2006-12-10 6 * Userspace interface for /dev/kvm - kernel based virtual machine 6aa8b732ca01c3 include/linux/kvm.h Avi Kivity 2006-12-10 7 * dea8caee7b6971 include/linux/kvm.h Rusty Russell 2007-07-17 8 * Note: you must update KVM_API_VERSION if you change this interface. 6aa8b732ca01c3 include/linux/kvm.h Avi Kivity 2006-12-10 9 */ 6aa8b732ca01c3 include/linux/kvm.h Avi Kivity 2006-12-10 10 fb1070d18edb37 include/uapi/linux/kvm.h Joe Richey 2021-05-21 11 #include <linux/const.h> 00bfddaf7f68a6 include/linux/kvm.h Jaswinder Singh Rajput 2009-01-15 12 #include <linux/types.h> 97646202bc3f19 include/linux/kvm.h Christian Borntraeger 2008-03-12 13 #include <linux/compiler.h> 6aa8b732ca01c3 include/linux/kvm.h Avi Kivity 2006-12-10 14 #include <linux/ioctl.h> f6a40e3bdf5fe0 include/linux/kvm.h Jerone Young 2007-11-19 @15 #include <asm/kvm.h> 6aa8b732ca01c3 include/linux/kvm.h Avi Kivity 2006-12-10 16
Signature · 2 lines
0-DAY CI Kernel Test Service https://github.com/intel/lkp-tests/wiki
kernel test robot
build botRe: [RFC PATCH 05/13] KVM: arm64: Implement vLPI ENABLE ioctl for per-vCPU vLPI injection API
In reply to Maximilian Dittgenkernel test robot noticed the following build errors:
Hi Maximilian,
[This is a private test report for your RFC patch.] kernel test robot noticed the following build errors:
[auto build test ERROR on kvmarm/next]
[cannot apply to kvm/queue kvm/next tip/irq/core linus/master kvm/linux-next v6.18-rc6 next-20251121] [If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]
url: https://github.com/intel-lab-lkp/linux/commits/Maximilian-Dittgen/KVM-Introduce-config-option-for-per-vCPU-vLPI-enablement/20251120-221612 base: https://git.kernel.org/pub/scm/linux/kernel/git/kvmarm/kvmarm.git next patch link: https://lore.kernel.org/r/20251120140305.63515-6-mdittgen%40amazon.de patch subject: [RFC PATCH 05/13] KVM: arm64: Implement vLPI ENABLE ioctl for per-vCPU vLPI injection API config: arm-randconfig-003-20251123 (https://download.01.org/0day-ci/archive/20251123/[email protected]/config) compiler: arm-linux-gnueabi-gcc (GCC) 10.5.0 reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20251123/[email protected]/reproduce)
If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/
All errors (new ones prefixed by >>):
In file included from include/linux/kvm_host.h:40,
from drivers/irqchip/irq-gic-v4.c:10:
>> include/uapi/linux/kvm.h:15:10: fatal error: asm/kvm.h: No such file or directory
15 | #include <asm/kvm.h>
| ^~~~~~~~~~~compilation terminated.
vim +15 include/uapi/linux/kvm.h
6aa8b732ca01c3 include/linux/kvm.h Avi Kivity 2006-12-10 4 6aa8b732ca01c3 include/linux/kvm.h Avi Kivity 2006-12-10 5 /* 6aa8b732ca01c3 include/linux/kvm.h Avi Kivity 2006-12-10 6 * Userspace interface for /dev/kvm - kernel based virtual machine 6aa8b732ca01c3 include/linux/kvm.h Avi Kivity 2006-12-10 7 * dea8caee7b6971 include/linux/kvm.h Rusty Russell 2007-07-17 8 * Note: you must update KVM_API_VERSION if you change this interface. 6aa8b732ca01c3 include/linux/kvm.h Avi Kivity 2006-12-10 9 */ 6aa8b732ca01c3 include/linux/kvm.h Avi Kivity 2006-12-10 10 fb1070d18edb37 include/uapi/linux/kvm.h Joe Richey 2021-05-21 11 #include <linux/const.h> 00bfddaf7f68a6 include/linux/kvm.h Jaswinder Singh Rajput 2009-01-15 12 #include <linux/types.h> 97646202bc3f19 include/linux/kvm.h Christian Borntraeger 2008-03-12 13 #include <linux/compiler.h> 6aa8b732ca01c3 include/linux/kvm.h Avi Kivity 2006-12-10 14 #include <linux/ioctl.h> f6a40e3bdf5fe0 include/linux/kvm.h Jerone Young 2007-11-19 @15 #include <asm/kvm.h> 6aa8b732ca01c3 include/linux/kvm.h Avi Kivity 2006-12-10 16
Signature · 2 lines
0-DAY CI Kernel Test Service https://github.com/intel/lkp-tests/wiki