Skip to contentSkip to conversation

x86: load FPU registers on return to userland

PATCH Superseded Sebastian Andrzej Siewior 8 participants 57 messages Oct 4, 2018 – Nov 2, 2018

Subsystems (5)
x86 KVM KVM x86 x86 MM +1 more subsystems

v3 of 9 Superseded Latest is v9 →

lore
Share & tools
lore
#0 lore raw

v3 cover x86: load FPU registers on return to userland

This is a refurbished series originally started by by Rik van Riel. The goal is load the FPU registers on return to userland and not on every context switch. By this optimisation we can:
- avoid loading the registers if the task stays in kernel and does not return to userland
- make kernel_fpu_begin() cheaper: it only saves the registers on the first invocation. The second invocation does not need save them again.

To access the FPU registers in kernel we need:
- disable preemption to avoid that the scheduler switches tasks. By doing so it would set TIF_LOAD_FPU and the FPU registers would be not valid.
- disable BH because the softirq might use kernel_fpu_begin() and then set TIF_LOAD_FPU instead loading the FPU registers on completion.

v1…v3:
v2 was never posted. I followed the idea to completely decouple PKRU from xstate. This didn't quite work and made a few things complicated. One obvious required fixup is copy_fpstate_to_sigframe() where the PKRU state needs to be fiddled into xstate. This required another xfeatures_mask so that the sanity checks were performed and xstate_offsets would be computed. Additionally ptrace also reads/sets xstate in order to get/set the register and PKRU is one of them. So this would need some fiddle, too.
In v3 I dropped that decouple idea. I also learned that the wrpkru instruction is not privileged and so caching it in kernel does not work. Instead I keep PKRU in xstate area and load it at context switch time while the remaining registers are deferred (until return to userland). The offset of PKRU within xstate is enumerated at boot time so why not use it.

This seems to work with my in-kernel test case and a userland test case which use xmm registers. The pkey feature was tested in non kvm accelerated qemu and it seems to work, too.

Sebastian

#1 lore raw

PATCH v3 1/11 x86/entry: remove _TIF_ALLWORK_MASK

There is no user of _TIF_ALLWORK_MASK since commit 21d375b6b34ff ("x86/entry/64: Remove the SYSCALL64 fast path"). Remove unused define _TIF_ALLWORK_MASK.

---
 arch/x86/include/asm/thread_info.h | 8 --------
 1 file changed, 8 deletions(-)
arch/x86/include/asm/thread_info.h +0 -8
index 2ff2a30a264f4..cd6920674b905 100644
@@ -136,14 +136,6 @@ struct thread_info {
 	 _TIF_SECCOMP | _TIF_SYSCALL_TRACEPOINT |	\
 	 _TIF_NOHZ)
 
-/* work to do on any return to user space */
-#define _TIF_ALLWORK_MASK						\
-	(_TIF_SYSCALL_TRACE | _TIF_NOTIFY_RESUME | _TIF_SIGPENDING |	\
-	 _TIF_NEED_RESCHED | _TIF_SINGLESTEP | _TIF_SYSCALL_EMU |	\
-	 _TIF_SYSCALL_AUDIT | _TIF_USER_RETURN_NOTIFY | _TIF_UPROBE |	\
-	 _TIF_PATCH_PENDING | _TIF_NOHZ | _TIF_SYSCALL_TRACEPOINT |	\
-	 _TIF_FSCHECK)
-
 /* flags to check in __switch_to() */
 #define _TIF_WORK_CTXSW							\
 	(_TIF_IO_BITMAP|_TIF_NOCPUID|_TIF_NOTSC|_TIF_BLOCKSTEP|_TIF_SSBD)
Signature · 1 line
2.19.0
maintainer Reviewed-by on v3 1/11
#2 lore raw

Re: [PATCH 01/11] x86/entry: remove _TIF_ALLWORK_MASK

Quoted 19 lines · Sebastian Andrzej Siewior

There is no user of _TIF_ALLWORK_MASK since commit 21d375b6b34ff ("x86/entry/64: Remove the SYSCALL64 fast path"). Remove unused define _TIF_ALLWORK_MASK.

---
 arch/x86/include/asm/thread_info.h | 8 --------
 1 file changed, 8 deletions(-)
arch/x86/include/asm/thread_info.h +0 -1
index 2ff2a30a264f4..cd6920674b905 100644
@@ -136,14 +136,6 @@ struct thread_info {
 	 _TIF_SECCOMP | _TIF_SYSCALL_TRACEPOINT |	\
 	 _TIF_NOHZ)
 
-/* work to do on any return to user space */

arch/x86/include/asm/thread_info.h struct thread_info { · line 141

Quoted 10 lines
arch/x86/include/asm/thread_info.h +0 -7
-#define _TIF_ALLWORK_MASK						\
-	(_TIF_SYSCALL_TRACE | _TIF_NOTIFY_RESUME | _TIF_SIGPENDING |	\
-	 _TIF_NEED_RESCHED | _TIF_SINGLESTEP | _TIF_SYSCALL_EMU |	\
-	 _TIF_SYSCALL_AUDIT | _TIF_USER_RETURN_NOTIFY | _TIF_UPROBE |	\
-	 _TIF_PATCH_PENDING | _TIF_NOHZ | _TIF_SYSCALL_TRACEPOINT |	\
-	 _TIF_FSCHECK)
-
 /* flags to check in __switch_to() */
 #define _TIF_WORK_CTXSW							\
 	(_TIF_IO_BITMAP|_TIF_NOCPUID|_TIF_NOTSC|_TIF_BLOCKSTEP|_TIF_SSBD)

Just a nitpick for the next version: pls start subjects with a capital letter, i.e.,

x86/entry: Remove _TIF_ALLWORK_MASK

Signature · 4 lines
Regards/Gruss,
    Boris.

Good mailing practices for 400: avoid top-posting and trim the reply.
#3 lore raw

PATCH v3 2/11 x86/fpu: add (__)make_fpregs_active helpers

In reply to Sebastian Andrzej Siewior

From: Rik van Riel <[email protected]>

Add helper function that ensures the floating point registers for the current task are active. Use with preemption disabled.

---
 arch/x86/include/asm/fpu/internal.h | 27 ++++++++++++++++++++++-----
 1 file changed, 22 insertions(+), 5 deletions(-)
arch/x86/include/asm/fpu/internal.h +22 -5
index a38bf5a1e37ad..16c4077ffc945 100644
@@ -514,6 +514,26 @@ static inline void fpregs_activate(struct fpu *fpu)
 	trace_x86_fpu_regs_activated(fpu);
 }
 
+/*
+ * Load the FPU state for the current task. Call with preemption disabled.
+ */
+static inline void __fpregs_load_activate(struct fpu *fpu, int cpu)
+{
+	if (!fpregs_state_valid(fpu, cpu))
+		copy_kernel_to_fpregs(&fpu->state);
+	fpregs_activate(fpu);
+}
+
+static inline void __fpregs_changes_begin(void)
+{
+	preempt_disable();
+}
+
+static inline void __fpregs_changes_end(void)
+{
+	preempt_enable();
+}
+
 /*
  * FPU state switching for scheduling.
  *
@@ -553,11 +573,8 @@ static inline void switch_fpu_finish(struct fpu *new_fpu, int cpu)
 	bool preload = static_cpu_has(X86_FEATURE_FPU) &&
 		       new_fpu->initialized;
 
-	if (preload) {
-		if (!fpregs_state_valid(new_fpu, cpu))
-			copy_kernel_to_fpregs(&new_fpu->state);
-		fpregs_activate(new_fpu);
-	}
+	if (preload)
+		__fpregs_load_activate(new_fpu, cpu);
 }
 
 /*
Signature · 1 line
2.19.0
#4 lore raw

PATCH v3 3/11 x86/fpu: make __raw_xsave_addr() use feature number instead of mask

In reply to Sebastian Andrzej Siewior

Most users of __raw_xsave_addr() use a feature number, shift it to a mask and then __raw_xsave_addr() shifts it back to the feature number.

Make __raw_xsave_addr() use the feature number as argument.

---
 arch/x86/kernel/fpu/xstate.c | 16 ++++++++--------
 1 file changed, 8 insertions(+), 8 deletions(-)
arch/x86/kernel/fpu/xstate.c +8 -8
index 87a57b7642d36..38d0b5ea40425 100644
@@ -811,10 +811,8 @@ void fpu__resume_cpu(void)
  *
  * Note: does not work for compacted buffers.
  */
