Re: [PATCH v2] RISC-V: KVM: Account G-stage page table memory

From: Anup Patel

Date: Sun Nov 29 2026 - 23:57:38 EST


On Thu, Nov 26, 2026 at 7:07 AM Tan Chi <tanchi25@xxxxxxxxxxxxxxxx> wrote:
>
> RISC-V KVM currently does not account G-stage page table memory
> in NR_SECONDARY_PAGETABLE.
>
> Use kvm_account_pgtable_pages() to account for the allocation and
> freeing of the root and lower-level page tables, including those
> allocated when splitting huge page mappings.
>
> This makes G-stage page table memory usage visible through
> SecPageTables in /proc/meminfo and sec_pagetables in the cgroup
> memory.stat interface.
>
> Update the documentation accordingly.
>
> Tested on a RISC-V KVM host running under QEMU TCG with Sv57x4.
> The observed G-stage page table memory usage was:
>
> 100 empty VMs: 1600 KiB
> 8 GiB sparsely mapped with 4 KiB pages: 16448 KiB
> 256 MiB mapped with 2 MiB pages: 36 KiB
> After enabling dirty logging: 548 KiB
>
> The accounting in /proc/meminfo, /proc/vmstat and cgroup
> memory.stat matched the expected values and returned to zero
> after destroying the VMs.
>
> This reimplements the work originally proposed by Yosry Ahmed
> in 2022, adapted to the current G-stage MMU code.
>
> Link: https://lore.kernel.org/r/20220426053904.3684293-6-yosryahmed@xxxxxxxxxx
>
> Signed-off-by: Tan Chi <tanchi25@xxxxxxxxxxxxxxxx>

Please rebase this on latest riscv_kvm_queue and send v3

Regards,
Anup

