[PATCH v6 03/39] KVM: arm64: Return early from kvm_finalize_sys_regs() if guest has run

Sascha Bischoff Sascha.Bischoff at arm.com
Thu Mar 19 04:41:45 PDT 2026


On Thu, 2026-03-19 at 10:12 +0000, Jonathan Cameron wrote:
> On Tue, 17 Mar 2026 11:40:44 +0000
> Sascha Bischoff <Sascha.Bischoff at arm.com> wrote:
> 
> > If the guest has already run, we have no business finalizing the
> > system register state - it is too late. Therefore, check early and
> > bail if the VM has already run.
> Given it isn't in the scope below, might be worth calling out that
> this is skipping kvm_init_nv_sysregs()  So on non NV setups isn't
> changing anything but on those it's indeed skipping setup of system
> registers.

I'll add that to the description to make it clearer, thanks!

> 
> Seems correct to me, but is this a fix?  So should it have a fixes
> tag?

I'm not sure it is a fix as such - it is more of an optimisation. While
the call to kvm_init_nv_sysregs() was previously happening irrespective
of the VM having run or not, it was not actually doing anything as kvm-
>arch.sysreg_masks is non-null the second time it is called, and hence
returned early.

Now we just skip calling it altogether if it has already run once.

Thanks,
Sascha

> 
> 
> > 
> > Signed-off-by: Sascha Bischoff <sascha.bischoff at arm.com>
> > ---
> >  arch/arm64/kvm/sys_regs.c | 5 ++++-
> >  1 file changed, 4 insertions(+), 1 deletion(-)
> > 
> > diff --git a/arch/arm64/kvm/sys_regs.c b/arch/arm64/kvm/sys_regs.c
> > index 0acd10e50aaba..42c84b7900ff5 100644
> > --- a/arch/arm64/kvm/sys_regs.c
> > +++ b/arch/arm64/kvm/sys_regs.c
> > @@ -5659,11 +5659,14 @@ int kvm_finalize_sys_regs(struct kvm_vcpu
> > *vcpu)
> >  
> >  	guard(mutex)(&kvm->arch.config_lock);
> >  
> > +	if (kvm_vm_has_ran_once(kvm))
> > +		return 0;
> > +
> >  	/*
> >  	 * This hacks into the ID registers, so only perform it
> > when the
> >  	 * first vcpu runs, or the kvm_set_vm_id_reg() helper will
> > scream.
> >  	 */
> > -	if (!irqchip_in_kernel(kvm) && !kvm_vm_has_ran_once(kvm))
> > {
> > +	if (!irqchip_in_kernel(kvm)) {
> >  		u64 val;
> >  
> >  		val = kvm_read_vm_id_reg(kvm, SYS_ID_AA64PFR0_EL1)
> > & ~ID_AA64PFR0_EL1_GIC;
> 



More information about the linux-arm-kernel mailing list