Skip to contentSkip to conversation

powerpc: add simd.h implementation specific to PowerPC

PATCH Superseded Shawn Landden 8 participants 24 messages May 12, 2019 – May 18, 2019 PowerPC

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 powerpc: add simd.h implementation specific to PowerPC

It is safe to do SIMD in an interrupt on PowerPC.
Only disable when there is no SIMD available
(and this is a static branch).

Tested and works with the WireGuard (Zinc) patch I wrote that needs this. Also improves performance of the crypto subsystem that checks this.

---
 arch/powerpc/include/asm/simd.h | 15 +++++++++++++++
 1 file changed, 15 insertions(+)
 create mode 100644 arch/powerpc/include/asm/simd.h
arch/powerpc/include/asm/simd.h new file +15 -0
new file mode 100644
index 000000000..b3fecb95a
@@ -0,0 +1,15 @@
+/* SPDX-License-Identifier: GPL-2.0+ */
+
+#include <asm/cpu_has_feature.h>
+
+/*
+ * may_use_simd - whether it is allowable at this time to issue SIMD
+ *                instructions or access the SIMD register file
+ *
+ * As documented in Chapter 6.2.1 Machine Status Save/Restore Registers
+ * of Power ISA (2.07 and 3.0), all registers are saved/restored in an interrupt.
+ */
+static inline bool may_use_simd(void)
+{
+	return !cpu_has_feature(CPU_FTR_FPU_UNAVAILABLE);
+}
Signature · 1 line
2.21.0.1020.gf2820cf01a
#1 lore raw

PATCH RESEND 1/1 powerpc: add simd.h implementation specific to PowerPC

It is safe to do SIMD in an interrupt on PowerPC.
Only disable when there is no SIMD available
(and this is a static branch).

Tested and works with the WireGuard (Zinc) patch I wrote that needs this. Also improves performance of the crypto subsystem that checks this.

Re-sending because this linuxppc-dev didn't seem to accept it.

---
 arch/powerpc/include/asm/simd.h | 15 +++++++++++++++
 1 file changed, 15 insertions(+)
 create mode 100644 arch/powerpc/include/asm/simd.h
arch/powerpc/include/asm/simd.h new file +15 -0
new file mode 100644
index 000000000..b3fecb95a
@@ -0,0 +1,15 @@
+/* SPDX-License-Identifier: GPL-2.0+ */
+
+#include <asm/cpu_has_feature.h>
+
+/*
+ * may_use_simd - whether it is allowable at this time to issue SIMD
+ *                instructions or access the SIMD register file
+ *
+ * As documented in Chapter 6.2.1 Machine Status Save/Restore Registers
+ * of Power ISA (2.07 and 3.0), all registers are saved/restored in an interrupt.
+ */
+static inline bool may_use_simd(void)
+{
+	return !cpu_has_feature(CPU_FTR_FPU_UNAVAILABLE);
+}
Signature · 1 line
2.21.0.1020.gf2820cf01a
#2 lore raw
Quoted 1 line · Shawn Landden

It is safe to do SIMD in an interrupt on PowerPC.

No it's not sorry :)

Quoted 7 lines

Only disable when there is no SIMD available
(and this is a static branch).

Tested and works with the WireGuard (Zinc) patch I wrote that needs this. Also improves performance of the crypto subsystem that checks this.

Re-sending because this linuxppc-dev didn't seem to accept it.

It did but you were probably moderated as a non-subscriber? In future if you just wait a while for the moderators to wake up it should come through. Though having posted once and been approved I think you might not get moderated at all in future (?).

Quoted 13 lines
---
 arch/powerpc/include/asm/simd.h | 15 +++++++++++++++
 1 file changed, 15 insertions(+)
 create mode 100644 arch/powerpc/include/asm/simd.h
arch/powerpc/include/asm/simd.h new file +0 -0
new file mode 100644
index 000000000..b3fecb95a

arch/powerpc/include/asm/simd.h line 10

Quoted 10 lines
arch/powerpc/include/asm/simd.h new file +10 -0
@@ -0,0 +1,15 @@
+/* SPDX-License-Identifier: GPL-2.0+ */
+
+#include <asm/cpu_has_feature.h>
+
+/*
+ * may_use_simd - whether it is allowable at this time to issue SIMD
+ *                instructions or access the SIMD register file
+ *
+ * As documented in Chapter 6.2.1 Machine Status Save/Restore Registers
+ * of Power ISA (2.07 and 3.0), all registers are saved/restored in an interrupt.

I think the confusion here is that the ISA says:

When various interrupts occur, the state of the machine is saved in the Machine Status Save/Restore registers (SRR0 and SRR1).

And you've read that to mean all "the state" of the machine, ie. including GPRs, FPRs etc.

But unfortunately it's not that simple. All the hardware does is write two 64-bit registers (SRR0 & SRR1) with the information required to be able to return to the state the CPU was in prior to the interrupt. That includes (obviously) the PC you were executing at, and what state the CPU was in (ie. 64/32-bit, MMU on/off, FP on/off etc.). All the saving of registers etc. is left up to software. It's the RISC way :)

I guess we need to work out why exactly may_use_simd() is returning false in your kworker.

cheers

#3 lore raw

PATCH 1/2 Add simd.h implementation

In reply to Shawn Landden

Based off the x86 one.

WireGuard really wants to be able to do SIMD in interrupts, so it can accelerate its in-bound path.

---
 arch/powerpc/include/asm/simd.h | 13 +++++++++++++
 arch/powerpc/kernel/process.c   | 30 ++++++++++++++++++++++++++++++
 2 files changed, 43 insertions(+)
 create mode 100644 arch/powerpc/include/asm/simd.h
arch/powerpc/include/asm/simd.h new file +13 -0
new file mode 100644
index 000000000..2c02ad531
@@ -0,0 +1,13 @@
+/* SPDX-License-Identifier: GPL-2.0+ */
+
+/*
+ * may_use_simd - whether it is allowable at this time to issue SIMD
+ *                instructions or access the SIMD register file
+ *
+ * It's always ok in process context (ie "not interrupt")
+ * but it is sometimes ok even from an irq.
+ */
+static __must_check inline bool may_use_simd(void)
+{
+	return irq_simd_usable();
+}
arch/powerpc/kernel/process.c +30 -0
index dd9e0d538..e436d708a 100644
@@ -345,6 +345,36 @@ static int restore_altivec(struct task_struct *tsk)
 	}
 	return 0;
 }
