[02/13] x86: add yield FPU context utility function

Message ID 20221219220223.3982176-3-elliott@hpe.com
State New
Headers
Series crypto: x86 - yield FPU context during long loops |

Commit Message

Elliott, Robert (Servers) Dec. 19, 2022, 10:02 p.m. UTC
  Add a function that may be called to avoid hogging the CPU
between kernel_fpu_begin() and kernel_fpu_end() calls.

Signed-off-by: Robert Elliott <elliott@hpe.com>
---
 arch/x86/include/asm/simd.h | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)
  

Patch

diff --git a/arch/x86/include/asm/simd.h b/arch/x86/include/asm/simd.h
index bd9c672a2792..2c887dec95a2 100644
--- a/arch/x86/include/asm/simd.h
+++ b/arch/x86/include/asm/simd.h
@@ -3,6 +3,7 @@ 
 #define _ASM_X86_SIMD_H
 
 #include <asm/fpu/api.h>
+#include <linux/sched.h>
 
 /*
  * may_use_simd - whether it is allowable at this time to issue SIMD
@@ -13,4 +14,22 @@  static __must_check inline bool may_use_simd(void)
 	return irq_fpu_usable();
 }
 
+/**
+ * kernel_fpu_relax - pause FPU preemption if scheduler wants
+ *
+ * Call this periodically during long loops between kernel_fpu_begin()
+ * and kernel_fpu_end() calls to avoid hogging the CPU if the
+ * scheduler wants to use the CPU for another thread
+ *
+ * Return: none
+ */
+static inline void kernel_fpu_yield(void)
+{
+	if (need_resched()) {
+		kernel_fpu_end();
+		cond_resched();
+		kernel_fpu_begin();
+	}
+}
+
 #endif /* _ASM_X86_SIMD_H */