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

Nutty.Liu nutty.liu at hotmail.com
Mon Nov 30 23:32:16 PST 2026


On 11/30/2026 10:36 PM, Tan Chi 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@google.com
>
> Signed-off-by: Tan Chi <tanchi25 at mails.ucas.ac.cn>
Reviewed-by: Nutty Liu <nutty.liu at hotmail.com>

Thanks,
Nutty
> ---
> Changes in v3:
> - Rebase on riscv_kvm_queue.
> - Move page table unaccounting to the RCU callbacks to match
>    the deferred freeing of G-stage page tables.
>
> 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@intel.com/
>
>   Documentation/admin-guide/cgroup-v2.rst | 4 ++--
>   Documentation/filesystems/proc.rst      | 2 +-
>   arch/riscv/kvm/gstage.c                 | 7 ++++++-
>   arch/riscv/kvm/mmu.c                    | 9 ++++++---
>   4 files changed, 15 insertions(+), 7 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 66e25b6b4743..241f4c19ca18 100644
> --- a/arch/riscv/kvm/gstage.c
> +++ b/arch/riscv/kvm/gstage.c
> @@ -24,7 +24,10 @@ unsigned long kvm_riscv_gstage_max_pgd_levels __ro_after_init = 2;
>   
>   static void gstage_free_page_table_rcu(struct rcu_head *head)
>   {
> -	put_page(container_of(head, struct page, rcu_head));
> +	struct page *page = container_of(head, struct page, rcu_head);
> +
> +	kvm_account_pgtable_pages(page_to_virt(page), -1);
> +	put_page(page);
>   }
>   
>   /*
> @@ -182,6 +185,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 {
>   			next_ptep = (pte_t *)gstage_pte_page_vaddr(pte);
>   		}
> @@ -379,6 +383,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);
> diff --git a/arch/riscv/kvm/mmu.c b/arch/riscv/kvm/mmu.c
> index dee36dc77c8b..e92d564f9c77 100644
> --- a/arch/riscv/kvm/mmu.c
> +++ b/arch/riscv/kvm/mmu.c
> @@ -802,17 +802,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;
> @@ -823,8 +824,10 @@ int kvm_riscv_mmu_alloc_pgd(struct kvm *kvm)
>   static void kvm_riscv_mmu_free_pgd_rcu(struct rcu_head *head)
>   {
>   	struct page *page = container_of(head, struct page, rcu_head);
> +	int order = get_order(kvm_riscv_gstage_pgd_size);
>   
> -	__free_pages(page, get_order(kvm_riscv_gstage_pgd_size));
> +	kvm_account_pgtable_pages(page_to_virt(page), -(1 << order));
> +	__free_pages(page, order);
>   }
>   
>   void kvm_riscv_mmu_free_pgd(struct kvm *kvm)
>
> base-commit: bd68956997dddcc3b1a7dd2fadadf584529027a1



More information about the linux-riscv mailing list