about summary refs log tree commit diff
path: root/nptl/tst-join7mod.c
diff options
context:
space:
mode:
authorSiddhesh Poyarekar <siddhesh@redhat.com>2015-07-24 19:13:38 +0530
committerSiddhesh Poyarekar <siddhesh@redhat.com>2015-07-24 19:13:38 +0530
commite400f3ccd36fe91d432cc7d45b4ccc799dece763 (patch)
treeb586fda337c690401881f8e805bbdd542ce48023 /nptl/tst-join7mod.c
parent48f5f7a63c5971a698ac2b97e4c383e04cbbe110 (diff)
downloadglibc-e400f3ccd36fe91d432cc7d45b4ccc799dece763.tar.gz
glibc-e400f3ccd36fe91d432cc7d45b4ccc799dece763.tar.xz
glibc-e400f3ccd36fe91d432cc7d45b4ccc799dece763.zip
Use IE model for static variables in libc.so, libpthread.so and rtld
The recently introduced TLS variables in the thread-local destructor
implementation (__cxa_thread_atexit_impl) used the default GD access
model, resulting in a call to __tls_get_addr.  This causes a deadlock
with recent changes to the way TLS is initialized because DTV
allocations are delayed and hence despite knowing the offset to the
variable inside its TLS block, the thread has to take the global rtld
lock to safely update the TLS offset.

This causes deadlocks when a thread is instantiated and joined inside
a destructor of a dlopen'd DSO.  The correct long term fix is to
somehow not take the lock, but that will need a lot deeper change set
to alter the way in which the big rtld lock is used.

Instead, this patch just eliminates the call to __tls_get_addr for the
thread-local variables inside libc.so, libpthread.so and rtld by
building all of their units with -mtls-model=initial-exec.

There were concerns that the static storage for TLS is limited and
hence we should not be using it.  Additionally, dynamically loaded
modules may result in libc.so looking for this static storage pretty
late in static binaries.  Both concerns are valid when using TLSDESC
since that is where one may attempt to allocate a TLS block from
static storage for even those variables that are not IE.  They're not
very strong arguments for the traditional TLS model though, since it
assumes that the static storage would be used sparingly and definitely
not by default.  Hence, for now this would only theoretically affect
ARM architectures.

The impact is hence limited to statically linked binaries that dlopen
modules that in turn load libc.so, all that on arm hardware.  It seems
like a small enough impact to justify fixing the larger problem that
currently affects everything everywhere.

This still does not solve the original problem completely.  That is,
it is still possible to deadlock on the big rtld lock with a small
tweak to the test case attached to this patch.  That problem is
however not a regression in 2.22 and hence could be tackled as a
separate project.  The test case is picked up as is from Alex's patch.

This change has been tested to verify that it does not cause any
issues on x86_64.

ChangeLog:

	[BZ #18457]
	* nptl/Makefile (tests): New test case tst-join7.
	(modules-names): New test case module tst-join7mod.
	* nptl/tst-join7.c: New file.
	* nptl/tst-join7mod.c: New file.
	* Makeconfig (tls-model): Pass -ftls-model=initial-exec for
	all translation units in libc.so, libpthread.so and rtld.
Diffstat (limited to 'nptl/tst-join7mod.c')
-rw-r--r--nptl/tst-join7mod.c61
1 files changed, 61 insertions, 0 deletions
diff --git a/nptl/tst-join7mod.c b/nptl/tst-join7mod.c
new file mode 100644
index 0000000000..92bb381705
--- /dev/null
+++ b/nptl/tst-join7mod.c
@@ -0,0 +1,61 @@
+/* Verify that TLS access in separate thread in a dlopened library does not
+   deadlock - the module.
+   Copyright (C) 2015 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, see
+   <http://www.gnu.org/licenses/>.  */
+
+#include <stdio.h>
+#include <pthread.h>
+#include <atomic.h>
+
+static pthread_t th;
+static int running = 1;
+
+static void *
+test_run (void *p)
+{
+  while (atomic_load_relaxed (&running))
+    printf ("Test running\n");
+  printf ("Test finished\n");
+  return NULL;
+}
+
+static void __attribute__ ((constructor))
+do_init (void)
+{
+  int ret = pthread_create (&th, NULL, test_run, NULL);
+
+  if (ret != 0)
+    {
+      printf ("failed to create thread: %s (%d)\n", strerror (ret), ret);
+      exit (1);
+    }
+}
+
+static void __attribute__ ((destructor))
+do_end (void)
+{
+  atomic_store_relaxed (&running, 0);
+  int ret = pthread_join (th, NULL);
+
+  if (ret != 0)
+    {
+      printf ("pthread_join: %s(%d)\n", strerror (ret), ret);
+      exit (1);
+    }
+
+  printf ("Thread joined\n");
+}