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