Skip to contentSkip to conversation

arm64: defer reloading a task's FPSIMD state to userland resume

PATCH RESEND New Ard Biesheuvel 2 participants 6 messages Jan 31, 2014 – Feb 4, 2014 arm64

lore
Share & tools
lore
#0 lore raw

PATCH RESEND 1/2 arm64: defer reloading a task's FPSIMD state to userland resume

If a task gets scheduled out and back in again and nothing has touched its FPSIMD state in the mean time, there is really no reason to reload it from memory. Similarly, repeated calls to kernel_neon_begin() and kernel_neon_end() will preserve and restore the FPSIMD state every time.

This patch defers the FPSIMD state restore to the last possible moment, i.e., right before the task re-enters userland. If a task does not enter userland at all (for any reason), the existing FPSIMD state is preserved and may be reused by the owning task if it gets scheduled in again on the same CPU.

---
 arch/arm64/include/asm/fpsimd.h      |  3 ++
 arch/arm64/include/asm/thread_info.h |  4 +-
 arch/arm64/kernel/entry.S            |  2 +-
 arch/arm64/kernel/fpsimd.c           | 79 +++++++++++++++++++++++++++++++-----
 arch/arm64/kernel/process.c          |  3 +-
 arch/arm64/kernel/signal.c           |  3 ++
 6 files changed, 81 insertions(+), 13 deletions(-)
arch/arm64/include/asm/fpsimd.h +3 -0
index c43b4ac13008..609bc44ceb8d 100644
@@ -37,6 +37,8 @@ struct fpsimd_state {
 			u32 fpcr;
 		};
 	};
+	/* the id of the last cpu to have restored this state */
+	unsigned int last_cpu;
 };
 
 #if defined(__KERNEL__) && defined(CONFIG_COMPAT)
@@ -57,6 +59,7 @@ extern void fpsimd_load_state(struct fpsimd_state *state);
 
 extern void fpsimd_thread_switch(struct task_struct *next);
 extern void fpsimd_flush_thread(void);
+extern void fpsimd_reload_fpstate(void);
 
 #endif
 
arch/arm64/include/asm/thread_info.h +3 -1
index 720e70b66ffd..4a1ca1cfb2f8 100644
@@ -100,6 +100,7 @@ static inline struct thread_info *current_thread_info(void)
 #define TIF_SIGPENDING		0
 #define TIF_NEED_RESCHED	1
 #define TIF_NOTIFY_RESUME	2	/* callback before returning to user */
+#define TIF_FOREIGN_FPSTATE	3	/* CPU's FP state is not current's */
 #define TIF_SYSCALL_TRACE	8
 #define TIF_POLLING_NRFLAG	16
 #define TIF_MEMDIE		18	/* is terminating due to OOM killer */
@@ -112,10 +113,11 @@ static inline struct thread_info *current_thread_info(void)
 #define _TIF_SIGPENDING		(1 << TIF_SIGPENDING)
 #define _TIF_NEED_RESCHED	(1 << TIF_NEED_RESCHED)
 #define _TIF_NOTIFY_RESUME	(1 << TIF_NOTIFY_RESUME)
+#define _TIF_FOREIGN_FPSTATE	(1 << TIF_FOREIGN_FPSTATE)
 #define _TIF_32BIT		(1 << TIF_32BIT)
 
 #define _TIF_WORK_MASK		(_TIF_NEED_RESCHED | _TIF_SIGPENDING | \
-				 _TIF_NOTIFY_RESUME)
+				 _TIF_NOTIFY_RESUME | _TIF_FOREIGN_FPSTATE)
 
 #endif /* __KERNEL__ */
 #endif /* __ASM_THREAD_INFO_H */
arch/arm64/kernel/entry.S +1 -1
index 39ac630d83de..80464e2fb1a5 100644
@@ -576,7 +576,7 @@ fast_work_pending:
 	str	x0, [sp, #S_X0]			// returned x0
 work_pending:
 	tbnz	x1, #TIF_NEED_RESCHED, work_resched
-	/* TIF_SIGPENDING or TIF_NOTIFY_RESUME case */
+	/* TIF_SIGPENDING, TIF_NOTIFY_RESUME or TIF_FOREIGN_FPSTATE case */
 	ldr	x2, [sp, #S_PSTATE]
 	mov	x0, sp				// 'regs'
 	tst	x2, #PSR_MODE_MASK		// user mode regs?
arch/arm64/kernel/fpsimd.c +69 -10

109 lines not on this page · raw message

arch/arm64/kernel/process.c +2 -1
index 248a15db37f2..274316df860f 100644
@@ -205,7 +205,8 @@ void release_thread(struct task_struct *dead_task)
 
 int arch_dup_task_struct(struct task_struct *dst, struct task_struct *src)
 {
-	fpsimd_save_state(&current->thread.fpsimd_state);
+	if (!test_thread_flag(TIF_FOREIGN_FPSTATE))
+		fpsimd_save_state(&current->thread.fpsimd_state);
 	*dst = *src;
 	return 0;
 }
arch/arm64/kernel/signal.c +3 -0
index 890a591f75dd..0a9eccf4fc0f 100644
@@ -416,4 +416,7 @@ asmlinkage void do_notify_resume(struct pt_regs *regs,
 		clear_thread_flag(TIF_NOTIFY_RESUME);
 		tracehook_notify_resume(regs);
 	}
+
+	if (thread_flags & _TIF_FOREIGN_FPSTATE)
+		fpsimd_reload_fpstate();
 }
Signature · 1 line
1.8.3.2
#1 lore raw

PATCH RESEND 2/2 arm64: add support for kernel mode NEON in interrupt context

This patch modifies kernel_neon_begin() and kernel_neon_end(), so they may be called from any context. To address the case where only a couple of registers are needed, kernel_neon_begin_partial(u32) is introduced which takes as a parameter the number of bottom 'n' NEON q-registers required. To mark the end of such a partial section, the regular kernel_neon_end() should be used.

---
 arch/arm64/include/asm/fpsimd.h       | 17 ++++++++++++++
 arch/arm64/include/asm/fpsimdmacros.h | 37 +++++++++++++++++++++++++++++
 arch/arm64/include/asm/neon.h         |  6 ++++-
 arch/arm64/kernel/entry-fpsimd.S      | 24 +++++++++++++++++++
 arch/arm64/kernel/fpsimd.c            | 44 +++++++++++++++++++++++------------
 5 files changed, 112 insertions(+), 16 deletions(-)
arch/arm64/include/asm/fpsimd.h +17 -0
index 609bc44ceb8d..dc9ef741c648 100644
@@ -41,6 +41,19 @@ struct fpsimd_state {
 	unsigned int last_cpu;
 };
 
+/*
+ * Struct for stacking the bottom 'n' FP/SIMD registers.
+ * Mainly intended for kernel use of v8 Crypto Extensions which only
+ * needs a few registers and may need to execute in atomic context.
+ */
+struct fpsimd_partial_state {
+	u32		num_regs;
+	u32		fpsr;
+	u32		fpcr;
+	__uint128_t	vregs[32] __aligned(16);
+} __aligned(16);
+
+
 #if defined(__KERNEL__) && defined(CONFIG_COMPAT)
 /* Masks for extracting the FPSR and FPCR from the FPSCR */
 #define VFP_FPSCR_STAT_MASK	0xf800009f
@@ -57,6 +70,10 @@ struct task_struct;
 extern void fpsimd_save_state(struct fpsimd_state *state);
 extern void fpsimd_load_state(struct fpsimd_state *state);
 
+extern void fpsimd_save_partial_state(struct fpsimd_partial_state *state,
+				      u32 num_regs);
+extern void fpsimd_load_partial_state(struct fpsimd_partial_state *state);
+
 extern void fpsimd_thread_switch(struct task_struct *next);
 extern void fpsimd_flush_thread(void);
 extern void fpsimd_reload_fpstate(void);
arch/arm64/include/asm/fpsimdmacros.h +37 -0
index bbec599c96bd..42990a82c671 100644
@@ -62,3 +62,40 @@
 	ldr	w\tmpnr, [\state, #16 * 2 + 4]
 	msr	fpcr, x\tmpnr
 .endm
+
+.altmacro
+.macro	q2op, op, q1, q2, state
+	\op	q\q1, q\q2, [\state, # -16 * \q1 - 16]
+.endm
+
+.macro fpsimd_save_partial state, numnr, tmpnr1, tmpnr2
+	mrs	x\tmpnr1, fpsr
+	str	w\numnr, [\state]
+	mrs	x\tmpnr2, fpcr
+	stp	w\tmpnr1, w\tmpnr2, [\state, #4]
+	adr	x\tmpnr1, 0f
+	add	\state, \state, x\numnr, lsl #4
+	sub	x\tmpnr1, x\tmpnr1, x\numnr, lsl #1
+	br	x\tmpnr1
+	.irp	qa, 30, 28, 26, 24, 22, 20, 18, 16, 14, 12, 10, 8, 6, 4, 2, 0
+		qb = \qa + 1
+	q2op	stp, \qa, %qb, \state
+	.endr
+0:
+.endm
+
+.macro fpsimd_restore_partial state, tmpnr1, tmpnr2
+	ldp	w\tmpnr1, w\tmpnr2, [\state, #4]
+	msr	fpsr, x\tmpnr1
+	msr	fpcr, x\tmpnr2
+	adr	x\tmpnr1, 0f
+	ldr	w\tmpnr2, [\state]
+	add	\state, \state, x\tmpnr2, lsl #4
+	sub	x\tmpnr1, x\tmpnr1, x\tmpnr2, lsl #1
+	br	x\tmpnr1
+	.irp	qa, 30, 28, 26, 24, 22, 20, 18, 16, 14, 12, 10, 8, 6, 4, 2, 0
+		qb = \qa + 1
+	q2op	ldp, \qa, %qb, \state
+	.endr
+0:
+.endm
arch/arm64/include/asm/neon.h +5 -1
index b0cc58a97780..13ce4cc18e26 100644
@@ -8,7 +8,11 @@
  * published by the Free Software Foundation.
  */
 
+#include <linux/types.h>
+
 #define cpu_has_neon()		(1)
 
-void kernel_neon_begin(void);
+#define kernel_neon_begin()	kernel_neon_begin_partial(32)
+
+void kernel_neon_begin_partial(u32 num_regs);
 void kernel_neon_end(void);
arch/arm64/kernel/entry-fpsimd.S +24 -0
index 6a27cd6dbfa6..d358ccacfc00 100644
@@ -41,3 +41,27 @@ ENTRY(fpsimd_load_state)
 	fpsimd_restore x0, 8
 	ret
 ENDPROC(fpsimd_load_state)
+
+#ifdef CONFIG_KERNEL_MODE_NEON
+
+/*
+ * Save the bottom n FP registers.
+ *
+ * x0 - pointer to struct fpsimd_partial_state
+ */
+ENTRY(fpsimd_save_partial_state)
+	fpsimd_save_partial x0, 1, 8, 9
+	ret
+ENDPROC(fpsimd_load_partial_state)
+
+/*
+ * Load the bottom n FP registers.
+ *
+ * x0 - pointer to struct fpsimd_partial_state
+ */
+ENTRY(fpsimd_load_partial_state)
+	fpsimd_restore_partial x0, 8, 9
+	ret
+ENDPROC(fpsimd_load_partial_state)
+
+#endif
arch/arm64/kernel/fpsimd.c +29 -15

59 lines not on this page · raw message

Signature · 1 line
1.8.3.2
#2 lore raw

PATCH RESEND 1/2 arm64: defer reloading a task's FPSIMD state to userland resume

In reply to Ard Biesheuvel

Hi Ard,

Quoted 10 lines · Ard Biesheuvel

If a task gets scheduled out and back in again and nothing has touched its FPSIMD state in the mean time, there is really no reason to reload it from memory. Similarly, repeated calls to kernel_neon_begin() and kernel_neon_end() will preserve and restore the FPSIMD state every time.

This patch defers the FPSIMD state restore to the last possible moment, i.e., right before the task re-enters userland. If a task does not enter userland at all (for any reason), the existing FPSIMD state is preserved and may be reused by the owning task if it gets scheduled in again on the same CPU.

The one situation I'm unsure of here is how you deal with the saved fpsimd state potentially being updated by a signal handler or a debugger. In this case, we probably need to set _TIF_FOREIGN_FPSTATE to force a reload, or are you handling this some other way?

Will

#3 lore raw

PATCH RESEND 1/2 arm64: defer reloading a task's FPSIMD state to userland resume

Quoted 14 lines · Will Deacon

Hi Ard,

Quoted 10 lines · Ard Biesheuvel

If a task gets scheduled out and back in again and nothing has touched its FPSIMD state in the mean time, there is really no reason to reload it from memory. Similarly, repeated calls to kernel_neon_begin() and kernel_neon_end() will preserve and restore the FPSIMD state every time.

This patch defers the FPSIMD state restore to the last possible moment, i.e., right before the task re-enters userland. If a task does not enter userland at all (for any reason), the existing FPSIMD state is preserved and may be reused by the owning task if it gets scheduled in again on the same CPU.

Quoted 5 lines

The one situation I'm unsure of here is how you deal with the saved fpsimd state potentially being updated by a signal handler or a debugger. In this case, we probably need to set _TIF_FOREIGN_FPSTATE to force a reload, or are you handling this some other way?

If I am reading the code correctly, the signal handler is entered using the normal userland resume path, so I don't think it requires special treatment.

For the ptrace() case, it should suffice to set the 'last_cpu' field to (u32)-1 to indicate that the FPSIMD context should be reloaded from memory regardless of which CPU the debuggee is restarted on.

Regards,
Ard.

#4 lore raw

PATCH RESEND 1/2 arm64: defer reloading a task's FPSIMD state to userland resume

Hello,

Quoted 19 lines · Ard Biesheuvel
Quoted 12 lines · Will Deacon
Quoted 10 lines · Ard Biesheuvel

If a task gets scheduled out and back in again and nothing has touched its FPSIMD state in the mean time, there is really no reason to reload it from memory. Similarly, repeated calls to kernel_neon_begin() and kernel_neon_end() will preserve and restore the FPSIMD state every time.

This patch defers the FPSIMD state restore to the last possible moment, i.e., right before the task re-enters userland. If a task does not enter userland at all (for any reason), the existing FPSIMD state is preserved and may be reused by the owning task if it gets scheduled in again on the same CPU.

Quoted 5 lines

The one situation I'm unsure of here is how you deal with the saved fpsimd state potentially being updated by a signal handler or a debugger. In this case, we probably need to set _TIF_FOREIGN_FPSTATE to force a reload, or are you handling this some other way?

Quoted 3 lines

If I am reading the code correctly, the signal handler is entered using the normal userland resume path, so I don't think it requires special treatment.

It was the exiting of the signal handler that I was worried about, where it may have modified the interrupted programs fpsimd state on the stack.

Quoted 3 lines

For the ptrace() case, it should suffice to set the 'last_cpu' field to (u32)-1 to indicate that the FPSIMD context should be reloaded from memory regardless of which CPU the debuggee is restarted on.

Something like that sounds right, but it needs adding/testing.

Will

#5 lore raw

PATCH RESEND 1/2 arm64: defer reloading a task's FPSIMD state to userland resume

Quoted 22 lines · Will Deacon

Hello,

Quoted 19 lines · Ard Biesheuvel
Quoted 17 lines · Will Deacon
Quoted 10 lines · Ard Biesheuvel

If a task gets scheduled out and back in again and nothing has touched its FPSIMD state in the mean time, there is really no reason to reload it from memory. Similarly, repeated calls to kernel_neon_begin() and kernel_neon_end() will preserve and restore the FPSIMD state every time.

This patch defers the FPSIMD state restore to the last possible moment, i.e., right before the task re-enters userland. If a task does not enter userland at all (for any reason), the existing FPSIMD state is preserved and may be reused by the owning task if it gets scheduled in again on the same CPU.

The one situation I'm unsure of here is how you deal with the saved fpsimd state potentially being updated by a signal handler or a debugger. In this case, we probably need to set _TIF_FOREIGN_FPSTATE to force a reload, or are you handling this some other way?

Quoted 7 lines
Quoted 3 lines

If I am reading the code correctly, the signal handler is entered using the normal userland resume path, so I don't think it requires special treatment.

It was the exiting of the signal handler that I was worried about, where it may have modified the interrupted programs fpsimd state on the stack.

Ah, ok, I see what you mean.

I will update the patch so
(a) it only saves the state if _TIF_FOREIGN_FPSTATE is cleared (so we don't overwrite the task's saved state inadvertently), and
(b) it sets _TIF_FOREIGN_FPSTATE instead of performing the restore upon return from the signal handler.

Quoted 6 lines
Quoted 3 lines

For the ptrace() case, it should suffice to set the 'last_cpu' field to (u32)-1 to indicate that the FPSIMD context should be reloaded from memory regardless of which CPU the debuggee is restarted on.

Something like that sounds right, but it needs adding/testing.

OK, I will add the above and do some more testing.

Cheers,
Ard.