Skip to contentSkip to conversation

KVM: arm64: CCA: Add basic plumbing for Realms

PATCH Superseded Suzuki K Poulose 4 participants 77 messages Sep 20 – Sep 28

Subsystems (4)

v19 of 23 Superseded Latest is v23 →

lore
Share & tools
lore

Suzuki K Poulose

author reviewer
#0 lore raw

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

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

     if (kvm_vm_is_protected() || kvm_vm_is_realm())

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. Also covers the SET_ONE_REG/GET_ONE_REG

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-v19/integration
[4] https://support.arm.com/documentation/den0137/2-0bet3/

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

- Patch count down by 3, after merging different patches together, see more below
- Retain NULL vm_offset for pVMs and move the counter offset flag initialisation to kvm_timer_init_vm() - Marc
- Merge widening the scope of kvm_vm_is_protected() to the patch where the flavors are introduced(Marc) and also dropped Fuad's reviewed-by, as the patch is now bigger.
- Merge "Use kvm_vm_is_unprotected_pkvm" for !kvm_vm_is_protected to patch where flavor is introuced.
- Merge "vgic-v3" mandate and preventing vgic-v2 mappings to a single patch, where kvm_vm_hyp_is_distrusting() introduced - Fuad
- Drop kvm_vm_hyp_is_pkvm(), reverting to is_protected_kvm_enabled()
- Use is_protected_kvm_enabeld() for pKVM guest flavor checks.
- s/PKVM/pKVM for commit descriptions too
- Make sure the vm_mem_abort callback is !NULL at init time.
- Bail out early for !pKVM && !Realm VMs in kvm_vm_ioctl_allowed(). Use kvm_vm_hyp_is_distrusting()
- WARN_ON_ONCE(!kvm) for kvm_vm_ioctl_allowed() as it must be only called with a valid kvm instance and only from kvm_arch_vm_ioctl()
- Rename kvm_arch_vm_{ext,ioctl}_allowed => kvm_vm_{ext,ioctl}_allowed - Fuad
- Move kvm_realm_ext_allowed() to asm/kvm_rmi.h - Fuad
- Don't expose PMCR_EL0 to the userspace until we support PMU

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 (4):
KVM: arm64: Avoid including linux/kvm_host.h in kvm_pgtable.h
KVM: arm64: CCA: Introduce Realms
KVM: arm64: CCA: WARN on injected undef exceptions
KVM: arm64: CCA: Support timers in realm RECs

Suzuki K Poulose (14):
KVM: arm64: protected VM: Handle user writes to 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: Mandate VGIC v3 for 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: 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             |  69 +++-
 arch/arm64/include/asm/kvm_pgtable.h          |   6 +-
 arch/arm64/include/asm/kvm_pkvm.h             |  25 +-
 arch/arm64/include/asm/kvm_rmi.h              |  84 +++++
 arch/arm64/include/asm/virt.h                 |   1 +
 arch/arm64/kvm/Makefile                       |   2 +-
 arch/arm64/kvm/arch_timer.c                   |  34 +-
 arch/arm64/kvm/arm.c                          | 300 +++++++++++++++---
 arch/arm64/kvm/guest.c                        |  73 ++++-
 arch/arm64/kvm/handle_exit.c                  |   2 +-
 arch/arm64/kvm/hyp/nvhe/pkvm.c                |   6 +-
 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                     |  28 +-
 arch/arm64/kvm/vgic/vgic-init.c               |   2 +
 include/kvm/arm_psci.h                        |   2 +
 23 files changed, 752 insertions(+), 155 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 v19 1/20 KVM: arm64: protected VM: Handle user writes to 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 KVM init time for protected VMs. A userspace writing to the CNT*CT_EL0 would observe success, without any real effect. This was chosen over preventing the writes to these registers and returning -EPERM.

Reported by Sashiko

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

---
Changes since v18:
- Retain NULL vm_offset for protected VMs to avoid host tampering with the offset.
- Moved the flag setting into kvm_timer_init_vm(), where it should have been in the first place

---
 arch/arm64/kvm/arch_timer.c | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)
arch/arm64/kvm/arch_timer.c +10 -2
index 6ac3321f4c575..226cd5a495c8b 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]);
+
+	/*
+	 * 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.
+	 */
+	if (kvm_vm_is_protected(kvm))
+		set_bit(KVM_ARCH_FLAG_VM_COUNTER_OFFSET, &kvm->arch.flags);
 }
 
 void kvm_timer_cpu_up(void)
Signature · 1 line
2.43.0
author reviewer
#7 lore raw

PATCH v19 2/20 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 8b080804bc90b..db36815630790 100644
@@ -447,7 +447,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
#12 lore raw

PATCH v19 3/20 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
#14 lore raw

PATCH v19 4/20 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
#17 lore raw

PATCH v19 5/20 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.

With that, add wrappers for checking the "type" of a VM and replace the existing users with the new wrappers.

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.

Adds __VM_PROTECTED marker vm flavor to generalize kvm_vm_is_protected() to predicate all confidential guests running on KVM. In later patches, we would add Realm VMs, which would also be classified as protected.

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. To tighten this init_pkvm_hyp_vm() restricts the hyp copy of the vm_flavor to the ones it supports.

---
Changes since v18:
- Merge the __VM_PROTECTED marker and the widening of kvm_vm_is_protected() to this patch.
- Merge the use of kvm_vm_is_unprotected_pkvm() for !kvm_vm_is_protected() given the scope changes here.
- Drop Fuad's review tag, as this patch has multiple merges
- Restrict the VM flavors to the supported types in init_pkvm_hyp_vm().
- Drop kvm_vm_hyp_is_pkvm() and revert to is_protected_kvm_enabled()
- Use is_protected_kvm_enabled() to make the pKVM guest flavor checks.
- s/PKVM/pKVM for commit descriptions too

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 | 22 +++++++++++++++++++---
 arch/arm64/include/asm/kvm_pkvm.h |  4 ++--
 arch/arm64/kvm/arm.c              | 31 ++++++++++++++++++++++++++-----
 arch/arm64/kvm/handle_exit.c      |  2 +-
 arch/arm64/kvm/hyp/nvhe/pkvm.c    |  6 +++++-
 arch/arm64/kvm/mmu.c              |  2 +-
 arch/arm64/kvm/pkvm.c             |  6 ++----
 7 files changed, 56 insertions(+), 17 deletions(-)
arch/arm64/include/asm/kvm_host.h +19 -3
index 286489a69dff5..9b1cf9c59e81f 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,19 @@ enum fgt_group_id {
 	__NR_FGT_GROUP_IDS__
 };
 
+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,
+};
+
 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,10 +1513,17 @@ 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)
 #define vcpu_is_protected(vcpu)		kvm_vm_is_protected((vcpu)->kvm)
 
+#define kvm_vm_is_protected_pkvm(kvm)		\
+	(is_protected_kvm_enabled() && ((kvm)->arch.vm_flavor == VM_PROTECTED_PKVM))
+#define vcpu_is_protected_pkvm(vcpu)	kvm_vm_is_protected_pkvm(vcpu->kvm)
+
+#define kvm_vm_is_unprotected_pkvm(kvm)		\
+	(is_protected_kvm_enabled() && ((kvm)->arch.vm_flavor == VM_PKVM))
+
+
 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_pkvm.h +2 -2
index 54a618d887fa4..e4ea80711bec6 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);
@@ -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 +26 -5
index db36815630790..8c784b266a8e8 100644
@@ -214,6 +214,26 @@ static int kvm_arm_default_max_vcpus(void)
 	return vgic_present ? kvm_vgic_get_max_vcpus() : KVM_MAX_VCPUS;
 }
 
+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
@@ -236,6 +256,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);
@@ -257,12 +281,9 @@ int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)
 		 * 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);
@@ -985,7 +1006,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;
 
 	/*
arch/arm64/kvm/hyp/nvhe/pkvm.c +5 -1
index 459bd9eb7e4bc..57e2eef6d7426 100644
@@ -432,7 +432,11 @@ 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);
+	if (kvm_vm_is_protected(host_kvm))
+		hyp_vm->kvm.arch.vm_flavor = VM_PROTECTED_PKVM;
+	else
+		hyp_vm->kvm.arch.vm_flavor = VM_PKVM;
+
 	hyp_vm->kvm.arch.flags = 0;
 	pkvm_init_features_from_host(hyp_vm, host_kvm);
 
arch/arm64/kvm/mmu.c +1 -1
index 9ba86450fe4af..0f4e8b71fa85d 100644
@@ -2624,7 +2624,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)) {
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
author reviewer
#32 lore raw

PATCH v19 6/20 KVM: arm64: Refactor the vcpu_load to allow for VM specific callbacks

In reply to Suzuki K Poulose

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. Based on a work by Marc Zyngier.

---
 arch/arm64/kvm/arm.c | 48 ++++++++++++++++++++++++++------------------
 1 file changed, 29 insertions(+), 19 deletions(-)
arch/arm64/kvm/arm.c +29 -19
index 8c784b266a8e8..c74706ed9a531 100644
@@ -683,14 +683,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);
 
@@ -720,10 +717,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).
@@ -736,19 +756,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()) {
@@ -772,7 +782,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
#35 lore raw

PATCH v19 7/20 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 9b1cf9c59e81f..149f4582c8b6a 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;
 
@@ -855,6 +860,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
#37 lore raw

PATCH v19 8/20 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 0f4e8b71fa85d..03f2017a7404a 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
#40 lore raw

PATCH v19 9/20 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.

---
 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 149f4582c8b6a..7664d8b8cce5a 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;
 
@@ -332,6 +345,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
#49 lore raw

PATCH v19 10/20 KVM: arm64: Abstract out memory abort handling

In reply to Suzuki K Poulose

Move the memory abort handling under VM specific s2 operation.

---
Changes since v18:
- Ensure vm_mem_abort handler is always !NULL

---
 arch/arm64/include/asm/kvm_host.h |  2 ++
 arch/arm64/kvm/mmu.c              | 38 +++++++++++++++++++------------
 2 files changed, 25 insertions(+), 15 deletions(-)
arch/arm64/include/asm/kvm_host.h +2 -0
index 7664d8b8cce5a..3211543a85b38 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 +23 -15
index d97a4a1bca23f..cf293d09e940a 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)		\
@@ -2887,7 +2895,7 @@ static int kvm_vm_init_vm_s2_ops(struct kvm *kvm)
 	BUILD_BUG_ON(ARRAY_SIZE(arm64_vm_s2_ops) != VM_FLAVOR_MAX);
 
 	kvm->arch.vm_s2_ops = arm64_vm_s2_ops[kvm->arch.vm_flavor];
-	if (WARN_ON(!kvm->arch.vm_s2_ops))
+	if (WARN_ON(!kvm->arch.vm_s2_ops || !kvm->arch.vm_s2_ops->vm_mem_abort))
 		return -EINVAL;
 	return 0;
 }
Signature · 1 line
2.43.0
author reviewer
#52 lore raw

PATCH v19 11/20 KVM: arm64: Mandate VGIC v3 for 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 and prevent creation of VGIC other than v3.

---
Changes since v18:
- Cover pKVM guests for VGIC v3 mandate.
- Merge the VGIC mandate check in here.

---
 arch/arm64/include/asm/kvm_host.h | 3 +++
 arch/arm64/kvm/mmu.c              | 2 +-
 arch/arm64/kvm/vgic/vgic-init.c   | 2 ++
 3 files changed, 6 insertions(+), 1 deletion(-)
arch/arm64/include/asm/kvm_host.h +3 -0
index 3211543a85b38..bb156a633ab75 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 */
@@ -1546,6 +1548,7 @@ struct kvm *kvm_arch_alloc_vm(void);
 #define kvm_vm_is_unprotected_pkvm(kvm)		\
 	(is_protected_kvm_enabled() && ((kvm)->arch.vm_flavor == VM_PKVM))
 
+#define kvm_vm_hyp_is_distrusting(kvm)	((kvm)->arch.vm_flavor >= __VM_DISTRUSTING_HYP)
 
 int kvm_arm_vcpu_finalize(struct kvm_vcpu *vcpu, int feature);
 bool kvm_arm_vcpu_is_finalized(struct kvm_vcpu *vcpu);
arch/arm64/kvm/mmu.c +1 -1
index cf293d09e940a..413c4b114d75d 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);
arch/arm64/kvm/vgic/vgic-init.c +2 -0
index 4012df6002ea6..874025513afcc 100644
@@ -84,6 +84,8 @@ int kvm_vgic_create(struct kvm *kvm, u32 type)
 		!kvm_vgic_global_state.can_emulate_gicv2)
 		return -ENODEV;
 
+	if (kvm_vm_hyp_is_distrusting(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
author reviewer
#56 lore raw

PATCH v19 12/20 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 bb156a633ab75..ecbf5a35cd59e 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 9b977dc734220..0e2ab5311e4d8 100644
@@ -3268,6 +3268,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
#58 lore raw

PATCH v19 13/20 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 0e2ab5311e4d8..bcc274df2a915 100644
@@ -795,6 +795,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;
@@ -845,6 +852,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);
@@ -2202,6 +2215,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
#60 lore raw

PATCH v19 14/20 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 413c4b114d75d..a38459f6b456e 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
#62 lore raw

PATCH v19 15/20 KVM: arm64: CCA: Introduce Realms

In reply to Suzuki K Poulose

From: Steven Price <[email protected]>

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 ecbf5a35cd59e..6d12e2c360af6 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 */
@@ -1549,6 +1554,9 @@ struct kvm *kvm_arch_alloc_vm(void);
 #define kvm_vm_is_unprotected_pkvm(kvm)		\
 	(is_protected_kvm_enabled() && ((kvm)->arch.vm_flavor == VM_PKVM))
 
+#define kvm_vm_is_realm(kvm)		((kvm)->arch.vm_flavor == VM_REALM)
+#define vcpu_is_rec(vcpu)		kvm_vm_is_realm((vcpu)->kvm)
+
 #define kvm_vm_hyp_is_distrusting(kvm)	((kvm)->arch.vm_flavor >= __VM_DISTRUSTING_HYP)
 
 int kvm_arm_vcpu_finalize(struct kvm_vcpu *vcpu, int feature);
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 bcc274df2a915..86e705330d7bd 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);
@@ -2228,6 +2231,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)
@@ -3181,6 +3185,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 a38459f6b456e..39ee27ba9d493 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
#65 lore raw

PATCH v19 16/20 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_vm_ioctl_allowed() and move it into arch/arm64/kvm/arm.c. Also add a generic kvm_vm_ext_allowed() to handle pKVM and Realm capability filtering and route them accordingly.

---
Changes since v18:
* Bail out early for !pKVM && !Realm vms. Use kvm_vm_hyp_is_distrusting()
* WARN_ON_ONCE(!kvm), we are only called from kvm_arch_vm_ioctl() with a valid kvm instance.
* Move the kvm_realm_ext_allowed() to asm/kvm_rmi.h - Fuad
* Rename kvm_arch_vm_*allowed => kvm_vm_*_allowed - Fuad
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/include/asm/kvm_rmi.h  | 23 +++++++++++++++
 arch/arm64/kvm/arm.c              | 49 +++++++++++++++++++++++++++++--
 3 files changed, 69 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/include/asm/kvm_rmi.h +23 -0
index 44f5c75a27b5b..6b8b9ee9ea245 100644
@@ -6,6 +6,8 @@
 #ifndef __ASM_KVM_RMI_H
 #define __ASM_KVM_RMI_H
 
+#include <linux/kvm.h>
+
 /**
  * enum realm_state - State of a Realm
  *
@@ -58,4 +60,25 @@ struct realm {
 
 void kvm_init_rmi(void);
 
+static inline bool kvm_realm_ext_allowed(long ext)
+{
+	switch (ext) {
+	case KVM_CAP_IRQCHIP:
+	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;
+	}
+	return false;
+}
+
 #endif /* __ASM_KVM_RMI_H */
arch/arm64/kvm/arm.c +46 -3

73 lines not on this page · raw message

Signature · 1 line
2.43.0
author reviewer
#67 lore raw

PATCH v19 17/20 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
#70 lore raw

PATCH v19 18/20 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 making 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 226cd5a495c8b..83c13bbdcc777 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,
@@ -1617,8 +1631,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
#71 lore raw

PATCH v19 19/20 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 eab8543a4194d..6a52b102d0ce4 100644
@@ -1993,6 +1993,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)
 {
@@ -2048,7 +2061,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
#73 lore raw

PATCH v19 20/20 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 :w
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 parameters. We don't support PMUs for the Realm VMs yet, so PMCR is not exposed.

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 v18:
- Don't expose PMCR_EL0 to the userspace now, we could do that when we support PMU
- Fix check patch warnings
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   | 28 +++++++++++++----
 3 files changed, 87 insertions(+), 8 deletions(-)
arch/arm64/kvm/guest.c +63 -0
index c3ca369882273..ffe3f5ce4b3cf 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);
+	}
+
+	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 +22 -6
index 44aae52c473d7..47ebde943a09e 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,27 @@ 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:
+		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 +5751,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 +5764,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
#15 lore raw

Re: [PATCH v19 04/20] KVM: arm64: Avoid including linux/kvm_host.h in kvm_pgtable.h

In reply to Suzuki K Poulose
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Low] kvm: missing includes in kvm_pgtable.h for FIELD_GET and others --

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Low] kvm: missing includes in kvm_pgtable.h for FIELD_GET and others
--

commit 0c7ef6102444f8add04f2e7e4854a331850bf438
Author: Steven Price <[email protected]>

KVM: arm64: Avoid including linux/kvm_host.h in kvm_pgtable.h

This commit drops the linux/kvm_host.h include from kvm_pgtable.h to avoid future include cycles. It replaces it with lightweight headers for types and a forward declaration for struct kvm_s2_mmu, while making the dependency on kvm_host.h explicit in pgtable.c and kvm_pkvm.h.

arch/arm64/include/asm/kvm_pgtable.h line 14

Quoted 13 lines
arch/arm64/include/asm/kvm_pgtable.h +3 -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>

[Severity: Low]
Does removing linux/kvm_host.h leave some macros used later in this header without their explicit includes?

For example, in kvm_get_parange_max(), the code relies on PAGE_SHIFT:

    if (kvm_lpa2_is_enabled() ||
           (IS_ENABLED(CONFIG_ARM64_PA_BITS_52) && PAGE_SHIFT == 16))

In kvm_pte_to_phys(), the code relies on FIELD_GET:

    pa |= FIELD_GET(KVM_PTE_ADDR_51_50_LPA2, pte) << 50;

And in kvm_pte_to_pfn(), it uses __phys_to_pfn:

    return __phys_to_pfn(kvm_pte_to_phys(pte));

Should this file explicitly include <linux/bitfield.h>, <asm/page.h>, and <asm/memory.h> to prevent build fragilities when it is included in an isolated compilation unit?

[ ... ]

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

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

In reply to Suzuki K Poulose
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] kvm: arm64: missing READ_ONCE() when reading untrusted host_kvm vm_flavor at EL2 --

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] kvm: arm64: missing READ_ONCE() when reading untrusted host_kvm vm_flavor at EL2 --

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

KVM: arm64: Track the type of VM in kvm_arch

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.

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

Quoted 13 lines
arch/arm64/kvm/hyp/nvhe/pkvm.c +4 -1
index 459bd9eb7e4bc..57e2eef6d7426 100644
@@ -432,7 +432,11 @@ 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);
+	if (kvm_vm_is_protected(host_kvm))
+		hyp_vm->kvm.arch.vm_flavor = VM_PROTECTED_PKVM;
+	else
+		hyp_vm->kvm.arch.vm_flavor = VM_PKVM;

[Severity: Medium]
Does init_pkvm_hyp_vm() evaluate the untrusted host memory directly without READ_ONCE()?

The pKVM EL2 trust boundary requires that data from host memory must be copied to private EL2 memory before validating or acting on it.

The macro kvm_vm_is_protected(host_kvm) evaluates host_kvm->arch.vm_flavor in the condition. The previous code safely used READ_ONCE(host_kvm->arch.pkvm.is_protected).

Could we wrap the host memory read with READ_ONCE() to prevent compiler-induced tearing or optimization issues?

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

Re: [PATCH v19 02/20] KVM: arm64: Disable Steal time accounting for protected guests

In reply to Suzuki K Poulose
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] arm64: kvm: contradictory SMCCC advertisement for PV_TIME on protected VMs --

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] arm64: kvm: contradictory SMCCC advertisement for PV_TIME on protected VMs --

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

KVM: arm64: Disable Steal time accounting for protected guests

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.

Quoted 5 lines
arch/arm64/kvm/pvtime.c +0 -0
index 4ceabaa4c30bd..579e0a4720ad2 100644

arch/arm64/kvm/pvtime.c gpa_t kvm_init_stolen_time(struct kvm_vcpu *vcpu) · line 73

Quoted 9 lines
arch/arm64/kvm/pvtime.c +2 -2
@@ -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));
 }

[Severity: Medium]
Does this create a contradictory SMCCC advertisement for protected VMs?

A protected guest querying ARM_SMCCC_ARCH_FEATURES_FUNC_ID for ARM_SMCCC_HV_PV_TIME_FEATURES in kvm_smccc_call_handler() will receive SMCCC_RET_SUCCESS because the KVM_REG_ARM_STD_HYP_BIT_PV_TIME bit is still set in std_hyp_bmap:

arch/arm64/kvm/hypercalls.c:kvm_smccc_call_handler() {
	...
	case ARM_SMCCC_HV_PV_TIME_FEATURES:
		if (test_bit(KVM_REG_ARM_STD_HYP_BIT_PV_TIME,
			     &smccc_feat->std_hyp_bmap))
			val[0] = SMCCC_RET_SUCCESS;
		break;
	...
}

However, when the guest actually invokes the feature, kvm_hypercall_pv_features() returns SMCCC_RET_NOT_SUPPORTED because the IPA is unconfigured:

arch/arm64/kvm/pvtime.c:kvm_hypercall_pv_features() {
	...
	case ARM_SMCCC_HV_PV_TIME_FEATURES:
	case ARM_SMCCC_HV_PV_TIME_ST:
		if (vcpu->arch.steal.base != INVALID_GPA)
			val = SMCCC_RET_SUCCESS;
		break;
	...
}

This violates the SMCCC ABI by advertising ARM_SMCCC_HV_PV_TIME_FEATURES as supported but returning NOT_SUPPORTED when it is invoked. Should the KVM_REG_ARM_STD_HYP_BIT_PV_TIME bit be cleared for protected VMs?

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

Re: [PATCH v19 02/20] KVM: arm64: Disable Steal time accounting for protected guests

Quoted 33 lines · On 20/09/2026 22:44, [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] arm64: kvm: contradictory SMCCC advertisement for PV_TIME on protected VMs --

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

KVM: arm64: Disable Steal time accounting for protected guests

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.

Quoted 5 lines
arch/arm64/kvm/pvtime.c +0 -0
index 4ceabaa4c30bd..579e0a4720ad2 100644

arch/arm64/kvm/pvtime.c gpa_t kvm_init_stolen_time(struct kvm_vcpu *vcpu) · line 73

Quoted 9 lines
arch/arm64/kvm/pvtime.c +2 -2
@@ -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));
  }

[Severity: Medium]
Does this create a contradictory SMCCC advertisement for protected VMs?

Quoted 4 lines

A protected guest querying ARM_SMCCC_ARCH_FEATURES_FUNC_ID for ARM_SMCCC_HV_PV_TIME_FEATURES in kvm_smccc_call_handler() will receive SMCCC_RET_SUCCESS because the KVM_REG_ARM_STD_HYP_BIT_PV_TIME bit is still set in std_hyp_bmap:

This looks like an existing issue in the code. I have something like this to fix this :

arch/arm64/kvm/hypercalls.c +6 -0
index b11b8821c9fbc..6fabcd8aaacb9 100644
@@ -402,6 +402,9 @@ void kvm_arm_init_hypercalls(struct kvm *kvm)
 
        smccc_feat->std_bmap = KVM_ARM_SMCCC_STD_FEATURES;
        smccc_feat->std_hyp_bmap = KVM_ARM_SMCCC_STD_HYP_FEATURES;
+       /* Clear PV_TIME support for protected VMs */
+       if (kvm_vm_is_protected(kvm))
+               clear_bit(KVM_REG_ARM_STD_HYP_BIT_PV_TIME, &smccc_feat->std_hyp_bmap);
        smccc_feat->vendor_hyp_bmap = KVM_ARM_SMCCC_VENDOR_HYP_FEATURES;
 
        mt_init(&kvm->arch.smccc_filter);
@@ -532,6 +535,9 @@ static int kvm_arm_set_fw_reg_bmap(struct kvm_vcpu *vcpu, u64 reg_id, u64 val)
        case KVM_REG_ARM_STD_HYP_BMAP:
                fw_reg_bmap = &smccc_feat->std_hyp_bmap;
                fw_reg_features = KVM_ARM_SMCCC_STD_HYP_FEATURES;
+               /* PV_TIME is not supported for protected VMs */
+               if (vcpu_is_protected(vcpu))
+                       clear_bit(KVM_REG_ARM_STD_HYP_BIT_PV_TIME, &fw_reg_features);
                break;
        case KVM_REG_ARM_VENDOR_HYP_BMAP:
                fw_reg_bmap = &smccc_feat->vendor_hyp_bmap;

Suzuki

Quoted 29 lines
arch/arm64/kvm/hypercalls.c:kvm_smccc_call_handler() {
	...
	case ARM_SMCCC_HV_PV_TIME_FEATURES:
		if (test_bit(KVM_REG_ARM_STD_HYP_BIT_PV_TIME,
			     &smccc_feat->std_hyp_bmap))
			val[0] = SMCCC_RET_SUCCESS;
		break;
	...
}

However, when the guest actually invokes the feature, kvm_hypercall_pv_features() returns SMCCC_RET_NOT_SUPPORTED because the IPA is unconfigured:

arch/arm64/kvm/pvtime.c:kvm_hypercall_pv_features() {
	...
	case ARM_SMCCC_HV_PV_TIME_FEATURES:
	case ARM_SMCCC_HV_PV_TIME_ST:
		if (vcpu->arch.steal.base != INVALID_GPA)
			val = SMCCC_RET_SUCCESS;
		break;
	...
}

This violates the SMCCC ABI by advertising ARM_SMCCC_HV_PV_TIME_FEATURES as supported but returning NOT_SUPPORTED when it is invoked. Should the KVM_REG_ARM_STD_HYP_BIT_PV_TIME bit be cleared for protected VMs?

author reviewer
#19 lore raw

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

In reply to [email protected]
Quoted 35 lines · On 20/09/2026 22:38, [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] kvm: arm64: missing READ_ONCE() when reading untrusted host_kvm vm_flavor at EL2 --

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

KVM: arm64: Track the type of VM in kvm_arch

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.

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

Quoted 13 lines
arch/arm64/kvm/hyp/nvhe/pkvm.c +4 -1
index 459bd9eb7e4bc..57e2eef6d7426 100644
@@ -432,7 +432,11 @@ 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);
+	if (kvm_vm_is_protected(host_kvm))
+		hyp_vm->kvm.arch.vm_flavor = VM_PROTECTED_PKVM;
+	else
+		hyp_vm->kvm.arch.vm_flavor = VM_PKVM;

[Severity: Medium]
Does init_pkvm_hyp_vm() evaluate the untrusted host memory directly without READ_ONCE()?

The pKVM EL2 trust boundary requires that data from host memory must be copied to private EL2 memory before validating or acting on it.

Quoted 6 lines

The macro kvm_vm_is_protected(host_kvm) evaluates host_kvm->arch.vm_flavor in the condition. The previous code safely used READ_ONCE(host_kvm->arch.pkvm.is_protected).

Could we wrap the host memory read with READ_ONCE() to prevent compiler-induced tearing or optimization issues?

Fair point. I have changed the above hunk to :

- hyp_vm->kvm.arch.pkvm.is_protected = READ_ONCE(host_kvm->arch.pkvm.is_protected);
+ if (READ_ONCE(host_kvm->arch.vm_flavor) == VM_PROTECTED_PKVM)
+ hyp_vm->kvm.arch.vm_flavor = VM_PROTECTED_PKVM;
+ else
+ hyp_vm->kvm.arch.vm_flavor = VM_PKVM;
+

Cheers
Suzuki

Quoted 1 line
author reviewer
#16 lore raw

Re: [PATCH v19 04/20] KVM: arm64: Avoid including linux/kvm_host.h in kvm_pgtable.h

In reply to [email protected]
Quoted 41 lines · On 20/09/2026 22:38, [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Low] kvm: missing includes in kvm_pgtable.h for FIELD_GET and others
--

commit 0c7ef6102444f8add04f2e7e4854a331850bf438
Author: Steven Price <[email protected]>

KVM: arm64: Avoid including linux/kvm_host.h in kvm_pgtable.h

This commit drops the linux/kvm_host.h include from kvm_pgtable.h to avoid future include cycles. It replaces it with lightweight headers for types and a forward declaration for struct kvm_s2_mmu, while making the dependency on kvm_host.h explicit in pgtable.c and kvm_pkvm.h.

arch/arm64/include/asm/kvm_pgtable.h line 14

Quoted 13 lines
arch/arm64/include/asm/kvm_pgtable.h +3 -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>

[Severity: Low]
Does removing linux/kvm_host.h leave some macros used later in this header without their explicit includes?

For example, in kvm_get_parange_max(), the code relies on PAGE_SHIFT:

     if (kvm_lpa2_is_enabled() ||
            (IS_ENABLED(CONFIG_ARM64_PA_BITS_52) && PAGE_SHIFT == 16))

In kvm_pte_to_phys(), the code relies on FIELD_GET:

     pa |= FIELD_GET(KVM_PTE_ADDR_51_50_LPA2, pte) << 50;
Quoted 8 lines

And in kvm_pte_to_pfn(), it uses __phys_to_pfn:

     return __phys_to_pfn(kvm_pte_to_phys(pte));

Should this file explicitly include <linux/bitfield.h>, <asm/page.h>, and <asm/memory.h> to prevent build fragilities when it is included in an isolated compilation unit?

I have added these changes to the header files and built the kvm integration branch. So I will pick these suggestions.

Cheers
Suzuki

Quoted 2 lines

[ ... ]

author reviewer
#10 lore raw

Re: [PATCH v19 02/20] KVM: arm64: Disable Steal time accounting for protected guests

In reply to Suzuki K Poulose
Quoted 42 lines · Suzuki K Poulose
Quoted 38 lines · On 20/09/2026 22:44, [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] arm64: kvm: contradictory SMCCC advertisement for PV_TIME
on protected VMs

Signature · 9 lines
commit db2c057e4ceefd58162587d4fd417788a4db3dbc
Author: Suzuki K Poulose <[email protected]>

     KVM: arm64: Disable Steal time accounting for protected guests
     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/kvm/pvtime.c gpa_t kvm_init_stolen_time(struct kvm_vcpu *vcpu) · line 72

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

