Skip to contentSkip to conversation

KVM: arm64: CCA: Add basic plumbing for Realms

PATCH Superseded Suzuki K Poulose 8 participants 54 messages Sep 15 – Sep 23

Subsystems (4)

v18 of 23 Superseded Latest is v23 →

lore
Share & tools
lore

Suzuki K Poulose

author reviewer
#0 lore raw

v18 cover KVM: arm64: CCA: Add basic plumbing for Realms

This series is a trimmed down version of the Arm CCA KVM support, previously posted here [0]. Like in the v17, we have tried to split the entire series into the following chunks.

1) Base RMM RMI support under drivers/firmware/arm_rmm -> [1]
2) Linux Host support for handling GPFs - [2]
3) NEW: Enlighten KVM arm64 about the different VM types and use call backs for the VM type, rather than spilling the is_this_type_of_vm() everywhere. Adds VCPU and Stage2 MMU related callbacks with support for the existing VM types. There are other places where we may be able to abstract, but those need careful performance evaluations to make sure they are fit (e.g., vcpu_run)

Later in the series, we generalise the predicate "kvm_vm_is_protected()" to cover all "Confidential" VMs (which includes Protected VM and Realms), which allows us to handle common themes without having to do things like :

     if (kvm_vm_is_protected() || kvm_vm_is_realm()),
     instead:
     if (kvm_vm_is_confidential())

Also replaces the code with precise check for a given VM type to avoiding combination of if (). e.g,, kvm_vm_is_unprotected_pkvm(kvm). The checks under arch/arm64/kvm/{nvhe,pkvm} still retain the vm_is_protected() check as pVMs are the only possible protected VMs there.

4) Bare minimal Realm VM support without the actual functionality to run a Realm. This would help the maintainers to review the series in smaller chunks. This doesn't depend on [1] and can be independently merged, without being "functional".
This series includes vcpu operations and the s2 vm operations, which do need the RMI driver backend to be meaningful. But the KVM handler is in the right shape. The remaining changes would be added once the RMI firmware library lands.
5) Core implementation of the RMI driver for KVM and actual enablement of the Realm support. This depends on (1), (2) and the guest-memfd-in-place conversion series v12 from Ackerley. This is available here at the integration branch [3]

This series is comprised of (3) and (4) above.

The integration branch has been tested with the following components:
tf-RMM: main branch (commit 5e6e2acd) compliant to RMM-v2.0-beta3 [4]
kvmtool: [email protected]:linux-arm/kvmtool-cca.git cca/kvm-v18

[0] Arm CCA KVM Support v16 : https://lore.kernel.org/all/[email protected]
[1] Linux firmware RMI https://lore.kernel.org/all/[email protected]
[2] Linux GPF Host https://lore.kernel.org/all/[email protected]
[3] https://git.gitlab.arm.com/linux-arm/linux-cca/ cca/cca-host/kvm-v18/integration
[4] https://support.arm.com/documentation/den0137/2-0bet3/

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

- Add a patch to fix pKVM handling of SYS_CNTVCT/CNTPCT to override the counter offset (Patch1)
- Restrict Realms to VGIC v3 only - New patch
- Add kvm_vm_is_unprotected() to replace is_protected_kvm_enabled() && !kvm_vm_is_protected() - New patch
- Use macro to initialize the per-flavor vcpu, s2_vm ops
- Add a wrapper to initialise vcpu and s2_vm ops with a BUILD_BUG_ON() for the array size checks against VM flavour types
- Drop forward decalaration of the vcpu, s2_vm operations that spoiled the fun ;-)
- Remove irrelevant comment about the order of timer loading for !VHE
- Use the explicti kvm_call_hyp_nvhe for pKVM specific ops
- Don't call nvhe_vcpu_put from pkvm_vcpu_put, open code them
- Drop cpu argument for vcpu_load() callback. We set the cpu
before the callbacks are invoked
- Drop kvm_vm_is_confidential(), instead widen the scope of kvm_vm_is_protected() to cover pVMs and Realms. Add an explicit helper kvm_vm_is_protected_pkvm() for the cases where we need to check for a "pVM on pKVM"
- Add kvm_vm_hyp_is_pkvm() for checking if the VM is running on pKVM. covers both unprotected and pvms. But really uses is_protected_kvm_enabled() under the hood
- Add kvm_vm_hyp_is_distrusting() to cover pKVM guests (both protected and unprotected) and Realms. Use this for preventing the vgic v2 mapping into Stage2 for a guest
- Drop superfluous !kvm check from kvm_vm_ioctl_enable_cap() - Sashiko
- Drop KVM_CAP_CREATE_IRQCHIP, as we don't support VGIC_V2 for Realms
- Filter out the vm_ioctls that are based on blocked cap.
- Repurpose the pkvm plumbing for filtering the caps and ioctl to generic and plumb the Realm support in
- s/PKVM/pKVM for the comments
- Drop type argument for pkvm_init_host_vm and also drop protected variable, now that we have the vm_flavor to check.
- Use kvm_vm_hyp_is_pkvm() to replace is_protected_kvm_enabled() with valid kvm instance
- CCA: Merge the GET/SET REG handling patches into a single patch
- CCA: Reword the commit description for SVE VL access handling
- Reordered the patches to group the Realm realted to changes to the rear end

Jean-Philippe Brucker (2):
KVM: arm64: CCA: Expose SVE VL register before VCPU finalization
KVM: arm64: CCA: Control user register access for Realms

Steven Price (3):
KVM: arm64: Avoid including linux/kvm_host.h in kvm_pgtable.h
KVM: arm64: CCA: Support timers in realm RECs
KVM: arm64: CCA: WARN on injected undef exceptions

Suzuki K Poulose (18):
KVM: arm64: protected VM: Handle set_one_reg CNTVCT_EL0/CNTPCT_EL0
KVM: arm64: Disable Steal time accounting for protected guests
KVM: arm64: Include kvm_emulate.h in kvm/arm_psci.h
KVM: arm64: Track the type of VM in kvm_arch
KVM: arm64: Refactor the vcpu_load to allow for VM specific callbacks
KVM: arm64: Add vcpu load/put call backs for flavors
KVM: arm64: Reuse kvm_stage2_unmap_range in kvm_unmap_gfn_range
KVM: arm64: Add VM specific callback for S2 MMU operations
KVM: arm64: Abstract out memory abort handling
KVM: arm64: Use kvm_vm_is_unprotected() for !kvm_vm_is_protected()
KVM: arm64: Widen the scope of "protected" VMs
KVM: arm64: Add a helper for VMs running on hyp that don't trust the
host
KVM: arm64: CCA: Add a new mode for supporting Realm guests
KVM: arm64: CCA: Add VCPU load/put for Realms
KVM: arm64: CCA: Add bare minimal S2 operations for Realm
KVM: arm64: CCA: Introduce Realms
KVM: arm64: CCA: Mandate VGIC_V3 for Realms
KVM: arm64: CCA: Don't expose unsupported capabilities for realm
guests

 .../admin-guide/kernel-parameters.txt         |   3 +
 arch/arm64/include/asm/kvm_emulate.h          |  16 +
 arch/arm64/include/asm/kvm_host.h             |  63 +++-
 arch/arm64/include/asm/kvm_pgtable.h          |   6 +-
 arch/arm64/include/asm/kvm_pkvm.h             |  25 +-
 arch/arm64/include/asm/kvm_rmi.h              |  61 ++++
 arch/arm64/include/asm/virt.h                 |   1 +
 arch/arm64/kvm/Makefile                       |   2 +-
 arch/arm64/kvm/arch_timer.c                   |  37 +-
 arch/arm64/kvm/arm.c                          | 330 +++++++++++++++---
 arch/arm64/kvm/guest.c                        |  73 +++-
 arch/arm64/kvm/handle_exit.c                  |   2 +-
 arch/arm64/kvm/hyp/nvhe/pkvm.c                |  28 +-
 arch/arm64/kvm/hyp/pgtable.c                  |   1 +
 arch/arm64/kvm/hypercalls.c                   |   4 +-
 arch/arm64/kvm/inject_fault.c                 |   1 +
 arch/arm64/kvm/mmu.c                          | 210 ++++++++---
 arch/arm64/kvm/pkvm.c                         |   6 +-
 arch/arm64/kvm/pvtime.c                       |  14 +-
 arch/arm64/kvm/rmi.c                          |  18 +
 arch/arm64/kvm/sys_regs.c                     |  29 +-
 arch/arm64/kvm/vgic/vgic-init.c               |   3 +
 include/kvm/arm_arch_timer.h                  |   3 +-
 include/kvm/arm_psci.h                        |   2 +
 24 files changed, 758 insertions(+), 180 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
author reviewer
#1 lore raw

PATCH v18 1/23 KVM: arm64: protected VM: Handle set_one_reg CNTVCT_EL0/CNTPCT_EL0

Protected VMs doesn't allow setting offsets for virtual and phyiscal counters, as the offset is always fixed to 0. The VM ioclt is filtered out based on the cap. However we don't prevent the userspace from trying to write to the CNTVCT/CNTPCT registers. This would lead to KVM triggering a WARN() in timer_set_offset() as the vm_offset pointer is set to NULL.

Fix this by always "fixing" the timer offsets to 0 and marking that the timer offset is set in the kvm->arch.flags at pKVM init time. The userspace cannot use the KVM_ARM_SET_COUNTER_OFFSET, as it is blocked for a protected VM.

A userspace writing to the SYS_CNT*CT would observe success, without any real effect. This was chosen over preventing the writes to these registers and returning -EPERM.

With that, we always have a valid vm_offset pointer, remove the checks for vm_offset == NULL.

Reported by Sashiko here
https://lore.kernel.org/all/[email protected]

Fixes f7d05ee84a6a ("KVM: arm64: Prevent host from managing timer offsets for protected VMs")
---
 arch/arm64/kvm/arch_timer.c    | 15 +++++----------
 arch/arm64/kvm/arm.c           | 15 +++++++++++++++
 arch/arm64/kvm/hyp/nvhe/pkvm.c | 26 +++++++++++++-------------
 include/kvm/arm_arch_timer.h   |  3 +--
 4 files changed, 34 insertions(+), 25 deletions(-)
arch/arm64/kvm/arch_timer.c +5 -10
index 6ac3321f4c575..dda020da4c9c7 100644
@@ -1079,14 +1079,10 @@ static void timer_context_init(struct kvm_vcpu *vcpu, int timerid)
 
 	ctxt->timer_id = timerid;
 
-	if (!kvm_vm_is_protected(vcpu->kvm)) {
-		if (timerid == TIMER_VTIMER)
-			ctxt->offset.vm_offset = &kvm->arch.timer_data.voffset;
-		else
-			ctxt->offset.vm_offset = &kvm->arch.timer_data.poffset;
-	} else {
-		ctxt->offset.vm_offset = NULL;
-	}
+	if (timerid == TIMER_VTIMER)
+		ctxt->offset.vm_offset = &kvm->arch.timer_data.voffset;
+	else
+		ctxt->offset.vm_offset = &kvm->arch.timer_data.poffset;
 
 	hrtimer_setup(&ctxt->hrtimer, kvm_hrtimer_expire, CLOCK_MONOTONIC, HRTIMER_MODE_ABS_HARD);
 
@@ -1110,8 +1106,7 @@ void kvm_timer_vcpu_init(struct kvm_vcpu *vcpu)
 		timer_context_init(vcpu, i);
 
 	/* Synchronize offsets across timers of a VM if not already provided */
-	if (!vcpu_is_protected(vcpu) &&
-	    !test_bit(KVM_ARCH_FLAG_VM_COUNTER_OFFSET, &vcpu->kvm->arch.flags)) {
+	if (!test_bit(KVM_ARCH_FLAG_VM_COUNTER_OFFSET, &vcpu->kvm->arch.flags)) {
 		timer_set_offset(vcpu_vtimer(vcpu), kvm_phys_timer_read());
 		timer_set_offset(vcpu_ptimer(vcpu), 0);
 	}
arch/arm64/kvm/arm.c +15 -0
index 8b080804bc90b..7c88508cac8a1 100644
@@ -214,6 +214,20 @@ static int kvm_arm_default_max_vcpus(void)
 	return vgic_present ? kvm_vgic_get_max_vcpus() : KVM_MAX_VCPUS;
 }
 
+/*
+ * Fix the counter offset to 0 for Protected VMs and mark the
+ * offset flag. The user can't set the offset via KVM_ARM_SET_COUNTER_OFFSET.
+ */
+static void kvm_arch_fix_timer_offsets(struct kvm *kvm)
+{
+	if (!kvm_vm_is_protected(kvm))
+		return;
+
+	/* Fix the counter offset to 0 and mark the offset initialised */
+	kvm->arch.timer_data.poffset = kvm->arch.timer_data.voffset = 0;
+	set_bit(KVM_ARCH_FLAG_VM_COUNTER_OFFSET, &kvm->arch.flags);
+}
+
 /**
  * kvm_arch_init_vm - initializes a VM data structure
  * @kvm:	pointer to the KVM struct
@@ -267,6 +281,7 @@ int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)
 
 	kvm_vgic_early_init(kvm);
 
+	kvm_arch_fix_timer_offsets(kvm);
 	kvm_timer_init_vm(kvm);
 
 	/* The maximum number of VCPUs is limited by the host's GIC model */
arch/arm64/kvm/hyp/nvhe/pkvm.c +13 -13
index 459bd9eb7e4bc..e7b38eff63bd1 100644
@@ -528,19 +528,19 @@ static int init_pkvm_hyp_vcpu(struct pkvm_hyp_vcpu *hyp_vcpu,
 	hyp_vcpu->vcpu.arch.cflags = READ_ONCE(host_vcpu->arch.cflags);
 	hyp_vcpu->vcpu.arch.mp_state.mp_state = KVM_MP_STATE_STOPPED;
 
-	if (!pkvm_hyp_vcpu_is_protected(hyp_vcpu)) {
-		/*
-		 * Timer offsets are pointing to the untrusted KVM copy,
-		 * which is pinned in __pkvm_init_vm() for the VM life time.
-		 * It is worth noting that hyp_vm->host_kvm points to an EL2
-		 * linear map address and timer_get_offset() will use
-		 * kern_hyp_va() which is safe as it is idempotent.
-		 */
-		vcpu_vtimer(&hyp_vcpu->vcpu)->offset.vm_offset =
-			&hyp_vm->host_kvm->arch.timer_data.voffset;
-		vcpu_ptimer(&hyp_vcpu->vcpu)->offset.vm_offset =
-			&hyp_vm->host_kvm->arch.timer_data.poffset;
-	}
+	/*
+	 * Timer offsets are pointing to the untrusted KVM copy,
+	 * which is pinned in __pkvm_init_vm() for the VM life time.
+	 * It is worth noting that hyp_vm->host_kvm points to an EL2
+	 * linear map address and timer_get_offset() will use
+	 * kern_hyp_va() which is safe as it is idempotent.
+	 * Also for protected VMs the offset is fixed to 0 and is prevented
+	 * from changing.
+	 */
+	vcpu_vtimer(&hyp_vcpu->vcpu)->offset.vm_offset =
+		&hyp_vm->host_kvm->arch.timer_data.voffset;
+	vcpu_ptimer(&hyp_vcpu->vcpu)->offset.vm_offset =
+		&hyp_vm->host_kvm->arch.timer_data.poffset;
 
 	ret = pkvm_vcpu_init_sysregs(hyp_vcpu);
 	if (ret)
include/kvm/arm_arch_timer.h +1 -2
index bc6f2fdd7ad33..4f0aa3bb69f45 100644
@@ -176,8 +176,7 @@ static inline bool has_cntpoff(void)
 		if (__ctxt) {						\
 			struct arch_timer_offset *ato = &__ctxt->offset;\
 									\
-			if (ato->vm_offset)				\
-				off += *KERN_HYP_VA(ato->vm_offset);	\
+			off += *KERN_HYP_VA(ato->vm_offset);		\
 			if (ato->vcpu_offset)				\
 				off += *KERN_HYP_VA(ato->vcpu_offset);	\
 		}							\
Signature · 1 line
2.43.0
#2 lore raw

Re: [PATCH v18 01/23] KVM: arm64: protected VM: Handle set_one_reg CNTVCT_EL0/CNTPCT_EL0

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] kvm: arm64: pKVM: protected VM timer offset vulnerable to host TOCTOU --

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] kvm: arm64: pKVM: protected VM timer offset vulnerable to host TOCTOU --

commit 960552f9d7645214acac73b01e88ba381530e333

Author: Suzuki K Poulose <[email protected]>
Date:   Tue Sep 15 16:01:19 2026 +0000

KVM: arm64: protected VM: Handle set_one_reg CNTVCT_EL0/CNTPCT_EL0

This commit fixes a WARN() in timer_set_offset() caused by userspace writing to CNTVCT/CNTPCT registers for protected VMs. It accomplishes this by setting the timer offsets to 0 and marking the offset initialized at pKVM init time, removing the need for the vm_offset pointer to be NULL.

[ ... ]

