[RFC PATCH 0/3] KVM: riscv: Reclaim unused G-stage page tables
Tan Chi
tanchi25 at mails.ucas.ac.cn
Sun Nov 29 06:22:49 PST 2026
The current G-stage unmap implementation retains unused page tables
until the VM is destroyed.
This series introduces a recursive page table walker, similar to the
arm64 stage-2 page table walker, as a basis for reclaiming unused
G-stage page tables.
Different reclamation policies are used depending on the unmap context:
- Memslot removal: reclaim fully covered page tables that are empty
after unmapping.
- MMU notifier invalidation: reclaim fully covered page tables that
were already empty before unmapping.
- I/O unmapping: keep the existing behavior, as the current use case
only unmaps individual IMSIC pages.
A similar selective reclamation approach has been proposed for the
x86 TDP MMU [1].
Testing
=======
Tests were performed on a RISC-V KVM host running under QEMU TCG
with 2 vCPUs and 2 GiB of memory.
Performance
-----------
Each memslot_perf_test subtest was run in three interleaved
baseline/patched pairs with the slot limit set to 32.
The following table shows the mean time per iteration and the
range of individual paired percentage changes.
Test Before After Change Range
----------------------------------------------------------------------
Map 15.710 s 16.338 s +4.00% -2.32% to +8.47%
Single-page unmap 11.664 s 12.006 s +2.93% -4.16% to +8.04%
Chunked unmap 45.990 ms 45.299 ms -1.50% -2.37% to -1.01%
Move active 15.414 ms 15.186 ms -1.48% -6.86% to +1.50%
Move inactive 13.599 ms 12.688 ms -6.70% -12.57% to -2.70%
RW 169.537 s 172.291 s +1.62% -3.76% to +5.73%
Positive values indicate increased execution time. The RW subtest
completed one iteration per run.
Page table reclamation and the additional TLB synchronization
required before freeing detached page tables introduce overhead
that may affect performance in some workloads.
Memory reclamation
------------------
With one 4 KiB mapping per 2 MiB of GPA space, memslot removal reduced
G-stage page table memory usage from 2084 KiB to 32 KiB for a 1 GiB
memslot, and from 16448 KiB to 32 KiB for an 8 GiB memslot.
For a 1 GiB sparse mapping repeatedly invalidated and accessed again,
the remaining page table memory usage was:
Hot region Before After Reduction
------------------------------------------------
10% 2084 KiB 240 KiB 88.5%
50% 2084 KiB 1060 KiB 49.1%
90% 2084 KiB 1876 KiB 10.0%
Memory usage was measured with additional G-stage page table accounting.
RFC notes
------------------
The mean single-page unmap time increased, although the results
varied across runs. Obtaining consistent performance results under
QEMU TCG has been difficult. I will try to address this issue in
the next revision.
To mitigate the single-page unmap overhead, the current implementation
retains a separate leaf lookup path in kvm_riscv_gstage_unmap_range().
Suggestions on reducing this overhead while sharing the recursive
walker would be appreciated.
Feedback on the reclamation policies and other aspects of the design
is also very welcome.
[1] https://lore.kernel.org/kvm/apVOV56MX4p4anIM@192.168.1.215/
Tan Chi (3):
KVM: riscv: Introduce a recursive G-stage unmap walker
KVM: riscv: Reclaim G-stage page tables on memslot removal
KVM: riscv: Selectively reclaim G-stage page tables on invalidation
arch/riscv/include/asm/kvm_gstage.h | 14 ++-
arch/riscv/kvm/gstage.c | 180 ++++++++++++++++++++++++----
arch/riscv/kvm/mmu.c | 8 +-
3 files changed, 174 insertions(+), 28 deletions(-)
base-commit: bc069e58fa1c1da46f7601840f2079a381370893
--
2.53.0
More information about the linux-riscv
mailing list