+
+/*
+ * Were we in user mode when we were
+ * interrupted?
+ *
+ * Doing kernel_altivec/vsx_begin/end() is ok if we are running
+ * in an interrupt context from user mode - we'll just
+ * save the FPU state as required.
+ */
+static bool interrupted_user_mode(void)
+{
+	struct pt_regs *regs = get_irq_regs();
+
+	return regs && user_mode(regs);
+}
+
+/*
+ * Can we use SIMD in kernel mode with the
+ * whole "kernel_altivec/vsx_begin/end()" sequence?
+ *
+ * It's always ok in process context (ie "not interrupt")
+ * but it is sometimes ok even from an irq.
+ */
+bool irq_simd_usable(void)
+{
+	return !in_interrupt() ||
+		interrupted_user_mode();
+}
+EXPORT_SYMBOL(irq_simd_usable);
+
 #else
 #define loadvec(thr) 0
 static inline int restore_altivec(struct task_struct *tsk) { return 0; }
Signature · 1 line
2.21.0.1020.gf2820cf01a
#4 lore raw

PATCH 2/2 Allow use of SIMD in interrupts from kernel code

This second patch is separate because it could be wrong, like I am not sure about how kernel thread migration works, and it is even allowing simd in preemptible kernel code.

---
 arch/powerpc/include/asm/simd.h      |  8 +++++
 arch/powerpc/include/asm/switch_to.h | 10 ++----
 arch/powerpc/kernel/process.c        | 50 ++++++++++++++++++++++++++--
 3 files changed, 57 insertions(+), 11 deletions(-)
arch/powerpc/include/asm/simd.h +8 -0
index 2c02ad531..7b582b07e 100644
@@ -7,7 +7,15 @@
  * It's always ok in process context (ie "not interrupt")
  * but it is sometimes ok even from an irq.
  */
+#ifdef CONFIG_ALTIVEC
+extern bool irq_simd_usable(void);
 static __must_check inline bool may_use_simd(void)
 {
 	return irq_simd_usable();
 }
+#else
+static inline bool may_use_simd(void)
+{
+	return false;
+}
+#endif
arch/powerpc/include/asm/switch_to.h +2 -8
index 5b03d8a82..537998997 100644
@@ -44,10 +44,7 @@ extern void enable_kernel_altivec(void);
 extern void flush_altivec_to_thread(struct task_struct *);
 extern void giveup_altivec(struct task_struct *);
 extern void save_altivec(struct task_struct *);
-static inline void disable_kernel_altivec(void)
-{
-	msr_check_and_clear(MSR_VEC);
-}
+extern void disable_kernel_altivec(void);
 #else
 static inline void save_altivec(struct task_struct *t) { }
 static inline void __giveup_altivec(struct task_struct *t) { }
@@ -56,10 +53,7 @@ static inline void __giveup_altivec(struct task_struct *t) { }
 #ifdef CONFIG_VSX
 extern void enable_kernel_vsx(void);
 extern void flush_vsx_to_thread(struct task_struct *);
-static inline void disable_kernel_vsx(void)
-{
-	msr_check_and_clear(MSR_FP|MSR_VEC|MSR_VSX);
-}
+extern void disable_kernel_vsx(void);
 #endif
 
 #ifdef CONFIG_SPE
arch/powerpc/kernel/process.c +47 -3

86 lines not on this page · raw message

Signature · 1 line
2.21.0.1020.gf2820cf01a
#5 lore raw

Re: [PATCH 1/2] [PowerPC] Add simd.h implementation

In reply to Shawn Landden
Quoted 6 lines · Shawn Landden
arch/powerpc/kernel/process.c +6 -0
+
+/*
+ * Were we in user mode when we were
+ * interrupted?
+ *
+ * Doing kernel_altivec/vsx_begin/end() is ok if we are running

arch/powerpc/kernel/process.c static int restore_altivec(struct task_struct *tsk) · line 363

Quoted 10 lines
arch/powerpc/kernel/process.c +10 -0
+ * in an interrupt context from user mode - we'll just
+ * save the FPU state as required.
+ */
+static bool interrupted_user_mode(void)
+{
+       struct pt_regs *regs = get_irq_regs();
+
+       return regs && user_mode(regs);
+}
+

That's interesting .... it *could* work but we'll have to careful audit the code to make sure thats ok.

