548bcb
commit 86f65dffc2396d408beb628f1cad2b8f63e197bd
548bcb
Author: H.J. Lu <hjl.tools@gmail.com>
548bcb
Date:   Sun Jul 12 06:04:53 2020 -0700
548bcb
548bcb
    ld.so: Add --list-tunables to print tunable values
548bcb
    
548bcb
    Pass --list-tunables to ld.so to print tunables with min and max values.
548bcb
    
548bcb
    Reviewed-by: Adhemerval Zanella  <adhemerval.zanella@linaro.org>
548bcb
548bcb
Conflicts:
548bcb
	elf/Makefile
548bcb
	  (different backporting order)
548bcb
548bcb
diff --git a/elf/Makefile b/elf/Makefile
548bcb
index 3e71939d3234c4c3..aa65ec59f143bccf 100644
548bcb
--- a/elf/Makefile
548bcb
+++ b/elf/Makefile
548bcb
@@ -44,6 +44,10 @@ dl-routines += dl-tunables
548bcb
 tunables-type = $(addprefix TUNABLES_FRONTEND_,$(have-tunables))
548bcb
 CPPFLAGS-dl-tunables.c += -DTUNABLES_FRONTEND=$(tunables-type)
548bcb
 
548bcb
+ifeq (yesyes,$(build-shared)$(run-built-tests))
548bcb
+tests-special += $(objpfx)list-tunables.out
548bcb
+endif
548bcb
+
548bcb
 # Make sure that the compiler does not insert any library calls in tunables
548bcb
 # code paths.
548bcb
 ifeq (yes,$(have-loop-to-function))
548bcb
@@ -1825,6 +1829,13 @@ $(objpfx)tst-glibc-hwcaps-mask.out: \
548bcb
 # tst-glibc-hwcaps-cache.
548bcb
 $(objpfx)tst-glibc-hwcaps-cache.out: $(objpfx)tst-glibc-hwcaps
548bcb
 
548bcb
+$(objpfx)list-tunables.out: tst-rtld-list-tunables.sh $(objpfx)ld.so
548bcb
+	$(SHELL) $< $(objpfx)ld.so '$(test-wrapper-env)' \
548bcb
+	    '$(run_program_env)' > $(objpfx)/tst-rtld-list-tunables.out
548bcb
+	cmp tst-rtld-list-tunables.exp \
548bcb
+	    $(objpfx)/tst-rtld-list-tunables.out > $@; \
548bcb
+	$(evaluate-test)
548bcb
+
548bcb
 tst-dst-static-ENV = LD_LIBRARY_PATH='$$ORIGIN'
548bcb
 
548bcb
 $(objpfx)tst-rtld-help.out: $(objpfx)ld.so
548bcb
diff --git a/elf/dl-main.h b/elf/dl-main.h
548bcb
index 566713a0d10cfdb7..9e7b51d8f010e904 100644
548bcb
--- a/elf/dl-main.h
548bcb
+++ b/elf/dl-main.h
548bcb
@@ -63,7 +63,7 @@ struct audit_list
548bcb
 enum rtld_mode
548bcb
   {
548bcb
     rtld_mode_normal, rtld_mode_list, rtld_mode_verify, rtld_mode_trace,
548bcb
-    rtld_mode_help,
548bcb
+    rtld_mode_list_tunables, rtld_mode_help,
548bcb
   };
548bcb
 
