aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSam bobroff <sam.bobroff@au1.ibm.com>2014-06-05 02:19:22 -0400
committerBenjamin Herrenschmidt <benh@kernel.crashing.org>2014-06-11 03:02:56 -0400
commit96d016108640bc2b7fb0ee800737f80923847294 (patch)
treefb14f3cf31e266ff70d4585c9d2aa91b5df5dc45
parentfb5a515704d7e84c139140a83c5eff515adfc000 (diff)
powerpc: Correct DSCR during TM context switch
Correct the DSCR SPR becoming temporarily corrupted if a task is context switched during a transaction. The problem occurs while suspending the task and is caused by saving the DSCR to thread.dscr after it has already been set to the CPU's default value: __switch_to() calls __switch_to_tm() which calls tm_reclaim_task() which calls tm_reclaim_thread() which calls tm_reclaim() where the DSCR is set to the CPU's default __switch_to() calls _switch() where thread.dscr is set to the DSCR When the task is resumed, it's transaction will be doomed (as usual) and the DSCR SPR will be corrupted, although the checkpointed value will be correct. Therefore the DSCR will be immediately corrected by the transaction aborting, unless it has been suspended. In that case the incorrect value can be seen by the task until it resumes the transaction. The fix is to treat the DSCR similarly to the TAR and save it early in __switch_to(). A program exposing the problem is added to the kernel self tests as: tools/testing/selftests/powerpc/tm/tm-resched-dscr. Signed-off-by: Sam Bobroff <sam.bobroff@au1.ibm.com> CC: <stable@vger.kernel.org> [v3.10+] Signed-off-by: Benjamin Herrenschmidt <benh@kernel.crashing.org>
-rw-r--r--arch/powerpc/include/asm/switch_to.h6
-rw-r--r--arch/powerpc/kernel/entry_64.S6
-rw-r--r--arch/powerpc/kernel/process.c8
-rw-r--r--tools/testing/selftests/powerpc/Makefile2
-rw-r--r--tools/testing/selftests/powerpc/tm/Makefile15
-rw-r--r--tools/testing/selftests/powerpc/tm/tm-resched-dscr.c90
6 files changed, 114 insertions, 13 deletions
diff --git a/arch/powerpc/include/asm/switch_to.h b/arch/powerpc/include/asm/switch_to.h
index 0e83e7d8c73f..d2468eb12639 100644
--- a/arch/powerpc/include/asm/switch_to.h
+++ b/arch/powerpc/include/asm/switch_to.h
@@ -16,13 +16,15 @@ struct thread_struct;
16extern struct task_struct *_switch(struct thread_struct *prev, 16extern struct task_struct *_switch(struct thread_struct *prev,
17 struct thread_struct *next); 17 struct thread_struct *next);
18#ifdef CONFIG_PPC_BOOK3S_64 18#ifdef CONFIG_PPC_BOOK3S_64
19static inline void save_tar(struct thread_struct *prev) 19static inline void save_early_sprs(struct thread_struct *prev)
20{ 20{
21 if (cpu_has_feature(CPU_FTR_ARCH_207S)) 21 if (cpu_has_feature(CPU_FTR_ARCH_207S))
22 prev->tar = mfspr(SPRN_TAR); 22 prev->tar = mfspr(SPRN_TAR);
23 if (cpu_has_feature(CPU_FTR_DSCR))
24 prev->dscr = mfspr(SPRN_DSCR);
23} 25}
24#else 26#else
25static inline void save_tar(struct thread_struct *prev) {} 27static inline void save_early_sprs(struct thread_struct *prev) {}
26#endif 28#endif
27 29
28extern void enable_kernel_fp(void); 30extern void enable_kernel_fp(void);
diff --git a/arch/powerpc/kernel/entry_64.S b/arch/powerpc/kernel/entry_64.S
index 911d45366f59..6528c5e2cc44 100644
--- a/arch/powerpc/kernel/entry_64.S
+++ b/arch/powerpc/kernel/entry_64.S
@@ -428,12 +428,6 @@ BEGIN_FTR_SECTION
428 std r24,THREAD_VRSAVE(r3) 428 std r24,THREAD_VRSAVE(r3)
429END_FTR_SECTION_IFSET(CPU_FTR_ALTIVEC) 429END_FTR_SECTION_IFSET(CPU_FTR_ALTIVEC)
430#endif /* CONFIG_ALTIVEC */ 430#endif /* CONFIG_ALTIVEC */
431#ifdef CONFIG_PPC64
432BEGIN_FTR_SECTION
433 mfspr r25,SPRN_DSCR
434 std r25,THREAD_DSCR(r3)
435END_FTR_SECTION_IFSET(CPU_FTR_DSCR)
436#endif
437 and. r0,r0,r22 431 and. r0,r0,r22
438 beq+ 1f 432 beq+ 1f
439 andc r22,r22,r0 433 andc r22,r22,r0
diff --git a/arch/powerpc/kernel/process.c b/arch/powerpc/kernel/process.c
index 8a1edbe26b8f..be99774d3f44 100644
--- a/arch/powerpc/kernel/process.c
+++ b/arch/powerpc/kernel/process.c
@@ -755,15 +755,15 @@ struct task_struct *__switch_to(struct task_struct *prev,
755 755
756 WARN_ON(!irqs_disabled()); 756 WARN_ON(!irqs_disabled());
757 757
758 /* Back up the TAR across context switches. 758 /* Back up the TAR and DSCR across context switches.
759 * Note that the TAR is not available for use in the kernel. (To 759 * Note that the TAR is not available for use in the kernel. (To
760 * provide this, the TAR should be backed up/restored on exception 760 * provide this, the TAR should be backed up/restored on exception
761 * entry/exit instead, and be in pt_regs. FIXME, this should be in 761 * entry/exit instead, and be in pt_regs. FIXME, this should be in
762 * pt_regs anyway (for debug).) 762 * pt_regs anyway (for debug).)
763 * Save the TAR here before we do treclaim/trecheckpoint as these 763 * Save the TAR and DSCR here before we do treclaim/trecheckpoint as
764 * will change the TAR. 764 * these will change them.
765 */ 765 */
766 save_tar(&prev->thread); 766 save_early_sprs(&prev->thread);
767 767
768 __switch_to_tm(prev); 768 __switch_to_tm(prev);
769 769
diff --git a/tools/testing/selftests/powerpc/Makefile b/tools/testing/selftests/powerpc/Makefile
index b3dbe9ef1a40..54833a791a44 100644
--- a/tools/testing/selftests/powerpc/Makefile
+++ b/tools/testing/selftests/powerpc/Makefile
@@ -13,7 +13,7 @@ CFLAGS := -Wall -O2 -flto -Wall -Werror -DGIT_VERSION='"$(GIT_VERSION)"' -I$(CUR
13 13
14export CC CFLAGS 14export CC CFLAGS
15 15
16TARGETS = pmu copyloops mm 16TARGETS = pmu copyloops mm tm
17 17
18endif 18endif
19 19
diff --git a/tools/testing/selftests/powerpc/tm/Makefile b/tools/testing/selftests/powerpc/tm/Makefile
new file mode 100644
index 000000000000..51267f4184a6
--- /dev/null
+++ b/tools/testing/selftests/powerpc/tm/Makefile
@@ -0,0 +1,15 @@
1PROGS := tm-resched-dscr
2
3all: $(PROGS)
4
5$(PROGS):
6
7run_tests: all
8 @-for PROG in $(PROGS); do \
9 ./$$PROG; \
10 done;
11
12clean:
13 rm -f $(PROGS) *.o
14
15.PHONY: all run_tests clean
diff --git a/tools/testing/selftests/powerpc/tm/tm-resched-dscr.c b/tools/testing/selftests/powerpc/tm/tm-resched-dscr.c
new file mode 100644
index 000000000000..ee98e3886af2
--- /dev/null
+++ b/tools/testing/selftests/powerpc/tm/tm-resched-dscr.c
@@ -0,0 +1,90 @@
1/* Test context switching to see if the DSCR SPR is correctly preserved
2 * when within a transaction.
3 *
4 * Note: We assume that the DSCR has been left at the default value (0)
5 * for all CPUs.
6 *
7 * Method:
8 *
9 * Set a value into the DSCR.
10 *
11 * Start a transaction, and suspend it (*).
12 *
13 * Hard loop checking to see if the transaction has become doomed.
14 *
15 * Now that we *may* have been preempted, record the DSCR and TEXASR SPRS.
16 *
17 * If the abort was because of a context switch, check the DSCR value.
18 * Otherwise, try again.
19 *
20 * (*) If the transaction is not suspended we can't see the problem because
21 * the transaction abort handler will restore the DSCR to it's checkpointed
22 * value before we regain control.
23 */
24
25#include <inttypes.h>
26#include <stdio.h>
27#include <stdlib.h>
28#include <assert.h>
29#include <asm/tm.h>
30
31#define TBEGIN ".long 0x7C00051D ;"
32#define TEND ".long 0x7C00055D ;"
33#define TCHECK ".long 0x7C00059C ;"
34#define TSUSPEND ".long 0x7C0005DD ;"
35#define TRESUME ".long 0x7C2005DD ;"
36#define SPRN_TEXASR 0x82
37#define SPRN_DSCR 0x03
38
39int main(void) {
40 uint64_t rv, dscr1 = 1, dscr2, texasr;
41
42 printf("Check DSCR TM context switch: ");
43 fflush(stdout);
44 for (;;) {
45 rv = 1;
46 asm __volatile__ (
47 /* set a known value into the DSCR */
48 "ld 3, %[dscr1];"
49 "mtspr %[sprn_dscr], 3;"
50
51 /* start and suspend a transaction */
52 TBEGIN
53 "beq 1f;"
54 TSUSPEND
55
56 /* hard loop until the transaction becomes doomed */
57 "2: ;"
58 TCHECK
59 "bc 4, 0, 2b;"
60
61 /* record DSCR and TEXASR */
62 "mfspr 3, %[sprn_dscr];"
63 "std 3, %[dscr2];"
64 "mfspr 3, %[sprn_texasr];"
65 "std 3, %[texasr];"
66
67 TRESUME
68 TEND
69 "li %[rv], 0;"
70 "1: ;"
71 : [rv]"=r"(rv), [dscr2]"=m"(dscr2), [texasr]"=m"(texasr)
72 : [dscr1]"m"(dscr1)
73 , [sprn_dscr]"i"(SPRN_DSCR), [sprn_texasr]"i"(SPRN_TEXASR)
74 : "memory", "r3"
75 );
76 assert(rv); /* make sure the transaction aborted */
77 if ((texasr >> 56) != TM_CAUSE_RESCHED) {
78 putchar('.');
79 fflush(stdout);
80 continue;
81 }
82 if (dscr2 != dscr1) {
83 printf(" FAIL\n");
84 exit(EXIT_FAILURE);
85 } else {
86 printf(" OK\n");
87 exit(EXIT_SUCCESS);
88 }
89 }
90}