[Severity: Medium]
Does this create a contradictory SMCCC advertisement for protected VMs?

A protected guest querying ARM_SMCCC_ARCH_FEATURES_FUNC_ID for ARM_SMCCC_HV_PV_TIME_FEATURES in kvm_smccc_call_handler() will receive SMCCC_RET_SUCCESS because the KVM_REG_ARM_STD_HYP_BIT_PV_TIME bit is still set in std_hyp_bmap:

This looks like an existing issue in the code. I have something like this to fix this :

....

Quoted 29 lines
arch/arm64/kvm/hypercalls.c +3 -0
index b11b8821c9fbc..6fabcd8aaacb9 100644
@@ -402,6 +402,9 @@ void kvm_arm_init_hypercalls(struct kvm *kvm)
 
         smccc_feat->std_bmap = KVM_ARM_SMCCC_STD_FEATURES;
         smccc_feat->std_hyp_bmap = KVM_ARM_SMCCC_STD_HYP_FEATURES;
+       /* Clear PV_TIME support for protected VMs */
+       if (kvm_vm_is_protected(kvm))
+               clear_bit(KVM_REG_ARM_STD_HYP_BIT_PV_TIME, &smccc_feat- 
 >std_hyp_bmap);
         smccc_feat->vendor_hyp_bmap = KVM_ARM_SMCCC_VENDOR_HYP_FEATURES;
 

        mt_init(&kvm->arch.smccc_filter);

arch/arm64/kvm/hypercalls.c +3 -0
@@ -532,6 +535,9 @@ static int kvm_arm_set_fw_reg_bmap(struct kvm_vcpu 
 *vcpu, u64 reg_id, u64 val)
         case KVM_REG_ARM_STD_HYP_BMAP:
                 fw_reg_bmap = &smccc_feat->std_hyp_bmap;
                 fw_reg_features = KVM_ARM_SMCCC_STD_HYP_FEATURES;
+               /* PV_TIME is not supported for protected VMs */
+               if (vcpu_is_protected(vcpu))
+                       clear_bit(KVM_REG_ARM_STD_HYP_BIT_PV_TIME, 
 &fw_reg_features);
                 break;

        case KVM_REG_ARM_VENDOR_HYP_BMAP:
                fw_reg_bmap = &smccc_feat->vendor_hyp_bmap;

Quoted 30 lines

Suzuki

Quoted 28 lines

arch/arm64/kvm/hypercalls.c:kvm_smccc_call_handler() {
    ...
    case ARM_SMCCC_HV_PV_TIME_FEATURES:
        if (test_bit(KVM_REG_ARM_STD_HYP_BIT_PV_TIME, &smccc_feat->std_hyp_bmap)) val[0] = SMCCC_RET_SUCCESS; break;
    ...
}

However, when the guest actually invokes the feature, kvm_hypercall_pv_features() returns SMCCC_RET_NOT_SUPPORTED because the IPA is unconfigured:

arch/arm64/kvm/pvtime.c:kvm_hypercall_pv_features() {
    ...
    case ARM_SMCCC_HV_PV_TIME_FEATURES:
    case ARM_SMCCC_HV_PV_TIME_ST:
        if (vcpu->arch.steal.base != INVALID_GPA) val = SMCCC_RET_SUCCESS;
        break;
    ...
}

This violates the SMCCC ABI by advertising ARM_SMCCC_HV_PV_TIME_FEATURES as supported but returning NOT_SUPPORTED when it is invoked. Should the KVM_REG_ARM_STD_HYP_BIT_PV_TIME bit be cleared for protected VMs?

I went back to this and the behavior is as per the DEN0057A.b, Section 4.2, the guest must use ARM_SMCCC_HV_PV_TIME_FEATURES call to actually test if the FIDs are implemented. So, we can ignore this one.

Cheers
Suzuki

Quoted 2 lines
Quoted 1 line
#2 lore raw

Re: [PATCH v19 01/20] KVM: arm64: protected VM: Handle user writes to CNTVCT_EL0/CNTPCT_EL0

In reply to Suzuki K Poulose

On Sun, 20 Sep 2026 22:28:26 +0100
Suzuki K Poulose <[email protected]> wrote:

Hi Suzuki,

Quoted 1 line

Protected VMs doesn't allow setting offsets for virtual and phyiscal

physical

Quoted 1 line

counters, as the offset is always fixed to 0. The VM ioclt is filtered

ioctl

Quoted 9 lines

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 KVM init time for protected VMs. A userspace writing to the CNT*CT_EL0 would observe success, without any real effect. This was chosen over preventing the writes to these registers and returning -EPERM.

Why? I don't mind the decision but telling us what was chosen is something we can see in the code - patch description should give us the stuff we can't see.

One comment on the comment below.

Quoted 32 lines

Reported by Sashiko

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

---
Changes since v18:
- Retain NULL vm_offset for protected VMs to avoid host tampering with the offset.
- Moved the flag setting into kvm_timer_init_vm(), where it should have been in the first place

---
 arch/arm64/kvm/arch_timer.c | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)
arch/arm64/kvm/arch_timer.c +1 -2
index 6ac3321f4c575..226cd5a495c8b 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);
 	}

arch/arm64/kvm/arch_timer.c void kvm_timer_init_vm(struct kvm *kvm) · line 1138

Quoted 7 lines
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]);
+
+	/*
+	 * Protected VMs don't allow any offset being set from userspace,
+	 * either set via writes to the counters or using the dedicated

This sentence confused me. Second clause isn't obviously the ways that are being blocked. Maybe shorten to:

Protected VMs don't allow the offset to be set from userspace, whether via writes to the counters or the dedicated ioctl.

Quoted 8 lines
arch/arm64/kvm/arch_timer.c +5 -0
+	 * ioctl. Pretend the offset has already been set and rely on the
+	 * default offset being 0.
+	 */
+	if (kvm_vm_is_protected(kvm))
+		set_bit(KVM_ARCH_FLAG_VM_COUNTER_OFFSET, &kvm->arch.flags);
 }
 
 void kvm_timer_cpu_up(void)
Reviewed-by on v19 3/20
#13 lore raw

Re: [PATCH v19 03/20] KVM: arm64: Include kvm_emulate.h in kvm/arm_psci.h

In reply to Suzuki K Poulose
Quoted 7 lines · On Sun, 20 Sep 2026 22:28:28 +0100 Suzuki K Poulose <[email protected]>…

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]

Quoted 7 lines
   29 |         if (vcpu_has_feature(vcpu, KVM_ARM_VCPU_PSCI_0_2)) {
	         |             ^~~~~~~~~~~~~~~~
			       |             cpu_have_feature

Seems like some of these precursor bits would ideally be picked up asap - though that does confuse the LLM magic.

Quoted 17 lines
---
 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)
Reviewed-by on v19 5/20
#20 lore raw

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

In reply to Suzuki K Poulose
Quoted 4 lines · On Sun, 20 Sep 2026 22:28:30 +0100 Suzuki K Poulose <[email protected]>…

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.

Let's have some optimism!

Quoted 9 lines

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. To tighten this init_pkvm_hyp_vm() restricts the hyp copy of the vm_flavor to the ones it supports.

Seems sensible to me. One trivial thing inline.

Quoted 23 lines
---
 arch/arm64/include/asm/kvm_host.h | 22 +++++++++++++++++++---
 arch/arm64/include/asm/kvm_pkvm.h |  4 ++--
 arch/arm64/kvm/arm.c              | 31 ++++++++++++++++++++++++++-----
 arch/arm64/kvm/handle_exit.c      |  2 +-
 arch/arm64/kvm/hyp/nvhe/pkvm.c    |  6 +++++-
 arch/arm64/kvm/mmu.c              |  2 +-
 arch/arm64/kvm/pkvm.c             |  6 ++----
 7 files changed, 56 insertions(+), 17 deletions(-)
arch/arm64/include/asm/kvm_host.h +0 -1
index 286489a69dff5..9b1cf9c59e81f 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;
 
 	/*

arch/arm64/include/asm/kvm_host.h enum fgt_group_id { · line 314

Quoted 10 lines
arch/arm64/include/asm/kvm_host.h +7 -0
@@ -306,9 +305,19 @@ enum fgt_group_id {
 	__NR_FGT_GROUP_IDS__
 };
 
+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,

Trivial but given whole point is this is an end marker, drop the trailing , No point in making it easy to break the ordering!

Quoted 2 lines
arch/arm64/include/asm/kvm_host.h +2 -0
+};
+
#33 lore raw

Re: [PATCH v19 06/20] KVM: arm64: Refactor the vcpu_load to allow for VM specific callbacks

In reply to Suzuki K Poulose
Quoted 9 lines · On Sun, 20 Sep 2026 22:28:31 +0100 Suzuki K Poulose <[email protected]>…

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. Based on a work by Marc Zyngier.

Trivial stuff inline + maybe one blob that should be in previous patch?

Quoted 11 lines
---
 arch/arm64/kvm/arm.c | 48 ++++++++++++++++++++++++++------------------
 1 file changed, 29 insertions(+), 19 deletions(-)
arch/arm64/kvm/arm.c +2 -0
index 8c784b266a8e8..c74706ed9a531 100644
 
+
+static void vcpu_set_wfx_traps(struct kvm_vcpu *vcpu)

arch/arm64/kvm/arm.c void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu) · line 732

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

You could use FIELD_MODIFY() for these but maybe not worth it.

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

arch/arm64/kvm/arm.c void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu) · line 786

Quoted 6 lines
arch/arm64/kvm/arm.c +1 -1
@@ -772,7 +782,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);

Why in this patch?

Quoted 2 lines
arch/arm64/kvm/arm.c +0 -0
 	}
 
author reviewer
#3 lore raw

Re: [PATCH v19 01/20] KVM: arm64: protected VM: Handle user writes to CNTVCT_EL0/CNTPCT_EL0

In reply to Jonathan Cameron
Quoted 23 lines · Jonathan Cameron

On Sun, 20 Sep 2026 22:28:26 +0100
Suzuki K Poulose <[email protected]> wrote:

Hi Suzuki,

Quoted 1 line

Protected VMs doesn't allow setting offsets for virtual and phyiscal

physical

Quoted 1 line

counters, as the offset is always fixed to 0. The VM ioclt is filtered

ioctl

Quoted 9 lines

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 KVM init time for protected VMs. A userspace writing to the CNT*CT_EL0 would observe success, without any real effect. This was chosen over preventing the writes to these registers and returning -EPERM.

Quoted 3 lines

Why? I don't mind the decision but telling us what was chosen is something we can see in the code - patch description should give us the stuff we can't see.

I have modified the paragraph to :

"Fix this by always "fixing" the timer offsets to 0 and marking that the timer offset is set in the kvm->arch.flags at KVM init time for protected VMs. This prevents the access to the VM specific vm_offset at low cost. A userspace writing to the CNT*CT_EL0 would observe success, without any real effect. This is cleaner over spilling *_is_protected()" checks and "matches" what we really do in practise. i.e., always run with "fixed counter offset of 0".

Quoted 44 lines

One comment on the comment below.

Quoted 32 lines

Reported by Sashiko

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

---
Changes since v18:
- Retain NULL vm_offset for protected VMs to avoid host tampering with the offset.
- Moved the flag setting into kvm_timer_init_vm(), where it should have been in the first place

---
  arch/arm64/kvm/arch_timer.c | 12 ++++++++++--
  1 file changed, 10 insertions(+), 2 deletions(-)
arch/arm64/kvm/arch_timer.c +1 -2
index 6ac3321f4c575..226cd5a495c8b 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);
  	}

arch/arm64/kvm/arch_timer.c void kvm_timer_init_vm(struct kvm *kvm) · line 1138

Quoted 7 lines
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]);
+
+	/*
+	 * Protected VMs don't allow any offset being set from userspace,
+	 * either set via writes to the counters or using the dedicated

This sentence confused me. Second clause isn't obviously the ways that are being blocked. Maybe shorten to:

Quoted 2 lines

Protected VMs don't allow the offset to be set from userspace, whether via writes to the counters or the dedicated ioctl.

Ack, will improve it.

Cheers
Suzuki

Quoted 10 lines
Quoted 9 lines
arch/arm64/kvm/arch_timer.c +5 -0
+	 * ioctl. Pretend the offset has already been set and rely on the
+	 * default offset being 0.
+	 */
+	if (kvm_vm_is_protected(kvm))
+		set_bit(KVM_ARCH_FLAG_VM_COUNTER_OFFSET, &kvm->arch.flags);
  }
 
  void kvm_timer_cpu_up(void)
author reviewer
#5 lore raw

Re: [PATCH v19 01/20] KVM: arm64: protected VM: Handle user writes to CNTVCT_EL0/CNTPCT_EL0

In reply to Jonathan Cameron
Quoted 70 lines · Jonathan Cameron

On Sun, 20 Sep 2026 22:28:26 +0100
Suzuki K Poulose <[email protected]> wrote:

Hi Suzuki,

Quoted 1 line

Protected VMs doesn't allow setting offsets for virtual and phyiscal

physical

Quoted 1 line

counters, as the offset is always fixed to 0. The VM ioclt is filtered

ioctl

Quoted 9 lines

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 KVM init time for protected VMs. A userspace writing to the CNT*CT_EL0 would observe success, without any real effect. This was chosen over preventing the writes to these registers and returning -EPERM.

Why? I don't mind the decision but telling us what was chosen is something we can see in the code - patch description should give us the stuff we can't see.

One comment on the comment below.

Quoted 32 lines

Reported by Sashiko

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

---
Changes since v18:
- Retain NULL vm_offset for protected VMs to avoid host tampering with the offset.
- Moved the flag setting into kvm_timer_init_vm(), where it should have been in the first place

---
  arch/arm64/kvm/arch_timer.c | 12 ++++++++++--
  1 file changed, 10 insertions(+), 2 deletions(-)
arch/arm64/kvm/arch_timer.c +1 -2
index 6ac3321f4c575..226cd5a495c8b 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);
  	}

arch/arm64/kvm/arch_timer.c void kvm_timer_init_vm(struct kvm *kvm) · line 1138

Quoted 7 lines
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]);
+
+	/*
+	 * Protected VMs don't allow any offset being set from userspace,
+	 * either set via writes to the counters or using the dedicated

This sentence confused me. Second clause isn't obviously the ways that are being blocked. Maybe shorten to:

Quoted 2 lines

Protected VMs don't allow the offset to be set from userspace, whether via writes to the counters or the dedicated ioctl.

LLM suggests:

* Protected VMs don't allow userspace to set counter offsets,
* either via counter register writes or the dedicated ioctl.

Have updated the comment.

Cheers
Suzuki

Quoted 10 lines
Quoted 9 lines
arch/arm64/kvm/arch_timer.c +5 -0
+	 * ioctl. Pretend the offset has already been set and rely on the
+	 * default offset being 0.
+	 */
+	if (kvm_vm_is_protected(kvm))
+		set_bit(KVM_ARCH_FLAG_VM_COUNTER_OFFSET, &kvm->arch.flags);
  }
 
  void kvm_timer_cpu_up(void)
author reviewer
#34 lore raw

Re: [PATCH v19 06/20] KVM: arm64: Refactor the vcpu_load to allow for VM specific callbacks

In reply to Jonathan Cameron
Quoted 15 lines · Jonathan Cameron
Quoted 9 lines · On Sun, 20 Sep 2026 22:28:31 +0100 Suzuki K Poulose <[email protected]>…

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. Based on a work by Marc Zyngier.

Trivial stuff inline + maybe one blob that should be in previous patch?

...

Quoted 15 lines

arch/arm64/kvm/arm.c void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu) · line 732

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

You could use FIELD_MODIFY() for these but maybe not worth it.

...

Quoted 9 lines

arch/arm64/kvm/arm.c void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu) · line 786

Quoted 7 lines
arch/arm64/kvm/arm.c +1 -1
@@ -772,7 +782,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);

Why in this patch?

I have moved it to the patch where we introduce the VM flavors, thanks!

Suzuki

Quoted 4 lines
Quoted 3 lines
(quoted diff) +0 -0
  	}
 
#36 lore raw

Re: [PATCH v19 07/20] KVM: arm64: Add vcpu load/put call backs for flavors

In reply to Suzuki K Poulose
Quoted 8 lines · On Sun, 20 Sep 2026 22:28:32 +0100 Suzuki K Poulose <[email protected]>…

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

A couple of comments on this one.

Quoted 20 lines

---
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/kvm/arm.c +3 -0
index c74706ed9a531..9b977dc734220 100644
 
+static void nvhe_vcpu_load(struct kvm_vcpu *vcpu)
+{
+	vcpu_prepare_mmu(vcpu);

arch/arm64/kvm/arm.c void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu) · line 776

Quoted 10 lines
arch/arm64/kvm/arm.c +10 -0
+	kvm_timer_vcpu_load(vcpu);
+	kvm_vgic_load(vcpu);
+	kvm_vcpu_load_debug(vcpu);
+	kvm_vcpu_load_fgt(vcpu);
+	kvm_arch_vcpu_load_fp(vcpu);
+	kvm_vcpu_pmu_restore_guest(vcpu);
+
+	vcpu_load_pvtime(vcpu);
+	vcpu_set_wfx_traps(vcpu);
+	vcpu_set_pauth_traps(vcpu);

Now it isn't called from the pkvm path can you remove the defenses in there on !is_protected_kvm_enabled()?

Quoted 8 lines
arch/arm64/kvm/arm.c +7 -0
+}
+
 
+
+static void pkvm_vcpu_put(struct kvm_vcpu *vcpu)
+{
+	kvm_call_hyp_nvhe(__vgic_v3_save_aprs, &vcpu->arch.vgic_cpu.vgic_v3);
+	kvm_call_hyp_nvhe(__pkvm_vcpu_put);

arch/arm64/kvm/arm.c void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu) · line 845

Quoted 10 lines
arch/arm64/kvm/arm.c +10 -0
+
+	/* __pkvm_vcpu_put implies a sync of the state */
+	if (kvm_vm_is_unprotected_pkvm(vcpu->kvm))
+		vcpu_set_flag(vcpu, PKVM_HOST_STATE_DIRTY);
+
+	kvm_vcpu_put_debug(vcpu);
+	kvm_arch_vcpu_put_fp(vcpu);
+	kvm_timer_vcpu_put(vcpu);
+	kvm_vgic_put(vcpu);
+	kvm_vcpu_pmu_restore_host(vcpu);

May have been incorrect or pointless but wasn't kvm_arm_vmid_clear_active() previously called in this path? Would be nice to tweak that to be under an appropriate gate in a precursor patch with explanation of why.

Quoted 11 lines
arch/arm64/kvm/arm.c +5 -0
+}
+
+void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
+{
+	vcpu->arch.vcpu_ops->vcpu_put(vcpu);
 	vcpu_clear_on_unsupported_cpu(vcpu);
 	vcpu->cpu = -1;
 }
@@ -2136,6 +2187,37 @@ int kvm_arch_vm_ioctl(struct file *filp, unsigned int ioctl, unsigned long arg)
 	}
 }

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

Quoted 10 lines
arch/arm64/kvm/arm.c +9 -0
 
+
+#define KVM_VCPU_OPS(flavor, ops)		\
+	[(flavor)] = (ops)
+
+static const struct kvm_vcpu_ops *arm64_vcpu_ops[] = {
+	KVM_VCPU_OPS(VM_VHE, &vhe_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),

I'd put these in same order as the enum. Given the construction doesn't really matter but just feels more natural when reading the code to check they are all there (obviously the BUILD_BUG below also does that).

Quoted 11 lines
arch/arm64/kvm/arm.c +8 -0
+};
+
+static void kvm_init_vcpu_ops(struct kvm_vcpu *vcpu)
+{
+	BUILD_BUG_ON(ARRAY_SIZE(arm64_vcpu_ops) != VM_FLAVOR_MAX);
+	vcpu->arch.vcpu_ops = arm64_vcpu_ops[vcpu->kvm->arch.vm_flavor];
+}
+
 static unsigned long nvhe_percpu_size(void)
 {
 	return (unsigned long)CHOOSE_NVHE_SYM(__per_cpu_end) -
Reviewed-by on v19 8/20
#38 lore raw

Re: [PATCH v19 08/20] KVM: arm64: Reuse kvm_stage2_unmap_range in kvm_unmap_gfn_range

In reply to Suzuki K Poulose
Quoted 11 lines · On Sun, 20 Sep 2026 22:28:33 +0100 Suzuki K Poulose <[email protected]>…

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.

Trivial thing inline. Otherwise this is fine.

Quoted 11 lines
---
 arch/arm64/kvm/mmu.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
arch/arm64/kvm/mmu.c +0 -0
index 0f4e8b71fa85d..03f2017a7404a 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)

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

Quoted 10 lines
arch/arm64/kvm/mmu.c +3 -4
 {
-	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,

Looks like indent is 1 too many space.

Quoted 4 lines
arch/arm64/kvm/mmu.c +1 -0
+				range->may_block);
 
 	kvm_nested_s2_unmap(kvm, range->may_block);
 	return false;
Reviewed-by on v19 9/20
#41 lore raw

Re: [PATCH v19 09/20] KVM: arm64: Add VM specific callback for S2 MMU operations

In reply to Suzuki K Poulose
Quoted 4 lines · On Sun, 20 Sep 2026 22:28:34 +0100 Suzuki K Poulose <[email protected]>…

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

A small suggestion inline that makes it explicit when there isn't range operation and a full flush of tlb is needed.

I don't mind either way, but thought I'd mention it anyway in case you hadn't already dismissed it.

Quoted 13 lines
arch/arm64/kvm/mmu.c +0 -0
index 03f2017a7404a..d97a4a1bca23f 100644
@@ -37,6 +37,8 @@ static unsigned long __ro_after_init io_map_base;
 
 #define KVM_PGT_FN(fn)		(!is_protected_kvm_enabled() ? fn : p ## fn)
 
 
 /**
  * kvm_arch_flush_remote_tlbs() - flush all VM TLB entries for v7/8
  * @kvm:	pointer to kvm structure.

arch/arm64/kvm/mmu.c static bool memslot_is_logging(struct kvm_memory_slot *memslot) · line 192

Quoted 10 lines
arch/arm64/kvm/mmu.c +2 -5
@@ -174,26 +188,36 @@ static bool memslot_is_logging(struct kvm_memory_slot *memslot)
  */
 int kvm_arch_flush_remote_tlbs(struct kvm *kvm)
 {
-	if (is_protected_kvm_enabled())
-		kvm_call_hyp_nvhe(__pkvm_tlb_flush_vmid, kvm->arch.pkvm.handle);
-	else
-		kvm_call_hyp(__kvm_tlb_flush_vmid, &kvm->arch.mmu);
-	return 0;
+	if (!kvm->arch.vm_s2_ops->vm_flush_remote_tlbs)
+		return 1;

For consistency with other cases and because it looks nicer blank line here.

arch/arm64/kvm/mmu.c static bool memslot_is_logging(struct kvm_memory_slot *memslot) · line 199

Quoted 9 lines
arch/arm64/kvm/mmu.c +5 -2
+	return kvm->arch.vm_s2_ops->vm_flush_remote_tlbs(kvm);
 }
 
-int kvm_arch_flush_remote_tlbs_range(struct kvm *kvm,
-				      gfn_t gfn, u64 nr_pages)
+static int pkvm_flush_remote_tlbs_range(struct kvm *kvm,
+					gfn_t gfn, u64 nr_pages)
+{
+	return pkvm_flush_remote_tlbs(kvm);

For the cases where there isn't a range call, perhaps you should just not provide the op and have the wrapper fall back to calling the 'flush all' variant if there is no range one available?

Quoted 1 line
arch/arm64/kvm/mmu.c +1 -0
+}
#50 lore raw

Re: [PATCH v19 10/20] KVM: arm64: Abstract out memory abort handling

In reply to Suzuki K Poulose
Quoted 3 lines · On Sun, 20 Sep 2026 22:28:35 +0100 Suzuki K Poulose <[email protected]>…

Move the memory abort handling under VM specific s2 operation.

Trivial comment + request to follow through on adding the local variable for kvm. I think the other places that can be used survive the other changes in this patch set.

Quoted 10 lines
arch/arm64/kvm/mmu.c +4 -0
index d97a4a1bca23f..cf293d09e940a 100644
 
 
+static int kvm_vm_mem_abort(const struct kvm_s2_fault_desc *s2fd)
+{
+	int ret;
+	struct kvm_vcpu *vcpu = s2fd->vcpu;

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

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

Personal preference but I'd just do

		return gmem_abort(s2fd);
	else
		return user_mem_abort(s2fd);

and save a couple of lines. I'd keep the else to make it clear it is an either / or choice rather than an if something true one.

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

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

If introducing this, also use it for the other places vcpu->kvm is used in this function. Separate patch though to keep down noise. Looks like that's mostly to shorten the chain of dereferences in the locks.

Quoted 24 lines
arch/arm64/kvm/mmu.c +1 -13
 	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:
Reviewed-by on v19 11/20
#53 lore raw

Re: [PATCH v19 11/20] KVM: arm64: Mandate VGIC v3 for for VMs running on hyp that don't trust the host

In reply to Suzuki K Poulose
Quoted 1 line · On Sun, 20 Sep 2026 22:28:36 +0100 Suzuki K Poulose <[email protected]>…

pKVM and RMM, both do not trust the host. Add a helper to detect the VMs

Maybe don't mention RMM yet, or make it clear that isn't here in this patch.

Quoted 4 lines

that have "distrusting" hyp. Use this for blocking ioremap of vgic-v2 into stage2 and prevent creation of VGIC other than v3.

Other than minor comments, it feels to me like these first 11 patches are good cleanup even without the RMM stuff that follows. Perhaps can get these upstream faster than the rest?

Jonathan

Quoted 57 lines

---
Changes since v18:
- Cover pKVM guests for VGIC v3 mandate.
- Merge the VGIC mandate check in here.

---
 arch/arm64/include/asm/kvm_host.h | 3 +++
 arch/arm64/kvm/mmu.c              | 2 +-
 arch/arm64/kvm/vgic/vgic-init.c   | 2 ++
 3 files changed, 6 insertions(+), 1 deletion(-)
arch/arm64/include/asm/kvm_host.h +3 -0
index 3211543a85b38..bb156a633ab75 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 */
@@ -1546,6 +1548,7 @@ struct kvm *kvm_arch_alloc_vm(void);
 #define kvm_vm_is_unprotected_pkvm(kvm)		\
 	(is_protected_kvm_enabled() && ((kvm)->arch.vm_flavor == VM_PKVM))
 
+#define kvm_vm_hyp_is_distrusting(kvm)	((kvm)->arch.vm_flavor >= __VM_DISTRUSTING_HYP)
 
 int kvm_arm_vcpu_finalize(struct kvm_vcpu *vcpu, int feature);
 bool kvm_arm_vcpu_is_finalized(struct kvm_vcpu *vcpu);
arch/arm64/kvm/mmu.c +1 -1
index cf293d09e940a..413c4b114d75d 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);
arch/arm64/kvm/vgic/vgic-init.c +2 -0
index 4012df6002ea6..874025513afcc 100644
@@ -84,6 +84,8 @@ int kvm_vgic_create(struct kvm *kvm, u32 type)
 		!kvm_vgic_global_state.can_emulate_gicv2)
 		return -ENODEV;
 
+	if (kvm_vm_hyp_is_distrusting(kvm) && type != KVM_DEV_TYPE_ARM_VGIC_V3)
+		return -ENODEV;
 	/*
 	 * Ensure mutual exclusion with vCPU creation and any vCPU ioctls by:
 	 *
Reviewed-by on v19 12/20
#57 lore raw

Re: [PATCH v19 12/20] KVM: arm64: CCA: Add a new mode for supporting Realm guests

In reply to Suzuki K Poulose
Quoted 3 lines · On Sun, 20 Sep 2026 22:28:37 +0100 Suzuki K Poulose <[email protected]>…

Add an explicit mode to support Arm CCA guests.

FWIW given how simple this is

Mostly given so I don't read it in v20 :)

Jonathan

Reviewed-by on v19 15/20
#63 lore raw

Re: [PATCH v19 15/20] KVM: arm64: CCA: Introduce Realms

In reply to Suzuki K Poulose
Quoted 11 lines · On Sun, 20 Sep 2026 22:28:40 +0100 Suzuki K Poulose <[email protected]>…

From: Steven Price <[email protected]>

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.

Quoted 6 lines

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.

Seems like sensible infrastructure to me.

#66 lore raw

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

In reply to Suzuki K Poulose
Quoted 9 lines · On Sun, 20 Sep 2026 22:28:41 +0100 Suzuki K Poulose <[email protected]>…

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_vm_ioctl_allowed() and move it into arch/arm64/kvm/arm.c. Also add a generic kvm_vm_ext_allowed() to handle pKVM and Realm capability filtering and route them accordingly.

One trivial - otherwise seems fine to me.

Quoted 14 lines
arch/arm64/kvm/arm.c +9 -0
index 86e705330d7bd..eab8543a4194d 100644
 
+/*
+ * 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_vm_ioctl_allowed(struct kvm *kvm, unsigned int ioctl)
+{
+	long ext;
+	int r;

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

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

Comment is a little confusing. This is the path that covers everything other than those two. Maybe 'Rest of function covers both...'

Quoted 9 lines
arch/arm64/kvm/arm.c +9 -0
+	if (!kvm_vm_hyp_is_distrusting(kvm))
+		return true;
+
+	r = kvm_get_cap_for_kvm_ioctl(ioctl, &ext);
+	if (WARN_ON_ONCE(r < 0))
+		return false;
+
+	return kvm_vm_ext_allowed(kvm, ext);
+}
Reviewed-by on v19 17/20
#68 lore raw

Re: [PATCH v19 17/20] KVM: arm64: CCA: WARN on injected undef exceptions

In reply to Suzuki K Poulose
Quoted 6 lines · On Sun, 20 Sep 2026 22:28:42 +0100 Suzuki K Poulose <[email protected]>…

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.

author reviewer
#42 lore raw

Re: [PATCH v19 09/20] KVM: arm64: Add VM specific callback for S2 MMU operations

In reply to Jonathan Cameron
Quoted 35 lines · Jonathan Cameron
Quoted 4 lines · On Sun, 20 Sep 2026 22:28:34 +0100 Suzuki K Poulose <[email protected]>…

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

A small suggestion inline that makes it explicit when there isn't range operation and a full flush of tlb is needed.

I don't mind either way, but thought I'd mention it anyway in case you hadn't already dismissed it.

Quoted 18 lines
arch/arm64/kvm/mmu.c +0 -2
index 03f2017a7404a..d97a4a1bca23f 100644
@@ -37,6 +37,8 @@ static unsigned long __ro_after_init io_map_base;
 
  #define KVM_PGT_FN(fn)		(!is_protected_kvm_enabled() ? fn : p ## fn)
 
 
  /**
   * kvm_arch_flush_remote_tlbs() - flush all VM TLB entries for v7/8
   * @kvm:	pointer to kvm structure.
@@ -174,26 +188,36 @@ static bool memslot_is_logging(struct kvm_memory_slot *memslot)
   */
  int kvm_arch_flush_remote_tlbs(struct kvm *kvm)
  {
-	if (is_protected_kvm_enabled())
-		kvm_call_hyp_nvhe(__pkvm_tlb_flush_vmid, kvm->arch.pkvm.handle);
Quoted 8 lines

arch/arm64/kvm/mmu.c static bool memslot_is_logging(struct kvm_memory_slot *memslot) · line 192

Quoted 5 lines
arch/arm64/kvm/mmu.c +2 -3
-	else
-		kvm_call_hyp(__kvm_tlb_flush_vmid, &kvm->arch.mmu);
-	return 0;
+	if (!kvm->arch.vm_s2_ops->vm_flush_remote_tlbs)
+		return 1;

For consistency with other cases and because it looks nicer blank line here.

Ack

Quoted 4 lines
Quoted 4 lines
arch/arm64/kvm/mmu.c +1 -0
+	return kvm->arch.vm_s2_ops->vm_flush_remote_tlbs(kvm);
  }
 
