Skip to contentSkip to conversation

KVM: arm64: CCA: Prepare Realm memory management

PATCH Superseded Steven Price 6 participants 45 messages Nov 16 – Nov 23

Subsystems (4)

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

v3 cover KVM: arm64: CCA: Prepare Realm memory management

This series adds the memory management plumbing for CCA, building on the basic RMM support [1]. Thanks to everyone who reviewed and tested v2.

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 v2:
- Move RMM mode reservation and RMI availability and feature checks to the integration stack.
- Require mmap-capable guest_memfd backing for all arm64 VMs and route all guest_memfd faults through gmem_abort().
- Store the Realm Descriptor physical address from initial Realm setup, and improve SRO cleanup and RTT teardown error handling.
- Use a local MMU cache for RTT invalidation, handle failed splits, and validate returned RTT levels.
- Bound protected RMI mapping calls to the encoded range and tighten RTT creation, retry, and error handling.
- Split common stage-2 destruction dispatch into its own patch before registering the Realm stage-2 MMU operations.

Changes since v1:
- Add a guest_memfd prerequisite patch that checks whether the backing file supports mmap when preparing an arm64 memslot.
- Combine the RMI availability and feature checks into one patch, and move Realm vCPU feature restrictions to the integration stack.
- Correct Realm IPA sizing and RTT teardown, including teardown before activation, RTT accounting, and handling of missing or folded RTTs.
- Rework RTT mapping to respect IPA and physical alignment, handle partial RMI progress, and retry interrupted operations.
- Preserve private mappings when a Realm guest_memfd memslot deletion is rejected, and tighten Realm fault handling and stage-2 destruction.

[1] https://lore.kernel.org/all/[email protected]/
[2] https://gitlab.arm.com/linux-arm/linux-cca
branch: cca-host-mm/v3/integration
[3] https://gitlab.arm.com/linux-arm/linux-cca
branch: cca-host-mm/v3/mm

Steven Price (6):
KVM: arm64: guest_memfd: Check mmap support when preparing memslot
KVM: arm64: CCA: Add basic infrastructure for creating a realm
KVM: arm64: CCA: Tear down RTTs
KVM: arm64: CCA: Add Realm RTT mapping and invalidation helpers
KVM: arm64: CCA: Support runtime faulting of memory
KVM: arm64: Dispatch stage-2 destruction by VM flavor

Suzuki K Poulose (5):
KVM: arm64: CCA: Introduce Realms
KVM: arm64: Handle attributes in stage2_unmap_range
KVM: Allow architectures to reject memslot changes before invalidation
KVM: arm64: CCA: Prevent memslot modifications after Realm creation
KVM: arm64: CCA: Add Realm stage-2 MMU operations

 arch/arm64/include/asm/kvm_emulate.h   |  29 +
 arch/arm64/include/asm/kvm_host.h      |  52 +-
 arch/arm64/include/asm/kvm_mmu.h       |   4 +-
 arch/arm64/include/asm/kvm_rmi.h       | 103 +++
 arch/arm64/kvm/Kconfig                 |   1 +
 arch/arm64/kvm/Makefile                |   2 +-
 arch/arm64/kvm/arm.c                   |  16 +-
 arch/arm64/kvm/hyp/include/nvhe/pkvm.h |   2 +-
 arch/arm64/kvm/hyp/nvhe/switch.c       |   4 +-
 arch/arm64/kvm/hyp/nvhe/timer-sr.c     |   2 +-
 arch/arm64/kvm/mmu.c                   | 291 ++++++--
 arch/arm64/kvm/pkvm.c                  |   4 +-
 arch/arm64/kvm/psci.c                  |   2 +-
 arch/arm64/kvm/rmi.c                   | 928 +++++++++++++++++++++++++
 drivers/firmware/arm_rmm/rmi.c         |  10 -
 include/linux/arm-rmi-cmds.h           |   8 +
 include/linux/kvm_host.h               |   5 +
 virt/kvm/guest_memfd.c                 |   6 +
 virt/kvm/kvm_main.c                    |  33 +-
 19 files changed, 1404 insertions(+), 98 deletions(-)
 create mode 100644 arch/arm64/include/asm/kvm_rmi.h
 create mode 100644 arch/arm64/kvm/rmi.c
Signature · 1 line
2.43.0
#1 lore raw

PATCH v3 1/11 KVM: arm64: guest_memfd: Check mmap support when preparing memslot

Check the guest_memfd backing file when deciding whether an arm64 memslot has mappable memory rather than checking whether the slot is gmem only. A public helper function is added to be able to reuse the static kvm_gmem_supports_mmap() function.

---
Changes since v2:
- Require mmap-capable guest_memfd backing for all arm64 VMs, as shared-memory access can use the memslot HVA.

Changes since v1:
- New patch in v2. Check the guest_memfd backing file rather than the memslot flag when deciding whether arm64 can map the backing memory.

---
 arch/arm64/kvm/mmu.c     | 6 +++---
 include/linux/kvm_host.h | 1 +
 virt/kvm/guest_memfd.c   | 6 ++++++
 3 files changed, 10 insertions(+), 3 deletions(-)
arch/arm64/kvm/mmu.c +3 -3
index 1db10d8d6db7..2825fccd2a19 100644
@@ -2839,10 +2839,10 @@ int kvm_arch_prepare_memory_region(struct kvm *kvm,
 		return -EFAULT;
 
 	/*
-	 * Only support guest_memfd backed memslots with mappable memory, since
-	 * there aren't any CoCo VMs that support only private memory on arm64.
+	 * All VMs need mappable guest_memfd memory, as the memslot's HVA may be
+	 * used to access shared memory even if guest faults use guest_memfd.
 	 */
-	if (kvm_slot_has_gmem(new) && !kvm_memslot_is_gmem_only(new))
+	if (kvm_slot_has_gmem(new) && !kvm_gmem_slot_supports_mmap(new))
 		return -EINVAL;
 
 	/* guest_memfd is incompatible with MTE. */
include/linux/kvm_host.h +1 -0
index e7fc94e2910a..f65728a713ec 100644
@@ -2614,6 +2614,7 @@ static inline bool kvm_is_private_gfn(struct kvm *kvm, gfn_t gfn)
 
 #ifdef CONFIG_KVM_GUEST_MEMFD
 bool kvm_gmem_is_private_gfn(struct kvm *kvm, gfn_t gfn);
+bool kvm_gmem_slot_supports_mmap(const struct kvm_memory_slot *slot);
 bool kvm_arch_supports_gmem_init_shared(struct kvm *kvm);
 
 static inline u64 kvm_gmem_get_supported_flags(struct kvm *kvm)
virt/kvm/guest_memfd.c +6 -0
index 8c97a69b16dc..3dd8d227bebb 100644
@@ -442,6 +442,12 @@ static bool kvm_gmem_supports_mmap(struct inode *inode)
 	return GMEM_I(inode)->flags & GUEST_MEMFD_FLAG_MMAP;
 }
 
+bool kvm_gmem_slot_supports_mmap(const struct kvm_memory_slot *slot)
+{
+	/* The guest_memfd file is pinned while preparing a new memslot. */
+	return kvm_gmem_supports_mmap(file_inode(slot->gmem.file));
+}
+
 static vm_fault_t kvm_gmem_fault_user_mapping(struct vm_fault *vmf)
 {
 	struct inode *inode = file_inode(vmf->vma->vm_file);
Signature · 1 line
2.43.0
#2 lore raw

Re: [PATCH v3 01/11] KVM: arm64: guest_memfd: Check mmap support when preparing memslot

Hi Steven,

Quoted 4 lines · Steven Price

Check the guest_memfd backing file when deciding whether an arm64 memslot has mappable memory rather than checking whether the slot is gmem only. A public helper function is added to be able to reuse the static kvm_gmem_supports_mmap() function.

nit: could the message say why this is needed? It's a no-op until patch 8 defines kvm_arch_has_private_mem(), which makes every guest_memfd slot gmem-only.

Cheers,
/fuad

#3 lore raw

Re: [PATCH v3 01/11] KVM: arm64: guest_memfd: Check mmap support when preparing memslot

Hi Steven

Quoted 11 lines · Fuad Tabba

Hi Steven,

Quoted 4 lines · Steven Price

Check the guest_memfd backing file when deciding whether an arm64 memslot has mappable memory rather than checking whether the slot is gmem only. A public helper function is added to be able to reuse the static kvm_gmem_supports_mmap() function.

nit: could the message say why this is needed? It's a no-op until patch 8 defines kvm_arch_has_private_mem(), which makes every guest_memfd slot gmem-only.

I still don't understand why this is needed. For arm64 we have always prevented guest_memfd without MMAP, in kvm_arch_prepare_memory_region().

Suzuki

Quoted 3 lines

Cheers,
/fuad

#4 lore raw

Re: [PATCH v3 01/11] KVM: arm64: guest_memfd: Check mmap support when preparing memslot

Quoted 12 lines · Suzuki K Poulose

Hi Steven

Quoted 9 lines · Fuad Tabba

Hi Steven,

Quoted 4 lines · Steven Price

Check the guest_memfd backing file when deciding whether an arm64 memslot has mappable memory rather than checking whether the slot is gmem only. A public helper function is added to be able to reuse the static kvm_gmem_supports_mmap() function.

Quoted 6 lines
Quoted 3 lines

nit: could the message say why this is needed? It's a no-op until patch 8 defines kvm_arch_has_private_mem(), which makes every guest_memfd slot gmem-only.

I still don't understand why this is needed. For arm64 we have always prevented guest_memfd without MMAP, in kvm_arch_prepare_memory_region().

Fuad is correct, this isn't really needed until patch 8. Patch 8 defines kvm_arch_has_private_mem() - that means that gmem_in_place_conversion becomes true (see include/linux/kvm_host.h). That in turn means that every guest_memfd slot is KVM_MEMSLOT_GMEM_ONLY including slots backed by a file without mmap support.

But regardless the check for kvm_memslot_is_gmem_only() isn't really what the code is trying to do. The fact that the memslot is "gmem only" isn't really relevant. The thing we care about is the mappability.

But I agree the commit message could do with improving. I'll expand that.

Thanks,
Steve

#5 lore raw

Re: [PATCH v3 01/11] KVM: arm64: guest_memfd: Check mmap support when preparing memslot

Quoted 20 lines · Steven Price
Quoted 12 lines · Suzuki K Poulose

Hi Steven

Quoted 9 lines · Fuad Tabba

Hi Steven,

Quoted 4 lines · Steven Price

Check the guest_memfd backing file when deciding whether an arm64 memslot has mappable memory rather than checking whether the slot is gmem only. A public helper function is added to be able to reuse the static kvm_gmem_supports_mmap() function.

Quoted 6 lines
Quoted 3 lines

nit: could the message say why this is needed? It's a no-op until patch 8 defines kvm_arch_has_private_mem(), which makes every guest_memfd slot gmem-only.

I still don't understand why this is needed. For arm64 we have always prevented guest_memfd without MMAP, in kvm_arch_prepare_memory_region().

Quoted 6 lines

Fuad is correct, this isn't really needed until patch 8. Patch 8 defines kvm_arch_has_private_mem() - that means that gmem_in_place_conversion becomes true (see include/linux/kvm_host.h). That in turn means that

every guest_memfd slot is KVM_MEMSLOT_GMEM_ONLY including slots backed by a file without mmap support.

But we dont support slots without MMAP anyways even without this series.
So, does it really matter for arm64 ?

Suzuki

Quoted 9 lines

But regardless the check for kvm_memslot_is_gmem_only() isn't really what the code is trying to do. The fact that the memslot is "gmem only" isn't really relevant. The thing we care about is the mappability.

But I agree the commit message could do with improving. I'll expand that.

Thanks,
Steve

#6 lore raw

Re: [PATCH v3 01/11] KVM: arm64: guest_memfd: Check mmap support when preparing memslot

Quoted 25 lines · Suzuki K Poulose
Quoted 23 lines · Steven Price
Quoted 12 lines · Suzuki K Poulose

Hi Steven

Quoted 9 lines · Fuad Tabba

Hi Steven,

Quoted 4 lines · Steven Price

Check the guest_memfd backing file when deciding whether an arm64 memslot has mappable memory rather than checking whether the slot is gmem only. A public helper function is added to be able to reuse the static kvm_gmem_supports_mmap() function.

Quoted 6 lines
Quoted 3 lines

nit: could the message say why this is needed? It's a no-op until patch 8 defines kvm_arch_has_private_mem(), which makes every guest_memfd slot gmem-only.

I still don't understand why this is needed. For arm64 we have always prevented guest_memfd without MMAP, in kvm_arch_prepare_memory_region().

Fuad is correct, this isn't really needed until patch 8. Patch 8 defines kvm_arch_has_private_mem() - that means that gmem_in_place_conversion becomes true (see include/linux/kvm_host.h). That in turn means that

Quoted 5 lines
Quoted 2 lines

every guest_memfd slot is KVM_MEMSLOT_GMEM_ONLY including slots backed by a file without mmap support.

But we dont support slots without MMAP anyways even without this series. So, does it really matter for arm64 ?

So ChatGPT produced the attached test. It creates a non-mappable gmem and tries to add it to a normal (non-realm/non-pkvm) VM. With v7.4-rc1 it is rejected. However without this patch it succeeds.

The issue is that gmem_in_place_conversion changes the behaviour for normal VMs.

Thanks,
Steve

// SPDX-License-Identifier: GPL-2.0-only
/*
* Reproduce the non-Realm guest_memfd memslot regression.
* Build from the kernel root with:
* cc -Wall -Wextra -O2 -Itools/include -Itools/include/uapi \
* tools/testing/selftests/kvm/arm64/guest_memfd_nommap_repro.c -o /tmp/gmem-repro
*/
#include <errno.h>
#include <fcntl.h>
#include <linux/kvm.h>
#include <stdint.h>
#include <stdio.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <unistd.h>

int main(void)

{
	long size = sysconf(_SC_PAGESIZE);
	struct kvm_create_guest_memfd gmem = { .size = size };
	struct kvm_userspace_memory_region2 region = {
		.flags = KVM_MEM_GUEST_MEMFD,
		.memory_size = size,
	};
	int kvm, vm, fd, ret, error;
	void *hva;

	if (size <= 0)
		return 1;
	kvm = open("/dev/kvm", O_RDWR | O_CLOEXEC);
	if (kvm < 0) {
		perror("open /dev/kvm");
		return 1;
	}
	vm = ioctl(kvm, KVM_CREATE_VM, 0);
	if (vm < 0) {
		perror("KVM_CREATE_VM");
		return 1;
	}
	fd = ioctl(vm, KVM_CREATE_GUEST_MEMFD, &gmem);
	if (fd < 0) {
		perror("KVM_CREATE_GUEST_MEMFD");
		return 1;
	}
	hva = mmap(NULL, size, PROT_READ | PROT_WRITE,
		   MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
	if (hva == MAP_FAILED) {
		perror("mmap");
		return 1;
	}
	region.userspace_addr = (uintptr_t)hva;
	region.guest_memfd = fd;
	ret = ioctl(vm, KVM_SET_USER_MEMORY_REGION2, &region);
	error = errno;
	if (ret == -1 && error == EINVAL) {
		puts("PASS: non-mappable guest_memfd memslot rejected");
		return 0;
	}
	fprintf(stderr, "FAIL: KVM_SET_USER_MEMORY_REGION2 returned %d (errno %d)\n",
		ret, ret < 0 ? error : 0);
	return 1;
}
#7 lore raw

Re: [PATCH v3 01/11] KVM: arm64: guest_memfd: Check mmap support when preparing memslot

Cc Ackerley, Sean
Quoted 32 lines · Steven Price
Quoted 25 lines · Suzuki K Poulose
Quoted 23 lines · Steven Price
Quoted 12 lines · Suzuki K Poulose

Hi Steven

Quoted 9 lines · Fuad Tabba

Hi Steven,

Quoted 4 lines · Steven Price

Check the guest_memfd backing file when deciding whether an arm64 memslot has mappable memory rather than checking whether the slot is gmem only. A public helper function is added to be able to reuse the static kvm_gmem_supports_mmap() function.

Quoted 6 lines
Quoted 3 lines

nit: could the message say why this is needed? It's a no-op until patch 8 defines kvm_arch_has_private_mem(), which makes every guest_memfd slot gmem-only.

I still don't understand why this is needed. For arm64 we have always prevented guest_memfd without MMAP, in kvm_arch_prepare_memory_region().

Fuad is correct, this isn't really needed until patch 8. Patch 8 defines kvm_arch_has_private_mem() - that means that gmem_in_place_conversion becomes true (see include/linux/kvm_host.h). That in turn means that

Quoted 5 lines
Quoted 2 lines

every guest_memfd slot is KVM_MEMSLOT_GMEM_ONLY including slots backed by a file without mmap support.

But we dont support slots without MMAP anyways even without this series.
 So, does it really matter for arm64 ?

Quoted 6 lines

So ChatGPT produced the attached test. It creates a non-mappable gmem and tries to add it to a normal (non-realm/non-pkvm) VM. With v7.4-rc1 it is rejected. However without this patch it succeeds.

The issue is that gmem_in_place_conversion changes the behaviour for normal VMs.

I see, thanks Steven. For context:

Arm64 currently (without inplace conversion support) only allows GMEM with MMAP flags only and relies on kvm_slot_gmem_only() to detect that the GMEM has MMAP flag. But, when we enable inplace conversion, we can't reliably reject the slots by kvm_slot_gmem_only(), because all GMEM slots are marked GMEM only with inplace conversion.

Also, from the API Documentation,

"When the KVM MMU performs a PFN lookup to service a guest fault, the fault will always be consumed from guest_memfd, regardless of whether it is a shared or a private fault (unless in-place conversion is disabled and the backing guest_memfd does not have the GUEST_MEMFD_FLAG_MMAP flag set)."

So, in effect, with in-place conversion enabled a KVM_CREATE_GUEST_MEMFD without GUEST_MEMFD_FLAG_MMAP is an error and thats what this patch is trying to fix in arm64 KVM. The question is:

1) Should this be fixed ? How does TDX/SNP support older VMMs with inplace conversion ? What was the conclusion on this breakage ?

2) If it should be, then shouldn't it be done in kvm_gmem_create() ?

Suzuki

Quoted 4 lines

Thanks,
Steve

#8 lore raw

Re: [PATCH v3 01/11] KVM: arm64: guest_memfd: Check mmap support when preparing memslot

Quoted 10 lines · Suzuki K Poulose

I see, thanks Steven. For context:

Arm64 currently (without inplace conversion support) only allows GMEM with MMAP flags only and relies on kvm_slot_gmem_only() to detect that the GMEM has MMAP flag. But, when we enable inplace conversion, we can't reliably reject the slots by kvm_slot_gmem_only(), because all GMEM slots are marked GMEM only with inplace conversion.

Also, from the API Documentation,

Quoted 7 lines

"When the KVM MMU performs a PFN lookup to service a guest fault, the fault will always be consumed from guest_memfd, regardless of whether it is a shared or a private fault (unless in-place conversion is disabled and the backing guest_memfd does not have the GUEST_MEMFD_FLAG_MMAP flag set)."

So, in effect, with in-place conversion enabled a KVM_CREATE_GUEST_MEMFD without GUEST_MEMFD_FLAG_MMAP is an error

It's only an error if (a) the userspace address in the memslot points at the gmem fd and (b) something in KVM tries to actually GUP the memslot using the userspace virtual address.

Quoted 4 lines

and thats what this patch is trying to fix in arm64 KVM. The question is:

1) Should this be fixed ? How does TDX/SNP support older VMMs with inplace conversion ? What was the conclusion on this breakage ?

There is no "breakage". If userspace doesn't point the userspace address at a valid VMA, that userspace's problem. The only thing the early check would do is prevent weird use cases that may or may not be valid (and for which KVM doesn't care), and error out earlier if KVM guesses right and userspace really did goof.

Quoted 1 line

2) If it should be, then shouldn't it be done in kvm_gmem_create() ?

No. As above, I don't see any reason to have the check at all.

#9 lore raw

Re: [PATCH v3 01/11] KVM: arm64: guest_memfd: Check mmap support when preparing memslot

Quoted 18 lines · Sean Christopherson
Quoted 15 lines · Suzuki K Poulose

I see, thanks Steven. For context:

Arm64 currently (without inplace conversion support) only allows GMEM with MMAP flags only and relies on kvm_slot_gmem_only() to detect that the GMEM has MMAP flag. But, when we enable inplace conversion, we can't reliably reject the slots by kvm_slot_gmem_only(), because all GMEM slots are marked GMEM only with inplace conversion.

Also, from the API Documentation,

"When the KVM MMU performs a PFN lookup to service a guest fault, the fault will always be consumed from guest_memfd, regardless of whether it is a shared or a private fault (unless in-place conversion is disabled and the backing guest_memfd does not have the GUEST_MEMFD_FLAG_MMAP flag set)."

Quoted 5 lines
Quoted 2 lines

So, in effect, with in-place conversion enabled a KVM_CREATE_GUEST_MEMFD without GUEST_MEMFD_FLAG_MMAP is an error

It's only an error if (a) the userspace address in the memslot points at the gmem fd and

I don't understand this part. With inplace conversion, we mark the slot as GMEM_ONLY and always resolve shared faults from the gmem (on arm64 as described by the API). So, isn't it a usage error from the VMM if it plans to use GUEST_MEMFD_FLAG_MMAP=false with inplace conversion ?

In other words, do you mean to say, passing a non-gmem fd userspace address is fine with GUEST_MEMFD_FLAG_MMAP=false ? But the API documentation and the GMEM_ONLY flags contradict the usage.

Now, should we reject that in KVM or not is the secondary question.

(b) something in KVM tries to actually GUP the memslot using the

Quoted 1 line

userspace virtual address.

Agreed

Suzuki

Quoted 14 lines
Quoted 5 lines

and thats what this patch is trying to fix in arm64 KVM. The question is:

1) Should this be fixed ? How does TDX/SNP support older VMMs with inplace conversion ? What was the conclusion on this breakage ?

There is no "breakage". If userspace doesn't point the userspace address at a valid VMA, that userspace's problem. The only thing the early check would do is prevent weird use cases that may or may not be valid (and for which KVM doesn't care), and error out earlier if KVM guesses right and userspace really did goof.

Quoted 1 line

2) If it should be, then shouldn't it be done in kvm_gmem_create() ?

No. As above, I don't see any reason to have the check at all.

#10 lore raw

PATCH v3 2/11 KVM: arm64: CCA: Introduce Realms

In reply to Steven Price

From: Suzuki K Poulose <[email protected]>

Introduce the Realm VM flavor and per-VM state tracking. Realm and protected pKVM states are mutually exclusive, so share their storage in kvm_arch. Adjust the protected VM helpers for the new flavor.

Realm VMs cannot be created yet; RMI initialization and the creation UABI follow in later patches.

---
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      | 47 ++++++++++++--------
 arch/arm64/include/asm/kvm_rmi.h       | 59 ++++++++++++++++++++++++++
 arch/arm64/kvm/arm.c                   |  7 ++-
 arch/arm64/kvm/hyp/include/nvhe/pkvm.h |  2 +-
 arch/arm64/kvm/hyp/nvhe/switch.c       |  4 +-
 arch/arm64/kvm/hyp/nvhe/timer-sr.c     |  2 +-
 arch/arm64/kvm/mmu.c                   |  1 +
 arch/arm64/kvm/pkvm.c                  |  2 +-
 arch/arm64/kvm/psci.c                  |  2 +-
 10 files changed, 117 insertions(+), 25 deletions(-)
 create mode 100644 arch/arm64/include/asm/kvm_rmi.h
arch/arm64/include/asm/kvm_emulate.h +16 -0
index 334211b8b2ee..32021bdf2610 100644
@@ -831,4 +831,20 @@ static inline void kvm_reset_vcpu_psci(struct kvm_vcpu *vcpu,
 	vcpu_set_reg(vcpu, 0, reset_state->r0);
 }
 
+static inline enum realm_state kvm_realm_state(struct kvm *kvm)
+{
+	return READ_ONCE(kvm->arch.realm.state);
+}
+
+static inline void kvm_set_realm_state(struct kvm *kvm,
+				       enum realm_state new_state)
+{
+	WRITE_ONCE(kvm->arch.realm.state, new_state);
+}
+
+static inline bool kvm_realm_is_created(struct kvm *kvm)
+{
+	return kvm_vm_is_realm(kvm) && kvm_realm_state(kvm) != REALM_STATE_NONE;
+}
+
 #endif /* __ARM64_KVM_EMULATE_H__ */
arch/arm64/include/asm/kvm_host.h +30 -17

84 lines not on this page · raw message

arch/arm64/include/asm/kvm_rmi.h new file +59 -0

59 lines not on this page · raw message

