Skip to content

Commit b4b56f9

Browse files
sambltcmpe
authored andcommitted
powerpc/tm: Abort syscalls in active transactions
This patch changes the syscall handler to doom (tabort) active transactions when a syscall is made and return very early without performing the syscall and keeping side effects to a minimum (no CPU accounting or system call tracing is performed). Also included is a new HWCAP2 bit, PPC_FEATURE2_HTM_NOSC, to indicate this behaviour to userspace. Currently, the system call instruction automatically suspends an active transaction which causes side effects to persist when an active transaction fails. This does change the kernel's behaviour, but in a way that was documented as unsupported. It doesn't reduce functionality as syscalls will still be performed after tsuspend; it just requires that the transaction be explicitly suspended. It also provides a consistent interface and makes the behaviour of user code substantially the same across powerpc and platforms that do not support suspended transactions (e.g. x86 and s390). Performance measurements using http://ozlabs.org/~anton/junkcode/null_syscall.c indicate the cost of a normal (non-aborted) system call increases by about 0.25%. Signed-off-by: Sam Bobroff <[email protected]> Signed-off-by: Michael Ellerman <[email protected]>
1 parent b592643 commit b4b56f9

File tree

8 files changed

+66
-25
lines changed

8 files changed

+66
-25
lines changed

Documentation/powerpc/transactional_memory.txt

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -74,22 +74,23 @@ Causes of transaction aborts
7474
Syscalls
7575
========
7676

77-
Performing syscalls from within transaction is not recommended, and can lead
78-
to unpredictable results.
77+
Syscalls made from within an active transaction will not be performed and the
78+
transaction will be doomed by the kernel with the failure code TM_CAUSE_SYSCALL
79+
| TM_CAUSE_PERSISTENT.
7980

80-
Syscalls do not by design abort transactions, but beware: The kernel code will
81-
not be running in transactional state. The effect of syscalls will always
82-
remain visible, but depending on the call they may abort your transaction as a
83-
side-effect, read soon-to-be-aborted transactional data that should not remain
84-
invisible, etc. If you constantly retry a transaction that constantly aborts
85-
itself by calling a syscall, you'll have a livelock & make no progress.
81+
Syscalls made from within a suspended transaction are performed as normal and
82+
the transaction is not explicitly doomed by the kernel. However, what the
83+
kernel does to perform the syscall may result in the transaction being doomed
84+
by the hardware. The syscall is performed in suspended mode so any side
85+
effects will be persistent, independent of transaction success or failure. No
86+
guarantees are provided by the kernel about which syscalls will affect
87+
transaction success.
8688

87-
Simple syscalls (e.g. sigprocmask()) "could" be OK. Even things like write()
88-
from, say, printf() should be OK as long as the kernel does not access any
89-
memory that was accessed transactionally.
90-
91-
Consider any syscalls that happen to work as debug-only -- not recommended for
92-
production use. Best to queue them up till after the transaction is over.
89+
Care must be taken when relying on syscalls to abort during active transactions
90+
if the calls are made via a library. Libraries may cache values (which may
91+
give the appearance of success) or perform operations that cause transaction
92+
failure before entering the kernel (which may produce different failure codes).
93+
Examples are glibc's getpid() and lazy symbol resolution.
9394

9495

9596
Signals
@@ -176,8 +177,7 @@ kernel aborted a transaction:
176177
TM_CAUSE_RESCHED Thread was rescheduled.
177178
TM_CAUSE_TLBI Software TLB invalid.
178179
TM_CAUSE_FAC_UNAV FP/VEC/VSX unavailable trap.
179-
TM_CAUSE_SYSCALL Currently unused; future syscalls that must abort
180-
transactions for consistency will use this.
180+
TM_CAUSE_SYSCALL Syscall from active transaction.
181181
TM_CAUSE_SIGNAL Signal delivered.
182182
TM_CAUSE_MISC Currently unused.
183183
TM_CAUSE_ALIGNMENT Alignment fault.

arch/powerpc/include/asm/cputable.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -242,11 +242,13 @@ enum {
242242

243243
/* We only set the TM feature if the kernel was compiled with TM supprt */
244244
#ifdef CONFIG_PPC_TRANSACTIONAL_MEM
245-
#define CPU_FTR_TM_COMP CPU_FTR_TM
246-
#define PPC_FEATURE2_HTM_COMP PPC_FEATURE2_HTM
245+
#define CPU_FTR_TM_COMP CPU_FTR_TM
246+
#define PPC_FEATURE2_HTM_COMP PPC_FEATURE2_HTM
247+
#define PPC_FEATURE2_HTM_NOSC_COMP PPC_FEATURE2_HTM_NOSC
247248
#else
248-
#define CPU_FTR_TM_COMP 0
249-
#define PPC_FEATURE2_HTM_COMP 0
249+
#define CPU_FTR_TM_COMP 0
250+
#define PPC_FEATURE2_HTM_COMP 0
251+
#define PPC_FEATURE2_HTM_NOSC_COMP 0
250252
#endif
251253

252254
/* We need to mark all pages as being coherent if we're SMP or we have a

arch/powerpc/include/uapi/asm/cputable.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,5 +42,6 @@
4242
#define PPC_FEATURE2_ISEL 0x08000000
4343
#define PPC_FEATURE2_TAR 0x04000000
4444
#define PPC_FEATURE2_VEC_CRYPTO 0x02000000
45+
#define PPC_FEATURE2_HTM_NOSC 0x01000000
4546

4647
#endif /* _UAPI__ASM_POWERPC_CPUTABLE_H */

arch/powerpc/include/uapi/asm/tm.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
#define TM_CAUSE_RESCHED 0xde
1212
#define TM_CAUSE_TLBI 0xdc
1313
#define TM_CAUSE_FAC_UNAV 0xda
14-
#define TM_CAUSE_SYSCALL 0xd8 /* future use */
14+
#define TM_CAUSE_SYSCALL 0xd8
1515
#define TM_CAUSE_MISC 0xd6 /* future use */
1616
#define TM_CAUSE_SIGNAL 0xd4
1717
#define TM_CAUSE_ALIGNMENT 0xd2

arch/powerpc/kernel/cputable.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,9 @@ extern void __restore_cpu_e6500(void);
108108
PPC_FEATURE_TRUE_LE | \
109109
PPC_FEATURE_PSERIES_PERFMON_COMPAT)
110110
#define COMMON_USER2_POWER8 (PPC_FEATURE2_ARCH_2_07 | \
111-
PPC_FEATURE2_HTM_COMP | PPC_FEATURE2_DSCR | \
111+
PPC_FEATURE2_HTM_COMP | \
112+
PPC_FEATURE2_HTM_NOSC_COMP | \
113+
PPC_FEATURE2_DSCR | \
112114
PPC_FEATURE2_ISEL | PPC_FEATURE2_TAR | \
113115
PPC_FEATURE2_VEC_CRYPTO)
114116
#define COMMON_USER_PA6T (COMMON_USER_PPC64 | PPC_FEATURE_PA6T |\

arch/powerpc/kernel/entry_64.S

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
#include <asm/ftrace.h>
3535
#include <asm/hw_irq.h>
3636
#include <asm/context_tracking.h>
37+
#include <asm/tm.h>
3738

3839
/*
3940
* System calls.
@@ -51,6 +52,12 @@ exception_marker:
5152

5253
.globl system_call_common
5354
system_call_common:
55+
#ifdef CONFIG_PPC_TRANSACTIONAL_MEM
56+
BEGIN_FTR_SECTION
57+
extrdi. r10, r12, 1, (63-MSR_TS_T_LG) /* transaction active? */
58+
bne tabort_syscall
59+
END_FTR_SECTION_IFSET(CPU_FTR_TM)
60+
#endif
5461
andi. r10,r12,MSR_PR
5562
mr r10,r1
5663
addi r1,r1,-INT_FRAME_SIZE
@@ -311,6 +318,34 @@ syscall_exit_work:
311318
bl do_syscall_trace_leave
312319
b ret_from_except
313320

321+
#ifdef CONFIG_PPC_TRANSACTIONAL_MEM
322+
tabort_syscall:
323+
/* Firstly we need to enable TM in the kernel */
324+
mfmsr r10
325+
li r13, 1
326+
rldimi r10, r13, MSR_TM_LG, 63-MSR_TM_LG
327+
mtmsrd r10, 0
328+
329+
/* tabort, this dooms the transaction, nothing else */
330+
li r13, (TM_CAUSE_SYSCALL|TM_CAUSE_PERSISTENT)
331+
TABORT(R13)
332+
333+
/*
334+
* Return directly to userspace. We have corrupted user register state,
335+
* but userspace will never see that register state. Execution will
336+
* resume after the tbegin of the aborted transaction with the
337+
* checkpointed register state.
338+
*/
339+
li r13, MSR_RI
340+
andc r10, r10, r13
341+
mtmsrd r10, 1
342+
mtspr SPRN_SRR0, r11
343+
mtspr SPRN_SRR1, r12
344+
345+
rfid
346+
b . /* prevent speculative execution */
347+
#endif
348+
314349
/* Save non-volatile GPRs, if not already saved. */
315350
_GLOBAL(save_nvgprs)
316351
ld r11,_TRAP(r1)

tools/testing/selftests/powerpc/tm/Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
TEST_PROGS := tm-resched-dscr
1+
TEST_PROGS := tm-resched-dscr tm-syscall
22

33
all: $(TEST_PROGS)
44

55
$(TEST_PROGS): ../harness.c
66

77
tm-syscall: tm-syscall-asm.S
8-
tm-syscall: CFLAGS += -mhtm
8+
tm-syscall: CFLAGS += -mhtm -I../../../../../usr/include
99

1010
include ../../lib.mk
1111

tools/testing/selftests/powerpc/tm/tm-syscall.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,8 @@ int tm_syscall(void)
8282
unsigned count = 0;
8383
struct timeval end, now;
8484

85-
SKIP_IF(!((long)get_auxv_entry(AT_HWCAP2) & PPC_FEATURE2_HTM));
85+
SKIP_IF(!((long)get_auxv_entry(AT_HWCAP2)
86+
& PPC_FEATURE2_HTM_NOSC));
8687
setbuf(stdout, NULL);
8788

8889
printf("Testing transactional syscalls for %d seconds...\n", TEST_DURATION);

0 commit comments

Comments
 (0)