* [PATCH] RISC-V: KVM: Account G-stage page table memory
@ 2026-11-25 12:50 Tan Chi
2026-11-25 13:10 ` sashiko-bot
0 siblings, 1 reply; 2+ messages in thread
From: Tan Chi @ 2026-11-25 12:50 UTC (permalink / raw)
To: anup
Cc: atish.patra, pjw, palmer, aou, alex, tj, hannes, mkoutny, corbet,
skhan, rdunlap, yosry, kvm, kvm-riscv, linux-riscv, cgroups,
linux-doc, linux-fsdevel, linux-kernel, Tan Chi
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@mails.ucas.ac.cn>
---
Documentation/admin-guide/cgroup-v2.rst | 4 ++--
Documentation/filesystems/proc.rst | 2 +-
arch/riscv/kvm/gstage.c | 6 +++++-
arch/riscv/kvm/mmu.c | 7 ++++++-
4 files changed, 14 insertions(+), 5 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..c7141395b9e3 100644
--- a/arch/riscv/kvm/mmu.c
+++ b/arch/riscv/kvm/mmu.c
@@ -816,6 +816,8 @@ int kvm_riscv_mmu_alloc_pgd(struct kvm *kvm)
if (!pgd_page)
return -ENOMEM;
kvm->arch.pgd = page_to_virt(pgd_page);
+ kvm_account_pgtable_pages(kvm->arch.pgd,
+ kvm_riscv_gstage_pgd_size >> PAGE_SHIFT);
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;
@@ -843,8 +845,11 @@ void kvm_riscv_mmu_free_pgd(struct kvm *kvm)
if (flush)
kvm_flush_remote_tlbs(kvm);
- if (pgd)
+ if (pgd) {
+ kvm_account_pgtable_pages(pgd,
+ -(kvm_riscv_gstage_pgd_size >> PAGE_SHIFT));
free_pages((unsigned long)pgd, get_order(kvm_riscv_gstage_pgd_size));
+ }
}
void kvm_riscv_mmu_update_hgatp(struct kvm_vcpu *vcpu)
--
2.53.0
^ permalink raw reply related [flat|nested] 2+ messages in thread
* Re: [PATCH] RISC-V: KVM: Account G-stage page table memory
2026-11-25 12:50 [PATCH] RISC-V: KVM: Account G-stage page table memory Tan Chi
@ 2026-11-25 13:10 ` sashiko-bot
0 siblings, 0 replies; 2+ messages in thread
From: sashiko-bot @ 2026-11-25 13:10 UTC (permalink / raw)
To: Tan Chi; +Cc: kvm, tj, hannes, mkoutny
Thank you for your contribution! Sashiko AI review found no new issues
in this patch, but noticed 1 related pre-existing issue it does not
introduce:
- [Critical] KVM: riscv: missing write memory barrier in
kvm_riscv_gstage_set_pte() [1]
--
commit 00af1817efd260bd8b07164e38fe69598a62a280
Author: Tan Chi <tanchi25@mails.ucas.ac.cn>
RISC-V: KVM: Account G-stage page table memory
This commit uses kvm_account_pgtable_pages() to account for the allocation
and freeing of RISC-V KVM G-stage root and lower-level page tables in
NR_SECONDARY_PAGETABLE.
Link: https://lore.kernel.org/r/20220426053904.3684293-6-yosryahmed@google.com
> diff --git a/arch/riscv/kvm/gstage.c b/arch/riscv/kvm/gstage.c
> index 0aace3215d3e8..f8841136d8526 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);
[Severity: Critical]
This problem wasn't introduced by this patch, but shouldn't there be an
smp_wmb() before set_pte() links next_ptep into ptep [1], as in
kvm_riscv_gstage_split_huge()?
set_pte() is a plain WRITE_ONCE() on RISC-V. Without a write memory
barrier after kvm_mmu_memory_cache_alloc() zeroes next_ptep, the store to
ptep can become visible to a concurrent hardware page table walker before
the zero-initialization of next_ptep completes, exposing uninitialized
entries as child PTEs.
[1] https://sashiko.dev/#/bug/linux-9339e577-5f31-42a2-a5ea-9844d30b43f8
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261125125024.1983082-1-tanchi25@mails.ucas.ac.cn?part=1
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2026-11-25 13:10 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-25 12:50 [PATCH] RISC-V: KVM: Account G-stage page table memory Tan Chi
2026-11-25 13:10 ` sashiko-bot
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox