[PATCH 0/2] RISC-V: KVM: Expose G-stage page table and mapping stats

From: Bingyu.Xian

Date: Fri Dec 04 2026 - 08:27:17 EST


From: Bingyu Xian <shanbeeyoo@xxxxxxxxx>

This series exposes per-VM G-stage page table and leaf mapping page counts
through the KVM binary stats interface on RISC-V, together with a KVM
selftest validating the counters across mapping, write-protection, huge
page splitting, and memslot removal.

While commit a92e6041c5e5 ("RISC-V: KVM: Account G-stage page table
memory") accounts G-stage page table allocations globally and per cgroup
via NR_SECONDARY_PAGETABLE (SecPageTables), userspace VMMs still cannot
inspect per-VM G-stage page table consumption or leaf mapping sizes
through KVM_GET_STATS_FD because kvm_vm_stats_desc[] on RISC-V currently
exposes only KVM_GENERIC_VM_STATS().

Patch 1 adds per-VM instantaneous counters (pgtable_pages, pages_4k,
pages_2m, pages_1g on RV64; pgtable_pages, pages_4k, pages_4m on RV32) to
struct kvm_vm_stat and kvm_vm_stats_desc[]. All updates happen under
write_lock(&kvm->mmu_lock) (or during PGD initialization), skip local
G-stages marked with KVM_GSTAGE_FLAGS_LOCAL, and decrement pgtable_pages
synchronously when page tables are unlinked rather than inside RCU free
callbacks. Matching x86's pages_4k/pages_2m/pages_1g naming also prepares
RISC-V for selftests such as dirty_log_page_splitting_test.

Patch 2 adds tools/testing/selftests/kvm/riscv/gstage_stats_test.c, which
verifies root PGD accounting on empty VMs, 4K pre-faulting, repeated
faulting and dirty-log write-protection without counter drift, 2M THP
mapping and eager page splitting (2M -> 512 x 4K, pgtable_pages +1) when
backed by THP and kvm.eager_page_split is enabled, and memslot removal.

Validation:
- Rebased on riscv_kvm_next (ffdc855900a8).
- RV64 full kernel Image + arch/riscv/kvm/kvm.ko (W=1): 0 errors, 0 warnings.
- RV32 rv32_defconfig + CONFIG_KVM=m (W=1 on vm.o/gstage.o/mmu.o): 0 errors,
0 warnings.
- QEMU 10.0.2 (rv64,h=true,sstc=true) L1 KVM runtime test:
* kvm_binary_stats_test 2 2: 2/2 pass
* riscv/gstage_stats_test: 2/2 pass (4k_mapping_and_unmap and
2m_thp_and_dirty_log_split), 0 kernel warnings.

Thanks,
Bingyu

Bingyu.Xian (2):
RISC-V: KVM: Expose G-stage page table and mapping stats
KVM: selftests: riscv: Add G-stage binary stats test

arch/riscv/include/asm/kvm_host.h | 8 +
arch/riscv/kvm/gstage.c | 60 ++++++-
arch/riscv/kvm/mmu.c | 2 +
arch/riscv/kvm/vm.c | 10 +-
tools/testing/selftests/kvm/Makefile.kvm | 1 +
.../selftests/kvm/riscv/gstage_stats_test.c | 158 ++++++++++++++++++
6 files changed, 234 insertions(+), 5 deletions(-)
create mode 100644 tools/testing/selftests/kvm/riscv/gstage_stats_test.c


base-commit: ffdc855900a894034a63a46d699e0326dea3c991
--
2.34.1