V4 [PATCH 2/2] ld.so: Add --list-tunables to print tunable values

Message ID 20201031154437.2689427-3-hjl.tools@gmail.com
State Superseded
Delegated to: Florian Weimer
Headers
Series V4 [PATCH 2/2] ld.so: Add --list-tunables to print tunable values |

Commit Message

H.J. Lu Oct. 31, 2020, 3:44 p.m. UTC
  Pass --list-tunables to ld.so to print tunables with min and max values.
---
 NEWS                 |  2 ++
 elf/Makefile         |  8 ++++++++
 elf/dl-main.h        |  2 +-
 elf/dl-tunables.c    | 36 ++++++++++++++++++++++++++++++++++++
 elf/dl-tunables.h    |  2 ++
 elf/dl-usage.c       |  7 ++++++-
 elf/rtld.c           | 23 +++++++++++++++++++++++
 manual/tunables.texi | 37 +++++++++++++++++++++++++++++++++++++
 8 files changed, 115 insertions(+), 2 deletions(-)
  

Comments

Adhemerval Zanella Netto Jan. 14, 2021, 6:35 p.m. UTC | #1
On 31/10/2020 12:44, H.J. Lu via Libc-alpha wrote:
> Pass --list-tunables to ld.so to print tunables with min and max values.
> ---
>  NEWS                 |  2 ++
>  elf/Makefile         |  8 ++++++++
>  elf/dl-main.h        |  2 +-
>  elf/dl-tunables.c    | 36 ++++++++++++++++++++++++++++++++++++
>  elf/dl-tunables.h    |  2 ++
>  elf/dl-usage.c       |  7 ++++++-
>  elf/rtld.c           | 23 +++++++++++++++++++++++
>  manual/tunables.texi | 37 +++++++++++++++++++++++++++++++++++++
>  8 files changed, 115 insertions(+), 2 deletions(-)
> 
> diff --git a/NEWS b/NEWS
> index 4307c4b1b0..a62e7307ef 100644
> --- a/NEWS
> +++ b/NEWS
> @@ -9,6 +9,8 @@ Version 2.33
>  
>  Major new features:
>  
> +* Pass --list-tunables to ld.so to print tunable values.
> +

Maybe use a similar sentence as for argv0:

  * The dynamic linker accepts the --list-tunables argument which prints
    all the supported tunables.  This option is disable is glibc is
    configured with tunables disabled (--enable-tunables=no).

>  * The dynamic linker accepts the --argv0 argument and provides opportunity
>    to change argv[0] string.
>  
> diff --git a/elf/Makefile b/elf/Makefile
> index f10cc59e7c..86b282a32b 100644
> --- a/elf/Makefile
> +++ b/elf/Makefile
> @@ -44,6 +44,10 @@ dl-routines += dl-tunables
>  tunables-type = $(addprefix TUNABLES_FRONTEND_,$(have-tunables))
>  CPPFLAGS-dl-tunables.c += -DTUNABLES_FRONTEND=$(tunables-type)
>  
> +ifeq (yesyes,$(build-shared)$(run-built-tests))
> +tests-special += $(objpfx)list-tunables.out
> +endif
> +
>  # Make sure that the compiler does not insert any library calls in tunables
>  # code paths.

Ok, it is enabled iff 'ifneq (no,$(have-tunables))'.

>  ifeq (yes,$(have-loop-to-function))
> @@ -1812,3 +1816,7 @@ $(objpfx)argv0test.out: tst-rtld-argv0.sh $(objpfx)ld.so \
>              '$(test-wrapper-env)' '$(run_program_env)' \
>              '$(rpath-link)' 'test-argv0' > $@; \
>      $(evaluate-test)
> +
> +$(objpfx)list-tunables.out: $(objpfx)ld.so
> +	$(objpfx)ld.so --list-tunables > $@; \
> +	$(evaluate-test)

Maybe at least check for a tunable with a default value which be
equal to all architecture, like glibc.rtld.nns, glibc.malloc.perturb, etc?

> diff --git a/elf/dl-main.h b/elf/dl-main.h
> index b51256d3b4..f229867b8e 100644
> --- a/elf/dl-main.h
> +++ b/elf/dl-main.h
> @@ -63,7 +63,7 @@ struct audit_list
>  enum rtld_mode
>    {
>      rtld_mode_normal, rtld_mode_list, rtld_mode_verify, rtld_mode_trace,
> -    rtld_mode_help,
> +    rtld_mode_list_tunables, rtld_mode_help,
>    };
>  
>  /* Aggregated state information extracted from environment variables

Ok.

> diff --git a/elf/dl-tunables.c b/elf/dl-tunables.c
> index 2ba2844075..048601b704 100644
> --- a/elf/dl-tunables.c
> +++ b/elf/dl-tunables.c
> @@ -398,6 +398,42 @@ __tunables_init (char **envp)
>      }
>  }
>  
> +void
> +__tunables_print (void)
> +{
> +  for (int i = 0; i < sizeof (tunable_list) / sizeof (tunable_t); i++)

Use array_length here.

> +    {
> +      tunable_t *cur = &tunable_list[i];

Maybe use a const modifier here.

> +      _dl_printf ("%s: ", cur->name);
> +      switch (cur->type.type_code)
> +	{
> +	case TUNABLE_TYPE_INT_32:
> +	  _dl_printf ("%d (min: %d, max: %d)\n",
> +		      (int) cur->val.numval,
> +		      (int) cur->type.min,
> +		      (int) cur->type.max);
> +	  break;
> +	case TUNABLE_TYPE_UINT_64:
> +	  _dl_printf ("0x%lx (min: 0x%lx, max: 0x%lx)\n",
> +		      (long int) cur->val.numval,
> +		      (long int) cur->type.min,
> +		      (long int) cur->type.max);
> +	  break;
> +	case TUNABLE_TYPE_SIZE_T:
> +	  _dl_printf ("0x%Zx (min: 0x%Zx, max: 0x%Zx)\n",
> +		      (size_t) cur->val.numval,
> +		      (size_t) cur->type.min,
> +		      (size_t) cur->type.max);
> +	  break;
> +	case TUNABLE_TYPE_STRING:
> +	  _dl_printf ("%s\n", cur->val.strval ? cur->val.strval : "");
> +	  break;
> +	default:
> +	  __builtin_unreachable ();
> +	}
> +    }
> +}
> +

Ok, it should be ok to use '%d' for signed and 'lx' for unsigned.

>  /* Set the tunable value.  This is called by the module that the tunable exists
>     in. */
>  void
> diff --git a/elf/dl-tunables.h b/elf/dl-tunables.h
> index 550b0cc7f4..4a0c90f3e0 100644
> --- a/elf/dl-tunables.h
> +++ b/elf/dl-tunables.h
> @@ -69,9 +69,11 @@ typedef struct _tunable tunable_t;
>  # include "dl-tunable-list.h"
>  
>  extern void __tunables_init (char **);
> +extern void __tunables_print (void);
>  extern void __tunable_get_val (tunable_id_t, void *, tunable_callback_t);
>  extern void __tunable_set_val (tunable_id_t, void *, void *, void *);
>  rtld_hidden_proto (__tunables_init)
> +rtld_hidden_proto (__tunables_print)
>  rtld_hidden_proto (__tunable_get_val)
>  rtld_hidden_proto (__tunable_set_val)
>  

