Skip to content

KVM: arm64: Introduce pKVM hypervisor heap allocator

Vincent Donnefort kvmarm linux-arm-kernel 6 versions – Applied

Status from this message: Applied to next, thanks!

Get series with b4
b4 am [email protected]

Versions newest first · linked by author and title

  1. v6 Latest18 patches 1 replyApplied
  2. v518 patches 5 repliesSuperseded
  3. v417 patches 25 repliesSuperseded
Earlier versions · 3
VersionPostedPatchesRepliesStatus
v317 patches8 repliesSuperseded
v218 patches29 repliesSuperseded
v117 patches0 repliesSuperseded

Changes in v6 from the cover letter

- Rebased on kvm-arm64/hyp-type-checking-7.4
- make __pkvm_hyp_alloc_selftest pre-deprivilege only (Sashiko)
- Remove __hyp_private_va_start for __io_map_next to ease merge.
- Fix sparse NULL pointer warnings in hyp_allocator_map() and hyp_allocator_create_chunk()

Patches in v6 Applied

#Subject and filesSizeRepliesApplied
1/18KVM: arm64: Add pkvm_private_va_range_paarch/arm64/kvm/hyp/include/nvhe/mm.h arch/arm64/kvm/hyp/include/nvhe/spinlock.h arch/arm64/kvm/hyp/nvhe/mm.c+23 -00 replies
2/18KVM: arm64: Add pkvm_remove_mappingsarch/arm64/kvm/hyp/include/nvhe/mm.h arch/arm64/kvm/hyp/nvhe/mm.c+19 -00 replies
3/18KVM: arm64: Add pkvm_map_private_va_rangearch/arm64/kvm/hyp/include/nvhe/mm.h arch/arm64/kvm/hyp/nvhe/mm.c+52 -80 replies
4/18KVM: arm64: Add a heap allocator for the pKVM hyparch/arm64/kvm/hyp/include/nvhe/alloc.h arch/arm64/kvm/hyp/nvhe/Makefile arch/arm64/kvm/hyp/nvhe/alloc.c +1 more+1051 -10 replies
5/18KVM: arm64: Allow kvm_hyp_memcache usage outside of stage-2arch/arm64/include/asm/kvm_host.h arch/arm64/kvm/mmu.c arch/arm64/kvm/pkvm.c+18 -30 replies
6/18KVM: arm64: Add pkvm_hyp_req infrastructurearch/arm64/include/asm/kvm_pkvm.h arch/arm64/kvm/pkvm.c arch/arm64/kvm/trace_pkvm.h+171 -00 replies
7/18KVM: arm64: Add PKVM_HYP_REQ_HYP_ALLOC requestarch/arm64/include/asm/kvm_asm.h arch/arm64/include/asm/kvm_hcall.h arch/arm64/include/asm/kvm_pkvm.h +3 more+72 -20 replies
8/18KVM: arm64: Add reclaim interface for the pKVM heap allocarch/arm64/include/asm/kvm_asm.h arch/arm64/include/asm/kvm_hcall.h arch/arm64/kvm/hyp/nvhe/hyp-main.c +1 more+84 -00 replies
9/18KVM: arm64: Add selftests for the pKVM heap allocatorarch/arm64/include/asm/kvm_asm.h arch/arm64/include/asm/kvm_hcall.h arch/arm64/include/asm/kvm_pkvm.h +5 more+247 -10 replies
10/18KVM: arm64: Add a shrinker for pKVMarch/arm64/include/asm/kvm_hcall.h arch/arm64/kvm/pkvm.c+47 -40 replies
11/18KVM: arm64: Filter out non-kernel addresses in kern_hyp_vaarch/arm64/include/asm/kvm_mmu.h+3 -00 replies
12/18KVM: arm64: Move hyp_vm refcount into the structurearch/arm64/kvm/hyp/include/nvhe/pkvm.h arch/arm64/kvm/hyp/nvhe/pkvm.c+18 -50 replies
13/18KVM: arm64: Alloc pkvm_hyp_vm using pKVM heap allocatorarch/arm64/include/asm/kvm_hcall.h arch/arm64/kvm/hyp/hyp-constants.c arch/arm64/kvm/hyp/include/nvhe/pkvm.h +3 more+47 -380 replies
14/18KVM: arm64: Alloc pkvm_hyp_vcpu using pKVM heap allocatorarch/arm64/include/asm/kvm_hcall.h arch/arm64/include/asm/kvm_host.h arch/arm64/kvm/hyp/hyp-constants.c +4 more+12 -500 replies
15/18KVM: arm64: Rename vCPU pkvm_memcache to stage2_mcarch/arm64/include/asm/kvm_host.h arch/arm64/kvm/arm.c arch/arm64/kvm/hyp/nvhe/hyp-main.c +4 more+16 -160 replies
16/18KVM: arm64: Reject hyp trace descriptors with fewer CPUs than hyp_nr_cpusarch/arm64/kvm/hyp/nvhe/trace.c+3 -00 replies
17/18KVM: arm64: Reject hyp trace descriptors with fewer than 3 pagesarch/arm64/kvm/hyp/nvhe/trace.c+4 -00 replies
18/18KVM: arm64: Alloc simple_buffer_page using pKVM hyp allocatorarch/arm64/kvm/hyp/nvhe/hyp-main.c arch/arm64/kvm/hyp/nvhe/trace.c arch/arm64/kvm/hyp_trace.c+50 -300 replies

Reviews trailers per patch

17 of 18 patches have a Reviewed-by or Acked-by.

