[PATCH v3 2/2] RISC-V KVM: Remove unnecessary HGATP csr_read

Guo Ren guoren at kernel.org
Wed Aug 20 18:13:47 PDT 2025


On Wed, Aug 20, 2025 at 10:40 PM Troy Mitchell <troy.mitchell at linux.dev> wrote:
>
> On Wed, Aug 20, 2025 at 08:59:52PM +0800, fangyu.yu at linux.alibaba.com wrote:
> > From: "Guo Ren (Alibaba DAMO Academy)" <guoren at kernel.org>
> >
> > The HGATP has been set to zero in gstage_mode_detect(), so there
> > is no need to save the old context. Unify the code convention
> > with gstage_mode_detect().
> >
> > Signed-off-by: Guo Ren (Alibaba DAMO Academy) <guoren at kernel.org>
> > Signed-off-by: Fangyu Yu <fangyu.yu at linux.alibaba.com>
> > ---
> >  arch/riscv/kvm/vmid.c | 5 +----
> >  1 file changed, 1 insertion(+), 4 deletions(-)
> >
> > diff --git a/arch/riscv/kvm/vmid.c b/arch/riscv/kvm/vmid.c
> > index 5f33625f4070..abb1c2bf2542 100644
> > --- a/arch/riscv/kvm/vmid.c
> > +++ b/arch/riscv/kvm/vmid.c
> > @@ -25,15 +25,12 @@ static DEFINE_SPINLOCK(vmid_lock);
> >
> >  void __init kvm_riscv_gstage_vmid_detect(void)
> >  {
> > -     unsigned long old;
> > -
> >       /* Figure-out number of VMID bits in HW */
> > -     old = csr_read(CSR_HGATP);
> >       csr_write(CSR_HGATP, (kvm_riscv_gstage_mode << HGATP_MODE_SHIFT) | HGATP_VMID);
> >       vmid_bits = csr_read(CSR_HGATP);
> >       vmid_bits = (vmid_bits & HGATP_VMID) >> HGATP_VMID_SHIFT;
> >       vmid_bits = fls_long(vmid_bits);
> > -     csr_write(CSR_HGATP, old);
> > +     csr_write(CSR_HGATP, 0);
> Is setting HGATP to 0 in gstage_mode_detect meaningless now?
It's not meaningless, it means keep hgatp off. CSR_HGATP is set to 0
by gstage_mode_detect(), but that's another independent function whose
coincidence is set to 0. And in that function, we use
"csr_write(CSR_HGATP, 0)", but here we change to confusing save_old &
restore_old coding convention?

So, keep kvm_riscv_gstage_vmid_detect() coding convention clear. We
should follow the same approach as gstage_mode_detect(), that is,
"csr_write(CSR_HGATP, 0)".

 - The first patch is focused on fixing meaning, so keep the minimum
modifications, no coding convention cleanup.
 - The second one is about coding convention, which is no need to Cc:
stable at kernel.org

Hope the above explanation makes sense.

> If so, it might be better to drop it and just keep the one here.
>
>                 - Troy
>
> >
> >       /* We polluted local TLB so flush all guest TLB */
> >       kvm_riscv_local_hfence_gvma_all();
> > --
> > 2.49.0
> >
> >
> > _______________________________________________
> > linux-riscv mailing list
> > linux-riscv at lists.infradead.org
> > http://lists.infradead.org/mailman/listinfo/linux-riscv



-- 
Best Regards
 Guo Ren



More information about the kvm-riscv mailing list