We probably also want to handle the case where the CPU is in the idle loop.

Do we always save the user state when switching out these days ? If yes, then there's no "live" state to worry about...

Cheers,
Ben.

#6 lore raw

Re: [PATCH 1/2] [PowerPC] Add simd.h implementation

Quoted 22 lines · Benjamin Herrenschmidt
Quoted 6 lines · Shawn Landden
arch/powerpc/kernel/process.c +6 -0
+
+/*
+ * Were we in user mode when we were
+ * interrupted?
+ *
+ * Doing kernel_altivec/vsx_begin/end() is ok if we are running

arch/powerpc/kernel/process.c static int restore_altivec(struct task_struct *tsk) · line 363

Quoted 10 lines
arch/powerpc/kernel/process.c +10 -0
+ * in an interrupt context from user mode - we'll just
+ * save the FPU state as required.
+ */
+static bool interrupted_user_mode(void)
+{
+       struct pt_regs *regs = get_irq_regs();
+
+       return regs && user_mode(regs);
+}
+

That's interesting .... it *could* work but we'll have to careful audit the code to make sure thats ok.

Quoted 2 lines

We probably also want to handle the case where the CPU is in the idle loop.

That is the next patch. It is best to split these up because then git bisect works better, and these are higher-risk changes.

Quoted 8 lines

Do we always save the user state when switching out these days ? If yes, then there's no "live" state to worry about...

Cheers,
Ben.

#7 lore raw

PATCH v3 1/2 Add simd.h implementation

In reply to Shawn Landden

Based off the x86 one.

WireGuard really wants to be able to do SIMD in interrupts, so it can accelerate its in-bound path.

---
 arch/powerpc/include/asm/simd.h | 17 +++++++++++++++++
 arch/powerpc/kernel/process.c   | 30 ++++++++++++++++++++++++++++++
 2 files changed, 47 insertions(+)
 create mode 100644 arch/powerpc/include/asm/simd.h
arch/powerpc/include/asm/simd.h new file +17 -0
new file mode 100644
index 000000000..2fe26f258
@@ -0,0 +1,17 @@
+/* SPDX-License-Identifier: GPL-2.0+ */
+
+/*
+ * may_use_simd - whether it is allowable at this time to issue SIMD
+ *                instructions or access the SIMD register file
+ *
+ * It's always ok in process context (ie "not interrupt")
+ * but it is sometimes ok even from an irq.
+ */
+#ifdef CONFIG_PPC_FPU
+extern bool may_use_simd(void);
+#else
+static inline bool may_use_simd(void)
+{
+	return false;
+}
+#endif
arch/powerpc/kernel/process.c +30 -0
index dd9e0d538..ef534831f 100644
@@ -345,6 +345,36 @@ static int restore_altivec(struct task_struct *tsk)
 	}
 	return 0;
 }
+
+/*
+ * Were we in user mode when we were
+ * interrupted?
+ *
+ * Doing kernel_altivec/vsx_begin/end() is ok if we are running
+ * in an interrupt context from user mode - we'll just
+ * save the FPU state as required.
+ */
+static bool interrupted_user_mode(void)
+{
+	struct pt_regs *regs = get_irq_regs();
+
+	return regs && user_mode(regs);
+}
+
+/*
+ * Can we use FPU in kernel mode with the
+ * whole "kernel_fpu/altivec/vsx_begin/end()" sequence?
+ *
+ * It's always ok in process context (ie "not interrupt")
+ * but it is sometimes ok even from an irq.
+ */
+bool may_use_simd(void)
+{
+	return !in_interrupt() ||
+		interrupted_user_mode();
+}
+EXPORT_SYMBOL(may_use_simd);
+
 #else
 #define loadvec(thr) 0
 static inline int restore_altivec(struct task_struct *tsk) { return 0; }
Signature · 1 line
2.21.0.1020.gf2820cf01a
#8 lore raw

PATCH v3 2/2 Allow use of SIMD in interrupts from kernel code

This second patch is separate because it could be wrong, like I am not sure about how kernel thread migration works, and it is even allowing simd in preemptible kernel code.

---
 arch/powerpc/include/asm/switch_to.h | 15 ++-----
 arch/powerpc/kernel/process.c        | 60 ++++++++++++++++++++++++++--
 2 files changed, 59 insertions(+), 16 deletions(-)
arch/powerpc/include/asm/switch_to.h +3 -12
index 5b03d8a82..c79f7d24a 100644
@@ -30,10 +30,7 @@ extern void enable_kernel_fp(void);
 extern void flush_fp_to_thread(struct task_struct *);
 extern void giveup_fpu(struct task_struct *);
 extern void save_fpu(struct task_struct *);
-static inline void disable_kernel_fp(void)
-{
-	msr_check_and_clear(MSR_FP);
-}
+extern void disable_kernel_fp(void);
 #else
 static inline void save_fpu(struct task_struct *t) { }
 static inline void flush_fp_to_thread(struct task_struct *t) { }
@@ -44,10 +41,7 @@ extern void enable_kernel_altivec(void);
 extern void flush_altivec_to_thread(struct task_struct *);
 extern void giveup_altivec(struct task_struct *);
 extern void save_altivec(struct task_struct *);
-static inline void disable_kernel_altivec(void)
-{
-	msr_check_and_clear(MSR_VEC);
-}
+extern void disable_kernel_altivec(void);
 #else
 static inline void save_altivec(struct task_struct *t) { }
 static inline void __giveup_altivec(struct task_struct *t) { }
