mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: "Bingyu.Xian" <shanbeeyoo@gmail.com>
To: Anup Patel <anup@brainfault.org>,
	Atish Patra <atish.patra@linux.dev>,
	Paolo Bonzini <pbonzini@redhat.com>,
	Sean Christopherson <seanjc@google.com>
Cc: Andrew Jones <andrew.jones@linux.dev>,
	Paul Walmsley <pjw@kernel.org>,
	Palmer Dabbelt <palmer@dabbelt.com>,
	Albert Ou <aou@eecs.berkeley.edu>,
	Alexandre Ghiti <alex@ghiti.fr>, Shuah Khan <shuah@kernel.org>,
	Quan Zhou <zhouquan@iscas.ac.cn>,
	"Bingyu . Xian" <shanbeeyoo@gmail.com>,
	kvm-riscv@lists.infradead.org, kvm@vger.kernel.org,
	linux-riscv@lists.infradead.org, linux-kselftest@vger.kernel.org,
	linux-kernel@vger.kernel.org
Subject: [PATCH 1/2] RISC-V: KVM: Expose G-stage page table and mapping stats
Date: Fri,  4 Dec 2026 21:26:50 +0800	[thread overview]
Message-ID: <20261204132651.413976-2-shanbeeyoo@gmail.com> (raw)
In-Reply-To: <20261204132651.413976-1-shanbeeyoo@gmail.com>

RISC-V KVM currently exposes only KVM_GENERIC_VM_STATS() in
kvm_vm_stats_desc[], leaving userspace VMMs without per-VM visibility
into G-stage page table consumption or leaf mapping granularity.

Add per-VM instantaneous counters to struct kvm_vm_stat:
  - pgtable_pages: total 4K pages allocated for the root PGD and
    lower-level G-stage page tables of the VM.
  - pages_4k, pages_2m, pages_1g (RV64) / pages_4k, pages_4m (RV32):
    number of active G-stage leaf mappings at each supported page size.

All counter updates happen under write_lock(&kvm->mmu_lock) (or during
VM initialization) and are skipped for KVM_GSTAGE_FLAGS_LOCAL contexts.
Page table pages are accounted synchronously when linked or unlinked
rather than inside RCU free callbacks, avoiding lifetime issues while
complementing global/cgroup SecPageTables accounting in commit
a92e6041c5e5 ("RISC-V: KVM: Account G-stage page table memory").

Matching the x86 pages_4k/pages_2m/pages_1g stat names also prepares
RISC-V for KVM selftests such as dirty_log_page_splitting_test.

Assisted-by: YuanSheng: deepseek-v4-pro
Co-developed-by: Quan Zhou <zhouquan@iscas.ac.cn>
Signed-off-by: Quan Zhou <zhouquan@iscas.ac.cn>
Signed-off-by: Bingyu.Xian <shanbeeyoo@gmail.com>
---
 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 +++++-
 4 files changed, 75 insertions(+), 5 deletions(-)

diff --git a/arch/riscv/include/asm/kvm_host.h b/arch/riscv/include/asm/kvm_host.h
index bdce08ca4..fd159ba05 100644
--- a/arch/riscv/include/asm/kvm_host.h
+++ b/arch/riscv/include/asm/kvm_host.h
@@ -58,6 +58,14 @@
 
 struct kvm_vm_stat {
 	struct kvm_vm_stat_generic generic;
+	u64 pgtable_pages;
+	u64 pages_4k;
+#ifdef CONFIG_64BIT
+	u64 pages_2m;
+	u64 pages_1g;
+#else
+	u64 pages_4m;
+#endif
 };
 
 struct kvm_vcpu_stat {
diff --git a/arch/riscv/kvm/gstage.c b/arch/riscv/kvm/gstage.c
index 241f4c19c..dc311b77a 100644
--- a/arch/riscv/kvm/gstage.c
+++ b/arch/riscv/kvm/gstage.c
@@ -39,6 +39,41 @@ static void gstage_free_page_table(pte_t *table)
 	call_rcu(&virt_to_page(table)->rcu_head, gstage_free_page_table_rcu);
 }
 
