Skip to contentSkip to conversation

KVM: arm64: CCA: Prepare Realm memory management

PATCH Superseded Steven Price 5 participants 39 messages Nov 11 – Nov 17

Subsystems (4)

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

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

This series adds the memory management plumbing for CCA, building on the basic RMM support [1]. Thank you to those who reviewed v1 - I'm attempted to address all the comments.

The patches introduce internal Realm VM state and RMI feature checks, 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.

As before there's an integration branch[2] that includes the realm creation, REC entry/exit etc building on top of this series. I've also pushed a git branch[3] of just this series.

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/v2/integration
[3] https://gitlab.arm.com/linux-arm/linux-cca
branch: cca-host-mm/v2/mm

Steven Price (7):
KVM: arm64: CCA: Reserve the RMM mode
KVM: arm64: guest_memfd: Check mmap support when preparing memslot
KVM: arm64: CCA: Check RMI support and features at init
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

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      |  53 +-
 arch/arm64/include/asm/kvm_mmu.h       |   4 +-
 arch/arm64/include/asm/kvm_rmi.h       | 104 +++
 arch/arm64/include/asm/virt.h          |   1 +
 arch/arm64/kvm/Kconfig                 |   1 +
 arch/arm64/kvm/Makefile                |   2 +-
 arch/arm64/kvm/arm.c                   |  21 +-
 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                   | 311 +++++++--
 arch/arm64/kvm/pkvm.c                  |   4 +-
 arch/arm64/kvm/psci.c                  |   2 +-
 arch/arm64/kvm/rmi.c                   | 883 +++++++++++++++++++++++++
 drivers/firmware/arm_rmm/rmi.c         |  10 -
 include/linux/arm-rmi-cmds.h           |  13 +
 include/linux/kvm_host.h               |   5 +
 virt/kvm/guest_memfd.c                 |   6 +
 virt/kvm/kvm_main.c                    |  33 +-
 20 files changed, 1391 insertions(+), 99 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 v2 1/12 KVM: arm64: CCA: Reserve the RMM mode

Reserve KVM_MODE_RMM for CCA realm support. The kernel parameter that selects this mode will be added when the Linux CCA implementation is ready.

---
 arch/arm64/include/asm/kvm_host.h | 1 +
 1 file changed, 1 insertion(+)
arch/arm64/include/asm/kvm_host.h +1 -0
index 97332ffcb130..ccd2dc8f73c3 100644
@@ -120,6 +120,7 @@ enum kvm_mode {
 	KVM_MODE_DEFAULT,
 	KVM_MODE_PROTECTED,
 	KVM_MODE_NV,
+	KVM_MODE_RMM,
 	KVM_MODE_NONE,
 };
 #ifdef CONFIG_KVM
Signature · 1 line
2.43.0
#2 lore raw

PATCH v2 2/12 KVM: arm64: guest_memfd: Check mmap support when preparing memslot

In reply to 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.

---
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     | 7 ++++---
 include/linux/kvm_host.h | 1 +
 virt/kvm/guest_memfd.c   | 6 ++++++
 3 files changed, 11 insertions(+), 3 deletions(-)
arch/arm64/kvm/mmu.c +4 -3
index 1db10d8d6db7..a146f6cde3d0 100644
@@ -2839,10 +2839,11 @@ 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.
+	 * Protected VMs currently require guest_memfd backed memslots with
+	 * mappable memory to support shared mappings.
 	 */
-	if (kvm_slot_has_gmem(new) && !kvm_memslot_is_gmem_only(new))
+	if (kvm_arch_has_private_mem(kvm) && 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
#3 lore raw

Re: [PATCH v2 02/12] KVM: arm64: guest_memfd: Check mmap support when preparing memslot

Quoted 5 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.

Please could you explain why we need this ? Why not rely on
kvm_memslot_is_gmem_only(), which already tells kvm_gmem_supports_mmap() ? With inplace conversion support gmem supports mmap.

Quoted 48 lines

---
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     | 7 ++++---
  include/linux/kvm_host.h | 1 +
  virt/kvm/guest_memfd.c   | 6 ++++++
  3 files changed, 11 insertions(+), 3 deletions(-)
arch/arm64/kvm/mmu.c +4 -3
index 1db10d8d6db7..a146f6cde3d0 100644
@@ -2839,10 +2839,11 @@ 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.
+	 * Protected VMs currently require guest_memfd backed memslots with
+	 * mappable memory to support shared mappings.
  	 */
-	if (kvm_slot_has_gmem(new) && !kvm_memslot_is_gmem_only(new))
+	if (kvm_arch_has_private_mem(kvm) && 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 +0 -0
index 8c97a69b16dc..3dd8d227bebb 100644

virt/kvm/guest_memfd.c static bool kvm_gmem_supports_mmap(struct inode *inode) · line 447

Quoted 6 lines
virt/kvm/guest_memfd.c +3 -0
@@ -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. */

We should gate this on kvm_slot_has_gmem() and how do we ensure that the call comes after it is prepared ?

virt/kvm/guest_memfd.c static bool kvm_gmem_supports_mmap(struct inode *inode) · line 453

Quoted 6 lines
virt/kvm/guest_memfd.c +3 -0
+	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);

Cheers
Suzuki

#4 lore raw

Re: [PATCH v2 02/12] KVM: arm64: guest_memfd: Check mmap support when preparing memslot

Quoted 11 lines · Suzuki K Poulose
Quoted 5 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.

Please could you explain why we need this ? Why not rely on kvm_memslot_is_gmem_only(), which already tells kvm_gmem_supports_mmap() ? With inplace conversion support gmem supports mmap.

Unless I'm missing something it's still possible to create a guest_memfd that doesn't support mmap() by not passing the GUEST_MEMFD_FLAG_MMAP flag. So kvm_memslot_is_gmem_only() doesn't actually guarantee that it's a guest_memfd that supports mmap().

Quoted 23 lines
Quoted 23 lines

---
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     | 7 ++++--- include/linux/kvm_host.h | 1 +
  virt/kvm/guest_memfd.c   | 6 ++++++

  3 files changed, 11 insertions(+), 3 deletions(-)
arch/arm64/kvm/mmu.c +0 -1
index 1db10d8d6db7..a146f6cde3d0 100644
@@ -2839,10 +2839,11 @@ int kvm_arch_prepare_memory_region(struct kvm
 *kvm,
           return -EFAULT;
         /*
-     * Only support guest_memfd backed memslots with mappable memory,

since

Quoted 8 lines
Quoted 8 lines

-     * there aren't any CoCo VMs that support only private memory on arm64.
+     * Protected VMs currently require guest_memfd backed memslots with +     * mappable memory to support shared mappings.
       */
-    if (kvm_slot_has_gmem(new) && !kvm_memslot_is_gmem_only(new)) +    if (kvm_arch_has_private_mem(kvm) && kvm_slot_has_gmem(new) && +        !kvm_gmem_slot_supports_mmap(new))

Sashiko has identified I've screwed up this condition though. kvm_arch_has_private_mem(kvm) shouldn't be in there as that allows a non-private VM to bind a non-mmappable gmem.

Quoted 19 lines
Quoted 19 lines

          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 +0 -0
index 8c97a69b16dc..3dd8d227bebb 100644
@@ -442,6 +442,12 @@ static bool kvm_gmem_supports_mmap(struct inode
 *inode)
Quoted 8 lines

virt/kvm/guest_memfd.c static bool kvm_gmem_supports_mmap(struct inode *inode) · line 447

Quoted 5 lines
virt/kvm/guest_memfd.c +2 -0
       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. */

We should gate this on kvm_slot_has_gmem() and how do we ensure that the call comes after it is prepared ?

I'm not sure what you mean by "gate this on kvm_slot_has_gmem()" - yes this function only makes sense on a gmem slot - the user above checks that first. I'm not sure what value the function should call if you call it on a non-gmem slot?

Thanks,
Steve

Quoted 10 lines
Quoted 6 lines
virt/kvm/guest_memfd.c +3 -0
+    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);

Cheers
Suzuki

#5 lore raw

Re: [PATCH v2 02/12] KVM: arm64: guest_memfd: Check mmap support when preparing memslot

Quoted 13 lines · Steven Price
Quoted 11 lines · Suzuki K Poulose
Quoted 5 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.

Please could you explain why we need this ? Why not rely on kvm_memslot_is_gmem_only(), which already tells kvm_gmem_supports_mmap() ? With inplace conversion support gmem supports mmap.

Quoted 4 lines

Unless I'm missing something it's still possible to create a guest_memfd that doesn't support mmap() by not passing the GUEST_MEMFD_FLAG_MMAP flag. So kvm_memslot_is_gmem_only() doesn't actually guarantee that it's a guest_memfd that supports mmap().

I see, while the KVM decides that, with inplace conversion shared pages come from gmem, we do allow creation of gmem without MMAP set, implying we would fail the mmap request from VMM, which is pretty pointless. But should the KVM fail this when VMM is shooting on its foot ? Only makes sense if there are no untrusted device DMA for the CoCo Guest.

Am I missing something ?

Cheers
Suzuki

Quoted 84 lines
Quoted 24 lines
Quoted 24 lines

---
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     | 7 ++++---
   include/linux/kvm_host.h | 1 +
   virt/kvm/guest_memfd.c   | 6 ++++++
   3 files changed, 11 insertions(+), 3 deletions(-)
arch/arm64/kvm/mmu.c +0 -1
index 1db10d8d6db7..a146f6cde3d0 100644
@@ -2839,10 +2839,11 @@ int kvm_arch_prepare_memory_region(struct kvm
 *kvm,
           return -EFAULT;
         /*
-     * Only support guest_memfd backed memslots with mappable memory,

since

Quoted 8 lines
Quoted 8 lines

-     * there aren't any CoCo VMs that support only private memory on arm64.
+     * Protected VMs currently require guest_memfd backed memslots with +     * mappable memory to support shared mappings.
       */
-    if (kvm_slot_has_gmem(new) && !kvm_memslot_is_gmem_only(new)) +    if (kvm_arch_has_private_mem(kvm) && kvm_slot_has_gmem(new) && +        !kvm_gmem_slot_supports_mmap(new))

Sashiko has identified I've screwed up this condition though. kvm_arch_has_private_mem(kvm) shouldn't be in there as that allows a non-private VM to bind a non-mmappable gmem.

Quoted 19 lines
Quoted 19 lines

          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 +0 -0
index 8c97a69b16dc..3dd8d227bebb 100644
@@ -442,6 +442,12 @@ static bool kvm_gmem_supports_mmap(struct inode
 *inode)
Quoted 8 lines

virt/kvm/guest_memfd.c static bool kvm_gmem_supports_mmap(struct inode *inode) · line 447

Quoted 5 lines
virt/kvm/guest_memfd.c +2 -0
       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. */

We should gate this on kvm_slot_has_gmem() and how do we ensure that the call comes after it is prepared ?

I'm not sure what you mean by "gate this on kvm_slot_has_gmem()" - yes this function only makes sense on a gmem slot - the user above checks that first. I'm not sure what value the function should call if you call it on a non-gmem slot?

Thanks,
Steve

Quoted 10 lines

virt/kvm/guest_memfd.c static bool kvm_gmem_supports_mmap(struct inode *inode) · line 453

Quoted 6 lines
virt/kvm/guest_memfd.c +3 -0
+    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);

Cheers
Suzuki

#6 lore raw

Re: [PATCH v2 02/12] KVM: arm64: guest_memfd: Check mmap support when preparing memslot

Quoted 19 lines · Suzuki K Poulose
Quoted 13 lines · Steven Price
Quoted 11 lines · Suzuki K Poulose
Quoted 5 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.

Please could you explain why we need this ? Why not rely on kvm_memslot_is_gmem_only(), which already tells kvm_gmem_supports_mmap() ? With inplace conversion support gmem supports mmap.

Quoted 4 lines

Unless I'm missing something it's still possible to create a guest_memfd that doesn't support mmap() by not passing the GUEST_MEMFD_FLAG_MMAP flag. So kvm_memslot_is_gmem_only() doesn't actually guarantee that it's a guest_memfd that supports mmap().

Quoted 5 lines

I see, while the KVM decides that, with inplace conversion shared pages come from gmem, we do allow creation of gmem without MMAP set, implying we would fail the mmap request from VMM, which is pretty pointless. But should the KVM fail this when VMM is shooting on its foot ? Only makes sense if there are no untrusted device DMA for the CoCo Guest.

See Documentation/virt/kvm/api.rst, under section : KVM_CREATE_GUEST_MEMFD

"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)."

Cheers
Suzuki

Quoted 96 lines

Am I missing something ?

Cheers
Suzuki

Quoted 86 lines
Quoted 25 lines
Quoted 25 lines

---
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     | 7 ++++--- include/linux/kvm_host.h | 1 +
   virt/kvm/guest_memfd.c   | 6 ++++++

   3 files changed, 11 insertions(+), 3 deletions(-)
arch/arm64/kvm/mmu.c +0 -1
index 1db10d8d6db7..a146f6cde3d0 100644
@@ -2839,10 +2839,11 @@ 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

Quoted 8 lines
Quoted 8 lines

arm64.
+     * Protected VMs currently require guest_memfd backed memslots with
+     * mappable memory to support shared mappings.
        */
-    if (kvm_slot_has_gmem(new) && !kvm_memslot_is_gmem_only(new)) +    if (kvm_arch_has_private_mem(kvm) && kvm_slot_has_gmem(new) && +        !kvm_gmem_slot_supports_mmap(new))

Sashiko has identified I've screwed up this condition though. kvm_arch_has_private_mem(kvm) shouldn't be in there as that allows a non-private VM to bind a non-mmappable gmem.

Quoted 20 lines
Quoted 20 lines

           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 +0 -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;
Quoted 8 lines
Quoted 5 lines
virt/kvm/guest_memfd.c +2 -0
    }
    +bool kvm_gmem_slot_supports_mmap(const struct kvm_memory_slot *slot)
+{
+    /* The guest_memfd file is pinned while preparing a new 

memslot. */

We should gate this on kvm_slot_has_gmem() and how do we ensure that the call comes after it is prepared ?

I'm not sure what you mean by "gate this on kvm_slot_has_gmem()" - yes this function only makes sense on a gmem slot - the user above checks that first. I'm not sure what value the function should call if you call it on a non-gmem slot?

Thanks,
Steve

Quoted 10 lines
Quoted 6 lines

+    return kvm_gmem_supports_mmap(file_inode(slot->gmem.file));

virt/kvm/guest_memfd.c +2 -0
+}
+

   static vm_fault_t kvm_gmem_fault_user_mapping(struct vm_fault *vmf) {
       struct inode *inode = file_inode(vmf->vma->vm_file);

Cheers
Suzuki

#7 lore raw

Re: [PATCH v2 02/12] KVM: arm64: guest_memfd: Check mmap support when preparing memslot

In reply to Steven Price
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Medium] KVM: arm64: mmap check bypassed in kvm_arch_prepare_memory_region() --

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Medium] KVM: arm64: mmap check bypassed in kvm_arch_prepare_memory_region()
--

commit 30a76d4c8b8b8454f9d59879df7f0b5de9a2cdbb
Author: Steven Price <[email protected]>

KVM: arm64: guest_memfd: Check mmap support when preparing memslot

This commit adds kvm_gmem_slot_supports_mmap() and updates kvm_arch_prepare_memory_region() to check whether the guest_memfd backing file supports mmap rather than checking kvm_memslot_is_gmem_only().

