KVM: arm64: CCA: Prepare Realm memory management
v5 cover KVM: arm64: CCA: Prepare Realm memory management
This series adds the memory management plumbing for CCA, building on the basic RMM support [1]. Thanks to everyone who reviewed and tested the earlier versions.
The patches introduce internal Realm VM state, prepare Realm stage-2 setup and teardown, and provide RTT mapping and invalidation helpers. Realm memslots are restricted to the lower half of the IPA space because the protected and shared halves alias each other. The final patch connects the helpers to KVM's stage-2 MMU operations.
This is the memory management portion of the larger Realm host series. Realm descriptor creation, REC entry and exit, activation, and the new uAPI follow in the integration stack. This series alone does not enable Realm creation.
The integration branch [2] includes Realm creation, REC entry and exit, and the remaining host support on top of this series. The MM branch [3] contains just this series.
Changes since v4:
- Permit private-only guest_memfd backing by dropping the arm64 memslot check that previously required mmap-capable guest_memfd.
- Restrict Realm memslots to the protected IPA half and advertise Realm private-memory support in separate patches. Remove the early post-creation Realm memslot change restriction; faults validate the guest_memfd state when mappings are made.
- Unmap only private mappings during Realm teardown. Drop RTT folding during unmap; shared invalidation uses atomic splits and unmaps the enclosing block if a split cannot be allocated.
- Preserve completed RMI mappings on partial failure or interruption, and account for partially mapped writable backing memory.
- Skip stage-2 debugfs dumps for Realms because the host cannot read their RTTs.
Changes since v3:
- Keep pKVM-specific protected VM handling in the nVHE path and move stage-2 destruction dispatch before basic Realm infrastructure.
- Derive the Realm RTT start level from VTCR, matching KVM's minimum two-level stage-2 layout for narrow IPAs on 64K-page hosts.
- Split Realm guest_memfd and userspace-backed faults into dedicated paths, leaving the native fault handlers unchanged.
- Unmap earlier shared mappings when a later RMI map call fails.
- Clarify the early memslot validation rationale and the arm64-wide effect of in-place guest_memfd conversion.
Changes since v2:
- Move RMM mode reservation and RMI availability and feature checks to the integration stack.
- Require mmap-capable guest_memfd backing for all arm64 VMs and route all guest_memfd faults through gmem_abort().
- Store the Realm Descriptor physical address from initial Realm setup, and improve SRO cleanup and RTT teardown error handling.
- Use a local MMU cache for RTT invalidation, handle failed splits, and validate returned RTT levels.
- Bound protected RMI mapping calls to the encoded range and tighten RTT creation, retry, and error handling.
- Split common stage-2 destruction dispatch into its own patch before registering the Realm stage-2 MMU operations.
Changes since v1:
- Add a guest_memfd prerequisite patch that checks whether the backing file supports mmap when preparing an arm64 memslot.
- Combine the RMI availability and feature checks into one patch, and move Realm vCPU feature restrictions to the integration stack.
- Correct Realm IPA sizing and RTT teardown, including teardown before activation, RTT accounting, and handling of missing or folded RTTs.
- Rework RTT mapping to respect IPA and physical alignment, handle partial RMI progress, and retry interrupted operations.
- Preserve private mappings when a Realm guest_memfd memslot deletion is rejected, and tighten Realm fault handling and stage-2 destruction.
[1] https://lore.kernel.org/all/[email protected]/
[2] https://gitlab.arm.com/linux-arm/linux-cca
branch: cca-host-mm/v5/integration
[3] https://gitlab.arm.com/linux-arm/linux-cca
branch: cca-host-mm/v5/mm
Steven Price (7):
KVM: arm64: guest_memfd: Drop mmap requirement for memslots
KVM: arm64: Dispatch stage-2 destruction by VM flavor
KVM: arm64: CCA: Add basic infrastructure for creating a realm
KVM: arm64: CCA: Destroy RTTs
KVM: arm64: CCA: Add Realm RTT mapping and invalidation helpers
KVM: arm64: CCA: Advertise private memory support for Realms
KVM: arm64: CCA: Support runtime faulting of memory
Suzuki K Poulose (4):
KVM: arm64: CCA: Introduce Realms
KVM: arm64: Pass range filter to stage2_unmap_range
KVM: arm64: CCA: Restrict Realm memslots to the lower IPA half
KVM: arm64: CCA: Add Realm stage-2 MMU operations
arch/arm64/include/asm/kvm_emulate.h | 29 + arch/arm64/include/asm/kvm_host.h | 27 +- arch/arm64/include/asm/kvm_mmu.h | 9 +- arch/arm64/include/asm/kvm_rmi.h | 107 ++++ arch/arm64/kvm/Kconfig | 1 + arch/arm64/kvm/Makefile | 2 +- arch/arm64/kvm/arm.c | 13 +- arch/arm64/kvm/mmu.c | 313 +++++++++-- arch/arm64/kvm/pkvm.c | 2 +- arch/arm64/kvm/psci.c | 3 +- arch/arm64/kvm/ptdump.c | 4 +- arch/arm64/kvm/rmi.c | 808 +++++++++++++++++++++++++++ drivers/firmware/arm_rmm/rmi.c | 10 - include/linux/arm-rmi-cmds.h | 8 + 14 files changed, 1273 insertions(+), 63 deletions(-) create mode 100644 arch/arm64/include/asm/kvm_rmi.h create mode 100644 arch/arm64/kvm/rmi.c
Signature · 1 line
2.43.0
PATCH v5 1/11 KVM: arm64: guest_memfd: Drop mmap requirement for memslots
Arm64 rejects guest_memfd-backed memslots unless generic KVM marks them KVM_MEMSLOT_GMEM_ONLY. Without in-place conversion, that flag is set for mmap-capable guest_memfd files, reflecting the original arm64 requirement for userspace-mappable backing.
Realm VMs also need to bind private-only guest_memfd files, which do not support mmap. Their fault path obtains PFNs directly from guest_memfd through kvm_gmem_get_pfn(), without a userspace mapping. Remove the memslot restriction so these files can be used by Realms. Userspace remains responsible for supplying backing for shared IPAs.
---
Changes since v4:
- Remove the arm64 memslot check that requires mmap-capable guest_memfd; Realms can use private-only guest_memfd backing.
--- arch/arm64/kvm/mmu.c | 7 ------- 1 file changed, 7 deletions(-)
arch/arm64/kvm/mmu.c +0 -7
@@ -2838,13 +2838,6 @@ int kvm_arch_prepare_memory_region(struct kvm *kvm,if ((new->base_gfn + new->npages) > (kvm_phys_size (&kvm->arch.mmu) >>PAGE_SHIFT ))return -EFAULT ; -/* -* Only support guest_memfd backed memslots with mappable memory, since -* there aren't any CoCo VMs that support only private memory on arm64. -*/ -if (kvm_slot_has_gmem (new) && !kvm_memslot_is_gmem_only (new)) -return -EINVAL ; -/* guest_memfd is incompatible with MTE. */ if (kvm_slot_has_gmem (new) &&kvm_has_mte (kvm))return -EINVAL ;
Signature · 1 line
2.43.0
[email protected]
AI reviewRe: [PATCH v5 01/11] KVM: arm64: guest_memfd: Drop mmap requirement for memslots
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Low] KVM: arm64: inappropriate Fixes: 32e200bd6e44 tag on feature prerequisite patch --
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Low] KVM: arm64: inappropriate Fixes: 32e200bd6e44 tag on feature prerequisite patch
--
commit ed4ff3be33a7e9e639a23bf7c8763bdcd1447922
Author: Steven Price <[email protected]>
Quoted 13 lines
KVM: arm64:
guest_memfd: Drop mmap requirement for memslotsArm64 rejects guest_memfd-backed memslots unless generic KVM marks them
KVM_MEMSLOT_GMEM_ONLY. Without in-place conversion, that flag is set for mmap-capableguest_memfdfiles, reflecting the original arm64 requirement for userspace-mappable backing.Realm VMs also need to bind private-only
guest_memfdfiles, which do not support mmap. Their fault path obtains PFNs directly fromguest_memfdthroughkvm_gmem_get_pfn(), without a userspace mapping. Remove the memslot restriction so these files can be used by Realms. Userspace remains responsible for supplying backing for shared IPAs.
Quoted 3 lines
Suggested-by Ackerley Tng <[email protected]>Fixes 32e200bd6e44 ("KVM: arm64: Enable support for guest_memfd backed memory")Signed-off-by Steven Price <[email protected]>
[Severity: Low]
Should the Fixes: tag be dropped here?
32e200bd6e44 ("KVM: arm64: Enable support for guest_memfd backed memory") intentionally required kvm_memslot_is_gmem_only() because arm64 had no VMs supporting private-only memory. Removing that check for Realm VMs is feature enablement rather than a bug fix, and the Fixes: tag can cause this patch to be backported to stable trees without Realm support or in-place conversion, allowing normal VMs to bind non-mappable private-only guest_memfd files and map private pages into shared stage-2 tables via gmem_abort().
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Suzuki K Poulose
reviewerRe: [PATCH v5 01/11] KVM: arm64: guest_memfd: Drop mmap requirement for memslots
In reply to Steven PriceHi Steven
Quoted 7 lines · Steven Price
Arm64 rejects guest_memfd-backed memslots unless generic KVM marks them
KVM_MEMSLOT_GMEM_ONLY. Without in-place conversion, that flag is set for mmap-capableguest_memfdfiles, reflecting the original arm64 requirement for userspace-mappable backing.Realm VMs also need to bind private-only
guest_memfdfiles, which do not support mmap. Their fault path obtains PFNs directly from
We don't want to support private-only guest-memfd for Realms. When we enable inplace conversion support, all guest-memfd slots become implicitly GMEM_ONLY, implying if a VMM creates a guest-memfd slot with a userspace address for shared pages, KVM ignores it and VMM would be shooting on its foot.
This is true for all architectures with in-place conversion and in our case CCA is simply turning the switch on. So, please drop this patch.
Cheers
Suzuki
Quoted 34 lines
guest_memfdthroughkvm_gmem_get_pfn(), without a userspace mapping. Remove the memslot restriction so these files can be used by Realms. Userspace remains responsible for supplying backing for shared IPAs.Suggested-by Ackerley Tng <[email protected]>Fixes 32e200bd6e44 ("KVM: arm64: Enable support for guest_memfd backed memory")Signed-off-by Steven Price <[email protected]>---
Changes since v4:
- Remove the arm64 memslot check that requires mmap-capableguest_memfd; Realms can use private-onlyguest_memfdbacking.--- arch/arm64/kvm/mmu.c | 7 ------- 1 file changed, 7 deletions(-)arch/arm64/kvm/mmu.c +0
-7@@ -2838,13 +2838,6 @@ int kvm_arch_prepare_memory_region(struct kvm *kvm,if ((new->base_gfn + new->npages) > (kvm_phys_size (&kvm->arch.mmu) >>PAGE_SHIFT ))return -EFAULT ; -/* -* Only support guest_memfd backed memslots with mappable memory, since -* there aren't any CoCo VMs that support only private memory on arm64. -*/ -if (kvm_slot_has_gmem (new) && !kvm_memslot_is_gmem_only (new)) -return -EINVAL ; -/* guest_memfd is incompatible with MTE. */ if (kvm_slot_has_gmem (new) &&kvm_has_mte (kvm))return -EINVAL ;
Re: [PATCH v5 01/11] KVM: arm64: guest_memfd: Drop mmap requirement for memslots
In reply to Steven PriceQuoted 11 lines · Steven Price
Arm64 rejects guest_memfd-backed memslots unless generic KVM marks them
KVM_MEMSLOT_GMEM_ONLY. Without in-place conversion, that flag is set for mmap-capableguest_memfdfiles, reflecting the original arm64 requirement for userspace-mappable backing.Realm VMs also need to bind private-only
guest_memfdfiles, which do not support mmap. Their fault path obtains PFNs directly fromguest_memfdthroughkvm_gmem_get_pfn(), without a userspace mapping. Remove the memslot restriction so these files can be used by Realms. Userspace remains responsible for supplying backing for shared IPAs.
Quoted 3 lines
Suggested-by Ackerley Tng <[email protected]>Fixes 32e200bd6e44 ("KVM: arm64: Enable support for guest_memfd backed memory")Signed-off-by Steven Price <[email protected]>
Suzuki and I just had a chat, and I think I might have got the wrong end of the stick with this patch. I think actually this patch can be dropped altogether. It's not needed for CCA, and I'm now not convinced it's the right change. But if other's disagree then please chime in!
Thanks,
Steve
Quoted 27 lines
---
Changes since v4:
- Remove the arm64 memslot check that requires mmap-capableguest_memfd; Realms can use private-onlyguest_memfdbacking.--- arch/arm64/kvm/mmu.c | 7 ------- 1 file changed, 7 deletions(-)arch/arm64/kvm/mmu.c +0
-7@@ -2838,13 +2838,6 @@ int kvm_arch_prepare_memory_region(struct kvm *kvm,if ((new->base_gfn + new->npages) > (kvm_phys_size (&kvm->arch.mmu) >>PAGE_SHIFT ))return -EFAULT ; -/* -* Only support guest_memfd backed memslots with mappable memory, since -* there aren't any CoCo VMs that support only private memory on arm64. -*/ -if (kvm_slot_has_gmem (new) && !kvm_memslot_is_gmem_only (new)) -return -EINVAL ; -/* guest_memfd is incompatible with MTE. */ if (kvm_slot_has_gmem (new) &&kvm_has_mte (kvm))return -EINVAL ;
PATCH v5 2/11 KVM: arm64: CCA: Introduce Realms
In reply to Steven PriceFrom: Suzuki K Poulose <[email protected]>
Introduce the Realm VM flavor and per-VM state tracking. Realm and protected pKVM states are mutually exclusive, so share their storage in kvm_arch. Update pKVM callers of the protected VM helpers to check the VM flavor.
Realm VMs cannot be created yet; RMI initialization and the creation UABI follow in later patches.
---
Changes since v3:
- Keep protected-VM helper semantics specific to pKVM in nVHE, and use VM flavor checks for pKVM power-state handling.
Changes since v1:
- Move RMI initialization to the RMI checks patch and Realm vCPU feature restrictions to the follow-on integration series. Keep this patch focused on the Realm VM flavor and per-VM state.
- Limit pKVM power state handling to pKVM vCPUs.
--- arch/arm64/include/asm/kvm_emulate.h | 16 ++++++++ arch/arm64/include/asm/kvm_host.h | 17 +++++--- arch/arm64/include/asm/kvm_rmi.h | 59 ++++++++++++++++++++++++++++ arch/arm64/kvm/arm.c | 4 +- arch/arm64/kvm/mmu.c | 1 + arch/arm64/kvm/pkvm.c | 2 +- arch/arm64/kvm/psci.c | 3 +- 7 files changed, 94 insertions(+), 8 deletions(-) create mode 100644 arch/arm64/include/asm/kvm_rmi.h
arch/arm64/include/asm/kvm_emulate.h +16 -0
@@ -831,4 +831,20 @@ static inline void kvm_reset_vcpu_psci(struct kvm_vcpu *vcpu, vcpu_set_reg(vcpu, 0, reset_state->r0); } +static inline enum realm_state kvm_realm_state (struct kvm *kvm) +{ +return READ_ONCE (kvm->arch.realm.state); +} + +static inline void kvm_set_realm_state (struct kvm *kvm, +enum realm_state new_state) +{ +WRITE_ONCE (kvm->arch.realm.state, new_state); +} + +static inline bool kvm_realm_is_created (struct kvm *kvm) +{ +return kvm_vm_is_realm (kvm) &&kvm_realm_state (kvm) !=REALM_STATE_NONE ; +} +#endif /* __ARM64_KVM_EMULATE_H__ */
arch/arm64/include/asm/kvm_host.h +12 -5
@@ -29,6 +29,7 @@#include <asm/kvm.h> #include <asm/kvm_asm.h> #include <asm/kvm_hcall.h> +#include <asm/kvm_rmi.h> #include <asm/vncr_mapping.h> #define __KVM_HAVE_ARCH_INTC_INITIALIZED
@@ -408,6 +409,7 @@ enum kvm_arm_vm_flavor {VM_PKVM ,/* Normal guests on pKVM */ MARKER (__VM_PROTECTED),VM_PROTECTED_PKVM ,/* Protected VM */ +VM_REALM ,/* CCA */ VM_FLAVOR_MAX };
@@ -501,11 +503,14 @@ struct kvm_arch {/* Count the number of VNCR_EL2 TLBs */ atomic_t vncr_tlb_count; -/* -* For an untrusted host VM, 'pkvm.handle' is used to lookup -* the associated pKVM instance in the hypervisor. -*/ -struct kvm_protected_vm pkvm; +union { +/* +* For an untrusted host VM, 'pkvm.handle' is used to lookup +* the associated pKVM instance in the hypervisor. +*/ +struct kvm_protected_vm pkvm; +struct realm realm; + };#ifdef CONFIG_PTDUMP_STAGE2_DEBUGFS /* Nested virtualization info */
@@ -1596,6 +1601,8 @@ struct kvm *kvm_arch_alloc_vm(void);#endif /* __KVM_NVHE_HYPERVISOR__ */ +#define kvm_vm_is_realm (kvm) ((kvm)->arch.vm_flavor == VM_REALM) +#define kvm_has_mte (kvm) \ (system_supports_mte() && \ test_bit(KVM_ARCH_FLAG_MTE_ENABLED, &(kvm)->arch.flags))
arch/arm64/include/asm/kvm_rmi.h new file +59 -0
59 lines not on this page · raw message
arch/arm64/kvm/arm.c +3 -1
@@ -877,7 +877,8 @@ static void __kvm_arm_vcpu_power_off(struct kvm_vcpu *vcpu, u32 mp_state)/* The guest's own CPU_OFF: EL2 has already powered the vCPU off. */ void kvm_arm_vcpu_power_off (struct kvm_vcpu *vcpu) { -u32 mp_state =vcpu_is_protected (vcpu) ?KVM_MP_STATE_UNINITIALIZED :KVM_MP_STATE_STOPPED ; +u32 mp_state = vcpu->kvm->arch.vm_flavor ==VM_PROTECTED_PKVM ? +KVM_MP_STATE_UNINITIALIZED :KVM_MP_STATE_STOPPED ;spin_lock (&vcpu->arch.mp_state_lock);__kvm_arm_vcpu_power_off (vcpu, mp_state);
@@ -2312,6 +2313,7 @@ static const struct kvm_vcpu_ops *arm64_vcpu_ops[] = {KVM_VCPU_OPS (VM_VHE , vhe_vcpu_ops),KVM_VCPU_OPS (VM_PKVM , pkvm_vcpu_ops), KVM_VCPU_OPS(VM_PROTECTED_PKVM, pkvm_vcpu_ops), + [VM_REALM] = NULL, };static void kvm_init_vcpu_ops (struct kvm_vcpu *vcpu)
arch/arm64/kvm/mmu.c +1 -0
@@ -3158,6 +3158,7 @@ static const struct kvm_vm_s2_ops *arm64_vm_s2_ops[] = {KVM_VM_S2_OPS (VM_NVHE , kvm_default_vm_s2_ops),KVM_VM_S2_OPS (VM_PKVM , pkvm_vm_s2_ops), KVM_VM_S2_OPS(VM_PROTECTED_PKVM, protected_pkvm_vm_s2_ops), + [VM_REALM] = NULL, };static int kvm_vm_init_vm_s2_ops (struct kvm *kvm)
arch/arm64/kvm/pkvm.c +1 -1
@@ -192,7 +192,7 @@ static int __pkvm_create_hyp_vcpu(struct kvm_vcpu *vcpu)* Mirror EL2's seeding of power_state from mp_state. The hyp vCPU is * published, so take mp_state_lock against kvm_psci_vcpu_on(). */ -if (vcpu_is_protected (vcpu)) { +if (vcpu->kvm->arch.vm_flavor ==VM_PROTECTED_PKVM ) {spin_lock (&vcpu->arch.mp_state_lock);if (kvm_arm_vcpu_stopped (vcpu))WRITE_ONCE (vcpu->arch.mp_state.mp_state,KVM_MP_STATE_UNINITIALIZED );
arch/arm64/kvm/psci.c +2 -1
@@ -68,7 +68,8 @@ static unsigned long kvm_psci_vcpu_on(struct kvm_vcpu *source_vcpu)* STOPPED before that is the VMM's pause. */ if (!kvm_arm_vcpu_stopped (vcpu) || - (vcpu_is_protected (vcpu) && !kvm_pkvm_vcpu_is_powered_off (vcpu))) { + (vcpu->kvm->arch.vm_flavor ==VM_PROTECTED_PKVM && + !kvm_pkvm_vcpu_is_powered_off (vcpu))) {if (kvm_psci_version (source_vcpu) !=KVM_ARM_PSCI_0_1 ) ret =PSCI_RET_ALREADY_ON ;else
Signature · 1 line
2.43.0
PATCH v5 3/11 KVM: arm64: Pass range filter to stage2_unmap_range
In reply to Steven PriceFrom: Suzuki K Poulose <[email protected]>
With the introduction of private memory support, we need to handle invalidation requests with filters for a given type. Pass the range filter down to the stage-2 unmap operation.
--- arch/arm64/include/asm/kvm_host.h | 4 +++- arch/arm64/kvm/mmu.c | 31 +++++++++++++++++++++++-------- 2 files changed, 26 insertions(+), 9 deletions(-)
arch/arm64/include/asm/kvm_host.h +3 -1
@@ -233,6 +233,7 @@ struct kvm_vcpu_ops { };struct kvm_gfn_range ; +enum kvm_gfn_range_filter ;struct kvm_s2_fault_desc ;struct kvm_vm_s2_ops {
@@ -243,7 +244,8 @@ struct kvm_vm_s2_ops { u64 nr_pages);void (*vm_stage2_unmap_range)(struct kvm_s2_mmu *mmu,phys_addr_t start,u64 size, -bool may_block); +bool may_block, +enum kvm_gfn_range_filter filter);int (*vm_mem_abort)(const struct kvm_s2_fault_desc *s2fd); };
arch/arm64/kvm/mmu.c +23 -8
@@ -324,7 +324,8 @@ static int kvm_pgtable_stage2_unmap_tracked(struct kvm_pgtable *pgt,static void kvm_vm_stage2_unmap_range (struct kvm_s2_mmu *mmu,phys_addr_t start, -u64 size,bool may_block) +u64 size,bool may_block, +enum kvm_gfn_range_filter filter) {struct kvm *kvm =kvm_s2_mmu_to_kvm (mmu);int (*fn)(struct kvm_pgtable *,u64 ,u64 );
@@ -339,14 +340,16 @@ static void kvm_vm_stage2_unmap_range(struct kvm_s2_mmu *mmu,static void pkvm_stage2_unmap_range (struct kvm_s2_mmu *mmu,phys_addr_t start, -u64 size,bool may_block) +u64 size,bool may_block, +enum kvm_gfn_range_filter filter) {WARN_ON (stage2_apply_range (mmu, start, start + size, pkvm_pgtable_stage2_unmap, may_block)); }static void no_stage2_unmap_range (struct kvm_s2_mmu *mmu, -phys_addr_t start,u64 size,bool may_block) +phys_addr_t start,u64 size,bool may_block, +enum kvm_gfn_range_filter filter) { }
@@ -388,25 +391,36 @@ static int kvm_pgtable_stage2_unmap_tracked(struct kvm_pgtable *pgt, u64 addr, u }/** -* kvm_stage2_unmap_range -- Clear stage2 page table entries to unmap a range +* kvm_stage2_unmap_range_filter -- Clear stage2 page table entries to unmap a +* range * @mmu: The KVM stage-2 MMU pointer * @start: The intermediate physical base address of the range to unmap * @size: The size of the area to unmap * @may_block: Whether or not we are permitted to block +* @filter: Filter for the ipa range (private and/or shared) * * Clear a range of stage-2 mappings, lowering the various ref-counts. Must * be called while holding mmu_lock otherwise another faulting VCPU may * come in and mess with things behind our backs. */ -void kvm_stage2_unmap_range (struct kvm_s2_mmu *mmu,phys_addr_t start, -u64 size,bool may_block) +static void kvm_stage2_unmap_range_filter (struct kvm_s2_mmu *mmu,phys_addr_t start, +u64 size,bool may_block, +enum kvm_gfn_range_filter filter) {struct kvm *kvm =kvm_s2_mmu_to_kvm (mmu);lockdep_assert_held_write (&kvm->mmu_lock);WARN_ON (size & ~PAGE_MASK ); - kvm->arch.vm_s2_ops->vm_stage2_unmap_range (mmu, start, size, may_block); + kvm->arch.vm_s2_ops->vm_stage2_unmap_range (mmu, start, size, + may_block, filter); +} + +void kvm_stage2_unmap_range (struct kvm_s2_mmu *mmu,phys_addr_t start, +u64 size,bool may_block) +{ +kvm_stage2_unmap_range_filter (mmu, start, size, may_block, +KVM_FILTER_PRIVATE |KVM_FILTER_SHARED ); }void kvm_stage2_flush_range (struct kvm_s2_mmu *mmu,phys_addr_t addr,phys_addr_t end)
@@ -2597,7 +2611,8 @@ bool kvm_unmap_gfn_range(struct kvm *kvm, struct kvm_gfn_range *range)if (!kvm->arch.mmu.pgt)return false; -kvm_stage2_unmap_range (&kvm->arch.mmu, gpa, size, may_block); +kvm_stage2_unmap_range_filter (&kvm->arch.mmu, gpa, size, may_block, + range->attr_filter);kvm_nested_unmap_cipa_range (kvm, gpa, size, may_block);return false;
Signature · 1 line
2.43.0
PATCH v5 4/11 KVM: arm64: Dispatch stage-2 destruction by VM flavor
In reply to Steven PriceSelect the stage-2 destruction functions through the VM's stage-2 operations. This keeps the native and pKVM paths explicit and lets a Realm provide its own destruction callback when Realm stage-2 operations are registered.
---
Changes since v3:
- Move before basic Realm infrastructure and declare the page-table type for the new callback.
Changes since v2:
- New patch in v3; split stage-2 destruction dispatch out of the Realm stage-2 MMU operations patch.
--- arch/arm64/include/asm/kvm_host.h | 2 ++ arch/arm64/kvm/mmu.c | 30 +++++++++++++++++++++++------- 2 files changed, 25 insertions(+), 7 deletions(-)
arch/arm64/include/asm/kvm_host.h +2 -0
@@ -234,6 +234,7 @@ struct kvm_vcpu_ops {struct kvm_gfn_range ;enum kvm_gfn_range_filter ; +struct kvm_pgtable ;struct kvm_s2_fault_desc ;struct kvm_vm_s2_ops {
@@ -242,6 +243,7 @@ struct kvm_vm_s2_ops {int (*vm_flush_remote_tlbs)(struct kvm *kvm);int (*vm_flush_remote_tlbs_range)(struct kvm *kvm,gfn_t gfn,u64 nr_pages); +void (*vm_stage2_destroy)(struct kvm_pgtable *pgt); void (*vm_stage2_unmap_range)(struct kvm_s2_mmu *mmu, phys_addr_t start, u64 size, bool may_block,
arch/arm64/kvm/mmu.c +23 -7
@@ -1018,26 +1018,39 @@ static int kvm_init_ipa_range(struct kvm_s2_mmu *mmu, unsigned long type)* cond_resched() periodically to prevent hogging the CPU for a long time * and schedule something else, if required. */ -static void stage2_destroy_range (struct kvm_pgtable *pgt,phys_addr_t addr, -phys_addr_t end) +static void stage2_destroy_range (struct kvm_pgtable *pgt, +void (*destroy_range)(struct kvm_pgtable *pgt, +u64 addr,u64 size)) { +u64 addr =0 ; +u64 end =BIT (VTCR_EL2_IPA (pgt->mmu->vtcr));u64 next;do { next =stage2_range_addr_end (addr, end); -KVM_PGT_FN (kvm_pgtable_stage2_destroy_range)(pgt, addr, - next - addr); +destroy_range (pgt, addr, next - addr);if (next != end)cond_resched (); }while (addr = next, addr != end); } +static void kvm_vm_stage2_destroy (struct kvm_pgtable *pgt) +{ +stage2_destroy_range (pgt, kvm_pgtable_stage2_destroy_range); +kvm_pgtable_stage2_destroy_pgd (pgt); +} + +static void pkvm_stage2_destroy (struct kvm_pgtable *pgt) +{ +stage2_destroy_range (pgt, pkvm_pgtable_stage2_destroy_range); +pkvm_pgtable_stage2_destroy_pgd (pgt); +} +static void kvm_stage2_destroy (struct kvm_pgtable *pgt) { -unsigned int ia_bits =VTCR_EL2_IPA (pgt->mmu->vtcr); +struct kvm *kvm =kvm_s2_mmu_to_kvm (pgt->mmu); -stage2_destroy_range (pgt,0 ,BIT (ia_bits)); -KVM_PGT_FN (kvm_pgtable_stage2_destroy_pgd)(pgt); + kvm->arch.vm_s2_ops->vm_stage2_destroy (pgt); }/**
@@ -3139,6 +3152,7 @@ long kvm_arch_vcpu_pre_fault_memory(struct kvm_vcpu *vcpu, }static const struct kvm_vm_s2_ops protected_pkvm_vm_s2_ops = { + .vm_stage2_destroy = pkvm_stage2_destroy, .vm_flush_remote_tlbs = pkvm_flush_remote_tlbs, .vm_flush_remote_tlbs_range = pkvm_flush_remote_tlbs_range, .vm_age_gfn = no_age_gfn,
@@ -3148,6 +3162,7 @@ static const struct kvm_vm_s2_ops protected_pkvm_vm_s2_ops = { };static const struct kvm_vm_s2_ops pkvm_vm_s2_ops = { + .vm_stage2_destroy = pkvm_stage2_destroy, .vm_flush_remote_tlbs = pkvm_flush_remote_tlbs, .vm_flush_remote_tlbs_range = pkvm_flush_remote_tlbs_range, .vm_age_gfn = pkvm_age_gfn,
@@ -3157,6 +3172,7 @@ static const struct kvm_vm_s2_ops pkvm_vm_s2_ops = { };static const struct kvm_vm_s2_ops kvm_default_vm_s2_ops = { + .vm_stage2_destroy = kvm_vm_stage2_destroy, .vm_flush_remote_tlbs = kvm_vm_flush_remote_tlbs, .vm_flush_remote_tlbs_range = kvm_vm_flush_remote_tlbs_range, .vm_age_gfn = kvm_vm_age_gfn,
Signature · 1 line
2.43.0
PATCH v5 5/11 KVM: arm64: CCA: Add basic infrastructure for creating a realm
In reply to Steven PriceIntroduce the skeleton functions for creating and destroying a realm. The requested IPA size is checked against both the KVM and RMM limits.
The actual work of constructing the realm will be added in future patches.
---
Changes since v4:
- Simplify RD teardown now that a created Realm always has an RD.
Changes since v3:
- Use the preceding destruction dispatch instead of a temporary Realm-specific check in kvm_stage2_destroy().
Changes since v2:
- Move ARM_RMM_RMI selection and rmi.o build registration here. Store the RD physical address from the outset and free SRO state on Realm teardown errors.
Changes since v1:
- Cap the Realm IPA size at the smaller KVM and RMM limit, and record the selected IPA size when initializing Realm state.
--- arch/arm64/include/asm/kvm_rmi.h | 23 +++++++++++++ arch/arm64/kvm/Kconfig | 1 + arch/arm64/kvm/Makefile | 2 +- arch/arm64/kvm/arm.c | 9 +++++ arch/arm64/kvm/mmu.c | 7 +++- arch/arm64/kvm/rmi.c | 58 ++++++++++++++++++++++++++++++++ 6 files changed, 98 insertions(+), 2 deletions(-) create mode 100644 arch/arm64/kvm/rmi.c
arch/arm64/include/asm/kvm_rmi.h +23 -0
@@ -6,6 +6,9 @@#ifndef __ASM_KVM_RMI_H#define __ASM_KVM_RMI_H +#include <linux/arm-smccc-rmi.h> +#include <linux/kvm.h> +/** * enum realm_state - State of a Realm *
@@ -50,10 +53,30 @@ enum realm_state {/** * struct realm - Additional per VM data for a Realm * +* @rd_phys: Physical address of the RMM-managed Realm Descriptor (RD) granule +* @sro: Preallocated SRO state context for Realm MMU operations * @state: The lifetime state machine for the realm +* @ia_bits: Number of valid Input Address bits in the IPA +* @start_level: Starting level of the Realm Translation Table */ struct realm { +phys_addr_t rd_phys; +/* +* Reused by RTT map/unmap SRO commands. Those commands are only +* issued from Realm stage-2 map/unmap paths while kvm->mmu_lock is +* held for write, including Realm fault handling where +* kvm_fault_lock() takes the write side, so concurrent use is +* serialized. +*/ +struct rmi_sro_state *sro;enum realm_state state; +unsigned int ia_bits; +int start_level; }; +u32 kvm_rmm_ipa_limit (void ); + +int kvm_init_realm (struct kvm *kvm); +void kvm_destroy_realm (struct kvm *kvm); +#endif /* __ASM_KVM_RMI_H */
arch/arm64/kvm/Kconfig +1 -0
@@ -34,6 +34,7 @@ menuconfig KVM select GUEST_PERF_EVENTS if PERF_EVENTS select KVM_GUEST_MEMFD select KVM_GENERIC_PRE_FAULT_MEMORY + select ARM_RMM_RMI help Support hosting virtualized guest machines.
arch/arm64/kvm/Makefile +1 -1
@@ -16,7 +16,7 @@ CFLAGS_handle_exit.o += -Wno-override-init kvm-y += arm.o mmu.o mmio.o psci.o hypercalls.o pvtime.o \ inject_fault.o va_layout.o handle_exit.o config.o \ guest.o debug.o reset.o sys_regs.o stacktrace.o \ - vgic-sys-reg-v3.o fpsimd.o pkvm.o \ + vgic-sys-reg-v3.o fpsimd.o pkvm.o rmi.o \ arch_timer.o trng.o vmid.o emulate-nested.o nested.o at.o \ vgic/vgic.o vgic/vgic-init.o \ vgic/vgic-irqfd.o vgic/vgic-v2.o \
arch/arm64/kvm/arm.c +9 -0
@@ -300,6 +300,13 @@ int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)bitmap_zero (kvm->arch.vcpu_features,KVM_VCPU_MAX_FEATURES ); +/* Initialise the realm bits after the generic bits are enabled */ +if (kvm_vm_is_realm (kvm)) { + ret =kvm_init_realm (kvm); +if (ret) + goto err_uninit_mmu; + } +return 0 ; err_uninit_mmu:
@@ -364,6 +371,8 @@ void kvm_arch_destroy_vm(struct kvm *kvm)kvm_destroy_nested (kvm);kvm_arm_teardown_hypercalls (kvm); +if (kvm_vm_is_realm (kvm)) +kvm_destroy_realm (kvm); }static bool kvm_has_full_ptr_auth (void )
arch/arm64/kvm/mmu.c +6 -1
@@ -981,10 +981,14 @@ static struct kvm_pgtable_mm_ops kvm_s2_mm_ops = {static int kvm_init_ipa_range (struct kvm_s2_mmu *mmu,unsigned long type) { +struct kvm *kvm =kvm_s2_mmu_to_kvm (mmu);u32 kvm_ipa_limit =get_kvm_ipa_limit ();u64 mmfr0, mmfr1;u32 phys_shift; +if (kvm_vm_is_realm (kvm)) + kvm_ipa_limit =min (kvm_ipa_limit,kvm_rmm_ipa_limit ()); + phys_shift =KVM_VM_TYPE_ARM_IPA_SIZE (type);if (is_protected_kvm_enabled ()) { phys_shift = kvm_ipa_limit;
@@ -1097,6 +1101,8 @@ int kvm_init_stage2_mmu(struct kvm *kvm, struct kvm_s2_mmu *mmu, unsigned long treturn -EINVAL ; } + mmu->arch = &kvm->arch; + err =kvm_init_ipa_range (mmu, type);if (err)return err;
@@ -1105,7 +1111,6 @@ int kvm_init_stage2_mmu(struct kvm *kvm, struct kvm_s2_mmu *mmu, unsigned long tif (!pgt)return -ENOMEM ; - mmu->arch = &kvm->arch; err =KVM_PGT_FN (kvm_pgtable_stage2_init)(pgt, mmu, &kvm_s2_mm_ops);if (err) goto out_free_pgtable;
arch/arm64/kvm/rmi.c new file +58 -0
58 lines not on this page · raw message
Signature · 1 line
2.43.0
Re: [PATCH v5 05/11] KVM: arm64: CCA: Add basic infrastructure for creating a realm
Quoted 7 lines · Steven Price
Introduce the skeleton functions for creating and destroying a realm. The requested IPA size is checked against both the KVM and RMM limits.
The actual work of constructing the realm will be added in future patches.
Signed-off-by Steven Price <[email protected]>
PATCH v5 6/11 KVM: arm64: CCA: Destroy RTTs
In reply to Steven PriceThe RMM owns the stage 2 page tables for a realm, and KVM must request that the RMM creates/destroys entries as necessary. The physical pages to store the page tables are delegated to the realm as required, and can be undelegated when no longer used.
Creating new RTTs is the easy part, destroying them is a little more tricky. The result of realm_rtt_destroy() can be used to effectively walk the tree and destroy the entries (undelegating pages that were given to the realm).
REALM_DESTROY requires no RECs, live mappings, or non-root RTTs. Unmap the protected IPA half, then use REALM_TERMINATE to move the Realm to ZOMBIE. Destroy non-root RTTs before destroying the RD. Unprotected entries are not live and need no explicit unmap. Undelegate the root RTTs only after the RD has gone away.
---
Changes since v4:
- Clarify Realm liveness and drop the stale start-level explanation.
- Fold the RTT destroy wrapper, use a scoped mutex guard, and unmap only private mappings during teardown.
Changes since v3:
- Match VTCR's minimum of two stage-2 levels for Realm RTTs.
- Use explicit config_lock cleanup in Realm destruction.
Changes since v2:
- Use the actual stage-2 start level and correct the RTT skip granularity. Free SRO state on final teardown errors.
Changes since v1:
- Tear down created Realms in both NEW and ACTIVE states. Fix the RTT start level, page accounting on failed undelegation, and handling of RTT granules returned by RMI.
--- arch/arm64/include/asm/kvm_mmu.h | 5 + arch/arm64/include/asm/kvm_rmi.h | 3 + arch/arm64/kvm/mmu.c | 23 ++++- arch/arm64/kvm/rmi.c | 171 +++++++++++++++++++++++++++++++ 4 files changed, 198 insertions(+), 4 deletions(-)
arch/arm64/include/asm/kvm_mmu.h +5 -0
@@ -182,6 +182,11 @@ int create_hyp_exec_mappings(phys_addr_t phys_addr, size_t size,int create_hyp_stack (phys_addr_t phys_addr,unsigned long *haddr);void __init free_hyp_pgds (void ); +enum kvm_gfn_range_filter ; + +void kvm_stage2_unmap_range_filter (struct kvm_s2_mmu *mmu,phys_addr_t start, +u64 size,bool may_block, +enum kvm_gfn_range_filter filter);void kvm_stage2_unmap_range (struct kvm_s2_mmu *mmu,phys_addr_t start,u64 size,bool may_block);void kvm_stage2_flush_range (struct kvm_s2_mmu *mmu,phys_addr_t addr,phys_addr_t end);
arch/arm64/include/asm/kvm_rmi.h +3 -0
@@ -58,6 +58,7 @@ enum realm_state {* @state: The lifetime state machine for the realm * @ia_bits: Number of valid Input Address bits in the IPA * @start_level: Starting level of the Realm Translation Table +* @stage2_unmapped: The Realm's protected stage-2 mappings have been removed */ struct realm {phys_addr_t rd_phys;
@@ -72,11 +73,13 @@ struct realm {enum realm_state state;unsigned int ia_bits;int start_level; + bool stage2_unmapped; };u32 kvm_rmm_ipa_limit (void );int kvm_init_realm (struct kvm *kvm);void kvm_destroy_realm (struct kvm *kvm); +void kvm_realm_unmap_stage2_all (struct kvm *kvm);#endif /* __ASM_KVM_RMI_H */
arch/arm64/kvm/mmu.c +19 -4
@@ -403,9 +403,9 @@ static int kvm_pgtable_stage2_unmap_tracked(struct kvm_pgtable *pgt, u64 addr, u* be called while holding mmu_lock otherwise another faulting VCPU may * come in and mess with things behind our backs. */ -static void kvm_stage2_unmap_range_filter (struct kvm_s2_mmu *mmu,phys_addr_t start, -u64 size,bool may_block, -enum kvm_gfn_range_filter filter) +void kvm_stage2_unmap_range_filter (struct kvm_s2_mmu *mmu,phys_addr_t start, +u64 size,bool may_block, +enum kvm_gfn_range_filter filter) {struct kvm *kvm =kvm_s2_mmu_to_kvm (mmu);
@@ -1149,9 +1149,24 @@ int kvm_init_stage2_mmu(struct kvm *kvm, struct kvm_s2_mmu *mmu, unsigned long treturn err; } +static void kvm_realm_uninit_stage2 (struct kvm_s2_mmu *mmu) +{ +struct kvm *kvm =kvm_s2_mmu_to_kvm (mmu); + +guard (mutex)(&kvm->arch.config_lock); +if (kvm_realm_state (kvm) ==REALM_STATE_NEW || +kvm_realm_state (kvm) ==REALM_STATE_ACTIVE ) { +kvm_set_realm_state (kvm,REALM_STATE_DYING ); +kvm_realm_unmap_stage2_all (kvm); + } +} +void kvm_uninit_stage2_mmu (struct kvm *kvm) { -kvm_free_stage2_pgd (&kvm->arch.mmu); +if (kvm_realm_is_created (kvm)) +kvm_realm_uninit_stage2 (&kvm->arch.mmu); +else +kvm_free_stage2_pgd (&kvm->arch.mmu);kvm_mmu_free_memory_cache (&kvm->arch.mmu.split_page_cache); }
arch/arm64/kvm/rmi.c +171 -0
196 lines not on this page · raw message
Signature · 1 line
2.43.0
Suzuki K Poulose
reviewerRe: [PATCH v5 06/11] KVM: arm64: CCA: Destroy RTTs
Quoted 48 lines · Steven Price
The RMM owns the stage 2 page tables for a realm, and KVM must request that the RMM creates/destroys entries as necessary. The physical pages to store the page tables are delegated to the realm as required, and can be undelegated when no longer used.
Creating new RTTs is the easy part, destroying them is a little more tricky. The result of
realm_rtt_destroy()can be used to effectively walk the tree and destroy the entries (undelegating pages that were given to the realm).
REALM_DESTROYrequires no RECs, live mappings, or non-root RTTs. Unmap the protected IPA half, then useREALM_TERMINATEto move the Realm to ZOMBIE. Destroy non-root RTTs before destroying the RD. Unprotected entries are not live and need no explicit unmap. Undelegate the root RTTs only after the RD has gone away.Signed-off-by Steven Price <[email protected]>---
Changes since v4:
- Clarify Realm liveness and drop the stale start-level explanation.
- Fold the RTT destroy wrapper, use a scoped mutex guard, and unmap only private mappings during teardown.Changes since v3:
- Match VTCR's minimum of two stage-2 levels for Realm RTTs.
- Use explicitconfig_lockcleanup in Realm destruction.Changes since v2:
- Use the actual stage-2 start level and correct the RTT skip granularity. Free SRO state on final teardown errors.Changes since v1:
- Tear down created Realms in both NEW and ACTIVE states. Fix the RTT start level, page accounting on failed undelegation, and handling of RTT granules returned by RMI.--- arch/arm64/include/asm/kvm_mmu.h | 5 + arch/arm64/include/asm/kvm_rmi.h | 3 + arch/arm64/kvm/mmu.c | 23 ++++- arch/arm64/kvm/rmi.c | 171 +++++++++++++++++++++++++++++++ 4 files changed, 198 insertions(+), 4 deletions(-)arch/arm64/include/asm/kvm_mmu.h +0
-0
arch/arm64/include/asm/kvm_mmu.h int create_hyp_exec_mappings(phys_addr_t phys_addr, size_t size, · line 189
Quoted 8 lines
arch/arm64/include/asm/kvm_mmu.h +5
-0@@ -182,6 +182,11 @@ int create_hyp_exec_mappings(phys_addr_t phys_addr, size_t size,int create_hyp_stack (phys_addr_t phys_addr,unsigned long *haddr);void __init free_hyp_pgds (void ); +enum kvm_gfn_range_filter ; + +void kvm_stage2_unmap_range_filter (struct kvm_s2_mmu *mmu,phys_addr_t start, +u64 size,bool may_block, +enum kvm_gfn_range_filter filter);
If we bring the Patch 7 before this one, we could avoid this and directly call kvm_realm_unmap_stage2_range_filter() or even realm_unmap_private_range()
Otherwise, looks good to me
Suzuki
Quoted 34 lines
arch/arm64/include/asm/kvm_mmu.h +0
-0void kvm_stage2_unmap_range (struct kvm_s2_mmu *mmu,phys_addr_t start,u64 size,bool may_block);void kvm_stage2_flush_range (struct kvm_s2_mmu *mmu,phys_addr_t addr,phys_addr_t end);arch/arm64/include/asm/kvm_rmi.h +3
-0@@ -58,6 +58,7 @@ enum realm_state {* @state: The lifetime state machine for the realm * @ia_bits: Number of valid Input Address bits in the IPA * @start_level: Starting level of the Realm Translation Table +* @stage2_unmapped: The Realm's protected stage-2 mappings have been removed */ struct realm {phys_addr_t rd_phys;@@ -72,11 +73,13 @@ struct realm {enum realm_state state;unsigned int ia_bits;int start_level; + bool stage2_unmapped; };u32 kvm_rmm_ipa_limit (void );int kvm_init_realm (struct kvm *kvm);void kvm_destroy_realm (struct kvm *kvm); +void kvm_realm_unmap_stage2_all (struct kvm *kvm);#endif /* __ASM_KVM_RMI_H */ arch/arm64/kvm/mmu.c +0
-0
arch/arm64/kvm/mmu.c static int kvm_pgtable_stage2_unmap_tracked(struct kvm_pgtable *pgt, u64 addr, u · line 409
Quoted 10 lines
arch/arm64/kvm/mmu.c +3
-3@@ -403,9 +403,9 @@ static int kvm_pgtable_stage2_unmap_tracked(struct kvm_pgtable *pgt, u64 addr, u* be called while holding mmu_lock otherwise another faulting VCPU may * come in and mess with things behind our backs. */ -static void kvm_stage2_unmap_range_filter (struct kvm_s2_mmu *mmu,phys_addr_t start, -u64 size,bool may_block, -enum kvm_gfn_range_filter filter) +void kvm_stage2_unmap_range_filter (struct kvm_s2_mmu *mmu,phys_addr_t start, +u64 size,bool may_block, +enum kvm_gfn_range_filter filter) {
I
Quoted 230 lines
arch/arm64/kvm/mmu.c +16
-1struct kvm *kvm =kvm_s2_mmu_to_kvm (mmu);@@ -1149,9 +1149,24 @@ int kvm_init_stage2_mmu(struct kvm *kvm, struct kvm_s2_mmu *mmu, unsigned long treturn err; } +static void kvm_realm_uninit_stage2 (struct kvm_s2_mmu *mmu) +{ +struct kvm *kvm =kvm_s2_mmu_to_kvm (mmu); + +guard (mutex)(&kvm->arch.config_lock); +if (kvm_realm_state (kvm) ==REALM_STATE_NEW || +kvm_realm_state (kvm) ==REALM_STATE_ACTIVE ) { +kvm_set_realm_state (kvm,REALM_STATE_DYING ); +kvm_realm_unmap_stage2_all (kvm); + } +} +void kvm_uninit_stage2_mmu (struct kvm *kvm) { -kvm_free_stage2_pgd (&kvm->arch.mmu); +if (kvm_realm_is_created (kvm)) +kvm_realm_uninit_stage2 (&kvm->arch.mmu); +else +kvm_free_stage2_pgd (&kvm->arch.mmu);kvm_mmu_free_memory_cache (&kvm->arch.mmu.split_page_cache); }arch/arm64/kvm/rmi.c +171
-0@@ -16,19 +16,187 @@ u32 kvm_rmm_ipa_limit(void)return u64_get_bits (rmi_feat_reg (0 ),RMI_FEATURE_REGISTER_0_S2SZ ); } +static void free_rtt (phys_addr_t phys) +{ +kvm_account_pgtable_pages (phys_to_virt (phys),-1 ); +if (rmi_free_delegated_page (phys)) +kvm_account_pgtable_pages (phys_to_virt (phys),1 ); +} + +/* +* realm_rtt_destroy - Destroy an RTT at @level for @addr. +* +* Returns - Result of the RMI_RTT_DESTROY call, and: +* @rtt_granule: RTT granule, if the RTT was destroyed. +* @next_addr: IPA corresponding to the next possible valid entry we +* can target +*/ +static long realm_rtt_destroy (struct realm *realm,unsigned long addr, +int level,phys_addr_t *rtt_granule, +unsigned long *next_addr) +{ +unsigned long out_rtt; +long ret; + + ret =rmi_rtt_destroy (realm->rd_phys, addr, level, + &out_rtt, next_addr); + +if (ret ==RMI_SUCCESS ) + *rtt_granule = out_rtt; + +return ret; +} + +static int realm_destroy_rtt_level (struct realm *realm,int level, +unsigned long start,unsigned long end) +{ +ssize_t map_size; +unsigned long addr, next_addr; + +if (WARN_ON (level >KVM_PGTABLE_LAST_LEVEL )) +return -EINVAL ; + + map_size =kvm_granule_size (level -1 ); +if (WARN_ON (!IS_ALIGNED (start, map_size))) +return -EINVAL ; + +for (addr = start; addr < end; addr = next_addr) { +phys_addr_t rtt_granule; +long ret; +unsigned long align_addr =ALIGN (addr, map_size); + + next_addr =ALIGN (addr +1 , map_size); + +if (next_addr > end || align_addr != addr) { +/* +* The target range is smaller than what this level +* covers, recurse deeper. +*/ + ret =realm_destroy_rtt_level (realm, + level +1 , + addr, +min (next_addr, end)); +if (ret) +return ret; +continue ; + } + + ret =realm_rtt_destroy (realm, addr, level, + &rtt_granule, &next_addr); +if (ret <0 ) +return ret; + +switch (RMI_RESULT_STATUS (ret)) { +case RMI_SUCCESS : +free_rtt (rtt_granule); +break ; +case RMI_ERROR_RTT : +if (next_addr > addr) { +/* Missing RTT, skip */ +break ; + } +/* +* We destroy the RTT range for the full IPA +* space, after everything is unmapped. Also we +* descend down only if we cannot destroy a +* top level RTT. Thus RMM must be able to walk +* to the requested level. e.g., a block mapping +* exists at L1 or L2. +*/ +if (WARN_ON (RMI_RESULT_DATA_LEVEL (ret) != level)) +return -EBUSY ; +if (WARN_ON (level ==KVM_PGTABLE_LAST_LEVEL )) +return -EBUSY ; + +/* +* The table has active entries in it, recurse deeper +* and destroy the RTTs. +*/ + next_addr =ALIGN (addr +1 , map_size); + ret =realm_destroy_rtt_level (realm, + level +1 , + addr, + next_addr); +if (ret) +return ret; +/* +* Now that the child RTTs are destroyed, +* retry at this level. +*/ + next_addr = addr; +break ; +default : +WARN_ON (1 ); +return -ENXIO ; + } + } + +return 0 ; +} + +static int realm_destroy_rtts (struct kvm *kvm) +{ +struct realm *realm = &kvm->arch.realm; +unsigned int ia_bits = realm->ia_bits; + +lockdep_assert_held (&kvm->arch.config_lock); +if (WARN_ON_ONCE (!realm->stage2_unmapped)) +return -EINVAL ; + +/* Root RTTs remain until after the RD has been destroyed. */ +return realm_destroy_rtt_level (realm, realm->start_level +1 , +0 ,1UL << ia_bits); +} + +void kvm_realm_unmap_stage2_all (struct kvm *kvm) +{ +struct realm *realm = &kvm->arch.realm; + +lockdep_assert_held (&kvm->arch.config_lock); + +if (realm->stage2_unmapped) +return ; + +write_lock (&kvm->mmu_lock); +kvm_stage2_unmap_range_filter (&kvm->arch.mmu,0 , +BIT (realm->ia_bits -1 ), true, +KVM_FILTER_PRIVATE ); +write_unlock (&kvm->mmu_lock); + + realm->stage2_unmapped = true; +} +void kvm_destroy_realm (struct kvm *kvm) {struct realm *realm = &kvm->arch.realm;size_t pgd_size =kvm_pgtable_stage2_pgd_size (kvm->arch.mmu.vtcr); +/* +* Serialize final Realm destruction with Realm creation and stage-2 unmapping, +* which also access Realm state under config_lock. +*/ +guard (mutex)(&kvm->arch.config_lock); +if (!kvm_realm_is_created (kvm)) goto out_free_sro;kvm_set_realm_state (kvm,REALM_STATE_DYING ); +/* +* REALM_DESTROY requires the realm to be non-live: all RECs must have +* been destroyed and the root RTTs must be empty. Unmap protected IPAs +* and destroy any non-root RTTs before tearing down the RD. The root +* RTT pages are still owned by the RMM at this point, so keep the KVM +* pgtable alive until after REALM_DESTROY and undelegation. +*/ +kvm_realm_unmap_stage2_all (kvm); +if (WARN_ON (rmi_realm_terminate (realm->rd_phys, realm->sro))) goto out_free_sro; +if (WARN_ON (realm_destroy_rtts (kvm))) + goto out_free_sro; +if (WARN_ON (rmi_realm_destroy (realm->rd_phys, realm->sro))) goto out_free_sro;rmi_free_delegated_page (realm->rd_phys);@@ -39,6 +207,9 @@ void kvm_destroy_realm(struct kvm *kvm)kvm_set_realm_state (kvm,REALM_STATE_DEAD ); +/* Now that the realm is destroyed, free the entry-level RTTs. */ +kvm_free_stage2_pgd (&kvm->arch.mmu); + out_free_sro:kfree (realm->sro); realm->sro =NULL ;
Re: [PATCH v5 06/11] KVM: arm64: CCA: Destroy RTTs
Quoted 55 lines · Suzuki K Poulose
Quoted 54 lines · Steven Price
The RMM owns the stage 2 page tables for a realm, and KVM must request that the RMM creates/destroys entries as necessary. The physical pages to store the page tables are delegated to the realm as required, and can be undelegated when no longer used.
Creating new RTTs is the easy part, destroying them is a little more tricky. The result of
realm_rtt_destroy()can be used to effectively walk the tree and destroy the entries (undelegating pages that were given to the realm).
REALM_DESTROYrequires no RECs, live mappings, or non-root RTTs. Unmap the protected IPA half, then useREALM_TERMINATEto move the Realm to ZOMBIE. Destroy non-root RTTs before destroying the RD. Unprotected entries are not live and need no explicit unmap. Undelegate the root RTTs only after the RD has gone away.Signed-off-by Steven Price <[email protected]>---
Changes since v4:
- Clarify Realm liveness and drop the stale start-level explanation.
- Fold the RTT destroy wrapper, use a scoped mutex guard, and unmap only private mappings during teardown.Changes since v3:
- Match VTCR's minimum of two stage-2 levels for Realm RTTs.
- Use explicitconfig_lockcleanup in Realm destruction.Changes since v2:
- Use the actual stage-2 start level and correct the RTT skip granularity. Free SRO state on final teardown errors.Changes since v1:
- Tear down created Realms in both NEW and ACTIVE states. Fix the RTT start level, page accounting on failed undelegation, and handling of RTT granules returned by RMI.
---
arch/arm64/include/asm/kvm_mmu.h| 5 +arch/arm64/include/asm/kvm_rmi.h| 3 + arch/arm64/kvm/mmu.c | 23 ++++- arch/arm64/kvm/rmi.c | 171 +++++++++++++++++++++++++++++++4 files changed, 198 insertions(+), 4 deletions(-)arch/arm64/include/ renamed from arch/arm64/include/asm/kvm_mmu.h +0
-0
asm/kvm_mmu.h
index32392abf2e21..6ae9d07fefee100644arch/arm64/include/asm/kvm_mmu.h +1
-0@@ -182,6 +182,11 @@ int create_hyp_exec_mappings(phys_addr_t phys_addr, size_t size, int create_hyp_stack(phys_addr_t phys_addr, unsigned long *haddr); void __init free_hyp_pgds(void); +enum kvm_gfn_range_filter; +
Quoted 8 lines
arch/arm64/include/asm/kvm_mmu.h int create_hyp_exec_mappings(phys_addr_t phys_addr, size_t size, · line 189
Quoted 4 lines
arch/arm64/include/asm/kvm_mmu.h +3
-0+void kvm_stage2_unmap_range_filter (struct kvm_s2_mmu *mmu,phys_addr_t start, + u64 size, bool may_block, + enum kvm_gfn_range_filter filter);If we bring the Patch 7 before this one, we could avoid this and directly call
kvm_realm_unmap_stage2_range_filter()or evenrealm_unmap_private_range()
Ah, yes that's a good change. Thanks for the suggestion.
Steve
Quoted 283 lines
Otherwise, looks good to me
Suzuki
Quoted 39 lines
arch/arm64/include/asm/kvm_mmu.h +0
-0void kvm_stage2_unmap_range(struct kvm_s2_mmu *mmu, phys_addr_t start,u64 size, bool
may_block);
voidkvm_stage2_flush_range(struct kvm_s2_mmu *mmu, phys_addr_t addr, phys_addr_t end);arch/arm64/include/ renamed from arch/arm64/include/asm/kvm_rmi.h +0
-0
asm/kvm_rmi.h
index4ac1af622872..ec6b06ffc28b100644arch/arm64/include/asm/kvm_rmi.h +1
-0@@ -58,6 +58,7 @@ enum realm_state { * @state: The lifetime state machine for the realm * @ia_bits: Number of valid Input Address bits in the IPA * @start_level: Starting level of the Realm Translation Table + * @stage2_unmapped: The Realm's protected stage-2 mappings have beenremoved
*/
struct realm {
phys_addr_trd_phys;arch/arm64/include/asm/kvm_rmi.h +2
-0@@ -72,11 +73,13 @@ struct realm { enum realm_state state; unsigned int ia_bits; int start_level; + bool stage2_unmapped; }; u32 kvm_rmm_ipa_limit(void); int kvm_init_realm(struct kvm *kvm); void kvm_destroy_realm(struct kvm *kvm); +void kvm_realm_unmap_stage2_all (struct kvm *kvm); #endif /* __ASM_KVM_RMI_H */arch/arm64/kvm/mmu.c +0
-0@@ -403,9 +403,9 @@ static int kvm_pgtable_stage2_unmap_tracked(struct
kvm_pgtable*pgt, u64 addr, u
* be called while holdingmmu_lockotherwise another faulting VCPU may
* come in and mess with things behind our backs.
*/
-static void kvm_stage2_unmap_range_filter(structkvm_s2_mmu*mmu,Quoted 8 lines
phys_addr_tstart,
- u64 size, boolmay_block,
- enumkvm_gfn_range_filterfilter) +void kvm_stage2_unmap_range_filter(structkvm_s2_mmu*mmu,phys_addr_tstart,
+ u64 size, boolmay_block,
+ enumkvm_gfn_range_filterfilter) {I
Quoted 227 lines
struct kvm *kvm =
kvm_s2_mmu_to_kvm(mmu);
@@ -1149,9 +1149,24 @@ int kvm_init_stage2_mmu(struct kvm *kvm, structkvm_s2_mmu*mmu, unsigned long t
return err;
}
+static voidkvm_realm_uninit_stage2(struct kvm_s2_mmu *mmu)arch/arm64/kvm/mmu.c +11
-0+{ + struct kvm *kvm = kvm_s2_mmu_to_kvm(mmu); + + guard(mutex)(&kvm->arch.config_lock); + if (kvm_realm_state(kvm) == REALM_STATE_NEW || + kvm_realm_state(kvm) == REALM_STATE_ACTIVE) { + kvm_set_realm_state(kvm, REALM_STATE_DYING); + kvm_realm_unmap_stage2_all(kvm); + } +} +void
kvm_uninit_stage2_mmu(struct kvm *kvm)
{
-kvm_free_stage2_pgd(&kvm->arch.mmu);
+ if (kvm_realm_is_created(kvm))
+kvm_realm_uninit_stage2(&kvm->arch.mmu);
+ else
+kvm_free_stage2_pgd(&kvm->arch.mmu);kvm_mmu_free_memory_cache(&kvm->arch.mmu.split_page_cache); }
diff --gita/arch/arm64/kvm/rmi.cb/arch/arm64/kvm/rmi.cindex6a8bff1d8d46..140c7f9ab2c2100644arch/arm64/kvm/rmi.c +12
-0@@ -16,19 +16,187 @@ u32 kvm_rmm_ipa_limit(void) return u64_get_bits(rmi_feat_reg(0), RMI_FEATURE_REGISTER_0_S2SZ); } +static void free_rtt(phys_addr_t phys) +{ + kvm_account_pgtable_pages(phys_to_virt(phys), -1); + if (rmi_free_delegated_page(phys)) + kvm_account_pgtable_pages(phys_to_virt(phys), 1); +} + +/* +* realm_rtt_destroy - Destroy an RTT at @level for @addr. +* +* Returns - Result of the RMI_RTT_DESTROY call, and: +* @rtt_granule: RTT granule, if the RTT was destroyed. +* @next_addr: IPA corresponding to the next possible valid entry we
+ * can target
+ */arch/arm64/kvm/rmi.c +135
-0+static long realm_rtt_destroy (struct realm *realm,unsigned long addr, + int level, phys_addr_t *rtt_granule, + unsigned long *next_addr) +{ + unsigned long out_rtt; + long ret; + + ret = rmi_rtt_destroy(realm->rd_phys, addr, level, + &out_rtt, next_addr); + + if (ret == RMI_SUCCESS) + *rtt_granule = out_rtt; + + return ret; +} + +static int realm_destroy_rtt_level (struct realm *realm,int level, + unsigned long start, unsigned long end) +{ + ssize_t map_size; + unsigned long addr, next_addr; + + if (WARN_ON(level > KVM_PGTABLE_LAST_LEVEL)) + return -EINVAL; + + map_size = kvm_granule_size(level - 1); + if (WARN_ON(!IS_ALIGNED(start, map_size))) + return -EINVAL; + + for (addr = start; addr < end; addr = next_addr) { + phys_addr_t rtt_granule; + long ret; + unsigned long align_addr = ALIGN(addr, map_size); + + next_addr = ALIGN(addr + 1, map_size); + + if (next_addr > end || align_addr != addr) { + /* + * The target range is smaller than what this level + * covers, recurse deeper. + */ + ret = realm_destroy_rtt_level(realm, + level + 1, + addr, + min(next_addr, end)); + if (ret) + return ret; + continue; + } + + ret = realm_rtt_destroy(realm, addr, level, + &rtt_granule, &next_addr); + if (ret < 0) + return ret; + + switch (RMI_RESULT_STATUS(ret)) { + case RMI_SUCCESS: + free_rtt(rtt_granule); + break; + case RMI_ERROR_RTT: + if (next_addr > addr) { + /* Missing RTT, skip */ + break; + } + /* + * We destroy the RTT range for the full IPA + * space, after everything is unmapped. Also we + * descend down only if we cannot destroy a + * top level RTT. Thus RMM must be able to walk + * to the requested level. e.g., a block mapping + * exists at L1 or L2. + */ + if (WARN_ON(RMI_RESULT_DATA_LEVEL(ret) != level)) + return -EBUSY; + if (WARN_ON(level == KVM_PGTABLE_LAST_LEVEL)) + return -EBUSY; + + /* + * The table has active entries in it, recurse deeper + * and destroy the RTTs. + */ + next_addr = ALIGN(addr + 1, map_size); + ret = realm_destroy_rtt_level(realm, + level + 1, + addr, + next_addr); + if (ret) + return ret; + /* + * Now that the child RTTs are destroyed, + * retry at this level. + */ + next_addr = addr; + break; + default: + WARN_ON(1); + return -ENXIO; + } + } + + return 0; +} + +static int realm_destroy_rtts (struct kvm *kvm) +{ + struct realm *realm = &kvm->arch.realm; + unsigned int ia_bits = realm->ia_bits; + + lockdep_assert_held(&kvm->arch.config_lock); + if (WARN_ON_ONCE(!realm->stage2_unmapped)) + return -EINVAL; + + /* Root RTTs remain until after the RD has been destroyed. */ + return realm_destroy_rtt_level(realm, realm->start_level + 1, + 0, 1UL << ia_bits); +} + +void kvm_realm_unmap_stage2_all (struct kvm *kvm) +{ + struct realm *realm = &kvm->arch.realm; + + lockdep_assert_held(&kvm->arch.config_lock); + + if (realm->stage2_unmapped) + return; + + write_lock(&kvm->mmu_lock); + kvm_stage2_unmap_range_filter(&kvm->arch.mmu, 0, + BIT(realm->ia_bits - 1), true, + KVM_FILTER_PRIVATE); + write_unlock(&kvm->mmu_lock); + + realm->stage2_unmapped = true; +} +void
kvm_destroy_realm(struct kvm *kvm)
{
struct realm *realm =&kvm->arch.realm;
size_tpgd_size=kvm_pgtable_stage2_pgd_size(kvm->arch.mmu.vtcr); + /*
+ * Serialize final Realm destruction with Realm creation and stage-2 unmapping,
+ * which also access Realm state underconfig_lock.
+ */
+ guard(mutex)(&kvm->arch.config_lock);
+
if (!kvm_realm_is_created(kvm))
gotoout_free_sro;
kvm_set_realm_state(kvm, REALM_STATE_DYING);
+ /*
+ *REALM_DESTROYrequires the realm to be non-live: all RECs must have
+ * been destroyed and the root RTTs must be empty. Unmap protected IPAs
+ * and destroy any non-root RTTs before tearing down the RD. The root
+ * RTT pages are still owned by the RMM at this point, so keep the KVM
+ * pgtable alive until afterREALM_DESTROYand undelegation. + */
+kvm_realm_unmap_stage2_all(kvm);
+
if (WARN_ON(rmi_realm_terminate(realm->rd_phys, realm->sro))) gotoout_free_sro;
+ if (WARN_ON(realm_destroy_rtts(kvm)))
+ gotoout_free_sro;
+
if (WARN_ON(rmi_realm_destroy(realm->rd_phys, realm->sro))) gotoout_free_sro;rmi_free_delegated_page(realm->rd_phys);arch/arm64/kvm/rmi.c +0
-0@@ -39,6 +207,9 @@ void kvm_destroy_realm(struct kvm *kvm) kvm_set_realm_state(kvm, REALM_STATE_DEAD);+ /* Now that the realm is destroyed, free the entry-level RTTs. */ +
kvm_free_stage2_pgd(&kvm->arch.mmu);
+
out_free_sro:
kfree(realm->sro);
realm->sro= NULL;
PATCH v5 7/11 KVM: arm64: CCA: Add Realm RTT mapping and invalidation helpers
In reply to Steven PriceAdd the RMI RTT creation and unmap helpers needed by CCA Realm stage-2 memory operations. REC exit handling and guest requested RIPAS changes follow in the vCPU series.
---
Changes since v4:
- Drop RTT folding during unmap.
- Avoid cache topups during shared unmap; use atomic RTT splits and fall back to unmapping the whole block if a split fails.
Changes since v3:
- Use the VTCR-derived Realm start level for RTT operations.
Changes since v2:
- Use a local MMU cache during notifier invalidation, handle failed RTT splits, and validate RMI levels and cache state.
Changes since v1:
- Rework RTT creation and folding around missing or existing RTTs. Use a shared RMI block-size helper and handle alignment, partial progress and errors in range operations.
--- arch/arm64/include/asm/kvm_rmi.h | 8 + arch/arm64/kvm/rmi.c | 312 +++++++++++++++++++++++++++++++ drivers/firmware/arm_rmm/rmi.c | 10 - include/linux/arm-rmi-cmds.h | 8 + 4 files changed, 328 insertions(+), 10 deletions(-)
arch/arm64/include/asm/kvm_rmi.h +8 -0
@@ -82,4 +82,12 @@ int kvm_init_realm(struct kvm *kvm);void kvm_destroy_realm (struct kvm *kvm);void kvm_realm_unmap_stage2_all (struct kvm *kvm); +enum kvm_gfn_range_filter ; + +void kvm_realm_unmap_range_filter (struct kvm *kvm, +unsigned long ipa, +unsigned long size, +bool may_block, +enum kvm_gfn_range_filter attr_filter); +#endif /* __ASM_KVM_RMI_H */
arch/arm64/kvm/rmi.c +312 -0
336 lines not on this page · raw message
drivers/firmware/arm_rmm/rmi.c +0 -10
@@ -11,7 +11,6 @@#include <linux/slab.h> #include <asm/memory.h> -#include <asm/pgtable-hwdef.h> /* RMM v2.0 defines RmiFeatureRegister0 to RmiFeatureRegister4. */ static unsigned long rmi_feat_reg_cache[5 ]__ro_after_init ;
@@ -183,15 +182,6 @@ int rmi_free_delegated_page(phys_addr_t phys) }EXPORT_SYMBOL_GPL (rmi_free_delegated_page); -/* -* Convert the RmiAddrBlockSize to actual size. This is used in RmiDonateReq -* and RmiAddrRangeDesc*. -*/ -static unsigned long rmi_addr_block_size_to_bytes (unsigned long block_size_fld) -{ -return BIT (ARM64_HW_PGTABLE_LEVEL_SHIFT (3 - block_size_fld)); -} -/* * free_addr_range: Free memory described by the address range entry, which may * be partially consumed by RMM.
include/linux/arm-rmi-cmds.h +8 -0
@@ -13,8 +13,16 @@#include <linux/string.h> #include <linux/types.h> +#include <asm/pgtable-hwdef.h> +#define RMI_MAX_ADDR_LIST 256 +/* Convert the block size used by RmiDonateReq and RmiAddrRangeDesc. */ +static inline unsigned long rmi_addr_block_size_to_bytes (unsigned long block_size) +{ +return BIT (ARM64_HW_PGTABLE_LEVEL_SHIFT (3 - block_size)); +} +struct rmi_sro_state {struct arm_smccc_1_2_regs regs;int addr_count;
Signature · 1 line
2.43.0
[email protected]
AI reviewRe: [PATCH v5 07/11] KVM: arm64: CCA: Add Realm RTT mapping and invalidation helpers
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Medium] KVM: arm64: cca: stale err_level and KVM_BUG_ON() on OOM in private unmap --
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Medium] KVM: arm64: cca: stale err_level and KVM_BUG_ON() on OOM in private unmap
--
commit ecf125967f6f8c4f0a2799914ef908db3d675dea
Author: Steven Price <[email protected]>
KVM: arm64: CCA: Add Realm RTT mapping and invalidation helpers
This commit adds RMI Realm Translation Table (RTT) creation and shared/private range unmap helpers for CCA Realm stage-2 memory operations.
Quoted 4 lines
arch/arm64/kvm/rmi.c +0
-0
[ ... ]
Quoted 26 lines
arch/arm64/kvm/rmi.c +22
-0@@ -166,6 +286,198 @@ void kvm_realm_unmap_stage2_all(struct kvm *kvm) realm->stage2_unmapped = true; } +static bool realm_unmap_cache_topup (struct kvm *kvm, +struct kvm_mmu_memory_cache *mc,int needed) +{ +int ret; + +if (kvm_mmu_memory_cache_nr_free_objects (mc) >= needed) +return false; + +lockdep_assert_held_write (&kvm->mmu_lock); +write_unlock (&kvm->mmu_lock); + ret =__kvm_mmu_topup_memory_cache (mc,KVM_PGTABLE_LAST_LEVEL , needed); +write_lock (&kvm->mmu_lock); + +return !ret; +} + +static void realm_unmap_shared_range (struct kvm *kvm, +unsigned long start, +unsigned long end, +bool may_block, +struct kvm_mmu_memory_cache *mc) +{
[ ... ]
Quoted 30 lines
arch/arm64/kvm/rmi.c +30
-0+ ret =realm_create_rtt_levels (realm, addr, err_level, + level, mc); +if (!ret) { +/* Retry with the RTT levels in place. */ + next_addr = addr; +break ; + } +WARN_ON (ret != -ENOMEM ); + +/* Splitting failed; unmap the entire existing block. */ + next_addr =ALIGN_DOWN (addr,kvm_granule_size (err_level)); + end =max (end, next_addr +kvm_granule_size (err_level)); +break ; + } +default : +WARN_ON (1 ); +return ; + } + +if (may_block) +cond_resched_rwlock_write (&kvm->mmu_lock); + } +} + +static void realm_unmap_private_range (struct kvm *kvm, +unsigned long start, +unsigned long end, +bool may_block, +struct kvm_mmu_memory_cache *mc) +{
[ ... ]
Quoted 23 lines
arch/arm64/kvm/rmi.c +23
-0+case RMI_ERROR_RTT : { +int err_level =RMI_RESULT_DATA_LEVEL (ret); +int level =find_map_level (realm, addr, end); + +if (WARN_ON (err_level < realm->start_level || + err_level >KVM_PGTABLE_LAST_LEVEL )) +return ; + +if (err_level >= level) { +/* Nothing present, so skip this RTT entry. */ + next_addr =ALIGN (addr +1 , +kvm_granule_size (err_level)); +break ; + } + +if (may_block &&realm_unmap_cache_topup (kvm, mc, + level - err_level)) { + next_addr = addr; +break ; + } + + ret =realm_create_rtt_levels (realm, addr, err_level, + level, mc);
arch/arm64/kvm/rmi.c void kvm_realm_unmap_stage2_all(struct kvm *kvm) · line 438
Quoted 10 lines
arch/arm64/kvm/rmi.c +10
-0+if (!ret) { + next_addr = addr; +break ; + } + +/* A failed split cannot leave the backing pages mapped. */ +KVM_BUG_ON (1 , kvm); + next_addr =ALIGN_DOWN (addr,kvm_granule_size (err_level)); + end =max (end, next_addr +kvm_granule_size (err_level)); +break ;
[Severity: Medium]
Does an allocation failure during an RTT split in realm_unmap_private_range() cause a stale err_level to be used and trigger a host kernel warning via KVM_BUG_ON(1, kvm)?
When realm_unmap_cache_topup() needs to allocate objects, it drops and re-acquires kvm->mmu_lock around __kvm_mmu_topup_memory_cache(), which uses GFP_KERNEL_ACCOUNT. If that allocation fails (for example under a memory cgroup limit), realm_unmap_cache_topup() returns false even though kvm->mmu_lock was released, so realm_unmap_private_range() proceeds directly to realm_create_rtt_levels() with the pre-unlock err_level instead of retrying rmi_rtt_data_unmap().
If realm_create_rtt_levels() then fails with -ENOMEM, KVM_BUG_ON(1, kvm) emits a host WARN_ON_ONCE() splat on a __GFP_ACCOUNT allocation failure instead of warning only when ret != -ENOMEM and marking the VM dead with kvm_vm_dead(kvm).
Also, if realm_create_rtt_levels() creates an intermediate RTT level before failing at a deeper level during a multi-level split, the fallback in both realm_unmap_private_range() and realm_unmap_shared_range() widens next_addr and end using kvm_granule_size(err_level) rather than the granule size of the last level reached.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=7
Re: [PATCH v5 07/11] KVM: arm64: CCA: Add Realm RTT mapping and invalidation helpers
Quoted 135 lines · On 30/11/2026 14:53, [email protected] wrote:
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Medium] KVM: arm64: cca: staleerr_levelandKVM_BUG_ON()on OOM in private unmap
--commit
ecf125967f6f8c4f0a2799914ef908db3d675dea
Author: Steven Price <[email protected]>KVM: arm64: CCA: Add Realm RTT mapping and invalidation helpers
This commit adds RMI Realm Translation Table (RTT) creation and shared/private range unmap helpers for CCA Realm stage-2 memory operations.
Quoted 4 lines
arch/arm64/kvm/rmi.c +0
-0[ ... ]
Quoted 26 lines
arch/arm64/kvm/rmi.c +22
-0@@ -166,6 +286,198 @@ void kvm_realm_unmap_stage2_all(struct kvm *kvm) realm->stage2_unmapped = true; } +static bool realm_unmap_cache_topup (struct kvm *kvm, +struct kvm_mmu_memory_cache *mc,int needed) +{ +int ret; + +if (kvm_mmu_memory_cache_nr_free_objects (mc) >= needed) +return false; + +lockdep_assert_held_write (&kvm->mmu_lock); +write_unlock (&kvm->mmu_lock); + ret =__kvm_mmu_topup_memory_cache (mc,KVM_PGTABLE_LAST_LEVEL , needed); +write_lock (&kvm->mmu_lock); + +return !ret; +} + +static void realm_unmap_shared_range (struct kvm *kvm, +unsigned long start, +unsigned long end, +bool may_block, +struct kvm_mmu_memory_cache *mc) +{[ ... ]
Quoted 30 lines
arch/arm64/kvm/rmi.c +30
-0+ ret =realm_create_rtt_levels (realm, addr, err_level, + level, mc); +if (!ret) { +/* Retry with the RTT levels in place. */ + next_addr = addr; +break ; + } +WARN_ON (ret != -ENOMEM ); + +/* Splitting failed; unmap the entire existing block. */ + next_addr =ALIGN_DOWN (addr,kvm_granule_size (err_level)); + end =max (end, next_addr +kvm_granule_size (err_level)); +break ; + } +default : +WARN_ON (1 ); +return ; + } + +if (may_block) +cond_resched_rwlock_write (&kvm->mmu_lock); + } +} + +static void realm_unmap_private_range (struct kvm *kvm, +unsigned long start, +unsigned long end, +bool may_block, +struct kvm_mmu_memory_cache *mc) +{[ ... ]
Quoted 23 lines
arch/arm64/kvm/rmi.c +23
-0+case RMI_ERROR_RTT : { +int err_level =RMI_RESULT_DATA_LEVEL (ret); +int level =find_map_level (realm, addr, end); + +if (WARN_ON (err_level < realm->start_level || + err_level >KVM_PGTABLE_LAST_LEVEL )) +return ; + +if (err_level >= level) { +/* Nothing present, so skip this RTT entry. */ + next_addr =ALIGN (addr +1 , +kvm_granule_size (err_level)); +break ; + } + +if (may_block &&realm_unmap_cache_topup (kvm, mc, + level - err_level)) { + next_addr = addr; +break ; + } + + ret =realm_create_rtt_levels (realm, addr, err_level, + level, mc);arch/arm64/kvm/rmi.c
Quoted 10 lines
arch/arm64/kvm/rmi.c +10
-0+if (!ret) { + next_addr = addr; +break ; + } + +/* A failed split cannot leave the backing pages mapped. */ +KVM_BUG_ON (1 , kvm); + next_addr =ALIGN_DOWN (addr,kvm_granule_size (err_level)); + end =max (end, next_addr +kvm_granule_size (err_level)); +break ;[Severity: Medium]
Does an allocation failure during an RTT split inrealm_unmap_private_range()cause a staleerr_levelto be used and trigger a host kernel warning viaKVM_BUG_ON(1, kvm)?When
realm_unmap_cache_topup()needs to allocate objects, it drops and re-acquireskvm->mmu_lockaround__kvm_mmu_topup_memory_cache(), which usesGFP_KERNEL_ACCOUNT. If that allocation fails (for example under a memory cgroup limit),realm_unmap_cache_topup()returns false even thoughkvm->mmu_lockwas released, sorealm_unmap_private_range()proceeds directly torealm_create_rtt_levels()with the pre-unlockerr_levelinstead of retryingrmi_rtt_data_unmap().If
realm_create_rtt_levels()then fails with-ENOMEM,KVM_BUG_ON(1, kvm)emits a hostWARN_ON_ONCE()splat on a__GFP_ACCOUNTallocation failure instead of warning only when ret !=-ENOMEMand marking the VM dead withkvm_vm_dead(kvm).
Quoted 6 lines
Also, if
realm_create_rtt_levels()creates an intermediate RTT level before failing at a deeper level during a multi-level split, the fallback in bothrealm_unmap_private_range()andrealm_unmap_shared_range()widensnext_addrand end usingkvm_granule_size(err_level)rather than the granule size of the last level reached.
I can see Sashiko is going to keep complaining about this...
While I don't agree this is actually a problem, I don't really like the return value from realm_unmap_cache_topup(). So I propose the following diff (on top of this patch) which adds a "lock_dropped" output flag and retries the walk in that case. This also adds a "try_topup" flag so we don't get stuck in an infinite loop if the memory isn't available.
----8<-----
arch/arm64/kvm/rmi.c +20 -8
@@ -331,20 +331,23 @@ void kvm_realm_unmap_stage2_all(struct kvm *kvm) realm->stage2_unmapped = true; } -static bool realm_unmap_cache_topup (struct kvm *kvm, -struct kvm_mmu_memory_cache *mc,int needed) +static int realm_unmap_cache_topup (struct kvm *kvm, +struct kvm_mmu_memory_cache *mc, +unsigned int needed,bool *lock_dropped) {int ret; + *lock_dropped = false;if (kvm_mmu_memory_cache_nr_free_objects (mc) >= needed) -return false; +return 0 ;lockdep_assert_held_write (&kvm->mmu_lock);write_unlock (&kvm->mmu_lock); ret =__kvm_mmu_topup_memory_cache (mc,KVM_PGTABLE_LAST_LEVEL , needed);write_lock (&kvm->mmu_lock); -return !ret; + *lock_dropped = true; +return ret; } static void realm_unmap_shared_range(struct kvm *kvm,
@@ -542,6 +545,7 @@ static void realm_unmap_private_range(struct kvm *kvm,struct realm *realm = &kvm->arch.realm;unsigned long rd = realm->rd_phys;unsigned long next_addr, addr; +bool try_topup = may_block;long ret;for (addr = start; addr < end; addr = next_addr) {
@@ -575,10 +579,18 @@ static void realm_unmap_private_range(struct kvm *kvm,break ; } -if (may_block &&realm_unmap_cache_topup (kvm, mc, - level - err_level)) { - next_addr = addr; -break ; +if (try_topup) { +bool lock_dropped; + +if (realm_unmap_cache_topup (kvm, mc, + level - err_level, + &lock_dropped)) + try_topup = false; +if (lock_dropped) { +/* Retry the RTT walk with mmu_lock held. */ + next_addr = addr; +break ; + } } ret = realm_create_rtt_levels(realm, addr, err_level,
Suzuki K Poulose
reviewerRe: [PATCH v5 07/11] KVM: arm64: CCA: Add Realm RTT mapping and invalidation helpers
In reply to Steven PriceQuoted 4 lines · Steven Price
Add the RMI RTT creation and unmap helpers needed by CCA Realm stage-2 memory operations. REC exit handling and guest requested RIPAS changes follow in the vCPU series.
minor nit :
Can this patch come before the Destroy RTTs ? If so we could usekvm_realm_unmap_range_filter() directly from kvm_realm_stage2_unmap_all() instead of looping through kvm_stage2_unmap_range_filter()
Quoted 207 lines
Signed-off-by Steven Price <[email protected]>---
Changes since v4:
- Drop RTT folding during unmap.
- Avoid cache topups during shared unmap; use atomic RTT splits and fall back to unmapping the whole block if a split fails.Changes since v3:
- Use the VTCR-derived Realm start level for RTT operations.Changes since v2:
- Use a local MMU cache during notifier invalidation, handle failed RTT splits, and validate RMI levels and cache state.Changes since v1:
- Rework RTT creation and folding around missing or existing RTTs. Use a shared RMI block-size helper and handle alignment, partial progress and errors in range operations.--- arch/arm64/include/asm/kvm_rmi.h | 8 + arch/arm64/kvm/rmi.c | 312 +++++++++++++++++++++++++++++++ drivers/firmware/arm_rmm/rmi.c | 10 - include/linux/arm-rmi-cmds.h | 8 + 4 files changed, 328 insertions(+), 10 deletions(-)arch/arm64/include/asm/kvm_rmi.h +8
-0@@ -82,4 +82,12 @@ int kvm_init_realm(struct kvm *kvm);void kvm_destroy_realm (struct kvm *kvm);void kvm_realm_unmap_stage2_all (struct kvm *kvm); +enum kvm_gfn_range_filter ; + +void kvm_realm_unmap_range_filter (struct kvm *kvm, +unsigned long ipa, +unsigned long size, +bool may_block, +enum kvm_gfn_range_filter attr_filter); +#endif /* __ASM_KVM_RMI_H */ arch/arm64/kvm/rmi.c +135
-0@@ -16,6 +16,76 @@ u32 kvm_rmm_ipa_limit(void)return u64_get_bits (rmi_feat_reg (0 ),RMI_FEATURE_REGISTER_0_S2SZ ); } +static int find_map_level (struct realm *realm, +unsigned long start, +unsigned long end) +{ +int start_level = realm->start_level; +int level =KVM_PGTABLE_LAST_LEVEL ; + +while (level > start_level) { +unsigned long map_size =kvm_granule_size (level -1 ); + +if (!IS_ALIGNED (start, map_size) || + (start + map_size) > end) +break ; + + level--; + } + +return level; +} + +static int undelegate_range_desc (unsigned long desc) +{ +unsigned long size =rmi_addr_block_size_to_bytes (RMI_ADDR_RANGE_BLOCK_SIZE (desc)); +unsigned long count =RMI_ADDR_RANGE_COUNT (desc); +unsigned long addr =RMI_ADDR_RANGE_ADDR (desc); +unsigned long state =RMI_ADDR_RANGE_STATE (desc); + +if (state ==RMI_OP_MEM_UNDELEGATED ) +return 0 ; + +if (size * count ==0 ) +return 0 ; + +return rmi_undelegate_range (addr, size * count); +} + +static phys_addr_t alloc_delegated_granule (struct kvm_mmu_memory_cache *mc) +{ +phys_addr_t phys; +void *virt; + +if (mc &&kvm_mmu_memory_cache_nr_free_objects (mc)) { + virt =kvm_mmu_memory_cache_alloc (mc); + }else { + virt = (void *)__get_free_page (GFP_ATOMIC | __GFP_ZERO | + __GFP_ACCOUNT); + } + +if (!virt) +return PHYS_ADDR_MAX ; + + phys =virt_to_phys (virt); +if (rmi_delegate_page (phys)) { +free_page ((unsigned long )virt); +return PHYS_ADDR_MAX ; + } + +return phys; +} + +static phys_addr_t alloc_rtt (struct kvm_mmu_memory_cache *mc) +{ +phys_addr_t phys =alloc_delegated_granule (mc); + +if (phys !=PHYS_ADDR_MAX ) +kvm_account_pgtable_pages (phys_to_virt (phys),1 ); + +return phys; +} +static void free_rtt (phys_addr_t phys) {kvm_account_pgtable_pages (phys_to_virt (phys),-1 );@@ -23,6 +93,15 @@ static void free_rtt(phys_addr_t phys)kvm_account_pgtable_pages (phys_to_virt (phys),1 ); } +static long realm_rtt_create (struct realm *realm, +unsigned long addr, +int level, +phys_addr_t phys) +{ + addr =ALIGN_DOWN (addr,kvm_granule_size (level -1 )); +return rmi_rtt_create (realm->rd_phys, phys, addr, level); +} +/* * realm_rtt_destroy - Destroy an RTT at @level for @addr. * @@ -47,6 +126,47 @@ static long realm_rtt_destroy(struct realm *realm, unsigned long addr,return ret; } +static int realm_create_rtt_levels (struct realm *realm, +unsigned long ipa, +int level, +int max_level, +struct kvm_mmu_memory_cache *mc) +{ +long ret =0 ; +phys_addr_t rtt =PHYS_ADDR_MAX ; + +while (level++ < max_level) { +if (rtt ==PHYS_ADDR_MAX ) + rtt =alloc_rtt (mc); +if (rtt ==PHYS_ADDR_MAX ) +return -ENOMEM ; + + ret =realm_rtt_create (realm, ipa, level, rtt); +if (ret <0 ) +break ; + +if (RMI_RESULT_STATUS (ret) ==RMI_ERROR_RTT && +RMI_RESULT_DATA_LEVEL (ret) == level -1 ) { +/* The RTT already exists, reuse the granule at the next level. */ + ret =0 ; +continue ; + } + +if (ret) { +WARN (1 ,"Failed to create RTT at level %d: %ld\n" , + level, ret); + ret = -ENXIO ; +break ; + } + rtt =PHYS_ADDR_MAX ; + } + +if (rtt !=PHYS_ADDR_MAX ) +free_rtt (rtt); + +return ret; +} +static int realm_destroy_rtt_level (struct realm *realm,int level,unsigned long start,unsigned long end) {@@ -166,6 +286,198 @@ void kvm_realm_unmap_stage2_all(struct kvm *kvm) realm->stage2_unmapped = true; } +static bool realm_unmap_cache_topup (struct kvm *kvm, +struct kvm_mmu_memory_cache *mc,int needed) +{ +int ret; + +if (kvm_mmu_memory_cache_nr_free_objects (mc) >= needed) +return false; + +lockdep_assert_held_write (&kvm->mmu_lock); +write_unlock (&kvm->mmu_lock); + ret =__kvm_mmu_topup_memory_cache (mc,KVM_PGTABLE_LAST_LEVEL , needed); +write_lock (&kvm->mmu_lock); + +return !ret; +}
...
arch/arm64/kvm/rmi.c void kvm_realm_unmap_stage2_all(struct kvm *kvm) · line 460
Quoted 12 lines
arch/arm64/kvm/rmi.c +12
-0+ +void kvm_realm_unmap_range_filter (struct kvm *kvm,unsigned long start, +unsigned long size,bool may_block, +enum kvm_gfn_range_filter attr_filter) +{ +/* +* MMU notifier callbacks only invalidate shared mappings, which can +* fall back to unmapping a larger block if an atomic split fails. +* Private invalidation uses a local cache instead of the eager split +* cache protected by slots_lock. +*/ +struct kvm_mmu_memory_cache cache = { .gfp_zero = __GFP_ZERO };
Is it better to keep a cache in struct realm that can be used here instead of the local cache ? That could be reused for RMI_POPULATE and even SET_RIPAS.
Suzuki
Re: [PATCH v5 07/11] KVM: arm64: CCA: Add Realm RTT mapping and invalidation helpers
Quoted 5 lines · Suzuki K Poulose
Quoted 4 lines · Steven Price
Add the RMI RTT creation and unmap helpers needed by CCA Realm stage-2 memory operations. REC exit handling and guest requested RIPAS changes follow in the vCPU series.
[...]
Quoted 9 lines
Quoted 9 lines
arch/arm64/kvm/rmi.c +9
-0+ +void kvm_realm_unmap_range_filter (struct kvm *kvm,unsigned long start, + unsigned long size, bool may_block, + enum kvm_gfn_range_filter attr_filter) +{ + /* +* MMU notifier callbacks only invalidate shared mappings, which can +* fall back to unmapping a larger block if an atomic split fails. +* Private invalidation uses a local cache instead of the eager
Quoted 8 lines
Quoted 4 lines
split
+ * cache protected byslots_lock.
+ */
+ structkvm_mmu_memory_cachecache = { .gfp_zero =__GFP_ZERO};Is it better to keep a cache in struct realm that can be used here instead of the local cache ? That could be reused for
RMI_POPULATEand evenSET_RIPAS.
The benefit of the local cache is it avoids locking issues. We drop mmu_lock to allocate pages so that doesn't help coordinate a shared cache, and holding it makes using another lock tricky without causing lock ordering issues. SET_RIPAS already makes use of the vCPU cache and RMI_POPULATE doesn't currently use a cache.
So a local cache is certainly much easier to reason about and without evidence of it causing performance issues I'd be keen to keep it this way.
Thanks,
Steve
PATCH v5 8/11 KVM: arm64: CCA: Restrict Realm memslots to the lower IPA half
In reply to Steven PriceFrom: Suzuki K Poulose <[email protected]>
The upper half of a Realm's IPA space aliases its lower, protected half. Restrict memslots to the lower half so a memslot cannot be created in the shared alias.
Keep the existing post-creation move/delete restriction scoped to pKVM. Protected VMs, including Realms, continue to reject dirty logging and read-only memslots.
---
Changes since v4:
- Remove the Realm guest_memfd memslot change restriction; private Realm faults now validate guest_memfd backing and attributes.
- Split out private-memory support and drop the unused early memslot validation hook.
Changes since v3:
- Explain the arm64-wide in-place conversion effect and keep the general IPA limit comment.
Changes since v1:
- Preserve private mappings if KVM invalidates a Realm guest_memfd slot before the prepare hook rejects its deletion.
--- arch/arm64/kvm/mmu.c | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-)
arch/arm64/kvm/mmu.c +10 -3
@@ -2860,11 +2860,13 @@ int kvm_arch_prepare_memory_region(struct kvm *kvm, enum kvm_mr_change change) {hva_t hva, reg_end; +gfn_t ipa_limit;int ret =0 ;if (kvm_vm_is_protected (kvm)) {/* Cannot modify memslots once a pVM has run. */ -if (pkvm_hyp_vm_is_created (kvm) && +if (kvm->arch.vm_flavor ==VM_PROTECTED_PKVM && +pkvm_hyp_vm_is_created (kvm) && (change ==KVM_MR_DELETE || change ==KVM_MR_MOVE )) {return -EPERM ; }
@@ -2881,9 +2883,14 @@ int kvm_arch_prepare_memory_region(struct kvm *kvm,/* * Prevent userspace from creating a memory region outside of the IPA -* space addressable by the KVM guest IPA space. +* space addressable by the KVM guest IPA space. Realm shared IPAs alias +* the corresponding private IPAs, so Realm memslots must reside entirely +* in the lower half of that space. */ -if ((new->base_gfn + new->npages) > (kvm_phys_size (&kvm->arch.mmu) >>PAGE_SHIFT )) + ipa_limit =kvm_phys_size (&kvm->arch.mmu) >>PAGE_SHIFT ; +if (kvm_vm_is_realm (kvm)) + ipa_limit >>=1 ; +if (new->base_gfn + new->npages > ipa_limit)return -EFAULT ;/* guest_memfd is incompatible with MTE. */
Signature · 1 line
2.43.0
Re: [PATCH v5 08/11] KVM: arm64: CCA: Restrict Realm memslots to the lower IPA half
Quoted 7 lines · Steven Price
From: Suzuki K Poulose <[email protected]>
The upper half of a Realm's IPA space aliases its lower, protected half. Restrict memslots to the lower half so a memslot cannot be created in the shared alias.
Quoted 6 lines
Keep the existing post-creation move/delete restriction scoped to pKVM. Protected VMs, including Realms, continue to reject dirty logging and read-only memslots.
Signed-off-by Suzuki K Poulose <[email protected]>Signed-off-by Steven Price <[email protected]>
Cheers,
/fuad
PATCH v5 9/11 KVM: arm64: CCA: Advertise private memory support for Realms
In reply to Steven PriceDefine kvm_arch_has_private_mem() so generic KVM recognizes Realm VMs as supporting private memory. This also enables gmem_in_place_conversion for arm64. Guest_memfd slots are therefore marked gmem-only even for non-Realm VMs, but non-Realm private-memory requests fail the VM check.
---
Changes since v4:
- Split private-memory capability advertisement from Realm memslot checks.
--- arch/arm64/include/asm/kvm_host.h | 2 ++ 1 file changed, 2 insertions(+)
arch/arm64/include/asm/kvm_host.h +2 -0
@@ -1607,6 +1607,8 @@ struct kvm *kvm_arch_alloc_vm(void);#define kvm_vm_is_realm (kvm) ((kvm)->arch.vm_flavor == VM_REALM) +#define kvm_arch_has_private_mem (kvm) kvm_vm_is_realm(kvm) +#define kvm_has_mte (kvm) \ (system_supports_mte() && \ test_bit(KVM_ARCH_FLAG_MTE_ENABLED, &(kvm)->arch.flags))
Signature · 1 line
2.43.0
Re: [PATCH v5 09/11] KVM: arm64: CCA: Advertise private memory support for Realms
Quoted 6 lines · Steven Price
Define
kvm_arch_has_private_mem()so generic KVM recognizes Realm VMs as supporting private memory. This also enablesgmem_in_place_conversionfor arm64.Guest_memfdslots are therefore marked gmem-only even for non-Realm VMs, but non-Realm private-memory requests fail the VM check.Signed-off-by Steven Price <[email protected]>
Quoted 21 lines
---
Changes since v4:
- Split private-memory capability advertisement from Realm memslot checks.--- arch/arm64/include/asm/kvm_host.h | 2 ++ 1 file changed, 2 insertions(+)arch/arm64/include/asm/kvm_host.h +2
-0@@ -1607,6 +1607,8 @@ struct kvm *kvm_arch_alloc_vm(void);#define kvm_vm_is_realm (kvm) ((kvm)->arch.vm_flavor == VM_REALM) +#define kvm_arch_has_private_mem (kvm) kvm_vm_is_realm(kvm) +#define kvm_has_mte (kvm) \ (system_supports_mte() && \ test_bit(KVM_ARCH_FLAG_MTE_ENABLED, &(kvm)->arch.flags))
PATCH v5 10/11 KVM: arm64: CCA: Support runtime faulting of memory
In reply to Steven PriceAt runtime, if the realm guest accesses an IPA without a stage-2 mapping, then KVM fixes up the stage-2 mapping for the backing memory.
For memory in the lower (protected) region of IPA, verify that the corresponding guest_memfd page is private before delegating to the RMM. The RMM then maps the delegated page into the Realm.
For memory in the upper (shared) IPA region, map the backing page as non-secure memory. Check that a guest_memfd page is shared when the fault uses guest_memfd; ordinary userspace-backed memslots can only be mapped at unprotected IPAs and follow the normal user memory fault path.
If the fault IPA and guest_memfd attributes disagree then exit to userspace with KVM_EXIT_MEMORY_FAULT so the VMM can perform the required conversion. If a shared IPA has no memslot, KVM follows the existing MMIO or abort-injection path. The latter can result in injecting a SEA to the guest.
Realm faults may need to create RTTs, including when updating a mapping after a permission fault. Take the write side of mmu_lock and top up the MMU cache for these faults. Do not pre-fault Realm memory.
The lock helpers are in a header compiled by nVHE too. Make kvm_vm_hyp_is_distrusting() available there for the lock choice.
---
Changes since v4:
- Preserve completed RMI mappings on partial failure or interruption and report how far mapping progressed. Account for partial writable mappings when releasing the backing folio.
- Use the VM flavor to select the fault lock and pre-fault policy.
Changes since v3:
- Split Realm faults into dedicated guest_memfd and userspace mapping paths, leaving the native fault paths unchanged.
- Clarify that runtime RMI data mapping does not guarantee zeroed contents.
Changes since v2:
- Route all guest_memfd faults through gmem_abort(). Bound each protected RMI map call to its encoded range, create the required RTT levels, and tighten retry and error handling.
Changes since v1:
- Use the write side of mmu_lock and top up the MMU cache for Realm faults that may create RTTs. Skip unnecessary cache flushing, disable Realm pre-faulting, and tighten memory-fault and mapping retry handling.
--- arch/arm64/include/asm/kvm_emulate.h | 13 ++ arch/arm64/include/asm/kvm_host.h | 4 +- arch/arm64/include/asm/kvm_mmu.h | 4 +- arch/arm64/include/asm/kvm_rmi.h | 14 ++ arch/arm64/kvm/mmu.c | 164 ++++++++++++++-- arch/arm64/kvm/rmi.c | 267 +++++++++++++++++++++++++++ 6 files changed, 451 insertions(+), 15 deletions(-)
arch/arm64/include/asm/kvm_emulate.h +13 -0
@@ -847,4 +847,17 @@ static inline bool kvm_realm_is_created(struct kvm *kvm) return kvm_vm_is_realm(kvm) && kvm_realm_state(kvm) != REALM_STATE_NONE; } +static inline gpa_t kvm_gpa_from_fault (struct kvm *kvm,phys_addr_t ipa) +{ +if (!kvm_vm_is_realm (kvm)) +return ipa; + +return ipa & ~BIT (kvm->arch.realm.ia_bits -1 ); +} + +static inline gfn_t kvm_gfn_from_fault (struct kvm *kvm,phys_addr_t ipa) +{ +return gpa_to_gfn (kvm_gpa_from_fault (kvm, ipa)); +} +#endif /* __ARM64_KVM_EMULATE_H__ */
arch/arm64/include/asm/kvm_host.h +2 -2
@@ -1601,12 +1601,12 @@ struct kvm *kvm_arch_alloc_vm(void);#define kvm_vm_is_protected (kvm) ((kvm)->arch.vm_flavor >= __VM_PROTECTED)#define vcpu_is_protected (vcpu) kvm_vm_is_protected((vcpu)->kvm) -#define kvm_vm_hyp_is_distrusting (kvm) ((kvm)->arch.vm_flavor >= __VM_DISTRUSTING_HYP) -#endif /* __KVM_NVHE_HYPERVISOR__ */ #define kvm_vm_is_realm (kvm) ((kvm)->arch.vm_flavor == VM_REALM) +#define kvm_vm_hyp_is_distrusting (kvm) ((kvm)->arch.vm_flavor >= __VM_DISTRUSTING_HYP) +#define kvm_arch_has_private_mem (kvm) kvm_vm_is_realm(kvm)#define kvm_has_mte (kvm) \
arch/arm64/include/asm/kvm_mmu.h +2 -2
@@ -376,7 +376,7 @@ static inline bool kvm_is_nested_s2_mmu(struct kvm *kvm, struct kvm_s2_mmu *mmu)static inline void kvm_fault_lock (struct kvm *kvm) { -if (is_protected_kvm_enabled ()) +if (kvm_vm_hyp_is_distrusting (kvm))write_lock (&kvm->mmu_lock);else read_lock (&kvm->mmu_lock);
@@ -384,7 +384,7 @@ static inline void kvm_fault_lock(struct kvm *kvm)static inline void kvm_fault_unlock (struct kvm *kvm) { -if (is_protected_kvm_enabled ()) +if (kvm_vm_hyp_is_distrusting (kvm))write_unlock (&kvm->mmu_lock);else read_unlock (&kvm->mmu_lock);
arch/arm64/include/asm/kvm_rmi.h +14 -0
@@ -6,6 +6,7 @@#ifndef __ASM_KVM_RMI_H#define __ASM_KVM_RMI_H +#include <asm/kvm_pgtable.h> #include <linux/arm-smccc-rmi.h> #include <linux/kvm.h>
@@ -90,4 +91,17 @@ void kvm_realm_unmap_range_filter(struct kvm *kvm,bool may_block, enum kvm_gfn_range_filter attr_filter); +/* mapped_top is the first IPA beyond the prefix installed by this call. */ +int realm_map_ipa (struct kvm *kvm,phys_addr_t ipa, +kvm_pfn_t pfn,unsigned long map_size, +enum kvm_pgtable_prot prot, +struct kvm_mmu_memory_cache *memcache, +phys_addr_t *mapped_top); + +static inline bool kvm_realm_is_private_address (struct realm *realm, +unsigned long addr) +{ +return !(addr &BIT (realm->ia_bits -1 )); +} +#endif /* __ASM_KVM_RMI_H */
arch/arm64/kvm/mmu.c +153 -11
254 lines not on this page · raw message
arch/arm64/kvm/rmi.c +267 -0
273 lines not on this page · raw message
Signature · 1 line
2.43.0
[email protected]
AI reviewRe: [PATCH v5 10/11] KVM: arm64: CCA: Support runtime faulting of memory
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [High] KVM: arm64: block mapping over child RTT fails in realm_map_unprotected() --
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [High] KVM: arm64: block mapping over child RTT fails in realm_map_unprotected()
--
commit 23c673437d9940025385704ad240e79450d0dcaa
Author: Steven Price <[email protected]>
KVM: arm64: CCA: Support runtime faulting of memory
This commit adds runtime stage-2 fault handling for Realm VMs, mapping private guest_memfd pages into the protected IPA range via the RMM and shared guest_memfd or userspace-backed memslot pages into the unprotected IPA range.
Quoted 24 lines
arch/arm64/kvm/rmi.c +16
-0@@ -478,6 +478,273 @@ void kvm_realm_unmap_range_filter(struct kvm *kvm, unsigned long start, } } +static unsigned long addr_range_desc (struct realm *realm, +unsigned long ipa,unsigned long phys, +unsigned long size,unsigned long state) +{ +unsigned long count =1 ; +unsigned long out; +int rmi_size; + +for (rmi_size =KVM_PGTABLE_LAST_LEVEL - realm->start_level; + rmi_size >=0 ; rmi_size--) { +unsigned long entry_size =rmi_addr_block_size_to_bytes (rmi_size); + +if (size >= entry_size &&IS_ALIGNED (ipa, entry_size) && +IS_ALIGNED (phys, entry_size)) +break ; + }
[ ... ]
Quoted 8 lines
arch/arm64/kvm/rmi.c +8
-0+static int realm_map_unprotected (struct kvm *kvm, +unsigned long ipa, +kvm_pfn_t pfn, +unsigned long size, +enum kvm_pgtable_prot prot, +struct kvm_mmu_memory_cache *memcache, +phys_addr_t *mapped_top) +{
[ ... ]
Quoted 22 lines
arch/arm64/kvm/rmi.c +22
-0+while (ipa < ipa_top) { +unsigned long range_desc =addr_range_desc (realm, ipa, phys, + ipa_top - ipa, +RMI_OP_MEM_UNDELEGATED ); +unsigned long call_top = ipa +RMI_ADDR_RANGE_COUNT (range_desc) * +rmi_addr_block_size_to_bytes (RMI_ADDR_RANGE_BLOCK_SIZE (range_desc)); +unsigned long out_top; + + ret =rmi_rtt_unprot_map (rd, ipa, call_top, flags, range_desc, + &out_top); +if (ret <0 ) +return ret; + +if (ret) { +/* Create missing RTTs and retry */ +int level =RMI_RESULT_DATA_LEVEL (ret); +int req_level =KVM_PGTABLE_LAST_LEVEL - +RMI_ADDR_RANGE_BLOCK_SIZE (range_desc); + +if (RMI_RESULT_STATUS (ret) ==RMI_BLOCKED ) +return -EAGAIN ; + if (RMI_RESULT_STATUS(ret) != RMI_ERROR_RTT ||
arch/arm64/kvm/rmi.c void kvm_realm_unmap_range_filter(struct kvm *kvm, unsigned long start, · line 696
Quoted 10 lines
arch/arm64/kvm/rmi.c +10
-0+ ++retries >KVM_PGTABLE_LAST_LEVEL +2 || + level < realm->start_level) + return -ENXIO; + +/* Replace an existing mapping to update its permissions. */ +if (level >= req_level) { +realm_unmap_shared_range (kvm, ipa, ipa_top, false, + memcache); +continue ; + }
[Severity: High]
Can realm_map_unprotected() fail with -ENXIO when installing a block mapping over an IPA range that already has a deeper child RTT table?
When realm_s2_fault_map() promotes mapping_size to PMD_SIZE for a THP (for example, on a write fault or after khugepaged collapse in a 2MB range where a 4KB page was previously faulted in), addr_range_desc() selects a block mapping at req_level = 2. Because a level 3 RTT table already exists, rmi_rtt_unprot_map() fails with RMI_ERROR_RTT and returns level >= req_level.
realm_unmap_shared_range() only calls rmi_rtt_unprot_unmap() to clear leaf entries and does not destroy or fold the empty child RTT table. Each retry therefore attempts the same level 2 block mapping over the still-present table entry until retries exceeds KVM_PGTABLE_LAST_LEVEL + 2 and KVM_RUN fails with -ENXIO.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=10
Re: [PATCH v5 10/11] KVM: arm64: CCA: Support runtime faulting of memory
Quoted 98 lines · On 30/11/2026 14:58, [email protected] wrote:
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [High] KVM: arm64: block mapping over child RTT fails inrealm_map_unprotected()
--commit
23c673437d9940025385704ad240e79450d0dcaa
Author: Steven Price <[email protected]>KVM: arm64: CCA: Support runtime faulting of memory
This commit adds runtime stage-2 fault handling for Realm VMs, mapping private
guest_memfdpages into the protected IPA range via the RMM and sharedguest_memfdor userspace-backed memslot pages into the unprotected IPA range.Quoted 24 lines
arch/arm64/kvm/rmi.c +16
-0@@ -478,6 +478,273 @@ void kvm_realm_unmap_range_filter(struct kvm *kvm, unsigned long start, } } +static unsigned long addr_range_desc (struct realm *realm, +unsigned long ipa,unsigned long phys, +unsigned long size,unsigned long state) +{ +unsigned long count =1 ; +unsigned long out; +int rmi_size; + +for (rmi_size =KVM_PGTABLE_LAST_LEVEL - realm->start_level; + rmi_size >=0 ; rmi_size--) { +unsigned long entry_size =rmi_addr_block_size_to_bytes (rmi_size); + +if (size >= entry_size &&IS_ALIGNED (ipa, entry_size) && +IS_ALIGNED (phys, entry_size)) +break ; + }[ ... ]
Quoted 8 lines
arch/arm64/kvm/rmi.c +8
-0+static int realm_map_unprotected (struct kvm *kvm, +unsigned long ipa, +kvm_pfn_t pfn, +unsigned long size, +enum kvm_pgtable_prot prot, +struct kvm_mmu_memory_cache *memcache, +phys_addr_t *mapped_top) +{[ ... ]
Quoted 22 lines
arch/arm64/kvm/rmi.c +22
-0+while (ipa < ipa_top) { +unsigned long range_desc =addr_range_desc (realm, ipa, phys, + ipa_top - ipa, +RMI_OP_MEM_UNDELEGATED ); +unsigned long call_top = ipa +RMI_ADDR_RANGE_COUNT (range_desc) * +rmi_addr_block_size_to_bytes (RMI_ADDR_RANGE_BLOCK_SIZE (range_desc)); +unsigned long out_top; + + ret =rmi_rtt_unprot_map (rd, ipa, call_top, flags, range_desc, + &out_top); +if (ret <0 ) +return ret; + +if (ret) { +/* Create missing RTTs and retry */ +int level =RMI_RESULT_DATA_LEVEL (ret); +int req_level =KVM_PGTABLE_LAST_LEVEL - +RMI_ADDR_RANGE_BLOCK_SIZE (range_desc); + +if (RMI_RESULT_STATUS (ret) ==RMI_BLOCKED ) +return -EAGAIN ; + if (RMI_RESULT_STATUS(ret) != RMI_ERROR_RTT ||arch/arm64/kvm/rmi.c void kvm_realm_unmap_range_filter(struct kvm *kvm, unsigned long start, · line 696
Quoted 10 lines
arch/arm64/kvm/rmi.c +10
-0+ ++retries >KVM_PGTABLE_LAST_LEVEL +2 || + level < realm->start_level) + return -ENXIO; + +/* Replace an existing mapping to update its permissions. */ +if (level >= req_level) { +realm_unmap_shared_range (kvm, ipa, ipa_top, false, + memcache); +continue ; + }[Severity: High]
Canrealm_map_unprotected()fail with-ENXIOwhen installing a block mapping over an IPA range that already has a deeper child RTT table?When
realm_s2_fault_map()promotesmapping_sizetoPMD_SIZEfor a THP (for example, on a write fault or after khugepaged collapse in a 2MB range where a 4KB page was previously faulted in),addr_range_desc()selects a block mapping atreq_level= 2. Because a level 3 RTT table already exists,rmi_rtt_unprot_map()fails withRMI_ERROR_RTTand returns level >=req_level.
Quoted 6 lines
realm_unmap_shared_range()only callsrmi_rtt_unprot_unmap()to clear leaf entries and does not destroy or fold the empty child RTT table. Each retry therefore attempts the same level 2 block mapping over the still-present table entry until retries exceedsKVM_PGTABLE_LAST_LEVEL+ 2 andKVM_RUNfails with-ENXIO.
This has been raised internally to get some clarification on. The intention is that rmi_rtt_unprot_map() would not fail in this case (it would instead map the entries at level 3). There's an RMM patch to align with this behaviour here:
https://review.trustedfirmware.org/c/TF-RMM/tf-rmm/+/57412
Thanks,
Steve
Re: [PATCH v5 10/11] KVM: arm64: CCA: Support runtime faulting of memory
In reply to Steven PriceHi Steven,
On Mon, 30 Nov 2026 14:25:31 +0000, Steven Price <[email protected]> wrote:
[...]
Quoted 1 line
arch/arm64/include/asm/kvm_mmu.h +0
-0
[...]
arch/arm64/include/asm/kvm_mmu.h
Quoted 4 lines
static inline void
kvm_fault_lock(struct kvm *kvm){arch/arm64/include/asm/kvm_mmu.h +1
-1-if (is_protected_kvm_enabled ()) +if (kvm_vm_hyp_is_distrusting (kvm))
Could you move the conversions from is_protected_kvm_enabled() to kvm_vm_hyp_is_distrusting() into their own patch, with the ptdump one from patch 11? That would make it easier to see that they don't change anything for pKVM VMs.
Cheers,
/fuad
Re: [PATCH v5 10/11] KVM: arm64: CCA: Support runtime faulting of memory
Quoted 11 lines · Fuad Tabba
Hi Steven,
On Mon, 30 Nov 2026 14:25:31 +0000, Steven Price <[email protected]> wrote:
[...]Quoted 1 line
arch/arm64/include/asm/kvm_mmu.h +0
-0[...]
arch/arm64/include/asm/kvm_mmu.h
Quoted 4 lines
static inline void
kvm_fault_lock(struct kvm *kvm){arch/arm64/include/asm/kvm_mmu.h +1
-1-if (is_protected_kvm_enabled ()) +if (kvm_vm_hyp_is_distrusting (kvm))
Quoted 4 lines
Could you move the conversions from
is_protected_kvm_enabled()tokvm_vm_hyp_is_distrusting()into their own patch, with the ptdump one from patch 11? That would make it easier to see that they don't change anything for pKVM VMs.
Good idea, yes that looks much nicer.
Thanks,
Steve
PATCH v5 11/11 KVM: arm64: CCA: Add Realm stage-2 MMU operations
In reply to Steven PriceFrom: Suzuki K Poulose <[email protected]>
Register the Realm stage-2 MMU operations now that the RMM mapping and unmapping helpers are available. Route unmapping through the RMM, and use the Realm-specific fault handler for Realm memory faults.
The RMM handles TLB invalidation when stage-2 mappings change, so the host has no separate TLB flush to perform. Realm stage-2 does not support access flags, including for shared IPAs.
Realm teardown destroys non-root RTTs through RMI, so the Realm stage-2 destruction callback frees only the root RTT pages.
---
Changes since v4:
- Omit Realm stage-2 debugfs dumps because the host cannot access RTTs.
Changes since v3:
- Register realm_mem_abort() as the Realm stage-2 fault handler.
Changes since v2:
- Move common stage-2 destruction dispatch to a separate patch; retain the Realm-specific destruction callback here.
--- arch/arm64/kvm/mmu.c | 47 +++++++++++++++++++++++++++++++++++++++-- arch/arm64/kvm/ptdump.c | 4 ++-- 2 files changed, 47 insertions(+), 4 deletions(-)
arch/arm64/kvm/mmu.c +45 -2
@@ -191,6 +191,12 @@ static int kvm_vm_flush_remote_tlbs(struct kvm *kvm)return 0 ; } +static int realm_flush_remote_tlbs (struct kvm *kvm) +{ +/* Nothing to do here, RMM does the job */ +return 0 ; +} +/** * kvm_arch_flush_remote_tlbs() - flush all VM TLB entries for v7/8 * @kvm: pointer to kvm structure.
@@ -218,6 +224,13 @@ static int kvm_vm_flush_remote_tlbs_range(struct kvm *kvm,return 0 ; } +static int realm_flush_remote_tlbs_range (struct kvm *kvm, +gfn_t gfn,u64 nr_pages) +{ +/* Nothing to do here, RMM does the job */ +return 0 ; +} +int kvm_arch_flush_remote_tlbs_range (struct kvm *kvm,gfn_t gfn,u64 nr_pages) {
@@ -353,6 +366,15 @@ static void no_stage2_unmap_range(struct kvm_s2_mmu *mmu, { } +static void realm_stage2_unmap_range (struct kvm_s2_mmu *mmu, +phys_addr_t start,u64 size,bool may_block, +enum kvm_gfn_range_filter filter) +{ +struct kvm *kvm =kvm_s2_mmu_to_kvm (mmu); + +kvm_realm_unmap_range_filter (kvm, start, size, may_block, filter); +} +/* * Unmapping vs dcache management: *
@@ -1054,6 +1076,17 @@ static void pkvm_stage2_destroy(struct kvm_pgtable *pgt)pkvm_pgtable_stage2_destroy_pgd (pgt); } +static void realm_stage2_destroy (struct kvm_pgtable *pgt) +{ +/* +* Realm RTTs are inaccessible to the host and may contain stale data +* after the RMM has released them. The non-root RTTs are explicitly +* destroyed through RMI before the RD is destroyed; only the root PGD +* pages remain to be freed here. +*/ +kvm_pgtable_stage2_destroy_pgd (pgt); +} +static void kvm_stage2_destroy (struct kvm_pgtable *pgt) {struct kvm *kvm =kvm_s2_mmu_to_kvm (pgt->mmu);
@@ -2478,7 +2511,7 @@ static int kvm_vm_mem_abort(const struct kvm_s2_fault_desc *s2fd)return user_mem_abort (s2fd,NULL , kvm_s2_fault_map); } -static int __maybe_unused realm_mem_abort (const struct kvm_s2_fault_desc *s2fd) +static int realm_mem_abort (const struct kvm_s2_fault_desc *s2fd) {struct kvm *kvm = s2fd->vcpu->kvm;
@@ -3350,6 +3383,16 @@ static const struct kvm_vm_s2_ops kvm_default_vm_s2_ops = { .vm_mem_abort = kvm_vm_mem_abort, }; +static const struct kvm_vm_s2_ops realm_vm_s2_ops = { + .vm_stage2_destroy = realm_stage2_destroy, + .vm_flush_remote_tlbs = realm_flush_remote_tlbs, + .vm_flush_remote_tlbs_range = realm_flush_remote_tlbs_range, + .vm_age_gfn = no_age_gfn, + .vm_test_age_gfn = no_age_gfn, + .vm_stage2_unmap_range = realm_stage2_unmap_range, + .vm_mem_abort = realm_mem_abort, +}; +#define KVM_VM_S2_OPS (flavor, ops) \ [(flavor)] = &(ops)
@@ -3358,7 +3401,7 @@ static const struct kvm_vm_s2_ops *arm64_vm_s2_ops[] = {KVM_VM_S2_OPS (VM_NVHE , kvm_default_vm_s2_ops),KVM_VM_S2_OPS (VM_PKVM , pkvm_vm_s2_ops),KVM_VM_S2_OPS (VM_PROTECTED_PKVM , protected_pkvm_vm_s2_ops), - [VM_REALM] = NULL, + KVM_VM_S2_OPS(VM_REALM, realm_vm_s2_ops), };static int kvm_vm_init_vm_s2_ops (struct kvm *kvm)
arch/arm64/kvm/ptdump.c +2 -2
@@ -321,8 +321,8 @@ void kvm_nested_s2_ptdump_create_debugfs(struct kvm_s2_mmu *mmu, int idx)void kvm_s2_ptdump_create_debugfs (struct kvm *kvm) { -/* pKVM handles the page-tables in the hypervisor */ -if (is_protected_kvm_enabled ()) +/* Protected VMs have stage-2 tables outside the host's access. */ +if (kvm_vm_hyp_is_distrusting (kvm))return ; debugfs_create_file("stage2_page_tables", 0400, kvm->debugfs_dentry,
Signature · 1 line
2.43.0
Quoted 10 lines · Steven Price
This series adds the memory management plumbing for CCA, building on the basic RMM support [1]. Thanks to everyone who reviewed and tested the earlier versions.
The patches introduce internal Realm VM state, prepare Realm stage-2 setup and teardown, and provide RTT mapping and invalidation helpers. Realm memslots are restricted to the lower half of the IPA space because the protected and shared halves alias each other. The final patch connects the helpers to KVM's stage-2 MMU operations.
Quoted 8 lines
This is the memory management portion of the larger Realm host series. Realm descriptor creation, REC entry and exit, activation, and the new uAPI follow in the integration stack. This series alone does not enable Realm creation.
The integration branch [2] includes Realm creation, REC entry and exit, and the remaining host support on top of this series. The MM branch [3] contains just this series.
Hi Steve,
I've tested the v5 integration branch on our real hardware, and I haven't seen any issues so far.
For the series:
I'd also like to review the series if time permits, but unfortunately I've only had enough bandwidth for testing so far :D
Quoted 20 lines · Kohei Enju
Quoted 10 lines · Steven Price
This series adds the memory management plumbing for CCA, building on the basic RMM support [1]. Thanks to everyone who reviewed and tested the earlier versions.
The patches introduce internal Realm VM state, prepare Realm stage-2 setup and teardown, and provide RTT mapping and invalidation helpers. Realm memslots are restricted to the lower half of the IPA space because the protected and shared halves alias each other. The final patch connects the helpers to KVM's stage-2 MMU operations.
Quoted 8 lines
This is the memory management portion of the larger Realm host series. Realm descriptor creation, REC entry and exit, activation, and the new uAPI follow in the integration stack. This series alone does not enable Realm creation.
The integration branch [2] includes Realm creation, REC entry and exit, and the remaining host support on top of this series. The MM branch [3] contains just this series.
Quoted 7 lines
Hi Steve,
I've tested the v5 integration branch on our real hardware, and I haven't seen any issues so far.
For the series:
Tested-by Kohei Enju <[email protected]>
Thanks!
Quoted 2 lines
I'd also like to review the series if time permits, but unfortunately I've only had enough bandwidth for testing so far :D
No worries - having someone test this on real hardware is very valuable.
Thanks,
Steve