-void *__raw_xsave_addr(struct xregs_state *xsave, int xstate_feature_mask)
+void *__raw_xsave_addr(struct xregs_state *xsave, int feature_nr)
 {
-	int feature_nr = fls64(xstate_feature_mask) - 1;
-
 	if (!xfeature_enabled(feature_nr)) {
 		WARN_ON_FPU(1);
 		return NULL;
@@ -842,6 +840,7 @@ void *__raw_xsave_addr(struct xregs_state *xsave, int xstate_feature_mask)
  */
 void *get_xsave_addr(struct xregs_state *xsave, int xstate_feature)
 {
+	int feature_nr;
 	/*
 	 * Do we even *have* xsave state?
 	 */
@@ -869,7 +868,8 @@ void *get_xsave_addr(struct xregs_state *xsave, int xstate_feature)
 	if (!(xsave->header.xfeatures & xstate_feature))
 		return NULL;
 
-	return __raw_xsave_addr(xsave, xstate_feature);
+	feature_nr = fls64(xstate_feature) - 1;
+	return __raw_xsave_addr(xsave, feature_nr);
 }
 EXPORT_SYMBOL_GPL(get_xsave_addr);
 
@@ -1018,7 +1018,7 @@ int copy_xstate_to_kernel(void *kbuf, struct xregs_state *xsave, unsigned int of
 		 * Copy only in-use xstates:
 		 */
 		if ((header.xfeatures >> i) & 1) {
-			void *src = __raw_xsave_addr(xsave, 1 << i);
+			void *src = __raw_xsave_addr(xsave, i);
 
 			offset = xstate_offsets[i];
 			size = xstate_sizes[i];
@@ -1104,7 +1104,7 @@ int copy_xstate_to_user(void __user *ubuf, struct xregs_state *xsave, unsigned i
 		 * Copy only in-use xstates:
 		 */
 		if ((header.xfeatures >> i) & 1) {
-			void *src = __raw_xsave_addr(xsave, 1 << i);
+			void *src = __raw_xsave_addr(xsave, i);
 
 			offset = xstate_offsets[i];
 			size = xstate_sizes[i];
@@ -1161,7 +1161,7 @@ int copy_kernel_to_xstate(struct xregs_state *xsave, const void *kbuf)
 		u64 mask = ((u64)1 << i);
 
 		if (hdr.xfeatures & mask) {
-			void *dst = __raw_xsave_addr(xsave, 1 << i);
+			void *dst = __raw_xsave_addr(xsave, i);
 
 			offset = xstate_offsets[i];
 			size = xstate_sizes[i];
@@ -1215,7 +1215,7 @@ int copy_user_to_xstate(struct xregs_state *xsave, const void __user *ubuf)
 		u64 mask = ((u64)1 << i);
 
 		if (hdr.xfeatures & mask) {
-			void *dst = __raw_xsave_addr(xsave, 1 << i);
+			void *dst = __raw_xsave_addr(xsave, i);
 
 			offset = xstate_offsets[i];
 			size = xstate_sizes[i];
Signature · 1 line
2.19.0
#5 lore raw

Re: [PATCH 03/11] x86/fpu: make __raw_xsave_addr() use feature number instead of mask

Quoted 15 lines · Sebastian Andrzej Siewior

Most users of __raw_xsave_addr() use a feature number, shift it to a mask and then __raw_xsave_addr() shifts it back to the feature number.

Make __raw_xsave_addr() use the feature number as argument.

---
 arch/x86/kernel/fpu/xstate.c | 16 ++++++++--------
 1 file changed, 8 insertions(+), 8 deletions(-)
arch/x86/kernel/fpu/xstate.c +0 -0
index 87a57b7642d36..38d0b5ea40425 100644

arch/x86/kernel/fpu/xstate.c void fpu__resume_cpu(void) · line 814

Quoted 5 lines
arch/x86/kernel/fpu/xstate.c +1 -1
@@ -811,10 +811,8 @@ void fpu__resume_cpu(void)
  *
  * Note: does not work for compacted buffers.
  */
-void *__raw_xsave_addr(struct xregs_state *xsave, int xstate_feature_mask)
+void *__raw_xsave_addr(struct xregs_state *xsave, int feature_nr)

It might be clearer to offer both interfaces, since both are used?

Quoted 15 lines
arch/x86/kernel/fpu/xstate.c +1 -2
 {
-	int feature_nr = fls64(xstate_feature_mask) - 1;
-
 	if (!xfeature_enabled(feature_nr)) {
 		WARN_ON_FPU(1);
 		return NULL;
@@ -842,6 +840,7 @@ void *__raw_xsave_addr(struct xregs_state *xsave, int xstate_feature_mask)
  */
 void *get_xsave_addr(struct xregs_state *xsave, int xstate_feature)
 {
+	int feature_nr;
 	/*
 	 * Do we even *have* xsave state?
 	 */

arch/x86/kernel/fpu/xstate.c void *get_xsave_addr(struct xregs_state *xsave, int xstate_feature) · line 872

Quoted 6 lines
arch/x86/kernel/fpu/xstate.c +2 -1
@@ -869,7 +868,8 @@ void *get_xsave_addr(struct xregs_state *xsave, int xstate_feature)
 	if (!(xsave->header.xfeatures & xstate_feature))
 		return NULL;
 
-	return __raw_xsave_addr(xsave, xstate_feature);
+	feature_nr = fls64(xstate_feature) - 1;
+	return __raw_xsave_addr(xsave, feature_nr);

and then move that to a wrapper function that takes a mask?

Quoted 31 lines
arch/x86/kernel/fpu/xstate.c +3 -3
 }
 EXPORT_SYMBOL_GPL(get_xsave_addr);
 
@@ -1018,7 +1018,7 @@ int copy_xstate_to_kernel(void *kbuf, struct xregs_state *xsave, unsigned int of
 		 * Copy only in-use xstates:
 		 */
 		if ((header.xfeatures >> i) & 1) {
-			void *src = __raw_xsave_addr(xsave, 1 << i);
+			void *src = __raw_xsave_addr(xsave, i);
 
 			offset = xstate_offsets[i];
 			size = xstate_sizes[i];
@@ -1104,7 +1104,7 @@ int copy_xstate_to_user(void __user *ubuf, struct xregs_state *xsave, unsigned i
 		 * Copy only in-use xstates:
 		 */
 		if ((header.xfeatures >> i) & 1) {
-			void *src = __raw_xsave_addr(xsave, 1 << i);
+			void *src = __raw_xsave_addr(xsave, i);
 
 			offset = xstate_offsets[i];
 			size = xstate_sizes[i];
@@ -1161,7 +1161,7 @@ int copy_kernel_to_xstate(struct xregs_state *xsave, const void *kbuf)
 		u64 mask = ((u64)1 << i);
 
 		if (hdr.xfeatures & mask) {
-			void *dst = __raw_xsave_addr(xsave, 1 << i);
+			void *dst = __raw_xsave_addr(xsave, i);
 
 			offset = xstate_offsets[i];
 			size = xstate_sizes[i];

arch/x86/kernel/fpu/xstate.c int copy_user_to_xstate(struct xregs_state *xsave, const void __user *ubuf) · line 1221

Quoted 8 lines
arch/x86/kernel/fpu/xstate.c +1 -1
@@ -1215,7 +1215,7 @@ int copy_user_to_xstate(struct xregs_state *xsave, const void __user *ubuf)
 		u64 mask = ((u64)1 << i);
 
 		if (hdr.xfeatures & mask) {
-			void *dst = __raw_xsave_addr(xsave, 1 << i);
+			void *dst = __raw_xsave_addr(xsave, i);
 
 			offset = xstate_offsets[i];
 			size = xstate_sizes[i];

--
Cheers,
Christophe de Dinechin (IRC c3d)

#6 lore raw

Re: [PATCH 03/11] x86/fpu: make __raw_xsave_addr() use feature number instead of mask

Quoted 13 lines · Christophe de Dinechin

arch/x86/kernel/fpu/xstate.c void fpu__resume_cpu(void) · line 814

Quoted 10 lines
arch/x86/kernel/fpu/xstate.c +1 -1
index 87a57b7642d36..38d0b5ea40425 100644
@@ -811,10 +811,8 @@ void fpu__resume_cpu(void)
  *
  * Note: does not work for compacted buffers.
  */
-void *__raw_xsave_addr(struct xregs_state *xsave, int xstate_feature_mask)
+void *__raw_xsave_addr(struct xregs_state *xsave, int feature_nr)

It might be clearer to offer both interfaces, since both are used?

…

Quoted 9 lines

arch/x86/kernel/fpu/xstate.c void *get_xsave_addr(struct xregs_state *xsave, int xstate_feature) · line 872

Quoted 7 lines
arch/x86/kernel/fpu/xstate.c +2 -1
@@ -869,7 +868,8 @@ void *get_xsave_addr(struct xregs_state *xsave, int xstate_feature)
 	if (!(xsave->header.xfeatures & xstate_feature))
 		return NULL;
 
-	return __raw_xsave_addr(xsave, xstate_feature);
+	feature_nr = fls64(xstate_feature) - 1;
+	return __raw_xsave_addr(xsave, feature_nr);

and then move that to a wrapper function that takes a mask?

made another patch this fls is gone now and everyone is using a number instead of a mask.

Sebastian

maintainer
#7 lore raw

Re: [PATCH 03/11] x86/fpu: make __raw_xsave_addr() use feature number instead of mask

In reply to Sebastian Andrzej Siewior
Quoted 4 lines · Sebastian Andrzej Siewior

Most users of __raw_xsave_addr() use a feature number, shift it to a mask and then __raw_xsave_addr() shifts it back to the feature number.

Make __raw_xsave_addr() use the feature number as argument.

This generally looks like a nice cleanup. Thanks for taking a look at it!

arch/x86/kernel/fpu/xstate.c void fpu__resume_cpu(void) · line 815

Quoted 11 lines
arch/x86/kernel/fpu/xstate.c +1 -1
index 87a57b7642d36..38d0b5ea40425 100644
@@ -811,10 +811,8 @@ void fpu__resume_cpu(void)
  *
  * Note: does not work for compacted buffers.
  */
-void *__raw_xsave_addr(struct xregs_state *xsave, int xstate_feature_mask)
+void *__raw_xsave_addr(struct xregs_state *xsave, int feature_nr)
 {

Could we call this 'xfeature_nr' consistently?

Quoted 14 lines
arch/x86/kernel/fpu/xstate.c +1 -2
-	int feature_nr = fls64(xstate_feature_mask) - 1;
-
 	if (!xfeature_enabled(feature_nr)) {
 		WARN_ON_FPU(1);
 		return NULL;
@@ -842,6 +840,7 @@ void *__raw_xsave_addr(struct xregs_state *xsave, int xstate_feature_mask)
  */
 void *get_xsave_addr(struct xregs_state *xsave, int xstate_feature)
 {
+	int feature_nr;
 	/*
 	 * Do we even *have* xsave state?
 	 */

arch/x86/kernel/fpu/xstate.c void *get_xsave_addr(struct xregs_state *xsave, int xstate_feature) · line 873

Quoted 7 lines
arch/x86/kernel/fpu/xstate.c +2 -1
@@ -869,7 +868,8 @@ void *get_xsave_addr(struct xregs_state *xsave, int xstate_feature)
 	if (!(xsave->header.xfeatures & xstate_feature))
 		return NULL;
 
-	return __raw_xsave_addr(xsave, xstate_feature);
+	feature_nr = fls64(xstate_feature) - 1;
+	return __raw_xsave_addr(xsave, feature_nr);
 }

Should we also be using a feature number for get_xsave_addr()? In other words, could you take a look and see how widely we should be doing this kind of conversion and not just limit it to __raw_xsave_addr()?

#8 lore raw

Re: [PATCH 03/11] x86/fpu: make __raw_xsave_addr() use feature number instead of mask

Quoted 5 lines · Dave Hansen
Quoted 5 lines
arch/x86/kernel/fpu/xstate.c +0 -0
index 87a57b7642d36..38d0b5ea40425 100644
@@ -811,10 +811,8 @@ void fpu__resume_cpu(void)
Quoted 8 lines

arch/x86/kernel/fpu/xstate.c void fpu__resume_cpu(void) · line 815

Quoted 6 lines
arch/x86/kernel/fpu/xstate.c +1 -1
  *
  * Note: does not work for compacted buffers.
  */
-void *__raw_xsave_addr(struct xregs_state *xsave, int xstate_feature_mask)
+void *__raw_xsave_addr(struct xregs_state *xsave, int feature_nr)
 {

Could we call this 'xfeature_nr' consistently?

yes.

Quoted 3 lines

Should we also be using a feature number for get_xsave_addr()? In other words, could you take a look and see how widely we should be doing this kind of conversion and not just limit it to __raw_xsave_addr()?

this brings us to:

Subject: [PATCH] x86/fpu: Make get_xsave_field_ptr() and get_xsave_addr() use
feature number instead of mask

After changing the argument of __raw_xsave_addr() from a mask to number Dave suggested to check if it makes sense to do the same for get_xsave_addr(). As it turns out it does. Only get_xsave_addr() needs the mask to check if the requested feature is part of what is support/saved and then uses the number again. The shift operation is cheaper compared to "find last bit set". Also, the feature number uses less opcode space compared to the mask :)

Make get_xsave_addr() argument a xfeature number instead of mask and fix up its callers.
As part of this use xfeature_nr and xfeature_mask consistently. This results in changes to the kvm code as:

	feature -> xfeature_mask
	index -> xfeature_nr
---
 arch/x86/include/asm/fpu/xstate.h |  2 +-
 arch/x86/kernel/fpu/xstate.c      | 23 +++++++++++------------
 arch/x86/kernel/traps.c           |  2 +-
 arch/x86/kvm/x86.c                | 28 ++++++++++++++--------------
 arch/x86/mm/mpx.c                 |  6 +++---
 5 files changed, 30 insertions(+), 31 deletions(-)
arch/x86/include/asm/fpu/xstate.h +1 -1
index 48581988d78c7..a0b6062012ed7 100644
@@ -47,7 +47,7 @@ extern void __init update_regset_xstate_info(unsigned int size,
 
 void fpu__xstate_clear_all_cpu_caps(void);
 void *get_xsave_addr(struct xregs_state *xsave, int xstate);
-const void *get_xsave_field_ptr(int xstate_field);
+const void *get_xsave_field_ptr(int xfeature_nr);
 int using_compacted_format(void);
 int copy_xstate_to_kernel(void *kbuf, struct xregs_state *xsave, unsigned int offset, unsigned int size);
 int copy_xstate_to_user(void __user *ubuf, struct xregs_state *xsave, unsigned int offset, unsigned int size);
arch/x86/kernel/fpu/xstate.c +11 -12
index 3dfe3627deaf6..375226055a413 100644
@@ -832,15 +832,15 @@ static void *__raw_xsave_addr(struct xregs_state *xsave, int xfeature_nr)
  *
  * Inputs:
  *	xstate: the thread's storage area for all FPU data
- *	xstate_feature: state which is defined in xsave.h (e.g.
- *	XFEATURE_MASK_FP, XFEATURE_MASK_SSE, etc...)
+ *	xfeature_nr: state which is defined in xsave.h (e.g. XFEATURE_FP,
+ *	XFEATURE_SSE, etc...)
  * Output:
  *	address of the state in the xsave area, or NULL if the
  *	field is not present in the xsave buffer.
  */
-void *get_xsave_addr(struct xregs_state *xsave, int xstate_feature)
+void *get_xsave_addr(struct xregs_state *xsave, int xfeature_nr)
 {
-	int xfeature_nr;
+	u64 xfeature_mask = 1ULL << xfeature_nr;
 	/*
 	 * Do we even *have* xsave state?
 	 */
@@ -852,11 +852,11 @@ void *get_xsave_addr(struct xregs_state *xsave, int xstate_feature)
 	 * have not enabled.  Remember that pcntxt_mask is
 	 * what we write to the XCR0 register.
 	 */
-	WARN_ONCE(!(xfeatures_mask & xstate_feature),
+	WARN_ONCE(!(xfeatures_mask & xfeature_mask),
 		  "get of unsupported state");
 	/*
 	 * This assumes the last 'xsave*' instruction to
-	 * have requested that 'xstate_feature' be saved.
+	 * have requested that 'xfeature_mask' be saved.
 	 * If it did not, we might be seeing and old value
 	 * of the field in the buffer.
 	 *
@@ -865,10 +865,9 @@ void *get_xsave_addr(struct xregs_state *xsave, int xstate_feature)
 	 * or because the "init optimization" caused it
 	 * to not be saved.
 	 */
-	if (!(xsave->header.xfeatures & xstate_feature))
+	if (!(xsave->header.xfeatures & xfeature_mask))
 		return NULL;
 
-	xfeature_nr = fls64(xstate_feature) - 1;
 	return __raw_xsave_addr(xsave, xfeature_nr);
 }
 EXPORT_SYMBOL_GPL(get_xsave_addr);
@@ -884,13 +883,13 @@ EXPORT_SYMBOL_GPL(get_xsave_addr);
  * Note that this only works on the current task.
  *
  * Inputs:
- *	@xsave_state: state which is defined in xsave.h (e.g. XFEATURE_MASK_FP,
- *	XFEATURE_MASK_SSE, etc...)
+ *	@xfeature_nr: state which is defined in xsave.h (e.g. XFEATURE_FP,
+ *	XFEATURE_SSE, etc...)
  * Output:
  *	address of the state in the xsave area or NULL if the state
  *	is not present or is in its 'init state'.
  */
-const void *get_xsave_field_ptr(int xsave_state)
+const void *get_xsave_field_ptr(int xfeature_nr)
 {
 	struct fpu *fpu = &current->thread.fpu;
 
@@ -900,7 +899,7 @@ const void *get_xsave_field_ptr(int xsave_state)
 	 */
 	fpu__save(fpu);
 
-	return get_xsave_addr(&fpu->state.xsave, xsave_state);
+	return get_xsave_addr(&fpu->state.xsave, xfeature_nr);
 }
 
 #ifdef CONFIG_ARCH_HAS_PKEYS
arch/x86/kernel/traps.c +1 -1
index e6db475164ede..aa4703d12e084 100644
@@ -477,7 +477,7 @@ dotraplinkage void do_bounds(struct pt_regs *regs, long error_code)
 	 * which is all zeros which indicates MPX was not
 	 * responsible for the exception.
 	 */
-	bndcsr = get_xsave_field_ptr(XFEATURE_MASK_BNDCSR);
+	bndcsr = get_xsave_field_ptr(XFEATURE_BNDCSR);
 	if (!bndcsr)
 		goto exit_trap;
 
arch/x86/kvm/x86.c +14 -14
index ca717737347e6..78006ea3cf46f 100644
@@ -3533,15 +3533,15 @@ static void fill_xsave(u8 *dest, struct kvm_vcpu *vcpu)
 	 */
 	valid = xstate_bv & ~XFEATURE_MASK_FPSSE;
 	while (valid) {
-		u64 feature = valid & -valid;
-		int index = fls64(feature) - 1;
-		void *src = get_xsave_addr(xsave, feature);
+		u64 xfeature_mask = valid & -valid;
+		int xfeature_nr = fls64(xfeature_mask) - 1;
+		void *src = get_xsave_addr(xsave, xfeature_nr);
 
 		if (src) {
 			u32 size, offset, ecx, edx;
-			cpuid_count(XSTATE_CPUID, index,
+			cpuid_count(XSTATE_CPUID, xfeature_nr,
 				    &size, &offset, &ecx, &edx);
-			if (feature == XFEATURE_MASK_PKRU)
+			if (xfeature_nr == XFEATURE_PKRU)
 				memcpy(dest + offset, &vcpu->arch.pkru,
 				       sizeof(vcpu->arch.pkru));
 			else
@@ -3549,7 +3549,7 @@ static void fill_xsave(u8 *dest, struct kvm_vcpu *vcpu)
 
 		}
 
-		valid -= feature;
+		valid -= xfeature_mask;
 	}
 }
 
@@ -3576,22 +3576,22 @@ static void load_xsave(struct kvm_vcpu *vcpu, u8 *src)
 	 */
 	valid = xstate_bv & ~XFEATURE_MASK_FPSSE;
 	while (valid) {
-		u64 feature = valid & -valid;
-		int index = fls64(feature) - 1;
-		void *dest = get_xsave_addr(xsave, feature);
+		u64 xfeature_mask = valid & -valid;
+		int xfeature_nr = fls64(xfeature_mask) - 1;
+		void *dest = get_xsave_addr(xsave, xfeature_nr);
 
 		if (dest) {
 			u32 size, offset, ecx, edx;
-			cpuid_count(XSTATE_CPUID, index,
+			cpuid_count(XSTATE_CPUID, xfeature_nr,
 				    &size, &offset, &ecx, &edx);
-			if (feature == XFEATURE_MASK_PKRU)
+			if (xfeature_nr == XFEATURE_PKRU)
 				memcpy(&vcpu->arch.pkru, src + offset,
 				       sizeof(vcpu->arch.pkru));
 			else
 				memcpy(dest, src + offset, size);
 		}
 
-		valid -= feature;
+		valid -= xfeature_mask;
 	}
 }
 
@@ -8562,11 +8562,11 @@ void kvm_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)
 		if (init_event)
 			kvm_put_guest_fpu(vcpu);
 		mpx_state_buffer = get_xsave_addr(&vcpu->arch.guest_fpu.state.xsave,
-					XFEATURE_MASK_BNDREGS);
+					XFEATURE_BNDREGS);
 		if (mpx_state_buffer)
 			memset(mpx_state_buffer, 0, sizeof(struct mpx_bndreg_state));
 		mpx_state_buffer = get_xsave_addr(&vcpu->arch.guest_fpu.state.xsave,
-					XFEATURE_MASK_BNDCSR);
+					XFEATURE_BNDCSR);
 		if (mpx_state_buffer)
 			memset(mpx_state_buffer, 0, sizeof(struct mpx_bndcsr));
 		if (init_event)
arch/x86/mm/mpx.c +3 -3
index e500949bae245..a50c3fe11c6ff 100644
@@ -145,7 +145,7 @@ siginfo_t *mpx_generate_siginfo(struct pt_regs *regs)
 		goto err_out;
 	}
 	/* get bndregs field from current task's xsave area */
-	bndregs = get_xsave_field_ptr(XFEATURE_MASK_BNDREGS);
+	bndregs = get_xsave_field_ptr(XFEATURE_BNDREGS);
 	if (!bndregs) {
 		err = -EINVAL;
 		goto err_out;
@@ -202,7 +202,7 @@ static __user void *mpx_get_bounds_dir(void)
 	 * The bounds directory pointer is stored in a register
 	 * only accessible if we first do an xsave.
 	 */
-	bndcsr = get_xsave_field_ptr(XFEATURE_MASK_BNDCSR);
+	bndcsr = get_xsave_field_ptr(XFEATURE_BNDCSR);
 	if (!bndcsr)
 		return MPX_INVALID_BOUNDS_DIR;
 
@@ -388,7 +388,7 @@ static int do_mpx_bt_fault(void)
 	const struct mpx_bndcsr *bndcsr;
 	struct mm_struct *mm = current->mm;
 
-	bndcsr = get_xsave_field_ptr(XFEATURE_MASK_BNDCSR);
+	bndcsr = get_xsave_field_ptr(XFEATURE_BNDCSR);
 	if (!bndcsr)
 		return -EINVAL;
 	/*
Signature · 4 lines
2.19.1


Sebastian
#9 lore raw

Re: [PATCH 03/11] x86/fpu: make __raw_xsave_addr() use feature number instead of mask

arch/x86/include/asm/fpu/xstate.h extern void __init update_regset_xstate_info(unsigned int size, · line 49

Quoted 6 lines · To Dave Hansen
arch/x86/include/asm/fpu/xstate.h +0 -0
@@ -47,7 +47,7 @@ extern void __init update_regset_xstate_info(unsigned int size,
 
 void fpu__xstate_clear_all_cpu_caps(void);
 void *get_xsave_addr(struct xregs_state *xsave, int xstate);

and now I locally changed xstate to xfeature_nr.

Sebastian

maintainer
#10 lore raw

Re: [PATCH 03/11] x86/fpu: make __raw_xsave_addr() use feature number instead of mask

In reply to Sebastian Andrzej Siewior
Quoted 15 lines · Sebastian Andrzej Siewior

Most users of __raw_xsave_addr() use a feature number, shift it to a mask and then __raw_xsave_addr() shifts it back to the feature number.

Make __raw_xsave_addr() use the feature number as argument.

---
 arch/x86/kernel/fpu/xstate.c | 16 ++++++++--------
 1 file changed, 8 insertions(+), 8 deletions(-)
arch/x86/kernel/fpu/xstate.c +0 -0
index 87a57b7642d36..38d0b5ea40425 100644

arch/x86/kernel/fpu/xstate.c void fpu__resume_cpu(void) · line 814

Quoted 5 lines
arch/x86/kernel/fpu/xstate.c +1 -1
@@ -811,10 +811,8 @@ void fpu__resume_cpu(void)
  *
  * Note: does not work for compacted buffers.
  */
-void *__raw_xsave_addr(struct xregs_state *xsave, int xstate_feature_mask)
+void *__raw_xsave_addr(struct xregs_state *xsave, int feature_nr)

Don't forget to update the comment above it too - it still says "mask".

Otherwise nice.

Signature · 4 lines
Regards/Gruss,
    Boris.

Good mailing practices for 400: avoid top-posting and trim the reply.
#11 lore raw

Re: [PATCH 03/11] x86/fpu: make __raw_xsave_addr() use feature number instead of mask

Quoted 4 lines · Borislav Petkov

arch/x86/kernel/fpu/xstate.c void fpu__resume_cpu(void) · line 814

Quoted 2 lines
arch/x86/kernel/fpu/xstate.c +1 -1
-void *__raw_xsave_addr(struct xregs_state *xsave, int xstate_feature_mask)
+void *__raw_xsave_addr(struct xregs_state *xsave, int feature_nr)

Don't forget to update the comment above it too - it still says "mask".

done.

Quoted 1 line

Otherwise nice.

thanks.

Sebastian

#12 lore raw

PATCH v3 4/11 x86/fpu: eager switch PKRU state

In reply to Sebastian Andrzej Siewior

From: Rik van Riel <[email protected]>

While most of a task's FPU state is only needed in user space, the protection keys need to be in place immediately after a context switch.

The reason is that any accesses to userspace memory while running in kernel mode also need to abide by the memory permissions specified in the protection keys.

The "eager switch" is a preparation for loading the FPU state on return to userland. Instead of decoupling PKRU state from xstate I update PKRU within xstate on write operations by the kernel.

[bigeasy: save pkru to xstate, no cache]
---
 arch/x86/include/asm/fpu/internal.h | 20 +++++++++++++++----
 arch/x86/include/asm/fpu/xstate.h   |  2 ++
 arch/x86/include/asm/pgtable.h      |  6 +-----
 arch/x86/include/asm/pkeys.h        |  2 +-
 arch/x86/kernel/fpu/core.c          |  2 +-
 arch/x86/mm/pkeys.c                 | 31 ++++++++++++++++++++++-------
 include/linux/pkeys.h               |  2 +-
 7 files changed, 46 insertions(+), 19 deletions(-)
arch/x86/include/asm/fpu/internal.h +16 -4
index 16c4077ffc945..956d967ca824a 100644
@@ -570,11 +570,23 @@ switch_fpu_prepare(struct fpu *old_fpu, int cpu)
  */
 static inline void switch_fpu_finish(struct fpu *new_fpu, int cpu)
 {
-	bool preload = static_cpu_has(X86_FEATURE_FPU) &&
-		       new_fpu->initialized;
+	bool load_fpu;
 
-	if (preload)
-		__fpregs_load_activate(new_fpu, cpu);
+	load_fpu = static_cpu_has(X86_FEATURE_FPU) && new_fpu->initialized;
+	if (!load_fpu)
+		return;
+
+	__fpregs_load_activate(new_fpu, cpu);
+
+#ifdef CONFIG_X86_INTEL_MEMORY_PROTECTION_KEYS
+	if (static_cpu_has(X86_FEATURE_OSPKE)) {
+		struct pkru_state *pk;
+
+		pk = __raw_xsave_addr(&new_fpu->state.xsave, XFEATURE_PKRU);
+		if (pk->pkru != __read_pkru())
+			__write_pkru(pk->pkru);
+	}
+#endif
 }
 
 /*
arch/x86/include/asm/fpu/xstate.h +2 -0
index 48581988d78c7..82227e222ca35 100644
@@ -5,6 +5,7 @@
 #include <linux/types.h>
 #include <asm/processor.h>
 #include <linux/uaccess.h>
+#include <asm/user.h>
 
 /* Bit 63 of XCR0 is reserved for future expansion */
 #define XFEATURE_MASK_EXTEND	(~(XFEATURE_MASK_FPSSE | (1ULL << 63)))
@@ -47,6 +48,7 @@ extern void __init update_regset_xstate_info(unsigned int size,
 
 void fpu__xstate_clear_all_cpu_caps(void);
 void *get_xsave_addr(struct xregs_state *xsave, int xstate);
+void *__raw_xsave_addr(struct xregs_state *xsave, int feature_nr);
 const void *get_xsave_field_ptr(int xstate_field);
 int using_compacted_format(void);
 int copy_xstate_to_kernel(void *kbuf, struct xregs_state *xsave, unsigned int offset, unsigned int size);
arch/x86/include/asm/pgtable.h +1 -5
index 690c0307afed0..80be15ba656f6 100644
@@ -132,11 +132,7 @@ static inline u32 read_pkru(void)
 	return 0;
 }
 
-static inline void write_pkru(u32 pkru)
-{
-	if (boot_cpu_has(X86_FEATURE_OSPKE))
-		__write_pkru(pkru);
-}
+void write_pkru(u32 pkru);
 
 static inline int pte_young(pte_t pte)
 {
arch/x86/include/asm/pkeys.h +1 -1
index 19b137f1b3beb..b184f916319e5 100644
@@ -119,7 +119,7 @@ extern int arch_set_user_pkey_access(struct task_struct *tsk, int pkey,
 		unsigned long init_val);
 extern int __arch_set_user_pkey_access(struct task_struct *tsk, int pkey,
 		unsigned long init_val);
-extern void copy_init_pkru_to_fpregs(void);
+extern void pkru_set_init_value(void);
 
 static inline int vma_pkey(struct vm_area_struct *vma)
 {
arch/x86/kernel/fpu/core.c +1 -1
index 2ea85b32421a0..72cd2e2a07194 100644
@@ -373,7 +373,7 @@ static inline void copy_init_fpstate_to_fpregs(void)
 		copy_kernel_to_fregs(&init_fpstate.fsave);
 
 	if (boot_cpu_has(X86_FEATURE_OSPKE))
-		copy_init_pkru_to_fpregs();
+		pkru_set_init_value();
 }
 
 /*
arch/x86/mm/pkeys.c +24 -7
index 6e98e0a7c9231..4409ada551c5e 100644
@@ -18,6 +18,7 @@
 
 #include <asm/cpufeature.h>             /* boot_cpu_has, ...            */
 #include <asm/mmu_context.h>            /* vma_pkey()                   */
+#include <asm/fpu/xstate.h>
 
 int __execute_only_pkey(struct mm_struct *mm)
 {
@@ -123,6 +124,24 @@ int __arch_override_mprotect_pkey(struct vm_area_struct *vma, int prot, int pkey
 	return vma_pkey(vma);
 }
 
+void write_pkru(u32 pkru)
+{
+	struct pkru_state *pk;
+
+	if (!boot_cpu_has(X86_FEATURE_OSPKE))
+		return;
+
+	pk = __raw_xsave_addr(&current->thread.fpu.state.xsave, XFEATURE_PKRU);
+	/*
+	 * Update the PKRU value in cstate and then in the CPU. A context
+	 * switch between those two operation would load the new value from the
+	 * updated xstate and then we would write (the same value) to the CPU.
+	 */
+	pk->pkru = pkru;
+	__write_pkru(pkru);
+
+}
+
 #define PKRU_AD_KEY(pkey)	(PKRU_AD_BIT << ((pkey) * PKRU_BITS_PER_PKEY))
 
 /*
@@ -143,20 +162,18 @@ u32 init_pkru_value = PKRU_AD_KEY( 1) | PKRU_AD_KEY( 2) | PKRU_AD_KEY( 3) |
  * we know the FPU regstiers are safe for use and we can use PKRU
  * directly.
  */
-void copy_init_pkru_to_fpregs(void)
+void pkru_set_init_value(void)
 {
 	u32 init_pkru_value_snapshot = READ_ONCE(init_pkru_value);
+
 	/*
 	 * Any write to PKRU takes it out of the XSAVE 'init
 	 * state' which increases context switch cost.  Avoid
-	 * writing 0 when PKRU was already 0.
+	 * writing then same value which is already written.
 	 */
-	if (!init_pkru_value_snapshot && !read_pkru())
+	if (init_pkru_value_snapshot == read_pkru())
 		return;
-	/*
-	 * Override the PKRU state that came from 'init_fpstate'
-	 * with the baseline from the process.
-	 */
+
 	write_pkru(init_pkru_value_snapshot);
 }
 
include/linux/pkeys.h +1 -1
index 2955ba9760489..9a9efecc1388f 100644
@@ -44,7 +44,7 @@ static inline bool arch_pkeys_enabled(void)
 	return false;
 }
 
-static inline void copy_init_pkru_to_fpregs(void)
+static inline void pkru_set_init_value(void)
 {
 }
 
Signature · 1 line
2.19.0
maintainer
#13 lore raw

Re: [PATCH 04/11] x86/fpu: eager switch PKRU state

Quoted 34 lines · Sebastian Andrzej Siewior

From: Rik van Riel <[email protected]>

While most of a task's FPU state is only needed in user space, the protection keys need to be in place immediately after a context switch.

The reason is that any accesses to userspace memory while running in kernel mode also need to abide by the memory permissions specified in the protection keys.

The "eager switch" is a preparation for loading the FPU state on return to userland. Instead of decoupling PKRU state from xstate I update PKRU within xstate on write operations by the kernel.

[bigeasy: save pkru to xstate, no cache]
---
 arch/x86/include/asm/fpu/internal.h | 20 +++++++++++++++----
 arch/x86/include/asm/fpu/xstate.h   |  2 ++
 arch/x86/include/asm/pgtable.h      |  6 +-----
 arch/x86/include/asm/pkeys.h        |  2 +-
 arch/x86/kernel/fpu/core.c          |  2 +-
 arch/x86/mm/pkeys.c                 | 31 ++++++++++++++++++++++-------
 include/linux/pkeys.h               |  2 +-
 7 files changed, 46 insertions(+), 19 deletions(-)
arch/x86/include/asm/fpu/internal.h +0 -0
index 16c4077ffc945..956d967ca824a 100644
@@ -570,11 +570,23 @@ switch_fpu_prepare(struct fpu *old_fpu, int cpu)
  */
 static inline void switch_fpu_finish(struct fpu *new_fpu, int cpu)

arch/x86/include/asm/fpu/internal.h switch_fpu_prepare(struct fpu *old_fpu, int cpu) · line 577

Quoted 10 lines
arch/x86/include/asm/fpu/internal.h +4 -4
 {
-	bool preload = static_cpu_has(X86_FEATURE_FPU) &&
-		       new_fpu->initialized;
+	bool load_fpu;
 
-	if (preload)
-		__fpregs_load_activate(new_fpu, cpu);
+	load_fpu = static_cpu_has(X86_FEATURE_FPU) && new_fpu->initialized;
+	if (!load_fpu)
+		return;

Needs comments, please. Especially around what an uninitialized new_fpu means.

arch/x86/include/asm/fpu/internal.h switch_fpu_prepare(struct fpu *old_fpu, int cpu) · line 582

Quoted 4 lines
arch/x86/include/asm/fpu/internal.h +4 -0
+	__fpregs_load_activate(new_fpu, cpu);
+
+#ifdef CONFIG_X86_INTEL_MEMORY_PROTECTION_KEYS
+	if (static_cpu_has(X86_FEATURE_OSPKE)) {

FWIW, you should be able to use cpu_feature_enabled() instead of an explicit #ifdef here.

arch/x86/include/asm/fpu/internal.h switch_fpu_prepare(struct fpu *old_fpu, int cpu) · line 590

Quoted 8 lines
arch/x86/include/asm/fpu/internal.h +7 -0
+		struct pkru_state *pk;
+
+		pk = __raw_xsave_addr(&new_fpu->state.xsave, XFEATURE_PKRU);
+		if (pk->pkru != __read_pkru())
+			__write_pkru(pk->pkru);
+	}
+#endif
 }

Comments here as well, please.

I think the goal is to keep the PKRU state in the 'init state' when possible and also to save the cost of WRPKRU. But, it would be really nice to be explicit.

arch/x86/include/asm/fpu/internal.h switch_fpu_prepare(struct fpu *old_fpu, int cpu)

Quoted 6 lines
arch/x86/include/asm/fpu/internal.h +1 -5
-static inline void write_pkru(u32 pkru)
-{
-	if (boot_cpu_has(X86_FEATURE_OSPKE))
-		__write_pkru(pkru);
-}
+void write_pkru(u32 pkru);

One reason I inlined this was because it enables the the PK code to be optimized away entirely. Putting the checks behind a function call makes this optimization impossible.

Could you elaborate on why you chose to do this and what you think the impact is or is not?

arch/x86/include/asm/pkeys.h extern int arch_set_user_pkey_access(struct task_struct *tsk, int pkey, · line 122

Quoted 10 lines
arch/x86/include/asm/pkeys.h +1 -1
index 19b137f1b3beb..b184f916319e5 100644
@@ -119,7 +119,7 @@ extern int arch_set_user_pkey_access(struct task_struct *tsk, int pkey,
 		unsigned long init_val);
 extern int __arch_set_user_pkey_access(struct task_struct *tsk, int pkey,
 		unsigned long init_val);
-extern void copy_init_pkru_to_fpregs(void);
+extern void pkru_set_init_value(void);

Could you elaborate on why the name is being changed?

arch/x86/include/asm/pkeys.h extern int arch_set_user_pkey_access(struct task_struct *tsk, int pkey,

Quoted 10 lines
arch/x86/include/asm/pkeys.h +10 -0
+void write_pkru(u32 pkru)
+{
+	struct pkru_state *pk;
+
+	if (!boot_cpu_has(X86_FEATURE_OSPKE))
+		return;
+
+	pk = __raw_xsave_addr(&current->thread.fpu.state.xsave, XFEATURE_PKRU);
+	/*
+	 * Update the PKRU value in cstate and then in the CPU. A context

"cstate"? Did you mean xstate?

arch/x86/include/asm/pkeys.h

Quoted 7 lines
arch/x86/include/asm/pkeys.h +7 -0
+	 * switch between those two operation would load the new value from the
+	 * updated xstate and then we would write (the same value) to the CPU.
+	 */
+	pk->pkru = pkru;
+	__write_pkru(pkru);
+
+}

There's an unnecessary line there.

This also needs a lot more high-level context about why it is necessary.
I think you had that in the changelog, but we also need the function
commented.

arch/x86/include/asm/pkeys.h

Quoted 11 lines
arch/x86/include/asm/pkeys.h +3 -2
-void copy_init_pkru_to_fpregs(void)
+void pkru_set_init_value(void)
 {
 	u32 init_pkru_value_snapshot = READ_ONCE(init_pkru_value);
+
 	/*
 	 * Any write to PKRU takes it out of the XSAVE 'init
 	 * state' which increases context switch cost.  Avoid
-	 * writing 0 when PKRU was already 0.
+	 * writing then same value which is already written.
 	 */

s/then/the/

arch/x86/include/asm/pkeys.h

Quoted 10 lines
arch/x86/include/asm/pkeys.h +2 -5
-	if (!init_pkru_value_snapshot && !read_pkru())
+	if (init_pkru_value_snapshot == read_pkru())
 		return;
-	/*
-	 * Override the PKRU state that came from 'init_fpstate'
-	 * with the baseline from the process.
-	 */
+
 	write_pkru(init_pkru_value_snapshot);
 }

Isn't this doing some of the same work (including rdpkru()) as write_pkru()?

maintainer
#14 lore raw

Re: [PATCH 04/11] x86/fpu: eager switch PKRU state

Quoted 41 lines · Dave Hansen
Quoted 39 lines · Sebastian Andrzej Siewior

From: Rik van Riel <[email protected]>

While most of a task's FPU state is only needed in user space, the protection keys need to be in place immediately after a context switch.

The reason is that any accesses to userspace memory while running in kernel mode also need to abide by the memory permissions specified in the protection keys.

The "eager switch" is a preparation for loading the FPU state on return to userland. Instead of decoupling PKRU state from xstate I update PKRU within xstate on write operations by the kernel.

[bigeasy: save pkru to xstate, no cache]
---
 arch/x86/include/asm/fpu/internal.h | 20 +++++++++++++++----
 arch/x86/include/asm/fpu/xstate.h   |  2 ++
 arch/x86/include/asm/pgtable.h      |  6 +-----
 arch/x86/include/asm/pkeys.h        |  2 +-
 arch/x86/kernel/fpu/core.c          |  2 +-
 arch/x86/mm/pkeys.c                 | 31 ++++++++++++++++++++++-------
 include/linux/pkeys.h               |  2 +-
 7 files changed, 46 insertions(+), 19 deletions(-)
arch/x86/include/asm/fpu/internal.h +1 -2
index 16c4077ffc945..956d967ca824a 100644
@@ -570,11 +570,23 @@ switch_fpu_prepare(struct fpu *old_fpu, int cpu)
  */
 static inline void switch_fpu_finish(struct fpu *new_fpu, int cpu)
 {
-     bool preload = static_cpu_has(X86_FEATURE_FPU) &&
-                    new_fpu->initialized;
+     bool load_fpu;
 
Quoted 8 lines

arch/x86/include/asm/fpu/internal.h switch_fpu_prepare(struct fpu *old_fpu, int cpu) · line 577

Quoted 5 lines
arch/x86/include/asm/fpu/internal.h +3 -2
-     if (preload)
-             __fpregs_load_activate(new_fpu, cpu);
+     load_fpu = static_cpu_has(X86_FEATURE_FPU) && new_fpu->initialized;
+     if (!load_fpu)
+             return;

Needs comments, please. Especially around what an uninitialized new_fpu means.

See my other comment about getting rid of ->initialized *first*.

Quoted 20 lines

arch/x86/include/asm/fpu/internal.h switch_fpu_prepare(struct fpu *old_fpu, int cpu) · line 582

Quoted 5 lines
arch/x86/include/asm/fpu/internal.h +4 -0
+     __fpregs_load_activate(new_fpu, cpu);
+
+#ifdef CONFIG_X86_INTEL_MEMORY_PROTECTION_KEYS
+     if (static_cpu_has(X86_FEATURE_OSPKE)) {

FWIW, you should be able to use cpu_feature_enabled() instead of an explicit #ifdef here.

arch/x86/include/asm/fpu/internal.h switch_fpu_prepare(struct fpu *old_fpu, int cpu) · line 590

Quoted 8 lines
arch/x86/include/asm/fpu/internal.h +7 -0
+             struct pkru_state *pk;
+
+             pk = __raw_xsave_addr(&new_fpu->state.xsave, XFEATURE_PKRU);
+             if (pk->pkru != __read_pkru())
+                     __write_pkru(pk->pkru);
+     }
+#endif
 }

Comments here as well, please.

Quoted 3 lines

I think the goal is to keep the PKRU state in the 'init state' when possible and also to save the cost of WRPKRU. But, it would be really nice to be explicit.

I suspect that this makes basically no difference. PKRU is almost never in the init state on Linux. Also, it's a single word -- I doubt that the init state optimization is worth much.

But maybe WRPKRU is more expensive than RDPKRU and a branch?

maintainer
#15 lore raw

Re: [PATCH 04/11] x86/fpu: eager switch PKRU state

Quoted 1 line · Andy Lutomirski

But maybe WRPKRU is more expensive than RDPKRU and a branch?

Yeah, it is more expensive. It has a higher cycle cost and it's also practically a (light) speculation barrier.

#16 lore raw

Re: [PATCH 04/11] x86/fpu: eager switch PKRU state

In reply to Dave Hansen
Quoted 12 lines · Dave Hansen
Quoted 12 lines
arch/x86/include/asm/fpu/internal.h +1 -2
index 16c4077ffc945..956d967ca824a 100644
@@ -570,11 +570,23 @@ switch_fpu_prepare(struct fpu *old_fpu, int cpu)
  */
 static inline void switch_fpu_finish(struct fpu *new_fpu, int cpu)
 {
-	bool preload = static_cpu_has(X86_FEATURE_FPU) &&
-		       new_fpu->initialized;
+	bool load_fpu;
 
Quoted 8 lines

arch/x86/include/asm/fpu/internal.h switch_fpu_prepare(struct fpu *old_fpu, int cpu) · line 577

Quoted 5 lines
arch/x86/include/asm/fpu/internal.h +3 -2
-	if (preload)
-		__fpregs_load_activate(new_fpu, cpu);
+	load_fpu = static_cpu_has(X86_FEATURE_FPU) && new_fpu->initialized;
+	if (!load_fpu)
+		return;

Needs comments, please. Especially around what an uninitialized new_fpu means.

that ->initialized field is gone.

Quoted 7 lines

arch/x86/include/asm/fpu/internal.h switch_fpu_prepare(struct fpu *old_fpu, int cpu) · line 582

Quoted 4 lines
arch/x86/include/asm/fpu/internal.h +4 -0
+	__fpregs_load_activate(new_fpu, cpu);
+
+#ifdef CONFIG_X86_INTEL_MEMORY_PROTECTION_KEYS
+	if (static_cpu_has(X86_FEATURE_OSPKE)) {

FWIW, you should be able to use cpu_feature_enabled() instead of an explicit #ifdef here.

okay.

Quoted 11 lines

arch/x86/include/asm/fpu/internal.h switch_fpu_prepare(struct fpu *old_fpu, int cpu) · line 590

Quoted 8 lines
arch/x86/include/asm/fpu/internal.h +7 -0
+		struct pkru_state *pk;
+
+		pk = __raw_xsave_addr(&new_fpu->state.xsave, XFEATURE_PKRU);
+		if (pk->pkru != __read_pkru())
+			__write_pkru(pk->pkru);
+	}
+#endif
 }

Comments here as well, please.

Quoted 3 lines

I think the goal is to keep the PKRU state in the 'init state' when possible and also to save the cost of WRPKRU. But, it would be really nice to be explicit.

added:
/*
* Writting PKRU is expensive. Only write the PKRU value if it is
* different from the current one.
*/

Quoted 11 lines

arch/x86/include/asm/fpu/internal.h switch_fpu_prepare(struct fpu *old_fpu, int cpu)

Quoted 6 lines
arch/x86/include/asm/fpu/internal.h +1 -5
-static inline void write_pkru(u32 pkru)
-{
-	if (boot_cpu_has(X86_FEATURE_OSPKE))
-		__write_pkru(pkru);
-}
+void write_pkru(u32 pkru);

One reason I inlined this was because it enables the the PK code to be optimized away entirely. Putting the checks behind a function call makes this optimization impossible.

Quoted 2 lines

Could you elaborate on why you chose to do this and what you think the impact is or is not?

One thing let to another. It gets to an include mess once I tried to do more than just reading / writting the value.
I kept now write_pkru() and added __write_pkru_if_new() which does the extra pieces. If you don't like the new version we would need to look on how to make it simpler :)

Quoted 4 lines
Quoted 4 lines
arch/x86/include/asm/pkeys.h +0 -0
index 19b137f1b3beb..b184f916319e5 100644
Quoted 8 lines

arch/x86/include/asm/pkeys.h extern int arch_set_user_pkey_access(struct task_struct *tsk, int pkey, · line 122

Quoted 6 lines
arch/x86/include/asm/pkeys.h +1 -1
@@ -119,7 +119,7 @@ extern int arch_set_user_pkey_access(struct task_struct *tsk, int pkey,
 		unsigned long init_val);
 extern int __arch_set_user_pkey_access(struct task_struct *tsk, int pkey,
 		unsigned long init_val);
-extern void copy_init_pkru_to_fpregs(void);
+extern void pkru_set_init_value(void);

Could you elaborate on why the name is being changed?

The function name read like init_pkru value is copied to fpregs save area which is not the case. I could revert it if you prefer.

Quoted 4 lines
Quoted 4 lines
arch/x86/include/asm/pkeys.h +4 -0
+void write_pkru(u32 pkru)
+{
+	struct pkru_state *pk;
+
Quoted 8 lines

arch/x86/include/asm/pkeys.h

Quoted 6 lines
arch/x86/include/asm/pkeys.h +6 -0
+	if (!boot_cpu_has(X86_FEATURE_OSPKE))
+		return;
+
+	pk = __raw_xsave_addr(&current->thread.fpu.state.xsave, XFEATURE_PKRU);
+	/*
+	 * Update the PKRU value in cstate and then in the CPU. A context

"cstate"? Did you mean xstate?

indeed.

Quoted 10 lines

arch/x86/include/asm/pkeys.h

Quoted 7 lines
arch/x86/include/asm/pkeys.h +7 -0
+	 * switch between those two operation would load the new value from the
+	 * updated xstate and then we would write (the same value) to the CPU.
+	 */
+	pk->pkru = pkru;
+	__write_pkru(pkru);
+
+}

There's an unnecessary line there.

Quoted 3 lines

This also needs a lot more high-level context about why it is necessary.
I think you had that in the changelog, but we also need the function
commented.

What is necessary? The manual update of "pk->pkru?

Quoted 4 lines
Quoted 4 lines
arch/x86/include/asm/pkeys.h +1 -2
-	if (!init_pkru_value_snapshot && !read_pkru())
+	if (init_pkru_value_snapshot == read_pkru())
 		return;
-	/*
Quoted 8 lines

arch/x86/include/asm/pkeys.h

Quoted 6 lines
arch/x86/include/asm/pkeys.h +1 -3
-	 * Override the PKRU state that came from 'init_fpstate'
-	 * with the baseline from the process.
-	 */
+
 	write_pkru(init_pkru_value_snapshot);
 }

Isn't this doing some of the same work (including rdpkru()) as write_pkru()?

Well, yes. Since my write_pkru() checks if the new value the same as the current I dropped most of the code here.

Sebastian

maintainer
#17 lore raw

Re: [PATCH 04/11] x86/fpu: eager switch PKRU state

Quoted 13 lines
Quoted 4 lines
Quoted 4 lines
arch/x86/include/asm/pkeys.h +0 -0
index 19b137f1b3beb..b184f916319e5 100644
Quoted 8 lines

arch/x86/include/asm/pkeys.h extern int arch_set_user_pkey_access(struct task_struct *tsk, int pkey, · line 122

Quoted 6 lines
arch/x86/include/asm/pkeys.h +1 -1
@@ -119,7 +119,7 @@ extern int arch_set_user_pkey_access(struct task_struct *tsk, int pkey,
 		unsigned long init_val);
 extern int __arch_set_user_pkey_access(struct task_struct *tsk, int pkey,
 		unsigned long init_val);
-extern void copy_init_pkru_to_fpregs(void);
+extern void pkru_set_init_value(void);

Could you elaborate on why the name is being changed?

Quoted 2 lines

The function name read like init_pkru value is copied to fpregs save area which is not the case. I could revert it if you prefer.

I would just prefer that we find some way of saying *where* pkru is being set. Is it fpstate, fpregs, or both?

Quoted 10 lines
Quoted 10 lines

arch/x86/include/asm/pkeys.h extern int arch_set_user_pkey_access(struct task_struct *tsk, int pkey,

Quoted 7 lines
arch/x86/include/asm/pkeys.h +7 -0
+	 * switch between those two operation would load the new value from the
+	 * updated xstate and then we would write (the same value) to the CPU.
+	 */
+	pk->pkru = pkru;
+	__write_pkru(pkru);
+
+}

There's an unnecessary line there.

Quoted 5 lines
Quoted 3 lines

This also needs a lot more high-level context about why it is necessary.
I think you had that in the changelog, but we also need the function
commented.

What is necessary? The manual update of "pk->pkru?

Why we need to update fpstate *and* the actual register.

#18 lore raw

PATCH v3 5/11 x86/fpu: set PKRU state for kernel threads

In reply to Sebastian Andrzej Siewior

The PKRU value is not set for kernel threads because they do not have the ->initialized value set. As a result the kernel thread has a random PKRU value set which it inherits from the previous task.
It has been suggested by Paolo Bonzini to set it for kernel threads, too because it might be a fix.
I *think* this is not required because the kernel threads don't copy data to/from userland and don't have access to any userspace mm in general.
However there is this use_mm(). If we gain a mm by use_mm() we don't have a matching PKRU value because those are per thread. It has been suggested to use 0 as the PKRU value but this would bypass PKRU.

Set the initial (default) PKRU value for kernel threads.

---
 arch/x86/include/asm/fpu/internal.h | 20 ++++++++++++--------
 1 file changed, 12 insertions(+), 8 deletions(-)
arch/x86/include/asm/fpu/internal.h +12 -8
index 956d967ca824a..4ecaf4d22954e 100644
@@ -14,6 +14,7 @@
 #include <linux/compat.h>
 #include <linux/sched.h>
 #include <linux/slab.h>
+#include <linux/pkeys.h>
 
 #include <asm/user.h>
 #include <asm/fpu/api.h>
@@ -573,20 +574,23 @@ static inline void switch_fpu_finish(struct fpu *new_fpu, int cpu)
 	bool load_fpu;
 
 	load_fpu = static_cpu_has(X86_FEATURE_FPU) && new_fpu->initialized;
-	if (!load_fpu)
-		return;
-
-	__fpregs_load_activate(new_fpu, cpu);
-
 #ifdef CONFIG_X86_INTEL_MEMORY_PROTECTION_KEYS
 	if (static_cpu_has(X86_FEATURE_OSPKE)) {
 		struct pkru_state *pk;
 
-		pk = __raw_xsave_addr(&new_fpu->state.xsave, XFEATURE_PKRU);
-		if (pk->pkru != __read_pkru())
-			__write_pkru(pk->pkru);
+		if (!load_fpu) {
+			pkru_set_init_value();
+		} else {
+			pk = __raw_xsave_addr(&new_fpu->state.xsave,
+					      XFEATURE_PKRU);
+			if (pk->pkru != __read_pkru())
+				__write_pkru(pk->pkru);
+		}
 	}
 #endif
+	if (!load_fpu)
+		return;
+	__fpregs_load_activate(new_fpu, cpu);
 }
 
 /*
Signature · 1 line
2.19.0
maintainer
#19 lore raw

Re: [PATCH 05/11] x86/fpu: set PKRU state for kernel threads

Quoted 12 lines · Sebastian Andrzej Siewior

The PKRU value is not set for kernel threads because they do not have the ->initialized value set. As a result the kernel thread has a random PKRU value set which it inherits from the previous task.
It has been suggested by Paolo Bonzini to set it for kernel threads, too because it might be a fix.
I *think* this is not required because the kernel threads don't copy data to/from userland and don't have access to any userspace mm in general.
However there is this use_mm(). If we gain a mm by use_mm() we don't have a matching PKRU value because those are per thread. It has been suggested to use 0 as the PKRU value but this would bypass PKRU.

Quoted 1 line

Set the initial (default) PKRU value for kernel threads.

We might want to do this for cleanliness reasons... Maybe.

But this *should* have no practical effects. Kernel threads have no real 'mm' and no user pages. They should not have do access to user mappings. Protection keys *only* apply to user mappings. Thus, logically, they should never be affected by PKRU values.

So I'm kinda missing the point of the patch.

maintainer
#20 lore raw

Re: [PATCH 05/11] x86/fpu: set PKRU state for kernel threads

Quoted 16 lines · Dave Hansen
Quoted 12 lines · Sebastian Andrzej Siewior

The PKRU value is not set for kernel threads because they do not have the ->initialized value set. As a result the kernel thread has a random PKRU value set which it inherits from the previous task.
It has been suggested by Paolo Bonzini to set it for kernel threads, too because it might be a fix.
I *think* this is not required because the kernel threads don't copy data to/from userland and don't have access to any userspace mm in general.
However there is this use_mm(). If we gain a mm by use_mm() we don't have a matching PKRU value because those are per thread. It has been suggested to use 0 as the PKRU value but this would bypass PKRU.

Quoted 1 line

Set the initial (default) PKRU value for kernel threads.

Quoted 8 lines

We might want to do this for cleanliness reasons... Maybe.

But this *should* have no practical effects. Kernel threads have no real 'mm' and no user pages. They should not have do access to user mappings. Protection keys *only* apply to user mappings. Thus, logically, they should never be affected by PKRU values.

So I'm kinda missing the point of the patch.

use_mm().

#21 lore raw

Re: [PATCH 05/11] x86/fpu: set PKRU state for kernel threads

Quoted 20 lines · Andy Lutomirski
Quoted 18 lines · Dave Hansen
Quoted 12 lines · Sebastian Andrzej Siewior

The PKRU value is not set for kernel threads because they do not have the ->initialized value set. As a result the kernel thread has a random PKRU value set which it inherits from the previous task.
It has been suggested by Paolo Bonzini to set it for kernel threads, too because it might be a fix.
I *think* this is not required because the kernel threads don't copy data to/from userland and don't have access to any userspace mm in general.
However there is this use_mm(). If we gain a mm by use_mm() we don't have a matching PKRU value because those are per thread. It has been suggested to use 0 as the PKRU value but this would bypass PKRU.

Quoted 1 line

Set the initial (default) PKRU value for kernel threads.

We might want to do this for cleanliness reasons... Maybe.

Quoted 8 lines
Quoted 6 lines

But this *should* have no practical effects. Kernel threads have no real 'mm' and no user pages. They should not have do access to user mappings. Protection keys *only* apply to user mappings. Thus, logically, they should never be affected by PKRU values.

So I'm kinda missing the point of the patch.

use_mm().

So. I would drop that patch from queue. Anyone feels different about it?

Sebastian

#22 lore raw

Re: [PATCH 05/11] x86/fpu: set PKRU state for kernel threads

Quoted 30 lines · Sebastian Andrzej Siewior
Quoted 27 lines · Andy Lutomirski
Quoted 23 lines · Dave Hansen
Quoted 14 lines · Sebastian Andrzej Siewior

The PKRU value is not set for kernel threads because they do not have the ->initialized value set. As a result the kernel thread has a random PKRU value set which it inherits from the previous task.
It has been suggested by Paolo Bonzini to set it for kernel threads, too because it might be a fix.
I *think* this is not required because the kernel threads don't copy data to/from userland and don't have access to any userspace mm in general.
However there is this use_mm(). If we gain a mm by use_mm() we don't have a matching PKRU value because those are per thread. It has been suggested to use 0 as the PKRU value but this would bypass PKRU.

Quoted 1 line

Set the initial (default) PKRU value for kernel threads.

We might want to do this for cleanliness reasons... Maybe.

But this *should* have no practical effects. Kernel threads have no real 'mm' and no user pages. They should not have do access to user mappings. Protection keys *only* apply to user mappings. Thus, logically, they should never be affected by PKRU values.

Quoted 6 lines
Quoted 3 lines
Quoted 1 line

So I'm kinda missing the point of the patch.

use_mm().

So. I would drop that patch from queue. Anyone feels different about it?

I think we *do* want the patch. It’s a bugfix for use_mm users, right?

Quoted 1 line

Sebastian

maintainer
#23 lore raw

Re: [PATCH 05/11] x86/fpu: set PKRU state for kernel threads

Quoted 7 lines · Andy Lutomirski
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

We might want to do this for cleanliness reasons... Maybe.

But this *should* have no practical effects. Kernel threads have no real 'mm' and no user pages. They should not have do access to user mappings. Protection keys *only* apply to user mappings. Thus, logically, they should never be affected by PKRU values.

Quoted 5 lines
Quoted 4 lines
Quoted 2 lines
Quoted 1 line

So I'm kinda missing the point of the patch.

use_mm().

So. I would drop that patch from queue. Anyone feels different about it?

I think we *do* want the patch. It’s a bugfix for use_mm users, right?

Yes, we need it. I was being dense and Andy kindly reminded me of the point of the patch.

#24 lore raw

Re: [PATCH 05/11] x86/fpu: set PKRU state for kernel threads

In reply to Andy Lutomirski
Quoted 11 lines · Andy Lutomirski
Quoted 9 lines · Sebastian Andrzej Siewior
Quoted 5 lines · Andy Lutomirski

On Fri, Oct 12, 2018 at 10:54 AM Dave Hansen

Quoted 1 line

So I'm kinda missing the point of the patch.

use_mm().

So. I would drop that patch from queue. Anyone feels different about it?

I think we *do* want the patch. It’s a bugfix for use_mm users, right?

This is the loophole that has been pointed out. I am not convinced what the correct behaviour should be here (and we have five users of that interface). For instance f_fs[0]. It reads data from the USB EP and then writes it to userland task. Due to $circumstances it happens in a workqueue instead of the task's context. So it borrows the mm with use_mm(). The current behaviour random because the PKRU value can not be predicted. It may or may not work.

Setting it to allow-all/none would let the operation always fail or succeed which might be an improvement in terms of debugging. However it is hard to judge what the correct behaviour should be. Should fail or succeed.
But this is not the only loophole: There is ptrace interface which is used by gdb (just checked) and also bypasses PKRU. So…

[0] drivers/usb/gadget/function/f_fs.c::ffs_user_copy_worker()

Sebastian

#25 lore raw

Re: [PATCH 05/11] x86/fpu: set PKRU state for kernel threads

Quoted 29 lines · Sebastian Andrzej Siewior
Quoted 11 lines · Andy Lutomirski
Quoted 9 lines · Sebastian Andrzej Siewior
Quoted 5 lines · Andy Lutomirski

On Fri, Oct 12, 2018 at 10:54 AM Dave Hansen

Quoted 1 line

So I'm kinda missing the point of the patch.

use_mm().

So. I would drop that patch from queue. Anyone feels different about it?

I think we *do* want the patch. It’s a bugfix for use_mm users, right?

This is the loophole that has been pointed out. I am not convinced what the correct behaviour should be here (and we have five users of that interface). For instance f_fs[0]. It reads data from the USB EP and then writes it to userland task. Due to $circumstances it happens in a workqueue instead of the task's context. So it borrows the mm with use_mm(). The current behaviour random because the PKRU value can not be predicted. It may or may not work.

Setting it to allow-all/none would let the operation always fail or succeed which might be an improvement in terms of debugging. However it is hard to judge what the correct behaviour should be. Should fail or succeed.
But this is not the only loophole: There is ptrace interface which is used by gdb (just checked) and also bypasses PKRU. So…

Quoted 3 lines

[0] drivers/usb/gadget/function/f_fs.c::ffs_user_copy_worker()

Sebastian

I think we need an entirely new API:

user_mm_ctx_t ctx = user_mm_ctx_get();

...

use_user_mm_ctx(ctx);
unuse_user_mm_ctx(ctx);

...

user_mm_ctx_put(ctx);

and ctx will store a copy of mm and PKRU.

maintainer
#26 lore raw

Re: [PATCH 05/11] x86/fpu: set PKRU state for kernel threads

Quoted 4 lines · Andy Lutomirski

Setting it to allow-all/none would let the operation always fail or succeed which might be an improvement in terms of debugging. However it is hard to judge what the correct behaviour should be. Should fail or succeed.

Succeed. :)

Quoted 2 lines

But this is not the only loophole: There is ptrace interface which is used by gdb (just checked) and also bypasses PKRU. So…

Bypassing protection keys is not a big deal IMNHO. In places where a sane one is not readily available, I'm totally fine with just effectively disabling it (PKRU=0) for the length of time it isn't available.

#27 lore raw

Re: [PATCH 05/11] x86/fpu: set PKRU state for kernel threads

Quoted 6 lines · Dave Hansen
Quoted 2 lines

But this is not the only loophole: There is ptrace interface which is used by gdb (just checked) and also bypasses PKRU. So…

Bypassing protection keys is not a big deal IMNHO. In places where a sane one is not readily available, I'm totally fine with just effectively disabling it (PKRU=0) for the length of time it isn't available.

Okay, this makes things easier. Let document that for kernel threads we use PKRU=0. This should be happening in my try right now. I double check tomorrow just in case…

Sebastian

#28 lore raw

Re: [PATCH 05/11] x86/fpu: set PKRU state for kernel threads

Quoted 8 lines · Sebastian Andrzej Siewior
Quoted 3 lines · Dave Hansen
Quoted 2 lines

But this is not the only loophole: There is ptrace interface which is used by gdb (just checked) and also bypasses PKRU. So…

Quoted 9 lines
Quoted 3 lines

Bypassing protection keys is not a big deal IMNHO. In places where a sane one is not readily available, I'm totally fine with just effectively disabling it (PKRU=0) for the length of time it isn't available.

Okay, this makes things easier. Let document that for kernel threads we use PKRU=0. This should be happening in my try right now. I double check tomorrow just in case…

If you document that, please at least document that it’s a bug and not intended behavior.

maintainer
#29 lore raw

Re: [PATCH 05/11] x86/fpu: set PKRU state for kernel threads

In reply to Andy Lutomirski
Quoted 9 lines · Andy Lutomirski
Quoted 3 lines

[0] drivers/usb/gadget/function/f_fs.c::ffs_user_copy_worker()

Sebastian

I think we need an entirely new API:

user_mm_ctx_t ctx = user_mm_ctx_get();

...

Quoted 9 lines

use_user_mm_ctx(ctx);
unuse_user_mm_ctx(ctx);

...

user_mm_ctx_put(ctx);

and ctx will store a copy of mm and PKRU.

That looks like a good API in general. The ffs_user_copy_worker that Sebastian mentioned seems to be used by AIO, in which case of course it has to happen in a kernel thread.

But while the API is good, deciding on the desired semantics is "interesting". The submitting thread might be changing PKRU between the time the I/O operation is submitted and the time it is completed, for example. You could look up the task's PKRU at use_mm time, except that the task might have disappeared... In the end just using PKRU=0 makes some sense and it only should be documented that some kernel services will ignore PKRU.

Paolo

#30 lore raw

Re: [PATCH 05/11] x86/fpu: set PKRU state for kernel threads

Quoted 24 lines · Paolo Bonzini
Quoted 9 lines · Andy Lutomirski
Quoted 3 lines

[0] drivers/usb/gadget/function/f_fs.c::ffs_user_copy_worker()

Sebastian

I think we need an entirely new API:

user_mm_ctx_t ctx = user_mm_ctx_get();

...

Quoted 9 lines

use_user_mm_ctx(ctx);
unuse_user_mm_ctx(ctx);

...

user_mm_ctx_put(ctx);

and ctx will store a copy of mm and PKRU.

Quoted 8 lines

That looks like a good API in general. The ffs_user_copy_worker that Sebastian mentioned seems to be used by AIO, in which case of course it has to happen in a kernel thread.

But while the API is good, deciding on the desired semantics is "interesting". The submitting thread might be changing PKRU between the time the I/O operation is submitted and the time it is completed, for example.

I think there’s only one sensible answer: capture PKRU at the time of submission.

maintainer
#31 lore raw

Re: [PATCH 05/11] x86/fpu: set PKRU state for kernel threads

Quoted 9 lines · Andy Lutomirski
Quoted 8 lines

That looks like a good API in general. The ffs_user_copy_worker that Sebastian mentioned seems to be used by AIO, in which case of course it has to happen in a kernel thread.

But while the API is good, deciding on the desired semantics is "interesting". The submitting thread might be changing PKRU between the time the I/O operation is submitted and the time it is completed, for example.

I think there’s only one sensible answer: capture PKRU at the time of submission.

I think it's much more straightforward to just not enforce pkeys. Having this "phantom" value could cause a very odd, nearly undebuggable I/O failure.

#32 lore raw

Re: [PATCH 05/11] x86/fpu: set PKRU state for kernel threads

Quoted 13 lines · Dave Hansen
Quoted 8 lines · Andy Lutomirski
Quoted 8 lines

That looks like a good API in general. The ffs_user_copy_worker that Sebastian mentioned seems to be used by AIO, in which case of course it has to happen in a kernel thread.

But while the API is good, deciding on the desired semantics is "interesting". The submitting thread might be changing PKRU between the time the I/O operation is submitted and the time it is completed, for example.

Quoted 5 lines
Quoted 1 line

I think there’s only one sensible answer: capture PKRU at the time of submission.

I think it's much more straightforward to just not enforce pkeys. Having this "phantom" value could cause a very odd, nearly undebuggable I/O failure.

But now we have the reverse. The IO can work if it’s truly async but, if the kernel decides to synchronously complete IO (with GUP or copy_to_user), it’ll fail, right. This isn’t exactly friendly either.

maintainer
#33 lore raw

Re: [PATCH 05/11] x86/fpu: set PKRU state for kernel threads

Quoted 7 lines · Andy Lutomirski
Quoted 3 lines

I think it's much more straightforward to just not enforce pkeys. Having this "phantom" value could cause a very odd, nearly undebuggable I/O failure.

But now we have the reverse. The IO can work if it’s truly async but, if the kernel decides to synchronously complete IO (with GUP or copy_to_user), it’ll fail, right. This isn’t exactly friendly either.

Yeah, but a synchronous I/O failure is really straightforward to debug because you get an immediate error message about it. This is certainly not the weirdest behavior or asymmetry that we would see from synchronous vs. async I/O.

#34 lore raw

PATCH v3 6/11 x86/pkeys: make init_pkru_value static

In reply to Sebastian Andrzej Siewior

The variable init_pkru_value isn't used outside of this file. Make init_pkru_value static.

---
 arch/x86/mm/pkeys.c | 1 +
 1 file changed, 1 insertion(+)
arch/x86/mm/pkeys.c +1 -0
index 4409ada551c5e..a150984171684 100644
@@ -150,6 +150,7 @@ void write_pkru(u32 pkru)
  * in the process's lifetime will not accidentally get access
  * to data which is pkey-protected later on.
  */
+static
 u32 init_pkru_value = PKRU_AD_KEY( 1) | PKRU_AD_KEY( 2) | PKRU_AD_KEY( 3) |
 		      PKRU_AD_KEY( 4) | PKRU_AD_KEY( 5) | PKRU_AD_KEY( 6) |
 		      PKRU_AD_KEY( 7) | PKRU_AD_KEY( 8) | PKRU_AD_KEY( 9) |
Signature · 1 line
2.19.0
#36 lore raw

PATCH v3 7/11 x86/pkeys: Drop the preempt-disable section

In reply to Sebastian Andrzej Siewior

From: Rik van Riel <[email protected]>

The fpu->initialized flag should not be changed underneath us. This might be a fallout during the removal of the LazyFPU support. The FPU is marked initialized as soon as the state has been set to an initial value. It does not signal if the CPU's FPU registers are loaded.

---
 arch/x86/mm/pkeys.c | 6 +-----
 1 file changed, 1 insertion(+), 5 deletions(-)
arch/x86/mm/pkeys.c +1 -5
index a150984171684..eeb03b0f0f514 100644
@@ -40,17 +40,13 @@ int __execute_only_pkey(struct mm_struct *mm)
 	 * dance to set PKRU if we do not need to.  Check it
 	 * first and assume that if the execute-only pkey is
 	 * write-disabled that we do not have to set it
-	 * ourselves.  We need preempt off so that nobody
-	 * can make fpregs inactive.
+	 * ourselves.
 	 */
-	preempt_disable();
 	if (!need_to_set_mm_pkey &&
 	    current->thread.fpu.initialized &&
 	    !__pkru_allows_read(read_pkru(), execute_only_pkey)) {
-		preempt_enable();
 		return execute_only_pkey;
 	}
-	preempt_enable();
 
 	/*
 	 * Set up PKRU so that it denies access for everything
Signature · 1 line
2.19.0
maintainer
#37 lore raw

Re: [PATCH 07/11] x86/pkeys: Drop the preempt-disable section

Quoted 4 lines · Sebastian Andrzej Siewior

The fpu->initialized flag should not be changed underneath us. This might be a fallout during the removal of the LazyFPU support. The FPU is marked initialized as soon as the state has been set to an initial value. It does not signal if the CPU's FPU registers are loaded.

If this is true, then the check for fpu->initialized should also go away.

If we were paranoid, we might also add a WARN_ON_ONCE() to make sure our assumption holds true.

maintainer
#38 lore raw

Re: [PATCH 07/11] x86/pkeys: Drop the preempt-disable section

Quoted 9 lines · Dave Hansen
Quoted 4 lines · Sebastian Andrzej Siewior

The fpu->initialized flag should not be changed underneath us. This might be a fallout during the removal of the LazyFPU support. The FPU is marked initialized as soon as the state has been set to an initial value. It does not signal if the CPU's FPU registers are loaded.

If this is true, then the check for fpu->initialized should also go away.

All these "if" statements in the FPU code are messy and make understanding and reviewing this code hard.

Can you prepare a patch for the beginning of your series that removes fpu->initialized and just ensures that the fpu is always initialized? This will regress performance, but you should get all that performance back with TIF_LOAD_FPU.

#39 lore raw

Re: [PATCH 07/11] x86/pkeys: Drop the preempt-disable section

Quoted 7 lines · Andy Lutomirski

All these "if" statements in the FPU code are messy and make understanding and reviewing this code hard.

Can you prepare a patch for the beginning of your series that removes fpu->initialized and just ensures that the fpu is always initialized? This will regress performance, but you should get all that performance back with TIF_LOAD_FPU.

okay, will do.

Sebastian

#40 lore raw

PATCH v3 8/11 x86/fpu: Always store the registers in copy_fpstate_to_sigframe()

In reply to Sebastian Andrzej Siewior

From: Rik van Riel <[email protected]>

copy_fpstate_to_sigframe() has two callers and both invoke the function only if fpu->initialized is set. So the check in the function for ->initialized makes no sense. It might be a relict from the lazy-FPU time: If the FPU registers were "loaded" then we could save them directly. Otherwise (the FPU registers are not up to date) they are saved in the FPU struct and it could be used for memcpy().

Since the registers always loaded at this point, save them and copy later.
This code is extracted from an earlier version of the patchset while there still was lazy-FPU on x86. This is a preparation for loading the FPU registers on return to userland.

---
 arch/x86/include/asm/fpu/internal.h | 45 ----------------------------
 arch/x86/kernel/fpu/signal.c        | 46 +++++++----------------------
 2 files changed, 11 insertions(+), 80 deletions(-)
arch/x86/include/asm/fpu/internal.h +0 -45
index 4ecaf4d22954e..df8816be3efdd 100644
@@ -126,22 +126,6 @@ extern void fpstate_sanitize_xstate(struct fpu *fpu);
 		     _ASM_EXTABLE_HANDLE(1b, 2b, ex_handler_fprestore)	\
 		     : output : input)
 
-static inline int copy_fregs_to_user(struct fregs_state __user *fx)
-{
-	return user_insn(fnsave %[fx]; fwait,  [fx] "=m" (*fx), "m" (*fx));
-}
-
-static inline int copy_fxregs_to_user(struct fxregs_state __user *fx)
-{
-	if (IS_ENABLED(CONFIG_X86_32))
-		return user_insn(fxsave %[fx], [fx] "=m" (*fx), "m" (*fx));
-	else if (IS_ENABLED(CONFIG_AS_FXSAVEQ))
-		return user_insn(fxsaveq %[fx], [fx] "=m" (*fx), "m" (*fx));
-
-	/* See comment in copy_fxregs_to_kernel() below. */
-	return user_insn(rex64/fxsave (%[fx]), "=m" (*fx), [fx] "R" (fx));
-}
-
 static inline void copy_kernel_to_fxregs(struct fxregs_state *fx)
 {
 	if (IS_ENABLED(CONFIG_X86_32)) {
@@ -352,35 +336,6 @@ static inline void copy_kernel_to_xregs(struct xregs_state *xstate, u64 mask)
 	XSTATE_XRESTORE(xstate, lmask, hmask);
 }
 
-/*
- * Save xstate to user space xsave area.
- *
- * We don't use modified optimization because xrstor/xrstors might track
- * a different application.
- *
- * We don't use compacted format xsave area for
- * backward compatibility for old applications which don't understand
- * compacted format of xsave area.
- */
-static inline int copy_xregs_to_user(struct xregs_state __user *buf)
-{
-	int err;
-
-	/*
-	 * Clear the xsave header first, so that reserved fields are
-	 * initialized to zero.
-	 */
-	err = __clear_user(&buf->header, sizeof(buf->header));
-	if (unlikely(err))
-		return -EFAULT;
-
-	stac();
-	XSTATE_OP(XSAVE, buf, -1, -1, err);
-	clac();
-
-	return err;
-}
-
 /*
  * Restore xstate from user space xsave area.
  */
arch/x86/kernel/fpu/signal.c +11 -35

61 lines not on this page · raw message

Signature · 1 line
2.19.0
#41 lore raw

Re: [PATCH 08/11] x86/fpu: Always store the registers in copy_fpstate_to_sigframe()

Quoted 4 lines · Sebastian Andrzej Siewior

From: Rik van Riel <[email protected]>

copy_fpstate_to_sigframe() has two callers and both invoke the function only if fpu->initialized is set.

One of the callers is in a different file.
Not sure if that warrants some kind of check that
fpu->initialized is set?

Quoted 3 lines

So the check in the function for ->initialized makes
no sense. It might be a relict from the lazy-FPU time: If the FPU registers

s/relict/relic/

Quoted 7 lines

were "loaded" then we could save them directly. Otherwise (the FPU registers are not up to date) they are saved in the FPU struct and it could be used for memcpy().

Since the registers always loaded at this point, save them and copy later.

register [are] always loaded

Quoted 3 lines

This code is extracted from an earlier version of the patchset while there still was lazy-FPU on x86. This is a preparation for loading the FPU registers on return to userland.

maybe "only on return"?

Quoted 89 lines
---
 arch/x86/include/asm/fpu/internal.h | 45 ----------------------------
 arch/x86/kernel/fpu/signal.c        | 46 +++++++----------------------
 2 files changed, 11 insertions(+), 80 deletions(-)
arch/x86/include/asm/fpu/internal.h +0 -45
index 4ecaf4d22954e..df8816be3efdd 100644
@@ -126,22 +126,6 @@ extern void fpstate_sanitize_xstate(struct fpu *fpu);
 		     _ASM_EXTABLE_HANDLE(1b, 2b, ex_handler_fprestore)	\
 		     : output : input)
 
-static inline int copy_fregs_to_user(struct fregs_state __user *fx)
-{
-	return user_insn(fnsave %[fx]; fwait,  [fx] "=m" (*fx), "m" (*fx));
-}
-
-static inline int copy_fxregs_to_user(struct fxregs_state __user *fx)
-{
-	if (IS_ENABLED(CONFIG_X86_32))
-		return user_insn(fxsave %[fx], [fx] "=m" (*fx), "m" (*fx));
-	else if (IS_ENABLED(CONFIG_AS_FXSAVEQ))
-		return user_insn(fxsaveq %[fx], [fx] "=m" (*fx), "m" (*fx));
-
-	/* See comment in copy_fxregs_to_kernel() below. */
-	return user_insn(rex64/fxsave (%[fx]), "=m" (*fx), [fx] "R" (fx));
-}
-
 static inline void copy_kernel_to_fxregs(struct fxregs_state *fx)
 {
 	if (IS_ENABLED(CONFIG_X86_32)) {
@@ -352,35 +336,6 @@ static inline void copy_kernel_to_xregs(struct xregs_state *xstate, u64 mask)
 	XSTATE_XRESTORE(xstate, lmask, hmask);
 }
 
-/*
- * Save xstate to user space xsave area.
- *
- * We don't use modified optimization because xrstor/xrstors might track
- * a different application.
- *
- * We don't use compacted format xsave area for
- * backward compatibility for old applications which don't understand
- * compacted format of xsave area.
- */
-static inline int copy_xregs_to_user(struct xregs_state __user *buf)
-{
-	int err;
-
-	/*
-	 * Clear the xsave header first, so that reserved fields are
-	 * initialized to zero.
-	 */
-	err = __clear_user(&buf->header, sizeof(buf->header));
-	if (unlikely(err))
-		return -EFAULT;
-
-	stac();
-	XSTATE_OP(XSAVE, buf, -1, -1, err);
-	clac();
-
-	return err;
-}
-
 /*
  * Restore xstate from user space xsave area.
  */
arch/x86/kernel/fpu/signal.c +0 -10
index 23f1691670b66..c8f5ff58578ed 100644
@@ -117,23 +117,6 @@ static inline int save_xstate_epilog(void __user *buf, int ia32_frame)
 
 	return err;
 }
-
-static inline int copy_fpregs_to_sigframe(struct xregs_state __user *buf)
-{
-	int err;
-
-	if (use_xsave())
-		err = copy_xregs_to_user(buf);
-	else if (use_fxsr())
-		err = copy_fxregs_to_user((struct fxregs_state __user *) buf);
-	else

arch/x86/kernel/fpu/signal.c static inline int save_xstate_epilog(void __user *buf, int ia32_frame) · line 122

Quoted 10 lines
arch/x86/kernel/fpu/signal.c +0 -7
-		err = copy_fregs_to_user((struct fregs_state __user *) buf);
-
-	if (unlikely(err) && __clear_user(buf, fpu_user_xstate_size))
-		err = -EFAULT;
-	return err;
-}
-
 /*
  * Save the fpu, extended register state to the user signal frame.
  *

I would modify the function comment to add the new expectation that fpu->initialized must be set on entry.

Quoted 28 lines
arch/x86/kernel/fpu/signal.c +6 -17
@@ -172,27 +155,20 @@ int copy_fpstate_to_sigframe(void __user *buf, void __user *buf_fx, int size)
 			sizeof(struct user_i387_ia32_struct), NULL,
 			(struct _fpstate_32 __user *) buf) ? -1 : 1;
 
-	if (fpu->initialized || using_compacted_format()) {
-		/* Save the live register state to the user directly. */
-		if (copy_fpregs_to_sigframe(buf_fx))
-			return -1;
-		/* Update the thread's fxstate to save the fsave header. */
-		if (ia32_fxstate)
-			copy_fxregs_to_kernel(fpu);
+	/* Update the thread's fxstate to save the fsave header. */
+	if (ia32_fxstate) {
+		copy_fxregs_to_kernel(fpu);
 	} else {
-		/*
-		 * It is a *bug* if kernel uses compacted-format for xsave
-		 * area and we copy it out directly to a signal frame. It
-		 * should have been handled above by saving the registers
-		 * directly.
-		 */
-		if (boot_cpu_has(X86_FEATURE_XSAVES)) {
-			WARN_ONCE(1, "x86/fpu: saving compacted-format xsave area to a signal frame!\n");
-			return -1;
-		}
+		copy_fpregs_to_fpstate(fpu);
+		fpregs_deactivate(fpu);
+	}

arch/x86/kernel/fpu/signal.c int copy_fpstate_to_sigframe(void __user *buf, void __user *buf_fx, int size) · line 173

Quoted 10 lines
arch/x86/kernel/fpu/signal.c +5 -1
 
+	if (using_compacted_format()) {
+		copy_xstate_to_user(buf_fx, xsave, 0, size);
+	} else {
 		fpstate_sanitize_xstate(fpu);
-		if (__copy_to_user(buf_fx, xsave, fpu_user_xstate_size))
+		size = fpu_user_xstate_size;
+		if (__copy_to_user(buf_fx, xsave, size))
 			return -1;
 	}

--
Cheers,
Christophe de Dinechin (IRC c3d)

maintainer
#42 lore raw

Re: [PATCH 08/11] x86/fpu: Always store the registers in copy_fpstate_to_sigframe()

Quoted 4 lines · Christophe de Dinechin

Sebastian Andrzej Siewior writes:

Quoted 8 lines
Quoted 4 lines

From: Rik van Riel <[email protected]>

copy_fpstate_to_sigframe() has two callers and both invoke the function only if fpu->initialized is set.

One of the callers is in a different file.
Not sure if that warrants some kind of check that
fpu->initialized is set?

I still kind of think that fpu->initialized should be removed entirely as part of this series. Keeping unnecessary complication around in the FPU code increaseds the scope for interesting bugs and makes understanding the code considerably harder.

maintainer
#43 lore raw

Re: [PATCH 08/11] x86/fpu: Always store the registers in copy_fpstate_to_sigframe()

In reply to Sebastian Andrzej Siewior
Quoted 9 lines · Sebastian Andrzej Siewior

From: Rik van Riel <[email protected]>

copy_fpstate_to_sigframe() has two callers and both invoke the function only if fpu->initialized is set. So the check in the function for ->initialized makes no sense. It might be a relict from the lazy-FPU time: If the FPU registers were "loaded" then we could save them directly. Otherwise (the FPU registers are not up to date) they are saved in the FPU struct and it could be used for memcpy().

Quoted 5 lines

Since the registers always loaded at this point, save them and copy later.
This code is extracted from an earlier version of the patchset while there still was lazy-FPU on x86. This is a preparation for loading the FPU registers on return to userland.

Could you make a pass through all the changelogs at at least make sure they look sane and consistent? The text width and paragraph spacing here looks rather cobbled together.

Quoted 4 lines
---
 arch/x86/include/asm/fpu/internal.h | 45 ----------------------------
 arch/x86/kernel/fpu/signal.c        | 46 +++++++----------------------
 2 files changed, 11 insertions(+), 80 deletions(-)

The diffstat here is really nice.

Quoted 109 lines
arch/x86/include/asm/fpu/internal.h +0 -45
index 4ecaf4d22954e..df8816be3efdd 100644
@@ -126,22 +126,6 @@ extern void fpstate_sanitize_xstate(struct fpu *fpu);
 		     _ASM_EXTABLE_HANDLE(1b, 2b, ex_handler_fprestore)	\
 		     : output : input)
 
-static inline int copy_fregs_to_user(struct fregs_state __user *fx)
-{
-	return user_insn(fnsave %[fx]; fwait,  [fx] "=m" (*fx), "m" (*fx));
-}
-
-static inline int copy_fxregs_to_user(struct fxregs_state __user *fx)
-{
-	if (IS_ENABLED(CONFIG_X86_32))
-		return user_insn(fxsave %[fx], [fx] "=m" (*fx), "m" (*fx));
-	else if (IS_ENABLED(CONFIG_AS_FXSAVEQ))
-		return user_insn(fxsaveq %[fx], [fx] "=m" (*fx), "m" (*fx));
-
-	/* See comment in copy_fxregs_to_kernel() below. */
-	return user_insn(rex64/fxsave (%[fx]), "=m" (*fx), [fx] "R" (fx));
-}
-
 static inline void copy_kernel_to_fxregs(struct fxregs_state *fx)
 {
 	if (IS_ENABLED(CONFIG_X86_32)) {
@@ -352,35 +336,6 @@ static inline void copy_kernel_to_xregs(struct xregs_state *xstate, u64 mask)
 	XSTATE_XRESTORE(xstate, lmask, hmask);
 }
 
-/*
- * Save xstate to user space xsave area.
- *
- * We don't use modified optimization because xrstor/xrstors might track
- * a different application.
- *
- * We don't use compacted format xsave area for
- * backward compatibility for old applications which don't understand
- * compacted format of xsave area.
- */
-static inline int copy_xregs_to_user(struct xregs_state __user *buf)
-{
-	int err;
-
-	/*
-	 * Clear the xsave header first, so that reserved fields are
-	 * initialized to zero.
-	 */
-	err = __clear_user(&buf->header, sizeof(buf->header));
-	if (unlikely(err))
-		return -EFAULT;
-
-	stac();
-	XSTATE_OP(XSAVE, buf, -1, -1, err);
-	clac();
-
-	return err;
-}
-
 /*
  * Restore xstate from user space xsave area.
  */
arch/x86/kernel/fpu/signal.c +3 -27
index 23f1691670b66..c8f5ff58578ed 100644
@@ -117,23 +117,6 @@ static inline int save_xstate_epilog(void __user *buf, int ia32_frame)
 
 	return err;
 }
-
-static inline int copy_fpregs_to_sigframe(struct xregs_state __user *buf)
-{
-	int err;
-
-	if (use_xsave())
-		err = copy_xregs_to_user(buf);
-	else if (use_fxsr())
-		err = copy_fxregs_to_user((struct fxregs_state __user *) buf);
-	else
-		err = copy_fregs_to_user((struct fregs_state __user *) buf);
-
-	if (unlikely(err) && __clear_user(buf, fpu_user_xstate_size))
-		err = -EFAULT;
-	return err;
-}
-
 /*
  * Save the fpu, extended register state to the user signal frame.
  *
@@ -172,27 +155,20 @@ int copy_fpstate_to_sigframe(void __user *buf, void __user *buf_fx, int size)
 			sizeof(struct user_i387_ia32_struct), NULL,
 			(struct _fpstate_32 __user *) buf) ? -1 : 1;
 
-	if (fpu->initialized || using_compacted_format()) {
-		/* Save the live register state to the user directly. */
-		if (copy_fpregs_to_sigframe(buf_fx))
-			return -1;
-		/* Update the thread's fxstate to save the fsave header. */
-		if (ia32_fxstate)
-			copy_fxregs_to_kernel(fpu);
+	/* Update the thread's fxstate to save the fsave header. */
+	if (ia32_fxstate) {
+		copy_fxregs_to_kernel(fpu);
 	} else {
-		/*
-		 * It is a *bug* if kernel uses compacted-format for xsave
-		 * area and we copy it out directly to a signal frame. It

arch/x86/kernel/fpu/signal.c int copy_fpstate_to_sigframe(void __user *buf, void __user *buf_fx, int size) · line 164

Quoted 10 lines
arch/x86/kernel/fpu/signal.c +3 -7
-		 * should have been handled above by saving the registers
-		 * directly.
-		 */
-		if (boot_cpu_has(X86_FEATURE_XSAVES)) {
-			WARN_ONCE(1, "x86/fpu: saving compacted-format xsave area to a signal frame!\n");
-			return -1;
-		}
+		copy_fpregs_to_fpstate(fpu);
+		fpregs_deactivate(fpu);
+	}

Could you add a high-level comment for this if{}else{} block that says something like:

	/* Save the registers to the fpstate. */

I also think it's worthwhile to explain the asymmetry between the ia32_fxstate case and the other branch. Why don't we fpregs_deactivate() in the ia32_fxstate path, for instance?

arch/x86/kernel/fpu/signal.c int copy_fpstate_to_sigframe(void __user *buf, void __user *buf_fx, int size) · line 173

Quoted 9 lines
arch/x86/kernel/fpu/signal.c +5 -1
+	if (using_compacted_format()) {
+		copy_xstate_to_user(buf_fx, xsave, 0, size);
+	} else {
 		fpstate_sanitize_xstate(fpu);
-		if (__copy_to_user(buf_fx, xsave, fpu_user_xstate_size))
+		size = fpu_user_xstate_size;
+		if (__copy_to_user(buf_fx, xsave, size))
 			return -1;
 	}

This seems unnecessary. Why are you updating 'size' like this?

#44 lore raw

Re: [PATCH 08/11] x86/fpu: Always store the registers in copy_fpstate_to_sigframe()

Quoted 32 lines · Dave Hansen
Quoted 18 lines
arch/x86/kernel/fpu/signal.c +3 -10
@@ -172,27 +155,20 @@ int copy_fpstate_to_sigframe(void __user *buf, void __user *buf_fx, int size)
 			sizeof(struct user_i387_ia32_struct), NULL,
 			(struct _fpstate_32 __user *) buf) ? -1 : 1;
 
-	if (fpu->initialized || using_compacted_format()) {
-		/* Save the live register state to the user directly. */
-		if (copy_fpregs_to_sigframe(buf_fx))
-			return -1;
-		/* Update the thread's fxstate to save the fsave header. */
-		if (ia32_fxstate)
-			copy_fxregs_to_kernel(fpu);
+	/* Update the thread's fxstate to save the fsave header. */
+	if (ia32_fxstate) {
+		copy_fxregs_to_kernel(fpu);
 	} else {
-		/*
-		 * It is a *bug* if kernel uses compacted-format for xsave
-		 * area and we copy it out directly to a signal frame. It

arch/x86/kernel/fpu/signal.c int copy_fpstate_to_sigframe(void __user *buf, void __user *buf_fx, int size) · line 164

Quoted 10 lines
arch/x86/kernel/fpu/signal.c +3 -7
-		 * should have been handled above by saving the registers
-		 * directly.
-		 */
-		if (boot_cpu_has(X86_FEATURE_XSAVES)) {
-			WARN_ONCE(1, "x86/fpu: saving compacted-format xsave area to a signal frame!\n");
-			return -1;
-		}
+		copy_fpregs_to_fpstate(fpu);
+		fpregs_deactivate(fpu);
+	}

Could you add a high-level comment for this if{}else{} block that says something like:

Quoted 5 lines
	/* Save the registers to the fpstate. */

I also think it's worthwhile to explain the asymmetry between the ia32_fxstate case and the other branch. Why don't we fpregs_deactivate() in the ia32_fxstate path, for instance?

Since the ->initialized is gone, the whole hunk here looks differently and probably easier to understand.

Quoted 9 lines

arch/x86/kernel/fpu/signal.c int copy_fpstate_to_sigframe(void __user *buf, void __user *buf_fx, int size) · line 173

Quoted 9 lines
arch/x86/kernel/fpu/signal.c +5 -1
+	if (using_compacted_format()) {
+		copy_xstate_to_user(buf_fx, xsave, 0, size);
+	} else {
 		fpstate_sanitize_xstate(fpu);
-		if (__copy_to_user(buf_fx, xsave, fpu_user_xstate_size))
+		size = fpu_user_xstate_size;
+		if (__copy_to_user(buf_fx, xsave, size))
 			return -1;
 	}

dropped this.

Quoted 1 line

This seems unnecessary. Why are you updating 'size' like this?

Sebastian

#45 lore raw

PATCH v3 9/11 x86/entry: add TIF_LOAD_FPU

In reply to Sebastian Andrzej Siewior

Add TIF_LOAD_FPU. This is reserved for loading the FPU registers before returning to userpace. This flag must not be set for systems without a FPU.
It is introduced now, so we can add code handling it now before adding the main feature.

---
 arch/x86/include/asm/thread_info.h | 2 ++
 1 file changed, 2 insertions(+)
arch/x86/include/asm/thread_info.h +2 -0
index cd6920674b905..cd910fd8c8c4c 100644
@@ -83,6 +83,7 @@ struct thread_info {
 #define TIF_SYSCALL_EMU		6	/* syscall emulation active */
 #define TIF_SYSCALL_AUDIT	7	/* syscall auditing active */
 #define TIF_SECCOMP		8	/* secure computing */
+#define TIF_LOAD_FPU		10	/* load FPU on return to userspace */
 #define TIF_USER_RETURN_NOTIFY	11	/* notify kernel of userspace return */
 #define TIF_UPROBE		12	/* breakpointed or singlestepping */
 #define TIF_PATCH_PENDING	13	/* pending live patching update */
@@ -110,6 +111,7 @@ struct thread_info {
 #define _TIF_SYSCALL_EMU	(1 << TIF_SYSCALL_EMU)
 #define _TIF_SYSCALL_AUDIT	(1 << TIF_SYSCALL_AUDIT)
 #define _TIF_SECCOMP		(1 << TIF_SECCOMP)
+#define _TIF_LOAD_FPU		(1 << TIF_LOAD_FPU)
 #define _TIF_USER_RETURN_NOTIFY	(1 << TIF_USER_RETURN_NOTIFY)
 #define _TIF_UPROBE		(1 << TIF_UPROBE)
 #define _TIF_PATCH_PENDING	(1 << TIF_PATCH_PENDING)
Signature · 1 line
2.19.0
#46 lore raw

PATCH v3 10/11 x86/fpu: prepare copy_fpstate_to_sigframe for TIF_LOAD_FPU

In reply to Sebastian Andrzej Siewior

From: Rik van Riel <[email protected]>

If TIF_LOAD_FPU is set, then the registers are saved (not loaded). In that case we skip the saving part.

---
 arch/x86/kernel/fpu/signal.c | 16 ++++++++++------
 1 file changed, 10 insertions(+), 6 deletions(-)
arch/x86/kernel/fpu/signal.c +10 -6
index c8f5ff58578ed..979dcd1ed82e0 100644
@@ -155,13 +155,17 @@ int copy_fpstate_to_sigframe(void __user *buf, void __user *buf_fx, int size)
 			sizeof(struct user_i387_ia32_struct), NULL,
 			(struct _fpstate_32 __user *) buf) ? -1 : 1;
 
-	/* Update the thread's fxstate to save the fsave header. */
-	if (ia32_fxstate) {
-		copy_fxregs_to_kernel(fpu);
-	} else {
-		copy_fpregs_to_fpstate(fpu);
-		fpregs_deactivate(fpu);
+	__fpregs_changes_begin();
+	if (!test_thread_flag(TIF_LOAD_FPU)) {
+		/* Update the thread's fxstate to save the fsave header. */
+		if (ia32_fxstate) {
+			copy_fxregs_to_kernel(fpu);
+		} else {
+			copy_fpregs_to_fpstate(fpu);
+			fpregs_deactivate(fpu);
+		}
 	}
+	__fpregs_changes_end();
 
 	if (using_compacted_format()) {
 		copy_xstate_to_user(buf_fx, xsave, 0, size);
Signature · 1 line
2.19.0
maintainer
#47 lore raw

Re: [PATCH 10/11] x86/fpu: prepare copy_fpstate_to_sigframe for TIF_LOAD_FPU

Quoted 4 lines · Sebastian Andrzej Siewior

From: Rik van Riel <[email protected]>

If TIF_LOAD_FPU is set, then the registers are saved (not loaded). In that case we skip the saving part.

This sentence hurts my brain.

"If TIF_LOAD_FPU is set the registers are ... not loaded"

I think that means that something could use some better naming.

Should TIF_LOAD_FPU be TIF_NEED_FPU_LOAD, perhaps?

Quoted 12 lines
---
 arch/x86/kernel/fpu/signal.c | 16 ++++++++++------
 1 file changed, 10 insertions(+), 6 deletions(-)
arch/x86/kernel/fpu/signal.c +0 -0
index c8f5ff58578ed..979dcd1ed82e0 100644
@@ -155,13 +155,17 @@ int copy_fpstate_to_sigframe(void __user *buf, void __user *buf_fx, int size)
 			sizeof(struct user_i387_ia32_struct), NULL,

arch/x86/kernel/fpu/signal.c int copy_fpstate_to_sigframe(void __user *buf, void __user *buf_fx, int size) · line 159

Quoted 10 lines
arch/x86/kernel/fpu/signal.c +2 -6
 			(struct _fpstate_32 __user *) buf) ? -1 : 1;
 
-	/* Update the thread's fxstate to save the fsave header. */
-	if (ia32_fxstate) {
-		copy_fxregs_to_kernel(fpu);
-	} else {
-		copy_fpregs_to_fpstate(fpu);
-		fpregs_deactivate(fpu);
+	__fpregs_changes_begin();
+	if (!test_thread_flag(TIF_LOAD_FPU)) {

This needs commenting, please.

If we do not need to load the FPU at return to userspace, it means the state is in the the registers, not the buffer. So, update the buffer to match the registers.

arch/x86/kernel/fpu/signal.c int copy_fpstate_to_sigframe(void __user *buf, void __user *buf_fx, int size) · line 168

Quoted 9 lines
arch/x86/kernel/fpu/signal.c +8 -0
+		/* Update the thread's fxstate to save the fsave header. */
+		if (ia32_fxstate) {
+			copy_fxregs_to_kernel(fpu);
+		} else {
+			copy_fpregs_to_fpstate(fpu);
+			fpregs_deactivate(fpu);
+		}
 	}
+	__fpregs_changes_end();

Do we really need the __fpregs_changes_*() abstraction for this single call site?

maintainer
#48 lore raw

Re: [PATCH 10/11] x86/fpu: prepare copy_fpstate_to_sigframe for TIF_LOAD_FPU

Quoted 4 lines · Dave Hansen

arch/x86/kernel/fpu/signal.c int copy_fpstate_to_sigframe(void __user *buf, void __user *buf_fx, int size) · line 168

Quoted 1 line
arch/x86/kernel/fpu/signal.c +1 -0
+	__fpregs_changes_end();

Do we really need the __fpregs_changes_*() abstraction for this single call site?

Yap, I'm staring at those in patch 2, there's no documentation there what they're supposed to do, only the commit message of patch 11 says:

"The __fpregs_changes_{begin|end}() section ensures that the register remain unchanged. Otherwise a context switch or a BH could save the registers to its FPU context and processor's FPU register would remain random."

So I'd say we should drop that abstraction, use preempt_* and put that text above the single usage site.

Thx.

Signature · 4 lines
Regards/Gruss,
    Boris.

Good mailing practices for 400: avoid top-posting and trim the reply.
#49 lore raw

Re: [PATCH 10/11] x86/fpu: prepare copy_fpstate_to_sigframe for TIF_LOAD_FPU

Quoted 9 lines · Borislav Petkov
Quoted 4 lines · Dave Hansen

arch/x86/kernel/fpu/signal.c int copy_fpstate_to_sigframe(void __user *buf, void __user *buf_fx, int size) · line 168

Quoted 1 line
arch/x86/kernel/fpu/signal.c +1 -0
+	__fpregs_changes_end();

Do we really need the __fpregs_changes_*() abstraction for this single call site?

Yap, I'm staring at those in patch 2, there's no documentation there what they're supposed to do, only the commit message of patch 11 says:

Quoted 7 lines

"The __fpregs_changes_{begin|end}() section ensures that the register remain unchanged. Otherwise a context switch or a BH could save the registers to its FPU context and processor's FPU register would remain random."

So I'd say we should drop that abstraction, use preempt_* and put that text above the single usage site.

There are more than one caller and this function disables preemption and BH in the end. Therefore I would like to keep it. But as suggested in the previous patch I will think about renaming it.

Quoted 1 line

Thx.

Sebastian

#50 lore raw

Re: [PATCH 10/11] x86/fpu: prepare copy_fpstate_to_sigframe for TIF_LOAD_FPU

In reply to Dave Hansen
Quoted 6 lines · Dave Hansen
Quoted 4 lines · Sebastian Andrzej Siewior

From: Rik van Riel <[email protected]>

If TIF_LOAD_FPU is set, then the registers are saved (not loaded). In that case we skip the saving part.

Quoted 7 lines

This sentence hurts my brain.

"If TIF_LOAD_FPU is set the registers are ... not loaded"

I think that means that something could use some better naming.

Should TIF_LOAD_FPU be TIF_NEED_FPU_LOAD, perhaps?

ARM has TIF_FOREIGN_FPSTATE which basically means that the current FP stat does not belong to current().
Let me try to use TIF_NEED_FPU_LOAD and see how that works out.

Quoted 25 lines
Quoted 12 lines
---
 arch/x86/kernel/fpu/signal.c | 16 ++++++++++------
 1 file changed, 10 insertions(+), 6 deletions(-)
arch/x86/kernel/fpu/signal.c +0 -0
index c8f5ff58578ed..979dcd1ed82e0 100644
@@ -155,13 +155,17 @@ int copy_fpstate_to_sigframe(void __user *buf, void __user *buf_fx, int size)
 			sizeof(struct user_i387_ia32_struct), NULL,

arch/x86/kernel/fpu/signal.c int copy_fpstate_to_sigframe(void __user *buf, void __user *buf_fx, int size) · line 159

Quoted 10 lines
arch/x86/kernel/fpu/signal.c +2 -6
 			(struct _fpstate_32 __user *) buf) ? -1 : 1;
 
-	/* Update the thread's fxstate to save the fsave header. */
-	if (ia32_fxstate) {
-		copy_fxregs_to_kernel(fpu);
-	} else {
-		copy_fpregs_to_fpstate(fpu);
-		fpregs_deactivate(fpu);
+	__fpregs_changes_begin();
+	if (!test_thread_flag(TIF_LOAD_FPU)) {

This needs commenting, please.

Quoted 3 lines

If we do not need to load the FPU at return to userspace, it means the state is in the the registers, not the buffer. So, update the buffer to match the registers.

okay. Let me add this then.

Sebastian

#51 lore raw

PATCH v3 11/11 x86/fpu: defer FPU state load until return to userspace

In reply to Sebastian Andrzej Siewior

From: Rik van Riel <[email protected]>

Defer loading of FPU state until return to userspace. This gives the kernel the potential to skip loading FPU state for tasks that stay in kernel mode, or for tasks that end up with repeated invocations of kernel_fpu_begin.

It also increases the chances that a task's FPU state will remain valid in the FPU registers until it is scheduled back in, allowing us to skip restoring that task's FPU state altogether.

The __fpregs_changes_{begin|end}() section ensures that the register remain unchanged. Otherwise a context switch or a BH could save the registers to its FPU context and processor's FPU register would remain random.
fpu__restore() has one user so I pulled that preempt_disable() part into fpu__restore(). While the function did *load* the registers, it now just makes sure that they are loaded on return to userland.

KVM swaps the host/guest register on enry/exit path. I kept the flow as is. First it ensures that the registers are loaded and then saves the current (host) state before it loads the guest's register. Before entring the guest, it ensures that the register are still loaded.

---
 arch/x86/entry/common.c             |   9 +++
 arch/x86/include/asm/fpu/api.h      |  11 +++
 arch/x86/include/asm/fpu/internal.h |  25 ++++---
 arch/x86/include/asm/trace/fpu.h    |   5 +-
 arch/x86/kernel/fpu/core.c          | 108 ++++++++++++++++++++--------
 arch/x86/kernel/fpu/signal.c        |   3 -
 arch/x86/kernel/process.c           |   2 +-
 arch/x86/kernel/process_32.c        |   7 +-
 arch/x86/kernel/process_64.c        |   7 +-
 arch/x86/kvm/x86.c                  |  18 +++--
 10 files changed, 143 insertions(+), 52 deletions(-)
arch/x86/entry/common.c +9 -0
index 3b2490b819181..3dad5c3b335eb 100644
@@ -31,6 +31,7 @@
 #include <asm/vdso.h>
 #include <linux/uaccess.h>
 #include <asm/cpufeature.h>
+#include <asm/fpu/api.h>
 
 #define CREATE_TRACE_POINTS
 #include <trace/events/syscalls.h>
@@ -196,6 +197,14 @@ __visible inline void prepare_exit_to_usermode(struct pt_regs *regs)
 	if (unlikely(cached_flags & EXIT_TO_USERMODE_LOOP_FLAGS))
 		exit_to_usermode_loop(regs, cached_flags);
 
+	/* Reload ti->flags; we may have rescheduled above. */
+	cached_flags = READ_ONCE(ti->flags);
+
+	if (unlikely(cached_flags & _TIF_LOAD_FPU))
+		switch_fpu_return();
+	else
+		fpregs_is_state_consistent();
+
 #ifdef CONFIG_COMPAT
 	/*
 	 * Compat syscalls set TS_COMPAT.  Make sure we clear it before
arch/x86/include/asm/fpu/api.h +11 -0
index a9caac9d4a729..e3077860f7333 100644
@@ -27,6 +27,17 @@ extern void kernel_fpu_begin(void);
 extern void kernel_fpu_end(void);
 extern bool irq_fpu_usable(void);
 
+#ifdef CONFIG_X86_DEBUG_FPU
+extern void fpregs_is_state_consistent(void);
+#else
+static inline void fpregs_is_state_consistent(void) { }
+#endif
+
+/*
+ * Load the task FPU state before returning to userspace.
+ */
+extern void switch_fpu_return(void);
+
 /*
  * Query the presence of one or more xfeatures. Works on any legacy CPU as well.
  *
arch/x86/include/asm/fpu/internal.h +17 -8
index df8816be3efdd..346f8057ecd7b 100644
@@ -32,7 +32,7 @@ extern void fpu__save(struct fpu *fpu);
 extern void fpu__restore(struct fpu *fpu);
 extern int  fpu__restore_sig(void __user *buf, int ia32_frame);
 extern void fpu__drop(struct fpu *fpu);
-extern int  fpu__copy(struct fpu *dst_fpu, struct fpu *src_fpu);
+extern int  fpu__copy(struct task_struct *dst, struct task_struct *src);
 extern void fpu__clear(struct fpu *fpu);
 extern int  fpu__exception_code(struct fpu *fpu, int trap_nr);
 extern int  dump_fpu(struct pt_regs *ptregs, struct user_i387_struct *fpstate);
@@ -473,21 +473,30 @@ static inline void fpregs_activate(struct fpu *fpu)
 /*
  * Load the FPU state for the current task. Call with preemption disabled.
  */
-static inline void __fpregs_load_activate(struct fpu *fpu, int cpu)
+static inline void __fpregs_load_activate(void)
 {
+	struct fpu *fpu = &current->thread.fpu;
+	int cpu = smp_processor_id();
+
 	if (!fpregs_state_valid(fpu, cpu))
 		copy_kernel_to_fpregs(&fpu->state);
 	fpregs_activate(fpu);
+	fpu->last_cpu = cpu;
+	clear_thread_flag(TIF_LOAD_FPU);
 }
 
+void fpregs_load_activate(void);
+
 static inline void __fpregs_changes_begin(void)
 {
 	preempt_disable();
+	local_bh_disable();
 }
 
 static inline void __fpregs_changes_end(void)
 {
 	preempt_enable();
+	local_bh_enable();
 }
 
 /*
@@ -498,8 +507,8 @@ static inline void __fpregs_changes_end(void)
  *  - switch_fpu_prepare() saves the old state.
  *    This is done within the context of the old process.
  *
- *  - switch_fpu_finish() restores the new state as
- *    necessary.
+ *  - switch_fpu_finish() sets TIF_LOAD_FPU; the floating point state
+ *    will get loaded on return to userspace, or when the kernel needs it.
  */
 static inline void
 switch_fpu_prepare(struct fpu *old_fpu, int cpu)
@@ -521,10 +530,10 @@ switch_fpu_prepare(struct fpu *old_fpu, int cpu)
  */
 
 /*
- * Set up the userspace FPU context for the new task, if the task
- * has used the FPU.
+ * Load PKRU from the FPU context if available. Delay loading the loading of the
+ * complete FPU state until the return to userland.
  */
-static inline void switch_fpu_finish(struct fpu *new_fpu, int cpu)
+static inline void switch_fpu_finish(struct fpu *new_fpu)
 {
 	bool load_fpu;
 
@@ -545,7 +554,7 @@ static inline void switch_fpu_finish(struct fpu *new_fpu, int cpu)
 #endif
 	if (!load_fpu)
 		return;
-	__fpregs_load_activate(new_fpu, cpu);
+	set_thread_flag(TIF_LOAD_FPU);
 }
 
 /*
arch/x86/include/asm/trace/fpu.h +4 -1
index 069c04be15076..ec3be1c9da7ec 100644
@@ -14,6 +14,7 @@ DECLARE_EVENT_CLASS(x86_fpu,
 	TP_STRUCT__entry(
 		__field(struct fpu *, fpu)
 		__field(bool, initialized)
+		__field(bool, load_fpu)
 		__field(u64, xfeatures)
 		__field(u64, xcomp_bv)
 		),
@@ -21,14 +22,16 @@ DECLARE_EVENT_CLASS(x86_fpu,
 	TP_fast_assign(
 		__entry->fpu		= fpu;
 		__entry->initialized	= fpu->initialized;
+		__entry->load_fpu	= test_thread_flag(TIF_LOAD_FPU);
 		if (boot_cpu_has(X86_FEATURE_OSXSAVE)) {
 			__entry->xfeatures = fpu->state.xsave.header.xfeatures;
 			__entry->xcomp_bv  = fpu->state.xsave.header.xcomp_bv;
 		}
 	),
-	TP_printk("x86/fpu: %p initialized: %d xfeatures: %llx xcomp_bv: %llx",
+	TP_printk("x86/fpu: %p initialized: %d load: %d xfeatures: %llx xcomp_bv: %llx",
 			__entry->fpu,
 			__entry->initialized,
+			__entry->load_fpu,
 			__entry->xfeatures,
 			__entry->xcomp_bv
 	)
arch/x86/kernel/fpu/core.c +79 -29

178 lines not on this page · raw message

arch/x86/kernel/fpu/signal.c +0 -3
index 979dcd1ed82e0..45d2f165b47ac 100644
@@ -325,10 +325,7 @@ static int __fpu__restore_sig(void __user *buf, void __user *buf_fx, int size)
 			sanitize_restored_xstate(tsk, &env, xfeatures, fx_only);
 		}
 
-		fpu->initialized = 1;
-		preempt_disable();
 		fpu__restore(fpu);
-		preempt_enable();
 
 		return err;
 	} else {
arch/x86/kernel/process.c +1 -1
index c93fcfdf16734..cd7105fb92bfc 100644
@@ -96,7 +96,7 @@ int arch_dup_task_struct(struct task_struct *dst, struct task_struct *src)
 	dst->thread.vm86 = NULL;
 #endif
 
-	return fpu__copy(&dst->thread.fpu, &src->thread.fpu);
+	return fpu__copy(dst, src);
 }
 
 /*
arch/x86/kernel/process_32.c +4 -3
index 5046a3c9dec2f..a65f8ce36379b 100644
@@ -236,7 +236,8 @@ __switch_to(struct task_struct *prev_p, struct task_struct *next_p)
 
 	/* never put a printk in __switch_to... printk() calls wake_up*() indirectly */
 
-	switch_fpu_prepare(prev_fpu, cpu);
+	if (prev_fpu->initialized && !test_thread_flag(TIF_LOAD_FPU))
+		switch_fpu_prepare(prev_fpu, cpu);
 
 	/*
 	 * Save away %gs. No need to save %fs, as it was saved on the
@@ -297,10 +298,10 @@ __switch_to(struct task_struct *prev_p, struct task_struct *next_p)
 	if (prev->gs | next->gs)
 		lazy_load_gs(next->gs);
 
-	switch_fpu_finish(next_fpu, cpu);
-
 	this_cpu_write(current_task, next_p);
 
+	switch_fpu_finish(next_fpu);
+
 	/* Load the Intel cache allocation PQR MSR. */
 	intel_rdt_sched_in();
 
arch/x86/kernel/process_64.c +4 -3
index ea5ea850348da..66b763f3da6a0 100644
@@ -427,7 +427,8 @@ __switch_to(struct task_struct *prev_p, struct task_struct *next_p)
 	WARN_ON_ONCE(IS_ENABLED(CONFIG_DEBUG_ENTRY) &&
 		     this_cpu_read(irq_count) != -1);
 
-	switch_fpu_prepare(prev_fpu, cpu);
+	if (prev_fpu->initialized && !test_thread_flag(TIF_LOAD_FPU))
+		switch_fpu_prepare(prev_fpu, cpu);
 
 	/* We must save %fs and %gs before load_TLS() because
 	 * %fs and %gs may be cleared by load_TLS().
@@ -478,8 +479,6 @@ __switch_to(struct task_struct *prev_p, struct task_struct *next_p)
 	load_seg_legacy(prev->gsindex, prev->gsbase,
 			next->gsindex, next->gsbase, GS);
 
-	switch_fpu_finish(next_fpu, cpu);
-
 	/*
 	 * Switch the PDA and FPU contexts.
 	 */
@@ -489,6 +488,8 @@ __switch_to(struct task_struct *prev_p, struct task_struct *next_p)
 	/* Reload sp0. */
 	update_task_stack(next_p);
 
+	switch_fpu_finish(next_fpu);
+
 	/*
 	 * Now maybe reload the debug registers and handle I/O bitmaps
 	 */
arch/x86/kvm/x86.c +14 -4
index edbf00ec56b34..2a9f35e8bb81e 100644
@@ -7592,6 +7592,11 @@ static int vcpu_enter_guest(struct kvm_vcpu *vcpu)
 		wait_lapic_expire(vcpu);
 	guest_enter_irqoff();
 
+	if (test_thread_flag(TIF_LOAD_FPU))
+		switch_fpu_return();
+	else
+		fpregs_is_state_consistent();
+
 	if (unlikely(vcpu->arch.switch_db_regs)) {
 		set_debugreg(0, 7);
 		set_debugreg(vcpu->arch.eff_db[0], 0);
@@ -7851,22 +7856,27 @@ static int complete_emulated_mmio(struct kvm_vcpu *vcpu)
 /* Swap (qemu) user FPU context for the guest FPU context. */
 static void kvm_load_guest_fpu(struct kvm_vcpu *vcpu)
 {
-	preempt_disable();
+	__fpregs_changes_begin();
+	fpregs_load_activate();
+
 	copy_fpregs_to_fpstate(&vcpu->arch.user_fpu);
+
 	/* PKRU is separately restored in kvm_x86_ops->run.  */
 	__copy_kernel_to_fpregs(&vcpu->arch.guest_fpu.state,
 				~XFEATURE_MASK_PKRU);
-	preempt_enable();
+	__fpregs_changes_end();
 	trace_kvm_fpu(1);
 }
 
 /* When vcpu_run ends, restore user space FPU context. */
 static void kvm_put_guest_fpu(struct kvm_vcpu *vcpu)
 {
-	preempt_disable();
+	__fpregs_changes_begin();
+	fpregs_load_activate();
+
 	copy_fpregs_to_fpstate(&vcpu->arch.guest_fpu);
 	copy_kernel_to_fpregs(&vcpu->arch.user_fpu.state);
-	preempt_enable();
+	__fpregs_changes_end();
 	++vcpu->stat.fpu_reload;
 	trace_kvm_fpu(0);
 }
Signature · 1 line
2.19.0
maintainer
#52 lore raw

Re: [PATCH 11/11] x86/fpu: defer FPU state load until return to userspace

Quoted 53 lines · Sebastian Andrzej Siewior

From: Rik van Riel <[email protected]>

Defer loading of FPU state until return to userspace. This gives the kernel the potential to skip loading FPU state for tasks that stay in kernel mode, or for tasks that end up with repeated invocations of kernel_fpu_begin.

It also increases the chances that a task's FPU state will remain valid in the FPU registers until it is scheduled back in, allowing us to skip restoring that task's FPU state altogether.

The __fpregs_changes_{begin|end}() section ensures that the register remain unchanged. Otherwise a context switch or a BH could save the registers to its FPU context and processor's FPU register would remain random.
fpu__restore() has one user so I pulled that preempt_disable() part into fpu__restore(). While the function did *load* the registers, it now just makes sure that they are loaded on return to userland.

KVM swaps the host/guest register on enry/exit path. I kept the flow as is. First it ensures that the registers are loaded and then saves the current (host) state before it loads the guest's register. Before entring the guest, it ensures that the register are still loaded.

---
 arch/x86/entry/common.c             |   9 +++
 arch/x86/include/asm/fpu/api.h      |  11 +++
 arch/x86/include/asm/fpu/internal.h |  25 ++++---
 arch/x86/include/asm/trace/fpu.h    |   5 +-
 arch/x86/kernel/fpu/core.c          | 108 ++++++++++++++++++++--------
 arch/x86/kernel/fpu/signal.c        |   3 -
 arch/x86/kernel/process.c           |   2 +-
 arch/x86/kernel/process_32.c        |   7 +-
 arch/x86/kernel/process_64.c        |   7 +-
 arch/x86/kvm/x86.c                  |  18 +++--
 10 files changed, 143 insertions(+), 52 deletions(-)
arch/x86/entry/common.c +1 -0
index 3b2490b819181..3dad5c3b335eb 100644
@@ -31,6 +31,7 @@
 #include <asm/vdso.h>
 #include <linux/uaccess.h>
 #include <asm/cpufeature.h>
+#include <asm/fpu/api.h>
 
 #define CREATE_TRACE_POINTS
 #include <trace/events/syscalls.h>

arch/x86/entry/common.c __visible inline void prepare_exit_to_usermode(struct pt_regs *regs) · line 206

Quoted 10 lines
arch/x86/entry/common.c +7 -0
@@ -196,6 +197,14 @@ __visible inline void prepare_exit_to_usermode(struct pt_regs *regs)
        if (unlikely(cached_flags & EXIT_TO_USERMODE_LOOP_FLAGS))
                exit_to_usermode_loop(regs, cached_flags);
 
+       /* Reload ti->flags; we may have rescheduled above. */
+       cached_flags = READ_ONCE(ti->flags);
+
+       if (unlikely(cached_flags & _TIF_LOAD_FPU))
+               switch_fpu_return();
+       else
+               fpregs_is_state_consistent();

Shouldn't this be:

fpregs_assert_state_consistent(); /* see below */

if (unlikely(cached_flags & _TIF_LOAD_FPU))
switch_fpu_return();

arch/x86/include/asm/fpu/api.h extern void kernel_fpu_begin(void); · line 34

Quoted 13 lines
arch/x86/include/asm/fpu/api.h +5 -0
index a9caac9d4a729..e3077860f7333 100644
@@ -27,6 +27,17 @@ extern void kernel_fpu_begin(void);
 extern void kernel_fpu_end(void);
 extern bool irq_fpu_usable(void);
 
+#ifdef CONFIG_X86_DEBUG_FPU
+extern void fpregs_is_state_consistent(void);
+#else
+static inline void fpregs_is_state_consistent(void) { }
+#endif

Can you name this something like fpregs_assert_state_consistent()? The "is" name makes it sound like it's:

bool fpregs_is_state_consistent();

and you're supposed to do:

WARN_ON(!fpregs_is_state_consistent());

#53 lore raw

Re: [PATCH 11/11] x86/fpu: defer FPU state load until return to userspace

Quoted 18 lines · Andy Lutomirski
Quoted 5 lines
arch/x86/entry/common.c +0 -0
index 3b2490b819181..3dad5c3b335eb 100644

arch/x86/entry/common.c __visible inline void prepare_exit_to_usermode(struct pt_regs *regs) · line 206

Quoted 10 lines
arch/x86/entry/common.c +7 -0
@@ -196,6 +197,14 @@ __visible inline void prepare_exit_to_usermode(struct pt_regs *regs)
        if (unlikely(cached_flags & EXIT_TO_USERMODE_LOOP_FLAGS))
                exit_to_usermode_loop(regs, cached_flags);
 
+       /* Reload ti->flags; we may have rescheduled above. */
+       cached_flags = READ_ONCE(ti->flags);
+
+       if (unlikely(cached_flags & _TIF_LOAD_FPU))
+               switch_fpu_return();
+       else
+               fpregs_is_state_consistent();

Shouldn't this be:

Quoted 4 lines

fpregs_assert_state_consistent(); /* see below */

if (unlikely(cached_flags & _TIF_LOAD_FPU))
switch_fpu_return();

hmm. This should work.

Quoted 8 lines
Quoted 8 lines
arch/x86/include/asm/fpu/api.h +0 -0
index a9caac9d4a729..e3077860f7333 100644
@@ -27,6 +27,17 @@ extern void kernel_fpu_begin(void);
 extern void kernel_fpu_end(void);
 extern bool irq_fpu_usable(void);
 
Quoted 7 lines

arch/x86/include/asm/fpu/api.h extern void kernel_fpu_begin(void); · line 34

Quoted 5 lines
arch/x86/include/asm/fpu/api.h +5 -0
+#ifdef CONFIG_X86_DEBUG_FPU
+extern void fpregs_is_state_consistent(void);
+#else
+static inline void fpregs_is_state_consistent(void) { }
+#endif

Can you name this something like fpregs_assert_state_consistent()?

sure.

Sebastian

#54 lore raw
In reply to Sebastian Andrzej Siewior
Quoted 3 lines · Sebastian Andrzej Siewior

In v3 I dropped that decouple idea. I also learned that the wrpkru instruction is not privileged and so caching it in kernel does not work.

Wait, so any thread can bypass its memory protection keys, even if there is a seccomp filter preventing it from calling the PKRU syscalls?

Is that intended?

Is that simply a hardware limitation, or something where we can set a flag somewhere to force tasks to go through the kernel?

Signature · 1 line
All Rights Reversed.
#55 lore raw
Quoted 15 lines · Rik van Riel
Quoted 3 lines · Sebastian Andrzej Siewior

In v3 I dropped that decouple idea. I also learned that the wrpkru instruction is not privileged and so caching it in kernel does not work.

Wait, so any thread can bypass its memory protection keys, even if there is a seccomp filter preventing it from calling the PKRU syscalls?

Quoted 7 lines

Is that intended?

Is that simply a hardware limitation, or something where we can set a flag somewhere to force tasks to go through the kernel?

Hardware limitation.

#56 lore raw
In reply to Rik van Riel
Quoted 3 lines · Rik van Riel

Wait, so any thread can bypass its memory protection keys, even if there is a seccomp filter preventing it from calling the PKRU syscalls?

We have SYS_pkey_alloc +free and SYS_pkey_mprotect. For read/ write of the register value, libc is using and opcodes.

Quoted 1 line

Is that intended?

Either that or it ended like that because someone failed to attend a meeting where this was discussed. Here is something from pkeys(7):

| Protection  keys  have  the  potential  to  add  a  layer  of security and
| reliability to applications.  But they have not been primarily designed as a
| security feature.  For instance, WRPKRU is a completely unprivileged
| instruction, so pkeys are useless in any case that an attacker controls the
| PKRU register or can execute arbitrary instructions.

Sebastian