ce426f
commit e400f3ccd36fe91d432cc7d45b4ccc799dece763
ce426f
Author: Siddhesh Poyarekar <siddhesh@redhat.com>
ce426f
Date:   Fri Jul 24 19:13:38 2015 +0530
ce426f
ce426f
    Use IE model for static variables in libc.so, libpthread.so and rtld
ce426f
    
ce426f
    The recently introduced TLS variables in the thread-local destructor
ce426f
    implementation (__cxa_thread_atexit_impl) used the default GD access
ce426f
    model, resulting in a call to __tls_get_addr.  This causes a deadlock
ce426f
    with recent changes to the way TLS is initialized because DTV
ce426f
    allocations are delayed and hence despite knowing the offset to the
ce426f
    variable inside its TLS block, the thread has to take the global rtld
ce426f
    lock to safely update the TLS offset.
ce426f
    
ce426f
    This causes deadlocks when a thread is instantiated and joined inside
ce426f
    a destructor of a dlopen'd DSO.  The correct long term fix is to
ce426f
    somehow not take the lock, but that will need a lot deeper change set
ce426f
    to alter the way in which the big rtld lock is used.
ce426f
    
ce426f
    Instead, this patch just eliminates the call to __tls_get_addr for the
ce426f
    thread-local variables inside libc.so, libpthread.so and rtld by
ce426f
    building all of their units with -mtls-model=initial-exec.
ce426f
    
ce426f
    There were concerns that the static storage for TLS is limited and
ce426f
    hence we should not be using it.  Additionally, dynamically loaded
ce426f
    modules may result in libc.so looking for this static storage pretty
ce426f
    late in static binaries.  Both concerns are valid when using TLSDESC
ce426f
    since that is where one may attempt to allocate a TLS block from
ce426f
    static storage for even those variables that are not IE.  They're not
ce426f
    very strong arguments for the traditional TLS model though, since it
ce426f
    assumes that the static storage would be used sparingly and definitely
ce426f
    not by default.  Hence, for now this would only theoretically affect
ce426f
    ARM architectures.
ce426f
    
ce426f
    The impact is hence limited to statically linked binaries that dlopen
ce426f
    modules that in turn load libc.so, all that on arm hardware.  It seems
ce426f
    like a small enough impact to justify fixing the larger problem that
ce426f
    currently affects everything everywhere.
ce426f
    
ce426f
    This still does not solve the original problem completely.  That is,
ce426f
    it is still possible to deadlock on the big rtld lock with a small
ce426f
    tweak to the test case attached to this patch.  That problem is
ce426f
    however not a regression in 2.22 and hence could be tackled as a
ce426f
    separate project.  The test case is picked up as is from Alex's patch.
ce426f
    
ce426f
    This change has been tested to verify that it does not cause any
ce426f
    issues on x86_64.
ce426f
    
ce426f
    ChangeLog:
ce426f
    
