Skip to contentSkip to conversation

FP/VEC/VSX switching optimisations

PATCH Superseded Cyril Bur 3 participants 20 messages Jan 15, 2016 – Jan 18, 2016

Subsystems (2)

v2 of 5 Superseded Latest is v5 →

lore
Share & tools
lore

Cyril Bur

author
#0 lore raw

v2 cover FP/VEC/VSX switching optimisations

Cover-letter for V1 of the series is at
https://lists.ozlabs.org/pipermail/linuxppc-dev/2015-November/136350.html

Version one of this series used a cmpb instruction in handcrafted assembly which it turns out is not supported on older power machines. Michael suggested replacing it with crandc, which instruction works fine. Testing also showed no difference in performance between using cmpb and crandc.

The primary objective improving the syscall hot path. While gut feelings may be that avoiding C is quicker it may also be the case that the C is not significantly slower. If C is not slower using C would provide a distinct readability and maintainability advantage.
I have benchmarked a few possible scenarios:
1. Always calling into C.
2. Testing for the common case in assembly and calling into C
3. Using crandc in the full assembly check

All benchmarks are the average of 50 runs of Antons context switch benchmark http://www.ozlabs.org/~anton/junkcode/context_switch2.c with the kernel and ramdisk run under QEMU/KVM on a POWER8.
To test for all cases a variety of flags were passed to the benchmark to see the effect of only touching a subset of the 'math' register space.

The absolute numbers are in context switches per second can vary greatly depending on the how the kernel is run (virt/powernv/ramdisk/disk) and as such units aren't very relevant here as we're interested in a speedup. The most interesting number here is the %speedup over the previous scenario. In this case 100% means there was no difference, therefore <100% indicates a decrease in performance and >100% an increase.

For 1 - Always calling into C
Flags | Average | Stddev |
========================================

          none | 2059785.00 | 14217.64 |
            fp | 1766297.65 | 10576.64 |
    fp altivec | 1636125.04 | 5693.84  |
     fp vector | 1640951.76 | 13141.93 |
       altivec | 1815133.80 | 10450.46 |
altivec vector | 1636438.60 | 5475.12  |
        vector | 1639628.16 | 11456.06 |
           all | 1629516.32 | 7785.36  |

For 2 - Common case checking in asm before calling into C
Flags | Average | Stddev | %speedup vs 1 |
========================================================

          none | 2058003.64 | 20464.22 | 99.91         |
            fp | 1757245.80 | 14455.45 | 99.49         |
    fp altivec | 1658240.12 | 6318.41  | 101.35        |
     fp vector | 1668912.96 | 9451.47  | 101.70        |
       altivec | 1815223.96 | 4819.82  | 100.00        |
altivec vector | 1648805.32 | 15100.50 | 100.76        |
        vector | 1663654.68 | 13814.79 | 101.47        |
           all | 1644884.04 | 11315.74 | 100.94        |

For 3 - Full checking in ASM using crandc instead of cmpb
Flags | Average | Stddev | %speedup vs 2 |
========================================================

          none | 2066930.52 | 19426.46 | 100.43        |
            fp | 1781653.24 | 7744.55  | 101.39        |
    fp altivec | 1653125.84 | 6727.36  | 99.69         |
     fp vector | 1656011.04 | 11678.56 | 99.23         |
       altivec | 1824934.72 | 16842.19 | 100.53        |
altivec vector | 1649486.92 | 3219.14  | 100.04        |
        vector | 1662420.20 | 9609.34  | 99.93         |
           all | 1647933.64 | 11121.22 | 100.19        |
Quoted 1 line

From these numbers it appears that reducing the call to C in the common

case is beneficial, possibly up to 1.5% speedup over always calling C. The benefit of the more complicated asm checking does appear to be very slight, fractions of a percent at best. In balance it may prove wise to use the option 2, there are much bigger fish to fry in terms of performance, the complexity of the assembly for a small fraction of one percent improvement is not worth it at this stage.

Version 2 of this series also addresses some comments from Mikey Neuling in the tests such as adding .gitignore and forcing 64 bit compiles of the tests as they use 64 bit only instructions.

Cyril Bur (8):
selftests/powerpc: Test the preservation of FPU and VMX regs across
syscall
selftests/powerpc: Test preservation of FPU and VMX regs across
preemption
selftests/powerpc: Test FPU and VMX regs in signal ucontext
powerpc: Explicitly disable math features when copying thread
powerpc: Restore FPU/VEC/VSX if previously used
powerpc: Add the ability to save FPU without giving it up
powerpc: Add the ability to save Altivec without giving it up
powerpc: Add the ability to save VSX without giving it up

 arch/powerpc/include/asm/processor.h               |   2 +
 arch/powerpc/include/asm/switch_to.h               |   5 +-
 arch/powerpc/kernel/asm-offsets.c                  |   2 +
 arch/powerpc/kernel/entry_64.S                     |  21 +-
 arch/powerpc/kernel/fpu.S                          |  25 +--
 arch/powerpc/kernel/ppc_ksyms.c                    |   4 -
 arch/powerpc/kernel/process.c                      | 144 +++++++++++--
 arch/powerpc/kernel/vector.S                       |  45 +---
 tools/testing/selftests/powerpc/Makefile           |   3 +-
 tools/testing/selftests/powerpc/basic_asm.h        |  26 +++
 tools/testing/selftests/powerpc/math/.gitignore    |   6 +
 tools/testing/selftests/powerpc/math/Makefile      |  19 ++
 tools/testing/selftests/powerpc/math/fpu_asm.S     | 195 ++++++++++++++++++
 tools/testing/selftests/powerpc/math/fpu_preempt.c | 113 ++++++++++
 tools/testing/selftests/powerpc/math/fpu_signal.c  | 135 ++++++++++++
 tools/testing/selftests/powerpc/math/fpu_syscall.c |  90 ++++++++
 tools/testing/selftests/powerpc/math/vmx_asm.S     | 229 +++++++++++++++++++++
 tools/testing/selftests/powerpc/math/vmx_preempt.c | 113 ++++++++++
 tools/testing/selftests/powerpc/math/vmx_signal.c  | 138 +++++++++++++
 tools/testing/selftests/powerpc/math/vmx_syscall.c |  92 +++++++++
 20 files changed, 1326 insertions(+), 81 deletions(-)
 create mode 100644 tools/testing/selftests/powerpc/basic_asm.h
 create mode 100644 tools/testing/selftests/powerpc/math/.gitignore
 create mode 100644 tools/testing/selftests/powerpc/math/Makefile
 create mode 100644 tools/testing/selftests/powerpc/math/fpu_asm.S
 create mode 100644 tools/testing/selftests/powerpc/math/fpu_preempt.c
 create mode 100644 tools/testing/selftests/powerpc/math/fpu_signal.c
 create mode 100644 tools/testing/selftests/powerpc/math/fpu_syscall.c
 create mode 100644 tools/testing/selftests/powerpc/math/vmx_asm.S
 create mode 100644 tools/testing/selftests/powerpc/math/vmx_preempt.c
 create mode 100644 tools/testing/selftests/powerpc/math/vmx_signal.c
 create mode 100644 tools/testing/selftests/powerpc/math/vmx_syscall.c
Signature · 1 line
2.7.0
author
#1 lore raw

PATCH v2 1/8 selftests/powerpc: Test the preservation of FPU and VMX regs across syscall

Test that the non volatile floating point and Altivec registers get correctly preserved across the fork() syscall.

fork() works nicely for this purpose, the registers should be the same for both parent and child

---
 tools/testing/selftests/powerpc/Makefile           |   3 +-
 tools/testing/selftests/powerpc/basic_asm.h        |  26 +++
 tools/testing/selftests/powerpc/math/.gitignore    |   2 +
 tools/testing/selftests/powerpc/math/Makefile      |  14 ++
 tools/testing/selftests/powerpc/math/fpu_asm.S     | 161 +++++++++++++++++
 tools/testing/selftests/powerpc/math/fpu_syscall.c |  90 ++++++++++
 tools/testing/selftests/powerpc/math/vmx_asm.S     | 193 +++++++++++++++++++++
 tools/testing/selftests/powerpc/math/vmx_syscall.c |  92 ++++++++++
 8 files changed, 580 insertions(+), 1 deletion(-)
 create mode 100644 tools/testing/selftests/powerpc/basic_asm.h
 create mode 100644 tools/testing/selftests/powerpc/math/.gitignore
 create mode 100644 tools/testing/selftests/powerpc/math/Makefile
 create mode 100644 tools/testing/selftests/powerpc/math/fpu_asm.S
 create mode 100644 tools/testing/selftests/powerpc/math/fpu_syscall.c
 create mode 100644 tools/testing/selftests/powerpc/math/vmx_asm.S
 create mode 100644 tools/testing/selftests/powerpc/math/vmx_syscall.c
tools/testing/selftests/powerpc/Makefile +2 -1
index 0c2706b..19e8191 100644
@@ -22,7 +22,8 @@ SUB_DIRS = benchmarks 		\
 	   switch_endian	\
 	   syscalls		\
 	   tm			\
-	   vphn
+	   vphn         \
+	   math
 
 endif
 
tools/testing/selftests/powerpc/basic_asm.h new file +26 -0
new file mode 100644
index 0000000..27aca79
@@ -0,0 +1,26 @@
+#include <ppc-asm.h>
+#include <asm/unistd.h>
+
+#define LOAD_REG_IMMEDIATE(reg,expr) \
+	lis	reg,(expr)@highest;	\
+	ori	reg,reg,(expr)@higher;	\
+	rldicr	reg,reg,32,31;	\
+	oris	reg,reg,(expr)@high;	\
+	ori	reg,reg,(expr)@l;
+
+#define PUSH_BASIC_STACK(size) \
+	std	2,24(sp); \
+	mflr	r0; \
+	std	r0,16(sp); \
+	mfcr	r0; \
+	stw	r0,8(sp); \
+	stdu	sp,-size(sp);
+
+#define POP_BASIC_STACK(size) \
+	addi	sp,sp,size; \
+	ld	2,24(sp); \
+	ld	r0,16(sp); \
+	mtlr	r0; \
+	lwz	r0,8(sp); \
+	mtcr	r0; \
+
tools/testing/selftests/powerpc/math/.gitignore new file +2 -0
new file mode 100644
index 0000000..b19b269
@@ -0,0 +1,2 @@
+fpu_syscall
+vmx_syscall
tools/testing/selftests/powerpc/math/Makefile new file +14 -0
new file mode 100644
index 0000000..418bef1
@@ -0,0 +1,14 @@
+TEST_PROGS := fpu_syscall vmx_syscall
+
+all: $(TEST_PROGS)
+
+$(TEST_PROGS): ../harness.c
+$(TEST_PROGS): CFLAGS += -O2 -g -pthread -m64 -maltivec
+
+fpu_syscall: fpu_asm.S
+vmx_syscall: vmx_asm.S
+
+include ../../lib.mk
+
+clean:
+	rm -f $(TEST_PROGS) *.o
tools/testing/selftests/powerpc/math/fpu_asm.S new file +161 -0

161 lines not on this page · raw message

tools/testing/selftests/powerpc/math/fpu_syscall.c new file +90 -0

90 lines not on this page · raw message

tools/testing/selftests/powerpc/math/vmx_asm.S new file +193 -0

193 lines not on this page · raw message

tools/testing/selftests/powerpc/math/vmx_syscall.c new file +92 -0

