Skip to contentSkip to conversation

arm64/sve: Improve performance when handling SVE access traps

RFC PATCH Superseded Mark Brown 1 participant 3 messages Jan 12, 2021

Subsystems (3)

v6 of 7 Superseded Latest is v7 →

lore
Share & tools
lore

Mark Brown

author
#0 lore raw

v6 cover arm64/sve: Improve performance when handling SVE access traps

This patch series aims to improve the performance of handling SVE access traps, earlier versions were originally written by Julien Gral but based on discussions on previous versions the patches have been substantially reworked to use a different approach. The patches are now different enough that I set myself as the author, hopefully that's OK for Julien.

I've marked this as RFC since it's not quite ready yet but I'd really like feedback on the overall approach, it's a big change in implementation. It needs at least one more pass for polish and while it's holding up in my testing thus far I've not done as much as I'd like yet.

Per the syscall ABI, SVE registers will be unknown after a syscall. In practice, the kernel will disable SVE and the registers will be zeroed (except the first 128 bits of each vector) on the next SVE instruction. Currently we do this by saving the FPSIMD state to memory, converting to the matching SVE state and then reloading the registers on return to userspace. This requires a lot of memory accesses that we shouldn't need, improve this by reworking the SVE state tracking so we track if we should trap on executing SVE instructions separately to if we need to save the full register state. This allows us to avoid tracking the full SVE state until we need to return to userspace and to convert directly in registers in the common case where the FPSIMD state is still in registers then.

As with current mainline we disable SVE on every syscall. This may not be ideal for applications that mix SVE and syscall usage, strategies such as SH's fpu_counter may perform better but we need to assess the performance on a wider range of systems than are currently available before implementing anything.

It is also possible to optimize the case when the SVE vector length is 128-bit (ie the same size as the FPSIMD vectors). This could be explored in the future, it becomes a lot easier to do with this implementation.

v6:
- Substantially rework the patch so that TIF_SVE is now replaced by two flags TIF_SVE_EXEC and TIF_SVE_FULL_REGS.
- Return to disabling SVE after every syscall as for current mainine rather than leaving it enabled unless reset via ptrace.
v5:
- Rebase onto v5.10-rc2.
- Explicitly support the case where TIF_SVE and TIF_SVE_NEEDS_FLUSH are set simultaneously, though this is not currently expected to happen.
- Extensively revised the documentation for TIF_SVE and TIF_SVE_NEEDS_FLUSH to hopefully make things more clear together with the above, I hope this addresses the comments on the prior version but it really needs fresh eyes to tell if that's actually the case.
- Make comments in ptrace.c more precise.
- Remove some redundant checks for system_has_sve().
v4:
- Rebase onto v5.9-rc2
- Address review comments from Dave Martin, mostly documentation but also some refactorings to ensure we don't check capabilities multiple times and the addition of some WARN_ONs to make sure assumptions we are making about what TIF_ flags can be set when are true.
v3:
- Rebased to current kernels.
- Addressed review comments from v2, mostly around tweaks in the documentation.

Mark Brown (2):
arm64/sve: Split TIF_SVE into separate execute and register state
flags
arm64/sve: Rework SVE trap access to minimise memory access

 arch/arm64/include/asm/fpsimd.h      |   2 +
 arch/arm64/include/asm/thread_info.h |   3 +-
 arch/arm64/kernel/entry-fpsimd.S     |   5 +
 arch/arm64/kernel/fpsimd.c           | 204 +++++++++++++++++++--------
 arch/arm64/kernel/process.c          |   7 +-
 arch/arm64/kernel/ptrace.c           |   8 +-
 arch/arm64/kernel/signal.c           |  15 +-
 arch/arm64/kernel/syscall.c          |   3 +-
 arch/arm64/kvm/fpsimd.c              |   6 +-
 9 files changed, 179 insertions(+), 74 deletions(-)

base-commit: 7c53f6b671f4aba70ff15e1b05148b10d58c2837

Signature · 7 lines
2.20.1


_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
author
#1 lore raw

RFC PATCH v6 1/2 arm64/sve: Split TIF_SVE into separate execute and register state flags

