From patchwork Thu Oct 26 17:11:43 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella Netto X-Patchwork-Id: 1855889 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=linaro.org header.i=@linaro.org header.a=rsa-sha256 header.s=google header.b=ahP/FPSC; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=sourceware.org (client-ip=8.43.85.97; helo=server2.sourceware.org; envelope-from=libc-alpha-bounces+incoming=patchwork.ozlabs.org@sourceware.org; receiver=patchwork.ozlabs.org) Received: from server2.sourceware.org (server2.sourceware.org [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4SGXSN0Kk2z202k for ; Fri, 27 Oct 2023 04:13:24 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 17994385DC17 for ; Thu, 26 Oct 2023 17:13:22 +0000 (GMT) X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-pf1-x432.google.com (mail-pf1-x432.google.com [IPv6:2607:f8b0:4864:20::432]) by sourceware.org (Postfix) with ESMTPS id 8ED383856965 for ; Thu, 26 Oct 2023 17:12:01 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 8ED383856965 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=linaro.org ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 8ED383856965 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::432 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1698340323; cv=none; b=AoOiLtrPXS2nYuHHx+YrfBu7gWz0SgyRvPrkjwoqefIUsoc3EQQjKp9GRjypIwzdIIkufy1291RIScxL3sqCl4Ky5e/IuiP9anj9yEjNuYbkel+aWq36W5UdFzXeIVsKLXFUynNnMyNoEeVWpkcqbF3En2RCc1yVWVDTnYo6PfU= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1698340323; c=relaxed/simple; bh=mJO+ueXBeLo99PiBCPtFZ0vvHPrgn066vjsi9RU+Nr4=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=HhKIAjS93Hma5NsHtJDdxlxQbNxvmS8vWVDkZA5Tfe8LAcBDUQJJT+tkC317j7s8C/f1OXxp75OwddFluMdx9d+iXobRe5Ys2JU7tB5f35c0IJk/GGEFh2kg3qQ3HnSJbcOfub/SkqV7hZiojjc4CUMbLkkznWPf/vopOc+UPlQ= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-pf1-x432.google.com with SMTP id d2e1a72fcca58-6b1ef786b7fso1126603b3a.3 for ; Thu, 26 Oct 2023 10:12:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1698340320; x=1698945120; darn=sourceware.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:from:to:cc:subject:date:message-id :reply-to; bh=wo3j3cgs16+wBEnOD2t8WWgHLIkkkYccGtYZ32ia+pE=; b=ahP/FPSCIPZGn+pv/LIH+a9PbK4HteWBxFxqvD7p+C39R/vIn9VpYZoFnnJVlnr9t/ uRlyMG36aU6NqxaKNjLUMCYRaX+10Cg02tN7InvDTnX9Pvf56GpfFjIrZlbAmuoMf3YX +nOiwPM6WMxoa6ncTvwZpjyM6CQiLhzrVvffordCTuhWpdWSF49qwcOcjhHiGLWiPXfN pvU78LW7MG7zKX2TciiawplxlW0HxTgSwOnzyK3rDOH/VNsoL/6YaqBqmzsCn7QvQkTh LHxpym9nWzuOQPK1Q3vATVpVhWb/2eK32lxzdmbAmkQfoKzxrKbVaL7XPYDrLoTxHjM2 t15A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698340320; x=1698945120; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=wo3j3cgs16+wBEnOD2t8WWgHLIkkkYccGtYZ32ia+pE=; b=HWOUXOcD+b+MYg67IF+bU81MaSf8zzrjufOK5wxe3+ArJkpwu1eHRGKEkwZiMMw/O8 kEF/djgn9v3HbsKPTMDHSGjvDSwT7tdtuOX6Fs2dm78yTmweS5svO6YFYsOtXcU2x9+4 nqKfYLs5T9SS6V9rW/BkN05SQEDT1mZEhPnnvU8Vr810kEqbajxSCk+b6Uy6qWZ8jbGK SOu6sm1KqprQADA9Ae8vWs/1Zy6hmZozM3sgPSnNJ6FNz3ViIrnC82bMMHSIyp8rb2+l ZaolZMlgv5Nrke+TbZcz0ZGhg8+0q4JjzTa9bYCxhqEois7Ca5ybEHLouwFbC5dZ4dv6 H4tA== X-Gm-Message-State: AOJu0YyOhJVK7EBJW3XjAvtx5Diz9wvzDaFVTt5dFKvmpdSUPOSWE9io 2kr+UdrbL9baUELmYP/bpm7cZUbAhwGNV1IvKOuXvg== X-Google-Smtp-Source: AGHT+IHpymdfiDZs7vzhMuE5Xf22k/VqdGTfdkcDTJfXoGv2piSh55wMDdYmmWGvP2CQwholKVIIFQ== X-Received: by 2002:a05:6a00:1a10:b0:6b1:cc77:4d2 with SMTP id g16-20020a056a001a1000b006b1cc7704d2mr135083pfv.15.1698340320076; Thu, 26 Oct 2023 10:12:00 -0700 (PDT) Received: from mandiga.. ([2804:1b3:a7c3:a647:ad90:6e37:bd13:fb33]) by smtp.gmail.com with ESMTPSA id w18-20020aa78592000000b006bee5ad4efasm11516394pfn.67.2023.10.26.10.11.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 26 Oct 2023 10:11:59 -0700 (PDT) From: Adhemerval Zanella To: libc-alpha@sourceware.org, Ian Rogers , Francesco Nigro , Carlos O'Donell , Siddhesh Poyarekar Subject: [PATCH 4/5] malloc: Decorate malloc maps Date: Thu, 26 Oct 2023 14:11:43 -0300 Message-Id: <20231026171144.2188549-5-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20231026171144.2188549-1-adhemerval.zanella@linaro.org> References: <20231026171144.2188549-1-adhemerval.zanella@linaro.org> MIME-Version: 1.0 X-Spam-Status: No, score=-12.5 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libc-alpha-bounces+incoming=patchwork.ozlabs.org@sourceware.org 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 ++++ sysdeps/aarch64/Makefile | 5 ++++- sysdeps/unix/sysv/linux/Makefile | 4 ++++ 8 files changed, 64 insertions(+), 1 deletion(-) diff --git a/elf/Makefile b/elf/Makefile index a82590703c..c3cf63a443 100644 --- a/elf/Makefile +++ b/elf/Makefile @@ -3021,3 +3021,7 @@ $(objpfx)tst-dlclose-lazy.out: \ $(objpfx)tst-dlclose-lazy-mod1.so $(objpfx)tst-dlclose-lazy-mod2.so $(objpfx)tst-decorate-maps: $(shared-thread-library) + +tst-decorate-maps-ENV = \ + GLIBC_TUNABLES=glibc.malloc.arena_max=8:glibc.malloc.mmap_threshold=1024 +tst-decorate-maps-ARGS = 8 diff --git a/elf/dl-minimal-malloc.c b/elf/dl-minimal-malloc.c index 27549645d0..da36986269 100644 --- a/elf/dl-minimal-malloc.c +++ b/elf/dl-minimal-malloc.c @@ -26,6 +26,7 @@ #include #include #include +#include static void *alloc_ptr, *alloc_end, *alloc_last_block; @@ -60,6 +61,7 @@ __minimal_malloc (size_t n) 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; diff --git a/elf/tst-decorate-maps.c b/elf/tst-decorate-maps.c index bbb7972094..bf508489c4 100644 --- a/elf/tst-decorate-maps.c +++ b/elf/tst-decorate-maps.c @@ -32,15 +32,22 @@ static pthread_barrier_t b; +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; } @@ -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; }; static struct proc_maps_t @@ -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); @@ -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; @@ -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); } /* 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); +} + +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 static int do_test (void) 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 . */ #include +#include #define TUNABLE_NAMESPACE malloc #include @@ -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); h = (heap_info *) p2; diff --git a/malloc/malloc.c b/malloc/malloc.c index d0bbbf3710..78a531bc7a 100644 --- a/malloc/malloc.c +++ b/malloc/malloc.c @@ -218,6 +218,7 @@ #include #include +#include #include #include /* needed for malloc_stats */ @@ -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"); + /* The offset to the start of the mmapped region is stored in the prev_size field of the chunk. This allows us to adjust returned start address to @@ -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"); + /* Record that we no longer have a contiguous sbrk region. After the first time mmap is used as backup, we do not ever rely on contiguous space since this could incorrectly bridge regions. */ diff --git a/nptl/Makefile b/nptl/Makefile index ffa5722e48..d969419af7 100644 --- a/nptl/Makefile +++ b/nptl/Makefile @@ -699,6 +699,10 @@ tst-audit-threads-ENV = LD_AUDIT=$(objpfx)tst-audit-threads-mod1.so tst-setuid1-static-ENV = \ LD_LIBRARY_PATH=$(ld-library-path):$(common-objpfx)elf:$(common-objpfx)nss +tst-pthread-proc-maps-ENV = \ + GLIBC_TUNABLES=glibc.malloc.arena_max=8:glibc.malloc.mmap_threshold=1024 +tst-pthread-proc-maps-ARGS = 8 + # The tests here better do not run in parallel. ifeq ($(run-built-tests),yes) ifneq ($(filter %tests,$(MAKECMDGOALS)),) diff --git a/sysdeps/aarch64/Makefile b/sysdeps/aarch64/Makefile index 6a9559e5f5..6c80ee8479 100644 --- a/sysdeps/aarch64/Makefile +++ b/sysdeps/aarch64/Makefile @@ -67,5 +67,8 @@ sysdep_routines += __mtag_tag_zero_region \ endif ifeq ($(subdir),malloc) -sysdep_malloc_debug_routines = __mtag_tag_zero_region __mtag_tag_region +sysdep_malloc_debug_routines += \ + __mtag_tag_zero_region \ + __mtag_tag_region \ + # sysdep_malloc_debug_routines endif diff --git a/sysdeps/unix/sysv/linux/Makefile b/sysdeps/unix/sysv/linux/Makefile index 250df6f455..2a8c49ca36 100644 --- a/sysdeps/unix/sysv/linux/Makefile +++ b/sysdeps/unix/sysv/linux/Makefile @@ -53,6 +53,10 @@ CFLAGS-assert-perr.c += -DFATAL_PREPARE_INCLUDE='' endif ifeq ($(subdir),malloc) +sysdep_malloc_debug_routines += \ + setvmaname \ + # sysdep_malloc_debug_routines + CFLAGS-malloc.c += -DMORECORE_CLEARS=2 endif