Quoted 10 lines

arch/arm64/kvm/mmu.c static bool memslot_is_logging(struct kvm_memory_slot *memslot) · line 199

Quoted 6 lines
arch/arm64/kvm/mmu.c +4 -2
-int kvm_arch_flush_remote_tlbs_range(struct kvm *kvm,
-				      gfn_t gfn, u64 nr_pages)
+static int pkvm_flush_remote_tlbs_range(struct kvm *kvm,
+					gfn_t gfn, u64 nr_pages)
+{
+	return pkvm_flush_remote_tlbs(kvm);

For the cases where there isn't a range call, perhaps you should just not provide the op and have the wrapper fall back to calling the 'flush all' variant if there is no range one available?

The core KVM falls back to flushing all TLBs if the range one returns true anyways. So, I would leave it as it is.

Cheers
Suzuki

Quoted 4 lines
Quoted 2 lines
arch/arm64/kvm/mmu.c +1 -0
+}
author reviewer
#54 lore raw

Re: [PATCH v19 11/20] KVM: arm64: Mandate VGIC v3 for for VMs running on hyp that don't trust the host

In reply to Jonathan Cameron
Quoted 7 lines · Jonathan Cameron
Quoted 1 line · On Sun, 20 Sep 2026 22:28:36 +0100 Suzuki K Poulose <[email protected]>…

pKVM and RMM, both do not trust the host. Add a helper to detect the VMs

Maybe don't mention RMM yet, or make it clear that isn't here in this patch.

Ack.

Quoted 7 lines
Quoted 5 lines

that have "distrusting" hyp. Use this for blocking ioremap of vgic-v2 into stage2 and prevent creation of VGIC other than v3.

Thanks

Quoted 65 lines

Other than minor comments, it feels to me like these first 11 patches are good cleanup even without the RMM stuff that follows. Perhaps can get these upstream faster than the rest?

Jonathan

Quoted 57 lines

---
Changes since v18:
- Cover pKVM guests for VGIC v3 mandate.
- Merge the VGIC mandate check in here.

---
  arch/arm64/include/asm/kvm_host.h | 3 +++
  arch/arm64/kvm/mmu.c              | 2 +-
  arch/arm64/kvm/vgic/vgic-init.c   | 2 ++
  3 files changed, 6 insertions(+), 1 deletion(-)
arch/arm64/include/asm/kvm_host.h +3 -0
index 3211543a85b38..bb156a633ab75 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 */
@@ -1546,6 +1548,7 @@ struct kvm *kvm_arch_alloc_vm(void);
  #define kvm_vm_is_unprotected_pkvm(kvm)		\
  	(is_protected_kvm_enabled() && ((kvm)->arch.vm_flavor == VM_PKVM))
 
+#define kvm_vm_hyp_is_distrusting(kvm)	((kvm)->arch.vm_flavor >= __VM_DISTRUSTING_HYP)
 
  int kvm_arm_vcpu_finalize(struct kvm_vcpu *vcpu, int feature);
  bool kvm_arm_vcpu_is_finalized(struct kvm_vcpu *vcpu);
arch/arm64/kvm/mmu.c +1 -1
index cf293d09e940a..413c4b114d75d 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);
arch/arm64/kvm/vgic/vgic-init.c +2 -0
index 4012df6002ea6..874025513afcc 100644
@@ -84,6 +84,8 @@ int kvm_vgic_create(struct kvm *kvm, u32 type)
  		!kvm_vgic_global_state.can_emulate_gicv2)
  		return -ENODEV;
 