Ok.

> diff --git a/elf/dl-usage.c b/elf/dl-usage.c
> index 796ad38b43..3ce19fb892 100644
> --- a/elf/dl-usage.c
> +++ b/elf/dl-usage.c
> @@ -190,7 +190,12 @@ setting environment variables (which would be inherited by subprocesses).\n\
>                          in LIST\n\
>    --audit LIST          use objects named in LIST as auditors\n\
>    --preload LIST        preload objects named in LIST\n\
> -  --argv0 STRING        set argv[0] to STRING before running\n\
> +  --argv0 STRING        set argv[0] to STRING before running\n"
> +#if HAVE_TUNABLES
> +"\
> +  --list-tunables       list all tunables with minimum and maximum values\n"
> +#endif
> +"\
>    --help                display this help and exit\n\
>    --version             output version information and exit\n\
>  \n\

Ok.

> diff --git a/elf/rtld.c b/elf/rtld.c
> index 5d117d0d2c..33993a6f8b 100644
> --- a/elf/rtld.c
> +++ b/elf/rtld.c
> @@ -49,6 +49,10 @@
>  #include <libc-early-init.h>
>  #include <dl-main.h>
>  
> +#if HAVE_TUNABLES
> +# include <dl-tunables.h>
> +#endif
> +

You can include this header regadless of HAVE_TUNABLES (it handles it
internally).

>  #include <assert.h>
>  
>  /* Only enables rtld profiling for architectures which provides non generic
> @@ -1248,6 +1252,16 @@ dl_main (const ElfW(Phdr) *phdr,
>  	    _dl_argc -= 2;
>  	    _dl_argv += 2;
>  	  }
> +#if HAVE_TUNABLES
> +	else if (! strcmp (_dl_argv[1], "--list-tunables"))
> +	  {
> +	    state.mode = rtld_mode_list_tunables;
> +
> +	    ++_dl_skip_args;
> +	    --_dl_argc;
> +	    ++_dl_argv;
> +	  }
> +#endif
>  	else if (strcmp (_dl_argv[1], "--help") == 0)
>  	  {
>  	    state.mode = rtld_mode_help;
> @@ -1268,6 +1282,15 @@ dl_main (const ElfW(Phdr) *phdr,
>  	else
>  	  break;
>  
> +#if HAVE_TUNABLES
> +      if (__builtin_expect (state.mode, rtld_mode_normal)
> +	  == rtld_mode_list_tunables)

Use

  if (__glibc_unlikely (state.mode == rtld_mode_list_tunables)

> +	{
> +	  __tunables_print ();
> +	  _exit (0);
> +	}
> +#endif
> +
>        /* If we have no further argument the program was called incorrectly.
>  	 Grant the user some education.  */
>        if (_dl_argc < 2)
> diff --git a/manual/tunables.texi b/manual/tunables.texi
> index d72d7a5ec0..924dac8876 100644
> --- a/manual/tunables.texi
> +++ b/manual/tunables.texi
> @@ -28,6 +28,43 @@ Finally, the set of tunables available may vary between distributions as
>  the tunables feature allows distributions to add their own tunables under
>  their own namespace.
>  
> +Passing @option{--list-tunables} to the dynamic loader to print all
> +tunables with minimum and maximum values:
> +
> +@example
> +$ /lib64/ld-linux-x86-64.so.2 --list-tunables
> +glibc.rtld.nns: 0x4 (min: 0x1, max: 0x10)
> +glibc.elision.skip_lock_after_retries: 3 (min: -2147483648, max: 2147483647)
> +glibc.malloc.trim_threshold: 0x0 (min: 0x0, max: 0xffffffffffffffff)
> +glibc.malloc.perturb: 0 (min: 0, max: 255)
> +glibc.cpu.x86_shared_cache_size: 0x100000 (min: 0x0, max: 0xffffffffffffffff)
> +glibc.elision.tries: 3 (min: -2147483648, max: 2147483647)
> +glibc.elision.enable: 0 (min: 0, max: 1)
> +glibc.cpu.x86_rep_movsb_threshold: 0x800 (min: 0x100, max: 0xffffffffffffffff)
> +glibc.malloc.mxfast: 0x0 (min: 0x0, max: 0xffffffffffffffff)
> +glibc.elision.skip_lock_busy: 3 (min: -2147483648, max: 2147483647)
> +glibc.malloc.top_pad: 0x0 (min: 0x0, max: 0xffffffffffffffff)
> +glibc.cpu.x86_rep_stosb_threshold: 0x800 (min: 0x1, max: 0xffffffffffffffff)
> +glibc.cpu.x86_non_temporal_threshold: 0xc0000 (min: 0x0, max: 0xffffffffffffffff)
> +glibc.cpu.x86_shstk: 

Trailing whitespace.

> +glibc.cpu.hwcap_mask: 0x6 (min: 0x0, max: 0xffffffffffffffff)
> +glibc.malloc.mmap_max: 0 (min: -2147483648, max: 2147483647)
> +glibc.elision.skip_trylock_internal_abort: 3 (min: -2147483648, max: 2147483647)
> +glibc.malloc.tcache_unsorted_limit: 0x0 (min: 0x0, max: 0xffffffffffffffff)
> +glibc.cpu.x86_ibt: 

Ditto.

> +glibc.cpu.hwcaps: 

Ditto.

> +glibc.elision.skip_lock_internal_abort: 3 (min: -2147483648, max: 2147483647)
> +glibc.malloc.arena_max: 0x0 (min: 0x1, max: 0xffffffffffffffff)
> +glibc.malloc.mmap_threshold: 0x0 (min: 0x0, max: 0xffffffffffffffff)
> +glibc.cpu.x86_data_cache_size: 0x8000 (min: 0x0, max: 0xffffffffffffffff)
> +glibc.malloc.tcache_count: 0x0 (min: 0x0, max: 0xffffffffffffffff)
> +glibc.malloc.arena_test: 0x0 (min: 0x1, max: 0xffffffffffffffff)
> +glibc.pthread.mutex_spin_count: 100 (min: 0, max: 32767)
> +glibc.rtld.optional_static_tls: 0x200 (min: 0x0, max: 0xffffffffffffffff)
> +glibc.malloc.tcache_max: 0x0 (min: 0x0, max: 0xffffffffffffffff)
> +glibc.malloc.check: 0 (min: 0, max: 3)
> +@end example
> +
>  @menu
>  * Tunable names::  The structure of a tunable name
>  * Memory Allocation Tunables::  Tunables in the memory allocation subsystem
>
  
