d8307d
commit f0458cf4f9ff3d870c43b624e6dccaaf657d5e83
d8307d
Author: Adhemerval Zanella <adhemerval.zanella@linaro.org>
d8307d
Date:   Mon Aug 27 09:42:50 2018 -0300
d8307d
d8307d
    powerpc: Only enable TLE with PPC_FEATURE2_HTM_NOSC
d8307d
    
d8307d
    Linux from 3.9 through 4.2 does not abort HTM transaction on syscalls,
d8307d
    instead it suspend and resume it when leaving the kernel.  The
d8307d
    side-effects of the syscall will always remain visible, even if the
d8307d
    transaction is aborted.  This is an issue when transaction is used along
d8307d
    with futex syscall, on pthread_cond_wait for instance, where the futex
d8307d
    call might succeed but the transaction is rolled back leading the
d8307d
    pthread_cond object in an inconsistent state.
d8307d
    
d8307d
    Glibc used to prevent it by always aborting a transaction before issuing
d8307d
    a syscall.  Linux 4.2 also decided to abort active transaction in
d8307d
    syscalls which makes the glibc workaround superfluous.  Worse, glibc
d8307d
    transaction abortion leads to a performance issue on recent kernels
d8307d
    where the HTM state is saved/restore lazily (v4.9).  By aborting a
d8307d
    transaction on every syscalls, regardless whether a transaction has being
d8307d
    initiated before, GLIBS makes the kernel always save/restore HTM state
d8307d
    (it can not even lazily disable it after a certain number of syscall
d8307d
    iterations).
d8307d
    
d8307d
    Because of this shortcoming, Transactional Lock Elision is just enabled
d8307d
    when it has been explicitly set (either by tunables of by a configure
d8307d
    switch) and if kernel aborts HTM transactions on syscalls
d8307d
    (PPC_FEATURE2_HTM_NOSC).  It is reported that using simple benchmark [1],
d8307d
    the context-switch is about 5% faster by not issuing a tabort in every
d8307d
    syscall in newer kernels.
d8307d
    
d8307d
    Checked on powerpc64le-linux-gnu with 4.4.0 kernel (Ubuntu 16.04).
d8307d
    
d8307d
    	* NEWS: Add note about new TLE support on powerpc64le.
d8307d
    	* sysdeps/powerpc/nptl/tcb-offsets.sym (TM_CAPABLE): Remove.
d8307d
    	* sysdeps/powerpc/nptl/tls.h (tcbhead_t): Rename tm_capable to
d8307d
    	__ununsed1.
d8307d
    	(TLS_INIT_TP, TLS_DEFINE_INIT_TP): Remove tm_capable setup.
d8307d
    	(THREAD_GET_TM_CAPABLE, THREAD_SET_TM_CAPABLE): Remove macros.
d8307d
    	* sysdeps/powerpc/powerpc32/sysdep.h,
d8307d
    	sysdeps/powerpc/powerpc64/sysdep.h (ABORT_TRANSACTION_IMPL,
d8307d
    	ABORT_TRANSACTION): Remove macros.
d8307d
    	* sysdeps/powerpc/sysdep.h (ABORT_TRANSACTION): Likewise.
d8307d
    	* sysdeps/unix/sysv/linux/powerpc/elision-conf.c (elision_init): Set
d8307d
    	__pthread_force_elision iff PPC_FEATURE2_HTM_NOSC is set.
d8307d
    	* sysdeps/unix/sysv/linux/powerpc/powerpc32/sysdep.h,
d8307d
    	sysdeps/unix/sysv/linux/powerpc/powerpc64/sysdep.h
d8307d
    	sysdeps/unix/sysv/linux/powerpc/syscall.S (ABORT_TRANSACTION): Remove
d8307d
    	usage.
d8307d
    	* sysdeps/unix/sysv/linux/powerpc/not-errno.h: Remove file.
d8307d
    
d8307d
    Reported-by: Breno Leitão <leitao@debian.org>
d8307d
d8307d
diff --git a/sysdeps/powerpc/nptl/tcb-offsets.sym b/sysdeps/powerpc/nptl/tcb-offsets.sym
d8307d
index e5bb2b3..4c01615 100644
d8307d
--- a/sysdeps/powerpc/nptl/tcb-offsets.sym
d8307d
+++ b/sysdeps/powerpc/nptl/tcb-offsets.sym
d8307d
@@ -21,7 +21,6 @@ DSO_SLOT2			(offsetof (tcbhead_t, dso_slot2) - TLS_TCB_OFFSET - sizeof (tcbhead_
d8307d
 #ifdef __powerpc64__
d8307d
 TCB_AT_PLATFORM			(offsetof (tcbhead_t, at_platform) - TLS_TCB_OFFSET - sizeof(tcbhead_t))
d8307d
 #endif
d8307d
-TM_CAPABLE			(offsetof (tcbhead_t, tm_capable) - TLS_TCB_OFFSET - sizeof (tcbhead_t))
d8307d
 #ifndef __powerpc64__
d8307d
 TCB_AT_PLATFORM			(offsetof (tcbhead_t, at_platform) - TLS_TCB_OFFSET - sizeof(tcbhead_t))
d8307d
 PADDING				(offsetof (tcbhead_t, padding) - TLS_TCB_OFFSET - sizeof(tcbhead_t))
d8307d
diff --git a/sysdeps/powerpc/nptl/tls.h b/sysdeps/powerpc/nptl/tls.h
d8307d
index f88fed5..8317ca7 100644
d8307d
--- a/sysdeps/powerpc/nptl/tls.h
d8307d
+++ b/sysdeps/powerpc/nptl/tls.h
d8307d
@@ -67,8 +67,7 @@ typedef struct
d8307d
   uint32_t padding;
d8307d
   uint32_t at_platform;
d8307d
 #endif
d8307d
-  /* Indicate if HTM capable (ISA 2.07).  */
d8307d
-  uint32_t tm_capable;
d8307d
+  uint32_t __unused;
d8307d
   /* Reservation for AT_PLATFORM data - powerpc64.  */
d8307d
 #ifdef __powerpc64__
d8307d
   uint32_t at_platform;
d8307d
@@ -142,7 +141,6 @@ register void *__thread_register __asm__ ("r13");
d8307d
 # define TLS_INIT_TP(tcbp) \
d8307d
   ({ 									      \
d8307d
     __thread_register = (void *) (tcbp) + TLS_TCB_OFFSET;		      \
d8307d
-    THREAD_SET_TM_CAPABLE (__tcb_hwcap & PPC_FEATURE2_HAS_HTM ? 1 : 0);	      \
d8307d
     THREAD_SET_HWCAP (__tcb_hwcap);					      \
d8307d
     THREAD_SET_AT_PLATFORM (__tcb_platform);				      \
d8307d
     NULL;								      \
d8307d
@@ -151,8 +149,6 @@ register void *__thread_register __asm__ ("r13");
d8307d
 /* Value passed to 'clone' for initialization of the thread register.  */
d8307d
 # define TLS_DEFINE_INIT_TP(tp, pd) \
d8307d
     void *tp = (void *) (pd) + TLS_TCB_OFFSET + TLS_PRE_TCB_SIZE;	      \
d8307d
-    (((tcbhead_t *) ((char *) tp - TLS_TCB_OFFSET))[-1].tm_capable) =	      \
d8307d
-      THREAD_GET_TM_CAPABLE ();						      \
d8307d
     (((tcbhead_t *) ((char *) tp - TLS_TCB_OFFSET))[-1].hwcap) =	      \
d8307d
       THREAD_GET_HWCAP ();						      \
d8307d
     (((tcbhead_t *) ((char *) tp - TLS_TCB_OFFSET))[-1].at_platform) =	      \
d8307d
@@ -210,13 +206,6 @@ register void *__thread_register __asm__ ("r13");
d8307d
 		     + TLS_PRE_TCB_SIZE))[-1].pointer_guard		      \
d8307d
      = THREAD_GET_POINTER_GUARD())
d8307d
 
d8307d
-/* tm_capable field in TCB head.  */
d8307d
-# define THREAD_GET_TM_CAPABLE() \
d8307d
-    (((tcbhead_t *) ((char *) __thread_register				      \
d8307d
-		     - TLS_TCB_OFFSET))[-1].tm_capable)
d8307d
-# define THREAD_SET_TM_CAPABLE(value) \
d8307d
-    (THREAD_GET_TM_CAPABLE () = (value))
d8307d
-
d8307d
 /* hwcap field in TCB head.  */
d8307d
 # define THREAD_GET_HWCAP() \
d8307d
     (((tcbhead_t *) ((char *) __thread_register				      \
d8307d
diff --git a/sysdeps/powerpc/powerpc32/sysdep.h b/sysdeps/powerpc/powerpc32/sysdep.h
d8307d
index 5f1294e..93097c5 100644
d8307d
--- a/sysdeps/powerpc/powerpc32/sysdep.h
d8307d
+++ b/sysdeps/powerpc/powerpc32/sysdep.h
d8307d
@@ -90,24 +90,7 @@ GOT_LABEL:			;					      \
d8307d
   cfi_endproc;								      \
d8307d
   ASM_SIZE_DIRECTIVE(name)
d8307d
 
d8307d
-#if !IS_IN(rtld) && !defined(__SPE__)
d8307d
-# define ABORT_TRANSACTION_IMPL \
d8307d
-    cmpwi    2,0;		\
d8307d
-    beq      1f;		\
d8307d
-    lwz      0,TM_CAPABLE(2);	\
d8307d
-    cmpwi    0,0;		\
d8307d
-    beq	     1f;		\
d8307d
-    li       11,_ABORT_SYSCALL;	\
d8307d
-    tabort.  11;		\
d8307d
-    .align 4;			\
d8307d
-1:
d8307d
-#else
d8307d
-# define ABORT_TRANSACTION_IMPL
d8307d
-#endif
d8307d
-#define ABORT_TRANSACTION ABORT_TRANSACTION_IMPL
d8307d
-
d8307d
 #define DO_CALL(syscall)						      \
d8307d
-    ABORT_TRANSACTION							      \
d8307d
     li 0,syscall;							      \
d8307d
     sc
d8307d
 
d8307d
diff --git a/sysdeps/powerpc/powerpc64/sysdep.h b/sysdeps/powerpc/powerpc64/sysdep.h
d8307d
index 2df1d9b..50e64f9 100644
d8307d
--- a/sysdeps/powerpc/powerpc64/sysdep.h
d8307d
+++ b/sysdeps/powerpc/powerpc64/sysdep.h
d8307d
@@ -263,24 +263,7 @@ LT_LABELSUFFIX(name,_name_end): ; \
d8307d
   TRACEBACK_MASK(name,mask);	\
d8307d
   END_2(name)
d8307d
 
d8307d
-#if !IS_IN(rtld)
d8307d
-# define ABORT_TRANSACTION_IMPL \
d8307d
-    cmpdi    13,0;		\
d8307d
-    beq      1f;		\
d8307d
-    lwz      0,TM_CAPABLE(13);	\
d8307d
-    cmpwi    0,0;		\
d8307d
-    beq	     1f;		\
d8307d
-    li       11,_ABORT_SYSCALL;	\
d8307d
-    tabort.  11;		\
d8307d
-    .p2align 4;			\
d8307d
-1:
d8307d
-#else
d8307d
-# define ABORT_TRANSACTION_IMPL
d8307d
-#endif
d8307d
-#define ABORT_TRANSACTION ABORT_TRANSACTION_IMPL
d8307d
-
d8307d
 #define DO_CALL(syscall) \
d8307d
-    ABORT_TRANSACTION \
d8307d
     li 0,syscall; \
d8307d
     sc
d8307d
 
d8307d
diff --git a/sysdeps/powerpc/sysdep.h b/sysdeps/powerpc/sysdep.h
d8307d
index 8a6d236..c8bf25e 100644
d8307d
--- a/sysdeps/powerpc/sysdep.h
d8307d
+++ b/sysdeps/powerpc/sysdep.h
d8307d
@@ -21,8 +21,6 @@
d8307d
  */
d8307d
 #define _SYSDEPS_SYSDEP_H 1
d8307d
 #include <bits/hwcap.h>
d8307d
-#include <tls.h>
d8307d
-#include <htm.h>
d8307d
 
d8307d
 #define PPC_FEATURE_970 (PPC_FEATURE_POWER4 + PPC_FEATURE_HAS_ALTIVEC)
d8307d
 
d8307d
@@ -166,22 +164,4 @@
d8307d
 #define ALIGNARG(log2) log2
d8307d
 #define ASM_SIZE_DIRECTIVE(name) .size name,.-name
d8307d
 
d8307d
-#else
d8307d
-
d8307d
-/* Linux kernel powerpc documentation [1] states issuing a syscall inside a
d8307d
-   transaction is not recommended and may lead to undefined behavior.  It
d8307d
-   also states syscalls do not abort transactions.  To avoid such traps,
d8307d
-   we abort transaction just before syscalls.
d8307d
-
d8307d
-   [1] Documentation/powerpc/transactional_memory.txt [Syscalls]  */
d8307d
-#if !IS_IN(rtld) && !defined(__SPE__)
d8307d
-# define ABORT_TRANSACTION \
d8307d
-  ({ 						\
d8307d
-    if (THREAD_GET_TM_CAPABLE ())		\
d8307d
-      __libc_tabort (_ABORT_SYSCALL);	\
d8307d
-  })
d8307d
-#else
d8307d
-# define ABORT_TRANSACTION
d8307d
-#endif
d8307d
-
d8307d
 #endif	/* __ASSEMBLER__ */
d8307d
diff --git a/sysdeps/unix/sysv/linux/powerpc/elision-conf.c b/sysdeps/unix/sysv/linux/powerpc/elision-conf.c
d8307d
index 906882a..fc82bd1 100644
d8307d
--- a/sysdeps/unix/sysv/linux/powerpc/elision-conf.c
d8307d
+++ b/sysdeps/unix/sysv/linux/powerpc/elision-conf.c
d8307d
@@ -127,6 +127,26 @@ elision_init (int argc __attribute__ ((unused)),
d8307d
 	       TUNABLE_CALLBACK (set_elision_skip_trylock_internal_abort));
d8307d
 #endif
d8307d
 
d8307d
+  /* Linux from 3.9 through 4.2 do not abort HTM transaction on syscalls,
d8307d
+     instead it suspends the transaction and resumes it when returning to
d8307d
+     usercode.  The side-effects of the syscall will always remain visible,
d8307d
+     even if the transaction is aborted.  This is an issue when a transaction
d8307d
+     is used along with futex syscall, on pthread_cond_wait for instance,
d8307d
+     where futex might succeed but the transaction is rolled back leading
d8307d
+     the condition variable object in an inconsistent state.
d8307d
+
d8307d
+     Glibc used to prevent it by always aborting a transaction before issuing
d8307d
+     a syscall.  Linux 4.2 also decided to abort active transaction in
d8307d
+     syscalls which makes the glibc workaround superflours.  Worse, glibc
d8307d
+     transaction abortions leads to a performance issues on recent kernels.
d8307d
+
d8307d
+     So Lock Elision is just enabled when it has been explict set (either
d8307d
+     by tunables of by a configure switch) and if kernel aborts HTM
d8307d
+     transactions on syscalls (PPC_FEATURE2_HTM_NOSC)  */
d8307d
+
d8307d
+  __pthread_force_elision = (__pthread_force_elision
d8307d
+			     && GLRO (dl_hwcap2) & PPC_FEATURE2_HTM_NOSC);
d8307d
+
d8307d
   if (!__pthread_force_elision)
d8307d
     __elision_aconf.try_tbegin = 0; /* Disable elision on rwlocks.  */
d8307d
 }
d8307d
diff --git a/sysdeps/unix/sysv/linux/powerpc/not-errno.h b/sysdeps/unix/sysv/linux/powerpc/not-errno.h
d8307d
deleted file mode 100644
d8307d
index 27da21b..0000000
d8307d
--- a/sysdeps/unix/sysv/linux/powerpc/not-errno.h
d8307d
+++ /dev/null
d8307d
@@ -1,30 +0,0 @@
d8307d
-/* Syscall wrapper that do not set errno.  Linux powerpc version.
d8307d
-   Copyright (C) 2018 Free Software Foundation, Inc.
d8307d
-   This file is part of the GNU C Library.
d8307d
-
d8307d
-   The GNU C Library is free software; you can redistribute it and/or
d8307d
-   modify it under the terms of the GNU Lesser General Public
d8307d
-   License as published by the Free Software Foundation; either
d8307d
-   version 2.1 of the License, or (at your option) any later version.
d8307d
-
d8307d
-   The GNU C Library is distributed in the hope that it will be useful,
d8307d
-   but WITHOUT ANY WARRANTY; without even the implied warranty of
d8307d
-   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
d8307d
-   Lesser General Public License for more details.
d8307d
-
d8307d
-   You should have received a copy of the GNU Lesser General Public
d8307d
-   License along with the GNU C Library; if not, see
d8307d
-   <http://www.gnu.org/licenses/>.  */
d8307d
-
d8307d
-/* __access_noerrno is used during process initialization in elf/dl-tunables.c
d8307d
-   before the TCB is initialized, prohibiting the usage of
d8307d
-   ABORT_TRANSACTION.  */
d8307d
-#undef ABORT_TRANSACTION
d8307d
-#define ABORT_TRANSACTION
d8307d
-
d8307d
-#include "sysdeps/unix/sysv/linux/not-errno.h"
d8307d
-
d8307d
-/* Recover ABORT_TRANSACTION's previous value, in order to not affect
d8307d
-   other syscalls.  */
d8307d
-#undef ABORT_TRANSACTION
d8307d
-#define ABORT_TRANSACTION ABORT_TRANSACTION_IMPL
d8307d
diff --git a/sysdeps/unix/sysv/linux/powerpc/powerpc32/sysdep.h b/sysdeps/unix/sysv/linux/powerpc/powerpc32/sysdep.h
d8307d
index f7277d5..ec5c525 100644
d8307d
--- a/sysdeps/unix/sysv/linux/powerpc/powerpc32/sysdep.h
d8307d
+++ b/sysdeps/unix/sysv/linux/powerpc/powerpc32/sysdep.h
d8307d
@@ -109,7 +109,6 @@
d8307d
     register long int r11 __asm__ ("r11");				\
d8307d
     register long int r12 __asm__ ("r12");				\
d8307d
     LOADARGS_##nr(name, args);						\
d8307d
-    ABORT_TRANSACTION;							\
d8307d
     __asm__ __volatile__						\
d8307d
       ("sc   \n\t"							\
d8307d
        "mfcr %0"							\
d8307d
diff --git a/sysdeps/unix/sysv/linux/powerpc/powerpc64/sysdep.h b/sysdeps/unix/sysv/linux/powerpc/powerpc64/sysdep.h
d8307d
index 0956cf0..1f17f7b 100644
d8307d
--- a/sysdeps/unix/sysv/linux/powerpc/powerpc64/sysdep.h
d8307d
+++ b/sysdeps/unix/sysv/linux/powerpc/powerpc64/sysdep.h
d8307d
@@ -131,7 +131,6 @@
d8307d
     register long int r7  __asm__ ("r7");				\
d8307d
     register long int r8  __asm__ ("r8");				\
d8307d
     LOADARGS_##nr (name, ##args);					\
d8307d
-    ABORT_TRANSACTION;							\
d8307d
     __asm__ __volatile__						\
d8307d
       ("sc\n\t"								\
d8307d
        "mfcr  %0\n\t"							\
d8307d
diff --git a/sysdeps/unix/sysv/linux/powerpc/syscall.S b/sysdeps/unix/sysv/linux/powerpc/syscall.S
d8307d
index 2da9172..bbab613 100644
d8307d
--- a/sysdeps/unix/sysv/linux/powerpc/syscall.S
d8307d
+++ b/sysdeps/unix/sysv/linux/powerpc/syscall.S
d8307d
@@ -18,7 +18,6 @@
d8307d
 #include <sysdep.h>
d8307d
 
d8307d
 ENTRY (syscall)
d8307d
-	ABORT_TRANSACTION
d8307d
 	mr   r0,r3
d8307d
 	mr   r3,r4
d8307d
 	mr   r4,r5