Skip to contentSkip to conversation

KVM: arm64: Fix handling of host fpsimd/sve state in protected mode

PATCH Superseded Fuad Tabba 4 participants 23 messages May 17, 2024 – May 21, 2024

Subsystems (2)

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Fuad Tabba

author
#0 lore raw

cover KVM: arm64: Fix handling of host fpsimd/sve state in protected mode

With the KVM host data rework [1], handling of fpsimd and sve state in protected mode is done at hyp. For protected VMs, we don't want to leak any guest state to the host, including whether a guest has used fpsimd/sve.

To complete the work started with the host data rework, in regards to protected mode, ensure that the host's fpsimd context and its sve context are restored on guest exit, since the rework has hidden the fpsimd/sve state from the host.

This patch series eagerly restores the host fpsimd/sve state on guest exit when running in protected mode, which happens only if the guest has used fpsimd/sve. This means that the saving of the state is lazy, similar to the behavior of KVM in other modes, but the restoration of the host state is eager.

This series is based on kvmarm-6.10-1 (kvmarm/next). It should not have any effect on modes other than protected mode.

Tested on qemu, with the kernel sve stress tests.

Cheers,
/fuad

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

Fuad Tabba (7):
KVM: arm64: Reintroduce __sve_save_state
KVM: arm64: Specialize deactivate fpsimd/sve traps on guest trap
KVM: arm64: Specialize handling of host fpsimd state on trap
KVM: arm64: Store the maximum sve vector length at hyp
KVM: arm64: Allocate memory at hyp for host sve state in pKVM
KVM: arm64: Eagerly restore host fpsimd/sve state in pKVM
KVM: arm64: Consolidate initializing the host data's fpsimd_state/sve
in pKVM

 arch/arm64/include/asm/kvm_host.h       | 11 +++-
 arch/arm64/include/asm/kvm_hyp.h        |  2 +
 arch/arm64/include/asm/kvm_pkvm.h       |  9 +++
 arch/arm64/include/uapi/asm/ptrace.h    | 14 +++++
 arch/arm64/kvm/arm.c                    | 75 +++++++++++++++++++++++++
 arch/arm64/kvm/hyp/fpsimd.S             |  6 ++
 arch/arm64/kvm/hyp/include/hyp/switch.h | 31 +++++-----
 arch/arm64/kvm/hyp/include/nvhe/pkvm.h  |  1 -
 arch/arm64/kvm/hyp/nvhe/hyp-main.c      | 57 ++++++++++++++++++-
 arch/arm64/kvm/hyp/nvhe/pkvm.c          | 15 ++---
 arch/arm64/kvm/hyp/nvhe/setup.c         | 25 ++++++++-
 arch/arm64/kvm/hyp/nvhe/switch.c        | 40 +++++++++++++
 arch/arm64/kvm/hyp/vhe/switch.c         | 16 ++++++
 arch/arm64/kvm/reset.c                  |  2 +
 14 files changed, 271 insertions(+), 33 deletions(-)

base-commit: eaa46a28d59655aa89a8fb885affa6fc0de44376

Signature · 1 line
2.45.0.rc1.225.g2a3ae87e7f-goog
author
#1 lore raw

PATCH 1/7 KVM: arm64: Reintroduce __sve_save_state

Now that the hypervisor is handling the host sve state in protected mode, it needs to be able to save it.

This reverts commit e66425fc9ba3 ("KVM: arm64: Remove unused __sve_save_state").

---
 arch/arm64/include/asm/kvm_hyp.h | 1 +
 arch/arm64/kvm/hyp/fpsimd.S      | 6 ++++++
 2 files changed, 7 insertions(+)
arch/arm64/include/asm/kvm_hyp.h +1 -0
index 3e80464f8953..2ab23589339a 100644
@@ -111,6 +111,7 @@ void __debug_restore_host_buffers_nvhe(struct kvm_vcpu *vcpu);
 
 void __fpsimd_save_state(struct user_fpsimd_state *fp_regs);
 void __fpsimd_restore_state(struct user_fpsimd_state *fp_regs);
+void __sve_save_state(void *sve_pffr, u32 *fpsr);
 void __sve_restore_state(void *sve_pffr, u32 *fpsr);
 
 u64 __guest_enter(struct kvm_vcpu *vcpu);
arch/arm64/kvm/hyp/fpsimd.S +6 -0
index 61e6f3ba7b7d..e950875e31ce 100644
@@ -25,3 +25,9 @@ SYM_FUNC_START(__sve_restore_state)
 	sve_load 0, x1, x2, 3
 	ret
 SYM_FUNC_END(__sve_restore_state)
+
+SYM_FUNC_START(__sve_save_state)
+	mov	x2, #1
+	sve_save 0, x1, x2, 3
+	ret
+SYM_FUNC_END(__sve_save_state)
Signature · 1 line
2.45.0.rc1.225.g2a3ae87e7f-goog
author
#2 lore raw

PATCH 2/7 KVM: arm64: Specialize deactivate fpsimd/sve traps on guest trap

In reply to Fuad Tabba

The code for deactivating traps, to be able to update the fpsimd/sve registers, is the only code in switch.h that is n/vhe specific, i.e., behaves differently whether it's running in vhe/nvhe. Move it to specialized functions in switch.c like other mode-specific code.

This is needed for subsequent patches, since the logic for deciding which traps to enable/disable will diverge between n/vhe.

No functional change intended.

---
 arch/arm64/kvm/hyp/include/hyp/switch.h | 18 +++---------------
 arch/arm64/kvm/hyp/nvhe/switch.c        | 21 +++++++++++++++++++++
 arch/arm64/kvm/hyp/vhe/switch.c         | 11 +++++++++++
 3 files changed, 35 insertions(+), 15 deletions(-)
arch/arm64/kvm/hyp/include/hyp/switch.h +3 -15
index a92566f36022..890388c17c3e 100644
@@ -320,6 +320,8 @@ static inline void __hyp_sve_restore_guest(struct kvm_vcpu *vcpu)
 	write_sysreg_el1(__vcpu_sys_reg(vcpu, ZCR_EL1), SYS_ZCR);
 }
 
+static void __deactivate_fpsimd_sve_traps(struct kvm_vcpu *vcpu);
+
 /*
  * We trap the first access to the FP/SIMD to save the host context and
  * restore the guest context lazily.
@@ -330,7 +332,6 @@ static bool kvm_hyp_handle_fpsimd(struct kvm_vcpu *vcpu, u64 *exit_code)
 {
 	bool sve_guest;
 	u8 esr_ec;
-	u64 reg;
 
 	if (!system_supports_fpsimd())
 		return false;
@@ -353,20 +354,7 @@ static bool kvm_hyp_handle_fpsimd(struct kvm_vcpu *vcpu, u64 *exit_code)
 	/* Valid trap.  Switch the context: */
 
 	/* First disable enough traps to allow us to update the registers */
-	if (has_vhe() || has_hvhe()) {
-		reg = CPACR_EL1_FPEN_EL0EN | CPACR_EL1_FPEN_EL1EN;
-		if (sve_guest)
-			reg |= CPACR_EL1_ZEN_EL0EN | CPACR_EL1_ZEN_EL1EN;
-
-		sysreg_clear_set(cpacr_el1, 0, reg);
-	} else {
-		reg = CPTR_EL2_TFP;
-		if (sve_guest)
-			reg |= CPTR_EL2_TZ;
-
-		sysreg_clear_set(cptr_el2, reg, 0);
-	}
-	isb();
+	__deactivate_fpsimd_sve_traps(vcpu);
 
 	/* Write out the host state if it's in the registers */
 	if (host_owns_fp_regs())
