Skip to contentSkip to conversation

KVM: arm64: Introduce pKVM hypervisor heap allocator

PATCH Superseded Vincent Donnefort 3 participants 55 messages Jul 6 – Jul 20

Subsystems (2)

v2 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

v2 cover KVM: arm64: Introduce pKVM hypervisor heap allocator

pKVM historically lacked a dynamic memory allocator: all hypervisor-side VM and VCPU structures had to be sized on the host, allocated as contiguous pages and donated to the hypervisor.

This design tightly coupled the hypervisor's memory footprint to host-side constraints, complicated memory reclaim, and severely restricted VM scalability.

This patch series introduces a dynamically-mapped custom heap allocator (hyp_allocator) to the pKVM hypervisor. The initial users are the pkvm_hyp_vm and pkvm_hyp_vcpu structs, and the hypervisor tracing metadata.

In the near future, this heap allocator is expected to be leveraged to support SVE in protected VMs and in the distant future, it will also support dynamic device assignment.

By moving to a hypervisor-managed dynamic allocator, we also allow deduplicating the donation/reclaim path of EL2-private structures.

The main building blocks for this series are:

1. pkvm_hyp_req:
----------------
When the hypervisor heap allocator goes out of memory (-ENOMEM), it suspends the hypercall, embeds a PKVM_HYP_REQ_HYP_ALLOC top-up request into the SMCCC HVC return registers, and exits back to the host.

This building block will also be useful for the future huge-mapping support in protected guests, allowing EL2 to raise requests such as block splitting back to the host.

2. hyp_allocator:
----------------
This heap allocator manages a reserved VA space range, dynamically mapping and unmapping physical pages on-demand to minimise the pKVM hypervisor footprint. As memory is reclaimed and relinquished to the host, unmapped holes are introduced within the VA space. To prevent orphan mapped regions, neighboring unused chunks cannot be merged if they are separated by an unmapped region.

The allocator chunk metadata is stored directly into the VA space range. To minimize metadata overhead, chunks only link to each other via a relative 32-bit offset.

A simple hardening of the metadata is added via a simple 32-bit hash.

3. shrinker:
------------
As the heap allocator isn't reclaimed actively on VM or tracing teardown, a shrinker is added to allow the host to reclaim unused memory from the hypervisor when the host is under heavy memory pressure.

v1 -> v2:
- Rebased series on 7.2-rc2.
- Use scope-based hyp_spinlock.
- Fix best_missing/best_data_size priority in hyp_allocator_find_efficient_chunk() (Sashiko)
- Fix missing free_hyp_memcache() in pkvm_hyp_topup() (Sashiko)
- Fix unused selftest_init() warning when !CONFIG_NVHE_EL2_DEBUG (Sashiko)
- Fix missing shrinker_free() in teardown_hyp_mode() (Sashiko)

v1: https://lore.kernel.org/r/[email protected]

Vincent Donnefort (18):
KVM: arm64: Add __pkvm_private_range_pa
KVM: arm64: Add pkvm_remove_mappings
KVM: arm64: Add __hyp_allocator_map for the pKVM hyp
KVM: arm64: Add a heap allocator for the pKVM hyp
KVM: arm64: Allow kvm_hyp_memcache usage outside of stage-2
KVM: arm64: Add topup interface for the pKVM heap allocator
KVM: arm64: Add pkvm_hyp_req infrastructure
KVM: arm64: Handle PKVM_HYP_REQ_HYP_ALLOC request
KVM: arm64: Add reclaim interface for the pKVM heap alloc
KVM: arm64: Add selftests for the pKVM heap allocator
KVM: arm64: Add a shrinker for pKVM
KVM: arm64: Filter out non-kernel addresses in kern_hyp_va
KVM: arm64: Move hyp_vm refcount into the structure
KVM: arm64: Use noclear for PGD in __pkvm_init_vm error path
KVM: arm64: Alloc pkvm_hyp_vm using pKVM heap allocator
KVM: arm64: Alloc pkvm_hyp_vcpu using pKVM heap allocator
KVM: arm64: Reject hyp trace descriptors with fewer CPUs than
hyp_nr_cpus
KVM: arm64: Alloc simple_buffer_page using pKVM hyp allocator

 arch/arm64/include/asm/kvm_asm.h           |    4 +
 arch/arm64/include/asm/kvm_host.h          |   14 +-
 arch/arm64/include/asm/kvm_mmu.h           |    3 +
 arch/arm64/include/asm/kvm_pkvm.h          |  104 ++
 arch/arm64/kvm/arm.c                       |   26 +-
 arch/arm64/kvm/hyp/hyp-constants.c         |    2 -
 arch/arm64/kvm/hyp/include/nvhe/alloc.h    |   24 +
 arch/arm64/kvm/hyp/include/nvhe/mm.h       |    3 +
 arch/arm64/kvm/hyp/include/nvhe/pkvm.h     |   19 +-
 arch/arm64/kvm/hyp/include/nvhe/spinlock.h |    5 +
 arch/arm64/kvm/hyp/nvhe/Makefile           |    2 +-
 arch/arm64/kvm/hyp/nvhe/alloc.c            | 1217 ++++++++++++++++++++
 arch/arm64/kvm/hyp/nvhe/hyp-main.c         |  124 +-
 arch/arm64/kvm/hyp/nvhe/mm.c               |   33 +
 arch/arm64/kvm/hyp/nvhe/pkvm.c             |  111 +-
 arch/arm64/kvm/hyp/nvhe/setup.c            |    5 +
 arch/arm64/kvm/hyp/nvhe/trace.c            |   66 +-
 arch/arm64/kvm/hyp_trace.c                 |   16 +-
 arch/arm64/kvm/mmu.c                       |    4 +-
 arch/arm64/kvm/pkvm.c                      |  136 ++-
 arch/arm64/kvm/trace_pkvm.h                |   37 +
 21 files changed, 1801 insertions(+), 154 deletions(-)
 create mode 100644 arch/arm64/kvm/hyp/include/nvhe/alloc.h
 create mode 100644 arch/arm64/kvm/hyp/nvhe/alloc.c
 create mode 100644 arch/arm64/kvm/trace_pkvm.h

base-commit: 8cdeaa50eae8dad34885515f62559ee83e7e8dda

Signature · 1 line
2.55.0.rc2.803.g1fd1e6609c-goog
#1 lore raw

PATCH v2 1/18 KVM: arm64: Add __pkvm_private_range_pa

Mappings in the pKVM private range are not identity mapped, making the standard __hyp_pa() unsuitable for translating these addresses.

Introduce __pkvm_private_range_pa() to resolve physical addresses for this range by walking the hypervisor page-table. This will be useful for the upcoming pKVM heap allocator.

arch/arm64/kvm/hyp/include/nvhe/mm.h +1 -0
index 6e83ce35c2f2..d3137c16b632 100644
@@ -30,5 +30,6 @@ int __pkvm_create_private_mapping(phys_addr_t phys, size_t size,
 				  unsigned long *haddr);
 int pkvm_create_stack(phys_addr_t phys, unsigned long *haddr);
 int pkvm_alloc_private_va_range(size_t size, unsigned long *haddr);
+phys_addr_t __pkvm_private_range_pa(void *va);
 
 #endif /* __KVM_HYP_MM_H */
arch/arm64/kvm/hyp/nvhe/mm.c +14 -0
index 3b0bee496bff..773426a68d2d 100644
@@ -502,3 +502,17 @@ int refill_memcache(struct kvm_hyp_memcache *mc, unsigned long min_pages,
 
 	return ret;
 }
+
+phys_addr_t __pkvm_private_range_pa(void *va)
+{
+	kvm_pte_t pte = 0;
+	s8 level;
+
+	hyp_spin_lock(&pkvm_pgd_lock);
+	WARN_ON(kvm_pgtable_get_leaf(&pkvm_pgtable, (u64)va, &pte, &level));
+	hyp_spin_unlock(&pkvm_pgd_lock);
+
+	WARN_ON(!kvm_pte_valid(pte));
+
+	return kvm_pte_to_phys(pte) + offset_in_page(va);
+}
Signature · 1 line
2.55.0.rc2.803.g1fd1e6609c-goog
reviewer Reviewed-by on v2 1/18
#2 lore raw

Re: [PATCH v2 01/18] KVM: arm64: Add __pkvm_private_range_pa

Quoted 27 lines · Vincent Donnefort

Mappings in the pKVM private range are not identity mapped, making the standard __hyp_pa() unsuitable for translating these addresses.

Introduce __pkvm_private_range_pa() to resolve physical addresses for this range by walking the hypervisor page-table. This will be useful for the upcoming pKVM heap allocator.

arch/arm64/kvm/hyp/include/nvhe/mm.h +1 -0
index 6e83ce35c2f2..d3137c16b632 100644
@@ -30,5 +30,6 @@ int __pkvm_create_private_mapping(phys_addr_t phys, size_t size,
                                  unsigned long *haddr);
 int pkvm_create_stack(phys_addr_t phys, unsigned long *haddr);
 int pkvm_alloc_private_va_range(size_t size, unsigned long *haddr);
+phys_addr_t __pkvm_private_range_pa(void *va);
 
 #endif /* __KVM_HYP_MM_H */
arch/arm64/kvm/hyp/nvhe/mm.c +0 -0
index 3b0bee496bff..773426a68d2d 100644
@@ -502,3 +502,17 @@ int refill_memcache(struct kvm_hyp_memcache *mc, unsigned long min_pages,
 

arch/arm64/kvm/hyp/nvhe/mm.c int refill_memcache(struct kvm_hyp_memcache *mc, unsigned long min_pages, · line 512

Quoted 10 lines
arch/arm64/kvm/hyp/nvhe/mm.c +8 -0
        return ret;
 }
+
+phys_addr_t __pkvm_private_range_pa(void *va)
+{
+       kvm_pte_t pte = 0;
+       s8 level;
+
+       hyp_spin_lock(&pkvm_pgd_lock);
+       WARN_ON(kvm_pgtable_get_leaf(&pkvm_pgtable, (u64)va, &pte, &level));

nit: can you pass NULL instead of level?

Cheers,
/fuad

Quoted 9 lines
arch/arm64/kvm/hyp/nvhe/mm.c +6 -1
+       hyp_spin_unlock(&pkvm_pgd_lock);
+
+       WARN_ON(!kvm_pte_valid(pte));
+
+       return kvm_pte_to_phys(pte) + offset_in_page(va);
+}
--

2.55.0.rc2.803.g1fd1e6609c-goog

#3 lore raw

PATCH v2 2/18 KVM: arm64: Add pkvm_remove_mappings

In reply to Vincent Donnefort

Add the counterpart to pkvm_create_mappings(), allowing previously mapped ranges to be removed. This will be useful for the upcoming pKVM heap allocator to manage its private mappings.

arch/arm64/kvm/hyp/include/nvhe/mm.h +1 -0
index d3137c16b632..725bb0fb941d 100644
@@ -25,6 +25,7 @@ int hyp_back_vmemmap(phys_addr_t back);
 int pkvm_cpu_set_vector(enum arm64_hyp_spectre_vector slot);
 int pkvm_create_mappings(void *from, void *to, enum kvm_pgtable_prot prot);
 int pkvm_create_mappings_locked(void *from, void *to, enum kvm_pgtable_prot prot);
+void pkvm_remove_mappings(void *from, void *to);
 int __pkvm_create_private_mapping(phys_addr_t phys, size_t size,
 				  enum kvm_pgtable_prot prot,
 				  unsigned long *haddr);
arch/arm64/kvm/hyp/nvhe/mm.c +14 -0
index 773426a68d2d..ec59da1322a7 100644
@@ -146,6 +146,20 @@ int pkvm_create_mappings(void *from, void *to, enum kvm_pgtable_prot prot)
 	return ret;
 }
 
+void pkvm_remove_mappings(void *from, void *to)
+{
+	u64 size;
+
+	to = PTR_ALIGN(to, PAGE_SIZE);
+	from = PTR_ALIGN_DOWN(from, PAGE_SIZE);
+	size = (u64)to - (u64)from;
+	WARN_ON(from > to);
+
+	hyp_spin_lock(&pkvm_pgd_lock);
+	WARN_ON(kvm_pgtable_hyp_unmap(&pkvm_pgtable, (u64)from, size) != size);
+	hyp_spin_unlock(&pkvm_pgd_lock);
+}
+
 int hyp_back_vmemmap(phys_addr_t back)
 {
 	unsigned long i, start, size, end = 0;
Signature · 1 line
2.55.0.rc2.803.g1fd1e6609c-goog
reviewer Reviewed-by on v2 2/18
#4 lore raw

Re: [PATCH v2 02/18] KVM: arm64: Add pkvm_remove_mappings

Quoted 6 lines · Vincent Donnefort

Add the counterpart to pkvm_create_mappings(), allowing previously mapped ranges to be removed. This will be useful for the upcoming pKVM heap allocator to manage its private mappings.

Cheers,
/fuad

Quoted 41 lines
arch/arm64/kvm/hyp/include/nvhe/mm.h +1 -0
index d3137c16b632..725bb0fb941d 100644
@@ -25,6 +25,7 @@ int hyp_back_vmemmap(phys_addr_t back);
 int pkvm_cpu_set_vector(enum arm64_hyp_spectre_vector slot);
 int pkvm_create_mappings(void *from, void *to, enum kvm_pgtable_prot prot);
 int pkvm_create_mappings_locked(void *from, void *to, enum kvm_pgtable_prot prot);
+void pkvm_remove_mappings(void *from, void *to);
 int __pkvm_create_private_mapping(phys_addr_t phys, size_t size,
                                  enum kvm_pgtable_prot prot,
                                  unsigned long *haddr);
arch/arm64/kvm/hyp/nvhe/mm.c +14 -1
index 773426a68d2d..ec59da1322a7 100644
@@ -146,6 +146,20 @@ int pkvm_create_mappings(void *from, void *to, enum kvm_pgtable_prot prot)
        return ret;
 }
 