+	if (kvm_vm_hyp_is_distrusting(kvm) && type != KVM_DEV_TYPE_ARM_VGIC_V3)
+		return -ENODEV;
  	/*
  	 * Ensure mutual exclusion with vCPU creation and any vCPU ioctls by:
  	 *
author reviewer
#51 lore raw

Re: [PATCH v19 10/20] KVM: arm64: Abstract out memory abort handling

In reply to Jonathan Cameron
Quoted 33 lines · Jonathan Cameron
Quoted 3 lines · On Sun, 20 Sep 2026 22:28:35 +0100 Suzuki K Poulose <[email protected]>…

Move the memory abort handling under VM specific s2 operation.

Trivial comment + request to follow through on adding the local variable for kvm. I think the other places that can be used survive the other changes in this patch set.

Quoted 10 lines
arch/arm64/kvm/mmu.c +4 -0
index d97a4a1bca23f..cf293d09e940a 100644
 
 
+static int kvm_vm_mem_abort(const struct kvm_s2_fault_desc *s2fd)
+{
+	int ret;
+	struct kvm_vcpu *vcpu = s2fd->vcpu;

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

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

Personal preference but I'd just do

Quoted 3 lines
		return gmem_abort(s2fd);
	else
		return user_mem_abort(s2fd);

Ack

Quoted 4 lines

and save a couple of lines. I'd keep the else to make it clear it is an either / or choice rather than an if something true one.

Quoted 15 lines

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

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

If introducing this, also use it for the other places vcpu->kvm is used in this function. Separate patch though to keep down noise. Looks like that's mostly to shorten the chain of dereferences in the locks.

Added a preparatory patch to convert the other users to a local "kvm" variable.

Cheers
Suzuki

#21 lore raw

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

In reply to Suzuki K Poulose

Hi Suzuki,

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

With that, add wrappers for checking the "type" of a VM and replace the existing users with the new wrappers.

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.

Adds __VM_PROTECTED marker vm flavor to generalize kvm_vm_is_protected() to predicate all confidential guests running on KVM. In later patches, we would add Realm VMs, which would also be classified as protected.

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. To tighten this init_pkvm_hyp_vm() restricts the hyp copy of the vm_flavor to the ones it supports.

---
Changes since v18:
- Merge the __VM_PROTECTED marker and the widening of kvm_vm_is_protected() to this patch.
- Merge the use of kvm_vm_is_unprotected_pkvm() for !kvm_vm_is_protected() given the scope changes here.
- Drop Fuad's review tag, as this patch has multiple merges
- Restrict the VM flavors to the supported types in init_pkvm_hyp_vm().
- Drop kvm_vm_hyp_is_pkvm() and revert to is_protected_kvm_enabled()
- Use is_protected_kvm_enabled() to make the pKVM guest flavor checks.
- s/PKVM/pKVM for commit descriptions too

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

Quoted 10 lines
---
  arch/arm64/include/asm/kvm_host.h | 22 +++++++++++++++++++---
  arch/arm64/include/asm/kvm_pkvm.h |  4 ++--
  arch/arm64/kvm/arm.c              | 31 ++++++++++++++++++++++++++-----
  arch/arm64/kvm/handle_exit.c      |  2 +-
  arch/arm64/kvm/hyp/nvhe/pkvm.c    |  6 +++++-
  arch/arm64/kvm/mmu.c              |  2 +-
  arch/arm64/kvm/pkvm.c             |  6 ++----
  7 files changed, 56 insertions(+), 17 deletions(-)

This causes broken nVHE mode. I applied PATCH[01-05] to v7.3.rc4 whose head commit is f0100363d8c3, and kselftests/kvm/guest_print causes host crash (as below). I don't see the crash if only PATCH[01-04] are applied.

host$ cat /proc/cmdline | grep kvm-arm\.mode
BOOT_IMAGE=(hd0,gpt2)/vmlinuz-7.3.0-rc4-gavin+ root=/dev/mapper/rhel_nvidia--grace--hopper--01-root ro crashkernel=2G-4G:406M,4G-64G:470M,64G-:726M rd.lvm.lv=rhel_nvidia-grace-hopper-01/root rd.lvm.lv=rhel_nvidia-grace-hopper-01/swap video=simplefb:off kvm-arm.mode=nvhe

host$ cd linux/tools/testing/selftests/kvm
host$ ./guest_print_test
Random seed: 0x193a0ed3

[  192.754328] kvm [6674]: nVHE hyp panic at: [<ffff8000811da044>] __kvm_nvhe___timer_enable_traps+0x4/0x160!
[  192.754338] kvm [6674]: nVHE call trace:
[  192.754339] kvm [6674]:  [<ffff8000811dcf34>] __kvm_nvhe_hyp_panic+0xb4/0xe0
[  192.754342] kvm [6674]:  [<ffff8000811dcba4>] __kvm_nvhe___kvm_vcpu_run+0x164/0x440
[  192.754344] kvm [6674]:  [<ffff8000811dfe90>] __kvm_nvhe_handle___kvm_vcpu_run+0x40/0x1f0
[  192.754346] kvm [6674]:  [<ffff8000811e0238>] __kvm_nvhe_handle_trap+0x158/0x280
[  192.754347] kvm [6674]:  [<ffff8000811dd8fc>] __kvm_nvhe___skip_pauth_save+0x4/0x4
[  192.754348] kvm [6674]: ---[ end nVHE call trace ]---
[  192.754350] Code: d2818002 17fffff7 d503201f f9400001 (b94a6821)
[  192.754350] kvm [6674]: Hyp Offset: 0xfffeb0d7fe2e0000
[  192.754351] Kernel panic - not syncing: HYP panic:
[  192.754351] PS:834003c9 PC:0000cf2882efa044 ESR:0000000096000004
[  192.754351] FAR:ffff00009b286a68 HPFAR:8000000000000000 PAR:1d00ec7edbadc8de
[  192.754351] VCPU:0000cf011848a350
[  192.845154] CPU: 12 UID: 0 PID: 6674 Comm: guest_print_tes Kdump: loaded Not tainted 7.3.0-rc4-gavin+ #7 PREEMPT(full)
[  192.856182] Hardware name:  GH200 P5042, BIOS 02.04.01 20250422
[  192.862231] Call trace:
[  192.864725]  show_stack+0x20/0x38 (C)
[  192.868471]  dump_stack_lvl+0x88/0xb8
[  192.872215]  dump_stack+0x18/0x30
[  192.875598]  vpanic+0x280/0x498
[  192.878806]  panic+0x68/0x70
[  192.881745]  nvhe_hyp_panic_handler+0x184/0x190
[  192.886372]  kvm_arm_vcpu_enter_exit+0x24/0x100
[  192.891003]  kvm_arch_vcpu_ioctl_run+0x254/0x7c0
[  192.895726]  kvm_vcpu_ioctl+0x174/0xb40
[  192.899645]  __arm64_sys_ioctl+0xb0/0x120
[  192.903745]  invoke_syscall.constprop.0+0xa8/0x100
[  192.908639]  do_el0_svc+0xb8/0xe0
[  192.912022]  el0_svc+0x48/0x1f8
[  192.915228]  el0t_64_sync_handler+0xa0/0xe8
[  192.919500]  el0t_64_sync+0x1ac/0x1b0
[  192.923243] SMP: stopping secondary CPUs
[  192.927653] Starting crashdump kernel...
[  192.931658] Bye!

Thanks,
Gavin

Quoted 208 lines
arch/arm64/include/asm/kvm_host.h +19 -3
index 286489a69dff5..9b1cf9c59e81f 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,19 @@ enum fgt_group_id {
  	__NR_FGT_GROUP_IDS__
  };
 
+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,
+};
+
  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,10 +1513,17 @@ 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)
  #define vcpu_is_protected(vcpu)		kvm_vm_is_protected((vcpu)->kvm)
 
+#define kvm_vm_is_protected_pkvm(kvm)		\
+	(is_protected_kvm_enabled() && ((kvm)->arch.vm_flavor == VM_PROTECTED_PKVM))
+#define vcpu_is_protected_pkvm(vcpu)	kvm_vm_is_protected_pkvm(vcpu->kvm)
+
+#define kvm_vm_is_unprotected_pkvm(kvm)		\
+	(is_protected_kvm_enabled() && ((kvm)->arch.vm_flavor == VM_PKVM))
+
+
  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_pkvm.h +2 -2
index 54a618d887fa4..e4ea80711bec6 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);
@@ -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 +26 -5
index db36815630790..8c784b266a8e8 100644
@@ -214,6 +214,26 @@ static int kvm_arm_default_max_vcpus(void)
  	return vgic_present ? kvm_vgic_get_max_vcpus() : KVM_MAX_VCPUS;
  }
 
+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
@@ -236,6 +256,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);
@@ -257,12 +281,9 @@ int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)
  		 * 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);
@@ -985,7 +1006,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;
 
  	/*
arch/arm64/kvm/hyp/nvhe/pkvm.c +5 -1
index 459bd9eb7e4bc..57e2eef6d7426 100644
@@ -432,7 +432,11 @@ 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);
+	if (kvm_vm_is_protected(host_kvm))
+		hyp_vm->kvm.arch.vm_flavor = VM_PROTECTED_PKVM;
+	else
+		hyp_vm->kvm.arch.vm_flavor = VM_PKVM;
+
  	hyp_vm->kvm.arch.flags = 0;
  	pkvm_init_features_from_host(hyp_vm, host_kvm);
 
arch/arm64/kvm/mmu.c +1 -1
index 9ba86450fe4af..0f4e8b71fa85d 100644
@@ -2624,7 +2624,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)) {
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);
  	}
#22 lore raw

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

Quoted 111 lines · Gavin Shan
Quoted 70 lines · 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.

With that, add wrappers for checking the "type" of a VM and replace the existing users with the new wrappers.

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.

Adds __VM_PROTECTED marker vm flavor to generalize kvm_vm_is_protected() to predicate all confidential guests running on KVM. In later patches, we would add Realm VMs, which would also be classified as protected.

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. To tighten this init_pkvm_hyp_vm() restricts the hyp copy of the vm_flavor to the ones it supports.

---
Changes since v18:
  - Merge the __VM_PROTECTED marker and the widening of kvm_vm_is_protected() to this patch.
  - Merge the use of kvm_vm_is_unprotected_pkvm() for !kvm_vm_is_protected() given the scope changes here.
  - Drop Fuad's review tag, as this patch has multiple merges
  - Restrict the VM flavors to the supported types in init_pkvm_hyp_vm().
  - Drop kvm_vm_hyp_is_pkvm() and revert to is_protected_kvm_enabled()
  - Use is_protected_kvm_enabled() to make the pKVM guest flavor checks.
  - s/PKVM/pKVM for commit descriptions too

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 | 22 +++++++++++++++++++--- arch/arm64/include/asm/kvm_pkvm.h |  4 ++--
  arch/arm64/kvm/arm.c              | 31 ++++++++++++++++++++++++++----- arch/arm64/kvm/handle_exit.c      |  2 +- arch/arm64/kvm/hyp/nvhe/pkvm.c    |  6 +++++- arch/arm64/kvm/mmu.c              |  2 +-
  arch/arm64/kvm/pkvm.c             |  6 ++----

Quoted 2 lines
  7 files changed, 56 insertions(+), 17 deletions(-)

This causes broken nVHE mode. I applied PATCH[01-05] to v7.3.rc4 whose head commit is f0100363d8c3, and kselftests/kvm/guest_print causes host crash (as below). I don't see the crash if only PATCH[01-04] are applied.

host$ cat /proc/cmdline | grep kvm-arm\.mode
BOOT_IMAGE=(hd0,gpt2)/vmlinuz-7.3.0-rc4-gavin+ root=/dev/mapper/rhel_nvidia--grace--hopper--01-root ro crashkernel=2G-4G:406M,4G-64G:470M,64G-:726M rd.lvm.lv=rhel_nvidia-grace-hopper-01/root rd.lvm.lv=rhel_nvidia-grace-hopper-01/swap video=simplefb:off kvm-arm.mode=nvhe

host$ cd linux/tools/testing/selftests/kvm
host$ ./guest_print_test
Random seed: 0x193a0ed3
[  192.754328] kvm [6674]: nVHE hyp panic at: [<ffff8000811da044>] __kvm_nvhe___timer_enable_traps+0x4/0x160! [  192.754338] kvm [6674]: nVHE call trace:
[  192.754339] kvm [6674]:  [<ffff8000811dcf34>] __kvm_nvhe_hyp_panic+0xb4/0xe0
[  192.754342] kvm [6674]:  [<ffff8000811dcba4>] __kvm_nvhe___kvm_vcpu_run+0x164/0x440
[  192.754344] kvm [6674]:  [<ffff8000811dfe90>] __kvm_nvhe_handle___kvm_vcpu_run+0x40/0x1f0
[  192.754346] kvm [6674]:  [<ffff8000811e0238>] __kvm_nvhe_handle_trap+0x158/0x280
[  192.754347] kvm [6674]:  [<ffff8000811dd8fc>] __kvm_nvhe___skip_pauth_save+0x4/0x4
[  192.754348] kvm [6674]: ---[ end nVHE call trace ]---
[  192.754350] Code: d2818002 17fffff7 d503201f f9400001 (b94a6821)
[  192.754350] kvm [6674]: Hyp Offset: 0xfffeb0d7fe2e0000
[  192.754351] Kernel panic - not syncing: HYP panic:
[  192.754351] PS:834003c9 PC:0000cf2882efa044 ESR:0000000096000004
[  192.754351] FAR:ffff00009b286a68 HPFAR:8000000000000000 PAR:1d00ec7edbadc8de
[  192.754351] VCPU:0000cf011848a350
[  192.845154] CPU: 12 UID: 0 PID: 6674 Comm: guest_print_tes Kdump: loaded Not tainted 7.3.0-rc4-gavin+ #7 PREEMPT(full) [  192.856182] Hardware name:  GH200 P5042, BIOS 02.04.01 20250422
[  192.862231] Call trace:
[  192.864725]  show_stack+0x20/0x38 (C)
[  192.868471]  dump_stack_lvl+0x88/0xb8
[  192.872215]  dump_stack+0x18/0x30
[  192.875598]  vpanic+0x280/0x498
[  192.878806]  panic+0x68/0x70
[  192.881745]  nvhe_hyp_panic_handler+0x184/0x190
[  192.886372]  kvm_arm_vcpu_enter_exit+0x24/0x100
[  192.891003]  kvm_arch_vcpu_ioctl_run+0x254/0x7c0
[  192.895726]  kvm_vcpu_ioctl+0x174/0xb40
[  192.899645]  __arm64_sys_ioctl+0xb0/0x120

Quoted 9 lines

[  192.903745]  invoke_syscall.constprop.0+0xa8/0x100 [  192.908639]  do_el0_svc+0xb8/0xe0
[  192.912022]  el0_svc+0x48/0x1f8
[  192.915228]  el0t_64_sync_handler+0xa0/0xe8 [  192.919500]  el0t_64_sync+0x1ac/0x1b0
[  192.923243] SMP: stopping secondary CPUs
[  192.927653] Starting crashdump kernel...
[  192.931658] Bye!

With the following changes applied after PATCH[01-05] on top of v7.3.rc4, the crash is avoided.

In arch/arm64/include/asm/kvm_host.h:

-#define kvm_vm_is_protected(kvm) ((kvm)->arch.vm_flavor >= __VM_PROTECTED)
+#define kvm_vm_is_protected(kvm) \
+ (is_protected_kvm_enabled() && (kvm)->arch.vm_flavor >= __VM_PROTECTED)

Thanks,
Gavin

Quoted 186 lines
Quoted 185 lines
arch/arm64/include/asm/kvm_host.h +0 -1
index 286489a69dff5..9b1cf9c59e81f 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,19 @@ enum fgt_group_id {

      __NR_FGT_GROUP_IDS__
  };

arch/arm64/include/asm/kvm_host.h +9 -0
+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,
+};
+

  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

arch/arm64/include/asm/kvm_host.h +9 -2
@@ -1504,10 +1513,17 @@ 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)
   #define vcpu_is_protected(vcpu)        kvm_vm_is_protected((vcpu)->kvm)
+#define kvm_vm_is_protected_pkvm(kvm)        \
+    (is_protected_kvm_enabled() && ((kvm)->arch.vm_flavor == VM_PROTECTED_PKVM))
+#define vcpu_is_protected_pkvm(vcpu)    kvm_vm_is_protected_pkvm(vcpu->kvm)
+
+#define kvm_vm_is_unprotected_pkvm(kvm)        \
+    (is_protected_kvm_enabled() && ((kvm)->arch.vm_flavor == VM_PKVM))
+
+
   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_pkvm.h +1 -1
index 54a618d887fa4..e4ea80711bec6 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);
@@ -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 +26 -5
index db36815630790..8c784b266a8e8 100644
@@ -214,6 +214,26 @@ static int kvm_arm_default_max_vcpus(void)
       return vgic_present ? kvm_vgic_get_max_vcpus() : KVM_MAX_VCPUS;
   }
+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
@@ -236,6 +256,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);
@@ -257,12 +281,9 @@ int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)
            * 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);
@@ -985,7 +1006,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;
       /*
arch/arm64/kvm/hyp/nvhe/pkvm.c +5 -1
index 459bd9eb7e4bc..57e2eef6d7426 100644
@@ -432,7 +432,11 @@ 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);
+    if (kvm_vm_is_protected(host_kvm))
+        hyp_vm->kvm.arch.vm_flavor = VM_PROTECTED_PKVM;
+    else
+        hyp_vm->kvm.arch.vm_flavor = VM_PKVM;
+
       hyp_vm->kvm.arch.flags = 0;
       pkvm_init_features_from_host(hyp_vm, host_kvm);
arch/arm64/kvm/mmu.c +1 -1
index 9ba86450fe4af..0f4e8b71fa85d 100644
@@ -2624,7 +2624,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)) {

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);
       }
author reviewer
#23 lore raw

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

Quoted 125 lines · Gavin Shan
Quoted 111 lines · Gavin Shan
Quoted 70 lines · 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.

With that, add wrappers for checking the "type" of a VM and replace the existing users with the new wrappers.

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.

Adds __VM_PROTECTED marker vm flavor to generalize kvm_vm_is_protected() to predicate all confidential guests running on KVM. In later patches, we would add Realm VMs, which would also be classified as protected.

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. To tighten this init_pkvm_hyp_vm() restricts the hyp copy of the vm_flavor to the ones it supports.

---
Changes since v18:
  - Merge the __VM_PROTECTED marker and the widening of kvm_vm_is_protected() to this patch.
  - Merge the use of kvm_vm_is_unprotected_pkvm() for ! kvm_vm_is_protected() given the scope changes here.
  - Drop Fuad's review tag, as this patch has multiple merges
  - Restrict the VM flavors to the supported types in init_pkvm_hyp_vm().
  - Drop kvm_vm_hyp_is_pkvm() and revert to is_protected_kvm_enabled()
  - Use is_protected_kvm_enabled() to make the pKVM guest flavor checks.
  - s/PKVM/pKVM for commit descriptions too

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 | 22 +++++++++++++++++++--- arch/arm64/include/asm/kvm_pkvm.h |  4 ++--
  arch/arm64/kvm/arm.c              | 31 ++++++++++++++++++++++++++----- arch/arm64/kvm/handle_exit.c      |  2 +- arch/arm64/kvm/hyp/nvhe/pkvm.c    |  6 +++++- arch/arm64/kvm/mmu.c              |  2 +-
  arch/arm64/kvm/pkvm.c             |  6 ++----

Quoted 2 lines
  7 files changed, 56 insertions(+), 17 deletions(-)

This causes broken nVHE mode. I applied PATCH[01-05] to v7.3.rc4 whose head commit is f0100363d8c3, and kselftests/kvm/guest_print causes host crash (as below). I don't see the crash if only PATCH[01-04] are applied.

host$ cat /proc/cmdline | grep kvm-arm\.mode
BOOT_IMAGE=(hd0,gpt2)/vmlinuz-7.3.0-rc4-gavin+ root=/dev/mapper/ rhel_nvidia--grace--hopper--01-root ro crashkernel=2G-4G:406M,4G-64G:470M,64G-:726M rd.lvm.lv=rhel_nvidia- grace-hopper-01/root rd.lvm.lv=rhel_nvidia-grace-hopper-01/swap video=simplefb:off kvm-arm.mode=nvhe

host$ cd linux/tools/testing/selftests/kvm
host$ ./guest_print_test
Random seed: 0x193a0ed3
[  192.754328] kvm [6674]: nVHE hyp panic at: [<ffff8000811da044>] __kvm_nvhe___timer_enable_traps+0x4/0x160! [  192.754338] kvm [6674]: nVHE call trace:
[  192.754339] kvm [6674]:  [<ffff8000811dcf34>] __kvm_nvhe_hyp_panic+0xb4/0xe0
[  192.754342] kvm [6674]:  [<ffff8000811dcba4>] __kvm_nvhe___kvm_vcpu_run+0x164/0x440
[  192.754344] kvm [6674]:  [<ffff8000811dfe90>] __kvm_nvhe_handle___kvm_vcpu_run+0x40/0x1f0
[  192.754346] kvm [6674]:  [<ffff8000811e0238>] __kvm_nvhe_handle_trap+0x158/0x280
[  192.754347] kvm [6674]:  [<ffff8000811dd8fc>] __kvm_nvhe___skip_pauth_save+0x4/0x4
[  192.754348] kvm [6674]: ---[ end nVHE call trace ]---
[  192.754350] Code: d2818002 17fffff7 d503201f f9400001 (b94a6821)
[  192.754350] kvm [6674]: Hyp Offset: 0xfffeb0d7fe2e0000
[  192.754351] Kernel panic - not syncing: HYP panic:
[  192.754351] PS:834003c9 PC:0000cf2882efa044 ESR:0000000096000004
[  192.754351] FAR:ffff00009b286a68 HPFAR:8000000000000000 PAR:1d00ec7edbadc8de
[  192.754351] VCPU:0000cf011848a350
[  192.845154] CPU: 12 UID: 0 PID: 6674 Comm: guest_print_tes Kdump: loaded Not tainted 7.3.0-rc4-gavin+ #7 PREEMPT(full) [  192.856182] Hardware name:  GH200 P5042, BIOS 02.04.01 20250422
[  192.862231] Call trace:
[  192.864725]  show_stack+0x20/0x38 (C)
[  192.868471]  dump_stack_lvl+0x88/0xb8
[  192.872215]  dump_stack+0x18/0x30
[  192.875598]  vpanic+0x280/0x498
[  192.878806]  panic+0x68/0x70
[  192.881745]  nvhe_hyp_panic_handler+0x184/0x190
[  192.886372]  kvm_arm_vcpu_enter_exit+0x24/0x100
[  192.891003]  kvm_arch_vcpu_ioctl_run+0x254/0x7c0
[  192.895726]  kvm_vcpu_ioctl+0x174/0xb40
[  192.899645]  __arm64_sys_ioctl+0xb0/0x120

Quoted 9 lines

[  192.903745]  invoke_syscall.constprop.0+0xa8/0x100 [  192.908639]  do_el0_svc+0xb8/0xe0
[  192.912022]  el0_svc+0x48/0x1f8
[  192.915228]  el0t_64_sync_handler+0xa0/0xe8 [  192.919500]  el0t_64_sync+0x1ac/0x1b0
[  192.923243] SMP: stopping secondary CPUs
[  192.927653] Starting crashdump kernel...
[  192.931658] Bye!

With the following changes applied after PATCH[01-05] on top of v7.3.rc4, the crash is avoided.

Quoted 7 lines

In arch/arm64/include/asm/kvm_host.h:

-#define kvm_vm_is_protected(kvm)       ((kvm)->arch.vm_flavor >= __VM_PROTECTED)
+#define kvm_vm_is_protected(kvm)       \
+       (is_protected_kvm_enabled() && (kvm)->arch.vm_flavor >= __VM_PROTECTED)

That only papers over the problem. We can't use the vcpu_is_* constructs from nvhe hyp, without converting the vcpu->kvm to the hyp address, before accessing it. The fix is a bit more involved. One option is to define the vcpu_is_* helpers only for the !NVHE hyp code in the kvm_host.h (to avoid pulling in the asm/kvm_mmu.h in to kvm_host.h and then make a mess with header dependencies) and define the NVHE version in asm/kvm_hyp.h. Something like :

arch/arm64/include/asm/kvm_host.h +0 -0
index 46a7f6c1e426c..4016d09ed39c3 100644
@@ -1545,11 +1545,14 @@ 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)
+
+#ifndef __KVM_NVHE_HYPERVISOR__
#define vcpu_is_protected(vcpu)
kvm_vm_is_protected((vcpu)->kvm)
+#define vcpu_is_protected_pkvm(vcpu) kvm_vm_is_protected_pkvm((vcpu)->kvm) +#endif

 #define kvm_vm_is_protected_pkvm(kvm)          \
        (is_protected_kvm_enabled() && ((kvm)->arch.vm_flavor == 

VM_PROTECTED_PKVM))
-#define vcpu_is_protected_pkvm(vcpu) kvm_vm_is_protected_pkvm(vcpu->kvm)

 #define kvm_vm_is_unprotected_pkvm(kvm)                \
        (is_protected_kvm_enabled() && ((kvm)->arch.vm_flavor == VM_PKVM))
arch/arm64/include/asm/kvm_hyp.h +13 -0
index 4974492744cc8..3bf87e81af430 100644
@@ -137,6 +137,19 @@ int __pkvm_init(phys_addr_t phys, unsigned long size, unsigned long *per_cpu_bas
 void __noreturn __host_enter(struct kvm_cpu_context *host_ctxt);
 #endif
 
+#ifdef __KVM_NVHE_HYPERVISOR__
+#define vcpu_is_protected(vcpu) 
       \
+       ({                                                              \
+               struct kvm *__kvm = READ_ONCE((vcpu)->kvm);             \
+               __kvm && kvm_vm_is_protected((kern_hyp_va(__kvm)));     \
+       })
+#define vcpu_is_protected_pkvm(vcpu)                                   \
+       ({                                                              \
+               struct kvm *__kvm = READ_ONCE((vcpu)->kvm);             \
+               __kvm && kvm_vm_is_protected_pkvm((kern_hyp_va(__kvm)));\
+       })
+#endif
+
 
 

Or explicitly convert all nvhe accessors to a new "nvhe_vcpu_is_protected"

The second one sounds like a better option to me.

Cheers
Suzuki

Quoted 206 lines

Thanks,
Gavin

Quoted 200 lines
Quoted 199 lines
arch/arm64/include/asm/kvm_host.h +0 -1
index 286489a69dff5..9b1cf9c59e81f 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,19 @@ enum fgt_group_id {

      __NR_FGT_GROUP_IDS__
  };

arch/arm64/include/asm/kvm_host.h +9 -0
+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,
+};
+

  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

arch/arm64/include/asm/kvm_host.h +9 -2
@@ -1504,10 +1513,17 @@ 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)
   #define vcpu_is_protected(vcpu)        kvm_vm_is_protected((vcpu)- 
 >kvm)
+#define kvm_vm_is_protected_pkvm(kvm)        \
+    (is_protected_kvm_enabled() && ((kvm)->arch.vm_flavor == 
 VM_PROTECTED_PKVM))
+#define vcpu_is_protected_pkvm(vcpu)    
 kvm_vm_is_protected_pkvm(vcpu->kvm)
+
+#define kvm_vm_is_unprotected_pkvm(kvm)        \
+    (is_protected_kvm_enabled() && ((kvm)->arch.vm_flavor == VM_PKVM))
+
+
   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_pkvm.h +1 -1
index 54a618d887fa4..e4ea80711bec6 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);
@@ -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 +26 -5
index db36815630790..8c784b266a8e8 100644
@@ -214,6 +214,26 @@ static int kvm_arm_default_max_vcpus(void)
       return vgic_present ? kvm_vgic_get_max_vcpus() : KVM_MAX_VCPUS;
   }
+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
@@ -236,6 +256,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);
@@ -257,12 +281,9 @@ int kvm_arch_init_vm(struct kvm *kvm, unsigned 
 long type)
            * 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);
@@ -985,7 +1006,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;
       /*
arch/arm64/kvm/hyp/nvhe/pkvm.c +5 -1
index 459bd9eb7e4bc..57e2eef6d7426 100644
@@ -432,7 +432,11 @@ 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);
+    if (kvm_vm_is_protected(host_kvm))
+        hyp_vm->kvm.arch.vm_flavor = VM_PROTECTED_PKVM;
+    else
+        hyp_vm->kvm.arch.vm_flavor = VM_PKVM;
+
       hyp_vm->kvm.arch.flags = 0;
       pkvm_init_features_from_host(hyp_vm, host_kvm);
arch/arm64/kvm/mmu.c +1 -1
index 9ba86450fe4af..0f4e8b71fa85d 100644
@@ -2624,7 +2624,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)) {

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);
       }
#24 lore raw

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

Quoted 187 lines · Suzuki K Poulose
Quoted 127 lines · Gavin Shan
Quoted 111 lines · Gavin Shan
Quoted 70 lines · 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.

With that, add wrappers for checking the "type" of a VM and replace the existing users with the new wrappers.

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.

Adds __VM_PROTECTED marker vm flavor to generalize kvm_vm_is_protected() to predicate all confidential guests running on KVM. In later patches, we would add Realm VMs, which would also be classified as protected.

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. To tighten this init_pkvm_hyp_vm() restricts the hyp copy of the vm_flavor to the ones it supports.

---
Changes since v18:
  - Merge the __VM_PROTECTED marker and the widening of kvm_vm_is_protected() to this patch.
  - Merge the use of kvm_vm_is_unprotected_pkvm() for ! kvm_vm_is_protected() given the scope changes here.
  - Drop Fuad's review tag, as this patch has multiple merges
  - Restrict the VM flavors to the supported types in init_pkvm_hyp_vm().
  - Drop kvm_vm_hyp_is_pkvm() and revert to is_protected_kvm_enabled()
  - Use is_protected_kvm_enabled() to make the pKVM guest flavor checks.
  - s/PKVM/pKVM for commit descriptions too

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 | 22 +++++++++++++++++++--- arch/arm64/include/asm/kvm_pkvm.h |  4 ++--
  arch/arm64/kvm/arm.c              | 31 ++++++++++++++++++++++++++----- arch/arm64/kvm/handle_exit.c      |  2 +- arch/arm64/kvm/hyp/nvhe/pkvm.c    |  6 +++++- arch/arm64/kvm/mmu.c              |  2 +-
  arch/arm64/kvm/pkvm.c             |  6 ++----

Quoted 2 lines
  7 files changed, 56 insertions(+), 17 deletions(-)

This causes broken nVHE mode. I applied PATCH[01-05] to v7.3.rc4 whose head commit is f0100363d8c3, and kselftests/kvm/guest_print causes host crash (as below). I don't see the crash if only PATCH[01-04] are applied.

host$ cat /proc/cmdline | grep kvm-arm\.mode
BOOT_IMAGE=(hd0,gpt2)/vmlinuz-7.3.0-rc4-gavin+ root=/dev/mapper/ rhel_nvidia--grace--hopper--01-root ro crashkernel=2G-4G:406M,4G-64G:470M,64G-:726M rd.lvm.lv=rhel_nvidia- grace-hopper-01/root rd.lvm.lv=rhel_nvidia-grace-hopper-01/swap video=simplefb:off kvm-arm.mode=nvhe

host$ cd linux/tools/testing/selftests/kvm
host$ ./guest_print_test
Random seed: 0x193a0ed3
[  192.754328] kvm [6674]: nVHE hyp panic at: [<ffff8000811da044>] __kvm_nvhe___timer_enable_traps+0x4/0x160! [  192.754338] kvm [6674]: nVHE call trace:
[  192.754339] kvm [6674]:  [<ffff8000811dcf34>] __kvm_nvhe_hyp_panic+0xb4/0xe0
[  192.754342] kvm [6674]:  [<ffff8000811dcba4>] __kvm_nvhe___kvm_vcpu_run+0x164/0x440
[  192.754344] kvm [6674]:  [<ffff8000811dfe90>] __kvm_nvhe_handle___kvm_vcpu_run+0x40/0x1f0
[  192.754346] kvm [6674]:  [<ffff8000811e0238>] __kvm_nvhe_handle_trap+0x158/0x280
[  192.754347] kvm [6674]:  [<ffff8000811dd8fc>] __kvm_nvhe___skip_pauth_save+0x4/0x4
[  192.754348] kvm [6674]: ---[ end nVHE call trace ]---
[  192.754350] Code: d2818002 17fffff7 d503201f f9400001 (b94a6821)
[  192.754350] kvm [6674]: Hyp Offset: 0xfffeb0d7fe2e0000
[  192.754351] Kernel panic - not syncing: HYP panic:
[  192.754351] PS:834003c9 PC:0000cf2882efa044 ESR:0000000096000004
[  192.754351] FAR:ffff00009b286a68 HPFAR:8000000000000000 PAR:1d00ec7edbadc8de
[  192.754351] VCPU:0000cf011848a350
[  192.845154] CPU: 12 UID: 0 PID: 6674 Comm: guest_print_tes Kdump: loaded Not tainted 7.3.0-rc4-gavin+ #7 PREEMPT(full) [  192.856182] Hardware name:  GH200 P5042, BIOS 02.04.01 20250422
[  192.862231] Call trace:
[  192.864725]  show_stack+0x20/0x38 (C)
[  192.868471]  dump_stack_lvl+0x88/0xb8
[  192.872215]  dump_stack+0x18/0x30
[  192.875598]  vpanic+0x280/0x498
[  192.878806]  panic+0x68/0x70
[  192.881745]  nvhe_hyp_panic_handler+0x184/0x190
[  192.886372]  kvm_arm_vcpu_enter_exit+0x24/0x100
[  192.891003]  kvm_arch_vcpu_ioctl_run+0x254/0x7c0
[  192.895726]  kvm_vcpu_ioctl+0x174/0xb40
[  192.899645]  __arm64_sys_ioctl+0xb0/0x120

Quoted 9 lines

[  192.903745]  invoke_syscall.constprop.0+0xa8/0x100 [  192.908639]  do_el0_svc+0xb8/0xe0
[  192.912022]  el0_svc+0x48/0x1f8
[  192.915228]  el0t_64_sync_handler+0xa0/0xe8 [  192.919500]  el0t_64_sync+0x1ac/0x1b0
[  192.923243] SMP: stopping secondary CPUs
[  192.927653] Starting crashdump kernel...
[  192.931658] Bye!

With the following changes applied after PATCH[01-05] on top of v7.3.rc4, the crash is avoided.

In arch/arm64/include/asm/kvm_host.h:

Review

Quoted 3 lines
(quoted diff) +2 -1
-#define kvm_vm_is_protected(kvm)       ((kvm)->arch.vm_flavor >= __VM_PROTECTED)
+#define kvm_vm_is_protected(kvm)       \
+       (is_protected_kvm_enabled() && (kvm)->arch.vm_flavor >= __VM_PROTECTED)

That only papers over the problem. We can't use the vcpu_is_* constructs from nvhe hyp, without converting the vcpu->kvm to the hyp address, before accessing it. The fix is a bit more involved. One option is to define the vcpu_is_* helpers only for the !NVHE hyp code in the kvm_host.h (to avoid pulling in the asm/kvm_mmu.h in to kvm_host.h and then make a mess with header dependencies) and define the NVHE version in asm/kvm_hyp.h. Something like :

arch/arm64/include/asm/kvm_host.h +4 -1
index 46a7f6c1e426c..4016d09ed39c3 100644
@@ -1545,11 +1545,14 @@ 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)
+
+#ifndef __KVM_NVHE_HYPERVISOR__
  #define vcpu_is_protected(vcpu) kvm_vm_is_protected((vcpu)->kvm)
+#define vcpu_is_protected_pkvm(vcpu) kvm_vm_is_protected_pkvm((vcpu)->kvm)
+#endif
 
  #define kvm_vm_is_protected_pkvm(kvm)          \
         (is_protected_kvm_enabled() && ((kvm)->arch.vm_flavor == VM_PROTECTED_PKVM))
-#define vcpu_is_protected_pkvm(vcpu)   kvm_vm_is_protected_pkvm(vcpu->kvm)
 
  #define kvm_vm_is_unprotected_pkvm(kvm)                \
         (is_protected_kvm_enabled() && ((kvm)->arch.vm_flavor == VM_PKVM))
arch/arm64/include/asm/kvm_hyp.h +13 -0
index 4974492744cc8..3bf87e81af430 100644
@@ -137,6 +137,19 @@ int __pkvm_init(phys_addr_t phys, unsigned long size, unsigned long *per_cpu_bas
  void __noreturn __host_enter(struct kvm_cpu_context *host_ctxt);
  #endif
 
+#ifdef __KVM_NVHE_HYPERVISOR__
+#define vcpu_is_protected(vcpu)        \
+       ({                                                              \
+               struct kvm *__kvm = READ_ONCE((vcpu)->kvm);             \
+               __kvm && kvm_vm_is_protected((kern_hyp_va(__kvm)));     \
+       })
+#define vcpu_is_protected_pkvm(vcpu)                                   \
+       ({                                                              \
+               struct kvm *__kvm = READ_ONCE((vcpu)->kvm);             \
+               __kvm && kvm_vm_is_protected_pkvm((kern_hyp_va(__kvm)));\
+       })
+#endif
+
 
 

Or explicitly convert all nvhe accessors to a new "nvhe_vcpu_is_protected"

Quoted 2 lines

The second one sounds like a better option to me.

We just need to include "asm/kvm_mmu.h" to "arch/arm64/mmio.c". With it, we can have unified functions (macros) in kvm_host.h to accomodate all cases, like below.

arch/arm64/include/asm/kvm_host.h +27 -2
index 9b1cf9c59e81..b9c9a9f203f9 100644
@@ -1514,11 +1514,36 @@ 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)
-#define vcpu_is_protected(vcpu)                kvm_vm_is_protected((vcpu)->kvm)
+#define vcpu_is_protected(vcpu)                                                \
+       ({                                                              \
+               struct kvm *__kvm = READ_ONCE((vcpu)->kvm);             \
+               bool __protected = false;                               \
+                                                                       \
+               if (__kvm) {                                            \
+                       if (is_nvhe_hyp_code())                         \
+                               __kvm = kern_hyp_va(__kvm);             \
+                       __protected = kvm_vm_is_protected(__kvm);       \
+               }                                                       \
+                                                                       \
+               __protected;                                            \
+        })
+
 
 #define kvm_vm_is_protected_pkvm(kvm)          \
        (is_protected_kvm_enabled() && ((kvm)->arch.vm_flavor == VM_PROTECTED_PKVM))
-#define vcpu_is_protected_pkvm(vcpu)   kvm_vm_is_protected_pkvm(vcpu->kvm)
+#define vcpu_is_protected_pkvm(vcpu)                                   \
+       ({                                                              \
+               struct kvm *__kvm = READ_ONCE((vcpu)->kvm);             \
+               bool __protected = false;                               \
+                                                                       \
+               if (__kvm) {                                            \
+                       if (is_nvhe_hyp_code())                         \
+                               __kvm = kern_hyp_va(__kvm);             \
+                       __protected = kvm_vm_is_protected_pkvm(__kvm);  \
+               }                                                       \
+                                                                       \
+               __protected;                                            \
+        })
 
 #define kvm_vm_is_unprotected_pkvm(kvm)                \
        (is_protected_kvm_enabled() && ((kvm)->arch.vm_flavor == VM_PKVM))
arch/arm64/kvm/mmio.c +1 -0
index d1c3a352d5a2..ab1d2fef9a52 100644
@@ -6,6 +6,7 @@
 
 #include <linux/kvm_host.h>
 #include <asm/kvm_emulate.h>
+#include <asm/kvm_mmu.h>
 #include <trace/events/kvm.h>
 
 #include "trace.h"

Thanks,
Gavin

#25 lore raw

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

Quoted 226 lines · Gavin Shan
Quoted 187 lines · Suzuki K Poulose
Quoted 127 lines · Gavin Shan
Quoted 111 lines · Gavin Shan
Quoted 70 lines · 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.

With that, add wrappers for checking the "type" of a VM and replace the existing users with the new wrappers.

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.

Adds __VM_PROTECTED marker vm flavor to generalize kvm_vm_is_protected() to predicate all confidential guests running on KVM. In later patches, we would add Realm VMs, which would also be classified as protected.

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. To tighten this init_pkvm_hyp_vm() restricts the hyp copy of the vm_flavor to the ones it supports.

---
Changes since v18:
  - Merge the __VM_PROTECTED marker and the widening of kvm_vm_is_protected() to this patch.
  - Merge the use of kvm_vm_is_unprotected_pkvm() for ! kvm_vm_is_protected() given the scope changes here.
  - Drop Fuad's review tag, as this patch has multiple merges
  - Restrict the VM flavors to the supported types in init_pkvm_hyp_vm().
  - Drop kvm_vm_hyp_is_pkvm() and revert to is_protected_kvm_enabled()
  - Use is_protected_kvm_enabled() to make the pKVM guest flavor checks.
  - s/PKVM/pKVM for commit descriptions too

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 | 22 +++++++++++++++++++--- arch/arm64/include/asm/kvm_pkvm.h |  4 ++--
  arch/arm64/kvm/arm.c              | 31 ++++++++++++++++++++++++++----- arch/arm64/kvm/handle_exit.c      |  2 +- arch/arm64/kvm/hyp/nvhe/pkvm.c    |  6 +++++- arch/arm64/kvm/mmu.c              |  2 +-
  arch/arm64/kvm/pkvm.c             |  6 ++----

Quoted 2 lines
  7 files changed, 56 insertions(+), 17 deletions(-)

This causes broken nVHE mode. I applied PATCH[01-05] to v7.3.rc4 whose head commit is f0100363d8c3, and kselftests/kvm/guest_print causes host crash (as below). I don't see the crash if only PATCH[01-04] are applied.

host$ cat /proc/cmdline | grep kvm-arm\.mode
BOOT_IMAGE=(hd0,gpt2)/vmlinuz-7.3.0-rc4-gavin+ root=/dev/mapper/ rhel_nvidia--grace--hopper--01-root ro crashkernel=2G-4G:406M,4G-64G:470M,64G-:726M rd.lvm.lv=rhel_nvidia- grace-hopper-01/root rd.lvm.lv=rhel_nvidia-grace-hopper-01/swap video=simplefb:off kvm-arm.mode=nvhe

host$ cd linux/tools/testing/selftests/kvm
host$ ./guest_print_test
Random seed: 0x193a0ed3
[  192.754328] kvm [6674]: nVHE hyp panic at: [<ffff8000811da044>] __kvm_nvhe___timer_enable_traps+0x4/0x160! [  192.754338] kvm [6674]: nVHE call trace:
[  192.754339] kvm [6674]:  [<ffff8000811dcf34>] __kvm_nvhe_hyp_panic+0xb4/0xe0
[  192.754342] kvm [6674]:  [<ffff8000811dcba4>] __kvm_nvhe___kvm_vcpu_run+0x164/0x440
[  192.754344] kvm [6674]:  [<ffff8000811dfe90>] __kvm_nvhe_handle___kvm_vcpu_run+0x40/0x1f0
[  192.754346] kvm [6674]:  [<ffff8000811e0238>] __kvm_nvhe_handle_trap+0x158/0x280
[  192.754347] kvm [6674]:  [<ffff8000811dd8fc>] __kvm_nvhe___skip_pauth_save+0x4/0x4
[  192.754348] kvm [6674]: ---[ end nVHE call trace ]---
[  192.754350] Code: d2818002 17fffff7 d503201f f9400001 (b94a6821)
[  192.754350] kvm [6674]: Hyp Offset: 0xfffeb0d7fe2e0000
[  192.754351] Kernel panic - not syncing: HYP panic:
[  192.754351] PS:834003c9 PC:0000cf2882efa044 ESR:0000000096000004
[  192.754351] FAR:ffff00009b286a68 HPFAR:8000000000000000 PAR:1d00ec7edbadc8de
[  192.754351] VCPU:0000cf011848a350
[  192.845154] CPU: 12 UID: 0 PID: 6674 Comm: guest_print_tes Kdump: loaded Not tainted 7.3.0-rc4-gavin+ #7 PREEMPT(full) [  192.856182] Hardware name:  GH200 P5042, BIOS 02.04.01 20250422
[  192.862231] Call trace:
[  192.864725]  show_stack+0x20/0x38 (C)
[  192.868471]  dump_stack_lvl+0x88/0xb8
[  192.872215]  dump_stack+0x18/0x30
[  192.875598]  vpanic+0x280/0x498
[  192.878806]  panic+0x68/0x70
[  192.881745]  nvhe_hyp_panic_handler+0x184/0x190
[  192.886372]  kvm_arm_vcpu_enter_exit+0x24/0x100
[  192.891003]  kvm_arch_vcpu_ioctl_run+0x254/0x7c0
[  192.895726]  kvm_vcpu_ioctl+0x174/0xb40
[  192.899645]  __arm64_sys_ioctl+0xb0/0x120

Quoted 9 lines

[  192.903745]  invoke_syscall.constprop.0+0xa8/0x100 [  192.908639]  do_el0_svc+0xb8/0xe0
[  192.912022]  el0_svc+0x48/0x1f8
[  192.915228]  el0t_64_sync_handler+0xa0/0xe8 [  192.919500]  el0t_64_sync+0x1ac/0x1b0
[  192.923243] SMP: stopping secondary CPUs
[  192.927653] Starting crashdump kernel...
[  192.931658] Bye!

With the following changes applied after PATCH[01-05] on top of v7.3.rc4, the crash is avoided.

In arch/arm64/include/asm/kvm_host.h:

Review

Quoted 3 lines
(quoted diff) +2 -1
-#define kvm_vm_is_protected(kvm)       ((kvm)->arch.vm_flavor >= __VM_PROTECTED)
+#define kvm_vm_is_protected(kvm)       \
+       (is_protected_kvm_enabled() && (kvm)->arch.vm_flavor >= __VM_PROTECTED)

That only papers over the problem. We can't use the vcpu_is_* constructs from nvhe hyp, without converting the vcpu->kvm to the hyp address, before accessing it. The fix is a bit more involved. One option is to define the vcpu_is_* helpers only for the !NVHE hyp code in the kvm_host.h (to avoid pulling in the asm/kvm_mmu.h in to kvm_host.h and then make a mess with header dependencies) and define the NVHE version in asm/kvm_hyp.h. Something like :

arch/arm64/include/asm/kvm_host.h +4 -1
index 46a7f6c1e426c..4016d09ed39c3 100644
@@ -1545,11 +1545,14 @@ 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)
+
+#ifndef __KVM_NVHE_HYPERVISOR__
   #define vcpu_is_protected(vcpu) kvm_vm_is_protected((vcpu)->kvm)
+#define vcpu_is_protected_pkvm(vcpu) kvm_vm_is_protected_pkvm((vcpu)->kvm)
+#endif
 
   #define kvm_vm_is_protected_pkvm(kvm)          \
          (is_protected_kvm_enabled() && ((kvm)->arch.vm_flavor == VM_PROTECTED_PKVM))
-#define vcpu_is_protected_pkvm(vcpu)   kvm_vm_is_protected_pkvm(vcpu->kvm)
 
   #define kvm_vm_is_unprotected_pkvm(kvm)                \
          (is_protected_kvm_enabled() && ((kvm)->arch.vm_flavor == VM_PKVM))
arch/arm64/include/asm/kvm_hyp.h +13 -0
index 4974492744cc8..3bf87e81af430 100644
@@ -137,6 +137,19 @@ int __pkvm_init(phys_addr_t phys, unsigned long size, unsigned long *per_cpu_bas
   void __noreturn __host_enter(struct kvm_cpu_context *host_ctxt);
   #endif
 
+#ifdef __KVM_NVHE_HYPERVISOR__
+#define vcpu_is_protected(vcpu)        \
+       ({                                                              \
+               struct kvm *__kvm = READ_ONCE((vcpu)->kvm);             \
+               __kvm && kvm_vm_is_protected((kern_hyp_va(__kvm)));     \
+       })
+#define vcpu_is_protected_pkvm(vcpu)                                   \
+       ({                                                              \
+               struct kvm *__kvm = READ_ONCE((vcpu)->kvm);             \
+               __kvm && kvm_vm_is_protected_pkvm((kern_hyp_va(__kvm)));\
+       })
+#endif
+
 
 

Or explicitly convert all nvhe accessors to a new "nvhe_vcpu_is_protected"

Quoted 2 lines

The second one sounds like a better option to me.

We just need to include "asm/kvm_mmu.h" to "arch/arm64/mmio.c". With it, we can have unified functions (macros) in kvm_host.h to accomodate all cases, like below.

arch/arm64/include/asm/kvm_host.h +18 -2
index 9b1cf9c59e81..b9c9a9f203f9 100644
@@ -1514,11 +1514,36 @@ 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)
-#define vcpu_is_protected(vcpu)                kvm_vm_is_protected((vcpu)->kvm)
+#define vcpu_is_protected(vcpu)                                                \
+       ({                                                              \
+               struct kvm *__kvm = READ_ONCE((vcpu)->kvm);             \
+               bool __protected = false;                               \
+                                                                       \
+               if (__kvm) {                                            \
+                       if (is_nvhe_hyp_code())                         \
+                               __kvm = kern_hyp_va(__kvm);             \
+                       __protected = kvm_vm_is_protected(__kvm);       \
+               }                                                       \
+                                                                       \
+               __protected;                                            \
+        })
+
 
  #define kvm_vm_is_protected_pkvm(kvm)          \
         (is_protected_kvm_enabled() && ((kvm)->arch.vm_flavor == VM_PROTECTED_PKVM))
-#define vcpu_is_protected_pkvm(vcpu)   kvm_vm_is_protected_pkvm(vcpu->kvm)
+#define vcpu_is_protected_pkvm(vcpu)                                   \
+       ({                                                              \
+               struct kvm *__kvm = READ_ONCE((vcpu)->kvm);             \
+               bool __protected = false;                               \

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

Quoted 10 lines
arch/arm64/include/asm/kvm_host.h +9 -0
+                                                                       \
+               if (__kvm) {                                            \
+                       if (is_nvhe_hyp_code())                         \
+                               __kvm = kern_hyp_va(__kvm);             \
+                       __protected = kvm_vm_is_protected_pkvm(__kvm);  \
+               }                                                       \
+                                                                       \
+               __protected;                                            \
+        })
 

Besides, vcpu_is_protected_pkvm() isn't used anywhere in branch "cca/cca-host/kvm-v19/integration", so it can be removed :)

Quoted 17 lines
arch/arm64/include/asm/kvm_host.h +0 -0
  #define kvm_vm_is_unprotected_pkvm(kvm)                \
         (is_protected_kvm_enabled() && ((kvm)->arch.vm_flavor == VM_PKVM))
arch/arm64/kvm/mmio.c +1 -0
index d1c3a352d5a2..ab1d2fef9a52 100644
@@ -6,6 +6,7 @@
 
  #include <linux/kvm_host.h>
  #include <asm/kvm_emulate.h>
+#include <asm/kvm_mmu.h>
  #include <trace/events/kvm.h>
 
  #include "trace.h"

Thanks,
Gavin

author reviewer
#26 lore raw

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

Quoted 254 lines · Gavin Shan
Quoted 242 lines · Gavin Shan
Quoted 198 lines · Suzuki K Poulose
Quoted 126 lines · Gavin Shan
Quoted 112 lines · Gavin Shan
Quoted 66 lines · 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.

With that, add wrappers for checking the "type" of a VM and replace the existing users with the new wrappers.

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.

Adds __VM_PROTECTED marker vm flavor to generalize kvm_vm_is_protected() to predicate all confidential guests running on KVM. In later patches, we would add Realm VMs, which would also be classified as protected.

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. To tighten this init_pkvm_hyp_vm() restricts the hyp copy of the vm_flavor to the ones it supports.

---
Changes since v18:
  - Merge the __VM_PROTECTED marker and the widening of kvm_vm_is_protected() to this patch.
  - Merge the use of kvm_vm_is_unprotected_pkvm() for ! kvm_vm_is_protected() given the scope changes here.
  - Drop Fuad's review tag, as this patch has multiple merges
  - Restrict the VM flavors to the supported types in init_pkvm_hyp_vm().
  - Drop kvm_vm_hyp_is_pkvm() and revert to is_protected_kvm_enabled()
  - Use is_protected_kvm_enabled() to make the pKVM guest flavor checks.
  - s/PKVM/pKVM for commit descriptions too

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 | 22 +++++++++++++++++++--- arch/arm64/include/asm/kvm_pkvm.h |  4 ++--
  arch/arm64/kvm/arm.c              | 31 +++++++++++++++++++++++++

Quoted 7 lines
+-----
  arch/arm64/kvm/handle_exit.c      |  2 +-
  arch/arm64/kvm/hyp/nvhe/pkvm.c    |  6 +++++-
  arch/arm64/kvm/mmu.c              |  2 +-
  arch/arm64/kvm/pkvm.c             |  6 ++----
  7 files changed, 56 insertions(+), 17 deletions(-)

This causes broken nVHE mode. I applied PATCH[01-05] to v7.3.rc4 whose head commit is f0100363d8c3, and kselftests/kvm/guest_print causes host crash (as below). I don't see the crash if only PATCH[01-04] are applied.

host$ cat /proc/cmdline | grep kvm-arm\.mode
BOOT_IMAGE=(hd0,gpt2)/vmlinuz-7.3.0-rc4-gavin+ root=/dev/mapper/ rhel_nvidia--grace--hopper--01-root ro crashkernel=2G-4G:406M,4G-64G:470M,64G-:726M rd.lvm.lv=rhel_nvidia- grace-hopper-01/root rd.lvm.lv=rhel_nvidia-grace-hopper-01/swap video=simplefb:off kvm-arm.mode=nvhe

host$ cd linux/tools/testing/selftests/kvm
host$ ./guest_print_test
Random seed: 0x193a0ed3
[  192.754328] kvm [6674]: nVHE hyp panic at: [<ffff8000811da044>] __kvm_nvhe___timer_enable_traps+0x4/0x160! [  192.754338] kvm [6674]: nVHE call trace:
[  192.754339] kvm [6674]:  [<ffff8000811dcf34>] __kvm_nvhe_hyp_panic+0xb4/0xe0
[  192.754342] kvm [6674]:  [<ffff8000811dcba4>] __kvm_nvhe___kvm_vcpu_run+0x164/0x440
[  192.754344] kvm [6674]:  [<ffff8000811dfe90>] __kvm_nvhe_handle___kvm_vcpu_run+0x40/0x1f0
[  192.754346] kvm [6674]:  [<ffff8000811e0238>] __kvm_nvhe_handle_trap+0x158/0x280
[  192.754347] kvm [6674]:  [<ffff8000811dd8fc>] __kvm_nvhe___skip_pauth_save+0x4/0x4
[  192.754348] kvm [6674]: ---[ end nVHE call trace ]---
[  192.754350] Code: d2818002 17fffff7 d503201f f9400001 (b94a6821)
[  192.754350] kvm [6674]: Hyp Offset: 0xfffeb0d7fe2e0000
[  192.754351] Kernel panic - not syncing: HYP panic:
[  192.754351] PS:834003c9 PC:0000cf2882efa044 ESR:0000000096000004
[  192.754351] FAR:ffff00009b286a68 HPFAR:8000000000000000 PAR:1d00ec7edbadc8de
[  192.754351] VCPU:0000cf011848a350
[  192.845154] CPU: 12 UID: 0 PID: 6674 Comm: guest_print_tes Kdump: loaded Not tainted 7.3.0-rc4-gavin+ #7 PREEMPT(full) [  192.856182] Hardware name:  GH200 P5042, BIOS 02.04.01 20250422
[  192.862231] Call trace:
[  192.864725]  show_stack+0x20/0x38 (C)
[  192.868471]  dump_stack_lvl+0x88/0xb8
[  192.872215]  dump_stack+0x18/0x30
[  192.875598]  vpanic+0x280/0x498
[  192.878806]  panic+0x68/0x70
[  192.881745]  nvhe_hyp_panic_handler+0x184/0x190
[  192.886372]  kvm_arm_vcpu_enter_exit+0x24/0x100
[  192.891003]  kvm_arch_vcpu_ioctl_run+0x254/0x7c0
[  192.895726]  kvm_vcpu_ioctl+0x174/0xb40
[  192.899645]  __arm64_sys_ioctl+0xb0/0x120

Quoted 9 lines

[  192.903745]  invoke_syscall.constprop.0+0xa8/0x100 [  192.908639]  do_el0_svc+0xb8/0xe0
[  192.912022]  el0_svc+0x48/0x1f8
[  192.915228]  el0t_64_sync_handler+0xa0/0xe8 [  192.919500]  el0t_64_sync+0x1ac/0x1b0
[  192.923243] SMP: stopping secondary CPUs
[  192.927653] Starting crashdump kernel...
[  192.931658] Bye!

With the following changes applied after PATCH[01-05] on top of v7.3.rc4, the crash is avoided.

Quoted 7 lines

In arch/arm64/include/asm/kvm_host.h:

-#define kvm_vm_is_protected(kvm)       ((kvm)->arch.vm_flavor >= __VM_PROTECTED)
+#define kvm_vm_is_protected(kvm)       \
+       (is_protected_kvm_enabled() && (kvm)->arch.vm_flavor >= __VM_PROTECTED)

That only papers over the problem. We can't use the vcpu_is_* constructs from nvhe hyp, without converting the vcpu->kvm to the hyp address, before accessing it. The fix is a bit more involved. One option is to define the vcpu_is_* helpers only for the !NVHE hyp code in the kvm_host.h (to avoid pulling in the asm/kvm_mmu.h in to kvm_host.h and then make a mess with header dependencies) and define the NVHE version in asm/kvm_hyp.h. Something like :

arch/arm64/include/asm/kvm_host.h +4 -1
index 46a7f6c1e426c..4016d09ed39c3 100644
@@ -1545,11 +1545,14 @@ 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)
+
+#ifndef __KVM_NVHE_HYPERVISOR__
   #define vcpu_is_protected(vcpu) kvm_vm_is_protected((vcpu)->kvm)
+#define vcpu_is_protected_pkvm(vcpu) 
 kvm_vm_is_protected_pkvm((vcpu)->kvm)
+#endif
 
   #define kvm_vm_is_protected_pkvm(kvm)          \
          (is_protected_kvm_enabled() && ((kvm)->arch.vm_flavor == VM_PROTECTED_PKVM))
-#define vcpu_is_protected_pkvm(vcpu)   
 kvm_vm_is_protected_pkvm(vcpu->kvm)
 

  #define kvm_vm_is_unprotected_pkvm(kvm)                \
         (is_protected_kvm_enabled() && ((kvm)->arch.vm_flavor == VM_PKVM))

arch/arm64/include/asm/kvm_hyp.h +0 -0
index 4974492744cc8..3bf87e81af430 100644
@@ -137,6 +137,19 @@ int __pkvm_init(phys_addr_t phys, unsigned long 

size, unsigned long *per_cpu_bas
  void __noreturn __host_enter(struct kvm_cpu_context *host_ctxt); #endif

arch/arm64/include/asm/kvm_hyp.h +3 -0
+#ifdef __KVM_NVHE_HYPERVISOR__
+#define vcpu_is_protected(vcpu)        \
+       

({                                                              \
+               struct kvm *__kvm = READ_ONCE((vcpu)-

Quoted 1 line

kvm);             \

+               __kvm && kvm_vm_is_protected((kern_hyp_va(__kvm)));     \
+       })
+#define
vcpu_is_protected_pkvm(vcpu)                                   \
+
({                                                              \
+               struct kvm *__kvm = READ_ONCE((vcpu)-

Quoted 1 line

kvm);             \

+               __kvm &&
kvm_vm_is_protected_pkvm((kern_hyp_va(__kvm)));\
+       })

arch/arm64/include/asm/kvm_hyp.h +2 -0
+#endif
+
Quoted 4 lines

Or explicitly convert all nvhe accessors to a new "nvhe_vcpu_is_protected"

The second one sounds like a better option to me.

We just need to include "asm/kvm_mmu.h" to "arch/arm64/mmio.c". With it, we can have unified functions (macros) in kvm_host.h to accomodate all cases, like below.

arch/arm64/include/asm/kvm_host.h +17 -2
index 9b1cf9c59e81..b9c9a9f203f9 100644
@@ -1514,11 +1514,36 @@ 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)
-#define vcpu_is_protected(vcpu)                
 kvm_vm_is_protected((vcpu)->kvm)
+#define 
 vcpu_is_protected(vcpu)                                                \
+       ({                                                              \
+               struct kvm *__kvm = READ_ONCE((vcpu)->kvm);             \
+               bool __protected = false;                               \
+                                                                       \
+               if (__kvm) {                                            \
+                       if (is_nvhe_hyp_code())                         \
+                               __kvm = kern_hyp_va(__kvm);             \
+                       __protected = kvm_vm_is_protected(__kvm);       \
+               }                                                       \
+                                                                       \
+               __protected;                                            \
+        })
+
 
   #define kvm_vm_is_protected_pkvm(kvm)          \
          (is_protected_kvm_enabled() && ((kvm)->arch.vm_flavor == VM_PROTECTED_PKVM))
-#define vcpu_is_protected_pkvm(vcpu)   kvm_vm_is_protected_pkvm(vcpu- 
 >kvm)
+#define vcpu_is_protected_pkvm(vcpu)                                   \
+       ({                                                              \
+               struct kvm *__kvm = READ_ONCE((vcpu)->kvm);             \

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

Quoted 10 lines
arch/arm64/include/asm/kvm_host.h +10 -0
+               bool __protected = false;                               \
+                                                                       \
+               if (__kvm) {                                            \
+                       if (is_nvhe_hyp_code())                         \
+                               __kvm = kern_hyp_va(__kvm);             \
+                       __protected = kvm_vm_is_protected_pkvm(__kvm);  \
+               }                                                       \
+                                                                       \
+               __protected;                                            \
+        })
Quoted 2 lines

Besides, vcpu_is_protected_pkvm() isn't used anywhere in branch "cca/ cca-host/kvm-v19/integration", so it can be removed :)

This was asked by Fuad, IIRC. And it is for the sake of completion with vcpu_is_protected and vcpu_is_rec

Cheers
Suzuki

author reviewer
#27 lore raw

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

In reply to Gavin Shan
Quoted 254 lines · Gavin Shan
Quoted 191 lines · Suzuki K Poulose
Quoted 126 lines · Gavin Shan
Quoted 112 lines · Gavin Shan
Quoted 66 lines · 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.

With that, add wrappers for checking the "type" of a VM and replace the existing users with the new wrappers.

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.

Adds __VM_PROTECTED marker vm flavor to generalize kvm_vm_is_protected() to predicate all confidential guests running on KVM. In later patches, we would add Realm VMs, which would also be classified as protected.

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. To tighten this init_pkvm_hyp_vm() restricts the hyp copy of the vm_flavor to the ones it supports.

---
Changes since v18:
  - Merge the __VM_PROTECTED marker and the widening of kvm_vm_is_protected() to this patch.
  - Merge the use of kvm_vm_is_unprotected_pkvm() for ! kvm_vm_is_protected() given the scope changes here.
  - Drop Fuad's review tag, as this patch has multiple merges
  - Restrict the VM flavors to the supported types in init_pkvm_hyp_vm().
  - Drop kvm_vm_hyp_is_pkvm() and revert to is_protected_kvm_enabled()
  - Use is_protected_kvm_enabled() to make the pKVM guest flavor checks.
  - s/PKVM/pKVM for commit descriptions too

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 | 22 +++++++++++++++++++--- arch/arm64/include/asm/kvm_pkvm.h |  4 ++--
  arch/arm64/kvm/arm.c              | 31 +++++++++++++++++++++++++

Quoted 7 lines
+-----
  arch/arm64/kvm/handle_exit.c      |  2 +-
  arch/arm64/kvm/hyp/nvhe/pkvm.c    |  6 +++++-
  arch/arm64/kvm/mmu.c              |  2 +-
  arch/arm64/kvm/pkvm.c             |  6 ++----
  7 files changed, 56 insertions(+), 17 deletions(-)

This causes broken nVHE mode. I applied PATCH[01-05] to v7.3.rc4 whose head commit is f0100363d8c3, and kselftests/kvm/guest_print causes host crash (as below). I don't see the crash if only PATCH[01-04] are applied.

host$ cat /proc/cmdline | grep kvm-arm\.mode
BOOT_IMAGE=(hd0,gpt2)/vmlinuz-7.3.0-rc4-gavin+ root=/dev/mapper/ rhel_nvidia--grace--hopper--01-root ro crashkernel=2G-4G:406M,4G-64G:470M,64G-:726M rd.lvm.lv=rhel_nvidia- grace-hopper-01/root rd.lvm.lv=rhel_nvidia-grace-hopper-01/swap video=simplefb:off kvm-arm.mode=nvhe

host$ cd linux/tools/testing/selftests/kvm
host$ ./guest_print_test
Random seed: 0x193a0ed3
[  192.754328] kvm [6674]: nVHE hyp panic at: [<ffff8000811da044>] __kvm_nvhe___timer_enable_traps+0x4/0x160! [  192.754338] kvm [6674]: nVHE call trace:
[  192.754339] kvm [6674]:  [<ffff8000811dcf34>] __kvm_nvhe_hyp_panic+0xb4/0xe0
[  192.754342] kvm [6674]:  [<ffff8000811dcba4>] __kvm_nvhe___kvm_vcpu_run+0x164/0x440
[  192.754344] kvm [6674]:  [<ffff8000811dfe90>] __kvm_nvhe_handle___kvm_vcpu_run+0x40/0x1f0
[  192.754346] kvm [6674]:  [<ffff8000811e0238>] __kvm_nvhe_handle_trap+0x158/0x280
[  192.754347] kvm [6674]:  [<ffff8000811dd8fc>] __kvm_nvhe___skip_pauth_save+0x4/0x4
[  192.754348] kvm [6674]: ---[ end nVHE call trace ]---
[  192.754350] Code: d2818002 17fffff7 d503201f f9400001 (b94a6821)
[  192.754350] kvm [6674]: Hyp Offset: 0xfffeb0d7fe2e0000
[  192.754351] Kernel panic - not syncing: HYP panic:
[  192.754351] PS:834003c9 PC:0000cf2882efa044 ESR:0000000096000004
[  192.754351] FAR:ffff00009b286a68 HPFAR:8000000000000000 PAR:1d00ec7edbadc8de
[  192.754351] VCPU:0000cf011848a350
[  192.845154] CPU: 12 UID: 0 PID: 6674 Comm: guest_print_tes Kdump: loaded Not tainted 7.3.0-rc4-gavin+ #7 PREEMPT(full) [  192.856182] Hardware name:  GH200 P5042, BIOS 02.04.01 20250422
[  192.862231] Call trace:
[  192.864725]  show_stack+0x20/0x38 (C)
[  192.868471]  dump_stack_lvl+0x88/0xb8
[  192.872215]  dump_stack+0x18/0x30
[  192.875598]  vpanic+0x280/0x498
[  192.878806]  panic+0x68/0x70
[  192.881745]  nvhe_hyp_panic_handler+0x184/0x190
[  192.886372]  kvm_arm_vcpu_enter_exit+0x24/0x100
[  192.891003]  kvm_arch_vcpu_ioctl_run+0x254/0x7c0
[  192.895726]  kvm_vcpu_ioctl+0x174/0xb40
[  192.899645]  __arm64_sys_ioctl+0xb0/0x120

Quoted 9 lines

[  192.903745]  invoke_syscall.constprop.0+0xa8/0x100 [  192.908639]  do_el0_svc+0xb8/0xe0
[  192.912022]  el0_svc+0x48/0x1f8
[  192.915228]  el0t_64_sync_handler+0xa0/0xe8 [  192.919500]  el0t_64_sync+0x1ac/0x1b0
[  192.923243] SMP: stopping secondary CPUs
[  192.927653] Starting crashdump kernel...
[  192.931658] Bye!

With the following changes applied after PATCH[01-05] on top of v7.3.rc4, the crash is avoided.

Quoted 7 lines

In arch/arm64/include/asm/kvm_host.h:

-#define kvm_vm_is_protected(kvm)       ((kvm)->arch.vm_flavor >= __VM_PROTECTED)
+#define kvm_vm_is_protected(kvm)       \
+       (is_protected_kvm_enabled() && (kvm)->arch.vm_flavor >= __VM_PROTECTED)

That only papers over the problem. We can't use the vcpu_is_* constructs from nvhe hyp, without converting the vcpu->kvm to the hyp address, before accessing it. The fix is a bit more involved. One option is to define the vcpu_is_* helpers only for the !NVHE hyp code in the kvm_host.h (to avoid pulling in the asm/kvm_mmu.h in to kvm_host.h and then make a mess with header dependencies) and define the NVHE version in asm/kvm_hyp.h. Something like :

arch/arm64/include/asm/kvm_host.h +4 -1
index 46a7f6c1e426c..4016d09ed39c3 100644
@@ -1545,11 +1545,14 @@ 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)
+
+#ifndef __KVM_NVHE_HYPERVISOR__
   #define vcpu_is_protected(vcpu) kvm_vm_is_protected((vcpu)->kvm)
+#define vcpu_is_protected_pkvm(vcpu) kvm_vm_is_protected_pkvm((vcpu)- 
 >kvm)
+#endif
 
   #define kvm_vm_is_protected_pkvm(kvm)          \
          (is_protected_kvm_enabled() && ((kvm)->arch.vm_flavor == VM_PROTECTED_PKVM))
-#define vcpu_is_protected_pkvm(vcpu)   kvm_vm_is_protected_pkvm(vcpu- 
 >kvm)
 

  #define kvm_vm_is_unprotected_pkvm(kvm)                \
         (is_protected_kvm_enabled() && ((kvm)->arch.vm_flavor == VM_PKVM))

arch/arm64/include/asm/kvm_hyp.h +0 -0
index 4974492744cc8..3bf87e81af430 100644
@@ -137,6 +137,19 @@ int __pkvm_init(phys_addr_t phys, unsigned long 

size, unsigned long *per_cpu_bas
  void __noreturn __host_enter(struct kvm_cpu_context *host_ctxt); #endif

arch/arm64/include/asm/kvm_hyp.h +13 -0
+#ifdef __KVM_NVHE_HYPERVISOR__
+#define vcpu_is_protected(vcpu)        \
+       ({                                                              \
+               struct kvm *__kvm = READ_ONCE((vcpu)->kvm);             \
+               __kvm && kvm_vm_is_protected((kern_hyp_va(__kvm)));     \
+       })
+#define vcpu_is_protected_pkvm(vcpu)                                   \
+       ({                                                              \
+               struct kvm *__kvm = READ_ONCE((vcpu)->kvm);             \
+               __kvm && kvm_vm_is_protected_pkvm((kern_hyp_va(__kvm)));\
+       })
+#endif
+
Quoted 4 lines

Or explicitly convert all nvhe accessors to a new "nvhe_vcpu_is_protected"

The second one sounds like a better option to me.

We just need to include "asm/kvm_mmu.h" to "arch/arm64/mmio.c". With it, we can have unified functions (macros) in kvm_host.h to accomodate all cases, like below.

arch/arm64/include/asm/kvm_host.h +27 -2
index 9b1cf9c59e81..b9c9a9f203f9 100644
@@ -1514,11 +1514,36 @@ 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)
-#define vcpu_is_protected(vcpu)                
 kvm_vm_is_protected((vcpu)->kvm)
+#define 
 vcpu_is_protected(vcpu)                                                \
+       ({                                                              \
+               struct kvm *__kvm = READ_ONCE((vcpu)->kvm);             \
+               bool __protected = false;                               \
+                                                                       \
+               if (__kvm) {                                            \
+                       if (is_nvhe_hyp_code())                         \
+                               __kvm = kern_hyp_va(__kvm);             \
+                       __protected = kvm_vm_is_protected(__kvm);       \
+               }                                                       \
+                                                                       \
+               __protected;                                            \
+        })
+
 
  #define kvm_vm_is_protected_pkvm(kvm)          \
         (is_protected_kvm_enabled() && ((kvm)->arch.vm_flavor == 
 VM_PROTECTED_PKVM))
-#define vcpu_is_protected_pkvm(vcpu)   kvm_vm_is_protected_pkvm(vcpu->kvm)
+#define vcpu_is_protected_pkvm(vcpu)                                   \
+       ({                                                              \
+               struct kvm *__kvm = READ_ONCE((vcpu)->kvm);             \
+               bool __protected = false;                               \
+                                                                       \
+               if (__kvm) {                                            \
+                       if (is_nvhe_hyp_code())                         \
+                               __kvm = kern_hyp_va(__kvm);             \
+                       __protected = kvm_vm_is_protected_pkvm(__kvm);  \
+               }                                                       \
+                                                                       \
+               __protected;                                            \
+        })
 
  #define kvm_vm_is_unprotected_pkvm(kvm)                \
         (is_protected_kvm_enabled() && ((kvm)->arch.vm_flavor == VM_PKVM))
arch/arm64/kvm/mmio.c +0 -0
index d1c3a352d5a2..ab1d2fef9a52 100644

arch/arm64/kvm/mmio.c line 12

Quoted 7 lines
arch/arm64/kvm/mmio.c +1 -0
@@ -6,6 +6,7 @@
 
  #include <linux/kvm_host.h>
  #include <asm/kvm_emulate.h>
+#include <asm/kvm_mmu.h>
  #include <trace/events/kvm.h>
 
  #include "trace.h"

Thanks Gavin, that works and looks neat. I will incorporate it.

Cheers
Suzuki

Quoted 3 lines

Thanks,
Gavin

#4 lore raw

Re: [PATCH v19 01/20] KVM: arm64: protected VM: Handle user writes to CNTVCT_EL0/CNTPCT_EL0

In reply to Suzuki K Poulose
Quoted 28 lines · On Tue, 22 Sep 2026 22:53:48 +0100 Suzuki K Poulose <[email protected]>…
Quoted 23 lines · Jonathan Cameron

On Sun, 20 Sep 2026 22:28:26 +0100
Suzuki K Poulose <[email protected]> wrote:

Hi Suzuki,

Quoted 1 line

Protected VMs doesn't allow setting offsets for virtual and phyiscal

physical

Quoted 1 line

counters, as the offset is always fixed to 0. The VM ioclt is filtered

ioctl

Quoted 9 lines

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 KVM init time for protected VMs. A userspace writing to the CNT*CT_EL0 would observe success, without any real effect. This was chosen over preventing the writes to these registers and returning -EPERM.

Quoted 3 lines

Why? I don't mind the decision but telling us what was chosen is something we can see in the code - patch description should give us the stuff we can't see.

Quoted 7 lines

I have modified the paragraph to :

"Fix this by always "fixing" the timer offsets to 0 and marking that the timer offset is set in the kvm->arch.flags at KVM init time for protected VMs. This prevents the access to the VM specific vm_offset at low cost. A userspace writing to the CNT*CT_EL0 would observe success, without any real effect. This is cleaner over spilling *_is_protected()"

cleaner than spilling.

Snag is over spill is a whole different thing and got me wondering what was over spilling!

Otherwise that works nicely. Thanks!

#6 lore raw

Re: [PATCH v19 01/20] KVM: arm64: protected VM: Handle user writes to CNTVCT_EL0/CNTPCT_EL0

In reply to Suzuki K Poulose
Quoted 71 lines · On Tue, 22 Sep 2026 23:04:42 +0100 Suzuki K Poulose <[email protected]>…
Quoted 70 lines · Jonathan Cameron

On Sun, 20 Sep 2026 22:28:26 +0100
Suzuki K Poulose <[email protected]> wrote:

Hi Suzuki,

Quoted 1 line

Protected VMs doesn't allow setting offsets for virtual and phyiscal

physical

Quoted 1 line

counters, as the offset is always fixed to 0. The VM ioclt is filtered

ioctl

Quoted 9 lines

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 KVM init time for protected VMs. A userspace writing to the CNT*CT_EL0 would observe success, without any real effect. This was chosen over preventing the writes to these registers and returning -EPERM.

Why? I don't mind the decision but telling us what was chosen is something we can see in the code - patch description should give us the stuff we can't see.

One comment on the comment below.

Quoted 32 lines

Reported by Sashiko

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

---
Changes since v18:
- Retain NULL vm_offset for protected VMs to avoid host tampering with the offset.
- Moved the flag setting into kvm_timer_init_vm(), where it should have been in the first place

---
  arch/arm64/kvm/arch_timer.c | 12 ++++++++++--
  1 file changed, 10 insertions(+), 2 deletions(-)
arch/arm64/kvm/arch_timer.c +1 -2
index 6ac3321f4c575..226cd5a495c8b 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);
  	}

arch/arm64/kvm/arch_timer.c void kvm_timer_init_vm(struct kvm *kvm) · line 1138

Quoted 7 lines
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]);
+
+	/*
+	 * Protected VMs don't allow any offset being set from userspace,
+	 * either set via writes to the counters or using the dedicated  

This sentence confused me. Second clause isn't obviously the ways that are being blocked. Maybe shorten to:

Quoted 8 lines
Quoted 2 lines

Protected VMs don't allow the offset to be set from userspace, whether via writes to the counters or the dedicated ioctl.

LLM suggests:

* Protected VMs don't allow userspace to set counter offsets,
* either via counter register writes or the dedicated ioctl.

Hmm. I'd not combine a negative with either. Still good enough I think! Our Booker prize may be a way off yet :)

Quoted 17 lines

Have updated the comment.

Cheers
Suzuki

Quoted 10 lines
Quoted 9 lines
arch/arm64/kvm/arch_timer.c +5 -0
+	 * ioctl. Pretend the offset has already been set and rely on the
+	 * default offset being 0.
+	 */
+	if (kvm_vm_is_protected(kvm))
+		set_bit(KVM_ARCH_FLAG_VM_COUNTER_OFFSET, &kvm->arch.flags);
  }
 
  void kvm_timer_cpu_up(void)  
