Skip to contentSkip to conversation

RISC-V: KVM: Fix hugepage mapping handling during dirty logging

PATCH Superseded [email protected] 2 participants 5 messages Mar 4 – Mar 6

Subsystems (3)

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

v2 cover RISC-V: KVM: Fix hugepage mapping handling during dirty logging

From: Wang Yechao <[email protected]>

This series fixes an issue where dirty logging fails to work correctly with huge pages.

v1 -> v2:
- fix the lost write protection on huge pages
- split huge pages before handling the write protection fault.

- Link to v1:
https://lore.kernel.org/linux-riscv/[email protected]/T/#u

Wang Yechao (3):
RISC-V: KVM: Fix lost write protection on huge pages during dirty
logging
RISC-V: KVM: Allow splitting huge pages to arbitrary level
RISC-V: KVM: Split huge pages during fault handling for dirty logging

 arch/riscv/include/asm/kvm_gstage.h |  4 ++
 arch/riscv/kvm/gstage.c             | 85 +++++++++++++++++++++++++++--
 2 files changed, 85 insertions(+), 4 deletions(-)
Signature · 1 line
2.47.3
#1 lore raw

PATCH v2 1/3 RISC-V: KVM: Fix lost write protection on huge pages during dirty logging

From: Wang Yechao <[email protected]>

When enabling dirty log in small chunks (e.g., QEMU default chunk size of 256K), the chunk size is always smaller than the page size of huge pages (1G or 2M) used in the gstage page tables. This caused the write protection to be incorrectly skipped for huge PTEs because the condition (end - addr) >= page_size was not satisfied.

Remove the size check in kvm_riscv_gstage_wp_range() to ensure huge PTEs are always write-protected regardless of the chunk size. Additionally, explicitly align the address down to the page size before invoking kvm_riscv_gstage_op_pte() to guarantee that the address passed to the operation function is page-aligned.

This fixes the issue where dirty pages might not be tracked correctly when using huge pages.

---
 arch/riscv/kvm/gstage.c | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)
arch/riscv/kvm/gstage.c +3 -4
index b67d60d722c2..d2001d508046 100644
@@ -304,10 +304,9 @@ void kvm_riscv_gstage_wp_range(struct kvm_gstage *gstage, gpa_t start, gpa_t end
 		if (!found_leaf)
 			goto next;
 
-		if (!(addr & (page_size - 1)) && ((end - addr) >= page_size))
-			kvm_riscv_gstage_op_pte(gstage, addr, ptep,
-						ptep_level, GSTAGE_OP_WP);
-
+		addr = ALIGN_DOWN(addr, page_size);
+		kvm_riscv_gstage_op_pte(gstage, addr, ptep,
+					ptep_level, GSTAGE_OP_WP);
 next:
 		addr += page_size;
 	}
Signature · 1 line
2.47.3
Reviewed-by on v2 1/3
#2 lore raw

Re: [PATCH v2 1/3] RISC-V: KVM: Fix lost write protection on huge pages during dirty logging

Quoted 8 lines · On 3/4/2026 5:26 PM, [email protected] wrote:

From: Wang Yechao <[email protected]>

When enabling dirty log in small chunks (e.g., QEMU default chunk size of 256K), the chunk size is always smaller than the page size of huge pages (1G or 2M) used in the gstage page tables. This caused the write protection to be incorrectly skipped for huge PTEs because the condition (end - addr) >= page_size was not satisfied.

Quoted 8 lines

Remove the size check in kvm_riscv_gstage_wp_range() to ensure huge PTEs are always write-protected regardless of the chunk size. Additionally, explicitly align the address down to the page size before invoking kvm_riscv_gstage_op_pte() to guarantee that the address passed to the operation function is page-aligned.

This fixes the issue where dirty pages might not be tracked correctly when using huge pages.

Would it be better to add a description of which specific commit is fixed  like  "Fixes: <id>..." ?

Otherwise,

Thanks,
Nutty

Quoted 23 lines
---
  arch/riscv/kvm/gstage.c | 7 +++----
  1 file changed, 3 insertions(+), 4 deletions(-)