92 lines not on this page · raw message

Signature · 1 line
2.7.0
author
#2 lore raw

PATCH v2 2/8 selftests/powerpc: Test preservation of FPU and VMX regs across preemption

In reply to Cyril Bur

Loop in assembly checking the registers with many threads.

---
 tools/testing/selftests/powerpc/math/.gitignore    |   2 +
 tools/testing/selftests/powerpc/math/Makefile      |   5 +-
 tools/testing/selftests/powerpc/math/fpu_asm.S     |  34 +++++++
 tools/testing/selftests/powerpc/math/fpu_preempt.c | 113 +++++++++++++++++++++
 tools/testing/selftests/powerpc/math/vmx_asm.S     |  44 +++++++-
 tools/testing/selftests/powerpc/math/vmx_preempt.c | 113 +++++++++++++++++++++
 6 files changed, 306 insertions(+), 5 deletions(-)
 create mode 100644 tools/testing/selftests/powerpc/math/fpu_preempt.c
 create mode 100644 tools/testing/selftests/powerpc/math/vmx_preempt.c
tools/testing/selftests/powerpc/math/.gitignore +2 -0
index b19b269..1a6f09e 100644
@@ -1,2 +1,4 @@
 fpu_syscall
 vmx_syscall
+fpu_preempt
+vmx_preempt
tools/testing/selftests/powerpc/math/Makefile +4 -1
index 418bef1..b6f4158 100644
@@ -1,4 +1,4 @@
-TEST_PROGS := fpu_syscall vmx_syscall
+TEST_PROGS := fpu_syscall fpu_preempt vmx_syscall vmx_preempt
 
 all: $(TEST_PROGS)
 
@@ -6,7 +6,10 @@ $(TEST_PROGS): ../harness.c
 $(TEST_PROGS): CFLAGS += -O2 -g -pthread -m64 -maltivec
 
 fpu_syscall: fpu_asm.S
+fpu_preempt: fpu_asm.S
+
 vmx_syscall: vmx_asm.S
+vmx_preempt: vmx_asm.S
 
 include ../../lib.mk
 
tools/testing/selftests/powerpc/math/fpu_asm.S +34 -0
index 8733874..46bbe99 100644
@@ -159,3 +159,37 @@ FUNC_START(test_fpu)
 	POP_BASIC_STACK(256)
 	blr
 FUNC_END(test_fpu)
+
+#int preempt_fpu(double *darray, int *threads_running, int *running)
+#On starting will (atomically) decrement not_ready as a signal that the FPU
+#has been loaded with darray. Will proceed to check the validity of the FPU
+#registers while running is not zero.
+FUNC_START(preempt_fpu)
+	PUSH_BASIC_STACK(256)
+	std r3,32(sp) #double *darray
+	std r4,40(sp) #volatile int *not_ready
+	std r5,48(sp) #int *running
+	PUSH_FPU(56)
+
+	bl load_fpu
+
+	#Atomic DEC
+	ld r3,40(sp)
+1:	lwarx r4,0,r3
+	addi r4,r4,-1
+	stwcx. r4,0,r3
+	bne- 1b
+
+2:	ld r3, 32(sp)
+	bl check_fpu
+	cmpdi r3,0
+	bne 3f
+	ld r4, 48(sp)
+	ld r5, 0(r4)
+	cmpwi r5,0
+	bne 2b
+
+3:	POP_FPU(56)
+	POP_BASIC_STACK(256)
+	blr
+FUNC_END(preempt_fpu)
tools/testing/selftests/powerpc/math/fpu_preempt.c new file +113 -0

113 lines not on this page · raw message

tools/testing/selftests/powerpc/math/vmx_asm.S +40 -4

80 lines not on this page · raw message

tools/testing/selftests/powerpc/math/vmx_preempt.c new file +113 -0

113 lines not on this page · raw message

Signature · 1 line
2.7.0
author
#3 lore raw

PATCH v2 3/8 selftests/powerpc: Test FPU and VMX regs in signal ucontext

In reply to Cyril Bur

Load up the non volatile FPU and VMX regs and ensure that they are the expected value in a signal handler

---
 tools/testing/selftests/powerpc/math/.gitignore   |   2 +
 tools/testing/selftests/powerpc/math/Makefile     |   4 +-
 tools/testing/selftests/powerpc/math/fpu_signal.c | 135 +++++++++++++++++++++
 tools/testing/selftests/powerpc/math/vmx_signal.c | 138 ++++++++++++++++++++++
 4 files changed, 278 insertions(+), 1 deletion(-)
 create mode 100644 tools/testing/selftests/powerpc/math/fpu_signal.c
 create mode 100644 tools/testing/selftests/powerpc/math/vmx_signal.c
tools/testing/selftests/powerpc/math/.gitignore +2 -0
index 1a6f09e..4fe13a4 100644
@@ -2,3 +2,5 @@ fpu_syscall
 vmx_syscall
 fpu_preempt
 vmx_preempt
+fpu_signal
+vmx_signal
tools/testing/selftests/powerpc/math/Makefile +3 -1
index b6f4158..5b88875 100644
@@ -1,4 +1,4 @@
-TEST_PROGS := fpu_syscall fpu_preempt vmx_syscall vmx_preempt
+TEST_PROGS := fpu_syscall fpu_preempt fpu_signal vmx_syscall vmx_preempt vmx_signal
 
 all: $(TEST_PROGS)
 
@@ -7,9 +7,11 @@ $(TEST_PROGS): CFLAGS += -O2 -g -pthread -m64 -maltivec
 
 fpu_syscall: fpu_asm.S
 fpu_preempt: fpu_asm.S
+fpu_signal:  fpu_asm.S
 
 vmx_syscall: vmx_asm.S
 vmx_preempt: vmx_asm.S
+vmx_signal: vmx_asm.S
 
 include ../../lib.mk
 
tools/testing/selftests/powerpc/math/fpu_signal.c new file +135 -0

135 lines not on this page · raw message

tools/testing/selftests/powerpc/math/vmx_signal.c new file +138 -0

138 lines not on this page · raw message

Signature · 1 line
2.7.0
author
#4 lore raw

PATCH v2 4/8 powerpc: Explicitly disable math features when copying thread

In reply to Cyril Bur

With threads leaving the math bits enabled in their saved MSR to indicate that the hardware is hot and a restore is not needed, children need to turn it off as when they do get scheduled, there's no way their registers could have been hot.

---
 arch/powerpc/kernel/process.c | 1 +
 1 file changed, 1 insertion(+)
arch/powerpc/kernel/process.c +1 -0
index dccc87e..e0c3d2d 100644
@@ -1307,6 +1307,7 @@ int copy_thread(unsigned long clone_flags, unsigned long usp,
 
 		f = ret_from_fork;
 	}
+	childregs->msr &= ~(MSR_FP|MSR_VEC|MSR_VSX);
 	sp -= STACK_FRAME_OVERHEAD;
 
 	/*
Signature · 1 line
2.7.0
#5 lore raw

Re: [PATCH V2 4/8] powerpc: Explicitly disable math features when copying thread

Quoted 2 lines · Cyril Bur

With threads leaving the math bits enabled in their saved MSR to indicate that the hardware is hot and a restore is not needed, children need to tu=

rn

Quoted 1 line

it off as when they do get scheduled, there's no way their registers coul=

d

Quoted 1 line

have been hot.

Is this a bug in the current code?

Mikey

Quoted 19 lines
---
 arch/powerpc/kernel/process.c | 1 +
 1 file changed, 1 insertion(+)

=20

arch/powerpc/kernel/process.c +0 -0
index dccc87e..e0c3d2d 100644
@@ -1307,6 +1307,7 @@ int copy_thread(unsigned long clone_flags,
 unsigned long usp,

=20

arch/powerpc/kernel/process.c +1 -0
 		f =3D ret_from_fork;
 	}
+	childregs->msr &=3D ~(MSR_FP|MSR_VEC|MSR_VSX);
 	sp -=3D STACK_FRAME_OVERHEAD;

=20
/*

author
#6 lore raw

Re: [PATCH V2 4/8] powerpc: Explicitly disable math features when copying thread

Quoted 8 lines · On Fri, 15 Jan 2016 16:42:22 +1100 Michael Neuling <[email protected]> wrote:
Quoted 4 lines · Cyril Bur

With threads leaving the math bits enabled in their saved MSR to indicate that the hardware is hot and a restore is not needed, children need to turn it off as when they do get scheduled, there's no way their registers could have been hot.

Is this a bug in the current code?

You're very consistent:

https://lists.ozlabs.org/pipermail/linuxppc-dev/2015-November/136469.html

;)

Quoted 21 lines

Mikey

Quoted 19 lines
---
 arch/powerpc/kernel/process.c | 1 +
 1 file changed, 1 insertion(+)
arch/powerpc/kernel/process.c +1 -0
index dccc87e..e0c3d2d 100644
@@ -1307,6 +1307,7 @@ int copy_thread(unsigned long clone_flags,
 unsigned long usp,
 
 		f = ret_from_fork;
 	}
+	childregs->msr &= ~(MSR_FP|MSR_VEC|MSR_VSX);
 	sp -= STACK_FRAME_OVERHEAD;
 

/*

#7 lore raw

Re: [PATCH V2 4/8] powerpc: Explicitly disable math features when copying thread

Quoted 11 lines · Cyril Bur

On Fri, 15 Jan 2016 16:42:22 +1100
Michael Neuling <[email protected]> wrote:
=20

Quoted 8 lines · Cyril Bur
Quoted 7 lines

With threads leaving the math bits enabled in their saved MSR to indicate
that the hardware is hot and a restore is not needed, children need to turn
it off as when they do get scheduled, there's no way their registers could
have been hot. =20

Quoted 8 lines
Quoted 3 lines

=20
Is this a bug in the current code?
=20

=20
You're very consistent:
=20
https://lists.ozlabs.org/pipermail/linuxppc-dev/2015-November/136469.
html

Fix the comment to explain why it's not a bug now.

Mikey

Quoted 28 lines

;)
=20

Quoted 13 lines

Mikey
=20

Quoted 11 lines
---
 arch/powerpc/kernel/process.c | 1 +
 1 file changed, 1 insertion(+)

=20

arch/powerpc/kernel/process.c +0 -0
index dccc87e..e0c3d2d 100644
@@ -1307,6 +1307,7 @@ int copy_thread(unsigned long clone_flags,
Quoted 8 lines
Quoted 8 lines
arch/powerpc/kernel/process.c +0 -0
 unsigned long usp,

=20

arch/powerpc/kernel/process.c +1 -0
 		f =3D ret_from_fork;
 	}
+	childregs->msr &=3D ~(MSR_FP|MSR_VEC|MSR_VSX);
 	sp -=3D STACK_FRAME_OVERHEAD;

=20
/* =20

=20
_______________________________________________
Linuxppc-dev mailing list
[email protected]
https://lists.ozlabs.org/listinfo/linuxppc-dev

author
#8 lore raw

PATCH v2 5/8 powerpc: Restore FPU/VEC/VSX if previously used

In reply to Cyril Bur

Currently the FPU, VEC and VSX facilities are lazily loaded. This is not a problem unless a process is using these facilities.

Modern versions of GCC are very good at automatically vectorising code, new and modernised workloads make use of floating point and vector facilities, even the kernel makes use of vectorised memcpy.

All this combined greatly increases the cost of a syscall since the kernel uses the facilities sometimes even in syscall fast-path making it increasingly common for a thread to take an *_unavailable exception soon after a syscall, not to mention potentially taking all three.

The obvious overcompensation to this problem is to simply always load all the facilities on every exit to userspace. Loading up all FPU, VEC and VSX registers every time can be expensive and if a workload does avoid using them, it should not be forced to incur this penalty.

An 8bit counter is used to detect if the registers have been used in the past and the registers are always loaded until the value wraps to back to zero.

Several versions of the assembly in entry_64.S. 1. Always calling C, 2. Performing a common case check and then calling C and 3. A complex check in asm. After some benchmarking it was determined that avoiding C in the common case is a performance benefit. The full check in asm greatly complicated that codepath for a negligible performance gain and the trade-off was deemed not worth it.

---
 arch/powerpc/include/asm/processor.h |  2 ++
 arch/powerpc/kernel/asm-offsets.c    |  2 ++
 arch/powerpc/kernel/entry_64.S       | 21 ++++++++++--
 arch/powerpc/kernel/fpu.S            |  4 +++
 arch/powerpc/kernel/process.c        | 66 ++++++++++++++++++++++++++++++------
 arch/powerpc/kernel/vector.S         |  4 +++
 6 files changed, 85 insertions(+), 14 deletions(-)
arch/powerpc/include/asm/processor.h +2 -0
index ac23308..dcab21f 100644
@@ -236,11 +236,13 @@ struct thread_struct {
 #endif
 	struct arch_hw_breakpoint hw_brk; /* info on the hardware breakpoint */
 	unsigned long	trap_nr;	/* last trap # on this thread */
