linux-arm-kernel.lists.infradead.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v4 0/3] Count pKVM stage-2 usage in secondary pagetable stat
@ 2025-03-13 11:40 Vincent Donnefort
  2025-03-13 11:40 ` [PATCH v4 1/3] KVM: arm64: Add flags to kvm_hyp_memcache Vincent Donnefort
                   ` (4 more replies)
  0 siblings, 5 replies; 6+ messages in thread
From: Vincent Donnefort @ 2025-03-13 11:40 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 v3: https://lore.kernel.org/all/20250307113411.469018-1-vdonnefort@google.com/
  - Remove unnecessary void * cast (Marc)
  - Rename reclaim_guest_pages() -> reclaim_pgtable_pages() (Marc)
  - Remove unnecessary PAGE_ALIGN(pgd_sz)

Changes since v2: https://lore.kernel.org/all/20250304134347.369854-1-vdonnefort@google.com/
  - Pass a pointer to kvm_hyp_memcache instead of just the flags
    (Oliver)

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/include/nvhe/mem_protect.h |  2 +-
 arch/arm64/kvm/hyp/nvhe/mem_protect.c         |  2 +-
 arch/arm64/kvm/hyp/nvhe/pkvm.c                |  8 +++----
 arch/arm64/kvm/mmu.c                          | 22 ++++++++++++++-----
 arch/arm64/kvm/pkvm.c                         |  5 +++++
 6 files changed, 32 insertions(+), 11 deletions(-)


base-commit: d082ecbc71e9e0bf49883ee4afd435a77a5101b6
-- 
2.49.0.rc0.332.g42c0ae87b1-goog



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

* [PATCH v4 1/3] KVM: arm64: Add flags to kvm_hyp_memcache
  2025-03-13 11:40 [PATCH v4 0/3] Count pKVM stage-2 usage in secondary pagetable stat Vincent Donnefort
@ 2025-03-13 11:40 ` Vincent Donnefort
  2025-03-13 11:40 ` [PATCH v4 2/3] KVM: arm64: Distinct pKVM teardown memcache for stage-2 Vincent Donnefort
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: Vincent Donnefort @ 2025-03-13 11:40 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 the latter 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..6107a3c8ccf6 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 *mc)
 {
 	free_page((unsigned long)addr);
 }
 