+static void gstage_update_pgtable_stats(struct kvm_gstage *gstage, s64 delta)
+{
+	if ((gstage->flags & KVM_GSTAGE_FLAGS_LOCAL) || !gstage->kvm)
+		return;
+
+	gstage->kvm->stat.pgtable_pages += delta;
+}
+
+static void gstage_update_leaf_stats(struct kvm_gstage *gstage, u32 level, s64 delta)
+{
+	if ((gstage->flags & KVM_GSTAGE_FLAGS_LOCAL) || !gstage->kvm)
+		return;
+
+	switch (level) {
+	case 0:
+		gstage->kvm->stat.pages_4k += delta;
+		break;
+#ifdef CONFIG_64BIT
+	case 1:
+		gstage->kvm->stat.pages_2m += delta;
+		break;
+	case 2:
+		gstage->kvm->stat.pages_1g += delta;
+		break;
+#else
+	case 1:
+		gstage->kvm->stat.pages_4m += delta;
+		break;
+#endif
+	default:
+		WARN_ON_ONCE(1);
+		break;
+	}
+}
+
 static inline unsigned long gstage_pte_index(struct kvm_gstage *gstage,
 					     gpa_t addr, u32 level)
 {
@@ -167,6 +202,7 @@ int kvm_riscv_gstage_set_pte(struct kvm_gstage *gstage,
 	u32 current_level = gstage->pgd_levels - 1;
 	pte_t *next_ptep = (pte_t *)gstage->pgd;
 	pte_t *ptep = &next_ptep[gstage_pte_index(gstage, map->addr, current_level)];
+	pte_t old_pte;
 
 	if (current_level < map->level)
 		return -EINVAL;
@@ -186,6 +222,7 @@ int kvm_riscv_gstage_set_pte(struct kvm_gstage *gstage,
 			set_pte(ptep, pfn_pte(PFN_DOWN(__pa(next_ptep)),
 					      __pgprot(_PAGE_TABLE)));
 			kvm_account_pgtable_pages(next_ptep, 1);
+			gstage_update_pgtable_stats(gstage, 1);
 		} else {
 			next_ptep = (pte_t *)gstage_pte_page_vaddr(pte);
 		}
@@ -194,10 +231,18 @@ int kvm_riscv_gstage_set_pte(struct kvm_gstage *gstage,
 		ptep = &next_ptep[gstage_pte_index(gstage, map->addr, current_level)];
 	}
 
-	if (pte_val(*ptep) != pte_val(map->pte)) {
-		bool was_invalid = !pte_val(*ptep);
+	old_pte = ptep_get(ptep);
+	if (pte_val(old_pte) != pte_val(map->pte)) {
+		bool was_invalid = !pte_val(old_pte);
+		bool was_leaf = gstage_pte_leaf(old_pte);
+		bool is_leaf = gstage_pte_leaf(map->pte);
+
 		set_pte(ptep, map->pte);
-		if (gstage_pte_leaf(map->pte) &&
+		if (!was_leaf && is_leaf)
+			gstage_update_leaf_stats(gstage, current_level, 1);
+		else if (was_leaf && !is_leaf)
+			gstage_update_leaf_stats(gstage, current_level, -1);
+		if (is_leaf &&
 		    !(was_invalid && riscv_has_extension_unlikely(RISCV_ISA_EXT_SVVPTC)))
 			gstage_tlb_flush(gstage, current_level, map->addr);
 	}
@@ -384,6 +429,9 @@ 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);
+		gstage_update_pgtable_stats(gstage, 1);
+		gstage_update_leaf_stats(gstage, current_level, -1);
+		gstage_update_leaf_stats(gstage, current_level - 1, PTRS_PER_PTE);
 
 		if (flush)
 			gstage_tlb_flush(gstage, current_level, addr);
@@ -427,11 +475,15 @@ 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) {
+			gstage_update_pgtable_stats(gstage, -1);
 			gstage_free_page_table(next_ptep);
+		}
 	} else {
 		if (op == GSTAGE_OP_CLEAR) {
 			set_pte(ptep, __pte(0));
+			if (gstage_pte_leaf(pte))
+				gstage_update_leaf_stats(gstage, ptep_level, -1);
 			flush = true;
 		} else if (op == GSTAGE_OP_WP) {
 			/*
diff --git a/arch/riscv/kvm/mmu.c b/arch/riscv/kvm/mmu.c
index e92d564f9..7b2344f59 100644
--- a/arch/riscv/kvm/mmu.c
+++ b/arch/riscv/kvm/mmu.c
@@ -814,6 +814,7 @@ int kvm_riscv_mmu_alloc_pgd(struct kvm *kvm)
 		return -ENOMEM;
 	kvm->arch.pgd = page_to_virt(pgd_page);
 	kvm_account_pgtable_pages(kvm->arch.pgd, 1 << order);
+	kvm->stat.pgtable_pages += 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;
@@ -847,6 +848,7 @@ void kvm_riscv_mmu_free_pgd(struct kvm *kvm)
 		 */
 		WRITE_ONCE(kvm->arch.pgd, NULL);
 		kvm->arch.pgd_phys = 0;
+		kvm->stat.pgtable_pages -= 1 << get_order(kvm_riscv_gstage_pgd_size);
 	}
 	write_unlock(&kvm->mmu_lock);
 
diff --git a/arch/riscv/kvm/vm.c b/arch/riscv/kvm/vm.c
index e52498354..1678fa391 100644
--- a/arch/riscv/kvm/vm.c
+++ b/arch/riscv/kvm/vm.c
@@ -14,7 +14,15 @@
 #include <asm/kvm_mmu.h>
 
 const struct kvm_stats_desc kvm_vm_stats_desc[] = {
-	KVM_GENERIC_VM_STATS()
+	KVM_GENERIC_VM_STATS(),
+	STATS_DESC_ICOUNTER(VM, pgtable_pages),
+	STATS_DESC_ICOUNTER(VM, pages_4k),
+#ifdef CONFIG_64BIT
+	STATS_DESC_ICOUNTER(VM, pages_2m),
+	STATS_DESC_ICOUNTER(VM, pages_1g),
+#else
+	STATS_DESC_ICOUNTER(VM, pages_4m),
+#endif
 };
 static_assert(ARRAY_SIZE(kvm_vm_stats_desc) ==
 		sizeof(struct kvm_vm_stat) / sizeof(u64));
-- 
2.34.1



  reply	other threads:[~2026-12-04 13:27 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-04 13:26 [PATCH 0/2] " Bingyu.Xian
2026-12-04 13:26 ` Bingyu.Xian [this message]
2026-12-04 13:26 ` [PATCH 2/2] KVM: selftests: riscv: Add G-stage binary stats test Bingyu.Xian

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261204132651.413976-2-shanbeeyoo@gmail.com \
    --to=shanbeeyoo@gmail.com \
    --cc=alex@ghiti.fr \
    --cc=andrew.jones@linux.dev \
    --cc=anup@brainfault.org \
    --cc=aou@eecs.berkeley.edu \
    --cc=atish.patra@linux.dev \
    --cc=kvm-riscv@lists.infradead.org \
    --cc=kvm@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=linux-riscv@lists.infradead.org \
    --cc=palmer@dabbelt.com \
    --cc=pbonzini@redhat.com \
    --cc=pjw@kernel.org \
    --cc=seanjc@google.com \
    --cc=shuah@kernel.org \
    --cc=zhouquan@iscas.ac.cn \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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®