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