Skip to contentSkip to conversation

RISC-V: KVM: Allow to downgrade HGATP mode via SATP mode

PATCH In review [email protected] 5 participants 8 messages Nov 22, 2025 – Dec 3, 2025

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 RISC-V: KVM: Allow to downgrade HGATP mode via SATP mode

From: Fangyu Yu <[email protected]>

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.

---
 arch/riscv/kvm/gstage.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
arch/riscv/kvm/gstage.c +4 -3
index b67d60d722c2..bff80c80ead3 100644
@@ -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);
+	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();
 }
Signature · 1 line
2.50.1
Acked-by on v1 1/1
#1 lore raw
Quoted 9 lines · On Sat, Nov 22, 2025 at 3:50 PM <[email protected]> wrote:

From: Fangyu Yu <[email protected]>

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.

Quoted 35 lines
---
 arch/riscv/kvm/gstage.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
arch/riscv/kvm/gstage.c +0 -3
index b67d60d722c2..bff80c80ead3 100644
@@ -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 */

arch/riscv/kvm/gstage.c void __init kvm_riscv_gstage_mode_detect(void) · line 355

Quoted 5 lines
arch/riscv/kvm/gstage.c +2 -0
@@ -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?

Quoted 8 lines
arch/riscv/kvm/gstage.c +2 -1
+       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

Signature · 2 lines
Best Regards
 Guo Ren
#2 lore raw
Quoted 4 lines · Guo Ren
Quoted 3 lines · On Sat, Nov 22, 2025 at 3:50 PM <[email protected]> wrote:

From: Fangyu Yu <[email protected]>

Quoted 8 lines
Quoted 6 lines

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.

The documentation should be improved, but I don't think we want to state that these parameters apply to both s- and g-stage. If we need parameters to dictate KVM behavior (g-stage management), then we should add KVM module parameters.

Thanks,
drew

#3 lore raw
Quoted 10 lines
Quoted 10 lines · On Sat, Nov 22, 2025 at 3:50 PM <[email protected]> wrote:
Quoted 9 lines

From: Fangyu Yu <[email protected]>

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.

Quoted 7 lines
Quoted 2 lines

Yes, no5/4lvl is not clear about satp or hgatp. So, covering HGPATP is reasonable.

The documentation should be improved, but I don't think we want to state that these parameters apply to both s- and g-stage. If we need parameters to dictate KVM behavior (g-stage management), then we should add KVM module parameters.

Right, adding new parameters for g-stage management is clear.

Or we could discuss this topic, from a virtual machine perspective, it may not be necessary to provide all hardware configuration combinations. For example, when SATP is configured as sv48, configuring HGATP as sv57*4 is not very meaningful, Because the VM cannot actually use more than 48 bits of GPA range.

Thanks,
Fangyu

Quoted 2 lines

Thanks,
drew

#4 lore raw
2025-11-25T22:18:11+08:00, <[email protected]>:
Quoted 18 lines
Quoted 10 lines
Quoted 10 lines · On Sat, Nov 22, 2025 at 3:50 PM <[email protected]> wrote:
Quoted 9 lines

From: Fangyu Yu <[email protected]>

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.

Quoted 7 lines
Quoted 2 lines

Yes, no5/4lvl is not clear about satp or hgatp. So, covering HGPATP is reasonable.

The documentation should be improved, but I don't think we want to state that these parameters apply to both s- and g-stage. If we need parameters to dictate KVM behavior (g-stage management), then we should add KVM module parameters.

Quoted 7 lines

Right, adding new parameters for g-stage management is clear.

Or we could discuss this topic, from a virtual machine perspective, it may not be necessary to provide all hardware configuration combinations. For example, when SATP is configured as sv48, configuring HGATP as sv57*4 is not very meaningful, Because the VM cannot actually use more than 48 bits of GPA range.

The choice of hgatp mode depends on how users configure guest's memory map, regardless of what satp or vsatp modes are.
(All RV64 SvXY modes map XY bit VA to 56 bit PA.)

If the machine model maps memory with set bit 55, then KVM needs to configure Sv57x4, and if nothing is mapped above 2 TiB, then KVM is completely fine with Sv39x4.

A module parameter works, but I think it would be nicer to set the hgatp mode per-VM, because most VMs could use the efficient Sv39x4, while it's not a good idea to pick it as the default.
I think KVM has enough information to do it automatically (and without too much complexity) by starting with Sv39x4, and expanding as needed.

Thanks.

#5 lore raw
Quoted 34 lines
Quoted 18 lines
Quoted 10 lines
Quoted 10 lines · On Sat, Nov 22, 2025 at 3:50 PM <[email protected]> wrote:
Quoted 9 lines

From: Fangyu Yu <[email protected]>

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.

Quoted 7 lines
Quoted 2 lines

Yes, no5/4lvl is not clear about satp or hgatp. So, covering HGPATP is reasonable.

The documentation should be improved, but I don't think we want to state that these parameters apply to both s- and g-stage. If we need parameters to dictate KVM behavior (g-stage management), then we should add KVM module parameters.

Quoted 7 lines

Right, adding new parameters for g-stage management is clear.

