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