kvmarm.lists.cs.columbia.edu archive mirror
 help / color / mirror / Atom feed
* [PATCH v2 0/3] KVM: arm64: Floating point documentation updates and code tweaks
@ 2023-03-07 17:37 Mark Brown
  2023-03-07 17:37 ` [PATCH v2 1/3] KVM: arm64: Document check for TIF_FOREIGN_FPSTATE Mark Brown
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: Mark Brown @ 2023-03-07 17:37 UTC (permalink / raw)
  To: Marc Zyngier, James Morse, Alexandru Elisei, Suzuki K Poulose,
	Oliver Upton, Catalin Marinas, Will Deacon
  Cc: linux-arm-kernel, kvmarm, Mark Brown

This series has some improvements to the documentation and code for the
KVM floating point code, the main one being that it documents that the
current behaviour where we disable streaming mode and ZA prior to
running guests as being the intended behaviour. There's a more detailed
discussion of that change in the patch commit log, but briefly we need
to disable streaming mode in order to avoid EL1 triggering SME traps to
itself due to executing instructions which should be valid.

Support for SME in guests is still in progress, due to the introduction
of new register state in SME2 it interacts with the pending SME2 support
series so I was hoping to get that merged first.

Changes in v2:
- Rebased onto v6.3-rc1.
- Small clarifications and tweaks.
- Link to v1: https://lore.kernel.org/r/20221214-kvm-arm64-sme-context-switch-v1-0-383b4699de06@kernel.org

---
Mark Brown (3):
      KVM: arm64: Document check for TIF_FOREIGN_FPSTATE
      KVM: arm64: Restructure check for SVE support in FP trap handler
      KVM: arm64: Clarify host SME state management

 arch/arm64/kvm/fpsimd.c                 | 26 +++++++++++++++++---------
 arch/arm64/kvm/hyp/include/hyp/switch.h | 12 ++++++++++--
 2 files changed, 27 insertions(+), 11 deletions(-)
---
base-commit: fe15c26ee26efa11741a7b632e9f23b01aca4cc6
change-id: 20221214-kvm-arm64-sme-context-switch-532dcefafb81

Best regards,
-- 
Mark Brown <broonie@kernel.org>


^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH v2 1/3] KVM: arm64: Document check for TIF_FOREIGN_FPSTATE
  2023-03-07 17:37 [PATCH v2 0/3] KVM: arm64: Floating point documentation updates and code tweaks Mark Brown
@ 2023-03-07 17:37 ` Mark Brown
  2023-03-07 17:37 ` [PATCH v2 2/3] KVM: arm64: Restructure check for SVE support in FP trap handler Mark Brown
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 5+ messages in thread
From: Mark Brown @ 2023-03-07 17:37 UTC (permalink / raw)
  To: Marc Zyngier, James Morse, Alexandru Elisei, Suzuki K Poulose,
	Oliver Upton, Catalin Marinas, Will Deacon
  Cc: linux-arm-kernel, kvmarm, Mark Brown

In kvm_arch_vcpu_load_fp() we unconditionally set the current FP state
to FP_STATE_HOST_OWNED, this will be overridden to FP_STATE_NONE if
TIF_FOREIGN_FPSTATE is set but the check is deferred until
kvm_arch_vcpu_ctxflush_fp() where we are no longer preemptable. Add a
comment to this effect to help avoid people being concerned about the
lack of a check and discover where the check is done.

Signed-off-by: Mark Brown <broonie@kernel.org>
---
 arch/arm64/kvm/fpsimd.c | 5 +++++
 1 file changed, 5 insertions(+)

diff --git a/arch/arm64/kvm/fpsimd.c b/arch/arm64/kvm/fpsimd.c
index 1279949599b5..3fd0ce6a3500 100644
--- a/arch/arm64/kvm/fpsimd.c
+++ b/arch/arm64/kvm/fpsimd.c
@@ -81,6 +81,11 @@ void kvm_arch_vcpu_load_fp(struct kvm_vcpu *vcpu)
 
 	fpsimd_kvm_prepare();
 
+	/*
+	 * We will check TIF_FOREIGN_FPSTATE just before entering the
+	 * 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;
 
 	vcpu_clear_flag(vcpu, HOST_SVE_ENABLED);

-- 
2.30.2


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* [PATCH v2 2/3] KVM: arm64: Restructure check for SVE support in FP trap handler
  2023-03-07 17:37 [PATCH v2 0/3] KVM: arm64: Floating point documentation updates and code tweaks Mark Brown
  2023-03-07 17:37 ` [PATCH v2 1/3] KVM: arm64: Document check for TIF_FOREIGN_FPSTATE Mark Brown
