This is the mail archive of the libc-alpha@sourceware.org mailing list for the glibc project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: [PATCH 1/9] Add vectorized getenv for glibc use


On 05/10/2013 06:41 PM, Andi Kleen wrote:
> From: Andi Kleen <ak@linux.intel.com>
> 
> This adds a general "vectorized getenv" for glibc internal use.
> The motivation is to allow subsystems to access environment variables
> cheaply without having to rescan the environment completely.
> 
> The dynamic linker already walks the environment to look for its
> LD_* variables. Extend this code to look for a number of
> pre-registered GLIBC_* variables too. This can be done at basically
> no additional cost. The only two variables currently pre-registered
> are for the lock elision code.
> 
> For static builds which do not use the dynamic linker a similar
> environment walking function is called at init time.
> 
> The variable values are saved in a global array that can be directly
> accessed by libc and related libraries like libpthread.

Thanks for working on this orthogonal piece of infrastructure!

> 2013-05-10  Andi Kleen  <ak@linux.intel.com>
> 
> 	* elf/Makefile (glibc-var.o): Add new file.
> 	* elf/Versions (_dl_glibc_var, __glibc_var_init): Export new symbols
> 	as GLIBC_PRIVATE.
> 	* elf/dl-environ.c (_dl_next_ld_env_entry): Look for GLIBC_ prefixes
> 	too. Return first character in new argument.
> 	* elf/dl-glibc-var.c (struct glibc_var): Define global array.
> 	(__record_glibc_var): New function to save glibc variables.
> 	(next_env_entry): Function to walk environment.
> 	(__glibc_var_init): Fallback function for static builds.
> 	* elf/rtld.c: Update comment.
> 	(process_envvars): Handle GLIBC_ variables and call __record_glibc_var.
> 	* include/glibc-var.h: New file.
> 	* sysdeps/generic/ldsodefs.h (_dl_next_ld_env_entry): Update prototype.

This code looks good to me modulo Andreas' comments.

You read my mind when I was thinking of this implementation. A global
variable with O(1) lookup using a list of known values is great. Also
hooking into the already running O(n^2) lookup that the dynamic loader
is doing ensures we don't add any significant runtime cost. From a security
perspective there is no additional worry, these parameters tune
the implementation and don't carry pointers to code, just like any
other piece of global data the library has.

I think this can go in as long as Roland has no objections. Roland has
expressed some concerns with using environment variables to expose glibc
tunables to users. If I understand correctly I think Roland's concerns
were around performance, and control (user vs. system admin). I'll
let Roland speak for himself though.

Personally I think this code you've added will be very useful for other
things I am working on and addresses the major complaint that checking
env vars is a startup performance issue. Folding the checks into one
check done at startup is a great idea.

Siddhesh, Could you take advantage of this to expose the pthread
default stack size env var with minimal performance impact? Would
this not also solve the issue of a preloaded DSO not working with
static applications?

