From: DJ Delorie <dj@redhat.com>
To: Adhemerval Zanella <adhemerval.zanella@linaro.org>
Cc: libc-alpha@sourceware.org
Subject: Re: [PATCH v2 4/7] malloc: Decorate malloc maps
Date: Tue, 31 Oct 2023 22:28:55 -0400 [thread overview]
Message-ID: <xnh6m6ryu0.fsf@greed.delorie.com> (raw)
In-Reply-To: <20231028195559.390407-5-adhemerval.zanella@linaro.org>
LGTM
Reviewed-by: DJ Delorie <dj@redhat.com>
Adhemerval Zanella <adhemerval.zanella@linaro.org> writes:
> Add anonymous mmap annotations on loader malloc, malloc when it
> allocates memory with mmap, and on malloc arena. The /proc/self/maps
> will now print:
>
> [anon: glibc: malloc arena]
> [anon: glibc: malloc]
> [anon: glibc: loader malloc]
>
> On arena allocation, glibc annotates only the read/write mapping.
>
> Checked on x86_64-linux-gnu and aarch64-linux-gnu.
> ---
> elf/Makefile | 4 ++++
> elf/dl-minimal-malloc.c | 2 ++
> elf/tst-decorate-maps.c | 37 +++++++++++++++++++++++++++++++++++++
> malloc/arena.c | 4 ++++
> malloc/malloc.c | 5 +++++
> nptl/Makefile | 4 ++++
> 6 files changed, 56 insertions(+)
> diff --git a/elf/Makefile b/elf/Makefile
> +
> +tst-decorate-maps-ENV = \
> + GLIBC_TUNABLES=glibc.malloc.arena_max=8:glibc.malloc.mmap_threshold=1024
> +tst-decorate-maps-ARGS = 8
Ok.
> diff --git a/elf/dl-minimal-malloc.c b/elf/dl-minimal-malloc.c
> +#include <setvmaname.h>
Ok.
> MAP_ANON|MAP_PRIVATE, -1, 0);
> if (page == MAP_FAILED)
> return NULL;
> + __set_vma_name (page, nup, " glibc: loader malloc");
> if (page != alloc_end)
> alloc_ptr = page;
> alloc_end = page + nup;
Ok.
> diff --git a/elf/tst-decorate-maps.c b/elf/tst-decorate-maps.c
>
> +static int expected_n_arenas;
> +
> static void *
> tf (void *closure)
> {
> + void *p = xmalloc (1024);
> +
> /* Wait the thread startup, so thread stack is allocated. */
> xpthread_barrier_wait (&b);
>
> /* Wait the test to read the process mapiping. */
> +
> xpthread_barrier_wait (&b);
>
> + free (p);
> +
> return NULL;
> }
Ok. Enough to create an arena, not enough to mmap the chunk.
> @@ -48,6 +55,9 @@ struct proc_maps_t
> {
> int n_def_threads;
> int n_user_threads;
> + int n_arenas;
> + int n_malloc_mmap;
> + int n_loader_malloc_mmap;
> };
Ok.
> @@ -69,6 +79,12 @@ read_proc_maps (void)
> r.n_def_threads++;
> else if (strstr (line, "[anon: glibc: pthread user stack:") != NULL)
> r.n_user_threads++;
> + else if (strstr (line, "[anon: glibc: malloc arena]") != NULL)
> + r.n_arenas++;
> + else if (strstr (line, "[anon: glibc: malloc]") != NULL)
> + r.n_malloc_mmap++;
> + else if (strstr (line, "[anon: glibc: loader malloc]") != NULL)
> + r.n_loader_malloc_mmap++;
> }
> free (line);
> xfclose (f);
Ok.
> @@ -90,6 +106,9 @@ do_test_threads (bool set_guard)
>
> xpthread_barrier_init (&b, NULL, num_threads + 1);
>
> + /* Issue a large malloc to trigger a mmap call. */
> + void *p = xmalloc (256 * 1024);
> +
> pthread_t thr[num_threads];
> {
> int i = 0;
Ok.
> @@ -128,6 +147,10 @@ do_test_threads (bool set_guard)
> struct proc_maps_t r = read_proc_maps ();
> TEST_COMPARE (r.n_def_threads, num_def_threads);
> TEST_COMPARE (r.n_user_threads, num_user_threads);
> + TEST_COMPARE (r.n_arenas, expected_n_arenas);
> + TEST_COMPARE (r.n_malloc_mmap, 1);
> + /* On some architectures the loader might use more than one page. */
> + TEST_VERIFY (r.n_loader_malloc_mmap >= 1);
> }
Ok.
> /* Let the threads finish. */
> @@ -140,8 +163,22 @@ do_test_threads (bool set_guard)
> struct proc_maps_t r = read_proc_maps ();
> TEST_COMPARE (r.n_def_threads, 0);
> TEST_COMPARE (r.n_user_threads, 0);
> + TEST_COMPARE (r.n_arenas, expected_n_arenas);
> + TEST_COMPARE (r.n_malloc_mmap, 1);
> + TEST_VERIFY (r.n_loader_malloc_mmap >= 1);
> }
> +
> + free (p);
> +}
Ok.
> +static void
> +do_prepare (int argc, char *argv[])
> +{
> + TEST_VERIFY_EXIT (argc == 2);
> + expected_n_arenas = strtol (argv[1], NULL, 10);
> + expected_n_arenas = expected_n_arenas - 1;
> }
> +#define PREPARE do_prepare
Ok.
> diff --git a/malloc/arena.c b/malloc/arena.c
> index 6f03955ff2..d1e214ac2e 100644
> --- a/malloc/arena.c
> +++ b/malloc/arena.c
> @@ -17,6 +17,7 @@
> not, see <https://www.gnu.org/licenses/>. */
>
> #include <stdbool.h>
> +#include <setvmaname.h>
Ok.
> #define TUNABLE_NAMESPACE malloc
> #include <elf/dl-tunables.h>
> @@ -436,6 +437,9 @@ alloc_new_heap (size_t size, size_t top_pad, size_t pagesize,
> return 0;
> }
>
> + /* Only considere the actual usable range. */
> + __set_vma_name (p2, size, " glibc: malloc arena");
> +
> madvise_thp (p2, size);
>
Ok.
> diff --git a/malloc/malloc.c b/malloc/malloc.c
> #include <sys/sysinfo.h>
>
> #include <ldsodefs.h>
> +#include <setvmaname.h>
Ok.
> @@ -2428,6 +2429,8 @@ sysmalloc_mmap (INTERNAL_SIZE_T nb, size_t pagesize, int extra_flags, mstate av)
> madvise_thp (mm, size);
> #endif
>
> + __set_vma_name (mm, size, " glibc: malloc");
> +
Ok.
> @@ -2513,6 +2516,8 @@ sysmalloc_mmap_fallback (long int *s, INTERNAL_SIZE_T nb,
> madvise_thp (mbrk, size);
> #endif
>
> + __set_vma_name (mbrk, size, " glibc: malloc");
> +
Ok.
> diff --git a/nptl/Makefile b/nptl/Makefile
> +tst-pthread-proc-maps-ENV = \
> + GLIBC_TUNABLES=glibc.malloc.arena_max=8:glibc.malloc.mmap_threshold=1024
> +tst-pthread-proc-maps-ARGS = 8
> +
Ok.
next prev parent reply other threads:[~2023-11-01 2:28 UTC|newest]
Thread overview: 17+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-10-28 19:55 [PATCH v2 0/7] Add a tunable to decorate anonymous memory maps Adhemerval Zanella
2023-10-28 19:55 ` [PATCH v2 1/7] linux: Add PR_SET_VMA_ANON_NAME support Adhemerval Zanella
2023-10-31 20:03 ` DJ Delorie
2023-10-28 19:55 ` [PATCH v2 2/7] support: Add support_set_vma_name Adhemerval Zanella
2023-10-31 21:34 ` DJ Delorie
2023-11-01 11:58 ` Adhemerval Zanella Netto
2023-10-28 19:55 ` [PATCH v2 3/7] nptl: Decorate thread stack on pthread_create Adhemerval Zanella
2023-11-01 2:04 ` DJ Delorie
2023-10-28 19:55 ` [PATCH v2 4/7] malloc: Decorate malloc maps Adhemerval Zanella
2023-11-01 2:28 ` DJ Delorie [this message]
2023-10-28 19:55 ` [PATCH v2 5/7] assert: Decorate error message buffer Adhemerval Zanella
2023-11-01 2:31 ` DJ Delorie
2023-10-28 19:55 ` [PATCH v2 6/7] linux: Decorate __libc_fatal error buffer Adhemerval Zanella
2023-11-01 2:32 ` DJ Delorie
2023-10-28 19:55 ` [PATCH v2 7/7] elf: Add glibc.mem.decorate_maps tunable Adhemerval Zanella
2023-11-01 3:09 ` DJ Delorie
2023-11-01 12:53 ` Adhemerval Zanella Netto
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=xnh6m6ryu0.fsf@greed.delorie.com \
--to=dj@redhat.com \
--cc=adhemerval.zanella@linaro.org \
--cc=libc-alpha@sourceware.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).