#43 lore raw

Re: [PATCH v19 09/20] KVM: arm64: Add VM specific callback for S2 MMU operations

In reply to Suzuki K Poulose
Quoted 62 lines · On Wed, 23 Sep 2026 00:21:58 +0100 Suzuki K Poulose <[email protected]>…
Quoted 35 lines · Jonathan Cameron
Quoted 4 lines · On Sun, 20 Sep 2026 22:28:34 +0100 Suzuki K Poulose <[email protected]>…

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

A small suggestion inline that makes it explicit when there isn't range operation and a full flush of tlb is needed.

I don't mind either way, but thought I'd mention it anyway in case you hadn't already dismissed it.

Quoted 18 lines
arch/arm64/kvm/mmu.c +0 -2
index 03f2017a7404a..d97a4a1bca23f 100644
@@ -37,6 +37,8 @@ static unsigned long __ro_after_init io_map_base;
 
  #define KVM_PGT_FN(fn)		(!is_protected_kvm_enabled() ? fn : p ## fn)
 
 
  /**
   * kvm_arch_flush_remote_tlbs() - flush all VM TLB entries for v7/8
   * @kvm:	pointer to kvm structure.
@@ -174,26 +188,36 @@ static bool memslot_is_logging(struct kvm_memory_slot *memslot)
   */
  int kvm_arch_flush_remote_tlbs(struct kvm *kvm)
  {
-	if (is_protected_kvm_enabled())
-		kvm_call_hyp_nvhe(__pkvm_tlb_flush_vmid, kvm->arch.pkvm.handle);
Quoted 8 lines

arch/arm64/kvm/mmu.c static bool memslot_is_logging(struct kvm_memory_slot *memslot) · line 192

Quoted 5 lines
arch/arm64/kvm/mmu.c +2 -3
-	else
-		kvm_call_hyp(__kvm_tlb_flush_vmid, &kvm->arch.mmu);
-	return 0;
+	if (!kvm->arch.vm_s2_ops->vm_flush_remote_tlbs)
+		return 1;  

For consistency with other cases and because it looks nicer blank line here.

Ack

Quoted 4 lines
Quoted 4 lines
arch/arm64/kvm/mmu.c +1 -0
+	return kvm->arch.vm_s2_ops->vm_flush_remote_tlbs(kvm);
  }
 
Quoted 10 lines

arch/arm64/kvm/mmu.c static bool memslot_is_logging(struct kvm_memory_slot *memslot) · line 199

Quoted 6 lines
arch/arm64/kvm/mmu.c +4 -2
-int kvm_arch_flush_remote_tlbs_range(struct kvm *kvm,
-				      gfn_t gfn, u64 nr_pages)
+static int pkvm_flush_remote_tlbs_range(struct kvm *kvm,
+					gfn_t gfn, u64 nr_pages)
+{
+	return pkvm_flush_remote_tlbs(kvm);  

For the cases where there isn't a range call, perhaps you should just not provide the op and have the wrapper fall back to calling the 'flush all' variant if there is no range one available?

Quoted 2 lines

The core KVM falls back to flushing all TLBs if the range one returns true anyways. So, I would leave it as it is.

That sounds like a reason to stub them to return true always rather than call the flush all so we don't have fallbacks added at two layers of the stack?

Quoted 10 lines

Cheers
Suzuki

Quoted 4 lines
Quoted 2 lines
arch/arm64/kvm/mmu.c +1 -0
+}  
author reviewer
#28 lore raw

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

In reply to Suzuki K Poulose
Quoted 216 lines · Suzuki K Poulose
Quoted 215 lines · Gavin Shan
Quoted 198 lines · Suzuki K Poulose
Quoted 126 lines · Gavin Shan
Quoted 112 lines · Gavin Shan
Quoted 66 lines · 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.

With that, add wrappers for checking the "type" of a VM and replace the existing users with the new wrappers.

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.

Adds __VM_PROTECTED marker vm flavor to generalize kvm_vm_is_protected() to predicate all confidential guests running on KVM. In later patches, we would add Realm VMs, which would also be classified as protected.

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. To tighten this init_pkvm_hyp_vm() restricts the hyp copy of the vm_flavor to the ones it supports.

---
Changes since v18:
  - Merge the __VM_PROTECTED marker and the widening of kvm_vm_is_protected() to this patch.
  - Merge the use of kvm_vm_is_unprotected_pkvm() for ! kvm_vm_is_protected() given the scope changes here.
  - Drop Fuad's review tag, as this patch has multiple merges
  - Restrict the VM flavors to the supported types in init_pkvm_hyp_vm().
  - Drop kvm_vm_hyp_is_pkvm() and revert to is_protected_kvm_enabled()
  - Use is_protected_kvm_enabled() to make the pKVM guest flavor checks.
  - s/PKVM/pKVM for commit descriptions too

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 | 22 +++++++++++++++++++--- arch/arm64/include/asm/kvm_pkvm.h |  4 ++--
  arch/arm64/kvm/arm.c              | 31 +++++++++++++++++++++++++

Quoted 7 lines
+-----
  arch/arm64/kvm/handle_exit.c      |  2 +-
  arch/arm64/kvm/hyp/nvhe/pkvm.c    |  6 +++++-
  arch/arm64/kvm/mmu.c              |  2 +-
  arch/arm64/kvm/pkvm.c             |  6 ++----
  7 files changed, 56 insertions(+), 17 deletions(-)

This causes broken nVHE mode. I applied PATCH[01-05] to v7.3.rc4 whose head commit is f0100363d8c3, and kselftests/kvm/guest_print causes host crash (as below). I don't see the crash if only PATCH[01-04] are applied.

host$ cat /proc/cmdline | grep kvm-arm\.mode
BOOT_IMAGE=(hd0,gpt2)/vmlinuz-7.3.0-rc4-gavin+ root=/dev/mapper/ rhel_nvidia--grace--hopper--01-root ro crashkernel=2G-4G:406M,4G-64G:470M,64G-:726M rd.lvm.lv=rhel_nvidia- grace-hopper-01/root rd.lvm.lv=rhel_nvidia-grace-hopper-01/swap video=simplefb:off kvm-arm.mode=nvhe

host$ cd linux/tools/testing/selftests/kvm
host$ ./guest_print_test
Random seed: 0x193a0ed3
[  192.754328] kvm [6674]: nVHE hyp panic at: [<ffff8000811da044>] __kvm_nvhe___timer_enable_traps+0x4/0x160! [  192.754338] kvm [6674]: nVHE call trace:
[  192.754339] kvm [6674]:  [<ffff8000811dcf34>] __kvm_nvhe_hyp_panic+0xb4/0xe0
[  192.754342] kvm [6674]:  [<ffff8000811dcba4>] __kvm_nvhe___kvm_vcpu_run+0x164/0x440
[  192.754344] kvm [6674]:  [<ffff8000811dfe90>] __kvm_nvhe_handle___kvm_vcpu_run+0x40/0x1f0
[  192.754346] kvm [6674]:  [<ffff8000811e0238>] __kvm_nvhe_handle_trap+0x158/0x280
[  192.754347] kvm [6674]:  [<ffff8000811dd8fc>] __kvm_nvhe___skip_pauth_save+0x4/0x4
[  192.754348] kvm [6674]: ---[ end nVHE call trace ]---
[  192.754350] Code: d2818002 17fffff7 d503201f f9400001 (b94a6821)
[  192.754350] kvm [6674]: Hyp Offset: 0xfffeb0d7fe2e0000
[  192.754351] Kernel panic - not syncing: HYP panic:
[  192.754351] PS:834003c9 PC:0000cf2882efa044 ESR:0000000096000004
[  192.754351] FAR:ffff00009b286a68 HPFAR:8000000000000000 PAR:1d00ec7edbadc8de
[  192.754351] VCPU:0000cf011848a350
[  192.845154] CPU: 12 UID: 0 PID: 6674 Comm: guest_print_tes Kdump: loaded Not tainted 7.3.0-rc4-gavin+ #7 PREEMPT(full) [  192.856182] Hardware name:  GH200 P5042, BIOS 02.04.01 20250422
[  192.862231] Call trace:
[  192.864725]  show_stack+0x20/0x38 (C)
[  192.868471]  dump_stack_lvl+0x88/0xb8
[  192.872215]  dump_stack+0x18/0x30
[  192.875598]  vpanic+0x280/0x498
[  192.878806]  panic+0x68/0x70
[  192.881745]  nvhe_hyp_panic_handler+0x184/0x190
[  192.886372]  kvm_arm_vcpu_enter_exit+0x24/0x100
[  192.891003]  kvm_arch_vcpu_ioctl_run+0x254/0x7c0
[  192.895726]  kvm_vcpu_ioctl+0x174/0xb40
[  192.899645]  __arm64_sys_ioctl+0xb0/0x120

Quoted 9 lines

[  192.903745]  invoke_syscall.constprop.0+0xa8/0x100 [  192.908639]  do_el0_svc+0xb8/0xe0
[  192.912022]  el0_svc+0x48/0x1f8
[  192.915228]  el0t_64_sync_handler+0xa0/0xe8 [  192.919500]  el0t_64_sync+0x1ac/0x1b0
[  192.923243] SMP: stopping secondary CPUs
[  192.927653] Starting crashdump kernel...
[  192.931658] Bye!

With the following changes applied after PATCH[01-05] on top of v7.3.rc4, the crash is avoided.

Quoted 7 lines

In arch/arm64/include/asm/kvm_host.h:

-#define kvm_vm_is_protected(kvm)       ((kvm)->arch.vm_flavor >= __VM_PROTECTED)
+#define kvm_vm_is_protected(kvm)       \
+       (is_protected_kvm_enabled() && (kvm)->arch.vm_flavor >= __VM_PROTECTED)

