ce426f
In RHEL7 we already have the newer cpu-feature support so we need
ce426f
to backport b376899d2 to get 1cdbe5794 to compile. The goal wtih elision
ce426f
is an incremental set of patches each which compile and introduce the
ce426f
required functionality for elision.
ce426f
ce426f
Partial backport of:
ce426f
ce426f
commit b376899d27e5ac892f0339cf1bbb3d2158347db8
ce426f
Author: H.J. Lu <hjl.tools@gmail.com>
ce426f
Date:   Thu Aug 13 03:40:40 2015 -0700
ce426f
ce426f
    Update x86 elision-conf.c for <cpu-features.h>
ce426f
    
ce426f
    This patch updates x86 elision-conf.c to use the newly defined
ce426f
    HAS_CPU_FEATURE from <cpu-features.h>.
ce426f
    
ce426f
            * sysdeps/unix/sysv/linux/x86/elision-conf.c (elision_init):
ce426f
            Replace HAS_RTM with HAS_CPU_FEATURE (RTM).
ce426f
ce426f
ce426f
Full backport of:
ce426f
ce426f
commit 1717da59aed9612becd56aaa1249aac695af4c8a
ce426f
Author: Andi Kleen <ak@linux.intel.com>
ce426f
Date:   Thu May 16 19:17:14 2013 -0700
ce426f
ce426f
    Add a configure option to enable lock elision and disable by default
ce426f
    
ce426f
    Can be enabled with --enable-lock-elision=yes at configure time.
ce426f
ce426f
commit 1cdbe579482c07e9f4bb3baa4864da2d3e7eb837
ce426f
Author: Andi Kleen <ak@linux.intel.com>
ce426f
Date:   Sat Nov 10 00:51:26 2012 -0800
ce426f
ce426f
    Add the low level infrastructure for pthreads lock elision with TSX
ce426f
    
ce426f
    Lock elision using TSX is a technique to optimize lock scaling
ce426f
    It allows to run locks in parallel using hardware support for
ce426f
    a transactional execution mode in 4th generation Intel Core CPUs.
ce426f
    See http://www.intel.com/software/tsx for more Information.
ce426f
    
ce426f
    This patch implements a simple adaptive lock elision algorithm based
ce426f
    on RTM. It enables elision for the pthread mutexes and rwlocks.
ce426f
    The algorithm keeps track whether a mutex successfully elides or not,
ce426f
    and stops eliding for some time when it is not.
ce426f
    
ce426f
    When the CPU supports RTM the elision path is automatically tried,
ce426f
    otherwise any elision is disabled.
ce426f
    
ce426f
    The adaptation algorithm and its tuning is currently preliminary.
ce426f
    
ce426f
    The code adds some checks to the lock fast paths. Micro-benchmarks
ce426f
    show little to no difference without RTM.
ce426f
    
ce426f
    This patch implements the low level "lll_" code for lock elision.
ce426f
    Followon patches hook this into the pthread implementation
ce426f
    
ce426f
    Changes with the RTM mutexes:
ce426f
    -----------------------------
ce426f
    Lock elision in pthreads is generally compatible with existing programs.
ce426f
    There are some obscure exceptions, which are expected to be uncommon.
ce426f
    See the manual for more details.
ce426f
    
ce426f
    - A broken program that unlocks a free lock will crash.
ce426f
      There are ways around this with some tradeoffs (more code in hot paths)
ce426f
      I'm still undecided on what approach to take here; have to wait for testing reports.
ce426f
    - pthread_mutex_destroy of a lock mutex will not return EBUSY but 0.
ce426f
    - There's also a similar situation with trylock outside the mutex,
ce426f
      "knowing" that the mutex must be held due to some other condition.
ce426f
      In this case an assert failure cannot be recovered. This situation is
ce426f
      usually an existing bug in the program.
ce426f
    - Same applies to the rwlocks. Some of the return values changes
ce426f
      (for example there is no EDEADLK for an elided lock, unless it aborts.
ce426f
       However when elided it will also never deadlock of course)
ce426f
    - Timing changes, so broken programs that make assumptions about specific timing
ce426f
      may expose already existing latent problems.  Note that these broken programs will
ce426f
      break in other situations too (loaded system, new faster hardware, compiler
ce426f
      optimizations etc.)
ce426f
    - Programs with non recursive mutexes that take them recursively in a thread and
ce426f
      which would always deadlock without elision may not always see a deadlock.
ce426f
      The deadlock will only happen on an early or delayed abort (which typically
ce426f
      happens at some point)
ce426f
      This only happens for mutexes not explicitely set to PTHREAD_MUTEX_NORMAL
ce426f
      or PTHREAD_MUTEX_ADAPTIVE_NP.  PTHREAD_MUTEX_NORMAL mutexes do not elide.
ce426f
    
ce426f
    The elision default can be set at configure time.
ce426f
    
ce426f
    This patch implements the basic infrastructure for elision.
