KVM: arm64: Allocate for maximum vector length for pKVM host sve state
PATCH v2 1/1 KVM: arm64: Allocate for maximum vector length for pKVM host sve state
In a heterogeneous system, a cpu could have an sve/sme vector length that is larger than the rest. Store the value of the largest possible vector length in the system. When allocating memory for the host sve state in pKVM, allocate for the largest possible sve vector length.
---
Based on kvmarm/fixes (afb91f5f8ad7)
Changes from v1:
- Rename sve_max_system_vl to sve_max_cpu_vl (Mark)
- Rename kvm_host_sve_max_vl to kvm_sve_max_cpu_vl (Mark)
- Initialize vl_info[ARM64_VEC_SVE].max_cpu_vl to SVE_VL_MIN
--- arch/arm64/include/asm/fpsimd.h | 13 +++++++++++++ arch/arm64/include/asm/kvm_host.h | 2 +- arch/arm64/include/asm/kvm_hyp.h | 2 +- arch/arm64/include/asm/kvm_pkvm.h | 2 +- arch/arm64/kernel/fpsimd.c | 8 +++++++- arch/arm64/kvm/hyp/include/hyp/switch.h | 2 +- arch/arm64/kvm/hyp/nvhe/hyp-main.c | 4 ++-- arch/arm64/kvm/hyp/nvhe/pkvm.c | 2 +- arch/arm64/kvm/reset.c | 6 +++--- 9 files changed, 30 insertions(+), 11 deletions(-)
arch/arm64/include/asm/fpsimd.h +13 -0
@@ -184,6 +184,9 @@ struct vl_info {int max_vl;int max_virtualisable_vl; +/* The maximum vl encountered for any cpu in the system. */ +int max_cpu_vl; +/* * Set of available vector lengths, * where length vq encoded as bit __vq_to_bit(vq):
@@ -274,6 +277,11 @@ static inline int vec_max_virtualisable_vl(enum vec_type type)return vl_info[type].max_virtualisable_vl; } +static inline int vec_max_cpu_vl (enum vec_type type) +{ +return vl_info[type].max_cpu_vl; +} +static inline int sve_max_vl (void ) {return vec_max_vl (ARM64_VEC_SVE );
@@ -284,6 +292,11 @@ static inline int sve_max_virtualisable_vl(void)return vec_max_virtualisable_vl (ARM64_VEC_SVE ); } +static inline int sve_max_cpu_vl (void ) +{ +return vec_max_cpu_vl (ARM64_VEC_SVE ); +} +/* Ensure vq >= SVE_VQ_MIN && vq <= SVE_VQ_MAX before calling this function */ static inline bool vq_available (enum vec_type type,unsigned int vq) {
arch/arm64/include/asm/kvm_host.h +1 -1
@@ -76,7 +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; +extern unsigned int __ro_after_init kvm_sve_max_cpu_vl;int __init kvm_arm_init_sve (void ); u32 __attribute_const__ kvm_target_cpu(void);
arch/arm64/include/asm/kvm_hyp.h +1 -1
@@ -143,6 +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 ); +extern unsigned int kvm_nvhe_sym (kvm_sve_max_cpu_vl );#endif /* __ARM64_KVM_HYP_H__ */
arch/arm64/include/asm/kvm_pkvm.h +1 -1
@@ -134,7 +134,7 @@ static inline size_t pkvm_host_sve_state_size(void)return 0 ;return size_add (sizeof (struct cpu_sve_state ), - SVE_SIG_REGS_SIZE(sve_vq_from_vl(kvm_host_sve_max_vl))); + SVE_SIG_REGS_SIZE(sve_vq_from_vl(kvm_sve_max_cpu_vl))); }#endif /* __ARM64_KVM_PKVM_H__ */
arch/arm64/kernel/fpsimd.c +7 -1
@@ -129,6 +129,7 @@ __ro_after_init struct vl_info vl_info[ARM64_VEC_MAX] = { .min_vl = SVE_VL_MIN, .max_vl = SVE_VL_MIN, .max_virtualisable_vl = SVE_VL_MIN, + .max_cpu_vl = SVE_VL_MIN, },#endif #ifdef CONFIG_ARM64_SME
@@ -1006,7 +1007,7 @@ int sme_get_current_vl(void)static void vec_probe_vqs (struct vl_info *info,DECLARE_BITMAP (map ,SVE_VQ_MAX )) { -unsigned int vq, vl; +unsigned int vq, vl, max_vl =0 ;bitmap_zero (map,SVE_VQ_MAX );
@@ -1031,7 +1032,12 @@ static void vec_probe_vqs(struct vl_info *info, vq =sve_vq_from_vl (vl);/* skip intervening lengths */ set_bit (__vq_to_bit (vq), map); + +if (!max_vl) + max_vl = vl; } + + info->max_cpu_vl =max ((int ) max_vl, info->max_cpu_vl); }/*
arch/arm64/kvm/hyp/include/hyp/switch.h +1 -1
@@ -327,7 +327,7 @@ static inline void __hyp_sve_save_host(void) sve_state->zcr_el1 =read_sysreg_el1 (SYS_ZCR );write_sysreg_s (ZCR_ELx_LEN_MASK,SYS_ZCR_EL2 ); -__sve_save_state (sve_state->sve_regs +sve_ffr_offset (kvm_host_sve_max_vl), +__sve_save_state (sve_state->sve_regs +sve_ffr_offset (kvm_sve_max_cpu_vl), &sve_state->fpsr, true); }
arch/arm64/kvm/hyp/nvhe/hyp-main.c +2 -2
@@ -49,7 +49,7 @@ static void __hyp_sve_restore_host(void)* supported by the system (or limited at EL3). */ write_sysreg_s (ZCR_ELx_LEN_MASK,SYS_ZCR_EL2 ); -__sve_restore_state (sve_state->sve_regs +sve_ffr_offset (kvm_host_sve_max_vl), +__sve_restore_state (sve_state->sve_regs +sve_ffr_offset (kvm_sve_max_cpu_vl), &sve_state->fpsr, true);write_sysreg_el1 (sve_state->zcr_el1,SYS_ZCR );
@@ -91,7 +91,7 @@ static void flush_hyp_vcpu(struct pkvm_hyp_vcpu *hyp_vcpu) hyp_vcpu->vcpu.arch.sve_state =kern_hyp_va (host_vcpu->arch.sve_state);/* Limit guest vector length to the maximum supported by the host. */ - hyp_vcpu->vcpu.arch.sve_max_vl =min (host_vcpu->arch.sve_max_vl, kvm_host_sve_max_vl); + hyp_vcpu->vcpu.arch.sve_max_vl =min (host_vcpu->arch.sve_max_vl, kvm_sve_max_cpu_vl); hyp_vcpu->vcpu.arch.hw_mmu = host_vcpu->arch.hw_mmu;
arch/arm64/kvm/hyp/nvhe/pkvm.c +1 -1
@@ -18,7 +18,7 @@ unsigned long __icache_flags;/* Used by kvm_get_vttbr(). */ unsigned int kvm_arm_vmid_bits; -unsigned int kvm_host_sve_max_vl; +unsigned int kvm_sve_max_cpu_vl;/* * Set trap register values based on features in ID_AA64PFR0.
arch/arm64/kvm/reset.c +3 -3
@@ -32,7 +32,7 @@/* Maximum phys_shift supported for any VM on this host */ static u32 __ro_after_init kvm_ipa_limit; -unsigned int __ro_after_init kvm_host_sve_max_vl; +unsigned int __ro_after_init kvm_sve_max_cpu_vl;/* * ARMv8 Reset Values
@@ -52,8 +52,8 @@ 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 (); -kvm_nvhe_sym (kvm_host_sve_max_vl) = kvm_host_sve_max_vl; + kvm_sve_max_cpu_vl =sve_max_cpu_vl (); +kvm_nvhe_sym (kvm_sve_max_cpu_vl) = kvm_sve_max_cpu_vl;/* * The get_sve_reg()/set_sve_reg() ioctl interface will need
base-commit: afb91f5f8ad7af172d993a34fde1947892408f53
Signature · 1 line
2.45.1.467.gbab1589fc0-goog
arch/arm64/kernel/fpsimd.c static void vec_probe_vqs(struct vl_info *info, · line 1043
Quoted 13 lines · Fuad Tabba
arch/arm64/kernel/fpsimd.c +5
-0@@ -1031,7 +1032,12 @@ static void vec_probe_vqs(struct vl_info *info, vq =sve_vq_from_vl (vl);/* skip intervening lengths */ set_bit (__vq_to_bit (vq), map); + +if (!max_vl) + max_vl = vl; } + + info->max_cpu_vl =max ((int ) max_vl, info->max_cpu_vl); }/*
As discussed I'm much more comfortable doing this in line with the rest of the discovery and not having vec_probe_vqs() modify global state. We also really ought to have some validation that late CPUs aren't doing anything funky too, even if we don't actually need it now.