That only papers over the problem. We can't use the vcpu_is_* constructs from nvhe hyp, without converting the vcpu->kvm to the hyp address, before accessing it. The fix is a bit more involved. One option is to define the vcpu_is_* helpers only for the !NVHE hyp code in the kvm_host.h (to avoid pulling in the asm/kvm_mmu.h in to kvm_host.h and then make a mess with header dependencies) and define the NVHE version in asm/kvm_hyp.h. Something like :

arch/arm64/include/asm/kvm_host.h +4 -1
index 46a7f6c1e426c..4016d09ed39c3 100644
@@ -1545,11 +1545,14 @@ 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)
+
+#ifndef __KVM_NVHE_HYPERVISOR__
   #define vcpu_is_protected(vcpu) kvm_vm_is_protected((vcpu)->kvm)
+#define vcpu_is_protected_pkvm(vcpu) 
 kvm_vm_is_protected_pkvm((vcpu)- >kvm)
+#endif
 
   #define kvm_vm_is_protected_pkvm(kvm)          \
          (is_protected_kvm_enabled() && ((kvm)->arch.vm_flavor == VM_PROTECTED_PKVM))
-#define vcpu_is_protected_pkvm(vcpu)   
 kvm_vm_is_protected_pkvm(vcpu- >kvm)
 

  #define kvm_vm_is_unprotected_pkvm(kvm)                \
         (is_protected_kvm_enabled() && ((kvm)->arch.vm_flavor == VM_PKVM))

arch/arm64/include/asm/kvm_hyp.h +0 -0
index 4974492744cc8..3bf87e81af430 100644
@@ -137,6 +137,19 @@ int __pkvm_init(phys_addr_t phys, unsigned long 

size, unsigned long *per_cpu_bas
  void __noreturn __host_enter(struct kvm_cpu_context *host_ctxt); #endif

arch/arm64/include/asm/kvm_hyp.h +3 -0
+#ifdef __KVM_NVHE_HYPERVISOR__
+#define vcpu_is_protected(vcpu)        \
+       

({                                                              \
+               struct kvm *__kvm = READ_ONCE((vcpu)-

Quoted 1 line

kvm);             \

+               __kvm && kvm_vm_is_protected((kern_hyp_va(__kvm)));     \
+       })
+#define
vcpu_is_protected_pkvm(vcpu)                                   \
+
({                                                              \
+               struct kvm *__kvm = READ_ONCE((vcpu)-

Quoted 1 line

kvm);             \

+               __kvm &&
kvm_vm_is_protected_pkvm((kern_hyp_va(__kvm)));\
+       })

arch/arm64/include/asm/kvm_hyp.h +2 -0
+#endif
+
Quoted 4 lines

Or explicitly convert all nvhe accessors to a new "nvhe_vcpu_is_protected"

The second one sounds like a better option to me.

We just need to include "asm/kvm_mmu.h" to "arch/arm64/mmio.c". With it, we can have unified functions (macros) in kvm_host.h to accomodate all cases, like below.

arch/arm64/include/asm/kvm_host.h +0 -0
index 9b1cf9c59e81..b9c9a9f203f9 100644
@@ -1514,11 +1514,36 @@ struct kvm *kvm_arch_alloc_vm(void);
   #define __KVM_HAVE_ARCH_FLUSH_REMOTE_TLBS_RANGE
 
Quoted 10 lines

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

Quoted 10 lines
arch/arm64/include/asm/kvm_host.h +7 -1
   #define kvm_vm_is_protected(kvm)       ((kvm)->arch.vm_flavor >= __VM_PROTECTED)
-#define vcpu_is_protected(vcpu) kvm_vm_is_protected((vcpu)->kvm)
+#define 
 vcpu_is_protected(vcpu)                                                \
+       ({                                                              \
+               struct kvm *__kvm = READ_ONCE((vcpu)->kvm);             \
+               bool __protected = false;                               \
+                                                                       \
+               if (__kvm) {                                            \
+                       if (is_nvhe_hyp_code())                         \

This needs to be :

			  if (is_nvhe_hyp_code() && !is_protected_kvm_enabled())
Quoted 1 line

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

Quoted 1 line
arch/arm64/include/asm/kvm_host.h +1 -0
+                               __kvm = kern_hyp_va(__kvm);             \

Otherwise, we unnecessarily convert the vcpu->kvm for a hyp_vcpu and that is not going to end well for pKVM.

Cheers
Suzuki

Quoted 50 lines
Quoted 33 lines
arch/arm64/include/asm/kvm_host.h +19 -1
+                       __protected = kvm_vm_is_protected(__kvm);       \
+               }                                                       \
+                                                                       \
+               __protected;                                            \
+        })
+
 
   #define kvm_vm_is_protected_pkvm(kvm)          \
          (is_protected_kvm_enabled() && ((kvm)->arch.vm_flavor == VM_PROTECTED_PKVM))
-#define vcpu_is_protected_pkvm(vcpu)   kvm_vm_is_protected_pkvm(vcpu- 
 >kvm)
+#define vcpu_is_protected_pkvm(vcpu)                                   \
+       ({                                                              \
+               struct kvm *__kvm = READ_ONCE((vcpu)->kvm);             \
+               bool __protected = false;                               \
+                                                                       \
+               if (__kvm) {                                            \
+                       if (is_nvhe_hyp_code())                         \
+                               __kvm = kern_hyp_va(__kvm);             \
+                       __protected = kvm_vm_is_protected_pkvm(__kvm);  \
+               }                                                       \
+                                                                       \
+               __protected;                                            \
+        })
 

  #define kvm_vm_is_unprotected_pkvm(kvm)                \
         (is_protected_kvm_enabled() && ((kvm)->arch.vm_flavor == VM_PKVM))

arch/arm64/kvm/mmio.c +0 -0
index d1c3a352d5a2..ab1d2fef9a52 100644

arch/arm64/kvm/mmio.c line 12

Quoted 7 lines
arch/arm64/kvm/mmio.c +1 -0
@@ -6,6 +6,7 @@
 
   #include <linux/kvm_host.h>
   #include <asm/kvm_emulate.h>
+#include <asm/kvm_mmu.h>
   #include <trace/events/kvm.h>
 
   #include "trace.h"

Thanks Gavin, that works and looks neat. I will incorporate it.

Cheers
Suzuki

Quoted 3 lines

Thanks,
Gavin

#29 lore raw

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

Quoted 204 lines · Suzuki K Poulose
Quoted 203 lines · Suzuki K Poulose
Quoted 202 lines · Gavin Shan
Quoted 187 lines · Suzuki K Poulose
Quoted 127 lines · Gavin Shan
Quoted 111 lines · Gavin Shan
Quoted 70 lines · 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.

With that, add wrappers for checking the "type" of a VM and replace the existing users with the new wrappers.

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.

Adds __VM_PROTECTED marker vm flavor to generalize kvm_vm_is_protected() to predicate all confidential guests running on KVM. In later patches, we would add Realm VMs, which would also be classified as protected.

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. To tighten this init_pkvm_hyp_vm() restricts the hyp copy of the vm_flavor to the ones it supports.

---
Changes since v18:
  - Merge the __VM_PROTECTED marker and the widening of kvm_vm_is_protected() to this patch.
  - Merge the use of kvm_vm_is_unprotected_pkvm() for ! kvm_vm_is_protected() given the scope changes here.
  - Drop Fuad's review tag, as this patch has multiple merges
  - Restrict the VM flavors to the supported types in init_pkvm_hyp_vm().
  - Drop kvm_vm_hyp_is_pkvm() and revert to is_protected_kvm_enabled()
  - Use is_protected_kvm_enabled() to make the pKVM guest flavor checks.
  - s/PKVM/pKVM for commit descriptions too

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 | 22 +++++++++++++++++++--- arch/arm64/include/asm/kvm_pkvm.h |  4 ++--
  arch/arm64/kvm/arm.c              | 31 +++++++++++++++++++++++++ +----- arch/arm64/kvm/handle_exit.c      |  2 +- arch/arm64/kvm/hyp/nvhe/pkvm.c    |  6 +++++- arch/arm64/kvm/mmu.c              |  2 +-
  arch/arm64/kvm/pkvm.c             |  6 ++----

Quoted 2 lines
  7 files changed, 56 insertions(+), 17 deletions(-)

This causes broken nVHE mode. I applied PATCH[01-05] to v7.3.rc4 whose head commit is f0100363d8c3, and kselftests/kvm/guest_print causes host crash (as below). I don't see the crash if only PATCH[01-04] are applied.

host$ cat /proc/cmdline | grep kvm-arm\.mode
BOOT_IMAGE=(hd0,gpt2)/vmlinuz-7.3.0-rc4-gavin+ root=/dev/mapper/ rhel_nvidia--grace--hopper--01-root ro crashkernel=2G-4G:406M,4G-64G:470M,64G-:726M rd.lvm.lv=rhel_nvidia- grace-hopper-01/root rd.lvm.lv=rhel_nvidia-grace-hopper-01/swap video=simplefb:off kvm-arm.mode=nvhe

host$ cd linux/tools/testing/selftests/kvm
host$ ./guest_print_test
Random seed: 0x193a0ed3
[  192.754328] kvm [6674]: nVHE hyp panic at: [<ffff8000811da044>] __kvm_nvhe___timer_enable_traps+0x4/0x160! [  192.754338] kvm [6674]: nVHE call trace:
[  192.754339] kvm [6674]:  [<ffff8000811dcf34>] __kvm_nvhe_hyp_panic+0xb4/0xe0
[  192.754342] kvm [6674]:  [<ffff8000811dcba4>] __kvm_nvhe___kvm_vcpu_run+0x164/0x440
[  192.754344] kvm [6674]:  [<ffff8000811dfe90>] __kvm_nvhe_handle___kvm_vcpu_run+0x40/0x1f0
[  192.754346] kvm [6674]:  [<ffff8000811e0238>] __kvm_nvhe_handle_trap+0x158/0x280
[  192.754347] kvm [6674]:  [<ffff8000811dd8fc>] __kvm_nvhe___skip_pauth_save+0x4/0x4
[  192.754348] kvm [6674]: ---[ end nVHE call trace ]---
[  192.754350] Code: d2818002 17fffff7 d503201f f9400001 (b94a6821)
[  192.754350] kvm [6674]: Hyp Offset: 0xfffeb0d7fe2e0000
[  192.754351] Kernel panic - not syncing: HYP panic:
[  192.754351] PS:834003c9 PC:0000cf2882efa044 ESR:0000000096000004
[  192.754351] FAR:ffff00009b286a68 HPFAR:8000000000000000 PAR:1d00ec7edbadc8de
[  192.754351] VCPU:0000cf011848a350
[  192.845154] CPU: 12 UID: 0 PID: 6674 Comm: guest_print_tes Kdump: loaded Not tainted 7.3.0-rc4-gavin+ #7 PREEMPT(full) [  192.856182] Hardware name:  GH200 P5042, BIOS 02.04.01 20250422
[  192.862231] Call trace:
[  192.864725]  show_stack+0x20/0x38 (C)
[  192.868471]  dump_stack_lvl+0x88/0xb8
[  192.872215]  dump_stack+0x18/0x30
[  192.875598]  vpanic+0x280/0x498
[  192.878806]  panic+0x68/0x70
[  192.881745]  nvhe_hyp_panic_handler+0x184/0x190
[  192.886372]  kvm_arm_vcpu_enter_exit+0x24/0x100
[  192.891003]  kvm_arch_vcpu_ioctl_run+0x254/0x7c0
[  192.895726]  kvm_vcpu_ioctl+0x174/0xb40
[  192.899645]  __arm64_sys_ioctl+0xb0/0x120

Quoted 9 lines

[  192.903745]  invoke_syscall.constprop.0+0xa8/0x100 [  192.908639]  do_el0_svc+0xb8/0xe0
[  192.912022]  el0_svc+0x48/0x1f8
[  192.915228]  el0t_64_sync_handler+0xa0/0xe8 [  192.919500]  el0t_64_sync+0x1ac/0x1b0
[  192.923243] SMP: stopping secondary CPUs
[  192.927653] Starting crashdump kernel...
[  192.931658] Bye!

With the following changes applied after PATCH[01-05] on top of v7.3.rc4, the crash is avoided.

In arch/arm64/include/asm/kvm_host.h:

Review

Quoted 3 lines
(quoted diff) +2 -1
-#define kvm_vm_is_protected(kvm)       ((kvm)->arch.vm_flavor >= __VM_PROTECTED)
+#define kvm_vm_is_protected(kvm)       \
+       (is_protected_kvm_enabled() && (kvm)->arch.vm_flavor >= __VM_PROTECTED)

That only papers over the problem. We can't use the vcpu_is_* constructs from nvhe hyp, without converting the vcpu->kvm to the hyp address, before accessing it. The fix is a bit more involved. One option is to define the vcpu_is_* helpers only for the !NVHE hyp code in the kvm_host.h (to avoid pulling in the asm/kvm_mmu.h in to kvm_host.h and then make a mess with header dependencies) and define the NVHE version in asm/kvm_hyp.h. Something like :

arch/arm64/include/asm/kvm_host.h +4 -1
index 46a7f6c1e426c..4016d09ed39c3 100644
@@ -1545,11 +1545,14 @@ 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)
+
+#ifndef __KVM_NVHE_HYPERVISOR__
   #define vcpu_is_protected(vcpu) kvm_vm_is_protected((vcpu)->kvm)
+#define vcpu_is_protected_pkvm(vcpu) kvm_vm_is_protected_pkvm((vcpu)- >kvm)
+#endif
 
   #define kvm_vm_is_protected_pkvm(kvm)          \
          (is_protected_kvm_enabled() && ((kvm)->arch.vm_flavor == VM_PROTECTED_PKVM))
-#define vcpu_is_protected_pkvm(vcpu) kvm_vm_is_protected_pkvm(vcpu- >kvm)
 
   #define kvm_vm_is_unprotected_pkvm(kvm)                \
          (is_protected_kvm_enabled() && ((kvm)->arch.vm_flavor == VM_PKVM))
arch/arm64/include/asm/kvm_hyp.h +13 -0
index 4974492744cc8..3bf87e81af430 100644
@@ -137,6 +137,19 @@ int __pkvm_init(phys_addr_t phys, unsigned long size, unsigned long *per_cpu_bas
   void __noreturn __host_enter(struct kvm_cpu_context *host_ctxt);
   #endif
 
+#ifdef __KVM_NVHE_HYPERVISOR__
+#define vcpu_is_protected(vcpu)        \
+ ({                                                              \
+               struct kvm *__kvm = READ_ONCE((vcpu)- >kvm);             \
+               __kvm && kvm_vm_is_protected((kern_hyp_va(__kvm)));     \
+       })
+#define vcpu_is_protected_pkvm(vcpu)                                   \
+ ({                                                              \
+               struct kvm *__kvm = READ_ONCE((vcpu)- >kvm);             \
+               __kvm && kvm_vm_is_protected_pkvm((kern_hyp_va(__kvm)));\
+       })
+#endif
+
 
 

Or explicitly convert all nvhe accessors to a new "nvhe_vcpu_is_protected"

Quoted 2 lines

The second one sounds like a better option to me.

We just need to include "asm/kvm_mmu.h" to "arch/arm64/mmio.c". With it, we can have unified functions (macros) in kvm_host.h to accomodate all cases, like below.

arch/arm64/include/asm/kvm_host.h +0 -0
index 9b1cf9c59e81..b9c9a9f203f9 100644
@@ -1514,11 +1514,36 @@ struct kvm *kvm_arch_alloc_vm(void);
   #define __KVM_HAVE_ARCH_FLUSH_REMOTE_TLBS_RANGE
 
Quoted 13 lines
Quoted 9 lines

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

Quoted 9 lines
arch/arm64/include/asm/kvm_host.h +7 -1
   #define kvm_vm_is_protected(kvm)       ((kvm)->arch.vm_flavor >= __VM_PROTECTED)
-#define vcpu_is_protected(vcpu) kvm_vm_is_protected((vcpu)->kvm)
+#define vcpu_is_protected(vcpu)                                                \
+       ({                                                              \
+               struct kvm *__kvm = READ_ONCE((vcpu)->kvm);             \
+               bool __protected = false;                               \
+                                                                       \
+               if (__kvm) {                                            \
+                       if (is_nvhe_hyp_code())                         \

This needs to be :
              if (is_nvhe_hyp_code() && !is_protected_kvm_enabled())

Yes.

Quoted 5 lines
Quoted 1 line

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

Quoted 1 line
arch/arm64/include/asm/kvm_host.h +1 -0
+                               __kvm = kern_hyp_va(__kvm);             \

Otherwise, we unnecessarily convert the vcpu->kvm for a hyp_vcpu and that is not going to end well for pKVM.

Yes. kern_hyp_va() isn't needed when is_protected_kvm_enabled() is true. Besides, vcpu_is_protected_pkvm() can be further simplifed by fully exploiting the check (is_protected_kvm_enabled()) done in vcpu_is_protected_pkvm(), see below.

Quoted 11 lines

Cheers
Suzuki

Quoted 7 lines
Quoted 7 lines
arch/arm64/include/asm/kvm_host.h +6 -0
+                       __protected = kvm_vm_is_protected(__kvm);       \
+               }                                                       \
+                                                                       \
+               __protected;                                            \
+        })
+
 
Quoted 16 lines
Quoted 8 lines
Quoted 8 lines
arch/arm64/include/asm/kvm_host.h +5 -1
   #define kvm_vm_is_protected_pkvm(kvm)          \
          (is_protected_kvm_enabled() && ((kvm)->arch.vm_flavor == VM_PROTECTED_PKVM))
-#define vcpu_is_protected_pkvm(vcpu)   kvm_vm_is_protected_pkvm(vcpu- >kvm)
+#define vcpu_is_protected_pkvm(vcpu)                                   \
+       ({                                                              \
+               struct kvm *__kvm = READ_ONCE((vcpu)->kvm);             \
+               bool __protected = false;                               \
+                                                                       \
Quoted 8 lines

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

Quoted 8 lines
arch/arm64/include/asm/kvm_host.h +8 -0
+               if (__kvm) {                                            \
+                       if (is_nvhe_hyp_code())                         \
+                               __kvm = kern_hyp_va(__kvm);             \
+                       __protected = kvm_vm_is_protected_pkvm(__kvm);  \
+               }                                                       \
+                                                                       \
+               __protected;                                            \
+        })

This can be further simplified by fully expliting the check (is_protected_kvm_enabled()) in kvm_vm_is_protected_pkvm(), as below. The point is that the check is_protected_kvm_enabled() gurantees that kern_hyp_va() isn't needed.

#define vcpu_is_protected_pkvm(vcpu)                                    \
        ({                                                              \
                struct kvm *__kvm = READ_ONCE((vcpu)->kvm);             \
                bool __protected = false;                               \
                                                                        \
                if (__kvm)                                              \
                        __protected = kvm_vm_is_protected_pkvm(__kvm);  \
                                                                        \
                __protected;                                            \
        })
Quoted 14 lines
Quoted 14 lines
Quoted 14 lines
arch/arm64/include/asm/kvm_host.h +0 -0
   #define kvm_vm_is_unprotected_pkvm(kvm)                \
          (is_protected_kvm_enabled() && ((kvm)->arch.vm_flavor == VM_PKVM))
arch/arm64/kvm/mmio.c +1 -0
index d1c3a352d5a2..ab1d2fef9a52 100644
@@ -6,6 +6,7 @@
 
   #include <linux/kvm_host.h>
   #include <asm/kvm_emulate.h>
+#include <asm/kvm_mmu.h>
   #include <trace/events/kvm.h>
 
Quoted 5 lines
Quoted 5 lines

arch/arm64/kvm/mmio.c line 12

Quoted 2 lines
arch/arm64/kvm/mmio.c +0 -0
   #include "trace.h"

Thanks Gavin, that works and looks neat. I will incorporate it.

Thanks,
Gavin

author reviewer
#30 lore raw

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

Quoted 238 lines · Gavin Shan
Quoted 231 lines · Suzuki K Poulose
Quoted 221 lines · Suzuki K Poulose
Quoted 220 lines · Gavin Shan
Quoted 196 lines · Suzuki K Poulose
Quoted 126 lines · Gavin Shan
Quoted 112 lines · Gavin Shan
Quoted 71 lines · 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.

With that, add wrappers for checking the "type" of a VM and replace the existing users with the new wrappers.

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.

Adds __VM_PROTECTED marker vm flavor to generalize kvm_vm_is_protected() to predicate all confidential guests running on KVM. In later patches, we would add Realm VMs, which would also be classified as protected.

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. To tighten this init_pkvm_hyp_vm() restricts the hyp copy of the vm_flavor to the ones it supports.

---
Changes since v18:
  - Merge the __VM_PROTECTED marker and the widening of kvm_vm_is_protected() to this patch.
  - Merge the use of kvm_vm_is_unprotected_pkvm() for ! kvm_vm_is_protected() given the scope changes here.
  - Drop Fuad's review tag, as this patch has multiple merges
  - Restrict the VM flavors to the supported types in init_pkvm_hyp_vm().
  - Drop kvm_vm_hyp_is_pkvm() and revert to is_protected_kvm_enabled()
  - Use is_protected_kvm_enabled() to make the pKVM guest flavor checks.
  - s/PKVM/pKVM for commit descriptions too

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 | 22 +++++++++++++++++++--- arch/arm64/include/asm/kvm_pkvm.h |  4 ++--
  arch/arm64/kvm/arm.c              | 31 +++++++++++++++++++++++
++ +-----
  arch/arm64/kvm/handle_exit.c      |  2 +- arch/arm64/kvm/hyp/nvhe/pkvm.c    |  6 +++++- arch/arm64/kvm/mmu.c              |  2 +-
  arch/arm64/kvm/pkvm.c             |  6 ++----

Quoted 2 lines
  7 files changed, 56 insertions(+), 17 deletions(-)

This causes broken nVHE mode. I applied PATCH[01-05] to v7.3.rc4 whose head commit is f0100363d8c3, and kselftests/kvm/guest_print causes host crash (as below). I don't see the crash if only PATCH[01-04] are applied.

host$ cat /proc/cmdline | grep kvm-arm\.mode
BOOT_IMAGE=(hd0,gpt2)/vmlinuz-7.3.0-rc4-gavin+ root=/dev/mapper/ rhel_nvidia--grace--hopper--01-root ro crashkernel=2G-4G:406M,4G-64G:470M,64G-:726M rd.lvm.lv=rhel_nvidia- grace-hopper-01/root rd.lvm.lv=rhel_nvidia-grace-hopper-01/swap video=simplefb:off kvm-arm.mode=nvhe

host$ cd linux/tools/testing/selftests/kvm
host$ ./guest_print_test
Random seed: 0x193a0ed3
[  192.754328] kvm [6674]: nVHE hyp panic at: [<ffff8000811da044>] __kvm_nvhe___timer_enable_traps+0x4/0x160! [  192.754338] kvm [6674]: nVHE call trace:
[  192.754339] kvm [6674]:  [<ffff8000811dcf34>] __kvm_nvhe_hyp_panic+0xb4/0xe0
[  192.754342] kvm [6674]:  [<ffff8000811dcba4>] __kvm_nvhe___kvm_vcpu_run+0x164/0x440
[  192.754344] kvm [6674]:  [<ffff8000811dfe90>] __kvm_nvhe_handle___kvm_vcpu_run+0x40/0x1f0
[  192.754346] kvm [6674]:  [<ffff8000811e0238>] __kvm_nvhe_handle_trap+0x158/0x280
[  192.754347] kvm [6674]:  [<ffff8000811dd8fc>] __kvm_nvhe___skip_pauth_save+0x4/0x4
[  192.754348] kvm [6674]: ---[ end nVHE call trace ]---
[  192.754350] Code: d2818002 17fffff7 d503201f f9400001 (b94a6821)
[  192.754350] kvm [6674]: Hyp Offset: 0xfffeb0d7fe2e0000
[  192.754351] Kernel panic - not syncing: HYP panic:
[  192.754351] PS:834003c9 PC:0000cf2882efa044 ESR:0000000096000004
[  192.754351] FAR:ffff00009b286a68 HPFAR:8000000000000000 PAR:1d00ec7edbadc8de
[  192.754351] VCPU:0000cf011848a350
[  192.845154] CPU: 12 UID: 0 PID: 6674 Comm: guest_print_tes Kdump: loaded Not tainted 7.3.0-rc4-gavin+ #7 PREEMPT(full) [  192.856182] Hardware name:  GH200 P5042, BIOS 02.04.01 20250422
[  192.862231] Call trace:
[  192.864725]  show_stack+0x20/0x38 (C)
[  192.868471]  dump_stack_lvl+0x88/0xb8
[  192.872215]  dump_stack+0x18/0x30
[  192.875598]  vpanic+0x280/0x498
[  192.878806]  panic+0x68/0x70
[  192.881745]  nvhe_hyp_panic_handler+0x184/0x190
[  192.886372]  kvm_arm_vcpu_enter_exit+0x24/0x100
[  192.891003]  kvm_arch_vcpu_ioctl_run+0x254/0x7c0
[  192.895726]  kvm_vcpu_ioctl+0x174/0xb40
[  192.899645]  __arm64_sys_ioctl+0xb0/0x120

Quoted 9 lines

[  192.903745]  invoke_syscall.constprop.0+0xa8/0x100 [  192.908639]  do_el0_svc+0xb8/0xe0
[  192.912022]  el0_svc+0x48/0x1f8
[  192.915228]  el0t_64_sync_handler+0xa0/0xe8 [  192.919500]  el0t_64_sync+0x1ac/0x1b0
[  192.923243] SMP: stopping secondary CPUs
[  192.927653] Starting crashdump kernel...
[  192.931658] Bye!

With the following changes applied after PATCH[01-05] on top of v7.3.rc4, the crash is avoided.

Quoted 7 lines

In arch/arm64/include/asm/kvm_host.h:

-#define kvm_vm_is_protected(kvm)       ((kvm)->arch.vm_flavor >= __VM_PROTECTED)
+#define kvm_vm_is_protected(kvm)       \
+       (is_protected_kvm_enabled() && (kvm)->arch.vm_flavor >= __VM_PROTECTED)

That only papers over the problem. We can't use the vcpu_is_* constructs from nvhe hyp, without converting the vcpu->kvm to the hyp address, before accessing it. The fix is a bit more involved. One option is to define the vcpu_is_* helpers only for the !NVHE hyp code in the kvm_host.h (to avoid pulling in the asm/kvm_mmu.h in to kvm_host.h and then make a mess with header dependencies) and define the NVHE version in asm/kvm_hyp.h. Something like :

arch/arm64/include/asm/kvm_host.h +4 -1
index 46a7f6c1e426c..4016d09ed39c3 100644
@@ -1545,11 +1545,14 @@ 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)
+
+#ifndef __KVM_NVHE_HYPERVISOR__
   #define vcpu_is_protected(vcpu) kvm_vm_is_protected((vcpu)->kvm)
+#define vcpu_is_protected_pkvm(vcpu) 
 kvm_vm_is_protected_pkvm((vcpu)- >kvm)
+#endif
 
   #define kvm_vm_is_protected_pkvm(kvm)          \
          (is_protected_kvm_enabled() && ((kvm)->arch.vm_flavor == VM_PROTECTED_PKVM))
-#define vcpu_is_protected_pkvm(vcpu) 
 kvm_vm_is_protected_pkvm(vcpu- >kvm)
 

  #define kvm_vm_is_unprotected_pkvm(kvm)                \
         (is_protected_kvm_enabled() && ((kvm)->arch.vm_flavor == VM_PKVM))

arch/arm64/include/asm/kvm_hyp.h +0 -0
index 4974492744cc8..3bf87e81af430 100644
@@ -137,6 +137,19 @@ int __pkvm_init(phys_addr_t phys, unsigned 

long size, unsigned long *per_cpu_bas
  void __noreturn __host_enter(struct kvm_cpu_context *host_ctxt); #endif

arch/arm64/include/asm/kvm_hyp.h +4 -0
+#ifdef __KVM_NVHE_HYPERVISOR__
+#define vcpu_is_protected(vcpu)        \
+ ({                                                              \
+               struct kvm *__kvm = READ_ONCE((vcpu)- 
Quoted 1 line

kvm);             \

+               __kvm && kvm_vm_is_protected((kern_hyp_va(__kvm)));     \
+       })
+#define
vcpu_is_protected_pkvm(vcpu)                                   \
+ ({                                                              \
+               struct kvm *__kvm = READ_ONCE((vcpu)-

Quoted 1 line

kvm);             \

+               __kvm &&
kvm_vm_is_protected_pkvm((kern_hyp_va(__kvm)));\
+       })

arch/arm64/include/asm/kvm_hyp.h +2 -0
+#endif
+
Quoted 4 lines

Or explicitly convert all nvhe accessors to a new "nvhe_vcpu_is_protected"

The second one sounds like a better option to me.

We just need to include "asm/kvm_mmu.h" to "arch/arm64/mmio.c". With it, we can have unified functions (macros) in kvm_host.h to accomodate all cases, like below.

arch/arm64/include/asm/kvm_host.h +3 -1
index 9b1cf9c59e81..b9c9a9f203f9 100644
@@ -1514,11 +1514,36 @@ 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)
-#define vcpu_is_protected(vcpu) kvm_vm_is_protected((vcpu)->kvm)
+#define 
 vcpu_is_protected(vcpu)                                                \
+       
 ({                                                              \
+               struct kvm *__kvm = READ_ONCE((vcpu)- 
Quoted 8 lines

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

Quoted 8 lines
arch/arm64/include/asm/kvm_host.h +4 -0
 >kvm);             \
+               bool __protected = 
 false;                               \
+                                                                       \
+               if (__kvm) 
 {                                            \
+                       if 
 (is_nvhe_hyp_code())                         \
Quoted 3 lines

This needs to be :
               if (is_nvhe_hyp_code() && !is_protected_kvm_enabled())

Yes.

Quoted 8 lines
Quoted 6 lines
Quoted 2 lines

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

Quoted 2 lines
arch/arm64/include/asm/kvm_host.h +1 -0
+                               __kvm = 
 kern_hyp_va(__kvm);             \

Otherwise, we unnecessarily convert the vcpu->kvm for a hyp_vcpu and that is not going to end well for pKVM.

Yes. kern_hyp_va() isn't needed when is_protected_kvm_enabled() is true.

nit : It is not that it is needed, but it is incorrect to convert the vcpu->kvm which is already a HYP VA via kern_hyp_va() which is not idempotent and thus we end up accessing something junk.

Quoted 3 lines

Besides, vcpu_is_protected_pkvm() can be further simplifed by fully exploiting the check (is_protected_kvm_enabled()) done in vcpu_is_protected_pkvm(), see below.

Good point.

Quoted 45 lines
Quoted 40 lines

Cheers
Suzuki

Quoted 35 lines
Quoted 35 lines
arch/arm64/include/asm/kvm_host.h +1 -0
+                       __protected = 

kvm_vm_is_protected(__kvm);       \
+               }                                                       \
+                                                                       \
+
__protected;                                            \
+        })
+

  #define kvm_vm_is_protected_pkvm(kvm)          \
         (is_protected_kvm_enabled() && ((kvm)->arch.vm_flavor == VM_PROTECTED_PKVM)) -#define vcpu_is_protected_pkvm(vcpu)
