public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
From: Siddhesh Poyarekar <siddhesh@redhat.com>
To: libc-alpha@sourceware.org
Cc: triegel@redhat.com, carlos@redhat.com
Subject: [PATCH] Also use l_tls_dtor_count to decide on object unload
Date: Thu, 09 Jul 2015 18:07:00 -0000	[thread overview]
Message-ID: <20150709180724.GA8552@spoyarek.pnq.redhat.com> (raw)

When an TLS destructor is registered, we set the DF_1_NODELETE flag to
signal that the object should not be destroyed.  We then clear the
DF_1_NODELETE flag when all destructors are called, which is wrong -
the flag could have been set by other means too.

This patch replaces this use of the flag by using l_tls_dtor_count
directly to determine whether it is safe to unload the object.  This
change has the added advantage of eliminating the lock taking when
calling the destructors, which could result in a deadlock.  The patch
also fixes the test case tst-tls-atexit - it was making an invalid
dlclose call, which would just return an error silently.

Change verified on x86_64; the test suite does not show any
regressions due to the patch.

ChangeLog:

	* elf/dl-close.c (_dl_close_worker): Don't unload DSO if there
	are pending TLS destructor calls.
	* stdlib/cxa_thread_atexit_impl.c (__cxa_thread_atexit_impl):
	Don't touch the link map flag.  Atomically increment
	l_tls_dtor_count.
	(__call_tls_dtors): Atomically decrement l_tls_dtor_count.
	Avoid taking the load lock and don't touch the link map flag.
	* stdlib/tst-tls-atexit.c (do_test): dlopen
	tst-tls-atexit-lib.so again before dlclose.
---
 elf/dl-close.c                  |  9 ++++++++-
 stdlib/cxa_thread_atexit_impl.c | 25 +++++++------------------
 stdlib/tst-tls-atexit.c         |  9 ++++++++-
 3 files changed, 23 insertions(+), 20 deletions(-)

diff --git a/elf/dl-close.c b/elf/dl-close.c
index 2104674..30e30e2 100644
--- a/elf/dl-close.c
+++ b/elf/dl-close.c
@@ -153,7 +153,11 @@ _dl_close_worker (struct link_map *map, bool force)
       maps[idx] = l;
       ++idx;
 
-      /* Clear DF_1_NODELETE to force object deletion.  */
+      /* Clear DF_1_NODELETE to force object deletion.  We don't need to touch
+	 l_tls_dtor_count because forced object deletion only happens when an
+	 error occurs during object load.  Destructor registration for TLS
+	 non-POD objects should not have happened till then for this
+	 object.  */
       if (force)
 	l->l_flags_1 &= ~DF_1_NODELETE;
     }
@@ -173,10 +177,13 @@ _dl_close_worker (struct link_map *map, bool force)
 	/* Already handled.  */
 	continue;
 
+      size_t tls_dtor_count = atomic_load_relaxed (&l->l_tls_dtor_count);
+
       /* Check whether this object is still used.  */
       if (l->l_type == lt_loaded
 	  && l->l_direct_opencount == 0
 	  && (l->l_flags_1 & DF_1_NODELETE) == 0
+	  && tls_dtor_count == 0
 	  && !used[done_index])
 	continue;
 
diff --git a/stdlib/cxa_thread_atexit_impl.c b/stdlib/cxa_thread_atexit_impl.c
index 9120162..fac2cc9 100644
--- a/stdlib/cxa_thread_atexit_impl.c
+++ b/stdlib/cxa_thread_atexit_impl.c
@@ -50,27 +50,25 @@ __cxa_thread_atexit_impl (dtor_func func, void *obj, void *dso_symbol)
   tls_dtor_list = new;
 
   /* See if we already encountered the DSO.  */
-  __rtld_lock_lock_recursive (GL(dl_load_lock));
-
   if (__glibc_unlikely (dso_symbol_cache != dso_symbol))
     {
       ElfW(Addr) caller = (ElfW(Addr)) dso_symbol;
 
+      /* _dl_find_dso_for_object assumes that we have the dl_load_lock.  */
+      __rtld_lock_lock_recursive (GL(dl_load_lock));
       struct link_map *l = _dl_find_dso_for_object (caller);
+      __rtld_lock_unlock_recursive (GL(dl_load_lock));
 
       /* If the address is not recognized the call comes from the main
          program (we hope).  */
       lm_cache = l ? l : GL(dl_ns)[LM_ID_BASE]._ns_loaded;
     }