+	u8 load_fp;
 #ifdef CONFIG_ALTIVEC
 	struct thread_vr_state vr_state;
 	struct thread_vr_state *vr_save_area;
 	unsigned long	vrsave;
 	int		used_vr;	/* set if process has used altivec */
+	u8 load_vec;
 #endif /* CONFIG_ALTIVEC */
 #ifdef CONFIG_VSX
 	/* VSR status */
arch/powerpc/kernel/asm-offsets.c +2 -0
index 07cebc3..10d5eab 100644
@@ -95,12 +95,14 @@ int main(void)
 	DEFINE(THREAD_FPSTATE, offsetof(struct thread_struct, fp_state));
 	DEFINE(THREAD_FPSAVEAREA, offsetof(struct thread_struct, fp_save_area));
 	DEFINE(FPSTATE_FPSCR, offsetof(struct thread_fp_state, fpscr));
+	DEFINE(THREAD_LOAD_FP, offsetof(struct thread_struct, load_fp));
 #ifdef CONFIG_ALTIVEC
 	DEFINE(THREAD_VRSTATE, offsetof(struct thread_struct, vr_state));
 	DEFINE(THREAD_VRSAVEAREA, offsetof(struct thread_struct, vr_save_area));
 	DEFINE(THREAD_VRSAVE, offsetof(struct thread_struct, vrsave));
 	DEFINE(THREAD_USED_VR, offsetof(struct thread_struct, used_vr));
 	DEFINE(VRSTATE_VSCR, offsetof(struct thread_vr_state, vscr));
+	DEFINE(THREAD_LOAD_VEC, offsetof(struct thread_struct, load_vec));
 #endif /* CONFIG_ALTIVEC */
 #ifdef CONFIG_VSX
 	DEFINE(THREAD_USED_VSR, offsetof(struct thread_struct, used_vsr));
arch/powerpc/kernel/entry_64.S +18 -3
index 0d525ce..038e0a1 100644
@@ -210,7 +210,20 @@ system_call:			/* label this so stack traces look sane */
 	li	r11,-MAX_ERRNO
 	andi.	r0,r9,(_TIF_SYSCALL_DOTRACE|_TIF_SINGLESTEP|_TIF_USER_WORK_MASK|_TIF_PERSYSCALL_MASK)
 	bne-	syscall_exit_work
-	cmpld	r3,r11
+
+	andi.	r0,r8,MSR_FP
+	beq 2f
+#ifdef CONFIG_ALTIVEC
+	andis.	r0,r8,MSR_VEC@h
+	bne	3f
+#endif
+2:	addi    r3,r1,STACK_FRAME_OVERHEAD
+	bl	restore_math
+	ld	r8,_MSR(r1)
+	ld	r3,RESULT(r1)
+	li	r11,-MAX_ERRNO
+
+3:	cmpld	r3,r11
 	ld	r5,_CCR(r1)
 	bge-	syscall_error
 .Lsyscall_error_cont:
@@ -602,8 +615,8 @@ _GLOBAL(ret_from_except_lite)
 
 	/* Check current_thread_info()->flags */
 	andi.	r0,r4,_TIF_USER_WORK_MASK
-#ifdef CONFIG_PPC_BOOK3E
 	bne	1f
+#ifdef CONFIG_PPC_BOOK3E
 	/*
 	 * Check to see if the dbcr0 register is set up to debug.
 	 * Use the internal debug mode bit to do this.
@@ -618,7 +631,9 @@ _GLOBAL(ret_from_except_lite)
 	mtspr	SPRN_DBSR,r10
 	b	restore
 #else
-	beq	restore
+	addi	r3,r1,STACK_FRAME_OVERHEAD
+	bl	restore_math
+	b	restore
 #endif
 1:	andi.	r0,r4,_TIF_NEED_RESCHED
 	beq	2f
arch/powerpc/kernel/fpu.S +4 -0
index 2117eac..b063524 100644
@@ -130,6 +130,10 @@ END_FTR_SECTION_IFSET(CPU_FTR_VSX)
 	or	r12,r12,r4
 	std	r12,_MSR(r1)
 #endif
+	/* Don't care if r4 overflows, this is desired behaviour */
+	lbz	r4,THREAD_LOAD_FP(r5)
+	addi	r4,r4,1
+	stb	r4,THREAD_LOAD_FP(r5)
 	addi	r10,r5,THREAD_FPSTATE
 	lfd	fr0,FPSTATE_FPSCR(r10)
 	MTFSF_L(fr0)
arch/powerpc/kernel/process.c +55 -11

84 lines not on this page · raw message

arch/powerpc/kernel/vector.S +4 -0
index 162d0f7..038cff8 100644
@@ -91,6 +91,10 @@ _GLOBAL(load_up_altivec)
 	oris	r12,r12,MSR_VEC@h
 	std	r12,_MSR(r1)
 #endif
+	/* Don't care if r4 overflows, this is desired behaviour */
+	lbz	r4,THREAD_LOAD_VEC(r5)
+	addi	r4,r4,1
+	stb	r4,THREAD_LOAD_VEC(r5)
 	addi	r6,r5,THREAD_VRSTATE
 	li	r4,1
 	li	r10,VRSTATE_VSCR
Signature · 1 line
2.7.0
#9 lore raw

Re: [PATCH V2 5/8] powerpc: Restore FPU/VEC/VSX if previously used

Quoted 1 line

Currently the FPU, VEC and VSX facilities are lazily loaded. This is not =

a

Quoted 3 lines

problem unless a process is using these facilities.

Modern versions of GCC are very good at automatically vectorising code, n=

ew

Quoted 1 line

and modernised workloads make use of floating point and vector facilities=

,

Quoted 3 lines

even the kernel makes use of vectorised memcpy.

All this combined greatly increases the cost of a syscall since the kerne=

l

Quoted 6 lines

uses the facilities sometimes even in syscall fast-path making it increasingly common for a thread to take an *_unavailable exception soon after a syscall, not to mention potentially taking all three.

The obvious overcompensation to this problem is to simply always load all the facilities on every exit to userspace. Loading up all FPU, VEC and VS=

X

Quoted 9 lines

registers every time can be expensive and if a workload does avoid using them, it should not be forced to incur this penalty.

An 8bit counter is used to detect if the registers have been used in the past and the registers are always loaded until the value wraps to back to zero.

Several versions of the assembly in entry_64.S. 1. Always calling C, 2. Performing a common case check and then calling C and 3. A complex check =

in

Quoted 5 lines

asm. After some benchmarking it was determined that avoiding C in the common case is a performance benefit. The full check in asm greatly complicated that codepath for a negligible performance gain and the trade-off was deemed not worth it.

Quoted 7 lines
---
 arch/powerpc/include/asm/processor.h |  2 ++
 arch/powerpc/kernel/asm-offsets.c    |  2 ++
 arch/powerpc/kernel/entry_64.S       | 21 ++++++++++--
 arch/powerpc/kernel/fpu.S            |  4 +++

arch/powerpc/kernel/process.c | 66 ++++++++++++++++++++++++++++++=

------

arch/powerpc/include/=

Quoted 4 lines
 arch/powerpc/kernel/vector.S         |  4 +++
 6 files changed, 85 insertions(+), 14 deletions(-)
arch/powerpc/include/= renamed from arch/powerpc/include/asm/processor.h +0 -0

asm/processor.h