ce426f
Index: glibc-2.17-c758a686/nptl/elision-conf.h
ce426f
===================================================================
ce426f
--- /dev/null
ce426f
+++ glibc-2.17-c758a686/nptl/elision-conf.h
ce426f
@@ -0,0 +1 @@
ce426f
+/* empty */
ce426f
Index: glibc-2.17-c758a686/nptl/sysdeps/unix/sysv/linux/i386/lowlevellock.h
ce426f
===================================================================
ce426f
--- glibc-2.17-c758a686.orig/nptl/sysdeps/unix/sysv/linux/i386/lowlevellock.h
ce426f
+++ glibc-2.17-c758a686/nptl/sysdeps/unix/sysv/linux/i386/lowlevellock.h
ce426f
@@ -430,6 +430,12 @@ LLL_STUB_UNWIND_INFO_END
ce426f
 		       : "memory");					      \
ce426f
      result; })
ce426f
 
ce426f
+extern int __lll_timedlock_elision (int *futex, short *adapt_count,
ce426f
+					 const struct timespec *timeout,
ce426f
+					 int private) attribute_hidden;
ce426f
+
ce426f
+#define lll_timedlock_elision(futex, adapt_count, timeout, private)	\
ce426f
+  __lll_timedlock_elision(&(futex), &(adapt_count), timeout, private)
ce426f
 
ce426f
 #define lll_robust_timedlock(futex, timeout, id, private) \
ce426f
   ({ int result, ignore1, ignore2, ignore3;				      \
ce426f
@@ -583,6 +589,22 @@ extern int __lll_timedwait_tid (int *tid
ce426f
       }									      \
ce426f
     __result; })
ce426f
 
ce426f
+extern int __lll_lock_elision (int *futex, short *adapt_count, int private)
ce426f
+  attribute_hidden;
ce426f
+
ce426f
+extern int __lll_unlock_elision(int *lock, int private)
ce426f
+  attribute_hidden;
ce426f
+
ce426f
+extern int __lll_trylock_elision(int *lock, short *adapt_count)
ce426f
+  attribute_hidden;
ce426f
+
ce426f
+#define lll_lock_elision(futex, adapt_count, private) \
ce426f
+  __lll_lock_elision (&(futex), &(adapt_count), private)
ce426f
+#define lll_unlock_elision(futex, private) \
ce426f
+  __lll_unlock_elision (&(futex), private)
ce426f
+#define lll_trylock_elision(futex, adapt_count) \
ce426f
+  __lll_trylock_elision(&(futex), &(adapt_count))
ce426f
+
ce426f
 #endif  /* !__ASSEMBLER__ */
ce426f
 
ce426f
 #endif	/* lowlevellock.h */
