[PATCH v3 2/3] KVM: arm64: Distinct pKVM teardown memcache for stage-2

Vincent Donnefort posted 3 patches 11 months, 1 week ago
There is a newer version of this series
[PATCH v3 2/3] KVM: arm64: Distinct pKVM teardown memcache for stage-2
Posted by Vincent Donnefort 11 months, 1 week ago
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
Re: [PATCH v3 2/3] KVM: arm64: Distinct pKVM teardown memcache for stage-2
Posted by Marc Zyngier 11 months ago
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.
Re: [PATCH v3 2/3] KVM: arm64: Distinct pKVM teardown memcache for stage-2
Posted by Vincent Donnefort 11 months ago
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.
Re: [PATCH v3 2/3] KVM: arm64: Distinct pKVM teardown memcache for stage-2
Posted by Marc Zyngier 11 months ago
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.