arch/arm64/kvm/hyp/nvhe/switch.c +21 -0
index 6758cd905570..2b5af10cdf1d 100644
@@ -112,6 +112,27 @@ static void __deactivate_traps(struct kvm_vcpu *vcpu)
 	write_sysreg(__kvm_hyp_host_vector, vbar_el2);
 }
 
+static void __deactivate_fpsimd_sve_traps(struct kvm_vcpu *vcpu)
+{
+	bool clear_sve_traps = vcpu_has_sve(vcpu);
+	u64 reg;
+
+	if (has_hvhe()) {
+		reg = CPACR_EL1_FPEN_EL0EN | CPACR_EL1_FPEN_EL1EN;
+		if (clear_sve_traps)
+			reg |= CPACR_EL1_ZEN_EL0EN | CPACR_EL1_ZEN_EL1EN;
+
+		sysreg_clear_set(cpacr_el1, 0, reg);
+	} else {
+		reg = CPTR_EL2_TFP;
+		if (clear_sve_traps)
+			reg |= CPTR_EL2_TZ;
+
+		sysreg_clear_set(cptr_el2, reg, 0);
+	}
+	isb();
+}
+
 /* Save VGICv3 state on non-VHE systems */
 static void __hyp_vgic_save_state(struct kvm_vcpu *vcpu)
 {
arch/arm64/kvm/hyp/vhe/switch.c +11 -0
index d7af5f46f22a..740360065d7d 100644
@@ -209,6 +209,17 @@ void kvm_vcpu_put_vhe(struct kvm_vcpu *vcpu)
 	host_data_ptr(host_ctxt)->__hyp_running_vcpu = NULL;
 }
 
+static void __deactivate_fpsimd_sve_traps(struct kvm_vcpu *vcpu)
+{
+	u64 reg = CPACR_EL1_FPEN_EL0EN | CPACR_EL1_FPEN_EL1EN;
+
+	if (vcpu_has_sve(vcpu))
+		reg |= CPACR_EL1_ZEN_EL0EN | CPACR_EL1_ZEN_EL1EN;
+
+	sysreg_clear_set(cpacr_el1, 0, reg);
+	isb();
+}
+
 static bool kvm_hyp_handle_eret(struct kvm_vcpu *vcpu, u64 *exit_code)
 {
 	u64 esr = kvm_vcpu_get_esr(vcpu);
Signature · 1 line
2.45.0.rc1.225.g2a3ae87e7f-goog
author
#3 lore raw

PATCH 3/7 KVM: arm64: Specialize handling of host fpsimd state on trap

In reply to Fuad Tabba

In subsequent patches, n/vhe will diverge on saving the host fpsimd/sve state when taking a guest fpsimd/sve trap. Add a specialized helper to handle it.

No functional change intended.

---
 arch/arm64/kvm/hyp/include/hyp/switch.h | 3 ++-
 arch/arm64/kvm/hyp/nvhe/switch.c        | 5 +++++
 arch/arm64/kvm/hyp/vhe/switch.c         | 5 +++++
 3 files changed, 12 insertions(+), 1 deletion(-)
arch/arm64/kvm/hyp/include/hyp/switch.h +2 -1
index 890388c17c3e..d15272445db2 100644
@@ -321,6 +321,7 @@ static inline void __hyp_sve_restore_guest(struct kvm_vcpu *vcpu)
 }
 
 static void __deactivate_fpsimd_sve_traps(struct kvm_vcpu *vcpu);
+static void kvm_hyp_save_fpsimd_host(struct kvm_vcpu *vcpu);
 
 /*
  * We trap the first access to the FP/SIMD to save the host context and
@@ -358,7 +359,7 @@ static bool kvm_hyp_handle_fpsimd(struct kvm_vcpu *vcpu, u64 *exit_code)
 
 	/* Write out the host state if it's in the registers */
 	if (host_owns_fp_regs())
-		__fpsimd_save_state(*host_data_ptr(fpsimd_state));
+		kvm_hyp_save_fpsimd_host(vcpu);
 
 	/* Restore the guest state */
 	if (sve_guest)
arch/arm64/kvm/hyp/nvhe/switch.c +5 -0
index 2b5af10cdf1d..935f3db245e9 100644
@@ -203,6 +203,11 @@ static bool kvm_handle_pvm_sys64(struct kvm_vcpu *vcpu, u64 *exit_code)
 		kvm_handle_pvm_sysreg(vcpu, exit_code));
 }
 
+static void kvm_hyp_save_fpsimd_host(struct kvm_vcpu *vcpu)
+{
+	__fpsimd_save_state(*host_data_ptr(fpsimd_state));
+}
+
 static const exit_handler_fn hyp_exit_handlers[] = {
 	[0 ... ESR_ELx_EC_MAX]		= NULL,
 	[ESR_ELx_EC_CP15_32]		= kvm_hyp_handle_cp15_32,
arch/arm64/kvm/hyp/vhe/switch.c +5 -0
index 740360065d7d..6a6d69c3c58a 100644
@@ -273,6 +273,11 @@ static bool kvm_hyp_handle_eret(struct kvm_vcpu *vcpu, u64 *exit_code)
 	return true;
 }
 
+static void kvm_hyp_save_fpsimd_host(struct kvm_vcpu *vcpu)
+{
+	__fpsimd_save_state(*host_data_ptr(fpsimd_state));
+}
+
 static const exit_handler_fn hyp_exit_handlers[] = {
 	[0 ... ESR_ELx_EC_MAX]		= NULL,
 	[ESR_ELx_EC_CP15_32]		= kvm_hyp_handle_cp15_32,
Signature · 1 line
2.45.0.rc1.225.g2a3ae87e7f-goog
author
#4 lore raw

PATCH 4/7 KVM: arm64: Store the maximum sve vector length at hyp

In reply to Fuad Tabba

In subsequent patches, hyp needs to know the maximum sve vector length for the host, without needing the trust the host for that. This is used when allocating memory for the host sve state in the following patch, as well as for allocating and restricting guest sve state in a future patch series.

---
 arch/arm64/include/asm/kvm_host.h | 1 +
 arch/arm64/include/asm/kvm_hyp.h  | 1 +
 arch/arm64/kvm/arm.c              | 1 +
 arch/arm64/kvm/hyp/nvhe/pkvm.c    | 2 ++
 arch/arm64/kvm/reset.c            | 2 ++
 5 files changed, 7 insertions(+)
arch/arm64/include/asm/kvm_host.h +1 -0
index 8170c04fde91..0a5fceb20f3a 100644
@@ -76,6 +76,7 @@ static inline enum kvm_mode kvm_get_mode(void) { return KVM_MODE_NONE; };
 DECLARE_STATIC_KEY_FALSE(userspace_irqchip_in_use);
 
 extern unsigned int __ro_after_init kvm_sve_max_vl;
+extern unsigned int __ro_after_init kvm_host_sve_max_vl;
 int __init kvm_arm_init_sve(void);
 
 u32 __attribute_const__ kvm_target_cpu(void);
arch/arm64/include/asm/kvm_hyp.h +1 -0
index 2ab23589339a..d313adf53bef 100644
@@ -143,5 +143,6 @@ extern u64 kvm_nvhe_sym(id_aa64smfr0_el1_sys_val);
 
 extern unsigned long kvm_nvhe_sym(__icache_flags);
 extern unsigned int kvm_nvhe_sym(kvm_arm_vmid_bits);
+extern unsigned int kvm_nvhe_sym(kvm_host_sve_max_vl);
 
 #endif /* __ARM64_KVM_HYP_H__ */
arch/arm64/kvm/arm.c +1 -0
index 9996a989b52e..9e565ea3d645 100644
@@ -2378,6 +2378,7 @@ static void kvm_hyp_init_symbols(void)
 	kvm_nvhe_sym(id_aa64smfr0_el1_sys_val) = read_sanitised_ftr_reg(SYS_ID_AA64SMFR0_EL1);
 	kvm_nvhe_sym(__icache_flags) = __icache_flags;
 	kvm_nvhe_sym(kvm_arm_vmid_bits) = kvm_arm_vmid_bits;
+	kvm_nvhe_sym(kvm_host_sve_max_vl) = kvm_host_sve_max_vl;
 }
 
 static int __init kvm_hyp_init_protection(u32 hyp_va_bits)