> ---
> Changes in v2:
> - Use the allocation order to calculate the number of root page
> table pages, fixing a Clang warning reported by the kernel test
> robot.
> https://lore.kernel.org/oe-kbuild-all/20261126015340.s7bzBtEB-lkp@xxxxxxxxx/
>
> Documentation/admin-guide/cgroup-v2.rst | 4 ++--
> Documentation/filesystems/proc.rst | 2 +-
> arch/riscv/kvm/gstage.c | 6 +++++-
> arch/riscv/kvm/mmu.c | 12 ++++++++----
> 4 files changed, 16 insertions(+), 8 deletions(-)
>
> diff --git a/Documentation/admin-guide/cgroup-v2.rst b/Documentation/admin-guide/cgroup-v2.rst
> index 9e3c67b02ae2..c79d6432ecc3 100644
> --- a/Documentation/admin-guide/cgroup-v2.rst
> +++ b/Documentation/admin-guide/cgroup-v2.rst
> @@ -1591,8 +1591,8 @@ The following nested keys are defined.
>
> sec_pagetables
> Amount of memory allocated for secondary page tables,
> - this currently includes KVM mmu allocations on x86
> - and arm64 and IOMMU page tables.
> + this currently includes KVM mmu allocations on x86,
> + arm64 and riscv and IOMMU page tables.
>
> percpu (npn)
> Amount of memory used for storing per-cpu kernel
> diff --git a/Documentation/filesystems/proc.rst b/Documentation/filesystems/proc.rst
> index fa7a468e8edf..cd1a74da287c 100644
> --- a/Documentation/filesystems/proc.rst
> +++ b/Documentation/filesystems/proc.rst
> @@ -1208,7 +1208,7 @@ PageTables
> Memory consumed by userspace page tables
> SecPageTables
> Memory consumed by secondary page tables, this currently includes
> - KVM mmu and IOMMU allocations on x86 and arm64.
> + KVM mmu and IOMMU allocations on x86, arm64 and riscv.
> NFS_Unstable
> Always zero. Previously counted pages which had been written to
> the server, but has not been committed to stable storage.
> diff --git a/arch/riscv/kvm/gstage.c b/arch/riscv/kvm/gstage.c
> index 0aace3215d3e..f8841136d852 100644
> --- a/arch/riscv/kvm/gstage.c
> +++ b/arch/riscv/kvm/gstage.c
> @@ -162,6 +162,7 @@ int kvm_riscv_gstage_set_pte(struct kvm_gstage *gstage,
> return -ENOMEM;
> set_pte(ptep, pfn_pte(PFN_DOWN(__pa(next_ptep)),
> __pgprot(_PAGE_TABLE)));
> + kvm_account_pgtable_pages(next_ptep, 1);
> } else {
> if (gstage_pte_leaf(ptep))
> return -EEXIST;
> @@ -357,6 +358,7 @@ bool kvm_riscv_gstage_split_huge(struct kvm_gstage *gstage,
>
> set_pte(ptep, pfn_pte(PFN_DOWN(__pa(next_ptep)),
> __pgprot(_PAGE_TABLE)));
> + kvm_account_pgtable_pages(next_ptep, 1);
>
> if (flush)
> gstage_tlb_flush(gstage, current_level, addr);
> @@ -399,8 +401,10 @@ bool kvm_riscv_gstage_op_pte(struct kvm_gstage *gstage, gpa_t addr,
> for (i = 0; i < PTRS_PER_PTE; i++)
> flush |= kvm_riscv_gstage_op_pte(gstage, addr + i * next_page_size,
> &next_ptep[i], next_ptep_level, op);
> - if (op == GSTAGE_OP_CLEAR)
> + if (op == GSTAGE_OP_CLEAR) {
> + kvm_account_pgtable_pages(next_ptep, -1);
> put_page(virt_to_page(next_ptep));
> + }
> } else {
> old_pte = *ptep;
> if (op == GSTAGE_OP_CLEAR)
> diff --git a/arch/riscv/kvm/mmu.c b/arch/riscv/kvm/mmu.c
> index 6861c0e2728d..ab8d8c94a866 100644
> --- a/arch/riscv/kvm/mmu.c
> +++ b/arch/riscv/kvm/mmu.c
> @@ -805,17 +805,18 @@ long kvm_arch_vcpu_pre_fault_memory(struct kvm_vcpu *vcpu,
> int kvm_riscv_mmu_alloc_pgd(struct kvm *kvm)
> {
> struct page *pgd_page;
> + int order = get_order(kvm_riscv_gstage_pgd_size);
>
> if (kvm->arch.pgd != NULL) {
> kvm_err("kvm_arch already initialized?\n");
> return -EINVAL;
> }
>
> - pgd_page = alloc_pages(GFP_KERNEL_ACCOUNT | __GFP_ZERO,
> - get_order(kvm_riscv_gstage_pgd_size));
> + pgd_page = alloc_pages(GFP_KERNEL_ACCOUNT | __GFP_ZERO, order);
> if (!pgd_page)
> return -ENOMEM;
> kvm->arch.pgd = page_to_virt(pgd_page);
> + kvm_account_pgtable_pages(kvm->arch.pgd, 1 << order);
> kvm->arch.pgd_phys = page_to_phys(pgd_page);
> kvm->arch.pgd_levels = kvm_riscv_gstage_max_pgd_levels;
> kvm->arch.pgd_split_page_cache.gfp_zero = __GFP_ZERO;
> @@ -828,6 +829,7 @@ void kvm_riscv_mmu_free_pgd(struct kvm *kvm)
> struct kvm_gstage gstage;
> void *pgd = NULL;
> bool flush = false;
> + int order = get_order(kvm_riscv_gstage_pgd_size);
>
> write_lock(&kvm->mmu_lock);
> if (kvm->arch.pgd) {
> @@ -843,8 +845,10 @@ void kvm_riscv_mmu_free_pgd(struct kvm *kvm)
> if (flush)
> kvm_flush_remote_tlbs(kvm);
>
> - if (pgd)
> - free_pages((unsigned long)pgd, get_order(kvm_riscv_gstage_pgd_size));
> + if (pgd) {
> + kvm_account_pgtable_pages(pgd, -(1 << order));
> + free_pages((unsigned long)pgd, order);
> + }
> }
>
> void kvm_riscv_mmu_update_hgatp(struct kvm_vcpu *vcpu)
>
> base-commit: 4dce97c6c265544342e716e62fd24bbb232590c5
> --
> 2.53.0
>