arrfab / rpms / glibc

Forked from rpms/glibc 4 years ago
Clone

Blame SOURCES/glibc-rh841653-0.patch

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