KVM: arm64: Make ICH_VTR_EL2 accesses an inlined literal
Marc Zyngier
maintainercover KVM: arm64: Make ICH_VTR_EL2 accesses an inlined literal
I did it again.
I was looking at NV traces, and realised that we were trapping a whole lot of accesses to ICH_VTR_EL2. But ICH_VTR_EL2 really is a constant from a hypervisor perspective, and we usually cache this in memory, just like any other ID register.
We do have a variable for this in kvm_vgic_global_state, but we don't map this one at EL2 since 8aaf3f7dce746 ("KVM: arm64: Don't map 'kvm_vgic_global_state' at EL2 with pKVM").
So what can we do? We can revert back to mapping the global state, but that's not a very good idea. Or we can be creative and turn the ICH_VTR_EL2 accesses into a runtime constant patched into the code. Since we already have a bunch of similar things for the GICv3 traps, that's no big deal.
And for consistency, let's kill kvm_vgic_global_state.ich_vtr_el2 altogether, so that there is only a single efficient way to read ICH_VTR_EL2.
As usual, this has a significant impact on deeply nested workloads, a solid 5% reduction in elapsed time for an L3 Linux guest running under nested KVM instances. I expect this to benefit KVM running under other hypervisors as well.
Patches on top of -rc3.
Marc Zyngier (5):
KVM: arm64: vgic-v3: Make vtr_to_* helpers use architectural field
symbols
KVM: arm64: Add a helper providing an inlined literal value for
ICH_VTR_EL2
KVM: arm64: Convert most ICH_VTR_EL2 accesses to inlined literal value
KVM: arm64: vgic-v3: Simplify initial GICv3 configuration sampling
KVM: arm64: vgic-v3: Kill kvm_vgic_global_state.ich_vtr_el2
arch/arm64/kernel/image-vars.h | 1 + arch/arm64/kvm/hyp/vgic-v3-sr.c | 29 ++++++-------- arch/arm64/kvm/nested.c | 3 +- arch/arm64/kvm/vgic-sys-reg-v3.c | 8 ++-- arch/arm64/kvm/vgic/vgic-v3.c | 69 +++++++++++++++++++++++++++----- arch/arm64/kvm/vgic/vgic-v5.c | 5 +-- arch/arm64/kvm/vgic/vgic.h | 39 +++++++++++++----- include/kvm/arm_vgic.h | 2 - 8 files changed, 108 insertions(+), 48 deletions(-)
Signature · 1 line
2.47.3
Marc Zyngier
maintainerPATCH 1/5 KVM: arm64: vgic-v3: Make vtr_to_* helpers use architectural field symbols
vgic-v3-sr.c still contains some hardcoded constants for some of the ICH)VTR_EL2 fields. Bring them into the modern world by using the named symbols from the sysreg file.
--- arch/arm64/kvm/hyp/vgic-v3-sr.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
arch/arm64/kvm/hyp/vgic-v3-sr.c +3 -3
@@ -16,9 +16,9 @@#include "../../vgic/vgic.h" -#define vtr_to_max_lr_idx (v) ((v) & 0xf) -#define vtr_to_nr_pre_bits (v) ((((u32)(v) >> 26) & 7) + 1) -#define vtr_to_nr_apr_regs (v) (1 << (vtr_to_nr_pre_bits(v) - 5)) +#define vtr_to_max_lr_idx (v) FIELD_GET(ICH_VTR_EL2_ListRegs, (v)) +#define vtr_to_nr_pre_bits (v) (FIELD_GET(ICH_VTR_EL2_PREbits, (v)) + 1) +#define vtr_to_nr_apr_regs (v) BIT(vtr_to_nr_pre_bits(v) - 5)u64 __gic_v3_get_lr (unsigned int lr) {
Signature · 1 line
2.47.3
Marc Zyngier
maintainerPATCH 2/5 KVM: arm64: Add a helper providing an inlined literal value for ICH_VTR_EL2
In reply to Marc ZyngierWe already have two ways to deal with ICH_VTR_EL2:
- directly read the system register
- read a cached copy in the vgic global state
Add a third way, in the form of an inlined literal value that will eventually replace all of the above. This literal value is computed at boot time, and patched in the relevant code locations.
--- arch/arm64/kernel/image-vars.h | 1 + arch/arm64/kvm/vgic/vgic-v3.c | 51 ++++++++++++++++++++++++++++++++++ arch/arm64/kvm/vgic/vgic.h | 17 ++++++++++++ 3 files changed, 69 insertions(+)
arch/arm64/kernel/image-vars.h +1 -0
@@ -92,6 +92,7 @@ KVM_NVHE_ALIAS(spectre_bhb_patch_wa3);KVM_NVHE_ALIAS (spectre_bhb_patch_clearbhb);KVM_NVHE_ALIAS (alt_cb_patch_nops);KVM_NVHE_ALIAS (kvm_compute_ich_hcr_trap_bits); +KVM_NVHE_ALIAS (kvm_patch_ich_vtr_el2);/* Global kernel state accessed by nVHE hyp code. */ KVM_NVHE_ALIAS (kvm_vgic_global_state);
arch/arm64/kvm/vgic/vgic-v3.c +51 -0
57 lines not on this page · raw message
arch/arm64/kvm/vgic/vgic.h +17 -0
@@ -180,6 +180,23 @@ static inline u64 vgic_ich_hcr_trap_bits(void)return hcr; } +void kvm_patch_ich_vtr_el2 (struct alt_instr *alt, +__le32 *origptr,__le32 *updptr,int nr_inst); + +static inline u64 vgic_ich_vtr (void ) +{ +u64 vtr; + +/* All non-RES0 bits are in the bottom 32bits */ + asm volatile(ALTERNATIVE_CB("movz %0, #0\n" + "movk %0, #0, lsl #16\n", + ARM64_ALWAYS_SYSTEM, + kvm_patch_ich_vtr_el2) + : "=r" (vtr)); + +return vtr; +} +/* * This struct provides an intermediate representation of the fields contained * in the GICH_VMCR and ICH_VMCR registers, such that code exporting the GIC
Signature · 1 line
2.47.3
[email protected]
AI reviewRe: [PATCH 2/5] KVM: arm64: Add a helper providing an inlined literal value for ICH_VTR_EL2
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [High] The `kvm_patch_ich_vtr_el2()` helper fails to apply the `vgic_v3_broken_seis()` workaround mask to…
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [High] The kvm_patch_ich_vtr_el2() helper fails to apply the vgic_v3_broken_seis() workaround mask to the ICH_VTR_EL2 value.
- [High] The noinstr function kvm_patch_ich_vtr_el2() calls out-of-line instrumentable functions, which violates .noinstr.text safety rules. --
--- Patch [2]: [PATCH 2/5] KVM: arm64: Add a helper providing an inlined literal value for ICH_VTR_EL2 --- commit aedd950f23aaa5667277a7a495b598168c06eb84
Author: Marc Zyngier <[email protected]>
KVM: arm64: Add a helper providing an inlined literal value for ICH_VTR_EL2
This commit adds an alternative patching callback to provide an inlined literal value for the ICH_VTR_EL2 system register. It computes the value at boot time and patches it into the relevant code locations.
Quoted 13 lines
arch/arm64/kvm/vgic/vgic-v3.c +5
-0@@ -876,6 +876,57 @@ void noinstr kvm_compute_ich_hcr_trap_bits(struct alt_instr *alt, *updptr =cpu_to_le32 (insn); } +void noinstr kvm_patch_ich_vtr_el2(struct alt_instr *alt, +__le32 *origptr,__le32 *updptr, +int nr_inst) +{ +struct arm_smccc_res res = {};
arch/arm64/kvm/vgic/vgic-v3.c void noinstr kvm_compute_ich_hcr_trap_bits(struct alt_instr *alt, · line 893
Quoted 10 lines
arch/arm64/kvm/vgic/vgic-v3.c +10
-0+u32 insn, oinsn, rd, vtr; + +/* No KVM? Nothing to do */ +if (!is_hyp_mode_available ()) +return ; + +/* No v3 (or v3 compat)? Nothing to do either */ +if (!this_cpu_has_cap (ARM64_HAS_GICV5_LEGACY ) && + !this_cpu_has_cap (ARM64_HAS_GICV3_CPUIF )) +return ;
[Severity: High]
Is it safe to call out-of-line and instrumentable functions like this_cpu_has_cap() from a noinstr context?
This violates the .noinstr.text safety rules and might expose the alternative patching sequence to ftrace, KASAN, and kprobes, potentially leading to recursive faults.
[ ... ]
arch/arm64/kvm/vgic/vgic-v3.c void noinstr kvm_compute_ich_hcr_trap_bits(struct alt_instr *alt, · line 905
Quoted 2 lines
arch/arm64/kvm/vgic/vgic-v3.c +2
-0+if (WARN_ON (res.a0 ==HVC_STUB_ERR )) +return ;
[Severity: High]
Could WARN_ON() also expand to out-of-line instrumentable trap or warning functions, further violating the noinstr constraints of kvm_patch_ich_vtr_el2()?
arch/arm64/kvm/vgic/vgic-v3.c void noinstr kvm_compute_ich_hcr_trap_bits(struct alt_instr *alt, · line 910
Quoted 5 lines
arch/arm64/kvm/vgic/vgic-v3.c +5
-0+ + vtr = res.a1; + +/* Compute target register */ + oinsn =le32_to_cpu (*origptr);
[Severity: High]
Does this sequence miss the workaround mask for broken locally generated System Error Interrupts (SEI)?
The code reads SYS_ICH_VTR_EL2 and directly bakes it into instructions without applying the vgic_v3_broken_seis() mask to clear the ICH_VTR_EL2_SEIS bit. Affected hardware might incorrectly advertise SEI support as a result.
arch/arm64/kvm/vgic/vgic-v3.c void noinstr kvm_compute_ich_hcr_trap_bits(struct alt_instr *alt, · line 919
Quoted 9 lines
arch/arm64/kvm/vgic/vgic-v3.c +9
-0+ rd =aarch64_insn_decode_register (AARCH64_INSN_REGTYPE_RD , oinsn); + +/* movz rd, #(vtr & 0xffff) */ + insn =aarch64_insn_gen_movewide (rd, + (u16 )vtr, +0 , +AARCH64_INSN_VARIANT_64BIT , +AARCH64_INSN_MOVEWIDE_ZERO ); + *updptr++ =cpu_to_le32 (insn);
[Severity: High]
Like this_cpu_has_cap() above, aarch64_insn_decode_register() and aarch64_insn_gen_movewide() are out-of-line C functions. Should these calls be avoided inside a noinstr patching callback to prevent recursive faulting during instruction patching?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
Marc Zyngier
maintainerRe: [PATCH 2/5] KVM: arm64: Add a helper providing an inlined literal value for ICH_VTR_EL2
Quoted 35 lines · On Mon, 20 Jul 2026 14:46:28 +0100, [email protected] wrote:
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [High] Thekvm_patch_ich_vtr_el2()helper fails to apply thevgic_v3_broken_seis()workaround mask to theICH_VTR_EL2value.
- [High] Thenoinstrfunctionkvm_patch_ich_vtr_el2()calls out-of-line instrumentable functions, which violates.noinstr.textsafety rules. ----- Patch [2]: [PATCH 2/5] KVM: arm64: Add a helper providing an inlined literal value for
ICH_VTR_EL2--- commitaedd950f23aaa5667277a7a495b598168c06eb84
Author: Marc Zyngier <[email protected]>KVM: arm64: Add a helper providing an inlined literal value for
ICH_VTR_EL2This commit adds an alternative patching callback to provide an inlined literal value for the
ICH_VTR_EL2system register. It computes the value at boot time and patches it into the relevant code locations.Quoted 19 lines
arch/arm64/kvm/vgic/vgic-v3.c +11
-0@@ -876,6 +876,57 @@ void noinstr kvm_compute_ich_hcr_trap_bits(struct alt_instr *alt, *updptr =cpu_to_le32 (insn); } +void noinstr kvm_patch_ich_vtr_el2(struct alt_instr *alt, +__le32 *origptr,__le32 *updptr, +int nr_inst) +{ +struct arm_smccc_res res = {}; +u32 insn, oinsn, rd, vtr; + +/* No KVM? Nothing to do */ +if (!is_hyp_mode_available ()) +return ; +
Quoted 8 lines
arch/arm64/kvm/vgic/vgic-v3.c void noinstr kvm_compute_ich_hcr_trap_bits(struct alt_instr *alt, · line 893
Quoted 4 lines
arch/arm64/kvm/vgic/vgic-v3.c +4
-0+/* No v3 (or v3 compat)? Nothing to do either */ +if (!this_cpu_has_cap (ARM64_HAS_GICV5_LEGACY ) && + !this_cpu_has_cap (ARM64_HAS_GICV3_CPUIF )) +return ;[Severity: High]
Is it safe to call out-of-line and instrumentable functions likethis_cpu_has_cap()from a noinstr context?
Yeah, that's borderline stupid. This should be "cpus_have_cap()".
Quoted 7 lines
This violates the .noinstr.text safety rules and might expose the alternative patching sequence to ftrace, KASAN, and kprobes, potentially leading to recursive faults.
[ ... ]
Quoted 7 lines
arch/arm64/kvm/vgic/vgic-v3.c void noinstr kvm_compute_ich_hcr_trap_bits(struct alt_instr *alt, · line 905
Quoted 2 lines
arch/arm64/kvm/vgic/vgic-v3.c +2
-0+if (WARN_ON (res.a0 ==HVC_STUB_ERR )) +return ;[Severity: High]
CouldWARN_ON()also expand to out-of-line instrumentable trap or warning functions, further violating the noinstr constraints ofkvm_patch_ich_vtr_el2()?
I guess that if that fails, we will already have failed much earlier, while probing for TDIR. I'll drop that.
Quoted 10 lines
arch/arm64/kvm/vgic/vgic-v3.c void noinstr kvm_compute_ich_hcr_trap_bits(struct alt_instr *alt, · line 910
Quoted 6 lines
arch/arm64/kvm/vgic/vgic-v3.c +5
-0+ + vtr = res.a1; + +/* Compute target register */ + oinsn =le32_to_cpu (*origptr);[Severity: High]
Does this sequence miss the workaround mask for broken locally generated System Error Interrupts (SEI)?
It isn't missing. It is just that the relevant hunk is in the wrong patch and needs to be moved here.
Quoted 6 lines
The code reads
SYS_ICH_VTR_EL2and directly bakes it into instructions without applying thevgic_v3_broken_seis()mask to clear theICH_VTR_EL2_SEISbit. Affected hardware might incorrectly advertise SEI support as a result.
Quoted 15 lines
arch/arm64/kvm/vgic/vgic-v3.c void noinstr kvm_compute_ich_hcr_trap_bits(struct alt_instr *alt, · line 919
Quoted 9 lines
arch/arm64/kvm/vgic/vgic-v3.c +9
-0+ rd =aarch64_insn_decode_register (AARCH64_INSN_REGTYPE_RD , oinsn); + +/* movz rd, #(vtr & 0xffff) */ + insn =aarch64_insn_gen_movewide (rd, + (u16 )vtr, +0 , +AARCH64_INSN_VARIANT_64BIT , +AARCH64_INSN_MOVEWIDE_ZERO ); + *updptr++ =cpu_to_le32 (insn);[Severity: High]
Likethis_cpu_has_cap()above,aarch64_insn_decode_register()andaarch64_insn_gen_movewide()are out-of-line C functions. Should these calls be avoided inside a noinstr patching callback to prevent recursive faulting during instruction patching?
That's something that needs to be separately addressed. There was some work from Ada a long while ago that needs to be brought back to life.
M.
Signature · 1 line
Without deviation from the norm, progress is not possible.
Marc Zyngier
maintainerPATCH 2/5 KVM: arm64: Add a helper providing an inlined litteral value for ICH_VTR_EL2
In reply to Marc ZyngierWe already have two ways to deal with ICH_VTR_EL2:
- directly read the system register
- read a cached copy in the vgic global state
Add a third way, in the form of an inlined litteral value that will eventually replace all of the above. This litteral value is computed at boot time, and patched in the relevant code locations.
--- arch/arm64/kernel/image-vars.h | 1 + arch/arm64/kvm/vgic/vgic-v3.c | 51 ++++++++++++++++++++++++++++++++++ arch/arm64/kvm/vgic/vgic.h | 17 ++++++++++++ 3 files changed, 69 insertions(+)
arch/arm64/kernel/image-vars.h +1 -0
@@ -92,6 +92,7 @@ KVM_NVHE_ALIAS(spectre_bhb_patch_wa3);KVM_NVHE_ALIAS (spectre_bhb_patch_clearbhb);KVM_NVHE_ALIAS (alt_cb_patch_nops);KVM_NVHE_ALIAS (kvm_compute_ich_hcr_trap_bits); +KVM_NVHE_ALIAS (kvm_patch_ich_vtr_el2);/* Global kernel state accessed by nVHE hyp code. */ KVM_NVHE_ALIAS (kvm_vgic_global_state);
arch/arm64/kvm/vgic/vgic-v3.c +51 -0
57 lines not on this page · raw message
arch/arm64/kvm/vgic/vgic.h +17 -0
@@ -180,6 +180,23 @@ static inline u64 vgic_ich_hcr_trap_bits(void)return hcr; } +void kvm_patch_ich_vtr_el2 (struct alt_instr *alt, +__le32 *origptr,__le32 *updptr,int nr_inst); + +static inline u64 vgic_ich_vtr (void ) +{ +u64 vtr; + +/* All non-RES0 bits are in the bottom 32bits */ + asm volatile(ALTERNATIVE_CB("movz %0, #0\n" + "movk %0, #0, lsl #16\n", + ARM64_ALWAYS_SYSTEM, + kvm_patch_ich_vtr_el2) + : "=r" (vtr)); + +return vtr; +} +/* * This struct provides an intermediate representation of the fields contained * in the GICH_VMCR and ICH_VMCR registers, such that code exporting the GIC
Signature · 1 line
2.47.3
Marc Zyngier
maintainerPATCH 3/5 KVM: arm64: Convert most ICH_VTR_EL2 accesses to inlined literal value
In reply to Marc ZyngierNow that we have a new helper that inlines the value of ICH_VTR_EL2 into the code, use this to replace most of the sysreg accesses to that register.
This avoids a lot of traps when running KVM under NV.
--- arch/arm64/kvm/hyp/vgic-v3-sr.c | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-)
arch/arm64/kvm/hyp/vgic-v3-sr.c +9 -9
@@ -367,7 +367,7 @@ void __vgic_v3_save_aprs(struct vgic_v3_cpu_if *cpu_if)u64 val;u32 nr_pre_bits; - val =read_gicreg (ICH_VTR_EL2 ); + val =vgic_ich_vtr (); nr_pre_bits =vtr_to_nr_pre_bits (val);switch (nr_pre_bits) {
@@ -400,7 +400,7 @@ static void __vgic_v3_restore_aprs(struct vgic_v3_cpu_if *cpu_if)u64 val;u32 nr_pre_bits; - val =read_gicreg (ICH_VTR_EL2 ); + val =vgic_ich_vtr (); nr_pre_bits =vtr_to_nr_pre_bits (val);switch (nr_pre_bits) {
@@ -430,7 +430,7 @@ static void __vgic_v3_restore_aprs(struct vgic_v3_cpu_if *cpu_if)void __vgic_v3_init_lrs (void ) { -int max_lr_idx =vtr_to_max_lr_idx (read_gicreg (ICH_VTR_EL2 )); +int max_lr_idx =vtr_to_max_lr_idx (vgic_ich_vtr ());int i;for (i =0 ; i <= max_lr_idx; i++)
@@ -455,7 +455,7 @@ u64 __vgic_v3_get_gic_config(void)* system, so we first check if we have GICv5 support. */ if (cpus_have_final_cap (ARM64_HAS_GICV5_CPUIF )) -return read_gicreg (ICH_VTR_EL2 ); +return vgic_ich_vtr (); sre =read_gicreg (ICC_SRE_EL1 );/*
@@ -498,7 +498,7 @@ u64 __vgic_v3_get_gic_config(void) } val = (val &ICC_SRE_EL1_SRE ) ?0 : (1ULL <<63 ); - val |=read_gicreg (ICH_VTR_EL2 ); + val |=vgic_ich_vtr ();return val; }
@@ -540,7 +540,7 @@ void __vgic_v3_restore_vmcr_aprs(struct vgic_v3_cpu_if *cpu_if)static int __vgic_v3_bpr_min (void ) {/* See Pseudocode for VPriorityGroup */ -return 8 -vtr_to_nr_pre_bits (read_gicreg (ICH_VTR_EL2 )); +return 8 -vtr_to_nr_pre_bits (vgic_ich_vtr ()); }static int __vgic_v3_get_group (struct kvm_vcpu *vcpu)
@@ -614,7 +614,7 @@ static int __vgic_v3_find_active_lr(struct kvm_vcpu *vcpu, int intid,static int __vgic_v3_get_highest_active_priority (void ) { -u8 nr_apr_regs =vtr_to_nr_apr_regs (read_gicreg (ICH_VTR_EL2 )); +u8 nr_apr_regs =vtr_to_nr_apr_regs (vgic_ich_vtr ());u32 hap =0 ;int i;
@@ -707,7 +707,7 @@ static void __vgic_v3_set_active_priority(u8 pri, u32 vmcr, int grp)static int __vgic_v3_clear_highest_active_priority (void ) { -u8 nr_apr_regs =vtr_to_nr_apr_regs (read_gicreg (ICH_VTR_EL2 )); +u8 nr_apr_regs =vtr_to_nr_apr_regs (vgic_ich_vtr ());u32 hap =0 ;int i;
@@ -1039,7 +1039,7 @@ static void __vgic_v3_read_ctlr(struct kvm_vcpu *vcpu, u32 vmcr, int rt) {u32 vtr, val; - vtr =read_gicreg (ICH_VTR_EL2 ); + vtr =vgic_ich_vtr ();/* PRIbits */ val = ((vtr >>29 ) &7 ) <<ICC_CTLR_EL1_PRI_BITS_SHIFT ;/* IDbits */
Signature · 1 line
2.47.3
Marc Zyngier
maintainerPATCH 4/5 KVM: arm64: vgic-v3: Simplify initial GICv3 configuration sampling
In reply to Marc ZyngierNow that we have our magic inline helper for ICH_VTR_EL2, we can get rid of the hack that was reporting a combination of that register and of the indication of the CPU interface supporting GICv2 compatibility. We now only report the latter.
--- arch/arm64/kvm/hyp/vgic-v3-sr.c | 9 ++------- arch/arm64/kvm/vgic/vgic-v3.c | 13 +++++++------ arch/arm64/kvm/vgic/vgic-v5.c | 2 +- 3 files changed, 10 insertions(+), 14 deletions(-)
arch/arm64/kvm/hyp/vgic-v3-sr.c +2 -7
@@ -439,8 +439,6 @@ void __vgic_v3_init_lrs(void)/* * Return the GIC CPU configuration: -* - [31:0] ICH_VTR_EL2 -* - [62:32] RES0 * - [63] MMIO (GICv2) capable */ u64 __vgic_v3_get_gic_config (void )
@@ -455,7 +453,7 @@ u64 __vgic_v3_get_gic_config(void)* system, so we first check if we have GICv5 support. */ if (cpus_have_final_cap (ARM64_HAS_GICV5_CPUIF )) -return vgic_ich_vtr (); +return 0 ; sre =read_gicreg (ICC_SRE_EL1 );/*
@@ -497,10 +495,7 @@ u64 __vgic_v3_get_gic_config(void)isb (); } - val = (val &ICC_SRE_EL1_SRE ) ?0 : (1ULL <<63 ); - val |=vgic_ich_vtr (); - -return val; +return (val &ICC_SRE_EL1_SRE ) ?0 : (1ULL <<63 ); }static void __vgic_v3_compat_mode_enable (void )
arch/arm64/kvm/vgic/vgic-v3.c +7 -6
@@ -906,6 +906,9 @@ void noinstr kvm_patch_ich_vtr_el2(struct alt_instr *alt, vtr = res.a1; +if (vgic_v3_broken_seis ()) + vtr &= ~ICH_VTR_EL2_SEIS ; +/* Compute target register */ oinsn =le32_to_cpu (*origptr); rd =aarch64_insn_decode_register (AARCH64_INSN_REGTYPE_RD , oinsn);
@@ -950,12 +953,12 @@ void vgic_v3_enable_cpuif_traps(void)*/ int vgic_v3_probe (const struct gic_kvm_info *info) { -u64 ich_vtr_el2 =kvm_call_hyp_ret (__vgic_v3_get_gic_config); +u64 ich_vtr_el2;bool has_v2;int ret; - has_v2 = ich_vtr_el2 >>63 ; - ich_vtr_el2 = (u32 )ich_vtr_el2; + has_v2 =kvm_call_hyp_ret (__vgic_v3_get_gic_config) >>63 ; + ich_vtr_el2 =vgic_ich_vtr ();/* * The ListRegs field is 5 bits, but there is an architectural
@@ -1010,10 +1013,8 @@ int vgic_v3_probe(const struct gic_kvm_info *info)if (has_v2)static_branch_enable (&vgic_v3_has_v2_compat); -if (vgic_v3_broken_seis ()) { +if (vgic_v3_broken_seis ())kvm_info ("GICv3 with broken locally generated SEI\n" ); - kvm_vgic_global_state.ich_vtr_el2 &= ~ICH_VTR_EL2_SEIS ; - }vgic_v3_enable_cpuif_traps ();
arch/arm64/kvm/vgic/vgic-v5.c +1 -1
@@ -83,7 +83,7 @@ int vgic_v5_probe(const struct gic_kvm_info *info) } kvm_vgic_global_state.has_gcie_v3_compat = true; - ich_vtr_el2 =kvm_call_hyp_ret (__vgic_v3_get_gic_config); + ich_vtr_el2 =vgic_ich_vtr (); kvm_vgic_global_state.ich_vtr_el2 = (u32 )ich_vtr_el2;/*
Signature · 1 line
2.47.3
[email protected]
AI reviewRe: [PATCH 4/5] KVM: arm64: vgic-v3: Simplify initial GICv3 configuration sampling
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [Medium] The `noinstr` function `kvm_patch_ich_vtr_el2()` calls a non-`noinstr` function…
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [Medium] The noinstr function kvm_patch_ich_vtr_el2() calls a non-noinstr function (vgic_v3_broken_seis()), causing a noinstr section violation.
- [Medium] WARN_ON inside the noinstr alternative patching callback kvm_patch_ich_vtr_el2() violates instrumentation rules and risks deadlocks. --
--- Patch [4]: [PATCH 4/5] KVM: arm64: vgic-v3: Simplify initial GICv3 configuration sampling --- commit bf26cd3d96184399493f95667d1786d9b2823890
Author: Marc Zyngier <[email protected]>
KVM: arm64: vgic-v3: Simplify initial GICv3 configuration sampling
This commit simplifies the initial GICv3 configuration sampling by returning early if GICv5 CPU interface support is present. It also inlines a workaround for a broken locally generated SEI directly into the alternative patching callback.
[ ... ]
arch/arm64/kvm/vgic/vgic-v3.c void noinstr kvm_patch_ich_vtr_el2(struct alt_instr *alt, · line 910
Quoted 10 lines
arch/arm64/kvm/vgic/vgic-v3.c +2
-0@@ -906,6 +906,9 @@ void noinstr kvm_patch_ich_vtr_el2(struct alt_instr *alt, vtr = res.a1; +if (vgic_v3_broken_seis ()) + vtr &= ~ICH_VTR_EL2_SEIS ;
[Severity: Medium]
Does this code call an instrumentable function from within a noinstr section?
Looking at vgic_v3_broken_seis(), it calls is_midr_in_range_list(). Calling an instrumentable function from a noinstr function like kvm_patch_ich_vtr_el2() breaks safety invariants, which can result in objtool build warnings or ftrace/kprobe recursion deadlocks if the patching code is traced.
[Severity: Medium]
Also, just before this addition in kvm_patch_ich_vtr_el2(), there is a WARN_ON call:
if (WARN_ON(res.a0 == HVC_STUB_ERR)) return; vtr = res.a1;
Is it safe to use WARN_ON inside a noinstr alternative patching callback?
Alternative patching callbacks like kvm_patch_ich_vtr_el2() typically run under stop_machine() with CPUs parked and interrupts disabled. If WARN_ON triggers, it invokes the warning exception machinery which can attempt to acquire console locks. If a parked CPU holds these locks, it could cause a hard deadlock. Additionally, WARN_ON expands to instrumentable paths, directly violating noinstr requirements.
Quoted 4 lines
arch/arm64/kvm/vgic/vgic-v3.c +1
-0+/* Compute target register */ oinsn =le32_to_cpu (*origptr); rd =aarch64_insn_decode_register (AARCH64_INSN_REGTYPE_RD , oinsn);
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
Marc Zyngier
maintainerRe: [PATCH 4/5] KVM: arm64: vgic-v3: Simplify initial GICv3 configuration sampling
Quoted 4 lines · On Mon, 20 Jul 2026 14:38:26 +0100, [email protected] wrote:
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [Medium] Thenoinstrfunctionkvm_patch_ich_vtr_el2()calls a non-noinstrfunction (vgic_v3_broken_seis()), causing anoinstrsection violation.
- [Medium]WARN_ONinside thenoinstralternative patching callbackkvm_patch_ich_vtr_el2()violates instrumentation rules and risks deadlocks.
[...]
See response to patch #2.
M.
Signature · 1 line
Without deviation from the norm, progress is not possible.
Marc Zyngier
maintainerPATCH 5/5 KVM: arm64: vgic-v3: Kill kvm_vgic_global_state.ich_vtr_el2
In reply to Marc Zyngierkvm_vgic_global_state.ich_vtr_el2 is the last bit of caching that we can get rid of. Not as bad as a sysreg access, but still worse than a constant.
Move over to the inlined stuff and remove the cached value.
--- arch/arm64/kvm/nested.c | 3 ++- arch/arm64/kvm/vgic-sys-reg-v3.c | 8 ++++---- arch/arm64/kvm/vgic/vgic-v3.c | 5 ++--- arch/arm64/kvm/vgic/vgic-v5.c | 5 +---- arch/arm64/kvm/vgic/vgic.h | 22 +++++++++++----------- include/kvm/arm_vgic.h | 2 -- 6 files changed, 20 insertions(+), 25 deletions(-)
arch/arm64/kvm/nested.c +2 -1
@@ -16,6 +16,7 @@#include <asm/sysreg.h> #include "sys_regs.h" +#include "vgic/vgic.h" struct vncr_tlb {/* The guest's VNCR_EL2 */
@@ -1904,7 +1905,7 @@ int kvm_init_nv_sysregs(struct kvm_vcpu *vcpu)/* ICH_HCR_EL2 */ resx.res0 =ICH_HCR_EL2_RES0 ; resx.res1 =ICH_HCR_EL2_RES1 ; -if (!(kvm_vgic_global_state.ich_vtr_el2 &ICH_VTR_EL2_TDS )) +if (!(vgic_ich_vtr () &ICH_VTR_EL2_TDS )) resx.res0 |=ICH_HCR_EL2_TDIR ;/* No GICv4 is presented to the guest */ resx.res0 |=ICH_HCR_EL2_DVIM | ICH_HCR_EL2_vSGIEOICount;
arch/arm64/kvm/vgic-sys-reg-v3.c +4 -4
@@ -35,12 +35,12 @@ static int set_gic_ctlr(struct kvm_vcpu *vcpu, const struct sys_reg_desc *r, vgic_v3_cpu->num_id_bits = host_id_bits; - host_seis =FIELD_GET (ICH_VTR_EL2_SEIS , kvm_vgic_global_state.ich_vtr_el2); + host_seis =FIELD_GET (ICH_VTR_EL2_SEIS ,vgic_ich_vtr ()); seis =FIELD_GET (ICC_CTLR_EL1_SEIS_MASK , val);if (host_seis != seis)return -EINVAL ; - host_a3v =FIELD_GET (ICH_VTR_EL2_A3V , kvm_vgic_global_state.ich_vtr_el2); + host_a3v =FIELD_GET (ICH_VTR_EL2_A3V ,vgic_ich_vtr ()); a3v =FIELD_GET (ICC_CTLR_EL1_A3V_MASK , val);if (host_a3v != a3v)return -EINVAL ;
@@ -69,9 +69,9 @@ static int get_gic_ctlr(struct kvm_vcpu *vcpu, const struct sys_reg_desc *r, val |=FIELD_PREP (ICC_CTLR_EL1_ID_BITS_MASK , vgic_v3_cpu->num_id_bits); val |=FIELD_PREP (ICC_CTLR_EL1_SEIS_MASK ,FIELD_GET (ICH_VTR_EL2_SEIS , - kvm_vgic_global_state.ich_vtr_el2)); +vgic_ich_vtr ())); val |=FIELD_PREP (ICC_CTLR_EL1_A3V_MASK , -FIELD_GET (ICH_VTR_EL2_A3V , kvm_vgic_global_state.ich_vtr_el2)); +FIELD_GET (ICH_VTR_EL2_A3V ,vgic_ich_vtr ()));/* * The VMCR.CTLR value is in ICC_CTLR_EL1 layout. * Extract it directly using ICC_CTLR_EL1 reg definitions.
arch/arm64/kvm/vgic/vgic-v3.c +2 -3
@@ -490,9 +490,9 @@ void vgic_v3_reset(struct kvm_vcpu *vcpu) } vcpu->arch.vgic_cpu.num_id_bits =FIELD_GET (ICH_VTR_EL2_IDbits, - kvm_vgic_global_state.ich_vtr_el2); +vgic_ich_vtr ()); vcpu->arch.vgic_cpu.num_pri_bits =FIELD_GET (ICH_VTR_EL2_PRIbits, - kvm_vgic_global_state.ich_vtr_el2) +1 ; +vgic_ich_vtr ()) +1 ; }void vcpu_set_ich_hcr (struct kvm_vcpu *vcpu)
@@ -966,7 +966,6 @@ int vgic_v3_probe(const struct gic_kvm_info *info)*/ kvm_vgic_global_state.nr_lr = (ich_vtr_el2 &0xf ) +1 ; kvm_vgic_global_state.can_emulate_gicv2 = false; - kvm_vgic_global_state.ich_vtr_el2 = ich_vtr_el2;/* GICv4 support? */ if (info->has_v4) {
arch/arm64/kvm/vgic/vgic-v5.c +1 -4
@@ -40,7 +40,6 @@ static void vgic_v5_get_implemented_ppis(void)int vgic_v5_probe (const struct gic_kvm_info *info) {bool v5_registered = false; -u64 ich_vtr_el2;int ret; kvm_vgic_global_state.type =VGIC_V5 ;
@@ -83,14 +82,12 @@ int vgic_v5_probe(const struct gic_kvm_info *info) } kvm_vgic_global_state.has_gcie_v3_compat = true; - ich_vtr_el2 =vgic_ich_vtr (); - kvm_vgic_global_state.ich_vtr_el2 = (u32 )ich_vtr_el2;/* * The ListRegs field is 5 bits, but there is an architectural * maximum of 16 list registers. Just ignore bit 4... */ - kvm_vgic_global_state.nr_lr = (ich_vtr_el2 &0xf ) +1 ; + kvm_vgic_global_state.nr_lr = (vgic_ich_vtr () &0xf ) +1 ; ret =kvm_register_vgic_device (KVM_DEV_TYPE_ARM_VGIC_V3 );if (ret) {
arch/arm64/kvm/vgic/vgic.h +11 -11
@@ -71,17 +71,6 @@ ICH_VTR_EL2_IDbits)#define KVM_ICH_VTR_EL2_RES1 ICH_VTR_EL2_nV4 -static inline u64 kvm_get_guest_vtr_el2 (void ) -{ -u64 vtr; - - vtr = kvm_vgic_global_state.ich_vtr_el2; - vtr &= ~KVM_ICH_VTR_EL2_RES0 ; - vtr |=KVM_ICH_VTR_EL2_RES1 ; - -return vtr; -} -/* * As per Documentation/virt/kvm/devices/arm-vgic-its.rst, * below macros are defined for ITS table entry encoding.
@@ -197,6 +186,17 @@ static inline u64 vgic_ich_vtr(void)return vtr; } +static inline u64 kvm_get_guest_vtr_el2 (void ) +{ +u64 vtr; + + vtr =vgic_ich_vtr (); + vtr &= ~KVM_ICH_VTR_EL2_RES0 ; + vtr |=KVM_ICH_VTR_EL2_RES1 ; + +return vtr; +} +/* * This struct provides an intermediate representation of the fields contained * in the GICH_VMCR and ICH_VMCR registers, such that code exporting the GIC
include/kvm/arm_vgic.h +0 -2
@@ -176,8 +176,6 @@ struct vgic_global {/* GICv3 compat mode on a GICv5 host */ bool has_gcie_v3_compat; -u32 ich_vtr_el2; -/* GICv5 PPI capabilities */ struct { DECLARE_BITMAP(impl_ppi_mask, VGIC_V5_NR_PRIVATE_IRQS);
Signature · 1 line
2.47.3