Currently we have a single flag TIF_SVE which says that a task is allowed to execute SVE instructions without trapping and also that full SVE register state is stored for the task. This results in us doing extra work storing and restoring the full SVE register state even in those cases where the ABI is that only the first 128 bits of the Z0-V31 registers which are shared with the FPSIMD V0-V31 are valid.

In order to allow us to avoid these overheads split TIF_SVE up so that we have two separate flags, TIF_SVE_EXEC which allows execution of SVE instructions without trapping and TIF_SVE_FULL_REGS which indicates that the full SVE register state is stored. If both are set the behaviour is as currently, if TIF_SVE_EXEC is set without TIF_SVE_FULL_REGS then we save and restore only the FPSIMD registers until we return to userspace with TIF_SVE_EXEC enabled at which point we convert the FPSIMD registers to SVE. It is not meaningful to have TIF_SVE_FULL_REGS set without TIF_SVE_EXEC.

This patch is intended only to split the flags, it does not take avantage of the ability to set the flags independently and the new state with TIF_SVE_EXEC only should not be observed.

This is based on earlier work by Julien Gral implementing a slightly different approach.

---
 arch/arm64/include/asm/thread_info.h |   3 +-
 arch/arm64/kernel/fpsimd.c           | 175 +++++++++++++++++++--------
 arch/arm64/kernel/process.c          |   7 +-
 arch/arm64/kernel/ptrace.c           |   8 +-
 arch/arm64/kernel/signal.c           |  15 ++-
 arch/arm64/kernel/syscall.c          |   3 +-
 arch/arm64/kvm/fpsimd.c              |   6 +-
 7 files changed, 152 insertions(+), 65 deletions(-)
arch/arm64/include/asm/thread_info.h +2 -1
index 9f4e3b266f21..c856159e071c 100644
@@ -65,6 +65,7 @@ void arch_release_task_struct(struct task_struct *tsk);
 #define TIF_UPROBE		4	/* uprobe breakpoint or singlestep */
 #define TIF_MTE_ASYNC_FAULT	5	/* MTE Asynchronous Tag Check Fault */
 #define TIF_NOTIFY_SIGNAL	6	/* signal notifications exist */
+#define TIF_SVE_EXEC		7	/* SVE instructions don't trap */
 #define TIF_SYSCALL_TRACE	8	/* syscall trace active */
 #define TIF_SYSCALL_AUDIT	9	/* syscall auditing */
 #define TIF_SYSCALL_TRACEPOINT	10	/* syscall tracepoint for ftrace */
@@ -75,7 +76,7 @@ void arch_release_task_struct(struct task_struct *tsk);
 #define TIF_RESTORE_SIGMASK	20
 #define TIF_SINGLESTEP		21
 #define TIF_32BIT		22	/* 32bit process */
-#define TIF_SVE			23	/* Scalable Vector Extension in use */
+#define TIF_SVE_FULL_REGS	23	/* Full SVE register set stored */
 #define TIF_SVE_VL_INHERIT	24	/* Inherit sve_vl_onexec across exec */
 #define TIF_SSBD		25	/* Wants SSB mitigation */
 #define TIF_TAGGED_ADDR		26	/* Allow tagged user addresses */
arch/arm64/kernel/fpsimd.c +127 -48

303 lines not on this page · raw message

arch/arm64/kernel/process.c +4 -3
index 6616486a58fe..71c8265b9139 100644
@@ -364,13 +364,14 @@ int arch_dup_task_struct(struct task_struct *dst, struct task_struct *src)
 	 * Detach src's sve_state (if any) from dst so that it does not
 	 * get erroneously used or freed prematurely.  dst's sve_state
 	 * will be allocated on demand later on if dst uses SVE.
-	 * For consistency, also clear TIF_SVE here: this could be done
+	 * For consistency, also clear TIF_SVE_* here: this could be done
 	 * later in copy_process(), but to avoid tripping up future
-	 * maintainers it is best not to leave TIF_SVE and sve_state in
+	 * maintainers it is best not to leave TIF_SVE_* and sve_state in
 	 * an inconsistent state, even temporarily.
 	 */
 	dst->thread.sve_state = NULL;