ce426f
Index: glibc-2.17-c758a686/nptl/sysdeps/unix/sysv/linux/x86/Makefile
ce426f
===================================================================
ce426f
--- /dev/null
ce426f
+++ glibc-2.17-c758a686/nptl/sysdeps/unix/sysv/linux/x86/Makefile
ce426f
@@ -0,0 +1,2 @@
ce426f
+libpthread-sysdep_routines += elision-lock elision-unlock elision-timed \
ce426f
+			      elision-trylock
ce426f
Index: glibc-2.17-c758a686/nptl/sysdeps/unix/sysv/linux/x86/elision-conf.c
ce426f
===================================================================
ce426f
--- /dev/null
ce426f
+++ glibc-2.17-c758a686/nptl/sysdeps/unix/sysv/linux/x86/elision-conf.c
ce426f
@@ -0,0 +1,90 @@
ce426f
+/* elision-conf.c: Lock elision tunable parameters.
ce426f
+   Copyright (C) 2013 Free Software Foundation, Inc.
ce426f
+   This file is part of the GNU C Library.
ce426f
+
ce426f
+   The GNU C Library is free software; you can redistribute it and/or
ce426f
+   modify it under the terms of the GNU Lesser General Public
ce426f
+   License as published by the Free Software Foundation; either
ce426f
+   version 2.1 of the License, or (at your option) any later version.
ce426f
+
ce426f
+   The GNU C Library is distributed in the hope that it will be useful,
ce426f
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
ce426f
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
ce426f
+   Lesser General Public License for more details.
ce426f
+
ce426f
+   You should have received a copy of the GNU Lesser General Public
ce426f
+   License along with the GNU C Library; if not, see
ce426f
+   <http://www.gnu.org/licenses/>. */
ce426f
+
ce426f
+#include "config.h"
ce426f
+#include <pthreadP.h>
ce426f
+#include <init-arch.h>
ce426f
+#include <elision-conf.h>
ce426f
+#include <unistd.h>
ce426f
+
ce426f
+/* Reasonable initial tuning values, may be revised in the future.
ce426f
+   This is a conservative initial value.  */
ce426f
+
ce426f
+struct elision_config __elision_aconf =
ce426f
+  {
ce426f
+    /* How often to not attempt to use elision if a transaction aborted
ce426f
+       because the lock is already acquired.  Expressed in number of lock
ce426f
+       acquisition attempts.  */
ce426f
+    .skip_lock_busy = 3,
ce426f
+    /* How often to not attempt to use elision if a transaction aborted due
ce426f
+       to reasons other than other threads' memory accesses. Expressed in
ce426f
+       number of lock acquisition attempts.  */
ce426f
+    .skip_lock_internal_abort = 3,
ce426f
+    /* How often we retry using elision if there is chance for the transaction
ce426f
+       to finish execution (e.g., it wasn't aborted due to the lock being
ce426f
+       already acquired.  */
ce426f
+    .retry_try_xbegin = 3,
ce426f
+    /* Same as SKIP_LOCK_INTERNAL_ABORT but for trylock.  */
ce426f
+    .skip_trylock_internal_abort = 3,
ce426f
+  };
ce426f
+
ce426f
+/* Elided rwlock toggle, set when elision is available and is
ce426f
+   enabled for rwlocks.  */
ce426f
+
ce426f
+int __rwlock_rtm_enabled attribute_hidden;
ce426f
+
ce426f
+/* Retries for elided rwlocks on read. Conservative initial value.  */
ce426f
+
ce426f
+int __rwlock_rtm_read_retries attribute_hidden = 3;
ce426f
+
ce426f
+/* Set when the CPU supports elision. When false elision is never attempted.  */
ce426f
+
ce426f
+int __elision_available attribute_hidden;
ce426f
+
ce426f
+/* Force elision for all new locks. This is used to decide whether existing
ce426f
+   DEFAULT locks should be automatically upgraded to elision in
ce426f
+   pthread_mutex_lock(). Disabled for suid programs. Only used when elision
ce426f
+   is available.  */
ce426f
+
ce426f
+int __pthread_force_elision attribute_hidden;
ce426f
+
ce426f
+/* Initialize elison.  */
ce426f
+
ce426f
+static void
ce426f
+elision_init (int argc __attribute__ ((unused)),
ce426f
+	      char **argv  __attribute__ ((unused)),
ce426f
+	      char **environ)
ce426f
+{
ce426f
+  __elision_available = HAS_CPU_FEATURE (RTM);
ce426f
+#ifdef ENABLE_LOCK_ELISION
ce426f
+  __pthread_force_elision = __libc_enable_secure ? 0 : __elision_available;
ce426f
+  __rwlock_rtm_enabled = __libc_enable_secure ? 0 : __elision_available;
ce426f
+#endif
ce426f
+}
ce426f
+
ce426f
+#ifdef SHARED
ce426f
+# define INIT_SECTION ".init_array"
ce426f
+#else
ce426f
+# define INIT_SECTION ".preinit_array"
ce426f
+#endif
ce426f
+
ce426f
+void (*const __pthread_init_array []) (int, char **, char **)
ce426f
+  __attribute__ ((section (INIT_SECTION), aligned (sizeof (void *)))) =
ce426f
+{
ce426f
+  &elision_init
ce426f
+};
ce426f
Index: glibc-2.17-c758a686/nptl/sysdeps/unix/sysv/linux/x86/elision-conf.h
ce426f
===================================================================
ce426f
--- /dev/null
ce426f
+++ glibc-2.17-c758a686/nptl/sysdeps/unix/sysv/linux/x86/elision-conf.h
ce426f
@@ -0,0 +1,44 @@
ce426f
+/* elision-conf.h: Lock elision tunable parameters.
ce426f
+   Copyright (C) 2013 Free Software Foundation, Inc.
ce426f
+   This file is part of the GNU C Library.
ce426f
+
ce426f
+   The GNU C Library is free software; you can redistribute it and/or
ce426f
+   modify it under the terms of the GNU Lesser General Public
ce426f
+   License as published by the Free Software Foundation; either
ce426f
+   version 2.1 of the License, or (at your option) any later version.
ce426f
+
ce426f
+   The GNU C Library is distributed in the hope that it will be useful,
ce426f
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
ce426f
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
ce426f
+   Lesser General Public License for more details.
ce426f
+
ce426f
+   You should have received a copy of the GNU Lesser General Public
ce426f
+   License along with the GNU C Library; if not, see
ce426f
+   <http://www.gnu.org/licenses/>. */
ce426f
+#ifndef _ELISION_CONF_H
ce426f
+#define _ELISION_CONF_H 1
ce426f
+
ce426f
+#include <pthread.h>
ce426f
+#include <cpuid.h>
ce426f
+#include <time.h>
ce426f
+
ce426f
+/* Should make sure there is no false sharing on this.  */
ce426f
+
ce426f
+struct elision_config
ce426f
+{
ce426f
+  int skip_lock_busy;
ce426f
+  int skip_lock_internal_abort;
ce426f
+  int retry_try_xbegin;
ce426f
+  int skip_trylock_internal_abort;
ce426f
+};
ce426f
+
ce426f
+extern struct elision_config __elision_aconf attribute_hidden;
ce426f
+
ce426f
+extern int __rwlock_rtm_enabled attribute_hidden;
ce426f
+extern int __elision_available attribute_hidden;
ce426f
+extern int __pthread_force_elision attribute_hidden;
ce426f
+
ce426f
+/* Tell the test suite to test elision for this architecture.  */
ce426f
+#define HAVE_ELISION 1
ce426f
+
ce426f
+#endif
ce426f
Index: glibc-2.17-c758a686/nptl/sysdeps/unix/sysv/linux/x86/elision-lock.c
ce426f
===================================================================
ce426f
--- /dev/null
ce426f
+++ glibc-2.17-c758a686/nptl/sysdeps/unix/sysv/linux/x86/elision-lock.c
ce426f
@@ -0,0 +1,95 @@
ce426f
+/* elision-lock.c: Elided pthread mutex lock.
ce426f
+   Copyright (C) 2011-2013 Free Software Foundation, Inc.
ce426f
+   This file is part of the GNU C Library.
ce426f
+
ce426f
+   The GNU C Library is free software; you can redistribute it and/or
ce426f
+   modify it under the terms of the GNU Lesser General Public
ce426f
+   License as published by the Free Software Foundation; either
ce426f
+   version 2.1 of the License, or (at your option) any later version.
ce426f
+
ce426f
+   The GNU C Library is distributed in the hope that it will be useful,
ce426f
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
ce426f
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
ce426f
+   Lesser General Public License for more details.
ce426f
+
ce426f
+   You should have received a copy of the GNU Lesser General Public
ce426f
+   License along with the GNU C Library; if not, see
ce426f
+   <http://www.gnu.org/licenses/>. */
ce426f
+
ce426f
+#include <pthread.h>
ce426f
+#include "pthreadP.h"
ce426f
+#include "lowlevellock.h"
ce426f
+#include "hle.h"
ce426f
+#include <elision-conf.h>
ce426f
+
ce426f
+#if !defined(LLL_LOCK) && !defined(EXTRAARG)
ce426f
+/* Make sure the configuration code is always linked in for static
ce426f
+   libraries.  */
ce426f
+#include "elision-conf.c"
ce426f
+#endif
ce426f
+
ce426f
+#ifndef EXTRAARG
ce426f
+#define EXTRAARG
ce426f
+#endif
ce426f
+#ifndef LLL_LOCK
ce426f
+#define LLL_LOCK(a,b) lll_lock(a,b), 0
ce426f
+#endif
ce426f
+
ce426f
+#define aconf __elision_aconf
ce426f
+
ce426f
+/* Adaptive lock using transactions.
ce426f
+   By default the lock region is run as a transaction, and when it
ce426f
+   aborts or the lock is busy the lock adapts itself.  */
ce426f
+
ce426f
+int
ce426f
+__lll_lock_elision (int *futex, short *adapt_count, EXTRAARG int private)
ce426f
+{
ce426f
+  if (*adapt_count <= 0)
ce426f
+    {
ce426f
+      unsigned status;
ce426f
+      int try_xbegin;
ce426f
+
ce426f
+      for (try_xbegin = aconf.retry_try_xbegin;
ce426f
+	   try_xbegin > 0;
ce426f
+	   try_xbegin--)
ce426f
+	{
ce426f
+	  if ((status = _xbegin()) == _XBEGIN_STARTED)
ce426f
+	    {
ce426f
+	      if (*futex == 0)
ce426f
+		return 0;
ce426f
+
ce426f
+	      /* Lock was busy. Fall back to normal locking.
ce426f
+		 Could also _xend here but xabort with 0xff code
ce426f
+		 is more visible in the profiler.  */
ce426f
+	      _xabort (_ABORT_LOCK_BUSY);
ce426f
+	    }
ce426f
+
ce426f
+	  if (!(status & _XABORT_RETRY))
ce426f
+	    {
ce426f
+	      if ((status & _XABORT_EXPLICIT)
ce426f
+			&& _XABORT_CODE (status) == _ABORT_LOCK_BUSY)
ce426f
+	        {
ce426f
+		  /* Right now we skip here. Better would be to wait a bit
ce426f
+		     and retry. This likely needs some spinning.  */
ce426f
+		  if (*adapt_count != aconf.skip_lock_busy)
ce426f
+		    *adapt_count = aconf.skip_lock_busy;
ce426f
+		}
ce426f
+	      /* Internal abort. There is no chance for retry.
ce426f
+		 Use the normal locking and next time use lock.
ce426f
+		 Be careful to avoid writing to the lock.  */
ce426f
+	      else if (*adapt_count != aconf.skip_lock_internal_abort)
ce426f
+		*adapt_count = aconf.skip_lock_internal_abort;
ce426f
+	      break;
ce426f
+	    }
ce426f
+	}
ce426f
+    }
ce426f
+  else
ce426f
+    {
ce426f
+      /* Use a normal lock until the threshold counter runs out.
ce426f
+	 Lost updates possible.  */
ce426f
+      (*adapt_count)--;
ce426f
+    }
ce426f
+
ce426f
+  /* Use a normal lock here.  */
ce426f
+  return LLL_LOCK ((*futex), private);
ce426f
+}
ce426f
Index: glibc-2.17-c758a686/nptl/sysdeps/unix/sysv/linux/x86/elision-timed.c
ce426f
===================================================================
ce426f
--- /dev/null
ce426f
+++ glibc-2.17-c758a686/nptl/sysdeps/unix/sysv/linux/x86/elision-timed.c
ce426f
@@ -0,0 +1,26 @@
ce426f
+/* elision-timed.c: Lock elision timed lock.
ce426f
+   Copyright (C) 2013 Free Software Foundation, Inc.
ce426f
+   This file is part of the GNU C Library.
ce426f
+
ce426f
+   The GNU C Library is free software; you can redistribute it and/or
ce426f
+   modify it under the terms of the GNU Lesser General Public
ce426f
+   License as published by the Free Software Foundation; either
ce426f
+   version 2.1 of the License, or (at your option) any later version.
ce426f
+
ce426f
+   The GNU C Library is distributed in the hope that it will be useful,
ce426f
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
ce426f
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
ce426f
+   Lesser General Public License for more details.
ce426f
+
ce426f
+   You should have received a copy of the GNU Lesser General Public
ce426f
+   License along with the GNU C Library; if not, see
ce426f
+   <http://www.gnu.org/licenses/>. */
ce426f
+
ce426f
+#include <time.h>
ce426f
+#include <elision-conf.h>
ce426f
+#include "lowlevellock.h"
ce426f
+#define __lll_lock_elision __lll_timedlock_elision
ce426f
+#define EXTRAARG const struct timespec *t,
ce426f
+#undef LLL_LOCK
ce426f
+#define LLL_LOCK(a, b) lll_timedlock(a, t, b)
ce426f
+#include "elision-lock.c"
ce426f
Index: glibc-2.17-c758a686/nptl/sysdeps/unix/sysv/linux/x86/elision-trylock.c
ce426f
===================================================================
ce426f
--- /dev/null
ce426f
+++ glibc-2.17-c758a686/nptl/sysdeps/unix/sysv/linux/x86/elision-trylock.c
ce426f
@@ -0,0 +1,72 @@
ce426f
+/* elision-trylock.c: Lock eliding trylock for pthreads.
ce426f
+   Copyright (C) 2013 Free Software Foundation, Inc.
ce426f
+   This file is part of the GNU C Library.
ce426f
+
ce426f
+   The GNU C Library is free software; you can redistribute it and/or
ce426f
+   modify it under the terms of the GNU Lesser General Public
ce426f
+   License as published by the Free Software Foundation; either
ce426f
+   version 2.1 of the License, or (at your option) any later version.
ce426f
+
ce426f
+   The GNU C Library is distributed in the hope that it will be useful,
ce426f
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
ce426f
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
ce426f
+   Lesser General Public License for more details.
ce426f
+
ce426f
+   You should have received a copy of the GNU Lesser General Public
ce426f
+   License along with the GNU C Library; if not, see
ce426f
+   <http://www.gnu.org/licenses/>. */
ce426f
+
ce426f
+#include <pthread.h>
ce426f
+#include <pthreadP.h>
ce426f
+#include <lowlevellock.h>
ce426f
+#include "hle.h"
ce426f
+#include <elision-conf.h>
ce426f
+
ce426f
+#define aconf __elision_aconf
ce426f
+
ce426f
+/* Try to elide a futex trylock. FUTEX is the futex variable. TRY_LOCK is the
ce426f
+   adaptation counter in the mutex. UPGRADED is != 0 when this is for an
ce426f
+   automatically upgraded lock.  */
ce426f
+
ce426f
+int
ce426f
+__lll_trylock_elision (int *futex, short *adapt_count)
ce426f
+{
ce426f
+  /* Implement POSIX semantics by forbiding nesting
ce426f
+     trylock. Sorry. After the abort the code is re-executed
ce426f
+     non transactional and if the lock was already locked
ce426f
+     return an error.  */
ce426f
+  _xabort (_ABORT_NESTED_TRYLOCK);
ce426f
+
ce426f
+  /* Only try a transaction if it's worth it.  */
ce426f
+  if (*adapt_count <= 0)
ce426f
+    {
ce426f
+      unsigned status;
ce426f
+
ce426f
+      if ((status = _xbegin()) == _XBEGIN_STARTED)
ce426f
+	{
ce426f
+	  if (*futex == 0)
ce426f
+	    return 0;
ce426f
+
ce426f
+	  /* Lock was busy. Fall back to normal locking.
ce426f
+	     Could also _xend here but xabort with 0xff code
ce426f
+	     is more visible in the profiler.  */
ce426f
+	  _xabort (_ABORT_LOCK_BUSY);
ce426f
+	}
ce426f
+
ce426f
+      if (!(status & _XABORT_RETRY))
ce426f
+        {
ce426f
+          /* Internal abort. No chance for retry. For future
ce426f
+             locks don't try speculation for some time.  */
ce426f
+          if (*adapt_count != aconf.skip_trylock_internal_abort)
ce426f
+            *adapt_count = aconf.skip_trylock_internal_abort;
ce426f
+        }
ce426f
+      /* Could do some retries here. */
ce426f
+    }
ce426f
+  else
ce426f
+    {
ce426f
+      /* Lost updates are possible, but harmless.  */
ce426f
+      (*adapt_count)--;
ce426f
+    }
ce426f
+
ce426f
+  return lll_trylock (*futex);
ce426f
+}
ce426f
Index: glibc-2.17-c758a686/nptl/sysdeps/unix/sysv/linux/x86/elision-unlock.c
ce426f
===================================================================
ce426f
--- /dev/null
ce426f
+++ glibc-2.17-c758a686/nptl/sysdeps/unix/sysv/linux/x86/elision-unlock.c
ce426f
@@ -0,0 +1,33 @@
ce426f
+/* elision-unlock.c: Commit an elided pthread lock.
ce426f
+   Copyright (C) 2013 Free Software Foundation, Inc.
ce426f
+   This file is part of the GNU C Library.
ce426f
+
ce426f
+   The GNU C Library is free software; you can redistribute it and/or
ce426f
+   modify it under the terms of the GNU Lesser General Public
ce426f
+   License as published by the Free Software Foundation; either
ce426f
+   version 2.1 of the License, or (at your option) any later version.
ce426f
+
ce426f
+   The GNU C Library is distributed in the hope that it will be useful,
ce426f
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
ce426f
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
ce426f
+   Lesser General Public License for more details.
ce426f
+
ce426f
+   You should have received a copy of the GNU Lesser General Public
ce426f
+   License along with the GNU C Library; if not, see
ce426f
+   <http://www.gnu.org/licenses/>.  */
ce426f
+
ce426f
+#include "pthreadP.h"
ce426f
+#include "lowlevellock.h"
ce426f
+#include "hle.h"
ce426f
+
ce426f
+int
ce426f
+__lll_unlock_elision(int *lock, int private)
ce426f
+{
ce426f
+  /* When the lock was free we're in a transaction.
ce426f
+     When you crash here you unlocked a free lock.  */
ce426f
+  if (*lock == 0)
ce426f
+    _xend();
ce426f
+  else
ce426f
+    lll_unlock ((*lock), private);
ce426f
+  return 0;
ce426f
+}
ce426f
Index: glibc-2.17-c758a686/nptl/sysdeps/unix/sysv/linux/x86/hle.h
ce426f
===================================================================
ce426f
--- /dev/null
ce426f
+++ glibc-2.17-c758a686/nptl/sysdeps/unix/sysv/linux/x86/hle.h
ce426f
@@ -0,0 +1,75 @@
ce426f
+/* Shared RTM header. Emulate TSX intrinsics for compilers and assemblers
ce426f
+   that do not support the intrinsics and instructions yet. */
ce426f
+#ifndef _HLE_H
ce426f
+#define _HLE_H 1
ce426f
+
ce426f
+#ifdef __ASSEMBLER__
ce426f
+
ce426f
+.macro XBEGIN target
ce426f
+	.byte 0xc7,0xf8
ce426f
+	.long \target-1f
ce426f
+1:
ce426f
+.endm
ce426f
+
ce426f
+.macro XEND
ce426f
+	.byte 0x0f,0x01,0xd5
ce426f
+.endm
ce426f
+
ce426f
+.macro XABORT code
ce426f
+	.byte 0xc6,0xf8,\code
ce426f
+.endm
ce426f
+
ce426f
+.macro XTEST
ce426f
+	 .byte 0x0f,0x01,0xd6
ce426f
+.endm
ce426f
+
ce426f
+#endif
ce426f
+
ce426f
+/* Official RTM intrinsics interface matching gcc/icc, but works
ce426f
+   on older gcc compatible compilers and binutils.
ce426f
+   We should somehow detect if the compiler supports it, because
ce426f
+   it may be able to generate slightly better code. */
ce426f
+
ce426f
+#define _XBEGIN_STARTED		(~0u)
ce426f
+#define _XABORT_EXPLICIT	(1 << 0)
ce426f
+#define _XABORT_RETRY		(1 << 1)
ce426f
+#define _XABORT_CONFLICT	(1 << 2)
ce426f
+#define _XABORT_CAPACITY	(1 << 3)
ce426f
+#define _XABORT_DEBUG		(1 << 4)
ce426f
+#define _XABORT_NESTED		(1 << 5)
ce426f
+#define _XABORT_CODE(x)		(((x) >> 24) & 0xff)
ce426f
+
ce426f
+#define _ABORT_LOCK_BUSY 	0xff
ce426f
+#define _ABORT_LOCK_IS_LOCKED	0xfe
ce426f
+#define _ABORT_NESTED_TRYLOCK	0xfd
ce426f
+
ce426f
+#ifndef __ASSEMBLER__
ce426f
+
ce426f
+#define __force_inline __attribute__((__always_inline__)) inline
ce426f
+
ce426f
+static __force_inline int _xbegin(void)
ce426f
+{
ce426f
+  int ret = _XBEGIN_STARTED;
ce426f
+  asm volatile (".byte 0xc7,0xf8 ; .long 0" : "+a" (ret) :: "memory");
ce426f
+  return ret;
ce426f
+}
ce426f
+
ce426f
+static __force_inline void _xend(void)
ce426f
+{
ce426f
+  asm volatile (".byte 0x0f,0x01,0xd5" ::: "memory");
ce426f
+}
ce426f
+
ce426f
+static __force_inline void _xabort(const unsigned int status)
ce426f
+{
ce426f
+  asm volatile (".byte 0xc6,0xf8,%P0" :: "i" (status) : "memory");
ce426f
+}
ce426f
+
ce426f
+static __force_inline int _xtest(void)
ce426f
+{
ce426f
+  unsigned char out;
ce426f
+  asm volatile (".byte 0x0f,0x01,0xd6 ; setnz %0" : "=r" (out) :: "memory");
ce426f
+  return out;
ce426f
+}
ce426f
+
ce426f
+#endif
ce426f
+#endif
ce426f
Index: glibc-2.17-c758a686/nptl/sysdeps/unix/sysv/linux/x86_64/lowlevellock.h
ce426f
===================================================================
ce426f
--- glibc-2.17-c758a686.orig/nptl/sysdeps/unix/sysv/linux/x86_64/lowlevellock.h
ce426f
+++ glibc-2.17-c758a686/nptl/sysdeps/unix/sysv/linux/x86_64/lowlevellock.h
ce426f
@@ -427,6 +427,13 @@ LLL_STUB_UNWIND_INFO_END
ce426f
 		       : "memory", "cx", "cc", "r10", "r11");		      \
