Add vectorized getenv for glibc use

This adds a general "vectorized getenv" for glibc internal use.
The motivation is to allow subsystems to access environment variables
cheaply without having to rescan the environment completely.

The dynamic linker already walks the environment to look for its
LD_* variables. Extend this code to look for a number of
pre-registered GLIBC_* variables too. This can be done at basically
no additional cost. The only two variables currently pre-registered
are for the lock elision code.

For static builds which do not use the dynamic linker a similar
environment walking function is called at init time.

The variable values are saved in a global array that can be directly
accessed by libc and related libraries like libpthread.

2013-05-10  Andi Kleen  <ak@linux.intel.com>

	* elf/Makefile (glibc-var.o): Add new file.
	* elf/Versions (_dl_glibc_var, __glibc_var_init): Export new symbols
	as GLIBC_PRIVATE.
	* elf/dl-environ.c (_dl_next_ld_env_entry): Look for GLIBC_ prefixes
	too. Return first character in new argument.
	* elf/dl-glibc-var.c (struct glibc_var): Define global array.
	(__record_glibc_var): New function to save glibc variables.
	(next_env_entry): Function to walk environment.
	(__glibc_var_init): Fallback function for static builds.
	* elf/rtld.c: Update comment.
	(process_envvars): Handle GLIBC_ variables and call __record_glibc_var.
	* include/glibc-var.h: New file.
	* sysdeps/generic/ldsodefs.h (_dl_next_ld_env_entry): Update prototype.
Add the low level infrastructure for pthreads lock elision with TSX

Lock elision using TSX is a technique to optimize lock scaling
It allows to run locks in parallel using hardware support for
a transactional execution mode in upcoming Intel CPUs.
See http://software.intel.com/file/41604 for the full
specification.

This patch implements a simple adaptive lock elision algorithm based
on RTM. It enables elision for the pthread mutexes and rwlocks.
The algorithm keeps track whether a mutex successfully elides or not,
and stops eliding for some time when it is not.

When the CPU supports RTM the elision path is automatically tried,
otherwise any elision is disabled.

The adaptation algorithm and its tuning is currently preliminary.

The code adds some checks to the lock fast paths. Micro-benchmarks
show little to no difference without RTM.

Lock elision can be enabled/disabled using environment variables.
It can be also enabled or disabled using new lock types for
mutex and rwlocks. The adaptation parameters are also tunable.

This patch implements the low level "lll_" code for lock elision.
Followon patches hook this into the pthread implement.

Changes with the RTM mutexes:
-----------------------------
Lock elision in pthreads is generally compatible with existing programs.
There are some obscure exceptions, which are expected to be uncommon.
See the manual for more details.

- A broken program that unlocks a free lock will crash.
  There are ways around this with some tradeoffs (more code in hot paths)
  This will also happen on systems without RTM with the patchkit.
  I'm still undecided on what approach to take here; have to wait for testing reports.
- pthread_mutex_destroy of a lock mutex will not return EBUSY but 0.
- mutex appears free when elided.
  pthread_mutex_lock(mutex);
  if (pthread_mutex_trylock(mutex) != 0) do_something
  will not do something when the lock elided.
  However note that if the check is an assert it works as expected because the
  assert failure aborts and the region is re-executed non transactionally,
  with the old behaviour.
  The same change applies to write locks for rwlocks.
  [This is now only done for mutexes that have elision explicitely enabled,
   standard mutexes abort in this situation]
- There's also a similar situation with trylock outside the mutex,
  "knowing" that the mutex must be held due to some other condition.
  In this case an assert failure cannot be recovered. This situation is
  usually an existing bug in the program.
- Same applies to the rwlocks. Some of the return values changes
  (for example there is no EDEADLK for an elided lock, unless it aborts.
   However when elided it will also never deadlock of course)
- Timing changes, so broken programs that make assumptions about specific timing
  may expose already existing latent problems.  Note that these broken programs will
  break in other situations too (loaded system, new faster hardware, compiler
  optimizations etc.)

Currently elision is enabled by default on systems that support RTM,
unless explicitely disabled either in the program or by the user.

This patch implements the basic infrastructure for elision.

Open issues:
- XTEST or not XTEST in unlock, see above.
- Adaptation for rwlocks
- Condition variables don't use elision so far
- Adaptation tuning

2013-05-02  Andi Kleen  <ak@linux.intel.com>
            Hongjiu Lu  <hongjiu.lu@intel.com>

	* nptl-init.c (__pthread_force_elision): Add.
	* pthreadP.h (__pthread_force_elision): Add.
	* sysdeps/unix/sysv/linux/i386/lowlevellock.h (__lll_timedwait_tid,
          lll_timedlock_elision, __lll_lock_elision, __lll_unlock_elision,
          __lll_trylock_elision, lll_lock_elision, lll_unlock_elision,
	  lll_trylock_elision): Add.
	* sysdeps/unix/sysv/linux/x86/Makefile: Imply x86
	* sysdeps/unix/sysv/linux/x86/elision-conf.c: New file.
	* sysdeps/unix/sysv/linux/x86/elision-conf.h: New file.
	* sysdeps/unix/sysv/linux/x86/elision-lock.c: New file.
	* sysdeps/unix/sysv/linux/x86/elision-timed.c: New file.
	* sysdeps/unix/sysv/linux/x86/elision-trylock.c: New file.
	* sysdeps/unix/sysv/linux/x86/elision-unlock.c: New file
	* sysdeps/unix/sysv/linux/x86_64/lowlevellock.h (__lll_timedwait_tid,
          lll_timedlock_elision, __lll_lock_elision, __lll_unlock_elision,
          __lll_trylock_elision, lll_lock_elision, lll_unlock_elision,
	  lll_trylock_elision): Add.
	* nptl/sysdeps/unix/sysv/linux/x86/hle.h: New file.
	* elision-conf.h: New file.
Add external interface changes: new lock types for pthread_mutex_t

Add a new PTHREAD_MUTEX_INIT_NP() macro that allows to set generic
mutex type/flags combinations. Expose PTHREAD_MUTEX_ELISION_NP
and PTHREAD_MUTEX_NO_ELISION_NP flags. In addition also expose
the existing PTHREAD_MUTEX_PSHARED_NP flag.

Similar flags are defined for the rwlocks.

This allows programs to set elision in a fine grained matter.
See the manual for more details.

recursive/pi/error checking mutexes do not elide.
Recursive could be implemented at some point, but are not currently.
The initializer allows to set illegal combinations currently.

2013-05-02  Andi Kleen  <ak@linux.intel.com>

	* pthreadP.h: Add elision types.
          (PTHREAD_MUTEX_TYPE_EL): Add.
	* sysdeps/pthread/pthread.h: Add elision initializers.
	  (PTHREAD_MUTEX_ELISION_NP, PTHREAD_MUTEX_NO_ELISION_NP,
	   PTHREAD_MUTEX_PSHARED_NP): Add new flags.
          (__PTHREAD_SPINS): Add.
	  Update mutex initializers.
	  (PTHREAD_RWLOCK_ELISION_NP, PTHREAD_RWLOCK_NO_ELISION_NP): Add.
Add elision to pthread_mutex_{try,timed,un,}lock

Add elision paths to the basic mutex locks.

The normal path has a check for RTM and upgrades the lock
to RTM when available. Trylocks cannot automatically upgrade,
so they check for elision every time.

We use a 4 byte value in the mutex to store the lock
elision adaptation state. This is separate from the adaptive
spin state and uses a separate field.

Condition variables currently do not support elision.

Recursive mutexes and condition variables may be supported at some point,
but are not in the current implementation. Also "trylock" will
not automatically enable elision unless some other lock call
has been already called on the lock.