+
   /* A destructor could result in a thread_local construction and the former
      could have cleared the flag.  */
-  if (lm_cache->l_type == lt_loaded && lm_cache->l_tls_dtor_count == 0)
-    lm_cache->l_flags_1 |= DF_1_NODELETE;
+  atomic_increment (&lm_cache->l_tls_dtor_count);
 
   new->map = lm_cache;
-  new->map->l_tls_dtor_count++;
-
-  __rtld_lock_unlock_recursive (GL(dl_load_lock));
 
   return 0;
 }
@@ -83,19 +81,10 @@ __call_tls_dtors (void)
   while (tls_dtor_list)
     {
       struct dtor_list *cur = tls_dtor_list;
-      tls_dtor_list = tls_dtor_list->next;
 
+      tls_dtor_list = tls_dtor_list->next;
       cur->func (cur->obj);
-
-      __rtld_lock_lock_recursive (GL(dl_load_lock));
-
-      /* Allow DSO unload if count drops to zero.  */
-      cur->map->l_tls_dtor_count--;
-      if (cur->map->l_tls_dtor_count == 0 && cur->map->l_type == lt_loaded)
-        cur->map->l_flags_1 &= ~DF_1_NODELETE;
-
-      __rtld_lock_unlock_recursive (GL(dl_load_lock));
-
+      atomic_decrement (&cur->map->l_tls_dtor_count);
       free (cur);
     }
 }
diff --git a/stdlib/tst-tls-atexit.c b/stdlib/tst-tls-atexit.c
index 68247d1..ba70790 100644
--- a/stdlib/tst-tls-atexit.c
+++ b/stdlib/tst-tls-atexit.c
@@ -82,7 +82,14 @@ do_test (void)
   if (thr_ret != NULL)
     return 1;
 
-  /* Now this should unload the DSO.  */
+  /* Now this sequence should unload the DSO.  */
+  handle = dlopen ("$ORIGIN/tst-tls-atexit-lib.so", RTLD_LAZY);
+  if (!handle)
+    {
+      printf ("main thread: Unable to load DSO: %s\n", dlerror ());
+      return 1;
+    }
+
   dlclose (handle);
 
   /* Run through our maps and ensure that the DSO is unloaded.  */
-- 
2.1.0

             reply	other threads:[~2015-07-09 18:07 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-07-09 18:07 Siddhesh Poyarekar [this message]
2015-07-09 20:43 ` Roland McGrath
2015-07-10 18:16   ` [PATCH v2] Also use l_tls_dtor_count to decide on object unload (BZ #18657) Siddhesh Poyarekar
2015-07-10 19:34     ` Ondřej Bílka
2015-07-15 10:51       ` Siddhesh Poyarekar
2015-07-10 20:19     ` Roland McGrath
2015-07-15  9:30     ` [PATCH v3] " Siddhesh Poyarekar
2015-07-15 11:10     ` [PATCH v4] " Siddhesh Poyarekar
2015-07-16 20:15       ` Ondřej Bílka
2015-07-17 13:32       ` Carlos O'Donell
2015-07-20 10:51         ` [PATCH v5] " Siddhesh Poyarekar
2015-07-20 17:31         ` [PATCH v6] " Siddhesh Poyarekar
2015-07-21 21:36           ` Torvald Riegel
2015-07-23  5:49             ` Siddhesh Poyarekar
2015-07-23 20:01               ` Carlos O'Donell
2015-07-24  0:41                 ` Siddhesh Poyarekar
2015-07-24  2:41                   ` Carlos O'Donell
2015-07-22  0:18           ` Roland McGrath
2015-07-23 19:54           ` Carlos O'Donell
2015-07-24  8:52             ` Torvald Riegel
2015-07-24 19:07               ` Carlos O'Donell
2015-08-03 15:36                 ` Torvald Riegel

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20150709180724.GA8552@spoyarek.pnq.redhat.com \
    --to=siddhesh@redhat.com \
    --cc=carlos@redhat.com \
    --cc=libc-alpha@sourceware.org \
    --cc=triegel@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).