H.J. Lu Jan. 14, 2021, 10:25 p.m. UTC | #2
On Thu, Jan 14, 2021 at 10:35 AM Adhemerval Zanella
<adhemerval.zanella@linaro.org> wrote:
>
>
>
> On 31/10/2020 12:44, H.J. Lu via Libc-alpha wrote:
> > Pass --list-tunables to ld.so to print tunables with min and max values.
> > ---
> >  NEWS                 |  2 ++
> >  elf/Makefile         |  8 ++++++++
> >  elf/dl-main.h        |  2 +-
> >  elf/dl-tunables.c    | 36 ++++++++++++++++++++++++++++++++++++
> >  elf/dl-tunables.h    |  2 ++
> >  elf/dl-usage.c       |  7 ++++++-
> >  elf/rtld.c           | 23 +++++++++++++++++++++++
> >  manual/tunables.texi | 37 +++++++++++++++++++++++++++++++++++++
> >  8 files changed, 115 insertions(+), 2 deletions(-)
> >
> > diff --git a/NEWS b/NEWS
> > index 4307c4b1b0..a62e7307ef 100644
> > --- a/NEWS
> > +++ b/NEWS
> > @@ -9,6 +9,8 @@ Version 2.33
> >
> >  Major new features:
> >
> > +* Pass --list-tunables to ld.so to print tunable values.
> > +
>
> Maybe use a similar sentence as for argv0:
>
>   * The dynamic linker accepts the --list-tunables argument which prints
>     all the supported tunables.  This option is disable is glibc is
>     configured with tunables disabled (--enable-tunables=no).

Fixed.

> >  * The dynamic linker accepts the --argv0 argument and provides opportunity
> >    to change argv[0] string.
> >
> > diff --git a/elf/Makefile b/elf/Makefile
> > index f10cc59e7c..86b282a32b 100644
> > --- a/elf/Makefile
> > +++ b/elf/Makefile
> > @@ -44,6 +44,10 @@ dl-routines += dl-tunables
> >  tunables-type = $(addprefix TUNABLES_FRONTEND_,$(have-tunables))
> >  CPPFLAGS-dl-tunables.c += -DTUNABLES_FRONTEND=$(tunables-type)
> >
> > +ifeq (yesyes,$(build-shared)$(run-built-tests))
> > +tests-special += $(objpfx)list-tunables.out
> > +endif
> > +
> >  # Make sure that the compiler does not insert any library calls in tunables
> >  # code paths.
>
> Ok, it is enabled iff 'ifneq (no,$(have-tunables))'.
>
> >  ifeq (yes,$(have-loop-to-function))
> > @@ -1812,3 +1816,7 @@ $(objpfx)argv0test.out: tst-rtld-argv0.sh $(objpfx)ld.so \
> >              '$(test-wrapper-env)' '$(run_program_env)' \
> >              '$(rpath-link)' 'test-argv0' > $@; \
> >      $(evaluate-test)
> > +
> > +$(objpfx)list-tunables.out: $(objpfx)ld.so
> > +     $(objpfx)ld.so --list-tunables > $@; \
> > +     $(evaluate-test)
>
> Maybe at least check for a tunable with a default value which be
> equal to all architecture, like glibc.rtld.nns, glibc.malloc.perturb, etc?

I added elf/tst-rtld-list-tunables.exp and elf/tst-rtld-list-tunables.sh
to compare against rtld and malloc tunables.  It passed on i686, x32
and x86-64.

> > diff --git a/elf/dl-main.h b/elf/dl-main.h
> > index b51256d3b4..f229867b8e 100644
> > --- a/elf/dl-main.h
> > +++ b/elf/dl-main.h
> > @@ -63,7 +63,7 @@ struct audit_list
> >  enum rtld_mode
> >    {
> >      rtld_mode_normal, rtld_mode_list, rtld_mode_verify, rtld_mode_trace,
> > -    rtld_mode_help,
> > +    rtld_mode_list_tunables, rtld_mode_help,
> >    };
> >
> >  /* Aggregated state information extracted from environment variables
>
> Ok.
>
> > diff --git a/elf/dl-tunables.c b/elf/dl-tunables.c
> > index 2ba2844075..048601b704 100644
> > --- a/elf/dl-tunables.c
> > +++ b/elf/dl-tunables.c
> > @@ -398,6 +398,42 @@ __tunables_init (char **envp)
> >      }
> >  }
> >
> > +void
> > +__tunables_print (void)
> > +{
> > +  for (int i = 0; i < sizeof (tunable_list) / sizeof (tunable_t); i++)
>
> Use array_length here.

Fixed.

> > +    {
> > +      tunable_t *cur = &tunable_list[i];
>
> Maybe use a const modifier here.

Fixed.

> > +      _dl_printf ("%s: ", cur->name);
> > +      switch (cur->type.type_code)
> > +     {
> > +     case TUNABLE_TYPE_INT_32:
> > +       _dl_printf ("%d (min: %d, max: %d)\n",
> > +                   (int) cur->val.numval,
> > +                   (int) cur->type.min,
> > +                   (int) cur->type.max);
> > +       break;
> > +     case TUNABLE_TYPE_UINT_64:
> > +       _dl_printf ("0x%lx (min: 0x%lx, max: 0x%lx)\n",
> > +                   (long int) cur->val.numval,
> > +                   (long int) cur->type.min,
> > +                   (long int) cur->type.max);
> > +       break;
> > +     case TUNABLE_TYPE_SIZE_T:
> > +       _dl_printf ("0x%Zx (min: 0x%Zx, max: 0x%Zx)\n",
> > +                   (size_t) cur->val.numval,
> > +                   (size_t) cur->type.min,
> > +                   (size_t) cur->type.max);
> > +       break;
> > +     case TUNABLE_TYPE_STRING:
> > +       _dl_printf ("%s\n", cur->val.strval ? cur->val.strval : "");
> > +       break;
> > +     default:
> > +       __builtin_unreachable ();
> > +     }
> > +    }
> > +}
> > +
>
> Ok, it should be ok to use '%d' for signed and 'lx' for unsigned.
>
> >  /* Set the tunable value.  This is called by the module that the tunable exists
> >     in. */
> >  void
> > diff --git a/elf/dl-tunables.h b/elf/dl-tunables.h
> > index 550b0cc7f4..4a0c90f3e0 100644
> > --- a/elf/dl-tunables.h
> > +++ b/elf/dl-tunables.h
> > @@ -69,9 +69,11 @@ typedef struct _tunable tunable_t;
> >  # include "dl-tunable-list.h"
> >
> >  extern void __tunables_init (char **);
> > +extern void __tunables_print (void);
> >  extern void __tunable_get_val (tunable_id_t, void *, tunable_callback_t);
> >  extern void __tunable_set_val (tunable_id_t, void *, void *, void *);
> >  rtld_hidden_proto (__tunables_init)
> > +rtld_hidden_proto (__tunables_print)
> >  rtld_hidden_proto (__tunable_get_val)
> >  rtld_hidden_proto (__tunable_set_val)
> >
>
> Ok.
>
> > diff --git a/elf/dl-usage.c b/elf/dl-usage.c
> > index 796ad38b43..3ce19fb892 100644
> > --- a/elf/dl-usage.c
> > +++ b/elf/dl-usage.c
> > @@ -190,7 +190,12 @@ setting environment variables (which would be inherited by subprocesses).\n\
> >                          in LIST\n\
> >    --audit LIST          use objects named in LIST as auditors\n\
> >    --preload LIST        preload objects named in LIST\n\
> > -  --argv0 STRING        set argv[0] to STRING before running\n\
> > +  --argv0 STRING        set argv[0] to STRING before running\n"
> > +#if HAVE_TUNABLES
> > +"\
> > +  --list-tunables       list all tunables with minimum and maximum values\n"
> > +#endif
> > +"\
> >    --help                display this help and exit\n\
> >    --version             output version information and exit\n\
> >  \n\
>
> Ok.
>
> > diff --git a/elf/rtld.c b/elf/rtld.c
> > index 5d117d0d2c..33993a6f8b 100644
> > --- a/elf/rtld.c
> > +++ b/elf/rtld.c
> > @@ -49,6 +49,10 @@
> >  #include <libc-early-init.h>
> >  #include <dl-main.h>
> >
> > +#if HAVE_TUNABLES
> > +# include <dl-tunables.h>
> > +#endif
> > +
>
> You can include this header regadless of HAVE_TUNABLES (it handles it
> internally).