-	clear_tsk_thread_flag(dst, TIF_SVE);
+	clear_tsk_thread_flag(dst, TIF_SVE_EXEC);
+	clear_tsk_thread_flag(dst, TIF_SVE_FULL_REGS);
 
 	/* clear any pending asynchronous tag fault raised by the parent */
 	clear_tsk_thread_flag(dst, TIF_MTE_ASYNC_FAULT);
arch/arm64/kernel/ptrace.c +5 -3
index 8ac487c84e37..f0406b3dc389 100644
@@ -719,7 +719,7 @@ static void sve_init_header_from_task(struct user_sve_header *header,
 
 	memset(header, 0, sizeof(*header));
 
-	header->flags = test_tsk_thread_flag(target, TIF_SVE) ?
+	header->flags = test_tsk_thread_flag(target, TIF_SVE_FULL_REGS) ?
 		SVE_PT_REGS_SVE : SVE_PT_REGS_FPSIMD;
 	if (test_tsk_thread_flag(target, TIF_SVE_VL_INHERIT))
 		header->flags |= SVE_PT_VL_INHERIT;
@@ -827,7 +827,8 @@ static int sve_set(struct task_struct *target,
 	if ((header.flags & SVE_PT_REGS_MASK) == SVE_PT_REGS_FPSIMD) {
 		ret = __fpr_set(target, regset, pos, count, kbuf, ubuf,
 				SVE_PT_FPSIMD_OFFSET);
-		clear_tsk_thread_flag(target, TIF_SVE);
+		clear_tsk_thread_flag(target, TIF_SVE_EXEC);
+		clear_tsk_thread_flag(target, TIF_SVE_FULL_REGS);
 		goto out;
 	}
 
@@ -851,7 +852,8 @@ static int sve_set(struct task_struct *target,
 	 * unmodified.
 	 */
 	fpsimd_sync_to_sve(target);
-	set_tsk_thread_flag(target, TIF_SVE);
+	set_tsk_thread_flag(target, TIF_SVE_EXEC);
+	set_tsk_thread_flag(target, TIF_SVE_FULL_REGS);
 
 	BUILD_BUG_ON(SVE_PT_SVE_OFFSET != sizeof(header));
 	start = SVE_PT_SVE_OFFSET;
arch/arm64/kernel/signal.c +9 -6
index f71d6ce4673f..5d5610af7ea3 100644
@@ -205,7 +205,8 @@ static int restore_fpsimd_context(struct fpsimd_context __user *ctx)
 	__get_user_error(fpsimd.fpsr, &ctx->fpsr, err);
 	__get_user_error(fpsimd.fpcr, &ctx->fpcr, err);
 
-	clear_thread_flag(TIF_SVE);
+	clear_thread_flag(TIF_SVE_EXEC);
+	clear_thread_flag(TIF_SVE_FULL_REGS);
 
 	/* load the hardware registers from the fpsimd_state structure */
 	if (!err)
@@ -229,7 +230,7 @@ static int preserve_sve_context(struct sve_context __user *ctx)
 	unsigned int vl = current->thread.sve_vl;
 	unsigned int vq = 0;
 
-	if (test_thread_flag(TIF_SVE))
+	if (test_thread_flag(TIF_SVE_EXEC))
 		vq = sve_vq_from_vl(vl);
 
 	memset(reserved, 0, sizeof(reserved));
@@ -241,7 +242,7 @@ static int preserve_sve_context(struct sve_context __user *ctx)
 	BUILD_BUG_ON(sizeof(ctx->__reserved) != sizeof(reserved));
 	err |= __copy_to_user(&ctx->__reserved, reserved, sizeof(reserved));
 
-	if (vq) {
+	if (vq && test_thread_flag(TIF_SVE_FULL_REGS)) {
 		/*
 		 * This assumes that the SVE state has already been saved to
 		 * the task struct by calling the function
@@ -269,7 +270,8 @@ static int restore_sve_fpsimd_context(struct user_ctxs *user)
 		return -EINVAL;
 
 	if (sve.head.size <= sizeof(*user->sve)) {
-		clear_thread_flag(TIF_SVE);
+		clear_thread_flag(TIF_SVE_EXEC);
+		clear_thread_flag(TIF_SVE_FULL_REGS);
 		goto fpsimd_only;
 	}
 
@@ -296,7 +298,8 @@ static int restore_sve_fpsimd_context(struct user_ctxs *user)
 	if (err)
 		return -EFAULT;
 
-	set_thread_flag(TIF_SVE);
+	set_thread_flag(TIF_SVE_EXEC);
+	set_thread_flag(TIF_SVE_FULL_REGS);
 
 fpsimd_only:
 	/* copy the FP and status/control registers */
@@ -587,7 +590,7 @@ static int setup_sigframe_layout(struct rt_sigframe_user_layout *user,
 	if (system_supports_sve()) {
 		unsigned int vq = 0;
 
-		if (add_all || test_thread_flag(TIF_SVE)) {
+		if (add_all || test_thread_flag(TIF_SVE_EXEC)) {
 			int vl = sve_max_vl;
 
 			if (!add_all)
arch/arm64/kernel/syscall.c +2 -1
index f61e9d8cc55a..f8a2598730c2 100644
@@ -186,7 +186,8 @@ static inline void sve_user_discard(void)
 	if (!system_supports_sve())
 		return;
 
-	clear_thread_flag(TIF_SVE);
+	clear_thread_flag(TIF_SVE_EXEC);
+	clear_thread_flag(TIF_SVE_FULL_REGS);
 
 	/*
 	 * task_fpsimd_load() won't be called to update CPACR_EL1 in
arch/arm64/kvm/fpsimd.c +3 -3
index 3e081d556e81..1b7c0d03581b 100644
@@ -67,7 +67,7 @@ void kvm_arch_vcpu_load_fp(struct kvm_vcpu *vcpu)
 			      KVM_ARM64_HOST_SVE_ENABLED);
 	vcpu->arch.flags |= KVM_ARM64_FP_HOST;
 
-	if (test_thread_flag(TIF_SVE))
+	if (test_thread_flag(TIF_SVE_EXEC))
 		vcpu->arch.flags |= KVM_ARM64_HOST_SVE_IN_USE;
 
 	if (read_sysreg(cpacr_el1) & CPACR_EL1_ZEN_EL0EN)
@@ -90,7 +90,7 @@ void kvm_arch_vcpu_ctxsync_fp(struct kvm_vcpu *vcpu)
 					 vcpu->arch.sve_max_vl);
 
 		clear_thread_flag(TIF_FOREIGN_FPSTATE);
-		update_thread_flag(TIF_SVE, vcpu_has_sve(vcpu));
+		update_thread_flag(TIF_SVE_EXEC, vcpu_has_sve(vcpu));
 	}
 }
 
@@ -127,7 +127,7 @@ void kvm_arch_vcpu_put_fp(struct kvm_vcpu *vcpu)
 			sysreg_clear_set(CPACR_EL1, CPACR_EL1_ZEN_EL0EN, 0);
 	}
 
-	update_thread_flag(TIF_SVE,
+	update_thread_flag(TIF_SVE_EXEC,
 			   vcpu->arch.flags & KVM_ARM64_HOST_SVE_IN_USE);
 
 	local_irq_restore(flags);
Signature · 7 lines
2.20.1


_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
author
#2 lore raw

RFC PATCH v6 2/2 arm64/sve: Rework SVE trap access to minimise memory access

In reply to Mark Brown

When we take a SVE access trap only the subset of the SVE Z0-Z31 registers shared with the FPSIMD V0-V31 registers is valid, the rest of the bits in the SVE registers must be cleared before returning to userspace. Currently we do this by saving the current FPSIMD register state to the task struct and then using that to initalize the copy of the SVE registers in the task struct so they can be loaded from there into the registers. This requires a lot more memory access than we need.

The newly added TIF_SVE_FULL_REGS can be used to reduce this overhead - instead of doing the conversion immediately we can set only TIF_SVE_EXEC and not TIF_SVE_FULL_REGS. This means that until we return to userspace we only need to store the FPSIMD registers and if (as should be the common case) the hardware still has the task state and does not need that to be reloaded from the task struct we can do the initialization of the SVE state entirely in registers. In the event that we do need to reload the registers from the task struct only the FPSIMD subset needs to be loaded from memory.

If the FPSIMD state is loaded then we need to set the vector length. This is because the vector length is only set when loading from memory, the expectation is that the vector length is set when TIF_SVE_EXEC is set. We also need to rebind the task to the CPU so the newly allocated SVE state is used when the task is saved.

This is based on earlier work by Julien Gral implementing a similar idea.

---
 arch/arm64/include/asm/fpsimd.h  |  2 ++
 arch/arm64/kernel/entry-fpsimd.S |  5 +++++
 arch/arm64/kernel/fpsimd.c       | 35 +++++++++++++++++++++-----------
 3 files changed, 30 insertions(+), 12 deletions(-)
arch/arm64/include/asm/fpsimd.h +2 -0
index bec5f14b622a..e60aa4ebb351 100644
@@ -74,6 +74,8 @@ extern void sve_load_from_fpsimd_state(struct user_fpsimd_state const *state,
 				       unsigned long vq_minus_1);
 extern unsigned int sve_get_vl(void);
 
+extern void sve_set_vq(unsigned long vq_minus_1);
+
 struct arm64_cpu_capabilities;
 extern void sve_kernel_enable(const struct arm64_cpu_capabilities *__unused);
 
arch/arm64/kernel/entry-fpsimd.S +5 -0
index 2ca395c25448..3ecec60d3295 100644
@@ -48,6 +48,11 @@ SYM_FUNC_START(sve_get_vl)
 	ret
 SYM_FUNC_END(sve_get_vl)
 
+SYM_FUNC_START(sve_set_vq)
+	sve_load_vq x0, x1, x2
+	ret
+SYM_FUNC_END(sve_set_vq)
+
 /*
  * Load SVE state from FPSIMD state.
  *
arch/arm64/kernel/fpsimd.c +23 -12
index af01d23bc8cc..bb5a3c915ce5 100644
@@ -994,10 +994,10 @@ void fpsimd_release_task(struct task_struct *dead_task)
 /*
  * Trapped SVE access
  *
- * Storage is allocated for the full SVE state, the current FPSIMD
- * register contents are migrated across, and TIF_SVE_EXEC is set so that
- * the SVE access trap will be disabled the next time this task
- * reaches ret_to_user.
+ * Storage is allocated for the full SVE state so that the code
+ * running subsequently has somewhere to save the SVE registers to. We
+ * then rely on ret_to_user to actually convert the FPSIMD registers
+ * to SVE state by flushing as required.
  *
  * TIF_SVE_EXEC should be clear on entry: otherwise,
  * fpsimd_restore_current_state() would have disabled the SVE access
@@ -1016,15 +1016,26 @@ void do_sve_acc(unsigned int esr, struct pt_regs *regs)
 
 	get_cpu_fpsimd_context();
 
-	fpsimd_save();
-
-	/* Force ret_to_user to reload the registers: */
-	fpsimd_flush_task_state(current);
-
-	fpsimd_to_sve(current);
+	/*
+	 * We shouldn't trap if we can execute SVE instructions and
+	 * there should be no SVE state if that is the case.
+	 */
 	if (test_and_set_thread_flag(TIF_SVE_EXEC))
-		WARN_ON(1); /* SVE access shouldn't have trapped */
-	set_thread_flag(TIF_SVE_FULL_REGS);
+		WARN_ON(1);
+	if (test_and_clear_thread_flag(TIF_SVE_FULL_REGS))
+		WARN_ON(1);
+
+	/*
+	 * When the FPSIMD state is loaded:
+	 *      - The return path (see fpsimd_restore_current_state) requires
+	 *        the vector length to be loaded beforehand.
+	 *      - We need to rebind the task to the CPU so the newly allocated
+	 *        SVE state is used when the task is saved.
+	 */
+	if (!test_thread_flag(TIF_FOREIGN_FPSTATE)) {
+		sve_set_vq(sve_vq_from_vl(current->thread.sve_vl) - 1);
+		fpsimd_bind_task_to_cpu();
+	}
 
 	put_cpu_fpsimd_context();
 }
Signature · 7 lines
2.20.1


_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel