Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

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 <sam.bobroff@au1.ibm.com>
Signed-off-by: Michael Ellerman <mpe@ellerman.id.au>

authored by

Sam bobroff and committed by
Michael Ellerman
b4b56f9e b5926430

+66 -25
+16 -16
Documentation/powerpc/transactional_memory.txt
··· 74 74 Syscalls 75 75 ======== 76 76 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. 79 80 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. 86 88 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. 93 94 94 95 95 96 Signals ··· 177 176 TM_CAUSE_RESCHED Thread was rescheduled. 178 177 TM_CAUSE_TLBI Software TLB invalid. 179 178 TM_CAUSE_FAC_UNAV FP/VEC/VSX unavailable trap. 180 - TM_CAUSE_SYSCALL Currently unused; future syscalls that must abort 181 - transactions for consistency will use this. 179 + TM_CAUSE_SYSCALL Syscall from active transaction. 182 180 TM_CAUSE_SIGNAL Signal delivered. 183 181 TM_CAUSE_MISC Currently unused. 184 182 TM_CAUSE_ALIGNMENT Alignment fault.
+6 -4
arch/powerpc/include/asm/cputable.h
··· 242 242 243 243 /* We only set the TM feature if the kernel was compiled with TM supprt */ 244 244 #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 247 248 #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 250 252 #endif 251 253 252 254 /* We need to mark all pages as being coherent if we're SMP or we have a
+1
arch/powerpc/include/uapi/asm/cputable.h
··· 42 42 #define PPC_FEATURE2_ISEL 0x08000000 43 43 #define PPC_FEATURE2_TAR 0x04000000 44 44 #define PPC_FEATURE2_VEC_CRYPTO 0x02000000 45 + #define PPC_FEATURE2_HTM_NOSC 0x01000000 45 46 46 47 #endif /* _UAPI__ASM_POWERPC_CPUTABLE_H */
+1 -1
arch/powerpc/include/uapi/asm/tm.h
··· 11 11 #define TM_CAUSE_RESCHED 0xde 12 12 #define TM_CAUSE_TLBI 0xdc 13 13 #define TM_CAUSE_FAC_UNAV 0xda 14 - #define TM_CAUSE_SYSCALL 0xd8 /* future use */ 14 + #define TM_CAUSE_SYSCALL 0xd8 15 15 #define TM_CAUSE_MISC 0xd6 /* future use */ 16 16 #define TM_CAUSE_SIGNAL 0xd4 17 17 #define TM_CAUSE_ALIGNMENT 0xd2
+3 -1
arch/powerpc/kernel/cputable.c
··· 108 108 PPC_FEATURE_TRUE_LE | \ 109 109 PPC_FEATURE_PSERIES_PERFMON_COMPAT) 110 110 #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 | \ 112 114 PPC_FEATURE2_ISEL | PPC_FEATURE2_TAR | \ 113 115 PPC_FEATURE2_VEC_CRYPTO) 114 116 #define COMMON_USER_PA6T (COMMON_USER_PPC64 | PPC_FEATURE_PA6T |\
+35
arch/powerpc/kernel/entry_64.S
··· 34 34 #include <asm/ftrace.h> 35 35 #include <asm/hw_irq.h> 36 36 #include <asm/context_tracking.h> 37 + #include <asm/tm.h> 37 38 38 39 /* 39 40 * System calls. ··· 52 51 53 52 .globl system_call_common 54 53 system_call_common: 54 + #ifdef CONFIG_PPC_TRANSACTIONAL_MEM 55 + BEGIN_FTR_SECTION 56 + extrdi. r10, r12, 1, (63-MSR_TS_T_LG) /* transaction active? */ 57 + bne tabort_syscall 58 + END_FTR_SECTION_IFSET(CPU_FTR_TM) 59 + #endif 55 60 andi. r10,r12,MSR_PR 56 61 mr r10,r1 57 62 addi r1,r1,-INT_FRAME_SIZE ··· 317 310 addi r3,r1,STACK_FRAME_OVERHEAD 318 311 bl do_syscall_trace_leave 319 312 b ret_from_except 313 + 314 + #ifdef CONFIG_PPC_TRANSACTIONAL_MEM 315 + tabort_syscall: 316 + /* Firstly we need to enable TM in the kernel */ 317 + mfmsr r10 318 + li r13, 1 319 + rldimi r10, r13, MSR_TM_LG, 63-MSR_TM_LG 320 + mtmsrd r10, 0 321 + 322 + /* tabort, this dooms the transaction, nothing else */ 323 + li r13, (TM_CAUSE_SYSCALL|TM_CAUSE_PERSISTENT) 324 + TABORT(R13) 325 + 326 + /* 327 + * Return directly to userspace. We have corrupted user register state, 328 + * but userspace will never see that register state. Execution will 329 + * resume after the tbegin of the aborted transaction with the 330 + * checkpointed register state. 331 + */ 332 + li r13, MSR_RI 333 + andc r10, r10, r13 334 + mtmsrd r10, 1 335 + mtspr SPRN_SRR0, r11 336 + mtspr SPRN_SRR1, r12 337 + 338 + rfid 339 + b . /* prevent speculative execution */ 340 + #endif 320 341 321 342 /* Save non-volatile GPRs, if not already saved. */ 322 343 _GLOBAL(save_nvgprs)
+2 -2
tools/testing/selftests/powerpc/tm/Makefile
··· 1 - TEST_PROGS := tm-resched-dscr 1 + TEST_PROGS := tm-resched-dscr tm-syscall 2 2 3 3 all: $(TEST_PROGS) 4 4 5 5 $(TEST_PROGS): ../harness.c 6 6 7 7 tm-syscall: tm-syscall-asm.S 8 - tm-syscall: CFLAGS += -mhtm 8 + tm-syscall: CFLAGS += -mhtm -I../../../../../usr/include 9 9 10 10 include ../../lib.mk 11 11
+2 -1
tools/testing/selftests/powerpc/tm/tm-syscall.c
··· 82 82 unsigned count = 0; 83 83 struct timeval end, now; 84 84 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)); 86 87 setbuf(stdout, NULL); 87 88 88 89 printf("Testing transactional syscalls for %d seconds...\n", TEST_DURATION);