@@ -56,10 +50,7 @@ static inline void __giveup_altivec(struct task_struct *t) { }
 #ifdef CONFIG_VSX
 extern void enable_kernel_vsx(void);
 extern void flush_vsx_to_thread(struct task_struct *);
-static inline void disable_kernel_vsx(void)
-{
-	msr_check_and_clear(MSR_FP|MSR_VEC|MSR_VSX);
-}
+extern void disable_kernel_vsx(void);
 #endif
 
 #ifdef CONFIG_SPE
arch/powerpc/kernel/process.c +56 -4

108 lines not on this page · raw message

Signature · 1 line
2.21.0.1020.gf2820cf01a
#9 lore raw

Re: [v3 2/2] [PowerPC] Allow use of SIMD in interrupts from kernel code

Thank you for the patch! Yet something to improve:

Hi Shawn,

Thank you for the patch! Yet something to improve:

[auto build test ERROR on powerpc/next]
[also build test ERROR on v5.1 next-20190514]
[if your patch is applied to the wrong git tree, please drop us a note to help improve the system]

url:    https://github.com/0day-ci/linux/commits/Shawn-Landden/Add-simd-h-implementation/20190515-062235
base:   https://git.kernel.org/pub/scm/linux/kernel/git/powerpc/linux.git next
config: powerpc-mpc836x_mds_defconfig (attached as .config)
compiler: powerpc-linux-gnu-gcc (Debian 7.2.0-11) 7.2.0

reproduce:
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# save the attached .config to linux build tree
GCC_VERSION=7.2.0 make.cross ARCH=powerpc

If you fix the issue, kindly add following tag

All errors (new ones prefixed by >>):

>> arch/powerpc/kernel/process.c:194:13: error: 'interrupted_kernel_fpu_idle' defined but not used [-Werror=unused-function]

static bool interrupted_kernel_fpu_idle(void)
^~~~~~~~~~~~~~~~~~~~~~~~~~~
cc1: all warnings being treated as errors

vim +/interrupted_kernel_fpu_idle +194 arch/powerpc/kernel/process.c

193

 > 194	static bool interrupted_kernel_fpu_idle(void)

195 {
196 return !kernel_fpu_disabled();
197 }
198

---
0-DAY kernel test infrastructure Open Source Technology Center
https://lists.01.org/pipermail/kbuild-all Intel Corporation

#10 lore raw

PATCH v2 1/2 Add simd.h implementation

In reply to Shawn Landden

Based off the x86 one.

WireGuard really wants to be able to do SIMD in interrupts, so it can accelerate its in-bound path.

---
 arch/powerpc/include/asm/simd.h | 10 ++++++++++
 arch/powerpc/kernel/process.c   | 30 ++++++++++++++++++++++++++++++
 2 files changed, 40 insertions(+)
 create mode 100644 arch/powerpc/include/asm/simd.h
arch/powerpc/include/asm/simd.h new file +10 -0
new file mode 100644
index 000000000..9b066d633
@@ -0,0 +1,10 @@
+/* SPDX-License-Identifier: GPL-2.0+ */
+
+/*
+ * may_use_simd - whether it is allowable at this time to issue SIMD
+ *                instructions or access the SIMD register file
+ *
+ * It's always ok in process context (ie "not interrupt")
+ * but it is sometimes ok even from an irq.
+ */
+extern bool may_use_simd(void);
arch/powerpc/kernel/process.c +30 -0
index dd9e0d538..ef534831f 100644
@@ -345,6 +345,36 @@ static int restore_altivec(struct task_struct *tsk)
 	}
 	return 0;
 }
+
+/*
+ * Were we in user mode when we were
+ * interrupted?
+ *
+ * Doing kernel_altivec/vsx_begin/end() is ok if we are running
+ * in an interrupt context from user mode - we'll just
+ * save the FPU state as required.
+ */
+static bool interrupted_user_mode(void)
+{
+	struct pt_regs *regs = get_irq_regs();
+
+	return regs && user_mode(regs);
+}
+
+/*
+ * Can we use FPU in kernel mode with the
+ * whole "kernel_fpu/altivec/vsx_begin/end()" sequence?
+ *
+ * It's always ok in process context (ie "not interrupt")
+ * but it is sometimes ok even from an irq.
+ */
+bool may_use_simd(void)
+{
+	return !in_interrupt() ||
+		interrupted_user_mode();
+}
+EXPORT_SYMBOL(may_use_simd);
+
 #else
 #define loadvec(thr) 0
 static inline int restore_altivec(struct task_struct *tsk) { return 0; }
Signature · 1 line
2.21.0.1020.gf2820cf01a
#11 lore raw

PATCH v2 2/2 Allow use of SIMD in interrupts from kernel code

This second patch is separate because it could be wrong, like I am not sure about how kernel thread migration works, and it is even allowing simd in preemptible kernel code.

---
 arch/powerpc/include/asm/switch_to.h | 15 ++-----
 arch/powerpc/kernel/process.c        | 60 ++++++++++++++++++++++++++--
 2 files changed, 59 insertions(+), 16 deletions(-)
arch/powerpc/include/asm/switch_to.h +3 -12
index 5b03d8a82..c79f7d24a 100644
@@ -30,10 +30,7 @@ extern void enable_kernel_fp(void);
 extern void flush_fp_to_thread(struct task_struct *);
 extern void giveup_fpu(struct task_struct *);
 extern void save_fpu(struct task_struct *);