arch/arm64/kvm/arm.c +5 -2
index 157bf4b81202..e06cc390a3d6 100644
@@ -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_is_protected_pkvm(vcpu) ?
+		       KVM_MP_STATE_UNINITIALIZED : KVM_MP_STATE_STOPPED;
 
 	spin_lock(&vcpu->arch.mp_state_lock);
 	__kvm_arm_vcpu_power_off(vcpu, mp_state);
@@ -906,7 +907,8 @@ static bool kvm_arm_vcpu_suspended(struct kvm_vcpu *vcpu)
 /* Only the guest's CPU_ON may start a vCPU EL2 holds powered off. */
 bool kvm_pkvm_vcpu_is_powered_off(struct kvm_vcpu *vcpu)
 {
-	return READ_ONCE(vcpu->arch.mp_state.mp_state) == KVM_MP_STATE_UNINITIALIZED;
+	return vcpu_is_protected_pkvm(vcpu) &&
+		READ_ONCE(vcpu->arch.mp_state.mp_state) == KVM_MP_STATE_UNINITIALIZED;
 }
 
 int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu,
@@ -2312,6 +2314,7 @@ static const struct kvm_vcpu_ops *arm64_vcpu_ops[] = {
 	KVM_VCPU_OPS(VM_VHE, vhe_vcpu_ops),
 	KVM_VCPU_OPS(VM_PKVM, pkvm_vcpu_ops),
 	KVM_VCPU_OPS(VM_PROTECTED_PKVM, pkvm_vcpu_ops),
+	[VM_REALM] = NULL,
 };
 
 static void kvm_init_vcpu_ops(struct kvm_vcpu *vcpu)
arch/arm64/kvm/hyp/include/nvhe/pkvm.h +1 -1
index 0261b2bc681f..ae6ced6d7b9e 100644
@@ -84,7 +84,7 @@ pkvm_hyp_vcpu_to_hyp_vm(struct pkvm_hyp_vcpu *hyp_vcpu)
 
 static inline bool pkvm_hyp_vcpu_is_protected(struct pkvm_hyp_vcpu *hyp_vcpu)
 {
-	return vcpu_is_protected(&hyp_vcpu->vcpu);
+	return vcpu_is_protected_pkvm(&hyp_vcpu->vcpu);
 }
 
 static inline bool pkvm_hyp_vm_is_protected(struct pkvm_hyp_vm *hyp_vm)
arch/arm64/kvm/hyp/nvhe/switch.c +2 -2
index 89fb9dd351ad..6c03f48e6be3 100644
@@ -238,7 +238,7 @@ static const exit_handler_fn pvm_exit_handlers[] = {
 
 static const exit_handler_fn *kvm_get_exit_handler_array(struct kvm_vcpu *vcpu)
 {
-	if (unlikely(vcpu_is_protected(vcpu)))
+	if (unlikely(vcpu_is_protected_pkvm(vcpu)))
 		return pvm_exit_handlers;
 
 	return hyp_exit_handlers;
@@ -259,7 +259,7 @@ static inline bool fixup_guest_exit(struct kvm_vcpu *vcpu, u64 *exit_code)
 	 * it.  The check below is based on the one in
 	 * kvm_arch_vcpu_ioctl_run().
 	 */
-	if (unlikely(vcpu_is_protected(vcpu) && vcpu_mode_is_32bit(vcpu))) {
+	if (unlikely(vcpu_is_protected_pkvm(vcpu) && vcpu_mode_is_32bit(vcpu))) {
 		/*
 		 * As we have caught the guest red-handed, decide that it isn't
 		 * fit for purpose anymore by making the vcpu invalid. The VMM
arch/arm64/kvm/hyp/nvhe/timer-sr.c +1 -1
index 993065716913..17bd1ff621ab 100644
@@ -48,7 +48,7 @@ void __timer_enable_traps(struct kvm_vcpu *vcpu)
 	 * or running a protected VM (we don't offset anything in this case).
 	 */
 	clr = CNTHCTL_EL1PCEN;
-	if (vcpu_is_protected(vcpu) ||
+	if (vcpu_is_protected_pkvm(vcpu) ||
 	    !timer_get_offset(vcpu_ptimer(vcpu)))
 		set |= CNTHCTL_EL1PCTEN;
 	else
arch/arm64/kvm/mmu.c +1 -0
index 2825fccd2a19..09c831a49ed3 100644
@@ -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)
arch/arm64/kvm/pkvm.c +1 -1
index 016997288fd2..a5145bd75d0c 100644
@@ -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_is_protected_pkvm(vcpu)) {
 		spin_lock(&vcpu->arch.mp_state_lock);
 		if (kvm_arm_vcpu_stopped(vcpu))
 			WRITE_ONCE(vcpu->arch.mp_state.mp_state, KVM_MP_STATE_UNINITIALIZED);
arch/arm64/kvm/psci.c +1 -1
index b01f8d44033f..6674e8d6ff4c 100644
@@ -68,7 +68,7 @@ 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_is_protected_pkvm(vcpu) && !kvm_pkvm_vcpu_is_powered_off(vcpu))) {
 		if (kvm_psci_version(source_vcpu) != KVM_ARM_PSCI_0_1)
 			ret = PSCI_RET_ALREADY_ON;
 		else
Signature · 1 line
2.43.0
#11 lore raw

Re: [PATCH v3 02/11] KVM: arm64: CCA: Introduce Realms

On Mon, 16 Nov 2026 16:43:48 +0000, Steven Price <[email protected]> wrote:
[...]

Quoted 1 line
arch/arm64/include/asm/kvm_host.h +0 -0

[...]

Quoted 32 lines
arch/arm64/include/asm/kvm_host.h +17 -5
@@ -1571,31 +1576,39 @@ struct kvm *kvm_arch_alloc_vm(void);
 
 #define __KVM_HAVE_ARCH_FLUSH_REMOTE_TLBS_RANGE
 
-#ifdef __KVM_NVHE_HYPERVISOR__
+#define kvm_vm_is_protected(kvm)        ((kvm)->arch.vm_flavor >= __VM_PROTECTED)
 
-#define kvm_vm_is_protected(kvm)                        \
-        (is_protected_kvm_enabled() && ((kvm)->arch.vm_flavor == VM_PROTECTED_PKVM))
+#ifdef __KVM_NVHE_HYPERVISOR__
 /*
  * Accessing vcpu->kvm from nVHE hyp stub is tricky, as we need to convert the
  * pointer to the hyp VA. With pKVM, the nVHE code runs with the hyp_vcpu,
- * which is populated correctly and is gated on is_protected_kvm_enabled().
+ * which is populated correctly. Always vcpu_is_protected_pkvm(), which is
+ * gated on is_protected_kvm_enabled().
  */
-#define vcpu_is_protected(vcpu)                                                \
+#define vcpu_is_protected(vcpu)                BUILD_BUG_ON(1)
+#else
+#define vcpu_is_protected(vcpu)                kvm_vm_is_protected((vcpu)->kvm)
+#endif
+
+#define kvm_vm_is_protected_pkvm(kvm)                                        \
+        (is_protected_kvm_enabled() && ((kvm)->arch.vm_flavor == VM_PROTECTED_PKVM))
+
+/*
+ * Rely on is_protected_kvm_enabled() check in kvm_vm_is_protected_pkvm() to
+ * make sure the vcpu->kvm is always valid VA in the context
+ */
+#define vcpu_is_protected_pkvm(vcpu)                                        \
         ({                                                                \

arch/arm64/include/asm/kvm_host.h struct kvm *kvm_arch_alloc_vm(void); · line 1593

Quoted 10 lines
arch/arm64/include/asm/kvm_host.h +1 -5
                 struct kvm *__kvm = READ_ONCE((vcpu)->kvm);                \
                                                                         \
-                (__kvm && kvm_vm_is_protected(__kvm));                        \
+                (__kvm && kvm_vm_is_protected_pkvm(__kvm));                \
         })
 
-#else
-
-#define kvm_vm_is_protected(kvm)        ((kvm)->arch.vm_flavor >= __VM_PROTECTED)
-#define vcpu_is_protected(vcpu)                kvm_vm_is_protected((vcpu)->kvm)

This brings back the _pkvm variants that Marc asked to drop in the plumbing series [1]. In the nVHE hyp code, kvm_vm_is_protected() and vcpu_is_protected() already mean pKVM-protected, so I think the hyp side can stay as it is.

At EL1, the pKVM-only checks that a Realm vCPU also reaches are in kvm_arm_vcpu_power_off() and kvm_psci_vcpu_on(). Could they check vm_flavor == VM_PROTECTED_PKVM directly?

arch/arm64/include/asm/kvm_host.h struct kvm *kvm_arch_alloc_vm(void); · line 1608

Quoted 2 lines
arch/arm64/include/asm/kvm_host.h +2 -0
+#define kvm_vm_is_realm(kvm)                ((kvm)->arch.vm_flavor == VM_REALM)
+#define vcpu_is_rec(vcpu)                kvm_vm_is_realm((vcpu)->kvm)

vcpu_is_rec() isn't used anywhere in this series. Could it be dropped?

[...]

Quoted 1 line
arch/arm64/kvm/arm.c +0 -0

[...]

arch/arm64/kvm/arm.c static bool kvm_arm_vcpu_suspended(struct kvm_vcpu *vcpu) · line 911

Quoted 7 lines
arch/arm64/kvm/arm.c +2 -1
@@ -906,7 +907,8 @@ static bool kvm_arm_vcpu_suspended(struct kvm_vcpu *vcpu)
 /* Only the guest's CPU_ON may start a vCPU EL2 holds powered off. */
 bool kvm_pkvm_vcpu_is_powered_off(struct kvm_vcpu *vcpu)
 {
-        return READ_ONCE(vcpu->arch.mp_state.mp_state) == KVM_MP_STATE_UNINITIALIZED;
+        return vcpu_is_protected_pkvm(vcpu) &&
+                READ_ONCE(vcpu->arch.mp_state.mp_state) == KVM_MP_STATE_UNINITIALIZED;

Only a pVM's vCPU can be in KVM_MP_STATE_UNINITIALIZED, so is this needed?

Cheers,
/fuad

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

#12 lore raw

Re: [PATCH v3 02/11] KVM: arm64: CCA: Introduce Realms

Quoted 52 lines · Fuad Tabba

On Mon, 16 Nov 2026 16:43:48 +0000, Steven Price <[email protected]> wrote:
[...]

Quoted 1 line
arch/arm64/include/asm/kvm_host.h +0 -0

[...]

Quoted 32 lines
arch/arm64/include/asm/kvm_host.h +17 -5
@@ -1571,31 +1576,39 @@ struct kvm *kvm_arch_alloc_vm(void);
 
 #define __KVM_HAVE_ARCH_FLUSH_REMOTE_TLBS_RANGE
 
-#ifdef __KVM_NVHE_HYPERVISOR__
+#define kvm_vm_is_protected(kvm)        ((kvm)->arch.vm_flavor >= __VM_PROTECTED)
 
-#define kvm_vm_is_protected(kvm)                        \
-        (is_protected_kvm_enabled() && ((kvm)->arch.vm_flavor == VM_PROTECTED_PKVM))
+#ifdef __KVM_NVHE_HYPERVISOR__
 /*
  * Accessing vcpu->kvm from nVHE hyp stub is tricky, as we need to convert the
  * pointer to the hyp VA. With pKVM, the nVHE code runs with the hyp_vcpu,
- * which is populated correctly and is gated on is_protected_kvm_enabled().
+ * which is populated correctly. Always vcpu_is_protected_pkvm(), which is
+ * gated on is_protected_kvm_enabled().
  */
-#define vcpu_is_protected(vcpu)                                                \
+#define vcpu_is_protected(vcpu)                BUILD_BUG_ON(1)
+#else
+#define vcpu_is_protected(vcpu)                kvm_vm_is_protected((vcpu)->kvm)
+#endif
+
+#define kvm_vm_is_protected_pkvm(kvm)                                        \
+        (is_protected_kvm_enabled() && ((kvm)->arch.vm_flavor == VM_PROTECTED_PKVM))
+
+/*
+ * Rely on is_protected_kvm_enabled() check in kvm_vm_is_protected_pkvm() to
+ * make sure the vcpu->kvm is always valid VA in the context
+ */
+#define vcpu_is_protected_pkvm(vcpu)                                        \
         ({                                                                \

arch/arm64/include/asm/kvm_host.h struct kvm *kvm_arch_alloc_vm(void); · line 1593

Quoted 10 lines
arch/arm64/include/asm/kvm_host.h +1 -5
                 struct kvm *__kvm = READ_ONCE((vcpu)->kvm);                \
                                                                         \
-                (__kvm && kvm_vm_is_protected(__kvm));                        \
+                (__kvm && kvm_vm_is_protected_pkvm(__kvm));                \
         })
 
-#else
-
-#define kvm_vm_is_protected(kvm)        ((kvm)->arch.vm_flavor >= __VM_PROTECTED)
-#define vcpu_is_protected(vcpu)                kvm_vm_is_protected((vcpu)->kvm)

This brings back the _pkvm variants that Marc asked to drop in the plumbing series [1]. In the nVHE hyp code, kvm_vm_is_protected() and vcpu_is_protected() already mean pKVM-protected, so I think the hyp side can stay as it is.

Quoted 3 lines

At EL1, the pKVM-only checks that a Realm vCPU also reaches are in kvm_arm_vcpu_power_off() and kvm_psci_vcpu_on(). Could they check vm_flavor == VM_PROTECTED_PKVM directly?

Ah, yes that should work better. Thanks for the suggestion.

Quoted 4 lines

arch/arm64/include/asm/kvm_host.h struct kvm *kvm_arch_alloc_vm(void); · line 1608

Quoted 2 lines
arch/arm64/include/asm/kvm_host.h +2 -0
+#define kvm_vm_is_realm(kvm)                ((kvm)->arch.vm_flavor == VM_REALM)
+#define vcpu_is_rec(vcpu)                kvm_vm_is_realm((vcpu)->kvm)

vcpu_is_rec() isn't used anywhere in this series. Could it be dropped?

Yep, I can move it after this series.

Quoted 12 lines

[...]

Quoted 1 line
arch/arm64/kvm/arm.c +0 -0

[...]

arch/arm64/kvm/arm.c static bool kvm_arm_vcpu_suspended(struct kvm_vcpu *vcpu) · line 911

Quoted 7 lines
arch/arm64/kvm/arm.c +2 -1
@@ -906,7 +907,8 @@ static bool kvm_arm_vcpu_suspended(struct kvm_vcpu *vcpu)
 /* Only the guest's CPU_ON may start a vCPU EL2 holds powered off. */
 bool kvm_pkvm_vcpu_is_powered_off(struct kvm_vcpu *vcpu)
 {
-        return READ_ONCE(vcpu->arch.mp_state.mp_state) == KVM_MP_STATE_UNINITIALIZED;
+        return vcpu_is_protected_pkvm(vcpu) &&
+                READ_ONCE(vcpu->arch.mp_state.mp_state) == KVM_MP_STATE_UNINITIALIZED;

Only a pVM's vCPU can be in KVM_MP_STATE_UNINITIALIZED, so is this needed?

I guess not. I'll drop this change.

Thanks,
Steve

Quoted 4 lines

Cheers,
/fuad

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

#13 lore raw

PATCH v3 3/11 KVM: arm64: Handle attributes in stage2_unmap_range

In reply to Steven Price

From: Suzuki K Poulose <[email protected]>

With the introduction of private memory support, we need to handle invalidation requests with filters for a given type. Add support for passing the attributes down.

---
 arch/arm64/include/asm/kvm_host.h |  4 +++-
 arch/arm64/kvm/mmu.c              | 31 +++++++++++++++++++++++--------
 2 files changed, 26 insertions(+), 9 deletions(-)
arch/arm64/include/asm/kvm_host.h +3 -1
index 0b6095886d54..aaac97a7461c 100644
@@ -233,6 +233,7 @@ struct kvm_vcpu_ops {
 };
 
 struct kvm_gfn_range;
+enum kvm_gfn_range_filter;
 struct kvm_s2_fault_desc;
 
 struct kvm_vm_s2_ops {
@@ -243,7 +244,8 @@ struct kvm_vm_s2_ops {
 					  u64 nr_pages);
 	void (*vm_stage2_unmap_range)(struct kvm_s2_mmu *mmu,
 				      phys_addr_t start, u64 size,
-				      bool may_block);
+				      bool may_block,
+				      enum kvm_gfn_range_filter filter);
 	int (*vm_mem_abort)(const struct kvm_s2_fault_desc *s2fd);
 };
 
arch/arm64/kvm/mmu.c +23 -8
index 09c831a49ed3..8a9a80c7cfab 100644
@@ -324,7 +324,8 @@ static int kvm_pgtable_stage2_unmap_tracked(struct kvm_pgtable *pgt,
 
 static void kvm_vm_stage2_unmap_range(struct kvm_s2_mmu *mmu,
 				      phys_addr_t start,
-				      u64 size, bool may_block)
+				      u64 size, bool may_block,
+				      enum kvm_gfn_range_filter filter)
 {
 	struct kvm *kvm = kvm_s2_mmu_to_kvm(mmu);
 	int (*fn)(struct kvm_pgtable *, u64, u64);
@@ -339,14 +340,16 @@ static void kvm_vm_stage2_unmap_range(struct kvm_s2_mmu *mmu,
 
 static void pkvm_stage2_unmap_range(struct kvm_s2_mmu *mmu,
 				    phys_addr_t start,
-				    u64 size, bool may_block)
+				    u64 size, bool may_block,
+				    enum kvm_gfn_range_filter filter)
 {
 	WARN_ON(stage2_apply_range(mmu, start, start + size,
 				   pkvm_pgtable_stage2_unmap, may_block));
 }
 
 static void no_stage2_unmap_range(struct kvm_s2_mmu *mmu,
-				  phys_addr_t start, u64 size, bool may_block)
+				  phys_addr_t start, u64 size, bool may_block,
+				  enum kvm_gfn_range_filter filter)
 {
 }
 
@@ -388,25 +391,36 @@ static int kvm_pgtable_stage2_unmap_tracked(struct kvm_pgtable *pgt, u64 addr, u
 }
 
 /**
- * kvm_stage2_unmap_range -- Clear stage2 page table entries to unmap a range
+ * kvm_stage2_unmap_range_filter -- Clear stage2 page table entries to unmap a
+ * range
  * @mmu:   The KVM stage-2 MMU pointer
  * @start: The intermediate physical base address of the range to unmap
  * @size:  The size of the area to unmap
  * @may_block: Whether or not we are permitted to block
+ * @filter: Filter for the ipa range (private and/or shared)
  *
  * Clear a range of stage-2 mappings, lowering the various ref-counts.  Must
  * be called while holding mmu_lock otherwise another faulting VCPU may
  * come in and mess with things behind our backs.
  */
-void kvm_stage2_unmap_range(struct kvm_s2_mmu *mmu, phys_addr_t start,
-			    u64 size, bool may_block)
+static void kvm_stage2_unmap_range_filter(struct kvm_s2_mmu *mmu, phys_addr_t start,
+					  u64 size, bool may_block,
+					  enum kvm_gfn_range_filter filter)
 {
 	struct kvm *kvm = kvm_s2_mmu_to_kvm(mmu);
 
 	lockdep_assert_held_write(&kvm->mmu_lock);
 	WARN_ON(size & ~PAGE_MASK);
 
-	kvm->arch.vm_s2_ops->vm_stage2_unmap_range(mmu, start, size, may_block);
+	kvm->arch.vm_s2_ops->vm_stage2_unmap_range(mmu, start, size,
+						   may_block, filter);
+}
+
+void kvm_stage2_unmap_range(struct kvm_s2_mmu *mmu, phys_addr_t start,
+			    u64 size, bool may_block)
+{
+	kvm_stage2_unmap_range_filter(mmu, start, size, may_block,
+				      KVM_FILTER_PRIVATE | KVM_FILTER_SHARED);
 }
 
 void kvm_stage2_flush_range(struct kvm_s2_mmu *mmu, phys_addr_t addr, phys_addr_t end)
@@ -2597,7 +2611,8 @@ bool kvm_unmap_gfn_range(struct kvm *kvm, struct kvm_gfn_range *range)
 	if (!kvm->arch.mmu.pgt)
 		return false;
 
-	kvm_stage2_unmap_range(&kvm->arch.mmu, gpa, size, may_block);
+	kvm_stage2_unmap_range_filter(&kvm->arch.mmu, gpa, size, may_block,
+				      range->attr_filter);
 	kvm_nested_unmap_cipa_range(kvm, gpa, size, may_block);
 
 	return false;
Signature · 1 line
2.43.0
Reviewed-by on v3 3/11
#14 lore raw

Re: [PATCH v3 03/11] KVM: arm64: Handle attributes in stage2_unmap_range

On Mon, 16 Nov 2026 16:43:49 +0000, Steven Price <[email protected]> wrote:
[...]

Quoted 3 lines

With the introduction of private memory support, we need to handle invalidation requests with filters for a given type. Add support for passing the attributes down.

nit: what this passes down is a filter rather than attributes, in both the subject and the message.

Cheers,
/fuad

#15 lore raw

Re: [PATCH v3 03/11] KVM: arm64: Handle attributes in stage2_unmap_range

Quoted 8 lines · Fuad Tabba

On Mon, 16 Nov 2026 16:43:49 +0000, Steven Price <[email protected]> wrote:
[...]

Quoted 3 lines

With the introduction of private memory support, we need to handle invalidation requests with filters for a given type. Add support for passing the attributes down.

nit: what this passes down is a filter rather than attributes, in both the subject and the message.

True - I'll update the commit message.

Quoted 2 lines

Thanks!
Steve

Quoted 2 lines

Cheers,
/fuad

#16 lore raw

PATCH v3 4/11 KVM: arm64: CCA: Add basic infrastructure for creating a realm

In reply to Steven Price

Introduce the skeleton functions for creating and destroying a realm. The requested IPA size is checked against both the KVM and RMM limits.

The actual work of constructing the realm will be added in future patches.

---
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 | 21 +++++++++++
 arch/arm64/kvm/Kconfig           |  1 +
 arch/arm64/kvm/Makefile          |  2 +-
 arch/arm64/kvm/arm.c             |  9 +++++
 arch/arm64/kvm/mmu.c             | 18 ++++++++--
 arch/arm64/kvm/rmi.c             | 62 ++++++++++++++++++++++++++++++++
 6 files changed, 110 insertions(+), 3 deletions(-)
 create mode 100644 arch/arm64/kvm/rmi.c
arch/arm64/include/asm/kvm_rmi.h +21 -0
index 67f16793b4d0..6994756a14e6 100644
@@ -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,28 @@ 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
  */
 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;
 };
 
+u32 kvm_rmm_ipa_limit(void);
+
+int kvm_init_realm(struct kvm *kvm);
+void kvm_destroy_realm(struct kvm *kvm);
+
 #endif /* __ASM_KVM_RMI_H */
arch/arm64/kvm/Kconfig +1 -0
index cecea0820e52..d067063a3686 100644
@@ -34,6 +34,7 @@ menuconfig KVM
 	select GUEST_PERF_EVENTS if PERF_EVENTS
 	select KVM_GUEST_MEMFD
 	select KVM_GENERIC_PRE_FAULT_MEMORY
+	select ARM_RMM_RMI
 	help
 	  Support hosting virtualized guest machines.
 
arch/arm64/kvm/Makefile +1 -1
index 7aaeeb84e788..66a719ab5b6b 100644
@@ -16,7 +16,7 @@ CFLAGS_handle_exit.o += -Wno-override-init
 kvm-y += arm.o mmu.o mmio.o psci.o hypercalls.o pvtime.o \
 	 inject_fault.o va_layout.o handle_exit.o config.o \
 	 guest.o debug.o reset.o sys_regs.o stacktrace.o \
-	 vgic-sys-reg-v3.o fpsimd.o pkvm.o \
+	 vgic-sys-reg-v3.o fpsimd.o pkvm.o rmi.o \
 	 arch_timer.o trng.o vmid.o emulate-nested.o nested.o at.o \
 	 vgic/vgic.o vgic/vgic-init.o \
 	 vgic/vgic-irqfd.o vgic/vgic-v2.o \
arch/arm64/kvm/arm.c +9 -0
index e06cc390a3d6..821e27d07f6a 100644
@@ -300,6 +300,13 @@ int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)
 
 	bitmap_zero(kvm->arch.vcpu_features, KVM_VCPU_MAX_FEATURES);
 
+	/* Initialise the realm bits after the generic bits are enabled */
+	if (kvm_vm_is_realm(kvm)) {
+		ret = kvm_init_realm(kvm);
+		if (ret)
+			goto err_uninit_mmu;
+	}
+
 	return 0;
 
 err_uninit_mmu:
@@ -364,6 +371,8 @@ void kvm_arch_destroy_vm(struct kvm *kvm)
 
 	kvm_destroy_nested(kvm);
 	kvm_arm_teardown_hypercalls(kvm);
+	if (kvm_vm_is_realm(kvm))
+		kvm_destroy_realm(kvm);
 }
 
 static bool kvm_has_full_ptr_auth(void)
arch/arm64/kvm/mmu.c +16 -2
index 8a9a80c7cfab..ac72db3d4505 100644
@@ -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;
@@ -1034,9 +1038,18 @@ static void stage2_destroy_range(struct kvm_pgtable *pgt, phys_addr_t addr,
 
 static void kvm_stage2_destroy(struct kvm_pgtable *pgt)
 {
+	struct kvm *kvm = kvm_s2_mmu_to_kvm(pgt->mmu);
 	unsigned int ia_bits = VTCR_EL2_IPA(pgt->mmu->vtcr);
 
-	stage2_destroy_range(pgt, 0, BIT(ia_bits));
+	/*
+	 * 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.
+	 */
+	if (!kvm_vm_is_realm(kvm))
+		stage2_destroy_range(pgt, 0, BIT(ia_bits));
+
 	KVM_PGT_FN(kvm_pgtable_stage2_destroy_pgd)(pgt);
 }
 
@@ -1084,6 +1097,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;
@@ -1092,7 +1107,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;
arch/arm64/kvm/rmi.c new file +62 -0

62 lines not on this page · raw message

Signature · 1 line
2.43.0
#17 lore raw

PATCH v3 5/11 KVM: arm64: CCA: Tear down RTTs

In reply to Steven Price

The RMM owns the stage 2 page tables for a realm, and KVM must request that the RMM creates/destroys entries as necessary. The physical pages to store the page tables are delegated to the realm as required, and can be undelegated when no longer used.

Creating new RTTs is the easy part, tearing down 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).

Tear down a created Realm whether it is still new or has been activated. Unmap the Realm IPA space once during stage-2 teardown. Defer destruction of non-root RTTs until final Realm destruction, after REALM_TERMINATE and before destroying the RD; root RTTs remain until the RD has gone away.

---
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 |   9 ++
 arch/arm64/kvm/mmu.c             |  18 ++-
 arch/arm64/kvm/rmi.c             | 194 +++++++++++++++++++++++++++++++
 3 files changed, 220 insertions(+), 1 deletion(-)
arch/arm64/include/asm/kvm_rmi.h +9 -0
index 6994756a14e6..dd16c004b831 100644
@@ -57,6 +57,7 @@ enum realm_state {
  * @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
+ * @stage2_unmapped: The Realm stage-2 mappings have been removed
  */
 struct realm {
 	phys_addr_t rd_phys;
@@ -70,11 +71,19 @@ struct realm {
 	struct rmi_sro_state *sro;
 	enum realm_state state;
 	unsigned int ia_bits;
+	bool stage2_unmapped;
 };
 
 u32 kvm_rmm_ipa_limit(void);
 
 int kvm_init_realm(struct kvm *kvm);
 void kvm_destroy_realm(struct kvm *kvm);
+int kvm_realm_teardown_stage2(struct kvm *kvm);
+
+static inline bool kvm_realm_is_private_address(struct realm *realm,
+						unsigned long addr)
+{
+	return !(addr & BIT(realm->ia_bits - 1));
+}
 
 #endif /* __ASM_KVM_RMI_H */
arch/arm64/kvm/mmu.c +17 -1
index ac72db3d4505..9baf2352d2a8 100644
@@ -1145,9 +1145,25 @@ 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);
+
+	mutex_lock(&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);
+		WARN_ON(kvm_realm_teardown_stage2(kvm));
+	}
+	mutex_unlock(&kvm->arch.config_lock);
+}
+
 void kvm_uninit_stage2_mmu(struct kvm *kvm)
 {
-	kvm_free_stage2_pgd(&kvm->arch.mmu);
+	if (kvm_realm_is_created(kvm))
+		kvm_realm_uninit_stage2(&kvm->arch.mmu);
+	else
+		kvm_free_stage2_pgd(&kvm->arch.mmu);
 	kvm_mmu_free_memory_cache(&kvm->arch.mmu.split_page_cache);
 }
 