ce426f
      result; })
ce426f
 
ce426f
+extern int __lll_timedlock_elision (int *futex, short *adapt_count,
ce426f
+					 const struct timespec *timeout,
ce426f
+					 int private) attribute_hidden;
ce426f
+
ce426f
+#define lll_timedlock_elision(futex, adapt_count, timeout, private)	\
ce426f
+  __lll_timedlock_elision(&(futex), &(adapt_count), timeout, private)
ce426f
+
ce426f
 #define lll_robust_timedlock(futex, timeout, id, private) \
ce426f
   ({ int result, ignore1, ignore2, ignore3;				      \
ce426f
      __asm __volatile (LOCK_INSTR "cmpxchgl %1, %4\n\t"			      \
ce426f
@@ -597,6 +604,22 @@ extern int __lll_timedwait_tid (int *tid
ce426f
       }									      \
ce426f
     __result; })
ce426f
 
ce426f
+extern int __lll_lock_elision (int *futex, short *adapt_count, int private)
ce426f
+  attribute_hidden;
ce426f
+
ce426f
+extern int __lll_unlock_elision (int *lock, int private)
ce426f
+  attribute_hidden;
ce426f
+
ce426f
+extern int __lll_trylock_elision (int *lock, short *adapt_count)
ce426f
+  attribute_hidden;
ce426f
+
ce426f
+#define lll_lock_elision(futex, adapt_count, private) \
ce426f
+  __lll_lock_elision (&(futex), &(adapt_count), private)
ce426f
+#define lll_unlock_elision(futex, private) \
ce426f
+  __lll_unlock_elision (&(futex), private)
ce426f
+#define lll_trylock_elision(futex, adapt_count) \
ce426f
+  __lll_trylock_elision (&(futex), &(adapt_count))
ce426f
+
ce426f
 #endif  /* !__ASSEMBLER__ */
ce426f
 
ce426f
 #endif	/* lowlevellock.h */
ce426f
Index: glibc-2.17-c758a686/sysdeps/i386/i686/multiarch/init-arch.c
ce426f
===================================================================
ce426f
--- glibc-2.17-c758a686.orig/sysdeps/i386/i686/multiarch/init-arch.c
ce426f
+++ /dev/null
ce426f
@@ -1 +0,0 @@
ce426f
-#include <sysdeps/x86_64/multiarch/init-arch.c>
ce426f
Index: glibc-2.17-c758a686/INSTALL
ce426f
===================================================================
ce426f
--- glibc-2.17-c758a686.orig/INSTALL
ce426f
+++ glibc-2.17-c758a686/INSTALL
ce426f
@@ -140,6 +140,9 @@ will be used, and CFLAGS sets optimizati
ce426f
      additional security risks to the system and you should enable it
ce426f
      only if you understand and accept those risks.
ce426f
 
ce426f
+`--enable-lock-elision=yes'
ce426f
+     Enable lock elision for pthread mutexes and rwlocks by default.
ce426f
+
ce426f
 `--build=BUILD-SYSTEM'
ce426f
 `--host=HOST-SYSTEM'
ce426f
      These options are for cross-compiling.  If you specify both
ce426f
Index: glibc-2.17-c758a686/config.h.in
ce426f
===================================================================
ce426f
--- glibc-2.17-c758a686.orig/config.h.in
ce426f
+++ glibc-2.17-c758a686/config.h.in
ce426f
@@ -180,6 +180,9 @@
ce426f
 /* Define if __stack_chk_guard canary should be randomized at program startup.  */
ce426f
 #undef ENABLE_STACKGUARD_RANDOMIZE
ce426f
 
ce426f
+/* Define if lock elision should be enabled by default.  */
ce426f
+#undef ENABLE_LOCK_ELISION
ce426f
+
ce426f
 /* Package description.  */
ce426f
 #undef PKGVERSION
ce426f
 
ce426f
Index: glibc-2.17-c758a686/configure
ce426f
===================================================================
ce426f
--- glibc-2.17-c758a686.orig/configure
ce426f
+++ glibc-2.17-c758a686/configure
ce426f
@@ -750,6 +750,7 @@ enable_profile
ce426f
 enable_versioning
ce426f
 enable_oldest_abi
ce426f
 enable_stackguard_randomization
ce426f
+enable_lock_elision
ce426f
 enable_add_ons
ce426f
 enable_hidden_plt
ce426f
 enable_bind_now
ce426f
@@ -1405,6 +1406,9 @@ Optional Features:
ce426f
   --enable-stackguard-randomization
ce426f
                           initialize __stack_chk_guard canary with a random
ce426f
                           number at program start
ce426f
+  --enable-lock-elision=yes/no
ce426f
+                          Enable lock elision for pthread mutexes and rwlocks
ce426f
+                          by default
ce426f
   --enable-add-ons[=DIRS...]
ce426f
                           configure and build add-ons in DIR1,DIR2,... search
ce426f
                           for add-ons if no parameter given
ce426f
@@ -3716,6 +3720,18 @@ if test "$enable_stackguard_randomize" =
ce426f
 
ce426f
 fi
ce426f
 
ce426f
+# Check whether --enable-lock-elision was given.
ce426f
+if test "${enable_lock_elision+set}" = set; then :
ce426f
+  enableval=$enable_lock_elision; enable_lock_elision=$enableval
ce426f
+else
ce426f
+  enable_lock_elision=no
ce426f
+fi
ce426f
+
ce426f
+if test "$enable_lock_elision" = yes ; then
ce426f
+  $as_echo "#define ENABLE_LOCK_ELISION 1" >>confdefs.h
ce426f
+
ce426f
+fi
ce426f
+
ce426f
 # Check whether --enable-add-ons was given.
ce426f
 if test "${enable_add_ons+set}" = set; then :
ce426f
   enableval=$enable_add_ons;
ce426f
Index: glibc-2.17-c758a686/configure.in
ce426f
===================================================================
ce426f
--- glibc-2.17-c758a686.orig/configure.in
ce426f
+++ glibc-2.17-c758a686/configure.in
ce426f
@@ -155,6 +155,15 @@ if test "$enable_stackguard_randomize" =
ce426f
   AC_DEFINE(ENABLE_STACKGUARD_RANDOMIZE)
ce426f
 fi
ce426f
 
ce426f
+AC_ARG_ENABLE([lock-elision],
ce426f
+	      AC_HELP_STRING([--enable-lock-elision[=yes/no]],
ce426f
+			     [Enable lock elision for pthread mutexes and rwlocks by default]),
ce426f
+	      [enable_lock_elision=$enableval],
ce426f
+	      [enable_lock_elision=no])
ce426f
+if test "$enable_lock_elision" = yes ; then
ce426f
+  AC_DEFINE(ENABLE_LOCK_ELISION)
ce426f
+fi
ce426f
+
ce426f
 dnl Generic infrastructure for drop-in additions to libc.
ce426f
 AC_ARG_ENABLE([add-ons],
ce426f
 	      AC_HELP_STRING([--enable-add-ons@<:@=DIRS...@:>@],
ce426f
Index: glibc-2.17-c758a686/manual/install.texi
ce426f
===================================================================
ce426f
--- glibc-2.17-c758a686.orig/manual/install.texi
ce426f
+++ glibc-2.17-c758a686/manual/install.texi
ce426f
@@ -174,6 +174,9 @@ setuid and owned by @code{root}.  The us
ce426f
 additional security risks to the system and you should enable it only if
ce426f
 you understand and accept those risks.
ce426f
 
ce426f
+@item --enable-lock-elision=yes
ce426f
+Enable lock elision for pthread mutexes by default.
ce426f
+
ce426f
 @item --build=@var{build-system}
ce426f
 @itemx --host=@var{host-system}
ce426f
 These options are for cross-compiling.  If you specify both options and