arch/riscv/kvm/gstage.c +3 -4
index b67d60d722c2..d2001d508046 100644
@@ -304,10 +304,9 @@ void kvm_riscv_gstage_wp_range(struct kvm_gstage *gstage, gpa_t start, gpa_t end
  		if (!found_leaf)
  			goto next;
 
-		if (!(addr & (page_size - 1)) && ((end - addr) >= page_size))
-			kvm_riscv_gstage_op_pte(gstage, addr, ptep,
-						ptep_level, GSTAGE_OP_WP);
-
+		addr = ALIGN_DOWN(addr, page_size);
+		kvm_riscv_gstage_op_pte(gstage, addr, ptep,
+					ptep_level, GSTAGE_OP_WP);
  next:
  		addr += page_size;
  	}
#3 lore raw

PATCH v2 2/3 RISC-V: KVM: Allow splitting huge pages to arbitrary level

In reply to [email protected]

From: Wang Yechao <[email protected]>

This patch introduces the function kvm_riscv_gstage_split_huge(). It splits the huge page covering a given guest physical address down to a specified target level (e.g., from 1G to 2M or 4K). The caller provides a memory cache for allocating any intermediate page tables and may request a TLB flush after the split.

This functionality will be used by subsequent patches to split huge pages before handling the write-protection fault, or for other operations that require page-level granularity.

---
 arch/riscv/include/asm/kvm_gstage.h |  4 ++
 arch/riscv/kvm/gstage.c             | 69 +++++++++++++++++++++++++++++
 2 files changed, 73 insertions(+)
arch/riscv/include/asm/kvm_gstage.h +4 -0
index 595e2183173e..373748c6745e 100644
@@ -53,6 +53,10 @@ int kvm_riscv_gstage_map_page(struct kvm_gstage *gstage,
 			      bool page_rdonly, bool page_exec,
 			      struct kvm_gstage_mapping *out_map);
 
+int kvm_riscv_gstage_split_huge(struct kvm_gstage *gstage,
+                                struct kvm_mmu_memory_cache *pcache,
+                                gpa_t addr, u32 target_level, bool flush);
+
 enum kvm_riscv_gstage_op {
 	GSTAGE_OP_NOP = 0,	/* Nothing */
 	GSTAGE_OP_CLEAR,	/* Clear/Unmap */
arch/riscv/kvm/gstage.c +69 -0

75 lines not on this page · raw message

Signature · 1 line
2.47.3
#4 lore raw

PATCH v2 3/3 RISC-V: KVM: Split huge pages during fault handling for dirty logging

In reply to [email protected]

From: Wang Yechao <[email protected]>

During dirty logging, all huge pages are write-protected. When the guest writes to a write-protected huge page, a page fault is triggered. Before recovering the write permission, the huge page must be split into smaller pages (e.g., 4K). After splitting, the normal mapping process proceeds, allowing write permission to be restored at the smaller page granularity.

This ensures that dirty logging works correctly with huge pages.

---
 arch/riscv/kvm/gstage.c | 9 +++++++++
 1 file changed, 9 insertions(+)
arch/riscv/kvm/gstage.c +9 -0
index 92331e9b0bb8..27ea6bb81553 100644
@@ -171,6 +171,9 @@ int kvm_riscv_gstage_map_page(struct kvm_gstage *gstage,
 {
 	pgprot_t prot;
 	int ret;
+	pte_t *ptep;
+	u32 ptep_level;
+	bool found_leaf;
 
 	out_map->addr = gpa;
 	out_map->level = 0;
@@ -179,6 +182,12 @@ int kvm_riscv_gstage_map_page(struct kvm_gstage *gstage,
 	if (ret)
 		return ret;
 
+	found_leaf = kvm_riscv_gstage_get_leaf(gstage, gpa, &ptep, &ptep_level);
+	if (found_leaf && ptep_level > out_map->level) {
+		kvm_riscv_gstage_split_huge(gstage, pcache, gpa,
+					    out_map->level, true);
+	}
+
 	/*
 	 * A RISC-V implementation can choose to either:
 	 * 1) Update 'A' and 'D' PTE bits in hardware
Signature · 1 line
2.47.3