KVM: arm64: Add GICv5 IRS support
Subsystems (8)
v5 cover KVM: arm64: Add GICv5 IRS support
Hi all,
This series builds on the initial vGICv5 support [1] and adds support for the GICv5 IRS, as described by the GICv5 (EAC0) specification [2]. With this, a GICv5 guest is no longer restricted to PPIs, and can make use of SPIs and LPIs as well.
With SPIs and LPIs available, this series makes it possible to boot a full Linux guest on the Arm FVP model [3], using the setup described in Lorenzo's GICv5 Linux software enablement guide [4]. In addition, GICv5 IPIs are typically implemented as LPIs, so LPI support is what makes guests with more than one vCPU possible.
The corresponding kvmtool changes are available separately [5]. With these changes, --irqchip=gicv5 works now. The --irqchip=gicv5-its configuration does not work with this series, as ITS/MSI support is not included here.
The IRS is created as part of the vgic-v5 device, and is not a separate KVM device. This is because implementing the CPU interface alone would limit a guest to a single vCPU and PPIs only. Hence, the IRS is bundled into the VGIC, much like the distributor for GICv3.
The series adds the host-side plumbing needed for KVM to interact with the physical IRS, including VM and VPE table management, VPE residency, and VPE doorbells. It also adds an emulated IRS MMIO interface for the guest, together with the UAPI needed for userspace to configure the IRS address and to save/restore the IRS and IST state.
The main pieces are:
* Host IRS capability discovery and configuration frame plumbing.
* Allocation and management of the GICv5 VM table, VPE table, VMTEs, VPEs, and host-backed guest IST storage.
* VPE residency and doorbell handling, allowing the IRS to select SPIs and LPIs for resident VPEs and wake non-resident VPEs when required.
* Emulation of the GICv5 IRS MMIO CONFIG_FRAME for guests. Virtualisation, MPAM, MEC, and SWERR are not supported and are RAZ/WI.
* SPI injection support using GIC VDPEND. Once injected, the GICv5 hardware manages the SPI lifecycle.
* Save/restore support for GICv5 EL1 system registers, IRS MMIO state, and guest ISTs. SPI and LPI IST state is transferred through userspace-owned buffers using KVM_DEV_ARM_VGIC_GRP_IST.
* Documentation for the new VGICv5 IRS userspace interfaces and the required save/restore ordering.
* Selftests for the UAPI components, the PPI, SPI, and LPI support, and save/restore.
This series keeps SPI, LPI, and save/restore support together intentionally. SPI and LPI support are needed to run realistic guests, while the save/restore interface is included at the same time so that the new userspace ABI is complete from the point it is exposed: userspace should not be able to create VGICv5 state that it cannot also migrate.
The IST migration selftest restores state into a separate VM, re-saves the SPI and LPI IST images before the destination runs, and compares them byte-for-byte with the source images. It also validates restored IRS MMIO state through SPI/LPI delivery, priority, enable, and affinity behavior.
LPIs are explicitly driven by the guest through the IRS/IST state. This series does not add direct LPI injection support, MSI support, or GICv5 ITS emulation.
These changes are based on v7.2-rc6. I've pushed the full set of changes to a branch at [6].
Changes since v4 [7]:
* Rebased onto v7.2-rc6 and addressed review feedback from Marc and Fuad, together with the Sashiko AI reports for v4. Thanks all!
* Aligned ACPI maintenance-interrupt discovery with GICv3. Broken MADT descriptions now produce FW_BUG warnings rather than preventing KVM initialisation, and the interrupt is always registered as level-sensitive.
* Reworked host IRS capability discovery to cache raw IRS_IDR2, IRS_IDR3, and IRS_IDR4 values and derive their fields through accessors. This avoids tying future EL2 code to the decoded layout of kvm_vgic_global_state.
* Hardened hardware-shared table handling for non-coherent IRS implementations. IRS operations and CPU table accesses now share a lock, VPET, VMD and VPED allocations are rounded up to whole cache lines, descriptors are initialised before publication, and VMT configuration waits for the IRS to become idle.
* Fixed sparse vCPU ID handling. The VPET and architectural VPE ID now use the userspace-provided vcpu_id, while doorbells and VPED storage use the dense internal vcpu_idx. IDs outside the available VPE ID space are rejected.
* Limited virtual GICv5 implementations to 1024 SPIs so that the physically contiguous per-SPI state allocation remains within KMALLOC_MAX_SIZE. The common IRQ routing table accommodates 1024 pins, while route validation retains the existing 988-pin limit for GICv2 and GICv3.
* Tightened LPI IST lifetime and migration rules. Guest Valid-to-Valid IRS_IST_BASER writes can no longer change the address, and once a host LPI IST exists, userspace may replay but not alter IRS_IST_BASER or IRS_IST_CFGR. LPI IST saves also require the VMTE ID width to match IRS_IST_CFGR. The IST migration descriptor has moved into the arm64-specific UAPI header.
* Hardened IRS and doorbell operation. GICv5 probing now fails without a host LPI domain, commands require a valid doorbell IRQ, running vCPUs are reloaded only on IRSEN transitions, and WFI doorbells are requested only while the virtual IRS is enabled.
* The resident, non-resident, and VDPEND nVHE handlers are explicit no-ops under pKVM. This matches the fix by Fuad for the already-upstreamed GICv5 hypercalls [8].
* Expanded the selftests to cover the 1024-SPI boundary, live IST register restrictions, sparse vCPU IDs, and rejection of vCPU IDs outside the GICv5 VPE ID space.
Thanks for taking the time to look at these changes!
Thanks,
Sascha
[1] https://lore.kernel.org/all/[email protected]/
[2] https://developer.arm.com/documentation/aes0070/latest
[3] https://developer.arm.com/documentation/108086/latest
[4] https://linaro.atlassian.net/wiki/x/CQAF-wY
[5] https://lore.kernel.org/all/[email protected]/
[6] https://gitlab.arm.com/linux-arm/linux-sb/-/tree/gicv5_kvm_irs_support_v5
[7] https://lore.kernel.org/all/[email protected]/
[8] https://lore.kernel.org/all/[email protected]/
Sascha Bischoff (49):
irqchip/gic-v5: Allow KVM setup without a maintenance IRQ irqchip/gic-v5: Provide OF IRS config frame attrs to KVM irqchip/gic-v5: Set up gic_kvm_info on ACPI hosts
KVM: arm64: gic-v5: Define remaining IRS MMIO registers arm64/sysreg: Add GICv5 GIC VDPEND encoding
arm64/sysreg: Update ICC_CR0_EL1 with LINK and LINK_IDLE fields
KVM: arm64: gic-v5: Cache host IRS ID registers
KVM: arm64: gic-v5: Add VPE doorbell domain
KVM: arm64: gic-v5: Create and manage VM and VPE tables
KVM: arm64: gic-v5: Introduce guest IST alloc and management
KVM: arm64: gic-v5: Implement VMT/vIST IRS MMIO Ops
KVM: arm64: gic-v5: Keep GICv5 vCPU limit model-specific
KVM: arm64: gic-v5: Implement VPE IRS MMIO Ops
KVM: arm64: gic-v5: Set up VMTEs and VPE doorbells
KVM: arm64: gic-v5: Add resident/non-resident hyp calls
KVM: arm64: gic-v5: Request doorbells when VPEs enter WFI
KVM: arm64: gic-v5: Introduce struct vgic_v5_irs and IRS base address
KVM: arm64: gic-v5: Add IRS IODEV support to MMIO handlers
KVM: arm64: gic-v5: Add KVM_VGIC_V5_ADDR_TYPE_IRS to UAPI
KVM: arm64: gic-v5: Add GICv5 IRS IODEV and MMIO emulation
KVM: arm64: gic-v5: Initialise per-VM IRS state
KVM: arm64: gic-v5: Register the IRS IODEV
KVM: arm64: gic-v5: Set IRICHPPIDIS based on IRS enable state
KVM: arm64: selftests: Update vGICv5 selftest to set IRS address
KVM: arm64: gic-v5: Add GIC VDPEND hyp call
KVM: arm64: gic: Introduce set_pending_state() to irq_ops
KVM: arm64: gic-v5: Support SPI injection
Documentation: KVM: Extend VGICv5 device attribute docs
KVM: arm64: gic-v5: Add GICv5 SPI injection to irqfd
KVM: arm64: gic-v5: Mask per-vCPU PPI state in
vgic_v5_finalize_ppi_state()
KVM: arm64: gic-v5: Add GICv5 EL1 sysreg userspace accessors
KVM: arm64: gic-v5: Handle userspace accesses to IRS MMIO region
KVM: arm64: gic-v5: Add CoreSight MMIO regs to IRS
KVM: arm64: gic-v5: Add VGICv5 IST save/restore UAPI
KVM: arm64: gic-v5: Implement save/restore mechanisms for ISTs
Documentation: KVM: Document KVM_DEV_ARM_VGIC_GRP_CPU_SYSREGS for
VGICv5
Documentation: KVM: Add KVM_DEV_ARM_VGIC_GRP_IRS_REGS to VGICv5 docs
Documentation: KVM: Add docs for KVM_DEV_ARM_VGIC_GRP_IST
Documentation: KVM: Add the VGICv5 IRS save/restore sequences
KVM: selftests: Add VGICv5 IRS address attribute tests
KVM: selftests: Add VGICv5 NR_IRQS attribute tests
KVM: selftests: Add VGICv5 IRS_REGS attribute tests
KVM: selftests: Add VGICv5 IST attribute tests
KVM: selftests: Add VGICv5 USERSPACE_PPIS tests
KVM: selftests: Add VGICv5 CPU sysreg attribute tests
KVM: selftests: Add VGICv5 SPI injection tests
KVM: selftests: Add VGICv5 LPI delivery tests
KVM: selftests: Add VGICv5 IST save/restore coverage
KVM: selftests: Add VGICv5 sparse vCPU IDs test
Documentation/virt/kvm/api.rst | 3 +- .../virt/kvm/devices/arm-vgic-v5.rst | 271 ++- arch/arm64/include/asm/kvm_asm.h | 3 + arch/arm64/include/asm/kvm_host.h | 1 + arch/arm64/include/asm/kvm_hyp.h | 3 + arch/arm64/include/asm/sysreg.h | 13 +- arch/arm64/include/uapi/asm/kvm.h | 15 + arch/arm64/kvm/Makefile | 3 +- arch/arm64/kvm/arm.c | 10 +- arch/arm64/kvm/hyp/nvhe/hyp-main.c | 35 + arch/arm64/kvm/hyp/vgic-v5-sr.c | 45 + arch/arm64/kvm/sys_regs.c | 6 +- arch/arm64/kvm/vgic-sys-reg-v5.c | 519 +++++ arch/arm64/kvm/vgic/vgic-init.c | 150 +- arch/arm64/kvm/vgic/vgic-irqfd.c | 18 +- arch/arm64/kvm/vgic/vgic-irs-v5.c | 1184 ++++++++++ arch/arm64/kvm/vgic/vgic-kvm-device.c | 270 ++- arch/arm64/kvm/vgic/vgic-mmio.c | 6 + arch/arm64/kvm/vgic/vgic-mmio.h | 2 + arch/arm64/kvm/vgic/vgic-v5-tables.c | 1860 ++++++++++++++++ arch/arm64/kvm/vgic/vgic-v5-tables.h | 140 ++ arch/arm64/kvm/vgic/vgic-v5.c | 1139 +++++++++- arch/arm64/kvm/vgic/vgic.c | 37 +- arch/arm64/kvm/vgic/vgic.h | 21 + arch/arm64/tools/sysreg | 4 +- drivers/irqchip/irq-gic-v5-irs.c | 19 +- drivers/irqchip/irq-gic-v5.c | 112 +- include/kvm/arm_vgic.h | 210 +- include/linux/irqchip/arm-gic-v5.h | 256 ++- include/linux/irqchip/arm-vgic-info.h | 5 + tools/arch/arm64/include/uapi/asm/kvm.h | 15 + tools/testing/selftests/kvm/arm64/vgic_v5.c | 1931 ++++++++++++++++- .../selftests/kvm/include/arm64/gic_v5.h | 105 + 33 files changed, 8208 insertions(+), 203 deletions(-) create mode 100644 arch/arm64/kvm/vgic-sys-reg-v5.c create mode 100644 arch/arm64/kvm/vgic/vgic-irs-v5.c create mode 100644 arch/arm64/kvm/vgic/vgic-v5-tables.c create mode 100644 arch/arm64/kvm/vgic/vgic-v5-tables.h
Signature · 1 line
2.34.1
PATCH v5 1/49 irqchip/gic-v5: Allow KVM setup without a maintenance IRQ
GICv5 does not require a virtual CPU interface maintenance interrupt for native GCIE operation. The interrupt is only needed when FEAT_GCIE_LEGACY is present, as the legacy GICv3 interface still relies on maintenance IRQ delivery.
Stop rejecting KVM setup solely because the maintenance interrupt is absent. Parse the interrupt if present, but if none is described and the system does not advertise FEAT_GCIE_LEGACY, tell KVM that no maintenance interrupt is required.
This lets native GICv5 KVM support be registered on systems that do not provide a maintenance interrupt, while requiring a maintenance interrupt for GICv3-capable systems.
--- drivers/irqchip/irq-gic-v5.c | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-)
drivers/irqchip/irq-gic-v5.c +12 -4
@@ -1143,10 +1143,18 @@ static void __init gic_of_setup_kvm_info(struct device_node *node)/* GIC Virtual CPU interface maintenance interrupt */ gic_v5_kvm_info.no_maint_irq_mask = false; gic_v5_kvm_info.maint_irq =irq_of_parse_and_map (node,0 ); -if (!gic_v5_kvm_info.maint_irq) { -pr_warn ("cannot find GICv5 virtual CPU interface maintenance interrupt\n" ); -return ; - } + +/* +* We require an MI if we have legacy support, but don't, otherwise. +* Given that there's an existing flag to convey that an MI isn't +* needed, we (ab)use it to tell KVM that the MI isn't needed if we +* don't support legacy. +* +* The check for ARM64_HAS_GICV5_LEGACY explicitly doesn't use +* cpus_have_final_cap() here as we run too early. +*/ +if (!cpus_have_cap (ARM64_HAS_GICV5_LEGACY ) && !gic_v5_kvm_info.maint_irq) + gic_v5_kvm_info.no_maint_irq_mask = true;vgic_set_kvm_info (&gic_v5_kvm_info); }
Signature · 1 line
2.34.1
PATCH v5 2/49 irqchip/gic-v5: Provide OF IRS config frame attrs to KVM
In reply to Sascha BischoffKVM needs to interact with the host IRS in order to, for example, make VMs or VPEs valid. There are two potential approaches here. Either the host irqchip driver can provide an interface, or KVM can interact directly with the host IRS. The latter of these two is chosen as the set of MMIO registers that KVM needs to interact with is orthogonal to the set used by the host irqchip driver (with the exception of some of the read-only IRS_IDRx registers).
Pass KVM a pointer to an IRS config frame - the config frame belonging to ANY IRS is fine as long as one IRS's config frame is used consistently - in struct gic_kvm_info. Additionally, include a flag telling KVM whether the IRS is coherent or non-coherent in order to make sure that KVM can do the correct cache state management, if required.
Only OF (Device Tree) is supported with this change. ACPI is not.
--- drivers/irqchip/irq-gic-v5-irs.c | 7 +++++-- drivers/irqchip/irq-gic-v5.c | 5 +++++ include/linux/irqchip/arm-gic-v5.h | 3 +++ include/linux/irqchip/arm-vgic-info.h | 5 +++++ 4 files changed, 18 insertions(+), 2 deletions(-)
drivers/irqchip/irq-gic-v5-irs.c +5 -2
@@ -21,8 +21,6 @@*/ #define LPI_ID_BITS_LINEAR 12 -#define IRS_FLAGS_NON_COHERENT BIT(0) - static DEFINE_PER_CPU_READ_MOSTLY(struct gicv5_irs_chip_data *, per_cpu_irs_data);static LIST_HEAD (irs_nodes);
@@ -50,6 +48,11 @@ static void irs_writeq_relaxed(struct gicv5_irs_chip_data *irs_data,writeq_relaxed (val, irs_data->irs_base + reg_offset); } +struct gicv5_irs_chip_data *gicv5_irs_get_chip_data (void ) +{ +return per_cpu (per_cpu_irs_data,0 ); +} +/* * The polling wait (in gicv5_wait_for_op_s_atomic()) on a GIC register * provides the memory barriers (through MMIO accessors)
drivers/irqchip/irq-gic-v5.c +5 -0
@@ -1128,6 +1128,8 @@ static struct gic_kvm_info gic_v5_kvm_info __initdata;static void __init gic_of_setup_kvm_info (struct device_node *node) { +struct gicv5_irs_chip_data *irs_data =gicv5_irs_get_chip_data (); +/* * If we don't have native GICv5 virtualisation support, then * we also don't have FEAT_GCIE_LEGACY - the architecture
@@ -1140,6 +1142,9 @@ static void __init gic_of_setup_kvm_info(struct device_node *node) gic_v5_kvm_info.type =GIC_V5 ; + gic_v5_kvm_info.gicv5_irs.base = irs_data->irs_base; + gic_v5_kvm_info.gicv5_irs.non_coherent = !!(irs_data->flags &IRS_FLAGS_NON_COHERENT ); +/* GIC Virtual CPU interface maintenance interrupt */ gic_v5_kvm_info.no_maint_irq_mask = false; gic_v5_kvm_info.maint_irq =irq_of_parse_and_map (node,0 );
include/linux/irqchip/arm-gic-v5.h +3 -0
@@ -330,6 +330,8 @@ struct gicv5_irs_chip_data {raw_spinlock_t spi_config_lock; }; +#define IRS_FLAGS_NON_COHERENT BIT(0) +static inline int gicv5_wait_for_op_s_atomic (void __iomem *addr,u32 offset,const char *reg_s,u32 mask,u32 *val)
@@ -377,6 +379,7 @@ void __init gicv5_free_lpi_domain(void);int gicv5_irs_of_probe (struct device_node *parent);int gicv5_irs_acpi_probe (void ); +struct gicv5_irs_chip_data *gicv5_irs_get_chip_data (void );void gicv5_irs_remove (void );int gicv5_irs_enable (void );void gicv5_irs_its_probe (void );
include/linux/irqchip/arm-vgic-info.h +5 -0
@@ -38,6 +38,11 @@ struct gic_kvm_info {bool has_v4_1;/* Deactivation impared, subpar stuff */ bool no_hw_deactivation; +/* GICv5 IRS base */ +struct { +void __iomem *base; +bool non_coherent; + } gicv5_irs; };#ifdef CONFIG_KVM
Signature · 1 line
2.34.1
[email protected]
AI reviewRe: [PATCH v5 02/49] irqchip/gic-v5: Provide OF IRS config frame attrs to KVM
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
New issues:
- [High] gic_of_setup_kvm_info() unconditionally dereferences irs_data, which may be NULL if CPU 0 is not connected to a GICv5 IRS.
Pre-existing issues:
- [High] Failure during IRS initialization leaves dangling pointers in per_cpu_irs_data, leading to a Use-After-Free. --
--- Patch [2]: [PATCH v5 02/49] irqchip/gic-v5: Provide OF IRS config frame attrs to KVM --- commit 1b935b47d4b6186ec42dafa24166af9a1d8adb3d
Author: Sascha Bischoff <[email protected]>
Subject: irqchip/gic-v5: Provide OF IRS config frame attrs to KVM
This commit passes a pointer to an IRS config frame to KVM via the gic_kvm_info structure. It uses the IRS config frame from CPU 0 as a representative frame, assuming all IRS config frames are consistent, and passes along coherency flags.
Quoted 4 lines
drivers/irqchip/irq-gic-v5-irs.c +0
-0
[ ... ]
drivers/irqchip/irq-gic-v5-irs.c static void irs_writeq_relaxed(struct gicv5_irs_chip_data *irs_data, · line 55
Quoted 9 lines
drivers/irqchip/irq-gic-v5-irs.c +5
-0@@ -50,6 +48,11 @@ static void irs_writeq_relaxed(struct gicv5_irs_chip_data *irs_data,writeq_relaxed (val, irs_data->irs_base + reg_offset); } +struct gicv5_irs_chip_data *gicv5_irs_get_chip_data (void ) +{ +return per_cpu (per_cpu_irs_data,0 ); +} +
[Severity: High]
This is a pre-existing issue, but if gicv5_irs_init() fails during IRS initialization, a dangling pointer may be left in per_cpu_irs_data:
drivers/irqchip/irq-gic-v5-irs.c:gicv5_irs_of_init() {
...
ret = gicv5_irs_of_init_affinity(node, irs_data, iaffid_bits);
...
ret = gicv5_irs_init(irs_data);
if (ret)
goto out_iomem;
...
out_err:
kfree(irs_data);
return ret;
}The error path frees irs_data without clearing the pointers previously populated in per_cpu_irs_data by gicv5_irs_of_init_affinity() (and the ACPI equivalent). Since IRS probing ignores failures of individual IRS nodes, a dangling pointer remains in the per-CPU array.
Will returning that dangling pointer here lead to a Use-After-Free when it is later accessed?
Quoted 10 lines
drivers/irqchip/irq-gic-v5.c +2
-0@@ -1128,6 +1128,8 @@ static struct gic_kvm_info gic_v5_kvm_info __initdata;static void __init gic_of_setup_kvm_info (struct device_node *node) { +struct gicv5_irs_chip_data *irs_data =gicv5_irs_get_chip_data (); +
[ ... ]
drivers/irqchip/irq-gic-v5.c static void __init gic_of_setup_kvm_info(struct device_node *node) · line 1147
Quoted 7 lines
drivers/irqchip/irq-gic-v5.c +3
-0@@ -1140,6 +1142,9 @@ static void __init gic_of_setup_kvm_info(struct device_node *node) gic_v5_kvm_info.type =GIC_V5 ; + gic_v5_kvm_info.gicv5_irs.base = irs_data->irs_base; + gic_v5_kvm_info.gicv5_irs.non_coherent = !!(irs_data->flags &IRS_FLAGS_NON_COHERENT ); +
[Severity: High]
If CPU 0 is not explicitly listed in the 'cpus' device tree property for any IRS (meaning CPU 0 lacks an IRS), gicv5_irs_get_chip_data() will return NULL.
Can this unconditionally dereference irs_data without a NULL check, potentially causing a panic during boot on valid topologies?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
Re: [PATCH v5 02/49] irqchip/gic-v5: Provide OF IRS config frame attrs to KVM
Quoted 69 lines · On Fri, 2026-08-07 at 11:53 +0000, [email protected] wrote:
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
New issues:
- [High]gic_of_setup_kvm_info()unconditionally dereferencesirs_data, which may be NULL if CPU 0 is not connected to a GICv5 IRS.Pre-existing issues:
- [High] Failure during IRS initialization leaves dangling pointers inper_cpu_irs_data, leading to a Use-After-Free.
----- Patch [2]: [PATCH v5 02/49] irqchip/gic-v5: Provide OF IRS config frame attrs to KVM ---
commit1b935b47d4b6186ec42dafa24166af9a1d8adb3d
Author: Sascha Bischoff <[email protected]>
Subject: irqchip/gic-v5: Provide OF IRS config frame attrs to KVMThis commit passes a pointer to an IRS config frame to KVM via the
gic_kvm_infostructure. It uses the IRS config frame from CPU 0 as a representative frame, assuming all IRS config frames are consistent,
and passes along coherency flags.Quoted 5 lines
drivers/irqchip/irq-gic-v5-irs.c +0
-0[ ... ]
drivers/irqchip/irq-gic-v5-irs.c static void irs_writeq_relaxed(struct gicv5_irs_chip_data *irs_data, · line 55
Quoted 10 lines
drivers/irqchip/irq-gic-v5-irs.c +0
-0@@ -50,6 +48,11 @@ static void irs_writeq_relaxed(structgicv5_irs_chip_data *irs_data, writeq_relaxed(val, irs_data->irs_base + reg_offset); }drivers/irqchip/irq-gic-v5-irs.c +5
-0+struct gicv5_irs_chip_data *gicv5_irs_get_chip_data (void ) +{ +return per_cpu (per_cpu_irs_data,0 ); +} +[Severity: High]
This is a pre-existing issue, but ifgicv5_irs_init()fails during IRS
initialization, a dangling pointer may be left inper_cpu_irs_data:drivers/irqchip/irq-gic-v5-irs.c:gicv5_irs_of_init() {
...
ret = gicv5_irs_of_init_affinity(node,irs_data,iaffid_bits);
...ret = gicv5_irs_init(irs_data); if (ret) goto out_iomem; ... out_err: kfree(irs_data); return ret; }The error path frees
irs_datawithout clearing the pointers previously populated
inper_cpu_irs_databygicv5_irs_of_init_affinity()(and the ACPI equivalent).
Since IRS probing ignores failures of individual IRS nodes, a dangling pointer
remains in the per-CPU array.
Quoted 3 lines
Will returning that dangling pointer here lead to a Use-After-Free when it is
later accessed?
The underlying issues has been addressed in the series here:https://lore.kernel.org/lkml/[email protected]/
With the changes there, the dangling pointers are reset to NULL, so this is no longer an issue.
Quoted 22 lines
Quoted 14 lines
drivers/irqchip/irq- renamed from drivers/irqchip/irq-gic-v5.c +0
-0
gic-v5.c
indexc04523a438cee..e7a7aedcfaf78100644drivers/irqchip/irq-gic-v5.c +0
-0@@ -1128,6 +1128,8 @@ static struct gic_kvm_info gic_v5_kvm_info__initdata ;
static void__initgic_of_setup_kvm_info(struct device_node *node){drivers/irqchip/irq-gic-v5.c +1
-0+ struct gicv5_irs_chip_data *irs_data =
gicv5_irs_get_chip_data();
+[ ... ]
Quoted 7 lines
drivers/irqchip/irq-gic-v5.c +0
-0@@ -1140,6 +1142,9 @@ static void __init gic_of_setup_kvm_info(struct device_node *node)
gic_v5_kvm_info.type =GIC_V5;drivers/irqchip/irq-gic-v5.c +2
-0+ gic_v5_kvm_info.gicv5_irs.base = irs_data->irs_base; + gic_v5_kvm_info.gicv5_irs.non_coherent = !!(irs_data-
Quoted 8 lines
Quoted 2 lines
Quoted 1 line
flags &
IRS_FLAGS_NON_COHERENT);+
[Severity: High]
If CPU 0 is not explicitly listed in the 'cpus' device tree property for any
IRS (meaning CPU 0 lacks an IRS),gicv5_irs_get_chip_data()will return NULL.
gicv5_of_init() calls gicv5_init_common() before gic_of_setup_kvm_info(). The common initialisation calls gicv5_starting_cpu() for the boot CPU, which reaches gicv5_irs_register_cpu(). If CPU0 has no associated IRS/IAFFID, that returns an error and GICv5 initialisation unwinds. Hence, gic_of_setup_kvm_info() is never reached with a NULL CPU0 IRS pointer.
In either case, a topology without a working IRS for the boot CPU cannot support IPIs with GICv5, so it is not a usable topology.
Quoted 5 lines
Can this unconditionally dereference
irs_datawithout a NULL check, potentially
causing a panic during boot on valid topologies?
In short, no.
Thanks,
Sascha
PATCH v5 3/49 irqchip/gic-v5: Set up gic_kvm_info on ACPI hosts
In reply to Sascha BischoffDevice-tree based GICv5 probing already passes the IRS details and maintenance interrupt to KVM, but the ACPI path only initialises the irqchip and installs the ACPI IRQ model. As a result, KVM never sees the GICv5 host information required to probe the vGIC on ACPI systems.
Add the ACPI equivalent of the DT KVM setup. Parse the MADT GICC entries for the maintenance interrupt, retaining the value from the first relevant entry and warning if later entries disagree. Warn if firmware describes the interrupt as edge-triggered, but register it as a level-sensitive, active-high GSI. Pass the resulting IRQ together with the IRS base and coherency information to KVM.
Native GICv5 does not require a maintenance interrupt unless the legacy GICv3-compatible CPU interface is present, so preserve the existing no-maintenance-IRQ handling for that case.
--- drivers/irqchip/irq-gic-v5.c | 95 ++++++++++++++++++++++++++++++++++-- 1 file changed, 91 insertions(+), 4 deletions(-)
drivers/irqchip/irq-gic-v5.c +91 -4
@@ -1126,7 +1126,7 @@ static void gicv5_set_cpuif_idbits(void)#ifdef CONFIG_KVM static struct gic_kvm_info gic_v5_kvm_info__initdata ; -static void __init gic_of_setup_kvm_info (struct device_node *node) +static void __init gic_setup_kvm_info (unsigned int maint_irq) {struct gicv5_irs_chip_data *irs_data =gicv5_irs_get_chip_data ();
@@ -1137,17 +1137,19 @@ static void __init gic_of_setup_kvm_info(struct device_node *node)*/ if (!gicv5_global_data.virt_capable) {pr_info ("GIC implementation is not virtualization capable\n" ); -return ; + goto out_dispose_maint_irq; } - gic_v5_kvm_info.type =GIC_V5 ; +if (WARN_ON (!irs_data)) + goto out_dispose_maint_irq; + gic_v5_kvm_info.type =GIC_V5 ; gic_v5_kvm_info.gicv5_irs.base = irs_data->irs_base; gic_v5_kvm_info.gicv5_irs.non_coherent = !!(irs_data->flags &IRS_FLAGS_NON_COHERENT );/* GIC Virtual CPU interface maintenance interrupt */ gic_v5_kvm_info.no_maint_irq_mask = false; - gic_v5_kvm_info.maint_irq =irq_of_parse_and_map (node,0 ); + gic_v5_kvm_info.maint_irq = maint_irq;/* * We require an MI if we have legacy support, but don't, otherwise.
@@ -1162,11 +1164,95 @@ static void __init gic_of_setup_kvm_info(struct device_node *node) gic_v5_kvm_info.no_maint_irq_mask = true;vgic_set_kvm_info (&gic_v5_kvm_info); +return ; + +out_dispose_maint_irq: +irq_dispose_mapping (maint_irq); +} + +static void __init gic_of_setup_kvm_info (struct device_node *node) +{ +/* GIC Virtual CPU interface maintenance interrupt */ +gic_setup_kvm_info (irq_of_parse_and_map (node,0 )); +} + +#ifdef CONFIG_ACPI +struct gicv5_acpi_kvm_info { +u32 maint_irq; +}; + +static struct gicv5_acpi_kvm_info acpi_v5_kvm_info__initdata ; + +static int __init gic_acpi_parse_virt_madt_gicc (union acpi_subtable_headers *header, +const unsigned long end) +{ +struct acpi_madt_generic_interrupt *gicc = + (struct acpi_madt_generic_interrupt *)header; +static int first_madt = true; + +if (!(gicc->flags & + (ACPI_MADT_ENABLED |ACPI_MADT_GICC_ONLINE_CAPABLE ))) +return 0 ; + +if (gicc->flags &ACPI_MADT_VGIC_IRQ_MODE ) +pr_warn_once (FW_BUG "MI wrongly advertised as Edge-triggered\n" ); + +if (first_madt) { + first_madt = false; + + acpi_v5_kvm_info.maint_irq = gicc->vgic_interrupt; +return 0 ; + } + +if (acpi_v5_kvm_info.maint_irq != gicc->vgic_interrupt) +pr_warn_once (FW_BUG "MI not the same on all CPUs\n" ); + +return 0 ; +} + +static bool __init gic_acpi_collect_virt_info (void ) +{ +int count; + + count =acpi_table_parse_madt (ACPI_MADT_TYPE_GENERIC_INTERRUPT , + gic_acpi_parse_virt_madt_gicc,0 ); + +return count >0 ; } + +static void __init gic_acpi_setup_kvm_info (void ) +{ +unsigned int maint_irq =0 ; +int irq; + +if (!gic_acpi_collect_virt_info ()) { +pr_warn ("Unable to get hardware information used for virtualization\n" ); +return ; + } + +if (acpi_v5_kvm_info.maint_irq) { + irq =acpi_register_gsi (NULL , acpi_v5_kvm_info.maint_irq, +ACPI_LEVEL_SENSITIVE , +ACPI_ACTIVE_HIGH ); +if (irq >0 ) + maint_irq = irq; +else +pr_warn ("Failed to register GSI for GICv5 maintenance IRQ\n" ); + } + +gic_setup_kvm_info (maint_irq); +} +#endif // CONFIG_ACPI #else static inline void __init gic_of_setup_kvm_info (struct device_node *node) { } + +#ifdef CONFIG_ACPI +static inline void __init gic_acpi_setup_kvm_info (void ) +{ +} +#endif #endif // CONFIG_KVM static int __init gicv5_init_common (struct fwnode_handle *parent_domain)
@@ -1265,6 +1351,7 @@ static int __init gic_acpi_init(union acpi_subtable_headers *header, const unsig goto out_irs;acpi_set_irq_model (ACPI_IRQ_MODEL_GIC_V5 , gic_v5_get_gsi_domain_id); +gic_acpi_setup_kvm_info ();return 0 ;
Signature · 1 line
2.34.1
[email protected]
AI reviewRe: [PATCH v5 03/49] irqchip/gic-v5: Set up gic_kvm_info on ACPI hosts
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] Missing bounds check in gic_acpi_parse_virt_madt_gicc() when parsing ACPI MADT GICC subtables…
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] Missing bounds check in gic_acpi_parse_virt_madt_gicc() when parsing ACPI MADT GICC subtables, leading to out-of-bounds read on malformed tables. --
--- Patch [3]: [PATCH v5 03/49] irqchip/gic-v5: Set up gic_kvm_info on ACPI hosts --- commit 7147e5b6fec39dcd71e533b7ddd5252d309103a5
Author: Sascha Bischoff <[email protected]>
irqchip/gic-v5: Set up gic_kvm_info on ACPI hosts
This commit adds the ACPI equivalent of the DT KVM setup for GICv5. It parses the MADT GICC entries to retrieve and register the maintenance interrupt, and passes the resulting IRQ along with IRS base and coherency information to KVM.
Quoted 4 lines
drivers/irqchip/irq-gic-v5.c +0
-0
[ ... ]
Quoted 1 line
drivers/irqchip/irq-gic-v5.c +0
-0@@ -1162,11 +1164,95 @@ static void __init gic_of_setup_kvm_info(struct device_node *node)
[ ... ]
drivers/irqchip/irq-gic-v5.c static void __init gic_of_setup_kvm_info(struct device_node *node) · line 1193
Quoted 8 lines
drivers/irqchip/irq-gic-v5.c +8
-0+static int __init gic_acpi_parse_virt_madt_gicc (union acpi_subtable_headers *header, +const unsigned long end) +{ +struct acpi_madt_generic_interrupt *gicc = + (struct acpi_madt_generic_interrupt *)header; +static int first_madt = true; + + if (!(gicc->flags &
[Severity: High]
Could this read of gicc->flags lead to an out-of-bounds memory access?
If the firmware provides a malformed ACPI MADT table with an entry length smaller than the size of struct acpi_madt_generic_interrupt, reading gicc->flags or gicc->vgic_interrupt might cross a page boundary and cause a kernel panic during early boot.
Would it make sense to validate the entry size against the end pointer here, perhaps by using a macro like BAD_MADT_GICC_ENTRY(gicc, end), before accessing any of the structure's fields?
Quoted 2 lines
drivers/irqchip/irq-gic-v5.c +2
-0+ (ACPI_MADT_ENABLED | ACPI_MADT_GICC_ONLINE_CAPABLE))) +return 0 ;
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
Re: [PATCH v5 03/49] irqchip/gic-v5: Set up gic_kvm_info on ACPI hosts
Quoted 52 lines · On Fri, 2026-08-07 at 12:01 +0000, [email protected] wrote:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] Missing bounds check ingic_acpi_parse_virt_madt_gicc()when parsing ACPI MADT GICC subtables, leading to out-of-bounds read on malformed tables.
----- Patch [3]: [PATCH v5 03/49] irqchip/gic-v5: Set up
gic_kvm_infoon ACPI hosts ---
commit7147e5b6fec39dcd71e533b7ddd5252d309103a5
Author: Sascha Bischoff <[email protected]>irqchip/gic-v5: Set up
gic_kvm_infoon ACPI hostsThis commit adds the ACPI equivalent of the DT KVM setup for GICv5. It parses
the MADT GICC entries to retrieve and register the maintenance interrupt, and
passes the resulting IRQ along with IRS base and coherency information to KVM.Quoted 5 lines
drivers/irqchip/irq- renamed from drivers/irqchip/irq-gic-v5.c +0
-0
gic-v5.c
indexe7a7aedcfaf78..5b8ea8551d832100644drivers/irqchip/irq-gic-v5.c +0
-0[ ... ]
Quoted 2 lines
drivers/irqchip/irq-gic-v5.c +0
-0@@ -1162,11 +1164,95 @@ static void __init gic_of_setup_kvm_info(struct device_node *node)[ ... ]
drivers/irqchip/irq-gic-v5.c static void __init gic_of_setup_kvm_info(struct device_node *node) · line 1193
Quoted 10 lines
+static int
__initgic_acpi_parse_virt_madt_gicc(unionacpi_subtable_headers*header,drivers/irqchip/irq-gic-v5.c +1
-0+ const unsignedlong end)
drivers/irqchip/irq-gic-v5.c +6
-0+{ +struct acpi_madt_generic_interrupt *gicc = + (struct acpi_madt_generic_interrupt *)header; +static int first_madt = true; + + if (!(gicc->flags &[Severity: High]
Could this read ofgicc->flagslead to an out-of-bounds memory access?If the firmware provides a malformed ACPI MADT table with an entry length
smaller than the size of structacpi_madt_generic_interrupt, readinggicc->flagsorgicc->vgic_interruptmight cross a page boundary and cause a
kernel panic during early boot.
Quoted 5 lines
Would it make sense to validate the entry size against the end pointer here,
perhaps by using a macro likeBAD_MADT_GICC_ENTRY(gicc, end), before accessing
any of the structure's fields?
This has been addressed by Lorenzo in his series here:
https://lore.kernel.org/all/[email protected]/
Quoted 5 lines
drivers/irqchip/irq-gic-v5.c
Quoted 4 lines
drivers/irqchip/irq-gic-v5.c +1
-0+ (ACPI_MADT_ENABLED |
ACPI_MADT_GICC_ONLINE_CAPABLE)))drivers/irqchip/irq-gic-v5.c +1
-0+return 0 ;
Thanks,
Sascha
Lorenzo Pieralisi
maintainerRe: [PATCH v5 03/49] irqchip/gic-v5: Set up gic_kvm_info on ACPI hosts
In reply to Sascha BischoffQuoted 12 lines · Sascha Bischoff
Device-tree based GICv5 probing already passes the IRS details and maintenance interrupt to KVM, but the ACPI path only initialises the irqchip and installs the ACPI IRQ model. As a result, KVM never sees the GICv5 host information required to probe the vGIC on ACPI systems.
Add the ACPI equivalent of the DT KVM setup. Parse the MADT GICC entries for the maintenance interrupt, retaining the value from the first relevant entry and warning if later entries disagree. Warn if firmware describes the interrupt as edge-triggered, but register it as a level-sensitive, active-high GSI. Pass the resulting IRQ together with the IRS base and coherency information to KVM.
Quoted 8 lines
Native GICv5 does not require a maintenance interrupt unless the legacy GICv3-compatible CPU interface is present, so preserve the existing no-maintenance-IRQ handling for that case.
Signed-off-by Sascha Bischoff <[email protected]>--- drivers/irqchip/irq-gic-v5.c | 95 ++++++++++++++++++++++++++++++++++-- 1 file changed, 91 insertions(+), 4 deletions(-)
Just a bunch of nits below.
Quoted 14 lines
drivers/irqchip/irq-gic-v5.c +1
-1@@ -1126,7 +1126,7 @@ static void gicv5_set_cpuif_idbits(void)#ifdef CONFIG_KVM static struct gic_kvm_info gic_v5_kvm_info__initdata ; -static void __init gic_of_setup_kvm_info (struct device_node *node) +static void __init gic_setup_kvm_info (unsigned int maint_irq) {struct gicv5_irs_chip_data *irs_data =gicv5_irs_get_chip_data ();
drivers/irqchip/irq-gic-v5.c static void __init gic_of_setup_kvm_info(struct device_node *node) · line 1140
Quoted 5 lines
drivers/irqchip/irq-gic-v5.c +1
-1@@ -1137,17 +1137,19 @@ static void __init gic_of_setup_kvm_info(struct device_node *node)*/ if (!gicv5_global_data.virt_capable) {pr_info ("GIC implementation is not virtualization capable\n" ); -return ; + goto out_dispose_maint_irq;
Isn't it cleaner to dispose the mapping in the caller (by returning an error value) rather than here ?
I appreciate this keeps the IRQ mapping disposal in one place but it decouples the allocation from the failure path.
Quoted 40 lines
drivers/irqchip/irq-gic-v5.c +23
-2} - gic_v5_kvm_info.type =GIC_V5 ; +if (WARN_ON (!irs_data)) + goto out_dispose_maint_irq; + gic_v5_kvm_info.type =GIC_V5 ; gic_v5_kvm_info.gicv5_irs.base = irs_data->irs_base; gic_v5_kvm_info.gicv5_irs.non_coherent = !!(irs_data->flags &IRS_FLAGS_NON_COHERENT );/* GIC Virtual CPU interface maintenance interrupt */ gic_v5_kvm_info.no_maint_irq_mask = false; - gic_v5_kvm_info.maint_irq =irq_of_parse_and_map (node,0 ); + gic_v5_kvm_info.maint_irq = maint_irq;/* * We require an MI if we have legacy support, but don't, otherwise. @@ -1162,11 +1164,95 @@ static void __init gic_of_setup_kvm_info(struct device_node *node) gic_v5_kvm_info.no_maint_irq_mask = true;vgic_set_kvm_info (&gic_v5_kvm_info); +return ; + +out_dispose_maint_irq: +irq_dispose_mapping (maint_irq); +} + +static void __init gic_of_setup_kvm_info (struct device_node *node) +{ +/* GIC Virtual CPU interface maintenance interrupt */ +gic_setup_kvm_info (irq_of_parse_and_map (node,0 )); +} + +#ifdef CONFIG_ACPI +struct gicv5_acpi_kvm_info { +u32 maint_irq; +}; + +static struct gicv5_acpi_kvm_info acpi_v5_kvm_info__initdata ; +
drivers/irqchip/irq-gic-v5.c static void __init gic_of_setup_kvm_info(struct device_node *node) · line 1195
Quoted 10 lines
drivers/irqchip/irq-gic-v5.c +10
-0+static int __init gic_acpi_parse_virt_madt_gicc (union acpi_subtable_headers *header, +const unsigned long end) +{ +struct acpi_madt_generic_interrupt *gicc = + (struct acpi_madt_generic_interrupt *)header; +static int first_madt = true; + +if (!(gicc->flags & + (ACPI_MADT_ENABLED |ACPI_MADT_GICC_ONLINE_CAPABLE ))) +return 0 ;
This would count as a valid parsed entry even though you don't retrieve the maintanance interrupt number, more below.
Quoted 26 lines
drivers/irqchip/irq-gic-v5.c +25
-0+ +if (gicc->flags &ACPI_MADT_VGIC_IRQ_MODE ) +pr_warn_once (FW_BUG "MI wrongly advertised as Edge-triggered\n" ); + +if (first_madt) { + first_madt = false; + + acpi_v5_kvm_info.maint_irq = gicc->vgic_interrupt; +return 0 ; + } + +if (acpi_v5_kvm_info.maint_irq != gicc->vgic_interrupt) +pr_warn_once (FW_BUG "MI not the same on all CPUs\n" ); + +return 0 ; +} + +static bool __init gic_acpi_collect_virt_info (void ) +{ +int count; + + count =acpi_table_parse_madt (ACPI_MADT_TYPE_GENERIC_INTERRUPT , + gic_acpi_parse_virt_madt_gicc,0 ); + +return count >0 ; }
drivers/irqchip/irq-gic-v5.c static void __init gic_of_setup_kvm_info(struct device_node *node) · line 1231
Quoted 10 lines
drivers/irqchip/irq-gic-v5.c +10
-0+ +static void __init gic_acpi_setup_kvm_info (void ) +{ +unsigned int maint_irq =0 ; +int irq; + +if (!gic_acpi_collect_virt_info ()) { +pr_warn ("Unable to get hardware information used for virtualization\n" ); +return ; + }
Nit: gic_acpi_collect_virt_info() might return true and you still don't get the info you need. Now, at least one GICC entry should be enabled and online capable (we are booting on a core after all but never overestimate ACPI tables :)) so this is theory but I thought I would mention it.
drivers/irqchip/irq-gic-v5.c static void __init gic_of_setup_kvm_info(struct device_node *node) · line 1240
Quoted 9 lines
drivers/irqchip/irq-gic-v5.c +9
-0+ +if (acpi_v5_kvm_info.maint_irq) { + irq =acpi_register_gsi (NULL , acpi_v5_kvm_info.maint_irq, +ACPI_LEVEL_SENSITIVE , +ACPI_ACTIVE_HIGH ); +if (irq >0 ) + maint_irq = irq; +else +pr_warn ("Failed to register GSI for GICv5 maintenance IRQ\n" );
Another nit: do we really need the maint_irq variable ?
Thanks,
Lorenzo
Quoted 28 lines
drivers/irqchip/irq-gic-v5.c +12
-0+ } + +gic_setup_kvm_info (maint_irq); +} +#endif // CONFIG_ACPI #else static inline void __init gic_of_setup_kvm_info (struct device_node *node) { } + +#ifdef CONFIG_ACPI +static inline void __init gic_acpi_setup_kvm_info (void ) +{ +} +#endif #endif // CONFIG_KVM static int __init gicv5_init_common (struct fwnode_handle *parent_domain)@@ -1265,6 +1351,7 @@ static int __init gic_acpi_init(union acpi_subtable_headers *header, const unsig goto out_irs;acpi_set_irq_model (ACPI_IRQ_MODEL_GIC_V5 , gic_v5_get_gsi_domain_id); +gic_acpi_setup_kvm_info ();return 0 ;Signature · 1 line
2.34.1
Re: [PATCH v5 03/49] irqchip/gic-v5: Set up gic_kvm_info on ACPI hosts
Quoted 57 lines · Lorenzo Pieralisi
Quoted 20 lines · Sascha Bischoff
Device-tree based GICv5 probing already passes the IRS details and maintenance interrupt to KVM, but the ACPI path only initialises the
irqchip and installs the ACPI IRQ model. As a result, KVM never sees
the GICv5 host information required to probe the vGIC on ACPI systems.Add the ACPI equivalent of the DT KVM setup. Parse the MADT GICC entries for the maintenance interrupt, retaining the value from the first relevant entry and warning if later entries disagree. Warn if firmware describes the interrupt as edge-triggered, but register it as
a level-sensitive, active-high GSI. Pass the resulting IRQ together with the IRS base and coherency information to KVM.Native GICv5 does not require a maintenance interrupt unless the legacy GICv3-compatible CPU interface is present, so preserve the existing no-maintenance-IRQ handling for that case.
Quoted 5 lines
Signed-off-by Sascha Bischoff <[email protected]>---
drivers/irqchip/irq-gic-v5.c | 95
++++++++++++++++++++++++++++++++++--1 file changed, 91 insertions(+), 4 deletions(-)Just a bunch of nits below.
Quoted 21 lines
drivers/irqchip/irq- renamed from drivers/irqchip/irq-gic-v5.c +0
-0
gic-v5.c
indexe7a7aedcfaf78..5b8ea8551d832100644drivers/irqchip/irq-gic-v5.c +0
-0@@ -1126,7 +1126,7 @@ static void gicv5_set_cpuif_idbits(void) #ifdef CONFIG_KVM static struct gic_kvm_info gic_v5_kvm_info __initdata;drivers/irqchip/irq-gic-v5.c +1
-1-static void __init gic_of_setup_kvm_info (struct device_node *node) +static void __init gic_setup_kvm_info (unsigned int maint_irq){
structgicv5_irs_chip_data*irs_data=gicv5_irs_get_chip_data();drivers/irqchip/irq-gic-v5.c +0
-0@@ -1137,17 +1137,19 @@ static void __initgic_of_setup_kvm_info(struct device_node *node) */ if (!gicv5_global_data.virt_capable) {
pr_info("GIC implementation is not virtualization capable\n");drivers/irqchip/irq-gic-v5.c
Quoted 2 lines
drivers/irqchip/irq-gic-v5.c +1
-1-return ; + goto out_dispose_maint_irq;Isn't it cleaner to dispose the mapping in the caller (by returning an
error value) rather than here ?
Quoted 2 lines
I appreciate this keeps the IRQ mapping disposal in one place but it decouples the allocation from the failure path.
That's a good point. I've moved the disposal to the callers.
Quoted 104 lines
Quoted 56 lines
}
drivers/irqchip/irq-gic-v5.c +2
-1- gic_v5_kvm_info.type =GIC_V5 ; +if (WARN_ON (!irs_data)) + goto out_dispose_maint_irq;drivers/irqchip/irq-gic-v5.c +1
-0+ gic_v5_kvm_info.type =GIC_V5 ;gic_v5_kvm_info.gicv5_irs.base =
irs_data->irs_base; gic_v5_kvm_info.gicv5_irs.non_coherent = !!(irs_data-Quoted 1 line
flags &
IRS_FLAGS_NON_COHERENT);
/* GIC Virtual CPU interface maintenance interrupt */ gic_v5_kvm_info.no_maint_irq_mask = false;drivers/irqchip/irq-gic-v5.c +1
-1- gic_v5_kvm_info.maint_irq =irq_of_parse_and_map (node,0 ); + gic_v5_kvm_info.maint_irq = maint_irq;
/*
* We require an MI if we have legacy support, but don't, otherwise.drivers/irqchip/irq-gic-v5.c +0
-0@@ -1162,11 +1164,95 @@ static void __init gic_of_setup_kvm_info(struct device_node *node) gic_v5_kvm_info.no_maint_irq_mask = true;
vgic_set_kvm_info(&gic_v5_kvm_info);drivers/irqchip/irq-gic-v5.c +20
-0+return ; + +out_dispose_maint_irq: +irq_dispose_mapping (maint_irq); +} + +static void __init gic_of_setup_kvm_info (struct device_node *node) +{ +/* GIC Virtual CPU interface maintenance interrupt */ +gic_setup_kvm_info (irq_of_parse_and_map (node,0 )); +} + +#ifdef CONFIG_ACPI +struct gicv5_acpi_kvm_info { +u32 maint_irq; +}; + +static struct gicv5_acpi_kvm_info acpi_v5_kvm_info__initdata ; + +static int __init gic_acpi_parse_virt_madt_gicc(union
acpi_subtable_headers*header,drivers/irqchip/irq-gic-v5.c +1
-0+ const unsignedlong end)
drivers/irqchip/irq-gic-v5.c +7
-0+{ +struct acpi_madt_generic_interrupt *gicc = + (struct acpi_madt_generic_interrupt *)header; +static int first_madt = true; + + if (!(gicc->flags & + (ACPI_MADT_ENABLED |
ACPI_MADT_GICC_ONLINE_CAPABLE)))drivers/irqchip/irq-gic-v5.c
Quoted 1 line
drivers/irqchip/irq-gic-v5.c +1
-0+return 0 ;This would count as a valid parsed entry even though you don't retrieve the
maintanance interrupt number, more below.Quoted 39 lines
drivers/irqchip/irq-gic-v5.c +3
-0+ +if (gicc->flags &ACPI_MADT_VGIC_IRQ_MODE ) + pr_warn_once(FW_BUG "MI wrongly advertised asEdge-triggered\n");
drivers/irqchip/irq-gic-v5.c +10
-0+ +if (first_madt) { + first_madt = false; + + acpi_v5_kvm_info.maint_irq = gicc->vgic_interrupt; +return 0 ; + } + +if (acpi_v5_kvm_info.maint_irq != gicc->vgic_interrupt) + pr_warn_once(FW_BUG "MI not the same on allCPUs\n");
drivers/irqchip/irq-gic-v5.c +9
-0+ +return 0 ; +} + +static bool __init gic_acpi_collect_virt_info (void ) +{ +int count; + + count =acpi_table_parse_madt(ACPI_MADT_TYPE_GENERIC_INTERRUPT,
drivers/irqchip/irq-gic-v5.c +1
-0+
gic_acpi_parse_virt_madt_gicc, 0);drivers/irqchip/irq-gic-v5.c +2
-0+ +return count >0 ;}
drivers/irqchip/irq-gic-v5.c +8
-0+ +static void __init gic_acpi_setup_kvm_info (void ) +{ +unsigned int maint_irq =0 ; +int irq; + +if (!gic_acpi_collect_virt_info ()) { + pr_warn("Unable to get hardware information usedfor virtualization\n");
drivers/irqchip/irq-gic-v5.c
Quoted 2 lines
drivers/irqchip/irq-gic-v5.c +2
-0+return ; + }
Quoted 7 lines
Nit:
gic_acpi_collect_virt_info()might return true and you still don't get
the info you need. Now, at least one GICC entry should be enabled and online
capable (we are booting on a core after all but never overestimate ACPI tables :))
so this is theory but I thought I would mention it.
I've added an explicit valid flag, which is set true when an enabled (or online_capable) GICC entry is actually processed. I've made gic_acpi_collect_virt_info() return that flag directly so now a disabled entry can't falsely return success.
Quoted 14 lines
Quoted 11 lines
drivers/irqchip/irq-gic-v5.c +3
-0+ +if (acpi_v5_kvm_info.maint_irq) { + irq = acpi_register_gsi(NULL,acpi_v5_kvm_info.maint_irq,
drivers/irqchip/irq-gic-v5.c +6
-0+ACPI_LEVEL_SENSITIVE , + ACPI_ACTIVE_HIGH); +if (irq >0 ) + maint_irq = irq; +else + pr_warn("Failed to register GSI for GICv5Quoted 1 line
maintenance IRQ\n");
Another nit: do we really need the
maint_irqvariable ?
No, I've removed it.
Quoted 3 lines
Thanks,
Lorenzo
Thanks,
Sascha
Quoted 33 lines
Quoted 33 lines
drivers/irqchip/irq-gic-v5.c +5
-0+ } + +gic_setup_kvm_info (maint_irq); +} +#endif // CONFIG_ACPI #else
static inline void__initgic_of_setup_kvm_info(struct device_node *node)
{
}drivers/irqchip/irq-gic-v5.c +6
-0+ +#ifdef CONFIG_ACPI +static inline void __init gic_acpi_setup_kvm_info (void ) +{ +} +#endif #endif //
CONFIG_KVM
static int__initgicv5_init_common(struct fwnode_handle *parent_domain)drivers/irqchip/irq-gic-v5.c +0
-0@@ -1265,6 +1351,7 @@ static int __init gic_acpi_init(union
acpi_subtable_headers*header, const unsig
gotoout_irs;
acpi_set_irq_model(ACPI_IRQ_MODEL_GIC_V5,gic_v5_get_gsi_domain_id);drivers/irqchip/irq-gic-v5.c +1
-0+gic_acpi_setup_kvm_info ();
return 0;Signature · 1 line
2.34.1
PATCH v5 4/49 KVM: arm64: gic-v5: Define remaining IRS MMIO registers
In reply to Sascha BischoffComplete the set of defined IRS MMIO registers in the GICv5 header file. Up until now, the set of defined IRS MMIO registers has been driven by code requirements. However, in order to properly emulate the IRS MMIO interface in KVM, the full set of IRS MMIO registers needs to be added.
--- include/linux/irqchip/arm-gic-v5.h | 239 +++++++++++++++++++++++++++-- 1 file changed, 230 insertions(+), 9 deletions(-)
include/linux/irqchip/arm-gic-v5.h +230 -9
@@ -62,20 +62,34 @@#define GICV5_OUTER_SHARE 0b10#define GICV5_INNER_SHARE 0b11 +#define GICV5_AIDR_COMPONENT_IRS 0b00 +#define GICV5_AIDR_COMPONENT_ITS 0b01 +#define GICV5_AIDR_COMPONENT_IWB 0b10 + +#define GICV5_AIDR_ARCH_MAJ_REV_V5 0 +#define GICV5_AIDR_ARCH_MIN_REV_V0 0 +/* * IRS registers and tables structures */ #define GICV5_IRS_IDR0 0x0000#define GICV5_IRS_IDR1 0x0004#define GICV5_IRS_IDR2 0x0008 +#define GICV5_IRS_IDR3 0x000c +#define GICV5_IRS_IDR4 0x0010#define GICV5_IRS_IDR5 0x0014#define GICV5_IRS_IDR6 0x0018#define GICV5_IRS_IDR7 0x001c +#define GICV5_IRS_IIDR 0x0040 +#define GICV5_IRS_AIDR 0x0044#define GICV5_IRS_CR0 0x0080#define GICV5_IRS_CR1 0x0084#define GICV5_IRS_SYNCR 0x00c0#define GICV5_IRS_SYNC_STATUSR 0x00c4 +#define GICV5_IRS_SPI_VMR 0x0100#define GICV5_IRS_SPI_SELR 0x0108 +#define GICV5_IRS_SPI_DOMAINR 0x010c +#define GICV5_IRS_SPI_RESAMPLER 0x0110#define GICV5_IRS_SPI_CFGR 0x0114#define GICV5_IRS_SPI_STATUSR 0x0118#define GICV5_IRS_PE_SELR 0x0140
@@ -85,11 +99,51 @@#define GICV5_IRS_IST_CFGR 0x0190#define GICV5_IRS_IST_STATUSR 0x0194#define GICV5_IRS_MAP_L2_ISTR 0x01c0 - +#define GICV5_IRS_VMT_BASER 0x0200 +#define GICV5_IRS_VMT_CFGR 0x0210 +#define GICV5_IRS_VMT_STATUSR 0x0214 +#define GICV5_IRS_VPE_SELR 0x0240 +#define GICV5_IRS_VPE_DBR 0x0248 +#define GICV5_IRS_VPE_HPPIR 0x0250 +#define GICV5_IRS_VPE_CR0 0x0258 +#define GICV5_IRS_VPE_STATUSR 0x025c +#define GICV5_IRS_VM_DBR 0x0280 +#define GICV5_IRS_VM_SELR 0x0288 +#define GICV5_IRS_VM_STATUSR 0x028c +#define GICV5_IRS_VMAP_L2_VMTR 0x02c0 +#define GICV5_IRS_VMAP_VMR 0x02c8 +#define GICV5_IRS_VMAP_VISTR 0x02d0 +#define GICV5_IRS_VMAP_L2_VISTR 0x02d8 +#define GICV5_IRS_VMAP_VPER 0x02e0 +#define GICV5_IRS_SAVE_VMR 0x0300 +#define GICV5_IRS_SAVE_VM_STATUSR 0x0308 +#define GICV5_IRS_MEC_IDR 0x0340 +#define GICV5_IRS_MEC_MECID_R 0x0344 +#define GICV5_IRS_MPAM_IDR 0x0380 +#define GICV5_IRS_MPAM_PARTID_R 0x0384 +#define GICV5_IRS_SWERR_STATUSR 0x03c0 +#define GICV5_IRS_SWERR_SYNDROMER0 0x03c8 +#define GICV5_IRS_SWERR_SYNDROMER1 0x03d0 + +#define GICV5_IRS_IDR0_IRSID GENMASK(31, 16) +#define GICV5_IRS_IDR0_SWE BIT(12) +#define GICV5_IRS_IDR0_MPAM BIT(11) +#define GICV5_IRS_IDR0_MEC BIT(10) +#define GICV5_IRS_IDR0_SETLPI BIT(9) +#define GICV5_IRS_IDR0_VIRT_ONE_N BIT(8) +#define GICV5_IRS_IDR0_ONE_N BIT(7)#define GICV5_IRS_IDR0_VIRT BIT(6) +#define GICV5_IRS_IDR0_PA_RANGE GENMASK(5, 2) +#define GICV5_IRS_IDR0_INT_DOM GENMASK(1, 0) + +#define GICV5_IRS_IDR0_INT_DOM_SECURE 0b00 +#define GICV5_IRS_IDR0_INT_DOM_NON_SECURE 0b01 +#define GICV5_IRS_IDR0_INT_DOM_EL3 0b10 +#define GICV5_IRS_IDR0_INT_DOM_REALM 0b11#define GICV5_IRS_IDR1_PRIORITY_BITS GENMASK(22, 20)#define GICV5_IRS_IDR1_IAFFID_BITS GENMASK(19, 16) +#define GICV5_IRS_IDR1_PE_CNT GENMASK(15, 0)#define GICV5_IRS_IDR1_PRIORITY_BITS_1BITS 0b000#define GICV5_IRS_IDR1_PRIORITY_BITS_2BITS 0b001
@@ -105,13 +159,30 @@#define GICV5_IRS_IDR2_LPI BIT(5)#define GICV5_IRS_IDR2_ID_BITS GENMASK(4, 0) +#define GICV5_IRS_IST_L2SZ_SUPPORT_4KB (r) FIELD_GET(BIT(11), (r)) +#define GICV5_IRS_IST_L2SZ_SUPPORT_16KB (r) FIELD_GET(BIT(12), (r)) +#define GICV5_IRS_IST_L2SZ_SUPPORT_64KB (r) FIELD_GET(BIT(13), (r)) + +#define GICV5_IRS_IDR3_VMT_LEVELS BIT(10) +#define GICV5_IRS_IDR3_VM_ID_BITS GENMASK(9, 5) +#define GICV5_IRS_IDR3_VMD_SZ GENMASK(4, 1) +#define GICV5_IRS_IDR3_VMD BIT(0) + +#define GICV5_IRS_IDR4_VPE_ID_BITS GENMASK(9, 6) +#define GICV5_IRS_IDR4_VPED_SZ GENMASK(5, 0) +#define GICV5_IRS_IDR5_SPI_RANGE GENMASK(24, 0)#define GICV5_IRS_IDR6_SPI_IRS_RANGE GENMASK(24, 0)#define GICV5_IRS_IDR7_SPI_BASE GENMASK(23, 0) -#define GICV5_IRS_IST_L2SZ_SUPPORT_4KB (r) FIELD_GET(BIT(11), (r)) -#define GICV5_IRS_IST_L2SZ_SUPPORT_16KB (r) FIELD_GET(BIT(12), (r)) -#define GICV5_IRS_IST_L2SZ_SUPPORT_64KB (r) FIELD_GET(BIT(13), (r)) +#define GICV5_IRS_IIDR_PRODUCT_ID GENMASK(31, 20) +#define GICV5_IRS_IIDR_VARIANT GENMASK(19, 16) +#define GICV5_IRS_IIDR_REVISION GENMASK(15, 12) +#define GICV5_IRS_IIDR_IMPLEMENTER GENMASK(11, 0) + +#define GICV5_IRS_AIDR_COMPONENT GENMASK(11, 8) +#define GICV5_IRS_AIDR_ARCHMAJORREV GENMASK(7, 4) +#define GICV5_IRS_AIDR_ARCHMINORREV GENMASK(3, 0)#define GICV5_IRS_CR0_IDLE BIT(1)#define GICV5_IRS_CR0_IRSEN BIT(0)
@@ -134,21 +205,39 @@#define GICV5_IRS_SYNC_STATUSR_IDLE BIT(0) -#define GICV5_IRS_SPI_STATUSR_V BIT(1) -#define GICV5_IRS_SPI_STATUSR_IDLE BIT(0) +#define GICV5_IRS_SPI_VMR_VIRT BIT_ULL(63) +#define GICV5_IRS_SPI_VMR_VM_ID GENMASK_ULL(15, 0)#define GICV5_IRS_SPI_SELR_ID GENMASK(23, 0) +#define GICV5_IRS_SPI_DOMAINR_DOMAIN GENMASK(1, 0) + +#define GICV5_IRS_SPI_DOMAINR_DOMAIN_SECURE 0b00 +#define GICV5_IRS_SPI_DOMAINR_DOMAIN_NON_SECURE 0b01 +#define GICV5_IRS_SPI_DOMAINR_DOMAIN_EL3 0b10 +#define GICV5_IRS_SPI_DOMAINR_DOMAIN_REALM 0b11 + +#define GICV5_IRS_SPI_RESAMPLER_ID GENMASK(23, 0) +#define GICV5_IRS_SPI_CFGR_TM BIT(0) +#define GICV5_IRS_SPI_CFGR_TM_EDGE 0b0 +#define GICV5_IRS_SPI_CFGR_TM_LEVEL 0b1 + +#define GICV5_IRS_SPI_STATUSR_V BIT(1) +#define GICV5_IRS_SPI_STATUSR_IDLE BIT(0) +#define GICV5_IRS_PE_SELR_IAFFID GENMASK(15, 0) +#define GICV5_IRS_PE_STATUSR_ONLINE BIT(2)#define GICV5_IRS_PE_STATUSR_V BIT(1)#define GICV5_IRS_PE_STATUSR_IDLE BIT(0)#define GICV5_IRS_PE_CR0_DPS BIT(0) -#define GICV5_IRS_IST_STATUSR_IDLE BIT(0) +#define GICV5_IRS_IST_BASER_ADDR_MASK GENMASK_ULL(55, 6) +#define GICV5_IRS_IST_BASER_VALID BIT_ULL(0) +#define GICV5_IRS_IST_BASER_ADDR_SHIFT 6ULL#define GICV5_IRS_IST_CFGR_STRUCTURE BIT(16)#define GICV5_IRS_IST_CFGR_ISTSZ GENMASK(8, 7)
@@ -166,15 +255,111 @@#define GICV5_IRS_IST_CFGR_L2SZ_16K 0b01#define GICV5_IRS_IST_CFGR_L2SZ_64K 0b10 -#define GICV5_IRS_IST_BASER_ADDR_MASK GENMASK_ULL(55, 6) -#define GICV5_IRS_IST_BASER_VALID BIT_ULL(0) +#define GICV5_IRS_IST_STATUSR_IDLE BIT(0)#define GICV5_IRS_MAP_L2_ISTR_ID GENMASK(23, 0) +#define GICV5_IRS_VMT_BASER_ADDR GENMASK_ULL(55, 3) +#define GICV5_IRS_VMT_BASER_ADDR_SHIFT 3ULL +#define GICV5_IRS_VMT_BASER_VALID BIT_ULL(0) + +#define GICV5_IRS_VMT_CFGR_STRUCTURE_TWO_LEVEL 0b1 +#define GICV5_IRS_VMT_CFGR_STRUCTURE_LINEAR 0b0 + +#define GICV5_IRS_VMT_CFGR_STRUCTURE BIT(16) +#define GICV5_IRS_VMT_CFGR_VM_ID_BITS GENMASK(4, 0) + +#define GICV5_IRS_VMT_STATUSR_IDLE BIT(0) + +#define GICV5_IRS_VPE_SELR_S BIT_ULL(63) +#define GICV5_IRS_VPE_SELR_VPE_ID GENMASK_ULL(47, 32) +#define GICV5_IRS_VPE_SELR_VM_ID GENMASK_ULL(15, 0) + +#define GICV5_IRS_VPE_DBR_DBV BIT_ULL(63) +#define GICV5_IRS_VPE_DBR_REQ_DB BIT_ULL(62) +#define GICV5_IRS_VPE_DBR_DBPM GENMASK_ULL(36, 32) +#define GICV5_IRS_VPE_DBR_INTID GENMASK_ULL(23, 0) + +#define GICV5_IRS_VPE_HPPIR_HPPIV BIT_ULL(32) +#define GICV5_IRS_VPE_HPPIR_TYPE GENMASK_ULL(31, 29) +#define GICV5_IRS_VPE_HPPIR_ID GENMASK_ULL(23, 0) + +#define GICV5_IRS_VPE_CR0_DPS BIT(0) + +#define GICV5_IRS_VPE_STATUSR_V BIT(1) +#define GICV5_IRS_VPE_STATUSR_IDLE BIT(0) + +#define GICV5_IRS_VM_DBR_EN BIT_ULL(63) +#define GICV5_IRS_VM_DBR_VPE_ID GENMASK_ULL(15, 0) + +#define GICV5_IRS_VM_SELR_VM_ID GENMASK(15, 0) + +#define GICV5_IRS_VM_STATUSR_V BIT(1) +#define GICV5_IRS_VM_STATUSR_IDLE BIT(0) + +#define GICV5_IRS_VMAP_L2_VMTR_M BIT_ULL(63) +#define GICV5_IRS_VMAP_L2_VMTR_VM_ID GENMASK_ULL(15, 0) + +#define GICV5_IRS_VMAP_VMR_M BIT_ULL(63) +#define GICV5_IRS_VMAP_VMR_U BIT_ULL(62) +#define GICV5_IRS_VMAP_VMR_VM_ID GENMASK_ULL(15, 0) + +#define GICV5_IRS_VMAP_VISTR_M BIT_ULL(63) +#define GICV5_IRS_VMAP_VISTR_U BIT_ULL(62) +#define GICV5_IRS_VMAP_VISTR_VM_ID GENMASK_ULL(47, 32) +#define GICV5_IRS_VMAP_VISTR_TYPE GENMASK_ULL(31, 29) + +#define GICV5_IRS_VMAP_L2_VISTR_M BIT_ULL(63) +#define GICV5_IRS_VMAP_L2_VISTR_VM_ID GENMASK_ULL(47, 32) +#define GICV5_IRS_VMAP_L2_VISTR_TYPE GENMASK_ULL(31, 29) +#define GICV5_IRS_VMAP_L2_VISTR_ID GENMASK_ULL(23, 0) + +#define GICV5_IRS_VMAP_VPER_M BIT_ULL(63) +#define GICV5_IRS_VMAP_VPER_VM_ID GENMASK_ULL(47, 32) +#define GICV5_IRS_VMAP_VPER_VPE_ID GENMASK_ULL(15, 0) + +#define GICV5_IRS_SAVE_VMR_VM_ID GENMASK_ULL(15, 0) +#define GICV5_IRS_SAVE_VMR_Q BIT_ULL(62) +#define GICV5_IRS_SAVE_VMR_S BIT_ULL(63) + +#define GICV5_IRS_SAVE_VM_STATUSR_IDLE BIT(0) +#define GICV5_IRS_SAVE_VM_STATUSR_Q BIT(1) + +#define GICV5_IRS_MEC_IDR_MECIDSIZE GENMASK(3, 0) + +#define GICV5_IRS_MEC_MECID_R_MECID GENMASK(15, 0) + +#define GICV5_IRS_MPAM_IDR_HAS_MPAM_SP BIT(24) +#define GICV5_IRS_MPAM_IDR_PMG_MAX GENMASK(23, 16) +#define GICV5_IRS_MPAM_IDR_PARTID_MAX GENMASK(15, 0) + +#define GICV5_IRS_MPAM_PARTID_R_IDLE BIT(31) +#define GICV5_IRS_MPAM_PARTID_R_MPAM_SP GENMASK(25, 24) +#define GICV5_IRS_MPAM_PARTID_R_PMG GENMASK(23, 16) +#define GICV5_IRS_MPAM_PARTID_R_PARTID GENMASK(15, 0) + +#define GICV5_IRS_SWERR_STATUSR_IMP_EC GENMASK_ULL(31, 24) +#define GICV5_IRS_SWERR_STATUSR_EC GENMASK_ULL(23, 16) +#define GICV5_IRS_SWERR_STATUSR_OF BIT_ULL(3) +#define GICV5_IRS_SWERR_STATUSR_S1V BIT_ULL(2) +#define GICV5_IRS_SWERR_STATUSR_S0V BIT_ULL(1) +#define GICV5_IRS_SWERR_STATUSR_V BIT_ULL(0) + +#define GICV5_IRS_SWERR_SYNDROMER0_VIRTUAL BIT_ULL(63) +#define GICV5_IRS_SWERR_SYNDROMER0_TYPE GENMASK_ULL(62, 60) +#define GICV5_IRS_SWERR_SYNDROMER0_ID GENMASK_ULL(55, 32) +#define GICV5_IRS_SWERR_SYNDROMER0_VM_ID GENMASK_ULL(15, 0) + +#define GICV5_IRS_SWERR_SYNDROMER1_ADDR GENMASK_ULL(55, 3) +#define GICV5_ISTL1E_VALID BIT_ULL(0) +#define GICV5_IRS_ISTL1E_SIZE 8UL#define GICV5_ISTL1E_L2_ADDR_MASK GENMASK_ULL(55, 12) +#define GICV5_IRS_SETLPIR 0x0000 +#define GICV5_IRS_SETLPIR_ID GENMASK(23, 0) +/* * ITS registers and tables structures */
@@ -296,6 +481,42 @@#define GICV5_GSI_IWB_FRAME_ID GENMASK(28, 16)#define GICV5_GSI_IWB_WIRE GENMASK(15, 0) +/* +* CoreSight identification registers - ordered by increasing offset. +*/ +#define GICV5_CORESIGHT_DEVARCH 0xffbc +#define GICV5_CORESIGHT_PIDR4 0xffd0 +#define GICV5_CORESIGHT_PIDR5 0xffd4 +#define GICV5_CORESIGHT_PIDR6 0xffd8 +#define GICV5_CORESIGHT_PIDR7 0xffdc +#define GICV5_CORESIGHT_PIDR0 0xffe0 +#define GICV5_CORESIGHT_PIDR1 0xffe4 +#define GICV5_CORESIGHT_PIDR2 0xffe8 +#define GICV5_CORESIGHT_PIDR3 0xffec +#define GICV5_CORESIGHT_CIDR0 0xfff0 +#define GICV5_CORESIGHT_CIDR1 0xfff4 +#define GICV5_CORESIGHT_CIDR2 0xfff8 +#define GICV5_CORESIGHT_CIDR3 0xfffc + +#define GICV5_CORESIGHT_DEVARCH_VAL \ + (FIELD_PREP(GENMASK(31, 21), 0x23b) | \ + BIT(20) | \ + 0x5a19) + +#define GICV5_CORESIGHT_PIDR4_JEP106_CONT 0x04 +#define GICV5_CORESIGHT_PIDR5_RES0 0x00 +#define GICV5_CORESIGHT_PIDR6_RES0 0x00 +#define GICV5_CORESIGHT_PIDR7_RES0 0x00 +#define GICV5_CORESIGHT_PIDR0_PART_0 0x4b +#define GICV5_CORESIGHT_PIDR1_DES_0_PART_1 0xb0 +#define GICV5_CORESIGHT_PIDR2_DES_1 0x0b +#define GICV5_CORESIGHT_PIDR3_REVAND_CMOD 0x00 + +#define GICV5_CORESIGHT_CIDR0_VAL 0x0d +#define GICV5_CORESIGHT_CIDR1_VAL 0xf0 +#define GICV5_CORESIGHT_CIDR2_VAL 0x05 +#define GICV5_CORESIGHT_CIDR3_VAL 0xb1 +/* * Global Data structures and functions */
Signature · 1 line
2.34.1
[email protected]
AI reviewRe: [PATCH v5 04/49] KVM: arm64: gic-v5: Define remaining IRS MMIO registers
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] `GICV5_IRS_SETLPIR` is incorrectly defined with an offset of `0x0000`, which aliases the read-only…
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] GICV5_IRS_SETLPIR is incorrectly defined with an offset of 0x0000, which aliases the read-only GICV5_IRS_IDR0 register. --
--- Patch [4]: [PATCH v5 04/49] KVM: arm64: gic-v5: Define remaining IRS MMIO registers --- commit 4e5353692b267764abed96c8124c3aebc194d9cf
Author: Sascha Bischoff <[email protected]>
KVM: arm64: gic-v5: Define remaining IRS MMIO registers
Complete the set of defined IRS MMIO registers in the GICv5 header file. Up until now, the set of defined IRS MMIO registers has been driven by code requirements. However, in order to properly emulate the IRS MMIO interface in KVM, the full set of IRS MMIO registers needs to be added.
Quoted 5 lines
include/linux/irqchip/arm-gic-v5.h +0
-0@@ -62,20 +62,34 @@
[ ... ]
Quoted 6 lines
/*
* IRS registers and tables structures
*/
#defineGICV5_IRS_IDR00x0000
#defineGICV5_IRS_IDR10x0004
#defineGICV5_IRS_IDR20x0008
[ ... ]
Quoted 1 line
include/linux/irqchip/arm-gic-v5.h +0
-0@@ -166,15 +255,111 @@
[ ... ]
include/linux/irqchip/arm-gic-v5.h line 360
Quoted 6 lines
#define
GICV5_ISTL1E_VALIDBIT_ULL(0)include/linux/irqchip/arm-gic-v5.h +2
-0+#define GICV5_IRS_ISTL1E_SIZE 8UL#define GICV5_ISTL1E_L2_ADDR_MASK GENMASK_ULL(55, 12) +#define GICV5_IRS_SETLPIR 0x0000
[Severity: High]
Is GICV5_IRS_SETLPIR defined with the correct offset here?
This aliases the read-only GICV5_IRS_IDR0 register defined earlier at 0x0000.
If KVM implements the IRS MMIO emulator expecting guest writes to the SETLPIR register at offset 0x0000, will guests attempting to use the correct architectural offset for SETLPIR fail?
Could writes to 0x0000 also interfere with IDR0 emulation, potentially breaking LPI generation via the IRS in virtual machines?
Quoted 5 lines
include/linux/irqchip/arm-gic-v5.h +2
-0+#define GICV5_IRS_SETLPIR_ID GENMASK(23, 0) +/* * ITS registers and tables structures */
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
Re: [PATCH v5 04/49] KVM: arm64: gic-v5: Define remaining IRS MMIO registers
Quoted 43 lines · On Fri, 2026-08-07 at 12:05 +0000, [email protected] wrote:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High]GICV5_IRS_SETLPIRis incorrectly defined with an offset of0x0000, which aliases the read-onlyGICV5_IRS_IDR0register. ----- Patch [4]: [PATCH v5 04/49] KVM: arm64: gic-v5: Define remaining IRS MMIO registers ---
commit4e5353692b267764abed96c8124c3aebc194d9cf
Author: Sascha Bischoff <[email protected]>KVM: arm64: gic-v5: Define remaining IRS MMIO registers
Complete the set of defined IRS MMIO registers in the GICv5 header file.
Up until now, the set of defined IRS MMIO registers has been driven by
code requirements. However, in order to properly emulate the IRS MMIO interface in KVM, the full set of IRS MMIO registers needs to be added.Quoted 6 lines
include/linux/irqchip/arm-gic-v5.h +0
-0@@ -62,20 +62,34 @@[ ... ]
Quoted 6 lines
/*
* IRS registers and tables structures
*/
#defineGICV5_IRS_IDR00x0000
#defineGICV5_IRS_IDR10x0004
#defineGICV5_IRS_IDR20x0008[ ... ]
Quoted 1 line
include/linux/irqchip/arm-gic-v5.h +0
-0@@ -166,15 +255,111 @@[ ... ]
Quoted 9 lines
Quoted 6 lines
#define
GICV5_ISTL1E_VALIDBIT_ULL(0)
+#defineGICV5_IRS_ISTL1E_SIZE8UL
#defineGICV5_ISTL1E_L2_ADDR_MASKGENMASK_ULL(55, 12)
+#defineGICV5_IRS_SETLPIR0x0000[Severity: High]
IsGICV5_IRS_SETLPIRdefined with the correct offset here?
Yes. It is at offset 0 into the SETLPI frame, which in our implementation happens to follow the CONFIG_FRAME. The offset is correct for the frame the register is in.
Quoted 3 lines
This aliases the read-only
GICV5_IRS_IDR0register defined earlier at0x0000.
Except that the offset is applied to a different base address, so this is perfectly fine.
Quoted 5 lines
If KVM implements the IRS MMIO emulator expecting guest writes to the SETLPIR
register at offset0x0000, will guests attempting to use the correct architectural offset for SETLPIR fail?
No.
Quoted 11 lines
Could writes to
0x0000also interfere with IDR0 emulation, potentially
breaking LPI generation via the IRS in virtual machines?Quoted 5 lines
include/linux/irqchip/arm-gic-v5.h +2
-0+#define GICV5_IRS_SETLPIR_ID GENMASK(23, 0) +/*
* ITS registers and tables structures
*/
Thanks,
Sascha
PATCH v5 5/49 arm64/sysreg: Add GICv5 GIC VDPEND encoding
In reply to Sascha BischoffAdd the encoding for the GIC VDPEND system instruction. This operates on the virtual interrupt domain, and is used to make interrupts pending for a VM.
This is part of enabling GICv5 KVM support, and is required for injection of SPIs and LPIs.
--- arch/arm64/include/asm/sysreg.h | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-)
arch/arm64/include/asm/sysreg.h +12 -1
@@ -1040,7 +1040,7 @@#define GCS_CAP (x) ((((unsigned long)x) & GCS_CAP_ADDR_MASK) | \ GCS_CAP_VALID_TOKEN)/* -* Definitions for GICv5 instructions +* Definitions for GICv5 instructions for the Current Domain */ #define GICV5_OP_GIC_CDAFF sys_insn(1, 0, 12, 1, 3)#define GICV5_OP_GIC_CDDI sys_insn(1, 0, 12, 2, 0)
@@ -1105,6 +1105,17 @@#define GICV5_GICR_CDNMIA_TYPE_MASK GENMASK_ULL(31, 29)#define GICV5_GICR_CDNMIA_ID_MASK GENMASK_ULL(23, 0) +/* +* Definitions for GICv5 instructions for the Virtual Domain +*/ +#define GICV5_OP_GIC_VDPEND sys_insn(1, 4, 12, 1, 4) + +/* Shift and mask definitions for GIC VDPEND */ +#define GICV5_GIC_VDPEND_PENDING_MASK BIT_ULL(63) +#define GICV5_GIC_VDPEND_VM_MASK GENMASK_ULL(47, 32) +#define GICV5_GIC_VDPEND_TYPE_MASK GENMASK_ULL(31, 29) +#define GICV5_GIC_VDPEND_ID_MASK GENMASK_ULL(23, 0) +#define gicr_insn (insn) read_sysreg_s(GICV5_OP_GICR_##insn)#define gic_insn (v, insn) write_sysreg_s(v, GICV5_OP_GIC_##insn)
Signature · 1 line
2.34.1
PATCH v5 6/49 arm64/sysreg: Update ICC_CR0_EL1 with LINK and LINK_IDLE fields
In reply to Sascha BischoffThese fields have been added to the architecture since this register was added to the generator, and were hence missing.
--- arch/arm64/tools/sysreg | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
arch/arm64/tools/sysreg +3 -1
@@ -3736,7 +3736,9 @@ Sysreg ICC_CR0_EL1 3 1 12 0 1 Res0 63:39 Field 38 PID Field 37:32 IPPT -Res0 31:1 +Res0 31:3 +Field 2 LINK_IDLE +Field 1 LINK Field 0 EN EndSysreg
Signature · 1 line
2.34.1
[email protected]
AI reviewRe: [PATCH v5 06/49] arm64/sysreg: Update ICC_CR0_EL1 with LINK and LINK_IDLE fields
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
Pre-existing issues:
- [High] Missing Context Synchronization Event (ISB) after control-plane system register writes.
- [High] CONSTRAINED UNPREDICTABLE instruction encoding when emitting system instructions that structurally demand a specific fixed register (e.g., CDEOI).
- [Medium] Potential clobbering of newly defined LINK and LINK_IDLE fields in ICC_CR0_EL1 due to absolute writes.
--
--- Patch [6]: [PATCH v5 06/49] arm64/sysreg: Update ICC_CR0_EL1 with LINK and LINK_IDLE fields --- commit 0855c41ea55b7d483129dc1d181e0c36c00b65e4
Author: Sascha Bischoff <[email protected]>
arm64/sysreg: Update ICC_CR0_EL1 with LINK and LINK_IDLE fields
This commit adds the LINK and LINK_IDLE fields to the ICC_CR0_EL1 sysreg definition. These fields were missing since the register was added to the generator.
arch/arm64/tools/sysreg Sysreg ICC_CR0_EL1 3 1 12 0 1 · line 3742
Quoted 12 lines
arch/arm64/tools/sysreg +3
-1@@ -3737,7 +3737,9 @@ Sysreg ICC_CR0_EL1 3 1 12 0 1 Res0 63:39 Field 38 PID Field 37:32 IPPT -Res0 31:1 +Res0 31:3 +Field 2 LINK_IDLE +Field 1 LINK
[Severity: Medium]
This isn't a bug introduced by this patch, but does the initialization code handle these new fields correctly?
If LINK and LINK_IDLE are expected to retain firmware-configured or default reset values, using an absolute write to initialize only the EN bit will clear them to zero, potentially breaking hardware functionality.
For instance, this pattern is seen in drivers/irqchip/irq-gic-v5.c in gicv5_cpu_enable_interrupts():
cr0 = FIELD_PREP(ICC_CR0_EL1_EN, 1); write_sysreg_s(cr0, SYS_ICC_CR0_EL1);
A similar absolute write clearing the newly defined bits is also present in tools/testing/selftests/kvm/include/arm64/gic_v5.h.
Could you confirm if the consumers are robust to this change?
[Severity: High]
This is a pre-existing issue, but I noticed a missing context synchronization event after control-plane system register writes in the GICv5 driver.
Executing instructions in an architecturally undefined pipeline state can result in unexpected exceptions, corrupted accesses, or dropped interrupts.
Multiple functions batch writes to ICC_*_EL1 registers or return immediately after a write without issuing an isb().
For example, in drivers/irqchip/irq-gic-v5.c:gicv5_cpu_disable_interrupts():
cr0 = FIELD_PREP(ICC_CR0_EL1_EN, 0); write_sysreg_s(cr0, SYS_ICC_CR0_EL1); }
A similar pattern batches consecutive writes in gicv5_cpu_enable_interrupts():
write_sysreg_s(0, SYS_ICC_PPI_ENABLER0_EL1); write_sysreg_s(0, SYS_ICC_PPI_ENABLER1_EL1);
Are these writes supposed to have an isb() as the very next instruction to synchronize the pipeline?
[Severity: High]
This is a pre-existing issue, but is there a risk of constrained unpredictable instruction encoding when emitting system instructions like CDEOI?
The write_sysreg_s macro relies on the compiler's "rZ" constraint for register allocation.
System instructions like GIC CDEOI architecturally mandate the use of XZR (register 31). Allowing the compiler to select an arbitrary general-purpose register containing zero misencodes the instruction.
This occurs in tools/testing/selftests/kvm/include/arm64/gic_v5.h:
#define gic_insn(v, insn) write_sysreg_s(v, GICV5_OP_GIC_##insn)
and is used in drivers/irqchip/irq-gic-v5.c:gicv5_handle_irq():
gic_insn(0, CDEOI);
Should this be hardcoded to use XZR to prevent the hardware from treating it as constrained unpredictable?
Quoted 2 lines
arch/arm64/tools/sysreg +0
-0Field 0 EN EndSysreg
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=6
Re: [PATCH v5 06/49] arm64/sysreg: Update ICC_CR0_EL1 with LINK and LINK_IDLE fields
Quoted 54 lines · On Fri, 2026-08-07 at 12:17 +0000, [email protected] wrote:
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
Pre-existing issues:
- [High] Missing Context Synchronization Event (ISB) after control- plane system register writes.
- [High] CONSTRAINED UNPREDICTABLE instruction encoding when emitting system instructions that structurally demand a specific fixed register (e.g.,CDEOI).
- [Medium] Potential clobbering of newly definedLINKandLINK_IDLEfields inICC_CR0_EL1due to absolute writes. ----- Patch [6]: [PATCH v5 06/49] arm64/sysreg: Update
ICC_CR0_EL1with LINK andLINK_IDLEfields ---
commit0855c41ea55b7d483129dc1d181e0c36c00b65e4
Author: Sascha Bischoff <[email protected]>arm64/sysreg: Update
ICC_CR0_EL1with LINK andLINK_IDLEfields
This commit adds the LINK andLINK_IDLEfields to theICC_CR0_EL1sysreg
definition. These fields were missing since the register was added to the
generator.arch/arm64/tools/sysreg Sysreg ICC_CR0_EL1 3 1 12 0 1 · line 3741
Quoted 13 lines
arch/arm64/tools/sysreg +0
-0@@ -3737,7 +3737,9 @@Sysreg
ICC_CR0_EL13 1 12 0 1
Res0 63:39
Field 38 PID
Field 37:32 IPPTarch/arm64/tools/sysreg +3
-1-Res0 31:1 +Res0 31:3 +Field 2 LINK_IDLE +Field 1 LINK[Severity: Medium]
This isn't a bug introduced by this patch, but does the initialization
code handle these new fields correctly?If LINK and
LINK_IDLEare expected to retain firmware-configured or default
reset values, using an absolute write to initialize only the EN bit will clear
them to zero, potentially breaking hardware functionality.For instance, this pattern is seen in
drivers/irqchip/irq-gic-v5.cingicv5_cpu_enable_interrupts():
Quoted 8 lines
cr0 = FIELD_PREP(ICC_CR0_EL1_EN, 1); write_sysreg_s(cr0, SYS_ICC_CR0_EL1);A similar absolute write clearing the newly defined bits is also present in
tools/testing/selftests/kvm/include/arm64/gic_v5.h.Could you confirm if the consumers are robust to this change?
Right now, the Linux host driver implementation somewhat assumes that EL3 is present. If EL3 is present, these fields are RO as it is EL3's responsibility to manage the link state. In this case, not doing a read-modify-write won't break anything.
If EL3 is not present, NS software must establish this link (if not established) and keep it active. However, in this case the IWB also breaks as the Most Permissive Security State must enable and configure it. If EL3 doesn't exist, then we have a single world and hence this becomes NS's responsibility too. In the current implementation, this would break too.
Although this is unlikely to be an issue anytime soon, I'll post a separate fix for the ICC_CR0_EL1 change.
Quoted 24 lines
[Severity: High]
This is a pre-existing issue, but I noticed a missing context synchronization
event after control-plane system register writes in the GICv5 driver.Executing instructions in an architecturally undefined pipeline state can result
in unexpected exceptions, corrupted accesses, or dropped interrupts.Multiple functions batch writes to ICC_*_EL1 registers or return immediately
after a write without issuing anisb().For example, in
drivers/irqchip/irq-gic-
v5.c:gicv5_cpu_disable_interrupts():cr0 = FIELD_PREP(ICC_CR0_EL1_EN, 0); write_sysreg_s(cr0, SYS_ICC_CR0_EL1); }A similar pattern batches consecutive writes in
gicv5_cpu_enable_interrupts():
Quoted 6 lines
write_sysreg_s(0, SYS_ICC_PPI_ENABLER0_EL1); write_sysreg_s(0, SYS_ICC_PPI_ENABLER1_EL1);Are these writes supposed to have an
isb()as the very next instruction to
synchronize the pipeline?
No. We don't need an ISB after every system register write. This is required in come circumstances and not others, and putting them everywhere would be excessive.
Quoted 21 lines
[Severity: High]
This is a pre-existing issue, but is there a risk of constrained unpredictable
instruction encoding when emitting system instructions like CDEOI?The
write_sysreg_smacro relies on the compiler's "rZ" constraint for register
allocation.System instructions like GIC CDEOI architecturally mandate the use of XZR
(register 31). Allowing the compiler to select an arbitrary general- purpose
register containing zero misencodes the instruction.This occurs in
tools/testing/selftests/kvm/include/arm64/gic_v5.h:#define gic_insn(v, insn) write_sysreg_s(v,GICV5_OP_GIC_##insn)
Quoted 7 lines
and is used in drivers/irqchip/irq-gic-v5.c:gicv5_handle_irq():
gic_insn(0, CDEOI);Should this be hardcoded to use XZR to prevent the hardware from treating it
as constrained unpredictable?
If you look at the definition of write_sysreg_s(), then you'd see that it explicitly uses XZR if a value of 0 is written. This is not an issue anymore.
Quoted 4 lines
Quoted 3 lines
Field 0 EN
EndSysreg
Thanks,
Sascha
PATCH v5 7/49 KVM: arm64: gic-v5: Cache host IRS ID registers
In reply to Sascha BischoffKVM needs access to the host IRS configuration frame to configure virtual-machine resources. The frame ID registers describe the capabilities available to KVM.
Cache IRS_IDR2, IRS_IDR3, and IRS_IDR4 rather than extracting their individual fields into kvm_vgic_global_state. Accessor helpers derive the required capabilities from the cached registers.
Use the cached IRS_IDR4 VPE capacity to limit the maximum number of vCPUs, while retaining VGIC_V5_MAX_CPUS as KVM's own upper bound.
Keeping the raw register values lets KVM adjust the exposed capabilities when needed, as is done for ICH_VTR_EL2, and avoids tying future EL2 code to the layout of kvm_vgic_global_state.
--- arch/arm64/kvm/vgic/vgic-v5.c | 22 +++++++++- include/kvm/arm_vgic.h | 75 +++++++++++++++++++++++++++++++++++ 2 files changed, 95 insertions(+), 2 deletions(-)
arch/arm64/kvm/vgic/vgic-v5.c +20 -2
@@ -11,6 +11,7 @@#include "vgic.h" #define ppi_caps kvm_vgic_global_state.vgic_v5_ppi_caps +#define irs_caps kvm_vgic_global_state.vgic_v5_irs_caps/* * Not all PPIs are guaranteed to be implemented for GICv5. Deterermine which
@@ -34,6 +35,21 @@ static void vgic_v5_get_implemented_ppis(void)__assign_bit (GICV5_ARCH_PPI_PMUIRQ , ppi_caps.impl_ppi_mask,system_supports_pmuv3 ()); } +static u32 irs_readl_relaxed (const u32 reg_offset) +{ +return readl_relaxed (irs_caps.irs_base + reg_offset); +} + +static void vgic_v5_irs_cache_id_regs (const struct gic_kvm_info *info) +{ + irs_caps.irs_base = info->gicv5_irs.base; + irs_caps.non_coherent = info->gicv5_irs.non_coherent; + + irs_caps.idr2 =irs_readl_relaxed (GICV5_IRS_IDR2 ); + irs_caps.idr3 =irs_readl_relaxed (GICV5_IRS_IDR3 ); + irs_caps.idr4 =irs_readl_relaxed (GICV5_IRS_IDR4 ); +} +/* * Probe for a vGICv5 compatible interrupt controller, returning 0 on success. */
@@ -61,10 +77,12 @@ int vgic_v5_probe(const struct gic_kvm_info *info) goto skip_v5; } - kvm_vgic_global_state.max_gic_vcpus =VGIC_V5_MAX_CPUS ; - +vgic_v5_irs_cache_id_regs (info);vgic_v5_get_implemented_ppis (); + kvm_vgic_global_state.max_gic_vcpus =min (vgic_v5_irs_max_vpes (&irs_caps), +VGIC_V5_MAX_CPUS ); + ret =kvm_register_vgic_device (KVM_DEV_TYPE_ARM_VGIC_V5 );if (ret) {kvm_err ("Cannot register GICv5 KVM device.\n" );
include/kvm/arm_vgic.h +75 -0
93 lines not on this page · raw message
Signature · 1 line
2.34.1
[email protected]
AI reviewRe: [PATCH v5 07/49] KVM: arm64: gic-v5: Cache host IRS ID registers
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [High] The hardware-based limit for maximum vCPUs is silently clobbered by a hardcoded maximum in the…
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [High] The hardware-based limit for maximum vCPUs is silently clobbered by a hardcoded maximum in the GICv3 legacy fallback path.
- [Medium] The return type of vgic_v5_irs_vped_size() is u16, which will silently truncate descriptor sizes of 64KB or larger to 0. --
--- Patch [7]: [PATCH v5 07/49] KVM: arm64: gic-v5: Cache host IRS ID registers --- commit d81802dd59faf96c570bb509e00ac335dada7904
Author: Sascha Bischoff <[email protected]>
KVM: arm64: gic-v5: Cache host IRS ID registers
This commit caches the GICv5 IRS ID registers to configure virtual-machine resources based on hardware capabilities rather than extracting individual fields. It limits the maximum number of vCPUs using the VPE capacity from the cached IRS_IDR4 register while retaining KVM's upper bound.
Quoted 4 lines
arch/arm64/kvm/vgic/vgic-v5.c +0
-0
[ ... ]
arch/arm64/kvm/vgic/vgic-v5.c int vgic_v5_probe(const struct gic_kvm_info *info) · line 85
Quoted 12 lines
arch/arm64/kvm/vgic/vgic-v5.c +4
-2@@ -61,10 +77,12 @@ int vgic_v5_probe(const struct gic_kvm_info *info) goto skip_v5; } - kvm_vgic_global_state.max_gic_vcpus =VGIC_V5_MAX_CPUS ; - +vgic_v5_irs_cache_id_regs (info);vgic_v5_get_implemented_ppis (); + kvm_vgic_global_state.max_gic_vcpus =min (vgic_v5_irs_max_vpes (&irs_caps), +VGIC_V5_MAX_CPUS ); +
[Severity: High]
Does the GICv3 legacy fallback path under the skip_v5 label in vgic_v5_probe() overwrite this dynamically computed hardware limit?
If the max_gic_vcpus limit is later expanded by a static value (like VGIC_V3_MAX_CPUS via a max() assignment), could it permit userspace to create more vCPUs for a GICv5 VM than the hardware IRS actually supports (for example, allowing 512 vCPUs even if the hardware restricts it to 16 VPEs)?
Quoted 8 lines
arch/arm64/kvm/vgic/vgic-v5.c +0
-0ret =kvm_register_vgic_device (KVM_DEV_TYPE_ARM_VGIC_V5 );if (ret) {kvm_err ("Cannot register GICv5 KVM device.\n" );include/kvm/arm_vgic.h +0
-0
[ ... ]
Quoted 4 lines
include/kvm/arm_vgic.h +0
-0@@ -630,6 +646,65 @@ int vgic_v4_load(struct kvm_vcpu *vcpu);void vgic_v4_commit (struct kvm_vcpu *vcpu);int vgic_v4_put (struct kvm_vcpu *vcpu);
[ ... ]
include/kvm/arm_vgic.h int vgic_v4_load(struct kvm_vcpu *vcpu); · line 698
Quoted 5 lines
include/kvm/arm_vgic.h +5
-0+static inline u16 vgic_v5_irs_vped_size (const struct vgic_v5_irs_caps *caps) +{ +return BIT (FIELD_GET (GICV5_IRS_IDR4_VPED_SZ , caps->idr4)); +} +
[Severity: Medium]
If a hardware implementation reports a size parameter of 16 or greater (indicating 64KB or more), will the BIT() operation in vgic_v5_irs_vped_size() result in 65536 and silently truncate to 0 when cast to the u16 return type?
Could this zero-byte size calculation lead to 0-byte allocations or infinite loops in dependent resource sizing?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=7
Re: [PATCH v5 07/49] KVM: arm64: gic-v5: Cache host IRS ID registers
Quoted 43 lines · On Fri, 2026-08-07 at 12:27 +0000, [email protected] wrote:
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [High] The hardware-based limit for maximum vCPUs is silently clobbered by a hardcoded maximum in the GICv3 legacy fallback path.
- [Medium] The return type ofvgic_v5_irs_vped_size()isu16, which will silently truncate descriptor sizes of 64KB or larger to 0. ----- Patch [7]: [PATCH v5 07/49] KVM: arm64: gic-v5: Cache host IRS ID registers ---
commitd81802dd59faf96c570bb509e00ac335dada7904
Author: Sascha Bischoff <[email protected]>KVM: arm64: gic-v5: Cache host IRS ID registers
This commit caches the GICv5 IRS ID registers to configure virtual- machine
resources based on hardware capabilities rather than extracting individual
fields. It limits the maximum number of vCPUs using the VPE capacity from
the cachedIRS_IDR4register while retaining KVM's upper bound.Quoted 5 lines
arch/arm64/kvm/vgic/vgic-v5.c +0
-0[ ... ]
Quoted 12 lines
arch/arm64/kvm/vgic/vgic-v5.c +0
-0@@ -61,10 +77,12 @@ int vgic_v5_probe(const struct gic_kvm_info *info) goto skip_v5; }arch/arm64/kvm/vgic/vgic-v5.c +1
-2- kvm_vgic_global_state.max_gic_vcpus =VGIC_V5_MAX_CPUS ; - +vgic_v5_irs_cache_id_regs (info);
vgic_v5_get_implemented_ppis();arch/arm64/kvm/vgic/vgic-v5.c +1
-0+ kvm_vgic_global_state.max_gic_vcpus =min(vgic_v5_irs_max_vpes(&irs_caps),
Quoted 8 lines
Quoted 3 lines
arch/arm64/kvm/vgic/vgic-v5.c +1
-0+
VGIC_V5_MAX_CPUS);
+[Severity: High]
Does the GICv3 legacy fallback path under theskip_v5label invgic_v5_probe()
overwrite this dynamically computed hardware limit?
First of all, this isn't a fallback path. Secondly, we treat the limits for GICv3 and GICv5 differently. The GICv5 limit comes from a physical limit in the hardware, but the GICv3 limit does not.
"KVM: arm64: gic-v5: Keep GICv5 vCPU limit model-specific" explicitly makes sure that the two limits are treated differently for the different GIC models.
Quoted 26 lines
If the
max_gic_vcpuslimit is later expanded by a static value (likeVGIC_V3_MAX_CPUSvia amax()assignment), could it permit userspace to create
more vCPUs for a GICv5 VM than the hardware IRS actually supports (for example,
allowing 512 vCPUs even if the hardware restricts it to 16 VPEs)?Quoted 8 lines
ret =
kvm_register_vgic_device(KVM_DEV_TYPE_ARM_VGIC_V5); if (ret) {
kvm_err("Cannot register GICv5 KVM device.\n");include/kvm/arm_vgic.h +0
-0[ ... ]
Quoted 4 lines
include/kvm/arm_vgic.h +0
-0@@ -630,6 +646,65 @@ int vgic_v4_load(struct kvm_vcpu *vcpu); void vgic_v4_commit(struct kvm_vcpu *vcpu); int vgic_v4_put(struct kvm_vcpu *vcpu);[ ... ]
Quoted 14 lines
include/kvm/arm_vgic.h int vgic_v4_load(struct kvm_vcpu *vcpu); · line 698
Quoted 6 lines
+static inline u16
vgic_v5_irs_vped_size(const struct vgic_v5_irs_caps *caps)include/kvm/arm_vgic.h +4
-0+{ +return BIT (FIELD_GET (GICV5_IRS_IDR4_VPED_SZ , caps->idr4)); +} +[Severity: Medium]
If a hardware implementation reports a size parameter of 16 or greater
(indicating 64KB or more), will theBIT()operation invgic_v5_irs_vped_size()
result in 65536 and silently truncate to 0 when cast to the u16 return type?
The minimum valid value for this field is 3 and the maximum valid value is 12. This is not an issue.
Quoted 5 lines
Could this zero-byte size calculation lead to 0-byte allocations or infinite
loops in dependent resource sizing?
No.
Thanks,
Sascha
PATCH v5 8/49 KVM: arm64: gic-v5: Add VPE doorbell domain
In reply to Sascha BischoffVPE doorbells allow the GICv5 hardware to notify KVM when an SPI or LPI can be signalled to a non-resident VPE. This provides the mechanism used to wake blocked vcpus once the hardware determines that the interrupt is eligible to be delivered.
Add support for a per-VM VPE doorbell irq domain. The domain is created under the GICv5 LPI domain, with one doorbell allocated per VPE. Store the allocated doorbell base in the VM's GICv5 state so that later patches can request per-vcpu doorbell IRQs and use them for IRS commands and wakeups.
Add the per-VPE doorbell state to the GICv5 CPU interface state. The doorbell IRQ number is populated when the IRQs are requested, and the db_fired state is used by later patches once doorbell delivery is wired up.
--- arch/arm64/kvm/vgic/vgic-init.c | 18 ++-- arch/arm64/kvm/vgic/vgic-v5.c | 144 +++++++++++++++++++++++++++++ arch/arm64/kvm/vgic/vgic.h | 1 + include/kvm/arm_vgic.h | 4 + include/linux/irqchip/arm-gic-v5.h | 2 + 5 files changed, 163 insertions(+), 6 deletions(-)
arch/arm64/kvm/vgic/vgic-init.c +12 -6
@@ -492,16 +492,22 @@ static void kvm_vgic_dist_destroy(struct kvm *kvm) dist->nr_spis =0 ; dist->vgic_dist_base =VGIC_ADDR_UNDEF ; -if (dist->vgic_model ==KVM_DEV_TYPE_ARM_VGIC_V3 ) { +switch (dist->vgic_model) { +case KVM_DEV_TYPE_ARM_VGIC_V2 : + dist->vgic_cpu_base =VGIC_ADDR_UNDEF ; +break ; +case KVM_DEV_TYPE_ARM_VGIC_V3 :list_for_each_entry_safe (rdreg, next, &dist->rd_regions, list)vgic_v3_free_redist_region (kvm, rdreg);INIT_LIST_HEAD (&dist->rd_regions); - }else { - dist->vgic_cpu_base =VGIC_ADDR_UNDEF ; - } -if (vgic_supports_direct_irqs (kvm)) -vgic_v4_teardown (kvm); +if (vgic_supports_direct_irqs (kvm)) +vgic_v4_teardown (kvm); +break ; +case KVM_DEV_TYPE_ARM_VGIC_V5 : +vgic_v5_teardown (kvm); +break ; + }xa_destroy (&dist->lpi_xa); }
arch/arm64/kvm/vgic/vgic-v5.c +144 -0
172 lines not on this page · raw message
arch/arm64/kvm/vgic/vgic.h +1 -0
@@ -366,6 +366,7 @@ void vgic_debug_destroy(struct kvm *kvm);int vgic_v5_probe (const struct gic_kvm_info *info);void vgic_v5_reset (struct kvm_vcpu *vcpu);int vgic_v5_init (struct kvm *kvm); +void vgic_v5_teardown (struct kvm *kvm);int vgic_v5_map_resources (struct kvm *kvm);void vgic_v5_set_ppi_ops (struct kvm_vcpu *vcpu,u32 vintid);bool vgic_v5_has_pending_ppi (struct kvm_vcpu *vcpu);
include/kvm/arm_vgic.h +4 -0
@@ -379,6 +379,10 @@ struct vgic_v5_vm {* convenient way to do that). */ DECLARE_BITMAP (vgic_ppi_hmr,VGIC_V5_NR_PRIVATE_IRQS ); + +struct fwnode_handle *fwnode; +struct irq_domain *domain; +int vpe_db_base; };struct vgic_dist {
include/linux/irqchip/arm-gic-v5.h +2 -0
@@ -613,6 +613,8 @@ void gicv5_irs_syncr(void);/* Embedded in kvm.arch */ struct gicv5_vpe { +int db; +bool db_fired;bool resident; };
Signature · 1 line
2.34.1
[email protected]
AI reviewRe: [PATCH v5 08/49] KVM: arm64: gic-v5: Add VPE doorbell domain
Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider: - [High] Resource leak of GICv5 VPE doorbell domain and fwnode on VGIC initialization retry. - [High]…
Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider:
- [High] Resource leak of GICv5 VPE doorbell domain and fwnode on VGIC initialization retry.
- [High] TOCTOU race between KVM_CREATE_VCPU and KVM_DEV_ARM_VGIC_CTRL_INIT leads to undersized doorbell domain allocation.
- [Medium] Fwnode ID collision for VMs created by the same userspace thread.
- [Low] Unreachable dead code in doorbell domain error handling path.
--
--- Patch [8]: [PATCH v5 08/49] KVM: arm64: gic-v5: Add VPE doorbell domain --- commit 087989feef3777beedf75a01512da5b9e331b6fa
Author: Sascha Bischoff <[email protected]>
KVM: arm64: gic-v5: Add VPE doorbell domain
This commit adds support for a per-VM VPE doorbell irq domain for GICv5. It creates the domain under the GICv5 LPI domain with one doorbell allocated per VPE, and stores the state for later IRS commands and wakeups.
arch/arm64/kvm/vgic/vgic-v5.c +0 -0
[ ... ]
Review
Quoted 4 lines
(quoted diff) +4
-0+static int vgic_v5_create_per_vm_domain(struct kvm *kvm) +{ + struct vgic_v5_vm *vm = &kvm->arch.vgic.gicv5_vm; + int nr_vcpus = atomic_read(&kvm->online_vcpus);
[Severity: High]
Can a race condition here lead to an undersized doorbell domain allocation?
If a userspace thread calls the initialization ioctl, it checks for concurrent VCPU creation, but this check is performed locklessly with respect to the kvm->lock held during VCPU creation.
Concurrently, a second thread could create a VCPU, which takes kvm->lock and increments created_vcpus. Since initialization hasn't completed yet, VCPU creation is allowed to proceed.
When the first thread reaches this point, it allocates exactly the number of online VCPUs at this moment. As the second thread completes and increments online_vcpus, the VM now has more VCPUs than allocated doorbell IRQs, which can lead to out-of-bounds IRQ array access later.
Review
Quoted 9 lines
(quoted diff) +9
-0+ int id = task_pid_nr(current); + int ret, db_virq = 0; + + if (!gicv5_global_data.lpi_domain) { + kvm_err("LPI domain uninitialized, can't set up KVM Doorbells\n"); + return -ENODEV; + } + + vm->fwnode = irq_domain_alloc_named_id_fwnode("GICv5-vpe-db", id);
[Severity: High]
Can this leak the GICv5 VPE doorbell domain and fwnode if initialization is retried?
If the initialization fails later in the setup process (for example, if kvm_vgic_setup_default_irq_routing() fails in vgic_init()), the VGIC state remains uninitialized.
If userspace then retries the initialization, this function is called again and unconditionally allocates a new fwnode and IRQ domain, overwriting the previous pointers without freeing them.
[Severity: Medium]
Could using the thread ID here cause fwnode naming collisions?
Since task_pid_nr(current) is used as the ID for the fwnode name, a single VMM thread sequentially creating multiple KVM VMs will cause them to share the exact same fwnode name.
When the IRQ domain is registered, this duplicate name causes debugfs directory creation failures due to the directory already being present.
Would it be more reliable to use the VM's unique vm_id instead?
Quoted 2 lines
(quoted diff) +2
-0+ if (!vm->fwnode) + return -ENOMEM;
[ ... ]
Review
Quoted 12 lines
(quoted diff) +12
-0+ db_virq = irq_domain_alloc_irqs(vm->domain, nr_vcpus, NUMA_NO_NODE, vm); + if (db_virq <= 0) { + ret = db_virq; + goto err; + } + + kvm->arch.vgic.gicv5_vm.vpe_db_base = db_virq; + + return 0; + +err: + if (db_virq > 0)
[Severity: Low]
Is this error cleanup path unreachable?
The only branch that jumps to the err label after modifying db_virq explicitly checks that db_virq is less than or equal to 0.
Because db_virq can never be strictly positive at this label, the irq_domain_free_irqs() block will never be executed.
Quoted 3 lines
(quoted diff) +3
-0+ irq_domain_free_irqs(db_virq, nr_vcpus); + if (vm->domain) + irq_domain_remove(vm->domain);
[ ... ]
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=8
Re: [PATCH v5 08/49] KVM: arm64: gic-v5: Add VPE doorbell domain
Quoted 51 lines · On Fri, 2026-08-07 at 12:45 +0000, [email protected] wrote:
Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider:
- [High] Resource leak of GICv5 VPE doorbell domain and fwnode on VGIC initialization retry.
- [High] TOCTOU race betweenKVM_CREATE_VCPUandKVM_DEV_ARM_VGIC_CTRL_INITleads to undersized doorbell domain allocation.
- [Medium] Fwnode ID collision for VMs created by the same userspace thread.
- [Low] Unreachable dead code in doorbell domain error handling path. ----- Patch [8]: [PATCH v5 08/49] KVM: arm64: gic-v5: Add VPE doorbell domain ---
commit087989feef3777beedf75a01512da5b9e331b6fa
Author: Sascha Bischoff <[email protected]>
KVM: arm64: gic-v5: Add VPE doorbell domainThis commit adds support for a per-VM VPE doorbell irq domain for GICv5.
It creates the domain under the GICv5 LPI domain with one doorbell allocated
per VPE, and stores the state for later IRS commands and wakeups.arch/arm64/kvm/vgic/vgic-v5.c +0
-0Review
Quoted 4 lines
(quoted diff) +4
-0+static int vgic_v5_create_per_vm_domain(struct kvm *kvm) +{ + struct vgic_v5_vm *vm = &kvm->arch.vgic.gicv5_vm; + int nr_vcpus = atomic_read(&kvm->online_vcpus);[Severity: High]
Can a race condition here lead to an undersized doorbell domain allocation?If a userspace thread calls the initialization ioctl, it checks for concurrent
VCPU creation, but this check is performed locklessly with respect to thekvm->lockheld during VCPU creation.Concurrently, a second thread could create a VCPU, which takes kvm-
Quoted 1 line
lock and
increments
created_vcpus. Since initialization hasn't completed yet, VCPU
creation is allowed to proceed.
Quoted 7 lines
When the first thread reaches this point, it allocates exactly the number
of online VCPUs at this moment. As the second thread completes and incrementsonline_vcpus, the VM now has more VCPUs than allocated doorbell IRQs, which
can lead to out-of-bounds IRQ array access later.
Agreed, I think that this is an issue. The existing check only detects vCPU creation that is already in progress. Since CTRL_INIT does not hold kvm->lock, another creation can start afterwards and leave the per-vCPU resources, including the doorbell domain, undersized. FWIW, I think this affects GICv4, too.
I will post a separate fixup to serialise CTRL_INIT against vCPU creation.
Quoted 24 lines
Quoted 11 lines
(quoted diff) +5
-0+ int id = task_pid_nr(current); + int ret, db_virq = 0; + + if (!gicv5_global_data.lpi_domain) { + kvm_err("LPI domain uninitialized, can't set upKVM Doorbells\n");
(quoted diff) +4
-0+ return -ENODEV; + } + + vm->fwnode = irq_domain_alloc_named_id_fwnode("GICv5-vpe-Quoted 1 line
db", id);
[Severity: High]
Can this leak the GICv5 VPE doorbell domain and fwnode if initialization
is retried?If the initialization fails later in the setup process (for example, if
kvm_vgic_setup_default_irq_routing()fails invgic_init()), the VGIC state
remains uninitialized.
Quoted 5 lines
If userspace then retries the initialization, this function is called again
and unconditionally allocates a new fwnode and IRQ domain, overwriting
the previous pointers without freeing them.
I've updated this commit to call vgic_v5_teardown() on subsequent failure. This way no state is leaked.
Quoted 11 lines
[Severity: Medium]
Could using the thread ID here cause fwnode naming collisions?Since
task_pid_nr(current)is used as the ID for the fwnode name, a single
VMM thread sequentially creating multiple KVM VMs will cause them to share
the exact same fwnode name.
Quoted 5 lines
When the IRQ domain is registered, this duplicate name causes debugfs directory creation failures due to the directory already being present.
Would it be more reliable to use the VM's unique
vm_idinstead?
The current code matches what GICv4 does for the fwnode. In my knowledge, it isn't a common thing for a single thread to create multiple VMs. If this is indeed an issue, it can be revisited and fixed, but the for the time being I suggest leaving this as is.
Quoted 21 lines
Quoted 3 lines
(quoted diff) +2
-0+ if (!vm->fwnode) + return -ENOMEM;[ ... ]
Review
Quoted 13 lines
(quoted diff) +1
-0+ db_virq = irq_domain_alloc_irqs(vm->domain, nr_vcpus,
NUMA_NO_NODE, vm);(quoted diff) +11
-0+ if (db_virq <= 0) { + ret = db_virq; + goto err; + } + + kvm->arch.vgic.gicv5_vm.vpe_db_base = db_virq; + + return 0; + +err: + if (db_virq > 0)[Severity: Low]
Is this error cleanup path unreachable?
Quoted 5 lines
The only branch that jumps to the err label after modifying
db_virqexplicitly checks thatdb_virqis less than or equal to 0.Because
db_virqcan never be strictly positive at this label, theirq_domain_free_irqs()block will never be executed.
True. Dropped the unreachable code.
Quoted 6 lines
Quoted 4 lines
(quoted diff) +3
-0+ irq_domain_free_irqs(db_virq, nr_vcpus); + if (vm->domain) + irq_domain_remove(vm->domain);[ ... ]
Thanks,
Sascha
PATCH v5 9/49 KVM: arm64: gic-v5: Create and manage VM and VPE tables
In reply to Sascha BischoffGICv5 uses a set of in-memory tables to track and manage VM state. These must be allocated by the hypervisor and provided to the IRS.
The VMT (Virtual Machine Table) is a linear or two-level table comprising VMT Entries (VMTEs). Each VMTE describes the state for a single VM. This state includes things such as the SPI and LPI IST configuration (coming in a future commit), an implementation-defined VM Descriptor, and a VPE Table (VPET).
The VPET contains one entry per possible VPE ID belonging to a VM. It is used to mark a VPE as valid and provide the address of an implementation-defined VPE Descriptor (VPED), which the hardware uses to track and manage VPE state.
Allocate each VM's VPEDs as a single dense array indexed by vcpu_idx, while the VPET remains indexed by the userspace-provided vcpu_id. This keeps VPED storage proportional to the number of vCPUs even when their IDs are sparse.
The VMT and VPET are shared with the IRS. On systems with a non-coherent IRS, cache maintenance operates at cache-line granularity, while multiple entries can occupy the same cache line. Use a common lock for CPU accesses to these tables and IRS command processing so that writing back one entry cannot overwrite an IRS update to a neighbouring entry.
The implementation-defined VMD and VPED storage is also visible to the IRS. Round these allocations up to whole cache lines to prevent cache maintenance from corrupting unrelated slab objects. Initialise the storage before publishing its addresses to the IRS.
This commit adds support for allocating the VMT and its descriptor backing state, and for managing VMTEs. The VMTEs can be initialised or released for reuse. VM IDs are allocated with an IDA, while an XArray tracks the host-side allocations associated with populated VMTEs.
--- arch/arm64/kvm/Makefile | 2 +- arch/arm64/kvm/vgic/vgic-init.c | 2 + arch/arm64/kvm/vgic/vgic-v5-tables.c | 681 +++++++++++++++++++++++++++ arch/arm64/kvm/vgic/vgic-v5-tables.h | 100 ++++ arch/arm64/kvm/vgic/vgic-v5.c | 19 + drivers/irqchip/irq-gic-v5-irs.c | 12 +- include/kvm/arm_vgic.h | 4 + include/linux/irqchip/arm-gic-v5.h | 14 +- 8 files changed, 824 insertions(+), 10 deletions(-) create mode 100644 arch/arm64/kvm/vgic/vgic-v5-tables.c create mode 100644 arch/arm64/kvm/vgic/vgic-v5-tables.h
arch/arm64/kvm/Makefile +1 -1
@@ -24,7 +24,7 @@ kvm-y += arm.o mmu.o mmio.o psci.o hypercalls.o pvtime.o \ vgic/vgic-mmio.o vgic/vgic-mmio-v2.o \ vgic/vgic-mmio-v3.o vgic/vgic-kvm-device.o \ vgic/vgic-its.o vgic/vgic-debug.o vgic/vgic-v3-nested.o \ - vgic/vgic-v5.o + vgic/vgic-v5.o vgic/vgic-v5-tables.o kvm-$(CONFIG_HW_PERF_EVENTS) += pmu-emul.o pmu.o kvm-$(CONFIG_ARM64_PTR_AUTH) += pauth.o
arch/arm64/kvm/vgic/vgic-init.c +2 -0
@@ -154,6 +154,8 @@ int kvm_vgic_create(struct kvm *kvm, u32 type)case KVM_DEV_TYPE_ARM_VGIC_V3 :INIT_LIST_HEAD (&kvm->arch.vgic.rd_regions);break ; +case KVM_DEV_TYPE_ARM_VGIC_V5 : + kvm->arch.vgic.gicv5_vm.vm_id =VGIC_V5_VM_ID_INVAL ; }/*
arch/arm64/kvm/vgic/vgic-v5-tables.c new file +681 -0
681 lines not on this page · raw message
arch/arm64/kvm/vgic/vgic-v5-tables.h new file +100 -0
100 lines not on this page · raw message
arch/arm64/kvm/vgic/vgic-v5.c +19 -0
@@ -5,10 +5,12 @@#include <kvm/arm_vgic.h> +#include <linux/kvm_host.h> #include <linux/bitops.h> #include <linux/irqchip/arm-vgic-info.h> #include <linux/irqdomain.h> +#include "vgic-v5-tables.h" #include "vgic.h" #define ppi_caps kvm_vgic_global_state.vgic_v5_ppi_caps
@@ -129,6 +131,22 @@ int vgic_v5_probe(const struct gic_kvm_info *info)return 0 ; } +static int vgic_v5_db_set_vcpu_affinity (struct irq_data *data,void *vcpu_info) +{ +enum gicv5_vcpu_cmd *cmd = vcpu_info; + +guard (raw_spinlock_irqsave)(&vgic_v5_irs_lock); + +switch (*cmd) { +case VMT_L2_MAP : +case VMTE_MAKE_VALID : +case VMTE_MAKE_INVALID : +/* Not yet implemented */ +default : +return -EINVAL ; + } +} +/* * This set of irq_chip functions is specific for doorbells. */
@@ -140,6 +158,7 @@ static const struct irq_chip vgic_v5_db_irq_chip = { .irq_set_affinity = irq_chip_set_affinity_parent, .irq_get_irqchip_state = irq_chip_get_parent_state, .irq_set_irqchip_state = irq_chip_set_parent_state, + .irq_set_vcpu_affinity = vgic_v5_db_set_vcpu_affinity, .flags = IRQCHIP_SET_TYPE_MASKED | IRQCHIP_SKIP_SET_WAKE | IRQCHIP_MASK_ON_SUSPEND, };
drivers/irqchip/irq-gic-v5-irs.c +6 -6
@@ -269,24 +269,24 @@ int gicv5_irs_iste_alloc(const u32 lpi)* itself is not supported) again serves to make it easier to find physically * contiguous blocks of memory. */ -static unsigned int gicv5_irs_l2_sz (u32 idr2) +unsigned int gicv5_irs_l2_sz (u32 l2sz) {switch (PAGE_SIZE ) {case SZ_64K : -if (GICV5_IRS_IST_L2SZ_SUPPORT_64KB (idr2)) +if (GICV5_IRS_IST_L2SZ_SUPPORT_64KB (l2sz))return GICV5_IRS_IST_CFGR_L2SZ_64K ; fallthrough;case SZ_4K : -if (GICV5_IRS_IST_L2SZ_SUPPORT_4KB (idr2)) +if (GICV5_IRS_IST_L2SZ_SUPPORT_4KB (l2sz))return GICV5_IRS_IST_CFGR_L2SZ_4K ; fallthrough;case SZ_16K : -if (GICV5_IRS_IST_L2SZ_SUPPORT_16KB (idr2)) +if (GICV5_IRS_IST_L2SZ_SUPPORT_16KB (l2sz))return GICV5_IRS_IST_CFGR_L2SZ_16K ;break ; } -if (GICV5_IRS_IST_L2SZ_SUPPORT_4KB (idr2)) +if (GICV5_IRS_IST_L2SZ_SUPPORT_4KB (l2sz))return GICV5_IRS_IST_CFGR_L2SZ_4K ;return GICV5_IRS_IST_CFGR_L2SZ_64K ;
@@ -334,7 +334,7 @@ static int __init gicv5_irs_init_ist(struct gicv5_irs_chip_data *irs_data) lpi_id_bits =min (lpi_id_bits, gicv5_global_data.cpuif_id_bits);if (two_levels) - l2sz =gicv5_irs_l2_sz (idr2); + l2sz =gicv5_irs_l2_sz (FIELD_GET (GICV5_IRS_IDR2_IST_L2SZ , idr2)); istmd = !!FIELD_GET (GICV5_IRS_IDR2_ISTMD , idr2);
include/kvm/arm_vgic.h +4 -0
@@ -361,6 +361,8 @@ struct vgic_redist_region {struct list_head list; }; +#define VGIC_V5_VM_ID_INVAL (-1) +struct vgic_v5_vm {/* * We only expose a subset of PPIs to the guest. This subset is a
@@ -383,6 +385,8 @@ struct vgic_v5_vm {struct fwnode_handle *fwnode;struct irq_domain *domain;int vpe_db_base; +u32 vm_id; +bool vmte_allocated; };struct vgic_dist {
include/linux/irqchip/arm-gic-v5.h +11 -3
@@ -159,9 +159,9 @@#define GICV5_IRS_IDR2_LPI BIT(5)#define GICV5_IRS_IDR2_ID_BITS GENMASK(4, 0) -#define GICV5_IRS_IST_L2SZ_SUPPORT_4KB (r) FIELD_GET(BIT(11), (r)) -#define GICV5_IRS_IST_L2SZ_SUPPORT_16KB (r) FIELD_GET(BIT(12), (r)) -#define GICV5_IRS_IST_L2SZ_SUPPORT_64KB (r) FIELD_GET(BIT(13), (r)) +#define GICV5_IRS_IST_L2SZ_SUPPORT_4KB (r) FIELD_GET(BIT(0), (r)) +#define GICV5_IRS_IST_L2SZ_SUPPORT_16KB (r) FIELD_GET(BIT(1), (r)) +#define GICV5_IRS_IST_L2SZ_SUPPORT_64KB (r) FIELD_GET(BIT(2), (r))#define GICV5_IRS_IDR3_VMT_LEVELS BIT(10)#define GICV5_IRS_IDR3_VM_ID_BITS GENMASK(9, 5)
@@ -609,6 +609,7 @@ int gicv5_irs_cpu_to_iaffid(int cpu_id, u16 *iaffid);struct gicv5_irs_chip_data *gicv5_irs_lookup_by_spi_id (u32 spi_id);int gicv5_spi_irq_set_type (struct irq_data *d,unsigned int type);int gicv5_irs_iste_alloc (u32 lpi); +unsigned int gicv5_irs_l2_sz (u32 l2sz);void gicv5_irs_syncr (void );/* Embedded in kvm.arch */
@@ -653,4 +654,11 @@ void gicv5_deinit_lpis(void);void __init gicv5_its_of_probe (struct device_node *parent);void __init gicv5_its_acpi_probe (void ); + +enum gicv5_vcpu_cmd { +VMT_L2_MAP ,/* Map in a L2 VMT - *may* happen on VM init */ +VMTE_MAKE_VALID ,/* Make the VMTE valid */ +VMTE_MAKE_INVALID ,/* Make the VMTE (et al.) invalid */ +}; +#endif
Signature · 1 line
2.34.1
[email protected]
AI reviewRe: [PATCH v5 09/49] KVM: arm64: gic-v5: Create and manage VM and VPE tables
Thank you for your contribution! Sashiko AI review found 6 potential issue(s) to consider: - [Critical] Initializing the VGICv5 with 0 vCPUs triggers a NULL pointer dereference and a cache…
Thank you for your contribution! Sashiko AI review found 6 potential issue(s) to consider:
- [Critical] Initializing the VGICv5 with 0 vCPUs triggers a NULL pointer dereference and a cache maintenance fault on ZERO_SIZE_PTR.
- [High] VMT tables and associated structures allocated via kzalloc lack required cacheline and hardware alignment, risking memory corruption and address truncation.
- [High] vgic_v5_db_set_vcpu_affinity intercepts and drops all IRS commands, breaking L2 VMT allocation.
- [High] VPET and L1 VMT entries are populated in memory without setting their architecturally required Valid bits.
- [High] The VM info structure (vmi) is inserted into the global vm_info XArray before it is fully initialized, creating a TOCTOU race.
- [High] Lockless XArray lookups of vmi lack rcu_read_lock() protection and race with synchronous kfree().
--
--- Patch [9]: [PATCH v5 09/49] KVM: arm64: gic-v5: Create and manage VM and VPE tables --- commit dc3f99568625ef5a0f577860cb4099269b170ef2
Author: Sascha Bischoff <[email protected]> Date: Fri Aug 7 11:17:11 2026 +0000
KVM: arm64: gic-v5: Create and manage VM and VPE tables
GICv5 uses a set of in-memory tables to track and manage VM state. These must be allocated by the hypervisor and provided to the IRS.
Quoted 5 lines
arch/arm64/kvm/vgic/vgic-v5-tables.c new file +0
-0
[ ... ]
arch/arm64/kvm/vgic/vgic-v5-tables.c line 84
Quoted 9 lines
arch/arm64/kvm/vgic/vgic-v5-tables.c new file +9
-0+static void vgic_v5_clean_inval (void *va,size_t size) +{ +unsigned long base = (unsigned long )va; + +dsb (ishst); + +if (kvm_vgic_global_state.vgic_v5_irs_caps.non_coherent) +dcache_clean_inval_poc (base, base + size); +}
[Severity: High]
Does dcache_clean_inval_poc() operate on cacheline-aligned base pointers here?
While vpet and vped_base allocations round their size up to dma_get_cache_alignment(), kzalloc() does not guarantee that the base pointer itself will be cacheline-aligned.
Performing cacheline-granular invalidation on unaligned buffers evicts shared cachelines, which could corrupt unrelated slab objects sharing the cacheline.
[ ... ]
Quoted 20 lines
arch/arm64/kvm/vgic/vgic-v5-tables.c new file +20
-0+static int vgic_v5_alloc_l2_vmt (struct kvm *kvm) +{ +struct kvm_vcpu *vcpu0 =kvm_get_vcpu (kvm,0 ); +u32 vm_id =vgic_v5_vm_id (kvm); +enum gicv5_vcpu_cmd cmd =VMT_L2_MAP ; +struct vmtl2_entry *l2_table; +unsigned int l1_index; +int ret; + +/* Nothing to do if we have linear tables! */ +if (!vmt_info->two_level) +return 0 ; + +if (vm_id ==VGIC_V5_VM_ID_INVAL ) +return -EINVAL ; + +/* +* We have 4k-sized L2 tables - this is mandated by the spec for +* two-level VMTs (GICv5 EAC0 Specification rule D_LSPBK). This means +* that we have 128 entries per L1 VMTE.
arch/arm64/kvm/vgic/vgic-v5-tables.c line 174
Quoted 10 lines
arch/arm64/kvm/vgic/vgic-v5-tables.c new file +10
-0+*/ + l1_index = vm_id /GICV5_VMT_L2_TABLE_ENTRIES ; + +guard (mutex)(&vmt_l2_lock); + +/* Already valid? Great! */ +if (vmt_info->l2.l2ptrs[l1_index]) +return 0 ; + + l2_table =kzalloc_objs (*l2_table,GICV5_VMT_L2_TABLE_ENTRIES );
[Severity: High]
Does the L2 table allocation meet the hardware's 4K alignment requirement?
l2_table is allocated using kzalloc_objs(). Hardware extracts the L2 physical address using GICV5_VMTEL1E_L2_ADDR (bits 51:12), which requires 4K alignment.
SLUB does not guarantee 4K base alignment for kmalloc allocations, which could cause silent address truncation when the physical address is written to the L1 entry.
arch/arm64/kvm/vgic/vgic-v5-tables.c line 185
Quoted 11 lines
arch/arm64/kvm/vgic/vgic-v5-tables.c new file +11
-0+if (!l2_table) +return -ENOMEM ; + +/* The VMT is shared between all VMs. */ + scoped_guard(raw_spinlock_irqsave, &vgic_v5_irs_lock) { +vgic_v5_clean_inval (l2_table,GICV5_VMT_L2_TABLE_SIZE ); +vgic_v5_clean_inval (vmt_info->l2.vmt_base + l1_index, +sizeof (vmtl1_entry)); + +WRITE_ONCE (vmt_info->l2.vmt_base[l1_index], +cpu_to_le64 (virt_to_phys (l2_table)));
[Severity: High]
Is the GICV5_VMTEL1E_VALID bit missing here?
The L1 VMT entry is populated with the physical address of the L2 table, but it doesn't look like the GICV5_VMTEL1E_VALID bit is ever set, which might cause the hardware to treat the table as invalid.
arch/arm64/kvm/vgic/vgic-v5-tables.c line 197
Quoted 12 lines
arch/arm64/kvm/vgic/vgic-v5-tables.c new file +12
-0+ +vgic_v5_clean_inval (vmt_info->l2.vmt_base + l1_index, +sizeof (vmtl1_entry)); + + } + +/* +* VMAP in the L2 VMT via the IRS. We use any of the VM's CPUs as a +* conduit for interacting with the host's IRS. In the current case, +* this lets us resolve the VM ID to pass to the hardware. +*/ + ret =irq_set_vcpu_affinity (vgic_v5_vpe_db (vcpu0), &cmd);
[Severity: Critical]
What happens if this is called when no vCPUs have been created yet?
If atomic_read(&kvm->online_vcpus) is 0 during vgic_v5_vmte_init(), kvm_get_vcpu(kvm, 0) returns NULL, which would cause a NULL pointer dereference when it is passed to vgic_v5_vpe_db() here.
[ ... ]
Quoted 39 lines
arch/arm64/kvm/vgic/vgic-v5-tables.c new file +39
-0+int vgic_v5_vmte_init (struct kvm *kvm) +{ +size_t vmd_alloc_size, vpet_alloc_size, vped_alloc_size; +void *vped_base =NULL , *vmd =NULL ; +struct vgic_v5_vm_info *vmi =NULL ; +u64 tmp, vmte_val0 =0 , vmte_val1; +u32 vm_id =vgic_v5_vm_id (kvm); +int ret, nr_cpus, nr_vcpus; +bool vmi_inserted = false; +struct vmtl2_entry *vmte; +vpe_entry *vpet =NULL ; +struct kvm_vcpu *vcpu; +u16 max_vpe_id =0 ; +unsigned long i; + + nr_vcpus =atomic_read (&kvm->online_vcpus); +if (nr_vcpus > vmt_info->max_vpes) +return -E2BIG ; + +/* +* If we're using two-level VMTs, L2 is allocated on demand. For linear +* VMTs, this is a NOP. +*/ + ret =vgic_v5_alloc_l2_vmt (kvm); +if (ret) +return ret; + + vmte =vgic_v5_get_l2_vmte (vm_id); +if (IS_ERR (vmte)) +return PTR_ERR (vmte); + +/* If the entry is already valid, something went wrong */ + scoped_guard(raw_spinlock_irqsave, &vgic_v5_irs_lock) { +vgic_v5_clean_inval (vmte,sizeof (*vmte)); +if (le64_to_cpu (READ_ONCE (vmte->val[0 ])) &GICV5_VMTEL2E_VALID ) +return -EINVAL ; + } + + ret =vgic_v5_reset_vmte (kvm);
arch/arm64/kvm/vgic/vgic-v5-tables.c line 440
Quoted 10 lines
arch/arm64/kvm/vgic/vgic-v5-tables.c new file +10
-0+if (ret) +return ret; + + vmi =kzalloc_obj (*vmi); +if (!vmi) { + ret = -ENOMEM ; + goto out_fail; + } + + ret =xa_insert (&vm_info, vm_id, vmi,GFP_KERNEL );
[Severity: High]
Is it safe to publish vmi before its fields are initialized?
vmi is published to the global vm_info XArray here, but sleepable allocations for vmi->vmd_base, vmi->vpet_base, and vmi->vped_base occur after this point.
Could a concurrent thread executing vgic_v5_vmte_alloc_vpe() retrieve the partially constructed vmi and dereference its NULL pointers?
Quoted 69 lines
arch/arm64/kvm/vgic/vgic-v5-tables.c new file +69
-0+if (ret) + goto out_fail; + vmi_inserted = true; + +/* Allocate and assign the VM Descriptor, if required. */ +if (vmt_info->vmd_size !=0 ) { + vmd_alloc_size =round_up (vmt_info->vmd_size, +dma_get_cache_alignment ()); + vmd =kzalloc (vmd_alloc_size,GFP_KERNEL ); +if (!vmd) { + ret = -ENOMEM ; + goto out_fail; + } + +/* Stash the VA so we can free it later */ + vmi->vmd_base = vmd; + + tmp =FIELD_PREP (GICV5_VMTEL2E_VMD_ADDR , +virt_to_phys (vmd) >>GICV5_VMTEL2E_VMD_ADDR_SHIFT ); + vmte_val0 = tmp; + } + +/* +* Allocate and assign the VPE Table. +* +* First of all, iterate over all vcpus to find the highest VPE ID we +* require - we need to ensure that we have enough storage for all +* vcpu_id values that userspace has picked and not just the total +* number of vcpus. This gives us the number of VPEs required for the +* VM. +* +* Round up the number of VPEs to a whole power of two as we cannot +* describe non-powers-of-two in the VMTE field as it conveys the number +* of ID bits used and not the number of vPEs. IRS_IDR1.IAFFID_BITS is +* encoded as N - 1, so expose at least one VPE ID bit even for a +* single-vCPU VM to keep the views consistent. +*/ +kvm_for_each_vcpu (i, vcpu, kvm) { +u16 vpe_id =vgic_v5_vpe_id (vcpu); + +if (vpe_id > max_vpe_id) + max_vpe_id = vpe_id; + } + + nr_cpus =max (2UL ,roundup_pow_of_two (max_vpe_id +1 )); + vmi->vpe_id_bits =fls (nr_cpus) -1 ; + + vpet_alloc_size =round_up ((size_t )nr_cpus *sizeof (*vpet), +dma_get_cache_alignment ()); + vpet =kzalloc (vpet_alloc_size,GFP_KERNEL ); +if (!vpet) { + ret = -ENOMEM ; + goto out_fail; + } + +/* Stash the VA so we can free it later */ + vmi->vpet_base = vpet; + + tmp =FIELD_PREP (GICV5_VMTEL2E_VPET_ADDR , +virt_to_phys (vpet) >>GICV5_VMTEL2E_VPET_ADDR_SHIFT ); + tmp |=FIELD_PREP (GICV5_VMTEL2E_VPE_ID_BITS , vmi->vpe_id_bits); + vmte_val1 = tmp; + +/* +* Allocate a dense VPED array indexed by vcpu_idx. The VPET is indexed +* by the potentially sparse vcpu_id, but using that ID here would waste +* memory. Given that this is not userspace visible, we can cheat a bit +* and use the dense index instead. This is the ONLY place that we do +* this.
arch/arm64/kvm/vgic/vgic-v5-tables.c line 519
Quoted 10 lines
arch/arm64/kvm/vgic/vgic-v5-tables.c new file +10
-0+* +* Round the requested size up to a whole cacheline. kzalloc() gurantees +* natural alignment, so we ensure that the cachelines cannot be shared +* with unrelated slab objects. VPED and cacheline sizes are powers of +* two, so this also preserves the required VPED alignment when a VPED +* is larger than a cacheline. +*/ + vped_alloc_size =round_up ((size_t )nr_vcpus * vmt_info->vped_size, +dma_get_cache_alignment ()); + vped_base =kzalloc (vped_alloc_size,GFP_KERNEL );
[Severity: Critical]
Can nr_vcpus be 0 here?
If no vCPUs are online, vped_alloc_size evaluates to 0. Calling kzalloc(0, GFP_KERNEL) returns the ZERO_SIZE_PTR (0x10).
arch/arm64/kvm/vgic/vgic-v5-tables.c line 529
Quoted 10 lines
arch/arm64/kvm/vgic/vgic-v5-tables.c new file +10
-0+if (!vped_base) { + ret = -ENOMEM ; + goto out_fail; + } + vmi->vped_base = vped_base; + +if (vmd) +vgic_v5_clean_inval (vmd, vmd_alloc_size); +vgic_v5_clean_inval (vpet, vpet_alloc_size); +vgic_v5_clean_inval (vped_base, vped_alloc_size);
Passing the ZERO_SIZE_PTR to vgic_v5_clean_inval() will execute a cache invalidation on address 0x10, which would cause a fatal translation fault and crash the kernel.
[ ... ]
Quoted 18 lines
arch/arm64/kvm/vgic/vgic-v5-tables.c new file +18
-0+int vgic_v5_vmte_release (struct kvm *kvm) +{ +u32 vm_id =vgic_v5_vm_id (kvm); +struct vgic_v5_vm_info *vmi; +struct vmtl2_entry *vmte; +int ret; + + vmte =vgic_v5_get_l2_vmte (vm_id); +if (IS_ERR (vmte)) +return PTR_ERR (vmte); + +/* Reject if the VMTE has not been marked as invalid! */ + scoped_guard(raw_spinlock_irqsave, &vgic_v5_irs_lock) { +vgic_v5_clean_inval (vmte,sizeof (*vmte)); +if (le64_to_cpu (READ_ONCE (vmte->val[0 ])) &GICV5_VMTEL2E_VALID ) +return -EINVAL ; + } +
arch/arm64/kvm/vgic/vgic-v5-tables.c line 589
Quoted 10 lines
arch/arm64/kvm/vgic/vgic-v5-tables.c new file +10
-0+ vmi =xa_load (&vm_info, vm_id); +if (!vmi) + goto no_vmi; + +kfree (vmi->vped_base); +kfree (vmi->vpet_base); +kfree (vmi->vmd_base); + +xa_erase (&vm_info, vm_id); +kfree (vmi);
[Severity: High]
Could this synchronously free a vmi object that is currently in use?
This path synchronously calls kfree(vmi) after erasing it from the XArray, but readers like vgic_v5_vmte_alloc_vpe() load from the XArray without RCU protection.
Will this cause a use-after-free for any lockless readers that have preempted?
[ ... ]
arch/arm64/kvm/vgic/vgic-v5-tables.c line 619
Quoted 13 lines
arch/arm64/kvm/vgic/vgic-v5-tables.c new file +13
-0+int vgic_v5_vmte_alloc_vpe (struct kvm_vcpu *vcpu) +{ +u32 vm_id =vgic_v5_vm_id (vcpu->kvm); +u16 vpe_id =vgic_v5_vpe_id (vcpu); +struct vgic_v5_vm_info *vmi; +vpe_entry tmp, *vpet_base; +void *vped; + +/* Make sure we're not over what the hardware supports */ +if (vpe_id >= vmt_info->max_vpes) +return -E2BIG ; + + vmi =xa_load (&vm_info, vm_id);
[Severity: High]
Does this xa_load() need rcu_read_lock() protection?
The vmi object is retrieved without RCU read-side protection, and vgic_v5_vmte_release() synchronously calls kfree(vmi).
Could a preempted reader here dereference a freed vmi object?
Quoted 12 lines
arch/arm64/kvm/vgic/vgic-v5-tables.c new file +12
-0+if (!vmi) +return -EINVAL ; + +if (vpe_id >=1 << vmi->vpe_id_bits) +return -E2BIG ; + + vpet_base = vmi->vpet_base; + +/* If the VPETE for this CPU is already valid we've gone wrong */ + scoped_guard(raw_spinlock_irqsave, &vgic_v5_irs_lock) { +vgic_v5_clean_inval (&vpet_base[vpe_id],sizeof (*vpet_base)); +if (le64_to_cpu (READ_ONCE (vpet_base[vpe_id])) &GICV5_VPE_VALID )
arch/arm64/kvm/vgic/vgic-v5-tables.c line 641
Quoted 10 lines
arch/arm64/kvm/vgic/vgic-v5-tables.c new file +10
-0+return -EBUSY ; + } + + vped = (u8 *)vmi->vped_base + + (size_t )vcpu->vcpu_idx * vmt_info->vped_size; + + tmp =FIELD_PREP (GICV5_VPED_ADDR ,virt_to_phys (vped) >>GICV5_VPED_ADDR_SHIFT ); + + scoped_guard(raw_spinlock_irqsave, &vgic_v5_irs_lock) { +WRITE_ONCE (vpet_base[vpe_id],cpu_to_le64 (tmp));
[Severity: High]
Is the GICV5_VPE_VALID bit missing here?
The VPET entry is written to memory, but the GICV5_VPE_VALID bit is not set, which might cause the hardware to treat the entry as invalid.
Quoted 10 lines
arch/arm64/kvm/vgic/vgic-v5-tables.c new file +5
-0+vgic_v5_clean_inval (vpet_base + vpe_id,sizeof (vpe_entry)); + } + +return 0 ; +}arch/arm64/kvm/vgic/vgic-v5.c +0
-0
[ ... ]
Quoted 5 lines
arch/arm64/kvm/vgic/vgic-v5.c +5
-0+static int vgic_v5_db_set_vcpu_affinity (struct irq_data *data,void *vcpu_info) +{ +enum gicv5_vcpu_cmd *cmd = vcpu_info; + +guard (raw_spinlock_irqsave)(&vgic_v5_irs_lock);
arch/arm64/kvm/vgic/vgic-v5.c int vgic_v5_probe(const struct gic_kvm_info *info) · line 148
Quoted 10 lines
arch/arm64/kvm/vgic/vgic-v5.c +10
-0+ +switch (*cmd) { +case VMT_L2_MAP : +case VMTE_MAKE_VALID : +case VMTE_MAKE_INVALID : +/* Not yet implemented */ +default : +return -EINVAL ; + } +}
[Severity: High]
Should this function forward valid commands to the parent irqchip?
By returning -EINVAL for commands like VMT_L2_MAP, it intercepts and drops them without calling irq_chip_set_vcpu_affinity_parent().
This causes the VMT_L2_MAP command issued by vgic_v5_alloc_l2_vmt() to always fail, preventing VMs from booting.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=9
Re: [PATCH v5 09/49] KVM: arm64: gic-v5: Create and manage VM and VPE tables
Quoted 57 lines · On Fri, 2026-08-07 at 12:50 +0000, [email protected] wrote:
Thank you for your contribution! Sashiko AI review found 6 potential issue(s) to consider:
- [Critical] Initializing the VGICv5 with 0 vCPUs triggers a NULL pointer dereference and a cache maintenance fault onZERO_SIZE_PTR.
- [High] VMT tables and associated structures allocated via kzalloc lack required cacheline and hardware alignment, risking memory corruption and address truncation.
- [High]vgic_v5_db_set_vcpu_affinityintercepts and drops all IRS commands, breaking L2 VMT allocation.
- [High] VPET and L1 VMT entries are populated in memory without setting their architecturally required Valid bits.
- [High] The VM info structure (vmi) is inserted into the globalvm_infoXArray before it is fully initialized, creating a TOCTOU race.
- [High] Lockless XArray lookups ofvmilackrcu_read_lock()protection and race with synchronouskfree().
----- Patch [9]: [PATCH v5 09/49] KVM: arm64: gic-v5: Create and manage VM and VPE tables ---
commitdc3f99568625ef5a0f577860cb4099269b170ef2
Author: Sascha Bischoff <[email protected]>
Date: Fri Aug 7 11:17:11 2026 +0000KVM: arm64: gic-v5: Create and manage VM and VPE tables
GICv5 uses a set of in-memory tables to track and manage VM state.
These must be allocated by the hypervisor and provided to the IRS.Quoted 6 lines
arch/arm64/kvm/vgic/vgic-v5-tables.c new file +0
-0[ ... ]
arch/arm64/kvm/vgic/vgic-v5-tables.c line 84
Quoted 9 lines
arch/arm64/kvm/vgic/vgic-v5-tables.c new file +9
-0+static void vgic_v5_clean_inval (void *va,size_t size) +{ +unsigned long base = (unsigned long )va; + +dsb (ishst); + +if (kvm_vgic_global_state.vgic_v5_irs_caps.non_coherent) +dcache_clean_inval_poc (base, base + size); +}[Severity: High]
Doesdcache_clean_inval_poc()operate on cacheline-aligned base pointers here?While vpet and
vped_baseallocations round their size up todma_get_cache_alignment(),kzalloc()does not guarantee that the base pointer
itself will be cacheline-aligned.
Quoted 4 lines
Performing cacheline-granular invalidation on unaligned buffers evicts shared
cachelines, which could corrupt unrelated slab objects sharing the cacheline.
This is not an issue. We're explicitly growing allocations to be at least one cache line, or they are already a multiple of whole cache lines which by definition means that they are cacheline-aligned due to the natural-alignment provided my the allocator.
Quoted 31 lines
[ ... ]
Quoted 29 lines
arch/arm64/kvm/vgic/vgic-v5-tables.c new file +18
-0+static int vgic_v5_alloc_l2_vmt (struct kvm *kvm) +{ +struct kvm_vcpu *vcpu0 =kvm_get_vcpu (kvm,0 ); +u32 vm_id =vgic_v5_vm_id (kvm); +enum gicv5_vcpu_cmd cmd =VMT_L2_MAP ; +struct vmtl2_entry *l2_table; +unsigned int l1_index; +int ret; + +/* Nothing to do if we have linear tables! */ +if (!vmt_info->two_level) +return 0 ; + +if (vm_id ==VGIC_V5_VM_ID_INVAL ) +return -EINVAL ; + +/* +* We have 4k-sized L2 tables - this is mandated by the spec for
arch/arm64/kvm/vgic/vgic-v5-tables.c new file +1
-0+* two-level VMTs (GICv5 EAC0 Specification rule D_LSPBK). This means
arch/arm64/kvm/vgic/vgic-v5-tables.c new file +8
-0+* that we have 128 entries per L1 VMTE. +*/ + l1_index = vm_id /GICV5_VMT_L2_TABLE_ENTRIES ; + +guard (mutex)(&vmt_l2_lock); + +/* Already valid? Great! */ +if (vmt_info->l2.l2ptrs[l1_index])
Quoted 8 lines
Quoted 4 lines
arch/arm64/kvm/vgic/vgic-v5-tables.c new file +3
-0+return 0 ; + + l2_table = kzalloc_objs(*l2_table,
GICV5_VMT_L2_TABLE_ENTRIES);[Severity: High]
Does the L2 table allocation meet the hardware's 4K alignment requirement?
Yes. The number of table entries in GICV5_VMT_L2_TABLE_ENTRIES is calculated by dividing 4k by the size of each entry.
Quoted 11 lines
l2_tableis allocated usingkzalloc_objs(). Hardware extracts the L2 physical
address usingGICV5_VMTEL1E_L2_ADDR(bits 51:12), which requires 4K alignment.SLUB does not guarantee 4K base alignment for kmalloc allocations, which could
cause silent address truncation when the physical address is written to the L1
entry.
This is not an issue.
Quoted 17 lines
Quoted 10 lines
arch/arm64/kvm/vgic/vgic-v5-tables.c new file +6
-0+if (!l2_table) +return -ENOMEM ; + +/* The VMT is shared between all VMs. */ + scoped_guard(raw_spinlock_irqsave, &vgic_v5_irs_lock) { + vgic_v5_clean_inval(l2_table,
GICV5_VMT_L2_TABLE_SIZE);arch/arm64/kvm/vgic/vgic-v5-tables.c new file +1
-0+ vgic_v5_clean_inval(vmt_info->l2.vmt_base +
l1_index,arch/arm64/kvm/vgic/vgic-v5-tables.c line 185
Quoted 4 lines
arch/arm64/kvm/vgic/vgic-v5-tables.c new file +4
-0+ sizeof(vmtl1_entry)); + +WRITE_ONCE (vmt_info->l2.vmt_base[l1_index], + cpu_to_le64(virt_to_phys(l2_table)));[Severity: High]
Is theGICV5_VMTEL1E_VALIDbit missing here?
No, it is not. This is set by the hardware.
Quoted 6 lines
The L1 VMT entry is populated with the physical address of the L2 table, but it
doesn't look like theGICV5_VMTEL1E_VALIDbit is ever set, which might cause
the hardware to treat the table as invalid.
As far as this part of the series is concerned, that's quite correct. Subsequent patches add the support for issuing the commands to the IRS that do exactly this.
Quoted 11 lines
Quoted 11 lines
arch/arm64/kvm/vgic/vgic-v5-tables.c new file +2
-0+ + vgic_v5_clean_inval(vmt_info->l2.vmt_base +
l1_index,arch/arm64/kvm/vgic/vgic-v5-tables.c new file +6
-0+ sizeof(vmtl1_entry)); + + } + +/* +* VMAP in the L2 VMT via the IRS. We use any of the VM's CPUs as a
Quoted 8 lines
arch/arm64/kvm/vgic/vgic-v5-tables.c line 197
Quoted 5 lines
arch/arm64/kvm/vgic/vgic-v5-tables.c new file +1
-0+* conduit for interacting with the host's IRS. In the current case,
arch/arm64/kvm/vgic/vgic-v5-tables.c new file +3
-0+* this lets us resolve the VM ID to pass to the hardware. +*/ + ret =irq_set_vcpu_affinity (vgic_v5_vpe_db (vcpu0), &cmd);[Severity: Critical]
What happens if this is called when no vCPUs have been created yet?
We don't get this far with 0 VCPUs so this is not an issue.
Quoted 5 lines
If
atomic_read(&kvm->online_vcpus)is 0 duringvgic_v5_vmte_init(),kvm_get_vcpu(kvm, 0)returns NULL, which would cause a NULL pointer dereference when it is passed tovgic_v5_vpe_db()here.
Yeah, that it would, but we don't allow that case to happen in the first place.
Quoted 47 lines
[ ... ]
Quoted 46 lines
arch/arm64/kvm/vgic/vgic-v5-tables.c new file +21
-0+int vgic_v5_vmte_init (struct kvm *kvm) +{ +size_t vmd_alloc_size, vpet_alloc_size, vped_alloc_size; +void *vped_base =NULL , *vmd =NULL ; +struct vgic_v5_vm_info *vmi =NULL ; +u64 tmp, vmte_val0 =0 , vmte_val1; +u32 vm_id =vgic_v5_vm_id (kvm); +int ret, nr_cpus, nr_vcpus; +bool vmi_inserted = false; +struct vmtl2_entry *vmte; +vpe_entry *vpet =NULL ; +struct kvm_vcpu *vcpu; +u16 max_vpe_id =0 ; +unsigned long i; + + nr_vcpus =atomic_read (&kvm->online_vcpus); +if (nr_vcpus > vmt_info->max_vpes) +return -E2BIG ; + +/* +* If we're using two-level VMTs, L2 is allocated on demand. For linear
arch/arm64/kvm/vgic/vgic-v5-tables.c new file +14
-0+* VMTs, this is a NOP. +*/ + ret =vgic_v5_alloc_l2_vmt (kvm); +if (ret) +return ret; + + vmte =vgic_v5_get_l2_vmte (vm_id); +if (IS_ERR (vmte)) +return PTR_ERR (vmte); + +/* If the entry is already valid, something went wrong */ + scoped_guard(raw_spinlock_irqsave, &vgic_v5_irs_lock) { +vgic_v5_clean_inval (vmte,sizeof (*vmte)); + if (le64_to_cpu(READ_ONCE(vmte->val[0])) &
GICV5_VMTEL2E_VALID)arch/arm64/kvm/vgic/vgic-v5-tables.c new file +9
-0+return -EINVAL ; + } + + ret =vgic_v5_reset_vmte (kvm); +if (ret) +return ret; + + vmi =kzalloc_obj (*vmi); +if (!vmi) {
Quoted 8 lines
arch/arm64/kvm/vgic/vgic-v5-tables.c line 440
Quoted 5 lines
arch/arm64/kvm/vgic/vgic-v5-tables.c new file +5
-0+ ret = -ENOMEM ; + goto out_fail; + } + + ret =xa_insert (&vm_info, vm_id, vmi,GFP_KERNEL );[Severity: High]
Is it safe to publish vmi before its fields are initialized?
We have nothing that concurrently reads (or modifies) it.
Quoted 9 lines
vmi is published to the global
vm_infoXArray here, but sleepable allocations
forvmi->vmd_base,vmi->vpet_base, andvmi->vped_baseoccur after this point.Could a concurrent thread executing
vgic_v5_vmte_alloc_vpe()retrieve the
partially constructed vmi and dereference its NULL pointers?
A concurrent thread could, but we don't have one. It would be rather broken to have one thread consuming things that happen on vgic_v5_vmte_init() before it has finished.
Quoted 99 lines
Quoted 99 lines
arch/arm64/kvm/vgic/vgic-v5-tables.c new file +8
-0+if (ret) + goto out_fail; + vmi_inserted = true; + +/* Allocate and assign the VM Descriptor, if required. */ +if (vmt_info->vmd_size !=0 ) { + vmd_alloc_size = round_up(vmt_info->vmd_size, +
dma_get_cache_alignment());arch/arm64/kvm/vgic/vgic-v5-tables.c new file +11
-0+ vmd =kzalloc (vmd_alloc_size,GFP_KERNEL ); +if (!vmd) { + ret = -ENOMEM ; + goto out_fail; + } + +/* Stash the VA so we can free it later */ + vmi->vmd_base = vmd; + + tmp = FIELD_PREP(GICV5_VMTEL2E_VMD_ADDR, + virt_to_phys(vmd) >>
GICV5_VMTEL2E_VMD_ADDR_SHIFT);arch/arm64/kvm/vgic/vgic-v5-tables.c new file +7
-0+ vmte_val0 = tmp; + } + +/* +* Allocate and assign the VPE Table. +* +* First of all, iterate over all vcpus to find the highest VPE ID we
arch/arm64/kvm/vgic/vgic-v5-tables.c new file +1
-0+* require - we need to ensure that we have enough storage for all
arch/arm64/kvm/vgic/vgic-v5-tables.c new file +1
-0+* vcpu_id values that userspace has picked and not just the total
arch/arm64/kvm/vgic/vgic-v5-tables.c new file +1
-0+* number of vcpus. This gives us the number of VPEs required for the
arch/arm64/kvm/vgic/vgic-v5-tables.c new file +3
-0+* VM. +* +* Round up the number of VPEs to a whole power of two as we cannot
arch/arm64/kvm/vgic/vgic-v5-tables.c new file +1
-0+* describe non-powers-of-two in the VMTE field as it conveys the number
arch/arm64/kvm/vgic/vgic-v5-tables.c new file +1
-0+* of ID bits used and not the number of vPEs. IRS_IDR1.IAFFID_BITS is
arch/arm64/kvm/vgic/vgic-v5-tables.c new file +1
-0+* encoded as N - 1, so expose at least one VPE ID bit even for a
arch/arm64/kvm/vgic/vgic-v5-tables.c new file +13
-0+* single-vCPU VM to keep the views consistent. +*/ +kvm_for_each_vcpu (i, vcpu, kvm) { +u16 vpe_id =vgic_v5_vpe_id (vcpu); + +if (vpe_id > max_vpe_id) + max_vpe_id = vpe_id; + } + + nr_cpus =max (2UL ,roundup_pow_of_two (max_vpe_id +1 )); + vmi->vpe_id_bits =fls (nr_cpus) -1 ; + + vpet_alloc_size = round_up((size_t)nr_cpus *
sizeof(*vpet),arch/arm64/kvm/vgic/vgic-v5-tables.c new file +12
-0+ dma_get_cache_alignment()); + vpet =kzalloc (vpet_alloc_size,GFP_KERNEL ); +if (!vpet) { + ret = -ENOMEM ; + goto out_fail; + } + +/* Stash the VA so we can free it later */ + vmi->vpet_base = vpet; + + tmp = FIELD_PREP(GICV5_VMTEL2E_VPET_ADDR, + virt_to_phys(vpet) >>
GICV5_VMTEL2E_VPET_ADDR_SHIFT);arch/arm64/kvm/vgic/vgic-v5-tables.c new file +1
-0+ tmp |= FIELD_PREP(GICV5_VMTEL2E_VPE_ID_BITS, vmi-Quoted 1 line
vpe_id_bits);arch/arm64/kvm/vgic/vgic-v5-tables.c new file +4
-0+ vmte_val1 = tmp; + +/* +* Allocate a dense VPED array indexed by vcpu_idx. The VPET is indexed
arch/arm64/kvm/vgic/vgic-v5-tables.c new file +1
-0+* by the potentially sparse vcpu_id, but using that ID here would waste
arch/arm64/kvm/vgic/vgic-v5-tables.c new file +1
-0+* memory. Given that this is not userspace visible, we can cheat a bit
arch/arm64/kvm/vgic/vgic-v5-tables.c new file +1
-0+* and use the dense index instead. This is the ONLY place that we do
arch/arm64/kvm/vgic/vgic-v5-tables.c new file +3
-0+* this. +* +* Round the requested size up to a whole cacheline.
kzalloc()guranteesarch/arm64/kvm/vgic/vgic-v5-tables.c new file +1
-0+* natural alignment, so we ensure that the cachelines cannot be shared
arch/arm64/kvm/vgic/vgic-v5-tables.c new file +1
-0+* with unrelated slab objects. VPED and cacheline sizes are powers of
arch/arm64/kvm/vgic/vgic-v5-tables.c new file +1
-0+* two, so this also preserves the required VPED alignment when a VPED
arch/arm64/kvm/vgic/vgic-v5-tables.c new file +3
-0+* is larger than a cacheline. +*/ + vped_alloc_size = round_up((size_t)nr_vcpus * vmt_info-
Quoted 6 lines
arch/arm64/kvm/vgic/vgic-v5-tables.c
Quoted 3 lines
Quoted 1 line
vped_size,arch/arm64/kvm/vgic/vgic-v5-tables.c new file +2
-0+ dma_get_cache_alignment()); + vped_base =kzalloc (vped_alloc_size,GFP_KERNEL );[Severity: Critical]
Cannr_vcpusbe 0 here?
No. We can't make it this far with 0 VCPUs.
Quoted 4 lines
If no vCPUs are online,
vped_alloc_sizeevaluates to 0. Callingkzalloc(0, GFP_KERNEL)returns theZERO_SIZE_PTR(0x10).
Quoted 16 lines
arch/arm64/kvm/vgic/vgic-v5-tables.c line 529
Quoted 10 lines
arch/arm64/kvm/vgic/vgic-v5-tables.c new file +10
-0+if (!vped_base) { + ret = -ENOMEM ; + goto out_fail; + } + vmi->vped_base = vped_base; + +if (vmd) +vgic_v5_clean_inval (vmd, vmd_alloc_size); +vgic_v5_clean_inval (vpet, vpet_alloc_size); +vgic_v5_clean_inval (vped_base, vped_alloc_size);Passing the
ZERO_SIZE_PTRtovgic_v5_clean_inval()will execute a cache
invalidation on address0x10, which would cause a fatal translation fault
and crash the kernel.
This case cannot happen. We require at least one VCPU.
Quoted 35 lines
[ ... ]
Quoted 19 lines
arch/arm64/kvm/vgic/vgic-v5-tables.c new file +15
-0+int vgic_v5_vmte_release (struct kvm *kvm) +{ +u32 vm_id =vgic_v5_vm_id (kvm); +struct vgic_v5_vm_info *vmi; +struct vmtl2_entry *vmte; +int ret; + + vmte =vgic_v5_get_l2_vmte (vm_id); +if (IS_ERR (vmte)) +return PTR_ERR (vmte); + +/* Reject if the VMTE has not been marked as invalid! */ + scoped_guard(raw_spinlock_irqsave, &vgic_v5_irs_lock) { +vgic_v5_clean_inval (vmte,sizeof (*vmte)); + if (le64_to_cpu(READ_ONCE(vmte->val[0])) &
GICV5_VMTEL2E_VALID)arch/arm64/kvm/vgic/vgic-v5-tables.c new file +3
-0+return -EINVAL ; + } +arch/arm64/kvm/vgic/vgic-v5-tables.c line 589
Quoted 10 lines
arch/arm64/kvm/vgic/vgic-v5-tables.c new file +10
-0+ vmi =xa_load (&vm_info, vm_id); +if (!vmi) + goto no_vmi; + +kfree (vmi->vped_base); +kfree (vmi->vpet_base); +kfree (vmi->vmd_base); + +xa_erase (&vm_info, vm_id); +kfree (vmi);[Severity: High]
Could this synchronously free a vmi object that is currently in use?
Quoted 5 lines
This path synchronously calls kfree(vmi) after erasing it from the XArray,
but readers likevgic_v5_vmte_alloc_vpe()load from the XArray without RCU
protection.
Except that they don't. XArrays use RCU protection internally for explicitly this reason.
Quoted 3 lines
Will this cause a use-after-free for any lockless readers that have preempted?
No.
Quoted 18 lines
[ ... ]
arch/arm64/kvm/vgic/vgic-v5-tables.c line 619
Quoted 13 lines
arch/arm64/kvm/vgic/vgic-v5-tables.c new file +13
-0+int vgic_v5_vmte_alloc_vpe (struct kvm_vcpu *vcpu) +{ +u32 vm_id =vgic_v5_vm_id (vcpu->kvm); +u16 vpe_id =vgic_v5_vpe_id (vcpu); +struct vgic_v5_vm_info *vmi; +vpe_entry tmp, *vpet_base; +void *vped; + +/* Make sure we're not over what the hardware supports */ +if (vpe_id >= vmt_info->max_vpes) +return -E2BIG ; + + vmi =xa_load (&vm_info, vm_id);[Severity: High]
Does thisxa_load()needrcu_read_lock()protection?
It does not.
Quoted 5 lines
The vmi object is retrieved without RCU read-side protection, and
vgic_v5_vmte_release()synchronously calls kfree(vmi).Could a preempted reader here dereference a freed vmi object?
No. There's no realistic path for this to happen. We should never be allocating and freeing the VPEs at the same time (for obvious reasons, I hope).
Quoted 22 lines
Quoted 22 lines
arch/arm64/kvm/vgic/vgic-v5-tables.c new file +9
-0+if (!vmi) +return -EINVAL ; + +if (vpe_id >=1 << vmi->vpe_id_bits) +return -E2BIG ; + + vpet_base = vmi->vpet_base; + +/* If the VPETE for this CPU is already valid we've gone wrong */
arch/arm64/kvm/vgic/vgic-v5-tables.c new file +2
-0+ scoped_guard(raw_spinlock_irqsave, &vgic_v5_irs_lock) { + vgic_v5_clean_inval(&vpet_base[vpe_id],
sizeof(*vpet_base));arch/arm64/kvm/vgic/vgic-v5-tables.c new file +1
-0+ if (le64_to_cpu(READ_ONCE(vpet_base[vpe_id])) &
GICV5_VPE_VALID)arch/arm64/kvm/vgic/vgic-v5-tables.c new file +6
-0+return -EBUSY ; + } + + vped = (u8 *)vmi->vped_base + + (size_t )vcpu->vcpu_idx * vmt_info->vped_size; +
Quoted 8 lines
arch/arm64/kvm/vgic/vgic-v5-tables.c line 641
Quoted 5 lines
arch/arm64/kvm/vgic/vgic-v5-tables.c new file +1
-0+ tmp = FIELD_PREP(GICV5_VPED_ADDR, virt_to_phys(vped) >>
GICV5_VPED_ADDR_SHIFT);arch/arm64/kvm/vgic/vgic-v5-tables.c new file +3
-0+ + scoped_guard(raw_spinlock_irqsave, &vgic_v5_irs_lock) { +WRITE_ONCE (vpet_base[vpe_id],cpu_to_le64 (tmp));[Severity: High]
Is theGICV5_VPE_VALIDbit missing here?
As I said above, no. This is set by the hardware after software issues the corresponding command via the IRS's MMIO interface. This is added in subsequent commits in the series.
Quoted 29 lines
The VPET entry is written to memory, but the
GICV5_VPE_VALIDbit is not set,
which might cause the hardware to treat the entry as invalid.Quoted 12 lines
arch/arm64/kvm/vgic/vgic-v5-tables.c new file +1
-0+ vgic_v5_clean_inval(vpet_base + vpe_id,
sizeof(vpe_entry));arch/arm64/kvm/vgic/vgic-v5-tables.c new file +4
-0+ } + +return 0 ; +}arch/arm64/kvm/vgic/vgic-v5.c +0
-0[ ... ]
Quoted 11 lines
+static int
vgic_v5_db_set_vcpu_affinity(struct irq_data *data, void *vcpu_info)arch/arm64/kvm/vgic/vgic-v5.c +9
-0+{ +enum gicv5_vcpu_cmd *cmd = vcpu_info; + +guard (raw_spinlock_irqsave)(&vgic_v5_irs_lock); + +switch (*cmd) { +case VMT_L2_MAP : +case VMTE_MAKE_VALID : +case VMTE_MAKE_INVALID :
Quoted 8 lines
arch/arm64/kvm/vgic/vgic-v5.c int vgic_v5_probe(const struct gic_kvm_info *info) · line 148
Quoted 5 lines
arch/arm64/kvm/vgic/vgic-v5.c +5
-0+/* Not yet implemented */ +default : +return -EINVAL ; + } +}[Severity: High]
Should this function forward valid commands to the parent irqchip?
No. These commands are not yet implemented in this commit this is stub code, but should definitely not be forwarded to the parent irqchip!
Quoted 9 lines
By returning
-EINVALfor commands likeVMT_L2_MAP, it intercepts and drops
them without callingirq_chip_set_vcpu_affinity_parent().This causes the
VMT_L2_MAPcommand issued byvgic_v5_alloc_l2_vmt()to always
fail, preventing VMs from booting.
Thanks,
Sascha
PATCH v5 10/49 KVM: arm64: gic-v5: Introduce guest IST alloc and management
In reply to Sascha BischoffGICv5 guests use Interrupt State Tables (ISTs) to track and manage the interrupt state for SPIs and LPIs. These ISTs are provided to the host's IRS via the VMTE.
On a host GICv5 system, SPIs do not require any up-front memory allocation prior to their use, unlike LPIs which require the OS to allocate an IST. For a GICv5 guest, the same holds from the guest's point of view: SPIs should require no explicit memory allocation by the guest. This means that KVM must provision the memory passed to the IRS for managing a guest's SPI state.
Introduce the machinery needed to allocate a linear SPI IST. A later commit will allocate it as part of VM creation, before running the guest for the first time. The host is responsible for freeing this memory on guest teardown.
For LPIs, the guest provisions memory for its LPI IST. KVM does not pass that memory directly to the host IRS. Instead, add support for allocating a shadow LPI IST and passing that to the IRS through the VMTE. The LPI IST may be allocated as a two-level structure when supported and required by the configured LPI ID space, as many more LPIs are expected than SPIs. The host frees this memory on guest teardown.
Assigning an IST to a VM requires an IRS operation rather than writing the Valid bit directly in the VMTE. Extend the doorbell command interface with the operations needed to make SPI and LPI ISTs valid or invalid. Together with the existing VMT commands, the command set is:
VMT_L2_MAP - Make a second level VM table valid VMTE_MAKE_VALID - Make a single VMTE, and hence VM, valid VMTE_MAKE_INVALID - Make a single VMTE, and hence VM, invalid SPI_VIST_MAKE_VALID - Make the SPI IST valid LPI_VIST_MAKE_VALID - Make the LPI IST valid LPI_VIST_MAKE_INVALID - Make the LPI IST invalid
Once implemented, irq_set_vcpu_affinity() will act as an ioctl-like interface for issuing commands specific to either a VM or the particular VPE that the doorbell belongs to. None of these commands are plumbed through to the host IRS at this stage.
There is intentionally no SPI_VIST_MAKE_INVALID command. The SPI IST will be allocated as part of VM creation and will remain valid for the lifetime of the VM. It can be freed after the VMTE has been made invalid during teardown. The LPI IST, on the other hand, is driven by the guest, which is free to invalidate and free its LPI IST at any point.
--- arch/arm64/kvm/vgic/vgic-v5-tables.c | 551 +++++++++++++++++++++++++++ arch/arm64/kvm/vgic/vgic-v5-tables.h | 22 ++ arch/arm64/kvm/vgic/vgic-v5.c | 3 + include/linux/irqchip/arm-gic-v5.h | 3 + 4 files changed, 579 insertions(+)
arch/arm64/kvm/vgic/vgic-v5-tables.c +551 -0
@@ -65,6 +65,14 @@ static DEFINE_XARRAY(vm_info);#define GICV5_VPED_ADDR_SHIFT 3ULL#define GICV5_VPED_ADDR GENMASK_ULL(55, 3) +/* +* The LPI and SPI configuration is stored in the 2nd and 3rd 64-bit chunks of +* the VMTE (0-based). We call this a section here in an attempt to simplify the +* code. +*/ +#define GICV5_VMTEL2_LPI_SECTION 2 +#define GICV5_VMTEL2_SPI_SECTION 3 +/* * Our IRS might be coherent or non-coherent. If coherent, we can just emit a * DSB to ensure that we're in sync. However, when non-coherent, we need to
@@ -553,6 +561,25 @@ int vgic_v5_vmte_init(struct kvm *kvm)return ret; } +/* +* The following set of forward declarations makes the code layout a *little* +* clearer as it lets us keep the IST-related code together. +*/ +static int vgic_v5_alloc_linear_ist (struct kvm *kvm,bool spi_ist, +unsigned int id_bits, +unsigned int istsz); +static int vgic_v5_alloc_l1_ist (struct kvm *kvm,unsigned int id_bits, +unsigned int istsz,unsigned int l2_split); +static int vgic_v5_alloc_l2_ists (struct kvm *kvm,unsigned int id_bits, +unsigned int istsz,unsigned int l2_split); +static int vgic_v5_alloc_two_level_lpi_ist (struct kvm *kvm, +unsigned int id_bits, +unsigned int istsz, +unsigned int l2_split); +static int vgic_v5_linear_ist_free (struct kvm *kvm,bool spi); +static int vgic_v5_two_level_ist_free (struct kvm *kvm,bool spi); +static int vgic_v5_spi_ist_free (struct kvm *kvm); +/* * Release the VMT Entry, freeing up any allocated data structures before * zeroing the VMTE.
@@ -581,6 +608,22 @@ int vgic_v5_vmte_release(struct kvm *kvm)if (!vmi) goto no_vmi; +/* If we have an LPI IST, free it */ +if (vmi->h_lpi_ist) { + ret =vgic_v5_lpi_ist_free (kvm); +if (ret) +return ret; + } + vmi->h_lpi_ist =NULL ; + +/* If we have an SPI IST, free it */ +if (vmi->h_spi_ist) { + ret =vgic_v5_spi_ist_free (kvm); +if (ret) +return ret; + } + vmi->h_spi_ist =NULL ; +kfree (vmi->vped_base);kfree (vmi->vpet_base);kfree (vmi->vmd_base);
@@ -679,3 +722,511 @@ int vgic_v5_vmte_free_vpe(struct kvm_vcpu *vcpu)return 0 ; } + +/* +* Assign an already allocated IST to the VM by populating the fields in the +* corresponding VMTE. We re-use this code for both an SPI IST and LPI IST, even +* if the paths to reach it might be vastly different. +*/ +static int vgic_v5_vmte_assign_ist (struct kvm *kvm,phys_addr_t ist_base, +bool two_level,unsigned int id_bits, +unsigned int l2sz,unsigned int istsz, +bool spi_ist) +{ +struct kvm_vcpu *vcpu0 =kvm_get_vcpu (kvm,0 ); +u32 vm_id =vgic_v5_vm_id (kvm); +enum gicv5_vcpu_cmd cmd; +struct vmtl2_entry *vmte; +unsigned int section; +u64 tmp; +int ret; + +/* +* The L2 VMTE comprises four 64-bit "sections", where sections 2 & 3 +* describe the LPI and SPI ISTs, respectively. Both the LPI and SPI +* sections have the same layout, and as we are either operating on SPIs +* or LPIs we pick a section of the VMTE to modify up-front. +* +* See the GICv5 EAC0 Specification 11.2.2 for more details about the +* VMTE layout. +*/ + section = spi_ist ?GICV5_VMTEL2_SPI_SECTION :GICV5_VMTEL2_LPI_SECTION ; + +if (ist_base & ~GICV5_VMTEL2E_IST_ADDR ) { +pr_err_ratelimited ("kvm [%i]: IST misaligned: address 0x%llx, mask 0x%llx\n" , +task_pid_nr (current), ist_base, +GICV5_VMTEL2E_IST_ADDR ); +return -EINVAL ; + } + + vmte =vgic_v5_get_l2_vmte (vm_id); +if (IS_ERR (vmte)) +return PTR_ERR (vmte); + + tmp =FIELD_PREP (GICV5_VMTEL2E_IST_L2SZ , l2sz); + tmp |=FIELD_PREP (GICV5_VMTEL2E_IST_ADDR , + ist_base >>GICV5_VMTEL2E_IST_ADDR_SHIFT ); + tmp |=FIELD_PREP (GICV5_VMTEL2E_IST_ISTSZ , istsz); + tmp |=FIELD_PREP (GICV5_VMTEL2E_IST_ID_BITS , id_bits); +if (two_level) + tmp |=GICV5_VMTEL2E_IST_STRUCTURE ; + + scoped_guard(raw_spinlock_irqsave, &vgic_v5_irs_lock) { +/* Bail if already allocated */ +vgic_v5_clean_inval (vmte,sizeof (*vmte)); +if (le64_to_cpu (READ_ONCE (vmte->val[section])) & +GICV5_VMTEL2E_IST_VALID ) +return -EINVAL ; + +WRITE_ONCE (vmte->val[section],cpu_to_le64 (tmp)); +vgic_v5_clean_inval (vmte,sizeof (*vmte)); + } + +/* Finally, mark the entry as valid */ + cmd = spi_ist ?SPI_VIST_MAKE_VALID :LPI_VIST_MAKE_VALID ; + ret =irq_set_vcpu_affinity (vgic_v5_vpe_db (vcpu0), &cmd); + +return ret; +} + +/* +* Allocate a Linear IST - always used for SPIs and potentially LPIs. +* +* The calculation for n has been taken from section 11.2.2 of the GICv5 EAC0 +* spec. +* +* NOTE: istsz is the FIELD used by GICv5, not the actual size (or log2() of the +* size). +*/ +static int vgic_v5_alloc_linear_ist (struct kvm *kvm,bool spi_ist, +unsigned int id_bits,unsigned int istsz) +{ +const size_t n =max (5 , id_bits +1 + istsz); +u32 vm_id =vgic_v5_vm_id (kvm); +struct vgic_v5_vm_info *vmi; +__le64 *ist; +u32 l1sz; + + vmi =xa_load (&vm_info, vm_id); +if (!vmi) +return -EINVAL ; + +/* +* Allocate the IST. We only have one level, so we just use the L2 ISTE. +*/ + l1sz =BIT (n +1 ); + ist =kzalloc (l1sz,GFP_KERNEL_ACCOUNT ); +if (!ist) +return -ENOMEM ; + +if (spi_ist) { + vmi->h_spi_ist = ist; + }else { + vmi->h_lpi_ist_structure = false; + vmi->h_lpi_ist = ist; + } + +vgic_v5_clean_inval (ist, l1sz); + +return 0 ; +} + +/* +* Allocate the first level of a two-level IST - LPI, only. +* +* The calculation for n has been taken from section 11.2.2 of the GICv5 EAC0 +* spec. +* +* NOTE: istsz and l2sz are the FIELDS used by GICv5, not the actual sizes (or +* log2() of the sizes). +*/ +static int vgic_v5_alloc_l1_ist (struct kvm *kvm,unsigned int id_bits, +unsigned int istsz,unsigned int l2sz) +{ +const size_t n =max (5 , id_bits - ((10 - istsz) + (2 * l2sz)) +3 -1 ); +u32 vm_id =vgic_v5_vm_id (kvm); +const u32 l1_size =BIT (n +1 ); +struct vgic_v5_vm_info *vmi; +__le64 *ist; + + vmi =xa_load (&vm_info, vm_id); +if (!vmi) +return -EINVAL ; + + ist =kzalloc (l1_size,GFP_KERNEL_ACCOUNT ); +if (!ist) +return -ENOMEM ; + + vmi->h_lpi_ist_structure = true; + vmi->h_lpi_ist = ist; + +vgic_v5_clean_inval (ist, l1_size); + +return 0 ; +} + +/* +* Allocate ALL of the second level ISTs for a two-level IST - LPI, only. +* +* The calculation for n has been taken from section 11.2.2 of the GICv5 EAC0 +* spec. The l2_size calculation is from section 11.2.3 of the same document. +* +* NOTE: istsz and l2sz are the FIELDS used by GICv5, not the actual sizes (or +* log2() of the sizes). +*/ +static int vgic_v5_alloc_l2_ists (struct kvm *kvm,unsigned int id_bits, +unsigned int istsz,unsigned int l2sz) +{ +const size_t n =max (5 , id_bits - ((10 - istsz) + (2 * l2sz)) +3 -1 ); +const int l1_entries =BIT (n +1 ) /GICV5_IRS_ISTL1E_SIZE ; +const size_t l2_size =BIT (11 + (2 * l2sz) +1 ); +u32 vm_id =vgic_v5_vm_id (kvm); +struct vgic_v5_vm_info *vmi; +__le64 *l2ist; +__le64 *l1ist; +int index; +u64 val; + + vmi =xa_load (&vm_info, vm_id); +if (!vmi) +return -EINVAL ; + + l1ist = vmi->h_lpi_ist; + +/* +* Allocate the storage for the pointers to the L2 ISTs (used when +* freeing later). +*/ + vmi->h_lpi_l2_ists =kzalloc_objs (*vmi->h_lpi_l2_ists, l1_entries, +GFP_KERNEL_ACCOUNT ); +if (!vmi->h_lpi_l2_ists) +return -ENOMEM ; + +/* Allocate the L2 IST for each L1 IST entry */ +for (index =0 ; index < l1_entries; ++index) { + l2ist =kzalloc (l2_size,GFP_KERNEL_ACCOUNT ); +if (!l2ist) { +while (--index >=0 ) +kfree (vmi->h_lpi_l2_ists[index]); + +kfree (vmi->h_lpi_l2_ists); + vmi->h_lpi_l2_ists =NULL ; + +return -ENOMEM ; + } + +/* +* We are not doing on-demand allocation of the L2 ISTs, and are +* instead provisioning the whole IST up front. This means that +* we are able to mark the L2 ISTs as valid in the L1 ISTEs as +* the overall IST is not yet valid. +*/ + val = (virt_to_phys (l2ist) &GICV5_ISTL1E_L2_ADDR_MASK ) | +GICV5_ISTL1E_VALID ; + l1ist[index] =cpu_to_le64 (val); + + vmi->h_lpi_l2_ists[index] = l2ist; + +vgic_v5_clean_inval (l2ist, l2_size); + } + +/* Handle CMOs for the whole L1 IST in one go */ +vgic_v5_clean_inval (l1ist, l1_entries *sizeof (*l1ist)); + +return 0 ; +} + +/* Allocate a two-level IST - LPIs, only */ +static int vgic_v5_alloc_two_level_lpi_ist (struct kvm *kvm,unsigned int id_bits, +unsigned int istsz,unsigned int l2sz) +{ +u32 vm_id =vgic_v5_vm_id (kvm); +struct vgic_v5_vm_info *vmi; +int ret; + +/* +* Allocate the L1 IST first, then all of the L2s. Everything +* is preallocated and we do no on-demand IST allocation. This +* is to avoid needing to track if and when the guest is doing +* on-demand IST allocation. +*/ + ret =vgic_v5_alloc_l1_ist (kvm, id_bits, istsz, l2sz); +if (ret) +return ret; + + ret =vgic_v5_alloc_l2_ists (kvm, id_bits, istsz, l2sz); +if (ret) { +/* Free the L1 IST again */ + vmi =xa_load (&vm_info, vm_id); +kfree (vmi->h_lpi_ist); + vmi->h_lpi_ist =0 ; + +return ret; + } + +return 0 ; +} + +static void vgic_v5_free_allocated_lpi_ist (struct vgic_v5_vm_info *vmi, +unsigned int id_bits, +unsigned int istsz, +unsigned int l2sz) +{ +if (!vmi->h_lpi_ist_structure) { +kfree (vmi->h_lpi_ist); + vmi->h_lpi_ist =NULL ; +return ; + } + +if (vmi->h_lpi_l2_ists) { +const size_t n =max (5 , id_bits - ((10 - istsz) + (2 * l2sz)) +3 -1 ); +const int l1_entries =BIT (n +1 ) /GICV5_IRS_ISTL1E_SIZE ; +int index; + +for (index =0 ; index < l1_entries; ++index) +kfree (vmi->h_lpi_l2_ists[index]); + +kfree (vmi->h_lpi_l2_ists); + vmi->h_lpi_l2_ists =NULL ; + } + +kfree (vmi->h_lpi_ist); + vmi->h_lpi_ist =NULL ; +} + +static void vgic_v5_free_allocated_spi_ist (struct kvm *kvm) +{ +u32 vm_id =vgic_v5_vm_id (kvm); +struct vgic_v5_vm_info *vmi; + + vmi =xa_load (&vm_info, vm_id); +if (!vmi) +return ; + +kfree (vmi->h_spi_ist); + vmi->h_spi_ist =NULL ; +} + +/* +* Free a Linear IST. Can only happen once the VM is dead. +*/ +static int vgic_v5_linear_ist_free (struct kvm *kvm,bool spi) +{ +u32 vm_id =vgic_v5_vm_id (kvm); +struct vmtl2_entry *vmte; +struct vgic_v5_vm_info *vmi; +int section; + + vmi =xa_load (&vm_info, vm_id); +if (!vmi) +return -EINVAL ; + + vmte =vgic_v5_get_l2_vmte (vm_id); +if (IS_ERR (vmte)) +return PTR_ERR (vmte); + +if (spi) { + section =GICV5_VMTEL2_SPI_SECTION ; +vgic_v5_free_allocated_spi_ist (kvm); + }else { + section =GICV5_VMTEL2_LPI_SECTION ; +vgic_v5_free_allocated_lpi_ist (vmi,0 ,0 ,0 ); + } + +/* The VM should be dead here, so we can just zero the VMT section */ + scoped_guard(raw_spinlock_irqsave, &vgic_v5_irs_lock) { +WRITE_ONCE (vmte->val[section],cpu_to_le64 (0 )); +vgic_v5_clean_inval (vmte,sizeof (*vmte)); + } + +return 0 ; +} + +/* +* Free a Two-Level IST. Can only happen once the VM is dead. +*/ +static int vgic_v5_two_level_ist_free (struct kvm *kvm,bool spi) +{ +unsigned int id_bits, istsz, l2sz; +u32 vm_id =vgic_v5_vm_id (kvm); +struct vgic_v5_vm_info *vmi; +struct vmtl2_entry *vmte; +u64 tmp; +int section; + +/* We don't create two-level SPI ISTs, so freeing is a bad idea! */ +if (spi) +return -EINVAL ; + + vmi =xa_load (&vm_info, vm_id); +if (!vmi) +return -EINVAL ; + + section =GICV5_VMTEL2_LPI_SECTION ; + +if (!vmi->h_lpi_ist_structure) +return -EINVAL ; + + vmte =vgic_v5_get_l2_vmte (vm_id); +if (IS_ERR (vmte)) +return PTR_ERR (vmte); + + scoped_guard(raw_spinlock_irqsave, &vgic_v5_irs_lock) { +vgic_v5_clean_inval (vmte,sizeof (*vmte)); + tmp =le64_to_cpu (READ_ONCE (vmte->val[section])); + } + + id_bits =FIELD_GET (GICV5_VMTEL2E_IST_ID_BITS , tmp); + istsz =FIELD_GET (GICV5_VMTEL2E_IST_ISTSZ , tmp); + l2sz =FIELD_GET (GICV5_VMTEL2E_IST_L2SZ , tmp); + +vgic_v5_free_allocated_lpi_ist (vmi, id_bits, istsz, l2sz); + +/* The VM must be dead, so we can just zero the VMT section */ + scoped_guard(raw_spinlock_irqsave, &vgic_v5_irs_lock) { +WRITE_ONCE (vmte->val[section],cpu_to_le64 (0 )); +vgic_v5_clean_inval (vmte,sizeof (*vmte)); + } + +return 0 ; +} + +/* Helper to determine ISTE size based on metadata requirements */ +static unsigned int vgic_v5_ist_istsz (unsigned int id_bits) +{ +if (!vgic_v5_irs_istmd (&irs_caps)) +return GICV5_IRS_IST_CFGR_ISTSZ_4 ; + +if (id_bits >=vgic_v5_irs_istmd_sz (&irs_caps)) +return GICV5_IRS_IST_CFGR_ISTSZ_16 ; + +return GICV5_IRS_IST_CFGR_ISTSZ_8 ; +} + +/* +* Allocate an IST for SPIs. +* +* We don't anticipate a large number of SPIs being allocated. Therefore, we +* always allocate a Linear IST for SPIs. This will need to be revisited should +* that assumption no longer hold. +*/ +int vgic_v5_spi_ist_alloc (struct kvm *kvm,unsigned int id_bits) +{ +u32 vm_id =vgic_v5_vm_id (kvm); +struct vgic_v5_vm_info *vmi; +phys_addr_t base_addr; +unsigned int istsz; +int ret; + +lockdep_assert_held (&kvm->arch.config_lock); + + istsz =vgic_v5_ist_istsz (id_bits); + + vmi =xa_load (&vm_info, vm_id); +if (!vmi) +return -EINVAL ; + +if (vmi->h_spi_ist) +return -EBUSY ; + + ret =vgic_v5_alloc_linear_ist (kvm, true, id_bits, istsz); +if (ret) +return ret; + base_addr =virt_to_phys (vmi->h_spi_ist); + + ret =vgic_v5_vmte_assign_ist (kvm, base_addr, false, id_bits,0 , istsz, + true); +if (ret) { +vgic_v5_free_allocated_spi_ist (kvm); +return ret; + } + +return 0 ; +} + +/* +* Free the IST for SPIs. Should only happen once the VM is dead. +*/ +static int vgic_v5_spi_ist_free (struct kvm *kvm) +{ +lockdep_assert_held (&kvm->arch.config_lock); + +return vgic_v5_linear_ist_free (kvm, true); +} + +/* +* Allocate an IST for LPIs. +* +* Unlike with SPIs, we anticipate that the guest will allocate a relatively +* large number of LPIs. Therefore, while we support doing a linear LPI IST, it +* is expected that LPI ISTs will be two-level. +*/ +int vgic_v5_lpi_ist_alloc (struct kvm *kvm,unsigned int id_bits) +{ +u32 vm_id =vgic_v5_vm_id (kvm); +struct vgic_v5_vm_info *vmi; +unsigned int istsz, l2sz; +phys_addr_t phys_addr; +bool two_level; +int ret; + +lockdep_assert_held (&kvm->arch.config_lock); + + vmi =xa_load (&vm_info, vm_id); +if (!vmi) +return -EINVAL ; + +if (vmi->h_lpi_ist) +return -EBUSY ; + + istsz =vgic_v5_ist_istsz (id_bits); + l2sz =gicv5_irs_l2_sz (vgic_v5_irs_ist_l2sz (&irs_caps)); + +/* +* Determine if we want to create a Linear or a Two-Level IST. +* +* A two-level IST is only required when a single L2 IST cannot cover +* the requested ID space. This depends on the L2 IST size selected for +* the IRS, not PAGE_SIZE. Using PAGE_SIZE here would switch to +* two-level too early when the selected L2 IST is larger than a page, +* and the allocation sizing arithmetic would underflow. +*/ + two_level =vgic_v5_irs_ist_levels (&irs_caps) && + id_bits > ((10 - istsz) + (2 * l2sz)); + +if (!two_level) + ret =vgic_v5_alloc_linear_ist (kvm, false/* LPIs, not SPIs */ , + id_bits, istsz); +else + ret =vgic_v5_alloc_two_level_lpi_ist (kvm, id_bits, istsz, + l2sz); + +if (ret) +return ret; + + phys_addr =virt_to_phys (vmi->h_lpi_ist); + ret =vgic_v5_vmte_assign_ist (kvm, phys_addr, two_level, id_bits, l2sz, + istsz, false); +if (ret) +vgic_v5_free_allocated_lpi_ist (vmi, id_bits, istsz, l2sz); + +return ret; +} + +/* Free the LPI IST again */ +int vgic_v5_lpi_ist_free (struct kvm *kvm) +{ +u32 vm_id =vgic_v5_vm_id (kvm); +struct vgic_v5_vm_info *vmi; + +lockdep_assert_held (&kvm->arch.config_lock); + + vmi =xa_load (&vm_info, vm_id); +if (!vmi) +return -ENXIO ; + +if (!vmi->h_lpi_ist_structure) +return vgic_v5_linear_ist_free (kvm, false); +else +return vgic_v5_two_level_ist_free (kvm, false); +}
arch/arm64/kvm/vgic/vgic-v5-tables.h +22 -0
@@ -26,6 +26,24 @@ struct vgic_v5_vm_info {vpe_entry __iomem *vpet_base;void *vped_base;u8 vpe_id_bits; + +/* +* Both the LPI and SPI ISTs are allocated by the hypervisor. While it +* would be possible to track and access them by iterating over the ISTs +* themselves, it makes more sense to store pointers to the ISTs. +* +* The LPI IST can either be two-level or linear. Hence, we keep track +* of the structure. If it is two-level, we retain pointers to the L1 +* IST and to each L2 IST array. If it is linear, we just store the base +* address of the IST array. +* +* The SPI IST is linear, and therefore we just store the base address +* of the SPI IST array. +*/ +bool h_lpi_ist_structure; +__le64 *h_lpi_ist; +__le64 **h_lpi_l2_ists; +__le64 *h_spi_ist; };struct vgic_v5_vmt {
@@ -97,4 +115,8 @@ int vgic_v5_vmte_release(struct kvm *kvm);int vgic_v5_vmte_alloc_vpe (struct kvm_vcpu *vcpu);int vgic_v5_vmte_free_vpe (struct kvm_vcpu *vcpu); +int vgic_v5_spi_ist_alloc (struct kvm *kvm,unsigned int id_bits); +int vgic_v5_lpi_ist_alloc (struct kvm *kvm,unsigned int id_bits); +int vgic_v5_lpi_ist_free (struct kvm *kvm); +#endif
arch/arm64/kvm/vgic/vgic-v5.c +3 -0
@@ -141,6 +141,9 @@ static int vgic_v5_db_set_vcpu_affinity(struct irq_data *data, void *vcpu_info)case VMT_L2_MAP :case VMTE_MAKE_VALID :case VMTE_MAKE_INVALID : +case SPI_VIST_MAKE_VALID : +case LPI_VIST_MAKE_VALID : +case LPI_VIST_MAKE_INVALID :/* Not yet implemented */ default :return -EINVAL ;
include/linux/irqchip/arm-gic-v5.h +3 -0
@@ -659,6 +659,9 @@ enum gicv5_vcpu_cmd {VMT_L2_MAP ,/* Map in a L2 VMT - *may* happen on VM init */ VMTE_MAKE_VALID ,/* Make the VMTE valid */ VMTE_MAKE_INVALID ,/* Make the VMTE (et al.) invalid */ +SPI_VIST_MAKE_VALID ,/* No corresponding invalid */ +LPI_VIST_MAKE_VALID ,/* Triggered by a guest */ + LPI_VIST_MAKE_INVALID, /* Triggered by a guest */ };#endif
Signature · 1 line
2.34.1
[email protected]
AI reviewRe: [PATCH v5 10/49] KVM: arm64: gic-v5: Introduce guest IST alloc and management
Thank you for your contribution! Sashiko AI review found 6 potential issue(s) to consider:
Thank you for your contribution! Sashiko AI review found 6 potential issue(s) to consider:
New issues:
- [Critical] Hardware Use-After-Free (UAF) due to missing IRS invalidation command during LPI IST teardown.
- [Critical] Integer truncation in vgic_v5_alloc_linear_ist causes ZERO_SIZE_PTR allocation and silent memory corruption.
- [High] Unconditional NULL pointer dereference of vcpu0 during IST assignment.
- [High] Silent physical address truncation in L2 IST allocation due to unvalidated masking.
- [Medium] Permanent memory leak of VM resources due to early aborts in vgic_v5_vmte_release.
Pre-existing issues:
- [High] Missing DSB barrier after cache maintenance allows the hardware IRS to read stale VMTE data. --
--- Patch [10]: [PATCH v5 10/49] KVM: arm64: gic-v5: Introduce guest IST alloc and management --- commit 4b81b458fb30bd2b57a30edcbe23a8f8de22edbb
Author: Sascha Bischoff <[email protected]> Date: Fri Aug 7 11:17:41 2026 +0000
KVM: arm64: gic-v5: Introduce guest IST alloc and management
GICv5 guests use Interrupt State Tables (ISTs) to track and manage the interrupt state for SPIs and LPIs. This commit introduces the machinery needed to allocate a linear SPI IST and a shadow LPI IST, and pass them to the host's IRS via the VMTE.
Quoted 4 lines
arch/arm64/kvm/vgic/vgic-v5-tables.c +0
-0
[ ... ]
arch/arm64/kvm/vgic/vgic-v5-tables.c int vgic_v5_vmte_release(struct kvm *kvm) · line 615
Quoted 9 lines
arch/arm64/kvm/vgic/vgic-v5-tables.c +5
-0@@ -581,6 +608,22 @@ int vgic_v5_vmte_release(struct kvm *kvm)if (!vmi) goto no_vmi; +/* If we have an LPI IST, free it */ +if (vmi->h_lpi_ist) { + ret =vgic_v5_lpi_ist_free (kvm); +if (ret) +return ret;
[Severity: Medium]
What happens to the remaining allocated resources if this returns early?
If vgic_v5_lpi_ist_free() returns an error, the function aborts immediately. This bypasses the release of vped_base, vpet_base, and vmd_base, and skips removing the entry from vm_info, permanently leaking these VM resources.
arch/arm64/kvm/vgic/vgic-v5-tables.c int vgic_v5_vmte_release(struct kvm *kvm) · line 623
Quoted 8 lines
arch/arm64/kvm/vgic/vgic-v5-tables.c +8
-0+ } + vmi->h_lpi_ist =NULL ; + +/* If we have an SPI IST, free it */ +if (vmi->h_spi_ist) { + ret =vgic_v5_spi_ist_free (kvm); +if (ret) +return ret;
[Severity: Medium]
Similarly, could an error here leak the same resources?
Quoted 2 lines
arch/arm64/kvm/vgic/vgic-v5-tables.c +2
-0+ } + vmi->h_spi_ist =NULL ;
[ ... ]
Quoted 16 lines
arch/arm64/kvm/vgic/vgic-v5-tables.c +12
-0@@ -679,3 +722,511 @@ int vgic_v5_vmte_free_vpe(struct kvm_vcpu *vcpu)return 0 ; } + +/* +* Assign an already allocated IST to the VM by populating the fields in the +* corresponding VMTE. We re-use this code for both an SPI IST and LPI IST, even +* if the paths to reach it might be vastly different. +*/ +static int vgic_v5_vmte_assign_ist (struct kvm *kvm,phys_addr_t ist_base, +bool two_level,unsigned int id_bits, +unsigned int l2sz,unsigned int istsz, +bool spi_ist) +{ +struct kvm_vcpu *vcpu0 =kvm_get_vcpu (kvm,0 );
[ ... ]
Quoted 4 lines
arch/arm64/kvm/vgic/vgic-v5-tables.c +4
-0+ scoped_guard(raw_spinlock_irqsave, &vgic_v5_irs_lock) { +/* Bail if already allocated */ +vgic_v5_clean_inval (vmte,sizeof (*vmte)); + if (le64_to_cpu(READ_ONCE(vmte->val[section])) &
arch/arm64/kvm/vgic/vgic-v5-tables.c int vgic_v5_vmte_free_vpe(struct kvm_vcpu *vcpu) · line 787
Quoted 10 lines
arch/arm64/kvm/vgic/vgic-v5-tables.c +10
-0+ GICV5_VMTEL2E_IST_VALID) +return -EINVAL ; + +WRITE_ONCE (vmte->val[section],cpu_to_le64 (tmp)); +vgic_v5_clean_inval (vmte,sizeof (*vmte)); + } + +/* Finally, mark the entry as valid */ + cmd = spi_ist ?SPI_VIST_MAKE_VALID :LPI_VIST_MAKE_VALID ; + ret =irq_set_vcpu_affinity (vgic_v5_vpe_db (vcpu0), &cmd);
[Severity: High]
What happens if vcpu0 is NULL?
Userspace can invoke KVM VGIC initialization ioctls before any VCPUs are created (where kvm->online_vcpus is 0). In this state, kvm_get_vcpu() returns NULL. When vgic_v5_vpe_db() attempts to dereference vcpu0 to get the doorbell address, it would cause a host kernel panic.
[Severity: High]
This is a pre-existing issue, but does vgic_v5_clean_inval() require a barrier to guarantee completion?
The vgic_v5_clean_inval() function calls dcache_clean_inval_poc() to flush caches for a non-coherent IRS. However, it lacks a trailing dsb(sy) or dsb(ish) barrier. Because of this, the memory-mapped write to the doorbell in irq_set_vcpu_affinity() might execute before the cache flush completes, allowing the IRS hardware to read stale VMTE data.
Quoted 3 lines
arch/arm64/kvm/vgic/vgic-v5-tables.c +3
-0+ +return ret; +}
[ ... ]
Quoted 16 lines
arch/arm64/kvm/vgic/vgic-v5-tables.c +16
-0+/* +* Allocate a Linear IST - always used for SPIs and potentially LPIs. +* +* The calculation for n has been taken from section 11.2.2 of the GICv5 EAC0 +* spec. +* +* NOTE: istsz is the FIELD used by GICv5, not the actual size (or log2() of the +* size). +*/ +static int vgic_v5_alloc_linear_ist (struct kvm *kvm,bool spi_ist, +unsigned int id_bits,unsigned int istsz) +{ +const size_t n =max (5 , id_bits +1 + istsz); +u32 vm_id =vgic_v5_vm_id (kvm); +struct vgic_v5_vm_info *vmi; +__le64 *ist;
arch/arm64/kvm/vgic/vgic-v5-tables.c int vgic_v5_vmte_free_vpe(struct kvm_vcpu *vcpu) · line 817
Quoted 10 lines
arch/arm64/kvm/vgic/vgic-v5-tables.c +10
-0+u32 l1sz; + + vmi =xa_load (&vm_info, vm_id); +if (!vmi) +return -EINVAL ; + +/* +* Allocate the IST. We only have one level, so we just use the L2 ISTE. +*/ + l1sz =BIT (n +1 );
[Severity: Critical]
Can l1sz overflow and truncate to zero?
If a guest requests a large LPI ID space (e.g., id_bits = 30), n can reach 32. BIT(33) evaluates to 8,589,934,592, which truncates to exactly 0 when assigned to the 32-bit u32 l1sz variable.
arch/arm64/kvm/vgic/vgic-v5-tables.c int vgic_v5_vmte_free_vpe(struct kvm_vcpu *vcpu) · line 818
Quoted 1 line
arch/arm64/kvm/vgic/vgic-v5-tables.c +1
-0+ ist =kzalloc (l1sz,GFP_KERNEL_ACCOUNT );
When kzalloc() is called with a size of 0, it returns ZERO_SIZE_PTR (0x10). KVM will then translate this via virt_to_phys() and program the bogus physical address into the hardware's VMTE. The IRS will perform DMA to this address, leading to host memory corruption.
Quoted 2 lines
arch/arm64/kvm/vgic/vgic-v5-tables.c +2
-0+if (!ist) +return -ENOMEM ;
[ ... ]
Quoted 12 lines
arch/arm64/kvm/vgic/vgic-v5-tables.c +12
-0+/* +* Allocate ALL of the second level ISTs for a two-level IST - LPI, only. +* +* The calculation for n has been taken from section 11.2.2 of the GICv5 EAC0 +* spec. The l2_size calculation is from section 11.2.3 of the same document. +* +* NOTE: istsz and l2sz are the FIELDS used by GICv5, not the actual sizes (or +* log2() of the sizes). +*/ +static int vgic_v5_alloc_l2_ists (struct kvm *kvm,unsigned int id_bits, +unsigned int istsz,unsigned int l2sz) +{
[ ... ]
arch/arm64/kvm/vgic/vgic-v5-tables.c int vgic_v5_vmte_free_vpe(struct kvm_vcpu *vcpu) · line 925
Quoted 8 lines
arch/arm64/kvm/vgic/vgic-v5-tables.c +8
-0+/* +* We are not doing on-demand allocation of the L2 ISTs, and are +* instead provisioning the whole IST up front. This means that +* we are able to mark the L2 ISTs as valid in the L1 ISTEs as +* the overall IST is not yet valid. +*/ + val = (virt_to_phys (l2ist) &GICV5_ISTL1E_L2_ADDR_MASK ) | +GICV5_ISTL1E_VALID ;
[Severity: High]
Is it possible for virt_to_phys(l2ist) to have bits above the GICV5_ISTL1E_L2_ADDR_MASK?
Unlike vgic_v5_vmte_assign_ist() which checks if the address fits the hardware mask, this function unconditionally applies a bitwise AND. On systems with LVA/LPA (up to 52-bit PAs), if the allocated page resides above the bits covered by the mask, the upper bits are silently truncated. The IRS would then write incoming LPI states to an aliased, incorrect physical memory address.
Quoted 6 lines
arch/arm64/kvm/vgic/vgic-v5-tables.c +6
-0+ l1ist[index] =cpu_to_le64 (val); + + vmi->h_lpi_l2_ists[index] = l2ist; + +vgic_v5_clean_inval (l2ist, l2_size); + }
[ ... ]
Quoted 9 lines
arch/arm64/kvm/vgic/vgic-v5-tables.c +9
-0+/* +* Free a Linear IST. Can only happen once the VM is dead. +*/ +static int vgic_v5_linear_ist_free (struct kvm *kvm,bool spi) +{ +u32 vm_id =vgic_v5_vm_id (kvm); +struct vmtl2_entry *vmte; +struct vgic_v5_vm_info *vmi; +int section;
[ ... ]
arch/arm64/kvm/vgic/vgic-v5-tables.c int vgic_v5_vmte_free_vpe(struct kvm_vcpu *vcpu) · line 1040
Quoted 13 lines
arch/arm64/kvm/vgic/vgic-v5-tables.c +13
-0+if (spi) { + section =GICV5_VMTEL2_SPI_SECTION ; +vgic_v5_free_allocated_spi_ist (kvm); + }else { + section =GICV5_VMTEL2_LPI_SECTION ; +vgic_v5_free_allocated_lpi_ist (vmi,0 ,0 ,0 ); + } + +/* The VM should be dead here, so we can just zero the VMT section */ + scoped_guard(raw_spinlock_irqsave, &vgic_v5_irs_lock) { +WRITE_ONCE (vmte->val[section],cpu_to_le64 (0 )); +vgic_v5_clean_inval (vmte,sizeof (*vmte)); + }
[Severity: Critical]
Does the hardware IRS need to be notified that the LPI IST is now invalid?
The memory is explicitly freed and the software VMTE is zeroed, but no LPI_VIST_MAKE_INVALID command is sent to the IRS hardware. Because the hardware IRS is never notified, it will retain the physical address in its cache and will continue to DMA incoming LPI state updates to the newly freed host memory pages, leading to a Use-After-Free and host memory corruption.
Quoted 9 lines
arch/arm64/kvm/vgic/vgic-v5-tables.c +9
-0+ +return 0 ; +} + +/* +* Free a Two-Level IST. Can only happen once the VM is dead. +*/ +static int vgic_v5_two_level_ist_free (struct kvm *kvm,bool spi) +{
[ ... ]
arch/arm64/kvm/vgic/vgic-v5-tables.c int vgic_v5_vmte_free_vpe(struct kvm_vcpu *vcpu) · line 1089
Quoted 7 lines
arch/arm64/kvm/vgic/vgic-v5-tables.c +7
-0+vgic_v5_free_allocated_lpi_ist (vmi, id_bits, istsz, l2sz); + +/* The VM must be dead, so we can just zero the VMT section */ + scoped_guard(raw_spinlock_irqsave, &vgic_v5_irs_lock) { +WRITE_ONCE (vmte->val[section],cpu_to_le64 (0 )); +vgic_v5_clean_inval (vmte,sizeof (*vmte)); + }
[Severity: Critical]
Similarly, does the two-level IST teardown also require an LPI_VIST_MAKE_INVALID hardware command?
The LPI IST memory is freed and the VMTE is zeroed without sending the invalidation command to the IRS, opening the same race window for hardware Use-After-Free.
Quoted 3 lines
arch/arm64/kvm/vgic/vgic-v5-tables.c +3
-0+ +return 0 ; +}
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=10
Re: [PATCH v5 10/49] KVM: arm64: gic-v5: Introduce guest IST alloc and management
Quoted 56 lines · On Fri, 2026-08-07 at 13:07 +0000, [email protected] wrote:
Thank you for your contribution! Sashiko AI review found 6 potential issue(s) to consider:
New issues:
- [Critical] Hardware Use-After-Free (UAF) due to missing IRS invalidation command during LPI IST teardown.
- [Critical] Integer truncation invgic_v5_alloc_linear_istcausesZERO_SIZE_PTRallocation and silent memory corruption.
- [High] Unconditional NULL pointer dereference ofvcpu0during IST assignment.
- [High] Silent physical address truncation in L2 IST allocation due to unvalidated masking.
- [Medium] Permanent memory leak of VM resources due to early aborts invgic_v5_vmte_release.Pre-existing issues:
- [High] Missing DSB barrier after cache maintenance allows the hardware IRS to read stale VMTE data.
----- Patch [10]: [PATCH v5 10/49] KVM: arm64: gic-v5: Introduce guest IST alloc and management ---
commit4b81b458fb30bd2b57a30edcbe23a8f8de22edbb
Author: Sascha Bischoff <[email protected]>
Date: Fri Aug 7 11:17:41 2026 +0000KVM: arm64: gic-v5: Introduce guest IST alloc and management
GICv5 guests use Interrupt State Tables (ISTs) to track and manage the
interrupt state for SPIs and LPIs. This commit introduces the machinery
needed to allocate a linear SPI IST and a shadow LPI IST, and pass them
to the host's IRS via the VMTE.Quoted 5 lines
arch/arm64/kvm/vgic/vgic-v5-tables.c +0
-0[ ... ]
arch/arm64/kvm/vgic/vgic-v5-tables.c int vgic_v5_vmte_release(struct kvm *kvm) · line 615
Quoted 9 lines
arch/arm64/kvm/vgic/vgic-v5-tables.c +0
-0@@ -581,6 +608,22 @@ int vgic_v5_vmte_release(struct kvm *kvm) if (!vmi) goto no_vmi;arch/arm64/kvm/vgic/vgic-v5-tables.c +5
-0+/* If we have an LPI IST, free it */ +if (vmi->h_lpi_ist) { + ret =vgic_v5_lpi_ist_free (kvm); +if (ret) +return ret;[Severity: Medium]
What happens to the remaining allocated resources if this returns early?
Quoted 6 lines
If
vgic_v5_lpi_ist_free()returns an error, the function aborts immediately.
This bypasses the release ofvped_base,vpet_base, andvmd_base, and skips
removing the entry fromvm_info, permanently leaking these VM resources.
That's true, but what else can we really do? If we've failed to free the IST here, then we're in a very broken state. The hardware still has a handle to it, and therefore could use the memory. Unless we can be sure that this can't happen, we can't continue the teardown. This memory, and other associated VM structures need to remain in place until we're sure that the hardware won't go and access them.
Quoted 12 lines
arch/arm64/kvm/vgic/vgic-v5-tables.c int vgic_v5_vmte_release(struct kvm *kvm) · line 623
Quoted 9 lines
arch/arm64/kvm/vgic/vgic-v5-tables.c +8
-0+ } + vmi->h_lpi_ist =NULL ; + +/* If we have an SPI IST, free it */ +if (vmi->h_spi_ist) { + ret =vgic_v5_spi_ist_free (kvm); +if (ret) +return ret;[Severity: Medium]
Similarly, could an error here leak the same resources?
Yes, and that needs to be the case. See above.
Quoted 36 lines
Quoted 3 lines
arch/arm64/kvm/vgic/vgic-v5-tables.c +2
-0+ } + vmi->h_spi_ist =NULL ;[ ... ]
Quoted 22 lines
arch/arm64/kvm/vgic/vgic-v5-tables.c +0
-0@@ -679,3 +722,511 @@ int vgic_v5_vmte_free_vpe(struct kvm_vcpu *vcpu)
return 0;
}arch/arm64/kvm/vgic/vgic-v5-tables.c +3
-0+ +/* +* Assign an already allocated IST to the VM by populating the fields in the
+ * corresponding VMTE. We re-use this code for both an SPI IST and LPI IST, even
+ * if the paths to reach it might be vastly different.
+ */
+static int vgic_v5_vmte_assign_ist(struct kvm *kvm,phys_addr_tist_base,arch/arm64/kvm/vgic/vgic-v5-tables.c +1
-0+ bool two_level, unsigned int
id_bits,arch/arm64/kvm/vgic/vgic-v5-tables.c +1
-0+ unsigned int l2sz, unsigned intistsz,
arch/arm64/kvm/vgic/vgic-v5-tables.c +3
-0+ bool spi_ist) +{ +struct kvm_vcpu *vcpu0 =kvm_get_vcpu (kvm,0 );[ ... ]
Quoted 9 lines
arch/arm64/kvm/vgic/vgic-v5-tables.c +9
-0+ scoped_guard(raw_spinlock_irqsave, &vgic_v5_irs_lock) { +/* Bail if already allocated */ +vgic_v5_clean_inval (vmte,sizeof (*vmte)); +if (le64_to_cpu (READ_ONCE (vmte->val[section])) & + GICV5_VMTEL2E_IST_VALID) +return -EINVAL ; + +WRITE_ONCE (vmte->val[section],cpu_to_le64 (tmp)); +vgic_v5_clean_inval (vmte,sizeof (*vmte));
Quoted 8 lines
arch/arm64/kvm/vgic/vgic-v5-tables.c int vgic_v5_vmte_free_vpe(struct kvm_vcpu *vcpu) · line 787
Quoted 5 lines
arch/arm64/kvm/vgic/vgic-v5-tables.c +5
-0+ } + +/* Finally, mark the entry as valid */ + cmd = spi_ist ?SPI_VIST_MAKE_VALID :LPI_VIST_MAKE_VALID ; + ret =irq_set_vcpu_affinity (vgic_v5_vpe_db (vcpu0), &cmd);[Severity: High]
What happens if vcpu0 is NULL?
We cannot make it this far without a vcpu. We must have at least one, and therefore we use the first one.
Quoted 8 lines
Userspace can invoke KVM VGIC initialization ioctls before any VCPUs are
created (wherekvm->online_vcpusis 0). In this state,kvm_get_vcpu()returns
NULL. Whenvgic_v5_vpe_db()attempts to dereference vcpu0 to get the doorbell
address, it would cause a host kernel panic.
We block that in vgic_v5_init() so, no, we cannot make it this far.
Quoted 5 lines
[Severity: High]
This is a pre-existing issue, but doesvgic_v5_clean_inval()require a barrier
to guarantee completion?
It does, and it has it, albeit implicitly. dcache_clean_inval_poc() uses dcache_by_line_op, which itself ends with dsb sy.
Quoted 40 lines
The
vgic_v5_clean_inval()function callsdcache_clean_inval_poc()to flush
caches for a non-coherent IRS. However, it lacks a trailing dsb(sy) or dsb(ish)
barrier. Because of this, the memory-mapped write to the doorbell inirq_set_vcpu_affinity()might execute before the cache flush completes,
allowing the IRS hardware to read stale VMTE data.Quoted 3 lines
arch/arm64/kvm/vgic/vgic-v5-tables.c +3
-0+ +return ret; +}[ ... ]
Quoted 26 lines
+/*
+ * Allocate a Linear IST - always used for SPIs and potentially LPIs.
+ *
+ * The calculation for n has been taken from section 11.2.2 of the GICv5 EAC0
+ * spec.
+ *
+ * NOTE: istsz is the FIELD used by GICv5, not the actual size (orlog2()of the
+ * size).
+ */arch/arm64/kvm/vgic/vgic-v5-tables.c +2
-0+static int vgic_v5_alloc_linear_ist (struct kvm *kvm,bool spi_ist, + unsigned int id_bits, unsignedint istsz)
arch/arm64/kvm/vgic/vgic-v5-tables.c +11
-0+{ +const size_t n =max (5 , id_bits +1 + istsz); +u32 vm_id =vgic_v5_vm_id (kvm); +struct vgic_v5_vm_info *vmi; +__le64 *ist; +u32 l1sz; + + vmi =xa_load (&vm_info, vm_id); +if (!vmi) +return -EINVAL ; +
Quoted 8 lines
arch/arm64/kvm/vgic/vgic-v5-tables.c
Quoted 5 lines
arch/arm64/kvm/vgic/vgic-v5-tables.c +2
-0+/* +* Allocate the IST. We only have one level, so we just use the L2 ISTE.
arch/arm64/kvm/vgic/vgic-v5-tables.c +2
-0+*/ + l1sz =BIT (n +1 );[Severity: Critical]
Can l1sz overflow and truncate to zero?
No.
Quoted 3 lines
If a guest requests a large LPI ID space (e.g.,
id_bits= 30), n can reach 32.
We limit the guest to 16-bits of ID space.
Quoted 4 lines
BIT(33) evaluates to 8,589,934,592, which truncates to exactly 0 when assigned
to the 32-bit u32 l1sz variable.
Quoted 9 lines
arch/arm64/kvm/vgic/vgic-v5-tables.c int vgic_v5_vmte_free_vpe(struct kvm_vcpu *vcpu) · line 818
Quoted 1 line
arch/arm64/kvm/vgic/vgic-v5-tables.c +1
-0+ ist =kzalloc (l1sz,GFP_KERNEL_ACCOUNT );When
kzalloc()is called with a size of 0, it returnsZERO_SIZE_PTR(0x10).
KVM will then translate this viavirt_to_phys()and program the bogus physical
address into the hardware's VMTE. The IRS will perform DMA to this address,
leading to host memory corruption.
Things are limited such that this cannot happen.
Quoted 40 lines
Quoted 3 lines
arch/arm64/kvm/vgic/vgic-v5-tables.c +2
-0+if (!ist) +return -ENOMEM ;[ ... ]
Quoted 18 lines
+/*
+ * Allocate ALL of the second level ISTs for a two-level IST - LPI, only.
+ *
+ * The calculation for n has been taken from section 11.2.2 of the GICv5 EAC0
+ * spec. Thel2_sizecalculation is from section 11.2.3 of the same document.
+ *
+ * NOTE: istsz and l2sz are the FIELDS used by GICv5, not the actual sizes (or
+ *log2()of the sizes).
+ */
+static int vgic_v5_alloc_l2_ists(struct kvm *kvm, unsigned intid_bits,arch/arm64/kvm/vgic/vgic-v5-tables.c +1
-0+ unsigned int istsz, unsigned intl2sz)
+{[ ... ]
arch/arm64/kvm/vgic/vgic-v5-tables.c int vgic_v5_vmte_free_vpe(struct kvm_vcpu *vcpu) · line 925
Quoted 12 lines
arch/arm64/kvm/vgic/vgic-v5-tables.c +2
-0+/* +* We are not doing on-demand allocation of the L2 ISTs, and are
arch/arm64/kvm/vgic/vgic-v5-tables.c +1
-0+* instead provisioning the whole IST up front. This means that
arch/arm64/kvm/vgic/vgic-v5-tables.c +1
-0+* we are able to mark the L2 ISTs as valid in the L1 ISTEs as
arch/arm64/kvm/vgic/vgic-v5-tables.c +3
-0+* the overall IST is not yet valid. +*/ + val = (virt_to_phys(l2ist) &
GICV5_ISTL1E_L2_ADDR_MASK) |arch/arm64/kvm/vgic/vgic-v5-tables.c +1
-0+ GICV5_ISTL1E_VALID;[Severity: High]
Is it possible forvirt_to_phys(l2ist)to have bits above theGICV5_ISTL1E_L2_ADDR_MASK?
Quoted 10 lines
Unlike
vgic_v5_vmte_assign_ist()which checks if the address fits the hardware
mask, this function unconditionally applies a bitwise AND. On systems with
LVA/LPA (up to 52-bit PAs), if the allocated page resides above the bits
covered by the mask, the upper bits are silently truncated. The IRS would then
write incoming LPI states to an aliased, incorrect physical memory address.
Did you check the mask? It goes up to bit 55 - it supports 56-bit addresses. Not an issue.
Quoted 28 lines
Quoted 7 lines
arch/arm64/kvm/vgic/vgic-v5-tables.c +6
-0+ l1ist[index] =cpu_to_le64 (val); + + vmi->h_lpi_l2_ists[index] = l2ist; + +vgic_v5_clean_inval (l2ist, l2_size); + }[ ... ]
Quoted 9 lines
arch/arm64/kvm/vgic/vgic-v5-tables.c +9
-0+/* +* Free a Linear IST. Can only happen once the VM is dead. +*/ +static int vgic_v5_linear_ist_free (struct kvm *kvm,bool spi) +{ +u32 vm_id =vgic_v5_vm_id (kvm); +struct vmtl2_entry *vmte; +struct vgic_v5_vm_info *vmi; +int section;[ ... ]
Quoted 10 lines
arch/arm64/kvm/vgic/vgic-v5-tables.c +9
-0+if (spi) { + section =GICV5_VMTEL2_SPI_SECTION ; +vgic_v5_free_allocated_spi_ist (kvm); + }else { + section =GICV5_VMTEL2_LPI_SECTION ; +vgic_v5_free_allocated_lpi_ist (vmi,0 ,0 ,0 ); + } + +/* The VM should be dead here, so we can just zero the VMT section */
Quoted 8 lines
arch/arm64/kvm/vgic/vgic-v5-tables.c int vgic_v5_vmte_free_vpe(struct kvm_vcpu *vcpu) · line 1040
Quoted 4 lines
arch/arm64/kvm/vgic/vgic-v5-tables.c +4
-0+ scoped_guard(raw_spinlock_irqsave, &vgic_v5_irs_lock) { +WRITE_ONCE (vmte->val[section],cpu_to_le64 (0 )); +vgic_v5_clean_inval (vmte,sizeof (*vmte)); + }[Severity: Critical]
Does the hardware IRS need to be notified that the LPI IST is now invalid?
Not if the VM is invalid. Once the VM is invalid, the hardware is not allowed to access anything that is referenced by the VMTE.
Quoted 21 lines
The memory is explicitly freed and the software VMTE is zeroed, but no
LPI_VIST_MAKE_INVALIDcommand is sent to the IRS hardware. Because the hardware
IRS is never notified, it will retain the physical address in its cache and
will continue to DMA incoming LPI state updates to the newly freed host memory
pages, leading to a Use-After-Free and host memory corruption.Quoted 9 lines
arch/arm64/kvm/vgic/vgic-v5-tables.c +9
-0+ +return 0 ; +} + +/* +* Free a Two-Level IST. Can only happen once the VM is dead. +*/ +static int vgic_v5_two_level_ist_free (struct kvm *kvm,bool spi) +{[ ... ]
Quoted 12 lines
arch/arm64/kvm/vgic/vgic-v5-tables.c int vgic_v5_vmte_free_vpe(struct kvm_vcpu *vcpu) · line 1040
Quoted 8 lines
arch/arm64/kvm/vgic/vgic-v5-tables.c +3
-0+vgic_v5_free_allocated_lpi_ist (vmi, id_bits, istsz, l2sz); + +/* The VM must be dead, so we can just zero the VMT section */
arch/arm64/kvm/vgic/vgic-v5-tables.c +4
-0+ scoped_guard(raw_spinlock_irqsave, &vgic_v5_irs_lock) { +WRITE_ONCE (vmte->val[section],cpu_to_le64 (0 )); +vgic_v5_clean_inval (vmte,sizeof (*vmte)); + }[Severity: Critical]
Similarly, does the two-level IST teardown also require anLPI_VIST_MAKE_INVALIDhardware command?
As above, no.
Quoted 11 lines
The LPI IST memory is freed and the VMTE is zeroed without sending the
invalidation command to the IRS, opening the same race window for hardware
Use-After-Free.arch/arm64/kvm/vgic/vgic-v5-tables.c int vgic_v5_vmte_free_vpe(struct kvm_vcpu *vcpu) · line 832
Quoted 3 lines
arch/arm64/kvm/vgic/vgic-v5-tables.c +3
-0+ +return 0 ; +}
Thanks,
Sascha
PATCH v5 11/49 KVM: arm64: gic-v5: Implement VMT/vIST IRS MMIO Ops
In reply to Sascha BischoffGICv5 has rules about which fields of a VMTE (or L1 VMT) may be directly written by the host once the table is valid. This ensures that no stale state is cached by the hardware, and provides a clear interface for making VMs, ISTs, etc. valid.
The hypervisor is responsible for populating the VMTE for a VM. However, it is not permitted to write the Valid bit, as the VM table is already valid. Instead, the VM is made valid via an IRS MMIO operation. The same applies to the ISTs: they must be made valid via the host IRS.
This commit adds support for:
* Making level 1 VMTs valid, allowing for dynamic level 2 array allocation
* Making VMTEs, and hence VMs, valid or invalid
* Making SPI ISTs valid, and LPI ISTs valid or invalid, for a specific VM
As part of this commit, the following vcpu_affinity-based commands are plumbed in:
VMT_L2_MAP - Make a second level VM table valid VMTE_MAKE_VALID - Make a single VMTE, and hence VM, valid VMTE_MAKE_INVALID - Make a single VMTE, and hence VM, invalid SPI_VIST_MAKE_VALID - Make the SPI IST valid LPI_VIST_MAKE_VALID - Make the LPI IST valid LPI_VIST_MAKE_INVALID - Make the LPI IST invalid
The lack of SPI_VIST_MAKE_INVALID is intentional.
When successfully probing for a GICv5, allocate the VMT and make it valid through the IRS MMIO interface. Treat failures while allocating or assigning the VMT as hard GICv5 probe failures. At that point the IRS VM table state is a prerequisite for vGICv5 operation, and falling back to the legacy path would leave the host without a valid GICv5 VM table setup.
Conversely, if vGICv5 registration succeeds but registration of the optional legacy vGICv3 device fails, retain the working vGICv5 device rather than failing the entire probe.
--- arch/arm64/kvm/vgic/vgic-v5-tables.c | 56 +++++--- arch/arm64/kvm/vgic/vgic-v5-tables.h | 1 + arch/arm64/kvm/vgic/vgic-v5.c | 192 ++++++++++++++++++++++++++- 3 files changed, 225 insertions(+), 24 deletions(-)
arch/arm64/kvm/vgic/vgic-v5-tables.c +36 -20
@@ -73,6 +73,21 @@ static DEFINE_XARRAY(vm_info);#define GICV5_VMTEL2_LPI_SECTION 2#define GICV5_VMTEL2_SPI_SECTION 3 +static int vgic_v5_alloc_linear_ist (struct kvm *kvm,bool spi_ist, +unsigned int id_bits, +unsigned int istsz); +static int vgic_v5_alloc_l1_ist (struct kvm *kvm,unsigned int id_bits, +unsigned int istsz,unsigned int l2_split); +static int vgic_v5_alloc_l2_ists (struct kvm *kvm,unsigned int id_bits, +unsigned int istsz,unsigned int l2_split); +static int vgic_v5_alloc_two_level_lpi_ist (struct kvm *kvm, +unsigned int id_bits, +unsigned int istsz, +unsigned int l2_split); +static int vgic_v5_linear_ist_free (struct kvm *kvm,bool spi); +static int vgic_v5_two_level_ist_free (struct kvm *kvm,bool spi); +static int vgic_v5_spi_ist_free (struct kvm *kvm); +/* * Our IRS might be coherent or non-coherent. If coherent, we can just emit a * DSB to ensure that we're in sync. However, when non-coherent, we need to
@@ -561,25 +576,6 @@ int vgic_v5_vmte_init(struct kvm *kvm)return ret; } -/* -* The following set of forward declarations makes the code layout a *little* -* clearer as it lets us keep the IST-related code together. -*/ -static int vgic_v5_alloc_linear_ist (struct kvm *kvm,bool spi_ist, -unsigned int id_bits, -unsigned int istsz); -static int vgic_v5_alloc_l1_ist (struct kvm *kvm,unsigned int id_bits, -unsigned int istsz,unsigned int l2_split); -static int vgic_v5_alloc_l2_ists (struct kvm *kvm,unsigned int id_bits, -unsigned int istsz,unsigned int l2_split); -static int vgic_v5_alloc_two_level_lpi_ist (struct kvm *kvm, -unsigned int id_bits, -unsigned int istsz, -unsigned int l2_split); -static int vgic_v5_linear_ist_free (struct kvm *kvm,bool spi); -static int vgic_v5_two_level_ist_free (struct kvm *kvm,bool spi); -static int vgic_v5_spi_ist_free (struct kvm *kvm); -/* * Release the VMT Entry, freeing up any allocated data structures before * zeroing the VMTE.
@@ -723,6 +719,18 @@ int vgic_v5_vmte_free_vpe(struct kvm_vcpu *vcpu)return 0 ; } +phys_addr_t vgic_v5_get_vmt_base (void ) +{ +phys_addr_t vmt_base; + +if (!vmt_info->two_level) + vmt_base =virt_to_phys (vmt_info->linear.vmt_base); +else + vmt_base =virt_to_phys (vmt_info->l2.vmt_base); + +return vmt_base; +} +/* * Assign an already allocated IST to the VM by populating the fields in the * corresponding VMTE. We re-use this code for both an SPI IST and LPI IST, even
@@ -785,8 +793,16 @@ static int vgic_v5_vmte_assign_ist(struct kvm *kvm, phys_addr_t ist_base,/* Finally, mark the entry as valid */ cmd = spi_ist ?SPI_VIST_MAKE_VALID :LPI_VIST_MAKE_VALID ; ret =irq_set_vcpu_affinity (vgic_v5_vpe_db (vcpu0), &cmd); +if (ret) { + scoped_guard(raw_spinlock_irqsave, &vgic_v5_irs_lock) { +WRITE_ONCE (vmte->val[section],0ULL ); +vgic_v5_clean_inval (vmte,sizeof (*vmte)); + } -return ret; +return ret; + } + +return 0 ; }/*
arch/arm64/kvm/vgic/vgic-v5-tables.h +1 -0
@@ -106,6 +106,7 @@ extern raw_spinlock_t vgic_v5_irs_lock;int vgic_v5_vmt_allocate (unsigned int max_vpes);int vgic_v5_vmt_free (void ); +phys_addr_t vgic_v5_get_vmt_base (void );int vgic_v5_allocate_vm_id (struct kvm *kvm);void vgic_v5_release_vm_id (struct kvm *kvm);
arch/arm64/kvm/vgic/vgic-v5.c +188 -4
251 lines not on this page · raw message
Signature · 1 line
2.34.1
[email protected]
AI reviewRe: [PATCH v5 11/49] KVM: arm64: gic-v5: Implement VMT/vIST IRS MMIO Ops
Thank you for your contribution! Sashiko AI review found 5 potential issue(s) to consider: - [High] Unconditional NULL pointer dereference in `vgic_v5_alloc_l2_vmt` when the VM has zero vCPUs.…
Thank you for your contribution! Sashiko AI review found 5 potential issue(s) to consider:
- [High] Unconditional NULL pointer dereference in vgic_v5_alloc_l2_vmt when the VM has zero vCPUs.
- [High] Hardware Use-After-Free (UAF) when IST validation times out.
- [High] GICv5 probe permanently aborts on kexec if the previous kernel left the VMT_BASER register valid.
- [High] Missing bounds check on max_vpe_id allows userspace to program out-of-bounds vpe_id_bits into hardware.
- [Low] Mixing goto-based error handling with scoped_guard() violates the cleanup subsystem guidelines. --
--- Patch [11]: [PATCH v5 11/49] KVM: arm64: gic-v5: Implement VMT/vIST IRS MMIO Ops --- commit 47ef012a3a648a37c053cce44ccc9973a562fddf
Author: Sascha Bischoff <[email protected]>
KVM: arm64: gic-v5: Implement VMT/vIST IRS MMIO Ops
This commit adds support for making GICv5 VM and IST tables valid via the IRS MMIO interface. It introduces functions to interact with IRS hardware and manage VM/IST validity.
Quoted 3 lines
arch/arm64/kvm/vgic/vgic-v5-tables.c +0
-0
[ ... ]
arch/arm64/kvm/vgic/vgic-v5-tables.c static int vgic_v5_vmte_assign_ist(struct kvm *kvm, phys_addr_t ist_base, · line 795
Quoted 4 lines
arch/arm64/kvm/vgic/vgic-v5-tables.c +0
-0@@ -785,8 +793,16 @@ static int vgic_v5_vmte_assign_ist(struct kvm *kvm, phys_addr_t ist_base,/* Finally, mark the entry as valid */ cmd = spi_ist ?SPI_VIST_MAKE_VALID :LPI_VIST_MAKE_VALID ; ret =irq_set_vcpu_affinity (vgic_v5_vpe_db (vcpu0), &cmd);
[Severity: High]
Can this result in a null pointer dereference if the VM has zero vCPUs?
The vcpu0 pointer is retrieved via kvm_get_vcpu(kvm, 0), which can return null if kvm->online_vcpus == 0. Unconditionally passing vcpu0 to vgic_v5_vpe_db() here, and similarly in vgic_v5_alloc_l2_vmt(), seems like it could trigger a host crash if invoked by KVM initialization ioctls before any vCPUs are created.
arch/arm64/kvm/vgic/vgic-v5-tables.c static int vgic_v5_vmte_assign_ist(struct kvm *kvm, phys_addr_t ist_base, · line 802
Quoted 8 lines
arch/arm64/kvm/vgic/vgic-v5-tables.c +6
-1+if (ret) { + scoped_guard(raw_spinlock_irqsave, &vgic_v5_irs_lock) { +WRITE_ONCE (vmte->val[section],0ULL ); +vgic_v5_clean_inval (vmte,sizeof (*vmte)); + } -return ret; +return ret;
[Severity: High]
Could this lead to a hardware use-after-free if the validation command times out?
If the MMIO command sent in vgic_v5_vmte_assign_ist() times out, it returns an error here. Callers like vgic_v5_spi_ist_alloc() and vgic_v5_lpi_ist_alloc() will react by freeing the IST memory:
arch/arm64/kvm/vgic/vgic-v5-tables.c:vgic_v5_spi_ist_alloc() {
...
if (ret) {
vgic_v5_free_allocated_spi_ist(kvm);
return ret;
}
...
}Since there is no SPI_VIST_MAKE_INVALID command, freeing this memory while the hardware is still processing the command (or caching the memory pointer) might corrupt host memory. Should KVM intentionally leak the memory on timeout instead?
Quoted 9 lines
arch/arm64/kvm/vgic/vgic-v5-tables.c +3
-0+ } + +return 0 ; }/* arch/arm64/kvm/vgic/vgic-v5.c +0
-0
[ ... ]
Quoted 4 lines
arch/arm64/kvm/vgic/vgic-v5.c +0
-0@@ -83,13 +102,52 @@ int vgic_v5_probe(const struct gic_kvm_info *info)vgic_v5_irs_cache_id_regs (info);vgic_v5_get_implemented_ppis ();
[ ... ]
arch/arm64/kvm/vgic/vgic-v5.c int vgic_v5_probe(const struct gic_kvm_info *info) · line 139
Quoted 9 lines
arch/arm64/kvm/vgic/vgic-v5.c +9
-0+ ret =vgic_v5_irs_assign_vmt (vgic_v5_irs_two_level_vmt_support (&irs_caps), +ilog2 (vgic_v5_irs_max_vms (&irs_caps)), +vgic_v5_get_vmt_base ()); +if (ret) { +kvm_err ("Failed to assign the GICv5 VM tables to the IRS; no GICv5 support\n" ); +if (!vgic_v5_irs_clear_vmt ()) +vgic_v5_vmt_free (); +return -ENODEV ; + }
[Severity: High]
Will this permanently abort the GICv5 probe across kexec?
If booting via kexec while a VM was running in the previous kernel, the hardware VMT_BASER register retains its valid state. When this occurs, vgic_v5_irs_assign_vmt() unconditionally returns -EBUSY:
arch/arm64/kvm/vgic/vgic-v5.c:vgic_v5_irs_assign_vmt() {
...
vmt_baser = irs_readq_relaxed(GICV5_IRS_VMT_BASER);
if (!!FIELD_GET(GICV5_IRS_VMT_BASER_VALID, vmt_baser))
return -EBUSY;
...
}This error path clears the stale state using vgic_v5_irs_clear_vmt(), but then returns -ENODEV. Should the probe retry the assignment instead of failing entirely?
[ ... ]
While reviewing the VM table setup, I also noticed a few potential regressions in vgic_v5_vmte_init():
arch/arm64/kvm/vgic/vgic-v5-tables.c:vgic_v5_vmte_init() {
...
kvm_for_each_vcpu(i, vcpu, kvm) {
u16 vpe_id = vgic_v5_vpe_id(vcpu);
if (vpe_id > max_vpe_id)
max_vpe_id = vpe_id;
}
nr_cpus = max(2UL, roundup_pow_of_two(max_vpe_id + 1));
vmi->vpe_id_bits = fls(nr_cpus) - 1;
...
tmp |= FIELD_PREP(GICV5_VMTEL2E_VPE_ID_BITS, vmi->vpe_id_bits);
vmte_val1 = tmp;
...
}[Severity: High]
Is there a missing bounds check on max_vpe_id here?
The function verifies the total number of online vCPUs against hardware limits, but calculates the hardware configuration based on the maximum sparse vcpu_id. Since KVM allows sparse IDs up to KVM_MAX_VCPU_IDS, an unprivileged userspace could create a single vCPU with a large ID.
This allows calculating an oversized vpe_id_bits and writing it directly to the VMTE, exposing a configuration to the hardware that exceeds its supported capabilities. Should max_vpe_id be checked against vmt_info->max_vpes?
Additionally, in the same function:
arch/arm64/kvm/vgic/vgic-v5-tables.c:vgic_v5_vmte_init() {
...
scoped_guard(raw_spinlock_irqsave, &vgic_v5_irs_lock) {
WRITE_ONCE(vmte->val[0], cpu_to_le64(vmte_val0));
...
}
...
out_fail:
kfree(vmd);
...
}[Severity: Low]
Does this mix goto-based error handling with scoped_guard()?
According to the cleanup subsystem guidelines, if a function uses guard() or scoped_guard(), all resources should be converted to scope-based cleanup, avoiding mixed usage with goto-based cleanup labels.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=11
Re: [PATCH v5 11/49] KVM: arm64: gic-v5: Implement VMT/vIST IRS MMIO Ops
Quoted 29 lines · On Fri, 2026-08-07 at 13:13 +0000, [email protected] wrote:
Thank you for your contribution! Sashiko AI review found 5 potential issue(s) to consider:
- [High] Unconditional NULL pointer dereference invgic_v5_alloc_l2_vmtwhen the VM has zero vCPUs.
- [High] Hardware Use-After-Free (UAF) when IST validation times out.
- [High] GICv5 probe permanently aborts on kexec if the previous kernel left theVMT_BASERregister valid.
- [High] Missing bounds check onmax_vpe_idallows userspace to program out-of-boundsvpe_id_bitsinto hardware.
- [Low] Mixing goto-based error handling withscoped_guard()violates the cleanup subsystem guidelines.
----- Patch [11]: [PATCH v5 11/49] KVM: arm64: gic-v5: Implement VMT/vIST IRS MMIO Ops ---
commit47ef012a3a648a37c053cce44ccc9973a562fddf
Author: Sascha Bischoff <[email protected]>KVM: arm64: gic-v5: Implement VMT/vIST IRS MMIO Ops
This commit adds support for making GICv5 VM and IST tables valid via the IRS MMIO interface. It introduces functions to interact with IRS hardware and manage VM/IST validity.Quoted 4 lines
arch/arm64/kvm/vgic/vgic-v5-tables.c +0
-0[ ... ]
Quoted 9 lines
arch/arm64/kvm/vgic/vgic-v5-tables.c static int vgic_v5_vmte_assign_ist(struct kvm · line 796
Quoted 5 lines
arch/arm64/kvm/vgic/vgic-v5-tables.c +0
-0@@ -785,8 +793,16 @@ static int vgic_v5_vmte_assign_ist(struct kvm *kvm, phys_addr_t ist_base, /* Finally, mark the entry as valid */ cmd = spi_ist ? SPI_VIST_MAKE_VALID : LPI_VIST_MAKE_VALID; ret = irq_set_vcpu_affinity(vgic_v5_vpe_db(vcpu0), &cmd);[Severity: High]
Can this result in a null pointer dereference if the VM has zero vCPUs?
No. We don't allow for 0 VCPUs.
Quoted 30 lines
The vcpu0 pointer is retrieved via
kvm_get_vcpu(kvm, 0), which can return
null ifkvm->online_vcpus== 0. Unconditionally passing vcpu0 tovgic_v5_vpe_db()here, and similarly invgic_v5_alloc_l2_vmt(), seems like
it could trigger a host crash if invoked by KVM initialization ioctls before
any vCPUs are created.arch/arm64/kvm/vgic/vgic-v5-tables.c static int vgic_v5_vmte_assign_ist(struct kvm *kvm, phys_addr_t ist_base, · line 802
Quoted 9 lines
arch/arm64/kvm/vgic/vgic-v5-tables.c +5
-0+if (ret) { +scoped_guard (raw_spinlock_irqsave, &vgic_v5_irs_lock) { +WRITE_ONCE (vmte->val[section],0ULL ); +vgic_v5_clean_inval (vmte,sizeof (*vmte)); + }arch/arm64/kvm/vgic/vgic-v5-tables.c +1
-1-return ret; +return ret;[Severity: High]
Could this lead to a hardware use-after-free if the validation command times
out?
If the MMIO command sent invgic_v5_vmte_assign_ist()times out, it returns
an error here. Callers likevgic_v5_spi_ist_alloc()andvgic_v5_lpi_ist_alloc()will react by freeing the IST memory:
Quoted 16 lines
arch/arm64/kvm/vgic/vgic-v5-tables.c:vgic_v5_spi_ist_alloc() {
...
if (ret) {
vgic_v5_free_allocated_spi_ist(kvm);
return ret;
}
...
}
Since there is noSPI_VIST_MAKE_INVALIDcommand, freeing this memory while
the hardware is still processing the command (or caching the memory pointer)
might corrupt host memory. Should KVM intentionally leak the memory on
timeout instead?
Yeah, I've made that change. It makes more sense to leak the memory if it is possible that the hardware can still access it.
Quoted 18 lines
Quoted 11 lines
arch/arm64/kvm/vgic/vgic-v5-tables.c +3
-0+ } + +return 0 ;}
/*arch/arm64/kvm/vgic/vgic-v5.c +0
-0[ ... ]
Quoted 5 lines
arch/arm64/kvm/vgic/vgic-v5.c +0
-0@@ -83,13 +102,52 @@ int vgic_v5_probe(const struct gic_kvm_info *info) vgic_v5_irs_cache_id_regs(info); vgic_v5_get_implemented_ppis();[ ... ]
Quoted 16 lines
arch/arm64/kvm/vgic/vgic-v5.c int vgic_v5_probe(const struct gic_kvm_info *info) · line 139
Quoted 13 lines
arch/arm64/kvm/vgic/vgic-v5.c +1
-0+ ret =vgic_v5_irs_assign_vmt(vgic_v5_irs_two_level_vmt_support(&irs_caps) ,
arch/arm64/kvm/vgic/vgic-v5.c +1
-0+ilog2(vgic_v5_irs_max_vms(&irs_caps)),
arch/arm64/kvm/vgic/vgic-v5.c +3
-0+ vgic_v5_get_vmt_base()); +if (ret) { + kvm_err("Failed to assign the GICv5 VM tables tothe IRS; no GICv5 support\n");
arch/arm64/kvm/vgic/vgic-v5.c +4
-0+if (!vgic_v5_irs_clear_vmt ()) +vgic_v5_vmt_free (); +return -ENODEV ; + }[Severity: High]
Will this permanently abort the GICv5 probe across kexec?
It will, but as things stand we can't properly cope with kexec in the host driver either. This does need addressing long-term, but I think that the scope is beyond this series and that it should be addressed later.
Quoted 15 lines
If booting via kexec while a VM was running in the previous kernel, the
hardwareVMT_BASERregister retains its valid state. When this occurs,vgic_v5_irs_assign_vmt()unconditionally returns-EBUSY:arch/arm64/kvm/vgic/vgic-v5.c:vgic_v5_irs_assign_vmt() {
...
vmt_baser=irs_readq_relaxed(GICV5_IRS_VMT_BASER); if (!!FIELD_GET(GICV5_IRS_VMT_BASER_VALID,vmt_baser)) return-EBUSY;
...
}
Quoted 6 lines
This error path clears the stale state using
vgic_v5_irs_clear_vmt(), but
then returns-ENODEV. Should the probe retry the assignment instead of
failing entirely?
I think that this is fine for the time being given that we don't strictly support kexec.
Quoted 33 lines
[ ... ]
While reviewing the VM table setup, I also noticed a few potential regressions
invgic_v5_vmte_init():arch/arm64/kvm/vgic/vgic-v5-tables.c:vgic_v5_vmte_init() {
...
kvm_for_each_vcpu(i, vcpu, kvm){
u16vpe_id=vgic_v5_vpe_id(vcpu);
if (vpe_id>max_vpe_id)
max_vpe_id=vpe_id;
}
nr_cpus=max(2UL, roundup_pow_of_two(max_vpe_id + 1));vmi->vpe_id_bits=fls(nr_cpus)- 1;
...
tmp |=FIELD_PREP(GICV5_VMTEL2E_VPE_ID_BITS, vmi->vpe_id_bits);vmte_val1= tmp;
...
}[Severity: High]
Is there a missing bounds check onmax_vpe_idhere?The function verifies the total number of online vCPUs against hardware
limits, but calculates the hardware configuration based on the maximum sparsevcpu_id. Since KVM allows sparse IDs up toKVM_MAX_VCPU_IDS, an unprivileged
userspace could create a single vCPU with a large ID.
Quoted 6 lines
This allows calculating an oversized
vpe_id_bitsand writing it directly to
the VMTE, exposing a configuration to the hardware that exceeds its supported
capabilities. Shouldmax_vpe_idbe checked against vmt_info-Quoted 1 line
max_vpes?
A limit is imposed by a later commit in the series. As this stage in the series, this code isn't reachable, so this isn't an issue. With the subsequent change, the ID is capped so avoid it going above the maximum supported number of VPEs.
Quoted 15 lines
Additionally, in the same function:
arch/arm64/kvm/vgic/vgic-v5-tables.c:vgic_v5_vmte_init() {
...
scoped_guard(raw_spinlock_irqsave, &vgic_v5_irs_lock){WRITE_ONCE(vmte->val[0], cpu_to_le64(vmte_val0));
...
}
...out_fail:
kfree(vmd);
...
}
Quoted 9 lines
[Severity: Low]
Does this mix goto-based error handling withscoped_guard()?According to the cleanup subsystem guidelines, if a function uses
guard()orscoped_guard(), all resources should be converted to scope-based cleanup,
avoiding mixed usage with goto-based cleanup labels.
I don't think that this is a valid concern. The scoped guard is totally unrelated to the goto-based cleanup.
Thanks,
Sascha
PATCH v5 12/49 KVM: arm64: gic-v5: Keep GICv5 vCPU limit model-specific
In reply to Sascha BischoffA GICv5 host with FEAT_GCIE_LEGACY can expose either a native vGICv5 or a vGICv3 device. These models do not necessarily have the same vCPU
limit: the native GICv5 limit is probed from the IRS VPE capacity, while the GICv3 limit remains the fixed KVM vGICv3 limit.
Keep the IRS-derived limit separately for vGICv5 creation. The pre-VGIC KVM_CAP_MAX_VCPUS value continues to expose the largest limit among the still-selectable models, and kvm_vgic_create() clamps the VM to the limit of the VGIC model userspace actually selected.
GICv5 uses the userspace-provided vcpu_id as the index into the VPET, and therefore as the VPE ID visible to the guest. Enforce the GICv5 limit against individual vCPU IDs as well as the number of vCPUs. Reject vGICv5 creation if an existing vCPU ID cannot be represented, and reject an out-of-range vCPU created after userspace has selected vGICv5.
--- arch/arm64/kvm/vgic/vgic-init.c | 53 ++++++++++++++++++++++++++------- arch/arm64/kvm/vgic/vgic-v5.c | 16 +++++----- include/kvm/arm_vgic.h | 16 +++++++--- 3 files changed, 64 insertions(+), 21 deletions(-)
arch/arm64/kvm/vgic/vgic-init.c +43 -10
@@ -129,13 +129,34 @@ int kvm_vgic_create(struct kvm *kvm, u32 type) } ret =0 ; -if (type ==KVM_DEV_TYPE_ARM_VGIC_V2 ) +switch (type) { +case KVM_DEV_TYPE_ARM_VGIC_V2 : kvm->max_vcpus =VGIC_V2_MAX_CPUS ; -else if (type ==KVM_DEV_TYPE_ARM_VGIC_V3 ) +break ; +case KVM_DEV_TYPE_ARM_VGIC_V3 : kvm->max_vcpus =VGIC_V3_MAX_CPUS ; -else if (type ==KVM_DEV_TYPE_ARM_VGIC_V5 ) - kvm->max_vcpus =min (VGIC_V5_MAX_CPUS , - kvm_vgic_global_state.max_gic_vcpus); +break ; +case KVM_DEV_TYPE_ARM_VGIC_V5 : + kvm->max_vcpus = kvm_vgic_global_state.max_gicv5_vcpus; + +/* +* We use the userspace-allocated vcpu_id as the index into the +* GICv5 VPE table. This ensures that userspace's view and the +* guest's view of IAFFIDs remains consistent - GICv5's virtual +* IAFFID is the VPE ID in that VM's VPET. +* +* Reject vGICv5 creation if we have already got any vCPUs that +* we cannot back with a GICv5 VPE in the VPET. +*/ +kvm_for_each_vcpu (i, vcpu, kvm) { +if (vcpu->vcpu_id >= kvm->max_vcpus) { + ret = -E2BIG ; + goto out_unlock; + } + } + +break ; + }if (atomic_read (&kvm->online_vcpus) > kvm->max_vcpus) { ret = -E2BIG ;
@@ -395,15 +416,27 @@ int kvm_vgic_vcpu_init(struct kvm_vcpu *vcpu)if (ret)return ret; -/* -* If we are creating a VCPU with a GICv3 we must also register the -* KVM io device for the redistributor that belongs to this VCPU. -*/ -if (dist->vgic_model ==KVM_DEV_TYPE_ARM_VGIC_V3 ) { +switch (dist->vgic_model) { +case KVM_DEV_TYPE_ARM_VGIC_V3 : +/* +* If we are creating a VCPU with a GICv3 we must also register +* the KVM io device for the redistributor that belongs to this +* VCPU. +*/ mutex_lock (&vcpu->kvm->slots_lock); ret =vgic_register_redist_iodev (vcpu);mutex_unlock (&vcpu->kvm->slots_lock); +break ; +case KVM_DEV_TYPE_ARM_VGIC_V5 : +/* +* Ensure that it is possible to represent the +* userspace-allocated vcpu_id in the hardware-limited (or +* KVM-capped) VPE table used by GICv5. +*/ +if (vcpu->vcpu_id >= kvm_vgic_global_state.max_gicv5_vcpus) +return -EINVAL ; } +return ret; }
arch/arm64/kvm/vgic/vgic-v5.c +9 -7
@@ -81,7 +81,8 @@ int vgic_v5_probe(const struct gic_kvm_info *info)int ret; kvm_vgic_global_state.type =VGIC_V5 ; - kvm_vgic_global_state.max_gic_vcpus =VGIC_V5_MAX_CPUS ; + kvm_vgic_global_state.max_gic_vcpus =0 ; + kvm_vgic_global_state.max_gicv5_vcpus =0 ; kvm_vgic_global_state.vcpu_base =0 ; kvm_vgic_global_state.vctrl_base =NULL ;
@@ -106,8 +107,8 @@ int vgic_v5_probe(const struct gic_kvm_info *info)* Even if the HW supports more per-VM vCPUs, artificially cap as we * can't use them all. */ - kvm_vgic_global_state.max_gic_vcpus =min (vgic_v5_irs_max_vpes (&irs_caps), -VGIC_V5_MAX_CPUS ); + kvm_vgic_global_state.max_gicv5_vcpus =min (vgic_v5_irs_max_vpes (&irs_caps), +VGIC_V5_MAX_CPUS );/* * GICv5 requires a set of tables to be allocated in order to manage
@@ -116,7 +117,7 @@ int vgic_v5_probe(const struct gic_kvm_info *info)* we want to run. For now, we match the maximum number offered by the * hardware, but this might not be a wise choice in the long term. */ - ret =vgic_v5_vmt_allocate (kvm_vgic_global_state.max_gic_vcpus); + ret =vgic_v5_vmt_allocate (kvm_vgic_global_state.max_gicv5_vcpus);if (ret) {kvm_err ("Failed to allocate the GICv5 VM tables; no GICv5 support\n" );return -ENODEV ;
@@ -151,6 +152,8 @@ int vgic_v5_probe(const struct gic_kvm_info *info) } v5_registered = true; + kvm_vgic_global_state.max_gic_vcpus = + kvm_vgic_global_state.max_gicv5_vcpus;kvm_info ("GCIE system register CPU interface\n" ); skip_v5:
@@ -178,9 +181,8 @@ int vgic_v5_probe(const struct gic_kvm_info *info)return v5_registered ?0 : ret; } -/* We potentially limit the max VCPUs further than we need to here */ - kvm_vgic_global_state.max_gic_vcpus =min (VGIC_V3_MAX_CPUS , - kvm_vgic_global_state.max_gic_vcpus); + kvm_vgic_global_state.max_gic_vcpus =max (kvm_vgic_global_state.max_gic_vcpus, +VGIC_V3_MAX_CPUS );static_branch_enable (&kvm_vgic_global_state.gicv3_cpuif);kvm_info ("GCIE legacy system register CPU interface\n" );
include/kvm/arm_vgic.h +12 -4
@@ -170,9 +170,16 @@ struct vgic_global {/* Maintenance IRQ number */ unsigned int maint_irq; -/* maximum number of VCPUs allowed (GICv2 limits us to 8) */ +/* +* Maximum number of VCPUs exposed before userspace has selected a +* VGIC model. Individual VGIC models can impose a lower limit +* (GICv2 limits us to 8). +*/ int max_gic_vcpus; +/* Maximum number of VCPUs allowed for a GICv5 VM. */ +int max_gicv5_vcpus; +/* Only needed for the legacy KVM_CREATE_IRQCHIP */ bool can_emulate_gicv2;
@@ -622,10 +629,11 @@ void kvm_vgic_process_async_update(struct kvm_vcpu *vcpu);void vgic_v3_dispatch_sgi (struct kvm_vcpu *vcpu,u64 reg,bool allow_group1);/** -* kvm_vgic_get_max_vcpus - Get the maximum number of VCPUs allowed by HW +* kvm_vgic_get_max_vcpus - Get the pre-VGIC-selection VCPU limit * -* The host's GIC naturally limits the maximum amount of VCPUs a guest -* can use. +* Userspace can query KVM_CAP_MAX_VCPUS before selecting a VGIC model, so +* expose the highest model-specific limit and let kvm_vgic_create() enforce +* the selected model's actual limit. */ static inline int kvm_vgic_get_max_vcpus (void ) {
Signature · 1 line
2.34.1
[email protected]
AI reviewRe: [PATCH v5 12/49] KVM: arm64: gic-v5: Keep GICv5 vCPU limit model-specific
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
Pre-existing issues:
- [High] kvm_vgic_create() fails to validate that previously created VCPUs have IDs within the newly selected VGIC model's limits (for GICv2 and GICv3), leading to undefined behavior in GICv2 SGI routing. --
--- Patch [12]: [PATCH v5 12/49] KVM: arm64: gic-v5: Keep GICv5 vCPU limit model-specific --- commit 42bacf6540695fc12a35eee006d5382303176971
Author: Sascha Bischoff <[email protected]>
KVM: arm64: gic-v5: Keep GICv5 vCPU limit model-specific
This patch separates the IRS-derived vCPU limit for GICv5 from the generic KVM maximum limit. It adds validation to reject GICv5 device creation if existing VCPU IDs exceed the GICv5 model-specific limit.
Quoted 4 lines
arch/arm64/kvm/vgic/vgic-init.c +0
-0
[ ... ]
Quoted 27 lines
arch/arm64/kvm/vgic/vgic-init.c +16
-5@@ -129,13 +129,34 @@ int kvm_vgic_create(struct kvm *kvm, u32 type) } ret =0 ; -if (type ==KVM_DEV_TYPE_ARM_VGIC_V2 ) +switch (type) { +case KVM_DEV_TYPE_ARM_VGIC_V2 : kvm->max_vcpus =VGIC_V2_MAX_CPUS ; -else if (type ==KVM_DEV_TYPE_ARM_VGIC_V3 ) +break ; +case KVM_DEV_TYPE_ARM_VGIC_V3 : kvm->max_vcpus =VGIC_V3_MAX_CPUS ; -else if (type ==KVM_DEV_TYPE_ARM_VGIC_V5 ) - kvm->max_vcpus =min (VGIC_V5_MAX_CPUS , - kvm_vgic_global_state.max_gic_vcpus); +break ; +case KVM_DEV_TYPE_ARM_VGIC_V5 : + kvm->max_vcpus = kvm_vgic_global_state.max_gicv5_vcpus; + +/* +* We use the userspace-allocated vcpu_id as the index into the +* GICv5 VPE table. This ensures that userspace's view and the +* guest's view of IAFFIDs remains consistent - GICv5's virtual +* IAFFID is the VPE ID in that VM's VPET. +* +* Reject vGICv5 creation if we have already got any vCPUs that +* we cannot back with a GICv5 VPE in the VPET.
arch/arm64/kvm/vgic/vgic-init.c int kvm_vgic_create(struct kvm *kvm, u32 type) · line 159
Quoted 10 lines
arch/arm64/kvm/vgic/vgic-init.c +10
-0+*/ +kvm_for_each_vcpu (i, vcpu, kvm) { +if (vcpu->vcpu_id >= kvm->max_vcpus) { + ret = -E2BIG ; + goto out_unlock; + } + } + +break ; + }
[Severity: High]
This is a pre-existing issue, but does restricting this validation loop to only GICv5 leave the older models vulnerable to an out-of-bounds vcpu_id?
If userspace creates a VCPU with a large ID (for example, 500) before creating the VGIC device, it succeeds because the pre-VGIC limit defaults to the maximum supported by the system. If userspace then selects a GICv2 device, kvm_vgic_create() drops max_vcpus to 8.
Because the vcpu_id check above is inside the GICv5 switch block, we skip validating the existing VCPUs against the new GICv2 limit.
When configuring private IRQs later during initialization, vgic_setup_private_irq() does this:
arch/arm64/kvm/vgic/vgic-init.c:vgic_setup_private_irq() {
...
irq->targets = BIT(vcpu->vcpu_id);
...
}Evaluating BIT(500) shifts a 64-bit integer by 500, which results in undefined behavior in the kernel. A similar shift happens for SGI routing in vgic_mmio_write_sgir().
Should this validation loop be moved outside the switch statement so it runs for all VGIC models and prevents these out-of-bounds shifts across the board?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=12
Re: [PATCH v5 12/49] KVM: arm64: gic-v5: Keep GICv5 vCPU limit model-specific
Quoted 107 lines · On Fri, 2026-08-07 at 13:30 +0000, [email protected] wrote:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
Pre-existing issues:
- [High]kvm_vgic_create()fails to validate that previously created VCPUs have IDs within the newly selected VGIC model's limits (for GICv2 and GICv3), leading to undefined behavior in GICv2 SGI routing. ----- Patch [12]: [PATCH v5 12/49] KVM: arm64: gic-v5: Keep GICv5 vCPU limit model-specific ---
commit42bacf6540695fc12a35eee006d5382303176971
Author: Sascha Bischoff <[email protected]>KVM: arm64: gic-v5: Keep GICv5 vCPU limit model-specific
This patch separates the IRS-derived vCPU limit for GICv5 from the generic
KVM maximum limit. It adds validation to reject GICv5 device creation if
existing VCPU IDs exceed the GICv5 model-specific limit.Quoted 5 lines
arch/arm64/kvm/vgic/vgic-init.c +0
-0[ ... ]
Quoted 34 lines
arch/arm64/kvm/vgic/vgic-init.c +0
-0@@ -129,13 +129,34 @@ int kvm_vgic_create(struct kvm *kvm, u32 type) } ret = 0;arch/arm64/kvm/vgic/vgic-init.c +2
-1-if (type ==KVM_DEV_TYPE_ARM_VGIC_V2 ) +switch (type) { +case KVM_DEV_TYPE_ARM_VGIC_V2 :
kvm->max_vcpus=VGIC_V2_MAX_CPUS;arch/arm64/kvm/vgic/vgic-init.c +2
-1-else if (type ==KVM_DEV_TYPE_ARM_VGIC_V3 ) +break ; +case KVM_DEV_TYPE_ARM_VGIC_V3 :
kvm->max_vcpus=VGIC_V3_MAX_CPUS;arch/arm64/kvm/vgic/vgic-init.c +0
-3-else if (type ==KVM_DEV_TYPE_ARM_VGIC_V5 ) - kvm->max_vcpus = min(VGIC_V5_MAX_CPUS, -kvm_vgic_global_state.max_gic_vcpus);
arch/arm64/kvm/vgic/vgic-init.c +3
-0+break ; +case KVM_DEV_TYPE_ARM_VGIC_V5 : + kvm->max_vcpus =kvm_vgic_global_state.max_gicv5_vcpus;
arch/arm64/kvm/vgic/vgic-init.c +3
-0+ +/* +* We use the userspace-allocated vcpu_id as the index into the
arch/arm64/kvm/vgic/vgic-init.c +1
-0+* GICv5 VPE table. This ensures that userspace's view and the
arch/arm64/kvm/vgic/vgic-init.c +1
-0+* guest's view of IAFFIDs remains consistent - GICv5's virtual
arch/arm64/kvm/vgic/vgic-init.c +3
-0+* IAFFID is the VPE ID in that VM's VPET. +* +* Reject vGICv5 creation if we have already got any vCPUs that
arch/arm64/kvm/vgic/vgic-init.c +1
-0+* we cannot back with a GICv5 VPE in the VPET. arch/arm64/kvm/vgic/vgic-init.c int kvm_vgic_create(struct kvm *kvm, u32 type) · line 159
Quoted 10 lines
arch/arm64/kvm/vgic/vgic-init.c +10
-0+*/ +kvm_for_each_vcpu (i, vcpu, kvm) { +if (vcpu->vcpu_id >= kvm->max_vcpus) { + ret = -E2BIG ; + goto out_unlock; + } + } + +break ; + }[Severity: High]
This is a pre-existing issue, but does restricting this validation loop to
only GICv5 leave the older models vulnerable to an out-of-boundsvcpu_id?If userspace creates a VCPU with a large ID (for example, 500) before creating
the VGIC device, it succeeds because the pre-VGIC limit defaults to the
maximum supported by the system. If userspace then selects a GICv2 device,kvm_vgic_create()dropsmax_vcpusto 8.Because the
vcpu_idcheck above is inside the GICv5 switch block, we skip
validating the existing VCPUs against the new GICv2 limit.When configuring private IRQs later during initialization,
vgic_setup_private_irq()does this:arch/arm64/kvm/vgic/vgic-init.c:vgic_setup_private_irq() {
...
irq->targets=BIT(vcpu->vcpu_id);
...
}Evaluating BIT(500) shifts a 64-bit integer by 500, which results in undefined
behavior in the kernel. A similar shift happens for SGI routing invgic_mmio_write_sgir().
Quoted 5 lines
Should this validation loop be moved outside the switch statement so it runs
for all VGIC models and prevents these out-of-bounds shifts across the board?
I think that this makes sense. I've reworked the commit to perform this check for all three vGIC models. This way we reject bad configurations across the board, rather than just for GICv5. In theory, this change makes no difference for GICv3 as that is always capped at 512, but for GICv2 and GICv5 it makes a difference by rejecting out of range vcpu_ids once the vGIC has been created.
Thanks,
Sascha
PATCH v5 13/49 KVM: arm64: gic-v5: Implement VPE IRS MMIO Ops
In reply to Sascha BischoffIntroduce interfaces to make VPEs valid, and to configure them, via the host's IRS. As with the other valid bits in the GICv5 VM tables, VPEs cannot be made valid directly, and instead are made valid via an IRS MMIO Op.
Additionally, some of the VPE configuration takes place via the IRS MMIO interface too (via the IRS_VPE_CR0, IRS_VPE_DBR). VPE doorbells are, for example, configured via this interface.
The existing VPE-doorbell-based commands are extended with:
VPE_MAKE_VALID - Make the VPE valid in the VPET
Note: There is no VPE_MAKE_INVALID as VPEs are only made invalid on teardown, at which point the whole VMTE is marked as invalid. Hence, it is not required.
--- arch/arm64/kvm/vgic/vgic-v5.c | 84 ++++++++++++++++++++++++++++++ include/linux/irqchip/arm-gic-v5.h | 1 + 2 files changed, 85 insertions(+)
arch/arm64/kvm/vgic/vgic-v5.c +84 -0
@@ -204,6 +204,18 @@ static int vgic_v5_irs_wait_for_vm_op(void) NULL); } +/* +* Wait for completion of a change in any of IRS_VPE_SELR, IRS_VPE_DBR, +* IRS_VPE_CR0. +*/ +static int vgic_v5_irs_wait_for_vpe_op (void ) +{ +return gicv5_wait_for_op_atomic (irs_caps.irs_base, +GICV5_IRS_VPE_STATUSR , +GICV5_IRS_VPE_STATUSR_IDLE , +NULL ); +} +static int vgic_v5_irs_write_vm_mmio_reg (u64 val,u32 offset) {int ret;
@@ -311,10 +323,73 @@ static int vgic_v5_irs_set_vist_invalid(u16 vm_id, bool spi_ist)return __vgic_v5_irs_update_vist_validity (vm_id, spi_ist, true); } +static int vgic_v5_irs_set_up_vpe (u16 vm_id,u16 vpe_id, +irq_hw_number_t db_hwirq) +{ +u64 vmap_vper, dbr, selr; +u32 statusr, cr0; +int ret; + +lockdep_assert_held (&vgic_v5_irs_lock); + +/* Make sure that we are idle to begin with */ + ret =vgic_v5_irs_wait_for_vm_op (); +if (ret) +return ret; + +/* Mark the VPE as valid */ + vmap_vper =FIELD_PREP (GICV5_IRS_VMAP_VPER_VPE_ID , vpe_id) | +FIELD_PREP (GICV5_IRS_VMAP_VPER_VM_ID , vm_id) | +GICV5_IRS_VMAP_VPER_M ; +irs_writeq_relaxed (vmap_vper,GICV5_IRS_VMAP_VPER ); + +/* Wait for the VPE to be marked valid in the VPET */ + ret =vgic_v5_irs_wait_for_vm_op (); +if (ret) +return ret; + + selr =FIELD_PREP (GICV5_IRS_VPE_SELR_VPE_ID , vpe_id) | +FIELD_PREP (GICV5_IRS_VPE_SELR_VM_ID , vm_id) | +GICV5_IRS_VPE_SELR_S ; +irs_writeq_relaxed (selr,GICV5_IRS_VPE_SELR ); + + ret =vgic_v5_irs_wait_for_vpe_op (); +if (ret) +return ret; + + statusr =irs_readl_relaxed (GICV5_IRS_VPE_STATUSR ); +if (!FIELD_GET (GICV5_IRS_VPE_STATUSR_V , statusr)) +return -EINVAL ; + +/* Set targeted only routing (disable 1ofN vPE selection) */ + cr0 =GICV5_IRS_VPE_CR0_DPS ; +irs_writel_relaxed (cr0,GICV5_IRS_VPE_CR0 ); + + ret =vgic_v5_irs_wait_for_vpe_op (); +if (ret) +return ret; + +/* +* The VPE has not yet run. Therefore, make sure that all interrupts +* will generate a doorbell. +*/ + dbr =FIELD_PREP (GICV5_IRS_VPE_DBR_INTID , db_hwirq) | +GICV5_IRS_VPE_DBR_DBV ; +irs_writeq_relaxed (dbr,GICV5_IRS_VPE_DBR ); + + ret =vgic_v5_irs_wait_for_vpe_op (); +if (ret) +return ret; + +return 0 ; +} +static int vgic_v5_db_set_vcpu_affinity (struct irq_data *data,void *vcpu_info) {struct vgic_v5_vm *vm = data->domain->host_data;enum gicv5_vcpu_cmd *cmd = vcpu_info; +/* Our VPE ID is the index within the doorbell domain */ +u16 vpe_id = data->hwirq;guard (raw_spinlock_irqsave)(&vgic_v5_irs_lock);
@@ -325,6 +400,15 @@ static int vgic_v5_db_set_vcpu_affinity(struct irq_data *data, void *vcpu_info)return vgic_v5_irs_set_vm_valid (vm->vm_id);case VMTE_MAKE_INVALID :return vgic_v5_irs_set_vm_invalid (vm->vm_id); +case VPE_MAKE_VALID : +/* +* We need the actual LPI ID which lives in the top-most parent +* domain. This hwirq won't include the type (LPI) but that's +* not required for the IRS_VPE_DBR. +*/ +while (data->parent_data) + data = data->parent_data; +return vgic_v5_irs_set_up_vpe (vm->vm_id, vpe_id, data->hwirq);case SPI_VIST_MAKE_VALID :return vgic_v5_irs_set_vist_valid (vm->vm_id, true);case LPI_VIST_MAKE_VALID :
include/linux/irqchip/arm-gic-v5.h +1 -0
@@ -659,6 +659,7 @@ enum gicv5_vcpu_cmd {VMT_L2_MAP ,/* Map in a L2 VMT - *may* happen on VM init */ VMTE_MAKE_VALID ,/* Make the VMTE valid */ VMTE_MAKE_INVALID ,/* Make the VMTE (et al.) invalid */ +VPE_MAKE_VALID ,/* No corresponding invalid */ SPI_VIST_MAKE_VALID ,/* No corresponding invalid */ LPI_VIST_MAKE_VALID ,/* Triggered by a guest */ LPI_VIST_MAKE_INVALID, /* Triggered by a guest */
Signature · 1 line
2.34.1
PATCH v5 14/49 KVM: arm64: gic-v5: Set up VMTEs and VPE doorbells
In reply to Sascha BischoffA GICv5 VM needs a VM table entry before it can use SPIs and LPIs, which are backed by the host IRS. The VM table itself is created at probe time, but each VM still needs to claim and populate one VMTE before it can use those interrupts.
VPE doorbells are allocated from the host LPI irq domain. Without that domain, KVM cannot issue IRS commands or receive doorbell wakeups. Fail the GICv5 KVM probe if the host driver did not create an LPI domain.
Allocate a VM ID during vgic_v5_init(). The VM ID is also the index into the VM table, so allocating it selects the VMTE slot that will be used for the lifetime of the VM.
Create a per-VM VPE doorbell irq domain, allocate one doorbell interrupt per vCPU, request the interrupts, and keep the doorbell IRQ number in the vCPU's GICv5 state. The doorbell handler marks the VPE doorbell as fired, raises KVM_REQ_IRQ_PENDING, and kicks the target vCPU so that KVM can re-evaluate pending interrupt state.
The doorbell domain indexes its interrupts using the dense vcpu_idx, while GICv5 uses the userspace-provided vcpu_id as the VPE ID. Store a backpointer to the VM and use it to resolve the vCPU before issuing VPE-specific IRS commands. This preserves the userspace VPE ID when vCPU IDs are sparse.
With the VM ID and doorbells in place, initialise the VMTE backing state, including the VM descriptor, VPE table, and preallocated VPED storage. The doorbells have to exist before making the VMTE valid, as they provide the IRQ-side conduit used by the IRS commands. Make the VMTE valid via the IRS, then populate the VPETE for each vCPU.
Add vgic_v5_teardown() to unwind the state in the reverse order. Make the VMTE invalid, clear the per-vCPU VPETEs, release the VMTE backing state, free the doorbell IRQs and irq domain, and finally release the VM ID so that the VMTE slot can be reused by a later VM. If hardware invalidation or VMTE release fails, still free the software doorbells and domain, but keep the VM ID allocated so that the VMTE slot is not reused while hardware-visible state may remain.
On init failure, call the same teardown path so that partially created state is unwound consistently.
As part of resetting vCPUs, mark them as valid in the VM's VPE table. This informs the IRS that a specific VPE may be made resident. Without this, the IRS will treat the VPE as invalid.
Also introduce vgic_v5_send_command(), a wrapper around the VPE doorbells. It takes a struct kvm_vcpu pointer and the command to run, and invokes the function bound to that command through the vCPU's doorbell.
--- arch/arm64/kvm/vgic/vgic-v5.c | 228 +++++++++++++++++++++++++++++----- include/kvm/arm_vgic.h | 2 + 2 files changed, 197 insertions(+), 33 deletions(-)
arch/arm64/kvm/vgic/vgic-v5.c +195 -33
@@ -100,6 +100,11 @@ int vgic_v5_probe(const struct gic_kvm_info *info) goto skip_v5; } +if (!gicv5_global_data.lpi_domain) { +kvm_err ("GICv5 LPI domain unavailable\n" ); +return -ENODEV ; + } +vgic_v5_irs_cache_id_regs (info);vgic_v5_get_implemented_ppis ();
@@ -384,12 +389,35 @@ static int vgic_v5_irs_set_up_vpe(u16 vm_id, u16 vpe_id,return 0 ; } +static irqreturn_t db_handler (int irq,void *data) +{ +struct kvm_vcpu *vcpu = data; + +WRITE_ONCE (vcpu->arch.vgic_cpu.vgic_v5.gicv5_vpe.db_fired, true); + +kvm_make_request (KVM_REQ_IRQ_PENDING , vcpu); +kvm_vcpu_kick (vcpu); + +return IRQ_HANDLED ; +} + +static int vgic_v5_send_command (struct kvm_vcpu *vcpu,enum gicv5_vcpu_cmd cmd) +{ +int irq =vgic_v5_vpe_db (vcpu); + +if (!irq) +return -ENXIO ; + +return irq_set_vcpu_affinity (irq, &cmd); +} +static int vgic_v5_db_set_vcpu_affinity (struct irq_data *data,void *vcpu_info) {struct vgic_v5_vm *vm = data->domain->host_data;enum gicv5_vcpu_cmd *cmd = vcpu_info; -/* Our VPE ID is the index within the doorbell domain */ -u16 vpe_id = data->hwirq; +unsigned int vcpu_idx = data->hwirq; +struct kvm_vcpu *vcpu; +u16 vpe_id;guard (raw_spinlock_irqsave)(&vgic_v5_irs_lock);
@@ -401,6 +429,17 @@ static int vgic_v5_db_set_vcpu_affinity(struct irq_data *data, void *vcpu_info)case VMTE_MAKE_INVALID :return vgic_v5_irs_set_vm_invalid (vm->vm_id);case VPE_MAKE_VALID : +/* +* The index in the doorbell domain aligns with our flat +* vcpu_idx index. However, we need the actual VPE ID, which +* means we first need to resolve the actual vcpu. +*/ + vcpu =kvm_get_vcpu (vm->kvm, vcpu_idx); +if (!vcpu) +return -EINVAL ; + + vpe_id =vgic_v5_vpe_id (vcpu); +/* * We need the actual LPI ID which lives in the top-most parent * domain. This hwirq won't include the type (LPI) but that's
@@ -456,15 +495,9 @@ static int vgic_v5_irq_db_domain_alloc(struct irq_domain *domain, void *arg) {const struct irq_chip *chip = &vgic_v5_db_irq_chip; -struct vgic_v5_vm *vm = arg;struct irq_data *irqd;int ret; -if (!vm) { -kvm_err ("invalid parameter for doorbell irq allocation\n" ); -return -EINVAL ; - } - ret =irq_domain_alloc_irqs_parent (domain, virq, nr_irqs,NULL );if (ret)return ret;
@@ -491,20 +524,10 @@ static int vgic_v5_create_per_vm_domain(struct kvm *kvm)int id =task_pid_nr (current);int ret, db_virq =0 ; -if (!gicv5_global_data.lpi_domain) { -kvm_err ("LPI domain uninitialized, can't set up KVM Doorbells\n" ); -return -ENODEV ; - } - vm->fwnode =irq_domain_alloc_named_id_fwnode ("GICv5-vpe-db" , id);if (!vm->fwnode)return -ENOMEM ; -/* -* KVM per-VM VPE DB domain; child of LPI domain; only ever handles -* doorbells. We know how many doorbells we have, and therefore we -* create a linear domain. -*/ vm->domain =irq_domain_create_hierarchy (gicv5_global_data.lpi_domain,0 , nr_vcpus, vm->fwnode, &vgic_v5_irq_db_domain_ops, vm);
@@ -543,11 +566,6 @@ static void vgic_v5_teardown_per_vm_domain(struct vgic_v5_vm *vm)if (!vm->domain)return ; -if (vm->vpe_db_base) { -irq_domain_free_irqs (vm->vpe_db_base, vm->domain->revmap_size); - vm->vpe_db_base =0 ; - } -irq_domain_remove (vm->domain);irq_domain_free_fwnode (vm->fwnode); vm->domain =NULL ;
@@ -567,33 +585,121 @@ void vgic_v5_reset(struct kvm_vcpu *vcpu)* CPUIF (but potentially fewer in the IRS). */ vcpu->arch.vgic_cpu.num_pri_bits =5 ; + +/* Make the VPE valid in the VPET */ +if (WARN_ON (vgic_v5_send_command (vcpu,VPE_MAKE_VALID ))) +return ; +} + +static void vgic_v5_free_doorbells (struct kvm *kvm,unsigned int nr_dbs) +{ +struct vgic_v5_vm *vm = &kvm->arch.vgic.gicv5_vm; +struct kvm_vcpu *vcpu; +unsigned long i; +int db; + +for (i =0 ; i < nr_dbs; i++) { + vcpu =kvm_get_vcpu (kvm, i); + db =vgic_v5_vpe_db (vcpu); +if (!db) +continue ; + +free_irq (db, vcpu); + vcpu->arch.vgic_cpu.vgic_v5.gicv5_vpe.db =0 ; + } + +if (vm->vpe_db_base) { +irq_domain_free_irqs (vm->vpe_db_base, +atomic_read (&kvm->online_vcpus)); + vm->vpe_db_base =0 ; + } }void vgic_v5_teardown (struct kvm *kvm) { +struct vgic_dist *dist = &kvm->arch.vgic; +struct kvm_vcpu *vcpu, *vcpu0; +bool release_vm_id = true; +unsigned long i; +int rc; + +lockdep_assert_held (&kvm->arch.config_lock); + +/* +* If the VM's ID isn't valid, then we either failed init very early or +* we've been called a second time. Nothing to do here in either case. +*/ +if (kvm->arch.vgic.gicv5_vm.vm_id ==VGIC_V5_VM_ID_INVAL ) +return ; + +if (kvm->arch.vgic.gicv5_vm.vmte_allocated) { +/* Make the VM invalid */ + vcpu0 =kvm_get_vcpu (kvm,0 ); + rc =vgic_v5_send_command (vcpu0,VMTE_MAKE_INVALID ); +if (rc) { +kvm_err ("could not make VMTE invalid\n" ); + release_vm_id = false; + goto out_free_doorbells; + } + +kvm_for_each_vcpu (i, vcpu, kvm) { +if (vgic_v5_vmte_free_vpe (vcpu)) { +kvm_err ("Failed to free VPE\n" ); + release_vm_id = false; + } + } + +if (vgic_v5_vmte_release (kvm)) { +kvm_err ("Failed to release VM 0x%x\n" , dist->gicv5_vm.vm_id); + release_vm_id = false; + } + } + +out_free_doorbells: +vgic_v5_free_doorbells (kvm,atomic_read (&kvm->online_vcpus));vgic_v5_teardown_per_vm_domain (&kvm->arch.vgic.gicv5_vm); + +/* +* We only release the VM ID itself if we didn't fail earlier. It does +* mean that we might lose the VM ID (and associated VMTE, etc), but +* given that we've failed to tear them down correctly there's no way to +* safely reuse them. The VM ID allocating IDA will make sure we don't +* accidentally reuse this partially torn down state. +*/ +if (release_vm_id) +vgic_v5_release_vm_id (kvm); } +/* +* Claim and populate a VMTE (optionally making a new L2 VMT valid), create VPE +* doorbells, allocate VPET and populate for each VPE. +* +* Note: We do need to put the cart before the horse here. The VPE doorbells are +* our conduit for communication with the IRS, which means we need to have those +* before making the VMTE valid. +* +* On failure, we clean up in the teardown path (vgic_v5_teardown()). +*/ int vgic_v5_init (struct kvm *kvm) { -struct kvm_vcpu *vcpu; -unsigned long idx; -int ret; +struct kvm_vcpu *vcpu, *vcpu0; +int nr_vcpus, ret =0 ; +unsigned int db_virq; +unsigned long i; -if (vgic_initialized (kvm)) -return 0 ; +lockdep_assert_held (&kvm->arch.config_lock); + + nr_vcpus =atomic_read (&kvm->online_vcpus); +if (nr_vcpus ==0 ) +return -ENODEV ; -kvm_for_each_vcpu (idx, vcpu, kvm) { +kvm_for_each_vcpu (i, vcpu, kvm) {if (vcpu_has_nv (vcpu)) {kvm_err ("Nested GICv5 VMs are currently unsupported\n" );return -EINVAL ; } } - ret =vgic_v5_create_per_vm_domain (kvm); -if (ret) -return ret; -/* We only allow userspace to drive the SW_PPI, if it is implemented. */ bitmap_zero (kvm->arch.vgic.gicv5_vm.userspace_ppis,VGIC_V5_NR_PRIVATE_IRQS );
@@ -602,7 +708,63 @@ int vgic_v5_init(struct kvm *kvm) kvm->arch.vgic.gicv5_vm.userspace_ppis, ppi_caps.impl_ppi_mask, VGIC_V5_NR_PRIVATE_IRQS); + ret =vgic_v5_allocate_vm_id (kvm); +if (ret) +return ret; + +/* +* Stash a backpointer to struct kvm. It is required to resolve the VPE +* ID from the doorbell index, which matches the flat vcpu_idx and not +* the vcpu_id. +*/ + kvm->arch.vgic.gicv5_vm.kvm = kvm; + + ret =vgic_v5_create_per_vm_domain (kvm); +if (ret) + goto err; + + db_virq = kvm->arch.vgic.gicv5_vm.vpe_db_base; +kvm_for_each_vcpu (i, vcpu, kvm) { + ret =request_irq (db_virq + i, db_handler,0 ,"vcpu" , vcpu); +if (ret) + goto err; + +/* Stash it with the VCPU for easy retrieval */ + vcpu->arch.vgic_cpu.vgic_v5.gicv5_vpe.db = db_virq + i; + } + +/* Populate VMTE (with VPET and VM descriptor) */ + ret =vgic_v5_vmte_init (kvm); +if (ret) + goto err; + +/* We pick the first vcpu to make the VMTE valid - any would do */ + vcpu0 =kvm_get_vcpu (kvm,0 ); + ret =vgic_v5_send_command (vcpu0,VMTE_MAKE_VALID ); +if (ret) + goto err; + +/* Populate the VPETE for each VPE. */ +kvm_for_each_vcpu (i, vcpu, kvm) { + ret =vgic_v5_vmte_alloc_vpe (vcpu); +if (ret) + goto err; + } +return 0 ; + +err: +/* +* Explicitly tear everything down on failure. The teardown function is +* written to handle any partial state we might have, so we don't need +* to do any clean-up first. Teardown will be called a second time on VM +* destruction, but that's fine - it is better to leave things in a +* clean state now, and doubly so because userspace could actually go +* and retry init. +*/ +vgic_v5_teardown (kvm); + +return ret; }int vgic_v5_map_resources (struct kvm *kvm)
include/kvm/arm_vgic.h +2 -0
@@ -371,6 +371,8 @@ struct vgic_redist_region {#define VGIC_V5_VM_ID_INVAL (-1)struct vgic_v5_vm { +struct kvm *kvm; +/* * We only expose a subset of PPIs to the guest. This subset is a * combination of the PPIs that are actually implemented and what we
Signature · 1 line
2.34.1
[email protected]
AI reviewRe: [PATCH v5 14/49] KVM: arm64: gic-v5: Set up VMTEs and VPE doorbells
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [High] Double-teardown vulnerability allows corrupted VMTE slots to be reused, subverting the explicit VM…
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [High] Double-teardown vulnerability allows corrupted VMTE slots to be reused, subverting the explicit VM ID leak mechanism.
- [Medium] vgic_v5_create_per_vm_domain uses task_pid_nr(current) for the fwnode name, causing debugfs collisions for multiple VMs in the same process. --
--- Patch [14]: [PATCH v5 14/49] KVM: arm64: gic-v5: Set up VMTEs and VPE doorbells --- commit 7e8599087432413da91f2b37e503a3ba7e749684
Author: Sascha Bischoff <[email protected]>
KVM: arm64: gic-v5: Set up VMTEs and VPE doorbells
A GICv5 VM needs a VM table entry before it can use SPIs and LPIs, which are backed by the host IRS. The VM table itself is created at probe time, but each VM still needs to claim and populate one VMTE before it can use those interrupts. Allocate a VM ID during vgic_v5_init() and create a per-VM VPE doorbell irq domain.
Quoted 4 lines
arch/arm64/kvm/vgic/vgic-v5.c +0
-0
[ ... ]
arch/arm64/kvm/vgic/vgic-v5.c static int vgic_v5_create_per_vm_domain(struct kvm *kvm) · line 527
Quoted 10 lines
arch/arm64/kvm/vgic/vgic-v5.c +0
-5@@ -491,20 +524,10 @@ static int vgic_v5_create_per_vm_domain(struct kvm *kvm)int id =task_pid_nr (current);int ret, db_virq =0 ; -if (!gicv5_global_data.lpi_domain) { -kvm_err ("LPI domain uninitialized, can't set up KVM Doorbells\n" ); -return -ENODEV ; - } - vm->fwnode =irq_domain_alloc_named_id_fwnode ("GICv5-vpe-db" , id);
[Severity: Medium]
Does using task_pid_nr() here cause debugfs collisions if a single process creates multiple VMs?
If userspace instantiates multiple GICv5 VMs within the same process, they will share the same PID. The irq_domain_alloc_named_id_fwnode() call would then create a fwnode based on the PID instead of a unique identifier like the VM ID.
Destroying the first VM removes the shared directory, breaking it for the remaining VMs.
[ ... ]
Quoted 29 lines
arch/arm64/kvm/vgic/vgic-v5.c +29
-0+void vgic_v5_teardown (struct kvm *kvm) +{ +struct vgic_dist *dist = &kvm->arch.vgic; +struct kvm_vcpu *vcpu, *vcpu0; +bool release_vm_id = true; +unsigned long i; +int rc; + +lockdep_assert_held (&kvm->arch.config_lock); + +/* +* If the VM's ID isn't valid, then we either failed init very early or +* we've been called a second time. Nothing to do here in either case. +*/ +if (kvm->arch.vgic.gicv5_vm.vm_id ==VGIC_V5_VM_ID_INVAL ) +return ; + +if (kvm->arch.vgic.gicv5_vm.vmte_allocated) { +/* Make the VM invalid */ + vcpu0 =kvm_get_vcpu (kvm,0 ); + rc =vgic_v5_send_command (vcpu0,VMTE_MAKE_INVALID ); +if (rc) { +kvm_err ("could not make VMTE invalid\n" ); + release_vm_id = false; + goto out_free_doorbells; + } + +kvm_for_each_vcpu (i, vcpu, kvm) { +if (vgic_v5_vmte_free_vpe (vcpu)) {
arch/arm64/kvm/vgic/vgic-v5.c void vgic_v5_reset(struct kvm_vcpu *vcpu) · line 656
Quoted 10 lines
arch/arm64/kvm/vgic/vgic-v5.c +10
-0+kvm_err ("Failed to free VPE\n" ); + release_vm_id = false; + } + } + +if (vgic_v5_vmte_release (kvm)) { +kvm_err ("Failed to release VM 0x%x\n" , dist->gicv5_vm.vm_id); + release_vm_id = false; + } + }
[Severity: High]
Could a double-teardown incorrectly release the VM ID despite a previous failure here, allowing corrupted VMTE slots to be reused?
During the first run of vgic_v5_teardown(), if vgic_v5_vmte_free_vpe() fails, release_vm_id is set to false to leak the VM ID and prevent reuse. However, if vgic_v5_vmte_release() then succeeds, it will clear vmte_allocated.
Quoted 5 lines
arch/arm64/kvm/vgic/vgic-v5.c +5
-0+ +out_free_doorbells: +vgic_v5_free_doorbells (kvm,atomic_read (&kvm->online_vcpus)); +vgic_v5_teardown_per_vm_domain (&kvm->arch.vgic.gicv5_vm); +
arch/arm64/kvm/vgic/vgic-v5.c void vgic_v5_reset(struct kvm_vcpu *vcpu)
Quoted 10 lines
arch/arm64/kvm/vgic/vgic-v5.c +10
-0+/* +* We only release the VM ID itself if we didn't fail earlier. It does +* mean that we might lose the VM ID (and associated VMTE, etc), but +* given that we've failed to tear them down correctly there's no way to +* safely reuse them. The VM ID allocating IDA will make sure we don't +* accidentally reuse this partially torn down state. +*/ +if (release_vm_id) +vgic_v5_release_vm_id (kvm); +}
When vgic_v5_teardown() is called a second time (e.g., on VM destruction after an initial failure), vm_id is still valid, but vmte_allocated is now false. This causes it to bypass the block that sets release_vm_id to false on errors.
Because release_vm_id defaults to true at the top of the function, it then proceeds to unconditionally release the VM ID, subverting the safety mechanism and potentially leading to hardware state corruption.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=14
Re: [PATCH v5 14/49] KVM: arm64: gic-v5: Set up VMTEs and VPE doorbells
Quoted 38 lines · On Fri, 2026-08-07 at 13:42 +0000, [email protected] wrote:
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [High] Double-teardown vulnerability allows corrupted VMTE slots to be reused, subverting the explicit VM ID leak mechanism.
- [Medium]vgic_v5_create_per_vm_domainusestask_pid_nr(current)for the fwnode name, causing debugfs collisions for multiple VMs in the same process.
----- Patch [14]: [PATCH v5 14/49] KVM: arm64: gic-v5: Set up VMTEs and VPE doorbells ---
commit7e8599087432413da91f2b37e503a3ba7e749684
Author: Sascha Bischoff <[email protected]>KVM: arm64: gic-v5: Set up VMTEs and VPE doorbells
A GICv5 VM needs a VM table entry before it can use SPIs and LPIs, which are backed by the host IRS. The VM table itself is created at probe time, but each VM still needs to claim and populate one VMTE before it can use those interrupts. Allocate a VM ID during
vgic_v5_init()and create a per-VM VPE doorbell irq domain.Quoted 5 lines
arch/arm64/kvm/vgic/vgic-v5.c +0
-0[ ... ]
Quoted 10 lines
arch/arm64/kvm/vgic/vgic-v5.c +0
-0@@ -491,20 +524,10 @@ static int vgic_v5_create_per_vm_domain(struct kvm *kvm) int id = task_pid_nr(current); int ret, db_virq = 0;arch/arm64/kvm/vgic/vgic-v5.c +0
-2-if (!gicv5_global_data.lpi_domain) { - kvm_err("LPI domain uninitialized, can't set upKVM Doorbells\n");
arch/arm64/kvm/vgic/vgic-v5.c +0
-2-return -ENODEV ; - }
Quoted 8 lines
Quoted 3 lines
-
vm->fwnode=irq_domain_alloc_named_id_fwnode("GICv5-vpe- db", id);[Severity: Medium]
Does usingtask_pid_nr()here cause debugfs collisions if a single process
creates multiple VMs?
Yes, it would, but this is the same as is done for GICv4. If anything, this needs to be addressed for both GICv4 and GICv5 if this is indeed an issue. FWIW, it is not common to create two VMs from a single thread.
Quoted 54 lines
If userspace instantiates multiple GICv5 VMs within the same process, they
will share the same PID. Theirq_domain_alloc_named_id_fwnode()call would
then create a fwnode based on the PID instead of a unique identifier like
the VM ID.Destroying the first VM removes the shared directory, breaking it for the
remaining VMs.[ ... ]
Quoted 40 lines
arch/arm64/kvm/vgic/vgic-v5.c +12
-0+void vgic_v5_teardown (struct kvm *kvm) +{ +struct vgic_dist *dist = &kvm->arch.vgic; +struct kvm_vcpu *vcpu, *vcpu0; +bool release_vm_id = true; +unsigned long i; +int rc; + +lockdep_assert_held (&kvm->arch.config_lock); + +/* +* If the VM's ID isn't valid, then we either failed init very early or
arch/arm64/kvm/vgic/vgic-v5.c +1
-0+* we've been called a second time. Nothing to do here in either case.
arch/arm64/kvm/vgic/vgic-v5.c +8
-0+*/ +if (kvm->arch.vgic.gicv5_vm.vm_id ==VGIC_V5_VM_ID_INVAL ) +return ; + +if (kvm->arch.vgic.gicv5_vm.vmte_allocated) { +/* Make the VM invalid */ + vcpu0 =kvm_get_vcpu (kvm,0 ); + rc = vgic_v5_send_command(vcpu0,
VMTE_MAKE_INVALID);arch/arm64/kvm/vgic/vgic-v5.c +15
-0+if (rc) { +kvm_err ("could not make VMTE invalid\n" ); + release_vm_id = false; + goto out_free_doorbells; + } + +kvm_for_each_vcpu (i, vcpu, kvm) { +if (vgic_v5_vmte_free_vpe (vcpu)) { +kvm_err ("Failed to free VPE\n" ); + release_vm_id = false; + } + } + +if (vgic_v5_vmte_release (kvm)) { + kvm_err("Failed to release VM 0x%x\n",
dist->gicv5_vm.vm_id);
Quoted 8 lines
arch/arm64/kvm/vgic/vgic-v5.c void vgic_v5_reset(struct kvm_vcpu *vcpu) · line 649
Quoted 3 lines
arch/arm64/kvm/vgic/vgic-v5.c +3
-0+ release_vm_id = false; + } + }[Severity: High]
Could a double-teardown incorrectly release the VM ID despite a previous
failure here, allowing corrupted VMTE slots to be reused?
The code was missing a goto out_free_doorbells in the case that the VPE teardown failed. I've added it.
Quoted 37 lines
During the first run of
vgic_v5_teardown(), ifvgic_v5_vmte_free_vpe()fails,release_vm_idis set to false to leak the VM ID and prevent reuse. However,
ifvgic_v5_vmte_release()then succeeds, it will clearvmte_allocated.Quoted 15 lines
arch/arm64/kvm/vgic/vgic-v5.c +3
-0+ +out_free_doorbells: + vgic_v5_free_doorbells(kvm, atomic_read(&kvm-Quoted 1 line
online_vcpus));arch/arm64/kvm/vgic/vgic-v5.c +4
-0+vgic_v5_teardown_per_vm_domain (&kvm->arch.vgic.gicv5_vm); + +/* +* We only release the VM ID itself if we didn't fail earlier. It does
arch/arm64/kvm/vgic/vgic-v5.c +1
-0+* mean that we might lose the VM ID (and associated VMTE, etc), but
arch/arm64/kvm/vgic/vgic-v5.c +1
-0+* given that we've failed to tear them down correctly there's no way to
arch/arm64/kvm/vgic/vgic-v5.c +1
-0+* safely reuse them. The VM ID allocating IDA will make sure we don't
arch/arm64/kvm/vgic/vgic-v5.c void vgic_v5_reset(struct kvm_vcpu *vcpu)
Quoted 5 lines
arch/arm64/kvm/vgic/vgic-v5.c +5
-0+* accidentally reuse this partially torn down state. +*/ +if (release_vm_id) +vgic_v5_release_vm_id (kvm); +}When
vgic_v5_teardown()is called a second time (e.g., on VM destruction
after an initial failure),vm_idis still valid, butvmte_allocatedis now
false. This causes it to bypass the block that setsrelease_vm_idto false
on errors.
Quoted 5 lines
Because
release_vm_iddefaults to true at the top of the function, it then
proceeds to unconditionally release the VM ID, subverting the safety mechanism and potentially leading to hardware state corruption.
This has been addressed by skipping the VMTE release if the freeing of a VPE failed. This keeps the behaviour consistent across attempts.
Thanks,
Sascha
PATCH v5 15/49 KVM: arm64: gic-v5: Add resident/non-resident hyp calls
In reply to Sascha BischoffGICv5 introduces the concept of VPE residency - a VPE can be either resident or non-resident. When the VPE is resident, the IRS is allowed to select interrupts that target that VPE (or the VM) as the HPPI (Highest Priority Pending Interrupt). As the IRS handles both SPIs and LPIs, these will only be picked as the IRS's HPPI when a VPE is resident.
A GICv5 VPE is made resident by writing ICH_CONTEXTR_EL2 with ICH_CONTEXTR_EL2.V set, together with valid VM and VPE IDs. This informs the IRS that a specific VPE is running, and that it can begin HPPI selection for that VPE. Making a VPE non-resident (by making the ICH_CONTEXTR_EL2 invalid) informs the IRS that the VPE is no longer running, and it stops HPPI selection for it.
This change introduces two new hyp calls - one to make a VPE resident and its counterpart to make a VPE non-resident. As part of making a VPE resident, the resulting ICH_CONTEXTR_EL2.F bit is checked to catch residency faults. Such a fault indicates a broken VM/VPE setup, so warn and mark the VM dead.
Both of these new hypercalls are explicitly no-ops with pKVM as we currently don't support the combination of GICv5 and pKVM.
Furthermore, this change extends vgic_v5_load() and vgic_v5_put() to make the VPEs resident and non-resident, respectively. Hence, the VPE is considered resident for the entire load-to-put interval.
--- arch/arm64/include/asm/kvm_asm.h | 2 ++ arch/arm64/include/asm/kvm_hyp.h | 2 ++ arch/arm64/kvm/hyp/nvhe/hyp-main.c | 22 ++++++++++++++++++++++ arch/arm64/kvm/hyp/vgic-v5-sr.c | 26 ++++++++++++++++++++++++++ arch/arm64/kvm/vgic/vgic-v5.c | 16 ++++++++++++++-- include/kvm/arm_vgic.h | 3 +++ 6 files changed, 69 insertions(+), 2 deletions(-)
arch/arm64/include/asm/kvm_asm.h +2 -0
@@ -87,6 +87,8 @@ enum __kvm_host_smccc_func { __KVM_HOST_SMCCC_FUNC___tracing_write_event, __KVM_HOST_SMCCC_FUNC___vgic_v3_save_aprs, __KVM_HOST_SMCCC_FUNC___vgic_v3_restore_vmcr_aprs, + __KVM_HOST_SMCCC_FUNC___vgic_v5_make_resident, + __KVM_HOST_SMCCC_FUNC___vgic_v5_make_non_resident, __KVM_HOST_SMCCC_FUNC___vgic_v5_save_apr, __KVM_HOST_SMCCC_FUNC___vgic_v5_restore_vmcr_apr,
arch/arm64/include/asm/kvm_hyp.h +2 -0
@@ -88,6 +88,8 @@ void __vgic_v3_restore_vmcr_aprs(struct vgic_v3_cpu_if *cpu_if);int __vgic_v3_perform_cpuif_access (struct kvm_vcpu *vcpu);/* GICv5 */ +void __vgic_v5_make_resident (struct vgic_v5_cpu_if *cpu_if); +void __vgic_v5_make_non_resident (struct vgic_v5_cpu_if *cpu_if);void __vgic_v5_save_apr (struct vgic_v5_cpu_if *cpu_if);void __vgic_v5_restore_vmcr_apr (struct vgic_v5_cpu_if *cpu_if);/* No hypercalls for the following */
arch/arm64/kvm/hyp/nvhe/hyp-main.c +22 -0
@@ -691,6 +691,26 @@ static void handle___tracing_write_event(struct kvm_cpu_context *host_ctxt)trace_selftest (id); } +static void handle___vgic_v5_make_resident (struct kvm_cpu_context *host_ctxt) +{ + DECLARE_REG(struct vgic_v5_cpu_if *, cpu_if, host_ctxt, 1); + +if (unlikely (is_protected_kvm_enabled ())) +return ; + +__vgic_v5_make_resident (kern_hyp_va (cpu_if)); +} + +static void handle___vgic_v5_make_non_resident (struct kvm_cpu_context *host_ctxt) +{ + DECLARE_REG(struct vgic_v5_cpu_if *, cpu_if, host_ctxt, 1); + +if (unlikely (is_protected_kvm_enabled ())) +return ; + +__vgic_v5_make_non_resident (kern_hyp_va (cpu_if)); +} +static void handle___vgic_v5_save_apr (struct kvm_cpu_context *host_ctxt) { DECLARE_REG(struct vgic_v5_cpu_if *, cpu_if, host_ctxt, 1);
@@ -738,6 +758,8 @@ static const hcall_t host_hcall[] = {HANDLE_FUNC (__tracing_write_event),HANDLE_FUNC (__vgic_v3_save_aprs),HANDLE_FUNC (__vgic_v3_restore_vmcr_aprs), +HANDLE_FUNC (__vgic_v5_make_resident), +HANDLE_FUNC (__vgic_v5_make_non_resident),HANDLE_FUNC (__vgic_v5_save_apr), HANDLE_FUNC(__vgic_v5_restore_vmcr_apr),
arch/arm64/kvm/hyp/vgic-v5-sr.c +26 -0
@@ -7,6 +7,32 @@#include <asm/kvm_hyp.h> +void __vgic_v5_make_resident (struct vgic_v5_cpu_if *cpu_if) +{ +write_sysreg_s (cpu_if->vgic_contextr,SYS_ICH_CONTEXTR_EL2 ); +isb (); + +/* Catch any faults */ + cpu_if->vgic_contextr =read_sysreg_s (SYS_ICH_CONTEXTR_EL2 ); +if (!!FIELD_GET (ICH_CONTEXTR_EL2_F , cpu_if->vgic_contextr)) +return ; + + cpu_if->gicv5_vpe.resident = true; +} + +void __vgic_v5_make_non_resident (struct vgic_v5_cpu_if *cpu_if) +{ +/* +* Make as non-resident before actually making non-resident. Avoids race +* with doorbell arriving. +*/ + cpu_if->gicv5_vpe.resident = false; +dsb (st); + +write_sysreg_s (cpu_if->vgic_contextr,SYS_ICH_CONTEXTR_EL2 ); +isb (); +} +void __vgic_v5_save_apr (struct vgic_v5_cpu_if *cpu_if) { cpu_if->vgic_apr =read_sysreg_s (SYS_ICH_APR_EL2 );
arch/arm64/kvm/vgic/vgic-v5.c +14 -2
@@ -1058,6 +1058,8 @@ void vgic_v5_flush_ppi_state(struct kvm_vcpu *vcpu)void vgic_v5_load (struct kvm_vcpu *vcpu) {struct vgic_v5_cpu_if *cpu_if = &vcpu->arch.vgic_cpu.vgic_v5; +u16 vm =vgic_v5_vm_id (vcpu->kvm); +u16 vpe =vgic_v5_vpe_id (vcpu);/* * On the WFI path, vgic_load is called a second time. The first is when
@@ -1070,7 +1072,15 @@ void vgic_v5_load(struct kvm_vcpu *vcpu)kvm_call_hyp (__vgic_v5_restore_vmcr_apr, cpu_if); - cpu_if->gicv5_vpe.resident = true; + cpu_if->vgic_contextr =FIELD_PREP (ICH_CONTEXTR_EL2_V , true) | +FIELD_PREP (ICH_CONTEXTR_EL2_VPE , vpe) | +FIELD_PREP (ICH_CONTEXTR_EL2_VM , vm); + +kvm_call_hyp (__vgic_v5_make_resident, cpu_if); + +/* Failed to make the VPE resident? Bang! */ +if (WARN_ON (!!FIELD_GET (ICH_CONTEXTR_EL2_F , cpu_if->vgic_contextr))) +kvm_vm_dead (vcpu->kvm); }void vgic_v5_put (struct kvm_vcpu *vcpu)
@@ -1088,7 +1098,9 @@ void vgic_v5_put(struct kvm_vcpu *vcpu)kvm_call_hyp (__vgic_v5_save_apr, cpu_if); - cpu_if->gicv5_vpe.resident = false; + cpu_if->vgic_contextr =0 ; + +kvm_call_hyp (__vgic_v5_make_non_resident, cpu_if);/* The shadow priority is only updated on entering WFI */ if (vcpu_get_flag (vcpu,IN_WFI ))
include/kvm/arm_vgic.h +3 -0
@@ -525,6 +525,9 @@ struct vgic_v5_cpu_if {*/ u64 vgic_icsr; +/* The contextr used to make VPEs resident and non-resident */ +u64 vgic_contextr; +struct gicv5_vpe gicv5_vpe; };
Signature · 1 line
2.34.1
PATCH v5 16/49 KVM: arm64: gic-v5: Request doorbells when VPEs enter WFI
In reply to Sascha BischoffWhen a GICv5 VPE is made non-resident as part of the vcpu entering WFI, request a VPE doorbell so that KVM can be notified when a suitable SPI or LPI becomes pending for that VPE.
Program the doorbell priority mask, DBPM, from the effective virtual priority mask before making the VPE non-resident. DBPM is the priority threshold used by the GICv5 hardware to decide whether a pending SPI or LPI is allowed to signal the VPE doorbell. This allows hardware to signal the doorbell only for interrupts that the vcpu can actually take, and avoids waking it for interrupts masked by the guest priority state. If no interrupt can be signalled to the vcpu, leave the doorbell request clear.
Make the doorbell interrupt affine to the current CPU before requesting it. This nudges the wakeup back towards the CPU that last ran the vcpu, where the relevant state is more likely to be cache-hot, while also spreading doorbell interrupts across host PEs as different vcpus enter WFI on different CPUs.
Clear stale db_fired state before making the VPE non-resident. Any previous doorbell notification has already been consumed by this point, and clearing it before the non-resident transition ensures that a newly fired doorbell is observed.
Finally, teach kvm_vgic_vcpu_pending_irq() to report pending work for a GICv5 vcpu when its VPE doorbell has fired, in addition to the existing pending-PPI check.
--- arch/arm64/kvm/hyp/vgic-v5-sr.c | 9 ++++++++ arch/arm64/kvm/vgic/vgic-v5.c | 40 +++++++++++++++++++++++++++++++++ arch/arm64/kvm/vgic/vgic.c | 6 ++++- 3 files changed, 54 insertions(+), 1 deletion(-)
arch/arm64/kvm/hyp/vgic-v5-sr.c +9 -0
@@ -22,6 +22,15 @@ void __vgic_v5_make_resident(struct vgic_v5_cpu_if *cpu_if)void __vgic_v5_make_non_resident (struct vgic_v5_cpu_if *cpu_if) { +/* +* Clear the db_fired state to ensure that we're ready for the next +* doorbell when it is requested. If a doorbell firing caused us to +* enter the guest, then we've already consumed that state at this +* point, so this is safe to clear. Use WRITE_ONCE() to ensure we're not +* racing with the doorbell firing and setting the state true again. +*/ +WRITE_ONCE (cpu_if->gicv5_vpe.db_fired, false); +/* * Make as non-resident before actually making non-resident. Avoids race * with doorbell arriving.
arch/arm64/kvm/vgic/vgic-v5.c +40 -0
@@ -1099,6 +1099,46 @@ void vgic_v5_put(struct kvm_vcpu *vcpu)kvm_call_hyp (__vgic_v5_save_apr, cpu_if); cpu_if->vgic_contextr =0 ; +if (vcpu_get_flag (vcpu,IN_WFI )) { +u32 priority_mask; +int dbpm; + +/* +* Find the virtual running priority and use this to calculate +* the doorbell priority mask. We combine the highest active +* priority and the CPU's priority mask. The guest can't handle +* interrupts with priorities less than or equal to the virtual +* running priority, so there's literally no point in waking the +* guest for these. +* +* The priority needs to be higher than the mask to signal, so +* pick the next higher priority (subtract 1). +*/ + priority_mask =vgic_v5_get_effective_priority_mask (vcpu); + +/* +* Request a doorbell *unless* the priority is 0, indicating +* that no interrupt can wake the CPU up. +*/ +if (priority_mask) { +int db_irq =vgic_v5_vpe_db (vcpu); +struct irq_data *d =irq_get_irq_data (db_irq); +const struct cpumask *aff =irq_data_get_effective_affinity_mask (d); +int cpu =smp_processor_id (); + + dbpm = priority_mask -1 ; + cpu_if->vgic_contextr =FIELD_PREP (ICH_CONTEXTR_EL2_DB ,1 ) | +FIELD_PREP (ICH_CONTEXTR_EL2_DBPM , dbpm); + +/* +* Make the doorbell affine to this CPU, if it isn't +* already. Actively check the cpumask first as it is +* cheaper than changing the affinity every time. +*/ +if (!cpumask_test_cpu (cpu, aff)) +WARN_ON (irq_set_affinity (db_irq,cpumask_of (cpu))); + } + }kvm_call_hyp (__vgic_v5_make_non_resident, cpu_if);
arch/arm64/kvm/vgic/vgic.c +5 -1
@@ -1233,8 +1233,12 @@ int kvm_vgic_vcpu_pending_irq(struct kvm_vcpu *vcpu)unsigned long flags;struct vgic_vmcr vmcr; -if (vgic_is_v5 (vcpu->kvm)) +if (vgic_is_v5 (vcpu->kvm)) { +if (READ_ONCE (vcpu->arch.vgic_cpu.vgic_v5.gicv5_vpe.db_fired)) +return true; +return vgic_v5_has_pending_ppi (vcpu); + }if (!vcpu->kvm->arch.vgic.enabled)return false;
Signature · 1 line
2.34.1
[email protected]
AI reviewRe: [PATCH v5 16/49] KVM: arm64: gic-v5: Request doorbells when VPEs enter WFI
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider: - [High] ABBA deadlock between `rq_lock` and `desc->lock` in `vgic_v5_put` via `irq_set_affinity`. - [High]…
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
- [High] ABBA deadlock between rq_lock and desc->lock in vgic_v5_put via irq_set_affinity.
- [High] Stale db_fired state causes infinite VMEXIT loops upon subsequent WFIs.
- [High] Guest-reachable WARN_ON due to host-side irq_set_affinity failures.
--
--- Patch [16]: [PATCH v5 16/49] KVM: arm64: gic-v5: Request doorbells when VPEs enter WFI --- commit c40948b426a64dc48dae28e60304e7f29600ab3c
Author: Sascha Bischoff <[email protected]>
KVM: arm64: gic-v5: Request doorbells when VPEs enter WFI
When a GICv5 VPE is made non-resident as part of the vcpu entering WFI, request a VPE doorbell so that KVM can be notified when a suitable SPI or LPI becomes pending for that VPE.
Quoted 5 lines
arch/arm64/kvm/hyp/vgic-v5-sr.c +0
-0@@ -22,6 +22,15 @@ void __vgic_v5_make_resident(struct vgic_v5_cpu_if *cpu_if)
arch/arm64/kvm/hyp/vgic-v5-sr.c void __vgic_v5_make_resident(struct vgic_v5_cpu_if *cpu_if) · line 32
Quoted 10 lines
arch/arm64/kvm/hyp/vgic-v5-sr.c +8
-0void __vgic_v5_make_non_resident (struct vgic_v5_cpu_if *cpu_if) { +/* +* Clear the db_fired state to ensure that we're ready for the next +* doorbell when it is requested. If a doorbell firing caused us to +* enter the guest, then we've already consumed that state at this +* point, so this is safe to clear. Use WRITE_ONCE() to ensure we're not +* racing with the doorbell firing and setting the state true again. +*/ +WRITE_ONCE (cpu_if->gicv5_vpe.db_fired, false);
[Severity: High]
Will this state management regression leave the db_fired state stale for back-to-back WFIs?
Since __vgic_v5_make_non_resident() is called from vgic_v5_put(), which is invoked upon host preemption or user exit (but not standard guest re-entry after kvm_vcpu_block), db_fired appears to remain true when the guest resumes.
If the guest executes WFI again, kvm_vcpu_check_block() checks kvm_vgic_vcpu_pending_irq() in arch/arm64/kvm/vgic/vgic.c and observes db_fired is still true, which causes it to instantly abort blocking. This could result in the guest continuously spinning in VMEXITs.
Quoted 3 lines
arch/arm64/kvm/vgic/vgic-v5.c +0
-0
[ ... ]
Quoted 1 line
arch/arm64/kvm/vgic/vgic-v5.c +0
-0@@ -1099,6 +1099,46 @@ void vgic_v5_put(struct kvm_vcpu *vcpu)
[ ... ]
arch/arm64/kvm/vgic/vgic-v5.c void vgic_v5_put(struct kvm_vcpu *vcpu) · line 1139
Quoted 7 lines
arch/arm64/kvm/vgic/vgic-v5.c +7
-0+/* +* Make the doorbell affine to this CPU, if it isn't +* already. Actively check the cpumask first as it is +* cheaper than changing the affinity every time. +*/ +if (!cpumask_test_cpu (cpu, aff)) +WARN_ON (irq_set_affinity (db_irq,cpumask_of (cpu)));
[Severity: High]
Does this introduce an ABBA deadlock regression between rq_lock and desc->lock?
When a guest vCPU thread is preempted or enters WFI, the host scheduler's __schedule() holds the runqueue lock (rq_lock) while calling prepare_task_switch(). This triggers the sched_out preempt notifier, taking us through kvm_sched_out() -> kvm_arch_vcpu_put() -> kvm_vgic_put() and finally to vgic_v5_put().
Inside vgic_v5_put(), irq_set_affinity() acquires the hardware interrupt descriptor lock (desc->lock). Taking desc->lock while already holding rq_lock violates the locking hierarchy where desc->lock must be acquired before rq_lock.
[Severity: High]
Can a guest trigger this WARN_ON regression to cause a host denial of service?
Calls to irq_set_affinity() can legitimately fail and return negative error codes due to hardware constraints (such as ITS socket-crossing limitations) or resource exhaustion.
Since the guest controls when it executes WFI, it can reliably trigger this WARN_ON if the host scheduler places it on a restricted CPU. If panic_on_warn is enabled, this allows the guest to crash the host.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=16
Re: [PATCH v5 16/49] KVM: arm64: gic-v5: Request doorbells when VPEs enter WFI
Quoted 49 lines · On Fri, 2026-08-07 at 14:17 +0000, [email protected] wrote:
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
- [High] ABBA deadlock betweenrq_lockanddesc->lockinvgic_v5_putviairq_set_affinity.
- [High] Staledb_firedstate causes infinite VMEXIT loops upon subsequent WFIs.
- [High] Guest-reachableWARN_ONdue to host-sideirq_set_affinityfailures.
----- Patch [16]: [PATCH v5 16/49] KVM: arm64: gic-v5: Request doorbells when VPEs enter WFI ---
commitc40948b426a64dc48dae28e60304e7f29600ab3c
Author: Sascha Bischoff <[email protected]>KVM: arm64: gic-v5: Request doorbells when VPEs enter WFI
When a GICv5 VPE is made non-resident as part of the vcpu entering WFI, request a VPE doorbell so that KVM can be notified when a suitable SPI or LPI becomes pending for that VPE.
Quoted 20 lines
arch/arm64/kvm/hyp/vgic-v5-sr.c +0
-0@@ -22,6 +22,15 @@ void __vgic_v5_make_resident(struct vgic_v5_cpu_if *cpu_if)
void__vgic_v5_make_non_resident(struct vgic_v5_cpu_if *cpu_if){arch/arm64/kvm/hyp/vgic-v5-sr.c +2
-0+/* +* Clear the db_fired state to ensure that we're ready for the next
arch/arm64/kvm/hyp/vgic-v5-sr.c +1
-0+* doorbell when it is requested. If a doorbell firing caused us to
arch/arm64/kvm/hyp/vgic-v5-sr.c +1
-0+* enter the guest, then we've already consumed that state at this
arch/arm64/kvm/hyp/vgic-v5-sr.c +1
-0+* point, so this is safe to clear. Use WRITE_ONCE() to ensure we're not
arch/arm64/kvm/hyp/vgic-v5-sr.c +1
-0+* racing with the doorbell firing and setting the state true again.
arch/arm64/kvm/hyp/vgic-v5-sr.c void __vgic_v5_make_resident(struct vgic_v5_cpu_if *cpu_if) · line 32
Quoted 2 lines
arch/arm64/kvm/hyp/vgic-v5-sr.c +2
-0+*/ +WRITE_ONCE (cpu_if->gicv5_vpe.db_fired, false);[Severity: High]
Will this state management regression leave thedb_firedstate stale for
back-to-back WFIs?
Quoted 6 lines
Since
__vgic_v5_make_non_resident()is called fromvgic_v5_put(), which is
invoked upon host preemption or user exit (but not standard guest re- entry
afterkvm_vcpu_block),db_firedappears to remain true when the guest resumes.
We clear db_fired as part of making a VPE non-resident. That means that the state persists until control returns to the host and the VPE is made non-resident. In order for it to be set to true again the doorbell needs to be requested and then needs to fire. This can happen, but that's by design because they guest has explicitly entered WFI again.
Quoted 7 lines
If the guest executes WFI again,
kvm_vcpu_check_block()checkskvm_vgic_vcpu_pending_irq()inarch/arm64/kvm/vgic/vgic.cand observesdb_firedis still true, which causes it to instantly abort blocking. This
could result in the guest continuously spinning in VMEXITs.
It won't still be true in this case. It will have been cleared. I don't believe this to be a real issue.
Quoted 33 lines
Quoted 5 lines
arch/arm64/kvm/vgic/vgic-v5.c +0
-0[ ... ]
Quoted 1 line
arch/arm64/kvm/vgic/vgic-v5.c +0
-0@@ -1099,6 +1099,46 @@ void vgic_v5_put(struct kvm_vcpu *vcpu)[ ... ]
Quoted 11 lines
arch/arm64/kvm/vgic/vgic-v5.c +2
-0+/* +* Make the doorbell affine to this CPU, if it isn't
arch/arm64/kvm/vgic/vgic-v5.c +1
-0+* already. Actively check the cpumask first as it is
arch/arm64/kvm/vgic/vgic-v5.c +1
-0+* cheaper than changing the affinity every time.
arch/arm64/kvm/vgic/vgic-v5.c +3
-0+*/ +if (!cpumask_test_cpu (cpu, aff)) + WARN_ON(irq_set_affinity(db_irq,
cpumask_of(cpu)));[Severity: High]
Does this introduce an ABBA deadlock regression betweenrq_lockanddesc->lock?When a guest vCPU thread is preempted or enters WFI, the host scheduler's
__schedule()holds the runqueue lock (rq_lock) while callingprepare_task_switch(). This triggers thesched_outpreempt notifier, taking
us throughkvm_sched_out()->kvm_arch_vcpu_put()->kvm_vgic_put()and
finally tovgic_v5_put().
Quoted 6 lines
Inside
vgic_v5_put(),irq_set_affinity()acquires the hardware interrupt
descriptor lock (desc->lock). Takingdesc->lockwhile already holdingrq_lockviolates the locking hierarchy wheredesc->lockmust be acquired
beforerq_lock.
This is why we have a if (!cpu_if->gicv5_vpe.resident) check in the code. On the first call to vgic_v5_put(), we make the VM non-resident. On the second call, it becomes a NOP. I don't believe that this is a real issue.
Quoted 11 lines
[Severity: High]
Can a guest trigger thisWARN_ONregression to cause a host denial of service?Calls to
irq_set_affinity()can legitimately fail and return negative error
codes due to hardware constraints (such as ITS socket-crossing limitations)
or resource exhaustion.
Quoted 6 lines
Since the guest controls when it executes WFI, it can reliably trigger this
WARN_ONif the host scheduler places it on a restricted CPU. Ifpanic_on_warn
is enabled, this allows the guest to crash the host.
I've changed the warn to a kvm_debug_ratelimited(). We still want to catch it going wrong, but it should never do more than that.
Thanks,
Sascha
PATCH v5 17/49 KVM: arm64: gic-v5: Introduce struct vgic_v5_irs and IRS base address
In reply to Sascha BischoffIn order to properly emulate the operation of the IRS from KVM, we require storage for the MMIO register state. This change introduces struct vgic_v5_irs, and adds a pointer to it to the struct vgic_dist.
This new data structure contains the storage for IRS MMIO state that is required for emulating the MMIO interface in KVM. This provides persistent storage, and a way to track data across MMIO writes, e.g., selecting an SPI and updating the configuration of it is two MMIO writes.
Note that only a pointer to the data structure is added to struct vgic_dist as this new structure is very large, and hence it makes sense to dynamically allocate it and just provide a pointer to retrieve it in struct vgic_dist.
In addition to adding a structure to store the MMIO state for the IRS, we add the base address in GPA space to struct vgic_v5_irs.
--- include/kvm/arm_vgic.h | 86 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 86 insertions(+)
include/kvm/arm_vgic.h +86 -0
@@ -398,6 +398,87 @@ struct vgic_v5_vm {bool vmte_allocated; }; +/*** GICv5 ***/ +struct vgic_v5_irs { +/* base addresses in guest physical address space: */ +gpa_t vgic_v5_irs_base; + +struct vgic_io_device iodev; +struct kvm_device *dev; + +/* IRS state - used for registers etc */ +struct { +u8 domain; +u8 pa_range; +bool virt; +bool setlpi; +bool mec; +bool mpam; +bool swe; +u16 irs_id; + } idr0; + +struct { +/* PE_CNT is populated from online_vcpus at runtime */ +u8 priority_bits; + } idr1; + +struct { +u8 id_bits; +u8 min_lpi_id_bits; +bool ist_levels; +u8 ist_l2sz; +bool istmd; +u8 istmd_sz; + } idr2; + +struct { +u32 spi_range; + } idr5; + +struct { +u32 spi_irs_range; + } idr6; + +struct { +u32 spi_base; + } idr7; + +struct { +u8 sh; +u8 oc; +u8 ic; +bool ist_ra; +bool ist_wa; +bool vmt_ra; +bool vpet_ra; +bool vmd_ra; +bool vmd_wa; +bool vped_ra; +bool vped_wa; + } cr1; + +struct { +u32 id; + } spi_selr; + +struct { +u32 iaffid; + } pe_selr; + +struct { +u8 lpi_id_bits; +u8 l2sz; +u8 istsz; +bool structure; + } ist_cfgr; + +struct { +bool valid; +u64 addr; + } ist_baser; +}; +struct vgic_dist {bool in_kernel;bool ready;
@@ -475,6 +556,11 @@ struct vgic_dist {* GICv5 per-VM data. */ struct vgic_v5_vm gicv5_vm; + +/* +* GICv5 IRS data. Dynamically allocated due to the size. +*/ +struct vgic_v5_irs *vgic_v5_irs_data; };struct vgic_v2_cpu_if {
Signature · 1 line
2.34.1
PATCH v5 18/49 KVM: arm64: gic-v5: Add IRS IODEV support to MMIO handlers
In reply to Sascha BischoffIn order to support proper VMs (that support more than just PPIs) for GICv5, it is important to emulate the GICv5 IRS too. The IRS includes an MMIO interface which is used to interact with and configure the IRS.
As part of providing the emulated IRS MMIO interface in KVM, extend enum iodev_type to include a GICv5 IRS device, and extend the MMIO code to handle reads and writes to that type of IO device. This will allow the creation of a GICv5 IRS IO Device in KVM.
--- arch/arm64/kvm/vgic/vgic-mmio.c | 6 ++++++ arch/arm64/kvm/vgic/vgic-mmio.h | 2 ++ include/kvm/arm_vgic.h | 3 ++- 3 files changed, 10 insertions(+), 1 deletion(-)
arch/arm64/kvm/vgic/vgic-mmio.c +6 -0
@@ -1065,6 +1065,9 @@ static int dispatch_mmio_read(struct kvm_vcpu *vcpu, struct kvm_io_device *dev,case IODEV_ITS : data = region->its_read (vcpu->kvm, iodev->its, addr, len);break ; +case IODEV_GICV5_IRS : + data = region->read (vcpu, addr, len); +break ; }vgic_data_host_to_mmio_bus (val, len, data);
@@ -1095,6 +1098,9 @@ static int dispatch_mmio_write(struct kvm_vcpu *vcpu, struct kvm_io_device *dev,case IODEV_ITS : region->its_write (vcpu->kvm, iodev->its, addr, len, data);break ; +case IODEV_GICV5_IRS : + region->write (vcpu, addr, len, data); +break ; }return 0 ;
arch/arm64/kvm/vgic/vgic-mmio.h +2 -0
@@ -217,6 +217,8 @@ unsigned int vgic_v2_init_cpuif_iodev(struct vgic_io_device *dev);unsigned int vgic_v3_init_dist_iodev (struct vgic_io_device *dev); +unsigned int vgic_v5_init_irs_iodev (struct vgic_io_device *dev); +u64 vgic_sanitise_outer_cacheability (u64 reg);u64 vgic_sanitise_inner_cacheability (u64 reg);u64 vgic_sanitise_shareability (u64 reg);
include/kvm/arm_vgic.h +2 -1
@@ -310,7 +310,8 @@ enum iodev_type {IODEV_CPUIF ,IODEV_DIST ,IODEV_REDIST , -IODEV_ITS +IODEV_ITS , +IODEV_GICV5_IRS }; struct vgic_io_device {
Signature · 1 line
2.34.1
PATCH v5 19/49 KVM: arm64: gic-v5: Add KVM_VGIC_V5_ADDR_TYPE_IRS to UAPI
In reply to Sascha BischoffDefine the UAPI address type used by userspace to describe the location of the emulated IRS in guest physical address space, together with the size reserved for that region.
As per the GICv5 specification, the IRS has one CONFIG_FRAME and optionally one SETLPI_FRAME per interrupt domain. Within a KVM VM we are only concerned with one interrupt domain. Each of these frames is 64kB in size, so reserve 2x64kB of contiguous memory in the GPA space for a GICv5 IRS.
--- arch/arm64/include/uapi/asm/kvm.h | 5 +++++ tools/arch/arm64/include/uapi/asm/kvm.h | 5 +++++ 2 files changed, 10 insertions(+)
arch/arm64/include/uapi/asm/kvm.h +5 -0
@@ -97,6 +97,11 @@ struct kvm_regs {#define KVM_VGIC_V3_REDIST_SIZE (2 * SZ_64K)#define KVM_VGIC_V3_ITS_SIZE (2 * SZ_64K) +/* Supported VGICv5 address types */ +#define KVM_VGIC_V5_ADDR_TYPE_IRS 6 + +#define KVM_VGIC_V5_IRS_SIZE (2 * SZ_64K) +#define KVM_ARM_VCPU_POWER_OFF 0/* CPU is started in OFF state */ #define KVM_ARM_VCPU_EL1_32BIT 1/* CPU running a 32bit VM */ #define KVM_ARM_VCPU_PSCI_0_2 2/* CPU uses PSCI v0.2 */
tools/arch/arm64/include/uapi/asm/kvm.h +5 -0
@@ -97,6 +97,11 @@ struct kvm_regs {#define KVM_VGIC_V3_REDIST_SIZE (2 * SZ_64K)#define KVM_VGIC_V3_ITS_SIZE (2 * SZ_64K) +/* Supported VGICv5 address types */ +#define KVM_VGIC_V5_ADDR_TYPE_IRS 6 + +#define KVM_VGIC_V5_IRS_SIZE (2 * SZ_64K) +#define KVM_ARM_VCPU_POWER_OFF 0/* CPU is started in OFF state */ #define KVM_ARM_VCPU_EL1_32BIT 1/* CPU running a 32bit VM */ #define KVM_ARM_VCPU_PSCI_0_2 2/* CPU uses PSCI v0.2 */
Signature · 1 line
2.34.1
[email protected]
AI reviewRe: [PATCH v5 19/49] KVM: arm64: gic-v5: Add KVM_VGIC_V5_ADDR_TYPE_IRS to UAPI
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [High] The patch defines the UAPI constant KVM_VGIC_V5_ADDR_TYPE_IRS for userspace to specify the IRS…
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [High] The patch defines the UAPI constant KVM_VGIC_V5_ADDR_TYPE_IRS for userspace to specify the IRS location, but provides no corresponding kernel implementation to handle it.
- [Low] The newly added UAPI macro KVM_VGIC_V5_IRS_SIZE uses the kernel-internal macro SZ_64K, which is not exported to userspace and will cause compilation errors. --
--- Patch [19]: [PATCH v5 19/49] KVM: arm64: gic-v5: Add KVM_VGIC_V5_ADDR_TYPE_IRS to UAPI --- commit 2deebb3b97ce3e2f88f0cdf0452104edbd0a7ea6
Author: Sascha Bischoff <[email protected]>
KVM: arm64: gic-v5: Add KVM_VGIC_V5_ADDR_TYPE_IRS to UAPI
This patch adds the UAPI definitions for the GICv5 IRS address type and size to the arm64 KVM headers. It reserves 2x64kB in the GPA space for the IRS CONFIG_FRAME and SETLPI_FRAME.
Quoted 4 lines
arch/arm64/include/uapi/asm/kvm.h +0
-0
[ ... ]
arch/arm64/include/uapi/asm/kvm.h struct kvm_regs { · line 102
Quoted 7 lines
arch/arm64/include/uapi/asm/kvm.h +3
-0@@ -97,6 +97,11 @@ struct kvm_regs {#define KVM_VGIC_V3_REDIST_SIZE (2 * SZ_64K)#define KVM_VGIC_V3_ITS_SIZE (2 * SZ_64K) +/* Supported VGICv5 address types */ +#define KVM_VGIC_V5_ADDR_TYPE_IRS 6 +
[Severity: High]
Does this patch provide the kernel implementation to handle KVM_VGIC_V5_ADDR_TYPE_IRS?
If userspace attempts to use this UAPI definition with the KVM_DEV_ARM_VGIC_GRP_ADDR attribute group, vgic_v5_set_attr() explicitly returns -ENXIO for that group:
arch/arm64/kvm/vgic/vgic-kvm-device.c:vgic_v5_set_attr() {
switch (attr->group) {
case KVM_DEV_ARM_VGIC_GRP_ADDR:
case KVM_DEV_ARM_VGIC_GRP_CPU_SYSREGS:
case KVM_DEV_ARM_VGIC_GRP_NR_IRQS:
return -ENXIO;...
}
Similarly, kvm_vgic_addr() lacks a case for KVM_VGIC_V5_ADDR_TYPE_IRS and would reject it with -ENODEV:
arch/arm64/kvm/vgic/vgic-kvm-device.c:kvm_vgic_addr() {
...
default: r = -ENODEV; }
...
}
Without handling in the kernel, userspace cannot configure the base address of the GICv5 IRS, which renders the GICv5 IRS emulation feature unusable.
arch/arm64/include/uapi/asm/kvm.h struct kvm_regs { · line 104
Quoted 2 lines
arch/arm64/include/uapi/asm/kvm.h +2
-0+#define KVM_VGIC_V5_IRS_SIZE (2 * SZ_64K) +
[Severity: Low]
Does this newly added UAPI definition rely on the kernel-internal macro SZ_64K?
Since <linux/sizes.h> is not exported to userspace, userspace programs attempting to use KVM_VGIC_V5_IRS_SIZE will fail to compile with an undeclared identifier error for SZ_64K.
While this pattern already exists in the file for GICv3 sizes, introducing a new instance perpetuates the UAPI breakage.
Quoted 7 lines
arch/arm64/include/uapi/asm/kvm.h +0
-0#define KVM_ARM_VCPU_POWER_OFF 0/* CPU is started in OFF state */ #define KVM_ARM_VCPU_EL1_32BIT 1/* CPU running a 32bit VM */ #define KVM_ARM_VCPU_PSCI_0_2 2/* CPU uses PSCI v0.2 */ tools/arch/arm64/include/uapi/asm/kvm.h +0
-0
[ ... ]
tools/arch/arm64/include/uapi/asm/kvm.h struct kvm_regs { · line 104
Quoted 9 lines
tools/arch/arm64/include/uapi/asm/kvm.h +5
-0@@ -97,6 +97,11 @@ struct kvm_regs {#define KVM_VGIC_V3_REDIST_SIZE (2 * SZ_64K)#define KVM_VGIC_V3_ITS_SIZE (2 * SZ_64K) +/* Supported VGICv5 address types */ +#define KVM_VGIC_V5_ADDR_TYPE_IRS 6 + +#define KVM_VGIC_V5_IRS_SIZE (2 * SZ_64K) +
[Severity: Low]
Are the same SZ_64K and missing implementation issues applicable to this mirrored tools UAPI header?
Quoted 3 lines
tools/arch/arm64/include/uapi/asm/kvm.h +0
-0#define KVM_ARM_VCPU_POWER_OFF 0/* CPU is started in OFF state */ #define KVM_ARM_VCPU_EL1_32BIT 1/* CPU running a 32bit VM */ #define KVM_ARM_VCPU_PSCI_0_2 2/* CPU uses PSCI v0.2 */
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=19
Re: [PATCH v5 19/49] KVM: arm64: gic-v5: Add KVM_VGIC_V5_ADDR_TYPE_IRS to UAPI
Quoted 29 lines · On Fri, 2026-08-07 at 14:27 +0000, [email protected] wrote:
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [High] The patch defines the UAPI constantKVM_VGIC_V5_ADDR_TYPE_IRSfor userspace to specify the IRS location, but provides no corresponding kernel implementation to handle it.
- [Low] The newly added UAPI macroKVM_VGIC_V5_IRS_SIZEuses the kernel-internal macroSZ_64K, which is not exported to userspace and will cause compilation errors.
----- Patch [19]: [PATCH v5 19/49] KVM: arm64: gic-v5: Add
KVM_VGIC_V5_ADDR_TYPE_IRSto UAPI ---
commit2deebb3b97ce3e2f88f0cdf0452104edbd0a7ea6
Author: Sascha Bischoff <[email protected]>KVM: arm64: gic-v5: Add
KVM_VGIC_V5_ADDR_TYPE_IRSto UAPIThis patch adds the UAPI definitions for the GICv5 IRS address type and size
to the arm64 KVM headers. It reserves 2x64kB in the GPA space for the IRSCONFIG_FRAMEandSETLPI_FRAME.Quoted 5 lines
arch/arm64/include/uapi/asm/kvm.h +0
-0[ ... ]
Quoted 11 lines
arch/arm64/include/uapi/asm/kvm.h struct kvm_regs { · line 102
Quoted 7 lines
arch/arm64/include/uapi/asm/kvm.h +0
-0@@ -97,6 +97,11 @@ struct kvm_regs { #define KVM_VGIC_V3_REDIST_SIZE (2 * SZ_64K) #define KVM_VGIC_V3_ITS_SIZE (2 * SZ_64K)arch/arm64/include/uapi/asm/kvm.h +3
-0+/* Supported VGICv5 address types */ +#define KVM_VGIC_V5_ADDR_TYPE_IRS 6 +[Severity: High]
Does this patch provide the kernel implementation to handleKVM_VGIC_V5_ADDR_TYPE_IRS?
No, it does not.
Quoted 24 lines
If userspace attempts to use this UAPI definition with the
KVM_DEV_ARM_VGIC_GRP_ADDRattribute group,vgic_v5_set_attr()explicitly
returns-ENXIOfor that group:arch/arm64/kvm/vgic/vgic-kvm-device.c:vgic_v5_set_attr() { switch (attr->group) { case KVM_DEV_ARM_VGIC_GRP_ADDR: case KVM_DEV_ARM_VGIC_GRP_CPU_SYSREGS: case KVM_DEV_ARM_VGIC_GRP_NR_IRQS: return -ENXIO;...
}Similarly,
kvm_vgic_addr()lacks a case forKVM_VGIC_V5_ADDR_TYPE_IRSand
would reject it with-ENODEV:arch/arm64/kvm/vgic/vgic-kvm-device.c:kvm_vgic_addr() {
...default: r = -ENODEV; }
Quoted 7 lines
...
}Without handling in the kernel, userspace cannot configure the base address
of the GICv5 IRS, which renders the GICv5 IRS emulation feature unusable.
See next commits in series.
Quoted 8 lines
arch/arm64/include/uapi/asm/kvm.h
Quoted 3 lines
arch/arm64/include/uapi/asm/kvm.h +2
-0+#define KVM_VGIC_V5_IRS_SIZE (2 * SZ_64K) +[Severity: Low]
Does this newly added UAPI definition rely on the kernel-internal macroSZ_64K?
This matches the existing defines for GICv3. I don't think that this is an issue.
Quoted 21 lines
Since <
linux/sizes.h> is not exported to userspace, userspace programs
attempting to useKVM_VGIC_V5_IRS_SIZEwill fail to compile with an undeclared identifier error forSZ_64K.While this pattern already exists in the file for GICv3 sizes, introducing a
new instance perpetuates the UAPI breakage.Quoted 10 lines
#define
KVM_ARM_VCPU_POWER_OFF0 /* CPU is started in OFF
state */
#defineKVM_ARM_VCPU_EL1_32BIT1 /* CPU running a 32bit
VM */
#defineKVM_ARM_VCPU_PSCI_0_22 /* CPU uses PSCI v0.2 */tools/arch/arm64/include/uapi/asm/kvm.h +0
-0[ ... ]
Quoted 14 lines
tools/arch/arm64/include/uapi/asm/kvm.h
Quoted 9 lines
tools/arch/arm64/include/uapi/asm/kvm.h +0
-0@@ -97,6 +97,11 @@ struct kvm_regs { #define KVM_VGIC_V3_REDIST_SIZE (2 * SZ_64K) #define KVM_VGIC_V3_ITS_SIZE (2 * SZ_64K)tools/arch/arm64/include/uapi/asm/kvm.h +5
-0+/* Supported VGICv5 address types */ +#define KVM_VGIC_V5_ADDR_TYPE_IRS 6 + +#define KVM_VGIC_V5_IRS_SIZE (2 * SZ_64K) +[Severity: Low]
Are the sameSZ_64Kand missing implementation issues applicable to this
mirrored tools UAPI header?
See above.
Quoted 7 lines
Quoted 6 lines
#define
KVM_ARM_VCPU_POWER_OFF0 /* CPU is started in OFF
state */
#defineKVM_ARM_VCPU_EL1_32BIT1 /* CPU running a 32bit
VM */
#defineKVM_ARM_VCPU_PSCI_0_22 /* CPU uses PSCI v0.2 */
Thanks,
Sascha
PATCH v5 20/49 KVM: arm64: gic-v5: Add GICv5 IRS IODEV and MMIO emulation
In reply to Sascha BischoffIn order to properly support GICv5-based VMs in KVM, emulate the CONFIG_FRAME for a virtual IRS. This emulation needs to handle guest accesses to the MMIO region and mimic the behaviour of a real IRS.
Introduce an IODEV for the GICv5 IRS and an associated initialisation function that sets up the SPIs and initial IRS state. The MMIO emulation allows the guest to query the IRS_IDx registers, manipulate SPIs, configure ISTs, and so forth.
The emulation tracks selector state across MMIO accesses. For example, a guest writes IRS_PE_SELR to select a PE by IAFFID. This is the VPE ID for a VM, but the guest does not know this. If the guest reads IRS_PE_STATUSR, KVM checks whether that IAFFID selects a valid VPE and sets the V bit accordingly. IRS_PE_CR0 is accepted as write-ignored because KVM does not support 1-of-N routing.
The same selector and status register model is exposed for SPIs.
Track the state of IRS_CR0.IRSEN and introduce KVM_REQ_RELOAD_GICv5 to reload the GICv5 context of running vCPUs when it changes. Only make the request when the enable state changes, avoiding unnecessary IPIs for writes that leave it unchanged.
The LPI IST requires KVM to perform actions on behalf of the guest. Treat changes to IRS_IST_BASER.Valid as the lifetime of the guest's IST. On an Invalid-to-Valid transition, validate IRS_IST_CFGR, allocate a shadow host IST, and assign it to the physical IRS through the VMTE. On a Valid-to-Invalid transition, invalidate and free the host IST. Ignore guest address changes while the BASER remains valid, and prevent changes to IRS_IST_CFGR during that time.
As far as the guest is concerned, the IST memory it provided is being used by the hardware, but the physical IRS uses the host shadow IST instead.
This change provides the core IRS IODEV and MMIO emulation, but does not plumb the device into the rest of KVM yet. The CoreSight identification registers are added separately.
--- arch/arm64/include/asm/kvm_host.h | 1 + arch/arm64/kvm/Makefile | 2 +- arch/arm64/kvm/arm.c | 8 + arch/arm64/kvm/vgic/vgic-irs-v5.c | 765 +++++++++++++++++++++++++++ arch/arm64/kvm/vgic/vgic-v5-tables.c | 16 + arch/arm64/kvm/vgic/vgic-v5-tables.h | 1 + arch/arm64/kvm/vgic/vgic.h | 2 + include/kvm/arm_vgic.h | 1 + 8 files changed, 795 insertions(+), 1 deletion(-) create mode 100644 arch/arm64/kvm/vgic/vgic-irs-v5.c
arch/arm64/include/asm/kvm_host.h +1 -0
@@ -55,6 +55,7 @@#define KVM_REQ_GUEST_HYP_IRQ_PENDING KVM_ARCH_REQ(9)#define KVM_REQ_MAP_L1_VNCR_EL2 KVM_ARCH_REQ(10)#define KVM_REQ_VGIC_PROCESS_UPDATE KVM_ARCH_REQ(11) +#define KVM_REQ_RELOAD_GICv5 KVM_ARCH_REQ(12)#define KVM_DIRTY_LOG_MANUAL_CAPS (KVM_DIRTY_LOG_MANUAL_PROTECT_ENABLE | \ KVM_DIRTY_LOG_INITIALLY_SET)
arch/arm64/kvm/Makefile +1 -1
@@ -24,7 +24,7 @@ kvm-y += arm.o mmu.o mmio.o psci.o hypercalls.o pvtime.o \ vgic/vgic-mmio.o vgic/vgic-mmio-v2.o \ vgic/vgic-mmio-v3.o vgic/vgic-kvm-device.o \ vgic/vgic-its.o vgic/vgic-debug.o vgic/vgic-v3-nested.o \ - vgic/vgic-v5.o vgic/vgic-v5-tables.o + vgic/vgic-v5.o vgic/vgic-v5-tables.o vgic/vgic-irs-v5.o kvm-$(CONFIG_HW_PERF_EVENTS) += pmu-emul.o pmu.o kvm-$(CONFIG_ARM64_PTR_AUTH) += pauth.o
arch/arm64/kvm/arm.c +8 -0
@@ -1151,6 +1151,14 @@ static int check_vcpu_requests(struct kvm_vcpu *vcpu)preempt_enable (); } +if (kvm_check_request (KVM_REQ_RELOAD_GICv5, vcpu)) { +/* The IRS enable bit was changed */ +preempt_disable (); +vgic_v5_put (vcpu); +vgic_v5_load (vcpu); +preempt_enable (); + } +if (kvm_check_request (KVM_REQ_RELOAD_PMU , vcpu))kvm_vcpu_reload_pmu (vcpu);
arch/arm64/kvm/vgic/vgic-irs-v5.c new file +765 -0
765 lines not on this page · raw message
arch/arm64/kvm/vgic/vgic-v5-tables.c +16 -0
@@ -642,6 +642,22 @@ int vgic_v5_vmte_release(struct kvm *kvm)return 0 ; } +/* +* Provide a way for the IRS MMIO emulation to correctly populate the number of +* IAFFID bits (which correspond to our vpe_id_bits. +*/ +u8 vgic_v5_vmte_vpe_id_bits (struct kvm_vcpu *vcpu) +{ +u32 vm_id =vgic_v5_vm_id (vcpu->kvm); +struct vgic_v5_vm_info *vmi; + + vmi =xa_load (&vm_info, vm_id); +if (!vmi) +return 0 ; + +return vmi->vpe_id_bits; +} +/* Provide the preallocated VPE descriptor to the hardware via the VPE Table. */ int vgic_v5_vmte_alloc_vpe (struct kvm_vcpu *vcpu) {
arch/arm64/kvm/vgic/vgic-v5-tables.h +1 -0
@@ -113,6 +113,7 @@ void vgic_v5_release_vm_id(struct kvm *kvm);int vgic_v5_vmte_init (struct kvm *kvm);int vgic_v5_vmte_release (struct kvm *kvm); +u8 vgic_v5_vmte_vpe_id_bits (struct kvm_vcpu *vcpu);int vgic_v5_vmte_alloc_vpe (struct kvm_vcpu *vcpu);int vgic_v5_vmte_free_vpe (struct kvm_vcpu *vcpu);
arch/arm64/kvm/vgic/vgic.h +2 -0
@@ -366,6 +366,7 @@ void vgic_debug_destroy(struct kvm *kvm);int vgic_v5_probe (const struct gic_kvm_info *info);void vgic_v5_reset (struct kvm_vcpu *vcpu);int vgic_v5_init (struct kvm *kvm); +int kvm_vgic_v5_irs_init (struct kvm *kvm,unsigned int nr_spis);void vgic_v5_teardown (struct kvm *kvm);int vgic_v5_map_resources (struct kvm *kvm);void vgic_v5_set_ppi_ops (struct kvm_vcpu *vcpu,u32 vintid);
@@ -378,6 +379,7 @@ void vgic_v5_set_vmcr(struct kvm_vcpu *vcpu, struct vgic_vmcr *vmcr);void vgic_v5_get_vmcr (struct kvm_vcpu *vcpu,struct vgic_vmcr *vmcr);void vgic_v5_restore_state (struct kvm_vcpu *vcpu);void vgic_v5_save_state (struct kvm_vcpu *vcpu); +int vgic_v5_register_irs_iodev (struct kvm *kvm,gpa_t irs_base_address);#define for_each_visible_v5_ppi (__i, __k) \ for_each_set_bit(__i, (__k)->arch.vgic.gicv5_vm.vgic_ppi_mask, VGIC_V5_NR_PRIVATE_IRQS)
include/kvm/arm_vgic.h +1 -0
@@ -40,6 +40,7 @@* in KVM for now. At a future stage, this can be bumped up to 128, if required. */ #define VGIC_V5_NR_PRIVATE_IRQS 64 +#define VGIC_V5_DEFAULT_NR_SPIS 32#define is_v5_type (t, i) (FIELD_GET(GICV5_HWIRQ_TYPE, (i)) == (t))
Signature · 1 line
2.34.1
[email protected]
AI reviewRe: [PATCH v5 20/49] KVM: arm64: gic-v5: Add GICv5 IRS IODEV and MMIO emulation
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider: - [High] 64-bit IRS MMIO registers lack support for 32-bit accesses, leading to unhandled MMIO aborts and…
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
- [High] 64-bit IRS MMIO registers lack support for 32-bit accesses, leading to unhandled MMIO aborts and broken read-modify-write behavior.
- [Medium] Missing locking in the IRS IST MMIO read handler allows guests to observe logically torn register states.
- [Medium] Lockless, sequential updates to GICV5_IRS_CR1 can result in a torn MMIO write.
--
--- Patch [20]: [PATCH v5 20/49] KVM: arm64: gic-v5: Add GICv5 IRS IODEV and MMIO emulation --- commit e3d747ac0ec8b60b894092ad4f2dc0c7959423ef
Author: Sascha Bischoff <[email protected]>
KVM: arm64: gic-v5: Add GICv5 IRS IODEV and MMIO emulation
In order to properly support GICv5-based VMs in KVM, emulate the CONFIG_FRAME for a virtual IRS. This emulation needs to handle guest accesses to the MMIO region and mimic the behaviour of a real IRS.
Quoted 5 lines
arch/arm64/kvm/vgic/vgic-irs-v5.c new file +0
-0
[ ... ]
Quoted 1 line
arch/arm64/kvm/vgic/vgic-irs-v5.c new file +0
-0@@ -0,0 +1,765 @@
[ ... ]
Quoted 8 lines
arch/arm64/kvm/vgic/vgic-irs-v5.c new file +8
-0+static void vgic_v5_mmio_write_irs_misc (struct kvm_vcpu *vcpu,gpa_t addr, +unsigned int len,unsigned long val) +{ +struct vgic_v5_irs *irs =vgic_v5_get_irs (vcpu); +struct vgic_dist *vgic =vgic_v5_get_vgic (vcpu); +const size_t offset = addr & (SZ_64K -1 ); + +switch (offset) {
[ ... ]
arch/arm64/kvm/vgic/vgic-irs-v5.c line 205
Quoted 13 lines
arch/arm64/kvm/vgic/vgic-irs-v5.c new file +13
-0+case GICV5_IRS_CR1 : + irs->cr1.sh =FIELD_GET (GICV5_IRS_CR1_SH , val); + irs->cr1.oc =FIELD_GET (GICV5_IRS_CR1_OC , val); + irs->cr1.ic =FIELD_GET (GICV5_IRS_CR1_IC , val); + irs->cr1.ist_ra = !!(val &GICV5_IRS_CR1_IST_RA ); + irs->cr1.ist_wa = !!(val &GICV5_IRS_CR1_IST_WA ); + irs->cr1.vmt_ra = !!(val &GICV5_IRS_CR1_VMT_RA ); + irs->cr1.vpet_ra = !!(val &GICV5_IRS_CR1_VPET_RA ); + irs->cr1.vmd_ra = !!(val &GICV5_IRS_CR1_VMD_RA ); + irs->cr1.vmd_wa = !!(val &GICV5_IRS_CR1_VMD_WA ); + irs->cr1.vped_ra = !!(val &GICV5_IRS_CR1_VPED_RA ); + irs->cr1.vped_wa = !!(val &GICV5_IRS_CR1_VPED_WA ); +return ;
[Severity: Medium]
Since these sequential writes are performed without holding config_lock, is there a risk of data races? If multiple vCPUs concurrently write different values to GICV5_IRS_CR1, it seems like the internal KVM state for irs->cr1 could end up in a torn state combining the concurrent writes.
[ ... ]
Quoted 8 lines
arch/arm64/kvm/vgic/vgic-irs-v5.c new file +8
-0+static unsigned long vgic_v5_mmio_read_irs_ist (struct kvm_vcpu *vcpu, +gpa_t addr,unsigned int len) +{ +struct vgic_v5_irs *irs =vgic_v5_get_irs (vcpu); +const size_t offset = addr & (SZ_64K -1 ); +u64 value =0 ; + +switch (offset) {
[ ... ]
arch/arm64/kvm/vgic/vgic-irs-v5.c line 395
Quoted 6 lines
arch/arm64/kvm/vgic/vgic-irs-v5.c new file +6
-0+case GICV5_IRS_IST_BASER : + value =FIELD_PREP (GICV5_IRS_IST_BASER_ADDR_MASK , + irs->ist_baser.addr >>GICV5_IRS_IST_BASER_ADDR_SHIFT ); +if (irs->ist_baser.valid) + value |=GICV5_IRS_IST_BASER_VALID ; +break ;
[Severity: Medium]
Can guests observe logically torn register states here? The write handler vgic_v5_mmio_write_irs_ist() correctly holds config_lock to sequentially update irs->ist_baser.valid and irs->ist_baser.addr, but this read handler accesses them locklessly. If a vCPU reads GICV5_IRS_IST_BASER concurrently with a write, it could see an impossible state combining the old address with the new valid bit or vice-versa.
[ ... ]
Quoted 8 lines
arch/arm64/kvm/vgic/vgic-irs-v5.c new file +8
-0+static void vgic_v5_mmio_write_irs_ist (struct kvm_vcpu *vcpu,gpa_t addr, +unsigned int len,unsigned long val) +{ +struct vgic_v5_irs *irs =vgic_v5_get_irs (vcpu); +const size_t offset = addr & (SZ_64K -1 ); +enum gicv5_vcpu_cmd cmd =LPI_VIST_MAKE_INVALID ; + +switch (offset) {
[ ... ]
arch/arm64/kvm/vgic/vgic-irs-v5.c line 422
Quoted 1 line
arch/arm64/kvm/vgic/vgic-irs-v5.c new file +1
-0+case GICV5_IRS_IST_BASER : {
[Severity: High]
Does this strict matching on exact offsets break read-modify-write for 32-bit accesses to the high half of the register? If a guest performs a 32-bit write to offset + 4, it looks like this switch statement drops it. Also, interpreting a 32-bit payload as a full 64-bit value without adjusting for the offset would corrupt the register state.
[ ... ]
Quoted 1 line
+static const struct
vgic_register_regionvgic_v5_irs_registers[] = {
[ ... ]
arch/arm64/kvm/vgic/vgic-irs-v5.c line 531
Quoted 3 lines
arch/arm64/kvm/vgic/vgic-irs-v5.c new file +3
-0+REGISTER_DESC_WITH_LENGTH (GICV5_IRS_IST_BASER , vgic_v5_mmio_read_irs_ist, + vgic_v5_mmio_write_irs_ist,8 , + VGIC_ACCESS_64bit),
[Severity: High]
Could omitting VGIC_ACCESS_32bit for this 64-bit register cause the KVM IO bus to reject valid 32-bit guest accesses? If KVM's mmio dispatcher rejects a 32-bit access, the request will fall back to userspace IO. Since userspace does not emulate the in-kernel IRS, this usually results in an unhandled data abort injected into the guest.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=20
Re: [PATCH v5 20/49] KVM: arm64: gic-v5: Add GICv5 IRS IODEV and MMIO emulation
Quoted 61 lines · On Fri, 2026-08-07 at 14:34 +0000, [email protected] wrote:
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
- [High] 64-bit IRS MMIO registers lack support for 32-bit accesses, leading to unhandled MMIO aborts and broken read-modify-write behavior.
- [Medium] Missing locking in the IRS IST MMIO read handler allows guests to observe logically torn register states.
- [Medium] Lockless, sequential updates toGICV5_IRS_CR1can result in a torn MMIO write.
----- Patch [20]: [PATCH v5 20/49] KVM: arm64: gic-v5: Add GICv5 IRS IODEV and MMIO emulation ---
commite3d747ac0ec8b60b894092ad4f2dc0c7959423ef
Author: Sascha Bischoff <[email protected]>KVM: arm64: gic-v5: Add GICv5 IRS IODEV and MMIO emulation
In order to properly support GICv5-based VMs in KVM, emulate theCONFIG_FRAMEfor a virtual IRS. This emulation needs to handle guest accesses to the MMIO region and mimic the behaviour of a real IRS.Quoted 6 lines
arch/arm64/kvm/vgic/vgic-irs-v5.c new file +0
-0[ ... ]
Quoted 1 line
arch/arm64/kvm/vgic/vgic-irs-v5.c new file +0
-0@@ -0,0 +1,765 @@[ ... ]
Quoted 10 lines
+static void vgic_v5_mmio_write_irs_misc(struct
kvm_vcpu*vcpu,gpa_taddr,arch/arm64/kvm/vgic/vgic-irs-v5.c new file +1
-0+unsigned int len, unsignedlong val)
arch/arm64/kvm/vgic/vgic-irs-v5.c new file +6
-0+{ +struct vgic_v5_irs *irs =vgic_v5_get_irs (vcpu); +struct vgic_dist *vgic =vgic_v5_get_vgic (vcpu); +const size_t offset = addr & (SZ_64K -1 ); + +switch (offset) {[ ... ]
Quoted 15 lines
arch/arm64/kvm/vgic/vgic-irs-v5.c new file +8
-0+case GICV5_IRS_CR1 : + irs->cr1.sh =FIELD_GET (GICV5_IRS_CR1_SH , val); + irs->cr1.oc =FIELD_GET (GICV5_IRS_CR1_OC , val); + irs->cr1.ic =FIELD_GET (GICV5_IRS_CR1_IC , val); + irs->cr1.ist_ra = !!(val &GICV5_IRS_CR1_IST_RA ); + irs->cr1.ist_wa = !!(val &GICV5_IRS_CR1_IST_WA ); + irs->cr1.vmt_ra = !!(val &GICV5_IRS_CR1_VMT_RA ); + irs->cr1.vpet_ra = !!(val &
GICV5_IRS_CR1_VPET_RA);arch/arm64/kvm/vgic/vgic-irs-v5.c new file +3
-0+ irs->cr1.vmd_ra = !!(val &GICV5_IRS_CR1_VMD_RA ); + irs->cr1.vmd_wa = !!(val &GICV5_IRS_CR1_VMD_WA ); + irs->cr1.vped_ra = !!(val &
GICV5_IRS_CR1_VPED_RA);arch/arm64/kvm/vgic/vgic-irs-v5.c new file +1
-0+ irs->cr1.vped_wa = !!(val &
GICV5_IRS_CR1_VPED_WA);arch/arm64/kvm/vgic/vgic-irs-v5.c
Quoted 1 line
arch/arm64/kvm/vgic/vgic-irs-v5.c new file +1
-0+return ;
Quoted 8 lines
[Severity: Medium]
Since these sequential writes are performed without holdingconfig_lock,
is there a risk of data races? If multiple vCPUs concurrently write different values toGICV5_IRS_CR1, it seems like the internal KVM state
forirs->cr1could end up in a torn state combining the concurrent writes.
While I agree that this would break, it would be broken software, and we don't actually use the values written to this register - we just provide them back to the guest if and what it reads them. Therefore, I don't think that this is worth the extra complications of changing it.
Quoted 24 lines
[ ... ]
Quoted 10 lines
+static unsigned long vgic_v5_mmio_read_irs_ist(struct
kvm_vcpu*vcpu,arch/arm64/kvm/vgic/vgic-irs-v5.c new file +1
-0+ gpa_t addr,unsigned int len)
arch/arm64/kvm/vgic/vgic-irs-v5.c new file +6
-0+{ +struct vgic_v5_irs *irs =vgic_v5_get_irs (vcpu); +const size_t offset = addr & (SZ_64K -1 ); +u64 value =0 ; + +switch (offset) {[ ... ]
arch/arm64/kvm/vgic/vgic-irs-v5.c line 395
Quoted 7 lines
arch/arm64/kvm/vgic/vgic-irs-v5.c new file +3
-0+case GICV5_IRS_IST_BASER : + value = FIELD_PREP(GICV5_IRS_IST_BASER_ADDR_MASK, + irs->ist_baser.addr >>
GICV5_IRS_IST_BASER_ADDR_SHIFT);arch/arm64/kvm/vgic/vgic-irs-v5.c new file +3
-0+if (irs->ist_baser.valid) + value |=GICV5_IRS_IST_BASER_VALID ; +break ;[Severity: Medium]
Can guests observe logically torn register states here? The write
Quoted 8 lines
handler
vgic_v5_mmio_write_irs_ist()correctly holdsconfig_lockto sequentially
updateirs->ist_baser.validandirs->ist_baser.addr, but this read handler accesses them locklessly. If a vCPU readsGICV5_IRS_IST_BASERconcurrently with a write, it could see an impossible state combining the
old address with the new valid bit or vice-versa.
As above, this is something that we shouldn't need to guard against. If software both writes and reads this register at the same time from multiple vCPUs, then that software should expect races. Frankly, this is broken software, and it will potentially get gibberish back.
Quoted 14 lines
[ ... ]
Quoted 10 lines
+static void vgic_v5_mmio_write_irs_ist(struct
kvm_vcpu*vcpu,gpa_taddr,arch/arm64/kvm/vgic/vgic-irs-v5.c new file +1
-0+ unsigned int len, unsignedlong val)
arch/arm64/kvm/vgic/vgic-irs-v5.c new file +6
-0+{ +struct vgic_v5_irs *irs =vgic_v5_get_irs (vcpu); +const size_t offset = addr & (SZ_64K -1 ); +enum gicv5_vcpu_cmd cmd =LPI_VIST_MAKE_INVALID ; + +switch (offset) {[ ... ]
Quoted 11 lines
arch/arm64/kvm/vgic/vgic-irs-v5.c line 422
Quoted 1 line
arch/arm64/kvm/vgic/vgic-irs-v5.c new file +1
-0+case GICV5_IRS_IST_BASER : {[Severity: High]
Does this strict matching on exact offsets break read-modify-write for
32-bit accesses to the high half of the register? If a guest performs a
32-bit write to offset + 4, it looks like this switch statement drops it.
Also, interpreting a 32-bit payload as a full 64-bit value without adjusting for the offset would corrupt the register state.
I've extended the accessors to cope with 32-bit accesses for the IRS_IST_BASER. This is the only implemented register we have that supports both 32- and 64-bit accesses. The rest are RAZ/WI in the current implementation. I've also enabled the 32-bit accesses for those, but they have no effects otherwise.
Quoted 6 lines
[ ... ]
Quoted 2 lines
+static const struct
vgic_register_regionvgic_v5_irs_registers[] = {[ ... ]
Quoted 16 lines
arch/arm64/kvm/vgic/vgic-irs-v5.c line 531
Quoted 4 lines
arch/arm64/kvm/vgic/vgic-irs-v5.c new file +1
-0+ REGISTER_DESC_WITH_LENGTH(GICV5_IRS_IST_BASER,
vgic_v5_mmio_read_irs_ist,arch/arm64/kvm/vgic/vgic-irs-v5.c new file +2
-0+ vgic_v5_mmio_write_irs_ist, 8, + VGIC_ACCESS_64bit),[Severity: High]
Could omittingVGIC_ACCESS_32bitfor this 64-bit register cause the KVM
IO bus to reject valid 32-bit guest accesses? If KVM's mmio dispatcher
rejects a 32-bit access, the request will fall back to userspace IO. Since
userspace does not emulate the in-kernel IRS, this usually results in an
unhandled data abort injected into the guest.
See my comment above - I have added 32-bit accesses for the IRS_IST_BASER.
Thanks,
Sascha
PATCH v5 21/49 KVM: arm64: gic-v5: Initialise per-VM IRS state
In reply to Sascha BischoffA virtual GICv5 needs an emulated IRS in addition to the host IRS state used to back VMTEs, VPEs, and ISTs. Without this, KVM can only provide the CPU-local PPI state and cannot expose the IRS-backed SPI and LPI configuration expected by a GICv5 guest.
Allocate the per-VM emulated IRS state when creating a virtual GICv5, and initialise it from vgic_v5_init(). If userspace has not provided a number of SPIs, use the GICv5 default of 32. The IRS init path allocates the SPI state, initialises the virtual IRS register state, and creates the backing SPI IST when SPIs are present.
Keep the per-VM IRS object alive for the lifetime of the virtual GICv5. vgic_v5_teardown() only unwinds resources allocated by vgic_v5_init(), so failed initialisation can be retried, while kvm_vgic_dist_destroy() frees the IRS object during final VGIC destruction.
This gives virtual GICv5s the IRS backing required for SPIs and LPIs, rather than being limited to PPIs only. Further patches add support for SPI injection and lifecycle tracking.
--- arch/arm64/kvm/vgic/vgic-init.c | 59 +++++++++++++++++++++++---------- arch/arm64/kvm/vgic/vgic-v5.c | 8 ++++- 2 files changed, 49 insertions(+), 18 deletions(-)
arch/arm64/kvm/vgic/vgic-init.c +42 -17
@@ -191,28 +191,48 @@ int kvm_vgic_create(struct kvm *kvm, u32 type)break ; } -if (ret) { -kvm_for_each_vcpu (i, vcpu, kvm) { -struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu; -kfree (vgic_cpu->private_irqs); - vgic_cpu->private_irqs =NULL ; - } - - kvm->arch.vgic.vgic_model =0 ; - goto out_unlock; - } +if (ret) + goto out_free_private_irqs;if (type ==KVM_DEV_TYPE_ARM_VGIC_V3 ) kvm->arch.vgic.nassgicap =system_supports_direct_sgis (); -/* -* We now know that we have a GICv5. The Arch Timer PPI interrupts may -* have been initialised at this stage, but will have done so assuming -* that we have an older GIC, meaning that the IntIDs won't be -* correct. We init them again, and this time they will be correct. -*/ -if (type ==KVM_DEV_TYPE_ARM_VGIC_V5 ) +if (type ==KVM_DEV_TYPE_ARM_VGIC_V5 ) { +/* Allocate a vIRS for GICv5 systems */ + kvm->arch.vgic.vgic_v5_irs_data = kzalloc_obj(struct vgic_v5_irs, +GFP_KERNEL_ACCOUNT ); +if (!kvm->arch.vgic.vgic_v5_irs_data) { + ret = -ENOMEM ; + goto out_free_private_irqs; + } + +/* +* Initialization happens later, for now just explicitly +* disable the device and undef its base address. +*/ + kvm->arch.vgic.vgic_v5_irs_data->vgic_v5_irs_base =VGIC_ADDR_UNDEF ; + +/* +* We now know that we have a GICv5. The Arch Timer PPI +* interrupts may have been initialised at this stage, but will +* have done so assuming that we have an older GIC, meaning that +* the IntIDs won't be correct. We init them again, and this +* time they will be correct. +*/ kvm_timer_init_vm (kvm); + } + + goto out_unlock; + +out_free_private_irqs: +kvm_for_each_vcpu (i, vcpu, kvm) { +struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu; + +kfree (vgic_cpu->private_irqs); + vgic_cpu->private_irqs =NULL ; + } + + kvm->arch.vgic.vgic_model =0 ; out_unlock:mutex_unlock (&kvm->arch.config_lock);
@@ -496,6 +516,9 @@ int vgic_init(struct kvm *kvm)return ret; } }else { +if (!dist->nr_spis) + dist->nr_spis =VGIC_V5_DEFAULT_NR_SPIS ; + ret =vgic_v5_init (kvm);if (ret)return ret;
@@ -541,6 +564,8 @@ static void kvm_vgic_dist_destroy(struct kvm *kvm)break ;case KVM_DEV_TYPE_ARM_VGIC_V5 :vgic_v5_teardown (kvm); +kfree (dist->vgic_v5_irs_data); + dist->vgic_v5_irs_data =NULL ;break ; }
arch/arm64/kvm/vgic/vgic-v5.c +7 -1
@@ -672,7 +672,8 @@ void vgic_v5_teardown(struct kvm *kvm)/* * Claim and populate a VMTE (optionally making a new L2 VMT valid), create VPE -* doorbells, allocate VPET and populate for each VPE. +* doorbells, allocate VPET and populate for each VPE. Finally, we also init the +* vIRS, which means allocating and making the virtual SPI IST valid. * * Note: We do need to put the cart before the horse here. The VPE doorbells are * our conduit for communication with the IRS, which means we need to have those
@@ -751,6 +752,11 @@ int vgic_v5_init(struct kvm *kvm) goto err; } +/* Init IRS (and alloc SPI IST) */ + ret =kvm_vgic_v5_irs_init (kvm, kvm->arch.vgic.nr_spis); +if (ret) + goto err; +return 0 ; err:
Signature · 1 line
2.34.1
[email protected]
AI reviewRe: [PATCH v5 21/49] KVM: arm64: gic-v5: Initialise per-VM IRS state
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] Unprivileged userspace can flood the kernel log (dmesg) by repeatedly triggering a `pr_warn` in…
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] Unprivileged userspace can flood the kernel log (dmesg) by repeatedly triggering a pr_warn in kvm_vgic_v5_irs_init(). --
--- Patch [21]: [PATCH v5 21/49] KVM: arm64: gic-v5: Initialise per-VM IRS state --- commit 1471593eea3e98c7d1747cf0894424955cddde44
Author: Sascha Bischoff <[email protected]>
KVM: arm64: gic-v5: Initialise per-VM IRS state
A virtual GICv5 needs an emulated IRS in addition to the host IRS state used to back VMTEs, VPEs, and ISTs. This patch allocates the per-VM emulated IRS state when creating a virtual GICv5, and initialises it from vgic_v5_init(). It also handles the teardown and cleanup of the IRS state during VGIC destruction.
Quoted 4 lines
arch/arm64/kvm/vgic/vgic-v5.c +0
-0
[ ... ]
arch/arm64/kvm/vgic/vgic-v5.c int vgic_v5_init(struct kvm *kvm) · line 756
Quoted 6 lines
arch/arm64/kvm/vgic/vgic-v5.c +2
-0@@ -751,6 +752,11 @@ int vgic_v5_init(struct kvm *kvm) goto err; } +/* Init IRS (and alloc SPI IST) */ + ret =kvm_vgic_v5_irs_init (kvm, kvm->arch.vgic.nr_spis);
[Severity: Medium]
Does this new call path allow userspace to flood the kernel log?
When userspace initializes a GICv5 via ioctl, this path calls kvm_vgic_v5_irs_init(). If the host IRS hardware supports fewer than 16 ID bits for ISTs, kvm_vgic_v5_irs_init() emits an unconditional warning:
arch/arm64/kvm/vgic/vgic-irs-v5.c:kvm_vgic_v5_irs_init() {
...
if (vgic_v5_irs_ist_id_bits(&irs_caps) < 16)
pr_warn("Host IRS supports fewer than 16 ID bits for ISTs (%u)\n",
vgic_v5_irs_ist_id_bits(&irs_caps));
...
}Since an unprivileged user can repeatedly create and destroy a VM in a tight loop, can this be used to spam dmesg and degrade system performance? Should this warning be converted to pr_warn_once() or be rate-limited?
Quoted 6 lines
arch/arm64/kvm/vgic/vgic-v5.c +3
-0+if (ret) + goto err; +return 0 ; err:
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=21
Re: [PATCH v5 21/49] KVM: arm64: gic-v5: Initialise per-VM IRS state
Quoted 53 lines · On Fri, 2026-08-07 at 14:49 +0000, [email protected] wrote:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] Unprivileged userspace can flood the kernel log (dmesg) by repeatedly triggering apr_warninkvm_vgic_v5_irs_init(). ----- Patch [21]: [PATCH v5 21/49] KVM: arm64: gic-v5: Initialise per- VM IRS state ---
commit1471593eea3e98c7d1747cf0894424955cddde44
Author: Sascha Bischoff <[email protected]>KVM: arm64: gic-v5: Initialise per-VM IRS state
A virtual GICv5 needs an emulated IRS in addition to the host IRS state used to back VMTEs, VPEs, and ISTs. This patch allocates the per-VM emulated IRS state when creating a virtual GICv5, and initialises
it fromvgic_v5_init(). It also handles the teardown and cleanup of the
IRS state during VGIC destruction.Quoted 5 lines
arch/arm64/kvm/vgic/vgic-v5.c +0
-0[ ... ]
arch/arm64/kvm/vgic/vgic-v5.c int vgic_v5_init(struct kvm *kvm) · line 756
Quoted 6 lines
arch/arm64/kvm/vgic/vgic-v5.c +0
-0@@ -751,6 +752,11 @@ int vgic_v5_init(struct kvm *kvm) goto err; }arch/arm64/kvm/vgic/vgic-v5.c +2
-0+/* Init IRS (and alloc SPI IST) */ + ret =kvm_vgic_v5_irs_init (kvm, kvm->arch.vgic.nr_spis);[Severity: Medium]
Does this new call path allow userspace to flood the kernel log?When userspace initializes a GICv5 via ioctl, this path calls
kvm_vgic_v5_irs_init(). If the host IRS hardware supports fewer than 16 ID
bits for ISTs,kvm_vgic_v5_irs_init()emits an unconditional warning:arch/arm64/kvm/vgic/vgic-irs-v5.c:kvm_vgic_v5_irs_init() {
...
if (vgic_v5_irs_ist_id_bits(&irs_caps)< 16) pr_warn("Host IRS supports fewer than 16 ID bits for ISTs (%u)\n",
vgic_v5_irs_ist_id_bits(&irs_caps));
...
}
Quoted 5 lines
Since an unprivileged user can repeatedly create and destroy a VM in a tight
loop, can this be used to spam dmesg and degrade system performance? Should
this warning be converted topr_warn_once()or be rate-limited?
I've made it rate-limited. I feel that us more useful than a pr_warn_once().
Quoted 8 lines
Quoted 7 lines
arch/arm64/kvm/vgic/vgic-v5.c +3
-0+if (ret) + goto err; +return 0;
err:
Thanks,
Sascha
PATCH v5 22/49 KVM: arm64: gic-v5: Register the IRS IODEV
In reply to Sascha BischoffNow that we have an emulated IRS, it needs to be registered, which ensures that guest accesses to the MMIO regions handled by the device are handled appropriately in KVM. Therefore, as part of vgic_map_resources, the GICv5 IRS IODEV is registered. If the address for the IRS is not provided, bail out reporting an error - this is not a supported config.
As part of this change, expose setting the address of the emulated IRS via KVM_VGIC_V5_ADDR_TYPE_IRS to userspace. Also allow userspace to set the number of SPIs handled by the emulated GICv5 implementation, using a GICv5-specific SPI count rather than the legacy total interrupt count.
Limit the configurable range to 32 through 1024 SPIs, in multiples of
32. KVM keeps one struct vgic_irq per SPI in a physically contiguous allocation, and allowing the full 16-bit KVM_IRQ_LINE SPI namespace would make that allocation exceed KMALLOC_MAX_SIZE on common arm64 configurations.
The default routing has one IRQCHIP route per configured SPI, so size the common IRQ routing table for the largest GICv5 configuration. The model-specific routing validation retains the 988-pin limit for GICv2 and GICv3.
--- arch/arm64/kvm/vgic/vgic-init.c | 18 ++++- arch/arm64/kvm/vgic/vgic-kvm-device.c | 106 ++++++++++++++++++-------- include/kvm/arm_vgic.h | 8 +- 3 files changed, 97 insertions(+), 35 deletions(-)
arch/arm64/kvm/vgic/vgic-init.c +14 -4
@@ -686,9 +686,8 @@ int vgic_lazy_init(struct kvm *kvm)int kvm_vgic_map_resources (struct kvm *kvm) {struct vgic_dist *dist = &kvm->arch.vgic; -bool needs_dist = true;enum vgic_type type; -gpa_t dist_base; +gpa_t dist_base, irs_base;int ret =0 ;if (likely (smp_load_acquire (&dist->ready)))
@@ -711,13 +710,12 @@ int kvm_vgic_map_resources(struct kvm *kvm) }else { ret =vgic_v5_map_resources (kvm); type =VGIC_V5 ; - needs_dist = false; }if (ret) goto out; -if (needs_dist) { +if (type !=VGIC_V5 ) { dist_base = dist->vgic_dist_base;mutex_unlock (&kvm->arch.config_lock);
@@ -727,7 +725,19 @@ int kvm_vgic_map_resources(struct kvm *kvm) goto out_slots; } }else { + irs_base = dist->vgic_v5_irs_data->vgic_v5_irs_base;mutex_unlock (&kvm->arch.config_lock); + +if (IS_VGIC_ADDR_UNDEF (irs_base)) { + ret = -ENXIO ; + goto out_slots; + } + + ret =vgic_v5_register_irs_iodev (kvm, irs_base); +if (ret) { +kvm_err ("Unable to register VGIC IRS MMIO regions\n" ); + goto out_slots; + } }smp_store_release (&dist->ready, true);
arch/arm64/kvm/vgic/vgic-kvm-device.c +76 -30
179 lines not on this page · raw message
include/kvm/arm_vgic.h +7 -1
@@ -31,7 +31,12 @@#define VGIC_MAX_SPI 1019#define VGIC_MAX_RESERVED 1023#define VGIC_MIN_LPI 8192 -#define KVM_IRQCHIP_NUM_PINS (1020 - 32) + +/* +* The common IRQ routing table must accommodate all GIC types. GICv2 and +* GICv3 need 988 pins, while GICv5 needs 1024 pins. +*/ +#define KVM_IRQCHIP_NUM_PINS 1024/* * GICv5 supports 128 PPIs, but only the first 64 are architected. We only
@@ -41,6 +46,7 @@*/ #define VGIC_V5_NR_PRIVATE_IRQS 64#define VGIC_V5_DEFAULT_NR_SPIS 32 +#define VGIC_V5_MAX_NR_SPIS BIT(10)#define is_v5_type (t, i) (FIELD_GET(GICV5_HWIRQ_TYPE, (i)) == (t))
Signature · 1 line
2.34.1
[email protected]
AI reviewRe: [PATCH v5 22/49] KVM: arm64: gic-v5: Register the IRS IODEV
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [High] GICv5 SPI injection via IRQFD and KVM_IRQ_ROUTING_IRQCHIP is broken due to missing UAPI INTID…
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [High] GICv5 SPI injection via IRQFD and KVM_IRQ_ROUTING_IRQCHIP is broken due to missing UAPI INTID translation and incomplete vgic_get_irq() support.
- [High] PMU IRQ configuration is broken for GICv5 VMs due to missing UAPI INTID translation.
--
--- Patch [22]: [PATCH v5 22/49] KVM: arm64: gic-v5: Register the IRS IODEV --- commit 1f7c421def2656a75d9f4c6c3c9bc8b90309dbb8
Author: Sascha Bischoff <[email protected]>
Subject: KVM: arm64: gic-v5: Register the IRS IODEV
This commit registers the GICv5 IRS IODEV during vgic_map_resources. It also exposes the IRS address configuration to userspace and allows setting the number of SPIs handled by the GICv5 implementation.
Quoted 4 lines
arch/arm64/kvm/vgic/vgic-kvm-device.c +0
-0
[ ... ]
Quoted 1 line
arch/arm64/kvm/vgic/vgic-kvm-device.c +0
-0@@ -224,31 +232,48 @@ static int vgic_set_common_attr(struct kvm_device *dev,
[ ... ]
Quoted 10 lines
arch/arm64/kvm/vgic/vgic-kvm-device.c +10
-0+ }else { +/* +* GICv5 reports a number of SPIs, not a total number of +* interrupts. Require a multiple of 32 SPIs. +*/ +if (val <VGIC_V5_DEFAULT_NR_SPIS || + val >VGIC_V5_MAX_NR_SPIS || + (val &31 )) +return -EINVAL ; +
arch/arm64/kvm/vgic/vgic-kvm-device.c static int vgic_set_common_attr(struct kvm_device *dev, · line 279
Quoted 10 lines
arch/arm64/kvm/vgic/vgic-kvm-device.c +7
-0+mutex_lock (&dev->kvm->arch.config_lock); +if (vgic_initialized (dev->kvm) || dev->kvm->arch.vgic.nr_spis) + ret = -EBUSY ; +else + dev->kvm->arch.vgic.nr_spis = val; +mutex_unlock (&dev->kvm->arch.config_lock); + }return ret; }
[Severity: High]
Does this leave PMU IRQ configuration broken for GICv5 VMs?
Since the UAPI INTID lacks the GICv5 internal hardware type bits, the PMU configuration fails unconditionally during validation. When userspace configures the PMU IRQ using the KVM_ARM_VCPU_PMU_V3_IRQ attribute (passing a standard PPI), the check fails:
arch/arm64/kvm/pmu-emul.c:kvm_arm_pmu_v3_set_attr() {
...
if (!(irq_is_ppi(vcpu->kvm, irq) || irq_is_spi(vcpu->kvm, irq)))
return -EINVAL;
...
}Because irq_is_ppi() and irq_is_spi() require the GICV5_HWIRQ_TYPE bits that are missing from the raw integer, will this cause PMU setup to fail with -EINVAL for GICv5 VMs?
Quoted 5 lines
include/kvm/arm_vgic.h +0
-0
include/kvm/arm_vgic.h line 39
Quoted 10 lines
include/kvm/arm_vgic.h +6
-1@@ -31,7 +31,12 @@#define VGIC_MAX_SPI 1019#define VGIC_MAX_RESERVED 1023#define VGIC_MIN_LPI 8192 -#define KVM_IRQCHIP_NUM_PINS (1020 - 32) + +/* +* The common IRQ routing table must accommodate all GIC types. GICv2 and +* GICv3 need 988 pins, while GICv5 needs 1024 pins. +*/ +#define KVM_IRQCHIP_NUM_PINS 1024
[Severity: High]
Does expanding the IRQ routing table to 1024 pins expose a gap in GICv5 SPI injection support?
While this expands the pins for GICv5, the underlying injection mechanisms do not translate the UAPI INTID. For example, in vgic_irqfd_set_irq():
arch/arm64/kvm/vgic/vgic-irqfd.c:vgic_irqfd_set_irq() {
...
unsigned int spi_id = e->irqchip.pin + VGIC_NR_PRIVATE_IRQS;
...
if (!vgic_valid_spi(kvm, spi_id))
return -EINVAL;
...
}This calculates spi_id without the required GICV5_HWIRQ_TYPE_SPI bits, causing vgic_valid_spi() to fail for GICv5.
Additionally, vgic_get_irq() hardcodes a NULL return for GICv5 SPIs, which blocks injection entirely:
arch/arm64/kvm/vgic/vgic.c:vgic_get_irq() {
...
if (vgic_is_v5(kvm))
return NULL;
...
}Will this prevent guests using device pass-through or virtio devices that rely on SPIs from receiving interrupts via irqfd/eventfd?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=22
Re: [PATCH v5 22/49] KVM: arm64: gic-v5: Register the IRS IODEV
Quoted 75 lines · On Fri, 2026-08-07 at 14:52 +0000, [email protected] wrote:
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [High] GICv5 SPI injection via IRQFD andKVM_IRQ_ROUTING_IRQCHIPis broken due to missing UAPI INTID translation and incompletevgic_get_irq()support.
- [High] PMU IRQ configuration is broken for GICv5 VMs due to missing UAPI INTID translation.
----- Patch [22]: [PATCH v5 22/49] KVM: arm64: gic-v5: Register the IRS IODEV ---
commit1f7c421def2656a75d9f4c6c3c9bc8b90309dbb8
Author: Sascha Bischoff <[email protected]>
Subject: KVM: arm64: gic-v5: Register the IRS IODEVThis commit registers the GICv5 IRS IODEV during
vgic_map_resources. It
also exposes the IRS address configuration to userspace and allows setting
the number of SPIs handled by the GICv5 implementation.Quoted 5 lines
arch/arm64/kvm/vgic/vgic-kvm-device.c +0
-0[ ... ]
Quoted 2 lines
arch/arm64/kvm/vgic/vgic-kvm-device.c +0
-0@@ -224,31 +232,48 @@ static int vgic_set_common_attr(struct kvm_device *dev,[ ... ]
Quoted 20 lines
arch/arm64/kvm/vgic/vgic-kvm-device.c +3
-0+ }else { +/* +* GICv5 reports a number of SPIs, not a total number of
arch/arm64/kvm/vgic/vgic-kvm-device.c +1
-0+* interrupts. Require a multiple of 32 SPIs.
arch/arm64/kvm/vgic/vgic-kvm-device.c +8
-0+*/ +if (val <VGIC_V5_DEFAULT_NR_SPIS || + val > VGIC_V5_MAX_NR_SPIS || + (val & 31)) +return -EINVAL ; + +mutex_lock (&dev->kvm->arch.config_lock); + if (vgic_initialized(dev->kvm) || dev-Quoted 1 line
kvm->arch.vgic.nr_spis)arch/arm64/kvm/vgic/vgic-kvm-device.c +5
-0+ ret = -EBUSY ; + else + dev->kvm->arch.vgic.nr_spis = val; +mutex_unlock (&dev->kvm->arch.config_lock); + }Quoted 3 lines
return ret;
}[Severity: High]
Does this leave PMU IRQ configuration broken for GICv5 VMs?Since the UAPI INTID lacks the GICv5 internal hardware type bits, the PMU configuration fails unconditionally during validation. When userspace
configures the PMU IRQ using theKVM_ARM_VCPU_PMU_V3_IRQattribute (passing
a standard PPI), the check fails:arch/arm64/kvm/pmu-emul.c:kvm_arm_pmu_v3_set_attr() {
...
if (!(irq_is_ppi(vcpu->kvm, irq) ||irq_is_spi(vcpu->kvm, irq))) return-EINVAL;
...
}
Quoted 5 lines
Because
irq_is_ppi()andirq_is_spi()require theGICV5_HWIRQ_TYPEbits
that are missing from the raw integer, will this cause PMU setup to fail
with-EINVALfor GICv5 VMs?
No, because we require a GICv5 VMM to provide a valid GICv5-style interrupt ID. If the VMM just provided us with an ID without a type with a vGICv5, we have no way whatsoever of knowing if it is a PPI or SPI. For GICv5, we mandate that it is the architected PPI, and that's it. Nothing else is allowed.
Quoted 14 lines
Quoted 14 lines
include/kvm/arm_vgic.h +3
-1@@ -31,7 +31,12 @@ #define VGIC_MAX_SPI 1019 #define VGIC_MAX_RESERVED 1023 #define VGIC_MIN_LPI 8192 -#define KVM_IRQCHIP_NUM_PINS (1020 - 32) + +/* +* The common IRQ routing table must accommodate all GIC types. GICv2 and
Quoted 8 lines
Quoted 3 lines
+ * GICv3 need 988 pins, while GICv5 needs 1024 pins.
+ */
+#defineKVM_IRQCHIP_NUM_PINS1024[Severity: High]
Does expanding the IRQ routing table to 1024 pins expose a gap in GICv5
SPI injection support?
With this commit, we don't yet support SPI injection, so this is not an issue. The support for SPI injection is added in a subsequent commit. Furthermore, the irqfd support is also added in a later commit in this series.
Quoted 29 lines
While this expands the pins for GICv5, the underlying injection mechanisms
do not translate the UAPI INTID. For example, invgic_irqfd_set_irq():arch/arm64/kvm/vgic/vgic-irqfd.c:vgic_irqfd_set_irq() {
...
unsigned intspi_id=e->irqchip.pin+VGIC_NR_PRIVATE_IRQS;
...
if (!vgic_valid_spi(kvm,spi_id))
return-EINVAL;
...
}This calculates
spi_idwithout the requiredGICV5_HWIRQ_TYPE_SPIbits,
causingvgic_valid_spi()to fail for GICv5.Additionally,
vgic_get_irq()hardcodes a NULL return for GICv5 SPIs, which blocks injection entirely:arch/arm64/kvm/vgic/vgic.c:vgic_get_irq() {
...
if (vgic_is_v5(kvm))
return NULL;
...
}
Quoted 3 lines
Will this prevent guests using device pass-through or virtio devices that rely on SPIs from receiving interrupts via irqfd/eventfd?
As of this commit, that is indeed the case as we've not added the support in yet. Hence, this is expected at this stage in the series.
Thanks,
Sascha
PATCH v5 23/49 KVM: arm64: gic-v5: Set IRICHPPIDIS based on IRS enable state
In reply to Sascha BischoffThe GICv5 ICH_CONTEXTR_EL2 register has the IRICHPPIDIS field, which allows the hypervisor to disable HPPI selection for SPIs and LPIs. Use this to emulate the guest enabling and disabling the IRS.
Program IRICHPPIDIS from the virtual IRS enable state when loading a VPE. This prevents SPIs and LPIs from being delivered until the guest enables the emulated IRS, matching the behaviour of real hardware.
Likewise, when making a VPE non-resident as it enters WFI, only calculate the doorbell priority mask and request a doorbell if the virtual IRS is enabled. A disabled IRS cannot select an SPI or LPI to wake the VPE, so neither the priority calculation nor a doorbell request serves any purpose in that state.
--- arch/arm64/kvm/vgic/vgic-v5.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-)
arch/arm64/kvm/vgic/vgic-v5.c +6 -1
@@ -1063,6 +1063,7 @@ void vgic_v5_flush_ppi_state(struct kvm_vcpu *vcpu)void vgic_v5_load (struct kvm_vcpu *vcpu) { +bool irichppidis = !READ_ONCE (vcpu->kvm->arch.vgic.enabled);struct vgic_v5_cpu_if *cpu_if = &vcpu->arch.vgic_cpu.vgic_v5;u16 vm =vgic_v5_vm_id (vcpu->kvm);u16 vpe =vgic_v5_vpe_id (vcpu);
@@ -1079,6 +1080,7 @@ void vgic_v5_load(struct kvm_vcpu *vcpu)kvm_call_hyp (__vgic_v5_restore_vmcr_apr, cpu_if); cpu_if->vgic_contextr =FIELD_PREP (ICH_CONTEXTR_EL2_V , true) | +FIELD_PREP (ICH_CONTEXTR_EL2_IRICHPPIDIS , irichppidis) |FIELD_PREP (ICH_CONTEXTR_EL2_VPE , vpe) |FIELD_PREP (ICH_CONTEXTR_EL2_VM , vm);
@@ -1105,7 +1107,10 @@ void vgic_v5_put(struct kvm_vcpu *vcpu)kvm_call_hyp (__vgic_v5_save_apr, cpu_if); cpu_if->vgic_contextr =0 ; -if (vcpu_get_flag (vcpu,IN_WFI )) { + +/* Request a doorbell if entering WFI, unless the IRS is disabled */ +if (vcpu_get_flag (vcpu,IN_WFI ) && +READ_ONCE (vcpu->kvm->arch.vgic.enabled)) {u32 priority_mask;int dbpm;
Signature · 1 line
2.34.1
PATCH v5 24/49 KVM: arm64: selftests: Update vGICv5 selftest to set IRS address
In reply to Sascha BischoffThis selftest was added before the GICv5 IRS was supported in KVM. Therefore, there was no address to set, and the specific UAPI didn't even exist.
Now that the IRS is supported, and setting its address is mandatory before VGIC resources are mapped, set the emulated IRS GPA before initialising the VGIC. Running a GICv5 VM will fail if userspace has not provided the IRS address before the first vCPU run.
--- tools/testing/selftests/kvm/arm64/vgic_v5.c | 6 ++++++ tools/testing/selftests/kvm/include/arm64/gic_v5.h | 3 +++ 2 files changed, 9 insertions(+)
tools/testing/selftests/kvm/arm64/vgic_v5.c +6 -0
@@ -100,6 +100,7 @@ static void test_vgic_v5_ppis(u32 gic_dev_type)struct ucall uc;u64 user_ppis[2 ];struct vm_gic v; +uint64_t attr;int ret, i; v.gic_dev_type = gic_dev_type;
@@ -116,6 +117,11 @@ static void test_vgic_v5_ppis(u32 gic_dev_type)for (i =0 ; i <NR_VCPUS ; i++)vcpu_init_descriptor_tables (vcpus[i]); +/* Set the address of the IRS before initialising the GIC */ + attr =GICV5_IRS_CONFIG_BASE_GPA ; +kvm_device_attr_set (v.gic_fd,KVM_DEV_ARM_VGIC_GRP_ADDR , +KVM_VGIC_V5_ADDR_TYPE_IRS , &attr); +kvm_device_attr_set (v.gic_fd,KVM_DEV_ARM_VGIC_GRP_CTRL ,KVM_DEV_ARM_VGIC_CTRL_INIT ,NULL );
tools/testing/selftests/kvm/include/arm64/gic_v5.h +3 -0
@@ -10,6 +10,9 @@#include "processor.h" +/* GIC component base address is guest PA space */ +#define GICV5_IRS_CONFIG_BASE_GPA 0x8000000ULL +/* * Definitions for GICv5 instructions for the Current Domain */
Signature · 1 line
2.34.1
[email protected]
AI reviewRe: [PATCH v5 24/49] KVM: arm64: selftests: Update vGICv5 selftest to set IRS address
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] The selftest unconditionally sets the newly introduced `KVM_VGIC_V5_ADDR_TYPE_IRS` device…
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] The selftest unconditionally sets the newly introduced KVM_VGIC_V5_ADDR_TYPE_IRS device attribute, which will assert and fail on older kernels that support GICv5 but lack this new UAPI. --
--- Patch [24]: [PATCH v5 24/49] KVM: arm64: selftests: Update vGICv5 selftest to set IRS address --- commit ea26c5c169753d8ee7936265dd3dd56864a96817
Author: Sascha Bischoff <[email protected]>
KVM: arm64: selftests: Update vGICv5 selftest to set IRS address
This commit updates the vGICv5 selftest to configure the IRS address before initializing the VGIC. Setting this address is now mandatory for running a GICv5 VM, as resources cannot be mapped without it.
Quoted 3 lines
tools/testing/selftests/kvm/arm64/vgic_v5.c +0
-0
[ ... ]
tools/testing/selftests/kvm/arm64/vgic_v5.c static void test_vgic_v5_ppis(u32 gic_dev_type) · line 123
Quoted 8 lines
tools/testing/selftests/kvm/arm64/vgic_v5.c +4
-0@@ -116,6 +117,11 @@ static void test_vgic_v5_ppis(u32 gic_dev_type)for (i =0 ; i <NR_VCPUS ; i++)vcpu_init_descriptor_tables (vcpus[i]); +/* Set the address of the IRS before initialising the GIC */ + attr =GICV5_IRS_CONFIG_BASE_GPA ; +kvm_device_attr_set (v.gic_fd,KVM_DEV_ARM_VGIC_GRP_ADDR , +KVM_VGIC_V5_ADDR_TYPE_IRS , &attr);
[Severity: Medium]
Will this cause the selftest to fail when run against older kernels that support GICv5 but lack the new KVM_VGIC_V5_ADDR_TYPE_IRS attribute?
Because kvm_device_attr_set() asserts on failure, running this test against an older kernel might abort the test suite entirely rather than gracefully skipping it.
Should this code use __kvm_has_device_attr() to check for the presence of KVM_VGIC_V5_ADDR_TYPE_IRS before setting it, or gracefully handle the error if it is missing?
Quoted 2 lines
tools/testing/selftests/kvm/arm64/vgic_v5.c +0
-0kvm_device_attr_set (v.gic_fd,KVM_DEV_ARM_VGIC_GRP_CTRL ,KVM_DEV_ARM_VGIC_CTRL_INIT ,NULL );
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=24
Re: [PATCH v5 24/49] KVM: arm64: selftests: Update vGICv5 selftest to set IRS address
Quoted 26 lines · On Fri, 2026-08-07 at 15:04 +0000, [email protected] wrote:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] The selftest unconditionally sets the newly introducedKVM_VGIC_V5_ADDR_TYPE_IRSdevice attribute, which will assert and fail on older kernels that support GICv5 but lack this new UAPI. ----- Patch [24]: [PATCH v5 24/49] KVM: arm64: selftests: Update vGICv5 selftest to set IRS address ---
commitea26c5c169753d8ee7936265dd3dd56864a96817
Author: Sascha Bischoff <[email protected]>KVM: arm64: selftests: Update vGICv5 selftest to set IRS address
This commit updates the vGICv5 selftest to configure the IRS address before initializing the VGIC. Setting this address is now mandatory for
running a GICv5 VM, as resources cannot be mapped without it.Quoted 4 lines
tools/testing/selftests/kvm/arm64/vgic_v5.c +0
-0[ ... ]
Quoted 15 lines
tools/testing/selftests/kvm/arm64/vgic_v5.c static void test_vgic_v5_ppis(u32 gic_dev_type) · line 123
Quoted 10 lines
tools/testing/selftests/kvm/arm64/vgic_v5.c +0
-0@@ -116,6 +117,11 @@ static void test_vgic_v5_ppis(u32 gic_dev_type) for (i = 0; i < NR_VCPUS; i++) vcpu_init_descriptor_tables(vcpus[i]);tools/testing/selftests/kvm/arm64/vgic_v5.c +1
-0+/* Set the address of the IRS before initialising the GIC */
tools/testing/selftests/kvm/arm64/vgic_v5.c +3
-0+ attr =GICV5_IRS_CONFIG_BASE_GPA ; +kvm_device_attr_set (v.gic_fd,KVM_DEV_ARM_VGIC_GRP_ADDR , + KVM_VGIC_V5_ADDR_TYPE_IRS, &attr);[Severity: Medium]
Will this cause the selftest to fail when run against older kernels that
support GICv5 but lack the newKVM_VGIC_V5_ADDR_TYPE_IRSattribute?
Yes. That's to be expected. With older kernels, this UAPI didn't exist. With new ones, it is mandatory. We can't have a meaningful VM with GICv5 that doesn't have an IRS. The IRS is required such that we have support for more than one vCPU, SPIs, and LPIs. Therefore, we mandate it henceforth.
Quoted 7 lines
Because
kvm_device_attr_set()asserts on failure, running this test against
an older kernel might abort the test suite entirely rather than gracefully
skipping it.
Quoted 9 lines
Should this code use
__kvm_has_device_attr()to check for the presence ofKVM_VGIC_V5_ADDR_TYPE_IRSbefore setting it, or gracefully handle the error
if it is missing?Quoted 2 lines
kvm_device_attr_set(v.gic_fd, KVM_DEV_ARM_VGIC_GRP_CTRL, KVM_DEV_ARM_VGIC_CTRL_INIT, NULL);
We don't want to allow it to be omitted once this series is merged, so this is intentional.
Thanks,
Sascha
PATCH v5 25/49 KVM: arm64: gic-v5: Add GIC VDPEND hyp call
In reply to Sascha BischoffWith PPIs, their state is injected via the ICH_PPI_x_EL2 system registers. For SPIs and LPIs, there are no such registers as these would limit the number of interrupts significantly. Instead, SPI and LPI pending state can be managed from the hypervisor using the GIC VDPEND instruction. This provides a way to set an SPI or LPI for a VM as pending or non-pending, i.e., to inject interrupts into a guest.
This system instruction is only executable from EL2, and therefore must be wrapped in a hypercall for NVHE/hVHE configurations.
With pKVM this hypercall is explicitly a no-op as we currently don't support the combination of GICv5 and pKVM.
--- arch/arm64/include/asm/kvm_asm.h | 1 + arch/arm64/include/asm/kvm_hyp.h | 1 + arch/arm64/kvm/hyp/nvhe/hyp-main.c | 13 +++++++++++++ arch/arm64/kvm/hyp/vgic-v5-sr.c | 10 ++++++++++ 4 files changed, 25 insertions(+)
arch/arm64/include/asm/kvm_asm.h +1 -0
@@ -89,6 +89,7 @@ enum __kvm_host_smccc_func { __KVM_HOST_SMCCC_FUNC___vgic_v3_restore_vmcr_aprs, __KVM_HOST_SMCCC_FUNC___vgic_v5_make_resident, __KVM_HOST_SMCCC_FUNC___vgic_v5_make_non_resident, + __KVM_HOST_SMCCC_FUNC___vgic_v5_vdpend, __KVM_HOST_SMCCC_FUNC___vgic_v5_save_apr, __KVM_HOST_SMCCC_FUNC___vgic_v5_restore_vmcr_apr,
arch/arm64/include/asm/kvm_hyp.h +1 -0
@@ -97,6 +97,7 @@ void __vgic_v5_save_ppi_state(struct vgic_v5_cpu_if *cpu_if);void __vgic_v5_restore_ppi_state (struct vgic_v5_cpu_if *cpu_if);void __vgic_v5_save_state (struct vgic_v5_cpu_if *cpu_if);void __vgic_v5_restore_state (struct vgic_v5_cpu_if *cpu_if); +void __vgic_v5_vdpend (u32 intid,bool pending,u16 vm);#ifdef __KVM_NVHE_HYPERVISOR__void __timer_enable_traps (struct kvm_vcpu *vcpu);
arch/arm64/kvm/hyp/nvhe/hyp-main.c +13 -0
@@ -725,6 +725,18 @@ static void handle___vgic_v5_restore_vmcr_apr(struct kvm_cpu_context *host_ctxt)__vgic_v5_restore_vmcr_apr (kern_hyp_va (cpu_if)); } +static void handle___vgic_v5_vdpend (struct kvm_cpu_context *host_ctxt) +{ +DECLARE_REG (u32, intid, host_ctxt,1 ); +DECLARE_REG (bool, pending, host_ctxt,2 ); +DECLARE_REG (u16, vm, host_ctxt,3 ); + +if (unlikely (is_protected_kvm_enabled ())) +return ; + +__vgic_v5_vdpend (intid, pending, vm); +} +typedef void (*hcall_t )(struct kvm_cpu_context *);#define HANDLE_FUNC (x) [__KVM_HOST_SMCCC_FUNC_##x] = (hcall_t)handle_##x
@@ -760,6 +772,7 @@ static const hcall_t host_hcall[] = {HANDLE_FUNC (__vgic_v3_restore_vmcr_aprs),HANDLE_FUNC (__vgic_v5_make_resident),HANDLE_FUNC (__vgic_v5_make_non_resident), +HANDLE_FUNC (__vgic_v5_vdpend),HANDLE_FUNC (__vgic_v5_save_apr), HANDLE_FUNC(__vgic_v5_restore_vmcr_apr),
arch/arm64/kvm/hyp/vgic-v5-sr.c +10 -0
@@ -149,3 +149,13 @@ void __vgic_v5_restore_state(struct vgic_v5_cpu_if *cpu_if) {write_sysreg_s (cpu_if->vgic_icsr,SYS_ICC_ICSR_EL1 ); } + +void __vgic_v5_vdpend (u32 intid,bool pending,u16 vm) +{ +u64 value; + + value = intid & (GICV5_GIC_VDPEND_ID_MASK |GICV5_GIC_VDPEND_TYPE_MASK ); + value |=FIELD_PREP (GICV5_GIC_VDPEND_PENDING_MASK , pending); + value |=FIELD_PREP (GICV5_GIC_VDPEND_VM_MASK , vm); +gic_insn (value,VDPEND ); +}
Signature · 1 line
2.34.1
PATCH v5 26/49 KVM: arm64: gic: Introduce set_pending_state() to irq_ops
In reply to Sascha BischoffThere are times, such as with GICv5 SPIs and LPIs, where the hardware itself manages parts of the interrupt lifecycle. This means that pending state can be directly communicated to the hardware instead of being represented only in the VGIC shadow state.
In order to accommodate cases where the hardware handles pending state directly, add a new set_pending_state() function pointer to irq_ops. The intent is for this to be used after the VGIC shadow pending state has changed, allowing the backend to mirror the updated state into hardware.
This new function is plumbed into kvm_vgic_inject_irq(), and is only called if irq_ops are provided and this function pointer is explicitly set. In the general case, this has no effect.
--- arch/arm64/kvm/vgic/vgic.c | 3 +++ include/kvm/arm_vgic.h | 6 ++++++ 2 files changed, 9 insertions(+)
arch/arm64/kvm/vgic/vgic.c +3 -0
@@ -562,6 +562,9 @@ int kvm_vgic_inject_irq(struct kvm *kvm, struct kvm_vcpu *vcpu,else irq->pending_latch = true; +if (irq->ops && irq->ops->set_pending_state) +WARN_ON_ONCE (!irq->ops->set_pending_state (vcpu, irq)); +vgic_queue_irq_unlock (kvm, irq, flags);vgic_put_irq (kvm, irq);
include/kvm/arm_vgic.h +6 -0
@@ -246,6 +246,12 @@ struct irq_ops {*/ bool (*get_input_level)(int vintid); +/* +* Function pointer to directly update hardware pending state after the +* VGIC shadow pending state has changed. +*/ +bool (*set_pending_state)(struct kvm_vcpu *vcpu,struct vgic_irq *irq); +/* * Function pointer to override the queuing of an IRQ. */
Signature · 1 line
2.34.1
[email protected]
AI reviewRe: [PATCH v5 26/49] KVM: arm64: gic: Introduce set_pending_state() to irq_ops
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] The `set_pending_state` callback is only invoked in `kvm_vgic_inject_irq()`, leaving hardware…
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] The set_pending_state callback is only invoked in kvm_vgic_inject_irq(), leaving hardware state out of sync when VGIC shadow state is modified by other paths. --
--- Patch [26]: [PATCH v5 26/49] KVM: arm64: gic: Introduce set_pending_state() to irq_ops --- commit 706acf491d97981f9dd498ec21c341cba46f1f47
Author: Sascha Bischoff <[email protected]>
Subject: KVM: arm64: gic: Introduce set_pending_state() to irq_ops
This commit introduces a set_pending_state() function pointer to irq_ops. This allows hardware-managed interrupts to mirror KVM's VGIC shadow pending state into the hardware when the state changes.
arch/arm64/kvm/vgic/vgic.c int kvm_vgic_inject_irq(struct kvm *kvm, struct kvm_vcpu *vcpu, · line 568
Quoted 11 lines
arch/arm64/kvm/vgic/vgic.c +3
-0@@ -563,6 +563,9 @@ int kvm_vgic_inject_irq(struct kvm *kvm, struct kvm_vcpu *vcpu,else irq->pending_latch = true; +if (irq->ops && irq->ops->set_pending_state) +WARN_ON_ONCE (!irq->ops->set_pending_state (vcpu, irq)); +
[Severity: High]
Since the set_pending_state callback is only invoked here in kvm_vgic_inject_irq(), will the hardware state become out of sync when the VGIC shadow state is modified by other paths?
For example, if a guest accesses the GICD_ICPENDR register, KVM updates irq->pending_latch in __clear_pending(), but does not appear to invoke the hardware sync callback:
arch/arm64/kvm/vgic/vgic-mmio.c:__clear_pending() {
if (irq->hw && !is_user)
vgic_hw_irq_cpending(vcpu, irq);
else
irq->pending_latch = false;
}A similar desync seems possible in __set_pending() when handling GICD_ISPENDR writes, as well as in vgic_write_irq_line_level_info() and kvm_vgic_reset_mapped_irq().
Could this lead to stuck interrupts or spurious injections for hardware-managed interrupts if the hardware remains pending after the shadow state is cleared?
Quoted 2 lines
arch/arm64/kvm/vgic/vgic.c +0
-0vgic_queue_irq_unlock (kvm, irq, flags);vgic_put_irq (kvm, irq);
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=26
Re: [PATCH v5 26/49] KVM: arm64: gic: Introduce set_pending_state() to irq_ops
Quoted 31 lines · On Fri, 2026-08-07 at 15:14 +0000, [email protected] wrote:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] Theset_pending_statecallback is only invoked inkvm_vgic_inject_irq(), leaving hardware state out of sync when VGIC shadow state is modified by other paths.
----- Patch [26]: [PATCH v5 26/49] KVM: arm64: gic: Introduce
set_pending_state()toirq_ops---
commit706acf491d97981f9dd498ec21c341cba46f1f47
Author: Sascha Bischoff <[email protected]>
Subject: KVM: arm64: gic: Introduceset_pending_state()toirq_opsThis commit introduces a
set_pending_state()function pointer toirq_ops.
This allows hardware-managed interrupts to mirror KVM's VGIC shadow pending
state into the hardware when the state changes.Quoted 12 lines
arch/arm64/kvm/vgic/vgic.c +0
-0@@ -563,6 +563,9 @@ int kvm_vgic_inject_irq(struct kvm *kvm, struct kvm_vcpu *vcpu,else
irq->pending_latch= true;arch/arm64/kvm/vgic/vgic.c +2
-0+if (irq->ops && irq->ops->set_pending_state) + WARN_ON_ONCE(!irq->ops->set_pending_state(vcpu,
Quoted 8 lines
Quoted 2 lines
irq));
+[Severity: High]
Since theset_pending_statecallback is only invoked here inkvm_vgic_inject_irq(), will the hardware state become out of sync when the
VGIC shadow state is modified by other paths?
We treat SPIs as "inject and forget". The hardware manages their state, and determines when they should be presented to the guest. All we do is to give hardware the most recent pending state. Therefore, we don't need to do anything in the other paths.
Quoted 14 lines
For example, if a guest accesses the
GICD_ICPENDRregister, KVM updatesirq->pending_latchin__clear_pending(), but does not appear to invoke the
hardware sync callback:arch/arm64/kvm/vgic/vgic-mmio.c:__clear_pending() { if (irq->hw && !is_user) vgic_hw_irq_cpending(vcpu, irq); else irq->pending_latch = false; }
Quoted 4 lines
A similar desync seems possible in
__set_pending()when handlingGICD_ISPENDRwrites, as well as invgic_write_irq_line_level_info()andkvm_vgic_reset_mapped_irq().
These are not present with GICv5 (as in, this is for a completely different GIC model), so this is not an issue.
Quoted 5 lines
Could this lead to stuck interrupts or spurious injections for hardware-managed interrupts if the hardware remains pending after the shadow
state is cleared?
No.
Quoted 4 lines
Quoted 3 lines
vgic_queue_irq_unlock(kvm, irq, flags);
vgic_put_irq(kvm, irq);
Thanks,
Sascha
PATCH v5 27/49 KVM: arm64: gic-v5: Support SPI injection
In reply to Sascha BischoffGICv5 SPI lifecycle is handled by the GICv5 hardware once the pending state has been injected.
This change adds support for injecting SPIs to the core VGIC code and GICv5 code. First of all, allow GICv5 SPIs to be looked up by ID via vgic_get_irq(). Previously, only PPIs were supported.
Two irq_ops are used for GICv5 SPI injection. The set_pending_state() irq_op is used to inject the SPI's pending state into the guest. The queue_irq_unlock() irq_op releases the IRQ lock without queueing the SPI on a per-vCPU AP list, as GICv5 does not track in-flight SPI state in KVM. Tracking the affinity and lifecycle of individual SPIs in software would negate much of the benefit of their lifecycle being hardware managed.
Limit injection to the configured per-VM SPI range. Since GICv5 SPI IDs start at zero, KVM_IRQ_LINE accepts IDs from zero through one less than the configured number of SPIs, up to 1023. Update the KVM_IRQ_LINE documentation accordingly.
--- Documentation/virt/kvm/api.rst | 3 ++- arch/arm64/kvm/vgic/vgic-irs-v5.c | 1 + arch/arm64/kvm/vgic/vgic-v5.c | 41 +++++++++++++++++++++++++++++++ arch/arm64/kvm/vgic/vgic.c | 28 +++++++++++++++------ arch/arm64/kvm/vgic/vgic.h | 2 ++ 5 files changed, 66 insertions(+), 9 deletions(-)
Documentation/virt/kvm/api.rst +2 -1
@@ -909,7 +909,8 @@ The irq_type field has the following values: - KVM_ARM_IRQ_TYPE_SPI: in-kernel GICv2/GICv3: SPI, irq_id between 32 and 1019 (incl.) (the vcpu_index field is ignored) - in-kernel GICv5: SPI, irq_id between 0 and 65535 (incl.) + in-kernel GICv5: SPI, irq_id between 0 and the configured + number of SPIs minus one (1023 maximum) - KVM_ARM_IRQ_TYPE_PPI: in-kernel GICv2/GICv3: PPI, irq_id between 16 and 31 (incl.) in-kernel GICv5: PPI, irq_id between 0 and 127 (incl.)
arch/arm64/kvm/vgic/vgic-irs-v5.c +1 -0
@@ -680,6 +680,7 @@ int kvm_vgic_v5_irs_init(struct kvm *kvm, unsigned int nr_spis)* view it is always enabled. */ irq->enabled =1 ; +vgic_v5_set_spi_ops (irq); } nr_spi_bits =fls (roundup_pow_of_two (nr_spis)) -1 ;
arch/arm64/kvm/vgic/vgic-v5.c +41 -0
47 lines not on this page · raw message
arch/arm64/kvm/vgic/vgic.c +20 -8
@@ -86,19 +86,31 @@ static struct vgic_irq *vgic_get_lpi(struct kvm *kvm, u32 intid)*/ struct vgic_irq *vgic_get_irq (struct kvm *kvm,u32 intid) { -/* Non-private IRQs are not yet implemented for GICv5 */ -if (vgic_is_v5 (kvm)) -return NULL ; +enum kvm_device_type type = kvm->arch.vgic.vgic_model;/* SPIs */ -if (intid >=VGIC_NR_PRIVATE_IRQS && - intid < (kvm->arch.vgic.nr_spis +VGIC_NR_PRIVATE_IRQS )) { - intid =array_index_nospec (intid, kvm->arch.vgic.nr_spis +VGIC_NR_PRIVATE_IRQS ); -return &kvm->arch.vgic.spis[intid -VGIC_NR_PRIVATE_IRQS ]; +if (__irq_is_spi (type, intid)) { +switch (type) { +case KVM_DEV_TYPE_ARM_VGIC_V5 : + intid =vgic_v5_get_hwirq_id (intid); + +if (intid >= kvm->arch.vgic.nr_spis) +return NULL ; + + intid =array_index_nospec (intid, kvm->arch.vgic.nr_spis); +return &kvm->arch.vgic.spis[intid]; +default : { +u32 max_intid = kvm->arch.vgic.nr_spis +VGIC_NR_PRIVATE_IRQS ; + +if (intid < max_intid) { + intid =array_index_nospec (intid, max_intid); +return &kvm->arch.vgic.spis[intid -VGIC_NR_PRIVATE_IRQS ]; + } + }} }/* LPIs */ -if (irq_is_lpi (kvm, intid)) +if (__irq_is_lpi (type, intid))return vgic_get_lpi (kvm, intid);return NULL ;
arch/arm64/kvm/vgic/vgic.h +2 -0
@@ -370,6 +370,8 @@ int kvm_vgic_v5_irs_init(struct kvm *kvm, unsigned int nr_spis);void vgic_v5_teardown (struct kvm *kvm);int vgic_v5_map_resources (struct kvm *kvm);void vgic_v5_set_ppi_ops (struct kvm_vcpu *vcpu,u32 vintid); +void vgic_v5_set_spi_ops (struct vgic_irq *irq); +void vgic_v5_set_irq_pend (struct kvm_vcpu *vcpu,struct vgic_irq *irq);bool vgic_v5_has_pending_ppi (struct kvm_vcpu *vcpu);void vgic_v5_flush_ppi_state (struct kvm_vcpu *vcpu);void vgic_v5_fold_ppi_state (struct kvm_vcpu *vcpu);
Signature · 1 line
2.34.1
[email protected]
AI reviewRe: [PATCH v5 27/49] KVM: arm64: gic-v5: Support SPI injection
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
Pre-existing issues:
- [High] Spectre v1 gadget in vgic_get_irq for GICv2/GICv3 SPIs due to improper use of array_index_nospec. --
--- Patch [27]: [PATCH v5 27/49] KVM: arm64: gic-v5: Support SPI injection --- commit 33e91ef5257fc1901a945d6c3b84eddc64cc9e09
Author: Sascha Bischoff <[email protected]>
KVM: arm64: gic-v5: Support SPI injection
This commit adds support for injecting GICv5 SPIs to the core VGIC code. It updates vgic_get_irq() to allow GICv5 SPI lookups by ID and introduces specific irq_ops for handling their pending state without AP list queueing.
Quoted 4 lines
arch/arm64/kvm/vgic/vgic.c +0
-0
[ ... ]
Quoted 16 lines
arch/arm64/kvm/vgic/vgic.c +3
-7@@ -86,19 +86,31 @@ static struct vgic_irq *vgic_get_lpi(struct kvm *kvm, u32 intid)*/ struct vgic_irq *vgic_get_irq (struct kvm *kvm,u32 intid) { -/* Non-private IRQs are not yet implemented for GICv5 */ -if (vgic_is_v5 (kvm)) -return NULL ; +enum kvm_device_type type = kvm->arch.vgic.vgic_model;/* SPIs */ -if (intid >=VGIC_NR_PRIVATE_IRQS && - intid < (kvm->arch.vgic.nr_spis +VGIC_NR_PRIVATE_IRQS )) { - intid =array_index_nospec (intid, kvm->arch.vgic.nr_spis +VGIC_NR_PRIVATE_IRQS ); -return &kvm->arch.vgic.spis[intid -VGIC_NR_PRIVATE_IRQS ]; +if (__irq_is_spi (type, intid)) { +switch (type) {
[ ... ]
arch/arm64/kvm/vgic/vgic.c static struct vgic_irq *vgic_get_lpi(struct kvm *kvm, u32 intid) · line 107
Quoted 6 lines
arch/arm64/kvm/vgic/vgic.c +6
-0+default : { +u32 max_intid = kvm->arch.vgic.nr_spis +VGIC_NR_PRIVATE_IRQS ; + +if (intid < max_intid) { + intid =array_index_nospec (intid, max_intid); +return &kvm->arch.vgic.spis[intid -VGIC_NR_PRIVATE_IRQS ];
[Severity: High]
This is a pre-existing issue, but does this array_index_nospec() usage leave the kernel open to speculative out-of-bounds reads?
Since array_index_nospec() only bounds the upper limit, if a guest or userspace ioctl triggers this path speculatively with an intid less than VGIC_NR_PRIVATE_IRQS (e.g., 0 to 31), the value is passed through unchanged.
When the code then subtracts VGIC_NR_PRIVATE_IRQS, it underflows to a negative index, allowing speculative access to the memory immediately preceding the spis array. Additionally, if the CPU speculatively bypasses the bounds check with a large intid, array_index_nospec() clamps it to 0, which again underflows when the subtraction occurs.
Should the array_index_nospec() instead be applied to the computed array index after the subtraction?
Quoted 5 lines
arch/arm64/kvm/vgic/vgic.c +2
-0+ } + }} }/* LPIs */
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=27
Re: [PATCH v5 27/49] KVM: arm64: gic-v5: Support SPI injection
Quoted 82 lines · On Fri, 2026-08-07 at 15:23 +0000, [email protected] wrote:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
Pre-existing issues:
- [High] Spectre v1 gadget invgic_get_irqfor GICv2/GICv3 SPIs due to improper use ofarray_index_nospec.
----- Patch [27]: [PATCH v5 27/49] KVM: arm64: gic-v5: Support SPI injection ---
commit33e91ef5257fc1901a945d6c3b84eddc64cc9e09
Author: Sascha Bischoff <[email protected]>KVM: arm64: gic-v5: Support SPI injection
This commit adds support for injecting GICv5 SPIs to the core VGIC code.
It updatesvgic_get_irq()to allow GICv5 SPI lookups by ID and introduces
specificirq_opsfor handling their pending state without AP list queueing.Quoted 5 lines
arch/arm64/kvm/vgic/vgic.c +0
-0[ ... ]
Quoted 20 lines
arch/arm64/kvm/vgic/vgic.c +1
-3@@ -86,19 +86,31 @@ static struct vgic_irq *vgic_get_lpi(struct kvm*kvm, u32 intid) */ struct vgic_irq *vgic_get_irq(struct kvm *kvm, u32 intid) { -/* Non-private IRQs are not yet implemented for GICv5 */ -if (vgic_is_v5 (kvm)) -return NULL ; +enum kvm_device_type type = kvm->arch.vgic.vgic_model;
/* SPIs */arch/arm64/kvm/vgic/vgic.c +0
-2-if (intid >=VGIC_NR_PRIVATE_IRQS && - intid < (kvm->arch.vgic.nr_spis +
VGIC_NR_PRIVATE_IRQS)) {arch/arm64/kvm/vgic/vgic.c +0
-1- intid = array_index_nospec(intid, kvm-Quoted 1 line
arch.vgic.nr_spis +
VGIC_NR_PRIVATE_IRQS);arch/arm64/kvm/vgic/vgic.c +0
-1- return &kvm->arch.vgic.spis[intid -
VGIC_NR_PRIVATE_IRQS];arch/arm64/kvm/vgic/vgic.c +2
-0+if (__irq_is_spi (type, intid)) { +switch (type) {[ ... ]
arch/arm64/kvm/vgic/vgic.c
Quoted 9 lines
arch/arm64/kvm/vgic/vgic.c +2
-0+default : { + u32 max_intid = kvm->arch.vgic.nr_spis +
VGIC_NR_PRIVATE_IRQS;arch/arm64/kvm/vgic/vgic.c +3
-0+ +if (intid < max_intid) { + intid = array_index_nospec(intid,
max_intid);arch/arm64/kvm/vgic/vgic.c +1
-1+ return &kvm->arch.vgic.spis[intid - VGIC_NR_PRIVATE_IRQS];[Severity: High]
This is a pre-existing issue, but does thisarray_index_nospec()usage leave
the kernel open to speculative out-of-bounds reads?Since
array_index_nospec()only bounds the upper limit, if a guest or userspace ioctl triggers this path speculatively with an intid less thanVGIC_NR_PRIVATE_IRQS(e.g., 0 to 31), the value is passed through unchanged.When the code then subtracts
VGIC_NR_PRIVATE_IRQS, it underflows to a negative index, allowing speculative access to the memory immediately preceding the spis array. Additionally, if the CPU speculatively bypasses
the bounds check with a large intid,array_index_nospec()clamps it to 0,
which again underflows when the subtraction occurs.
Quoted 3 lines
Should the
array_index_nospec()instead be applied to the computed array
index after the subtraction?
This has been address in the series here:
https://lore.kernel.org/all/[email protected]/
The corresponding change has been worked into this commit too, i.e., we preserve it for the non-GICv5 path.
Quoted 7 lines
Quoted 6 lines
arch/arm64/kvm/vgic/vgic.c +2
-0+ } + }}}
/* LPIs */
Thanks,
Sascha
PATCH v5 28/49 Documentation: KVM: Extend VGICv5 device attribute docs
In reply to Sascha BischoffThe VGICv5 documentation still describes the device as supporting PPIs only. Update the overview to cover PPI, SPI, and LPI interrupts. PPIs and SPIs can be injected by in-kernel devices or through KVM_IRQ_LINE, while LPI pending state is driven by the guest and handled in hardware through the LPI IST.
Document the GICv5 IRS address attribute and its GPA layout requirements. The IRS region must be 64K aligned and covers a 128K range.
Also document the GICv5-specific interpretation of KVM_DEV_ARM_VGIC_GRP_NR_IRQS. Unlike GICv2 and GICv3, the value describes only the number of SPIs exposed by the IRS and does not include SGIs or PPIs. The supported range is 32 through 1024 SPIs in multiples of 32, and defaults to 32 when userspace leaves it unset.
--- .../virt/kvm/devices/arm-vgic-v5.rst | 35 ++++++++++++++++--- 1 file changed, 31 insertions(+), 4 deletions(-)
Documentation/virt/kvm/devices/arm-vgic-v5.rst +31 -4
@@ -12,12 +12,39 @@ Only one VGIC instance may be instantiated through this API. The created VGIC will act as the VM interrupt controller, requiring emulated user-space devices to inject interrupts to the VGIC instead of directly to CPUs. -Creating a guest GICv5 device requires a GICv5 host. The current VGICv5 device -only supports PPI interrupts. These can either be injected from emulated -in-kernel devices (such as the Arch Timer, or PMU), or via the KVM_IRQ_LINE -ioctl. +Creating a guest GICv5 device requires a GICv5 host. The VGICv5 device supports +PPI, SPI, and LPI interrupts. The PPI and SPI interrupts can either be injected +from emulated in-kernel devices (such as the Arch Timer, or PMU), or via the +KVM_IRQ_LINE ioctl. LPIs are not externally injected, but are handled in +hardware via the LPI IST. Their pending state is driven directly by the guest. Groups: + KVM_DEV_ARM_VGIC_GRP_ADDR + Attributes: + + KVM_VGIC_V5_ADDR_TYPE_IRS (rw, 64-bit) + Base address in the guest physical address space of the GICv5 IRS + (Interrupt Routing Service) register mappings. Only valid for + KVM_DEV_TYPE_ARM_VGIC_V5. This address needs to be 64K aligned and the + region covers 128 KByte - the IRS has a CONFIG_FRAME and a SETLPI_FRAME, + each of which is 64 KBytes in size. + + Setting the address of the IRS in GPA space is mandatory before VGIC + resources are mapped, as the IRS is responsible for handling SPIs and + LPIs. Failure to set the IRS address before the first vCPU run results in + an error. + + KVM_DEV_ARM_VGIC_GRP_NR_IRQS + Attributes: + + A value describing the number of SPIs for this GIC instance. This is + GICv5-specific: unlike GICv2/v3, the value does not include SGIs or PPIs. + The value ranges from 32 to KVM's VGICv5 maximum of 1024 SPIs, in + increments of 32. If userspace does not set this attribute, KVM uses 32 + SPIs by default. + + kvm_device_attr.addr points to a __u32 value. + KVM_DEV_ARM_VGIC_GRP_CTRL Attributes:
Signature · 1 line
2.34.1
[email protected]
AI reviewRe: [PATCH v5 28/49] Documentation: KVM: Extend VGICv5 device attribute docs
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] Reusing KVM_DEV_ARM_VGIC_GRP_NR_IRQS with conflicting semantics for VGICv5 creates a UAPI…
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] Reusing KVM_DEV_ARM_VGIC_GRP_NR_IRQS with conflicting semantics for VGICv5 creates a UAPI inconsistency that breaks generic VMM initialization. --
--- Patch [28]: [PATCH v5 28/49] Documentation: KVM: Extend VGICv5 device attribute docs --- commit b29b54b5c700b9d3c2f478e0ca8a49cc3cef29a7
Author: Sascha Bischoff <[email protected]>
Documentation: KVM: Extend VGICv5 device attribute docs
This commit updates the VGICv5 documentation to cover PPI, SPI, and LPI interrupts. It documents the GICv5 IRS address attribute and its GPA layout, as well as a GICv5-specific interpretation of the KVM_DEV_ARM_VGIC_GRP_NR_IRQS attribute.
Quoted 4 lines
Documentation/virt/kvm/devices/arm-vgic-v5.rst +0
-0
[ ... ]
Documentation/virt/kvm/devices/arm-vgic-v5.rst Only one VGIC instance may be instantiated through this API. The created VGIC · line 41
Quoted 5 lines
Documentation/virt/kvm/devices/arm-vgic-v5.rst +5
-0+ KVM_DEV_ARM_VGIC_GRP_NR_IRQS + Attributes: + + A value describing the number of SPIs for this GIC instance. This is + GICv5-specific: unlike GICv2/v3, the value does not include SGIs or PPIs.
[Severity: Medium]
Does reusing the KVM_DEV_ARM_VGIC_GRP_NR_IRQS attribute with conflicting semantics for VGICv5 create a UAPI inconsistency that breaks generic VMM initialization?
VMMs that use shared, generic GIC initialization code will likely pass num_spis + 32 (the standard for GICv2/v3) to this attribute. Will this cause them to incorrectly provision the number of IRQs for VGICv5, either allocating an unintended number of SPIs or failing initialization entirely if the value exceeds the GICv5 SPI limit?
Quoted 5 lines
Documentation/virt/kvm/devices/arm-vgic-v5.rst +5
-0+ The value ranges from 32 to KVM's VGICv5 maximum of 1024 SPIs, in + increments of 32. If userspace does not set this attribute, KVM uses 32 + SPIs by default. + + kvm_device_attr.addr points to a __u32 value.
[ ... ]
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=28
Re: [PATCH v5 28/49] Documentation: KVM: Extend VGICv5 device attribute docs
Quoted 38 lines · On Fri, 2026-08-07 at 15:29 +0000, [email protected] wrote:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] ReusingKVM_DEV_ARM_VGIC_GRP_NR_IRQSwith conflicting semantics for VGICv5 creates a UAPI inconsistency that breaks generic VMM initialization.
----- Patch [28]: [PATCH v5 28/49] Documentation: KVM: Extend VGICv5 device attribute docs ---
commitb29b54b5c700b9d3c2f478e0ca8a49cc3cef29a7
Author: Sascha Bischoff <[email protected]>Documentation: KVM: Extend VGICv5 device attribute docs
This commit updates the VGICv5 documentation to cover PPI, SPI, and LPI
interrupts. It documents the GICv5 IRS address attribute and its GPA layout,
as well as a GICv5-specific interpretation of theKVM_DEV_ARM_VGIC_GRP_NR_IRQS
attribute.Quoted 5 lines
Documentation/virt/kvm/devices/arm-vgic-v5.rst +0
-0[ ... ]
Quoted 7 lines
+
KVM_DEV_ARM_VGIC_GRP_NR_IRQS
+ Attributes:
+
+ A value describing the number of SPIs for this GIC instance. This is
+ GICv5-specific: unlike GICv2/v3, the value does not include SGIs or PPIs.
Quoted 6 lines
[Severity: Medium]
Does reusing theKVM_DEV_ARM_VGIC_GRP_NR_IRQSattribute with conflicting
semantics for VGICv5 create a UAPI inconsistency that breaks generic VMM
initialization?
They are different GICs with different limitations. There's no way to consolidate those two views here. GICv3 has 16 SGIs, then 16 PPIs, then a number of SPIs, all while using range-based interrupt types. GICv5 has separate "namespaces" for the different interrupt types so they are not sequential.
For non-GICv5 GICs, it makes sense to include the SGIs and PPIs in the count as they effectively shift the base index of SPIs by 32. For GICv5, that's just not a thing, and it would be strange to mandate that the 128 potential PPIs are included in this count.
Quoted 10 lines
VMMs that use shared, generic GIC initialization code will likely pass
num_spis+ 32 (the standard for GICv2/v3) to this attribute. Will this
cause them to incorrectly provision the number of IRQs for VGICv5, either
allocating an unintended number of SPIs or failing initialization entirely
if the value exceeds the GICv5 SPI limit?
That's fine. The VMM NEEDS to be aware that a different GIC is being used and that it comes with a different set of requirements. It can't just blindly treat GICv5 the same as other GICs.
Quoted 11 lines
Quoted 8 lines
+ The value ranges from 32 to KVM's VGICv5 maximum of 1024 SPIs, in
+ increments of 32. If userspace does not set this attribute, KVM uses 32
+ SPIs by default.
+
+ kvm_device_attr.addr points to a__u32value.[ ... ]
Thanks,
Sascha
PATCH v5 29/49 KVM: arm64: gic-v5: Add GICv5 SPI injection to irqfd
In reply to Sascha BischoffNow that there is support for GICv5 SPIs in KVM, update vgic_irqfd_set_irq() to translate irqchip pins into GICv5 SPI IntIDs before injecting them.
Validate IRQCHIP route pins against the GIC model. GICv2 and GICv3 only support 988 pins for SPIs 32 through 1019, while KVM supports up to 1024 GICv5 SPI pins. The common routing table is sized for the largest supported range.
--- arch/arm64/kvm/vgic/vgic-irqfd.c | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-)
arch/arm64/kvm/vgic/vgic-irqfd.c +15 -3
@@ -19,9 +19,14 @@ static int vgic_irqfd_set_irq(struct kvm_kernel_irq_routing_entry *e, struct kvm *kvm, int irq_source_id, int level, bool line_status) { - unsigned int spi_id = e->irqchip.pin + VGIC_NR_PRIVATE_IRQS; + unsigned int spi_id;int ret; +if (kvm->arch.vgic.vgic_model ==KVM_DEV_TYPE_ARM_VGIC_V5 ) + spi_id =vgic_v5_make_spi (e->irqchip.pin); +else + spi_id = e->irqchip.pin +VGIC_NR_PRIVATE_IRQS ; +if (!vgic_valid_spi (kvm, spi_id))return -EINVAL ;
@@ -45,6 +50,13 @@ int kvm_set_routing_entry(struct kvm *kvm, struct kvm_kernel_irq_routing_entry *e, const struct kvm_irq_routing_entry *ue) { +/* +* The common routing table reserves enough entries for all GICv5 SPI +* pins. KVM limits GICv5 to 1024 SPI pins, while GICv2 and GICv3 +* only support SPIs 32..1019, leaving 988 pins. +*/ +unsigned int max_irqchip_pins =vgic_is_v5 (kvm) ? +VGIC_V5_MAX_NR_SPIS :VGIC_MAX_SPI -VGIC_NR_PRIVATE_IRQS +1 ;int r = -EINVAL ;switch (ue->type) {
@@ -52,8 +64,8 @@ int kvm_set_routing_entry(struct kvm *kvm, e->set = vgic_irqfd_set_irq; e->irqchip.irqchip = ue->u.irqchip.irqchip; e->irqchip.pin = ue->u.irqchip.pin; -if ((e->irqchip.pin >=KVM_IRQCHIP_NUM_PINS ) || - (e->irqchip.irqchip >=KVM_NR_IRQCHIPS )) +if (e->irqchip.pin >= max_irqchip_pins || + e->irqchip.irqchip >=KVM_NR_IRQCHIPS ) goto out;break ;case KVM_IRQ_ROUTING_MSI :
Signature · 1 line
2.34.1
[email protected]
AI reviewRe: [PATCH v5 29/49] KVM: arm64: gic-v5: Add GICv5 SPI injection to irqfd
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] GICv5 SPI injection via irqfd breaks KVM_IRQFD_FLAG_RESAMPLE because KVM never observes the…
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] GICv5 SPI injection via irqfd breaks KVM_IRQFD_FLAG_RESAMPLE because KVM never observes the guest's EOI. --
--- Patch [29]: [PATCH v5 29/49] KVM: arm64: gic-v5: Add GICv5 SPI injection to irqfd --- commit a0b6c4d036693f8696b21a848670f40de7bc34b7
Author: Sascha Bischoff <[email protected]>
KVM: arm64: gic-v5: Add GICv5 SPI injection to irqfd
This commit updates vgic_irqfd_set_irq() to translate irqchip pins into GICv5 SPI IntIDs before injection. It also validates the IRQCHIP route pins against the GIC model, supporting up to 1024 SPI pins when using GICv5.
Quoted 5 lines
arch/arm64/kvm/vgic/vgic-irqfd.c +0
-0
arch/arm64/kvm/vgic/vgic-irqfd.c static int vgic_irqfd_set_irq(struct kvm_kernel_irq_routing_entry *e, · line 26
Quoted 9 lines
arch/arm64/kvm/vgic/vgic-irqfd.c +3
-1@@ -19,9 +19,14 @@ static int vgic_irqfd_set_irq(struct kvm_kernel_irq_routing_entry *e, struct kvm *kvm, int irq_source_id, int level, bool line_status) { - unsigned int spi_id = e->irqchip.pin + VGIC_NR_PRIVATE_IRQS; + unsigned int spi_id;int ret; +if (kvm->arch.vgic.vgic_model ==KVM_DEV_TYPE_ARM_VGIC_V5 ) + spi_id =vgic_v5_make_spi (e->irqchip.pin);
[Severity: High]
Does enabling irqfd injection for GICv5 SPIs here break the KVM_IRQFD_FLAG_RESAMPLE contract?
If userspace configures an irqfd with KVM_IRQFD_FLAG_RESAMPLE for a GICv5 SPI, KVM injects the interrupt into the hardware Virtual Domain, and the guest's EOI is handled directly by the GICv5 hardware.
Since KVM does not track the in-flight state of GICv5 SPIs via List Registers, does the EOI fail to trap to the hypervisor?
Without KVM observing the guest's EOI, it seems KVM will never call kvm_notify_acked_irq() and the resampler eventfd will never be signaled.
Could this cause passthrough devices using level-triggered interrupts mapped to GICv5 SPIs to silently hang after the first interrupt because the physical interrupt is never unmasked?
[ ... ]
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=29
Re: [PATCH v5 29/49] KVM: arm64: gic-v5: Add GICv5 SPI injection to irqfd
Quoted 50 lines · On Fri, 2026-08-07 at 15:40 +0000, [email protected] wrote:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] GICv5 SPI injection via irqfd breaksKVM_IRQFD_FLAG_RESAMPLEbecause KVM never observes the guest's EOI.
----- Patch [29]: [PATCH v5 29/49] KVM: arm64: gic-v5: Add GICv5 SPI injection to irqfd ---
commita0b6c4d036693f8696b21a848670f40de7bc34b7
Author: Sascha Bischoff <[email protected]>KVM: arm64: gic-v5: Add GICv5 SPI injection to irqfd
This commit updates
vgic_irqfd_set_irq()to translate irqchip pins into
GICv5 SPI IntIDs before injection. It also validates the IRQCHIP route pins
against the GIC model, supporting up to 1024 SPI pins when using GICv5.Quoted 15 lines
arch/arm64/kvm/vgic/vgic-irqfd.c +1
-1@@ -19,9 +19,14 @@ static int vgic_irqfd_set_irq(struct kvm_kernel_irq_routing_entry *e, struct kvm *kvm, int irq_source_id, int level, bool line_status) { - unsigned int spi_id = e->irqchip.pin +VGIC_NR_PRIVATE_IRQS ; +unsigned int spi_id; int ret;arch/arm64/kvm/vgic/vgic-irqfd.c static int vgic_irqfd_set_irq(struct kvm_kernel_irq_routing_entry *e, · line 26
Quoted 2 lines
arch/arm64/kvm/vgic/vgic-irqfd.c +2
-0+if (kvm->arch.vgic.vgic_model ==KVM_DEV_TYPE_ARM_VGIC_V5 ) + spi_id =vgic_v5_make_spi (e->irqchip.pin);[Severity: High]
Does enabling irqfd injection for GICv5 SPIs here break theKVM_IRQFD_FLAG_RESAMPLEcontract?If userspace configures an irqfd with
KVM_IRQFD_FLAG_RESAMPLEfor a GICv5 SPI, KVM injects the interrupt into the hardware Virtual Domain,
and the guest's EOI is handled directly by the GICv5 hardware.Since KVM does not track the in-flight state of GICv5 SPIs via List Registers, does the EOI fail to trap to the hypervisor?
Quoted 8 lines
Without KVM observing the guest's EOI, it seems KVM will never call
kvm_notify_acked_irq()and the resampler eventfd will never be signaled.Could this cause passthrough devices using level-triggered interrupts mapped
to GICv5 SPIs to silently hang after the first interrupt because the physical interrupt is never unmasked?
This is indeed an issue with the current patch set - there is no support for KVM_IRQFD_FLAG_RESAMPLE as we don't track the state of in- flight interrupts. The original version of this series had support, but it was discovered that this was broken. Hence, the decision was made to drop this support for the time being.
Quoted 3 lines
[ ... ]
Thanks,
Sascha
PATCH v5 30/49 KVM: arm64: gic-v5: Mask per-vCPU PPI state in vgic_v5_finalize_ppi_state()
In reply to Sascha BischoffOnly a subset of the possible PPIs are exposed to a guest when running with a vGICv5. First of all, only the architected PPIs are considered by KVM. Secondly, only a set of those is exposed to a guest: those corresponding to devices that KVM emulates, such as the timers and PMU, and the GICv5 SW_PPI.
The finalisation of exposed PPIs happens on first vCPU run, as this is the first time when the full set of exposed devices is known. At this stage a mask is calculated, and this mask is applied both to hide non-exposed PPI state from the guest and to reduce overhead when iterating over the PPIs.
While preparing userspace access to the GICv5 system registers, it became apparent that restoring the GICv5 PPI registers can result in a mismatch between the state supplied by userspace and the state KVM intends to expose. Userspace can provide Enable, Active, and Pending state for PPIs that KVM has chosen to hide from the guest.
Userspace must restore PPI state before any vCPU runs. The userspace access path added subsequently enforces this ordering. Rework vgic_v5_finalize_ppi_state() to calculate the mask of exposed PPIs and clear any state belonging to non-exposed PPIs. This ensures that only the state KVM intends to expose is visible to the guest.
--- arch/arm64/kvm/arm.c | 2 +- arch/arm64/kvm/vgic/vgic-v5.c | 71 +++++++++++++++++++++++++---------- include/kvm/arm_vgic.h | 2 +- 3 files changed, 53 insertions(+), 22 deletions(-)
arch/arm64/kvm/arm.c +1 -1
@@ -974,7 +974,7 @@ int kvm_arch_vcpu_run_pid_change(struct kvm_vcpu *vcpu)return ret; } - ret =vgic_v5_finalize_ppi_state (kvm); + ret =vgic_v5_finalize_ppi_state (vcpu);if (ret)return ret;
arch/arm64/kvm/vgic/vgic-v5.c +51 -20
95 lines not on this page · raw message
include/kvm/arm_vgic.h +1 -1
@@ -823,7 +823,7 @@ static inline u32 vgic_v5_irs_max_vpes(const struct vgic_v5_irs_caps *caps)return BIT (FIELD_GET (GICV5_IRS_IDR4_VPE_ID_BITS , caps->idr4) +1 ); } -int vgic_v5_finalize_ppi_state (struct kvm *kvm); +int vgic_v5_finalize_ppi_state (struct kvm_vcpu *vcpu);bool vgic_v5_ppi_queue_irq_unlock (struct kvm *kvm,struct vgic_irq *irq,unsigned long flags);void vgic_v5_set_ppi_dvi (struct kvm_vcpu *vcpu,struct vgic_irq *irq,bool dvi);
Signature · 1 line
2.34.1
PATCH v5 31/49 KVM: arm64: gic-v5: Add GICv5 EL1 sysreg userspace accessors
In reply to Sascha BischoffNow that KVM is able to run meaningful VMs with GICv5, it is important to be able to save and restore the GICv5 state in order to support VM migration.
Add functions to handle userspace reads and writes of the GICv5 EL1 system registers. Some architected register values are reconstructed from KVM's internal representation rather than stored as raw register state. For example, ICC_CR0_EL1.En and ICC_PCR_EL1.Priority are backed by fields in the saved virtual VMCR, while the PPI Enable, Active, and Pending registers are assembled from per-interrupt vgic_irq state. Userspace writes are translated back into the corresponding internal representation.
Wire the accessors into KVM_DEV_ARM_VGIC_GRP_CPU_SYSREGS for the GICv5 device, reusing the existing GICv3 CPU sysreg UAPI encoding. Require the VGIC to be initialised before accessing the register state.
Allow userspace to read the state after a vCPU has run, but reject writes once any vCPU has run. This ensures that userspace restores the GICv5 CPU interface state before KVM finalises it for guest execution.
--- arch/arm64/kvm/Makefile | 3 +- arch/arm64/kvm/sys_regs.c | 6 +- arch/arm64/kvm/vgic-sys-reg-v5.c | 519 ++++++++++++++++++++++++++ arch/arm64/kvm/vgic/vgic-kvm-device.c | 124 +++++- arch/arm64/kvm/vgic/vgic.h | 7 + 5 files changed, 651 insertions(+), 8 deletions(-) create mode 100644 arch/arm64/kvm/vgic-sys-reg-v5.c
arch/arm64/kvm/Makefile +2 -1
@@ -24,7 +24,8 @@ kvm-y += arm.o mmu.o mmio.o psci.o hypercalls.o pvtime.o \ vgic/vgic-mmio.o vgic/vgic-mmio-v2.o \ vgic/vgic-mmio-v3.o vgic/vgic-kvm-device.o \ vgic/vgic-its.o vgic/vgic-debug.o vgic/vgic-v3-nested.o \ - vgic/vgic-v5.o vgic/vgic-v5-tables.o vgic/vgic-irs-v5.o + vgic/vgic-v5.o vgic/vgic-v5-tables.o vgic/vgic-irs-v5.o \ + vgic-sys-reg-v5.o kvm-$(CONFIG_HW_PERF_EVENTS) += pmu-emul.o pmu.o kvm-$(CONFIG_ARM64_PTR_AUTH) += pauth.o
arch/arm64/kvm/sys_regs.c +5 -1
@@ -5827,7 +5827,7 @@ int kvm_finalize_sys_regs(struct kvm_vcpu *vcpu)int __init kvm_sys_reg_table_init (void ) { -const struct sys_reg_desc *gicv3_regs; +const struct sys_reg_desc *gicv3_regs, *gicv5_regs;bool valid = true;unsigned int i, sz;int ret =0 ;
@@ -5840,8 +5840,12 @@ int __init kvm_sys_reg_table_init(void) valid &=check_sysreg_table (cp15_64_regs,ARRAY_SIZE (cp15_64_regs), false); valid &=check_sysreg_table (sys_insn_descs,ARRAY_SIZE (sys_insn_descs), false); +/* The GICv3 system registers... */ gicv3_regs =vgic_v3_get_sysreg_table (&sz); valid &=check_sysreg_table (gicv3_regs, sz, false); +/* ...and the GICv5 system registers. */ + gicv5_regs =vgic_v5_get_sysreg_table (&sz); + valid &=check_sysreg_table (gicv5_regs, sz, false);if (!valid)return -EINVAL ;
arch/arm64/kvm/vgic-sys-reg-v5.c new file +519 -0
519 lines not on this page · raw message
arch/arm64/kvm/vgic/vgic-kvm-device.c +118 -6
160 lines not on this page · raw message
arch/arm64/kvm/vgic/vgic.h +7 -0
@@ -252,6 +252,8 @@ struct ap_list_summary {#define irqs_active_outside_lrs (s) \ ((s)->nr_act && irqs_outside_lrs(s)) +int vgic_v5_parse_attr (struct kvm_device *dev,struct kvm_device_attr *attr, +struct vgic_reg_attr *reg_attr);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,
@@ -383,6 +385,11 @@ void vgic_v5_restore_state(struct kvm_vcpu *vcpu);void vgic_v5_save_state (struct kvm_vcpu *vcpu);int vgic_v5_register_irs_iodev (struct kvm *kvm,gpa_t irs_base_address); +int vgic_v5_cpu_sysregs_uaccess (struct kvm_vcpu *vcpu, +struct kvm_device_attr *attr,bool is_write); +int vgic_v5_has_cpu_sysregs_attr (struct kvm_vcpu *vcpu,struct kvm_device_attr *attr); +const struct sys_reg_desc *vgic_v5_get_sysreg_table (unsigned int *sz); +#define for_each_visible_v5_ppi (__i, __k) \ for_each_set_bit(__i, (__k)->arch.vgic.gicv5_vm.vgic_ppi_mask, VGIC_V5_NR_PRIVATE_IRQS)
Signature · 1 line
2.34.1
[email protected]
AI reviewRe: [PATCH v5 31/49] KVM: arm64: gic-v5: Add GICv5 EL1 sysreg userspace accessors
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] The `gic_v5_icc_reg_descs` array is not sorted by sysreg encoding, which will cause KVM module…
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] The gic_v5_icc_reg_descs array is not sorted by sysreg encoding, which will cause KVM module initialization to fail. --
--- Patch [31]: [PATCH v5 31/49] KVM: arm64: gic-v5: Add GICv5 EL1 sysreg userspace accessors --- commit fc4bd92f943a451cf5400e9e01e75819d02ae623
Author: Sascha Bischoff <[email protected]>
KVM: arm64: gic-v5: Add GICv5 EL1 sysreg userspace accessors
This commit adds functions to handle userspace reads and writes of the GICv5 EL1 system registers to support VM migration. It wires these accessors into KVM_DEV_ARM_VGIC_GRP_CPU_SYSREGS and enforces that register state can only be written before a vCPU has run.
Quoted 5 lines
arch/arm64/kvm/vgic-sys-reg-v5.c new file +0
-0
[ ... ]
Quoted 7 lines
arch/arm64/kvm/vgic-sys-reg-v5.c new file +7
-0+static const struct sys_reg_desc gic_v5_icc_reg_descs[] = { + {SYS_DESC (SYS_ICC_ICSR_EL1 ), + .set_user = set_gic_icsr, .get_user = get_gic_icsr, }, + {SYS_DESC (SYS_ICC_PPI_ENABLER0_EL1 ), + .set_user = set_gic_ppi_enabler, .get_user = get_gic_ppi_enabler, }, + {SYS_DESC (SYS_ICC_PPI_ENABLER1_EL1 ), + .set_user = set_gic_ppi_enabler, .get_user = get_gic_ppi_enabler, },
[ ... ]
arch/arm64/kvm/vgic-sys-reg-v5.c line 474
Quoted 11 lines
arch/arm64/kvm/vgic-sys-reg-v5.c new file +11
-0+ { SYS_DESC(SYS_ICC_PPI_PRIORITYR14_EL1), + .set_user = set_gic_ppi_priorityr, .get_user = get_gic_ppi_priorityr, }, + { SYS_DESC(SYS_ICC_PPI_PRIORITYR15_EL1), + .set_user = set_gic_ppi_priorityr, .get_user = get_gic_ppi_priorityr, }, + { SYS_DESC(SYS_ICC_APR_EL1), + .set_user = set_gic_apr, .get_user = get_gic_apr, }, + { SYS_DESC(SYS_ICC_CR0_EL1), + .set_user = set_gic_cr0, .get_user = get_gic_cr0, }, + { SYS_DESC(SYS_ICC_PCR_EL1), + .set_user = set_gic_pcr, .get_user = get_gic_pcr, }, +};
[Severity: High]
Will this cause KVM module initialization to fail?
During load, check_sysreg_table() verifies that the sys_reg_desc arrays are strictly sorted by their sysreg encoding (Op0, Op1, CRn, CRm, Op2). If they are out of order, check_sysreg_table() returns false and causes the init to fail with -EINVAL.
Here, SYS_ICC_APR_EL1, SYS_ICC_CR0_EL1, and SYS_ICC_PCR_EL1 (which all have CRm=0) are placed at the end of the array after SYS_ICC_PPI_PRIORITYR15_EL1 (which has CRm=9). Additionally, SYS_ICC_ICSR_EL1 is placed at the very beginning of the array.
Does this array need to be reordered strictly by encoding so the validation checks pass and the binary searches work correctly?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=31
Re: [PATCH v5 31/49] KVM: arm64: gic-v5: Add GICv5 EL1 sysreg userspace accessors
Quoted 75 lines · On Fri, 2026-08-07 at 16:27 +0000, [email protected] wrote:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] Thegic_v5_icc_reg_descsarray is not sorted by sysreg encoding, which will cause KVM module initialization to fail. ----- Patch [31]: [PATCH v5 31/49] KVM: arm64: gic-v5: Add GICv5 EL1 sysreg userspace accessors ---
commitfc4bd92f943a451cf5400e9e01e75819d02ae623
Author: Sascha Bischoff <[email protected]>KVM: arm64: gic-v5: Add GICv5 EL1 sysreg userspace accessors
This commit adds functions to handle userspace reads and writes of the GICv5
EL1 system registers to support VM migration. It wires these accessors intoKVM_DEV_ARM_VGIC_GRP_CPU_SYSREGSand enforces that register state can only
be written before a vCPU has run.Quoted 6 lines
arch/arm64/kvm/vgic-sys-reg-v5.c new file +0
-0[ ... ]
Quoted 9 lines
arch/arm64/kvm/vgic-sys-reg-v5.c new file +5
-0+static const struct sys_reg_desc gic_v5_icc_reg_descs[] = { + {SYS_DESC (SYS_ICC_ICSR_EL1 ), + .set_user = set_gic_icsr, .get_user = get_gic_icsr, }, + {SYS_DESC (SYS_ICC_PPI_ENABLER0_EL1 ), + .set_user = set_gic_ppi_enabler, .get_user =
get_gic_ppi_enabler, },arch/arm64/kvm/vgic-sys-reg-v5.c new file +2
-0+ {SYS_DESC (SYS_ICC_PPI_ENABLER1_EL1 ), + .set_user = set_gic_ppi_enabler, .get_user =
get_gic_ppi_enabler, },[ ... ]
arch/arm64/kvm/vgic-sys-reg-v5.c line 474
Quoted 13 lines
arch/arm64/kvm/vgic-sys-reg-v5.c new file +2
-0+ {SYS_DESC (SYS_ICC_PPI_PRIORITYR14_EL1 ), + .set_user = set_gic_ppi_priorityr, .get_user =
get_gic_ppi_priorityr, },arch/arm64/kvm/vgic-sys-reg-v5.c new file +2
-0+ {SYS_DESC (SYS_ICC_PPI_PRIORITYR15_EL1 ), + .set_user = set_gic_ppi_priorityr, .get_user =
get_gic_ppi_priorityr, },arch/arm64/kvm/vgic-sys-reg-v5.c new file +7
-0+ {SYS_DESC (SYS_ICC_APR_EL1 ), + .set_user = set_gic_apr, .get_user = get_gic_apr, }, + { SYS_DESC(SYS_ICC_CR0_EL1), + .set_user = set_gic_cr0, .get_user = get_gic_cr0, }, + { SYS_DESC(SYS_ICC_PCR_EL1), + .set_user = set_gic_pcr, .get_user = get_gic_pcr, }, +};[Severity: High]
Will this cause KVM module initialization to fail?During load,
check_sysreg_table()verifies that thesys_reg_descarrays are
strictly sorted by their sysreg encoding (Op0, Op1, CRn, CRm, Op2). If they
are out of order,check_sysreg_table()returns false and causes the init to
fail with-EINVAL.Here,
SYS_ICC_APR_EL1,SYS_ICC_CR0_EL1, andSYS_ICC_PCR_EL1(which all have
CRm=0) are placed at the end of the array afterSYS_ICC_PPI_PRIORITYR15_EL1
(which has CRm=9). Additionally,SYS_ICC_ICSR_EL1is placed at the very
beginning of the array.
Quoted 4 lines
Does this array need to be reordered strictly by encoding so the validation
checks pass and the binary searches work correctly?
I'm really not sure where you're getting this from. They are in the correct order as far as I can tell (I made the point of doing that when I added this code because I knew that it had to be in order). I've re- checked them against their definitions, and they look good.
Also, we don't fail the init which strongly suggests that the ordering must be correct. As in, I've built and run this code and don't hit the init failure you refer to.
In summary: this is fine as it is; won't fix as there is nothing to fix.
Thanks,
Sascha
PATCH v5 32/49 KVM: arm64: gic-v5: Handle userspace accesses to IRS MMIO region
In reply to Sascha BischoffAs part of saving and restoring the state of a GICv5-based system, userspace must save and restore the IRS MMIO registers. These include important information such as the guest IST configuration, and KVM must present consistent state to the guest after migration.
Introduce KVM_DEV_ARM_VGIC_GRP_IRS_REGS and provide accessors to read and write the virtual IRS register state. This is modelled on the GICv3 ITS register interface, as the migration requirements are broadly the same.
Reuse the guest MMIO handlers where userspace and guest accesses have the same semantics. Add userspace-specific handling where restoring a register image must not trigger the operation associated with a guest MMIO write.
Validate restored ID register fields against the capabilities KVM and the host can support. Restore the emulated IST configuration without allocating or freeing a host IST, and report operation status registers as idle. Accept writes to IRS_SPI_CFGR, IRS_IIDR, and IRS_AIDR without changing their state, allowing userspace to replay the values it previously read.
--- arch/arm64/include/uapi/asm/kvm.h | 1 + arch/arm64/kvm/vgic/vgic-irs-v5.c | 447 ++++++++++++++++++++---- arch/arm64/kvm/vgic/vgic-kvm-device.c | 55 ++- arch/arm64/kvm/vgic/vgic.h | 4 + tools/arch/arm64/include/uapi/asm/kvm.h | 1 + 5 files changed, 423 insertions(+), 85 deletions(-)
arch/arm64/include/uapi/asm/kvm.h +1 -0
@@ -422,6 +422,7 @@ enum {#define KVM_DEV_ARM_VGIC_GRP_LEVEL_INFO 7#define KVM_DEV_ARM_VGIC_GRP_ITS_REGS 8#define KVM_DEV_ARM_VGIC_GRP_MAINT_IRQ 9 +#define KVM_DEV_ARM_VGIC_GRP_IRS_REGS 10#define KVM_DEV_ARM_VGIC_LINE_LEVEL_INFO_SHIFT 10#define KVM_DEV_ARM_VGIC_LINE_LEVEL_INFO_MASK \ (0x3fffffULL << KVM_DEV_ARM_VGIC_LINE_LEVEL_INFO_SHIFT)
arch/arm64/kvm/vgic/vgic-irs-v5.c +372 -75
507 lines not on this page · raw message
arch/arm64/kvm/vgic/vgic-kvm-device.c +45 -10
105 lines not on this page · raw message
arch/arm64/kvm/vgic/vgic.h +4 -0
@@ -389,6 +389,10 @@ int vgic_v5_cpu_sysregs_uaccess(struct kvm_vcpu *vcpu, struct kvm_device_attr *attr, bool is_write);int vgic_v5_has_cpu_sysregs_attr (struct kvm_vcpu *vcpu,struct kvm_device_attr *attr);const struct sys_reg_desc *vgic_v5_get_sysreg_table (unsigned int *sz); +int vgic_v5_irs_attr_regs_access (struct kvm_device *dev, +struct kvm_device_attr *attr, +u64 *reg,bool is_write); +int vgic_v5_has_attr_regs (struct kvm_device *dev,struct kvm_device_attr *attr);#define for_each_visible_v5_ppi (__i, __k) \ for_each_set_bit(__i, (__k)->arch.vgic.gicv5_vm.vgic_ppi_mask, VGIC_V5_NR_PRIVATE_IRQS)
tools/arch/arm64/include/uapi/asm/kvm.h +1 -0
@@ -422,6 +422,7 @@ enum {#define KVM_DEV_ARM_VGIC_GRP_LEVEL_INFO 7#define KVM_DEV_ARM_VGIC_GRP_ITS_REGS 8#define KVM_DEV_ARM_VGIC_GRP_MAINT_IRQ 9 +#define KVM_DEV_ARM_VGIC_GRP_IRS_REGS 10#define KVM_DEV_ARM_VGIC_LINE_LEVEL_INFO_SHIFT 10#define KVM_DEV_ARM_VGIC_LINE_LEVEL_INFO_MASK \ (0x3fffffULL << KVM_DEV_ARM_VGIC_LINE_LEVEL_INFO_SHIFT)
Signature · 1 line
2.34.1
[email protected]
AI reviewRe: [PATCH v5 32/49] KVM: arm64: gic-v5: Handle userspace accesses to IRS MMIO region
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] Userspace can inject an inconsistent state by setting IRS_IST_BASER.valid=1 without allocating a…
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] Userspace can inject an inconsistent state by setting IRS_IST_BASER.valid=1 without allocating a host IST, leading to a CONSTRAINED UNPREDICTABLE hardware state if the guest subsequently clears the valid bit. --
--- Patch [32]: [PATCH v5 32/49] KVM: arm64: gic-v5: Handle userspace accesses to IRS MMIO region --- commit 24ee880222eb3e4ff06c9b27b1d655da134efd7a
Author: Sascha Bischoff <[email protected]> Date: Fri Aug 7 11:28:54 2026 +0000
KVM: arm64: gic-v5: Handle userspace accesses to IRS MMIO region
As part of saving and restoring the state of a GICv5-based system, userspace must save and restore the IRS MMIO registers. These include important information such as the guest IST configuration, and KVM must present consistent state to the guest after migration.
Quoted 4 lines
arch/arm64/kvm/vgic/vgic-irs-v5.c +0
-0
[ ... ]
Quoted 21 lines
arch/arm64/kvm/vgic/vgic-irs-v5.c +11
-8+static int vgic_v5_mmio_uaccess_write_irs (struct kvm_vcpu *vcpu,gpa_t addr, +unsigned int len,unsigned long val) +{ +struct vgic_dist *vgic = &vcpu->kvm->arch.vgic; +struct vgic_v5_irs *irs_data = vgic->vgic_v5_irs_data; +size_t offset = addr & (SZ_64K -1 ); -/* The address cannot be changed while the IST is valid. */ -if (irs->ist_baser.valid && valid) -return ; +/* +* The following registers are ONLY settable via uaccesses. The guest +* cannot write them! +*/ -/* Valid -> Invalid */ -if (irs->ist_baser.valid && !valid) { -/* Make the LPI IST invalid and then ... */ -if (irq_set_vcpu_affinity (vgic_v5_vpe_db (vcpu), &cmd)) -break ; +switch (offset) {
[ ... ]
arch/arm64/kvm/vgic/vgic-irs-v5.c static void vgic_v5_mmio_write_irs_ist(struct kvm_vcpu *vcpu, gpa_t addr, · line 617
Quoted 7 lines
arch/arm64/kvm/vgic/vgic-irs-v5.c +7
-0+case GICV5_IRS_IST_BASER : +if (irs_data->ist_baser.valid && + !vgic_v5_ist_baser_matches (irs_data, val)) +return -EINVAL ; + +vgic_v5_update_irs_ist_baser (irs_data, val); +break ;
[Severity: High]
Does this allow userspace to inject an inconsistent state?
If userspace sets the valid bit of GICV5_IRS_IST_BASER to 1 via vgic_v5_mmio_uaccess_write_irs() but does not actually allocate a host LPI IST (by omitting the KVM_DEV_ARM_VGIC_GRP_IST ioctl), the VM can still be started.
When the guest subsequently clears the valid bit, KVM's MMIO handler:
vgic_v5_write_irs_ist_baser() {
...
if (irs->ist_baser.valid && !valid) {
/* Make the LPI IST invalid and then ... */
rc = irq_set_vcpu_affinity(vgic_v5_vpe_db(vcpu), &cmd);detects a Valid -> Invalid transition and issues the LPI_VIST_MAKE_INVALID command. This sends an unmap command to the hardware for a VIST that was never mapped.
Since this results in CONSTRAINED UNPREDICTABLE hardware behavior, should KVM reject uaccess writes that set the valid bit without a backing IST, or perhaps verify IST presence before issuing the unmap command?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=32
Re: [PATCH v5 32/49] KVM: arm64: gic-v5: Handle userspace accesses to IRS MMIO region
Quoted 86 lines · On Fri, 2026-08-07 at 16:20 +0000, [email protected] wrote:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] Userspace can inject an inconsistent state by setting IRS_IST_BASER.valid=1 without allocating a host IST, leading to a CONSTRAINED UNPREDICTABLE hardware state if the guest subsequently clears the valid bit.
----- Patch [32]: [PATCH v5 32/49] KVM: arm64: gic-v5: Handle userspace accesses to IRS MMIO region ---
commit24ee880222eb3e4ff06c9b27b1d655da134efd7a
Author: Sascha Bischoff <[email protected]>
Date: Fri Aug 7 11:28:54 2026 +0000KVM: arm64: gic-v5: Handle userspace accesses to IRS MMIO region
As part of saving and restoring the state of a GICv5-based system, userspace must save and restore the IRS MMIO registers. These include important information such as the guest IST configuration, and KVM must present consistent state to the guest after migration.Quoted 5 lines
arch/arm64/kvm/vgic/vgic-irs-v5.c +0
-0[ ... ]
Quoted 27 lines
+static int vgic_v5_mmio_uaccess_write_irs(struct
kvm_vcpu*vcpu,gpa_taddr,arch/arm64/kvm/vgic/vgic-irs-v5.c +1
-0+ unsigned int len,unsigned long val)
arch/arm64/kvm/vgic/vgic-irs-v5.c +4
-0+{ +struct vgic_dist *vgic = &vcpu->kvm->arch.vgic; +struct vgic_v5_irs *irs_data = vgic->vgic_v5_irs_data; +size_t offset = addr & (SZ_64K -1 );arch/arm64/kvm/vgic/vgic-irs-v5.c +0
-1-/* The address cannot be changed while the IST is valid. */
arch/arm64/kvm/vgic/vgic-irs-v5.c +2
-2-if (irs->ist_baser.valid && valid) -return ; +/* +* The following registers are ONLY settable via uaccesses. The guest
arch/arm64/kvm/vgic/vgic-irs-v5.c +2
-0+* cannot write them! +*/ arch/arm64/kvm/vgic/vgic-irs-v5.c +0
-3-/* Valid -> Invalid */ -if (irs->ist_baser.valid && !valid) { -/* Make the LPI IST invalid and then ... */
arch/arm64/kvm/vgic/vgic-irs-v5.c +0
-1- if(
irq_set_vcpu_affinity(vgic_v5_vpe_db(vcpu), &cmd))arch/arm64/kvm/vgic/vgic-irs-v5.c +1
-1-break ; +switch (offset) {[ ... ]
arch/arm64/kvm/vgic/vgic-irs-v5.c static void vgic_v5_mmio_write_irs_ist(struct kvm_vcpu *vcpu, gpa_t addr, · line 617
Quoted 7 lines
arch/arm64/kvm/vgic/vgic-irs-v5.c +7
-0+case GICV5_IRS_IST_BASER : +if (irs_data->ist_baser.valid && + !vgic_v5_ist_baser_matches(irs_data, val)) +return -EINVAL ; + +vgic_v5_update_irs_ist_baser (irs_data, val); +break ;[Severity: High]
Does this allow userspace to inject an inconsistent state?If userspace sets the valid bit of
GICV5_IRS_IST_BASERto 1 viavgic_v5_mmio_uaccess_write_irs()but does not actually allocate a host LPI IST
(by omitting theKVM_DEV_ARM_VGIC_GRP_ISTioctl), the VM can still be started.When the guest subsequently clears the valid bit, KVM's MMIO handler:
vgic_v5_write_irs_ist_baser(){
...
if (irs->ist_baser.valid&& !valid) {
/* Make the LPI IST invalid and then ... */ rc =irq_set_vcpu_affinity(vgic_v5_vpe_db(vcpu), &cmd);detects a Valid -> Invalid transition and issues the
LPI_VIST_MAKE_INVALID
command. This sends an unmap command to the hardware for a VIST that was never
mapped.
Quoted 6 lines
Since this results in CONSTRAINED UNPREDICTABLE hardware behavior, should KVM
reject uaccess writes that set the valid bit without a backing IST, or perhaps
verify IST presence before issuing the unmap command?
Right, this is an issue. I've added some tracking in such that when userspace sets the IRS_IST_BASER.Valid bit it must also restore an LPI IST. Without that, we now fail KVM_RUN. This means that the userspace- set state must be consistent with the state being provided to the hardware (via the VMTE) for the guest to be runnable.
This change spans this commit and "KVM: arm64: gic-v5: Implement save/restore mechanisms for ISTs".
Thanks,
Sascha
PATCH v5 33/49 KVM: arm64: gic-v5: Add CoreSight MMIO regs to IRS
In reply to Sascha BischoffExpose the CoreSight identification registers in the emulated GICv5 IRS MMIO region. These registers are read-only from the guest's point of view, so return the architectural identification values and ignore writes.
--- arch/arm64/kvm/vgic/vgic-irs-v5.c | 78 +++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+)
arch/arm64/kvm/vgic/vgic-irs-v5.c +78 -0
@@ -487,6 +487,43 @@ static void vgic_v5_mmio_write_irs_ist(struct kvm_vcpu *vcpu, gpa_t addr, } } +static unsigned long vgic_v5_coresight_read (struct kvm_vcpu *vcpu, +gpa_t addr,unsigned int len) +{ +const size_t offset = addr & (SZ_64K -1 ); + +switch (offset) { +case GICV5_CORESIGHT_DEVARCH : +return GICV5_CORESIGHT_DEVARCH_VAL ; +case GICV5_CORESIGHT_PIDR4 : +return GICV5_CORESIGHT_PIDR4_JEP106_CONT ; +case GICV5_CORESIGHT_PIDR5 : +return GICV5_CORESIGHT_PIDR5_RES0 ; +case GICV5_CORESIGHT_PIDR6 : +return GICV5_CORESIGHT_PIDR6_RES0 ; +case GICV5_CORESIGHT_PIDR7 : +return GICV5_CORESIGHT_PIDR7_RES0 ; +case GICV5_CORESIGHT_PIDR0 : +return GICV5_CORESIGHT_PIDR0_PART_0 ; +case GICV5_CORESIGHT_PIDR1 : +return GICV5_CORESIGHT_PIDR1_DES_0_PART_1 ; +case GICV5_CORESIGHT_PIDR2 : +return GICV5_CORESIGHT_PIDR2_DES_1 ; +case GICV5_CORESIGHT_PIDR3 : +return GICV5_CORESIGHT_PIDR3_REVAND_CMOD ; +case GICV5_CORESIGHT_CIDR0 : +return GICV5_CORESIGHT_CIDR0_VAL ; +case GICV5_CORESIGHT_CIDR1 : +return GICV5_CORESIGHT_CIDR1_VAL ; +case GICV5_CORESIGHT_CIDR2 : +return GICV5_CORESIGHT_CIDR2_VAL ; +case GICV5_CORESIGHT_CIDR3 : +return GICV5_CORESIGHT_CIDR3_VAL ; +default : +return 0 ; + } +} +static unsigned long vgic_v5_mmio_uaccess_read_irs_status (struct kvm_vcpu *vcpu,gpa_t addr,unsigned int len)
@@ -808,6 +845,47 @@ static const struct vgic_register_region vgic_v5_irs_registers[] = {REGISTER_DESC_WITH_LENGTH (GICV5_IRS_SWERR_SYNDROMER1 , vgic_mmio_read_raz, vgic_mmio_write_wi,8 , VGIC_ACCESS_64bit), + +/* CoreSight identification registers */ +REGISTER_DESC_WITH_LENGTH (GICV5_CORESIGHT_DEVARCH , + vgic_v5_coresight_read, vgic_mmio_write_wi,4 , + VGIC_ACCESS_32bit), +REGISTER_DESC_WITH_LENGTH (GICV5_CORESIGHT_PIDR4 , + vgic_v5_coresight_read, vgic_mmio_write_wi,4 , + VGIC_ACCESS_32bit), +REGISTER_DESC_WITH_LENGTH (GICV5_CORESIGHT_PIDR5 , + vgic_v5_coresight_read, vgic_mmio_write_wi,4 , + VGIC_ACCESS_32bit), +REGISTER_DESC_WITH_LENGTH (GICV5_CORESIGHT_PIDR6 , + vgic_v5_coresight_read, vgic_mmio_write_wi,4 , + VGIC_ACCESS_32bit), +REGISTER_DESC_WITH_LENGTH (GICV5_CORESIGHT_PIDR7 , + vgic_v5_coresight_read, vgic_mmio_write_wi,4 , + VGIC_ACCESS_32bit), +REGISTER_DESC_WITH_LENGTH (GICV5_CORESIGHT_PIDR0 , + vgic_v5_coresight_read, vgic_mmio_write_wi,4 , + VGIC_ACCESS_32bit), +REGISTER_DESC_WITH_LENGTH (GICV5_CORESIGHT_PIDR1 , + vgic_v5_coresight_read, vgic_mmio_write_wi,4 , + VGIC_ACCESS_32bit), +REGISTER_DESC_WITH_LENGTH (GICV5_CORESIGHT_PIDR2 , + vgic_v5_coresight_read, vgic_mmio_write_wi,4 , + VGIC_ACCESS_32bit), +REGISTER_DESC_WITH_LENGTH (GICV5_CORESIGHT_PIDR3 , + vgic_v5_coresight_read, vgic_mmio_write_wi,4 , + VGIC_ACCESS_32bit), +REGISTER_DESC_WITH_LENGTH (GICV5_CORESIGHT_CIDR0 , + vgic_v5_coresight_read, vgic_mmio_write_wi,4 , + VGIC_ACCESS_32bit), +REGISTER_DESC_WITH_LENGTH (GICV5_CORESIGHT_CIDR1 , + vgic_v5_coresight_read, vgic_mmio_write_wi,4 , + VGIC_ACCESS_32bit), +REGISTER_DESC_WITH_LENGTH (GICV5_CORESIGHT_CIDR2 , + vgic_v5_coresight_read, vgic_mmio_write_wi,4 , + VGIC_ACCESS_32bit), +REGISTER_DESC_WITH_LENGTH (GICV5_CORESIGHT_CIDR3 , + vgic_v5_coresight_read, vgic_mmio_write_wi,4 , + VGIC_ACCESS_32bit), };unsigned int vgic_v5_init_irs_iodev (struct vgic_io_device *dev)
Signature · 1 line
2.34.1
PATCH v5 34/49 KVM: arm64: gic-v5: Add VGICv5 IST save/restore UAPI
In reply to Sascha BischoffIntroduce KVM_DEV_ARM_VGIC_GRP_IST as the VGICv5 device attribute group used to transfer SPI and LPI IST state between KVM and userspace during migration.
The group uses struct kvm_vgic_v5_ist as a descriptor for the userspace-owned SPI and LPI IST buffers. Each buffer contains the architected 32-bit ISTE state for the corresponding interrupt number space, while the host IST layout remains private to KVM.
The descriptor is architecture-specific, as it is only the payload of an ARM VGIC device attribute.
--- arch/arm64/include/uapi/asm/kvm.h | 9 +++++++++ tools/arch/arm64/include/uapi/asm/kvm.h | 9 +++++++++ 2 files changed, 18 insertions(+)
arch/arm64/include/uapi/asm/kvm.h +9 -0
@@ -423,6 +423,15 @@ enum {#define KVM_DEV_ARM_VGIC_GRP_ITS_REGS 8#define KVM_DEV_ARM_VGIC_GRP_MAINT_IRQ 9#define KVM_DEV_ARM_VGIC_GRP_IRS_REGS 10 +#define KVM_DEV_ARM_VGIC_GRP_IST 11 + +struct kvm_vgic_v5_ist { +__u64 spi_ist_addr; +__u64 spi_ist_size; +__u64 lpi_ist_addr; +__u64 lpi_ist_size; +}; +#define KVM_DEV_ARM_VGIC_LINE_LEVEL_INFO_SHIFT 10#define KVM_DEV_ARM_VGIC_LINE_LEVEL_INFO_MASK \ (0x3fffffULL << KVM_DEV_ARM_VGIC_LINE_LEVEL_INFO_SHIFT)
tools/arch/arm64/include/uapi/asm/kvm.h +9 -0
@@ -423,6 +423,15 @@ enum {#define KVM_DEV_ARM_VGIC_GRP_ITS_REGS 8#define KVM_DEV_ARM_VGIC_GRP_MAINT_IRQ 9#define KVM_DEV_ARM_VGIC_GRP_IRS_REGS 10 +#define KVM_DEV_ARM_VGIC_GRP_IST 11 + +struct kvm_vgic_v5_ist { +__u64 spi_ist_addr; +__u64 spi_ist_size; +__u64 lpi_ist_addr; +__u64 lpi_ist_size; +}; +#define KVM_DEV_ARM_VGIC_LINE_LEVEL_INFO_SHIFT 10#define KVM_DEV_ARM_VGIC_LINE_LEVEL_INFO_MASK \ (0x3fffffULL << KVM_DEV_ARM_VGIC_LINE_LEVEL_INFO_SHIFT)
Signature · 1 line
2.34.1
[email protected]
AI reviewRe: [PATCH v5 34/49] KVM: arm64: gic-v5: Add VGICv5 IST save/restore UAPI
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] Incomplete implementation: UAPI definitions added without corresponding kernel backend --
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] Incomplete implementation: UAPI definitions added without corresponding kernel backend --
--- Patch [34]: [PATCH v5 34/49] KVM: arm64: gic-v5: Add VGICv5 IST save/restore UAPI --- commit db4f69bf4672970c99891b1c1bd9652c4ebfb88a
Author: Sascha Bischoff <[email protected]>
KVM: arm64: gic-v5: Add VGICv5 IST save/restore UAPI
This commit introduces KVM_DEV_ARM_VGIC_GRP_IST as a VGICv5 device attribute group to transfer SPI and LPI IST state between KVM and userspace. It defines struct kvm_vgic_v5_ist as a descriptor for the IST buffers.
Quoted 6 lines
arch/arm64/include/uapi/asm/kvm.h +0
-0@@ -423,6 +423,15 @@ enum {#define KVM_DEV_ARM_VGIC_GRP_ITS_REGS 8
arch/arm64/include/uapi/asm/kvm.h enum { · line 433
Quoted 10 lines
arch/arm64/include/uapi/asm/kvm.h +8
-0#define KVM_DEV_ARM_VGIC_GRP_MAINT_IRQ 9#define KVM_DEV_ARM_VGIC_GRP_IRS_REGS 10 +#define KVM_DEV_ARM_VGIC_GRP_IST 11 + +struct kvm_vgic_v5_ist { +__u64 spi_ist_addr; +__u64 spi_ist_size; +__u64 lpi_ist_addr; +__u64 lpi_ist_size; +};
[Severity: Medium]
Are the backend ioctl handlers for this new UAPI missing from this patch?
The commit message notes this is used to transfer state between KVM and userspace, but it looks like only the header definitions are present.
Without the backend code, userspace attempting to use KVM_DEV_ARM_VGIC_GRP_IST will receive an error.
[ ... ]
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=34
Re: [PATCH v5 34/49] KVM: arm64: gic-v5: Add VGICv5 IST save/restore UAPI
Quoted 45 lines · On Fri, 2026-08-07 at 16:30 +0000, [email protected] wrote:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] Incomplete implementation: UAPI definitions added without corresponding kernel backend
----- Patch [34]: [PATCH v5 34/49] KVM: arm64: gic-v5: Add VGICv5 IST save/restore UAPI ---
commitdb4f69bf4672970c99891b1c1bd9652c4ebfb88a
Author: Sascha Bischoff <[email protected]>KVM: arm64: gic-v5: Add VGICv5 IST save/restore UAPI
This commit introducesKVM_DEV_ARM_VGIC_GRP_ISTas a VGICv5 device attribute
group to transfer SPI and LPI IST state between KVM and userspace. It defines
structkvm_vgic_v5_istas a descriptor for the IST buffers.Quoted 7 lines
arch/arm64/include/uapi/asm/kvm.h +0
-0@@ -423,6 +423,15 @@ enum { #define KVM_DEV_ARM_VGIC_GRP_ITS_REGS 8arch/arm64/include/uapi/asm/kvm.h enum { · line 433
Quoted 10 lines
arch/arm64/include/uapi/asm/kvm.h +8
-0#define KVM_DEV_ARM_VGIC_GRP_MAINT_IRQ 9 #define KVM_DEV_ARM_VGIC_GRP_IRS_REGS 10 +#define KVM_DEV_ARM_VGIC_GRP_IST 11 + +struct kvm_vgic_v5_ist { +__u64 spi_ist_addr; +__u64 spi_ist_size; +__u64 lpi_ist_addr; +__u64 lpi_ist_size; +};[Severity: Medium]
Are the backend ioctl handlers for this new UAPI missing from this patch?The commit message notes this is used to transfer state between KVM and
userspace, but it looks like only the header definitions are present.
Quoted 6 lines
Without the backend code, userspace attempting to use
KVM_DEV_ARM_VGIC_GRP_IST
will receive an error.[ ... ]
That's indeed correct. They are added in subsequent commits, so this is fine and by design.
Thanks,
Sascha
PATCH v5 35/49 KVM: arm64: gic-v5: Implement save/restore mechanisms for ISTs
In reply to Sascha BischoffWhen running a GICv5 VM, there are up to two ISTs that must be saved or restored when migrating a VM.
The SPI IST is allocated by the hypervisor, as the guest presumes the memory for the SPI state is allocated by the hardware. The LPI IST is also shadowed in KVM when the guest enables LPIs, so the guest's LPI IST memory is not used directly by the physical GICv5 hardware.
As both in-use ISTs are backed by host allocations, userspace provides migration storage for both tables through KVM_DEV_ARM_VGIC_GRP_IST. The userspace descriptor supplies separate SPI and LPI buffers, each containing the architected 32-bit ISTE state for the corresponding interrupt number space. If the guest has not configured an LPI IST, userspace must omit the LPI buffer.
On save, acquire every vCPU mutex, returning -EBUSY if any vCPU is already running. Holding these locks blocks KVM_RUN while the IST state is exported. Use IRS_SAVE_VMR to write the IRS's internal state back to the ISTs and check that the VM remained quiescent. After copying each IST, issue a Q-only IRS_SAVE_VMR operation to update IRS_SAVE_VM_STATUSR.Q and repeat the check. If the VM has not remained quiescent since the save began, propagate an error to userspace so that the save can be retried without losing incoming interrupt state.
On restore, reject the operation if any vCPU has already run. Validate the userspace buffers and, if the restored IRS state describes an LPI IST, allocate the shadow host IST while the VMTE is still valid. This allows the IRS operation that assigns the IST to update the VMTE. Then make the VMTE invalid before copying the SPI and LPI IST state from the userspace-provided buffers, and make it valid again once the copy is complete.
As part of restoring the ISTs, track pending interrupts and clear their pending state from the restored host ISTs. Once the VM is valid again, make those interrupts pending through the GIC VDPEND system instruction.
Once a host LPI IST has been allocated, the guest-visible IRS_IST_BASER and IRS_IST_CFGR state describes that allocation. Userspace may replay values with the same defined fields, but KVM rejects changes while the host IST exists. During save, also require the VMTE IST_ID_BITS value to match IRS_IST_CFGR.LPI_ID_BITS so that userspace buffer validation and the host IST walk use the same number of entries.
--- arch/arm64/kvm/vgic/vgic-irs-v5.c | 47 +- arch/arm64/kvm/vgic/vgic-kvm-device.c | 13 + arch/arm64/kvm/vgic/vgic-v5-tables.c | 596 ++++++++++++++++++++++++++ arch/arm64/kvm/vgic/vgic-v5-tables.h | 16 + arch/arm64/kvm/vgic/vgic-v5.c | 330 +++++++++++++- arch/arm64/kvm/vgic/vgic.h | 5 + 6 files changed, 1001 insertions(+), 6 deletions(-)
arch/arm64/kvm/vgic/vgic-irs-v5.c +45 -2
@@ -370,6 +370,20 @@ static bool vgic_v5_ist_cfgr_valid(struct vgic_v5_irs *irs)return irs->ist_cfgr.l2sz == irs->idr2.ist_l2sz; } +static bool vgic_v5_ist_cfgr_matches (const struct vgic_v5_irs *irs, +unsigned long val) +{ +u8 lpi_id_bits =FIELD_GET (GICV5_IRS_IST_CFGR_LPI_ID_BITS , val); +u8 l2sz =FIELD_GET (GICV5_IRS_IST_CFGR_L2SZ , val); +u8 istsz =FIELD_GET (GICV5_IRS_IST_CFGR_ISTSZ , val); +bool structure = !!(val &GICV5_IRS_IST_CFGR_STRUCTURE ); + +return irs->ist_cfgr.lpi_id_bits == lpi_id_bits && + irs->ist_cfgr.l2sz == l2sz && + irs->ist_cfgr.istsz == istsz && + irs->ist_cfgr.structure == structure; +} +static unsigned long vgic_v5_mmio_read_irs_ist (struct kvm_vcpu *vcpu,gpa_t addr,unsigned int len) {
@@ -550,6 +564,7 @@ static int vgic_v5_mmio_uaccess_write_irs(struct kvm_vcpu *vcpu, gpa_t addr,struct vgic_dist *vgic = &vcpu->kvm->arch.vgic;struct vgic_v5_irs *irs_data = vgic->vgic_v5_irs_data;size_t offset = addr & (SZ_64K -1 ); +int ret;/* * The following registers are ONLY settable via uaccesses. The guest
@@ -646,13 +661,23 @@ static int vgic_v5_mmio_uaccess_write_irs(struct kvm_vcpu *vcpu, gpa_t addr,return -EINVAL ;break ;case GICV5_IRS_IST_BASER : -if (irs_data->ist_baser.valid && - !vgic_v5_ist_baser_matches (irs_data, val)) + ret =vgic_v5_lpi_ist_exists (vcpu->kvm); +if (ret <0 ) +return ret; + +if (ret && !vgic_v5_ist_baser_matches (irs_data, val))return -EINVAL ;vgic_v5_update_irs_ist_baser (irs_data, val);break ;case GICV5_IRS_IST_CFGR : + ret =vgic_v5_lpi_ist_exists (vcpu->kvm); +if (ret <0 ) +return ret; + +if (ret && !vgic_v5_ist_cfgr_matches (irs_data, val)) +return -EINVAL ; + irs_data->ist_cfgr.lpi_id_bits =FIELD_GET (GICV5_IRS_IST_CFGR_LPI_ID_BITS , val); irs_data->ist_cfgr.l2sz =FIELD_GET (GICV5_IRS_IST_CFGR_L2SZ , val); irs_data->ist_cfgr.istsz =FIELD_GET (GICV5_IRS_IST_CFGR_ISTSZ , val);
@@ -1053,6 +1078,24 @@ int kvm_vgic_v5_irs_init(struct kvm *kvm, unsigned int nr_spis)return 0 ; } +int vgic_v5_irs_lpi_ist_id_bits (struct kvm *kvm,unsigned int *id_bits) +{ +struct vgic_v5_irs *irs = kvm->arch.vgic.vgic_v5_irs_data; + +if (!irs) +return -ENXIO ; + +if (!irs->ist_baser.valid) +return 0 ; + +if (!vgic_v5_ist_cfgr_valid (irs)) +return -EINVAL ; + + *id_bits = irs->ist_cfgr.lpi_id_bits; + +return 1 ; +} +int vgic_v5_has_attr_regs (struct kvm_device *dev,struct kvm_device_attr *attr) {const struct vgic_register_region *region;
arch/arm64/kvm/vgic/vgic-kvm-device.c +13 -0
@@ -920,6 +920,11 @@ static int vgic_v5_set_attr(struct kvm_device *dev,switch (attr->group) {case KVM_DEV_ARM_VGIC_GRP_ADDR :break ; +case KVM_DEV_ARM_VGIC_GRP_IST : +if (attr->attr) +return -ENXIO ; + +return vgic_v5_irs_restore_ists (dev->kvm, attr);case KVM_DEV_ARM_VGIC_GRP_IRS_REGS : fallthrough;case KVM_DEV_ARM_VGIC_GRP_CPU_SYSREGS :
@@ -948,6 +953,11 @@ static int vgic_v5_get_attr(struct kvm_device *dev,switch (attr->group) {case KVM_DEV_ARM_VGIC_GRP_ADDR :break ; +case KVM_DEV_ARM_VGIC_GRP_IST : +if (attr->attr) +return -ENXIO ; + +return vgic_v5_irs_save_ists (dev->kvm, attr);case KVM_DEV_ARM_VGIC_GRP_IRS_REGS : fallthrough;case KVM_DEV_ARM_VGIC_GRP_CPU_SYSREGS :
@@ -997,6 +1007,9 @@ static int vgic_v5_has_attr(struct kvm_device *dev,default :return -ENXIO ; } +break ; +case KVM_DEV_ARM_VGIC_GRP_IST : +return attr->attr ? -ENXIO :0 ;default :return -ENXIO ; }
arch/arm64/kvm/vgic/vgic-v5-tables.c +596 -0
641 lines not on this page · raw message
arch/arm64/kvm/vgic/vgic-v5-tables.h +16 -0
@@ -9,6 +9,7 @@#include <linux/spinlock.h> #include <linux/idr.h> #include <linux/irqchip/arm-gic-v5.h> +#include <linux/list.h> /* Level 1 Virtual Machine Table Entry */ typedef __le64 vmtl1_entry ;
@@ -44,6 +45,9 @@ struct vgic_v5_vm_info {__le64 *h_lpi_ist;__le64 **h_lpi_l2_ists;__le64 *h_spi_ist; + +/* Tracking of pending interrupts as part of IST restore */ +struct list_head pending_irqs; };struct vgic_v5_vmt {
@@ -118,7 +122,19 @@ int vgic_v5_vmte_alloc_vpe(struct kvm_vcpu *vcpu);int vgic_v5_vmte_free_vpe (struct kvm_vcpu *vcpu);int vgic_v5_spi_ist_alloc (struct kvm *kvm,unsigned int id_bits); +int vgic_v5_lpi_ist_exists (struct kvm *kvm);int vgic_v5_lpi_ist_alloc (struct kvm *kvm,unsigned int id_bits);int vgic_v5_lpi_ist_free (struct kvm *kvm); +int vgic_v5_save_spi_ist (struct kvm *kvm, +struct kvm_vgic_v5_ist *ist_attr); +int vgic_v5_save_lpi_ist (struct kvm *kvm, +struct kvm_vgic_v5_ist *ist_attr); +int vgic_v5_restore_spi_ist (struct kvm *kvm, +struct kvm_vgic_v5_ist *ist_attr); +int vgic_v5_restore_lpi_ist (struct kvm *kvm, +struct kvm_vgic_v5_ist *ist_attr); +int vgic_v5_restore_pending_irqs (struct kvm *kvm); +void vgic_v5_discard_pending_irqs (struct kvm *kvm); +#endif
arch/arm64/kvm/vgic/vgic-v5.c +326 -4
360 lines not on this page · raw message
arch/arm64/kvm/vgic/vgic.h +5 -0
@@ -373,6 +373,8 @@ void vgic_v5_teardown(struct kvm *kvm);int vgic_v5_map_resources (struct kvm *kvm);void vgic_v5_set_ppi_ops (struct kvm_vcpu *vcpu,u32 vintid);void vgic_v5_set_spi_ops (struct vgic_irq *irq); +bool vgic_v5_spi_queue_irq_unlock (struct kvm *kvm,struct vgic_irq *irq, +unsigned long flags);void vgic_v5_set_irq_pend (struct kvm_vcpu *vcpu,struct vgic_irq *irq);bool vgic_v5_has_pending_ppi (struct kvm_vcpu *vcpu);void vgic_v5_flush_ppi_state (struct kvm_vcpu *vcpu);
@@ -384,11 +386,14 @@ void vgic_v5_get_vmcr(struct kvm_vcpu *vcpu, struct vgic_vmcr *vmcr);void vgic_v5_restore_state (struct kvm_vcpu *vcpu);void vgic_v5_save_state (struct kvm_vcpu *vcpu);int vgic_v5_register_irs_iodev (struct kvm *kvm,gpa_t irs_base_address); +int vgic_v5_irs_lpi_ist_id_bits (struct kvm *kvm,unsigned int *id_bits);int vgic_v5_cpu_sysregs_uaccess (struct kvm_vcpu *vcpu,struct kvm_device_attr *attr,bool is_write);int vgic_v5_has_cpu_sysregs_attr (struct kvm_vcpu *vcpu,struct kvm_device_attr *attr);const struct sys_reg_desc *vgic_v5_get_sysreg_table (unsigned int *sz); +int vgic_v5_irs_save_ists (struct kvm *kvm,struct kvm_device_attr *attr); +int vgic_v5_irs_restore_ists (struct kvm *kvm,struct kvm_device_attr *attr);int vgic_v5_irs_attr_regs_access (struct kvm_device *dev,struct kvm_device_attr *attr,u64 *reg,bool is_write);
Signature · 1 line
2.34.1
[email protected]
AI reviewRe: [PATCH v5 35/49] KVM: arm64: gic-v5: Implement save/restore mechanisms for ISTs
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider: - [High] Restoration of GICv5 IRS registers and IST state unconditionally fails before the VM runs.…
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
- [High] Restoration of GICv5 IRS registers and IST state unconditionally fails before the VM runs.
- [High] Missing memory barrier between DMA memory reads and MMIO writes in IST save paths allows hardware to corrupt IST state.
- [Medium] Early returns in vgic_v5_vmte_release leak VM memory structures if IST freeing fails.
--
--- Patch [35]: [PATCH v5 35/49] KVM: arm64: gic-v5: Implement save/restore mechanisms for ISTs --- commit 32376ab3f3a6714615e18eef70c631afa3b5cd05
Author: Sascha Bischoff <[email protected]>
KVM: arm64: gic-v5: Implement save/restore mechanisms for ISTs
This patch adds support for saving and restoring GICv5 Interrupt State Tables (ISTs) during virtual machine migration. It introduces interfaces to export the internal SPI and LPI IST states to userspace and inject pending interrupts upon restoration. The state is checked for hardware quiescence during the save operation.
Quoted 3 lines
arch/arm64/kvm/vgic/vgic-irs-v5.c +0
-0
[ ... ]
arch/arm64/kvm/vgic/vgic-irs-v5.c static int vgic_v5_mmio_uaccess_write_irs(struct kvm_vcpu *vcpu, gpa_t addr, · line 666
Quoted 9 lines
arch/arm64/kvm/vgic/vgic-irs-v5.c +3
-2@@ -646,13 +661,23 @@ static int vgic_v5_mmio_uaccess_write_irs(struct kvm_vcpu *vcpu, gpa_t addr,return -EINVAL ;break ;case GICV5_IRS_IST_BASER : -if (irs_data->ist_baser.valid && - !vgic_v5_ist_baser_matches (irs_data, val)) + ret =vgic_v5_lpi_ist_exists (vcpu->kvm); +if (ret <0 ) +return ret;
[Severity: High]
Is this check going to unconditionally fail during live migration?
Userspace typically restores registers before the first KVM_RUN. However, vgic_v5_lpi_ist_exists() requires the vmi structure to be present in the vm_info XArray, which is only allocated in vgic_v5_vmte_init() (triggered by the first KVM_RUN).
Will this cause -ENXIO to be returned when restoring GICV5_IRS_IST_BASER?
[ ... ]
Quoted 3 lines
arch/arm64/kvm/vgic/vgic-v5-tables.c +0
-0
[ ... ]
arch/arm64/kvm/vgic/vgic-v5-tables.c int vgic_v5_vmte_release(struct kvm *kvm) · line 670
Quoted 10 lines
arch/arm64/kvm/vgic/vgic-v5-tables.c +3
-0@@ -604,6 +662,9 @@ int vgic_v5_vmte_release(struct kvm *kvm)if (!vmi) goto no_vmi; +/* Unlikely, but possible. Avoid leaking the memory. */ +vgic_v5_drain_pending_irqs (kvm, vmi, false); +/* If we have an LPI IST, free it */ if (vmi->h_lpi_ist) { ret =vgic_v5_lpi_ist_free (kvm);
[Severity: Medium]
Does returning early from vgic_v5_vmte_release() cause a memory leak?
If vgic_v5_lpi_ist_free() or vgic_v5_spi_ist_free() fails, the function returns immediately without freeing vmi->vped_base, vmi->vpet_base, vmi->vmd_base, and the vmi structure itself.
Should these teardown paths make a best-effort attempt to free the remaining resources rather than aborting?
[ ... ]
Quoted 3 lines
arch/arm64/kvm/vgic/vgic-v5.c +0
-0
[ ... ]
Quoted 1 line
arch/arm64/kvm/vgic/vgic-v5.c +0
-0@@ -1267,3 +1300,292 @@ void vgic_v5_save_state(struct kvm_vcpu *vcpu)
[ ... ]
arch/arm64/kvm/vgic/vgic-v5.c void vgic_v5_save_state(struct kvm_vcpu *vcpu) · line 1422
Quoted 6 lines
arch/arm64/kvm/vgic/vgic-v5.c +6
-0+/* Save the SPI IST to the userspace buffer. */ + ret =vgic_v5_save_spi_ist (kvm, &ist_attr); +if (ret) + goto out_unlock; + + ret =vgic_v5_irs_is_quiesced (vm_id);
[Severity: High]
Can the hardware corrupt the IST state during the read loop?
The vgic_v5_save_spi_ist() function reads from DMA memory. It is immediately followed by a call to vgic_v5_irs_is_quiesced(), which uses writeq_relaxed() to issue an MMIO write.
Since raw_spin_lock_irqsave() only provides an ACQUIRE barrier, is there anything preventing the CPU from reordering the relaxed MMIO write before the DMA reads complete? If reordered, the hardware could break quiescence and modify the ISTs concurrently, while the software mistakenly concludes the VM remained quiesced.
Would adding a dma_rmb() or rmb() ensure the DMA reads complete before checking the hardware status?
[ ... ]
Quoted 2 lines
arch/arm64/kvm/vgic/vgic-v5.c +2
-0+int vgic_v5_irs_restore_ists (struct kvm *kvm,struct kvm_device_attr *attr) +{
[ ... ]
arch/arm64/kvm/vgic/vgic-v5.c void vgic_v5_save_state(struct kvm_vcpu *vcpu) · line 1524
Quoted 6 lines
arch/arm64/kvm/vgic/vgic-v5.c +6
-0+ ret =vgic_v5_lpi_ist_exists (kvm); +if (ret) { +if (ret >0 ) + ret = -EBUSY ; + goto out_unlock; + }
[Severity: High]
Does this have the same migration failure as vgic_v5_mmio_uaccess_write_irs()?
Since vmi hasn't been allocated by the first KVM_RUN yet, vgic_v5_lpi_ist_exists() will return -ENXIO, causing the restoration to fail unconditionally.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=35
Re: [PATCH v5 35/49] KVM: arm64: gic-v5: Implement save/restore mechanisms for ISTs
Quoted 53 lines · On Fri, 2026-08-07 at 16:48 +0000, [email protected] wrote:
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
- [High] Restoration of GICv5 IRS registers and IST state unconditionally fails before the VM runs.
- [High] Missing memory barrier between DMA memory reads and MMIO writes in IST save paths allows hardware to corrupt IST state.
- [Medium] Early returns invgic_v5_vmte_releaseleak VM memory structures if IST freeing fails.
----- Patch [35]: [PATCH v5 35/49] KVM: arm64: gic-v5: Implement save/restore mechanisms for ISTs ---
commit32376ab3f3a6714615e18eef70c631afa3b5cd05
Author: Sascha Bischoff <[email protected]>KVM: arm64: gic-v5: Implement save/restore mechanisms for ISTs
This patch adds support for saving and restoring GICv5 Interrupt State Tables
(ISTs) during virtual machine migration. It introduces interfaces to export
the internal SPI and LPI IST states to userspace and inject pending interrupts upon restoration. The state is checked for hardware quiescence
during the save operation.Quoted 4 lines
arch/arm64/kvm/vgic/vgic-irs-v5.c +0
-0[ ... ]
arch/arm64/kvm/vgic/vgic-irs-v5.c static int vgic_v5_mmio_uaccess_write_irs(struct kvm_vcpu *vcpu, gpa_t addr, · line 666
Quoted 10 lines
arch/arm64/kvm/vgic/vgic-irs-v5.c +0
-0@@ -646,13 +661,23 @@ static int vgic_v5_mmio_uaccess_write_irs(struct kvm_vcpu *vcpu, gpa_t addr, return -EINVAL; break;case
GICV5_IRS_IST_BASER:arch/arm64/kvm/vgic/vgic-irs-v5.c +3
-2-if (irs_data->ist_baser.valid && - !vgic_v5_ist_baser_matches(irs_data, val)) + ret =vgic_v5_lpi_ist_exists (vcpu->kvm); +if (ret <0 ) +return ret;[Severity: High]
Is this check going to unconditionally fail during live migration?Userspace typically restores registers before the first
KVM_RUN. However,vgic_v5_lpi_ist_exists()requires the vmi structure to be present in thevm_infoXArray, which is only allocated invgic_v5_vmte_init()(triggered by
the firstKVM_RUN).
Quoted 2 lines
Will this cause
-ENXIOto be returned when restoringGICV5_IRS_IST_BASER?
There's no way to restore the GICv5 registers without first initialising it. Doing that in the opposite order would result in the restored register state being obliterated (and as you say, we'd miss the required structures to do so). This is not a real issue IMO.
Quoted 21 lines
[ ... ]
Quoted 4 lines
arch/arm64/kvm/vgic/vgic-v5-tables.c +0
-0[ ... ]
Quoted 10 lines
arch/arm64/kvm/vgic/vgic-v5-tables.c +0
-0@@ -604,6 +662,9 @@ int vgic_v5_vmte_release(struct kvm *kvm) if (!vmi) goto no_vmi;arch/arm64/kvm/vgic/vgic-v5-tables.c +3
-0+/* Unlikely, but possible. Avoid leaking the memory. */ +vgic_v5_drain_pending_irqs (kvm, vmi, false); +/* If we have an LPI IST, free it */
if (vmi->h_lpi_ist) {
ret =vgic_v5_lpi_ist_free(kvm);[Severity: Medium]
Does returning early fromvgic_v5_vmte_release()cause a memory leak?
Quoted 8 lines
If
vgic_v5_lpi_ist_free()orvgic_v5_spi_ist_free()fails, the function
returns immediately without freeingvmi->vped_base,vmi->vpet_base,vmi->vmd_base, and the vmi structure itself.Should these teardown paths make a best-effort attempt to free the remaining
resources rather than aborting?
They have freed as much as they can. If the hardware may still access the memory, then it cannot be freed as it otherwise might be reused for something else. This is by design, and should only happen if things have already gone very wrong.
Quoted 36 lines
[ ... ]
Quoted 4 lines
arch/arm64/kvm/vgic/vgic-v5.c +0
-0[ ... ]
Quoted 2 lines
arch/arm64/kvm/vgic/vgic-v5.c +0
-0@@ -1267,3 +1300,292 @@ void vgic_v5_save_state(struct kvm_vcpu *vcpu)[ ... ]
arch/arm64/kvm/vgic/vgic-v5.c void vgic_v5_save_state(struct kvm_vcpu *vcpu) · line 1422
Quoted 6 lines
arch/arm64/kvm/vgic/vgic-v5.c +6
-0+/* Save the SPI IST to the userspace buffer. */ + ret =vgic_v5_save_spi_ist (kvm, &ist_attr); +if (ret) + goto out_unlock; + + ret =vgic_v5_irs_is_quiesced (vm_id);[Severity: High]
Can the hardware corrupt the IST state during the read loop?The
vgic_v5_save_spi_ist()function reads from DMA memory. It is immediately
followed by a call tovgic_v5_irs_is_quiesced(), which useswriteq_relaxed()
to issue an MMIO write.Since
raw_spin_lock_irqsave()only provides an ACQUIRE barrier, is there
anything preventing the CPU from reordering the relaxed MMIO write before
the DMA reads complete? If reordered, the hardware could break quiescence and
modify the ISTs concurrently, while the software mistakenly concludes the VM
remained quiesced.
Quoted 3 lines
Would adding a
dma_rmb()orrmb()ensure the DMA reads complete before
checking the hardware status?
I think you are correct here, although I think you need an mb() not a rmb(). We want read-to-write ordering, not read-to-read. I've added in the mb() before doing the quiescence check.
Quoted 17 lines
[ ... ]
Quoted 3 lines
+int
vgic_v5_irs_restore_ists(struct kvm *kvm, struct kvm_device_attr *attr)
+{[ ... ]
arch/arm64/kvm/vgic/vgic-v5.c void vgic_v5_save_state(struct kvm_vcpu *vcpu) · line 1524
Quoted 6 lines
arch/arm64/kvm/vgic/vgic-v5.c +6
-0+ ret =vgic_v5_lpi_ist_exists (kvm); +if (ret) { +if (ret >0 ) + ret = -EBUSY ; + goto out_unlock; + }[Severity: High]
Does this have the same migration failure asvgic_v5_mmio_uaccess_write_irs()?
Quoted 5 lines
Since vmi hasn't been allocated by the first
KVM_RUNyet,vgic_v5_lpi_ist_exists()will return-ENXIO, causing the restoration to
fail unconditionally.
This is not a real issue. See above.
Thanks,
Sascha
PATCH v5 36/49 Documentation: KVM: Document KVM_DEV_ARM_VGIC_GRP_CPU_SYSREGS for VGICv5
In reply to Sascha BischoffThe virtual GICv5 adopts the same mechanism as GICv3 for userspace reads and writes of the system registers, albeit operating on a different set of registers, of course.
Document KVM_DEV_ARM_VGIC_GRP_CPU_SYSREGS for GICv5 in the VGICv5 documentation, explicitly calling out the registers it operates on. The main body of documentation has been directly copied from the VGICv3 documentation as it has identical operation.
One key thing to note is that for two sets of GICv5 registers - those pertaining to Active and Pending state - the operation of the interface is different to how the actual registers operate. Both of these registers have C and S variants (to set and clear bits) in hardware. However for this interface, we ONLY implement the S variant, AND treat it as a raw write. This simplifies the act of reading or writing the state.
--- .../virt/kvm/devices/arm-vgic-v5.rst | 74 +++++++++++++++++++ 1 file changed, 74 insertions(+)
Documentation/virt/kvm/devices/arm-vgic-v5.rst +74 -0
@@ -75,3 +75,77 @@ Groups: -EFAULT Invalid guest ram access -EBUSY One or more VCPUS are running ======= ======================================================== + + KVM_DEV_ARM_VGIC_GRP_CPU_SYSREGS + Attributes: + + The attr field of kvm_device_attr encodes two values:: + + bits: | 63 .... 32 | 31 .... 16 | 15 .... 0 | + values: | mpidr | RES | instr | + + The mpidr field encodes the CPU ID based on the affinity information in the + architecture defined MPIDR, and the field is encoded as follows:: + + | 63 .... 56 | 55 .... 48 | 47 .... 40 | 39 .... 32 | + | Aff3 | Aff2 | Aff1 | Aff0 | + + The instr field encodes the system register to access based on the fields + defined in the A64 instruction set encoding for system register access + (RES means the bits are reserved for future use and should be zero):: + + | 15 ... 14 | 13 ... 11 | 10 ... 7 | 6 ... 3 | 2 ... 0 | + | Op 0 | Op1 | CRn | CRm | Op2 | + + All system regs accessed through this API are (rw, 64-bit) and + kvm_device_attr.addr points to a __u64 value. + + KVM_DEV_ARM_VGIC_GRP_CPU_SYSREGS accesses the CPU interface registers for the + CPU specified by the mpidr field. + + The available registers are: + + ======================= =================================================== + ICC_ICSR_EL1 + ICC_PPI_ENABLER0_EL1 + ICC_PPI_ENABLER1_EL1 + ICC_PPI_SACTIVER0_EL1 ICC_PPI_CACTIVER0_EL1 is not supported. Writes to + ICC_PPI_SACTIVER0_EL1 are treated as RAW writes of + the underlying state. + ICC_PPI_SACTIVER1_EL1 ICC_PPI_CACTIVER1_EL1 is not supported. Writes to + ICC_PPI_SACTIVER1_EL1 are treated as RAW writes of + the underlying state. + ICC_PPI_SPENDR0_EL1 ICC_PPI_CPENDR0_EL1 is not supported. Writes to + ICC_PPI_SPENDR0_EL1 are treated as RAW writes of + the underlying state. + ICC_PPI_SPENDR1_EL1 ICC_PPI_CPENDR1_EL1 is not supported. Writes to + ICC_PPI_SPENDR1_EL1 are treated as RAW writes of + the underlying state. + ICC_PPI_PRIORITYR0_EL1 + ICC_PPI_PRIORITYR1_EL1 + ICC_PPI_PRIORITYR2_EL1 + ICC_PPI_PRIORITYR3_EL1 + ICC_PPI_PRIORITYR4_EL1 + ICC_PPI_PRIORITYR5_EL1 + ICC_PPI_PRIORITYR6_EL1 + ICC_PPI_PRIORITYR7_EL1 + ICC_PPI_PRIORITYR8_EL1 + ICC_PPI_PRIORITYR9_EL1 + ICC_PPI_PRIORITYR10_EL1 + ICC_PPI_PRIORITYR11_EL1 + ICC_PPI_PRIORITYR12_EL1 + ICC_PPI_PRIORITYR13_EL1 + ICC_PPI_PRIORITYR14_EL1 + ICC_PPI_PRIORITYR15_EL1 + ICC_APR_EL1 + ICC_CR0_EL1 + ICC_PCR_EL1 + ======================= =================================================== + + Errors: + + ======= ============================================================= + -ENXIO Getting or setting this register is not supported + -EBUSY VCPU is running, or write attempted after a VCPU has run + -EINVAL Invalid mpidr or register value supplied + ======= =============================================================
Signature · 1 line
2.34.1
[email protected]
AI reviewRe: [PATCH v5 36/49] Documentation: KVM: Document KVM_DEV_ARM_VGIC_GRP_CPU_SYSREGS for VGICv5
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] Restricting user-space writes to VGICv5 system registers, IRS MMIO registers, and IST state after…
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] Restricting user-space writes to VGICv5 system registers, IRS MMIO registers, and IST state after a VCPU has run prevents VM soft reset. --
--- Patch [36]: [PATCH v5 36/49] Documentation: KVM: Document KVM_DEV_ARM_VGIC_GRP_CPU_SYSREGS for VGICv5 --- commit 5be826b5b86ddbcfa778f582ad314ba1e4599113
Author: Sascha Bischoff <[email protected]>
Documentation: KVM: Document KVM_DEV_ARM_VGIC_GRP_CPU_SYSREGS for VGICv5
This commit documents the KVM_DEV_ARM_VGIC_GRP_CPU_SYSREGS API for the virtual GICv5, defining the register layout, supported system registers, and error codes.
Quoted 4 lines
Documentation/virt/kvm/devices/arm-vgic-v5.rst +0
-0
[ ... ]
Documentation/virt/kvm/devices/arm-vgic-v5.rst Groups: · line 149
Quoted 5 lines
Documentation/virt/kvm/devices/arm-vgic-v5.rst +5
-0+ Errors: + + ======= ============================================================= + -ENXIO Getting or setting this register is not supported + -EBUSY VCPU is running, or write attempted after a VCPU has run
[Severity: High]
Does returning -EBUSY when a write is attempted after a VCPU has run prevent VM soft resets?
During a soft reset (such as PSCI SYSTEM_RESET), QEMU typically pauses the vCPUs and rewrites the initial state of the GIC device while the KVM VM is still considered to have run once, since it doesn't destroy and recreate the entire KVM VM.
If KVM enforces this restriction on KVM_DEV_ARM_VGIC_GRP_CPU_SYSREGS and other device states, won't this reject QEMU's state restore sequence and prevent the VM from rebooting gracefully?
Quoted 2 lines
Documentation/virt/kvm/devices/arm-vgic-v5.rst +2
-0+ -EINVAL Invalid mpidr or register value supplied + ======= =============================================================
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=36
Re: [PATCH v5 36/49] Documentation: KVM: Document KVM_DEV_ARM_VGIC_GRP_CPU_SYSREGS for VGICv5
Quoted 48 lines · On Fri, 2026-08-07 at 16:55 +0000, [email protected] wrote:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] Restricting user-space writes to VGICv5 system registers, IRS MMIO registers, and IST state after a VCPU has run prevents VM soft reset.
----- Patch [36]: [PATCH v5 36/49] Documentation: KVM: Document
KVM_DEV_ARM_VGIC_GRP_CPU_SYSREGSfor VGICv5 --- commit5be826b5b86ddbcfa778f582ad314ba1e4599113
Author: Sascha Bischoff <[email protected]>Documentation: KVM: Document
KVM_DEV_ARM_VGIC_GRP_CPU_SYSREGSfor VGICv5
This commit documents theKVM_DEV_ARM_VGIC_GRP_CPU_SYSREGSAPI for the virtual
GICv5, defining the register layout, supported system registers, and error
codes.Quoted 5 lines
Documentation/virt/kvm/devices/arm-vgic-v5.rst +0
-0[ ... ]
Quoted 7 lines
+ Errors:
+
+ =======
=============================================================
+ -ENXIO Getting or setting this register is not supported + -EBUSY VCPU is running, or write attempted after a VCPU has run[Severity: High]
Does returning-EBUSYwhen a write is attempted after a VCPU has run prevent
VM soft resets?During a soft reset (such as PSCI
SYSTEM_RESET), QEMU typically pauses the
vCPUs and rewrites the initial state of the GIC device while the KVM VM is
still considered to have run once, since it doesn't destroy and recreate the
entire KVM VM.
Quoted 5 lines
If KVM enforces this restriction on
KVM_DEV_ARM_VGIC_GRP_CPU_SYSREGSand
other device states, won't this reject QEMU's state restore sequence and
prevent the VM from rebooting gracefully?
This is a good point, but I don't think that we can realistically work this into the existing APIs.
I think we will actually need an explicit KVM_DEV_ARM_VGIC_V5_CTRL_RESET or similar, like we already have for the ITS (KVM_DEV_ARM_ITS_CTRL_RESET). The problem we have is that GICv5 manages the state of interrupts in hardware, so on a reset we pretty much need to make the VM invalid, tear down the ISTs, reallocate a clean SPI IST, zero KVM's PPI/SPI shadow state, reset the IRS MMIO register state, and then make the VM valid again. Either that or we let state persist across that boundary, in which case YMMV.
Is this something that we'd be happy to address later, or should I work this into the current series?
Quoted 5 lines
Quoted 4 lines
+ -EINVAL Invalid mpidr or register value supplied + =======
=============================================================
Thanks,
Sascha
PATCH v5 37/49 Documentation: KVM: Add KVM_DEV_ARM_VGIC_GRP_IRS_REGS to VGICv5 docs
In reply to Sascha BischoffDocument the KVM_DEV_ARM_VGIC_GRP_IRS_REGS attribute group used to read and write the virtual IRS's MMIO register state. This provides a GICv5-specific interface for state that is conceptually similar to the VGICv3 ITS register interface, but uses IRS terminology instead of ITS.
--- .../virt/kvm/devices/arm-vgic-v5.rst | 47 +++++++++++++++++++ 1 file changed, 47 insertions(+)
Documentation/virt/kvm/devices/arm-vgic-v5.rst +47 -0
@@ -149,3 +149,50 @@ Groups: -EBUSY VCPU is running, or write attempted after a VCPU has run -EINVAL Invalid mpidr or register value supplied ======= ============================================================= + + +KVM_DEV_ARM_VGIC_GRP_IRS_REGS + Attributes: + The attr field of kvm_device_attr encodes the offset of the IRS register, + relative to the IRS CONFIG_FRAME base address. This is the address that + was provided via KVM_VGIC_V5_ADDR_TYPE_IRS when creating VGICv5 in the + first place. + + kvm_device_attr.addr points to a __u64 value whatever the width + of the addressed register (32/64 bits). 64 bit registers can only + be accessed with full length. + + Writes to read-only registers are ignored by the kernel except for + IRS_IDR0 - IRS_IDR2 and IRS_IDR5 - IRS_IDR7, which are sanity checked to + ensure that they match a sane config. + + IRS_IDR3 and IRS_IDR4 are RAZ/WI, as nested virtualization is not + supported. + + For registers without dedicated userspace accessors, getting or setting a + register uses the same emulated MMIO handlers as guest reads/writes. + Dedicated userspace accessors may instead save or restore migration state + without triggering guest-visible side effects. For example, restoring + IRS_IST_BASER only restores the emulated register state; any host LPI IST + allocation based on the restored IRS_IST_CFGR and IRS_IST_BASER state + happens when KVM_DEV_ARM_VGIC_GRP_IST is restored. + + Once an LPI IST has been allocated, IRS_IST_CFGR is immutable. Userspace + may write back its current value, but setting a different value is + rejected with -EINVAL. + + Once an LPI IST has been allocated, IRS_IST_BASER is immutable. + Userspace may write back its current value, but setting a different value + is rejected with -EINVAL. + + Errors: + + ======= ================================================================= + -ENXIO Offset does not correspond to any supported register + -EFAULT Invalid user pointer for attr->addr + -EINVAL Offset is not 32-bit aligned for 32-bit MMIO registers, not + 64-bit aligned for 64-bit registers, or the supplied register + value is not compatible with the configured VGICv5 IRS state + -EBUSY VGIC is not initialized, one or more VCPUs are running, or a + write is attempted after a VCPU has run + ======= =================================================================
Signature · 1 line
2.34.1
[email protected]
AI reviewRe: [PATCH v5 37/49] Documentation: KVM: Add KVM_DEV_ARM_VGIC_GRP_IRS_REGS to VGICv5 docs
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Low] Missing indentation for the KVM_DEV_ARM_VGIC_GRP_IRS_REGS group breaks the document's list…
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Low] Missing indentation for the KVM_DEV_ARM_VGIC_GRP_IRS_REGS group breaks the document's list structure. --
--- Patch [37]: [PATCH v5 37/49] Documentation: KVM: Add KVM_DEV_ARM_VGIC_GRP_IRS_REGS to VGICv5 docs --- commit e5dffc46ed9c5cdaecdbb49991374d8f99bfa132
Author: Sascha Bischoff <[email protected]>
Documentation: KVM: Add KVM_DEV_ARM_VGIC_GRP_IRS_REGS to VGICv5 docs
This commit documents the KVM_DEV_ARM_VGIC_GRP_IRS_REGS attribute group used to read and write the virtual IRS's MMIO register state. It provides a GICv5-specific interface using IRS terminology.
Documentation/virt/kvm/devices/arm-vgic-v5.rst Groups: · line 154
Quoted 11 lines
Documentation/virt/kvm/devices/arm-vgic-v5.rst +3
-0@@ -149,3 +149,50 @@ Groups: -EBUSY VCPU is running, or write attempted after a VCPU has run -EINVAL Invalid mpidr or register value supplied ======= ============================================================= + + +KVM_DEV_ARM_VGIC_GRP_IRS_REGS
[Severity: Low]
This isn't a bug, but does this group name need to be indented by two spaces?
Being flush left breaks the list structure under the "Groups:" section. This will cause the entry to be rendered as a top-level paragraph rather than a list item in the generated HTML documentation.
Quoted 1 line
Documentation/virt/kvm/devices/arm-vgic-v5.rst +1
-0+ Attributes:
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=37
Re: [PATCH v5 37/49] Documentation: KVM: Add KVM_DEV_ARM_VGIC_GRP_IRS_REGS to VGICv5 docs
Quoted 38 lines · On Fri, 2026-08-07 at 16:52 +0000, [email protected] wrote:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Low] Missing indentation for theKVM_DEV_ARM_VGIC_GRP_IRS_REGSgroup breaks the document's list structure.
----- Patch [37]: [PATCH v5 37/49] Documentation: KVM: Add
KVM_DEV_ARM_VGIC_GRP_IRS_REGSto VGICv5 docs --- commite5dffc46ed9c5cdaecdbb49991374d8f99bfa132
Author: Sascha Bischoff <[email protected]>Documentation: KVM: Add
KVM_DEV_ARM_VGIC_GRP_IRS_REGSto VGICv5 docsThis commit documents the
KVM_DEV_ARM_VGIC_GRP_IRS_REGSattribute group used
to read and write the virtual IRS's MMIO register state. It provides a
GICv5-specific interface using IRS terminology.Quoted 11 lines
Documentation/virt/kvm/devices/arm-vgic-v5.rst +0
-0@@ -149,3 +149,50 @@ Groups:-EBUSY VCPU is running, or write attempted after a VCPU has run
-EINVAL Invalid mpidr or register value supplied
=======
=============================================================Documentation/virt/kvm/devices/arm-vgic-v5.rst Groups: · line 154
Quoted 3 lines
Documentation/virt/kvm/devices/arm-vgic-v5.rst +3
-0+ + +KVM_DEV_ARM_VGIC_GRP_IRS_REGS[Severity: Low]
This isn't a bug, but does this group name need to be indented by two spaces?
Quoted 5 lines
Being flush left breaks the list structure under the "Groups:" section. This
will cause the entry to be rendered as a top-level paragraph rather than a
list item in the generated HTML documentation.
This was a mistake. I have fixed the indentation.
Quoted 3 lines
Quoted 2 lines
+ Attributes:
Thanks,
Sascha
PATCH v5 38/49 Documentation: KVM: Add docs for KVM_DEV_ARM_VGIC_GRP_IST
In reply to Sascha BischoffDocument the IST save/restore userspace interface for the VGICv5 device, KVM_DEV_ARM_VGIC_GRP_IST.
--- .../virt/kvm/devices/arm-vgic-v5.rst | 66 +++++++++++++++++++ 1 file changed, 66 insertions(+)
Documentation/virt/kvm/devices/arm-vgic-v5.rst +66 -0
@@ -196,3 +196,69 @@ KVM_DEV_ARM_VGIC_GRP_IRS_REGS -EBUSY VGIC is not initialized, one or more VCPUs are running, or a write is attempted after a VCPU has run ======= ================================================================= + + KVM_DEV_ARM_VGIC_GRP_IST + Attributes: + This interface is used to either save the state of the IRS's Interrupt + State Tables (ISTs), or to restore them. A get operation saves IST state, + and a set operation restores IST state. kvm_device_attr.attr is reserved + and must be zero. + + The VGIC must be initialized before using this interface. Restore must be + performed before the VM has run. For restore, userspace must have already + restored the IRS state needed to describe any guest LPI IST. + + Saving first asks the IRS to save and quiesce the VM so that interrupt + state has been written back to the ISTs. KVM checks that the VM remains + quiesced while copying out the SPI and LPI IST state. + + kvm_device_attr.addr points to a struct kvm_vgic_v5_ist:: + + struct kvm_vgic_v5_ist { + __u64 spi_ist_addr; + __u64 spi_ist_size; + __u64 lpi_ist_addr; + __u64 lpi_ist_size; + }; + + The SPI and LPI IST buffers contain one little-endian 32-bit IST entry per + interrupt, in interrupt number order. Only the architected 32-bit ISTE + state is exposed to userspace; host IST layout and metadata are private to + KVM. + + The SPI IST buffer is required and its size must be: + + nr_spis * sizeof(__u32) + + where nr_spis is the value returned by KVM_DEV_ARM_VGIC_GRP_NR_IRQS for + the VGICv5 device. For VGICv5 this value is the number of SPIs, not the + total number of interrupts. + + The LPI IST buffer is required if the guest's IRS_IST_CFGR and + IRS_IST_BASER state describes a valid LPI IST, and its size must be: + + BIT(lpi_id_bits) * sizeof(__u32) + + where lpi_id_bits is the LPI ID width configured in IRS_IST_CFGR. If no + LPI IST is configured, lpi_ist_addr and lpi_ist_size must both be zero. + + Errors: + + =========== ============================================================ + -EBUSY One or more VCPUs are running, the VGIC is not initialized, + restore was requested after the VM has run, an LPI IST + already exists, or the save operation completed but the VM + did not remain quiesced + -EINVAL attr->addr is NULL, the userspace IST descriptor is missing + a required buffer, supplies a non-zero LPI buffer when no + LPI IST is configured, has a buffer size that does not match + the configured VGICv5 IST state, or an internal VM table + operation rejected the VM state + -EFAULT Invalid user pointer for attr->addr, spi_ist_addr, or + lpi_ist_addr + -ENXIO Required per-VM VGICv5/IST backing state is missing or + inconsistent + -ENOMEM Restoring IST state failed while allocating the host LPI IST + or tracking pending interrupts + -ETIMEDOUT An IRS save/VM operation timed out + =========== ============================================================
Signature · 1 line
2.34.1
PATCH v5 39/49 Documentation: KVM: Add the VGICv5 IRS save/restore sequences
In reply to Sascha BischoffDocument the ordering requirements for saving and restoring the GICv5 IRS state.
The IST state is exposed through KVM_DEV_ARM_VGIC_GRP_IST and is copied to or from userspace-provided buffers. Saving the ISTs therefore does not update guest memory, so userspace does not need to serialize guest memory after saving the IST state.
On restore, userspace must create the vCPUs, configure the IRS address, restore the number of SPIs, and initialize the VGIC before restoring IRS state. The IRS MMIO registers may be restored in any order, but the final IRS_IDR* state and the IRS_IST_CFGR/IRS_IST_BASER state that describes any LPI IST must be restored before KVM_DEV_ARM_VGIC_GRP_IST. KVM uses that state to validate the userspace IST buffers and allocate any host LPI IST.
The IRS_*_STATUSR registers are observational state in KVM. Userspace may save them for validation or debugging, but they are not required as restore input and do not need to be replayed.
--- .../virt/kvm/devices/arm-vgic-v5.rst | 49 +++++++++++++++++++ 1 file changed, 49 insertions(+)
Documentation/virt/kvm/devices/arm-vgic-v5.rst +49 -0
@@ -262,3 +262,52 @@ KVM_DEV_ARM_VGIC_GRP_IRS_REGS or tracking pending interrupts -ETIMEDOUT An IRS save/VM operation timed out =========== ============================================================ + +IRS Save Sequence: +------------------ + +The following operations are required when saving the virtual GICv5 IRS: + +a) Save the ISTs by issuing KVM_GET_DEVICE_ATTR on KVM_DEV_ARM_VGIC_GRP_IST. +b) Save the IRS MMIO register state by issuing KVM_GET_DEVICE_ATTR on + KVM_DEV_ARM_VGIC_GRP_IRS_REGS. + +These two steps may be performed in either order. Saving the ISTs writes all +IST state to userspace-provided buffers and does not update guest memory. + +IRS Restore Sequence: +--------------------- + +The following ordering must be followed when restoring the virtual GICv5 and +IRS: + +a) Create vCPUs. +b) Provide the IRS base address by issuing KVM_SET_DEVICE_ATTR on + KVM_DEV_ARM_VGIC_GRP_ADDR +c) Restore the number of SPIs by issuing KVM_SET_DEVICE_ATTR on + KVM_DEV_ARM_VGIC_GRP_NR_IRQS. +d) Initialise the GIC - this sets up the default state and creates the SPI + IST - by issuing KVM_SET_DEVICE_ATTR on KVM_DEV_ARM_VGIC_GRP_CTRL with + KVM_DEV_ARM_VGIC_CTRL_INIT +e) Restore the IRS MMIO register state by issuing KVM_SET_DEVICE_ATTR on + KVM_DEV_ARM_VGIC_GRP_IRS_REGS. The IRS registers may be restored in any + order, but any IRS_IDR* state and the IRS_IST_CFGR/IRS_IST_BASER state + describing an LPI IST must be restored before restoring the ISTs. KVM uses + the restored IRS_IST_CFGR and IRS_IST_BASER state to allocate the LPI IST + during the following step. +f) Restore the ISTs by issuing KVM_SET_DEVICE_ATTR on + KVM_DEV_ARM_VGIC_GRP_IST. + +The number of SPIs must be restored before VGIC initialization because +initialization allocates the SPI state and fixes the SPI range exposed by the +IRS ID registers. + +The IRS's MMIO registers must be restored prior to restoring the ISTs as these +are used to convey the number of LPIs the guest has configured to KVM. + +The various ``*_STATUSR`` registers are observational state in the current KVM +implementation. Userspace may save them for validation or debugging purposes, +but they are not required as restore input and do not need to be replayed during +restore. + +Then vCPUs can be started.
Signature · 1 line
2.34.1
PATCH v5 40/49 KVM: selftests: Add VGICv5 IRS address attribute tests
In reply to Sascha BischoffAdd coverage for the VGICv5 IRS address attribute. The address is the base of the guest-visible MMIO region used to configure the virtual IRS, so userspace must provide a valid base address before the VM can run.
Verify that KVM reports the attribute, returns VGIC_ADDR_UNDEF before it is configured, rejects misaligned and out-of-IPA-range addresses, accepts a valid base, rejects a second base, and fails vCPU entry if the VGIC is initialized without an IRS address.
--- tools/testing/selftests/kvm/arm64/vgic_v5.c | 121 ++++++++++++++++-- .../selftests/kvm/include/arm64/gic_v5.h | 3 + 2 files changed, 110 insertions(+), 14 deletions(-)
tools/testing/selftests/kvm/arm64/vgic_v5.c +107 -14
@@ -14,6 +14,8 @@#define NR_VCPUS 1 +static u64 max_phys_size; +struct vm_gic {struct kvm_vm *vm;int gic_fd;
@@ -25,6 +27,104 @@ struct vm_gic {#define GUEST_CMD_IS_AWAKE 12#define GUEST_CMD_IS_READY 13 +/* we don't want to assert on run execution, hence that helper */ +static int run_vcpu (struct kvm_vcpu *vcpu) +{ +return __vcpu_run (vcpu) ? -errno :0 ; +} + +static void vm_gic_destroy (struct vm_gic *v) +{ +close (v->gic_fd); +kvm_vm_free (v->vm); +} + +struct vgic_region_attr { +u64 attr; +u64 size; +u64 alignment; +}; + +static const struct vgic_region_attr gic_v5_irs_region = { + .attr =KVM_VGIC_V5_ADDR_TYPE_IRS , + .size =GICV5_IRS_SIZE , + .alignment =GICV5_IRS_ALIGN , +}; + +static void test_vgic_v5_create (void ) +{ +struct kvm_vcpu *vcpu; +struct vm_gic v; +u64 addr; +int ret; + + v.gic_dev_type =KVM_DEV_TYPE_ARM_VGIC_V5 ; + v.vm =__vm_create (VM_SHAPE_DEFAULT ,NR_VCPUS ,0 ); + v.gic_fd =kvm_create_device (v.vm, v.gic_dev_type); + +/* Check existing group/attributes */ +kvm_has_device_attr (v.gic_fd,KVM_DEV_ARM_VGIC_GRP_ADDR , gic_v5_irs_region.attr); + +/* check non existing attribute */ + ret =__kvm_has_device_attr (v.gic_fd,KVM_DEV_ARM_VGIC_GRP_ADDR ,-1 ); +TEST_ASSERT (ret && errno ==ENXIO ,"attribute not supported" ); + +/* get IRS base address before setting*/ + ret =__kvm_device_attr_get (v.gic_fd,KVM_DEV_ARM_VGIC_GRP_ADDR , +KVM_VGIC_V5_ADDR_TYPE_IRS , &addr); +TEST_ASSERT (!ret && addr == (-1ULL ),"GICv5 IRS returns VGIC_ADDR_UNDEF" ); + +/* misaligned IRS address settings */ + addr = gic_v5_irs_region.alignment /0x10 ; + ret =__kvm_device_attr_set (v.gic_fd,KVM_DEV_ARM_VGIC_GRP_ADDR , +KVM_VGIC_V5_ADDR_TYPE_IRS , &addr); +TEST_ASSERT (ret && errno ==EINVAL ,"GIC IRS base not aligned" ); + +/* out of range address */ + addr = max_phys_size; + ret =__kvm_device_attr_set (v.gic_fd,KVM_DEV_ARM_VGIC_GRP_ADDR , +KVM_VGIC_V5_ADDR_TYPE_IRS , &addr); +TEST_ASSERT (ret && errno ==E2BIG ,"IRS address beyond IPA limit" ); + +/* Space for half an IRS (an IRS is: 2 * irs.alignment). */ + addr = max_phys_size - gic_v5_irs_region.alignment; + ret =__kvm_device_attr_set (v.gic_fd,KVM_DEV_ARM_VGIC_GRP_ADDR , +KVM_VGIC_V5_ADDR_TYPE_IRS , &addr); +TEST_ASSERT (ret && errno ==E2BIG , +"half of the IRS is beyond IPA limit" ); + +/* set IRS base address @0x0*/ + addr =0x00000 ; + ret =__kvm_device_attr_set (v.gic_fd,KVM_DEV_ARM_VGIC_GRP_ADDR , +KVM_VGIC_V5_ADDR_TYPE_IRS , &addr); +TEST_ASSERT (!ret,"GICv5 IRS base correctly set" ); + +/* get IRS base address */ + addr =0xbad ; + ret =__kvm_device_attr_get (v.gic_fd,KVM_DEV_ARM_VGIC_GRP_ADDR , +KVM_VGIC_V5_ADDR_TYPE_IRS , &addr); +TEST_ASSERT (!ret && addr ==0 ,"GICv5 IRS base correctly set" ); + +/* Attempt to create a second IRS region */ + addr =0xE0000 ; + ret =__kvm_device_attr_set (v.gic_fd,KVM_DEV_ARM_VGIC_GRP_ADDR , +KVM_VGIC_V5_ADDR_TYPE_IRS , &addr); +TEST_ASSERT (ret && errno ==EEXIST ,"GICv5 IRS base set again" ); + +vm_gic_destroy (&v); + +/* Try running a VM without ever setting the IRS base addr */ + v.vm =__vm_create (VM_SHAPE_DEFAULT ,NR_VCPUS ,0 ); + v.gic_fd =kvm_create_device (v.vm, v.gic_dev_type); + vcpu =vm_vcpu_add (v.vm,0 ,NULL ); +kvm_device_attr_set (v.gic_fd,KVM_DEV_ARM_VGIC_GRP_CTRL , +KVM_DEV_ARM_VGIC_CTRL_INIT ,NULL ); + ret =run_vcpu (vcpu); +TEST_ASSERT (ret && errno ==ENXIO ,"GICv5 IRS base not set" ); + +vm_gic_destroy (&v); +} +static void guest_irq_handler (struct ex_regs *regs) {bool valid;
@@ -81,19 +181,6 @@ static void guest_code(void)while (1 ); } - -/* we don't want to assert on run execution, hence that helper */ -static int run_vcpu (struct kvm_vcpu *vcpu) -{ -return __vcpu_run (vcpu) ? -errno :0 ; -} - -static void vm_gic_destroy (struct vm_gic *v) -{ -close (v->gic_fd); -kvm_vm_free (v->vm); -} -static void test_vgic_v5_ppis (u32 gic_dev_type) {struct kvm_vcpu *vcpus[NR_VCPUS ];
@@ -207,13 +294,19 @@ int test_kvm_device(u32 gic_dev_type)void run_tests (u32 gic_dev_type) { +pr_info ("Test VGICv5 Creation & Setup\n" ); +test_vgic_v5_create (); +pr_info ("Test VGICv5 PPIs\n" );test_vgic_v5_ppis (gic_dev_type); }int main (int ac,char **av) { -int ret; +int pa_bits, ret; + + pa_bits = vm_guest_mode_params[VM_MODE_DEFAULT ].pa_bits; + max_phys_size =1ULL << pa_bits;test_disable_default_vgic ();
tools/testing/selftests/kvm/include/arm64/gic_v5.h +3 -0
@@ -13,6 +13,9 @@/* GIC component base address is guest PA space */ #define GICV5_IRS_CONFIG_BASE_GPA 0x8000000ULL +#define GICV5_IRS_SIZE 0x20000 +#define GICV5_IRS_ALIGN 0x10000 +/* * Definitions for GICv5 instructions for the Current Domain */
Signature · 1 line
2.34.1
PATCH v5 41/49 KVM: selftests: Add VGICv5 NR_IRQS attribute tests
In reply to Sascha BischoffAdd coverage for the VGICv5 NR_IRQS attribute. VGICv5 exposes a userspace-selectable SPI count, and KVM must reject values outside its supported range or values that would change the interrupt layout after it has been fixed.
Verify that the attribute defaults to zero before initialisation, rejects too few SPIs, non-32-aligned counts, and a 32-aligned count beyond the 1024-SPI maximum. Also verify that the maximum count is accepted and can be initialised, that the selected count is reported, that initialisation selects the default count, and that subsequent changes are rejected.
--- tools/testing/selftests/kvm/arm64/vgic_v5.c | 190 +++++++++++++++----- 1 file changed, 142 insertions(+), 48 deletions(-)
tools/testing/selftests/kvm/arm64/vgic_v5.c +142 -48
@@ -13,6 +13,8 @@#include "vgic.h" #define NR_VCPUS 1 +#define VGIC_V5_DEFAULT_NR_SPIS 32 +#define VGIC_V5_MAX_NR_SPIS BIT(16)static u64 max_phys_size;
@@ -27,6 +29,63 @@ struct vm_gic {#define GUEST_CMD_IS_AWAKE 12#define GUEST_CMD_IS_READY 13 +static void guest_irq_handler (struct ex_regs *regs) +{ +bool valid; +u32 hwirq; +u64 ia; +static int count; + +/* +* We have pending interrupts. Should never actually enter WFI +* here! +*/ +wfi (); +GUEST_SYNC (GUEST_CMD_IS_AWAKE ); + + ia =gicr_insn (CDIA ); + valid =GICV5_GICR_CDIA_VALID (ia); + +GUEST_SYNC (GUEST_CMD_IRQ_CDIA ); + +if (!valid) +return ; + +gsb_ack (); +isb (); + + hwirq =FIELD_GET (GICV5_GICR_CDIA_INTID , ia); + +gic_insn (hwirq,CDDI ); +gic_insn (0 ,CDEOI ); + +GUEST_SYNC (GUEST_CMD_IRQ_DIEOI ); + +if (++count >=2 ) +GUEST_DONE (); + +/* Ask for the next interrupt to be injected */ +GUEST_SYNC (GUEST_CMD_IS_READY ); +} + +static void guest_code (void ) +{ +local_irq_disable (); + +gicv5_cpu_enable_interrupts (); +local_irq_enable (); + +/* Enable the SW_PPI (3) */ +write_sysreg_s (BIT_ULL (3 ),SYS_ICC_PPI_ENABLER0_EL1 ); + +/* Ask for the first interrupt to be injected */ +GUEST_SYNC (GUEST_CMD_IS_READY ); + +/* Loop forever waiting for interrupts */ +for (;;) +cpu_relax (); +} +/* we don't want to assert on run execution, hence that helper */ static int run_vcpu (struct kvm_vcpu *vcpu) {
@@ -51,7 +110,7 @@ static const struct vgic_region_attr gic_v5_irs_region = { .alignment = GICV5_IRS_ALIGN, }; -static void test_vgic_v5_create (void ) +static void test_vgic_v5_addr_attrs (void ) {struct kvm_vcpu *vcpu;struct vm_gic v;
@@ -125,60 +184,92 @@ static void test_vgic_v5_create(void)vm_gic_destroy (&v); } -static void guest_irq_handler (struct ex_regs *regs) +static void test_vgic_v5_nr_irqs_attrs (void ) { -bool valid; -u32 hwirq; -u64 ia; -static int count; - -/* -* We have pending interrupts. Should never actually enter WFI -* here! -*/ -wfi (); -GUEST_SYNC (GUEST_CMD_IS_AWAKE ); - - ia =gicr_insn (CDIA ); - valid =GICV5_GICR_CDIA_VALID (ia); - -GUEST_SYNC (GUEST_CMD_IRQ_CDIA ); - -if (!valid) -return ; - -gsb_ack (); -isb (); - - hwirq =FIELD_GET (GICV5_GICR_CDIA_INTID , ia); - -gic_insn (hwirq,CDDI ); -gic_insn (0 ,CDEOI ); +const u32 max_nr_spis =BIT (10 ); +struct kvm_vcpu *vcpu; +struct vm_gic v; +u32 nr_irqs; +int ret; -GUEST_SYNC (GUEST_CMD_IRQ_DIEOI ); + v.gic_dev_type =KVM_DEV_TYPE_ARM_VGIC_V5 ; + v.vm =__vm_create (VM_SHAPE_DEFAULT ,NR_VCPUS ,0 ); + v.gic_fd =kvm_create_device (v.vm, v.gic_dev_type); -if (++count >=2 ) -GUEST_DONE (); +/* Check existing group/attribute */ +kvm_has_device_attr (v.gic_fd,KVM_DEV_ARM_VGIC_GRP_NR_IRQS ,0 ); + +/* Before userspace sets NR_IRQS, no SPI count has been selected. */ + nr_irqs =0xbad ; + ret =__kvm_device_attr_get (v.gic_fd,KVM_DEV_ARM_VGIC_GRP_NR_IRQS , +0 , &nr_irqs); +TEST_ASSERT (!ret && nr_irqs ==0 ,"GICv5 NR_IRQS defaults to 0 before init" ); + +/* Too few SPIs */ + nr_irqs =VGIC_V5_DEFAULT_NR_SPIS -1 ; + ret =__kvm_device_attr_set (v.gic_fd,KVM_DEV_ARM_VGIC_GRP_NR_IRQS , +0 , &nr_irqs); +TEST_ASSERT (ret && errno ==EINVAL ,"GICv5 NR_IRQS below minimum" ); + +/* Not a multiple of 32 */ + nr_irqs =VGIC_V5_DEFAULT_NR_SPIS +1 ; + ret =__kvm_device_attr_set (v.gic_fd,KVM_DEV_ARM_VGIC_GRP_NR_IRQS , +0 , &nr_irqs); +TEST_ASSERT (ret && errno ==EINVAL ,"GICv5 NR_IRQS not 32-aligned" ); + +/* Larger than KVM's supported VGICv5 SPI count */ + nr_irqs = max_nr_spis +32 ; + ret =__kvm_device_attr_set (v.gic_fd,KVM_DEV_ARM_VGIC_GRP_NR_IRQS , +0 , &nr_irqs); +TEST_ASSERT (ret && errno ==EINVAL ,"GICv5 NR_IRQS above maximum" ); + +/* Valid custom SPI count */ + nr_irqs = max_nr_spis; + ret =__kvm_device_attr_set (v.gic_fd,KVM_DEV_ARM_VGIC_GRP_NR_IRQS , +0 , &nr_irqs); +TEST_ASSERT (!ret,"GICv5 NR_IRQS accepts valid custom SPI count" ); + + nr_irqs =0xbad ; + ret =__kvm_device_attr_get (v.gic_fd,KVM_DEV_ARM_VGIC_GRP_NR_IRQS , +0 , &nr_irqs); +TEST_ASSERT (!ret && nr_irqs == max_nr_spis, +"GICv5 NR_IRQS returns SPI count only" ); + +/* A second successful configuration attempt must be rejected. */ + nr_irqs =VGIC_V5_DEFAULT_NR_SPIS ; + ret =__kvm_device_attr_set (v.gic_fd,KVM_DEV_ARM_VGIC_GRP_NR_IRQS , +0 , &nr_irqs); +TEST_ASSERT (ret && errno ==EBUSY ,"GICv5 NR_IRQS set twice" ); + +/* The maximum supported count must also initialize successfully. */ + vcpu =vm_vcpu_add (v.vm,0 ,NULL ); +TEST_ASSERT (vcpu,"Failed to create vCPU" ); +kvm_device_attr_set (v.gic_fd,KVM_DEV_ARM_VGIC_GRP_CTRL , +KVM_DEV_ARM_VGIC_CTRL_INIT ,NULL ); -/* Ask for the next interrupt to be injected */ -GUEST_SYNC (GUEST_CMD_IS_READY ); -} +vm_gic_destroy (&v); -static void guest_code (void ) -{ -local_irq_disable (); +/* If userspace does not set NR_IRQS, init selects the default. */ + v.vm =__vm_create (VM_SHAPE_DEFAULT ,NR_VCPUS ,0 ); + v.gic_fd =kvm_create_device (v.vm, v.gic_dev_type); + vcpu =vm_vcpu_add (v.vm,0 ,NULL ); +TEST_ASSERT (vcpu,"Failed to create vCPU" ); +kvm_device_attr_set (v.gic_fd,KVM_DEV_ARM_VGIC_GRP_CTRL , +KVM_DEV_ARM_VGIC_CTRL_INIT ,NULL ); -gicv5_cpu_enable_interrupts (); -local_irq_enable (); + nr_irqs =0xbad ; + ret =__kvm_device_attr_get (v.gic_fd,KVM_DEV_ARM_VGIC_GRP_NR_IRQS , +0 , &nr_irqs); +TEST_ASSERT (!ret && nr_irqs ==VGIC_V5_DEFAULT_NR_SPIS , +"GICv5 NR_IRQS defaults to 32 SPIs after init" ); -/* Enable the SW_PPI (3) */ -write_sysreg_s (BIT_ULL (3 ),SYS_ICC_PPI_ENABLER0_EL1 ); + nr_irqs =VGIC_V5_DEFAULT_NR_SPIS *2 ; + ret =__kvm_device_attr_set (v.gic_fd,KVM_DEV_ARM_VGIC_GRP_NR_IRQS , +0 , &nr_irqs); +TEST_ASSERT (ret && errno ==EBUSY ,"GICv5 NR_IRQS set after init" ); -/* Ask for the first interrupt to be injected */ -GUEST_SYNC (GUEST_CMD_IS_READY ); +vm_gic_destroy (&v); -/* Loop forever waiting for interrupts */ -while (1 ); }static void test_vgic_v5_ppis (u32 gic_dev_type)
@@ -294,8 +385,11 @@ int test_kvm_device(u32 gic_dev_type)void run_tests (u32 gic_dev_type) { -pr_info ("Test VGICv5 Creation & Setup\n" ); -test_vgic_v5_create (); +pr_info ("Test VGICv5 address attrs\n" ); +test_vgic_v5_addr_attrs (); + +pr_info ("Test VGICv5 NR_IRQS attrs\n" ); +test_vgic_v5_nr_irqs_attrs ();pr_info ("Test VGICv5 PPIs\n" );test_vgic_v5_ppis (gic_dev_type);
Signature · 1 line
2.34.1
[email protected]
AI reviewRe: [PATCH v5 41/49] KVM: selftests: Add VGICv5 NR_IRQS attribute tests
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Low] The file-level macro `VGIC_V5_MAX_NR_SPIS` contradicts the actual maximum SPI count hardcoded in…
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Low] The file-level macro VGIC_V5_MAX_NR_SPIS contradicts the actual maximum SPI count hardcoded in the test logic and KVM implementation. --
--- Patch [41]: [PATCH v5 41/49] KVM: selftests: Add VGICv5 NR_IRQS attribute tests --- commit da8da00920607df68a148cd61772ea241d9a820f
Author: Sascha Bischoff <[email protected]>
KVM: selftests: Add VGICv5 NR_IRQS attribute tests
This commit adds coverage for the VGICv5 NR_IRQS attribute. It verifies that KVM rejects values outside the supported range, non-32-aligned counts, and attempts to change the count after initialization.
tools/testing/selftests/kvm/arm64/vgic_v5.c line 17
Quoted 10 lines
tools/testing/selftests/kvm/arm64/vgic_v5.c +2
-0@@ -13,6 +13,8 @@#include "vgic.h" #define NR_VCPUS 1 +#define VGIC_V5_DEFAULT_NR_SPIS 32 +#define VGIC_V5_MAX_NR_SPIS BIT(16)
[Severity: Low]
This isn't a bug, but this macro defines the maximum as 65536, whereas the commit message correctly mentions a "1024-SPI maximum".
[ ... ]
Quoted 25 lines
tools/testing/selftests/kvm/arm64/vgic_v5.c +1
-19@@ -125,60 +184,92 @@ static void test_vgic_v5_create(void)vm_gic_destroy (&v); } -static void guest_irq_handler (struct ex_regs *regs) +static void test_vgic_v5_nr_irqs_attrs (void ) { -bool valid; -u32 hwirq; -u64 ia; -static int count; - -/* -* We have pending interrupts. Should never actually enter WFI -* here! -*/ -wfi (); -GUEST_SYNC (GUEST_CMD_IS_AWAKE ); - - ia =gicr_insn (CDIA ); - valid =GICV5_GICR_CDIA_VALID (ia); - -GUEST_SYNC (GUEST_CMD_IRQ_CDIA ); - -if (!valid)
tools/testing/selftests/kvm/arm64/vgic_v5.c static void test_vgic_v5_create(void) · line 189
Quoted 10 lines
tools/testing/selftests/kvm/arm64/vgic_v5.c +1
-9-return ; - -gsb_ack (); -isb (); - - hwirq =FIELD_GET (GICV5_GICR_CDIA_INTID , ia); - -gic_insn (hwirq,CDDI ); -gic_insn (0 ,CDEOI ); +const u32 max_nr_spis =BIT (10 );
[Severity: Low]
Does VGIC_V5_MAX_NR_SPIS go completely unused in favor of this local max_nr_spis variable?
Should VGIC_V5_MAX_NR_SPIS be defined as BIT(10) and used here instead to avoid having two contradictory limits?
Quoted 4 lines
tools/testing/selftests/kvm/arm64/vgic_v5.c +4
-0+struct kvm_vcpu *vcpu; +struct vm_gic v; +u32 nr_irqs; +int ret;
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=41
Re: [PATCH v5 41/49] KVM: selftests: Add VGICv5 NR_IRQS attribute tests
Quoted 20 lines · On Fri, 2026-08-07 at 17:12 +0000, [email protected] wrote:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Low] The file-level macroVGIC_V5_MAX_NR_SPIScontradicts the actual maximum SPI count hardcoded in the test logic and KVM implementation.
----- Patch [41]: [PATCH v5 41/49] KVM: selftests: Add VGICv5
NR_IRQSattribute tests ---
commitda8da00920607df68a148cd61772ea241d9a820f
Author: Sascha Bischoff <[email protected]>KVM: selftests: Add VGICv5
NR_IRQSattribute testsThis commit adds coverage for the VGICv5
NR_IRQSattribute. It verifies
that KVM rejects values outside the supported range, non-32-aligned counts,
and attempts to change the count after initialization.
Quoted 16 lines
tools/testing/selftests/kvm/arm64/vgic_v5.c line 17
Quoted 11 lines
tools/testing/selftests/kvm/arm64/vgic_v5.c +0
-0@@ -13,6 +13,8 @@ #include "vgic.h"
#defineNR_VCPUS1tools/testing/selftests/kvm/arm64/vgic_v5.c +2
-0+#define VGIC_V5_DEFAULT_NR_SPIS 32 +#define VGIC_V5_MAX_NR_SPIS BIT(16)[Severity: Low]
This isn't a bug, but this macro defines the maximum as 65536, whereas the
commit message correctly mentions a "1024-SPI maximum".
I have fixed this to use BIT(10).
Quoted 44 lines
[ ... ]
Quoted 26 lines
tools/testing/selftests/kvm/arm64/vgic_v5.c +0
-0@@ -125,60 +184,92 @@ static void test_vgic_v5_create(void) vm_gic_destroy(&v); }tools/testing/selftests/kvm/arm64/vgic_v5.c +1
-1-static void guest_irq_handler (struct ex_regs *regs) +static void test_vgic_v5_nr_irqs_attrs (void ){
tools/testing/selftests/kvm/arm64/vgic_v5.c +0
-7-bool valid; -u32 hwirq; -u64 ia; -static int count; - -/* -* We have pending interrupts. Should never actually enter WFI
tools/testing/selftests/kvm/arm64/vgic_v5.c +0
-11-* here! -*/ -wfi (); -GUEST_SYNC (GUEST_CMD_IS_AWAKE ); - - ia =gicr_insn (CDIA ); - valid =GICV5_GICR_CDIA_VALID (ia); - -GUEST_SYNC (GUEST_CMD_IRQ_CDIA ); - -if (!valid)tools/testing/selftests/kvm/arm64/vgic_v5.c static void test_vgic_v5_create(void) · line 189
Quoted 10 lines
tools/testing/selftests/kvm/arm64/vgic_v5.c +1
-9-return ; - -gsb_ack (); -isb (); - - hwirq =FIELD_GET (GICV5_GICR_CDIA_INTID , ia); - -gic_insn (hwirq,CDDI ); -gic_insn (0 ,CDEOI ); +const u32 max_nr_spis =BIT (10 );[Severity: Low]
DoesVGIC_V5_MAX_NR_SPISgo completely unused in favor of this localmax_nr_spisvariable?
Quoted 3 lines
Should
VGIC_V5_MAX_NR_SPISbe defined as BIT(10) and used here instead to
avoid having two contradictory limits?
I've switched to using the global define.
Quoted 6 lines
tools/testing/selftests/kvm/arm64/vgic_v5.c static void test_vgic_v5_create(void) · line 193
Quoted 5 lines
tools/testing/selftests/kvm/arm64/vgic_v5.c +4
-0+struct kvm_vcpu *vcpu; +struct vm_gic v; +u32 nr_irqs; +int ret;
Thanks,
Sascha
PATCH v5 42/49 KVM: selftests: Add VGICv5 IRS_REGS attribute tests
In reply to Sascha BischoffAdd coverage for the VGICv5 IRS_REGS attribute group. Migration needs a userspace-visible view of the virtual IRS register state, but KVM must expose only valid registers and reject state that cannot be restored safely.
Verify that KVM_HAS_DEVICE_ATTR reports the IRS_REGS group before the IRS base is configured, while register reads and writes are rejected before VGIC initialisation. Once the VGIC is initialised, verify that all exposed registers can be read and their values written back.
Check that bad offsets, incorrect alignment, and bad userspace pointers are rejected. Also check ID register restore validation, idle status register values, and IRS_IST_BASER restore handling.
--- tools/testing/selftests/kvm/arm64/vgic_v5.c | 255 ++++++++++++++++++ .../selftests/kvm/include/arm64/gic_v5.h | 72 +++++ 2 files changed, 327 insertions(+)
tools/testing/selftests/kvm/arm64/vgic_v5.c +255 -0
@@ -18,6 +18,8 @@static u64 max_phys_size; +#define VGIC_V5_TEST_IST_BASER_GPA 0x10000000ULL +struct vm_gic {struct kvm_vm *vm;int gic_fd;
@@ -110,6 +112,72 @@ static const struct vgic_region_attr gic_v5_irs_region = { .alignment = GICV5_IRS_ALIGN, }; +struct vgic_irs_reg_attr { +const char *name; +u64 attr; +}; + +#define IRS_REG (r) \ + { \ + .name = #r, \ + .attr = r, \ + } + +static const struct vgic_irs_reg_attr gic_v5_irs_regs[] = { +IRS_REG (GICV5_IRS_IDR0 ), +IRS_REG (GICV5_IRS_IDR1 ), +IRS_REG (GICV5_IRS_IDR2 ), +IRS_REG (GICV5_IRS_IDR3 ), +IRS_REG (GICV5_IRS_IDR4 ), +IRS_REG (GICV5_IRS_IDR5 ), +IRS_REG (GICV5_IRS_IDR6 ), +IRS_REG (GICV5_IRS_IDR7 ), +IRS_REG (GICV5_IRS_IIDR ), +IRS_REG (GICV5_IRS_AIDR ), +IRS_REG (GICV5_IRS_CR0 ), +IRS_REG (GICV5_IRS_CR1 ), +IRS_REG (GICV5_IRS_SYNCR ), +IRS_REG (GICV5_IRS_SYNC_STATUSR ), +IRS_REG (GICV5_IRS_SPI_VMR ), +IRS_REG (GICV5_IRS_SPI_SELR ), +IRS_REG (GICV5_IRS_SPI_DOMAINR ), +IRS_REG (GICV5_IRS_SPI_RESAMPLER ), +IRS_REG (GICV5_IRS_SPI_CFGR ), +IRS_REG (GICV5_IRS_SPI_STATUSR ), +IRS_REG (GICV5_IRS_PE_SELR ), +IRS_REG (GICV5_IRS_PE_STATUSR ), +IRS_REG (GICV5_IRS_PE_CR0 ), +IRS_REG (GICV5_IRS_IST_BASER ), +IRS_REG (GICV5_IRS_IST_CFGR ), +IRS_REG (GICV5_IRS_IST_STATUSR ), +IRS_REG (GICV5_IRS_MAP_L2_ISTR ), +IRS_REG (GICV5_IRS_VMT_BASER ), +IRS_REG (GICV5_IRS_VMT_CFGR ), +IRS_REG (GICV5_IRS_VMT_STATUSR ), +IRS_REG (GICV5_IRS_VPE_SELR ), +IRS_REG (GICV5_IRS_VPE_DBR ), +IRS_REG (GICV5_IRS_VPE_HPPIR ), +IRS_REG (GICV5_IRS_VPE_CR0 ), +IRS_REG (GICV5_IRS_VPE_STATUSR ), +IRS_REG (GICV5_IRS_VM_DBR ), +IRS_REG (GICV5_IRS_VM_SELR ), +IRS_REG (GICV5_IRS_VM_STATUSR ), +IRS_REG (GICV5_IRS_VMAP_L2_VMTR ), +IRS_REG (GICV5_IRS_VMAP_VMR ), +IRS_REG (GICV5_IRS_VMAP_VISTR ), +IRS_REG (GICV5_IRS_VMAP_L2_VISTR ), +IRS_REG (GICV5_IRS_VMAP_VPER ), +IRS_REG (GICV5_IRS_SAVE_VMR ), +IRS_REG (GICV5_IRS_SAVE_VM_STATUSR ), +IRS_REG (GICV5_IRS_MEC_IDR ), +IRS_REG (GICV5_IRS_MEC_MECID_R ), +IRS_REG (GICV5_IRS_MPAM_IDR ), +IRS_REG (GICV5_IRS_MPAM_PARTID_R ), +IRS_REG (GICV5_IRS_SWERR_STATUSR ), +IRS_REG (GICV5_IRS_SWERR_SYNDROMER0 ), +IRS_REG (GICV5_IRS_SWERR_SYNDROMER1 ), +}; +static void test_vgic_v5_addr_attrs (void ) {struct kvm_vcpu *vcpu;
@@ -272,6 +340,189 @@ static void test_vgic_v5_nr_irqs_attrs(void) } +static void test_vgic_v5_irs_regs_attrs (void ) +{ +struct kvm_vcpu *vcpu; +struct vm_gic v; +u64 attr, val; +int ret, i; + + v.gic_dev_type =KVM_DEV_TYPE_ARM_VGIC_V5 ; + v.vm =__vm_create (VM_SHAPE_DEFAULT ,NR_VCPUS ,0 ); + v.gic_fd =kvm_create_device (v.vm, v.gic_dev_type); + vcpu =vm_vcpu_add (v.vm,0 ,NULL ); +TEST_ASSERT (vcpu,"Failed to create vCPU" ); + +/* IRS_REGS attributes can be probed before the IRS base is set. */ +kvm_has_device_attr (v.gic_fd,KVM_DEV_ARM_VGIC_GRP_IRS_REGS , +GICV5_IRS_IDR0 ); + + attr =GICV5_IRS_CONFIG_BASE_GPA ; +kvm_device_attr_set (v.gic_fd,KVM_DEV_ARM_VGIC_GRP_ADDR , +KVM_VGIC_V5_ADDR_TYPE_IRS , &attr); + +/* Check existing group/attribute */ +kvm_has_device_attr (v.gic_fd,KVM_DEV_ARM_VGIC_GRP_IRS_REGS , +GICV5_IRS_IDR0 ); + +/* IRS_REGS are not accessible before the VGIC is initialized. */ + ret =__kvm_device_attr_get (v.gic_fd,KVM_DEV_ARM_VGIC_GRP_IRS_REGS , +GICV5_IRS_IDR0 , &val); +TEST_ASSERT (ret && errno ==EBUSY ,"GICv5 IRS_REGS get before init" ); + + val =0 ; + ret =__kvm_device_attr_set (v.gic_fd,KVM_DEV_ARM_VGIC_GRP_IRS_REGS , +GICV5_IRS_IDR0 , &val); +TEST_ASSERT (ret && errno ==EBUSY ,"GICv5 IRS_REGS set before init" ); + +kvm_device_attr_set (v.gic_fd,KVM_DEV_ARM_VGIC_GRP_CTRL , +KVM_DEV_ARM_VGIC_CTRL_INIT ,NULL ); + +/* Read all supported IRS regs and write the value back. */ +for (i =0 ; i <ARRAY_SIZE (gic_v5_irs_regs); i++) { + attr = gic_v5_irs_regs[i].attr; + ret =__kvm_has_device_attr (v.gic_fd, +KVM_DEV_ARM_VGIC_GRP_IRS_REGS , + attr); +TEST_ASSERT (!ret,"GICv5 IRS_REGS missing %s" , + gic_v5_irs_regs[i].name); + + val =0xbad ; + ret =__kvm_device_attr_get (v.gic_fd, +KVM_DEV_ARM_VGIC_GRP_IRS_REGS , + attr, &val); +TEST_ASSERT (!ret,"GICv5 IRS_REGS get failed for %s" , + gic_v5_irs_regs[i].name); + + ret =__kvm_device_attr_set (v.gic_fd, +KVM_DEV_ARM_VGIC_GRP_IRS_REGS , + attr, &val); +TEST_ASSERT (!ret,"GICv5 IRS_REGS set failed for %s" , + gic_v5_irs_regs[i].name); + } + +/* Check bad offsets */ + attr =0x10000 ; + ret =__kvm_has_device_attr (v.gic_fd,KVM_DEV_ARM_VGIC_GRP_IRS_REGS , + attr); +TEST_ASSERT (ret && errno ==ENXIO ,"GICv5 IRS_REGS accepted bad offset" ); + + ret =__kvm_device_attr_get (v.gic_fd,KVM_DEV_ARM_VGIC_GRP_IRS_REGS , + attr, &val); +TEST_ASSERT (ret && errno ==ENXIO ,"GICv5 IRS_REGS get bad offset" ); + +/* Check alignment for 32-bit and 64-bit IRS regs. */ + attr =GICV5_IRS_IDR0 +2 ; + ret =__kvm_has_device_attr (v.gic_fd,KVM_DEV_ARM_VGIC_GRP_IRS_REGS , + attr); +TEST_ASSERT (ret && errno ==EINVAL ,"GICv5 IRS_REGS accepted unaligned 32-bit attr" ); + + attr =GICV5_IRS_IST_BASER +4 ; + ret =__kvm_has_device_attr (v.gic_fd,KVM_DEV_ARM_VGIC_GRP_IRS_REGS , + attr); +TEST_ASSERT (ret && errno ==EINVAL ,"GICv5 IRS_REGS accepted unaligned 64-bit attr" ); + +/* Check bad user pointers */ + ret =__kvm_device_attr_get (v.gic_fd,KVM_DEV_ARM_VGIC_GRP_IRS_REGS , +GICV5_IRS_IDR0 ,NULL ); +TEST_ASSERT (ret && errno ==EFAULT ,"GICv5 IRS_REGS get with bad pointer" ); + + ret =__kvm_device_attr_set (v.gic_fd,KVM_DEV_ARM_VGIC_GRP_IRS_REGS , +GICV5_IRS_IDR0 ,NULL ); +TEST_ASSERT (ret && errno ==EFAULT ,"GICv5 IRS_REGS set with bad pointer" ); + +/* ID restore validation rejects unsupported values. */ + val =FIELD_PREP (GICV5_IRS_IDR0_INT_DOM ,GICV5_IRS_IDR0_INT_DOM_SECURE ); + ret =__kvm_device_attr_set (v.gic_fd,KVM_DEV_ARM_VGIC_GRP_IRS_REGS , +GICV5_IRS_IDR0 , &val); +TEST_ASSERT (ret && errno ==EINVAL ,"GICv5 IRS_REGS accepted bad IDR0 domain" ); + + ret =__kvm_device_attr_get (v.gic_fd,KVM_DEV_ARM_VGIC_GRP_IRS_REGS , +GICV5_IRS_IDR0 , &val); +TEST_ASSERT (!ret,"GICv5 IRS_REGS get IDR0 failed" ); + val |=GICV5_IRS_IDR0_SETLPI ; + ret =__kvm_device_attr_set (v.gic_fd,KVM_DEV_ARM_VGIC_GRP_IRS_REGS , +GICV5_IRS_IDR0 , &val); +TEST_ASSERT (ret && errno ==EINVAL ,"GICv5 IRS_REGS accepted unsupported IDR0" ); + + ret =__kvm_device_attr_get (v.gic_fd,KVM_DEV_ARM_VGIC_GRP_IRS_REGS , +GICV5_IRS_IDR1 , &val); +TEST_ASSERT (!ret,"GICv5 IRS_REGS get IDR1 failed" ); + val |=FIELD_PREP (GICV5_IRS_IDR1_PRIORITY_BITS ,0x7 ); + ret =__kvm_device_attr_set (v.gic_fd,KVM_DEV_ARM_VGIC_GRP_IRS_REGS , +GICV5_IRS_IDR1 , &val); +TEST_ASSERT (ret && errno ==EINVAL ,"GICv5 IRS_REGS accepted bad IDR1" ); + + val =0 ; + ret =__kvm_device_attr_set (v.gic_fd,KVM_DEV_ARM_VGIC_GRP_IRS_REGS , +GICV5_IRS_IDR2 , &val); +TEST_ASSERT (ret && errno ==EINVAL ,"GICv5 IRS_REGS accepted bad IDR2" ); + + ret =__kvm_device_attr_get (v.gic_fd,KVM_DEV_ARM_VGIC_GRP_IRS_REGS , +GICV5_IRS_IDR5 , &val); +TEST_ASSERT (!ret,"GICv5 IRS_REGS get IDR5 failed" ); + val++; + ret =__kvm_device_attr_set (v.gic_fd,KVM_DEV_ARM_VGIC_GRP_IRS_REGS , +GICV5_IRS_IDR5 , &val); +TEST_ASSERT (ret && errno ==EINVAL ,"GICv5 IRS_REGS accepted bad IDR5" ); + + ret =__kvm_device_attr_get (v.gic_fd,KVM_DEV_ARM_VGIC_GRP_IRS_REGS , +GICV5_IRS_IDR6 , &val); +TEST_ASSERT (!ret,"GICv5 IRS_REGS get IDR6 failed" ); + val++; + ret =__kvm_device_attr_set (v.gic_fd,KVM_DEV_ARM_VGIC_GRP_IRS_REGS , +GICV5_IRS_IDR6 , &val); +TEST_ASSERT (ret && errno ==EINVAL ,"GICv5 IRS_REGS accepted bad IDR6" ); + + ret =__kvm_device_attr_get (v.gic_fd,KVM_DEV_ARM_VGIC_GRP_IRS_REGS , +GICV5_IRS_IDR7 , &val); +TEST_ASSERT (!ret,"GICv5 IRS_REGS get IDR7 failed" ); + val++; + ret =__kvm_device_attr_set (v.gic_fd,KVM_DEV_ARM_VGIC_GRP_IRS_REGS , +GICV5_IRS_IDR7 , &val); +TEST_ASSERT (ret && errno ==EINVAL ,"GICv5 IRS_REGS accepted bad IDR7" ); + +/* Status registers read as idle through userspace accessors. */ + ret =__kvm_device_attr_get (v.gic_fd,KVM_DEV_ARM_VGIC_GRP_IRS_REGS , +GICV5_IRS_SYNC_STATUSR , &val); +TEST_ASSERT (!ret && val ==GICV5_IRS_SYNC_STATUSR_IDLE , +"GICv5 IRS_REGS SYNC_STATUSR is not idle" ); + + ret =__kvm_device_attr_get (v.gic_fd,KVM_DEV_ARM_VGIC_GRP_IRS_REGS , +GICV5_IRS_SPI_STATUSR , &val); +TEST_ASSERT (!ret && val ==GICV5_IRS_SPI_STATUSR_IDLE , +"GICv5 IRS_REGS SPI_STATUSR is not idle" ); + + ret =__kvm_device_attr_get (v.gic_fd,KVM_DEV_ARM_VGIC_GRP_IRS_REGS , +GICV5_IRS_PE_STATUSR , &val); +TEST_ASSERT (!ret && val ==GICV5_IRS_PE_STATUSR_IDLE , +"GICv5 IRS_REGS PE_STATUSR is not idle" ); + + ret =__kvm_device_attr_get (v.gic_fd,KVM_DEV_ARM_VGIC_GRP_IRS_REGS , +GICV5_IRS_IST_STATUSR , &val); +TEST_ASSERT (!ret && val ==GICV5_IRS_IST_STATUSR_IDLE , +"GICv5 IRS_REGS IST_STATUSR is not idle" ); + +/* +* Userspace restores IST_BASER without allocating or accessing an IST. +* Supply a synthetic, aligned guest address only to verify the register +* round trip. +*/ + val =FIELD_PREP (GICV5_IRS_IST_BASER_ADDR_MASK , +VGIC_V5_TEST_IST_BASER_GPA >>GICV5_IRS_IST_BASER_ADDR_SHIFT ); + val |=GICV5_IRS_IST_BASER_VALID ; + ret =__kvm_device_attr_set (v.gic_fd,KVM_DEV_ARM_VGIC_GRP_IRS_REGS , +GICV5_IRS_IST_BASER , &val); +TEST_ASSERT (!ret,"GICv5 IRS_REGS failed to restore IST_BASER" ); + + attr =0xbad ; + ret =__kvm_device_attr_get (v.gic_fd,KVM_DEV_ARM_VGIC_GRP_IRS_REGS , +GICV5_IRS_IST_BASER , &attr); +TEST_ASSERT (!ret && attr == val,"GICv5 IRS_REGS IST_BASER restore mismatch" ); + +vm_gic_destroy (&v); +} +static void test_vgic_v5_ppis (u32 gic_dev_type) {struct kvm_vcpu *vcpus[NR_VCPUS ];
@@ -391,6 +642,10 @@ void run_tests(u32 gic_dev_type)pr_info ("Test VGICv5 NR_IRQS attrs\n" );test_vgic_v5_nr_irqs_attrs (); +pr_info ("Test VGICv5 IRS_REGS attrs\n" ); +test_vgic_v5_irs_regs_attrs (); + +pr_info ("Test VGICv5 PPIs\n" );test_vgic_v5_ppis (gic_dev_type); }
tools/testing/selftests/kvm/include/arm64/gic_v5.h +72 -0
78 lines not on this page · raw message
Signature · 1 line
2.34.1
[email protected]
AI reviewRe: [PATCH v5 42/49] KVM: selftests: Add VGICv5 IRS_REGS attribute tests
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] The selftest for validating `GICV5_IRS_IDR0` domain rejection assigns a completely zeroed value…
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] The selftest for validating GICV5_IRS_IDR0 domain rejection assigns a completely zeroed value to val instead of modifying the existing register value, making the test ineffective. --
--- Patch [42]: [PATCH v5 42/49] KVM: selftests: Add VGICv5 IRS_REGS attribute tests --- commit 11fcbef9e79956f20030f1a93c0eeaba44375ad8
Author: Sascha Bischoff <[email protected]>
KVM: selftests: Add VGICv5 IRS_REGS attribute tests
This commit adds test coverage for the VGICv5 IRS_REGS attribute group. It verifies that KVM properly exposes valid registers and rejects invalid inputs, bad offsets, or incorrect alignment.
Quoted 4 lines
tools/testing/selftests/kvm/arm64/vgic_v5.c +0
-0
[ ... ]
Quoted 6 lines
tools/testing/selftests/kvm/arm64/vgic_v5.c +2
-0@@ -272,6 +340,189 @@ static void test_vgic_v5_nr_irqs_attrs(void) } +static void test_vgic_v5_irs_regs_attrs (void ) +{
[ ... ]
tools/testing/selftests/kvm/arm64/vgic_v5.c static void test_vgic_v5_nr_irqs_attrs(void) · line 438
Quoted 5 lines
tools/testing/selftests/kvm/arm64/vgic_v5.c +5
-0+/* ID restore validation rejects unsupported values. */ + val =FIELD_PREP (GICV5_IRS_IDR0_INT_DOM ,GICV5_IRS_IDR0_INT_DOM_SECURE ); + ret =__kvm_device_attr_set (v.gic_fd,KVM_DEV_ARM_VGIC_GRP_IRS_REGS , +GICV5_IRS_IDR0 , &val); +TEST_ASSERT (ret && errno ==EINVAL ,"GICv5 IRS_REGS accepted bad IDR0 domain" );
[Severity: Medium]
Will this test effectively check the INT_DOM field rejection?
Since val is assigned directly rather than modifying a valid read value (like the SETLPI test below it), and since GICV5_IRS_IDR0_INT_DOM_SECURE is 0, val is evaluated as 0.
The kernel will likely reject IDR0 = 0 due to other mismatched fields (such as version numbers or feature bits), making this test pass even if the kernel fails to validate the INT_DOM field specifically.
Could this be updated to read the existing IDR0 value and modify its INT_DOM field before calling __kvm_device_attr_set?
Quoted 8 lines
tools/testing/selftests/kvm/arm64/vgic_v5.c +8
-0+ + ret =__kvm_device_attr_get (v.gic_fd,KVM_DEV_ARM_VGIC_GRP_IRS_REGS , +GICV5_IRS_IDR0 , &val); +TEST_ASSERT (!ret,"GICv5 IRS_REGS get IDR0 failed" ); + val |=GICV5_IRS_IDR0_SETLPI ; + ret =__kvm_device_attr_set (v.gic_fd,KVM_DEV_ARM_VGIC_GRP_IRS_REGS , +GICV5_IRS_IDR0 , &val); +TEST_ASSERT (ret && errno ==EINVAL ,"GICv5 IRS_REGS accepted unsupported IDR0" );
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=42
Re: [PATCH v5 42/49] KVM: selftests: Add VGICv5 IRS_REGS attribute tests
Quoted 60 lines · On Fri, 2026-08-07 at 17:17 +0000, [email protected] wrote:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] The selftest for validatingGICV5_IRS_IDR0domain rejection assigns a completely zeroed value tovalinstead of modifying the existing register value, making the test ineffective. ----- Patch [42]: [PATCH v5 42/49] KVM: selftests: Add VGICv5
IRS_REGSattribute tests ---
commit11fcbef9e79956f20030f1a93c0eeaba44375ad8
Author: Sascha Bischoff <[email protected]>KVM: selftests: Add VGICv5
IRS_REGSattribute tests
This commit adds test coverage for the VGICv5IRS_REGSattribute group.
It verifies that KVM properly exposes valid registers and rejects invalid inputs, bad offsets, or incorrect alignment.Quoted 5 lines
tools/testing/selftests/kvm/arm64/vgic_v5.c +0
-0[ ... ]
Quoted 6 lines
tools/testing/selftests/kvm/arm64/vgic_v5.c +0
-0@@ -272,6 +340,189 @@ static void test_vgic_v5_nr_irqs_attrs(void)
}tools/testing/selftests/kvm/arm64/vgic_v5.c +2
-0+static void test_vgic_v5_irs_regs_attrs (void ) +{[ ... ]
Quoted 8 lines
tools/testing/selftests/kvm/arm64/vgic_v5.c +2
-0+/* ID restore validation rejects unsupported values. */ + val = FIELD_PREP(GICV5_IRS_IDR0_INT_DOM,
GICV5_IRS_IDR0_INT_DOM_SECURE);tools/testing/selftests/kvm/arm64/vgic_v5.c +1
-0+ ret = __kvm_device_attr_set(v.gic_fd,
KVM_DEV_ARM_VGIC_GRP_IRS_REGS,tools/testing/selftests/kvm/arm64/vgic_v5.c +2
-0+ GICV5_IRS_IDR0, &val); + TEST_ASSERT(ret && errno == EINVAL, "GICv5 IRS_REGSaccepted bad IDR0 domain");
[Severity: Medium]
Will this test effectively check theINT_DOMfield rejection?Since val is assigned directly rather than modifying a valid read value (like
the SETLPI test below it), and sinceGICV5_IRS_IDR0_INT_DOM_SECUREis 0,
val is evaluated as 0.The kernel will likely reject IDR0 = 0 due to other mismatched fields (such
as version numbers or feature bits), making this test pass even if the kernel
fails to validate theINT_DOMfield specifically.
Quoted 3 lines
Could this be updated to read the existing IDR0 value and modify its
INT_DOM
field before calling__kvm_device_attr_set?
Good point. A zero value currently is rejected specifically because it selects the Secure domain, but preserving the other IDR0 fields makes the test’s intent clearer and should be more future-proof. I’ve changed the test to read IDR0 and modify only the INT_DOM field itself.
Quoted 13 lines
Quoted 11 lines
tools/testing/selftests/kvm/arm64/vgic_v5.c +2
-0+ + ret = __kvm_device_attr_get(v.gic_fd,
KVM_DEV_ARM_VGIC_GRP_IRS_REGS,tools/testing/selftests/kvm/arm64/vgic_v5.c +4
-0+ GICV5_IRS_IDR0, &val); +TEST_ASSERT (!ret,"GICv5 IRS_REGS get IDR0 failed" ); + val |=GICV5_IRS_IDR0_SETLPI ; + ret = __kvm_device_attr_set(v.gic_fd,
KVM_DEV_ARM_VGIC_GRP_IRS_REGS,tools/testing/selftests/kvm/arm64/vgic_v5.c +2
-0+ GICV5_IRS_IDR0, &val); + TEST_ASSERT(ret && errno == EINVAL, "GICv5 IRS_REGSQuoted 1 line
accepted unsupported IDR0");
Thanks,
Sascha
PATCH v5 43/49 KVM: selftests: Add VGICv5 IST attribute tests
In reply to Sascha BischoffAdd API coverage for the VGICv5 IST attribute group. This does not test that IST save/restore preserves interrupt state; it only checks the attribute plumbing and the cases KVM must reject before the end-to-end restore path can rely on the interface.
Verify that KVM reports the IST attribute group, rejects unknown attributes, rejects save/restore before initialization, rejects bad userspace pointers, and rejects IST restore after the VM has run.
Also cover the userspace IST descriptor validation: a VM with SPIs must provide a SPI IST buffer of the expected size, and LPI storage must not be supplied when no LPI IST is configured.
--- tools/testing/selftests/kvm/arm64/vgic_v5.c | 122 ++++++++++++++++++++ 1 file changed, 122 insertions(+)
tools/testing/selftests/kvm/arm64/vgic_v5.c +122 -0
@@ -31,6 +31,17 @@ struct vm_gic {#define GUEST_CMD_IS_AWAKE 12#define GUEST_CMD_IS_READY 13 +static struct kvm_vgic_v5_ist vgic_v5_ist_attr (void *spi_ist,size_t spi_size, +void *lpi_ist,size_t lpi_size) +{ +return (struct kvm_vgic_v5_ist ) { + .spi_ist_addr = (uintptr_t )spi_ist, + .spi_ist_size = spi_size, + .lpi_ist_addr = (uintptr_t )lpi_ist, + .lpi_ist_size = lpi_size, + }; +} +static void guest_irq_handler (struct ex_regs *regs) {bool valid;
@@ -523,6 +534,114 @@ static void test_vgic_v5_irs_regs_attrs(void)vm_gic_destroy (&v); } +static void test_vgic_v5_ist_attrs (void ) +{ +struct kvm_vgic_v5_ist ist_attr; +struct kvm_vcpu *vcpu; +struct vm_gic v; +u32 spi_ist[VGIC_V5_DEFAULT_NR_SPIS ]; +u64 attr; +int ret; + + v.gic_dev_type =KVM_DEV_TYPE_ARM_VGIC_V5 ; + v.vm =__vm_create (VM_SHAPE_DEFAULT ,NR_VCPUS ,0 ); + v.gic_fd =kvm_create_device (v.vm, v.gic_dev_type); + vcpu =vm_vcpu_add (v.vm,0 ,NULL ); +TEST_ASSERT (vcpu,"Failed to create vCPU" ); + +/* Check existing group/attribute */ +kvm_has_device_attr (v.gic_fd,KVM_DEV_ARM_VGIC_GRP_IST ,0 ); + +/* Check non-existing attribute */ + ret =__kvm_has_device_attr (v.gic_fd,KVM_DEV_ARM_VGIC_GRP_IST ,1 ); +TEST_ASSERT (ret && errno ==ENXIO ,"GICv5 IST accepted bad attr" ); + + ist_attr =vgic_v5_ist_attr (spi_ist,sizeof (spi_ist),NULL ,0 ); + ret =__kvm_device_attr_get (v.gic_fd,KVM_DEV_ARM_VGIC_GRP_IST , +1 , &ist_attr); +TEST_ASSERT (ret && errno ==ENXIO ,"GICv5 IST get accepted bad attr" ); + + ret =__kvm_device_attr_set (v.gic_fd,KVM_DEV_ARM_VGIC_GRP_IST , +1 , &ist_attr); +TEST_ASSERT (ret && errno ==ENXIO ,"GICv5 IST set accepted bad attr" ); + + attr =GICV5_IRS_CONFIG_BASE_GPA ; +kvm_device_attr_set (v.gic_fd,KVM_DEV_ARM_VGIC_GRP_ADDR , +KVM_VGIC_V5_ADDR_TYPE_IRS , &attr); + +/* IST save/restore is not accessible before the VGIC is initialized. */ + ret =__kvm_device_attr_get (v.gic_fd,KVM_DEV_ARM_VGIC_GRP_IST , +0 , &ist_attr); +TEST_ASSERT (ret && errno ==EBUSY ,"GICv5 IST get before init" ); + + ret =__kvm_device_attr_set (v.gic_fd,KVM_DEV_ARM_VGIC_GRP_IST , +0 , &ist_attr); +TEST_ASSERT (ret && errno ==EBUSY ,"GICv5 IST set before init" ); + +kvm_device_attr_set (v.gic_fd,KVM_DEV_ARM_VGIC_GRP_CTRL , +KVM_DEV_ARM_VGIC_CTRL_INIT ,NULL ); + +/* A VM with SPIs must provide a userspace IST descriptor. */ + ret =__kvm_device_attr_get (v.gic_fd,KVM_DEV_ARM_VGIC_GRP_IST , +0 ,NULL ); +TEST_ASSERT (ret && errno ==EINVAL ,"GICv5 IST get accepted NULL descriptor" ); + + ret =__kvm_device_attr_set (v.gic_fd,KVM_DEV_ARM_VGIC_GRP_IST , +0 ,NULL ); +TEST_ASSERT (ret && errno ==EINVAL ,"GICv5 IST set accepted NULL descriptor" ); + +/* Check bad userspace IST descriptors. */ + ret =__kvm_device_attr_get (v.gic_fd,KVM_DEV_ARM_VGIC_GRP_IST , +0 , (void *)1 ); +TEST_ASSERT (ret && errno ==EFAULT ,"GICv5 IST get with bad descriptor" ); + + ret =__kvm_device_attr_set (v.gic_fd,KVM_DEV_ARM_VGIC_GRP_IST , +0 , (void *)1 ); +TEST_ASSERT (ret && errno ==EFAULT ,"GICv5 IST set with bad descriptor" ); + +/* Check missing and incorrectly sized SPI IST buffers. */ + ist_attr =vgic_v5_ist_attr (NULL ,0 ,NULL ,0 ); + ret =__kvm_device_attr_get (v.gic_fd,KVM_DEV_ARM_VGIC_GRP_IST , +0 , &ist_attr); +TEST_ASSERT (ret && errno ==EINVAL ,"GICv5 IST get accepted missing SPI buffer" ); + + ist_attr =vgic_v5_ist_attr (spi_ist,sizeof (spi_ist) -sizeof (__u32), +NULL ,0 ); + ret =__kvm_device_attr_set (v.gic_fd,KVM_DEV_ARM_VGIC_GRP_IST , +0 , &ist_attr); +TEST_ASSERT (ret && errno ==EINVAL ,"GICv5 IST set accepted bad SPI size" ); + +/* LPI storage must be absent when no LPI IST is configured. */ + ist_attr =vgic_v5_ist_attr (spi_ist,sizeof (spi_ist), spi_ist, +sizeof (__u32)); + ret =__kvm_device_attr_get (v.gic_fd,KVM_DEV_ARM_VGIC_GRP_IST , +0 , &ist_attr); +TEST_ASSERT (ret && errno ==EINVAL ,"GICv5 IST get accepted unexpected LPI buffer" ); + +vm_gic_destroy (&v); + +/* IST restore is rejected after the VM has run. */ + v.vm =__vm_create (VM_SHAPE_DEFAULT ,NR_VCPUS ,0 ); + v.gic_fd =kvm_create_device (v.vm, v.gic_dev_type); + vcpu =vm_vcpu_add (v.vm,0 , guest_code); + + attr =GICV5_IRS_CONFIG_BASE_GPA ; +kvm_device_attr_set (v.gic_fd,KVM_DEV_ARM_VGIC_GRP_ADDR , +KVM_VGIC_V5_ADDR_TYPE_IRS , &attr); +kvm_device_attr_set (v.gic_fd,KVM_DEV_ARM_VGIC_GRP_CTRL , +KVM_DEV_ARM_VGIC_CTRL_INIT ,NULL ); + + ret =run_vcpu (vcpu); +TEST_ASSERT (!ret,"Failed to run GICv5 VM before IST restore test" ); + + ist_attr =vgic_v5_ist_attr (spi_ist,sizeof (spi_ist),NULL ,0 ); + ret =__kvm_device_attr_set (v.gic_fd,KVM_DEV_ARM_VGIC_GRP_IST , +0 , &ist_attr); +TEST_ASSERT (ret && errno ==EBUSY ,"GICv5 IST restore after run" ); + +vm_gic_destroy (&v); +} +static void test_vgic_v5_ppis (u32 gic_dev_type) {struct kvm_vcpu *vcpus[NR_VCPUS ];
@@ -645,6 +764,9 @@ void run_tests(u32 gic_dev_type)pr_info ("Test VGICv5 IRS_REGS attrs\n" );test_vgic_v5_irs_regs_attrs (); +pr_info ("Test VGICv5 IST attrs\n" ); +test_vgic_v5_ist_attrs (); +pr_info ("Test VGICv5 PPIs\n" );test_vgic_v5_ppis (gic_dev_type);
Signature · 1 line
2.34.1
[email protected]
AI reviewRe: [PATCH v5 43/49] KVM: selftests: Add VGICv5 IST attribute tests
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] Incomplete test coverage for VGICv5 IST attribute validation --
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] Incomplete test coverage for VGICv5 IST attribute validation
--
--- Patch [43]: [PATCH v5 43/49] KVM: selftests: Add VGICv5 IST attribute tests --- commit 9ba6d67381650763d50c218233de98711d15304f
Author: Sascha Bischoff <[email protected]>
KVM: selftests: Add VGICv5 IST attribute tests
This commit adds selftests for the VGICv5 IST attribute group. It verifies that KVM reports the attribute group correctly and rejects invalid operations, such as out-of-order save/restore attempts and badly sized IST buffers.
Quoted 4 lines
tools/testing/selftests/kvm/arm64/vgic_v5.c +0
-0
[ ... ]
Quoted 6 lines
tools/testing/selftests/kvm/arm64/vgic_v5.c +2
-0@@ -523,6 +534,114 @@ static void test_vgic_v5_irs_regs_attrs(void)vm_gic_destroy (&v); } +static void test_vgic_v5_ist_attrs (void ) +{
[ ... ]
tools/testing/selftests/kvm/arm64/vgic_v5.c static void test_vgic_v5_irs_regs_attrs(void) · line 612
Quoted 11 lines
tools/testing/selftests/kvm/arm64/vgic_v5.c +11
-0+/* Check missing and incorrectly sized SPI IST buffers. */ + ist_attr =vgic_v5_ist_attr (NULL ,0 ,NULL ,0 ); + ret =__kvm_device_attr_get (v.gic_fd,KVM_DEV_ARM_VGIC_GRP_IST , +0 , &ist_attr); +TEST_ASSERT (ret && errno ==EINVAL ,"GICv5 IST get accepted missing SPI buffer" ); + + ist_attr =vgic_v5_ist_attr (spi_ist,sizeof (spi_ist) -sizeof (__u32), +NULL ,0 ); + ret =__kvm_device_attr_set (v.gic_fd,KVM_DEV_ARM_VGIC_GRP_IST , +0 , &ist_attr); +TEST_ASSERT (ret && errno ==EINVAL ,"GICv5 IST set accepted bad SPI size" );
[Severity: Medium]
Does this test leave coverage gaps for the set/get paths? For the missing SPI buffer, the test only checks __kvm_device_attr_get(), but neglects __kvm_device_attr_set(). Similarly, for the incorrectly sized SPI buffer, it checks __kvm_device_attr_set() but not __kvm_device_attr_get().
Would it make sense to test both the get and set paths for these validation checks, similar to how the NULL descriptor checks were handled earlier?
tools/testing/selftests/kvm/arm64/vgic_v5.c static void test_vgic_v5_irs_regs_attrs(void) · line 619
Quoted 7 lines
tools/testing/selftests/kvm/arm64/vgic_v5.c +7
-0+ +/* LPI storage must be absent when no LPI IST is configured. */ + ist_attr =vgic_v5_ist_attr (spi_ist,sizeof (spi_ist), spi_ist, +sizeof (__u32)); + ret =__kvm_device_attr_get (v.gic_fd,KVM_DEV_ARM_VGIC_GRP_IST , +0 , &ist_attr); +TEST_ASSERT (ret && errno ==EINVAL ,"GICv5 IST get accepted unexpected LPI buffer" );
[Severity: Medium]
Is this test missing the corresponding check for the set path? As with the SPI buffer tests above, it only validates that __kvm_device_attr_get() rejects the unexpected LPI buffer, but does not check if __kvm_device_attr_set() correctly rejects it as well.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=43
Re: [PATCH v5 43/49] KVM: selftests: Add VGICv5 IST attribute tests
Quoted 63 lines · On Fri, 2026-08-07 at 17:21 +0000, [email protected] wrote:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] Incomplete test coverage for VGICv5 IST attribute validation
----- Patch [43]: [PATCH v5 43/49] KVM: selftests: Add VGICv5 IST attribute tests ---
commit9ba6d67381650763d50c218233de98711d15304f
Author: Sascha Bischoff <[email protected]>KVM: selftests: Add VGICv5 IST attribute tests
This commit adds selftests for the VGICv5 IST attribute group. It verifies
that KVM reports the attribute group correctly and rejects invalid operations,
such as out-of-order save/restore attempts and badly sized IST buffers.Quoted 5 lines
tools/testing/selftests/kvm/arm64/vgic_v5.c +0
-0[ ... ]
Quoted 6 lines
tools/testing/selftests/kvm/arm64/vgic_v5.c +0
-0@@ -523,6 +534,114 @@ static void test_vgic_v5_irs_regs_attrs(void) vm_gic_destroy(&v); }tools/testing/selftests/kvm/arm64/vgic_v5.c +2
-0+static void test_vgic_v5_ist_attrs (void ) +{[ ... ]
Quoted 15 lines
tools/testing/selftests/kvm/arm64/vgic_v5.c +3
-0+/* Check missing and incorrectly sized SPI IST buffers. */ + ist_attr =vgic_v5_ist_attr (NULL ,0 ,NULL ,0 ); + ret = __kvm_device_attr_get(v.gic_fd,
KVM_DEV_ARM_VGIC_GRP_IST,tools/testing/selftests/kvm/arm64/vgic_v5.c +2
-0+ 0, &ist_attr); + TEST_ASSERT(ret && errno == EINVAL, "GICv5 IST getaccepted missing SPI buffer");
tools/testing/selftests/kvm/arm64/vgic_v5.c +2
-0+ + ist_attr = vgic_v5_ist_attr(spi_ist, sizeof(spi_ist) -
sizeof(__u32),tools/testing/selftests/kvm/arm64/vgic_v5.c +2
-0+ NULL, 0); + ret = __kvm_device_attr_set(v.gic_fd,
KVM_DEV_ARM_VGIC_GRP_IST,tools/testing/selftests/kvm/arm64/vgic_v5.c +2
-0+ 0, &ist_attr); + TEST_ASSERT(ret && errno == EINVAL, "GICv5 IST setQuoted 1 line
accepted bad SPI size");
[Severity: Medium]
Does this test leave coverage gaps for the set/get paths? For the missing
SPI buffer, the test only checks__kvm_device_attr_get(), but neglects__kvm_device_attr_set(). Similarly, for the incorrectly sized SPI buffer,
it checks__kvm_device_attr_set()but not__kvm_device_attr_get().
Quoted 4 lines
Would it make sense to test both the get and set paths for these validation
checks, similar to how the NULL descriptor checks were handled earlier?
Both save and restore use the same vgic_v5_copy_ist_attr() and vgic_v5_validate_ist_attr() helpers, so the existing tests collectively cover these cases. However, checking each case through both paths provides better protection against either caller bypassing the common validation in the future. I’ve added the additional get/set cases.
Quoted 13 lines
Quoted 11 lines
tools/testing/selftests/kvm/arm64/vgic_v5.c +2
-0+ +/* LPI storage must be absent when no LPI IST is configured. */
tools/testing/selftests/kvm/arm64/vgic_v5.c +1
-0+ ist_attr = vgic_v5_ist_attr(spi_ist, sizeof(spi_ist),
spi_ist,tools/testing/selftests/kvm/arm64/vgic_v5.c +2
-0+ sizeof(__u32)); + ret = __kvm_device_attr_get(v.gic_fd,
KVM_DEV_ARM_VGIC_GRP_IST,tools/testing/selftests/kvm/arm64/vgic_v5.c +2
-0+ 0, &ist_attr); + TEST_ASSERT(ret && errno == EINVAL, "GICv5 IST getQuoted 1 line
accepted unexpected LPI buffer");
Quoted 8 lines
[Severity: Medium]
Is this test missing the corresponding check for the set path? As with the
SPI buffer tests above, it only validates that__kvm_device_attr_get()
rejects the unexpected LPI buffer, but does not check if__kvm_device_attr_set()correctly rejects it as well.
As above.
Thanks,
Sascha
PATCH v5 44/49 KVM: selftests: Add VGICv5 USERSPACE_PPIS tests
In reply to Sascha BischoffAdd coverage for the VGICv5 USERSPACE_PPIS control attribute. VGICv5 only allows userspace to drive PPIs that KVM explicitly advertises, so the selftest should verify the discovery path and the corresponding KVM_IRQ_LINE rejection cases.
Verify that KVM reports the control attribute and that the returned bitmap contains SW_PPI. Also check that the upper 64 bits are zero, as KVM only exposes architected PPIs (the lower 64 entries). Finally, verify that writes, bad userspace pointers, PPIs outside the advertised bitmap, and the first out-of-range PPI number are rejected.
--- tools/testing/selftests/kvm/arm64/vgic_v5.c | 70 +++++++++++++++++++++ 1 file changed, 70 insertions(+)
tools/testing/selftests/kvm/arm64/vgic_v5.c +70 -0
@@ -13,6 +13,7 @@#include "vgic.h" #define NR_VCPUS 1 +#define VGIC_V5_NR_PRIVATE_IRQS 64#define VGIC_V5_DEFAULT_NR_SPIS 32#define VGIC_V5_MAX_NR_SPIS BIT(16)
@@ -111,6 +112,17 @@ static void vm_gic_destroy(struct vm_gic *v)kvm_vm_free (v->vm); } +static u32 vgic_v5_irq_line_payload (u32 type,u32 num) +{ +return (type <<KVM_ARM_IRQ_TYPE_SHIFT ) | +FIELD_PREP (KVM_ARM_IRQ_NUM_MASK , num); +} + +static int __vgic_v5_irq_line (struct kvm_vm *vm,u32 type,u32 num,int level) +{ +return _kvm_irq_line (vm,vgic_v5_irq_line_payload (type, num), level); +} +struct vgic_region_attr {u64 attr;u64 size;
@@ -642,6 +654,47 @@ static void test_vgic_v5_ist_attrs(void)vm_gic_destroy (&v); } +static void test_vgic_v5_userspace_ppis_attrs (void ) +{ +struct kvm_vcpu *vcpu; +struct vm_gic v; +u64 user_ppis[2 ]; +int ret; + + v.gic_dev_type =KVM_DEV_TYPE_ARM_VGIC_V5 ; + v.vm =__vm_create (VM_SHAPE_DEFAULT ,NR_VCPUS ,0 ); + v.gic_fd =kvm_create_device (v.vm, v.gic_dev_type); + vcpu =vm_vcpu_add (v.vm,0 ,NULL ); +TEST_ASSERT (vcpu,"Failed to create vCPU" ); + +kvm_has_device_attr (v.gic_fd,KVM_DEV_ARM_VGIC_GRP_CTRL , +KVM_DEV_ARM_VGIC_CTRL_INIT ); +kvm_has_device_attr (v.gic_fd,KVM_DEV_ARM_VGIC_GRP_CTRL , +KVM_DEV_ARM_VGIC_USERSPACE_PPIS ); + +kvm_device_attr_set (v.gic_fd,KVM_DEV_ARM_VGIC_GRP_CTRL , +KVM_DEV_ARM_VGIC_CTRL_INIT ,NULL ); + + user_ppis[0 ] =0 ; + user_ppis[1 ] =0xbad ; + ret =__kvm_device_attr_get (v.gic_fd,KVM_DEV_ARM_VGIC_GRP_CTRL , +KVM_DEV_ARM_VGIC_USERSPACE_PPIS , user_ppis); +TEST_ASSERT (!ret,"GICv5 USERSPACE_PPIS get failed" ); +TEST_ASSERT (user_ppis[0 ] &BIT (GICV5_ARCH_PPI_SW_PPI ), +"GICv5 USERSPACE_PPIS does not expose SW_PPI" ); +TEST_ASSERT (!user_ppis[1 ],"GICv5 USERSPACE_PPIS upper word is not zero" ); + + ret =__kvm_device_attr_set (v.gic_fd,KVM_DEV_ARM_VGIC_GRP_CTRL , +KVM_DEV_ARM_VGIC_USERSPACE_PPIS , user_ppis); +TEST_ASSERT (ret && errno ==ENXIO ,"GICv5 USERSPACE_PPIS set accepted" ); + + ret =__kvm_device_attr_get (v.gic_fd,KVM_DEV_ARM_VGIC_GRP_CTRL , +KVM_DEV_ARM_VGIC_USERSPACE_PPIS ,NULL ); +TEST_ASSERT (ret && errno ==EFAULT ,"GICv5 USERSPACE_PPIS get with bad pointer" ); + +vm_gic_destroy (&v); +} +static void test_vgic_v5_ppis (u32 gic_dev_type) {struct kvm_vcpu *vcpus[NR_VCPUS ];
@@ -681,6 +734,20 @@ static void test_vgic_v5_ppis(u32 gic_dev_type)TEST_ASSERT (user_ppis[0 ] &BIT (GICV5_ARCH_PPI_SW_PPI ),"SW_PPI is not drivable by userspace" ); +/* PPIs not explicitly exposed to userspace must be rejected. */ +for (i =0 ; i <VGIC_V5_NR_PRIVATE_IRQS ; i++) { +if (user_ppis[i /64 ] &BIT_ULL (i %64 )) +continue ; + + ret =__vgic_v5_irq_line (v.vm,KVM_ARM_IRQ_TYPE_PPI , i,1 ); +TEST_ASSERT (ret && errno ==EINVAL , +"GICv5 accepted non-userspace PPI %d" , i); + } + + ret =__vgic_v5_irq_line (v.vm,KVM_ARM_IRQ_TYPE_PPI , +VGIC_V5_NR_PRIVATE_IRQS ,1 ); +TEST_ASSERT (ret && errno ==EINVAL ,"GICv5 accepted out-of-range PPI" ); +while (1 ) { ret =run_vcpu (vcpus[0 ]);if (ret)
@@ -767,6 +834,9 @@ void run_tests(u32 gic_dev_type)pr_info ("Test VGICv5 IST attrs\n" );test_vgic_v5_ist_attrs (); +pr_info ("Test VGICv5 userspace PPI attrs\n" ); +test_vgic_v5_userspace_ppis_attrs (); +pr_info ("Test VGICv5 PPIs\n" );test_vgic_v5_ppis (gic_dev_type);
Signature · 1 line
2.34.1
PATCH v5 45/49 KVM: selftests: Add VGICv5 CPU sysreg attribute tests
In reply to Sascha BischoffAdd coverage for the VGICv5 CPU_SYSREGS attribute group. The vCPU-local CPU interface registers are part of the VGICv5 userspace ABI, so KVM must expose the supported registers and reject invalid register or vCPU selectors.
Verify that CPU_SYSREGS access is rejected before VGIC initialization, that all exposed VGICv5 CPU interface registers can be read and written back, and that unsupported sysregs, invalid MPIDRs and bad userspace pointers are rejected.
--- tools/testing/selftests/kvm/arm64/vgic_v5.c | 137 ++++++++++++++++++++ 1 file changed, 137 insertions(+)
tools/testing/selftests/kvm/arm64/vgic_v5.c +137 -0
@@ -135,11 +135,29 @@ static const struct vgic_region_attr gic_v5_irs_region = { .alignment = GICV5_IRS_ALIGN, }; +struct vgic_sysreg_attr { +const char *name; +u32 encoding; +}; +struct vgic_irs_reg_attr {const char *name;u64 attr; }; +#define PACK_SR (r) \ + ((sys_reg_Op0(r) << 14) | \ + (sys_reg_Op1(r) << 11) | \ + (sys_reg_CRn(r) << 7) | \ + (sys_reg_CRm(r) << 3) | \ + (sys_reg_Op2(r))) + +#define SR (r) \ + { \ + .name = #r, \ + .encoding = r, \ + } +#define IRS_REG (r) \ { \ .name = #r, \
@@ -201,6 +219,35 @@ static const struct vgic_irs_reg_attr gic_v5_irs_regs[] = { IRS_REG(GICV5_IRS_SWERR_SYNDROMER1), }; +static const struct vgic_sysreg_attr gic_v5_cpu_sysregs[] = { +SR (SYS_ICC_ICSR_EL1 ), +SR (SYS_ICC_PPI_ENABLER0_EL1 ), +SR (SYS_ICC_PPI_ENABLER1_EL1 ), +SR (SYS_ICC_PPI_SACTIVER0_EL1 ), +SR (SYS_ICC_PPI_SACTIVER1_EL1 ), +SR (SYS_ICC_PPI_SPENDR0_EL1 ), +SR (SYS_ICC_PPI_SPENDR1_EL1 ), +SR (SYS_ICC_PPI_PRIORITYR0_EL1 ), +SR (SYS_ICC_PPI_PRIORITYR1_EL1 ), +SR (SYS_ICC_PPI_PRIORITYR2_EL1 ), +SR (SYS_ICC_PPI_PRIORITYR3_EL1 ), +SR (SYS_ICC_PPI_PRIORITYR4_EL1 ), +SR (SYS_ICC_PPI_PRIORITYR5_EL1 ), +SR (SYS_ICC_PPI_PRIORITYR6_EL1 ), +SR (SYS_ICC_PPI_PRIORITYR7_EL1 ), +SR (SYS_ICC_PPI_PRIORITYR8_EL1 ), +SR (SYS_ICC_PPI_PRIORITYR9_EL1 ), +SR (SYS_ICC_PPI_PRIORITYR10_EL1 ), +SR (SYS_ICC_PPI_PRIORITYR11_EL1 ), +SR (SYS_ICC_PPI_PRIORITYR12_EL1 ), +SR (SYS_ICC_PPI_PRIORITYR13_EL1 ), +SR (SYS_ICC_PPI_PRIORITYR14_EL1 ), +SR (SYS_ICC_PPI_PRIORITYR15_EL1 ), +SR (SYS_ICC_APR_EL1 ), +SR (SYS_ICC_CR0_EL1 ), +SR (SYS_ICC_PCR_EL1 ), +}; +static void test_vgic_v5_addr_attrs (void ) {struct kvm_vcpu *vcpu;
@@ -695,6 +742,93 @@ static void test_vgic_v5_userspace_ppis_attrs(void)vm_gic_destroy (&v); } +static void test_vgic_v5_cpu_sysreg_attrs (void ) +{ +struct kvm_vcpu *vcpu; +struct vm_gic v; +u64 attr, val; +int ret, i; + + v.gic_dev_type =KVM_DEV_TYPE_ARM_VGIC_V5 ; + v.vm =__vm_create (VM_SHAPE_DEFAULT ,NR_VCPUS ,0 ); + v.gic_fd =kvm_create_device (v.vm, v.gic_dev_type); + vcpu =vm_vcpu_add (v.vm,0 ,NULL ); +TEST_ASSERT (vcpu,"Failed to create vCPU" ); + +/* Check existing group/attribute */ + attr =PACK_SR (SYS_ICC_CR0_EL1 ); +kvm_has_device_attr (v.gic_fd,KVM_DEV_ARM_VGIC_GRP_CPU_SYSREGS , attr); + +/* CPU sysregs are not accessible before the VGIC is initialized. */ + ret =__kvm_device_attr_get (v.gic_fd,KVM_DEV_ARM_VGIC_GRP_CPU_SYSREGS , + attr, &val); +TEST_ASSERT (ret && errno ==EBUSY ,"GICv5 CPU_SYSREGS get before init" ); + + val =0 ; + ret =__kvm_device_attr_set (v.gic_fd,KVM_DEV_ARM_VGIC_GRP_CPU_SYSREGS , + attr, &val); +TEST_ASSERT (ret && errno ==EBUSY ,"GICv5 CPU_SYSREGS set before init" ); + +kvm_device_attr_set (v.gic_fd,KVM_DEV_ARM_VGIC_GRP_CTRL , +KVM_DEV_ARM_VGIC_CTRL_INIT ,NULL ); + +/* Read all exposed CPU sysregs and write the value back. */ +for (i =0 ; i <ARRAY_SIZE (gic_v5_cpu_sysregs); i++) { + attr =PACK_SR (gic_v5_cpu_sysregs[i].encoding); + ret =__kvm_has_device_attr (v.gic_fd, +KVM_DEV_ARM_VGIC_GRP_CPU_SYSREGS , + attr); +TEST_ASSERT (!ret,"GICv5 CPU_SYSREGS missing %s" , + gic_v5_cpu_sysregs[i].name); + + val =0xbad ; + ret =__kvm_device_attr_get (v.gic_fd, +KVM_DEV_ARM_VGIC_GRP_CPU_SYSREGS , + attr, &val); +TEST_ASSERT (!ret,"GICv5 CPU_SYSREGS get failed for %s" , + gic_v5_cpu_sysregs[i].name); + + ret =__kvm_device_attr_set (v.gic_fd, +KVM_DEV_ARM_VGIC_GRP_CPU_SYSREGS , + attr, &val); +TEST_ASSERT (!ret,"GICv5 CPU_SYSREGS set failed for %s" , + gic_v5_cpu_sysregs[i].name); + } + +/* Check non existent GICv3 sysreg */ + attr =PACK_SR (SYS_ICC_CTLR_EL1 ); + ret =__kvm_has_device_attr (v.gic_fd,KVM_DEV_ARM_VGIC_GRP_CPU_SYSREGS , + attr); +TEST_ASSERT (ret && errno ==ENXIO ,"GICv5 CPU_SYSREGS accepted bad sysreg" ); + + ret =__kvm_device_attr_get (v.gic_fd,KVM_DEV_ARM_VGIC_GRP_CPU_SYSREGS , + attr, &val); +TEST_ASSERT (ret && errno ==ENOENT ,"GICv5 CPU_SYSREGS get bad sysreg" ); + +/* Check non existing vCPU */ + attr =PACK_SR (SYS_ICC_CR0_EL1 ) | + (1ULL <<KVM_DEV_ARM_VGIC_V3_MPIDR_SHIFT ); + ret =__kvm_has_device_attr (v.gic_fd,KVM_DEV_ARM_VGIC_GRP_CPU_SYSREGS , + attr); +TEST_ASSERT (ret && errno ==EINVAL ,"GICv5 CPU_SYSREGS accepted bad MPIDR" ); + + ret =__kvm_device_attr_get (v.gic_fd,KVM_DEV_ARM_VGIC_GRP_CPU_SYSREGS , + attr, &val); +TEST_ASSERT (ret && errno ==EINVAL ,"GICv5 CPU_SYSREGS get bad MPIDR" ); + +/* Check bad user pointers */ + attr =PACK_SR (SYS_ICC_CR0_EL1 ); + ret =__kvm_device_attr_get (v.gic_fd,KVM_DEV_ARM_VGIC_GRP_CPU_SYSREGS , + attr,NULL ); +TEST_ASSERT (ret && errno ==EFAULT ,"GICv5 CPU_SYSREGS get with bad pointer" ); + + ret =__kvm_device_attr_set (v.gic_fd,KVM_DEV_ARM_VGIC_GRP_CPU_SYSREGS , + attr,NULL ); +TEST_ASSERT (ret && errno ==EFAULT ,"GICv5 CPU_SYSREGS set with bad pointer" ); + +vm_gic_destroy (&v); +} +static void test_vgic_v5_ppis (u32 gic_dev_type) {struct kvm_vcpu *vcpus[NR_VCPUS ];
@@ -837,6 +971,9 @@ void run_tests(u32 gic_dev_type)pr_info ("Test VGICv5 userspace PPI attrs\n" );test_vgic_v5_userspace_ppis_attrs (); +pr_info ("Test VGICv5 CPU sysreg attrs\n" ); +test_vgic_v5_cpu_sysreg_attrs (); +pr_info ("Test VGICv5 PPIs\n" );test_vgic_v5_ppis (gic_dev_type);
Signature · 1 line
2.34.1