linux-arm-kernel.lists.infradead.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v3 0/3] Count pKVM stage-2 usage in secondary pagetable stat
@ 2025-03-07 11:34 Vincent Donnefort
  2025-03-07 11:34 ` [PATCH v3 1/3] KVM: arm64: Add flags to kvm_hyp_memcache Vincent Donnefort
                   ` (2 more replies)
  0 siblings, 3 replies; 9+ messages in thread
From: Vincent Donnefort @ 2025-03-07 11:34 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 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/nvhe/pkvm.c    |  7 ++++---
 arch/arm64/kvm/mmu.c              | 22 +++++++++++++++++-----
 arch/arm64/kvm/pkvm.c             |  5 +++++
 4 files changed, 30 insertions(+), 8 deletions(-)


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



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

* [PATCH v3 1/3] KVM: arm64: Add flags to kvm_hyp_memcache
  2025-03-07 11:34 [PATCH v3 0/3] Count pKVM stage-2 usage in secondary pagetable stat Vincent Donnefort
@ 2025-03-07 11:34 ` Vincent Donnefort
  2025-03-12  8:38   ` Marc Zyngier
  2025-03-07 11:34 ` [PATCH v3 2/3] KVM: arm64: Distinct pKVM teardown memcache for stage-2 Vincent Donnefort
  2025-03-07 11:34 ` [PATCH v3 3/3] KVM: arm64: Count pKVM stage-2 usage in secondary pagetable stats Vincent Donnefort
  2 siblings, 1 reply; 9+ messages in thread
From: Vincent Donnefort @ 2025-03-07 11:34 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..c01ad4430729 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, (void *)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, (void *)mc);
 }
 
 /**
-- 
2.49.0.rc0.332.g42c0ae87b1-goog



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

* [PATCH v3 2/3] KVM: arm64: Distinct pKVM teardown memcache for stage-2
  2025-03-07 11:34 [PATCH v3 0/3] Count pKVM stage-2 usage in secondary pagetable stat Vincent Donnefort
  2025-03-07 11:34 ` [PATCH v3 1/3] KVM: arm64: Add flags to kvm_hyp_memcache Vincent Donnefort
@ 2025-03-07 11:34 ` Vincent Donnefort
  2025-03-12  8:59   ` Marc Zyngier
  2025-03-07 11:34 ` [PATCH v3 3/3] KVM: arm64: Count pKVM stage-2 usage in secondary pagetable stats Vincent Donnefort
  2 siblings, 1 reply; 9+ messages in thread
From: Vincent Donnefort @ 2025-03-07 11:34 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.49.0.rc0.332.g42c0ae87b1-goog



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

* [PATCH v3 3/3] KVM: arm64: Count pKVM stage-2 usage in secondary pagetable stats
  2025-03-07 11:34 [PATCH v3 0/3] Count pKVM stage-2 usage in secondary pagetable stat Vincent Donnefort
  2025-03-07 11:34 ` [PATCH v3 1/3] KVM: arm64: Add flags to kvm_hyp_memcache Vincent Donnefort
  2025-03-07 11:34 ` [PATCH v3 2/3] KVM: arm64: Distinct pKVM teardown memcache for stage-2 Vincent Donnefort
@ 2025-03-07 11:34 ` Vincent Donnefort
  2 siblings, 0 replies; 9+ messages in thread
From: Vincent Donnefort @ 2025-03-07 11:34 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 c01ad4430729..73b44f1200c7 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..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.49.0.rc0.332.g42c0ae87b1-goog



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

* Re: [PATCH v3 1/3] KVM: arm64: Add flags to kvm_hyp_memcache
  2025-03-07 11:34 ` [PATCH v3 1/3] KVM: arm64: Add flags to kvm_hyp_memcache Vincent Donnefort
@ 2025-03-12  8:38   ` Marc Zyngier
  2025-03-13  9:16     ` Vincent Donnefort
  0 siblings, 1 reply; 9+ messages in thread
From: Marc Zyngier @ 2025-03-12  8:38 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 Fri, 07 Mar 2025 11:34:09 +0000,
Vincent Donnefort <vdonnefort@google.com> wrote:
> 
> 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..c01ad4430729 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, (void *)mc);

Why the cast? It looks superfluous to me.

>  }
>  
>  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);

Same here.

Thanks,

	M.

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


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

* Re: [PATCH v3 2/3] KVM: arm64: Distinct pKVM teardown memcache for stage-2
  2025-03-07 11:34 ` [PATCH v3 2/3] KVM: arm64: Distinct pKVM teardown memcache for stage-2 Vincent Donnefort
@ 2025-03-12  8:59   ` Marc Zyngier
  2025-03-13  9:13     ` Vincent Donnefort
  0 siblings, 1 reply; 9+ messages in thread
From: Marc Zyngier @ 2025-03-12  8:59 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 Fri, 07 Mar 2025 11:34:10 +0000,
Vincent Donnefort <vdonnefort@google.com> wrote:
> 
> 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);

This looks odd. What counts as stage-2 pages here? Or is it that
reclaim_guest_pages() is very badly named?

Thanks,

	M.

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


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

* Re: [PATCH v3 2/3] KVM: arm64: Distinct pKVM teardown memcache for stage-2
  2025-03-12  8:59   ` Marc Zyngier
@ 2025-03-13  9:13     ` Vincent Donnefort
  2025-03-13  9:31       ` Marc Zyngier
  0 siblings, 1 reply; 9+ messages in thread
From: Vincent Donnefort @ 2025-03-13  9:13 UTC (permalink / raw)
  To: Marc Zyngier
  Cc: oliver.upton, joey.gouly, suzuki.poulose, yuzenghui,
	catalin.marinas, will, qperret, linux-arm-kernel, kvmarm,
	linux-kernel, kernel-team

On Wed, Mar 12, 2025 at 08:59:15AM +0000, Marc Zyngier wrote:
> On Fri, 07 Mar 2025 11:34:10 +0000,
> Vincent Donnefort <vdonnefort@google.com> wrote:
> > 
> > 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);
> 
> This looks odd. What counts as stage-2 pages here? Or is it that
> reclaim_guest_pages() is very badly named?

Yes, this is a naming issue here. How about 

  reclaim_pgtable_pages(hyp_vm, stage2_mc);

Then I can probably drop that /* Push the metadata pages to the teardown
memcache */  comment as it is clear what is pgtable and what is meta-data?

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


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