arch/arm64/kvm/rmi.c +194 -0

222 lines not on this page · raw message

Signature · 1 line
2.43.0
#18 lore raw

Re: [PATCH v3 05/11] KVM: arm64: CCA: Tear down RTTs

Thank you for your contribution! Sashiko AI review found 4 potential issues to consider: - [High] KVM: arm64: unclamped stage-2 levels in get_start_level() on 64K pages - [Medium] KVM: arm64: CCA…

Thank you for your contribution! Sashiko AI review found 4 potential issues to consider:
- [High] KVM: arm64: unclamped stage-2 levels in get_start_level() on 64K pages
- [Medium] KVM: arm64: CCA: unlocked kvm_realm_is_created() check in kvm_uninit_stage2_mmu()
- [Low] KVM: arm64: mix of guard() and goto cleanup in kvm_destroy_realm()
- [Low] KVM: arm64: possible skipped parent retry in realm_tear_down_rtt_level()
--

commit 80a462fb8b46e04c72060d069b1296caf72d83ff
Author: Steven Price <[email protected]>

KVM: arm64: CCA: Tear down RTTs

This commit adds stage-2 unmapping and Realm Translation Table (RTT) teardown for Arm CCA Realms during stage-2 MMU uninitialization and final Realm destruction.

Quoted 20 lines
arch/arm64/kvm/mmu.c +12 -0
index ac72db3d4505f..9baf2352d2a8f 100644
@@ -1145,9 +1145,25 @@ 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);
+
+	mutex_lock(&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);
+		WARN_ON(kvm_realm_teardown_stage2(kvm));
+	}
+	mutex_unlock(&kvm->arch.config_lock);
+}

arch/arm64/kvm/mmu.c int kvm_init_stage2_mmu(struct kvm *kvm, struct kvm_s2_mmu *mmu, unsigned long t · line 1168

Quoted 10 lines
arch/arm64/kvm/mmu.c +5 -1
+
 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);
 }

[Severity: Medium]
Can kvm_uninit_stage2_mmu() race with Realm creation by checking kvm_realm_is_created() outside kvm->arch.config_lock?

If kvm_mmu_notifier_release() -> kvm_arch_flush_shadow_all() runs before or concurrently with Realm creation, kvm_uninit_stage2_mmu() takes the else branch without holding config_lock, freeing mmu->pgt and clearing mmu->pgd_phys while leaving realm.state at REALM_STATE_NONE. Checking kvm_vm_is_realm(kvm) here and handling all realm.state transitions inside kvm_realm_uninit_stage2() under config_lock (including moving REALM_STATE_NONE to REALM_STATE_DEAD and freeing the PGD) would avoid the race.

Quoted 6 lines
arch/arm64/kvm/rmi.c +0 -0
index 57f59b618a96e..74b89da4e8015 100644
@@ -16,22 +16,213 @@ u32 kvm_rmm_ipa_limit(void)
 	return u64_get_bits(rmi_feat_reg(0), RMI_FEATURE_REGISTER_0_S2SZ);

arch/arm64/kvm/rmi.c u32 kvm_rmm_ipa_limit(void) · line 26

Quoted 10 lines
arch/arm64/kvm/rmi.c +8 -0
 }
 
+static int get_start_level(struct realm *realm)
+{
+	/*
+	 * kvm_init_ipa_range() enforces a minimum of 32 IPA bits
+	 * (ARM64_MIN_PARANGE_BITS).
+	 */
+	return 4 - stage2_pgtable_levels(realm->ia_bits);
+}

[Severity: High]
Does get_start_level() return 3 instead of 2 for a 32-bit or 33-bit IPA Realm on CONFIG_ARM64_64K_PAGES?

stage2_pgtable_levels(ipa) evaluates ARM64_HW_PGTABLE_LEVELS((ipa) - 4), which is 1 for 32 or 33 IPA bits with 64K pages, whereas kvm_get_vtcr() clamps the stage-2 levels to at least 2:

arch/arm64/kvm/hyp/pgtable.c:kvm_get_vtcr() {
    ...
	lvls = stage2_pgtable_levels(phys_shift);
	if (lvls < 2)
		lvls = 2;
    ...
}

When get_start_level() returns 3, realm_tear_down_rtt_range() calls realm_tear_down_rtt_level() with level = 4 (> KVM_PGTABLE_LAST_LEVEL), triggering WARN_ON() and returning -EINVAL, which causes kvm_destroy_realm() to abort and leak the Realm Descriptor, RTT granules and stage-2 PGD pages.

[ ... ]

Quoted 48 lines
arch/arm64/kvm/rmi.c +48 -0
+static int realm_tear_down_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_tear_down_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;
+			}

arch/arm64/kvm/rmi.c u32 kvm_rmm_ipa_limit(void) · line 116

Quoted 10 lines
arch/arm64/kvm/rmi.c +10 -0
+			/*
+			 * We tear down the RTT range for the full IPA
+			 * space, after everything is unmapped. Also we
+			 * descend down only if we cannot tear down 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;

[Severity: Low]
This question rests on an Arm CCA RMM specification detail that could not be confirmed from the kernel source: when SMC_RMI_RTT_DESTROY fails with RMI_ERROR_RTT because the RTT at @level still has live child table entries, can the RMM return out_top > addr (with addr < next_addr < addr + map_size) if the initial entries in that RTT are non-live, or report RMI_RESULT_DATA_LEVEL(ret) == level - 1?

If out_top advances to the first live entry inside the RTT at @level, the if (next_addr > addr) check treats the RTT as missing and advances addr to that unaligned next_addr; the next iteration then sees align_addr != addr, destroys the remaining child RTTs at level + 1, and continues to the next map_size boundary without retrying realm_rtt_destroy() on the parent RTT at @level, leaving it live and causing rmi_realm_destroy() to fail. If instead the walk terminates at level - 1 with next_addr == addr, WARN_ON(RMI_RESULT_DATA_LEVEL(ret) != level) fires and aborts teardown with -EBUSY.

[ ... ]

Quoted 38 lines
arch/arm64/kvm/rmi.c +18 -0
 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 RMM teardown with Realm creation and stage-2 teardown,
+	 * 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 the IPA space
+	 * 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.
+	 */
+	realm_unmap_stage2(kvm);
+
 	if (realm->rd_phys) {
 		phys_addr_t rd_phys = realm->rd_phys;
 
 		if (WARN_ON(rmi_realm_terminate(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(rd_phys, realm->sro)))
 			goto out_free_sro;
 		rmi_free_delegated_page(rd_phys);

arch/arm64/kvm/rmi.c void kvm_destroy_realm(struct kvm *kvm) · line 243

Quoted 9 lines
arch/arm64/kvm/rmi.c +3 -0
@@ -44,6 +235,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: Low]
This isn't a bug, but kvm_destroy_realm() mixes guard(mutex) with goto out_free_sro cleanup. Using __free(kfree) for realm->sro (or explicit mutex_lock() and mutex_unlock()) would avoid mixing scope-based cleanup and goto unwinding in the same function.

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

Re: [PATCH v3 05/11] KVM: arm64: CCA: Tear down RTTs

Quoted 56 lines · On 16/11/2026 17:08, [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 4 potential issues to consider:
- [High] KVM: arm64: unclamped stage-2 levels in get_start_level() on 64K pages
- [Medium] KVM: arm64: CCA: unlocked kvm_realm_is_created() check in kvm_uninit_stage2_mmu()
- [Low] KVM: arm64: mix of guard() and goto cleanup in kvm_destroy_realm()
- [Low] KVM: arm64: possible skipped parent retry in realm_tear_down_rtt_level()
--

commit 80a462fb8b46e04c72060d069b1296caf72d83ff
Author: Steven Price <[email protected]>

KVM: arm64: CCA: Tear down RTTs

This commit adds stage-2 unmapping and Realm Translation Table (RTT) teardown for Arm CCA Realms during stage-2 MMU uninitialization and final Realm destruction.

Quoted 20 lines
arch/arm64/kvm/mmu.c +12 -0
index ac72db3d4505f..9baf2352d2a8f 100644
@@ -1145,9 +1145,25 @@ 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);
+
+	mutex_lock(&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);
+		WARN_ON(kvm_realm_teardown_stage2(kvm));
+	}
+	mutex_unlock(&kvm->arch.config_lock);
+}

arch/arm64/kvm/mmu.c int kvm_init_stage2_mmu(struct kvm *kvm, struct kvm_s2_mmu *mmu, unsigned long t · line 1168

Quoted 10 lines
arch/arm64/kvm/mmu.c +5 -1
+
 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);
 }

[Severity: Medium]
Can kvm_uninit_stage2_mmu() race with Realm creation by checking kvm_realm_is_created() outside kvm->arch.config_lock?

Quoted 8 lines

If kvm_mmu_notifier_release() -> kvm_arch_flush_shadow_all() runs before or concurrently with Realm creation, kvm_uninit_stage2_mmu() takes the else branch without holding config_lock, freeing mmu->pgt and clearing mmu->pgd_phys while leaving realm.state at REALM_STATE_NONE. Checking kvm_vm_is_realm(kvm) here and handling all realm.state transitions inside kvm_realm_uninit_stage2() under config_lock (including moving REALM_STATE_NONE to REALM_STATE_DEAD and freeing the PGD) would avoid the race.

I don't believe this race can occur. The MMU notifier release runs as the owning mm exits, while that mm will be help during ioctls (including realm creation).

Quoted 33 lines
Quoted 6 lines
arch/arm64/kvm/rmi.c +0 -0
index 57f59b618a96e..74b89da4e8015 100644
@@ -16,22 +16,213 @@ u32 kvm_rmm_ipa_limit(void)
 	return u64_get_bits(rmi_feat_reg(0), RMI_FEATURE_REGISTER_0_S2SZ);

arch/arm64/kvm/rmi.c u32 kvm_rmm_ipa_limit(void) · line 26

Quoted 10 lines
arch/arm64/kvm/rmi.c +8 -0
 }
 
+static int get_start_level(struct realm *realm)
+{
+	/*
+	 * kvm_init_ipa_range() enforces a minimum of 32 IPA bits
+	 * (ARM64_MIN_PARANGE_BITS).
+	 */
+	return 4 - stage2_pgtable_levels(realm->ia_bits);
+}

[Severity: High]
Does get_start_level() return 3 instead of 2 for a 32-bit or 33-bit IPA Realm on CONFIG_ARM64_64K_PAGES?

stage2_pgtable_levels(ipa) evaluates ARM64_HW_PGTABLE_LEVELS((ipa) - 4), which is 1 for 32 or 33 IPA bits with 64K pages, whereas kvm_get_vtcr() clamps the stage-2 levels to at least 2:

arch/arm64/kvm/hyp/pgtable.c:kvm_get_vtcr() {
    ...
	lvls = stage2_pgtable_levels(phys_shift);
	if (lvls < 2)
		lvls = 2;
    ...
}
Quoted 7 lines

When get_start_level() returns 3, realm_tear_down_rtt_range() calls realm_tear_down_rtt_level() with level = 4 (> KVM_PGTABLE_LAST_LEVEL), triggering WARN_ON() and returning -EINVAL, which causes kvm_destroy_realm() to abort and leak the Realm Descriptor, RTT granules and stage-2 PGD pages.

[ ... ]

Hmm, I changed this based on Suzuki's argument[1] that ia_bits is at least 32 bits. But I think Sashiko might have a point.

@Suzuki: Are we missing something? It certainly seems safer to just match the logic in kvm_get_vtcr() and explicitly clamp. So unless you can tell me what I'm missing then I'll change this back to:

  return 4 - max(2, stage2_pgtable_levels(realm->ia_bits));

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

Quoted 59 lines
Quoted 48 lines
arch/arm64/kvm/rmi.c +48 -0
+static int realm_tear_down_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_tear_down_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;
+			}

arch/arm64/kvm/rmi.c u32 kvm_rmm_ipa_limit(void) · line 116

Quoted 10 lines
arch/arm64/kvm/rmi.c +10 -0
+			/*
+			 * We tear down the RTT range for the full IPA
+			 * space, after everything is unmapped. Also we
+			 * descend down only if we cannot tear down 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;
Quoted 7 lines

[Severity: Low]
This question rests on an Arm CCA RMM specification detail that could not be confirmed from the kernel source: when SMC_RMI_RTT_DESTROY fails with RMI_ERROR_RTT because the RTT at @level still has live child table entries, can the RMM return out_top > addr (with addr < next_addr < addr + map_size) if the initial entries in that RTT are non-live, or report RMI_RESULT_DATA_LEVEL(ret) == level - 1?

Nope. RMI_RTT_DESTROY will return with out_top == ipa if the target RTT is live. out_top > ipa describes a missing RTT.

Quoted 53 lines

If out_top advances to the first live entry inside the RTT at @level, the if (next_addr > addr) check treats the RTT as missing and advances addr to that unaligned next_addr; the next iteration then sees align_addr != addr, destroys the remaining child RTTs at level + 1, and continues to the next map_size boundary without retrying realm_rtt_destroy() on the parent RTT at @level, leaving it live and causing rmi_realm_destroy() to fail. If instead the walk terminates at level - 1 with next_addr == addr, WARN_ON(RMI_RESULT_DATA_LEVEL(ret) != level) fires and aborts teardown with -EBUSY.

[ ... ]

Quoted 41 lines
arch/arm64/kvm/rmi.c +18 -0
 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 RMM teardown with Realm creation and stage-2 teardown,
+	 * 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 the IPA space
+	 * 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.
+	 */
+	realm_unmap_stage2(kvm);
+
 	if (realm->rd_phys) {
 		phys_addr_t rd_phys = realm->rd_phys;
 
 		if (WARN_ON(rmi_realm_terminate(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(rd_phys, realm->sro)))
 			goto out_free_sro;
 		rmi_free_delegated_page(rd_phys);
@@ -44,6 +235,9 @@ void kvm_destroy_realm(struct kvm *kvm)
 
 	kvm_set_realm_state(kvm, REALM_STATE_DEAD);
 
Quoted 13 lines

arch/arm64/kvm/rmi.c void kvm_destroy_realm(struct kvm *kvm) · line 243

Quoted 6 lines
arch/arm64/kvm/rmi.c +3 -0
+	/* 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: Low]
This isn't a bug, but kvm_destroy_realm() mixes guard(mutex) with goto out_free_sro cleanup. Using __free(kfree) for realm->sro (or explicit mutex_lock() and mutex_unlock()) would avoid mixing scope-based cleanup and goto unwinding in the same function.

Yes I guess it's a little confusing mixing the two styles.

Thanks,
Steve

#20 lore raw

Re: [PATCH v3 05/11] KVM: arm64: CCA: Tear down RTTs

In reply to [email protected]
Quoted 98 lines · On 16/11/2026 17:08, [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 4 potential issues to consider:
- [High] KVM: arm64: unclamped stage-2 levels in get_start_level() on 64K pages
- [Medium] KVM: arm64: CCA: unlocked kvm_realm_is_created() check in kvm_uninit_stage2_mmu()
- [Low] KVM: arm64: mix of guard() and goto cleanup in kvm_destroy_realm()
- [Low] KVM: arm64: possible skipped parent retry in realm_tear_down_rtt_level()
--

commit 80a462fb8b46e04c72060d069b1296caf72d83ff
Author: Steven Price <[email protected]>

KVM: arm64: CCA: Tear down RTTs

This commit adds stage-2 unmapping and Realm Translation Table (RTT) teardown for Arm CCA Realms during stage-2 MMU uninitialization and final Realm destruction.

Quoted 20 lines
arch/arm64/kvm/mmu.c +12 -0
index ac72db3d4505f..9baf2352d2a8f 100644
@@ -1145,9 +1145,25 @@ 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);
+
+	mutex_lock(&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);
+		WARN_ON(kvm_realm_teardown_stage2(kvm));
+	}
+	mutex_unlock(&kvm->arch.config_lock);
+}

arch/arm64/kvm/mmu.c int kvm_init_stage2_mmu(struct kvm *kvm, struct kvm_s2_mmu *mmu, unsigned long t · line 1168

Quoted 10 lines
arch/arm64/kvm/mmu.c +5 -1
+
  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);
  }

[Severity: Medium]
Can kvm_uninit_stage2_mmu() race with Realm creation by checking kvm_realm_is_created() outside kvm->arch.config_lock?

If kvm_mmu_notifier_release() -> kvm_arch_flush_shadow_all() runs before or concurrently with Realm creation, kvm_uninit_stage2_mmu() takes the else branch without holding config_lock, freeing mmu->pgt and clearing mmu->pgd_phys while leaving realm.state at REALM_STATE_NONE. Checking kvm_vm_is_realm(kvm) here and handling all realm.state transitions inside kvm_realm_uninit_stage2() under config_lock (including moving REALM_STATE_NONE to REALM_STATE_DEAD and freeing the PGD) would avoid the race.

Quoted 6 lines
arch/arm64/kvm/rmi.c +0 -0
index 57f59b618a96e..74b89da4e8015 100644
@@ -16,22 +16,213 @@ u32 kvm_rmm_ipa_limit(void)
  	return u64_get_bits(rmi_feat_reg(0), RMI_FEATURE_REGISTER_0_S2SZ);

arch/arm64/kvm/rmi.c u32 kvm_rmm_ipa_limit(void) · line 26

Quoted 10 lines
arch/arm64/kvm/rmi.c +8 -0
  }
 
+static int get_start_level(struct realm *realm)
+{
+	/*
+	 * kvm_init_ipa_range() enforces a minimum of 32 IPA bits
+	 * (ARM64_MIN_PARANGE_BITS).
+	 */
+	return 4 - stage2_pgtable_levels(realm->ia_bits);
+}

[Severity: High]
Does get_start_level() return 3 instead of 2 for a 32-bit or 33-bit IPA Realm on CONFIG_ARM64_64K_PAGES?

stage2_pgtable_levels(ipa) evaluates ARM64_HW_PGTABLE_LEVELS((ipa) - 4), which is 1 for 32 or 33 IPA bits with 64K pages, whereas kvm_get_vtcr() clamps the stage-2 levels to at least 2:

arch/arm64/kvm/hyp/pgtable.c:kvm_get_vtcr() {
     ...
	lvls = stage2_pgtable_levels(phys_shift);
	if (lvls < 2)
		lvls = 2;
     ...
}
Quoted 5 lines

When get_start_level() returns 3, realm_tear_down_rtt_range() calls realm_tear_down_rtt_level() with level = 4 (> KVM_PGTABLE_LAST_LEVEL), triggering WARN_ON() and returning -EINVAL, which causes kvm_destroy_realm() to abort and leak the Realm Descriptor, RTT granules and stage-2 PGD pages.

My bad. There is already a helper, VTCR_EL2_LVLS() to convert vtcr back to levels. We could as well save it in realm, as we seem to need it more than once.

Quoted 58 lines

[ ... ]

Quoted 55 lines
arch/arm64/kvm/rmi.c +55 -0
+static int realm_tear_down_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_tear_down_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 tear down the RTT range for the full IPA
+			 * space, after everything is unmapped. Also we
+			 * descend down only if we cannot tear down 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.
Quoted 8 lines

arch/arm64/kvm/rmi.c u32 kvm_rmm_ipa_limit(void) · line 116

Quoted 3 lines
arch/arm64/kvm/rmi.c +3 -0
+			 */
+			if (WARN_ON(RMI_RESULT_DATA_LEVEL(ret) != level))
+				return -EBUSY;

[Severity: Low]
This question rests on an Arm CCA RMM specification detail that could not be confirmed from the kernel source: when SMC_RMI_RTT_DESTROY fails with RMI_ERROR_RTT because the RTT at @level still has live child table

If the RTT entry at @level is live, RMM returns (RMI_ERROR_RTT, level) with out_top == addr.

Quoted 3 lines

entries, can the RMM return out_top > addr (with addr < next_addr < addr + map_size) if the initial entries in that RTT are non-live, or report RMI_RESULT_DATA_LEVEL(ret) == level - 1?

Suzuki

#21 lore raw

Re: [PATCH v3 05/11] KVM: arm64: CCA: Tear down RTTs

Quoted 98 lines · Suzuki K Poulose
Quoted 97 lines · On 16/11/2026 17:08, [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 4 potential issues to consider:
- [High] KVM: arm64: unclamped stage-2 levels in get_start_level() on 64K pages
- [Medium] KVM: arm64: CCA: unlocked kvm_realm_is_created() check in kvm_uninit_stage2_mmu()
- [Low] KVM: arm64: mix of guard() and goto cleanup in kvm_destroy_realm()
- [Low] KVM: arm64: possible skipped parent retry in realm_tear_down_rtt_level()

Signature · 8 lines
commit 80a462fb8b46e04c72060d069b1296caf72d83ff
Author: Steven Price <[email protected]>

KVM: arm64: CCA: Tear down RTTs

This commit adds stage-2 unmapping and Realm Translation Table (RTT)
teardown for Arm CCA Realms during stage-2 MMU uninitialization and final
Realm destruction.
Quoted 22 lines
arch/arm64/kvm/mmu.c +0 -0
index ac72db3d4505f..9baf2352d2a8f 100644
@@ -1145,9 +1145,25 @@ int kvm_init_stage2_mmu(struct kvm *kvm, 

struct kvm_s2_mmu *mmu, unsigned long t
      return err;
  }

arch/arm64/kvm/mmu.c +13 -0
+static void kvm_realm_uninit_stage2(struct kvm_s2_mmu *mmu)
+{
+    struct kvm *kvm = kvm_s2_mmu_to_kvm(mmu);
+
+    mutex_lock(&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);
+        WARN_ON(kvm_realm_teardown_stage2(kvm));
+    }
+    mutex_unlock(&kvm->arch.config_lock);
+}
+

  void kvm_uninit_stage2_mmu(struct kvm *kvm)

Quoted 8 lines

  {
-    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); }

