Skip to contentSkip to conversation

Support runtime configuration for per-VM's HGATP mode

PATCH Superseded [email protected] 2 participants 11 messages Jan 25 – Jan 31

Subsystems (4)

v3 of 8 Superseded Latest is v8 →

lore
Share & tools
lore
#0 lore raw

v3 cover Support runtime configuration for per-VM's HGATP mode

From: Fangyu Yu <[email protected]>

Currently, RISC-V KVM hardcodes the G-stage page table format (HGATP mode) to the maximum mode detected at boot time (e.g., SV57x4 if supported). but often such a wide GPA is unnecessary, just as a host sometimes doesn't need sv57.

This patch introduces per-VM configurability of the G-stage mode via a new KVM capability: KVM_CAP_RISCV_SET_HGATP_MODE. User-space can now explicitly request a specific HGATP mode (SV39x4, SV48x4, or SV57x4 on 64-bit) during VM creation.

---
Changes in v3:
- Reworked the patch formatting (per Drew).
- Dropped kvm->arch.kvm_riscv_gstage_mode and derive HGATP.MODE from kvm_riscv_gstage_pgd_levels via a helper, avoiding redundant per-VM state(per Drew).
- Removed kvm_riscv_gstage_max_mode and keep only kvm_riscv_gstage_max_pgd_levels for host capability detection(per Drew).
- Other initialization and return value issues(per Drew).
- Enforce that KVM_CAP_RISCV_SET_HGATP_MODE can only be enabled before any vCPUs are created by rejecting the ioctl once kvm->created_vcpus is non-zero(per Radim).
- Add a memslot safety check and reject the capability unless kvm_are_all_memslots_empty(kvm) is true, ensuring the G-stage format is not changed after any memslots have been installed(per Radim).
- Link to v2:
https://lore.kernel.org/linux-riscv/[email protected]/
---

Fangyu Yu (2):
RISC-V: KVM: Support runtime configuration for per-VM's HGATP mode
RISC-V: KVM: add KVM_CAP_RISCV_SET_HGATP_MODE

 Documentation/virt/kvm/api.rst      | 18 +++++++
 arch/riscv/include/asm/kvm_gstage.h | 11 ++---
 arch/riscv/include/asm/kvm_host.h   | 19 +++++++
 arch/riscv/kvm/gstage.c             | 77 ++++++++++++++++-------------
 arch/riscv/kvm/main.c               | 12 ++---
 arch/riscv/kvm/mmu.c                | 23 ++++++---
 arch/riscv/kvm/vm.c                 | 28 +++++++++--
 arch/riscv/kvm/vmid.c               |  3 +-
 include/uapi/linux/kvm.h            |  1 +
 9 files changed, 133 insertions(+), 59 deletions(-)
Signature · 1 line
2.50.1
#1 lore raw

PATCH v3 1/2 RISC-V: KVM: Support runtime configuration for per-VM's HGATP mode

From: Fangyu Yu <[email protected]>

Introduces one per-VM architecture-specific fields to support runtime configuration of the G-stage page table format:

- kvm->arch.kvm_riscv_gstage_pgd_levels: the corresponding number of page table levels for the selected mode.

These fields replace the previous global variables kvm_riscv_gstage_mode and kvm_riscv_gstage_pgd_levels, enabling different virtual machines to independently select their G-stage page table format instead of being forced to share the maximum mode detected by the kernel at boot time.

---
 arch/riscv/include/asm/kvm_gstage.h | 11 ++---
 arch/riscv/include/asm/kvm_host.h   | 19 +++++++
 arch/riscv/kvm/gstage.c             | 77 ++++++++++++++++-------------
 arch/riscv/kvm/main.c               | 12 ++---
 arch/riscv/kvm/mmu.c                | 23 ++++++---
 arch/riscv/kvm/vm.c                 |  2 +-
 arch/riscv/kvm/vmid.c               |  3 +-
 7 files changed, 90 insertions(+), 57 deletions(-)
arch/riscv/include/asm/kvm_gstage.h +4 -7
index 595e2183173e..7993b15ebfcd 100644
@@ -29,16 +29,10 @@ struct kvm_gstage_mapping {
 #define kvm_riscv_gstage_index_bits	10
 #endif
 
-extern unsigned long kvm_riscv_gstage_mode;
-extern unsigned long kvm_riscv_gstage_pgd_levels;
+extern unsigned long kvm_riscv_gstage_max_pgd_levels;
 
 #define kvm_riscv_gstage_pgd_xbits	2
 #define kvm_riscv_gstage_pgd_size	(1UL << (HGATP_PAGE_SHIFT + kvm_riscv_gstage_pgd_xbits))
-#define kvm_riscv_gstage_gpa_bits	(HGATP_PAGE_SHIFT + \
-					 (kvm_riscv_gstage_pgd_levels * \
-					  kvm_riscv_gstage_index_bits) + \
-					 kvm_riscv_gstage_pgd_xbits)
-#define kvm_riscv_gstage_gpa_size	((gpa_t)(1ULL << kvm_riscv_gstage_gpa_bits))
 
 bool kvm_riscv_gstage_get_leaf(struct kvm_gstage *gstage, gpa_t addr,
 			       pte_t **ptepp, u32 *ptep_level);
@@ -69,4 +63,7 @@ void kvm_riscv_gstage_wp_range(struct kvm_gstage *gstage, gpa_t start, gpa_t end
 
 void kvm_riscv_gstage_mode_detect(void);
 
+gpa_t kvm_riscv_gstage_gpa_size(struct kvm_arch *k);
+unsigned long kvm_riscv_gstage_gpa_bits(struct kvm_arch *k);
+
 #endif
arch/riscv/include/asm/kvm_host.h +19 -0
index 24585304c02b..a111bff965fa 100644
@@ -87,6 +87,22 @@ struct kvm_vcpu_stat {
 struct kvm_arch_memory_slot {
 };
 
+static inline unsigned long kvm_riscv_gstage_mode(unsigned long pgd_levels)
+{
+	switch (pgd_levels) {
+	case 2:
+		return HGATP_MODE_SV32X4;
+	case 3:
+		return HGATP_MODE_SV39X4;
+	case 4:
+		return HGATP_MODE_SV48X4;
+	case 5:
+		return HGATP_MODE_SV57X4;
+	default:
+		return HGATP_MODE_OFF;
+	}
+}
+
 struct kvm_arch {
 	/* G-stage vmid */
 	struct kvm_vmid vmid;
@@ -103,6 +119,9 @@ struct kvm_arch {
 
 	/* KVM_CAP_RISCV_MP_STATE_RESET */
 	bool mp_state_reset;
+
+	unsigned long kvm_riscv_gstage_pgd_levels;
+	bool gstage_mode_user_initialized;
 };
 
 struct kvm_cpu_trap {
arch/riscv/kvm/gstage.c +43 -34

215 lines not on this page · raw message

arch/riscv/kvm/main.c +6 -6
index 45536af521f0..786c0025e2c3 100644
@@ -105,17 +105,17 @@ static int __init riscv_kvm_init(void)
 		return rc;
 
 	kvm_riscv_gstage_mode_detect();
-	switch (kvm_riscv_gstage_mode) {
-	case HGATP_MODE_SV32X4:
+	switch (kvm_riscv_gstage_max_pgd_levels) {
+	case 2:
 		str = "Sv32x4";
 		break;
-	case HGATP_MODE_SV39X4:
+	case 3:
 		str = "Sv39x4";
 		break;
-	case HGATP_MODE_SV48X4:
+	case 4:
 		str = "Sv48x4";
 		break;
-	case HGATP_MODE_SV57X4:
+	case 5:
 		str = "Sv57x4";
 		break;
 	default:
@@ -164,7 +164,7 @@ static int __init riscv_kvm_init(void)
 			 (rc) ? slist : "no features");
 	}
 
-	kvm_info("using %s G-stage page table format\n", str);
+	kvm_info("Max G-stage page table format %s\n", str);
 
 	kvm_info("VMID %ld bits available\n", kvm_riscv_gstage_vmid_bits());
 
arch/riscv/kvm/mmu.c +15 -8
index 4ab06697bfc0..f91a25175305 100644
@@ -67,7 +67,7 @@ int kvm_riscv_mmu_ioremap(struct kvm *kvm, gpa_t gpa, phys_addr_t hpa,
 		if (!writable)
 			map.pte = pte_wrprotect(map.pte);
 
-		ret = kvm_mmu_topup_memory_cache(&pcache, kvm_riscv_gstage_pgd_levels);
+		ret = kvm_mmu_topup_memory_cache(&pcache, kvm->arch.kvm_riscv_gstage_pgd_levels);
 		if (ret)
 			goto out;
 
@@ -186,7 +186,7 @@ int kvm_arch_prepare_memory_region(struct kvm *kvm,
 	 * space addressable by the KVM guest GPA space.
 	 */
 	if ((new->base_gfn + new->npages) >=
-	    (kvm_riscv_gstage_gpa_size >> PAGE_SHIFT))
+	     kvm_riscv_gstage_gpa_size(&kvm->arch) >> PAGE_SHIFT)
 		return -EFAULT;
 
 	hva = new->userspace_addr;
@@ -332,7 +332,7 @@ int kvm_riscv_mmu_map(struct kvm_vcpu *vcpu, struct kvm_memory_slot *memslot,
 	memset(out_map, 0, sizeof(*out_map));
 
 	/* We need minimum second+third level pages */
-	ret = kvm_mmu_topup_memory_cache(pcache, kvm_riscv_gstage_pgd_levels);
+	ret = kvm_mmu_topup_memory_cache(pcache, kvm->arch.kvm_riscv_gstage_pgd_levels);
 	if (ret) {
 		kvm_err("Failed to topup G-stage cache\n");
 		return ret;
@@ -431,6 +431,10 @@ int kvm_riscv_mmu_alloc_pgd(struct kvm *kvm)
 		return -ENOMEM;
 	kvm->arch.pgd = page_to_virt(pgd_page);
 	kvm->arch.pgd_phys = page_to_phys(pgd_page);
+	if (!kvm->arch.gstage_mode_user_initialized) {
+		/* User-space didn't set KVM_CAP_RISC_HGATP_MODE capability. */
+		kvm->arch.kvm_riscv_gstage_pgd_levels = kvm_riscv_gstage_max_pgd_levels;
+	}
 
 	return 0;
 }
@@ -446,10 +450,12 @@ void kvm_riscv_mmu_free_pgd(struct kvm *kvm)
 		gstage.flags = 0;
 		gstage.vmid = READ_ONCE(kvm->arch.vmid.vmid);
 		gstage.pgd = kvm->arch.pgd;
-		kvm_riscv_gstage_unmap_range(&gstage, 0UL, kvm_riscv_gstage_gpa_size, false);
+		kvm_riscv_gstage_unmap_range(&gstage, 0UL,
+			kvm_riscv_gstage_gpa_size(&kvm->arch), false);
 		pgd = READ_ONCE(kvm->arch.pgd);
 		kvm->arch.pgd = NULL;
 		kvm->arch.pgd_phys = 0;
+		kvm->arch.kvm_riscv_gstage_pgd_levels = 0;
 	}
 	spin_unlock(&kvm->mmu_lock);
 
@@ -459,11 +465,12 @@ void kvm_riscv_mmu_free_pgd(struct kvm *kvm)
 
 void kvm_riscv_mmu_update_hgatp(struct kvm_vcpu *vcpu)
 {
-	unsigned long hgatp = kvm_riscv_gstage_mode << HGATP_MODE_SHIFT;
-	struct kvm_arch *k = &vcpu->kvm->arch;
+	struct kvm_arch *ka = &vcpu->kvm->arch;
+	unsigned long hgatp = kvm_riscv_gstage_mode(ka->kvm_riscv_gstage_pgd_levels)
+			      << HGATP_MODE_SHIFT;
 
-	hgatp |= (READ_ONCE(k->vmid.vmid) << HGATP_VMID_SHIFT) & HGATP_VMID;
-	hgatp |= (k->pgd_phys >> PAGE_SHIFT) & HGATP_PPN;
+	hgatp |= (READ_ONCE(ka->vmid.vmid) << HGATP_VMID_SHIFT) & HGATP_VMID;
+	hgatp |= (ka->pgd_phys >> PAGE_SHIFT) & HGATP_PPN;
 
 	ncsr_write(CSR_HGATP, hgatp);
 
arch/riscv/kvm/vm.c +1 -1
index 66d91ae6e9b2..4b2156df40fc 100644
@@ -200,7 +200,7 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
 		r = KVM_USER_MEM_SLOTS;
 		break;
 	case KVM_CAP_VM_GPA_BITS:
-		r = kvm_riscv_gstage_gpa_bits;
+		r = kvm_riscv_gstage_gpa_bits(&kvm->arch);
 		break;
 	default:
 		r = 0;
arch/riscv/kvm/vmid.c +2 -1
index cf34d448289d..c15bdb1dd8be 100644
@@ -26,7 +26,8 @@ static DEFINE_SPINLOCK(vmid_lock);
 void __init kvm_riscv_gstage_vmid_detect(void)
 {
 	/* Figure-out number of VMID bits in HW */
-	csr_write(CSR_HGATP, (kvm_riscv_gstage_mode << HGATP_MODE_SHIFT) | HGATP_VMID);
+	csr_write(CSR_HGATP, (kvm_riscv_gstage_mode(kvm_riscv_gstage_max_pgd_levels) <<
+			      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);
Signature · 1 line
2.50.1
#2 lore raw

Re: [PATCH v3 1/2] RISC-V: KVM: Support runtime configuration for per-VM's HGATP mode

2026-01-25T23:04:49+08:00, <[email protected]>:
Quoted 24 lines

From: Fangyu Yu <[email protected]>

Introduces one per-VM architecture-specific fields to support runtime configuration of the G-stage page table format:

- kvm->arch.kvm_riscv_gstage_pgd_levels: the corresponding number of page table levels for the selected mode.

These fields replace the previous global variables kvm_riscv_gstage_mode and kvm_riscv_gstage_pgd_levels, enabling different virtual machines to independently select their G-stage page table format instead of being forced to share the maximum mode detected by the kernel at boot time.

---

arch/riscv/include/asm/kvm_host.h +3 -0
@@ -87,6 +87,22 @@ struct kvm_vcpu_stat {
 struct kvm_arch_memory_slot {
 };
 
+static inline unsigned long kvm_riscv_gstage_mode(unsigned long pgd_levels)
+{
+	switch (pgd_levels) {

arch/riscv/include/asm/kvm_host.h struct kvm_vcpu_stat { · line 102

Quoted 10 lines
arch/riscv/include/asm/kvm_host.h +10 -0
+	case 2:
+		return HGATP_MODE_SV32X4;
+	case 3:
+		return HGATP_MODE_SV39X4;
+	case 4:
+		return HGATP_MODE_SV48X4;
+	case 5:
+		return HGATP_MODE_SV57X4;
+	default:
+		return HGATP_MODE_OFF;

I think default should be an internal error.
We can do "case 0: return HGATP_MODE_OFF;", or just error it too since KVM shouldn't ever ask for mode without protection anyway.

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

Quoted 13 lines
arch/riscv/kvm/gstage.c +11 -0
@@ -319,41 +321,48 @@ void __init kvm_riscv_gstage_mode_detect(void)
+unsigned long kvm_riscv_gstage_gpa_bits(struct kvm_arch *ka)
+{
+	return (HGATP_PAGE_SHIFT +
+		ka->kvm_riscv_gstage_pgd_levels * kvm_riscv_gstage_index_bits +
+		kvm_riscv_gstage_pgd_xbits);
+}
+
+gpa_t kvm_riscv_gstage_gpa_size(struct kvm_arch *ka)
+{
+	return BIT_ULL(kvm_riscv_gstage_gpa_bits(ka));
+}

Please define these two functions as static inline in the header files. They used to be just macros there, so it'd be safer not put LTO into the equation.

Quoted 25 lines
arch/riscv/kvm/main.c +5 -5
@@ -105,17 +105,17 @@ static int __init riscv_kvm_init(void)
 		return rc;
 
 	kvm_riscv_gstage_mode_detect();
-	switch (kvm_riscv_gstage_mode) {
-	case HGATP_MODE_SV32X4:
+	switch (kvm_riscv_gstage_max_pgd_levels) {
+	case 2:
 		str = "Sv32x4";
 		break;
-	case HGATP_MODE_SV39X4:
+	case 3:
 		str = "Sv39x4";
 		break;
-	case HGATP_MODE_SV48X4:
+	case 4:
 		str = "Sv48x4";
 		break;
-	case HGATP_MODE_SV57X4:
+	case 5:
 		str = "Sv57x4";
 		break;
 	default:

arch/riscv/kvm/main.c static int __init riscv_kvm_init(void) · line 167

Quoted 5 lines
arch/riscv/kvm/main.c +1 -1
@@ -164,7 +164,7 @@ static int __init riscv_kvm_init(void)
 			 (rc) ? slist : "no features");
 	}
 
-	kvm_info("using %s G-stage page table format\n", str);
+	kvm_info("Max G-stage page table format %s\n", str);

Fun fact: the ISA doesn't define the same hierarchy for hgatp modes as it does for satp modes, so we could have just Sv57x4 and nothing below.

We could do just with a code comment that we're assuming vendors will do better, but I'd rather not introduce more assumptions...
I think the easiest would be to kvm_riscv_gstage_mode_detect() levels in reverse and stop on the first one that is not supported. (I'll reply with a patch later.)

Thanks.

#3 lore raw

[SUGGESTION] KVM: RISC-V: detect gstage mode hierarchy

2026-01-29T15:27:35+00:00, Radim Krčmář <[email protected]>:
Quoted 1 line

(I'll reply with a patch later.)

Something like this would avoid a bit of technical debt. The solution could be even more generic by returning a bitmap of supported modes, but that would be larger refactoring...

Feel free to use it in the series, but beware: only compile-tested. It's late and hope it at least boots. :)

Thanks.
---8<---
RISC-V ISA does not require a hierarchy of standard hgatp mode (i.e. 57x4 does not depend on 48x4 onr 39x4), yet future patches want to assume that the hierarchy exists, for simplicity.

Only accept a hgatp mode if all narrower modes are supported as well.

All sensible RISC-V implementations should have the hierarchy, since it's very cheap to add the narrower modes.

---
The hunk that removes the default values at the beginning of gstage.c should have been a separate patch, sorry.

---
 arch/riscv/include/asm/kvm_gstage.h |  2 +-
 arch/riscv/kvm/gstage.c             | 66 ++++++++++++-----------------
 arch/riscv/kvm/main.c               |  6 ++-
 3 files changed, 32 insertions(+), 42 deletions(-)
arch/riscv/include/asm/kvm_gstage.h +1 -1
index 595e2183173e..18db474ce583 100644
@@ -67,6 +67,6 @@ void kvm_riscv_gstage_unmap_range(struct kvm_gstage *gstage,
 
 void kvm_riscv_gstage_wp_range(struct kvm_gstage *gstage, gpa_t start, gpa_t end);
 
-void kvm_riscv_gstage_mode_detect(void);
+unsigned long kvm_riscv_gstage_mode_detect(void);
 
 #endif
arch/riscv/kvm/gstage.c +26 -40

86 lines not on this page · raw message

arch/riscv/kvm/main.c +5 -1
index 45536af521f0..58fd6ae8e04a 100644
@@ -104,19 +104,23 @@ static int __init riscv_kvm_init(void)
 	if (rc && rc != -ENODEV)
 		return rc;
 
-	kvm_riscv_gstage_mode_detect();
+	kvm_riscv_gstage_mode = kvm_riscv_gstage_mode_detect();
 	switch (kvm_riscv_gstage_mode) {
 	case HGATP_MODE_SV32X4:
 		str = "Sv32x4";
+		kvm_riscv_gstage_pgd_levels = 2;
 		break;
 	case HGATP_MODE_SV39X4:
 		str = "Sv39x4";
+		kvm_riscv_gstage_pgd_levels = 3;
 		break;
 	case HGATP_MODE_SV48X4:
 		str = "Sv48x4";
+		kvm_riscv_gstage_pgd_levels = 4;
 		break;
 	case HGATP_MODE_SV57X4:
 		str = "Sv57x4";
+		kvm_riscv_gstage_pgd_levels = 5;
 		break;
 	default:
 		kvm_riscv_nacl_exit();
Signature · 1 line
2.51.0
#4 lore raw

Re: Re: [PATCH v3 1/2] RISC-V: KVM: Support runtime configuration for per-VM's HGATP mode

In reply to Radim Krčmář
Quoted 30 lines
Quoted 30 lines

From: Fangyu Yu <[email protected]>

Introduces one per-VM architecture-specific fields to support runtime configuration of the G-stage page table format:

- kvm->arch.kvm_riscv_gstage_pgd_levels: the corresponding number of page table levels for the selected mode.

These fields replace the previous global variables kvm_riscv_gstage_mode and kvm_riscv_gstage_pgd_levels, enabling different virtual machines to independently select their G-stage page table format instead of being forced to share the maximum mode detected by the kernel at boot time.

---

arch/riscv/include/asm/kvm_host.h +9 -0
@@ -87,6 +87,22 @@ struct kvm_vcpu_stat {
 struct kvm_arch_memory_slot {
 };
 
+static inline unsigned long kvm_riscv_gstage_mode(unsigned long pgd_levels)
+{
+	switch (pgd_levels) {
+	case 2:
+		return HGATP_MODE_SV32X4;
+	case 3:
+		return HGATP_MODE_SV39X4;
+	case 4:
+		return HGATP_MODE_SV48X4;
Quoted 8 lines

arch/riscv/include/asm/kvm_host.h struct kvm_vcpu_stat { · line 102

Quoted 4 lines
arch/riscv/include/asm/kvm_host.h +4 -0
+	case 5:
+		return HGATP_MODE_SV57X4;
+	default:
+		return HGATP_MODE_OFF;

I think default should be an internal error.
We can do "case 0: return HGATP_MODE_OFF;", or just error it too since KVM shouldn't ever ask for mode without protection anyway.

Good point. Returning HGATP_MODE_OFF in the default case would hide an internal bug (unexpected pgd_levels). I’ll treat it as an internal error instead, Something like:

    default:
        WARN_ON_ONCE(1);
        return HGATP_MODE_OFF;
    }
Quoted 9 lines
Quoted 9 lines
arch/riscv/kvm/gstage.c +7 -0
@@ -319,41 +321,48 @@ void __init kvm_riscv_gstage_mode_detect(void)
+unsigned long kvm_riscv_gstage_gpa_bits(struct kvm_arch *ka)
+{
+	return (HGATP_PAGE_SHIFT +
+		ka->kvm_riscv_gstage_pgd_levels * kvm_riscv_gstage_index_bits +
+		kvm_riscv_gstage_pgd_xbits);
+}
+
Quoted 8 lines

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

Quoted 4 lines
arch/riscv/kvm/gstage.c +4 -0
+gpa_t kvm_riscv_gstage_gpa_size(struct kvm_arch *ka)
+{
+	return BIT_ULL(kvm_riscv_gstage_gpa_bits(ka));
+}

Please define these two functions as static inline in the header files. They used to be just macros there, so it'd be safer not put LTO into the equation.

Agreed.

Quoted 34 lines
Quoted 25 lines
arch/riscv/kvm/main.c +5 -5
@@ -105,17 +105,17 @@ static int __init riscv_kvm_init(void)
 		return rc;
 
 	kvm_riscv_gstage_mode_detect();
-	switch (kvm_riscv_gstage_mode) {
-	case HGATP_MODE_SV32X4:
+	switch (kvm_riscv_gstage_max_pgd_levels) {
+	case 2:
 		str = "Sv32x4";
 		break;
-	case HGATP_MODE_SV39X4:
+	case 3:
 		str = "Sv39x4";
 		break;
-	case HGATP_MODE_SV48X4:
+	case 4:
 		str = "Sv48x4";
 		break;
-	case HGATP_MODE_SV57X4:
+	case 5:
 		str = "Sv57x4";
 		break;
 	default:

arch/riscv/kvm/main.c static int __init riscv_kvm_init(void) · line 167

Quoted 5 lines
arch/riscv/kvm/main.c +1 -1
@@ -164,7 +164,7 @@ static int __init riscv_kvm_init(void)
 			 (rc) ? slist : "no features");
 	}
 
-	kvm_info("using %s G-stage page table format\n", str);
+	kvm_info("Max G-stage page table format %s\n", str);

Fun fact: the ISA doesn't define the same hierarchy for hgatp modes as it does for satp modes, so we could have just Sv57x4 and nothing below.

Quoted 5 lines

We could do just with a code comment that we're assuming vendors will do better, but I'd rather not introduce more assumptions...
I think the easiest would be to kvm_riscv_gstage_mode_detect() levels in reverse and stop on the first one that is not supported. (I'll reply with a patch later.)

Please refer to the discussion here:
https://github.com/riscv/riscv-isa-manual/issues/2208
If Sv57x4 is implemented, then Sv48x4 and Sv39x4 must also be implemented.

Quoted 1 line

Thanks.

Thanks,
Fangyu

#5 lore raw

Re: [PATCH v3 1/2] RISC-V: KVM: Support runtime configuration for per-VM's HGATP mode

2026-01-30T21:24:58+08:00, <[email protected]>:
Quoted 13 lines
Quoted 7 lines

arch/riscv/kvm/main.c static int __init riscv_kvm_init(void) · line 167

Quoted 3 lines

From: Fangyu Yu <[email protected]>

arch/riscv/kvm/main.c +1 -1
-	kvm_info("using %s G-stage page table format\n", str);
+	kvm_info("Max G-stage page table format %s\n", str);

Fun fact: the ISA doesn't define the same hierarchy for hgatp modes as it does for satp modes, so we could have just Sv57x4 and nothing below.

Quoted 5 lines

We could do just with a code comment that we're assuming vendors will do better, but I'd rather not introduce more assumptions...
I think the easiest would be to kvm_riscv_gstage_mode_detect() levels in reverse and stop on the first one that is not supported. (I'll reply with a patch later.)

Quoted 3 lines

Please refer to the discussion here:
https://github.com/riscv/riscv-isa-manual/issues/2208
If Sv57x4 is implemented, then Sv48x4 and Sv39x4 must also be implemented.

I don't think so, sadly, but we're mostly dealing with technicalities here. As Andrew pointed out:

"The H extension itself does not impose this requirement, so technically Sv57x4 without Sv48x4 conforms to the H extension spec."

This means it's completely valid to support {Bare, Sv39x4, Sv57x4}. The RVA23 profile imposes additional constraints via Shgatpa:

"For each supported virtual memory scheme SvNN supported in satp, the corresponding hgatp SvNNx4 mode must be supported.
The hgatp mode Bare must also be supported."

The requirement only goes one way, so an RVA23 implementation with just {Bare, Sv39} in satp could support {Bare, Sv39x4, Sv57x4} in hgatp, because RVA23 nor ISA prevent Sv57x4 to be there.
Not that I expect any sensible implementation to do this...

Btw. do we target only RVA23 with KVM?

Thanks.

#6 lore raw

Re: Re: [PATCH v3 1/2] RISC-V: KVM: Support runtime configuration for per-VM's HGATP mode

Quoted 30 lines
Quoted 13 lines
Quoted 7 lines

arch/riscv/kvm/main.c static int __init riscv_kvm_init(void) · line 167

Quoted 3 lines

From: Fangyu Yu <[email protected]>

arch/riscv/kvm/main.c +1 -1
-	kvm_info("using %s G-stage page table format\n", str);
+	kvm_info("Max G-stage page table format %s\n", str);

Fun fact: the ISA doesn't define the same hierarchy for hgatp modes as it does for satp modes, so we could have just Sv57x4 and nothing below.

Quoted 5 lines

We could do just with a code comment that we're assuming vendors will do better, but I'd rather not introduce more assumptions...
I think the easiest would be to kvm_riscv_gstage_mode_detect() levels in reverse and stop on the first one that is not supported. (I'll reply with a patch later.)

Quoted 3 lines

Please refer to the discussion here:
https://github.com/riscv/riscv-isa-manual/issues/2208
If Sv57x4 is implemented, then Sv48x4 and Sv39x4 must also be implemented.

I don't think so, sadly, but we're mostly dealing with technicalities here. As Andrew pointed out:

"The H extension itself does not impose this requirement, so technically Sv57x4 without Sv48x4 conforms to the H extension spec."

This means it's completely valid to support {Bare, Sv39x4, Sv57x4}. The RVA23 profile imposes additional constraints via Shgatpa:

"For each supported virtual memory scheme SvNN supported in satp, the corresponding hgatp SvNNx4 mode must be supported.
The hgatp mode Bare must also be supported."

Quoted 6 lines

The requirement only goes one way, so an RVA23 implementation with just {Bare, Sv39} in satp could support {Bare, Sv39x4, Sv57x4} in hgatp, because RVA23 nor ISA prevent Sv57x4 to be there.
Not that I expect any sensible implementation to do this...

Btw. do we target only RVA23 with KVM?

Thanks for the clarification.

No, I don't think we should assume we're targeting only RVA23 (or any specific profile) with KVM. In general KVM should work with any H-extension implementation that satisfies KVM’s requirements, without depending on additional profile constraints unless explicitly stated.

Given that, relying on “if Sv57x4 exists then Sv48x4/Sv39x4 must exist” would be an extra assumption. To avoid that, I’ll update the detection logic to probe modes independently and record all supported modes (and derive the max level from the resulting set by default).

Also, I will expose the full set of detected host-supported HGATP.MODE values to userspace via KVM_CHECK_EXTENSION(KVM_CAP_RISCV_SET_HGATP_MODE) as a bitmask, so userspace can select an appropriate mode.

Quoted 2 lines

Thanks

Thanks,
Fangyu

#7 lore raw

PATCH v3 2/2 RISC-V: KVM: add KVM_CAP_RISCV_SET_HGATP_MODE

In reply to [email protected]

From: Fangyu Yu <[email protected]>

This capability allows userspace to explicitly select the HGATP mode for the VM. The selected mode must be less than or equal to the max HGATP mode supported by the hardware. This capability must be enabled before creating any vCPUs, and can only be set once per VM.

---
 Documentation/virt/kvm/api.rst | 18 ++++++++++++++++++
 arch/riscv/kvm/vm.c            | 26 ++++++++++++++++++++++++--
 include/uapi/linux/kvm.h       |  1 +
 3 files changed, 43 insertions(+), 2 deletions(-)
Documentation/virt/kvm/api.rst +18 -0
index 01a3abef8abb..9d0794b174c7 100644
@@ -8765,6 +8765,24 @@ helpful if user space wants to emulate instructions which are not
 This capability can be enabled dynamically even if VCPUs were already
 created and are running.
 
+7.47 KVM_CAP_RISCV_SET_HGATP_MODE
+---------------------------------
+
+:Architectures: riscv
+:Type: VM
+:Parameters: args[0] contains the requested HGATP mode
+:Returns:
+  - 0 on success.
+  - -EINVAL if args[0] is outside the range of HGATP modes supported by the
+    hardware.
+  - -EBUSY if vCPUs have already been created for the VM, if the VM has any
+    non-empty memslots, or if the capability has already been set for the VM.
+
+This capability allows userspace to explicitly select the HGATP mode for
+the VM. The selected mode must be less than or equal to the maximum HGATP
+mode supported by the hardware. This capability must be enabled before
+creating any vCPUs, and can only be set once per VM.
+
 8. Other capabilities.
 ======================
 
arch/riscv/kvm/vm.c +24 -2
index 4b2156df40fc..7bc9b193dcaa 100644
@@ -202,6 +202,9 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
 	case KVM_CAP_VM_GPA_BITS:
 		r = kvm_riscv_gstage_gpa_bits(&kvm->arch);
 		break;
+	case KVM_CAP_RISCV_SET_HGATP_MODE:
+		r = IS_ENABLED(CONFIG_64BIT) ? 1 : 0;
+		break;
 	default:
 		r = 0;
 		break;
@@ -212,12 +215,31 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
 
 int kvm_vm_ioctl_enable_cap(struct kvm *kvm, struct kvm_enable_cap *cap)
 {
+	if (cap->flags)
+		return -EINVAL;
+
 	switch (cap->cap) {
 	case KVM_CAP_RISCV_MP_STATE_RESET:
-		if (cap->flags)
-			return -EINVAL;
 		kvm->arch.mp_state_reset = true;
 		return 0;
+	case KVM_CAP_RISCV_SET_HGATP_MODE:
+#ifdef CONFIG_64BIT
+		if (cap->args[0] < HGATP_MODE_SV39X4 ||
+		    cap->args[0] > kvm_riscv_gstage_mode(kvm_riscv_gstage_max_pgd_levels))
+			return -EINVAL;
+
+		if (kvm->arch.gstage_mode_user_initialized || kvm->created_vcpus ||
+		    !kvm_are_all_memslots_empty(kvm))
+			return -EBUSY;
+
+		kvm->arch.gstage_mode_user_initialized = true;
+		kvm->arch.kvm_riscv_gstage_pgd_levels =
+				3 + cap->args[0] - HGATP_MODE_SV39X4;
+		kvm_debug("VM (vmid:%lu) using SV%lluX4 G-stage page table format\n",
+			  kvm->arch.vmid.vmid,
+			  39 + (cap->args[0] - HGATP_MODE_SV39X4) * 9);
+#endif
+		return 0;
 	default:
 		return -EINVAL;
 	}
include/uapi/linux/kvm.h +1 -0
index dddb781b0507..00c02a880518 100644
@@ -974,6 +974,7 @@ struct kvm_enable_cap {
 #define KVM_CAP_GUEST_MEMFD_FLAGS 244
 #define KVM_CAP_ARM_SEA_TO_USER 245
 #define KVM_CAP_S390_USER_OPEREXEC 246
+#define KVM_CAP_RISCV_SET_HGATP_MODE 247
 
 struct kvm_irq_routing_irqchip {
 	__u32 irqchip;
Signature · 1 line
2.50.1
#8 lore raw

Re: [PATCH v3 2/2] RISC-V: KVM: add KVM_CAP_RISCV_SET_HGATP_MODE

Quoted 30 lines

From: Fangyu Yu <[email protected]>

This capability allows userspace to explicitly select the HGATP mode for the VM. The selected mode must be less than or equal to the max HGATP mode supported by the hardware. This capability must be enabled before creating any vCPUs, and can only be set once per VM.

---

Documentation/virt/kvm/api.rst | 18 ++++++++++++++++++
arch/riscv/kvm/vm.c            | 26 ++++++++++++++++++++++++--
include/uapi/linux/kvm.h       |  1 +
3 files changed, 43 insertions(+), 2 deletions(-)
Documentation/virt/kvm/api.rst +0 -0
index 01a3abef8abb..9d0794b174c7 100644
@@ -8765,6 +8765,24 @@ helpful if user space wants to emulate instructions which are not

This capability can be enabled dynamically even if VCPUs were already created and are running.

+7.47 KVM_CAP_RISCV_SET_HGATP_MODE
+---------------------------------

Documentation/virt/kvm/api.rst +6 -0
+
+:Architectures: riscv
+:Type: VM
+:Parameters: args[0] contains the requested HGATP mode
+:Returns:
+  - 0 on success.

Documentation/virt/kvm/api.rst helpful if user space wants to emulate instructions which are not · line 8785

Quoted 10 lines
Documentation/virt/kvm/api.rst +10 -0
+  - -EINVAL if args[0] is outside the range of HGATP modes supported by the
+    hardware.
+  - -EBUSY if vCPUs have already been created for the VM, if the VM has any
+    non-empty memslots, or if the capability has already been set for the VM.
+
+This capability allows userspace to explicitly select the HGATP mode for
+the VM. The selected mode must be less than or equal to the maximum HGATP
+mode supported by the hardware. This capability must be enabled before
+creating any vCPUs, and can only be set once per VM.
+

Hi Andrew:

In v3,no changes were made here, as KVM_CAP_RISCV_SET_HGATP_MODE might be appropriate, just like the no5lvl and no4lvl in the kernel. If you think it's needed, I'm happy to discuss this further.

Thanks,
Fangyu

Quoted 65 lines

8. Other capabilities.
======================

arch/riscv/kvm/vm.c +24 -2
index 4b2156df40fc..7bc9b193dcaa 100644
@@ -202,6 +202,9 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
 	case KVM_CAP_VM_GPA_BITS:
 		r = kvm_riscv_gstage_gpa_bits(&kvm->arch);
 		break;
+	case KVM_CAP_RISCV_SET_HGATP_MODE:
+		r = IS_ENABLED(CONFIG_64BIT) ? 1 : 0;
+		break;
 	default:
 		r = 0;
 		break;
@@ -212,12 +215,31 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
 
 int kvm_vm_ioctl_enable_cap(struct kvm *kvm, struct kvm_enable_cap *cap)
 {
+	if (cap->flags)
+		return -EINVAL;
+
 	switch (cap->cap) {
 	case KVM_CAP_RISCV_MP_STATE_RESET:
-		if (cap->flags)
-			return -EINVAL;
 		kvm->arch.mp_state_reset = true;
 		return 0;
+	case KVM_CAP_RISCV_SET_HGATP_MODE:
+#ifdef CONFIG_64BIT
+		if (cap->args[0] < HGATP_MODE_SV39X4 ||
+		    cap->args[0] > kvm_riscv_gstage_mode(kvm_riscv_gstage_max_pgd_levels))
+			return -EINVAL;
+
+		if (kvm->arch.gstage_mode_user_initialized || kvm->created_vcpus ||
+		    !kvm_are_all_memslots_empty(kvm))
+			return -EBUSY;
+
+		kvm->arch.gstage_mode_user_initialized = true;
+		kvm->arch.kvm_riscv_gstage_pgd_levels =
+				3 + cap->args[0] - HGATP_MODE_SV39X4;
+		kvm_debug("VM (vmid:%lu) using SV%lluX4 G-stage page table format\n",
+			  kvm->arch.vmid.vmid,
+			  39 + (cap->args[0] - HGATP_MODE_SV39X4) * 9);
+#endif
+		return 0;
 	default:
 		return -EINVAL;
 	}
include/uapi/linux/kvm.h +1 -0
index dddb781b0507..00c02a880518 100644
@@ -974,6 +974,7 @@ struct kvm_enable_cap {
 #define KVM_CAP_GUEST_MEMFD_FLAGS 244
 #define KVM_CAP_ARM_SEA_TO_USER 245
 #define KVM_CAP_S390_USER_OPEREXEC 246
+#define KVM_CAP_RISCV_SET_HGATP_MODE 247
 
 struct kvm_irq_routing_irqchip {
 	__u32 irqchip;
Signature · 1 line
2.50.1
#9 lore raw

Re: [PATCH v3 2/2] RISC-V: KVM: add KVM_CAP_RISCV_SET_HGATP_MODE

In reply to [email protected]
2026-01-25T23:04:50+08:00, <[email protected]>:
Quoted 22 lines

From: Fangyu Yu <[email protected]>

This capability allows userspace to explicitly select the HGATP mode for the VM. The selected mode must be less than or equal to the max HGATP mode supported by the hardware. This capability must be enabled before creating any vCPUs, and can only be set once per VM.

---
 Documentation/virt/kvm/api.rst | 18 ++++++++++++++++++
 arch/riscv/kvm/vm.c            | 26 ++++++++++++++++++++++++--
 include/uapi/linux/kvm.h       |  1 +
 3 files changed, 43 insertions(+), 2 deletions(-)
Documentation/virt/kvm/api.rst +6 -0
@@ -8765,6 +8765,24 @@ helpful if user space wants to emulate instructions which are not
+7.47 KVM_CAP_RISCV_SET_HGATP_MODE
+---------------------------------
+
+:Architectures: riscv
+:Type: VM
+:Parameters: args[0] contains the requested HGATP mode

Documentation/virt/kvm/api.rst helpful if user space wants to emulate instructions which are not · line 8780

Quoted 10 lines
Documentation/virt/kvm/api.rst +10 -0
+:Returns:
+  - 0 on success.
+  - -EINVAL if args[0] is outside the range of HGATP modes supported by the
+    hardware.
+  - -EBUSY if vCPUs have already been created for the VM, if the VM has any
+    non-empty memslots, or if the capability has already been set for the VM.
+
+This capability allows userspace to explicitly select the HGATP mode for
+the VM. The selected mode must be less than or equal to the maximum HGATP
+mode supported by the hardware.

"The selected mode must be supported by both KVM and hardware."

(The comparison is a technical detail, and incorrect too since the value
is bouded from the bottom as well.)

Documentation/virt/kvm/api.rst helpful if user space wants to emulate instructions which are not · line 8782

Quoted 2 lines
Documentation/virt/kvm/api.rst +1 -0
                                 This capability must be enabled before
+creating any vCPUs, and can only be set once per VM.

^ "or memslots"

arch/riscv/kvm/vm.c int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext) · line 206

Quoted 7 lines
arch/riscv/kvm/vm.c +2 -0
@@ -202,6 +202,9 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
 	case KVM_CAP_VM_GPA_BITS:
 		r = kvm_riscv_gstage_gpa_bits(&kvm->arch);
 		break;
+	case KVM_CAP_RISCV_SET_HGATP_MODE:
+		r = IS_ENABLED(CONFIG_64BIT) ? 1 : 0;

Maybe we can return the currently selected mode for a bit of extra info? Another nice option would be to return a bitmask of all supported modes.

I think userspace has otherwise no reason to call it, since it's fine to just try enable and handle the -EINVAL as "don't care".
1 syscall instead of 2.

Quoted 5 lines
arch/riscv/kvm/vm.c +1 -0
@@ -212,12 +215,31 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
 
 int kvm_vm_ioctl_enable_cap(struct kvm *kvm, struct kvm_enable_cap *cap)
 {
+	case KVM_CAP_RISCV_SET_HGATP_MODE:

arch/riscv/kvm/vm.c int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext) · line 228

Quoted 10 lines
arch/riscv/kvm/vm.c +10 -0
+#ifdef CONFIG_64BIT
+		if (cap->args[0] < HGATP_MODE_SV39X4 ||
+		    cap->args[0] > kvm_riscv_gstage_mode(kvm_riscv_gstage_max_pgd_levels))
+			return -EINVAL;
+
+		if (kvm->arch.gstage_mode_user_initialized || kvm->created_vcpus ||
+		    !kvm_are_all_memslots_empty(kvm))
+			return -EBUSY;
+
+		kvm->arch.gstage_mode_user_initialized = true;

No need to have gstage_mode_user_initialized, since if the user could have changed it once, there shouldn't be an issue in changing it again. It's the other protections that must work.

arch/riscv/kvm/vm.c int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext) · line 240

Quoted 5 lines
arch/riscv/kvm/vm.c +5 -0
+		kvm->arch.kvm_riscv_gstage_pgd_levels =
+				3 + cap->args[0] - HGATP_MODE_SV39X4;
+		kvm_debug("VM (vmid:%lu) using SV%lluX4 G-stage page table format\n",
+			  kvm->arch.vmid.vmid,
+			  39 + (cap->args[0] - HGATP_MODE_SV39X4) * 9);

(I don't think this debug message is going to be useful after a short
debugging period, and it would clog the log on each VM launch, so I'd rather get rid of it.)

Thanks.

#10 lore raw

Re: Re: [PATCH v3 2/2] RISC-V: KVM: add KVM_CAP_RISCV_SET_HGATP_MODE

Quoted 26 lines
Quoted 26 lines

From: Fangyu Yu <[email protected]>

This capability allows userspace to explicitly select the HGATP mode for the VM. The selected mode must be less than or equal to the max HGATP mode supported by the hardware. This capability must be enabled before creating any vCPUs, and can only be set once per VM.

---
 Documentation/virt/kvm/api.rst | 18 ++++++++++++++++++
 arch/riscv/kvm/vm.c            | 26 ++++++++++++++++++++++++--
 include/uapi/linux/kvm.h       |  1 +
 3 files changed, 43 insertions(+), 2 deletions(-)
Documentation/virt/kvm/api.rst +10 -0
@@ -8765,6 +8765,24 @@ helpful if user space wants to emulate instructions which are not
+7.47 KVM_CAP_RISCV_SET_HGATP_MODE
+---------------------------------
+
+:Architectures: riscv
+:Type: VM
+:Parameters: args[0] contains the requested HGATP mode
+:Returns:
+  - 0 on success.
+  - -EINVAL if args[0] is outside the range of HGATP modes supported by the
+    hardware.
Quoted 8 lines

Documentation/virt/kvm/api.rst helpful if user space wants to emulate instructions which are not

Quoted 6 lines
Documentation/virt/kvm/api.rst +6 -0
+  - -EBUSY if vCPUs have already been created for the VM, if the VM has any
+    non-empty memslots, or if the capability has already been set for the VM.
+
+This capability allows userspace to explicitly select the HGATP mode for
+the VM. The selected mode must be less than or equal to the maximum HGATP
+mode supported by the hardware.

"The selected mode must be supported by both KVM and hardware."

This description is clear, Agreed.

Quoted 7 lines

(The comparison is a technical detail, and incorrect too since the value is bouded from the bottom as well.)

Documentation/virt/kvm/api.rst helpful if user space wants to emulate instructions which are not · line 8782

Quoted 2 lines
Documentation/virt/kvm/api.rst +1 -0
                                 This capability must be enabled before
+creating any vCPUs, and can only be set once per VM.

^ "or memslots"

Right, thanks for catching that.

Quoted 12 lines

arch/riscv/kvm/vm.c int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext) · line 206

Quoted 8 lines
arch/riscv/kvm/vm.c +2 -0
@@ -202,6 +202,9 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
 	case KVM_CAP_VM_GPA_BITS:
 		r = kvm_riscv_gstage_gpa_bits(&kvm->arch);
 		break;
+	case KVM_CAP_RISCV_SET_HGATP_MODE:
+		r = IS_ENABLED(CONFIG_64BIT) ? 1 : 0;

Maybe we can return the currently selected mode for a bit of extra info? Another nice option would be to return a bitmask of all supported modes.

Quoted 3 lines

I think userspace has otherwise no reason to call it, since it's fine to just try enable and handle the -EINVAL as "don't care".
1 syscall instead of 2.

I’d prefer to keep an explicit KVM_CHECK_EXTENSION implementation for KVM_CAP_RISCV_SET_HGATP_MODE. Userspace commonly uses CHECK_EXTENSION for capability discovery, and returning 0 would make it assume the capability is unsupported even though KVM_ENABLE_CAP works.

Returning 1/0 should be sufficient here, as the actual mode support is validated by KVM_ENABLE_CAP itself (with -EINVAL for unsupported modes).

Quoted 11 lines
Quoted 11 lines
arch/riscv/kvm/vm.c +7 -0
@@ -212,12 +215,31 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
 
 int kvm_vm_ioctl_enable_cap(struct kvm *kvm, struct kvm_enable_cap *cap)
 {
+	case KVM_CAP_RISCV_SET_HGATP_MODE:
+#ifdef CONFIG_64BIT
+		if (cap->args[0] < HGATP_MODE_SV39X4 ||
+		    cap->args[0] > kvm_riscv_gstage_mode(kvm_riscv_gstage_max_pgd_levels))
+			return -EINVAL;
+
+		if (kvm->arch.gstage_mode_user_initialized || kvm->created_vcpus ||
Quoted 8 lines

arch/riscv/kvm/vm.c int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext) · line 235

Quoted 4 lines
arch/riscv/kvm/vm.c +4 -0
+		    !kvm_are_all_memslots_empty(kvm))
+			return -EBUSY;
+
+		kvm->arch.gstage_mode_user_initialized = true;

No need to have gstage_mode_user_initialized, since if the user could have changed it once, there shouldn't be an issue in changing it again. It's the other protections that must work.

Agreed — I'll drop gstage_mode_user_initialized. Userspace can change the mode multiple times before the VM is committed, and updates will be gated by the existing protections (i.e. once <vcpus created/ran | memslots active>, the mode change will be rejected).

Quoted 9 lines

arch/riscv/kvm/vm.c int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext) · line 240

Quoted 5 lines
arch/riscv/kvm/vm.c +5 -0
+		kvm->arch.kvm_riscv_gstage_pgd_levels =
+				3 + cap->args[0] - HGATP_MODE_SV39X4;
+		kvm_debug("VM (vmid:%lu) using SV%lluX4 G-stage page table format\n",
+			  kvm->arch.vmid.vmid,
+			  39 + (cap->args[0] - HGATP_MODE_SV39X4) * 9);

(I don't think this debug message is going to be useful after a short debugging period, and it would clog the log on each VM launch, so I'd rather get rid of it.)

I'll drop this kvm_debug() from the capability path to avoid spamming the log on each VM creation.

Quoted 1 line

Thanks.

Thanks,
Fangyu