PatchFuad Tabba
1/18R-b T-b
2/18R-b T-b
3/18R-b T-b
4/18T-b R-b
5/18R-b T-b
6/18R-b T-b
7/18T-b R-b
8/18R-b T-b
9/18R-b T-b
10/18T-b
11/18R-b T-b
12/18R-b T-b
13/18T-b R-b
14/18T-b R-b
15/18T-b R-b
16/18R-b T-b
17/18T-b R-b
18/18T-b R-b

Trailers given in replies are filled; those carried in the patches from earlier versions are outlined with dashes.

Read the v6 thread

Cover letter [PATCH v6 00/18] 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.

Changelog
---------

v6:

- Rebased on kvm-arm64/hyp-type-checking-7.4
- make __pkvm_hyp_alloc_selftest pre-deprivilege only (Sashiko)
- Remove __hyp_private_va_start for __io_map_next to ease merge.
- Fix sparse NULL pointer warnings in hyp_allocator_map() and hyp_allocator_create_chunk()

v5:

- Remove unreachable !prev checks in hyp_allocator_destroy_chunk() (Fuad)
- Add kerneldoc to pkvm_call_hyp_req() (Fuad)
- Avoid duplicate handle___pkvm_hyp_alloc_selftest() definitions when !CONFIG_NVHE_EL2_DEBUG
- Chunk pkvm_hyp_reclaim() with cond_resched() to avoid blocking in EL2
- Allow shrinker to scan across all runtime topup IDs
- Reclaim chunks before draining allocator->mc in hyp_allocator_reclaim() (Fuad)
- Make is_ttbr1_addr() check in __kern_hyp_va() conditional to pKVM (Fuad)
- Rename pkvm_memcache to stage2_mc
- Rebased on 7.3-rc1

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

- Add kerneldoc to pkvm_remove_mappings
- Allow pkvm_private_va_range_pa() to work with block-level mappings (Sashiko)
- Add rollback and harden pkvm_map_private_va_range input (Sashiko)
- Add missing mc count into reclaimable memory
- Differentiate -ENOMEM from hyp_alloc in errno_to_smccc() (Sashiko)
- Collect Fuad's Tested-by

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

- Remove unsafe WARN_ON(hyp_spin_is_locked(&pkvm_pgd_lock)) check in hyp_allocator_alloc() (Sashiko)
- Modify MIN_ALLOC_SIZE to 16-bytes to comply with FPSIMD alignment requirements (Sashiko)
- Allow hyp topup/reclaim HVCs pre-deprivilege
- Add enum symbols to pkvm_hyp_req_handle event (Fuad)
- Various clarification in commit descriptions (Fuad)
- Restore unmap_donated_memory() for PGD on error path (Fuad)
- Renamed __hyp_allocator_map -> pkvm_map_private_va_range (Fuad)
- Collected Fuad's Reviewed-by tags
- Rebased on 7.2-rc4

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

- 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_va_range_pa
KVM: arm64: Add pkvm_remove_mappings
KVM: arm64: Add pkvm_map_private_va_range
KVM: arm64: Add a heap allocator for the pKVM hyp
KVM: arm64: Allow kvm_hyp_memcache usage outside of stage-2
KVM: arm64: Add pkvm_hyp_req infrastructure
KVM: arm64: Add 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: Alloc pkvm_hyp_vm using pKVM heap allocator
KVM: arm64: Alloc pkvm_hyp_vcpu using pKVM heap allocator
KVM: arm64: Rename vCPU pkvm_memcache to stage2_mc
KVM: arm64: Reject hyp trace descriptors with fewer CPUs than
hyp_nr_cpus
KVM: arm64: Reject hyp trace descriptors with fewer than 3 pages
KVM: arm64: Alloc simple_buffer_page using pKVM hyp allocator

 arch/arm64/include/asm/kvm_asm.h           |    4 +
 arch/arm64/include/asm/kvm_hcall.h         |   19 +-
 arch/arm64/include/asm/kvm_host.h          |   18 +-
 arch/arm64/include/asm/kvm_mmu.h           |    3 +
 arch/arm64/include/asm/kvm_pkvm.h          |  111 ++
 arch/arm64/kvm/arm.c                       |    4 +-
 arch/arm64/kvm/hyp/hyp-constants.c         |    2 -
 arch/arm64/kvm/hyp/include/nvhe/alloc.h    |   28 +
 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 |    4 +
 arch/arm64/kvm/hyp/nvhe/Makefile           |    2 +-
 arch/arm64/kvm/hyp/nvhe/alloc.c            | 1211 ++++++++++++++++++++
 arch/arm64/kvm/hyp/nvhe/hyp-main.c         |  129 ++-
 arch/arm64/kvm/hyp/nvhe/mem_protect.c      |   10 +-
 arch/arm64/kvm/hyp/nvhe/mm.c               |   95 +-
 arch/arm64/kvm/hyp/nvhe/pkvm.c             |   79 +-
 arch/arm64/kvm/hyp/nvhe/setup.c            |    6 +
 arch/arm64/kvm/hyp/nvhe/trace.c            |   70 +-
 arch/arm64/kvm/hyp_trace.c                 |   15 +-
 arch/arm64/kvm/mmu.c                       |    6 +-
 arch/arm64/kvm/pkvm.c                      |  200 +++-
 arch/arm64/kvm/trace_pkvm.h                |   45 +
 23 files changed, 1931 insertions(+), 152 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: 8bd0e6812a1be71d50620f00445940ded0df897c

Signature · 1 line
2.56.0.rc1.315.gc6ed9934b7-goog