-static inline void disable_kernel_fp(void)
-{
-	msr_check_and_clear(MSR_FP);
-}
+extern void disable_kernel_fp(void);
 #else
 static inline void save_fpu(struct task_struct *t) { }
 static inline void flush_fp_to_thread(struct task_struct *t) { }
@@ -44,10 +41,7 @@ extern void enable_kernel_altivec(void);
 extern void flush_altivec_to_thread(struct task_struct *);
 extern void giveup_altivec(struct task_struct *);
 extern void save_altivec(struct task_struct *);
-static inline void disable_kernel_altivec(void)
-{
-	msr_check_and_clear(MSR_VEC);
-}
+extern void disable_kernel_altivec(void);
 #else
 static inline void save_altivec(struct task_struct *t) { }
 static inline void __giveup_altivec(struct task_struct *t) { }
@@ -56,10 +50,7 @@ static inline void __giveup_altivec(struct task_struct *t) { }
 #ifdef CONFIG_VSX
 extern void enable_kernel_vsx(void);
 extern void flush_vsx_to_thread(struct task_struct *);
-static inline void disable_kernel_vsx(void)
-{
-	msr_check_and_clear(MSR_FP|MSR_VEC|MSR_VSX);
-}
+extern void disable_kernel_vsx(void);
 #endif
 
 #ifdef CONFIG_SPE
arch/powerpc/kernel/process.c +56 -4

108 lines not on this page · raw message

Signature · 1 line
2.21.0.1020.gf2820cf01a
#12 lore raw

Re: [v2 2/2] [PowerPC] Allow use of SIMD in interrupts from kernel code

Quoted 6 lines · Shawn Landden

This second patch is separate because it could be wrong, like I am not sure about how kernel thread migration works, and it is even allowing simd in preemptible kernel code.

---

Hi Shawn,

This patch doesn't build on 64-bit embedded (ppc64e_defconfig):

arch/powerpc/kernel/process.c:194:13: error: 'interrupted_kernel_fpu_idle' defined but not used [-Werror=unused-function]
static bool interrupted_kernel_fpu_idle(void)

             ^~~~~~~~~~~~~~~~~~~~~~~~~~~

and otherwise adds two sparse warnings:

+arch/powerpc/kernel/process.c:356:13: warning: function 'disable_kernel_altivec' with external linkage has definition +arch/powerpc/kernel/process.c:416:6: warning: symbol 'may_use_simd' was not declared. Should it be static?

There's also some style issues (spaces instead of tabs).