[Severity: Medium]
Can kvm_uninit_stage2_mmu() race with Realm creation by checking kvm_realm_is_created() outside kvm->arch.config_lock?

If kvm_mmu_notifier_release() -> kvm_arch_flush_shadow_all() runs before or concurrently with Realm creation, kvm_uninit_stage2_mmu() takes the else branch without holding config_lock, freeing mmu->pgt and clearing mmu->pgd_phys while leaving realm.state at REALM_STATE_NONE.  Checking kvm_vm_is_realm(kvm) here and handling all realm.state transitions inside kvm_realm_uninit_stage2() under config_lock (including moving REALM_STATE_NONE to REALM_STATE_DEAD and freeing the PGD) would avoid the race.

Quoted 5 lines
arch/arm64/kvm/rmi.c +0 -0
index 57f59b618a96e..74b89da4e8015 100644

arch/arm64/kvm/rmi.c u32 kvm_rmm_ipa_limit(void) · line 25

Quoted 10 lines
arch/arm64/kvm/rmi.c +8 -0
@@ -16,22 +16,213 @@ u32 kvm_rmm_ipa_limit(void)
       return u64_get_bits(rmi_feat_reg(0), RMI_FEATURE_REGISTER_0_S2SZ);
   }
+static int get_start_level(struct realm *realm)
+{
+    /*
+     * kvm_init_ipa_range() enforces a minimum of 32 IPA bits
+     * (ARM64_MIN_PARANGE_BITS).
+     */
+    return 4 - stage2_pgtable_levels(realm->ia_bits);
+}

[Severity: High]
Does get_start_level() return 3 instead of 2 for a 32-bit or 33-bit IPA Realm on CONFIG_ARM64_64K_PAGES?

stage2_pgtable_levels(ipa) evaluates ARM64_HW_PGTABLE_LEVELS((ipa) - 4), which is 1 for 32 or 33 IPA bits with 64K pages, whereas kvm_get_vtcr() clamps the stage-2 levels to at least 2:

arch/arm64/kvm/hyp/pgtable.c:kvm_get_vtcr() {
     ...
    lvls = stage2_pgtable_levels(phys_shift);
    if (lvls < 2)
        lvls = 2;
     ...
}

Quoted 8 lines
Quoted 5 lines

When get_start_level() returns 3, realm_tear_down_rtt_range() calls realm_tear_down_rtt_level() with level = 4 (> KVM_PGTABLE_LAST_LEVEL), triggering WARN_ON() and returning -EINVAL, which causes kvm_destroy_realm() to abort and leak the Realm Descriptor, RTT granules and stage-2 PGD pages.

My bad. There is already a helper, VTCR_EL2_LVLS() to convert vtcr back to levels. We could as well save it in realm, as we seem to need it more than once.

Or even we should reuse the code by wrapping the above logic in kvm_get_vtcr() to something like:

int kvm_stage2_pgtable_levels(phys_shift)

Suzuki

Quoted 78 lines
Quoted 59 lines

[ ... ]

Quoted 55 lines
arch/arm64/kvm/rmi.c +55 -0
+static int realm_tear_down_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_tear_down_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 tear down the RTT range for the full IPA
+             * space, after everything is unmapped. Also we
+             * descend down only if we cannot tear down 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.
Quoted 8 lines
Quoted 3 lines

+             */
+            if (WARN_ON(RMI_RESULT_DATA_LEVEL(ret) != level)) +                return -EBUSY;

[Severity: Low]
This question rests on an Arm CCA RMM specification detail that could not be confirmed from the kernel source: when SMC_RMI_RTT_DESTROY fails with RMI_ERROR_RTT because the RTT at @level still has live child table

If the RTT entry at @level is live, RMM returns (RMI_ERROR_RTT, level) with out_top == addr.

Quoted 3 lines

entries, can the RMM return out_top > addr (with addr < next_addr < addr + map_size) if the initial entries in that RTT are non-live, or report RMI_RESULT_DATA_LEVEL(ret) == level - 1?

Suzuki

#22 lore raw

Re: [PATCH v3 05/11] KVM: arm64: CCA: Tear down RTTs

In reply to Suzuki K Poulose
Quoted 104 lines · Suzuki K Poulose
Quoted 102 lines · On 16/11/2026 17:08, [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 4 potential issues to consider:
- [High] KVM: arm64: unclamped stage-2 levels in get_start_level() on 64K pages
- [Medium] KVM: arm64: CCA: unlocked kvm_realm_is_created() check in kvm_uninit_stage2_mmu()
- [Low] KVM: arm64: mix of guard() and goto cleanup in kvm_destroy_realm()
- [Low] KVM: arm64: possible skipped parent retry in realm_tear_down_rtt_level()

Signature · 8 lines
commit 80a462fb8b46e04c72060d069b1296caf72d83ff
Author: Steven Price <[email protected]>

KVM: arm64: CCA: Tear down RTTs

This commit adds stage-2 unmapping and Realm Translation Table (RTT)
teardown for Arm CCA Realms during stage-2 MMU uninitialization and final
Realm destruction.
Quoted 22 lines
arch/arm64/kvm/mmu.c +0 -0
index ac72db3d4505f..9baf2352d2a8f 100644
@@ -1145,9 +1145,25 @@ 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)

arch/arm64/kvm/mmu.c +12 -0
+{
+    struct kvm *kvm = kvm_s2_mmu_to_kvm(mmu);
+
+    mutex_lock(&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);
+        WARN_ON(kvm_realm_teardown_stage2(kvm));
+    }
+    mutex_unlock(&kvm->arch.config_lock);
+}
+

  void kvm_uninit_stage2_mmu(struct kvm *kvm)

Quoted 8 lines

  {
-    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); }

[Severity: Medium]
Can kvm_uninit_stage2_mmu() race with Realm creation by checking kvm_realm_is_created() outside kvm->arch.config_lock?

If kvm_mmu_notifier_release() -> kvm_arch_flush_shadow_all() runs before or concurrently with Realm creation, kvm_uninit_stage2_mmu() takes the else branch without holding config_lock, freeing mmu->pgt and clearing mmu->pgd_phys while leaving realm.state at REALM_STATE_NONE.  Checking kvm_vm_is_realm(kvm) here and handling all realm.state transitions inside kvm_realm_uninit_stage2() under config_lock (including moving REALM_STATE_NONE to REALM_STATE_DEAD and freeing the PGD) would avoid the race.

Quoted 5 lines
arch/arm64/kvm/rmi.c +0 -0
index 57f59b618a96e..74b89da4e8015 100644

arch/arm64/kvm/rmi.c u32 kvm_rmm_ipa_limit(void) · line 25

Quoted 10 lines
arch/arm64/kvm/rmi.c +7 -0
@@ -16,22 +16,213 @@ u32 kvm_rmm_ipa_limit(void)
       return u64_get_bits(rmi_feat_reg(0), RMI_FEATURE_REGISTER_0_S2SZ);
   }
   +static int get_start_level(struct realm *realm)
+{
+    /*
+     * kvm_init_ipa_range() enforces a minimum of 32 IPA bits
+     * (ARM64_MIN_PARANGE_BITS).
+     */
+    return 4 - stage2_pgtable_levels(realm->ia_bits);
+}

[Severity: High]
Does get_start_level() return 3 instead of 2 for a 32-bit or 33-bit IPA Realm on CONFIG_ARM64_64K_PAGES?

stage2_pgtable_levels(ipa) evaluates ARM64_HW_PGTABLE_LEVELS((ipa) - 4), which is 1 for 32 or 33 IPA bits with 64K pages, whereas kvm_get_vtcr() clamps the stage-2 levels to at least 2:

arch/arm64/kvm/hyp/pgtable.c:kvm_get_vtcr() {
     ...
    lvls = stage2_pgtable_levels(phys_shift);
    if (lvls < 2)
        lvls = 2;
     ...
}

When get_start_level() returns 3, realm_tear_down_rtt_range() calls realm_tear_down_rtt_level() with level = 4 (> KVM_PGTABLE_LAST_LEVEL), triggering WARN_ON() and returning -EINVAL, which causes kvm_destroy_realm() to abort and leak the Realm Descriptor, RTT granules and stage-2 PGD pages.

Quoted 3 lines

My bad. There is already a helper, VTCR_EL2_LVLS() to convert vtcr back to levels. We could as well save it in realm, as we seem to need it more than once.

Actually that's probably the best option - use VTCR_EL2_LVLS() (to side-step all the issues that Sashiko found), but stash the result in struct realm (to avoid having to pass the kvm pointer around to get to the VTCR). I'll make that change for v4.

Thanks,
Steve

Quoted 79 lines
Quoted 58 lines

[ ... ]

Quoted 55 lines
arch/arm64/kvm/rmi.c +55 -0
+static int realm_tear_down_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_tear_down_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 tear down the RTT range for the full IPA
+             * space, after everything is unmapped. Also we
+             * descend down only if we cannot tear down 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.
Quoted 8 lines
Quoted 3 lines

+             */
+            if (WARN_ON(RMI_RESULT_DATA_LEVEL(ret) != level)) +                return -EBUSY;

[Severity: Low]
This question rests on an Arm CCA RMM specification detail that could not be confirmed from the kernel source: when SMC_RMI_RTT_DESTROY fails with RMI_ERROR_RTT because the RTT at @level still has live child table

If the RTT entry at @level is live, RMM returns (RMI_ERROR_RTT, level) with out_top == addr.

Quoted 4 lines

entries, can the RMM return out_top > addr (with addr < next_addr < addr + map_size) if the initial entries in that RTT are non-live, or report
RMI_RESULT_DATA_LEVEL(ret) == level - 1?

Suzuki

#23 lore raw

Re: [PATCH v3 05/11] KVM: arm64: CCA: Tear down RTTs

In reply to Steven Price

Hi Steven,

Quoted 53 lines · Steven Price

The RMM owns the stage 2 page tables for a realm, and KVM must request that the RMM creates/destroys entries as necessary. The physical pages to store the page tables are delegated to the realm as required, and can be undelegated when no longer used.

Creating new RTTs is the easy part, tearing down 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).

Tear down a created Realm whether it is still new or has been activated. Unmap the Realm IPA space once during stage-2 teardown. Defer destruction of non-root RTTs until final Realm destruction, after REALM_TERMINATE and before destroying the RD; root RTTs remain until the RD has gone away.

---
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 |   9 ++
 arch/arm64/kvm/mmu.c             |  18 ++-
 arch/arm64/kvm/rmi.c             | 194 +++++++++++++++++++++++++++++++
 3 files changed, 220 insertions(+), 1 deletion(-)