Fixed.

> >  #include <assert.h>
> >
> >  /* Only enables rtld profiling for architectures which provides non generic
> > @@ -1248,6 +1252,16 @@ dl_main (const ElfW(Phdr) *phdr,
> >           _dl_argc -= 2;
> >           _dl_argv += 2;
> >         }
> > +#if HAVE_TUNABLES
> > +     else if (! strcmp (_dl_argv[1], "--list-tunables"))
> > +       {
> > +         state.mode = rtld_mode_list_tunables;
> > +
> > +         ++_dl_skip_args;
> > +         --_dl_argc;
> > +         ++_dl_argv;
> > +       }
> > +#endif
> >       else if (strcmp (_dl_argv[1], "--help") == 0)
> >         {
> >           state.mode = rtld_mode_help;
> > @@ -1268,6 +1282,15 @@ dl_main (const ElfW(Phdr) *phdr,
> >       else
> >         break;
> >
> > +#if HAVE_TUNABLES
> > +      if (__builtin_expect (state.mode, rtld_mode_normal)
> > +       == rtld_mode_list_tunables)
>
> Use
>
>   if (__glibc_unlikely (state.mode == rtld_mode_list_tunables)

Fixed.

> > +     {
> > +       __tunables_print ();
> > +       _exit (0);
> > +     }
> > +#endif
> > +
> >        /* If we have no further argument the program was called incorrectly.
> >        Grant the user some education.  */
> >        if (_dl_argc < 2)
> > diff --git a/manual/tunables.texi b/manual/tunables.texi
> > index d72d7a5ec0..924dac8876 100644
> > --- a/manual/tunables.texi
> > +++ b/manual/tunables.texi
> > @@ -28,6 +28,43 @@ Finally, the set of tunables available may vary between distributions as
> >  the tunables feature allows distributions to add their own tunables under
> >  their own namespace.
> >
> > +Passing @option{--list-tunables} to the dynamic loader to print all
> > +tunables with minimum and maximum values:
> > +
> > +@example
> > +$ /lib64/ld-linux-x86-64.so.2 --list-tunables
> > +glibc.rtld.nns: 0x4 (min: 0x1, max: 0x10)
> > +glibc.elision.skip_lock_after_retries: 3 (min: -2147483648, max: 2147483647)
> > +glibc.malloc.trim_threshold: 0x0 (min: 0x0, max: 0xffffffffffffffff)
> > +glibc.malloc.perturb: 0 (min: 0, max: 255)
> > +glibc.cpu.x86_shared_cache_size: 0x100000 (min: 0x0, max: 0xffffffffffffffff)
> > +glibc.elision.tries: 3 (min: -2147483648, max: 2147483647)
> > +glibc.elision.enable: 0 (min: 0, max: 1)
> > +glibc.cpu.x86_rep_movsb_threshold: 0x800 (min: 0x100, max: 0xffffffffffffffff)
> > +glibc.malloc.mxfast: 0x0 (min: 0x0, max: 0xffffffffffffffff)
> > +glibc.elision.skip_lock_busy: 3 (min: -2147483648, max: 2147483647)
> > +glibc.malloc.top_pad: 0x0 (min: 0x0, max: 0xffffffffffffffff)
> > +glibc.cpu.x86_rep_stosb_threshold: 0x800 (min: 0x1, max: 0xffffffffffffffff)
> > +glibc.cpu.x86_non_temporal_threshold: 0xc0000 (min: 0x0, max: 0xffffffffffffffff)
> > +glibc.cpu.x86_shstk:
>
> Trailing whitespace.

Fixed.

> > +glibc.cpu.hwcap_mask: 0x6 (min: 0x0, max: 0xffffffffffffffff)
> > +glibc.malloc.mmap_max: 0 (min: -2147483648, max: 2147483647)
> > +glibc.elision.skip_trylock_internal_abort: 3 (min: -2147483648, max: 2147483647)
> > +glibc.malloc.tcache_unsorted_limit: 0x0 (min: 0x0, max: 0xffffffffffffffff)
> > +glibc.cpu.x86_ibt:
>
> Ditto.

Fixed.

> > +glibc.cpu.hwcaps:
>
> Ditto.

Fixed.

> > +glibc.elision.skip_lock_internal_abort: 3 (min: -2147483648, max: 2147483647)
> > +glibc.malloc.arena_max: 0x0 (min: 0x1, max: 0xffffffffffffffff)
> > +glibc.malloc.mmap_threshold: 0x0 (min: 0x0, max: 0xffffffffffffffff)
> > +glibc.cpu.x86_data_cache_size: 0x8000 (min: 0x0, max: 0xffffffffffffffff)
> > +glibc.malloc.tcache_count: 0x0 (min: 0x0, max: 0xffffffffffffffff)
> > +glibc.malloc.arena_test: 0x0 (min: 0x1, max: 0xffffffffffffffff)
> > +glibc.pthread.mutex_spin_count: 100 (min: 0, max: 32767)
> > +glibc.rtld.optional_static_tls: 0x200 (min: 0x0, max: 0xffffffffffffffff)
> > +glibc.malloc.tcache_max: 0x0 (min: 0x0, max: 0xffffffffffffffff)
> > +glibc.malloc.check: 0 (min: 0, max: 3)
> > +@end example
> > +
> >  @menu
> >  * Tunable names::  The structure of a tunable name
> >  * Memory Allocation Tunables::  Tunables in the memory allocation subsystem
> >

Here is the updated patch.   OK for master?

Thanks.
  
Adhemerval Zanella Netto Jan. 15, 2021, 12:47 p.m. UTC | #3
On 14/01/2021 19:25, H.J. Lu wrote:
> On Thu, Jan 14, 2021 at 10:35 AM Adhemerval Zanella
> <adhemerval.zanella@linaro.org> wrote:
>>
>>
>>
>> On 31/10/2020 12:44, H.J. Lu via Libc-alpha wrote:
> 
> Here is the updated patch.   OK for master?
> 
> Thanks.

LGTM, thanks.

Reviewed-by: Adhemerval Zanella  <adhemerval.zanella@linaro.org>

> 

> From 2745fc527295ad735d14bdaf6052c40dc24772a8 Mon Sep 17 00:00:00 2001
> From: "H.J. Lu" <hjl.tools@gmail.com>
> Date: Sun, 12 Jul 2020 06:04:53 -0700
> Subject: [PATCH] ld.so: Add --list-tunables to print tunable values
> 
> Pass --list-tunables to ld.so to print tunables with min and max values.
> ---
>  NEWS                           |  4 ++++
>  elf/Makefile                   | 13 ++++++++++
>  elf/dl-main.h                  |  2 +-
>  elf/dl-tunables.c              | 43 ++++++++++++++++++++++++++++++++++
>  elf/dl-tunables.h              |  2 ++
>  elf/dl-usage.c                 |  7 +++++-
>  elf/rtld.c                     | 19 +++++++++++++++
>  elf/tst-rtld-list-tunables.exp | 14 +++++++++++
>  elf/tst-rtld-list-tunables.sh  | 34 +++++++++++++++++++++++++++
>  manual/tunables.texi           | 38 ++++++++++++++++++++++++++++++
>  10 files changed, 174 insertions(+), 2 deletions(-)
>  create mode 100644 elf/tst-rtld-list-tunables.exp
>  create mode 100755 elf/tst-rtld-list-tunables.sh
> 
> diff --git a/NEWS b/NEWS
> index face78cd10..d121f7df87 100644
> --- a/NEWS
> +++ b/NEWS
> @@ -9,6 +9,10 @@ Version 2.33
>  
>  Major new features:
>  
> +* The dynamic linker accepts the --list-tunables argument which prints
> +  all the supported tunables.  This option is disable if glibc is
> +  configured with tunables disabled (--enable-tunables=no).
> +
>  * The dynamic linker accepts the --argv0 argument and provides opportunity
>    to change argv[0] string.
>  

Ok.

> diff --git a/elf/Makefile b/elf/Makefile
> index c41d11693b..0e46cab699 100644
> --- a/elf/Makefile
> +++ b/elf/Makefile
> @@ -45,6 +45,10 @@ dl-routines += dl-tunables
>  tunables-type = $(addprefix TUNABLES_FRONTEND_,$(have-tunables))
>  CPPFLAGS-dl-tunables.c += -DTUNABLES_FRONTEND=$(tunables-type)
>  
> +ifeq (yesyes,$(build-shared)$(run-built-tests))
> +tests-special += $(objpfx)list-tunables.out
> +endif
> +
>  # Make sure that the compiler does not insert any library calls in tunables
>  # code paths.
>  ifeq (yes,$(have-loop-to-function))

Ok.

> @@ -1896,3 +1900,12 @@ $(objpfx)tst-glibc-hwcaps-mask.out: \
>  # Generic dependency for sysdeps implementation of
>  # tst-glibc-hwcaps-cache.
>  $(objpfx)tst-glibc-hwcaps-cache.out: $(objpfx)tst-glibc-hwcaps
> +
> +$(objpfx)list-tunables.out: tst-rtld-list-tunables.sh $(objpfx)ld.so
> +	$(objpfx)ld.so --list-tunables > $@; \
> +	$(evaluate-test)
> +	$(SHELL) $< $(objpfx)ld.so '$(test-wrapper-env)' \
> +	    '$(run_program_env)' > $(objpfx)/tst-rtld-list-tunables.out
> +	cmp tst-rtld-list-tunables.exp \
> +	    $(objpfx)/tst-rtld-list-tunables.out > $@; \
> +	$(evaluate-test)

Ok.

> diff --git a/elf/dl-main.h b/elf/dl-main.h
> index 0bccab165f..3a5e13c739 100644
> --- a/elf/dl-main.h
> +++ b/elf/dl-main.h
> @@ -63,7 +63,7 @@ struct audit_list
>  enum rtld_mode
>    {
>      rtld_mode_normal, rtld_mode_list, rtld_mode_verify, rtld_mode_trace,
> -    rtld_mode_help,
> +    rtld_mode_list_tunables, rtld_mode_help,
>    };
>  
>  /* Aggregated state information extracted from environment variables

Ok.

> diff --git a/elf/dl-tunables.c b/elf/dl-tunables.c
> index 9b4d737fb8..33be00e447 100644
> --- a/elf/dl-tunables.c
> +++ b/elf/dl-tunables.c
> @@ -26,6 +26,7 @@
>  #include <sysdep.h>
>  #include <fcntl.h>
>  #include <ldsodefs.h>
> +#include <array_length.h>
>  
>  #define TUNABLES_INTERNAL 1
>  #include "dl-tunables.h"
> @@ -398,6 +399,48 @@ __tunables_init (char **envp)
>      }
>  }
>  
> +void
> +__tunables_print (void)
> +{
> +  for (int i = 0; i < array_length (tunable_list); i++)
> +    {
> +      const tunable_t *cur = &tunable_list[i];
> +      if (cur->type.type_code == TUNABLE_TYPE_STRING
> +	  && cur->val.strval == NULL)
> +	_dl_printf ("%s:\n", cur->name);
> +      else
> +	{
> +	  _dl_printf ("%s: ", cur->name);
> +	  switch (cur->type.type_code)
> +	    {
> +	    case TUNABLE_TYPE_INT_32:
> +	      _dl_printf ("%d (min: %d, max: %d)\n",
> +			  (int) cur->val.numval,
> +			  (int) cur->type.min,
> +			  (int) cur->type.max);
> +	      break;
> +	    case TUNABLE_TYPE_UINT_64:
> +	      _dl_printf ("0x%lx (min: 0x%lx, max: 0x%lx)\n",
> +			  (long int) cur->val.numval,
> +			  (long int) cur->type.min,
> +			  (long int) cur->type.max);
> +	      break;
> +	    case TUNABLE_TYPE_SIZE_T:
> +	      _dl_printf ("0x%Zx (min: 0x%Zx, max: 0x%Zx)\n",
> +			  (size_t) cur->val.numval,
> +			  (size_t) cur->type.min,
> +			  (size_t) cur->type.max);
> +	      break;
> +	    case TUNABLE_TYPE_STRING:
> +	      _dl_printf ("%s\n", cur->val.strval);
> +	      break;
> +	    default:
> +	      __builtin_unreachable ();
> +	    }
> +	}
> +    }
> +}
> +
>  /* Set the tunable value.  This is called by the module that the tunable exists
>     in. */
>  void

Ok.

> diff --git a/elf/dl-tunables.h b/elf/dl-tunables.h
> index 518342a300..dfa16c1977 100644
> --- a/elf/dl-tunables.h
> +++ b/elf/dl-tunables.h
> @@ -69,9 +69,11 @@ typedef struct _tunable tunable_t;
>  # include "dl-tunable-list.h"
>  
>  extern void __tunables_init (char **);
> +extern void __tunables_print (void);
>  extern void __tunable_get_val (tunable_id_t, void *, tunable_callback_t);
>  extern void __tunable_set_val (tunable_id_t, void *, void *, void *);
>  rtld_hidden_proto (__tunables_init)
> +rtld_hidden_proto (__tunables_print)
>  rtld_hidden_proto (__tunable_get_val)
>  rtld_hidden_proto (__tunable_set_val)
>  

Ok.

