]> git.ipfire.org Git - thirdparty/linux.git/commitdiff
KVM: arm64: Bound used_lrs when flushing the pKVM hyp vCPU
authorHyunwoo Kim <imv4bel@gmail.com>
Sat, 6 Jun 2026 17:56:11 +0000 (02:56 +0900)
committerMarc Zyngier <maz@kernel.org>
Sun, 7 Jun 2026 13:07:06 +0000 (14:07 +0100)
flush_hyp_vcpu() copies the host vGIC state into the hyp's private vCPU
on every run. The vGIC list register save and restore use used_lrs as
their loop bound and expect it to stay within the number of implemented
list registers. While this is generally the case, flush_hyp_vcpu()
copies vgic_v3 verbatim and does not enforce this, so a value provided
by the host is used at EL2 to index vgic_lr[] and access ICH_LR<n>_EL2
(host -> EL2).

Fix by clamping used_lrs to the number of implemented list registers
after the copy, as the trusted path already does in
vgic_flush_lr_state(). The number of implemented list registers is
constant after init, so it is replicated once from
kvm_vgic_global_state.nr_lr into hyp_gicv3_nr_lr rather than read on
every entry.

Cc: stable@vger.kernel.org
Fixes: be66e67f1750 ("KVM: arm64: Use the pKVM hyp vCPU structure in handle___kvm_vcpu_run()")
Signed-off-by: Hyunwoo Kim <imv4bel@gmail.com>
Reviewed-by: Fuad Tabba <tabba@google.com>
Tested-by: Fuad Tabba <tabba@google.com>
Link: https://patch.msgid.link/20260606175614.83273-3-imv4bel@gmail.com
Signed-off-by: Marc Zyngier <maz@kernel.org>
arch/arm64/include/asm/kvm_hyp.h
arch/arm64/kvm/arm.c
arch/arm64/kvm/hyp/nvhe/hyp-main.c

index 8d06b62e7188cf08e27a47d2850d5a1ded0e045a..e9b2b0c40ec63ba5846d8df1d132d786efe9b662 100644 (file)
@@ -157,5 +157,6 @@ extern unsigned long kvm_nvhe_sym(__icache_flags);
 extern unsigned int kvm_nvhe_sym(kvm_arm_vmid_bits);
 extern unsigned int kvm_nvhe_sym(kvm_host_sve_max_vl);
 extern unsigned long kvm_nvhe_sym(hyp_nr_cpus);
+extern unsigned int kvm_nvhe_sym(hyp_gicv3_nr_lr);
 
 #endif /* __ARM64_KVM_HYP_H__ */
index 8bb2c7422cc8b0ad1c6952047cbb878af57fcaca..74965b358cdf1877990b116b8f4ce2446430f4ec 100644 (file)
@@ -2423,6 +2423,8 @@ static int __init init_subsystems(void)
        switch (err) {
        case 0:
                vgic_present = true;
+               if (static_branch_unlikely(&kvm_vgic_global_state.gicv3_cpuif))
+                       kvm_nvhe_sym(hyp_gicv3_nr_lr) = kvm_vgic_global_state.nr_lr;
                break;
        case -ENODEV:
        case -ENXIO:
index 02c5d6e5abcbf06acace0e64ee447e8b6ad2ebbe..a0da08caa6c27eee463063c2a6b24ba8bb9ec352 100644 (file)
@@ -24,6 +24,9 @@
 
 DEFINE_PER_CPU(struct kvm_nvhe_init_params, kvm_init_params);
 
+/* Number of implemented GICv3 LRs. Used by flush_hyp_vcpu(). */
+unsigned int hyp_gicv3_nr_lr;
+
 void __kvm_hyp_host_forward_smc(struct kvm_cpu_context *host_ctxt);
 
 static void __hyp_sve_save_guest(struct kvm_vcpu *vcpu)
@@ -142,6 +145,12 @@ static void flush_hyp_vcpu(struct pkvm_hyp_vcpu *hyp_vcpu)
 
        hyp_vcpu->vcpu.arch.vgic_cpu.vgic_v3 = host_vcpu->arch.vgic_cpu.vgic_v3;
 
+       /* Bound used_lrs by the number of implemented list registers. */
+       hyp_vcpu->vcpu.arch.vgic_cpu.vgic_v3.used_lrs =
+               min_t(unsigned int,
+                     hyp_vcpu->vcpu.arch.vgic_cpu.vgic_v3.used_lrs,
+                     hyp_gicv3_nr_lr);
+
        hyp_vcpu->vcpu.arch.pid = host_vcpu->arch.pid;
 }