179894
commit f4f8f4d4e0f92488431b268c8cd9555730b9afe9
179894
Author: Szabolcs Nagy <szabolcs.nagy@arm.com>
179894
Date:   Wed Dec 30 19:19:37 2020 +0000
179894
179894
    elf: Use relaxed atomics for racy accesses [BZ #19329]
179894
    
179894
    This is a follow up patch to the fix for bug 19329.  This adds relaxed
179894
    MO atomics to accesses that were previously data races but are now
179894
    race conditions, and where relaxed MO is sufficient.
179894
    
179894
    The race conditions all follow the pattern that the write is behind the
179894
    dlopen lock, but a read can happen concurrently (e.g. during tls access)
179894
    without holding the lock.  For slotinfo entries the read value only
179894
    matters if it reads from a synchronized write in dlopen or dlclose,
179894
    otherwise the related dtv entry is not valid to access so it is fine
179894
    to leave it in an inconsistent state.  The same applies for
179894
    GL(dl_tls_max_dtv_idx) and GL(dl_tls_generation), but there the
179894
    algorithm relies on the fact that the read of the last synchronized
179894
    write is an increasing value.
179894
    
179894
    Reviewed-by: Adhemerval Zanella  <adhemerval.zanella@linaro.org>
179894
179894
diff --git a/elf/dl-close.c b/elf/dl-close.c
179894
index 1ece0ae1dd062d1e..7d2dc2272cd643f5 100644
179894
--- a/elf/dl-close.c
179894
+++ b/elf/dl-close.c
179894
@@ -79,9 +79,10 @@ remove_slotinfo (size_t idx, struct dtv_slotinfo_list *listp, size_t disp,
179894
 	{
179894
 	  assert (old_map->l_tls_modid == idx);
179894
 
179894
-	  /* Mark the entry as unused. */
179894
-	  listp->slotinfo[idx - disp].gen = GL(dl_tls_generation) + 1;
179894
-	  listp->slotinfo[idx - disp].map = NULL;
179894
+	  /* Mark the entry as unused.  These can be read concurrently.  */
179894
+	  atomic_store_relaxed (&listp->slotinfo[idx - disp].gen,
179894
+				GL(dl_tls_generation) + 1);
179894
+	  atomic_store_relaxed (&listp->slotinfo[idx - disp].map, NULL);
179894
 	}
179894
 
179894
       /* If this is not the last currently used entry no need to look
179894
@@ -96,8 +97,8 @@ remove_slotinfo (size_t idx, struct dtv_slotinfo_list *listp, size_t disp,
179894
 
179894
       if (listp->slotinfo[idx - disp].map != NULL)
179894
 	{
179894
-	  /* Found a new last used index.  */
179894
-	  GL(dl_tls_max_dtv_idx) = idx;
179894
+	  /* Found a new last used index.  This can be read concurrently.  */
179894
+	  atomic_store_relaxed (&GL(dl_tls_max_dtv_idx), idx);
179894
 	  return true;
179894
 	}
179894
     }
179894
@@ -571,7 +572,9 @@ _dl_close_worker (struct link_map *map, bool force)
179894
 					GL(dl_tls_dtv_slotinfo_list), 0,
179894
 					imap->l_init_called))
179894
 		/* All dynamically loaded modules with TLS are unloaded.  */
179894
-		GL(dl_tls_max_dtv_idx) = GL(dl_tls_static_nelem);
179894
+		/* Can be read concurrently.  */
179894
+		atomic_store_relaxed (&GL(dl_tls_max_dtv_idx),
179894
+				      GL(dl_tls_static_nelem));
179894
 
179894
 	      if (imap->l_tls_offset != NO_TLS_OFFSET
179894
 		  && imap->l_tls_offset != FORCED_DYNAMIC_TLS_OFFSET)
179894
@@ -769,8 +772,11 @@ _dl_close_worker (struct link_map *map, bool force)
179894
   /* If we removed any object which uses TLS bump the generation counter.  */
179894
   if (any_tls)
179894
     {
179894
-      if (__glibc_unlikely (++GL(dl_tls_generation) == 0))
179894
+      size_t newgen = GL(dl_tls_generation) + 1;
179894
+      if (__glibc_unlikely (newgen == 0))
179894
 	_dl_fatal_printf ("TLS generation counter wrapped!  Please report as described in "REPORT_BUGS_TO".\n");
179894
+      /* Can be read concurrently.  */
179894
+      atomic_store_relaxed (&GL(dl_tls_generation), newgen);
179894
 
179894
       if (tls_free_end == GL(dl_tls_static_used))
179894
 	GL(dl_tls_static_used) = tls_free_start;
179894
diff --git a/elf/dl-open.c b/elf/dl-open.c
179894
index b052bb0bc2cd17aa..a67fb3aee40860e1 100644
179894
--- a/elf/dl-open.c
179894
+++ b/elf/dl-open.c
179894
@@ -395,9 +395,12 @@ update_tls_slotinfo (struct link_map *new)
179894
 	}
179894
     }
179894
 