kvm_vm_is_protected_pkvm(vcpu- >kvm)
+#define
vcpu_is_protected_pkvm(vcpu)                                   \
+
({                                                              \
+               struct kvm *__kvm = READ_ONCE((vcpu)-

Quoted 1 line

kvm);             \

+               bool __protected =
false;                               \
+                                                                       \
+               if (__kvm)
{                                            \
+                       if
(is_nvhe_hyp_code())                         \
+                               __kvm =
kern_hyp_va(__kvm);             \
+                       __protected = kvm_vm_is_protected_pkvm(__kvm);  \
+               }                                                       \
+                                                                       \
+
__protected;                                            \
+        })

This can be further simplified by fully expliting the check (is_protected_kvm_enabled()) in kvm_vm_is_protected_pkvm(), as below. The point is that the check is_protected_kvm_enabled() gurantees that kern_hyp_va() isn't needed.

Quoted 11 lines

#define vcpu_is_protected_pkvm(vcpu)                                    \
        ({                                                              \
                struct kvm *__kvm = READ_ONCE((vcpu)->kvm);             \
                bool __protected = false;                               \
                                                                        \
                if (__kvm)                                              \
                        __protected = kvm_vm_is_protected_pkvm(__kvm);  \
                                                                        \
                __protected;                                            \
        })

Ack, I have reduced this to:

/*
* Rely on is_protected_kvm_enabled() check in
kvm_vm_is_protected_pkvm() to
* make sure the vcpu->kvm is always valid VA in the context */

#define vcpu_is_protected_pkvm(vcpu)                                    \
        ({                                                              \
                struct kvm *__kvm = READ_ONCE((vcpu)->kvm);             \
                                                                        \
                (__kvm) && kvm_vm_is_protected_pkvm(__kvm));            \
        })

Cheers
Suzuki

Quoted 23 lines
Quoted 14 lines
Quoted 14 lines
Quoted 14 lines

  #define kvm_vm_is_unprotected_pkvm(kvm)                \
         (is_protected_kvm_enabled() && ((kvm)->arch.vm_flavor == VM_PKVM))

arch/arm64/kvm/mmio.c +1 -0
index d1c3a352d5a2..ab1d2fef9a52 100644
@@ -6,6 +6,7 @@
 
   #include <linux/kvm_host.h>
   #include <asm/kvm_emulate.h>
+#include <asm/kvm_mmu.h>
   #include <trace/events/kvm.h>
 
Quoted 5 lines
Quoted 5 lines

arch/arm64/kvm/mmio.c line 12

Quoted 2 lines
arch/arm64/kvm/mmio.c +0 -0
   #include "trace.h"

Thanks Gavin, that works and looks neat. I will incorporate it.

Thanks,
Gavin

#31 lore raw

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

Quoted 224 lines · Suzuki K Poulose
Quoted 223 lines · Gavin Shan
Quoted 204 lines · Suzuki K Poulose
Quoted 203 lines · Suzuki K Poulose
Quoted 202 lines · Gavin Shan
Quoted 187 lines · Suzuki K Poulose
Quoted 127 lines · Gavin Shan
Quoted 111 lines · Gavin Shan
Quoted 70 lines · 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.

With that, add wrappers for checking the "type" of a VM and replace the existing users with the new wrappers.

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.

Adds __VM_PROTECTED marker vm flavor to generalize kvm_vm_is_protected() to predicate all confidential guests running on KVM. In later patches, we would add Realm VMs, which would also be classified as protected.

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. To tighten this init_pkvm_hyp_vm() restricts the hyp copy of the vm_flavor to the ones it supports.

---
Changes since v18:
  - Merge the __VM_PROTECTED marker and the widening of kvm_vm_is_protected() to this patch.
  - Merge the use of kvm_vm_is_unprotected_pkvm() for ! kvm_vm_is_protected() given the scope changes here.
  - Drop Fuad's review tag, as this patch has multiple merges
  - Restrict the VM flavors to the supported types in init_pkvm_hyp_vm().
  - Drop kvm_vm_hyp_is_pkvm() and revert to is_protected_kvm_enabled()
  - Use is_protected_kvm_enabled() to make the pKVM guest flavor checks.
  - s/PKVM/pKVM for commit descriptions too

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 | 22 +++++++++++++++++++--- arch/arm64/include/asm/kvm_pkvm.h |  4 ++--
  arch/arm64/kvm/arm.c              | 31 +++++++++++++++++++++++ ++ +----- arch/arm64/kvm/handle_exit.c      |  2 +- arch/arm64/kvm/hyp/nvhe/pkvm.c    |  6 +++++- arch/arm64/kvm/mmu.c              |  2 +-
  arch/arm64/kvm/pkvm.c             |  6 ++----

Quoted 2 lines
  7 files changed, 56 insertions(+), 17 deletions(-)

This causes broken nVHE mode. I applied PATCH[01-05] to v7.3.rc4 whose head commit is f0100363d8c3, and kselftests/kvm/guest_print causes host crash (as below). I don't see the crash if only PATCH[01-04] are applied.

host$ cat /proc/cmdline | grep kvm-arm\.mode
BOOT_IMAGE=(hd0,gpt2)/vmlinuz-7.3.0-rc4-gavin+ root=/dev/mapper/ rhel_nvidia--grace--hopper--01-root ro crashkernel=2G-4G:406M,4G-64G:470M,64G-:726M rd.lvm.lv=rhel_nvidia- grace-hopper-01/root rd.lvm.lv=rhel_nvidia-grace-hopper-01/swap video=simplefb:off kvm-arm.mode=nvhe

host$ cd linux/tools/testing/selftests/kvm
host$ ./guest_print_test
Random seed: 0x193a0ed3
[  192.754328] kvm [6674]: nVHE hyp panic at: [<ffff8000811da044>] __kvm_nvhe___timer_enable_traps+0x4/0x160! [  192.754338] kvm [6674]: nVHE call trace:
[  192.754339] kvm [6674]:  [<ffff8000811dcf34>] __kvm_nvhe_hyp_panic+0xb4/0xe0
[  192.754342] kvm [6674]:  [<ffff8000811dcba4>] __kvm_nvhe___kvm_vcpu_run+0x164/0x440
[  192.754344] kvm [6674]:  [<ffff8000811dfe90>] __kvm_nvhe_handle___kvm_vcpu_run+0x40/0x1f0
[  192.754346] kvm [6674]:  [<ffff8000811e0238>] __kvm_nvhe_handle_trap+0x158/0x280
[  192.754347] kvm [6674]:  [<ffff8000811dd8fc>] __kvm_nvhe___skip_pauth_save+0x4/0x4
[  192.754348] kvm [6674]: ---[ end nVHE call trace ]---
[  192.754350] Code: d2818002 17fffff7 d503201f f9400001 (b94a6821)
[  192.754350] kvm [6674]: Hyp Offset: 0xfffeb0d7fe2e0000
[  192.754351] Kernel panic - not syncing: HYP panic:
[  192.754351] PS:834003c9 PC:0000cf2882efa044 ESR:0000000096000004
[  192.754351] FAR:ffff00009b286a68 HPFAR:8000000000000000 PAR:1d00ec7edbadc8de
[  192.754351] VCPU:0000cf011848a350
[  192.845154] CPU: 12 UID: 0 PID: 6674 Comm: guest_print_tes Kdump: loaded Not tainted 7.3.0-rc4-gavin+ #7 PREEMPT(full) [  192.856182] Hardware name:  GH200 P5042, BIOS 02.04.01 20250422
[  192.862231] Call trace:
[  192.864725]  show_stack+0x20/0x38 (C)
[  192.868471]  dump_stack_lvl+0x88/0xb8
[  192.872215]  dump_stack+0x18/0x30
[  192.875598]  vpanic+0x280/0x498
[  192.878806]  panic+0x68/0x70
[  192.881745]  nvhe_hyp_panic_handler+0x184/0x190
[  192.886372]  kvm_arm_vcpu_enter_exit+0x24/0x100
[  192.891003]  kvm_arch_vcpu_ioctl_run+0x254/0x7c0
[  192.895726]  kvm_vcpu_ioctl+0x174/0xb40
[  192.899645]  __arm64_sys_ioctl+0xb0/0x120

Quoted 9 lines

[  192.903745]  invoke_syscall.constprop.0+0xa8/0x100 [  192.908639]  do_el0_svc+0xb8/0xe0
[  192.912022]  el0_svc+0x48/0x1f8
[  192.915228]  el0t_64_sync_handler+0xa0/0xe8 [  192.919500]  el0t_64_sync+0x1ac/0x1b0
[  192.923243] SMP: stopping secondary CPUs
[  192.927653] Starting crashdump kernel...
[  192.931658] Bye!

With the following changes applied after PATCH[01-05] on top of v7.3.rc4, the crash is avoided.

In arch/arm64/include/asm/kvm_host.h:

Review

Quoted 3 lines
(quoted diff) +2 -1
-#define kvm_vm_is_protected(kvm)       ((kvm)->arch.vm_flavor >= __VM_PROTECTED)
+#define kvm_vm_is_protected(kvm)       \
+       (is_protected_kvm_enabled() && (kvm)->arch.vm_flavor >= __VM_PROTECTED)

That only papers over the problem. We can't use the vcpu_is_* constructs from nvhe hyp, without converting the vcpu->kvm to the hyp address, before accessing it. The fix is a bit more involved. One option is to define the vcpu_is_* helpers only for the !NVHE hyp code in the kvm_host.h (to avoid pulling in the asm/kvm_mmu.h in to kvm_host.h and then make a mess with header dependencies) and define the NVHE version in asm/kvm_hyp.h. Something like :

arch/arm64/include/asm/kvm_host.h +4 -1
index 46a7f6c1e426c..4016d09ed39c3 100644
@@ -1545,11 +1545,14 @@ 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)
+
+#ifndef __KVM_NVHE_HYPERVISOR__
   #define vcpu_is_protected(vcpu) kvm_vm_is_protected((vcpu)->kvm)
+#define vcpu_is_protected_pkvm(vcpu) kvm_vm_is_protected_pkvm((vcpu)- >kvm)
+#endif
 
   #define kvm_vm_is_protected_pkvm(kvm)          \
          (is_protected_kvm_enabled() && ((kvm)->arch.vm_flavor == VM_PROTECTED_PKVM))
-#define vcpu_is_protected_pkvm(vcpu) kvm_vm_is_protected_pkvm(vcpu- >kvm)
 
   #define kvm_vm_is_unprotected_pkvm(kvm)                \
          (is_protected_kvm_enabled() && ((kvm)->arch.vm_flavor == VM_PKVM))
arch/arm64/include/asm/kvm_hyp.h +13 -0
index 4974492744cc8..3bf87e81af430 100644
@@ -137,6 +137,19 @@ int __pkvm_init(phys_addr_t phys, unsigned long size, unsigned long *per_cpu_bas
   void __noreturn __host_enter(struct kvm_cpu_context *host_ctxt);
   #endif
 
+#ifdef __KVM_NVHE_HYPERVISOR__
+#define vcpu_is_protected(vcpu)        \
+ ({                                                              \
+               struct kvm *__kvm = READ_ONCE((vcpu)- >kvm);             \
+               __kvm && kvm_vm_is_protected((kern_hyp_va(__kvm)));     \
+       })
+#define vcpu_is_protected_pkvm(vcpu)                                   \
+ ({                                                              \
+               struct kvm *__kvm = READ_ONCE((vcpu)- >kvm);             \
+               __kvm && kvm_vm_is_protected_pkvm((kern_hyp_va(__kvm)));\
+       })
+#endif
+
 
 

Or explicitly convert all nvhe accessors to a new "nvhe_vcpu_is_protected"

Quoted 2 lines

The second one sounds like a better option to me.

We just need to include "asm/kvm_mmu.h" to "arch/arm64/mmio.c". With it, we can have unified functions (macros) in kvm_host.h to accomodate all cases, like below.

arch/arm64/include/asm/kvm_host.h +0 -0
index 9b1cf9c59e81..b9c9a9f203f9 100644
@@ -1514,11 +1514,36 @@ struct kvm *kvm_arch_alloc_vm(void);
   #define __KVM_HAVE_ARCH_FLUSH_REMOTE_TLBS_RANGE
 
Quoted 13 lines
Quoted 9 lines

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

Quoted 9 lines
arch/arm64/include/asm/kvm_host.h +7 -1
   #define kvm_vm_is_protected(kvm)       ((kvm)->arch.vm_flavor >= __VM_PROTECTED)
-#define vcpu_is_protected(vcpu) kvm_vm_is_protected((vcpu)->kvm)
+#define vcpu_is_protected(vcpu)                                                \
+ ({                                                              \
+               struct kvm *__kvm = READ_ONCE((vcpu)- >kvm);             \
+               bool __protected = false;                               \
+                                                                       \
+               if (__kvm) {                                            \
+                       if (is_nvhe_hyp_code())                         \

This needs to be :
               if (is_nvhe_hyp_code() && !is_protected_kvm_enabled())

Yes.

Quoted 2 lines
Quoted 1 line
Quoted 1 line
arch/arm64/include/asm/kvm_host.h +1 -0
+                               __kvm = kern_hyp_va(__kvm);             \
Quoted 9 lines
Quoted 5 lines
Quoted 3 lines

Otherwise, we unnecessarily convert the vcpu->kvm for a hyp_vcpu and that is not going to end well for pKVM.

Yes. kern_hyp_va() isn't needed when is_protected_kvm_enabled() is true.

nit : It is not that it is needed, but it is incorrect to convert the vcpu->kvm which is already a HYP VA via kern_hyp_va() which is not idempotent and thus we end up accessing something junk.

Yes.

Quoted 54 lines
Quoted 3 lines

Besides, vcpu_is_protected_pkvm() can be further simplifed by fully exploiting the check (is_protected_kvm_enabled()) done in vcpu_is_protected_pkvm(), see below.

Good point.

Quoted 33 lines
Quoted 12 lines

Cheers
Suzuki

Quoted 7 lines
Quoted 7 lines
arch/arm64/include/asm/kvm_host.h +6 -0
+                       __protected = kvm_vm_is_protected(__kvm);       \
+               }                                                       \
+                                                                       \
+ __protected;                                            \
+        })
+
 
Quoted 16 lines
Quoted 8 lines
Quoted 8 lines
arch/arm64/include/asm/kvm_host.h +5 -1
   #define kvm_vm_is_protected_pkvm(kvm)          \
          (is_protected_kvm_enabled() && ((kvm)->arch.vm_flavor == VM_PROTECTED_PKVM))
-#define vcpu_is_protected_pkvm(vcpu) kvm_vm_is_protected_pkvm(vcpu- >kvm)
+#define vcpu_is_protected_pkvm(vcpu)                                   \
+ ({                                                              \
+               struct kvm *__kvm = READ_ONCE((vcpu)- >kvm);             \
+               bool __protected = false;                               \
+                                                                       \
Quoted 8 lines

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

Quoted 8 lines
arch/arm64/include/asm/kvm_host.h +8 -0
+               if (__kvm) {                                            \
+                       if (is_nvhe_hyp_code())                         \
+                               __kvm = kern_hyp_va(__kvm);             \
+                       __protected = kvm_vm_is_protected_pkvm(__kvm);  \
+               }                                                       \
+                                                                       \
+ __protected;                                            \
+        })

This can be further simplified by fully expliting the check (is_protected_kvm_enabled()) in kvm_vm_is_protected_pkvm(), as below. The point is that the check is_protected_kvm_enabled() gurantees that kern_hyp_va() isn't needed.

Quoted 11 lines

#define vcpu_is_protected_pkvm(vcpu)                                    \
         ({                                                              \
                 struct kvm *__kvm = READ_ONCE((vcpu)->kvm);             \
                 bool __protected = false;                               \
                                                                         \
                 if (__kvm)                                              \
                         __protected = kvm_vm_is_protected_pkvm(__kvm);  \
                                                                         \
                 __protected;                                            \
         })

Ack, I have reduced this to:

Quoted 11 lines

/*
 * Rely on is_protected_kvm_enabled() check in kvm_vm_is_protected_pkvm() to
 * make sure the vcpu->kvm is always valid VA in the context
 */
#define vcpu_is_protected_pkvm(vcpu)                                    \
        ({                                                              \
                struct kvm *__kvm = READ_ONCE((vcpu)->kvm);             \
                                                                        \
                (__kvm) && kvm_vm_is_protected_pkvm(__kvm));            \
        })

Parentheses for the local variable '__kvm' can be dropped. With this, this looks good to me.

                   (__kvm && kvm_vm_is_protected_pkvm(__kvm));

Thanks,
Gavin

Quoted 28 lines

Cheers
Suzuki

Quoted 23 lines
Quoted 14 lines
Quoted 14 lines
Quoted 14 lines
arch/arm64/include/asm/kvm_host.h +0 -0
   #define kvm_vm_is_unprotected_pkvm(kvm)                \
          (is_protected_kvm_enabled() && ((kvm)->arch.vm_flavor == VM_PKVM))
arch/arm64/kvm/mmio.c +1 -0
index d1c3a352d5a2..ab1d2fef9a52 100644
@@ -6,6 +6,7 @@
 
   #include <linux/kvm_host.h>
   #include <asm/kvm_emulate.h>
+#include <asm/kvm_mmu.h>
   #include <trace/events/kvm.h>
 
Quoted 5 lines
Quoted 5 lines

arch/arm64/kvm/mmio.c line 12

Quoted 2 lines
arch/arm64/kvm/mmio.c +0 -0
   #include "trace.h"

Thanks Gavin, that works and looks neat. I will incorporate it.

Thanks,
Gavin

Tested-by on the v19 series
#75 lore raw
In reply to Suzuki K Poulose
Quoted 42 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)

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

if (kvm_vm_is_protected() || kvm_vm_is_realm())

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. Also covers the SET_ONE_REG/GET_ONE_REG

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:

Quoted 9 lines

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-v19/integration
[4] https://support.arm.com/documentation/den0137/2-0bet3/

Apart from the issue found against PATCH[v19 05/20], I didn't see more issues with this series in my tests where kselftest/kvm cases and kvm-unit-tests are done on various
combinations: 4KB host kernel, 64KB host kernel, kernel parameter "kvm-arm.mode= {nvhe, protected}" or nil. So with the found issue caused by PATCH[v19 05/20] fixed:

Thanks,
Gavin

Quoted 122 lines

Changes since v18:

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

- Patch count down by 3, after merging different patches together, see more below
- Retain NULL vm_offset for pVMs and move the counter offset flag initialisation to kvm_timer_init_vm() - Marc
- Merge widening the scope of kvm_vm_is_protected() to the patch where the flavors are introduced(Marc) and also dropped Fuad's reviewed-by, as the patch is now bigger.
- Merge "Use kvm_vm_is_unprotected_pkvm" for !kvm_vm_is_protected to patch where flavor is introuced.
- Merge "vgic-v3" mandate and preventing vgic-v2 mappings to a single patch, where kvm_vm_hyp_is_distrusting() introduced - Fuad
- Drop kvm_vm_hyp_is_pkvm(), reverting to is_protected_kvm_enabled()
- Use is_protected_kvm_enabeld() for pKVM guest flavor checks.
- s/PKVM/pKVM for commit descriptions too
- Make sure the vm_mem_abort callback is !NULL at init time.
- Bail out early for !pKVM && !Realm VMs in kvm_vm_ioctl_allowed(). Use kvm_vm_hyp_is_distrusting()
- WARN_ON_ONCE(!kvm) for kvm_vm_ioctl_allowed() as it must be only called with a valid kvm instance and only from kvm_arch_vm_ioctl()
- Rename kvm_arch_vm_{ext,ioctl}_allowed => kvm_vm_{ext,ioctl}_allowed - Fuad
- Move kvm_realm_ext_allowed() to asm/kvm_rmi.h - Fuad
- Don't expose PMCR_EL0 to the userspace until we support PMU

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 (4):
KVM: arm64: Avoid including linux/kvm_host.h in kvm_pgtable.h
KVM: arm64: CCA: Introduce Realms
KVM: arm64: CCA: WARN on injected undef exceptions
KVM: arm64: CCA: Support timers in realm RECs

Suzuki K Poulose (14):
KVM: arm64: protected VM: Handle user writes to 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: Mandate VGIC v3 for 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: 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             |  69 +++-
  arch/arm64/include/asm/kvm_pgtable.h          |   6 +-
  arch/arm64/include/asm/kvm_pkvm.h             |  25 +-
  arch/arm64/include/asm/kvm_rmi.h              |  84 +++++
  arch/arm64/include/asm/virt.h                 |   1 +
  arch/arm64/kvm/Makefile                       |   2 +-
  arch/arm64/kvm/arch_timer.c                   |  34 +-
  arch/arm64/kvm/arm.c                          | 300 +++++++++++++++---
  arch/arm64/kvm/guest.c                        |  73 ++++-
  arch/arm64/kvm/handle_exit.c                  |   2 +-
  arch/arm64/kvm/hyp/nvhe/pkvm.c                |   6 +-
  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                     |  28 +-
  arch/arm64/kvm/vgic/vgic-init.c               |   2 +
  include/kvm/arm_psci.h                        |   2 +
  23 files changed, 752 insertions(+), 155 deletions(-)

create mode 100644 arch/arm64/include/asm/kvm_rmi.h create mode 100644 arch/arm64/kvm/rmi.c

author reviewer
#76 lore raw
Quoted 52 lines · Gavin Shan
Quoted 41 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)

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

      if (kvm_vm_is_protected() || kvm_vm_is_realm())

    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. Also covers the SET_ONE_REG/GET_ONE_REG

  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:

Quoted 9 lines

   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- v19/integration
[4] https://support.arm.com/documentation/den0137/2-0bet3/

Quoted 6 lines

Apart from the issue found against PATCH[v19 05/20], I didn't see more issues with this series in my tests where kselftest/kvm cases and kvm-unit-tests are done on various
combinations: 4KB host kernel, 64KB host kernel, kernel parameter "kvm- arm.mode= {nvhe, protected}" or nil. So with the found issue caused by PATCH[v19 05/20] fixed:

Thank you Gavin, much appreciated !

Cheers
Suzuki

author reviewer
#44 lore raw

Re: [PATCH v19 09/20] KVM: arm64: Add VM specific callback for S2 MMU operations

In reply to Jonathan Cameron
Quoted 65 lines · Jonathan Cameron
Quoted 62 lines · On Wed, 23 Sep 2026 00:21:58 +0100 Suzuki K Poulose <[email protected]>…
Quoted 35 lines · Jonathan Cameron
Quoted 4 lines · On Sun, 20 Sep 2026 22:28:34 +0100 Suzuki K Poulose <[email protected]>…

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

A small suggestion inline that makes it explicit when there isn't range operation and a full flush of tlb is needed.

I don't mind either way, but thought I'd mention it anyway in case you hadn't already dismissed it.

Quoted 18 lines
arch/arm64/kvm/mmu.c +0 -2
index 03f2017a7404a..d97a4a1bca23f 100644
@@ -37,6 +37,8 @@ static unsigned long __ro_after_init io_map_base;
 
   #define KVM_PGT_FN(fn)		(!is_protected_kvm_enabled() ? fn : p ## fn)
 
 
   /**
    * kvm_arch_flush_remote_tlbs() - flush all VM TLB entries for v7/8
    * @kvm:	pointer to kvm structure.
@@ -174,26 +188,36 @@ static bool memslot_is_logging(struct kvm_memory_slot *memslot)
    */
   int kvm_arch_flush_remote_tlbs(struct kvm *kvm)
   {
-	if (is_protected_kvm_enabled())
-		kvm_call_hyp_nvhe(__pkvm_tlb_flush_vmid, kvm->arch.pkvm.handle);
Quoted 8 lines

arch/arm64/kvm/mmu.c static bool memslot_is_logging(struct kvm_memory_slot *memslot) · line 192

Quoted 5 lines
arch/arm64/kvm/mmu.c +2 -3
-	else
-		kvm_call_hyp(__kvm_tlb_flush_vmid, &kvm->arch.mmu);
-	return 0;
+	if (!kvm->arch.vm_s2_ops->vm_flush_remote_tlbs)
+		return 1;

For consistency with other cases and because it looks nicer blank line here.

Ack

Quoted 14 lines

arch/arm64/kvm/mmu.c static bool memslot_is_logging(struct kvm_memory_slot *memslot) · line 199

Quoted 10 lines
arch/arm64/kvm/mmu.c +5 -2
+	return kvm->arch.vm_s2_ops->vm_flush_remote_tlbs(kvm);
   }
 
-int kvm_arch_flush_remote_tlbs_range(struct kvm *kvm,
-				      gfn_t gfn, u64 nr_pages)
+static int pkvm_flush_remote_tlbs_range(struct kvm *kvm,
+					gfn_t gfn, u64 nr_pages)
+{
+	return pkvm_flush_remote_tlbs(kvm);

For the cases where there isn't a range call, perhaps you should just not provide the op and have the wrapper fall back to calling the 'flush all' variant if there is no range one available?

Quoted 6 lines
Quoted 2 lines

The core KVM falls back to flushing all TLBs if the range one returns true anyways. So, I would leave it as it is.

That sounds like a reason to stub them to return true always rather than call the flush all so we don't have fallbacks added at two layers of the stack?

But that doesn't help, as the only one that matters is pKVM, which handles this anyway. If tlbs_range is not available, then the all tlbs call back also return the same value. So, the core is anyway going to call the full version anyways.

So I have left it as it is. A sensible backend could do something like the pKVM does.

Cheers
Suzuki

Quoted 13 lines
Quoted 11 lines

Cheers
Suzuki

Quoted 4 lines
Quoted 2 lines
arch/arm64/kvm/mmu.c +1 -0
+}
Reviewed-by on v19 2/20
#11 lore raw

Re: [PATCH v19 02/20] KVM: arm64: Disable Steal time accounting for protected guests

In reply to Suzuki K Poulose
Quoted 6 lines · 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.

Quoted 8 lines
---
  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(-)
Reviewed-by on v19 8/20
#39 lore raw

Re: [PATCH v19 08/20] KVM: arm64: Reuse kvm_stage2_unmap_range in kvm_unmap_gfn_range

In reply to Suzuki K Poulose
Quoted 7 lines · 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.

Quoted 8 lines

No functional changes intended.

---
  arch/arm64/kvm/mmu.c | 8 ++++----
  1 file changed, 4 insertions(+), 4 deletions(-)
#45 lore raw

Re: [PATCH v19 09/20] KVM: arm64: Add VM specific callback for S2 MMU operations

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

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

---
  arch/arm64/include/asm/kvm_host.h |  15 ++++
  arch/arm64/kvm/mmu.c              | 137 +++++++++++++++++++++++++-----
  2 files changed, 131 insertions(+), 21 deletions(-)

Apart from the comments from Jonathan, some nitpicks and questions below.

Quoted 66 lines
arch/arm64/include/asm/kvm_host.h +15 -0
index 149f4582c8b6a..7664d8b8cce5a 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;
 
@@ -332,6 +345,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 +14 -0
index 03f2017a7404a..d97a4a1bca23f 100644
@@ -37,6 +37,8 @@ static unsigned long __ro_after_init io_map_base;
 
  #define KVM_PGT_FN(fn)		(!is_protected_kvm_enabled() ? fn : p ## fn)
 
+static int kvm_vm_init_vm_s2_ops(struct kvm *kvm);
+
  static phys_addr_t __stage2_range_addr_end(phys_addr_t addr, phys_addr_t end,
  					   phys_addr_t size)
  {
@@ -166,6 +168,18 @@ static bool memslot_is_logging(struct kvm_memory_slot *memslot)
  	return memslot->dirty_bitmap && !(memslot->flags & KVM_MEM_READONLY);
  }
 
+static int pkvm_flush_remote_tlbs(struct kvm *kvm)
+{
+	kvm_call_hyp_nvhe(__pkvm_tlb_flush_vmid, kvm->arch.pkvm.handle);
+	return 0;
+}
+
+static int kvm_vm_flush_remote_tlbs(struct kvm *kvm)
+{
+	kvm_call_hyp(__kvm_tlb_flush_vmid, &kvm->arch.mmu);
+	return 0;
+}
+
  /**
   * kvm_arch_flush_remote_tlbs() - flush all VM TLB entries for v7/8
   * @kvm:	pointer to kvm structure.

arch/arm64/kvm/mmu.c static bool memslot_is_logging(struct kvm_memory_slot *memslot) · line 192

Quoted 10 lines
arch/arm64/kvm/mmu.c +2 -5
@@ -174,26 +188,36 @@ static bool memslot_is_logging(struct kvm_memory_slot *memslot)
   */
  int kvm_arch_flush_remote_tlbs(struct kvm *kvm)
  {
-	if (is_protected_kvm_enabled())
-		kvm_call_hyp_nvhe(__pkvm_tlb_flush_vmid, kvm->arch.pkvm.handle);
-	else
-		kvm_call_hyp(__kvm_tlb_flush_vmid, &kvm->arch.mmu);
-	return 0;
+	if (!kvm->arch.vm_s2_ops->vm_flush_remote_tlbs)
+		return 1;

For the return value, I'm wandering if 0 should be returned. More details can be found below.

Quoted 21 lines
arch/arm64/kvm/mmu.c +9 -6
+	return kvm->arch.vm_s2_ops->vm_flush_remote_tlbs(kvm);
  }
 
-int kvm_arch_flush_remote_tlbs_range(struct kvm *kvm,
-				      gfn_t gfn, u64 nr_pages)
+static int pkvm_flush_remote_tlbs_range(struct kvm *kvm,
+					gfn_t gfn, u64 nr_pages)
+{
+	return pkvm_flush_remote_tlbs(kvm);
+}
+
+static int kvm_vm_flush_remote_tlbs_range(struct kvm *kvm,
+					 gfn_t gfn, u64 nr_pages)
  {
  	u64 size = nr_pages << PAGE_SHIFT;
  	u64 addr = gfn << PAGE_SHIFT;
 
-	if (is_protected_kvm_enabled())
-		kvm_call_hyp_nvhe(__pkvm_tlb_flush_vmid, kvm->arch.pkvm.handle);
-	else
-		kvm_tlb_flush_vmid_range(&kvm->arch.mmu, addr, size);

arch/arm64/kvm/mmu.c static bool memslot_is_logging(struct kvm_memory_slot *memslot) · line 217

Quoted 10 lines
arch/arm64/kvm/mmu.c +7 -0
+	kvm_tlb_flush_vmid_range(&kvm->arch.mmu, addr, size);
  	return 0;
  }
 