Quoted 7 lines
arch/arm64/kvm/mmu.c +0 -0
index 1db10d8d6db73..a146f6cde3d02 100644
@@ -2839,10 +2839,11 @@ int kvm_arch_prepare_memory_region(struct kvm *kvm,
 		return -EFAULT;
 

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

Quoted 10 lines
arch/arm64/kvm/mmu.c +4 -3
 	/*
-	 * Only support guest_memfd backed memslots with mappable memory, since
-	 * there aren't any CoCo VMs that support only private memory on arm64.
+	 * Protected VMs currently require guest_memfd backed memslots with
+	 * mappable memory to support shared mappings.
 	 */
-	if (kvm_slot_has_gmem(new) && !kvm_memslot_is_gmem_only(new))
+	if (kvm_arch_has_private_mem(kvm) && kvm_slot_has_gmem(new) &&
+	    !kvm_gmem_slot_supports_mmap(new))
 		return -EINVAL;

[Severity: Medium]
Does gating this check on kvm_arch_has_private_mem(kvm) cause a regression that allows binding non-mappable guest_memfds to non-Realm VMs on arm64?

At this commit kvm_arch_has_private_mem(kvm) resolves to the stub in include/linux/kvm_host.h that unconditionally returns false, and even after commit "KVM: arm64: CCA: Prevent memslot modifications after Realm creation" defines it as kvm_vm_is_realm(kvm), it remains false for all non-Realm VMs.

Userspace can then create a guest_memfd without GUEST_MEMFD_FLAG_MMAP and bind it to a normal VM via KVM_SET_USER_MEMORY_REGION2. Stage-2 faults in kvm_vm_mem_abort() -> gmem_abort() will fault those unmappable pages into the guest while HVA-based KVM accesses via slot->userspace_addr (such as kvm_read_guest() and kvm_write_guest()) access unrelated VMA memory. Dropping the kvm_arch_has_private_mem(kvm) condition here would preserve the requirement across all arm64 VMs.

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

PATCH v2 3/12 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 a146f6cde3d0..434502e8c4f9 100644
@@ -3166,6 +3166,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
#9 lore raw

PATCH v2 4/12 KVM: arm64: CCA: Check RMI support and features at init

In reply to Steven Price

Wire RMI initialization into KVM startup and select ARM_RMM_RMI. Proceed only when RMM mode is selected and RMI is available. If KVM uses LPA2, require RMM LPA2 support. Also require SHA-256 measurement support.

Define a disabled kvm_rmi_is_available static key for later Realm capability checks. The key is enabled after the remaining Realm support is in place.

---
Changes since v1:
- Combine the separate v1 RMI availability and LPA2/SHA-256 feature check patches into one initialization patch.

---
 arch/arm64/include/asm/kvm_rmi.h |  2 ++
 arch/arm64/include/asm/virt.h    |  1 +
 arch/arm64/kvm/Kconfig           |  1 +
 arch/arm64/kvm/Makefile          |  2 +-
 arch/arm64/kvm/arm.c             |  5 ++++
 arch/arm64/kvm/rmi.c             | 42 ++++++++++++++++++++++++++++++++
 include/linux/arm-rmi-cmds.h     |  5 ++++
 7 files changed, 57 insertions(+), 1 deletion(-)
 create mode 100644 arch/arm64/kvm/rmi.c
arch/arm64/include/asm/kvm_rmi.h +2 -0
index 67f16793b4d0..3194daaacf2b 100644
@@ -56,4 +56,6 @@ struct realm {
 	enum realm_state state;
 };
 
+void kvm_init_rmi(void);
+
 #endif /* __ASM_KVM_RMI_H */
arch/arm64/include/asm/virt.h +1 -0
index b546703c3ab9..92cec42952f4 100644
@@ -87,6 +87,7 @@ void __hyp_reset_vectors(void);
 bool is_kvm_arm_initialised(void);
 
 DECLARE_STATIC_KEY_FALSE(kvm_protected_mode_initialized);
+DECLARE_STATIC_KEY_FALSE(kvm_rmi_is_available);
 
 static inline bool is_pkvm_initialized(void)
 {
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 +5 -0
index e06cc390a3d6..318e8a4b19f8 100644
@@ -42,6 +42,7 @@
 #include <asm/kvm_nested.h>
 #include <asm/kvm_pkvm.h>
 #include <asm/kvm_ptrauth.h>
+#include <asm/kvm_rmi.h>
 #include <asm/sections.h>
 #include <asm/stacktrace/nvhe.h>
 
@@ -112,6 +113,8 @@ long kvm_get_cap_for_kvm_ioctl(unsigned int ioctl, long *ext)
 	return -EINVAL;
 }
 
+DEFINE_STATIC_KEY_FALSE(kvm_rmi_is_available);
+
 DECLARE_KVM_HYP_PER_CPU(unsigned long, kvm_hyp_vector);
 
 DEFINE_PER_CPU(unsigned long, kvm_arm_hyp_stack_base);
@@ -3272,6 +3275,8 @@ static __init int kvm_arm_init(void)
 
 	in_hyp_mode = is_kernel_in_hyp_mode();
 
+	kvm_init_rmi();
+
 	if (cpus_have_final_cap(ARM64_WORKAROUND_DEVICE_LOAD_ACQUIRE) ||
 	    cpus_have_final_cap(ARM64_WORKAROUND_1508412))
 		kvm_info("Guests without required CPU erratum workarounds can deadlock system!\n" \
arch/arm64/kvm/rmi.c new file +42 -0

42 lines not on this page · raw message

include/linux/arm-rmi-cmds.h +5 -0
index a84e50ac851c..6f43eac7c7be 100644
@@ -67,6 +67,11 @@ static __always_inline void rmi_smccc_invoke_once(struct arm_smccc_1_2_regs *reg
 
 unsigned long rmi_feat_reg(unsigned int index);
 
+static inline bool rmi_has_feature(unsigned int index, unsigned long feature)
+{
+	return !!(rmi_feat_reg(index) & feature);
+}
+
 int rmi_delegate_range(phys_addr_t phys, unsigned long size,
 		       phys_addr_t *out_phys);
 int rmi_undelegate_range(phys_addr_t phys, unsigned long size);
Signature · 1 line
2.43.0
#10 lore raw

Re: [PATCH v2 04/12] KVM: arm64: CCA: Check RMI support and features at init

Hi Steven

Quoted 8 lines · Steven Price

Wire RMI initialization into KVM startup and select ARM_RMM_RMI. Proceed only when RMM mode is selected and RMI is available. If KVM uses LPA2, require RMM LPA2 support. Also require SHA-256 measurement support.

Define a disabled kvm_rmi_is_available static key for later Realm capability checks. The key is enabled after the remaining Realm support is in place.

Given all of this is only used for the UABI (KVM_CAP_ARM_RMI and others), does it make sense to move this patch to the next batch of the CCA support and keep this only to sort out the memory management support ? Similarly for the Patch 1 ?

Cheers
Suzuki

Quoted 160 lines

---
Changes since v1:
- Combine the separate v1 RMI availability and LPA2/SHA-256 feature check patches into one initialization patch.

---
  arch/arm64/include/asm/kvm_rmi.h |  2 ++
  arch/arm64/include/asm/virt.h    |  1 +
  arch/arm64/kvm/Kconfig           |  1 +
  arch/arm64/kvm/Makefile          |  2 +-
  arch/arm64/kvm/arm.c             |  5 ++++
  arch/arm64/kvm/rmi.c             | 42 ++++++++++++++++++++++++++++++++
  include/linux/arm-rmi-cmds.h     |  5 ++++
  7 files changed, 57 insertions(+), 1 deletion(-)

create mode 100644 arch/arm64/kvm/rmi.c

arch/arm64/include/asm/kvm_rmi.h +2 -0
index 67f16793b4d0..3194daaacf2b 100644
@@ -56,4 +56,6 @@ struct realm {
  	enum realm_state state;
  };
 
+void kvm_init_rmi(void);
+
  #endif /* __ASM_KVM_RMI_H */
arch/arm64/include/asm/virt.h +1 -0
index b546703c3ab9..92cec42952f4 100644
@@ -87,6 +87,7 @@ void __hyp_reset_vectors(void);
  bool is_kvm_arm_initialised(void);
 
  DECLARE_STATIC_KEY_FALSE(kvm_protected_mode_initialized);
+DECLARE_STATIC_KEY_FALSE(kvm_rmi_is_available);
 
  static inline bool is_pkvm_initialized(void)
  {
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 +5 -0
index e06cc390a3d6..318e8a4b19f8 100644
@@ -42,6 +42,7 @@
  #include <asm/kvm_nested.h>
  #include <asm/kvm_pkvm.h>
  #include <asm/kvm_ptrauth.h>
+#include <asm/kvm_rmi.h>
  #include <asm/sections.h>
  #include <asm/stacktrace/nvhe.h>
 
@@ -112,6 +113,8 @@ long kvm_get_cap_for_kvm_ioctl(unsigned int ioctl, long *ext)
  	return -EINVAL;
  }
 
+DEFINE_STATIC_KEY_FALSE(kvm_rmi_is_available);
+
  DECLARE_KVM_HYP_PER_CPU(unsigned long, kvm_hyp_vector);
 
  DEFINE_PER_CPU(unsigned long, kvm_arm_hyp_stack_base);
@@ -3272,6 +3275,8 @@ static __init int kvm_arm_init(void)
 
  	in_hyp_mode = is_kernel_in_hyp_mode();
 
+	kvm_init_rmi();
+
  	if (cpus_have_final_cap(ARM64_WORKAROUND_DEVICE_LOAD_ACQUIRE) ||
  	    cpus_have_final_cap(ARM64_WORKAROUND_1508412))
  		kvm_info("Guests without required CPU erratum workarounds can deadlock system!\n" \
arch/arm64/kvm/rmi.c new file +42 -0
new file mode 100644
index 000000000000..f4c45021c6f7
@@ -0,0 +1,42 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Copyright (C) 2023-2026 ARM Ltd.
+ */
+
+#include <linux/arm-rmi-cmds.h>
+#include <linux/kvm_host.h>
+
+#include <asm/kvm_pgtable.h>
+#include <asm/virt.h>
+
+static int rmm_check_features(void)
+{
+	if (kvm_lpa2_is_enabled() &&
+	    !rmi_has_feature(0, RMI_FEATURE_REGISTER_0_LPA2)) {
+		kvm_err("RMM doesn't support LPA2\n");
+		return -ENXIO;
+	}
+
+	if (!rmi_has_feature(1, RMI_FEATURE_REGISTER_1_HASH_SHA_256)) {
+		kvm_err("RMM doesn't support SHA-256 measurements\n");
+		return -ENXIO;
+	}
+
+	return 0;
+}
+
+void kvm_init_rmi(void)
+{
+	if (kvm_get_mode() != KVM_MODE_RMM)
+		return;
+
+	if (!is_rmi_available()) {
+		kvm_info("Realm VM support is disabled because RMI is not available\n");
+		return;
+	}
+
+	if (rmm_check_features())
+		return;
+
+	/* Future patch will enable static branch kvm_rmi_is_available */
+}
include/linux/arm-rmi-cmds.h +5 -0
index a84e50ac851c..6f43eac7c7be 100644
@@ -67,6 +67,11 @@ static __always_inline void rmi_smccc_invoke_once(struct arm_smccc_1_2_regs *reg
 
  unsigned long rmi_feat_reg(unsigned int index);
 
+static inline bool rmi_has_feature(unsigned int index, unsigned long feature)
+{
+	return !!(rmi_feat_reg(index) & feature);
+}
+
  int rmi_delegate_range(phys_addr_t phys, unsigned long size,
  		       phys_addr_t *out_phys);
  int rmi_undelegate_range(phys_addr_t phys, unsigned long size);
#11 lore raw

Re: [PATCH v2 04/12] KVM: arm64: CCA: Check RMI support and features at init

Quoted 8 lines · Suzuki K Poulose

Hi Steven

Quoted 5 lines · Steven Price

Wire RMI initialization into KVM startup and select ARM_RMM_RMI. Proceed only when RMM mode is selected and RMI is available. If KVM uses LPA2, require RMM LPA2 support. Also require SHA-256 measurement support.

Quoted 8 lines
Quoted 3 lines

Define a disabled kvm_rmi_is_available static key for later Realm capability checks. The key is enabled after the remaining Realm support is in place.

Given all of this is only used for the UABI (KVM_CAP_ARM_RMI and others), does it make sense to move this patch to the next batch of the CCA support and keep this only to sort out the memory management support ? Similarly for the Patch 1 ?

True, I can move this patch (and patch 1) out and include it as part of a future series.

Thanks,
Steve

Quoted 160 lines

Cheers
Suzuki

Quoted 156 lines

---
Changes since v1:
- Combine the separate v1 RMI availability and LPA2/SHA-256 feature check patches into one initialization patch.
---
  arch/arm64/include/asm/kvm_rmi.h |  2 ++ arch/arm64/include/asm/virt.h    |  1 + arch/arm64/kvm/Kconfig           |  1 + arch/arm64/kvm/Makefile          |  2 +- arch/arm64/kvm/arm.c             |  5 ++++ arch/arm64/kvm/rmi.c             | 42 ++++++++++++++++++++++++++++++++ include/linux/arm-rmi-cmds.h     |  5 ++++

  7 files changed, 57 insertions(+), 1 deletion(-)

  create mode 100644 arch/arm64/kvm/rmi.c

arch/arm64/include/ renamed from arch/arm64/include/asm/kvm_rmi.h +0 -0

asm/kvm_rmi.h
index 67f16793b4d0..3194daaacf2b 100644

arch/arm64/include/asm/kvm_rmi.h +1 -0
@@ -56,4 +56,6 @@ struct realm {
       enum realm_state state;
   };
   +void kvm_init_rmi(void);
+
   #endif /* __ASM_KVM_RMI_H */
arch/arm64/include/asm/ renamed from arch/arm64/include/asm/virt.h +0 -0

virt.h
index b546703c3ab9..92cec42952f4 100644

arch/arm64/include/asm/virt.h +1 -0
@@ -87,6 +87,7 @@ void __hyp_reset_vectors(void);
   bool is_kvm_arm_initialised(void);
     DECLARE_STATIC_KEY_FALSE(kvm_protected_mode_initialized);
+DECLARE_STATIC_KEY_FALSE(kvm_rmi_is_available);
     static inline bool is_pkvm_initialized(void)
   {
arch/arm64/kvm/Kconfig +0 -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. diff --git a/arch/arm64/kvm/Makefile b/arch/arm64/kvm/Makefile index 7aaeeb84e788..66a719ab5b6b 100644

arch/arm64/kvm/Makefile +1 -1
@@ -16,7 +16,7 @@ CFLAGS_handle_exit.o += -Wno-override-init
   kvm-y += arm.o mmu.o mmio.o psci.o hypercalls.o pvtime.o \
        inject_fault.o va_layout.o handle_exit.o config.o \
        guest.o debug.o reset.o sys_regs.o stacktrace.o \
-     vgic-sys-reg-v3.o fpsimd.o pkvm.o \
+     vgic-sys-reg-v3.o fpsimd.o pkvm.o rmi.o \
        arch_timer.o trng.o vmid.o emulate-nested.o nested.o at.o \
        vgic/vgic.o vgic/vgic-init.o \
        vgic/vgic-irqfd.o vgic/vgic-v2.o \
arch/arm64/kvm/arm.c +1 -0
index e06cc390a3d6..318e8a4b19f8 100644
@@ -42,6 +42,7 @@
   #include <asm/kvm_nested.h>
   #include <asm/kvm_pkvm.h>
   #include <asm/kvm_ptrauth.h>
+#include <asm/kvm_rmi.h>
   #include <asm/sections.h>
   #include <asm/stacktrace/nvhe.h>

  @@ -112,6 +113,8 @@ long kvm_get_cap_for_kvm_ioctl(unsigned int ioctl, long *ext)
      return -EINVAL;
  }
  +DEFINE_STATIC_KEY_FALSE(kvm_rmi_is_available);
+
  DECLARE_KVM_HYP_PER_CPU(unsigned long, kvm_hyp_vector); DEFINE_PER_CPU(unsigned long, kvm_arm_hyp_stack_base);

arch/arm64/kvm/arm.c +1 -0
@@ -3272,6 +3275,8 @@ static __init int kvm_arm_init(void)
         in_hyp_mode = is_kernel_in_hyp_mode();
   +    kvm_init_rmi();
+

      if (cpus_have_final_cap(ARM64_WORKAROUND_DEVICE_LOAD_ACQUIRE) || cpus_have_final_cap(ARM64_WORKAROUND_1508412)) kvm_info("Guests without required CPU erratum workarounds can deadlock system!\n" \

arch/arm64/kvm/rmi.c new file +34 -0
new file mode 100644
index 000000000000..f4c45021c6f7
@@ -0,0 +1,42 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Copyright (C) 2023-2026 ARM Ltd.
+ */
+
+#include <linux/arm-rmi-cmds.h>
+#include <linux/kvm_host.h>
+
+#include <asm/kvm_pgtable.h>
+#include <asm/virt.h>
+
+static int rmm_check_features(void)
+{
+    if (kvm_lpa2_is_enabled() &&
+        !rmi_has_feature(0, RMI_FEATURE_REGISTER_0_LPA2)) {
+        kvm_err("RMM doesn't support LPA2\n");
+        return -ENXIO;
+    }
+
+    if (!rmi_has_feature(1, RMI_FEATURE_REGISTER_1_HASH_SHA_256)) {
+        kvm_err("RMM doesn't support SHA-256 measurements\n");
+        return -ENXIO;
+    }
+
+    return 0;
+}
+
+void kvm_init_rmi(void)
+{
+    if (kvm_get_mode() != KVM_MODE_RMM)
+        return;
+
+    if (!is_rmi_available()) {
+        kvm_info("Realm VM support is disabled because RMI is not

available\n");
+        return;
+    }

arch/arm64/kvm/rmi.c new file +6 -0
+
+    if (rmm_check_features())
+        return;
+
+    /* Future patch will enable static branch kvm_rmi_is_available */
+}
include/linux/arm-rmi-cmds.h +0 -0
index a84e50ac851c..6f43eac7c7be 100644
@@ -67,6 +67,11 @@ static __always_inline void

rmi_smccc_invoke_once(struct arm_smccc_1_2_regs *reg unsigned long rmi_feat_reg(unsigned int index); +static inline bool rmi_has_feature(unsigned int index, unsigned long feature)

include/linux/arm-rmi-cmds.h +4 -0
+{
+    return !!(rmi_feat_reg(index) & feature);
+}
+

  int rmi_delegate_range(phys_addr_t phys, unsigned long size, phys_addr_t *out_phys);
  int rmi_undelegate_range(phys_addr_t phys, unsigned long size);

#12 lore raw

PATCH v2 5/12 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 7948b177bf13..d672d87ffe62 100644
@@ -234,6 +234,7 @@ struct kvm_vcpu_ops {
 };
 
 struct kvm_gfn_range;
+enum kvm_gfn_range_filter;
 struct kvm_s2_fault_desc;
 
 struct kvm_vm_s2_ops {
@@ -244,7 +245,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 434502e8c4f9..c5aeb3a58417 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
#13 lore raw

PATCH v2 6/12 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 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 | 20 +++++++++++
 arch/arm64/kvm/arm.c             |  9 +++++
 arch/arm64/kvm/mmu.c             | 18 ++++++++--
 arch/arm64/kvm/rmi.c             | 57 ++++++++++++++++++++++++++++++++
 4 files changed, 102 insertions(+), 2 deletions(-)
arch/arm64/include/asm/kvm_rmi.h +20 -0
index 3194daaacf2b..f1a1ed6b91f5 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,12 +53,29 @@ enum realm_state {
 /**
  * struct realm - Additional per VM data for a Realm
  *
+ * @rd: Kernel mapping 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 {
+	void *rd;
+	/*
+	 * 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;
 };
 
 void kvm_init_rmi(void);
+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/arm.c +9 -0
index 318e8a4b19f8..b65a83ab31d8 100644
@@ -303,6 +303,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:
@@ -367,6 +374,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 c5aeb3a58417..9df3f8d5b4ef 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 +57 -0

66 lines not on this page · raw message

Signature · 1 line
2.43.0
#14 lore raw

PATCH v2 7/12 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 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             | 189 +++++++++++++++++++++++++++++++
 3 files changed, 215 insertions(+), 1 deletion(-)
arch/arm64/include/asm/kvm_rmi.h +9 -0
index f1a1ed6b91f5..58d825e40bdf 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 {
 	void *rd;
@@ -70,6 +71,7 @@ struct realm {
 	struct rmi_sro_state *sro;
 	enum realm_state state;
 	unsigned int ia_bits;
+	bool stage2_unmapped;
 };
 
 void kvm_init_rmi(void);
@@ -77,5 +79,12 @@ 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 9df3f8d5b4ef..be014f6c00e7 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 +189 -0

217 lines not on this page · raw message

Signature · 1 line
2.43.0
#15 lore raw

Re: [PATCH v2 07/12] KVM: arm64: CCA: Tear down RTTs

Thank you for your contribution! Sashiko AI review found 2 potential issues to consider: - [Low] KVM: arm64: possible skipped parent retry in realm_tear_down_rtt_level() - [Low] KVM: arm64: cca…

Thank you for your contribution! Sashiko AI review found 2 potential issues to consider:
- [Low] KVM: arm64: possible skipped parent retry in realm_tear_down_rtt_level()
- [Low] KVM: arm64: cca: leak of realm->sro on error returns in kvm_destroy_realm()
--

commit 9b5e6ebefa08ce4aff4f7ddec8f9b62647e79039
Author: Steven Price <[email protected]>

KVM: arm64: CCA: Tear down RTTs

This commit adds stage-2 unmapping and RTT destruction for CCA Realms, walking and undelegating non-root RTTs before destroying the Realm Descriptor.

Quoted 4 lines
arch/arm64/kvm/rmi.c +0 -0
index 69c628cfb6c74..b904e6541fb07 100644

[ ... ]

Quoted 46 lines
arch/arm64/kvm/rmi.c +46 -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 = rmi_rtt_level_mapsize(level - 1);
+
+	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 line 118

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]
Can SMC_RMI_RTT_DESTROY return RMI_ERROR_RTT with next_addr > addr (and next_addr < addr + map_size) when the target RTT at level has non-live initial entries followed by a live child table entry, or does the walk terminate at level - 1? This rests on an RMM specification detail for SMC_RMI_RTT_DESTROY that could not be confirmed from the kernel source.

If RMM returns RMI_RESULT_DATA_LEVEL(ret) == level with addr < next_addr < addr + map_size, the next_addr > addr check treats the live RTT as missing and advances addr to the unaligned next_addr. The next iteration sees align_addr != addr, tears down the remaining child RTTs at level + 1, and executes continue, advancing to the next map_size boundary without retrying realm_rtt_destroy() on the parent RTT at level. Alternatively, if the walk terminates at the parent entry and returns RMI_RESULT_DATA_LEVEL(ret) == level - 1 with next_addr == addr, WARN_ON(RMI_RESULT_DATA_LEVEL(ret) != level) triggers and aborts teardown with -EBUSY, leaving the RD and RTTs leaked.

[ ... ]

Quoted 15 lines
arch/arm64/kvm/rmi.c +9 -0
@@ -29,12 +206,24 @@ void kvm_destroy_realm(struct kvm *kvm)
 
 	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_addr_t rd_phys = virt_to_phys(realm->rd);

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

Quoted 10 lines
arch/arm64/kvm/rmi.c +3 -0
 
 		if (WARN_ON(rmi_realm_terminate(rd_phys, realm->sro)))
 			return;
 
+		if (WARN_ON(realm_destroy_rtts(kvm)))
+			return;
+
 		if (WARN_ON(rmi_realm_destroy(rd_phys, realm->sro)))
 			return;
 		rmi_free_delegated_page(rd_phys);

[Severity: Low]
Does returning early on failure in realm_destroy_rtts() (or the surrounding RMI teardown calls) leak realm->sro? Unlike realm->rd and the stage-2 PGD pages, realm->sro is host-owned heap memory that is never delegated to the RMM, and kfree(realm->sro) at the end of kvm_destroy_realm() is skipped on these early returns.

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

Re: [PATCH v2 07/12] KVM: arm64: CCA: Tear down RTTs

Quoted 79 lines · On 11/11/2026 17:38, [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 2 potential issues to consider:
- [Low] KVM: arm64: possible skipped parent retry in realm_tear_down_rtt_level()
- [Low] KVM: arm64: cca: leak of realm->sro on error returns in kvm_destroy_realm()
--

commit 9b5e6ebefa08ce4aff4f7ddec8f9b62647e79039
Author: Steven Price <[email protected]>

KVM: arm64: CCA: Tear down RTTs

This commit adds stage-2 unmapping and RTT destruction for CCA Realms, walking and undelegating non-root RTTs before destroying the Realm Descriptor.

Quoted 4 lines
arch/arm64/kvm/rmi.c +0 -0
index 69c628cfb6c74..b904e6541fb07 100644

[ ... ]

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 = rmi_rtt_level_mapsize(level - 1);
+
+	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))
Quoted 8 lines

arch/arm64/kvm/rmi.c

Quoted 1 line
arch/arm64/kvm/rmi.c +1 -0
+				return -EBUSY;

[Severity: Low]
Can SMC_RMI_RTT_DESTROY return RMI_ERROR_RTT with next_addr > addr (and next_addr < addr + map_size) when the target RTT at level has non-live initial entries followed by a live child table entry, or does the walk terminate at level - 1? This rests on an RMM specification detail for SMC_RMI_RTT_DESTROY that could not be confirmed from the kernel source.

This is ruled out by the spec. The only situation for RMI_ERROR_RTT with top > ipa is the "Missing RTT" case.

[Note I have noticed the rendering in the PDF spec is broken with ">" not appearing - I've raised this internally]

Quoted 32 lines

If RMM returns RMI_RESULT_DATA_LEVEL(ret) == level with addr < next_addr < addr + map_size, the next_addr > addr check treats the live RTT as missing and advances addr to the unaligned next_addr. The next iteration sees align_addr != addr, tears down the remaining child RTTs at level + 1, and executes continue, advancing to the next map_size boundary without retrying realm_rtt_destroy() on the parent RTT at level. Alternatively, if the walk terminates at the parent entry and returns RMI_RESULT_DATA_LEVEL(ret) == level - 1 with next_addr == addr, WARN_ON(RMI_RESULT_DATA_LEVEL(ret) != level) triggers and aborts teardown with -EBUSY, leaving the RD and RTTs leaked.

[ ... ]

Quoted 19 lines
arch/arm64/kvm/rmi.c +9 -0
@@ -29,12 +206,24 @@ void kvm_destroy_realm(struct kvm *kvm)
 
 	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_addr_t rd_phys = virt_to_phys(realm->rd);
 
 		if (WARN_ON(rmi_realm_terminate(rd_phys, realm->sro)))
 			return;
 
Quoted 13 lines

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

Quoted 6 lines
arch/arm64/kvm/rmi.c +3 -0
+		if (WARN_ON(realm_destroy_rtts(kvm)))
+			return;
+
 		if (WARN_ON(rmi_realm_destroy(rd_phys, realm->sro)))
 			return;
 		rmi_free_delegated_page(rd_phys);

[Severity: Low]
Does returning early on failure in realm_destroy_rtts() (or the surrounding RMI teardown calls) leak realm->sro? Unlike realm->rd and the stage-2 PGD pages, realm->sro is host-owned heap memory that is never delegated to the RMM, and kfree(realm->sro) at the end of kvm_destroy_realm() is skipped on these early returns.

Technically correct. Although all these early returns are WARN_ON() cases so Should Never Happen™. Still I guess I can handle this by a "goto out_free_sro" case rather than a straight return.

Steve

#17 lore raw

Re: [PATCH v2 07/12] KVM: arm64: CCA: Tear down RTTs

In reply to Steven Price
Quoted 80 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 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             | 189 +++++++++++++++++++++++++++++++
  3 files changed, 215 insertions(+), 1 deletion(-)
arch/arm64/include/asm/kvm_rmi.h +9 -0
index f1a1ed6b91f5..58d825e40bdf 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 {
  	void *rd;
@@ -70,6 +71,7 @@ struct realm {
  	struct rmi_sro_state *sro;
  	enum realm_state state;
  	unsigned int ia_bits;
+	bool stage2_unmapped;
  };
 
  void kvm_init_rmi(void);
@@ -77,5 +79,12 @@ 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 +10 -0
index 9df3f8d5b4ef..be014f6c00e7 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));
+	}

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

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

We added kvm_free_stage2_pgd() from kvm_destroy_realm() for Realms in the previous patch. Should we move that here since, this is where we

1. Deal with tearing down stage2
2. And more importantly we are skipping it for Realms.

Quoted 9 lines
arch/arm64/kvm/mmu.c +0 -0
  	kvm_mmu_free_memory_cache(&kvm->arch.mmu.split_page_cache);
  }
 
arch/arm64/kvm/rmi.c +0 -0
index 69c628cfb6c7..b904e6541fb0 100644
@@ -11,16 +11,193 @@
  #include <asm/kvm_pgtable.h>

arch/arm64/kvm/rmi.c line 21

Quoted 10 lines
arch/arm64/kvm/rmi.c +8 -0
  #include <asm/virt.h>
 
+static inline unsigned long rmi_rtt_level_mapsize(int level)
+{
+	if (WARN_ON(level > KVM_PGTABLE_LAST_LEVEL))
+		return PAGE_SIZE;
+
+	return (1UL << ARM64_HW_PGTABLE_LEVEL_SHIFT(level));
+}
+

We agreed to use kvm_granule_size() instead of this ?

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

arch/arm64/kvm/rmi.c line 29

Quoted 8 lines
arch/arm64/kvm/rmi.c +3 -0
  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)
+{
+	return 4 - max(2, stage2_pgtable_levels(realm->ia_bits));

I don't think we need this "max()". ia_bits is guaranteed to be

Quoted 1 line

= 32bit (ARM64_MIN_PARANGE_BITS, see kvm_init_ipa_range()), implying

that we have at least 2 levels on all page sizes.

arch/arm64/kvm/rmi.c line 36

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

Is there a reason why remove the account before freeing ? Could we do this after we have reclaimed it ?

Quoted 27 lines
arch/arm64/kvm/rmi.c +27 -0
+}
+
+/*
+ * 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(virt_to_phys(realm->rd), 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,

arch/arm64/kvm/rmi.c line 73

Quoted 10 lines
arch/arm64/kvm/rmi.c +10 -0
+				     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 = rmi_rtt_level_mapsize(level - 1);
+

We agreed to add a WARN_ON() here to ensure the start address is aligned ?

Suzuki

Quoted 116 lines
arch/arm64/kvm/rmi.c +116 -0
+	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)
+{
+	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)
+{
+	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);
+	write_unlock(&kvm->mmu_lock);
+
+	realm->stage2_unmapped = true;
+}
+
+int kvm_realm_teardown_stage2(struct kvm *kvm)
+{
+	lockdep_assert_held(&kvm->arch.config_lock);
+

arch/arm64/kvm/rmi.c line 199

Quoted 10 lines
arch/arm64/kvm/rmi.c +5 -0
+	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);
 
+	guard(mutex)(&kvm->arch.config_lock);

Do we need to document, why we need this mutex ?

Suzuki

#18 lore raw

Re: [PATCH v2 07/12] KVM: arm64: CCA: Tear down RTTs

Quoted 93 lines · Suzuki K Poulose
Quoted 81 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 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             | 189 +++++++++++++++++++++++++++++++

  3 files changed, 215 insertions(+), 1 deletion(-)
arch/arm64/include/ renamed from arch/arm64/include/asm/kvm_rmi.h +0 -0

asm/kvm_rmi.h
index f1a1ed6b91f5..58d825e40bdf 100644

arch/arm64/include/asm/kvm_rmi.h +9 -0
@@ -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 {
       void *rd;
@@ -70,6 +71,7 @@ struct realm {
       struct rmi_sro_state *sro;
       enum realm_state state;
       unsigned int ia_bits;
+    bool stage2_unmapped;
   };
     void kvm_init_rmi(void);
@@ -77,5 +79,12 @@ 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 +0 -0
index 9df3f8d5b4ef..be014f6c00e7 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);
+}
+
Quoted 7 lines

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

We added kvm_free_stage2_pgd() from kvm_destroy_realm() for Realms in the previous patch. Should we move that here since, this is where we

Quoted 2 lines

1. Deal with tearing down stage2
2. And more importantly we are skipping it for Realms.

I'm not quite sure what you're asking for here. We can't free the stage2 PGD while the realm is existing (because the RMM has the pages) - so we defer it.

Quoted 20 lines
Quoted 7 lines

      kvm_mmu_free_memory_cache(&kvm->arch.mmu.split_page_cache); }
  diff --git a/arch/arm64/kvm/rmi.c b/arch/arm64/kvm/rmi.c index 69c628cfb6c7..b904e6541fb0 100644

arch/arm64/kvm/rmi.c line 20

Quoted 10 lines
arch/arm64/kvm/rmi.c +7 -0
@@ -11,16 +11,193 @@
   #include <asm/kvm_pgtable.h>
   #include <asm/virt.h>
   +static inline unsigned long rmi_rtt_level_mapsize(int level)
+{
+    if (WARN_ON(level > KVM_PGTABLE_LAST_LEVEL))
+        return PAGE_SIZE;
+
+    return (1UL << ARM64_HW_PGTABLE_LEVEL_SHIFT(level));
+}
+

We agreed to use kvm_granule_size() instead of this ?

Quoted 1 line

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

That was a screw-up when I was squashing fixes back in - the next patch removes the code. I'll fix this up in the next version so it never appears. Thanks for pointing it out.

Quoted 11 lines

arch/arm64/kvm/rmi.c line 29

Quoted 7 lines
arch/arm64/kvm/rmi.c +2 -0
   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)
+{
+    return 4 - max(2, stage2_pgtable_levels(realm->ia_bits));

I don't think we need this "max()". ia_bits is guaranteed to be

Quoted 1 line

= 32bit (ARM64_MIN_PARANGE_BITS, see kvm_init_ipa_range()), implying

that we have at least 2 levels on all page sizes.

Ah, very true - Sashiko complained, but I hadn't noticed ARM64_MIN_PARANGE_BITS provided an effective minimum. I'll remove the max() and add a comment.

Quoted 10 lines

arch/arm64/kvm/rmi.c line 36

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

Is there a reason why remove the account before freeing ? Could we do this after we have reclaimed it ?

Sashiko again:

Quoted 3 lines

Does calling kvm_account_pgtable_pages() after rmi_free_delegated_page() access the page after it has already been freed and uncharged from its memcg?

Basically the problem is that rmi_free_delegated_page() frees the page back to the allocator. So doing the account after freeing means that technically the page could have been reused.

Quoted 32 lines
Quoted 32 lines
arch/arm64/kvm/rmi.c +8 -0
+}
+
+/*
+ * realm_rtt_destroy - Destroy an RTT at @level for @addr.
+ *
+ * Returns - Result of the RMI_RTT_DESTROY call, and:
+ * @rtt_granule:    RTT granule, if the RTT was destroyed.
+ * @next_addr:        IPA corresponding to the next possible valid

entry we
+ *            can target
+ */

arch/arm64/kvm/rmi.c +21 -0
+static long realm_rtt_destroy(struct realm *realm, unsigned long addr,
+                  int level, phys_addr_t *rtt_granule,
+                  unsigned long *next_addr)
+{
+    unsigned long out_rtt;
+    long ret;
+
+    ret = rmi_rtt_destroy(virt_to_phys(realm->rd), 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;
Quoted 8 lines

arch/arm64/kvm/rmi.c line 73

Quoted 6 lines
arch/arm64/kvm/rmi.c +6 -0
+
+    if (WARN_ON(level > KVM_PGTABLE_LAST_LEVEL))
+        return -EINVAL;
+
+    map_size = rmi_rtt_level_mapsize(level - 1);
+

We agreed to add a WARN_ON() here to ensure the start address is aligned ?

Indeed, and again I've managed to put it in the wrong commit (see the next patch). I'll fix this in the next revision.

Quoted 120 lines
Quoted 120 lines

+    for (addr = start; addr < end; addr = next_addr) { +        phys_addr_t rtt_granule;
+        long ret;
+        unsigned long align_addr = ALIGN(addr, map_size);

arch/arm64/kvm/rmi.c +75 -0
+
+        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);

arch/arm64/kvm/rmi.c +36 -0
+}
+
+static int realm_destroy_rtts(struct kvm *kvm)
+{
+    struct realm *realm = &kvm->arch.realm;
+    unsigned int ia_bits = realm->ia_bits;
+
+    lockdep_assert_held(&kvm->arch.config_lock);
+
+    return realm_tear_down_rtt_range(realm, 0, (1UL << ia_bits));
+}
+
+static void realm_unmap_stage2(struct kvm *kvm)
+{
+    struct realm *realm = &kvm->arch.realm;
+
+    lockdep_assert_held(&kvm->arch.config_lock);
+
+    if (realm->stage2_unmapped)
+        return;
+
+    write_lock(&kvm->mmu_lock);
+    kvm_stage2_unmap_range(&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)
+{
+    lockdep_assert_held(&kvm->arch.config_lock);
+
+    realm_unmap_stage2(kvm);
+    return 0;
+}
Quoted 8 lines
Quoted 6 lines

+
  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); +    guard(mutex)(&kvm->arch.config_lock);

Do we need to document, why we need this mutex ?

I'll add a comment.

Thanks,
Steve

#19 lore raw

Re: [PATCH v2 07/12] KVM: arm64: CCA: Tear down RTTs

Quoted 20 lines · Steven Price
Quoted 19 lines · Suzuki K Poulose
Quoted 18 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.

...

Quoted 9 lines
Quoted 9 lines
Quoted 9 lines

    #endif /* __ASM_KVM_RMI_H */

arch/arm64/kvm/mmu.c +0 -0
index 9df3f8d5b4ef..be014f6c00e7 100644
@@ -1145,9 +1145,25 @@ int kvm_init_stage2_mmu(struct kvm *kvm, struct

kvm_s2_mmu *mmu, unsigned long t
      return err;
  }

...

Quoted 11 lines
Quoted 11 lines
Quoted 7 lines

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

We added kvm_free_stage2_pgd() from kvm_destroy_realm() for Realms in the previous patch. Should we move that here since, this is where we

Quoted 6 lines
Quoted 2 lines

1. Deal with tearing down stage2
2. And more importantly we are skipping it for Realms.

I'm not quite sure what you're asking for here. We can't free the stage2 PGD while the realm is existing (because the RMM has the pages) - so we defer it.

I understand. My point is, we are deferring the stage2 free for Realm in this patch, without explicitly showing where it is done. We have introduced that as part of kvm_destroy_realm() in Patch 6.

My recommendation is to add kvm_free_stage2_pgd() to kvm_destroy_realm() in this patch, where we are showing when this is deferred to. e.g., this patch functionally leaves the reader with no clue when this is actually freed.

Does that make sense ?

Quoted 56 lines
Quoted 21 lines
Quoted 8 lines

      kvm_mmu_free_memory_cache(&kvm->arch.mmu.split_page_cache); }
  diff --git a/arch/arm64/kvm/rmi.c b/arch/arm64/kvm/rmi.c
index 69c628cfb6c7..b904e6541fb0 100644

arch/arm64/kvm/rmi.c line 20

Quoted 10 lines
arch/arm64/kvm/rmi.c +7 -0
@@ -11,16 +11,193 @@
   #include <asm/kvm_pgtable.h>
   #include <asm/virt.h>
   +static inline unsigned long rmi_rtt_level_mapsize(int level)
+{
+    if (WARN_ON(level > KVM_PGTABLE_LAST_LEVEL))
+        return PAGE_SIZE;
+
+    return (1UL << ARM64_HW_PGTABLE_LEVEL_SHIFT(level));
+}
+

We agreed to use kvm_granule_size() instead of this ?

Quoted 1 line

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

That was a screw-up when I was squashing fixes back in - the next patch removes the code. I'll fix this up in the next version so it never appears. Thanks for pointing it out.

Quoted 11 lines

arch/arm64/kvm/rmi.c line 29

Quoted 7 lines
arch/arm64/kvm/rmi.c +2 -0
   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)
+{
+    return 4 - max(2, stage2_pgtable_levels(realm->ia_bits));

I don't think we need this "max()". ia_bits is guaranteed to be

Quoted 1 line

= 32bit (ARM64_MIN_PARANGE_BITS, see kvm_init_ipa_range()), implying

that we have at least 2 levels on all page sizes.

Ah, very true - Sashiko complained, but I hadn't noticed ARM64_MIN_PARANGE_BITS provided an effective minimum. I'll remove the max() and add a comment.

Quoted 10 lines

arch/arm64/kvm/rmi.c line 36

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

Is there a reason why remove the account before freeing ? Could we do this after we have reclaimed it ?

Sashiko again:

Quoted 7 lines
Quoted 3 lines

Does calling kvm_account_pgtable_pages() after rmi_free_delegated_page() access the page after it has already been freed and uncharged from its memcg?

Basically the problem is that rmi_free_delegated_page() frees the page back to the allocator. So doing the account after freeing means that technically the page could have been reused.

Thanks, for the explanation, that makes sense.

Cheers
Suzuki

#20 lore raw

Re: [PATCH v2 07/12] KVM: arm64: CCA: Tear down RTTs

Quoted 61 lines · Suzuki K Poulose
Quoted 23 lines · Steven Price
Quoted 22 lines · Suzuki K Poulose
Quoted 21 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.

...

Quoted 9 lines
Quoted 9 lines
Quoted 9 lines

     #endif /* __ASM_KVM_RMI_H */

arch/arm64/kvm/mmu.c +0 -0
index 9df3f8d5b4ef..be014f6c00e7 100644
@@ -1145,9 +1145,25 @@ int kvm_init_stage2_mmu(struct kvm *kvm, struct

kvm_s2_mmu *mmu, unsigned long t
       return err;
   }

...

Quoted 11 lines
Quoted 11 lines
Quoted 7 lines

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

We added kvm_free_stage2_pgd() from kvm_destroy_realm() for Realms in the previous patch. Should we move that here since, this is where we

Quoted 6 lines
Quoted 2 lines

1. Deal with tearing down stage2
2. And more importantly we are skipping it for Realms.

I'm not quite sure what you're asking for here. We can't free the stage2 PGD while the realm is existing (because the RMM has the pages) - so we defer it.

I understand. My point is, we are deferring the stage2 free for Realm in this patch, without explicitly showing where it is done. We have introduced that as part of kvm_destroy_realm() in Patch 6.

Quoted 6 lines

My recommendation is to add kvm_free_stage2_pgd() to kvm_destroy_realm() in this patch, where we are showing when this is deferred to. e.g., this patch functionally leaves the reader with no clue when this is actually freed.

Does that make sense ?

Ah, yes move the code between patches - I thought you were suggesting somehow moving the code between functions (hence I was very confused).

Yes, I can move the kvm_destroy_realm() code changes into this patch.

Thanks,
Steve

Quoted 71 lines
Quoted 58 lines
Quoted 22 lines
Quoted 9 lines

       kvm_mmu_free_memory_cache(&kvm->arch.mmu.split_page_cache); }
   diff --git a/arch/arm64/kvm/rmi.c b/arch/arm64/kvm/rmi.c index 69c628cfb6c7..b904e6541fb0 100644

arch/arm64/kvm/rmi.c line 20

Quoted 10 lines
arch/arm64/kvm/rmi.c +7 -0
@@ -11,16 +11,193 @@
    #include <asm/kvm_pgtable.h>
    #include <asm/virt.h>
    +static inline unsigned long rmi_rtt_level_mapsize(int level)
+{
+    if (WARN_ON(level > KVM_PGTABLE_LAST_LEVEL))
+        return PAGE_SIZE;
+
+    return (1UL << ARM64_HW_PGTABLE_LEVEL_SHIFT(level));
+}
+

We agreed to use kvm_granule_size() instead of this ?

Quoted 1 line

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

That was a screw-up when I was squashing fixes back in - the next patch removes the code. I'll fix this up in the next version so it never appears. Thanks for pointing it out.

Quoted 9 lines

arch/arm64/kvm/rmi.c line 29

Quoted 8 lines
arch/arm64/kvm/rmi.c +2 -0
    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)
+{
+    return 4 - max(2, stage2_pgtable_levels(realm->ia_bits));
Quoted 3 lines

I don't think we need this "max()". ia_bits is guaranteed to be

Quoted 1 line

= 32bit (ARM64_MIN_PARANGE_BITS, see kvm_init_ipa_range()), implying

that we have at least 2 levels on all page sizes.

Ah, very true - Sashiko complained, but I hadn't noticed ARM64_MIN_PARANGE_BITS provided an effective minimum. I'll remove the max() and add a comment.

Quoted 10 lines

arch/arm64/kvm/rmi.c line 36

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

Is there a reason why remove the account before freeing ? Could we do this after we have reclaimed it ?

Sashiko again:

Quoted 7 lines
Quoted 3 lines

Does calling kvm_account_pgtable_pages() after rmi_free_delegated_page() access the page after it has already been freed and uncharged from its memcg?

Basically the problem is that rmi_free_delegated_page() frees the page back to the allocator. So doing the account after freeing means that technically the page could have been reused.

Thanks, for the explanation, that makes sense.

Cheers
Suzuki

#21 lore raw

PATCH v2 8/12 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 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             | 378 ++++++++++++++++++++++++++++++-
 drivers/firmware/arm_rmm/rmi.c   |  10 -
 include/linux/arm-rmi-cmds.h     |   8 +
 4 files changed, 385 insertions(+), 19 deletions(-)
arch/arm64/include/asm/kvm_rmi.h +8 -0
index 58d825e40bdf..e64c3bcce160 100644
@@ -81,6 +81,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 +369 -9

426 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 6f43eac7c7be..71ba94eb72c0 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
#22 lore raw

Re: [PATCH v2 08/12] KVM: arm64: CCA: Add Realm RTT mapping and invalidation helpers

Thank you for your contribution! Sashiko AI review found 2 potential issues to consider: - [Medium] KVM: arm64: wrong granule level when skipping missing RTT in realm_fold_rtt_level() - [Medium]…

Thank you for your contribution! Sashiko AI review found 2 potential issues to consider:
- [Medium] KVM: arm64: wrong granule level when skipping missing RTT in realm_fold_rtt_level()
- [Medium] KVM: arm64: cca: unhandled GFP_ATOMIC failure in realm_create_rtt_levels()
--

commit 68d993c0f3cb9846fd5972acfd789cb5ebe22bac
Author: Steven Price <[email protected]>

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

This commit adds helpers to create, fold, and unmap Realm Translation Tables (RTTs) for shared and private IPA ranges.

Quoted 4 lines
arch/arm64/kvm/rmi.c +0 -0
index b904e6541fb07..5aa34d68de595 100644

[ ... ]

Quoted 35 lines
arch/arm64/kvm/rmi.c +31 -0
@@ -156,12 +286,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.
+ */
+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;
+
+	map_size = kvm_granule_size(level - 1);
+
+	for (addr = start; addr < end; addr = next_addr) {
+		phys_addr_t rtt_granule;
+		long ret;
+
+		next_addr = ALIGN(addr + 1, map_size);
+
+		ret = realm_rtt_fold(realm, addr, level, &rtt_granule);
+		if (ret < 0)
+			return ret;
+
+		switch (RMI_RESULT_STATUS(ret)) {
+		case RMI_SUCCESS:
+			free_rtt(rtt_granule);
+			break;
+		case RMI_ERROR_RTT: {

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

Quoted 10 lines
arch/arm64/kvm/rmi.c +10 -0
+			int err_level = RMI_RESULT_DATA_LEVEL(ret);
+
+			if (err_level < level) {
+				/* No RTT at this level; skip the missing ancestor. */
+				if (WARN_ON(err_level < get_start_level(realm)))
+					return -EINVAL;
+				next_addr = ALIGN(addr + 1,
+						  kvm_granule_size(err_level - 1));
+				break;
+			}

[Severity: Medium]
Should this advance next_addr by kvm_granule_size(err_level) instead of kvm_granule_size(err_level - 1) (matching realm_unmap_shared_range() and realm_unmap_private_range())?

When realm_rtt_fold() fails at err_level < level, the walk stopped at a single non-table entry in the table at err_level, which spans kvm_granule_size(err_level). Using err_level - 1 skips the span of the entire table at err_level, leaving any foldable sibling RTTs in the rest of that table unfolded, and evaluates kvm_granule_size(-1) when err_level is 0 on 4-level stage-2 tables (shifting past 64 bits on 16KB and 64KB pages).

[ ... ]

Quoted 9 lines
arch/arm64/kvm/rmi.c +5 -0
@@ -191,6 +403,154 @@ int kvm_realm_teardown_stage2(struct kvm *kvm)
 	return 0;
 }
 
+static void realm_unmap_shared_range(struct kvm *kvm,
+				     unsigned long start,
+				     unsigned long end,
+				     bool may_block)
+{

[ ... ]

Quoted 18 lines
arch/arm64/kvm/rmi.c +18 -0
+		case RMI_ERROR_RTT: {
+			int err_level = RMI_RESULT_DATA_LEVEL(ret);
+			int level = find_map_level(realm, addr, end);
+
+			if (WARN_ON(err_level > KVM_PGTABLE_LAST_LEVEL))
+				return;
+
+			if (err_level >= level) {
+				/* Nothing present, so skip */
+				next_addr = ALIGN(addr + 1,
+						  kvm_granule_size(err_level));
+				break;
+			}
+
+			ret = realm_create_rtt_levels(realm, addr, err_level,
+						      level, NULL);
+			if (WARN_ON(ret))
+				return;

[ ... ]

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

[ ... ]

Quoted 8 lines
arch/arm64/kvm/rmi.c +8 -0
+		case RMI_ERROR_RTT: {
+			int err_level = RMI_RESULT_DATA_LEVEL(ret);
+			int level = find_map_level(realm, addr, end);
+
+			if (WARN_ON(err_level > KVM_PGTABLE_LAST_LEVEL))
+				return;
+
+			if (err_level >= level) {

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

Quoted 10 lines
arch/arm64/kvm/rmi.c +10 -0
+				/* Nothing present, so skip this RTT entry. */
+				next_addr = ALIGN(addr + 1,
+						  kvm_granule_size(err_level));
+				break;
+			}
+
+			ret = realm_create_rtt_levels(realm, addr, err_level,
+						      level, NULL);
+			if (WARN_ON(ret))
+				return;

[Severity: Medium]
Can realm_create_rtt_levels() fail with -ENOMEM here, triggering WARN_ON() and aborting the unmap?

Passing mc = NULL causes alloc_delegated_granule() to allocate RTT pages with GFP_ATOMIC | __GFP_ZERO | __GFP_ACCOUNT. When unmapping a sub-block range under memory pressure or a memcg limit, an allocation failure fires WARN_ON(ret) and returns early, leaving the rest of [addr, end) mapped in the Realm RTTs (for example across an MMU notifier invalidation before the host frees the backing pages).

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

Re: [PATCH v2 08/12] KVM: arm64: CCA: Add Realm RTT mapping and invalidation helpers

Quoted 65 lines · On 11/11/2026 18:06, [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 2 potential issues to consider:
- [Medium] KVM: arm64: wrong granule level when skipping missing RTT in realm_fold_rtt_level()
- [Medium] KVM: arm64: cca: unhandled GFP_ATOMIC failure in realm_create_rtt_levels()
--

commit 68d993c0f3cb9846fd5972acfd789cb5ebe22bac
Author: Steven Price <[email protected]>

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

This commit adds helpers to create, fold, and unmap Realm Translation Tables (RTTs) for shared and private IPA ranges.

Quoted 4 lines
arch/arm64/kvm/rmi.c +0 -0
index b904e6541fb07..5aa34d68de595 100644

[ ... ]

Quoted 42 lines
arch/arm64/kvm/rmi.c +38 -0
@@ -156,12 +286,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.
+ */
+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;
+
+	map_size = kvm_granule_size(level - 1);
+
+	for (addr = start; addr < end; addr = next_addr) {
+		phys_addr_t rtt_granule;
+		long ret;
+
+		next_addr = ALIGN(addr + 1, map_size);
+
+		ret = realm_rtt_fold(realm, addr, level, &rtt_granule);
+		if (ret < 0)
+			return ret;
+
+		switch (RMI_RESULT_STATUS(ret)) {
+		case RMI_SUCCESS:
+			free_rtt(rtt_granule);
+			break;
+		case RMI_ERROR_RTT: {
+			int err_level = RMI_RESULT_DATA_LEVEL(ret);
+
+			if (err_level < level) {
+				/* No RTT at this level; skip the missing ancestor. */
+				if (WARN_ON(err_level < get_start_level(realm)))
+					return -EINVAL;
+				next_addr = ALIGN(addr + 1,
Quoted 8 lines

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

Quoted 3 lines
arch/arm64/kvm/rmi.c +3 -0
+						  kvm_granule_size(err_level - 1));
+				break;
+			}

[Severity: Medium]
Should this advance next_addr by kvm_granule_size(err_level) instead of kvm_granule_size(err_level - 1) (matching realm_unmap_shared_range() and realm_unmap_private_range())?

Indeed, this looks wrong. I'll correct in the next version.

Quoted 73 lines

When realm_rtt_fold() fails at err_level < level, the walk stopped at a single non-table entry in the table at err_level, which spans kvm_granule_size(err_level). Using err_level - 1 skips the span of the entire table at err_level, leaving any foldable sibling RTTs in the rest of that table unfolded, and evaluates kvm_granule_size(-1) when err_level is 0 on 4-level stage-2 tables (shifting past 64 bits on 16KB and 64KB pages).

[ ... ]

Quoted 9 lines
arch/arm64/kvm/rmi.c +5 -0
@@ -191,6 +403,154 @@ int kvm_realm_teardown_stage2(struct kvm *kvm)
 	return 0;
 }
 
+static void realm_unmap_shared_range(struct kvm *kvm,
+				     unsigned long start,
+				     unsigned long end,
+				     bool may_block)
+{

[ ... ]

Quoted 18 lines
arch/arm64/kvm/rmi.c +18 -0
+		case RMI_ERROR_RTT: {
+			int err_level = RMI_RESULT_DATA_LEVEL(ret);
+			int level = find_map_level(realm, addr, end);
+
+			if (WARN_ON(err_level > KVM_PGTABLE_LAST_LEVEL))
+				return;
+
+			if (err_level >= level) {
+				/* Nothing present, so skip */
+				next_addr = ALIGN(addr + 1,
+						  kvm_granule_size(err_level));
+				break;
+			}
+
+			ret = realm_create_rtt_levels(realm, addr, err_level,
+						      level, NULL);
+			if (WARN_ON(ret))
+				return;

[ ... ]

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

[ ... ]

Quoted 8 lines
arch/arm64/kvm/rmi.c +8 -0
+		case RMI_ERROR_RTT: {
+			int err_level = RMI_RESULT_DATA_LEVEL(ret);
+			int level = find_map_level(realm, addr, end);
+
+			if (WARN_ON(err_level > KVM_PGTABLE_LAST_LEVEL))
+				return;
+
+			if (err_level >= level) {

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

Quoted 10 lines
arch/arm64/kvm/rmi.c +10 -0
+				/* Nothing present, so skip this RTT entry. */
+				next_addr = ALIGN(addr + 1,
+						  kvm_granule_size(err_level));
+				break;
+			}
+
+			ret = realm_create_rtt_levels(realm, addr, err_level,
+						      level, NULL);
+			if (WARN_ON(ret))
+				return;

[Severity: Medium]
Can realm_create_rtt_levels() fail with -ENOMEM here, triggering WARN_ON() and aborting the unmap?

Quoted 6 lines

Passing mc = NULL causes alloc_delegated_granule() to allocate RTT pages with GFP_ATOMIC | __GFP_ZERO | __GFP_ACCOUNT. When unmapping a sub-block range under memory pressure or a memcg limit, an allocation failure fires WARN_ON(ret) and returns early, leaving the rest of [addr, end) mapped in the Realm RTTs (for example across an MMU notifier invalidation before the host frees the backing pages).

Yes, this isn't ideal.

This should really have a memcache and allocate from that - that would stop this path hitting in the normal case where the allocation can be satisfied from the cache. With the cache being topped up when necessary with the mmu_lock dropped.

Finally I could handle the case where we actually can't allocate an RTT (and therefore can't split a huge page) by simply unmapping the huge page in one go. For the shared range this is fine (the pages can be faulted back in - assuming the memory becomes available), for the private range it would be fatal to the VM, but the system can continue.

As Sashiko has pointed out, this would already have provoked a WARN_ON() from the allocator, so I think that's probably fine.

Steve

#24 lore raw

Re: [PATCH v2 08/12] KVM: arm64: CCA: Add Realm RTT mapping and invalidation helpers

In reply to Steven Price
Quoted 57 lines · Steven Price

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

---
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             | 378 ++++++++++++++++++++++++++++++-
  drivers/firmware/arm_rmm/rmi.c   |  10 -
  include/linux/arm-rmi-cmds.h     |   8 +
  4 files changed, 385 insertions(+), 19 deletions(-)
arch/arm64/include/asm/kvm_rmi.h +8 -0
index 58d825e40bdf..e64c3bcce160 100644
@@ -81,6 +81,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 +0 -8
index b904e6541fb0..5aa34d68de59 100644
@@ -11,14 +11,6 @@
  #include <asm/kvm_pgtable.h>
  #include <asm/virt.h>
 
-static inline unsigned long rmi_rtt_level_mapsize(int level)
-{
-	if (WARN_ON(level > KVM_PGTABLE_LAST_LEVEL))
-		return PAGE_SIZE;
-
-	return (1UL << ARM64_HW_PGTABLE_LEVEL_SHIFT(level));
-}
-
  u32 kvm_rmm_ipa_limit(void)
  {
  	return u64_get_bits(rmi_feat_reg(0), RMI_FEATURE_REGISTER_0_S2SZ);

arch/arm64/kvm/rmi.c static int get_start_level(struct realm *realm) · line 30

Quoted 10 lines
arch/arm64/kvm/rmi.c +7 -0
@@ -29,6 +21,75 @@ static int get_start_level(struct realm *realm)
  	return 4 - max(2, stage2_pgtable_levels(realm->ia_bits));
  }
 
+static int find_map_level(struct realm *realm,
+			  unsigned long start,
+			  unsigned long end)
+{
+	int level = KVM_PGTABLE_LAST_LEVEL;
+
+	while (level > get_start_level(realm)) {

minor nit: Could we save the start_level in a local variable. The generated assembly seems to be evaluating get_start_level() for each iteration. For that matter, I don't mind caching this in struct realm,
as we end up calculating it quite often.

Quoted 147 lines
arch/arm64/kvm/rmi.c +129 -0
+		unsigned long map_size = kvm_granule_size(level - 1);
+
+		if (!IS_ALIGNED(start, map_size) ||
+		    (start + map_size) > end)
+			break;
+
+		level--;
+	}
+
+	return level;
+}
+
+static int undelegate_range_desc(unsigned long desc)
+{
+	unsigned long size = rmi_addr_block_size_to_bytes(RMI_ADDR_RANGE_BLOCK_SIZE(desc));
+	unsigned long count = RMI_ADDR_RANGE_COUNT(desc);
+	unsigned long addr = RMI_ADDR_RANGE_ADDR(desc);
+	unsigned long state = RMI_ADDR_RANGE_STATE(desc);
+
+	if (state == RMI_OP_MEM_UNDELEGATED)
+		return 0;
+
+	if (size * count == 0)
+		return 0;
+
+	return rmi_undelegate_range(addr, size * count);
+}
+
+static phys_addr_t alloc_delegated_granule(struct kvm_mmu_memory_cache *mc)
+{
+	phys_addr_t phys;
+	void *virt;
+
+	if (mc) {
+		virt = kvm_mmu_memory_cache_alloc(mc);
+	} else {
+		virt = (void *)__get_free_page(GFP_ATOMIC | __GFP_ZERO |
+					       __GFP_ACCOUNT);
+	}
+
+	if (!virt)
+		return PHYS_ADDR_MAX;
+
+	phys = virt_to_phys(virt);
+	if (rmi_delegate_page(phys)) {
+		free_page((unsigned long)virt);
+		return PHYS_ADDR_MAX;
+	}
+
+	return phys;
+}
+
+static phys_addr_t alloc_rtt(struct kvm_mmu_memory_cache *mc)
+{
+	phys_addr_t phys = alloc_delegated_granule(mc);
+
+	if (phys != PHYS_ADDR_MAX)
+		kvm_account_pgtable_pages(phys_to_virt(phys), 1);
+
+	return phys;
+}
+
  static void free_rtt(phys_addr_t phys)
  {
  	kvm_account_pgtable_pages(phys_to_virt(phys), -1);
@@ -36,6 +97,32 @@ static void free_rtt(phys_addr_t phys)
  		kvm_account_pgtable_pages(phys_to_virt(phys), 1);
  }
 
+static long realm_rtt_create(struct realm *realm,
+			     unsigned long addr,
+			     int level,
+			     phys_addr_t phys)
+{
+	addr = ALIGN_DOWN(addr, kvm_granule_size(level - 1));
+	return rmi_rtt_create(virt_to_phys(realm->rd), phys, addr, level);
+}
+
+static long realm_rtt_fold(struct realm *realm,
+			   unsigned long addr,
+			   int level,
+			   phys_addr_t *rtt_granule)
+{
+	unsigned long out_rtt;
+	long ret;
+
+	addr = ALIGN_DOWN(addr, kvm_granule_size(level - 1));
+	ret = rmi_rtt_fold(virt_to_phys(realm->rd), addr, level, &out_rtt);
+
+	if (ret == RMI_SUCCESS && rtt_granule)
+		*rtt_granule = out_rtt;
+
+	return ret;
+}
+
  /*
   * realm_rtt_destroy - Destroy an RTT at @level for @addr.
   *
@@ -60,6 +147,47 @@ static long realm_rtt_destroy(struct realm *realm, unsigned long addr,
  	return ret;
  }
 
+static int realm_create_rtt_levels(struct realm *realm,
+				   unsigned long ipa,
+				   int level,
+				   int max_level,
+				   struct kvm_mmu_memory_cache *mc)
+{
+	long ret = 0;
+	phys_addr_t rtt = PHYS_ADDR_MAX;
+
+	while (level++ < max_level) {
+		if (rtt == PHYS_ADDR_MAX)
+			rtt = alloc_rtt(mc);
+		if (rtt == PHYS_ADDR_MAX)
+			return -ENOMEM;
+
+		ret = realm_rtt_create(realm, ipa, level, rtt);
+		if (ret < 0)
+			break;
+
+		if (RMI_RESULT_STATUS(ret) == RMI_ERROR_RTT &&
+		    RMI_RESULT_DATA_LEVEL(ret) == level - 1) {
+			/* The RTT already exists, reuse the granule at the next level. */
+			ret = 0;
+			continue;
+		}
+
+		if (ret) {
+			WARN(1, "Failed to create RTT at level %d: %ld\n",
+			     level, ret);
+			ret = -ENXIO;
+			break;
+		}
+		rtt = PHYS_ADDR_MAX;
+	}
+
+	if (rtt != PHYS_ADDR_MAX)
+		free_rtt(rtt);
+
+	return ret;
+}
+
  static int realm_tear_down_rtt_level(struct realm *realm, int level,
  				     unsigned long start, unsigned long end)
  {

arch/arm64/kvm/rmi.c static int realm_tear_down_rtt_level(struct realm *realm, int level, · line 203

Quoted 8 lines
arch/arm64/kvm/rmi.c +3 -1
@@ -69,7 +197,9 @@ static int realm_tear_down_rtt_level(struct realm *realm, int level,
  	if (WARN_ON(level > KVM_PGTABLE_LAST_LEVEL))
  		return -EINVAL;
 
-	map_size = rmi_rtt_level_mapsize(level - 1);
+	map_size = kvm_granule_size(level - 1);
+	if (WARN_ON(!IS_ALIGNED(start, map_size)))
+		return -EINVAL;
 

I see, as discussed please move it to the other patch.

Quoted 18 lines
arch/arm64/kvm/rmi.c +12 -0
  	for (addr = start; addr < end; addr = next_addr) {
  		phys_addr_t rtt_granule;
@@ -156,12 +286,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.
+ */
+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))

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

Quoted 10 lines
arch/arm64/kvm/rmi.c +10 -0
+		return -EINVAL;
+
+	map_size = kvm_granule_size(level - 1);
+
+	for (addr = start; addr < end; addr = next_addr) {
+		phys_addr_t rtt_granule;
+		long ret;
+
+		next_addr = ALIGN(addr + 1, map_size);
+

retry: // See below

Quoted 24 lines
arch/arm64/kvm/rmi.c +24 -0
+		ret = realm_rtt_fold(realm, addr, level, &rtt_granule);
+		if (ret < 0)
+			return ret;
+
+		switch (RMI_RESULT_STATUS(ret)) {
+		case RMI_SUCCESS:
+			free_rtt(rtt_granule);
+			break;
+		case RMI_ERROR_RTT: {
+			int err_level = RMI_RESULT_DATA_LEVEL(ret);
+
+			if (err_level < level) {
+				/* No RTT at this level; skip the missing ancestor. */
+				if (WARN_ON(err_level < get_start_level(realm)))
+					return -EINVAL;
+				next_addr = ALIGN(addr + 1,
+						  kvm_granule_size(err_level - 1));
+				break;
+			}
+			if (level == KVM_PGTABLE_LAST_LEVEL) {
+				not_folded++;
+				break;
+			}
+			/* Recurse a level deeper */

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

Quoted 10 lines
arch/arm64/kvm/rmi.c +10 -0
+			ret = realm_fold_rtt_level(realm,
+						   level + 1,
+						   addr,
+						   min(next_addr, end));
+			if (ret < 0)
+				return ret;
+			if (ret) {
+				not_folded++;
+				break;
+			}

---8>---

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

Quoted 12 lines
arch/arm64/kvm/rmi.c +12 -0
+			/* Try once more after folding the child RTTs. */
+			ret = realm_rtt_fold(realm, addr, level, &rtt_granule);
+			if (ret < 0)
+				return ret;
+			if (RMI_RESULT_STATUS(ret) == RMI_SUCCESS) {
+				free_rtt(rtt_granule);
+				break;
+			}
+			if (RMI_RESULT_STATUS(ret) != RMI_ERROR_RTT) {
+				WARN_ON(1);
+				return -ENXIO;
+			}

---<8---

This seems like a repeat of the code above, could we do :

		goto retry;
Quoted 38 lines
arch/arm64/kvm/rmi.c +25 -0
+			not_folded++;
+			break;
+		}
+		default:
+			WARN_ON(1);
+			return -ENXIO;
+		}
+	}
+
+	return not_folded;
+}
+
  static int realm_destroy_rtts(struct kvm *kvm)
  {
  	struct realm *realm = &kvm->arch.realm;
  	unsigned int ia_bits = realm->ia_bits;
 
  	lockdep_assert_held(&kvm->arch.config_lock);
+	if (WARN_ON_ONCE(!realm->stage2_unmapped))
+		return -EINVAL;
 
  	return realm_tear_down_rtt_range(realm, 0, (1UL << ia_bits));
  }
@@ -191,6 +403,154 @@ int kvm_realm_teardown_stage2(struct kvm *kvm)
  	return 0;
  }
 
+static void realm_unmap_shared_range(struct kvm *kvm,
+				     unsigned long start,
+				     unsigned long end,
+				     bool may_block)
+{
+	struct realm *realm = &kvm->arch.realm;
+	unsigned long rd = virt_to_phys(realm->rd);
+	unsigned long next_addr, addr;
+	unsigned long shared_bit = BIT(realm->ia_bits - 1);
+
+	if (start >= end)

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

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

Could this overflow "end" to beyond ipa_size ? Why are we not using

		addr |= shared_bit;

?

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

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

I am wondering if this should be wrapped in a

	ret = rmi_rtt_unprot_unmap_single(rd, addr, end, &next_addr);

But, we might replace these with an ADDR_TYPE_LIST in the near future, so may be not.

Quoted 45 lines
arch/arm64/kvm/rmi.c +45 -0
+		if (WARN_ON(ret < 0))
+			return;
+
+		switch (RMI_RESULT_STATUS(ret)) {
+		case RMI_SUCCESS:
+			break;
+		case RMI_ERROR_RTT: {
+			int err_level = RMI_RESULT_DATA_LEVEL(ret);
+			int level = find_map_level(realm, addr, end);
+
+			if (WARN_ON(err_level > KVM_PGTABLE_LAST_LEVEL))
+				return;
+
+			if (err_level >= level) {
+				/* Nothing present, so skip */
+				next_addr = ALIGN(addr + 1,
+						  kvm_granule_size(err_level));
+				break;
+			}
+
+			ret = realm_create_rtt_levels(realm, addr, err_level,
+						      level, NULL);
+			if (WARN_ON(ret))
+				return;
+			/* Retry with the RTT levels in place */
+			next_addr = addr;
+			break;
+		}
+		default:
+			WARN_ON(1);
+			return;
+		}
+
+		if (may_block)
+			cond_resched_rwlock_write(&kvm->mmu_lock);
+	}
+
+	realm_fold_rtt_level(realm, get_start_level(realm) + 1,
+			     start, end);
+}
+
+static void realm_unmap_private_range(struct kvm *kvm,
+				      unsigned long start,
+				      unsigned long end,
+				      bool may_block)

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

Quoted 10 lines
arch/arm64/kvm/rmi.c +10 -0
+{
+	struct realm *realm = &kvm->arch.realm;
+	unsigned long rd = virt_to_phys(realm->rd);
+	unsigned long next_addr, addr;
+	long ret;
+
+	for (addr = start; addr < end; addr = next_addr) {
+		unsigned long out_range;
+		unsigned long flags = RMI_ADDR_TYPE_SINGLE;
+		/* TODO: Optimise using RMI_ADDR_TYPE_LIST */

With RMI_ADDR_TYPE_LIST support, I think we may be able to merge realm_unmap_shared_range() with this, by simply selecting the call data_unmap vs unprot_unmap

Quoted 47 lines
arch/arm64/kvm/rmi.c +47 -0
+
+		ret = rmi_rtt_data_unmap(rd, addr, end, flags, 0,
+					 &next_addr, &out_range, NULL);
+		if (WARN_ON(ret < 0))
+			return;
+
+		switch (RMI_RESULT_STATUS(ret)) {
+		case RMI_SUCCESS:
+			ret = undelegate_range_desc(out_range);
+			if (WARN_ON(ret))
+				return;
+			break;
+		case RMI_ERROR_RTT: {
+			int err_level = RMI_RESULT_DATA_LEVEL(ret);
+			int level = find_map_level(realm, addr, end);
+
+			if (WARN_ON(err_level > KVM_PGTABLE_LAST_LEVEL))
+				return;
+
+			if (err_level >= level) {
+				/* Nothing present, so skip this RTT entry. */
+				next_addr = ALIGN(addr + 1,
+						  kvm_granule_size(err_level));
+				break;
+			}
+
+			ret = realm_create_rtt_levels(realm, addr, err_level,
+						      level, NULL);
+			if (WARN_ON(ret))
+				return;
+			/* Retry with the RTT levels in place. */
+			next_addr = addr;
+			break;
+		}
+		default:
+			WARN_ON(1);
+			return;
+		}
+
+		if (may_block)
+			cond_resched_rwlock_write(&kvm->mmu_lock);
+	}
+
+	realm_fold_rtt_level(realm, get_start_level(realm) + 1,
+			     start, end);
+}
+

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

Quoted 10 lines
arch/arm64/kvm/rmi.c +10 -0
+void kvm_realm_unmap_range_filter(struct kvm *kvm, unsigned long start,
+				  unsigned long size, bool may_block,
+				  enum kvm_gfn_range_filter attr_filter)
+{
+	unsigned long end = start + size;
+	struct realm *realm = &kvm->arch.realm;
+
+	if (!kvm_realm_is_created(kvm))
+		return;
+

Do we need to ensure we get the canonical GPA for start here ?

Suzuki

Quoted 62 lines
arch/arm64/kvm/rmi.c +8 -0
+	end = min(BIT(realm->ia_bits - 1), end);
+
+	if (attr_filter & KVM_FILTER_SHARED)
+		realm_unmap_shared_range(kvm, start, end, may_block);
+	if (attr_filter & KVM_FILTER_PRIVATE)
+		realm_unmap_private_range(kvm, start, end, may_block);
+}
+
  void kvm_destroy_realm(struct kvm *kvm)
  {
  	struct realm *realm = &kvm->arch.realm;
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 6f43eac7c7be..71ba94eb72c0 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;

#25 lore raw

Re: [PATCH v2 08/12] KVM: arm64: CCA: Add Realm RTT mapping and invalidation helpers

Quoted 63 lines · Suzuki K Poulose
Quoted 62 lines · Steven Price

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

---
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             | 378 ++++++++++++++++++++++++++++++- drivers/firmware/arm_rmm/rmi.c   |  10 - include/linux/arm-rmi-cmds.h     |   8 +

  4 files changed, 385 insertions(+), 19 deletions(-)
arch/arm64/include/ renamed from arch/arm64/include/asm/kvm_rmi.h +0 -0

asm/kvm_rmi.h
index 58d825e40bdf..e64c3bcce160 100644

arch/arm64/include/asm/kvm_rmi.h +7 -0
@@ -81,6 +81,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 +3 -7
index b904e6541fb0..5aa34d68de59 100644
@@ -11,14 +11,6 @@
   #include <asm/kvm_pgtable.h>
   #include <asm/virt.h>
   -static inline unsigned long rmi_rtt_level_mapsize(int level)
-{
-    if (WARN_ON(level > KVM_PGTABLE_LAST_LEVEL))
-        return PAGE_SIZE;
-
-    return (1UL << ARM64_HW_PGTABLE_LEVEL_SHIFT(level));
-}
-
   u32 kvm_rmm_ipa_limit(void)
   {
       return u64_get_bits(rmi_feat_reg(0), RMI_FEATURE_REGISTER_0_S2SZ);
@@ -29,6 +21,75 @@ static int get_start_level(struct realm *realm)
       return 4 - max(2, stage2_pgtable_levels(realm->ia_bits));
   }
   +static int find_map_level(struct realm *realm,
+              unsigned long start,
+              unsigned long end)
+{
Quoted 8 lines

arch/arm64/kvm/rmi.c static int get_start_level(struct realm *realm) · line 30

Quoted 3 lines
arch/arm64/kvm/rmi.c +3 -0
+    int level = KVM_PGTABLE_LAST_LEVEL;
+
+    while (level > get_start_level(realm)) {

minor nit: Could we save the start_level in a local variable. The generated assembly seems to be evaluating get_start_level() for each iteration. For that matter, I don't mind caching this in struct realm, as we end up calculating it quite often.

Hmm, I'm not sure why your compiler is calculting it once per loop - the compiler should be able to see that get_start_level() is pure and there's no write to realm. But pulling it out into a local variable seems reasonable for clarity.

Quoted 151 lines
Quoted 151 lines
arch/arm64/kvm/rmi.c +62 -0
+        unsigned long map_size = kvm_granule_size(level - 1);
+
+        if (!IS_ALIGNED(start, map_size) ||
+            (start + map_size) > end)
+            break;
+
+        level--;
+    }
+
+    return level;
+}
+
+static int undelegate_range_desc(unsigned long desc)
+{
+    unsigned long size =
 rmi_addr_block_size_to_bytes(RMI_ADDR_RANGE_BLOCK_SIZE(desc));
+    unsigned long count = RMI_ADDR_RANGE_COUNT(desc);
+    unsigned long addr = RMI_ADDR_RANGE_ADDR(desc);
+    unsigned long state = RMI_ADDR_RANGE_STATE(desc);
+
+    if (state == RMI_OP_MEM_UNDELEGATED)
+        return 0;
+
+    if (size * count == 0)
+        return 0;
+
+    return rmi_undelegate_range(addr, size * count);
+}
+
+static phys_addr_t alloc_delegated_granule(struct
 kvm_mmu_memory_cache *mc)
+{
+    phys_addr_t phys;
+    void *virt;
+
+    if (mc) {
+        virt = kvm_mmu_memory_cache_alloc(mc);
+    } else {
+        virt = (void *)__get_free_page(GFP_ATOMIC | __GFP_ZERO |
+                           __GFP_ACCOUNT);
+    }
+
+    if (!virt)
+        return PHYS_ADDR_MAX;
+
+    phys = virt_to_phys(virt);
+    if (rmi_delegate_page(phys)) {
+        free_page((unsigned long)virt);
+        return PHYS_ADDR_MAX;
+    }
+
+    return phys;
+}
+
+static phys_addr_t alloc_rtt(struct kvm_mmu_memory_cache *mc)
+{
+    phys_addr_t phys = alloc_delegated_granule(mc);
+
+    if (phys != PHYS_ADDR_MAX)
+        kvm_account_pgtable_pages(phys_to_virt(phys), 1);
+
+    return phys;
+}
+
   static void free_rtt(phys_addr_t phys)

  { kvm_account_pgtable_pages(phys_to_virt(phys), -1);

arch/arm64/kvm/rmi.c +46 -0
@@ -36,6 +97,32 @@ static void free_rtt(phys_addr_t phys)
           kvm_account_pgtable_pages(phys_to_virt(phys), 1);
   }
   +static long realm_rtt_create(struct realm *realm,
+                 unsigned long addr,
+                 int level,
+                 phys_addr_t phys)
+{
+    addr = ALIGN_DOWN(addr, kvm_granule_size(level - 1));
+    return rmi_rtt_create(virt_to_phys(realm->rd), phys, addr, level);
+}
+
+static long realm_rtt_fold(struct realm *realm,
+               unsigned long addr,
+               int level,
+               phys_addr_t *rtt_granule)
+{
+    unsigned long out_rtt;
+    long ret;
+
+    addr = ALIGN_DOWN(addr, kvm_granule_size(level - 1));
+    ret = rmi_rtt_fold(virt_to_phys(realm->rd), addr, level, &out_rtt);
+
+    if (ret == RMI_SUCCESS && rtt_granule)
+        *rtt_granule = out_rtt;
+
+    return ret;
+}
+
   /*
    * realm_rtt_destroy - Destroy an RTT at @level for @addr.
    *
@@ -60,6 +147,47 @@ static long realm_rtt_destroy(struct realm *realm,
 unsigned long addr,
       return ret;
   }
   +static int realm_create_rtt_levels(struct realm *realm,
+                   unsigned long ipa,
+                   int level,
+                   int max_level,
+                   struct kvm_mmu_memory_cache *mc)
+{
+    long ret = 0;
+    phys_addr_t rtt = PHYS_ADDR_MAX;
+
+    while (level++ < max_level) {
+        if (rtt == PHYS_ADDR_MAX)
+            rtt = alloc_rtt(mc);
+        if (rtt == PHYS_ADDR_MAX)
+            return -ENOMEM;
+
+        ret = realm_rtt_create(realm, ipa, level, rtt);
+        if (ret < 0)
+            break;
+
+        if (RMI_RESULT_STATUS(ret) == RMI_ERROR_RTT &&
+            RMI_RESULT_DATA_LEVEL(ret) == level - 1) {
+            /* The RTT already exists, reuse the granule at the next

level. */
+            ret = 0;
+            continue;
+        }

arch/arm64/kvm/rmi.c +16 -0
+
+        if (ret) {
+            WARN(1, "Failed to create RTT at level %d: %ld\n",
+                 level, ret);
+            ret = -ENXIO;
+            break;
+        }
+        rtt = PHYS_ADDR_MAX;
+    }
+
+    if (rtt != PHYS_ADDR_MAX)
+        free_rtt(rtt);
+
+    return ret;
+}
+

  static int realm_tear_down_rtt_level(struct realm *realm, int level, unsigned long start, unsigned long end) {

arch/arm64/kvm/rmi.c +0 -0
@@ -69,7 +197,9 @@ static int realm_tear_down_rtt_level(struct realm
 *realm, int level,
       if (WARN_ON(level > KVM_PGTABLE_LAST_LEVEL))
Quoted 8 lines
Quoted 6 lines
arch/arm64/kvm/rmi.c +3 -0
           return -EINVAL;
   -    map_size = rmi_rtt_level_mapsize(level - 1);
+    map_size = kvm_granule_size(level - 1);
+    if (WARN_ON(!IS_ALIGNED(start, map_size)))
+        return -EINVAL;

I see, as discussed please move it to the other patch.

Ack.

Quoted 82 lines
Quoted 19 lines

      for (addr = start; addr < end; addr = next_addr) { phys_addr_t rtt_granule;

arch/arm64/kvm/rmi.c +0 -0
@@ -156,12 +286,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 +8 -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))

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

Quoted 10 lines
arch/arm64/kvm/rmi.c +10 -0
+        return -EINVAL;
+
+    map_size = kvm_granule_size(level - 1);
+
+    for (addr = start; addr < end; addr = next_addr) {
+        phys_addr_t rtt_granule;
+        long ret;
+
+        next_addr = ALIGN(addr + 1, map_size);
+

retry:        // See below

Quoted 24 lines

+        ret = realm_rtt_fold(realm, addr, level, &rtt_granule); +        if (ret < 0)
+            return ret;

arch/arm64/kvm/rmi.c +21 -0
+
+        switch (RMI_RESULT_STATUS(ret)) {
+        case RMI_SUCCESS:
+            free_rtt(rtt_granule);
+            break;
+        case RMI_ERROR_RTT: {
+            int err_level = RMI_RESULT_DATA_LEVEL(ret);
+
+            if (err_level < level) {
+                /* No RTT at this level; skip the missing ancestor. */
+                if (WARN_ON(err_level < get_start_level(realm)))
+                    return -EINVAL;
+                next_addr = ALIGN(addr + 1,
+                          kvm_granule_size(err_level - 1));
+                break;
+            }
+            if (level == KVM_PGTABLE_LAST_LEVEL) {
+                not_folded++;
+                break;
+            }
+            /* Recurse a level deeper */

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

Quoted 10 lines
arch/arm64/kvm/rmi.c +10 -0
+            ret = realm_fold_rtt_level(realm,
+                           level + 1,
+                           addr,
+                           min(next_addr, end));
+            if (ret < 0)
+                return ret;
+            if (ret) {
+                not_folded++;
+                break;
+            }

---8>---

Quoted 4 lines

+            /* Try once more after folding the child RTTs. */ +            ret = realm_rtt_fold(realm, addr, level, &rtt_granule); +            if (ret < 0)
+                return ret;

Quoted 8 lines

+            if (RMI_RESULT_STATUS(ret) == RMI_SUCCESS) { +                free_rtt(rtt_granule);
+                break;
+            }
+            if (RMI_RESULT_STATUS(ret) != RMI_ERROR_RTT) { +                WARN_ON(1);
+                return -ENXIO;
+            }

Quoted 4 lines

---<8---

This seems like a repeat of the code above, could we do :
        goto retry;

I guess it depends on how much we "trust" the RMM/Linux not to be buggy. The "repeat" is subtly different in that a second failure to fold handles RMI_ERROR_RTT by continuing with the next entry rather than retrying again. This prevents the code getting stuck in a loop if recursing didn't help.

Quoted 49 lines
Quoted 40 lines

+            not_folded++;
+            break;
+        }
+        default:
+            WARN_ON(1);
+            return -ENXIO;
+        }
+    }

arch/arm64/kvm/rmi.c +4 -0
+
+    return not_folded;
+}
+

  static int realm_destroy_rtts(struct kvm *kvm)
  {
      struct realm *realm = &kvm->arch.realm;
      unsigned int ia_bits = realm->ia_bits; lockdep_assert_held(&kvm->arch.config_lock);
+    if (WARN_ON_ONCE(!realm->stage2_unmapped))
+        return -EINVAL;
        return realm_tear_down_rtt_range(realm, 0, (1UL << ia_bits)); }

arch/arm64/kvm/rmi.c +15 -0
@@ -191,6 +403,154 @@ int kvm_realm_teardown_stage2(struct kvm *kvm)
       return 0;
   }
   +static void realm_unmap_shared_range(struct kvm *kvm,
+                     unsigned long start,
+                     unsigned long end,
+                     bool may_block)
+{
+    struct realm *realm = &kvm->arch.realm;
+    unsigned long rd = virt_to_phys(realm->rd);
+    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
Quoted 6 lines

end at
+     * shared_bit advances to the end of the IPA space. +     */
+    if (start < shared_bit) {
+        start += shared_bit;
+        end += shared_bit;

Could this overflow "end" to beyond ipa_size ? Why are we not using

Quoted 3 lines

        addr |= shared_bit;

?

The problem with doing an OR is in the case where we want to handle until the end of the protected region, i.e.:

start = 0
end = shared_bit

That currently ends up with:

start = shared_bit;
end = shared_bit + shared_bit (or top of IPA).

I guess we could OR in the shared_bit every time we use "addr", but I think this would make the loop below overly complex, especially since we have "next_addr" coming back from the RMM.

Ultimately the callers already clamp "end" - kvm_realm_unmap_range_filter() clamps it to shared_bit, and realm_map_unprotected() passes a shared address in so doesn't hit this addition case.

Quoted 10 lines

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

Quoted 7 lines

+    }

arch/arm64/kvm/rmi.c +6 -0
+
+    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);

I am wondering if this should be wrapped in a

Quoted 4 lines

    ret = rmi_rtt_unprot_unmap_single(rd, addr, end, &next_addr);

But, we might replace these with an ADDR_TYPE_LIST in the near future, so may be not.

I have tried to keep the wrappers matching the RMM spec - as I find it confusing matching up with the spec otherwise. So far I haven't actually found a use for SMC_RMI_RTT_UNPROT_UNMAP returning the output addresses. So logically we could remove those fields, but then it looks odd when compared against the spec. Given this is (currently) the only call site it seems unnecessary to have a wrapper.

Quoted 51 lines
Quoted 51 lines

+        if (WARN_ON(ret < 0))
+            return;

arch/arm64/kvm/rmi.c +49 -0
+
+        switch (RMI_RESULT_STATUS(ret)) {
+        case RMI_SUCCESS:
+            break;
+        case RMI_ERROR_RTT: {
+            int err_level = RMI_RESULT_DATA_LEVEL(ret);
+            int level = find_map_level(realm, addr, end);
+
+            if (WARN_ON(err_level > KVM_PGTABLE_LAST_LEVEL))
+                return;
+
+            if (err_level >= level) {
+                /* Nothing present, so skip */
+                next_addr = ALIGN(addr + 1,
+                          kvm_granule_size(err_level));
+                break;
+            }
+
+            ret = realm_create_rtt_levels(realm, addr, err_level,
+                              level, NULL);
+            if (WARN_ON(ret))
+                return;
+            /* Retry with the RTT levels in place */
+            next_addr = addr;
+            break;
+        }
+        default:
+            WARN_ON(1);
+            return;
+        }
+
+        if (may_block)
+            cond_resched_rwlock_write(&kvm->mmu_lock);
+    }
+
+    realm_fold_rtt_level(realm, get_start_level(realm) + 1,
+                 start, end);
+}
+
+static void realm_unmap_private_range(struct kvm *kvm,
+                      unsigned long start,
+                      unsigned long end,
+                      bool may_block)
+{
+    struct realm *realm = &kvm->arch.realm;
+    unsigned long rd = virt_to_phys(realm->rd);
+    unsigned long next_addr, addr;
+    long ret;
+
Quoted 8 lines
Quoted 4 lines

+    for (addr = start; addr < end; addr = next_addr) { +        unsigned long out_range;
+        unsigned long flags = RMI_ADDR_TYPE_SINGLE; +        /* TODO: Optimise using RMI_ADDR_TYPE_LIST */

With RMI_ADDR_TYPE_LIST support, I think we may be able to merge realm_unmap_shared_range() with this, by simply selecting the call data_unmap vs unprot_unmap

Possibly. The two functions are quite similar, but there are a number of difference in behaviour:

* Shared currently deals with the shared bit alias (as above), but
private doesn't.

* Private requires getting the physical addresses back and undelegating
them, shared doesn't.

* In my tree for v3 I've now got recovery for the case where memory
pressure prevents creating RTTs. For shared we can safely just unmap a block, for private doing so will kill the VM (so I've got a KVM_BUG_ON()).

RMI_ADDR_TYPE_LIST will make the situation slightly more complex as the private case will require walking that list - although we might be able to hide all that in a helper function like undelegate_range_desc().

Quoted 51 lines
Quoted 51 lines
arch/arm64/kvm/rmi.c +51 -0
+
+        ret = rmi_rtt_data_unmap(rd, addr, end, flags, 0,
+                     &next_addr, &out_range, NULL);
+        if (WARN_ON(ret < 0))
+            return;
+
+        switch (RMI_RESULT_STATUS(ret)) {
+        case RMI_SUCCESS:
+            ret = undelegate_range_desc(out_range);
+            if (WARN_ON(ret))
+                return;
+            break;
+        case RMI_ERROR_RTT: {
+            int err_level = RMI_RESULT_DATA_LEVEL(ret);
+            int level = find_map_level(realm, addr, end);
+
+            if (WARN_ON(err_level > KVM_PGTABLE_LAST_LEVEL))
+                return;
+
+            if (err_level >= level) {
+                /* Nothing present, so skip this RTT entry. */
+                next_addr = ALIGN(addr + 1,
+                          kvm_granule_size(err_level));
+                break;
+            }
+
+            ret = realm_create_rtt_levels(realm, addr, err_level,
+                              level, NULL);
+            if (WARN_ON(ret))
+                return;
+            /* Retry with the RTT levels in place. */
+            next_addr = addr;
+            break;
+        }
+        default:
+            WARN_ON(1);
+            return;
+        }
+
+        if (may_block)
+            cond_resched_rwlock_write(&kvm->mmu_lock);
+    }
+
+    realm_fold_rtt_level(realm, get_start_level(realm) + 1,
+                 start, end);
+}
+
+void kvm_realm_unmap_range_filter(struct kvm *kvm, unsigned long start,
+                  unsigned long size, bool may_block,
+                  enum kvm_gfn_range_filter attr_filter)
+{
Quoted 8 lines

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

Quoted 6 lines

+    unsigned long end = start + size;
+    struct realm *realm = &kvm->arch.realm;

arch/arm64/kvm/rmi.c +4 -0
+
+    if (!kvm_realm_is_created(kvm))
+        return;
+

Do we need to ensure we get the canonical GPA for start here ?

The below capping of "end" means that the GPA must be in the lower half of the address range. Otherwise this effectively becomes a no-op. We enforce that the memslots are in this lower half so we should always be getting canonical GPAs here.

Thanks,
Steve

Quoted 69 lines

Suzuki

Quoted 63 lines

+    end = min(BIT(realm->ia_bits - 1), end);

arch/arm64/kvm/rmi.c +7 -0
+
+    if (attr_filter & KVM_FILTER_SHARED)
+        realm_unmap_shared_range(kvm, start, end, may_block);
+    if (attr_filter & KVM_FILTER_PRIVATE)
+        realm_unmap_private_range(kvm, start, end, may_block);
+}
+

  void kvm_destroy_realm(struct kvm *kvm)
  {
      struct realm *realm = &kvm->arch.realm;

drivers/firmware/ renamed from drivers/firmware/arm_rmm/rmi.c +0 -0

arm_rmm/rmi.c
index 77255a90e0a6..a9f42c9a4f79 100644

drivers/firmware/arm_rmm/rmi.c +0 -1
@@ -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)

drivers/firmware/arm_rmm/rmi.c +0 -4
-{
-    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 +0 -0
index 6f43eac7c7be..71ba94eb72c0 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)

include/linux/arm-rmi-cmds.h +4 -0
+{
+    return BIT(ARM64_HW_PGTABLE_LEVEL_SHIFT(3 - block_size));
+}
+

  struct rmi_sro_state {
      struct arm_smccc_1_2_regs regs;
      int addr_count;

#26 lore raw

PATCH v2 9/12 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 be014f6c00e7..8c29ed204d61 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
#27 lore raw

PATCH v2 10/12 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 any changes to guest_memfd backed memslots, as the private memory is provided by slots backed by gmem. This will also be validated when we handle a fault for Realm Stage2 (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.

A rejected deletion can already have invalidated the memslot before the prepare hook rejects it. Keep private mappings intact in this case.

Additionally, similar to pKVM, we 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 d672d87ffe62..a82b18a3e4e5 100644
@@ -1612,6 +1612,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 8c29ed204d61..e550360fc94d 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
#28 lore raw

Re: [PATCH v2 10/12] KVM: arm64: CCA: Prevent memslot modifications after Realm creation

Thank you for your contribution! Sashiko AI review found 2 potential issues to consider: - [Medium] KVM: arm64: non-Realm VMs bypass guest_memfd mmap and private memory checks - [Low] KVM: arm64…

Thank you for your contribution! Sashiko AI review found 2 potential issues to consider:
- [Medium] KVM: arm64: non-Realm VMs bypass guest_memfd mmap and private memory checks
- [Low] KVM: arm64: stale memslot invalidation claim and missing macro in commit log
--

commit 72b617a00596c14c4599f4a8b9b980bcb6c02644
Author: Suzuki K Poulose <[email protected]>

Quoted 14 lines

KVM: arm64: CCA: Prevent memslot modifications after Realm creation

Once the Realm is created, prevent any changes to guest_memfd backed memslots, as the private memory is provided by slots backed by gmem. This will also be validated when we handle a fault for Realm Stage2 (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.

A rejected deletion can already have invalidated the memslot before the prepare hook rejects it. Keep private mappings intact in this case.

Quoted 5 lines

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

[Severity: Low]
Is the paragraph about a rejected deletion invalidating the memslot before the prepare hook rejects it left over from an earlier version? This patch rejects guest_memfd memslot changes in kvm_arch_validate_memory_region(), which commit "KVM: Allow architectures to reject memslot changes before invalidation" runs before kvm_invalidate_memslot().

Also, the commit message does not mention defining kvm_arch_has_private_mem(kvm) for Realm VMs.

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

Quoted 9 lines
arch/arm64/include/asm/kvm_host.h +1 -0
index d672d87ffe620..a82b18a3e4e5d 100644
@@ -1612,6 +1612,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)

[Severity: Medium]
Does defining kvm_arch_has_private_mem(kvm) as kvm_vm_is_realm(kvm) cause non-Realm VMs to bypass guest_memfd mmap and private memory checks?

Because kvm_arch_has_private_mem is defined, include/linux/kvm_host.h sets gmem_in_place_conversion to true for all arm64 VMs, so kvm_gmem_prepare_memory_region() sets KVM_MEMSLOT_GMEM_ONLY on every guest_memfd memslot even without GUEST_MEMFD_FLAG_MMAP.

However, commit "KVM: arm64: guest_memfd: Check mmap support when preparing memslot" gated the !kvm_gmem_slot_supports_mmap(new) check in kvm_arch_prepare_memory_region() on kvm_arch_has_private_mem(kvm), and commit "KVM: arm64: CCA: Support runtime faulting of memory" likewise gates the kvm_gmem_is_private_gfn() check in gmem_abort() on kvm_arch_has_private_mem(kvm). As a result, a non-Realm VM can bind a guest_memfd created without GUEST_MEMFD_FLAG_MMAP or GUEST_MEMFD_FLAG_INIT_SHARED and fault pages marked KVM_MEMORY_ATTRIBUTE_PRIVATE into its shared stage-2 page tables.

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

PATCH v2 11/12 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 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                 | 115 +++++++++++--
 arch/arm64/kvm/rmi.c                 | 235 +++++++++++++++++++++++++++
 5 files changed, 353 insertions(+), 20 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 e64c3bcce160..d1ba0c325bb0 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>
 
@@ -89,6 +90,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 +97 -18

233 lines not on this page · raw message

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

241 lines not on this page · raw message

Signature · 1 line
2.43.0
#30 lore raw

Re: [PATCH v2 11/12] KVM: arm64: CCA: Support runtime faulting of memory

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

Thank you for your contribution! Sashiko AI review found 6 potential issues to consider:
- [Medium] KVM: arm64: KVM_CAP_PRE_FAULT_MEMORY advertised for Realm VMs
- [Medium] KVM: arm64: cca: user-triggerable WARN_ON and retry bug in realm_map_protected()
- [Medium] KVM: arm64: cca: bound block level in addr_range_desc() by start level
- [Low] KVM: arm64: unhandled exec fault on shared Realm IPA in gmem_abort() and user_mem_abort()
- [Low] KVM: arm64: dead code and missing memory fault exit in kvm_vm_mem_abort()
- [Low] KVM: arm64: unreachable dead code in kvm_fault_is_gmem_abort() --

commit 7a04ef54472b7db03df4200b280f4db2cc49ed58
Author: Steven Price <[email protected]>

KVM: arm64: CCA: Support runtime faulting of memory

This commit adds stage-2 runtime fault handling for Realm VMs, mapping private guest_memfd pages into the protected IPA range via the RMM and mapping shared guest_memfd or normal memslot pages into the unprotected IPA range.

Quoted 4 lines
arch/arm64/kvm/mmu.c +0 -0
index e550360fc94d0..b6c61d7890c89 100644

[ ... ]

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

Quoted 13 lines
arch/arm64/kvm/mmu.c +9 -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;
+	}

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

If the answer is yes, unprotected Realm mappings cannot grant execute permission because RMI_RTT_UNPROT_MAP only encodes RMI_S2AP_DIRECT_READ and RMI_S2AP_DIRECT_WRITE, yet neither gmem_abort() nor user_mem_abort() checks kvm_s2_fault_is_exec(s2fd) on a shared IPA to return -ENOEXEC (so kvm_handle_guest_abort() can inject an instruction abort via kvm_inject_sea_iabt()). Instead, realm_map_unprotected() installs a non-executable mapping and returns 0, and on subsequent instruction permission faults at that IPA, unmaps and remaps the page with the same non-executable permissions in an endless fault loop.

[ ... ]

Quoted 16 lines
arch/arm64/kvm/mmu.c +12 -0
@@ -2340,13 +2391,40 @@ static int user_mem_abort(const struct kvm_s2_fault_desc *s2fd,
 	return kvm_s2_fault_map(s2fd, &s2vi, prot, memcache, result);
 }
 
+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);
+}
+
+/* Decide if an abort should be handled by the gmem */
+static bool kvm_fault_is_gmem_abort(struct kvm *kvm,
+				    const struct kvm_s2_fault_desc *s2fd)
+{
+	if (!kvm_slot_has_gmem(s2fd->memslot))
+		return false;

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

Quoted 10 lines
arch/arm64/kvm/mmu.c +10 -0
+	if (kvm_memslot_is_gmem_only(s2fd->memslot))
+		return true;
+	/*
+	 * For Realms, all private faults must be backed by GMEM.
+	 * TODO: Handle Trusted device private memory mappings.
+	 */
+	if (private_ipa_fault(kvm, s2fd->fault_ipa))
+		return true;
+	return false;
+}

[Severity: Low]
Is the private_ipa_fault() check in kvm_fault_is_gmem_abort() reachable?

On arm64, include/linux/kvm_host.h defines gmem_in_place_conversion as true because kvm_arch_has_private_mem is defined without CONFIG_KVM_PRIVATE_MEMATTR_VM, so kvm_gmem_prepare_memory_region() sets KVM_MEMSLOT_GMEM_ONLY on every guest_memfd-backed memslot:

virt/kvm/guest_memfd.c:kvm_gmem_prepare_memory_region() {
    ...
	if (gmem_in_place_conversion || kvm_gmem_supports_mmap(inode))
		slot->flags |= KVM_MEMSLOT_GMEM_ONLY;
    ...
}

Once kvm_slot_has_gmem(s2fd->memslot) is true, kvm_memslot_is_gmem_only(s2fd->memslot) always returns true, making the rest of kvm_fault_is_gmem_abort() unreachable and the helper equivalent to kvm_slot_has_gmem(s2fd->memslot).

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

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

Quoted 10 lines
arch/arm64/kvm/mmu.c +5 -1
 
-	if (kvm_slot_has_gmem(s2fd->memslot))
+	if (private_ipa_fault(s2fd->vcpu->kvm, s2fd->fault_ipa) &&
+	    !kvm_slot_has_gmem(s2fd->memslot))
+		return -EINVAL;
+
+	if (kvm_fault_is_gmem_abort(s2fd->vcpu->kvm, s2fd))
 		return gmem_abort(s2fd, NULL);
 	else
 		return user_mem_abort(s2fd, NULL);

[Severity: Low]
When a Realm guest faults on the private IPA alias of a non-guest_memfd memslot, should kvm_vm_mem_abort() call kvm_prepare_memory_fault_exit() and return -EFAULT instead of returning -EINVAL?

Returning -EINVAL leaves vcpu->run->exit_reason unpopulated and causes KVM_RUN to fail with EINVAL rather than reporting KVM_EXIT_MEMORY_FAULT (with KVM_MEMORY_EXIT_FLAG_PRIVATE) to userspace, and as noted above, kvm_fault_is_gmem_abort() is also redundant with kvm_slot_has_gmem() on arm64.

[ ... ]

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

Quoted 9 lines
arch/arm64/kvm/mmu.c +1 -1
@@ -3156,7 +3235,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 gate KVM_CAP_PRE_FAULT_MEMORY on !kvm_vm_is_realm(kvm)?

kvm_arch_pre_fault_allowed() rejects KVM_PRE_FAULT_MEMORY on Realm VMs with -EOPNOTSUPP, but kvm_vm_ioctl_check_extension() in arch/arm64/kvm/arm.c still advertises KVM_CAP_PRE_FAULT_MEMORY as supported when queried on a Realm VM file descriptor.

Quoted 12 lines
arch/arm64/kvm/rmi.c +4 -0
index 5aa34d68de595..682ed7005d579 100644
@@ -551,6 +551,241 @@ void kvm_realm_unmap_range_filter(struct kvm *kvm, unsigned long start,
 		realm_unmap_private_range(kvm, start, end, may_block);
 }
 
+static unsigned long addr_range_desc(unsigned long ipa, unsigned long phys,
+				     unsigned long size, unsigned long state)
+{
+	unsigned long count = 1;

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

Quoted 10 lines
arch/arm64/kvm/rmi.c +10 -0
+	unsigned long out;
+	int rmi_size;
+
+	for (rmi_size = KVM_PGTABLE_LAST_LEVEL; rmi_size >= 0; rmi_size--) {
+		unsigned long entry_size = rmi_addr_block_size_to_bytes(rmi_size);
+
+		if (size >= entry_size && IS_ALIGNED(ipa, entry_size) &&
+		    IS_ALIGNED(phys, entry_size))
+			break;
+	}

[Severity: Medium]
Can addr_range_desc() select a block level above the Realm's RTT start level?

When a Realm VM is created with a 32..34-bit IPA range on 4KB pages, get_start_level(realm) is 2 (concatenated level-2 root RTTs). If the guest faults on a 1GB-aligned shared memslot backed by a 1GB hugetlbfs VMA, kvm_s2_resolve_vma_size() sets vma_pagesize to PUD_SIZE (1GB) and addr_range_desc() picks rmi_size = 2 (RTT level 1). In realm_map_unprotected(), req_level is then 1, which is above the root RTT level 2, causing rmi_rtt_unprot_map() to fail until retries are exhausted and terminating the fault with -ENXIO.

Quoted 27 lines
arch/arm64/kvm/rmi.c +27 -0
+
+	if (rmi_size == 0) {
+		/*
+		 * Stop at the next block boundary so that a later iteration can
+		 * use a larger mapping when both addresses are aligned.
+		 */
+		count = MIN(size >> PAGE_SHIFT, PTRS_PER_PTE - 1);
+		count = min(count, (PMD_SIZE - (ipa & (PMD_SIZE - 1))) >> PAGE_SHIFT);
+		count = min(count, (PMD_SIZE - (phys & (PMD_SIZE - 1))) >> PAGE_SHIFT);
+	}
+
+	WARN_ON(phys & ~PAGE_MASK);
+
+	out = FIELD_PREP(RMI_ADDR_RANGE_BLOCK_SIZE_MASK, rmi_size) |
+	      FIELD_PREP(RMI_ADDR_RANGE_COUNT_MASK, count) |
+	      FIELD_PREP(RMI_ADDR_RANGE_STATE_MASK, state);
+	out |= phys & PAGE_MASK;
+
+	return out;
+}
+
+static int realm_map_protected(struct kvm *kvm,
+			       unsigned long ipa,
+			       kvm_pfn_t pfn,
+			       unsigned long map_size,
+			       struct kvm_mmu_memory_cache *memcache)
+{

[ ... ]

Quoted 18 lines
arch/arm64/kvm/rmi.c +18 -0
+	while (ipa < ipa_top) {
+		unsigned long flags = RMI_ADDR_TYPE_SINGLE;
+		unsigned long range_desc = addr_range_desc(ipa, phys, ipa_top - ipa,
+							   RMI_OP_MEM_DELEGATED);
+		unsigned long out_top;
+
+		ret = rmi_rtt_data_map(rd, ipa, ipa_top, flags, range_desc,
+				       &out_top);
+		if (ret < 0)
+			goto err_undelegate;
+
+		if (RMI_RESULT_STATUS(ret) == RMI_ERROR_RTT) {
+			/* Create missing RTTs and retry */
+			int level = RMI_RESULT_DATA_LEVEL(ret);
+
+			if (WARN_ON(level >= KVM_PGTABLE_LAST_LEVEL))
+				goto err_undelegate;
+			ret = realm_create_rtt_levels(realm, ipa, level,

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

Quoted 10 lines
arch/arm64/kvm/rmi.c +10 -0
+						      level + 1,
+						      memcache);
+			if (ret)
+				goto err_undelegate;
+
+			continue;
+		}
+
+		if (WARN_ON(ret))
+			goto err_undelegate;

[Severity: Medium]
Can userspace or a Realm guest trigger WARN_ON(level >= KVM_PGTABLE_LAST_LEVEL) and WARN_ON(ret) in realm_map_protected()?

If userspace punches a hole in guest_memfd on an already-mapped protected page, realm_unmap_private_range() unmaps the page via rmi_rtt_data_unmap() (leaving the level-3 RTT entry in the DESTROYED state) and undelegates the folio while the guest_memfd range remains private. When a vCPU faults on that GPA again, gmem_abort() allocates a fresh undelegated folio and realm_map_protected() delegates it, but rmi_rtt_data_map() fails with RMI_ERROR_RTT at level 3 (or if the guest has not initialized RIPAS to RAM), hitting WARN_ON(level >= KVM_PGTABLE_LAST_LEVEL). Similarly, any non-RTT status such as RMI_BLOCKED hits WARN_ON(ret).

In addition, unlike realm_map_unprotected(), realm_map_protected() passes ipa_top instead of call_top to rmi_rtt_data_map(), checks KVM_PGTABLE_LAST_LEVEL and passes level + 1 instead of req_level to realm_create_rtt_levels(), and does not bound retries. If map_size > PAGE_SIZE is used, addr_range_desc() can describe a sub-range smaller than ipa_top - ipa (failing with RMI_ERROR_INPUT) or a block mapping where a child RTT at level + 1 already exists, causing realm_create_rtt_levels() to return 0 on RMI_ERROR_RTT and spin forever under write_lock(&kvm->mmu_lock).

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

Re: [PATCH v2 11/12] KVM: arm64: CCA: Support runtime faulting of memory

Quoted 52 lines · On 11/11/2026 19:34, [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 6 potential issues to consider:
- [Medium] KVM: arm64: KVM_CAP_PRE_FAULT_MEMORY advertised for Realm VMs
- [Medium] KVM: arm64: cca: user-triggerable WARN_ON and retry bug in realm_map_protected()
- [Medium] KVM: arm64: cca: bound block level in addr_range_desc() by start level
- [Low] KVM: arm64: unhandled exec fault on shared Realm IPA in gmem_abort() and user_mem_abort()
- [Low] KVM: arm64: dead code and missing memory fault exit in kvm_vm_mem_abort()
- [Low] KVM: arm64: unreachable dead code in kvm_fault_is_gmem_abort() --

commit 7a04ef54472b7db03df4200b280f4db2cc49ed58
Author: Steven Price <[email protected]>

KVM: arm64: CCA: Support runtime faulting of memory

This commit adds stage-2 runtime fault handling for Realm VMs, mapping private guest_memfd pages into the protected IPA range via the RMM and mapping shared guest_memfd or normal memslot pages into the unprotected IPA range.

Quoted 4 lines
arch/arm64/kvm/mmu.c +0 -0
index e550360fc94d0..b6c61d7890c89 100644

[ ... ]

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

Quoted 13 lines
arch/arm64/kvm/mmu.c +9 -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;
+	}

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

Quoted 9 lines

If the answer is yes, unprotected Realm mappings cannot grant execute permission because RMI_RTT_UNPROT_MAP only encodes RMI_S2AP_DIRECT_READ and RMI_S2AP_DIRECT_WRITE, yet neither gmem_abort() nor user_mem_abort() checks kvm_s2_fault_is_exec(s2fd) on a shared IPA to return -ENOEXEC (so kvm_handle_guest_abort() can inject an instruction abort via kvm_inject_sea_iabt()). Instead, realm_map_unprotected() installs a non-executable mapping and returns 0, and on subsequent instruction permission faults at that IPA, unmaps and remaps the page with the same non-executable permissions in an endless fault loop.

Realm guests should never execute from unprotected mappings (that would largely defeat the point of confidential compute). The spec covers this case:

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

So the fault isn't taken to the host.

Quoted 44 lines

[ ... ]

Quoted 16 lines
arch/arm64/kvm/mmu.c +12 -0
@@ -2340,13 +2391,40 @@ static int user_mem_abort(const struct kvm_s2_fault_desc *s2fd,
 	return kvm_s2_fault_map(s2fd, &s2vi, prot, memcache, result);
 }
 
+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);
+}
+
+/* Decide if an abort should be handled by the gmem */
+static bool kvm_fault_is_gmem_abort(struct kvm *kvm,
+				    const struct kvm_s2_fault_desc *s2fd)
+{
+	if (!kvm_slot_has_gmem(s2fd->memslot))
+		return false;

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

Quoted 10 lines
arch/arm64/kvm/mmu.c +10 -0
+	if (kvm_memslot_is_gmem_only(s2fd->memslot))
+		return true;
+	/*
+	 * For Realms, all private faults must be backed by GMEM.
+	 * TODO: Handle Trusted device private memory mappings.
+	 */
+	if (private_ipa_fault(kvm, s2fd->fault_ipa))
+		return true;
+	return false;
+}

[Severity: Low]
Is the private_ipa_fault() check in kvm_fault_is_gmem_abort() reachable?

On arm64, include/linux/kvm_host.h defines gmem_in_place_conversion as true because kvm_arch_has_private_mem is defined without CONFIG_KVM_PRIVATE_MEMATTR_VM, so kvm_gmem_prepare_memory_region() sets KVM_MEMSLOT_GMEM_ONLY on every guest_memfd-backed memslot:

virt/kvm/guest_memfd.c:kvm_gmem_prepare_memory_region() {
    ...
	if (gmem_in_place_conversion || kvm_gmem_supports_mmap(inode))
		slot->flags |= KVM_MEMSLOT_GMEM_ONLY;
    ...
}
Quoted 4 lines

Once kvm_slot_has_gmem(s2fd->memslot) is true, kvm_memslot_is_gmem_only(s2fd->memslot) always returns true, making the rest of kvm_fault_is_gmem_abort() unreachable and the helper equivalent to kvm_slot_has_gmem(s2fd->memslot).

Indeed it isn't reachable. All memslots are now gmem only. Which means this function is now simply kvm_slot_has_gmem() so I'll drop it.

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

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

Quoted 9 lines
arch/arm64/kvm/mmu.c +5 -1
-	if (kvm_slot_has_gmem(s2fd->memslot))
+	if (private_ipa_fault(s2fd->vcpu->kvm, s2fd->fault_ipa) &&
+	    !kvm_slot_has_gmem(s2fd->memslot))
+		return -EINVAL;
+
+	if (kvm_fault_is_gmem_abort(s2fd->vcpu->kvm, s2fd))
 		return gmem_abort(s2fd, NULL);
 	else
 		return user_mem_abort(s2fd, NULL);

[Severity: Low]
When a Realm guest faults on the private IPA alias of a non-guest_memfd memslot, should kvm_vm_mem_abort() call kvm_prepare_memory_fault_exit() and return -EFAULT instead of returning -EINVAL?

I don't think we can actually reach this path without protected device support (which will come later). Exposing this to the VM as a memory fault doesn't seem appropriate when we don't have the kernel support for handling protected devices.

Quoted 22 lines

Returning -EINVAL leaves vcpu->run->exit_reason unpopulated and causes KVM_RUN to fail with EINVAL rather than reporting KVM_EXIT_MEMORY_FAULT (with KVM_MEMORY_EXIT_FLAG_PRIVATE) to userspace, and as noted above, kvm_fault_is_gmem_abort() is also redundant with kvm_slot_has_gmem() on arm64.

[ ... ]

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

Quoted 9 lines
arch/arm64/kvm/mmu.c +1 -1
@@ -3156,7 +3235,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 gate KVM_CAP_PRE_FAULT_MEMORY on !kvm_vm_is_realm(kvm)?

Quoted 4 lines

kvm_arch_pre_fault_allowed() rejects KVM_PRE_FAULT_MEMORY on Realm VMs with -EOPNOTSUPP, but kvm_vm_ioctl_check_extension() in arch/arm64/kvm/arm.c still advertises KVM_CAP_PRE_FAULT_MEMORY as supported when queried on a Realm VM file descriptor.

False positive. kvm_realm_ext_allowed() already prevents kvm_vm_ioctl_check_extension() returning 1 for a realm VM.

Quoted 26 lines
Quoted 12 lines
arch/arm64/kvm/rmi.c +4 -0
index 5aa34d68de595..682ed7005d579 100644
@@ -551,6 +551,241 @@ void kvm_realm_unmap_range_filter(struct kvm *kvm, unsigned long start,
 		realm_unmap_private_range(kvm, start, end, may_block);
 }
 
+static unsigned long addr_range_desc(unsigned long ipa, unsigned long phys,
+				     unsigned long size, unsigned long state)
+{
+	unsigned long count = 1;

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

Quoted 10 lines
arch/arm64/kvm/rmi.c +10 -0
+	unsigned long out;
+	int rmi_size;
+
+	for (rmi_size = KVM_PGTABLE_LAST_LEVEL; rmi_size >= 0; rmi_size--) {
+		unsigned long entry_size = rmi_addr_block_size_to_bytes(rmi_size);
+
+		if (size >= entry_size && IS_ALIGNED(ipa, entry_size) &&
+		    IS_ALIGNED(phys, entry_size))
+			break;
+	}

[Severity: Medium]
Can addr_range_desc() select a block level above the Realm's RTT start level?

Quoted 8 lines

When a Realm VM is created with a 32..34-bit IPA range on 4KB pages, get_start_level(realm) is 2 (concatenated level-2 root RTTs). If the guest faults on a 1GB-aligned shared memslot backed by a 1GB hugetlbfs VMA, kvm_s2_resolve_vma_size() sets vma_pagesize to PUD_SIZE (1GB) and addr_range_desc() picks rmi_size = 2 (RTT level 1). In realm_map_unprotected(), req_level is then 1, which is above the root RTT level 2, causing rmi_rtt_unprot_map() to fail until retries are exhausted and terminating the fault with -ENXIO.

Valid point - I'll change the for loop to:

	for (rmi_size = KVM_PGTABLE_LAST_LEVEL - get_start_level(realm);
	     rmi_size >= 0; rmi_size--) {
Quoted 63 lines
Quoted 27 lines
arch/arm64/kvm/rmi.c +27 -0
+
+	if (rmi_size == 0) {
+		/*
+		 * Stop at the next block boundary so that a later iteration can
+		 * use a larger mapping when both addresses are aligned.
+		 */
+		count = MIN(size >> PAGE_SHIFT, PTRS_PER_PTE - 1);
+		count = min(count, (PMD_SIZE - (ipa & (PMD_SIZE - 1))) >> PAGE_SHIFT);
+		count = min(count, (PMD_SIZE - (phys & (PMD_SIZE - 1))) >> PAGE_SHIFT);
+	}
+
+	WARN_ON(phys & ~PAGE_MASK);
+
+	out = FIELD_PREP(RMI_ADDR_RANGE_BLOCK_SIZE_MASK, rmi_size) |
+	      FIELD_PREP(RMI_ADDR_RANGE_COUNT_MASK, count) |
+	      FIELD_PREP(RMI_ADDR_RANGE_STATE_MASK, state);
+	out |= phys & PAGE_MASK;
+
+	return out;
+}
+
+static int realm_map_protected(struct kvm *kvm,
+			       unsigned long ipa,
+			       kvm_pfn_t pfn,
+			       unsigned long map_size,
+			       struct kvm_mmu_memory_cache *memcache)
+{

[ ... ]

Quoted 18 lines
arch/arm64/kvm/rmi.c +18 -0
+	while (ipa < ipa_top) {
+		unsigned long flags = RMI_ADDR_TYPE_SINGLE;
+		unsigned long range_desc = addr_range_desc(ipa, phys, ipa_top - ipa,
+							   RMI_OP_MEM_DELEGATED);
+		unsigned long out_top;
+
+		ret = rmi_rtt_data_map(rd, ipa, ipa_top, flags, range_desc,
+				       &out_top);
+		if (ret < 0)
+			goto err_undelegate;
+
+		if (RMI_RESULT_STATUS(ret) == RMI_ERROR_RTT) {
+			/* Create missing RTTs and retry */
+			int level = RMI_RESULT_DATA_LEVEL(ret);
+
+			if (WARN_ON(level >= KVM_PGTABLE_LAST_LEVEL))
+				goto err_undelegate;
+			ret = realm_create_rtt_levels(realm, ipa, level,

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

Quoted 10 lines
arch/arm64/kvm/rmi.c +10 -0
+						      level + 1,
+						      memcache);
+			if (ret)
+				goto err_undelegate;
+
+			continue;
+		}
+
+		if (WARN_ON(ret))
+			goto err_undelegate;

[Severity: Medium]
Can userspace or a Realm guest trigger WARN_ON(level >= KVM_PGTABLE_LAST_LEVEL) and WARN_ON(ret) in realm_map_protected()?

Quoted 9 lines

If userspace punches a hole in guest_memfd on an already-mapped protected page, realm_unmap_private_range() unmaps the page via rmi_rtt_data_unmap() (leaving the level-3 RTT entry in the DESTROYED state) and undelegates the folio while the guest_memfd range remains private. When a vCPU faults on that GPA again, gmem_abort() allocates a fresh undelegated folio and realm_map_protected() delegates it, but rmi_rtt_data_map() fails with RMI_ERROR_RTT at level 3 (or if the guest has not initialized RIPAS to RAM), hitting WARN_ON(level >= KVM_PGTABLE_LAST_LEVEL). Similarly, any non-RTT status such as RMI_BLOCKED hits WARN_ON(ret).

If the guest hasn't reinitialized to RIPAS_RAM then the RMM wouldn't allow this. A realm guest hitting DESTROYED is fatal for the guest.

Quoted 8 lines

In addition, unlike realm_map_unprotected(), realm_map_protected() passes ipa_top instead of call_top to rmi_rtt_data_map(), checks KVM_PGTABLE_LAST_LEVEL and passes level + 1 instead of req_level to realm_create_rtt_levels(), and does not bound retries. If map_size > PAGE_SIZE is used, addr_range_desc() can describe a sub-range smaller than ipa_top - ipa (failing with RMI_ERROR_INPUT) or a block mapping where a child RTT at level + 1 already exists, causing realm_create_rtt_levels() to return 0 on RMI_ERROR_RTT and spin forever under write_lock(&kvm->mmu_lock).

That might well be valid though. I'll do the call_top dance here too.

Steve

#32 lore raw

Re: [PATCH v2 11/12] KVM: arm64: CCA: Support runtime faulting of memory

In reply to Steven Price
Quoted 115 lines · 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 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                 | 115 +++++++++++--
  arch/arm64/kvm/rmi.c                 | 235 +++++++++++++++++++++++++++
  5 files changed, 353 insertions(+), 20 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 e64c3bcce160..d1ba0c325bb0 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>
 
@@ -89,6 +90,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 +0 -0
index e550360fc94d..b6c61d7890c8 100644

arch/arm64/kvm/mmu.c static void stage2_flush_vm(struct kvm *kvm) · line 455

Quoted 6 lines
arch/arm64/kvm/mmu.c +3 -0
@@ -450,6 +450,10 @@ static void stage2_flush_vm(struct kvm *kvm)
  	struct kvm_memory_slot *memslot;
  	int idx, bkt;
 
+	/* The RMM requires FWB, so Realm stage-2 mappings need no cache flush. */
+	if (kvm_vm_is_realm(kvm))
+		return;

While this is true, it would be also worth mentioning KVM can't handle this for a Realm as the traps are handled within RMM. So this could be something like:

	/*
	 * The RMM mandates FEAT_FWB and additionally KVM can't handle
	 * such traps anyway.
	 */

arch/arm64/kvm/mmu.c void stage2_unmap_vm(struct kvm *kvm) · line 1229

Quoted 9 lines
arch/arm64/kvm/mmu.c +2 -0
+
  	idx = srcu_read_lock(&kvm->srcu);
  	write_lock(&kvm->mmu_lock);
 
@@ -1222,6 +1226,10 @@ void stage2_unmap_vm(struct kvm *kvm)
  	struct kvm_memory_slot *memslot;
  	int idx, bkt;
 
+	/* For realms this is handled by the RMM so nothing to do here */

minor nit: Might be better to add a similar comment as above.

	/*
	 * The RMM mandates FEAT_FWB and additionally KVM can't handle
	 * such traps anyway.
	 */

as we skip calling stage2_unmap_vm() if we have FWB anyways.

Quoted 7 lines
arch/arm64/kvm/mmu.c +3 -0
+	if (kvm_vm_is_realm(kvm))
+		return;
+
  	idx = srcu_read_lock(&kvm->srcu);
  	mmap_read_lock(current->mm);
  	write_lock(&kvm->mmu_lock);

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

Quoted 10 lines
arch/arm64/kvm/mmu.c +5 -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;
+
+	if (!perm_fault || kvm_arch_has_private_mem(kvm)) {

should this be s/kvm_arch_has_private_mem/kvm_vm_is_realm ? I dont think this would be required for pKVM yet ?

Also, I wonder why pKVM doesnt need mmu cache pages with permission faults with gmem. They do for user_mem_abort(). (may be because they assume Gmem is always single page ? in which case
we could also have the same behavior)

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

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

Quoted 4 lines
arch/arm64/kvm/mmu.c +1 -0
@@ -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)) {

--8>--

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

Quoted 4 lines
arch/arm64/kvm/mmu.c +4 -0
+		switch (kvm->arch.vm_flavor) {
+		case VM_REALM:
+			is_private = kvm_realm_is_private_address(&kvm->arch.realm,
+								  s2fd->fault_ipa);

Could we for now use

		is_private = private_ipa_fault(kvm, s2fd->fault_ipa);

In other words use a wrapper for detecting if a given ipa is private or not ? May be something like: kvm_ipa_is_private(kvm, ipa)? (choose ipa to make it explicit, it is arm64 thing).

Where, pKVM could hook in its own check.

Quoted 56 lines
arch/arm64/kvm/mmu.c +30 -2
+			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);
 

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

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

Please could we add a comment here, why we do this ? It is a subtle case.

Quoted 31 lines
arch/arm64/kvm/mmu.c +10 -7
  			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

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

Quoted 10 lines
arch/arm64/kvm/mmu.c +2 -1
  	 * 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)) {

kvm_vm_hyp_is_distrusting() ? More importantly, as commented in gmem_abort, I don't see why pKVM is inconsistent here.

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

Quoted 8 lines
arch/arm64/kvm/mmu.c +1 -0
  		ret = topup_mmu_memcache(s2fd->mmu, memcache);
  		if (ret)
  			return ret;
@@ -2340,13 +2391,40 @@ static int user_mem_abort(const struct kvm_s2_fault_desc *s2fd,
  	return kvm_s2_fault_map(s2fd, &s2vi, prot, memcache, result);
  }
 
+static bool private_ipa_fault(struct kvm *kvm, phys_addr_t fault_ipa)

See more on the naming above.

Quoted 22 lines
arch/arm64/kvm/mmu.c +22 -0
+{
+	return kvm_vm_is_realm(kvm) &&
+		kvm_realm_is_private_address(&kvm->arch.realm, fault_ipa);
+}
+
+/* Decide if an abort should be handled by the gmem */
+static bool kvm_fault_is_gmem_abort(struct kvm *kvm,
+				    const struct kvm_s2_fault_desc *s2fd)
+{
+	if (!kvm_slot_has_gmem(s2fd->memslot))
+		return false;
+	if (kvm_memslot_is_gmem_only(s2fd->memslot))
+		return true;
+	/*
+	 * For Realms, all private faults must be backed by GMEM.
+	 * TODO: Handle Trusted device private memory mappings.
+	 */
+	if (private_ipa_fault(kvm, s2fd->fault_ipa))
+		return true;
+	return false;
+}
+

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

Quoted 10 lines
arch/arm64/kvm/mmu.c +3 -1
  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 (kvm_slot_has_gmem(s2fd->memslot))
+	if (private_ipa_fault(s2fd->vcpu->kvm, s2fd->fault_ipa) &&
+	    !kvm_slot_has_gmem(s2fd->memslot))
+		return -EINVAL;

This looks like duplicated with kvm_fault_is_gmem_abort(). We could remove it may be ?

Quoted 6 lines
arch/arm64/kvm/mmu.c +2 -0
+
+	if (kvm_fault_is_gmem_abort(s2fd->vcpu->kvm, s2fd))
  		return gmem_abort(s2fd, NULL);
  	else
  		return user_mem_abort(s2fd, NULL);

arch/arm64/kvm/mmu.c int kvm_handle_guest_sea(struct kvm_vcpu *vcpu) · line 2464

Quoted 8 lines
arch/arm64/kvm/mmu.c +1 -1
@@ -2458,10 +2536,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;

Spurious change ?

Quoted 24 lines
arch/arm64/kvm/mmu.c +3 -2
  	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 +2632,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 +2677,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;
  	}
 

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

Quoted 6 lines
arch/arm64/kvm/mmu.c +1 -1
@@ -3156,7 +3235,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;

kvm_vm_hyp_is_distrusting() ?

Quoted 76 lines
arch/arm64/kvm/mmu.c +0 -0
  	if (!kvm_vcpu_initialized(vcpu))
  		return -ENOEXEC;
arch/arm64/kvm/rmi.c +66 -0
index 5aa34d68de59..682ed7005d57 100644
@@ -551,6 +551,241 @@ void kvm_realm_unmap_range_filter(struct kvm *kvm, unsigned long start,
  		realm_unmap_private_range(kvm, start, end, may_block);
  }
 
+static unsigned long addr_range_desc(unsigned long ipa, unsigned long phys,
+				     unsigned long size, unsigned long state)
+{
+	unsigned long count = 1;
+	unsigned long out;
+	int rmi_size;
+
+	for (rmi_size = KVM_PGTABLE_LAST_LEVEL; rmi_size >= 0; rmi_size--) {
+		unsigned long entry_size = rmi_addr_block_size_to_bytes(rmi_size);
+
+		if (size >= entry_size && IS_ALIGNED(ipa, entry_size) &&
+		    IS_ALIGNED(phys, entry_size))
+			break;
+	}
+
+	if (rmi_size == 0) {
+		/*
+		 * Stop at the next block boundary so that a later iteration can
+		 * use a larger mapping when both addresses are aligned.
+		 */
+		count = MIN(size >> PAGE_SHIFT, PTRS_PER_PTE - 1);
+		count = min(count, (PMD_SIZE - (ipa & (PMD_SIZE - 1))) >> PAGE_SHIFT);
+		count = min(count, (PMD_SIZE - (phys & (PMD_SIZE - 1))) >> PAGE_SHIFT);
+	}
+
+	WARN_ON(phys & ~PAGE_MASK);
+
+	out = FIELD_PREP(RMI_ADDR_RANGE_BLOCK_SIZE_MASK, rmi_size) |
+	      FIELD_PREP(RMI_ADDR_RANGE_COUNT_MASK, count) |
+	      FIELD_PREP(RMI_ADDR_RANGE_STATE_MASK, state);
+	out |= phys & PAGE_MASK;
+
+	return out;
+}
+
+static int realm_map_protected(struct kvm *kvm,
+			       unsigned long ipa,
+			       kvm_pfn_t pfn,
+			       unsigned long map_size,
+			       struct kvm_mmu_memory_cache *memcache)
+{
+	struct realm *realm = &kvm->arch.realm;
+	phys_addr_t phys = __pfn_to_phys(pfn);
+	phys_addr_t base_phys = phys;
+	phys_addr_t rd = virt_to_phys(realm->rd);
+	phys_addr_t delegated_phys;
+	unsigned long base_ipa = ipa;
+	unsigned long ipa_top;
+	long ret = 0;
+
+	if (WARN_ON(!IS_ALIGNED(map_size, PAGE_SIZE) ||
+		    !IS_ALIGNED(ipa, map_size)))
+		return -EINVAL;
+
+	if (rmi_delegate_range(phys, map_size, &delegated_phys)) {
+		if (delegated_phys == phys) {
+			/*
+			 * It's likely we raced with another VCPU on the same
+			 * fault. Assume the other VCPU has handled the fault
+			 * and return to the guest.
+			 */
+			return 0;
+		}
+		/* Partial delegation - map as much as we can */
+		map_size = delegated_phys - phys;
+	}

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

Quoted 10 lines
arch/arm64/kvm/rmi.c +7 -0
+
+	ipa_top = ipa + map_size;
+
+	while (ipa < ipa_top) {
+		unsigned long flags = RMI_ADDR_TYPE_SINGLE;
 
 
 
+		unsigned long range_desc = addr_range_desc(ipa, phys, ipa_top - ipa,
+							   RMI_OP_MEM_DELEGATED);

May be pass the range covered back here as an out parameter ( &top ) ?

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

Quoted 4 lines
arch/arm64/kvm/rmi.c +4 -0
+		unsigned long out_top;
+
+		ret = rmi_rtt_data_map(rd, ipa, ipa_top, flags, range_desc,
+				       &out_top);

Should we limit this to what the addr_range_desc covers with out back from addr_range_desc() ? (or do at least what we do for map_unprotected below ?)

Quoted 80 lines
arch/arm64/kvm/rmi.c +80 -0
+		if (ret < 0)
+			goto err_undelegate;
+
+		if (RMI_RESULT_STATUS(ret) == RMI_ERROR_RTT) {
+			/* Create missing RTTs and retry */
+			int level = RMI_RESULT_DATA_LEVEL(ret);
+
+			if (WARN_ON(level >= KVM_PGTABLE_LAST_LEVEL))
+				goto err_undelegate;
+			ret = realm_create_rtt_levels(realm, ipa, level,
+						      level + 1,
+						      memcache);
+			if (ret)
+				goto err_undelegate;
+
+			continue;
+		}
+
+		if (WARN_ON(ret))
+			goto err_undelegate;
+
+		/* An IRQ can stop the RMM before it maps the first entry. */
+		if (out_top == ipa) {
+			ret = -EAGAIN;
+			goto err_undelegate;
+		}
+		if (WARN_ON_ONCE(out_top < ipa || out_top > ipa_top)) {
+			ret = -ENXIO;
+			goto err_undelegate;
+		}
+
+		phys += out_top - ipa;
+		ipa = out_top;
+	}
+
+	return 0;
+
+err_undelegate:
+	realm_unmap_private_range(kvm, base_ipa, ipa, false);
+	/* A range which cannot be undelegated remains owned by the RMM. */
+	WARN_ON(rmi_undelegate_range(base_phys, map_size));
+	return ret < 0 ? ret : -ENXIO;
+}
+
+static int realm_map_unprotected(struct kvm *kvm,
+				 unsigned long ipa,
+				 kvm_pfn_t pfn,
+				 unsigned long size,
+				 enum kvm_pgtable_prot prot,
+				 struct kvm_mmu_memory_cache *memcache)
+{
+	struct realm *realm = &kvm->arch.realm;
+	unsigned long attr, flags = 0;
+	phys_addr_t rd = virt_to_phys(realm->rd);
+	phys_addr_t phys = __pfn_to_phys(pfn);
+	unsigned long ipa_top = ipa + size;
+	unsigned int retries = 0;
+	long ret;
+
+	if (WARN_ON(!IS_ALIGNED(size, PAGE_SIZE)))
+		return -EINVAL;
+
+	switch (prot & (KVM_PGTABLE_PROT_DEVICE | KVM_PGTABLE_PROT_NORMAL_NC)) {
+	case KVM_PGTABLE_PROT_DEVICE | KVM_PGTABLE_PROT_NORMAL_NC:
+		return -EINVAL;
+	case KVM_PGTABLE_PROT_DEVICE:
+		attr = MT_S2_FWB_DEVICE_nGnRE;
+		break;
+	case KVM_PGTABLE_PROT_NORMAL_NC:
+		attr = MT_S2_FWB_NORMAL_NC;
+		break;
+	default:
+		attr = MT_S2_FWB_NORMAL;
+	}
+
+	flags |= FIELD_PREP(RMI_RTT_UNPROT_MAP_FLAGS_MEMATTR_MASK, attr);
+
+	if (prot & KVM_PGTABLE_PROT_R)
+		flags |= FIELD_PREP(RMI_RTT_UNPROT_MAP_FLAGS_S2AP_MASK, RMI_S2AP_DIRECT_READ);
+	if (prot & KVM_PGTABLE_PROT_W)

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

Quoted 10 lines
arch/arm64/kvm/rmi.c +10 -0
+		flags |= FIELD_PREP(RMI_RTT_UNPROT_MAP_FLAGS_S2AP_MASK, RMI_S2AP_DIRECT_WRITE);
+
+	/* TODO: Batch mappings with RMI_ADDR_TYPE_LIST. */
+	flags |= RMI_ADDR_TYPE_SINGLE;
+
+	while (ipa < ipa_top) {
+		unsigned long range_desc = addr_range_desc(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));

We could return this from addr_range_desc(), as mentioned above.

Suzuki

reviewer
#33 lore raw

Re: [PATCH v2 11/12] KVM: arm64: CCA: Support runtime faulting of memory

Hi Suzuki,

(sorry for the resend, my email client mangled the first one)

Quoted 1 line · Suzuki K Poulose

On 11/11/2026 14:57, Steven Price wrote:

[...]

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

[...]

Quoted 15 lines

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

Quoted 11 lines
arch/arm64/kvm/mmu.c +5 -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;
+
+        if (!perm_fault || kvm_arch_has_private_mem(kvm)) {

should this be s/kvm_arch_has_private_mem/kvm_vm_is_realm ? I dont think this would be required for pKVM yet ?

Quoted 4 lines

Also, I wonder why pKVM doesnt need mmu cache pages with permission faults with gmem. They do for user_mem_abort().
(may be because they assume Gmem is always single page ? in which case we could also have the same behavior)

gmem_abort() only maps single pages, so a permission fault there just relaxes permissions and doesn't use the memcache. In user_mem_abort(), a pKVM permission fault can map a block, and that uses the memcache. So yes, the top-up on a permission fault here is only for Realms.

[...]

Quoted 13 lines

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

Quoted 5 lines
arch/arm64/kvm/mmu.c +1 -0
@@ -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)) {

--8>--

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

Quoted 4 lines
arch/arm64/kvm/mmu.c +4 -0
+                switch (kvm->arch.vm_flavor) {
+                case VM_REALM:
+                        is_private = kvm_realm_is_private_address(&kvm->arch.realm,
+                                                                  s2fd->fault_ipa);
Quoted 8 lines

Could we for now use

                is_private = private_ipa_fault(kvm, s2fd->fault_ipa);

In other words use a wrapper for detecting if a given ipa is private or
not ? May be something like: kvm_ipa_is_private(kvm, ipa)? (choose ipa
to make it explicit, it is arm64 thing).

Where, pKVM could hook in its own check.

I'd keep the switch. pKVM can add its own case when it uses this path.

Cheers,
/fuad

#34 lore raw

PATCH v2 12/12 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.

Dispatch stage-2 destruction according to the VM flavor. Realm teardown destroys non-root RTTs through RMI, so the host frees only the root RTT pages.

---
Changes since v1:
- Dispatch stage-2 destruction by VM flavor. Free only the Realm root RTT pages after RMI teardown while preserving the native and pKVM destruction paths.

---
 arch/arm64/include/asm/kvm_host.h |  1 +
 arch/arm64/kvm/mmu.c              | 72 ++++++++++++++++++++++++++-----
 2 files changed, 62 insertions(+), 11 deletions(-)
arch/arm64/include/asm/kvm_host.h +1 -0
index a82b18a3e4e5..33b1bd2740d3 100644
@@ -243,6 +243,7 @@ struct kvm_vm_s2_ops {
 	int (*vm_flush_remote_tlbs)(struct kvm *kvm);
 	int (*vm_flush_remote_tlbs_range)(struct kvm *kvm, gfn_t gfn,
 					  u64 nr_pages);
+	void (*vm_stage2_destroy)(struct kvm_pgtable *pgt);
 	void (*vm_stage2_unmap_range)(struct kvm_s2_mmu *mmu,
 				      phys_addr_t start, u64 size,
 				      bool may_block,
arch/arm64/kvm/mmu.c +61 -11

145 lines not on this page · raw message

Signature · 1 line
2.43.0
#35 lore raw

Re: [PATCH v2 12/12] KVM: arm64: CCA: Add Realm stage-2 MMU operations

Quoted 6 lines · 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.

Quoted 7 lines

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.

Dispatch stage-2 destruction according to the VM flavor. Realm teardown destroys non-root RTTs through RMI, so the host frees only the root RTT pages.

Minor nit: It may be worth splitting the destroy abstraction as a separate patch. Otherwise looks good to me.

Quoted 4 lines
#36 lore raw

Re: [PATCH v2 12/12] KVM: arm64: CCA: Add Realm stage-2 MMU operations

Quoted 11 lines · Suzuki K Poulose
Quoted 10 lines · 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.

Quoted 6 lines
Quoted 3 lines

Dispatch stage-2 destruction according to the VM flavor. Realm teardown destroys non-root RTTs through RMI, so the host frees only the root RTT pages.

Minor nit: It may be worth splitting the destroy abstraction as a separate patch. Otherwise looks good to me.

Good point - I can split that out.

Thanks,
Steve

Quoted 4 lines
Quoted 4 lines
Tested-by on the v2 series
#37 lore raw
In reply to Steven Price

Hi Steve,

Quoted 47 lines · Steven Price

This series adds the memory management plumbing for CCA, building on the basic RMM support [1]. Thank you to those who reviewed v1 - I'm attempted to address all the comments.

The patches introduce internal Realm VM state and RMI feature checks, 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.

As before there's an integration branch[2] that includes the realm creation, REC entry/exit etc building on top of this series. I've also pushed a git branch[3] of just this series.

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/v2/integration
[3] https://gitlab.arm.com/linux-arm/linux-cca
branch: cca-host-mm/v2/mm

Steven Price (7):
KVM: arm64: CCA: Reserve the RMM mode
KVM: arm64: guest_memfd: Check mmap support when preparing memslot
KVM: arm64: CCA: Check RMI support and features at init
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

Quoted 6 lines

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

I've tested the integration branch [2] including this series on our hardware using the QEMU-VMM proposed in:
https://lore.kernel.org/qemu-devel/[email protected]/

The Realm guest VM (32 vCPUs, 16 GiB RAM) booted successfully. To exercise the guest with a more realistic workload, I also performed a full Linux kernel build, which completed successfully.

A Tested-by tag might not be carried over to the next revision, if any, but anyway, for this series:

Thanks,
Kohei

#38 lore raw
Quoted 1 line · Kohei Enju

Hi Steve,

Hi Kohei,

Quoted 55 lines · Steven Price
Quoted 47 lines

This series adds the memory management plumbing for CCA, building on the basic RMM support [1]. Thank you to those who reviewed v1 - I'm attempted to address all the comments.

The patches introduce internal Realm VM state and RMI feature checks, 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.

As before there's an integration branch[2] that includes the realm creation, REC entry/exit etc building on top of this series. I've also pushed a git branch[3] of just this series.

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/v2/integration
[3] https://gitlab.arm.com/linux-arm/linux-cca
branch: cca-host-mm/v2/mm

Steven Price (7):
KVM: arm64: CCA: Reserve the RMM mode
KVM: arm64: guest_memfd: Check mmap support when preparing memslot
KVM: arm64: CCA: Check RMI support and features at init
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

Quoted 6 lines

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

Quoted 7 lines

I've tested the integration branch [2] including this series on our hardware using the QEMU-VMM proposed in:
https://lore.kernel.org/qemu-devel/[email protected]/

The Realm guest VM (32 vCPUs, 16 GiB RAM) booted successfully. To exercise the guest with a more realistic workload, I also performed a full Linux kernel build, which completed successfully.

Awesome - thank you very much for testing.

Quoted 4 lines

A Tested-by tag might not be carried over to the next revision, if any, but anyway, for this series:

Thanks,
Steve