Skip to contentSkip to conversation

KVM: arm64: Move host-specific data out of kvm_vcpu_arch

PATCH In review Marc Zyngier 3 participants 16 messages Mar 22, 2024 – Mar 27, 2024

Subsystems (3)
lore
Share & tools
lore

Marc Zyngier

author maintainer
#0 lore raw

v2 cover KVM: arm64: Move host-specific data out of kvm_vcpu_arch

This is the second take on this series aiming at reducing the abuse of the kvm_vcpu_arch structure, and moving things info the per-CPU context.

* From v1 [1]:

- Fixed the per-CPU accessor outside of the hypervisor code (the protected case is... interesting)

- Spelling fixes

- Collected RBs

- Rebased on kvmarm-6.9

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

Marc Zyngier (5):
KVM: arm64: Add accessor for per-CPU state
KVM: arm64: Exclude host_debug_data from vcpu_arch
KVM: arm64: Exclude mdcr_el2_host from kvm_vcpu_arch
KVM: arm64: Exclude host_fpsimd_state pointer from kvm_vcpu_arch
KVM: arm64: Exclude FP ownership from kvm_vcpu_arch

 arch/arm64/include/asm/kvm_emulate.h      |  4 +-
 arch/arm64/include/asm/kvm_host.h         | 89 ++++++++++++++++-------
 arch/arm64/kvm/arm.c                      |  8 +-
 arch/arm64/kvm/fpsimd.c                   | 13 ++--
 arch/arm64/kvm/hyp/include/hyp/debug-sr.h |  8 +-
 arch/arm64/kvm/hyp/include/hyp/switch.h   | 20 ++---
 arch/arm64/kvm/hyp/nvhe/debug-sr.c        |  8 +-
 arch/arm64/kvm/hyp/nvhe/hyp-main.c        |  3 -
 arch/arm64/kvm/hyp/nvhe/psci-relay.c      |  2 +-
 arch/arm64/kvm/hyp/nvhe/setup.c           |  3 +-
 arch/arm64/kvm/hyp/nvhe/switch.c          |  6 +-
 arch/arm64/kvm/hyp/vhe/switch.c           |  6 +-
 arch/arm64/kvm/hyp/vhe/sysreg-sr.c        |  4 +-
 arch/arm64/kvm/pmu.c                      |  2 +-
 14 files changed, 102 insertions(+), 74 deletions(-)
Signature · 1 line
2.39.2
author maintainer
#1 lore raw

PATCH v2 1/5 KVM: arm64: Add accessor for per-CPU state

In order to facilitate the introduction of new per-CPU state, add a new host_data_ptr() helped that hides some of the per-CPU verbosity, and make it easier to move that state around in the future.

---
 arch/arm64/include/asm/kvm_host.h         | 37 +++++++++++++++++++++++
 arch/arm64/kvm/arm.c                      |  2 +-
 arch/arm64/kvm/hyp/include/hyp/debug-sr.h |  4 +--
 arch/arm64/kvm/hyp/include/hyp/switch.h   |  8 ++---
 arch/arm64/kvm/hyp/nvhe/psci-relay.c      |  2 +-
 arch/arm64/kvm/hyp/nvhe/setup.c           |  3 +-
 arch/arm64/kvm/hyp/nvhe/switch.c          |  4 +--
 arch/arm64/kvm/hyp/vhe/switch.c           |  4 +--
 arch/arm64/kvm/hyp/vhe/sysreg-sr.c        |  4 +--
 arch/arm64/kvm/pmu.c                      |  2 +-
 10 files changed, 53 insertions(+), 17 deletions(-)
arch/arm64/include/asm/kvm_host.h +37 -0
index 6883963bbc3a..ca6ef663950d 100644
@@ -530,6 +530,17 @@ struct kvm_cpu_context {
 	u64 *vncr_array;
 };
 
+/*
+ * This structure is instantiated on a per-CPU basis, and contains
+ * data that is:
+ *
+ * - tied to a single physical CPU, and
+ * - either have a lifetime that does not extend past vcpu_put()
+ * - or is an invariant for the lifetime of the system
+ *
+ * Use host_data_ptr(field) as a way to access a pointer to such a
+ * field.
+ */
 struct kvm_host_data {
 	struct kvm_cpu_context host_ctxt;
 };
@@ -1167,6 +1178,32 @@ struct kvm_vcpu *kvm_mpidr_to_vcpu(struct kvm *kvm, unsigned long mpidr);
 
 DECLARE_KVM_HYP_PER_CPU(struct kvm_host_data, kvm_host_data);
 