> diff --git a/elf/dl-usage.c b/elf/dl-usage.c
> index 907f068cfb..6e26818bd7 100644
> --- a/elf/dl-usage.c
> +++ b/elf/dl-usage.c
> @@ -255,7 +255,12 @@ setting environment variables (which would be inherited by subprocesses).\n\
>                          in LIST\n\
>    --audit LIST          use objects named in LIST as auditors\n\
>    --preload LIST        preload objects named in LIST\n\
> -  --argv0 STRING        set argv[0] to STRING before running\n\
> +  --argv0 STRING        set argv[0] to STRING before running\n"
> +#if HAVE_TUNABLES
> +"\
> +  --list-tunables       list all tunables with minimum and maximum values\n"
> +#endif
> +"\
>    --help                display this help and exit\n\
>    --version             output version information and exit\n\
>  \n\

Ok.

> diff --git a/elf/rtld.c b/elf/rtld.c
> index 8d9add90e3..596b6ac3d9 100644
> --- a/elf/rtld.c
> +++ b/elf/rtld.c
> @@ -50,6 +50,7 @@
>  #include <dl-main.h>
>  #include <list.h>
>  #include <gnu/lib-names.h>
> +#include <dl-tunables.h>
>  
>  #include <assert.h>
>  
> @@ -1276,6 +1277,16 @@ dl_main (const ElfW(Phdr) *phdr,
>  	    _dl_argc -= 2;
>  	    _dl_argv += 2;
>  	  }
> +#if HAVE_TUNABLES
> +	else if (! strcmp (_dl_argv[1], "--list-tunables"))
> +	  {
> +	    state.mode = rtld_mode_list_tunables;
> +
> +	    ++_dl_skip_args;
> +	    --_dl_argc;
> +	    ++_dl_argv;
> +	  }
> +#endif
>  	else if (strcmp (_dl_argv[1], "--help") == 0)
>  	  {
>  	    state.mode = rtld_mode_help;
> @@ -1296,6 +1307,14 @@ dl_main (const ElfW(Phdr) *phdr,
>  	else
>  	  break;
>  
> +#if HAVE_TUNABLES
> +      if (__glibc_unlikely (state.mode == rtld_mode_list_tunables))
> +	{
> +	  __tunables_print ();
> +	  _exit (0);
> +	}
> +#endif
> +
>        /* If we have no further argument the program was called incorrectly.
>  	 Grant the user some education.  */
>        if (_dl_argc < 2)

Ok.

> diff --git a/elf/tst-rtld-list-tunables.exp b/elf/tst-rtld-list-tunables.exp
> new file mode 100644
> index 0000000000..4f3f7ee4e3
> --- /dev/null
> +++ b/elf/tst-rtld-list-tunables.exp
> @@ -0,0 +1,14 @@
> +glibc.malloc.arena_max: 0x0 (min: 0x1, max: 0x[f]+)
> +glibc.malloc.arena_test: 0x0 (min: 0x1, max: 0x[f]+)
> +glibc.malloc.check: 0 (min: 0, max: 3)
> +glibc.malloc.mmap_max: 0 (min: -2147483648, max: 2147483647)
> +glibc.malloc.mmap_threshold: 0x0 (min: 0x0, max: 0x[f]+)
> +glibc.malloc.mxfast: 0x0 (min: 0x0, max: 0x[f]+)
> +glibc.malloc.perturb: 0 (min: 0, max: 255)
> +glibc.malloc.tcache_count: 0x0 (min: 0x0, max: 0x[f]+)
> +glibc.malloc.tcache_max: 0x0 (min: 0x0, max: 0x[f]+)
> +glibc.malloc.tcache_unsorted_limit: 0x0 (min: 0x0, max: 0x[f]+)
> +glibc.malloc.top_pad: 0x0 (min: 0x0, max: 0x[f]+)
> +glibc.malloc.trim_threshold: 0x0 (min: 0x0, max: 0x[f]+)
> +glibc.rtld.nns: 0x4 (min: 0x1, max: 0x10)
> +glibc.rtld.optional_static_tls: 0x200 (min: 0x0, max: 0x[f]+)

Ok, it seems to be to common ones.

> diff --git a/elf/tst-rtld-list-tunables.sh b/elf/tst-rtld-list-tunables.sh
> new file mode 100755
> index 0000000000..e7bbdde949
> --- /dev/null
> +++ b/elf/tst-rtld-list-tunables.sh
> @@ -0,0 +1,34 @@
> +#!/bin/sh
> +# Test for --list-tunables option ld.so.
> +# Copyright (C) 2021 Free Software Foundation, Inc.
> +# This file is part of the GNU C Library.
> +#
> +# The GNU C Library is free software; you can redistribute it and/or
> +# modify it under the terms of the GNU Lesser General Public
> +# License as published by the Free Software Foundation; either
> +# version 2.1 of the License, or (at your option) any later version.
> +#
> +# The GNU C Library is distributed in the hope that it will be useful,
> +# but WITHOUT ANY WARRANTY; without even the implied warranty of
> +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
> +# Lesser General Public License for more details.
> +#
> +# You should have received a copy of the GNU Lesser General Public
> +# License along with the GNU C Library; if not, see
> +# <https://www.gnu.org/licenses/>.
> +
> +set -e
> +
> +rtld=$1
> +test_wrapper_env=$2
> +run_program_env=$3
> +
> +LC_ALL=C
> +export LC_ALL
> +
> +${test_wrapper_env} \
> +${run_program_env} \
> +$rtld --list-tunables \
> +| sort -u \
> +| egrep "(rtld|malloc)" \
> +| sed -e "s/0xf\+/0x[f]+/"

Ok.

> diff --git a/manual/tunables.texi b/manual/tunables.texi
> index 1bbdc88281..1b746c0fa1 100644
> --- a/manual/tunables.texi
> +++ b/manual/tunables.texi
> @@ -28,6 +28,44 @@ Finally, the set of tunables available may vary between distributions as
>  the tunables feature allows distributions to add their own tunables under
>  their own namespace.
>  
> +Passing @option{--list-tunables} to the dynamic loader to print all
> +tunables with minimum and maximum values:
> +
> +@example
> +$ /lib64/ld-linux-x86-64.so.2 --list-tunables
> +glibc.rtld.nns: 0x4 (min: 0x1, max: 0x10)
> +glibc.elision.skip_lock_after_retries: 3 (min: -2147483648, max: 2147483647)
> +glibc.malloc.trim_threshold: 0x0 (min: 0x0, max: 0xffffffffffffffff)
> +glibc.malloc.perturb: 0 (min: 0, max: 255)
> +glibc.cpu.x86_shared_cache_size: 0x100000 (min: 0x0, max: 0xffffffffffffffff)
> +glibc.mem.tagging: 0 (min: 0, max: 255)
> +glibc.elision.tries: 3 (min: -2147483648, max: 2147483647)
> +glibc.elision.enable: 0 (min: 0, max: 1)
> +glibc.cpu.x86_rep_movsb_threshold: 0x1000 (min: 0x100, max: 0xffffffffffffffff)
> +glibc.malloc.mxfast: 0x0 (min: 0x0, max: 0xffffffffffffffff)
> +glibc.elision.skip_lock_busy: 3 (min: -2147483648, max: 2147483647)
> +glibc.malloc.top_pad: 0x0 (min: 0x0, max: 0xffffffffffffffff)
> +glibc.cpu.x86_rep_stosb_threshold: 0x800 (min: 0x1, max: 0xffffffffffffffff)
> +glibc.cpu.x86_non_temporal_threshold: 0xc0000 (min: 0x0, max: 0xffffffffffffffff)
> +glibc.cpu.x86_shstk:
> +glibc.cpu.hwcap_mask: 0x6 (min: 0x0, max: 0xffffffffffffffff)
> +glibc.malloc.mmap_max: 0 (min: -2147483648, max: 2147483647)
> +glibc.elision.skip_trylock_internal_abort: 3 (min: -2147483648, max: 2147483647)
> +glibc.malloc.tcache_unsorted_limit: 0x0 (min: 0x0, max: 0xffffffffffffffff)
> +glibc.cpu.x86_ibt:
> +glibc.cpu.hwcaps:
> +glibc.elision.skip_lock_internal_abort: 3 (min: -2147483648, max: 2147483647)
> +glibc.malloc.arena_max: 0x0 (min: 0x1, max: 0xffffffffffffffff)
> +glibc.malloc.mmap_threshold: 0x0 (min: 0x0, max: 0xffffffffffffffff)
> +glibc.cpu.x86_data_cache_size: 0x8000 (min: 0x0, max: 0xffffffffffffffff)
> +glibc.malloc.tcache_count: 0x0 (min: 0x0, max: 0xffffffffffffffff)
> +glibc.malloc.arena_test: 0x0 (min: 0x1, max: 0xffffffffffffffff)
> +glibc.pthread.mutex_spin_count: 100 (min: 0, max: 32767)
> +glibc.rtld.optional_static_tls: 0x200 (min: 0x0, max: 0xffffffffffffffff)
> +glibc.malloc.tcache_max: 0x0 (min: 0x0, max: 0xffffffffffffffff)
> +glibc.malloc.check: 0 (min: 0, max: 3)
> +@end example
> +
>  @menu
>  * Tunable names::  The structure of a tunable name
>  * Memory Allocation Tunables::  Tunables in the memory allocation subsystem
> -- 
> 2.29.2
  
Andreas Schwab Feb. 2, 2021, 10:59 a.m. UTC | #4
On Jan 14 2021, H.J. Lu via Libc-alpha wrote:

> diff --git a/elf/tst-rtld-list-tunables.exp b/elf/tst-rtld-list-tunables.exp
> new file mode 100644
> index 0000000000..4f3f7ee4e3
> --- /dev/null
> +++ b/elf/tst-rtld-list-tunables.exp
> @@ -0,0 +1,14 @@
> +glibc.malloc.arena_max: 0x0 (min: 0x1, max: 0x[f]+)
> +glibc.malloc.arena_test: 0x0 (min: 0x1, max: 0x[f]+)
> +glibc.malloc.check: 0 (min: 0, max: 3)
> +glibc.malloc.mmap_max: 0 (min: -2147483648, max: 2147483647)
> +glibc.malloc.mmap_threshold: 0x0 (min: 0x0, max: 0x[f]+)
> +glibc.malloc.mxfast: 0x0 (min: 0x0, max: 0x[f]+)
> +glibc.malloc.perturb: 0 (min: 0, max: 255)
> +glibc.malloc.tcache_count: 0x0 (min: 0x0, max: 0x[f]+)
> +glibc.malloc.tcache_max: 0x0 (min: 0x0, max: 0x[f]+)
> +glibc.malloc.tcache_unsorted_limit: 0x0 (min: 0x0, max: 0x[f]+)
> +glibc.malloc.top_pad: 0x0 (min: 0x0, max: 0x[f]+)
> +glibc.malloc.trim_threshold: 0x0 (min: 0x0, max: 0x[f]+)
> +glibc.rtld.nns: 0x4 (min: 0x1, max: 0x10)
> +glibc.rtld.optional_static_tls: 0x200 (min: 0x0, max: 0x[f]+)

This won't match if MALLOC_PERTURB_ is set.

Andreas.
  

Patch

diff --git a/NEWS b/NEWS
index 4307c4b1b0..a62e7307ef 100644
--- a/NEWS
+++ b/NEWS
@@ -9,6 +9,8 @@  Version 2.33
 
 Major new features:
 
+* Pass --list-tunables to ld.so to print tunable values.
+
 * The dynamic linker accepts the --argv0 argument and provides opportunity
   to change argv[0] string.
 
diff --git a/elf/Makefile b/elf/Makefile
index f10cc59e7c..86b282a32b 100644
--- a/elf/Makefile
+++ b/elf/Makefile
@@ -44,6 +44,10 @@  dl-routines += dl-tunables
 tunables-type = $(addprefix TUNABLES_FRONTEND_,$(have-tunables))
 CPPFLAGS-dl-tunables.c += -DTUNABLES_FRONTEND=$(tunables-type)
 
+ifeq (yesyes,$(build-shared)$(run-built-tests))
+tests-special += $(objpfx)list-tunables.out
+endif
+
 # Make sure that the compiler does not insert any library calls in tunables
 # code paths.
 ifeq (yes,$(have-loop-to-function))
@@ -1812,3 +1816,7 @@  $(objpfx)argv0test.out: tst-rtld-argv0.sh $(objpfx)ld.so \
             '$(test-wrapper-env)' '$(run_program_env)' \
             '$(rpath-link)' 'test-argv0' > $@; \
     $(evaluate-test)
+
+$(objpfx)list-tunables.out: $(objpfx)ld.so
+	$(objpfx)ld.so --list-tunables > $@; \
+	$(evaluate-test)
diff --git a/elf/dl-main.h b/elf/dl-main.h
index b51256d3b4..f229867b8e 100644
--- a/elf/dl-main.h
+++ b/elf/dl-main.h
@@ -63,7 +63,7 @@  struct audit_list
 enum rtld_mode
   {
     rtld_mode_normal, rtld_mode_list, rtld_mode_verify, rtld_mode_trace,
-    rtld_mode_help,
+    rtld_mode_list_tunables, rtld_mode_help,
   };
 
 /* Aggregated state information extracted from environment variables
diff --git a/elf/dl-tunables.c b/elf/dl-tunables.c
index 2ba2844075..048601b704 100644
--- a/elf/dl-tunables.c
+++ b/elf/dl-tunables.c
@@ -398,6 +398,42 @@  __tunables_init (char **envp)
     }
 }
 
+void
+__tunables_print (void)
+{
+  for (int i = 0; i < sizeof (tunable_list) / sizeof (tunable_t); i++)
+    {
+      tunable_t *cur = &tunable_list[i];
+      _dl_printf ("%s: ", cur->name);
+      switch (cur->type.type_code)
+	{
+	case TUNABLE_TYPE_INT_32:
+	  _dl_printf ("%d (min: %d, max: %d)\n",
+		      (int) cur->val.numval,
+		      (int) cur->type.min,
+		      (int) cur->type.max);
+	  break;
+	case TUNABLE_TYPE_UINT_64:
+	  _dl_printf ("0x%lx (min: 0x%lx, max: 0x%lx)\n",
+		      (long int) cur->val.numval,
+		      (long int) cur->type.min,
+		      (long int) cur->type.max);
+	  break;
+	case TUNABLE_TYPE_SIZE_T:
+	  _dl_printf ("0x%Zx (min: 0x%Zx, max: 0x%Zx)\n",
+		      (size_t) cur->val.numval,
+		      (size_t) cur->type.min,
+		      (size_t) cur->type.max);
+	  break;
+	case TUNABLE_TYPE_STRING:
+	  _dl_printf ("%s\n", cur->val.strval ? cur->val.strval : "");
+	  break;
+	default:
+	  __builtin_unreachable ();
+	}
+    }
+}
+
 /* Set the tunable value.  This is called by the module that the tunable exists
    in. */
 void
diff --git a/elf/dl-tunables.h b/elf/dl-tunables.h
index 550b0cc7f4..4a0c90f3e0 100644
--- a/elf/dl-tunables.h
+++ b/elf/dl-tunables.h
@@ -69,9 +69,11 @@  typedef struct _tunable tunable_t;
 # include "dl-tunable-list.h"
 
 extern void __tunables_init (char **);
+extern void __tunables_print (void);
 extern void __tunable_get_val (tunable_id_t, void *, tunable_callback_t);
 extern void __tunable_set_val (tunable_id_t, void *, void *, void *);
 rtld_hidden_proto (__tunables_init)
+rtld_hidden_proto (__tunables_print)
 rtld_hidden_proto (__tunable_get_val)
 rtld_hidden_proto (__tunable_set_val)
 
diff --git a/elf/dl-usage.c b/elf/dl-usage.c
index 796ad38b43..3ce19fb892 100644
--- a/elf/dl-usage.c
+++ b/elf/dl-usage.c
@@ -190,7 +190,12 @@  setting environment variables (which would be inherited by subprocesses).\n\
                         in LIST\n\
   --audit LIST          use objects named in LIST as auditors\n\
   --preload LIST        preload objects named in LIST\n\
-  --argv0 STRING        set argv[0] to STRING before running\n\
+  --argv0 STRING        set argv[0] to STRING before running\n"
+#if HAVE_TUNABLES
+"\
+  --list-tunables       list all tunables with minimum and maximum values\n"
+#endif
+"\
   --help                display this help and exit\n\
   --version             output version information and exit\n\
 \n\
diff --git a/elf/rtld.c b/elf/rtld.c
index 5d117d0d2c..33993a6f8b 100644
--- a/elf/rtld.c
+++ b/elf/rtld.c
@@ -49,6 +49,10 @@ 
 #include <libc-early-init.h>
 #include <dl-main.h>
 
+#if HAVE_TUNABLES
+# include <dl-tunables.h>
+#endif
+
 #include <assert.h>
 
 /* Only enables rtld profiling for architectures which provides non generic
@@ -1248,6 +1252,16 @@  dl_main (const ElfW(Phdr) *phdr,
 	    _dl_argc -= 2;
 	    _dl_argv += 2;
 	  }
+#if HAVE_TUNABLES
+	else if (! strcmp (_dl_argv[1], "--list-tunables"))
+	  {
+	    state.mode = rtld_mode_list_tunables;
+
+	    ++_dl_skip_args;
+	    --_dl_argc;
+	    ++_dl_argv;
+	  }
+#endif
 	else if (strcmp (_dl_argv[1], "--help") == 0)
 	  {
 	    state.mode = rtld_mode_help;
@@ -1268,6 +1282,15 @@  dl_main (const ElfW(Phdr) *phdr,
 	else
 	  break;
 
+#if HAVE_TUNABLES
+      if (__builtin_expect (state.mode, rtld_mode_normal)
+	  == rtld_mode_list_tunables)
+	{
+	  __tunables_print ();
+	  _exit (0);
+	}
+#endif
+
       /* If we have no further argument the program was called incorrectly.
 	 Grant the user some education.  */
       if (_dl_argc < 2)
diff --git a/manual/tunables.texi b/manual/tunables.texi
index d72d7a5ec0..924dac8876 100644
--- a/manual/tunables.texi
+++ b/manual/tunables.texi
@@ -28,6 +28,43 @@  Finally, the set of tunables available may vary between distributions as
 the tunables feature allows distributions to add their own tunables under
 their own namespace.
 
+Passing @option{--list-tunables} to the dynamic loader to print all
+tunables with minimum and maximum values:
+
+@example
+$ /lib64/ld-linux-x86-64.so.2 --list-tunables
+glibc.rtld.nns: 0x4 (min: 0x1, max: 0x10)
+glibc.elision.skip_lock_after_retries: 3 (min: -2147483648, max: 2147483647)
+glibc.malloc.trim_threshold: 0x0 (min: 0x0, max: 0xffffffffffffffff)
+glibc.malloc.perturb: 0 (min: 0, max: 255)
+glibc.cpu.x86_shared_cache_size: 0x100000 (min: 0x0, max: 0xffffffffffffffff)
+glibc.elision.tries: 3 (min: -2147483648, max: 2147483647)
+glibc.elision.enable: 0 (min: 0, max: 1)
+glibc.cpu.x86_rep_movsb_threshold: 0x800 (min: 0x100, max: 0xffffffffffffffff)
+glibc.malloc.mxfast: 0x0 (min: 0x0, max: 0xffffffffffffffff)
+glibc.elision.skip_lock_busy: 3 (min: -2147483648, max: 2147483647)
+glibc.malloc.top_pad: 0x0 (min: 0x0, max: 0xffffffffffffffff)
+glibc.cpu.x86_rep_stosb_threshold: 0x800 (min: 0x1, max: 0xffffffffffffffff)
+glibc.cpu.x86_non_temporal_threshold: 0xc0000 (min: 0x0, max: 0xffffffffffffffff)
+glibc.cpu.x86_shstk: 
+glibc.cpu.hwcap_mask: 0x6 (min: 0x0, max: 0xffffffffffffffff)
+glibc.malloc.mmap_max: 0 (min: -2147483648, max: 2147483647)
+glibc.elision.skip_trylock_internal_abort: 3 (min: -2147483648, max: 2147483647)
+glibc.malloc.tcache_unsorted_limit: 0x0 (min: 0x0, max: 0xffffffffffffffff)
+glibc.cpu.x86_ibt: 
+glibc.cpu.hwcaps: 
+glibc.elision.skip_lock_internal_abort: 3 (min: -2147483648, max: 2147483647)
+glibc.malloc.arena_max: 0x0 (min: 0x1, max: 0xffffffffffffffff)
+glibc.malloc.mmap_threshold: 0x0 (min: 0x0, max: 0xffffffffffffffff)
+glibc.cpu.x86_data_cache_size: 0x8000 (min: 0x0, max: 0xffffffffffffffff)
+glibc.malloc.tcache_count: 0x0 (min: 0x0, max: 0xffffffffffffffff)
+glibc.malloc.arena_test: 0x0 (min: 0x1, max: 0xffffffffffffffff)
+glibc.pthread.mutex_spin_count: 100 (min: 0, max: 32767)
+glibc.rtld.optional_static_tls: 0x200 (min: 0x0, max: 0xffffffffffffffff)
+glibc.malloc.tcache_max: 0x0 (min: 0x0, max: 0xffffffffffffffff)
+glibc.malloc.check: 0 (min: 0, max: 3)
+@end example
+
 @menu
 * Tunable names::  The structure of a tunable name
 * Memory Allocation Tunables::  Tunables in the memory allocation subsystem