From patchwork Wed Nov 1 12:56:08 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: 740063 Delivered-To: patch@linaro.org Received: by 2002:a5d:538f:0:b0:32d:baff:b0ca with SMTP id d15csp267559wrv; Wed, 1 Nov 2023 05:57:07 -0700 (PDT) X-Google-Smtp-Source: AGHT+IE/1qI27OgceQxW4O6xs764P2Ccf94KwOpQqzGJF4kksI1M3liw7YIGNeGVIidxfJbqni5W X-Received: by 2002:a05:6808:2882:b0:3af:d7dc:a47e with SMTP id eu2-20020a056808288200b003afd7dca47emr15976241oib.42.1698843427534; Wed, 01 Nov 2023 05:57:07 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1698843427; cv=pass; d=google.com; s=arc-20160816; b=aPws3QINncvVmpS8VZIoro7oekiCX3jX/QmQDaz/pn8PAqj3TFZ2EdkNYod9tHOQY6 pXES7v+qbF/VIRj4BCRmlHPftc4u/0bKeNy7eXtEJHM6C1NmSCh+1vFspRf6RjGn/TLG UbFgrcy8H/xZGKzDmhYGhjB7/V9k1lRNC15P8OWFncs0gKyS+Vm/nmeYSmBCXTxjEUL/ TdxQLOZu9VJlWzC/1BskePUjza6PJvNKCMcbaZyt/m1cjzYZDNSQoIfrzpZbcJnSc+46 v3q8cjVklqHasVcHG7aqZRPozFxN78jcfurNpPOJsbSrdmMx2gsUoFcLzPh0qJuVESRl 7XaA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:to:from :dkim-signature:arc-filter:dmarc-filter:delivered-to; bh=5UU5ZCHspqZTvou61ny9pg0yAH56hmKfpVaGbMIT1qQ=; fh=wOialSeEyZrL0Zmx/XQW8gMhKHOwjxYz8NEgiPSksGI=; b=qFgQesTTIoMBJyh2H3RUainIA9dCgCZmex/fWXRxJ2kCRXYz03brpOTMlmicQHKSmz deXU/tR21pjok2x54z0rfa5MRtC5YBEaHOJUqgFPwifYUV1h2yYnMr/lGjpbAukkePK5 QGE7m3SoSbKnOvSRgEHxJivi7T1yLKxQOfpf3jEADuZxQlqYOTO9FOA7wqFB2FI56GSk IUbORRhfob353NWcmXYgVRL9Jc2iYuGpwWtPRwiBukGdivuW/FJEmC1OKpAKNunJpcE0 pK/fJo3MbGskaB4fXcqiYwfieps+YPJtdBVes5ND8ylrMKVTflXuNAz00fcBaQ3MF4c0 pR1Q== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=EGR3oH7J; arc=pass (i=1); spf=pass (google.com: domain of libc-alpha-bounces+patch=linaro.org@sourceware.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="libc-alpha-bounces+patch=linaro.org@sourceware.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from server2.sourceware.org (server2.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id c3-20020a05620a134300b007672b5d6f96si2642039qkl.17.2023.11.01.05.57.07 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 01 Nov 2023 05:57:07 -0700 (PDT) Received-SPF: pass (google.com: domain of libc-alpha-bounces+patch=linaro.org@sourceware.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=EGR3oH7J; arc=pass (i=1); spf=pass (google.com: domain of libc-alpha-bounces+patch=linaro.org@sourceware.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="libc-alpha-bounces+patch=linaro.org@sourceware.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 0ECAD385772F for ; Wed, 1 Nov 2023 12:57:07 +0000 (GMT) X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-yb1-xb30.google.com (mail-yb1-xb30.google.com [IPv6:2607:f8b0:4864:20::b30]) by sourceware.org (Postfix) with ESMTPS id E89BF3858415 for ; Wed, 1 Nov 2023 12:56:27 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org E89BF3858415 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 E89BF3858415 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::b30 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1698843398; cv=none; b=n5wAEisB3ymKsVHqT2kNjdkqnnV4ixLPt2GFKslA9RObhXUzxXxhQlobhxPz/uwNre7Bg9ONIsuDLs4caWgtKnj+U1bQiyKrLKX3siNVosKdvCt2OH6uTTnew5gkbInmerCp0guAKLcpM4x/x7d6e0SVX98yTY6DLV7jhQbCqwg= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1698843398; c=relaxed/simple; bh=NcvGmsowQEOUgp/21ZCaXbh7XDjjpADGSDeP9F0hqMg=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=gOatP2eYTBWzvBO4jstsS29FyPGg665aWtdQJJlmJaDpYRhIiZ/ZEpt9kvuqN972XQHgTPjFy0X/kei5+8VJNPlllDYzJxiEI2ltlf2thbMCp9lSDF2qsMDOnVPJ1yrcWGV07BXXASWhzGWf3DS60Sv6YVoZySZSindBfL2Mivk= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-yb1-xb30.google.com with SMTP id 3f1490d57ef6-d9c7bba32beso6394705276.1 for ; Wed, 01 Nov 2023 05:56:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1698843386; x=1699448186; 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=5UU5ZCHspqZTvou61ny9pg0yAH56hmKfpVaGbMIT1qQ=; b=EGR3oH7JtW0SZn0Gb/s9LhnXXWBeEIa9nNwPCwImqrWrgwoxxz1poeqlfwiWNwej6D DMuuygfU+A4qFUd98KQo0RK1VrE/XlwPNviDS7mahDaiASfQbs2aCDtf/p4x4fJNkaoJ vnzkIv9jDNCjL15Rz4RT18hBaOvwKQsTfIY0QjooNnuDn6MvJKycD1xfDzD2Pcqk1gHM 1z6wHfHLYFDS+nvG9DDXwhh4gxMQ6icDdOpwJ1GlKpCjcAuDnG+ehOMaO7ActAvD5ZAp e75HhpDDPKgjoCw4/LepnJWlQS19FrIf1tF09+KWsvF+2TS9XVcMhQTQJZctLV5VDhd+ uHnQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698843386; x=1699448186; 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=5UU5ZCHspqZTvou61ny9pg0yAH56hmKfpVaGbMIT1qQ=; b=GRKEsIjzuxHfA/WmsBGC6pHBWNfIIfAhBvpw4Fk1X0F7kgnCq0KauzBSYE0nnTlE/6 Q3eCZKSCVqXjg2qMgmfW8szR3rF7eotdBvR1yLk5eK56mYc8ITjySPQ39+gAX/mAMELU yOIfSx085pWwVgeCL6fSBGLO/jyy2C+9z/L6lfe/hTjmp5AIwEmClGzqryJ5kSOLWf9E 8uR60IIPadQGZBCHhgvqm4dWCmQHfeGffZKYnify7ZLIZxN11gLgsaWbkjC8GNGqV8+O /hwVHSeJydwWJjkpWyQs4FmwEjkX/9B1seNkkmU/GWBdBBc6MKP4WHFwhV0n/HC99/ZG L+hQ== X-Gm-Message-State: AOJu0YzMv+BgI8iy6OZlGicyEWzhhbdfA7I+TYXKyhXSyY21YlDbdpuK a+RDHCqEp5VbGD30+HaNmA3xmBmNEmdyoKIjL94YYA== X-Received: by 2002:a25:6b47:0:b0:da0:cd7b:5cb3 with SMTP id o7-20020a256b47000000b00da0cd7b5cb3mr15974309ybm.15.1698843386499; Wed, 01 Nov 2023 05:56:26 -0700 (PDT) Received: from mandiga.. ([2804:1b3:a7c0:a715:15ac:c4b6:7a0e:f5c5]) by smtp.gmail.com with ESMTPSA id w11-20020a5b08cb000000b00da034601e2esm1888100ybq.52.2023.11.01.05.56.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 01 Nov 2023 05:56:25 -0700 (PDT) From: Adhemerval Zanella To: libc-alpha@sourceware.org, DJ Delorie , Ian Rogers , Francesco Nigro , Carlos O'Donell Subject: [PATCH v3 4/7] malloc: Decorate malloc maps Date: Wed, 1 Nov 2023 09:56:08 -0300 Message-Id: <20231101125611.208544-5-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20231101125611.208544-1-adhemerval.zanella@linaro.org> References: <20231101125611.208544-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, T_SCC_BODY_TEXT_LINE 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+patch=linaro.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. Reviewed-by: DJ Delorie --- elf/Makefile | 4 ++++ elf/dl-minimal-malloc.c | 2 ++ elf/tst-decorate-maps.c | 36 ++++++++++++++++++++++++++++++++++++ malloc/arena.c | 4 ++++ malloc/malloc.c | 5 +++++ nptl/Makefile | 4 ++++ 6 files changed, 55 insertions(+) diff --git a/elf/Makefile b/elf/Makefile index 33f88bae96..328dbe82de 100644 --- a/elf/Makefile +++ b/elf/Makefile @@ -2983,3 +2983,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 b40a0e9b49..b0ea65ffd7 100644 --- a/elf/tst-decorate-maps.c +++ b/elf/tst-decorate-maps.c @@ -32,15 +32,21 @@ 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 mapping. */ xpthread_barrier_wait (&b); + free (p); + return NULL; } @@ -48,6 +54,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 +78,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 +105,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 +146,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 +162,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)),)