@ 2023-03-07 17:37 ` Mark Brown
  2023-03-07 17:37 ` [PATCH v2 3/3] KVM: arm64: Clarify host SME state management Mark Brown
  2023-04-21 12:54 ` [PATCH v2 0/3] KVM: arm64: Floating point documentation updates and code tweaks Marc Zyngier
  3 siblings, 0 replies; 5+ messages in thread
From: Mark Brown @ 2023-03-07 17:37 UTC (permalink / raw)
  To: Marc Zyngier, James Morse, Alexandru Elisei, Suzuki K Poulose,
	Oliver Upton, Catalin Marinas, Will Deacon
  Cc: linux-arm-kernel, kvmarm, Mark Brown

We share the same handler for general floating point and SVE traps with a
check to make sure we don't handle any SVE traps if the system doesn't
have SVE support. Since we will be adding SME support and wishing to handle
that along with other FP related traps rewrite the check to be more scalable
and a bit clearer too, ensuring we don't misidentify SME traps as SVE ones.

Signed-off-by: Mark Brown <broonie@kernel.org>
---
 arch/arm64/kvm/hyp/include/hyp/switch.h | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)

diff --git a/arch/arm64/kvm/hyp/include/hyp/switch.h b/arch/arm64/kvm/hyp/include/hyp/switch.h
index 07d37ff88a3f..d29d2ebf9126 100644
--- a/arch/arm64/kvm/hyp/include/hyp/switch.h
+++ b/arch/arm64/kvm/hyp/include/hyp/switch.h
@@ -176,9 +176,17 @@ static bool kvm_hyp_handle_fpsimd(struct kvm_vcpu *vcpu, u64 *exit_code)
 	sve_guest = vcpu_has_sve(vcpu);
 	esr_ec = kvm_vcpu_trap_get_class(vcpu);
 
-	/* Don't handle SVE traps for non-SVE vcpus here: */
-	if (!sve_guest && esr_ec != ESR_ELx_EC_FP_ASIMD)
+	/* Only handle traps the vCPU can support here: */
+	switch (esr_ec) {
+	case ESR_ELx_EC_FP_ASIMD:
+		break;
+	case ESR_ELx_EC_SVE:
+		if (!sve_guest)
+			return false;
+		break;
+	default:
 		return false;
+	}
 
 	/* Valid trap.  Switch the context: */
 

-- 
2.30.2


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* [PATCH v2 3/3] KVM: arm64: Clarify host SME state management
  2023-03-07 17:37 [PATCH v2 0/3] KVM: arm64: Floating point documentation updates and code tweaks Mark Brown
  2023-03-07 17:37 ` [PATCH v2 1/3] KVM: arm64: Document check for TIF_FOREIGN_FPSTATE Mark Brown
  2023-03-07 17:37 ` [PATCH v2 2/3] KVM: arm64: Restructure check for SVE support in FP trap handler Mark Brown
@ 2023-03-07 17:37 ` Mark Brown
  2023-04-21 12:54 ` [PATCH v2 0/3] KVM: arm64: Floating point documentation updates and code tweaks Marc Zyngier
  3 siblings, 0 replies; 5+ messages in thread
From: Mark Brown @ 2023-03-07 17:37 UTC (permalink / raw)
  To: Marc Zyngier, James Morse, Alexandru Elisei, Suzuki K Poulose,
	Oliver Upton, Catalin Marinas, Will Deacon
  Cc: linux-arm-kernel, kvmarm, Mark Brown

Normally when running a guest we do not touch the floating point
register state until first use of floating point by the guest, saving
the current state and loading the guest state at that point. This has
been found to offer a performance benefit in common cases. However
currently if SME is active when switching to a guest then we exit
streaming mode, disable ZA and invalidate the floating point register
state prior to starting the guest.

The exit from streaming mode is required for correct guest operation, if
we leave streaming mode enabled then many non-SME operations can
generate SME traps (eg, SVE operations will become streaming SVE
operations). If EL1 leaves CPACR_EL1.SMEN disabled then the host is
unable to intercept these traps. This will mean that a SME unaware guest
will see SME exceptions which will confuse it. Disabling streaming mode
also avoids creating spurious indications of usage of the SME hardware
which could impact system performance, especially with shared SME
implementations. Document the requirement to exit streaming mode
clearly.

There is no issue with guest operation caused by PSTATE.ZA so we can
defer handling for that until first floating point usage, do so if the
register state is not that of the current task and hence has already
been saved. We could also do this for the case where the register state
is that for the current task however this is very unlikely to happen and
would require disproportionate effort so continue to save the state in
that case.

Saving this state on first use would require that we map and unmap
storage for the host version of these registers for use by the
hypervisor, taking care to deal with protected KVM and the fact that the
host can free or reallocate the backing storage. Given that the strong
recommendation is that applications should only keep PSTATE.ZA enabled
when the state it enables is in active use it is difficult to see a case
where a VMM would wish to do this, it would need to not only be using
SME but also running the guest in the middle of SME usage. This can be
revisited in the future if a use case does arises, in the interim such
tasks will work but experience a performance overhead.

This brings our handling of SME more into line with our handling of
other floating point state and documents more clearly the constraints we
have, especially around streaming mode.

Signed-off-by: Mark Brown <broonie@kernel.org>
---
 arch/arm64/kvm/fpsimd.c | 21 ++++++++++++---------
 1 file changed, 12 insertions(+), 9 deletions(-)

diff --git a/arch/arm64/kvm/fpsimd.c b/arch/arm64/kvm/fpsimd.c
index 3fd0ce6a3500..4c9dcd8fc939 100644
--- a/arch/arm64/kvm/fpsimd.c
+++ b/arch/arm64/kvm/fpsimd.c
@@ -92,20 +92,23 @@ void kvm_arch_vcpu_load_fp(struct kvm_vcpu *vcpu)
 	if (read_sysreg(cpacr_el1) & CPACR_EL1_ZEN_EL0EN)
 		vcpu_set_flag(vcpu, HOST_SVE_ENABLED);
 
-	/*
-	 * We don't currently support SME guests but if we leave
-	 * things in streaming mode then when the guest starts running
-	 * FPSIMD or SVE code it may generate SME traps so as a
-	 * special case if we are in streaming mode we force the host
-	 * state to be saved now and exit streaming mode so that we
-	 * don't have to handle any SME traps for valid guest
-	 * operations. Do this for ZA as well for now for simplicity.
-	 */
 	if (system_supports_sme()) {
 		vcpu_clear_flag(vcpu, HOST_SME_ENABLED);
 		if (read_sysreg(cpacr_el1) & CPACR_EL1_SMEN_EL0EN)
 			vcpu_set_flag(vcpu, HOST_SME_ENABLED);
 
+		/*
+		 * If PSTATE.SM is enabled then save any pending FP
+		 * state and disable PSTATE.SM. If we leave PSTATE.SM
+		 * enabled and the guest does not enable SME via
+		 * CPACR_EL1.SMEN then operations that should be valid
+		 * may generate SME traps from EL1 to EL1 which we
+		 * can't intercept and which would confuse the guest.
+		 *
+		 * Do the same for PSTATE.ZA in the case where there
+		 * is state in the registers which has not already
+		 * 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;
 			fpsimd_save_and_flush_cpu_state();

-- 
2.30.2


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH v2 0/3] KVM: arm64: Floating point documentation updates and code tweaks
  2023-03-07 17:37 [PATCH v2 0/3] KVM: arm64: Floating point documentation updates and code tweaks Mark Brown
                   ` (2 preceding siblings ...)
  2023-03-07 17:37 ` [PATCH v2 3/3] KVM: arm64: Clarify host SME state management Mark Brown
@ 2023-04-21 12:54 ` Marc Zyngier
  3 siblings, 0 replies; 5+ messages in thread