arch/powerpc/include/asm/processor.h struct thread_struct { · line 237

Quoted 6 lines

index ac23308..dcab21f 100644

arch/powerpc/include/asm/processor.h +0 -0
@@ -236,11 +236,13 @@ struct thread_struct {
 #endif
        struct arch_hw_breakpoint hw_brk; /* info on the hardware breakpo=

int */

arch/powerpc/include/asm/processor.h struct thread_struct {

Quoted 7 lines
arch/powerpc/include/asm/processor.h +1 -0
        unsigned long   trap_nr;        /* last trap # on this thread */
+       u8 load_fp;
 #ifdef CONFIG_ALTIVEC
        struct thread_vr_state vr_state;
        struct thread_vr_state *vr_save_area;
        unsigned long   vrsave;
        int             used_vr;        /* set if process has used altive=

c */

arch/powerpc/kernel/asm-=

Quoted 5 lines
arch/powerpc/include/asm/processor.h +1 -0
+       u8 load_vec;
 #endif /* CONFIG_ALTIVEC */
 #ifdef CONFIG_VSX
        /* VSR status */
arch/powerpc/kernel/asm-= renamed from arch/powerpc/kernel/asm-offsets.c +0 -0

offsets.c

arch/powerpc/kernel/asm-offsets.c int main(void) · line 96

Quoted 6 lines

index 07cebc3..10d5eab 100644

arch/powerpc/kernel/asm-offsets.c +0 -0
@@ -95,12 +95,14 @@ int main(void)
        DEFINE(THREAD_FPSTATE, offsetof(struct thread_struct, fp_state));
        DEFINE(THREAD_FPSAVEAREA, offsetof(struct thread_struct, fp_save_=

area));

arch/powerpc/kernel/asm-offsets.c int main(void)

Quoted 5 lines
arch/powerpc/kernel/asm-offsets.c +1 -0
        DEFINE(FPSTATE_FPSCR, offsetof(struct thread_fp_state, fpscr));
+       DEFINE(THREAD_LOAD_FP, offsetof(struct thread_struct, load_fp));
 #ifdef CONFIG_ALTIVEC
        DEFINE(THREAD_VRSTATE, offsetof(struct thread_struct, vr_state));
        DEFINE(THREAD_VRSAVEAREA, offsetof(struct thread_struct, vr_save_=

area));

arch/powerpc/kernel/asm-offsets.c int main(void)

Quoted 4 lines
arch/powerpc/kernel/asm-offsets.c +1 -0
        DEFINE(THREAD_VRSAVE, offsetof(struct thread_struct, vrsave));
        DEFINE(THREAD_USED_VR, offsetof(struct thread_struct, used_vr));
        DEFINE(VRSTATE_VSCR, offsetof(struct thread_vr_state, vscr));
+       DEFINE(THREAD_LOAD_VEC, offsetof(struct thread_struct, load_vec))=

;

arch/powerpc/kernel/asm-offsets.c int main(void) · line 108

Quoted 3 lines
arch/powerpc/kernel/asm-offsets.c +0 -0
 #endif /* CONFIG_ALTIVEC */
 #ifdef CONFIG_VSX
        DEFINE(THREAD_USED_VSR, offsetof(struct thread_struct, used_vsr))=

;

arch/powerpc/kernel/entry_6=

Quoted 1 line
arch/powerpc/kernel/entry_6= renamed from arch/powerpc/kernel/entry_64.S +0 -0

4.S

arch/powerpc/kernel/entry_64.S system_call: /* label this so =

Quoted 4 lines

index 0d525ce..038e0a1 100644

arch/powerpc/kernel/entry_64.S +0 -0
@@ -210,7 +210,20 @@ system_call:                       /* label this so =

stack traces look sane */

arch/powerpc/kernel/entry_64.S system_call: /* label this so = · line 211

Quoted 2 lines
arch/powerpc/kernel/entry_64.S +0 -0
        li      r11,-MAX_ERRNO
        andi.   r0,r9,(_TIF_SYSCALL_DOTRACE|_TIF_SINGLESTEP|_TIF_USER_WOR=

K_MASK|_TIF_PERSYSCALL_MASK)

arch/powerpc/kernel/entry_64.S _GLOBAL(ret_from_except_lite)

Quoted 20 lines
arch/powerpc/kernel/entry_64.S +14 -1
        bne-    syscall_exit_work
-       cmpld   r3,r11
+
+       andi.   r0,r8,MSR_FP
+       beq 2f
+#ifdef CONFIG_ALTIVEC
+       andis.  r0,r8,MSR_VEC@h
+       bne     3f
+#endif
+2:     addi    r3,r1,STACK_FRAME_OVERHEAD
+       bl      restore_math
+       ld      r8,_MSR(r1)
+       ld      r3,RESULT(r1)
+       li      r11,-MAX_ERRNO
+
+3:     cmpld   r3,r11
        ld      r5,_CCR(r1)
        bge-    syscall_error
 .Lsyscall_error_cont:
@@ -602,8 +615,8 @@ _GLOBAL(ret_from_except_lite)

=20

arch/powerpc/kernel/process.=

Quoted 35 lines
arch/powerpc/kernel/entry_64.S +4 -2
        /* Check current_thread_info()->flags */
        andi.   r0,r4,_TIF_USER_WORK_MASK
-#ifdef CONFIG_PPC_BOOK3E
        bne     1f
+#ifdef CONFIG_PPC_BOOK3E
        /*
         * Check to see if the dbcr0 register is set up to debug.
         * Use the internal debug mode bit to do this.
@@ -618,7 +631,9 @@ _GLOBAL(ret_from_except_lite)
        mtspr   SPRN_DBSR,r10
        b       restore
 #else
-       beq     restore
+       addi    r3,r1,STACK_FRAME_OVERHEAD
+       bl      restore_math
+       b       restore
 #endif
 1:     andi.   r0,r4,_TIF_NEED_RESCHED
        beq     2f
arch/powerpc/kernel/fpu.S +4 -0
index 2117eac..b063524 100644
@@ -130,6 +130,10 @@ END_FTR_SECTION_IFSET(CPU_FTR_VSX)
        or      r12,r12,r4
        std     r12,_MSR(r1)
 #endif
+       /* Don't care if r4 overflows, this is desired behaviour */
+       lbz     r4,THREAD_LOAD_FP(r5)
+       addi    r4,r4,1
+       stb     r4,THREAD_LOAD_FP(r5)
        addi    r10,r5,THREAD_FPSTATE
        lfd     fr0,FPSTATE_FPSCR(r10)
        MTFSF_L(fr0)
arch/powerpc/kernel/process.= renamed from arch/powerpc/kernel/process.c +0 -0

c

arch/powerpc/kernel/process.c void giveup_all(struct task_struct *tsk) · line 375

Quoted 6 lines

index e0c3d2d..ec53468 100644

arch/powerpc/kernel/process.c +0 -0
@@ -374,6 +374,53 @@ void giveup_all(struct task_struct *tsk)
 }
 EXPORT_SYMBOL(giveup_all);

=20

arch/powerpc/kernel/process.c void giveup_all(struct task_struct *tsk) · line 387

Quoted 11 lines
arch/powerpc/kernel/process.c +11 -0
+void restore_math(struct pt_regs *regs)
+{
+       unsigned long msr;
+
+       if (!current->thread.load_fp
+#ifdef CONFIG_ALTIVEC
+               && !current->thread.load_vec)
+#else
+               )
+#endif
+               return;

Can you make the inline code easier to read? Something like

#ifdef CONFIG_ALTIVEC
#define loadvec(thr) ((thr).load_vec)
#else
#define loadvec(thr) 0
#endif

void restore_math(struct pt_regs *regs)

{
       unsigned long msr;

       if (!current->thread.load_fp && !loadvec(current->thread)
		return;

arch/powerpc/kernel/process.c void giveup_all(struct task_struct *tsk)

Quoted 6 lines
arch/powerpc/kernel/process.c +6 -0
+
+       msr =3D regs->msr;
+       msr_check_and_set(msr_all_available);
+
+       /*
+        * Only reload if the bit is not set in the user MSR, the bit BEI=

NG set

Quoted 10 lines
arch/powerpc/kernel/process.c +10 -0
+        * indicates that the registers are hot
+        */
+#ifdef CONFIG_PPC_FPU
+       if (current->thread.load_fp && !(msr & MSR_FP)) {
+               load_fp_state(&current->thread.fp_state);
+               msr |=3D MSR_FP | current->thread.fpexc_mode;
+               current->thread.load_fp++;
+       }
+#endif
+#ifdef CONFIG_ALTIVEC

arch/powerpc/kernel/process.c void giveup_all(struct task_struct *tsk)

Quoted 10 lines
arch/powerpc/kernel/process.c +10 -0
+       if (current->thread.load_vec && !(msr & MSR_VEC) &&
+                       cpu_has_feature(CPU_FTR_ALTIVEC)) {
+               load_vr_state(&current->thread.vr_state);
+               current->thread.used_vr =3D 1;
+               msr |=3D MSR_VEC;
+               current->thread.load_vec++;
+       }
+#endif
+#ifdef CONFIG_VSX
+       if (!(msr & MSR_VSX) && (msr & (MSR_FP | MSR_VEC)) =3D=3D (MSR_FP=

| MSR_VEC)) {

What are you trying to hit with this if statement?

Seems you are turning on VSX if VSX is not already on but FP and VEC is. Why do you need the check MSR_VSX is not used? That seems redundant.

arch/powerpc/kernel/process.c void giveup_all(struct task_struct *tsk) · line 419

Quoted 6 lines
arch/powerpc/kernel/process.c +6 -0
+               current->thread.used_vsr =3D 1;
+               msr |=3D MSR_VSX;
+       }
+#endif
+
+       msr_check_and_clear(msr_all_available);

Why are you doing this? Why all, and not just the ones you've enabled abov= e?

arch/powerpc/kernel/process.c void restore_tm_state(struct pt_regs *regs)

Quoted 8 lines
arch/powerpc/kernel/process.c +4 -0
+
+       regs->msr =3D msr;
+}
+
 void flush_all_to_thread(struct task_struct *tsk)
 {
        if (tsk->thread.regs) {
@@ -832,17 +879,9 @@ void restore_tm_state(struct pt_regs *regs)

=20

Quoted 8 lines
arch/powerpc/kernel/process.c +0 -6
        msr_diff =3D current->thread.ckpt_regs.msr & ~regs->msr;
        msr_diff &=3D MSR_FP | MSR_VEC | MSR_VSX;
-       if (msr_diff & MSR_FP) {
-               msr_check_and_set(MSR_FP);
-               load_fp_state(&current->thread.fp_state);
-               msr_check_and_clear(MSR_FP);
-               regs->msr |=3D current->thread.fpexc_mode;
-       }

arch/powerpc/kernel/process.c void restore_tm_state(struct pt_regs *regs) · line 886

Quoted 10 lines
arch/powerpc/kernel/process.c +3 -5
-       if (msr_diff & MSR_VEC) {
-               msr_check_and_set(MSR_VEC);
-               load_vr_state(&current->thread.vr_state);
-               msr_check_and_clear(MSR_VEC);
-       }
+
+       restore_math(regs);
+
        regs->msr |=3D msr_diff;
 }

=20

arch/powerpc/kernel/process.c struct task_struct *__switch_to(struct task_struct=

Quoted 1 line
arch/powerpc/kernel/process.c +0 -0
@@ -1006,6 +1045,11 @@ struct task_struct *__switch_to(struct task_struct=

*prev,

arch/powerpc/kernel/process.c struct task_struct *__switch_to(struct task_struct *prev, · line 1049

Quoted 5 lines
arch/powerpc/kernel/process.c +2 -0
                batch =3D this_cpu_ptr(&ppc64_tlb_batch);
                batch->active =3D 1;
        }
+
+       /* Don't do this on a kernel thread */

Why not?

arch/powerpc/kernel/process.c struct task_struct *__switch_to(struct task_struct *prev, · line 1053

Quoted 4 lines
arch/powerpc/kernel/process.c +3 -0
+       if (current_thread_info()->task->thread.regs)
+               restore_math(current_thread_info()->task->thread.regs);
+
 #endif /* CONFIG_PPC_BOOK3S_64 */

=20

Quoted 19 lines
arch/powerpc/kernel/process.c +0 -0
        return last;
arch/powerpc/kernel/vector.S +4 -1
index 162d0f7..038cff8 100644
@@ -91,6 +91,10 @@ _GLOBAL(load_up_altivec)
        oris    r12,r12,MSR_VEC@h
        std     r12,_MSR(r1)
 #endif
+       /* Don't care if r4 overflows, this is desired behaviour */
+       lbz     r4,THREAD_LOAD_VEC(r5)
+       addi    r4,r4,1
+       stb     r4,THREAD_LOAD_VEC(r5)
        addi    r6,r5,THREAD_VRSTATE
        li      r4,1
        li      r10,VRSTATE_VSCR
--=20

2.7.0

Quoted 4 lines

_______________________________________________
Linuxppc-dev mailing list
[email protected]
https://lists.ozlabs.org/listinfo/linuxppc-devOn Fri, 2016-01-15 at 16:04=

+1100, Cyril Bur wrote:

author
#10 lore raw

Re: [PATCH V2 5/8] powerpc: Restore FPU/VEC/VSX if previously used

On Fri, 15 Jan 2016 17:02:41 +1100
Michael Neuling <[email protected]> wrote:

Hey Mikey,

Thanks for the review, as always you're correct :).

Quoted 48 lines

Can you make the inline code easier to read? Something like

#ifdef CONFIG_ALTIVEC
#define loadvec(thr) ((thr).load_vec)
#else
#define loadvec(thr) 0
#endif

void restore_math(struct pt_regs *regs)

{
       unsigned long msr;

       if (!current->thread.load_fp && !loadvec(current->thread)
		return;
Quoted 16 lines
arch/powerpc/kernel/process.c +16 -0
+
+       msr = regs->msr;
+       msr_check_and_set(msr_all_available);
+
+       /*
+        * Only reload if the bit is not set in the user MSR, the bit BEING set
+        * indicates that the registers are hot
+        */
+#ifdef CONFIG_PPC_FPU
+       if (current->thread.load_fp && !(msr & MSR_FP)) {
+               load_fp_state(&current->thread.fp_state);
+               msr |= MSR_FP | current->thread.fpexc_mode;
+               current->thread.load_fp++;
+       }
+#endif
+#ifdef CONFIG_ALTIVEC

arch/powerpc/kernel/process.c void giveup_all(struct task_struct *tsk) · line 413

Quoted 10 lines
arch/powerpc/kernel/process.c +10 -0
+       if (current->thread.load_vec && !(msr & MSR_VEC) &&
+                       cpu_has_feature(CPU_FTR_ALTIVEC)) {
+               load_vr_state(&current->thread.vr_state);
+               current->thread.used_vr = 1;
+               msr |= MSR_VEC;
+               current->thread.load_vec++;
+       }
+#endif
+#ifdef CONFIG_VSX
+       if (!(msr & MSR_VSX) && (msr & (MSR_FP | MSR_VEC)) == (MSR_FP | MSR_VEC)) {  

What are you trying to hit with this if statement?

Seems you are turning on VSX if VSX is not already on but FP and VEC is. Why do you need the check MSR_VSX is not used? That seems redundant.

Quoted 9 lines

arch/powerpc/kernel/process.c void giveup_all(struct task_struct *tsk) · line 419

Quoted 6 lines
arch/powerpc/kernel/process.c +6 -0
+               current->thread.used_vsr = 1;
+               msr |= MSR_VSX;
+       }
+#endif
+
+       msr_check_and_clear(msr_all_available);  

Why are you doing this? Why all, and not just the ones you've enabled above?

This is part of the batching of MSR reads and writes. We turned everything on at the start of restore_math() because it means only one write, the MSR reads/writes are where the performance hit, not the number of bits changed. Obviously we subsequently we turn everything off again because it also means only one write (and we had unconditionally turned everything on).

The check at the start of restore_math() and in entry_64.S should mean that we don't the msr_check_and_set()/msr_check_and_clear() block with nothing to do.

Quoted 65 lines
Quoted 29 lines
arch/powerpc/kernel/process.c +7 -11
+
+       regs->msr = msr;
+}
+
 void flush_all_to_thread(struct task_struct *tsk)
 {
        if (tsk->thread.regs) {
@@ -832,17 +879,9 @@ void restore_tm_state(struct pt_regs *regs)  
 
        msr_diff = current->thread.ckpt_regs.msr & ~regs->msr;
        msr_diff &= MSR_FP | MSR_VEC | MSR_VSX;
-       if (msr_diff & MSR_FP) {
-               msr_check_and_set(MSR_FP);
-               load_fp_state(&current->thread.fp_state);
-               msr_check_and_clear(MSR_FP);
-               regs->msr |= current->thread.fpexc_mode;
-       }
-       if (msr_diff & MSR_VEC) {
-               msr_check_and_set(MSR_VEC);
-               load_vr_state(&current->thread.vr_state);
-               msr_check_and_clear(MSR_VEC);
-       }
+
+       restore_math(regs);
+
        regs->msr |= msr_diff;
 }  
 

arch/powerpc/kernel/process.c struct task_struct *__switch_to(struct task_struct *prev, · line 1049

Quoted 5 lines
arch/powerpc/kernel/process.c +2 -0
@@ -1006,6 +1045,11 @@ struct task_struct *__switch_to(struct task_struct *prev,
                batch = this_cpu_ptr(&ppc64_tlb_batch);
                batch->active = 1;
        }
+
+       /* Don't do this on a kernel thread */  

Why not?

Quoted 28 lines
arch/powerpc/kernel/process.c +3 -0
+       if (current_thread_info()->task->thread.regs)
+               restore_math(current_thread_info()->task->thread.regs);
+
 #endif /* CONFIG_PPC_BOOK3S_64 */  
 
        return last;
arch/powerpc/kernel/vector.S +4 -0
index 162d0f7..038cff8 100644
@@ -91,6 +91,10 @@ _GLOBAL(load_up_altivec)
        oris    r12,r12,MSR_VEC@h
        std     r12,_MSR(r1)
 #endif
+       /* Don't care if r4 overflows, this is desired behaviour */
+       lbz     r4,THREAD_LOAD_VEC(r5)
+       addi    r4,r4,1
+       stb     r4,THREAD_LOAD_VEC(r5)
        addi    r6,r5,THREAD_VRSTATE
        li      r4,1
        li      r10,VRSTATE_VSCR
Signature · 5 lines
2.7.0  

_______________________________________________
Linuxppc-dev mailing list
[email protected]

https://lists.ozlabs.org/listinfo/linuxppc-devOn Fri, 2016-01-15 at 16:04 +1100, Cyril Bur wrote:

author
#11 lore raw

PATCH v2 6/8 powerpc: Add the ability to save FPU without giving it up

In reply to Cyril Bur

This patch adds the ability to be able to save the FPU registers to the thread struct without giving up (disabling the facility) next time the process returns to userspace.

This patch optimises the thread copy path (as a result of a fork() or clone()) so that the parent thread can return to userspace with hot registers avoiding a possibly pointless reload of FPU register state.

---
 arch/powerpc/include/asm/switch_to.h |  2 +-
 arch/powerpc/kernel/fpu.S            | 21 ++++------------
 arch/powerpc/kernel/process.c        | 46 +++++++++++++++++++++++++++++++++++-
 3 files changed, 50 insertions(+), 19 deletions(-)
arch/powerpc/include/asm/switch_to.h +1 -1
index 5b268b6..c4d50e9 100644
@@ -28,7 +28,7 @@ extern void giveup_all(struct task_struct *);
 extern void enable_kernel_fp(void);
 extern void flush_fp_to_thread(struct task_struct *);
 extern void giveup_fpu(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);
arch/powerpc/kernel/fpu.S +4 -17
index b063524..15da2b5 100644
@@ -143,33 +143,20 @@ END_FTR_SECTION_IFSET(CPU_FTR_VSX)
 	blr
 
 /*
- * __giveup_fpu(tsk)
- * Disable FP for the task given as the argument,
- * and save the floating-point registers in its thread_struct.
+ * save_fpu(tsk)
+ * Save the floating-point registers in its thread_struct.
  * Enables the FPU for use in the kernel on return.
  */
-_GLOBAL(__giveup_fpu)
+_GLOBAL(save_fpu)
 	addi	r3,r3,THREAD	        /* want THREAD of task */
 	PPC_LL	r6,THREAD_FPSAVEAREA(r3)
 	PPC_LL	r5,PT_REGS(r3)
 	PPC_LCMPI	0,r6,0
 	bne	2f
 	addi	r6,r3,THREAD_FPSTATE
-2:	PPC_LCMPI	0,r5,0
-	SAVE_32FPVSRS(0, R4, R6)
+2:	SAVE_32FPVSRS(0, R4, R6)
 	mffs	fr0
 	stfd	fr0,FPSTATE_FPSCR(r6)
-	beq	1f
-	PPC_LL	r4,_MSR-STACK_FRAME_OVERHEAD(r5)
-	li	r3,MSR_FP|MSR_FE0|MSR_FE1
-#ifdef CONFIG_VSX
-BEGIN_FTR_SECTION
-	oris	r3,r3,MSR_VSX@h
-END_FTR_SECTION_IFSET(CPU_FTR_VSX)
-#endif
-	andc	r4,r4,r3		/* disable FP for previous task */
-	PPC_STL	r4,_MSR-STACK_FRAME_OVERHEAD(r5)
-1:
 	blr
 
 /*
arch/powerpc/kernel/process.c +45 -1

63 lines not on this page · raw message

Signature · 1 line
2.7.0
#12 lore raw

Re: [PATCH V2 6/8] powerpc: Add the ability to save FPU without giving it up

Quoted 9 lines · Cyril Bur

This patch adds the ability to be able to save the FPU registers to the
thread struct without giving up (disabling the facility) next time the
process returns to userspace.
=20
This patch optimises the thread copy path (as a result of a fork() or clone()) so that the parent thread can return to userspace with hot registers avoiding a possibly pointless reload of FPU register state.

This comment seems misleading. The patch seems to touch the VSX, VMX and SPE state as well. =20

Mikey

Quoted 137 lines

=20

---
 arch/powerpc/include/asm/switch_to.h |  2 +-
 arch/powerpc/kernel/fpu.S            | 21 ++++------------
 arch/powerpc/kernel/process.c        | 46
+++++++++++++++++++++++++++++++++++-
 3 files changed, 50 insertions(+), 19 deletions(-)

=20

arch/powerpc/include/asm/switch_to.h +1 -1
index 5b268b6..c4d50e9 100644
@@ -28,7 +28,7 @@ extern void giveup_all(struct task_struct *);
 extern void enable_kernel_fp(void);
 extern void flush_fp_to_thread(struct task_struct *);
 extern void giveup_fpu(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);
arch/powerpc/kernel/fpu.S +0 -0
index b063524..15da2b5 100644
@@ -143,33 +143,20 @@ END_FTR_SECTION_IFSET(CPU_FTR_VSX)
 	blr

=20
/*
- * __giveup_fpu(tsk)
- * Disable FP for the task given as the argument,
- * and save the floating-point registers in its thread_struct.
+ * save_fpu(tsk)
+ * Save the floating-point registers in its thread_struct.
* Enables the FPU for use in the kernel on return. */

arch/powerpc/kernel/fpu.S +1 -1
-_GLOBAL(__giveup_fpu)
+_GLOBAL(save_fpu)
 	addi	r3,r3,THREAD	        /* want THREAD of

task */

arch/powerpc/kernel/fpu.S +1 -11
 	PPC_LL	r6,THREAD_FPSAVEAREA(r3)
 	PPC_LL	r5,PT_REGS(r3)
 	PPC_LCMPI	0,r6,0
 	bne	2f
 	addi	r6,r3,THREAD_FPSTATE
-2:	PPC_LCMPI	0,r5,0
-	SAVE_32FPVSRS(0, R4, R6)
+2:	SAVE_32FPVSRS(0, R4, R6)
 	mffs	fr0
 	stfd	fr0,FPSTATE_FPSCR(r6)
-	beq	1f
-	PPC_LL	r4,_MSR-STACK_FRAME_OVERHEAD(r5)
-	li	r3,MSR_FP|MSR_FE0|MSR_FE1
-#ifdef CONFIG_VSX
-BEGIN_FTR_SECTION
-	oris	r3,r3,MSR_VSX@h
-END_FTR_SECTION_IFSET(CPU_FTR_VSX)
-#endif
-	andc	r4,r4,r3		/* disable FP for

previous task */

arch/powerpc/kernel/fpu.S +0 -2
-	PPC_STL	r4,_MSR-STACK_FRAME_OVERHEAD(r5)
-1:
 	blr

=20
/*

arch/powerpc/kernel/process.c +0 -0
index ec53468..8a96e4f 100644
@@ -133,6 +133,16 @@ void __msr_check_and_clear(unsigned long bits)
 EXPORT_SYMBOL(__msr_check_and_clear);

=20
#ifdef CONFIG_PPC_FPU

arch/powerpc/kernel/process.c +10 -0
+void __giveup_fpu(struct task_struct *tsk)
+{
+	save_fpu(tsk);
+	tsk->thread.regs->msr &=3D ~MSR_FP;
+#ifdef CONFIG_VSX
+	if (cpu_has_feature(CPU_FTR_VSX))
+		tsk->thread.regs->msr &=3D ~MSR_VSX;
+#endif
+}
+
 void giveup_fpu(struct task_struct *tsk)
 {
 	check_if_tm_restore_required(tsk);
@@ -421,12 +431,46 @@ void restore_math(struct pt_regs *regs)
 	regs->msr =3D msr;
 }

=20

arch/powerpc/kernel/process.c +35 -1
+void save_all(struct task_struct *tsk)
+{
+	unsigned long usermsr;
+
+	if (!tsk->thread.regs)
+		return;
+
+	usermsr =3D tsk->thread.regs->msr;
+
+	if ((usermsr & msr_all_available) =3D=3D 0)
+		return;
+
+	msr_check_and_set(msr_all_available);
+
+#ifdef CONFIG_PPC_FPU
+	if (usermsr & MSR_FP)
+		save_fpu(tsk);
+#endif
+#ifdef CONFIG_ALTIVEC
+	if (usermsr & MSR_VEC)
+		__giveup_altivec(tsk);
+#endif
+#ifdef CONFIG_VSX
+	if (usermsr & MSR_VSX)
+		__giveup_vsx(tsk);
+#endif
+#ifdef CONFIG_SPE
+	if (usermsr & MSR_SPE)
+		__giveup_spe(tsk);
+#endif
+
+	msr_check_and_clear(msr_all_available);
+}
+
 void flush_all_to_thread(struct task_struct *tsk)
 {
 	if (tsk->thread.regs) {
 		preempt_disable();
 		BUG_ON(tsk !=3D current);
-		giveup_all(tsk);
+		save_all(tsk);

=20
#ifdef CONFIG_SPE
if (tsk->thread.regs->msr & MSR_SPE)

#13 lore raw

Re: [PATCH V2 6/8] powerpc: Add the ability to save FPU without giving it up

In reply to Cyril Bur
Quoted 7 lines · Cyril Bur

This patch adds the ability to be able to save the FPU registers to the thread struct without giving up (disabling the facility) next time the process returns to userspace.

This patch optimises the thread copy path (as a result of a fork() or clone()) so that the parent thread can return to userspace with hot registers avoiding a possibly pointless reload of FPU register state.

Ok, but if the patch optimizes the copy path then show the performance numbers.

Thanks!

Quoted 141 lines
---
 arch/powerpc/include/asm/switch_to.h |  2 +-
 arch/powerpc/kernel/fpu.S            | 21 ++++------------
 arch/powerpc/kernel/process.c        | 46
+++++++++++++++++++++++++++++++++++-
 3 files changed, 50 insertions(+), 19 deletions(-)
arch/powerpc/include/asm/switch_to.h +1 -1
index 5b268b6..c4d50e9 100644
@@ -28,7 +28,7 @@ extern void giveup_all(struct task_struct *);
 extern void enable_kernel_fp(void);
 extern void flush_fp_to_thread(struct task_struct *);
 extern void giveup_fpu(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);
arch/powerpc/kernel/fpu.S +4 -17
index b063524..15da2b5 100644
@@ -143,33 +143,20 @@ END_FTR_SECTION_IFSET(CPU_FTR_VSX)
 	blr
 
 /*
- * __giveup_fpu(tsk)
- * Disable FP for the task given as the argument,
- * and save the floating-point registers in its thread_struct.
+ * save_fpu(tsk)
+ * Save the floating-point registers in its thread_struct.
  * Enables the FPU for use in the kernel on return.
  */
-_GLOBAL(__giveup_fpu)
+_GLOBAL(save_fpu)
 	addi	r3,r3,THREAD	        /* want THREAD of task */
 	PPC_LL	r6,THREAD_FPSAVEAREA(r3)
 	PPC_LL	r5,PT_REGS(r3)
 	PPC_LCMPI	0,r6,0
 	bne	2f
 	addi	r6,r3,THREAD_FPSTATE
-2:	PPC_LCMPI	0,r5,0
-	SAVE_32FPVSRS(0, R4, R6)
+2:	SAVE_32FPVSRS(0, R4, R6)
 	mffs	fr0
 	stfd	fr0,FPSTATE_FPSCR(r6)
-	beq	1f
-	PPC_LL	r4,_MSR-STACK_FRAME_OVERHEAD(r5)
-	li	r3,MSR_FP|MSR_FE0|MSR_FE1
-#ifdef CONFIG_VSX
-BEGIN_FTR_SECTION
-	oris	r3,r3,MSR_VSX@h
-END_FTR_SECTION_IFSET(CPU_FTR_VSX)
-#endif
-	andc	r4,r4,r3		/* disable FP for previous task */
-	PPC_STL	r4,_MSR-STACK_FRAME_OVERHEAD(r5)
-1:
 	blr
 
 /*
arch/powerpc/kernel/process.c +45 -2
index ec53468..8a96e4f 100644
@@ -133,6 +133,16 @@ void __msr_check_and_clear(unsigned long bits)
 EXPORT_SYMBOL(__msr_check_and_clear);
 
 #ifdef CONFIG_PPC_FPU
+void __giveup_fpu(struct task_struct *tsk)
+{
+	save_fpu(tsk);
+	tsk->thread.regs->msr &= ~MSR_FP;
+#ifdef CONFIG_VSX
+	if (cpu_has_feature(CPU_FTR_VSX))
+		tsk->thread.regs->msr &= ~MSR_VSX;
+#endif
+}
+
 void giveup_fpu(struct task_struct *tsk)
 {
 	check_if_tm_restore_required(tsk);
@@ -421,12 +431,46 @@ void restore_math(struct pt_regs *regs)
 	regs->msr = msr;
 }
 
+void save_all(struct task_struct *tsk)
+{
+	unsigned long usermsr;
+
+	if (!tsk->thread.regs)
+		return;
+
+	usermsr = tsk->thread.regs->msr;
+
+	if ((usermsr & msr_all_available) == 0)
+		return;
+
+	msr_check_and_set(msr_all_available);
+
+#ifdef CONFIG_PPC_FPU
+	if (usermsr & MSR_FP)
+		save_fpu(tsk);
+#endif
+#ifdef CONFIG_ALTIVEC
+	if (usermsr & MSR_VEC)
+		__giveup_altivec(tsk);
+#endif
+#ifdef CONFIG_VSX
+	if (usermsr & MSR_VSX)
+		__giveup_vsx(tsk);
+#endif
+#ifdef CONFIG_SPE
+	if (usermsr & MSR_SPE)
+		__giveup_spe(tsk);
+#endif
+
+	msr_check_and_clear(msr_all_available);
+}
+
 void flush_all_to_thread(struct task_struct *tsk)
 {
 	if (tsk->thread.regs) {
 		preempt_disable();
 		BUG_ON(tsk != current);
-		giveup_all(tsk);
+		save_all(tsk);
 
 #ifdef CONFIG_SPE
 		if (tsk->thread.regs->msr & MSR_SPE)
--

2.7.0

_______________________________________________
Linuxppc-dev mailing list
[email protected]
https://lists.ozlabs.org/listinfo/linuxppc-dev

#14 lore raw

Re: [PATCH V2 6/8] powerpc: Add the ability to save FPU without giving it up

Quoted 5 lines · Denis Kirjanov
Quoted 4 lines · Cyril Bur

This patch adds the ability to be able to save the FPU registers to the thread struct without giving up (disabling the facility) next time the process returns to userspace.

Quoted 8 lines
Quoted 3 lines

This patch optimises the thread copy path (as a result of a fork() or clone()) so that the parent thread can return to userspace with hot registers avoiding a possibly pointless reload of FPU register state.

Ok, but if the patch optimizes the copy path then show the performance numbers.

Thanks!

Oh, I've missed your cover letter.

Quoted 143 lines
Quoted 142 lines
---
 arch/powerpc/include/asm/switch_to.h |  2 +-
 arch/powerpc/kernel/fpu.S            | 21 ++++------------
 arch/powerpc/kernel/process.c        | 46
+++++++++++++++++++++++++++++++++++-
 3 files changed, 50 insertions(+), 19 deletions(-)
arch/powerpc/include/asm/switch_to.h +1 -1
index 5b268b6..c4d50e9 100644
@@ -28,7 +28,7 @@ extern void giveup_all(struct task_struct *);
 extern void enable_kernel_fp(void);
 extern void flush_fp_to_thread(struct task_struct *);
 extern void giveup_fpu(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);
arch/powerpc/kernel/fpu.S +4 -17
index b063524..15da2b5 100644
@@ -143,33 +143,20 @@ END_FTR_SECTION_IFSET(CPU_FTR_VSX)
 	blr
 
 /*
- * __giveup_fpu(tsk)
- * Disable FP for the task given as the argument,
- * and save the floating-point registers in its thread_struct.
+ * save_fpu(tsk)
+ * Save the floating-point registers in its thread_struct.
  * Enables the FPU for use in the kernel on return.
  */
-_GLOBAL(__giveup_fpu)
+_GLOBAL(save_fpu)
 	addi	r3,r3,THREAD	        /* want THREAD of task */
 	PPC_LL	r6,THREAD_FPSAVEAREA(r3)
 	PPC_LL	r5,PT_REGS(r3)
 	PPC_LCMPI	0,r6,0
 	bne	2f
 	addi	r6,r3,THREAD_FPSTATE
-2:	PPC_LCMPI	0,r5,0
-	SAVE_32FPVSRS(0, R4, R6)
+2:	SAVE_32FPVSRS(0, R4, R6)
 	mffs	fr0
 	stfd	fr0,FPSTATE_FPSCR(r6)
-	beq	1f
-	PPC_LL	r4,_MSR-STACK_FRAME_OVERHEAD(r5)
-	li	r3,MSR_FP|MSR_FE0|MSR_FE1
-#ifdef CONFIG_VSX
-BEGIN_FTR_SECTION
-	oris	r3,r3,MSR_VSX@h
-END_FTR_SECTION_IFSET(CPU_FTR_VSX)
-#endif
-	andc	r4,r4,r3		/* disable FP for previous task */
-	PPC_STL	r4,_MSR-STACK_FRAME_OVERHEAD(r5)
-1:
 	blr
 
 /*
arch/powerpc/kernel/process.c +45 -2
index ec53468..8a96e4f 100644
@@ -133,6 +133,16 @@ void __msr_check_and_clear(unsigned long bits)
 EXPORT_SYMBOL(__msr_check_and_clear);
 
 #ifdef CONFIG_PPC_FPU
+void __giveup_fpu(struct task_struct *tsk)
+{
+	save_fpu(tsk);
+	tsk->thread.regs->msr &= ~MSR_FP;
+#ifdef CONFIG_VSX
+	if (cpu_has_feature(CPU_FTR_VSX))
+		tsk->thread.regs->msr &= ~MSR_VSX;
+#endif
+}
+
 void giveup_fpu(struct task_struct *tsk)
 {
 	check_if_tm_restore_required(tsk);
@@ -421,12 +431,46 @@ void restore_math(struct pt_regs *regs)
 	regs->msr = msr;
 }
 
+void save_all(struct task_struct *tsk)
+{
+	unsigned long usermsr;
+
+	if (!tsk->thread.regs)
+		return;
+
+	usermsr = tsk->thread.regs->msr;
+
+	if ((usermsr & msr_all_available) == 0)
+		return;
+
+	msr_check_and_set(msr_all_available);
+
+#ifdef CONFIG_PPC_FPU
+	if (usermsr & MSR_FP)
+		save_fpu(tsk);
+#endif
+#ifdef CONFIG_ALTIVEC
+	if (usermsr & MSR_VEC)
+		__giveup_altivec(tsk);
+#endif
+#ifdef CONFIG_VSX
+	if (usermsr & MSR_VSX)
+		__giveup_vsx(tsk);
+#endif
+#ifdef CONFIG_SPE
+	if (usermsr & MSR_SPE)
+		__giveup_spe(tsk);
+#endif
+
+	msr_check_and_clear(msr_all_available);
+}
+
 void flush_all_to_thread(struct task_struct *tsk)
 {
 	if (tsk->thread.regs) {
 		preempt_disable();
 		BUG_ON(tsk != current);
-		giveup_all(tsk);
+		save_all(tsk);
 
 #ifdef CONFIG_SPE
 		if (tsk->thread.regs->msr & MSR_SPE)
--

2.7.0

_______________________________________________
Linuxppc-dev mailing list
[email protected]
https://lists.ozlabs.org/listinfo/linuxppc-dev

author
#15 lore raw

PATCH v2 7/8 powerpc: Add the ability to save Altivec without giving it up

In reply to Cyril Bur

This patch adds the ability to be able to save the VEC registers to the thread struct without giving up (disabling the facility) next time the process returns to userspace.

This patch builds on a previous optimisation for the FPU registers in the thread copy path to avoid a possibly pointless reload of VEC state.

---
 arch/powerpc/include/asm/switch_to.h |  2 +-
 arch/powerpc/kernel/process.c        | 12 +++++++++++-
 arch/powerpc/kernel/vector.S         | 24 ++++--------------------
 3 files changed, 16 insertions(+), 22 deletions(-)
arch/powerpc/include/asm/switch_to.h +1 -1
index c4d50e9..29dda9d 100644
@@ -41,7 +41,7 @@ static inline void flush_fp_to_thread(struct task_struct *t) { }
 extern void enable_kernel_altivec(void);
 extern void flush_altivec_to_thread(struct task_struct *);
 extern void giveup_altivec(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);
arch/powerpc/kernel/process.c +11 -1
index 8a96e4f..5566c32 100644
@@ -200,6 +200,16 @@ EXPORT_SYMBOL(enable_kernel_fp);
 #endif /* CONFIG_PPC_FPU */
 
 #ifdef CONFIG_ALTIVEC
+void __giveup_altivec(struct task_struct *tsk)
+{
+	save_altivec(tsk);
+	tsk->thread.regs->msr &= ~MSR_VEC;
+#ifdef CONFIG_VSX
+	if (cpu_has_feature(CPU_FTR_VSX))
+		tsk->thread.regs->msr &= ~MSR_VSX;
+#endif
+}
+
 void giveup_altivec(struct task_struct *tsk)
 {
 	check_if_tm_restore_required(tsk);
@@ -451,7 +461,7 @@ void save_all(struct task_struct *tsk)
 #endif
 #ifdef CONFIG_ALTIVEC
 	if (usermsr & MSR_VEC)
-		__giveup_altivec(tsk);
+		save_altivec(tsk);
 #endif
 #ifdef CONFIG_VSX
 	if (usermsr & MSR_VSX)
arch/powerpc/kernel/vector.S +4 -20
index 038cff8..51b0c17 100644
@@ -106,36 +106,20 @@ _GLOBAL(load_up_altivec)
 	blr
 
 /*
- * __giveup_altivec(tsk)
- * Disable VMX for the task given as the argument,
- * and save the vector registers in its thread_struct.
+ * save_altivec(tsk)
+ * Save the vector registers to its thread_struct
  */
-_GLOBAL(__giveup_altivec)
+_GLOBAL(save_altivec)
 	addi	r3,r3,THREAD		/* want THREAD of task */
 	PPC_LL	r7,THREAD_VRSAVEAREA(r3)
 	PPC_LL	r5,PT_REGS(r3)
 	PPC_LCMPI	0,r7,0
 	bne	2f
 	addi	r7,r3,THREAD_VRSTATE
-2:	PPC_LCMPI	0,r5,0
-	SAVE_32VRS(0,r4,r7)
+2:	SAVE_32VRS(0,r4,r7)
 	mfvscr	v0
 	li	r4,VRSTATE_VSCR
 	stvx	v0,r4,r7
-	beq	1f
-	PPC_LL	r4,_MSR-STACK_FRAME_OVERHEAD(r5)
-#ifdef CONFIG_VSX
-BEGIN_FTR_SECTION
-	lis	r3,(MSR_VEC|MSR_VSX)@h
-FTR_SECTION_ELSE
-	lis	r3,MSR_VEC@h
-ALT_FTR_SECTION_END_IFSET(CPU_FTR_VSX)
-#else
-	lis	r3,MSR_VEC@h
-#endif
-	andc	r4,r4,r3		/* disable FP for previous task */
-	PPC_STL	r4,_MSR-STACK_FRAME_OVERHEAD(r5)
-1:
 	blr
 
 #ifdef CONFIG_VSX
Signature · 1 line
2.7.0
author
#16 lore raw

PATCH v2 8/8 powerpc: Add the ability to save VSX without giving it up

In reply to Cyril Bur

This patch adds the ability to be able to save the VSX registers to the thread struct without giving up (disabling the facility) next time the process returns to userspace.

This patch builds on a previous optimisation for the FPU and VEC registers in the thread copy path to avoid a possibly pointless reload of VSX state.

---
 arch/powerpc/include/asm/switch_to.h |  1 -
 arch/powerpc/kernel/ppc_ksyms.c      |  4 ----
 arch/powerpc/kernel/process.c        | 23 ++++++++++++++++++-----
 arch/powerpc/kernel/vector.S         | 17 -----------------
 4 files changed, 18 insertions(+), 27 deletions(-)
arch/powerpc/include/asm/switch_to.h +0 -1
index 29dda9d..4dfcd3e 100644
@@ -52,7 +52,6 @@ static inline void disable_kernel_altivec(void)
 extern void enable_kernel_vsx(void);
 extern void flush_vsx_to_thread(struct task_struct *);
 extern void giveup_vsx(struct task_struct *);
-extern void __giveup_vsx(struct task_struct *);
 static inline void disable_kernel_vsx(void)
 {
 	msr_check_and_clear(MSR_FP|MSR_VEC|MSR_VSX);
arch/powerpc/kernel/ppc_ksyms.c +0 -4
index 41e1607..ef7024da 100644
@@ -28,10 +28,6 @@ EXPORT_SYMBOL(load_vr_state);
 EXPORT_SYMBOL(store_vr_state);
 #endif
 
-#ifdef CONFIG_VSX
-EXPORT_SYMBOL_GPL(__giveup_vsx);
-#endif
-
 #ifdef CONFIG_EPAPR_PARAVIRT
 EXPORT_SYMBOL(epapr_hypercall_start);
 #endif
arch/powerpc/kernel/process.c +18 -5
index 5566c32..3d907b8 100644
@@ -252,20 +252,33 @@ EXPORT_SYMBOL_GPL(flush_altivec_to_thread);
 #endif /* CONFIG_ALTIVEC */
 
 #ifdef CONFIG_VSX
-void giveup_vsx(struct task_struct *tsk)
+void __giveup_vsx(struct task_struct *tsk)
 {
-	check_if_tm_restore_required(tsk);
-
-	msr_check_and_set(MSR_FP|MSR_VEC|MSR_VSX);
 	if (tsk->thread.regs->msr & MSR_FP)
 		__giveup_fpu(tsk);
 	if (tsk->thread.regs->msr & MSR_VEC)
 		__giveup_altivec(tsk);
+	tsk->thread.regs->msr &= ~MSR_VSX;
+}
+
+void giveup_vsx(struct task_struct *tsk)
+{
+	check_if_tm_restore_required(tsk);
+
+	msr_check_and_set(MSR_FP|MSR_VEC|MSR_VSX);
 	__giveup_vsx(tsk);
 	msr_check_and_clear(MSR_FP|MSR_VEC|MSR_VSX);
 }
 EXPORT_SYMBOL(giveup_vsx);
 
+void save_vsx(struct task_struct *tsk)
+{
+	if (tsk->thread.regs->msr & MSR_FP)
+		save_fpu(tsk);
+	if (tsk->thread.regs->msr & MSR_VEC)
+		save_altivec(tsk);
+}
+
 void enable_kernel_vsx(void)
 {
 	WARN_ON(preemptible());
@@ -465,7 +478,7 @@ void save_all(struct task_struct *tsk)
 #endif
 #ifdef CONFIG_VSX
 	if (usermsr & MSR_VSX)
-		__giveup_vsx(tsk);
+		save_vsx(tsk);
 #endif
 #ifdef CONFIG_SPE
 	if (usermsr & MSR_SPE)
arch/powerpc/kernel/vector.S +0 -17
index 51b0c17..1c2e7a3 100644
@@ -151,23 +151,6 @@ _GLOBAL(load_up_vsx)
 	std	r12,_MSR(r1)
 	b	fast_exception_return
 
-/*
- * __giveup_vsx(tsk)
- * Disable VSX for the task given as the argument.
- * Does NOT save vsx registers.
- */
-_GLOBAL(__giveup_vsx)
-	addi	r3,r3,THREAD		/* want THREAD of task */
-	ld	r5,PT_REGS(r3)
-	cmpdi	0,r5,0
-	beq	1f
-	ld	r4,_MSR-STACK_FRAME_OVERHEAD(r5)
-	lis	r3,MSR_VSX@h
-	andc	r4,r4,r3		/* disable VSX for previous task */
-	std	r4,_MSR-STACK_FRAME_OVERHEAD(r5)
-1:
-	blr
-
 #endif /* CONFIG_VSX */
 
 
Signature · 1 line
2.7.0
#17 lore raw

Re: [PATCH V2 8/8] powerpc: Add the ability to save VSX without giving it up

Quoted 92 lines · Cyril Bur

This patch adds the ability to be able to save the VSX registers to the
thread struct without giving up (disabling the facility) next time the
process returns to userspace.
=20
This patch builds on a previous optimisation for the FPU and VEC registers
in the thread copy path to avoid a possibly pointless reload of VSX state.
=20

---
 arch/powerpc/include/asm/switch_to.h |  1 -
 arch/powerpc/kernel/ppc_ksyms.c      |  4 ----
 arch/powerpc/kernel/process.c        | 23 ++++++++++++++++++-----
 arch/powerpc/kernel/vector.S         | 17 -----------------
 4 files changed, 18 insertions(+), 27 deletions(-)

=20

arch/powerpc/include/asm/switch_to.h +0 -1
index 29dda9d..4dfcd3e 100644
@@ -52,7 +52,6 @@ static inline void disable_kernel_altivec(void)
 extern void enable_kernel_vsx(void);
 extern void flush_vsx_to_thread(struct task_struct *);
 extern void giveup_vsx(struct task_struct *);
-extern void __giveup_vsx(struct task_struct *);
 static inline void disable_kernel_vsx(void)
 {
 	msr_check_and_clear(MSR_FP|MSR_VEC|MSR_VSX);
arch/powerpc/kernel/ppc_ksyms.c +0 -0
index 41e1607..ef7024da 100644
@@ -28,10 +28,6 @@ EXPORT_SYMBOL(load_vr_state);
 EXPORT_SYMBOL(store_vr_state);
 #endif

=20

arch/powerpc/kernel/ppc_ksyms.c +0 -4
-#ifdef CONFIG_VSX
-EXPORT_SYMBOL_GPL(__giveup_vsx);
-#endif
-
 #ifdef CONFIG_EPAPR_PARAVIRT
 EXPORT_SYMBOL(epapr_hypercall_start);
 #endif
arch/powerpc/kernel/process.c +0 -0
index 5566c32..3d907b8 100644
@@ -252,20 +252,33 @@ EXPORT_SYMBOL_GPL(flush_altivec_to_thread);
 #endif /* CONFIG_ALTIVEC */

=20
#ifdef CONFIG_VSX

arch/powerpc/kernel/process.c +9 -4
-void giveup_vsx(struct task_struct *tsk)
+void __giveup_vsx(struct task_struct *tsk)
 {
-	check_if_tm_restore_required(tsk);
-
-	msr_check_and_set(MSR_FP|MSR_VEC|MSR_VSX);
 	if (tsk->thread.regs->msr & MSR_FP)
 		__giveup_fpu(tsk);
 	if (tsk->thread.regs->msr & MSR_VEC)
 		__giveup_altivec(tsk);
+	tsk->thread.regs->msr &=3D ~MSR_VSX;
+}
+
+void giveup_vsx(struct task_struct *tsk)
+{
+	check_if_tm_restore_required(tsk);
+
+	msr_check_and_set(MSR_FP|MSR_VEC|MSR_VSX);
 	__giveup_vsx(tsk);
 	msr_check_and_clear(MSR_FP|MSR_VEC|MSR_VSX);
 }
 EXPORT_SYMBOL(giveup_vsx);

=20

arch/powerpc/kernel/process.c +8 -0
+void save_vsx(struct task_struct *tsk)
+{
+	if (tsk->thread.regs->msr & MSR_FP)
+		save_fpu(tsk);
+	if (tsk->thread.regs->msr & MSR_VEC)
+		save_altivec(tsk);
+}
+
 void enable_kernel_vsx(void)
 {
 	WARN_ON(preemptible());

arch/powerpc/kernel/process.c void save_all(struct task_struct *tsk) · line 481

Quoted 5 lines
arch/powerpc/kernel/process.c +1 -1
@@ -465,7 +478,7 @@ void save_all(struct task_struct *tsk)
 #endif
 #ifdef CONFIG_VSX
 	if (usermsr & MSR_VSX)
-		__giveup_vsx(tsk);
+		save_vsx(tsk);

This seems suboptimal. save_vsx() will call save_fpu() and save_altivec() again, which you just called earlier in save_all().

save_vsx() is only used here, so could be static.=20

Also, put the #ifdef junk as part of the function so that the caller doesn't have to deal with it.=20

Mikey

Quoted 34 lines
arch/powerpc/kernel/process.c +0 -0
 #endif
 #ifdef CONFIG_SPE
 	if (usermsr & MSR_SPE)
arch/powerpc/kernel/vector.S +0 -0
index 51b0c17..1c2e7a3 100644
@@ -151,23 +151,6 @@ _GLOBAL(load_up_vsx)
 	std	r12,_MSR(r1)
 	b	fast_exception_return

=20

arch/powerpc/kernel/vector.S +0 -7
-/*
- * __giveup_vsx(tsk)
- * Disable VSX for the task given as the argument.
- * Does NOT save vsx registers.
- */
-_GLOBAL(__giveup_vsx)
-	addi	r3,r3,THREAD		/* want THREAD of

task */

arch/powerpc/kernel/vector.S +0 -6
-	ld	r5,PT_REGS(r3)
-	cmpdi	0,r5,0
-	beq	1f
-	ld	r4,_MSR-STACK_FRAME_OVERHEAD(r5)
-	lis	r3,MSR_VSX@h
-	andc	r4,r4,r3		/* disable VSX for

previous task */

arch/powerpc/kernel/vector.S +0 -4
-	std	r4,_MSR-STACK_FRAME_OVERHEAD(r5)
-1:
-	blr
-
 #endif /* CONFIG_VSX */

=20
=20

author
#18 lore raw

Re: [PATCH V2 8/8] powerpc: Add the ability to save VSX without giving it up

Quoted 93 lines · On Fri, 15 Jan 2016 17:25:26 +1100 Michael Neuling <[email protected]> wrote:
Quoted 92 lines · Cyril Bur

This patch adds the ability to be able to save the VSX registers to the
thread struct without giving up (disabling the facility) next time the
process returns to userspace.

This patch builds on a previous optimisation for the FPU and VEC registers
in the thread copy path to avoid a possibly pointless reload of VSX state.

---
 arch/powerpc/include/asm/switch_to.h |  1 -
 arch/powerpc/kernel/ppc_ksyms.c      |  4 ----
 arch/powerpc/kernel/process.c        | 23 ++++++++++++++++++-----
 arch/powerpc/kernel/vector.S         | 17 -----------------
 4 files changed, 18 insertions(+), 27 deletions(-)
arch/powerpc/include/asm/switch_to.h +0 -1
index 29dda9d..4dfcd3e 100644
@@ -52,7 +52,6 @@ static inline void disable_kernel_altivec(void)
 extern void enable_kernel_vsx(void);
 extern void flush_vsx_to_thread(struct task_struct *);
 extern void giveup_vsx(struct task_struct *);
-extern void __giveup_vsx(struct task_struct *);
 static inline void disable_kernel_vsx(void)
 {
 	msr_check_and_clear(MSR_FP|MSR_VEC|MSR_VSX);
arch/powerpc/kernel/ppc_ksyms.c +0 -4
index 41e1607..ef7024da 100644
@@ -28,10 +28,6 @@ EXPORT_SYMBOL(load_vr_state);
 EXPORT_SYMBOL(store_vr_state);
 #endif
 
-#ifdef CONFIG_VSX
-EXPORT_SYMBOL_GPL(__giveup_vsx);
-#endif
-
 #ifdef CONFIG_EPAPR_PARAVIRT
 EXPORT_SYMBOL(epapr_hypercall_start);
 #endif
arch/powerpc/kernel/process.c +17 -4
index 5566c32..3d907b8 100644
@@ -252,20 +252,33 @@ EXPORT_SYMBOL_GPL(flush_altivec_to_thread);
 #endif /* CONFIG_ALTIVEC */
 
 #ifdef CONFIG_VSX
-void giveup_vsx(struct task_struct *tsk)
+void __giveup_vsx(struct task_struct *tsk)
 {
-	check_if_tm_restore_required(tsk);
-
-	msr_check_and_set(MSR_FP|MSR_VEC|MSR_VSX);
 	if (tsk->thread.regs->msr & MSR_FP)
 		__giveup_fpu(tsk);
 	if (tsk->thread.regs->msr & MSR_VEC)
 		__giveup_altivec(tsk);
+	tsk->thread.regs->msr &= ~MSR_VSX;
+}
+
+void giveup_vsx(struct task_struct *tsk)
+{
+	check_if_tm_restore_required(tsk);
+
+	msr_check_and_set(MSR_FP|MSR_VEC|MSR_VSX);
 	__giveup_vsx(tsk);
 	msr_check_and_clear(MSR_FP|MSR_VEC|MSR_VSX);
 }
 EXPORT_SYMBOL(giveup_vsx);
 
+void save_vsx(struct task_struct *tsk)
+{
+	if (tsk->thread.regs->msr & MSR_FP)
+		save_fpu(tsk);
+	if (tsk->thread.regs->msr & MSR_VEC)
+		save_altivec(tsk);
+}
+
 void enable_kernel_vsx(void)
 {
 	WARN_ON(preemptible());
@@ -465,7 +478,7 @@ void save_all(struct task_struct *tsk)
Quoted 9 lines

arch/powerpc/kernel/process.c void save_all(struct task_struct *tsk) · line 481

Quoted 5 lines
arch/powerpc/kernel/process.c +1 -1
 #endif
 #ifdef CONFIG_VSX
 	if (usermsr & MSR_VSX)
-		__giveup_vsx(tsk);
+		save_vsx(tsk);  

This seems suboptimal. save_vsx() will call save_fpu() and save_altivec() again, which you just called earlier in save_all().

Ah yes, will fix

Quoted 2 lines

save_vsx() is only used here, so could be static.

Thanks.

Quoted 3 lines

Also, put the #ifdef junk as part of the function so that the caller doesn't have to deal with it.

Can do absolutely, however this means that in save_all I can't check if the function needs to be called or not. For example, without CONFIG_VSX, MSR_VSX won't exist which means we might end up calling save_vsx THEN checking MSR_VSX and returning early.

I'm happy to defer to you and mpe on what's nicer, I would side with avoiding the function call at the cost of ugly #ifdefs but I can always see the merits of clean code.

Thanks for the review,

Cyril

Quoted 36 lines

Mikey

Quoted 34 lines
arch/powerpc/kernel/process.c +0 -0
 #endif
 #ifdef CONFIG_SPE
 	if (usermsr & MSR_SPE)
arch/powerpc/kernel/vector.S +0 -7
index 51b0c17..1c2e7a3 100644
@@ -151,23 +151,6 @@ _GLOBAL(load_up_vsx)
 	std	r12,_MSR(r1)
 	b	fast_exception_return
 
-/*
- * __giveup_vsx(tsk)
- * Disable VSX for the task given as the argument.
- * Does NOT save vsx registers.
- */
-_GLOBAL(__giveup_vsx)
-	addi	r3,r3,THREAD		/* want THREAD of

task */

arch/powerpc/kernel/vector.S +0 -6
-	ld	r5,PT_REGS(r3)
-	cmpdi	0,r5,0
-	beq	1f
-	ld	r4,_MSR-STACK_FRAME_OVERHEAD(r5)
-	lis	r3,MSR_VSX@h
-	andc	r4,r4,r3		/* disable VSX for

previous task */

arch/powerpc/kernel/vector.S +0 -4
-	std	r4,_MSR-STACK_FRAME_OVERHEAD(r5)
-1:
-	blr
-
 #endif /* CONFIG_VSX */
#19 lore raw

Re: [PATCH V2 8/8] powerpc: Add the ability to save VSX without giving it up

Quoted 6 lines
Quoted 4 lines

Also, put the #ifdef junk as part of the function so that the caller doesn't have to deal with it.=20
=20

=20
Can do absolutely, however this means that in save_all I can't check if t=

he

Quoted 1 line

function needs to be called or not. For example, without CONFIG_VSX, MSR_=

VSX

Quoted 1 line

won't exist which means we might end up calling save_vsx THEN checking MS=

R_VSX

Quoted 1 line

and returning early.

MSR_VSX should exist even without CONFIG_VSX, so you should be able to do it.

Quoted 1 line

I'm happy to defer to you and mpe on what's nicer, I would side with avoi=

ding

Quoted 1 line

the function call at the cost of ugly #ifdefs but I can always see the me=

rits

Quoted 1 line

of clean code.

I'd prefer the cleanup. This code has been pretty bad in the past for being a bit of an #ifdef mess, so it'd be nice to clean it up a bit if possible.

Mikey