This version does not use IFUNC, so it means every lock has one
additional check for elision. Benchmarking showed the overhead
to be negligible.

2013-05-02  Andi Kleen  <ak@linux.intel.com>
            Hongjiu Lu  <hongjiu.lu@intel.com>

	* pthread_mutex_lock.c (adaptive_lock): Add
	(__pthread_mutex_lock): Add lock elision support.
	* pthread_mutex_timedlock.c (pthread_mutex_timedlock): dito.
	* pthread_mutex_trylock.c (__pthread_mutex_trylock): dito.
	* pthread_mutex_unlock.c (__pthread_mutex_unlock_usercnt): dito.
	* sysdeps/unix/sysv/linux/pthread_mutex_cond_lock.c: dito.
	* sysdeps/unix/sysv/linux/x86/bits/pthreadtypes.h: dito.
	* sysdeps/unix/sysv/linux/x86/Makefile: New file.
	* sysdeps/unix/sysv/linux/x86/force-elision.h: New file
	* sysdeps/unix/sysv/linux/x86/pthread_mutex_cond_lock.c: dito.
	* sysdeps/unix/sysv/linux/x86/pthread_mutex_lock.c: dito.
	* sysdeps/unix/sysv/linux/x86/pthread_mutex_timedlock.c: dito.
	* sysdeps/unix/sysv/linux/x86/pthread_mutex_trylock.c: dito.
	* sysdeps/unix/sysv/linux/x86/pthread_mutex_unlock.c: dito.
	* pthreadP.h: (PTHREAD_MUTEX_UPGRADED_ELISION_NP): Add.
Support setting elision in pthread_mutexattr_settype

2013-05-02 Andi Kleen  <ak@linux.intel.com>

	* pthread_mutexattr_settype.c (__pthread_mutexattr_settype):
        Support elision flags.
Add lock elision to rwlocks

This code is very similar to the main pthread mutex code, but we have
to implement part of it in assembler. IFUNC is used to avoid any
fast path overhead on non RTM systems for the lock functions (but not
unlock)

The patch kit is large because there are so many combinations
of reader and writer locks, but it is fairly repetive.

The basic elision algorithm is very similar to normal mutexes:
a wrapper checks if the lock is free (both reader and writer)
We have to check both reader and writer because separating them
would require putting state on different cache lines.

The algorithm is very basic currently and may be tuned
more in the future.

No adaptation at this point, the elision will not automatically
disable itself. The programmer or user can do it explicitely though.

New lock type flags are exposed to force enable/disable elision
for a specific rwlocks.

We use some padding in the internal rwlock as a elision flag.
This is 8 bytes on 64bits currently, but could be made smaller later
without compatibility problems (or to share some bytes with a
adaptation count). 32bit uses one byte.

2013-05-02  Andi Kleen <ak@linux.intel.com>
            Hongjiu Lu <hongjiu.lu@intel.com>

	* pthread_rwlock_init.c (__pthread_rwlock_init): Support elision.
	* pthread_rwlockattr_setkind_np.c (pthread_rwlockattr_setkind_np): Support elision.
	* sysdeps/unix/sysv/linux/i386/i486/pthread_rwlock_rdlock.S: dito.
	* sysdeps/unix/sysv/linux/i386/i486/pthread_rwlock_timedrdlock.S: dito
	* sysdeps/unix/sysv/linux/i386/i486/pthread_rwlock_timedwrlock.S: dito.
	* sysdeps/unix/sysv/linux/i386/i486/pthread_rwlock_unlock.S: dito.
	* sysdeps/unix/sysv/linux/i386/i486/pthread_rwlock_wrlock.S: dito.
	* sysdeps/unix/sysv/linux/internaltypes.h (struct pthread_condattr):
	(struct pthread_rwlockattr): Support elision flags in internal rwlock.
	* sysdeps/unix/sysv/linux/lowlevelrwlock.sym: Add ELIDED.
	* sysdeps/unix/sysv/linux/x86/bits/pthreadtypes.h: Add elision flags
	* sysdeps/unix/sysv/linux/x86/pthread_rwlock_tryrdlock.c: New file
	* sysdeps/unix/sysv/linux/x86/pthread_rwlock_trywrlock.c: dito.
	* sysdeps/unix/sysv/linux/x86_64/pthread_rwlock_rdlock.S: Handle elision.
	* sysdeps/unix/sysv/linux/x86_64/pthread_rwlock_timedrdlock.S: dito.
	* sysdeps/unix/sysv/linux/x86_64/pthread_rwlock_timedwrlock.S: dito.
	* sysdeps/unix/sysv/linux/x86_64/pthread_rwlock_unlock.S: dito.
	* sysdeps/unix/sysv/linux/x86_64/pthread_rwlock_wrlock.S: dito.