> ---
>  elf/Makefile               |    1 +
>  elf/Versions               |    2 +
>  elf/dl-environ.c           |   19 +++++++-
>  elf/dl-glibc-var.c         |  107 ++++++++++++++++++++++++++++++++++++++++++++
>  elf/rtld.c                 |   17 ++++++-
>  include/glibc-var.h        |   48 ++++++++++++++++++++
>  sysdeps/generic/ldsodefs.h |    6 +-
>  7 files changed, 193 insertions(+), 7 deletions(-)
>  create mode 100644 elf/dl-glibc-var.c
>  create mode 100644 include/glibc-var.h
> 
> diff --git a/elf/Makefile b/elf/Makefile
> index c01ca9e..7e02623 100644
> --- a/elf/Makefile
> +++ b/elf/Makefile
> @@ -35,6 +35,7 @@ dl-routines	= $(addprefix dl-,load lookup object reloc deps hwcaps \
>  ifeq (yes,$(use-ldconfig))
>  dl-routines += dl-cache
>  endif
> +dl-routines += dl-glibc-var
>  all-dl-routines = $(dl-routines) $(sysdep-dl-routines)
>  # But they are absent from the shared libc, because that code is in ld.so.
>  elide-routines.os = $(all-dl-routines) dl-support enbl-secure dl-origin \
> diff --git a/elf/Versions b/elf/Versions
> index 2383992..caf40fc 100644
> --- a/elf/Versions
> +++ b/elf/Versions
> @@ -54,6 +54,8 @@ ld {
>      _dl_argv; _dl_find_dso_for_object; _dl_get_tls_static_info;
>      _dl_deallocate_tls; _dl_make_stack_executable; _dl_out_of_memory;
>      _dl_rtld_di_serinfo; _dl_starting_up; _dl_tls_setup;
> +    _dl_glibc_var;
> +    __glibc_var_init;
>      _rtld_global; _rtld_global_ro;
>  
>      # Only here for gdb while a better method is developed.
> diff --git a/elf/dl-environ.c b/elf/dl-environ.c
> index b971569..bdc6c49 100644
> --- a/elf/dl-environ.c
> +++ b/elf/dl-environ.c
> @@ -22,10 +22,10 @@
>  #include <ldsodefs.h>
>  
>  /* Walk through the environment of the process and return all entries
> -   starting with `LD_'.  */
> +   starting with `LD_' or 'GLIBC_'.  */
>  char *
>  internal_function
> -_dl_next_ld_env_entry (char ***position)
> +_dl_next_ld_env_entry (char ***position, char *first)
>  {
>    char **current = *position;
>    char *result = NULL;
> @@ -35,6 +35,7 @@ _dl_next_ld_env_entry (char ***position)
>        if (__builtin_expect ((*current)[0] == 'L', 0)
>  	  && (*current)[1] == 'D' && (*current)[2] == '_')
>  	{
> +	  *first = (*current)[0];
>  	  result = &(*current)[3];
>  
>  	  /* Save current position for next visit.  */
> @@ -43,6 +44,20 @@ _dl_next_ld_env_entry (char ***position)
>  	  break;
>  	}
>  
> +      if (__builtin_expect ((*current)[0] == 'G', 0)
> +	  && (*current)[1] == 'L' && (*current)[2] == 'I'
> +	  && (*current)[3] == 'B' && (*current)[4] == 'C'
> +	  && (*current)[5] == '_')
> +        {
> +	  *first = (*current)[0];
> +	  result = &(*current)[6];
> +
> +	  /* Save current position for next visit.  */
> +	  *position = ++current;
> +
> +	  break;
> +        }
> +
>        ++current;
>      }
>  
> diff --git a/elf/dl-glibc-var.c b/elf/dl-glibc-var.c
> new file mode 100644
> index 0000000..e43c5ee
> --- /dev/null
> +++ b/elf/dl-glibc-var.c
> @@ -0,0 +1,107 @@
> +/* Fast access to GLIBC_* environment variables, without having to walk
> +   the environment multiple times.
> +   Copyright (C) 2013 Free Software Foundation, Inc.
> +
> +   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
> +   <http://www.gnu.org/licenses/>.  */
> +
> +#include <string.h>
> +#include <glibc-var.h>
> +
> +struct glibc_var _dl_glibc_var[] = 
> +{
> +  [GLIBC_VAR_MUTEX]  = { "MUTEX",  5, NULL },
> +  [GLIBC_VAR_RWLOCK] = { "RWLOCK", 6, NULL },
> +  /* Add more GLIBC_ variables here.  */
> +  [GLIBC_VAR_MAX] =    { NULL, 0, NULL }
> +};
> +
> +internal_function void
> +__record_glibc_var (char *name, int len, char *val)
> +{
> +  int i;
> +
> +  for (i = 0; i < GLIBC_VAR_MAX; i++)
> +    {
> +      struct glibc_var *v = &_dl_glibc_var[i];
> +
> +      if (len == v->len && !memcmp (v->name, name, v->len))
> +        {
> +	  v->val = val;
> +	  break;
> +	}
> +    }
> +  /* Ignore unknown GLIBC_ variables.  */
> +}
> +
> +#ifndef SHARED
> +
> +/* If SHARED the env walk is shared with rtld.c.  */
> +
> +static char *
> +next_env_entry (char first, char ***position)
> +{
> +  char **current = *position;
> +  char *result = NULL;
> +
> +  while (*current != NULL)
> +    {
> +      if ((*current)[0] == first)
> +	{
> +	  result = *current;
> +	  *position = ++current;
> +	  break;
> +	}
> +
> +      ++current;
> +    }
> +
> +  return result;
> +}
> +
> +/* May be called from libpthread.  */
> +
> +void
> +__glibc_var_init (int argc __attribute__ ((unused)),
> +		  char **argv  __attribute__ ((unused)),
> +		  char **environ)
> +{
> +  char *envline;
> +  static int initialized;
> +
> +  if (initialized)
> +    return;
> +  initialized = 1;
> +
> +  while ((envline = next_env_entry ('G', &environ)) != NULL)
> +    {
> +      if (envline[1] == 'L' && envline[2] == 'I' && envline[3] == 'B'
> +	  && envline[4] == 'C' && envline[5] == '_')
> +	{
> +	  char *e = envline + 6;
> +	  while (*e && *e != '=')
> +	    e++;
> +	  if (*e == 0)
> +	    continue;
> +	  __record_glibc_var (envline + 6, e - (envline + 6), e + 1);
> +	}
> +    }
> +}
> +
> +void (*const __glibc_var_init_array []) (int, char **, char **)
> +  __attribute__ ((section (".preinit_array"), aligned (sizeof (void *)))) =
> +{
> +  &__glibc_var_init
> +};
> +#endif
> diff --git a/elf/rtld.c b/elf/rtld.c
> index 23238ad..c76be70 100644
> --- a/elf/rtld.c
> +++ b/elf/rtld.c
> @@ -41,6 +41,7 @@
>  #include <tls.h>
>  #include <stap-probe.h>
>  #include <stackinfo.h>
> +#include <glibc-var.h>
>  
>  #include <assert.h>
>  
> @@ -2476,7 +2477,9 @@ process_dl_audit (char *str)
>  
>  /* Process all environments variables the dynamic linker must recognize.
>     Since all of them start with `LD_' we are a bit smarter while finding
> -   all the entries.  */
> +   all the entries.  In addition we also save a bunch of GLIBC_ variables
> +   used by other parts of glibc, so that each startup only has to walk the
> +   environment once.  */
>  extern char **_environ attribute_hidden;
>  
>  
> @@ -2487,12 +2490,13 @@ process_envvars (enum mode *modep)
>    char *envline;
>    enum mode mode = normal;
>    char *debug_output = NULL;
> +  char first;
>  
>    /* This is the default place for profiling data file.  */
>    GLRO(dl_profile_output)
>      = &"/var/tmp\0/var/profile"[INTUSE(__libc_enable_secure) ? 9 : 0];
>  
> -  while ((envline = _dl_next_ld_env_entry (&runp)) != NULL)
> +  while ((envline = _dl_next_ld_env_entry (&runp, &first)) != NULL)
>      {
>        size_t len = 0;
>  
> @@ -2505,6 +2509,15 @@ process_envvars (enum mode *modep)
>  	   invalid memory below.  */
>  	continue;
>  
> +      /* Must be for GLIBC_ */
> +      if (first == 'G')
> + 	{
> +	  __record_glibc_var (envline, len, envline + len + 1);
> +	  continue;
> +	}
> +
> +      /* Must be for LD_ */
> +
>        switch (len)
>  	{
>  	case 4:
> diff --git a/include/glibc-var.h b/include/glibc-var.h
> new file mode 100644
> index 0000000..e84fc42
> --- /dev/null
> +++ b/include/glibc-var.h
> @@ -0,0 +1,48 @@
> +/* Fast access to GLIBC_* environment variables, without having to walk
> +   the environment. Register new ones in in elf/glibc-var.c
> +   Copyright (C) 2013 Free Software Foundation, Inc.
> +
> +   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
> +   <http://www.gnu.org/licenses/>.  */
> +#ifndef _GLIBC_VAR_H
> +#define _GLIBC_VAR_H 1
> +
> +#include <libc-symbols.h>
> +
> +enum
> +{
> + GLIBC_VAR_MUTEX,
> + GLIBC_VAR_RWLOCK,
> + GLIBC_VAR_MAX
> +};
> +
> +struct glibc_var
> +{
> +  const char *name;
> +  int len; 
> +  char *val;
> +};
> +
> +extern struct glibc_var _dl_glibc_var[];
> +extern void __record_glibc_var (char *name, int len, char *val) internal_function;
> +
> +/* Call this if you're in a constructor that may run before glibc-var's.  */
> +#ifndef SHARED
> +extern void __glibc_var_init (int ac, char **av, char **env);
> +#else
> +/* For shared this is always done in the dynamic linker early enough.  */
> +# define __glibc_var_init(a,b,c) do {} while (0)
> +#endif
> +
> +#endif
> diff --git a/sysdeps/generic/ldsodefs.h b/sysdeps/generic/ldsodefs.h
> index 41684f3..b56b9ed 100644
> --- a/sysdeps/generic/ldsodefs.h
> +++ b/sysdeps/generic/ldsodefs.h
> @@ -889,9 +889,9 @@ extern void _dl_mcount_wrapper (void *selfpc);
>  /* Show the members of the auxiliary array passed up from the kernel.  */
>  extern void _dl_show_auxv (void) internal_function;
>  
> -/* Return all environment variables starting with `LD_', one after the
> -   other.  */
> -extern char *_dl_next_ld_env_entry (char ***position) internal_function;
> +/* Return all environment variables starting with `LD_' or `GLIBC_', one
> +   after the other.  */
> +extern char *_dl_next_ld_env_entry (char ***position, char *first) internal_function;
>  
>  /* Return an array with the names of the important hardware capabilities.  */
>  extern const struct r_strlenpair *_dl_important_hwcaps (const char *platform,
> 


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]