linux-arm-kernel.lists.infradead.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v2 0/3] Count pKVM stage-2 usage in secondary pagetable stat
@ 2025-03-04 13:43 Vincent Donnefort
  2025-03-04 13:43 ` [PATCH v2 1/3] KVM: arm64: Add flags to kvm_hyp_memcache Vincent Donnefort
                   ` (2 more replies)
  0 siblings, 3 replies; 6+ messages in thread
From: Vincent Donnefort @ 2025-03-04 13:43 UTC (permalink / raw)
  To: maz, oliver.upton, joey.gouly, suzuki.poulose, yuzenghui,
	catalin.marinas, will
  Cc: qperret, linux-arm-kernel, kvmarm, linux-kernel, kernel-team,
	Vincent Donnefort

This series allows to count stage-2 related memory when using pKVM. The
value can be found in the /proc/meminfo field SecPageTables.

Changes since v1: https://lore.kernel.org/all/20250228121355.1377891-1-vdonnefort@google.com/
  - Flags to kvm_hyp_memcache
  - Separate stage-2 memcache
  - Account for PGD

Vincent Donnefort (3):
  KVM: arm64: Add flags to kvm_hyp_memcache
  KVM: arm64: Distinct pKVM teardown memcache for stage-2
  KVM: arm64: Count pKVM stage-2 usage in secondary pagetable stats

 arch/arm64/include/asm/kvm_host.h |  4 ++++
 arch/arm64/kvm/hyp/nvhe/pkvm.c    |  7 ++++---
 arch/arm64/kvm/mmu.c              | 18 +++++++++++++-----
 arch/arm64/kvm/pkvm.c             |  5 +++++
 4 files changed, 26 insertions(+), 8 deletions(-)


base-commit: d082ecbc71e9e0bf49883ee4afd435a77a5101b6
-- 
2.48.1.711.g2feabab25a-goog



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

* [PATCH v2 1/3] KVM: arm64: Add flags to kvm_hyp_memcache
  2025-03-04 13:43 [PATCH v2 0/3] Count pKVM stage-2 usage in secondary pagetable stat Vincent Donnefort
@ 2025-03-04 13:43 ` Vincent Donnefort
  2025-03-07  7:20   ` Oliver Upton
  2025-03-04 13:43 ` [PATCH v2 2/3] KVM: arm64: Distinct pKVM teardown memcache for stage-2 Vincent Donnefort
  2025-03-04 13:43 ` [PATCH v2 3/3] KVM: arm64: Count pKVM stage-2 usage in secondary pagetable stats Vincent Donnefort
  2 siblings, 1 reply; 6+ messages in thread
From: Vincent Donnefort @ 2025-03-04 13:43 UTC (permalink / raw)
  To: maz, oliver.upton, joey.gouly, suzuki.poulose, yuzenghui,
	catalin.marinas, will
  Cc: qperret, linux-arm-kernel, kvmarm, linux-kernel, kernel-team,
	Vincent Donnefort

Add flags to kvm_hyp_memcache and propagate them up to the allocation
and free callbacks. This will later allow to account for memory, based
on the memcache configuration.

Signed-off-by: Vincent Donnefort <vdonnefort@google.com>

diff --git a/arch/arm64/include/asm/kvm_host.h b/arch/arm64/include/asm/kvm_host.h
index 3a7ec98ef123..12691ae23d4c 100644
--- a/arch/arm64/include/asm/kvm_host.h
+++ b/arch/arm64/include/asm/kvm_host.h
@@ -86,6 +86,7 @@ struct kvm_hyp_memcache {
 	phys_addr_t head;
 	unsigned long nr_pages;
 	struct pkvm_mapping *mapping; /* only used from EL1 */
+	unsigned long flags;
 };
 
 static inline void push_hyp_memcache(struct kvm_hyp_memcache *mc,
diff --git a/arch/arm64/kvm/mmu.c b/arch/arm64/kvm/mmu.c
index 1f55b0c7b11d..68d99baa76c2 100644
--- a/arch/arm64/kvm/mmu.c
+++ b/arch/arm64/kvm/mmu.c
@@ -1086,12 +1086,12 @@ void kvm_free_stage2_pgd(struct kvm_s2_mmu *mmu)
 	}
 }
 
-static void hyp_mc_free_fn(void *addr, void *unused)
+static void hyp_mc_free_fn(void *addr, void *flags)
 {
 	free_page((unsigned long)addr);
 }
 
-static void *hyp_mc_alloc_fn(void *unused)
+static void *hyp_mc_alloc_fn(void *flags)
 {
 	return (void *)__get_free_page(GFP_KERNEL_ACCOUNT);
 }
@@ -1102,7 +1102,7 @@ void free_hyp_memcache(struct kvm_hyp_memcache *mc)
 		return;
 
 	kfree(mc->mapping);
-	__free_hyp_memcache(mc, hyp_mc_free_fn, kvm_host_va, NULL);
+	__free_hyp_memcache(mc, hyp_mc_free_fn, kvm_host_va, (void *)mc->flags);
 }
 
 int topup_hyp_memcache(struct kvm_hyp_memcache *mc, unsigned long min_pages)
@@ -1117,7 +1117,7 @@ int topup_hyp_memcache(struct kvm_hyp_memcache *mc, unsigned long min_pages)
 	}
 
 	return __topup_hyp_memcache(mc, min_pages, hyp_mc_alloc_fn,
-				    kvm_host_pa, NULL);
+				    kvm_host_pa, (void *)mc->flags);
 }
 
 /**
-- 
2.48.1.711.g2feabab25a-goog



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

* [PATCH v2 2/3] KVM: arm64: Distinct pKVM teardown memcache for stage-2
  2025-03-04 13:43 [PATCH v2 0/3] Count pKVM stage-2 usage in secondary pagetable stat Vincent Donnefort
  2025-03-04 13:43 ` [PATCH v2 1/3] KVM: arm64: Add flags to kvm_hyp_memcache Vincent Donnefort
@ 2025-03-04 13:43 ` Vincent Donnefort
  2025-03-04 13:43 ` [PATCH v2 3/3] KVM: arm64: Count pKVM stage-2 usage in secondary pagetable stats Vincent Donnefort
  2 siblings, 0 replies; 6+ messages in thread
From: Vincent Donnefort @ 2025-03-04 13:43 UTC (permalink / raw)
  To: maz, oliver.upton, joey.gouly, suzuki.poulose, yuzenghui,
	catalin.marinas, will
  Cc: qperret, linux-arm-kernel, kvmarm, linux-kernel, kernel-team,
	Vincent Donnefort

In order to account for memory dedicated to the stage-2 page-tables, use
a separated memcache when tearing down the VM.

Signed-off-by: Vincent Donnefort <vdonnefort@google.com>

diff --git a/arch/arm64/include/asm/kvm_host.h b/arch/arm64/include/asm/kvm_host.h
index 12691ae23d4c..ace3969e8106 100644
--- a/arch/arm64/include/asm/kvm_host.h
+++ b/arch/arm64/include/asm/kvm_host.h
@@ -246,6 +246,7 @@ typedef unsigned int pkvm_handle_t;
 struct kvm_protected_vm {
 	pkvm_handle_t handle;
 	struct kvm_hyp_memcache teardown_mc;
+	struct kvm_hyp_memcache stage2_teardown_mc;
 	bool enabled;
 };
 
diff --git a/arch/arm64/kvm/hyp/nvhe/pkvm.c b/arch/arm64/kvm/hyp/nvhe/pkvm.c
index 3927fe52a3dd..15f8d5315959 100644
--- a/arch/arm64/kvm/hyp/nvhe/pkvm.c
+++ b/arch/arm64/kvm/hyp/nvhe/pkvm.c
@@ -678,7 +678,7 @@ teardown_donated_memory(struct kvm_hyp_memcache *mc, void *addr, size_t size)
 
 int __pkvm_teardown_vm(pkvm_handle_t handle)
 {
-	struct kvm_hyp_memcache *mc;
+	struct kvm_hyp_memcache *mc, *stage2_mc;
 	struct pkvm_hyp_vm *hyp_vm;
 	struct kvm *host_kvm;
 	unsigned int idx;
@@ -706,7 +706,8 @@ int __pkvm_teardown_vm(pkvm_handle_t handle)
 
 	/* Reclaim guest pages (including page-table pages) */
 	mc = &host_kvm->arch.pkvm.teardown_mc;
-	reclaim_guest_pages(hyp_vm, mc);
+	stage2_mc = &host_kvm->arch.pkvm.stage2_teardown_mc;
+	reclaim_guest_pages(hyp_vm, stage2_mc);
 	unpin_host_vcpus(hyp_vm->vcpus, hyp_vm->nr_vcpus);
 
 	/* Push the metadata pages to the teardown memcache */
@@ -717,7 +718,7 @@ int __pkvm_teardown_vm(pkvm_handle_t handle)
 		while (vcpu_mc->nr_pages) {
 			void *addr = pop_hyp_memcache(vcpu_mc, hyp_phys_to_virt);
 
-			push_hyp_memcache(mc, addr, hyp_virt_to_phys);
+			push_hyp_memcache(stage2_mc, addr, hyp_virt_to_phys);
 			unmap_donated_memory_noclear(addr, PAGE_SIZE);
 		}
 
diff --git a/arch/arm64/kvm/pkvm.c b/arch/arm64/kvm/pkvm.c
index 930b677eb9b0..19921ca407c6 100644
--- a/arch/arm64/kvm/pkvm.c
+++ b/arch/arm64/kvm/pkvm.c
@@ -111,6 +111,7 @@ static void __pkvm_destroy_hyp_vm(struct kvm *host_kvm)
 
 	host_kvm->arch.pkvm.handle = 0;
 	free_hyp_memcache(&host_kvm->arch.pkvm.teardown_mc);
+	free_hyp_memcache(&host_kvm->arch.pkvm.stage2_teardown_mc);
 }
 
 /*
-- 
2.48.1.711.g2feabab25a-goog



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

* [PATCH v2 3/3] KVM: arm64: Count pKVM stage-2 usage in secondary pagetable stats
  2025-03-04 13:43 [PATCH v2 0/3] Count pKVM stage-2 usage in secondary pagetable stat Vincent Donnefort
  2025-03-04 13:43 ` [PATCH v2 1/3] KVM: arm64: Add flags to kvm_hyp_memcache Vincent Donnefort
  2025-03-04 13:43 ` [PATCH v2 2/3] KVM: arm64: Distinct pKVM teardown memcache for stage-2 Vincent Donnefort
@ 2025-03-04 13:43 ` Vincent Donnefort
  2 siblings, 0 replies; 6+ messages in thread
From: Vincent Donnefort @ 2025-03-04 13:43 UTC (permalink / raw)
  To: maz, oliver.upton, joey.gouly, suzuki.poulose, yuzenghui,
	catalin.marinas, will
  Cc: qperret, linux-arm-kernel, kvmarm, linux-kernel, kernel-team,
	Vincent Donnefort

Count the pages used by pKVM for the guest stage-2 in memory stats under
secondary pagetable stats, similarly to what the VHE mode does.

Signed-off-by: Vincent Donnefort <vdonnefort@google.com>

diff --git a/arch/arm64/include/asm/kvm_host.h b/arch/arm64/include/asm/kvm_host.h
index ace3969e8106..51754a354b7a 100644
--- a/arch/arm64/include/asm/kvm_host.h
+++ b/arch/arm64/include/asm/kvm_host.h
@@ -86,6 +86,8 @@ struct kvm_hyp_memcache {
 	phys_addr_t head;
 	unsigned long nr_pages;
 	struct pkvm_mapping *mapping; /* only used from EL1 */
+
+#define	HYP_MEMCACHE_ACCOUNT_STAGE2	BIT(1)
 	unsigned long flags;
 };
 
diff --git a/arch/arm64/kvm/mmu.c b/arch/arm64/kvm/mmu.c
index 68d99baa76c2..85d739fbddb8 100644
--- a/arch/arm64/kvm/mmu.c
+++ b/arch/arm64/kvm/mmu.c
@@ -1088,12 +1088,20 @@ void kvm_free_stage2_pgd(struct kvm_s2_mmu *mmu)
 
 static void hyp_mc_free_fn(void *addr, void *flags)
 {
+	if ((unsigned long)flags & HYP_MEMCACHE_ACCOUNT_STAGE2)
+		kvm_account_pgtable_pages(addr, -1);
+
 	free_page((unsigned long)addr);
 }
 
 static void *hyp_mc_alloc_fn(void *flags)
 {
-	return (void *)__get_free_page(GFP_KERNEL_ACCOUNT);
+	void *addr = (void *)__get_free_page(GFP_KERNEL_ACCOUNT);
+
+	if (addr && (unsigned long)flags & HYP_MEMCACHE_ACCOUNT_STAGE2)
+		kvm_account_pgtable_pages(addr, 1);
+
+	return addr;
 }
 
 void free_hyp_memcache(struct kvm_hyp_memcache *mc)
diff --git a/arch/arm64/kvm/pkvm.c b/arch/arm64/kvm/pkvm.c
index 19921ca407c6..7e6e3b0518f8 100644
--- a/arch/arm64/kvm/pkvm.c
+++ b/arch/arm64/kvm/pkvm.c
@@ -165,12 +165,16 @@ static int __pkvm_create_hyp_vm(struct kvm *host_kvm)
 	handle = ret;
 
 	host_kvm->arch.pkvm.handle = handle;
+	host_kvm->arch.pkvm.stage2_teardown_mc.flags |= HYP_MEMCACHE_ACCOUNT_STAGE2;
+	kvm_account_pgtable_pages(pgd, PAGE_ALIGN(pgd_sz) >> PAGE_SHIFT);
 
 	/* Donate memory for the vcpus at hyp and initialize it. */
 	hyp_vcpu_sz = PAGE_ALIGN(PKVM_HYP_VCPU_SIZE);
 	kvm_for_each_vcpu(idx, host_vcpu, host_kvm) {
 		void *hyp_vcpu;
 
+		host_vcpu->arch.pkvm_memcache.flags |= HYP_MEMCACHE_ACCOUNT_STAGE2;
+
 		/* Indexing of the vcpus to be sequential starting at 0. */
 		if (WARN_ON(host_vcpu->vcpu_idx != idx)) {
 			ret = -EINVAL;
-- 
2.48.1.711.g2feabab25a-goog



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

* Re: [PATCH v2 1/3] KVM: arm64: Add flags to kvm_hyp_memcache
  2025-03-04 13:43 ` [PATCH v2 1/3] KVM: arm64: Add flags to kvm_hyp_memcache Vincent Donnefort
@ 2025-03-07  7:20   ` Oliver Upton
  2025-03-07 11:35     ` Vincent Donnefort
  0 siblings, 1 reply; 6+ messages in thread
From: Oliver Upton @ 2025-03-07  7:20 UTC (permalink / raw)
  To: Vincent Donnefort
  Cc: maz, joey.gouly, suzuki.poulose, yuzenghui, catalin.marinas, will,
	qperret, linux-arm-kernel, kvmarm, linux-kernel, kernel-team

Hi Vincent,

On Tue, Mar 04, 2025 at 01:43:44PM +0000, Vincent Donnefort wrote:
> Add flags to kvm_hyp_memcache and propagate them up to the allocation
> and free callbacks. This will later allow to account for memory, based
> on the memcache configuration.

It seems slightly more obvious to me if we pass a pointer to the
memcache instead, but that's just a minor nit.

Unless you have a preference on this, I can change it when I apply the
patch.

Thanks,
Oliver


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

* Re: [PATCH v2 1/3] KVM: arm64: Add flags to kvm_hyp_memcache
  2025-03-07  7:20   ` Oliver Upton
@ 2025-03-07 11:35     ` Vincent Donnefort
  0 siblings, 0 replies; 6+ messages in thread
From: Vincent Donnefort @ 2025-03-07 11:35 UTC (permalink / raw)
  To: Oliver Upton
  Cc: maz, joey.gouly, suzuki.poulose, yuzenghui, catalin.marinas, will,
	qperret, linux-arm-kernel, kvmarm, linux-kernel, kernel-team

On Thu, Mar 06, 2025 at 11:20:00PM -0800, Oliver Upton wrote:
> Hi Vincent,
> 
> On Tue, Mar 04, 2025 at 01:43:44PM +0000, Vincent Donnefort wrote:
> > Add flags to kvm_hyp_memcache and propagate them up to the allocation
> > and free callbacks. This will later allow to account for memory, based
> > on the memcache configuration.
> 
> It seems slightly more obvious to me if we pass a pointer to the
> memcache instead, but that's just a minor nit.

That sounds good!

> 
> Unless you have a preference on this, I can change it when I apply the
> patch.

I have just sent a v3 to save you the trouble.

> 
> Thanks,
> Oliver


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

end of thread, other threads:[~2025-03-07 11:42 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-03-04 13:43 [PATCH v2 0/3] Count pKVM stage-2 usage in secondary pagetable stat Vincent Donnefort
2025-03-04 13:43 ` [PATCH v2 1/3] KVM: arm64: Add flags to kvm_hyp_memcache Vincent Donnefort
2025-03-07  7:20   ` Oliver Upton
2025-03-07 11:35     ` Vincent Donnefort
2025-03-04 13:43 ` [PATCH v2 2/3] KVM: arm64: Distinct pKVM teardown memcache for stage-2 Vincent Donnefort
2025-03-04 13:43 ` [PATCH v2 3/3] KVM: arm64: Count pKVM stage-2 usage in secondary pagetable stats Vincent Donnefort

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).