+void pkvm_remove_mappings(void *from, void *to)
+{
+       u64 size;
+
+       to = PTR_ALIGN(to, PAGE_SIZE);
+       from = PTR_ALIGN_DOWN(from, PAGE_SIZE);
+       size = (u64)to - (u64)from;
+       WARN_ON(from > to);
+
+       hyp_spin_lock(&pkvm_pgd_lock);
+       WARN_ON(kvm_pgtable_hyp_unmap(&pkvm_pgtable, (u64)from, size) != size);
+       hyp_spin_unlock(&pkvm_pgd_lock);
+}
+
 int hyp_back_vmemmap(phys_addr_t back)
 {
        unsigned long i, start, size, end = 0;
--

2.55.0.rc2.803.g1fd1e6609c-goog

#5 lore raw

PATCH v2 3/18 KVM: arm64: Add __hyp_allocator_map for the pKVM hyp

In reply to Vincent Donnefort

In preparation for the pKVM heap allocator, introduce __hyp_allocator_map() to map a single physical page to a given virtual address in the hypervisor stage-1 page-table.

arch/arm64/kvm/hyp/include/nvhe/mm.h +1 -0
index 725bb0fb941d..98a7774b541c 100644
@@ -32,5 +32,6 @@ int __pkvm_create_private_mapping(phys_addr_t phys, size_t size,
 int pkvm_create_stack(phys_addr_t phys, unsigned long *haddr);
 int pkvm_alloc_private_va_range(size_t size, unsigned long *haddr);
 phys_addr_t __pkvm_private_range_pa(void *va);
+int __hyp_allocator_map(void *va, phys_addr_t phys);
 
 #endif /* __KVM_HYP_MM_H */
arch/arm64/kvm/hyp/nvhe/mm.c +5 -0
index ec59da1322a7..c1c01f81ac5f 100644
@@ -160,6 +160,11 @@ void pkvm_remove_mappings(void *from, void *to)
 	hyp_spin_unlock(&pkvm_pgd_lock);
 }
 
+int __hyp_allocator_map(void *va, phys_addr_t phys)
+{
+	return __pkvm_create_mappings((unsigned long)va, PAGE_SIZE, phys, PAGE_HYP);
+}
+
 int hyp_back_vmemmap(phys_addr_t back)
 {
 	unsigned long i, start, size, end = 0;
Signature · 1 line
2.55.0.rc2.803.g1fd1e6609c-goog
reviewer Reviewed-by on v2 3/18
#6 lore raw

Re: [PATCH v2 03/18] KVM: arm64: Add __hyp_allocator_map for the pKVM hyp

Quoted 23 lines · Vincent Donnefort

In preparation for the pKVM heap allocator, introduce __hyp_allocator_map() to map a single physical page to a given virtual address in the hypervisor stage-1 page-table.

arch/arm64/kvm/hyp/include/nvhe/mm.h +1 -0
index 725bb0fb941d..98a7774b541c 100644
@@ -32,5 +32,6 @@ int __pkvm_create_private_mapping(phys_addr_t phys, size_t size,
 int pkvm_create_stack(phys_addr_t phys, unsigned long *haddr);
 int pkvm_alloc_private_va_range(size_t size, unsigned long *haddr);
 phys_addr_t __pkvm_private_range_pa(void *va);
+int __hyp_allocator_map(void *va, phys_addr_t phys);
 
 #endif /* __KVM_HYP_MM_H */
arch/arm64/kvm/hyp/nvhe/mm.c +0 -0
index ec59da1322a7..c1c01f81ac5f 100644

arch/arm64/kvm/hyp/nvhe/mm.c void pkvm_remove_mappings(void *from, void *to) · line 167

Quoted 8 lines
arch/arm64/kvm/hyp/nvhe/mm.c +5 -0
@@ -160,6 +160,11 @@ void pkvm_remove_mappings(void *from, void *to)
        hyp_spin_unlock(&pkvm_pgd_lock);
 }
 
+int __hyp_allocator_map(void *va, phys_addr_t phys)
+{
+       return __pkvm_create_mappings((unsigned long)va, PAGE_SIZE, phys, PAGE_HYP);
+}
+

nit: __hyp_allocator_map() is named for its caller rather than what it does (map a single hyp page). Its siblings here (pkvm_create_mappings, pkvm_remove_mappings) are named for the operation. Consider a name like pkvm_map_page() so it reads as a generic primitive.

That said, naming is hard:

Cheers,
/fuad

Quoted 6 lines
arch/arm64/kvm/hyp/nvhe/mm.c +0 -1
 int hyp_back_vmemmap(phys_addr_t back)
 {
        unsigned long i, start, size, end = 0;
--

2.55.0.rc2.803.g1fd1e6609c-goog

#7 lore raw

Re: [PATCH v2 03/18] KVM: arm64: Add __hyp_allocator_map for the pKVM hyp

Quoted 27 lines · Fuad Tabba
Quoted 26 lines · Vincent Donnefort

In preparation for the pKVM heap allocator, introduce __hyp_allocator_map() to map a single physical page to a given virtual address in the hypervisor stage-1 page-table.

arch/arm64/kvm/hyp/include/nvhe/mm.h +1 -0
index 725bb0fb941d..98a7774b541c 100644
@@ -32,5 +32,6 @@ int __pkvm_create_private_mapping(phys_addr_t phys, size_t size,
 int pkvm_create_stack(phys_addr_t phys, unsigned long *haddr);
 int pkvm_alloc_private_va_range(size_t size, unsigned long *haddr);
 phys_addr_t __pkvm_private_range_pa(void *va);
+int __hyp_allocator_map(void *va, phys_addr_t phys);
 
 #endif /* __KVM_HYP_MM_H */
arch/arm64/kvm/hyp/nvhe/mm.c +0 -0
index ec59da1322a7..c1c01f81ac5f 100644
@@ -160,6 +160,11 @@ void pkvm_remove_mappings(void *from, void *to)
        hyp_spin_unlock(&pkvm_pgd_lock);
 }
 
Quoted 10 lines

arch/arm64/kvm/hyp/nvhe/mm.c void pkvm_remove_mappings(void *from, void *to) · line 167

Quoted 5 lines
arch/arm64/kvm/hyp/nvhe/mm.c +5 -0
+int __hyp_allocator_map(void *va, phys_addr_t phys)
+{
+       return __pkvm_create_mappings((unsigned long)va, PAGE_SIZE, phys, PAGE_HYP);
+}
+

nit: __hyp_allocator_map() is named for its caller rather than what it does (map a single hyp page). Its siblings here (pkvm_create_mappings, pkvm_remove_mappings) are named for the operation. Consider a name like pkvm_map_page() so it reads as a generic primitive.

IIRC (it was a few years ago) this was a suggestion from Quentin who didn't want to expose too much of mm and suggested to make it clear that was a function private to the allocator.

But looking at what we have now. perhaps

int __pkvm_map_private_pages(phys_addr_t phys, void *addr, size_t size);

And this could verify that addr is actually aprt of the private range

WDYS?

Quoted 14 lines

That said, naming is hard:

Cheers,
/fuad

Quoted 6 lines
arch/arm64/kvm/hyp/nvhe/mm.c +0 -1
 int hyp_back_vmemmap(phys_addr_t back)
 {
        unsigned long i, start, size, end = 0;
--

2.55.0.rc2.803.g1fd1e6609c-goog

reviewer
#8 lore raw

Re: [PATCH v2 03/18] KVM: arm64: Add __hyp_allocator_map for the pKVM hyp

Quoted 44 lines · Vincent Donnefort
Quoted 27 lines · Fuad Tabba
Quoted 26 lines · Vincent Donnefort

In preparation for the pKVM heap allocator, introduce __hyp_allocator_map() to map a single physical page to a given virtual address in the hypervisor stage-1 page-table.

arch/arm64/kvm/hyp/include/nvhe/mm.h +1 -0
index 725bb0fb941d..98a7774b541c 100644
@@ -32,5 +32,6 @@ int __pkvm_create_private_mapping(phys_addr_t phys, size_t size,
 int pkvm_create_stack(phys_addr_t phys, unsigned long *haddr);
 int pkvm_alloc_private_va_range(size_t size, unsigned long *haddr);
 phys_addr_t __pkvm_private_range_pa(void *va);
+int __hyp_allocator_map(void *va, phys_addr_t phys);
 
 #endif /* __KVM_HYP_MM_H */
arch/arm64/kvm/hyp/nvhe/mm.c +0 -0
index ec59da1322a7..c1c01f81ac5f 100644
@@ -160,6 +160,11 @@ void pkvm_remove_mappings(void *from, void *to)
        hyp_spin_unlock(&pkvm_pgd_lock);
 }
 
Quoted 10 lines

arch/arm64/kvm/hyp/nvhe/mm.c void pkvm_remove_mappings(void *from, void *to) · line 167

Quoted 5 lines
arch/arm64/kvm/hyp/nvhe/mm.c +5 -0
+int __hyp_allocator_map(void *va, phys_addr_t phys)
+{
+       return __pkvm_create_mappings((unsigned long)va, PAGE_SIZE, phys, PAGE_HYP);
+}
+

nit: __hyp_allocator_map() is named for its caller rather than what it does (map a single hyp page). Its siblings here (pkvm_create_mappings, pkvm_remove_mappings) are named for the operation. Consider a name like pkvm_map_page() so it reads as a generic primitive.

IIRC (it was a few years ago) this was a suggestion from Quentin who didn't want to expose too much of mm and suggested to make it clear that was a function private to the allocator.

Quoted 7 lines

But looking at what we have now. perhaps

int __pkvm_map_private_pages(phys_addr_t phys, void *addr, size_t size);

And this could verify that addr is actually aprt of the private range

WDYS?

I think this sounds better. That was a nit, so up to you.

Thanks,
/fuad

Quoted 15 lines
Quoted 15 lines

That said, naming is hard:

Cheers,
/fuad

Quoted 6 lines
arch/arm64/kvm/hyp/nvhe/mm.c +0 -1
 int hyp_back_vmemmap(phys_addr_t back)
 {
        unsigned long i, start, size, end = 0;
--

2.55.0.rc2.803.g1fd1e6609c-goog

#9 lore raw

PATCH v2 4/18 KVM: arm64: Add a heap allocator for the pKVM hyp

In reply to Vincent Donnefort

Currently, memory used by the hypervisor comes from donations that are embedded within HVCs. e.g. __pkvm_init_vm()'s hyp_vm.

This is cumbersome: the host needs to know the size of those struct, the memory must be page-aligned and physically contiguous which may be difficult to satisfy when host memory is highly fragmented.

Create a heap allocator to manage VA-contiguous memory. This allocator grows upward, recycles unused chunks of memory and provides a simple API to allocate and free:

hyp_alloc(size), hyp_free(addr)

This heap allocator also manages the underlying physical memory, allowing the host to top up the allocator's pool and reclaim memory.

hyp_alloc_topup(), hyp_alloc_reclaim().

Pages remain mapped in the allocator's VA-space as long as they are not reclaimed.

When the allocator runs out of memory, hyp_alloc() fails and hyp_alloc_errno() returns -ENOMEM to signal that a top-up is required.

Additionally, harden the allocator by hashing chunk headers to detect metadata corruption.

arch/arm64/kvm/hyp/include/nvhe/alloc.h new file +17 -0
new file mode 100644
index 000000000000..8f87a63f8946
@@ -0,0 +1,17 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+#ifndef __KVM_NVHE_ALLOC__
+#define __KVM_NVHE_ALLOC__
+#include <linux/types.h>
+
+#include <asm/kvm_host.h>
+
+void *hyp_alloc(size_t size);
+int hyp_alloc_errno(void);
+u32 hyp_alloc_topup_needed(void);
+void hyp_free(void *addr);
+
+int hyp_alloc_init(size_t size);
+int hyp_alloc_topup(struct kvm_hyp_memcache *host_mc);
+unsigned long hyp_alloc_reclaimable(void);
+void hyp_alloc_reclaim(struct kvm_hyp_memcache *host_mc, unsigned long target);
+#endif
arch/arm64/kvm/hyp/include/nvhe/spinlock.h +5 -0
index 7c7ea8c55405..c0b3a1a48e45 100644
@@ -16,6 +16,7 @@
 #include <asm/alternative.h>
 #include <asm/lse.h>
 #include <asm/rwonce.h>
+#include <linux/cleanup.h>
 
 typedef union hyp_spinlock {
 	u32	__val;
@@ -122,4 +123,8 @@ static inline void hyp_assert_lock_held(hyp_spinlock_t *lock)
 static inline void hyp_assert_lock_held(hyp_spinlock_t *lock) { }
 #endif
 
+DEFINE_LOCK_GUARD_1(hyp_spinlock, hyp_spinlock_t,
+		    hyp_spin_lock(_T->lock),
+		    hyp_spin_unlock(_T->lock))
+
 #endif /* __ARM64_KVM_NVHE_SPINLOCK_H__ */
arch/arm64/kvm/hyp/nvhe/Makefile +1 -1
index f57450ebcb49..985fe3343f4c 100644
@@ -23,7 +23,7 @@ lib-objs := $(addprefix ../../../lib/, $(lib-objs))
 CFLAGS_switch.nvhe.o += -Wno-override-init
 
 hyp-obj-y := timer-sr.o sysreg-sr.o debug-sr.o switch.o tlb.o hyp-init.o host.o \
-	 hyp-main.o hyp-smp.o psci-relay.o early_alloc.o page_alloc.o \
+	 hyp-main.o hyp-smp.o psci-relay.o alloc.o early_alloc.o page_alloc.o \
 	 cache.o setup.o mm.o mem_protect.o sys_regs.o pkvm.o stacktrace.o ffa.o
 hyp-obj-y += ../vgic-v3-sr.o ../aarch32.o ../vgic-v2-cpuif-proxy.o ../entry.o \
 	 ../hyp-entry.o ../exception.o ../pgtable.o ../vgic-v5-sr.o
arch/arm64/kvm/hyp/nvhe/alloc.c new file +1033 -0

1,033 lines not on this page · raw message

arch/arm64/kvm/hyp/nvhe/setup.c +5 -0
index 75b00c323310..1c10cf78c869 100644
@@ -10,6 +10,7 @@
 #include <asm/kvm_pgtable.h>
 #include <asm/kvm_pkvm.h>
 
+#include <nvhe/alloc.h>
 #include <nvhe/early_alloc.h>
 #include <nvhe/ffa.h>
 #include <nvhe/gfp.h>
@@ -363,6 +364,10 @@ int __pkvm_init(phys_addr_t phys, unsigned long size, unsigned long *per_cpu_bas
 	if (ret)
 		return ret;
 
+	ret = hyp_alloc_init(SZ_128M);
+	if (ret)
+		return ret;
+
 	update_nvhe_init_params();
 
 	/* Jump in the idmap page to switch to the new page-tables */
Signature · 1 line
2.55.0.rc2.803.g1fd1e6609c-goog
#10 lore raw

Re: [PATCH v2 04/18] KVM: arm64: Add a heap allocator for the pKVM hyp

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [Critical] hyp_allocator_map remaps already-mapped pages and corrupts unmapped tracker state due to…

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [Critical] hyp_allocator_map remaps already-mapped pages and corrupts unmapped tracker state due to unaligned next chunks.
- [High] Fatal EL2 WARN_ON creates a dead-branch trap and panics the hypervisor on global lock contention in hyp_allocator_alloc. --

commit d07913766ae6b18b86e593f40b4fc4a8165e2722
Author: Vincent Donnefort <[email protected]>

KVM: arm64: Add a heap allocator for the pKVM hyp

This commit creates a heap allocator to manage VA-contiguous memory for the hypervisor, replacing donations embedded within HVCs. It supports allocating, freeing, topping up from a host memcache, and reclaiming memory.

Quoted 4 lines
arch/arm64/kvm/hyp/nvhe/alloc.c new file +0 -0
index 0000000000000..07ef0a13855f7

[ ... ]

Quoted 18 lines
arch/arm64/kvm/hyp/nvhe/alloc.c new file +18 -0
+static int hyp_allocator_map(struct hyp_allocator *allocator, struct chunk_hdr *chunk,
+			     struct chunk_hdr *next,
+			     void *addr, void *end)
+{
+	void *unmapped = chunk ? chunk_unmapped(chunk) : allocator->first_unmapped;
+
+	/*
+	 * hyp_allocator_can_create_chunk() already validates addr/end
+	 * belong to the chunk.
+	 */
+	WARN_ON(end <= addr);
+
+	/* The chunk does not span an unmapped region */
+	if (!unmapped)
+		return 0;
+
+	while (unmapped < end) {
+		void *page = pop_hyp_memcache(&allocator->mc, hyp_phys_to_virt);

arch/arm64/kvm/hyp/nvhe/alloc.c line 351

Quoted 10 lines
arch/arm64/kvm/hyp/nvhe/alloc.c new file +10 -0
+		int ret;
+
+		if (!page) {
+			end = PTR_ALIGN(end, PAGE_SIZE);
+			*this_cpu_ptr(allocator->topup_needed) =
+				(unsigned long)(end - unmapped) >> PAGE_SHIFT;
+			return -ENOMEM;
+		}
+
+		ret = __hyp_allocator_map(unmapped, hyp_virt_to_phys(page));

[Severity: Critical]
Does this loop correctly handle unaligned next chunks when re-mapping reclaimed memory?

If the allocator reclaims a chunk where the next chunk is unaligned (e.g., at PAGE_SIZE + 32), the unmapped hole is safely truncated to PAGE_SIZE by PTR_ALIGN_DOWN(next, PAGE_SIZE). However, if the chunk is later reused with an end address extending into the partially shared page (e.g., PAGE_SIZE + 16):

The loop in hyp_allocator_map() iterates while (unmapped < end). When unmapped reaches the end of the true hole (PAGE_SIZE), the loop continues because PAGE_SIZE < PAGE_SIZE + 16. It then pops a new physical page and maps it at PAGE_SIZE, overwriting the existing active PTE containing the next chunk.

Additionally, the unmapped == next reset check fails because of the unaligned next pointer, propagating the corrupted tracker state.

[ ... ]

Quoted 15 lines
arch/arm64/kvm/hyp/nvhe/alloc.c new file +15 -0
+static void *hyp_allocator_alloc(struct hyp_allocator *allocator, size_t size)
+{
+	struct chunk_hdr *chunk;
+
+	size = max(size, MIN_ALLOC_SIZE);
+
+	/* Ensure we do not overflow ALIGN(MIN_ALLOC_SIZE) */
+	if (size > U32_MAX) {
+		hyp_allocator_set_errno(allocator, -E2BIG);
+		return NULL;
+	}
+
+	size = ALIGN(size, MIN_ALLOC_SIZE);
+	if (size > (allocator->end - allocator->start - chunk_hdr_size())) {
+		hyp_allocator_set_errno(allocator, -E2BIG);

arch/arm64/kvm/hyp/nvhe/alloc.c line 690

Quoted 10 lines
arch/arm64/kvm/hyp/nvhe/alloc.c new file +10 -0
+		return NULL;
+	}
+
+#ifdef CONFIG_NVHE_EL2_DEBUG
+	/* The allocator can modify the hyp stage-1 */
+	if (WARN_ON(hyp_spin_is_locked(&pkvm_pgd_lock))) {
+		hyp_allocator_set_errno(allocator, -EINVAL);
+		return NULL;
+	}
+#endif

[Severity: High]
Can this lock check trigger a hypervisor panic under normal SMP contention?

At EL2, WARN_ON() expands to a fatal BRK instruction, meaning the error handling path here acts as a dead-branch trap and will never execute.

Furthermore, hyp_spin_is_locked() returns true if the lock is held by any CPU, not just the local one. If host userspace triggers concurrent VM creations or VCPU initializations, driving concurrent EL2 hypercalls to hyp_alloc(), CPU A might hold pkvm_pgd_lock while CPU B concurrently evaluates this WARN_ON().

This converts standard SMP lock contention into a fatal host denial of service.

Quoted 8 lines
arch/arm64/kvm/hyp/nvhe/alloc.c new file +8 -0
+	scoped_guard(hyp_spinlock, &allocator->lock) {
+		chunk = hyp_allocator_alloc_chunk(allocator, size);
+		if (IS_ERR_OR_NULL(chunk)) {
+			hyp_allocator_set_errno(allocator,
+						IS_ERR(chunk) ? PTR_ERR(chunk) : -EINVAL);
+			return NULL;
+		}
+	}
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
#11 lore raw

Re: [PATCH v2 04/18] KVM: arm64: Add a heap allocator for the pKVM hyp

Quoted 59 lines · On Mon, Jul 06, 2026 at 06:12:41PM +0000, [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [Critical] hyp_allocator_map remaps already-mapped pages and corrupts unmapped tracker state due to unaligned next chunks.
- [High] Fatal EL2 WARN_ON creates a dead-branch trap and panics the hypervisor on global lock contention in hyp_allocator_alloc. --

commit d07913766ae6b18b86e593f40b4fc4a8165e2722
Author: Vincent Donnefort <[email protected]>

KVM: arm64: Add a heap allocator for the pKVM hyp

This commit creates a heap allocator to manage VA-contiguous memory for the hypervisor, replacing donations embedded within HVCs. It supports allocating, freeing, topping up from a host memcache, and reclaiming memory.

Quoted 4 lines
arch/arm64/kvm/hyp/nvhe/alloc.c new file +0 -0
index 0000000000000..07ef0a13855f7

[ ... ]

Quoted 18 lines
arch/arm64/kvm/hyp/nvhe/alloc.c new file +18 -0
+static int hyp_allocator_map(struct hyp_allocator *allocator, struct chunk_hdr *chunk,
+			     struct chunk_hdr *next,
+			     void *addr, void *end)
+{
+	void *unmapped = chunk ? chunk_unmapped(chunk) : allocator->first_unmapped;
+
+	/*
+	 * hyp_allocator_can_create_chunk() already validates addr/end
+	 * belong to the chunk.
+	 */
+	WARN_ON(end <= addr);
+
+	/* The chunk does not span an unmapped region */
+	if (!unmapped)
+		return 0;
+
+	while (unmapped < end) {
+		void *page = pop_hyp_memcache(&allocator->mc, hyp_phys_to_virt);

arch/arm64/kvm/hyp/nvhe/alloc.c line 351

Quoted 10 lines
arch/arm64/kvm/hyp/nvhe/alloc.c new file +10 -0
+		int ret;
+
+		if (!page) {
+			end = PTR_ALIGN(end, PAGE_SIZE);
+			*this_cpu_ptr(allocator->topup_needed) =
+				(unsigned long)(end - unmapped) >> PAGE_SHIFT;
+			return -ENOMEM;
+		}
+
+		ret = __hyp_allocator_map(unmapped, hyp_virt_to_phys(page));

[Severity: Critical]
Does this loop correctly handle unaligned next chunks when re-mapping reclaimed memory?

If the allocator reclaims a chunk where the next chunk is unaligned (e.g., at PAGE_SIZE + 32), the unmapped hole is safely truncated to PAGE_SIZE by PTR_ALIGN_DOWN(next, PAGE_SIZE). However, if the chunk is later reused with an end address extending into the partially shared page (e.g., PAGE_SIZE + 16):

Quoted 7 lines

The loop in hyp_allocator_map() iterates while (unmapped < end). When unmapped reaches the end of the true hole (PAGE_SIZE), the loop continues because PAGE_SIZE < PAGE_SIZE + 16. It then pops a new physical page and maps it at PAGE_SIZE, overwriting the existing active PTE containing the next chunk.

Additionally, the unmapped == next reset check fails because of the unaligned next pointer, propagating the corrupted tracker state.

I don't believe this can happen. Chunks __always__ describe a mapped section, followed (or not) by an unmapped one. So if we are adding mappings to a chunk, next has to be PAGE_ALIGNED.

However, I could add a WARN_ON() here.

Quoted 35 lines

[ ... ]

Quoted 15 lines
arch/arm64/kvm/hyp/nvhe/alloc.c new file +15 -0
+static void *hyp_allocator_alloc(struct hyp_allocator *allocator, size_t size)
+{
+	struct chunk_hdr *chunk;
+
+	size = max(size, MIN_ALLOC_SIZE);
+
+	/* Ensure we do not overflow ALIGN(MIN_ALLOC_SIZE) */
+	if (size > U32_MAX) {
+		hyp_allocator_set_errno(allocator, -E2BIG);
+		return NULL;
+	}
+
+	size = ALIGN(size, MIN_ALLOC_SIZE);
+	if (size > (allocator->end - allocator->start - chunk_hdr_size())) {
+		hyp_allocator_set_errno(allocator, -E2BIG);

arch/arm64/kvm/hyp/nvhe/alloc.c line 690

Quoted 10 lines
arch/arm64/kvm/hyp/nvhe/alloc.c new file +10 -0
+		return NULL;
+	}
+
+#ifdef CONFIG_NVHE_EL2_DEBUG
+	/* The allocator can modify the hyp stage-1 */
+	if (WARN_ON(hyp_spin_is_locked(&pkvm_pgd_lock))) {
+		hyp_allocator_set_errno(allocator, -EINVAL);
+		return NULL;
+	}
+#endif

[Severity: High]
Can this lock check trigger a hypervisor panic under normal SMP contention?

At EL2, WARN_ON() expands to a fatal BRK instruction, meaning the error handling path here acts as a dead-branch trap and will never execute.

Quoted 6 lines

Furthermore, hyp_spin_is_locked() returns true if the lock is held by any CPU, not just the local one. If host userspace triggers concurrent VM creations or VCPU initializations, driving concurrent EL2 hypercalls to hyp_alloc(), CPU A might hold pkvm_pgd_lock while CPU B concurrently evaluates this WARN_ON().

This converts standard SMP lock contention into a fatal host denial of service.

Duh, this hyp_spin_is_locked() is of course utterly broken! I'll add a comment for hyp_alloc() instead.

Quoted 12 lines
Quoted 9 lines
arch/arm64/kvm/hyp/nvhe/alloc.c new file +8 -0
+	scoped_guard(hyp_spinlock, &allocator->lock) {
+		chunk = hyp_allocator_alloc_chunk(allocator, size);
+		if (IS_ERR_OR_NULL(chunk)) {
+			hyp_allocator_set_errno(allocator,
+						IS_ERR(chunk) ? PTR_ERR(chunk) : -EINVAL);
+			return NULL;
+		}
+	}
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
reviewer
#12 lore raw

Re: [PATCH v2 04/18] KVM: arm64: Add a heap allocator for the pKVM hyp

In reply to Vincent Donnefort

On Mon, 6 Jul 2026 at 18:54, Vincent Donnefort <[email protected]> wrote:

...

arch/arm64/kvm/hyp/nvhe/alloc.c line 79

Quoted 8 lines
arch/arm64/kvm/hyp/nvhe/alloc.c new file +8 -0
+static u32 chunk_hash_compute(const struct chunk_hdr *chunk)
+{
+       u32 hash = 0;
+
+       BUILD_BUG_ON(sizeof(*chunk) != 16);
+
+       hash ^= hash_64(*(const u64 *)chunk, 32);
+       hash ^= hash_32(chunk->__unmapped, 32);

Could you state the threat model in the commit msg? The VA range is hyp-private and the hash is unkeyed (anything that can write EL2 memory recomputes it), so this only catches _accidental_ corruption, i.e. a debug aid.

Since that's the goal, should it be gated on CONFIG_NVHE_EL2_DEBUG like the other checks here? As written it's O(n) hashing per hyp_alloc(), in production, for a check that only fires on a hyp bug. And a plain XOR/additive checksum would be cheaper and just as good for accidental corruption.

Quoted 3 lines
arch/arm64/kvm/hyp/nvhe/alloc.c new file +3 -0
+       return hash;
+}
+

...

Quoted 13 lines
arch/arm64/kvm/hyp/nvhe/setup.c +1 -0
index 75b00c323310..1c10cf78c869 100644
@@ -10,6 +10,7 @@
 #include <asm/kvm_pgtable.h>
 #include <asm/kvm_pkvm.h>
 
+#include <nvhe/alloc.h>
 #include <nvhe/early_alloc.h>
 #include <nvhe/ffa.h>
 #include <nvhe/gfp.h>

arch/arm64/kvm/hyp/nvhe/setup.c int __pkvm_init(phys_addr_t phys, unsigned long size, unsigned long *per_cpu_bas · line 367

Quoted 4 lines
arch/arm64/kvm/hyp/nvhe/setup.c +1 -0
@@ -363,6 +364,10 @@ int __pkvm_init(phys_addr_t phys, unsigned long size, unsigned long *per_cpu_bas
        if (ret)
                return ret;
 
+       ret = hyp_alloc_init(SZ_128M);

Where does 128M come from? A comment would help.

Cheers,
/fuad

Quoted 9 lines
arch/arm64/kvm/hyp/nvhe/setup.c +3 -1
+       if (ret)
+               return ret;
+
        update_nvhe_init_params();
 
        /* Jump in the idmap page to switch to the new page-tables */
--

2.55.0.rc2.803.g1fd1e6609c-goog

reviewer
#13 lore raw

Re: [PATCH v2 04/18] KVM: arm64: Add a heap allocator for the pKVM hyp

Quoted 19 lines · Fuad Tabba

On Mon, 6 Jul 2026 at 18:54, Vincent Donnefort <[email protected]> wrote:

...

arch/arm64/kvm/hyp/nvhe/alloc.c line 79

Quoted 8 lines
arch/arm64/kvm/hyp/nvhe/alloc.c new file +8 -0
+static u32 chunk_hash_compute(const struct chunk_hdr *chunk)
+{
+       u32 hash = 0;
+
+       BUILD_BUG_ON(sizeof(*chunk) != 16);
+
+       hash ^= hash_64(*(const u64 *)chunk, 32);
+       hash ^= hash_32(chunk->__unmapped, 32);

Could you state the threat model in the commit msg? The VA range is hyp-private and the hash is unkeyed (anything that can write EL2 memory recomputes it), so this only catches _accidental_ corruption, i.e. a debug aid.

Quoted 5 lines

Since that's the goal, should it be gated on CONFIG_NVHE_EL2_DEBUG like the other checks here? As written it's O(n) hashing per hyp_alloc(), in production, for a check that only fires on a hyp bug. And a plain XOR/additive checksum would be cheaper and just as good for accidental corruption.

Thinking about it, I was wrong to say that it would be just as good, slightly cheaper, but it would detect things like swaps.

Still, I think this should be gated.

Cheers,
/fuad

Quoted 42 lines
Quoted 4 lines
arch/arm64/kvm/hyp/nvhe/alloc.c new file +3 -0
+       return hash;
+}
+

...

Quoted 13 lines
arch/arm64/kvm/hyp/nvhe/setup.c +1 -0
index 75b00c323310..1c10cf78c869 100644
@@ -10,6 +10,7 @@
 #include <asm/kvm_pgtable.h>
 #include <asm/kvm_pkvm.h>
 
+#include <nvhe/alloc.h>
 #include <nvhe/early_alloc.h>
 #include <nvhe/ffa.h>
 #include <nvhe/gfp.h>

arch/arm64/kvm/hyp/nvhe/setup.c int __pkvm_init(phys_addr_t phys, unsigned long size, unsigned long *per_cpu_bas · line 367

Quoted 4 lines
arch/arm64/kvm/hyp/nvhe/setup.c +1 -0
@@ -363,6 +364,10 @@ int __pkvm_init(phys_addr_t phys, unsigned long size, unsigned long *per_cpu_bas
        if (ret)
                return ret;
 
+       ret = hyp_alloc_init(SZ_128M);

Where does 128M come from? A comment would help.

Cheers,
/fuad

Quoted 9 lines
arch/arm64/kvm/hyp/nvhe/setup.c +3 -1
+       if (ret)
+               return ret;
+
        update_nvhe_init_params();
 
        /* Jump in the idmap page to switch to the new page-tables */
--

2.55.0.rc2.803.g1fd1e6609c-goog

#14 lore raw

Re: [PATCH v2 04/18] KVM: arm64: Add a heap allocator for the pKVM hyp

Quoted 26 lines · Fuad Tabba
Quoted 19 lines · Fuad Tabba

On Mon, 6 Jul 2026 at 18:54, Vincent Donnefort <[email protected]> wrote:

...

arch/arm64/kvm/hyp/nvhe/alloc.c line 79

Quoted 8 lines
arch/arm64/kvm/hyp/nvhe/alloc.c new file +8 -0
+static u32 chunk_hash_compute(const struct chunk_hdr *chunk)
+{
+       u32 hash = 0;
+
+       BUILD_BUG_ON(sizeof(*chunk) != 16);
+
+       hash ^= hash_64(*(const u64 *)chunk, 32);
+       hash ^= hash_32(chunk->__unmapped, 32);

Could you state the threat model in the commit msg? The VA range is hyp-private and the hash is unkeyed (anything that can write EL2 memory recomputes it), so this only catches _accidental_ corruption, i.e. a debug aid.

Quoted 5 lines

Since that's the goal, should it be gated on CONFIG_NVHE_EL2_DEBUG like the other checks here? As written it's O(n) hashing per hyp_alloc(), in production, for a check that only fires on a hyp bug. And a plain XOR/additive checksum would be cheaper and just as good for accidental corruption.

Quoted 4 lines

Thinking about it, I was wrong to say that it would be just as good, slightly cheaper, but it would detect things like swaps.

Still, I think this should be gated.

After a longer offline discussion, we concluded this doesn't cost anything while the weak protection can still be effective against attackers who may not always control the entire bit-width.

I'll add some details in the commit message.

Quoted 31 lines

Cheers,
/fuad

Quoted 27 lines
Quoted 4 lines
arch/arm64/kvm/hyp/nvhe/alloc.c new file +3 -0
+       return hash;
+}
+

...

Quoted 13 lines
arch/arm64/kvm/hyp/nvhe/setup.c +1 -0
index 75b00c323310..1c10cf78c869 100644
@@ -10,6 +10,7 @@
 #include <asm/kvm_pgtable.h>
 #include <asm/kvm_pkvm.h>
 
+#include <nvhe/alloc.h>
 #include <nvhe/early_alloc.h>
 #include <nvhe/ffa.h>
 #include <nvhe/gfp.h>

arch/arm64/kvm/hyp/nvhe/setup.c int __pkvm_init(phys_addr_t phys, unsigned long size, unsigned long *per_cpu_bas · line 367

Quoted 4 lines
arch/arm64/kvm/hyp/nvhe/setup.c +1 -0
@@ -363,6 +364,10 @@ int __pkvm_init(phys_addr_t phys, unsigned long size, unsigned long *per_cpu_bas
        if (ret)
                return ret;
 
+       ret = hyp_alloc_init(SZ_128M);

Where does 128M come from? A comment would help.

This is arbitrary, big enough to allow us to allocate plenty of VMs with tracing enable. But I will add something.

Quoted 15 lines
Quoted 15 lines

Cheers,
/fuad

Quoted 9 lines
arch/arm64/kvm/hyp/nvhe/setup.c +3 -1
+       if (ret)
+               return ret;
+
        update_nvhe_init_params();
 
        /* Jump in the idmap page to switch to the new page-tables */
--

2.55.0.rc2.803.g1fd1e6609c-goog

#15 lore raw

PATCH v2 5/18 KVM: arm64: Allow kvm_hyp_memcache usage outside of stage-2

In reply to Vincent Donnefort

Although currently limited to guest stage-2 page-table allocations, struct kvm_hyp_memcache is a useful primitive for passing a list of discontiguous pages between host and hypervisor.

Introduce init_hyp_memcache() to initialise a generic hyp memcache, and init_hyp_stage2_memcache() for stage-2 specific memcaches. The generic initialiser will be used to top up the upcoming pKVM heap allocator.

arch/arm64/include/asm/kvm_host.h +13 -0
index bae2c4f92ef5..caea8e9986d1 100644
@@ -91,9 +91,22 @@ struct kvm_hyp_memcache {
 	struct pkvm_mapping *mapping; /* only used from EL1 */
 
 #define	HYP_MEMCACHE_ACCOUNT_STAGE2	BIT(1)
+#define	HYP_MEMCACHE_ACCOUNT_KMEMCG	BIT(2)
 	unsigned long flags;
 };
 
+static inline void init_hyp_memcache(struct kvm_hyp_memcache *mc)
+{
+	memset(mc, 0, sizeof(*mc));
+	mc->mapping = ZERO_SIZE_PTR; /* Prevent allocation, solely useful for stage2 memcache */
+}
+
+static inline void init_hyp_stage2_memcache(struct kvm_hyp_memcache *mc)
+{
+	memset(mc, 0, sizeof(*mc));
+	mc->flags = HYP_MEMCACHE_ACCOUNT_STAGE2 | HYP_MEMCACHE_ACCOUNT_KMEMCG;
+}
+
 static inline void push_hyp_memcache(struct kvm_hyp_memcache *mc,
 				     phys_addr_t *p,
 				     phys_addr_t (*to_pa)(void *virt))
arch/arm64/kvm/mmu.c +3 -1
index 6c941aaa10c6..fb38821f2037 100644
@@ -1159,9 +1159,11 @@ static void hyp_mc_free_fn(void *addr, void *mc)
 static void *hyp_mc_alloc_fn(void *mc)
 {
 	struct kvm_hyp_memcache *memcache = mc;
+	gfp_t gfp = (memcache->flags & HYP_MEMCACHE_ACCOUNT_KMEMCG) ?
+		    GFP_KERNEL_ACCOUNT : GFP_KERNEL;
 	void *addr;
 
-	addr = (void *)__get_free_page(GFP_KERNEL_ACCOUNT);
+	addr = (void *)__get_free_page(gfp);
 	if (addr && memcache->flags & HYP_MEMCACHE_ACCOUNT_STAGE2)
 		kvm_account_pgtable_pages(addr, 1);
 
arch/arm64/kvm/pkvm.c +2 -2
index 053e4f733e4b..8324a6a1bc48 100644
@@ -111,7 +111,7 @@ static int __pkvm_create_hyp_vcpu(struct kvm_vcpu *vcpu)
 	void *hyp_vcpu;
 	int ret;
 
-	vcpu->arch.pkvm_memcache.flags |= HYP_MEMCACHE_ACCOUNT_STAGE2;
+	init_hyp_stage2_memcache(&vcpu->arch.pkvm_memcache);
 
 	hyp_vcpu = alloc_pages_exact(hyp_vcpu_sz, GFP_KERNEL_ACCOUNT);
 	if (!hyp_vcpu)
@@ -172,7 +172,7 @@ static int __pkvm_create_hyp_vm(struct kvm *kvm)
 		goto free_vm;
 
 	kvm->arch.pkvm.is_created = true;
-	kvm->arch.pkvm.stage2_teardown_mc.flags |= HYP_MEMCACHE_ACCOUNT_STAGE2;
+	init_hyp_stage2_memcache(&kvm->arch.pkvm.stage2_teardown_mc);
 	kvm_account_pgtable_pages(pgd, pgd_sz / PAGE_SIZE);
 
 	return 0;
Signature · 1 line
2.55.0.rc2.803.g1fd1e6609c-goog
reviewer Reviewed-by on v2 5/18
#16 lore raw

Re: [PATCH v2 05/18] KVM: arm64: Allow kvm_hyp_memcache usage outside of stage-2

Quoted 43 lines · Vincent Donnefort

Although currently limited to guest stage-2 page-table allocations, struct kvm_hyp_memcache is a useful primitive for passing a list of discontiguous pages between host and hypervisor.

Introduce init_hyp_memcache() to initialise a generic hyp memcache, and init_hyp_stage2_memcache() for stage-2 specific memcaches. The generic initialiser will be used to top up the upcoming pKVM heap allocator.

arch/arm64/include/asm/kvm_host.h +13 -0
index bae2c4f92ef5..caea8e9986d1 100644
@@ -91,9 +91,22 @@ struct kvm_hyp_memcache {
        struct pkvm_mapping *mapping; /* only used from EL1 */
 
 #define        HYP_MEMCACHE_ACCOUNT_STAGE2     BIT(1)
+#define        HYP_MEMCACHE_ACCOUNT_KMEMCG     BIT(2)
        unsigned long flags;
 };
 
+static inline void init_hyp_memcache(struct kvm_hyp_memcache *mc)
+{
+       memset(mc, 0, sizeof(*mc));
+       mc->mapping = ZERO_SIZE_PTR; /* Prevent allocation, solely useful for stage2 memcache */
+}
+
+static inline void init_hyp_stage2_memcache(struct kvm_hyp_memcache *mc)
+{
+       memset(mc, 0, sizeof(*mc));
+       mc->flags = HYP_MEMCACHE_ACCOUNT_STAGE2 | HYP_MEMCACHE_ACCOUNT_KMEMCG;
+}
+
 static inline void push_hyp_memcache(struct kvm_hyp_memcache *mc,
                                     phys_addr_t *p,
                                     phys_addr_t (*to_pa)(void *virt))
arch/arm64/kvm/mmu.c +0 -0
index 6c941aaa10c6..fb38821f2037 100644

arch/arm64/kvm/mmu.c static void hyp_mc_free_fn(void *addr, void *mc) · line 1166

Quoted 9 lines
arch/arm64/kvm/mmu.c +3 -1
@@ -1159,9 +1159,11 @@ static void hyp_mc_free_fn(void *addr, void *mc)
 static void *hyp_mc_alloc_fn(void *mc)
 {
        struct kvm_hyp_memcache *memcache = mc;
+       gfp_t gfp = (memcache->flags & HYP_MEMCACHE_ACCOUNT_KMEMCG) ?
+                   GFP_KERNEL_ACCOUNT : GFP_KERNEL;
        void *addr;
 
-       addr = (void *)__get_free_page(GFP_KERNEL_ACCOUNT);
+       addr = (void *)__get_free_page(gfp);

Generic memcaches are not accounted anymore. That looks intentional and right since heap pages are hyp-global, not attributable to the topup'ing task. Maybe worth a line in the commit message confirming this is deliberate?

Cheers,
/fuad

Quoted 28 lines
arch/arm64/kvm/mmu.c +0 -0
        if (addr && memcache->flags & HYP_MEMCACHE_ACCOUNT_STAGE2)
                kvm_account_pgtable_pages(addr, 1);
 
arch/arm64/kvm/pkvm.c +2 -3
index 053e4f733e4b..8324a6a1bc48 100644
@@ -111,7 +111,7 @@ static int __pkvm_create_hyp_vcpu(struct kvm_vcpu *vcpu)
        void *hyp_vcpu;
        int ret;
 
-       vcpu->arch.pkvm_memcache.flags |= HYP_MEMCACHE_ACCOUNT_STAGE2;
+       init_hyp_stage2_memcache(&vcpu->arch.pkvm_memcache);
 
        hyp_vcpu = alloc_pages_exact(hyp_vcpu_sz, GFP_KERNEL_ACCOUNT);
        if (!hyp_vcpu)
@@ -172,7 +172,7 @@ static int __pkvm_create_hyp_vm(struct kvm *kvm)
                goto free_vm;
 
        kvm->arch.pkvm.is_created = true;
-       kvm->arch.pkvm.stage2_teardown_mc.flags |= HYP_MEMCACHE_ACCOUNT_STAGE2;
+       init_hyp_stage2_memcache(&kvm->arch.pkvm.stage2_teardown_mc);
        kvm_account_pgtable_pages(pgd, pgd_sz / PAGE_SIZE);
 
        return 0;
--

2.55.0.rc2.803.g1fd1e6609c-goog

#17 lore raw

PATCH v2 6/18 KVM: arm64: Add topup interface for the pKVM heap allocator

In reply to Vincent Donnefort

Introduce a host HVC interface and a host side helper to allow refilling the pKVM heap allocator.

arch/arm64/include/asm/kvm_asm.h +1 -0
index 043495f7fc78..681b7bf8ac08 100644
@@ -114,6 +114,7 @@ enum __kvm_host_smccc_func {
 	__KVM_HOST_SMCCC_FUNC___pkvm_vcpu_load,
 	__KVM_HOST_SMCCC_FUNC___pkvm_vcpu_put,
 	__KVM_HOST_SMCCC_FUNC___pkvm_tlb_flush_vmid,
+	__KVM_HOST_SMCCC_FUNC___pkvm_hyp_topup,
 
 	MARKER(__KVM_HOST_SMCCC_FUNC_MAX)
 };
arch/arm64/include/asm/kvm_pkvm.h +4 -0
index 74fedd9c5ff0..870f7870bb62 100644
@@ -17,6 +17,10 @@
 
 #define HYP_MEMBLOCK_REGIONS 128
 
+enum pkvm_topup_id {
+	PKVM_TOPUP_HYP_ALLOC,
+};
+
 int pkvm_init_host_vm(struct kvm *kvm, unsigned long type);
 int pkvm_create_hyp_vm(struct kvm *kvm);
 bool pkvm_hyp_vm_is_created(struct kvm *kvm);
arch/arm64/kvm/hyp/nvhe/hyp-main.c +26 -0
index d3c69de698f4..e270a974fa37 100644
@@ -15,6 +15,7 @@
 #include <asm/kvm_hypevents.h>
 #include <asm/kvm_mmu.h>
 
+#include <nvhe/alloc.h>
 #include <nvhe/ffa.h>
 #include <nvhe/mem_protect.h>
 #include <nvhe/mm.h>
@@ -632,6 +633,30 @@ static void handle___pkvm_finalize_teardown_vm(struct kvm_cpu_context *host_ctxt
 	cpu_reg(host_ctxt, 1) = __pkvm_finalize_teardown_vm(handle);
 }
 
+static void handle___pkvm_hyp_topup(struct kvm_cpu_context *host_ctxt)
+{
+	DECLARE_REG(enum pkvm_topup_id, id, host_ctxt, 1);
+	DECLARE_REG(phys_addr_t, head, host_ctxt, 2);
+	DECLARE_REG(unsigned long, nr_pages, host_ctxt, 3);
+	struct kvm_hyp_memcache host_mc = {
+		.head = head,
+		.nr_pages = nr_pages,
+	};
+	int ret;
+
+	switch (id) {
+	case PKVM_TOPUP_HYP_ALLOC:
+		ret = hyp_alloc_topup(&host_mc);
+		break;
+	default:
+		ret = -EINVAL;
+	}
+
+	cpu_reg(host_ctxt, 1) = ret;
+	cpu_reg(host_ctxt, 2) = host_mc.head;
+	cpu_reg(host_ctxt, 3) = host_mc.nr_pages;
+}
+
 static void handle___tracing_load(struct kvm_cpu_context *host_ctxt)
 {
 	DECLARE_REG(unsigned long, desc_hva, host_ctxt, 1);
@@ -762,6 +787,7 @@ static const hcall_t host_hcall[] = {
 	HANDLE_FUNC(__pkvm_vcpu_load),
 	HANDLE_FUNC(__pkvm_vcpu_put),
 	HANDLE_FUNC(__pkvm_tlb_flush_vmid),
+	HANDLE_FUNC(__pkvm_hyp_topup),
 };
 
 static void handle_host_hcall(struct kvm_cpu_context *host_ctxt)
arch/arm64/kvm/pkvm.c +27 -0
index 8324a6a1bc48..e2c8714e8ccc 100644
@@ -85,6 +85,33 @@ void __init kvm_hyp_reserve(void)
 		 hyp_mem_base);
 }
 
+static int pkvm_hyp_topup(enum pkvm_topup_id id, unsigned long nr_pages)
+{
+	struct kvm_hyp_memcache mc;
+	struct arm_smccc_res res;
+	int ret;
+
+	init_hyp_memcache(&mc);
+	ret = topup_hyp_memcache(&mc, nr_pages);
+	if (ret)
+		goto err;
+
+	arm_smccc_1_1_hvc(KVM_HOST_SMCCC_FUNC(__pkvm_hyp_topup), id, mc.head,
+			  mc.nr_pages, &res);
+	if (WARN_ON_ONCE(res.a0 != SMCCC_RET_SUCCESS)) {
+		ret = -EINVAL;
+		goto err;
+	}
+
+	ret = res.a1;
+	mc.head = res.a2;
+	mc.nr_pages = res.a3;
+
+err:
+	free_hyp_memcache(&mc);
+	return ret;
+}
+
 static void __pkvm_destroy_hyp_vm(struct kvm *kvm)
 {
 	if (pkvm_hyp_vm_is_created(kvm)) {
Signature · 1 line
2.55.0.rc2.803.g1fd1e6609c-goog
#18 lore raw

Re: [PATCH v2 06/18] KVM: arm64: Add topup interface for the pKVM heap allocator

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:

New issues:
- [Low] Unused static function pkvm_hyp_topup causes build failure.

Pre-existing issues:
- [High] __pkvm_host_donate_hyp() handles allocator failures with WARN_ON(), allowing userspace to trivially panic the hypervisor on memory exhaustion. --

commit ed5697a7fc612043a616e5de99f36c231db3d5dd
Author: Vincent Donnefort <[email protected]>

KVM: arm64: Add topup interface for the pKVM heap allocator

Introduce a host HVC interface and a host side helper to allow refilling the pKVM heap allocator.

Quoted 4 lines
arch/arm64/kvm/pkvm.c +0 -0
index 8324a6a1bc48b..e2c8714e8ccc0 100644

[ ... ]

arch/arm64/kvm/pkvm.c void __init kvm_hyp_reserve(void) · line 91

Quoted 8 lines
arch/arm64/kvm/pkvm.c +4 -0
@@ -85,6 +85,33 @@ void __init kvm_hyp_reserve(void)
 		 hyp_mem_base);
 }
 
+static int pkvm_hyp_topup(enum pkvm_topup_id id, unsigned long nr_pages)
+{
+	struct kvm_hyp_memcache mc;
+	struct arm_smccc_res res;

[Severity: Low]
Does this cause a build failure with CONFIG_WERROR=y?

This static function is added here but doesn't appear to be called anywhere in this patch, which usually triggers a -Wunused-function warning. While it may be used in a subsequent patch in the series, each patch should compile independently to preserve git bisectability.

[Severity: High]
This is a pre-existing issue, but can host userspace crash the hypervisor by exhausting EL2 memory?

In arch/arm64/kvm/hyp/nvhe/mem_protect.c:__pkvm_host_donate_hyp():

    WARN_ON(pkvm_create_mappings_locked(virt, virt + size, PAGE_HYP));
    WARN_ON(host_stage2_set_owner_locked(phys, size, PKVM_ID_HYP));

At EL2 in nVHE/pKVM, WARN_ON() expands to BRK and panics the hypervisor. If pkvm_create_mappings_locked() requires a new page table level and the EL2 buddy allocator is exhausted, it returns -ENOMEM.

By wrapping this in WARN_ON(), a fallible allocator outcome is transformed into a fatal EL2 panic. This allows host userspace to trivially panic the host kernel by exhausting EL2 memory, such as by creating numerous VMs.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=6
reviewer
#19 lore raw

Re: [PATCH v2 06/18] KVM: arm64: Add topup interface for the pKVM heap allocator

In reply to Vincent Donnefort
Quoted 46 lines · Vincent Donnefort

Introduce a host HVC interface and a host side helper to allow refilling the pKVM heap allocator.

arch/arm64/include/asm/kvm_asm.h +1 -0
index 043495f7fc78..681b7bf8ac08 100644
@@ -114,6 +114,7 @@ enum __kvm_host_smccc_func {
        __KVM_HOST_SMCCC_FUNC___pkvm_vcpu_load,
        __KVM_HOST_SMCCC_FUNC___pkvm_vcpu_put,
        __KVM_HOST_SMCCC_FUNC___pkvm_tlb_flush_vmid,
+       __KVM_HOST_SMCCC_FUNC___pkvm_hyp_topup,
 
        MARKER(__KVM_HOST_SMCCC_FUNC_MAX)
 };
arch/arm64/include/asm/kvm_pkvm.h +4 -0
index 74fedd9c5ff0..870f7870bb62 100644
@@ -17,6 +17,10 @@
 
 #define HYP_MEMBLOCK_REGIONS 128
 
+enum pkvm_topup_id {
+       PKVM_TOPUP_HYP_ALLOC,
+};
+
 int pkvm_init_host_vm(struct kvm *kvm, unsigned long type);
 int pkvm_create_hyp_vm(struct kvm *kvm);
 bool pkvm_hyp_vm_is_created(struct kvm *kvm);
arch/arm64/kvm/hyp/nvhe/hyp-main.c +1 -0
index d3c69de698f4..e270a974fa37 100644
@@ -15,6 +15,7 @@
 #include <asm/kvm_hypevents.h>
 #include <asm/kvm_mmu.h>
 
+#include <nvhe/alloc.h>
 #include <nvhe/ffa.h>
 #include <nvhe/mem_protect.h>
 #include <nvhe/mm.h>

arch/arm64/kvm/hyp/nvhe/hyp-main.c static void handle___pkvm_finalize_teardown_vm(struct kvm_cpu_context *host_ctxt · line 636

Quoted 4 lines
arch/arm64/kvm/hyp/nvhe/hyp-main.c +1 -0
@@ -632,6 +633,30 @@ static void handle___pkvm_finalize_teardown_vm(struct kvm_cpu_context *host_ctxt
        cpu_reg(host_ctxt, 1) = __pkvm_finalize_teardown_vm(handle);
 }
 
+static void handle___pkvm_hyp_topup(struct kvm_cpu_context *host_ctxt)

Sashiko caught this. Suggest moving just this helper to patch 8 alongside its caller.

Cheers,
/fuad

Quoted 75 lines
arch/arm64/kvm/hyp/nvhe/hyp-main.c +24 -0
+{
+       DECLARE_REG(enum pkvm_topup_id, id, host_ctxt, 1);
+       DECLARE_REG(phys_addr_t, head, host_ctxt, 2);
+       DECLARE_REG(unsigned long, nr_pages, host_ctxt, 3);
+       struct kvm_hyp_memcache host_mc = {
+               .head = head,
+               .nr_pages = nr_pages,
+       };
+       int ret;
+
+       switch (id) {
+       case PKVM_TOPUP_HYP_ALLOC:
+               ret = hyp_alloc_topup(&host_mc);
+               break;
+       default:
+               ret = -EINVAL;
+       }
+
+       cpu_reg(host_ctxt, 1) = ret;
+       cpu_reg(host_ctxt, 2) = host_mc.head;
+       cpu_reg(host_ctxt, 3) = host_mc.nr_pages;
+}
+
 static void handle___tracing_load(struct kvm_cpu_context *host_ctxt)
 {
        DECLARE_REG(unsigned long, desc_hva, host_ctxt, 1);
@@ -762,6 +787,7 @@ static const hcall_t host_hcall[] = {
        HANDLE_FUNC(__pkvm_vcpu_load),
        HANDLE_FUNC(__pkvm_vcpu_put),
        HANDLE_FUNC(__pkvm_tlb_flush_vmid),
+       HANDLE_FUNC(__pkvm_hyp_topup),
 };
 
 static void handle_host_hcall(struct kvm_cpu_context *host_ctxt)
arch/arm64/kvm/pkvm.c +27 -1
index 8324a6a1bc48..e2c8714e8ccc 100644
@@ -85,6 +85,33 @@ void __init kvm_hyp_reserve(void)
                 hyp_mem_base);
 }
 
+static int pkvm_hyp_topup(enum pkvm_topup_id id, unsigned long nr_pages)
+{
+       struct kvm_hyp_memcache mc;
+       struct arm_smccc_res res;
+       int ret;
+
+       init_hyp_memcache(&mc);
+       ret = topup_hyp_memcache(&mc, nr_pages);
+       if (ret)
+               goto err;
+
+       arm_smccc_1_1_hvc(KVM_HOST_SMCCC_FUNC(__pkvm_hyp_topup), id, mc.head,
+                         mc.nr_pages, &res);
+       if (WARN_ON_ONCE(res.a0 != SMCCC_RET_SUCCESS)) {
+               ret = -EINVAL;
+               goto err;
+       }
+
+       ret = res.a1;
+       mc.head = res.a2;
+       mc.nr_pages = res.a3;
+
+err:
+       free_hyp_memcache(&mc);
+       return ret;
+}
+
 static void __pkvm_destroy_hyp_vm(struct kvm *kvm)
 {
        if (pkvm_hyp_vm_is_created(kvm)) {
--

2.55.0.rc2.803.g1fd1e6609c-goog

#20 lore raw

PATCH v2 7/18 KVM: arm64: Add pkvm_hyp_req infrastructure

In reply to Vincent Donnefort

Introduce a struct pkvm_hyp_req to enable the pKVM hypervisor to request resources from the host.

Provide serialisation helpers to transport these requests via SMCCC registers (starting from a2):

pkvm_hyp_req_to_smccc() to encode into the SMCCC args. smccc_to_pkvm_hyp_req() to decode them.

When the hypervisor raises a request, the host must handle it and retry the HVC. To automate this sequence, introduce the pkvm_call_hyp_req() macro. This intercepts pending requests, invokes the handler and retries the HVC.

Additionally, introduce a trace event to track the handling of these requests.

arch/arm64/include/asm/kvm_pkvm.h +89 -0
index 870f7870bb62..0be1469e7874 100644
@@ -203,6 +203,95 @@ struct pkvm_mapping {
 	u64 __subtree_last;	/* Internal member for interval tree */
 };
 
+enum pkvm_hyp_req_type {
+	PKVM_HYP_NO_REQ = 0,
+	__PKVM_HYP_REQ_TYPE_MAX,
+};
+
+#define PKVM_HYP_REQ_SMCCC_ARG_SIZE_MAX \
+	(sizeof(struct arm_smccc_res) - offsetof(struct arm_smccc_res, a2) - 1)
+
+struct pkvm_hyp_req {
+	u8 type;
+	union {
+		struct {
+			u32	nr_pages;
+		} mem;
+		struct {
+			/* Helper for SMCCC encoding/decoding */
+			u8	args[PKVM_HYP_REQ_SMCCC_ARG_SIZE_MAX];
+		} args;
+	};
+};
+
+static inline size_t pkvm_hyp_req_arg_size(u8 type)
+{
+	switch (type) {
+	case PKVM_HYP_NO_REQ:
+		return 0;
+	default:
+		WARN_ON(1);
+	}
+
+	return 0;
+}
+
+/* Encode the pending pkvm_hyp_req type into the SMCCC args */
+static inline void
+pkvm_hyp_req_to_smccc(struct kvm_cpu_context *host_ctxt, struct pkvm_hyp_req *req)
+{
+	u8 *dst, type = req->type;
+	size_t size;
+
+	if (type == PKVM_HYP_NO_REQ || type >= __PKVM_HYP_REQ_TYPE_MAX) {
+		host_ctxt->regs.regs[2] = 0;
+		return;
+	}
+
+	size = pkvm_hyp_req_arg_size(type);
+	if (WARN_ON(size > PKVM_HYP_REQ_SMCCC_ARG_SIZE_MAX))
+		return;
+
+	dst = (u8 *)&host_ctxt->regs.regs[2];
+	*dst = type;
+
+	memcpy(dst + 1, &req->args, size);
+}
+
+/* Return true if a pkvm_hyp_req has been decoded from the SMCCC args */
+static inline bool smccc_to_pkvm_hyp_req(struct pkvm_hyp_req *req, struct arm_smccc_res *res)
+{
+	u8 *src = (u8 *)res + offsetof(struct arm_smccc_res, a2);
+	u8 type = *src;
+
+	if (type == PKVM_HYP_NO_REQ || type >= __PKVM_HYP_REQ_TYPE_MAX)
+		return false;
+
+	req->type = type;
+	memcpy(&req->args, src + 1, pkvm_hyp_req_arg_size(type));
+
+	return true;
+}
+
+int __pkvm_handle_smccc_req(struct arm_smccc_res *res);
+
+#define pkvm_call_hyp_req(f, ...)								\
+({												\
+	struct arm_smccc_res __res;								\
+	int __ret;										\
+	do {											\
+		__ret = -1;									\
+		arm_smccc_1_1_hvc(KVM_HOST_SMCCC_FUNC(f), ##__VA_ARGS__, &__res);		\
+		if (WARN_ON(__res.a0 != SMCCC_RET_SUCCESS))					\
+			break;									\
+		__ret = __res.a1;								\
+		if (!__ret)									\
+			break;									\
+		__ret = __pkvm_handle_smccc_req(&__res);					\
+	} while (!__ret);									\
+	__ret;											\
+})
+
 int pkvm_pgtable_stage2_init(struct kvm_pgtable *pgt, struct kvm_s2_mmu *mmu,
 			     struct kvm_pgtable_mm_ops *mm_ops);
 void pkvm_pgtable_stage2_destroy_range(struct kvm_pgtable *pgt,
arch/arm64/kvm/pkvm.c +25 -0
index e2c8714e8ccc..5ba8b81b8c6c 100644
@@ -16,6 +16,9 @@
 
 #include "hyp_constants.h"
 
+#define CREATE_TRACE_POINTS
+#include "trace_pkvm.h"
+
 DEFINE_STATIC_KEY_FALSE(kvm_protected_mode_initialized);
 
 static struct memblock_region *hyp_memory = kvm_nvhe_sym(hyp_memory);
@@ -617,3 +620,25 @@ bool pkvm_force_reclaim_guest_page(phys_addr_t phys)
 
 	return !ret || ret == -EAGAIN;
 }
+
+static int pkvm_handle_hyp_req(struct pkvm_hyp_req *req)
+{
+	int ret = -EINVAL;
+
+	switch (req->type) {
+	}
+
+	trace_kvm_handle_pkvm_hyp_req(req, ret);
+
+	return ret;
+}
+
+int __pkvm_handle_smccc_req(struct arm_smccc_res *res)
+{
+	struct pkvm_hyp_req req;
+
+	if (smccc_to_pkvm_hyp_req(&req, res))
+		return pkvm_handle_hyp_req(&req);
+
+	return res->a1;
+}
arch/arm64/kvm/trace_pkvm.h new file +37 -0
new file mode 100644
index 000000000000..4bf57c12e7de
@@ -0,0 +1,37 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#if !defined(_TRACE_PKVM_ARM64_KVM_H) || defined(TRACE_HEADER_MULTI_READ)
+#define _TRACE_PKVM_ARM64_KVM_H
+
+#include <linux/tracepoint.h>
+#include <asm/kvm_pkvm.h>
+
+#undef TRACE_SYSTEM
+#define TRACE_SYSTEM kvm
+
+TRACE_EVENT(kvm_handle_pkvm_hyp_req,
+	TP_PROTO(struct pkvm_hyp_req *req, int ret),
+	TP_ARGS(req, ret),
+
+	TP_STRUCT__entry(
+		__field(u8,	type)
+		__field(int,	ret)
+	),
+
+	TP_fast_assign(
+		__entry->type = req->type;
+		__entry->ret = ret;
+	),
+
+	TP_printk("type: %u ret: %d",
+		  __entry->type, __entry->ret)
+);
+
+#endif /* _TRACE_PKVM_ARM64_KVM_H */
+
+#undef TRACE_INCLUDE_PATH
+#define TRACE_INCLUDE_PATH .
+#undef TRACE_INCLUDE_FILE
+#define TRACE_INCLUDE_FILE trace_pkvm
+
+/* This part must be outside protection */
+#include <trace/define_trace.h>
Signature · 1 line
2.55.0.rc2.803.g1fd1e6609c-goog
reviewer Reviewed-by on v2 7/18
#21 lore raw

Re: [PATCH v2 07/18] KVM: arm64: Add pkvm_hyp_req infrastructure

Quoted 15 lines · Vincent Donnefort

Introduce a struct pkvm_hyp_req to enable the pKVM hypervisor to request resources from the host.

Provide serialisation helpers to transport these requests via SMCCC registers (starting from a2):

pkvm_hyp_req_to_smccc() to encode into the SMCCC args. smccc_to_pkvm_hyp_req() to decode them.

When the hypervisor raises a request, the host must handle it and retry the HVC. To automate this sequence, introduce the pkvm_call_hyp_req() macro. This intercepts pending requests, invokes the handler and retries the HVC.

Quoted 4 lines

Additionally, introduce a trace event to track the handling of these requests.

Cheers,
/fuad

Quoted 187 lines
arch/arm64/include/asm/kvm_pkvm.h +89 -0
index 870f7870bb62..0be1469e7874 100644
@@ -203,6 +203,95 @@ struct pkvm_mapping {
        u64 __subtree_last;     /* Internal member for interval tree */
 };
 
+enum pkvm_hyp_req_type {
+       PKVM_HYP_NO_REQ = 0,
+       __PKVM_HYP_REQ_TYPE_MAX,
+};
+
+#define PKVM_HYP_REQ_SMCCC_ARG_SIZE_MAX \
+       (sizeof(struct arm_smccc_res) - offsetof(struct arm_smccc_res, a2) - 1)
+
+struct pkvm_hyp_req {
+       u8 type;
+       union {
+               struct {
+                       u32     nr_pages;
+               } mem;
+               struct {
+                       /* Helper for SMCCC encoding/decoding */
+                       u8      args[PKVM_HYP_REQ_SMCCC_ARG_SIZE_MAX];
+               } args;
+       };
+};
+
+static inline size_t pkvm_hyp_req_arg_size(u8 type)
+{
+       switch (type) {
+       case PKVM_HYP_NO_REQ:
+               return 0;
+       default:
+               WARN_ON(1);
+       }
+
+       return 0;
+}
+
+/* Encode the pending pkvm_hyp_req type into the SMCCC args */
+static inline void
+pkvm_hyp_req_to_smccc(struct kvm_cpu_context *host_ctxt, struct pkvm_hyp_req *req)
+{
+       u8 *dst, type = req->type;
+       size_t size;
+
+       if (type == PKVM_HYP_NO_REQ || type >= __PKVM_HYP_REQ_TYPE_MAX) {
+               host_ctxt->regs.regs[2] = 0;
+               return;
+       }
+
+       size = pkvm_hyp_req_arg_size(type);
+       if (WARN_ON(size > PKVM_HYP_REQ_SMCCC_ARG_SIZE_MAX))
+               return;
+
+       dst = (u8 *)&host_ctxt->regs.regs[2];
+       *dst = type;
+
+       memcpy(dst + 1, &req->args, size);
+}
+
+/* Return true if a pkvm_hyp_req has been decoded from the SMCCC args */
+static inline bool smccc_to_pkvm_hyp_req(struct pkvm_hyp_req *req, struct arm_smccc_res *res)
+{
+       u8 *src = (u8 *)res + offsetof(struct arm_smccc_res, a2);
+       u8 type = *src;
+
+       if (type == PKVM_HYP_NO_REQ || type >= __PKVM_HYP_REQ_TYPE_MAX)
+               return false;
+
+       req->type = type;
+       memcpy(&req->args, src + 1, pkvm_hyp_req_arg_size(type));
+
+       return true;
+}
+
+int __pkvm_handle_smccc_req(struct arm_smccc_res *res);
+
+#define pkvm_call_hyp_req(f, ...)                                                              \
+({                                                                                             \
+       struct arm_smccc_res __res;                                                             \
+       int __ret;                                                                              \
+       do {                                                                                    \
+               __ret = -1;                                                                     \
+               arm_smccc_1_1_hvc(KVM_HOST_SMCCC_FUNC(f), ##__VA_ARGS__, &__res);               \
+               if (WARN_ON(__res.a0 != SMCCC_RET_SUCCESS))                                     \
+                       break;                                                                  \
+               __ret = __res.a1;                                                               \
+               if (!__ret)                                                                     \
+                       break;                                                                  \
+               __ret = __pkvm_handle_smccc_req(&__res);                                        \
+       } while (!__ret);                                                                       \
+       __ret;                                                                                  \
+})
+
 int pkvm_pgtable_stage2_init(struct kvm_pgtable *pgt, struct kvm_s2_mmu *mmu,
                             struct kvm_pgtable_mm_ops *mm_ops);
 void pkvm_pgtable_stage2_destroy_range(struct kvm_pgtable *pgt,
arch/arm64/kvm/pkvm.c +25 -0
index e2c8714e8ccc..5ba8b81b8c6c 100644
@@ -16,6 +16,9 @@
 
 #include "hyp_constants.h"
 
+#define CREATE_TRACE_POINTS
+#include "trace_pkvm.h"
+
 DEFINE_STATIC_KEY_FALSE(kvm_protected_mode_initialized);
 
 static struct memblock_region *hyp_memory = kvm_nvhe_sym(hyp_memory);
@@ -617,3 +620,25 @@ bool pkvm_force_reclaim_guest_page(phys_addr_t phys)
 
        return !ret || ret == -EAGAIN;
 }
+
+static int pkvm_handle_hyp_req(struct pkvm_hyp_req *req)
+{
+       int ret = -EINVAL;
+
+       switch (req->type) {
+       }
+
+       trace_kvm_handle_pkvm_hyp_req(req, ret);
+
+       return ret;
+}
+
+int __pkvm_handle_smccc_req(struct arm_smccc_res *res)
+{
+       struct pkvm_hyp_req req;
+
+       if (smccc_to_pkvm_hyp_req(&req, res))
+               return pkvm_handle_hyp_req(&req);
+
+       return res->a1;
+}
arch/arm64/kvm/trace_pkvm.h new file +37 -1
new file mode 100644
index 000000000000..4bf57c12e7de
@@ -0,0 +1,37 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#if !defined(_TRACE_PKVM_ARM64_KVM_H) || defined(TRACE_HEADER_MULTI_READ)
+#define _TRACE_PKVM_ARM64_KVM_H
+
+#include <linux/tracepoint.h>
+#include <asm/kvm_pkvm.h>
+
+#undef TRACE_SYSTEM
+#define TRACE_SYSTEM kvm
+
+TRACE_EVENT(kvm_handle_pkvm_hyp_req,
+       TP_PROTO(struct pkvm_hyp_req *req, int ret),
+       TP_ARGS(req, ret),
+
+       TP_STRUCT__entry(
+               __field(u8,     type)
+               __field(int,    ret)
+       ),
+
+       TP_fast_assign(
+               __entry->type = req->type;
+               __entry->ret = ret;
+       ),
+
+       TP_printk("type: %u ret: %d",
+                 __entry->type, __entry->ret)
+);
+
+#endif /* _TRACE_PKVM_ARM64_KVM_H */
+
+#undef TRACE_INCLUDE_PATH
+#define TRACE_INCLUDE_PATH .
+#undef TRACE_INCLUDE_FILE
+#define TRACE_INCLUDE_FILE trace_pkvm
+
+/* This part must be outside protection */
+#include <trace/define_trace.h>
--

2.55.0.rc2.803.g1fd1e6609c-goog

#22 lore raw

PATCH v2 8/18 KVM: arm64: Handle PKVM_HYP_REQ_HYP_ALLOC request

In reply to Vincent Donnefort

Introduce a new pkvm_hyp_request type asking the host to top up the pKVM heap allocator.

arch/arm64/include/asm/kvm_pkvm.h +5 -0
index 0be1469e7874..99daa985856f 100644
@@ -205,6 +205,7 @@ struct pkvm_mapping {
 
 enum pkvm_hyp_req_type {
 	PKVM_HYP_NO_REQ = 0,
+	PKVM_HYP_REQ_HYP_ALLOC,
 	__PKVM_HYP_REQ_TYPE_MAX,
 };
 
@@ -226,9 +227,13 @@ struct pkvm_hyp_req {
 
 static inline size_t pkvm_hyp_req_arg_size(u8 type)
 {
+	struct pkvm_hyp_req *req;
+
 	switch (type) {
 	case PKVM_HYP_NO_REQ:
 		return 0;
+	case PKVM_HYP_REQ_HYP_ALLOC:
+		return sizeof(req->mem);
 	default:
 		WARN_ON(1);
 	}
arch/arm64/kvm/pkvm.c +3 -0
index 5ba8b81b8c6c..adc418076cb0 100644
@@ -626,6 +626,9 @@ static int pkvm_handle_hyp_req(struct pkvm_hyp_req *req)
 	int ret = -EINVAL;
 
 	switch (req->type) {
+	case PKVM_HYP_REQ_HYP_ALLOC:
+		ret = pkvm_hyp_topup(PKVM_TOPUP_HYP_ALLOC, req->mem.nr_pages);
+		break;
 	}
 
 	trace_kvm_handle_pkvm_hyp_req(req, ret);
Signature · 1 line
2.55.0.rc2.803.g1fd1e6609c-goog
reviewer
#23 lore raw

Re: [PATCH v2 08/18] KVM: arm64: Handle PKVM_HYP_REQ_HYP_ALLOC request

Quoted 11 lines · Vincent Donnefort

Introduce a new pkvm_hyp_request type asking the host to top up the pKVM heap allocator.

arch/arm64/include/asm/kvm_pkvm.h +0 -0
index 0be1469e7874..99daa985856f 100644

arch/arm64/include/asm/kvm_pkvm.h struct pkvm_mapping { · line 208

Quoted 4 lines
arch/arm64/include/asm/kvm_pkvm.h +1 -0
@@ -205,6 +205,7 @@ struct pkvm_mapping {
 
 enum pkvm_hyp_req_type {
        PKVM_HYP_NO_REQ = 0,
+       PKVM_HYP_REQ_HYP_ALLOC,

Now that the enum has a request type the tracepoint can actually fire for, would you be able to print it symbolically:
TRACE_DEFINE_ENUM(PKVM_HYP_REQ_HYP_ALLOC) plus __print_symbolic() in kvm_handle_pkvm_hyp_req's TP_printk, so traces read HYP_ALLOC rather than a raw number. I'm referring to trace_pkvm.h from the previous patch.

Cheers,
/fuad

Quoted 34 lines
arch/arm64/include/asm/kvm_pkvm.h +4 -0
        __PKVM_HYP_REQ_TYPE_MAX,
 };
 
@@ -226,9 +227,13 @@ struct pkvm_hyp_req {
 
 static inline size_t pkvm_hyp_req_arg_size(u8 type)
 {
+       struct pkvm_hyp_req *req;
+
        switch (type) {
        case PKVM_HYP_NO_REQ:
                return 0;
+       case PKVM_HYP_REQ_HYP_ALLOC:
+               return sizeof(req->mem);
        default:
                WARN_ON(1);
        }
arch/arm64/kvm/pkvm.c +3 -1
index 5ba8b81b8c6c..adc418076cb0 100644
@@ -626,6 +626,9 @@ static int pkvm_handle_hyp_req(struct pkvm_hyp_req *req)
        int ret = -EINVAL;
 
        switch (req->type) {
+       case PKVM_HYP_REQ_HYP_ALLOC:
+               ret = pkvm_hyp_topup(PKVM_TOPUP_HYP_ALLOC, req->mem.nr_pages);
+               break;
        }
 
        trace_kvm_handle_pkvm_hyp_req(req, ret);
--

2.55.0.rc2.803.g1fd1e6609c-goog

#24 lore raw

PATCH v2 9/18 KVM: arm64: Add reclaim interface for the pKVM heap alloc

In reply to Vincent Donnefort

Introduce a host interface to reclaim donated memory from the pKVM heap allocator back to the host.

It specifically provides two helpers that will make it easier to create a shrinker for pKVM:

pkvm_hyp_reclaimable()
pkvm_hyp_relaim()

arch/arm64/include/asm/kvm_asm.h +2 -0
index 681b7bf8ac08..b427ef790b15 100644
@@ -115,6 +115,8 @@ enum __kvm_host_smccc_func {
 	__KVM_HOST_SMCCC_FUNC___pkvm_vcpu_put,
 	__KVM_HOST_SMCCC_FUNC___pkvm_tlb_flush_vmid,
 	__KVM_HOST_SMCCC_FUNC___pkvm_hyp_topup,
+	__KVM_HOST_SMCCC_FUNC___pkvm_hyp_reclaim,
+	__KVM_HOST_SMCCC_FUNC___pkvm_hyp_reclaimable,
 
 	MARKER(__KVM_HOST_SMCCC_FUNC_MAX)
 };
arch/arm64/include/asm/kvm_pkvm.h +3 -0
index 99daa985856f..3a8590d1793e 100644
@@ -21,6 +21,9 @@ enum pkvm_topup_id {
 	PKVM_TOPUP_HYP_ALLOC,
 };
 
+unsigned long pkvm_hyp_reclaim(enum pkvm_topup_id id, unsigned long target);
+unsigned long pkvm_hyp_reclaimable(enum pkvm_topup_id id);
+
 int pkvm_init_host_vm(struct kvm *kvm, unsigned long type);
 int pkvm_create_hyp_vm(struct kvm *kvm);
 bool pkvm_hyp_vm_is_created(struct kvm *kvm);
arch/arm64/kvm/hyp/nvhe/hyp-main.c +38 -0
index e270a974fa37..8fbebe64a93d 100644
@@ -657,6 +657,42 @@ static void handle___pkvm_hyp_topup(struct kvm_cpu_context *host_ctxt)
 	cpu_reg(host_ctxt, 3) = host_mc.nr_pages;
 }
 
+static void handle___pkvm_hyp_reclaim(struct kvm_cpu_context *host_ctxt)
+{
+	DECLARE_REG(enum pkvm_topup_id, id, host_ctxt, 1);
+	DECLARE_REG(unsigned long, target, host_ctxt, 2);
+	struct kvm_hyp_memcache host_mc = {};
+	int ret = 0;
+
+	switch (id) {
+	case PKVM_TOPUP_HYP_ALLOC:
+		hyp_alloc_reclaim(&host_mc, target);
+		break;
+	default:
+		ret = -EINVAL;
+	}
+
+	cpu_reg(host_ctxt, 1) = ret;
+	cpu_reg(host_ctxt, 2) = host_mc.head;
+	cpu_reg(host_ctxt, 3) = host_mc.nr_pages;
+}
+
+static void handle___pkvm_hyp_reclaimable(struct kvm_cpu_context *host_ctxt)
+{
+	DECLARE_REG(enum pkvm_topup_id, id, host_ctxt, 1);
+	unsigned long reclaimable = 0;
+
+	switch (id) {
+	case PKVM_TOPUP_HYP_ALLOC:
+		reclaimable = hyp_alloc_reclaimable();
+		break;
+	default:
+		reclaimable = 0;
+	}
+
+	cpu_reg(host_ctxt, 1) = reclaimable;
+}
+
 static void handle___tracing_load(struct kvm_cpu_context *host_ctxt)
 {
 	DECLARE_REG(unsigned long, desc_hva, host_ctxt, 1);
@@ -788,6 +824,8 @@ static const hcall_t host_hcall[] = {
 	HANDLE_FUNC(__pkvm_vcpu_put),
 	HANDLE_FUNC(__pkvm_tlb_flush_vmid),
 	HANDLE_FUNC(__pkvm_hyp_topup),
+	HANDLE_FUNC(__pkvm_hyp_reclaim),
+	HANDLE_FUNC(__pkvm_hyp_reclaimable),
 };
 
 static void handle_host_hcall(struct kvm_cpu_context *host_ctxt)
arch/arm64/kvm/pkvm.c +22 -0
index adc418076cb0..eb4c60a0db5a 100644
@@ -115,6 +115,28 @@ static int pkvm_hyp_topup(enum pkvm_topup_id id, unsigned long nr_pages)
 	return ret;
 }
 
+unsigned long pkvm_hyp_reclaim(enum pkvm_topup_id id, unsigned long target)
+{
+	struct kvm_hyp_memcache mc;
+	struct arm_smccc_res res;
+	unsigned long reclaimed;
+
+	arm_smccc_1_1_hvc(KVM_HOST_SMCCC_FUNC(__pkvm_hyp_reclaim), id, target, &res);
+	if (WARN_ON_ONCE(res.a0 != SMCCC_RET_SUCCESS) || WARN_ON_ONCE(res.a1))
+		return 0;
+
+	init_hyp_memcache(&mc);
+	mc.head = res.a2;
+	mc.nr_pages = reclaimed = res.a3;
+	free_hyp_memcache(&mc);
+
+	return reclaimed;
+}
+
+unsigned long pkvm_hyp_reclaimable(enum pkvm_topup_id id)
+{
+	return kvm_call_hyp_nvhe(__pkvm_hyp_reclaimable, id);
+}
 static void __pkvm_destroy_hyp_vm(struct kvm *kvm)
 {
 	if (pkvm_hyp_vm_is_created(kvm)) {
Signature · 1 line
2.55.0.rc2.803.g1fd1e6609c-goog
reviewer Reviewed-by on v2 9/18
#25 lore raw

Re: [PATCH v2 09/18] KVM: arm64: Add reclaim interface for the pKVM heap alloc

On Mon, 6 Jul 2026 at 18:54, Vincent Donnefort <[email protected]> wrote:

...

arch/arm64/kvm/pkvm.c static int pkvm_hyp_topup(enum pkvm_topup_id id, unsigned long nr_pages) · line 139

Quoted 5 lines
arch/arm64/kvm/pkvm.c +5 -0
+
+unsigned long pkvm_hyp_reclaimable(enum pkvm_topup_id id)
+{
+       return kvm_call_hyp_nvhe(__pkvm_hyp_reclaimable, id);
+}

Missing new line.

With that fixed:

Cheers,
/fuad

Quoted 6 lines

static void __pkvm_destroy_hyp_vm(struct kvm *kvm)

 {
        if (pkvm_hyp_vm_is_created(kvm)) {

--
2.55.0.rc2.803.g1fd1e6609c-goog

#26 lore raw

PATCH v2 10/18 KVM: arm64: Add selftests for the pKVM heap allocator

In reply to Vincent Donnefort

Introduce a comprehensive runtime selftest for the pKVM hypervisor heap allocator, executed during init when CONFIG_NVHE_EL2_DEBUG is enabled.

The selftest runs entirely at EL2 and exercises allocator's core mechanisms:

* over-sized allocations
* basic allocation and alignment
* chunk recycling, splitting, merging
* memory reclaiming
* memory topup

arch/arm64/include/asm/kvm_asm.h +1 -0
index b427ef790b15..07a46860c8b2 100644
@@ -117,6 +117,7 @@ enum __kvm_host_smccc_func {
 	__KVM_HOST_SMCCC_FUNC___pkvm_hyp_topup,
 	__KVM_HOST_SMCCC_FUNC___pkvm_hyp_reclaim,
 	__KVM_HOST_SMCCC_FUNC___pkvm_hyp_reclaimable,
+	__KVM_HOST_SMCCC_FUNC___pkvm_hyp_alloc_selftest,
 
 	MARKER(__KVM_HOST_SMCCC_FUNC_MAX)
 };
arch/arm64/include/asm/kvm_pkvm.h +3 -0
index 3a8590d1793e..5837c2d7928a 100644
@@ -19,6 +19,7 @@
 
 enum pkvm_topup_id {
 	PKVM_TOPUP_HYP_ALLOC,
+	PKVM_TOPUP_HYP_ALLOC_SELFTEST,
 };
 
 unsigned long pkvm_hyp_reclaim(enum pkvm_topup_id id, unsigned long target);
@@ -209,6 +210,7 @@ struct pkvm_mapping {
 enum pkvm_hyp_req_type {
 	PKVM_HYP_NO_REQ = 0,
 	PKVM_HYP_REQ_HYP_ALLOC,
+	PKVM_HYP_REQ_HYP_ALLOC_SELFTEST,
 	__PKVM_HYP_REQ_TYPE_MAX,
 };
 
@@ -236,6 +238,7 @@ static inline size_t pkvm_hyp_req_arg_size(u8 type)
 	case PKVM_HYP_NO_REQ:
 		return 0;
 	case PKVM_HYP_REQ_HYP_ALLOC:
+	case PKVM_HYP_REQ_HYP_ALLOC_SELFTEST:
 		return sizeof(req->mem);
 	default:
 		WARN_ON(1);
arch/arm64/kvm/hyp/include/nvhe/alloc.h +7 -0
index 8f87a63f8946..329250dad6f6 100644
@@ -14,4 +14,11 @@ int hyp_alloc_init(size_t size);
 int hyp_alloc_topup(struct kvm_hyp_memcache *host_mc);
 unsigned long hyp_alloc_reclaimable(void);
 void hyp_alloc_reclaim(struct kvm_hyp_memcache *host_mc, unsigned long target);
+
+#ifdef CONFIG_NVHE_EL2_DEBUG
+int hyp_allocator_selftest(void);
+u32 hyp_alloc_selftest_topup_needed(void);
+int hyp_alloc_selftest_topup(struct kvm_hyp_memcache *host_mc);
+void hyp_alloc_selftest_reclaim(struct kvm_hyp_memcache *host_mc, unsigned long target);
+#endif
 #endif
arch/arm64/kvm/hyp/nvhe/alloc.c +185 -1

197 lines not on this page · raw message

arch/arm64/kvm/hyp/nvhe/hyp-main.c +33 -0
index 8fbebe64a93d..105313bb3dd9 100644
@@ -633,6 +633,28 @@ static void handle___pkvm_finalize_teardown_vm(struct kvm_cpu_context *host_ctxt
 	cpu_reg(host_ctxt, 1) = __pkvm_finalize_teardown_vm(handle);
 }
 
+#ifdef CONFIG_NVHE_EL2_DEBUG
+static void handle___pkvm_hyp_alloc_selftest(struct kvm_cpu_context *host_ctxt)
+{
+	struct pkvm_hyp_req req = { .type = PKVM_HYP_NO_REQ };
+	int ret;
+
+	ret = hyp_allocator_selftest();
+	if (ret == -ENOMEM) {
+		req.type = PKVM_HYP_REQ_HYP_ALLOC_SELFTEST;
+		req.mem.nr_pages = hyp_alloc_selftest_topup_needed();
+	}
+
+	cpu_reg(host_ctxt, 1) = ret;
+	pkvm_hyp_req_to_smccc(host_ctxt, &req);
+}
+#else
+static void handle___pkvm_hyp_alloc_selftest(struct kvm_cpu_context *host_ctxt)
+{
+	cpu_reg(host_ctxt, 1) = -EPERM;
+}
+#endif
+
 static void handle___pkvm_hyp_topup(struct kvm_cpu_context *host_ctxt)
 {
 	DECLARE_REG(enum pkvm_topup_id, id, host_ctxt, 1);
@@ -648,6 +670,11 @@ static void handle___pkvm_hyp_topup(struct kvm_cpu_context *host_ctxt)
 	case PKVM_TOPUP_HYP_ALLOC:
 		ret = hyp_alloc_topup(&host_mc);
 		break;
+#ifdef CONFIG_NVHE_EL2_DEBUG
+	case PKVM_TOPUP_HYP_ALLOC_SELFTEST:
+		ret = hyp_alloc_selftest_topup(&host_mc);
+		break;
+#endif
 	default:
 		ret = -EINVAL;
 	}
@@ -668,6 +695,11 @@ static void handle___pkvm_hyp_reclaim(struct kvm_cpu_context *host_ctxt)
 	case PKVM_TOPUP_HYP_ALLOC:
 		hyp_alloc_reclaim(&host_mc, target);
 		break;
+#ifdef CONFIG_NVHE_EL2_DEBUG
+	case PKVM_TOPUP_HYP_ALLOC_SELFTEST:
+		hyp_alloc_selftest_reclaim(&host_mc, target);
+		break;
+#endif
 	default:
 		ret = -EINVAL;
 	}
@@ -826,6 +858,7 @@ static const hcall_t host_hcall[] = {
 	HANDLE_FUNC(__pkvm_hyp_topup),
 	HANDLE_FUNC(__pkvm_hyp_reclaim),
 	HANDLE_FUNC(__pkvm_hyp_reclaimable),
+	HANDLE_FUNC(__pkvm_hyp_alloc_selftest),
 };
 
 static void handle_host_hcall(struct kvm_cpu_context *host_ctxt)
arch/arm64/kvm/pkvm.c +23 -0
index eb4c60a0db5a..f7e1846d6093 100644
@@ -137,6 +137,7 @@ unsigned long pkvm_hyp_reclaimable(enum pkvm_topup_id id)
 {
 	return kvm_call_hyp_nvhe(__pkvm_hyp_reclaimable, id);
 }
+
 static void __pkvm_destroy_hyp_vm(struct kvm *kvm)
 {
 	if (pkvm_hyp_vm_is_created(kvm)) {
@@ -325,6 +326,22 @@ static int __init pkvm_drop_host_privileges(void)
 	return ret;
 }
 
+static void __init pkvm_selftests(void)
+{
+#ifdef CONFIG_NVHE_EL2_DEBUG
+	int ret = pkvm_call_hyp_req(__pkvm_hyp_alloc_selftest);
+	unsigned long reclaimed;
+
+	reclaimed = pkvm_hyp_reclaim(PKVM_TOPUP_HYP_ALLOC_SELFTEST, ULONG_MAX);
+
+	/* On failure, not all the pages may be reclaimable */
+	if (!ret)
+		WARN_ON(reclaimed != 6 /* SELFTEST_MAX_PAGES */);
+	else
+		kvm_err("pKVM hyp allocator selftest failed (%d)\n", ret);
+#endif
+}
+
 static int __init finalize_pkvm(void)
 {
 	int ret;
@@ -345,6 +362,9 @@ static int __init finalize_pkvm(void)
 	if (ret)
 		pr_err("Failed to finalize Hyp protection: %d\n", ret);
 
+	if (!ret)
+		pkvm_selftests();
+
 	return ret;
 }
 device_initcall_sync(finalize_pkvm);
@@ -651,6 +671,9 @@ static int pkvm_handle_hyp_req(struct pkvm_hyp_req *req)
 	case PKVM_HYP_REQ_HYP_ALLOC:
 		ret = pkvm_hyp_topup(PKVM_TOPUP_HYP_ALLOC, req->mem.nr_pages);
 		break;
+	case PKVM_HYP_REQ_HYP_ALLOC_SELFTEST:
+		ret = pkvm_hyp_topup(PKVM_TOPUP_HYP_ALLOC_SELFTEST, req->mem.nr_pages);
+		break;
 	}
 
 	trace_kvm_handle_pkvm_hyp_req(req, ret);
Signature · 1 line
2.55.0.rc2.803.g1fd1e6609c-goog
reviewer Reviewed-by on v2 10/18
#27 lore raw

Re: [PATCH v2 10/18] KVM: arm64: Add selftests for the pKVM heap allocator

Quoted 7 lines · Vincent Donnefort

Introduce a comprehensive runtime selftest for the pKVM hypervisor heap allocator, executed during init when CONFIG_NVHE_EL2_DEBUG is enabled.

The selftest runs entirely at EL2 and exercises allocator's core mechanisms:

Quoted 7 lines

* over-sized allocations
* basic allocation and alignment
* chunk recycling, splitting, merging
* memory reclaiming
* memory topup

Cheers,
/fuad

Quoted 384 lines
arch/arm64/include/asm/kvm_asm.h +1 -0
index b427ef790b15..07a46860c8b2 100644
@@ -117,6 +117,7 @@ enum __kvm_host_smccc_func {
        __KVM_HOST_SMCCC_FUNC___pkvm_hyp_topup,
        __KVM_HOST_SMCCC_FUNC___pkvm_hyp_reclaim,
        __KVM_HOST_SMCCC_FUNC___pkvm_hyp_reclaimable,
+       __KVM_HOST_SMCCC_FUNC___pkvm_hyp_alloc_selftest,
 
        MARKER(__KVM_HOST_SMCCC_FUNC_MAX)
 };
arch/arm64/include/asm/kvm_pkvm.h +3 -0
index 3a8590d1793e..5837c2d7928a 100644
@@ -19,6 +19,7 @@
 
 enum pkvm_topup_id {
        PKVM_TOPUP_HYP_ALLOC,
+       PKVM_TOPUP_HYP_ALLOC_SELFTEST,
 };
 
 unsigned long pkvm_hyp_reclaim(enum pkvm_topup_id id, unsigned long target);
@@ -209,6 +210,7 @@ struct pkvm_mapping {
 enum pkvm_hyp_req_type {
        PKVM_HYP_NO_REQ = 0,
        PKVM_HYP_REQ_HYP_ALLOC,
+       PKVM_HYP_REQ_HYP_ALLOC_SELFTEST,
        __PKVM_HYP_REQ_TYPE_MAX,
 };
 
@@ -236,6 +238,7 @@ static inline size_t pkvm_hyp_req_arg_size(u8 type)
        case PKVM_HYP_NO_REQ:
                return 0;
        case PKVM_HYP_REQ_HYP_ALLOC:
+       case PKVM_HYP_REQ_HYP_ALLOC_SELFTEST:
                return sizeof(req->mem);
        default:
                WARN_ON(1);
arch/arm64/kvm/hyp/include/nvhe/alloc.h +7 -0
index 8f87a63f8946..329250dad6f6 100644
@@ -14,4 +14,11 @@ int hyp_alloc_init(size_t size);
 int hyp_alloc_topup(struct kvm_hyp_memcache *host_mc);
 unsigned long hyp_alloc_reclaimable(void);
 void hyp_alloc_reclaim(struct kvm_hyp_memcache *host_mc, unsigned long target);
+
+#ifdef CONFIG_NVHE_EL2_DEBUG
+int hyp_allocator_selftest(void);
+u32 hyp_alloc_selftest_topup_needed(void);
+int hyp_alloc_selftest_topup(struct kvm_hyp_memcache *host_mc);
+void hyp_alloc_selftest_reclaim(struct kvm_hyp_memcache *host_mc, unsigned long target);
+#endif
 #endif
arch/arm64/kvm/hyp/nvhe/alloc.c +185 -1
index 07ef0a13855f..e7114dde9142 100644
@@ -1007,9 +1007,17 @@ int hyp_alloc_errno(void)
        return hyp_allocator_errno(&hyp_allocator);
 }
 
+static int selftest_init(void);
+
 int hyp_alloc_init(size_t size)
 {
-       return hyp_allocator_init(&hyp_allocator, size);
+       int ret;
+
+       ret = hyp_allocator_init(&hyp_allocator, size);
+       if (ret)
+               return ret;
+
+       return selftest_init();
 }
 
 void hyp_alloc_reclaim(struct kvm_hyp_memcache *mc, unsigned long target)
@@ -1031,3 +1039,179 @@ u32 hyp_alloc_topup_needed(void)
 {
        return hyp_allocator_topup_needed(&hyp_allocator);
 }
+
+#ifdef CONFIG_NVHE_EL2_DEBUG
+#define SELFTEST_MAX_PAGES 6
+#define SELFTEST_MAX_SIZE (PAGE_SIZE * SELFTEST_MAX_PAGES)
+
+static DEFINE_PER_CPU(int, __selftest_errno);
+static DEFINE_PER_CPU(u32, __selftest_topup_needed);
+
+static struct hyp_allocator selftest_allocator = {
+       .errno = &__selftest_errno,
+       .topup_needed = &__selftest_topup_needed,
+       .lock = __HYP_SPIN_LOCK_UNLOCKED,
+};
+
+int hyp_alloc_selftest_topup(struct kvm_hyp_memcache *host_mc)
+{
+       return hyp_allocator_topup(&selftest_allocator, host_mc);
+}
+
+void hyp_alloc_selftest_reclaim(struct kvm_hyp_memcache *host_mc, unsigned long target)
+{
+       hyp_allocator_reclaim(&selftest_allocator, host_mc, target);
+}
+
+u32 hyp_alloc_selftest_topup_needed(void)
+{
+       return hyp_allocator_topup_needed(&selftest_allocator);
+}
+
+static int selftest_init(void)
+{
+       return hyp_allocator_init(&selftest_allocator, SELFTEST_MAX_SIZE);
+}
+
+static void *selftest_alloc(size_t size)
+{
+       return hyp_allocator_alloc(&selftest_allocator, size);
+}
+
+static void selftest_free(void *addr)
+{
+       hyp_allocator_free(&selftest_allocator, addr);
+}
+
+static int selftest_errno(void)
+{
+       return hyp_allocator_errno(&selftest_allocator);
+}
+
+int hyp_allocator_selftest(void)
+{
+       struct hyp_allocator *allocator = &selftest_allocator;
+       static DEFINE_HYP_SPINLOCK(selftest_lock);
+       struct kvm_hyp_memcache host_mc = { };
+       void *addr1, *addr2, *addr3, *addr4;
+       int ret;
+
+       guard(hyp_spinlock)(&selftest_lock);
+
+       if (allocator->mc.nr_pages < SELFTEST_MAX_PAGES) {
+               *this_cpu_ptr(allocator->topup_needed) = SELFTEST_MAX_PAGES -
+                                                        allocator->mc.nr_pages;
+               return -ENOMEM;
+       }
+
+       selftest_alloc(SELFTEST_MAX_SIZE);
+       if (selftest_errno() != -E2BIG)
+               return -EINVAL;
+
+       selftest_alloc(SIZE_MAX);
+       if (selftest_errno() != -E2BIG)
+               return -EINVAL;
+
+       /* Test first chunk */
+       addr1 = selftest_alloc(0);
+       if (!addr1 || addr1 != (void *)allocator->start + chunk_hdr_size())
+               return -EINVAL;
+
+       /* Test second contiguous chunk with unaligned size */
+       addr2 = selftest_alloc(MIN_ALLOC_SIZE + 1);
+       if (!addr2)
+               return -EINVAL;
+       addr3 = selftest_alloc(0);
+       if (!addr3 ||
+           addr3 != addr2 + (2 * MIN_ALLOC_SIZE) + chunk_hdr_size())
+               return -EINVAL;
+
+       selftest_free(addr3);
+
+       /* Test chunk recycling */
+       selftest_free(addr1);
+       if (addr1 != selftest_alloc(0))
+               return -EINVAL;
+
+       /* Test chunk forward merging */
+       addr3 = selftest_alloc(0);
+       selftest_free(addr2);
+       selftest_free(addr1);
+       if (addr1 != selftest_alloc(MIN_ALLOC_SIZE * 2))
+               return -EINVAL;
+
+       selftest_free(addr1);
+
+       /* Test chunk splitting */
+       if (addr1 != selftest_alloc(0))
+               return -EINVAL;
+       if (addr2 != selftest_alloc(0))
+               return -EINVAL;
+
+       /* Test chunk backward merging */
+       selftest_free(addr1);
+       selftest_free(addr2);
+       if (addr1 != selftest_alloc(MIN_ALLOC_SIZE * 2))
+               return -EINVAL;
+
+       selftest_free(addr1);
+
+       /* Test chunk 3-way merging */
+       addr1 = selftest_alloc(0);
+       addr2 = selftest_alloc(0);
+       addr4 = selftest_alloc(0);
+       selftest_free(addr1);
+       selftest_free(addr3);
+       selftest_free(addr2);
+       if (addr1 != selftest_alloc(MIN_ALLOC_SIZE * 3))
+               return -EINVAL;
+
+       selftest_free(addr4);
+       selftest_free(addr1);
+
+       /* Test reclaiming */
+       if (addr1 != selftest_alloc(0))
+               return -EINVAL;
+       if (addr2 != selftest_alloc(PAGE_SIZE * 2))
+               return -EINVAL;
+       addr3 = selftest_alloc(0);
+       addr4 = selftest_alloc(PAGE_SIZE);
+
+       /* Test reclaiming the last chunk of the list */
+       selftest_free(addr4);
+       hyp_allocator_reclaim(allocator, &host_mc, SELFTEST_MAX_PAGES);
+       if (host_mc.nr_pages != SELFTEST_MAX_PAGES - 3)
+               return -EINVAL;
+
+       /* Test punching a hole in the middle of a free chunk ... */
+       selftest_free(addr2);
+       hyp_allocator_reclaim(allocator, &host_mc, SELFTEST_MAX_PAGES);
+       if (host_mc.nr_pages != SELFTEST_MAX_PAGES - 2)
+               return -EINVAL;
+
+       if (selftest_alloc(PAGE_SIZE))
+               return -EINVAL;
+       if (selftest_errno() != -ENOMEM)
+               return -EINVAL;
+
+       /* ... and to refill this hole */
+       ret = hyp_allocator_topup(allocator, &host_mc);
+       if (ret)
+               return ret;
+       /* Chunk at addr2 was made smaller by the reclaim */
+       if (addr2 != selftest_alloc(PAGE_SIZE))
+               return -EINVAL;
+
+       /* Test reclaiming the entire allocator from the host */
+       selftest_free(addr3);
+       selftest_free(addr2);
+       selftest_free(addr1);
+       if (addr1 != selftest_alloc(SELFTEST_MAX_PAGES * PAGE_SIZE - chunk_hdr_size()))
+               return -EINVAL;
+       selftest_free(addr1);
+
+       return 0;
+}
+#else
+static int selftest_init(void) { return 0; }
+#endif
arch/arm64/kvm/hyp/nvhe/hyp-main.c +33 -0
index 8fbebe64a93d..105313bb3dd9 100644
@@ -633,6 +633,28 @@ static void handle___pkvm_finalize_teardown_vm(struct kvm_cpu_context *host_ctxt
        cpu_reg(host_ctxt, 1) = __pkvm_finalize_teardown_vm(handle);
 }
 
+#ifdef CONFIG_NVHE_EL2_DEBUG
+static void handle___pkvm_hyp_alloc_selftest(struct kvm_cpu_context *host_ctxt)
+{
+       struct pkvm_hyp_req req = { .type = PKVM_HYP_NO_REQ };
+       int ret;
+
+       ret = hyp_allocator_selftest();
+       if (ret == -ENOMEM) {
+               req.type = PKVM_HYP_REQ_HYP_ALLOC_SELFTEST;
+               req.mem.nr_pages = hyp_alloc_selftest_topup_needed();
+       }
+
+       cpu_reg(host_ctxt, 1) = ret;
+       pkvm_hyp_req_to_smccc(host_ctxt, &req);
+}
+#else
+static void handle___pkvm_hyp_alloc_selftest(struct kvm_cpu_context *host_ctxt)
+{
+       cpu_reg(host_ctxt, 1) = -EPERM;
+}
+#endif
+
 static void handle___pkvm_hyp_topup(struct kvm_cpu_context *host_ctxt)
 {
        DECLARE_REG(enum pkvm_topup_id, id, host_ctxt, 1);
@@ -648,6 +670,11 @@ static void handle___pkvm_hyp_topup(struct kvm_cpu_context *host_ctxt)
        case PKVM_TOPUP_HYP_ALLOC:
                ret = hyp_alloc_topup(&host_mc);
                break;
+#ifdef CONFIG_NVHE_EL2_DEBUG
+       case PKVM_TOPUP_HYP_ALLOC_SELFTEST:
+               ret = hyp_alloc_selftest_topup(&host_mc);
+               break;
+#endif
        default:
                ret = -EINVAL;
        }
@@ -668,6 +695,11 @@ static void handle___pkvm_hyp_reclaim(struct kvm_cpu_context *host_ctxt)
        case PKVM_TOPUP_HYP_ALLOC:
                hyp_alloc_reclaim(&host_mc, target);
                break;
+#ifdef CONFIG_NVHE_EL2_DEBUG
+       case PKVM_TOPUP_HYP_ALLOC_SELFTEST:
+               hyp_alloc_selftest_reclaim(&host_mc, target);
+               break;
+#endif
        default:
                ret = -EINVAL;
        }
@@ -826,6 +858,7 @@ static const hcall_t host_hcall[] = {
        HANDLE_FUNC(__pkvm_hyp_topup),
        HANDLE_FUNC(__pkvm_hyp_reclaim),
        HANDLE_FUNC(__pkvm_hyp_reclaimable),
+       HANDLE_FUNC(__pkvm_hyp_alloc_selftest),
 };
 
 static void handle_host_hcall(struct kvm_cpu_context *host_ctxt)
arch/arm64/kvm/pkvm.c +23 -1
index eb4c60a0db5a..f7e1846d6093 100644
@@ -137,6 +137,7 @@ unsigned long pkvm_hyp_reclaimable(enum pkvm_topup_id id)
 {
        return kvm_call_hyp_nvhe(__pkvm_hyp_reclaimable, id);
 }
+
 static void __pkvm_destroy_hyp_vm(struct kvm *kvm)
 {
        if (pkvm_hyp_vm_is_created(kvm)) {
@@ -325,6 +326,22 @@ static int __init pkvm_drop_host_privileges(void)
        return ret;
 }
 
+static void __init pkvm_selftests(void)
+{
+#ifdef CONFIG_NVHE_EL2_DEBUG
+       int ret = pkvm_call_hyp_req(__pkvm_hyp_alloc_selftest);
+       unsigned long reclaimed;
+
+       reclaimed = pkvm_hyp_reclaim(PKVM_TOPUP_HYP_ALLOC_SELFTEST, ULONG_MAX);
+
+       /* On failure, not all the pages may be reclaimable */
+       if (!ret)
+               WARN_ON(reclaimed != 6 /* SELFTEST_MAX_PAGES */);
+       else
+               kvm_err("pKVM hyp allocator selftest failed (%d)\n", ret);
+#endif
+}
+
 static int __init finalize_pkvm(void)
 {
        int ret;
@@ -345,6 +362,9 @@ static int __init finalize_pkvm(void)
        if (ret)
                pr_err("Failed to finalize Hyp protection: %d\n", ret);
 
+       if (!ret)
+               pkvm_selftests();
+
        return ret;
 }
 device_initcall_sync(finalize_pkvm);
@@ -651,6 +671,9 @@ static int pkvm_handle_hyp_req(struct pkvm_hyp_req *req)
        case PKVM_HYP_REQ_HYP_ALLOC:
                ret = pkvm_hyp_topup(PKVM_TOPUP_HYP_ALLOC, req->mem.nr_pages);
                break;
+       case PKVM_HYP_REQ_HYP_ALLOC_SELFTEST:
+               ret = pkvm_hyp_topup(PKVM_TOPUP_HYP_ALLOC_SELFTEST, req->mem.nr_pages);
+               break;
        }
 
        trace_kvm_handle_pkvm_hyp_req(req, ret);
--

2.55.0.rc2.803.g1fd1e6609c-goog

#28 lore raw

PATCH v2 11/18 KVM: arm64: Add a shrinker for pKVM

In reply to Vincent Donnefort

Integrate the pKVM memory reclaim interface with the host's memory management subsystem.

This allows the host to automatically recover unused memory fom the hypervisor's heap allocator when the host is under memory pressure.

arch/arm64/kvm/arm.c +25 -1
index 50adfff75be8..3dc2b9d2c322 100644
@@ -122,6 +122,8 @@ static bool vgic_present, kvm_arm_initialised;
 
 static DEFINE_PER_CPU(unsigned char, kvm_hyp_initialized);
 
+static struct shrinker *pkvm_shrinker;
+
 bool is_kvm_arm_initialised(void)
 {
 	return kvm_arm_initialised;
@@ -2526,8 +2528,8 @@ static void __init teardown_hyp_mode(void)
 		}
 
 		free_pages(kvm_nvhe_sym(kvm_arm_hyp_percpu_base)[cpu], nvhe_percpu_order());
-
 	}
+	shrinker_free(pkvm_shrinker);
 }
 
 static int __init do_pkvm_init(u32 hyp_va_bits)
@@ -2713,6 +2715,18 @@ static void pkvm_hyp_init_ptrauth(void)
 	}
 }
 
+static unsigned long
+pkvm_shrinker_count(struct shrinker *shrink, struct shrink_control *sc)
+{
+	return pkvm_hyp_reclaimable(PKVM_TOPUP_HYP_ALLOC) ?: SHRINK_EMPTY;
+}
+
+static unsigned long
+pkvm_shrinker_scan(struct shrinker *shrink, struct shrink_control *sc)
+{
+	return pkvm_hyp_reclaim(PKVM_TOPUP_HYP_ALLOC, sc->nr_to_scan);
+}
+
 /* Inits Hyp-mode on all online CPUs */
 static int __init init_hyp_mode(void)
 {
@@ -2883,6 +2897,16 @@ static int __init init_hyp_mode(void)
 			kvm_err("Failed to init hyp memory protection\n");
 			goto out_err;
 		}
+
+		pkvm_shrinker = shrinker_alloc(0, "pkvm");
+		if (pkvm_shrinker) {
+			pkvm_shrinker->count_objects = pkvm_shrinker_count;
+			pkvm_shrinker->scan_objects = pkvm_shrinker_scan;
+			shrinker_register(pkvm_shrinker);
+		} else {
+			kvm_err("Failed to register shrinker for pKVM\n");
+		}
+
 	}
 
 	return 0;
Signature · 1 line
2.55.0.rc2.803.g1fd1e6609c-goog
#29 lore raw

Re: [PATCH v2 11/18] KVM: arm64: Add a shrinker for pKVM

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] The pKVM memory shrinker is registered before pKVM is finalized, exposing 'pKVM-finalised-only'…

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] The pKVM memory shrinker is registered before pKVM is finalized, exposing 'pKVM-finalised-only' hypercalls to early execution which triggers a WARN_ON and potential boot panic. --

commit 32c5640182999493c354bd3285732dfbfa3fd23d
Author: Vincent Donnefort <[email protected]>

KVM: arm64: Add a shrinker for pKVM

Integrate the pKVM memory reclaim interface with the host's memory management subsystem. This allows the host to automatically recover unused memory fom the hypervisor's heap allocator when the host is under memory pressure.

Quoted 3 lines
arch/arm64/kvm/arm.c +0 -0

[ ... ]

Quoted 16 lines
arch/arm64/kvm/arm.c +12 -0
@@ -2713,6 +2715,18 @@ static void pkvm_hyp_init_ptrauth(void)
 	}
 }
 
+static unsigned long
+pkvm_shrinker_count(struct shrinker *shrink, struct shrink_control *sc)
+{
+	return pkvm_hyp_reclaimable(PKVM_TOPUP_HYP_ALLOC) ?: SHRINK_EMPTY;
+}
+
+static unsigned long
+pkvm_shrinker_scan(struct shrinker *shrink, struct shrink_control *sc)
+{
+	return pkvm_hyp_reclaim(PKVM_TOPUP_HYP_ALLOC, sc->nr_to_scan);
+}
+

[ ... ]

Quoted 7 lines
arch/arm64/kvm/arm.c +3 -0
@@ -2883,6 +2897,16 @@ static int __init init_hyp_mode(void)
 			kvm_err("Failed to init hyp memory protection\n");
 			goto out_err;
 		}
+
+		pkvm_shrinker = shrinker_alloc(0, "pkvm");
+		if (pkvm_shrinker) {

arch/arm64/kvm/arm.c static int __init init_hyp_mode(void) · line 2912

Quoted 10 lines
arch/arm64/kvm/arm.c +7 -0
+			pkvm_shrinker->count_objects = pkvm_shrinker_count;
+			pkvm_shrinker->scan_objects = pkvm_shrinker_scan;
+			shrinker_register(pkvm_shrinker);
+		} else {
+			kvm_err("Failed to register shrinker for pKVM\n");
+		}
+
 	}
 
 	return 0;

[Severity: High]
Could registering the shrinker here expose pKVM-finalised-only hypercalls to early execution?

Since init_hyp_mode() runs at device_initcall, the shrinker becomes globally visible before pKVM is finalized at device_initcall_sync in finalize_pkvm().

If memory pressure triggers the shrinker in this window, pkvm_shrinker_count() and pkvm_shrinker_scan() will invoke reclaim hypercalls. Because pKVM is not yet finalized, the EL2 handler will reject hypercalls beyond __KVM_HOST_SMCCC_FUNC_PKVM_ONLY with SMCCC_RET_NOT_SUPPORTED.

Will this failure cause the host-side kvm_call_hyp_nvhe macro to trigger a WARN_ON, potentially panicking the host if panic_on_warn is set?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=11
reviewer
#30 lore raw

Re: [PATCH v2 11/18] KVM: arm64: Add a shrinker for pKVM

In reply to Vincent Donnefort
Quoted 56 lines · Vincent Donnefort

Integrate the pKVM memory reclaim interface with the host's memory management subsystem.

This allows the host to automatically recover unused memory fom the hypervisor's heap allocator when the host is under memory pressure.

arch/arm64/kvm/arm.c +16 -1
index 50adfff75be8..3dc2b9d2c322 100644
@@ -122,6 +122,8 @@ static bool vgic_present, kvm_arm_initialised;
 
 static DEFINE_PER_CPU(unsigned char, kvm_hyp_initialized);
 
+static struct shrinker *pkvm_shrinker;
+
 bool is_kvm_arm_initialised(void)
 {
        return kvm_arm_initialised;
@@ -2526,8 +2528,8 @@ static void __init teardown_hyp_mode(void)
                }
 
                free_pages(kvm_nvhe_sym(kvm_arm_hyp_percpu_base)[cpu], nvhe_percpu_order());
-
        }
+       shrinker_free(pkvm_shrinker);
 }
 
 static int __init do_pkvm_init(u32 hyp_va_bits)
@@ -2713,6 +2715,18 @@ static void pkvm_hyp_init_ptrauth(void)
        }
 }
 
+static unsigned long
+pkvm_shrinker_count(struct shrinker *shrink, struct shrink_control *sc)
+{
+       return pkvm_hyp_reclaimable(PKVM_TOPUP_HYP_ALLOC) ?: SHRINK_EMPTY;
+}
+
+static unsigned long
+pkvm_shrinker_scan(struct shrinker *shrink, struct shrink_control *sc)
+{
+       return pkvm_hyp_reclaim(PKVM_TOPUP_HYP_ALLOC, sc->nr_to_scan);
+}
+
 /* Inits Hyp-mode on all online CPUs */
 static int __init init_hyp_mode(void)
 {
@@ -2883,6 +2897,16 @@ static int __init init_hyp_mode(void)
                        kvm_err("Failed to init hyp memory protection\n");
                        goto out_err;
                }
+

arch/arm64/kvm/arm.c static int __init init_hyp_mode(void) · line 2910

Quoted 10 lines
arch/arm64/kvm/arm.c +9 -0
+               pkvm_shrinker = shrinker_alloc(0, "pkvm");
+               if (pkvm_shrinker) {
+                       pkvm_shrinker->count_objects = pkvm_shrinker_count;
+                       pkvm_shrinker->scan_objects = pkvm_shrinker_scan;
+                       shrinker_register(pkvm_shrinker);
+               } else {
+                       kvm_err("Failed to register shrinker for pKVM\n");
+               }
+
        }

Sashiko might be onto something here. The shrinker goes live in init_hyp_mode(), but kvm_protected_mode_initialized isn't set until finalize_pkvm()...

Cheers,
/fuad

Quoted 5 lines
arch/arm64/kvm/arm.c +0 -1
        return 0;
--

2.55.0.rc2.803.g1fd1e6609c-goog

#31 lore raw

Re: [PATCH v2 11/18] KVM: arm64: Add a shrinker for pKVM

Quoted 63 lines · Fuad Tabba
Quoted 62 lines · Vincent Donnefort

Integrate the pKVM memory reclaim interface with the host's memory management subsystem.

This allows the host to automatically recover unused memory fom the hypervisor's heap allocator when the host is under memory pressure.

arch/arm64/kvm/arm.c +22 -1
index 50adfff75be8..3dc2b9d2c322 100644
@@ -122,6 +122,8 @@ static bool vgic_present, kvm_arm_initialised;
 
 static DEFINE_PER_CPU(unsigned char, kvm_hyp_initialized);
 
+static struct shrinker *pkvm_shrinker;
+
 bool is_kvm_arm_initialised(void)
 {
        return kvm_arm_initialised;
@@ -2526,8 +2528,8 @@ static void __init teardown_hyp_mode(void)
                }
 
                free_pages(kvm_nvhe_sym(kvm_arm_hyp_percpu_base)[cpu], nvhe_percpu_order());
-
        }
+       shrinker_free(pkvm_shrinker);
 }
 
 static int __init do_pkvm_init(u32 hyp_va_bits)
@@ -2713,6 +2715,18 @@ static void pkvm_hyp_init_ptrauth(void)
        }
 }
 
+static unsigned long
+pkvm_shrinker_count(struct shrinker *shrink, struct shrink_control *sc)
+{
+       return pkvm_hyp_reclaimable(PKVM_TOPUP_HYP_ALLOC) ?: SHRINK_EMPTY;
+}
+
+static unsigned long
+pkvm_shrinker_scan(struct shrinker *shrink, struct shrink_control *sc)
+{
+       return pkvm_hyp_reclaim(PKVM_TOPUP_HYP_ALLOC, sc->nr_to_scan);
+}
+
 /* Inits Hyp-mode on all online CPUs */
 static int __init init_hyp_mode(void)
 {
@@ -2883,6 +2897,16 @@ static int __init init_hyp_mode(void)
                        kvm_err("Failed to init hyp memory protection\n");
                        goto out_err;
                }
+
+               pkvm_shrinker = shrinker_alloc(0, "pkvm");
+               if (pkvm_shrinker) {
+                       pkvm_shrinker->count_objects = pkvm_shrinker_count;
+                       pkvm_shrinker->scan_objects = pkvm_shrinker_scan;
+                       shrinker_register(pkvm_shrinker);
+               } else {
Quoted 8 lines

arch/arm64/kvm/arm.c static int __init init_hyp_mode(void) · line 2910

Quoted 4 lines
arch/arm64/kvm/arm.c +3 -0
+                       kvm_err("Failed to register shrinker for pKVM\n");
+               }
+
        }

Sashiko might be onto something here. The shrinker goes live in init_hyp_mode(), but kvm_protected_mode_initialized isn't set until finalize_pkvm()...

I doubt the shrinker can be triggered that early... BUT

The hyp tracing is a user of this hyp allocator and I have been working on enabling early tracing [1] so I shall move those HVCs as well!

[1] https://lore.kernel.org/all/[email protected]/

Quoted 9 lines

Cheers,
/fuad

Quoted 5 lines
arch/arm64/kvm/arm.c +0 -1
        return 0;
--

2.55.0.rc2.803.g1fd1e6609c-goog

#32 lore raw

PATCH v2 12/18 KVM: arm64: Filter out non-kernel addresses in kern_hyp_va

In reply to Vincent Donnefort

kern_hyp_va() is idempotent for the hypervisor linear space. This is handy for nVHE hypervisor callers handling kvm_vcpu or kvm_arch pointers. Those pointers can originate from the hypervisor space (when protected mode is enabled, we don't trust the kernel and the hypervisor uses its own copy) or from the kernel space (we do trust the kernel in "non-protected" nVHE).

This idempotence does not hold for addresses within the hypervisor private range, like the ones you get from the pKVM heap allocator (hyp_alloc()). To resolve this, filter out non-kernel addresses based on PAGE_OFFSET.

Leave the assembly version untouched as it has no current users.

arch/arm64/include/asm/kvm_mmu.h +3 -0
index 6eae7e7e2a68..d60e5f2de10c 100644
@@ -126,6 +126,9 @@ static __always_inline unsigned long __kern_hyp_va(unsigned long v)
  * replace the instructions with `nop`s.
  */
 #ifndef __KVM_VHE_HYPERVISOR__
+	if (!is_ttbr1_addr(v))
+		return v;
+
 	asm volatile(ALTERNATIVE_CB("and %0, %0, #1\n"         /* mask with va_mask */
 				    "ror %0, %0, #1\n"         /* rotate to the first tag bit */
 				    "add %0, %0, #0\n"         /* insert the low 12 bits of the tag */
Signature · 1 line
2.55.0.rc2.803.g1fd1e6609c-goog
reviewer Reviewed-by on v2 12/18
#33 lore raw

Re: [PATCH v2 12/18] KVM: arm64: Filter out non-kernel addresses in kern_hyp_va

Quoted 8 lines · Vincent Donnefort

kern_hyp_va() is idempotent for the hypervisor linear space. This is handy for nVHE hypervisor callers handling kvm_vcpu or kvm_arch pointers. Those pointers can originate from the hypervisor space (when protected mode is enabled, we don't trust the kernel and the hypervisor uses its own copy) or from the kernel space (we do trust the kernel in "non-protected" nVHE).

Quoted 8 lines

This idempotence does not hold for addresses within the hypervisor private range, like the ones you get from the pKVM heap allocator (hyp_alloc()). To resolve this, filter out non-kernel addresses based on PAGE_OFFSET.

Leave the assembly version untouched as it has no current users.

Cheers,
/fuad

Quoted 18 lines
arch/arm64/include/asm/kvm_mmu.h +3 -1
index 6eae7e7e2a68..d60e5f2de10c 100644
@@ -126,6 +126,9 @@ static __always_inline unsigned long __kern_hyp_va(unsigned long v)
  * replace the instructions with `nop`s.
  */
 #ifndef __KVM_VHE_HYPERVISOR__
+       if (!is_ttbr1_addr(v))
+               return v;
+
        asm volatile(ALTERNATIVE_CB("and %0, %0, #1\n"         /* mask with va_mask */
                                    "ror %0, %0, #1\n"         /* rotate to the first tag bit */
                                    "add %0, %0, #0\n"         /* insert the low 12 bits of the tag */
--

2.55.0.rc2.803.g1fd1e6609c-goog

#34 lore raw

PATCH v2 13/18 KVM: arm64: Move hyp_vm refcount into the structure

In reply to Vincent Donnefort

In preparation for allocating hyp_vm using the pKVM heap allocator (hyp_alloc()), move its reference count out of the page metadata (vmemmap) and place it into the structure itself. This transition is necessary because hyp_alloc() allows multiple small objects to share the same physical page.

arch/arm64/kvm/hyp/include/nvhe/pkvm.h +13 -0
index c904647d2f76..624367d0ef5b 100644
@@ -41,6 +41,7 @@ struct pkvm_hyp_vm {
 	struct kvm_pgtable pgt;
 	struct kvm_pgtable_mm_ops mm_ops;
 	struct hyp_pool pool;
+	unsigned short refcount;
 	hyp_spinlock_t lock;
 
 	/* Array of the hyp vCPU structures for this VM. */
@@ -65,6 +66,18 @@ static inline bool pkvm_hyp_vm_is_protected(struct pkvm_hyp_vm *hyp_vm)
 	return kvm_vm_is_protected(&hyp_vm->kvm);
 }
 
+static inline void pkvm_hyp_vm_ref_inc(struct pkvm_hyp_vm *hyp_vm)
+{
+	BUG_ON(hyp_vm->refcount == USHRT_MAX);
+	hyp_vm->refcount++;
+}
+
+static inline void pkvm_hyp_vm_ref_dec(struct pkvm_hyp_vm *hyp_vm)
+{
+	BUG_ON(!hyp_vm->refcount);
+	hyp_vm->refcount--;
+}
+
 void pkvm_hyp_vm_table_init(void *tbl);
 
 int __pkvm_reserve_vm(void);
arch/arm64/kvm/hyp/nvhe/pkvm.c +5 -5
index 3b2c4fbc34d8..d9ab58226889 100644
@@ -278,7 +278,7 @@ struct pkvm_hyp_vcpu *pkvm_load_hyp_vcpu(pkvm_handle_t handle,
 	}
 
 	hyp_vcpu->loaded_hyp_vcpu = this_cpu_ptr(&loaded_hyp_vcpu);
-	hyp_page_ref_inc(hyp_virt_to_page(hyp_vm));
+	pkvm_hyp_vm_ref_inc(hyp_vm);
 unlock:
 	hyp_spin_unlock(&vm_table_lock);
 
@@ -294,7 +294,7 @@ void pkvm_put_hyp_vcpu(struct pkvm_hyp_vcpu *hyp_vcpu)
 	hyp_spin_lock(&vm_table_lock);
 	hyp_vcpu->loaded_hyp_vcpu = NULL;
 	__this_cpu_write(loaded_hyp_vcpu, NULL);
-	hyp_page_ref_dec(hyp_virt_to_page(hyp_vm));
+	pkvm_hyp_vm_ref_dec(hyp_vm);
 	hyp_spin_unlock(&vm_table_lock);
 }
 
@@ -311,7 +311,7 @@ struct pkvm_hyp_vm *get_pkvm_hyp_vm(pkvm_handle_t handle)
 	hyp_spin_lock(&vm_table_lock);
 	hyp_vm = get_vm_by_handle(handle);
 	if (hyp_vm)
-		hyp_page_ref_inc(hyp_virt_to_page(hyp_vm));
+		pkvm_hyp_vm_ref_inc(hyp_vm);
 	hyp_spin_unlock(&vm_table_lock);
 
 	return hyp_vm;
@@ -320,7 +320,7 @@ struct pkvm_hyp_vm *get_pkvm_hyp_vm(pkvm_handle_t handle)
 void put_pkvm_hyp_vm(struct pkvm_hyp_vm *hyp_vm)
 {
 	hyp_spin_lock(&vm_table_lock);
-	hyp_page_ref_dec(hyp_virt_to_page(hyp_vm));
+	pkvm_hyp_vm_ref_dec(hyp_vm);
 	hyp_spin_unlock(&vm_table_lock);
 }
 
@@ -966,7 +966,7 @@ static struct pkvm_hyp_vm *get_pkvm_unref_hyp_vm_locked(pkvm_handle_t handle)
 	hyp_assert_lock_held(&vm_table_lock);
 
 	hyp_vm = get_vm_by_handle(handle);
-	if (!hyp_vm || hyp_page_count(hyp_vm))
+	if (!hyp_vm || hyp_vm->refcount)
 		return NULL;
 
 	return hyp_vm;
Signature · 1 line
2.55.0.rc2.803.g1fd1e6609c-goog
reviewer Reviewed-by on v2 13/18
#35 lore raw

Re: [PATCH v2 13/18] KVM: arm64: Move hyp_vm refcount into the structure

Quoted 8 lines · Vincent Donnefort

In preparation for allocating hyp_vm using the pKVM heap allocator (hyp_alloc()), move its reference count out of the page metadata (vmemmap) and place it into the structure itself. This transition is necessary because hyp_alloc() allows multiple small objects to share the same physical page.

Cheers,
/fuad

Quoted 84 lines
arch/arm64/kvm/hyp/include/nvhe/pkvm.h +13 -0
index c904647d2f76..624367d0ef5b 100644
@@ -41,6 +41,7 @@ struct pkvm_hyp_vm {
        struct kvm_pgtable pgt;
        struct kvm_pgtable_mm_ops mm_ops;
        struct hyp_pool pool;
+       unsigned short refcount;
        hyp_spinlock_t lock;
 
        /* Array of the hyp vCPU structures for this VM. */
@@ -65,6 +66,18 @@ static inline bool pkvm_hyp_vm_is_protected(struct pkvm_hyp_vm *hyp_vm)
        return kvm_vm_is_protected(&hyp_vm->kvm);
 }
 
+static inline void pkvm_hyp_vm_ref_inc(struct pkvm_hyp_vm *hyp_vm)
+{
+       BUG_ON(hyp_vm->refcount == USHRT_MAX);
+       hyp_vm->refcount++;
+}
+
+static inline void pkvm_hyp_vm_ref_dec(struct pkvm_hyp_vm *hyp_vm)
+{
+       BUG_ON(!hyp_vm->refcount);
+       hyp_vm->refcount--;
+}
+
 void pkvm_hyp_vm_table_init(void *tbl);
 
 int __pkvm_reserve_vm(void);
arch/arm64/kvm/hyp/nvhe/pkvm.c +5 -6
index 3b2c4fbc34d8..d9ab58226889 100644
@@ -278,7 +278,7 @@ struct pkvm_hyp_vcpu *pkvm_load_hyp_vcpu(pkvm_handle_t handle,
        }
 
        hyp_vcpu->loaded_hyp_vcpu = this_cpu_ptr(&loaded_hyp_vcpu);
-       hyp_page_ref_inc(hyp_virt_to_page(hyp_vm));
+       pkvm_hyp_vm_ref_inc(hyp_vm);
 unlock:
        hyp_spin_unlock(&vm_table_lock);
 
@@ -294,7 +294,7 @@ void pkvm_put_hyp_vcpu(struct pkvm_hyp_vcpu *hyp_vcpu)
        hyp_spin_lock(&vm_table_lock);
        hyp_vcpu->loaded_hyp_vcpu = NULL;
        __this_cpu_write(loaded_hyp_vcpu, NULL);
-       hyp_page_ref_dec(hyp_virt_to_page(hyp_vm));
+       pkvm_hyp_vm_ref_dec(hyp_vm);
        hyp_spin_unlock(&vm_table_lock);
 }
 
@@ -311,7 +311,7 @@ struct pkvm_hyp_vm *get_pkvm_hyp_vm(pkvm_handle_t handle)
        hyp_spin_lock(&vm_table_lock);
        hyp_vm = get_vm_by_handle(handle);
        if (hyp_vm)
-               hyp_page_ref_inc(hyp_virt_to_page(hyp_vm));
+               pkvm_hyp_vm_ref_inc(hyp_vm);
        hyp_spin_unlock(&vm_table_lock);
 
        return hyp_vm;
@@ -320,7 +320,7 @@ struct pkvm_hyp_vm *get_pkvm_hyp_vm(pkvm_handle_t handle)
 void put_pkvm_hyp_vm(struct pkvm_hyp_vm *hyp_vm)
 {
        hyp_spin_lock(&vm_table_lock);
-       hyp_page_ref_dec(hyp_virt_to_page(hyp_vm));
+       pkvm_hyp_vm_ref_dec(hyp_vm);
        hyp_spin_unlock(&vm_table_lock);
 }
 
@@ -966,7 +966,7 @@ static struct pkvm_hyp_vm *get_pkvm_unref_hyp_vm_locked(pkvm_handle_t handle)
        hyp_assert_lock_held(&vm_table_lock);
 
        hyp_vm = get_vm_by_handle(handle);
-       if (!hyp_vm || hyp_page_count(hyp_vm))
+       if (!hyp_vm || hyp_vm->refcount)
                return NULL;
 
        return hyp_vm;
--

2.55.0.rc2.803.g1fd1e6609c-goog

#36 lore raw

PATCH v2 14/18 KVM: arm64: Use noclear for PGD in __pkvm_init_vm error path

In reply to Vincent Donnefort

In the error path of __pkvm_init_vm(), use unmap_donated_memory_noclear() instead of the clearing variant to release the donated stage-2 PGD back to the host.

This intends to eliminate the clearing variant of unmap_donated_memory(), as zeroing the PGD memory before returning it to the host is unnecessary in this failure path.

arch/arm64/kvm/hyp/nvhe/pkvm.c +1 -1
index d9ab58226889..0c82f1ddb93d 100644
@@ -861,7 +861,7 @@ int __pkvm_init_vm(struct kvm *host_kvm, unsigned long vm_hva,
 	kvm_guest_destroy_stage2(hyp_vm);
 err_remove_mappings:
 	unmap_donated_memory(hyp_vm, vm_size);
-	unmap_donated_memory(pgd, pgd_size);
+	unmap_donated_memory_noclear(pgd, pgd_size);
 err_unpin_kvm:
 	hyp_unpin_shared_mem(host_kvm, host_kvm + 1);
 	return ret;
Signature · 1 line
2.55.0.rc2.803.g1fd1e6609c-goog
#37 lore raw

Re: [PATCH v2 14/18] KVM: arm64: Use noclear for PGD in __pkvm_init_vm error path

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] Using `unmap_donated_memory_noclear()` for the PGD on the error path leaks EL2 virtual addresses…

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] Using unmap_donated_memory_noclear() for the PGD on the error path leaks EL2 virtual addresses to the untrusted host, bypassing hypervisor ASLR. --

commit d94f8fc664be6ec0ac22803e7fe54acff9cf8af1
Author: Vincent Donnefort <[email protected]>

KVM: arm64: Use noclear for PGD in __pkvm_init_vm error path

This commit modifies the error path of __pkvm_init_vm() to use unmap_donated_memory_noclear() instead of unmap_donated_memory() when returning the donated stage-2 PGD to the host.

arch/arm64/kvm/hyp/nvhe/pkvm.c int __pkvm_init_vm(struct kvm *host_kvm, unsigned long vm_hva, · line 864

Quoted 10 lines
arch/arm64/kvm/hyp/nvhe/pkvm.c +1 -1
index d9ab582268895..0c82f1ddb93df 100644
@@ -861,7 +861,7 @@ int __pkvm_init_vm(struct kvm *host_kvm, unsigned long vm_hva,
 	kvm_guest_destroy_stage2(hyp_vm);
 err_remove_mappings:
 	unmap_donated_memory(hyp_vm, vm_size);
-	unmap_donated_memory(pgd, pgd_size);
+	unmap_donated_memory_noclear(pgd, pgd_size);

[Severity: High]
Does using unmap_donated_memory_noclear() here leak EL2 virtual addresses to the untrusted host?

If the host deliberately provides an invalid handle before triggering this hypercall, insert_vm_table_entry() will fail and execution will take this error path.

Before reaching this error path, kvm_guest_prepare_stage2() initializes the donated PGD memory as a buddy pool. During this process, __hyp_attach_page() uses page_add_to_list() to write EL2 list_head pointers into the free pages.

By using the noclear variant here, the memory is returned to the host without zeroing those pointers. The host can then read this returned memory to extract valid EL2 virtual addresses, which breaks hypervisor KASLR.

Should this remain unmap_donated_memory() to ensure these internal pointers are properly scrubbed before the host regains access?

Quoted 3 lines
arch/arm64/kvm/hyp/nvhe/pkvm.c +0 -0
 err_unpin_kvm:
 	hyp_unpin_shared_mem(host_kvm, host_kvm + 1);
 	return ret;
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=14
reviewer
#38 lore raw

Re: [PATCH v2 14/18] KVM: arm64: Use noclear for PGD in __pkvm_init_vm error path

In reply to Vincent Donnefort
Quoted 16 lines · Vincent Donnefort

In the error path of __pkvm_init_vm(), use unmap_donated_memory_noclear() instead of the clearing variant to release the donated stage-2 PGD back to the host.

This intends to eliminate the clearing variant of unmap_donated_memory(), as zeroing the PGD memory before returning it to the host is unnecessary in this failure path.

arch/arm64/kvm/hyp/nvhe/pkvm.c +0 -0
index d9ab58226889..0c82f1ddb93d 100644

arch/arm64/kvm/hyp/nvhe/pkvm.c int __pkvm_init_vm(struct kvm *host_kvm, unsigned long vm_hva, · line 864

Quoted 5 lines
arch/arm64/kvm/hyp/nvhe/pkvm.c +1 -1
@@ -861,7 +861,7 @@ int __pkvm_init_vm(struct kvm *host_kvm, unsigned long vm_hva,
        kvm_guest_destroy_stage2(hyp_vm);
 err_remove_mappings:
        unmap_donated_memory(hyp_vm, vm_size);
-       unmap_donated_memory(pgd, pgd_size);
+       unmap_donated_memory_noclear(pgd, pgd_size);

We spoke about this in person before you sent the series and I thought it was ok. Sashiko's review made me doubt at first, since the freed blocks carry EL2 list_head pointers that the noclear variant hands back to the host.

You mentioned that the mapping isn't secret (later when we discussed Sashiko's review in person), and I checked: kvm_compute_layout() runs at EL1 on the host and rolls the tag itself. The host already knows the PA of every page it donated, so it can compute those VAs anyway. So I don't think this is a real leak.

However, this relies on the pool and hyp_vm both being donated linear-map memory here. Once hyp_vm moves to hyp_alloc() in the private range, the free_area anchor becomes a private-range VA, and I haven't checked whether that's host-derivable.

Given it's the init error path and not hot, clearing costs basically nothing, so I'd lean towards just keeping unmap_donated_memory() rather than reasoning about derivability.

What do you think?

/fuad

Quoted 6 lines
arch/arm64/kvm/hyp/nvhe/pkvm.c +0 -1
 err_unpin_kvm:
        hyp_unpin_shared_mem(host_kvm, host_kvm + 1);
        return ret;
--

2.55.0.rc2.803.g1fd1e6609c-goog

#39 lore raw

Re: [PATCH v2 14/18] KVM: arm64: Use noclear for PGD in __pkvm_init_vm error path

Quoted 40 lines · Fuad Tabba
Quoted 16 lines · Vincent Donnefort

In the error path of __pkvm_init_vm(), use unmap_donated_memory_noclear() instead of the clearing variant to release the donated stage-2 PGD back to the host.

This intends to eliminate the clearing variant of unmap_donated_memory(), as zeroing the PGD memory before returning it to the host is unnecessary in this failure path.

arch/arm64/kvm/hyp/nvhe/pkvm.c +0 -0
index d9ab58226889..0c82f1ddb93d 100644

arch/arm64/kvm/hyp/nvhe/pkvm.c int __pkvm_init_vm(struct kvm *host_kvm, unsigned long vm_hva, · line 864

Quoted 5 lines
arch/arm64/kvm/hyp/nvhe/pkvm.c +1 -1
@@ -861,7 +861,7 @@ int __pkvm_init_vm(struct kvm *host_kvm, unsigned long vm_hva,
        kvm_guest_destroy_stage2(hyp_vm);
 err_remove_mappings:
        unmap_donated_memory(hyp_vm, vm_size);
-       unmap_donated_memory(pgd, pgd_size);
+       unmap_donated_memory_noclear(pgd, pgd_size);

We spoke about this in person before you sent the series and I thought it was ok. Sashiko's review made me doubt at first, since the freed blocks carry EL2 list_head pointers that the noclear variant hands back to the host.

You mentioned that the mapping isn't secret (later when we discussed Sashiko's review in person), and I checked: kvm_compute_layout() runs at EL1 on the host and rolls the tag itself. The host already knows the PA of every page it donated, so it can compute those VAs anyway. So I don't think this is a real leak.

However, this relies on the pool and hyp_vm both being donated linear-map memory here. Once hyp_vm moves to hyp_alloc() in the private range, the free_area anchor becomes a private-range VA, and I haven't checked whether that's host-derivable.

Quoted 5 lines

Given it's the init error path and not hot, clearing costs basically nothing, so I'd lean towards just keeping unmap_donated_memory() rather than reasoning about derivability.

What do you think?

Happy to not take any risk and to drop it. It was just an attempt to simplify the code anyway.

Quoted 11 lines

/fuad

Quoted 6 lines
arch/arm64/kvm/hyp/nvhe/pkvm.c +0 -1
 err_unpin_kvm:
        hyp_unpin_shared_mem(host_kvm, host_kvm + 1);
        return ret;
--

2.55.0.rc2.803.g1fd1e6609c-goog

#40 lore raw

PATCH v2 15/18 KVM: arm64: Alloc pkvm_hyp_vm using pKVM heap allocator

In reply to Vincent Donnefort

Transition the allocation of the hypervisor VM state structure (pkvm_hyp_vm) from the host to the hypervisor using the new pKVM heap allocator (hyp_alloc()).

Previously, the host was responsible for calculating the size of, allocating, and donating memory for pkvm_hyp_vm during VM creation. With the heap allocator in place, the hypervisor now allocates this structure dynamically at EL2.

Use the pkvm_call_hyp_req() wrapper in the host to invoke __pkvm_init_vm, which automatically handles any top-up requests if the hypervisor runs out of heap memory during allocation.

arch/arm64/kvm/hyp/hyp-constants.c +0 -1
index b257a3b4bfc5..501ab35a3840 100644
@@ -7,7 +7,6 @@
 int main(void)
 {
 	DEFINE(STRUCT_HYP_PAGE_SIZE,	sizeof(struct hyp_page));
-	DEFINE(PKVM_HYP_VM_SIZE,	sizeof(struct pkvm_hyp_vm));
 	DEFINE(PKVM_HYP_VCPU_SIZE,	sizeof(struct pkvm_hyp_vcpu));
 	return 0;
 }
arch/arm64/kvm/hyp/include/nvhe/pkvm.h +1 -2
index 624367d0ef5b..8e930c8729af 100644
@@ -82,8 +82,7 @@ void pkvm_hyp_vm_table_init(void *tbl);
 
 int __pkvm_reserve_vm(void);
 void __pkvm_unreserve_vm(pkvm_handle_t handle);
-int __pkvm_init_vm(struct kvm *host_kvm, unsigned long vm_hva,
-		   unsigned long pgd_hva);
+int __pkvm_init_vm(struct kvm *host_kvm, void *pgd);
 int __pkvm_init_vcpu(pkvm_handle_t handle, struct kvm_vcpu *host_vcpu,
 		     unsigned long vcpu_hva);
 
arch/arm64/kvm/hyp/nvhe/hyp-main.c +19 -3
index 105313bb3dd9..16f963aedd70 100644
@@ -575,14 +575,30 @@ static void handle___pkvm_unreserve_vm(struct kvm_cpu_context *host_ctxt)
 	__pkvm_unreserve_vm(handle);
 }
 
+static void errno_to_smccc(int ret, struct kvm_cpu_context *host_ctxt)
+{
+	struct pkvm_hyp_req req = { .type = PKVM_HYP_NO_REQ };
+
+	switch (ret) {
+	case -ENOMEM:
+		req.type = PKVM_HYP_REQ_HYP_ALLOC;
+		req.mem.nr_pages = hyp_alloc_topup_needed();
+		break;
+	}
+
+	cpu_reg(host_ctxt, 1) = ret;
+	pkvm_hyp_req_to_smccc(host_ctxt, &req);
+}
+
 static void handle___pkvm_init_vm(struct kvm_cpu_context *host_ctxt)
 {
 	DECLARE_REG(struct kvm *, host_kvm, host_ctxt, 1);
-	DECLARE_REG(unsigned long, vm_hva, host_ctxt, 2);
-	DECLARE_REG(unsigned long, pgd_hva, host_ctxt, 3);
+	DECLARE_REG(unsigned long, pgd_hva, host_ctxt, 2);
+	void *pgd;
 
 	host_kvm = kern_hyp_va(host_kvm);
-	cpu_reg(host_ctxt, 1) = __pkvm_init_vm(host_kvm, vm_hva, pgd_hva);
+	pgd = (void *)kern_hyp_va(pgd_hva);
+	errno_to_smccc(__pkvm_init_vm(host_kvm, pgd), host_ctxt);
 }
 
 static void handle___pkvm_init_vcpu(struct kvm_cpu_context *host_ctxt)
arch/arm64/kvm/hyp/nvhe/pkvm.c +18 -20
index 0c82f1ddb93d..2f616a89f8b3 100644
@@ -11,6 +11,7 @@
 
 #include <asm/kvm_emulate.h>
 
+#include <nvhe/alloc.h>
 #include <nvhe/mem_protect.h>
 #include <nvhe/memory.h>
 #include <nvhe/pkvm.h>
@@ -797,24 +798,22 @@ void teardown_selftest_vm(void)
  * Unmap the donated memory from the host at stage 2.
  *
  * host_kvm: A pointer to the host's struct kvm.
- * vm_hva: The host va of the area being donated for the VM state.
- *	   Must be page aligned.
- * pgd_hva: The host va of the area being donated for the stage-2 PGD for
- *	    the VM. Must be page aligned. Its size is implied by the VM's
- *	    VTCR.
+ * pgd: The va of the area being donated for the stage-2 PGD for the VM. Must
+ *      be page aligned. Its size is implied by the VM's VTCR.
  *
  * Return 0 success, negative error code on failure.
  */
-int __pkvm_init_vm(struct kvm *host_kvm, unsigned long vm_hva,
-		   unsigned long pgd_hva)
+int __pkvm_init_vm(struct kvm *host_kvm, void *pgd)
 {
 	struct pkvm_hyp_vm *hyp_vm = NULL;
 	size_t vm_size, pgd_size;
 	unsigned int nr_vcpus;
 	pkvm_handle_t handle;
-	void *pgd = NULL;
 	int ret;
 
+	if (!PAGE_ALIGNED(pgd))
+		return -EINVAL;
+
 	ret = hyp_pin_shared_mem(host_kvm, host_kvm + 1);
 	if (ret)
 		return ret;
@@ -834,15 +833,15 @@ int __pkvm_init_vm(struct kvm *host_kvm, unsigned long vm_hva,
 	vm_size = pkvm_get_hyp_vm_size(nr_vcpus);
 	pgd_size = kvm_pgtable_stage2_pgd_size(host_mmu.arch.mmu.vtcr);
 
-	ret = -ENOMEM;
+	hyp_vm = hyp_alloc(vm_size);
+	if (!hyp_vm) {
+		ret = hyp_alloc_errno();
+		goto err_unpin_kvm;
+	}
 
-	hyp_vm = map_donated_memory(vm_hva, vm_size);
-	if (!hyp_vm)
-		goto err_remove_mappings;
-
-	pgd = map_donated_memory_noclear(pgd_hva, pgd_size);
-	if (!pgd)
-		goto err_remove_mappings;
+	ret = __pkvm_host_donate_hyp(hyp_virt_to_pfn(pgd), PAGE_ALIGN(pgd_size) >> PAGE_SHIFT);
+	if (ret)
+		goto err_free_hyp_vm;
 
 	init_pkvm_hyp_vm(host_kvm, hyp_vm, nr_vcpus, handle);
 
@@ -860,8 +859,9 @@ int __pkvm_init_vm(struct kvm *host_kvm, unsigned long vm_hva,
 err_destroy_stage2:
 	kvm_guest_destroy_stage2(hyp_vm);
 err_remove_mappings:
-	unmap_donated_memory(hyp_vm, vm_size);
 	unmap_donated_memory_noclear(pgd, pgd_size);
+err_free_hyp_vm:
+	hyp_free(hyp_vm);
 err_unpin_kvm:
 	hyp_unpin_shared_mem(host_kvm, host_kvm + 1);
 	return ret;
@@ -997,7 +997,6 @@ int __pkvm_finalize_teardown_vm(pkvm_handle_t handle)
 	struct pkvm_hyp_vm *hyp_vm;
 	struct kvm *host_kvm;
 	unsigned int idx;
-	size_t vm_size;
 	int err;
 
 	hyp_spin_lock(&vm_table_lock);
@@ -1040,8 +1039,7 @@ int __pkvm_finalize_teardown_vm(pkvm_handle_t handle)
 		teardown_donated_memory(mc, hyp_vcpu, sizeof(*hyp_vcpu));
 	}
 
-	vm_size = pkvm_get_hyp_vm_size(hyp_vm->kvm.created_vcpus);
-	teardown_donated_memory(mc, hyp_vm, vm_size);
+	hyp_free(hyp_vm);
 	hyp_unpin_shared_mem(host_kvm, host_kvm + 1);
 	return 0;
 
arch/arm64/kvm/pkvm.c +4 -17
index f7e1846d6093..8a8b5b84825c 100644
@@ -191,8 +191,8 @@ static int __pkvm_create_hyp_vcpu(struct kvm_vcpu *vcpu)
  */
 static int __pkvm_create_hyp_vm(struct kvm *kvm)
 {
-	size_t pgd_sz, hyp_vm_sz;
-	void *pgd, *hyp_vm;
+	size_t pgd_sz;
+	void *pgd;
 	int ret;
 
 	if (kvm->created_vcpus < 1)
@@ -209,28 +209,15 @@ static int __pkvm_create_hyp_vm(struct kvm *kvm)
 	if (!pgd)
 		return -ENOMEM;
 
-	/* Allocate memory to donate to hyp for vm and vcpu pointers. */
-	hyp_vm_sz = PAGE_ALIGN(size_add(PKVM_HYP_VM_SIZE,
-					size_mul(sizeof(void *),
-						 kvm->created_vcpus)));
-	hyp_vm = alloc_pages_exact(hyp_vm_sz, GFP_KERNEL_ACCOUNT);
-	if (!hyp_vm) {
-		ret = -ENOMEM;
-		goto free_pgd;
-	}
-
-	/* Donate the VM memory to hyp and let hyp initialize it. */
-	ret = kvm_call_hyp_nvhe(__pkvm_init_vm, kvm, hyp_vm, pgd);
+	ret = pkvm_call_hyp_req(__pkvm_init_vm, kvm, pgd);
 	if (ret)
-		goto free_vm;
+		goto free_pgd;
 
 	kvm->arch.pkvm.is_created = true;
 	init_hyp_stage2_memcache(&kvm->arch.pkvm.stage2_teardown_mc);
 	kvm_account_pgtable_pages(pgd, pgd_sz / PAGE_SIZE);
 
 	return 0;
-free_vm:
-	free_pages_exact(hyp_vm, hyp_vm_sz);
 free_pgd:
 	free_pages_exact(pgd, pgd_sz);
 	return ret;
Signature · 1 line
2.55.0.rc2.803.g1fd1e6609c-goog
#41 lore raw

Re: [PATCH v2 15/18] KVM: arm64: Alloc pkvm_hyp_vm using pKVM heap allocator

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] Memcg accounting bypass in EL2 heap top-up allows host memory exhaustion --

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] Memcg accounting bypass in EL2 heap top-up allows host memory exhaustion --

commit 66032df021feb40dc76572f7b2d6e0341f8b7e65
Author: Vincent Donnefort <[email protected]>

KVM: arm64: Alloc pkvm_hyp_vm using pKVM heap allocator

Transition the allocation of the hypervisor VM state structure (pkvm_hyp_vm) from the host to the hypervisor using the new pKVM heap allocator (hyp_alloc()).

Quoted 4 lines
arch/arm64/kvm/pkvm.c +0 -0
index f7e1846d6093f..8a8b5b84825c7 100644

[ ... ]

Quoted 7 lines
arch/arm64/kvm/pkvm.c +0 -3
@@ -209,28 +209,15 @@ static int __pkvm_create_hyp_vm(struct kvm *kvm)
 	if (!pgd)
 		return -ENOMEM;
 
-	/* Allocate memory to donate to hyp for vm and vcpu pointers. */
-	hyp_vm_sz = PAGE_ALIGN(size_add(PKVM_HYP_VM_SIZE,
-					size_mul(sizeof(void *),

arch/arm64/kvm/pkvm.c static int __pkvm_create_hyp_vm(struct kvm *kvm) · line 212

Quoted 10 lines
arch/arm64/kvm/pkvm.c +1 -9
-						 kvm->created_vcpus)));
-	hyp_vm = alloc_pages_exact(hyp_vm_sz, GFP_KERNEL_ACCOUNT);
-	if (!hyp_vm) {
-		ret = -ENOMEM;
-		goto free_pgd;
-	}
-
-	/* Donate the VM memory to hyp and let hyp initialize it. */
-	ret = kvm_call_hyp_nvhe(__pkvm_init_vm, kvm, hyp_vm, pgd);
+	ret = pkvm_call_hyp_req(__pkvm_init_vm, kvm, pgd);

[Severity: High]
Does this change bypass memory cgroup accounting for the VM allocation?

Previously, the host allocated hyp_vm using GFP_KERNEL_ACCOUNT. With this patch, the allocation is deferred to EL2, and if the EL2 heap is depleted, it triggers a top-up request handled by pkvm_call_hyp_req() and pkvm_hyp_topup() on the host.

Looking at how pkvm_hyp_topup() initializes the memcache:

init_hyp_memcache(&mc);
ret = topup_hyp_memcache(&mc, nr_pages);

The init_hyp_memcache() helper zeros the memcache flags, meaning it omits the HYP_MEMCACHE_ACCOUNT_KMEMCG flag. As a result, the subsequent host page allocations in hyp_mc_alloc_fn() will use a plain GFP_KERNEL without __GFP_ACCOUNT.

Could an unprivileged container repeatedly trigger KVM_CREATE_VM to exhaust host memory without being restricted by its memory cgroup limits?

Quoted 6 lines
arch/arm64/kvm/pkvm.c +1 -1
 	if (ret)
-		goto free_vm;
+		goto free_pgd;
 
 	kvm->arch.pkvm.is_created = true;
 	init_hyp_stage2_memcache(&kvm->arch.pkvm.stage2_teardown_mc);
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=15
reviewer
#42 lore raw

Re: [PATCH v2 15/18] KVM: arm64: Alloc pkvm_hyp_vm using pKVM heap allocator

In reply to Vincent Donnefort

On Mon, 6 Jul 2026 at 18:54, 'Vincent Donnefort' via kernel-team <[email protected]> wrote:

...

Quoted 20 lines
arch/arm64/kvm/pkvm.c +2 -5
@@ -191,8 +191,8 @@ static int __pkvm_create_hyp_vcpu(struct kvm_vcpu *vcpu)
  */
 static int __pkvm_create_hyp_vm(struct kvm *kvm)
 {
-       size_t pgd_sz, hyp_vm_sz;
-       void *pgd, *hyp_vm;
+       size_t pgd_sz;
+       void *pgd;
        int ret;
 
        if (kvm->created_vcpus < 1)
@@ -209,28 +209,15 @@ static int __pkvm_create_hyp_vm(struct kvm *kvm)
        if (!pgd)
                return -ENOMEM;
 
-       /* Allocate memory to donate to hyp for vm and vcpu pointers. */
-       hyp_vm_sz = PAGE_ALIGN(size_add(PKVM_HYP_VM_SIZE,
-                                       size_mul(sizeof(void *),

arch/arm64/kvm/pkvm.c static int __pkvm_create_hyp_vm(struct kvm *kvm) · line 212

Quoted 10 lines
arch/arm64/kvm/pkvm.c +1 -9
-                                                kvm->created_vcpus)));
-       hyp_vm = alloc_pages_exact(hyp_vm_sz, GFP_KERNEL_ACCOUNT);
-       if (!hyp_vm) {
-               ret = -ENOMEM;
-               goto free_pgd;
-       }
-
-       /* Donate the VM memory to hyp and let hyp initialize it. */
-       ret = kvm_call_hyp_nvhe(__pkvm_init_vm, kvm, hyp_vm, pgd);
+       ret = pkvm_call_hyp_req(__pkvm_init_vm, kvm, pgd);

Sashiko might be right on the memcg point: hyp_vm used to be GFP_KERNEL_ACCOUNT and now comes from the heap unaccounted, while pgd next to it is still accounted, so it's inconsistent.

If we do want to fix it, would it go in pkvm_hyp_topup() to cover all consumers rather than here?

Cheers,
/fuad

#43 lore raw

Re: [PATCH v2 15/18] KVM: arm64: Alloc pkvm_hyp_vm using pKVM heap allocator

Quoted 31 lines · Fuad Tabba

On Mon, 6 Jul 2026 at 18:54, 'Vincent Donnefort' via kernel-team <[email protected]> wrote:

...

Quoted 26 lines
arch/arm64/kvm/pkvm.c +2 -11
@@ -191,8 +191,8 @@ static int __pkvm_create_hyp_vcpu(struct kvm_vcpu *vcpu)
  */
 static int __pkvm_create_hyp_vm(struct kvm *kvm)
 {
-       size_t pgd_sz, hyp_vm_sz;
-       void *pgd, *hyp_vm;
+       size_t pgd_sz;
+       void *pgd;
        int ret;
 
        if (kvm->created_vcpus < 1)
@@ -209,28 +209,15 @@ static int __pkvm_create_hyp_vm(struct kvm *kvm)
        if (!pgd)
                return -ENOMEM;
 
-       /* Allocate memory to donate to hyp for vm and vcpu pointers. */
-       hyp_vm_sz = PAGE_ALIGN(size_add(PKVM_HYP_VM_SIZE,
-                                       size_mul(sizeof(void *),
-                                                kvm->created_vcpus)));
-       hyp_vm = alloc_pages_exact(hyp_vm_sz, GFP_KERNEL_ACCOUNT);
-       if (!hyp_vm) {
-               ret = -ENOMEM;
-               goto free_pgd;
-       }
Quoted 8 lines

arch/arm64/kvm/pkvm.c static int __pkvm_create_hyp_vm(struct kvm *kvm) · line 212

Quoted 4 lines
arch/arm64/kvm/pkvm.c +1 -3
-
-       /* Donate the VM memory to hyp and let hyp initialize it. */
-       ret = kvm_call_hyp_nvhe(__pkvm_init_vm, kvm, hyp_vm, pgd);
+       ret = pkvm_call_hyp_req(__pkvm_init_vm, kvm, pgd);

Sashiko might be right on the memcg point: hyp_vm used to be GFP_KERNEL_ACCOUNT and now comes from the heap unaccounted, while pgd next to it is still accounted, so it's inconsistent.

Yep that is correct but there is no way around that:

* Donated memory can now be shared be shared by several VM (or other parts of the hypervisor)

* Donated memory is also not reclaimed until the shrinker fires.

So we don't really have a way to account for that meta-data anymore.

(I should at least mention this in the commit message)

Quoted 6 lines

If we do want to fix it, would it go in pkvm_hyp_topup() to cover all consumers rather than here?

Cheers,
/fuad

reviewer
#44 lore raw

Re: [PATCH v2 15/18] KVM: arm64: Alloc pkvm_hyp_vm using pKVM heap allocator

Quoted 6 lines
Quoted 3 lines

Sashiko might be right on the memcg point: hyp_vm used to be GFP_KERNEL_ACCOUNT and now comes from the heap unaccounted, while pgd next to it is still accounted, so it's inconsistent.

Yep that is correct but there is no way around that:

Quoted 8 lines

* Donated memory can now be shared be shared by several VM (or other parts of the hypervisor)

* Donated memory is also not reclaimed until the shrinker fires.

So we don't really have a way to account for that meta-data anymore.

(I should at least mention this in the commit message)

Sgtm.
/fuad

Quoted 7 lines
Quoted 7 lines

If we do want to fix it, would it go in pkvm_hyp_topup() to cover all consumers rather than here?

Cheers,
/fuad

#45 lore raw

PATCH v2 16/18 KVM: arm64: Alloc pkvm_hyp_vcpu using pKVM heap allocator

In reply to Vincent Donnefort

Transition the allocation of the hypervisor vCPU state structure (pkvm_hyp_vcpu) from the host to the hypervisor using the new pKVM heap allocator (hyp_alloc()).

Previously, the host was responsible for calculating the size of, allocating, and donating memory for pkvm_hyp_vcpu during VM creation. With the heap allocator in place, the hypervisor now allocates this structure dynamically at EL2.

Use the pkvm_call_hyp_req() wrapper in the host to invoke __pkvm_create_hyp_vcpu, which automatically handles any top-up requests if the hypervisor runs out of heap memory during allocation.

arch/arm64/include/asm/kvm_host.h +0 -1
index caea8e9986d1..95e05cee0f3a 100644
@@ -268,7 +268,6 @@ typedef u16 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 is_protected;
 	bool is_created;
arch/arm64/kvm/hyp/hyp-constants.c +0 -1
index 501ab35a3840..b2caae21f271 100644
@@ -7,6 +7,5 @@
 int main(void)
 {
 	DEFINE(STRUCT_HYP_PAGE_SIZE,	sizeof(struct hyp_page));
-	DEFINE(PKVM_HYP_VCPU_SIZE,	sizeof(struct pkvm_hyp_vcpu));
 	return 0;
 }
arch/arm64/kvm/hyp/include/nvhe/pkvm.h +1 -2
index 8e930c8729af..cfb6e409bf49 100644
@@ -83,8 +83,7 @@ void pkvm_hyp_vm_table_init(void *tbl);
 int __pkvm_reserve_vm(void);
 void __pkvm_unreserve_vm(pkvm_handle_t handle);
 int __pkvm_init_vm(struct kvm *host_kvm, void *pgd);
-int __pkvm_init_vcpu(pkvm_handle_t handle, struct kvm_vcpu *host_vcpu,
-		     unsigned long vcpu_hva);
+int __pkvm_init_vcpu(pkvm_handle_t handle, struct kvm_vcpu *host_vcpu);
 
 int __pkvm_reclaim_dying_guest_page(pkvm_handle_t handle, u64 gfn);
 int __pkvm_start_teardown_vm(pkvm_handle_t handle);
arch/arm64/kvm/hyp/nvhe/hyp-main.c +1 -2
index 16f963aedd70..1315129dcd1f 100644
@@ -605,10 +605,9 @@ static void handle___pkvm_init_vcpu(struct kvm_cpu_context *host_ctxt)
 {
 	DECLARE_REG(pkvm_handle_t, handle, host_ctxt, 1);
 	DECLARE_REG(struct kvm_vcpu *, host_vcpu, host_ctxt, 2);
-	DECLARE_REG(unsigned long, vcpu_hva, host_ctxt, 3);
 
 	host_vcpu = kern_hyp_va(host_vcpu);
-	cpu_reg(host_ctxt, 1) = __pkvm_init_vcpu(handle, host_vcpu, vcpu_hva);
+	errno_to_smccc(__pkvm_init_vcpu(handle, host_vcpu), host_ctxt);
 }
 
 static void handle___pkvm_vcpu_in_poison_fault(struct kvm_cpu_context *host_ctxt)
arch/arm64/kvm/hyp/nvhe/pkvm.c +7 -54

113 lines not on this page · raw message

arch/arm64/kvm/pkvm.c +1 -10
index 8a8b5b84825c..1899b05d9fc2 100644
@@ -153,28 +153,19 @@ static void __pkvm_destroy_hyp_vm(struct kvm *kvm)
 
 	kvm->arch.pkvm.handle = 0;
 	kvm->arch.pkvm.is_created = false;
-	free_hyp_memcache(&kvm->arch.pkvm.teardown_mc);
 	free_hyp_memcache(&kvm->arch.pkvm.stage2_teardown_mc);
 }
 
 static int __pkvm_create_hyp_vcpu(struct kvm_vcpu *vcpu)
 {
-	size_t hyp_vcpu_sz = PAGE_ALIGN(PKVM_HYP_VCPU_SIZE);
 	pkvm_handle_t handle = vcpu->kvm->arch.pkvm.handle;
-	void *hyp_vcpu;
 	int ret;
 
 	init_hyp_stage2_memcache(&vcpu->arch.pkvm_memcache);
 
-	hyp_vcpu = alloc_pages_exact(hyp_vcpu_sz, GFP_KERNEL_ACCOUNT);
-	if (!hyp_vcpu)
-		return -ENOMEM;
-
-	ret = kvm_call_hyp_nvhe(__pkvm_init_vcpu, handle, vcpu, hyp_vcpu);
+	ret = pkvm_call_hyp_req(__pkvm_init_vcpu, handle, vcpu);
 	if (!ret)
 		vcpu_set_flag(vcpu, VCPU_PKVM_FINALIZED);
-	else
-		free_pages_exact(hyp_vcpu, hyp_vcpu_sz);
 
 	return ret;
 }
Signature · 1 line
2.55.0.rc2.803.g1fd1e6609c-goog
#46 lore raw

Re: [PATCH v2 16/18] KVM: arm64: Alloc pkvm_hyp_vcpu using pKVM heap allocator

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [High] The pKVM heap allocator (`hyp_alloc`) guarantees only 8-byte alignment, which violates the strict…

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [High] The pKVM heap allocator (hyp_alloc) guarantees only 8-byte alignment, which violates the strict 16-byte alignment requirement of struct pkvm_hyp_vcpu.
- [High] Delegating pkvm_hyp_vcpu allocation to the global EL2 heap bypasses memory cgroup (memcg) accounting, allowing unprivileged userspace to exceed memory limits. --

commit 6cb252c4c2be901676d82bdcc257dd6e52864352
Author: Vincent Donnefort <[email protected]>

KVM: arm64: Alloc pkvm_hyp_vcpu using pKVM heap allocator

Transition the allocation of the hypervisor vCPU state structure (pkvm_hyp_vcpu) from the host to the hypervisor using the new pKVM heap allocator (hyp_alloc()).

Quoted 4 lines
arch/arm64/kvm/hyp/nvhe/pkvm.c +0 -0
index 2f616a89f8b35..45ec77e9eca82 100644

[ ... ]

Quoted 4 lines
arch/arm64/kvm/hyp/nvhe/pkvm.c +0 -0
@@ -896,16 +863,15 @@ static int register_hyp_vcpu(struct pkvm_hyp_vm *hyp_vm,
 	return 0;
 }
 

arch/arm64/kvm/hyp/nvhe/pkvm.c static int register_hyp_vcpu(struct pkvm_hyp_vm *hyp_vm, · line 872

Quoted 10 lines
arch/arm64/kvm/hyp/nvhe/pkvm.c +2 -3
-int __pkvm_init_vcpu(pkvm_handle_t handle, struct kvm_vcpu *host_vcpu,
-		     unsigned long vcpu_hva)
+int __pkvm_init_vcpu(pkvm_handle_t handle, struct kvm_vcpu *host_vcpu)
 {
 	struct pkvm_hyp_vcpu *hyp_vcpu;
 	struct pkvm_hyp_vm *hyp_vm;
 	int ret;
 
-	hyp_vcpu = map_donated_memory(vcpu_hva, sizeof(*hyp_vcpu));
+	hyp_vcpu = hyp_alloc(sizeof(*hyp_vcpu));

[Severity: High]
Will this allocation guarantee the 16-byte alignment required by struct pkvm_hyp_vcpu?

The struct embeds a struct kvm_vcpu, which contains a struct user_fpsimd_state with a __uint128_t vregs[32] array. The C ABI and AAPCS64 mandate strict 16-byte alignment for 128-bit types.

If hyp_alloc() only guarantees 8-byte alignment (since its MIN_ALLOC_SIZE is 8), returning an 8-byte aligned pointer could trigger an alignment fault during compiler-generated SIMD instructions (like LDP Qx, Qy) when saving or restoring FPSIMD state, potentially leading to a hypervisor panic.

Quoted 5 lines
arch/arm64/kvm/hyp/nvhe/pkvm.c +1 -1
 	if (!hyp_vcpu)
-		return -ENOMEM;
+		return hyp_alloc_errno();
 
 	hyp_spin_lock(&vm_table_lock);

[ ... ]

Quoted 4 lines
arch/arm64/kvm/pkvm.c +0 -0
index 8a8b5b84825c7..1899b05d9fc22 100644

[ ... ]

Quoted 13 lines
arch/arm64/kvm/pkvm.c +0 -3
@@ -153,28 +153,19 @@ static void __pkvm_destroy_hyp_vm(struct kvm *kvm)
 
 	kvm->arch.pkvm.handle = 0;
 	kvm->arch.pkvm.is_created = false;
-	free_hyp_memcache(&kvm->arch.pkvm.teardown_mc);
 	free_hyp_memcache(&kvm->arch.pkvm.stage2_teardown_mc);
 }
 
 static int __pkvm_create_hyp_vcpu(struct kvm_vcpu *vcpu)
 {
-	size_t hyp_vcpu_sz = PAGE_ALIGN(PKVM_HYP_VCPU_SIZE);
 	pkvm_handle_t handle = vcpu->kvm->arch.pkvm.handle;
-	void *hyp_vcpu;

arch/arm64/kvm/pkvm.c static void __pkvm_destroy_hyp_vm(struct kvm *kvm) · line 166

Quoted 10 lines
arch/arm64/kvm/pkvm.c +1 -5
 	int ret;
 
 	init_hyp_stage2_memcache(&vcpu->arch.pkvm_memcache);
 
-	hyp_vcpu = alloc_pages_exact(hyp_vcpu_sz, GFP_KERNEL_ACCOUNT);
-	if (!hyp_vcpu)
-		return -ENOMEM;
-
-	ret = kvm_call_hyp_nvhe(__pkvm_init_vcpu, handle, vcpu, hyp_vcpu);
+	ret = pkvm_call_hyp_req(__pkvm_init_vcpu, handle, vcpu);

[Severity: High]
Does this change bypass memory cgroup (memcg) accounting for the vCPU allocations?

Previously, alloc_pages_exact() used GFP_KERNEL_ACCOUNT, which ensured the memory was charged to the allocating process. By delegating the allocation to the EL2 heap via pkvm_call_hyp_req(), a heap depletion will trigger a top-up request (PKVM_HYP_REQ_HYP_ALLOC).

The host handler for this top-up (pkvm_hyp_topup()) uses init_hyp_memcache(), which defaults to GFP_KERNEL and omits the HYP_MEMCACHE_ACCOUNT_KMEMCG flag.

Could this allow an unprivileged userspace process to bypass memcg limits and exhaust system memory by creating numerous VMs and vCPUs?

Quoted 7 lines
arch/arm64/kvm/pkvm.c +0 -2
 	if (!ret)
 		vcpu_set_flag(vcpu, VCPU_PKVM_FINALIZED);
-	else
-		free_pages_exact(hyp_vcpu, hyp_vcpu_sz);
 
 	return ret;
 }
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=16
reviewer
#47 lore raw

Re: [PATCH v2 16/18] KVM: arm64: Alloc pkvm_hyp_vcpu using pKVM heap allocator

In reply to Vincent Donnefort

On Mon, 6 Jul 2026 at 18:54, Vincent Donnefort <[email protected]> wrote:

...

arch/arm64/kvm/hyp/nvhe/pkvm.c static int register_hyp_vcpu(struct pkvm_hyp_vm *hyp_vm, · line 872

Quoted 10 lines
arch/arm64/kvm/hyp/nvhe/pkvm.c +2 -3
-int __pkvm_init_vcpu(pkvm_handle_t handle, struct kvm_vcpu *host_vcpu,
-                    unsigned long vcpu_hva)
+int __pkvm_init_vcpu(pkvm_handle_t handle, struct kvm_vcpu *host_vcpu)
 {
        struct pkvm_hyp_vcpu *hyp_vcpu;
        struct pkvm_hyp_vm *hyp_vm;
        int ret;
 
-       hyp_vcpu = map_donated_memory(vcpu_hva, sizeof(*hyp_vcpu));
+       hyp_vcpu = hyp_alloc(sizeof(*hyp_vcpu));

On Sashiko's alignment point: hyp_alloc only guarantees 8-byte alignment while pkvm_hyp_vcpu wants 16 (the __uint128_t vregs[32]), and the old map_donated_memory path was page-aligned, so it's a real reduction.

But if my understanding of the ARM ARM is correct, and please note that I just consulted the spec when replying to this, so it's a big *if*: I don't think it faults. We run EL2 with SCTLR_EL2.A == 0 (INIT_SCTLR_EL2_MMU_ON has SA but not A), and FPSIMD save/restore is plain LDR/STR/LDP/STP of Q registers. From the ARM ARM (DDI 0487M.c, B2.8.2.1.1):

"When the value of SCTLR_ELx.A applicable to the current Exception level is 0: An unaligned access is performed."

and, covering LDP/STP:

"For these instructions, the definition of an unaligned access is based on the size of the accessed elements, not the overall size of the memory access. This affects SIMD and SVE element and structure loads and stores, and also load/store pair instructions."

However, the alignment here is still weaker than the kmalloc/alloc_pages callers relied on , so it might be cleaner to give hyp_alloc a 16-byte minimum alignment at the allocator level rather than per-caller. What do you think?

Cheers,
/fuad

Quoted 96 lines
        if (!hyp_vcpu)
arch/arm64/kvm/hyp/nvhe/pkvm.c +5 -18
-               return -ENOMEM;
+               return hyp_alloc_errno();
 
        hyp_spin_lock(&vm_table_lock);
@@ -926,24 +892,12 @@ int __pkvm_init_vcpu(pkvm_handle_t handle, struct kvm_vcpu *host_vcpu,
        }
 unlock:
        hyp_spin_unlock(&vm_table_lock);
-
        if (ret)
-               unmap_donated_memory(hyp_vcpu, sizeof(*hyp_vcpu));
+               hyp_free(hyp_vcpu);
+
        return ret;
 }
 
-static void
-teardown_donated_memory(struct kvm_hyp_memcache *mc, void *addr, size_t size)
-{
-       size = PAGE_ALIGN(size);
-       memset(addr, 0, size);
-
-       for (void *start = addr; start < addr + size; start += PAGE_SIZE)
-               push_hyp_memcache(mc, start, hyp_virt_to_phys);
-
-       unmap_donated_memory_noclear(addr, size);
-}
-
 int __pkvm_reclaim_dying_guest_page(pkvm_handle_t handle, u64 gfn)
 {
        struct pkvm_hyp_vm *hyp_vm = get_pkvm_hyp_vm(handle);
@@ -993,7 +947,7 @@ int __pkvm_start_teardown_vm(pkvm_handle_t handle)
 
 int __pkvm_finalize_teardown_vm(pkvm_handle_t handle)
 {
-       struct kvm_hyp_memcache *mc, *stage2_mc;
+       struct kvm_hyp_memcache *stage2_mc;
        struct pkvm_hyp_vm *hyp_vm;
        struct kvm *host_kvm;
        unsigned int idx;
@@ -1014,7 +968,6 @@ int __pkvm_finalize_teardown_vm(pkvm_handle_t handle)
        hyp_spin_unlock(&vm_table_lock);
 
        /* Reclaim guest pages (including page-table pages) */
-       mc = &host_kvm->arch.pkvm.teardown_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->kvm.created_vcpus);
@@ -1036,7 +989,7 @@ int __pkvm_finalize_teardown_vm(pkvm_handle_t handle)
                        unmap_donated_memory_noclear(addr, PAGE_SIZE);
                }
 
-               teardown_donated_memory(mc, hyp_vcpu, sizeof(*hyp_vcpu));
+               hyp_free(hyp_vcpu);
        }
 
        hyp_free(hyp_vm);
arch/arm64/kvm/pkvm.c +1 -11
index 8a8b5b84825c..1899b05d9fc2 100644
@@ -153,28 +153,19 @@ static void __pkvm_destroy_hyp_vm(struct kvm *kvm)
 
        kvm->arch.pkvm.handle = 0;
        kvm->arch.pkvm.is_created = false;
-       free_hyp_memcache(&kvm->arch.pkvm.teardown_mc);
        free_hyp_memcache(&kvm->arch.pkvm.stage2_teardown_mc);
 }
 
 static int __pkvm_create_hyp_vcpu(struct kvm_vcpu *vcpu)
 {
-       size_t hyp_vcpu_sz = PAGE_ALIGN(PKVM_HYP_VCPU_SIZE);
        pkvm_handle_t handle = vcpu->kvm->arch.pkvm.handle;
-       void *hyp_vcpu;
        int ret;
 
        init_hyp_stage2_memcache(&vcpu->arch.pkvm_memcache);
 
-       hyp_vcpu = alloc_pages_exact(hyp_vcpu_sz, GFP_KERNEL_ACCOUNT);
-       if (!hyp_vcpu)
-               return -ENOMEM;
-
-       ret = kvm_call_hyp_nvhe(__pkvm_init_vcpu, handle, vcpu, hyp_vcpu);
+       ret = pkvm_call_hyp_req(__pkvm_init_vcpu, handle, vcpu);
        if (!ret)
                vcpu_set_flag(vcpu, VCPU_PKVM_FINALIZED);
-       else
-               free_pages_exact(hyp_vcpu, hyp_vcpu_sz);
 
        return ret;
 }
--

2.55.0.rc2.803.g1fd1e6609c-goog

#48 lore raw

PATCH v2 17/18 KVM: arm64: Reject hyp trace descriptors with fewer CPUs than hyp_nr_cpus

In reply to Vincent Donnefort

While a trace descriptor with fewer CPUs than hyp_nr_cpus currently causes no functional issue, such a configuration is invalid and would cause the hypervisor to skip ring buffer initialization when allocating backing pages in EL2.

Enforce nr_cpus == hyp_nr_cpus during trace descriptor validation to guarantee that every possible CPU has a valid ring buffer descriptor.

arch/arm64/kvm/hyp/nvhe/trace.c +3 -0
index e7e150ab265f..6e716295247a 100644
@@ -181,6 +181,9 @@ static bool hyp_trace_desc_is_valid(struct hyp_trace_desc *desc, size_t desc_siz
 	desc_end = (void *)desc + desc_size;
 	nr_bpages = desc->bpages_backing_size / sizeof(struct simple_buffer_page);
 
+	if (desc->trace_buffer_desc.nr_cpus != hyp_nr_cpus)
+		return false;
+
 	for_each_ring_buffer_desc(rb_desc, cpu, &desc->trace_buffer_desc) {
 		/* Can we read nr_page_va? */
 		if ((void *)rb_desc + struct_size(rb_desc, page_va, 0) > desc_end)
Signature · 1 line
2.55.0.rc2.803.g1fd1e6609c-goog
reviewer Reviewed-by on v2 17/18
#49 lore raw

Re: [PATCH v2 17/18] KVM: arm64: Reject hyp trace descriptors with fewer CPUs than hyp_nr_cpus

Quoted 10 lines · Vincent Donnefort

While a trace descriptor with fewer CPUs than hyp_nr_cpus currently causes no functional issue, such a configuration is invalid and would cause the hypervisor to skip ring buffer initialization when allocating backing pages in EL2.

Enforce nr_cpus == hyp_nr_cpus during trace descriptor validation to guarantee that every possible CPU has a valid ring buffer descriptor.

Cheers,
/fuad

Quoted 18 lines
arch/arm64/kvm/hyp/nvhe/trace.c +3 -1
index e7e150ab265f..6e716295247a 100644
@@ -181,6 +181,9 @@ static bool hyp_trace_desc_is_valid(struct hyp_trace_desc *desc, size_t desc_siz
        desc_end = (void *)desc + desc_size;
        nr_bpages = desc->bpages_backing_size / sizeof(struct simple_buffer_page);
 
+       if (desc->trace_buffer_desc.nr_cpus != hyp_nr_cpus)
+               return false;
+
        for_each_ring_buffer_desc(rb_desc, cpu, &desc->trace_buffer_desc) {
                /* Can we read nr_page_va? */
                if ((void *)rb_desc + struct_size(rb_desc, page_va, 0) > desc_end)
--

2.55.0.rc2.803.g1fd1e6609c-goog

#50 lore raw

PATCH v2 18/18 KVM: arm64: Alloc simple_buffer_page using pKVM hyp allocator

In reply to Vincent Donnefort

In protected mode, transition the allocation of the simple_ring_buffer structures from the host to the hypervisor using the new pKVM heap allocator.

Previously, the host allocated and donated a contiguous backing memory for these structures. In pKVM the hypervisor can now allocate them dynamically.

Use the pkvm_call_hyp_req() wrapper in the host to invoke __tracing_load, which automatically handles any top-up requests if the hypervisor runs out of heap memory during allocation.

arch/arm64/kvm/hyp/nvhe/hyp-main.c +1 -1
index 1315129dcd1f..090319bddf86 100644
@@ -745,7 +745,7 @@ static void handle___tracing_load(struct kvm_cpu_context *host_ctxt)
 	DECLARE_REG(unsigned long, desc_hva, host_ctxt, 1);
 	DECLARE_REG(size_t, desc_size, host_ctxt, 2);
 
-	cpu_reg(host_ctxt, 1) = __tracing_load(desc_hva, desc_size);
+	errno_to_smccc(__tracing_load(desc_hva, desc_size), host_ctxt);
 }
 
 static void handle___tracing_unload(struct kvm_cpu_context *host_ctxt)
arch/arm64/kvm/hyp/nvhe/trace.c +36 -27

125 lines not on this page · raw message

arch/arm64/kvm/hyp_trace.c +14 -2
index 2411b4c32932..405ccd954524 100644
@@ -13,6 +13,7 @@
 #include <asm/kvm_host.h>
 #include <asm/kvm_hyptrace.h>
 #include <asm/kvm_mmu.h>
+#include <asm/kvm_pkvm.h>
 
 #include "hyp_trace.h"
 
@@ -157,10 +158,18 @@ static void __unshare_page(unsigned long va)
 
 static int hyp_trace_buffer_alloc_bpages_backing(struct hyp_trace_buffer *trace_buffer, size_t size)
 {
-	int nr_bpages = (PAGE_ALIGN(size) / PAGE_SIZE) + 1;
 	size_t backing_size;
+	int nr_bpages;
 	void *start;
 
+	/* pKVM uses hyp_alloc() to allocate struct simple_buffer_page */
+	if (is_protected_kvm_enabled()) {
+		trace_buffer->desc->bpages_backing_start = 0;
+		trace_buffer->desc->bpages_backing_size = 0;
+		return 0;
+	}
+
+	nr_bpages = (PAGE_ALIGN(size) / PAGE_SIZE) + 1;
 	backing_size = PAGE_ALIGN(sizeof(struct simple_buffer_page) * nr_bpages *
 				  num_possible_cpus());
 
@@ -176,6 +185,9 @@ static int hyp_trace_buffer_alloc_bpages_backing(struct hyp_trace_buffer *trace_
 
 static void hyp_trace_buffer_free_bpages_backing(struct hyp_trace_buffer *trace_buffer)
 {
+	if (!trace_buffer->desc->bpages_backing_start)
+		return;
+
 	free_pages_exact((void *)trace_buffer->desc->bpages_backing_start,
 			 trace_buffer->desc->bpages_backing_size);
 }
@@ -264,7 +276,7 @@ static struct trace_buffer_desc *hyp_trace_load(unsigned long size, void *priv)
 	if (ret)
 		goto err_free_buffer;
 
-	ret = kvm_call_hyp_nvhe(__tracing_load, (unsigned long)desc, desc_size);
+	ret = pkvm_call_hyp_req(__tracing_load, (unsigned long)desc, desc_size);
 	if (ret)
 		goto err_unload_pages;
 
Signature · 1 line
2.55.0.rc2.803.g1fd1e6609c-goog
#51 lore raw

Re: [PATCH v2 18/18] KVM: arm64: Alloc simple_buffer_page using pKVM hyp allocator

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] Missing validation of host-supplied `nr_page_va` causes EL2 memory leak and host infinite loop. --

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] Missing validation of host-supplied nr_page_va causes EL2 memory leak and host infinite loop. --

commit 20e38c941fd5614b7d02825f2350aeac0276e149
Author: Vincent Donnefort <[email protected]>

KVM: arm64: Alloc simple_buffer_page using pKVM hyp allocator

This patch transitions the allocation of the simple_ring_buffer structures from the host to the hypervisor in protected mode using the new pKVM heap allocator. It uses pkvm_call_hyp_req() to handle potential top-up requests during allocation.

Quoted 4 lines
arch/arm64/kvm/hyp/nvhe/trace.c +0 -0
index 6e716295247ad..bcfe06bba29bb 100644

[ ... ]

Quoted 17 lines
arch/arm64/kvm/hyp/nvhe/trace.c +4 -6
@@ -62,18 +63,34 @@ static void __release_host_mem(void *start, u64 size)
 	WARN_ON(__pkvm_hyp_donate_host(hyp_virt_to_pfn(start), size >> PAGE_SHIFT));
 }
 
-static int hyp_trace_buffer_load_bpage_backing(struct hyp_trace_buffer *trace_buffer,
-					       struct hyp_trace_desc *desc)
+static int hyp_trace_buffer_alloc_bpages(struct hyp_trace_buffer *trace_buffer,
+					 struct hyp_trace_desc *desc)
 {
-	void *start = (void *)kern_hyp_va(desc->bpages_backing_start);
-	size_t size = desc->bpages_backing_size;
+	void *start;
+	size_t size;
 	int ret;
 
-	ret = __admit_host_mem(start, size);
-	if (ret)

arch/arm64/kvm/hyp/nvhe/trace.c static void __release_host_mem(void *start, u64 size) · line 81

Quoted 10 lines
arch/arm64/kvm/hyp/nvhe/trace.c +9 -1
-		return ret;
+	if (is_protected_kvm_enabled()) {
+		struct ring_buffer_desc *rb_desc;
+		int cpu;
+
+		size = 0;
+		for_each_ring_buffer_desc(rb_desc, cpu, &desc->trace_buffer_desc)
+			size += rb_desc->nr_page_va * sizeof(struct simple_buffer_page);
+
+		start = hyp_alloc(size);

[Severity: High]
Is there a missing bounds check on the host-supplied nr_page_va here?

If the host sets nr_page_va to 0 for all CPUs, size will be 0, but hyp_alloc(0) will still succeed by allocating MIN_ALLOC_SIZE.

Quoted 13 lines
arch/arm64/kvm/hyp/nvhe/trace.c +11 -1
+		if (!start)
+			return hyp_alloc_errno();
+	} else {
+		start = (void *)kern_hyp_va(desc->bpages_backing_start);
+		size = desc->bpages_backing_size;
 
-	memset(start, 0, size);
+		ret = __admit_host_mem(start, size);
+		if (ret)
+			return ret;
+
+		memset(start, 0, size);
+	}

[ ... ]

arch/arm64/kvm/hyp/nvhe/trace.c static void hyp_trace_buffer_unload_bpage_backing(struct hyp_trace_buffer *trace · line 107

Quoted 12 lines
arch/arm64/kvm/hyp/nvhe/trace.c +1 -1
@@ -81,7 +98,7 @@ static int hyp_trace_buffer_load_bpage_backing(struct hyp_trace_buffer *trace_bu
 	return 0;
 }
 
-static void hyp_trace_buffer_unload_bpage_backing(struct hyp_trace_buffer *trace_buffer)
+static void hyp_trace_buffer_free_bpages(struct hyp_trace_buffer *trace_buffer)
 {
 	void *start = trace_buffer->bpages_backing_start;
 	size_t size = trace_buffer->bpages_backing_size;
@@ -89,9 +106,12 @@ static void hyp_trace_buffer_unload_bpage_backing(struct hyp_trace_buffer *trace
 	if (!size)
 		return;

[Severity: High]
Will this early return permanently leak the memory allocated above?

If size evaluated to 0 during allocation, simple_ring_buffer_init_mm() fails with -EINVAL and triggers this cleanup path. Since size is 0, this returns early without calling hyp_free(), leaking the MIN_ALLOC_SIZE chunk in the EL2 heap.

Could exhausting the EL2 heap this way cause a subsequent 0-page topup request back to the host, resulting in an infinite loop in the host CPU's pkvm_call_hyp_req() macro?

Quoted 12 lines
arch/arm64/kvm/hyp/nvhe/trace.c +6 -3
-	memset(start, 0, size);
-
-	__release_host_mem(start, size);
+	if (is_protected_kvm_enabled()) {
+		hyp_free(start);
+	} else {
+		memset(start, 0, size);
+		__release_host_mem(start, size);
+	}
 
 	trace_buffer->bpages_backing_start = 0;
 	trace_buffer->bpages_backing_size = 0;
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=18
reviewer
#52 lore raw

Re: [PATCH v2 18/18] KVM: arm64: Alloc simple_buffer_page using pKVM hyp allocator

In reply to Vincent Donnefort

On Mon, 6 Jul 2026 at 18:54, Vincent Donnefort <[email protected]> wrote:

...

arch/arm64/kvm/hyp/nvhe/trace.c static bool hyp_trace_desc_is_valid(struct hyp_trace_desc *desc, size_t desc_siz · line 215

Quoted 13 lines
arch/arm64/kvm/hyp/nvhe/trace.c +0 -7
@@ -193,17 +211,8 @@ static bool hyp_trace_desc_is_valid(struct hyp_trace_desc *desc, size_t desc_siz
                if ((void *)rb_desc + struct_size(rb_desc, page_va, rb_desc->nr_page_va) > desc_end)
                        return false;
 
-               /* Overflow bpages backing memory? */
-               if (nr_bpages < rb_desc->nr_page_va)
-                       return false;
-
-               if (cpu >= hyp_nr_cpus)
-                       return false;
-
                if (cpu != rb_desc->cpu)
                        return false;

Sashiko flagged this. Nothing here bounds nr_page_va from below, so with nr_page_va == 0 on every CPU the bpage size sums to 0. hyp_alloc() still hands back a chunk, but bpages_backing_size is stored as 0, so when init_mm() fails on nr_page_va < 3 the free path takes its if (!size) return and never frees it. The host can repeat it. Would validating nr_page_va >= 3 here work?

I don't think the rest of Sashiko's note holds: the zero-size path succeeds rather than hitting -ENOMEM. And it's only reachable from the host kernel, not userspace.

Cheers,
/fuad

#53 lore raw

Re: [PATCH v2 18/18] KVM: arm64: Alloc simple_buffer_page using pKVM hyp allocator

Quoted 8 lines · Fuad Tabba

On Mon, 6 Jul 2026 at 18:54, Vincent Donnefort <[email protected]> wrote:

...

Quoted 4 lines
arch/arm64/kvm/hyp/nvhe/trace.c +0 -0
@@ -193,17 +211,8 @@ static bool hyp_trace_desc_is_valid(struct hyp_trace_desc *desc, size_t desc_siz
                if ((void *)rb_desc + struct_size(rb_desc, page_va, rb_desc->nr_page_va) > desc_end)
                        return false;
 
Quoted 16 lines

arch/arm64/kvm/hyp/nvhe/trace.c static bool hyp_trace_desc_is_valid(struct hyp_trace_desc *desc, size_t desc_siz · line 215

Quoted 9 lines
arch/arm64/kvm/hyp/nvhe/trace.c +0 -7
-               /* Overflow bpages backing memory? */
-               if (nr_bpages < rb_desc->nr_page_va)
-                       return false;
-
-               if (cpu >= hyp_nr_cpus)
-                       return false;
-
                if (cpu != rb_desc->cpu)
                        return false;

Sashiko flagged this. Nothing here bounds nr_page_va from below, so with nr_page_va == 0 on every CPU the bpage size sums to 0. hyp_alloc() still hands back a chunk, but bpages_backing_size is stored as 0, so when init_mm() fails on nr_page_va < 3 the free path takes its if (!size) return and never frees it. The host can repeat it. Would validating nr_page_va >= 3 here work?

simple_ring_buffer_init_mm() already validates nr_page_va. And on error, we do seem to rollback properly and hyp_free().

So I don't believe there's anything to do here.

Quoted 7 lines

I don't think the rest of Sashiko's note holds: the zero-size path succeeds rather than hitting -ENOMEM. And it's only reachable from the host kernel, not userspace.

Cheers,
/fuad

#54 lore raw

Re: [PATCH v2 18/18] KVM: arm64: Alloc simple_buffer_page using pKVM hyp allocator

Quoted 26 lines · Vincent Donnefort
Quoted 8 lines · Fuad Tabba

On Mon, 6 Jul 2026 at 18:54, Vincent Donnefort <[email protected]> wrote:

...

Quoted 4 lines
arch/arm64/kvm/hyp/nvhe/trace.c +0 -0
@@ -193,17 +211,8 @@ static bool hyp_trace_desc_is_valid(struct hyp_trace_desc *desc, size_t desc_siz
                if ((void *)rb_desc + struct_size(rb_desc, page_va, rb_desc->nr_page_va) > desc_end)
                        return false;
 
Quoted 16 lines

arch/arm64/kvm/hyp/nvhe/trace.c static bool hyp_trace_desc_is_valid(struct hyp_trace_desc *desc, size_t desc_siz · line 215

Quoted 9 lines
arch/arm64/kvm/hyp/nvhe/trace.c +0 -7
-               /* Overflow bpages backing memory? */
-               if (nr_bpages < rb_desc->nr_page_va)
-                       return false;
-
-               if (cpu >= hyp_nr_cpus)
-                       return false;
-
                if (cpu != rb_desc->cpu)
                        return false;

Sashiko flagged this. Nothing here bounds nr_page_va from below, so with nr_page_va == 0 on every CPU the bpage size sums to 0. hyp_alloc() still hands back a chunk, but bpages_backing_size is stored as 0, so when init_mm() fails on nr_page_va < 3 the free path takes its if (!size) return and never frees it. The host can repeat it. Would validating nr_page_va >= 3 here work?

Quoted 4 lines

simple_ring_buffer_init_mm() already validates nr_page_va. And on error, we do seem to rollback properly and hyp_free().

So I don't believe there's anything to do here.

Ahhh no my bad! Sashiko's right. Because the size is 0... this will appear as "unloaded" ...

Indeed perhaps the best check here is to make sure we have the minium nr_page_va...

Quoted 8 lines
Quoted 8 lines

I don't think the rest of Sashiko's note holds: the zero-size path succeeds rather than hitting -ENOMEM. And it's only reachable from the host kernel, not userspace.

Cheers,
/fuad