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