arch/arm64/kvm/hyp/nvhe/pkvm.c +2 -0
index 16aa4875ddb8..25e9a94f6d76 100644
@@ -18,6 +18,8 @@ unsigned long __icache_flags;
 /* Used by kvm_get_vttbr(). */
 unsigned int kvm_arm_vmid_bits;
 
+unsigned int kvm_host_sve_max_vl;
+
 /*
  * Set trap register values based on features in ID_AA64PFR0.
  */
arch/arm64/kvm/reset.c +2 -0
index 1b7b58cb121f..e818727900ec 100644
@@ -46,11 +46,13 @@ static u32 __ro_after_init kvm_ipa_limit;
 				 PSR_AA32_I_BIT | PSR_AA32_F_BIT)
 
 unsigned int __ro_after_init kvm_sve_max_vl;
+unsigned int __ro_after_init kvm_host_sve_max_vl;
 
 int __init kvm_arm_init_sve(void)
 {
 	if (system_supports_sve()) {
 		kvm_sve_max_vl = sve_max_virtualisable_vl();
+		kvm_host_sve_max_vl = sve_max_vl();
 
 		/*
 		 * The get_sve_reg()/set_sve_reg() ioctl interface will need
Signature · 1 line
2.45.0.rc1.225.g2a3ae87e7f-goog
author
#5 lore raw

PATCH 5/7 KVM: arm64: Allocate memory at hyp for host sve state in pKVM

In reply to Fuad Tabba

Protected mode needs to maintain (save/restore) the host's sve state, rather than relying on the host kernel to do that. This is to avoid leaking information to the host about guests and the type of operations they are performing.

As a first step towards that, allocate memory at hyp, per cpu, to hold the host sve data. The following patch will use this memory to save/restore the host state.

---
Note that the last patch in this series will consolidate the setup of the host's fpsimd and sve states, which currently take place in two different locations. Moreover, that last patch will also place the host fpsimd and sve_state pointers in a union.

---
 arch/arm64/include/asm/kvm_host.h    |  2 +
 arch/arm64/include/asm/kvm_pkvm.h    |  9 ++++
 arch/arm64/include/uapi/asm/ptrace.h | 14 ++++++
 arch/arm64/kvm/arm.c                 | 68 ++++++++++++++++++++++++++++
 arch/arm64/kvm/hyp/nvhe/setup.c      | 24 ++++++++++
 5 files changed, 117 insertions(+)
arch/arm64/include/asm/kvm_host.h +2 -0
index 0a5fceb20f3a..7b3745ef1d73 100644
@@ -535,7 +535,9 @@ struct kvm_cpu_context {
  */
 struct kvm_host_data {
 	struct kvm_cpu_context host_ctxt;
+
 	struct user_fpsimd_state *fpsimd_state;	/* hyp VA */
+	struct user_sve_state *sve_state;	/* hyp VA */
 
 	/* Ownership of the FP regs */
 	enum {
arch/arm64/include/asm/kvm_pkvm.h +9 -0
index ad9cfb5c1ff4..b9d12e123efb 100644
@@ -128,4 +128,13 @@ static inline unsigned long hyp_ffa_proxy_pages(void)
 	return (2 * KVM_FFA_MBOX_NR_PAGES) + DIV_ROUND_UP(desc_max, PAGE_SIZE);
 }
 
+static inline size_t pkvm_host_sve_state_size(void)
+{
+	if (!system_supports_sve())
+		return 0;
+
+	return size_add(sizeof(struct user_sve_state),
+			SVE_SIG_REGS_SIZE(sve_vq_from_vl(kvm_host_sve_max_vl)));
+}
+
 #endif	/* __ARM64_KVM_PKVM_H__ */
arch/arm64/include/uapi/asm/ptrace.h +14 -0
index 7fa2f7036aa7..77aabf964071 100644
@@ -120,6 +120,20 @@ struct user_sve_header {
 	__u16 __reserved;
 };
 
+struct user_sve_state {
+	__u64 zcr_el1;
+
+	/*
+	 * Ordering is important since __sve_save_state/__sve_restore_state
+	 * relies on it.
+	 */
+	__u32 fpsr;
+	__u32 fpcr;
+
+	/* Must be SVE_VQ_BYTES (128 bit) aligned. */
+	__u8 sve_regs[];
+};
+
 /* Definitions for user_sve_header.flags: */
 #define SVE_PT_REGS_MASK		(1 << 0)
 
arch/arm64/kvm/arm.c +68 -0

99 lines not on this page · raw message

arch/arm64/kvm/hyp/nvhe/setup.c +24 -0
index 859f22f754d3..5c8cd806efb9 100644
@@ -67,6 +67,28 @@ static int divide_memory_pool(void *virt, unsigned long size)
 	return 0;
 }
 
+static int pkvm_create_host_sve_mappings(void)
+{
+	void *start, *end;
+	int ret, i;
+
+	if (!system_supports_sve())
+		return 0;
+
+	for (i = 0; i < hyp_nr_cpus; i++) {
+		struct kvm_host_data *host_data = per_cpu_ptr(&kvm_host_data, i);
+		struct user_sve_state *sve_state = host_data->sve_state;
+
+		start = kern_hyp_va(sve_state);
+		end = start + PAGE_ALIGN(pkvm_host_sve_state_size());
+		ret = pkvm_create_mappings(start, end, PAGE_HYP);
+		if (ret)
+			return ret;
+	}
+
+	return 0;
+}
+
 static int recreate_hyp_mappings(phys_addr_t phys, unsigned long size,
 				 unsigned long *per_cpu_base,
 				 u32 hyp_va_bits)
@@ -125,6 +147,8 @@ static int recreate_hyp_mappings(phys_addr_t phys, unsigned long size,
 			return ret;
 	}
 
+	pkvm_create_host_sve_mappings();
+
 	/*
 	 * Map the host sections RO in the hypervisor, but transfer the
 	 * ownership from the host to the hypervisor itself to make sure they
Signature · 1 line
2.45.0.rc1.225.g2a3ae87e7f-goog
author
#6 lore raw

PATCH 6/7 KVM: arm64: Eagerly restore host fpsimd/sve state in pKVM

In reply to Fuad Tabba

When running in protected mode we don't want to leak protected guest state to the host, including whether a guest has used fpsimd/sve. Therefore, eagerly restore the host state on guest exit when running in protected mode, which happens only if the guest has used fpsimd/sve.

As a future optimisation, we could go back to lazy restoring state at the host after exiting non-protected guests.

---
 arch/arm64/kvm/hyp/include/hyp/switch.h | 10 +++++
 arch/arm64/kvm/hyp/nvhe/hyp-main.c      | 57 +++++++++++++++++++++++--
 arch/arm64/kvm/hyp/nvhe/pkvm.c          |  2 +
 arch/arm64/kvm/hyp/nvhe/switch.c        | 18 +++++++-
 4 files changed, 82 insertions(+), 5 deletions(-)
arch/arm64/kvm/hyp/include/hyp/switch.h +10 -0
index d15272445db2..4dc620499e5d 100644
@@ -320,6 +320,16 @@ static inline void __hyp_sve_restore_guest(struct kvm_vcpu *vcpu)
 	write_sysreg_el1(__vcpu_sys_reg(vcpu, ZCR_EL1), SYS_ZCR);
 }
 
+static inline void __hyp_sve_save_host(void)
+{
+	struct user_sve_state *sve_state = *host_data_ptr(sve_state);
+
+	sve_state->zcr_el1 = read_sysreg_el1(SYS_ZCR);
+	sve_cond_update_zcr_vq(ZCR_ELx_LEN_MASK, SYS_ZCR_EL2);
+	__sve_save_state(sve_state->sve_regs + sve_ffr_offset(kvm_host_sve_max_vl),
+			 &sve_state->fpsr);
+}
+
 static void __deactivate_fpsimd_sve_traps(struct kvm_vcpu *vcpu);
 static void kvm_hyp_save_fpsimd_host(struct kvm_vcpu *vcpu);
 
arch/arm64/kvm/hyp/nvhe/hyp-main.c +54 -3

84 lines not on this page · raw message

arch/arm64/kvm/hyp/nvhe/pkvm.c +2 -0
index 25e9a94f6d76..feb27b4ce459 100644
@@ -588,6 +588,8 @@ int __pkvm_init_vcpu(pkvm_handle_t handle, struct kvm_vcpu *host_vcpu,
 	if (ret)
 		unmap_donated_memory(hyp_vcpu, sizeof(*hyp_vcpu));
 
+	hyp_vcpu->vcpu.arch.cptr_el2 = kvm_get_reset_cptr_el2(&hyp_vcpu->vcpu);
+
 	return ret;
 }
 
arch/arm64/kvm/hyp/nvhe/switch.c +16 -2
index 935f3db245e9..a2e1419f0f0f 100644
@@ -114,7 +114,8 @@ static void __deactivate_traps(struct kvm_vcpu *vcpu)
 
 static void __deactivate_fpsimd_sve_traps(struct kvm_vcpu *vcpu)
 {
-	bool clear_sve_traps = vcpu_has_sve(vcpu);
+	bool clear_sve_traps = vcpu_has_sve(vcpu) ||
+			       (is_protected_kvm_enabled() && system_supports_sve());
 	u64 reg;
 
 	if (has_hvhe()) {
@@ -205,7 +206,20 @@ static bool kvm_handle_pvm_sys64(struct kvm_vcpu *vcpu, u64 *exit_code)
 
 static void kvm_hyp_save_fpsimd_host(struct kvm_vcpu *vcpu)
 {
-	__fpsimd_save_state(*host_data_ptr(fpsimd_state));
+	/*
+	 * Non-protected kvm relies on the host restoring its sve state.
+	 * Protected kvm restores the host's sve state as not to reveal that
+	 * fpsimd was used by a guest nor leak upper sve bits.
+	 */
+	if (unlikely(is_protected_kvm_enabled() && system_supports_sve())) {
+		__hyp_sve_save_host();
+
+		/* Re-enable SVE traps for guests that do not support it. */
+		if (!vcpu_has_sve(vcpu))
+			sysreg_clear_set(cptr_el2, 0, CPTR_EL2_TZ);
+	} else {
+		__fpsimd_save_state(*host_data_ptr(fpsimd_state));
+	}
 }
 
 static const exit_handler_fn hyp_exit_handlers[] = {
Signature · 1 line
2.45.0.rc1.225.g2a3ae87e7f-goog
author
#12 lore raw

PATCH 7/7 KVM: arm64: Consolidate initializing the host data's fpsimd_state/sve in pKVM

In reply to Fuad Tabba

Now that we have introduced finalize_init_hyp_mode(), lets consolidate the initializing of the host_data fpsimd_state and sve state.

---
 arch/arm64/include/asm/kvm_host.h      | 10 ++++++++--
 arch/arm64/kvm/arm.c                   | 18 ++++++++++++------
 arch/arm64/kvm/hyp/include/nvhe/pkvm.h |  1 -
 arch/arm64/kvm/hyp/nvhe/pkvm.c         | 11 -----------
 arch/arm64/kvm/hyp/nvhe/setup.c        |  1 -
 5 files changed, 20 insertions(+), 21 deletions(-)
arch/arm64/include/asm/kvm_host.h +8 -2
index 7b3745ef1d73..8a170f314498 100644
@@ -536,8 +536,14 @@ struct kvm_cpu_context {
 struct kvm_host_data {
 	struct kvm_cpu_context host_ctxt;
 
-	struct user_fpsimd_state *fpsimd_state;	/* hyp VA */
-	struct user_sve_state *sve_state;	/* hyp VA */
+	/*
+	 * All pointers in this union are hyp VA.
+	 * sve_state is only used in pKVM and if system_supports_sve().
+	 */
+	union {
+		struct user_fpsimd_state *fpsimd_state;
+		struct user_sve_state *sve_state;
+	};
 
 	/* Ownership of the FP regs */
 	enum {
arch/arm64/kvm/arm.c +12 -6
index a9b1b0e9c319..a1c7e0ad6951 100644
@@ -2445,14 +2445,20 @@ static void finalize_init_hyp_mode(void)
 {
 	int cpu;
 
-	if (!is_protected_kvm_enabled() || !system_supports_sve())
-		return;
-
 	for_each_possible_cpu(cpu) {
-		struct user_sve_state *sve_state;
+		if (system_supports_sve() && is_protected_kvm_enabled()) {
+			struct user_sve_state *sve_state;
 
-		sve_state = per_cpu_ptr_nvhe_sym(kvm_host_data, cpu)->sve_state;
-		per_cpu_ptr_nvhe_sym(kvm_host_data, cpu)->sve_state = kern_hyp_va(sve_state);
+			sve_state = per_cpu_ptr_nvhe_sym(kvm_host_data, cpu)->sve_state;
+			per_cpu_ptr_nvhe_sym(kvm_host_data, cpu)->sve_state =
+				kern_hyp_va(sve_state);
+		} else {
+			struct user_fpsimd_state *fpsimd_state;
+
+			fpsimd_state = &per_cpu_ptr_nvhe_sym(kvm_host_data, cpu)->host_ctxt.fp_regs;
+			per_cpu_ptr_nvhe_sym(kvm_host_data, cpu)->fpsimd_state =
+				kern_hyp_va(fpsimd_state);
+		}
 	}
 }
 
arch/arm64/kvm/hyp/include/nvhe/pkvm.h +0 -1
index 22f374e9f532..24a9a8330d19 100644
@@ -59,7 +59,6 @@ static inline bool pkvm_hyp_vcpu_is_protected(struct pkvm_hyp_vcpu *hyp_vcpu)
 }
 
 void pkvm_hyp_vm_table_init(void *tbl);
-void pkvm_host_fpsimd_state_init(void);
 
 int __pkvm_init_vm(struct kvm *host_kvm, unsigned long vm_hva,
 		   unsigned long pgd_hva);
arch/arm64/kvm/hyp/nvhe/pkvm.c +0 -11
index feb27b4ce459..ea67fcbf8376 100644
@@ -249,17 +249,6 @@ void pkvm_hyp_vm_table_init(void *tbl)
 	vm_table = tbl;
 }
 
-void pkvm_host_fpsimd_state_init(void)
-{
-	unsigned long i;
-
-	for (i = 0; i < hyp_nr_cpus; i++) {
-		struct kvm_host_data *host_data = per_cpu_ptr(&kvm_host_data, i);
-
-		host_data->fpsimd_state = &host_data->host_ctxt.fp_regs;
-	}
-}
-
 /*
  * Return the hyp vm structure corresponding to the handle.
  */
arch/arm64/kvm/hyp/nvhe/setup.c +0 -1
index 5c8cd806efb9..84f766ab1810 100644
@@ -324,7 +324,6 @@ void __noreturn __pkvm_init_finalise(void)
 		goto out;
 
 	pkvm_hyp_vm_table_init(vm_table_base);
-	pkvm_host_fpsimd_state_init();
 out:
 	/*
 	 * We tail-called to here from handle___pkvm_init() and will not return,
Signature · 1 line
2.45.0.rc1.225.g2a3ae87e7f-goog
#7 lore raw

Re: [PATCH v1 6/7] KVM: arm64: Eagerly restore host fpsimd/sve state in pKVM

In reply to Fuad Tabba

Hi Fuad,

Quoted 4 lines · Fuad Tabba

static void kvm_hyp_save_fpsimd_host(struct kvm_vcpu *vcpu) {

arch/arm64/kvm/hyp/nvhe/switch.c +1 -1
-	__fpsimd_save_state(*host_data_ptr(fpsimd_state));
+	/*

arch/arm64/kvm/hyp/nvhe/switch.c static bool kvm_handle_pvm_sys64(struct kvm_vcpu *vcpu, u64 *exit_code) · line 219

Quoted 10 lines
arch/arm64/kvm/hyp/nvhe/switch.c +10 -0
+	 * Non-protected kvm relies on the host restoring its sve state.
+	 * Protected kvm restores the host's sve state as not to reveal that
+	 * fpsimd was used by a guest nor leak upper sve bits.
+	 */
+	if (unlikely(is_protected_kvm_enabled() && system_supports_sve())) {
+		__hyp_sve_save_host();
+
+		/* Re-enable SVE traps for guests that do not support it. */
+		if (!vcpu_has_sve(vcpu))
+			sysreg_clear_set(cptr_el2, 0, CPTR_EL2_TZ);

This doesn't account for hVHE. I wonder we'd be better off abstracting CPTR_EL2 behind a helper wherever it gets used in nVHE and translate into the VHE-format behind the scenes:

static inline void __cptr_clear_set_hvhe(u64 cptr_clr, u64 cptr_set)

{
	u64 clr = 0, set = 0;

	if (cptr_clr & CPTR_EL2_TFP)
		set |= CPACR_ELx_FPEN;
	if (cptr_clr & CPTR_EL2_TZ)
		set |= CPACR_ELx_ZEN;
	if (cptr_clr & CPTR_EL2_TSM)
		set |= CPACR_ELx_SMEN;

	if (cptr_set & CPTR_EL2_TFP)
		clr |= CPACR_ELx_FPEN;
	if (cptr_set & CPTR_EL2_TZ)
		clr |= CPACR_ELx_ZEN;
	if (cptr_set & CPTR_EL2_TSM)
		clr |= CPACR_ELx_SMEN;

	sysreg_clear_set(cpacr_el1, clr, set);
}

static inline void cptr_clear_set(u64 clr, u64 set)

{
	if (has_hvhe())
		__cptr_clear_set_hvhe(clr, set);
	else
		sysreg_clear_set(cptr_el2, clr, set);
}
Signature · 2 lines
Thanks,
Oliver
#13 lore raw
In reply to Fuad Tabba

Hi Fuad,

Quoted 10 lines · Fuad Tabba

With the KVM host data rework [1], handling of fpsimd and sve state in protected mode is done at hyp. For protected VMs, we don't want to leak any guest state to the host, including whether a guest has used fpsimd/sve.

To complete the work started with the host data rework, in regards to protected mode, ensure that the host's fpsimd context and its sve context are restored on guest exit, since the rework has hidden the fpsimd/sve state from the host.

Quoted 5 lines

This patch series eagerly restores the host fpsimd/sve state on guest exit when running in protected mode, which happens only if the guest has used fpsimd/sve. This means that the saving of the state is lazy, similar to the behavior of KVM in other modes, but the restoration of the host state is eager.

Hmm... Is there any reason why we need to be concerned about preserving host SVE state?

The syscall ABI has it that only the first 128 bits of the vector registers are preserved by the kernel, and I see no reason why we couldn't apply a similar restriction to KVM_RUN HVCs into EL2. We'd need to eagerly flush the vector registers on entry to avoid disclosing guest usage of SVE.

What you have is certainly correct, I just wonder if we're going out of our way to save/restore 0's for larger VLs.

Signature · 2 lines
Thanks,
Oliver
#14 lore raw
Quoted 11 lines · Oliver Upton

Hmm... Is there any reason why we need to be concerned about preserving host SVE state?

The syscall ABI has it that only the first 128 bits of the vector registers are preserved by the kernel, and I see no reason why we couldn't apply a similar restriction to KVM_RUN HVCs into EL2. We'd need to eagerly flush the vector registers on entry to avoid disclosing guest usage of SVE.

What you have is certainly correct, I just wonder if we're going out of our way to save/restore 0's for larger VLs.

Not just larger VLs, there's also the P registers even for 128 bit SVE.

I think it'd be sensible to discard. A big part of why the host ABI is like that is that the AAPCS makes the SVE specific state caller preserved on function calls, with syscalls mirroring that. This means that even if the kernel is using FP the HVC would need to be inline in a function using SVE in order to get any state that needs to be preserved in there, or there'd need to be some other non-AAPCS thing going on. We already ensure that any EL0 state is saved prior to trying to run a VM, I've not checked the interaction with pKVM here but if there's any issues I'd hope it's not too difficult to close them.

author
#15 lore raw

Hi Oliver and Mark,

Quoted 17 lines · Mark Brown
Quoted 11 lines · Oliver Upton

Hmm... Is there any reason why we need to be concerned about preserving host SVE state?

The syscall ABI has it that only the first 128 bits of the vector registers are preserved by the kernel, and I see no reason why we couldn't apply a similar restriction to KVM_RUN HVCs into EL2. We'd need to eagerly flush the vector registers on entry to avoid disclosing guest usage of SVE.

What you have is certainly correct, I just wonder if we're going out of our way to save/restore 0's for larger VLs.

Not just larger VLs, there's also the P registers even for 128 bit SVE.

Quoted 9 lines

I think it'd be sensible to discard. A big part of why the host ABI is like that is that the AAPCS makes the SVE specific state caller preserved on function calls, with syscalls mirroring that. This means that even if the kernel is using FP the HVC would need to be inline in a function using SVE in order to get any state that needs to be preserved in there, or there'd need to be some other non-AAPCS thing going on. We already ensure that any EL0 state is saved prior to trying to run a VM, I've not checked the interaction with pKVM here but if there's any issues I'd hope it's not too difficult to close them.

The reason for that is that in pKVM we want to avoid leaking any information about protected VM activity to the host, including whether the VM might have performed fpsimd/sve operations. Therefore, we need to ensure that the host SVE state looks the same after a protected guest has run as it did before a protected guest has run.

It would be correct to only save/restore the host's fpsimd state (i.e., first 128 bits of the vector registers), which is what KVM does in other modes. However, unless we always zero out the rest of the state, regardless whether the protected guest has used fpsimd/sve, then the host would be able to find out that the guest has in fact performed fpsimd/sve operations.

This isn't necessary for non-protected VMs, but Marc thought that for now it would be better to simplify things and have pKVM behave the same way for both protected and non-protected VMs. As a future optimization for non-protected VMs, we could have them behave as VMs in other modes.

Thanks,
/fuad

author
#8 lore raw

Re: [PATCH v1 6/7] KVM: arm64: Eagerly restore host fpsimd/sve state in pKVM

In reply to Oliver Upton

Hi Oliver,

Quoted 19 lines · Oliver Upton

Hi Fuad,

Quoted 4 lines · Fuad Tabba

static void kvm_hyp_save_fpsimd_host(struct kvm_vcpu *vcpu) {

arch/arm64/kvm/hyp/nvhe/switch.c +1 -1
-     __fpsimd_save_state(*host_data_ptr(fpsimd_state));
+     /*

arch/arm64/kvm/hyp/nvhe/switch.c static bool kvm_handle_pvm_sys64(struct kvm_vcpu *vcpu, u64 *exit_code) · line 219

Quoted 10 lines
arch/arm64/kvm/hyp/nvhe/switch.c +10 -0
+      * Non-protected kvm relies on the host restoring its sve state.
+      * Protected kvm restores the host's sve state as not to reveal that
+      * fpsimd was used by a guest nor leak upper sve bits.
+      */
+     if (unlikely(is_protected_kvm_enabled() && system_supports_sve())) {
+             __hyp_sve_save_host();
+
+             /* Re-enable SVE traps for guests that do not support it. */
+             if (!vcpu_has_sve(vcpu))
+                     sysreg_clear_set(cptr_el2, 0, CPTR_EL2_TZ);
Quoted 3 lines

This doesn't account for hVHE. I wonder we'd be better off abstracting CPTR_EL2 behind a helper wherever it gets used in nVHE and translate into the VHE-format behind the scenes:

Right! Too many modes to keep track of :)

Abstracting cptr_el2 would make things clearer and less error-prone. I'll do that on the respin.

Cheers,
/fuad

Quoted 33 lines

static inline void __cptr_clear_set_hvhe(u64 cptr_clr, u64 cptr_set)

{
        u64 clr = 0, set = 0;

        if (cptr_clr & CPTR_EL2_TFP)
                set |= CPACR_ELx_FPEN;
        if (cptr_clr & CPTR_EL2_TZ)
                set |= CPACR_ELx_ZEN;
        if (cptr_clr & CPTR_EL2_TSM)
                set |= CPACR_ELx_SMEN;

        if (cptr_set & CPTR_EL2_TFP)
                clr |= CPACR_ELx_FPEN;
        if (cptr_set & CPTR_EL2_TZ)
                clr |= CPACR_ELx_ZEN;
        if (cptr_set & CPTR_EL2_TSM)
                clr |= CPACR_ELx_SMEN;

        sysreg_clear_set(cpacr_el1, clr, set);
}

static inline void cptr_clear_set(u64 clr, u64 set)

{
        if (has_hvhe())
                __cptr_clear_set_hvhe(clr, set);
        else
                sysreg_clear_set(cptr_el2, clr, set);
}

--
Thanks,
Oliver

maintainer
#9 lore raw

Re: [PATCH v1 6/7] KVM: arm64: Eagerly restore host fpsimd/sve state in pKVM

Quoted 23 lines · Fuad Tabba

Hi Oliver,

Quoted 19 lines · Oliver Upton

Hi Fuad,

Quoted 14 lines · Fuad Tabba

static void kvm_hyp_save_fpsimd_host(struct kvm_vcpu *vcpu) {

arch/arm64/kvm/hyp/nvhe/switch.c +11 -1
-     __fpsimd_save_state(*host_data_ptr(fpsimd_state));
+     /*
+      * Non-protected kvm relies on the host restoring its sve state.
+      * Protected kvm restores the host's sve state as not to reveal that
+      * fpsimd was used by a guest nor leak upper sve bits.
+      */
+     if (unlikely(is_protected_kvm_enabled() && system_supports_sve())) {
+             __hyp_sve_save_host();
+
+             /* Re-enable SVE traps for guests that do not support it. */
+             if (!vcpu_has_sve(vcpu))
+                     sysreg_clear_set(cptr_el2, 0, CPTR_EL2_TZ);
Quoted 8 lines
Quoted 3 lines

This doesn't account for hVHE. I wonder we'd be better off abstracting CPTR_EL2 behind a helper wherever it gets used in nVHE and translate into the VHE-format behind the scenes:

Right! Too many modes to keep track of :)

Abstracting cptr_el2 would make things clearer and less error-prone. I'll do that on the respin.

If we're going with the conversion game, then I'd suggest you use the VHE format as the reference, and convert it to nVHE on the flight. That's for a few reasons:

- like it or not, nVHE is going the way of the dodo. I love my v8.0 hardware to bits, but it sucks, and nVHE is now optional anyway.

- Keeping everything in the VHE format helps drawing a parallel with what is happening in the kernel (you grep for the same symbols).

- One day, I hope to be able to rip any form of SVE/SME support out of nVHE and only keep it for hVHE, because there is no ARMv8.0 implementations with these extensions (apart from SW models). One day...

Thanks,

	M.
Signature · 1 line
Without deviation from the norm, progress is not possible.
maintainer
#16 lore raw
In reply to Fuad Tabba
Quoted 44 lines · Fuad Tabba

Hi Oliver and Mark,

Quoted 17 lines · Mark Brown
Quoted 11 lines · Oliver Upton

Hmm... Is there any reason why we need to be concerned about preserving host SVE state?

The syscall ABI has it that only the first 128 bits of the vector registers are preserved by the kernel, and I see no reason why we couldn't apply a similar restriction to KVM_RUN HVCs into EL2. We'd need to eagerly flush the vector registers on entry to avoid disclosing guest usage of SVE.

What you have is certainly correct, I just wonder if we're going out of our way to save/restore 0's for larger VLs.

Not just larger VLs, there's also the P registers even for 128 bit SVE.

Quoted 9 lines

I think it'd be sensible to discard. A big part of why the host ABI is like that is that the AAPCS makes the SVE specific state caller preserved on function calls, with syscalls mirroring that. This means that even if the kernel is using FP the HVC would need to be inline in a function using SVE in order to get any state that needs to be preserved in there, or there'd need to be some other non-AAPCS thing going on. We already ensure that any EL0 state is saved prior to trying to run a VM, I've not checked the interaction with pKVM here but if there's any issues I'd hope it's not too difficult to close them.

The reason for that is that in pKVM we want to avoid leaking any information about protected VM activity to the host, including whether the VM might have performed fpsimd/sve operations. Therefore, we need to ensure that the host SVE state looks the same after a protected guest has run as it did before a protected guest has run.

It would be correct to only save/restore the host's fpsimd state (i.e., first 128 bits of the vector registers), which is what KVM does in other modes. However, unless we always zero out the rest of the state, regardless whether the protected guest has used fpsimd/sve, then the host would be able to find out that the guest has in fact performed fpsimd/sve operations.

Quoted 5 lines

This isn't necessary for non-protected VMs, but Marc thought that for now it would be better to simplify things and have pKVM behave the same way for both protected and non-protected VMs. As a future optimization for non-protected VMs, we could have them behave as VMs in other modes.

And I stand by what I said. Having a hybrid mode is a maintenance burden, and it will absolutely lead to some sort of horrible bugs (it just take a look at the mailing list to see that we have no shortage of bugs related to lazy FP/SVE handling).

If someone is desperate for lazy handling, then the lazy handling should apply to both protected and non-protected VMs so that we can actually reason about it.

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

Re: [PATCH v1 6/7] KVM: arm64: Eagerly restore host fpsimd/sve state in pKVM

In reply to Marc Zyngier

Hi Marc,

Quoted 30 lines · Marc Zyngier
Quoted 27 lines · Fuad Tabba

Hi Oliver,

Quoted 22 lines · Oliver Upton

Hi Fuad,

Quoted 4 lines · Fuad Tabba

static void kvm_hyp_save_fpsimd_host(struct kvm_vcpu *vcpu) {

arch/arm64/kvm/hyp/nvhe/switch.c +1 -1
-     __fpsimd_save_state(*host_data_ptr(fpsimd_state));
+     /*

arch/arm64/kvm/hyp/nvhe/switch.c static bool kvm_handle_pvm_sys64(struct kvm_vcpu *vcpu, u64 *exit_code) · line 219

Quoted 10 lines
arch/arm64/kvm/hyp/nvhe/switch.c +10 -0
+      * Non-protected kvm relies on the host restoring its sve state.
+      * Protected kvm restores the host's sve state as not to reveal that
+      * fpsimd was used by a guest nor leak upper sve bits.
+      */
+     if (unlikely(is_protected_kvm_enabled() && system_supports_sve())) {
+             __hyp_sve_save_host();
+
+             /* Re-enable SVE traps for guests that do not support it. */
+             if (!vcpu_has_sve(vcpu))
+                     sysreg_clear_set(cptr_el2, 0, CPTR_EL2_TZ);

This doesn't account for hVHE. I wonder we'd be better off abstracting CPTR_EL2 behind a helper wherever it gets used in nVHE and translate into the VHE-format behind the scenes:

Quoted 8 lines
Quoted 4 lines

Right! Too many modes to keep track of :)

Abstracting cptr_el2 would make things clearer and less error-prone. I'll do that on the respin.

If we're going with the conversion game, then I'd suggest you use the VHE format as the reference, and convert it to nVHE on the flight. That's for a few reasons:

Will do.

Cheers,
/fuad

Quoted 18 lines

- like it or not, nVHE is going the way of the dodo. I love my v8.0 hardware to bits, but it sucks, and nVHE is now optional anyway.

- Keeping everything in the VHE format helps drawing a parallel with what is happening in the kernel (you grep for the same symbols).

- One day, I hope to be able to rip any form of SVE/SME support out of nVHE and only keep it for hVHE, because there is no ARMv8.0 implementations with these extensions (apart from SW models). One day...

Thanks,

        M.

--
Without deviation from the norm, progress is not possible.

#11 lore raw

Re: [PATCH v1 6/7] KVM: arm64: Eagerly restore host fpsimd/sve state in pKVM

In reply to Marc Zyngier
Quoted 29 lines · Marc Zyngier
Quoted 27 lines · Fuad Tabba

Hi Oliver,

Quoted 22 lines · Oliver Upton

Hi Fuad,

Quoted 4 lines · Fuad Tabba

static void kvm_hyp_save_fpsimd_host(struct kvm_vcpu *vcpu) {

arch/arm64/kvm/hyp/nvhe/switch.c +1 -1
-     __fpsimd_save_state(*host_data_ptr(fpsimd_state));
+     /*

arch/arm64/kvm/hyp/nvhe/switch.c static bool kvm_handle_pvm_sys64(struct kvm_vcpu *vcpu, u64 *exit_code) · line 219

Quoted 10 lines
arch/arm64/kvm/hyp/nvhe/switch.c +10 -0
+      * Non-protected kvm relies on the host restoring its sve state.
+      * Protected kvm restores the host's sve state as not to reveal that
+      * fpsimd was used by a guest nor leak upper sve bits.
+      */
+     if (unlikely(is_protected_kvm_enabled() && system_supports_sve())) {
+             __hyp_sve_save_host();
+
+             /* Re-enable SVE traps for guests that do not support it. */
+             if (!vcpu_has_sve(vcpu))
+                     sysreg_clear_set(cptr_el2, 0, CPTR_EL2_TZ);

This doesn't account for hVHE. I wonder we'd be better off abstracting CPTR_EL2 behind a helper wherever it gets used in nVHE and translate into the VHE-format behind the scenes:

Quoted 8 lines
Quoted 4 lines

Right! Too many modes to keep track of :)

Abstracting cptr_el2 would make things clearer and less error-prone. I'll do that on the respin.

If we're going with the conversion game, then I'd suggest you use the VHE format as the reference, and convert it to nVHE on the flight. That's for a few reasons:

Agreed, I was thinking of hVHE as this 'thing on the side', but really it is the direction of the architecture :) So yeah, prefer your suggestion.

Signature · 2 lines
Thanks,
Oliver
#17 lore raw
In reply to Marc Zyngier
Quoted 6 lines · Marc Zyngier
Quoted 5 lines · Fuad Tabba

The reason for that is that in pKVM we want to avoid leaking any information about protected VM activity to the host, including whether the VM might have performed fpsimd/sve operations. Therefore, we need to ensure that the host SVE state looks the same after a protected guest has run as it did before a protected guest has run.

Wouldn't it be equally valid to just zero the state that will not be preserved regardless of whether or not the guest used fpsimd/sve?

Quoted 13 lines
Quoted 7 lines

It would be correct to only save/restore the host's fpsimd state (i.e., first 128 bits of the vector registers), which is what KVM does in other modes. However, unless we always zero out the rest of the state, regardless whether the protected guest has used fpsimd/sve, then the host would be able to find out that the guest has in fact performed fpsimd/sve operations.

Quoted 5 lines

This isn't necessary for non-protected VMs, but Marc thought that for now it would be better to simplify things and have pKVM behave the same way for both protected and non-protected VMs. As a future optimization for non-protected VMs, we could have them behave as VMs in other modes.

Quoted 4 lines

And I stand by what I said. Having a hybrid mode is a maintenance burden, and it will absolutely lead to some sort of horrible bugs (it just take a look at the mailing list to see that we have no shortage of bugs related to lazy FP/SVE handling).

Agree, but I don't think the suggestion is in any way incompatible with eager save/restore of FP/SVE state.

From the looks of it, we're *still* adding protected-mode specialization to save/restore the host's SVE state, even though we decided in commit 8383741ab2e7 ("KVM: arm64: Get rid of host SVE tracking/saving") that this was completely unnecessary in non-protected configurations.

What I'm instead suggesting is that we make it part of the __kvm_vcpu_run() API that the non-overlapping SVE state gets discarded by the callee, which would align with an expectation that the host kernel has already done this upon syscall entry.

Then all of the FPSIMD/SVE save/restore logic we have in the hyp 'just works' so long as we 0 the SVE registers before loading in the host's FPSIMD state.

Signature · 2 lines
Thanks,
Oliver
#18 lore raw
Quoted 2 lines · Oliver Upton

Wouldn't it be equally valid to just zero the state that will not be preserved regardless of whether or not the guest used fpsimd/sve?

FWIW I've had this benchmarked for some implementations as causing a low single digits percentage overhead on syscalls (as you'd expect there's additional overhead when VLs over 128 are supported).

author
#20 lore raw
In reply to Oliver Upton

Hi Oliver,

Quoted 11 lines · Oliver Upton
Quoted 6 lines · Marc Zyngier
Quoted 5 lines · Fuad Tabba

The reason for that is that in pKVM we want to avoid leaking any information about protected VM activity to the host, including whether the VM might have performed fpsimd/sve operations. Therefore, we need to ensure that the host SVE state looks the same after a protected guest has run as it did before a protected guest has run.

Wouldn't it be equally valid to just zero the state that will not be preserved regardless of whether or not the guest used fpsimd/sve?

Yes it would. I think I did mention that as an option. However, that would need to be done at every protected guest exit, whereas restoring the host SVE state only needs to be done if the guest has used fpsimd/sve.

I think the code for the latter (i.e., zeroing out), would be simpler. I'm happy to do it that way if you and the others think it's better.

Quoted 26 lines
Quoted 13 lines
Quoted 7 lines

It would be correct to only save/restore the host's fpsimd state (i.e., first 128 bits of the vector registers), which is what KVM does in other modes. However, unless we always zero out the rest of the state, regardless whether the protected guest has used fpsimd/sve, then the host would be able to find out that the guest has in fact performed fpsimd/sve operations.

Quoted 5 lines

This isn't necessary for non-protected VMs, but Marc thought that for now it would be better to simplify things and have pKVM behave the same way for both protected and non-protected VMs. As a future optimization for non-protected VMs, we could have them behave as VMs in other modes.

Quoted 4 lines

And I stand by what I said. Having a hybrid mode is a maintenance burden, and it will absolutely lead to some sort of horrible bugs (it just take a look at the mailing list to see that we have no shortage of bugs related to lazy FP/SVE handling).

Agree, but I don't think the suggestion is in any way incompatible with eager save/restore of FP/SVE state.

From the looks of it, we're *still* adding protected-mode specialization to save/restore the host's SVE state, even though we decided in commit 8383741ab2e7 ("KVM: arm64: Get rid of host SVE tracking/saving") that this was completely unnecessary in non-protected configurations.

Quoted 8 lines

What I'm instead suggesting is that we make it part of the __kvm_vcpu_run() API that the non-overlapping SVE state gets discarded by the callee, which would align with an expectation that the host kernel has already done this upon syscall entry.

Then all of the FPSIMD/SVE save/restore logic we have in the hyp 'just works' so long as we 0 the SVE registers before loading in the host's FPSIMD state.

If Marc is happy with this approach, I could do it that way. Either way, I'll hack on it and present it as an alternative in my respin.

Cheers,
/fuad

Quoted 3 lines

--
Thanks,
Oliver

author
#19 lore raw
In reply to Mark Brown
Quoted 9 lines · Mark Brown
Quoted 2 lines · Oliver Upton

Wouldn't it be equally valid to just zero the state that will not be preserved regardless of whether or not the guest used fpsimd/sve?

FWIW I've had this benchmarked for some implementations as causing a low single digits percentage overhead on syscalls (as you'd expect there's additional overhead when VLs over 128 are supported).

Interesting. Is this an argument in favor of zeroing (i.e., the overhead is low and acceptable), or against (it is still an overhead)?

Thanks,
/fuad

#21 lore raw
In reply to Fuad Tabba

Hey Fuad,

Quoted 12 lines · Fuad Tabba

Hi Oliver,

Quoted 9 lines · Oliver Upton
Quoted 6 lines · Marc Zyngier
Quoted 5 lines · Fuad Tabba

The reason for that is that in pKVM we want to avoid leaking any information about protected VM activity to the host, including whether the VM might have performed fpsimd/sve operations. Therefore, we need to ensure that the host SVE state looks the same after a protected guest has run as it did before a protected guest has run.

Quoted 4 lines
Quoted 2 lines

Wouldn't it be equally valid to just zero the state that will not be preserved regardless of whether or not the guest used fpsimd/sve?

Yes it would. I think I did mention that as an option.

Apologies, I probably missed it earlier on then.

Quoted 3 lines

However, that would need to be done at every protected guest exit, whereas restoring the host SVE state only needs to be done if the guest has used fpsimd/sve.

Indeed, what I was _hoping_ is that implementations do a decent job of handling a zeroing idiom for SVE and avoid needing to fetch a bunch of state out of memory.

Quoted 2 lines

I think the code for the latter (i.e., zeroing out), would be simpler. I'm happy to do it that way if you and the others think it's better.

Right, I have no fundamental objections to fully managing the host SVE state in EL2. Strong preference for something simple + correct in the interim. Anyway, thanks for suffering through my whining and hopefully we can land a fix soon :)

Signature · 2 lines
Best,
Oliver
author
#22 lore raw

Hi Oliver,

Quoted 31 lines · Oliver Upton

Hey Fuad,

Quoted 12 lines · Fuad Tabba

Hi Oliver,

Quoted 9 lines · Oliver Upton
Quoted 6 lines · Marc Zyngier
Quoted 5 lines · Fuad Tabba

The reason for that is that in pKVM we want to avoid leaking any information about protected VM activity to the host, including whether the VM might have performed fpsimd/sve operations. Therefore, we need to ensure that the host SVE state looks the same after a protected guest has run as it did before a protected guest has run.

Quoted 4 lines
Quoted 2 lines

Wouldn't it be equally valid to just zero the state that will not be preserved regardless of whether or not the guest used fpsimd/sve?

Yes it would. I think I did mention that as an option.

Apologies, I probably missed it earlier on then.

Quoted 3 lines

However, that would need to be done at every protected guest exit, whereas restoring the host SVE state only needs to be done if the guest has used fpsimd/sve.

Indeed, what I was _hoping_ is that implementations do a decent job of handling a zeroing idiom for SVE and avoid needing to fetch a bunch of state out of memory.

Quoted 7 lines
Quoted 2 lines

I think the code for the latter (i.e., zeroing out), would be simpler. I'm happy to do it that way if you and the others think it's better.

Right, I have no fundamental objections to fully managing the host SVE state in EL2. Strong preference for something simple + correct in the interim. Anyway, thanks for suffering through my whining and hopefully we can land a fix soon :)

Thanks for your review and comments, which are very helpful as always. I'll respin this within the next couple of days.

Cheers,
/fuad

Quoted 3 lines

--
Best,
Oliver