Skip to contentSkip to conversation

x86 fpu cleanups

PATCH In review Christoph Hellwig 5 participants 15 messages Jun 4, 2019 – Jun 17, 2019 x86

lore
Share & tools
lore
#0 lore raw

Hi all,

this series finishes off some cleanups that are possible now that Sebastians fpu rework is finished.

#1 lore raw

PATCH 1/3 x86/fpu: Simplify kernel_fpu_end

Remove two little helpers and merge them into kernel_fpu_end to streamline the function.

---
 arch/x86/kernel/fpu/core.c | 15 +++------------
 1 file changed, 3 insertions(+), 12 deletions(-)
arch/x86/kernel/fpu/core.c +3 -12
index 466fca686fb9..1d09af1158e1 100644
@@ -49,12 +49,6 @@ static void kernel_fpu_disable(void)
 	this_cpu_write(in_kernel_fpu, true);
 }
 
-static void kernel_fpu_enable(void)
-{
-	WARN_ON_FPU(!this_cpu_read(in_kernel_fpu));
-	this_cpu_write(in_kernel_fpu, false);
-}
-
 static bool kernel_fpu_disabled(void)
 {
 	return this_cpu_read(in_kernel_fpu);
@@ -115,11 +109,6 @@ static void __kernel_fpu_begin(void)
 	__cpu_invalidate_fpregs_state();
 }
 
-static void __kernel_fpu_end(void)
-{
-	kernel_fpu_enable();
-}
-
 void kernel_fpu_begin(void)
 {
 	preempt_disable();
@@ -129,7 +118,9 @@ EXPORT_SYMBOL_GPL(kernel_fpu_begin);
 
 void kernel_fpu_end(void)
 {
-	__kernel_fpu_end();
+	WARN_ON_FPU(!this_cpu_read(in_kernel_fpu));
+
+	this_cpu_write(in_kernel_fpu, false);
 	preempt_enable();
 }
 EXPORT_SYMBOL_GPL(kernel_fpu_end);
Signature · 1 line
2.20.1
#2 lore raw

[tip:x86/fpu] x86/fpu: Simplify kernel_fpu_end()

x86/fpu: Simplify kernel_fpu_end()
Commit-ID:  b78ea19ac22fd7b32d7828066cce3d8f2db5226a
Gitweb:     https://git.kernel.org/tip/b78ea19ac22fd7b32d7828066cce3d8f2db5226a
Author:     Christoph Hellwig <[email protected]>
AuthorDate: Tue, 4 Jun 2019 09:15:22 +0200
Committer:  Borislav Petkov <[email protected]>
CommitDate: Mon, 17 Jun 2019 10:43:43 +0200

x86/fpu: Simplify kernel_fpu_end()

Remove two little helpers and merge them into kernel_fpu_end() to streamline the function.

---
 arch/x86/kernel/fpu/core.c | 15 +++------------
 1 file changed, 3 insertions(+), 12 deletions(-)
arch/x86/kernel/fpu/core.c +3 -12
index 649fbc3fcf9f..8e046068d20f 100644
@@ -49,12 +49,6 @@ static void kernel_fpu_disable(void)
 	this_cpu_write(in_kernel_fpu, true);
 }
 
-static void kernel_fpu_enable(void)
-{
-	WARN_ON_FPU(!this_cpu_read(in_kernel_fpu));
-	this_cpu_write(in_kernel_fpu, false);
-}
-
 static bool kernel_fpu_disabled(void)
 {
 	return this_cpu_read(in_kernel_fpu);
@@ -115,11 +109,6 @@ static void __kernel_fpu_begin(void)
 	__cpu_invalidate_fpregs_state();
 }
 
