* [PATCH v6 00/11] KVM: arm64: CCA: Prepare Realm memory management
@ 2026-12-04 11:57 Steven Price
2026-12-04 11:57 ` [PATCH v6 01/11] KVM: arm64: CCA: Introduce Realms Steven Price
` (11 more replies)
0 siblings, 12 replies; 26+ messages in thread
From: Steven Price @ 2026-12-04 11:57 UTC (permalink / raw)
To: kvm, kvmarm
Cc: Steven Price, Catalin Marinas, Marc Zyngier, Will Deacon,
James Morse, Oliver Upton, Suzuki K Poulose, Zenghui Yu,
linux-arm-kernel, linux-kernel, Joey Gouly, Alexandru Elisei,
Christoffer Dall, Fuad Tabba, linux-coco, Ganapatrao Kulkarni,
Gavin Shan, Shanker Donthineni, Alper Gun, Aneesh Kumar K . V,
Emi Kisanuki, Vishal Annapurve, WeiLin.Chang, Lorenzo Pieralisi,
Kohei Enju
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 v5:
- Drop the guest_memfd mmap patch altogether.
- Move RTT teardown after the RTT mapping and invalidation helpers.
Private invalidation can top up its local cache after dropping
mmu_lock, then restart the RTT walk. Shared invalidation retains
atomic splits and its block-unmap fallback.
- Move protected VM fault lock, cache, pre-fault and stage-2 debugfs
decisions into a separate VM flavour patch. The Realm fault and
stage-2 operations patches now focus on Realm-specific behaviour.
- Add Reviewed-by tags from Suzuki K Poulose and Fuad Tabba.
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/20261007073537.2454351-1-suzuki.poulose@arm.com/
[2] https://gitlab.arm.com/linux-arm/linux-cca
branch: cca-host-mm/v6/integration
[3] https://gitlab.arm.com/linux-arm/linux-cca
branch: cca-host-mm/v6/mm
Steven Price (7):
KVM: arm64: Dispatch stage-2 destruction by VM flavor
KVM: arm64: CCA: Add basic infrastructure for creating a realm
KVM: arm64: CCA: Add Realm RTT mapping and invalidation helpers
KVM: arm64: CCA: Destroy RTTs
KVM: arm64: CCA: Advertise private memory support for Realms
KVM: arm64: Select protected VM behaviour by VM flavour
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 | 4 +-
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 | 306 +++++++++-
arch/arm64/kvm/pkvm.c | 2 +-
arch/arm64/kvm/psci.c | 3 +-
arch/arm64/kvm/ptdump.c | 4 +-
arch/arm64/kvm/rmi.c | 820 +++++++++++++++++++++++++++
drivers/firmware/arm_rmm/rmi.c | 10 -
include/linux/arm-rmi-cmds.h | 8 +
14 files changed, 1280 insertions(+), 56 deletions(-)
create mode 100644 arch/arm64/include/asm/kvm_rmi.h
create mode 100644 arch/arm64/kvm/rmi.c
--
2.43.0
^ permalink raw reply [flat|nested] 26+ messages in thread
* [PATCH v6 01/11] KVM: arm64: CCA: Introduce Realms
2026-12-04 11:57 [PATCH v6 00/11] KVM: arm64: CCA: Prepare Realm memory management Steven Price
@ 2026-12-04 11:57 ` Steven Price
2026-12-04 16:07 ` Kohei Enju
2026-12-04 11:57 ` [PATCH v6 02/11] KVM: arm64: Pass range filter to stage2_unmap_range Steven Price
` (10 subsequent siblings)
11 siblings, 1 reply; 26+ messages in thread
From: Steven Price @ 2026-12-04 11:57 UTC (permalink / raw)
To: kvm, kvmarm
Cc: Suzuki K Poulose, Catalin Marinas, Marc Zyngier, Will Deacon,
James Morse, Oliver Upton, Zenghui Yu, linux-arm-kernel,
linux-kernel, Joey Gouly, Alexandru Elisei, Christoffer Dall,
Fuad Tabba, linux-coco, Ganapatrao Kulkarni, Gavin Shan,
Shanker Donthineni, Alper Gun, Aneesh Kumar K . V, Emi Kisanuki,
Vishal Annapurve, WeiLin.Chang, Lorenzo Pieralisi, Kohei Enju,
Steven Price, Fuad Tabba
From: Suzuki K Poulose <suzuki.poulose@arm.com>
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.
Signed-off-by: Suzuki K Poulose <suzuki.poulose@arm.com>
Signed-off-by: Steven Price <steven.price@arm.com>
Reviewed-by: Fuad Tabba <fuad.tabba@linux.dev>
---
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
diff --git a/arch/arm64/include/asm/kvm_emulate.h b/arch/arm64/include/asm/kvm_emulate.h
index 334211b8b2ee..32021bdf2610 100644
--- a/arch/arm64/include/asm/kvm_emulate.h
+++ b/arch/arm64/include/asm/kvm_emulate.h
@@ -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__ */
diff --git a/arch/arm64/include/asm/kvm_host.h b/arch/arm64/include/asm/kvm_host.h
index 97332ffcb130..8438251e1d1b 100644
--- a/arch/arm64/include/asm/kvm_host.h
+++ b/arch/arm64/include/asm/kvm_host.h
@@ -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))
diff --git a/arch/arm64/include/asm/kvm_rmi.h b/arch/arm64/include/asm/kvm_rmi.h
new file mode 100644
index 000000000000..67f16793b4d0
--- /dev/null
+++ b/arch/arm64/include/asm/kvm_rmi.h
@@ -0,0 +1,59 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * Copyright (C) 2023-2026 ARM Ltd.
+ */
+
+#ifndef __ASM_KVM_RMI_H
+#define __ASM_KVM_RMI_H
+
+/**
+ * enum realm_state - State of a Realm
+ *
+ * Mirrors the RMM's Realm lifecycle states where they are meaningful to KVM,
+ * with REALM_STATE_DYING being a KVM-internal state used to prevent further
+ * requests while teardown is in progress. KVM does not track REALM_SYSTEM_OFF
+ * or REALM_ZOMBIE separately as they naturally lead to teardown.
+ */
+enum realm_state {
+ /**
+ * @REALM_STATE_NONE:
+ * Realm has not yet been created. rmi_realm_create() has not
+ * yet been called.
+ */
+ REALM_STATE_NONE,
+ /**
+ * @REALM_STATE_NEW:
+ * Realm is under construction, Realm Descriptor has been created,
+ * but it is not yet activated. Memory may be populated.
+ */
+ REALM_STATE_NEW,
+ /**
+ * @REALM_STATE_ACTIVE:
+ * Realm has been created and is eligible for execution with
+ * rmi_rec_enter(). Memory may no longer be populated with known
+ * contents.
+ */
+ REALM_STATE_ACTIVE,
+ /**
+ * @REALM_STATE_DYING:
+ * Realm is in the process of being destroyed or has already been
+ * destroyed.
+ */
+ REALM_STATE_DYING,
+ /**
+ * @REALM_STATE_DEAD:
+ * Realm has been destroyed.
+ */
+ REALM_STATE_DEAD
+};
+
+/**
+ * struct realm - Additional per VM data for a Realm
+ *
+ * @state: The lifetime state machine for the realm
+ */
+struct realm {
+ enum realm_state state;
+};
+
+#endif /* __ASM_KVM_RMI_H */
diff --git a/arch/arm64/kvm/arm.c b/arch/arm64/kvm/arm.c
index 157bf4b81202..d79e5451923d 100644
--- a/arch/arm64/kvm/arm.c
+++ b/arch/arm64/kvm/arm.c
@@ -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)
diff --git a/arch/arm64/kvm/mmu.c b/arch/arm64/kvm/mmu.c
index 1db10d8d6db7..7ebc8959d6fd 100644
--- a/arch/arm64/kvm/mmu.c
+++ b/arch/arm64/kvm/mmu.c
@@ -3165,6 +3165,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)
diff --git a/arch/arm64/kvm/pkvm.c b/arch/arm64/kvm/pkvm.c
index 016997288fd2..8241a05e25d1 100644
--- a/arch/arm64/kvm/pkvm.c
+++ b/arch/arm64/kvm/pkvm.c
@@ -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);
diff --git a/arch/arm64/kvm/psci.c b/arch/arm64/kvm/psci.c
index b01f8d44033f..d14414d66dd5 100644
--- a/arch/arm64/kvm/psci.c
+++ b/arch/arm64/kvm/psci.c
@@ -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
--
2.43.0
^ permalink raw reply related [flat|nested] 26+ messages in thread
* [PATCH v6 02/11] KVM: arm64: Pass range filter to stage2_unmap_range
2026-12-04 11:57 [PATCH v6 00/11] KVM: arm64: CCA: Prepare Realm memory management Steven Price
2026-12-04 11:57 ` [PATCH v6 01/11] KVM: arm64: CCA: Introduce Realms Steven Price
@ 2026-12-04 11:57 ` Steven Price
2026-12-04 11:57 ` [PATCH v6 03/11] KVM: arm64: Dispatch stage-2 destruction by VM flavor Steven Price
` (9 subsequent siblings)
11 siblings, 0 replies; 26+ messages in thread
From: Steven Price @ 2026-12-04 11:57 UTC (permalink / raw)
To: kvm, kvmarm
Cc: Suzuki K Poulose, Catalin Marinas, Marc Zyngier, Will Deacon,
James Morse, Oliver Upton, Zenghui Yu, linux-arm-kernel,
linux-kernel, Joey Gouly, Alexandru Elisei, Christoffer Dall,
Fuad Tabba, linux-coco, Ganapatrao Kulkarni, Gavin Shan,
Shanker Donthineni, Alper Gun, Aneesh Kumar K . V, Emi Kisanuki,
Vishal Annapurve, WeiLin.Chang, Lorenzo Pieralisi, Kohei Enju,
Steven Price, Fuad Tabba
From: Suzuki K Poulose <suzuki.poulose@arm.com>
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.
Signed-off-by: Suzuki K Poulose <suzuki.poulose@arm.com>
Signed-off-by: Steven Price <steven.price@arm.com>
Reviewed-by: Fuad Tabba <fuad.tabba@linux.dev>
---
arch/arm64/include/asm/kvm_host.h | 4 +++-
arch/arm64/kvm/mmu.c | 31 +++++++++++++++++++++++--------
2 files changed, 26 insertions(+), 9 deletions(-)
diff --git a/arch/arm64/include/asm/kvm_host.h b/arch/arm64/include/asm/kvm_host.h
index 8438251e1d1b..9d6258b213d1 100644
--- a/arch/arm64/include/asm/kvm_host.h
+++ b/arch/arm64/include/asm/kvm_host.h
@@ -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);
};
diff --git a/arch/arm64/kvm/mmu.c b/arch/arm64/kvm/mmu.c
index 7ebc8959d6fd..1c72c7a622b3 100644
--- a/arch/arm64/kvm/mmu.c
+++ b/arch/arm64/kvm/mmu.c
@@ -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;
--
2.43.0
^ permalink raw reply related [flat|nested] 26+ messages in thread
* [PATCH v6 03/11] KVM: arm64: Dispatch stage-2 destruction by VM flavor
2026-12-04 11:57 [PATCH v6 00/11] KVM: arm64: CCA: Prepare Realm memory management Steven Price
2026-12-04 11:57 ` [PATCH v6 01/11] KVM: arm64: CCA: Introduce Realms Steven Price
2026-12-04 11:57 ` [PATCH v6 02/11] KVM: arm64: Pass range filter to stage2_unmap_range Steven Price
@ 2026-12-04 11:57 ` Steven Price
2026-12-04 11:57 ` [PATCH v6 04/11] KVM: arm64: CCA: Add basic infrastructure for creating a realm Steven Price
` (8 subsequent siblings)
11 siblings, 0 replies; 26+ messages in thread
From: Steven Price @ 2026-12-04 11:57 UTC (permalink / raw)
To: kvm, kvmarm
Cc: Steven Price, Catalin Marinas, Marc Zyngier, Will Deacon,
James Morse, Oliver Upton, Suzuki K Poulose, Zenghui Yu,
linux-arm-kernel, linux-kernel, Joey Gouly, Alexandru Elisei,
Christoffer Dall, Fuad Tabba, linux-coco, Ganapatrao Kulkarni,
Gavin Shan, Shanker Donthineni, Alper Gun, Aneesh Kumar K . V,
Emi Kisanuki, Vishal Annapurve, WeiLin.Chang, Lorenzo Pieralisi,
Kohei Enju, Fuad Tabba
Select 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.
Reviewed-by: Suzuki K Poulose <suzuki.poulose@arm.com>
Reviewed-by: Fuad Tabba <fuad.tabba@linux.dev>
Signed-off-by: Steven Price <steven.price@arm.com>
---
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(-)
diff --git a/arch/arm64/include/asm/kvm_host.h b/arch/arm64/include/asm/kvm_host.h
index 9d6258b213d1..3ddeddfacd06 100644
--- a/arch/arm64/include/asm/kvm_host.h
+++ b/arch/arm64/include/asm/kvm_host.h
@@ -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,
diff --git a/arch/arm64/kvm/mmu.c b/arch/arm64/kvm/mmu.c
index 1c72c7a622b3..df79d7dfb720 100644
--- a/arch/arm64/kvm/mmu.c
+++ b/arch/arm64/kvm/mmu.c
@@ -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);
}
/**
@@ -3146,6 +3159,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,
@@ -3155,6 +3169,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,
@@ -3164,6 +3179,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,
--
2.43.0
^ permalink raw reply related [flat|nested] 26+ messages in thread
* [PATCH v6 04/11] KVM: arm64: CCA: Add basic infrastructure for creating a realm
2026-12-04 11:57 [PATCH v6 00/11] KVM: arm64: CCA: Prepare Realm memory management Steven Price
` (2 preceding siblings ...)
2026-12-04 11:57 ` [PATCH v6 03/11] KVM: arm64: Dispatch stage-2 destruction by VM flavor Steven Price
@ 2026-12-04 11:57 ` Steven Price
2026-12-04 12:15 ` sashiko-bot
2026-12-04 11:57 ` [PATCH v6 05/11] KVM: arm64: CCA: Add Realm RTT mapping and invalidation helpers Steven Price
` (7 subsequent siblings)
11 siblings, 1 reply; 26+ messages in thread
From: Steven Price @ 2026-12-04 11:57 UTC (permalink / raw)
To: kvm, kvmarm
Cc: Steven Price, Catalin Marinas, Marc Zyngier, Will Deacon,
James Morse, Oliver Upton, Suzuki K Poulose, Zenghui Yu,
linux-arm-kernel, linux-kernel, Joey Gouly, Alexandru Elisei,
Christoffer Dall, Fuad Tabba, linux-coco, Ganapatrao Kulkarni,
Gavin Shan, Shanker Donthineni, Alper Gun, Aneesh Kumar K . V,
Emi Kisanuki, Vishal Annapurve, WeiLin.Chang, Lorenzo Pieralisi,
Kohei Enju
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 <steven.price@arm.com>
Reviewed-by: Suzuki K Poulose <suzuki.poulose@arm.com>
---
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
diff --git a/arch/arm64/include/asm/kvm_rmi.h b/arch/arm64/include/asm/kvm_rmi.h
index 67f16793b4d0..4ac1af622872 100644
--- a/arch/arm64/include/asm/kvm_rmi.h
+++ b/arch/arm64/include/asm/kvm_rmi.h
@@ -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 */
diff --git a/arch/arm64/kvm/Kconfig b/arch/arm64/kvm/Kconfig
index cecea0820e52..d067063a3686 100644
--- a/arch/arm64/kvm/Kconfig
+++ b/arch/arm64/kvm/Kconfig
@@ -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.
diff --git a/arch/arm64/kvm/Makefile b/arch/arm64/kvm/Makefile
index 7aaeeb84e788..66a719ab5b6b 100644
--- a/arch/arm64/kvm/Makefile
+++ b/arch/arm64/kvm/Makefile
@@ -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 \
diff --git a/arch/arm64/kvm/arm.c b/arch/arm64/kvm/arm.c
index d79e5451923d..7c22d9474a38 100644
--- a/arch/arm64/kvm/arm.c
+++ b/arch/arm64/kvm/arm.c
@@ -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)
diff --git a/arch/arm64/kvm/mmu.c b/arch/arm64/kvm/mmu.c
index df79d7dfb720..5e34555d4fa6 100644
--- a/arch/arm64/kvm/mmu.c
+++ b/arch/arm64/kvm/mmu.c
@@ -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 t
return -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 t
if (!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;
diff --git a/arch/arm64/kvm/rmi.c b/arch/arm64/kvm/rmi.c
new file mode 100644
index 000000000000..6a8bff1d8d46
--- /dev/null
+++ b/arch/arm64/kvm/rmi.c
@@ -0,0 +1,58 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Copyright (C) 2023-2026 ARM Ltd.
+ */
+
+#include <linux/arm-rmi-cmds.h>
+#include <linux/kvm_host.h>
+
+#include <asm/kvm_emulate.h>
+#include <asm/kvm_mmu.h>
+#include <asm/kvm_pgtable.h>
+#include <asm/virt.h>
+
+u32 kvm_rmm_ipa_limit(void)
+{
+ return u64_get_bits(rmi_feat_reg(0), RMI_FEATURE_REGISTER_0_S2SZ);
+}
+
+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);
+
+ if (!kvm_realm_is_created(kvm))
+ goto out_free_sro;
+
+ kvm_set_realm_state(kvm, REALM_STATE_DYING);
+
+ if (WARN_ON(rmi_realm_terminate(realm->rd_phys, realm->sro)))
+ 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);
+
+ if (WARN_ON(rmi_undelegate_range(kvm->arch.mmu.pgd_phys,
+ pgd_size)))
+ goto out_free_sro;
+
+ kvm_set_realm_state(kvm, REALM_STATE_DEAD);
+
+out_free_sro:
+ kfree(realm->sro);
+ realm->sro = NULL;
+}
+
+int kvm_init_realm(struct kvm *kvm)
+{
+ struct realm *realm = &kvm->arch.realm;
+
+ realm->ia_bits = VTCR_EL2_IPA(kvm->arch.mmu.vtcr);
+ realm->start_level = 4 - VTCR_EL2_LVLS(kvm->arch.mmu.vtcr);
+ realm->sro = kmalloc_obj(*realm->sro);
+ if (!realm->sro)
+ return -ENOMEM;
+
+ return 0;
+}
--
2.43.0
^ permalink raw reply related [flat|nested] 26+ messages in thread
* [PATCH v6 05/11] KVM: arm64: CCA: Add Realm RTT mapping and invalidation helpers
2026-12-04 11:57 [PATCH v6 00/11] KVM: arm64: CCA: Prepare Realm memory management Steven Price
` (3 preceding siblings ...)
2026-12-04 11:57 ` [PATCH v6 04/11] KVM: arm64: CCA: Add basic infrastructure for creating a realm Steven Price
@ 2026-12-04 11:57 ` Steven Price
2026-12-04 12:28 ` sashiko-bot
2026-12-04 11:57 ` [PATCH v6 06/11] KVM: arm64: CCA: Destroy RTTs Steven Price
` (6 subsequent siblings)
11 siblings, 1 reply; 26+ messages in thread
From: Steven Price @ 2026-12-04 11:57 UTC (permalink / raw)
To: kvm, kvmarm
Cc: Steven Price, Catalin Marinas, Marc Zyngier, Will Deacon,
James Morse, Oliver Upton, Suzuki K Poulose, Zenghui Yu,
linux-arm-kernel, linux-kernel, Joey Gouly, Alexandru Elisei,
Christoffer Dall, Fuad Tabba, linux-coco, Ganapatrao Kulkarni,
Gavin Shan, Shanker Donthineni, Alper Gun, Aneesh Kumar K . V,
Emi Kisanuki, Vishal Annapurve, WeiLin.Chang, Lorenzo Pieralisi,
Kohei Enju
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.
Signed-off-by: Steven Price <steven.price@arm.com>
---
Changes since v5:
- Move RTT destruction into the following patch so creation and
invalidation helpers precede teardown. Allow private unmap to top up
its cache after dropping mmu_lock, then retry the RTT walk.
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 | 331 +++++++++++++++++++++++++++++++
drivers/firmware/arm_rmm/rmi.c | 10 -
include/linux/arm-rmi-cmds.h | 8 +
4 files changed, 347 insertions(+), 10 deletions(-)
diff --git a/arch/arm64/include/asm/kvm_rmi.h b/arch/arm64/include/asm/kvm_rmi.h
index 4ac1af622872..8d23d542bdd0 100644
--- a/arch/arm64/include/asm/kvm_rmi.h
+++ b/arch/arm64/include/asm/kvm_rmi.h
@@ -79,4 +79,12 @@ u32 kvm_rmm_ipa_limit(void);
int kvm_init_realm(struct kvm *kvm);
void kvm_destroy_realm(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 */
diff --git a/arch/arm64/kvm/rmi.c b/arch/arm64/kvm/rmi.c
index 6a8bff1d8d46..ee16f5364b9a 100644
--- a/arch/arm64/kvm/rmi.c
+++ b/arch/arm64/kvm/rmi.c
@@ -16,6 +16,337 @@ 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);
+ if (rmi_free_delegated_page(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);
+}
+
+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_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 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);
+
+ *lock_dropped = true;
+ 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)
+{
+ struct realm *realm = &kvm->arch.realm;
+ unsigned long rd = realm->rd_phys;
+ unsigned long next_addr, addr;
+ unsigned long shared_bit = BIT(realm->ia_bits - 1);
+
+ if (start >= end)
+ return;
+
+ /*
+ * Callers may pass either an IPA range or its shared alias. Add the
+ * shared bit only for the former. Use addition so an exclusive end at
+ * shared_bit advances to the end of the IPA space.
+ */
+ if (start < shared_bit) {
+ start += shared_bit;
+ end += shared_bit;
+ }
+
+ for (addr = start; addr < end; addr = next_addr) {
+ long ret;
+
+ ret = rmi_rtt_unprot_unmap(rd, addr, end, RMI_ADDR_TYPE_NONE,
+ 0, &next_addr, NULL, NULL);
+ if (WARN_ON(ret < 0))
+ return;
+
+ switch (RMI_RESULT_STATUS(ret)) {
+ case RMI_SUCCESS:
+ break;
+ 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 */
+ next_addr = ALIGN(addr + 1,
+ kvm_granule_size(err_level));
+ break;
+ }
+
+ 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)
+{
+ 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) {
+ unsigned long out_range;
+ unsigned long flags = RMI_ADDR_TYPE_SINGLE;
+ /* TODO: Optimise using RMI_ADDR_TYPE_LIST */
+
+ ret = rmi_rtt_data_unmap(rd, addr, end, flags, 0,
+ &next_addr, &out_range, NULL);
+ if (WARN_ON(ret < 0))
+ return;
+
+ switch (RMI_RESULT_STATUS(ret)) {
+ case RMI_SUCCESS:
+ ret = undelegate_range_desc(out_range);
+ if (WARN_ON(ret))
+ return;
+ break;
+ 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 (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,
+ level, mc);
+ 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;
+ }
+ default:
+ WARN_ON(1);
+ return;
+ }
+
+ if (may_block)
+ cond_resched_rwlock_write(&kvm->mmu_lock);
+ }
+}
+
+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 };
+ unsigned long end = start + size;
+ struct realm *realm = &kvm->arch.realm;
+
+ if (!kvm_realm_is_created(kvm))
+ return;
+
+ end = min(BIT(realm->ia_bits - 1), end);
+
+ if (attr_filter & KVM_FILTER_SHARED)
+ realm_unmap_shared_range(kvm, start, end, may_block, NULL);
+ if (attr_filter & KVM_FILTER_PRIVATE)
+ realm_unmap_private_range(kvm, start, end, may_block, &cache);
+
+ if (cache.objects) {
+ write_unlock(&kvm->mmu_lock);
+ kvm_mmu_free_memory_cache(&cache);
+ write_lock(&kvm->mmu_lock);
+ }
+}
+
void kvm_destroy_realm(struct kvm *kvm)
{
struct realm *realm = &kvm->arch.realm;
diff --git a/drivers/firmware/arm_rmm/rmi.c b/drivers/firmware/arm_rmm/rmi.c
index 77255a90e0a6..a9f42c9a4f79 100644
--- a/drivers/firmware/arm_rmm/rmi.c
+++ b/drivers/firmware/arm_rmm/rmi.c
@@ -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.
diff --git a/include/linux/arm-rmi-cmds.h b/include/linux/arm-rmi-cmds.h
index a84e50ac851c..1a885f1017df 100644
--- a/include/linux/arm-rmi-cmds.h
+++ b/include/linux/arm-rmi-cmds.h
@@ -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;
--
2.43.0
^ permalink raw reply related [flat|nested] 26+ messages in thread
* [PATCH v6 06/11] KVM: arm64: CCA: Destroy RTTs
2026-12-04 11:57 [PATCH v6 00/11] KVM: arm64: CCA: Prepare Realm memory management Steven Price
` (4 preceding siblings ...)
2026-12-04 11:57 ` [PATCH v6 05/11] KVM: arm64: CCA: Add Realm RTT mapping and invalidation helpers Steven Price
@ 2026-12-04 11:57 ` Steven Price
2026-12-04 12:28 ` sashiko-bot
2026-12-04 11:57 ` [PATCH v6 07/11] KVM: arm64: CCA: Restrict Realm memslots to the lower IPA half Steven Price
` (5 subsequent siblings)
11 siblings, 1 reply; 26+ messages in thread
From: Steven Price @ 2026-12-04 11:57 UTC (permalink / raw)
To: kvm, kvmarm
Cc: Steven Price, Catalin Marinas, Marc Zyngier, Will Deacon,
James Morse, Oliver Upton, Suzuki K Poulose, Zenghui Yu,
linux-arm-kernel, linux-kernel, Joey Gouly, Alexandru Elisei,
Christoffer Dall, Fuad Tabba, linux-coco, Ganapatrao Kulkarni,
Gavin Shan, Shanker Donthineni, Alper Gun, Aneesh Kumar K . V,
Emi Kisanuki, Vishal Annapurve, WeiLin.Chang, Lorenzo Pieralisi,
Kohei Enju
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_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.
Signed-off-by: Steven Price <steven.price@arm.com>
---
Changes since v5:
- Place RTT teardown after the mapping helpers it calls. Use the
Realm unmap helper directly during destruction.
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_rmi.h | 3 +
arch/arm64/kvm/mmu.c | 17 +++-
arch/arm64/kvm/rmi.c | 164 +++++++++++++++++++++++++++++++
3 files changed, 183 insertions(+), 1 deletion(-)
diff --git a/arch/arm64/include/asm/kvm_rmi.h b/arch/arm64/include/asm/kvm_rmi.h
index 8d23d542bdd0..d23ec620195b 100644
--- a/arch/arm64/include/asm/kvm_rmi.h
+++ b/arch/arm64/include/asm/kvm_rmi.h
@@ -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,12 +73,14 @@ 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);
enum kvm_gfn_range_filter;
diff --git a/arch/arm64/kvm/mmu.c b/arch/arm64/kvm/mmu.c
index 5e34555d4fa6..37adc20f0042 100644
--- a/arch/arm64/kvm/mmu.c
+++ b/arch/arm64/kvm/mmu.c
@@ -1149,9 +1149,24 @@ int kvm_init_stage2_mmu(struct kvm *kvm, struct kvm_s2_mmu *mmu, unsigned long t
return 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);
}
diff --git a/arch/arm64/kvm/rmi.c b/arch/arm64/kvm/rmi.c
index ee16f5364b9a..f176ea9184d7 100644
--- a/arch/arm64/kvm/rmi.c
+++ b/arch/arm64/kvm/rmi.c
@@ -102,6 +102,30 @@ static long realm_rtt_create(struct realm *realm,
return rmi_rtt_create(realm->rd_phys, phys, addr, level);
}
+/*
+ * 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_create_rtt_levels(struct realm *realm,
unsigned long ipa,
int level,
@@ -143,6 +167,125 @@ static int realm_create_rtt_levels(struct realm *realm,
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_realm_unmap_range_filter(kvm, 0,
+ BIT(realm->ia_bits - 1), true,
+ KVM_FILTER_PRIVATE);
+ write_unlock(&kvm->mmu_lock);
+
+ realm->stage2_unmapped = true;
+}
+
static int realm_unmap_cache_topup(struct kvm *kvm,
struct kvm_mmu_memory_cache *mc,
unsigned int needed, bool *lock_dropped)
@@ -352,14 +495,32 @@ 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);
@@ -370,6 +531,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;
--
2.43.0
^ permalink raw reply related [flat|nested] 26+ messages in thread
* [PATCH v6 07/11] KVM: arm64: CCA: Restrict Realm memslots to the lower IPA half
2026-12-04 11:57 [PATCH v6 00/11] KVM: arm64: CCA: Prepare Realm memory management Steven Price
` (5 preceding siblings ...)
2026-12-04 11:57 ` [PATCH v6 06/11] KVM: arm64: CCA: Destroy RTTs Steven Price
@ 2026-12-04 11:57 ` Steven Price
2026-12-04 15:48 ` Kohei Enju
2026-12-04 11:57 ` [PATCH v6 08/11] KVM: arm64: CCA: Advertise private memory support for Realms Steven Price
` (4 subsequent siblings)
11 siblings, 1 reply; 26+ messages in thread
From: Steven Price @ 2026-12-04 11:57 UTC (permalink / raw)
To: kvm, kvmarm
Cc: Suzuki K Poulose, Catalin Marinas, Marc Zyngier, Will Deacon,
James Morse, Oliver Upton, Zenghui Yu, linux-arm-kernel,
linux-kernel, Joey Gouly, Alexandru Elisei, Christoffer Dall,
Fuad Tabba, linux-coco, Ganapatrao Kulkarni, Gavin Shan,
Shanker Donthineni, Alper Gun, Aneesh Kumar K . V, Emi Kisanuki,
Vishal Annapurve, WeiLin.Chang, Lorenzo Pieralisi, Kohei Enju,
Steven Price, Fuad Tabba
From: Suzuki K Poulose <suzuki.poulose@arm.com>
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.
Signed-off-by: Suzuki K Poulose <suzuki.poulose@arm.com>
Signed-off-by: Steven Price <steven.price@arm.com>
Reviewed-by: Fuad Tabba <fuad.tabba@linux.dev>
---
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(-)
diff --git a/arch/arm64/kvm/mmu.c b/arch/arm64/kvm/mmu.c
index 37adc20f0042..ce7cea447f58 100644
--- a/arch/arm64/kvm/mmu.c
+++ b/arch/arm64/kvm/mmu.c
@@ -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;
/*
--
2.43.0
^ permalink raw reply related [flat|nested] 26+ messages in thread
* [PATCH v6 08/11] KVM: arm64: CCA: Advertise private memory support for Realms
2026-12-04 11:57 [PATCH v6 00/11] KVM: arm64: CCA: Prepare Realm memory management Steven Price
` (6 preceding siblings ...)
2026-12-04 11:57 ` [PATCH v6 07/11] KVM: arm64: CCA: Restrict Realm memslots to the lower IPA half Steven Price
@ 2026-12-04 11:57 ` Steven Price
2026-12-04 12:23 ` sashiko-bot
2026-12-04 11:57 ` [PATCH v6 09/11] KVM: arm64: Select protected VM behaviour by VM flavour Steven Price
` (3 subsequent siblings)
11 siblings, 1 reply; 26+ messages in thread
From: Steven Price @ 2026-12-04 11:57 UTC (permalink / raw)
To: kvm, kvmarm
Cc: Steven Price, Catalin Marinas, Marc Zyngier, Will Deacon,
James Morse, Oliver Upton, Suzuki K Poulose, Zenghui Yu,
linux-arm-kernel, linux-kernel, Joey Gouly, Alexandru Elisei,
Christoffer Dall, Fuad Tabba, linux-coco, Ganapatrao Kulkarni,
Gavin Shan, Shanker Donthineni, Alper Gun, Aneesh Kumar K . V,
Emi Kisanuki, Vishal Annapurve, WeiLin.Chang, Lorenzo Pieralisi,
Kohei Enju
Define 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.
Signed-off-by: Steven Price <steven.price@arm.com>
Reviewed-by: Suzuki K Poulose <suzuki.poulose@arm.com>
---
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(+)
diff --git a/arch/arm64/include/asm/kvm_host.h b/arch/arm64/include/asm/kvm_host.h
index 3ddeddfacd06..55673d08e3eb 100644
--- a/arch/arm64/include/asm/kvm_host.h
+++ b/arch/arm64/include/asm/kvm_host.h
@@ -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))
--
2.43.0
^ permalink raw reply related [flat|nested] 26+ messages in thread
* [PATCH v6 09/11] KVM: arm64: Select protected VM behaviour by VM flavour
2026-12-04 11:57 [PATCH v6 00/11] KVM: arm64: CCA: Prepare Realm memory management Steven Price
` (7 preceding siblings ...)
2026-12-04 11:57 ` [PATCH v6 08/11] KVM: arm64: CCA: Advertise private memory support for Realms Steven Price
@ 2026-12-04 11:57 ` Steven Price
2026-12-04 13:21 ` Fuad Tabba
2026-12-04 11:57 ` [PATCH v6 10/11] KVM: arm64: CCA: Support runtime faulting of memory Steven Price
` (2 subsequent siblings)
11 siblings, 1 reply; 26+ messages in thread
From: Steven Price @ 2026-12-04 11:57 UTC (permalink / raw)
To: kvm, kvmarm
Cc: Steven Price, Catalin Marinas, Marc Zyngier, Will Deacon,
James Morse, Oliver Upton, Suzuki K Poulose, Zenghui Yu,
linux-arm-kernel, linux-kernel, Joey Gouly, Alexandru Elisei,
Christoffer Dall, Fuad Tabba, linux-coco, Ganapatrao Kulkarni,
Gavin Shan, Shanker Donthineni, Alper Gun, Aneesh Kumar K . V,
Emi Kisanuki, Vishal Annapurve, WeiLin.Chang, Lorenzo Pieralisi,
Kohei Enju
Use the VM flavour when choosing the stage-2 fault lock, fault memory
cache, pre-fault policy and ptdump visibility. This prepares these paths
for Realm VMs, whose stage-2 tables are managed outside the host.
Move the VM flavour helper outside the nVHE guard so the shared
fault-lock helpers can use it.
Signed-off-by: Steven Price <steven.price@arm.com>
---
Changes since v5:
- New patch: select the fault lock, cache, pre-fault policy and
stage-2 debugfs visibility by VM flavour.
---
arch/arm64/include/asm/kvm_host.h | 4 ++--
arch/arm64/include/asm/kvm_mmu.h | 4 ++--
arch/arm64/kvm/mmu.c | 4 ++--
arch/arm64/kvm/ptdump.c | 4 ++--
4 files changed, 8 insertions(+), 8 deletions(-)
diff --git a/arch/arm64/include/asm/kvm_host.h b/arch/arm64/include/asm/kvm_host.h
index 55673d08e3eb..505c0c5b7dbb 100644
--- a/arch/arm64/include/asm/kvm_host.h
+++ b/arch/arm64/include/asm/kvm_host.h
@@ -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) \
diff --git a/arch/arm64/include/asm/kvm_mmu.h b/arch/arm64/include/asm/kvm_mmu.h
index 32392abf2e21..a70f8bd4b2b9 100644
--- a/arch/arm64/include/asm/kvm_mmu.h
+++ b/arch/arm64/include/asm/kvm_mmu.h
@@ -371,7 +371,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);
@@ -379,7 +379,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);
diff --git a/arch/arm64/kvm/mmu.c b/arch/arm64/kvm/mmu.c
index ce7cea447f58..a1ae89ce3707 100644
--- a/arch/arm64/kvm/mmu.c
+++ b/arch/arm64/kvm/mmu.c
@@ -2317,7 +2317,7 @@ static int user_mem_abort(const struct kvm_s2_fault_desc *s2fd,
*/
memcache = get_mmu_memcache(s2fd->vcpu);
if (!perm_fault || memslot_is_logging(s2fd->memslot) ||
- is_protected_kvm_enabled()) {
+ kvm_vm_hyp_is_distrusting(s2fd->vcpu->kvm)) {
ret = topup_mmu_memcache(s2fd->mmu, memcache);
if (ret)
return ret;
@@ -3136,7 +3136,7 @@ pre_fault_bytes_consumed(gpa_t gpa, unsigned long granule_size,
int kvm_arch_pre_fault_allowed(struct kvm_vcpu *vcpu)
{
- if (is_protected_kvm_enabled())
+ if (kvm_vm_hyp_is_distrusting(vcpu->kvm))
return -EOPNOTSUPP;
if (!kvm_vcpu_initialized(vcpu))
return -ENOEXEC;
diff --git a/arch/arm64/kvm/ptdump.c b/arch/arm64/kvm/ptdump.c
index 9f0aebacea5b..ec3acbefa1be 100644
--- a/arch/arm64/kvm/ptdump.c
+++ b/arch/arm64/kvm/ptdump.c
@@ -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,
--
2.43.0
^ permalink raw reply related [flat|nested] 26+ messages in thread
* [PATCH v6 10/11] KVM: arm64: CCA: Support runtime faulting of memory
2026-12-04 11:57 [PATCH v6 00/11] KVM: arm64: CCA: Prepare Realm memory management Steven Price
` (8 preceding siblings ...)
2026-12-04 11:57 ` [PATCH v6 09/11] KVM: arm64: Select protected VM behaviour by VM flavour Steven Price
@ 2026-12-04 11:57 ` Steven Price
2026-12-04 12:39 ` sashiko-bot
2026-12-04 15:32 ` Kohei Enju
2026-12-04 11:57 ` [PATCH v6 11/11] KVM: arm64: CCA: Add Realm stage-2 MMU operations Steven Price
2026-12-04 16:30 ` [PATCH v6 00/11] KVM: arm64: CCA: Prepare Realm memory management Kohei Enju
11 siblings, 2 replies; 26+ messages in thread
From: Steven Price @ 2026-12-04 11:57 UTC (permalink / raw)
To: kvm, kvmarm
Cc: Steven Price, Catalin Marinas, Marc Zyngier, Will Deacon,
James Morse, Oliver Upton, Suzuki K Poulose, Zenghui Yu,
linux-arm-kernel, linux-kernel, Joey Gouly, Alexandru Elisei,
Christoffer Dall, Fuad Tabba, linux-coco, Ganapatrao Kulkarni,
Gavin Shan, Shanker Donthineni, Alper Gun, Aneesh Kumar K . V,
Emi Kisanuki, Vishal Annapurve, WeiLin.Chang, Lorenzo Pieralisi,
Kohei Enju
At 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.
Co-developed-by: Suzuki K Poulose <suzuki.poulose@arm.com>
Signed-off-by: Suzuki K Poulose <suzuki.poulose@arm.com>
Signed-off-by: Steven Price <steven.price@arm.com>
---
Changes since v5:
- Move protected VM fault lock, cache and pre-fault policy changes
into a preceding VM flavour patch.
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_rmi.h | 14 ++
arch/arm64/kvm/mmu.c | 160 +++++++++++++++-
arch/arm64/kvm/rmi.c | 267 +++++++++++++++++++++++++++
4 files changed, 445 insertions(+), 9 deletions(-)
diff --git a/arch/arm64/include/asm/kvm_emulate.h b/arch/arm64/include/asm/kvm_emulate.h
index 32021bdf2610..8a71fddfdb85 100644
--- a/arch/arm64/include/asm/kvm_emulate.h
+++ b/arch/arm64/include/asm/kvm_emulate.h
@@ -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__ */
diff --git a/arch/arm64/include/asm/kvm_rmi.h b/arch/arm64/include/asm/kvm_rmi.h
index d23ec620195b..3594b2332e14 100644
--- a/arch/arm64/include/asm/kvm_rmi.h
+++ b/arch/arm64/include/asm/kvm_rmi.h
@@ -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 */
diff --git a/arch/arm64/kvm/mmu.c b/arch/arm64/kvm/mmu.c
index a1ae89ce3707..ee81d147336c 100644
--- a/arch/arm64/kvm/mmu.c
+++ b/arch/arm64/kvm/mmu.c
@@ -450,6 +450,10 @@ static void stage2_flush_vm(struct kvm *kvm)
struct kvm_memory_slot *memslot;
int idx, bkt;
+ /* The RMM requires FWB, so Realm stage-2 mappings need no cache flush. */
+ if (kvm_vm_is_realm(kvm))
+ return;
+
idx = srcu_read_lock(&kvm->srcu);
write_lock(&kvm->mmu_lock);
@@ -1225,6 +1229,10 @@ void stage2_unmap_vm(struct kvm *kvm)
struct kvm_memory_slot *memslot;
int idx, bkt;
+ /* For realms this is handled by the RMM so nothing to do here */
+ if (kvm_vm_is_realm(kvm))
+ return;
+
idx = srcu_read_lock(&kvm->srcu);
mmap_read_lock(current->mm);
write_lock(&kvm->mmu_lock);
@@ -1890,6 +1898,64 @@ static int gmem_abort(const struct kvm_s2_fault_desc *s2fd,
return ret;
}
+static int realm_gmem_abort(const struct kvm_s2_fault_desc *s2fd)
+{
+ struct kvm *kvm = s2fd->vcpu->kvm;
+ bool write_fault = kvm_s2_fault_is_write(s2fd);
+ bool exec_fault = kvm_s2_fault_is_exec(s2fd);
+ bool is_private;
+ enum kvm_pgtable_prot prot = KVM_PGTABLE_PROT_R | KVM_PGTABLE_PROT_W;
+ phys_addr_t mapped_top;
+ kvm_pfn_t pfn;
+ gfn_t gfn;
+ u64 mmu_seq;
+ void *memcache;
+ int ret;
+
+ memcache = get_mmu_memcache(s2fd->vcpu);
+ ret = topup_mmu_memcache(s2fd->mmu, memcache);
+ if (ret)
+ return ret;
+
+ gfn = kvm_gfn_from_fault(kvm, s2fd->fault_ipa);
+ is_private = kvm_realm_is_private_address(&kvm->arch.realm,
+ s2fd->fault_ipa);
+ VM_WARN_ON_ONCE(write_fault && exec_fault);
+ VM_WARN_ON_ONCE(is_private && kvm_s2_fault_is_perm(s2fd));
+
+ mmu_seq = kvm->mmu_invalidate_seq;
+ /* Pairs with the smp_wmb() in kvm_mmu_invalidate_end(). */
+ smp_rmb();
+
+ if (is_private != kvm_gmem_is_private_gfn(kvm, gfn)) {
+ kvm_prepare_memory_fault_exit(s2fd->vcpu, gfn_to_gpa(gfn),
+ PAGE_SIZE, write_fault, exec_fault,
+ is_private);
+ return -EFAULT;
+ }
+
+ ret = kvm_gmem_get_pfn(kvm, s2fd->memslot, gfn, &pfn, NULL);
+ if (ret) {
+ kvm_prepare_memory_fault_exit(s2fd->vcpu, gfn_to_gpa(gfn),
+ PAGE_SIZE, write_fault, exec_fault,
+ is_private);
+ return ret;
+ }
+
+ kvm_fault_lock(kvm);
+ if (mmu_invalidate_retry(kvm, mmu_seq)) {
+ ret = -EAGAIN;
+ } else {
+ ret = realm_map_ipa(kvm, s2fd->fault_ipa, pfn, PAGE_SIZE,
+ prot, memcache, &mapped_top);
+ if (mapped_top > s2fd->fault_ipa)
+ ret = 0;
+ }
+ kvm_fault_unlock(kvm);
+
+ return ret == -EAGAIN ? 0 : ret;
+}
+
static int protected_pkvm_mem_abort(const struct kvm_s2_fault_desc *s2fd)
{
unsigned int flags = FOLL_HWPOISON | FOLL_LONGTERM | FOLL_WRITE;
@@ -2050,7 +2116,8 @@ static int kvm_s2_fault_get_vma_info(const struct kvm_s2_fault_desc *s2fd,
* mapping size to ensure we find the right PFN and lay down the
* mapping in the right place.
*/
- s2vi->gfn = ALIGN_DOWN(s2fd->fault_ipa, s2vi->vma_pagesize) >> PAGE_SHIFT;
+ s2vi->gfn = kvm_gfn_from_fault(kvm, ALIGN_DOWN(s2fd->fault_ipa,
+ s2vi->vma_pagesize));
s2vi->mte_allowed = kvm_vma_mte_allowed(vma);
@@ -2297,8 +2364,63 @@ static int kvm_s2_fault_map(const struct kvm_s2_fault_desc *s2fd,
return ret;
}
+static int realm_s2_fault_map(const struct kvm_s2_fault_desc *s2fd,
+ const struct kvm_s2_fault_vma_info *s2vi,
+ enum kvm_pgtable_prot prot,
+ void *memcache,
+ struct kvm_s2_fault_result *result)
+{
+ struct kvm *kvm = s2fd->vcpu->kvm;
+ bool writable = prot & KVM_PGTABLE_PROT_W;
+ phys_addr_t mapped_top = 0, map_start = 0;
+ long mapping_size = s2vi->vma_pagesize;
+ kvm_pfn_t pfn = s2vi->pfn;
+ gfn_t gfn = s2vi->gfn;
+ int ret = -EAGAIN;
+
+ kvm_fault_lock(kvm);
+ if (mmu_invalidate_retry(kvm, s2vi->mmu_seq))
+ goto out_unlock;
+
+ if (mapping_size == PAGE_SIZE && s2vi->max_map_size != PAGE_SIZE) {
+ mapping_size = transparent_hugepage_adjust(s2fd, &pfn, &gfn);
+ if (mapping_size < 0) {
+ ret = mapping_size;
+ goto out_unlock;
+ }
+ }
+
+ map_start = ALIGN_DOWN(s2fd->fault_ipa, mapping_size);
+ ret = realm_map_ipa(kvm, s2fd->fault_ipa, pfn, mapping_size,
+ prot, memcache, &mapped_top);
+ if (mapped_top > s2fd->fault_ipa)
+ ret = 0;
+
+out_unlock:
+ /* A partial writable mapping still requires the backing folio to be dirty. */
+ kvm_release_faultin_page(kvm, s2vi->page, mapped_top <= map_start,
+ writable);
+ kvm_fault_unlock(kvm);
+
+ if (ret == -EAGAIN)
+ return result ? ret : 0;
+
+ if (result && !ret)
+ result->mapping_size = mapped_top == map_start + mapping_size ?
+ mapping_size : PAGE_SIZE;
+
+ return ret;
+}
+
+typedef int (*kvm_s2_fault_map_fn)(const struct kvm_s2_fault_desc *s2fd,
+ const struct kvm_s2_fault_vma_info *s2vi,
+ enum kvm_pgtable_prot prot,
+ void *memcache,
+ struct kvm_s2_fault_result *result);
+
static int user_mem_abort(const struct kvm_s2_fault_desc *s2fd,
- struct kvm_s2_fault_result *result)
+ struct kvm_s2_fault_result *result,
+ kvm_s2_fault_map_fn fault_map)
{
bool perm_fault = kvm_s2_fault_is_perm(s2fd);
struct kvm_s2_fault_vma_info s2vi = {};
@@ -2309,8 +2431,9 @@ static int user_mem_abort(const struct kvm_s2_fault_desc *s2fd,
/*
* Permission faults just need to update the existing leaf entry,
* and so normally don't require allocations from the memcache. The
- * only exception to this is when dirty logging is enabled at runtime
+ * exceptions are when dirty logging is enabled at runtime
* and a fault needs to collapse a block entry into a table.
+ * Realms can also require new RTT levels when replacing a mapping.
* Under pKVM a permission fault can also collapse pages into a block,
* which needs a fresh mapping object, and the hypervisor requires the
* min-pages memcache even when the install allocates nothing.
@@ -2340,7 +2463,7 @@ static int user_mem_abort(const struct kvm_s2_fault_desc *s2fd,
return ret;
}
- return kvm_s2_fault_map(s2fd, &s2vi, prot, memcache, result);
+ return fault_map(s2fd, &s2vi, prot, memcache, result);
}
static int kvm_vm_mem_abort(const struct kvm_s2_fault_desc *s2fd)
@@ -2352,7 +2475,25 @@ static int kvm_vm_mem_abort(const struct kvm_s2_fault_desc *s2fd)
if (kvm_slot_has_gmem(s2fd->memslot))
return gmem_abort(s2fd, NULL);
else
- return user_mem_abort(s2fd, NULL);
+ return user_mem_abort(s2fd, NULL, kvm_s2_fault_map);
+}
+
+static int __maybe_unused realm_mem_abort(const struct kvm_s2_fault_desc *s2fd)
+{
+ struct kvm *kvm = s2fd->vcpu->kvm;
+
+ VM_WARN_ON_ONCE(kvm_s2_fault_is_perm(s2fd) &&
+ !esr_abt_is_write_fault(s2fd->esr) &&
+ !kvm_s2_fault_is_exec(s2fd));
+
+ if (kvm_realm_is_private_address(&kvm->arch.realm, s2fd->fault_ipa) &&
+ !kvm_slot_has_gmem(s2fd->memslot))
+ return -EINVAL;
+
+ if (kvm_slot_has_gmem(s2fd->memslot))
+ return realm_gmem_abort(s2fd);
+ else
+ return user_mem_abort(s2fd, NULL, realm_s2_fault_map);
}
/* Resolve the access fault by making the page young again. */
@@ -2461,10 +2602,10 @@ int kvm_handle_guest_sea(struct kvm_vcpu *vcpu)
*/
int kvm_handle_guest_abort(struct kvm_vcpu *vcpu)
{
+ struct kvm *kvm = vcpu->kvm;
struct kvm_s2_trans nested_trans, *nested = NULL;
unsigned long esr = kvm_vcpu_get_esr(vcpu);
struct kvm_s2_mmu *mmu = vcpu->arch.hw_mmu;
- struct kvm *kvm = vcpu->kvm;
phys_addr_t fault_ipa; /* The address we faulted on */
phys_addr_t ipa; /* Always the IPA in the L1 guest phys space */
struct kvm_memory_slot *memslot;
@@ -2557,8 +2698,9 @@ int kvm_handle_guest_abort(struct kvm_vcpu *vcpu)
nested = &nested_trans;
}
- gfn = ipa >> PAGE_SHIFT;
+ gfn = kvm_gfn_from_fault(kvm, ipa);
memslot = gfn_to_memslot(kvm, gfn);
+
hva = gfn_to_hva_memslot_prot(memslot, gfn, &writable);
write_fault = esr_abt_is_write_fault(esr);
if (kvm_is_error_hva(hva) || (write_fault && !writable)) {
@@ -2601,7 +2743,7 @@ int kvm_handle_guest_abort(struct kvm_vcpu *vcpu)
* of the page size.
*/
ipa |= FAR_TO_FIPA_OFFSET(kvm_vcpu_get_hfar(vcpu));
- ret = io_mem_abort(vcpu, ipa);
+ ret = io_mem_abort(vcpu, kvm_gpa_from_fault(kvm, ipa));
goto out_unlock;
}
@@ -3100,7 +3242,7 @@ static long __pre_fault_s2(struct kvm_s2_mmu *mmu, struct kvm_vcpu *vcpu,
if (is_gmem)
ret = gmem_abort(&s2fd, &result);
else
- ret = user_mem_abort(&s2fd, &result);
+ ret = user_mem_abort(&s2fd, &result, kvm_s2_fault_map);
if (IS_ERR_VALUE(ret))
return ret;
return result.mapping_size;
diff --git a/arch/arm64/kvm/rmi.c b/arch/arm64/kvm/rmi.c
index f176ea9184d7..d48481a33f8f 100644
--- a/arch/arm64/kvm/rmi.c
+++ b/arch/arm64/kvm/rmi.c
@@ -490,6 +490,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;
+ }
+
+ if (rmi_size == 0) {
+ /*
+ * Stop at the next block boundary so that a later iteration can
+ * use a larger mapping when both addresses are aligned.
+ */
+ count = MIN(size >> PAGE_SHIFT, PTRS_PER_PTE - 1);
+ count = min(count, (PMD_SIZE - (ipa & (PMD_SIZE - 1))) >> PAGE_SHIFT);
+ count = min(count, (PMD_SIZE - (phys & (PMD_SIZE - 1))) >> PAGE_SHIFT);
+ }
+
+ WARN_ON(phys & ~PAGE_MASK);
+
+ out = FIELD_PREP(RMI_ADDR_RANGE_BLOCK_SIZE_MASK, rmi_size) |
+ FIELD_PREP(RMI_ADDR_RANGE_COUNT_MASK, count) |
+ FIELD_PREP(RMI_ADDR_RANGE_STATE_MASK, state);
+ out |= phys & PAGE_MASK;
+
+ return out;
+}
+
+static int realm_map_protected(struct kvm *kvm,
+ unsigned long ipa,
+ kvm_pfn_t pfn,
+ unsigned long map_size,
+ struct kvm_mmu_memory_cache *memcache,
+ phys_addr_t *mapped_top)
+{
+ struct realm *realm = &kvm->arch.realm;
+ phys_addr_t phys = __pfn_to_phys(pfn);
+ phys_addr_t rd = realm->rd_phys;
+ phys_addr_t delegated_phys;
+ unsigned long ipa_top;
+ unsigned int retries = 0;
+ long ret = 0;
+
+ if (WARN_ON(!IS_ALIGNED(map_size, PAGE_SIZE) ||
+ !IS_ALIGNED(ipa, map_size)))
+ return -EINVAL;
+
+ if (rmi_delegate_range(phys, map_size, &delegated_phys)) {
+ if (delegated_phys == phys) {
+ /*
+ * It's likely we raced with another VCPU on the same
+ * fault. Assume the other VCPU has handled the fault
+ * and return to the guest.
+ */
+ return 0;
+ }
+ /* Partial delegation - map as much as we can */
+ map_size = delegated_phys - phys;
+ }
+
+ ipa_top = ipa + map_size;
+
+ while (ipa < ipa_top) {
+ unsigned long flags = RMI_ADDR_TYPE_SINGLE;
+ unsigned long range_desc = addr_range_desc(realm, ipa, phys,
+ ipa_top - ipa,
+ RMI_OP_MEM_DELEGATED);
+ 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_data_map(rd, ipa, call_top, flags, range_desc,
+ &out_top);
+ if (ret < 0)
+ goto err_undelegate;
+
+ if (RMI_RESULT_STATUS(ret) == RMI_ERROR_RTT) {
+ /* 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 (++retries > KVM_PGTABLE_LAST_LEVEL + 2 ||
+ level < realm->start_level || level >= req_level) {
+ ret = -ENXIO;
+ goto err_undelegate;
+ }
+ ret = realm_create_rtt_levels(realm, ipa, level,
+ req_level,
+ memcache);
+ if (ret)
+ goto err_undelegate;
+
+ continue;
+ }
+
+ if (ret) {
+ ret = RMI_RESULT_STATUS(ret) == RMI_BLOCKED ? -EAGAIN : -ENXIO;
+ goto err_undelegate;
+ }
+
+ /* An IRQ can stop the RMM before it maps the first entry. */
+ if (out_top == ipa) {
+ ret = -EAGAIN;
+ goto err_undelegate;
+ }
+ if (WARN_ON_ONCE(out_top < ipa || out_top > call_top)) {
+ ret = -ENXIO;
+ goto err_undelegate;
+ }
+
+ phys += out_top - ipa;
+ ipa = out_top;
+ *mapped_top = ipa;
+ retries = 0;
+ }
+
+ return 0;
+
+err_undelegate:
+ /*
+ * Keep completed mappings: unmapping them would destroy their RIPAS.
+ * A range which cannot be undelegated remains owned by the RMM.
+ */
+ WARN_ON(rmi_undelegate_range(phys, ipa_top - ipa));
+ return ret < 0 ? ret : -ENXIO;
+}
+
+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)
+{
+ struct realm *realm = &kvm->arch.realm;
+ unsigned long attr, flags = 0;
+ phys_addr_t rd = realm->rd_phys;
+ phys_addr_t phys = __pfn_to_phys(pfn);
+ unsigned long ipa_top = ipa + size;
+ unsigned int retries = 0;
+ long ret;
+
+ if (WARN_ON(!IS_ALIGNED(size, PAGE_SIZE)))
+ return -EINVAL;
+
+ switch (prot & (KVM_PGTABLE_PROT_DEVICE | KVM_PGTABLE_PROT_NORMAL_NC)) {
+ case KVM_PGTABLE_PROT_DEVICE | KVM_PGTABLE_PROT_NORMAL_NC:
+ return -EINVAL;
+ case KVM_PGTABLE_PROT_DEVICE:
+ attr = MT_S2_FWB_DEVICE_nGnRE;
+ break;
+ case KVM_PGTABLE_PROT_NORMAL_NC:
+ attr = MT_S2_FWB_NORMAL_NC;
+ break;
+ default:
+ attr = MT_S2_FWB_NORMAL;
+ }
+
+ flags |= FIELD_PREP(RMI_RTT_UNPROT_MAP_FLAGS_MEMATTR_MASK, attr);
+
+ /*
+ * An instruction fetch from an unprotected IPA takes a Synchronous
+ * External Abort in the Realm. KVM never handles an execute permission
+ * fault for an unprotected mapping.
+ */
+ if (prot & KVM_PGTABLE_PROT_R)
+ flags |= FIELD_PREP(RMI_RTT_UNPROT_MAP_FLAGS_S2AP_MASK, RMI_S2AP_DIRECT_READ);
+ if (prot & KVM_PGTABLE_PROT_W)
+ flags |= FIELD_PREP(RMI_RTT_UNPROT_MAP_FLAGS_S2AP_MASK, RMI_S2AP_DIRECT_WRITE);
+
+ /* TODO: Batch mappings with RMI_ADDR_TYPE_LIST. */
+ flags |= RMI_ADDR_TYPE_SINGLE;
+
+ 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 ||
+ ++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;
+ }
+
+ ret = realm_create_rtt_levels(realm, ipa, level,
+ req_level,
+ memcache);
+ if (ret)
+ return ret;
+
+ continue;
+ }
+
+ /* Retry the fault after releasing mmu_lock to service the IRQ. */
+ if (out_top == ipa)
+ return -EAGAIN;
+ if (WARN_ON_ONCE(out_top < ipa || out_top > call_top))
+ return -ENXIO;
+
+ phys += out_top - ipa;
+ ipa = out_top;
+ *mapped_top = ipa;
+ retries = 0;
+ }
+
+ return 0;
+}
+
+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)
+{
+ struct realm *realm = &kvm->arch.realm;
+
+ *mapped_top = 0;
+ if (kvm_realm_state(kvm) != REALM_STATE_ACTIVE)
+ return -EBUSY;
+
+ ipa = ALIGN_DOWN(ipa, map_size);
+ *mapped_top = ipa;
+ if (!kvm_realm_is_private_address(realm, ipa)) {
+ return realm_map_unprotected(kvm, ipa, pfn, map_size, prot,
+ memcache, mapped_top);
+ }
+
+ /* It's impossible to map protected pages read-only. */
+ if (WARN_ON(!(prot & KVM_PGTABLE_PROT_W)))
+ return -EFAULT;
+ return realm_map_protected(kvm, ipa, pfn, map_size, memcache,
+ mapped_top);
+}
+
void kvm_destroy_realm(struct kvm *kvm)
{
struct realm *realm = &kvm->arch.realm;
--
2.43.0
^ permalink raw reply related [flat|nested] 26+ messages in thread
* [PATCH v6 11/11] KVM: arm64: CCA: Add Realm stage-2 MMU operations
2026-12-04 11:57 [PATCH v6 00/11] KVM: arm64: CCA: Prepare Realm memory management Steven Price
` (9 preceding siblings ...)
2026-12-04 11:57 ` [PATCH v6 10/11] KVM: arm64: CCA: Support runtime faulting of memory Steven Price
@ 2026-12-04 11:57 ` Steven Price
2026-12-04 12:44 ` sashiko-bot
2026-12-04 16:30 ` [PATCH v6 00/11] KVM: arm64: CCA: Prepare Realm memory management Kohei Enju
11 siblings, 1 reply; 26+ messages in thread
From: Steven Price @ 2026-12-04 11:57 UTC (permalink / raw)
To: kvm, kvmarm
Cc: Suzuki K Poulose, Catalin Marinas, Marc Zyngier, Will Deacon,
James Morse, Oliver Upton, Zenghui Yu, linux-arm-kernel,
linux-kernel, Joey Gouly, Alexandru Elisei, Christoffer Dall,
Fuad Tabba, linux-coco, Ganapatrao Kulkarni, Gavin Shan,
Shanker Donthineni, Alper Gun, Aneesh Kumar K . V, Emi Kisanuki,
Vishal Annapurve, WeiLin.Chang, Lorenzo Pieralisi, Kohei Enju,
Steven Price
From: Suzuki K Poulose <suzuki.poulose@arm.com>
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.
Signed-off-by: Suzuki K Poulose <suzuki.poulose@arm.com>
Signed-off-by: Steven Price <steven.price@arm.com>
---
Changes since v5:
- Move protected VM debugfs handling into the VM flavour patch.
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 ++++++++++++++++++++++++++++++++++++++++++--
1 file changed, 45 insertions(+), 2 deletions(-)
diff --git a/arch/arm64/kvm/mmu.c b/arch/arm64/kvm/mmu.c
index ee81d147336c..8a4b4cfe1b4a 100644
--- a/arch/arm64/kvm/mmu.c
+++ b/arch/arm64/kvm/mmu.c
@@ -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;
@@ -3357,6 +3390,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)
@@ -3365,7 +3408,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)
--
2.43.0
^ permalink raw reply related [flat|nested] 26+ messages in thread
* Re: [PATCH v6 04/11] KVM: arm64: CCA: Add basic infrastructure for creating a realm
2026-12-04 11:57 ` [PATCH v6 04/11] KVM: arm64: CCA: Add basic infrastructure for creating a realm Steven Price
@ 2026-12-04 12:15 ` sashiko-bot
0 siblings, 0 replies; 26+ messages in thread
From: sashiko-bot @ 2026-12-04 12:15 UTC (permalink / raw)
To: Steven Price, kvm, kvmarm; +Cc: Oliver Upton, Marc Zyngier
Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Low] KVM: arm64: missing GFP_KERNEL_ACCOUNT for realm->sro in
kvm_init_realm()
--
commit c9247832a90b04b03d28464012e13d88047af6a2
Author: Steven Price <steven.price@arm.com>
KVM: arm64: CCA: Add basic infrastructure for creating a realm
This commit adds skeleton realm creation and teardown helpers along with
RMM IPA limit checking during stage-2 MMU initialization.
> diff --git a/arch/arm64/kvm/rmi.c b/arch/arm64/kvm/rmi.c
> new file mode 100644
> index 0000000000000..6a8bff1d8d467
> --- /dev/null
> +++ b/arch/arm64/kvm/rmi.c
[ ... ]
> +int kvm_init_realm(struct kvm *kvm)
> +{
> + struct realm *realm = &kvm->arch.realm;
> +
> + realm->ia_bits = VTCR_EL2_IPA(kvm->arch.mmu.vtcr);
> + realm->start_level = 4 - VTCR_EL2_LVLS(kvm->arch.mmu.vtcr);
> + realm->sro = kmalloc_obj(*realm->sro);
> + if (!realm->sro)
> + return -ENOMEM;
[Severity: Low]
Should realm->sro be allocated with GFP_KERNEL_ACCOUNT?
kmalloc_obj(*realm->sro) defaults to GFP_KERNEL, so each Realm VM's
struct rmi_sro_state (over 2 KiB, backed by a 4 KiB slab object) bypasses
the memcg accounting used by sibling per-VM allocations on the
kvm_arch_init_vm() path, such as supported_cpus and pgt.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261204115725.45272-1-steven.price@arm.com?part=4
^ permalink raw reply [flat|nested] 26+ messages in thread
* Re: [PATCH v6 08/11] KVM: arm64: CCA: Advertise private memory support for Realms
2026-12-04 11:57 ` [PATCH v6 08/11] KVM: arm64: CCA: Advertise private memory support for Realms Steven Price
@ 2026-12-04 12:23 ` sashiko-bot
0 siblings, 0 replies; 26+ messages in thread
From: sashiko-bot @ 2026-12-04 12:23 UTC (permalink / raw)
To: Steven Price, kvm, kvmarm; +Cc: Oliver Upton, Marc Zyngier
Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Medium] KVM: arm64: gmem_in_place_conversion bypasses non-Realm gmem
guards
--
commit 2c356668c26ea5656ad67c30240060dbb5dec9b6
Author: Steven Price <steven.price@arm.com>
KVM: arm64: CCA: Advertise private memory support for Realms
This commit defines kvm_arch_has_private_mem() to identify Realm VMs as
supporting private memory, which also enables gmem_in_place_conversion on
arm64.
> diff --git a/arch/arm64/include/asm/kvm_host.h b/arch/arm64/include/asm/kvm_host.h
> index 3ddeddfacd062..55673d08e3ebc 100644
> --- a/arch/arm64/include/asm/kvm_host.h
> +++ b/arch/arm64/include/asm/kvm_host.h
> @@ -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))
[Severity: Medium]
Does enabling gmem_in_place_conversion bypass the non-Realm guest_memfd
guard in kvm_arch_prepare_memory_region() and allow non-Realm VMs to fault
private guest_memfd pages?
Defining kvm_arch_has_private_mem() makes gmem_in_place_conversion true
for all arm64 VMs, so kvm_gmem_prepare_memory_region() unconditionally
sets KVM_MEMSLOT_GMEM_ONLY before kvm_arch_prepare_memory_region() runs.
The !kvm_memslot_is_gmem_only(new) check there becomes dead code.
Binding a non-mappable guest_memfd (flags == 0, initialized as private)
to a non-Realm VM now succeeds instead of returning -EINVAL.
On a fault, neither gmem_abort() nor kvm_translate_vncr() checks
kvm_is_private_gfn() before calling kvm_gmem_get_pfn(). The VM therefore
silently maps KVM_MEMORY_ATTRIBUTE_PRIVATE pages into stage-2 instead of
exiting with KVM_EXIT_MEMORY_FAULT.
Should kvm_arch_prepare_memory_region() still reject non-mappable
guest_memfd slots on non-Realm VMs, and should gmem_abort() and
kvm_translate_vncr() check kvm_is_private_gfn()?
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261204115725.45272-1-steven.price@arm.com?part=8
^ permalink raw reply [flat|nested] 26+ messages in thread
* Re: [PATCH v6 06/11] KVM: arm64: CCA: Destroy RTTs
2026-12-04 11:57 ` [PATCH v6 06/11] KVM: arm64: CCA: Destroy RTTs Steven Price
@ 2026-12-04 12:28 ` sashiko-bot
0 siblings, 0 replies; 26+ messages in thread
From: sashiko-bot @ 2026-12-04 12:28 UTC (permalink / raw)
To: Steven Price, kvm, kvmarm; +Cc: Oliver Upton, Marc Zyngier
Thank you for your contribution! Sashiko AI review found 3 potential
issues to consider:
- [Medium] KVM: arm64: kvm_destroy_realm() leaks mmu->pgt and
last_vcpu_ran on error
- [Low] KVM: arm64: mix of guard(mutex) and goto cleanup in
kvm_destroy_realm()
- [Low] KVM: arm64: mismatched RMI_SUCCESS check in realm_rtt_destroy()
--
commit 4cf622c6d71a9932890c5983b5d74291beb02ebe
Author: Steven Price <steven.price@arm.com>
KVM: arm64: CCA: Destroy RTTs
This commit adds helpers to unmap a Realm's protected stage-2 range and
recursively destroy non-root Realm Translation Tables (RTTs) during Realm
teardown, deferring kvm_free_stage2_pgd() until after the RD is destroyed
and the root RTTs are undelegated.
> diff --git a/arch/arm64/kvm/rmi.c b/arch/arm64/kvm/rmi.c
> index ee16f5364b9aa..f176ea9184d7c 100644
> --- a/arch/arm64/kvm/rmi.c
> +++ b/arch/arm64/kvm/rmi.c
> @@ -102,6 +102,30 @@ static long realm_rtt_create(struct realm *realm,
> return rmi_rtt_create(realm->rd_phys, phys, addr, level);
> }
>
> +/*
> + * 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;
> +}
[ ... ]
> + for (addr = start; addr < end; addr = next_addr) {
> + phys_addr_t rtt_granule;
[ ... ]
> + 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;
[Severity: Low]
This question rests on an RMM ABI detail that could not be confirmed from
the kernel source: can SMC_RMI_RTT_DESTROY return a non-negative RmiResult
with RMI_RESULT_STATUS(ret) == RMI_SUCCESS while any upper bits are
non-zero?
If so, rmi_rtt_destroy() sets *out_rtt (switching on RMI_RESULT_STATUS(ret))
and returns the raw result, but realm_rtt_destroy() skips assigning
*rtt_granule because it tests ret == RMI_SUCCESS. realm_destroy_rtt_level()
then enters case RMI_SUCCESS and passes the uninitialized stack variable
rtt_granule to free_rtt(), which can corrupt memory or crash during Realm
teardown. Checking RMI_RESULT_STATUS(ret) == RMI_SUCCESS in
realm_rtt_destroy() would avoid depending on the upper bits being zero.
[ ... ]
> @@ -352,14 +495,32 @@ 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;
[Severity: Low]
This isn't a bug, but kvm_destroy_realm() mixes guard(mutex) with
goto out_free_sro cleanup. Should it use explicit mutex_lock() and
mutex_unlock() (or scope-based cleanup for realm->sro as well) instead of
mixing the two styles?
> 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);
> @@ -370,6 +531,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;
[Severity: Medium]
Does jumping to out_free_sro on teardown errors leak mmu->pgt and
mmu->last_vcpu_ran?
Because kvm_uninit_stage2_mmu() skips kvm_free_stage2_pgd() for created
Realms, kvm_destroy_realm() is the only path that frees the host-only
allocations created in kvm_init_stage2_mmu(). When rmi_realm_terminate(),
realm_destroy_rtts(), rmi_realm_destroy(), or rmi_undelegate_range() fails,
skipping kvm_free_stage2_pgd() intentionally retains the still-delegated
pgt->pgd pages, but it also permanently leaks the non-delegated mmu->pgt
slab object and mmu->last_vcpu_ran per-CPU allocation.
Separating freeing of the host-only mmu->pgt and mmu->last_vcpu_ran
allocations from freeing the delegated pgt->pgd pages would avoid leaking
them on error.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261204115725.45272-1-steven.price@arm.com?part=6
^ permalink raw reply [flat|nested] 26+ messages in thread
* Re: [PATCH v6 05/11] KVM: arm64: CCA: Add Realm RTT mapping and invalidation helpers
2026-12-04 11:57 ` [PATCH v6 05/11] KVM: arm64: CCA: Add Realm RTT mapping and invalidation helpers Steven Price
@ 2026-12-04 12:28 ` sashiko-bot
0 siblings, 0 replies; 26+ messages in thread
From: sashiko-bot @ 2026-12-04 12:28 UTC (permalink / raw)
To: Steven Price, kvm, kvmarm; +Cc: Oliver Upton, Marc Zyngier
Thank you for your contribution! Sashiko AI review found 4 potential
issues to consider:
- [Medium] KVM: arm64: early return on undelegate error abandons private
unmap range
- [Medium] KVM: arm64: KVM_BUG_ON() warns on -ENOMEM in
realm_unmap_private_range()
- [Medium] KVM: arm64: unhandled RMI_BLOCKED in Realm RTT unmap and
create helpers
- [Low] KVM: arm64: try_topup never resets on address advance in private
unmap
--
commit 6a6c7236f977c112e6be13a65e7a962486113d13
Author: Steven Price <steven.price@arm.com>
KVM: arm64: CCA: Add Realm RTT mapping and invalidation helpers
This commit adds the RMI Realm Translation Table (RTT) creation and
unmapping helpers for CCA Realm stage-2 memory operations, and moves
rmi_addr_block_size_to_bytes() into include/linux/arm-rmi-cmds.h.
> diff --git a/arch/arm64/kvm/rmi.c b/arch/arm64/kvm/rmi.c
> index 6a8bff1d8d467..ee16f5364b9aa 100644
> --- a/arch/arm64/kvm/rmi.c
> +++ b/arch/arm64/kvm/rmi.c
> @@ -16,6 +16,337 @@ u32 kvm_rmm_ipa_limit(void)
[ ... ]
> +static void realm_unmap_shared_range(struct kvm *kvm,
> + unsigned long start,
> + unsigned long end,
> + bool may_block,
> + struct kvm_mmu_memory_cache *mc)
> +{
[ ... ]
> + ret = rmi_rtt_unprot_unmap(rd, addr, end, RMI_ADDR_TYPE_NONE,
> + 0, &next_addr, NULL, NULL);
> + if (WARN_ON(ret < 0))
> + return;
> +
> + switch (RMI_RESULT_STATUS(ret)) {
[ ... ]
> + default:
> + WARN_ON(1);
> + return;
> + }
[Severity: Medium]
Can an RMI_BLOCKED return from rmi_rtt_unprot_unmap() abort shared
unmapping mid-range?
rmi_smccc_invoke() only retries RMI_BLOCKED twice before returning it
for the caller to handle. Hitting default here returns early from a
void invalidation callback, so callers such as MMU notifiers or
kvm_gmem_punch_hole() can reclaim or free host pages while the Realm
still has live unprotected stage-2 mappings to them (use-after-free).
realm_unmap_private_range() has the same early return on RMI_BLOCKED,
leaving still-mapped, still-delegated granules to be freed back to the
host allocator. realm_create_rtt_levels() also warns and turns
RMI_BLOCKED into -ENXIO instead of retrying or returning -EAGAIN,
which triggers KVM_BUG_ON() in realm_unmap_private_range().
[ ... ]
> +static void realm_unmap_private_range(struct kvm *kvm,
> + unsigned long start,
> + unsigned long end,
> + bool may_block,
> + struct kvm_mmu_memory_cache *mc)
> +{
> + 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) {
> + unsigned long out_range;
> + unsigned long flags = RMI_ADDR_TYPE_SINGLE;
> + /* TODO: Optimise using RMI_ADDR_TYPE_LIST */
> +
> + ret = rmi_rtt_data_unmap(rd, addr, end, flags, 0,
> + &next_addr, &out_range, NULL);
> + if (WARN_ON(ret < 0))
> + return;
> +
> + switch (RMI_RESULT_STATUS(ret)) {
> + case RMI_SUCCESS:
> + ret = undelegate_range_desc(out_range);
> + if (WARN_ON(ret))
> + return;
> + break;
[Severity: Medium]
Does returning here on an undelegate_range_desc() failure abandon
unmapping and undelegating [next_addr, end)?
Because kvm_realm_unmap_range_filter() returns void, callers such as
kvm_gmem_punch_hole() still truncate and free the remaining backing
pages back to the host allocator. Leaving [next_addr, end) mapped in
a runnable Realm and still delegated risks guest use-after-free and
host Granule Protection Faults.
Continuing the loop after WARN_ON(ret) would leak only the failed
range (as rmi_sro_free() does) while still unmapping the rest. The
other early returns in realm_unmap_private_range() also exit without
marking the VM bugged via KVM_BUG_ON(1, kvm).
[ ... ]
> + 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;
> + }
> + }
[Severity: Low]
Should try_topup be reset to may_block whenever addr advances?
Once realm_unmap_cache_topup() fails for one address, try_topup stays
false for the rest of the range. If the loop moves on (for instance
after a concurrent split/unmap while mmu_lock was dropped, or via the
atomic fallback), later splits in the same call skip sleepable topups
and rely only on GFP_ATOMIC, making a failed split and KVM_BUG_ON()
more likely.
> +
> + ret = realm_create_rtt_levels(realm, addr, err_level,
> + level, mc);
> + if (!ret) {
> + next_addr = addr;
> + break;
> + }
> +
> + /* A failed split cannot leave the backing pages mapped. */
> + KVM_BUG_ON(1, kvm);
[Severity: Medium]
Can this KVM_BUG_ON(1, kvm) trigger a host WARN_ON_ONCE() on -ENOMEM?
When realm_create_rtt_levels() fails with -ENOMEM (from a GFP_ATOMIC
allocation failure or rmi_delegate_page() failure in
alloc_delegated_granule()), KVM_BUG_ON() emits a WARN_ON_ONCE(),
tainting the kernel and panicking hosts with panic_on_warn=1 on an
out-of-memory condition rather than a kernel bug.
Unlike realm_unmap_shared_range(), which uses WARN_ON(ret != -ENOMEM),
this warns unconditionally. Using kvm_vm_dead(kvm) on -ENOMEM and
reserving KVM_BUG_ON() for ret != -ENOMEM would still kill the VM
without firing a warning on allocation failure.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261204115725.45272-1-steven.price@arm.com?part=5
^ permalink raw reply [flat|nested] 26+ messages in thread
* Re: [PATCH v6 10/11] KVM: arm64: CCA: Support runtime faulting of memory
2026-12-04 11:57 ` [PATCH v6 10/11] KVM: arm64: CCA: Support runtime faulting of memory Steven Price
@ 2026-12-04 12:39 ` sashiko-bot
2026-12-04 15:32 ` Kohei Enju
1 sibling, 0 replies; 26+ messages in thread
From: sashiko-bot @ 2026-12-04 12:39 UTC (permalink / raw)
To: Steven Price, kvm, kvmarm; +Cc: Oliver Upton, Marc Zyngier
Thank you for your contribution! Sashiko AI review found 5 potential
issues to consider:
- [Medium] KVM: arm64: realm_mem_abort() returns -EINVAL without
KVM_EXIT_MEMORY_FAULT
- [Medium] KVM: arm64: realm_map_protected() ignores
rmi_delegate_range() errors
- [Medium] KVM: arm64: realm_map_protected() leaves failed undelegate
page unpinned
- [Low] KVM: arm64: commit message describes changes moved to preceding
commit
- [Low] KVM: arm64: does realm_s2_fault_map() omit needed
sanitise_mte_tags()?
--
commit b74285b1140496ce40baae3d7c0ccb94905144dd
Author: Steven Price <steven.price@arm.com>
KVM: arm64: CCA: Support runtime faulting of memory
This commit adds runtime stage-2 fault handling for Realm guests,
introducing private guest_memfd and shared memory mapping helpers in
mmu.c and rmi.c along with IPA-to-GPA conversion helpers.
> 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.
[Severity: Low]
These two paragraphs appear to be stale after the v5 split into commit
"KVM: arm64: Select protected VM behaviour by VM flavour". That preceding
patch already moves kvm_vm_hyp_is_distrusting() for nVHE, updates
kvm_fault_lock(), tops up the memcache on permission faults, and disables
pre-faulting, while this commit only adds a comment line in
user_mem_abort().
> diff --git a/arch/arm64/kvm/mmu.c b/arch/arm64/kvm/mmu.c
> index a1ae89ce37074..ee81d147336c2 100644
> --- a/arch/arm64/kvm/mmu.c
> +++ b/arch/arm64/kvm/mmu.c
[ ... ]
> @@ -2297,8 +2364,63 @@ static int kvm_s2_fault_map(const struct kvm_s2_fault_desc *s2fd,
> return ret;
> }
>
> +static int realm_s2_fault_map(const struct kvm_s2_fault_desc *s2fd,
> + const struct kvm_s2_fault_vma_info *s2vi,
> + enum kvm_pgtable_prot prot,
> + void *memcache,
> + struct kvm_s2_fault_result *result)
> +{
[ ... ]
> + map_start = ALIGN_DOWN(s2fd->fault_ipa, mapping_size);
> + ret = realm_map_ipa(kvm, s2fd->fault_ipa, pfn, mapping_size,
> + prot, memcache, &mapped_top);
[Severity: Low]
This question rests on an RMM/RME architecture detail that could not be
confirmed from the kernel source: can a Realm guest on an MTE-capable host
observe MTE allocation tags on unprotected Normal Cacheable
(MT_S2_FWB_NORMAL) stage-2 mappings?
If so, realm_s2_fault_map() omits sanitise_mte_tags() before mapping
cacheable userspace pages into a shared IPA (and kvm_vm_ioctl_enable_cap()
does not reject KVM_CAP_ARM_MTE for Realms), which would expose stale host
allocation tags to the guest. If the RMM unconditionally prevents Realm
access to MTE tags, a short comment here would clarify the invariant.
[ ... ]
> +static int __maybe_unused realm_mem_abort(const struct kvm_s2_fault_desc *s2fd)
> +{
> + struct kvm *kvm = s2fd->vcpu->kvm;
> +
> + VM_WARN_ON_ONCE(kvm_s2_fault_is_perm(s2fd) &&
> + !esr_abt_is_write_fault(s2fd->esr) &&
> + !kvm_s2_fault_is_exec(s2fd));
> +
> + if (kvm_realm_is_private_address(&kvm->arch.realm, s2fd->fault_ipa) &&
> + !kvm_slot_has_gmem(s2fd->memslot))
> + return -EINVAL;
[Severity: Medium]
Should a private-IPA fault on a non-guest_memfd memslot prepare a
KVM_EXIT_MEMORY_FAULT exit and return -EFAULT instead of -EINVAL?
Per Documentation/virt/kvm/api.rst, userspace must treat
kvm_run.exit_reason as undefined when KVM_RUN returns anything other than
-EFAULT or -EHWPOISON, so returning -EINVAL leaves the VMM without the
faulting GPA, size, or KVM_MEMORY_EXIT_FLAG_PRIVATE metadata. Both
realm_gmem_abort() on attribute mismatch and x86 kvm_mmu_faultin_pfn_gmem()
prepare KVM_EXIT_MEMORY_FAULT and return -EFAULT here.
> diff --git a/arch/arm64/kvm/rmi.c b/arch/arm64/kvm/rmi.c
> index f176ea9184d7c..d48481a33f8f7 100644
> --- a/arch/arm64/kvm/rmi.c
> +++ b/arch/arm64/kvm/rmi.c
[ ... ]
> +static int realm_map_protected(struct kvm *kvm,
> + unsigned long ipa,
> + kvm_pfn_t pfn,
> + unsigned long map_size,
> + struct kvm_mmu_memory_cache *memcache,
> + phys_addr_t *mapped_top)
> +{
[ ... ]
> + if (rmi_delegate_range(phys, map_size, &delegated_phys)) {
> + if (delegated_phys == phys) {
> + /*
> + * It's likely we raced with another VCPU on the same
> + * fault. Assume the other VCPU has handled the fault
> + * and return to the guest.
> + */
> + return 0;
> + }
> + /* Partial delegation - map as much as we can */
> + map_size = delegated_phys - phys;
> + }
[Severity: Medium]
Can discarding the return value of rmi_delegate_range() here trap the vCPU
in an infinite stage-2 fault loop?
If a previous fault delegated the page, failed in rmi_rtt_data_map() (for
example returning -EAGAIN on an IRQ), and then failed to undelegate it in
err_undelegate, the guest_memfd page stays delegated while the IPA remains
unmapped. On the next fault, rmi_delegate_range() fails with
delegated_phys == phys and realm_map_protected() returns 0 without checking
the error or verifying the mapping, so the guest re-enters and faults on
the same IPA indefinitely.
[ ... ]
> +err_undelegate:
> + /*
> + * Keep completed mappings: unmapping them would destroy their RIPAS.
> + * A range which cannot be undelegated remains owned by the RMM.
> + */
> + WARN_ON(rmi_undelegate_range(phys, ipa_top - ipa));
> + return ret < 0 ? ret : -ENXIO;
> +}
[Severity: Medium]
If rmi_undelegate_range() fails here, does the still-delegated page risk
being freed back to the normal-world allocator?
kvm_gmem_get_pfn() drops its temporary folio reference before returning to
realm_gmem_abort(), so only the guest_memfd page cache holds a reference to
the page. When userspace later punches a hole or closes the guest_memfd
inode, realm_unmap_private_range() finds no RTT entry to undelegate and the
still-delegated page is returned to the buddy allocator, where normal-world
access triggers a Granule Protection Fault. Pinning the folio on
undelegation failure (matching rmi_free_delegated_page() leaking
undelegatable pages) would prevent it from being freed while owned by the
RMM.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261204115725.45272-1-steven.price@arm.com?part=10
^ permalink raw reply [flat|nested] 26+ messages in thread
* Re: [PATCH v6 11/11] KVM: arm64: CCA: Add Realm stage-2 MMU operations
2026-12-04 11:57 ` [PATCH v6 11/11] KVM: arm64: CCA: Add Realm stage-2 MMU operations Steven Price
@ 2026-12-04 12:44 ` sashiko-bot
0 siblings, 0 replies; 26+ messages in thread
From: sashiko-bot @ 2026-12-04 12:44 UTC (permalink / raw)
To: Steven Price, kvm, kvmarm; +Cc: Oliver Upton, Marc Zyngier
Thank you for your contribution! Sashiko AI review found 5 potential
issues to consider:
- [Critical] KVM: arm64: kvm_realm_unmap_stage2_all() omits shared
mappings on mm release
- [High] KVM: arm64: realm stage-2 unmap aborts early on RMI_BLOCKED or
undelegate
- [Low] KVM: arm64: handle_access_fault() unguarded for VM_REALM stage-2
walk
- [Low] KVM: arm64: realm_map_unprotected() retries block map without
RTT destroy
- [Low] KVM: arm64: realm unmap allocates RTT levels on RMI_ERROR_RTT
err_level
--
commit 0969bc581a2fdc2d828121e71b83004a225097a1
Author: Suzuki K Poulose <suzuki.poulose@arm.com>
KVM: arm64: CCA: Add Realm stage-2 MMU operations
This commit defines realm_vm_s2_ops and registers it for VM_REALM, wiring
Realm stage-2 unmapping, fault handling, TLB flushing, and root PGD teardown
into the arm64 VM stage-2 operations table.
> diff --git a/arch/arm64/kvm/mmu.c b/arch/arm64/kvm/mmu.c
> index ee81d147336c2..8a4b4cfe1b4a6 100644
> --- a/arch/arm64/kvm/mmu.c
> +++ b/arch/arm64/kvm/mmu.c
[ ... ]
> @@ -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);
> +}
> +
[Severity: High]
Can realm_stage2_unmap_range() leave part of the requested range mapped when
an RMI call returns RMI_BLOCKED?
rmi_smccc_invoke() only retries RMI_BLOCKED RMM_BLOCKED_RETRY_COUNT (2) times
and expects callers to reissue if needed. Unlike realm_map_protected() and
realm_map_unprotected(), neither realm_unmap_shared_range() nor
realm_unmap_private_range() handles RMI_BLOCKED; both hit the default case
with WARN_ON(1) and return early, abandoning the rest of the range.
In realm_unmap_private_range(), a non-zero return from undelegate_range_desc()
(including RMI_BLOCKED from rmi_undelegate_range()) also logs WARN_ON(ret) and
returns immediately without undelegating the rest of out_range or unmapping
the remaining IPAs.
Because vm_stage2_unmap_range() returns void, callers such as MMU notifier
invalidations assume the whole range is gone. Leaving shared mappings live
after Linux frees the backing pages allows guest-to-host physical memory
use-after-free, while leaving private granules mapped or delegated in Realm
PAS causes host Granule Protection Faults once Linux reuses them.
[Severity: Low]
This question rests on an RMM architectural detail that could not be confirmed
from the kernel source: do SMC_RMI_RTT_UNPROT_UNMAP and SMC_RMI_RTT_DATA_UNMAP
return RMI_ERROR_RTT with err_level < level when walking an unassigned RTT
entry above the target page level (no child RTT exists), or do they return
RMI_SUCCESS for unassigned entries and only return RMI_ERROR_RTT for mapped
blocks that need splitting?
If they return RMI_ERROR_RTT for unassigned entries, unmapping a 4KB page in
an unpopulated 1GB or 2MB region (for example via madvise(MADV_DONTNEED) or
FALLOC_FL_PUNCH_HOLE) fails the err_level >= level check in both
realm_unmap_shared_range() and realm_unmap_private_range(). Both then call
realm_create_rtt_levels() to allocate and delegate new child RTT pages for
untouched memory, and realm_unmap_private_range() triggers KVM_BUG_ON(1, kvm)
if that allocation fails.
[ ... ]
> @@ -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;
>
[Severity: Low]
This question rests on an RMM architectural detail that could not be confirmed
from the kernel source: does SMC_RMI_RTT_UNPROT_MAP reject a 2MB block
descriptor (block_size == 1, req_level == 2) with RMI_ERROR_RTT at level >= 2
when the L2 RTT entry is a table pointing to an existing empty L3 RTT, rather
than replacing or folding the L3 RTT?
If it does, upgrading a previously 4KB-faulted shared userspace region to a
2MB transparent huge page via realm_s2_fault_map() -> realm_map_ipa() ->
realm_map_unprotected() will fail. Since realm_unmap_shared_range() never
folds or destroys child L3 RTTs, realm_map_unprotected() will unmap the range
on level >= req_level and retry the same 2MB block mapping until retries are
exhausted and KVM_RUN returns -ENXIO.
[ ... ]
> @@ -3357,6 +3390,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)
>
> @@ -3365,7 +3408,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),
> };
[Severity: Critical]
Does registering realm_mem_abort() leave unprotected shared stage-2 mappings
live across kvm_mmu_notifier_release()?
realm_mem_abort() allows a Realm guest to install unpinned userspace-backed
shared mappings via user_mem_abort() -> realm_s2_fault_map(), which releases
its page reference immediately and relies on MMU notifiers to tear down all
stage-2 mappings before host pages are freed. During exit_mmap(),
mn_hlist_release() invokes kvm_mmu_notifier_release() ->
kvm_uninit_stage2_mmu() -> kvm_realm_uninit_stage2() ->
kvm_realm_unmap_stage2_all() and unlinks the notifier before unmap_vmas()
frees the mm pages without calling invalidate_range_start().
arch/arm64/kvm/rmi.c:kvm_realm_unmap_stage2_all() {
...
write_lock(&kvm->mmu_lock);
kvm_realm_unmap_range_filter(kvm, 0,
BIT(realm->ia_bits - 1), true,
KVM_FILTER_PRIVATE);
write_unlock(&kvm->mmu_lock);
...
}
Because kvm_realm_unmap_stage2_all() omits KVM_FILTER_SHARED, shared RTT
entries remain live after exit_mmap() returns those physical pages to the
kernel allocator. If another process holds the VM or vCPU fd, a Realm vCPU
can read and write reallocated host RAM (guest-to-host physical memory
use-after-free). Passing KVM_FILTER_PRIVATE | KVM_FILTER_SHARED in
kvm_realm_unmap_stage2_all() would tear down shared mappings before the pages
are freed.
[Severity: Low]
This question rests on an architecture detail that could not be confirmed from
the kernel source: can a Realm REC exit ever report a stage-2 access flag
fault FSC in ESR that reaches kvm_handle_guest_abort() (for example on a
shared IPA)?
If it can, kvm_handle_guest_abort() handles esr_fsc_is_access_flag_fault(esr)
via handle_access_fault() before dispatching to vm_mem_abort(). Unlike
protected pKVM, which guards pkvm_pgtable_stage2_mkyoung() with
WARN_ON(kvm_vm_is_protected(...)), is_protected_kvm_enabled() is false for
VM_REALM, so handle_access_fault() calls kvm_pgtable_stage2_mkyoung() directly
on mmu->pgt, walking the RMM-delegated root RTT pages from the host and
triggering a Granule Protection Fault.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261204115725.45272-1-steven.price@arm.com?part=11
^ permalink raw reply [flat|nested] 26+ messages in thread
* Re: [PATCH v6 09/11] KVM: arm64: Select protected VM behaviour by VM flavour
2026-12-04 11:57 ` [PATCH v6 09/11] KVM: arm64: Select protected VM behaviour by VM flavour Steven Price
@ 2026-12-04 13:21 ` Fuad Tabba
0 siblings, 0 replies; 26+ messages in thread
From: Fuad Tabba @ 2026-12-04 13:21 UTC (permalink / raw)
To: Steven Price
Cc: kvm, kvmarm, Catalin Marinas, Marc Zyngier, Will Deacon,
James Morse, Oliver Upton, Suzuki K Poulose, Zenghui Yu,
linux-arm-kernel, linux-kernel, Joey Gouly, Alexandru Elisei,
Christoffer Dall, linux-coco, Ganapatrao Kulkarni, Gavin Shan,
Shanker Donthineni, Alper Gun, Aneesh Kumar K . V, Emi Kisanuki,
Vishal Annapurve, WeiLin.Chang, Lorenzo Pieralisi, Kohei Enju
On Fri, 4 Dec 2026 at 11:58, Steven Price <steven.price@arm.com> wrote:
>
> Use the VM flavour when choosing the stage-2 fault lock, fault memory
> cache, pre-fault policy and ptdump visibility. This prepares these paths
> for Realm VMs, whose stage-2 tables are managed outside the host.
>
> Move the VM flavour helper outside the nVHE guard so the shared
> fault-lock helpers can use it.
>
> Signed-off-by: Steven Price <steven.price@arm.com>
Reviewed-by: Fuad Tabba <fuad.tabba@linux.dev>
Cheers,
/fuad
^ permalink raw reply [flat|nested] 26+ messages in thread
* Re: [PATCH v6 10/11] KVM: arm64: CCA: Support runtime faulting of memory
2026-12-04 11:57 ` [PATCH v6 10/11] KVM: arm64: CCA: Support runtime faulting of memory Steven Price
2026-12-04 12:39 ` sashiko-bot
@ 2026-12-04 15:32 ` Kohei Enju
2026-12-04 16:27 ` Steven Price
1 sibling, 1 reply; 26+ messages in thread
From: Kohei Enju @ 2026-12-04 15:32 UTC (permalink / raw)
To: Steven Price
Cc: kvm, kvmarm, Catalin Marinas, Marc Zyngier, Will Deacon,
James Morse, Oliver Upton, Suzuki K Poulose, Zenghui Yu,
linux-arm-kernel, linux-kernel, Joey Gouly, Alexandru Elisei,
Christoffer Dall, Fuad Tabba, linux-coco, Ganapatrao Kulkarni,
Gavin Shan, Shanker Donthineni, Alper Gun, Aneesh Kumar K . V,
Emi Kisanuki, Vishal Annapurve, WeiLin.Chang, Lorenzo Pieralisi
On 12/04 11:57, Steven Price wrote:
> At 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.
Hello Steve,
Not a review comment, but just a question for clarification.
My understanding is that this series and the following ones aim to
provide basic CCA/KVM functionality, leaving out optional features for
the initial upstream support.
I've been looking into supporting pre-faulting for CCA/KVM in the
future, and I'm not aware of any architectural blockers.
Are you aware of any potential blockers or limitations that might
prevent us from supporting pre-faulting in the future, or is it simply
being left out for now as an *optional* feature, as I assume?
Thanks,
Kohei
^ permalink raw reply [flat|nested] 26+ messages in thread
* Re: [PATCH v6 07/11] KVM: arm64: CCA: Restrict Realm memslots to the lower IPA half
2026-12-04 11:57 ` [PATCH v6 07/11] KVM: arm64: CCA: Restrict Realm memslots to the lower IPA half Steven Price
@ 2026-12-04 15:48 ` Kohei Enju
2026-12-04 16:27 ` Steven Price
0 siblings, 1 reply; 26+ messages in thread
From: Kohei Enju @ 2026-12-04 15:48 UTC (permalink / raw)
To: Steven Price
Cc: kvm, kvmarm, Suzuki K Poulose, Catalin Marinas, Marc Zyngier,
Will Deacon, James Morse, Oliver Upton, Zenghui Yu,
linux-arm-kernel, linux-kernel, Joey Gouly, Alexandru Elisei,
Christoffer Dall, Fuad Tabba, linux-coco, Ganapatrao Kulkarni,
Gavin Shan, Shanker Donthineni, Alper Gun, Aneesh Kumar K . V,
Emi Kisanuki, Vishal Annapurve, WeiLin.Chang, Lorenzo Pieralisi,
Fuad Tabba
On 12/04 11:57, Steven Price wrote:
> From: Suzuki K Poulose <suzuki.poulose@arm.com>
>
> 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.
>
> Signed-off-by: Suzuki K Poulose <suzuki.poulose@arm.com>
> Signed-off-by: Steven Price <steven.price@arm.com>
> Reviewed-by: Fuad Tabba <fuad.tabba@linux.dev>
>
> ---
> 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(-)
>
> diff --git a/arch/arm64/kvm/mmu.c b/arch/arm64/kvm/mmu.c
> index 37adc20f0042..ce7cea447f58 100644
> --- a/arch/arm64/kvm/mmu.c
> +++ b/arch/arm64/kvm/mmu.c
> @@ -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;
Not a big deal, but if you resend this series, how about renaming
ipa_limit to gfn_limit? The current name is a bit confusing to me
because the value is GFNs rather than bytes.
Thanks,
Kohei
> + if (kvm_vm_is_realm(kvm))
> + ipa_limit >>= 1;
> + if (new->base_gfn + new->npages > ipa_limit)
> return -EFAULT;
>
> /*
> --
> 2.43.0
>
^ permalink raw reply [flat|nested] 26+ messages in thread
* Re: [PATCH v6 01/11] KVM: arm64: CCA: Introduce Realms
2026-12-04 11:57 ` [PATCH v6 01/11] KVM: arm64: CCA: Introduce Realms Steven Price
@ 2026-12-04 16:07 ` Kohei Enju
2026-12-04 17:05 ` Steven Price
0 siblings, 1 reply; 26+ messages in thread
From: Kohei Enju @ 2026-12-04 16:07 UTC (permalink / raw)
To: Steven Price
Cc: kvm, kvmarm, Suzuki K Poulose, Catalin Marinas, Marc Zyngier,
Will Deacon, James Morse, Oliver Upton, Zenghui Yu,
linux-arm-kernel, linux-kernel, Joey Gouly, Alexandru Elisei,
Christoffer Dall, Fuad Tabba, linux-coco, Ganapatrao Kulkarni,
Gavin Shan, Shanker Donthineni, Alper Gun, Aneesh Kumar K . V,
Emi Kisanuki, Vishal Annapurve, WeiLin.Chang, Lorenzo Pieralisi,
Fuad Tabba
On 12/04 11:57, Steven Price wrote:
> From: Suzuki K Poulose <suzuki.poulose@arm.com>
>
> 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.
>
> Signed-off-by: Suzuki K Poulose <suzuki.poulose@arm.com>
> Signed-off-by: Steven Price <steven.price@arm.com>
> Reviewed-by: Fuad Tabba <fuad.tabba@linux.dev>
Reviewed-by: Kohei Enju <enju.kohei@fujitsu.com>
Minor nit below.
[...]
> +/**
> + * enum realm_state - State of a Realm
> + *
> + * Mirrors the RMM's Realm lifecycle states where they are meaningful to KVM,
> + * with REALM_STATE_DYING being a KVM-internal state used to prevent further
Strictly speaking, REALM_STATE_NONE and REALM_STATE_DEAD are also
KVM-internal states, so I think the comment should reflect this if you
resend the series.
Thanks,
Kohei
> + * requests while teardown is in progress. KVM does not track REALM_SYSTEM_OFF
> + * or REALM_ZOMBIE separately as they naturally lead to teardown.
> + */
> +enum realm_state {
> + /**
> + * @REALM_STATE_NONE:
> + * Realm has not yet been created. rmi_realm_create() has not
> + * yet been called.
> + */
> + REALM_STATE_NONE,
> + /**
> + * @REALM_STATE_NEW:
> + * Realm is under construction, Realm Descriptor has been created,
> + * but it is not yet activated. Memory may be populated.
> + */
> + REALM_STATE_NEW,
> + /**
> + * @REALM_STATE_ACTIVE:
> + * Realm has been created and is eligible for execution with
> + * rmi_rec_enter(). Memory may no longer be populated with known
> + * contents.
> + */
> + REALM_STATE_ACTIVE,
> + /**
> + * @REALM_STATE_DYING:
> + * Realm is in the process of being destroyed or has already been
> + * destroyed.
> + */
> + REALM_STATE_DYING,
> + /**
> + * @REALM_STATE_DEAD:
> + * Realm has been destroyed.
> + */
> + REALM_STATE_DEAD
> +};
^ permalink raw reply [flat|nested] 26+ messages in thread
* Re: [PATCH v6 10/11] KVM: arm64: CCA: Support runtime faulting of memory
2026-12-04 15:32 ` Kohei Enju
@ 2026-12-04 16:27 ` Steven Price
0 siblings, 0 replies; 26+ messages in thread
From: Steven Price @ 2026-12-04 16:27 UTC (permalink / raw)
To: Kohei Enju
Cc: kvm, kvmarm, Catalin Marinas, Marc Zyngier, Will Deacon,
James Morse, Oliver Upton, Suzuki K Poulose, Zenghui Yu,
linux-arm-kernel, linux-kernel, Joey Gouly, Alexandru Elisei,
Christoffer Dall, Fuad Tabba, linux-coco, Ganapatrao Kulkarni,
Gavin Shan, Shanker Donthineni, Alper Gun, Aneesh Kumar K . V,
Emi Kisanuki, Vishal Annapurve, WeiLin.Chang, Lorenzo Pieralisi
On 04/12/2026 15:32, Kohei Enju wrote:
> On 12/04 11:57, Steven Price wrote:
>> At 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.
>
> Hello Steve,
>
> Not a review comment, but just a question for clarification.
>
> My understanding is that this series and the following ones aim to
> provide basic CCA/KVM functionality, leaving out optional features for
> the initial upstream support.
>
> I've been looking into supporting pre-faulting for CCA/KVM in the
> future, and I'm not aware of any architectural blockers.
>
> Are you aware of any potential blockers or limitations that might
> prevent us from supporting pre-faulting in the future, or is it simply
> being left out for now as an *optional* feature, as I assume?
(I assume you mean stage-2 pre-faulting - i.e. populating the guest's
IPA-to-PA translations with KVM_PRE_FAULT_MEMORY, pre-faulting the
guest_memfd itself isn't specific to CCA.)
There's nothing architectural preventing pre-faulting. The host is free
to proactively map the protected memory into the guest before the first
fault.
My reasons for leaving it out now are:
* It's obviously an "optional" feature - and I'm trying to focus on
getting enough support to boot a guest before looking at more features.
* As a feature it could hide bugs - faulting is the more complex path,
so I've wanted to focus testing on that path rather than the pre-fault
path which is naturally less racy.
* It's a performance feature and I'm currently doing most of my testing
on a model. So I've no way of meaningfully telling whether it's actually
useful in reality. Although my suspicion is that it could turn out to be
important.
Note that KVM_PRE_FAULT_MEMORY is a VCPU ioctl, but the RMI API executes
it on the realm descriptor - so I'm not sure how much benefit there
would be to running multiple in parallel.
My assumption is that KVM_PRE_FAULT_MEMORY would be treated as a "first
VCPU run" and would trigger the realm setup (kvm_activate_realm()), but
that's certainly an area of uAPI that needs nailing down.
Let me know if you do come up with an implementation - hopefully it
won't be too long before we move on past the basic functionality.
Thanks,
Steve
^ permalink raw reply [flat|nested] 26+ messages in thread
* Re: [PATCH v6 07/11] KVM: arm64: CCA: Restrict Realm memslots to the lower IPA half
2026-12-04 15:48 ` Kohei Enju
@ 2026-12-04 16:27 ` Steven Price
0 siblings, 0 replies; 26+ messages in thread
From: Steven Price @ 2026-12-04 16:27 UTC (permalink / raw)
To: Kohei Enju
Cc: kvm, kvmarm, Suzuki K Poulose, Catalin Marinas, Marc Zyngier,
Will Deacon, James Morse, Oliver Upton, Zenghui Yu,
linux-arm-kernel, linux-kernel, Joey Gouly, Alexandru Elisei,
Christoffer Dall, Fuad Tabba, linux-coco, Ganapatrao Kulkarni,
Gavin Shan, Shanker Donthineni, Alper Gun, Aneesh Kumar K . V,
Emi Kisanuki, Vishal Annapurve, WeiLin.Chang, Lorenzo Pieralisi,
Fuad Tabba
On 04/12/2026 15:48, Kohei Enju wrote:
> On 12/04 11:57, Steven Price wrote:
>> From: Suzuki K Poulose <suzuki.poulose@arm.com>
>>
>> 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.
>>
>> Signed-off-by: Suzuki K Poulose <suzuki.poulose@arm.com>
>> Signed-off-by: Steven Price <steven.price@arm.com>
>> Reviewed-by: Fuad Tabba <fuad.tabba@linux.dev>
>>
>> ---
>> 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(-)
>>
>> diff --git a/arch/arm64/kvm/mmu.c b/arch/arm64/kvm/mmu.c
>> index 37adc20f0042..ce7cea447f58 100644
>> --- a/arch/arm64/kvm/mmu.c
>> +++ b/arch/arm64/kvm/mmu.c
>> @@ -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;
>
> Not a big deal, but if you resend this series, how about renaming
> ipa_limit to gfn_limit? The current name is a bit confusing to me
> because the value is GFNs rather than bytes.
Yes sure - that makes sense.
Thanks,
Steve
> Thanks,
> Kohei
>
>> + if (kvm_vm_is_realm(kvm))
>> + ipa_limit >>= 1;
>> + if (new->base_gfn + new->npages > ipa_limit)
>> return -EFAULT;
>>
>> /*
>> --
>> 2.43.0
>>
^ permalink raw reply [flat|nested] 26+ messages in thread
* Re: [PATCH v6 00/11] KVM: arm64: CCA: Prepare Realm memory management
2026-12-04 11:57 [PATCH v6 00/11] KVM: arm64: CCA: Prepare Realm memory management Steven Price
` (10 preceding siblings ...)
2026-12-04 11:57 ` [PATCH v6 11/11] KVM: arm64: CCA: Add Realm stage-2 MMU operations Steven Price
@ 2026-12-04 16:30 ` Kohei Enju
11 siblings, 0 replies; 26+ messages in thread
From: Kohei Enju @ 2026-12-04 16:30 UTC (permalink / raw)
To: Steven Price
Cc: kvm, kvmarm, Catalin Marinas, Marc Zyngier, Will Deacon,
James Morse, Oliver Upton, Suzuki K Poulose, Zenghui Yu,
linux-arm-kernel, linux-kernel, Joey Gouly, Alexandru Elisei,
Christoffer Dall, Fuad Tabba, linux-coco, Ganapatrao Kulkarni,
Gavin Shan, Shanker Donthineni, Alper Gun, Aneesh Kumar K . V,
Emi Kisanuki, Vishal Annapurve, WeiLin.Chang, Lorenzo Pieralisi
On 12/04 11:57, Steven Price wrote:
> 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.
Hi Steve,
As usual, I've tested the v6 integration branch on our FUJITSU-MONAKA
hardware and confirmed that a Realm VM booted successfully. I also
tested it by building the Linux kernel inside the VM. No issues observed
so far.
For the series:
Tested-by: Kohei Enju <enju.kohei@fujitsu.com>
Thanks,
Kohei
^ permalink raw reply [flat|nested] 26+ messages in thread
* Re: [PATCH v6 01/11] KVM: arm64: CCA: Introduce Realms
2026-12-04 16:07 ` Kohei Enju
@ 2026-12-04 17:05 ` Steven Price
0 siblings, 0 replies; 26+ messages in thread
From: Steven Price @ 2026-12-04 17:05 UTC (permalink / raw)
To: Kohei Enju
Cc: kvm, kvmarm, Suzuki K Poulose, Catalin Marinas, Marc Zyngier,
Will Deacon, James Morse, Oliver Upton, Zenghui Yu,
linux-arm-kernel, linux-kernel, Joey Gouly, Alexandru Elisei,
Christoffer Dall, Fuad Tabba, linux-coco, Ganapatrao Kulkarni,
Gavin Shan, Shanker Donthineni, Alper Gun, Aneesh Kumar K . V,
Emi Kisanuki, Vishal Annapurve, WeiLin.Chang, Lorenzo Pieralisi,
Fuad Tabba
On 04/12/2026 16:07, Kohei Enju wrote:
> On 12/04 11:57, Steven Price wrote:
>> From: Suzuki K Poulose <suzuki.poulose@arm.com>
>>
>> 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.
>>
>> Signed-off-by: Suzuki K Poulose <suzuki.poulose@arm.com>
>> Signed-off-by: Steven Price <steven.price@arm.com>
>> Reviewed-by: Fuad Tabba <fuad.tabba@linux.dev>
>
> Reviewed-by: Kohei Enju <enju.kohei@fujitsu.com>
>
> Minor nit below.
>
> [...]
>> +/**
>> + * enum realm_state - State of a Realm
>> + *
>> + * Mirrors the RMM's Realm lifecycle states where they are meaningful to KVM,
>> + * with REALM_STATE_DYING being a KVM-internal state used to prevent further
>
> Strictly speaking, REALM_STATE_NONE and REALM_STATE_DEAD are also
> KVM-internal states, so I think the comment should reflect this if you
> resend the series.
Good point - it's a little confusing not mentioning those two other states.
Thanks,
Steve
> Thanks,
> Kohei
>
>> + * requests while teardown is in progress. KVM does not track REALM_SYSTEM_OFF
>> + * or REALM_ZOMBIE separately as they naturally lead to teardown.
>> + */
>> +enum realm_state {
>> + /**
>> + * @REALM_STATE_NONE:
>> + * Realm has not yet been created. rmi_realm_create() has not
>> + * yet been called.
>> + */
>> + REALM_STATE_NONE,
>> + /**
>> + * @REALM_STATE_NEW:
>> + * Realm is under construction, Realm Descriptor has been created,
>> + * but it is not yet activated. Memory may be populated.
>> + */
>> + REALM_STATE_NEW,
>> + /**
>> + * @REALM_STATE_ACTIVE:
>> + * Realm has been created and is eligible for execution with
>> + * rmi_rec_enter(). Memory may no longer be populated with known
>> + * contents.
>> + */
>> + REALM_STATE_ACTIVE,
>> + /**
>> + * @REALM_STATE_DYING:
>> + * Realm is in the process of being destroyed or has already been
>> + * destroyed.
>> + */
>> + REALM_STATE_DYING,
>> + /**
>> + * @REALM_STATE_DEAD:
>> + * Realm has been destroyed.
>> + */
>> + REALM_STATE_DEAD
>> +};
^ permalink raw reply [flat|nested] 26+ messages in thread
end of thread, other threads:[~2026-12-04 17:05 UTC | newest]
Thread overview: 26+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-04 11:57 [PATCH v6 00/11] KVM: arm64: CCA: Prepare Realm memory management Steven Price
2026-12-04 11:57 ` [PATCH v6 01/11] KVM: arm64: CCA: Introduce Realms Steven Price
2026-12-04 16:07 ` Kohei Enju
2026-12-04 17:05 ` Steven Price
2026-12-04 11:57 ` [PATCH v6 02/11] KVM: arm64: Pass range filter to stage2_unmap_range Steven Price
2026-12-04 11:57 ` [PATCH v6 03/11] KVM: arm64: Dispatch stage-2 destruction by VM flavor Steven Price
2026-12-04 11:57 ` [PATCH v6 04/11] KVM: arm64: CCA: Add basic infrastructure for creating a realm Steven Price
2026-12-04 12:15 ` sashiko-bot
2026-12-04 11:57 ` [PATCH v6 05/11] KVM: arm64: CCA: Add Realm RTT mapping and invalidation helpers Steven Price
2026-12-04 12:28 ` sashiko-bot
2026-12-04 11:57 ` [PATCH v6 06/11] KVM: arm64: CCA: Destroy RTTs Steven Price
2026-12-04 12:28 ` sashiko-bot
2026-12-04 11:57 ` [PATCH v6 07/11] KVM: arm64: CCA: Restrict Realm memslots to the lower IPA half Steven Price
2026-12-04 15:48 ` Kohei Enju
2026-12-04 16:27 ` Steven Price
2026-12-04 11:57 ` [PATCH v6 08/11] KVM: arm64: CCA: Advertise private memory support for Realms Steven Price
2026-12-04 12:23 ` sashiko-bot
2026-12-04 11:57 ` [PATCH v6 09/11] KVM: arm64: Select protected VM behaviour by VM flavour Steven Price
2026-12-04 13:21 ` Fuad Tabba
2026-12-04 11:57 ` [PATCH v6 10/11] KVM: arm64: CCA: Support runtime faulting of memory Steven Price
2026-12-04 12:39 ` sashiko-bot
2026-12-04 15:32 ` Kohei Enju
2026-12-04 16:27 ` Steven Price
2026-12-04 11:57 ` [PATCH v6 11/11] KVM: arm64: CCA: Add Realm stage-2 MMU operations Steven Price
2026-12-04 12:44 ` sashiko-bot
2026-12-04 16:30 ` [PATCH v6 00/11] KVM: arm64: CCA: Prepare Realm memory management Kohei Enju
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox