From patchwork Wed Nov 1 12:56:11 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: 740066 Delivered-To: patch@linaro.org Received: by 2002:a5d:538f:0:b0:32d:baff:b0ca with SMTP id d15csp267744wrv; Wed, 1 Nov 2023 05:57:34 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHIA02xTauIB68mAUMQ7A3/TGsliOeyFCHDx4ny75A/B/4//hVRkOUQdkU8so9FXD4aeDHm X-Received: by 2002:a05:622a:345:b0:41e:173e:82b7 with SMTP id r5-20020a05622a034500b0041e173e82b7mr16751416qtw.66.1698843454188; Wed, 01 Nov 2023 05:57:34 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1698843454; cv=pass; d=google.com; s=arc-20160816; b=06dOVuyTcGYkxoOgsLCIR69hRuVjpdmilVP0WpIFaAF6XprN//s3Kjggl0guhIoLHA hJds7+QuoiQIVrWlfS99p+FrZ9mjVhxpFN8JGqq6n868ydlmxOTlnkb7iAOeuEyCyXdF EKIioc+4qpv44ASA/ixNDHk81aG41ZyWEgdSXb/fT6mWQ3F51eCsadeE2WvbTniVv1hw lDuQ7DVSHrgNO0WGkvUU12xCf7BMBfyCCfgDBGAohtTi3WGst7b9Qa4qWjTrAoE8q/sj 7tFb6gdB9OdGnEjTaOxZpwZfORR7dnIoXQ8xZ12UQHVI335H3gAvsqlCjoz+SGXgikuH tuHA== 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=qZWaRRzU8/sTrAhxTNqYWAPaTlsmh/sGkN6xGP2coh4=; fh=wOialSeEyZrL0Zmx/XQW8gMhKHOwjxYz8NEgiPSksGI=; b=ZdNbLXeA//utWyFOBPYLdKkpjT353rrnPII24xQ99AnNcAW22mtqFMbRT5w0KZruHo /F1exAeJGkyGMsYGJMCj/l/TqAz32fBZQrcUFuWrFGfqN5R3Irur/Kx5DZLLkMadBfPF gakYxIW+FZPGW/7WTynAmiIXxyLIGrO7Ocexra+iWdbRDIAifyh6V398GDG588T+OsGS CLrAIVz6FmxiRuezjrjgRT0qYHerJNtf8+dxUj/4ufKgYwjESm/9rzRHrvmtolcwq7FT 0KLOJFbFyyMs++npC0H3agau0XqGZKnPU/rhnTARw6ooSh6NTd+MMtaGFOY/QWLqmRT2 XTaQ== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=HTAfZhhl; 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 bp40-20020a05622a1ba800b0040fe2df28c6si2590370qtb.723.2023.11.01.05.57.33 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 01 Nov 2023 05:57:34 -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=HTAfZhhl; 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 390BF385771E for ; Wed, 1 Nov 2023 12:57:33 +0000 (GMT) X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-yb1-xb31.google.com (mail-yb1-xb31.google.com [IPv6:2607:f8b0:4864:20::b31]) by sourceware.org (Postfix) with ESMTPS id CCF4C3857705 for ; Wed, 1 Nov 2023 12:56:34 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org CCF4C3857705 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 CCF4C3857705 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::b31 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1698843405; cv=none; b=nD/eV7uh3BGl/ESy0H78OfAQrqjhdPxxjV58ZPt4Pj70qarSoGKX7NIJHvs2T44EFaEG1jTfnMiVQruTBGfP5vXRZEIMDA0hqcieV7eAXKnq0YUVdfBn4rdUWx3qgeJ3cYuW2gtrH0VRpaDJB4dqyfW66ySjp57uJl4xW6fBEzc= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1698843405; c=relaxed/simple; bh=2yE+axQLzm6gQZDSffqcCYC8eZg7lyq2yku0NikOPVU=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=cK9Vptpof1JJMG6q/MKPTnPsehHcjgYKBSkFTMlLAiK0HiVdOgOLTDAuVqIBTqj+HPB/2kHce661jQ0Ge/i/MbOLWiIyUTJ4yWjT4gioEGboC+/wc09xvo2ZZaV+og1jxlf7VzSbaXKvPkQLfJQbPIxaZwg+cbKzM1hWb4ytrQ8= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-yb1-xb31.google.com with SMTP id 3f1490d57ef6-d852b28ec3bso6244424276.2 for ; Wed, 01 Nov 2023 05:56:34 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1698843393; x=1699448193; 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=qZWaRRzU8/sTrAhxTNqYWAPaTlsmh/sGkN6xGP2coh4=; b=HTAfZhhl8q8RX4HxzFOScoM6PfIUPNPbzvH+i85jsZIhJtH4l07WSLbFcLea50kl/4 jw0cwPt6yeaFTiLEpml4Q41mX/C7WftrnXHAIg1ZKSI2EdP2mARwn/x7iRwptCSjxIpK X2lvIQnaADZ2DorOeEW2iB3yvTlB3U/mdz95KOJMwU2NhVy3NOG/2hB/0gOTnqvb3RGj OPEHQotDWAN/qKKSoCx4P+p5Mqjo1+g/WDbm2Gy+lex1vwrGFSMCP6nIUbsGuoOxYtxN /Mw/GDdmWKBYST4JbX2+I+PjoX8Gjth2zgniOv9jZyC/XHLM0QcyRMgdKnR3iDOcgGm+ iseA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698843393; x=1699448193; 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=qZWaRRzU8/sTrAhxTNqYWAPaTlsmh/sGkN6xGP2coh4=; b=TCyqqyx7NHPZx+R+JVryeJL6MuGoYc66XTpxwu2K6RLcX3igRKN/vGpvWPeGWlGRCs CPln/KRV0WYDMUbagLYKhdQdlMPzfd1ZEDD6hSamQkPvnnsC9tj+aZZOXBQensuv6Ede VaFwzpIL6P74zaHmOdzEz9Z2PgQj9GrqIwbIcyLdbm3HcCmB3w1EtlKo6nvkf/2faBk5 PNg3y5KwknmSASCGZQdfrldKm6zUZDkihDVHc47J2s1qq0MNCXX34JXwQ5ETM5LCHFL6 yIm98uAYgxTvnoHiUb7a4Ki7yRrMpLetKvc+K+aZ8rO4lO5Onu2gzuC8TT4FA3pKcYiU oIgQ== X-Gm-Message-State: AOJu0Yy7mD4MRfmT/SD7wx/6wJiTVlEEhQo9DGNZ2W0/RRDoRzOMNOB0 gmgMDjFZRyJQkvRx5FFnnDSQEVIZcdt57RUCWXk/IA== X-Received: by 2002:a25:aa85:0:b0:d9a:5244:32e5 with SMTP id t5-20020a25aa85000000b00d9a524432e5mr16703982ybi.35.1698843393494; Wed, 01 Nov 2023 05:56:33 -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.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 01 Nov 2023 05:56:32 -0700 (PDT) From: Adhemerval Zanella To: libc-alpha@sourceware.org, DJ Delorie , Ian Rogers , Francesco Nigro , Carlos O'Donell Subject: [PATCH v3 7/7] elf: Add glibc.mem.decorate_maps tunable Date: Wed, 1 Nov 2023 09:56:11 -0300 Message-Id: <20231101125611.208544-8-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.8 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 The PR_SET_VMA_ANON_NAME support is only enabled through a configurable kernel switch, mainly because assigning a name to a anonymous virtual memory area might prevent that area from being merged with adjacent virtual memory areas. For instance, with the following code: void *p1 = mmap (NULL, 1024 * 4096, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); void *p2 = mmap (p1 + (1024 * 4096), 1024 * 4096, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); The kernel will potentially merge both mappings resulting in only one segment of size 0x800000. If the segment is names with PR_SET_VMA_ANON_NAME with different names, it results in two mappings. Although this will unlikely be an issue for pthread stacks and malloc arenas (since for pthread stacks the guard page will result in a PROT_NONE segment, similar to the alignment requirement for the arena block), it still might prevent the mmap memory allocated for detail malloc. There is also another potential scalability issue, where the prctl requires to take the mmap global lock which is still not fully fixed in Linux [1] (for pthread stacks and arenas, it is mitigated by the stack cached and the arena reuse). So this patch disables anonymous mapping annotations as default and add a new tunable, glibc.mem.decorate_maps, can be used to enable it. [1] https://lwn.net/Articles/906852/ Checked on x86_64-linux-gnu and aarch64-linux-gnu. Reviewed-by: DJ Delorie --- NEWS | 5 +++++ elf/Makefile | 2 +- elf/dl-tunables.list | 5 +++++ manual/tunables.texi | 17 +++++++++++++++++ sysdeps/unix/sysv/linux/setvmaname.c | 15 ++++++++++----- 5 files changed, 38 insertions(+), 6 deletions(-) diff --git a/NEWS b/NEWS index 4580fe381d..139cfef1b0 100644 --- a/NEWS +++ b/NEWS @@ -38,6 +38,11 @@ Major new features: and the wfN format length modifiers for arguments pointing to types int_fastN_t or uint_fastN_t, as specified in draft ISO C2X. +* A new tunable, glibc.mem.decorate_maps, can be used to add additional + information on underlying memory allocated by the glibc (for instance, + on thread stack created by pthread_create or memory allocated by + malloc). + Deprecated and removed features, and other changes affecting compatibility: * The ldconfig program now skips file names containing ';' or ending in diff --git a/elf/Makefile b/elf/Makefile index 328dbe82de..f9bd86a05a 100644 --- a/elf/Makefile +++ b/elf/Makefile @@ -2985,5 +2985,5 @@ $(objpfx)tst-dlclose-lazy.out: \ $(objpfx)tst-decorate-maps: $(shared-thread-library) tst-decorate-maps-ENV = \ - GLIBC_TUNABLES=glibc.malloc.arena_max=8:glibc.malloc.mmap_threshold=1024 + GLIBC_TUNABLES=glibc.malloc.arena_max=8:glibc.malloc.mmap_threshold=1024:glibc.mem.decorate_maps=1 tst-decorate-maps-ARGS = 8 diff --git a/elf/dl-tunables.list b/elf/dl-tunables.list index 695ba7192e..888d2ede04 100644 --- a/elf/dl-tunables.list +++ b/elf/dl-tunables.list @@ -160,6 +160,11 @@ glibc { maxval: 255 security_level: SXID_IGNORE } + decorate_maps { + type: INT_32 + minval: 0 + maxval: 1 + } } rtld { diff --git a/manual/tunables.texi b/manual/tunables.texi index 776fd93fd9..c28360adcd 100644 --- a/manual/tunables.texi +++ b/manual/tunables.texi @@ -653,6 +653,23 @@ support in the kernel if this tunable has any non-zero value. The default value is @samp{0}, which disables all memory tagging. @end deftp +@deftp Tunable glibc.mem.decorate_maps +If the kernel supports naming anonymous virtual memory areas (since +Linux version 5.17, although not always enabled by some kernel +configurations), this tunable can be used to control whether +@theglibc{} decorates the underlying memory obtained from operating +system with a string describing its usage (for instance, on the thread +stack created by @code{ptthread_create} or memory allocated by +@code{malloc}). + +The process mappings can be obtained by reading the @code{/proc/maps} +(with @code{pid} being either the @dfn{process ID} or @code{self} for the +process own mapping). + +This tunable takes a value of 0 and 1, where 1 enables the feature. +The default value is @samp{0}, which disables the decoration. +@end deftp + @node gmon Tunables @section gmon Tunables @cindex gmon tunables diff --git a/sysdeps/unix/sysv/linux/setvmaname.c b/sysdeps/unix/sysv/linux/setvmaname.c index 9960ab5917..cd6d571772 100644 --- a/sysdeps/unix/sysv/linux/setvmaname.c +++ b/sysdeps/unix/sysv/linux/setvmaname.c @@ -20,6 +20,7 @@ #include #include #include +#include /* If PR_SET_VMA_ANON_NAME is not supported by the kernel, prctl returns EINVAL. However, it also returns the same error for invalid argument. @@ -34,11 +35,15 @@ __set_vma_name (void *start, size_t len, const char *name) if (atomic_load_relaxed (&prctl_supported) == 0) return; - int r = INTERNAL_SYSCALL_CALL (prctl, PR_SET_VMA, PR_SET_VMA_ANON_NAME, - start, len, name); - if (r == 0 || r != -EINVAL) - return; - + /* Set the prctl as not supported to avoid checking the tunable on every + call. */ + if (TUNABLE_GET (glibc, mem, decorate_maps, int32_t, NULL) != 0) + { + int r = INTERNAL_SYSCALL_CALL (prctl, PR_SET_VMA, PR_SET_VMA_ANON_NAME, + start, len, name); + if (r == 0 || r != -EINVAL) + return; + } atomic_store_relaxed (&prctl_supported, 0); return; }