-static void __kernel_fpu_end(void)
-{
-	kernel_fpu_enable();
-}
-
 void kernel_fpu_begin(void)
 {
 	preempt_disable();
@@ -129,7 +118,9 @@ EXPORT_SYMBOL_GPL(kernel_fpu_begin);
 
 void kernel_fpu_end(void)
 {
-	__kernel_fpu_end();
+	WARN_ON_FPU(!this_cpu_read(in_kernel_fpu));
+
+	this_cpu_write(in_kernel_fpu, false);
 	preempt_enable();
 }
 EXPORT_SYMBOL_GPL(kernel_fpu_end);
#3 lore raw

PATCH 2/3 x86/fpu: Simplify kernel_fpu_begin

In reply to Christoph Hellwig

Merge two helpers into the main function, remove a pointless local variable and flatten a conditional.

---
 arch/x86/kernel/fpu/core.c | 35 +++++++++++------------------------
 1 file changed, 11 insertions(+), 24 deletions(-)
arch/x86/kernel/fpu/core.c +11 -24
index 1d09af1158e1..03c2d306e6f2 100644
@@ -43,12 +43,6 @@ static DEFINE_PER_CPU(bool, in_kernel_fpu);
  */
 DEFINE_PER_CPU(struct fpu *, fpu_fpregs_owner_ctx);
 
-static void kernel_fpu_disable(void)
-{
-	WARN_ON_FPU(this_cpu_read(in_kernel_fpu));
-	this_cpu_write(in_kernel_fpu, true);
-}
-
 static bool kernel_fpu_disabled(void)
 {
 	return this_cpu_read(in_kernel_fpu);
@@ -88,32 +82,25 @@ bool irq_fpu_usable(void)
 }
 EXPORT_SYMBOL(irq_fpu_usable);
 
-static void __kernel_fpu_begin(void)
+void kernel_fpu_begin(void)
 {
-	struct fpu *fpu = &current->thread.fpu;
+	preempt_disable();
 
 	WARN_ON_FPU(!irq_fpu_usable());
+	WARN_ON_FPU(this_cpu_read(in_kernel_fpu));
 
-	kernel_fpu_disable();
+	this_cpu_write(in_kernel_fpu, true);
 
-	if (current->mm) {
-		if (!test_thread_flag(TIF_NEED_FPU_LOAD)) {
-			set_thread_flag(TIF_NEED_FPU_LOAD);
-			/*
-			 * Ignore return value -- we don't care if reg state
-			 * is clobbered.
-			 */
-			copy_fpregs_to_fpstate(fpu);
-		}
+	if (current->mm && !test_thread_flag(TIF_NEED_FPU_LOAD)) {
+		set_thread_flag(TIF_NEED_FPU_LOAD);
+		/*
+		 * Ignore return value -- we don't care if reg state
+		 * is clobbered.
+		 */
+		copy_fpregs_to_fpstate(&current->thread.fpu);
 	}
 	__cpu_invalidate_fpregs_state();
 }
-
-void kernel_fpu_begin(void)
-{
-	preempt_disable();
-	__kernel_fpu_begin();
-}
 EXPORT_SYMBOL_GPL(kernel_fpu_begin);
 
 void kernel_fpu_end(void)
Signature · 1 line
2.20.1
#4 lore raw

Re: [PATCH 2/3] x86/fpu: Simplify kernel_fpu_begin

arch/x86/kernel/fpu/core.c bool irq_fpu_usable(void) · line 94

Quoted 10 lines · Christoph Hellwig
arch/x86/kernel/fpu/core.c +5 -0
+void kernel_fpu_begin(void)
 {
+	preempt_disable();
 
 	WARN_ON_FPU(!irq_fpu_usable());
+	WARN_ON_FPU(this_cpu_read(in_kernel_fpu));
 
+	this_cpu_write(in_kernel_fpu, true);
 
+	if (current->mm && !test_thread_flag(TIF_NEED_FPU_LOAD)) {

Did that want to be: !(current->flags & PF_KTHREAD), instead?

Because I'm thinking that kernel_fpu_begin() on a kthread that has use_mm() employed shouldn't be doing this..

Quoted 9 lines
arch/x86/kernel/fpu/core.c +6 -0
+		set_thread_flag(TIF_NEED_FPU_LOAD);
+		/*
+		 * Ignore return value -- we don't care if reg state
+		 * is clobbered.
+		 */
+		copy_fpregs_to_fpstate(&current->thread.fpu);
 	}
 	__cpu_invalidate_fpregs_state();
 }
#5 lore raw

Re: [PATCH 2/3] x86/fpu: Simplify kernel_fpu_begin

Quoted 6 lines · Peter Zijlstra

arch/x86/kernel/fpu/core.c bool irq_fpu_usable(void) · line 94

Quoted 1 line
arch/x86/kernel/fpu/core.c +1 -0
+	if (current->mm && !test_thread_flag(TIF_NEED_FPU_LOAD)) {

Did that want to be: !(current->flags & PF_KTHREAD), instead?

Because I'm thinking that kernel_fpu_begin() on a kthread that has use_mm() employed shouldn't be doing this..

Well, this is intended to not change semantics. If we should fix this is should be a separate patch before or after this series.

#6 lore raw

Re: [PATCH 2/3] x86/fpu: Simplify kernel_fpu_begin

Quoted 10 lines · Christoph Hellwig
Quoted 6 lines · Peter Zijlstra

arch/x86/kernel/fpu/core.c bool irq_fpu_usable(void) · line 94

Quoted 1 line
arch/x86/kernel/fpu/core.c +1 -0
+	if (current->mm && !test_thread_flag(TIF_NEED_FPU_LOAD)) {

Did that want to be: !(current->flags & PF_KTHREAD), instead?

Because I'm thinking that kernel_fpu_begin() on a kthread that has use_mm() employed shouldn't be doing this..

Well, this is intended to not change semantics. If we should fix this is should be a separate patch before or after this series.

Sure; it's just that I just noticed it. We've recently ran into a similar issue elsewhere.

#7 lore raw

Re: [PATCH 2/3] x86/fpu: Simplify kernel_fpu_begin

Quoted 5 lines · Peter Zijlstra
Quoted 2 lines

Well, this is intended to not change semantics. If we should fix this is should be a separate patch before or after this series.

Sure; it's just that I just noticed it. We've recently ran into a similar issue elsewhere.

Ok, I'll send a follow on.

#8 lore raw

[tip:x86/fpu] x86/fpu: Simplify kernel_fpu_begin()

In reply to Christoph Hellwig
x86/fpu: Simplify kernel_fpu_begin()
Commit-ID:  6d79d86f9600510e08ad45c72b9d7e664e439e62
Gitweb:     https://git.kernel.org/tip/6d79d86f9600510e08ad45c72b9d7e664e439e62
Author:     Christoph Hellwig <[email protected]>
AuthorDate: Tue, 4 Jun 2019 09:15:23 +0200
Committer:  Borislav Petkov <[email protected]>
CommitDate: Mon, 17 Jun 2019 12:19:49 +0200

x86/fpu: Simplify kernel_fpu_begin()

Merge two helpers into the main function, remove a pointless local variable and flatten a conditional.

---
 arch/x86/kernel/fpu/core.c | 36 ++++++++++++------------------------
 1 file changed, 12 insertions(+), 24 deletions(-)
arch/x86/kernel/fpu/core.c +12 -24
index 8e046068d20f..3f92cfad88f0 100644
@@ -43,12 +43,6 @@ static DEFINE_PER_CPU(bool, in_kernel_fpu);
  */
 DEFINE_PER_CPU(struct fpu *, fpu_fpregs_owner_ctx);
 
-static void kernel_fpu_disable(void)
-{
-	WARN_ON_FPU(this_cpu_read(in_kernel_fpu));
-	this_cpu_write(in_kernel_fpu, true);
-}
-
 static bool kernel_fpu_disabled(void)
 {
 	return this_cpu_read(in_kernel_fpu);
@@ -88,32 +82,26 @@ bool irq_fpu_usable(void)
 }
 EXPORT_SYMBOL(irq_fpu_usable);
 
-static void __kernel_fpu_begin(void)
+void kernel_fpu_begin(void)
 {
-	struct fpu *fpu = &current->thread.fpu;
+	preempt_disable();
 
 	WARN_ON_FPU(!irq_fpu_usable());
+	WARN_ON_FPU(this_cpu_read(in_kernel_fpu));
 
-	kernel_fpu_disable();
+	this_cpu_write(in_kernel_fpu, true);
 
-	if (!(current->flags & PF_KTHREAD)) {
-		if (!test_thread_flag(TIF_NEED_FPU_LOAD)) {
-			set_thread_flag(TIF_NEED_FPU_LOAD);
-			/*
-			 * Ignore return value -- we don't care if reg state
-			 * is clobbered.
-			 */
-			copy_fpregs_to_fpstate(fpu);
-		}
+	if (!(current->flags & PF_KTHREAD) &&
+	    !test_thread_flag(TIF_NEED_FPU_LOAD)) {
+		set_thread_flag(TIF_NEED_FPU_LOAD);
+		/*
+		 * Ignore return value -- we don't care if reg state
+		 * is clobbered.
+		 */
+		copy_fpregs_to_fpstate(&current->thread.fpu);
 	}
 	__cpu_invalidate_fpregs_state();
 }
-
-void kernel_fpu_begin(void)
-{
-	preempt_disable();
-	__kernel_fpu_begin();
-}
 EXPORT_SYMBOL_GPL(kernel_fpu_begin);
 
 void kernel_fpu_end(void)
#9 lore raw

PATCH 3/3 x86/fpu: remove the fpu__save export

In reply to Christoph Hellwig

This function is only use by the core fpu code.

---
 arch/x86/kernel/fpu/core.c | 1 -
 1 file changed, 1 deletion(-)
arch/x86/kernel/fpu/core.c +0 -1
index 03c2d306e6f2..5e0240d029fd 100644
@@ -133,7 +133,6 @@ void fpu__save(struct fpu *fpu)
 	trace_x86_fpu_after_save(fpu);
 	fpregs_unlock();
 }
-EXPORT_SYMBOL_GPL(fpu__save);
 
 /*
  * Legacy x87 fpstate state init:
Signature · 1 line
2.20.1
#10 lore raw

[tip:x86/fpu] x86/fpu: Remove the fpu__save() export

x86/fpu: Remove the fpu__save() export
Commit-ID:  466329bf407cc5143c3211620faa2c132b9d9a06
Gitweb:     https://git.kernel.org/tip/466329bf407cc5143c3211620faa2c132b9d9a06
Author:     Christoph Hellwig <[email protected]>
AuthorDate: Tue, 4 Jun 2019 09:15:24 +0200
Committer:  Borislav Petkov <[email protected]>
CommitDate: Mon, 17 Jun 2019 12:21:26 +0200

x86/fpu: Remove the fpu__save() export

This function is only use by the core FPU code.

---
 arch/x86/kernel/fpu/core.c | 1 -
 1 file changed, 1 deletion(-)
arch/x86/kernel/fpu/core.c +0 -1
index 3f92cfad88f0..12c70840980e 100644
@@ -134,7 +134,6 @@ void fpu__save(struct fpu *fpu)
 	trace_x86_fpu_after_save(fpu);
 	fpregs_unlock();
 }
-EXPORT_SYMBOL_GPL(fpu__save);
 
 /*
  * Legacy x87 fpstate state init:
#11 lore raw

[PATCH 4/3] x86/fpu: don't use current->mm to check for a kthread

In reply to Christoph Hellwig

current->mm can be non-NULL if a kthread calls use_mm. Check for PF_KTHREAD instead to decide when to store user mode FP state.

Fixes 2722146eb784 ("x86/fpu: Remove fpu->initialized")
---
 arch/x86/include/asm/fpu/internal.h | 6 +++---
 arch/x86/kernel/fpu/core.c          | 3 ++-
 2 files changed, 5 insertions(+), 4 deletions(-)
arch/x86/include/asm/fpu/internal.h +3 -3
index 9e27fa05a7ae..4c95c365058a 100644
@@ -536,7 +536,7 @@ static inline void __fpregs_load_activate(void)
 	struct fpu *fpu = &current->thread.fpu;
 	int cpu = smp_processor_id();
 
-	if (WARN_ON_ONCE(current->mm == NULL))
+	if (WARN_ON_ONCE(current->flags & PF_KTHREAD))
 		return;
 
 	if (!fpregs_state_valid(fpu, cpu)) {
@@ -567,11 +567,11 @@ static inline void __fpregs_load_activate(void)
  * otherwise.
  *
  * The FPU context is only stored/restored for a user task and
- * ->mm is used to distinguish between kernel and user threads.
+ * PF_KTHREAD is used to distinguish between kernel and user threads.
  */
 static inline void switch_fpu_prepare(struct fpu *old_fpu, int cpu)
 {
-	if (static_cpu_has(X86_FEATURE_FPU) && current->mm) {
+	if (static_cpu_has(X86_FEATURE_FPU) && !(current->flags & PF_KTHREAD)) {
 		if (!copy_fpregs_to_fpstate(old_fpu))
 			old_fpu->last_cpu = -1;
 		else
arch/x86/kernel/fpu/core.c +2 -1
index 5e0240d029fd..12c70840980e 100644
@@ -91,7 +91,8 @@ void kernel_fpu_begin(void)
 
 	this_cpu_write(in_kernel_fpu, true);
 
-	if (current->mm && !test_thread_flag(TIF_NEED_FPU_LOAD)) {
+	if (!(current->flags & PF_KTHREAD) &&
+	    !test_thread_flag(TIF_NEED_FPU_LOAD)) {
 		set_thread_flag(TIF_NEED_FPU_LOAD);
 		/*
 		 * Ignore return value -- we don't care if reg state
Signature · 1 line
2.20.1
#12 lore raw

Re: [PATCH 4/3] x86/fpu: don't use current->mm to check for a kthread

Quoted 6 lines · Christoph Hellwig

current->mm can be non-NULL if a kthread calls use_mm. Check for PF_KTHREAD instead to decide when to store user mode FP state.

Fixes 2722146eb784 ("x86/fpu: Remove fpu->initialized")

For the whole series.

Sebastian

maintainer
#13 lore raw

Re: [PATCH 4/3] x86/fpu: don't use current->mm to check for a kthread

In reply to Christoph Hellwig
Quoted 10 lines · Christoph Hellwig

current->mm can be non-NULL if a kthread calls use_mm. Check for PF_KTHREAD instead to decide when to store user mode FP state.

Fixes 2722146eb784 ("x86/fpu: Remove fpu->initialized")
---
 arch/x86/include/asm/fpu/internal.h | 6 +++---
 arch/x86/kernel/fpu/core.c          | 3 ++-
 2 files changed, 5 insertions(+), 4 deletions(-)

I had to take this one first because of the Fixes: tag and expedite it through tip:x86/urgent. Check the tip-bot notification mail whether it is still ok.

I'll redo the other ones ontop, or you can if you feel bored. :)

Thx.

Signature · 4 lines
Regards/Gruss,
    Boris.

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

[tip:x86/urgent] x86/fpu: Don't use current->mm to check for a kthread

In reply to Christoph Hellwig
x86/fpu: Don't use current->mm to check for a kthread
Commit-ID:  8d3289f2fa1e0c7e2f72c7352f1efb75d2ad7c76
Gitweb:     https://git.kernel.org/tip/8d3289f2fa1e0c7e2f72c7352f1efb75d2ad7c76
Author:     Christoph Hellwig <[email protected]>
AuthorDate: Tue, 4 Jun 2019 19:54:12 +0200
Committer:  Borislav Petkov <[email protected]>
CommitDate: Thu, 13 Jun 2019 20:57:49 +0200

x86/fpu: Don't use current->mm to check for a kthread

current->mm can be non-NULL if a kthread calls use_mm(). Check for PF_KTHREAD instead to decide when to store user mode FP state.

Fixes 2722146eb784 ("x86/fpu: Remove fpu->initialized")
Cc Aubrey Li <[email protected]>
Cc Dave Hansen <[email protected]>
Cc "H. Peter Anvin" <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Jann Horn <[email protected]>
Cc Nicolai Stange <[email protected]>
Cc Rik van Riel <[email protected]>
Cc Thomas Gleixner <[email protected]>
Cc x86-ml <[email protected]>
---
 arch/x86/include/asm/fpu/internal.h | 6 +++---
 arch/x86/kernel/fpu/core.c          | 2 +-
 2 files changed, 4 insertions(+), 4 deletions(-)
arch/x86/include/asm/fpu/internal.h +3 -3
index 9e27fa05a7ae..4c95c365058a 100644
@@ -536,7 +536,7 @@ static inline void __fpregs_load_activate(void)
 	struct fpu *fpu = &current->thread.fpu;
 	int cpu = smp_processor_id();
 
-	if (WARN_ON_ONCE(current->mm == NULL))
+	if (WARN_ON_ONCE(current->flags & PF_KTHREAD))
 		return;
 
 	if (!fpregs_state_valid(fpu, cpu)) {
@@ -567,11 +567,11 @@ static inline void __fpregs_load_activate(void)
  * otherwise.
  *
  * The FPU context is only stored/restored for a user task and
- * ->mm is used to distinguish between kernel and user threads.
+ * PF_KTHREAD is used to distinguish between kernel and user threads.
  */
 static inline void switch_fpu_prepare(struct fpu *old_fpu, int cpu)
 {
-	if (static_cpu_has(X86_FEATURE_FPU) && current->mm) {
+	if (static_cpu_has(X86_FEATURE_FPU) && !(current->flags & PF_KTHREAD)) {
 		if (!copy_fpregs_to_fpstate(old_fpu))
 			old_fpu->last_cpu = -1;
 		else
arch/x86/kernel/fpu/core.c +1 -1
index 466fca686fb9..649fbc3fcf9f 100644
@@ -102,7 +102,7 @@ static void __kernel_fpu_begin(void)
 
 	kernel_fpu_disable();
 
-	if (current->mm) {
+	if (!(current->flags & PF_KTHREAD)) {
 		if (!test_thread_flag(TIF_NEED_FPU_LOAD)) {
 			set_thread_flag(TIF_NEED_FPU_LOAD);
 			/*