Extend the test suite for TSX and add some new tests to test elision

- Some of the existing mutex tests do not work with lock elision
semantics. We run them with PTHREAD_MUTEX=none PTHREAD_RWLOCK=none,
but do not otherwise modify it.

This is expected behaviour, related to RTM lock elision's inability
to track the owner of lock (see the original description for more
details). It's not expected that these changes will break any programs,
it's mostly related to obscure behaviour.
- I added some ifdefs to these tests and add new variants that
set the defines to test elision behaviour.
- Add new tests that explicitely check for elision.
Note strictly this violates the TSX spec which never guarantees any transaction
ever succeeding. However we assume simple transactions will succeed and will
fail if not.
- Add tests covering the new lock kinds.

2013-05-02  Andi Kleen  <ak@linux.intel.com>

	* Makefile: Add elision tests. Disable elision for some existing
        tests.
	* tst-abstime.c: Fix for elision.
	* tst-abstime1b.c: New file for lock elision code.
	* tst-abstime1c.c:dito.
	* tst-elision-common.c: dito.
	* tst-elision1.c: dito.
	* tst-elision1b.c: dito.
	* tst-elision2.c: dito.
	* tst-initializers2-c89.c: Handle lock elision
	* tst-initializers2-c99.c: dito.
	* tst-initializers2-gnu89.c: dito.
	* tst-initializers2-gnu99.c: dito.
	* tst-initializers2.c: dito.
	* tst-mutex5.c: Handle lock elision.
	* tst-mutex5b.c: New file.
	* tst-mutex5c.c: dito.
	* tst-mutex5d.c: dito
	* tst-mutex5e.c: dito.
	* tst-mutex7b.c: dito.
	* tst-mutex7c.c: dito.
	* tst-mutex7d.c: dito.
	* tst-mutex7e.c: dito.
	* tst-mutex8.c: Handle lock elision.
	* tst-mutex8b.c: New file.
	* tst-mutex8c.c: dito.
	* tst-mutex8d.c: dito.
	* tst-mutex8e.c: dito.
	* tst-mutex8f.c: dito.
	* tst-mutex8g.c: dito.
Fix tst-mutexpi8

tst-mutexpi8 didn't actually test PI because tst-mutex8 didn't have any
code to enable priority inheritance. Add the needed code in a ifdef.

This also fixes it for lock elision because priority inheritance turns
off elision, so we don't need to explicitely disable it.

2013-05-02  Andi Kleen  <ak@linux.intel.com>

	* tst-mutex8.c: Check for ENABLE_PI
	* tst-mutexpi8.c:  Set ENABLE_PI.
Add manual for lock elision

pthreads are not described in the documentation, but I decided to document
lock elision there at least.

2013-05-02  Andi Kleen  <ak@linux.intel.com>

	* manual/Makefile: Add elision.texi.
	* manual/threads.texi: Link to elision.
	* manual/elision.texi: New file.
	* manual/intro.texi: Link to elision.
	* manual/lang.texi: dito.
Add README.md