Reported by snowpatch (see https://patchwork.ozlabs.org/patch/1099181/)

- Russell

#13 lore raw

Re: [v2 2/2] [PowerPC] Allow use of SIMD in interrupts from kernel code

Quoted 9 lines · Russell Currey
Quoted 6 lines · Shawn Landden

This second patch is separate because it could be wrong, like I am not sure about how kernel thread migration works, and it is even allowing simd in preemptible kernel code.

---

Quoted 7 lines

Hi Shawn,

This patch doesn't build on 64-bit embedded (ppc64e_defconfig):

arch/powerpc/kernel/process.c:194:13: error: 'interrupted_kernel_fpu_idle' defined but not used [-Werror=unused-function]
static bool interrupted_kernel_fpu_idle(void)

             ^~~~~~~~~~~~~~~~~~~~~~~~~~~

Thanks for noticing this. I knew what I needed to do, and this was just sloppiness on my end.

Review

Quoted 5 lines

and otherwise adds two sparse warnings:

(quoted diff) +2 -0
+arch/powerpc/kernel/process.c:356:13: warning: function 'disable_kernel_altivec' with external linkage has definition
+arch/powerpc/kernel/process.c:416:6: warning: symbol 'may_use_simd' was not declared. Should it be static?

This is the same problem as above.

Quoted 2 lines

There's also some style issues (spaces instead of tabs).

Yes, I have to be careful using nano on a VPS.

New patch coming in 1 sec.

Quoted 5 lines

Reported by snowpatch (see https://patchwork.ozlabs.org/patch/1099181/)

- Russell

#14 lore raw

PATCH 1/1 powerpc: Allow may_use_simd() to function as feature detection

In reply to Shawn Landden

ARM does this, so we might as well too.
I am a bit confused however as CONFIG_ALTIVEC does not select CONFIG_PPC_FPU. Would you ever have altivec without a fpu?

---
 arch/powerpc/include/asm/simd.h | 7 +++++++
 1 file changed, 7 insertions(+)
arch/powerpc/include/asm/simd.h +7 -0
index 9b066d633..2fe26f258 100644
@@ -7,4 +7,11 @@
  * It's always ok in process context (ie "not interrupt")
  * but it is sometimes ok even from an irq.
  */
+#ifdef CONFIG_PPC_FPU
 extern bool may_use_simd(void);
+#else
+static inline bool may_use_simd(void)
+{
+	return false;
+}
+#endif
Signature · 1 line
2.21.0.1020.gf2820cf01a
#15 lore raw

Re: [PATCH] powerpc: Allow may_use_simd() to function as feature detection

Quoted 3 lines · Shawn Landden

ARM does this, so we might as well too.
I am a bit confused however as CONFIG_ALTIVEC does not select CONFIG_PPC_FPU. Would you ever have altivec without a fpu?

There is no hardware like that, none supported anyway. It does not make very much sense, and it cannot happen with VSX, so no hardware like it will ever show up most likely.

It is much simpler to just make a Kconfig dependency (or a select) between the symbols than to have to add code like this patch.

Segher

#16 lore raw

Re: [PATCH] powerpc: Allow may_use_simd() to function as feature detection

Wireguard is not in the kernel (yet) and uses these symbols. (It is IS_ENABLED so doesn't need it, but I think it is reasonable) I think these enable/disable symbols should not be marked GPL-only.

Quoted 17 lines · Segher Boessenkool
Quoted 3 lines · Shawn Landden

ARM does this, so we might as well too.
I am a bit confused however as CONFIG_ALTIVEC does not select CONFIG_PPC_FPU. Would you ever have altivec without a fpu?

There is no hardware like that, none supported anyway. It does not make very much sense, and it cannot happen with VSX, so no hardware like it will ever show up most likely.

It is much simpler to just make a Kconfig dependency (or a select) between the symbols than to have to add code like this patch.

Segher

#17 lore raw

PATCH 1/2 Add simd.h implementation

In reply to Shawn Landden

Based off the x86 one.

WireGuard really wants to be able to do SIMD in interrupts, so it can accelerate its in-bound path.

---
 arch/powerpc/include/asm/simd.h | 17 +++++++++++++++++
 arch/powerpc/kernel/process.c   | 30 ++++++++++++++++++++++++++++++
 2 files changed, 47 insertions(+)
 create mode 100644 arch/powerpc/include/asm/simd.h
arch/powerpc/include/asm/simd.h new file +17 -0
new file mode 100644
index 000000000..2fe26f258
@@ -0,0 +1,17 @@
+/* SPDX-License-Identifier: GPL-2.0+ */
+
+/*
+ * may_use_simd - whether it is allowable at this time to issue SIMD
+ *                instructions or access the SIMD register file
+ *
+ * It's always ok in process context (ie "not interrupt")
+ * but it is sometimes ok even from an irq.
+ */
+#ifdef CONFIG_PPC_FPU
+extern bool may_use_simd(void);
+#else
+static inline bool may_use_simd(void)
+{
+	return false;
+}
+#endif
arch/powerpc/kernel/process.c +30 -0
index dd9e0d538..ef534831f 100644
@@ -345,6 +345,36 @@ static int restore_altivec(struct task_struct *tsk)
 	}
 	return 0;
 }
+
+/*
+ * Were we in user mode when we were
+ * interrupted?
+ *
+ * Doing kernel_altivec/vsx_begin/end() is ok if we are running
+ * in an interrupt context from user mode - we'll just
+ * save the FPU state as required.
+ */
+static bool interrupted_user_mode(void)
+{
+	struct pt_regs *regs = get_irq_regs();
+
+	return regs && user_mode(regs);
+}
+
+/*
+ * Can we use FPU in kernel mode with the
+ * whole "kernel_fpu/altivec/vsx_begin/end()" sequence?
+ *
+ * It's always ok in process context (ie "not interrupt")
+ * but it is sometimes ok even from an irq.
+ */
+bool may_use_simd(void)
+{
+	return !in_interrupt() ||
+		interrupted_user_mode();
+}
+EXPORT_SYMBOL(may_use_simd);
+
 #else
 #define loadvec(thr) 0
 static inline int restore_altivec(struct task_struct *tsk) { return 0; }
Signature · 1 line
2.21.0.1020.gf2820cf01a
#18 lore raw

PATCH v4 1/2 Add simd.h implementation

In reply to Shawn Landden

Based off the x86 one.

WireGuard really wants to be able to do SIMD in interrupts, so it can accelerate its in-bound path.

---
 arch/powerpc/include/asm/simd.h | 17 +++++++++++++++++
 arch/powerpc/kernel/process.c   | 30 ++++++++++++++++++++++++++++++
 2 files changed, 47 insertions(+)
 create mode 100644 arch/powerpc/include/asm/simd.h
arch/powerpc/include/asm/simd.h new file +17 -0
new file mode 100644
index 000000000..2fe26f258
@@ -0,0 +1,17 @@
+/* SPDX-License-Identifier: GPL-2.0+ */
+
+/*
+ * may_use_simd - whether it is allowable at this time to issue SIMD
+ *                instructions or access the SIMD register file
+ *
+ * It's always ok in process context (ie "not interrupt")
+ * but it is sometimes ok even from an irq.
+ */
+#ifdef CONFIG_PPC_FPU
+extern bool may_use_simd(void);
+#else
+static inline bool may_use_simd(void)
+{
+	return false;
+}
+#endif
arch/powerpc/kernel/process.c +30 -0
index dd9e0d538..ef534831f 100644
@@ -345,6 +345,36 @@ static int restore_altivec(struct task_struct *tsk)
 	}
 	return 0;
 }
+
+/*
+ * Were we in user mode when we were
+ * interrupted?
+ *
+ * Doing kernel_altivec/vsx_begin/end() is ok if we are running
+ * in an interrupt context from user mode - we'll just
+ * save the FPU state as required.
+ */
+static bool interrupted_user_mode(void)
+{
+	struct pt_regs *regs = get_irq_regs();
+
+	return regs && user_mode(regs);
+}
+
+/*
+ * Can we use FPU in kernel mode with the
+ * whole "kernel_fpu/altivec/vsx_begin/end()" sequence?
+ *
+ * It's always ok in process context (ie "not interrupt")
+ * but it is sometimes ok even from an irq.
+ */
+bool may_use_simd(void)
+{
+	return !in_interrupt() ||
+		interrupted_user_mode();
+}
+EXPORT_SYMBOL(may_use_simd);
+
 #else
 #define loadvec(thr) 0
 static inline int restore_altivec(struct task_struct *tsk) { return 0; }
