Re: [PATCH] RISC-V: KVM: Allow to downgrade HGATP mode via SATP mode

Guo Ren
2025-11-23T02:33:23Z

On Sat, Nov 22, 2025 at 3:50 PM <fangyu.yu@linux.alibaba.com> wrote:
>
> From: Fangyu Yu <fangyu.yu@linux.alibaba.com>
>
> Currently, HGATP mode uses the maximum value detected by the hardware
> but often such a wide GPA is unnecessary, just as a host sometimes
> doesn't need sv57.
> It's likely that no additional parameters (like no5lvl and no4lvl) are
> needed, aligning HGATP mode to SATP mode should meet the requirements
> of most scenarios.
Yes, no5/4lvl is not clear about satp or hgatp. So, covering HGPATP is
reasonable.

Acked-by: Guo Ren <guoren@kernel.org>

>
> Signed-off-by: Fangyu Yu <fangyu.yu@linux.alibaba.com>
> ---
> arch/riscv/kvm/gstage.c | 7 ++++---
> 1 file changed, 4 insertions(+), 3 deletions(-)
>
> diff --git a/arch/riscv/kvm/gstage.c b/arch/riscv/kvm/gstage.c
> index b67d60d722c2..bff80c80ead3 100644
> --- a/arch/riscv/kvm/gstage.c
> +++ b/arch/riscv/kvm/gstage.c
> @@ -320,7 +320,6 @@ void __init kvm_riscv_gstage_mode_detect(void)
> csr_write(CSR_HGATP, HGATP_MODE_SV57X4 << HGATP_MODE_SHIFT);
> if ((csr_read(CSR_HGATP) >> HGATP_MODE_SHIFT) == HGATP_MODE_SV57X4) {
> kvm_riscv_gstage_mode = HGATP_MODE_SV57X4;
> - kvm_riscv_gstage_pgd_levels = 5;
> goto done;
> }
>
> @@ -328,7 +327,6 @@ void __init kvm_riscv_gstage_mode_detect(void)
> csr_write(CSR_HGATP, HGATP_MODE_SV48X4 << HGATP_MODE_SHIFT);
> if ((csr_read(CSR_HGATP) >> HGATP_MODE_SHIFT) == HGATP_MODE_SV48X4) {
> kvm_riscv_gstage_mode = HGATP_MODE_SV48X4;
> - kvm_riscv_gstage_pgd_levels = 4;
> goto done;
> }
>
> @@ -336,7 +334,6 @@ void __init kvm_riscv_gstage_mode_detect(void)
> csr_write(CSR_HGATP, HGATP_MODE_SV39X4 << HGATP_MODE_SHIFT);
> if ((csr_read(CSR_HGATP) >> HGATP_MODE_SHIFT) == HGATP_MODE_SV39X4) {
> kvm_riscv_gstage_mode = HGATP_MODE_SV39X4;
> - kvm_riscv_gstage_pgd_levels = 3;
> goto done;
> }
> #else /* CONFIG_32BIT */
> @@ -354,6 +351,10 @@ void __init kvm_riscv_gstage_mode_detect(void)
> kvm_riscv_gstage_pgd_levels = 0;
>
> done:
> +#ifdef CONFIG_64BIT
> + kvm_riscv_gstage_mode = min(satp_mode >> SATP_MODE_SHIFT, kvm_riscv_gstage_mode);
It's out of the no5/4lvl scope, because we lose (hs-mode)satp=sv39 +
hgatp=sv48x4 combination.

How about re-parsing no5/4lvl parameters?

> + kvm_riscv_gstage_pgd_levels = kvm_riscv_gstage_mode - HGATP_MODE_SV39X4 + 3;
> +#endif
> csr_write(CSR_HGATP, 0);
> kvm_riscv_local_hfence_gvma_all();
> }
> --
> 2.50.1
>

--
Best Regards
Guo Ren