Quoted 24 lines
arch/arm64/kvm/hyp/nvhe/pkvm.c +3 -13
index 459bd9eb7e4bc..e7b38eff63bd1 100644
@@ -528,19 +528,19 @@ static int init_pkvm_hyp_vcpu(struct pkvm_hyp_vcpu *hyp_vcpu,
 	hyp_vcpu->vcpu.arch.cflags = READ_ONCE(host_vcpu->arch.cflags);
 	hyp_vcpu->vcpu.arch.mp_state.mp_state = KVM_MP_STATE_STOPPED;
 
-	if (!pkvm_hyp_vcpu_is_protected(hyp_vcpu)) {
-		/*
-		 * Timer offsets are pointing to the untrusted KVM copy,
-		 * which is pinned in __pkvm_init_vm() for the VM life time.
-		 * It is worth noting that hyp_vm->host_kvm points to an EL2
-		 * linear map address and timer_get_offset() will use
-		 * kern_hyp_va() which is safe as it is idempotent.
-		 */
-		vcpu_vtimer(&hyp_vcpu->vcpu)->offset.vm_offset =
-			&hyp_vm->host_kvm->arch.timer_data.voffset;
-		vcpu_ptimer(&hyp_vcpu->vcpu)->offset.vm_offset =
-			&hyp_vm->host_kvm->arch.timer_data.poffset;
-	}
+	/*
+	 * Timer offsets are pointing to the untrusted KVM copy,
+	 * which is pinned in __pkvm_init_vm() for the VM life time.

arch/arm64/kvm/hyp/nvhe/pkvm.c static int init_pkvm_hyp_vcpu(struct pkvm_hyp_vcpu *hyp_vcpu, · line 543

Quoted 10 lines
arch/arm64/kvm/hyp/nvhe/pkvm.c +10 -0
+	 * It is worth noting that hyp_vm->host_kvm points to an EL2
+	 * linear map address and timer_get_offset() will use
+	 * kern_hyp_va() which is safe as it is idempotent.
+	 * Also for protected VMs the offset is fixed to 0 and is prevented
+	 * from changing.
+	 */
+	vcpu_vtimer(&hyp_vcpu->vcpu)->offset.vm_offset =
+		&hyp_vm->host_kvm->arch.timer_data.voffset;
+	vcpu_ptimer(&hyp_vcpu->vcpu)->offset.vm_offset =
+		&hyp_vm->host_kvm->arch.timer_data.poffset;

[Severity: High]
Does this change introduce a TOCTOU regression for protected VMs? By removing the !pkvm_hyp_vcpu_is_protected() check in init_pkvm_hyp_vcpu(), the protected VM's vm_offset is now unconditionally assigned to point to the untrusted host memory back-pointer (hyp_vm->host_kvm).

Under the pKVM threat model, the host kernel is untrusted. If the host modifies the voffset or poffset in its timer_data at runtime, EL2 will read this manipulated value. This could allow a compromised host kernel to alter the virtual and physical timer offsets observed by a protected VM, violating guest integrity.

Should the offset instead be derived from the EL2-private &hyp_vm->kvm structure or statically set to 0 for protected VMs?

[ ... ]

Quoted 5 lines
include/kvm/arm_arch_timer.h +0 -0
index bc6f2fdd7ad33..4f0aa3bb69f45 100644

include/kvm/arm_arch_timer.h static inline bool has_cntpoff(void) · line 182

Quoted 9 lines
include/kvm/arm_arch_timer.h +1 -2
@@ -176,8 +176,7 @@ static inline bool has_cntpoff(void)
 		if (__ctxt) {						\
 			struct arch_timer_offset *ato = &__ctxt->offset;\
 									\
-			if (ato->vm_offset)				\
-				off += *KERN_HYP_VA(ato->vm_offset);	\
+			off += *KERN_HYP_VA(ato->vm_offset);		\
 			if (ato->vcpu_offset)				\
 				off += *KERN_HYP_VA(ato->vcpu_offset);	\
 		}							\

[Severity: High]
Since the pointer is now unconditionally assigned to host memory for protected VMs, EL2 will dereference this host-controlled pointer here in timer_get_offset() via KERN_HYP_VA(). Can this cause the hypervisor to read a security-sensitive value from untrusted host memory?

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

Re: [PATCH v18 01/23] KVM: arm64: protected VM: Handle set_one_reg CNTVCT_EL0/CNTPCT_EL0

In reply to Suzuki K Poulose
Quoted 91 lines · Suzuki K Poulose

Protected VMs doesn't allow setting offsets for virtual and phyiscal counters, as the offset is always fixed to 0. The VM ioclt is filtered out based on the cap. However we don't prevent the userspace from trying to write to the CNTVCT/CNTPCT registers. This would lead to KVM triggering a WARN() in timer_set_offset() as the vm_offset pointer is set to NULL.

Fix this by always "fixing" the timer offsets to 0 and marking that the timer offset is set in the kvm->arch.flags at pKVM init time. The userspace cannot use the KVM_ARM_SET_COUNTER_OFFSET, as it is blocked for a protected VM.

A userspace writing to the SYS_CNT*CT would observe success, without any real effect. This was chosen over preventing the writes to these registers and returning -EPERM.

With that, we always have a valid vm_offset pointer, remove the checks for vm_offset == NULL.

Reported by Sashiko here
https://lore.kernel.org/all/[email protected]

Fixes f7d05ee84a6a ("KVM: arm64: Prevent host from managing timer offsets for protected VMs")
---
 arch/arm64/kvm/arch_timer.c    | 15 +++++----------
 arch/arm64/kvm/arm.c           | 15 +++++++++++++++
 arch/arm64/kvm/hyp/nvhe/pkvm.c | 26 +++++++++++++-------------
 include/kvm/arm_arch_timer.h   |  3 +--
 4 files changed, 34 insertions(+), 25 deletions(-)
arch/arm64/kvm/arch_timer.c +5 -10
index 6ac3321f4c575..dda020da4c9c7 100644
@@ -1079,14 +1079,10 @@ static void timer_context_init(struct kvm_vcpu *vcpu, int timerid)
 
 	ctxt->timer_id = timerid;
 
-	if (!kvm_vm_is_protected(vcpu->kvm)) {
-		if (timerid == TIMER_VTIMER)
-			ctxt->offset.vm_offset = &kvm->arch.timer_data.voffset;
-		else
-			ctxt->offset.vm_offset = &kvm->arch.timer_data.poffset;
-	} else {
-		ctxt->offset.vm_offset = NULL;
-	}
+	if (timerid == TIMER_VTIMER)
+		ctxt->offset.vm_offset = &kvm->arch.timer_data.voffset;
+	else
+		ctxt->offset.vm_offset = &kvm->arch.timer_data.poffset;
 
 	hrtimer_setup(&ctxt->hrtimer, kvm_hrtimer_expire, CLOCK_MONOTONIC, HRTIMER_MODE_ABS_HARD);
 
@@ -1110,8 +1106,7 @@ void kvm_timer_vcpu_init(struct kvm_vcpu *vcpu)
 		timer_context_init(vcpu, i);
 
 	/* Synchronize offsets across timers of a VM if not already provided */
-	if (!vcpu_is_protected(vcpu) &&
-	    !test_bit(KVM_ARCH_FLAG_VM_COUNTER_OFFSET, &vcpu->kvm->arch.flags)) {
+	if (!test_bit(KVM_ARCH_FLAG_VM_COUNTER_OFFSET, &vcpu->kvm->arch.flags)) {
 		timer_set_offset(vcpu_vtimer(vcpu), kvm_phys_timer_read());
 		timer_set_offset(vcpu_ptimer(vcpu), 0);
 	}
arch/arm64/kvm/arm.c +14 -0
index 8b080804bc90b..7c88508cac8a1 100644
@@ -214,6 +214,20 @@ static int kvm_arm_default_max_vcpus(void)
 	return vgic_present ? kvm_vgic_get_max_vcpus() : KVM_MAX_VCPUS;
 }
 
+/*
+ * Fix the counter offset to 0 for Protected VMs and mark the
+ * offset flag. The user can't set the offset via KVM_ARM_SET_COUNTER_OFFSET.
+ */
+static void kvm_arch_fix_timer_offsets(struct kvm *kvm)
+{
+	if (!kvm_vm_is_protected(kvm))
+		return;
+
+	/* Fix the counter offset to 0 and mark the offset initialised */
+	kvm->arch.timer_data.poffset = kvm->arch.timer_data.voffset = 0;
+	set_bit(KVM_ARCH_FLAG_VM_COUNTER_OFFSET, &kvm->arch.flags);
+}
+
 /**
  * kvm_arch_init_vm - initializes a VM data structure
  * @kvm:	pointer to the KVM struct

arch/arm64/kvm/arm.c int kvm_arch_init_vm(struct kvm *kvm, unsigned long type) · line 285

Quoted 5 lines
arch/arm64/kvm/arm.c +1 -0
@@ -267,6 +281,7 @@ int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)
 
 	kvm_vgic_early_init(kvm);
 
+	kvm_arch_fix_timer_offsets(kvm);
 	kvm_timer_init_vm(kvm);

This should all be moved to the timer code.

Quoted 26 lines
arch/arm64/kvm/arm.c +0 -0
 	/* The maximum number of VCPUs is limited by the host's GIC model */
arch/arm64/kvm/hyp/nvhe/pkvm.c +3 -13
index 459bd9eb7e4bc..e7b38eff63bd1 100644
@@ -528,19 +528,19 @@ static int init_pkvm_hyp_vcpu(struct pkvm_hyp_vcpu *hyp_vcpu,
 	hyp_vcpu->vcpu.arch.cflags = READ_ONCE(host_vcpu->arch.cflags);
 	hyp_vcpu->vcpu.arch.mp_state.mp_state = KVM_MP_STATE_STOPPED;
 
-	if (!pkvm_hyp_vcpu_is_protected(hyp_vcpu)) {
-		/*
-		 * Timer offsets are pointing to the untrusted KVM copy,
-		 * which is pinned in __pkvm_init_vm() for the VM life time.
-		 * It is worth noting that hyp_vm->host_kvm points to an EL2
-		 * linear map address and timer_get_offset() will use
-		 * kern_hyp_va() which is safe as it is idempotent.
-		 */
-		vcpu_vtimer(&hyp_vcpu->vcpu)->offset.vm_offset =
-			&hyp_vm->host_kvm->arch.timer_data.voffset;
-		vcpu_ptimer(&hyp_vcpu->vcpu)->offset.vm_offset =
-			&hyp_vm->host_kvm->arch.timer_data.poffset;
-	}
+	/*
+	 * Timer offsets are pointing to the untrusted KVM copy,
+	 * which is pinned in __pkvm_init_vm() for the VM life time.

arch/arm64/kvm/hyp/nvhe/pkvm.c static int init_pkvm_hyp_vcpu(struct pkvm_hyp_vcpu *hyp_vcpu, · line 543

Quoted 10 lines
arch/arm64/kvm/hyp/nvhe/pkvm.c +10 -0
+	 * It is worth noting that hyp_vm->host_kvm points to an EL2
+	 * linear map address and timer_get_offset() will use
+	 * kern_hyp_va() which is safe as it is idempotent.
+	 * Also for protected VMs the offset is fixed to 0 and is prevented
+	 * from changing.
+	 */
+	vcpu_vtimer(&hyp_vcpu->vcpu)->offset.vm_offset =
+		&hyp_vm->host_kvm->arch.timer_data.voffset;
+	vcpu_ptimer(&hyp_vcpu->vcpu)->offset.vm_offset =
+		&hyp_vm->host_kvm->arch.timer_data.poffset;

I don't think this is right. Protected guests have no offset, and this needs to be ensured by the hypervisor. Here, the host can change the offset any time it wants, and that's not acceptable.

Quoted 8 lines
arch/arm64/kvm/hyp/nvhe/pkvm.c +0 -0
 	ret = pkvm_vcpu_init_sysregs(hyp_vcpu);
 	if (ret)
include/kvm/arm_arch_timer.h +0 -0
index bc6f2fdd7ad33..4f0aa3bb69f45 100644

include/kvm/arm_arch_timer.h static inline bool has_cntpoff(void) · line 182

Quoted 9 lines
include/kvm/arm_arch_timer.h +1 -2
@@ -176,8 +176,7 @@ static inline bool has_cntpoff(void)
 		if (__ctxt) {						\
 			struct arch_timer_offset *ato = &__ctxt->offset;\
 									\
-			if (ato->vm_offset)				\
-				off += *KERN_HYP_VA(ato->vm_offset);	\
+			off += *KERN_HYP_VA(ato->vm_offset);		\
 			if (ato->vcpu_offset)				\
 				off += *KERN_HYP_VA(ato->vcpu_offset);	\
 		}							\

And as you drop the previous hunk, this also needs to be restored to its original state.

	M.
Signature · 1 line
Without deviation from the norm, progress is not possible.
author reviewer
#4 lore raw

Re: [PATCH v18 01/23] KVM: arm64: protected VM: Handle set_one_reg CNTVCT_EL0/CNTPCT_EL0

Quoted 133 lines · Marc Zyngier
Quoted 91 lines · Suzuki K Poulose

Protected VMs doesn't allow setting offsets for virtual and phyiscal counters, as the offset is always fixed to 0. The VM ioclt is filtered out based on the cap. However we don't prevent the userspace from trying to write to the CNTVCT/CNTPCT registers. This would lead to KVM triggering a WARN() in timer_set_offset() as the vm_offset pointer is set to NULL.

Fix this by always "fixing" the timer offsets to 0 and marking that the timer offset is set in the kvm->arch.flags at pKVM init time. The userspace cannot use the KVM_ARM_SET_COUNTER_OFFSET, as it is blocked for a protected VM.

A userspace writing to the SYS_CNT*CT would observe success, without any real effect. This was chosen over preventing the writes to these registers and returning -EPERM.

With that, we always have a valid vm_offset pointer, remove the checks for vm_offset == NULL.

Reported by Sashiko here
https://lore.kernel.org/all/[email protected]

Fixes f7d05ee84a6a ("KVM: arm64: Prevent host from managing timer offsets for protected VMs")
---
  arch/arm64/kvm/arch_timer.c    | 15 +++++----------
  arch/arm64/kvm/arm.c           | 15 +++++++++++++++
  arch/arm64/kvm/hyp/nvhe/pkvm.c | 26 +++++++++++++-------------
  include/kvm/arm_arch_timer.h   |  3 +--
  4 files changed, 34 insertions(+), 25 deletions(-)
arch/arm64/kvm/arch_timer.c +5 -10
index 6ac3321f4c575..dda020da4c9c7 100644
@@ -1079,14 +1079,10 @@ static void timer_context_init(struct kvm_vcpu *vcpu, int timerid)
 
  	ctxt->timer_id = timerid;
 
-	if (!kvm_vm_is_protected(vcpu->kvm)) {
-		if (timerid == TIMER_VTIMER)
-			ctxt->offset.vm_offset = &kvm->arch.timer_data.voffset;
-		else
-			ctxt->offset.vm_offset = &kvm->arch.timer_data.poffset;
-	} else {
-		ctxt->offset.vm_offset = NULL;
-	}
+	if (timerid == TIMER_VTIMER)
+		ctxt->offset.vm_offset = &kvm->arch.timer_data.voffset;
+	else
+		ctxt->offset.vm_offset = &kvm->arch.timer_data.poffset;
 
  	hrtimer_setup(&ctxt->hrtimer, kvm_hrtimer_expire, CLOCK_MONOTONIC, HRTIMER_MODE_ABS_HARD);
 
@@ -1110,8 +1106,7 @@ void kvm_timer_vcpu_init(struct kvm_vcpu *vcpu)
  		timer_context_init(vcpu, i);
 
  	/* Synchronize offsets across timers of a VM if not already provided */
-	if (!vcpu_is_protected(vcpu) &&
-	    !test_bit(KVM_ARCH_FLAG_VM_COUNTER_OFFSET, &vcpu->kvm->arch.flags)) {
+	if (!test_bit(KVM_ARCH_FLAG_VM_COUNTER_OFFSET, &vcpu->kvm->arch.flags)) {
  		timer_set_offset(vcpu_vtimer(vcpu), kvm_phys_timer_read());
  		timer_set_offset(vcpu_ptimer(vcpu), 0);
  	}
arch/arm64/kvm/arm.c +14 -0
index 8b080804bc90b..7c88508cac8a1 100644
@@ -214,6 +214,20 @@ static int kvm_arm_default_max_vcpus(void)
  	return vgic_present ? kvm_vgic_get_max_vcpus() : KVM_MAX_VCPUS;
  }
 
+/*
+ * Fix the counter offset to 0 for Protected VMs and mark the
+ * offset flag. The user can't set the offset via KVM_ARM_SET_COUNTER_OFFSET.
+ */
+static void kvm_arch_fix_timer_offsets(struct kvm *kvm)
+{
+	if (!kvm_vm_is_protected(kvm))
+		return;
+
+	/* Fix the counter offset to 0 and mark the offset initialised */
+	kvm->arch.timer_data.poffset = kvm->arch.timer_data.voffset = 0;
+	set_bit(KVM_ARCH_FLAG_VM_COUNTER_OFFSET, &kvm->arch.flags);
+}
+
  /**
   * kvm_arch_init_vm - initializes a VM data structure
   * @kvm:	pointer to the KVM struct

arch/arm64/kvm/arm.c int kvm_arch_init_vm(struct kvm *kvm, unsigned long type) · line 285

Quoted 5 lines
arch/arm64/kvm/arm.c +1 -0
@@ -267,6 +281,7 @@ int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)
 
  	kvm_vgic_early_init(kvm);
 
+	kvm_arch_fix_timer_offsets(kvm);
  	kvm_timer_init_vm(kvm);

This should all be moved to the timer code.

Quoted 32 lines
arch/arm64/kvm/arm.c +0 -0
  	/* The maximum number of VCPUs is limited by the host's GIC model */
arch/arm64/kvm/hyp/nvhe/pkvm.c +9 -13
index 459bd9eb7e4bc..e7b38eff63bd1 100644
@@ -528,19 +528,19 @@ static int init_pkvm_hyp_vcpu(struct pkvm_hyp_vcpu *hyp_vcpu,
  	hyp_vcpu->vcpu.arch.cflags = READ_ONCE(host_vcpu->arch.cflags);
  	hyp_vcpu->vcpu.arch.mp_state.mp_state = KVM_MP_STATE_STOPPED;
 
-	if (!pkvm_hyp_vcpu_is_protected(hyp_vcpu)) {
-		/*
-		 * Timer offsets are pointing to the untrusted KVM copy,
-		 * which is pinned in __pkvm_init_vm() for the VM life time.
-		 * It is worth noting that hyp_vm->host_kvm points to an EL2
-		 * linear map address and timer_get_offset() will use
-		 * kern_hyp_va() which is safe as it is idempotent.
-		 */
-		vcpu_vtimer(&hyp_vcpu->vcpu)->offset.vm_offset =
-			&hyp_vm->host_kvm->arch.timer_data.voffset;
-		vcpu_ptimer(&hyp_vcpu->vcpu)->offset.vm_offset =
-			&hyp_vm->host_kvm->arch.timer_data.poffset;
-	}
+	/*
+	 * Timer offsets are pointing to the untrusted KVM copy,
+	 * which is pinned in __pkvm_init_vm() for the VM life time.
+	 * It is worth noting that hyp_vm->host_kvm points to an EL2
+	 * linear map address and timer_get_offset() will use
+	 * kern_hyp_va() which is safe as it is idempotent.
+	 * Also for protected VMs the offset is fixed to 0 and is prevented
+	 * from changing.
+	 */
Quoted 8 lines

arch/arm64/kvm/hyp/nvhe/pkvm.c static int init_pkvm_hyp_vcpu(struct pkvm_hyp_vcpu *hyp_vcpu, · line 543

Quoted 4 lines
arch/arm64/kvm/hyp/nvhe/pkvm.c +4 -0
+	vcpu_vtimer(&hyp_vcpu->vcpu)->offset.vm_offset =
+		&hyp_vm->host_kvm->arch.timer_data.voffset;
+	vcpu_ptimer(&hyp_vcpu->vcpu)->offset.vm_offset =
+		&hyp_vm->host_kvm->arch.timer_data.poffset;

I don't think this is right. Protected guests have no offset, and this needs to be ensured by the hypervisor. Here, the host can change the offset any time it wants, and that's not acceptable.

Ah, you're right. :facepalm:

Quoted 13 lines
Quoted 13 lines
arch/arm64/kvm/hyp/nvhe/pkvm.c +0 -0
  	ret = pkvm_vcpu_init_sysregs(hyp_vcpu);
  	if (ret)
include/kvm/arm_arch_timer.h +0 -1
index bc6f2fdd7ad33..4f0aa3bb69f45 100644
@@ -176,8 +176,7 @@ static inline bool has_cntpoff(void)
  		if (__ctxt) {						\
  			struct arch_timer_offset *ato = &__ctxt->offset;\
  									\
-			if (ato->vm_offset)				\
Quoted 8 lines

include/kvm/arm_arch_timer.h static inline bool has_cntpoff(void) · line 182

Quoted 5 lines
include/kvm/arm_arch_timer.h +1 -1
-				off += *KERN_HYP_VA(ato->vm_offset);	\
+			off += *KERN_HYP_VA(ato->vm_offset);		\
  			if (ato->vcpu_offset)				\
  				off += *KERN_HYP_VA(ato->vcpu_offset);	\
  		}							\

And as you drop the previous hunk, this also needs to be restored to its original state.

Ack.

Suzuki

Quoted 3 lines
	M.
author reviewer
#5 lore raw

Re: [PATCH v18 01/23] KVM: arm64: protected VM: Handle set_one_reg CNTVCT_EL0/CNTPCT_EL0

Quoted 162 lines · Suzuki K Poulose
Quoted 129 lines · Marc Zyngier
Quoted 89 lines · Suzuki K Poulose

Protected VMs doesn't allow setting offsets for virtual and phyiscal counters, as the offset is always fixed to 0. The VM ioclt is filtered out based on the cap. However we don't prevent the userspace from trying to write to the CNTVCT/CNTPCT registers. This would lead to KVM triggering a WARN() in timer_set_offset() as the vm_offset pointer is set to NULL.

Fix this by always "fixing" the timer offsets to 0 and marking that the timer offset is set in the kvm->arch.flags at pKVM init time. The userspace cannot use the KVM_ARM_SET_COUNTER_OFFSET, as it is blocked for a protected VM.

A userspace writing to the SYS_CNT*CT would observe success, without any real effect. This was chosen over preventing the writes to these registers and returning -EPERM.

With that, we always have a valid vm_offset pointer, remove the checks for vm_offset == NULL.

Reported by Sashiko here
https://lore.kernel.org/all/[email protected]

Fixes f7d05ee84a6a ("KVM: arm64: Prevent host from managing timer offsets for protected VMs")

---
  arch/arm64/kvm/arch_timer.c    | 15 +++++---------- arch/arm64/kvm/arm.c           | 15 +++++++++++++++ arch/arm64/kvm/hyp/nvhe/pkvm.c | 26 +++++++++++++------------- include/kvm/arm_arch_timer.h   |  3 +--

  4 files changed, 34 insertions(+), 25 deletions(-)
arch/arm64/kvm/arch_timer.c +5 -10
index 6ac3321f4c575..dda020da4c9c7 100644
@@ -1079,14 +1079,10 @@ static void timer_context_init(struct 
 kvm_vcpu *vcpu, int timerid)
       ctxt->timer_id = timerid;
-    if (!kvm_vm_is_protected(vcpu->kvm)) {
-        if (timerid == TIMER_VTIMER)
-            ctxt->offset.vm_offset = &kvm->arch.timer_data.voffset;
-        else
-            ctxt->offset.vm_offset = &kvm->arch.timer_data.poffset;
-    } else {
-        ctxt->offset.vm_offset = NULL;
-    }
+    if (timerid == TIMER_VTIMER)
+        ctxt->offset.vm_offset = &kvm->arch.timer_data.voffset;
+    else
+        ctxt->offset.vm_offset = &kvm->arch.timer_data.poffset;
       hrtimer_setup(&ctxt->hrtimer, kvm_hrtimer_expire, CLOCK_MONOTONIC, HRTIMER_MODE_ABS_HARD);
@@ -1110,8 +1106,7 @@ void kvm_timer_vcpu_init(struct kvm_vcpu *vcpu)
           timer_context_init(vcpu, i);
       /* Synchronize offsets across timers of a VM if not already provided */
-    if (!vcpu_is_protected(vcpu) &&
-        !test_bit(KVM_ARCH_FLAG_VM_COUNTER_OFFSET, &vcpu->kvm- 
 >arch.flags)) {
+    if (!test_bit(KVM_ARCH_FLAG_VM_COUNTER_OFFSET, &vcpu->kvm- 
 >arch.flags)) {
           timer_set_offset(vcpu_vtimer(vcpu), kvm_phys_timer_read());
           timer_set_offset(vcpu_ptimer(vcpu), 0);

      }

arch/arm64/kvm/arm.c +3 -0
index 8b080804bc90b..7c88508cac8a1 100644
@@ -214,6 +214,20 @@ static int kvm_arm_default_max_vcpus(void)
       return vgic_present ? kvm_vgic_get_max_vcpus() : KVM_MAX_VCPUS;
   }
+/*
+ * Fix the counter offset to 0 for Protected VMs and mark the
+ * offset flag. The user can't set the offset via 

KVM_ARM_SET_COUNTER_OFFSET.
+ */

arch/arm64/kvm/arm.c +10 -0
+static void kvm_arch_fix_timer_offsets(struct kvm *kvm)
+{
+    if (!kvm_vm_is_protected(kvm))
+        return;
+
+    /* Fix the counter offset to 0 and mark the offset initialised */
+    kvm->arch.timer_data.poffset = kvm->arch.timer_data.voffset = 0;
+    set_bit(KVM_ARCH_FLAG_VM_COUNTER_OFFSET, &kvm->arch.flags);
+}
+

  /**
   * kvm_arch_init_vm - initializes a VM data structure
   * @kvm:    pointer to the KVM struct

arch/arm64/kvm/arm.c int kvm_arch_init_vm(struct kvm *kvm, unsigned · line 284

Quoted 4 lines
arch/arm64/kvm/arm.c +1 -0
@@ -267,6 +281,7 @@ int kvm_arch_init_vm(struct kvm *kvm, unsigned 
 long type)
       kvm_vgic_early_init(kvm);
+    kvm_arch_fix_timer_offsets(kvm);
       kvm_timer_init_vm(kvm);

This should all be moved to the timer code.

Quoted 31 lines
arch/arm64/kvm/arm.c +0 -0
       /* The maximum number of VCPUs is limited by the host's GIC model */
arch/arm64/kvm/hyp/nvhe/pkvm.c +9 -13
index 459bd9eb7e4bc..e7b38eff63bd1 100644
@@ -528,19 +528,19 @@ static int init_pkvm_hyp_vcpu(struct 
 pkvm_hyp_vcpu *hyp_vcpu,
       hyp_vcpu->vcpu.arch.cflags = READ_ONCE(host_vcpu->arch.cflags);
       hyp_vcpu->vcpu.arch.mp_state.mp_state = KVM_MP_STATE_STOPPED;
-    if (!pkvm_hyp_vcpu_is_protected(hyp_vcpu)) {
-        /*
-         * Timer offsets are pointing to the untrusted KVM copy,
-         * which is pinned in __pkvm_init_vm() for the VM life time.
-         * It is worth noting that hyp_vm->host_kvm points to an EL2
-         * linear map address and timer_get_offset() will use
-         * kern_hyp_va() which is safe as it is idempotent.
-         */
-        vcpu_vtimer(&hyp_vcpu->vcpu)->offset.vm_offset =
-            &hyp_vm->host_kvm->arch.timer_data.voffset;
-        vcpu_ptimer(&hyp_vcpu->vcpu)->offset.vm_offset =
-            &hyp_vm->host_kvm->arch.timer_data.poffset;
-    }
+    /*
+     * Timer offsets are pointing to the untrusted KVM copy,
+     * which is pinned in __pkvm_init_vm() for the VM life time.
+     * It is worth noting that hyp_vm->host_kvm points to an EL2
+     * linear map address and timer_get_offset() will use
+     * kern_hyp_va() which is safe as it is idempotent.
+     * Also for protected VMs the offset is fixed to 0 and is prevented
+     * from changing.
+     */
Quoted 8 lines

arch/arm64/kvm/hyp/nvhe/pkvm.c static int init_pkvm_hyp_vcpu(struct · line 543

Quoted 4 lines
arch/arm64/kvm/hyp/nvhe/pkvm.c +4 -0
+    vcpu_vtimer(&hyp_vcpu->vcpu)->offset.vm_offset =
+        &hyp_vm->host_kvm->arch.timer_data.voffset;
+    vcpu_ptimer(&hyp_vcpu->vcpu)->offset.vm_offset =
+        &hyp_vm->host_kvm->arch.timer_data.poffset;

I don't think this is right. Protected guests have no offset, and this needs to be ensured by the hypervisor. Here, the host can change the offset any time it wants, and that's not acceptable.

Ah, you're right. :facepalm:

Quoted 12 lines
Quoted 12 lines
arch/arm64/kvm/hyp/nvhe/pkvm.c +0 -0
       ret = pkvm_vcpu_init_sysregs(hyp_vcpu);
       if (ret)
include/kvm/arm_arch_timer.h +0 -1
index bc6f2fdd7ad33..4f0aa3bb69f45 100644
@@ -176,8 +176,7 @@ static inline bool has_cntpoff(void)
           if (__ctxt) {                        \
               struct arch_timer_offset *ato = &__ctxt->offset;\
                                       \
-            if (ato->vm_offset)                \
Quoted 8 lines

include/kvm/arm_arch_timer.h static inline bool has_cntpoff(void) · line 182

Quoted 5 lines
include/kvm/arm_arch_timer.h +1 -1
-                off += *KERN_HYP_VA(ato->vm_offset);    \
+            off += *KERN_HYP_VA(ato->vm_offset);        \
               if (ato->vcpu_offset)                \
                   off += *KERN_HYP_VA(ato->vcpu_offset);    \
           }                            \

And as you drop the previous hunk, this also needs to be restored to its original state.

Quoted 3 lines

Ack.

Suzuki

This is how it looks like now, reworked.

arch/arm64/kvm/arch_timer.c +0 -0
index 6ac3321f4c575..1c8fac222a643 100644
@@ -1110,8 +1110,7 @@ void kvm_timer_vcpu_init(struct kvm_vcpu *vcpu)
                timer_context_init(vcpu, i);
 
        /* Synchronize offsets across timers of a VM if not already 

provided */
- if (!vcpu_is_protected(vcpu) &&
- !test_bit(KVM_ARCH_FLAG_VM_COUNTER_OFFSET, &vcpu->kvm->arch.flags)) {

+       if (!test_bit(KVM_ARCH_FLAG_VM_COUNTER_OFFSET, &vcpu->kvm->arch.flags)) {
                timer_set_offset(vcpu_vtimer(vcpu), kvm_phys_timer_read());
                timer_set_offset(vcpu_ptimer(vcpu), 0);
        }
@@ -1133,6 +1132,15 @@ void kvm_timer_init_vm(struct kvm *kvm)
         */
        for (int i = 0; i < NR_KVM_TIMERS; i++)
                kvm->arch.timer_data.ppi[i] = get_vgic_ppi(kvm, 
default_ppi[i]);
+
+       /*
+        * For the protected VMs, fix the counter offset to 0 by marking the
+        * KVM_ARCH_FLAG_VM_COUNTER_OFFSET. This would also prevent userspace
+        * writes to CNTVCT_ELO & CNTPCT_EL0.
+        * The user can't set the offset via vm ioctl KVM_ARM_SET_COUNTER_OFFSET.
+        */

+ if (kvm_vm_is_protected(kvm))
+ set_bit(KVM_ARCH_FLAG_VM_COUNTER_OFFSET, &kvm->arch.flags);
}

void kvm_timer_cpu_up(void)

maintainer
#6 lore raw

Re: [PATCH v18 01/23] KVM: arm64: protected VM: Handle set_one_reg CNTVCT_EL0/CNTPCT_EL0

Quoted 33 lines · Suzuki K Poulose

This is how it looks like now, reworked.

arch/arm64/kvm/arch_timer.c +0 -0
index 6ac3321f4c575..1c8fac222a643 100644
@@ -1110,8 +1110,7 @@ void kvm_timer_vcpu_init(struct kvm_vcpu *vcpu)
                timer_context_init(vcpu, i);
 
        /* Synchronize offsets across timers of a VM if not already

provided */

arch/arm64/kvm/arch_timer.c +0 -2
-       if (!vcpu_is_protected(vcpu) &&
-           !test_bit(KVM_ARCH_FLAG_VM_COUNTER_OFFSET,

&vcpu->kvm->arch.flags)) {
+ if (!test_bit(KVM_ARCH_FLAG_VM_COUNTER_OFFSET,

&vcpu->kvm->arch.flags)) {
                timer_set_offset(vcpu_vtimer(vcpu), kvm_phys_timer_read());
                timer_set_offset(vcpu_ptimer(vcpu), 0);
        }
arch/arm64/kvm/arch_timer.c +4 -0
@@ -1133,6 +1132,15 @@ void kvm_timer_init_vm(struct kvm *kvm)
         */
        for (int i = 0; i < NR_KVM_TIMERS; i++)
                kvm->arch.timer_data.ppi[i] = get_vgic_ppi(kvm,
 default_ppi[i]);
+
+       /*
+        * For the protected VMs, fix the counter offset to 0 by marking the
+        * KVM_ARCH_FLAG_VM_COUNTER_OFFSET. This would also prevent

userspace

arch/arm64/kvm/arch_timer.c +2 -0
+        * writes to CNTVCT_ELO & CNTPCT_EL0.
+        * The user can't set the offset via vm ioctl
Quoted 2 lines

KVM_ARM_SET_COUNTER_OFFSET.
+ */

This is a bit wordy. Something like:

Protected VMs don't allow any offset being set from userspace, either set via writes to the counters or using the dedicated ioctl. Pretend the offset has already been set and rely on the default offset being 0.

arch/arm64/kvm/arch_timer.c

Quoted 5 lines
arch/arm64/kvm/arch_timer.c +2 -0
+       if (kvm_vm_is_protected(kvm))
+               set_bit(KVM_ARCH_FLAG_VM_COUNTER_OFFSET, &kvm->arch.flags);
 }
 
 void kvm_timer_cpu_up(void)

Otherwise LGTM.

	M.
Signature · 1 line
Without deviation from the norm, progress is not possible.
author reviewer
#7 lore raw

Re: [PATCH v18 01/23] KVM: arm64: protected VM: Handle set_one_reg CNTVCT_EL0/CNTPCT_EL0

Quoted 45 lines · Marc Zyngier
Quoted 33 lines · Suzuki K Poulose

This is how it looks like now, reworked.

arch/arm64/kvm/arch_timer.c +0 -0
index 6ac3321f4c575..1c8fac222a643 100644
@@ -1110,8 +1110,7 @@ void kvm_timer_vcpu_init(struct kvm_vcpu *vcpu)
                 timer_context_init(vcpu, i);
 
         /* Synchronize offsets across timers of a VM if not already

provided */

arch/arm64/kvm/arch_timer.c +0 -2
-       if (!vcpu_is_protected(vcpu) &&
-           !test_bit(KVM_ARCH_FLAG_VM_COUNTER_OFFSET,

&vcpu->kvm->arch.flags)) {
+ if (!test_bit(KVM_ARCH_FLAG_VM_COUNTER_OFFSET,

&vcpu->kvm->arch.flags)) {
                 timer_set_offset(vcpu_vtimer(vcpu), kvm_phys_timer_read());
                 timer_set_offset(vcpu_ptimer(vcpu), 0);
         }
arch/arm64/kvm/arch_timer.c +4 -0
@@ -1133,6 +1132,15 @@ void kvm_timer_init_vm(struct kvm *kvm)
          */
         for (int i = 0; i < NR_KVM_TIMERS; i++)
                 kvm->arch.timer_data.ppi[i] = get_vgic_ppi(kvm,
 default_ppi[i]);
+
+       /*
+        * For the protected VMs, fix the counter offset to 0 by marking the
+        * KVM_ARCH_FLAG_VM_COUNTER_OFFSET. This would also prevent

userspace

arch/arm64/kvm/arch_timer.c +2 -0
+        * writes to CNTVCT_ELO & CNTPCT_EL0.
+        * The user can't set the offset via vm ioctl
Quoted 2 lines

KVM_ARM_SET_COUNTER_OFFSET.
+ */

This is a bit wordy. Something like:

Protected VMs don't allow any offset being set from userspace, either set via writes to the counters or using the dedicated ioctl. Pretend the offset has already been set and rely on the default offset being 0.

Quoted 7 lines

arch/arm64/kvm/arch_timer.c

Quoted 5 lines
arch/arm64/kvm/arch_timer.c +2 -0
+       if (kvm_vm_is_protected(kvm))
+               set_bit(KVM_ARCH_FLAG_VM_COUNTER_OFFSET, &kvm->arch.flags);
  }
 
  void kvm_timer_cpu_up(void)

Otherwise LGTM.

Thanks,

Cheers
Suzuki

Quoted 3 lines
	M.
author reviewer
#8 lore raw

PATCH v18 2/23 KVM: arm64: Disable Steal time accounting for protected guests

In reply to Suzuki K Poulose

PVTIME support is advertised by KVM_CAP_STEAL_TIME, which doesn't take into account the kvm instance. Even with that, a VMM could skip the CAP check and proceed to configure the PVTIME as we don't do further check on the DEVICE_CTRL. Tighten this up by passing the KVM instance around wherever possible and catch things early

---
 arch/arm64/include/asm/kvm_host.h |  2 +-
 arch/arm64/kvm/arm.c              |  2 +-
 arch/arm64/kvm/pvtime.c           | 14 +++++++-------
 3 files changed, 9 insertions(+), 9 deletions(-)
arch/arm64/include/asm/kvm_host.h +1 -1
index 27fe0cd5b2d7a..286489a69dff5 100644
@@ -1346,7 +1346,7 @@ long kvm_hypercall_pv_features(struct kvm_vcpu *vcpu);
 gpa_t kvm_init_stolen_time(struct kvm_vcpu *vcpu);
 void kvm_update_stolen_time(struct kvm_vcpu *vcpu);
 
-bool kvm_arm_pvtime_supported(void);
+bool kvm_arm_pvtime_supported(struct kvm *kvm);
 int kvm_arm_pvtime_set_attr(struct kvm_vcpu *vcpu,
 			    struct kvm_device_attr *attr);
 int kvm_arm_pvtime_get_attr(struct kvm_vcpu *vcpu,
arch/arm64/kvm/arm.c +1 -1
index 7c88508cac8a1..3fbdfce926475 100644
@@ -462,7 +462,7 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
 		r = system_supports_mte();
 		break;
 	case KVM_CAP_STEAL_TIME:
-		r = kvm_arm_pvtime_supported();
+		r = kvm_arm_pvtime_supported(kvm);
 		break;
 	case KVM_CAP_ARM_EL1_32BIT:
 		r = cpus_have_final_cap(ARM64_HAS_32BIT_EL1);
arch/arm64/kvm/pvtime.c +7 -7
index 4ceabaa4c30bd..579e0a4720ad2 100644
@@ -67,9 +67,9 @@ gpa_t kvm_init_stolen_time(struct kvm_vcpu *vcpu)
 	return base;
 }
 
-bool kvm_arm_pvtime_supported(void)
+bool kvm_arm_pvtime_supported(struct kvm *kvm)
 {
-	return !!sched_info_on();
+	return !!sched_info_on() && (!kvm || !kvm_vm_is_protected(kvm));
 }
 
 int kvm_arm_pvtime_set_attr(struct kvm_vcpu *vcpu,
@@ -81,8 +81,8 @@ int kvm_arm_pvtime_set_attr(struct kvm_vcpu *vcpu,
 	int ret = 0;
 	int idx;
 
-	if (!kvm_arm_pvtime_supported() ||
-	    attr->attr != KVM_ARM_VCPU_PVTIME_IPA)
+	if (!kvm_arm_pvtime_supported(kvm) ||
+	    (attr->attr != KVM_ARM_VCPU_PVTIME_IPA))
 		return -ENXIO;
 
 	if (get_user(ipa, user))
@@ -110,8 +110,8 @@ int kvm_arm_pvtime_get_attr(struct kvm_vcpu *vcpu,
 	u64 __user *user = (u64 __user *)attr->addr;
 	u64 ipa;
 
-	if (!kvm_arm_pvtime_supported() ||
-	    attr->attr != KVM_ARM_VCPU_PVTIME_IPA)
+	if (!kvm_arm_pvtime_supported(vcpu->kvm) ||
+	    (attr->attr != KVM_ARM_VCPU_PVTIME_IPA))
 		return -ENXIO;
 
 	ipa = vcpu->arch.steal.base;
@@ -126,7 +126,7 @@ int kvm_arm_pvtime_has_attr(struct kvm_vcpu *vcpu,
 {
 	switch (attr->attr) {
 	case KVM_ARM_VCPU_PVTIME_IPA:
-		if (kvm_arm_pvtime_supported())
+		if (kvm_arm_pvtime_supported(vcpu->kvm))
 			return 0;
 	}
 	return -ENXIO;
Signature · 1 line
2.43.0
author reviewer
#9 lore raw

PATCH v18 3/23 KVM: arm64: Include kvm_emulate.h in kvm/arm_psci.h

In reply to Suzuki K Poulose

Fix a potential build error (like below, when asm/kvm_emulate.h gets included after the kvm/arm_psci.h) by including the missing header file in kvm/arm_psci.h:

./include/kvm/arm_psci.h: In function ‘kvm_psci_version’:
./include/kvm/arm_psci.h:29:13: error: implicit declaration of function
‘vcpu_has_feature’; did you mean ‘cpu_have_feature’? [-Werror=implicit-function-declaration]

   29 |         if (vcpu_has_feature(vcpu, KVM_ARM_VCPU_PSCI_0_2)) {
	         |             ^~~~~~~~~~~~~~~~
			       |             cpu_have_feature
---
 include/kvm/arm_psci.h | 2 ++
 1 file changed, 2 insertions(+)
include/kvm/arm_psci.h +2 -0
index f86a006d67136..06c20612e9e7d 100644
@@ -10,6 +10,8 @@
 #include <linux/kvm_host.h>
 #include <uapi/linux/psci.h>
 
+#include <asm/kvm_emulate.h>
+
 #define KVM_ARM_PSCI_0_1	PSCI_VERSION(0, 1)
 #define KVM_ARM_PSCI_0_2	PSCI_VERSION(0, 2)
 #define KVM_ARM_PSCI_1_0	PSCI_VERSION(1, 0)
Signature · 1 line
2.43.0
author reviewer
#10 lore raw

PATCH v18 4/23 KVM: arm64: Avoid including linux/kvm_host.h in kvm_pgtable.h

In reply to Suzuki K Poulose

From: Steven Price <[email protected]>

To avoid future include cycles, drop the linux/kvm_host.h include in kvm_pgtable.h and include the lightweight headers required for the types and inline helpers used there. Additionally provide a forward declaration for struct kvm_s2_mmu as it's only used as a pointer in this file.

Both pgtable.c and kvm_pkvm.h relied on the indirect inclusion of kvm_host.h, so make that explicit.

---
 arch/arm64/include/asm/kvm_pgtable.h | 6 +++++-
 arch/arm64/include/asm/kvm_pkvm.h    | 2 +-
 arch/arm64/kvm/hyp/pgtable.c         | 1 +
 3 files changed, 7 insertions(+), 2 deletions(-)
arch/arm64/include/asm/kvm_pgtable.h +5 -1
index 41a8687938eb6..c2e4b29e605fc 100644
@@ -8,9 +8,13 @@
 #define __ARM64_KVM_PGTABLE_H__
 
 #include <linux/bits.h>
-#include <linux/kvm_host.h>
+#include <linux/kvm_types.h>
+#include <linux/rbtree_types.h>
+#include <linux/rcupdate.h>
 #include <linux/types.h>
 
+struct kvm_s2_mmu;
+
 #define KVM_PGTABLE_FIRST_LEVEL		-1
 #define KVM_PGTABLE_LAST_LEVEL		3
 
arch/arm64/include/asm/kvm_pkvm.h +1 -1
index beea00e693a0a..54a618d887fa4 100644
@@ -7,9 +7,9 @@
 #define __ARM64_KVM_PKVM_H__
 
 #include <linux/arm_ffa.h>
+#include <linux/kvm_host.h>
 #include <linux/memblock.h>
 #include <linux/scatterlist.h>
-#include <asm/kvm_host.h>
 #include <asm/kvm_pgtable.h>
 
 /* Maximum number of VMs that can co-exist under pKVM. */
arch/arm64/kvm/hyp/pgtable.c +1 -0
index b74dd5ce1efd3..f48253b9d88b5 100644
@@ -8,6 +8,7 @@
  */
 
 #include <linux/bitfield.h>
+#include <linux/kvm_host.h>
 #include <asm/kvm_pgtable.h>
 #include <asm/stage2_pgtable.h>
 
Signature · 1 line
2.43.0
author reviewer
#11 lore raw

PATCH v18 5/23 KVM: arm64: Track the type of VM in kvm_arch

In reply to Suzuki K Poulose

KVM arm64 has different types of VMs with all the different modes in which the hypervisor code can be run. e.g., VHE, nVHE, PKVM etc. Then there is protected VM and normal VMs with PKVM. We might soon add other types, e.g., Arm CCA Realm. So in an effort to make the handling of these different types of VMs a bit more friendly to the eyes, add a VM flavor to the kvm_arch and we could then add handlers for different operations based on the VM type.

Keep the flavor initialisation at the beginning to allow for the detection early enough and fail out on any unsupported requests. (e.g., protected on !pKVM)

With that, use the vm_flavor to detect if a VM is protected VM on PKVM. A later patch would generalize the "protected" VM predicate to cater for all confidential compute VMs.

Based on a patch by Marc Zyngier

---
Changes since v17:
* s/PKVM/pKVM for the comments
* Drop type argument for pkvm_init_host_vm and also drop protected variable.
* Add helpers for checking if the VM is running on pKVM (kvm_vm_hyp_is_pkvm())
* Use kvm_vm_hyp_is_pkvm() to replace is_protected_kvm_enabled() with valid kvm instance

---
 arch/arm64/include/asm/kvm_host.h | 14 +++++++++++--
 arch/arm64/include/asm/kvm_pkvm.h |  2 +-
 arch/arm64/kvm/arm.c              | 35 ++++++++++++++++++++++++-------
 arch/arm64/kvm/hyp/nvhe/pkvm.c    |  2 +-
 arch/arm64/kvm/pkvm.c             |  6 ++----
 5 files changed, 44 insertions(+), 15 deletions(-)
arch/arm64/include/asm/kvm_host.h +12 -2
index 286489a69dff5..39d04ff702bc1 100644
@@ -257,7 +257,6 @@ struct kvm_protected_vm {
 	pkvm_handle_t handle;
 	struct kvm_hyp_memcache teardown_mc;
 	struct kvm_hyp_memcache stage2_teardown_mc;
-	bool is_protected;
 	bool is_created;
 
 	/*
@@ -306,9 +305,18 @@ enum fgt_group_id {
 	__NR_FGT_GROUP_IDS__
 };
 
+enum kvm_arm_vm_flavor {
+	VM_NVHE,
+	VM_VHE,
+	VM_PKVM,		/* Normal guests on pKVM */
+	VM_PROTECTED_PKVM,	/* Protected VM */
+	VM_FLAVOR_MAX,
+};
+
 struct kvm_arch {
 	struct kvm_s2_mmu mmu;
 
+	enum kvm_arm_vm_flavor vm_flavor;
 	/*
 	 * Fine-Grained UNDEF, mimicking the FGT layout defined by the
 	 * architecture. We track them globally, as we present the
@@ -1504,8 +1512,10 @@ struct kvm *kvm_arch_alloc_vm(void);
 
 #define __KVM_HAVE_ARCH_FLUSH_REMOTE_TLBS_RANGE
 
-#define kvm_vm_is_protected(kvm)	(is_protected_kvm_enabled() && (kvm)->arch.pkvm.is_protected)
+#define kvm_vm_is_protected(kvm)	((kvm)->arch.vm_flavor == VM_PROTECTED_PKVM)
+#define kvm_vm_is_unprotected_pkvm(kvm)	((kvm)->arch.vm_flavor == VM_PKVM)
 
+#define kvm_vm_hyp_is_pkvm(kvm)		(is_protected_kvm_enabled())
 #define vcpu_is_protected(vcpu)		kvm_vm_is_protected((vcpu)->kvm)
 
 int kvm_arm_vcpu_finalize(struct kvm_vcpu *vcpu, int feature);
arch/arm64/include/asm/kvm_pkvm.h +1 -1
index 54a618d887fa4..a1f3e05e75dc1 100644
@@ -17,7 +17,7 @@
 
 #define HYP_MEMBLOCK_REGIONS 128
 
-int pkvm_init_host_vm(struct kvm *kvm, unsigned long type);
+int pkvm_init_host_vm(struct kvm *kvm);
 int pkvm_create_hyp_vm(struct kvm *kvm);
 bool pkvm_hyp_vm_is_created(struct kvm *kvm);
 void pkvm_destroy_hyp_vm(struct kvm *kvm);
arch/arm64/kvm/arm.c +28 -7
index 3fbdfce926475..4329c49fe49da 100644
@@ -228,6 +228,26 @@ static void kvm_arch_fix_timer_offsets(struct kvm *kvm)
 	set_bit(KVM_ARCH_FLAG_VM_COUNTER_OFFSET, &kvm->arch.flags);
 }
 
+static int kvm_init_vm_flavor(struct kvm *kvm, unsigned long type)
+{
+	bool protected = type & KVM_VM_TYPE_ARM_PROTECTED;
+
+	if (is_protected_kvm_enabled()) {
+		if (protected)
+			kvm->arch.vm_flavor = VM_PROTECTED_PKVM;
+		else
+			kvm->arch.vm_flavor = VM_PKVM;
+	} else if (protected) {
+		return -EINVAL;
+	} else if (has_vhe()) {
+		kvm->arch.vm_flavor = VM_VHE;
+	} else {
+		kvm->arch.vm_flavor = VM_NVHE;
+	}
+
+	return 0;
+}
+
 /**
  * kvm_arch_init_vm - initializes a VM data structure
  * @kvm:	pointer to the KVM struct
@@ -250,6 +270,10 @@ int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)
 	mutex_unlock(&kvm->lock);
 #endif
 
+	ret = kvm_init_vm_flavor(kvm, type);
+	if (ret)
+		return ret;
+
 	kvm_init_nested(kvm);
 
 	ret = kvm_share_hyp(kvm, kvm + 1);
@@ -266,17 +290,14 @@ int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)
 	if (ret)
 		goto err_free_cpumask;
 
-	if (is_protected_kvm_enabled()) {
+	if (kvm_vm_hyp_is_pkvm(kvm)) {
 		/*
 		 * If any failures occur after this is successful, make sure to
 		 * call __pkvm_unreserve_vm to unreserve the VM in hyp.
 		 */
-		ret = pkvm_init_host_vm(kvm, type);
+		ret = pkvm_init_host_vm(kvm);
 		if (ret)
 			goto err_uninit_mmu;
-	} else if (type & KVM_VM_TYPE_ARM_PROTECTED) {
-		ret = -EINVAL;
-		goto err_uninit_mmu;
 	}
 
 	kvm_vgic_early_init(kvm);
@@ -341,7 +362,7 @@ void kvm_arch_destroy_vm(struct kvm *kvm)
 
 	kvm_vgic_destroy(kvm);
 
-	if (is_protected_kvm_enabled())
+	if (kvm_vm_hyp_is_pkvm(kvm))
 		pkvm_destroy_hyp_vm(kvm);
 
 	kvm_uninit_stage2_mmu(kvm);
@@ -603,7 +624,7 @@ void kvm_arch_vcpu_postcreate(struct kvm_vcpu *vcpu)
 
 void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)
 {
-	if (!is_protected_kvm_enabled())
+	if (!kvm_vm_hyp_is_pkvm(vcpu->kvm))
 		kvm_mmu_free_memory_cache(&vcpu->arch.mmu_page_cache);
 	else
 		free_hyp_memcache(&vcpu->arch.pkvm_memcache);
arch/arm64/kvm/hyp/nvhe/pkvm.c +1 -1
index e7b38eff63bd1..9b69228f8402c 100644
@@ -432,7 +432,7 @@ static void init_pkvm_hyp_vm(struct kvm *host_kvm, struct pkvm_hyp_vm *hyp_vm,
 
 	hyp_vm->host_kvm = host_kvm;
 	hyp_vm->kvm.created_vcpus = nr_vcpus;
-	hyp_vm->kvm.arch.pkvm.is_protected = READ_ONCE(host_kvm->arch.pkvm.is_protected);
+	hyp_vm->kvm.arch.vm_flavor = READ_ONCE(host_kvm->arch.vm_flavor);
 	hyp_vm->kvm.arch.flags = 0;
 	pkvm_init_features_from_host(hyp_vm, host_kvm);
 
arch/arm64/kvm/pkvm.c +2 -4
index 8e4c6e4bec123..8e9176a700926 100644
@@ -229,10 +229,9 @@ void pkvm_destroy_hyp_vm(struct kvm *kvm)
 	mutex_unlock(&kvm->arch.config_lock);
 }
 
-int pkvm_init_host_vm(struct kvm *kvm, unsigned long type)
+int pkvm_init_host_vm(struct kvm *kvm)
 {
 	int ret;
-	bool protected = type & KVM_VM_TYPE_ARM_PROTECTED;
 
 	/* Reserve the VM in hyp and obtain a hyp handle for the VM. */
 	ret = kvm_call_hyp_nvhe(__pkvm_reserve_vm);
@@ -240,8 +239,7 @@ int pkvm_init_host_vm(struct kvm *kvm, unsigned long type)
 		return ret;
 
 	kvm->arch.pkvm.handle = ret;
-	kvm->arch.pkvm.is_protected = protected;
-	if (protected) {
+	if (kvm_vm_is_protected(kvm)) {
 		pr_warn_once("kvm: protected VMs are experimental and for development only, tainting kernel\n");
 		add_taint(TAINT_USER, LOCKDEP_STILL_OK);
 	}
Signature · 1 line
2.43.0
reviewer
#12 lore raw

Re: [PATCH v18 05/23] KVM: arm64: Track the type of VM in kvm_arch

Hi Suzuki,

On Tue, 15 Sep 2026 17:01:23 +0100, Suzuki K Poulose <[email protected]> wrote:
[...]

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

[...]

Quoted 1 line

+#define kvm_vm_hyp_is_pkvm(kvm) (is_protected_kvm_enabled())

I'd keep is_protected_kvm_enabled() at the three call sites: this takes a VM and ignores it, so it reads as per-VM when it's the mode, and patch 20's comment about not being able to use the kvm_vm_ helpers with a NULL kvm goes away too.

Quoted 1 line
arch/arm64/kvm/hyp/nvhe/pkvm.c +0 -0

[...]

arch/arm64/kvm/hyp/nvhe/pkvm.c static void init_pkvm_hyp_vm(struct kvm *host_kvm, struct pkvm_hyp_vm *hyp_vm, · line 435

Quoted 6 lines
arch/arm64/kvm/hyp/nvhe/pkvm.c +1 -1
@@ -432,7 +432,7 @@ static void init_pkvm_hyp_vm(struct kvm *host_kvm, struct pkvm_hyp_vm *hyp_vm,
 
         hyp_vm->host_kvm = host_kvm;
         hyp_vm->kvm.created_vcpus = nr_vcpus;
-        hyp_vm->kvm.arch.pkvm.is_protected = READ_ONCE(host_kvm->arch.pkvm.is_protected);
+        hyp_vm->kvm.arch.vm_flavor = READ_ONCE(host_kvm->arch.vm_flavor);

Could EL2 reduce this to VM_PKVM or VM_PROTECTED_PKVM here instead of storing what it reads? The host supplies the value, and it's now an enum rather than a bool. Patch 12 relies on EL2 only ever seeing those two, and with VM_REALM above the marker a host-written value passes kvm_vm_is_protected() but not the == form, if EL2 ever adds one.

Both hunks are new since v17, which is where I gave the Reviewed-by. Happy to keep it if you can settle these please.

Cheers,
/fuad

author reviewer
#13 lore raw

Re: [PATCH v18 05/23] KVM: arm64: Track the type of VM in kvm_arch

Quoted 5 lines · Fuad Tabba

Hi Suzuki,

On Tue, 15 Sep 2026 17:01:23 +0100, Suzuki K Poulose <[email protected]> wrote:
[...]

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

[...]

Quoted 1 line

+#define kvm_vm_hyp_is_pkvm(kvm) (is_protected_kvm_enabled())

I'd keep is_protected_kvm_enabled() at the three call sites: this takes a VM and ignores it, so it reads as per-VM when it's the mode, and patch 20's comment about not being able to use the kvm_vm_ helpers with a NULL kvm goes away too.

Actually, that comment is invalid. kvm_arch_vm_ioctl_allowed, is only
called from the kvm_arch_vm_ioctl() with a valid kvm instance. I have changed that hunk to:

+/*
+ * Check whether the KVM VM IOCTL is allowed. For pKVM and Realm VMs, certain
+ * ioctls are not allowed. Further, certain features are allowed only for
+ * non-protected VMs in pKVM.
+ */
+static inline bool kvm_arch_vm_ioctl_allowed(struct kvm *kvm, unsigned int ioctl) +{
+ long ext;
+ int r;
+
+ /*

+        * We are guaranteed to be called with a valid kvm instance, as the
+        * only caller is kvm_arch_vm_ioctl(). Catch any deviations, as we
+        * rely on the kvm instance below.
+        */

+ if (WARN_ON_ONCE(!kvm))
+ return false;
+
+ /* Cover both pKVM host and Realm VMs */
+ if (!kvm_vm_hyp_is_distrusting(kvm))
+ return true;
+

I resorted to is_protected_kvm_enabled() to make it faster than using the vm_flavor checks. I could rever those anyways.

Quoted 5 lines
Quoted 2 lines
arch/arm64/kvm/hyp/nvhe/pkvm.c +0 -0

[...]

Quoted 2 lines
arch/arm64/kvm/hyp/nvhe/pkvm.c +0 -0
@@ -432,7 +432,7 @@ static void init_pkvm_hyp_vm(struct kvm *host_kvm, struct pkvm_hyp_vm *hyp_vm,
 
Quoted 10 lines

arch/arm64/kvm/hyp/nvhe/pkvm.c static void init_pkvm_hyp_vm(struct kvm *host_kvm, struct pkvm_hyp_vm *hyp_vm, · line 435

Quoted 4 lines
arch/arm64/kvm/hyp/nvhe/pkvm.c +1 -1
          hyp_vm->host_kvm = host_kvm;
          hyp_vm->kvm.created_vcpus = nr_vcpus;
-        hyp_vm->kvm.arch.pkvm.is_protected = READ_ONCE(host_kvm->arch.pkvm.is_protected);
+        hyp_vm->kvm.arch.vm_flavor = READ_ONCE(host_kvm->arch.vm_flavor);

Could EL2 reduce this to VM_PKVM or VM_PROTECTED_PKVM here instead of storing what it reads? The host supplies the value, and it's now an enum rather than a bool. Patch 12 relies on EL2 only ever seeing those two, and with VM_REALM above the marker a host-written value passes kvm_vm_is_protected() but not the == form, if EL2 ever adds one.

Do you mean something like :

	if (kvm_vm_is_protected_pkvm(host_kvm))
		hyp_vm->kvm.arch.vm_flavor = VM_PROTECTED_PKVM;
	else
		hyp_vm->kvm.arch.vm_flavor = VM_PKVM;

Do we additionally need to WARN_ON() if we encounter a VM_REALM ?

Quoted 3 lines

Both hunks are new since v17, which is where I gave the Reviewed-by. Happy to keep it if you can settle these please.

Apologies, I thought this one didn't change, but later patches changes them, which is why I kept this.

Cheers
Suzuki

reviewer
#14 lore raw

Re: [PATCH v18 05/23] KVM: arm64: Track the type of VM in kvm_arch

Hi Suzuki,

On <date>, Suzuki K Poulose <[email protected]> wrote:
[...]

Review

Quoted 6 lines
(quoted diff) +6 -0
+       if (WARN_ON_ONCE(!kvm))
+               return false;
+
+       /* Cover both pKVM host and Realm VMs */
+       if (!kvm_vm_hyp_is_distrusting(kvm))
+               return true;

I'd drop the WARN_ON_ONCE. And this early return needs the >= from patch 13, or non-protected pKVM VMs skip the filter.

Quoted 2 lines

I resorted to is_protected_kvm_enabled() to make it faster than using the vm_flavor checks. I could rever those anyways.

Agreed it's the faster test. I'd just call it by name at the three sites rather than through a macro.

Quoted 6 lines

Do you mean something like :

        if (kvm_vm_is_protected_pkvm(host_kvm))
                hyp_vm->kvm.arch.vm_flavor = VM_PROTECTED_PKVM;
        else
                hyp_vm->kvm.arch.vm_flavor = VM_PKVM;

Yes, exactly that.

Quoted 1 line

Do we additionally need to WARN_ON() if we encounter a VM_REALM ?

No, the host shouldn't be able to trigger a WARN at EL2.

Quoted 2 lines

Apologies, I thought this one didn't change, but later patches changes them, which is why I kept this.

No worries. With those two in, please keep the tag.

Cheers,
/fuad

author reviewer
#15 lore raw

Re: [PATCH v18 05/23] KVM: arm64: Track the type of VM in kvm_arch

Quoted 4 lines · Fuad Tabba

Hi Suzuki,

On <date>, Suzuki K Poulose <[email protected]> wrote:
[...]

Quoted 8 lines

Review

Quoted 6 lines
(quoted diff) +6 -0
+       if (WARN_ON_ONCE(!kvm))
+               return false;
+
+       /* Cover both pKVM host and Realm VMs */
+       if (!kvm_vm_hyp_is_distrusting(kvm))
+               return true;

I'd drop the WARN_ON_ONCE.

I would say, we need it. i.e., we are filtering kvm_arch_vm_ioctl(), where it is called from the kvm instance ioctl. So, kvm must be !NULL.

Quoted 3 lines

And this early return needs the >= from patch 13, or non-protected pKVM VMs skip the filter.

Of course, This snippet is for Patch 20 and I have fixed the comparison.

Quoted 13 lines
Quoted 2 lines

I resorted to is_protected_kvm_enabled() to make it faster than using the vm_flavor checks. I could rever those anyways.

Agreed it's the faster test. I'd just call it by name at the three sites rather than through a macro.

Quoted 6 lines

Do you mean something like :

         if (kvm_vm_is_protected_pkvm(host_kvm))
                 hyp_vm->kvm.arch.vm_flavor = VM_PROTECTED_PKVM;
         else
                 hyp_vm->kvm.arch.vm_flavor = VM_PKVM;
Quoted 5 lines

Yes, exactly that.

Quoted 1 line

Do we additionally need to WARN_ON() if we encounter a VM_REALM ?

No, the host shouldn't be able to trigger a WARN at EL2.

Alright.

Quoted 5 lines
Quoted 3 lines

Apologies, I thought this one didn't change, but later patches changes them, which is why I kept this.

No worries. With those two in, please keep the tag.

Thanks
Suzuki

Quoted 3 lines

Cheers,
/fuad

author reviewer
#16 lore raw

PATCH v18 6/23 KVM: arm64: Refactor the vcpu_load to allow for VM specific callbacks

In reply to Suzuki K Poulose

Based on a work by Marc Zyngier

To keep the VCPU load/put handling cleaner with the different kinds of VM types, we are about to introduce VM specific callbacks to do just the right thing. In preparation for that, make some refactoring to add the change easier.

No functional changes intended.

---
 arch/arm64/kvm/arm.c | 48 ++++++++++++++++++++++++++------------------
 1 file changed, 29 insertions(+), 19 deletions(-)
arch/arm64/kvm/arm.c +29 -19
index 4329c49fe49da..8f94323f2968b 100644
@@ -698,14 +698,11 @@ static bool kvm_vcpu_should_clear_twe(struct kvm_vcpu *vcpu)
 	return single_task_running();
 }
 
-void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
+static void vcpu_prepare_mmu(struct kvm_vcpu *vcpu)
 {
 	struct kvm_s2_mmu *mmu;
 	int *last_ran;
 
-	if (is_protected_kvm_enabled())
-		goto nommu;
-
 	if (vcpu_has_nv(vcpu))
 		kvm_vcpu_load_hw_mmu(vcpu);
 
@@ -735,10 +732,33 @@ void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
 		kvm_call_hyp(__kvm_flush_cpu_context, mmu);
 		*last_ran = vcpu->vcpu_idx;
 	}
+}
+
+static void vcpu_set_wfx_traps(struct kvm_vcpu *vcpu)
+{
+	if (kvm_vcpu_should_clear_twe(vcpu))
+		vcpu->arch.hcr_el2 &= ~HCR_TWE;
+	else
+		vcpu->arch.hcr_el2 |= HCR_TWE;
+
+	if (kvm_vcpu_should_clear_twi(vcpu))
+		vcpu->arch.hcr_el2 &= ~HCR_TWI;
+	else
+		vcpu->arch.hcr_el2 |= HCR_TWI;
+}
+
+static void vcpu_load_pvtime(struct kvm_vcpu *vcpu)
+{
+	if (kvm_arm_is_pvtime_enabled(&vcpu->arch))
+		kvm_make_request(KVM_REQ_RECORD_STEAL, vcpu);
+}
+
+void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
+{
+	if (!is_protected_kvm_enabled())
+		vcpu_prepare_mmu(vcpu);
 
-nommu:
 	vcpu->cpu = cpu;
-
 	/*
 	 * The timer must be loaded before the vgic to correctly set up physical
 	 * interrupt deactivation in nested state (e.g. timer interrupt).
@@ -751,19 +771,9 @@ void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
 		kvm_vcpu_load_vhe(vcpu);
 	kvm_arch_vcpu_load_fp(vcpu);
 	kvm_vcpu_pmu_restore_guest(vcpu);
-	if (kvm_arm_is_pvtime_enabled(&vcpu->arch))
-		kvm_make_request(KVM_REQ_RECORD_STEAL, vcpu);
-
-	if (kvm_vcpu_should_clear_twe(vcpu))
-		vcpu->arch.hcr_el2 &= ~HCR_TWE;
-	else
-		vcpu->arch.hcr_el2 |= HCR_TWE;
-
-	if (kvm_vcpu_should_clear_twi(vcpu))
-		vcpu->arch.hcr_el2 &= ~HCR_TWI;
-	else
-		vcpu->arch.hcr_el2 |= HCR_TWI;
 
+	vcpu_load_pvtime(vcpu);
+	vcpu_set_wfx_traps(vcpu);
 	vcpu_set_pauth_traps(vcpu);
 
 	if (is_protected_kvm_enabled()) {
@@ -787,7 +797,7 @@ void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
 		kvm_call_hyp_nvhe(__pkvm_vcpu_put);
 
 		/* __pkvm_vcpu_put implies a sync of the state */
-		if (!kvm_vm_is_protected(vcpu->kvm))
+		if (kvm_vm_is_unprotected_pkvm(vcpu->kvm))
 			vcpu_set_flag(vcpu, PKVM_HOST_STATE_DIRTY);
 	}
 
Signature · 1 line
2.43.0
author reviewer
#17 lore raw

PATCH v18 7/23 KVM: arm64: Add vcpu load/put call backs for flavors

In reply to Suzuki K Poulose

Add VM flavor specific handlers for VCPU load/put, in an effort to make it easier to follow the code. pauth traps were removed from VMs running PKVM as it is a no-op for them.

Based on a patch by Marc Zyngier

---
Changes since v17:
- Use macro to initialize the per-flavor ops
- Add a wrapper to initialise ops in the vcpu structure.
- Add BUILD_BUG_ON for the array size
- Remove irrelevant comment about the order of timer loading for !VHE
- Use the explicti kvm_call_hyp_nvhe for nVHE flavor
- Don't call nvhe_vcpu_put from pkvm_vcpu_put, open code them
- Drop cpu argument for vcpu_load() callback. We set the cpu before the callbacks are invoked

---
 arch/arm64/include/asm/kvm_host.h |   6 ++
 arch/arm64/kvm/arm.c              | 134 ++++++++++++++++++++++++------
 2 files changed, 114 insertions(+), 26 deletions(-)
arch/arm64/include/asm/kvm_host.h +6 -0
index 39d04ff702bc1..8601103717643 100644
@@ -150,6 +150,11 @@ struct kvm_vmid {
 	atomic64_t id;
 };
 
+struct kvm_vcpu_ops {
+	void (*vcpu_load)(struct kvm_vcpu *vcpu);
+	void (*vcpu_put)(struct kvm_vcpu *vcpu);
+};
+
 struct kvm_s2_mmu {
 	struct kvm_vmid vmid;
 
@@ -854,6 +859,7 @@ struct vncr_tlb;
 
 struct kvm_vcpu_arch {
 	struct kvm_cpu_context ctxt;
+	const struct kvm_vcpu_ops *vcpu_ops;
 
 	/*
 	 * Guest floating point state
arch/arm64/kvm/arm.c +108 -26

188 lines not on this page · raw message

Signature · 1 line
2.43.0
author reviewer
#18 lore raw

PATCH v18 8/23 KVM: arm64: Reuse kvm_stage2_unmap_range in kvm_unmap_gfn_range

In reply to Suzuki K Poulose

In preparation for adding VM specific backends for stage2 operation, switch to kvm_stage2_unmap_range() instead of __unmap_stage2_range() from the kvm_unmap_gfn_range(). Drop the bail out check for protected VMs and defer that to the one in kvm_stage2_unmap_range(). Later we would replace the logic in kvm_stage2_unmap_range() with VM specific backends.

No functional changes intended.

---
 arch/arm64/kvm/mmu.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
arch/arm64/kvm/mmu.c +4 -4
index 9ba86450fe4af..67852acf7a6f8 100644
@@ -2436,12 +2436,12 @@ int kvm_handle_guest_abort(struct kvm_vcpu *vcpu)
 
 bool kvm_unmap_gfn_range(struct kvm *kvm, struct kvm_gfn_range *range)
 {
-	if (!kvm->arch.mmu.pgt || kvm_vm_is_protected(kvm))
+	if (!kvm->arch.mmu.pgt)
 		return false;
 
-	__unmap_stage2_range(&kvm->arch.mmu, range->start << PAGE_SHIFT,
-			     (range->end - range->start) << PAGE_SHIFT,
-			     range->may_block);
+	kvm_stage2_unmap_range(&kvm->arch.mmu, range->start << PAGE_SHIFT,
+				(range->end - range->start) << PAGE_SHIFT,
+				range->may_block);
 
 	kvm_nested_s2_unmap(kvm, range->may_block);
 	return false;
Signature · 1 line
2.43.0
author reviewer
#19 lore raw

PATCH v18 9/23 KVM: arm64: Add VM specific callback for S2 MMU operations

In reply to Suzuki K Poulose

Add VM type specific S2 MMU operation backends which can be initialized per VM flavor, to keep the handling cleaner. Realm VM ops will be added gradually.

---
 arch/arm64/include/asm/kvm_host.h |  15 ++++
 arch/arm64/kvm/mmu.c              | 137 +++++++++++++++++++++++++-----
 2 files changed, 131 insertions(+), 21 deletions(-)
arch/arm64/include/asm/kvm_host.h +15 -0
index 8601103717643..ad3b34ba0b805 100644
@@ -155,6 +155,19 @@ struct kvm_vcpu_ops {
 	void (*vcpu_put)(struct kvm_vcpu *vcpu);
 };
 
+struct kvm_gfn_range;
+
+struct kvm_vm_s2_ops {
+	bool (*vm_age_gfn)(struct kvm *kvm, struct kvm_gfn_range *range);
+	bool (*vm_test_age_gfn)(struct kvm *kvm, struct kvm_gfn_range *range);
+	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_unmap_range)(struct kvm_s2_mmu *mmu,
+				      phys_addr_t start, u64 size,
+				      bool may_block);
+};
+
 struct kvm_s2_mmu {
 	struct kvm_vmid vmid;
 
@@ -331,6 +344,8 @@ struct kvm_arch {
 	 */
 	u64 fgu[__NR_FGT_GROUP_IDS__];
 
+	const struct kvm_vm_s2_ops *vm_s2_ops;
+
 	/*
 	 * Stage 2 paging state for VMs with nested S2 using a virtual
 	 * VMID.
arch/arm64/kvm/mmu.c +116 -21

210 lines not on this page · raw message

Signature · 1 line
2.43.0
author reviewer
#20 lore raw

PATCH v18 10/23 KVM: arm64: Abstract out memory abort handling

In reply to Suzuki K Poulose

Move the memory abort handling under VM specific s2 operation.

---
 arch/arm64/include/asm/kvm_host.h |  2 ++
 arch/arm64/kvm/mmu.c              | 36 +++++++++++++++++++------------
 2 files changed, 24 insertions(+), 14 deletions(-)
arch/arm64/include/asm/kvm_host.h +2 -0
index ad3b34ba0b805..7ce46d853c47e 100644
@@ -156,6 +156,7 @@ struct kvm_vcpu_ops {
 };
 
 struct kvm_gfn_range;
+struct kvm_s2_fault_desc;
 
 struct kvm_vm_s2_ops {
 	bool (*vm_age_gfn)(struct kvm *kvm, struct kvm_gfn_range *range);
@@ -166,6 +167,7 @@ struct kvm_vm_s2_ops {
 	void (*vm_stage2_unmap_range)(struct kvm_s2_mmu *mmu,
 				      phys_addr_t start, u64 size,
 				      bool may_block);
+	int (*vm_mem_abort)(const struct kvm_s2_fault_desc *s2fd);
 };
 
 struct kvm_s2_mmu {
arch/arm64/kvm/mmu.c +22 -14
index 3d8b6e5b811ed..607c908c15c42 100644
@@ -1742,7 +1742,7 @@ struct kvm_s2_fault_vma_info {
 	bool		map_non_cacheable;
 };
 
-static int pkvm_mem_abort(const struct kvm_s2_fault_desc *s2fd)
+static int protected_vm_mem_abort(const struct kvm_s2_fault_desc *s2fd)
 {
 	unsigned int flags = FOLL_HWPOISON | FOLL_LONGTERM | FOLL_WRITE;
 	struct kvm_vcpu *vcpu = s2fd->vcpu;
@@ -2180,6 +2180,22 @@ static int user_mem_abort(const struct kvm_s2_fault_desc *s2fd)
 	return kvm_s2_fault_map(s2fd, &s2vi, prot, memcache);
 }
 
+static int kvm_vm_mem_abort(const struct kvm_s2_fault_desc *s2fd)
+{
+	int ret;
+	struct kvm_vcpu *vcpu = s2fd->vcpu;
+
+	VM_WARN_ON_ONCE(kvm_vcpu_trap_is_permission_fault(vcpu) &&
+			!kvm_is_write_fault(vcpu) &&
+			!kvm_vcpu_trap_is_exec_fault(vcpu));
+
+	if (kvm_slot_has_gmem(s2fd->memslot))
+		ret = gmem_abort(s2fd);
+	else
+		ret = user_mem_abort(s2fd);
+	return ret;
+}
+
 /* Resolve the access fault by making the page young again. */
 static void handle_access_fault(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa)
 {
@@ -2287,6 +2303,7 @@ int kvm_handle_guest_sea(struct kvm_vcpu *vcpu)
 int kvm_handle_guest_abort(struct kvm_vcpu *vcpu)
 {
 	struct kvm_s2_trans nested_trans, *nested = NULL;
+	struct kvm *kvm = vcpu->kvm;
 	unsigned long esr;
 	phys_addr_t fault_ipa; /* The address we faulted on */
 	phys_addr_t ipa; /* Always the IPA in the L1 guest phys space */
@@ -2448,19 +2465,7 @@ int kvm_handle_guest_abort(struct kvm_vcpu *vcpu)
 		.hva		= hva,
 	};
 
-	if (kvm_vm_is_protected(vcpu->kvm)) {
-		ret = pkvm_mem_abort(&s2fd);
-	} else {
-		VM_WARN_ON_ONCE(kvm_vcpu_trap_is_permission_fault(vcpu) &&
-				!write_fault &&
-				!kvm_vcpu_trap_is_exec_fault(vcpu));
-
-		if (kvm_slot_has_gmem(memslot))
-			ret = gmem_abort(&s2fd);
-		else
-			ret = user_mem_abort(&s2fd);
-	}
-
+	ret = kvm->arch.vm_s2_ops->vm_mem_abort(&s2fd);
 	if (ret == 0)
 		ret = 1;
 out:
@@ -2855,6 +2860,7 @@ static const struct kvm_vm_s2_ops protected_pkvm_vm_s2_ops = {
 	 * .vm_test_age_gfn
 	 * .vm_stage2_unmap_range
 	 */
+	.vm_mem_abort			= protected_vm_mem_abort,
 };
 
 static const struct kvm_vm_s2_ops pkvm_vm_s2_ops = {
@@ -2863,6 +2869,7 @@ static const struct kvm_vm_s2_ops pkvm_vm_s2_ops = {
 	.vm_age_gfn			= kvm_vm_age_gfn,
 	.vm_test_age_gfn		= kvm_vm_test_age_gfn,
 	.vm_stage2_unmap_range		= kvm_vm_stage2_unmap_range,
+	.vm_mem_abort			= kvm_vm_mem_abort,
 };
 
 static const struct kvm_vm_s2_ops kvm_default_vm_s2_ops = {
@@ -2871,6 +2878,7 @@ static const struct kvm_vm_s2_ops kvm_default_vm_s2_ops = {
 	.vm_age_gfn			= kvm_vm_age_gfn,
 	.vm_test_age_gfn		= kvm_vm_test_age_gfn,
 	.vm_stage2_unmap_range		= kvm_vm_stage2_unmap_range,
+	.vm_mem_abort			= kvm_vm_mem_abort,
 };
 
 #define KVM_VM_S2_OPS(flavor, ops)		\
Signature · 1 line
2.43.0
author reviewer
#21 lore raw

PATCH v18 11/23 KVM: arm64: Use kvm_vm_is_unprotected() for !kvm_vm_is_protected()

In reply to Suzuki K Poulose

Now that we have a flavor to check, use the helper to explicitly check the type for an unprotected PKVM guest. We don't convert all of the !kvm_vm_is_protected(), but only the ones that target unprotected PKVM guests.

---
 arch/arm64/include/asm/kvm_pkvm.h | 2 +-
 arch/arm64/kvm/arm.c              | 2 +-
 arch/arm64/kvm/handle_exit.c      | 2 +-
 3 files changed, 3 insertions(+), 3 deletions(-)
arch/arm64/include/asm/kvm_pkvm.h +1 -1
index a1f3e05e75dc1..e4ea80711bec6 100644
@@ -49,7 +49,7 @@ static inline bool kvm_pkvm_ext_allowed(struct kvm *kvm, long ext)
 	case KVM_CAP_ARM_SUPPORTED_BLOCK_SIZES:
 		return false;
 	default:
-		return !kvm || !kvm_vm_is_protected(kvm);
+		return !kvm || kvm_vm_is_unprotected_pkvm(kvm);
 	}
 }
 
arch/arm64/kvm/arm.c +1 -1
index 36bd1c6c04899..42d9385f5e329 100644
@@ -1082,7 +1082,7 @@ int kvm_arch_vcpu_run_pid_change(struct kvm_vcpu *vcpu)
 
 	if (is_protected_kvm_enabled()) {
 		/* Start with the vcpu in a dirty state */
-		if (!kvm_vm_is_protected(vcpu->kvm))
+		if (kvm_vm_is_unprotected_pkvm(vcpu->kvm))
 			vcpu_set_flag(vcpu, PKVM_HOST_STATE_DIRTY);
 		ret = pkvm_create_hyp_vm(kvm);
 		if (ret)
arch/arm64/kvm/handle_exit.c +1 -1
index db37678dcb05c..384c5d258c7f8 100644
@@ -490,7 +490,7 @@ static void handle_exit_pkvm_state(struct kvm_vcpu *vcpu, int exception_index)
 {
 	int exception_code = ARM_EXCEPTION_CODE(exception_index);
 
-	if (!is_protected_kvm_enabled() || kvm_vm_is_protected(vcpu->kvm))
+	if (!kvm_vm_is_unprotected_pkvm(vcpu->kvm))
 		return;
 
 	/*
Signature · 1 line
2.43.0
#22 lore raw

Re: [PATCH v18 11/23] KVM: arm64: Use kvm_vm_is_unprotected() for !kvm_vm_is_protected()

Hi Suzuki,

On Tue, 15 Sep 2026 17:01:29 +0100, Suzuki K Poulose <[email protected]> wrote:
[...]

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

[...]

arch/arm64/kvm/handle_exit.c static void handle_exit_pkvm_state(struct kvm_vcpu *vcpu, int exception_index) · line 494

Quoted 7 lines
arch/arm64/kvm/handle_exit.c +1 -1
@@ -490,7 +490,7 @@ static void handle_exit_pkvm_state(struct kvm_vcpu *vcpu, int exception_index)
 {
         int exception_code = ARM_EXCEPTION_CODE(exception_index);
 
-        if (!is_protected_kvm_enabled() || kvm_vm_is_protected(vcpu->kvm))
+        if (!kvm_vm_is_unprotected_pkvm(vcpu->kvm))
                 return;

Could the two pKVM-only helpers keep the static key in front? With is_protected_kvm_enabled() && inside kvm_vm_is_unprotected_pkvm() and kvm_vm_is_protected_pkvm(), as kvm_vm_is_protected() had it before patch 5, the boot-patched branch skips the load again for every user. This runs on every exit: the old test is a cpucap, so on a host without pKVM the function returns straight away, while the new one loads vcpu->kvm->arch.vm_flavor on every host. The wide kvm_vm_is_protected() can't have it, since Realms run without pKVM.

Cheers,
/fuad

author reviewer
#23 lore raw

Re: [PATCH v18 11/23] KVM: arm64: Use kvm_vm_is_unprotected() for !kvm_vm_is_protected()

Quoted 11 lines · Fuad Tabba

Hi Suzuki,

On Tue, 15 Sep 2026 17:01:29 +0100, Suzuki K Poulose <[email protected]> wrote:
[...]

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

[...]

Quoted 4 lines
arch/arm64/kvm/handle_exit.c +0 -0
@@ -490,7 +490,7 @@ static void handle_exit_pkvm_state(struct kvm_vcpu *vcpu, int exception_index)
  {
          int exception_code = ARM_EXCEPTION_CODE(exception_index);
 
Quoted 12 lines

arch/arm64/kvm/handle_exit.c static void handle_exit_pkvm_state(struct kvm_vcpu *vcpu, int exception_index) · line 494

Quoted 3 lines
arch/arm64/kvm/handle_exit.c +1 -1
-        if (!is_protected_kvm_enabled() || kvm_vm_is_protected(vcpu->kvm))
+        if (!kvm_vm_is_unprotected_pkvm(vcpu->kvm))
                  return;

Could the two pKVM-only helpers keep the static key in front? With is_protected_kvm_enabled() && inside kvm_vm_is_unprotected_pkvm() and kvm_vm_is_protected_pkvm(), as kvm_vm_is_protected() had it before patch 5, the boot-patched branch skips the load again for every user. This runs on every exit: the old test is a cpucap, so on a host without pKVM the function returns straight away, while the new one loads vcpu->kvm->arch.vm_flavor on every host. The wide kvm_vm_is_protected() can't have it, since Realms run without pKVM.

Ack

Suzuki

Quoted 3 lines

Cheers,
/fuad

author reviewer
#24 lore raw

PATCH v18 12/23 KVM: arm64: Widen the scope of "protected" VMs

In reply to Suzuki K Poulose

On arm64 we have "protected" VMs that run on PKVM as a confidential compute guest. Given we already have the construct of "kvm_vm_is_protected" in the core KVM code, use that for all confidential compute guests including Realms that we are about to add.

Use the VM flavor to detect the "protected" VMs by introducing a marker. Add explicit helper to detect if a given VM is a "protected" VM under PKVM. Change the existing users that precisely want to check the VM type. These include :
- kvm_arch_prepare_memory_region - For preventing memslot changes after pVM creation.

All the others are retained as a wider check for confidential guest VMs. These are:
- kvm_vm_ioctl_set_counter_offset - For disallowing timer offset configuration
- io_mem_abort for dabt handling without valid syndrome information

Both of which are true for Realms too.

Realms support is restricted to VHE host and thus "kvm_vm_is_protected()" checks in the pkvm hyp specific code doesn't need to change, as the only protected guests it deals with is "protected PKVM" guests.

---
 arch/arm64/include/asm/kvm_host.h | 4 +++-
 arch/arm64/kvm/mmu.c              | 2 +-
 2 files changed, 4 insertions(+), 2 deletions(-)
arch/arm64/include/asm/kvm_host.h +3 -1
index 7ce46d853c47e..1bb43c57fb0f0 100644
@@ -329,6 +329,7 @@ enum kvm_arm_vm_flavor {
 	VM_NVHE,
 	VM_VHE,
 	VM_PKVM,		/* Normal guests on pKVM */
+	MARKER(__VM_PROTECTED),
 	VM_PROTECTED_PKVM,	/* Protected VM */
 	VM_FLAVOR_MAX,
 };
@@ -1535,7 +1536,8 @@ struct kvm *kvm_arch_alloc_vm(void);
 
 #define __KVM_HAVE_ARCH_FLUSH_REMOTE_TLBS_RANGE
 
-#define kvm_vm_is_protected(kvm)	((kvm)->arch.vm_flavor == VM_PROTECTED_PKVM)
+#define kvm_vm_is_protected(kvm)	((kvm)->arch.vm_flavor >= __VM_PROTECTED)
+#define kvm_vm_is_protected_pkvm(kvm)	((kvm)->arch.vm_flavor == VM_PROTECTED_PKVM)
 #define kvm_vm_is_unprotected_pkvm(kvm)	((kvm)->arch.vm_flavor == VM_PKVM)
 
 #define kvm_vm_hyp_is_pkvm(kvm)		(is_protected_kvm_enabled())
arch/arm64/kvm/mmu.c +1 -1
index 607c908c15c42..38f3bf772278f 100644
@@ -2678,7 +2678,7 @@ int kvm_arch_prepare_memory_region(struct kvm *kvm,
 	hva_t hva, reg_end;
 	int ret = 0;
 
-	if (kvm_vm_is_protected(kvm)) {
+	if (kvm_vm_is_protected_pkvm(kvm)) {
 		/* Cannot modify memslots once a pVM has run. */
 		if (pkvm_hyp_vm_is_created(kvm) &&
 		    (change == KVM_MR_DELETE || change == KVM_MR_MOVE)) {
Signature · 1 line
2.43.0
maintainer
#25 lore raw

Re: [PATCH v18 12/23] KVM: arm64: Widen the scope of "protected" VMs

Quoted 43 lines · Suzuki K Poulose

On arm64 we have "protected" VMs that run on PKVM as a confidential compute guest. Given we already have the construct of "kvm_vm_is_protected" in the core KVM code, use that for all confidential compute guests including Realms that we are about to add.

Use the VM flavor to detect the "protected" VMs by introducing a marker. Add explicit helper to detect if a given VM is a "protected" VM under PKVM. Change the existing users that precisely want to check the VM type. These include :
- kvm_arch_prepare_memory_region - For preventing memslot changes after pVM creation.

All the others are retained as a wider check for confidential guest VMs. These are:
- kvm_vm_ioctl_set_counter_offset - For disallowing timer offset configuration
- io_mem_abort for dabt handling without valid syndrome information

Both of which are true for Realms too.

Realms support is restricted to VHE host and thus "kvm_vm_is_protected()" checks in the pkvm hyp specific code doesn't need to change, as the only protected guests it deals with is "protected PKVM" guests.

---
 arch/arm64/include/asm/kvm_host.h | 4 +++-
 arch/arm64/kvm/mmu.c              | 2 +-
 2 files changed, 4 insertions(+), 2 deletions(-)
arch/arm64/include/asm/kvm_host.h +1 -0
index 7ce46d853c47e..1bb43c57fb0f0 100644
@@ -329,6 +329,7 @@ enum kvm_arm_vm_flavor {
 	VM_NVHE,
 	VM_VHE,
 	VM_PKVM,		/* Normal guests on pKVM */
+	MARKER(__VM_PROTECTED),
 	VM_PROTECTED_PKVM,	/* Protected VM */
 	VM_FLAVOR_MAX,
 };

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

Quoted 8 lines
arch/arm64/include/asm/kvm_host.h +2 -1
@@ -1535,7 +1536,8 @@ struct kvm *kvm_arch_alloc_vm(void);
 
 #define __KVM_HAVE_ARCH_FLUSH_REMOTE_TLBS_RANGE
 
-#define kvm_vm_is_protected(kvm)	((kvm)->arch.vm_flavor == VM_PROTECTED_PKVM)
+#define kvm_vm_is_protected(kvm)	((kvm)->arch.vm_flavor >= __VM_PROTECTED)
+#define kvm_vm_is_protected_pkvm(kvm)	((kvm)->arch.vm_flavor == VM_PROTECTED_PKVM)
 #define kvm_vm_is_unprotected_pkvm(kvm)	((kvm)->arch.vm_flavor == VM_PKVM)
 

This really should be added from where you introduce the enumeration, as my original patch did. Otherwise, this is pure churn for no benefit.

Thanks,

	M.
Signature · 1 line
Without deviation from the norm, progress is not possible.
author reviewer
#26 lore raw

Re: [PATCH v18 12/23] KVM: arm64: Widen the scope of "protected" VMs

Quoted 48 lines · Marc Zyngier
Quoted 46 lines · Suzuki K Poulose

On arm64 we have "protected" VMs that run on PKVM as a confidential compute guest. Given we already have the construct of "kvm_vm_is_protected" in the core KVM code, use that for all confidential compute guests including Realms that we are about to add.

Use the VM flavor to detect the "protected" VMs by introducing a marker. Add explicit helper to detect if a given VM is a "protected" VM under PKVM. Change the existing users that precisely want to check the VM type. These include :
- kvm_arch_prepare_memory_region - For preventing memslot changes after pVM creation.

All the others are retained as a wider check for confidential guest VMs. These are:
- kvm_vm_ioctl_set_counter_offset - For disallowing timer offset configuration
- io_mem_abort for dabt handling without valid syndrome information

Both of which are true for Realms too.

Realms support is restricted to VHE host and thus "kvm_vm_is_protected()" checks in the pkvm hyp specific code doesn't need to change, as the only protected guests it deals with is "protected PKVM" guests.

---
  arch/arm64/include/asm/kvm_host.h | 4 +++-
  arch/arm64/kvm/mmu.c              | 2 +-
  2 files changed, 4 insertions(+), 2 deletions(-)
arch/arm64/include/asm/kvm_host.h +1 -0
index 7ce46d853c47e..1bb43c57fb0f0 100644
@@ -329,6 +329,7 @@ enum kvm_arm_vm_flavor {
  	VM_NVHE,
  	VM_VHE,
  	VM_PKVM,		/* Normal guests on pKVM */
+	MARKER(__VM_PROTECTED),
  	VM_PROTECTED_PKVM,	/* Protected VM */
  	VM_FLAVOR_MAX,
  };
@@ -1535,7 +1536,8 @@ struct kvm *kvm_arch_alloc_vm(void);
 
  #define __KVM_HAVE_ARCH_FLUSH_REMOTE_TLBS_RANGE
 
Quoted 8 lines

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

Quoted 5 lines
arch/arm64/include/asm/kvm_host.h +2 -1
-#define kvm_vm_is_protected(kvm)	((kvm)->arch.vm_flavor == VM_PROTECTED_PKVM)
+#define kvm_vm_is_protected(kvm)	((kvm)->arch.vm_flavor >= __VM_PROTECTED)
+#define kvm_vm_is_protected_pkvm(kvm)	((kvm)->arch.vm_flavor == VM_PROTECTED_PKVM)
  #define kvm_vm_is_unprotected_pkvm(kvm)	((kvm)->arch.vm_flavor == VM_PKVM)
 

This really should be added from where you introduce the enumeration, as my original patch did. Otherwise, this is pure churn for no benefit.

Ack, the idea was to introduce this, after converting the existing code with the new wrappers. I will fold that change here.

Cheers
Suzuki

Quoted 5 lines

Thanks,

	M.
#27 lore raw

Re: [PATCH v18 12/23] KVM: arm64: Widen the scope of "protected" VMs

In reply to Suzuki K Poulose

Hi Suzuki,

On Tue, 15 Sep 2026 17:01:30 +0100, Suzuki K Poulose <[email protected]> wrote:
[...]

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

[...]

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

Quoted 4 lines
arch/arm64/include/asm/kvm_host.h +2 -1
-#define kvm_vm_is_protected(kvm)        ((kvm)->arch.vm_flavor == VM_PROTECTED_PKVM)
+#define kvm_vm_is_protected(kvm)        ((kvm)->arch.vm_flavor >= __VM_PROTECTED)
+#define kvm_vm_is_protected_pkvm(kvm)        ((kvm)->arch.vm_flavor == VM_PROTECTED_PKVM)
 #define kvm_vm_is_unprotected_pkvm(kvm)        ((kvm)->arch.vm_flavor == VM_PKVM)

Agree with Marc that this belongs in patch 5. When it moves, could you add vcpu_is_protected_pkvm() next to vcpu_is_protected()? The pKVM host-side series [1] has vcpu_is_protected() checks that are pKVM mechanism (the vCPU feature allowlist, CPU_ON against the pKVM power-off state, the ONE_REG rejection after the first run), and with the wider predicate those have to carry a pKVM-specific name.

Cheers,
/fuad

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

author reviewer
#28 lore raw

Re: [PATCH v18 12/23] KVM: arm64: Widen the scope of "protected" VMs

Quoted 7 lines · Fuad Tabba

Hi Suzuki,

On Tue, 15 Sep 2026 17:01:30 +0100, Suzuki K Poulose <[email protected]> wrote:
[...]

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

[...]

Quoted 11 lines

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

Quoted 4 lines
arch/arm64/include/asm/kvm_host.h +2 -1
-#define kvm_vm_is_protected(kvm)        ((kvm)->arch.vm_flavor == VM_PROTECTED_PKVM)
+#define kvm_vm_is_protected(kvm)        ((kvm)->arch.vm_flavor >= __VM_PROTECTED)
+#define kvm_vm_is_protected_pkvm(kvm)        ((kvm)->arch.vm_flavor == VM_PROTECTED_PKVM)
  #define kvm_vm_is_unprotected_pkvm(kvm)        ((kvm)->arch.vm_flavor == VM_PKVM)

Agree with Marc that this belongs in patch 5. When it moves, could you add vcpu_is_protected_pkvm() next to vcpu_is_protected()? The pKVM host-side series [1] has vcpu_is_protected() checks that are pKVM mechanism (the vCPU feature allowlist, CPU_ON against the pKVM power-off state, the ONE_REG rejection after the first run), and with the wider predicate those have to carry a pKVM-specific name.

Ack.

Suzuki

Quoted 5 lines

Cheers,
/fuad

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

reviewer
#29 lore raw

Re: [PATCH v18 12/23] KVM: arm64: Widen the scope of "protected" VMs

In reply to Suzuki K Poulose
Quoted 17 lines · Suzuki K Poulose

On arm64 we have "protected" VMs that run on PKVM as a confidential compute guest. Given we already have the construct of "kvm_vm_is_protected" in the core KVM code, use that for all confidential compute guests including Realms that we are about to add.

Use the VM flavor to detect the "protected" VMs by introducing a marker. Add explicit helper to detect if a given VM is a "protected" VM under PKVM. Change the existing users that precisely want to check the VM type. These include :
- kvm_arch_prepare_memory_region - For preventing memslot changes after pVM creation.

All the others are retained as a wider check for confidential guest VMs. These are:
- kvm_vm_ioctl_set_counter_offset - For disallowing timer offset configuration
- io_mem_abort for dabt handling without valid syndrome information

Quoted 7 lines

Both of which are true for Realms too.

Realms support is restricted to VHE host and thus "kvm_vm_is_protected()" checks in the pkvm hyp specific code doesn't need to change, as the only protected guests it deals with is "protected PKVM" guests.

nit: need to s/PKVM/pKVM/ on the commit message too!

Thanks,
Joey

author reviewer
#30 lore raw

PATCH v18 13/23 KVM: arm64: Add a helper for VMs running on hyp that don't trust the host

In reply to Suzuki K Poulose

PKVM and RMM, both do not trust the host. Add a helper to detect the VMs that have "distrusting" hyp. Use this for blocking ioremap of vgic-v2 into stage2.

---
 arch/arm64/include/asm/kvm_host.h | 3 +++
 arch/arm64/kvm/mmu.c              | 2 +-
 2 files changed, 4 insertions(+), 1 deletion(-)
arch/arm64/include/asm/kvm_host.h +3 -0
index 1bb43c57fb0f0..09910b2c61bfb 100644
@@ -328,6 +328,8 @@ enum fgt_group_id {
 enum kvm_arm_vm_flavor {
 	VM_NVHE,
 	VM_VHE,
+	/* VMs running on a hyp that doesn't trust */
+	MARKER(__VM_DISTRUSTING_HYP),
 	VM_PKVM,		/* Normal guests on pKVM */
 	MARKER(__VM_PROTECTED),
 	VM_PROTECTED_PKVM,	/* Protected VM */
@@ -1540,6 +1542,7 @@ struct kvm *kvm_arch_alloc_vm(void);
 #define kvm_vm_is_protected_pkvm(kvm)	((kvm)->arch.vm_flavor == VM_PROTECTED_PKVM)
 #define kvm_vm_is_unprotected_pkvm(kvm)	((kvm)->arch.vm_flavor == VM_PKVM)
 
+#define kvm_vm_hyp_is_distrusting(kvm)	((kvm)->arch.vm_flavor > __VM_DISTRUSTING_HYP)
 #define kvm_vm_hyp_is_pkvm(kvm)		(is_protected_kvm_enabled())
 #define vcpu_is_protected(vcpu)		kvm_vm_is_protected((vcpu)->kvm)
 
arch/arm64/kvm/mmu.c +1 -1
index 38f3bf772278f..559a8286807cd 100644
@@ -1251,7 +1251,7 @@ int kvm_phys_addr_ioremap(struct kvm *kvm, phys_addr_t guest_ipa,
 				     KVM_PGTABLE_PROT_R |
 				     (writable ? KVM_PGTABLE_PROT_W : 0);
 
-	if (is_protected_kvm_enabled())
+	if (kvm_vm_hyp_is_distrusting(kvm))
 		return -EPERM;
 
 	size += offset_in_page(guest_ipa);
Signature · 1 line
2.43.0
#31 lore raw

Re: [PATCH v18 13/23] KVM: arm64: Add a helper for VMs running on hyp that don't trust the host

Hi Suzuki,

On Tue, 15 Sep 2026 17:01:31 +0100, Suzuki K Poulose <[email protected]> wrote:
[...]

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

[...]

Quoted 6 lines
 enum kvm_arm_vm_flavor {
         VM_NVHE,
         VM_VHE,
arch/arm64/include/asm/kvm_host.h +2 -0
+        /* VMs running on a hyp that doesn't trust */
+        MARKER(__VM_DISTRUSTING_HYP),
         VM_PKVM,                /* Normal guests on pKVM */

[...]

Quoted 1 line

+#define kvm_vm_hyp_is_distrusting(kvm) ((kvm)->arch.vm_flavor > __VM_DISTRUSTING_HYP)

I think this should be >=, as in kvm_vm_is_protected(): MARKER() gives the marker the same value as the enumerator after it, so > leaves out VM_PKVM. As posted, kvm_phys_addr_ioremap() no longer returns -EPERM for non-protected VMs under pKVM (EL2 still rejects the mapping, so it's only the errno that changes).

Cheers,
/fuad

author reviewer
#32 lore raw

PATCH v18 14/23 KVM: arm64: CCA: Add a new mode for supporting Realm guests

In reply to Suzuki K Poulose

Add an explicit mode to support Arm CCA guests.

---
 Documentation/admin-guide/kernel-parameters.txt | 3 +++
 arch/arm64/include/asm/kvm_host.h               | 1 +
 arch/arm64/kvm/arm.c                            | 5 +++++
 3 files changed, 9 insertions(+)
Documentation/admin-guide/kernel-parameters.txt +3 -0
index 68647ff4bdd24..1afe3df3b923e 100644
@@ -3256,6 +3256,9 @@ Kernel parameters
 			nested: VHE-based mode with support for nested
 				virtualization. Requires at least ARMv8.4
 				hardware (with FEAT_NV2).
+			rmm: Support for running confidential guests in Realm
+			     world using RMM, as defined by Arm Confidential
+			     Compute Architecture (CCA)
 
 			Defaults to VHE/nVHE based on hardware support. Setting
 			mode to "protected" will disable kexec and hibernation
arch/arm64/include/asm/kvm_host.h +1 -0
index 09910b2c61bfb..3cedb71f49ab8 100644
@@ -69,6 +69,7 @@ enum kvm_mode {
 	KVM_MODE_DEFAULT,
 	KVM_MODE_PROTECTED,
 	KVM_MODE_NV,
+	KVM_MODE_RMM,
 	KVM_MODE_NONE,
 };
 #ifdef CONFIG_KVM
arch/arm64/kvm/arm.c +5 -0
index 42d9385f5e329..ae09d0d9812c5 100644
@@ -3283,6 +3283,11 @@ static int __init early_kvm_mode_cfg(char *arg)
 		return 0;
 	}
 
+	if (strcmp(arg, "rmm") == 0 && !WARN_ON(!is_kernel_in_hyp_mode())) {
+		kvm_mode = KVM_MODE_RMM;
+		return 0;
+	}
+
 	return -EINVAL;
 }
 early_param("kvm-arm.mode", early_kvm_mode_cfg);
Signature · 1 line
2.43.0
author reviewer
#33 lore raw

PATCH v18 15/23 KVM: arm64: CCA: Add VCPU load/put for Realms

In reply to Suzuki K Poulose

RMM controls the VCPU settings and most are hidden from the KVM, except for the VGIC and timer bits.

A later patch would add syncing the VCPU state into the Realm REC related SMC parameters.

---
 arch/arm64/kvm/arm.c | 18 ++++++++++++++++++
 1 file changed, 18 insertions(+)
arch/arm64/kvm/arm.c +18 -0
index ae09d0d9812c5..8defec75b8438 100644
@@ -810,6 +810,13 @@ static void pkvm_vcpu_load(struct kvm_vcpu *vcpu)
 			  &vcpu->arch.vgic_cpu.vgic_v3);
 }
 
+static void realm_vcpu_load(struct kvm_vcpu *vcpu)
+{
+	kvm_timer_vcpu_load(vcpu);
+	kvm_vgic_load(vcpu);
+	vcpu_set_wfx_traps(vcpu);
+}
+
 void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
 {
 	vcpu->cpu = cpu;
@@ -860,6 +867,12 @@ static void pkvm_vcpu_put(struct kvm_vcpu *vcpu)
 	kvm_vcpu_pmu_restore_host(vcpu);
 }
 
+static void realm_vcpu_put(struct kvm_vcpu *vcpu)
+{
+	kvm_timer_vcpu_put(vcpu);
+	kvm_vgic_put(vcpu);
+}
+
 void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
 {
 	vcpu->arch.vcpu_ops->vcpu_put(vcpu);
@@ -2217,6 +2230,11 @@ static const struct kvm_vcpu_ops pkvm_vcpu_ops = {
 	.vcpu_put = pkvm_vcpu_put,
 };
 
+static const struct kvm_vcpu_ops realm_vcpu_ops = {
+	.vcpu_load = realm_vcpu_load,
+	.vcpu_put = realm_vcpu_put,
+};
+
 #define KVM_VCPU_OPS(flavor, ops)	\
 	[(flavor)] = 	(ops)
 
Signature · 1 line
2.43.0
author reviewer
#34 lore raw

PATCH v18 16/23 KVM: arm64: CCA: Add bare minimal S2 operations for Realm

In reply to Suzuki K Poulose

Add bare minimal MMU operation hooks for Realms. The mem_abort handling is chosen as the default KVM variant. However this cannot be reached for Realms yet and we would need real RMI command support to make it fully functional.

RMM takes care of the TLB flushing as required, when the Stage2 is modified. So host doesn't need to do anything explicitly. RMM doesn't support access flags for the stage2, even for the shared IPA.

---
 arch/arm64/kvm/mmu.c | 24 ++++++++++++++++++++++++
 1 file changed, 24 insertions(+)
arch/arm64/kvm/mmu.c +24 -0
index 559a8286807cd..ac86b5a41c315 100644
@@ -180,6 +180,12 @@ static int kvm_vm_flush_remote_tlbs(struct kvm *kvm)
 	return 0;
 }
 
+static int realm_vm_flush_remote_tlbs(struct kvm *kvm)
+{
+	/* Nothing to do here, RMM takes care of this */
+	return 0;
+}
+
 /**
  * kvm_arch_flush_remote_tlbs() - flush all VM TLB entries for v7/8
  * @kvm:	pointer to kvm structure.
@@ -209,6 +215,13 @@ static int kvm_vm_flush_remote_tlbs_range(struct kvm *kvm,
 	return 0;
 }
 
+static int realm_vm_flush_remote_tlbs_range(struct kvm *kvm,
+					   gfn_t gfn, u64 nr_pages)
+{
+	/* Nothing to do here, RMM takes care of this */
+	return 0;
+}
+
 int kvm_arch_flush_remote_tlbs_range(struct kvm *kvm,
 				     gfn_t gfn, u64 nr_pages)
 {
@@ -2881,6 +2894,17 @@ static const struct kvm_vm_s2_ops kvm_default_vm_s2_ops = {
 	.vm_mem_abort			= kvm_vm_mem_abort,
 };
 
+static const struct kvm_vm_s2_ops realm_vm_s2_ops = {
+	.vm_flush_remote_tlbs		= realm_vm_flush_remote_tlbs,
+	.vm_flush_remote_tlbs_range	= realm_vm_flush_remote_tlbs_range,
+	.vm_mem_abort			= kvm_vm_mem_abort,
+	/*
+	 * Not supported for Realms
+	 *	.vm_age_gfn			= realm_vm_age_gfn,
+	 *	.vm_test_age_gfn		= realm_vm_test_age_gfn,
+	 */
+};
+
 #define KVM_VM_S2_OPS(flavor, ops)		\
 		[flavor] = ops
 static const struct kvm_vm_s2_ops *arm64_vm_s2_ops[] = {
Signature · 1 line
2.43.0
author reviewer
#35 lore raw

PATCH v18 17/23 KVM: arm64: CCA: Introduce Realms

In reply to Suzuki K Poulose

Add foundational work for supporting Realms.
- Add a new VM flavor.
- At KVM init, check if the KVM can support Realms (though not functional yet) and will be advertised by static key kvm_rmi_is_available. This will be turned on in a later patches, once we have all the bits and pieces ready. For now check if we are blessed with KVM_MODE_RMM.
- Add realm specific tracking in kvm_arch. Since Realm and protected pKVM states are mutually exclusive, move them into a union.

Please note that we cannot create Realm VMs yet. This requires further changes to the UABI and core RMI driver support, which will come later.

---
Changes since v16:
* Share mutually exclusive pKVM and Realm per-VM storage in a union.
* Move to the new VM flavor infrastructure, split bits out. Trimmed down
* Move in Realm state and basic boiler plates
Changes since v13:
* Most of the init has been moved out of the 'kvm' directory so this is much more basic now.
Changes since v12:
* Drop check for 4k page size.
Changes since v11:
* Reword slightly the comments on the realm states.
Changes since v10:
* kvm_is_realm() no longer has a NULL check.
* Rename from "rme" to "rmi" when referring to the RMM interface.
* Check for RME (hardware) support before probing for RMI support.
Changes since v8:
* No need to guard kvm_init_rme() behind 'in_hyp_mode'.
Changes since v6:
* Improved message for an unsupported RMI ABI version.
Changes since v5:
* Reword "unsupported" message from "host supports" to "we want" to clarify that 'we' are the 'host'.
Changes since v2:
* Drop return value from kvm_init_rme(), it was always 0.
* Rely on the RMM return value to identify whether the RSI ABI is compatible.

---
 arch/arm64/include/asm/kvm_emulate.h | 16 ++++++++
 arch/arm64/include/asm/kvm_host.h    | 18 +++++---
 arch/arm64/include/asm/kvm_rmi.h     | 61 ++++++++++++++++++++++++++++
 arch/arm64/include/asm/virt.h        |  1 +
 arch/arm64/kvm/Makefile              |  2 +-
 arch/arm64/kvm/arm.c                 |  6 +++
 arch/arm64/kvm/mmu.c                 |  1 +
 arch/arm64/kvm/rmi.c                 | 18 ++++++++
 8 files changed, 117 insertions(+), 6 deletions(-)
 create mode 100644 arch/arm64/include/asm/kvm_rmi.h
 create mode 100644 arch/arm64/kvm/rmi.c
arch/arm64/include/asm/kvm_emulate.h +16 -0
index a3c1928bdf743..d360a8b05b8bf 100644
@@ -793,4 +793,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 +13 -5
index 3cedb71f49ab8..90bfa0fa4f3cf 100644
@@ -27,6 +27,7 @@
 #include <asm/fpsimd.h>
 #include <asm/kvm.h>
 #include <asm/kvm_asm.h>
+#include <asm/kvm_rmi.h>
 #include <asm/vncr_mapping.h>
 
 #define __KVM_HAVE_ARCH_INTC_INITIALIZED
@@ -334,6 +335,7 @@ enum kvm_arm_vm_flavor {
 	VM_PKVM,		/* Normal guests on pKVM */
 	MARKER(__VM_PROTECTED),
 	VM_PROTECTED_PKVM,	/* Protected VM */
+	VM_REALM,		/* CCA */
 	VM_FLAVOR_MAX,
 };
 
@@ -451,11 +453,14 @@ struct kvm_arch {
 	/* Count the number of VNCR_EL2 TLBs */
 	atomic_t vncr_tlb_count;
 
-	/*
-	 * For an untrusted host VM, 'pkvm.handle' is used to lookup
-	 * the associated pKVM instance in the hypervisor.
-	 */
-	struct kvm_protected_vm pkvm;
+	union {
+		/*
+		 * For an untrusted host VM, 'pkvm.handle' is used to lookup
+		 * the associated pKVM instance in the hypervisor.
+		 */
+		struct kvm_protected_vm pkvm;
+		struct realm realm;
+	};
 
 #ifdef CONFIG_PTDUMP_STAGE2_DEBUGFS
 	/* Nested virtualization info */
@@ -1545,7 +1550,10 @@ struct kvm *kvm_arch_alloc_vm(void);
 
 #define kvm_vm_hyp_is_distrusting(kvm)	((kvm)->arch.vm_flavor > __VM_DISTRUSTING_HYP)
 #define kvm_vm_hyp_is_pkvm(kvm)		(is_protected_kvm_enabled())
+#define kvm_vm_is_realm(kvm)		((kvm)->arch.vm_flavor == VM_REALM)
+
 #define vcpu_is_protected(vcpu)		kvm_vm_is_protected((vcpu)->kvm)
+#define vcpu_is_rec(vcpu)		kvm_vm_is_realm((vcpu)->kvm)
 
 int kvm_arm_vcpu_finalize(struct kvm_vcpu *vcpu, int feature);
 bool kvm_arm_vcpu_is_finalized(struct kvm_vcpu *vcpu);
arch/arm64/include/asm/kvm_rmi.h new file +61 -0

61 lines not on this page · raw message

arch/arm64/include/asm/virt.h +1 -0
index b546703c3ab9a..92cec42952f42 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/Makefile +1 -1
index 59612d2f277c1..ed3cf30eb06e7 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 +6 -0
index 8defec75b8438..8ed7dd1ce446c 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);
@@ -2243,6 +2246,7 @@ static const struct kvm_vcpu_ops *arm64_vcpu_ops[] = {
 	KVM_VCPU_OPS(VM_NVHE, &nvhe_vcpu_ops),
 	KVM_VCPU_OPS(VM_PKVM, &pkvm_vcpu_ops),
 	KVM_VCPU_OPS(VM_PROTECTED_PKVM, &pkvm_vcpu_ops),
+	KVM_VCPU_OPS(VM_REALM, &realm_vcpu_ops),
 };
 
 static void kvm_init_vcpu_ops(struct kvm_vcpu *vcpu)
@@ -3196,6 +3200,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/mmu.c +1 -0
index ac86b5a41c315..cc264726a6df8 100644
@@ -2912,6 +2912,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),
+	KVM_VM_S2_OPS(VM_REALM, &realm_vm_s2_ops),
 };
 
 static int kvm_vm_init_vm_s2_ops(struct kvm *kvm)
arch/arm64/kvm/rmi.c new file +18 -0
new file mode 100644
index 0000000000000..5ecc8b3498698
@@ -0,0 +1,18 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Copyright (C) 2023-2026 ARM Ltd.
+ */
+
+#include <linux/kvm_host.h>
+
+#include <asm/virt.h>
+
+void kvm_init_rmi(void)
+{
+	if (kvm_get_mode() != KVM_MODE_RMM)
+		return;
+
+	/* TODO: Check if the RMI is available */
+
+	/* Future patch will enable static branch kvm_rmi_is_available */
+}
Signature · 1 line
2.43.0
author reviewer
#36 lore raw

PATCH v18 18/23 KVM: arm64: CCA: Mandate VGIC_V3 for Realms

In reply to Suzuki K Poulose

RMM mandates that we use a VGIC v3 for a Realm and nothing else is supported. Mandate this in KVM.

---
 arch/arm64/kvm/vgic/vgic-init.c | 3 +++
 1 file changed, 3 insertions(+)
arch/arm64/kvm/vgic/vgic-init.c +3 -0
index 4012df6002ea6..452c085073f3e 100644
@@ -84,6 +84,9 @@ int kvm_vgic_create(struct kvm *kvm, u32 type)
 		!kvm_vgic_global_state.can_emulate_gicv2)
 		return -ENODEV;
 
+	/* Realms only support VGIC_V3 */
+	if (kvm_vm_is_realm(kvm) && type != KVM_DEV_TYPE_ARM_VGIC_V3)
+		return -ENODEV;
 	/*
 	 * Ensure mutual exclusion with vCPU creation and any vCPU ioctls by:
 	 *
Signature · 1 line
2.43.0
#37 lore raw

Re: [PATCH v18 18/23] KVM: arm64: CCA: Mandate VGIC_V3 for Realms

Hi Suzuki,

On Tue, 15 Sep 2026 17:01:36 +0100, Suzuki K Poulose <[email protected]> wrote:
[...]

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

[...]

arch/arm64/kvm/vgic/vgic-init.c int kvm_vgic_create(struct kvm *kvm, u32 type) · line 89

Quoted 7 lines
arch/arm64/kvm/vgic/vgic-init.c +3 -0
@@ -84,6 +84,9 @@ int kvm_vgic_create(struct kvm *kvm, u32 type)
                 !kvm_vgic_global_state.can_emulate_gicv2)
                 return -ENODEV;
 
+        /* Realms only support VGIC_V3 */
+        if (kvm_vm_is_realm(kvm) && type != KVM_DEV_TYPE_ARM_VGIC_V3)
+                return -ENODEV;

Could this be kvm_vm_hyp_is_distrusting(kvm) (with patch 13's compare as >=) rather than a Realm-only check? A GICv2 isn't possible under pKVM either, but there the v2 mapping is only rejected later, in kvm_vgic_map_resources() at the first run; this would give both the same -ENODEV at KVM_CREATE_DEVICE.

Cheers,
/fuad

author reviewer
#38 lore raw

PATCH v18 19/23 KVM: arm64: CCA: Support timers in realm RECs

In reply to Suzuki K Poulose

From: Steven Price <[email protected]>

The RMM keeps track of the timer while the realm REC is running, but on exit to the normal world KVM is responsible for handling the timers.

A later patch adds the support for propagating the timer values from the exit data structure and makeing sure the values are in sync for KVM.

Also, RMM doesn't support injecting virtual interrupts backed by Physical interrupts. So, use the existing software resampling mechanims for Realm timer interrupts.

---
 arch/arm64/kvm/arch_timer.c | 22 ++++++++++++++++++++--
 1 file changed, 20 insertions(+), 2 deletions(-)
arch/arm64/kvm/arch_timer.c +20 -2
index dda020da4c9c7..ef5690956ee64 100644
@@ -56,11 +56,25 @@ static unsigned long kvm_arch_timer_get_irq_flags(void)
 	return kvm_vgic_global_state.no_hw_deactivation ? VGIC_IRQ_SW_RESAMPLE : 0;
 }
 
+static unsigned long kvm_realm_timer_get_irq_flags(void)
+{
+	/*
+	 * RMI_REC_ENTER rejects LRs with the HW bit set, so use the existing
+	 * software resampling mechanism for Realm timer interrupts.
+	 */
+	return VGIC_IRQ_SW_RESAMPLE;
+}
+
 static const struct irq_ops arch_timer_irq_ops = {
 	.get_flags	 = kvm_arch_timer_get_irq_flags,
 	.get_input_level = kvm_arch_timer_get_input_level,
 };
 
+static const struct irq_ops realm_timer_irq_ops = {
+	.get_flags	 = kvm_realm_timer_get_irq_flags,
+	.get_input_level = kvm_arch_timer_get_input_level,
+};
+
 static const struct irq_ops arch_timer_irq_ops_vgic_v5 = {
 	.get_input_level = kvm_arch_timer_get_input_level,
 	.queue_irq_unlock = vgic_v5_ppi_queue_irq_unlock,
@@ -1604,8 +1618,12 @@ int kvm_timer_enable(struct kvm_vcpu *vcpu)
 
 	get_timer_map(vcpu, &map);
 
-	ops = vgic_is_v5(vcpu->kvm) ? &arch_timer_irq_ops_vgic_v5 :
-				      &arch_timer_irq_ops;
+	if (vcpu_is_rec(vcpu))
+		ops = &realm_timer_irq_ops;
+	else if (vgic_is_v5(vcpu->kvm))
+		ops = &arch_timer_irq_ops_vgic_v5;
+	else
+		ops = &arch_timer_irq_ops;
 
 	for (int i = 0; i < nr_timers(vcpu); i++)
 		kvm_vgic_set_irq_ops(vcpu, timer_irq(vcpu_get_timer(vcpu, i)), ops);
Signature · 1 line
2.43.0
author reviewer
#39 lore raw

PATCH v18 20/23 KVM: arm64: CCA: Don't expose unsupported capabilities for realm guests

In reply to Suzuki K Poulose

Limit the capabilities that are allowed for Realm VMs. Similarly block the vm_ioctls backed by the capabilities.

Repurpose the kvm_pkvm_ioctl_allowed() to support both pKVM and Realm ioctls. Rename the helper to kvm_arch_vm_ioctl_allowed() and move it into arch/arm64/kvm/arm.c. Also add a generic kvm_arch_vm_ext_allowed() to handle pKVM and Realm capability filtering.

---
Changes since v17:
* Drop superfluous !kvm check from kvm_vm_ioctl_enable_cap() - Sashiko
* Drop KVM_CAP_CREATE_IRQCHIP, as we don't support VGIC_V2 for Realms
* Filter out the vm_ioctls that are based on blocked cap.
* Repurpose the pkvm plumbing for filtering the caps and ioctl to generic and plumb the Realm support in
Changes since v13:
* Add missing check in kvm_vm_ioctl_enable_cap().
Changes since v10:
* Add a kvm_realm_ext_allowed() function which limits which extensions are exposed to an allowlist. This removes the need for special casing various extensions.
Changes since v7:
* Remove the helper functions and inline the kvm_is_realm() check with a ternary operator.
* Rewrite the commit message to explain this patch.

---
 arch/arm64/include/asm/kvm_pkvm.h | 19 ----------
 arch/arm64/kvm/arm.c              | 58 +++++++++++++++++++++++++++++--
 2 files changed, 55 insertions(+), 22 deletions(-)
arch/arm64/include/asm/kvm_pkvm.h +0 -19
index e4ea80711bec6..1bc4fe2726e9b 100644
@@ -53,25 +53,6 @@ static inline bool kvm_pkvm_ext_allowed(struct kvm *kvm, long ext)
 	}
 }
 
-/*
- * Check whether the KVM VM IOCTL is allowed in pKVM.
- *
- * Certain features are allowed only for non-protected VMs in pKVM, which is why
- * this takes the VM (kvm) as a parameter.
- */
-static inline bool kvm_pkvm_ioctl_allowed(struct kvm *kvm, unsigned int ioctl)
-{
-	long ext;
-	int r;
-
-	r = kvm_get_cap_for_kvm_ioctl(ioctl, &ext);
-
-	if (WARN_ON_ONCE(r < 0))
-		return false;
-
-	return kvm_pkvm_ext_allowed(kvm, ext);
-}
-
 extern struct memblock_region kvm_nvhe_sym(hyp_memory)[];
 extern unsigned int kvm_nvhe_sym(hyp_memblock_nr);
 
arch/arm64/kvm/arm.c +55 -3

82 lines not on this page · raw message

Signature · 1 line
2.43.0
#40 lore raw

Re: [PATCH v18 20/23] KVM: arm64: CCA: Don't expose unsupported capabilities for realm guests

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] kvm: host Denial of Service via WARN_ON_ONCE in kvm_arch_vm_ioctl_allowed() --

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] kvm: host Denial of Service via WARN_ON_ONCE in kvm_arch_vm_ioctl_allowed() --

commit f9fdb12a2363dfa41ace289596ec2d2bd95cde48
Author: Suzuki K Poulose <[email protected]>
KVM: arm64: CCA: Don't expose unsupported capabilities for realm guests

This commit limits the capabilities allowed for Realm VMs and blocks vm_ioctls backed by unsupported capabilities. It introduces generic capability and ioctl filtering helpers kvm_arch_vm_ext_allowed() and kvm_arch_vm_ioctl_allowed() to support both pKVM and Realm guests.

Quoted 3 lines
arch/arm64/kvm/arm.c +0 -0

[ ... ]

arch/arm64/kvm/arm.c int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu) · line 186

Quoted 8 lines
arch/arm64/kvm/arm.c +8 -0
+static inline bool kvm_arch_vm_ioctl_allowed(struct kvm *kvm, unsigned int ioctl)
+{
+	long ext;
+	int r;
+
+	r = kvm_get_cap_for_kvm_ioctl(ioctl, &ext);
+	if (WARN_ON_ONCE(r < 0))
+		return false;

[Severity: High]
Could this allow an unprivileged host userspace process to crash the host?

When kvm_arch_vm_ioctl() receives an unrecognized or garbage ioctl, kvm_get_cap_for_kvm_ioctl() returns a negative value. This evaluates to true and triggers the WARN_ON_ONCE() here. On systems configured with panic_on_warn (which is common in production and Android environments), this results in a kernel panic and a complete host denial of service.

[ ... ]

arch/arm64/kvm/arm.c int kvm_arch_vm_ioctl(struct file *filp, unsigned int ioctl, unsigned long arg) · line 2206

Quoted 7 lines
arch/arm64/kvm/arm.c +1 -1
@@ -2150,7 +2202,7 @@ int kvm_arch_vm_ioctl(struct file *filp, unsigned int ioctl, unsigned long arg)
 	void __user *argp = (void __user *)arg;
 	struct kvm_device_attr attr;
 
-	if (is_protected_kvm_enabled() && !kvm_pkvm_ioctl_allowed(kvm, ioctl))
+	if (!kvm_arch_vm_ioctl_allowed(kvm, ioctl))
 		return -EINVAL;

[Severity: High]
Does removing the is_protected_kvm_enabled() check expose the WARN_ON_ONCE() above to all ARM64 KVM deployments?

Previously, the warning in kvm_pkvm_ioctl_allowed() was strictly guarded by is_protected_kvm_enabled(). By unconditionally calling kvm_arch_vm_ioctl_allowed() for all VMs, any local user with access to /dev/kvm can open a VM file descriptor and issue an unrecognized ioctl to trigger the warning. This exposes the panic condition to all ARM64 KVM deployments rather than strictly limiting it to pKVM configurations.

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

Re: [PATCH v18 20/23] KVM: arm64: CCA: Don't expose unsupported capabilities for realm guests

Quoted 17 lines · On 15/09/2026 17:43, [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] kvm: host Denial of Service via WARN_ON_ONCE in kvm_arch_vm_ioctl_allowed() --

commit f9fdb12a2363dfa41ace289596ec2d2bd95cde48
Author: Suzuki K Poulose <[email protected]>
KVM: arm64: CCA: Don't expose unsupported capabilities for realm guests

This commit limits the capabilities allowed for Realm VMs and blocks vm_ioctls backed by unsupported capabilities. It introduces generic capability and ioctl filtering helpers kvm_arch_vm_ext_allowed() and kvm_arch_vm_ioctl_allowed() to support both pKVM and Realm guests.

Quoted 3 lines
arch/arm64/kvm/arm.c +0 -0

[ ... ]

Quoted 11 lines

arch/arm64/kvm/arm.c int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu) · line 186

Quoted 8 lines
arch/arm64/kvm/arm.c +8 -0
+static inline bool kvm_arch_vm_ioctl_allowed(struct kvm *kvm, unsigned int ioctl)
+{
+	long ext;
+	int r;
+
+	r = kvm_get_cap_for_kvm_ioctl(ioctl, &ext);
+	if (WARN_ON_ONCE(r < 0))
+		return false;

[Severity: High]
Could this allow an unprivileged host userspace process to crash the host?

This is addressed by a patch from Fuad, hence I didn't try to fix that here.

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

Quoted 12 lines

When kvm_arch_vm_ioctl() receives an unrecognized or garbage ioctl, kvm_get_cap_for_kvm_ioctl() returns a negative value. This evaluates to true and triggers the WARN_ON_ONCE() here. On systems configured with panic_on_warn (which is common in production and Android environments), this results in a kernel panic and a complete host denial of service.

[ ... ]

Quoted 4 lines
arch/arm64/kvm/arm.c +0 -0
@@ -2150,7 +2202,7 @@ int kvm_arch_vm_ioctl(struct file *filp, unsigned int ioctl, unsigned long arg)
  	void __user *argp = (void __user *)arg;
  	struct kvm_device_attr attr;
 
Quoted 7 lines

arch/arm64/kvm/arm.c int kvm_arch_vm_ioctl(struct file *filp, unsigned int ioctl, unsigned long arg) · line 2206

Quoted 3 lines
arch/arm64/kvm/arm.c +1 -1
-	if (is_protected_kvm_enabled() && !kvm_pkvm_ioctl_allowed(kvm, ioctl))
+	if (!kvm_arch_vm_ioctl_allowed(kvm, ioctl))
  		return -EINVAL;

[Severity: High]
Does removing the is_protected_kvm_enabled() check expose the WARN_ON_ONCE() above to all ARM64 KVM deployments?

Thats right, I will add the following check in the kvm_arch_vm_ioctl_allowed()

if (!kvm || !kvm_vm_is_hyp_distrusting(kvm))

	return true;

Suzuki

Quoted 8 lines

Previously, the warning in kvm_pkvm_ioctl_allowed() was strictly guarded by is_protected_kvm_enabled(). By unconditionally calling kvm_arch_vm_ioctl_allowed() for all VMs, any local user with access to /dev/kvm can open a VM file descriptor and issue an unrecognized ioctl to trigger the warning. This exposes the panic condition to all ARM64 KVM deployments rather than strictly limiting it to pKVM configurations.

author reviewer
#42 lore raw

Re: [PATCH v18 20/23] KVM: arm64: CCA: Don't expose unsupported capabilities for realm guests

Quoted 59 lines · Suzuki K Poulose
Quoted 30 lines · On 15/09/2026 17:43, [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] kvm: host Denial of Service via WARN_ON_ONCE in kvm_arch_vm_ioctl_allowed()

Signature · 8 lines
commit f9fdb12a2363dfa41ace289596ec2d2bd95cde48
Author: Suzuki K Poulose <[email protected]>
KVM: arm64: CCA: Don't expose unsupported capabilities for realm guests

This commit limits the capabilities allowed for Realm VMs and blocks
vm_ioctls backed by unsupported capabilities. It introduces generic
capability and ioctl filtering helpers kvm_arch_vm_ext_allowed() and
kvm_arch_vm_ioctl_allowed() to support both pKVM and Realm guests.
Quoted 3 lines
arch/arm64/kvm/arm.c +0 -0

[ ... ]

arch/arm64/kvm/arm.c int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu) · line 186

Quoted 9 lines

+static inline bool kvm_arch_vm_ioctl_allowed(struct kvm *kvm, unsigned int ioctl)

arch/arm64/kvm/arm.c +7 -0
+{
+    long ext;
+    int r;
+
+    r = kvm_get_cap_for_kvm_ioctl(ioctl, &ext);
+    if (WARN_ON_ONCE(r < 0))
+        return false;

[Severity: High]
Could this allow an unprivileged host userspace process to crash the host?

This is addressed by a patch from Fuad, hence I didn't try to fix that here.

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

Quoted 8 lines

When kvm_arch_vm_ioctl() receives an unrecognized or garbage ioctl, kvm_get_cap_for_kvm_ioctl() returns a negative value. This evaluates to true and triggers the WARN_ON_ONCE() here. On systems configured with panic_on_warn (which is common in production and Android environments), this results in a kernel panic and a complete host denial of service.

[ ... ]

Quoted 12 lines

arch/arm64/kvm/arm.c int kvm_arch_vm_ioctl(struct file *filp, · line 2207

Quoted 8 lines
arch/arm64/kvm/arm.c +1 -1
@@ -2150,7 +2202,7 @@ int kvm_arch_vm_ioctl(struct file *filp, 
 unsigned int ioctl, unsigned long arg)
       void __user *argp = (void __user *)arg;
       struct kvm_device_attr attr;
-    if (is_protected_kvm_enabled() && !kvm_pkvm_ioctl_allowed(kvm, 
 ioctl))
+    if (!kvm_arch_vm_ioctl_allowed(kvm, ioctl))
           return -EINVAL;

[Severity: High]
Does removing the is_protected_kvm_enabled() check expose the WARN_ON_ONCE() above to all ARM64 KVM deployments?

Quoted 4 lines

Thats right, I will add the following check in the kvm_arch_vm_ioctl_allowed()

if (!kvm || !kvm_vm_is_hyp_distrusting(kvm))
    return true;

Thats not correct. It should be :

if (!is_protected_kvm_enabled() &&

    (!kvm || !kvm_is_realm(kvm)))
	return true;

Cheers
Suzuki

#43 lore raw

Re: [PATCH v18 20/23] KVM: arm64: CCA: Don't expose unsupported capabilities for realm guests

In reply to Suzuki K Poulose

Hi Suzuki,

On Tue, 15 Sep 2026 17:01:38 +0100, Suzuki K Poulose <[email protected]> wrote:
[...]

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

[...]

Quoted 9 lines
arch/arm64/kvm/arm.c +9 -0
+static inline bool kvm_realm_ext_allowed(long ext)
+{
+        switch (ext) {
+        case KVM_CAP_ARM_PSCI:
+        case KVM_CAP_ARM_PSCI_0_2:
+        case KVM_CAP_NR_VCPUS:
+        case KVM_CAP_MAX_VCPUS:
+        case KVM_CAP_MAX_VCPU_ID:
+        case KVM_CAP_MSI_DEVID:

arch/arm64/kvm/arm.c int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu) · line 157

Quoted 10 lines
arch/arm64/kvm/arm.c +10 -0
+        case KVM_CAP_ARM_VM_IPA_SIZE:
+        case KVM_CAP_ARM_SVE:
+        case KVM_CAP_ONE_REG:
+        case KVM_CAP_ARM_PTRAUTH_ADDRESS:
+        case KVM_CAP_ARM_PTRAUTH_GENERIC:
+        case KVM_CAP_SYNC_MMU:
+                return true;
+        }
+        return false;
+}

I'd keep KVM_CAP_IRQCHIP in this list. api.rst has it advertising KVM_IRQ_LINE as well as KVM_CREATE_IRQCHIP, so the cap means in-kernel irqchip rather than GICv2, and the GICv2 case is already rejected by patch 18 in kvm_vgic_create(). It also keeps the two protected lists the same where the support is the same. And could this live in kvm_rmi.h, next to where kvm_pkvm_ext_allowed() lives in kvm_pkvm.h, rather than in arm.c?

[...]

arch/arm64/kvm/arm.c int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu) · line 186

Quoted 8 lines
arch/arm64/kvm/arm.c +8 -0
+static inline bool kvm_arch_vm_ioctl_allowed(struct kvm *kvm, unsigned int ioctl)
+{
+        long ext;
+        int r;
+
+        r = kvm_get_cap_for_kvm_ioctl(ioctl, &ext);
+        if (WARN_ON_ONCE(r < 0))
+                return false;

You told Sashiko my fix covers this one [1], but that fix changes kvm_pkvm_ioctl_allowed(), which this patch deletes, so the if (r < 0) has to go into this copy too. The guard you posted for the second finding still leaves the WARN reachable on pKVM and Realm hosts, which is the case the fix removed.

nit: kvm_arch_ is the prefix generic KVM uses for the hooks it calls into the arch. For two static helpers in arm.c, kvm_vm_ext_allowed() and kvm_vm_ioctl_allowed() would avoid the collision, and the same for kvm_arch_vm_ext_allowed() above.

Cheers,
/fuad

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

author reviewer
#44 lore raw

Re: [PATCH v18 20/23] KVM: arm64: CCA: Don't expose unsupported capabilities for realm guests

Quoted 23 lines · Fuad Tabba

Hi Suzuki,

On Tue, 15 Sep 2026 17:01:38 +0100, Suzuki K Poulose <[email protected]> wrote:
[...]

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

[...]

Quoted 16 lines
arch/arm64/kvm/arm.c +16 -0
+static inline bool kvm_realm_ext_allowed(long ext)
+{
+        switch (ext) {
+        case KVM_CAP_ARM_PSCI:
+        case KVM_CAP_ARM_PSCI_0_2:
+        case KVM_CAP_NR_VCPUS:
+        case KVM_CAP_MAX_VCPUS:
+        case KVM_CAP_MAX_VCPU_ID:
+        case KVM_CAP_MSI_DEVID:
+        case KVM_CAP_ARM_VM_IPA_SIZE:
+        case KVM_CAP_ARM_SVE:
+        case KVM_CAP_ONE_REG:
+        case KVM_CAP_ARM_PTRAUTH_ADDRESS:
+        case KVM_CAP_ARM_PTRAUTH_GENERIC:
+        case KVM_CAP_SYNC_MMU:
+                return true;
Quoted 8 lines

arch/arm64/kvm/arm.c int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu) · line 157

Quoted 3 lines
arch/arm64/kvm/arm.c +3 -0
+        }
+        return false;
+}

I'd keep KVM_CAP_IRQCHIP in this list. api.rst has it advertising KVM_IRQ_LINE as well as KVM_CREATE_IRQCHIP, so the cap means in-kernel irqchip rather than GICv2, and the GICv2 case is already rejected by patch 18 in kvm_vgic_create(). It also keeps the two protected lists

Ack

Quoted 3 lines

the same where the support is the same. And could this live in kvm_rmi.h, next to where kvm_pkvm_ext_allowed() lives in kvm_pkvm.h, rather than in arm.c?

From what I remember, it creates weird header file dependencies. I will see if I can resolve them.

Quoted 16 lines

[...]

arch/arm64/kvm/arm.c int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu) · line 186

Quoted 8 lines
arch/arm64/kvm/arm.c +8 -0
+static inline bool kvm_arch_vm_ioctl_allowed(struct kvm *kvm, unsigned int ioctl)
+{
+        long ext;
+        int r;
+
+        r = kvm_get_cap_for_kvm_ioctl(ioctl, &ext);
+        if (WARN_ON_ONCE(r < 0))
+                return false;

You told Sashiko my fix covers this one [1], but that fix changes kvm_pkvm_ioctl_allowed(), which this patch deletes, so the if (r < 0) has to go into this copy too. The guard you posted for the second finding still leaves the WARN reachable on pKVM and Realm hosts, which is the case the fix removed.

I had the chat with Marc about this and his recommendation was to post my changes as they are and he would resolve the changes while managing the conflict.

Quoted 5 lines

nit: kvm_arch_ is the prefix generic KVM uses for the hooks it calls into the arch. For two static helpers in arm.c, kvm_vm_ext_allowed() and kvm_vm_ioctl_allowed() would avoid the collision, and the same for kvm_arch_vm_ext_allowed() above.

Ack

Suzuki

Quoted 5 lines

Cheers,
/fuad

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

author reviewer
#45 lore raw

PATCH v18 21/23 KVM: arm64: CCA: WARN on injected undef exceptions

In reply to Suzuki K Poulose

From: Steven Price <[email protected]>

The RMM doesn't allow injection of a undefined exception into a realm guest. Add a WARN to catch if this ever happens.

---
Changes since v15:
* Switch to KVM_BUG() to mark the VM as bugged as well.
Changes since v6:
* if (x) WARN(1, ...) makes no sense, just WARN(x, ...)!

---
 arch/arm64/kvm/inject_fault.c | 1 +
 1 file changed, 1 insertion(+)
arch/arm64/kvm/inject_fault.c +1 -0
index d6c4fc16f8795..d61a3ff04fabb 100644
@@ -317,6 +317,7 @@ void kvm_inject_size_fault(struct kvm_vcpu *vcpu)
  */
 void kvm_inject_undefined(struct kvm_vcpu *vcpu)
 {
+	KVM_BUG(vcpu_is_rec(vcpu), vcpu->kvm, "Unexpected undefined exception injection to REC");
 	if (vcpu_el1_is_32bit(vcpu))
 		inject_undef32(vcpu);
 	else
Signature · 1 line
2.43.0
author reviewer
#46 lore raw

PATCH v18 22/23 KVM: arm64: CCA: Expose SVE VL register before VCPU finalization

In reply to Suzuki K Poulose

From: Jean-Philippe Brucker <[email protected]>

Userspace must configure the SVE vector length before the Realm is created (as it is part of the parameter for Realm creation), but the Realm VCPUs cannot be finalized until after the Realm Descriptor has been created.

KVM_GET_REG_LIST currently rejects the unfinalized VCPUs, which prevents the userspace from discovering and configuring the VLs for the Realm.

Allow KVM_GET_REG_LIST for unfinalized RECs and make the SVE register enumeration handle the unfinalized case explicitly. i.e., only expose KVM_REG_ARM64_SVE_VLS before SVE is finalized.

One adverse side effect of this change is that a KVM_GET_REG_LIST call that only probes for the array size will now succeed even if SVE is not finalized, but that seems harmless since the following KVM_GET_REG_LIST with the full array will fail.

---
Changes since v17:
- Rewrite the commit description to clearly describe the purpose

---
 arch/arm64/kvm/arm.c   | 15 ++++++++++++++-
 arch/arm64/kvm/guest.c | 10 +++++-----
 2 files changed, 19 insertions(+), 6 deletions(-)
arch/arm64/kvm/arm.c +14 -1
index df0aa66b6f5ed..3d8ede8460e96 100644
@@ -2017,6 +2017,19 @@ static int kvm_arm_vcpu_set_events(struct kvm_vcpu *vcpu,
 	return __kvm_arm_vcpu_set_events(vcpu, events);
 }
 
+/*
+ * Realm VCPUs can be finalized only after the Realm descriptor is created.
+ * But in order to seal the SVE VL, we need to allow the userspace to read/write
+ * to the SVE_VL, before everything is finalized.
+ * Allow the register list for RECs before the VCPUs are finalized.
+ */
+static bool kvm_arm_vcpu_reg_list_allowed(struct kvm_vcpu *vcpu)
+{
+	if (kvm_arm_vcpu_is_finalized(vcpu))
+		return true;
+	return vcpu_is_rec(vcpu);
+}
+
 long kvm_arch_vcpu_ioctl(struct file *filp,
 			 unsigned int ioctl, unsigned long arg)
 {
@@ -2072,7 +2085,7 @@ long kvm_arch_vcpu_ioctl(struct file *filp,
 			break;
 
 		r = -EPERM;
-		if (!kvm_arm_vcpu_is_finalized(vcpu))
+		if (!kvm_arm_vcpu_reg_list_allowed(vcpu))
 			break;
 
 		r = -EFAULT;
arch/arm64/kvm/guest.c +5 -5
index b01d6622b8720..c3ca369882273 100644
@@ -598,8 +598,8 @@ static unsigned long num_sve_regs(const struct kvm_vcpu *vcpu)
 	if (!vcpu_has_sve(vcpu))
 		return 0;
 
-	/* Policed by KVM_GET_REG_LIST: */
-	WARN_ON(!kvm_arm_vcpu_sve_finalized(vcpu));
+	if (!kvm_arm_vcpu_sve_finalized(vcpu))
+		return 1; /* KVM_REG_ARM64_SVE_VLS */
 
 	return slices * (SVE_NUM_PREGS + SVE_NUM_ZREGS + 1 /* FFR */)
 		+ 1; /* KVM_REG_ARM64_SVE_VLS */
@@ -616,9 +616,6 @@ static int copy_sve_reg_indices(const struct kvm_vcpu *vcpu,
 	if (!vcpu_has_sve(vcpu))
 		return 0;
 
-	/* Policed by KVM_GET_REG_LIST: */
-	WARN_ON(!kvm_arm_vcpu_sve_finalized(vcpu));
-
 	/*
 	 * Enumerate this first, so that userspace can save/restore in
 	 * the order reported by KVM_GET_REG_LIST:
@@ -628,6 +625,9 @@ static int copy_sve_reg_indices(const struct kvm_vcpu *vcpu,
 		return -EFAULT;
 	++num_regs;
 
+	if (!kvm_arm_vcpu_sve_finalized(vcpu))
+		return num_regs;
+
 	for (i = 0; i < slices; i++) {
 		for (n = 0; n < SVE_NUM_ZREGS; n++) {
 			reg = KVM_REG_ARM64_SVE_ZREG(n, i);
Signature · 1 line
2.43.0
author reviewer
#47 lore raw

PATCH v18 23/23 KVM: arm64: CCA: Control user register access for Realms

In reply to Suzuki K Poulose

From: Jean-Philippe Brucker <[email protected]>

The RMM restricts the access to the register states that the host can read/modify for a given Realm.

e.g., At VCPU creation, can modify GPRS (x0-x30) and PC.
While servicing SMCCC calls via RSI_HOST_CALL or servicing PSCI requests. MMIO emulation in the unprotected space.

Additionally we use the sysreg configuration to advertise/configure the following Realm parameters, which are required before the Realm Descriptor is created:
- SVE Vector Length
- Number of HW Breakpoints/Watchpoints
- PMU Counters.

Thus KVM also additionally allows access to ID_AA64DFR0_EL1 and SVE_VLS for the configuration of Realm creation paramters.

The RMM makes similar restrictions for reading of the guest's registers (this is *confidential* compute after all), however we don't impose the restriction here. This allows the VMM to read (stale) values from the registers which might be useful to read back the initial values even if the RMM doesn't provide the latest version. For migration of a realm VM, a new interface will be needed so that the VMM can receive an (encrypted) blob of the VM's state.

Reflect the above in KVM_GET_REG_LIST, KVM_SET_ONE_REG calls.

---
Changes since v17:
- Merge related changes into one single patch for the user set/get registers I have retained the Review tags, as the code hasn't changed, just the patches were merged into a single one with the same tags.
- Limit KVM_GET_REG_LIST to the allowed CORE registers.

---
 arch/arm64/kvm/guest.c      | 63 +++++++++++++++++++++++++++++++++++++
 arch/arm64/kvm/hypercalls.c |  4 +--
 arch/arm64/kvm/sys_regs.c   | 29 +++++++++++++----
 3 files changed, 88 insertions(+), 8 deletions(-)
arch/arm64/kvm/guest.c +63 -0
index c3ca369882273..4ec95810ca89b 100644
@@ -73,6 +73,25 @@ static u64 core_reg_offset_from_id(u64 id)
 	return id & ~(KVM_REG_ARCH_MASK | KVM_REG_SIZE_MASK | KVM_REG_ARM_CORE);
 }
 
+static bool kvm_realm_validate_core_reg(u64 off)
+{
+	/*
+	 * Note that GPRs can only sometimes be controlled by the VMM.
+	 * For PSCI only X0-X6 are used, higher registers are ignored (restored
+	 * from the REC).
+	 * For HOST_CALL all of X0-X30 are copied to the RsiHostCall structure.
+	 * For emulated MMIO X0 is always used.
+	 * PC can only be set before the realm is activated.
+	 */
+	switch (off) {
+	case KVM_REG_ARM_CORE_REG(regs.regs[0]) ...
+	     KVM_REG_ARM_CORE_REG(regs.regs[30]):
+	case KVM_REG_ARM_CORE_REG(regs.pc):
+		return true;
+	}
+	return false;
+}
+
 static int core_reg_size_from_offset(const struct kvm_vcpu *vcpu, u64 off)
 {
 	int size;
@@ -553,6 +572,9 @@ static int copy_core_reg_indices(const struct kvm_vcpu *vcpu,
 		u64 reg = KVM_REG_ARM64 | KVM_REG_ARM_CORE | i;
 		int size = core_reg_size_from_offset(vcpu, i);
 
+		if (vcpu_is_rec(vcpu) && !kvm_realm_validate_core_reg(i))
+			continue;
+
 		if (size < 0)
 			continue;
 
@@ -598,6 +620,9 @@ static unsigned long num_sve_regs(const struct kvm_vcpu *vcpu)
 	if (!vcpu_has_sve(vcpu))
 		return 0;
 
+	if (kvm_vm_is_realm(vcpu->kvm))
+		return 1; /* KVM_REG_ARM64_SVE_VLS */
+
 	if (!kvm_arm_vcpu_sve_finalized(vcpu))
 		return 1; /* KVM_REG_ARM64_SVE_VLS */
 
@@ -625,6 +650,10 @@ static int copy_sve_reg_indices(const struct kvm_vcpu *vcpu,
 		return -EFAULT;
 	++num_regs;
 
+	/* For Realms only support SVE_VLS */
+	if (kvm_vm_is_realm(vcpu->kvm))
+		return num_regs;
+
 	if (!kvm_arm_vcpu_sve_finalized(vcpu))
 		return num_regs;
 
@@ -705,6 +734,11 @@ int kvm_arm_get_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg)
 	if ((reg->id & ~KVM_REG_SIZE_MASK) >> 32 != KVM_REG_ARM64 >> 32)
 		return -EINVAL;
 
+	/*
+	  * We don't filter out the register reads for Realms, like we do for
+	  * the user writes. We expose junk data for the VMM instead of
+	  * denying the requests.
+	  */
 	switch (reg->id & KVM_REG_ARM_COPROC_MASK) {
 	case KVM_REG_ARM_CORE:	return get_core_reg(vcpu, reg);
 	case KVM_REG_ARM_FW:
@@ -716,12 +750,41 @@ int kvm_arm_get_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg)
 	return kvm_arm_sys_reg_get_reg(vcpu, reg);
 }
 
+#define KVM_REG_ARM_ID_AA64DFR0_EL1	ARM64_SYS_REG(3, 0, 0, 5, 0)
+/*
+ * The RMI ABI only enables setting some GPRs and PC. The selection of GPRs
+ * that are available depends on the Realm state and the reason for the last
+ * exit.  All other registers are reset to architectural or otherwise defined
+ * reset values by the RMM, except for a few configuration fields that
+ * correspond to Realm parameters.
+ */
+static bool validate_realm_set_reg(struct kvm_vcpu *vcpu,
+				   const struct kvm_one_reg *reg)
+{
+	if ((reg->id & KVM_REG_ARM_COPROC_MASK) == KVM_REG_ARM_CORE) {
+		u64 off = core_reg_offset_from_id(reg->id);
+
+		return kvm_realm_validate_core_reg(off);
+	} else {
+		switch (reg->id) {
+		case KVM_REG_ARM_ID_AA64DFR0_EL1:
+		case KVM_REG_ARM64_SVE_VLS:
+			return true;
+		}
+	}
+
+	return false;
+}
+
 int kvm_arm_set_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg)
 {
 	/* We currently use nothing arch-specific in upper 32 bits */
 	if ((reg->id & ~KVM_REG_SIZE_MASK) >> 32 != KVM_REG_ARM64 >> 32)
 		return -EINVAL;
 
+	if (kvm_vm_is_realm(vcpu->kvm) && !validate_realm_set_reg(vcpu, reg))
+		return -EINVAL;
+
 	switch (reg->id & KVM_REG_ARM_COPROC_MASK) {
 	case KVM_REG_ARM_CORE:	return set_core_reg(vcpu, reg);
 	case KVM_REG_ARM_FW:
arch/arm64/kvm/hypercalls.c +2 -2
index b11b8821c9fbc..2b1e6fdeb4d5c 100644
@@ -414,14 +414,14 @@ void kvm_arm_teardown_hypercalls(struct kvm *kvm)
 
 int kvm_arm_get_fw_num_regs(struct kvm_vcpu *vcpu)
 {
-	return ARRAY_SIZE(kvm_arm_fw_reg_ids);
+	return vcpu_is_rec(vcpu) ? 0 : ARRAY_SIZE(kvm_arm_fw_reg_ids);
 }
 
 int kvm_arm_copy_fw_reg_indices(struct kvm_vcpu *vcpu, u64 __user *uindices)
 {
 	int i;
 
-	for (i = 0; i < ARRAY_SIZE(kvm_arm_fw_reg_ids); i++) {
+	for (i = 0; i < kvm_arm_get_fw_num_regs(vcpu); i++) {
 		if (put_user(kvm_arm_fw_reg_ids[i], uindices++))
 			return -EFAULT;
 	}
arch/arm64/kvm/sys_regs.c +23 -6
index 44aae52c473d7..520cec19f49fa 100644
@@ -5638,18 +5638,18 @@ int kvm_arm_sys_reg_set_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg
 				    sys_reg_descs, ARRAY_SIZE(sys_reg_descs));
 }
 
-static unsigned int num_demux_regs(void)
+static inline unsigned int num_demux_regs(struct kvm_vcpu *vcpu)
 {
-	return CSSELR_MAX;
+	return vcpu_is_rec(vcpu) ? 0 : CSSELR_MAX;
 }
 
-static int write_demux_regids(u64 __user *uindices)
+static int write_demux_regids(struct kvm_vcpu *vcpu, u64 __user *uindices)
 {
 	u64 val = KVM_REG_ARM64 | KVM_REG_SIZE_U32 | KVM_REG_ARM_DEMUX;
 	unsigned int i;
 
 	val |= KVM_REG_ARM_DEMUX_ID_CCSIDR;
-	for (i = 0; i < CSSELR_MAX; i++) {
+	for (i = 0; i < num_demux_regs(vcpu); i++) {
 		if (put_user(val | i, uindices))
 			return -EFAULT;
 		uindices++;
@@ -5693,11 +5693,28 @@ static bool copy_reg_to_user(const struct sys_reg_desc *reg, u64 __user **uind)
 	return true;
 }
 
+static inline bool kvm_realm_sys_reg_hidden_user(const struct kvm_vcpu *vcpu,
+						 u64 reg)
+{
+	if (!vcpu_is_rec(vcpu))
+		return false;
+
+	switch (reg) {
+	case SYS_ID_AA64DFR0_EL1:
+	case SYS_PMCR_EL0:
+		return false;
+	}
+	return true;
+}
+
 static int walk_one_sys_reg(const struct kvm_vcpu *vcpu,
 			    const struct sys_reg_desc *rd,
 			    u64 __user **uind,
 			    unsigned int *total)
 {
+	if (kvm_realm_sys_reg_hidden_user(vcpu, reg_to_encoding(rd)))
+		return 0;
+
 	/*
 	 * Ignore registers we trap but don't save,
 	 * and for which no custom user accessor is provided.
@@ -5735,7 +5752,7 @@ static int walk_sys_regs(struct kvm_vcpu *vcpu, u64 __user *uind)
 
 unsigned long kvm_arm_num_sys_reg_descs(struct kvm_vcpu *vcpu)
 {
-	return num_demux_regs()
+	return num_demux_regs(vcpu)
 		+ walk_sys_regs(vcpu, (u64 __user *)NULL);
 }
 
@@ -5748,7 +5765,7 @@ int kvm_arm_copy_sys_reg_indices(struct kvm_vcpu *vcpu, u64 __user *uindices)
 		return err;
 	uindices += err;
 
-	return write_demux_regids(uindices);
+	return write_demux_regids(vcpu, uindices);
 }
 
 #define KVM_ARM_FEATURE_ID_RANGE_INDEX(r)			\
Signature · 1 line
2.43.0
#48 lore raw

Re: [PATCH v18 23/23] KVM: arm64: CCA: Control user register access for Realms

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] arm64: kvm: KVM_SET_ONE_REG rejects PMCR_EL0 for Realm VMs breaking PMU config --

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] arm64: kvm: KVM_SET_ONE_REG rejects PMCR_EL0 for Realm VMs breaking PMU config --

commit 2d966a4231d9ac9707a0d74a6ba5b743ae7b69b3
Author: Jean-Philippe Brucker <[email protected]>

KVM: arm64: CCA: Control user register access for Realms

This commit restricts user register access for Realm VMs by limiting which registers the VMM can read or modify. KVM explicitly allows access to ID_AA64DFR0_EL1 and SVE_VLS for Realm parameter configuration before the Realm Descriptor is created.

Quoted 3 lines
arch/arm64/kvm/guest.c +0 -0

[ ... ]

Quoted 7 lines
arch/arm64/kvm/guest.c +7 -0
+static bool validate_realm_set_reg(struct kvm_vcpu *vcpu,
+				   const struct kvm_one_reg *reg)
+{
+	if ((reg->id & KVM_REG_ARM_COPROC_MASK) == KVM_REG_ARM_CORE) {
+		u64 off = core_reg_offset_from_id(reg->id);
+
+		return kvm_realm_validate_core_reg(off);

arch/arm64/kvm/guest.c int kvm_arm_get_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg) · line 777

Quoted 10 lines
arch/arm64/kvm/guest.c +10 -0
+	} else {
+		switch (reg->id) {
+		case KVM_REG_ARM_ID_AA64DFR0_EL1:
+		case KVM_REG_ARM64_SVE_VLS:
+			return true;
+		}
+	}
+
+	return false;
+}

[Severity: High]
Does this logic unconditionally reject KVM_SET_ONE_REG calls for PMCR_EL0?

When the VMM attempts to restore or configure the PMU, kvm_arm_set_reg() calls validate_realm_set_reg(). Because the UAPI equivalent of SYS_PMCR_EL0 is missing from this whitelist, the VMM is prevented from configuring the number of PMU counters for Realm VMs, breaking VM initialization.

Quoted 3 lines
arch/arm64/kvm/sys_regs.c +0 -0

[ ... ]

arch/arm64/kvm/sys_regs.c static bool copy_reg_to_user(const struct sys_reg_desc *reg, u64 __user **uind) · line 5708

Quoted 13 lines
arch/arm64/kvm/sys_regs.c +13 -0
+static inline bool kvm_realm_sys_reg_hidden_user(const struct kvm_vcpu *vcpu,
+						 u64 reg)
+{
+	if (!vcpu_is_rec(vcpu))
+		return false;
+
+	switch (reg) {
+	case SYS_ID_AA64DFR0_EL1:
+	case SYS_PMCR_EL0:
+		return false;
+	}
+	return true;
+}

[Severity: High]
This explicitly unhides SYS_PMCR_EL0 in kvm_realm_sys_reg_hidden_user(), meaning it is returned to the VMM via KVM_GET_REG_LIST.

Since validate_realm_set_reg() in arch/arm64/kvm/guest.c rejects writes to the PMU register, does this create an ABI inconsistency where the register is listed but cannot be set?

Could this mismatch in configuration flow be corrected to allow PMU support for Realm VMs?

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

Re: [PATCH v18 23/23] KVM: arm64: CCA: Control user register access for Realms

Quoted 66 lines · On 15/09/2026 17:49, [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] arm64: kvm: KVM_SET_ONE_REG rejects PMCR_EL0 for Realm VMs breaking PMU config --

commit 2d966a4231d9ac9707a0d74a6ba5b743ae7b69b3
Author: Jean-Philippe Brucker <[email protected]>

KVM: arm64: CCA: Control user register access for Realms

This commit restricts user register access for Realm VMs by limiting which registers the VMM can read or modify. KVM explicitly allows access to ID_AA64DFR0_EL1 and SVE_VLS for Realm parameter configuration before the Realm Descriptor is created.

Quoted 3 lines
arch/arm64/kvm/guest.c +0 -0

[ ... ]

Quoted 7 lines
arch/arm64/kvm/guest.c +7 -0
+static bool validate_realm_set_reg(struct kvm_vcpu *vcpu,
+				   const struct kvm_one_reg *reg)
+{
+	if ((reg->id & KVM_REG_ARM_COPROC_MASK) == KVM_REG_ARM_CORE) {
+		u64 off = core_reg_offset_from_id(reg->id);
+
+		return kvm_realm_validate_core_reg(off);

arch/arm64/kvm/guest.c int kvm_arm_get_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg) · line 777

Quoted 10 lines
arch/arm64/kvm/guest.c +10 -0
+	} else {
+		switch (reg->id) {
+		case KVM_REG_ARM_ID_AA64DFR0_EL1:
+		case KVM_REG_ARM64_SVE_VLS:
+			return true;
+		}
+	}
+
+	return false;
+}

[Severity: High]
Does this logic unconditionally reject KVM_SET_ONE_REG calls for PMCR_EL0?

When the VMM attempts to restore or configure the PMU, kvm_arm_set_reg() calls validate_realm_set_reg(). Because the UAPI equivalent of SYS_PMCR_EL0 is missing from this whitelist, the VMM is prevented from configuring the number of PMU counters for Realm VMs, breaking VM initialization.

Quoted 3 lines
arch/arm64/kvm/sys_regs.c +0 -0

[ ... ]

arch/arm64/kvm/sys_regs.c static bool copy_reg_to_user(const struct sys_reg_desc *reg, u64 __user **uind) · line 5708

Quoted 13 lines
arch/arm64/kvm/sys_regs.c +13 -0
+static inline bool kvm_realm_sys_reg_hidden_user(const struct kvm_vcpu *vcpu,
+						 u64 reg)
+{
+	if (!vcpu_is_rec(vcpu))
+		return false;
+
+	switch (reg) {
+	case SYS_ID_AA64DFR0_EL1:
+	case SYS_PMCR_EL0:
+		return false;
+	}
+	return true;
+}

[Severity: High]
This explicitly unhides SYS_PMCR_EL0 in kvm_realm_sys_reg_hidden_user(), meaning it is returned to the VMM via KVM_GET_REG_LIST.

Quoted 6 lines

Since validate_realm_set_reg() in arch/arm64/kvm/guest.c rejects writes to the PMU register, does this create an ABI inconsistency where the register is listed but cannot be set?

Could this mismatch in configuration flow be corrected to allow PMU support for Realm VMs?

Yep, this is the idea. We don't have the PMU support in the current integration branch. That said, I will remove the PMCR_EL0 from the validate_realm_set_reg().

Cheers
Suzuki

Quoted 1 line
#50 lore raw
In reply to Suzuki K Poulose

Hi Suzuki,

Quoted 43 lines · Suzuki K Poulose

This series is a trimmed down version of the Arm CCA KVM support, previously posted here [0]. Like in the v17, we have tried to split the entire series into the following chunks.

1) Base RMM RMI support under drivers/firmware/arm_rmm -> [1]
2) Linux Host support for handling GPFs - [2]
3) NEW: Enlighten KVM arm64 about the different VM types and use call backs for the VM type, rather than spilling the is_this_type_of_vm() everywhere. Adds VCPU and Stage2 MMU related callbacks with support for the existing VM types. There are other places where we may be able to abstract, but those need careful performance evaluations to make sure they are fit (e.g., vcpu_run)

Later in the series, we generalise the predicate "kvm_vm_is_protected()" to cover all "Confidential" VMs (which includes Protected VM and Realms), which allows us to handle common themes without having to do things like :

     if (kvm_vm_is_protected() || kvm_vm_is_realm()),
     instead:
     if (kvm_vm_is_confidential())

Also replaces the code with precise check for a given VM type to avoiding combination of if (). e.g,, kvm_vm_is_unprotected_pkvm(kvm). The checks under arch/arm64/kvm/{nvhe,pkvm} still retain the vm_is_protected() check as pVMs are the only possible protected VMs there.

4) Bare minimal Realm VM support without the actual functionality to run a Realm. This would help the maintainers to review the series in smaller chunks. This doesn't depend on [1] and can be independently merged, without being "functional".
This series includes vcpu operations and the s2 vm operations, which do need the RMI driver backend to be meaningful. But the KVM handler is in the right shape. The remaining changes would be added once the RMI firmware library lands.
5) Core implementation of the RMI driver for KVM and actual enablement of the Realm support. This depends on (1), (2) and the guest-memfd-in-place conversion series v12 from Ackerley. This is available here at the integration branch [3]

This series is comprised of (3) and (4) above.

The integration branch has been tested with the following components:
tf-RMM: main branch (commit 5e6e2acd) compliant to RMM-v2.0-beta3 [4]
kvmtool: [email protected]:linux-arm/kvmtool-cca.git cca/kvm-v18

Quoted 5 lines

[0] Arm CCA KVM Support v16 : https://lore.kernel.org/all/[email protected]
[1] Linux firmware RMI https://lore.kernel.org/all/[email protected]
[2] Linux GPF Host https://lore.kernel.org/all/[email protected]
[3] https://git.gitlab.arm.com/linux-arm/linux-cca/ cca/cca-host/kvm-v18/integration
[4] https://support.arm.com/documentation/den0137/2-0bet3/

When testing on the FVP model with the above baselines, I get the following error messagaes in a loop, preventing the system from reaching the command line:

[    3.112577] Freeing unused kernel memory: 3520K
[    3.115398] Run /sbin/init as init process
[    3.142926] virtio-pci 0000:00:02.0: swiotlb buffer is full (sz: 4096 bytes),
total 512 (slots), used 511 (slots)
[    3.150111] virtio-pci 0000:00:02.0: swiotlb buffer is full (sz: 4096 bytes),
total 512 (slots), used 511 (slots)
[    3.177190] virtio-pci 0000:00:02.0: swiotlb buffer is full (sz: 4096 bytes),
total 512 (slots), used 511 (slots)
[    3.197194] virtio-pci 0000:00:02.0: swiotlb buffer is full (sz: 4096 bytes),
total 512 (slots), used 511 (slots)
[    3.217188] virtio-pci 0000:00:02.0: swiotlb buffer is full (sz: 4096 bytes),
total 512 (slots), used 511 (slots)
[    3.237194] virtio-pci 0000:00:02.0: swiotlb buffer is full (sz: 4096 bytes),
total 512 (slots), used 511 (slots)
[    3.257181] virtio-pci 0000:00:02.0: swiotlb buffer is full (sz: 4096 bytes),
total 512 (slots), used 511 (slots)
[    3.277189] virtio-pci 0000:00:02.0: swiotlb buffer is full (sz: 4096 bytes),
total 512 (slots), used 511 (slots)
[    3.297193] virtio-pci 0000:00:02.0: swiotlb buffer is full (sz: 4096 bytes),
total 512 (slots), used 511 (slots)
[    3.317188] virtio-pci 0000:00:02.0: swiotlb buffer is full (sz: 4096 bytes),
total 512 (slots), used 511 (slots)
[    8.157185] swiotlb_tbl_map_single: 241 callbacks suppressed

I get the same result with v17. Have you seen this before? Perhaps a kernel option that needs to be adjusted?

Thanks,
Mathieu

Quoted 105 lines

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

- Add a patch to fix pKVM handling of SYS_CNTVCT/CNTPCT to override the counter offset (Patch1)
- Restrict Realms to VGIC v3 only - New patch
- Add kvm_vm_is_unprotected() to replace is_protected_kvm_enabled() && !kvm_vm_is_protected() - New patch
- Use macro to initialize the per-flavor vcpu, s2_vm ops
- Add a wrapper to initialise vcpu and s2_vm ops with a BUILD_BUG_ON() for the array size checks against VM flavour types
- Drop forward decalaration of the vcpu, s2_vm operations that spoiled the fun ;-)
- Remove irrelevant comment about the order of timer loading for !VHE
- Use the explicti kvm_call_hyp_nvhe for pKVM specific ops
- Don't call nvhe_vcpu_put from pkvm_vcpu_put, open code them
- Drop cpu argument for vcpu_load() callback. We set the cpu
before the callbacks are invoked
- Drop kvm_vm_is_confidential(), instead widen the scope of kvm_vm_is_protected() to cover pVMs and Realms. Add an explicit helper kvm_vm_is_protected_pkvm() for the cases where we need to check for a "pVM on pKVM"
- Add kvm_vm_hyp_is_pkvm() for checking if the VM is running on pKVM. covers both unprotected and pvms. But really uses is_protected_kvm_enabled() under the hood
- Add kvm_vm_hyp_is_distrusting() to cover pKVM guests (both protected and unprotected) and Realms. Use this for preventing the vgic v2 mapping into Stage2 for a guest
- Drop superfluous !kvm check from kvm_vm_ioctl_enable_cap() - Sashiko
- Drop KVM_CAP_CREATE_IRQCHIP, as we don't support VGIC_V2 for Realms
- Filter out the vm_ioctls that are based on blocked cap.
- Repurpose the pkvm plumbing for filtering the caps and ioctl to generic and plumb the Realm support in
- s/PKVM/pKVM for the comments
- Drop type argument for pkvm_init_host_vm and also drop protected variable, now that we have the vm_flavor to check.
- Use kvm_vm_hyp_is_pkvm() to replace is_protected_kvm_enabled() with valid kvm instance
- CCA: Merge the GET/SET REG handling patches into a single patch
- CCA: Reword the commit description for SVE VL access handling
- Reordered the patches to group the Realm realted to changes to the rear end

Jean-Philippe Brucker (2):
KVM: arm64: CCA: Expose SVE VL register before VCPU finalization
KVM: arm64: CCA: Control user register access for Realms

Steven Price (3):
KVM: arm64: Avoid including linux/kvm_host.h in kvm_pgtable.h
KVM: arm64: CCA: Support timers in realm RECs
KVM: arm64: CCA: WARN on injected undef exceptions

Suzuki K Poulose (18):
KVM: arm64: protected VM: Handle set_one_reg CNTVCT_EL0/CNTPCT_EL0
KVM: arm64: Disable Steal time accounting for protected guests
KVM: arm64: Include kvm_emulate.h in kvm/arm_psci.h
KVM: arm64: Track the type of VM in kvm_arch
KVM: arm64: Refactor the vcpu_load to allow for VM specific callbacks
KVM: arm64: Add vcpu load/put call backs for flavors
KVM: arm64: Reuse kvm_stage2_unmap_range in kvm_unmap_gfn_range
KVM: arm64: Add VM specific callback for S2 MMU operations
KVM: arm64: Abstract out memory abort handling
KVM: arm64: Use kvm_vm_is_unprotected() for !kvm_vm_is_protected()
KVM: arm64: Widen the scope of "protected" VMs
KVM: arm64: Add a helper for VMs running on hyp that don't trust the
host
KVM: arm64: CCA: Add a new mode for supporting Realm guests
KVM: arm64: CCA: Add VCPU load/put for Realms
KVM: arm64: CCA: Add bare minimal S2 operations for Realm
KVM: arm64: CCA: Introduce Realms
KVM: arm64: CCA: Mandate VGIC_V3 for Realms
KVM: arm64: CCA: Don't expose unsupported capabilities for realm
guests

 .../admin-guide/kernel-parameters.txt         |   3 +
 arch/arm64/include/asm/kvm_emulate.h          |  16 +
 arch/arm64/include/asm/kvm_host.h             |  63 +++-
 arch/arm64/include/asm/kvm_pgtable.h          |   6 +-
 arch/arm64/include/asm/kvm_pkvm.h             |  25 +-
 arch/arm64/include/asm/kvm_rmi.h              |  61 ++++
 arch/arm64/include/asm/virt.h                 |   1 +
 arch/arm64/kvm/Makefile                       |   2 +-
 arch/arm64/kvm/arch_timer.c                   |  37 +-
 arch/arm64/kvm/arm.c                          | 330 +++++++++++++++---
 arch/arm64/kvm/guest.c                        |  73 +++-
 arch/arm64/kvm/handle_exit.c                  |   2 +-
 arch/arm64/kvm/hyp/nvhe/pkvm.c                |  28 +-
 arch/arm64/kvm/hyp/pgtable.c                  |   1 +
 arch/arm64/kvm/hypercalls.c                   |   4 +-
 arch/arm64/kvm/inject_fault.c                 |   1 +
 arch/arm64/kvm/mmu.c                          | 210 ++++++++---
 arch/arm64/kvm/pkvm.c                         |   6 +-
 arch/arm64/kvm/pvtime.c                       |  14 +-
 arch/arm64/kvm/rmi.c                          |  18 +
 arch/arm64/kvm/sys_regs.c                     |  29 +-
 arch/arm64/kvm/vgic/vgic-init.c               |   3 +
 include/kvm/arm_arch_timer.h                  |   3 +-
 include/kvm/arm_psci.h                        |   2 +
 24 files changed, 758 insertions(+), 180 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
#51 lore raw
Quoted 3 lines · Mathieu Poirier

Hi Suzuki,

On Tue, Sep 15, 2026 at 05:01:18PM +0100, Suzuki K Poulose wrote:

[ ... 42 lines skipped ... ]

Quoted 34 lines
Quoted 6 lines

[0] Arm CCA KVM Support v16 : https://lore.kernel.org/all/[email protected]
[1] Linux firmware RMI https://lore.kernel.org/all/[email protected]
[2] Linux GPF Host https://lore.kernel.org/all/[email protected]
[3] https://git.gitlab.arm.com/linux-arm/linux-cca/ cca/cca-host/kvm-v18/integration
[4] https://support.arm.com/documentation/den0137/2-0bet3/

When testing on the FVP model with the above baselines, I get the following error messagaes in a loop, preventing the system from reaching the command line:

[    3.112577] Freeing unused kernel memory: 3520K
[    3.115398] Run /sbin/init as init process
[    3.142926] virtio-pci 0000:00:02.0: swiotlb buffer is full (sz: 4096 bytes),
total 512 (slots), used 511 (slots)
[    3.150111] virtio-pci 0000:00:02.0: swiotlb buffer is full (sz: 4096 bytes),
total 512 (slots), used 511 (slots)
[    3.177190] virtio-pci 0000:00:02.0: swiotlb buffer is full (sz: 4096 bytes),
total 512 (slots), used 511 (slots)
[    3.197194] virtio-pci 0000:00:02.0: swiotlb buffer is full (sz: 4096 bytes),
total 512 (slots), used 511 (slots)
[    3.217188] virtio-pci 0000:00:02.0: swiotlb buffer is full (sz: 4096 bytes),
total 512 (slots), used 511 (slots)
[    3.237194] virtio-pci 0000:00:02.0: swiotlb buffer is full (sz: 4096 bytes),
total 512 (slots), used 511 (slots)
[    3.257181] virtio-pci 0000:00:02.0: swiotlb buffer is full (sz: 4096 bytes),
total 512 (slots), used 511 (slots)
[    3.277189] virtio-pci 0000:00:02.0: swiotlb buffer is full (sz: 4096 bytes),
total 512 (slots), used 511 (slots)
[    3.297193] virtio-pci 0000:00:02.0: swiotlb buffer is full (sz: 4096 bytes),
total 512 (slots), used 511 (slots)
[    3.317188] virtio-pci 0000:00:02.0: swiotlb buffer is full (sz: 4096 bytes),
total 512 (slots), used 511 (slots)
[    8.157185] swiotlb_tbl_map_single: 241 callbacks suppressed
Quoted 3 lines

I get the same result with v17. Have you seen this before? Perhaps a kernel option that needs to be adjusted?

This is due to a change in the swiotlb setup.

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

You can increase the swiotlb size using the swiotlb= kernel command-line option. I also have a patch series in progress to address this issue:

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

-aneesh

#52 lore raw
Quoted 46 lines · Aneesh Kumar K.V
Quoted 3 lines · Mathieu Poirier

Hi Suzuki,

On Tue, Sep 15, 2026 at 05:01:18PM +0100, Suzuki K Poulose wrote:

[ ... 42 lines skipped ... ]

Quoted 34 lines
Quoted 6 lines

[0] Arm CCA KVM Support v16 : https://lore.kernel.org/all/[email protected]
[1] Linux firmware RMI https://lore.kernel.org/all/[email protected]
[2] Linux GPF Host https://lore.kernel.org/all/[email protected]
[3] https://git.gitlab.arm.com/linux-arm/linux-cca/ cca/cca-host/kvm-v18/integration
[4] https://support.arm.com/documentation/den0137/2-0bet3/

When testing on the FVP model with the above baselines, I get the following error messagaes in a loop, preventing the system from reaching the command line:

[    3.112577] Freeing unused kernel memory: 3520K
[    3.115398] Run /sbin/init as init process
[    3.142926] virtio-pci 0000:00:02.0: swiotlb buffer is full (sz: 4096 bytes),
total 512 (slots), used 511 (slots)
[    3.150111] virtio-pci 0000:00:02.0: swiotlb buffer is full (sz: 4096 bytes),
total 512 (slots), used 511 (slots)
[    3.177190] virtio-pci 0000:00:02.0: swiotlb buffer is full (sz: 4096 bytes),
total 512 (slots), used 511 (slots)
[    3.197194] virtio-pci 0000:00:02.0: swiotlb buffer is full (sz: 4096 bytes),
total 512 (slots), used 511 (slots)
[    3.217188] virtio-pci 0000:00:02.0: swiotlb buffer is full (sz: 4096 bytes),
total 512 (slots), used 511 (slots)
[    3.237194] virtio-pci 0000:00:02.0: swiotlb buffer is full (sz: 4096 bytes),
total 512 (slots), used 511 (slots)
[    3.257181] virtio-pci 0000:00:02.0: swiotlb buffer is full (sz: 4096 bytes),
total 512 (slots), used 511 (slots)
[    3.277189] virtio-pci 0000:00:02.0: swiotlb buffer is full (sz: 4096 bytes),
total 512 (slots), used 511 (slots)
[    3.297193] virtio-pci 0000:00:02.0: swiotlb buffer is full (sz: 4096 bytes),
total 512 (slots), used 511 (slots)
[    3.317188] virtio-pci 0000:00:02.0: swiotlb buffer is full (sz: 4096 bytes),
total 512 (slots), used 511 (slots)
[    8.157185] swiotlb_tbl_map_single: 241 callbacks suppressed
Quoted 3 lines

I get the same result with v17. Have you seen this before? Perhaps a kernel option that needs to be adjusted?

Quoted 5 lines

This is due to a change in the swiotlb setup.

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

You can increase the swiotlb size using the swiotlb= kernel command-line

What size do you recommend setting it at?

Quoted 6 lines

option. I also have a patch series in progress to address this issue:

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

-aneesh

#53 lore raw
In reply to Suzuki K Poulose
Quoted 4 lines · Suzuki K Poulose

This series is a trimmed down version of the Arm CCA KVM support, previously posted here [0]. Like in the v17, we have tried to split the entire series into the following chunks.

FWIW, this boots and runs guests on VHE, nVHE and pKVM (protected and non-protected) under QEMU, and on an M4; arch_timer and get-reg-list pass. Didn't try it on FVP.

Cheers,
/fuad

Quoted 149 lines

1) Base RMM RMI support under drivers/firmware/arm_rmm -> [1]
2) Linux Host support for handling GPFs - [2]
3) NEW: Enlighten KVM arm64 about the different VM types and use call backs for the VM type, rather than spilling the is_this_type_of_vm() everywhere. Adds VCPU and Stage2 MMU related callbacks with support for the existing VM types. There are other places where we may be able to abstract, but those need careful performance evaluations to make sure they are fit (e.g., vcpu_run)

Later in the series, we generalise the predicate "kvm_vm_is_protected()" to cover all "Confidential" VMs (which includes Protected VM and Realms), which allows us to handle common themes without having to do things like :

     if (kvm_vm_is_protected() || kvm_vm_is_realm()),
     instead:
     if (kvm_vm_is_confidential())

Also replaces the code with precise check for a given VM type to avoiding combination of if (). e.g,, kvm_vm_is_unprotected_pkvm(kvm). The checks under arch/arm64/kvm/{nvhe,pkvm} still retain the vm_is_protected() check as pVMs are the only possible protected VMs there.

4) Bare minimal Realm VM support without the actual functionality to run a Realm. This would help the maintainers to review the series in smaller chunks. This doesn't depend on [1] and can be independently merged, without being "functional".
This series includes vcpu operations and the s2 vm operations, which do need the RMI driver backend to be meaningful. But the KVM handler is in the right shape. The remaining changes would be added once the RMI firmware library lands.
5) Core implementation of the RMI driver for KVM and actual enablement of the Realm support. This depends on (1), (2) and the guest-memfd-in-place conversion series v12 from Ackerley. This is available here at the integration branch [3]

This series is comprised of (3) and (4) above.

The integration branch has been tested with the following components:
tf-RMM: main branch (commit 5e6e2acd) compliant to RMM-v2.0-beta3 [4]
kvmtool: [email protected]:linux-arm/kvmtool-cca.git cca/kvm-v18

[0] Arm CCA KVM Support v16 : https://lore.kernel.org/all/[email protected]
[1] Linux firmware RMI https://lore.kernel.org/all/[email protected]
[2] Linux GPF Host https://lore.kernel.org/all/[email protected]
[3] https://git.gitlab.arm.com/linux-arm/linux-cca/ cca/cca-host/kvm-v18/integration
[4] https://support.arm.com/documentation/den0137/2-0bet3/

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

- Add a patch to fix pKVM handling of SYS_CNTVCT/CNTPCT to override the counter offset (Patch1)
- Restrict Realms to VGIC v3 only - New patch
- Add kvm_vm_is_unprotected() to replace is_protected_kvm_enabled() && !kvm_vm_is_protected() - New patch
- Use macro to initialize the per-flavor vcpu, s2_vm ops
- Add a wrapper to initialise vcpu and s2_vm ops with a BUILD_BUG_ON() for the array size checks against VM flavour types
- Drop forward decalaration of the vcpu, s2_vm operations that spoiled the fun ;-)
- Remove irrelevant comment about the order of timer loading for !VHE
- Use the explicti kvm_call_hyp_nvhe for pKVM specific ops
- Don't call nvhe_vcpu_put from pkvm_vcpu_put, open code them
- Drop cpu argument for vcpu_load() callback. We set the cpu
before the callbacks are invoked
- Drop kvm_vm_is_confidential(), instead widen the scope of kvm_vm_is_protected() to cover pVMs and Realms. Add an explicit helper kvm_vm_is_protected_pkvm() for the cases where we need to check for a "pVM on pKVM"
- Add kvm_vm_hyp_is_pkvm() for checking if the VM is running on pKVM. covers both unprotected and pvms. But really uses is_protected_kvm_enabled() under the hood
- Add kvm_vm_hyp_is_distrusting() to cover pKVM guests (both protected and unprotected) and Realms. Use this for preventing the vgic v2 mapping into Stage2 for a guest
- Drop superfluous !kvm check from kvm_vm_ioctl_enable_cap() - Sashiko
- Drop KVM_CAP_CREATE_IRQCHIP, as we don't support VGIC_V2 for Realms
- Filter out the vm_ioctls that are based on blocked cap.
- Repurpose the pkvm plumbing for filtering the caps and ioctl to generic and plumb the Realm support in
- s/PKVM/pKVM for the comments
- Drop type argument for pkvm_init_host_vm and also drop protected variable, now that we have the vm_flavor to check.
- Use kvm_vm_hyp_is_pkvm() to replace is_protected_kvm_enabled() with valid kvm instance
- CCA: Merge the GET/SET REG handling patches into a single patch
- CCA: Reword the commit description for SVE VL access handling
- Reordered the patches to group the Realm realted to changes to the rear end

Jean-Philippe Brucker (2):
KVM: arm64: CCA: Expose SVE VL register before VCPU finalization
KVM: arm64: CCA: Control user register access for Realms

Steven Price (3):
KVM: arm64: Avoid including linux/kvm_host.h in kvm_pgtable.h
KVM: arm64: CCA: Support timers in realm RECs
KVM: arm64: CCA: WARN on injected undef exceptions

Suzuki K Poulose (18):
KVM: arm64: protected VM: Handle set_one_reg CNTVCT_EL0/CNTPCT_EL0
KVM: arm64: Disable Steal time accounting for protected guests
KVM: arm64: Include kvm_emulate.h in kvm/arm_psci.h
KVM: arm64: Track the type of VM in kvm_arch
KVM: arm64: Refactor the vcpu_load to allow for VM specific callbacks
KVM: arm64: Add vcpu load/put call backs for flavors
KVM: arm64: Reuse kvm_stage2_unmap_range in kvm_unmap_gfn_range
KVM: arm64: Add VM specific callback for S2 MMU operations
KVM: arm64: Abstract out memory abort handling
KVM: arm64: Use kvm_vm_is_unprotected() for !kvm_vm_is_protected()
KVM: arm64: Widen the scope of "protected" VMs
KVM: arm64: Add a helper for VMs running on hyp that don't trust the
host
KVM: arm64: CCA: Add a new mode for supporting Realm guests
KVM: arm64: CCA: Add VCPU load/put for Realms
KVM: arm64: CCA: Add bare minimal S2 operations for Realm
KVM: arm64: CCA: Introduce Realms
KVM: arm64: CCA: Mandate VGIC_V3 for Realms
KVM: arm64: CCA: Don't expose unsupported capabilities for realm
guests

 .../admin-guide/kernel-parameters.txt         |   3 +
 arch/arm64/include/asm/kvm_emulate.h          |  16 +
 arch/arm64/include/asm/kvm_host.h             |  63 +++-
 arch/arm64/include/asm/kvm_pgtable.h          |   6 +-
 arch/arm64/include/asm/kvm_pkvm.h             |  25 +-
 arch/arm64/include/asm/kvm_rmi.h              |  61 ++++
 arch/arm64/include/asm/virt.h                 |   1 +
 arch/arm64/kvm/Makefile                       |   2 +-
 arch/arm64/kvm/arch_timer.c                   |  37 +-
 arch/arm64/kvm/arm.c                          | 330 +++++++++++++++---
 arch/arm64/kvm/guest.c                        |  73 +++-
 arch/arm64/kvm/handle_exit.c                  |   2 +-
 arch/arm64/kvm/hyp/nvhe/pkvm.c                |  28 +-
 arch/arm64/kvm/hyp/pgtable.c                  |   1 +
 arch/arm64/kvm/hypercalls.c                   |   4 +-
 arch/arm64/kvm/inject_fault.c                 |   1 +
 arch/arm64/kvm/mmu.c                          | 210 ++++++++---
 arch/arm64/kvm/pkvm.c                         |   6 +-
 arch/arm64/kvm/pvtime.c                       |  14 +-
 arch/arm64/kvm/rmi.c                          |  18 +
 arch/arm64/kvm/sys_regs.c                     |  29 +-
 arch/arm64/kvm/vgic/vgic-init.c               |   3 +
 include/kvm/arm_arch_timer.h                  |   3 +-
 include/kvm/arm_psci.h                        |   2 +
 24 files changed, 758 insertions(+), 180 deletions(-)
 create mode 100644 arch/arm64/include/asm/kvm_rmi.h
 create mode 100644 arch/arm64/kvm/rmi.c

--
2.43.0