Signature · 1 line
2.21.0.1020.gf2820cf01a
#19 lore raw

PATCH v4 2/2 Allow use of SIMD in interrupts from kernel code

This second patch is separate because it could be wrong, like I am not sure about how kernel thread migration works, and it is even allowing simd in preemptible kernel code.

v4: fix build without CONFIG_AVX

---
 arch/powerpc/include/asm/switch_to.h |  15 +---
 arch/powerpc/kernel/process.c        | 117 +++++++++++++++++++--------
 2 files changed, 88 insertions(+), 44 deletions(-)
arch/powerpc/include/asm/switch_to.h +3 -12
index 5b03d8a82..c79f7d24a 100644
@@ -30,10 +30,7 @@ extern void enable_kernel_fp(void);
 extern void flush_fp_to_thread(struct task_struct *);
 extern void giveup_fpu(struct task_struct *);
 extern void save_fpu(struct task_struct *);
-static inline void disable_kernel_fp(void)
-{
-	msr_check_and_clear(MSR_FP);
-}
+extern void disable_kernel_fp(void);
 #else
 static inline void save_fpu(struct task_struct *t) { }
 static inline void flush_fp_to_thread(struct task_struct *t) { }
@@ -44,10 +41,7 @@ extern void enable_kernel_altivec(void);
 extern void flush_altivec_to_thread(struct task_struct *);
 extern void giveup_altivec(struct task_struct *);
 extern void save_altivec(struct task_struct *);
-static inline void disable_kernel_altivec(void)
-{
-	msr_check_and_clear(MSR_VEC);
-}
+extern void disable_kernel_altivec(void);
 #else
 static inline void save_altivec(struct task_struct *t) { }
 static inline void __giveup_altivec(struct task_struct *t) { }
@@ -56,10 +50,7 @@ static inline void __giveup_altivec(struct task_struct *t) { }
 #ifdef CONFIG_VSX
 extern void enable_kernel_vsx(void);
 extern void flush_vsx_to_thread(struct task_struct *);
-static inline void disable_kernel_vsx(void)
-{
-	msr_check_and_clear(MSR_FP|MSR_VEC|MSR_VSX);
-}
+extern void disable_kernel_vsx(void);
 #endif
 
 #ifdef CONFIG_SPE
arch/powerpc/kernel/process.c +85 -32

171 lines not on this page · raw message

Signature · 1 line
2.21.0.1020.gf2820cf01a
#20 lore raw

Re: [v4 PATCH 1/2] [PowerPC] Add simd.h implementation

In reply to Shawn Landden

Le 15/05/2019 à 03:37, Shawn Landden a écrit :

Quoted 7 lines

Based off the x86 one.

WireGuard really wants to be able to do SIMD in interrupts, so it can accelerate its in-bound path.

---

Could you please as usual list here the changes provided by each version to ease the review ?

Thanks
Christophe

Quoted 70 lines
  arch/powerpc/include/asm/simd.h | 17 +++++++++++++++++
  arch/powerpc/kernel/process.c   | 30 ++++++++++++++++++++++++++++++
  2 files changed, 47 insertions(+)

create mode 100644 arch/powerpc/include/asm/simd.h

arch/powerpc/include/asm/simd.h new file +17 -0
new file mode 100644
index 000000000..2fe26f258
@@ -0,0 +1,17 @@
+/* SPDX-License-Identifier: GPL-2.0+ */
+
+/*
+ * may_use_simd - whether it is allowable at this time to issue SIMD
+ *                instructions or access the SIMD register file
+ *
+ * It's always ok in process context (ie "not interrupt")
+ * but it is sometimes ok even from an irq.
+ */
+#ifdef CONFIG_PPC_FPU
+extern bool may_use_simd(void);
+#else
+static inline bool may_use_simd(void)
+{
+	return false;
+}
+#endif
arch/powerpc/kernel/process.c +30 -0
index dd9e0d538..ef534831f 100644
@@ -345,6 +345,36 @@ static int restore_altivec(struct task_struct *tsk)
  	}
  	return 0;
  }
+
+/*
+ * Were we in user mode when we were
+ * interrupted?
+ *
+ * Doing kernel_altivec/vsx_begin/end() is ok if we are running
+ * in an interrupt context from user mode - we'll just
+ * save the FPU state as required.
+ */
+static bool interrupted_user_mode(void)
+{
+	struct pt_regs *regs = get_irq_regs();
+
+	return regs && user_mode(regs);
+}
+
+/*
+ * Can we use FPU in kernel mode with the
+ * whole "kernel_fpu/altivec/vsx_begin/end()" sequence?
+ *
+ * It's always ok in process context (ie "not interrupt")
+ * but it is sometimes ok even from an irq.
+ */
+bool may_use_simd(void)
+{
+	return !in_interrupt() ||
+		interrupted_user_mode();
+}
+EXPORT_SYMBOL(may_use_simd);
+
  #else
  #define loadvec(thr) 0
  static inline int restore_altivec(struct task_struct *tsk) { return 0; }