ce426f
    	[BZ #18457]
ce426f
    	* nptl/Makefile (tests): New test case tst-join7.
ce426f
    	(modules-names): New test case module tst-join7mod.
ce426f
    	* nptl/tst-join7.c: New file.
ce426f
    	* nptl/tst-join7mod.c: New file.
ce426f
    	* Makeconfig (tls-model): Pass -ftls-model=initial-exec for
ce426f
    	all translation units in libc.so, libpthread.so and rtld.
ce426f
ce426f
diff --git a/nptl/Makefile b/nptl/Makefile
ce426f
index 140f063..aaca0a4 100644
ce426f
--- a/nptl/Makefile
ce426f
+++ b/nptl/Makefile
ce426f
@@ -245,7 +245,7 @@ tests = tst-typesizes \
ce426f
 	tst-basic7 \
ce426f
 	tst-kill1 tst-kill2 tst-kill3 tst-kill4 tst-kill5 tst-kill6 \
ce426f
 	tst-raise1 \
ce426f
-	tst-join1 tst-join2 tst-join3 tst-join4 tst-join5 tst-join6 \
ce426f
+	tst-join1 tst-join2 tst-join3 tst-join4 tst-join5 tst-join6 tst-join7 \
ce426f
 	tst-detach1 \
ce426f
 	tst-eintr1 tst-eintr2 tst-eintr3 tst-eintr4 tst-eintr5 \
ce426f
 	tst-tsd1 tst-tsd2 tst-tsd3 tst-tsd4 tst-tsd5 tst-tsd6 \
ce426f
@@ -327,7 +327,8 @@ endif
ce426f
 modules-names = tst-atfork2mod tst-tls3mod tst-tls4moda tst-tls4modb \
ce426f
 		tst-tls5mod tst-tls5moda tst-tls5modb tst-tls5modc \
ce426f
 		tst-tls5modd tst-tls5mode tst-tls5modf tst-stack4mod \
ce426f
-		tst-_res1mod1 tst-_res1mod2 tst-execstack-mod tst-fini1mod
ce426f
+		tst-_res1mod1 tst-_res1mod2 tst-execstack-mod tst-fini1mod \
ce426f
+		tst-join7mod
ce426f
 extra-test-objs += $(addsuffix .os,$(strip $(modules-names))) tst-cleanup4aux.o
ce426f
 test-extras += $(modules-names) tst-cleanup4aux
ce426f
 test-modules = $(addprefix $(objpfx),$(addsuffix .so,$(modules-names)))
ce426f
@@ -532,6 +533,11 @@ $(objpfx)tst-tls6.out: tst-tls6.sh $(objpfx)tst-tls5 \
ce426f
 		    $(rtld-installed-name) '$(test-wrapper-env)'
ce426f
 endif
ce426f
 
ce426f
+$(objpfx)tst-join7: $(libdl) $(shared-thread-library)
ce426f
+$(objpfx)tst-join7.out: $(objpfx)tst-join7mod.so
ce426f
+$(objpfx)tst-join7mod.so: $(shared-thread-library)
ce426f
+LDFLAGS-tst-join7mod.so = -Wl,-soname,tst-join7mod.so
ce426f
+
ce426f
 $(objpfx)tst-dlsym1: $(libdl) $(shared-thread-library)
ce426f
 
ce426f
 $(objpfx)tst-fini1: $(shared-thread-library) $(objpfx)tst-fini1mod.so
ce426f
diff --git a/nptl/tst-join7.c b/nptl/tst-join7.c
ce426f
new file mode 100644
ce426f
index 0000000..439d0fc
ce426f
--- /dev/null
ce426f
+++ b/nptl/tst-join7.c
ce426f
@@ -0,0 +1,46 @@
ce426f
+/* Verify that TLS access in separate thread in a dlopened library does not
ce426f
+   deadlock.
ce426f
+   Copyright (C) 2015 Free Software Foundation, Inc.
ce426f
+   This file is part of the GNU C Library.
ce426f
+
ce426f
+   The GNU C Library is free software; you can redistribute it and/or
ce426f
+   modify it under the terms of the GNU Lesser General Public
ce426f
+   License as published by the Free Software Foundation; either
ce426f
+   version 2.1 of the License, or (at your option) any later version.
ce426f
+
ce426f
+   The GNU C Library is distributed in the hope that it will be useful,
ce426f
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
ce426f
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
ce426f
+   Lesser General Public License for more details.
ce426f
+
ce426f
+   You should have received a copy of the GNU Lesser General Public
ce426f
+   License along with the GNU C Library; if not, see
ce426f
+   <http://www.gnu.org/licenses/>.  */
ce426f
+
ce426f
+#include <dlfcn.h>
ce426f
+
ce426f
+/* When one dynamically loads a module, which spawns a thread to perform some
ce426f
+   activities, it could be possible that TLS storage is accessed for the first
ce426f
+   time in that thread.  This results in an allocation request within the
ce426f
+   thread, which could result in an attempt to take the rtld load_lock.  This
ce426f
+   is a problem because it would then deadlock with the dlopen (which owns the
ce426f
+   lock), if the main thread is waiting for the spawned thread to exit.  We can
ce426f
+   at least ensure that this problem does not occur due to accesses within
ce426f
+   libc.so, by marking TLS variables within libc.so as IE.  The problem of an
ce426f
+   arbitrary variable being accessed and constructed within such a thread still
ce426f
+   exists but this test case does not verify that.  */
ce426f
+
ce426f
+int
ce426f
+do_test (void)
ce426f
+{
ce426f
+  void *f = dlopen ("tst-join7mod.so", RTLD_NOW | RTLD_GLOBAL);
ce426f
+  if (f)
ce426f
+    dlclose (f);
ce426f
+  else
ce426f
+    return 1;
ce426f
+
ce426f
+  return 0;
ce426f
+}
ce426f
+
ce426f
+#define TEST_FUNCTION do_test ()
ce426f
+#include "../test-skeleton.c"
ce426f
diff --git a/nptl/tst-join7mod.c b/nptl/tst-join7mod.c
ce426f
new file mode 100644
ce426f
index 0000000..92bb381
ce426f
--- /dev/null
ce426f
+++ b/nptl/tst-join7mod.c
ce426f
@@ -0,0 +1,61 @@
ce426f
+/* Verify that TLS access in separate thread in a dlopened library does not
ce426f
+   deadlock - the module.
ce426f
+   Copyright (C) 2015 Free Software Foundation, Inc.
ce426f
+   This file is part of the GNU C Library.
ce426f
+
ce426f
+   The GNU C Library is free software; you can redistribute it and/or
ce426f
+   modify it under the terms of the GNU Lesser General Public
ce426f
+   License as published by the Free Software Foundation; either
ce426f
+   version 2.1 of the License, or (at your option) any later version.
ce426f
+
ce426f
+   The GNU C Library is distributed in the hope that it will be useful,
ce426f
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
ce426f
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
ce426f
+   Lesser General Public License for more details.
ce426f
+
ce426f
+   You should have received a copy of the GNU Lesser General Public
ce426f
+   License along with the GNU C Library; if not, see
ce426f
+   <http://www.gnu.org/licenses/>.  */
ce426f
+
ce426f
+#include <stdio.h>
ce426f
+#include <pthread.h>
ce426f
+#include <atomic.h>
ce426f
+
ce426f
+static pthread_t th;
ce426f
+static int running = 1;
ce426f
+
ce426f
+static void *
ce426f
+test_run (void *p)
ce426f
+{
ce426f
+  while (atomic_load_relaxed (&running))
ce426f
+    printf ("Test running\n");
ce426f
+  printf ("Test finished\n");
ce426f
+  return NULL;
ce426f
+}
ce426f
+
ce426f
+static void __attribute__ ((constructor))
ce426f
+do_init (void)
ce426f
+{
ce426f
+  int ret = pthread_create (&th, NULL, test_run, NULL);
ce426f
+
ce426f
+  if (ret != 0)
ce426f
+    {
ce426f
+      printf ("failed to create thread: %s (%d)\n", strerror (ret), ret);
ce426f
+      exit (1);
ce426f
+    }
ce426f
+}
ce426f
+
ce426f
+static void __attribute__ ((destructor))
ce426f
+do_end (void)
ce426f
+{
ce426f
+  atomic_store_relaxed (&running, 0);
ce426f
+  int ret = pthread_join (th, NULL);
ce426f
+
ce426f
+  if (ret != 0)
ce426f
+    {
ce426f
+      printf ("pthread_join: %s(%d)\n", strerror (ret), ret);
ce426f
+      exit (1);
ce426f
+    }
ce426f
+
ce426f
+  printf ("Thread joined\n");
ce426f
+}
ce426f
diff -pruN a/string/strerror_l.c b/string/strerror_l.c
ce426f
--- a/string/strerror_l.c
ce426f
+++ b/string/strerror_l.c
ce426f
@@ -23,7 +23,7 @@
ce426f
 #include <sys/param.h>
ce426f
 
ce426f
 
ce426f
-static __thread char *last_value;
ce426f
+static __thread char *last_value attribute_tls_model_ie;
ce426f
 
ce426f
 
ce426f
 static const char *