Description:  Improve performance of dynamic loader for deeply nested DSO dependencies
 TODO: 
 .
 glibc (2.23-0ubuntu11) xenial; urgency=medium
 .
   * debian/patches/ubuntu/xsave-part1.diff and
     debian/patches/ubuntu/xsave-part2.diff: Fix a serious performance
     regression when mixing SSE and AVX code on certain processors.
     The patches are from the upstream 2.23 stable branch. (LP: #1663280)
Author: Daniel Axtens <daniel.axtens@canonical.com>
Bug-Ubuntu: https://bugs.launchpad.net/bugs/1663280

---
The information above should follow the Patch Tagging Guidelines, please
checkout http://dep.debian.net/deps/dep3/ to learn about the format. Here
are templates for supplementary fields that you might want to add:

Origin: <vendor|upstream|other>, <url of original patch>
Bug: <url in upstream bugtracker>
Bug-Debian: https://bugs.debian.org/<bugnumber>
Bug-Ubuntu: https://launchpad.net/bugs/<bugnumber>
Forwarded: <no|not-needed|url proving that it has been forwarded>
Reviewed-By: <name and email of someone who approved the patch>
Last-Update: <YYYY-MM-DD>

--- glibc-2.23.orig/elf/dl-deps.c
+++ glibc-2.23/elf/dl-deps.c
@@ -42,6 +42,11 @@
 
 /* When loading auxiliary objects we must ignore errors.  It's ok if
    an object is missing.  */
+void
+_dl_sort_maps (struct link_map **maps, size_t nmaps);
+static void
+weight (struct link_map **maps, size_t nmaps, char *seen, unsigned int *w);
+
 struct openaux_args
   {
     /* The arguments to openaux.  */
@@ -611,64 +616,12 @@ Filters not supported with LD_TRACE_PREL
   memcpy (l_initfini, map->l_searchlist.r_list,
 	  nlist * sizeof (struct link_map *));
   if (__glibc_likely (nlist > 1))
-    {
-      /* We can skip looking for the binary itself which is at the front
-	 of the search list.  */
-      i = 1;
-      uint16_t seen[nlist];
-      memset (seen, 0, nlist * sizeof (seen[0]));
-      while (1)
-	{
-	  /* Keep track of which object we looked at this round.  */
-	  ++seen[i];
-	  struct link_map *thisp = l_initfini[i];
-
-	  /* Find the last object in the list for which the current one is
-	     a dependency and move the current object behind the object
-	     with the dependency.  */
-	  unsigned int k = nlist - 1;
-	  while (k > i)
-	    {
-	      struct link_map **runp = l_initfini[k]->l_initfini;
-	      if (runp != NULL)
-		/* Look through the dependencies of the object.  */
-		while (*runp != NULL)
-		  if (__glibc_unlikely (*runp++ == thisp))
-		    {
-		      /* Move the current object to the back past the last
-			 object with it as the dependency.  */
-		      memmove (&l_initfini[i], &l_initfini[i + 1],
-			       (k - i) * sizeof (l_initfini[0]));
-		      l_initfini[k] = thisp;
-
-		      if (seen[i + 1] > nlist - i)
-			{
-			  ++i;
-			  goto next_clear;
-			}
-
-		      uint16_t this_seen = seen[i];
-		      memmove (&seen[i], &seen[i + 1],
-			       (k - i) * sizeof (seen[0]));
-		      seen[k] = this_seen;
-
-		      goto next;
-		    }
-
-	      --k;
-	    }
-
-	  if (++i == nlist)
-	    break;
-	next_clear:
-	  memset (&seen[i], 0, (nlist - i) * sizeof (seen[0]));
-
-	next:;
-	}
-    }
-
+    _dl_sort_maps (l_initfini, nlist );
   /* Terminate the list of dependencies.  */
   l_initfini[nlist] = NULL;
+  /*for (int tt=0; t<nlist ; t++) {
+	_dl_debug_printf("The library: %s\n",l_initfini[nlist]->l_name);
+  }*/
   atomic_write_barrier ();
   map->l_initfini = l_initfini;
   map->l_free_initfini = 1;
@@ -686,3 +639,78 @@ Filters not supported with LD_TRACE_PREL
     _dl_signal_error (errno_reason == -1 ? 0 : errno_reason, objname,
 		      NULL, errstring);
 }
+
+void
+_dl_sort_maps (struct link_map **maps, size_t nmaps)
+{
+  int c, i, k;
+  unsigned int w[nmaps];
+  char seen[nmaps];
+  struct link_map *t;
+  memset(seen, 0, nmaps);
+  /* Use the l_idx field as an unique identifier. */
+  for (i = 1; i < nmaps; ++i)
+    {
+      /* Ensure to not touch a link_map with l_idx < 0 as it is
+         used to flag a dso is being unloaded.  */
+      if (maps[i]->l_idx >= 0)
+	maps[i]->l_idx = i;
+    }
+  /* The list is sorted in place, with entries added from end to start. */
+  weight(maps, k = nmaps, seen, w);
+  c = 0;
+  while (c < k)
+    {
+      for (i = k - 1; i >= 1; --i)
+        {
+	  if (w[i] == c && maps[i]->l_idx >= 0)
+	    {
+	      seen[maps[i]->l_idx] = 1;
+	      /* Reducing the number of entries or swaping entries
+		 require remaking the {w}eight map. */
+	      if (--k != i)
+		{
+		  t = maps[k];
+		  maps[k] = maps[i];
+		  maps[i] = t;
+		}
+	      weight(maps, k, seen, w);
+	      c = -1;
+	      break;
+	    }
+	}
+      /* If c != 0, there are cycles, and the first entry in the map,
+	 with the lowest reference {c}ount is removed attempting to
+	 to break the cycle. */
+      ++c;
+    }
+
+}
+static void
+weight (struct link_map **maps, size_t nmaps, char *seen, unsigned int *w)
+{
+  int i, c;
+  struct link_map *m, **r;
+  for (i = 1; i < nmaps; ++i)
+    {
+      m = maps[i];
+      if ((r = m->l_initfini))
+        {
+	  c = 0;
+	  while (*r)
+	    {
+	      /* If not a self reference, not being unloaded, and not
+	         already removed from the list, increment the reference
+	         counter. */
+	      if (*r != m && (*r)->l_idx >= 0 && !seen[(*r)->l_idx])
+		++c;
+	      ++r;
+	    }
+	  /* Update the {w}eight vector with {c}ount map(s) dependency */
+	  w[i] = c;
+	}
+      else
+	w[i] = 0;
+    }
+}
+
--- glibc-2.23.orig/elf/dl-fini.c
+++ glibc-2.23/elf/dl-fini.c
@@ -23,104 +23,110 @@
 
 /* Type of the constructor functions.  */
 typedef void (*fini_t) (void);
-
-
-void
-internal_function
-_dl_sort_fini (struct link_map **maps, size_t nmaps, char *used, Lmid_t ns)
+static void weight (struct link_map **maps, size_t nmaps, char *seen, unsigned int *w, int from)
 {
-  /* A list of one element need not be sorted.  */
-  if (nmaps == 1)
-    return;
+	int i, c;
+	struct link_map *m, **r;
 
-  /* We can skip looking for the binary itself which is at the front
-     of the search list for the main namespace.  */
-  unsigned int i = ns == LM_ID_BASE;
-  uint16_t seen[nmaps];
-  memset (seen, 0, nmaps * sizeof (seen[0]));
-  while (1)
-    {
-      /* Keep track of which object we looked at this round.  */
-      ++seen[i];
-      struct link_map *thisp = maps[i];
-
-      /* Do not handle ld.so in secondary namespaces and object which
-	 are not removed.  */
-      if (thisp != thisp->l_real || thisp->l_idx == -1)
-	goto skip;
-
-      /* Find the last object in the list for which the current one is
-	 a dependency and move the current object behind the object
-	 with the dependency.  */
-      unsigned int k = nmaps - 1;
-      while (k > i)
+	/* Start at 1 because the first entry is the binary itself. */
+	for (i = from; i < nmaps; ++i)
 	{
-	  struct link_map **runp = maps[k]->l_initfini;
-	  if (runp != NULL)
-	    /* Look through the dependencies of the object.  */
-	    while (*runp != NULL)
-	      if (__glibc_unlikely (*runp++ == thisp))
+		m = maps[i];
+		if ((r = m->l_initfini))
 		{
-		move:
-		  /* Move the current object to the back past the last
-		     object with it as the dependency.  */
-		  memmove (&maps[i], &maps[i + 1],
-			   (k - i) * sizeof (maps[0]));
-		  maps[k] = thisp;
-
-		  if (used != NULL)
-		    {
-		      char here_used = used[i];
-		      memmove (&used[i], &used[i + 1],
-			       (k - i) * sizeof (used[0]));
-		      used[k] = here_used;
-		    }
-
-		  if (seen[i + 1] > nmaps - i)
-		    {
-		      ++i;
-		      goto next_clear;
-		    }
-
-		  uint16_t this_seen = seen[i];
-		  memmove (&seen[i], &seen[i + 1], (k - i) * sizeof (seen[0]));
-		  seen[k] = this_seen;
-
-		  goto next;
+			c = 0;
+			while (*r)
+			{
+				/* If not a self reference, not being unloaded, and not
+				already removed from the list, increment the reference
+				counter. */
+				if (*r != m && (*r)->l_idx >= 0 && !seen[(*r)->l_idx])
+					++c;
+				++r;
+			}
+			/* Update the {w}eight vector with {c}ount map(s) dependency */
+			w[i] = c;
 		}
+		else
+			w[i] = 0;
+	}
+}
+
+
+static void sort (struct link_map **maps, size_t nmaps, char *used, Lmid_t ns)
+{
+	int from;
+	int c, i, k;
+	unsigned int w[nmaps];
+	char seen[nmaps];
+	struct link_map *t;
+
+	memset(seen, 0, nmaps);
 
-	  if (__glibc_unlikely (maps[k]->l_reldeps != NULL))
-	    {
-	      unsigned int m = maps[k]->l_reldeps->act;
-	      struct link_map **relmaps = &maps[k]->l_reldeps->list[0];
-
-	      /* Look through the relocation dependencies of the object.  */
-	      while (m-- > 0)
-		if (__glibc_unlikely (relmaps[m] == thisp))
-		  {
-		    /* If a cycle exists with a link time dependency,
-		       preserve the latter.  */
-		    struct link_map **runp = thisp->l_initfini;
-		    if (runp != NULL)
-		      while (*runp != NULL)
-			if (__glibc_unlikely (*runp++ == maps[k]))
-			  goto ignore;
-		    goto move;
-		  }
-	    ignore:;
-	    }
+	/* Use the l_idx field as an unique identifier. */
+	for (i = 0; i < nmaps; ++i)
+	{
+		/* Ensure to not touch a link_map with l_idx < 0 as it is
+		used to flag a dso is being unloaded.  */
+		if (maps[i]->l_idx >= 0)
+			maps[i]->l_idx = i;
+	}
+	/* We can skip looking for the binary itself which is at the front
+	of the search list for the main namespace.  */
 
-	  --k;
+	from = ns == LM_ID_BASE;
+	if (from)
+	{
+		w[0] = 0;
+		seen[0] = 1;
 	}
 
-    skip:
-      if (++i == nmaps)
-	break;
-    next_clear:
-      memset (&seen[i], 0, (nmaps - i) * sizeof (seen[0]));
+	/* The list is sorted in place, with entries added from end to start. */
+	weight(maps, k = nmaps, seen, w, from);
+	c = 0;
+	while (c < k)
+	{
+		for (i = k - 1; i >= from; --i)
+		{
+			t = maps[i];
+			/* Do not handle ld.so in secondary namespaces and object which
+			are not removed.  */
+			if (w[i] == c && maps[i]->l_idx >= 0 && t->l_real == t)
+			{
+				seen[maps[i]->l_idx] = 1;
+				/* Reducing the number of entries or swaping entries
+				require remaking the {w}eight map. */
+				if (--k != i)
+				{
+					maps[i] = maps[k];
+					maps[k] = t;
+					if (used)
+					{
+						char c = used[i];
+						used[i] = used[k];
+						used[k] = c;
+					}	  
+				}
+				weight(maps, k, seen, w, from);
+				c=-1;
+				break;
+			}
+		}
+		/* If c != 0, there are cycles, and the first entry in the map,
+		with the lowest reference {c}ount is removed attempting to
+		to break the cycle. */
+		++c;
+	}
+}
 
-    next:;
-    }
+void
+internal_function
+_dl_sort_fini (struct link_map **maps, size_t nmaps, char *used, Lmid_t ns)
+{
+  /* A list of one element need not be sorted.  */
+  if (nmaps == 1)
+    return;
+  sort (maps, nmaps, used, ns);
 }
 
 