#21 lore raw

Re: [v4 PATCH 1/2] [PowerPC] Add simd.h implementation

Quoted 2 lines · Christophe Leroy

Could you please as usual list here the changes provided by each version to ease the review ?

A bunch of embarrassing stuff that caused it not to build on some set-ups (the functions were under the wrong include guards), and I added include guards on simd.h so that you can use may_use_simd() even if you don't have the FPU enabled (ARM's simd.h does this).

#22 lore raw

PATCH RESEND v4 1/2 Add simd.h implementation

In reply to Shawn Landden

Based off the x86 one.

WireGuard really wants to be able to do SIMD in interrupts, so it can accelerate its in-bound path.

v4: allow using the may_use_simd symbol even when it always
returns false (via include guards)

---
 arch/powerpc/include/asm/simd.h | 17 +++++++++++++++++
 arch/powerpc/kernel/process.c   | 30 ++++++++++++++++++++++++++++++
 2 files changed, 47 insertions(+)
 create mode 100644 arch/powerpc/include/asm/simd.h
arch/powerpc/include/asm/simd.h new file +17 -0
new file mode 100644
index 000000000..2fe26f258
@@ -0,0 +1,17 @@
+/* SPDX-License-Identifier: GPL-2.0+ */
+
+/*
+ * may_use_simd - whether it is allowable at this time to issue SIMD
+ *                instructions or access the SIMD register file
+ *
+ * It's always ok in process context (ie "not interrupt")
+ * but it is sometimes ok even from an irq.
+ */
+#ifdef CONFIG_PPC_FPU
+extern bool may_use_simd(void);
+#else
+static inline bool may_use_simd(void)
+{
+	return false;
+}
+#endif
arch/powerpc/kernel/process.c +30 -0
index dd9e0d538..ef534831f 100644
@@ -345,6 +345,36 @@ static int restore_altivec(struct task_struct *tsk)
 	}
 	return 0;
 }
+
+/*
+ * Were we in user mode when we were
+ * interrupted?
+ *
+ * Doing kernel_altivec/vsx_begin/end() is ok if we are running
+ * in an interrupt context from user mode - we'll just
+ * save the FPU state as required.
+ */
+static bool interrupted_user_mode(void)
+{
+	struct pt_regs *regs = get_irq_regs();
+
+	return regs && user_mode(regs);
+}
+
+/*
+ * Can we use FPU in kernel mode with the
+ * whole "kernel_fpu/altivec/vsx_begin/end()" sequence?
+ *
+ * It's always ok in process context (ie "not interrupt")
+ * but it is sometimes ok even from an irq.
+ */
+bool may_use_simd(void)
+{
+	return !in_interrupt() ||
+		interrupted_user_mode();
+}
+EXPORT_SYMBOL(may_use_simd);
+
 #else
 #define loadvec(thr) 0
 static inline int restore_altivec(struct task_struct *tsk) { return 0; }
Signature · 1 line
2.21.0.1020.gf2820cf01a
#23 lore raw

PATCH RESEND v4 2/2 Allow use of SIMD in interrupts from kernel code

This even allows simd in preemptible kernel code, as does x86, although this is rarely safe (could be used with kthread_create_on_cpu). All callers are disabling preemption.

v4: fix build without CONFIG_AVX
change commit message

---
 arch/powerpc/include/asm/switch_to.h |  15 +---
 arch/powerpc/kernel/process.c        | 117 +++++++++++++++++++--------
 2 files changed, 88 insertions(+), 44 deletions(-)
arch/powerpc/include/asm/switch_to.h +3 -12
index 5b03d8a82..c79f7d24a 100644
@@ -30,10 +30,7 @@ extern void enable_kernel_fp(void);
 extern void flush_fp_to_thread(struct task_struct *);
 extern void giveup_fpu(struct task_struct *);
 extern void save_fpu(struct task_struct *);
-static inline void disable_kernel_fp(void)
-{
-	msr_check_and_clear(MSR_FP);
-}
+extern void disable_kernel_fp(void);
 #else
 static inline void save_fpu(struct task_struct *t) { }
 static inline void flush_fp_to_thread(struct task_struct *t) { }
@@ -44,10 +41,7 @@ extern void enable_kernel_altivec(void);
 extern void flush_altivec_to_thread(struct task_struct *);
 extern void giveup_altivec(struct task_struct *);
 extern void save_altivec(struct task_struct *);
-static inline void disable_kernel_altivec(void)
-{
-	msr_check_and_clear(MSR_VEC);
-}
+extern void disable_kernel_altivec(void);
 #else
 static inline void save_altivec(struct task_struct *t) { }
 static inline void __giveup_altivec(struct task_struct *t) { }
@@ -56,10 +50,7 @@ static inline void __giveup_altivec(struct task_struct *t) { }
 #ifdef CONFIG_VSX
 extern void enable_kernel_vsx(void);
 extern void flush_vsx_to_thread(struct task_struct *);
-static inline void disable_kernel_vsx(void)
-{
-	msr_check_and_clear(MSR_FP|MSR_VEC|MSR_VSX);
-}
+extern void disable_kernel_vsx(void);
 #endif
 
 #ifdef CONFIG_SPE
arch/powerpc/kernel/process.c +85 -32

171 lines not on this page · raw message

Signature · 1 line
2.21.0.1020.gf2820cf01a