2017-01-01 02:02:17 +08:00
|
|
|
/* The tunable framework. See the README.tunables to know how to use the
|
|
|
|
tunable in a glibc module.
|
|
|
|
|
2021-01-03 03:32:25 +08:00
|
|
|
Copyright (C) 2016-2021 Free Software Foundation, Inc.
|
2017-01-01 02:02:17 +08:00
|
|
|
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
|
Prefer https to http for gnu.org and fsf.org URLs
Also, change sources.redhat.com to sourceware.org.
This patch was automatically generated by running the following shell
script, which uses GNU sed, and which avoids modifying files imported
from upstream:
sed -ri '
s,(http|ftp)(://(.*\.)?(gnu|fsf|sourceware)\.org($|[^.]|\.[^a-z])),https\2,g
s,(http|ftp)(://(.*\.)?)sources\.redhat\.com($|[^.]|\.[^a-z]),https\2sourceware.org\4,g
' \
$(find $(git ls-files) -prune -type f \
! -name '*.po' \
! -name 'ChangeLog*' \
! -path COPYING ! -path COPYING.LIB \
! -path manual/fdl-1.3.texi ! -path manual/lgpl-2.1.texi \
! -path manual/texinfo.tex ! -path scripts/config.guess \
! -path scripts/config.sub ! -path scripts/install-sh \
! -path scripts/mkinstalldirs ! -path scripts/move-if-change \
! -path INSTALL ! -path locale/programs/charmap-kw.h \
! -path po/libc.pot ! -path sysdeps/gnu/errlist.c \
! '(' -name configure \
-execdir test -f configure.ac -o -f configure.in ';' ')' \
! '(' -name preconfigure \
-execdir test -f preconfigure.ac ';' ')' \
-print)
and then by running 'make dist-prepare' to regenerate files built
from the altered files, and then executing the following to cleanup:
chmod a+x sysdeps/unix/sysv/linux/riscv/configure
# Omit irrelevant whitespace and comment-only changes,
# perhaps from a slightly-different Autoconf version.
git checkout -f \
sysdeps/csky/configure \
sysdeps/hppa/configure \
sysdeps/riscv/configure \
sysdeps/unix/sysv/linux/csky/configure
# Omit changes that caused a pre-commit check to fail like this:
# remote: *** error: sysdeps/powerpc/powerpc64/ppc-mcount.S: trailing lines
git checkout -f \
sysdeps/powerpc/powerpc64/ppc-mcount.S \
sysdeps/unix/sysv/linux/s390/s390-64/syscall.S
# Omit change that caused a pre-commit check to fail like this:
# remote: *** error: sysdeps/sparc/sparc64/multiarch/memcpy-ultra3.S: last line does not end in newline
git checkout -f sysdeps/sparc/sparc64/multiarch/memcpy-ultra3.S
2019-09-07 13:40:42 +08:00
|
|
|
<https://www.gnu.org/licenses/>. */
|
2017-01-01 02:02:17 +08:00
|
|
|
|
2021-01-15 20:49:24 +08:00
|
|
|
/* Mark symbols hidden in static PIE for early self relocation to work. */
|
|
|
|
#if BUILD_PIE_DEFAULT
|
|
|
|
# pragma GCC visibility push(hidden)
|
|
|
|
#endif
|
i386: Add <startup.h> [BZ #21913]
On Linux/i386, there are 3 ways to make a system call:
1. call *%gs:SYSINFO_OFFSET. This requires TLS initialization.
2. call *_dl_sysinfo. This requires relocation of _dl_sysinfo.
3. int $0x80. This is slower than #2 and #3, but works everywhere.
When an object file is compiled with PIC, #1 is prefered since it is
faster than #3 and doesn't require relocation of _dl_sysinfo. For
dynamic executables, ld.so initializes TLS. However, for static
executables, before TLS is initialized by __libc_setup_tls, #3 should
be used for system calls.
This patch adds <startup.h> which defines _startup_fatal and defaults
it to __libc_fatal. It replaces __libc_fatal with _startup_fatal in
static executables where it is called before __libc_setup_tls is called.
This header file is included in all files containing functions which are
called before __libc_setup_tls is called. On Linux/i386, when PIE is
enabled by default, _startup_fatal is turned into ABORT_INSTRUCTION and
I386_USE_SYSENTER is defined to 0 so that "int $0x80" is used for system
calls before __libc_setup_tls is called.
Tested on i686 and x86-64. Without this patch, all statically-linked
tests will fail on i686 when the compiler defaults to -fPIE.
[BZ #21913]
* csu/libc-tls.c: Include <startup.h> first.
(__libc_setup_tls): Call _startup_fatal instead of __libc_fatal.
* elf/dl-tunables.c: Include <startup.h> first.
* include/libc-symbols.h (BUILD_PIE_DEFAULT): New.
* sysdeps/generic/startup.h: New file.
* sysdeps/unix/sysv/linux/i386/startup.h: Likewise.
* sysdeps/unix/sysv/linux/i386/brk.c [BUILD_PIE_DEFAULT != 0]
(I386_USE_SYSENTER): New. Defined to 0.
2017-08-08 23:41:08 +08:00
|
|
|
#include <startup.h>
|
2017-01-01 02:02:17 +08:00
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <sysdep.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <ldsodefs.h>
|
2020-07-12 21:04:53 +08:00
|
|
|
#include <array_length.h>
|
2017-01-01 02:02:17 +08:00
|
|
|
|
|
|
|
#define TUNABLES_INTERNAL 1
|
|
|
|
#include "dl-tunables.h"
|
|
|
|
|
2017-07-18 05:11:12 +08:00
|
|
|
#include <not-errno.h>
|
|
|
|
|
2017-01-01 02:04:04 +08:00
|
|
|
#if TUNABLES_FRONTEND == TUNABLES_FRONTEND_valstring
|
|
|
|
# define GLIBC_TUNABLES "GLIBC_TUNABLES"
|
|
|
|
#endif
|
2017-01-01 02:03:27 +08:00
|
|
|
|
2017-01-01 02:04:04 +08:00
|
|
|
#if TUNABLES_FRONTEND == TUNABLES_FRONTEND_valstring
|
2017-01-01 02:03:27 +08:00
|
|
|
static char *
|
|
|
|
tunables_strdup (const char *in)
|
|
|
|
{
|
|
|
|
size_t i = 0;
|
|
|
|
|
|
|
|
while (in[i++] != '\0');
|
|
|
|
char *out = __sbrk (i);
|
|
|
|
|
2019-12-14 02:36:58 +08:00
|
|
|
/* For most of the tunables code, we ignore user errors. However,
|
|
|
|
this is a system error - and running out of memory at program
|
|
|
|
startup should be reported, so we do. */
|
2017-01-01 02:03:27 +08:00
|
|
|
if (out == (void *)-1)
|
2019-12-14 02:36:58 +08:00
|
|
|
_dl_fatal_printf ("sbrk() failure while processing tunables\n");
|
2017-01-01 02:03:27 +08:00
|
|
|
|
|
|
|
i--;
|
|
|
|
|
|
|
|
while (i-- > 0)
|
|
|
|
out[i] = in[i];
|
|
|
|
|
|
|
|
return out;
|
|
|
|
}
|
2017-01-01 02:04:04 +08:00
|
|
|
#endif
|
2017-01-01 02:03:27 +08:00
|
|
|
|
2017-01-01 02:02:17 +08:00
|
|
|
static char **
|
2017-02-02 18:16:01 +08:00
|
|
|
get_next_env (char **envp, char **name, size_t *namelen, char **val,
|
|
|
|
char ***prev_envp)
|
2017-01-01 02:02:17 +08:00
|
|
|
{
|
|
|
|
while (envp != NULL && *envp != NULL)
|
|
|
|
{
|
2017-02-02 18:16:01 +08:00
|
|
|
char **prev = envp;
|
2017-01-20 03:15:09 +08:00
|
|
|
char *envline = *envp++;
|
2017-01-01 02:02:17 +08:00
|
|
|
int len = 0;
|
|
|
|
|
|
|
|
while (envline[len] != '\0' && envline[len] != '=')
|
|
|
|
len++;
|
|
|
|
|
|
|
|
/* Just the name and no value, go to the next one. */
|
|
|
|
if (envline[len] == '\0')
|
|
|
|
continue;
|
|
|
|
|
|
|
|
*name = envline;
|
|
|
|
*namelen = len;
|
|
|
|
*val = &envline[len + 1];
|
2017-02-02 18:16:01 +08:00
|
|
|
*prev_envp = prev;
|
2017-01-01 02:02:17 +08:00
|
|
|
|
2017-01-20 03:15:09 +08:00
|
|
|
return envp;
|
2017-01-01 02:02:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2021-02-05 15:48:58 +08:00
|
|
|
do_tunable_update_val (tunable_t *cur, const tunable_val_t *valp,
|
|
|
|
const tunable_num_t *minp,
|
|
|
|
const tunable_num_t *maxp)
|
2017-01-01 02:02:17 +08:00
|
|
|
{
|
2021-02-05 15:48:58 +08:00
|
|
|
tunable_num_t val, min, max;
|
2017-05-17 15:41:55 +08:00
|
|
|
|
2021-02-05 15:48:58 +08:00
|
|
|
if (cur->type.type_code == TUNABLE_TYPE_STRING)
|
|
|
|
{
|
|
|
|
cur->val.strval = valp->strval;
|
|
|
|
cur->initialized = true;
|
|
|
|
return;
|
|
|
|
}
|
2017-05-17 15:41:55 +08:00
|
|
|
|
2021-02-05 15:48:58 +08:00
|
|
|
val = valp->numval;
|
|
|
|
min = minp != NULL ? *minp : cur->type.min;
|
|
|
|
max = maxp != NULL ? *maxp : cur->type.max;
|
|
|
|
|
|
|
|
/* We allow only increasingly restrictive bounds. */
|
|
|
|
if (min < cur->type.min)
|
|
|
|
min = cur->type.min;
|
|
|
|
|
|
|
|
if (max > cur->type.max)
|
|
|
|
max = cur->type.max;
|
|
|
|
|
|
|
|
/* Skip both bounds if they're inconsistent. */
|
|
|
|
if (min > max)
|
2017-01-01 02:02:17 +08:00
|
|
|
{
|
2021-02-05 15:48:58 +08:00
|
|
|
min = cur->type.min;
|
|
|
|
max = cur->type.max;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Write everything out if the value and the bounds are valid. */
|
|
|
|
if (min <= val && val <= max)
|
|
|
|
{
|
|
|
|
cur->val.numval = val;
|
|
|
|
cur->type.min = min;
|
|
|
|
cur->type.max = max;
|
|
|
|
cur->initialized = true;
|
2017-01-01 02:02:17 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
tunables: Clean up hooks to get and set tunables
The TUNABLE_SET_VALUE and family of macros (and my later attempt to
add a TUNABLE_GET) never quite went together very well because the
overall interface was not clearly defined. This patch is an attempt
to do just that.
This patch consolidates the API to two simple sets of macros,
TUNABLE_GET* and TUNABLE_SET*. If TUNABLE_NAMESPACE is defined,
TUNABLE_GET takes just the tunable name, type and a (optionally NULL)
callback function to get the value of the tunable. The callback
function, if non-NULL, is called if the tunable was externally set
(i.e. via GLIBC_TUNABLES or any future mechanism). For example:
val = TUNABLE_GET (check, int32_t, check_callback)
returns the value of the glibc.malloc.check tunable (assuming
TUNABLE_NAMESPACE is set to malloc) as an int32_t into VAL after
calling check_callback.
Likewise, TUNABLE_SET can be used to set the value of the tunable,
although this is currently possible only in the dynamic linker before
it relocates itself. For example:
TUNABLE_SET (check, int32_t, 2)
will set glibc.malloc.check to 2. Of course, this is not possible
since we set (or read) glibc.malloc.check long after it is relocated.
To access or set a tunable outside of TUNABLE_NAMESPACE, use the
TUNABLE_GET_FULL and TUNABLE_SET_FULL macros, which have the following
prototype:
TUNABLE_GET_FULL (glibc, tune, hwcap_mask, uint64_t, NULL)
TUNABLE_SET_FULL (glibc, tune, hwcap_mask, uint64_t, 0xffff)
In future the tunable list may get split into mutable and immutable
tunables where mutable tunables can be modified by the library and
userspace after relocation as well and TUNABLE_SET will be more useful
than it currently is. However whenever we actually do that split, we
will have to ensure that the mutable tunables are protected with
locks.
* elf/Versions (__tunable_set_val): Rename to __tunable_get_val.
* elf/dl-tunables.c: Likewise.
(do_tunable_update_val): New function.
(__tunable_set_val): New function.
(__tunable_get_val): Call CB only if the tunable was externally
initialized.
(tunables_strtoul): Replace strval with initialized.
* elf/dl-tunables.h (strval): Replace with a bool initialized.
(TUNABLE_ENUM_NAME, TUNABLE_ENUM_NAME1): Adjust names to
prevent collision.
(__tunable_set_val): New function.
(TUNABLE_GET, TUNABLE_GET_FULL): New macros.
(TUNABLE_SET, TUNABLE_SET_FULL): Likewise.
(TUNABLE_SET_VAL): Remove.
(TUNABLE_SET_VAL_WITH_CALLBACK): Likewise.
* README.tunables: Document the new macros.
* malloc/arena.c (ptmalloc_init): Adjust.
2017-06-01 22:54:46 +08:00
|
|
|
/* Validate range of the input value and initialize the tunable CUR if it looks
|
|
|
|
good. */
|
|
|
|
static void
|
|
|
|
tunable_initialize (tunable_t *cur, const char *strval)
|
|
|
|
{
|
2021-02-05 15:48:58 +08:00
|
|
|
tunable_val_t val;
|
tunables: Clean up hooks to get and set tunables
The TUNABLE_SET_VALUE and family of macros (and my later attempt to
add a TUNABLE_GET) never quite went together very well because the
overall interface was not clearly defined. This patch is an attempt
to do just that.
This patch consolidates the API to two simple sets of macros,
TUNABLE_GET* and TUNABLE_SET*. If TUNABLE_NAMESPACE is defined,
TUNABLE_GET takes just the tunable name, type and a (optionally NULL)
callback function to get the value of the tunable. The callback
function, if non-NULL, is called if the tunable was externally set
(i.e. via GLIBC_TUNABLES or any future mechanism). For example:
val = TUNABLE_GET (check, int32_t, check_callback)
returns the value of the glibc.malloc.check tunable (assuming
TUNABLE_NAMESPACE is set to malloc) as an int32_t into VAL after
calling check_callback.
Likewise, TUNABLE_SET can be used to set the value of the tunable,
although this is currently possible only in the dynamic linker before
it relocates itself. For example:
TUNABLE_SET (check, int32_t, 2)
will set glibc.malloc.check to 2. Of course, this is not possible
since we set (or read) glibc.malloc.check long after it is relocated.
To access or set a tunable outside of TUNABLE_NAMESPACE, use the
TUNABLE_GET_FULL and TUNABLE_SET_FULL macros, which have the following
prototype:
TUNABLE_GET_FULL (glibc, tune, hwcap_mask, uint64_t, NULL)
TUNABLE_SET_FULL (glibc, tune, hwcap_mask, uint64_t, 0xffff)
In future the tunable list may get split into mutable and immutable
tunables where mutable tunables can be modified by the library and
userspace after relocation as well and TUNABLE_SET will be more useful
than it currently is. However whenever we actually do that split, we
will have to ensure that the mutable tunables are protected with
locks.
* elf/Versions (__tunable_set_val): Rename to __tunable_get_val.
* elf/dl-tunables.c: Likewise.
(do_tunable_update_val): New function.
(__tunable_set_val): New function.
(__tunable_get_val): Call CB only if the tunable was externally
initialized.
(tunables_strtoul): Replace strval with initialized.
* elf/dl-tunables.h (strval): Replace with a bool initialized.
(TUNABLE_ENUM_NAME, TUNABLE_ENUM_NAME1): Adjust names to
prevent collision.
(__tunable_set_val): New function.
(TUNABLE_GET, TUNABLE_GET_FULL): New macros.
(TUNABLE_SET, TUNABLE_SET_FULL): Likewise.
(TUNABLE_SET_VAL): Remove.
(TUNABLE_SET_VAL_WITH_CALLBACK): Likewise.
* README.tunables: Document the new macros.
* malloc/arena.c (ptmalloc_init): Adjust.
2017-06-01 22:54:46 +08:00
|
|
|
|
|
|
|
if (cur->type.type_code != TUNABLE_TYPE_STRING)
|
2021-02-05 15:48:58 +08:00
|
|
|
val.numval = (tunable_num_t) _dl_strtoul (strval, NULL);
|
tunables: Clean up hooks to get and set tunables
The TUNABLE_SET_VALUE and family of macros (and my later attempt to
add a TUNABLE_GET) never quite went together very well because the
overall interface was not clearly defined. This patch is an attempt
to do just that.
This patch consolidates the API to two simple sets of macros,
TUNABLE_GET* and TUNABLE_SET*. If TUNABLE_NAMESPACE is defined,
TUNABLE_GET takes just the tunable name, type and a (optionally NULL)
callback function to get the value of the tunable. The callback
function, if non-NULL, is called if the tunable was externally set
(i.e. via GLIBC_TUNABLES or any future mechanism). For example:
val = TUNABLE_GET (check, int32_t, check_callback)
returns the value of the glibc.malloc.check tunable (assuming
TUNABLE_NAMESPACE is set to malloc) as an int32_t into VAL after
calling check_callback.
Likewise, TUNABLE_SET can be used to set the value of the tunable,
although this is currently possible only in the dynamic linker before
it relocates itself. For example:
TUNABLE_SET (check, int32_t, 2)
will set glibc.malloc.check to 2. Of course, this is not possible
since we set (or read) glibc.malloc.check long after it is relocated.
To access or set a tunable outside of TUNABLE_NAMESPACE, use the
TUNABLE_GET_FULL and TUNABLE_SET_FULL macros, which have the following
prototype:
TUNABLE_GET_FULL (glibc, tune, hwcap_mask, uint64_t, NULL)
TUNABLE_SET_FULL (glibc, tune, hwcap_mask, uint64_t, 0xffff)
In future the tunable list may get split into mutable and immutable
tunables where mutable tunables can be modified by the library and
userspace after relocation as well and TUNABLE_SET will be more useful
than it currently is. However whenever we actually do that split, we
will have to ensure that the mutable tunables are protected with
locks.
* elf/Versions (__tunable_set_val): Rename to __tunable_get_val.
* elf/dl-tunables.c: Likewise.
(do_tunable_update_val): New function.
(__tunable_set_val): New function.
(__tunable_get_val): Call CB only if the tunable was externally
initialized.
(tunables_strtoul): Replace strval with initialized.
* elf/dl-tunables.h (strval): Replace with a bool initialized.
(TUNABLE_ENUM_NAME, TUNABLE_ENUM_NAME1): Adjust names to
prevent collision.
(__tunable_set_val): New function.
(TUNABLE_GET, TUNABLE_GET_FULL): New macros.
(TUNABLE_SET, TUNABLE_SET_FULL): Likewise.
(TUNABLE_SET_VAL): Remove.
(TUNABLE_SET_VAL_WITH_CALLBACK): Likewise.
* README.tunables: Document the new macros.
* malloc/arena.c (ptmalloc_init): Adjust.
2017-06-01 22:54:46 +08:00
|
|
|
else
|
2021-02-05 15:48:58 +08:00
|
|
|
val.strval = strval;
|
|
|
|
do_tunable_update_val (cur, &val, NULL, NULL);
|
tunables: Clean up hooks to get and set tunables
The TUNABLE_SET_VALUE and family of macros (and my later attempt to
add a TUNABLE_GET) never quite went together very well because the
overall interface was not clearly defined. This patch is an attempt
to do just that.
This patch consolidates the API to two simple sets of macros,
TUNABLE_GET* and TUNABLE_SET*. If TUNABLE_NAMESPACE is defined,
TUNABLE_GET takes just the tunable name, type and a (optionally NULL)
callback function to get the value of the tunable. The callback
function, if non-NULL, is called if the tunable was externally set
(i.e. via GLIBC_TUNABLES or any future mechanism). For example:
val = TUNABLE_GET (check, int32_t, check_callback)
returns the value of the glibc.malloc.check tunable (assuming
TUNABLE_NAMESPACE is set to malloc) as an int32_t into VAL after
calling check_callback.
Likewise, TUNABLE_SET can be used to set the value of the tunable,
although this is currently possible only in the dynamic linker before
it relocates itself. For example:
TUNABLE_SET (check, int32_t, 2)
will set glibc.malloc.check to 2. Of course, this is not possible
since we set (or read) glibc.malloc.check long after it is relocated.
To access or set a tunable outside of TUNABLE_NAMESPACE, use the
TUNABLE_GET_FULL and TUNABLE_SET_FULL macros, which have the following
prototype:
TUNABLE_GET_FULL (glibc, tune, hwcap_mask, uint64_t, NULL)
TUNABLE_SET_FULL (glibc, tune, hwcap_mask, uint64_t, 0xffff)
In future the tunable list may get split into mutable and immutable
tunables where mutable tunables can be modified by the library and
userspace after relocation as well and TUNABLE_SET will be more useful
than it currently is. However whenever we actually do that split, we
will have to ensure that the mutable tunables are protected with
locks.
* elf/Versions (__tunable_set_val): Rename to __tunable_get_val.
* elf/dl-tunables.c: Likewise.
(do_tunable_update_val): New function.
(__tunable_set_val): New function.
(__tunable_get_val): Call CB only if the tunable was externally
initialized.
(tunables_strtoul): Replace strval with initialized.
* elf/dl-tunables.h (strval): Replace with a bool initialized.
(TUNABLE_ENUM_NAME, TUNABLE_ENUM_NAME1): Adjust names to
prevent collision.
(__tunable_set_val): New function.
(TUNABLE_GET, TUNABLE_GET_FULL): New macros.
(TUNABLE_SET, TUNABLE_SET_FULL): Likewise.
(TUNABLE_SET_VAL): Remove.
(TUNABLE_SET_VAL_WITH_CALLBACK): Likewise.
* README.tunables: Document the new macros.
* malloc/arena.c (ptmalloc_init): Adjust.
2017-06-01 22:54:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2021-02-05 15:48:58 +08:00
|
|
|
__tunable_set_val (tunable_id_t id, tunable_val_t *valp, tunable_num_t *minp,
|
|
|
|
tunable_num_t *maxp)
|
tunables: Clean up hooks to get and set tunables
The TUNABLE_SET_VALUE and family of macros (and my later attempt to
add a TUNABLE_GET) never quite went together very well because the
overall interface was not clearly defined. This patch is an attempt
to do just that.
This patch consolidates the API to two simple sets of macros,
TUNABLE_GET* and TUNABLE_SET*. If TUNABLE_NAMESPACE is defined,
TUNABLE_GET takes just the tunable name, type and a (optionally NULL)
callback function to get the value of the tunable. The callback
function, if non-NULL, is called if the tunable was externally set
(i.e. via GLIBC_TUNABLES or any future mechanism). For example:
val = TUNABLE_GET (check, int32_t, check_callback)
returns the value of the glibc.malloc.check tunable (assuming
TUNABLE_NAMESPACE is set to malloc) as an int32_t into VAL after
calling check_callback.
Likewise, TUNABLE_SET can be used to set the value of the tunable,
although this is currently possible only in the dynamic linker before
it relocates itself. For example:
TUNABLE_SET (check, int32_t, 2)
will set glibc.malloc.check to 2. Of course, this is not possible
since we set (or read) glibc.malloc.check long after it is relocated.
To access or set a tunable outside of TUNABLE_NAMESPACE, use the
TUNABLE_GET_FULL and TUNABLE_SET_FULL macros, which have the following
prototype:
TUNABLE_GET_FULL (glibc, tune, hwcap_mask, uint64_t, NULL)
TUNABLE_SET_FULL (glibc, tune, hwcap_mask, uint64_t, 0xffff)
In future the tunable list may get split into mutable and immutable
tunables where mutable tunables can be modified by the library and
userspace after relocation as well and TUNABLE_SET will be more useful
than it currently is. However whenever we actually do that split, we
will have to ensure that the mutable tunables are protected with
locks.
* elf/Versions (__tunable_set_val): Rename to __tunable_get_val.
* elf/dl-tunables.c: Likewise.
(do_tunable_update_val): New function.
(__tunable_set_val): New function.
(__tunable_get_val): Call CB only if the tunable was externally
initialized.
(tunables_strtoul): Replace strval with initialized.
* elf/dl-tunables.h (strval): Replace with a bool initialized.
(TUNABLE_ENUM_NAME, TUNABLE_ENUM_NAME1): Adjust names to
prevent collision.
(__tunable_set_val): New function.
(TUNABLE_GET, TUNABLE_GET_FULL): New macros.
(TUNABLE_SET, TUNABLE_SET_FULL): Likewise.
(TUNABLE_SET_VAL): Remove.
(TUNABLE_SET_VAL_WITH_CALLBACK): Likewise.
* README.tunables: Document the new macros.
* malloc/arena.c (ptmalloc_init): Adjust.
2017-06-01 22:54:46 +08:00
|
|
|
{
|
|
|
|
tunable_t *cur = &tunable_list[id];
|
|
|
|
|
2020-06-02 05:11:32 +08:00
|
|
|
do_tunable_update_val (cur, valp, minp, maxp);
|
tunables: Clean up hooks to get and set tunables
The TUNABLE_SET_VALUE and family of macros (and my later attempt to
add a TUNABLE_GET) never quite went together very well because the
overall interface was not clearly defined. This patch is an attempt
to do just that.
This patch consolidates the API to two simple sets of macros,
TUNABLE_GET* and TUNABLE_SET*. If TUNABLE_NAMESPACE is defined,
TUNABLE_GET takes just the tunable name, type and a (optionally NULL)
callback function to get the value of the tunable. The callback
function, if non-NULL, is called if the tunable was externally set
(i.e. via GLIBC_TUNABLES or any future mechanism). For example:
val = TUNABLE_GET (check, int32_t, check_callback)
returns the value of the glibc.malloc.check tunable (assuming
TUNABLE_NAMESPACE is set to malloc) as an int32_t into VAL after
calling check_callback.
Likewise, TUNABLE_SET can be used to set the value of the tunable,
although this is currently possible only in the dynamic linker before
it relocates itself. For example:
TUNABLE_SET (check, int32_t, 2)
will set glibc.malloc.check to 2. Of course, this is not possible
since we set (or read) glibc.malloc.check long after it is relocated.
To access or set a tunable outside of TUNABLE_NAMESPACE, use the
TUNABLE_GET_FULL and TUNABLE_SET_FULL macros, which have the following
prototype:
TUNABLE_GET_FULL (glibc, tune, hwcap_mask, uint64_t, NULL)
TUNABLE_SET_FULL (glibc, tune, hwcap_mask, uint64_t, 0xffff)
In future the tunable list may get split into mutable and immutable
tunables where mutable tunables can be modified by the library and
userspace after relocation as well and TUNABLE_SET will be more useful
than it currently is. However whenever we actually do that split, we
will have to ensure that the mutable tunables are protected with
locks.
* elf/Versions (__tunable_set_val): Rename to __tunable_get_val.
* elf/dl-tunables.c: Likewise.
(do_tunable_update_val): New function.
(__tunable_set_val): New function.
(__tunable_get_val): Call CB only if the tunable was externally
initialized.
(tunables_strtoul): Replace strval with initialized.
* elf/dl-tunables.h (strval): Replace with a bool initialized.
(TUNABLE_ENUM_NAME, TUNABLE_ENUM_NAME1): Adjust names to
prevent collision.
(__tunable_set_val): New function.
(TUNABLE_GET, TUNABLE_GET_FULL): New macros.
(TUNABLE_SET, TUNABLE_SET_FULL): Likewise.
(TUNABLE_SET_VAL): Remove.
(TUNABLE_SET_VAL_WITH_CALLBACK): Likewise.
* README.tunables: Document the new macros.
* malloc/arena.c (ptmalloc_init): Adjust.
2017-06-01 22:54:46 +08:00
|
|
|
}
|
|
|
|
|
2017-01-01 02:04:04 +08:00
|
|
|
#if TUNABLES_FRONTEND == TUNABLES_FRONTEND_valstring
|
2017-02-02 18:16:01 +08:00
|
|
|
/* Parse the tunable string TUNESTR and adjust it to drop any tunables that may
|
|
|
|
be unsafe for AT_SECURE processes so that it can be used as the new
|
|
|
|
environment variable value for GLIBC_TUNABLES. VALSTRING is the original
|
|
|
|
environment variable string which we use to make NULL terminated values so
|
|
|
|
that we don't have to allocate memory again for it. */
|
2017-01-01 02:03:27 +08:00
|
|
|
static void
|
2017-02-02 18:16:01 +08:00
|
|
|
parse_tunables (char *tunestr, char *valstring)
|
2017-01-01 02:03:27 +08:00
|
|
|
{
|
|
|
|
if (tunestr == NULL || *tunestr == '\0')
|
|
|
|
return;
|
|
|
|
|
|
|
|
char *p = tunestr;
|
|
|
|
|
|
|
|
while (true)
|
|
|
|
{
|
|
|
|
char *name = p;
|
|
|
|
size_t len = 0;
|
|
|
|
|
|
|
|
/* First, find where the name ends. */
|
|
|
|
while (p[len] != '=' && p[len] != ':' && p[len] != '\0')
|
|
|
|
len++;
|
|
|
|
|
|
|
|
/* If we reach the end of the string before getting a valid name-value
|
|
|
|
pair, bail out. */
|
|
|
|
if (p[len] == '\0')
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* We did not find a valid name-value pair before encountering the
|
|
|
|
colon. */
|
|
|
|
if (p[len]== ':')
|
|
|
|
{
|
|
|
|
p += len + 1;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
p += len + 1;
|
|
|
|
|
2017-02-02 18:16:01 +08:00
|
|
|
/* Take the value from the valstring since we need to NULL terminate it. */
|
|
|
|
char *value = &valstring[p - tunestr];
|
2017-01-01 02:03:27 +08:00
|
|
|
len = 0;
|
|
|
|
|
|
|
|
while (p[len] != ':' && p[len] != '\0')
|
|
|
|
len++;
|
|
|
|
|
|
|
|
/* Add the tunable if it exists. */
|
|
|
|
for (size_t i = 0; i < sizeof (tunable_list) / sizeof (tunable_t); i++)
|
|
|
|
{
|
|
|
|
tunable_t *cur = &tunable_list[i];
|
|
|
|
|
2017-07-01 01:28:39 +08:00
|
|
|
if (tunable_is_name (cur->name, name))
|
2017-01-01 02:03:27 +08:00
|
|
|
{
|
2017-02-02 18:16:01 +08:00
|
|
|
/* If we are in a secure context (AT_SECURE) then ignore the tunable
|
|
|
|
unless it is explicitly marked as secure. Tunable values take
|
2020-03-13 18:27:23 +08:00
|
|
|
precedence over their envvar aliases. */
|
2017-02-02 18:16:01 +08:00
|
|
|
if (__libc_enable_secure)
|
|
|
|
{
|
|
|
|
if (cur->security_level == TUNABLE_SECLEVEL_SXID_ERASE)
|
|
|
|
{
|
|
|
|
if (p[len] == '\0')
|
|
|
|
{
|
|
|
|
/* Last tunable in the valstring. Null-terminate and
|
|
|
|
return. */
|
|
|
|
*name = '\0';
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Remove the current tunable from the string. We do
|
|
|
|
this by overwriting the string starting from NAME
|
|
|
|
(which is where the current tunable begins) with
|
|
|
|
the remainder of the string. We then have P point
|
|
|
|
to NAME so that we continue in the correct
|
|
|
|
position in the valstring. */
|
|
|
|
char *q = &p[len + 1];
|
|
|
|
p = name;
|
|
|
|
while (*q != '\0')
|
|
|
|
*name++ = *q++;
|
|
|
|
name[0] = '\0';
|
|
|
|
len = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cur->security_level != TUNABLE_SECLEVEL_NONE)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
value[len] = '\0';
|
2017-01-01 02:03:27 +08:00
|
|
|
tunable_initialize (cur, value);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-02 18:16:01 +08:00
|
|
|
if (p[len] == '\0')
|
2017-01-01 02:03:27 +08:00
|
|
|
return;
|
2017-02-02 18:16:01 +08:00
|
|
|
else
|
|
|
|
p += len + 1;
|
2017-01-01 02:03:27 +08:00
|
|
|
}
|
|
|
|
}
|
2017-01-01 02:04:04 +08:00
|
|
|
#endif
|
2017-01-01 02:03:27 +08:00
|
|
|
|
2017-01-10 19:05:58 +08:00
|
|
|
/* Enable the glibc.malloc.check tunable in SETUID/SETGID programs only when
|
|
|
|
the system administrator has created the /etc/suid-debug file. This is a
|
|
|
|
special case where we want to conditionally enable/disable a tunable even
|
|
|
|
for setuid binaries. We use the special version of access() to avoid
|
|
|
|
setting ERRNO, which is a TLS variable since TLS has not yet been set
|
|
|
|
up. */
|
2019-02-07 01:16:43 +08:00
|
|
|
static __always_inline void
|
2017-01-10 19:05:58 +08:00
|
|
|
maybe_enable_malloc_check (void)
|
2017-01-01 02:02:17 +08:00
|
|
|
{
|
2017-02-02 18:16:01 +08:00
|
|
|
tunable_id_t id = TUNABLE_ENUM_NAME (glibc, malloc, check);
|
|
|
|
if (__libc_enable_secure && __access_noerrno ("/etc/suid-debug", F_OK) == 0)
|
|
|
|
tunable_list[id].security_level = TUNABLE_SECLEVEL_NONE;
|
2017-01-01 02:02:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Initialize the tunables list from the environment. For now we only use the
|
|
|
|
ENV_ALIAS to find values. Later we will also use the tunable names to find
|
|
|
|
values. */
|
|
|
|
void
|
|
|
|
__tunables_init (char **envp)
|
|
|
|
{
|
|
|
|
char *envname = NULL;
|
|
|
|
char *envval = NULL;
|
|
|
|
size_t len = 0;
|
2017-02-02 18:16:01 +08:00
|
|
|
char **prev_envp = envp;
|
2017-01-01 02:02:17 +08:00
|
|
|
|
2017-01-10 19:05:58 +08:00
|
|
|
maybe_enable_malloc_check ();
|
2017-01-01 02:02:17 +08:00
|
|
|
|
2017-02-02 18:16:01 +08:00
|
|
|
while ((envp = get_next_env (envp, &envname, &len, &envval,
|
|
|
|
&prev_envp)) != NULL)
|
2017-01-01 02:02:17 +08:00
|
|
|
{
|
2017-01-01 02:04:04 +08:00
|
|
|
#if TUNABLES_FRONTEND == TUNABLES_FRONTEND_valstring
|
2017-07-01 01:28:39 +08:00
|
|
|
if (tunable_is_name (GLIBC_TUNABLES, envname))
|
2017-01-01 02:03:27 +08:00
|
|
|
{
|
2017-02-02 18:16:01 +08:00
|
|
|
char *new_env = tunables_strdup (envname);
|
|
|
|
if (new_env != NULL)
|
|
|
|
parse_tunables (new_env + len + 1, envval);
|
|
|
|
/* Put in the updated envval. */
|
|
|
|
*prev_envp = new_env;
|
2017-01-01 02:03:27 +08:00
|
|
|
continue;
|
|
|
|
}
|
2017-01-01 02:04:04 +08:00
|
|
|
#endif
|
2017-01-01 02:03:27 +08:00
|
|
|
|
2017-01-01 02:02:17 +08:00
|
|
|
for (int i = 0; i < sizeof (tunable_list) / sizeof (tunable_t); i++)
|
|
|
|
{
|
|
|
|
tunable_t *cur = &tunable_list[i];
|
|
|
|
|
|
|
|
/* Skip over tunables that have either been set already or should be
|
|
|
|
skipped. */
|
2021-01-13 00:28:27 +08:00
|
|
|
if (cur->initialized || cur->env_alias[0] == '\0')
|
2017-01-01 02:02:17 +08:00
|
|
|
continue;
|
|
|
|
|
|
|
|
const char *name = cur->env_alias;
|
|
|
|
|
|
|
|
/* We have a match. Initialize and move on to the next line. */
|
2017-07-01 01:28:39 +08:00
|
|
|
if (tunable_is_name (name, envname))
|
2017-01-01 02:02:17 +08:00
|
|
|
{
|
2017-02-02 18:16:01 +08:00
|
|
|
/* For AT_SECURE binaries, we need to check the security settings of
|
|
|
|
the tunable and decide whether we read the value and also whether
|
|
|
|
we erase the value so that child processes don't inherit them in
|
|
|
|
the environment. */
|
|
|
|
if (__libc_enable_secure)
|
|
|
|
{
|
|
|
|
if (cur->security_level == TUNABLE_SECLEVEL_SXID_ERASE)
|
|
|
|
{
|
|
|
|
/* Erase the environment variable. */
|
|
|
|
char **ep = prev_envp;
|
|
|
|
|
|
|
|
while (*ep != NULL)
|
|
|
|
{
|
2017-07-01 01:28:39 +08:00
|
|
|
if (tunable_is_name (name, *ep))
|
2017-02-02 18:16:01 +08:00
|
|
|
{
|
|
|
|
char **dp = ep;
|
|
|
|
|
|
|
|
do
|
|
|
|
dp[0] = dp[1];
|
|
|
|
while (*dp++);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
++ep;
|
|
|
|
}
|
|
|
|
/* Reset the iterator so that we read the environment again
|
|
|
|
from the point we erased. */
|
|
|
|
envp = prev_envp;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cur->security_level != TUNABLE_SECLEVEL_NONE)
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2017-01-01 02:02:17 +08:00
|
|
|
tunable_initialize (cur, envval);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-12 21:04:53 +08:00
|
|
|
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 ();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-01 02:02:17 +08:00
|
|
|
/* Set the tunable value. This is called by the module that the tunable exists
|
|
|
|
in. */
|
|
|
|
void
|
tunables: Clean up hooks to get and set tunables
The TUNABLE_SET_VALUE and family of macros (and my later attempt to
add a TUNABLE_GET) never quite went together very well because the
overall interface was not clearly defined. This patch is an attempt
to do just that.
This patch consolidates the API to two simple sets of macros,
TUNABLE_GET* and TUNABLE_SET*. If TUNABLE_NAMESPACE is defined,
TUNABLE_GET takes just the tunable name, type and a (optionally NULL)
callback function to get the value of the tunable. The callback
function, if non-NULL, is called if the tunable was externally set
(i.e. via GLIBC_TUNABLES or any future mechanism). For example:
val = TUNABLE_GET (check, int32_t, check_callback)
returns the value of the glibc.malloc.check tunable (assuming
TUNABLE_NAMESPACE is set to malloc) as an int32_t into VAL after
calling check_callback.
Likewise, TUNABLE_SET can be used to set the value of the tunable,
although this is currently possible only in the dynamic linker before
it relocates itself. For example:
TUNABLE_SET (check, int32_t, 2)
will set glibc.malloc.check to 2. Of course, this is not possible
since we set (or read) glibc.malloc.check long after it is relocated.
To access or set a tunable outside of TUNABLE_NAMESPACE, use the
TUNABLE_GET_FULL and TUNABLE_SET_FULL macros, which have the following
prototype:
TUNABLE_GET_FULL (glibc, tune, hwcap_mask, uint64_t, NULL)
TUNABLE_SET_FULL (glibc, tune, hwcap_mask, uint64_t, 0xffff)
In future the tunable list may get split into mutable and immutable
tunables where mutable tunables can be modified by the library and
userspace after relocation as well and TUNABLE_SET will be more useful
than it currently is. However whenever we actually do that split, we
will have to ensure that the mutable tunables are protected with
locks.
* elf/Versions (__tunable_set_val): Rename to __tunable_get_val.
* elf/dl-tunables.c: Likewise.
(do_tunable_update_val): New function.
(__tunable_set_val): New function.
(__tunable_get_val): Call CB only if the tunable was externally
initialized.
(tunables_strtoul): Replace strval with initialized.
* elf/dl-tunables.h (strval): Replace with a bool initialized.
(TUNABLE_ENUM_NAME, TUNABLE_ENUM_NAME1): Adjust names to
prevent collision.
(__tunable_set_val): New function.
(TUNABLE_GET, TUNABLE_GET_FULL): New macros.
(TUNABLE_SET, TUNABLE_SET_FULL): Likewise.
(TUNABLE_SET_VAL): Remove.
(TUNABLE_SET_VAL_WITH_CALLBACK): Likewise.
* README.tunables: Document the new macros.
* malloc/arena.c (ptmalloc_init): Adjust.
2017-06-01 22:54:46 +08:00
|
|
|
__tunable_get_val (tunable_id_t id, void *valp, tunable_callback_t callback)
|
2017-01-01 02:02:17 +08:00
|
|
|
{
|
|
|
|
tunable_t *cur = &tunable_list[id];
|
|
|
|
|
|
|
|
switch (cur->type.type_code)
|
|
|
|
{
|
2017-05-17 15:41:55 +08:00
|
|
|
case TUNABLE_TYPE_UINT_64:
|
|
|
|
{
|
|
|
|
*((uint64_t *) valp) = (uint64_t) cur->val.numval;
|
|
|
|
break;
|
|
|
|
}
|
2017-01-01 02:02:17 +08:00
|
|
|
case TUNABLE_TYPE_INT_32:
|
|
|
|
{
|
|
|
|
*((int32_t *) valp) = (int32_t) cur->val.numval;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case TUNABLE_TYPE_SIZE_T:
|
|
|
|
{
|
|
|
|
*((size_t *) valp) = (size_t) cur->val.numval;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case TUNABLE_TYPE_STRING:
|
|
|
|
{
|
|
|
|
*((const char **)valp) = cur->val.strval;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
__builtin_unreachable ();
|
|
|
|
}
|
|
|
|
|
tunables: Clean up hooks to get and set tunables
The TUNABLE_SET_VALUE and family of macros (and my later attempt to
add a TUNABLE_GET) never quite went together very well because the
overall interface was not clearly defined. This patch is an attempt
to do just that.
This patch consolidates the API to two simple sets of macros,
TUNABLE_GET* and TUNABLE_SET*. If TUNABLE_NAMESPACE is defined,
TUNABLE_GET takes just the tunable name, type and a (optionally NULL)
callback function to get the value of the tunable. The callback
function, if non-NULL, is called if the tunable was externally set
(i.e. via GLIBC_TUNABLES or any future mechanism). For example:
val = TUNABLE_GET (check, int32_t, check_callback)
returns the value of the glibc.malloc.check tunable (assuming
TUNABLE_NAMESPACE is set to malloc) as an int32_t into VAL after
calling check_callback.
Likewise, TUNABLE_SET can be used to set the value of the tunable,
although this is currently possible only in the dynamic linker before
it relocates itself. For example:
TUNABLE_SET (check, int32_t, 2)
will set glibc.malloc.check to 2. Of course, this is not possible
since we set (or read) glibc.malloc.check long after it is relocated.
To access or set a tunable outside of TUNABLE_NAMESPACE, use the
TUNABLE_GET_FULL and TUNABLE_SET_FULL macros, which have the following
prototype:
TUNABLE_GET_FULL (glibc, tune, hwcap_mask, uint64_t, NULL)
TUNABLE_SET_FULL (glibc, tune, hwcap_mask, uint64_t, 0xffff)
In future the tunable list may get split into mutable and immutable
tunables where mutable tunables can be modified by the library and
userspace after relocation as well and TUNABLE_SET will be more useful
than it currently is. However whenever we actually do that split, we
will have to ensure that the mutable tunables are protected with
locks.
* elf/Versions (__tunable_set_val): Rename to __tunable_get_val.
* elf/dl-tunables.c: Likewise.
(do_tunable_update_val): New function.
(__tunable_set_val): New function.
(__tunable_get_val): Call CB only if the tunable was externally
initialized.
(tunables_strtoul): Replace strval with initialized.
* elf/dl-tunables.h (strval): Replace with a bool initialized.
(TUNABLE_ENUM_NAME, TUNABLE_ENUM_NAME1): Adjust names to
prevent collision.
(__tunable_set_val): New function.
(TUNABLE_GET, TUNABLE_GET_FULL): New macros.
(TUNABLE_SET, TUNABLE_SET_FULL): Likewise.
(TUNABLE_SET_VAL): Remove.
(TUNABLE_SET_VAL_WITH_CALLBACK): Likewise.
* README.tunables: Document the new macros.
* malloc/arena.c (ptmalloc_init): Adjust.
2017-06-01 22:54:46 +08:00
|
|
|
if (cur->initialized && callback != NULL)
|
2017-01-01 02:02:17 +08:00
|
|
|
callback (&cur->val);
|
|
|
|
}
|
2017-05-25 21:55:42 +08:00
|
|
|
|
tunables: Clean up hooks to get and set tunables
The TUNABLE_SET_VALUE and family of macros (and my later attempt to
add a TUNABLE_GET) never quite went together very well because the
overall interface was not clearly defined. This patch is an attempt
to do just that.
This patch consolidates the API to two simple sets of macros,
TUNABLE_GET* and TUNABLE_SET*. If TUNABLE_NAMESPACE is defined,
TUNABLE_GET takes just the tunable name, type and a (optionally NULL)
callback function to get the value of the tunable. The callback
function, if non-NULL, is called if the tunable was externally set
(i.e. via GLIBC_TUNABLES or any future mechanism). For example:
val = TUNABLE_GET (check, int32_t, check_callback)
returns the value of the glibc.malloc.check tunable (assuming
TUNABLE_NAMESPACE is set to malloc) as an int32_t into VAL after
calling check_callback.
Likewise, TUNABLE_SET can be used to set the value of the tunable,
although this is currently possible only in the dynamic linker before
it relocates itself. For example:
TUNABLE_SET (check, int32_t, 2)
will set glibc.malloc.check to 2. Of course, this is not possible
since we set (or read) glibc.malloc.check long after it is relocated.
To access or set a tunable outside of TUNABLE_NAMESPACE, use the
TUNABLE_GET_FULL and TUNABLE_SET_FULL macros, which have the following
prototype:
TUNABLE_GET_FULL (glibc, tune, hwcap_mask, uint64_t, NULL)
TUNABLE_SET_FULL (glibc, tune, hwcap_mask, uint64_t, 0xffff)
In future the tunable list may get split into mutable and immutable
tunables where mutable tunables can be modified by the library and
userspace after relocation as well and TUNABLE_SET will be more useful
than it currently is. However whenever we actually do that split, we
will have to ensure that the mutable tunables are protected with
locks.
* elf/Versions (__tunable_set_val): Rename to __tunable_get_val.
* elf/dl-tunables.c: Likewise.
(do_tunable_update_val): New function.
(__tunable_set_val): New function.
(__tunable_get_val): Call CB only if the tunable was externally
initialized.
(tunables_strtoul): Replace strval with initialized.
* elf/dl-tunables.h (strval): Replace with a bool initialized.
(TUNABLE_ENUM_NAME, TUNABLE_ENUM_NAME1): Adjust names to
prevent collision.
(__tunable_set_val): New function.
(TUNABLE_GET, TUNABLE_GET_FULL): New macros.
(TUNABLE_SET, TUNABLE_SET_FULL): Likewise.
(TUNABLE_SET_VAL): Remove.
(TUNABLE_SET_VAL_WITH_CALLBACK): Likewise.
* README.tunables: Document the new macros.
* malloc/arena.c (ptmalloc_init): Adjust.
2017-06-01 22:54:46 +08:00
|
|
|
rtld_hidden_def (__tunable_get_val)
|