548bcb
 /* Aggregated state information extracted from environment variables
548bcb
diff --git a/elf/dl-tunables.c b/elf/dl-tunables.c
548bcb
index bbc3679e3564a766..3c84809d44381241 100644
548bcb
--- a/elf/dl-tunables.c
548bcb
+++ b/elf/dl-tunables.c
548bcb
@@ -26,6 +26,7 @@
548bcb
 #include <sysdep.h>
548bcb
 #include <fcntl.h>
548bcb
 #include <ldsodefs.h>
548bcb
+#include <array_length.h>
548bcb
 
548bcb
 #define TUNABLES_INTERNAL 1
548bcb
 #include "dl-tunables.h"
548bcb
@@ -359,6 +360,48 @@ __tunables_init (char **envp)
548bcb
     }
548bcb
 }
548bcb
 
548bcb
+void
548bcb
+__tunables_print (void)
548bcb
+{
548bcb
+  for (int i = 0; i < array_length (tunable_list); i++)
548bcb
+    {
548bcb
+      const tunable_t *cur = &tunable_list[i];
548bcb
+      if (cur->type.type_code == TUNABLE_TYPE_STRING
548bcb
+	  && cur->val.strval == NULL)
548bcb
+	_dl_printf ("%s:\n", cur->name);
548bcb
+      else
548bcb
+	{
548bcb
+	  _dl_printf ("%s: ", cur->name);
548bcb
+	  switch (cur->type.type_code)
548bcb
+	    {
548bcb
+	    case TUNABLE_TYPE_INT_32:
548bcb
+	      _dl_printf ("%d (min: %d, max: %d)\n",
548bcb
+			  (int) cur->val.numval,
548bcb
+			  (int) cur->type.min,
548bcb
+			  (int) cur->type.max);
548bcb
+	      break;
548bcb
+	    case TUNABLE_TYPE_UINT_64:
548bcb
+	      _dl_printf ("0x%lx (min: 0x%lx, max: 0x%lx)\n",
548bcb
+			  (long int) cur->val.numval,
548bcb
+			  (long int) cur->type.min,
548bcb
+			  (long int) cur->type.max);
548bcb
+	      break;
548bcb
+	    case TUNABLE_TYPE_SIZE_T:
548bcb
+	      _dl_printf ("0x%Zx (min: 0x%Zx, max: 0x%Zx)\n",
548bcb
+			  (size_t) cur->val.numval,
548bcb
+			  (size_t) cur->type.min,
548bcb
+			  (size_t) cur->type.max);
548bcb
+	      break;
548bcb
+	    case TUNABLE_TYPE_STRING:
548bcb
+	      _dl_printf ("%s\n", cur->val.strval);
548bcb
+	      break;
548bcb
+	    default:
548bcb
+	      __builtin_unreachable ();
548bcb
+	    }
548bcb
+	}
548bcb
+    }
548bcb
+}
548bcb
+
548bcb
 /* Set the tunable value.  This is called by the module that the tunable exists
548bcb
    in. */
548bcb
 void
548bcb
diff --git a/elf/dl-tunables.h b/elf/dl-tunables.h
548bcb
index 7f181f3316cd9fc1..f4f2cfaeb9828599 100644
548bcb
--- a/elf/dl-tunables.h
548bcb
+++ b/elf/dl-tunables.h
548bcb
@@ -69,9 +69,11 @@ typedef struct _tunable tunable_t;
548bcb
 # include "dl-tunable-list.h"
548bcb
 
548bcb
 extern void __tunables_init (char **);
548bcb
+extern void __tunables_print (void);
548bcb
 extern void __tunable_get_val (tunable_id_t, void *, tunable_callback_t);
548bcb
 extern void __tunable_set_val (tunable_id_t, void *);
548bcb
 rtld_hidden_proto (__tunables_init)
548bcb
+rtld_hidden_proto (__tunables_print)
548bcb
 rtld_hidden_proto (__tunable_get_val)
548bcb
 
548bcb
 /* Define TUNABLE_GET and TUNABLE_SET in short form if TOP_NAMESPACE and
548bcb
diff --git a/elf/dl-usage.c b/elf/dl-usage.c
548bcb
index e22a9c39427187d1..908b4894b3014b2d 100644
548bcb
--- a/elf/dl-usage.c
548bcb
+++ b/elf/dl-usage.c
548bcb
@@ -255,7 +255,12 @@ setting environment variables (which would be inherited by subprocesses).\n\
548bcb
                         in LIST\n\
548bcb
   --audit LIST          use objects named in LIST as auditors\n\
548bcb
   --preload LIST        preload objects named in LIST\n\
548bcb
-  --argv0 STRING        set argv[0] to STRING before running\n\
548bcb
+  --argv0 STRING        set argv[0] to STRING before running\n"
548bcb
+#if HAVE_TUNABLES
548bcb
+"\
548bcb
+  --list-tunables       list all tunables with minimum and maximum values\n"
548bcb
+#endif
548bcb
+"\
548bcb
   --help                display this help and exit\n\
548bcb
   --version             output version information and exit\n\
548bcb
 \n\
548bcb
diff --git a/elf/rtld.c b/elf/rtld.c
548bcb
index 9e09896da078274d..54b621ec5ca014fa 100644
548bcb
--- a/elf/rtld.c
548bcb
+++ b/elf/rtld.c
548bcb
@@ -47,6 +47,7 @@
548bcb
 #include <libc-early-init.h>
548bcb
 #include <dl-main.h>
548bcb
 #include <gnu/lib-names.h>
548bcb
+#include <dl-tunables.h>
548bcb
 
548bcb
 #include <assert.h>
548bcb
 
548bcb
@@ -1262,6 +1263,16 @@ dl_main (const ElfW(Phdr) *phdr,
548bcb
 	    _dl_argc -= 2;
548bcb
 	    _dl_argv += 2;
548bcb
 	  }
548bcb
+#if HAVE_TUNABLES
548bcb
+	else if (! strcmp (_dl_argv[1], "--list-tunables"))
548bcb
+	  {
548bcb
+	    state.mode = rtld_mode_list_tunables;
548bcb
+
548bcb
+	    ++_dl_skip_args;
548bcb
+	    --_dl_argc;
548bcb
+	    ++_dl_argv;
548bcb
+	  }
548bcb
+#endif
548bcb
 	else if (strcmp (_dl_argv[1], "--help") == 0)
548bcb
 	  {
548bcb
 	    state.mode = rtld_mode_help;
548bcb
@@ -1282,6 +1293,14 @@ dl_main (const ElfW(Phdr) *phdr,
548bcb
 	else
548bcb
 	  break;
548bcb
 
548bcb
+#if HAVE_TUNABLES
548bcb
+      if (__glibc_unlikely (state.mode == rtld_mode_list_tunables))
548bcb
+	{
548bcb
+	  __tunables_print ();
548bcb
+	  _exit (0);
548bcb
+	}
548bcb
+#endif
548bcb
+
548bcb
       /* If we have no further argument the program was called incorrectly.
548bcb
 	 Grant the user some education.  */
548bcb
       if (_dl_argc < 2)
548bcb
diff --git a/elf/tst-rtld-list-tunables.exp b/elf/tst-rtld-list-tunables.exp
548bcb
new file mode 100644
548bcb
index 0000000000000000..4f3f7ee4e30a2b42
548bcb
--- /dev/null
548bcb
+++ b/elf/tst-rtld-list-tunables.exp
548bcb
@@ -0,0 +1,14 @@
548bcb
+glibc.malloc.arena_max: 0x0 (min: 0x1, max: 0x[f]+)
548bcb
+glibc.malloc.arena_test: 0x0 (min: 0x1, max: 0x[f]+)
548bcb
+glibc.malloc.check: 0 (min: 0, max: 3)
548bcb
+glibc.malloc.mmap_max: 0 (min: -2147483648, max: 2147483647)
548bcb
+glibc.malloc.mmap_threshold: 0x0 (min: 0x0, max: 0x[f]+)
548bcb
+glibc.malloc.mxfast: 0x0 (min: 0x0, max: 0x[f]+)
548bcb
+glibc.malloc.perturb: 0 (min: 0, max: 255)
548bcb
+glibc.malloc.tcache_count: 0x0 (min: 0x0, max: 0x[f]+)
548bcb
+glibc.malloc.tcache_max: 0x0 (min: 0x0, max: 0x[f]+)
548bcb
+glibc.malloc.tcache_unsorted_limit: 0x0 (min: 0x0, max: 0x[f]+)
548bcb
+glibc.malloc.top_pad: 0x0 (min: 0x0, max: 0x[f]+)
548bcb
+glibc.malloc.trim_threshold: 0x0 (min: 0x0, max: 0x[f]+)
548bcb
+glibc.rtld.nns: 0x4 (min: 0x1, max: 0x10)
548bcb
+glibc.rtld.optional_static_tls: 0x200 (min: 0x0, max: 0x[f]+)
548bcb
diff --git a/elf/tst-rtld-list-tunables.sh b/elf/tst-rtld-list-tunables.sh
548bcb
new file mode 100755
548bcb
index 0000000000000000..e7bbdde94952b872
548bcb
--- /dev/null
548bcb
+++ b/elf/tst-rtld-list-tunables.sh
548bcb
@@ -0,0 +1,34 @@
548bcb
+#!/bin/sh
548bcb
+# Test for --list-tunables option ld.so.
548bcb
+# Copyright (C) 2021 Free Software Foundation, Inc.
548bcb
+# This file is part of the GNU C Library.
548bcb
+#
548bcb
+# The GNU C Library is free software; you can redistribute it and/or
548bcb
+# modify it under the terms of the GNU Lesser General Public
548bcb
+# License as published by the Free Software Foundation; either
548bcb
+# version 2.1 of the License, or (at your option) any later version.
548bcb
+#
548bcb
+# The GNU C Library is distributed in the hope that it will be useful,
548bcb
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
548bcb
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
548bcb
+# Lesser General Public License for more details.
548bcb
+#
548bcb
+# You should have received a copy of the GNU Lesser General Public
548bcb
+# License along with the GNU C Library; if not, see
548bcb
+# <https://www.gnu.org/licenses/>.
548bcb
+
548bcb
+set -e
548bcb
+
548bcb
+rtld=$1
548bcb
+test_wrapper_env=$2
548bcb
+run_program_env=$3
548bcb
+
548bcb
+LC_ALL=C
548bcb
+export LC_ALL
548bcb
+
548bcb
+${test_wrapper_env} \
548bcb
+${run_program_env} \
548bcb
+$rtld --list-tunables \
548bcb
+| sort -u \
548bcb
+| egrep "(rtld|malloc)" \
548bcb
+| sed -e "s/0xf\+/0x[f]+/"
548bcb
diff --git a/manual/tunables.texi b/manual/tunables.texi
548bcb
index 07887981748bc44b..43272cf885d1e3e6 100644
548bcb
--- a/manual/tunables.texi
548bcb
+++ b/manual/tunables.texi
548bcb
@@ -28,6 +28,44 @@ Finally, the set of tunables available may vary between distributions as
548bcb
 the tunables feature allows distributions to add their own tunables under