Or we could discuss this topic, from a virtual machine perspective, it may not be necessary to provide all hardware configuration combinations. For example, when SATP is configured as sv48, configuring HGATP as sv57*4 is not very meaningful, Because the VM cannot actually use more than 48 bits of GPA range.

The choice of hgatp mode depends on how users configure guest's memory map, regardless of what satp or vsatp modes are.
(All RV64 SvXY modes map XY bit VA to 56 bit PA.)

If the machine model maps memory with set bit 55, then KVM needs to configure Sv57x4, and if nothing is mapped above 2 TiB, then KVM is completely fine with Sv39x4.

Quoted 6 lines

A module parameter works, but I think it would be nicer to set the hgatp mode per-VM, because most VMs could use the efficient Sv39x4, while it's not a good idea to pick it as the default.
I think KVM has enough information to do it automatically (and without too much complexity) by starting with Sv39x4, and expanding as needed.

It does seem more reasonable to select the HGATP mode for each VM based on the actual required GPA range.
I will send an updated patch based on this suggestion.

Thanks,
Fangyu

#6 lore raw
In reply to Radim Krčmář
Quoted 36 lines · Radim Krčmář
2025-11-25T22:18:11+08:00, <[email protected]>:
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines · On Sat, Nov 22, 2025 at 3:50 PM <[email protected]> wrote:
Quoted 7 lines

From: Fangyu Yu <[email protected]>

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

Quoted 10 lines
Quoted 9 lines
Quoted 4 lines
Quoted 2 lines

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.

The documentation should be improved, but I don't think we want to state that these parameters apply to both s- and g-stage. If we need parameters to dictate KVM behavior (g-stage management), then we should add KVM module parameters.

Quoted 7 lines

Right, adding new parameters for g-stage management is clear.

Or we could discuss this topic, from a virtual machine perspective, it may not be necessary to provide all hardware configuration combinations. For example, when SATP is configured as sv48, configuring HGATP as sv57*4 is not very meaningful, Because the VM cannot actually use more than 48 bits of GPA range.

The choice of hgatp mode depends on how users configure guest's memory map, regardless of what satp or vsatp modes are.
(All RV64 SvXY modes map XY bit VA to 56 bit PA.)

If the machine model maps memory with set bit 55, then KVM needs to configure Sv57x4, and if nothing is mapped above 2 TiB, then KVM is completely fine with Sv39x4.

Quoted 6 lines

A module parameter works, but I think it would be nicer to set the hgatp mode per-VM, because most VMs could use the efficient Sv39x4, while it's not a good idea to pick it as the default.
I think KVM has enough information to do it automatically (and without too much complexity) by starting with Sv39x4, and expanding as needed.

Good point; if only a 128GB GPA memory region is needed, there is no need for Sv57x4, which costs PTW cycles.

So, the detection should start from Sv39x4 to Sv57x4 with the guest memory size.

Signature · 2 lines
Best Regards
 Guo Ren

_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv

maintainer
#7 lore raw
Quoted 46 lines · Guo Ren
Quoted 36 lines · Radim Krčmář
2025-11-25T22:18:11+08:00, <[email protected]>:
Quoted 18 lines
Quoted 10 lines
Quoted 10 lines · On Sat, Nov 22, 2025 at 3:50 PM <[email protected]> wrote:
Quoted 9 lines

From: Fangyu Yu <[email protected]>

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.

Quoted 7 lines
Quoted 2 lines

Yes, no5/4lvl is not clear about satp or hgatp. So, covering HGPATP is reasonable.

The documentation should be improved, but I don't think we want to state that these parameters apply to both s- and g-stage. If we need parameters to dictate KVM behavior (g-stage management), then we should add KVM module parameters.

Quoted 7 lines

Right, adding new parameters for g-stage management is clear.

Or we could discuss this topic, from a virtual machine perspective, it may not be necessary to provide all hardware configuration combinations. For example, when SATP is configured as sv48, configuring HGATP as sv57*4 is not very meaningful, Because the VM cannot actually use more than 48 bits of GPA range.

The choice of hgatp mode depends on how users configure guest's memory map, regardless of what satp or vsatp modes are.
(All RV64 SvXY modes map XY bit VA to 56 bit PA.)

If the machine model maps memory with set bit 55, then KVM needs to configure Sv57x4, and if nothing is mapped above 2 TiB, then KVM is completely fine with Sv39x4.

Quoted 5 lines

A module parameter works, but I think it would be nicer to set the hgatp mode per-VM, because most VMs could use the efficient Sv39x4, while it's not a good idea to pick it as the default.
I think KVM has enough information to do it automatically (and without too much complexity) by starting with Sv39x4, and expanding as needed.

Quoted 4 lines

Good point; if only a 128GB GPA memory region is needed, there is no need for Sv57x4, which costs PTW cycles.

So, the detection should start from Sv39x4 to Sv57x4 with the guest memory size.

NACK to the approach of detecting backwards.

HGATP mode is a per-VM attribute hence it makes sense to define a VM-level KVM_CAP_RISC_xyz for this purpose. The default behaviour should be use highest HGATP mode unless KVM user-space changes the KVM_CAP_RISC_xyz setting.

---
Anup