arch/arm64/include/asm/kvm_rmi.h +2 -0
index 6994756a14e6..dd16c004b831 100644
@@ -57,6 +57,7 @@ enum realm_state {
  * @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
+ * @stage2_unmapped: The Realm stage-2 mappings have been removed
  */
 struct realm {
 	phys_addr_t rd_phys;
@@ -70,11 +71,19 @@ struct realm {
 	struct rmi_sro_state *sro;
 	enum realm_state state;
 	unsigned int ia_bits;
+	bool stage2_unmapped;
 };
 
 u32 kvm_rmm_ipa_limit(void);

arch/arm64/include/asm/kvm_rmi.h struct realm { · line 87

Quoted 10 lines
arch/arm64/include/asm/kvm_rmi.h +7 -0
 
 int kvm_init_realm(struct kvm *kvm);
 void kvm_destroy_realm(struct kvm *kvm);
+int kvm_realm_teardown_stage2(struct kvm *kvm);
+
+static inline bool kvm_realm_is_private_address(struct realm *realm,
+						unsigned long addr)
+{
+	return !(addr & BIT(realm->ia_bits - 1));
+}

Maybe move this to patch 9?

Quoted 7 lines
arch/arm64/include/asm/kvm_rmi.h +0 -0
 #endif /* __ASM_KVM_RMI_H */
arch/arm64/kvm/mmu.c +0 -0
index ac72db3d4505..9baf2352d2a8 100644

arch/arm64/kvm/mmu.c int kvm_init_stage2_mmu(struct kvm *kvm, struct kvm_s2_mmu *mmu, unsigned long t · line 1152

Quoted 8 lines
arch/arm64/kvm/mmu.c +5 -0
@@ -1145,9 +1145,25 @@ 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);
+
+	mutex_lock(&kvm->arch.config_lock);

Use guard(mutex)(&kvm->arch.config_lock); here?

arch/arm64/kvm/mmu.c int kvm_init_stage2_mmu(struct kvm *kvm, struct kvm_s2_mmu *mmu, unsigned long t · line 1156

Quoted 4 lines
arch/arm64/kvm/mmu.c +4 -0
+	if (kvm_realm_state(kvm) == REALM_STATE_NEW ||
+	    kvm_realm_state(kvm) == REALM_STATE_ACTIVE) {
+		kvm_set_realm_state(kvm, REALM_STATE_DYING);
+		WARN_ON(kvm_realm_teardown_stage2(kvm));

"teardown" here means unmap, but below like in
realm_tear_down_rtt_level(), it means destroy.

This creates ambiguity and is confusing, can we avoid using "teardown" completely? Or at least make its meaning consistent.

Quoted 161 lines
arch/arm64/kvm/mmu.c +8 -1
+	}
+	mutex_unlock(&kvm->arch.config_lock);
+}
+
 void kvm_uninit_stage2_mmu(struct kvm *kvm)
 {
-	kvm_free_stage2_pgd(&kvm->arch.mmu);
+	if (kvm_realm_is_created(kvm))
+		kvm_realm_uninit_stage2(&kvm->arch.mmu);
+	else
+		kvm_free_stage2_pgd(&kvm->arch.mmu);
 	kvm_mmu_free_memory_cache(&kvm->arch.mmu.split_page_cache);
 }
 
arch/arm64/kvm/rmi.c +139 -0
index 57f59b618a96..74b89da4e801 100644
@@ -16,22 +16,213 @@ u32 kvm_rmm_ipa_limit(void)
 	return u64_get_bits(rmi_feat_reg(0), RMI_FEATURE_REGISTER_0_S2SZ);
 }
 
+static int get_start_level(struct realm *realm)
+{
+	/*
+	 * kvm_init_ipa_range() enforces a minimum of 32 IPA bits
+	 * (ARM64_MIN_PARANGE_BITS).
+	 */
+	return 4 - stage2_pgtable_levels(realm->ia_bits);
+}
+
+static void free_rtt(phys_addr_t phys)
+{
+	kvm_account_pgtable_pages(phys_to_virt(phys), -1);
+	if (rmi_free_delegated_page(phys))
+		kvm_account_pgtable_pages(phys_to_virt(phys), 1);
+}
+
+/*
+ * realm_rtt_destroy - Destroy an RTT at @level for @addr.
+ *
+ * Returns - Result of the RMI_RTT_DESTROY call, and:
+ * @rtt_granule:	RTT granule, if the RTT was destroyed.
+ * @next_addr:		IPA corresponding to the next possible valid entry we
+ *			can target
+ */
+static long realm_rtt_destroy(struct realm *realm, unsigned long addr,
+			      int level, phys_addr_t *rtt_granule,
+			      unsigned long *next_addr)
+{
+	unsigned long out_rtt;
+	long ret;
+
+	ret = rmi_rtt_destroy(realm->rd_phys, addr, level,
+			      &out_rtt, next_addr);
+
+	if (ret == RMI_SUCCESS)
+		*rtt_granule = out_rtt;
+
+	return ret;
+}
+
+static int realm_tear_down_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_tear_down_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 tear down the RTT range for the full IPA
+			 * space, after everything is unmapped. Also we
+			 * descend down only if we cannot tear down 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 tear down the RTTs.
+			 */
+			next_addr = ALIGN(addr + 1, map_size);
+			ret = realm_tear_down_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_tear_down_rtt_range(struct realm *realm,
+				     unsigned long start, unsigned long end)
+{
+	/*
+	 * Root level RTTs can only be destroyed after the RD is destroyed. So
+	 * tear down everything below the root level
+	 */
+	return realm_tear_down_rtt_level(realm, get_start_level(realm) + 1,
+					 start, end);
+}
+
+static int realm_destroy_rtts(struct kvm *kvm)

arch/arm64/kvm/rmi.c u32 kvm_rmm_ipa_limit(void) · line 167

Quoted 10 lines
arch/arm64/kvm/rmi.c +10 -0
+{
+	struct realm *realm = &kvm->arch.realm;
+	unsigned int ia_bits = realm->ia_bits;
+
+	lockdep_assert_held(&kvm->arch.config_lock);
+
+	return realm_tear_down_rtt_range(realm, 0, (1UL << ia_bits));
+}
+
+static void realm_unmap_stage2(struct kvm *kvm)

Can we call this kvm_realm_unmap_stage2_all, expose this..

Quoted 7 lines
arch/arm64/kvm/rmi.c +7 -0
+{
+	struct realm *realm = &kvm->arch.realm;
+
+	lockdep_assert_held(&kvm->arch.config_lock);
+
+	if (realm->stage2_unmapped)
+		return;

arch/arm64/kvm/rmi.c u32 kvm_rmm_ipa_limit(void) · line 184

Quoted 10 lines
arch/arm64/kvm/rmi.c +10 -0
+
+	write_lock(&kvm->mmu_lock);
+	kvm_stage2_unmap_range(&kvm->arch.mmu, 0,
+			       BIT(realm->ia_bits - 1), true);
+	write_unlock(&kvm->mmu_lock);
+
+	realm->stage2_unmapped = true;
+}
+
+int kvm_realm_teardown_stage2(struct kvm *kvm)

and remove this? I feel this function doesn't add much.

Thanks,
Wei-Lin Chang

Quoted 57 lines
arch/arm64/kvm/rmi.c +28 -0
+{
+	lockdep_assert_held(&kvm->arch.config_lock);
+
+	realm_unmap_stage2(kvm);
+	return 0;
+}
+
 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 RMM teardown with Realm creation and stage-2 teardown,
+	 * 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 the IPA space
+	 * 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.
+	 */
+	realm_unmap_stage2(kvm);
+
 	if (realm->rd_phys) {
 		phys_addr_t rd_phys = realm->rd_phys;
 
 		if (WARN_ON(rmi_realm_terminate(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(rd_phys, realm->sro)))
 			goto out_free_sro;
 		rmi_free_delegated_page(rd_phys);
@@ -44,6 +235,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;
Signature · 1 line
2.43.0
#24 lore raw

Re: [PATCH v3 05/11] KVM: arm64: CCA: Tear down RTTs

Quoted 57 lines · Wei-Lin Chang

Hi Steven,

Quoted 54 lines · Steven Price

The RMM owns the stage 2 page tables for a realm, and KVM must request that the RMM creates/destroys entries as necessary. The physical pages to store the page tables are delegated to the realm as required, and can be undelegated when no longer used.

Creating new RTTs is the easy part, tearing down 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).

Tear down a created Realm whether it is still new or has been activated. Unmap the Realm IPA space once during stage-2 teardown. Defer destruction of non-root RTTs until final Realm destruction, after REALM_TERMINATE and before destroying the RD; root RTTs remain until the RD has gone away.

---
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 |   9 ++
 arch/arm64/kvm/mmu.c             |  18 ++-
 arch/arm64/kvm/rmi.c             | 194 +++++++++++++++++++++++++++++++
 3 files changed, 220 insertions(+), 1 deletion(-)
arch/arm64/include/asm/kvm_rmi.h +2 -0
index 6994756a14e6..dd16c004b831 100644
@@ -57,6 +57,7 @@ enum realm_state {
  * @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
+ * @stage2_unmapped: The Realm stage-2 mappings have been removed
  */
 struct realm {
 	phys_addr_t rd_phys;
@@ -70,11 +71,19 @@ struct realm {
 	struct rmi_sro_state *sro;
 	enum realm_state state;
 	unsigned int ia_bits;
+	bool stage2_unmapped;
 };
 
 u32 kvm_rmm_ipa_limit(void);
 
Quoted 11 lines

arch/arm64/include/asm/kvm_rmi.h struct realm { · line 87

Quoted 9 lines
arch/arm64/include/asm/kvm_rmi.h +7 -0
 int kvm_init_realm(struct kvm *kvm);
 void kvm_destroy_realm(struct kvm *kvm);
+int kvm_realm_teardown_stage2(struct kvm *kvm);
+
+static inline bool kvm_realm_is_private_address(struct realm *realm,
+						unsigned long addr)
+{
+	return !(addr & BIT(realm->ia_bits - 1));
+}

Maybe move this to patch 9?

Yes that makes sense.

Quoted 10 lines
Quoted 10 lines
arch/arm64/include/asm/kvm_rmi.h +0 -0
 #endif /* __ASM_KVM_RMI_H */
arch/arm64/kvm/mmu.c +0 -0
index ac72db3d4505..9baf2352d2a8 100644
@@ -1145,9 +1145,25 @@ int kvm_init_stage2_mmu(struct kvm *kvm, struct kvm_s2_mmu *mmu, unsigned long t
 	return err;
 }
 
Quoted 7 lines

arch/arm64/kvm/mmu.c int kvm_init_stage2_mmu(struct kvm *kvm, struct kvm_s2_mmu *mmu, unsigned long t · line 1152

Quoted 5 lines
arch/arm64/kvm/mmu.c +5 -0
+static void kvm_realm_uninit_stage2(struct kvm_s2_mmu *mmu)
+{
+	struct kvm *kvm = kvm_s2_mmu_to_kvm(mmu);
+
+	mutex_lock(&kvm->arch.config_lock);

Use guard(mutex)(&kvm->arch.config_lock); here?

Ack.

Quoted 10 lines

arch/arm64/kvm/mmu.c int kvm_init_stage2_mmu(struct kvm *kvm, struct kvm_s2_mmu *mmu, unsigned long t · line 1156

Quoted 4 lines
arch/arm64/kvm/mmu.c +4 -0
+	if (kvm_realm_state(kvm) == REALM_STATE_NEW ||
+	    kvm_realm_state(kvm) == REALM_STATE_ACTIVE) {
+		kvm_set_realm_state(kvm, REALM_STATE_DYING);
+		WARN_ON(kvm_realm_teardown_stage2(kvm));

"teardown" here means unmap, but below like in
realm_tear_down_rtt_level(), it means destroy.

This creates ambiguity and is confusing, can we avoid using "teardown" completely? Or at least make its meaning consistent.

There are two operations here:

* Unmapping (e.g. RMI_RTT_DATA_UNMAP) where the VM's mapping is removed.

* Destroying (e.g. RMI_RTT_DESTROY) where the VM's page tables are removed.

"Teardown" (or "tear down" - I seem to be inconsistent) really meant a combination of the two - i.e. unmapping everything and destroying the page tables.

But as you note the usage in the code isn't very consistent and it's more confusing that helpful. So I'll reword things to avoid "teardown" which will hopefully make things clearer.

Quoted 185 lines
Quoted 161 lines
arch/arm64/kvm/mmu.c +8 -1
+	}
+	mutex_unlock(&kvm->arch.config_lock);
+}
+
 void kvm_uninit_stage2_mmu(struct kvm *kvm)
 {
-	kvm_free_stage2_pgd(&kvm->arch.mmu);
+	if (kvm_realm_is_created(kvm))
+		kvm_realm_uninit_stage2(&kvm->arch.mmu);
+	else
+		kvm_free_stage2_pgd(&kvm->arch.mmu);
 	kvm_mmu_free_memory_cache(&kvm->arch.mmu.split_page_cache);
 }
 
arch/arm64/kvm/rmi.c +139 -0
index 57f59b618a96..74b89da4e801 100644
@@ -16,22 +16,213 @@ u32 kvm_rmm_ipa_limit(void)
 	return u64_get_bits(rmi_feat_reg(0), RMI_FEATURE_REGISTER_0_S2SZ);
 }
 
+static int get_start_level(struct realm *realm)
+{
+	/*
+	 * kvm_init_ipa_range() enforces a minimum of 32 IPA bits
+	 * (ARM64_MIN_PARANGE_BITS).
+	 */
+	return 4 - stage2_pgtable_levels(realm->ia_bits);
+}
+
+static void free_rtt(phys_addr_t phys)
+{
+	kvm_account_pgtable_pages(phys_to_virt(phys), -1);
+	if (rmi_free_delegated_page(phys))
+		kvm_account_pgtable_pages(phys_to_virt(phys), 1);
+}
+
+/*
+ * realm_rtt_destroy - Destroy an RTT at @level for @addr.
+ *
+ * Returns - Result of the RMI_RTT_DESTROY call, and:
+ * @rtt_granule:	RTT granule, if the RTT was destroyed.
+ * @next_addr:		IPA corresponding to the next possible valid entry we
+ *			can target
+ */
+static long realm_rtt_destroy(struct realm *realm, unsigned long addr,
+			      int level, phys_addr_t *rtt_granule,
+			      unsigned long *next_addr)
+{
+	unsigned long out_rtt;
+	long ret;
+
+	ret = rmi_rtt_destroy(realm->rd_phys, addr, level,
+			      &out_rtt, next_addr);
+
+	if (ret == RMI_SUCCESS)
+		*rtt_granule = out_rtt;
+
+	return ret;
+}
+
+static int realm_tear_down_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_tear_down_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 tear down the RTT range for the full IPA
+			 * space, after everything is unmapped. Also we
+			 * descend down only if we cannot tear down 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 tear down the RTTs.
+			 */
+			next_addr = ALIGN(addr + 1, map_size);
+			ret = realm_tear_down_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_tear_down_rtt_range(struct realm *realm,
+				     unsigned long start, unsigned long end)
+{
+	/*
+	 * Root level RTTs can only be destroyed after the RD is destroyed. So
+	 * tear down everything below the root level
+	 */
+	return realm_tear_down_rtt_level(realm, get_start_level(realm) + 1,
+					 start, end);
+}
+
+static int realm_destroy_rtts(struct kvm *kvm)

arch/arm64/kvm/rmi.c u32 kvm_rmm_ipa_limit(void) · line 167

Quoted 10 lines
arch/arm64/kvm/rmi.c +10 -0
+{
+	struct realm *realm = &kvm->arch.realm;
+	unsigned int ia_bits = realm->ia_bits;
+
+	lockdep_assert_held(&kvm->arch.config_lock);
+
+	return realm_tear_down_rtt_range(realm, 0, (1UL << ia_bits));
+}
+
+static void realm_unmap_stage2(struct kvm *kvm)

Can we call this kvm_realm_unmap_stage2_all, expose this..

Quoted 11 lines
arch/arm64/kvm/rmi.c +11 -0
+{
+	struct realm *realm = &kvm->arch.realm;
+
+	lockdep_assert_held(&kvm->arch.config_lock);
+
+	if (realm->stage2_unmapped)
+		return;
+
+	write_lock(&kvm->mmu_lock);
+	kvm_stage2_unmap_range(&kvm->arch.mmu, 0,
+			       BIT(realm->ia_bits - 1), true);
Quoted 8 lines

arch/arm64/kvm/rmi.c u32 kvm_rmm_ipa_limit(void) · line 184

Quoted 6 lines
arch/arm64/kvm/rmi.c +6 -0
+	write_unlock(&kvm->mmu_lock);
+
+	realm->stage2_unmapped = true;
+}
+
+int kvm_realm_teardown_stage2(struct kvm *kvm)

and remove this? I feel this function doesn't add much.

Yes kvm_realm_teardown_stage2() is a bit pointless now - I think I must have refactored this over time down to nothing but not spotted that it's now not needed. I've also got a WARN_ON() that the function returns 0 which seems somewhat pointless! I'll refactor some more ;)

Thanks for the review!

Steve

Quoted 60 lines

Thanks,
Wei-Lin Chang

Quoted 57 lines
arch/arm64/kvm/rmi.c +28 -0
+{
+	lockdep_assert_held(&kvm->arch.config_lock);
+
+	realm_unmap_stage2(kvm);
+	return 0;
+}
+
 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 RMM teardown with Realm creation and stage-2 teardown,
+	 * 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 the IPA space
+	 * 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.
+	 */
+	realm_unmap_stage2(kvm);
+
 	if (realm->rd_phys) {
 		phys_addr_t rd_phys = realm->rd_phys;
 
 		if (WARN_ON(rmi_realm_terminate(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(rd_phys, realm->sro)))
 			goto out_free_sro;
 		rmi_free_delegated_page(rd_phys);
@@ -44,6 +235,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;
Signature · 1 line
2.43.0
#25 lore raw

PATCH v3 6/11 KVM: arm64: CCA: Add Realm RTT mapping and invalidation helpers

In reply to Steven Price

Add the RMI RTT creation and unmap helpers needed by CCA Realm stage-2 memory operations. REC exit handling and guest requested RIPAS changes follow in the vCPU series.

---
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             | 421 +++++++++++++++++++++++++++++++
 drivers/firmware/arm_rmm/rmi.c   |  10 -
 include/linux/arm-rmi-cmds.h     |   8 +
 4 files changed, 437 insertions(+), 10 deletions(-)
arch/arm64/include/asm/kvm_rmi.h +8 -0
index dd16c004b831..11e227172faf 100644
@@ -80,6 +80,14 @@ int kvm_init_realm(struct kvm *kvm);
 void kvm_destroy_realm(struct kvm *kvm);
 int kvm_realm_teardown_stage2(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);
+
 static inline bool kvm_realm_is_private_address(struct realm *realm,
 						unsigned long addr)
 {
arch/arm64/kvm/rmi.c +421 -0

457 lines not on this page · raw message

drivers/firmware/arm_rmm/rmi.c +0 -10
index 77255a90e0a6..a9f42c9a4f79 100644
@@ -11,7 +11,6 @@
 #include <linux/slab.h>
 
 #include <asm/memory.h>
-#include <asm/pgtable-hwdef.h>
 
 /* RMM v2.0 defines RmiFeatureRegister0 to RmiFeatureRegister4. */
 static unsigned long rmi_feat_reg_cache[5] __ro_after_init;
@@ -183,15 +182,6 @@ int rmi_free_delegated_page(phys_addr_t phys)
 }
 EXPORT_SYMBOL_GPL(rmi_free_delegated_page);
 
-/*
- * Convert the RmiAddrBlockSize to actual size. This is used in RmiDonateReq
- * and RmiAddrRangeDesc*.
- */
-static unsigned long rmi_addr_block_size_to_bytes(unsigned long block_size_fld)
-{
-	return BIT(ARM64_HW_PGTABLE_LEVEL_SHIFT(3 - block_size_fld));
-}
-
 /*
  * free_addr_range: Free memory described by the address range entry, which may
  *		    be partially consumed by RMM.
include/linux/arm-rmi-cmds.h +8 -0
index a84e50ac851c..1a885f1017df 100644
@@ -13,8 +13,16 @@
 #include <linux/string.h>
 #include <linux/types.h>
 
+#include <asm/pgtable-hwdef.h>
+
 #define RMI_MAX_ADDR_LIST	256
 
+/* Convert the block size used by RmiDonateReq and RmiAddrRangeDesc. */
+static inline unsigned long rmi_addr_block_size_to_bytes(unsigned long block_size)
+{
+	return BIT(ARM64_HW_PGTABLE_LEVEL_SHIFT(3 - block_size));
+}
+
 struct rmi_sro_state {
 	struct arm_smccc_1_2_regs regs;
 	int addr_count;
Signature · 1 line
2.43.0
#26 lore raw

Re: [PATCH v3 06/11] KVM: arm64: CCA: Add Realm RTT mapping and invalidation helpers

Thank you for your contribution! Sashiko AI review found 5 potential issues to consider: - [Medium] KVM: arm64: cca: broken split-failure fallback in realm_unmap_*_range() - [Medium] KVM: arm64…

Thank you for your contribution! Sashiko AI review found 5 potential issues to consider:
- [Medium] KVM: arm64: cca: broken split-failure fallback in realm_unmap_*_range()
- [Medium] KVM: arm64: cca: stale err_level after realm_unmap_cache_topup() failure
- [Medium] KVM: arm64: cca: get_start_level() misses 2-level minimum on 64K pages
- [Medium] KVM: arm64: KVM_BUG_ON() on -ENOMEM in realm_unmap_private_range()
- [Low] KVM: arm64: cca: possible RTT creation for unmapped entry in realm_unmap_*()
--

commit 453fd020abf481912e2fee6d2f403740b481af92
Author: Steven Price <[email protected]>

KVM: arm64: CCA: Add Realm RTT mapping and invalidation helpers

This commit adds RMI RTT creation, folding, and unmap helpers for CCA Realm stage-2 shared and private address ranges.

Quoted 4 lines
arch/arm64/kvm/rmi.c +0 -0
index 74b89da4e8015..838f95c201ada 100644

[ ... ]

Quoted 7 lines
arch/arm64/kvm/rmi.c +3 -0
@@ -154,12 +291,94 @@ static int realm_tear_down_rtt_range(struct realm *realm,
 					 start, end);
 }
 
+/*
+ * Returns 0 on successful fold, a negative value on error, a positive value if
+ * we were not able to fold all tables at this level.

arch/arm64/kvm/rmi.c static int realm_tear_down_rtt_range(struct realm *realm, · line 306

Quoted 10 lines
arch/arm64/kvm/rmi.c +10 -0
+ */
+static int realm_fold_rtt_level(struct realm *realm, int level,
+				unsigned long start, unsigned long end)
+{
+	int not_folded = 0;
+	ssize_t map_size;
+	unsigned long addr, next_addr;
+
+	if (WARN_ON(level > KVM_PGTABLE_LAST_LEVEL))
+		return -EINVAL;

[Severity: Medium]
On a CONFIG_ARM64_64K_PAGES kernel, can realm_fold_rtt_level() be called with level == 4 and trigger this WARN_ON()?

For a 32-bit or 33-bit IPA, stage2_pgtable_levels(realm->ia_bits) returns 1 on 64K pages, so get_start_level() returns 4 - 1 = 3 (KVM_PGTABLE_LAST_LEVEL) even though kvm_get_vtcr() clamps stage-2 tables to at least 2 levels (start_level = 2). When realm_unmap_shared_range() or realm_unmap_private_range() then calls realm_fold_rtt_level(realm, get_start_level(realm) + 1, start, end), level is 4, triggering the warning and skipping RTT folding (and similarly in realm_tear_down_rtt_range()).

[ ... ]

Quoted 59 lines
arch/arm64/kvm/rmi.c +55 -0
@@ -189,6 +408,208 @@ int kvm_realm_teardown_stage2(struct kvm *kvm)
 	return 0;
 }
 
+static bool realm_unmap_cache_topup(struct kvm *kvm,
+				    struct kvm_mmu_memory_cache *mc, int needed)
+{
+	int ret;
+
+	if (!mc || kvm_mmu_memory_cache_nr_free_objects(mc) >= needed)
+		return false;
+
+	lockdep_assert_held_write(&kvm->mmu_lock);
+	write_unlock(&kvm->mmu_lock);
+	ret = __kvm_mmu_topup_memory_cache(mc, KVM_PGTABLE_LAST_LEVEL, needed);
+	write_lock(&kvm->mmu_lock);
+
+	return !ret;
+}
+
+static void realm_unmap_shared_range(struct kvm *kvm,
+				     unsigned long start,
+				     unsigned long end,
+				     bool may_block,
+				     struct kvm_mmu_memory_cache *mc)
+{
+	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);
+

arch/arm64/kvm/rmi.c int kvm_realm_teardown_stage2(struct kvm *kvm) · line 475

Quoted 10 lines
arch/arm64/kvm/rmi.c +10 -0
+			if (WARN_ON(err_level < get_start_level(realm) ||
+				    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;
+			}

[Severity: Low]
This question rests on an RMM architecture detail that could not be confirmed from the kernel source: can SMC_RMI_RTT_UNPROT_UNMAP or SMC_RMI_RTT_DATA_UNMAP return RMI_ERROR_RTT with err_level < level when the RTT walk stops at an unassigned ancestor entry (where neither a child RTT nor a block mapping exists), rather than advancing next_addr across the unmapped ancestor and returning RMI_SUCCESS?

If yes, unmapping a sub-block range (such as a single page where level is KVM_PGTABLE_LAST_LEVEL) in an unpopulated region will fall through to realm_create_rtt_levels() in both realm_unmap_shared_range() and realm_unmap_private_range(), allocating and delegating intermediate RTTs only to fold them again at the end of the function, and hitting KVM_BUG_ON(1, kvm) in realm_unmap_private_range() if that allocation fails.

arch/arm64/kvm/rmi.c int kvm_realm_teardown_stage2(struct kvm *kvm) · line 484

Quoted 9 lines
arch/arm64/kvm/rmi.c +9 -0
+
+			if (may_block && realm_unmap_cache_topup(kvm, mc,
+								 level - err_level)) {
+				next_addr = addr;
+				break;
+			}
+
+			ret = realm_create_rtt_levels(realm, addr, err_level,
+						      level, mc);

[Severity: Medium]
Can realm_create_rtt_levels() run with a stale err_level if __kvm_mmu_topup_memory_cache() fails inside realm_unmap_cache_topup()?

When the cache has fewer than needed objects and topup fails with -ENOMEM, realm_unmap_cache_topup() still drops and re-acquires kvm->mmu_lock before returning false. If another thread folds or modifies the RTTs while mmu_lock is dropped, continuing into realm_create_rtt_levels() with the pre-unlock err_level (in both realm_unmap_shared_range() and realm_unmap_private_range()) can fail with RMI_ERROR_RTT at an ancestor level and trigger the WARN() in realm_create_rtt_levels().

arch/arm64/kvm/rmi.c int kvm_realm_teardown_stage2(struct kvm *kvm) · line 495

Quoted 11 lines
arch/arm64/kvm/rmi.c +11 -0
+			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;

[Severity: Medium]
If realm_create_rtt_levels() fails partway through a multi-level split (for example, after creating a level-2 RTT to split a 1 GiB level-1 block, then failing with -ENOMEM at level 3), doesn't this fallback use the initial err_level and widen the unmap to the full 1 GiB block instead of the 2 MiB block at the last level created?

Also, when next_addr is aligned down below start, start is not updated, so the realm_fold_rtt_level() call after the loop starts at the original start and skips folding RTTs in [next_addr, start). Both issues also apply to the fallback in realm_unmap_private_range().

[ ... ]

Quoted 6 lines
arch/arm64/kvm/rmi.c +6 -0
+static void realm_unmap_private_range(struct kvm *kvm,
+				      unsigned long start,
+				      unsigned long end,
+				      bool may_block,
+				      struct kvm_mmu_memory_cache *mc)
+{

[ ... ]

arch/arm64/kvm/rmi.c int kvm_realm_teardown_stage2(struct kvm *kvm)

Quoted 8 lines
arch/arm64/kvm/rmi.c +8 -0
+			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. */\n> +			KVM_BUG_ON(1, kvm);

[Severity: Medium]
Can an RTT allocation failure (-ENOMEM) in realm_create_rtt_levels() trigger a host WARN_ON_ONCE() via KVM_BUG_ON()?

Both __kvm_mmu_topup_memory_cache() and alloc_delegated_granule() use __GFP_ACCOUNT, so a host VMM hitting its memcg limit or host memory pressure can cause realm_create_rtt_levels() to return -ENOMEM. Unlike realm_unmap_shared_range(), which guards its warning with WARN_ON(ret != -ENOMEM), KVM_BUG_ON(1, kvm) unconditionally warns even on -ENOMEM; using WARN_ON(ret != -ENOMEM) and marking the VM dead or bugged without warning on allocation failure would avoid a user-triggerable kernel warning.

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

Re: [PATCH v3 06/11] KVM: arm64: CCA: Add Realm RTT mapping and invalidation helpers

Quoted 60 lines · On 16/11/2026 17:21, [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 5 potential issues to consider:
- [Medium] KVM: arm64: cca: broken split-failure fallback in realm_unmap_*_range()
- [Medium] KVM: arm64: cca: stale err_level after realm_unmap_cache_topup() failure
- [Medium] KVM: arm64: cca: get_start_level() misses 2-level minimum on 64K pages
- [Medium] KVM: arm64: KVM_BUG_ON() on -ENOMEM in realm_unmap_private_range()
- [Low] KVM: arm64: cca: possible RTT creation for unmapped entry in realm_unmap_*()
--

commit 453fd020abf481912e2fee6d2f403740b481af92
Author: Steven Price <[email protected]>

KVM: arm64: CCA: Add Realm RTT mapping and invalidation helpers

This commit adds RMI RTT creation, folding, and unmap helpers for CCA Realm stage-2 shared and private address ranges.

Quoted 4 lines
arch/arm64/kvm/rmi.c +0 -0
index 74b89da4e8015..838f95c201ada 100644

[ ... ]

Quoted 7 lines
arch/arm64/kvm/rmi.c +3 -0
@@ -154,12 +291,94 @@ static int realm_tear_down_rtt_range(struct realm *realm,
 					 start, end);
 }
 
+/*
+ * Returns 0 on successful fold, a negative value on error, a positive value if
+ * we were not able to fold all tables at this level.

arch/arm64/kvm/rmi.c static int realm_tear_down_rtt_range(struct realm *realm, · line 306

Quoted 10 lines
arch/arm64/kvm/rmi.c +10 -0
+ */
+static int realm_fold_rtt_level(struct realm *realm, int level,
+				unsigned long start, unsigned long end)
+{
+	int not_folded = 0;
+	ssize_t map_size;
+	unsigned long addr, next_addr;
+
+	if (WARN_ON(level > KVM_PGTABLE_LAST_LEVEL))
+		return -EINVAL;

[Severity: Medium]
On a CONFIG_ARM64_64K_PAGES kernel, can realm_fold_rtt_level() be called with level == 4 and trigger this WARN_ON()?

For a 32-bit or 33-bit IPA, stage2_pgtable_levels(realm->ia_bits) returns 1 on 64K pages, so get_start_level() returns 4 - 1 = 3 (KVM_PGTABLE_LAST_LEVEL) even though kvm_get_vtcr() clamps stage-2 tables to at least 2 levels (start_level = 2). When realm_unmap_shared_range() or realm_unmap_private_range() then calls realm_fold_rtt_level(realm, get_start_level(realm) + 1, start, end), level is 4, triggering the warning and skipping RTT folding (and similarly in realm_tear_down_rtt_range()).

Quoted 1 line

[ ... ]

Already discussed in previous email about get_start_level().

Quoted 70 lines
Quoted 59 lines
arch/arm64/kvm/rmi.c +55 -0
@@ -189,6 +408,208 @@ int kvm_realm_teardown_stage2(struct kvm *kvm)
 	return 0;
 }
 
+static bool realm_unmap_cache_topup(struct kvm *kvm,
+				    struct kvm_mmu_memory_cache *mc, int needed)
+{
+	int ret;
+
+	if (!mc || kvm_mmu_memory_cache_nr_free_objects(mc) >= needed)
+		return false;
+
+	lockdep_assert_held_write(&kvm->mmu_lock);
+	write_unlock(&kvm->mmu_lock);
+	ret = __kvm_mmu_topup_memory_cache(mc, KVM_PGTABLE_LAST_LEVEL, needed);
+	write_lock(&kvm->mmu_lock);
+
+	return !ret;
+}
+
+static void realm_unmap_shared_range(struct kvm *kvm,
+				     unsigned long start,
+				     unsigned long end,
+				     bool may_block,
+				     struct kvm_mmu_memory_cache *mc)
+{
+	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);
+

arch/arm64/kvm/rmi.c int kvm_realm_teardown_stage2(struct kvm *kvm) · line 475

Quoted 10 lines
arch/arm64/kvm/rmi.c +10 -0
+			if (WARN_ON(err_level < get_start_level(realm) ||
+				    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;
+			}
Quoted 7 lines

[Severity: Low]
This question rests on an RMM architecture detail that could not be confirmed from the kernel source: can SMC_RMI_RTT_UNPROT_UNMAP or SMC_RMI_RTT_DATA_UNMAP return RMI_ERROR_RTT with err_level < level when the RTT walk stops at an unassigned ancestor entry (where neither a child RTT nor a block mapping exists), rather than advancing next_addr across the unmapped ancestor and returning RMI_SUCCESS?

As per the spec:

IQTXJY: A Range-based memory unmapping operation skips over RTT entries within the target IPA range whose state is already equal to the target value.

So no it won't stop at an unassigned entry.

Quoted 22 lines

If yes, unmapping a sub-block range (such as a single page where level is KVM_PGTABLE_LAST_LEVEL) in an unpopulated region will fall through to realm_create_rtt_levels() in both realm_unmap_shared_range() and realm_unmap_private_range(), allocating and delegating intermediate RTTs only to fold them again at the end of the function, and hitting KVM_BUG_ON(1, kvm) in realm_unmap_private_range() if that allocation fails.

arch/arm64/kvm/rmi.c int kvm_realm_teardown_stage2(struct kvm *kvm) · line 484

Quoted 9 lines
arch/arm64/kvm/rmi.c +9 -0
+
+			if (may_block && realm_unmap_cache_topup(kvm, mc,
+								 level - err_level)) {
+				next_addr = addr;
+				break;
+			}
+
+			ret = realm_create_rtt_levels(realm, addr, err_level,
+						      level, mc);

[Severity: Medium]
Can realm_create_rtt_levels() run with a stale err_level if __kvm_mmu_topup_memory_cache() fails inside realm_unmap_cache_topup()?

Quoted 8 lines

When the cache has fewer than needed objects and topup fails with -ENOMEM, realm_unmap_cache_topup() still drops and re-acquires kvm->mmu_lock before returning false. If another thread folds or modifies the RTTs while mmu_lock is dropped, continuing into realm_create_rtt_levels() with the pre-unlock err_level (in both realm_unmap_shared_range() and realm_unmap_private_range()) can fail with RMI_ERROR_RTT at an ancestor level and trigger the WARN() in realm_create_rtt_levels().

I don't really get how this could sensibly be improved. In the event that the cache top-up fails then we're already critically low on memory. Looping round to have an up-to-date err_level would (most likely) put us back in the same situation of needing to top-up the cache (and failing).

Continuing does indeed use a potentially stale err_level in the call to realm_create_rtt_levels(). The only issue here is if another thread had folded (if the RTTs had been created then we already handle that case gracefully). In that case we'd need even more memory so the operation would almost certainly fail.

And if realm_create_rtt_levels() fails then we fall to unmapping the entire block - which isn't ideal but regains memory (which we're apparently very short on).

There's perhaps an argument that the WARN_ON() below might be able to trigger, but then under these extreme memory allocation failures we should already have the allocator WARNing. So I don't think that's a real consideration either.

Quoted 19 lines

arch/arm64/kvm/rmi.c int kvm_realm_teardown_stage2(struct kvm *kvm) · line 495

Quoted 11 lines
arch/arm64/kvm/rmi.c +11 -0
+			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;

[Severity: Medium]
If realm_create_rtt_levels() fails partway through a multi-level split (for example, after creating a level-2 RTT to split a 1 GiB level-1 block, then failing with -ENOMEM at level 3), doesn't this fallback use the initial err_level and widen the unmap to the full 1 GiB block instead of the 2 MiB block at the last level created?

Quoted 4 lines

Also, when next_addr is aligned down below start, start is not updated, so the realm_fold_rtt_level() call after the loop starts at the original start and skips folding RTTs in [next_addr, start). Both issues also apply to the fallback in realm_unmap_private_range().

This is technically correct. But equally this is a "shouldn't happen in practice" situation. If we can't split then we can't unmap the requested range. While there are corner cases like Sashiko describes which could be improved on slightly, I don't see the point in unnecessarily complicating the code for them. Ultimately if we get here then we're unmapping too much - and we just have to hope that the system as a whole can recover at the cost of the VM.

Certainly in the case of realm_unmap_private_range() the VM can't recover (hence KVM_BUG_ON()). For the shared memory case we can at least fault the memory back in if memory pressure has reduced sufficiently.

Quoted 11 lines

[ ... ]

Quoted 6 lines
arch/arm64/kvm/rmi.c +6 -0
+static void realm_unmap_private_range(struct kvm *kvm,
+				      unsigned long start,
+				      unsigned long end,
+				      bool may_block,
+				      struct kvm_mmu_memory_cache *mc)
+{

[ ... ]

Quoted 12 lines

arch/arm64/kvm/rmi.c int kvm_realm_teardown_stage2(struct kvm *kvm)

Quoted 8 lines
arch/arm64/kvm/rmi.c +8 -0
+			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. */\n> +			KVM_BUG_ON(1, kvm);

[Severity: Medium]
Can an RTT allocation failure (-ENOMEM) in realm_create_rtt_levels() trigger a host WARN_ON_ONCE() via KVM_BUG_ON()?

Yes.

Quoted 8 lines

Both __kvm_mmu_topup_memory_cache() and alloc_delegated_granule() use __GFP_ACCOUNT, so a host VMM hitting its memcg limit or host memory pressure can cause realm_create_rtt_levels() to return -ENOMEM. Unlike realm_unmap_shared_range(), which guards its warning with WARN_ON(ret != -ENOMEM), KVM_BUG_ON(1, kvm) unconditionally warns even on -ENOMEM; using WARN_ON(ret != -ENOMEM) and marking the VM dead or bugged without warning on allocation failure would avoid a user-triggerable kernel warning.

So the cache topup can fail (silently) due to memcg limits. However alloc_delegated_granule() falls back to __get_free_page(GFP_ATOMIC | __GFP_ZERO | __GFP_ACCOUNT) which AFAICT will not be subject to the memcg limit and will WARN itself before failing.

So unless I've misunderstood something the only path that can trigger the WARN_ON_ONCE in KVM_BUG_ON() is one which has already triggered a WARN.

As above this isn't a case I expect to happen, and it would be good to be fairly noisy about it because we've just killed off the VM (most likely due to no fault of the VM or VMM). But I'm open to suggestions to improve this if anyone has them.

Thanks,
Steve

#28 lore raw

PATCH v3 7/11 KVM: Allow architectures to reject memslot changes before invalidation

In reply to Steven Price

From: Suzuki K Poulose <[email protected]>

Architectures may need to reject deletion of a memory slot without changing the guest's memory mappings. Currently, KVM invalidates the slot and calls kvm_arch_flush_shadow_memslot() before kvm_arch_prepare_memory_region() can reject the request. On arm64, this unmaps the slot from stage-2. Restoring the old memslot on failure does not restore those mappings, so even a rejected deletion has side effects.

Add kvm_arch_validate_memory_region() and invoke it before invalidating the slot, with slots_lock and slots_arch_lock held. The hook performs validation only, allowing architectures to reject a request without changing the memslot or its mappings. Provide a weak no-op implementation for architectures that do not need early validation.

Move arm64's protected VM checks into the new hook so that unsupported deletions and moves are rejected before stage-2 unmapping. Keep resource preparation after invalidation, since invalidation drops slots_arch_lock and allows concurrent updates to architecture-specific memslot metadata.

---
 arch/arm64/kvm/mmu.c     | 22 ++++++++++++++++------
 arch/arm64/kvm/pkvm.c    |  2 +-
 include/linux/kvm_host.h |  4 ++++
 virt/kvm/kvm_main.c      | 33 ++++++++++++++++++++++++++-------
 4 files changed, 47 insertions(+), 14 deletions(-)
arch/arm64/kvm/mmu.c +16 -6
index 9baf2352d2a8..759ac0453828 100644
@@ -2851,13 +2851,12 @@ void kvm_arch_commit_memory_region(struct kvm *kvm,
 	}
 }
 
-int kvm_arch_prepare_memory_region(struct kvm *kvm,
-				   const struct kvm_memory_slot *old,
-				   struct kvm_memory_slot *new,
-				   enum kvm_mr_change change)
+int kvm_arch_validate_memory_region(struct kvm *kvm,
+				    const struct kvm_memory_slot *old,
+				    const struct kvm_memory_slot *new,
+				    enum kvm_mr_change change)
 {
-	hva_t hva, reg_end;
-	int ret = 0;
+	lockdep_assert_held(&kvm->slots_lock);
 
 	if (kvm_vm_is_protected(kvm)) {
 		/* Cannot modify memslots once a pVM has run. */
@@ -2872,6 +2871,17 @@ int kvm_arch_prepare_memory_region(struct kvm *kvm,
 		}
 	}
 
+	return 0;
+}
+
+int kvm_arch_prepare_memory_region(struct kvm *kvm,
+				   const struct kvm_memory_slot *old,
+				   struct kvm_memory_slot *new,
+				   enum kvm_mr_change change)
+{
+	hva_t hva, reg_end;
+	int ret = 0;
+
 	if (change != KVM_MR_CREATE && change != KVM_MR_MOVE &&
 			change != KVM_MR_FLAGS_ONLY)
 		return 0;
arch/arm64/kvm/pkvm.c +1 -1
index a5145bd75d0c..f4db0e051d6a 100644
@@ -262,7 +262,7 @@ int pkvm_create_hyp_vm(struct kvm *kvm)
 	int ret = 0;
 
 	/*
-	 * Synchronise with kvm_arch_prepare_memory_region(), as we
+	 * Synchronise with kvm_arch_validate_memory_region(), as we
 	 * prevent memslot modifications on a pVM that has been run.
 	 */
 	mutex_lock(&kvm->slots_lock);
include/linux/kvm_host.h +4 -0
index f65728a713ec..15848221860f 100644
@@ -1255,6 +1255,10 @@ int kvm_set_internal_memslot(struct kvm *kvm,
 			     const struct kvm_userspace_memory_region2 *mem);
 void kvm_arch_free_memslot(struct kvm *kvm, struct kvm_memory_slot *slot);
 void kvm_arch_memslots_updated(struct kvm *kvm, u64 gen);
+int kvm_arch_validate_memory_region(struct kvm *kvm,
+				    const struct kvm_memory_slot *old,
+				    const struct kvm_memory_slot *new,
+				    enum kvm_mr_change change);
 int kvm_arch_prepare_memory_region(struct kvm *kvm,
 				const struct kvm_memory_slot *old,
 				struct kvm_memory_slot *new,
virt/kvm/kvm_main.c +26 -7
index cf4cb586f140..3ef370d71818 100644
@@ -1682,6 +1682,20 @@ static void kvm_swap_active_memslots(struct kvm *kvm, int as_id)
 	slots->generation = gen;
 }
 
+/*
+ * Validate a memslot change before invalidation, which may irreversibly reclaim
+ * private guest memory. Called with slots_lock and slots_arch_lock held.
+ * Implementations must not modify memslot state or allocate resources; leave
+ * those operations to kvm_arch_prepare_memory_region().
+ */
+__weak int kvm_arch_validate_memory_region(struct kvm *kvm,
+					   const struct kvm_memory_slot *old,
+					   const struct kvm_memory_slot *new,
+					   enum kvm_mr_change change)
+{
+	return 0;
+}
+
 static int kvm_prepare_memory_region(struct kvm *kvm,
 				     const struct kvm_memory_slot *old,
 				     struct kvm_memory_slot *new,
@@ -1934,6 +1948,10 @@ static int kvm_set_memslot(struct kvm *kvm,
 	 */
 	mutex_lock(&kvm->slots_arch_lock);
 
+	r = kvm_arch_validate_memory_region(kvm, old, new, change);
+	if (r)
+		goto unlock_err;
+
 	/*
 	 * Invalidate the old slot if it's being deleted or moved.  This is
 	 * done prior to actually deleting/moving the memslot to allow vCPUs to
@@ -1950,22 +1968,22 @@ static int kvm_set_memslot(struct kvm *kvm,
 	if (change == KVM_MR_DELETE || change == KVM_MR_MOVE) {
 		invalid_slot = kzalloc_obj(*invalid_slot, GFP_KERNEL_ACCOUNT);
 		if (!invalid_slot) {
-			mutex_unlock(&kvm->slots_arch_lock);
-			return -ENOMEM;
+			r = -ENOMEM;
+			goto unlock_err;
 		}
 		kvm_invalidate_memslot(kvm, old, invalid_slot);
 	}
 
 	r = kvm_prepare_memory_region(kvm, old, new, change);
 	if (r)
-		goto err;
+		goto revert_slot;
 
 	if (change == KVM_MR_CREATE && (new->flags & KVM_MEM_GUEST_MEMFD)) {
 		r = kvm_gmem_commit_memory_region(kvm, new);
 		if (r) {
 			kvm_arch_free_memslot(kvm, new);
 			kvm_destroy_dirty_bitmap(new);
-			goto err;
+			goto unlock_err;
 		}
 	}
 
@@ -2000,7 +2018,7 @@ static int kvm_set_memslot(struct kvm *kvm,
 
 	return 0;
 
-err:
+revert_slot:
 	/*
 	 * For DELETE/MOVE, revert the above INVALID change.  No modifications
 	 * required since the original slot was preserved in the inactive slots.
@@ -2009,9 +2027,10 @@ static int kvm_set_memslot(struct kvm *kvm,
 	if (change == KVM_MR_DELETE || change == KVM_MR_MOVE) {
 		kvm_activate_memslot(kvm, invalid_slot, old);
 		kfree(invalid_slot);
-	} else {
-		mutex_unlock(&kvm->slots_arch_lock);
+		return r;
 	}
+unlock_err:
+	mutex_unlock(&kvm->slots_arch_lock);
 	return r;
 }
 
Signature · 1 line
2.43.0
#29 lore raw

Re: [PATCH v3 07/11] KVM: Allow architectures to reject memslot changes before invalidation

Hi Suzuki,

On Mon, 16 Nov 2026 16:43:53 +0000, Steven Price <[email protected]> wrote:
[...]

Quoted 6 lines

Architectures may need to reject deletion of a memory slot without changing the guest's memory mappings. Currently, KVM invalidates the slot and calls kvm_arch_flush_shadow_memslot() before kvm_arch_prepare_memory_region() can reject the request. On arm64, this unmaps the slot from stage-2. Restoring the old memslot on failure does not restore those mappings, so even a rejected deletion has side effects.

[...]

Quoted 4 lines

Move arm64's protected VM checks into the new hook so that unsupported deletions and moves are rejected before stage-2 unmapping. Keep resource preparation after invalidation, since invalidation drops slots_arch_lock and allows concurrent updates to architecture-specific memslot metadata.

For pVMs the invalidation doesn't touch stage-2 (protected_pkvm_vm_s2_ops uses no_stage2_unmap_range()), so the reason for the hook is the Realm case in the next patch. Can you change the message to say that?

Cheers,
/fuad

#30 lore raw

Re: [PATCH v3 07/11] KVM: Allow architectures to reject memslot changes before invalidation

Quoted 16 lines · Fuad Tabba

Hi Suzuki,

On Mon, 16 Nov 2026 16:43:53 +0000, Steven Price <[email protected]> wrote:
[...]

Quoted 6 lines

Architectures may need to reject deletion of a memory slot without changing the guest's memory mappings. Currently, KVM invalidates the slot and calls kvm_arch_flush_shadow_memslot() before kvm_arch_prepare_memory_region() can reject the request. On arm64, this unmaps the slot from stage-2. Restoring the old memslot on failure does not restore those mappings, so even a rejected deletion has side effects.

[...]

Quoted 4 lines

Move arm64's protected VM checks into the new hook so that unsupported deletions and moves are rejected before stage-2 unmapping. Keep resource preparation after invalidation, since invalidation drops slots_arch_lock and allows concurrent updates to architecture-specific memslot metadata.

Quoted 4 lines

For pVMs the invalidation doesn't touch stage-2 (protected_pkvm_vm_s2_ops uses no_stage2_unmap_range()), so the reason for the hook is the Realm case in the next patch. Can you change the message to say that?

Yes I'll clarify that.

Thanks,
Steve

#31 lore raw

PATCH v3 8/11 KVM: arm64: CCA: Prevent memslot modifications after Realm creation

In reply to Steven Price

From: Suzuki K Poulose <[email protected]>

Once the Realm is created, prevent changes to guest_memfd backed memslots, which can provide private memory. Validate these changes before KVM invalidates the old memslot, so a rejected deletion leaves its stage-2 mappings intact. Realm stage-2 faults will also validate the private memory backing in a later commit.

Require Realm memslots to lie in the lower half of the IPA space, as the upper, shared half aliases the lower, protected half.

Define kvm_arch_has_private_mem() so generic KVM recognizes Realm VMs as supporting private memory.

Additionally, similar to pKVM, do not support dirty logging or read-only memslots.

---
Changes since v1:
- Preserve private mappings if KVM invalidates a Realm guest_memfd slot before the prepare hook rejects its deletion.

---
 arch/arm64/include/asm/kvm_host.h |  2 ++
 arch/arm64/kvm/mmu.c              | 37 +++++++++++++++++++++++--------
 2 files changed, 30 insertions(+), 9 deletions(-)
arch/arm64/include/asm/kvm_host.h +2 -0
index aaac97a7461c..2c8cd9a1dd52 100644
@@ -1611,6 +1611,8 @@ struct kvm *kvm_arch_alloc_vm(void);
 
 #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)					\
 	(system_supports_mte() &&				\
 	 test_bit(KVM_ARCH_FLAG_MTE_ENABLED, &(kvm)->arch.flags))
arch/arm64/kvm/mmu.c +28 -9
index 759ac0453828..0db3c293fdd5 100644
@@ -2858,17 +2858,32 @@ int kvm_arch_validate_memory_region(struct kvm *kvm,
 {
 	lockdep_assert_held(&kvm->slots_lock);
 
-	if (kvm_vm_is_protected(kvm)) {
+	if (kvm_vm_is_protected(kvm) && new &&
+	    (new->flags & (KVM_MEM_LOG_DIRTY_PAGES | KVM_MEM_READONLY)))
+		return -EPERM;
+
+	switch (kvm->arch.vm_flavor) {
+	case VM_PROTECTED_PKVM:
 		/* Cannot modify memslots once a pVM has run. */
 		if (pkvm_hyp_vm_is_created(kvm) &&
-		    (change == KVM_MR_DELETE || change == KVM_MR_MOVE)) {
+		    (change == KVM_MR_DELETE || change == KVM_MR_MOVE))
 			return -EPERM;
-		}
+		break;
+	case VM_REALM:
+		/*
+		 * Once the Realm is created, we cannot modify any slots that
+		 * could be providing private memory. i.e., guest_memfd backed
+		 * slots.
+		 * TODO: Handle trusted device private memory slots
+		 */
+		if (!kvm_realm_is_created(kvm))
+			break;
 
-		if (new &&
-		    new->flags & (KVM_MEM_LOG_DIRTY_PAGES | KVM_MEM_READONLY)) {
+		if (kvm_slot_has_gmem(old) || kvm_slot_has_gmem(new))
 			return -EPERM;
-		}
+		break;
+	default:
+		break;
 	}
 
 	return 0;
@@ -2880,6 +2895,7 @@ 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 (change != KVM_MR_CREATE && change != KVM_MR_MOVE &&
@@ -2887,10 +2903,13 @@ int kvm_arch_prepare_memory_region(struct kvm *kvm,
 		return 0;
 
 	/*
-	 * Prevent userspace from creating a memory region outside of the IPA
-	 * space addressable by the KVM guest IPA space.
+	 * Realm shared IPAs alias the corresponding private IPAs, so memslots
+	 * must reside entirely in the canonical lower half of the IPA 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;
 
 	/*
Signature · 1 line
2.43.0
#32 lore raw

Re: [PATCH v3 08/11] KVM: arm64: CCA: Prevent memslot modifications after Realm creation

On Mon, 16 Nov 2026 16:43:54 +0000, Steven Price <[email protected]> wrote:
[...]

Quoted 2 lines

Define kvm_arch_has_private_mem() so generic KVM recognizes Realm VMs as supporting private memory.

[...]

Quoted 1 line
arch/arm64/include/asm/kvm_host.h +0 -0

[...]

arch/arm64/include/asm/kvm_host.h struct kvm *kvm_arch_alloc_vm(void); · line 1614

Quoted 5 lines
arch/arm64/include/asm/kvm_host.h +1 -0
@@ -1611,6 +1611,8 @@ struct kvm *kvm_arch_alloc_vm(void);
 
 #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)

Could the message say that defining the macro makes gmem_in_place_conversion true for every arm64 VM? One visible change is that KVM_SET_MEMORY_ATTRIBUTES2 on a non-Realm VM's guest_memfd returns -EINVAL instead of -ENOTTY.

[...]

Quoted 1 line
arch/arm64/kvm/mmu.c +0 -0

[...]

Quoted 5 lines
arch/arm64/kvm/mmu.c +0 -1
@@ -2887,10 +2903,13 @@ int kvm_arch_prepare_memory_region(struct kvm *kvm,
                 return 0;
 
         /*
-         * Prevent userspace from creating a memory region outside of the IPA

arch/arm64/kvm/mmu.c int kvm_arch_prepare_memory_region(struct kvm *kvm, · line 2913

Quoted 10 lines
arch/arm64/kvm/mmu.c +6 -2
-         * space addressable by the KVM guest IPA space.
+         * Realm shared IPAs alias the corresponding private IPAs, so memslots
+         * must reside entirely in the canonical lower half of the IPA 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;

This check still applies to every VM, so I'd keep the old comment and add the Realm sentence to it.

Cheers,
/fuad

#33 lore raw

Re: [PATCH v3 08/11] KVM: arm64: CCA: Prevent memslot modifications after Realm creation

Quoted 9 lines · Fuad Tabba

On Mon, 16 Nov 2026 16:43:54 +0000, Steven Price <[email protected]> wrote:
[...]

Quoted 2 lines

Define kvm_arch_has_private_mem() so generic KVM recognizes Realm VMs as supporting private memory.

[...]

Quoted 1 line
arch/arm64/include/asm/kvm_host.h +0 -0

[...]

Quoted 2 lines
arch/arm64/include/asm/kvm_host.h +0 -0
@@ -1611,6 +1611,8 @@ struct kvm *kvm_arch_alloc_vm(void);
 
Quoted 8 lines

arch/arm64/include/asm/kvm_host.h struct kvm *kvm_arch_alloc_vm(void); · line 1614

Quoted 3 lines
arch/arm64/include/asm/kvm_host.h +1 -0
 #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)

Could the message say that defining the macro makes gmem_in_place_conversion true for every arm64 VM? One visible change is that KVM_SET_MEMORY_ATTRIBUTES2 on a non-Realm VM's guest_memfd returns -EINVAL instead of -ENOTTY.

True - I'll explicitly mention that the commit message just in case anyone bisects that in the future.

Quoted 6 lines

[...]

Quoted 1 line
arch/arm64/kvm/mmu.c +0 -0

[...]

Quoted 3 lines
arch/arm64/kvm/mmu.c +0 -0
@@ -2887,10 +2903,13 @@ int kvm_arch_prepare_memory_region(struct kvm *kvm,
                 return 0;
 
Quoted 15 lines

arch/arm64/kvm/mmu.c int kvm_arch_prepare_memory_region(struct kvm *kvm, · line 2913

Quoted 12 lines
arch/arm64/kvm/mmu.c +6 -3
         /*
-         * Prevent userspace from creating a memory region outside of the IPA
-         * space addressable by the KVM guest IPA space.
+         * Realm shared IPAs alias the corresponding private IPAs, so memslots
+         * must reside entirely in the canonical lower half of the IPA 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;

This check still applies to every VM, so I'd keep the old comment and add the Realm sentence to it.

Ack.

Thanks,
Steve

#34 lore raw

PATCH v3 9/11 KVM: arm64: CCA: Support runtime faulting of memory

In reply to Steven Price

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 page into the realm and initializes its contents to zero.

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.

---
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_mmu.h     |   4 +-
 arch/arm64/include/asm/kvm_rmi.h     |   6 +
 arch/arm64/kvm/mmu.c                 |  96 ++++++++--
 arch/arm64/kvm/rmi.c                 | 251 +++++++++++++++++++++++++++
 5 files changed, 351 insertions(+), 19 deletions(-)
arch/arm64/include/asm/kvm_emulate.h +13 -0
index 32021bdf2610..8a71fddfdb85 100644
@@ -847,4 +847,17 @@ static inline bool kvm_realm_is_created(struct kvm *kvm)
 	return kvm_vm_is_realm(kvm) && kvm_realm_state(kvm) != REALM_STATE_NONE;
 }
 
+static inline gpa_t kvm_gpa_from_fault(struct kvm *kvm, phys_addr_t ipa)
+{
+	if (!kvm_vm_is_realm(kvm))
+		return ipa;
+
+	return ipa & ~BIT(kvm->arch.realm.ia_bits - 1);
+}
+
+static inline gfn_t kvm_gfn_from_fault(struct kvm *kvm, phys_addr_t ipa)
+{
+	return gpa_to_gfn(kvm_gpa_from_fault(kvm, ipa));
+}
+
 #endif /* __ARM64_KVM_EMULATE_H__ */
arch/arm64/include/asm/kvm_mmu.h +2 -2
index 32392abf2e21..a70f8bd4b2b9 100644
@@ -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);
arch/arm64/include/asm/kvm_rmi.h +6 -0
index 11e227172faf..698aa2cefc7b 100644
@@ -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>
 
@@ -88,6 +89,11 @@ void kvm_realm_unmap_range_filter(struct kvm *kvm,
 				  bool may_block,
 				  enum kvm_gfn_range_filter attr_filter);
 
+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);
+
 static inline bool kvm_realm_is_private_address(struct realm *realm,
 						unsigned long addr)
 {
arch/arm64/kvm/mmu.c +79 -17

214 lines not on this page · raw message

arch/arm64/kvm/rmi.c +251 -0

257 lines not on this page · raw message

Signature · 1 line
2.43.0
#35 lore raw

Re: [PATCH v3 09/11] KVM: arm64: CCA: Support runtime faulting of memory

Thank you for your contribution! Sashiko AI review found 3 potential issues to consider: - [Medium] KVM: arm64: KVM_CAP_PRE_FAULT_MEMORY advertised for Realm VMs - [Medium] KVM: arm64: missing unmap…

Thank you for your contribution! Sashiko AI review found 3 potential issues to consider:
- [Medium] KVM: arm64: KVM_CAP_PRE_FAULT_MEMORY advertised for Realm VMs
- [Medium] KVM: arm64: missing unmap on partial failure in realm_map_unprotected()
- [Low] KVM: arm64: unhandled exec fault on shared Realm IPA in gmem_abort() and user_mem_abort()
--

commit 5a0919ef6adc946a1afb4f6ad7cf84e5573597f3
Author: Steven Price <[email protected]>

KVM: arm64: CCA: Support runtime faulting of memory

This commit adds runtime stage-2 fault handling for Realm VMs, delegating and mapping private guest_memfd pages in the protected IPA region and mapping shared pages in the unprotected IPA region via the RMM.

Quoted 4 lines
arch/arm64/kvm/mmu.c +0 -0
index 0db3c293fdd52..9b371159be92c 100644

[ ... ]

Quoted 14 lines
arch/arm64/kvm/mmu.c +10 -0
@@ -1846,6 +1882,16 @@ static int gmem_abort(const struct kvm_s2_fault_desc *s2fd,
 		goto out_unlock;
 	}
 
+	switch (kvm->arch.vm_flavor) {
+	case VM_REALM:
+		prot &= ~KVM_PGTABLE_PROT_X;
+		ret = realm_map_ipa(kvm, s2fd->fault_ipa, s2vi.pfn,
+				    PAGE_SIZE, prot, memcache);
+		goto out_unlock;
+	default:
+		break;
+	}
+

[ ... ]

arch/arm64/kvm/mmu.c static int kvm_s2_fault_map(const struct kvm_s2_fault_desc *s2fd, · line 2294

Quoted 13 lines
arch/arm64/kvm/mmu.c +3 -6
@@ -2242,12 +2289,15 @@ static int kvm_s2_fault_map(const struct kvm_s2_fault_desc *s2fd,
 	if (!perm_fault_granule && !s2vi->map_non_cacheable && kvm_has_mte(kvm))
 		sanitise_mte_tags(kvm, pfn, mapping_size);
 
-	/*
-	 * Under the premise of getting a FSC_PERM fault, we just need to relax
-	 * permissions only if mapping_size equals perm_fault_granule. Otherwise,
-	 * kvm_pgtable_stage2_map() should be called to change block size.
-	 */
-	if (mapping_size == perm_fault_granule) {
+	if (kvm_vm_is_realm(kvm)) {
+		ret = realm_map_ipa(kvm, s2fd->fault_ipa, pfn, mapping_size,
+				    prot, memcache);

[Severity: Low]
This question rests on an architecture detail that could not be confirmed from the kernel source: can a Realm guest instruction fetch from an unprotected (shared) IPA exit to the host as a stage-2 instruction abort that reaches kvm_handle_guest_abort()?

If so, unprotected RTT mappings only encode RMI_S2AP_DIRECT_READ and RMI_S2AP_DIRECT_WRITE and cannot grant execute permission, but neither gmem_abort() nor user_mem_abort() rejects kvm_s2_fault_is_exec(s2fd) with -ENOEXEC to inject an instruction abort. Instead, realm_map_unprotected() installs a non-executable mapping, and on subsequent instruction permission faults at the same IPA where level >= req_level, unmaps and remaps the page with the same non-executable permissions and returns 0 in an endless fault loop.

[ ... ]

arch/arm64/kvm/mmu.c pre_fault_bytes_consumed(gpa_t gpa, unsigned long granule_size, · line 3223

Quoted 9 lines
arch/arm64/kvm/mmu.c +1 -1
@@ -3155,7 +3217,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 (is_protected_kvm_enabled() || kvm_vm_is_realm(vcpu->kvm))
 		return -EOPNOTSUPP;
 	if (!kvm_vcpu_initialized(vcpu))
 		return -ENOEXEC;

[Severity: Medium]
Should kvm_vm_ioctl_check_extension() also report 0 for KVM_CAP_PRE_FAULT_MEMORY when kvm && kvm_vm_is_realm(kvm)?

Currently kvm_vm_ioctl_check_extension() only gates capabilities on is_protected_kvm_enabled() and otherwise unconditionally sets r = 1 for KVM_CAP_PRE_FAULT_MEMORY, advertising the capability on a Realm VM file descriptor even though KVM_PRE_FAULT_MEMORY always fails here with -EOPNOTSUPP.

Quoted 4 lines
arch/arm64/kvm/rmi.c +0 -0
index 838f95c201ada..4cacb9fe000f4 100644

[ ... ]

Quoted 7 lines
arch/arm64/kvm/rmi.c +7 -0
+static int realm_map_unprotected(struct kvm *kvm,
+				 unsigned long ipa,
+				 kvm_pfn_t pfn,
+				 unsigned long size,
+				 enum kvm_pgtable_prot prot,
+				 struct kvm_mmu_memory_cache *memcache)
+{

[ ... ]

Quoted 39 lines
arch/arm64/kvm/rmi.c +39 -0
+	while (ipa < ipa_top) {
+		unsigned long range_desc = addr_range_desc(realm, ipa, phys,
+							   ipa_top - ipa,
+							   RMI_OP_MEM_UNDELEGATED);
+		unsigned long call_top = ipa + RMI_ADDR_RANGE_COUNT(range_desc) *
+			rmi_addr_block_size_to_bytes(RMI_ADDR_RANGE_BLOCK_SIZE(range_desc));
+		unsigned long out_top;
+
+		ret = rmi_rtt_unprot_map(rd, ipa, call_top, flags, range_desc,
+					 &out_top);
+		if (ret < 0)
+			return ret;
+
+		if (ret) {
+			/* Create missing RTTs and retry */
+			int level = RMI_RESULT_DATA_LEVEL(ret);
+			int req_level = KVM_PGTABLE_LAST_LEVEL -
+				RMI_ADDR_RANGE_BLOCK_SIZE(range_desc);
+
+			if (RMI_RESULT_STATUS(ret) != RMI_ERROR_RTT ||
+			    ++retries > KVM_PGTABLE_LAST_LEVEL + 2)
+				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;
+		}
+

arch/arm64/kvm/rmi.c void kvm_realm_unmap_range_filter(struct kvm *kvm, unsigned long start, · line 837

Quoted 10 lines
arch/arm64/kvm/rmi.c +10 -0
+		/* 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;
+		retries = 0;
+	}

[Severity: Medium]
Can a multi-iteration call to realm_map_unprotected() leave a writable prefix mapped when a later iteration fails or returns -EAGAIN?

For a Realm with a 32- to 34-bit IPA space on 4KB pages, get_start_level() is 2, so addr_range_desc() caps rmi_size at 1 (PMD_SIZE). When a shared memslot is backed by a 1GB hugetlbfs VMA, kvm_s2_fault_map() passes mapping_size = PUD_SIZE and this loop runs across multiple 2MB iterations. If an iteration after the first is interrupted (out_top == ipa) or hits an error, realm_map_unprotected() returns non-zero without unmapping the already-installed prefix (unlike realm_map_protected()). kvm_s2_fault_map() then passes !!ret (unused = true) to kvm_release_faultin_page() and skips mark_page_dirty_in_slot(), so if the faulting IPA lies in the mapped prefix, the guest can write to the page without marking the host folio dirty.

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

Re: [PATCH v3 09/11] KVM: arm64: CCA: Support runtime faulting of memory

Quoted 58 lines · On 16/11/2026 17:31, [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 3 potential issues to consider:
- [Medium] KVM: arm64: KVM_CAP_PRE_FAULT_MEMORY advertised for Realm VMs
- [Medium] KVM: arm64: missing unmap on partial failure in realm_map_unprotected()
- [Low] KVM: arm64: unhandled exec fault on shared Realm IPA in gmem_abort() and user_mem_abort()
--

commit 5a0919ef6adc946a1afb4f6ad7cf84e5573597f3
Author: Steven Price <[email protected]>

KVM: arm64: CCA: Support runtime faulting of memory

This commit adds runtime stage-2 fault handling for Realm VMs, delegating and mapping private guest_memfd pages in the protected IPA region and mapping shared pages in the unprotected IPA region via the RMM.

Quoted 4 lines
arch/arm64/kvm/mmu.c +0 -0
index 0db3c293fdd52..9b371159be92c 100644

[ ... ]

Quoted 14 lines
arch/arm64/kvm/mmu.c +10 -0
@@ -1846,6 +1882,16 @@ static int gmem_abort(const struct kvm_s2_fault_desc *s2fd,
  		goto out_unlock;
  	}
 
+	switch (kvm->arch.vm_flavor) {
+	case VM_REALM:
+		prot &= ~KVM_PGTABLE_PROT_X;
+		ret = realm_map_ipa(kvm, s2fd->fault_ipa, s2vi.pfn,
+				    PAGE_SIZE, prot, memcache);
+		goto out_unlock;
+	default:
+		break;
+	}
+

[ ... ]

arch/arm64/kvm/mmu.c static int kvm_s2_fault_map(const struct kvm_s2_fault_desc *s2fd, · line 2294

Quoted 13 lines
arch/arm64/kvm/mmu.c +3 -6
@@ -2242,12 +2289,15 @@ static int kvm_s2_fault_map(const struct kvm_s2_fault_desc *s2fd,
  	if (!perm_fault_granule && !s2vi->map_non_cacheable && kvm_has_mte(kvm))
  		sanitise_mte_tags(kvm, pfn, mapping_size);
 
-	/*
-	 * Under the premise of getting a FSC_PERM fault, we just need to relax
-	 * permissions only if mapping_size equals perm_fault_granule. Otherwise,
-	 * kvm_pgtable_stage2_map() should be called to change block size.
-	 */
-	if (mapping_size == perm_fault_granule) {
+	if (kvm_vm_is_realm(kvm)) {
+		ret = realm_map_ipa(kvm, s2fd->fault_ipa, pfn, mapping_size,
+				    prot, memcache);

[Severity: Low]
This question rests on an architecture detail that could not be confirmed from the kernel source: can a Realm guest instruction fetch from an unprotected (shared) IPA exit to the host as a stage-2 instruction abort that reaches kvm_handle_guest_abort()?

Quoted 8 lines

If so, unprotected RTT mappings only encode RMI_S2AP_DIRECT_READ and RMI_S2AP_DIRECT_WRITE and cannot grant execute permission, but neither gmem_abort() nor user_mem_abort() rejects kvm_s2_fault_is_exec(s2fd) with -ENOEXEC to inject an instruction abort. Instead, realm_map_unprotected() installs a non-executable mapping, and on subsequent instruction permission faults at the same IPA where level >= req_level, unmaps and remaps the page with the same non-executable permissions and returns 0 in an endless fault loop.

No, this is wrong. As per RMM spec, Rule XLSKP in A5.2.7 Realm access to an Unprotected IPA , version 2.0-beta3:

"Realm instruction fetch from an Unprotected IPA causes a Synchronous External Abort taken to the Realm."

So the execute permission fault do not come to the KVM.

We could a comment here to make it clear.

Quoted 17 lines

[ ... ]

arch/arm64/kvm/mmu.c pre_fault_bytes_consumed(gpa_t gpa, unsigned long granule_size, · line 3223

Quoted 9 lines
arch/arm64/kvm/mmu.c +1 -1
@@ -3155,7 +3217,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 (is_protected_kvm_enabled() || kvm_vm_is_realm(vcpu->kvm))
  		return -EOPNOTSUPP;
  	if (!kvm_vcpu_initialized(vcpu))
  		return -ENOEXEC;

[Severity: Medium]
Should kvm_vm_ioctl_check_extension() also report 0 for KVM_CAP_PRE_FAULT_MEMORY when kvm && kvm_vm_is_realm(kvm)?

Quoted 5 lines

Currently kvm_vm_ioctl_check_extension() only gates capabilities on is_protected_kvm_enabled() and otherwise unconditionally sets r = 1 for KVM_CAP_PRE_FAULT_MEMORY, advertising the capability on a Realm VM file descriptor even though KVM_PRE_FAULT_MEMORY always fails here with -EOPNOTSUPP.

This is taken care of in the later patches on the integration branch, where the capabilities are restricted for Realms.

Suzuki

#37 lore raw

Re: [PATCH v3 09/11] KVM: arm64: CCA: Support runtime faulting of memory

In reply to [email protected]
Quoted 58 lines · On 16/11/2026 17:31, [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 3 potential issues to consider:
- [Medium] KVM: arm64: KVM_CAP_PRE_FAULT_MEMORY advertised for Realm VMs
- [Medium] KVM: arm64: missing unmap on partial failure in realm_map_unprotected()
- [Low] KVM: arm64: unhandled exec fault on shared Realm IPA in gmem_abort() and user_mem_abort()
--

commit 5a0919ef6adc946a1afb4f6ad7cf84e5573597f3
Author: Steven Price <[email protected]>

KVM: arm64: CCA: Support runtime faulting of memory

This commit adds runtime stage-2 fault handling for Realm VMs, delegating and mapping private guest_memfd pages in the protected IPA region and mapping shared pages in the unprotected IPA region via the RMM.

Quoted 4 lines
arch/arm64/kvm/mmu.c +0 -0
index 0db3c293fdd52..9b371159be92c 100644

[ ... ]

Quoted 14 lines
arch/arm64/kvm/mmu.c +10 -0
@@ -1846,6 +1882,16 @@ static int gmem_abort(const struct kvm_s2_fault_desc *s2fd,
 		goto out_unlock;
 	}
 
+	switch (kvm->arch.vm_flavor) {
+	case VM_REALM:
+		prot &= ~KVM_PGTABLE_PROT_X;
+		ret = realm_map_ipa(kvm, s2fd->fault_ipa, s2vi.pfn,
+				    PAGE_SIZE, prot, memcache);
+		goto out_unlock;
+	default:
+		break;
+	}
+

[ ... ]

arch/arm64/kvm/mmu.c static int kvm_s2_fault_map(const struct kvm_s2_fault_desc *s2fd, · line 2294

Quoted 13 lines
arch/arm64/kvm/mmu.c +3 -6
@@ -2242,12 +2289,15 @@ static int kvm_s2_fault_map(const struct kvm_s2_fault_desc *s2fd,
 	if (!perm_fault_granule && !s2vi->map_non_cacheable && kvm_has_mte(kvm))
 		sanitise_mte_tags(kvm, pfn, mapping_size);
 
-	/*
-	 * Under the premise of getting a FSC_PERM fault, we just need to relax
-	 * permissions only if mapping_size equals perm_fault_granule. Otherwise,
-	 * kvm_pgtable_stage2_map() should be called to change block size.
-	 */
-	if (mapping_size == perm_fault_granule) {
+	if (kvm_vm_is_realm(kvm)) {
+		ret = realm_map_ipa(kvm, s2fd->fault_ipa, pfn, mapping_size,
+				    prot, memcache);

[Severity: Low]
This question rests on an architecture detail that could not be confirmed from the kernel source: can a Realm guest instruction fetch from an unprotected (shared) IPA exit to the host as a stage-2 instruction abort that reaches kvm_handle_guest_abort()?

Quoted 8 lines

If so, unprotected RTT mappings only encode RMI_S2AP_DIRECT_READ and RMI_S2AP_DIRECT_WRITE and cannot grant execute permission, but neither gmem_abort() nor user_mem_abort() rejects kvm_s2_fault_is_exec(s2fd) with -ENOEXEC to inject an instruction abort. Instead, realm_map_unprotected() installs a non-executable mapping, and on subsequent instruction permission faults at the same IPA where level >= req_level, unmaps and remaps the page with the same non-executable permissions and returns 0 in an endless fault loop.

Suzuki already covered this. I'll add a comment in the code.

Quoted 16 lines

[ ... ]

arch/arm64/kvm/mmu.c pre_fault_bytes_consumed(gpa_t gpa, unsigned long granule_size, · line 3223

Quoted 9 lines
arch/arm64/kvm/mmu.c +1 -1
@@ -3155,7 +3217,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 (is_protected_kvm_enabled() || kvm_vm_is_realm(vcpu->kvm))
 		return -EOPNOTSUPP;
 	if (!kvm_vcpu_initialized(vcpu))
 		return -ENOEXEC;

[Severity: Medium]
Should kvm_vm_ioctl_check_extension() also report 0 for KVM_CAP_PRE_FAULT_MEMORY when kvm && kvm_vm_is_realm(kvm)?

Quoted 5 lines

Currently kvm_vm_ioctl_check_extension() only gates capabilities on is_protected_kvm_enabled() and otherwise unconditionally sets r = 1 for KVM_CAP_PRE_FAULT_MEMORY, advertising the capability on a Realm VM file descriptor even though KVM_PRE_FAULT_MEMORY always fails here with -EOPNOTSUPP.

As Suzuki says, this is handled in later patches in the integration branch. I don't think we want to complicate matters by trying to pull it into this series.

Quoted 67 lines
Quoted 4 lines
arch/arm64/kvm/rmi.c +0 -0
index 838f95c201ada..4cacb9fe000f4 100644

[ ... ]

Quoted 7 lines
arch/arm64/kvm/rmi.c +7 -0
+static int realm_map_unprotected(struct kvm *kvm,
+				 unsigned long ipa,
+				 kvm_pfn_t pfn,
+				 unsigned long size,
+				 enum kvm_pgtable_prot prot,
+				 struct kvm_mmu_memory_cache *memcache)
+{

[ ... ]

Quoted 39 lines
arch/arm64/kvm/rmi.c +39 -0
+	while (ipa < ipa_top) {
+		unsigned long range_desc = addr_range_desc(realm, ipa, phys,
+							   ipa_top - ipa,
+							   RMI_OP_MEM_UNDELEGATED);
+		unsigned long call_top = ipa + RMI_ADDR_RANGE_COUNT(range_desc) *
+			rmi_addr_block_size_to_bytes(RMI_ADDR_RANGE_BLOCK_SIZE(range_desc));
+		unsigned long out_top;
+
+		ret = rmi_rtt_unprot_map(rd, ipa, call_top, flags, range_desc,
+					 &out_top);
+		if (ret < 0)
+			return ret;
+
+		if (ret) {
+			/* Create missing RTTs and retry */
+			int level = RMI_RESULT_DATA_LEVEL(ret);
+			int req_level = KVM_PGTABLE_LAST_LEVEL -
+				RMI_ADDR_RANGE_BLOCK_SIZE(range_desc);
+
+			if (RMI_RESULT_STATUS(ret) != RMI_ERROR_RTT ||
+			    ++retries > KVM_PGTABLE_LAST_LEVEL + 2)
+				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;
+		}
+

arch/arm64/kvm/rmi.c void kvm_realm_unmap_range_filter(struct kvm *kvm, unsigned long start, · line 837

Quoted 10 lines
arch/arm64/kvm/rmi.c +10 -0
+		/* 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;
+		retries = 0;
+	}

[Severity: Medium]
Can a multi-iteration call to realm_map_unprotected() leave a writable prefix mapped when a later iteration fails or returns -EAGAIN?

Quoted 11 lines

For a Realm with a 32- to 34-bit IPA space on 4KB pages, get_start_level() is 2, so addr_range_desc() caps rmi_size at 1 (PMD_SIZE). When a shared memslot is backed by a 1GB hugetlbfs VMA, kvm_s2_fault_map() passes mapping_size = PUD_SIZE and this loop runs across multiple 2MB iterations. If an iteration after the first is interrupted (out_top == ipa) or hits an error, realm_map_unprotected() returns non-zero without unmapping the already-installed prefix (unlike realm_map_protected()). kvm_s2_fault_map() then passes !!ret (unused = true) to kvm_release_faultin_page() and skips mark_page_dirty_in_slot(), so if the faulting IPA lies in the mapped prefix, the guest can write to the page without marking the host folio dirty.

Valid point, I'll unmap if things fail.

Thanks,
Steve

#38 lore raw

Re: [PATCH v3 09/11] KVM: arm64: CCA: Support runtime faulting of memory

In reply to Steven Price

Hi Steven,

On Mon, Nov 16, 2026 at 04:43:55PM +0000, Steven Price wrote:

[...]

arch/arm64/kvm/mmu.c static int gmem_abort(const struct kvm_s2_fault_desc *s2fd, · line 1805

Quoted 9 lines
arch/arm64/kvm/mmu.c +3 -1
@@ -1791,12 +1799,18 @@ static int gmem_abort(const struct kvm_s2_fault_desc *s2fd,
 	void *memcache = NULL;
 	gfn_t canonical_gfn;
 	int ret;
+	bool is_private = false;
 
-	if (!perm_fault) {
+	if (KVM_BUG_ON(!kvm_slot_has_gmem(s2fd->memslot), kvm))
+		return -EINVAL;

I feel this is being too cautious, gmem_abort is always run after a kvm_slot_has_gmem() check.

Quoted 135 lines
arch/arm64/kvm/mmu.c +53 -12
+
+	if (!perm_fault || kvm_arch_has_private_mem(kvm)) {
 		memcache = get_mmu_memcache(s2fd->vcpu);
 		ret = topup_mmu_memcache(s2fd->mmu, memcache);
 		if (ret)
 			return ret;
+	}
+	if (!perm_fault) {
 		if (kvm_is_nested_s2_mmu(kvm, pgt->mmu)) {
 			mapping = kmalloc_obj(struct kvm_guest_s2_mapping, GFP_KERNEL_ACCOUNT);
 			if (!mapping)
@@ -1805,7 +1819,7 @@ static int gmem_abort(const struct kvm_s2_fault_desc *s2fd,
 	}
 
 	s2vi.vma_pagesize = PAGE_SIZE;
-	s2vi.gfn = ALIGN_DOWN(s2fd->fault_ipa, s2vi.vma_pagesize) >> PAGE_SHIFT;
+	s2vi.gfn = gpa_to_gfn(kvm_gpa_from_fault(kvm, s2fd->fault_ipa));
 	canonical_gfn = get_canonical_gfn(s2fd, &s2vi);
 
 	write_fault = kvm_s2_fault_is_write(s2fd);
@@ -1817,13 +1831,35 @@ static int gmem_abort(const struct kvm_s2_fault_desc *s2fd,
 	/* Pairs with the smp_wmb() in kvm_mmu_invalidate_end(). */
 	smp_rmb();
 
+	if (kvm_arch_has_private_mem(kvm)) {
+		switch (kvm->arch.vm_flavor) {
+		case VM_REALM:
+			is_private = kvm_realm_is_private_address(&kvm->arch.realm,
+								  s2fd->fault_ipa);
+			break;
+		default:
+			break;
+		}
+
+		if (is_private != kvm_gmem_is_private_gfn(kvm, canonical_gfn)) {
+			if (!result)
+				kvm_prepare_memory_fault_exit(s2fd->vcpu,
+							      gfn_to_gpa(canonical_gfn),
+							      PAGE_SIZE,
+							      write_fault, exec_fault,
+							      is_private);
+			kfree(mapping);
+			return -EFAULT;
+		}
+	}
+
 	ret = kvm_gmem_get_pfn(kvm, s2fd->memslot, canonical_gfn, &s2vi.pfn, NULL);
 	if (ret) {
 		/* If result is non-NULL this is a synthetic fault. */
 		if (!result)
 			kvm_prepare_memory_fault_exit(s2fd->vcpu, gfn_to_gpa(canonical_gfn),
 						      s2vi.vma_pagesize, write_fault, exec_fault,
-						      false);
+						      is_private);
 		kfree(mapping);
 		return ret;
 	}
@@ -1846,6 +1882,16 @@ static int gmem_abort(const struct kvm_s2_fault_desc *s2fd,
 		goto out_unlock;
 	}
 
+	switch (kvm->arch.vm_flavor) {
+	case VM_REALM:
+		prot &= ~KVM_PGTABLE_PROT_X;
+		ret = realm_map_ipa(kvm, s2fd->fault_ipa, s2vi.pfn,
+				    PAGE_SIZE, prot, memcache);
+		goto out_unlock;
+	default:
+		break;
+	}
+
 	if (perm_fault) {
 		/*
 		 * Drop the SW bits in favour of those stored in the
@@ -2047,7 +2093,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);
 
@@ -2227,7 +2274,7 @@ static int kvm_s2_fault_map(const struct kvm_s2_fault_desc *s2fd,
 	 * possible.
 	 */
 	if (mapping_size == PAGE_SIZE && s2vi->max_map_size != PAGE_SIZE) {
-		if (perm_fault_granule > PAGE_SIZE) {
+		if (perm_fault_granule > PAGE_SIZE && !kvm_vm_is_realm(kvm)) {
 			mapping_size = perm_fault_granule;
 		} else {
 			mapping_size = transparent_hugepage_adjust(s2fd, &pfn, &gfn);
@@ -2242,12 +2289,15 @@ static int kvm_s2_fault_map(const struct kvm_s2_fault_desc *s2fd,
 	if (!perm_fault_granule && !s2vi->map_non_cacheable && kvm_has_mte(kvm))
 		sanitise_mte_tags(kvm, pfn, mapping_size);
 
-	/*
-	 * Under the premise of getting a FSC_PERM fault, we just need to relax
-	 * permissions only if mapping_size equals perm_fault_granule. Otherwise,
-	 * kvm_pgtable_stage2_map() should be called to change block size.
-	 */
-	if (mapping_size == perm_fault_granule) {
+	if (kvm_vm_is_realm(kvm)) {
+		ret = realm_map_ipa(kvm, s2fd->fault_ipa, pfn, mapping_size,
+				    prot, memcache);
+	} else if (mapping_size == perm_fault_granule) {
+		/*
+		 * Under the premise of getting a FSC_PERM fault, we just need to relax
+		 * permissions only if mapping_size equals perm_fault_granule. Otherwise,
+		 * kvm_pgtable_stage2_map() should be called to change block size.
+		 */
 		/*
 		 * Drop the SW bits in favour of those stored in the
 		 * PTE, which will be preserved.
@@ -2306,15 +2356,16 @@ 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.
 	 */
 	memcache = get_mmu_memcache(s2fd->vcpu);
 	if (!perm_fault || memslot_is_logging(s2fd->memslot) ||
-	    is_protected_kvm_enabled()) {
+	    is_protected_kvm_enabled() || kvm_vm_is_realm(s2fd->vcpu->kvm)) {
 		ret = topup_mmu_memcache(s2fd->mmu, memcache);
 		if (ret)
 			return ret;

arch/arm64/kvm/mmu.c static int user_mem_abort(const struct kvm_s2_fault_desc *s2fd, · line 2392

Quoted 2 lines
arch/arm64/kvm/mmu.c +0 -0
@@ -2340,12 +2391,22 @@ static int user_mem_abort(const struct kvm_s2_fault_desc *s2fd,
 	return kvm_s2_fault_map(s2fd, &s2vi, prot, memcache, result);
 }

I see that the changes to gmem_abort() and kvm_s2_fault_map() are more significant, and can possibly be turned into its realm version leaving the original unchanged. With this can we give realm_vm_s2_ops its own .vm_mem_abort? Something like:

First, make user_mem_abort() take a function pointer, call it at the current kvm_s2_fault_map() place.

Then realm_vm_s2_ops.vm_mem_abort = realm_mem_abort()

And in realm_mem_abort() do:

realm_mem_abort()
if gmem slot: realm_gmem_abort()
if regular slot: user_mem_abort(kvm_realm_fault_map)

realm_gmem_abort() and kvm_realm_fault_map() are just the realm version of gmem_abort() and kvm_s2_fault_map(). For example they won't need NV related code, and vm_flavor switch case.

Proposing this because I think we would like the CCA changes more self-contained.

Thanks,
Wei-Lin Chang

Quoted 52 lines
arch/arm64/kvm/mmu.c +14 -3
+static bool private_ipa_fault(struct kvm *kvm, phys_addr_t fault_ipa)
+{
+	return kvm_vm_is_realm(kvm) &&
+		kvm_realm_is_private_address(&kvm->arch.realm, fault_ipa);
+}
+
 static int kvm_vm_mem_abort(const struct kvm_s2_fault_desc *s2fd)
 {
 	VM_WARN_ON_ONCE(kvm_s2_fault_is_perm(s2fd) &&
 			!esr_abt_is_write_fault(s2fd->esr) &&
 			!kvm_s2_fault_is_exec(s2fd));
 
+	if (private_ipa_fault(s2fd->vcpu->kvm, s2fd->fault_ipa) &&
+	    !kvm_slot_has_gmem(s2fd->memslot))
+		return -EINVAL;
+
 	if (kvm_slot_has_gmem(s2fd->memslot))
 		return gmem_abort(s2fd, NULL);
 	else
@@ -2458,10 +2519,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;
@@ -2554,8 +2615,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)) {
@@ -2598,7 +2660,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;
 	}
 

[...]

#39 lore raw

Re: [PATCH v3 09/11] KVM: arm64: CCA: Support runtime faulting of memory

Quoted 12 lines · Wei-Lin Chang

Hi Steven,

On Mon, Nov 16, 2026 at 04:43:55PM +0000, Steven Price wrote:

[...]

Quoted 6 lines
arch/arm64/kvm/mmu.c +1 -0
@@ -1791,12 +1799,18 @@ static int gmem_abort(const struct kvm_s2_fault_desc *s2fd,
 	void *memcache = NULL;
 	gfn_t canonical_gfn;
 	int ret;
+	bool is_private = false;
 
Quoted 6 lines

arch/arm64/kvm/mmu.c static int gmem_abort(const struct kvm_s2_fault_desc *s2fd, · line 1805

Quoted 3 lines
arch/arm64/kvm/mmu.c +2 -1
-	if (!perm_fault) {
+	if (KVM_BUG_ON(!kvm_slot_has_gmem(s2fd->memslot), kvm))
+		return -EINVAL;

I feel this is being too cautious, gmem_abort is always run after a kvm_slot_has_gmem() check.

Fair point - I'll drop that.

Quoted 154 lines
Quoted 135 lines
arch/arm64/kvm/mmu.c +53 -12
+
+	if (!perm_fault || kvm_arch_has_private_mem(kvm)) {
 		memcache = get_mmu_memcache(s2fd->vcpu);
 		ret = topup_mmu_memcache(s2fd->mmu, memcache);
 		if (ret)
 			return ret;
+	}
+	if (!perm_fault) {
 		if (kvm_is_nested_s2_mmu(kvm, pgt->mmu)) {
 			mapping = kmalloc_obj(struct kvm_guest_s2_mapping, GFP_KERNEL_ACCOUNT);
 			if (!mapping)
@@ -1805,7 +1819,7 @@ static int gmem_abort(const struct kvm_s2_fault_desc *s2fd,
 	}
 
 	s2vi.vma_pagesize = PAGE_SIZE;
-	s2vi.gfn = ALIGN_DOWN(s2fd->fault_ipa, s2vi.vma_pagesize) >> PAGE_SHIFT;
+	s2vi.gfn = gpa_to_gfn(kvm_gpa_from_fault(kvm, s2fd->fault_ipa));
 	canonical_gfn = get_canonical_gfn(s2fd, &s2vi);
 
 	write_fault = kvm_s2_fault_is_write(s2fd);
@@ -1817,13 +1831,35 @@ static int gmem_abort(const struct kvm_s2_fault_desc *s2fd,
 	/* Pairs with the smp_wmb() in kvm_mmu_invalidate_end(). */
 	smp_rmb();
 
+	if (kvm_arch_has_private_mem(kvm)) {
+		switch (kvm->arch.vm_flavor) {
+		case VM_REALM:
+			is_private = kvm_realm_is_private_address(&kvm->arch.realm,
+								  s2fd->fault_ipa);
+			break;
+		default:
+			break;
+		}
+
+		if (is_private != kvm_gmem_is_private_gfn(kvm, canonical_gfn)) {
+			if (!result)
+				kvm_prepare_memory_fault_exit(s2fd->vcpu,
+							      gfn_to_gpa(canonical_gfn),
+							      PAGE_SIZE,
+							      write_fault, exec_fault,
+							      is_private);
+			kfree(mapping);
+			return -EFAULT;
+		}
+	}
+
 	ret = kvm_gmem_get_pfn(kvm, s2fd->memslot, canonical_gfn, &s2vi.pfn, NULL);
 	if (ret) {
 		/* If result is non-NULL this is a synthetic fault. */
 		if (!result)
 			kvm_prepare_memory_fault_exit(s2fd->vcpu, gfn_to_gpa(canonical_gfn),
 						      s2vi.vma_pagesize, write_fault, exec_fault,
-						      false);
+						      is_private);
 		kfree(mapping);
 		return ret;
 	}
@@ -1846,6 +1882,16 @@ static int gmem_abort(const struct kvm_s2_fault_desc *s2fd,
 		goto out_unlock;
 	}
 
+	switch (kvm->arch.vm_flavor) {
+	case VM_REALM:
+		prot &= ~KVM_PGTABLE_PROT_X;
+		ret = realm_map_ipa(kvm, s2fd->fault_ipa, s2vi.pfn,
+				    PAGE_SIZE, prot, memcache);
+		goto out_unlock;
+	default:
+		break;
+	}
+
 	if (perm_fault) {
 		/*
 		 * Drop the SW bits in favour of those stored in the
@@ -2047,7 +2093,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);
 
@@ -2227,7 +2274,7 @@ static int kvm_s2_fault_map(const struct kvm_s2_fault_desc *s2fd,
 	 * possible.
 	 */
 	if (mapping_size == PAGE_SIZE && s2vi->max_map_size != PAGE_SIZE) {
-		if (perm_fault_granule > PAGE_SIZE) {
+		if (perm_fault_granule > PAGE_SIZE && !kvm_vm_is_realm(kvm)) {
 			mapping_size = perm_fault_granule;
 		} else {
 			mapping_size = transparent_hugepage_adjust(s2fd, &pfn, &gfn);
@@ -2242,12 +2289,15 @@ static int kvm_s2_fault_map(const struct kvm_s2_fault_desc *s2fd,
 	if (!perm_fault_granule && !s2vi->map_non_cacheable && kvm_has_mte(kvm))
 		sanitise_mte_tags(kvm, pfn, mapping_size);
 
-	/*
-	 * Under the premise of getting a FSC_PERM fault, we just need to relax
-	 * permissions only if mapping_size equals perm_fault_granule. Otherwise,
-	 * kvm_pgtable_stage2_map() should be called to change block size.
-	 */
-	if (mapping_size == perm_fault_granule) {
+	if (kvm_vm_is_realm(kvm)) {
+		ret = realm_map_ipa(kvm, s2fd->fault_ipa, pfn, mapping_size,
+				    prot, memcache);
+	} else if (mapping_size == perm_fault_granule) {
+		/*
+		 * Under the premise of getting a FSC_PERM fault, we just need to relax
+		 * permissions only if mapping_size equals perm_fault_granule. Otherwise,
+		 * kvm_pgtable_stage2_map() should be called to change block size.
+		 */
 		/*
 		 * Drop the SW bits in favour of those stored in the
 		 * PTE, which will be preserved.
@@ -2306,15 +2356,16 @@ 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.
 	 */
 	memcache = get_mmu_memcache(s2fd->vcpu);
 	if (!perm_fault || memslot_is_logging(s2fd->memslot) ||
-	    is_protected_kvm_enabled()) {
+	    is_protected_kvm_enabled() || kvm_vm_is_realm(s2fd->vcpu->kvm)) {
 		ret = topup_mmu_memcache(s2fd->mmu, memcache);
 		if (ret)
 			return ret;

arch/arm64/kvm/mmu.c static int user_mem_abort(const struct kvm_s2_fault_desc *s2fd, · line 2392

Quoted 2 lines
arch/arm64/kvm/mmu.c +0 -0
@@ -2340,12 +2391,22 @@ static int user_mem_abort(const struct kvm_s2_fault_desc *s2fd,
 	return kvm_s2_fault_map(s2fd, &s2vi, prot, memcache, result);
 }

I see that the changes to gmem_abort() and kvm_s2_fault_map() are more significant, and can possibly be turned into its realm version leaving the original unchanged. With this can we give realm_vm_s2_ops its own .vm_mem_abort? Something like:

First, make user_mem_abort() take a function pointer, call it at the current kvm_s2_fault_map() place.

Then realm_vm_s2_ops.vm_mem_abort = realm_mem_abort()

And in realm_mem_abort() do:

realm_mem_abort()
if gmem slot: realm_gmem_abort()
if regular slot: user_mem_abort(kvm_realm_fault_map)

Quoted 6 lines

realm_gmem_abort() and kvm_realm_fault_map() are just the realm version of gmem_abort() and kvm_s2_fault_map(). For example they won't need NV related code, and vm_flavor switch case.

Proposing this because I think we would like the CCA changes more self-contained.

It's a little marginal - there's quite a bit of common code between the realm path and the normal path. But it does have the advantage that you mention of making CCA more self contained. So it's probably worth it.

Thanks,
Steve

Quoted 57 lines

Thanks,
Wei-Lin Chang

Quoted 52 lines
arch/arm64/kvm/mmu.c +14 -3
+static bool private_ipa_fault(struct kvm *kvm, phys_addr_t fault_ipa)
+{
+	return kvm_vm_is_realm(kvm) &&
+		kvm_realm_is_private_address(&kvm->arch.realm, fault_ipa);
+}
+
 static int kvm_vm_mem_abort(const struct kvm_s2_fault_desc *s2fd)
 {
 	VM_WARN_ON_ONCE(kvm_s2_fault_is_perm(s2fd) &&
 			!esr_abt_is_write_fault(s2fd->esr) &&
 			!kvm_s2_fault_is_exec(s2fd));
 
+	if (private_ipa_fault(s2fd->vcpu->kvm, s2fd->fault_ipa) &&
+	    !kvm_slot_has_gmem(s2fd->memslot))
+		return -EINVAL;
+
 	if (kvm_slot_has_gmem(s2fd->memslot))
 		return gmem_abort(s2fd, NULL);
 	else
@@ -2458,10 +2519,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;
@@ -2554,8 +2615,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)) {
@@ -2598,7 +2660,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;
 	}
 

[...]

#40 lore raw

Re: [PATCH v3 09/11] KVM: arm64: CCA: Support runtime faulting of memory

Quoted 180 lines · Steven Price
Quoted 12 lines · Wei-Lin Chang

Hi Steven,

On Mon, Nov 16, 2026 at 04:43:55PM +0000, Steven Price wrote:

[...]

Quoted 6 lines
arch/arm64/kvm/mmu.c +1 -0
@@ -1791,12 +1799,18 @@ static int gmem_abort(const struct kvm_s2_fault_desc *s2fd,
  	void *memcache = NULL;
  	gfn_t canonical_gfn;
  	int ret;
+	bool is_private = false;
 
Quoted 6 lines

arch/arm64/kvm/mmu.c static int gmem_abort(const struct kvm_s2_fault_desc *s2fd, · line 1805

Quoted 3 lines
arch/arm64/kvm/mmu.c +2 -1
-	if (!perm_fault) {
+	if (KVM_BUG_ON(!kvm_slot_has_gmem(s2fd->memslot), kvm))
+		return -EINVAL;

I feel this is being too cautious, gmem_abort is always run after a kvm_slot_has_gmem() check.

Fair point - I'll drop that.

Quoted 158 lines
Quoted 135 lines
arch/arm64/kvm/mmu.c +53 -12
+
+	if (!perm_fault || kvm_arch_has_private_mem(kvm)) {
  		memcache = get_mmu_memcache(s2fd->vcpu);
  		ret = topup_mmu_memcache(s2fd->mmu, memcache);
  		if (ret)
  			return ret;
+	}
+	if (!perm_fault) {
  		if (kvm_is_nested_s2_mmu(kvm, pgt->mmu)) {
  			mapping = kmalloc_obj(struct kvm_guest_s2_mapping, GFP_KERNEL_ACCOUNT);
  			if (!mapping)
@@ -1805,7 +1819,7 @@ static int gmem_abort(const struct kvm_s2_fault_desc *s2fd,
  	}
 
  	s2vi.vma_pagesize = PAGE_SIZE;
-	s2vi.gfn = ALIGN_DOWN(s2fd->fault_ipa, s2vi.vma_pagesize) >> PAGE_SHIFT;
+	s2vi.gfn = gpa_to_gfn(kvm_gpa_from_fault(kvm, s2fd->fault_ipa));
  	canonical_gfn = get_canonical_gfn(s2fd, &s2vi);
 
  	write_fault = kvm_s2_fault_is_write(s2fd);
@@ -1817,13 +1831,35 @@ static int gmem_abort(const struct kvm_s2_fault_desc *s2fd,
  	/* Pairs with the smp_wmb() in kvm_mmu_invalidate_end(). */
  	smp_rmb();
 
+	if (kvm_arch_has_private_mem(kvm)) {
+		switch (kvm->arch.vm_flavor) {
+		case VM_REALM:
+			is_private = kvm_realm_is_private_address(&kvm->arch.realm,
+								  s2fd->fault_ipa);
+			break;
+		default:
+			break;
+		}
+
+		if (is_private != kvm_gmem_is_private_gfn(kvm, canonical_gfn)) {
+			if (!result)
+				kvm_prepare_memory_fault_exit(s2fd->vcpu,
+							      gfn_to_gpa(canonical_gfn),
+							      PAGE_SIZE,
+							      write_fault, exec_fault,
+							      is_private);
+			kfree(mapping);
+			return -EFAULT;
+		}
+	}
+
  	ret = kvm_gmem_get_pfn(kvm, s2fd->memslot, canonical_gfn, &s2vi.pfn, NULL);
  	if (ret) {
  		/* If result is non-NULL this is a synthetic fault. */
  		if (!result)
  			kvm_prepare_memory_fault_exit(s2fd->vcpu, gfn_to_gpa(canonical_gfn),
  						      s2vi.vma_pagesize, write_fault, exec_fault,
-						      false);
+						      is_private);
  		kfree(mapping);
  		return ret;
  	}
@@ -1846,6 +1882,16 @@ static int gmem_abort(const struct kvm_s2_fault_desc *s2fd,
  		goto out_unlock;
  	}
 
+	switch (kvm->arch.vm_flavor) {
+	case VM_REALM:
+		prot &= ~KVM_PGTABLE_PROT_X;
+		ret = realm_map_ipa(kvm, s2fd->fault_ipa, s2vi.pfn,
+				    PAGE_SIZE, prot, memcache);
+		goto out_unlock;
+	default:
+		break;
+	}
+
  	if (perm_fault) {
  		/*
  		 * Drop the SW bits in favour of those stored in the
@@ -2047,7 +2093,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);
 
@@ -2227,7 +2274,7 @@ static int kvm_s2_fault_map(const struct kvm_s2_fault_desc *s2fd,
  	 * possible.
  	 */
  	if (mapping_size == PAGE_SIZE && s2vi->max_map_size != PAGE_SIZE) {
-		if (perm_fault_granule > PAGE_SIZE) {
+		if (perm_fault_granule > PAGE_SIZE && !kvm_vm_is_realm(kvm)) {
  			mapping_size = perm_fault_granule;
  		} else {
  			mapping_size = transparent_hugepage_adjust(s2fd, &pfn, &gfn);
@@ -2242,12 +2289,15 @@ static int kvm_s2_fault_map(const struct kvm_s2_fault_desc *s2fd,
  	if (!perm_fault_granule && !s2vi->map_non_cacheable && kvm_has_mte(kvm))
  		sanitise_mte_tags(kvm, pfn, mapping_size);
 
-	/*
-	 * Under the premise of getting a FSC_PERM fault, we just need to relax
-	 * permissions only if mapping_size equals perm_fault_granule. Otherwise,
-	 * kvm_pgtable_stage2_map() should be called to change block size.
-	 */
-	if (mapping_size == perm_fault_granule) {
+	if (kvm_vm_is_realm(kvm)) {
+		ret = realm_map_ipa(kvm, s2fd->fault_ipa, pfn, mapping_size,
+				    prot, memcache);
+	} else if (mapping_size == perm_fault_granule) {
+		/*
+		 * Under the premise of getting a FSC_PERM fault, we just need to relax
+		 * permissions only if mapping_size equals perm_fault_granule. Otherwise,
+		 * kvm_pgtable_stage2_map() should be called to change block size.
+		 */
  		/*
  		 * Drop the SW bits in favour of those stored in the
  		 * PTE, which will be preserved.
@@ -2306,15 +2356,16 @@ 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.
  	 */
  	memcache = get_mmu_memcache(s2fd->vcpu);
  	if (!perm_fault || memslot_is_logging(s2fd->memslot) ||
-	    is_protected_kvm_enabled()) {
+	    is_protected_kvm_enabled() || kvm_vm_is_realm(s2fd->vcpu->kvm)) {
  		ret = topup_mmu_memcache(s2fd->mmu, memcache);
  		if (ret)
  			return ret;

arch/arm64/kvm/mmu.c static int user_mem_abort(const struct kvm_s2_fault_desc *s2fd, · line 2392

Quoted 2 lines
arch/arm64/kvm/mmu.c +0 -0
@@ -2340,12 +2391,22 @@ static int user_mem_abort(const struct kvm_s2_fault_desc *s2fd,
  	return kvm_s2_fault_map(s2fd, &s2vi, prot, memcache, result);
  }

I see that the changes to gmem_abort() and kvm_s2_fault_map() are more significant, and can possibly be turned into its realm version leaving the original unchanged. With this can we give realm_vm_s2_ops its own .vm_mem_abort? Something like:

First, make user_mem_abort() take a function pointer, call it at the current kvm_s2_fault_map() place.

Then realm_vm_s2_ops.vm_mem_abort = realm_mem_abort()

And in realm_mem_abort() do:

realm_mem_abort()
if gmem slot: realm_gmem_abort()
if regular slot: user_mem_abort(kvm_realm_fault_map)

realm_gmem_abort() and kvm_realm_fault_map() are just the realm version of gmem_abort() and kvm_s2_fault_map(). For example they won't need NV related code, and vm_flavor switch case.

Quoted 6 lines
Quoted 2 lines

Proposing this because I think we would like the CCA changes more self-contained.

It's a little marginal - there's quite a bit of common code between the realm path and the normal path. But it does have the advantage that you mention of making CCA more self contained. So it's probably worth it.

The only odd bit would be handling the permission fault separately for Realms ?

Suzuki

Quoted 62 lines

Thanks,
Steve

Quoted 57 lines

Thanks,
Wei-Lin Chang

Quoted 52 lines
arch/arm64/kvm/mmu.c +14 -3
+static bool private_ipa_fault(struct kvm *kvm, phys_addr_t fault_ipa)
+{
+	return kvm_vm_is_realm(kvm) &&
+		kvm_realm_is_private_address(&kvm->arch.realm, fault_ipa);
+}
+
  static int kvm_vm_mem_abort(const struct kvm_s2_fault_desc *s2fd)
  {
  	VM_WARN_ON_ONCE(kvm_s2_fault_is_perm(s2fd) &&
  			!esr_abt_is_write_fault(s2fd->esr) &&
  			!kvm_s2_fault_is_exec(s2fd));
 
+	if (private_ipa_fault(s2fd->vcpu->kvm, s2fd->fault_ipa) &&
+	    !kvm_slot_has_gmem(s2fd->memslot))
+		return -EINVAL;
+
  	if (kvm_slot_has_gmem(s2fd->memslot))
  		return gmem_abort(s2fd, NULL);
  	else
@@ -2458,10 +2519,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;
@@ -2554,8 +2615,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)) {
@@ -2598,7 +2660,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;
  	}
 

[...]

#41 lore raw

PATCH v3 10/11 KVM: arm64: Dispatch stage-2 destruction by VM flavor

In reply to Steven Price

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.

The Realm callback will only free the root RTT pages, because RMI teardown destroys the non-root RTTs separately.

---
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 |  1 +
 arch/arm64/kvm/mmu.c              | 37 ++++++++++++++++++-------------
 2 files changed, 23 insertions(+), 15 deletions(-)
arch/arm64/include/asm/kvm_host.h +1 -0
index 2c8cd9a1dd52..52ccb8b4ccb2 100644
@@ -242,6 +242,7 @@ struct kvm_vm_s2_ops {
 	int (*vm_flush_remote_tlbs)(struct kvm *kvm);
 	int (*vm_flush_remote_tlbs_range)(struct kvm *kvm, gfn_t gfn,
 					  u64 nr_pages);
+	void (*vm_stage2_destroy)(struct kvm_pgtable *pgt);
 	void (*vm_stage2_unmap_range)(struct kvm_s2_mmu *mmu,
 				      phys_addr_t start, u64 size,
 				      bool may_block,
arch/arm64/kvm/mmu.c +22 -15
index 9b371159be92..46a83c58c12f 100644
@@ -1026,35 +1026,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)
 {
 	struct kvm *kvm = kvm_s2_mmu_to_kvm(pgt->mmu);
-	unsigned int ia_bits = VTCR_EL2_IPA(pgt->mmu->vtcr);
-
-	/*
-	 * 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.
-	 */
-	if (!kvm_vm_is_realm(kvm))
-		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);
 }
 
 /**
@@ -3267,6 +3271,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,
@@ -3276,6 +3281,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,
@@ -3285,6 +3291,7 @@ static const struct kvm_vm_s2_ops pkvm_vm_s2_ops = {
 };
 
 static const struct kvm_vm_s2_ops kvm_default_vm_s2_ops = {
+	.vm_stage2_destroy		= kvm_vm_stage2_destroy,
 	.vm_flush_remote_tlbs		= kvm_vm_flush_remote_tlbs,
 	.vm_flush_remote_tlbs_range	= kvm_vm_flush_remote_tlbs_range,
 	.vm_age_gfn			= kvm_vm_age_gfn,
Signature · 1 line
2.43.0
#42 lore raw

Re: [PATCH v3 10/11] KVM: arm64: Dispatch stage-2 destruction by VM flavor

Hi Steven,

On Mon, 16 Nov 2026 16:43:56 +0000, Steven Price <[email protected]> wrote:
[...]

Quoted 1 line
arch/arm64/kvm/mmu.c +0 -0

[...]

arch/arm64/kvm/mmu.c static int kvm_init_ipa_range(struct kvm_s2_mmu *mmu, unsigned long type) · line 1055

Quoted 13 lines

static void kvm_stage2_destroy(struct kvm_pgtable *pgt)

 {
         struct kvm *kvm = kvm_s2_mmu_to_kvm(pgt->mmu);
arch/arm64/kvm/mmu.c +0 -10
-        unsigned int ia_bits = VTCR_EL2_IPA(pgt->mmu->vtcr);
-
-        /*
-         * 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.
-         */
-        if (!kvm_vm_is_realm(kvm))
-                stage2_destroy_range(pgt, 0, BIT(ia_bits));

Could this patch go before patch 4? Then patch 4 wouldn't need to add this check.

Cheers,
/fuad

#43 lore raw

Re: [PATCH v3 10/11] KVM: arm64: Dispatch stage-2 destruction by VM flavor

Quoted 20 lines · Fuad Tabba

Hi Steven,

On Mon, 16 Nov 2026 16:43:56 +0000, Steven Price <[email protected]> wrote:
[...]

Quoted 1 line
arch/arm64/kvm/mmu.c +0 -0

[...]

arch/arm64/kvm/mmu.c static int kvm_init_ipa_range(struct kvm_s2_mmu *mmu, unsigned long type) · line 1055

Quoted 13 lines

static void kvm_stage2_destroy(struct kvm_pgtable *pgt)

 {
         struct kvm *kvm = kvm_s2_mmu_to_kvm(pgt->mmu);
arch/arm64/kvm/mmu.c +0 -10
-        unsigned int ia_bits = VTCR_EL2_IPA(pgt->mmu->vtcr);
-
-        /*
-         * 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.
-         */
-        if (!kvm_vm_is_realm(kvm))
-                stage2_destroy_range(pgt, 0, BIT(ia_bits));
Quoted 2 lines

Could this patch go before patch 4? Then patch 4 wouldn't need to add this check.

Yes, that's a good idea - I'll make that change.

Thanks,
Steve

#44 lore raw

PATCH v3 11/11 KVM: arm64: CCA: Add Realm stage-2 MMU operations

In reply to Steven Price

From: Suzuki K Poulose <[email protected]>

Register the Realm stage-2 MMU operations now that the RMM mapping and unmapping helpers are available. Route unmapping through the RMM, and use the existing fault handler for Realm memory faults.

The RMM handles TLB invalidation when stage-2 mappings change, so the host has no separate TLB flush to perform. Realm stage-2 does not support access flags, including for shared IPAs.

Realm teardown destroys non-root RTTs through RMI, so the Realm stage-2 destruction callback frees only the root RTT pages.

---
Changes since v2:
- Move common stage-2 destruction dispatch to the preceding patch; retain the Realm-specific destruction callback here.

---
 arch/arm64/kvm/mmu.c | 45 +++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 44 insertions(+), 1 deletion(-)
arch/arm64/kvm/mmu.c +44 -1
index 46a83c58c12f..b3fa03d411fb 100644
@@ -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);
@@ -3300,6 +3333,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			= kvm_vm_mem_abort,
+};
+
 #define KVM_VM_S2_OPS(flavor, ops)		\
 		[(flavor)] = &(ops)
 
@@ -3308,7 +3351,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)
Signature · 1 line
2.43.0