mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] RISC-V: KVM: Account G-stage page table memory
@ 2026-11-25 12:50 Tan Chi
  2026-11-25 17:58 ` kernel test robot
  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	[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 17:58 ` kernel test robot
  0 siblings, 0 replies; 2+ messages in thread
From: kernel test robot @ 2026-11-25 17:58 UTC (permalink / raw)
  To: Tan Chi, anup
  Cc: llvm, oe-kbuild-all, 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

Hi Tan,

kernel test robot noticed the following build warnings:

[auto build test WARNING on kvm-riscv/riscv_kvm_next]
[also build test WARNING on linus/master next-20261124]
[cannot apply to tj-cgroup/for-next kvm/queue kvm/next mst-vhost/linux-next kvm/linux-next v7.4-rc4]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Tan-Chi/RISC-V-KVM-Account-G-stage-page-table-memory/20261125-205023
base:   https://github.com/kvm-riscv/linux.git riscv_kvm_next
patch link:    https://lore.kernel.org/r/20261125125024.1983082-1-tanchi25%40mails.ucas.ac.cn
patch subject: [PATCH] RISC-V: KVM: Account G-stage page table memory
config: riscv-defconfig (https://download.01.org/0day-ci/archive/20261126/20261126015340.s7bzBtEB-lkp@intel.com/config)
compiler: clang version 24.0.0git (https://github.com/llvm/llvm-project 2012344e5da8e03ea4b8283ecd6fad6e13f09d64)
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20261126/20261126015340.s7bzBtEB-lkp@intel.com/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of
the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <lkp@intel.com>
| Closes: https://lore.kernel.org/oe-kbuild-all/20261126015340.s7bzBtEB-lkp@intel.com/

All warnings (new ones prefixed by >>):

>> arch/riscv/kvm/mmu.c:850:5: warning: implicit conversion from 'unsigned long' to 'int' changes value from 18446744073709551612 to -4 [-Wconstant-conversion]
     849 |                 kvm_account_pgtable_pages(pgd,
         |                 ~~~~~~~~~~~~~~~~~~~~~~~~~
     850 |                                 -(kvm_riscv_gstage_pgd_size >> PAGE_SHIFT));
         |                                 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   1 warning generated.


vim +850 arch/riscv/kvm/mmu.c

   827	
   828	void kvm_riscv_mmu_free_pgd(struct kvm *kvm)
   829	{
   830		struct kvm_gstage gstage;
   831		void *pgd = NULL;
   832		bool flush = false;
   833	
   834		write_lock(&kvm->mmu_lock);
   835		if (kvm->arch.pgd) {
   836			kvm_riscv_gstage_init(&gstage, kvm);
   837			flush = kvm_riscv_gstage_unmap_all(&gstage);
   838			pgd = READ_ONCE(kvm->arch.pgd);
   839			kvm->arch.pgd = NULL;
   840			kvm->arch.pgd_phys = 0;
   841			kvm->arch.pgd_levels = 0;
   842		}
   843		write_unlock(&kvm->mmu_lock);
   844	
   845		if (flush)
   846			kvm_flush_remote_tlbs(kvm);
   847	
   848		if (pgd) {
   849			kvm_account_pgtable_pages(pgd,
 > 850					-(kvm_riscv_gstage_pgd_size >> PAGE_SHIFT));
   851			free_pages((unsigned long)pgd, get_order(kvm_riscv_gstage_pgd_size));
   852		}
   853	}
   854	

-- 
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-11-25 17:59 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 17:58 ` kernel test robot

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®