-static void *hyp_mc_alloc_fn(void *unused)
+static void *hyp_mc_alloc_fn(void *mc)
 {
 	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, mc);
 }
 
 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, mc);
 }
 
 /**
-- 
2.49.0.rc0.332.g42c0ae87b1-goog



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

* [PATCH v4 2/3] KVM: arm64: Distinct pKVM teardown memcache for stage-2
  2025-03-13 11:40 [PATCH v4 0/3] Count pKVM stage-2 usage in secondary pagetable stat Vincent Donnefort
  2025-03-13 11:40 ` [PATCH v4 1/3] KVM: arm64: Add flags to kvm_hyp_memcache Vincent Donnefort
@ 2025-03-13 11:40 ` Vincent Donnefort
  2025-03-13 11:40 ` [PATCH v4 3/3] KVM: arm64: Count pKVM stage-2 usage in secondary pagetable stats Vincent Donnefort
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: Vincent Donnefort @ 2025-03-13 11:40 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. Meanwhile rename
reclaim_guest_pages to reflect the fact it only reclaim page-table
pages.

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/include/nvhe/mem_protect.h b/arch/arm64/kvm/hyp/include/nvhe/mem_protect.h
index 978f38c386ee..ea0a704da9b8 100644
--- a/arch/arm64/kvm/hyp/include/nvhe/mem_protect.h
+++ b/arch/arm64/kvm/hyp/include/nvhe/mem_protect.h
@@ -56,7 +56,7 @@ void handle_host_mem_abort(struct kvm_cpu_context *host_ctxt);
 
 int hyp_pin_shared_mem(void *from, void *to);
 void hyp_unpin_shared_mem(void *from, void *to);
-void reclaim_guest_pages(struct pkvm_hyp_vm *vm, struct kvm_hyp_memcache *mc);
+void reclaim_pgtable_pages(struct pkvm_hyp_vm *vm, struct kvm_hyp_memcache *mc);
 int refill_memcache(struct kvm_hyp_memcache *mc, unsigned long min_pages,
 		    struct kvm_hyp_memcache *host_mc);
 
diff --git a/arch/arm64/kvm/hyp/nvhe/mem_protect.c b/arch/arm64/kvm/hyp/nvhe/mem_protect.c
index 19c3c631708c..f34f11c720d7 100644
--- a/arch/arm64/kvm/hyp/nvhe/mem_protect.c
+++ b/arch/arm64/kvm/hyp/nvhe/mem_protect.c
@@ -266,7 +266,7 @@ int kvm_guest_prepare_stage2(struct pkvm_hyp_vm *vm, void *pgd)
 	return 0;
 }
 
-void reclaim_guest_pages(struct pkvm_hyp_vm *vm, struct kvm_hyp_memcache *mc)
+void reclaim_pgtable_pages(struct pkvm_hyp_vm *vm, struct kvm_hyp_memcache *mc)
 {
 	struct hyp_page *page;
 	void *addr;
diff --git a/arch/arm64/kvm/hyp/nvhe/pkvm.c b/arch/arm64/kvm/hyp/nvhe/pkvm.c
index 3927fe52a3dd..ac85bc51b8d3 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,10 +706,10 @@ 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_pgtable_pages(hyp_vm, stage2_mc);
 	unpin_host_vcpus(hyp_vm->vcpus, hyp_vm->nr_vcpus);
 
-	/* Push the metadata pages to the teardown memcache */
 	for (idx = 0; idx < hyp_vm->nr_vcpus; ++idx) {
 		struct pkvm_hyp_vcpu *hyp_vcpu = hyp_vm->vcpus[idx];
 		struct kvm_hyp_memcache *vcpu_mc = &hyp_vcpu->vcpu.arch.pkvm_memcache;
@@ -717,7 +717,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.49.0.rc0.332.g42c0ae87b1-goog



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

* [PATCH v4 3/3] KVM: arm64: Count pKVM stage-2 usage in secondary pagetable stats
  2025-03-13 11:40 [PATCH v4 0/3] Count pKVM stage-2 usage in secondary pagetable stat Vincent Donnefort
  2025-03-13 11:40 ` [PATCH v4 1/3] KVM: arm64: Add flags to kvm_hyp_memcache Vincent Donnefort
  2025-03-13 11:40 ` [PATCH v4 2/3] KVM: arm64: Distinct pKVM teardown memcache for stage-2 Vincent Donnefort
@ 2025-03-13 11:40 ` Vincent Donnefort
  2025-03-13 13:13 ` [PATCH v4 0/3] Count pKVM stage-2 usage in secondary pagetable stat Marc Zyngier
  2025-03-14 23:11 ` Oliver Upton
  4 siblings, 0 replies; 6+ messages in thread
From: Vincent Donnefort @ 2025-03-13 11:40 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, 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 6107a3c8ccf6..2feb6c6b63af 100644
--- a/arch/arm64/kvm/mmu.c
+++ b/arch/arm64/kvm/mmu.c
@@ -1088,12 +1088,24 @@ void kvm_free_stage2_pgd(struct kvm_s2_mmu *mmu)
 
 static void hyp_mc_free_fn(void *addr, void *mc)
 {
+	struct kvm_hyp_memcache *memcache = mc;
+
+	if (memcache->flags & HYP_MEMCACHE_ACCOUNT_STAGE2)
+		kvm_account_pgtable_pages(addr, -1);
+
 	free_page((unsigned long)addr);
 }
 
 static void *hyp_mc_alloc_fn(void *mc)
 {
-	return (void *)__get_free_page(GFP_KERNEL_ACCOUNT);
+	struct kvm_hyp_memcache *memcache = mc;
+	void *addr;
+
+	addr = (void *)__get_free_page(GFP_KERNEL_ACCOUNT);
+	if (addr && memcache->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..4409a10d02b6 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, pgd_sz / PAGE_SIZE);
 
 	/* 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.49.0.rc0.332.g42c0ae87b1-goog



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

* Re: [PATCH v4 0/3] Count pKVM stage-2 usage in secondary pagetable stat
  2025-03-13 11:40 [PATCH v4 0/3] Count pKVM stage-2 usage in secondary pagetable stat Vincent Donnefort
                   ` (2 preceding siblings ...)
  2025-03-13 11:40 ` [PATCH v4 3/3] KVM: arm64: Count pKVM stage-2 usage in secondary pagetable stats Vincent Donnefort
@ 2025-03-13 13:13 ` Marc Zyngier
  2025-03-14 23:11 ` Oliver Upton
  4 siblings, 0 replies; 6+ messages in thread
From: Marc Zyngier @ 2025-03-13 13:13 UTC (permalink / raw)
  To: Vincent Donnefort
  Cc: oliver.upton, joey.gouly, suzuki.poulose, yuzenghui,
	catalin.marinas, will, qperret, linux-arm-kernel, kvmarm,
	linux-kernel, kernel-team

On Thu, 13 Mar 2025 11:40:35 +0000,
Vincent Donnefort <vdonnefort@google.com> wrote:
> 
> 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 v3: https://lore.kernel.org/all/20250307113411.469018-1-vdonnefort@google.com/
>   - Remove unnecessary void * cast (Marc)
>   - Rename reclaim_guest_pages() -> reclaim_pgtable_pages() (Marc)
>   - Remove unnecessary PAGE_ALIGN(pgd_sz)
> 
> Changes since v2: https://lore.kernel.org/all/20250304134347.369854-1-vdonnefort@google.com/
>   - Pass a pointer to kvm_hyp_memcache instead of just the flags
>     (Oliver)
> 
> 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/include/nvhe/mem_protect.h |  2 +-
>  arch/arm64/kvm/hyp/nvhe/mem_protect.c         |  2 +-
>  arch/arm64/kvm/hyp/nvhe/pkvm.c                |  8 +++----
>  arch/arm64/kvm/mmu.c                          | 22 ++++++++++++++-----
>  arch/arm64/kvm/pkvm.c                         |  5 +++++
>  6 files changed, 32 insertions(+), 11 deletions(-)

Acked-by: Marc Zyngier <maz@kernel.org>

	M.

-- 
Without deviation from the norm, progress is not possible.


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

* Re: [PATCH v4 0/3] Count pKVM stage-2 usage in secondary pagetable stat
  2025-03-13 11:40 [PATCH v4 0/3] Count pKVM stage-2 usage in secondary pagetable stat Vincent Donnefort
                   ` (3 preceding siblings ...)
  2025-03-13 13:13 ` [PATCH v4 0/3] Count pKVM stage-2 usage in secondary pagetable stat Marc Zyngier
@ 2025-03-14 23:11 ` Oliver Upton
  4 siblings, 0 replies; 6+ messages in thread
From: Oliver Upton @ 2025-03-14 23:11 UTC (permalink / raw)
  To: maz, joey.gouly, suzuki.poulose, yuzenghui, catalin.marinas, will,
	Vincent Donnefort
  Cc: Oliver Upton, qperret, linux-arm-kernel, kvmarm, linux-kernel,
	kernel-team

On Thu, 13 Mar 2025 11:40:35 +0000, Vincent Donnefort wrote:
> 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 v3: https://lore.kernel.org/all/20250307113411.469018-1-vdonnefort@google.com/
>   - Remove unnecessary void * cast (Marc)
>   - Rename reclaim_guest_pages() -> reclaim_pgtable_pages() (Marc)
>   - Remove unnecessary PAGE_ALIGN(pgd_sz)
> 
> [...]

Applied to next, thanks!

[1/3] KVM: arm64: Add flags to kvm_hyp_memcache
      https://git.kernel.org/kvmarm/kvmarm/c/cf2d228da9a8
[2/3] KVM: arm64: Distinct pKVM teardown memcache for stage-2
      https://git.kernel.org/kvmarm/kvmarm/c/8c0d7d14c5cd
[3/3] KVM: arm64: Count pKVM stage-2 usage in secondary pagetable stats
      https://git.kernel.org/kvmarm/kvmarm/c/79ea66231599

--
Best,
Oliver


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

end of thread, other threads:[~2025-03-15  0:21 UTC | newest]

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

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).