From: Marc Zyngier @ 2023-04-21 12:54 UTC (permalink / raw)
  To: Will Deacon, Oliver Upton, Alexandru Elisei, James Morse,
	Suzuki K Poulose, Mark Brown, Catalin Marinas
  Cc: linux-arm-kernel, kvmarm

On Tue, 07 Mar 2023 17:37:13 +0000, Mark Brown wrote:
> This series has some improvements to the documentation and code for the
> KVM floating point code, the main one being that it documents that the
> current behaviour where we disable streaming mode and ZA prior to
> running guests as being the intended behaviour. There's a more detailed
> discussion of that change in the patch commit log, but briefly we need
> to disable streaming mode in order to avoid EL1 triggering SME traps to
> itself due to executing instructions which should be valid.
> 
> [...]

Applied to next, thanks!

[1/3] KVM: arm64: Document check for TIF_FOREIGN_FPSTATE
      commit: 4c181e3d352e9280c84fb4b4c7a8940ce005374e
[2/3] KVM: arm64: Restructure check for SVE support in FP trap handler
      commit: d071cefdcca39fdbcdd4bf36868448820dbac34b
[3/3] KVM: arm64: Clarify host SME state management
      commit: aaa2f14e6f3f34de8edfb13566110a0fe0d88785

Cheers,

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



^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2023-04-21 12:54 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-03-07 17:37 [PATCH v2 0/3] KVM: arm64: Floating point documentation updates and code tweaks Mark Brown
2023-03-07 17:37 ` [PATCH v2 1/3] KVM: arm64: Document check for TIF_FOREIGN_FPSTATE Mark Brown
2023-03-07 17:37 ` [PATCH v2 2/3] KVM: arm64: Restructure check for SVE support in FP trap handler Mark Brown
2023-03-07 17:37 ` [PATCH v2 3/3] KVM: arm64: Clarify host SME state management Mark Brown
2023-04-21 12:54 ` [PATCH v2 0/3] KVM: arm64: Floating point documentation updates and code tweaks Marc Zyngier

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).