548bcb
 their own namespace.
548bcb
 
548bcb
+Passing @option{--list-tunables} to the dynamic loader to print all
548bcb
+tunables with minimum and maximum values:
548bcb
+
548bcb
+@example
548bcb
+$ /lib64/ld-linux-x86-64.so.2 --list-tunables
548bcb
+glibc.rtld.nns: 0x4 (min: 0x1, max: 0x10)
548bcb
+glibc.elision.skip_lock_after_retries: 3 (min: -2147483648, max: 2147483647)
548bcb
+glibc.malloc.trim_threshold: 0x0 (min: 0x0, max: 0xffffffffffffffff)
548bcb
+glibc.malloc.perturb: 0 (min: 0, max: 255)
548bcb
+glibc.cpu.x86_shared_cache_size: 0x100000 (min: 0x0, max: 0xffffffffffffffff)
548bcb
+glibc.mem.tagging: 0 (min: 0, max: 255)
548bcb
+glibc.elision.tries: 3 (min: -2147483648, max: 2147483647)
548bcb
+glibc.elision.enable: 0 (min: 0, max: 1)
548bcb
+glibc.cpu.x86_rep_movsb_threshold: 0x1000 (min: 0x100, max: 0xffffffffffffffff)
548bcb
+glibc.malloc.mxfast: 0x0 (min: 0x0, max: 0xffffffffffffffff)
548bcb
+glibc.elision.skip_lock_busy: 3 (min: -2147483648, max: 2147483647)
548bcb
+glibc.malloc.top_pad: 0x0 (min: 0x0, max: 0xffffffffffffffff)
548bcb
+glibc.cpu.x86_rep_stosb_threshold: 0x800 (min: 0x1, max: 0xffffffffffffffff)
548bcb
+glibc.cpu.x86_non_temporal_threshold: 0xc0000 (min: 0x0, max: 0xffffffffffffffff)
548bcb
+glibc.cpu.x86_shstk:
548bcb
+glibc.cpu.hwcap_mask: 0x6 (min: 0x0, max: 0xffffffffffffffff)
548bcb
+glibc.malloc.mmap_max: 0 (min: -2147483648, max: 2147483647)
548bcb
+glibc.elision.skip_trylock_internal_abort: 3 (min: -2147483648, max: 2147483647)
548bcb
+glibc.malloc.tcache_unsorted_limit: 0x0 (min: 0x0, max: 0xffffffffffffffff)
548bcb
+glibc.cpu.x86_ibt:
548bcb
+glibc.cpu.hwcaps:
548bcb
+glibc.elision.skip_lock_internal_abort: 3 (min: -2147483648, max: 2147483647)
548bcb
+glibc.malloc.arena_max: 0x0 (min: 0x1, max: 0xffffffffffffffff)
548bcb
+glibc.malloc.mmap_threshold: 0x0 (min: 0x0, max: 0xffffffffffffffff)
548bcb
+glibc.cpu.x86_data_cache_size: 0x8000 (min: 0x0, max: 0xffffffffffffffff)
548bcb
+glibc.malloc.tcache_count: 0x0 (min: 0x0, max: 0xffffffffffffffff)
548bcb
+glibc.malloc.arena_test: 0x0 (min: 0x1, max: 0xffffffffffffffff)
548bcb
+glibc.pthread.mutex_spin_count: 100 (min: 0, max: 32767)
548bcb
+glibc.rtld.optional_static_tls: 0x200 (min: 0x0, max: 0xffffffffffffffff)
548bcb
+glibc.malloc.tcache_max: 0x0 (min: 0x0, max: 0xffffffffffffffff)
548bcb
+glibc.malloc.check: 0 (min: 0, max: 3)
548bcb
+@end example
548bcb
+
548bcb
 @menu
548bcb
 * Tunable names::  The structure of a tunable name
548bcb
 * Memory Allocation Tunables::  Tunables in the memory allocation subsystem