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