[RFC PATCH 3/3] KVM: riscv: Selectively reclaim G-stage page tables on invalidation

Tan Chi tanchi25 at mails.ucas.ac.cn
Sun Nov 29 06:22:52 PST 2026


Unlike memslot removal, MMU notifier invalidation may be followed by
accesses to the same guest physical addresses. Reclaiming page tables
whenever they become empty may therefore lead to repeated allocation
and freeing of page tables.

However, retaining empty page tables also requires subsequent
invalidations to traverse them even when there are no mappings left
to remove.

Introduce the EMPTY reclamation mode to reclaim fully covered page
table subtrees that contain no leaf mappings before unmapping, while
retaining page tables whose leaf mappings are removed by the current
walk.

Determine whether a page table can be reclaimed during the existing
walk without an additional scan.

A similar approach has been proposed for the x86 TDP MMU [1].

To compare EMPTY and ALL, create a 1 GiB sparse mapping with one
4 KiB mapping per 2 MiB of GPA space. Repeatedly invalidate the
entire range and access only the hot region after each invalidation.

The number of L0 page table allocations across six hot-region
remappings was:

    Hot region       EMPTY       ALL
    --------------------------------
    10%                  0       306
    50%                  0      1530
    90%                  0      2760

Both policies achieved the same page table memory usage after
hot-region remapping in these tests, but EMPTY avoided repeated
allocation of hot page tables.

[1] https://lore.kernel.org/kvm/apVOV56MX4p4anIM@192.168.1.215/

Signed-off-by: Tan Chi <tanchi25 at mails.ucas.ac.cn>
---
 arch/riscv/include/asm/kvm_gstage.h | 2 ++
 arch/riscv/kvm/gstage.c             | 3 +++
 arch/riscv/kvm/mmu.c                | 2 +-
 3 files changed, 6 insertions(+), 1 deletion(-)

diff --git a/arch/riscv/include/asm/kvm_gstage.h b/arch/riscv/include/asm/kvm_gstage.h
index 97fc376ecebe..ffc14b2842b0 100644
--- a/arch/riscv/include/asm/kvm_gstage.h
+++ b/arch/riscv/include/asm/kvm_gstage.h
@@ -11,10 +11,12 @@
 
 /*
  * NONE: Do not reclaim page tables.
+ * EMPTY: Reclaim fully covered tables that were already empty.
  * ALL: Reclaim fully covered tables that become empty after unmapping.
  */
 enum kvm_gstage_reclaim_mode {
 	KVM_GSTAGE_RECLAIM_NONE,
+	KVM_GSTAGE_RECLAIM_EMPTY,
 	KVM_GSTAGE_RECLAIM_ALL,
 };
 
diff --git a/arch/riscv/kvm/gstage.c b/arch/riscv/kvm/gstage.c
index c8b8297b6610..25f1bfae157f 100644
--- a/arch/riscv/kvm/gstage.c
+++ b/arch/riscv/kvm/gstage.c
@@ -500,6 +500,9 @@ static int gstage_unmap_walk(struct kvm_gstage *gstage,
 		if (!pte_val(pte)) {
 			*addr = ALIGN(*addr + 1, page_size);
 		} else if (gstage_pte_leaf(pte)) {
+			if (reclaim_mode == KVM_GSTAGE_RECLAIM_EMPTY)
+				reclaimable = false;
+
 			if (IS_ALIGNED(*addr, page_size) && end - *addr >= page_size) {
 				*flush |= kvm_riscv_gstage_op_pte(gstage, *addr, ptep,
 								level, GSTAGE_OP_CLEAR);
diff --git a/arch/riscv/kvm/mmu.c b/arch/riscv/kvm/mmu.c
index 9653a7c1e7cd..44ad0515e7b8 100644
--- a/arch/riscv/kvm/mmu.c
+++ b/arch/riscv/kvm/mmu.c
@@ -344,7 +344,7 @@ bool kvm_unmap_gfn_range(struct kvm *kvm, struct kvm_gfn_range *range)
 	kvm_riscv_gstage_init(&gstage, kvm);
 	flush = kvm_riscv_gstage_unmap_range(&gstage, range->start << PAGE_SHIFT,
 					     (range->end - range->start) << PAGE_SHIFT,
-					     range->may_block, KVM_GSTAGE_RECLAIM_NONE);
+					     range->may_block, KVM_GSTAGE_RECLAIM_EMPTY);
 	if (flush)
 		kvm_flush_remote_tlbs_range(kvm, range->start,
 					    range->end - range->start);
-- 
2.53.0




More information about the linux-riscv mailing list