179894
-  if (__builtin_expect (++GL(dl_tls_generation) == 0, 0))
179894
+  size_t newgen = GL(dl_tls_generation) + 1;
179894
+  if (__glibc_unlikely (newgen == 0))
179894
     _dl_fatal_printf (N_("\
179894
 TLS generation counter wrapped!  Please report this."));
179894
+  /* Can be read concurrently.  */
179894
+  atomic_store_relaxed (&GL(dl_tls_generation), newgen);
179894
 
179894
   /* We need a second pass for static tls data, because
179894
      _dl_update_slotinfo must not be run while calls to
179894
diff --git a/elf/dl-tls.c b/elf/dl-tls.c
179894
index da83cd6ae2ee6504..801eafad3961573c 100644
179894
--- a/elf/dl-tls.c
179894
+++ b/elf/dl-tls.c
179894
@@ -175,7 +175,9 @@ _dl_next_tls_modid (void)
179894
       /* No gaps, allocate a new entry.  */
179894
     nogaps:
179894
 
179894
-      result = ++GL(dl_tls_max_dtv_idx);
179894
+      result = GL(dl_tls_max_dtv_idx) + 1;
179894
+      /* Can be read concurrently.  */
179894
+      atomic_store_relaxed (&GL(dl_tls_max_dtv_idx), result);
179894
     }
179894
 
179894
   return result;
179894
@@ -359,10 +361,12 @@ allocate_dtv (void *result)
179894
   dtv_t *dtv;
179894
   size_t dtv_length;
179894
 
179894
+  /* Relaxed MO, because the dtv size is later rechecked, not relied on.  */
179894
+  size_t max_modid = atomic_load_relaxed (&GL(dl_tls_max_dtv_idx));
179894
   /* We allocate a few more elements in the dtv than are needed for the
179894
      initial set of modules.  This should avoid in most cases expansions
179894
      of the dtv.  */
179894
-  dtv_length = GL(dl_tls_max_dtv_idx) + DTV_SURPLUS;
179894
+  dtv_length = max_modid + DTV_SURPLUS;
179894
   dtv = calloc (dtv_length + 2, sizeof (dtv_t));
179894
   if (dtv != NULL)
179894
     {
179894
@@ -767,7 +771,7 @@ _dl_update_slotinfo (unsigned long int req_modid)
179894
 	      if (modid > max_modid)
179894
 		break;
179894
 
179894
-	      size_t gen = listp->slotinfo[cnt].gen;
179894
+	      size_t gen = atomic_load_relaxed (&listp->slotinfo[cnt].gen);
179894
 
179894
 	      if (gen > new_gen)
179894
 		/* Not relevant.  */
179894
@@ -779,7 +783,8 @@ _dl_update_slotinfo (unsigned long int req_modid)
179894
 		continue;
179894
 
179894
 	      /* If there is no map this means the entry is empty.  */
179894
-	      struct link_map *map = listp->slotinfo[cnt].map;
179894
+	      struct link_map *map
179894
+		= atomic_load_relaxed (&listp->slotinfo[cnt].map);
179894
 	      /* Check whether the current dtv array is large enough.  */
179894
 	      if (dtv[-1].counter < modid)
179894
 		{
179894
@@ -923,7 +928,12 @@ __tls_get_addr (GET_ADDR_ARGS)
179894
 {
179894
   dtv_t *dtv = THREAD_DTV ();
179894
 
179894
-  if (__glibc_unlikely (dtv[0].counter != GL(dl_tls_generation)))
179894
+  /* Update is needed if dtv[0].counter < the generation of the accessed
179894
+     module.  The global generation counter is used here as it is easier
179894
+     to check.  Synchronization for the relaxed MO access is guaranteed
179894
+     by user code, see CONCURRENCY NOTES in _dl_update_slotinfo.  */
179894
+  size_t gen = atomic_load_relaxed (&GL(dl_tls_generation));
179894
+  if (__glibc_unlikely (dtv[0].counter != gen))
179894
     return update_get_addr (GET_ADDR_PARAM);
179894
 
179894
   void *p = dtv[GET_ADDR_MODULE].pointer.val;
179894
@@ -946,7 +956,10 @@ _dl_tls_get_addr_soft (struct link_map *l)
179894
     return NULL;
179894
 
179894
   dtv_t *dtv = THREAD_DTV ();
179894
-  if (__glibc_unlikely (dtv[0].counter != GL(dl_tls_generation)))
179894
+  /* This may be called without holding the GL(dl_load_lock).  Reading
179894
+     arbitrary gen value is fine since this is best effort code.  */
179894
+  size_t gen = atomic_load_relaxed (&GL(dl_tls_generation));
179894
+  if (__glibc_unlikely (dtv[0].counter != gen))
179894
     {
179894
       /* This thread's DTV is not completely current,
179894
 	 but it might already cover this module.  */
179894
@@ -1032,7 +1045,9 @@ cannot create TLS data structures"));
179894
   /* Add the information into the slotinfo data structure.  */
179894
   if (do_add)
179894
     {
179894
-      listp->slotinfo[idx].map = l;
179894
-      listp->slotinfo[idx].gen = GL(dl_tls_generation) + 1;
179894
+      /* Can be read concurrently.  See _dl_update_slotinfo.  */
179894
+      atomic_store_relaxed (&listp->slotinfo[idx].map, l);
179894
+      atomic_store_relaxed (&listp->slotinfo[idx].gen,
179894
+			    GL(dl_tls_generation) + 1);
179894
     }
179894
 }
179894
diff --git a/sysdeps/x86_64/dl-tls.c b/sysdeps/x86_64/dl-tls.c
179894
index 533ee2b3a6e85ad8..bc543dcc264ea361 100644
179894
--- a/sysdeps/x86_64/dl-tls.c
179894
+++ b/sysdeps/x86_64/dl-tls.c
179894
@@ -40,7 +40,8 @@ __tls_get_addr_slow (GET_ADDR_ARGS)
179894
 {
179894
   dtv_t *dtv = THREAD_DTV ();
179894
 
179894
-  if (__glibc_unlikely (dtv[0].counter != GL(dl_tls_generation)))
179894
+  size_t gen = atomic_load_relaxed (&GL(dl_tls_generation));
179894
+  if (__glibc_unlikely (dtv[0].counter != gen))
179894
     return update_get_addr (GET_ADDR_PARAM);
179894
 
179894
   return tls_get_addr_tail (GET_ADDR_PARAM, dtv, NULL);