* Re: [PATCH v3 1/3] KVM: arm64: Add flags to kvm_hyp_memcache
  2025-03-12  8:38   ` Marc Zyngier
@ 2025-03-13  9:16     ` Vincent Donnefort
  0 siblings, 0 replies; 9+ messages in thread
From: Vincent Donnefort @ 2025-03-13  9:16 UTC (permalink / raw)
  To: Marc Zyngier
  Cc: oliver.upton, joey.gouly, suzuki.poulose, yuzenghui,
	catalin.marinas, will, qperret, linux-arm-kernel, kvmarm,
	linux-kernel, kernel-team

[...]

> > @@ -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);
> 
> Why the cast? It looks superfluous to me.

Ha yes it is now I pass mc and not 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);
> 
> Same here.
> 
> Thanks,
> 
> 	M.
> 
> -- 
> Without deviation from the norm, progress is not possible.


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

* Re: [PATCH v3 2/3] KVM: arm64: Distinct pKVM teardown memcache for stage-2
  2025-03-13  9:13     ` Vincent Donnefort
@ 2025-03-13  9:31       ` Marc Zyngier
  0 siblings, 0 replies; 9+ messages in thread
From: Marc Zyngier @ 2025-03-13  9:31 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 09:13:45 +0000,
Vincent Donnefort <vdonnefort@google.com> wrote:
> 
> On Wed, Mar 12, 2025 at 08:59:15AM +0000, Marc Zyngier wrote:
> > On Fri, 07 Mar 2025 11:34:10 +0000,
> > Vincent Donnefort <vdonnefort@google.com> wrote:
> > > 
> > > 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);
> > 
> > This looks odd. What counts as stage-2 pages here? Or is it that
> > reclaim_guest_pages() is very badly named?
> 
> Yes, this is a naming issue here. How about 
> 
>   reclaim_pgtable_pages(hyp_vm, stage2_mc);

LGTM.

> 
> Then I can probably drop that /* Push the metadata pages to the teardown
> memcache */  comment as it is clear what is pgtable and what is meta-data?

Yeah, the comments are actively getting in the way.

Thanks,

	M.

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


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

end of thread, other threads:[~2025-03-13  9:33 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-03-07 11:34 [PATCH v3 0/3] Count pKVM stage-2 usage in secondary pagetable stat Vincent Donnefort
2025-03-07 11:34 ` [PATCH v3 1/3] KVM: arm64: Add flags to kvm_hyp_memcache Vincent Donnefort
2025-03-12  8:38   ` Marc Zyngier
2025-03-13  9:16     ` Vincent Donnefort
2025-03-07 11:34 ` [PATCH v3 2/3] KVM: arm64: Distinct pKVM teardown memcache for stage-2 Vincent Donnefort
2025-03-12  8:59   ` Marc Zyngier
2025-03-13  9:13     ` Vincent Donnefort
2025-03-13  9:31       ` Marc Zyngier
2025-03-07 11:34 ` [PATCH v3 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).