+/*
+ * How we access per-CPU host data depends on the where we access it from,
+ * and the mode we're in:
+ *
+ * - VHE and nVHE hypervisor bits use their locally defined instance
+ *
+ * - the rest of the kernel use either the VHE or nVHE one, depending on
+ *   the mode we're running in.
+ *
+ *   Unless we're in protected mode, fully deprivileged, and the nVHE
+ *   per-CPU stuff is exclusively accessible to the protected EL2 code.
+ *   In this case, the EL1 code uses the *VHE* data as its private state
+ *   (which makes sense in a way as there shouldn't be any shared state
+ *   between the host and the hypervisor).
+ *
+ * Yes, this is all totally trivial. Shoot me now.
+ */
+#if defined(__KVM_NVHE_HYPERVISOR__) || defined(__KVM_VHE_HYPERVISOR__)
+#define host_data_ptr(f)	(&this_cpu_ptr(&kvm_host_data)->f)
+#else
+#define host_data_ptr(f)						\
+	(static_branch_unlikely(&kvm_protected_mode_initialized) ?	\
+	 &this_cpu_ptr(&kvm_host_data)->f :				\
+	 &this_cpu_ptr_hyp_sym(kvm_host_data)->f)
+#endif
+
 static inline void kvm_init_host_cpu_context(struct kvm_cpu_context *cpu_ctxt)
 {
 	/* The host's MPIDR is immutable, so let's set it up at boot time */
arch/arm64/kvm/arm.c +1 -1
index 3dee5490eea9..a24287c3ba99 100644
@@ -1971,7 +1971,7 @@ static void cpu_set_hyp_vector(void)
 
 static void cpu_hyp_init_context(void)
 {
-	kvm_init_host_cpu_context(&this_cpu_ptr_hyp_sym(kvm_host_data)->host_ctxt);
+	kvm_init_host_cpu_context(host_data_ptr(host_ctxt));
 
 	if (!is_kernel_in_hyp_mode())
 		cpu_init_hyp_mode();
arch/arm64/kvm/hyp/include/hyp/debug-sr.h +2 -2
index 961bbef104a6..eec0f8ccda56 100644
@@ -135,7 +135,7 @@ static inline void __debug_switch_to_guest_common(struct kvm_vcpu *vcpu)
 	if (!vcpu_get_flag(vcpu, DEBUG_DIRTY))
 		return;
 
-	host_ctxt = &this_cpu_ptr(&kvm_host_data)->host_ctxt;
+	host_ctxt = host_data_ptr(host_ctxt);
 	guest_ctxt = &vcpu->arch.ctxt;
 	host_dbg = &vcpu->arch.host_debug_state.regs;
 	guest_dbg = kern_hyp_va(vcpu->arch.debug_ptr);
@@ -154,7 +154,7 @@ static inline void __debug_switch_to_host_common(struct kvm_vcpu *vcpu)
 	if (!vcpu_get_flag(vcpu, DEBUG_DIRTY))
 		return;
 
-	host_ctxt = &this_cpu_ptr(&kvm_host_data)->host_ctxt;
+	host_ctxt = host_data_ptr(host_ctxt);
 	guest_ctxt = &vcpu->arch.ctxt;
 	host_dbg = &vcpu->arch.host_debug_state.regs;
 	guest_dbg = kern_hyp_va(vcpu->arch.debug_ptr);
arch/arm64/kvm/hyp/include/hyp/switch.h +4 -4
index e3fcf8c4d5b4..ae198b84ca01 100644
@@ -155,7 +155,7 @@ static inline bool cpu_has_amu(void)
 
 static inline void __activate_traps_hfgxtr(struct kvm_vcpu *vcpu)
 {
-	struct kvm_cpu_context *hctxt = &this_cpu_ptr(&kvm_host_data)->host_ctxt;
+	struct kvm_cpu_context *hctxt = host_data_ptr(host_ctxt);
 	struct kvm *kvm = kern_hyp_va(vcpu->kvm);
 
 	CHECK_FGT_MASKS(HFGRTR_EL2);
@@ -191,7 +191,7 @@ static inline void __activate_traps_hfgxtr(struct kvm_vcpu *vcpu)
 
 static inline void __deactivate_traps_hfgxtr(struct kvm_vcpu *vcpu)
 {
-	struct kvm_cpu_context *hctxt = &this_cpu_ptr(&kvm_host_data)->host_ctxt;
+	struct kvm_cpu_context *hctxt = host_data_ptr(host_ctxt);
 	struct kvm *kvm = kern_hyp_va(vcpu->kvm);
 
 	if (!cpus_have_final_cap(ARM64_HAS_FGT))
@@ -226,7 +226,7 @@ static inline void __activate_traps_common(struct kvm_vcpu *vcpu)
 
 		write_sysreg(0, pmselr_el0);
 
-		hctxt = &this_cpu_ptr(&kvm_host_data)->host_ctxt;
+		hctxt = host_data_ptr(host_ctxt);
 		ctxt_sys_reg(hctxt, PMUSERENR_EL0) = read_sysreg(pmuserenr_el0);
 		write_sysreg(ARMV8_PMU_USERENR_MASK, pmuserenr_el0);
 		vcpu_set_flag(vcpu, PMUSERENR_ON_CPU);
@@ -260,7 +260,7 @@ static inline void __deactivate_traps_common(struct kvm_vcpu *vcpu)
 	if (kvm_arm_support_pmu_v3()) {
 		struct kvm_cpu_context *hctxt;
 
-		hctxt = &this_cpu_ptr(&kvm_host_data)->host_ctxt;
+		hctxt = host_data_ptr(host_ctxt);
 		write_sysreg(ctxt_sys_reg(hctxt, PMUSERENR_EL0), pmuserenr_el0);
 		vcpu_clear_flag(vcpu, PMUSERENR_ON_CPU);
 	}
arch/arm64/kvm/hyp/nvhe/psci-relay.c +1 -1
index d57bcb6ab94d..dfe8fe0f7eaf 100644
@@ -205,7 +205,7 @@ asmlinkage void __noreturn __kvm_host_psci_cpu_entry(bool is_cpu_on)
 	struct psci_boot_args *boot_args;
 	struct kvm_cpu_context *host_ctxt;
 
-	host_ctxt = &this_cpu_ptr(&kvm_host_data)->host_ctxt;
+	host_ctxt = host_data_ptr(host_ctxt);
 
 	if (is_cpu_on)
 		boot_args = this_cpu_ptr(&cpu_on_args);
arch/arm64/kvm/hyp/nvhe/setup.c +1 -2
index bc58d1b515af..ae00dfa80801 100644
@@ -257,8 +257,7 @@ static int fix_hyp_pgtable_refcnt(void)
 
 void __noreturn __pkvm_init_finalise(void)
 {
-	struct kvm_host_data *host_data = this_cpu_ptr(&kvm_host_data);
-	struct kvm_cpu_context *host_ctxt = &host_data->host_ctxt;
+	struct kvm_cpu_context *host_ctxt = host_data_ptr(host_ctxt);
 	unsigned long nr_pages, reserved_pages, pfn;
 	int ret;
 
arch/arm64/kvm/hyp/nvhe/switch.c +2 -2
index c50f8459e4fc..544a419b9a39 100644
@@ -264,7 +264,7 @@ int __kvm_vcpu_run(struct kvm_vcpu *vcpu)
 		pmr_sync();
 	}
 
-	host_ctxt = &this_cpu_ptr(&kvm_host_data)->host_ctxt;
+	host_ctxt = host_data_ptr(host_ctxt);
 	host_ctxt->__hyp_running_vcpu = vcpu;
 	guest_ctxt = &vcpu->arch.ctxt;
 
@@ -367,7 +367,7 @@ asmlinkage void __noreturn hyp_panic(void)
 	struct kvm_cpu_context *host_ctxt;
 	struct kvm_vcpu *vcpu;
 
-	host_ctxt = &this_cpu_ptr(&kvm_host_data)->host_ctxt;
+	host_ctxt = host_data_ptr(host_ctxt);
 	vcpu = host_ctxt->__hyp_running_vcpu;
 
 	if (vcpu) {
arch/arm64/kvm/hyp/vhe/switch.c +2 -2
index 1581df6aec87..14b7a6bc5909 100644
@@ -221,7 +221,7 @@ static int __kvm_vcpu_run_vhe(struct kvm_vcpu *vcpu)
 	struct kvm_cpu_context *guest_ctxt;
 	u64 exit_code;
 
-	host_ctxt = &this_cpu_ptr(&kvm_host_data)->host_ctxt;
+	host_ctxt = host_data_ptr(host_ctxt);
 	host_ctxt->__hyp_running_vcpu = vcpu;
 	guest_ctxt = &vcpu->arch.ctxt;
 
@@ -306,7 +306,7 @@ static void __hyp_call_panic(u64 spsr, u64 elr, u64 par)
 	struct kvm_cpu_context *host_ctxt;
 	struct kvm_vcpu *vcpu;
 
-	host_ctxt = &this_cpu_ptr(&kvm_host_data)->host_ctxt;
+	host_ctxt = host_data_ptr(host_ctxt);
 	vcpu = host_ctxt->__hyp_running_vcpu;
 
 	__deactivate_traps(vcpu);
arch/arm64/kvm/hyp/vhe/sysreg-sr.c +2 -2
index a8b9ea496706..e12bd7d6d2dc 100644
@@ -67,7 +67,7 @@ void __vcpu_load_switch_sysregs(struct kvm_vcpu *vcpu)
 	struct kvm_cpu_context *guest_ctxt = &vcpu->arch.ctxt;
 	struct kvm_cpu_context *host_ctxt;
 
-	host_ctxt = &this_cpu_ptr(&kvm_host_data)->host_ctxt;
+	host_ctxt = host_data_ptr(host_ctxt);
 	__sysreg_save_user_state(host_ctxt);
 
 	/*
@@ -110,7 +110,7 @@ void __vcpu_put_switch_sysregs(struct kvm_vcpu *vcpu)
 	struct kvm_cpu_context *guest_ctxt = &vcpu->arch.ctxt;
 	struct kvm_cpu_context *host_ctxt;
 
-	host_ctxt = &this_cpu_ptr(&kvm_host_data)->host_ctxt;
+	host_ctxt = host_data_ptr(host_ctxt);
 
 	__sysreg_save_el1_state(guest_ctxt);
 	__sysreg_save_user_state(guest_ctxt);
arch/arm64/kvm/pmu.c +1 -1
index a243934c5568..329819806096 100644
@@ -232,7 +232,7 @@ bool kvm_set_pmuserenr(u64 val)
 	if (!vcpu || !vcpu_get_flag(vcpu, PMUSERENR_ON_CPU))
 		return false;
 
-	hctxt = &this_cpu_ptr(&kvm_host_data)->host_ctxt;
+	hctxt = host_data_ptr(host_ctxt);
 	ctxt_sys_reg(hctxt, PMUSERENR_EL0) = val;
 	return true;
 }
Signature · 1 line
2.39.2
reviewer Reviewed-by on v2 1/5
#2 lore raw

Re: [PATCH v2 1/5] KVM: arm64: Add accessor for per-CPU state

Hi Marc

Quoted 49 lines · Marc Zyngier

In order to facilitate the introduction of new per-CPU state, add a new host_data_ptr() helped that hides some of the per-CPU verbosity, and make it easier to move that state around in the future.

---
  arch/arm64/include/asm/kvm_host.h         | 37 +++++++++++++++++++++++
  arch/arm64/kvm/arm.c                      |  2 +-
  arch/arm64/kvm/hyp/include/hyp/debug-sr.h |  4 +--
  arch/arm64/kvm/hyp/include/hyp/switch.h   |  8 ++---
  arch/arm64/kvm/hyp/nvhe/psci-relay.c      |  2 +-
  arch/arm64/kvm/hyp/nvhe/setup.c           |  3 +-
  arch/arm64/kvm/hyp/nvhe/switch.c          |  4 +--
  arch/arm64/kvm/hyp/vhe/switch.c           |  4 +--
  arch/arm64/kvm/hyp/vhe/sysreg-sr.c        |  4 +--
  arch/arm64/kvm/pmu.c                      |  2 +-
  10 files changed, 53 insertions(+), 17 deletions(-)
arch/arm64/include/asm/kvm_host.h +15 -0
index 6883963bbc3a..ca6ef663950d 100644
@@ -530,6 +530,17 @@ struct kvm_cpu_context {
  	u64 *vncr_array;
  };
 
+/*
+ * This structure is instantiated on a per-CPU basis, and contains
+ * data that is:
+ *
+ * - tied to a single physical CPU, and
+ * - either have a lifetime that does not extend past vcpu_put()
+ * - or is an invariant for the lifetime of the system
+ *
+ * Use host_data_ptr(field) as a way to access a pointer to such a
+ * field.
+ */
  struct kvm_host_data {
  	struct kvm_cpu_context host_ctxt;
  };
@@ -1167,6 +1178,32 @@ struct kvm_vcpu *kvm_mpidr_to_vcpu(struct kvm *kvm, unsigned long mpidr);
 
  DECLARE_KVM_HYP_PER_CPU(struct kvm_host_data, kvm_host_data);
 
+/*
+ * How we access per-CPU host data depends on the where we access it from,
+ * and the mode we're in:
+ *

arch/arm64/include/asm/kvm_host.h struct kvm_vcpu *kvm_mpidr_to_vcpu(struct kvm *kvm, unsigned long mpidr); · line 1194

Quoted 10 lines
arch/arm64/include/asm/kvm_host.h +10 -0
+ * - VHE and nVHE hypervisor bits use their locally defined instance
+ *
+ * - the rest of the kernel use either the VHE or nVHE one, depending on
+ *   the mode we're running in.
+ *
+ *   Unless we're in protected mode, fully deprivileged, and the nVHE
+ *   per-CPU stuff is exclusively accessible to the protected EL2 code.
+ *   In this case, the EL1 code uses the *VHE* data as its private state
+ *   (which makes sense in a way as there shouldn't be any shared state
+ *   between the host and the hypervisor).

Does this mean we have a bug in cpu_hyp_init_context(), e.g. for a hotplugged CPU and needs to be fixed for stable ?

Eitherways,

Quoted 196 lines
arch/arm64/include/asm/kvm_host.h +12 -0
+ *
+ * Yes, this is all totally trivial. Shoot me now.
+ */
+#if defined(__KVM_NVHE_HYPERVISOR__) || defined(__KVM_VHE_HYPERVISOR__)
+#define host_data_ptr(f)	(&this_cpu_ptr(&kvm_host_data)->f)
+#else
+#define host_data_ptr(f)						\
+	(static_branch_unlikely(&kvm_protected_mode_initialized) ?	\
+	 &this_cpu_ptr(&kvm_host_data)->f :				\
+	 &this_cpu_ptr_hyp_sym(kvm_host_data)->f)
+#endif
+
  static inline void kvm_init_host_cpu_context(struct kvm_cpu_context *cpu_ctxt)
  {
  	/* The host's MPIDR is immutable, so let's set it up at boot time */
arch/arm64/kvm/arm.c +1 -1
index 3dee5490eea9..a24287c3ba99 100644
@@ -1971,7 +1971,7 @@ static void cpu_set_hyp_vector(void)
 
  static void cpu_hyp_init_context(void)
  {
-	kvm_init_host_cpu_context(&this_cpu_ptr_hyp_sym(kvm_host_data)->host_ctxt);
+	kvm_init_host_cpu_context(host_data_ptr(host_ctxt));
 
  	if (!is_kernel_in_hyp_mode())
  		cpu_init_hyp_mode();
arch/arm64/kvm/hyp/include/hyp/debug-sr.h +2 -2
index 961bbef104a6..eec0f8ccda56 100644
@@ -135,7 +135,7 @@ static inline void __debug_switch_to_guest_common(struct kvm_vcpu *vcpu)
  	if (!vcpu_get_flag(vcpu, DEBUG_DIRTY))
  		return;
 
-	host_ctxt = &this_cpu_ptr(&kvm_host_data)->host_ctxt;
+	host_ctxt = host_data_ptr(host_ctxt);
  	guest_ctxt = &vcpu->arch.ctxt;
  	host_dbg = &vcpu->arch.host_debug_state.regs;
  	guest_dbg = kern_hyp_va(vcpu->arch.debug_ptr);
@@ -154,7 +154,7 @@ static inline void __debug_switch_to_host_common(struct kvm_vcpu *vcpu)
  	if (!vcpu_get_flag(vcpu, DEBUG_DIRTY))
  		return;
 
-	host_ctxt = &this_cpu_ptr(&kvm_host_data)->host_ctxt;
+	host_ctxt = host_data_ptr(host_ctxt);
  	guest_ctxt = &vcpu->arch.ctxt;
  	host_dbg = &vcpu->arch.host_debug_state.regs;
  	guest_dbg = kern_hyp_va(vcpu->arch.debug_ptr);
arch/arm64/kvm/hyp/include/hyp/switch.h +4 -4
index e3fcf8c4d5b4..ae198b84ca01 100644
@@ -155,7 +155,7 @@ static inline bool cpu_has_amu(void)
 
  static inline void __activate_traps_hfgxtr(struct kvm_vcpu *vcpu)
  {
-	struct kvm_cpu_context *hctxt = &this_cpu_ptr(&kvm_host_data)->host_ctxt;
+	struct kvm_cpu_context *hctxt = host_data_ptr(host_ctxt);
  	struct kvm *kvm = kern_hyp_va(vcpu->kvm);
 
  	CHECK_FGT_MASKS(HFGRTR_EL2);
@@ -191,7 +191,7 @@ static inline void __activate_traps_hfgxtr(struct kvm_vcpu *vcpu)
 
  static inline void __deactivate_traps_hfgxtr(struct kvm_vcpu *vcpu)
  {
-	struct kvm_cpu_context *hctxt = &this_cpu_ptr(&kvm_host_data)->host_ctxt;
+	struct kvm_cpu_context *hctxt = host_data_ptr(host_ctxt);
  	struct kvm *kvm = kern_hyp_va(vcpu->kvm);
 
  	if (!cpus_have_final_cap(ARM64_HAS_FGT))
@@ -226,7 +226,7 @@ static inline void __activate_traps_common(struct kvm_vcpu *vcpu)
 
  		write_sysreg(0, pmselr_el0);
 
-		hctxt = &this_cpu_ptr(&kvm_host_data)->host_ctxt;
+		hctxt = host_data_ptr(host_ctxt);
  		ctxt_sys_reg(hctxt, PMUSERENR_EL0) = read_sysreg(pmuserenr_el0);
  		write_sysreg(ARMV8_PMU_USERENR_MASK, pmuserenr_el0);
  		vcpu_set_flag(vcpu, PMUSERENR_ON_CPU);
@@ -260,7 +260,7 @@ static inline void __deactivate_traps_common(struct kvm_vcpu *vcpu)
  	if (kvm_arm_support_pmu_v3()) {
  		struct kvm_cpu_context *hctxt;
 
-		hctxt = &this_cpu_ptr(&kvm_host_data)->host_ctxt;
+		hctxt = host_data_ptr(host_ctxt);
  		write_sysreg(ctxt_sys_reg(hctxt, PMUSERENR_EL0), pmuserenr_el0);
  		vcpu_clear_flag(vcpu, PMUSERENR_ON_CPU);
  	}
arch/arm64/kvm/hyp/nvhe/psci-relay.c +1 -1
index d57bcb6ab94d..dfe8fe0f7eaf 100644
@@ -205,7 +205,7 @@ asmlinkage void __noreturn __kvm_host_psci_cpu_entry(bool is_cpu_on)
  	struct psci_boot_args *boot_args;
  	struct kvm_cpu_context *host_ctxt;
 
-	host_ctxt = &this_cpu_ptr(&kvm_host_data)->host_ctxt;
+	host_ctxt = host_data_ptr(host_ctxt);
 
  	if (is_cpu_on)
  		boot_args = this_cpu_ptr(&cpu_on_args);
arch/arm64/kvm/hyp/nvhe/setup.c +1 -2
index bc58d1b515af..ae00dfa80801 100644
@@ -257,8 +257,7 @@ static int fix_hyp_pgtable_refcnt(void)
 
  void __noreturn __pkvm_init_finalise(void)
  {
-	struct kvm_host_data *host_data = this_cpu_ptr(&kvm_host_data);
-	struct kvm_cpu_context *host_ctxt = &host_data->host_ctxt;
+	struct kvm_cpu_context *host_ctxt = host_data_ptr(host_ctxt);
  	unsigned long nr_pages, reserved_pages, pfn;
  	int ret;
 
arch/arm64/kvm/hyp/nvhe/switch.c +2 -2
index c50f8459e4fc..544a419b9a39 100644
@@ -264,7 +264,7 @@ int __kvm_vcpu_run(struct kvm_vcpu *vcpu)
  		pmr_sync();
  	}
 
-	host_ctxt = &this_cpu_ptr(&kvm_host_data)->host_ctxt;
+	host_ctxt = host_data_ptr(host_ctxt);
  	host_ctxt->__hyp_running_vcpu = vcpu;
  	guest_ctxt = &vcpu->arch.ctxt;
 
@@ -367,7 +367,7 @@ asmlinkage void __noreturn hyp_panic(void)
  	struct kvm_cpu_context *host_ctxt;
  	struct kvm_vcpu *vcpu;
 
-	host_ctxt = &this_cpu_ptr(&kvm_host_data)->host_ctxt;
+	host_ctxt = host_data_ptr(host_ctxt);
  	vcpu = host_ctxt->__hyp_running_vcpu;
 
  	if (vcpu) {
arch/arm64/kvm/hyp/vhe/switch.c +2 -2
index 1581df6aec87..14b7a6bc5909 100644
@@ -221,7 +221,7 @@ static int __kvm_vcpu_run_vhe(struct kvm_vcpu *vcpu)
  	struct kvm_cpu_context *guest_ctxt;
  	u64 exit_code;
 
-	host_ctxt = &this_cpu_ptr(&kvm_host_data)->host_ctxt;
+	host_ctxt = host_data_ptr(host_ctxt);
  	host_ctxt->__hyp_running_vcpu = vcpu;
  	guest_ctxt = &vcpu->arch.ctxt;
 
@@ -306,7 +306,7 @@ static void __hyp_call_panic(u64 spsr, u64 elr, u64 par)
  	struct kvm_cpu_context *host_ctxt;
  	struct kvm_vcpu *vcpu;
 
-	host_ctxt = &this_cpu_ptr(&kvm_host_data)->host_ctxt;
+	host_ctxt = host_data_ptr(host_ctxt);
  	vcpu = host_ctxt->__hyp_running_vcpu;
 
  	__deactivate_traps(vcpu);
arch/arm64/kvm/hyp/vhe/sysreg-sr.c +2 -2
index a8b9ea496706..e12bd7d6d2dc 100644
@@ -67,7 +67,7 @@ void __vcpu_load_switch_sysregs(struct kvm_vcpu *vcpu)
  	struct kvm_cpu_context *guest_ctxt = &vcpu->arch.ctxt;
  	struct kvm_cpu_context *host_ctxt;
 
-	host_ctxt = &this_cpu_ptr(&kvm_host_data)->host_ctxt;
+	host_ctxt = host_data_ptr(host_ctxt);
  	__sysreg_save_user_state(host_ctxt);
 
  	/*
@@ -110,7 +110,7 @@ void __vcpu_put_switch_sysregs(struct kvm_vcpu *vcpu)
  	struct kvm_cpu_context *guest_ctxt = &vcpu->arch.ctxt;
  	struct kvm_cpu_context *host_ctxt;
 
-	host_ctxt = &this_cpu_ptr(&kvm_host_data)->host_ctxt;
+	host_ctxt = host_data_ptr(host_ctxt);
 
  	__sysreg_save_el1_state(guest_ctxt);
  	__sysreg_save_user_state(guest_ctxt);
arch/arm64/kvm/pmu.c +1 -1
index a243934c5568..329819806096 100644
@@ -232,7 +232,7 @@ bool kvm_set_pmuserenr(u64 val)
  	if (!vcpu || !vcpu_get_flag(vcpu, PMUSERENR_ON_CPU))
  		return false;
 
-	hctxt = &this_cpu_ptr(&kvm_host_data)->host_ctxt;
+	hctxt = host_data_ptr(host_ctxt);
  	ctxt_sys_reg(hctxt, PMUSERENR_EL0) = val;
  	return true;
  }
author maintainer
#3 lore raw

PATCH v2 2/5 KVM: arm64: Exclude host_debug_data from vcpu_arch

In reply to Marc Zyngier

Keeping host_debug_state on a per-vcpu basis is completely pointless. The lifetime of this data is only that of the inner run-loop, which means it is never accessed outside of the core EL2 code.

Move the structure into kvm_host_data, and save over 500 bytes per vcpu.

---
 arch/arm64/include/asm/kvm_host.h         | 31 +++++++++++++----------
 arch/arm64/kvm/hyp/include/hyp/debug-sr.h |  4 +--
 arch/arm64/kvm/hyp/nvhe/debug-sr.c        |  8 +++---
 3 files changed, 23 insertions(+), 20 deletions(-)
arch/arm64/include/asm/kvm_host.h +17 -14
index ca6ef663950d..8c149e4ae99d 100644
@@ -543,6 +543,19 @@ struct kvm_cpu_context {
  */
 struct kvm_host_data {
 	struct kvm_cpu_context host_ctxt;
+
+	/*
+	 * host_debug_state contains the host registers which are
+	 * saved and restored during world switches.
+	 */
+	 struct {
+		/* {Break,watch}point registers */
+		struct kvm_guest_debug_arch regs;
+		/* Statistical profiling extension */
+		u64 pmscr_el1;
+		/* Self-hosted trace */
+		u64 trfcr_el1;
+	} host_debug_state;
 };
 
 struct kvm_host_psci_config {
@@ -637,11 +650,10 @@ struct kvm_vcpu_arch {
 	 * We maintain more than a single set of debug registers to support
 	 * debugging the guest from the host and to maintain separate host and
 	 * guest state during world switches. vcpu_debug_state are the debug
-	 * registers of the vcpu as the guest sees them.  host_debug_state are
-	 * the host registers which are saved and restored during
-	 * world switches. external_debug_state contains the debug
-	 * values we want to debug the guest. This is set via the
-	 * KVM_SET_GUEST_DEBUG ioctl.
+	 * registers of the vcpu as the guest sees them.
+	 *
+	 * external_debug_state contains the debug values we want to debug the
+	 * guest. This is set via the KVM_SET_GUEST_DEBUG ioctl.
 	 *
 	 * debug_ptr points to the set of debug registers that should be loaded
 	 * onto the hardware when running the guest.
@@ -653,15 +665,6 @@ struct kvm_vcpu_arch {
 	struct user_fpsimd_state *host_fpsimd_state;	/* hyp VA */
 	struct task_struct *parent_task;
 
-	struct {
-		/* {Break,watch}point registers */
-		struct kvm_guest_debug_arch regs;
-		/* Statistical profiling extension */
-		u64 pmscr_el1;
-		/* Self-hosted trace */
-		u64 trfcr_el1;
-	} host_debug_state;
-
 	/* VGIC state */
 	struct vgic_cpu vgic_cpu;
 	struct arch_timer_cpu timer_cpu;
arch/arm64/kvm/hyp/include/hyp/debug-sr.h +2 -2
index eec0f8ccda56..d00093699aaf 100644
@@ -137,7 +137,7 @@ static inline void __debug_switch_to_guest_common(struct kvm_vcpu *vcpu)
 
 	host_ctxt = host_data_ptr(host_ctxt);
 	guest_ctxt = &vcpu->arch.ctxt;
-	host_dbg = &vcpu->arch.host_debug_state.regs;
+	host_dbg = host_data_ptr(host_debug_state.regs);
 	guest_dbg = kern_hyp_va(vcpu->arch.debug_ptr);
 
 	__debug_save_state(host_dbg, host_ctxt);
@@ -156,7 +156,7 @@ static inline void __debug_switch_to_host_common(struct kvm_vcpu *vcpu)
 
 	host_ctxt = host_data_ptr(host_ctxt);
 	guest_ctxt = &vcpu->arch.ctxt;
-	host_dbg = &vcpu->arch.host_debug_state.regs;
+	host_dbg = host_data_ptr(host_debug_state.regs);
 	guest_dbg = kern_hyp_va(vcpu->arch.debug_ptr);
 
 	__debug_save_state(guest_dbg, guest_ctxt);
arch/arm64/kvm/hyp/nvhe/debug-sr.c +4 -4
index 7746ea507b6f..53efda0235cf 100644
@@ -83,10 +83,10 @@ void __debug_save_host_buffers_nvhe(struct kvm_vcpu *vcpu)
 {
 	/* Disable and flush SPE data generation */
 	if (vcpu_get_flag(vcpu, DEBUG_STATE_SAVE_SPE))
-		__debug_save_spe(&vcpu->arch.host_debug_state.pmscr_el1);
+		__debug_save_spe(host_data_ptr(host_debug_state.pmscr_el1));
 	/* Disable and flush Self-Hosted Trace generation */
 	if (vcpu_get_flag(vcpu, DEBUG_STATE_SAVE_TRBE))
-		__debug_save_trace(&vcpu->arch.host_debug_state.trfcr_el1);
+		__debug_save_trace(host_data_ptr(host_debug_state.trfcr_el1));
 }
 
 void __debug_switch_to_guest(struct kvm_vcpu *vcpu)
@@ -97,9 +97,9 @@ void __debug_switch_to_guest(struct kvm_vcpu *vcpu)
 void __debug_restore_host_buffers_nvhe(struct kvm_vcpu *vcpu)
 {
 	if (vcpu_get_flag(vcpu, DEBUG_STATE_SAVE_SPE))
-		__debug_restore_spe(vcpu->arch.host_debug_state.pmscr_el1);
+		__debug_restore_spe(*host_data_ptr(host_debug_state.pmscr_el1));
 	if (vcpu_get_flag(vcpu, DEBUG_STATE_SAVE_TRBE))
-		__debug_restore_trace(vcpu->arch.host_debug_state.trfcr_el1);
+		__debug_restore_trace(*host_data_ptr(host_debug_state.trfcr_el1));
 }
 
 void __debug_switch_to_host(struct kvm_vcpu *vcpu)
Signature · 1 line
2.39.2
reviewer Reviewed-by on v2 2/5
#4 lore raw

Re: [PATCH v2 2/5] KVM: arm64: Exclude host_debug_data from vcpu_arch

Quoted 9 lines · Marc Zyngier

Keeping host_debug_state on a per-vcpu basis is completely pointless. The lifetime of this data is only that of the inner run-loop, which means it is never accessed outside of the core EL2 code.

Move the structure into kvm_host_data, and save over 500 bytes per vcpu.

author maintainer
#5 lore raw

PATCH v2 3/5 KVM: arm64: Exclude mdcr_el2_host from kvm_vcpu_arch

In reply to Marc Zyngier

As for the rest of the host debug state, the host copy of mdcr_el2 has little to do in the vcpu, and is better placed in the host_data structure.

---
 arch/arm64/include/asm/kvm_host.h       | 5 ++---
 arch/arm64/kvm/hyp/include/hyp/switch.h | 4 ++--
 2 files changed, 4 insertions(+), 5 deletions(-)
arch/arm64/include/asm/kvm_host.h +2 -3
index 8c149e4ae99d..590e8767b720 100644
@@ -555,6 +555,8 @@ struct kvm_host_data {
 		u64 pmscr_el1;
 		/* Self-hosted trace */
 		u64 trfcr_el1;
+		/* Values of trap registers for the host before guest entry. */
+		u64 mdcr_el2;
 	} host_debug_state;
 };
 
@@ -615,9 +617,6 @@ struct kvm_vcpu_arch {
 	u64 mdcr_el2;
 	u64 cptr_el2;
 
-	/* Values of trap registers for the host before guest entry. */
-	u64 mdcr_el2_host;
-
 	/* Exception Information */
 	struct kvm_vcpu_fault_info fault;
 
arch/arm64/kvm/hyp/include/hyp/switch.h +2 -2
index ae198b84ca01..7d7de0245ed0 100644
@@ -232,7 +232,7 @@ static inline void __activate_traps_common(struct kvm_vcpu *vcpu)
 		vcpu_set_flag(vcpu, PMUSERENR_ON_CPU);
 	}
 
-	vcpu->arch.mdcr_el2_host = read_sysreg(mdcr_el2);
+	*host_data_ptr(host_debug_state.mdcr_el2) = read_sysreg(mdcr_el2);
 	write_sysreg(vcpu->arch.mdcr_el2, mdcr_el2);
 
 	if (cpus_have_final_cap(ARM64_HAS_HCX)) {
@@ -254,7 +254,7 @@ static inline void __activate_traps_common(struct kvm_vcpu *vcpu)
 
 static inline void __deactivate_traps_common(struct kvm_vcpu *vcpu)
 {
-	write_sysreg(vcpu->arch.mdcr_el2_host, mdcr_el2);
+	write_sysreg(*host_data_ptr(host_debug_state.mdcr_el2), mdcr_el2);
 
 	write_sysreg(0, hstr_el2);
 	if (kvm_arm_support_pmu_v3()) {
Signature · 1 line
2.39.2
author maintainer
#7 lore raw

PATCH v2 4/5 KVM: arm64: Exclude host_fpsimd_state pointer from kvm_vcpu_arch

In reply to Marc Zyngier

As the name of the field indicates, host_fpsimd_state is strictly a host piece of data, and we reset this pointer on each PID change.

So let's move it where it belongs, and set it at load-time. Although this is slightly more often, it is a well defined life-cycle which matches other pieces of data.

---
 arch/arm64/include/asm/kvm_host.h       | 2 +-
 arch/arm64/kvm/fpsimd.c                 | 3 +--
 arch/arm64/kvm/hyp/include/hyp/switch.h | 2 +-
 arch/arm64/kvm/hyp/nvhe/hyp-main.c      | 1 -
 4 files changed, 3 insertions(+), 5 deletions(-)
arch/arm64/include/asm/kvm_host.h +1 -1
index 590e8767b720..838cdee2ecf7 100644
@@ -543,6 +543,7 @@ struct kvm_cpu_context {
  */
 struct kvm_host_data {
 	struct kvm_cpu_context host_ctxt;
+	struct user_fpsimd_state *fpsimd_state;	/* hyp VA */
 
 	/*
 	 * host_debug_state contains the host registers which are
@@ -661,7 +662,6 @@ struct kvm_vcpu_arch {
 	struct kvm_guest_debug_arch vcpu_debug_state;
 	struct kvm_guest_debug_arch external_debug_state;
 
-	struct user_fpsimd_state *host_fpsimd_state;	/* hyp VA */
 	struct task_struct *parent_task;
 
 	/* VGIC state */
arch/arm64/kvm/fpsimd.c +1 -2
index 571cf6eef1e1..e6bd99358615 100644
@@ -49,8 +49,6 @@ int kvm_arch_vcpu_run_map_fp(struct kvm_vcpu *vcpu)
 	if (ret)
 		return ret;
 
-	vcpu->arch.host_fpsimd_state = kern_hyp_va(fpsimd);
-
 	/*
 	 * We need to keep current's task_struct pinned until its data has been
 	 * unshared with the hypervisor to make sure it is not re-used by the
@@ -87,6 +85,7 @@ void kvm_arch_vcpu_load_fp(struct kvm_vcpu *vcpu)
 	 * FP_STATE_FREE if the flag set.
 	 */
 	vcpu->arch.fp_state = FP_STATE_HOST_OWNED;
+	*host_data_ptr(fpsimd_state) = kern_hyp_va(&current->thread.uw.fpsimd_state);
 
 	vcpu_clear_flag(vcpu, HOST_SVE_ENABLED);
 	if (read_sysreg(cpacr_el1) & CPACR_EL1_ZEN_EL0EN)
arch/arm64/kvm/hyp/include/hyp/switch.h +1 -1
index 7d7de0245ed0..6def6ad8dd48 100644
@@ -377,7 +377,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 (vcpu->arch.fp_state == FP_STATE_HOST_OWNED)
-		__fpsimd_save_state(vcpu->arch.host_fpsimd_state);
+		__fpsimd_save_state(*host_data_ptr(fpsimd_state));
 
 	/* Restore the guest state */
 	if (sve_guest)
arch/arm64/kvm/hyp/nvhe/hyp-main.c +0 -1
index 2385fd03ed87..c5f625dc1f07 100644
@@ -42,7 +42,6 @@ static void flush_hyp_vcpu(struct pkvm_hyp_vcpu *hyp_vcpu)
 	hyp_vcpu->vcpu.arch.fp_state	= host_vcpu->arch.fp_state;
 
 	hyp_vcpu->vcpu.arch.debug_ptr	= kern_hyp_va(host_vcpu->arch.debug_ptr);
-	hyp_vcpu->vcpu.arch.host_fpsimd_state = host_vcpu->arch.host_fpsimd_state;
 
 	hyp_vcpu->vcpu.arch.vsesr_el2	= host_vcpu->arch.vsesr_el2;
 
Signature · 1 line
2.39.2
author maintainer
#8 lore raw

PATCH v2 5/5 KVM: arm64: Exclude FP ownership from kvm_vcpu_arch

In reply to Marc Zyngier

In retrospect, it is fairly obvious that the FP state ownership is only meaningful for a given CPU, and that locating this information in the vcpu was just a mistake.

Move the ownership tracking into the host data structure, and rename it from fp_state to fp_owner, which is a better description (name suggested by Mark Brown).

---
 arch/arm64/include/asm/kvm_emulate.h    |  4 ++--
 arch/arm64/include/asm/kvm_host.h       | 14 +++++++-------
 arch/arm64/kvm/arm.c                    |  6 ------
 arch/arm64/kvm/fpsimd.c                 | 10 +++++-----
 arch/arm64/kvm/hyp/include/hyp/switch.h |  6 +++---
 arch/arm64/kvm/hyp/nvhe/hyp-main.c      |  2 --
 arch/arm64/kvm/hyp/nvhe/switch.c        |  2 +-
 arch/arm64/kvm/hyp/vhe/switch.c         |  2 +-
 8 files changed, 19 insertions(+), 27 deletions(-)
arch/arm64/include/asm/kvm_emulate.h +2 -2
index debc3753d2ef..b17f2269fb81 100644
@@ -594,7 +594,7 @@ static __always_inline u64 kvm_get_reset_cptr_el2(struct kvm_vcpu *vcpu)
 		val = (CPACR_EL1_FPEN_EL0EN | CPACR_EL1_FPEN_EL1EN);
 
 		if (!vcpu_has_sve(vcpu) ||
-		    (vcpu->arch.fp_state != FP_STATE_GUEST_OWNED))
+		    (*host_data_ptr(fp_owner) != FP_STATE_GUEST_OWNED))
 			val |= CPACR_EL1_ZEN_EL1EN | CPACR_EL1_ZEN_EL0EN;
 		if (cpus_have_final_cap(ARM64_SME))
 			val |= CPACR_EL1_SMEN_EL1EN | CPACR_EL1_SMEN_EL0EN;
@@ -602,7 +602,7 @@ static __always_inline u64 kvm_get_reset_cptr_el2(struct kvm_vcpu *vcpu)
 		val = CPTR_NVHE_EL2_RES1;
 
 		if (vcpu_has_sve(vcpu) &&
-		    (vcpu->arch.fp_state == FP_STATE_GUEST_OWNED))
+		    (*host_data_ptr(fp_owner) == FP_STATE_GUEST_OWNED))
 			val |= CPTR_EL2_TZ;
 		if (cpus_have_final_cap(ARM64_SME))
 			val &= ~CPTR_EL2_TSM;
arch/arm64/include/asm/kvm_host.h +7 -7
index 838cdee2ecf7..951303e976de 100644
@@ -545,6 +545,13 @@ struct kvm_host_data {
 	struct kvm_cpu_context host_ctxt;
 	struct user_fpsimd_state *fpsimd_state;	/* hyp VA */
 
+	/* Ownership of the FP regs */
+	enum {
+		FP_STATE_FREE,
+		FP_STATE_HOST_OWNED,
+		FP_STATE_GUEST_OWNED,
+	} fp_owner;
+
 	/*
 	 * host_debug_state contains the host registers which are
 	 * saved and restored during world switches.
@@ -621,13 +628,6 @@ struct kvm_vcpu_arch {
 	/* Exception Information */
 	struct kvm_vcpu_fault_info fault;
 
-	/* Ownership of the FP regs */
-	enum {
-		FP_STATE_FREE,
-		FP_STATE_HOST_OWNED,
-		FP_STATE_GUEST_OWNED,
-	} fp_state;
-
 	/* Configuration flags, set once and for all before the vcpu can run */
 	u8 cflags;
 
arch/arm64/kvm/arm.c +0 -6
index a24287c3ba99..66d8112da268 100644
@@ -378,12 +378,6 @@ int kvm_arch_vcpu_create(struct kvm_vcpu *vcpu)
 
 	vcpu->arch.mmu_page_cache.gfp_zero = __GFP_ZERO;
 
-	/*
-	 * Default value for the FP state, will be overloaded at load
-	 * time if we support FP (pretty likely)
-	 */
-	vcpu->arch.fp_state = FP_STATE_FREE;
-
 	/* Set up the timer */
 	kvm_timer_vcpu_init(vcpu);
 
arch/arm64/kvm/fpsimd.c +5 -5
index e6bd99358615..98f8b9272e24 100644
@@ -84,7 +84,7 @@ void kvm_arch_vcpu_load_fp(struct kvm_vcpu *vcpu)
 	 * guest in kvm_arch_vcpu_ctxflush_fp() and override this to
 	 * FP_STATE_FREE if the flag set.
 	 */
-	vcpu->arch.fp_state = FP_STATE_HOST_OWNED;
+	*host_data_ptr(fp_owner) = FP_STATE_HOST_OWNED;
 	*host_data_ptr(fpsimd_state) = kern_hyp_va(&current->thread.uw.fpsimd_state);
 
 	vcpu_clear_flag(vcpu, HOST_SVE_ENABLED);
@@ -109,7 +109,7 @@ void kvm_arch_vcpu_load_fp(struct kvm_vcpu *vcpu)
 		 * been saved, this is very unlikely to happen.
 		 */
 		if (read_sysreg_s(SYS_SVCR) & (SVCR_SM_MASK | SVCR_ZA_MASK)) {
-			vcpu->arch.fp_state = FP_STATE_FREE;
+			*host_data_ptr(fp_owner) = FP_STATE_FREE;
 			fpsimd_save_and_flush_cpu_state();
 		}
 	}
@@ -125,7 +125,7 @@ void kvm_arch_vcpu_load_fp(struct kvm_vcpu *vcpu)
 void kvm_arch_vcpu_ctxflush_fp(struct kvm_vcpu *vcpu)
 {
 	if (test_thread_flag(TIF_FOREIGN_FPSTATE))
-		vcpu->arch.fp_state = FP_STATE_FREE;
+		*host_data_ptr(fp_owner) = FP_STATE_FREE;
 }
 
 /*
@@ -141,7 +141,7 @@ void kvm_arch_vcpu_ctxsync_fp(struct kvm_vcpu *vcpu)
 
 	WARN_ON_ONCE(!irqs_disabled());
 
-	if (vcpu->arch.fp_state == FP_STATE_GUEST_OWNED) {
+	if (*host_data_ptr(fp_owner) == FP_STATE_GUEST_OWNED) {
 
 		/*
 		 * Currently we do not support SME guests so SVCR is
@@ -194,7 +194,7 @@ void kvm_arch_vcpu_put_fp(struct kvm_vcpu *vcpu)
 		isb();
 	}
 
-	if (vcpu->arch.fp_state == FP_STATE_GUEST_OWNED) {
+	if (*host_data_ptr(fp_owner) == FP_STATE_GUEST_OWNED) {
 		if (vcpu_has_sve(vcpu)) {
 			__vcpu_sys_reg(vcpu, ZCR_EL1) = read_sysreg_el1(SYS_ZCR);
 
arch/arm64/kvm/hyp/include/hyp/switch.h +3 -3
index 6def6ad8dd48..2629420d0659 100644
@@ -42,7 +42,7 @@ extern struct kvm_exception_table_entry __stop___kvm_ex_table;
 /* Check whether the FP regs are owned by the guest */
 static inline bool guest_owns_fp_regs(struct kvm_vcpu *vcpu)
 {
-	return vcpu->arch.fp_state == FP_STATE_GUEST_OWNED;
+	return *host_data_ptr(fp_owner) == FP_STATE_GUEST_OWNED;
 }
 
 /* Save the 32-bit only FPSIMD system register state */
@@ -376,7 +376,7 @@ static bool kvm_hyp_handle_fpsimd(struct kvm_vcpu *vcpu, u64 *exit_code)
 	isb();
 
 	/* Write out the host state if it's in the registers */
-	if (vcpu->arch.fp_state == FP_STATE_HOST_OWNED)
+	if (*host_data_ptr(fp_owner) == FP_STATE_HOST_OWNED)
 		__fpsimd_save_state(*host_data_ptr(fpsimd_state));
 
 	/* Restore the guest state */
@@ -389,7 +389,7 @@ static bool kvm_hyp_handle_fpsimd(struct kvm_vcpu *vcpu, u64 *exit_code)
 	if (!(read_sysreg(hcr_el2) & HCR_RW))
 		write_sysreg(__vcpu_sys_reg(vcpu, FPEXC32_EL2), fpexc32_el2);
 
-	vcpu->arch.fp_state = FP_STATE_GUEST_OWNED;
+	*host_data_ptr(fp_owner) = FP_STATE_GUEST_OWNED;
 
 	return true;
 }
arch/arm64/kvm/hyp/nvhe/hyp-main.c +0 -2
index c5f625dc1f07..26561c562f7a 100644
@@ -39,7 +39,6 @@ static void flush_hyp_vcpu(struct pkvm_hyp_vcpu *hyp_vcpu)
 	hyp_vcpu->vcpu.arch.cptr_el2	= host_vcpu->arch.cptr_el2;
 
 	hyp_vcpu->vcpu.arch.iflags	= host_vcpu->arch.iflags;
-	hyp_vcpu->vcpu.arch.fp_state	= host_vcpu->arch.fp_state;
 
 	hyp_vcpu->vcpu.arch.debug_ptr	= kern_hyp_va(host_vcpu->arch.debug_ptr);
 
@@ -63,7 +62,6 @@ static void sync_hyp_vcpu(struct pkvm_hyp_vcpu *hyp_vcpu)
 	host_vcpu->arch.fault		= hyp_vcpu->vcpu.arch.fault;
 
 	host_vcpu->arch.iflags		= hyp_vcpu->vcpu.arch.iflags;
-	host_vcpu->arch.fp_state	= hyp_vcpu->vcpu.arch.fp_state;
 
 	host_cpu_if->vgic_hcr		= hyp_cpu_if->vgic_hcr;
 	for (i = 0; i < hyp_cpu_if->used_lrs; ++i)
arch/arm64/kvm/hyp/nvhe/switch.c +1 -1
index 544a419b9a39..1f82d531a494 100644
@@ -337,7 +337,7 @@ int __kvm_vcpu_run(struct kvm_vcpu *vcpu)
 
 	__sysreg_restore_state_nvhe(host_ctxt);
 
-	if (vcpu->arch.fp_state == FP_STATE_GUEST_OWNED)
+	if (*host_data_ptr(fp_owner) == FP_STATE_GUEST_OWNED)
 		__fpsimd_save_fpexc32(vcpu);
 
 	__debug_switch_to_host(vcpu);
arch/arm64/kvm/hyp/vhe/switch.c +1 -1
index 14b7a6bc5909..b92f9fe2d50e 100644
@@ -258,7 +258,7 @@ static int __kvm_vcpu_run_vhe(struct kvm_vcpu *vcpu)
 
 	sysreg_restore_host_state_vhe(host_ctxt);
 
-	if (vcpu->arch.fp_state == FP_STATE_GUEST_OWNED)
+	if (*host_data_ptr(fp_owner) == FP_STATE_GUEST_OWNED)
 		__fpsimd_save_fpexc32(vcpu);
 
 	__debug_switch_to_host(vcpu);
Signature · 1 line
2.39.2
#9 lore raw

Re: [PATCH v2 5/5] KVM: arm64: Exclude FP ownership from kvm_vcpu_arch

Quoted 7 lines · Marc Zyngier

In retrospect, it is fairly obvious that the FP state ownership is only meaningful for a given CPU, and that locating this information in the vcpu was just a mistake.

Move the ownership tracking into the host data structure, and rename it from fp_state to fp_owner, which is a better description (name suggested by Mark Brown).

There's still the thing with the interaction with SME support - to summarise what I think you're asking for the userspace ABI there:

- Create a requirement for userspace to set SVCR prior to setting any vector impacted register to ensure the correct format and that data isn't zeroed when SVCR is set.
- Use the value of SVCR.SM and the guest maximum SVE and SME VLs to select the currently visible vector length for the Z, P and FFR registers, and if FFR can be accessed if not available in streaming mode.
- Changes to SVCR.SM zero register data in the same way writes to the physical register do.
- This also implies discarding or failing all writes to ZA and ZT0 unless SVCR.ZA is set for consistency.
- Add support for the V registers in the sysreg interface when SVE is enabled.

then the implementation can do what it likes to achieve that, the most obvious thing being to store in native format for the current hardware mode based on SVCR.{SM,ZA}. Does that sound about right?

author maintainer
#10 lore raw

Re: [PATCH v2 5/5] KVM: arm64: Exclude FP ownership from kvm_vcpu_arch

Quoted 6 lines · Mark Brown
Quoted 4 lines · Marc Zyngier

In retrospect, it is fairly obvious that the FP state ownership is only meaningful for a given CPU, and that locating this information in the vcpu was just a mistake.

Quoted 6 lines
Quoted 3 lines

Move the ownership tracking into the host data structure, and rename it from fp_state to fp_owner, which is a better description (name suggested by Mark Brown).

There's still the thing with the interaction with SME support - to summarise what I think you're asking for the userspace ABI there:

Well, the SME support is still pretty prospective, and this patch has no impact on an existing ABI.

Quoted 12 lines

- Create a requirement for userspace to set SVCR prior to setting any vector impacted register to ensure the correct format and that data isn't zeroed when SVCR is set.
- Use the value of SVCR.SM and the guest maximum SVE and SME VLs to select the currently visible vector length for the Z, P and FFR registers, and if FFR can be accessed if not available in streaming mode.
- Changes to SVCR.SM zero register data in the same way writes to the physical register do.
- This also implies discarding or failing all writes to ZA and ZT0 unless SVCR.ZA is set for consistency.

All of that seems reasonable, as long as it is comes as a consequence of enabling SME. It should be run by the QEMU people though, as they are the ones that will make use of it. Please Cc them when you post the patches or even better, reach out to them beforehand.

Quoted 2 lines

- Add support for the V registers in the sysreg interface when SVE is enabled.

We already support the V registers with KVM_REG_ARM_CORE_REG(). Why would you add any new interface for this? The kernel should be perfectly capable of dealing with the placement of the data in the internal structures, and there is no need to tie the userspace ABI to how we deal with that placement (kvm_regs is already purely userspace).

Quoted 3 lines

then the implementation can do what it likes to achieve that, the most obvious thing being to store in native format for the current hardware mode based on SVCR.{SM,ZA}. Does that sound about right?

Apart from the statement about the V registers, this seems OK. But again, I want to see this agreed with the QEMU folks.

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

Re: [PATCH v2 5/5] KVM: arm64: Exclude FP ownership from kvm_vcpu_arch

Quoted 7 lines · Marc Zyngier
Quoted 2 lines · Mark Brown

There's still the thing with the interaction with SME support - to summarise what I think you're asking for the userspace ABI there:

Well, the SME support is still pretty prospective, and this patch has no impact on an existing ABI.

Sure, hopefully I should have a new verison out this release - it was mostly held up waiting for the ID register parsing framework you got merged last release. Though holidays do make things a bit tight timing wise.

Quoted 9 lines
Quoted 2 lines

- Add support for the V registers in the sysreg interface when SVE is enabled.

We already support the V registers with KVM_REG_ARM_CORE_REG(). Why would you add any new interface for this? The kernel should be perfectly capable of dealing with the placement of the data in the internal structures, and there is no need to tie the userspace ABI to how we deal with that placement (kvm_regs is already purely userspace).

This was referring to the fact that currently when SVE is enabled access to the V registers as V registers via _CORE_REG() is blocked and they can only be accessed as a subset of the Z registers (see the check at the end of core_reg_size_from_offset() in guest.c).

Quoted 6 lines
Quoted 3 lines

then the implementation can do what it likes to achieve that, the most obvious thing being to store in native format for the current hardware mode based on SVCR.{SM,ZA}. Does that sound about right?

Apart from the statement about the V registers, this seems OK. But again, I want to see this agreed with the QEMU folks.

Great, thanks.

author maintainer
#12 lore raw

Re: [PATCH v2 5/5] KVM: arm64: Exclude FP ownership from kvm_vcpu_arch

Quoted 11 lines · Mark Brown
Quoted 10 lines
Quoted 3 lines

- Add support for the V registers in the sysreg interface when SVE is enabled.

We already support the V registers with KVM_REG_ARM_CORE_REG(). Why would you add any new interface for this? The kernel should be perfectly capable of dealing with the placement of the data in the internal structures, and there is no need to tie the userspace ABI to how we deal with that placement (kvm_regs is already purely userspace).

Quoted 4 lines

This was referring to the fact that currently when SVE is enabled access to the V registers as V registers via _CORE_REG() is blocked and they can only be accessed as a subset of the Z registers (see the check at the end of core_reg_size_from_offset() in guest.c).

But what behaviour do you expect from allowing such a write? Insert in place? Or zero the upper bits of the vector, as per R_WKYLB? One is wrong, and the other wrecks havoc on unsuspecting userspace.

My take on this is that when a VM is S*E aware, only the writes to the largest *enabled* registers should take place. This is similar to what we do for FP/SIMD: we only allow writes to the V registers, and not to Q, D, S, H or B, although that happens by construction. For S*E, dropping the write on the floor (or return some error that userspace will understand as benign) is the least bad option.

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

Re: [PATCH v2 5/5] KVM: arm64: Exclude FP ownership from kvm_vcpu_arch

Quoted 10 lines · Marc Zyngier
Quoted 4 lines · Mark Brown

This was referring to the fact that currently when SVE is enabled access to the V registers as V registers via _CORE_REG() is blocked and they can only be accessed as a subset of the Z registers (see the check at the end of core_reg_size_from_offset() in guest.c).

But what behaviour do you expect from allowing such a write? Insert in place? Or zero the upper bits of the vector, as per R_WKYLB? One is wrong, and the other wrecks havoc on unsuspecting userspace.

It would have to be the former due to the ABI issue I think.

Quoted 6 lines

My take on this is that when a VM is S*E aware, only the writes to the largest *enabled* registers should take place. This is similar to what we do for FP/SIMD: we only allow writes to the V registers, and not to Q, D, S, H or B, although that happens by construction. For S*E, dropping the write on the floor (or return some error that userspace will understand as benign) is the least bad option.

OK, this does mean that in the case of a SME only guest we'll end up with registers not just changing size but appearing and disappearing depending on SVCR.SM. It wasn't clear to me that this was a good idea from an ABI point of view, it's a level up beyond the size changing thing and there's a tradeoff with the "model what the architecture does" model.

author maintainer
#14 lore raw

Re: [PATCH v2 5/5] KVM: arm64: Exclude FP ownership from kvm_vcpu_arch

Quoted 10 lines · Mark Brown

[1 <text/plain; us-ascii (quoted-printable)>]

Quoted 6 lines · Marc Zyngier
Quoted 4 lines · Mark Brown

This was referring to the fact that currently when SVE is enabled access to the V registers as V registers via _CORE_REG() is blocked and they can only be accessed as a subset of the Z registers (see the check at the end of core_reg_size_from_offset() in guest.c).

Quoted 5 lines
Quoted 3 lines

But what behaviour do you expect from allowing such a write? Insert in place? Or zero the upper bits of the vector, as per R_WKYLB? One is wrong, and the other wrecks havoc on unsuspecting userspace.

It would have to be the former due to the ABI issue I think.

No, that's an architecture violation.

Quoted 7 lines
Quoted 6 lines

My take on this is that when a VM is S*E aware, only the writes to the largest *enabled* registers should take place. This is similar to what we do for FP/SIMD: we only allow writes to the V registers, and not to Q, D, S, H or B, although that happens by construction. For S*E, dropping the write on the floor (or return some error that userspace will understand as benign) is the least bad option.

Quoted 6 lines

OK, this does mean that in the case of a SME only guest we'll end up with registers not just changing size but appearing and disappearing depending on SVCR.SM. It wasn't clear to me that this was a good idea from an ABI point of view, it's a level up beyond the size changing thing and there's a tradeoff with the "model what the architecture does" model.

The registers don't have to disappear, they just have to become WI, just like it is the case today with SVE. Yes, the ABI becomes contextual, but we're past the point where we can treat the various register files as a bag of bits that can be save/restored without any ordering.

We already have similar requirements for complex parts of KVM where ordering is required, such as GICv3 and the ITS, and we follow what the architecture requires. The same thing applies for the CPU.

	M.
Signature · 1 line
Without deviation from the norm, progress is not possible.
Reviewed-by on v2 5/5
#15 lore raw

Re: [PATCH v2 5/5] KVM: arm64: Exclude FP ownership from kvm_vcpu_arch

In reply to Marc Zyngier
Quoted 7 lines · Marc Zyngier

In retrospect, it is fairly obvious that the FP state ownership is only meaningful for a given CPU, and that locating this information in the vcpu was just a mistake.

Move the ownership tracking into the host data structure, and rename it from fp_state to fp_owner, which is a better description (name suggested by Mark Brown).