+int kvm_arch_flush_remote_tlbs_range(struct kvm *kvm,
+				     gfn_t gfn, u64 nr_pages)
+{
+	if (!kvm->arch.vm_s2_ops->vm_flush_remote_tlbs_range)
+		return 1;
+

Realm would the only case where vm_s2_ops->vm_flush_remote_{tlbs, tlbs_range) are NULL. On request to flush remote TLBs by kvm_flush_remote_tlbs_range(), it ends up with event KVM_REQ_TLB_FLUSH queued for each vCPU. How this queued event is linked to a remote TLB flush for realm? The problem is TLBs are owned by EL2 realm and there are no RMI calls for the management. So I'm wandering we should return 0 here?

Quoted 121 lines
arch/arm64/kvm/mmu.c +58 -10
+	return kvm->arch.vm_s2_ops->vm_flush_remote_tlbs_range(kvm, gfn, nr_pages);
+}
+
  static void *stage2_memcache_zalloc_page(void *arg)
  {
  	struct kvm_mmu_memory_cache *mc = arg;
@@ -337,13 +361,20 @@ static void __unmap_stage2_range(struct kvm_s2_mmu *mmu, phys_addr_t start, u64
  				   may_block));
  }
 
+static void kvm_vm_stage2_unmap_range(struct kvm_s2_mmu *mmu,
+				      phys_addr_t start,
+				      u64 size, bool may_block)
+{
+	__unmap_stage2_range(mmu, start, size, may_block);
+}
+
  void kvm_stage2_unmap_range(struct kvm_s2_mmu *mmu, phys_addr_t start,
  			    u64 size, bool may_block)
  {
-	if (kvm_vm_is_protected(kvm_s2_mmu_to_kvm(mmu)))
-		return;
+	struct kvm *kvm = kvm_s2_mmu_to_kvm(mmu);
 
-	__unmap_stage2_range(mmu, start, size, may_block);
+	if (kvm->arch.vm_s2_ops->vm_stage2_unmap_range)
+		kvm->arch.vm_s2_ops->vm_stage2_unmap_range(mmu, start, size, may_block);
  }
 
  void kvm_stage2_flush_range(struct kvm_s2_mmu *mmu, phys_addr_t addr, phys_addr_t end)
@@ -983,6 +1014,12 @@ int kvm_init_stage2_mmu(struct kvm *kvm, struct kvm_s2_mmu *mmu, unsigned long t
  	int cpu, err;
  	struct kvm_pgtable *pgt;
 
+	/* Initialize the VM ops for the VM instance for the first time */
+	if (mmu == &kvm->arch.mmu) {
+		err = kvm_vm_init_vm_s2_ops(kvm);
+		if (err)
+			return err;
+	}
  	/*
  	 * If we already have our page tables in place, and that the
  	 * MMU context is the canonical one, we have a bug somewhere,
@@ -2447,34 +2484,46 @@ bool kvm_unmap_gfn_range(struct kvm *kvm, struct kvm_gfn_range *range)
  	return false;
  }
 
-bool kvm_age_gfn(struct kvm *kvm, struct kvm_gfn_range *range)
+static bool kvm_vm_age_gfn(struct kvm *kvm, struct kvm_gfn_range *range)
  {
  	u64 size = (range->end - range->start) << PAGE_SHIFT;
 
-	if (!kvm->arch.mmu.pgt || kvm_vm_is_protected(kvm))
-		return false;
-
  	return KVM_PGT_FN(kvm_pgtable_stage2_test_clear_young)(kvm->arch.mmu.pgt,
  						   range->start << PAGE_SHIFT,
  						   size, true);
+}
+
+bool kvm_age_gfn(struct kvm *kvm, struct kvm_gfn_range *range)
+{
+	if (!kvm->arch.mmu.pgt || !kvm->arch.vm_s2_ops->vm_age_gfn)
+		return false;
+
+	return kvm->arch.vm_s2_ops->vm_age_gfn(kvm, range);
  	/*
  	 * TODO: Handle nested_mmu structures here using the reverse mapping in
  	 * a later version of patch series.
  	 */
  }
 
-bool kvm_test_age_gfn(struct kvm *kvm, struct kvm_gfn_range *range)
+static bool kvm_vm_test_age_gfn(struct kvm *kvm, struct kvm_gfn_range *range)
  {
  	u64 size = (range->end - range->start) << PAGE_SHIFT;
 
-	if (!kvm->arch.mmu.pgt || kvm_vm_is_protected(kvm))
-		return false;
 
  	return KVM_PGT_FN(kvm_pgtable_stage2_test_clear_young)(kvm->arch.mmu.pgt,
  						   range->start << PAGE_SHIFT,
  						   size, false);
  }
 
+bool kvm_test_age_gfn(struct kvm *kvm, struct kvm_gfn_range *range)
+{
+
+	if (!kvm->arch.mmu.pgt || !kvm->arch.vm_s2_ops->vm_test_age_gfn)
+		return false;
+
+	return kvm->arch.vm_s2_ops->vm_test_age_gfn(kvm, range);
+}
+
  phys_addr_t kvm_mmu_get_httbr(void)
  {
  	return __pa(hyp_pgtable->pgd);
@@ -2796,3 +2845,49 @@ void kvm_toggle_cache(struct kvm_vcpu *vcpu, bool was_enabled)
 
  	trace_kvm_toggle_cache(*vcpu_pc(vcpu), was_enabled, now_enabled);
  }
+
+static const struct kvm_vm_s2_ops protected_pkvm_vm_s2_ops = {
+	.vm_flush_remote_tlbs		= pkvm_flush_remote_tlbs,
+	.vm_flush_remote_tlbs_range	= pkvm_flush_remote_tlbs_range,
+	/*
+	 * Not supported for Protected VMs under pKVM
+	 * .vm_age_gfn
+	 * .vm_test_age_gfn
+	 * .vm_stage2_unmap_range
+	 */
+};
+
+static const struct kvm_vm_s2_ops pkvm_vm_s2_ops = {
+	.vm_flush_remote_tlbs		= pkvm_flush_remote_tlbs,
+	.vm_flush_remote_tlbs_range	= pkvm_flush_remote_tlbs_range,
+	.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,
+};
+

arch/arm64/kvm/mmu.c void kvm_toggle_cache(struct kvm_vcpu *vcpu, bool was_enabled) · line 2877

Quoted 10 lines
arch/arm64/kvm/mmu.c +10 -0
+static const struct kvm_vm_s2_ops kvm_default_vm_s2_ops = {
+	.vm_flush_remote_tlbs		= kvm_vm_flush_remote_tlbs,
+	.vm_flush_remote_tlbs_range	= kvm_vm_flush_remote_tlbs_range,
+	.vm_age_gfn			= kvm_vm_age_gfn,
+	.vm_test_age_gfn		= kvm_vm_test_age_gfn,
+	.vm_stage2_unmap_range		= kvm_vm_stage2_unmap_range,
+};
+
+#define KVM_VM_S2_OPS(flavor, ops)		\
+		[flavor] = ops

Parentheses are needed, to be consistent with KVM_VCPU_OPS at least.

#define KVM_VM_S2_OPS(flavor, ops)		\
		[(flavor)] = (ops)

Actually, KVM_{VCPU, VM_S2}_OPS() can be combined to one in kvm_host.h as below.

#define KVM_FLAVOR_OPS() [(flavor)] = (ops)

Quoted 6 lines
arch/arm64/kvm/mmu.c +6 -0
+static const struct kvm_vm_s2_ops *arm64_vm_s2_ops[] = {
+	KVM_VM_S2_OPS(VM_VHE, &kvm_default_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),
+};

arch/arm64/kvm/mmu.c void kvm_toggle_cache(struct kvm_vcpu *vcpu, bool was_enabled) · line 2893

Quoted 10 lines
arch/arm64/kvm/mmu.c +10 -0
+
+static int kvm_vm_init_vm_s2_ops(struct kvm *kvm)
+{
+	BUILD_BUG_ON(ARRAY_SIZE(arm64_vm_s2_ops) != VM_FLAVOR_MAX);
+
+	kvm->arch.vm_s2_ops = arm64_vm_s2_ops[kvm->arch.vm_flavor];
+	if (WARN_ON(!kvm->arch.vm_s2_ops))
+		return -EINVAL;
+	return 0;
+}

Thanks,
Gavin

Reviewed-by on v19 11/20
#55 lore raw

Re: [PATCH v19 11/20] KVM: arm64: Mandate VGIC v3 for for VMs running on hyp that don't trust the host

In reply to Suzuki K Poulose
Quoted 9 lines · 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 and prevent creation of VGIC other than v3.

---
Changes since v18:
- Cover pKVM guests for VGIC v3 mandate.
- Merge the VGIC mandate check in here.

Quoted 6 lines
---
  arch/arm64/include/asm/kvm_host.h | 3 +++
  arch/arm64/kvm/mmu.c              | 2 +-
  arch/arm64/kvm/vgic/vgic-init.c   | 2 ++
  3 files changed, 6 insertions(+), 1 deletion(-)
#59 lore raw

Re: [PATCH v19 13/20] KVM: arm64: CCA: Add VCPU load/put for Realms

In reply to Suzuki K Poulose
Quoted 16 lines · 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 +0 -0
index 0e2ab5311e4d8..bcc274df2a915 100644

arch/arm64/kvm/arm.c static void pkvm_vcpu_load(struct kvm_vcpu *vcpu) · line 802

Quoted 8 lines
arch/arm64/kvm/arm.c +5 -0
@@ -795,6 +795,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);

vcpu_set_wfx_traps() is updating HCR_{TWE, TWI} field for vcpu->arch.hcr_el2. Could you help to clarify those two fields need to be updated here? :-)

Quoted 20 lines
arch/arm64/kvm/arm.c +8 -0
+}
+
  void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
  {
  	vcpu->cpu = cpu;
@@ -845,6 +852,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);
@@ -2202,6 +2215,11 @@ static const struct kvm_vcpu_ops pkvm_vcpu_ops = {
  	.vcpu_put = pkvm_vcpu_put,

arch/arm64/kvm/arm.c static const struct kvm_vcpu_ops pkvm_vcpu_ops = { · line 2225

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

Thanks,
Gavin

#46 lore raw

Re: [PATCH v19 09/20] KVM: arm64: Add VM specific callback for S2 MMU operations

In reply to Gavin Shan
Quoted 114 lines · Gavin Shan
Quoted 9 lines · Suzuki K Poulose

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

---
  arch/arm64/include/asm/kvm_host.h |  15 ++++ arch/arm64/kvm/mmu.c              | 137 +++++++++++++++++++++++++-----

  2 files changed, 131 insertions(+), 21 deletions(-)

Apart from the comments from Jonathan, some nitpicks and questions below.

Quoted 60 lines
arch/arm64/include/asm/kvm_host.h +15 -0
index 149f4582c8b6a..7664d8b8cce5a 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;
@@ -332,6 +345,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 +14 -0
index 03f2017a7404a..d97a4a1bca23f 100644
@@ -37,6 +37,8 @@ static unsigned long __ro_after_init io_map_base;
   #define KVM_PGT_FN(fn)        (!is_protected_kvm_enabled() ? fn : p ## fn)
+static int kvm_vm_init_vm_s2_ops(struct kvm *kvm);
+
   static phys_addr_t __stage2_range_addr_end(phys_addr_t addr, phys_addr_t end,
                          phys_addr_t size)
   {
@@ -166,6 +168,18 @@ static bool memslot_is_logging(struct kvm_memory_slot *memslot)
       return memslot->dirty_bitmap && !(memslot->flags & KVM_MEM_READONLY);
   }
+static int pkvm_flush_remote_tlbs(struct kvm *kvm)
+{
+    kvm_call_hyp_nvhe(__pkvm_tlb_flush_vmid, kvm->arch.pkvm.handle);
+    return 0;
+}
+
+static int kvm_vm_flush_remote_tlbs(struct kvm *kvm)
+{
+    kvm_call_hyp(__kvm_tlb_flush_vmid, &kvm->arch.mmu);
+    return 0;
+}
+
   /**
    * kvm_arch_flush_remote_tlbs() - flush all VM TLB entries for v7/8
    * @kvm:    pointer to kvm structure.

arch/arm64/kvm/mmu.c static bool memslot_is_logging(struct kvm_memory_slot *memslot) · line 192

Quoted 10 lines
arch/arm64/kvm/mmu.c +2 -5
@@ -174,26 +188,36 @@ static bool memslot_is_logging(struct kvm_memory_slot *memslot)
    */
   int kvm_arch_flush_remote_tlbs(struct kvm *kvm)
   {
-    if (is_protected_kvm_enabled())
-        kvm_call_hyp_nvhe(__pkvm_tlb_flush_vmid, kvm->arch.pkvm.handle);
-    else
-        kvm_call_hyp(__kvm_tlb_flush_vmid, &kvm->arch.mmu);
-    return 0;
+    if (!kvm->arch.vm_s2_ops->vm_flush_remote_tlbs)
+        return 1;

For the return value, I'm wandering if 0 should be returned. More details can be found below.

Quoted 27 lines
arch/arm64/kvm/mmu.c +15 -6
+    return kvm->arch.vm_s2_ops->vm_flush_remote_tlbs(kvm);
   }
-int kvm_arch_flush_remote_tlbs_range(struct kvm *kvm,
-                      gfn_t gfn, u64 nr_pages)
+static int pkvm_flush_remote_tlbs_range(struct kvm *kvm,
+                    gfn_t gfn, u64 nr_pages)
+{
+    return pkvm_flush_remote_tlbs(kvm);
+}
+
+static int kvm_vm_flush_remote_tlbs_range(struct kvm *kvm,
+                     gfn_t gfn, u64 nr_pages)
   {
       u64 size = nr_pages << PAGE_SHIFT;
       u64 addr = gfn << PAGE_SHIFT;
-    if (is_protected_kvm_enabled())
-        kvm_call_hyp_nvhe(__pkvm_tlb_flush_vmid, kvm->arch.pkvm.handle);
-    else
-        kvm_tlb_flush_vmid_range(&kvm->arch.mmu, addr, size);
+    kvm_tlb_flush_vmid_range(&kvm->arch.mmu, addr, size);
       return 0;
   }
+int kvm_arch_flush_remote_tlbs_range(struct kvm *kvm,
+                     gfn_t gfn, u64 nr_pages)
+{
+    if (!kvm->arch.vm_s2_ops->vm_flush_remote_tlbs_range)
+        return 1;
Quoted 9 lines

arch/arm64/kvm/mmu.c static bool memslot_is_logging(struct kvm_memory_slot *memslot) · line 214

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

Realm would the only case where vm_s2_ops->vm_flush_remote_{tlbs, tlbs_range) are NULL. On request to flush remote TLBs by kvm_flush_remote_tlbs_range(), it ends up with event KVM_REQ_TLB_FLUSH queued for each vCPU. How this queued event is linked to a remote TLB flush for realm? The problem is TLBs are owned by EL2 realm and there are no RMI calls for the management. So I'm wandering we should return 0 here?

vm_s2_ops->vm_flush_remote_{tlbs, tlbs_range} are added in PATCH[14] where 0 is returned for both function. So I guess needn't this check at all?

	 if (!kvm->arch.vm_s2_ops->vm_flush_remote_tlbs_range)
Quoted 130 lines
Quoted 110 lines
arch/arm64/kvm/mmu.c +58 -10
+    return kvm->arch.vm_s2_ops->vm_flush_remote_tlbs_range(kvm, gfn, nr_pages);
+}
+
   static void *stage2_memcache_zalloc_page(void *arg)
   {
       struct kvm_mmu_memory_cache *mc = arg;
@@ -337,13 +361,20 @@ static void __unmap_stage2_range(struct kvm_s2_mmu *mmu, phys_addr_t start, u64
                      may_block));
   }
+static void kvm_vm_stage2_unmap_range(struct kvm_s2_mmu *mmu,
+                      phys_addr_t start,
+                      u64 size, bool may_block)
+{
+    __unmap_stage2_range(mmu, start, size, may_block);
+}
+
   void kvm_stage2_unmap_range(struct kvm_s2_mmu *mmu, phys_addr_t start,
                   u64 size, bool may_block)
   {
-    if (kvm_vm_is_protected(kvm_s2_mmu_to_kvm(mmu)))
-        return;
+    struct kvm *kvm = kvm_s2_mmu_to_kvm(mmu);
-    __unmap_stage2_range(mmu, start, size, may_block);
+    if (kvm->arch.vm_s2_ops->vm_stage2_unmap_range)
+        kvm->arch.vm_s2_ops->vm_stage2_unmap_range(mmu, start, size, may_block);
   }
   void kvm_stage2_flush_range(struct kvm_s2_mmu *mmu, phys_addr_t addr, phys_addr_t end)
@@ -983,6 +1014,12 @@ int kvm_init_stage2_mmu(struct kvm *kvm, struct kvm_s2_mmu *mmu, unsigned long t
       int cpu, err;
       struct kvm_pgtable *pgt;
+    /* Initialize the VM ops for the VM instance for the first time */
+    if (mmu == &kvm->arch.mmu) {
+        err = kvm_vm_init_vm_s2_ops(kvm);
+        if (err)
+            return err;
+    }
       /*
        * If we already have our page tables in place, and that the
        * MMU context is the canonical one, we have a bug somewhere,
@@ -2447,34 +2484,46 @@ bool kvm_unmap_gfn_range(struct kvm *kvm, struct kvm_gfn_range *range)
       return false;
   }
-bool kvm_age_gfn(struct kvm *kvm, struct kvm_gfn_range *range)
+static bool kvm_vm_age_gfn(struct kvm *kvm, struct kvm_gfn_range *range)
   {
       u64 size = (range->end - range->start) << PAGE_SHIFT;
-    if (!kvm->arch.mmu.pgt || kvm_vm_is_protected(kvm))
-        return false;
-
       return KVM_PGT_FN(kvm_pgtable_stage2_test_clear_young)(kvm->arch.mmu.pgt,
                              range->start << PAGE_SHIFT,
                              size, true);
+}
+
+bool kvm_age_gfn(struct kvm *kvm, struct kvm_gfn_range *range)
+{
+    if (!kvm->arch.mmu.pgt || !kvm->arch.vm_s2_ops->vm_age_gfn)
+        return false;
+
+    return kvm->arch.vm_s2_ops->vm_age_gfn(kvm, range);
       /*
        * TODO: Handle nested_mmu structures here using the reverse mapping in
        * a later version of patch series.
        */
   }
-bool kvm_test_age_gfn(struct kvm *kvm, struct kvm_gfn_range *range)
+static bool kvm_vm_test_age_gfn(struct kvm *kvm, struct kvm_gfn_range *range)
   {
       u64 size = (range->end - range->start) << PAGE_SHIFT;
-    if (!kvm->arch.mmu.pgt || kvm_vm_is_protected(kvm))
-        return false;
       return KVM_PGT_FN(kvm_pgtable_stage2_test_clear_young)(kvm->arch.mmu.pgt,
                              range->start << PAGE_SHIFT,
                              size, false);
   }
+bool kvm_test_age_gfn(struct kvm *kvm, struct kvm_gfn_range *range)
+{
+
+    if (!kvm->arch.mmu.pgt || !kvm->arch.vm_s2_ops->vm_test_age_gfn)
+        return false;
+
+    return kvm->arch.vm_s2_ops->vm_test_age_gfn(kvm, range);
+}
+
   phys_addr_t kvm_mmu_get_httbr(void)
   {
       return __pa(hyp_pgtable->pgd);
@@ -2796,3 +2845,49 @@ void kvm_toggle_cache(struct kvm_vcpu *vcpu, bool was_enabled)
       trace_kvm_toggle_cache(*vcpu_pc(vcpu), was_enabled, now_enabled);
   }
+
+static const struct kvm_vm_s2_ops protected_pkvm_vm_s2_ops = {
+    .vm_flush_remote_tlbs        = pkvm_flush_remote_tlbs,
+    .vm_flush_remote_tlbs_range    = pkvm_flush_remote_tlbs_range,
+    /*
+     * Not supported for Protected VMs under pKVM
+     * .vm_age_gfn
+     * .vm_test_age_gfn
+     * .vm_stage2_unmap_range
+     */
+};
+
+static const struct kvm_vm_s2_ops pkvm_vm_s2_ops = {
+    .vm_flush_remote_tlbs        = pkvm_flush_remote_tlbs,
+    .vm_flush_remote_tlbs_range    = pkvm_flush_remote_tlbs_range,
+    .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,
+};
+

arch/arm64/kvm/mmu.c void kvm_toggle_cache(struct kvm_vcpu *vcpu, bool was_enabled) · line 2876

Quoted 10 lines
arch/arm64/kvm/mmu.c +10 -0
+static const struct kvm_vm_s2_ops kvm_default_vm_s2_ops = {
+    .vm_flush_remote_tlbs        = kvm_vm_flush_remote_tlbs,
+    .vm_flush_remote_tlbs_range    = kvm_vm_flush_remote_tlbs_range,
+    .vm_age_gfn            = kvm_vm_age_gfn,
+    .vm_test_age_gfn        = kvm_vm_test_age_gfn,
+    .vm_stage2_unmap_range        = kvm_vm_stage2_unmap_range,
+};
+
+#define KVM_VM_S2_OPS(flavor, ops)        \
+        [flavor] = ops

Parentheses are needed, to be consistent with KVM_VCPU_OPS at least.

#define KVM_VM_S2_OPS(flavor, ops)        \
        [(flavor)] = (ops)

Actually, KVM_{VCPU, VM_S2}_OPS() can be combined to one in kvm_host.h as below.

#define KVM_FLAVOR_OPS() [(flavor)] = (ops)

Quoted 16 lines
Quoted 6 lines
arch/arm64/kvm/mmu.c +6 -0
+static const struct kvm_vm_s2_ops *arm64_vm_s2_ops[] = {
+    KVM_VM_S2_OPS(VM_VHE, &kvm_default_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),
+};

arch/arm64/kvm/mmu.c void kvm_toggle_cache(struct kvm_vcpu *vcpu, bool was_enabled) · line 2893

Quoted 10 lines
arch/arm64/kvm/mmu.c +10 -0
+
+static int kvm_vm_init_vm_s2_ops(struct kvm *kvm)
+{
+    BUILD_BUG_ON(ARRAY_SIZE(arm64_vm_s2_ops) != VM_FLAVOR_MAX);
+
+    kvm->arch.vm_s2_ops = arm64_vm_s2_ops[kvm->arch.vm_flavor];
+    if (WARN_ON(!kvm->arch.vm_s2_ops))
+        return -EINVAL;
+    return 0;
+}

Thanks,
Gavin

Reviewed-by on v19 14/20
#61 lore raw

Re: [PATCH v19 14/20] KVM: arm64: CCA: Add bare minimal S2 operations for Realm

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

Quoted 9 lines

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(+)
Reviewed-by on v19 15/20
#64 lore raw

Re: [PATCH v19 15/20] KVM: arm64: CCA: Introduce Realms

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

From: Steven Price <[email protected]>

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(-)
Quoted 3 lines

create mode 100644 arch/arm64/include/asm/kvm_rmi.h create mode 100644 arch/arm64/kvm/rmi.c

Reviewed-by on v19 17/20
#69 lore raw

Re: [PATCH v19 17/20] KVM: arm64: CCA: WARN on injected undef exceptions

In reply to Suzuki K Poulose
Quoted 6 lines · 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.

Quoted 9 lines

---
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(+)
Reviewed-by on v19 19/20
#72 lore raw

Re: [PATCH v19 19/20] KVM: arm64: CCA: Expose SVE VL register before VCPU finalization

In reply to Suzuki K Poulose
Quoted 21 lines · 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.

Quoted 8 lines

---
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(-)
Reviewed-by on v19 20/20
#74 lore raw

Re: [PATCH v19 20/20] KVM: arm64: CCA: Control user register access for Realms

In reply to Suzuki K Poulose
Quoted 47 lines · 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 :w
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 parameters. We don't support PMUs for the Realm VMs yet, so PMCR is not exposed.

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 v18:
- Don't expose PMCR_EL0 to the userspace now, we could do that when we support PMU
- Fix check patch warnings
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.

Quoted 6 lines
---
  arch/arm64/kvm/guest.c      | 63 +++++++++++++++++++++++++++++++++++++
  arch/arm64/kvm/hypercalls.c |  4 +--
  arch/arm64/kvm/sys_regs.c   | 28 +++++++++++++----
  3 files changed, 87 insertions(+), 8 deletions(-)
author reviewer
#48 lore raw

Re: [PATCH v19 09/20] KVM: arm64: Add VM specific callback for S2 MMU operations

In reply to Gavin Shan
Quoted 27 lines · Gavin Shan
Quoted 9 lines · Suzuki K Poulose

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

---
  arch/arm64/include/asm/kvm_host.h |  15 ++++ arch/arm64/kvm/mmu.c              | 137 +++++++++++++++++++++++++-----

  2 files changed, 131 insertions(+), 21 deletions(-)

Apart from the comments from Jonathan, some nitpicks and questions below.

Quoted 14 lines
arch/arm64/include/asm/kvm_host.h +6 -0
index 149f4582c8b6a..7664d8b8cce5a 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);

...

Quoted 26 lines
Quoted 26 lines
arch/arm64/include/asm/kvm_host.h +12 -2
@@ -166,6 +168,18 @@ static bool memslot_is_logging(struct 
 kvm_memory_slot *memslot)
       return memslot->dirty_bitmap && !(memslot->flags & KVM_MEM_READONLY);
   }
+static int pkvm_flush_remote_tlbs(struct kvm *kvm)
+{
+    kvm_call_hyp_nvhe(__pkvm_tlb_flush_vmid, kvm->arch.pkvm.handle);
+    return 0;
+}
+
+static int kvm_vm_flush_remote_tlbs(struct kvm *kvm)
+{
+    kvm_call_hyp(__kvm_tlb_flush_vmid, &kvm->arch.mmu);
+    return 0;
+}
+
   /**
    * kvm_arch_flush_remote_tlbs() - flush all VM TLB entries for v7/8
    * @kvm:    pointer to kvm structure.
@@ -174,26 +188,36 @@ static bool memslot_is_logging(struct 
 kvm_memory_slot *memslot)
    */
   int kvm_arch_flush_remote_tlbs(struct kvm *kvm)
   {
-    if (is_protected_kvm_enabled())
-        kvm_call_hyp_nvhe(__pkvm_tlb_flush_vmid, kvm->arch.pkvm.handle);
Quoted 8 lines

arch/arm64/include/asm/kvm_host.h static bool memslot_is_logging(struct · line 193

Quoted 5 lines
arch/arm64/include/asm/kvm_host.h +2 -3
-    else
-        kvm_call_hyp(__kvm_tlb_flush_vmid, &kvm->arch.mmu);
-    return 0;
+    if (!kvm->arch.vm_s2_ops->vm_flush_remote_tlbs)
+        return 1;

For the return value, I'm wandering if 0 should be returned. More details can be found below.

Answered below.

Quoted 1 line

...

Quoted 16 lines
Quoted 16 lines

+static int kvm_vm_flush_remote_tlbs_range(struct kvm *kvm,
+                     gfn_t gfn, u64 nr_pages)
  {
      u64 size = nr_pages << PAGE_SHIFT;
      u64 addr = gfn << PAGE_SHIFT;
-    if (is_protected_kvm_enabled())
-        kvm_call_hyp_nvhe(__pkvm_tlb_flush_vmid, kvm->arch.pkvm.handle); -    else
-        kvm_tlb_flush_vmid_range(&kvm->arch.mmu, addr, size); +    kvm_tlb_flush_vmid_range(&kvm->arch.mmu, addr, size); return 0;
  }

arch/arm64/include/asm/kvm_host.h +4 -0
+int kvm_arch_flush_remote_tlbs_range(struct kvm *kvm,
+                     gfn_t gfn, u64 nr_pages)
+{
+    if (!kvm->arch.vm_s2_ops->vm_flush_remote_tlbs_range)
Quoted 8 lines
Quoted 2 lines

+        return 1;
+

Realm would the only case where vm_s2_ops->vm_flush_remote_{tlbs, tlbs_range) are NULL. On request to flush remote TLBs by kvm_flush_remote_tlbs_range(), it ends up with event KVM_REQ_TLB_FLUSH queued for each vCPU. How this queued event
is linked to a remote TLB flush for realm? The problem is TLBs are owned by EL2
realm and there are no RMI calls for the management. So I'm wandering we should
return 0 here?

Please note that, the Realm s2 callbacks are not NULL for flush_remote_tlb*. They all return 0, indicating that everything is taken care of. (See Patch 14/20: KVM: arm64: CCA: Add bare minimal S2 operations for Realm)
...

Quoted 9 lines

arch/arm64/include/asm/kvm_host.h

Quoted 3 lines
arch/arm64/include/asm/kvm_host.h +3 -0
+
+#define KVM_VM_S2_OPS(flavor, ops)        \
+        [flavor] = ops

Parentheses are needed, to be consistent with KVM_VCPU_OPS at least.

#define KVM_VM_S2_OPS(flavor, ops)        \
        [(flavor)] = (ops)

Ack

Quoted 3 lines

Actually, KVM_{VCPU, VM_S2}_OPS() can be combined to one in kvm_host.h as below.

#define KVM_FLAVOR_OPS() [(flavor)] = (ops)

I would leave it as they are to avoid confusion.

Cheers
Suzuki

author reviewer
#47 lore raw

Re: [PATCH v19 09/20] KVM: arm64: Add VM specific callback for S2 MMU operations

In reply to Gavin Shan
Quoted 10 lines · Gavin Shan
Quoted 9 lines · Gavin Shan
Quoted 8 lines · Suzuki K Poulose

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

---
  arch/arm64/include/asm/kvm_host.h |  15 ++++ arch/arm64/kvm/mmu.c              | 137 +++++++++++++++++++++++++-----

  2 files changed, 131 insertions(+), 21 deletions(-)

...

Quoted 15 lines
Quoted 5 lines
Quoted 5 lines
arch/arm64/kvm/mmu.c +5 -0
+int kvm_arch_flush_remote_tlbs_range(struct kvm *kvm,
+                     gfn_t gfn, u64 nr_pages)
+{
+    if (!kvm->arch.vm_s2_ops->vm_flush_remote_tlbs_range)
+        return 1;
Quoted 9 lines
Quoted 1 line

+

Realm would the only case where vm_s2_ops->vm_flush_remote_{tlbs, tlbs_range) are NULL. On request to flush remote TLBs by kvm_flush_remote_tlbs_range(), it ends up with event KVM_REQ_TLB_FLUSH queued for each vCPU. How this queued event is linked to a remote TLB flush for realm? The problem is TLBs are owned by EL2 realm and there are no RMI calls for the management. So I'm wandering we should return 0 here?

Quoted 4 lines

vm_s2_ops->vm_flush_remote_{tlbs, tlbs_range} are added in PATCH[14] where 0 is returned for both function. So I guess needn't this check at all?

     if (!kvm->arch.vm_s2_ops->vm_flush_remote_tlbs_range)

I would leave that check as they are. The only callback that is mandatory is the vm_mem_abort and we do enforce it at init.

Cheers
Suzuki