6c0556
commit 572bd547d57a39b6cf0ea072545dc4048921f4c3
6c0556
Author: Szabolcs Nagy <szabolcs.nagy@arm.com>
6c0556
Date:   Thu Dec 31 13:59:38 2020 +0000
6c0556
6c0556
    elf: Fix DTV gap reuse logic [BZ #27135]
6c0556
    
6c0556
    For some reason only dlopen failure caused dtv gaps to be reused.
6c0556
    
6c0556
    It is possible that the intent was to never reuse modids for a
6c0556
    different module, but after dlopen failure all gaps are reused
6c0556
    not just the ones caused by the unfinished dlopened.
6c0556
    
6c0556
    So the code has to handle reused modids already which seems to
6c0556
    work, however the data races at thread creation and tls access
6c0556
    (see bug 19329 and bug 27111) may be more severe if slots are
6c0556
    reused so this is scheduled after those fixes. I think fixing
6c0556
    the races are not simpler if reuse is disallowed and reuse has
6c0556
    other benefits, so set GL(dl_tls_dtv_gaps) whenever entries are
6c0556
    removed from the middle of the slotinfo list. The value does
6c0556
    not have to be correct: incorrect true value causes the next
6c0556
    modid query to do a slotinfo walk, incorrect false will leave
6c0556
    gaps and new entries are added at the end.
6c0556
    
6c0556
    Fixes bug 27135.
6c0556
    
6c0556
    Reviewed-by: Adhemerval Zanella  <adhemerval.zanella@linaro.org>
6c0556
6c0556
diff --git a/elf/dl-close.c b/elf/dl-close.c
6c0556
index 7d2dc2272cd643f5..41cb6c58491c364b 100644
6c0556
--- a/elf/dl-close.c
6c0556
+++ b/elf/dl-close.c
6c0556
@@ -88,7 +88,11 @@ remove_slotinfo (size_t idx, struct dtv_slotinfo_list *listp, size_t disp,
6c0556
       /* If this is not the last currently used entry no need to look
6c0556
 	 further.  */
6c0556
       if (idx != GL(dl_tls_max_dtv_idx))
6c0556
-	return true;
6c0556
+	{
6c0556
+	  /* There is an unused dtv entry in the middle.  */
6c0556
+	  GL(dl_tls_dtv_gaps) = true;
6c0556
+	  return true;
6c0556
+	}
6c0556
     }
6c0556
 
6c0556
   while (idx - disp > (disp == 0 ? 1 + GL(dl_tls_static_nelem) : 0))
6c0556
diff --git a/elf/dl-open.c b/elf/dl-open.c
6c0556
index a67fb3aee40860e1..54727402750f4c0c 100644
6c0556
--- a/elf/dl-open.c
6c0556
+++ b/elf/dl-open.c
6c0556
@@ -896,16 +896,6 @@ no more namespaces available for dlmopen()"));
6c0556
 	 state if relocation failed, for example.  */
6c0556
       if (args.map)
6c0556
 	{
6c0556
-	  /* Maybe some of the modules which were loaded use TLS.
6c0556
-	     Since it will be removed in the following _dl_close call
6c0556
-	     we have to mark the dtv array as having gaps to fill the
6c0556
-	     holes.  This is a pessimistic assumption which won't hurt
6c0556
-	     if not true.  There is no need to do this when we are
6c0556
-	     loading the auditing DSOs since TLS has not yet been set
6c0556
-	     up.  */
6c0556
-	  if ((mode & __RTLD_AUDIT) == 0)
6c0556
-	    GL(dl_tls_dtv_gaps) = true;
6c0556
-
6c0556
 	  _dl_close_worker (args.map, true);
6c0556
 
6c0556
 	  /* All l_nodelete_pending objects should have been deleted
6c0556
diff --git a/elf/dl-tls.c b/elf/dl-tls.c
6c0556
index 801eafad3961573c..bacb4101e2e2c4e5 100644
6c0556
--- a/elf/dl-tls.c
6c0556
+++ b/elf/dl-tls.c
6c0556
@@ -187,10 +187,7 @@ _dl_next_tls_modid (void)
6c0556
 size_t
6c0556
 _dl_count_modids (void)
6c0556
 {
6c0556
-  /* It is rare that we have gaps; see elf/dl-open.c (_dl_open) where
6c0556
-     we fail to load a module and unload it leaving a gap.  If we don't
6c0556
-     have gaps then the number of modids is the current maximum so
6c0556
-     return that.  */
6c0556
+  /* The count is the max unless dlclose or failed dlopen created gaps.  */
6c0556
   if (__glibc_likely (!GL(dl_tls_dtv_gaps)))
6c0556
     return GL(dl_tls_max_dtv_idx);
6c0556